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, struct sctp_tcb *stcb,
85 int *abort_no_unlock, uint32_t vrf_id, uint16_t port)
87 struct sctp_init *init;
90 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
96 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
97 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
98 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
101 *abort_no_unlock = 1;
104 /* validate parameters */
106 if (init->initiate_tag == 0) {
107 /* protocol error... send abort */
108 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
109 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
112 *abort_no_unlock = 1;
115 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
116 /* invalid parameter... send abort */
117 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
118 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
121 *abort_no_unlock = 1;
124 if (init->num_inbound_streams == 0) {
125 /* protocol error... send abort */
126 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
127 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
130 *abort_no_unlock = 1;
133 if (init->num_outbound_streams == 0) {
134 /* protocol error... send abort */
135 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
136 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
139 *abort_no_unlock = 1;
142 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
143 offset + ntohs(cp->ch.chunk_length))) {
144 /* auth parameter(s) error... send abort */
145 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
147 *abort_no_unlock = 1;
151 * We are only accepting if we have a socket with positive
154 if ((stcb == NULL) &&
155 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
156 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
157 (inp->sctp_socket == NULL) ||
158 (inp->sctp_socket->so_qlimit == 0))) {
160 * FIX ME ?? What about TCP model and we have a
161 * match/restart case? Actually no fix is needed. the lookup
162 * will always find the existing assoc so stcb would not be
163 * NULL. It may be questionable to do this since we COULD
164 * just send back the INIT-ACK and hope that the app did
165 * accept()'s by the time the COOKIE was sent. But there is
166 * a price to pay for COOKIE generation and I don't want to
167 * pay it on the chance that the app will actually do some
168 * accepts(). The App just looses and should NOT be in this
171 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
172 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
176 if ((stcb != NULL) &&
177 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
178 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
179 sctp_send_shutdown_ack(stcb, NULL);
180 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
182 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
183 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
184 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
188 SCTP_INP_RUNLOCK(inp);
193 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
197 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
198 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
205 struct sctp_stream_queue_pending *sp;
206 struct sctp_association *asoc;
209 * This function returns the number of streams that have true unsent
210 * data on them. Note that as it looks through it will clean up any
211 * places that have old data that has been sent but left at top of
215 SCTP_TCB_SEND_LOCK(stcb);
216 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
217 /* Check to see if some data queued */
218 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
219 /* sa_ignore FREED_MEMORY */
220 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
224 if ((sp->msg_is_complete) &&
226 (sp->sender_all_done)) {
228 * We are doing differed cleanup. Last time
229 * through when we took all the data the
230 * sender_all_done was not set.
232 if (sp->put_last_out == 0) {
233 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
234 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
240 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
241 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
243 sctp_free_remote_addr(sp->net);
247 sctp_m_freem(sp->data);
250 sctp_free_a_strmoq(stcb, sp, so_locked);
257 SCTP_TCB_SEND_UNLOCK(stcb);
258 return (unsent_data);
262 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
264 struct sctp_init *init;
265 struct sctp_association *asoc;
266 struct sctp_nets *lnet;
271 /* save off parameters */
272 asoc->peer_vtag = ntohl(init->initiate_tag);
273 asoc->peers_rwnd = ntohl(init->a_rwnd);
275 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
277 if (!TAILQ_EMPTY(&asoc->nets)) {
278 /* update any ssthresh's that may have a default */
279 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
280 lnet->ssthresh = asoc->peers_rwnd;
281 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
282 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
286 SCTP_TCB_SEND_LOCK(stcb);
287 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
289 struct sctp_stream_out *outs;
290 struct sctp_stream_queue_pending *sp, *nsp;
291 struct sctp_tmit_chunk *chk, *nchk;
293 /* abandon the upper streams */
294 newcnt = ntohs(init->num_inbound_streams);
295 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
296 if (chk->rec.data.stream_number >= newcnt) {
297 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
298 asoc->send_queue_cnt--;
299 if (chk->data != NULL) {
300 sctp_free_bufspace(stcb, asoc, chk, 1);
301 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
302 0, chk, SCTP_SO_NOT_LOCKED);
304 sctp_m_freem(chk->data);
308 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
309 /* sa_ignore FREED_MEMORY */
313 for (i = newcnt; i < asoc->pre_open_streams; i++) {
314 outs = &asoc->strmout[i];
315 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
316 TAILQ_REMOVE(&outs->outqueue, sp, next);
317 asoc->stream_queue_cnt--;
318 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
319 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
321 sctp_m_freem(sp->data);
325 sctp_free_remote_addr(sp->net);
329 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
330 /* sa_ignore FREED_MEMORY */
334 /* cut back the count */
335 asoc->pre_open_streams = newcnt;
337 SCTP_TCB_SEND_UNLOCK(stcb);
338 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
340 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
341 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
342 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
343 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
345 /* This is the next one we expect */
346 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
348 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
349 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
351 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
352 /* open the requested streams */
354 if (asoc->strmin != NULL) {
355 /* Free the old ones */
356 struct sctp_queued_to_read *ctl, *nctl;
358 for (i = 0; i < asoc->streamincnt; i++) {
359 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
360 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
361 sctp_free_remote_addr(ctl->whoFrom);
363 sctp_m_freem(ctl->data);
365 sctp_free_a_readq(stcb, ctl);
368 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
370 asoc->streamincnt = ntohs(init->num_outbound_streams);
371 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
372 asoc->streamincnt = MAX_SCTP_STREAMS;
374 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
375 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
376 if (asoc->strmin == NULL) {
377 /* we didn't get memory for the streams! */
378 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
381 for (i = 0; i < asoc->streamincnt; i++) {
382 asoc->strmin[i].stream_no = i;
383 asoc->strmin[i].last_sequence_delivered = 0xffff;
385 * U-stream ranges will be set when the cookie is unpacked.
386 * Or for the INIT sender they are un set (if pr-sctp not
387 * supported) when the INIT-ACK arrives.
389 TAILQ_INIT(&asoc->strmin[i].inqueue);
390 asoc->strmin[i].delivery_started = 0;
393 * load_address_from_init will put the addresses into the
394 * association when the COOKIE is processed or the INIT-ACK is
395 * processed. Both types of COOKIE's existing and new call this
396 * routine. It will remove addresses that are no longer in the
397 * association (for the restarting case where addresses are
398 * removed). Up front when the INIT arrives we will discard it if it
399 * is a restart and new addresses have been added.
401 /* sa_ignore MEMLEAK */
406 * INIT-ACK message processing/consumption returns value < 0 on error
409 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
410 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
411 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
413 struct sctp_association *asoc;
415 int retval, abort_flag;
416 uint32_t initack_limit;
417 int nat_friendly = 0;
419 /* First verify that we have no illegal param's */
423 op_err = sctp_arethere_unrecognized_parameters(m,
424 (offset + sizeof(struct sctp_init_chunk)),
425 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
427 /* Send an abort and notify peer */
428 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
429 *abort_no_unlock = 1;
433 asoc->peer_supports_nat = (uint8_t) nat_friendly;
434 /* process the peer's parameters in the INIT-ACK */
435 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
439 initack_limit = offset + ntohs(cp->ch.chunk_length);
440 /* load all addresses */
441 if ((retval = sctp_load_addresses_from_init(stcb, m,
442 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
444 /* Huh, we should abort */
445 SCTPDBG(SCTP_DEBUG_INPUT1,
446 "Load addresses from INIT causes an abort %d\n",
448 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
450 *abort_no_unlock = 1;
453 /* if the peer doesn't support asconf, flush the asconf queue */
454 if (asoc->peer_supports_asconf == 0) {
455 struct sctp_asconf_addr *param, *nparam;
457 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
458 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
459 SCTP_FREE(param, SCTP_M_ASC_ADDR);
462 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
463 stcb->asoc.local_hmacs);
465 sctp_queue_op_err(stcb, op_err);
466 /* queuing will steal away the mbuf chain to the out queue */
469 /* extract the cookie and queue it to "echo" it back... */
470 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
471 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
472 stcb->asoc.overall_error_count,
474 SCTP_FROM_SCTP_INPUT,
477 stcb->asoc.overall_error_count = 0;
478 net->error_count = 0;
481 * Cancel the INIT timer, We do this first before queueing the
482 * cookie. We always cancel at the primary to assue that we are
483 * canceling the timer started by the INIT which always goes to the
486 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
487 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
489 /* calculate the RTO */
490 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
491 SCTP_RTT_FROM_NON_DATA);
493 retval = sctp_send_cookie_echo(m, offset, stcb, net);
496 * No cookie, we probably should send a op error. But in any
497 * case if there is no cookie in the INIT-ACK, we can
498 * abandon the peer, its broke.
501 /* We abort with an error of missing mandatory param */
503 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
506 * Expand beyond to include the mandatory
509 struct sctp_inv_mandatory_param *mp;
511 SCTP_BUF_LEN(op_err) =
512 sizeof(struct sctp_inv_mandatory_param);
514 struct sctp_inv_mandatory_param *);
515 /* Subtract the reserved param */
517 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
518 mp->num_param = htonl(1);
519 mp->param = htons(SCTP_STATE_COOKIE);
522 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
523 sh, op_err, vrf_id, net->port);
524 *abort_no_unlock = 1;
532 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
533 struct sctp_tcb *stcb, struct sctp_nets *net)
535 struct sockaddr_storage store;
536 struct sctp_nets *r_net, *f_net;
539 uint16_t old_error_counter;
542 struct sockaddr_in *sin;
546 struct sockaddr_in6 *sin6;
550 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
554 memset(&store, 0, sizeof(store));
555 switch (cp->heartbeat.hb_info.addr_family) {
558 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
559 sin = (struct sockaddr_in *)&store;
560 sin->sin_family = cp->heartbeat.hb_info.addr_family;
561 sin->sin_len = cp->heartbeat.hb_info.addr_len;
562 sin->sin_port = stcb->rport;
563 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
564 sizeof(sin->sin_addr));
572 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
573 sin6 = (struct sockaddr_in6 *)&store;
574 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
575 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
576 sin6->sin6_port = stcb->rport;
577 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
578 sizeof(sin6->sin6_addr));
587 r_net = sctp_findnet(stcb, (struct sockaddr *)&store);
589 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
592 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
593 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
594 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
596 * If the its a HB and it's random value is correct when can
597 * confirm the destination.
599 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
600 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
601 stcb->asoc.primary_destination = r_net;
602 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
603 f_net = TAILQ_FIRST(&stcb->asoc.nets);
604 if (f_net != r_net) {
606 * first one on the list is NOT the primary
607 * sctp_cmpaddr() is much more efficent if
608 * the primary is the first on the list,
611 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
612 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
616 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
617 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
618 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
619 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
621 old_error_counter = r_net->error_count;
622 r_net->error_count = 0;
623 r_net->hb_responded = 1;
624 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
625 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
626 /* Now lets do a RTO with this */
627 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
628 SCTP_RTT_FROM_NON_DATA);
629 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
630 r_net->dest_state |= SCTP_ADDR_REACHABLE;
631 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
632 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
634 if (r_net->dest_state & SCTP_ADDR_PF) {
635 r_net->dest_state &= ~SCTP_ADDR_PF;
636 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
638 if (old_error_counter > 0) {
639 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
640 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
642 if (r_net == stcb->asoc.primary_destination) {
643 if (stcb->asoc.alternate) {
644 /* release the alternate, primary is good */
645 sctp_free_remote_addr(stcb->asoc.alternate);
646 stcb->asoc.alternate = NULL;
649 /* Mobility adaptation */
651 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
652 SCTP_MOBILITY_BASE) ||
653 sctp_is_mobility_feature_on(stcb->sctp_ep,
654 SCTP_MOBILITY_FASTHANDOFF)) &&
655 sctp_is_mobility_feature_on(stcb->sctp_ep,
656 SCTP_MOBILITY_PRIM_DELETED)) {
658 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
659 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
660 SCTP_MOBILITY_FASTHANDOFF)) {
661 sctp_assoc_immediate_retrans(stcb,
662 stcb->asoc.primary_destination);
664 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
665 SCTP_MOBILITY_BASE)) {
666 sctp_move_chunks_from_net(stcb,
667 stcb->asoc.deleted_primary);
669 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
670 stcb->asoc.deleted_primary);
676 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
679 * return 0 means we want you to proceed with the abort non-zero
680 * means no abort processing
682 struct sctpasochead *head;
684 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
685 /* generate a new vtag and send init */
686 LIST_REMOVE(stcb, sctp_asocs);
687 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
688 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
690 * put it in the bucket in the vtag hash of assoc's for the
693 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
694 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
697 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
699 * treat like a case where the cookie expired i.e.: - dump
700 * current cookie. - generate a new vtag. - resend init.
702 /* generate a new vtag and send init */
703 LIST_REMOVE(stcb, sctp_asocs);
704 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
705 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
706 sctp_stop_all_cookie_timers(stcb);
707 sctp_toss_old_cookies(stcb, &stcb->asoc);
708 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
709 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
711 * put it in the bucket in the vtag hash of assoc's for the
714 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
715 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
722 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
723 struct sctp_nets *net)
726 * return 0 means we want you to proceed with the abort non-zero
727 * means no abort processing
729 if (stcb->asoc.peer_supports_auth == 0) {
730 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
733 sctp_asconf_send_nat_state_update(stcb, net);
739 sctp_handle_abort(struct sctp_abort_chunk *abort,
740 struct sctp_tcb *stcb, struct sctp_nets *net)
742 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
749 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
753 len = ntohs(abort->ch.chunk_length);
754 if (len > sizeof(struct sctp_chunkhdr)) {
756 * Need to check the cause codes for our two magic nat
757 * aborts which don't kill the assoc necessarily.
759 struct sctp_missing_nat_state *natc;
761 natc = (struct sctp_missing_nat_state *)(abort + 1);
762 error = ntohs(natc->cause);
763 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
764 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
765 abort->ch.chunk_flags);
766 if (sctp_handle_nat_colliding_state(stcb)) {
769 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
770 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
771 abort->ch.chunk_flags);
772 if (sctp_handle_nat_missing_state(stcb, net)) {
779 /* stop any receive timers */
780 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
781 /* notify user of the abort and clean up... */
782 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
784 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
785 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
786 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
787 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
789 #ifdef SCTP_ASOCLOG_OF_TSNS
790 sctp_print_out_track_log(stcb);
792 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
793 so = SCTP_INP_SO(stcb->sctp_ep);
794 atomic_add_int(&stcb->asoc.refcnt, 1);
795 SCTP_TCB_UNLOCK(stcb);
796 SCTP_SOCKET_LOCK(so, 1);
798 atomic_subtract_int(&stcb->asoc.refcnt, 1);
800 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
801 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
802 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
803 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
804 SCTP_SOCKET_UNLOCK(so, 1);
806 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
810 sctp_start_net_timers(struct sctp_tcb *stcb)
812 uint32_t cnt_hb_sent;
813 struct sctp_nets *net;
816 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
818 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
819 * If the dest in unconfirmed send a hb as well if under
820 * max_hb_burst have been sent.
822 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
823 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
824 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
825 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
826 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
831 sctp_chunk_output(stcb->sctp_ep, stcb,
832 SCTP_OUTPUT_FROM_COOKIE_ACK,
839 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
840 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
842 struct sctp_association *asoc;
843 int some_on_streamwheel;
845 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
850 SCTPDBG(SCTP_DEBUG_INPUT2,
851 "sctp_handle_shutdown: handling SHUTDOWN\n");
855 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
856 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
859 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
860 /* Shutdown NOT the expected size */
863 sctp_update_acked(stcb, cp, abort_flag);
868 if (asoc->control_pdapi) {
870 * With a normal shutdown we assume the end of last record.
872 SCTP_INP_READ_LOCK(stcb->sctp_ep);
873 asoc->control_pdapi->end_added = 1;
874 asoc->control_pdapi->pdapi_aborted = 1;
875 asoc->control_pdapi = NULL;
876 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
877 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
878 so = SCTP_INP_SO(stcb->sctp_ep);
879 atomic_add_int(&stcb->asoc.refcnt, 1);
880 SCTP_TCB_UNLOCK(stcb);
881 SCTP_SOCKET_LOCK(so, 1);
883 atomic_subtract_int(&stcb->asoc.refcnt, 1);
884 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
885 /* assoc was freed while we were unlocked */
886 SCTP_SOCKET_UNLOCK(so, 1);
890 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
891 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
892 SCTP_SOCKET_UNLOCK(so, 1);
895 /* goto SHUTDOWN_RECEIVED state to block new requests */
896 if (stcb->sctp_socket) {
897 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
898 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
899 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
900 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
901 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
903 * notify upper layer that peer has initiated a
906 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
909 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
912 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
914 * stop the shutdown timer, since we WILL move to
917 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
919 /* Now is there unsent data on a stream somewhere? */
920 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
922 if (!TAILQ_EMPTY(&asoc->send_queue) ||
923 !TAILQ_EMPTY(&asoc->sent_queue) ||
924 some_on_streamwheel) {
925 /* By returning we will push more data out */
928 /* no outstanding data to send, so move on... */
929 /* send SHUTDOWN-ACK */
930 sctp_send_shutdown_ack(stcb, net);
931 /* move to SHUTDOWN-ACK-SENT state */
932 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
933 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
934 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
936 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
937 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
938 sctp_stop_timers_for_shutdown(stcb);
939 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
945 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
946 struct sctp_tcb *stcb,
947 struct sctp_nets *net)
949 struct sctp_association *asoc;
951 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
954 so = SCTP_INP_SO(stcb->sctp_ep);
956 SCTPDBG(SCTP_DEBUG_INPUT2,
957 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
962 /* process according to association state */
963 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
964 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
965 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
966 sctp_send_shutdown_complete(stcb, net, 1);
967 SCTP_TCB_UNLOCK(stcb);
970 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
971 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
972 /* unexpected SHUTDOWN-ACK... so ignore... */
973 SCTP_TCB_UNLOCK(stcb);
976 if (asoc->control_pdapi) {
978 * With a normal shutdown we assume the end of last record.
980 SCTP_INP_READ_LOCK(stcb->sctp_ep);
981 asoc->control_pdapi->end_added = 1;
982 asoc->control_pdapi->pdapi_aborted = 1;
983 asoc->control_pdapi = NULL;
984 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
985 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
986 atomic_add_int(&stcb->asoc.refcnt, 1);
987 SCTP_TCB_UNLOCK(stcb);
988 SCTP_SOCKET_LOCK(so, 1);
990 atomic_subtract_int(&stcb->asoc.refcnt, 1);
991 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
992 /* assoc was freed while we were unlocked */
993 SCTP_SOCKET_UNLOCK(so, 1);
997 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
998 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
999 SCTP_SOCKET_UNLOCK(so, 1);
1002 /* are the queues empty? */
1003 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1004 !TAILQ_EMPTY(&asoc->sent_queue) ||
1005 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1006 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
1008 /* stop the timer */
1009 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
1010 /* send SHUTDOWN-COMPLETE */
1011 sctp_send_shutdown_complete(stcb, net, 0);
1012 /* notify upper layer protocol */
1013 if (stcb->sctp_socket) {
1014 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1015 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1016 stcb->sctp_socket->so_snd.sb_cc = 0;
1018 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1020 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1021 /* free the TCB but first save off the ep */
1022 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1023 atomic_add_int(&stcb->asoc.refcnt, 1);
1024 SCTP_TCB_UNLOCK(stcb);
1025 SCTP_SOCKET_LOCK(so, 1);
1026 SCTP_TCB_LOCK(stcb);
1027 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1029 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1030 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1031 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1032 SCTP_SOCKET_UNLOCK(so, 1);
1037 * Skip past the param header and then we will find the chunk that caused the
1038 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1039 * our peer must be broken.
1042 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1043 struct sctp_nets *net)
1045 struct sctp_chunkhdr *chk;
1047 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1048 switch (chk->chunk_type) {
1049 case SCTP_ASCONF_ACK:
1051 sctp_asconf_cleanup(stcb, net);
1053 case SCTP_FORWARD_CUM_TSN:
1054 stcb->asoc.peer_supports_prsctp = 0;
1057 SCTPDBG(SCTP_DEBUG_INPUT2,
1058 "Peer does not support chunk type %d(%x)??\n",
1059 chk->chunk_type, (uint32_t) chk->chunk_type);
1065 * Skip past the param header and then we will find the param that caused the
1066 * problem. There are a number of param's in a ASCONF OR the prsctp param
1067 * these will turn of specific features.
1070 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1072 struct sctp_paramhdr *pbad;
1075 switch (ntohs(pbad->param_type)) {
1077 case SCTP_PRSCTP_SUPPORTED:
1078 stcb->asoc.peer_supports_prsctp = 0;
1080 case SCTP_SUPPORTED_CHUNK_EXT:
1082 /* draft-ietf-tsvwg-addip-sctp */
1083 case SCTP_HAS_NAT_SUPPORT:
1084 stcb->asoc.peer_supports_nat = 0;
1086 case SCTP_ADD_IP_ADDRESS:
1087 case SCTP_DEL_IP_ADDRESS:
1088 case SCTP_SET_PRIM_ADDR:
1089 stcb->asoc.peer_supports_asconf = 0;
1091 case SCTP_SUCCESS_REPORT:
1092 case SCTP_ERROR_CAUSE_IND:
1093 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1094 SCTPDBG(SCTP_DEBUG_INPUT2,
1095 "Turning off ASCONF to this strange peer\n");
1096 stcb->asoc.peer_supports_asconf = 0;
1099 SCTPDBG(SCTP_DEBUG_INPUT2,
1100 "Peer does not support param type %d(%x)??\n",
1101 pbad->param_type, (uint32_t) pbad->param_type);
1107 sctp_handle_error(struct sctp_chunkhdr *ch,
1108 struct sctp_tcb *stcb, struct sctp_nets *net)
1111 struct sctp_paramhdr *phdr;
1112 uint16_t error, error_type;
1114 struct sctp_association *asoc;
1117 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1122 /* parse through all of the errors and process */
1124 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1125 sizeof(struct sctp_chunkhdr));
1126 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1128 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1129 /* Process an Error Cause */
1130 error_type = ntohs(phdr->param_type);
1131 error_len = ntohs(phdr->param_length);
1132 if ((error_len > chklen) || (error_len == 0)) {
1133 /* invalid param length for this param */
1134 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1139 /* report the first error cause */
1142 switch (error_type) {
1143 case SCTP_CAUSE_INVALID_STREAM:
1144 case SCTP_CAUSE_MISSING_PARAM:
1145 case SCTP_CAUSE_INVALID_PARAM:
1146 case SCTP_CAUSE_NO_USER_DATA:
1147 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1150 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1151 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1153 if (sctp_handle_nat_colliding_state(stcb)) {
1157 case SCTP_CAUSE_NAT_MISSING_STATE:
1158 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1160 if (sctp_handle_nat_missing_state(stcb, net)) {
1164 case SCTP_CAUSE_STALE_COOKIE:
1166 * We only act if we have echoed a cookie and are
1169 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1172 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1173 /* Save the time doubled */
1174 asoc->cookie_preserve_req = ntohl(*p) << 1;
1175 asoc->stale_cookie_count++;
1176 if (asoc->stale_cookie_count >
1177 asoc->max_init_times) {
1178 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1179 /* now free the asoc */
1180 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1181 so = SCTP_INP_SO(stcb->sctp_ep);
1182 atomic_add_int(&stcb->asoc.refcnt, 1);
1183 SCTP_TCB_UNLOCK(stcb);
1184 SCTP_SOCKET_LOCK(so, 1);
1185 SCTP_TCB_LOCK(stcb);
1186 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1188 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1189 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1190 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1191 SCTP_SOCKET_UNLOCK(so, 1);
1195 /* blast back to INIT state */
1196 sctp_toss_old_cookies(stcb, &stcb->asoc);
1197 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1198 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1199 sctp_stop_all_cookie_timers(stcb);
1200 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1203 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1205 * Nothing we can do here, we don't do hostname
1206 * addresses so if the peer does not like my IPv6
1207 * (or IPv4 for that matter) it does not matter. If
1208 * they don't support that type of address, they can
1209 * NOT possibly get that packet type... i.e. with no
1210 * IPv6 you can't recieve a IPv6 packet. so we can
1211 * safely ignore this one. If we ever added support
1212 * for HOSTNAME Addresses, then we would need to do
1216 case SCTP_CAUSE_UNRECOG_CHUNK:
1217 sctp_process_unrecog_chunk(stcb, phdr, net);
1219 case SCTP_CAUSE_UNRECOG_PARAM:
1220 sctp_process_unrecog_param(stcb, phdr);
1222 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1224 * We ignore this since the timer will drive out a
1225 * new cookie anyway and there timer will drive us
1226 * to send a SHUTDOWN_COMPLETE. We can't send one
1227 * here since we don't have their tag.
1230 case SCTP_CAUSE_DELETING_LAST_ADDR:
1231 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1232 case SCTP_CAUSE_DELETING_SRC_ADDR:
1234 * We should NOT get these here, but in a
1237 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1240 case SCTP_CAUSE_OUT_OF_RESC:
1242 * And what, pray tell do we do with the fact that
1243 * the peer is out of resources? Not really sure we
1244 * could do anything but abort. I suspect this
1245 * should have came WITH an abort instead of in a
1250 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1254 adjust = SCTP_SIZE32(error_len);
1256 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1258 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1263 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1264 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1265 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1267 struct sctp_init_ack *init_ack;
1268 struct mbuf *op_err;
1270 SCTPDBG(SCTP_DEBUG_INPUT2,
1271 "sctp_handle_init_ack: handling INIT-ACK\n");
1274 SCTPDBG(SCTP_DEBUG_INPUT2,
1275 "sctp_handle_init_ack: TCB is null\n");
1278 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1279 /* Invalid length */
1280 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1281 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1282 op_err, 0, net->port);
1283 *abort_no_unlock = 1;
1286 init_ack = &cp->init;
1287 /* validate parameters */
1288 if (init_ack->initiate_tag == 0) {
1289 /* protocol error... send an abort */
1290 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1291 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1292 op_err, 0, net->port);
1293 *abort_no_unlock = 1;
1296 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1297 /* protocol error... send an abort */
1298 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1299 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1300 op_err, 0, net->port);
1301 *abort_no_unlock = 1;
1304 if (init_ack->num_inbound_streams == 0) {
1305 /* protocol error... send an abort */
1306 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1307 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1308 op_err, 0, net->port);
1309 *abort_no_unlock = 1;
1312 if (init_ack->num_outbound_streams == 0) {
1313 /* protocol error... send an abort */
1314 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1315 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1316 op_err, 0, net->port);
1317 *abort_no_unlock = 1;
1320 /* process according to association state... */
1321 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1322 case SCTP_STATE_COOKIE_WAIT:
1323 /* this is the expected state for this chunk */
1324 /* process the INIT-ACK parameters */
1325 if (stcb->asoc.primary_destination->dest_state &
1326 SCTP_ADDR_UNCONFIRMED) {
1328 * The primary is where we sent the INIT, we can
1329 * always consider it confirmed when the INIT-ACK is
1330 * returned. Do this before we load addresses
1333 stcb->asoc.primary_destination->dest_state &=
1334 ~SCTP_ADDR_UNCONFIRMED;
1335 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1336 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1338 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1339 net, abort_no_unlock, vrf_id) < 0) {
1340 /* error in parsing parameters */
1343 /* update our state */
1344 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1345 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1347 /* reset the RTO calc */
1348 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1349 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1350 stcb->asoc.overall_error_count,
1352 SCTP_FROM_SCTP_INPUT,
1355 stcb->asoc.overall_error_count = 0;
1356 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1358 * collapse the init timer back in case of a exponential
1361 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1364 * the send at the end of the inbound data processing will
1365 * cause the cookie to be sent
1368 case SCTP_STATE_SHUTDOWN_SENT:
1369 /* incorrect state... discard */
1371 case SCTP_STATE_COOKIE_ECHOED:
1372 /* incorrect state... discard */
1374 case SCTP_STATE_OPEN:
1375 /* incorrect state... discard */
1377 case SCTP_STATE_EMPTY:
1378 case SCTP_STATE_INUSE:
1380 /* incorrect state... discard */
1384 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1388 static struct sctp_tcb *
1389 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1390 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1391 struct sctp_inpcb *inp, struct sctp_nets **netp,
1392 struct sockaddr *init_src, int *notification,
1393 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1394 uint32_t vrf_id, uint16_t port);
1398 * handle a state cookie for an existing association m: input packet mbuf
1399 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1400 * "split" mbuf and the cookie signature does not exist offset: offset into
1401 * mbuf to the cookie-echo chunk
1403 static struct sctp_tcb *
1404 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1405 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1406 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1407 struct sockaddr *init_src, int *notification,
1408 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port)
1410 struct sctp_association *asoc;
1411 struct sctp_init_chunk *init_cp, init_buf;
1412 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1413 struct sctp_nets *net;
1414 struct mbuf *op_err;
1415 struct sctp_paramhdr *ph;
1416 int init_offset, initack_offset, i;
1422 /* I know that the TCB is non-NULL from the caller */
1424 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1425 if (asoc->cookie_how[how_indx] == 0)
1428 if (how_indx < sizeof(asoc->cookie_how)) {
1429 asoc->cookie_how[how_indx] = 1;
1431 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1432 /* SHUTDOWN came in after sending INIT-ACK */
1433 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1434 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1435 0, M_DONTWAIT, 1, MT_DATA);
1436 if (op_err == NULL) {
1441 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1442 ph = mtod(op_err, struct sctp_paramhdr *);
1443 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1444 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1445 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1447 if (how_indx < sizeof(asoc->cookie_how))
1448 asoc->cookie_how[how_indx] = 2;
1452 * find and validate the INIT chunk in the cookie (peer's info) the
1453 * INIT should start after the cookie-echo header struct (chunk
1454 * header, state cookie header struct)
1456 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1458 init_cp = (struct sctp_init_chunk *)
1459 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1460 (uint8_t *) & init_buf);
1461 if (init_cp == NULL) {
1462 /* could not pull a INIT chunk in cookie */
1465 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1469 * find and validate the INIT-ACK chunk in the cookie (my info) the
1470 * INIT-ACK follows the INIT chunk
1472 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1473 initack_cp = (struct sctp_init_ack_chunk *)
1474 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1475 (uint8_t *) & initack_buf);
1476 if (initack_cp == NULL) {
1477 /* could not pull INIT-ACK chunk in cookie */
1480 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1483 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1484 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1486 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1487 * to get into the OPEN state
1489 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1491 * Opps, this means that we somehow generated two vtag's
1492 * the same. I.e. we did:
1494 * <---INIT(tag=a)------
1495 * ----INIT-ACK(tag=t)-->
1496 * ----INIT(tag=t)------> *1
1497 * <---INIT-ACK(tag=a)---
1498 * <----CE(tag=t)------------- *2
1500 * At point *1 we should be generating a different
1501 * tag t'. Which means we would throw away the CE and send
1502 * ours instead. Basically this is case C (throw away side).
1504 if (how_indx < sizeof(asoc->cookie_how))
1505 asoc->cookie_how[how_indx] = 17;
1509 switch SCTP_GET_STATE
1511 case SCTP_STATE_COOKIE_WAIT:
1512 case SCTP_STATE_COOKIE_ECHOED:
1514 * INIT was sent but got a COOKIE_ECHO with the
1515 * correct tags... just accept it...but we must
1516 * process the init so that we can make sure we have
1517 * the right seq no's.
1519 /* First we must process the INIT !! */
1520 retval = sctp_process_init(init_cp, stcb);
1522 if (how_indx < sizeof(asoc->cookie_how))
1523 asoc->cookie_how[how_indx] = 3;
1526 /* we have already processed the INIT so no problem */
1527 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1528 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1529 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1530 /* update current state */
1531 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1532 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1534 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1536 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1537 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1538 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1539 stcb->sctp_ep, stcb, asoc->primary_destination);
1541 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1542 sctp_stop_all_cookie_timers(stcb);
1543 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1544 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1545 (inp->sctp_socket->so_qlimit == 0)
1547 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1552 * Here is where collision would go if we
1553 * did a connect() and instead got a
1554 * init/init-ack/cookie done before the
1555 * init-ack came back..
1557 stcb->sctp_ep->sctp_flags |=
1558 SCTP_PCB_FLAGS_CONNECTED;
1559 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1560 so = SCTP_INP_SO(stcb->sctp_ep);
1561 atomic_add_int(&stcb->asoc.refcnt, 1);
1562 SCTP_TCB_UNLOCK(stcb);
1563 SCTP_SOCKET_LOCK(so, 1);
1564 SCTP_TCB_LOCK(stcb);
1565 atomic_add_int(&stcb->asoc.refcnt, -1);
1566 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1567 SCTP_SOCKET_UNLOCK(so, 1);
1571 soisconnected(stcb->sctp_socket);
1572 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1573 SCTP_SOCKET_UNLOCK(so, 1);
1576 /* notify upper layer */
1577 *notification = SCTP_NOTIFY_ASSOC_UP;
1579 * since we did not send a HB make sure we don't
1582 net->hb_responded = 1;
1583 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1584 &cookie->time_entered,
1585 sctp_align_unsafe_makecopy,
1586 SCTP_RTT_FROM_NON_DATA);
1588 if (stcb->asoc.sctp_autoclose_ticks &&
1589 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1590 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1596 * we're in the OPEN state (or beyond), so peer must
1597 * have simply lost the COOKIE-ACK
1601 sctp_stop_all_cookie_timers(stcb);
1603 * We ignore the return code here.. not sure if we should
1604 * somehow abort.. but we do have an existing asoc. This
1605 * really should not fail.
1607 if (sctp_load_addresses_from_init(stcb, m,
1608 init_offset + sizeof(struct sctp_init_chunk),
1609 initack_offset, sh, init_src)) {
1610 if (how_indx < sizeof(asoc->cookie_how))
1611 asoc->cookie_how[how_indx] = 4;
1614 /* respond with a COOKIE-ACK */
1615 sctp_toss_old_cookies(stcb, asoc);
1616 sctp_send_cookie_ack(stcb);
1617 if (how_indx < sizeof(asoc->cookie_how))
1618 asoc->cookie_how[how_indx] = 5;
1621 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1622 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1623 cookie->tie_tag_my_vtag == 0 &&
1624 cookie->tie_tag_peer_vtag == 0) {
1626 * case C in Section 5.2.4 Table 2: XMOO silently discard
1628 if (how_indx < sizeof(asoc->cookie_how))
1629 asoc->cookie_how[how_indx] = 6;
1633 * If nat support, and the below and stcb is established, send back
1634 * a ABORT(colliding state) if we are established.
1636 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1637 (asoc->peer_supports_nat) &&
1638 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1639 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1640 (asoc->peer_vtag == 0)))) {
1642 * Special case - Peer's support nat. We may have two init's
1643 * that we gave out the same tag on since one was not
1644 * established.. i.e. we get INIT from host-1 behind the nat
1645 * and we respond tag-a, we get a INIT from host-2 behind
1646 * the nat and we get tag-a again. Then we bring up host-1
1647 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1648 * Now we have colliding state. We must send an abort here
1649 * with colliding state indication.
1651 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1652 0, M_DONTWAIT, 1, MT_DATA);
1653 if (op_err == NULL) {
1657 /* pre-reserve some space */
1659 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1661 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1663 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1664 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1666 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1667 ph = mtod(op_err, struct sctp_paramhdr *);
1668 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1669 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1670 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
1673 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1674 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1675 (asoc->peer_vtag == 0))) {
1677 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1678 * should be ok, re-accept peer info
1680 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1682 * Extension of case C. If we hit this, then the
1683 * random number generator returned the same vtag
1684 * when we first sent our INIT-ACK and when we later
1685 * sent our INIT. The side with the seq numbers that
1686 * are different will be the one that normnally
1687 * would have hit case C. This in effect "extends"
1688 * our vtags in this collision case to be 64 bits.
1689 * The same collision could occur aka you get both
1690 * vtag and seq number the same twice in a row.. but
1691 * is much less likely. If it did happen then we
1692 * would proceed through and bring up the assoc.. we
1693 * may end up with the wrong stream setup however..
1694 * which would be bad.. but there is no way to
1695 * tell.. until we send on a stream that does not
1698 if (how_indx < sizeof(asoc->cookie_how))
1699 asoc->cookie_how[how_indx] = 7;
1703 if (how_indx < sizeof(asoc->cookie_how))
1704 asoc->cookie_how[how_indx] = 8;
1705 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1706 sctp_stop_all_cookie_timers(stcb);
1708 * since we did not send a HB make sure we don't double
1711 net->hb_responded = 1;
1712 if (stcb->asoc.sctp_autoclose_ticks &&
1713 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1714 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1717 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1718 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1720 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1722 * Ok the peer probably discarded our data (if we
1723 * echoed a cookie+data). So anything on the
1724 * sent_queue should be marked for retransmit, we
1725 * may not get something to kick us so it COULD
1726 * still take a timeout to move these.. but it can't
1727 * hurt to mark them.
1729 struct sctp_tmit_chunk *chk;
1731 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1732 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1733 chk->sent = SCTP_DATAGRAM_RESEND;
1734 sctp_flight_size_decrease(chk);
1735 sctp_total_flight_decrease(stcb, chk);
1736 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1742 /* process the INIT info (peer's info) */
1743 retval = sctp_process_init(init_cp, stcb);
1745 if (how_indx < sizeof(asoc->cookie_how))
1746 asoc->cookie_how[how_indx] = 9;
1749 if (sctp_load_addresses_from_init(stcb, m,
1750 init_offset + sizeof(struct sctp_init_chunk),
1751 initack_offset, sh, init_src)) {
1752 if (how_indx < sizeof(asoc->cookie_how))
1753 asoc->cookie_how[how_indx] = 10;
1756 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1757 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1758 *notification = SCTP_NOTIFY_ASSOC_UP;
1760 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1761 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1762 (inp->sctp_socket->so_qlimit == 0)) {
1763 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1767 stcb->sctp_ep->sctp_flags |=
1768 SCTP_PCB_FLAGS_CONNECTED;
1769 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1770 so = SCTP_INP_SO(stcb->sctp_ep);
1771 atomic_add_int(&stcb->asoc.refcnt, 1);
1772 SCTP_TCB_UNLOCK(stcb);
1773 SCTP_SOCKET_LOCK(so, 1);
1774 SCTP_TCB_LOCK(stcb);
1775 atomic_add_int(&stcb->asoc.refcnt, -1);
1776 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1777 SCTP_SOCKET_UNLOCK(so, 1);
1781 soisconnected(stcb->sctp_socket);
1782 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1783 SCTP_SOCKET_UNLOCK(so, 1);
1786 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1787 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1789 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1790 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1791 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1792 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1794 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1796 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1797 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1798 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1799 stcb->sctp_ep, stcb, asoc->primary_destination);
1801 sctp_stop_all_cookie_timers(stcb);
1802 sctp_toss_old_cookies(stcb, asoc);
1803 sctp_send_cookie_ack(stcb);
1806 * only if we have retrans set do we do this. What
1807 * this call does is get only the COOKIE-ACK out and
1808 * then when we return the normal call to
1809 * sctp_chunk_output will get the retrans out behind
1812 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1814 if (how_indx < sizeof(asoc->cookie_how))
1815 asoc->cookie_how[how_indx] = 11;
1819 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1820 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1821 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1822 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1823 cookie->tie_tag_peer_vtag != 0) {
1824 struct sctpasochead *head;
1826 if (asoc->peer_supports_nat) {
1828 * This is a gross gross hack. just call the
1829 * cookie_new code since we are allowing a duplicate
1830 * association. I hope this works...
1832 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1833 inp, netp, init_src, notification,
1834 auth_skipped, auth_offset, auth_len,
1838 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1841 if (how_indx < sizeof(asoc->cookie_how))
1842 asoc->cookie_how[how_indx] = 12;
1843 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1844 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1846 /* notify upper layer */
1847 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1848 atomic_add_int(&stcb->asoc.refcnt, 1);
1849 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1850 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1851 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1852 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1854 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1855 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1856 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1857 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1859 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1860 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1861 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1862 stcb->sctp_ep, stcb, asoc->primary_destination);
1864 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1865 /* move to OPEN state, if not in SHUTDOWN_SENT */
1866 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1868 asoc->pre_open_streams =
1869 ntohs(initack_cp->init.num_outbound_streams);
1870 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1871 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1872 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1874 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1876 asoc->str_reset_seq_in = asoc->init_seq_number;
1878 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1879 if (asoc->mapping_array) {
1880 memset(asoc->mapping_array, 0,
1881 asoc->mapping_array_size);
1883 if (asoc->nr_mapping_array) {
1884 memset(asoc->nr_mapping_array, 0,
1885 asoc->mapping_array_size);
1887 SCTP_TCB_UNLOCK(stcb);
1888 SCTP_INP_INFO_WLOCK();
1889 SCTP_INP_WLOCK(stcb->sctp_ep);
1890 SCTP_TCB_LOCK(stcb);
1891 atomic_add_int(&stcb->asoc.refcnt, -1);
1892 /* send up all the data */
1893 SCTP_TCB_SEND_LOCK(stcb);
1895 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_NOT_LOCKED);
1896 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1897 stcb->asoc.strmout[i].stream_no = i;
1898 stcb->asoc.strmout[i].next_sequence_sent = 0;
1899 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1901 /* process the INIT-ACK info (my info) */
1902 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1903 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1905 /* pull from vtag hash */
1906 LIST_REMOVE(stcb, sctp_asocs);
1907 /* re-insert to new vtag position */
1908 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1909 SCTP_BASE_INFO(hashasocmark))];
1911 * put it in the bucket in the vtag hash of assoc's for the
1914 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1916 /* process the INIT info (peer's info) */
1917 SCTP_TCB_SEND_UNLOCK(stcb);
1918 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1919 SCTP_INP_INFO_WUNLOCK();
1921 retval = sctp_process_init(init_cp, stcb);
1923 if (how_indx < sizeof(asoc->cookie_how))
1924 asoc->cookie_how[how_indx] = 13;
1929 * since we did not send a HB make sure we don't double
1932 net->hb_responded = 1;
1934 if (sctp_load_addresses_from_init(stcb, m,
1935 init_offset + sizeof(struct sctp_init_chunk),
1936 initack_offset, sh, init_src)) {
1937 if (how_indx < sizeof(asoc->cookie_how))
1938 asoc->cookie_how[how_indx] = 14;
1942 /* respond with a COOKIE-ACK */
1943 sctp_stop_all_cookie_timers(stcb);
1944 sctp_toss_old_cookies(stcb, asoc);
1945 sctp_send_cookie_ack(stcb);
1946 if (how_indx < sizeof(asoc->cookie_how))
1947 asoc->cookie_how[how_indx] = 15;
1951 if (how_indx < sizeof(asoc->cookie_how))
1952 asoc->cookie_how[how_indx] = 16;
1953 /* all other cases... */
1959 * handle a state cookie for a new association m: input packet mbuf chain--
1960 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1961 * and the cookie signature does not exist offset: offset into mbuf to the
1962 * cookie-echo chunk length: length of the cookie chunk to: where the init
1963 * was from returns a new TCB
1966 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1967 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1968 struct sctp_inpcb *inp, struct sctp_nets **netp,
1969 struct sockaddr *init_src, int *notification,
1970 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1971 uint32_t vrf_id, uint16_t port)
1973 struct sctp_tcb *stcb;
1974 struct sctp_init_chunk *init_cp, init_buf;
1975 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1976 struct sockaddr_storage sa_store;
1977 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1978 struct sctp_association *asoc;
1979 int init_offset, initack_offset, initack_limit;
1982 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1985 struct sockaddr_in *sin;
1989 struct sockaddr_in6 *sin6;
1992 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1995 so = SCTP_INP_SO(inp);
1999 * find and validate the INIT chunk in the cookie (peer's info) the
2000 * INIT should start after the cookie-echo header struct (chunk
2001 * header, state cookie header struct)
2003 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2004 init_cp = (struct sctp_init_chunk *)
2005 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2006 (uint8_t *) & init_buf);
2007 if (init_cp == NULL) {
2008 /* could not pull a INIT chunk in cookie */
2009 SCTPDBG(SCTP_DEBUG_INPUT1,
2010 "process_cookie_new: could not pull INIT chunk hdr\n");
2013 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2014 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2017 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2019 * find and validate the INIT-ACK chunk in the cookie (my info) the
2020 * INIT-ACK follows the INIT chunk
2022 initack_cp = (struct sctp_init_ack_chunk *)
2023 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2024 (uint8_t *) & initack_buf);
2025 if (initack_cp == NULL) {
2026 /* could not pull INIT-ACK chunk in cookie */
2027 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2030 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2034 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2035 * "initack_limit" value. This is because the chk_length field
2036 * includes the length of the cookie, but the cookie is omitted when
2037 * the INIT and INIT_ACK are tacked onto the cookie...
2039 initack_limit = offset + cookie_len;
2042 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2047 * Here we do a trick, we set in NULL for the proc/thread argument.
2048 * We do this since in effect we only use the p argument when the
2049 * socket is unbound and we must do an implicit bind. Since we are
2050 * getting a cookie, we cannot be unbound.
2052 stcb = sctp_aloc_assoc(inp, init_src, &error,
2053 ntohl(initack_cp->init.initiate_tag), vrf_id,
2054 (struct thread *)NULL
2057 struct mbuf *op_err;
2059 /* memory problem? */
2060 SCTPDBG(SCTP_DEBUG_INPUT1,
2061 "process_cookie_new: no room for another TCB!\n");
2062 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2064 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2065 sh, op_err, vrf_id, port);
2068 /* get the correct sctp_nets */
2070 *netp = sctp_findnet(stcb, init_src);
2073 /* get scope variables out of cookie */
2074 asoc->ipv4_local_scope = cookie->ipv4_scope;
2075 asoc->site_scope = cookie->site_scope;
2076 asoc->local_scope = cookie->local_scope;
2077 asoc->loopback_scope = cookie->loopback_scope;
2079 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2080 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2081 struct mbuf *op_err;
2084 * Houston we have a problem. The EP changed while the
2085 * cookie was in flight. Only recourse is to abort the
2088 atomic_add_int(&stcb->asoc.refcnt, 1);
2089 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2090 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2091 sh, op_err, vrf_id, port);
2092 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2093 SCTP_TCB_UNLOCK(stcb);
2094 SCTP_SOCKET_LOCK(so, 1);
2095 SCTP_TCB_LOCK(stcb);
2097 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2098 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2099 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2100 SCTP_SOCKET_UNLOCK(so, 1);
2102 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2105 /* process the INIT-ACK info (my info) */
2106 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2107 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2108 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2109 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2110 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2111 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2112 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2113 asoc->str_reset_seq_in = asoc->init_seq_number;
2115 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2117 /* process the INIT info (peer's info) */
2119 retval = sctp_process_init(init_cp, stcb);
2123 atomic_add_int(&stcb->asoc.refcnt, 1);
2124 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2125 SCTP_TCB_UNLOCK(stcb);
2126 SCTP_SOCKET_LOCK(so, 1);
2127 SCTP_TCB_LOCK(stcb);
2129 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2130 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2131 SCTP_SOCKET_UNLOCK(so, 1);
2133 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2136 /* load all addresses */
2137 if (sctp_load_addresses_from_init(stcb, m,
2138 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2140 atomic_add_int(&stcb->asoc.refcnt, 1);
2141 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2142 SCTP_TCB_UNLOCK(stcb);
2143 SCTP_SOCKET_LOCK(so, 1);
2144 SCTP_TCB_LOCK(stcb);
2146 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2147 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2148 SCTP_SOCKET_UNLOCK(so, 1);
2150 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2154 * verify any preceding AUTH chunk that was skipped
2156 /* pull the local authentication parameters from the cookie/init-ack */
2157 sctp_auth_get_cookie_params(stcb, m,
2158 initack_offset + sizeof(struct sctp_init_ack_chunk),
2159 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2161 struct sctp_auth_chunk *auth;
2163 auth = (struct sctp_auth_chunk *)
2164 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2165 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2166 /* auth HMAC failed, dump the assoc and packet */
2167 SCTPDBG(SCTP_DEBUG_AUTH1,
2168 "COOKIE-ECHO: AUTH failed\n");
2169 atomic_add_int(&stcb->asoc.refcnt, 1);
2170 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2171 SCTP_TCB_UNLOCK(stcb);
2172 SCTP_SOCKET_LOCK(so, 1);
2173 SCTP_TCB_LOCK(stcb);
2175 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2176 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2177 SCTP_SOCKET_UNLOCK(so, 1);
2179 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2182 /* remaining chunks checked... good to go */
2183 stcb->asoc.authenticated = 1;
2186 /* update current state */
2187 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2188 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2189 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2190 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2191 stcb->sctp_ep, stcb, asoc->primary_destination);
2193 sctp_stop_all_cookie_timers(stcb);
2194 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2195 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2198 * if we're doing ASCONFs, check to see if we have any new local
2199 * addresses that need to get added to the peer (eg. addresses
2200 * changed while cookie echo in flight). This needs to be done
2201 * after we go to the OPEN state to do the correct asconf
2202 * processing. else, make sure we have the correct addresses in our
2206 /* warning, we re-use sin, sin6, sa_store here! */
2207 /* pull in local_address (our "from" address) */
2208 switch (cookie->laddr_type) {
2210 case SCTP_IPV4_ADDRESS:
2211 /* source addr is IPv4 */
2212 sin = (struct sockaddr_in *)initack_src;
2213 memset(sin, 0, sizeof(*sin));
2214 sin->sin_family = AF_INET;
2215 sin->sin_len = sizeof(struct sockaddr_in);
2216 sin->sin_addr.s_addr = cookie->laddress[0];
2220 case SCTP_IPV6_ADDRESS:
2221 /* source addr is IPv6 */
2222 sin6 = (struct sockaddr_in6 *)initack_src;
2223 memset(sin6, 0, sizeof(*sin6));
2224 sin6->sin6_family = AF_INET6;
2225 sin6->sin6_len = sizeof(struct sockaddr_in6);
2226 sin6->sin6_scope_id = cookie->scope_id;
2227 memcpy(&sin6->sin6_addr, cookie->laddress,
2228 sizeof(sin6->sin6_addr));
2232 atomic_add_int(&stcb->asoc.refcnt, 1);
2233 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2234 SCTP_TCB_UNLOCK(stcb);
2235 SCTP_SOCKET_LOCK(so, 1);
2236 SCTP_TCB_LOCK(stcb);
2238 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2239 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2240 SCTP_SOCKET_UNLOCK(so, 1);
2242 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2246 /* set up to notify upper layer */
2247 *notification = SCTP_NOTIFY_ASSOC_UP;
2248 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2249 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2250 (inp->sctp_socket->so_qlimit == 0)) {
2252 * This is an endpoint that called connect() how it got a
2253 * cookie that is NEW is a bit of a mystery. It must be that
2254 * the INIT was sent, but before it got there.. a complete
2255 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2256 * should have went to the other code.. not here.. oh well..
2257 * a bit of protection is worth having..
2259 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2260 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2261 atomic_add_int(&stcb->asoc.refcnt, 1);
2262 SCTP_TCB_UNLOCK(stcb);
2263 SCTP_SOCKET_LOCK(so, 1);
2264 SCTP_TCB_LOCK(stcb);
2265 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2266 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2267 SCTP_SOCKET_UNLOCK(so, 1);
2271 soisconnected(stcb->sctp_socket);
2272 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2273 SCTP_SOCKET_UNLOCK(so, 1);
2275 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2276 (inp->sctp_socket->so_qlimit)) {
2278 * We don't want to do anything with this one. Since it is
2279 * the listening guy. The timer will get started for
2280 * accepted connections in the caller.
2284 /* since we did not send a HB make sure we don't double things */
2285 if ((netp) && (*netp))
2286 (*netp)->hb_responded = 1;
2288 if (stcb->asoc.sctp_autoclose_ticks &&
2289 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2290 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2292 /* calculate the RTT */
2293 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2294 if ((netp) && (*netp)) {
2295 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2296 &cookie->time_entered, sctp_align_unsafe_makecopy,
2297 SCTP_RTT_FROM_NON_DATA);
2299 /* respond with a COOKIE-ACK */
2300 sctp_send_cookie_ack(stcb);
2303 * check the address lists for any ASCONFs that need to be sent
2304 * AFTER the cookie-ack is sent
2306 sctp_check_address_list(stcb, m,
2307 initack_offset + sizeof(struct sctp_init_ack_chunk),
2308 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2309 initack_src, cookie->local_scope, cookie->site_scope,
2310 cookie->ipv4_scope, cookie->loopback_scope);
2317 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2318 * we NEED to make sure we are not already using the vtag. If so we
2319 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2320 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2321 SCTP_BASE_INFO(hashasocmark))];
2322 LIST_FOREACH(stcb, head, sctp_asocs) {
2323 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2324 -- SEND ABORT - TRY AGAIN --
2330 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2331 * existing (non-NULL) TCB
2333 static struct mbuf *
2334 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2335 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2336 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2337 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2338 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2340 struct sctp_state_cookie *cookie;
2341 struct sctp_tcb *l_stcb = *stcb;
2342 struct sctp_inpcb *l_inp;
2343 struct sockaddr *to;
2344 struct sctp_pcb *ep;
2346 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2348 uint8_t cookie_ok = 0;
2349 unsigned int size_of_pkt, sig_offset, cookie_offset;
2350 unsigned int cookie_len;
2352 struct timeval time_expires;
2353 struct sockaddr_storage dest_store;
2354 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2356 int notification = 0;
2357 struct sctp_nets *netl;
2358 int had_a_existing_tcb = 0;
2359 int send_int_conf = 0;
2362 struct sockaddr_in sin;
2366 struct sockaddr_in6 sin6;
2370 SCTPDBG(SCTP_DEBUG_INPUT2,
2371 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2373 if (inp_p == NULL) {
2376 /* First get the destination address setup too. */
2377 iph = mtod(m, struct ip *);
2378 switch (iph->ip_v) {
2383 struct sockaddr_in *lsin;
2385 lsin = (struct sockaddr_in *)(localep_sa);
2386 memset(lsin, 0, sizeof(*lsin));
2387 lsin->sin_family = AF_INET;
2388 lsin->sin_len = sizeof(*lsin);
2389 lsin->sin_port = sh->dest_port;
2390 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2391 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2396 case IPV6_VERSION >> 4:
2399 struct ip6_hdr *ip6;
2400 struct sockaddr_in6 *lsin6;
2402 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2403 memset(lsin6, 0, sizeof(*lsin6));
2404 lsin6->sin6_family = AF_INET6;
2405 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2406 ip6 = mtod(m, struct ip6_hdr *);
2407 lsin6->sin6_port = sh->dest_port;
2408 lsin6->sin6_addr = ip6->ip6_dst;
2409 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2417 cookie = &cp->cookie;
2418 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2419 cookie_len = ntohs(cp->ch.chunk_length);
2421 if ((cookie->peerport != sh->src_port) &&
2422 (cookie->myport != sh->dest_port) &&
2423 (cookie->my_vtag != sh->v_tag)) {
2425 * invalid ports or bad tag. Note that we always leave the
2426 * v_tag in the header in network order and when we stored
2427 * it in the my_vtag slot we also left it in network order.
2428 * This maintains the match even though it may be in the
2429 * opposite byte order of the machine :->
2433 if (cookie_len > size_of_pkt ||
2434 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2435 sizeof(struct sctp_init_chunk) +
2436 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2437 /* cookie too long! or too small */
2441 * split off the signature into its own mbuf (since it should not be
2442 * calculated in the sctp_hmac_m() call).
2444 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2445 if (sig_offset > size_of_pkt) {
2446 /* packet not correct size! */
2447 /* XXX this may already be accounted for earlier... */
2450 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2451 if (m_sig == NULL) {
2452 /* out of memory or ?? */
2455 #ifdef SCTP_MBUF_LOGGING
2456 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2459 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2460 if (SCTP_BUF_IS_EXTENDED(mat)) {
2461 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2468 * compute the signature/digest for the cookie
2470 ep = &(*inp_p)->sctp_ep;
2473 SCTP_TCB_UNLOCK(l_stcb);
2475 SCTP_INP_RLOCK(l_inp);
2477 SCTP_TCB_LOCK(l_stcb);
2479 /* which cookie is it? */
2480 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2481 (ep->current_secret_number != ep->last_secret_number)) {
2482 /* it's the old cookie */
2483 (void)sctp_hmac_m(SCTP_HMAC,
2484 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2485 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2487 /* it's the current cookie */
2488 (void)sctp_hmac_m(SCTP_HMAC,
2489 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2490 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2492 /* get the signature */
2493 SCTP_INP_RUNLOCK(l_inp);
2494 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2496 /* couldn't find signature */
2497 sctp_m_freem(m_sig);
2500 /* compare the received digest with the computed digest */
2501 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2502 /* try the old cookie? */
2503 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2504 (ep->current_secret_number != ep->last_secret_number)) {
2505 /* compute digest with old */
2506 (void)sctp_hmac_m(SCTP_HMAC,
2507 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2508 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2510 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2518 * Now before we continue we must reconstruct our mbuf so that
2519 * normal processing of any other chunks will work.
2525 while (SCTP_BUF_NEXT(m_at) != NULL) {
2526 m_at = SCTP_BUF_NEXT(m_at);
2528 SCTP_BUF_NEXT(m_at) = m_sig;
2531 if (cookie_ok == 0) {
2532 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2533 SCTPDBG(SCTP_DEBUG_INPUT2,
2534 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2535 (uint32_t) offset, cookie_offset, sig_offset);
2539 * check the cookie timestamps to be sure it's not stale
2541 (void)SCTP_GETTIME_TIMEVAL(&now);
2542 /* Expire time is in Ticks, so we convert to seconds */
2543 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2544 time_expires.tv_usec = cookie->time_entered.tv_usec;
2546 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2549 if (timevalcmp(&now, &time_expires, >)) {
2550 /* cookie is stale! */
2551 struct mbuf *op_err;
2552 struct sctp_stale_cookie_msg *scm;
2555 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2556 0, M_DONTWAIT, 1, MT_DATA);
2557 if (op_err == NULL) {
2562 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2563 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2564 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2565 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2566 (sizeof(uint32_t))));
2567 /* seconds to usec */
2568 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2571 tim = now.tv_usec - cookie->time_entered.tv_usec;
2572 scm->time_usec = htonl(tim);
2573 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2578 * Now we must see with the lookup address if we have an existing
2579 * asoc. This will only happen if we were in the COOKIE-WAIT state
2580 * and a INIT collided with us and somewhere the peer sent the
2581 * cookie on another address besides the single address our assoc
2582 * had for him. In this case we will have one of the tie-tags set at
2583 * least AND the address field in the cookie can be used to look it
2587 switch (cookie->addr_type) {
2589 case SCTP_IPV6_ADDRESS:
2590 memset(&sin6, 0, sizeof(sin6));
2591 sin6.sin6_family = AF_INET6;
2592 sin6.sin6_len = sizeof(sin6);
2593 sin6.sin6_port = sh->src_port;
2594 sin6.sin6_scope_id = cookie->scope_id;
2595 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2596 sizeof(sin6.sin6_addr.s6_addr));
2597 to = (struct sockaddr *)&sin6;
2601 case SCTP_IPV4_ADDRESS:
2602 memset(&sin, 0, sizeof(sin));
2603 sin.sin_family = AF_INET;
2604 sin.sin_len = sizeof(sin);
2605 sin.sin_port = sh->src_port;
2606 sin.sin_addr.s_addr = cookie->address[0];
2607 to = (struct sockaddr *)&sin;
2611 /* This should not happen */
2614 if ((*stcb == NULL) && to) {
2615 /* Yep, lets check */
2616 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2617 if (*stcb == NULL) {
2619 * We should have only got back the same inp. If we
2620 * got back a different ep we have a problem. The
2621 * original findep got back l_inp and now
2623 if (l_inp != *inp_p) {
2624 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2627 if (*locked_tcb == NULL) {
2629 * In this case we found the assoc only
2630 * after we locked the create lock. This
2631 * means we are in a colliding case and we
2632 * must make sure that we unlock the tcb if
2633 * its one of the cases where we throw away
2634 * the incoming packets.
2636 *locked_tcb = *stcb;
2639 * We must also increment the inp ref count
2640 * since the ref_count flags was set when we
2641 * did not find the TCB, now we found it
2642 * which reduces the refcount.. we must
2643 * raise it back out to balance it all :-)
2645 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2646 if ((*stcb)->sctp_ep != l_inp) {
2647 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2648 (*stcb)->sctp_ep, l_inp);
2656 cookie_len -= SCTP_SIGNATURE_SIZE;
2657 if (*stcb == NULL) {
2658 /* this is the "normal" case... get a new TCB */
2659 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2660 cookie_len, *inp_p, netp, to, ¬ification,
2661 auth_skipped, auth_offset, auth_len, vrf_id, port);
2663 /* this is abnormal... cookie-echo on existing TCB */
2664 had_a_existing_tcb = 1;
2665 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2666 cookie, cookie_len, *inp_p, *stcb, netp, to,
2667 ¬ification, vrf_id, auth_skipped, auth_offset, auth_len, port);
2670 if (*stcb == NULL) {
2671 /* still no TCB... must be bad cookie-echo */
2674 if ((*netp != NULL) && (m->m_flags & M_FLOWID)) {
2675 (*netp)->flowid = m->m_pkthdr.flowid;
2677 (*netp)->flowidset = 1;
2681 * Ok, we built an association so confirm the address we sent the
2684 netl = sctp_findnet(*stcb, to);
2686 * This code should in theory NOT run but
2689 /* TSNH! Huh, why do I need to add this address here? */
2690 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2693 netl = sctp_findnet(*stcb, to);
2696 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2697 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2698 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2703 sctp_start_net_timers(*stcb);
2704 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2705 if (!had_a_existing_tcb ||
2706 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2708 * If we have a NEW cookie or the connect never
2709 * reached the connected state during collision we
2710 * must do the TCP accept thing.
2712 struct socket *so, *oso;
2713 struct sctp_inpcb *inp;
2715 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2717 * For a restart we will keep the same
2718 * socket, no need to do anything. I THINK!!
2720 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2721 if (send_int_conf) {
2722 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2723 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2727 oso = (*inp_p)->sctp_socket;
2728 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2729 SCTP_TCB_UNLOCK((*stcb));
2730 CURVNET_SET(oso->so_vnet);
2731 so = sonewconn(oso, 0
2734 SCTP_TCB_LOCK((*stcb));
2735 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2738 struct mbuf *op_err;
2740 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2741 struct socket *pcb_so;
2744 /* Too many sockets */
2745 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2746 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2747 sctp_abort_association(*inp_p, NULL, m, iphlen,
2748 sh, op_err, vrf_id, port);
2749 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2750 pcb_so = SCTP_INP_SO(*inp_p);
2751 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2752 SCTP_TCB_UNLOCK((*stcb));
2753 SCTP_SOCKET_LOCK(pcb_so, 1);
2754 SCTP_TCB_LOCK((*stcb));
2755 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2757 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2758 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2759 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2763 inp = (struct sctp_inpcb *)so->so_pcb;
2764 SCTP_INP_INCR_REF(inp);
2766 * We add the unbound flag here so that if we get an
2767 * soabort() before we get the move_pcb done, we
2768 * will properly cleanup.
2770 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2771 SCTP_PCB_FLAGS_CONNECTED |
2772 SCTP_PCB_FLAGS_IN_TCPPOOL |
2773 SCTP_PCB_FLAGS_UNBOUND |
2774 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2775 SCTP_PCB_FLAGS_DONT_WAKE);
2776 inp->sctp_features = (*inp_p)->sctp_features;
2777 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2778 inp->sctp_socket = so;
2779 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2780 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2781 inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable;
2782 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2783 inp->sctp_context = (*inp_p)->sctp_context;
2784 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2785 inp->inp_starting_point_for_iterator = NULL;
2787 * copy in the authentication parameters from the
2790 if (inp->sctp_ep.local_hmacs)
2791 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2792 inp->sctp_ep.local_hmacs =
2793 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2794 if (inp->sctp_ep.local_auth_chunks)
2795 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2796 inp->sctp_ep.local_auth_chunks =
2797 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2800 * Now we must move it from one hash table to
2801 * another and get the tcb in the right place.
2805 * This is where the one-2-one socket is put into
2806 * the accept state waiting for the accept!
2809 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2811 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2813 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2814 SCTP_TCB_UNLOCK((*stcb));
2816 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2818 SCTP_TCB_LOCK((*stcb));
2819 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2823 * now we must check to see if we were aborted while
2824 * the move was going on and the lock/unlock
2827 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2829 * yep it was, we leave the assoc attached
2830 * to the socket since the sctp_inpcb_free()
2831 * call will send an abort for us.
2833 SCTP_INP_DECR_REF(inp);
2836 SCTP_INP_DECR_REF(inp);
2837 /* Switch over to the new guy */
2839 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2840 if (send_int_conf) {
2841 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2842 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2845 * Pull it from the incomplete queue and wake the
2848 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2849 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2850 SCTP_TCB_UNLOCK((*stcb));
2851 SCTP_SOCKET_LOCK(so, 1);
2854 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2855 SCTP_TCB_LOCK((*stcb));
2856 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2857 SCTP_SOCKET_UNLOCK(so, 1);
2862 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
2864 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2866 if (send_int_conf) {
2867 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2868 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2875 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2876 struct sctp_tcb *stcb, struct sctp_nets *net)
2878 /* cp must not be used, others call this without a c-ack :-) */
2879 struct sctp_association *asoc;
2881 SCTPDBG(SCTP_DEBUG_INPUT2,
2882 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2888 sctp_stop_all_cookie_timers(stcb);
2889 /* process according to association state */
2890 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2891 /* state change only needed when I am in right state */
2892 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2893 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2894 sctp_start_net_timers(stcb);
2895 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2896 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2897 stcb->sctp_ep, stcb, asoc->primary_destination);
2901 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2902 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2903 if (asoc->overall_error_count == 0) {
2904 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2905 &asoc->time_entered, sctp_align_safe_nocopy,
2906 SCTP_RTT_FROM_NON_DATA);
2908 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2909 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2910 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2911 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2912 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2916 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2917 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2918 so = SCTP_INP_SO(stcb->sctp_ep);
2919 atomic_add_int(&stcb->asoc.refcnt, 1);
2920 SCTP_TCB_UNLOCK(stcb);
2921 SCTP_SOCKET_LOCK(so, 1);
2922 SCTP_TCB_LOCK(stcb);
2923 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2925 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2926 soisconnected(stcb->sctp_socket);
2928 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2929 SCTP_SOCKET_UNLOCK(so, 1);
2933 * since we did not send a HB make sure we don't double
2936 net->hb_responded = 1;
2938 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2940 * We don't need to do the asconf thing, nor hb or
2941 * autoclose if the socket is closed.
2945 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2949 if (stcb->asoc.sctp_autoclose_ticks &&
2950 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2951 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2952 stcb->sctp_ep, stcb, NULL);
2955 * send ASCONF if parameters are pending and ASCONFs are
2956 * allowed (eg. addresses changed when init/cookie echo were
2959 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2960 (stcb->asoc.peer_supports_asconf) &&
2961 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2962 #ifdef SCTP_TIMER_BASED_ASCONF
2963 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2964 stcb->sctp_ep, stcb,
2965 stcb->asoc.primary_destination);
2967 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2968 SCTP_ADDR_NOT_LOCKED);
2973 /* Toss the cookie if I can */
2974 sctp_toss_old_cookies(stcb, asoc);
2975 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2976 /* Restart the timer if we have pending data */
2977 struct sctp_tmit_chunk *chk;
2979 chk = TAILQ_FIRST(&asoc->sent_queue);
2980 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2985 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2986 struct sctp_tcb *stcb)
2988 struct sctp_nets *net;
2989 struct sctp_tmit_chunk *lchk;
2990 struct sctp_ecne_chunk bkup;
2991 uint8_t override_bit;
2992 uint32_t tsn, window_data_tsn;
2994 unsigned int pkt_cnt;
2996 len = ntohs(cp->ch.chunk_length);
2997 if ((len != sizeof(struct sctp_ecne_chunk)) &&
2998 (len != sizeof(struct old_sctp_ecne_chunk))) {
3001 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3002 /* Its the old format */
3003 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3004 bkup.num_pkts_since_cwr = htonl(1);
3007 SCTP_STAT_INCR(sctps_recvecne);
3008 tsn = ntohl(cp->tsn);
3009 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3010 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3012 window_data_tsn = stcb->asoc.sending_seq - 1;
3014 window_data_tsn = lchk->rec.data.TSN_seq;
3017 /* Find where it was sent to if possible. */
3019 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3020 if (lchk->rec.data.TSN_seq == tsn) {
3022 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3025 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3031 * What to do. A previous send of a CWR was possibly lost.
3032 * See how old it is, we may have it marked on the actual
3035 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3036 if (tsn == net->last_cwr_tsn) {
3037 /* Found him, send it off */
3043 * If we reach here, we need to send a special CWR
3044 * that says hey, we did this a long time ago and
3045 * you lost the response.
3047 net = TAILQ_FIRST(&stcb->asoc.nets);
3052 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3059 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3060 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3062 * JRS - Use the congestion control given in the pluggable
3065 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3067 * We reduce once every RTT. So we will only lower cwnd at
3068 * the next sending seq i.e. the window_data_tsn
3070 net->cwr_window_tsn = window_data_tsn;
3071 net->ecn_ce_pkt_cnt += pkt_cnt;
3072 net->lost_cnt = pkt_cnt;
3073 net->last_cwr_tsn = tsn;
3075 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3076 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3077 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3079 * Another loss in the same window update how many
3080 * marks/packets lost we have had.
3084 if (pkt_cnt > net->lost_cnt) {
3085 /* Should be the case */
3086 cnt = (pkt_cnt - net->lost_cnt);
3087 net->ecn_ce_pkt_cnt += cnt;
3089 net->lost_cnt = pkt_cnt;
3090 net->last_cwr_tsn = tsn;
3092 * Most CC functions will ignore this call, since we
3093 * are in-window yet of the initial CE the peer saw.
3095 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3099 * We always send a CWR this way if our previous one was lost our
3100 * peer will get an update, or if it is not time again to reduce we
3101 * still get the cwr to the peer. Note we set the override when we
3102 * could not find the TSN on the chunk or the destination network.
3104 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3108 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3111 * Here we get a CWR from the peer. We must look in the outqueue and
3112 * make sure that we have a covered ECNE in the control chunk part.
3115 struct sctp_tmit_chunk *chk;
3116 struct sctp_ecne_chunk *ecne;
3120 cwr_tsn = ntohl(cp->tsn);
3122 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3123 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3124 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3127 if ((override == 0) && (chk->whoTo != net)) {
3128 /* Must be from the right src unless override is set */
3131 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3132 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3133 /* this covers this ECNE, we can remove it */
3134 stcb->asoc.ecn_echo_cnt_onq--;
3135 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3138 sctp_m_freem(chk->data);
3141 stcb->asoc.ctrl_queue_cnt--;
3142 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3143 if (override == 0) {
3151 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3152 struct sctp_tcb *stcb, struct sctp_nets *net)
3154 struct sctp_association *asoc;
3156 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3161 SCTPDBG(SCTP_DEBUG_INPUT2,
3162 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3167 /* process according to association state */
3168 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3169 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3170 SCTPDBG(SCTP_DEBUG_INPUT2,
3171 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3172 SCTP_TCB_UNLOCK(stcb);
3175 /* notify upper layer protocol */
3176 if (stcb->sctp_socket) {
3177 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3178 /* are the queues empty? they should be */
3179 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3180 !TAILQ_EMPTY(&asoc->sent_queue) ||
3181 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3182 sctp_report_all_outbound(stcb, 0, 0, SCTP_SO_NOT_LOCKED);
3185 /* stop the timer */
3186 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3187 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3189 SCTPDBG(SCTP_DEBUG_INPUT2,
3190 "sctp_handle_shutdown_complete: calls free-asoc\n");
3191 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3192 so = SCTP_INP_SO(stcb->sctp_ep);
3193 atomic_add_int(&stcb->asoc.refcnt, 1);
3194 SCTP_TCB_UNLOCK(stcb);
3195 SCTP_SOCKET_LOCK(so, 1);
3196 SCTP_TCB_LOCK(stcb);
3197 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3199 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3200 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3201 SCTP_SOCKET_UNLOCK(so, 1);
3207 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3208 struct sctp_nets *net, uint8_t flg)
3210 switch (desc->chunk_type) {
3212 /* find the tsn to resend (possibly */
3215 struct sctp_tmit_chunk *tp1;
3217 tsn = ntohl(desc->tsn_ifany);
3218 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3219 if (tp1->rec.data.TSN_seq == tsn) {
3223 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3231 * Do it the other way , aka without paying
3232 * attention to queue seq order.
3234 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3235 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3236 if (tp1->rec.data.TSN_seq == tsn) {
3243 SCTP_STAT_INCR(sctps_pdrptsnnf);
3245 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3248 if (((flg & SCTP_BADCRC) == 0) &&
3249 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3252 if ((stcb->asoc.peers_rwnd == 0) &&
3253 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3254 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3257 if (stcb->asoc.peers_rwnd == 0 &&
3258 (flg & SCTP_FROM_MIDDLE_BOX)) {
3259 SCTP_STAT_INCR(sctps_pdrpdizrw);
3262 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3263 sizeof(struct sctp_data_chunk));
3267 for (iii = 0; iii < sizeof(desc->data_bytes);
3269 if (ddp[iii] != desc->data_bytes[iii]) {
3270 SCTP_STAT_INCR(sctps_pdrpbadd);
3278 * this guy had a RTO calculation
3279 * pending on it, cancel it
3281 if (tp1->whoTo->rto_needed == 0) {
3282 tp1->whoTo->rto_needed = 1;
3286 SCTP_STAT_INCR(sctps_pdrpmark);
3287 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3288 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3290 * mark it as if we were doing a FR, since
3291 * we will be getting gap ack reports behind
3292 * the info from the router.
3294 tp1->rec.data.doing_fast_retransmit = 1;
3296 * mark the tsn with what sequences can
3299 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3300 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3302 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3305 /* restart the timer */
3306 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3307 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3308 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3311 /* fix counts and things */
3312 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3313 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3314 tp1->whoTo->flight_size,
3317 tp1->rec.data.TSN_seq);
3319 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3320 sctp_flight_size_decrease(tp1);
3321 sctp_total_flight_decrease(stcb, tp1);
3323 tp1->sent = SCTP_DATAGRAM_RESEND;
3329 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3330 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3333 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3335 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3338 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3339 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3340 audit, stcb->asoc.sent_queue_retran_cnt);
3341 #ifndef SCTP_AUDITING_ENABLED
3342 stcb->asoc.sent_queue_retran_cnt = audit;
3350 struct sctp_tmit_chunk *asconf;
3352 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3354 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3359 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3360 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3361 asconf->sent = SCTP_DATAGRAM_RESEND;
3362 asconf->snd_count--;
3366 case SCTP_INITIATION:
3367 /* resend the INIT */
3368 stcb->asoc.dropped_special_cnt++;
3369 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3371 * If we can get it in, in a few attempts we do
3372 * this, otherwise we let the timer fire.
3374 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3375 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3376 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3379 case SCTP_SELECTIVE_ACK:
3380 case SCTP_NR_SELECTIVE_ACK:
3381 /* resend the sack */
3382 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3384 case SCTP_HEARTBEAT_REQUEST:
3385 /* resend a demand HB */
3386 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3388 * Only retransmit if we KNOW we wont destroy the
3391 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3395 sctp_send_shutdown(stcb, net);
3397 case SCTP_SHUTDOWN_ACK:
3398 sctp_send_shutdown_ack(stcb, net);
3400 case SCTP_COOKIE_ECHO:
3402 struct sctp_tmit_chunk *cookie;
3405 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3407 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3412 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3413 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3414 cookie->sent = SCTP_DATAGRAM_RESEND;
3415 sctp_stop_all_cookie_timers(stcb);
3419 case SCTP_COOKIE_ACK:
3420 sctp_send_cookie_ack(stcb);
3422 case SCTP_ASCONF_ACK:
3423 /* resend last asconf ack */
3424 sctp_send_asconf_ack(stcb);
3426 case SCTP_FORWARD_CUM_TSN:
3427 send_forward_tsn(stcb, &stcb->asoc);
3429 /* can't do anything with these */
3430 case SCTP_PACKET_DROPPED:
3431 case SCTP_INITIATION_ACK: /* this should not happen */
3432 case SCTP_HEARTBEAT_ACK:
3433 case SCTP_ABORT_ASSOCIATION:
3434 case SCTP_OPERATION_ERROR:
3435 case SCTP_SHUTDOWN_COMPLETE:
3445 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3451 * We set things to 0xffff since this is the last delivered sequence
3452 * and we will be sending in 0 after the reset.
3455 if (number_entries) {
3456 for (i = 0; i < number_entries; i++) {
3457 temp = ntohs(list[i]);
3458 if (temp >= stcb->asoc.streamincnt) {
3461 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3465 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3466 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3469 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3473 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3477 if (number_entries == 0) {
3478 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3479 stcb->asoc.strmout[i].next_sequence_sent = 0;
3481 } else if (number_entries) {
3482 for (i = 0; i < number_entries; i++) {
3485 temp = ntohs(list[i]);
3486 if (temp >= stcb->asoc.streamoutcnt) {
3487 /* no such stream */
3490 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3493 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3497 struct sctp_stream_reset_out_request *
3498 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3500 struct sctp_association *asoc;
3501 struct sctp_stream_reset_out_req *req;
3502 struct sctp_stream_reset_out_request *r;
3503 struct sctp_tmit_chunk *chk;
3507 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3508 asoc->stream_reset_outstanding = 0;
3511 if (stcb->asoc.str_reset == NULL) {
3512 asoc->stream_reset_outstanding = 0;
3515 chk = stcb->asoc.str_reset;
3516 if (chk->data == NULL) {
3520 /* he wants a copy of the chk pointer */
3523 clen = chk->send_size;
3524 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3526 if (ntohl(r->request_seq) == seq) {
3530 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3531 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3532 /* move to the next one, there can only be a max of two */
3533 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3534 if (ntohl(r->request_seq) == seq) {
3538 /* that seq is not here */
3543 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3545 struct sctp_association *asoc;
3546 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3548 if (stcb->asoc.str_reset == NULL) {
3553 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3554 TAILQ_REMOVE(&asoc->control_send_queue,
3558 sctp_m_freem(chk->data);
3561 asoc->ctrl_queue_cnt--;
3562 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3563 /* sa_ignore NO_NULL_CHK */
3564 stcb->asoc.str_reset = NULL;
3569 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3570 uint32_t seq, uint32_t action,
3571 struct sctp_stream_reset_response *respin)
3575 struct sctp_association *asoc = &stcb->asoc;
3576 struct sctp_tmit_chunk *chk;
3577 struct sctp_stream_reset_out_request *srparam;
3580 if (asoc->stream_reset_outstanding == 0) {
3584 if (seq == stcb->asoc.str_reset_seq_out) {
3585 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3587 stcb->asoc.str_reset_seq_out++;
3588 type = ntohs(srparam->ph.param_type);
3589 lparm_len = ntohs(srparam->ph.param_length);
3590 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3591 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3592 asoc->stream_reset_out_is_outstanding = 0;
3593 if (asoc->stream_reset_outstanding)
3594 asoc->stream_reset_outstanding--;
3595 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3597 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3598 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3599 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3601 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3603 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3604 /* Answered my request */
3605 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3606 if (asoc->stream_reset_outstanding)
3607 asoc->stream_reset_outstanding--;
3608 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3609 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3610 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3611 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3612 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3613 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3615 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3616 /* Ok we now may have more streams */
3619 num_stream = stcb->asoc.strm_pending_add_size;
3620 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3622 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3624 stcb->asoc.strm_pending_add_size = 0;
3625 if (asoc->stream_reset_outstanding)
3626 asoc->stream_reset_outstanding--;
3627 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3628 /* Put the new streams into effect */
3629 stcb->asoc.streamoutcnt += num_stream;
3630 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3631 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3632 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3633 SCTP_STREAM_CHANGE_DENIED);
3635 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3636 SCTP_STREAM_CHANGE_FAILED);
3638 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3639 if (asoc->stream_reset_outstanding)
3640 asoc->stream_reset_outstanding--;
3641 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3642 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3643 SCTP_STREAM_CHANGE_DENIED);
3644 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3645 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3646 SCTP_STREAM_CHANGE_FAILED);
3648 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3650 * a) Adopt the new in tsn.
3652 * c) Adopt the new out-tsn
3654 struct sctp_stream_reset_response_tsn *resp;
3655 struct sctp_forward_tsn_chunk fwdtsn;
3658 if (respin == NULL) {
3662 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3663 resp = (struct sctp_stream_reset_response_tsn *)respin;
3664 asoc->stream_reset_outstanding--;
3665 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3666 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3667 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3668 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3672 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3673 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3674 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3676 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3677 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3678 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3680 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3681 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3683 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3684 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3686 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3687 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3688 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3689 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3690 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3691 SCTP_ASSOC_RESET_DENIED);
3693 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3694 SCTP_ASSOC_RESET_FAILED);
3697 /* get rid of the request and get the request flags */
3698 if (asoc->stream_reset_outstanding == 0) {
3699 sctp_clean_up_stream_reset(stcb);
3707 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3708 struct sctp_tmit_chunk *chk,
3709 struct sctp_stream_reset_in_request *req, int trunc)
3717 * peer wants me to send a str-reset to him for my outgoing seq's if
3720 struct sctp_association *asoc = &stcb->asoc;
3722 seq = ntohl(req->request_seq);
3723 if (asoc->str_reset_seq_in == seq) {
3724 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3725 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3726 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3728 /* Can't do it, since they exceeded our buffer size */
3729 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3730 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3731 len = ntohs(req->ph.param_length);
3732 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3733 for (i = 0; i < number_entries; i++) {
3734 temp = ntohs(req->list_of_streams[i]);
3735 req->list_of_streams[i] = temp;
3737 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3738 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3739 asoc->str_reset_seq_out,
3740 seq, (asoc->sending_seq - 1));
3741 asoc->stream_reset_out_is_outstanding = 1;
3742 asoc->str_reset = chk;
3743 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3744 stcb->asoc.stream_reset_outstanding++;
3746 /* Can't do it, since we have sent one out */
3747 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3749 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3750 asoc->str_reset_seq_in++;
3751 } else if (asoc->str_reset_seq_in - 1 == seq) {
3752 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3753 } else if (asoc->str_reset_seq_in - 2 == seq) {
3754 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3756 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3761 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3762 struct sctp_tmit_chunk *chk,
3763 struct sctp_stream_reset_tsn_request *req)
3765 /* reset all in and out and update the tsn */
3767 * A) reset my str-seq's on in and out. B) Select a receive next,
3768 * and set cum-ack to it. Also process this selected number as a
3769 * fwd-tsn as well. C) set in the response my next sending seq.
3771 struct sctp_forward_tsn_chunk fwdtsn;
3772 struct sctp_association *asoc = &stcb->asoc;
3776 seq = ntohl(req->request_seq);
3777 if (asoc->str_reset_seq_in == seq) {
3778 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3779 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3780 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3782 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3783 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3784 fwdtsn.ch.chunk_flags = 0;
3785 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3786 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3790 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3791 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3792 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3794 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3795 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3796 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3797 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3798 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3799 atomic_add_int(&asoc->sending_seq, 1);
3800 /* save off historical data for retrans */
3801 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3802 asoc->last_sending_seq[0] = asoc->sending_seq;
3803 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3804 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3805 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3806 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3807 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3808 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3810 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3811 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3812 asoc->str_reset_seq_in++;
3813 } else if (asoc->str_reset_seq_in - 1 == seq) {
3814 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3815 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3816 } else if (asoc->str_reset_seq_in - 2 == seq) {
3817 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3818 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3820 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3826 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3827 struct sctp_tmit_chunk *chk,
3828 struct sctp_stream_reset_out_request *req, int trunc)
3831 int number_entries, len;
3832 struct sctp_association *asoc = &stcb->asoc;
3834 seq = ntohl(req->request_seq);
3836 /* now if its not a duplicate we process it */
3837 if (asoc->str_reset_seq_in == seq) {
3838 len = ntohs(req->ph.param_length);
3839 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3841 * the sender is resetting, handle the list issue.. we must
3842 * a) verify if we can do the reset, if so no problem b) If
3843 * we can't do the reset we must copy the request. c) queue
3844 * it, and setup the data in processor to trigger it off
3845 * when needed and dequeue all the queued data.
3847 tsn = ntohl(req->send_reset_at_tsn);
3849 /* move the reset action back one */
3850 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3851 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3852 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3854 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3855 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3856 /* we can do it now */
3857 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3858 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3861 * we must queue it up and thus wait for the TSN's
3862 * to arrive that are at or before tsn
3864 struct sctp_stream_reset_list *liste;
3867 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3868 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3869 siz, SCTP_M_STRESET);
3870 if (liste == NULL) {
3871 /* gak out of memory */
3872 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3873 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3877 liste->number_entries = number_entries;
3878 memcpy(&liste->req, req,
3879 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3880 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3881 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3883 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3884 asoc->str_reset_seq_in++;
3885 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3887 * one seq back, just echo back last action since my
3888 * response was lost.
3890 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3891 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3893 * two seq back, just echo back last action since my
3894 * response was lost.
3896 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3898 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3903 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3904 struct sctp_stream_reset_add_strm *str_add)
3907 * Peer is requesting to add more streams. If its within our
3908 * max-streams we will allow it.
3910 uint32_t num_stream, i;
3912 struct sctp_association *asoc = &stcb->asoc;
3913 struct sctp_queued_to_read *ctl, *nctl;
3915 /* Get the number. */
3916 seq = ntohl(str_add->request_seq);
3917 num_stream = ntohs(str_add->number_of_streams);
3918 /* Now what would be the new total? */
3919 if (asoc->str_reset_seq_in == seq) {
3920 num_stream += stcb->asoc.streamincnt;
3921 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3922 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3923 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3924 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3925 (num_stream > 0xffff)) {
3926 /* We must reject it they ask for to many */
3928 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3930 /* Ok, we can do that :-) */
3931 struct sctp_stream_in *oldstrm;
3933 /* save off the old */
3934 oldstrm = stcb->asoc.strmin;
3935 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3936 (num_stream * sizeof(struct sctp_stream_in)),
3938 if (stcb->asoc.strmin == NULL) {
3939 stcb->asoc.strmin = oldstrm;
3942 /* copy off the old data */
3943 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3944 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3945 stcb->asoc.strmin[i].stream_no = i;
3946 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3947 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3948 /* now anything on those queues? */
3949 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
3950 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3951 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3954 /* Init the new streams */
3955 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3956 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3957 stcb->asoc.strmin[i].stream_no = i;
3958 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3959 stcb->asoc.strmin[i].delivery_started = 0;
3961 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3962 /* update the size */
3963 stcb->asoc.streamincnt = num_stream;
3964 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3965 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3967 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3968 asoc->str_reset_seq_in++;
3969 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3971 * one seq back, just echo back last action since my
3972 * response was lost.
3974 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3975 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3977 * two seq back, just echo back last action since my
3978 * response was lost.
3980 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3982 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3988 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3989 struct sctp_stream_reset_add_strm *str_add)
3992 * Peer is requesting to add more streams. If its within our
3993 * max-streams we will allow it.
3995 uint16_t num_stream;
3997 struct sctp_association *asoc = &stcb->asoc;
3999 /* Get the number. */
4000 seq = ntohl(str_add->request_seq);
4001 num_stream = ntohs(str_add->number_of_streams);
4002 /* Now what would be the new total? */
4003 if (asoc->str_reset_seq_in == seq) {
4004 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4005 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4006 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4007 } else if (stcb->asoc.stream_reset_outstanding) {
4008 /* We must reject it we have something pending */
4009 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4011 /* Ok, we can do that :-) */
4014 mychk = stcb->asoc.streamoutcnt;
4015 mychk += num_stream;
4016 if (mychk < 0x10000) {
4017 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4018 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4019 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4022 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4025 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4026 asoc->str_reset_seq_in++;
4027 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4029 * one seq back, just echo back last action since my
4030 * response was lost.
4032 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4033 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4035 * two seq back, just echo back last action since my
4036 * response was lost.
4038 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4040 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4045 __attribute__((noinline))
4048 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4049 struct sctp_stream_reset_out_req *sr_req)
4051 int chk_length, param_len, ptype;
4052 struct sctp_paramhdr pstore;
4053 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4057 struct sctp_tmit_chunk *chk;
4058 struct sctp_chunkhdr *ch;
4059 struct sctp_paramhdr *ph;
4063 /* now it may be a reset or a reset-response */
4064 chk_length = ntohs(sr_req->ch.chunk_length);
4066 /* setup for adding the response */
4067 sctp_alloc_a_chunk(stcb, chk);
4071 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4072 chk->rec.chunk_id.can_take_data = 0;
4073 chk->asoc = &stcb->asoc;
4074 chk->no_fr_allowed = 0;
4075 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4076 chk->book_size_scale = 0;
4077 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4078 if (chk->data == NULL) {
4081 sctp_m_freem(chk->data);
4084 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4087 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4089 /* setup chunk parameters */
4090 chk->sent = SCTP_DATAGRAM_UNSENT;
4094 ch = mtod(chk->data, struct sctp_chunkhdr *);
4095 ch->chunk_type = SCTP_STREAM_RESET;
4096 ch->chunk_flags = 0;
4097 ch->chunk_length = htons(chk->send_size);
4098 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4099 offset += sizeof(struct sctp_chunkhdr);
4100 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4101 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
4104 param_len = ntohs(ph->param_length);
4105 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4109 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4110 (uint8_t *) & cstore);
4111 ptype = ntohs(ph->param_type);
4113 if (param_len > (int)sizeof(cstore)) {
4118 if (num_param > SCTP_MAX_RESET_PARAMS) {
4119 /* hit the max of parameters already sorry.. */
4122 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4123 struct sctp_stream_reset_out_request *req_out;
4125 req_out = (struct sctp_stream_reset_out_request *)ph;
4127 if (stcb->asoc.stream_reset_outstanding) {
4128 seq = ntohl(req_out->response_seq);
4129 if (seq == stcb->asoc.str_reset_seq_out) {
4131 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4134 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4135 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4136 struct sctp_stream_reset_add_strm *str_add;
4138 str_add = (struct sctp_stream_reset_add_strm *)ph;
4140 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4141 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4142 struct sctp_stream_reset_add_strm *str_add;
4144 str_add = (struct sctp_stream_reset_add_strm *)ph;
4146 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4147 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4148 struct sctp_stream_reset_in_request *req_in;
4151 req_in = (struct sctp_stream_reset_in_request *)ph;
4152 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4153 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4154 struct sctp_stream_reset_tsn_request *req_tsn;
4157 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4158 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4160 goto strres_nochunk;
4164 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4165 struct sctp_stream_reset_response *resp;
4168 resp = (struct sctp_stream_reset_response *)ph;
4169 seq = ntohl(resp->response_seq);
4170 result = ntohl(resp->result);
4171 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4173 goto strres_nochunk;
4178 offset += SCTP_SIZE32(param_len);
4179 chk_length -= SCTP_SIZE32(param_len);
4182 /* we have no response free the stuff */
4183 goto strres_nochunk;
4185 /* ok we have a chunk to link in */
4186 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4189 stcb->asoc.ctrl_queue_cnt++;
4194 * Handle a router or endpoints report of a packet loss, there are two ways
4195 * to handle this, either we get the whole packet and must disect it
4196 * ourselves (possibly with truncation and or corruption) or it is a summary
4197 * from a middle box that did the disectting for us.
4200 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4201 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4203 uint32_t bottle_bw, on_queue;
4207 struct sctp_chunk_desc desc;
4208 struct sctp_chunkhdr *ch;
4210 chlen = ntohs(cp->ch.chunk_length);
4211 chlen -= sizeof(struct sctp_pktdrop_chunk);
4212 /* XXX possible chlen underflow */
4215 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4216 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4218 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4219 chlen -= sizeof(struct sctphdr);
4220 /* XXX possible chlen underflow */
4221 memset(&desc, 0, sizeof(desc));
4223 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4224 if (trunc_len > limit) {
4227 /* now the chunks themselves */
4228 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4229 desc.chunk_type = ch->chunk_type;
4230 /* get amount we need to move */
4231 at = ntohs(ch->chunk_length);
4232 if (at < sizeof(struct sctp_chunkhdr)) {
4233 /* corrupt chunk, maybe at the end? */
4234 SCTP_STAT_INCR(sctps_pdrpcrupt);
4237 if (trunc_len == 0) {
4238 /* we are supposed to have all of it */
4240 /* corrupt skip it */
4241 SCTP_STAT_INCR(sctps_pdrpcrupt);
4245 /* is there enough of it left ? */
4246 if (desc.chunk_type == SCTP_DATA) {
4247 if (chlen < (sizeof(struct sctp_data_chunk) +
4248 sizeof(desc.data_bytes))) {
4252 if (chlen < sizeof(struct sctp_chunkhdr)) {
4257 if (desc.chunk_type == SCTP_DATA) {
4258 /* can we get out the tsn? */
4259 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4260 SCTP_STAT_INCR(sctps_pdrpmbda);
4262 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4264 struct sctp_data_chunk *dcp;
4268 dcp = (struct sctp_data_chunk *)ch;
4269 ddp = (uint8_t *) (dcp + 1);
4270 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4271 desc.data_bytes[iii] = ddp[iii];
4273 desc.tsn_ifany = dcp->dp.tsn;
4275 /* nope we are done. */
4276 SCTP_STAT_INCR(sctps_pdrpnedat);
4280 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4281 SCTP_STAT_INCR(sctps_pdrpmbct);
4284 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4285 SCTP_STAT_INCR(sctps_pdrppdbrk);
4288 if (SCTP_SIZE32(at) > chlen) {
4291 chlen -= SCTP_SIZE32(at);
4292 if (chlen < sizeof(struct sctp_chunkhdr)) {
4293 /* done, none left */
4296 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4298 /* Now update any rwnd --- possibly */
4299 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4300 /* From a peer, we get a rwnd report */
4303 SCTP_STAT_INCR(sctps_pdrpfehos);
4305 bottle_bw = ntohl(cp->bottle_bw);
4306 on_queue = ntohl(cp->current_onq);
4307 if (bottle_bw && on_queue) {
4308 /* a rwnd report is in here */
4309 if (bottle_bw > on_queue)
4310 a_rwnd = bottle_bw - on_queue;
4315 stcb->asoc.peers_rwnd = 0;
4317 if (a_rwnd > stcb->asoc.total_flight) {
4318 stcb->asoc.peers_rwnd =
4319 a_rwnd - stcb->asoc.total_flight;
4321 stcb->asoc.peers_rwnd = 0;
4323 if (stcb->asoc.peers_rwnd <
4324 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4325 /* SWS sender side engages */
4326 stcb->asoc.peers_rwnd = 0;
4331 SCTP_STAT_INCR(sctps_pdrpfmbox);
4334 /* now middle boxes in sat networks get a cwnd bump */
4335 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4336 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4337 (stcb->asoc.sat_network)) {
4339 * This is debateable but for sat networks it makes sense
4340 * Note if a T3 timer has went off, we will prohibit any
4341 * changes to cwnd until we exit the t3 loss recovery.
4343 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4344 net, cp, &bottle_bw, &on_queue);
4349 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4350 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4351 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4352 * length of the complete packet outputs: - length: modified to remaining
4353 * length after control processing - netp: modified to new sctp_nets after
4354 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4355 * bad packet,...) otherwise return the tcb for this packet
4358 __attribute__((noinline))
4360 static struct sctp_tcb *
4361 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4362 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4363 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4364 uint32_t vrf_id, uint16_t port)
4366 struct sctp_association *asoc;
4368 int num_chunks = 0; /* number of control chunks processed */
4369 uint32_t chk_length;
4371 int abort_no_unlock = 0;
4375 * How big should this be, and should it be alloc'd? Lets try the
4376 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4377 * until we get into jumbo grams and such..
4379 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4380 struct sctp_tcb *locked_tcb = stcb;
4382 uint32_t auth_offset = 0, auth_len = 0;
4383 int auth_skipped = 0;
4386 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4391 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4392 iphlen, *offset, length, stcb);
4394 /* validate chunk header length... */
4395 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4396 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4397 ntohs(ch->chunk_length));
4399 SCTP_TCB_UNLOCK(locked_tcb);
4404 * validate the verification tag
4406 vtag_in = ntohl(sh->v_tag);
4409 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4411 if (ch->chunk_type == SCTP_INITIATION) {
4412 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4413 ntohs(ch->chunk_length), vtag_in);
4415 /* protocol error- silently discard... */
4416 SCTP_STAT_INCR(sctps_badvtag);
4418 SCTP_TCB_UNLOCK(locked_tcb);
4422 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4424 * If there is no stcb, skip the AUTH chunk and process
4425 * later after a stcb is found (to validate the lookup was
4428 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4430 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4431 /* save this chunk for later processing */
4433 auth_offset = *offset;
4434 auth_len = ntohs(ch->chunk_length);
4436 /* (temporarily) move past this chunk */
4437 *offset += SCTP_SIZE32(auth_len);
4438 if (*offset >= length) {
4439 /* no more data left in the mbuf chain */
4442 SCTP_TCB_UNLOCK(locked_tcb);
4446 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4447 sizeof(struct sctp_chunkhdr), chunk_buf);
4453 SCTP_TCB_UNLOCK(locked_tcb);
4457 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4458 goto process_control_chunks;
4461 * first check if it's an ASCONF with an unknown src addr we
4462 * need to look inside to find the association
4464 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4465 struct sctp_chunkhdr *asconf_ch = ch;
4466 uint32_t asconf_offset = 0, asconf_len = 0;
4468 /* inp's refcount may be reduced */
4469 SCTP_INP_INCR_REF(inp);
4471 asconf_offset = *offset;
4473 asconf_len = ntohs(asconf_ch->chunk_length);
4474 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4476 stcb = sctp_findassociation_ep_asconf(m,
4477 *offset, sh, &inp, netp, vrf_id);
4480 asconf_offset += SCTP_SIZE32(asconf_len);
4481 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4482 sizeof(struct sctp_chunkhdr), chunk_buf);
4483 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4486 * reduce inp's refcount if not reduced in
4487 * sctp_findassociation_ep_asconf().
4489 SCTP_INP_DECR_REF(inp);
4494 /* now go back and verify any auth chunk to be sure */
4495 if (auth_skipped && (stcb != NULL)) {
4496 struct sctp_auth_chunk *auth;
4498 auth = (struct sctp_auth_chunk *)
4499 sctp_m_getptr(m, auth_offset,
4500 auth_len, chunk_buf);
4503 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4505 /* auth HMAC failed so dump it */
4508 SCTP_TCB_UNLOCK(locked_tcb);
4512 /* remaining chunks are HMAC checked */
4513 stcb->asoc.authenticated = 1;
4518 /* no association, so it's out of the blue... */
4519 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4523 SCTP_TCB_UNLOCK(locked_tcb);
4528 /* ABORT and SHUTDOWN can use either v_tag... */
4529 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4530 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4531 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4532 if ((vtag_in == asoc->my_vtag) ||
4533 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4534 (vtag_in == asoc->peer_vtag))) {
4537 /* drop this packet... */
4538 SCTP_STAT_INCR(sctps_badvtag);
4540 SCTP_TCB_UNLOCK(locked_tcb);
4544 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4545 if (vtag_in != asoc->my_vtag) {
4547 * this could be a stale SHUTDOWN-ACK or the
4548 * peer never got the SHUTDOWN-COMPLETE and
4549 * is still hung; we have started a new asoc
4550 * but it won't complete until the shutdown
4554 SCTP_TCB_UNLOCK(locked_tcb);
4556 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4561 /* for all other chunks, vtag must match */
4562 if (vtag_in != asoc->my_vtag) {
4563 /* invalid vtag... */
4564 SCTPDBG(SCTP_DEBUG_INPUT3,
4565 "invalid vtag: %xh, expect %xh\n",
4566 vtag_in, asoc->my_vtag);
4567 SCTP_STAT_INCR(sctps_badvtag);
4569 SCTP_TCB_UNLOCK(locked_tcb);
4575 } /* end if !SCTP_COOKIE_ECHO */
4577 * process all control chunks...
4579 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4580 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4581 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4582 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4583 /* implied cookie-ack.. we must have lost the ack */
4584 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4585 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4586 stcb->asoc.overall_error_count,
4588 SCTP_FROM_SCTP_INPUT,
4591 stcb->asoc.overall_error_count = 0;
4592 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4595 process_control_chunks:
4596 while (IS_SCTP_CONTROL(ch)) {
4597 /* validate chunk length */
4598 chk_length = ntohs(ch->chunk_length);
4599 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4600 ch->chunk_type, chk_length);
4601 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4602 if (chk_length < sizeof(*ch) ||
4603 (*offset + (int)chk_length) > length) {
4606 SCTP_TCB_UNLOCK(locked_tcb);
4610 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4612 * INIT-ACK only gets the init ack "header" portion only
4613 * because we don't have to process the peer's COOKIE. All
4614 * others get a complete chunk.
4616 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4617 (ch->chunk_type == SCTP_INITIATION)) {
4618 /* get an init-ack chunk */
4619 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4620 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4624 SCTP_TCB_UNLOCK(locked_tcb);
4629 /* For cookies and all other chunks. */
4630 if (chk_length > sizeof(chunk_buf)) {
4632 * use just the size of the chunk buffer so
4633 * the front part of our chunks fit in
4634 * contiguous space up to the chunk buffer
4635 * size (508 bytes). For chunks that need to
4636 * get more than that they must use the
4637 * sctp_m_getptr() function or other means
4638 * (e.g. know how to parse mbuf chains).
4639 * Cookies do this already.
4641 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4642 (sizeof(chunk_buf) - 4),
4647 SCTP_TCB_UNLOCK(locked_tcb);
4652 /* We can fit it all */
4653 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4654 chk_length, chunk_buf);
4656 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4659 SCTP_TCB_UNLOCK(locked_tcb);
4666 /* Save off the last place we got a control from */
4668 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4670 * allow last_control to be NULL if
4671 * ASCONF... ASCONF processing will find the
4674 if ((netp != NULL) && (*netp != NULL))
4675 stcb->asoc.last_control_chunk_from = *netp;
4678 #ifdef SCTP_AUDITING_ENABLED
4679 sctp_audit_log(0xB0, ch->chunk_type);
4682 /* check to see if this chunk required auth, but isn't */
4683 if ((stcb != NULL) &&
4684 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4685 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4686 !stcb->asoc.authenticated) {
4687 /* "silently" ignore */
4688 SCTP_STAT_INCR(sctps_recvauthmissing);
4691 switch (ch->chunk_type) {
4692 case SCTP_INITIATION:
4693 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4694 /* The INIT chunk must be the only chunk. */
4695 if ((num_chunks > 1) ||
4696 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4697 sctp_abort_association(inp, stcb, m,
4698 iphlen, sh, NULL, vrf_id, port);
4702 /* Honor our resource limit. */
4703 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4704 struct mbuf *op_err;
4706 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
4707 sctp_abort_association(inp, stcb, m,
4708 iphlen, sh, op_err, vrf_id, port);
4712 sctp_handle_init(m, iphlen, *offset, sh,
4713 (struct sctp_init_chunk *)ch, inp,
4714 stcb, &abort_no_unlock, vrf_id, port);
4716 if ((!abort_no_unlock) && (locked_tcb)) {
4717 SCTP_TCB_UNLOCK(locked_tcb);
4721 case SCTP_PAD_CHUNK:
4723 case SCTP_INITIATION_ACK:
4724 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4725 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4726 /* We are not interested anymore */
4727 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4730 if (locked_tcb != stcb) {
4732 SCTP_TCB_UNLOCK(locked_tcb);
4736 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4737 so = SCTP_INP_SO(inp);
4738 atomic_add_int(&stcb->asoc.refcnt, 1);
4739 SCTP_TCB_UNLOCK(stcb);
4740 SCTP_SOCKET_LOCK(so, 1);
4741 SCTP_TCB_LOCK(stcb);
4742 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4744 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4745 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4746 SCTP_SOCKET_UNLOCK(so, 1);
4752 /* The INIT-ACK chunk must be the only chunk. */
4753 if ((num_chunks > 1) ||
4754 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4757 SCTP_TCB_UNLOCK(locked_tcb);
4761 if ((netp) && (*netp)) {
4762 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4763 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4768 if (abort_no_unlock) {
4772 * Special case, I must call the output routine to
4773 * get the cookie echoed
4775 if ((stcb != NULL) && (ret == 0)) {
4776 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4779 SCTP_TCB_UNLOCK(locked_tcb);
4783 case SCTP_SELECTIVE_ACK:
4785 struct sctp_sack_chunk *sack;
4787 uint32_t a_rwnd, cum_ack;
4788 uint16_t num_seg, num_dup;
4790 int offset_seg, offset_dup;
4792 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4793 SCTP_STAT_INCR(sctps_recvsacks);
4795 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4798 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4799 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4802 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4804 * If we have sent a shutdown-ack, we will pay no
4805 * attention to a sack sent in to us since
4806 * we don't care anymore.
4810 sack = (struct sctp_sack_chunk *)ch;
4811 flags = ch->chunk_flags;
4812 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4813 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4814 num_dup = ntohs(sack->sack.num_dup_tsns);
4815 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4816 if (sizeof(struct sctp_sack_chunk) +
4817 num_seg * sizeof(struct sctp_gap_ack_block) +
4818 num_dup * sizeof(uint32_t) != chk_length) {
4819 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4822 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4823 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4824 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4825 cum_ack, num_seg, a_rwnd);
4826 stcb->asoc.seen_a_sack_this_pkt = 1;
4827 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4829 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4830 (stcb->asoc.saw_sack_with_frags == 0) &&
4831 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4832 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4835 * We have a SIMPLE sack having no
4836 * prior segments and data on sent
4837 * queue to be acked.. Use the
4838 * faster path sack processing. We
4839 * also allow window update sacks
4840 * with no missing segments to go
4843 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4846 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4847 num_seg, 0, num_dup, &abort_now, flags,
4848 cum_ack, a_rwnd, ecne_seen);
4851 /* ABORT signal from sack processing */
4855 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4856 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4857 (stcb->asoc.stream_queue_cnt == 0)) {
4858 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4863 * EY - nr_sack: If the received chunk is an
4866 case SCTP_NR_SELECTIVE_ACK:
4868 struct sctp_nr_sack_chunk *nr_sack;
4870 uint32_t a_rwnd, cum_ack;
4871 uint16_t num_seg, num_nr_seg, num_dup;
4873 int offset_seg, offset_dup;
4875 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4876 SCTP_STAT_INCR(sctps_recvsacks);
4878 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4881 if ((stcb->asoc.sctp_nr_sack_on_off == 0) ||
4882 (stcb->asoc.peer_supports_nr_sack == 0)) {
4885 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4886 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4889 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4891 * If we have sent a shutdown-ack, we will pay no
4892 * attention to a sack sent in to us since
4893 * we don't care anymore.
4897 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4898 flags = ch->chunk_flags;
4899 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4900 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4901 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4902 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4903 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4904 if (sizeof(struct sctp_nr_sack_chunk) +
4905 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4906 num_dup * sizeof(uint32_t) != chk_length) {
4907 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4910 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4911 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4912 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4913 cum_ack, num_seg, a_rwnd);
4914 stcb->asoc.seen_a_sack_this_pkt = 1;
4915 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4916 (num_seg == 0) && (num_nr_seg == 0) &&
4917 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4918 (stcb->asoc.saw_sack_with_frags == 0) &&
4919 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4920 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4922 * We have a SIMPLE sack having no
4923 * prior segments and data on sent
4924 * queue to be acked. Use the faster
4925 * path sack processing. We also
4926 * allow window update sacks with no
4927 * missing segments to go this way
4930 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4931 &abort_now, ecne_seen);
4934 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4935 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4936 cum_ack, a_rwnd, ecne_seen);
4939 /* ABORT signal from sack processing */
4943 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4944 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4945 (stcb->asoc.stream_queue_cnt == 0)) {
4946 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4951 case SCTP_HEARTBEAT_REQUEST:
4952 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4953 if ((stcb) && netp && *netp) {
4954 SCTP_STAT_INCR(sctps_recvheartbeat);
4955 sctp_send_heartbeat_ack(stcb, m, *offset,
4958 /* He's alive so give him credit */
4959 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4960 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4961 stcb->asoc.overall_error_count,
4963 SCTP_FROM_SCTP_INPUT,
4966 stcb->asoc.overall_error_count = 0;
4969 case SCTP_HEARTBEAT_ACK:
4970 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4971 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4975 SCTP_TCB_UNLOCK(locked_tcb);
4979 /* He's alive so give him credit */
4980 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4981 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4982 stcb->asoc.overall_error_count,
4984 SCTP_FROM_SCTP_INPUT,
4987 stcb->asoc.overall_error_count = 0;
4988 SCTP_STAT_INCR(sctps_recvheartbeatack);
4990 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4993 case SCTP_ABORT_ASSOCIATION:
4994 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4996 if ((stcb) && netp && *netp)
4997 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5003 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5005 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5008 SCTP_TCB_UNLOCK(locked_tcb);
5012 if (netp && *netp) {
5015 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5016 stcb, *netp, &abort_flag);
5023 case SCTP_SHUTDOWN_ACK:
5024 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
5025 if ((stcb) && (netp) && (*netp))
5026 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5031 case SCTP_OPERATION_ERROR:
5032 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5033 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5038 case SCTP_COOKIE_ECHO:
5039 SCTPDBG(SCTP_DEBUG_INPUT3,
5040 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
5041 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5044 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5045 /* We are not interested anymore */
5048 SCTP_TCB_UNLOCK(stcb);
5055 * First are we accepting? We do this again here
5056 * since it is possible that a previous endpoint WAS
5057 * listening responded to a INIT-ACK and then
5058 * closed. We opened and bound.. and are now no
5062 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5063 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5064 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5065 struct mbuf *op_err;
5067 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
5068 sctp_abort_association(inp, stcb, m,
5069 iphlen, sh, op_err, vrf_id, port);
5074 struct mbuf *ret_buf;
5075 struct sctp_inpcb *linp;
5084 SCTP_ASOC_CREATE_LOCK(linp);
5085 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5086 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5087 SCTP_ASOC_CREATE_UNLOCK(linp);
5093 sctp_handle_cookie_echo(m, iphlen,
5095 (struct sctp_cookie_echo_chunk *)ch,
5107 SCTP_ASOC_CREATE_UNLOCK(linp);
5109 if (ret_buf == NULL) {
5111 SCTP_TCB_UNLOCK(locked_tcb);
5113 SCTPDBG(SCTP_DEBUG_INPUT3,
5114 "GAK, null buffer\n");
5118 /* if AUTH skipped, see if it verified... */
5123 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5125 * Restart the timer if we have
5128 struct sctp_tmit_chunk *chk;
5130 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5131 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5135 case SCTP_COOKIE_ACK:
5136 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
5137 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5139 SCTP_TCB_UNLOCK(locked_tcb);
5143 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5144 /* We are not interested anymore */
5145 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5148 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5149 so = SCTP_INP_SO(inp);
5150 atomic_add_int(&stcb->asoc.refcnt, 1);
5151 SCTP_TCB_UNLOCK(stcb);
5152 SCTP_SOCKET_LOCK(so, 1);
5153 SCTP_TCB_LOCK(stcb);
5154 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5156 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
5157 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5158 SCTP_SOCKET_UNLOCK(so, 1);
5164 /* He's alive so give him credit */
5165 if ((stcb) && netp && *netp) {
5166 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5167 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5168 stcb->asoc.overall_error_count,
5170 SCTP_FROM_SCTP_INPUT,
5173 stcb->asoc.overall_error_count = 0;
5174 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5178 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5179 /* He's alive so give him credit */
5180 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5183 SCTP_TCB_UNLOCK(locked_tcb);
5189 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5190 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5191 stcb->asoc.overall_error_count,
5193 SCTP_FROM_SCTP_INPUT,
5196 stcb->asoc.overall_error_count = 0;
5197 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5203 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5204 /* He's alive so give him credit */
5205 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5208 SCTP_TCB_UNLOCK(locked_tcb);
5214 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5215 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5216 stcb->asoc.overall_error_count,
5218 SCTP_FROM_SCTP_INPUT,
5221 stcb->asoc.overall_error_count = 0;
5222 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5225 case SCTP_SHUTDOWN_COMPLETE:
5226 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5227 /* must be first and only chunk */
5228 if ((num_chunks > 1) ||
5229 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5232 SCTP_TCB_UNLOCK(locked_tcb);
5236 if ((stcb) && netp && *netp) {
5237 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5244 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5245 /* He's alive so give him credit */
5247 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5248 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5249 stcb->asoc.overall_error_count,
5251 SCTP_FROM_SCTP_INPUT,
5254 stcb->asoc.overall_error_count = 0;
5255 sctp_handle_asconf(m, *offset,
5256 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5260 case SCTP_ASCONF_ACK:
5261 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5262 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5265 SCTP_TCB_UNLOCK(locked_tcb);
5270 if ((stcb) && netp && *netp) {
5271 /* He's alive so give him credit */
5272 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5273 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5274 stcb->asoc.overall_error_count,
5276 SCTP_FROM_SCTP_INPUT,
5279 stcb->asoc.overall_error_count = 0;
5280 sctp_handle_asconf_ack(m, *offset,
5281 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5282 if (abort_no_unlock)
5286 case SCTP_FORWARD_CUM_TSN:
5287 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5288 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5291 SCTP_TCB_UNLOCK(locked_tcb);
5296 /* He's alive so give him credit */
5300 stcb->asoc.overall_error_count = 0;
5301 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5302 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5303 stcb->asoc.overall_error_count,
5305 SCTP_FROM_SCTP_INPUT,
5309 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5310 /* We are not interested anymore */
5311 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5312 so = SCTP_INP_SO(inp);
5313 atomic_add_int(&stcb->asoc.refcnt, 1);
5314 SCTP_TCB_UNLOCK(stcb);
5315 SCTP_SOCKET_LOCK(so, 1);
5316 SCTP_TCB_LOCK(stcb);
5317 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5319 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5320 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5321 SCTP_SOCKET_UNLOCK(so, 1);
5326 sctp_handle_forward_tsn(stcb,
5327 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5332 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5333 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5334 stcb->asoc.overall_error_count,
5336 SCTP_FROM_SCTP_INPUT,
5339 stcb->asoc.overall_error_count = 0;
5344 case SCTP_STREAM_RESET:
5345 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5346 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5349 SCTP_TCB_UNLOCK(locked_tcb);
5354 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5355 /* We are not interested anymore */
5356 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5357 so = SCTP_INP_SO(inp);
5358 atomic_add_int(&stcb->asoc.refcnt, 1);
5359 SCTP_TCB_UNLOCK(stcb);
5360 SCTP_SOCKET_LOCK(so, 1);
5361 SCTP_TCB_LOCK(stcb);
5362 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5364 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5365 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5366 SCTP_SOCKET_UNLOCK(so, 1);
5371 if (stcb->asoc.peer_supports_strreset == 0) {
5373 * hmm, peer should have announced this, but
5374 * we will turn it on since he is sending us
5377 stcb->asoc.peer_supports_strreset = 1;
5379 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5380 /* stop processing */
5385 case SCTP_PACKET_DROPPED:
5386 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5387 /* re-get it all please */
5388 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5391 SCTP_TCB_UNLOCK(locked_tcb);
5396 if (ch && (stcb) && netp && (*netp)) {
5397 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5399 min(chk_length, (sizeof(chunk_buf) - 4)));
5404 case SCTP_AUTHENTICATION:
5405 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5406 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5410 /* save the first AUTH for later processing */
5411 if (auth_skipped == 0) {
5412 auth_offset = *offset;
5413 auth_len = chk_length;
5416 /* skip this chunk (temporarily) */
5419 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5420 (chk_length > (sizeof(struct sctp_auth_chunk) +
5421 SCTP_AUTH_DIGEST_LEN_MAX))) {
5424 SCTP_TCB_UNLOCK(locked_tcb);
5429 if (got_auth == 1) {
5430 /* skip this chunk... it's already auth'd */
5434 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5436 /* auth HMAC failed so dump the packet */
5440 /* remaining chunks are HMAC checked */
5441 stcb->asoc.authenticated = 1;
5447 /* it's an unknown chunk! */
5448 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5450 struct sctp_paramhdr *phd;
5452 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5453 0, M_DONTWAIT, 1, MT_DATA);
5455 phd = mtod(mm, struct sctp_paramhdr *);
5457 * We cheat and use param type since
5458 * we did not bother to define a
5459 * error cause struct. They are the
5460 * same basic format with different
5463 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5464 phd->param_length = htons(chk_length + sizeof(*phd));
5465 SCTP_BUF_LEN(mm) = sizeof(*phd);
5466 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
5467 if (SCTP_BUF_NEXT(mm)) {
5468 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
5471 #ifdef SCTP_MBUF_LOGGING
5472 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5475 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5476 if (SCTP_BUF_IS_EXTENDED(mat)) {
5477 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5482 sctp_queue_op_err(stcb, mm);
5489 if ((ch->chunk_type & 0x80) == 0) {
5490 /* discard this packet */
5493 } /* else skip this bad chunk and continue... */
5495 } /* switch (ch->chunk_type) */
5499 /* get the next chunk */
5500 *offset += SCTP_SIZE32(chk_length);
5501 if (*offset >= length) {
5502 /* no more data left in the mbuf chain */
5505 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5506 sizeof(struct sctp_chunkhdr), chunk_buf);
5509 SCTP_TCB_UNLOCK(locked_tcb);
5516 if (asconf_cnt > 0 && stcb != NULL) {
5517 sctp_send_asconf_ack(stcb);
5525 __attribute__((noinline))
5528 sctp_validate_no_locks(struct sctp_inpcb *inp)
5530 struct sctp_tcb *lstcb;
5532 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5533 if (mtx_owned(&lstcb->tcb_mtx)) {
5534 panic("Own lock on stcb at return from input");
5537 if (mtx_owned(&inp->inp_create_mtx)) {
5538 panic("Own create lock on inp");
5540 if (mtx_owned(&inp->inp_mtx)) {
5541 panic("Own inp lock on inp");
5548 * common input chunk processing (v4 and v6)
5551 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5552 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5553 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5554 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5557 * Control chunk processing
5560 int fwd_tsn_seen = 0, data_processed = 0;
5561 struct mbuf *m = *mm;
5563 int cnt_ctrl_ready = 0;
5565 SCTP_STAT_INCR(sctps_recvdatagrams);
5566 #ifdef SCTP_AUDITING_ENABLED
5567 sctp_audit_log(0xE0, 1);
5568 sctp_auditing(0, inp, stcb, net);
5571 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5572 m, iphlen, offset, length, stcb);
5574 /* always clear this before beginning a packet */
5575 stcb->asoc.authenticated = 0;
5576 stcb->asoc.seen_a_sack_this_pkt = 0;
5577 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5578 stcb, stcb->asoc.state);
5580 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5581 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5583 * If we hit here, we had a ref count
5584 * up when the assoc was aborted and the
5585 * timer is clearing out the assoc, we should
5586 * NOT respond to any packet.. its OOTB.
5588 SCTP_TCB_UNLOCK(stcb);
5589 sctp_handle_ootb(m, iphlen, offset, sh, inp,
5594 if (IS_SCTP_CONTROL(ch)) {
5595 /* process the control portion of the SCTP packet */
5596 /* sa_ignore NO_NULL_CHK */
5597 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5598 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5601 * This covers us if the cookie-echo was there and
5602 * it changes our INP.
5604 inp = stcb->sctp_ep;
5605 if ((net) && (port)) {
5606 if (net->port == 0) {
5607 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5614 * no control chunks, so pre-process DATA chunks (these
5615 * checks are taken care of by control processing)
5619 * if DATA only packet, and auth is required, then punt...
5620 * can't have authenticated without any AUTH (control)
5623 if ((stcb != NULL) &&
5624 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5625 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5626 /* "silently" ignore */
5627 SCTP_STAT_INCR(sctps_recvauthmissing);
5628 SCTP_TCB_UNLOCK(stcb);
5632 /* out of the blue DATA chunk */
5633 sctp_handle_ootb(m, iphlen, offset, sh, inp,
5637 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5638 /* v_tag mismatch! */
5639 SCTP_STAT_INCR(sctps_badvtag);
5640 SCTP_TCB_UNLOCK(stcb);
5647 * no valid TCB for this packet, or we found it's a bad
5648 * packet while processing control, or we're done with this
5649 * packet (done or skip rest of data), so we drop it...
5654 * DATA chunk processing
5656 /* plow through the data chunks while length > offset */
5659 * Rest should be DATA only. Check authentication state if AUTH for
5662 if ((length > offset) &&
5664 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5665 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5666 !stcb->asoc.authenticated) {
5667 /* "silently" ignore */
5668 SCTP_STAT_INCR(sctps_recvauthmissing);
5669 SCTPDBG(SCTP_DEBUG_AUTH1,
5670 "Data chunk requires AUTH, skipped\n");
5673 if (length > offset) {
5677 * First check to make sure our state is correct. We would
5678 * not get here unless we really did have a tag, so we don't
5679 * abort if this happens, just dump the chunk silently.
5681 switch (SCTP_GET_STATE(&stcb->asoc)) {
5682 case SCTP_STATE_COOKIE_ECHOED:
5684 * we consider data with valid tags in this state
5685 * shows us the cookie-ack was lost. Imply it was
5688 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5689 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5690 stcb->asoc.overall_error_count,
5692 SCTP_FROM_SCTP_INPUT,
5695 stcb->asoc.overall_error_count = 0;
5696 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5698 case SCTP_STATE_COOKIE_WAIT:
5700 * We consider OOTB any data sent during asoc setup.
5702 sctp_handle_ootb(m, iphlen, offset, sh, inp,
5704 SCTP_TCB_UNLOCK(stcb);
5706 /* sa_ignore NOTREACHED */
5708 case SCTP_STATE_EMPTY: /* should not happen */
5709 case SCTP_STATE_INUSE: /* should not happen */
5710 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5711 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5713 SCTP_TCB_UNLOCK(stcb);
5715 /* sa_ignore NOTREACHED */
5717 case SCTP_STATE_OPEN:
5718 case SCTP_STATE_SHUTDOWN_SENT:
5721 /* plow through the data chunks while length > offset */
5722 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5723 inp, stcb, net, &high_tsn);
5726 * The association aborted, NO UNLOCK needed since
5727 * the association is destroyed.
5733 * Anything important needs to have been m_copy'ed in
5737 /* take care of ecn */
5738 if ((data_processed == 1) &&
5739 (stcb->asoc.ecn_allowed == 1) &&
5740 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5741 /* Yep, we need to add a ECNE */
5742 sctp_send_ecn_echo(stcb, net, high_tsn);
5744 if ((data_processed == 0) && (fwd_tsn_seen)) {
5746 uint32_t highest_tsn;
5748 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5749 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5751 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5753 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5754 stcb->asoc.send_sack = 1;
5755 sctp_sack_check(stcb, was_a_gap);
5756 } else if (fwd_tsn_seen) {
5757 stcb->asoc.send_sack = 1;
5759 /* trigger send of any chunks in queue... */
5761 #ifdef SCTP_AUDITING_ENABLED
5762 sctp_audit_log(0xE0, 2);
5763 sctp_auditing(1, inp, stcb, net);
5765 SCTPDBG(SCTP_DEBUG_INPUT1,
5766 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5767 stcb->asoc.peers_rwnd,
5768 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5769 stcb->asoc.total_flight);
5770 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5771 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5772 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5774 if (cnt_ctrl_ready ||
5776 (stcb->asoc.peers_rwnd > 0 ||
5777 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5778 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5779 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5780 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5782 #ifdef SCTP_AUDITING_ENABLED
5783 sctp_audit_log(0xE0, 3);
5784 sctp_auditing(2, inp, stcb, net);
5786 SCTP_TCB_UNLOCK(stcb);
5789 sctp_validate_no_locks(inp);
5796 sctp_print_mbuf_chain(struct mbuf *m)
5798 for (; m; m = SCTP_BUF_NEXT(m)) {
5799 SCTP_PRINTF("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5800 if (SCTP_BUF_IS_EXTENDED(m))
5801 SCTP_PRINTF("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5809 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5813 uint32_t vrf_id = 0;
5817 struct sctp_inpcb *inp = NULL;
5818 struct sctp_nets *net;
5819 struct sctp_tcb *stcb = NULL;
5820 struct sctp_chunkhdr *ch;
5821 int refcount_up = 0;
5822 int length, mlen, offset;
5824 #if !defined(SCTP_WITH_NO_CSUM)
5825 uint32_t check, calc_check;
5829 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5830 SCTP_RELEASE_PKT(i_pak);
5833 mlen = SCTP_HEADER_LEN(i_pak);
5835 m = SCTP_HEADER_TO_CHAIN(i_pak);
5838 SCTP_STAT_INCR(sctps_recvpackets);
5839 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5842 #ifdef SCTP_MBUF_LOGGING
5843 /* Log in any input mbufs */
5844 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5847 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
5848 if (SCTP_BUF_IS_EXTENDED(mat)) {
5849 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5854 #ifdef SCTP_PACKET_LOGGING
5855 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5856 sctp_packet_log(m, mlen);
5859 * Must take out the iphlen, since mlen expects this (only effect lb
5865 * Get IP, SCTP, and first chunk header together in first mbuf.
5867 ip = mtod(m, struct ip *);
5868 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5869 if (SCTP_BUF_LEN(m) < offset) {
5870 if ((m = m_pullup(m, offset)) == 0) {
5871 SCTP_STAT_INCR(sctps_hdrops);
5874 ip = mtod(m, struct ip *);
5876 /* validate mbuf chain length with IP payload length */
5877 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5878 SCTP_STAT_INCR(sctps_hdrops);
5881 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5882 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5883 SCTPDBG(SCTP_DEBUG_INPUT1,
5884 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5886 /* SCTP does not allow broadcasts or multicasts */
5887 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5890 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5892 * We only look at broadcast if its a front state, All
5893 * others we will not have a tcb for anyway.
5897 /* validate SCTP checksum */
5898 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5899 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5901 if_name(m->m_pkthdr.rcvif),
5902 m->m_pkthdr.csum_flags);
5903 #if defined(SCTP_WITH_NO_CSUM)
5904 SCTP_STAT_INCR(sctps_recvnocrc);
5906 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5907 SCTP_STAT_INCR(sctps_recvhwcrc);
5908 goto sctp_skip_csum_4;
5910 check = sh->checksum; /* save incoming checksum */
5911 sh->checksum = 0; /* prepare for calc */
5912 calc_check = sctp_calculate_cksum(m, iphlen);
5913 sh->checksum = check;
5914 SCTP_STAT_INCR(sctps_recvswcrc);
5915 if (calc_check != check) {
5916 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5917 calc_check, check, m, mlen, iphlen);
5919 stcb = sctp_findassociation_addr(m,
5920 offset - sizeof(*ch),
5923 if ((net) && (port)) {
5924 if (net->port == 0) {
5925 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5929 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5930 net->flowid = m->m_pkthdr.flowid;
5935 if ((inp) && (stcb)) {
5936 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5937 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5938 } else if ((inp != NULL) && (stcb == NULL)) {
5941 SCTP_STAT_INCR(sctps_badsum);
5942 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5947 /* destination port of 0 is illegal, based on RFC2960. */
5948 if (sh->dest_port == 0) {
5949 SCTP_STAT_INCR(sctps_hdrops);
5953 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5954 * IP/SCTP/first chunk header...
5956 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
5957 sh, ch, &inp, &net, vrf_id);
5958 if ((net) && (port)) {
5959 if (net->port == 0) {
5960 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5964 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
5965 net->flowid = m->m_pkthdr.flowid;
5970 /* inp's ref-count increased && stcb locked */
5972 struct sctp_init_chunk *init_chk, chunk_buf;
5974 SCTP_STAT_INCR(sctps_noport);
5977 * we use the bandwidth limiting to protect against sending
5978 * too many ABORTS all at once. In this case these count the
5979 * same as an ICMP message.
5981 if (badport_bandlim(0) < 0)
5983 #endif /* ICMP_BANDLIM */
5984 SCTPDBG(SCTP_DEBUG_INPUT1,
5985 "Sending a ABORT from packet entry!\n");
5986 if (ch->chunk_type == SCTP_INITIATION) {
5988 * we do a trick here to get the INIT tag, dig in
5989 * and get the tag from the INIT and put it in the
5992 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5993 iphlen + sizeof(*sh), sizeof(*init_chk),
5994 (uint8_t *) & chunk_buf);
5995 if (init_chk != NULL)
5996 sh->v_tag = init_chk->init.initiate_tag;
5998 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5999 sctp_send_shutdown_complete2(m, sh, vrf_id, port);
6002 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
6005 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
6006 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
6007 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
6008 (ch->chunk_type != SCTP_INIT))) {
6009 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
6013 } else if (stcb == NULL) {
6018 * I very much doubt any of the IPSEC stuff will work but I have no
6019 * idea, so I will leave it in place.
6021 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
6022 MODULE_GLOBAL(ipsec4stat).in_polvio++;
6023 SCTP_STAT_INCR(sctps_hdrops);
6029 * common chunk processing
6031 length = ip->ip_len + iphlen;
6032 offset -= sizeof(struct sctp_chunkhdr);
6034 ecn_bits = ip->ip_tos;
6036 /* sa_ignore NO_NULL_CHK */
6037 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
6038 inp, stcb, net, ecn_bits, vrf_id, port);
6039 /* inp's ref-count reduced && stcb unlocked */
6043 if ((inp) && (refcount_up)) {
6044 /* reduce ref-count */
6045 SCTP_INP_DECR_REF(inp);
6050 SCTP_TCB_UNLOCK(stcb);
6052 if ((inp) && (refcount_up)) {
6053 /* reduce ref-count */
6054 SCTP_INP_DECR_REF(inp);
6062 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6063 extern int *sctp_cpuarry;
6068 sctp_input(struct mbuf *m, int off)
6070 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6075 uint32_t flowid, tag;
6078 if (m->m_flags & M_FLOWID) {
6079 flowid = m->m_pkthdr.flowid;
6082 * No flow id built by lower layers fix it so we
6085 ip = mtod(m, struct ip *);
6086 offset = off + sizeof(*sh);
6087 if (SCTP_BUF_LEN(m) < offset) {
6088 if ((m = m_pullup(m, offset)) == 0) {
6089 SCTP_STAT_INCR(sctps_hdrops);
6092 ip = mtod(m, struct ip *);
6094 sh = (struct sctphdr *)((caddr_t)ip + off);
6095 tag = htonl(sh->v_tag);
6096 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6097 m->m_pkthdr.flowid = flowid;
6098 m->m_flags |= M_FLOWID;
6100 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6101 sctp_queue_to_mcore(m, off, cpu_to_use);
6105 sctp_input_with_port(m, off, 0);