2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */
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>
55 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
57 struct sctp_nets *net;
60 * This now not only stops all cookie timers it also stops any INIT
61 * timers as well. This will make sure that the timers are stopped
62 * in all collision cases.
64 SCTP_TCB_LOCK_ASSERT(stcb);
65 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
66 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
67 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
70 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
71 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
72 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
75 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
82 sctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
83 struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
84 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port)
86 struct sctp_init *init;
90 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
94 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
100 /* First are we accepting? */
101 if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) {
102 SCTPDBG(SCTP_DEBUG_INPUT2,
103 "sctp_handle_init: Abort, so_qlimit:%d\n",
104 inp->sctp_socket->so_qlimit);
106 * FIX ME ?? What about TCP model and we have a
107 * match/restart case? Actually no fix is needed. the lookup
108 * will always find the existing assoc so stcb would not be
109 * NULL. It may be questionable to do this since we COULD
110 * just send back the INIT-ACK and hope that the app did
111 * accept()'s by the time the COOKIE was sent. But there is
112 * a price to pay for COOKIE generation and I don't want to
113 * pay it on the chance that the app will actually do some
114 * accepts(). The App just looses and should NOT be in this
117 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
120 *abort_no_unlock = 1;
123 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
125 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
126 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
129 *abort_no_unlock = 1;
132 /* validate parameters */
133 if (init->initiate_tag == 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 (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
143 /* invalid parameter... send abort */
144 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
145 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
148 *abort_no_unlock = 1;
151 if (init->num_inbound_streams == 0) {
152 /* protocol error... send abort */
153 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
154 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
157 *abort_no_unlock = 1;
160 if (init->num_outbound_streams == 0) {
161 /* protocol error... send abort */
162 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
163 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
166 *abort_no_unlock = 1;
169 init_limit = offset + ntohs(cp->ch.chunk_length);
170 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
172 /* auth parameter(s) error... send abort */
173 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
175 *abort_no_unlock = 1;
178 /* send an INIT-ACK w/cookie */
179 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
180 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
181 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
184 SCTP_INP_RUNLOCK(inp);
189 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
193 sctp_is_there_unsent_data(struct sctp_tcb *stcb)
196 struct sctp_stream_queue_pending *sp;
197 struct sctp_stream_out *strq;
198 struct sctp_association *asoc;
201 * This function returns the number of streams that have true unsent
202 * data on them. Note that as it looks through it will clean up any
203 * places that have old data that has been sent but left at top of
207 SCTP_TCB_SEND_LOCK(stcb);
208 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
209 /* Check to see if some data queued */
210 TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) {
212 /* sa_ignore FREED_MEMORY */
213 sp = TAILQ_FIRST(&strq->outqueue);
217 if ((sp->msg_is_complete) &&
219 (sp->sender_all_done)) {
221 * We are doing differed cleanup. Last time
222 * through when we took all the data the
223 * sender_all_done was not set.
225 if (sp->put_last_out == 0) {
226 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
227 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
233 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
234 TAILQ_REMOVE(&strq->outqueue, sp, next);
235 sctp_free_remote_addr(sp->net);
237 sctp_m_freem(sp->data);
240 sctp_free_a_strmoq(stcb, sp);
241 goto is_there_another;
248 SCTP_TCB_SEND_UNLOCK(stcb);
249 return (unsent_data);
253 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
254 struct sctp_nets *net)
256 struct sctp_init *init;
257 struct sctp_association *asoc;
258 struct sctp_nets *lnet;
263 /* save off parameters */
264 asoc->peer_vtag = ntohl(init->initiate_tag);
265 asoc->peers_rwnd = ntohl(init->a_rwnd);
266 if (TAILQ_FIRST(&asoc->nets)) {
267 /* update any ssthresh's that may have a default */
268 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
269 lnet->ssthresh = asoc->peers_rwnd;
271 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
272 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
276 SCTP_TCB_SEND_LOCK(stcb);
277 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
279 struct sctp_stream_out *outs;
280 struct sctp_stream_queue_pending *sp;
281 struct sctp_tmit_chunk *chk, *chk_next;
283 /* abandon the upper streams */
284 newcnt = ntohs(init->num_inbound_streams);
285 if (!TAILQ_EMPTY(&asoc->send_queue)) {
286 chk = TAILQ_FIRST(&asoc->send_queue);
288 chk_next = TAILQ_NEXT(chk, sctp_next);
289 if (chk->rec.data.stream_number >= newcnt) {
290 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
291 asoc->send_queue_cnt--;
292 if (chk->data != NULL) {
293 sctp_free_bufspace(stcb, asoc, chk, 1);
294 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
295 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_NOT_LOCKED);
297 sctp_m_freem(chk->data);
301 sctp_free_a_chunk(stcb, chk);
302 /* sa_ignore FREED_MEMORY */
308 for (i = newcnt; i < asoc->pre_open_streams; i++) {
309 outs = &asoc->strmout[i];
310 sp = TAILQ_FIRST(&outs->outqueue);
312 TAILQ_REMOVE(&outs->outqueue, sp, next);
313 asoc->stream_queue_cnt--;
314 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
315 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
316 sp, SCTP_SO_NOT_LOCKED);
318 sctp_m_freem(sp->data);
321 sctp_free_remote_addr(sp->net);
324 sctp_free_a_strmoq(stcb, sp);
325 /* sa_ignore FREED_MEMORY */
326 sp = TAILQ_FIRST(&outs->outqueue);
330 /* cut back the count */
331 asoc->pre_open_streams = newcnt;
333 SCTP_TCB_SEND_UNLOCK(stcb);
334 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
336 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
337 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
338 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
339 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
340 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
342 /* This is the next one we expect */
343 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
345 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
346 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
347 asoc->last_echo_tsn = asoc->asconf_seq_in;
348 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
349 /* open the requested streams */
351 if (asoc->strmin != NULL) {
352 /* Free the old ones */
353 struct sctp_queued_to_read *ctl;
355 for (i = 0; i < asoc->streamincnt; i++) {
356 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
358 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
359 sctp_free_remote_addr(ctl->whoFrom);
361 sctp_m_freem(ctl->data);
363 sctp_free_a_readq(stcb, ctl);
364 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
367 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
369 asoc->streamincnt = ntohs(init->num_outbound_streams);
370 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
371 asoc->streamincnt = MAX_SCTP_STREAMS;
373 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
374 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
375 if (asoc->strmin == NULL) {
376 /* we didn't get memory for the streams! */
377 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
380 for (i = 0; i < asoc->streamincnt; i++) {
381 asoc->strmin[i].stream_no = i;
382 asoc->strmin[i].last_sequence_delivered = 0xffff;
384 * U-stream ranges will be set when the cookie is unpacked.
385 * Or for the INIT sender they are un set (if pr-sctp not
386 * supported) when the INIT-ACK arrives.
388 TAILQ_INIT(&asoc->strmin[i].inqueue);
389 asoc->strmin[i].delivery_started = 0;
392 * load_address_from_init will put the addresses into the
393 * association when the COOKIE is processed or the INIT-ACK is
394 * processed. Both types of COOKIE's existing and new call this
395 * routine. It will remove addresses that are no longer in the
396 * association (for the restarting case where addresses are
397 * removed). Up front when the INIT arrives we will discard it if it
398 * is a restart and new addresses have been added.
400 /* sa_ignore MEMLEAK */
405 * INIT-ACK message processing/consumption returns value < 0 on error
408 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
409 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
410 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
412 struct sctp_association *asoc;
414 int retval, abort_flag;
415 uint32_t initack_limit;
416 int nat_friendly = 0;
418 /* First verify that we have no illegal param's */
422 op_err = sctp_arethere_unrecognized_parameters(m,
423 (offset + sizeof(struct sctp_init_chunk)),
424 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
426 /* Send an abort and notify peer */
427 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED);
428 *abort_no_unlock = 1;
432 asoc->peer_supports_nat = (uint8_t) nat_friendly;
433 /* process the peer's parameters in the INIT-ACK */
434 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
438 initack_limit = offset + ntohs(cp->ch.chunk_length);
439 /* load all addresses */
440 if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen,
441 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
443 /* Huh, we should abort */
444 SCTPDBG(SCTP_DEBUG_INPUT1,
445 "Load addresses from INIT causes an abort %d\n",
447 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
449 *abort_no_unlock = 1;
452 /* if the peer doesn't support asconf, flush the asconf queue */
453 if (asoc->peer_supports_asconf == 0) {
454 struct sctp_asconf_addr *aparam;
456 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
457 /* sa_ignore FREED_MEMORY */
458 aparam = TAILQ_FIRST(&asoc->asconf_queue);
459 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
460 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
463 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
464 stcb->asoc.local_hmacs);
466 sctp_queue_op_err(stcb, op_err);
467 /* queuing will steal away the mbuf chain to the out queue */
470 /* extract the cookie and queue it to "echo" it back... */
471 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
472 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
473 stcb->asoc.overall_error_count,
475 SCTP_FROM_SCTP_INPUT,
478 stcb->asoc.overall_error_count = 0;
479 net->error_count = 0;
482 * Cancel the INIT timer, We do this first before queueing the
483 * cookie. We always cancel at the primary to assue that we are
484 * canceling the timer started by the INIT which always goes to the
487 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
488 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
490 /* calculate the RTO */
491 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy);
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, 0, 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 sockaddr_in *sin;
537 struct sockaddr_in6 *sin6;
538 struct sctp_nets *r_net;
542 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
546 sin = (struct sockaddr_in *)&store;
547 sin6 = (struct sockaddr_in6 *)&store;
549 memset(&store, 0, sizeof(store));
550 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
551 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
552 sin->sin_family = cp->heartbeat.hb_info.addr_family;
553 sin->sin_len = cp->heartbeat.hb_info.addr_len;
554 sin->sin_port = stcb->rport;
555 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
556 sizeof(sin->sin_addr));
557 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
558 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
559 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
560 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
561 sin6->sin6_port = stcb->rport;
562 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
563 sizeof(sin6->sin6_addr));
567 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
569 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
572 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
573 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
574 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
576 * If the its a HB and it's random value is correct when can
577 * confirm the destination.
579 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
580 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
581 stcb->asoc.primary_destination = r_net;
582 r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
583 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
584 r_net = TAILQ_FIRST(&stcb->asoc.nets);
585 if (r_net != stcb->asoc.primary_destination) {
587 * first one on the list is NOT the primary
588 * sctp_cmpaddr() is much more efficent if
589 * the primary is the first on the list,
592 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
593 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
597 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
598 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
600 r_net->error_count = 0;
601 r_net->hb_responded = 1;
602 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
603 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
604 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
605 r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
606 r_net->dest_state |= SCTP_ADDR_REACHABLE;
607 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
608 SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED);
609 /* now was it the primary? if so restore */
610 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
611 (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
615 * JRS 5/14/07 - If CMT PF is on and the destination is in PF state,
616 * set the destination to active state and set the cwnd to one or
617 * two MTU's based on whether PF1 or PF2 is being used. If a T3
618 * timer is running, for the destination, stop the timer because a
619 * PF-heartbeat was received.
621 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
622 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
623 (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) {
624 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
625 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
627 SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
629 net->dest_state &= ~SCTP_ADDR_PF;
630 net->cwnd = net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf);
631 SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n",
634 /* Now lets do a RTO with this */
635 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy);
636 /* Mobility adaptation */
638 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
639 SCTP_MOBILITY_BASE) ||
640 sctp_is_mobility_feature_on(stcb->sctp_ep,
641 SCTP_MOBILITY_FASTHANDOFF)) &&
642 sctp_is_mobility_feature_on(stcb->sctp_ep,
643 SCTP_MOBILITY_PRIM_DELETED)) {
645 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
646 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
647 SCTP_MOBILITY_FASTHANDOFF)) {
648 sctp_assoc_immediate_retrans(stcb,
649 stcb->asoc.primary_destination);
651 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
652 SCTP_MOBILITY_BASE)) {
653 sctp_move_chunks_from_deleted_prim(stcb,
654 stcb->asoc.primary_destination);
656 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
657 stcb->asoc.deleted_primary);
663 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
666 * return 0 means we want you to proceed with the abort non-zero
667 * means no abort processing
669 struct sctpasochead *head;
671 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
672 /* generate a new vtag and send init */
673 LIST_REMOVE(stcb, sctp_asocs);
674 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
675 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
677 * put it in the bucket in the vtag hash of assoc's for the
680 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
681 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
684 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
686 * treat like a case where the cookie expired i.e.: - dump
687 * current cookie. - generate a new vtag. - resend init.
689 /* generate a new vtag and send init */
690 LIST_REMOVE(stcb, sctp_asocs);
691 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
692 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
693 sctp_stop_all_cookie_timers(stcb);
694 sctp_toss_old_cookies(stcb, &stcb->asoc);
695 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
696 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
698 * put it in the bucket in the vtag hash of assoc's for the
701 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
702 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
709 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
710 struct sctp_nets *net)
713 * return 0 means we want you to proceed with the abort non-zero
714 * means no abort processing
716 if (stcb->asoc.peer_supports_auth == 0) {
717 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
720 sctp_asconf_send_nat_state_update(stcb, net);
726 sctp_handle_abort(struct sctp_abort_chunk *cp,
727 struct sctp_tcb *stcb, struct sctp_nets *net)
729 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
735 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
739 len = ntohs(cp->ch.chunk_length);
740 if (len > sizeof(struct sctp_chunkhdr)) {
742 * Need to check the cause codes for our two magic nat
743 * aborts which don't kill the assoc necessarily.
745 struct sctp_abort_chunk *cpnext;
746 struct sctp_missing_nat_state *natc;
751 natc = (struct sctp_missing_nat_state *)cpnext;
752 cause = ntohs(natc->cause);
753 if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) {
754 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
756 if (sctp_handle_nat_colliding_state(stcb)) {
759 } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) {
760 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
762 if (sctp_handle_nat_missing_state(stcb, net)) {
767 /* stop any receive timers */
768 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
769 /* notify user of the abort and clean up... */
770 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
772 #if defined(SCTP_PANIC_ON_ABORT)
773 printf("stcb:%p state:%d rport:%d net:%p\n",
774 stcb, stcb->asoc.state, stcb->rport, net);
775 if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
776 panic("Received an ABORT");
778 printf("No panic its in state %x closed\n", stcb->asoc.state);
781 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
782 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
783 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
784 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
786 #ifdef SCTP_ASOCLOG_OF_TSNS
787 sctp_print_out_track_log(stcb);
789 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
790 so = SCTP_INP_SO(stcb->sctp_ep);
791 atomic_add_int(&stcb->asoc.refcnt, 1);
792 SCTP_TCB_UNLOCK(stcb);
793 SCTP_SOCKET_LOCK(so, 1);
795 atomic_subtract_int(&stcb->asoc.refcnt, 1);
797 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
798 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
799 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
800 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
801 SCTP_SOCKET_UNLOCK(so, 1);
803 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
807 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
808 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
810 struct sctp_association *asoc;
811 int some_on_streamwheel;
813 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
818 SCTPDBG(SCTP_DEBUG_INPUT2,
819 "sctp_handle_shutdown: handling SHUTDOWN\n");
823 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
824 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
827 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
828 /* Shutdown NOT the expected size */
831 sctp_update_acked(stcb, cp, net, abort_flag);
836 if (asoc->control_pdapi) {
838 * With a normal shutdown we assume the end of last record.
840 SCTP_INP_READ_LOCK(stcb->sctp_ep);
841 asoc->control_pdapi->end_added = 1;
842 asoc->control_pdapi->pdapi_aborted = 1;
843 asoc->control_pdapi = NULL;
844 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
845 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
846 so = SCTP_INP_SO(stcb->sctp_ep);
847 atomic_add_int(&stcb->asoc.refcnt, 1);
848 SCTP_TCB_UNLOCK(stcb);
849 SCTP_SOCKET_LOCK(so, 1);
851 atomic_subtract_int(&stcb->asoc.refcnt, 1);
852 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
853 /* assoc was freed while we were unlocked */
854 SCTP_SOCKET_UNLOCK(so, 1);
858 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
859 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
860 SCTP_SOCKET_UNLOCK(so, 1);
863 /* goto SHUTDOWN_RECEIVED state to block new requests */
864 if (stcb->sctp_socket) {
865 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
866 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
867 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
868 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
869 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
871 * notify upper layer that peer has initiated a
874 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
877 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
880 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
882 * stop the shutdown timer, since we WILL move to
885 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
887 /* Now is there unsent data on a stream somewhere? */
888 some_on_streamwheel = sctp_is_there_unsent_data(stcb);
890 if (!TAILQ_EMPTY(&asoc->send_queue) ||
891 !TAILQ_EMPTY(&asoc->sent_queue) ||
892 some_on_streamwheel) {
893 /* By returning we will push more data out */
896 /* no outstanding data to send, so move on... */
897 /* send SHUTDOWN-ACK */
898 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
899 /* move to SHUTDOWN-ACK-SENT state */
900 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
901 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
902 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
904 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
905 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
906 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
907 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
908 /* start SHUTDOWN timer */
909 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
915 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
916 struct sctp_tcb *stcb,
917 struct sctp_nets *net)
919 struct sctp_association *asoc;
921 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
924 so = SCTP_INP_SO(stcb->sctp_ep);
926 SCTPDBG(SCTP_DEBUG_INPUT2,
927 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
932 /* process according to association state */
933 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
934 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
935 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
936 sctp_send_shutdown_complete(stcb, net, 1);
937 SCTP_TCB_UNLOCK(stcb);
940 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
941 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
942 /* unexpected SHUTDOWN-ACK... so ignore... */
943 SCTP_TCB_UNLOCK(stcb);
946 if (asoc->control_pdapi) {
948 * With a normal shutdown we assume the end of last record.
950 SCTP_INP_READ_LOCK(stcb->sctp_ep);
951 asoc->control_pdapi->end_added = 1;
952 asoc->control_pdapi->pdapi_aborted = 1;
953 asoc->control_pdapi = NULL;
954 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
955 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
956 atomic_add_int(&stcb->asoc.refcnt, 1);
957 SCTP_TCB_UNLOCK(stcb);
958 SCTP_SOCKET_LOCK(so, 1);
960 atomic_subtract_int(&stcb->asoc.refcnt, 1);
961 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
962 /* assoc was freed while we were unlocked */
963 SCTP_SOCKET_UNLOCK(so, 1);
967 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
968 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
969 SCTP_SOCKET_UNLOCK(so, 1);
972 /* are the queues empty? */
973 if (!TAILQ_EMPTY(&asoc->send_queue) ||
974 !TAILQ_EMPTY(&asoc->sent_queue) ||
975 !TAILQ_EMPTY(&asoc->out_wheel)) {
976 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
979 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
980 /* send SHUTDOWN-COMPLETE */
981 sctp_send_shutdown_complete(stcb, net, 0);
982 /* notify upper layer protocol */
983 if (stcb->sctp_socket) {
984 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
985 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
986 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
987 /* Set the connected flag to disconnected */
988 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
991 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
992 /* free the TCB but first save off the ep */
993 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
994 atomic_add_int(&stcb->asoc.refcnt, 1);
995 SCTP_TCB_UNLOCK(stcb);
996 SCTP_SOCKET_LOCK(so, 1);
998 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1000 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1001 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1002 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1003 SCTP_SOCKET_UNLOCK(so, 1);
1008 * Skip past the param header and then we will find the chunk that caused the
1009 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1010 * our peer must be broken.
1013 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1014 struct sctp_nets *net)
1016 struct sctp_chunkhdr *chk;
1018 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1019 switch (chk->chunk_type) {
1020 case SCTP_ASCONF_ACK:
1022 sctp_asconf_cleanup(stcb, net);
1024 case SCTP_FORWARD_CUM_TSN:
1025 stcb->asoc.peer_supports_prsctp = 0;
1028 SCTPDBG(SCTP_DEBUG_INPUT2,
1029 "Peer does not support chunk type %d(%x)??\n",
1030 chk->chunk_type, (uint32_t) chk->chunk_type);
1036 * Skip past the param header and then we will find the param that caused the
1037 * problem. There are a number of param's in a ASCONF OR the prsctp param
1038 * these will turn of specific features.
1041 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1043 struct sctp_paramhdr *pbad;
1046 switch (ntohs(pbad->param_type)) {
1048 case SCTP_PRSCTP_SUPPORTED:
1049 stcb->asoc.peer_supports_prsctp = 0;
1051 case SCTP_SUPPORTED_CHUNK_EXT:
1053 /* draft-ietf-tsvwg-addip-sctp */
1054 case SCTP_HAS_NAT_SUPPORT:
1055 stcb->asoc.peer_supports_nat = 0;
1057 case SCTP_ECN_NONCE_SUPPORTED:
1058 stcb->asoc.peer_supports_ecn_nonce = 0;
1059 stcb->asoc.ecn_nonce_allowed = 0;
1060 stcb->asoc.ecn_allowed = 0;
1062 case SCTP_ADD_IP_ADDRESS:
1063 case SCTP_DEL_IP_ADDRESS:
1064 case SCTP_SET_PRIM_ADDR:
1065 stcb->asoc.peer_supports_asconf = 0;
1067 case SCTP_SUCCESS_REPORT:
1068 case SCTP_ERROR_CAUSE_IND:
1069 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1070 SCTPDBG(SCTP_DEBUG_INPUT2,
1071 "Turning off ASCONF to this strange peer\n");
1072 stcb->asoc.peer_supports_asconf = 0;
1075 SCTPDBG(SCTP_DEBUG_INPUT2,
1076 "Peer does not support param type %d(%x)??\n",
1077 pbad->param_type, (uint32_t) pbad->param_type);
1083 sctp_handle_error(struct sctp_chunkhdr *ch,
1084 struct sctp_tcb *stcb, struct sctp_nets *net)
1087 struct sctp_paramhdr *phdr;
1088 uint16_t error_type;
1090 struct sctp_association *asoc;
1093 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1098 /* parse through all of the errors and process */
1100 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1101 sizeof(struct sctp_chunkhdr));
1102 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1103 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1104 /* Process an Error Cause */
1105 error_type = ntohs(phdr->param_type);
1106 error_len = ntohs(phdr->param_length);
1107 if ((error_len > chklen) || (error_len == 0)) {
1108 /* invalid param length for this param */
1109 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1113 switch (error_type) {
1114 case SCTP_CAUSE_INVALID_STREAM:
1115 case SCTP_CAUSE_MISSING_PARAM:
1116 case SCTP_CAUSE_INVALID_PARAM:
1117 case SCTP_CAUSE_NO_USER_DATA:
1118 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1121 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1122 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1124 if (sctp_handle_nat_colliding_state(stcb)) {
1128 case SCTP_CAUSE_NAT_MISSING_STATE:
1129 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1131 if (sctp_handle_nat_missing_state(stcb, net)) {
1135 case SCTP_CAUSE_STALE_COOKIE:
1137 * We only act if we have echoed a cookie and are
1140 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1143 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1144 /* Save the time doubled */
1145 asoc->cookie_preserve_req = ntohl(*p) << 1;
1146 asoc->stale_cookie_count++;
1147 if (asoc->stale_cookie_count >
1148 asoc->max_init_times) {
1149 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
1150 /* now free the asoc */
1151 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1152 so = SCTP_INP_SO(stcb->sctp_ep);
1153 atomic_add_int(&stcb->asoc.refcnt, 1);
1154 SCTP_TCB_UNLOCK(stcb);
1155 SCTP_SOCKET_LOCK(so, 1);
1156 SCTP_TCB_LOCK(stcb);
1157 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1159 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1160 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1161 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1162 SCTP_SOCKET_UNLOCK(so, 1);
1166 /* blast back to INIT state */
1167 sctp_toss_old_cookies(stcb, &stcb->asoc);
1168 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1169 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1170 sctp_stop_all_cookie_timers(stcb);
1171 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1174 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1176 * Nothing we can do here, we don't do hostname
1177 * addresses so if the peer does not like my IPv6
1178 * (or IPv4 for that matter) it does not matter. If
1179 * they don't support that type of address, they can
1180 * NOT possibly get that packet type... i.e. with no
1181 * IPv6 you can't recieve a IPv6 packet. so we can
1182 * safely ignore this one. If we ever added support
1183 * for HOSTNAME Addresses, then we would need to do
1187 case SCTP_CAUSE_UNRECOG_CHUNK:
1188 sctp_process_unrecog_chunk(stcb, phdr, net);
1190 case SCTP_CAUSE_UNRECOG_PARAM:
1191 sctp_process_unrecog_param(stcb, phdr);
1193 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1195 * We ignore this since the timer will drive out a
1196 * new cookie anyway and there timer will drive us
1197 * to send a SHUTDOWN_COMPLETE. We can't send one
1198 * here since we don't have their tag.
1201 case SCTP_CAUSE_DELETING_LAST_ADDR:
1202 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1203 case SCTP_CAUSE_DELETING_SRC_ADDR:
1205 * We should NOT get these here, but in a
1208 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1211 case SCTP_CAUSE_OUT_OF_RESC:
1213 * And what, pray tell do we do with the fact that
1214 * the peer is out of resources? Not really sure we
1215 * could do anything but abort. I suspect this
1216 * should have came WITH an abort instead of in a
1221 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1225 adjust = SCTP_SIZE32(error_len);
1227 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1233 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1234 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1235 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1237 struct sctp_init_ack *init_ack;
1238 struct mbuf *op_err;
1240 SCTPDBG(SCTP_DEBUG_INPUT2,
1241 "sctp_handle_init_ack: handling INIT-ACK\n");
1244 SCTPDBG(SCTP_DEBUG_INPUT2,
1245 "sctp_handle_init_ack: TCB is null\n");
1248 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1249 /* Invalid length */
1250 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1251 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1252 op_err, 0, net->port);
1253 *abort_no_unlock = 1;
1256 init_ack = &cp->init;
1257 /* validate parameters */
1258 if (init_ack->initiate_tag == 0) {
1259 /* protocol error... send an abort */
1260 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1261 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1262 op_err, 0, net->port);
1263 *abort_no_unlock = 1;
1266 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1267 /* protocol error... send an abort */
1268 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1269 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1270 op_err, 0, net->port);
1271 *abort_no_unlock = 1;
1274 if (init_ack->num_inbound_streams == 0) {
1275 /* protocol error... send an abort */
1276 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1277 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1278 op_err, 0, net->port);
1279 *abort_no_unlock = 1;
1282 if (init_ack->num_outbound_streams == 0) {
1283 /* protocol error... send an abort */
1284 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1285 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1286 op_err, 0, net->port);
1287 *abort_no_unlock = 1;
1290 /* process according to association state... */
1291 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1292 case SCTP_STATE_COOKIE_WAIT:
1293 /* this is the expected state for this chunk */
1294 /* process the INIT-ACK parameters */
1295 if (stcb->asoc.primary_destination->dest_state &
1296 SCTP_ADDR_UNCONFIRMED) {
1298 * The primary is where we sent the INIT, we can
1299 * always consider it confirmed when the INIT-ACK is
1300 * returned. Do this before we load addresses
1303 stcb->asoc.primary_destination->dest_state &=
1304 ~SCTP_ADDR_UNCONFIRMED;
1305 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1306 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1308 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1309 net, abort_no_unlock, vrf_id) < 0) {
1310 /* error in parsing parameters */
1313 /* update our state */
1314 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1315 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1317 /* reset the RTO calc */
1318 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1319 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1320 stcb->asoc.overall_error_count,
1322 SCTP_FROM_SCTP_INPUT,
1325 stcb->asoc.overall_error_count = 0;
1326 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1328 * collapse the init timer back in case of a exponential
1331 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1334 * the send at the end of the inbound data processing will
1335 * cause the cookie to be sent
1338 case SCTP_STATE_SHUTDOWN_SENT:
1339 /* incorrect state... discard */
1341 case SCTP_STATE_COOKIE_ECHOED:
1342 /* incorrect state... discard */
1344 case SCTP_STATE_OPEN:
1345 /* incorrect state... discard */
1347 case SCTP_STATE_EMPTY:
1348 case SCTP_STATE_INUSE:
1350 /* incorrect state... discard */
1354 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1358 static struct sctp_tcb *
1359 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1360 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1361 struct sctp_inpcb *inp, struct sctp_nets **netp,
1362 struct sockaddr *init_src, int *notification,
1363 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1364 uint32_t vrf_id, uint16_t port);
1368 * handle a state cookie for an existing association m: input packet mbuf
1369 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1370 * "split" mbuf and the cookie signature does not exist offset: offset into
1371 * mbuf to the cookie-echo chunk
1373 static struct sctp_tcb *
1374 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1375 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1376 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1377 struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id,
1378 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port)
1380 struct sctp_association *asoc;
1381 struct sctp_init_chunk *init_cp, init_buf;
1382 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1383 struct sctp_nets *net;
1384 struct mbuf *op_err;
1385 struct sctp_paramhdr *ph;
1387 int init_offset, initack_offset, i;
1393 /* I know that the TCB is non-NULL from the caller */
1395 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1396 if (asoc->cookie_how[how_indx] == 0)
1399 if (how_indx < sizeof(asoc->cookie_how)) {
1400 asoc->cookie_how[how_indx] = 1;
1402 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1403 /* SHUTDOWN came in after sending INIT-ACK */
1404 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1405 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1406 0, M_DONTWAIT, 1, MT_DATA);
1407 if (op_err == NULL) {
1412 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1413 ph = mtod(op_err, struct sctp_paramhdr *);
1414 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1415 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1416 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1418 if (how_indx < sizeof(asoc->cookie_how))
1419 asoc->cookie_how[how_indx] = 2;
1423 * find and validate the INIT chunk in the cookie (peer's info) the
1424 * INIT should start after the cookie-echo header struct (chunk
1425 * header, state cookie header struct)
1427 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1429 init_cp = (struct sctp_init_chunk *)
1430 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1431 (uint8_t *) & init_buf);
1432 if (init_cp == NULL) {
1433 /* could not pull a INIT chunk in cookie */
1436 chk_length = ntohs(init_cp->ch.chunk_length);
1437 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1441 * find and validate the INIT-ACK chunk in the cookie (my info) the
1442 * INIT-ACK follows the INIT chunk
1444 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1445 initack_cp = (struct sctp_init_ack_chunk *)
1446 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1447 (uint8_t *) & initack_buf);
1448 if (initack_cp == NULL) {
1449 /* could not pull INIT-ACK chunk in cookie */
1452 chk_length = ntohs(initack_cp->ch.chunk_length);
1453 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1456 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1457 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1459 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1460 * to get into the OPEN state
1462 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1464 * Opps, this means that we somehow generated two vtag's
1465 * the same. I.e. we did:
1467 * <---INIT(tag=a)------
1468 * ----INIT-ACK(tag=t)-->
1469 * ----INIT(tag=t)------> *1
1470 * <---INIT-ACK(tag=a)---
1471 * <----CE(tag=t)------------- *2
1473 * At point *1 we should be generating a different
1474 * tag t'. Which means we would throw away the CE and send
1475 * ours instead. Basically this is case C (throw away side).
1477 if (how_indx < sizeof(asoc->cookie_how))
1478 asoc->cookie_how[how_indx] = 17;
1482 switch SCTP_GET_STATE
1484 case SCTP_STATE_COOKIE_WAIT:
1485 case SCTP_STATE_COOKIE_ECHOED:
1487 * INIT was sent but got a COOKIE_ECHO with the
1488 * correct tags... just accept it...but we must
1489 * process the init so that we can make sure we have
1490 * the right seq no's.
1492 /* First we must process the INIT !! */
1493 retval = sctp_process_init(init_cp, stcb, net);
1495 if (how_indx < sizeof(asoc->cookie_how))
1496 asoc->cookie_how[how_indx] = 3;
1499 /* we have already processed the INIT so no problem */
1500 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1501 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1502 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1503 /* update current state */
1504 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1505 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1507 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1509 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1510 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1511 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1512 stcb->sctp_ep, stcb, asoc->primary_destination);
1514 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1515 sctp_stop_all_cookie_timers(stcb);
1516 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1517 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1518 (inp->sctp_socket->so_qlimit == 0)
1520 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1525 * Here is where collision would go if we
1526 * did a connect() and instead got a
1527 * init/init-ack/cookie done before the
1528 * init-ack came back..
1530 stcb->sctp_ep->sctp_flags |=
1531 SCTP_PCB_FLAGS_CONNECTED;
1532 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1533 so = SCTP_INP_SO(stcb->sctp_ep);
1534 atomic_add_int(&stcb->asoc.refcnt, 1);
1535 SCTP_TCB_UNLOCK(stcb);
1536 SCTP_SOCKET_LOCK(so, 1);
1537 SCTP_TCB_LOCK(stcb);
1538 atomic_add_int(&stcb->asoc.refcnt, -1);
1539 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1540 SCTP_SOCKET_UNLOCK(so, 1);
1544 soisconnected(stcb->sctp_socket);
1545 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1546 SCTP_SOCKET_UNLOCK(so, 1);
1549 /* notify upper layer */
1550 *notification = SCTP_NOTIFY_ASSOC_UP;
1552 * since we did not send a HB make sure we don't
1555 net->hb_responded = 1;
1556 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1557 &cookie->time_entered, sctp_align_unsafe_makecopy);
1559 if (stcb->asoc.sctp_autoclose_ticks &&
1560 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1561 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1567 * we're in the OPEN state (or beyond), so peer must
1568 * have simply lost the COOKIE-ACK
1572 sctp_stop_all_cookie_timers(stcb);
1574 * We ignore the return code here.. not sure if we should
1575 * somehow abort.. but we do have an existing asoc. This
1576 * really should not fail.
1578 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1579 init_offset + sizeof(struct sctp_init_chunk),
1580 initack_offset, sh, init_src)) {
1581 if (how_indx < sizeof(asoc->cookie_how))
1582 asoc->cookie_how[how_indx] = 4;
1585 /* respond with a COOKIE-ACK */
1586 sctp_toss_old_cookies(stcb, asoc);
1587 sctp_send_cookie_ack(stcb);
1588 if (how_indx < sizeof(asoc->cookie_how))
1589 asoc->cookie_how[how_indx] = 5;
1592 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1593 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1594 cookie->tie_tag_my_vtag == 0 &&
1595 cookie->tie_tag_peer_vtag == 0) {
1597 * case C in Section 5.2.4 Table 2: XMOO silently discard
1599 if (how_indx < sizeof(asoc->cookie_how))
1600 asoc->cookie_how[how_indx] = 6;
1604 * If nat support, and the below and stcb is established, send back
1605 * a ABORT(colliding state) if we are established.
1607 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1608 (asoc->peer_supports_nat) &&
1609 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1610 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1611 (asoc->peer_vtag == 0)))) {
1613 * Special case - Peer's support nat. We may have two init's
1614 * that we gave out the same tag on since one was not
1615 * established.. i.e. we get INIT from host-1 behind the nat
1616 * and we respond tag-a, we get a INIT from host-2 behind
1617 * the nat and we get tag-a again. Then we bring up host-1
1618 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1619 * Now we have colliding state. We must send an abort here
1620 * with colliding state indication.
1622 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1623 0, M_DONTWAIT, 1, MT_DATA);
1624 if (op_err == NULL) {
1628 /* pre-reserve some space */
1630 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1632 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1634 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1635 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1637 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1638 ph = mtod(op_err, struct sctp_paramhdr *);
1639 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1640 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1641 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
1644 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1645 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1646 (asoc->peer_vtag == 0))) {
1648 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1649 * should be ok, re-accept peer info
1651 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1653 * Extension of case C. If we hit this, then the
1654 * random number generator returned the same vtag
1655 * when we first sent our INIT-ACK and when we later
1656 * sent our INIT. The side with the seq numbers that
1657 * are different will be the one that normnally
1658 * would have hit case C. This in effect "extends"
1659 * our vtags in this collision case to be 64 bits.
1660 * The same collision could occur aka you get both
1661 * vtag and seq number the same twice in a row.. but
1662 * is much less likely. If it did happen then we
1663 * would proceed through and bring up the assoc.. we
1664 * may end up with the wrong stream setup however..
1665 * which would be bad.. but there is no way to
1666 * tell.. until we send on a stream that does not
1669 if (how_indx < sizeof(asoc->cookie_how))
1670 asoc->cookie_how[how_indx] = 7;
1674 if (how_indx < sizeof(asoc->cookie_how))
1675 asoc->cookie_how[how_indx] = 8;
1676 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1677 sctp_stop_all_cookie_timers(stcb);
1679 * since we did not send a HB make sure we don't double
1682 net->hb_responded = 1;
1683 if (stcb->asoc.sctp_autoclose_ticks &&
1684 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1685 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1688 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1689 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1691 /* Note last_cwr_tsn? where is this used? */
1692 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1693 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1695 * Ok the peer probably discarded our data (if we
1696 * echoed a cookie+data). So anything on the
1697 * sent_queue should be marked for retransmit, we
1698 * may not get something to kick us so it COULD
1699 * still take a timeout to move these.. but it can't
1700 * hurt to mark them.
1702 struct sctp_tmit_chunk *chk;
1704 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1705 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1706 chk->sent = SCTP_DATAGRAM_RESEND;
1707 sctp_flight_size_decrease(chk);
1708 sctp_total_flight_decrease(stcb, chk);
1709 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1715 /* process the INIT info (peer's info) */
1716 retval = sctp_process_init(init_cp, stcb, net);
1718 if (how_indx < sizeof(asoc->cookie_how))
1719 asoc->cookie_how[how_indx] = 9;
1722 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1723 init_offset + sizeof(struct sctp_init_chunk),
1724 initack_offset, sh, init_src)) {
1725 if (how_indx < sizeof(asoc->cookie_how))
1726 asoc->cookie_how[how_indx] = 10;
1729 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1730 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1731 *notification = SCTP_NOTIFY_ASSOC_UP;
1733 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1734 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1735 (inp->sctp_socket->so_qlimit == 0)) {
1736 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1740 stcb->sctp_ep->sctp_flags |=
1741 SCTP_PCB_FLAGS_CONNECTED;
1742 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1743 so = SCTP_INP_SO(stcb->sctp_ep);
1744 atomic_add_int(&stcb->asoc.refcnt, 1);
1745 SCTP_TCB_UNLOCK(stcb);
1746 SCTP_SOCKET_LOCK(so, 1);
1747 SCTP_TCB_LOCK(stcb);
1748 atomic_add_int(&stcb->asoc.refcnt, -1);
1749 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1750 SCTP_SOCKET_UNLOCK(so, 1);
1754 soisconnected(stcb->sctp_socket);
1755 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1756 SCTP_SOCKET_UNLOCK(so, 1);
1759 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1760 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1762 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1763 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1764 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1765 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1767 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1769 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1770 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1771 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1772 stcb->sctp_ep, stcb, asoc->primary_destination);
1774 sctp_stop_all_cookie_timers(stcb);
1775 sctp_toss_old_cookies(stcb, asoc);
1776 sctp_send_cookie_ack(stcb);
1779 * only if we have retrans set do we do this. What
1780 * this call does is get only the COOKIE-ACK out and
1781 * then when we return the normal call to
1782 * sctp_chunk_output will get the retrans out behind
1785 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1787 if (how_indx < sizeof(asoc->cookie_how))
1788 asoc->cookie_how[how_indx] = 11;
1792 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1793 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1794 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1795 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1796 cookie->tie_tag_peer_vtag != 0) {
1797 struct sctpasochead *head;
1799 if (asoc->peer_supports_nat) {
1801 * This is a gross gross hack. just call the
1802 * cookie_new code since we are allowing a duplicate
1803 * association. I hope this works...
1805 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1806 inp, netp, init_src, notification,
1807 auth_skipped, auth_offset, auth_len,
1811 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1814 if (how_indx < sizeof(asoc->cookie_how))
1815 asoc->cookie_how[how_indx] = 12;
1816 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1817 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1819 *sac_assoc_id = sctp_get_associd(stcb);
1820 /* notify upper layer */
1821 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1822 atomic_add_int(&stcb->asoc.refcnt, 1);
1823 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1824 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1825 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1826 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1828 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1829 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1830 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1831 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1833 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1834 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1835 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1836 stcb->sctp_ep, stcb, asoc->primary_destination);
1838 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1839 /* move to OPEN state, if not in SHUTDOWN_SENT */
1840 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1842 asoc->pre_open_streams =
1843 ntohs(initack_cp->init.num_outbound_streams);
1844 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1845 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1846 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1848 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1849 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1851 asoc->str_reset_seq_in = asoc->init_seq_number;
1853 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1854 if (asoc->mapping_array) {
1855 memset(asoc->mapping_array, 0,
1856 asoc->mapping_array_size);
1858 if (asoc->nr_mapping_array) {
1859 memset(asoc->nr_mapping_array, 0,
1860 asoc->mapping_array_size);
1862 SCTP_TCB_UNLOCK(stcb);
1863 SCTP_INP_INFO_WLOCK();
1864 SCTP_INP_WLOCK(stcb->sctp_ep);
1865 SCTP_TCB_LOCK(stcb);
1866 atomic_add_int(&stcb->asoc.refcnt, -1);
1867 /* send up all the data */
1868 SCTP_TCB_SEND_LOCK(stcb);
1870 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED);
1871 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1872 stcb->asoc.strmout[i].stream_no = i;
1873 stcb->asoc.strmout[i].next_sequence_sent = 0;
1874 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1876 /* process the INIT-ACK info (my info) */
1877 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1878 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1880 /* pull from vtag hash */
1881 LIST_REMOVE(stcb, sctp_asocs);
1882 /* re-insert to new vtag position */
1883 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1884 SCTP_BASE_INFO(hashasocmark))];
1886 * put it in the bucket in the vtag hash of assoc's for the
1889 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1891 /* process the INIT info (peer's info) */
1892 SCTP_TCB_SEND_UNLOCK(stcb);
1893 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1894 SCTP_INP_INFO_WUNLOCK();
1896 retval = sctp_process_init(init_cp, stcb, net);
1898 if (how_indx < sizeof(asoc->cookie_how))
1899 asoc->cookie_how[how_indx] = 13;
1904 * since we did not send a HB make sure we don't double
1907 net->hb_responded = 1;
1909 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1910 init_offset + sizeof(struct sctp_init_chunk),
1911 initack_offset, sh, init_src)) {
1912 if (how_indx < sizeof(asoc->cookie_how))
1913 asoc->cookie_how[how_indx] = 14;
1917 /* respond with a COOKIE-ACK */
1918 sctp_stop_all_cookie_timers(stcb);
1919 sctp_toss_old_cookies(stcb, asoc);
1920 sctp_send_cookie_ack(stcb);
1921 if (how_indx < sizeof(asoc->cookie_how))
1922 asoc->cookie_how[how_indx] = 15;
1926 if (how_indx < sizeof(asoc->cookie_how))
1927 asoc->cookie_how[how_indx] = 16;
1928 /* all other cases... */
1934 * handle a state cookie for a new association m: input packet mbuf chain--
1935 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1936 * and the cookie signature does not exist offset: offset into mbuf to the
1937 * cookie-echo chunk length: length of the cookie chunk to: where the init
1938 * was from returns a new TCB
1941 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1942 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1943 struct sctp_inpcb *inp, struct sctp_nets **netp,
1944 struct sockaddr *init_src, int *notification,
1945 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1946 uint32_t vrf_id, uint16_t port)
1948 struct sctp_tcb *stcb;
1949 struct sctp_init_chunk *init_cp, init_buf;
1950 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1951 struct sockaddr_storage sa_store;
1952 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1953 struct sockaddr_in *sin;
1954 struct sockaddr_in6 *sin6;
1955 struct sctp_association *asoc;
1957 int init_offset, initack_offset, initack_limit;
1961 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1963 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1966 so = SCTP_INP_SO(inp);
1970 * find and validate the INIT chunk in the cookie (peer's info) the
1971 * INIT should start after the cookie-echo header struct (chunk
1972 * header, state cookie header struct)
1974 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1975 init_cp = (struct sctp_init_chunk *)
1976 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1977 (uint8_t *) & init_buf);
1978 if (init_cp == NULL) {
1979 /* could not pull a INIT chunk in cookie */
1980 SCTPDBG(SCTP_DEBUG_INPUT1,
1981 "process_cookie_new: could not pull INIT chunk hdr\n");
1984 chk_length = ntohs(init_cp->ch.chunk_length);
1985 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1986 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1989 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1991 * find and validate the INIT-ACK chunk in the cookie (my info) the
1992 * INIT-ACK follows the INIT chunk
1994 initack_cp = (struct sctp_init_ack_chunk *)
1995 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1996 (uint8_t *) & initack_buf);
1997 if (initack_cp == NULL) {
1998 /* could not pull INIT-ACK chunk in cookie */
1999 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2002 chk_length = ntohs(initack_cp->ch.chunk_length);
2003 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2007 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2008 * "initack_limit" value. This is because the chk_length field
2009 * includes the length of the cookie, but the cookie is omitted when
2010 * the INIT and INIT_ACK are tacked onto the cookie...
2012 initack_limit = offset + cookie_len;
2015 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2020 * Here we do a trick, we set in NULL for the proc/thread argument.
2021 * We do this since in effect we only use the p argument when the
2022 * socket is unbound and we must do an implicit bind. Since we are
2023 * getting a cookie, we cannot be unbound.
2025 stcb = sctp_aloc_assoc(inp, init_src, &error,
2026 ntohl(initack_cp->init.initiate_tag), vrf_id,
2027 (struct thread *)NULL
2030 struct mbuf *op_err;
2032 /* memory problem? */
2033 SCTPDBG(SCTP_DEBUG_INPUT1,
2034 "process_cookie_new: no room for another TCB!\n");
2035 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2037 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2038 sh, op_err, vrf_id, port);
2041 /* get the correct sctp_nets */
2043 *netp = sctp_findnet(stcb, init_src);
2046 /* get scope variables out of cookie */
2047 asoc->ipv4_local_scope = cookie->ipv4_scope;
2048 asoc->site_scope = cookie->site_scope;
2049 asoc->local_scope = cookie->local_scope;
2050 asoc->loopback_scope = cookie->loopback_scope;
2052 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2053 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2054 struct mbuf *op_err;
2057 * Houston we have a problem. The EP changed while the
2058 * cookie was in flight. Only recourse is to abort the
2061 atomic_add_int(&stcb->asoc.refcnt, 1);
2062 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2063 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2064 sh, op_err, vrf_id, port);
2065 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2066 SCTP_TCB_UNLOCK(stcb);
2067 SCTP_SOCKET_LOCK(so, 1);
2068 SCTP_TCB_LOCK(stcb);
2070 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2071 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2072 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2073 SCTP_SOCKET_UNLOCK(so, 1);
2075 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2078 /* process the INIT-ACK info (my info) */
2079 old_tag = asoc->my_vtag;
2080 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2081 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2082 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2083 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2084 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2085 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2086 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
2087 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2088 asoc->str_reset_seq_in = asoc->init_seq_number;
2090 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2092 /* process the INIT info (peer's info) */
2094 retval = sctp_process_init(init_cp, stcb, *netp);
2098 atomic_add_int(&stcb->asoc.refcnt, 1);
2099 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2100 SCTP_TCB_UNLOCK(stcb);
2101 SCTP_SOCKET_LOCK(so, 1);
2102 SCTP_TCB_LOCK(stcb);
2104 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2105 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2106 SCTP_SOCKET_UNLOCK(so, 1);
2108 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2111 /* load all addresses */
2112 if (sctp_load_addresses_from_init(stcb, m, iphlen,
2113 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2115 atomic_add_int(&stcb->asoc.refcnt, 1);
2116 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2117 SCTP_TCB_UNLOCK(stcb);
2118 SCTP_SOCKET_LOCK(so, 1);
2119 SCTP_TCB_LOCK(stcb);
2121 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2122 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2123 SCTP_SOCKET_UNLOCK(so, 1);
2125 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2129 * verify any preceding AUTH chunk that was skipped
2131 /* pull the local authentication parameters from the cookie/init-ack */
2132 sctp_auth_get_cookie_params(stcb, m,
2133 initack_offset + sizeof(struct sctp_init_ack_chunk),
2134 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2136 struct sctp_auth_chunk *auth;
2138 auth = (struct sctp_auth_chunk *)
2139 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2140 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2141 /* auth HMAC failed, dump the assoc and packet */
2142 SCTPDBG(SCTP_DEBUG_AUTH1,
2143 "COOKIE-ECHO: AUTH failed\n");
2144 atomic_add_int(&stcb->asoc.refcnt, 1);
2145 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2146 SCTP_TCB_UNLOCK(stcb);
2147 SCTP_SOCKET_LOCK(so, 1);
2148 SCTP_TCB_LOCK(stcb);
2150 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2151 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2152 SCTP_SOCKET_UNLOCK(so, 1);
2154 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2157 /* remaining chunks checked... good to go */
2158 stcb->asoc.authenticated = 1;
2161 /* update current state */
2162 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2163 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2164 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2165 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2166 stcb->sctp_ep, stcb, asoc->primary_destination);
2168 sctp_stop_all_cookie_timers(stcb);
2169 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2170 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2173 * if we're doing ASCONFs, check to see if we have any new local
2174 * addresses that need to get added to the peer (eg. addresses
2175 * changed while cookie echo in flight). This needs to be done
2176 * after we go to the OPEN state to do the correct asconf
2177 * processing. else, make sure we have the correct addresses in our
2181 /* warning, we re-use sin, sin6, sa_store here! */
2182 /* pull in local_address (our "from" address) */
2183 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
2184 /* source addr is IPv4 */
2185 sin = (struct sockaddr_in *)initack_src;
2186 memset(sin, 0, sizeof(*sin));
2187 sin->sin_family = AF_INET;
2188 sin->sin_len = sizeof(struct sockaddr_in);
2189 sin->sin_addr.s_addr = cookie->laddress[0];
2190 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
2191 /* source addr is IPv6 */
2192 sin6 = (struct sockaddr_in6 *)initack_src;
2193 memset(sin6, 0, sizeof(*sin6));
2194 sin6->sin6_family = AF_INET6;
2195 sin6->sin6_len = sizeof(struct sockaddr_in6);
2196 sin6->sin6_scope_id = cookie->scope_id;
2197 memcpy(&sin6->sin6_addr, cookie->laddress,
2198 sizeof(sin6->sin6_addr));
2200 atomic_add_int(&stcb->asoc.refcnt, 1);
2201 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2202 SCTP_TCB_UNLOCK(stcb);
2203 SCTP_SOCKET_LOCK(so, 1);
2204 SCTP_TCB_LOCK(stcb);
2206 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2207 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2208 SCTP_SOCKET_UNLOCK(so, 1);
2210 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2214 /* set up to notify upper layer */
2215 *notification = SCTP_NOTIFY_ASSOC_UP;
2216 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2217 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2218 (inp->sctp_socket->so_qlimit == 0)) {
2220 * This is an endpoint that called connect() how it got a
2221 * cookie that is NEW is a bit of a mystery. It must be that
2222 * the INIT was sent, but before it got there.. a complete
2223 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2224 * should have went to the other code.. not here.. oh well..
2225 * a bit of protection is worth having..
2227 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2228 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2229 atomic_add_int(&stcb->asoc.refcnt, 1);
2230 SCTP_TCB_UNLOCK(stcb);
2231 SCTP_SOCKET_LOCK(so, 1);
2232 SCTP_TCB_LOCK(stcb);
2233 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2234 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2235 SCTP_SOCKET_UNLOCK(so, 1);
2239 soisconnected(stcb->sctp_socket);
2240 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2241 SCTP_SOCKET_UNLOCK(so, 1);
2243 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2244 (inp->sctp_socket->so_qlimit)) {
2246 * We don't want to do anything with this one. Since it is
2247 * the listening guy. The timer will get started for
2248 * accepted connections in the caller.
2252 /* since we did not send a HB make sure we don't double things */
2253 if ((netp) && (*netp))
2254 (*netp)->hb_responded = 1;
2256 if (stcb->asoc.sctp_autoclose_ticks &&
2257 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2258 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2260 /* calculate the RTT */
2261 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2262 if ((netp) && (*netp)) {
2263 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2264 &cookie->time_entered, sctp_align_unsafe_makecopy);
2266 /* respond with a COOKIE-ACK */
2267 sctp_send_cookie_ack(stcb);
2270 * check the address lists for any ASCONFs that need to be sent
2271 * AFTER the cookie-ack is sent
2273 sctp_check_address_list(stcb, m,
2274 initack_offset + sizeof(struct sctp_init_ack_chunk),
2275 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2276 initack_src, cookie->local_scope, cookie->site_scope,
2277 cookie->ipv4_scope, cookie->loopback_scope);
2284 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2285 * we NEED to make sure we are not already using the vtag. If so we
2286 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2287 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2288 SCTP_BASE_INFO(hashasocmark))];
2289 LIST_FOREACH(stcb, head, sctp_asocs) {
2290 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2291 -- SEND ABORT - TRY AGAIN --
2297 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2298 * existing (non-NULL) TCB
2300 static struct mbuf *
2301 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2302 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2303 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2304 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2305 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2307 struct sctp_state_cookie *cookie;
2308 struct sockaddr_in6 sin6;
2309 struct sockaddr_in sin;
2310 struct sctp_tcb *l_stcb = *stcb;
2311 struct sctp_inpcb *l_inp;
2312 struct sockaddr *to;
2313 sctp_assoc_t sac_restart_id;
2314 struct sctp_pcb *ep;
2316 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2318 uint8_t cookie_ok = 0;
2319 unsigned int size_of_pkt, sig_offset, cookie_offset;
2320 unsigned int cookie_len;
2322 struct timeval time_expires;
2323 struct sockaddr_storage dest_store;
2324 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2326 int notification = 0;
2327 struct sctp_nets *netl;
2328 int had_a_existing_tcb = 0;
2330 SCTPDBG(SCTP_DEBUG_INPUT2,
2331 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2333 if (inp_p == NULL) {
2336 /* First get the destination address setup too. */
2337 iph = mtod(m, struct ip *);
2338 switch (iph->ip_v) {
2342 struct sockaddr_in *lsin;
2344 lsin = (struct sockaddr_in *)(localep_sa);
2345 memset(lsin, 0, sizeof(*lsin));
2346 lsin->sin_family = AF_INET;
2347 lsin->sin_len = sizeof(*lsin);
2348 lsin->sin_port = sh->dest_port;
2349 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2350 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2354 case IPV6_VERSION >> 4:
2357 struct ip6_hdr *ip6;
2358 struct sockaddr_in6 *lsin6;
2360 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2361 memset(lsin6, 0, sizeof(*lsin6));
2362 lsin6->sin6_family = AF_INET6;
2363 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2364 ip6 = mtod(m, struct ip6_hdr *);
2365 lsin6->sin6_port = sh->dest_port;
2366 lsin6->sin6_addr = ip6->ip6_dst;
2367 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2375 cookie = &cp->cookie;
2376 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2377 cookie_len = ntohs(cp->ch.chunk_length);
2379 if ((cookie->peerport != sh->src_port) &&
2380 (cookie->myport != sh->dest_port) &&
2381 (cookie->my_vtag != sh->v_tag)) {
2383 * invalid ports or bad tag. Note that we always leave the
2384 * v_tag in the header in network order and when we stored
2385 * it in the my_vtag slot we also left it in network order.
2386 * This maintains the match even though it may be in the
2387 * opposite byte order of the machine :->
2391 if (cookie_len > size_of_pkt ||
2392 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2393 sizeof(struct sctp_init_chunk) +
2394 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2395 /* cookie too long! or too small */
2399 * split off the signature into its own mbuf (since it should not be
2400 * calculated in the sctp_hmac_m() call).
2402 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2403 if (sig_offset > size_of_pkt) {
2404 /* packet not correct size! */
2405 /* XXX this may already be accounted for earlier... */
2408 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2409 if (m_sig == NULL) {
2410 /* out of memory or ?? */
2413 #ifdef SCTP_MBUF_LOGGING
2414 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2419 if (SCTP_BUF_IS_EXTENDED(mat)) {
2420 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2422 mat = SCTP_BUF_NEXT(mat);
2428 * compute the signature/digest for the cookie
2430 ep = &(*inp_p)->sctp_ep;
2433 SCTP_TCB_UNLOCK(l_stcb);
2435 SCTP_INP_RLOCK(l_inp);
2437 SCTP_TCB_LOCK(l_stcb);
2439 /* which cookie is it? */
2440 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2441 (ep->current_secret_number != ep->last_secret_number)) {
2442 /* it's the old cookie */
2443 (void)sctp_hmac_m(SCTP_HMAC,
2444 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2445 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2447 /* it's the current cookie */
2448 (void)sctp_hmac_m(SCTP_HMAC,
2449 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2450 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2452 /* get the signature */
2453 SCTP_INP_RUNLOCK(l_inp);
2454 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2456 /* couldn't find signature */
2457 sctp_m_freem(m_sig);
2460 /* compare the received digest with the computed digest */
2461 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2462 /* try the old cookie? */
2463 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2464 (ep->current_secret_number != ep->last_secret_number)) {
2465 /* compute digest with old */
2466 (void)sctp_hmac_m(SCTP_HMAC,
2467 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2468 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2470 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2478 * Now before we continue we must reconstruct our mbuf so that
2479 * normal processing of any other chunks will work.
2485 while (SCTP_BUF_NEXT(m_at) != NULL) {
2486 m_at = SCTP_BUF_NEXT(m_at);
2488 SCTP_BUF_NEXT(m_at) = m_sig;
2491 if (cookie_ok == 0) {
2492 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2493 SCTPDBG(SCTP_DEBUG_INPUT2,
2494 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2495 (uint32_t) offset, cookie_offset, sig_offset);
2499 * check the cookie timestamps to be sure it's not stale
2501 (void)SCTP_GETTIME_TIMEVAL(&now);
2502 /* Expire time is in Ticks, so we convert to seconds */
2503 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2504 time_expires.tv_usec = cookie->time_entered.tv_usec;
2506 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2509 if (timevalcmp(&now, &time_expires, >)) {
2510 /* cookie is stale! */
2511 struct mbuf *op_err;
2512 struct sctp_stale_cookie_msg *scm;
2515 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2516 0, M_DONTWAIT, 1, MT_DATA);
2517 if (op_err == NULL) {
2522 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2523 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2524 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2525 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2526 (sizeof(uint32_t))));
2527 /* seconds to usec */
2528 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2531 tim = now.tv_usec - cookie->time_entered.tv_usec;
2532 scm->time_usec = htonl(tim);
2533 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2538 * Now we must see with the lookup address if we have an existing
2539 * asoc. This will only happen if we were in the COOKIE-WAIT state
2540 * and a INIT collided with us and somewhere the peer sent the
2541 * cookie on another address besides the single address our assoc
2542 * had for him. In this case we will have one of the tie-tags set at
2543 * least AND the address field in the cookie can be used to look it
2547 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2548 memset(&sin6, 0, sizeof(sin6));
2549 sin6.sin6_family = AF_INET6;
2550 sin6.sin6_len = sizeof(sin6);
2551 sin6.sin6_port = sh->src_port;
2552 sin6.sin6_scope_id = cookie->scope_id;
2553 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2554 sizeof(sin6.sin6_addr.s6_addr));
2555 to = (struct sockaddr *)&sin6;
2556 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2557 memset(&sin, 0, sizeof(sin));
2558 sin.sin_family = AF_INET;
2559 sin.sin_len = sizeof(sin);
2560 sin.sin_port = sh->src_port;
2561 sin.sin_addr.s_addr = cookie->address[0];
2562 to = (struct sockaddr *)&sin;
2564 /* This should not happen */
2567 if ((*stcb == NULL) && to) {
2568 /* Yep, lets check */
2569 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2570 if (*stcb == NULL) {
2572 * We should have only got back the same inp. If we
2573 * got back a different ep we have a problem. The
2574 * original findep got back l_inp and now
2576 if (l_inp != *inp_p) {
2577 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2580 if (*locked_tcb == NULL) {
2582 * In this case we found the assoc only
2583 * after we locked the create lock. This
2584 * means we are in a colliding case and we
2585 * must make sure that we unlock the tcb if
2586 * its one of the cases where we throw away
2587 * the incoming packets.
2589 *locked_tcb = *stcb;
2592 * We must also increment the inp ref count
2593 * since the ref_count flags was set when we
2594 * did not find the TCB, now we found it
2595 * which reduces the refcount.. we must
2596 * raise it back out to balance it all :-)
2598 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2599 if ((*stcb)->sctp_ep != l_inp) {
2600 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2601 (*stcb)->sctp_ep, l_inp);
2609 cookie_len -= SCTP_SIGNATURE_SIZE;
2610 if (*stcb == NULL) {
2611 /* this is the "normal" case... get a new TCB */
2612 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2613 cookie_len, *inp_p, netp, to, ¬ification,
2614 auth_skipped, auth_offset, auth_len, vrf_id, port);
2616 /* this is abnormal... cookie-echo on existing TCB */
2617 had_a_existing_tcb = 1;
2618 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2619 cookie, cookie_len, *inp_p, *stcb, netp, to,
2620 ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port);
2623 if (*stcb == NULL) {
2624 /* still no TCB... must be bad cookie-echo */
2628 * Ok, we built an association so confirm the address we sent the
2631 netl = sctp_findnet(*stcb, to);
2633 * This code should in theory NOT run but
2636 /* TSNH! Huh, why do I need to add this address here? */
2639 ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE,
2640 SCTP_IN_COOKIE_PROC);
2641 netl = sctp_findnet(*stcb, to);
2644 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2645 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2646 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2648 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2649 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2653 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p,
2656 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2657 if (!had_a_existing_tcb ||
2658 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2660 * If we have a NEW cookie or the connect never
2661 * reached the connected state during collision we
2662 * must do the TCP accept thing.
2664 struct socket *so, *oso;
2665 struct sctp_inpcb *inp;
2667 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2669 * For a restart we will keep the same
2670 * socket, no need to do anything. I THINK!!
2672 sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED);
2675 oso = (*inp_p)->sctp_socket;
2676 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2677 SCTP_TCB_UNLOCK((*stcb));
2678 so = sonewconn(oso, 0
2680 SCTP_TCB_LOCK((*stcb));
2681 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2684 struct mbuf *op_err;
2686 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2687 struct socket *pcb_so;
2690 /* Too many sockets */
2691 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2692 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2693 sctp_abort_association(*inp_p, NULL, m, iphlen,
2694 sh, op_err, vrf_id, port);
2695 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2696 pcb_so = SCTP_INP_SO(*inp_p);
2697 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2698 SCTP_TCB_UNLOCK((*stcb));
2699 SCTP_SOCKET_LOCK(pcb_so, 1);
2700 SCTP_TCB_LOCK((*stcb));
2701 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2703 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2704 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2705 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2709 inp = (struct sctp_inpcb *)so->so_pcb;
2710 SCTP_INP_INCR_REF(inp);
2712 * We add the unbound flag here so that if we get an
2713 * soabort() before we get the move_pcb done, we
2714 * will properly cleanup.
2716 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2717 SCTP_PCB_FLAGS_CONNECTED |
2718 SCTP_PCB_FLAGS_IN_TCPPOOL |
2719 SCTP_PCB_FLAGS_UNBOUND |
2720 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2721 SCTP_PCB_FLAGS_DONT_WAKE);
2722 inp->sctp_features = (*inp_p)->sctp_features;
2723 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2724 inp->sctp_socket = so;
2725 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2726 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2727 inp->sctp_context = (*inp_p)->sctp_context;
2728 inp->inp_starting_point_for_iterator = NULL;
2730 * copy in the authentication parameters from the
2733 if (inp->sctp_ep.local_hmacs)
2734 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2735 inp->sctp_ep.local_hmacs =
2736 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2737 if (inp->sctp_ep.local_auth_chunks)
2738 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2739 inp->sctp_ep.local_auth_chunks =
2740 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2743 * Now we must move it from one hash table to
2744 * another and get the tcb in the right place.
2746 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2748 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2749 SCTP_TCB_UNLOCK((*stcb));
2751 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2753 SCTP_TCB_LOCK((*stcb));
2754 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2758 * now we must check to see if we were aborted while
2759 * the move was going on and the lock/unlock
2762 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2764 * yep it was, we leave the assoc attached
2765 * to the socket since the sctp_inpcb_free()
2766 * call will send an abort for us.
2768 SCTP_INP_DECR_REF(inp);
2771 SCTP_INP_DECR_REF(inp);
2772 /* Switch over to the new guy */
2774 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2777 * Pull it from the incomplete queue and wake the
2780 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2781 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2782 SCTP_TCB_UNLOCK((*stcb));
2783 SCTP_SOCKET_LOCK(so, 1);
2786 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2787 SCTP_TCB_LOCK((*stcb));
2788 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2789 SCTP_SOCKET_UNLOCK(so, 1);
2794 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2795 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2801 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2802 struct sctp_tcb *stcb, struct sctp_nets *net)
2804 /* cp must not be used, others call this without a c-ack :-) */
2805 struct sctp_association *asoc;
2807 SCTPDBG(SCTP_DEBUG_INPUT2,
2808 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2814 sctp_stop_all_cookie_timers(stcb);
2815 /* process according to association state */
2816 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2817 /* state change only needed when I am in right state */
2818 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2819 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2820 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2821 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2822 stcb->sctp_ep, stcb, asoc->primary_destination);
2826 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2827 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2828 if (asoc->overall_error_count == 0) {
2829 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2830 &asoc->time_entered, sctp_align_safe_nocopy);
2832 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2833 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2834 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2835 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2836 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2840 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2841 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2842 so = SCTP_INP_SO(stcb->sctp_ep);
2843 atomic_add_int(&stcb->asoc.refcnt, 1);
2844 SCTP_TCB_UNLOCK(stcb);
2845 SCTP_SOCKET_LOCK(so, 1);
2846 SCTP_TCB_LOCK(stcb);
2847 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2848 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2849 SCTP_SOCKET_UNLOCK(so, 1);
2853 soisconnected(stcb->sctp_socket);
2854 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2855 SCTP_SOCKET_UNLOCK(so, 1);
2858 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2861 * since we did not send a HB make sure we don't double
2864 net->hb_responded = 1;
2866 if (stcb->asoc.sctp_autoclose_ticks &&
2867 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2868 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2869 stcb->sctp_ep, stcb, NULL);
2872 * send ASCONF if parameters are pending and ASCONFs are
2873 * allowed (eg. addresses changed when init/cookie echo were
2876 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2877 (stcb->asoc.peer_supports_asconf) &&
2878 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2879 #ifdef SCTP_TIMER_BASED_ASCONF
2880 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2881 stcb->sctp_ep, stcb,
2882 stcb->asoc.primary_destination);
2884 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2885 SCTP_ADDR_NOT_LOCKED);
2889 /* Toss the cookie if I can */
2890 sctp_toss_old_cookies(stcb, asoc);
2891 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2892 /* Restart the timer if we have pending data */
2893 struct sctp_tmit_chunk *chk;
2895 chk = TAILQ_FIRST(&asoc->sent_queue);
2897 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2904 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2905 struct sctp_tcb *stcb)
2907 struct sctp_nets *net;
2908 struct sctp_tmit_chunk *lchk;
2911 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2914 SCTP_STAT_INCR(sctps_recvecne);
2915 tsn = ntohl(cp->tsn);
2916 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2917 /* Also we make sure we disable the nonce_wait */
2918 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2920 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2922 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2924 stcb->asoc.nonce_wait_for_ecne = 0;
2925 stcb->asoc.nonce_sum_check = 0;
2927 /* Find where it was sent, if possible */
2929 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2931 if (lchk->rec.data.TSN_seq == tsn) {
2935 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2937 lchk = TAILQ_NEXT(lchk, sctp_next);
2940 /* default is we use the primary */
2941 net = stcb->asoc.primary_destination;
2943 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2945 * JRS - Use the congestion control given in the pluggable
2948 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net);
2950 * we reduce once every RTT. So we will only lower cwnd at
2951 * the next sending seq i.e. the resync_tsn.
2953 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2956 * We always send a CWR this way if our previous one was lost our
2957 * peer will get an update, or if it is not time again to reduce we
2958 * still get the cwr to the peer.
2960 sctp_send_cwr(stcb, net, tsn);
2964 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2967 * Here we get a CWR from the peer. We must look in the outqueue and
2968 * make sure that we have a covered ECNE in teh control chunk part.
2971 struct sctp_tmit_chunk *chk;
2972 struct sctp_ecne_chunk *ecne;
2974 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2975 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2979 * Look for and remove if it is the right TSN. Since there
2980 * is only ONE ECNE on the control queue at any one time we
2981 * don't need to worry about more than one!
2983 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2984 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2985 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2986 /* this covers this ECNE, we can remove it */
2987 stcb->asoc.ecn_echo_cnt_onq--;
2988 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2991 sctp_m_freem(chk->data);
2994 stcb->asoc.ctrl_queue_cnt--;
2995 sctp_free_a_chunk(stcb, chk);
3002 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
3003 struct sctp_tcb *stcb, struct sctp_nets *net)
3005 struct sctp_association *asoc;
3007 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3012 SCTPDBG(SCTP_DEBUG_INPUT2,
3013 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3018 /* process according to association state */
3019 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3020 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3021 SCTPDBG(SCTP_DEBUG_INPUT2,
3022 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3023 SCTP_TCB_UNLOCK(stcb);
3026 /* notify upper layer protocol */
3027 if (stcb->sctp_socket) {
3028 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3029 /* are the queues empty? they should be */
3030 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3031 !TAILQ_EMPTY(&asoc->sent_queue) ||
3032 !TAILQ_EMPTY(&asoc->out_wheel)) {
3033 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
3036 /* stop the timer */
3037 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3038 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3040 SCTPDBG(SCTP_DEBUG_INPUT2,
3041 "sctp_handle_shutdown_complete: calls free-asoc\n");
3042 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3043 so = SCTP_INP_SO(stcb->sctp_ep);
3044 atomic_add_int(&stcb->asoc.refcnt, 1);
3045 SCTP_TCB_UNLOCK(stcb);
3046 SCTP_SOCKET_LOCK(so, 1);
3047 SCTP_TCB_LOCK(stcb);
3048 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3050 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3051 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3052 SCTP_SOCKET_UNLOCK(so, 1);
3058 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3059 struct sctp_nets *net, uint8_t flg)
3061 switch (desc->chunk_type) {
3063 /* find the tsn to resend (possibly */
3066 struct sctp_tmit_chunk *tp1;
3068 tsn = ntohl(desc->tsn_ifany);
3069 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3071 if (tp1->rec.data.TSN_seq == tsn) {
3075 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
3081 tp1 = TAILQ_NEXT(tp1, sctp_next);
3085 * Do it the other way , aka without paying
3086 * attention to queue seq order.
3088 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3089 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3091 if (tp1->rec.data.TSN_seq == tsn) {
3095 tp1 = TAILQ_NEXT(tp1, sctp_next);
3099 SCTP_STAT_INCR(sctps_pdrptsnnf);
3101 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3104 if ((stcb->asoc.peers_rwnd == 0) &&
3105 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3106 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3109 if (stcb->asoc.peers_rwnd == 0 &&
3110 (flg & SCTP_FROM_MIDDLE_BOX)) {
3111 SCTP_STAT_INCR(sctps_pdrpdizrw);
3114 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3115 sizeof(struct sctp_data_chunk));
3119 for (iii = 0; iii < sizeof(desc->data_bytes);
3121 if (ddp[iii] != desc->data_bytes[iii]) {
3122 SCTP_STAT_INCR(sctps_pdrpbadd);
3128 * We zero out the nonce so resync not
3131 tp1->rec.data.ect_nonce = 0;
3135 * this guy had a RTO calculation
3136 * pending on it, cancel it
3140 SCTP_STAT_INCR(sctps_pdrpmark);
3141 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3142 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3143 tp1->sent = SCTP_DATAGRAM_RESEND;
3145 * mark it as if we were doing a FR, since
3146 * we will be getting gap ack reports behind
3147 * the info from the router.
3149 tp1->rec.data.doing_fast_retransmit = 1;
3151 * mark the tsn with what sequences can
3154 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3155 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3157 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3160 /* restart the timer */
3161 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3162 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3163 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3166 /* fix counts and things */
3167 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3168 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3169 tp1->whoTo->flight_size,
3172 tp1->rec.data.TSN_seq);
3174 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3175 sctp_flight_size_decrease(tp1);
3176 sctp_total_flight_decrease(stcb, tp1);
3183 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3184 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3187 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3189 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3192 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3193 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3194 audit, stcb->asoc.sent_queue_retran_cnt);
3195 #ifndef SCTP_AUDITING_ENABLED
3196 stcb->asoc.sent_queue_retran_cnt = audit;
3204 struct sctp_tmit_chunk *asconf;
3206 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3208 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3213 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3214 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3215 asconf->sent = SCTP_DATAGRAM_RESEND;
3216 asconf->snd_count--;
3220 case SCTP_INITIATION:
3221 /* resend the INIT */
3222 stcb->asoc.dropped_special_cnt++;
3223 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3225 * If we can get it in, in a few attempts we do
3226 * this, otherwise we let the timer fire.
3228 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3229 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3230 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3233 case SCTP_SELECTIVE_ACK:
3234 case SCTP_NR_SELECTIVE_ACK:
3235 /* resend the sack */
3236 sctp_send_sack(stcb);
3238 case SCTP_HEARTBEAT_REQUEST:
3239 /* resend a demand HB */
3240 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3242 * Only retransmit if we KNOW we wont destroy the
3245 (void)sctp_send_hb(stcb, 1, net);
3249 sctp_send_shutdown(stcb, net);
3251 case SCTP_SHUTDOWN_ACK:
3252 sctp_send_shutdown_ack(stcb, net);
3254 case SCTP_COOKIE_ECHO:
3256 struct sctp_tmit_chunk *cookie;
3259 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3261 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3266 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3267 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3268 cookie->sent = SCTP_DATAGRAM_RESEND;
3269 sctp_stop_all_cookie_timers(stcb);
3273 case SCTP_COOKIE_ACK:
3274 sctp_send_cookie_ack(stcb);
3276 case SCTP_ASCONF_ACK:
3277 /* resend last asconf ack */
3278 sctp_send_asconf_ack(stcb);
3280 case SCTP_FORWARD_CUM_TSN:
3281 send_forward_tsn(stcb, &stcb->asoc);
3283 /* can't do anything with these */
3284 case SCTP_PACKET_DROPPED:
3285 case SCTP_INITIATION_ACK: /* this should not happen */
3286 case SCTP_HEARTBEAT_ACK:
3287 case SCTP_ABORT_ASSOCIATION:
3288 case SCTP_OPERATION_ERROR:
3289 case SCTP_SHUTDOWN_COMPLETE:
3299 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3305 * We set things to 0xffff since this is the last delivered sequence
3306 * and we will be sending in 0 after the reset.
3309 if (number_entries) {
3310 for (i = 0; i < number_entries; i++) {
3311 temp = ntohs(list[i]);
3312 if (temp >= stcb->asoc.streamincnt) {
3315 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3319 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3320 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3323 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3327 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3331 if (number_entries == 0) {
3332 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3333 stcb->asoc.strmout[i].next_sequence_sent = 0;
3335 } else if (number_entries) {
3336 for (i = 0; i < number_entries; i++) {
3339 temp = ntohs(list[i]);
3340 if (temp >= stcb->asoc.streamoutcnt) {
3341 /* no such stream */
3344 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3347 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3351 struct sctp_stream_reset_out_request *
3352 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3354 struct sctp_association *asoc;
3355 struct sctp_stream_reset_out_req *req;
3356 struct sctp_stream_reset_out_request *r;
3357 struct sctp_tmit_chunk *chk;
3361 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3362 asoc->stream_reset_outstanding = 0;
3365 if (stcb->asoc.str_reset == NULL) {
3366 asoc->stream_reset_outstanding = 0;
3369 chk = stcb->asoc.str_reset;
3370 if (chk->data == NULL) {
3374 /* he wants a copy of the chk pointer */
3377 clen = chk->send_size;
3378 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3380 if (ntohl(r->request_seq) == seq) {
3384 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3385 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3386 /* move to the next one, there can only be a max of two */
3387 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3388 if (ntohl(r->request_seq) == seq) {
3392 /* that seq is not here */
3397 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3399 struct sctp_association *asoc;
3400 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3402 if (stcb->asoc.str_reset == NULL) {
3407 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3408 TAILQ_REMOVE(&asoc->control_send_queue,
3412 sctp_m_freem(chk->data);
3415 asoc->ctrl_queue_cnt--;
3416 sctp_free_a_chunk(stcb, chk);
3417 /* sa_ignore NO_NULL_CHK */
3418 stcb->asoc.str_reset = NULL;
3423 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3424 uint32_t seq, uint32_t action,
3425 struct sctp_stream_reset_response *respin)
3429 struct sctp_association *asoc = &stcb->asoc;
3430 struct sctp_tmit_chunk *chk;
3431 struct sctp_stream_reset_out_request *srparam;
3434 if (asoc->stream_reset_outstanding == 0) {
3438 if (seq == stcb->asoc.str_reset_seq_out) {
3439 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3441 stcb->asoc.str_reset_seq_out++;
3442 type = ntohs(srparam->ph.param_type);
3443 lparm_len = ntohs(srparam->ph.param_length);
3444 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3445 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3446 asoc->stream_reset_out_is_outstanding = 0;
3447 if (asoc->stream_reset_outstanding)
3448 asoc->stream_reset_outstanding--;
3449 if (action == SCTP_STREAM_RESET_PERFORMED) {
3451 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3453 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3455 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3456 /* Answered my request */
3457 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3458 if (asoc->stream_reset_outstanding)
3459 asoc->stream_reset_outstanding--;
3460 if (action != SCTP_STREAM_RESET_PERFORMED) {
3461 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3463 } else if (type == SCTP_STR_RESET_ADD_STREAMS) {
3464 /* Ok we now may have more streams */
3465 if (asoc->stream_reset_outstanding)
3466 asoc->stream_reset_outstanding--;
3467 if (action == SCTP_STREAM_RESET_PERFORMED) {
3468 /* Put the new streams into effect */
3469 stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize;
3470 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb,
3471 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3473 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb,
3474 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3476 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3478 * a) Adopt the new in tsn.
3480 * c) Adopt the new out-tsn
3482 struct sctp_stream_reset_response_tsn *resp;
3483 struct sctp_forward_tsn_chunk fwdtsn;
3486 if (respin == NULL) {
3490 if (action == SCTP_STREAM_RESET_PERFORMED) {
3491 resp = (struct sctp_stream_reset_response_tsn *)respin;
3492 asoc->stream_reset_outstanding--;
3493 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3494 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3495 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3496 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3500 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3501 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3502 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3504 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3505 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3506 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3508 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3509 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3511 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3512 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3514 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3515 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3519 /* get rid of the request and get the request flags */
3520 if (asoc->stream_reset_outstanding == 0) {
3521 sctp_clean_up_stream_reset(stcb);
3529 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3530 struct sctp_tmit_chunk *chk,
3531 struct sctp_stream_reset_in_request *req, int trunc)
3539 * peer wants me to send a str-reset to him for my outgoing seq's if
3542 struct sctp_association *asoc = &stcb->asoc;
3544 seq = ntohl(req->request_seq);
3545 if (asoc->str_reset_seq_in == seq) {
3547 /* Can't do it, since they exceeded our buffer size */
3548 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3549 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3550 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3551 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3552 len = ntohs(req->ph.param_length);
3553 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3554 for (i = 0; i < number_entries; i++) {
3555 temp = ntohs(req->list_of_streams[i]);
3556 req->list_of_streams[i] = temp;
3558 /* move the reset action back one */
3559 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3560 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3561 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3562 asoc->str_reset_seq_out,
3563 seq, (asoc->sending_seq - 1));
3564 asoc->stream_reset_out_is_outstanding = 1;
3565 asoc->str_reset = chk;
3566 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3567 stcb->asoc.stream_reset_outstanding++;
3569 /* Can't do it, since we have sent one out */
3570 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3571 asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
3572 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3574 asoc->str_reset_seq_in++;
3575 } else if (asoc->str_reset_seq_in - 1 == seq) {
3576 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3577 } else if (asoc->str_reset_seq_in - 2 == seq) {
3578 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3580 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3585 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3586 struct sctp_tmit_chunk *chk,
3587 struct sctp_stream_reset_tsn_request *req)
3589 /* reset all in and out and update the tsn */
3591 * A) reset my str-seq's on in and out. B) Select a receive next,
3592 * and set cum-ack to it. Also process this selected number as a
3593 * fwd-tsn as well. C) set in the response my next sending seq.
3595 struct sctp_forward_tsn_chunk fwdtsn;
3596 struct sctp_association *asoc = &stcb->asoc;
3600 seq = ntohl(req->request_seq);
3601 if (asoc->str_reset_seq_in == seq) {
3602 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3603 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3604 fwdtsn.ch.chunk_flags = 0;
3605 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3606 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3610 stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3611 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3612 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3614 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3615 stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3616 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3617 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3618 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3619 atomic_add_int(&stcb->asoc.sending_seq, 1);
3620 /* save off historical data for retrans */
3621 stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3622 stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3623 stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3624 stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3626 sctp_add_stream_reset_result_tsn(chk,
3627 ntohl(req->request_seq),
3628 SCTP_STREAM_RESET_PERFORMED,
3629 stcb->asoc.sending_seq,
3630 stcb->asoc.mapping_array_base_tsn);
3631 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3632 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3633 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3634 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3636 asoc->str_reset_seq_in++;
3637 } else if (asoc->str_reset_seq_in - 1 == seq) {
3638 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3639 stcb->asoc.last_sending_seq[0],
3640 stcb->asoc.last_base_tsnsent[0]
3642 } else if (asoc->str_reset_seq_in - 2 == seq) {
3643 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3644 stcb->asoc.last_sending_seq[1],
3645 stcb->asoc.last_base_tsnsent[1]
3648 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3654 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3655 struct sctp_tmit_chunk *chk,
3656 struct sctp_stream_reset_out_request *req, int trunc)
3659 int number_entries, len;
3660 struct sctp_association *asoc = &stcb->asoc;
3662 seq = ntohl(req->request_seq);
3664 /* now if its not a duplicate we process it */
3665 if (asoc->str_reset_seq_in == seq) {
3666 len = ntohs(req->ph.param_length);
3667 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3669 * the sender is resetting, handle the list issue.. we must
3670 * a) verify if we can do the reset, if so no problem b) If
3671 * we can't do the reset we must copy the request. c) queue
3672 * it, and setup the data in processor to trigger it off
3673 * when needed and dequeue all the queued data.
3675 tsn = ntohl(req->send_reset_at_tsn);
3677 /* move the reset action back one */
3678 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3680 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3681 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3682 } else if ((tsn == asoc->cumulative_tsn) ||
3683 (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) {
3684 /* we can do it now */
3685 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3686 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3687 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3690 * we must queue it up and thus wait for the TSN's
3691 * to arrive that are at or before tsn
3693 struct sctp_stream_reset_list *liste;
3696 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3697 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3698 siz, SCTP_M_STRESET);
3699 if (liste == NULL) {
3700 /* gak out of memory */
3701 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3702 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3706 liste->number_entries = number_entries;
3707 memcpy(&liste->req, req,
3708 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3709 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3710 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3711 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3713 asoc->str_reset_seq_in++;
3714 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3716 * one seq back, just echo back last action since my
3717 * response was lost.
3719 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3720 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3722 * two seq back, just echo back last action since my
3723 * response was lost.
3725 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3727 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3732 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3733 struct sctp_stream_reset_add_strm *str_add)
3736 * Peer is requesting to add more streams. If its within our
3737 * max-streams we will allow it.
3739 uint16_t num_stream, i;
3741 struct sctp_association *asoc = &stcb->asoc;
3742 struct sctp_queued_to_read *ctl;
3744 /* Get the number. */
3745 seq = ntohl(str_add->request_seq);
3746 num_stream = ntohs(str_add->number_of_streams);
3747 /* Now what would be the new total? */
3748 if (asoc->str_reset_seq_in == seq) {
3749 num_stream += stcb->asoc.streamincnt;
3750 if (num_stream > stcb->asoc.max_inbound_streams) {
3751 /* We must reject it they ask for to many */
3753 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3754 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3755 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3757 /* Ok, we can do that :-) */
3758 struct sctp_stream_in *oldstrm;
3760 /* save off the old */
3761 oldstrm = stcb->asoc.strmin;
3762 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3763 (num_stream * sizeof(struct sctp_stream_in)),
3765 if (stcb->asoc.strmin == NULL) {
3766 stcb->asoc.strmin = oldstrm;
3769 /* copy off the old data */
3770 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3771 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3772 stcb->asoc.strmin[i].stream_no = i;
3773 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3774 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3775 /* now anything on those queues? */
3776 while (TAILQ_EMPTY(&oldstrm[i].inqueue) == 0) {
3777 ctl = TAILQ_FIRST(&oldstrm[i].inqueue);
3778 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3779 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3782 /* Init the new streams */
3783 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3784 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3785 stcb->asoc.strmin[i].stream_no = i;
3786 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3787 stcb->asoc.strmin[i].delivery_started = 0;
3789 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3790 /* update the size */
3791 stcb->asoc.streamincnt = num_stream;
3793 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3794 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3795 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3796 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb,
3797 (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED);
3799 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3801 * one seq back, just echo back last action since my
3802 * response was lost.
3804 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3805 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3807 * two seq back, just echo back last action since my
3808 * response was lost.
3810 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3812 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3818 __attribute__((noinline))
3821 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3822 struct sctp_stream_reset_out_req *sr_req)
3824 int chk_length, param_len, ptype;
3825 struct sctp_paramhdr pstore;
3826 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3831 struct sctp_tmit_chunk *chk;
3832 struct sctp_chunkhdr *ch;
3833 struct sctp_paramhdr *ph;
3837 /* now it may be a reset or a reset-response */
3838 chk_length = ntohs(sr_req->ch.chunk_length);
3840 /* setup for adding the response */
3841 sctp_alloc_a_chunk(stcb, chk);
3845 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3846 chk->rec.chunk_id.can_take_data = 0;
3847 chk->asoc = &stcb->asoc;
3848 chk->no_fr_allowed = 0;
3849 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3850 chk->book_size_scale = 0;
3851 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3852 if (chk->data == NULL) {
3855 sctp_m_freem(chk->data);
3858 sctp_free_a_chunk(stcb, chk);
3861 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3863 /* setup chunk parameters */
3864 chk->sent = SCTP_DATAGRAM_UNSENT;
3866 chk->whoTo = stcb->asoc.primary_destination;
3867 atomic_add_int(&chk->whoTo->ref_count, 1);
3869 ch = mtod(chk->data, struct sctp_chunkhdr *);
3870 ch->chunk_type = SCTP_STREAM_RESET;
3871 ch->chunk_flags = 0;
3872 ch->chunk_length = htons(chk->send_size);
3873 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3874 offset += sizeof(struct sctp_chunkhdr);
3875 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
3876 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
3879 param_len = ntohs(ph->param_length);
3880 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
3884 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
3885 (uint8_t *) & cstore);
3886 ptype = ntohs(ph->param_type);
3888 if (param_len > (int)sizeof(cstore)) {
3894 if (num_param > SCTP_MAX_RESET_PARAMS) {
3895 /* hit the max of parameters already sorry.. */
3898 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
3899 struct sctp_stream_reset_out_request *req_out;
3901 req_out = (struct sctp_stream_reset_out_request *)ph;
3903 if (stcb->asoc.stream_reset_outstanding) {
3904 seq = ntohl(req_out->response_seq);
3905 if (seq == stcb->asoc.str_reset_seq_out) {
3907 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
3910 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
3911 } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) {
3912 struct sctp_stream_reset_add_strm *str_add;
3914 str_add = (struct sctp_stream_reset_add_strm *)ph;
3916 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
3917 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
3918 struct sctp_stream_reset_in_request *req_in;
3922 req_in = (struct sctp_stream_reset_in_request *)ph;
3924 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
3925 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
3926 struct sctp_stream_reset_tsn_request *req_tsn;
3929 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
3931 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
3933 goto strres_nochunk;
3937 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
3938 struct sctp_stream_reset_response *resp;
3941 resp = (struct sctp_stream_reset_response *)ph;
3942 seq = ntohl(resp->response_seq);
3943 result = ntohl(resp->result);
3944 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
3946 goto strres_nochunk;
3951 offset += SCTP_SIZE32(param_len);
3952 chk_length -= SCTP_SIZE32(param_len);
3955 /* we have no response free the stuff */
3956 goto strres_nochunk;
3958 /* ok we have a chunk to link in */
3959 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
3962 stcb->asoc.ctrl_queue_cnt++;
3967 * Handle a router or endpoints report of a packet loss, there are two ways
3968 * to handle this, either we get the whole packet and must disect it
3969 * ourselves (possibly with truncation and or corruption) or it is a summary
3970 * from a middle box that did the disectting for us.
3973 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
3974 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
3976 uint32_t bottle_bw, on_queue;
3980 struct sctp_chunk_desc desc;
3981 struct sctp_chunkhdr *ch;
3983 chlen = ntohs(cp->ch.chunk_length);
3984 chlen -= sizeof(struct sctp_pktdrop_chunk);
3985 /* XXX possible chlen underflow */
3988 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
3989 SCTP_STAT_INCR(sctps_pdrpbwrpt);
3991 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
3992 chlen -= sizeof(struct sctphdr);
3993 /* XXX possible chlen underflow */
3994 memset(&desc, 0, sizeof(desc));
3996 trunc_len = (uint16_t) ntohs(cp->trunc_len);
3997 if (trunc_len > limit) {
4000 /* now the chunks themselves */
4001 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4002 desc.chunk_type = ch->chunk_type;
4003 /* get amount we need to move */
4004 at = ntohs(ch->chunk_length);
4005 if (at < sizeof(struct sctp_chunkhdr)) {
4006 /* corrupt chunk, maybe at the end? */
4007 SCTP_STAT_INCR(sctps_pdrpcrupt);
4010 if (trunc_len == 0) {
4011 /* we are supposed to have all of it */
4013 /* corrupt skip it */
4014 SCTP_STAT_INCR(sctps_pdrpcrupt);
4018 /* is there enough of it left ? */
4019 if (desc.chunk_type == SCTP_DATA) {
4020 if (chlen < (sizeof(struct sctp_data_chunk) +
4021 sizeof(desc.data_bytes))) {
4025 if (chlen < sizeof(struct sctp_chunkhdr)) {
4030 if (desc.chunk_type == SCTP_DATA) {
4031 /* can we get out the tsn? */
4032 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4033 SCTP_STAT_INCR(sctps_pdrpmbda);
4035 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4037 struct sctp_data_chunk *dcp;
4041 dcp = (struct sctp_data_chunk *)ch;
4042 ddp = (uint8_t *) (dcp + 1);
4043 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4044 desc.data_bytes[iii] = ddp[iii];
4046 desc.tsn_ifany = dcp->dp.tsn;
4048 /* nope we are done. */
4049 SCTP_STAT_INCR(sctps_pdrpnedat);
4053 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4054 SCTP_STAT_INCR(sctps_pdrpmbct);
4057 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4058 SCTP_STAT_INCR(sctps_pdrppdbrk);
4061 if (SCTP_SIZE32(at) > chlen) {
4064 chlen -= SCTP_SIZE32(at);
4065 if (chlen < sizeof(struct sctp_chunkhdr)) {
4066 /* done, none left */
4069 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4071 /* Now update any rwnd --- possibly */
4072 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4073 /* From a peer, we get a rwnd report */
4076 SCTP_STAT_INCR(sctps_pdrpfehos);
4078 bottle_bw = ntohl(cp->bottle_bw);
4079 on_queue = ntohl(cp->current_onq);
4080 if (bottle_bw && on_queue) {
4081 /* a rwnd report is in here */
4082 if (bottle_bw > on_queue)
4083 a_rwnd = bottle_bw - on_queue;
4088 stcb->asoc.peers_rwnd = 0;
4090 if (a_rwnd > stcb->asoc.total_flight) {
4091 stcb->asoc.peers_rwnd =
4092 a_rwnd - stcb->asoc.total_flight;
4094 stcb->asoc.peers_rwnd = 0;
4096 if (stcb->asoc.peers_rwnd <
4097 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4098 /* SWS sender side engages */
4099 stcb->asoc.peers_rwnd = 0;
4104 SCTP_STAT_INCR(sctps_pdrpfmbox);
4107 /* now middle boxes in sat networks get a cwnd bump */
4108 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4109 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4110 (stcb->asoc.sat_network)) {
4112 * This is debateable but for sat networks it makes sense
4113 * Note if a T3 timer has went off, we will prohibit any
4114 * changes to cwnd until we exit the t3 loss recovery.
4116 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4117 net, cp, &bottle_bw, &on_queue);
4122 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4123 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4124 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4125 * length of the complete packet outputs: - length: modified to remaining
4126 * length after control processing - netp: modified to new sctp_nets after
4127 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4128 * bad packet,...) otherwise return the tcb for this packet
4131 __attribute__((noinline))
4133 static struct sctp_tcb *
4134 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4135 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4136 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4137 uint32_t vrf_id, uint16_t port)
4139 struct sctp_association *asoc;
4141 int num_chunks = 0; /* number of control chunks processed */
4142 uint32_t chk_length;
4144 int abort_no_unlock = 0;
4147 * How big should this be, and should it be alloc'd? Lets try the
4148 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4149 * until we get into jumbo grams and such..
4151 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4152 struct sctp_tcb *locked_tcb = stcb;
4154 uint32_t auth_offset = 0, auth_len = 0;
4155 int auth_skipped = 0;
4158 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4163 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4164 iphlen, *offset, length, stcb);
4166 /* validate chunk header length... */
4167 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4168 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4169 ntohs(ch->chunk_length));
4171 SCTP_TCB_UNLOCK(locked_tcb);
4176 * validate the verification tag
4178 vtag_in = ntohl(sh->v_tag);
4181 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4183 if (ch->chunk_type == SCTP_INITIATION) {
4184 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4185 ntohs(ch->chunk_length), vtag_in);
4187 /* protocol error- silently discard... */
4188 SCTP_STAT_INCR(sctps_badvtag);
4190 SCTP_TCB_UNLOCK(locked_tcb);
4194 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4196 * If there is no stcb, skip the AUTH chunk and process
4197 * later after a stcb is found (to validate the lookup was
4200 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4202 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4203 /* save this chunk for later processing */
4205 auth_offset = *offset;
4206 auth_len = ntohs(ch->chunk_length);
4208 /* (temporarily) move past this chunk */
4209 *offset += SCTP_SIZE32(auth_len);
4210 if (*offset >= length) {
4211 /* no more data left in the mbuf chain */
4214 SCTP_TCB_UNLOCK(locked_tcb);
4218 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4219 sizeof(struct sctp_chunkhdr), chunk_buf);
4225 SCTP_TCB_UNLOCK(locked_tcb);
4229 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4230 goto process_control_chunks;
4233 * first check if it's an ASCONF with an unknown src addr we
4234 * need to look inside to find the association
4236 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4237 struct sctp_chunkhdr *asconf_ch = ch;
4238 uint32_t asconf_offset = 0, asconf_len = 0;
4240 /* inp's refcount may be reduced */
4241 SCTP_INP_INCR_REF(inp);
4243 asconf_offset = *offset;
4245 asconf_len = ntohs(asconf_ch->chunk_length);
4246 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4248 stcb = sctp_findassociation_ep_asconf(m, iphlen,
4249 *offset, sh, &inp, netp, vrf_id);
4252 asconf_offset += SCTP_SIZE32(asconf_len);
4253 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4254 sizeof(struct sctp_chunkhdr), chunk_buf);
4255 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4258 * reduce inp's refcount if not reduced in
4259 * sctp_findassociation_ep_asconf().
4261 SCTP_INP_DECR_REF(inp);
4266 /* now go back and verify any auth chunk to be sure */
4267 if (auth_skipped && (stcb != NULL)) {
4268 struct sctp_auth_chunk *auth;
4270 auth = (struct sctp_auth_chunk *)
4271 sctp_m_getptr(m, auth_offset,
4272 auth_len, chunk_buf);
4275 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4277 /* auth HMAC failed so dump it */
4280 SCTP_TCB_UNLOCK(locked_tcb);
4284 /* remaining chunks are HMAC checked */
4285 stcb->asoc.authenticated = 1;
4290 /* no association, so it's out of the blue... */
4291 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
4295 SCTP_TCB_UNLOCK(locked_tcb);
4300 /* ABORT and SHUTDOWN can use either v_tag... */
4301 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4302 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4303 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4304 if ((vtag_in == asoc->my_vtag) ||
4305 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4306 (vtag_in == asoc->peer_vtag))) {
4309 /* drop this packet... */
4310 SCTP_STAT_INCR(sctps_badvtag);
4312 SCTP_TCB_UNLOCK(locked_tcb);
4316 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4317 if (vtag_in != asoc->my_vtag) {
4319 * this could be a stale SHUTDOWN-ACK or the
4320 * peer never got the SHUTDOWN-COMPLETE and
4321 * is still hung; we have started a new asoc
4322 * but it won't complete until the shutdown
4326 SCTP_TCB_UNLOCK(locked_tcb);
4328 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4329 NULL, vrf_id, port);
4333 /* for all other chunks, vtag must match */
4334 if (vtag_in != asoc->my_vtag) {
4335 /* invalid vtag... */
4336 SCTPDBG(SCTP_DEBUG_INPUT3,
4337 "invalid vtag: %xh, expect %xh\n",
4338 vtag_in, asoc->my_vtag);
4339 SCTP_STAT_INCR(sctps_badvtag);
4341 SCTP_TCB_UNLOCK(locked_tcb);
4347 } /* end if !SCTP_COOKIE_ECHO */
4349 * process all control chunks...
4351 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4353 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4354 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4355 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4356 /* implied cookie-ack.. we must have lost the ack */
4357 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4358 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4359 stcb->asoc.overall_error_count,
4361 SCTP_FROM_SCTP_INPUT,
4364 stcb->asoc.overall_error_count = 0;
4365 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4368 process_control_chunks:
4369 while (IS_SCTP_CONTROL(ch)) {
4370 /* validate chunk length */
4371 chk_length = ntohs(ch->chunk_length);
4372 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4373 ch->chunk_type, chk_length);
4374 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4375 if (chk_length < sizeof(*ch) ||
4376 (*offset + (int)chk_length) > length) {
4379 SCTP_TCB_UNLOCK(locked_tcb);
4383 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4385 * INIT-ACK only gets the init ack "header" portion only
4386 * because we don't have to process the peer's COOKIE. All
4387 * others get a complete chunk.
4389 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4390 (ch->chunk_type == SCTP_INITIATION)) {
4391 /* get an init-ack chunk */
4392 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4393 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4397 SCTP_TCB_UNLOCK(locked_tcb);
4402 /* For cookies and all other chunks. */
4403 if (chk_length > sizeof(chunk_buf)) {
4405 * use just the size of the chunk buffer so
4406 * the front part of our chunks fit in
4407 * contiguous space up to the chunk buffer
4408 * size (508 bytes). For chunks that need to
4409 * get more than that they must use the
4410 * sctp_m_getptr() function or other means
4411 * (e.g. know how to parse mbuf chains).
4412 * Cookies do this already.
4414 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4415 (sizeof(chunk_buf) - 4),
4420 SCTP_TCB_UNLOCK(locked_tcb);
4425 /* We can fit it all */
4426 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4427 chk_length, chunk_buf);
4429 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4432 SCTP_TCB_UNLOCK(locked_tcb);
4439 /* Save off the last place we got a control from */
4441 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4443 * allow last_control to be NULL if
4444 * ASCONF... ASCONF processing will find the
4447 if ((netp != NULL) && (*netp != NULL))
4448 stcb->asoc.last_control_chunk_from = *netp;
4451 #ifdef SCTP_AUDITING_ENABLED
4452 sctp_audit_log(0xB0, ch->chunk_type);
4455 /* check to see if this chunk required auth, but isn't */
4456 if ((stcb != NULL) &&
4457 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4458 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4459 !stcb->asoc.authenticated) {
4460 /* "silently" ignore */
4461 SCTP_STAT_INCR(sctps_recvauthmissing);
4464 switch (ch->chunk_type) {
4465 case SCTP_INITIATION:
4466 /* must be first and only chunk */
4467 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4468 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4469 /* We are not interested anymore? */
4470 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4472 * collision case where we are
4473 * sending to them too
4478 SCTP_TCB_UNLOCK(locked_tcb);
4484 if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) ||
4486 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4489 SCTP_TCB_UNLOCK(locked_tcb);
4493 if ((stcb != NULL) &&
4494 (SCTP_GET_STATE(&stcb->asoc) ==
4495 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4496 sctp_send_shutdown_ack(stcb,
4497 stcb->asoc.primary_destination);
4499 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4501 SCTP_TCB_UNLOCK(locked_tcb);
4506 sctp_handle_init(m, iphlen, *offset, sh,
4507 (struct sctp_init_chunk *)ch, inp,
4508 stcb, *netp, &abort_no_unlock, vrf_id, port);
4510 if (abort_no_unlock)
4515 SCTP_TCB_UNLOCK(locked_tcb);
4519 case SCTP_PAD_CHUNK:
4521 case SCTP_INITIATION_ACK:
4522 /* must be first and only chunk */
4523 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4524 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4525 /* We are not interested anymore */
4526 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4530 SCTP_TCB_UNLOCK(locked_tcb);
4534 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4535 so = SCTP_INP_SO(inp);
4536 atomic_add_int(&stcb->asoc.refcnt, 1);
4537 SCTP_TCB_UNLOCK(stcb);
4538 SCTP_SOCKET_LOCK(so, 1);
4539 SCTP_TCB_LOCK(stcb);
4540 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4542 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4543 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4544 SCTP_SOCKET_UNLOCK(so, 1);
4550 if ((num_chunks > 1) ||
4551 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4554 SCTP_TCB_UNLOCK(locked_tcb);
4558 if ((netp) && (*netp)) {
4559 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4560 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4565 * Special case, I must call the output routine to
4566 * get the cookie echoed
4568 if (abort_no_unlock)
4571 if ((stcb) && ret == 0)
4572 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4575 SCTP_TCB_UNLOCK(locked_tcb);
4579 case SCTP_SELECTIVE_ACK:
4580 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4581 SCTP_STAT_INCR(sctps_recvsacks);
4583 struct sctp_sack_chunk *sack;
4585 uint32_t a_rwnd, cum_ack;
4586 uint16_t num_seg, num_dup;
4588 int offset_seg, offset_dup;
4592 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4595 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4596 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4599 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4601 * If we have sent a shutdown-ack, we will pay no
4602 * attention to a sack sent in to us since
4603 * we don't care anymore.
4607 sack = (struct sctp_sack_chunk *)ch;
4608 flags = ch->chunk_flags;
4609 nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM;
4610 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4611 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4612 num_dup = ntohs(sack->sack.num_dup_tsns);
4613 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4614 if (sizeof(struct sctp_sack_chunk) +
4615 num_seg * sizeof(struct sctp_gap_ack_block) +
4616 num_dup * sizeof(uint32_t) != chk_length) {
4617 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4620 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4621 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4622 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4623 cum_ack, num_seg, a_rwnd);
4624 stcb->asoc.seen_a_sack_this_pkt = 1;
4625 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4627 ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
4628 (cum_ack == stcb->asoc.last_acked_seq)) &&
4629 (stcb->asoc.saw_sack_with_frags == 0) &&
4630 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4633 * We have a SIMPLE sack having no
4634 * prior segments and data on sent
4635 * queue to be acked.. Use the
4636 * faster path sack processing. We
4637 * also allow window update sacks
4638 * with no missing segments to go
4641 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
4645 sctp_handle_sack(m, offset_seg, offset_dup,
4647 num_seg, 0, num_dup, &abort_now, flags,
4651 /* ABORT signal from sack processing */
4655 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4656 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4657 (stcb->asoc.stream_queue_cnt == 0)) {
4658 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4663 * EY - nr_sack: If the received chunk is an
4666 case SCTP_NR_SELECTIVE_ACK:
4667 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4668 SCTP_STAT_INCR(sctps_recvsacks);
4670 struct sctp_nr_sack_chunk *nr_sack;
4672 uint32_t a_rwnd, cum_ack;
4673 uint16_t num_seg, num_nr_seg, num_dup;
4675 int offset_seg, offset_dup;
4679 * EY nr_sacks have not been negotiated but
4680 * the peer end sent an nr_sack, silently
4683 if (!(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
4684 stcb->asoc.peer_supports_nr_sack)) {
4688 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
4691 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4692 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
4695 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4697 * If we have sent a shutdown-ack, we will pay no
4698 * attention to a sack sent in to us since
4699 * we don't care anymore.
4703 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4704 flags = ch->chunk_flags;
4705 nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM;
4707 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4708 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4709 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4710 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4711 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4712 if (sizeof(struct sctp_nr_sack_chunk) +
4713 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4714 num_dup * sizeof(uint32_t) != chk_length) {
4715 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4718 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4719 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4720 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4721 cum_ack, num_seg, a_rwnd);
4722 stcb->asoc.seen_a_sack_this_pkt = 1;
4723 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4724 (num_seg == 0) && (num_nr_seg == 0) &&
4725 ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
4726 (cum_ack == stcb->asoc.last_acked_seq)) &&
4727 (stcb->asoc.saw_sack_with_frags == 0) &&
4728 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4730 * We have a SIMPLE sack having no
4731 * prior segments and data on sent
4732 * queue to be acked. Use the faster
4733 * path sack processing. We also
4734 * allow window update sacks with no
4735 * missing segments to go this way
4738 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
4742 sctp_handle_sack(m, offset_seg, offset_dup,
4744 num_seg, num_nr_seg, num_dup, &abort_now, flags,
4748 /* ABORT signal from sack processing */
4752 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4753 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4754 (stcb->asoc.stream_queue_cnt == 0)) {
4755 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4760 case SCTP_HEARTBEAT_REQUEST:
4761 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4762 if ((stcb) && netp && *netp) {
4763 SCTP_STAT_INCR(sctps_recvheartbeat);
4764 sctp_send_heartbeat_ack(stcb, m, *offset,
4767 /* He's alive so give him credit */
4768 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4769 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4770 stcb->asoc.overall_error_count,
4772 SCTP_FROM_SCTP_INPUT,
4775 stcb->asoc.overall_error_count = 0;
4778 case SCTP_HEARTBEAT_ACK:
4779 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4780 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4784 SCTP_TCB_UNLOCK(locked_tcb);
4788 /* He's alive so give him credit */
4789 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4790 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4791 stcb->asoc.overall_error_count,
4793 SCTP_FROM_SCTP_INPUT,
4796 stcb->asoc.overall_error_count = 0;
4797 SCTP_STAT_INCR(sctps_recvheartbeatack);
4799 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4802 case SCTP_ABORT_ASSOCIATION:
4803 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4805 if ((stcb) && netp && *netp)
4806 sctp_handle_abort((struct sctp_abort_chunk *)ch,
4812 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4814 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
4817 SCTP_TCB_UNLOCK(locked_tcb);
4821 if (netp && *netp) {
4824 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4825 stcb, *netp, &abort_flag);
4832 case SCTP_SHUTDOWN_ACK:
4833 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
4834 if ((stcb) && (netp) && (*netp))
4835 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4840 case SCTP_OPERATION_ERROR:
4841 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
4842 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
4848 case SCTP_COOKIE_ECHO:
4849 SCTPDBG(SCTP_DEBUG_INPUT3,
4850 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
4851 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4854 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4855 /* We are not interested anymore */
4861 * First are we accepting? We do this again here
4862 * sincen it is possible that a previous endpoint
4863 * WAS listening responded to a INIT-ACK and then
4864 * closed. We opened and bound.. and are now no
4868 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
4869 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4870 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4872 struct sctp_paramhdr *phdr;
4874 oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4875 0, M_DONTWAIT, 1, MT_DATA);
4877 SCTP_BUF_LEN(oper) =
4878 sizeof(struct sctp_paramhdr);
4880 struct sctp_paramhdr *);
4882 htons(SCTP_CAUSE_OUT_OF_RESC);
4883 phdr->param_length =
4884 htons(sizeof(struct sctp_paramhdr));
4886 sctp_abort_association(inp, stcb, m,
4887 iphlen, sh, oper, vrf_id, port);
4892 struct mbuf *ret_buf;
4893 struct sctp_inpcb *linp;
4902 SCTP_ASOC_CREATE_LOCK(linp);
4906 sctp_handle_cookie_echo(m, iphlen,
4908 (struct sctp_cookie_echo_chunk *)ch,
4920 SCTP_ASOC_CREATE_UNLOCK(linp);
4922 if (ret_buf == NULL) {
4924 SCTP_TCB_UNLOCK(locked_tcb);
4926 SCTPDBG(SCTP_DEBUG_INPUT3,
4927 "GAK, null buffer\n");
4932 /* if AUTH skipped, see if it verified... */
4937 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4939 * Restart the timer if we have
4942 struct sctp_tmit_chunk *chk;
4944 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
4946 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4947 stcb->sctp_ep, stcb,
4953 case SCTP_COOKIE_ACK:
4954 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
4955 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
4957 SCTP_TCB_UNLOCK(locked_tcb);
4961 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4962 /* We are not interested anymore */
4963 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4966 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4967 so = SCTP_INP_SO(inp);
4968 atomic_add_int(&stcb->asoc.refcnt, 1);
4969 SCTP_TCB_UNLOCK(stcb);
4970 SCTP_SOCKET_LOCK(so, 1);
4971 SCTP_TCB_LOCK(stcb);
4972 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4974 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4975 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4976 SCTP_SOCKET_UNLOCK(so, 1);
4982 /* He's alive so give him credit */
4983 if ((stcb) && netp && *netp) {
4984 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4985 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4986 stcb->asoc.overall_error_count,
4988 SCTP_FROM_SCTP_INPUT,
4991 stcb->asoc.overall_error_count = 0;
4992 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
4996 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
4997 /* He's alive so give him credit */
4998 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5001 SCTP_TCB_UNLOCK(locked_tcb);
5007 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5008 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5009 stcb->asoc.overall_error_count,
5011 SCTP_FROM_SCTP_INPUT,
5014 stcb->asoc.overall_error_count = 0;
5015 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5020 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5021 /* He's alive so give him credit */
5022 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5025 SCTP_TCB_UNLOCK(locked_tcb);
5031 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5032 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5033 stcb->asoc.overall_error_count,
5035 SCTP_FROM_SCTP_INPUT,
5038 stcb->asoc.overall_error_count = 0;
5039 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
5042 case SCTP_SHUTDOWN_COMPLETE:
5043 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5044 /* must be first and only chunk */
5045 if ((num_chunks > 1) ||
5046 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5049 SCTP_TCB_UNLOCK(locked_tcb);
5053 if ((stcb) && netp && *netp) {
5054 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5061 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5062 /* He's alive so give him credit */
5064 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5065 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5066 stcb->asoc.overall_error_count,
5068 SCTP_FROM_SCTP_INPUT,
5071 stcb->asoc.overall_error_count = 0;
5072 sctp_handle_asconf(m, *offset,
5073 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5077 case SCTP_ASCONF_ACK:
5078 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5079 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5082 SCTP_TCB_UNLOCK(locked_tcb);
5087 if ((stcb) && netp && *netp) {
5088 /* He's alive so give him credit */
5089 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5090 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5091 stcb->asoc.overall_error_count,
5093 SCTP_FROM_SCTP_INPUT,
5096 stcb->asoc.overall_error_count = 0;
5097 sctp_handle_asconf_ack(m, *offset,
5098 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5099 if (abort_no_unlock)
5103 case SCTP_FORWARD_CUM_TSN:
5104 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5105 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5108 SCTP_TCB_UNLOCK(locked_tcb);
5113 /* He's alive so give him credit */
5117 stcb->asoc.overall_error_count = 0;
5118 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5119 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5120 stcb->asoc.overall_error_count,
5122 SCTP_FROM_SCTP_INPUT,
5126 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5127 /* We are not interested anymore */
5128 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5129 so = SCTP_INP_SO(inp);
5130 atomic_add_int(&stcb->asoc.refcnt, 1);
5131 SCTP_TCB_UNLOCK(stcb);
5132 SCTP_SOCKET_LOCK(so, 1);
5133 SCTP_TCB_LOCK(stcb);
5134 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5136 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5137 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5138 SCTP_SOCKET_UNLOCK(so, 1);
5143 sctp_handle_forward_tsn(stcb,
5144 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5149 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5150 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5151 stcb->asoc.overall_error_count,
5153 SCTP_FROM_SCTP_INPUT,
5156 stcb->asoc.overall_error_count = 0;
5161 case SCTP_STREAM_RESET:
5162 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5163 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5166 SCTP_TCB_UNLOCK(locked_tcb);
5171 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5172 /* We are not interested anymore */
5173 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5174 so = SCTP_INP_SO(inp);
5175 atomic_add_int(&stcb->asoc.refcnt, 1);
5176 SCTP_TCB_UNLOCK(stcb);
5177 SCTP_SOCKET_LOCK(so, 1);
5178 SCTP_TCB_LOCK(stcb);
5179 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5181 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5182 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5183 SCTP_SOCKET_UNLOCK(so, 1);
5188 if (stcb->asoc.peer_supports_strreset == 0) {
5190 * hmm, peer should have announced this, but
5191 * we will turn it on since he is sending us
5194 stcb->asoc.peer_supports_strreset = 1;
5196 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5197 /* stop processing */
5202 case SCTP_PACKET_DROPPED:
5203 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5204 /* re-get it all please */
5205 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5208 SCTP_TCB_UNLOCK(locked_tcb);
5213 if (ch && (stcb) && netp && (*netp)) {
5214 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5216 min(chk_length, (sizeof(chunk_buf) - 4)));
5221 case SCTP_AUTHENTICATION:
5222 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5223 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5227 /* save the first AUTH for later processing */
5228 if (auth_skipped == 0) {
5229 auth_offset = *offset;
5230 auth_len = chk_length;
5233 /* skip this chunk (temporarily) */
5236 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5237 (chk_length > (sizeof(struct sctp_auth_chunk) +
5238 SCTP_AUTH_DIGEST_LEN_MAX))) {
5241 SCTP_TCB_UNLOCK(locked_tcb);
5246 if (got_auth == 1) {
5247 /* skip this chunk... it's already auth'd */
5251 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5253 /* auth HMAC failed so dump the packet */
5257 /* remaining chunks are HMAC checked */
5258 stcb->asoc.authenticated = 1;
5264 /* it's an unknown chunk! */
5265 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5267 struct sctp_paramhdr *phd;
5269 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5270 0, M_DONTWAIT, 1, MT_DATA);
5272 phd = mtod(mm, struct sctp_paramhdr *);
5274 * We cheat and use param type since
5275 * we did not bother to define a
5276 * error cause struct. They are the
5277 * same basic format with different
5280 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5281 phd->param_length = htons(chk_length + sizeof(*phd));
5282 SCTP_BUF_LEN(mm) = sizeof(*phd);
5283 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
5285 if (SCTP_BUF_NEXT(mm)) {
5286 #ifdef SCTP_MBUF_LOGGING
5287 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5290 mat = SCTP_BUF_NEXT(mm);
5292 if (SCTP_BUF_IS_EXTENDED(mat)) {
5293 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5295 mat = SCTP_BUF_NEXT(mat);
5299 sctp_queue_op_err(stcb, mm);
5305 if ((ch->chunk_type & 0x80) == 0) {
5306 /* discard this packet */
5309 } /* else skip this bad chunk and continue... */
5311 } /* switch (ch->chunk_type) */
5315 /* get the next chunk */
5316 *offset += SCTP_SIZE32(chk_length);
5317 if (*offset >= length) {
5318 /* no more data left in the mbuf chain */
5321 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5322 sizeof(struct sctp_chunkhdr), chunk_buf);
5325 SCTP_TCB_UNLOCK(locked_tcb);
5332 if (asconf_cnt > 0 && stcb != NULL) {
5333 sctp_send_asconf_ack(stcb);
5340 * Process the ECN bits we have something set so we must look to see if it is
5341 * ECN(0) or ECN(1) or CE
5344 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
5347 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
5349 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
5351 * we only add to the nonce sum for ECT1, ECT0 does not
5352 * change the NS bit (that we have yet to find a way to send
5356 /* ECN Nonce stuff */
5357 stcb->asoc.receiver_nonce_sum++;
5358 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
5361 * Drag up the last_echo point if cumack is larger since we
5362 * don't want the point falling way behind by more than
5363 * 2^^31 and then having it be incorrect.
5365 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
5366 stcb->asoc.last_echo_tsn, MAX_TSN)) {
5367 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
5369 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
5371 * Drag up the last_echo point if cumack is larger since we
5372 * don't want the point falling way behind by more than
5373 * 2^^31 and then having it be incorrect.
5375 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
5376 stcb->asoc.last_echo_tsn, MAX_TSN)) {
5377 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
5383 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
5384 uint32_t high_tsn, uint8_t ecn_bits)
5386 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
5388 * we possibly must notify the sender that a congestion
5389 * window reduction is in order. We do this by adding a ECNE
5390 * chunk to the output chunk queue. The incoming CWR will
5391 * remove this chunk.
5393 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
5395 /* Yep, we need to add a ECNE */
5396 sctp_send_ecn_echo(stcb, net, high_tsn);
5397 stcb->asoc.last_echo_tsn = high_tsn;
5404 sctp_validate_no_locks(struct sctp_inpcb *inp)
5406 struct sctp_tcb *stcb;
5408 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5409 if (mtx_owned(&stcb->tcb_mtx)) {
5410 panic("Own lock on stcb at return from input");
5418 * common input chunk processing (v4 and v6)
5421 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5422 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5423 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5424 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5427 * Control chunk processing
5430 int fwd_tsn_seen = 0, data_processed = 0;
5431 struct mbuf *m = *mm;
5435 SCTP_STAT_INCR(sctps_recvdatagrams);
5436 #ifdef SCTP_AUDITING_ENABLED
5437 sctp_audit_log(0xE0, 1);
5438 sctp_auditing(0, inp, stcb, net);
5441 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5442 m, iphlen, offset, length, stcb);
5444 /* always clear this before beginning a packet */
5445 stcb->asoc.authenticated = 0;
5446 stcb->asoc.seen_a_sack_this_pkt = 0;
5447 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5448 stcb, stcb->asoc.state);
5450 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5451 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5453 * If we hit here, we had a ref count
5454 * up when the assoc was aborted and the
5455 * timer is clearing out the assoc, we should
5456 * NOT respond to any packet.. its OOTB.
5458 SCTP_TCB_UNLOCK(stcb);
5459 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5464 if (IS_SCTP_CONTROL(ch)) {
5465 /* process the control portion of the SCTP packet */
5466 /* sa_ignore NO_NULL_CHK */
5467 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5468 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5471 * This covers us if the cookie-echo was there and
5472 * it changes our INP.
5474 inp = stcb->sctp_ep;
5475 if ((net) && (port)) {
5476 if (net->port == 0) {
5477 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5484 * no control chunks, so pre-process DATA chunks (these
5485 * checks are taken care of by control processing)
5489 * if DATA only packet, and auth is required, then punt...
5490 * can't have authenticated without any AUTH (control)
5493 if ((stcb != NULL) &&
5494 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5495 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5496 /* "silently" ignore */
5497 SCTP_STAT_INCR(sctps_recvauthmissing);
5498 SCTP_TCB_UNLOCK(stcb);
5502 /* out of the blue DATA chunk */
5503 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5507 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5508 /* v_tag mismatch! */
5509 SCTP_STAT_INCR(sctps_badvtag);
5510 SCTP_TCB_UNLOCK(stcb);
5517 * no valid TCB for this packet, or we found it's a bad
5518 * packet while processing control, or we're done with this
5519 * packet (done or skip rest of data), so we drop it...
5524 * DATA chunk processing
5526 /* plow through the data chunks while length > offset */
5529 * Rest should be DATA only. Check authentication state if AUTH for
5532 if ((length > offset) &&
5534 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5535 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5536 !stcb->asoc.authenticated) {
5537 /* "silently" ignore */
5538 SCTP_STAT_INCR(sctps_recvauthmissing);
5539 SCTPDBG(SCTP_DEBUG_AUTH1,
5540 "Data chunk requires AUTH, skipped\n");
5543 if (length > offset) {
5547 * First check to make sure our state is correct. We would
5548 * not get here unless we really did have a tag, so we don't
5549 * abort if this happens, just dump the chunk silently.
5551 switch (SCTP_GET_STATE(&stcb->asoc)) {
5552 case SCTP_STATE_COOKIE_ECHOED:
5554 * we consider data with valid tags in this state
5555 * shows us the cookie-ack was lost. Imply it was
5558 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5559 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5560 stcb->asoc.overall_error_count,
5562 SCTP_FROM_SCTP_INPUT,
5565 stcb->asoc.overall_error_count = 0;
5566 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5568 case SCTP_STATE_COOKIE_WAIT:
5570 * We consider OOTB any data sent during asoc setup.
5572 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5574 SCTP_TCB_UNLOCK(stcb);
5576 /* sa_ignore NOTREACHED */
5578 case SCTP_STATE_EMPTY: /* should not happen */
5579 case SCTP_STATE_INUSE: /* should not happen */
5580 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5581 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5583 SCTP_TCB_UNLOCK(stcb);
5585 /* sa_ignore NOTREACHED */
5587 case SCTP_STATE_OPEN:
5588 case SCTP_STATE_SHUTDOWN_SENT:
5591 /* take care of ECN, part 1. */
5592 if (stcb->asoc.ecn_allowed &&
5593 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5594 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
5596 /* plow through the data chunks while length > offset */
5597 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5598 inp, stcb, net, &high_tsn);
5601 * The association aborted, NO UNLOCK needed since
5602 * the association is destroyed.
5608 /* take care of ecn part 2. */
5609 if (stcb->asoc.ecn_allowed &&
5610 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5611 sctp_process_ecn_marked_b(stcb, net, high_tsn,
5616 * Anything important needs to have been m_copy'ed in
5620 if ((data_processed == 0) && (fwd_tsn_seen)) {
5623 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
5624 stcb->asoc.cumulative_tsn, MAX_TSN)) {
5625 /* there was a gap before this data was processed */
5628 stcb->asoc.send_sack = 1;
5629 sctp_sack_check(stcb, was_a_gap, &abort_flag);
5631 /* Again, we aborted so NO UNLOCK needed */
5634 } else if (fwd_tsn_seen) {
5635 stcb->asoc.send_sack = 1;
5637 /* trigger send of any chunks in queue... */
5639 #ifdef SCTP_AUDITING_ENABLED
5640 sctp_audit_log(0xE0, 2);
5641 sctp_auditing(1, inp, stcb, net);
5643 SCTPDBG(SCTP_DEBUG_INPUT1,
5644 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5645 stcb->asoc.peers_rwnd,
5646 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5647 stcb->asoc.total_flight);
5648 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5650 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
5652 (stcb->asoc.peers_rwnd > 0 ||
5653 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5654 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5655 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5656 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5658 #ifdef SCTP_AUDITING_ENABLED
5659 sctp_audit_log(0xE0, 3);
5660 sctp_auditing(2, inp, stcb, net);
5662 SCTP_TCB_UNLOCK(stcb);
5665 sctp_validate_no_locks(inp);
5672 sctp_print_mbuf_chain(struct mbuf *m)
5674 for (; m; m = SCTP_BUF_NEXT(m)) {
5675 printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5676 if (SCTP_BUF_IS_EXTENDED(m))
5677 printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5684 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5686 #ifdef SCTP_MBUF_LOGGING
5692 uint32_t vrf_id = 0;
5696 struct sctp_inpcb *inp = NULL;
5698 uint32_t check, calc_check;
5699 struct sctp_nets *net;
5700 struct sctp_tcb *stcb = NULL;
5701 struct sctp_chunkhdr *ch;
5702 int refcount_up = 0;
5703 int length, mlen, offset;
5705 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5706 SCTP_RELEASE_PKT(i_pak);
5709 mlen = SCTP_HEADER_LEN(i_pak);
5711 m = SCTP_HEADER_TO_CHAIN(i_pak);
5714 SCTP_STAT_INCR(sctps_recvpackets);
5715 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5718 #ifdef SCTP_MBUF_LOGGING
5719 /* Log in any input mbufs */
5720 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5723 if (SCTP_BUF_IS_EXTENDED(mat)) {
5724 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5726 mat = SCTP_BUF_NEXT(mat);
5730 #ifdef SCTP_PACKET_LOGGING
5731 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5732 sctp_packet_log(m, mlen);
5735 * Must take out the iphlen, since mlen expects this (only effect lb
5741 * Get IP, SCTP, and first chunk header together in first mbuf.
5743 ip = mtod(m, struct ip *);
5744 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5745 if (SCTP_BUF_LEN(m) < offset) {
5746 if ((m = m_pullup(m, offset)) == 0) {
5747 SCTP_STAT_INCR(sctps_hdrops);
5750 ip = mtod(m, struct ip *);
5752 /* validate mbuf chain length with IP payload length */
5753 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5754 SCTP_STAT_INCR(sctps_hdrops);
5757 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5758 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5759 SCTPDBG(SCTP_DEBUG_INPUT1,
5760 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5762 /* SCTP does not allow broadcasts or multicasts */
5763 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5766 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5768 * We only look at broadcast if its a front state, All
5769 * others we will not have a tcb for anyway.
5773 /* validate SCTP checksum */
5774 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5775 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5777 if_name(m->m_pkthdr.rcvif),
5778 m->m_pkthdr.csum_flags);
5779 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5780 SCTP_STAT_INCR(sctps_recvhwcrc);
5781 goto sctp_skip_csum_4;
5783 check = sh->checksum; /* save incoming checksum */
5784 if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
5785 ((ip->ip_src.s_addr == ip->ip_dst.s_addr) ||
5786 (SCTP_IS_IT_LOOPBACK(m)))
5788 SCTP_STAT_INCR(sctps_recvnocrc);
5789 goto sctp_skip_csum_4;
5791 sh->checksum = 0; /* prepare for calc */
5792 calc_check = sctp_calculate_cksum(m, iphlen);
5793 sh->checksum = check;
5794 SCTP_STAT_INCR(sctps_recvswcrc);
5795 if (calc_check != check) {
5796 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5797 calc_check, check, m, mlen, iphlen);
5799 stcb = sctp_findassociation_addr(m, iphlen,
5800 offset - sizeof(*ch),
5803 if ((net) && (port)) {
5804 if (net->port == 0) {
5805 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5809 if ((inp) && (stcb)) {
5810 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5811 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5812 } else if ((inp != NULL) && (stcb == NULL)) {
5815 SCTP_STAT_INCR(sctps_badsum);
5816 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5820 /* destination port of 0 is illegal, based on RFC2960. */
5821 if (sh->dest_port == 0) {
5822 SCTP_STAT_INCR(sctps_hdrops);
5826 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5827 * IP/SCTP/first chunk header...
5829 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
5830 sh, ch, &inp, &net, vrf_id);
5831 if ((net) && (port)) {
5832 if (net->port == 0) {
5833 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5837 /* inp's ref-count increased && stcb locked */
5839 struct sctp_init_chunk *init_chk, chunk_buf;
5841 SCTP_STAT_INCR(sctps_noport);
5844 * we use the bandwidth limiting to protect against sending
5845 * too many ABORTS all at once. In this case these count the
5846 * same as an ICMP message.
5848 if (badport_bandlim(0) < 0)
5850 #endif /* ICMP_BANDLIM */
5851 SCTPDBG(SCTP_DEBUG_INPUT1,
5852 "Sending a ABORT from packet entry!\n");
5853 if (ch->chunk_type == SCTP_INITIATION) {
5855 * we do a trick here to get the INIT tag, dig in
5856 * and get the tag from the INIT and put it in the
5859 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5860 iphlen + sizeof(*sh), sizeof(*init_chk),
5861 (uint8_t *) & chunk_buf);
5862 if (init_chk != NULL)
5863 sh->v_tag = init_chk->init.initiate_tag;
5865 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5866 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
5869 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5872 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
5873 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
5875 } else if (stcb == NULL) {
5880 * I very much doubt any of the IPSEC stuff will work but I have no
5881 * idea, so I will leave it in place.
5883 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5884 MODULE_GLOBAL(ipsec4stat).in_polvio++;
5885 SCTP_STAT_INCR(sctps_hdrops);
5891 * common chunk processing
5893 length = ip->ip_len + iphlen;
5894 offset -= sizeof(struct sctp_chunkhdr);
5896 ecn_bits = ip->ip_tos;
5898 /* sa_ignore NO_NULL_CHK */
5899 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
5900 inp, stcb, net, ecn_bits, vrf_id, port);
5901 /* inp's ref-count reduced && stcb unlocked */
5905 if ((inp) && (refcount_up)) {
5906 /* reduce ref-count */
5907 SCTP_INP_DECR_REF(inp);
5912 SCTP_TCB_UNLOCK(stcb);
5914 if ((inp) && (refcount_up)) {
5915 /* reduce ref-count */
5916 SCTP_INP_DECR_REF(inp);
5924 sctp_input(i_pak, off)
5928 sctp_input_with_port(i_pak, off, 0);