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;
282 /* cut back on number of streams */
283 newcnt = ntohs(init->num_inbound_streams);
284 /* This if is probably not needed but I am cautious */
286 /* First make sure no data chunks are trapped */
287 for (i = newcnt; i < asoc->pre_open_streams; i++) {
288 outs = &asoc->strmout[i];
289 sp = TAILQ_FIRST(&outs->outqueue);
291 TAILQ_REMOVE(&outs->outqueue, sp,
293 asoc->stream_queue_cnt--;
294 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
295 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
296 sp, SCTP_SO_NOT_LOCKED);
298 sctp_m_freem(sp->data);
301 sctp_free_remote_addr(sp->net);
304 SCTP_PRINTF("sp:%p tcb:%p weird free case\n",
307 sctp_free_a_strmoq(stcb, sp);
308 /* sa_ignore FREED_MEMORY */
309 sp = TAILQ_FIRST(&outs->outqueue);
313 /* cut back the count and abandon the upper streams */
314 asoc->pre_open_streams = newcnt;
316 SCTP_TCB_SEND_UNLOCK(stcb);
317 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
319 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
320 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
321 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
322 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
323 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
325 /* This is the next one we expect */
326 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
328 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
330 * EY 05/13/08 - nr_sack: initialize nr_mapping array's base tsn
333 asoc->nr_mapping_array_base_tsn = ntohl(init->initial_tsn);
334 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
335 asoc->last_echo_tsn = asoc->asconf_seq_in;
336 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
337 /* open the requested streams */
339 if (asoc->strmin != NULL) {
340 /* Free the old ones */
341 struct sctp_queued_to_read *ctl;
343 for (i = 0; i < asoc->streamincnt; i++) {
344 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
346 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
347 sctp_free_remote_addr(ctl->whoFrom);
349 sctp_m_freem(ctl->data);
351 sctp_free_a_readq(stcb, ctl);
352 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
355 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
357 asoc->streamincnt = ntohs(init->num_outbound_streams);
358 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
359 asoc->streamincnt = MAX_SCTP_STREAMS;
361 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
362 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
363 if (asoc->strmin == NULL) {
364 /* we didn't get memory for the streams! */
365 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
368 for (i = 0; i < asoc->streamincnt; i++) {
369 asoc->strmin[i].stream_no = i;
370 asoc->strmin[i].last_sequence_delivered = 0xffff;
372 * U-stream ranges will be set when the cookie is unpacked.
373 * Or for the INIT sender they are un set (if pr-sctp not
374 * supported) when the INIT-ACK arrives.
376 TAILQ_INIT(&asoc->strmin[i].inqueue);
377 asoc->strmin[i].delivery_started = 0;
380 * load_address_from_init will put the addresses into the
381 * association when the COOKIE is processed or the INIT-ACK is
382 * processed. Both types of COOKIE's existing and new call this
383 * routine. It will remove addresses that are no longer in the
384 * association (for the restarting case where addresses are
385 * removed). Up front when the INIT arrives we will discard it if it
386 * is a restart and new addresses have been added.
388 /* sa_ignore MEMLEAK */
393 * INIT-ACK message processing/consumption returns value < 0 on error
396 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
397 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
398 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
400 struct sctp_association *asoc;
402 int retval, abort_flag;
403 uint32_t initack_limit;
404 int nat_friendly = 0;
406 /* First verify that we have no illegal param's */
410 op_err = sctp_arethere_unrecognized_parameters(m,
411 (offset + sizeof(struct sctp_init_chunk)),
412 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
414 /* Send an abort and notify peer */
415 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED);
416 *abort_no_unlock = 1;
420 asoc->peer_supports_nat = (uint8_t) nat_friendly;
421 /* process the peer's parameters in the INIT-ACK */
422 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
426 initack_limit = offset + ntohs(cp->ch.chunk_length);
427 /* load all addresses */
428 if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen,
429 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
431 /* Huh, we should abort */
432 SCTPDBG(SCTP_DEBUG_INPUT1,
433 "Load addresses from INIT causes an abort %d\n",
435 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
437 *abort_no_unlock = 1;
440 /* if the peer doesn't support asconf, flush the asconf queue */
441 if (asoc->peer_supports_asconf == 0) {
442 struct sctp_asconf_addr *aparam;
444 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
445 /* sa_ignore FREED_MEMORY */
446 aparam = TAILQ_FIRST(&asoc->asconf_queue);
447 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
448 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
451 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
452 stcb->asoc.local_hmacs);
454 sctp_queue_op_err(stcb, op_err);
455 /* queuing will steal away the mbuf chain to the out queue */
458 /* extract the cookie and queue it to "echo" it back... */
459 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
460 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
461 stcb->asoc.overall_error_count,
463 SCTP_FROM_SCTP_INPUT,
466 stcb->asoc.overall_error_count = 0;
467 net->error_count = 0;
470 * Cancel the INIT timer, We do this first before queueing the
471 * cookie. We always cancel at the primary to assue that we are
472 * canceling the timer started by the INIT which always goes to the
475 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
476 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
478 /* calculate the RTO */
479 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy);
481 retval = sctp_send_cookie_echo(m, offset, stcb, net);
484 * No cookie, we probably should send a op error. But in any
485 * case if there is no cookie in the INIT-ACK, we can
486 * abandon the peer, its broke.
489 /* We abort with an error of missing mandatory param */
491 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
494 * Expand beyond to include the mandatory
497 struct sctp_inv_mandatory_param *mp;
499 SCTP_BUF_LEN(op_err) =
500 sizeof(struct sctp_inv_mandatory_param);
502 struct sctp_inv_mandatory_param *);
503 /* Subtract the reserved param */
505 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
506 mp->num_param = htonl(1);
507 mp->param = htons(SCTP_STATE_COOKIE);
510 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
511 sh, op_err, 0, net->port);
512 *abort_no_unlock = 1;
520 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
521 struct sctp_tcb *stcb, struct sctp_nets *net)
523 struct sockaddr_storage store;
524 struct sockaddr_in *sin;
525 struct sockaddr_in6 *sin6;
526 struct sctp_nets *r_net;
530 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
534 sin = (struct sockaddr_in *)&store;
535 sin6 = (struct sockaddr_in6 *)&store;
537 memset(&store, 0, sizeof(store));
538 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
539 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
540 sin->sin_family = cp->heartbeat.hb_info.addr_family;
541 sin->sin_len = cp->heartbeat.hb_info.addr_len;
542 sin->sin_port = stcb->rport;
543 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
544 sizeof(sin->sin_addr));
545 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
546 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
547 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
548 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
549 sin6->sin6_port = stcb->rport;
550 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
551 sizeof(sin6->sin6_addr));
555 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
557 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
560 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
561 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
562 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
564 * If the its a HB and it's random value is correct when can
565 * confirm the destination.
567 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
568 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
569 stcb->asoc.primary_destination = r_net;
570 r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
571 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
572 r_net = TAILQ_FIRST(&stcb->asoc.nets);
573 if (r_net != stcb->asoc.primary_destination) {
575 * first one on the list is NOT the primary
576 * sctp_cmpaddr() is much more efficent if
577 * the primary is the first on the list,
580 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
581 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
585 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
586 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
588 r_net->error_count = 0;
589 r_net->hb_responded = 1;
590 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
591 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
592 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
593 r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
594 r_net->dest_state |= SCTP_ADDR_REACHABLE;
595 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
596 SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED);
597 /* now was it the primary? if so restore */
598 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
599 (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
603 * JRS 5/14/07 - If CMT PF is on and the destination is in PF state,
604 * set the destination to active state and set the cwnd to one or
605 * two MTU's based on whether PF1 or PF2 is being used. If a T3
606 * timer is running, for the destination, stop the timer because a
607 * PF-heartbeat was received.
609 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
610 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
611 (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) {
612 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
613 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
615 SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
617 net->dest_state &= ~SCTP_ADDR_PF;
618 net->cwnd = net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf);
619 SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n",
622 /* Now lets do a RTO with this */
623 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy);
624 /* Mobility adaptation */
626 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
627 SCTP_MOBILITY_BASE) ||
628 sctp_is_mobility_feature_on(stcb->sctp_ep,
629 SCTP_MOBILITY_FASTHANDOFF)) &&
630 sctp_is_mobility_feature_on(stcb->sctp_ep,
631 SCTP_MOBILITY_PRIM_DELETED)) {
633 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
634 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
635 SCTP_MOBILITY_FASTHANDOFF)) {
636 sctp_assoc_immediate_retrans(stcb,
637 stcb->asoc.primary_destination);
639 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
640 SCTP_MOBILITY_BASE)) {
641 sctp_move_chunks_from_deleted_prim(stcb,
642 stcb->asoc.primary_destination);
644 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
645 stcb->asoc.deleted_primary);
651 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
654 * return 0 means we want you to proceed with the abort non-zero
655 * means no abort processing
657 struct sctpasochead *head;
659 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
660 /* generate a new vtag and send init */
661 LIST_REMOVE(stcb, sctp_asocs);
662 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
663 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
665 * put it in the bucket in the vtag hash of assoc's for the
668 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
669 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
672 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
674 * treat like a case where the cookie expired i.e.: - dump
675 * current cookie. - generate a new vtag. - resend init.
677 /* generate a new vtag and send init */
678 LIST_REMOVE(stcb, sctp_asocs);
679 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
680 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
681 sctp_stop_all_cookie_timers(stcb);
682 sctp_toss_old_cookies(stcb, &stcb->asoc);
683 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
684 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
686 * put it in the bucket in the vtag hash of assoc's for the
689 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
690 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
697 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
698 struct sctp_nets *net)
701 * return 0 means we want you to proceed with the abort non-zero
702 * means no abort processing
704 if (stcb->asoc.peer_supports_auth == 0) {
705 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
708 sctp_asconf_send_nat_state_update(stcb, net);
714 sctp_handle_abort(struct sctp_abort_chunk *cp,
715 struct sctp_tcb *stcb, struct sctp_nets *net)
717 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
723 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
727 len = ntohs(cp->ch.chunk_length);
728 if (len > sizeof(struct sctp_chunkhdr)) {
730 * Need to check the cause codes for our two magic nat
731 * aborts which don't kill the assoc necessarily.
733 struct sctp_abort_chunk *cpnext;
734 struct sctp_missing_nat_state *natc;
739 natc = (struct sctp_missing_nat_state *)cpnext;
740 cause = ntohs(natc->cause);
741 if (cause == SCTP_CAUSE_NAT_COLLIDING_STATE) {
742 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
744 if (sctp_handle_nat_colliding_state(stcb)) {
747 } else if (cause == SCTP_CAUSE_NAT_MISSING_STATE) {
748 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
750 if (sctp_handle_nat_missing_state(stcb, net)) {
755 /* stop any receive timers */
756 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
757 /* notify user of the abort and clean up... */
758 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
760 #if defined(SCTP_PANIC_ON_ABORT)
761 printf("stcb:%p state:%d rport:%d net:%p\n",
762 stcb, stcb->asoc.state, stcb->rport, net);
763 if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
764 panic("Received an ABORT");
766 printf("No panic its in state %x closed\n", stcb->asoc.state);
769 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
770 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
771 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
772 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
774 #ifdef SCTP_ASOCLOG_OF_TSNS
775 sctp_print_out_track_log(stcb);
777 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
778 so = SCTP_INP_SO(stcb->sctp_ep);
779 atomic_add_int(&stcb->asoc.refcnt, 1);
780 SCTP_TCB_UNLOCK(stcb);
781 SCTP_SOCKET_LOCK(so, 1);
783 atomic_subtract_int(&stcb->asoc.refcnt, 1);
785 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
786 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
787 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
788 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
789 SCTP_SOCKET_UNLOCK(so, 1);
791 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
795 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
796 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
798 struct sctp_association *asoc;
799 int some_on_streamwheel;
801 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
806 SCTPDBG(SCTP_DEBUG_INPUT2,
807 "sctp_handle_shutdown: handling SHUTDOWN\n");
811 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
812 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
815 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
816 /* Shutdown NOT the expected size */
819 sctp_update_acked(stcb, cp, net, abort_flag);
821 if (asoc->control_pdapi) {
823 * With a normal shutdown we assume the end of last record.
825 SCTP_INP_READ_LOCK(stcb->sctp_ep);
826 asoc->control_pdapi->end_added = 1;
827 asoc->control_pdapi->pdapi_aborted = 1;
828 asoc->control_pdapi = NULL;
829 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
830 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
831 so = SCTP_INP_SO(stcb->sctp_ep);
832 atomic_add_int(&stcb->asoc.refcnt, 1);
833 SCTP_TCB_UNLOCK(stcb);
834 SCTP_SOCKET_LOCK(so, 1);
836 atomic_subtract_int(&stcb->asoc.refcnt, 1);
837 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
838 /* assoc was freed while we were unlocked */
839 SCTP_SOCKET_UNLOCK(so, 1);
843 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
844 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
845 SCTP_SOCKET_UNLOCK(so, 1);
848 /* goto SHUTDOWN_RECEIVED state to block new requests */
849 if (stcb->sctp_socket) {
850 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
851 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
852 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
853 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
854 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
856 * notify upper layer that peer has initiated a
859 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
862 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
865 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
867 * stop the shutdown timer, since we WILL move to
870 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
872 /* Now is there unsent data on a stream somewhere? */
873 some_on_streamwheel = sctp_is_there_unsent_data(stcb);
875 if (!TAILQ_EMPTY(&asoc->send_queue) ||
876 !TAILQ_EMPTY(&asoc->sent_queue) ||
877 some_on_streamwheel) {
878 /* By returning we will push more data out */
881 /* no outstanding data to send, so move on... */
882 /* send SHUTDOWN-ACK */
883 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
884 /* move to SHUTDOWN-ACK-SENT state */
885 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
886 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
887 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
889 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
890 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
891 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
892 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
893 /* start SHUTDOWN timer */
894 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
900 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
901 struct sctp_tcb *stcb, struct sctp_nets *net)
903 struct sctp_association *asoc;
905 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
908 so = SCTP_INP_SO(stcb->sctp_ep);
910 SCTPDBG(SCTP_DEBUG_INPUT2,
911 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
916 /* process according to association state */
917 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
918 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
919 /* unexpected SHUTDOWN-ACK... so ignore... */
920 SCTP_TCB_UNLOCK(stcb);
923 if (asoc->control_pdapi) {
925 * With a normal shutdown we assume the end of last record.
927 SCTP_INP_READ_LOCK(stcb->sctp_ep);
928 asoc->control_pdapi->end_added = 1;
929 asoc->control_pdapi->pdapi_aborted = 1;
930 asoc->control_pdapi = NULL;
931 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
932 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
933 atomic_add_int(&stcb->asoc.refcnt, 1);
934 SCTP_TCB_UNLOCK(stcb);
935 SCTP_SOCKET_LOCK(so, 1);
937 atomic_subtract_int(&stcb->asoc.refcnt, 1);
938 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
939 /* assoc was freed while we were unlocked */
940 SCTP_SOCKET_UNLOCK(so, 1);
944 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
945 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
946 SCTP_SOCKET_UNLOCK(so, 1);
949 /* are the queues empty? */
950 if (!TAILQ_EMPTY(&asoc->send_queue) ||
951 !TAILQ_EMPTY(&asoc->sent_queue) ||
952 !TAILQ_EMPTY(&asoc->out_wheel)) {
953 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
956 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
957 /* send SHUTDOWN-COMPLETE */
958 sctp_send_shutdown_complete(stcb, net);
959 /* notify upper layer protocol */
960 if (stcb->sctp_socket) {
961 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
962 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
963 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
964 /* Set the connected flag to disconnected */
965 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
968 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
969 /* free the TCB but first save off the ep */
970 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
971 atomic_add_int(&stcb->asoc.refcnt, 1);
972 SCTP_TCB_UNLOCK(stcb);
973 SCTP_SOCKET_LOCK(so, 1);
975 atomic_subtract_int(&stcb->asoc.refcnt, 1);
977 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
978 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
979 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
980 SCTP_SOCKET_UNLOCK(so, 1);
985 * Skip past the param header and then we will find the chunk that caused the
986 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
987 * our peer must be broken.
990 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
991 struct sctp_nets *net)
993 struct sctp_chunkhdr *chk;
995 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
996 switch (chk->chunk_type) {
997 case SCTP_ASCONF_ACK:
999 sctp_asconf_cleanup(stcb, net);
1001 case SCTP_FORWARD_CUM_TSN:
1002 stcb->asoc.peer_supports_prsctp = 0;
1005 SCTPDBG(SCTP_DEBUG_INPUT2,
1006 "Peer does not support chunk type %d(%x)??\n",
1007 chk->chunk_type, (uint32_t) chk->chunk_type);
1013 * Skip past the param header and then we will find the param that caused the
1014 * problem. There are a number of param's in a ASCONF OR the prsctp param
1015 * these will turn of specific features.
1018 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1020 struct sctp_paramhdr *pbad;
1023 switch (ntohs(pbad->param_type)) {
1025 case SCTP_PRSCTP_SUPPORTED:
1026 stcb->asoc.peer_supports_prsctp = 0;
1028 case SCTP_SUPPORTED_CHUNK_EXT:
1030 /* draft-ietf-tsvwg-addip-sctp */
1031 case SCTP_HAS_NAT_SUPPORT:
1032 stcb->asoc.peer_supports_nat = 0;
1034 case SCTP_ECN_NONCE_SUPPORTED:
1035 stcb->asoc.peer_supports_ecn_nonce = 0;
1036 stcb->asoc.ecn_nonce_allowed = 0;
1037 stcb->asoc.ecn_allowed = 0;
1039 case SCTP_ADD_IP_ADDRESS:
1040 case SCTP_DEL_IP_ADDRESS:
1041 case SCTP_SET_PRIM_ADDR:
1042 stcb->asoc.peer_supports_asconf = 0;
1044 case SCTP_SUCCESS_REPORT:
1045 case SCTP_ERROR_CAUSE_IND:
1046 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1047 SCTPDBG(SCTP_DEBUG_INPUT2,
1048 "Turning off ASCONF to this strange peer\n");
1049 stcb->asoc.peer_supports_asconf = 0;
1052 SCTPDBG(SCTP_DEBUG_INPUT2,
1053 "Peer does not support param type %d(%x)??\n",
1054 pbad->param_type, (uint32_t) pbad->param_type);
1060 sctp_handle_error(struct sctp_chunkhdr *ch,
1061 struct sctp_tcb *stcb, struct sctp_nets *net)
1064 struct sctp_paramhdr *phdr;
1065 uint16_t error_type;
1067 struct sctp_association *asoc;
1070 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1075 /* parse through all of the errors and process */
1077 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1078 sizeof(struct sctp_chunkhdr));
1079 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1080 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1081 /* Process an Error Cause */
1082 error_type = ntohs(phdr->param_type);
1083 error_len = ntohs(phdr->param_length);
1084 if ((error_len > chklen) || (error_len == 0)) {
1085 /* invalid param length for this param */
1086 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1090 switch (error_type) {
1091 case SCTP_CAUSE_INVALID_STREAM:
1092 case SCTP_CAUSE_MISSING_PARAM:
1093 case SCTP_CAUSE_INVALID_PARAM:
1094 case SCTP_CAUSE_NO_USER_DATA:
1095 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1098 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1099 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1101 if (sctp_handle_nat_colliding_state(stcb)) {
1105 case SCTP_CAUSE_NAT_MISSING_STATE:
1106 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1108 if (sctp_handle_nat_missing_state(stcb, net)) {
1112 case SCTP_CAUSE_STALE_COOKIE:
1114 * We only act if we have echoed a cookie and are
1117 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1120 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1121 /* Save the time doubled */
1122 asoc->cookie_preserve_req = ntohl(*p) << 1;
1123 asoc->stale_cookie_count++;
1124 if (asoc->stale_cookie_count >
1125 asoc->max_init_times) {
1126 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
1127 /* now free the asoc */
1128 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1129 so = SCTP_INP_SO(stcb->sctp_ep);
1130 atomic_add_int(&stcb->asoc.refcnt, 1);
1131 SCTP_TCB_UNLOCK(stcb);
1132 SCTP_SOCKET_LOCK(so, 1);
1133 SCTP_TCB_LOCK(stcb);
1134 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1136 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1137 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1138 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1139 SCTP_SOCKET_UNLOCK(so, 1);
1143 /* blast back to INIT state */
1144 sctp_toss_old_cookies(stcb, &stcb->asoc);
1145 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1146 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1147 sctp_stop_all_cookie_timers(stcb);
1148 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1151 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1153 * Nothing we can do here, we don't do hostname
1154 * addresses so if the peer does not like my IPv6
1155 * (or IPv4 for that matter) it does not matter. If
1156 * they don't support that type of address, they can
1157 * NOT possibly get that packet type... i.e. with no
1158 * IPv6 you can't recieve a IPv6 packet. so we can
1159 * safely ignore this one. If we ever added support
1160 * for HOSTNAME Addresses, then we would need to do
1164 case SCTP_CAUSE_UNRECOG_CHUNK:
1165 sctp_process_unrecog_chunk(stcb, phdr, net);
1167 case SCTP_CAUSE_UNRECOG_PARAM:
1168 sctp_process_unrecog_param(stcb, phdr);
1170 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1172 * We ignore this since the timer will drive out a
1173 * new cookie anyway and there timer will drive us
1174 * to send a SHUTDOWN_COMPLETE. We can't send one
1175 * here since we don't have their tag.
1178 case SCTP_CAUSE_DELETING_LAST_ADDR:
1179 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1180 case SCTP_CAUSE_DELETING_SRC_ADDR:
1182 * We should NOT get these here, but in a
1185 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1188 case SCTP_CAUSE_OUT_OF_RESC:
1190 * And what, pray tell do we do with the fact that
1191 * the peer is out of resources? Not really sure we
1192 * could do anything but abort. I suspect this
1193 * should have came WITH an abort instead of in a
1198 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1202 adjust = SCTP_SIZE32(error_len);
1204 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1210 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1211 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1212 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1214 struct sctp_init_ack *init_ack;
1215 struct mbuf *op_err;
1217 SCTPDBG(SCTP_DEBUG_INPUT2,
1218 "sctp_handle_init_ack: handling INIT-ACK\n");
1221 SCTPDBG(SCTP_DEBUG_INPUT2,
1222 "sctp_handle_init_ack: TCB is null\n");
1225 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1226 /* Invalid length */
1227 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1228 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1229 op_err, 0, net->port);
1230 *abort_no_unlock = 1;
1233 init_ack = &cp->init;
1234 /* validate parameters */
1235 if (init_ack->initiate_tag == 0) {
1236 /* protocol error... send an abort */
1237 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1238 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1239 op_err, 0, net->port);
1240 *abort_no_unlock = 1;
1243 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1244 /* protocol error... send an abort */
1245 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1246 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1247 op_err, 0, net->port);
1248 *abort_no_unlock = 1;
1251 if (init_ack->num_inbound_streams == 0) {
1252 /* protocol error... send an abort */
1253 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1254 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1255 op_err, 0, net->port);
1256 *abort_no_unlock = 1;
1259 if (init_ack->num_outbound_streams == 0) {
1260 /* protocol error... send an abort */
1261 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1262 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1263 op_err, 0, net->port);
1264 *abort_no_unlock = 1;
1267 /* process according to association state... */
1268 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1269 case SCTP_STATE_COOKIE_WAIT:
1270 /* this is the expected state for this chunk */
1271 /* process the INIT-ACK parameters */
1272 if (stcb->asoc.primary_destination->dest_state &
1273 SCTP_ADDR_UNCONFIRMED) {
1275 * The primary is where we sent the INIT, we can
1276 * always consider it confirmed when the INIT-ACK is
1277 * returned. Do this before we load addresses
1280 stcb->asoc.primary_destination->dest_state &=
1281 ~SCTP_ADDR_UNCONFIRMED;
1282 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1283 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1285 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1286 net, abort_no_unlock, vrf_id) < 0) {
1287 /* error in parsing parameters */
1290 /* update our state */
1291 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1292 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1294 /* reset the RTO calc */
1295 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1296 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1297 stcb->asoc.overall_error_count,
1299 SCTP_FROM_SCTP_INPUT,
1302 stcb->asoc.overall_error_count = 0;
1303 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1305 * collapse the init timer back in case of a exponential
1308 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1311 * the send at the end of the inbound data processing will
1312 * cause the cookie to be sent
1315 case SCTP_STATE_SHUTDOWN_SENT:
1316 /* incorrect state... discard */
1318 case SCTP_STATE_COOKIE_ECHOED:
1319 /* incorrect state... discard */
1321 case SCTP_STATE_OPEN:
1322 /* incorrect state... discard */
1324 case SCTP_STATE_EMPTY:
1325 case SCTP_STATE_INUSE:
1327 /* incorrect state... discard */
1331 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1335 static struct sctp_tcb *
1336 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1337 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1338 struct sctp_inpcb *inp, struct sctp_nets **netp,
1339 struct sockaddr *init_src, int *notification,
1340 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1341 uint32_t vrf_id, uint16_t port);
1345 * handle a state cookie for an existing association m: input packet mbuf
1346 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1347 * "split" mbuf and the cookie signature does not exist offset: offset into
1348 * mbuf to the cookie-echo chunk
1350 static struct sctp_tcb *
1351 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1352 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1353 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1354 struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id,
1355 uint32_t vrf_id, int auth_skipped, uint32_t auth_offset, uint32_t auth_len, uint16_t port)
1357 struct sctp_association *asoc;
1358 struct sctp_init_chunk *init_cp, init_buf;
1359 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1360 struct sctp_nets *net;
1361 struct mbuf *op_err;
1362 struct sctp_paramhdr *ph;
1364 int init_offset, initack_offset, i;
1370 /* I know that the TCB is non-NULL from the caller */
1372 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1373 if (asoc->cookie_how[how_indx] == 0)
1376 if (how_indx < sizeof(asoc->cookie_how)) {
1377 asoc->cookie_how[how_indx] = 1;
1379 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1380 /* SHUTDOWN came in after sending INIT-ACK */
1381 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1382 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1383 0, M_DONTWAIT, 1, MT_DATA);
1384 if (op_err == NULL) {
1389 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1390 ph = mtod(op_err, struct sctp_paramhdr *);
1391 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1392 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1393 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1395 if (how_indx < sizeof(asoc->cookie_how))
1396 asoc->cookie_how[how_indx] = 2;
1400 * find and validate the INIT chunk in the cookie (peer's info) the
1401 * INIT should start after the cookie-echo header struct (chunk
1402 * header, state cookie header struct)
1404 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1406 init_cp = (struct sctp_init_chunk *)
1407 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1408 (uint8_t *) & init_buf);
1409 if (init_cp == NULL) {
1410 /* could not pull a INIT chunk in cookie */
1413 chk_length = ntohs(init_cp->ch.chunk_length);
1414 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1418 * find and validate the INIT-ACK chunk in the cookie (my info) the
1419 * INIT-ACK follows the INIT chunk
1421 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1422 initack_cp = (struct sctp_init_ack_chunk *)
1423 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1424 (uint8_t *) & initack_buf);
1425 if (initack_cp == NULL) {
1426 /* could not pull INIT-ACK chunk in cookie */
1429 chk_length = ntohs(initack_cp->ch.chunk_length);
1430 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1433 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1434 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1436 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1437 * to get into the OPEN state
1439 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1441 * Opps, this means that we somehow generated two vtag's
1442 * the same. I.e. we did:
1444 * <---INIT(tag=a)------
1445 * ----INIT-ACK(tag=t)-->
1446 * ----INIT(tag=t)------> *1
1447 * <---INIT-ACK(tag=a)---
1448 * <----CE(tag=t)------------- *2
1450 * At point *1 we should be generating a different
1451 * tag t'. Which means we would throw away the CE and send
1452 * ours instead. Basically this is case C (throw away side).
1454 if (how_indx < sizeof(asoc->cookie_how))
1455 asoc->cookie_how[how_indx] = 17;
1459 switch SCTP_GET_STATE
1461 case SCTP_STATE_COOKIE_WAIT:
1462 case SCTP_STATE_COOKIE_ECHOED:
1464 * INIT was sent but got a COOKIE_ECHO with the
1465 * correct tags... just accept it...but we must
1466 * process the init so that we can make sure we have
1467 * the right seq no's.
1469 /* First we must process the INIT !! */
1470 retval = sctp_process_init(init_cp, stcb, net);
1472 if (how_indx < sizeof(asoc->cookie_how))
1473 asoc->cookie_how[how_indx] = 3;
1476 /* we have already processed the INIT so no problem */
1477 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1478 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1479 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1480 /* update current state */
1481 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1482 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1484 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1486 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1487 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1488 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1489 stcb->sctp_ep, stcb, asoc->primary_destination);
1491 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1492 sctp_stop_all_cookie_timers(stcb);
1493 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1494 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1495 (inp->sctp_socket->so_qlimit == 0)
1497 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1502 * Here is where collision would go if we
1503 * did a connect() and instead got a
1504 * init/init-ack/cookie done before the
1505 * init-ack came back..
1507 stcb->sctp_ep->sctp_flags |=
1508 SCTP_PCB_FLAGS_CONNECTED;
1509 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1510 so = SCTP_INP_SO(stcb->sctp_ep);
1511 atomic_add_int(&stcb->asoc.refcnt, 1);
1512 SCTP_TCB_UNLOCK(stcb);
1513 SCTP_SOCKET_LOCK(so, 1);
1514 SCTP_TCB_LOCK(stcb);
1515 atomic_add_int(&stcb->asoc.refcnt, -1);
1516 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1517 SCTP_SOCKET_UNLOCK(so, 1);
1521 soisconnected(stcb->sctp_socket);
1522 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1523 SCTP_SOCKET_UNLOCK(so, 1);
1526 /* notify upper layer */
1527 *notification = SCTP_NOTIFY_ASSOC_UP;
1529 * since we did not send a HB make sure we don't
1532 net->hb_responded = 1;
1533 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1534 &cookie->time_entered, sctp_align_unsafe_makecopy);
1536 if (stcb->asoc.sctp_autoclose_ticks &&
1537 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1538 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1544 * we're in the OPEN state (or beyond), so peer must
1545 * have simply lost the COOKIE-ACK
1549 sctp_stop_all_cookie_timers(stcb);
1551 * We ignore the return code here.. not sure if we should
1552 * somehow abort.. but we do have an existing asoc. This
1553 * really should not fail.
1555 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1556 init_offset + sizeof(struct sctp_init_chunk),
1557 initack_offset, sh, init_src)) {
1558 if (how_indx < sizeof(asoc->cookie_how))
1559 asoc->cookie_how[how_indx] = 4;
1562 /* respond with a COOKIE-ACK */
1563 sctp_toss_old_cookies(stcb, asoc);
1564 sctp_send_cookie_ack(stcb);
1565 if (how_indx < sizeof(asoc->cookie_how))
1566 asoc->cookie_how[how_indx] = 5;
1569 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1570 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1571 cookie->tie_tag_my_vtag == 0 &&
1572 cookie->tie_tag_peer_vtag == 0) {
1574 * case C in Section 5.2.4 Table 2: XMOO silently discard
1576 if (how_indx < sizeof(asoc->cookie_how))
1577 asoc->cookie_how[how_indx] = 6;
1581 * If nat support, and the below and stcb is established, send back
1582 * a ABORT(colliding state) if we are established.
1584 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1585 (asoc->peer_supports_nat) &&
1586 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1587 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1588 (asoc->peer_vtag == 0)))) {
1590 * Special case - Peer's support nat. We may have two init's
1591 * that we gave out the same tag on since one was not
1592 * established.. i.e. we get INIT from host-1 behind the nat
1593 * and we respond tag-a, we get a INIT from host-2 behind
1594 * the nat and we get tag-a again. Then we bring up host-1
1595 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1596 * Now we have colliding state. We must send an abort here
1597 * with colliding state indication.
1599 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1600 0, M_DONTWAIT, 1, MT_DATA);
1601 if (op_err == NULL) {
1605 /* pre-reserve some space */
1607 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1609 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1611 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1612 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1614 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1615 ph = mtod(op_err, struct sctp_paramhdr *);
1616 ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE);
1617 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1618 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
1621 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1622 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1623 (asoc->peer_vtag == 0))) {
1625 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1626 * should be ok, re-accept peer info
1628 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1630 * Extension of case C. If we hit this, then the
1631 * random number generator returned the same vtag
1632 * when we first sent our INIT-ACK and when we later
1633 * sent our INIT. The side with the seq numbers that
1634 * are different will be the one that normnally
1635 * would have hit case C. This in effect "extends"
1636 * our vtags in this collision case to be 64 bits.
1637 * The same collision could occur aka you get both
1638 * vtag and seq number the same twice in a row.. but
1639 * is much less likely. If it did happen then we
1640 * would proceed through and bring up the assoc.. we
1641 * may end up with the wrong stream setup however..
1642 * which would be bad.. but there is no way to
1643 * tell.. until we send on a stream that does not
1646 if (how_indx < sizeof(asoc->cookie_how))
1647 asoc->cookie_how[how_indx] = 7;
1651 if (how_indx < sizeof(asoc->cookie_how))
1652 asoc->cookie_how[how_indx] = 8;
1653 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1654 sctp_stop_all_cookie_timers(stcb);
1656 * since we did not send a HB make sure we don't double
1659 net->hb_responded = 1;
1660 if (stcb->asoc.sctp_autoclose_ticks &&
1661 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1662 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1665 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1666 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1668 /* Note last_cwr_tsn? where is this used? */
1669 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1670 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1672 * Ok the peer probably discarded our data (if we
1673 * echoed a cookie+data). So anything on the
1674 * sent_queue should be marked for retransmit, we
1675 * may not get something to kick us so it COULD
1676 * still take a timeout to move these.. but it can't
1677 * hurt to mark them.
1679 struct sctp_tmit_chunk *chk;
1681 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1682 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1683 chk->sent = SCTP_DATAGRAM_RESEND;
1684 sctp_flight_size_decrease(chk);
1685 sctp_total_flight_decrease(stcb, chk);
1686 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1692 /* process the INIT info (peer's info) */
1693 retval = sctp_process_init(init_cp, stcb, net);
1695 if (how_indx < sizeof(asoc->cookie_how))
1696 asoc->cookie_how[how_indx] = 9;
1699 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1700 init_offset + sizeof(struct sctp_init_chunk),
1701 initack_offset, sh, init_src)) {
1702 if (how_indx < sizeof(asoc->cookie_how))
1703 asoc->cookie_how[how_indx] = 10;
1706 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1707 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1708 *notification = SCTP_NOTIFY_ASSOC_UP;
1710 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1711 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1712 (inp->sctp_socket->so_qlimit == 0)) {
1713 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1717 stcb->sctp_ep->sctp_flags |=
1718 SCTP_PCB_FLAGS_CONNECTED;
1719 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1720 so = SCTP_INP_SO(stcb->sctp_ep);
1721 atomic_add_int(&stcb->asoc.refcnt, 1);
1722 SCTP_TCB_UNLOCK(stcb);
1723 SCTP_SOCKET_LOCK(so, 1);
1724 SCTP_TCB_LOCK(stcb);
1725 atomic_add_int(&stcb->asoc.refcnt, -1);
1726 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1727 SCTP_SOCKET_UNLOCK(so, 1);
1731 soisconnected(stcb->sctp_socket);
1732 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1733 SCTP_SOCKET_UNLOCK(so, 1);
1736 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1737 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1739 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1740 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1741 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1742 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1744 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1746 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1747 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1748 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1749 stcb->sctp_ep, stcb, asoc->primary_destination);
1751 sctp_stop_all_cookie_timers(stcb);
1752 sctp_toss_old_cookies(stcb, asoc);
1753 sctp_send_cookie_ack(stcb);
1756 * only if we have retrans set do we do this. What
1757 * this call does is get only the COOKIE-ACK out and
1758 * then when we return the normal call to
1759 * sctp_chunk_output will get the retrans out behind
1762 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1764 if (how_indx < sizeof(asoc->cookie_how))
1765 asoc->cookie_how[how_indx] = 11;
1769 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1770 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1771 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1772 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1773 cookie->tie_tag_peer_vtag != 0) {
1774 struct sctpasochead *head;
1776 if (asoc->peer_supports_nat) {
1778 * This is a gross gross hack. just call the
1779 * cookie_new code since we are allowing a duplicate
1780 * association. I hope this works...
1782 return (sctp_process_cookie_new(m, iphlen, offset, sh, cookie, cookie_len,
1783 inp, netp, init_src, notification,
1784 auth_skipped, auth_offset, auth_len,
1788 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1791 if (how_indx < sizeof(asoc->cookie_how))
1792 asoc->cookie_how[how_indx] = 12;
1793 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1794 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1796 *sac_assoc_id = sctp_get_associd(stcb);
1797 /* notify upper layer */
1798 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1799 atomic_add_int(&stcb->asoc.refcnt, 1);
1800 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1801 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1802 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1803 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1805 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1806 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1807 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1808 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1810 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1811 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1812 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1813 stcb->sctp_ep, stcb, asoc->primary_destination);
1815 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1816 /* move to OPEN state, if not in SHUTDOWN_SENT */
1817 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1819 asoc->pre_open_streams =
1820 ntohs(initack_cp->init.num_outbound_streams);
1821 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1822 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1823 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1825 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1826 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1828 asoc->str_reset_seq_in = asoc->init_seq_number;
1830 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1831 if (asoc->mapping_array) {
1832 memset(asoc->mapping_array, 0,
1833 asoc->mapping_array_size);
1835 /* EY 05/13/08 - nr_sack version of the above if statement */
1836 if (asoc->nr_mapping_array && SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)
1837 && asoc->peer_supports_nr_sack) {
1838 memset(asoc->nr_mapping_array, 0,
1839 asoc->nr_mapping_array_size);
1841 SCTP_TCB_UNLOCK(stcb);
1842 SCTP_INP_INFO_WLOCK();
1843 SCTP_INP_WLOCK(stcb->sctp_ep);
1844 SCTP_TCB_LOCK(stcb);
1845 atomic_add_int(&stcb->asoc.refcnt, -1);
1846 /* send up all the data */
1847 SCTP_TCB_SEND_LOCK(stcb);
1849 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED);
1850 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1851 stcb->asoc.strmout[i].stream_no = i;
1852 stcb->asoc.strmout[i].next_sequence_sent = 0;
1853 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1855 /* process the INIT-ACK info (my info) */
1856 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1857 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1859 /* pull from vtag hash */
1860 LIST_REMOVE(stcb, sctp_asocs);
1861 /* re-insert to new vtag position */
1862 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1863 SCTP_BASE_INFO(hashasocmark))];
1865 * put it in the bucket in the vtag hash of assoc's for the
1868 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1870 /* process the INIT info (peer's info) */
1871 SCTP_TCB_SEND_UNLOCK(stcb);
1872 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1873 SCTP_INP_INFO_WUNLOCK();
1875 retval = sctp_process_init(init_cp, stcb, net);
1877 if (how_indx < sizeof(asoc->cookie_how))
1878 asoc->cookie_how[how_indx] = 13;
1883 * since we did not send a HB make sure we don't double
1886 net->hb_responded = 1;
1888 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1889 init_offset + sizeof(struct sctp_init_chunk),
1890 initack_offset, sh, init_src)) {
1891 if (how_indx < sizeof(asoc->cookie_how))
1892 asoc->cookie_how[how_indx] = 14;
1896 /* respond with a COOKIE-ACK */
1897 sctp_stop_all_cookie_timers(stcb);
1898 sctp_toss_old_cookies(stcb, asoc);
1899 sctp_send_cookie_ack(stcb);
1900 if (how_indx < sizeof(asoc->cookie_how))
1901 asoc->cookie_how[how_indx] = 15;
1905 if (how_indx < sizeof(asoc->cookie_how))
1906 asoc->cookie_how[how_indx] = 16;
1907 /* all other cases... */
1913 * handle a state cookie for a new association m: input packet mbuf chain--
1914 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1915 * and the cookie signature does not exist offset: offset into mbuf to the
1916 * cookie-echo chunk length: length of the cookie chunk to: where the init
1917 * was from returns a new TCB
1920 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1921 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1922 struct sctp_inpcb *inp, struct sctp_nets **netp,
1923 struct sockaddr *init_src, int *notification,
1924 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1925 uint32_t vrf_id, uint16_t port)
1927 struct sctp_tcb *stcb;
1928 struct sctp_init_chunk *init_cp, init_buf;
1929 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1930 struct sockaddr_storage sa_store;
1931 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1932 struct sockaddr_in *sin;
1933 struct sockaddr_in6 *sin6;
1934 struct sctp_association *asoc;
1936 int init_offset, initack_offset, initack_limit;
1940 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1942 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1945 so = SCTP_INP_SO(inp);
1949 * find and validate the INIT chunk in the cookie (peer's info) the
1950 * INIT should start after the cookie-echo header struct (chunk
1951 * header, state cookie header struct)
1953 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1954 init_cp = (struct sctp_init_chunk *)
1955 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1956 (uint8_t *) & init_buf);
1957 if (init_cp == NULL) {
1958 /* could not pull a INIT chunk in cookie */
1959 SCTPDBG(SCTP_DEBUG_INPUT1,
1960 "process_cookie_new: could not pull INIT chunk hdr\n");
1963 chk_length = ntohs(init_cp->ch.chunk_length);
1964 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1965 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1968 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1970 * find and validate the INIT-ACK chunk in the cookie (my info) the
1971 * INIT-ACK follows the INIT chunk
1973 initack_cp = (struct sctp_init_ack_chunk *)
1974 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1975 (uint8_t *) & initack_buf);
1976 if (initack_cp == NULL) {
1977 /* could not pull INIT-ACK chunk in cookie */
1978 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1981 chk_length = ntohs(initack_cp->ch.chunk_length);
1982 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1986 * NOTE: We can't use the INIT_ACK's chk_length to determine the
1987 * "initack_limit" value. This is because the chk_length field
1988 * includes the length of the cookie, but the cookie is omitted when
1989 * the INIT and INIT_ACK are tacked onto the cookie...
1991 initack_limit = offset + cookie_len;
1994 * now that we know the INIT/INIT-ACK are in place, create a new TCB
1999 * Here we do a trick, we set in NULL for the proc/thread argument.
2000 * We do this since in effect we only use the p argument when the
2001 * socket is unbound and we must do an implicit bind. Since we are
2002 * getting a cookie, we cannot be unbound.
2004 stcb = sctp_aloc_assoc(inp, init_src, 0, &error,
2005 ntohl(initack_cp->init.initiate_tag), vrf_id,
2006 (struct thread *)NULL
2009 struct mbuf *op_err;
2011 /* memory problem? */
2012 SCTPDBG(SCTP_DEBUG_INPUT1,
2013 "process_cookie_new: no room for another TCB!\n");
2014 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2016 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2017 sh, op_err, vrf_id, port);
2020 /* get the correct sctp_nets */
2022 *netp = sctp_findnet(stcb, init_src);
2025 /* get scope variables out of cookie */
2026 asoc->ipv4_local_scope = cookie->ipv4_scope;
2027 asoc->site_scope = cookie->site_scope;
2028 asoc->local_scope = cookie->local_scope;
2029 asoc->loopback_scope = cookie->loopback_scope;
2031 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2032 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2033 struct mbuf *op_err;
2036 * Houston we have a problem. The EP changed while the
2037 * cookie was in flight. Only recourse is to abort the
2040 atomic_add_int(&stcb->asoc.refcnt, 1);
2041 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2042 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2043 sh, op_err, vrf_id, port);
2044 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2045 SCTP_TCB_UNLOCK(stcb);
2046 SCTP_SOCKET_LOCK(so, 1);
2047 SCTP_TCB_LOCK(stcb);
2049 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2050 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2051 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2052 SCTP_SOCKET_UNLOCK(so, 1);
2054 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2057 /* process the INIT-ACK info (my info) */
2058 old_tag = asoc->my_vtag;
2059 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2060 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2061 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2062 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2063 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2064 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2065 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
2066 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2067 asoc->str_reset_seq_in = asoc->init_seq_number;
2069 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2071 /* process the INIT info (peer's info) */
2073 retval = sctp_process_init(init_cp, stcb, *netp);
2077 atomic_add_int(&stcb->asoc.refcnt, 1);
2078 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2079 SCTP_TCB_UNLOCK(stcb);
2080 SCTP_SOCKET_LOCK(so, 1);
2081 SCTP_TCB_LOCK(stcb);
2083 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
2084 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2085 SCTP_SOCKET_UNLOCK(so, 1);
2087 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2090 /* load all addresses */
2091 if (sctp_load_addresses_from_init(stcb, m, iphlen,
2092 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
2094 atomic_add_int(&stcb->asoc.refcnt, 1);
2095 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2096 SCTP_TCB_UNLOCK(stcb);
2097 SCTP_SOCKET_LOCK(so, 1);
2098 SCTP_TCB_LOCK(stcb);
2100 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
2101 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2102 SCTP_SOCKET_UNLOCK(so, 1);
2104 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2108 * verify any preceding AUTH chunk that was skipped
2110 /* pull the local authentication parameters from the cookie/init-ack */
2111 sctp_auth_get_cookie_params(stcb, m,
2112 initack_offset + sizeof(struct sctp_init_ack_chunk),
2113 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2115 struct sctp_auth_chunk *auth;
2117 auth = (struct sctp_auth_chunk *)
2118 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2119 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2120 /* auth HMAC failed, dump the assoc and packet */
2121 SCTPDBG(SCTP_DEBUG_AUTH1,
2122 "COOKIE-ECHO: AUTH failed\n");
2123 atomic_add_int(&stcb->asoc.refcnt, 1);
2124 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2125 SCTP_TCB_UNLOCK(stcb);
2126 SCTP_SOCKET_LOCK(so, 1);
2127 SCTP_TCB_LOCK(stcb);
2129 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2130 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2131 SCTP_SOCKET_UNLOCK(so, 1);
2133 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2136 /* remaining chunks checked... good to go */
2137 stcb->asoc.authenticated = 1;
2140 /* update current state */
2141 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2142 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2143 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2144 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2145 stcb->sctp_ep, stcb, asoc->primary_destination);
2147 sctp_stop_all_cookie_timers(stcb);
2148 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2149 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2152 * if we're doing ASCONFs, check to see if we have any new local
2153 * addresses that need to get added to the peer (eg. addresses
2154 * changed while cookie echo in flight). This needs to be done
2155 * after we go to the OPEN state to do the correct asconf
2156 * processing. else, make sure we have the correct addresses in our
2160 /* warning, we re-use sin, sin6, sa_store here! */
2161 /* pull in local_address (our "from" address) */
2162 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
2163 /* source addr is IPv4 */
2164 sin = (struct sockaddr_in *)initack_src;
2165 memset(sin, 0, sizeof(*sin));
2166 sin->sin_family = AF_INET;
2167 sin->sin_len = sizeof(struct sockaddr_in);
2168 sin->sin_addr.s_addr = cookie->laddress[0];
2169 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
2170 /* source addr is IPv6 */
2171 sin6 = (struct sockaddr_in6 *)initack_src;
2172 memset(sin6, 0, sizeof(*sin6));
2173 sin6->sin6_family = AF_INET6;
2174 sin6->sin6_len = sizeof(struct sockaddr_in6);
2175 sin6->sin6_scope_id = cookie->scope_id;
2176 memcpy(&sin6->sin6_addr, cookie->laddress,
2177 sizeof(sin6->sin6_addr));
2179 atomic_add_int(&stcb->asoc.refcnt, 1);
2180 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2181 SCTP_TCB_UNLOCK(stcb);
2182 SCTP_SOCKET_LOCK(so, 1);
2183 SCTP_TCB_LOCK(stcb);
2185 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2186 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2187 SCTP_SOCKET_UNLOCK(so, 1);
2189 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2193 /* set up to notify upper layer */
2194 *notification = SCTP_NOTIFY_ASSOC_UP;
2195 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2196 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2197 (inp->sctp_socket->so_qlimit == 0)) {
2199 * This is an endpoint that called connect() how it got a
2200 * cookie that is NEW is a bit of a mystery. It must be that
2201 * the INIT was sent, but before it got there.. a complete
2202 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2203 * should have went to the other code.. not here.. oh well..
2204 * a bit of protection is worth having..
2206 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2207 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2208 atomic_add_int(&stcb->asoc.refcnt, 1);
2209 SCTP_TCB_UNLOCK(stcb);
2210 SCTP_SOCKET_LOCK(so, 1);
2211 SCTP_TCB_LOCK(stcb);
2212 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2213 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2214 SCTP_SOCKET_UNLOCK(so, 1);
2218 soisconnected(stcb->sctp_socket);
2219 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2220 SCTP_SOCKET_UNLOCK(so, 1);
2222 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2223 (inp->sctp_socket->so_qlimit)) {
2225 * We don't want to do anything with this one. Since it is
2226 * the listening guy. The timer will get started for
2227 * accepted connections in the caller.
2231 /* since we did not send a HB make sure we don't double things */
2232 if ((netp) && (*netp))
2233 (*netp)->hb_responded = 1;
2235 if (stcb->asoc.sctp_autoclose_ticks &&
2236 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2237 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2239 /* calculate the RTT */
2240 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2241 if ((netp) && (*netp)) {
2242 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2243 &cookie->time_entered, sctp_align_unsafe_makecopy);
2245 /* respond with a COOKIE-ACK */
2246 sctp_send_cookie_ack(stcb);
2249 * check the address lists for any ASCONFs that need to be sent
2250 * AFTER the cookie-ack is sent
2252 sctp_check_address_list(stcb, m,
2253 initack_offset + sizeof(struct sctp_init_ack_chunk),
2254 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2255 initack_src, cookie->local_scope, cookie->site_scope,
2256 cookie->ipv4_scope, cookie->loopback_scope);
2263 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2264 * we NEED to make sure we are not already using the vtag. If so we
2265 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2266 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2267 SCTP_BASE_INFO(hashasocmark))];
2268 LIST_FOREACH(stcb, head, sctp_asocs) {
2269 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2270 -- SEND ABORT - TRY AGAIN --
2276 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2277 * existing (non-NULL) TCB
2279 static struct mbuf *
2280 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2281 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2282 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2283 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2284 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2286 struct sctp_state_cookie *cookie;
2287 struct sockaddr_in6 sin6;
2288 struct sockaddr_in sin;
2289 struct sctp_tcb *l_stcb = *stcb;
2290 struct sctp_inpcb *l_inp;
2291 struct sockaddr *to;
2292 sctp_assoc_t sac_restart_id;
2293 struct sctp_pcb *ep;
2295 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2297 uint8_t cookie_ok = 0;
2298 unsigned int size_of_pkt, sig_offset, cookie_offset;
2299 unsigned int cookie_len;
2301 struct timeval time_expires;
2302 struct sockaddr_storage dest_store;
2303 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2305 int notification = 0;
2306 struct sctp_nets *netl;
2307 int had_a_existing_tcb = 0;
2309 SCTPDBG(SCTP_DEBUG_INPUT2,
2310 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2312 if (inp_p == NULL) {
2315 /* First get the destination address setup too. */
2316 iph = mtod(m, struct ip *);
2317 switch (iph->ip_v) {
2321 struct sockaddr_in *lsin;
2323 lsin = (struct sockaddr_in *)(localep_sa);
2324 memset(lsin, 0, sizeof(*lsin));
2325 lsin->sin_family = AF_INET;
2326 lsin->sin_len = sizeof(*lsin);
2327 lsin->sin_port = sh->dest_port;
2328 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2329 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2333 case IPV6_VERSION >> 4:
2336 struct ip6_hdr *ip6;
2337 struct sockaddr_in6 *lsin6;
2339 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2340 memset(lsin6, 0, sizeof(*lsin6));
2341 lsin6->sin6_family = AF_INET6;
2342 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2343 ip6 = mtod(m, struct ip6_hdr *);
2344 lsin6->sin6_port = sh->dest_port;
2345 lsin6->sin6_addr = ip6->ip6_dst;
2346 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2354 cookie = &cp->cookie;
2355 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2356 cookie_len = ntohs(cp->ch.chunk_length);
2358 if ((cookie->peerport != sh->src_port) &&
2359 (cookie->myport != sh->dest_port) &&
2360 (cookie->my_vtag != sh->v_tag)) {
2362 * invalid ports or bad tag. Note that we always leave the
2363 * v_tag in the header in network order and when we stored
2364 * it in the my_vtag slot we also left it in network order.
2365 * This maintains the match even though it may be in the
2366 * opposite byte order of the machine :->
2370 if (cookie_len > size_of_pkt ||
2371 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2372 sizeof(struct sctp_init_chunk) +
2373 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2374 /* cookie too long! or too small */
2378 * split off the signature into its own mbuf (since it should not be
2379 * calculated in the sctp_hmac_m() call).
2381 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2382 if (sig_offset > size_of_pkt) {
2383 /* packet not correct size! */
2384 /* XXX this may already be accounted for earlier... */
2387 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2388 if (m_sig == NULL) {
2389 /* out of memory or ?? */
2392 #ifdef SCTP_MBUF_LOGGING
2393 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2398 if (SCTP_BUF_IS_EXTENDED(mat)) {
2399 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2401 mat = SCTP_BUF_NEXT(mat);
2407 * compute the signature/digest for the cookie
2409 ep = &(*inp_p)->sctp_ep;
2412 SCTP_TCB_UNLOCK(l_stcb);
2414 SCTP_INP_RLOCK(l_inp);
2416 SCTP_TCB_LOCK(l_stcb);
2418 /* which cookie is it? */
2419 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2420 (ep->current_secret_number != ep->last_secret_number)) {
2421 /* it's the old cookie */
2422 (void)sctp_hmac_m(SCTP_HMAC,
2423 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2424 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2426 /* it's the current cookie */
2427 (void)sctp_hmac_m(SCTP_HMAC,
2428 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2429 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2431 /* get the signature */
2432 SCTP_INP_RUNLOCK(l_inp);
2433 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2435 /* couldn't find signature */
2436 sctp_m_freem(m_sig);
2439 /* compare the received digest with the computed digest */
2440 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2441 /* try the old cookie? */
2442 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2443 (ep->current_secret_number != ep->last_secret_number)) {
2444 /* compute digest with old */
2445 (void)sctp_hmac_m(SCTP_HMAC,
2446 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2447 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2449 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2457 * Now before we continue we must reconstruct our mbuf so that
2458 * normal processing of any other chunks will work.
2464 while (SCTP_BUF_NEXT(m_at) != NULL) {
2465 m_at = SCTP_BUF_NEXT(m_at);
2467 SCTP_BUF_NEXT(m_at) = m_sig;
2470 if (cookie_ok == 0) {
2471 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2472 SCTPDBG(SCTP_DEBUG_INPUT2,
2473 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2474 (uint32_t) offset, cookie_offset, sig_offset);
2478 * check the cookie timestamps to be sure it's not stale
2480 (void)SCTP_GETTIME_TIMEVAL(&now);
2481 /* Expire time is in Ticks, so we convert to seconds */
2482 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2483 time_expires.tv_usec = cookie->time_entered.tv_usec;
2485 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2488 if (timevalcmp(&now, &time_expires, >)) {
2489 /* cookie is stale! */
2490 struct mbuf *op_err;
2491 struct sctp_stale_cookie_msg *scm;
2494 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2495 0, M_DONTWAIT, 1, MT_DATA);
2496 if (op_err == NULL) {
2501 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2502 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2503 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2504 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2505 (sizeof(uint32_t))));
2506 /* seconds to usec */
2507 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2510 tim = now.tv_usec - cookie->time_entered.tv_usec;
2511 scm->time_usec = htonl(tim);
2512 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2517 * Now we must see with the lookup address if we have an existing
2518 * asoc. This will only happen if we were in the COOKIE-WAIT state
2519 * and a INIT collided with us and somewhere the peer sent the
2520 * cookie on another address besides the single address our assoc
2521 * had for him. In this case we will have one of the tie-tags set at
2522 * least AND the address field in the cookie can be used to look it
2526 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2527 memset(&sin6, 0, sizeof(sin6));
2528 sin6.sin6_family = AF_INET6;
2529 sin6.sin6_len = sizeof(sin6);
2530 sin6.sin6_port = sh->src_port;
2531 sin6.sin6_scope_id = cookie->scope_id;
2532 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2533 sizeof(sin6.sin6_addr.s6_addr));
2534 to = (struct sockaddr *)&sin6;
2535 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2536 memset(&sin, 0, sizeof(sin));
2537 sin.sin_family = AF_INET;
2538 sin.sin_len = sizeof(sin);
2539 sin.sin_port = sh->src_port;
2540 sin.sin_addr.s_addr = cookie->address[0];
2541 to = (struct sockaddr *)&sin;
2543 /* This should not happen */
2546 if ((*stcb == NULL) && to) {
2547 /* Yep, lets check */
2548 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2549 if (*stcb == NULL) {
2551 * We should have only got back the same inp. If we
2552 * got back a different ep we have a problem. The
2553 * original findep got back l_inp and now
2555 if (l_inp != *inp_p) {
2556 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2559 if (*locked_tcb == NULL) {
2561 * In this case we found the assoc only
2562 * after we locked the create lock. This
2563 * means we are in a colliding case and we
2564 * must make sure that we unlock the tcb if
2565 * its one of the cases where we throw away
2566 * the incoming packets.
2568 *locked_tcb = *stcb;
2571 * We must also increment the inp ref count
2572 * since the ref_count flags was set when we
2573 * did not find the TCB, now we found it
2574 * which reduces the refcount.. we must
2575 * raise it back out to balance it all :-)
2577 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2578 if ((*stcb)->sctp_ep != l_inp) {
2579 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2580 (*stcb)->sctp_ep, l_inp);
2588 cookie_len -= SCTP_SIGNATURE_SIZE;
2589 if (*stcb == NULL) {
2590 /* this is the "normal" case... get a new TCB */
2591 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2592 cookie_len, *inp_p, netp, to, ¬ification,
2593 auth_skipped, auth_offset, auth_len, vrf_id, port);
2595 /* this is abnormal... cookie-echo on existing TCB */
2596 had_a_existing_tcb = 1;
2597 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2598 cookie, cookie_len, *inp_p, *stcb, netp, to,
2599 ¬ification, &sac_restart_id, vrf_id, auth_skipped, auth_offset, auth_len, port);
2602 if (*stcb == NULL) {
2603 /* still no TCB... must be bad cookie-echo */
2607 * Ok, we built an association so confirm the address we sent the
2610 netl = sctp_findnet(*stcb, to);
2612 * This code should in theory NOT run but
2615 /* TSNH! Huh, why do I need to add this address here? */
2618 ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE,
2619 SCTP_IN_COOKIE_PROC);
2620 netl = sctp_findnet(*stcb, to);
2623 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2624 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2625 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2627 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2628 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2632 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p,
2635 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2636 if (!had_a_existing_tcb ||
2637 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2639 * If we have a NEW cookie or the connect never
2640 * reached the connected state during collision we
2641 * must do the TCP accept thing.
2643 struct socket *so, *oso;
2644 struct sctp_inpcb *inp;
2646 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2648 * For a restart we will keep the same
2649 * socket, no need to do anything. I THINK!!
2651 sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED);
2654 oso = (*inp_p)->sctp_socket;
2655 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2656 SCTP_TCB_UNLOCK((*stcb));
2657 so = sonewconn(oso, 0
2659 SCTP_TCB_LOCK((*stcb));
2660 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2663 struct mbuf *op_err;
2665 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2666 struct socket *pcb_so;
2669 /* Too many sockets */
2670 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2671 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2672 sctp_abort_association(*inp_p, NULL, m, iphlen,
2673 sh, op_err, vrf_id, port);
2674 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2675 pcb_so = SCTP_INP_SO(*inp_p);
2676 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2677 SCTP_TCB_UNLOCK((*stcb));
2678 SCTP_SOCKET_LOCK(pcb_so, 1);
2679 SCTP_TCB_LOCK((*stcb));
2680 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2682 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2683 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2684 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2688 inp = (struct sctp_inpcb *)so->so_pcb;
2689 SCTP_INP_INCR_REF(inp);
2691 * We add the unbound flag here so that if we get an
2692 * soabort() before we get the move_pcb done, we
2693 * will properly cleanup.
2695 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2696 SCTP_PCB_FLAGS_CONNECTED |
2697 SCTP_PCB_FLAGS_IN_TCPPOOL |
2698 SCTP_PCB_FLAGS_UNBOUND |
2699 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2700 SCTP_PCB_FLAGS_DONT_WAKE);
2701 inp->sctp_features = (*inp_p)->sctp_features;
2702 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2703 inp->sctp_socket = so;
2704 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2705 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2706 inp->sctp_context = (*inp_p)->sctp_context;
2707 inp->inp_starting_point_for_iterator = NULL;
2709 * copy in the authentication parameters from the
2712 if (inp->sctp_ep.local_hmacs)
2713 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2714 inp->sctp_ep.local_hmacs =
2715 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2716 if (inp->sctp_ep.local_auth_chunks)
2717 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2718 inp->sctp_ep.local_auth_chunks =
2719 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2722 * Now we must move it from one hash table to
2723 * another and get the tcb in the right place.
2725 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2727 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2728 SCTP_TCB_UNLOCK((*stcb));
2730 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2732 SCTP_TCB_LOCK((*stcb));
2733 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2737 * now we must check to see if we were aborted while
2738 * the move was going on and the lock/unlock
2741 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2743 * yep it was, we leave the assoc attached
2744 * to the socket since the sctp_inpcb_free()
2745 * call will send an abort for us.
2747 SCTP_INP_DECR_REF(inp);
2750 SCTP_INP_DECR_REF(inp);
2751 /* Switch over to the new guy */
2753 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2756 * Pull it from the incomplete queue and wake the
2759 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2760 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2761 SCTP_TCB_UNLOCK((*stcb));
2762 SCTP_SOCKET_LOCK(so, 1);
2765 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2766 SCTP_TCB_LOCK((*stcb));
2767 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2768 SCTP_SOCKET_UNLOCK(so, 1);
2773 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2774 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2780 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2781 struct sctp_tcb *stcb, struct sctp_nets *net)
2783 /* cp must not be used, others call this without a c-ack :-) */
2784 struct sctp_association *asoc;
2786 SCTPDBG(SCTP_DEBUG_INPUT2,
2787 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2793 sctp_stop_all_cookie_timers(stcb);
2794 /* process according to association state */
2795 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2796 /* state change only needed when I am in right state */
2797 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2798 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2799 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2800 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2801 stcb->sctp_ep, stcb, asoc->primary_destination);
2805 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2806 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2807 if (asoc->overall_error_count == 0) {
2808 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2809 &asoc->time_entered, sctp_align_safe_nocopy);
2811 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2812 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2813 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2814 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2815 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2819 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2820 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2821 so = SCTP_INP_SO(stcb->sctp_ep);
2822 atomic_add_int(&stcb->asoc.refcnt, 1);
2823 SCTP_TCB_UNLOCK(stcb);
2824 SCTP_SOCKET_LOCK(so, 1);
2825 SCTP_TCB_LOCK(stcb);
2826 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2827 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2828 SCTP_SOCKET_UNLOCK(so, 1);
2832 soisconnected(stcb->sctp_socket);
2833 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2834 SCTP_SOCKET_UNLOCK(so, 1);
2837 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2840 * since we did not send a HB make sure we don't double
2843 net->hb_responded = 1;
2845 if (stcb->asoc.sctp_autoclose_ticks &&
2846 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2847 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2848 stcb->sctp_ep, stcb, NULL);
2851 * send ASCONF if parameters are pending and ASCONFs are
2852 * allowed (eg. addresses changed when init/cookie echo were
2855 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2856 (stcb->asoc.peer_supports_asconf) &&
2857 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2858 #ifdef SCTP_TIMER_BASED_ASCONF
2859 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2860 stcb->sctp_ep, stcb,
2861 stcb->asoc.primary_destination);
2863 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2864 SCTP_ADDR_NOT_LOCKED);
2868 /* Toss the cookie if I can */
2869 sctp_toss_old_cookies(stcb, asoc);
2870 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2871 /* Restart the timer if we have pending data */
2872 struct sctp_tmit_chunk *chk;
2874 chk = TAILQ_FIRST(&asoc->sent_queue);
2876 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2883 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2884 struct sctp_tcb *stcb)
2886 struct sctp_nets *net;
2887 struct sctp_tmit_chunk *lchk;
2890 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2893 SCTP_STAT_INCR(sctps_recvecne);
2894 tsn = ntohl(cp->tsn);
2895 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2896 /* Also we make sure we disable the nonce_wait */
2897 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2899 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2901 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2903 stcb->asoc.nonce_wait_for_ecne = 0;
2904 stcb->asoc.nonce_sum_check = 0;
2906 /* Find where it was sent, if possible */
2908 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2910 if (lchk->rec.data.TSN_seq == tsn) {
2914 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2916 lchk = TAILQ_NEXT(lchk, sctp_next);
2919 /* default is we use the primary */
2920 net = stcb->asoc.primary_destination;
2922 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2924 * JRS - Use the congestion control given in the pluggable
2927 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net);
2929 * we reduce once every RTT. So we will only lower cwnd at
2930 * the next sending seq i.e. the resync_tsn.
2932 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2935 * We always send a CWR this way if our previous one was lost our
2936 * peer will get an update, or if it is not time again to reduce we
2937 * still get the cwr to the peer.
2939 sctp_send_cwr(stcb, net, tsn);
2943 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2946 * Here we get a CWR from the peer. We must look in the outqueue and
2947 * make sure that we have a covered ECNE in teh control chunk part.
2950 struct sctp_tmit_chunk *chk;
2951 struct sctp_ecne_chunk *ecne;
2953 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2954 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2958 * Look for and remove if it is the right TSN. Since there
2959 * is only ONE ECNE on the control queue at any one time we
2960 * don't need to worry about more than one!
2962 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2963 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2964 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2965 /* this covers this ECNE, we can remove it */
2966 stcb->asoc.ecn_echo_cnt_onq--;
2967 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2970 sctp_m_freem(chk->data);
2973 stcb->asoc.ctrl_queue_cnt--;
2974 sctp_free_a_chunk(stcb, chk);
2981 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2982 struct sctp_tcb *stcb, struct sctp_nets *net)
2984 struct sctp_association *asoc;
2986 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2991 SCTPDBG(SCTP_DEBUG_INPUT2,
2992 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2997 /* process according to association state */
2998 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2999 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3000 SCTPDBG(SCTP_DEBUG_INPUT2,
3001 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3002 SCTP_TCB_UNLOCK(stcb);
3005 /* notify upper layer protocol */
3006 if (stcb->sctp_socket) {
3007 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3008 /* are the queues empty? they should be */
3009 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3010 !TAILQ_EMPTY(&asoc->sent_queue) ||
3011 !TAILQ_EMPTY(&asoc->out_wheel)) {
3012 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
3015 /* stop the timer */
3016 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
3017 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3019 SCTPDBG(SCTP_DEBUG_INPUT2,
3020 "sctp_handle_shutdown_complete: calls free-asoc\n");
3021 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3022 so = SCTP_INP_SO(stcb->sctp_ep);
3023 atomic_add_int(&stcb->asoc.refcnt, 1);
3024 SCTP_TCB_UNLOCK(stcb);
3025 SCTP_SOCKET_LOCK(so, 1);
3026 SCTP_TCB_LOCK(stcb);
3027 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3029 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
3030 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3031 SCTP_SOCKET_UNLOCK(so, 1);
3037 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3038 struct sctp_nets *net, uint8_t flg)
3040 switch (desc->chunk_type) {
3042 /* find the tsn to resend (possibly */
3045 struct sctp_tmit_chunk *tp1;
3047 tsn = ntohl(desc->tsn_ifany);
3048 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3050 if (tp1->rec.data.TSN_seq == tsn) {
3054 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
3060 tp1 = TAILQ_NEXT(tp1, sctp_next);
3064 * Do it the other way , aka without paying
3065 * attention to queue seq order.
3067 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3068 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3070 if (tp1->rec.data.TSN_seq == tsn) {
3074 tp1 = TAILQ_NEXT(tp1, sctp_next);
3078 SCTP_STAT_INCR(sctps_pdrptsnnf);
3080 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3083 if ((stcb->asoc.peers_rwnd == 0) &&
3084 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3085 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3088 if (stcb->asoc.peers_rwnd == 0 &&
3089 (flg & SCTP_FROM_MIDDLE_BOX)) {
3090 SCTP_STAT_INCR(sctps_pdrpdizrw);
3093 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3094 sizeof(struct sctp_data_chunk));
3098 for (iii = 0; iii < sizeof(desc->data_bytes);
3100 if (ddp[iii] != desc->data_bytes[iii]) {
3101 SCTP_STAT_INCR(sctps_pdrpbadd);
3107 * We zero out the nonce so resync not
3110 tp1->rec.data.ect_nonce = 0;
3114 * this guy had a RTO calculation
3115 * pending on it, cancel it
3119 SCTP_STAT_INCR(sctps_pdrpmark);
3120 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3121 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3122 tp1->sent = SCTP_DATAGRAM_RESEND;
3124 * mark it as if we were doing a FR, since
3125 * we will be getting gap ack reports behind
3126 * the info from the router.
3128 tp1->rec.data.doing_fast_retransmit = 1;
3130 * mark the tsn with what sequences can
3133 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3134 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3136 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3139 /* restart the timer */
3140 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3141 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3142 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3145 /* fix counts and things */
3146 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3147 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3148 tp1->whoTo->flight_size,
3151 tp1->rec.data.TSN_seq);
3153 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3154 sctp_flight_size_decrease(tp1);
3155 sctp_total_flight_decrease(stcb, tp1);
3162 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3163 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3166 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3168 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3171 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3172 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3173 audit, stcb->asoc.sent_queue_retran_cnt);
3174 #ifndef SCTP_AUDITING_ENABLED
3175 stcb->asoc.sent_queue_retran_cnt = audit;
3183 struct sctp_tmit_chunk *asconf;
3185 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3187 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3192 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3193 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3194 asconf->sent = SCTP_DATAGRAM_RESEND;
3195 asconf->snd_count--;
3199 case SCTP_INITIATION:
3200 /* resend the INIT */
3201 stcb->asoc.dropped_special_cnt++;
3202 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3204 * If we can get it in, in a few attempts we do
3205 * this, otherwise we let the timer fire.
3207 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3208 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3209 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3212 case SCTP_SELECTIVE_ACK:
3213 /* resend the sack */
3214 sctp_send_sack(stcb);
3216 /* EY for nr_sacks */
3217 case SCTP_NR_SELECTIVE_ACK:
3218 sctp_send_nr_sack(stcb); /* EY resend the nr-sack */
3220 case SCTP_HEARTBEAT_REQUEST:
3221 /* resend a demand HB */
3222 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3224 * Only retransmit if we KNOW we wont destroy the
3227 (void)sctp_send_hb(stcb, 1, net);
3231 sctp_send_shutdown(stcb, net);
3233 case SCTP_SHUTDOWN_ACK:
3234 sctp_send_shutdown_ack(stcb, net);
3236 case SCTP_COOKIE_ECHO:
3238 struct sctp_tmit_chunk *cookie;
3241 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3243 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3248 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3249 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3250 cookie->sent = SCTP_DATAGRAM_RESEND;
3251 sctp_stop_all_cookie_timers(stcb);
3255 case SCTP_COOKIE_ACK:
3256 sctp_send_cookie_ack(stcb);
3258 case SCTP_ASCONF_ACK:
3259 /* resend last asconf ack */
3260 sctp_send_asconf_ack(stcb);
3262 case SCTP_FORWARD_CUM_TSN:
3263 send_forward_tsn(stcb, &stcb->asoc);
3265 /* can't do anything with these */
3266 case SCTP_PACKET_DROPPED:
3267 case SCTP_INITIATION_ACK: /* this should not happen */
3268 case SCTP_HEARTBEAT_ACK:
3269 case SCTP_ABORT_ASSOCIATION:
3270 case SCTP_OPERATION_ERROR:
3271 case SCTP_SHUTDOWN_COMPLETE:
3281 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3287 * We set things to 0xffff since this is the last delivered sequence
3288 * and we will be sending in 0 after the reset.
3291 if (number_entries) {
3292 for (i = 0; i < number_entries; i++) {
3293 temp = ntohs(list[i]);
3294 if (temp >= stcb->asoc.streamincnt) {
3297 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3301 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3302 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3305 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3309 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3313 if (number_entries == 0) {
3314 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3315 stcb->asoc.strmout[i].next_sequence_sent = 0;
3317 } else if (number_entries) {
3318 for (i = 0; i < number_entries; i++) {
3321 temp = ntohs(list[i]);
3322 if (temp >= stcb->asoc.streamoutcnt) {
3323 /* no such stream */
3326 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3329 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3333 struct sctp_stream_reset_out_request *
3334 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3336 struct sctp_association *asoc;
3337 struct sctp_stream_reset_out_req *req;
3338 struct sctp_stream_reset_out_request *r;
3339 struct sctp_tmit_chunk *chk;
3343 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3344 asoc->stream_reset_outstanding = 0;
3347 if (stcb->asoc.str_reset == NULL) {
3348 asoc->stream_reset_outstanding = 0;
3351 chk = stcb->asoc.str_reset;
3352 if (chk->data == NULL) {
3356 /* he wants a copy of the chk pointer */
3359 clen = chk->send_size;
3360 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3362 if (ntohl(r->request_seq) == seq) {
3366 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3367 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3368 /* move to the next one, there can only be a max of two */
3369 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3370 if (ntohl(r->request_seq) == seq) {
3374 /* that seq is not here */
3379 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3381 struct sctp_association *asoc;
3382 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3384 if (stcb->asoc.str_reset == NULL) {
3389 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3390 TAILQ_REMOVE(&asoc->control_send_queue,
3394 sctp_m_freem(chk->data);
3397 asoc->ctrl_queue_cnt--;
3398 sctp_free_a_chunk(stcb, chk);
3399 /* sa_ignore NO_NULL_CHK */
3400 stcb->asoc.str_reset = NULL;
3405 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3406 uint32_t seq, uint32_t action,
3407 struct sctp_stream_reset_response *respin)
3411 struct sctp_association *asoc = &stcb->asoc;
3412 struct sctp_tmit_chunk *chk;
3413 struct sctp_stream_reset_out_request *srparam;
3416 if (asoc->stream_reset_outstanding == 0) {
3420 if (seq == stcb->asoc.str_reset_seq_out) {
3421 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3423 stcb->asoc.str_reset_seq_out++;
3424 type = ntohs(srparam->ph.param_type);
3425 lparm_len = ntohs(srparam->ph.param_length);
3426 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3427 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3428 asoc->stream_reset_out_is_outstanding = 0;
3429 if (asoc->stream_reset_outstanding)
3430 asoc->stream_reset_outstanding--;
3431 if (action == SCTP_STREAM_RESET_PERFORMED) {
3433 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3435 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3437 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3438 /* Answered my request */
3439 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3440 if (asoc->stream_reset_outstanding)
3441 asoc->stream_reset_outstanding--;
3442 if (action != SCTP_STREAM_RESET_PERFORMED) {
3443 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3445 } else if (type == SCTP_STR_RESET_ADD_STREAMS) {
3446 /* Ok we now may have more streams */
3447 if (asoc->stream_reset_outstanding)
3448 asoc->stream_reset_outstanding--;
3449 if (action == SCTP_STREAM_RESET_PERFORMED) {
3450 /* Put the new streams into effect */
3451 stcb->asoc.streamoutcnt = stcb->asoc.strm_realoutsize;
3452 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_OK, stcb,
3453 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3455 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD_FAIL, stcb,
3456 (uint32_t) stcb->asoc.streamoutcnt, NULL, SCTP_SO_NOT_LOCKED);
3458 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3460 * a) Adopt the new in tsn.
3462 * c) Adopt the new out-tsn
3464 struct sctp_stream_reset_response_tsn *resp;
3465 struct sctp_forward_tsn_chunk fwdtsn;
3468 if (respin == NULL) {
3472 if (action == SCTP_STREAM_RESET_PERFORMED) {
3473 resp = (struct sctp_stream_reset_response_tsn *)respin;
3474 asoc->stream_reset_outstanding--;
3475 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3476 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3477 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3478 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3482 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3483 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3484 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3486 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3487 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3488 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3491 * EY 05/13/08 - nr_sack: to keep
3492 * nr_mapping array be consistent
3493 * with mapping_array
3495 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack) {
3496 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3497 stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.mapping_array_base_tsn;
3498 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
3500 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3501 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3503 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3504 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3508 /* get rid of the request and get the request flags */
3509 if (asoc->stream_reset_outstanding == 0) {
3510 sctp_clean_up_stream_reset(stcb);
3518 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3519 struct sctp_tmit_chunk *chk,
3520 struct sctp_stream_reset_in_request *req, int trunc)
3528 * peer wants me to send a str-reset to him for my outgoing seq's if
3531 struct sctp_association *asoc = &stcb->asoc;
3533 seq = ntohl(req->request_seq);
3534 if (asoc->str_reset_seq_in == seq) {
3536 /* Can't do it, since they exceeded our buffer size */
3537 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3538 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3539 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3540 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3541 len = ntohs(req->ph.param_length);
3542 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3543 for (i = 0; i < number_entries; i++) {
3544 temp = ntohs(req->list_of_streams[i]);
3545 req->list_of_streams[i] = temp;
3547 /* move the reset action back one */
3548 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3549 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3550 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3551 asoc->str_reset_seq_out,
3552 seq, (asoc->sending_seq - 1));
3553 asoc->stream_reset_out_is_outstanding = 1;
3554 asoc->str_reset = chk;
3555 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3556 stcb->asoc.stream_reset_outstanding++;
3558 /* Can't do it, since we have sent one out */
3559 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3560 asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
3561 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3563 asoc->str_reset_seq_in++;
3564 } else if (asoc->str_reset_seq_in - 1 == seq) {
3565 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3566 } else if (asoc->str_reset_seq_in - 2 == seq) {
3567 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3569 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3574 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3575 struct sctp_tmit_chunk *chk,
3576 struct sctp_stream_reset_tsn_request *req)
3578 /* reset all in and out and update the tsn */
3580 * A) reset my str-seq's on in and out. B) Select a receive next,
3581 * and set cum-ack to it. Also process this selected number as a
3582 * fwd-tsn as well. C) set in the response my next sending seq.
3584 struct sctp_forward_tsn_chunk fwdtsn;
3585 struct sctp_association *asoc = &stcb->asoc;
3589 seq = ntohl(req->request_seq);
3590 if (asoc->str_reset_seq_in == seq) {
3591 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3592 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3593 fwdtsn.ch.chunk_flags = 0;
3594 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3595 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3599 stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3600 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3601 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3603 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3604 stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3605 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3607 * EY 05/13/08 -nr_sack: to keep nr_mapping array consistent
3608 * with mapping array
3610 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack) {
3611 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3612 stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3613 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
3615 atomic_add_int(&stcb->asoc.sending_seq, 1);
3616 /* save off historical data for retrans */
3617 stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3618 stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3619 stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3620 stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3622 sctp_add_stream_reset_result_tsn(chk,
3623 ntohl(req->request_seq),
3624 SCTP_STREAM_RESET_PERFORMED,
3625 stcb->asoc.sending_seq,
3626 stcb->asoc.mapping_array_base_tsn);
3627 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3628 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3629 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3630 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3632 asoc->str_reset_seq_in++;
3633 } else if (asoc->str_reset_seq_in - 1 == seq) {
3634 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3635 stcb->asoc.last_sending_seq[0],
3636 stcb->asoc.last_base_tsnsent[0]
3638 } else if (asoc->str_reset_seq_in - 2 == seq) {
3639 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3640 stcb->asoc.last_sending_seq[1],
3641 stcb->asoc.last_base_tsnsent[1]
3644 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3650 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3651 struct sctp_tmit_chunk *chk,
3652 struct sctp_stream_reset_out_request *req, int trunc)
3655 int number_entries, len;
3656 struct sctp_association *asoc = &stcb->asoc;
3658 seq = ntohl(req->request_seq);
3660 /* now if its not a duplicate we process it */
3661 if (asoc->str_reset_seq_in == seq) {
3662 len = ntohs(req->ph.param_length);
3663 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3665 * the sender is resetting, handle the list issue.. we must
3666 * a) verify if we can do the reset, if so no problem b) If
3667 * we can't do the reset we must copy the request. c) queue
3668 * it, and setup the data in processor to trigger it off
3669 * when needed and dequeue all the queued data.
3671 tsn = ntohl(req->send_reset_at_tsn);
3673 /* move the reset action back one */
3674 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3676 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3677 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3678 } else if ((tsn == asoc->cumulative_tsn) ||
3679 (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) {
3680 /* we can do it now */
3681 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3682 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3683 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3686 * we must queue it up and thus wait for the TSN's
3687 * to arrive that are at or before tsn
3689 struct sctp_stream_reset_list *liste;
3692 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3693 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3694 siz, SCTP_M_STRESET);
3695 if (liste == NULL) {
3696 /* gak out of memory */
3697 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3698 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3702 liste->number_entries = number_entries;
3703 memcpy(&liste->req, req,
3704 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3705 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3706 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3707 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3709 asoc->str_reset_seq_in++;
3710 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3712 * one seq back, just echo back last action since my
3713 * response was lost.
3715 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3716 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3718 * two seq back, just echo back last action since my
3719 * response was lost.
3721 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3723 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3728 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3729 struct sctp_stream_reset_add_strm *str_add)
3732 * Peer is requesting to add more streams. If its within our
3733 * max-streams we will allow it.
3735 uint16_t num_stream, i;
3737 struct sctp_association *asoc = &stcb->asoc;
3738 struct sctp_queued_to_read *ctl;
3740 /* Get the number. */
3741 seq = ntohl(str_add->request_seq);
3742 num_stream = ntohs(str_add->number_of_streams);
3743 /* Now what would be the new total? */
3744 if (asoc->str_reset_seq_in == seq) {
3745 num_stream += stcb->asoc.streamincnt;
3746 if (num_stream > stcb->asoc.max_inbound_streams) {
3747 /* We must reject it they ask for to many */
3749 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3750 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3751 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3753 /* Ok, we can do that :-) */
3754 struct sctp_stream_in *oldstrm;
3756 /* save off the old */
3757 oldstrm = stcb->asoc.strmin;
3758 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3759 (num_stream * sizeof(struct sctp_stream_in)),
3761 if (stcb->asoc.strmin == NULL) {
3762 stcb->asoc.strmin = oldstrm;
3765 /* copy off the old data */
3766 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3767 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3768 stcb->asoc.strmin[i].stream_no = i;
3769 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
3770 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
3771 /* now anything on those queues? */
3772 while (TAILQ_EMPTY(&oldstrm[i].inqueue) == 0) {
3773 ctl = TAILQ_FIRST(&oldstrm[i].inqueue);
3774 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
3775 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
3778 /* Init the new streams */
3779 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3780 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
3781 stcb->asoc.strmin[i].stream_no = i;
3782 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3783 stcb->asoc.strmin[i].delivery_started = 0;
3785 SCTP_FREE(oldstrm, SCTP_M_STRMI);
3786 /* update the size */
3787 stcb->asoc.streamincnt = num_stream;
3789 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3790 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3791 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3792 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK, stcb,
3793 (uint32_t) stcb->asoc.streamincnt, NULL, SCTP_SO_NOT_LOCKED);
3795 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3797 * one seq back, just echo back last action since my
3798 * response was lost.
3800 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3801 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3803 * two seq back, just echo back last action since my
3804 * response was lost.
3806 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3808 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3814 __attribute__((noinline))
3817 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3818 struct sctp_stream_reset_out_req *sr_req)
3820 int chk_length, param_len, ptype;
3821 struct sctp_paramhdr pstore;
3822 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3827 struct sctp_tmit_chunk *chk;
3828 struct sctp_chunkhdr *ch;
3829 struct sctp_paramhdr *ph;
3833 /* now it may be a reset or a reset-response */
3834 chk_length = ntohs(sr_req->ch.chunk_length);
3836 /* setup for adding the response */
3837 sctp_alloc_a_chunk(stcb, chk);
3841 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3842 chk->rec.chunk_id.can_take_data = 0;
3843 chk->asoc = &stcb->asoc;
3844 chk->no_fr_allowed = 0;
3845 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3846 chk->book_size_scale = 0;
3847 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3848 if (chk->data == NULL) {
3851 sctp_m_freem(chk->data);
3854 sctp_free_a_chunk(stcb, chk);
3857 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3859 /* setup chunk parameters */
3860 chk->sent = SCTP_DATAGRAM_UNSENT;
3862 chk->whoTo = stcb->asoc.primary_destination;
3863 atomic_add_int(&chk->whoTo->ref_count, 1);
3865 ch = mtod(chk->data, struct sctp_chunkhdr *);
3866 ch->chunk_type = SCTP_STREAM_RESET;
3867 ch->chunk_flags = 0;
3868 ch->chunk_length = htons(chk->send_size);
3869 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3870 offset += sizeof(struct sctp_chunkhdr);
3871 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
3872 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
3875 param_len = ntohs(ph->param_length);
3876 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
3880 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
3881 (uint8_t *) & cstore);
3882 ptype = ntohs(ph->param_type);
3884 if (param_len > (int)sizeof(cstore)) {
3890 if (num_param > SCTP_MAX_RESET_PARAMS) {
3891 /* hit the max of parameters already sorry.. */
3894 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
3895 struct sctp_stream_reset_out_request *req_out;
3897 req_out = (struct sctp_stream_reset_out_request *)ph;
3899 if (stcb->asoc.stream_reset_outstanding) {
3900 seq = ntohl(req_out->response_seq);
3901 if (seq == stcb->asoc.str_reset_seq_out) {
3903 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
3906 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
3907 } else if (ptype == SCTP_STR_RESET_ADD_STREAMS) {
3908 struct sctp_stream_reset_add_strm *str_add;
3910 str_add = (struct sctp_stream_reset_add_strm *)ph;
3912 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
3913 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
3914 struct sctp_stream_reset_in_request *req_in;
3918 req_in = (struct sctp_stream_reset_in_request *)ph;
3920 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
3921 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
3922 struct sctp_stream_reset_tsn_request *req_tsn;
3925 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
3927 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
3929 goto strres_nochunk;
3933 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
3934 struct sctp_stream_reset_response *resp;
3937 resp = (struct sctp_stream_reset_response *)ph;
3938 seq = ntohl(resp->response_seq);
3939 result = ntohl(resp->result);
3940 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
3942 goto strres_nochunk;
3947 offset += SCTP_SIZE32(param_len);
3948 chk_length -= SCTP_SIZE32(param_len);
3951 /* we have no response free the stuff */
3952 goto strres_nochunk;
3954 /* ok we have a chunk to link in */
3955 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
3958 stcb->asoc.ctrl_queue_cnt++;
3963 * Handle a router or endpoints report of a packet loss, there are two ways
3964 * to handle this, either we get the whole packet and must disect it
3965 * ourselves (possibly with truncation and or corruption) or it is a summary
3966 * from a middle box that did the disectting for us.
3969 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
3970 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
3972 uint32_t bottle_bw, on_queue;
3976 struct sctp_chunk_desc desc;
3977 struct sctp_chunkhdr *ch;
3979 chlen = ntohs(cp->ch.chunk_length);
3980 chlen -= sizeof(struct sctp_pktdrop_chunk);
3981 /* XXX possible chlen underflow */
3984 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
3985 SCTP_STAT_INCR(sctps_pdrpbwrpt);
3987 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
3988 chlen -= sizeof(struct sctphdr);
3989 /* XXX possible chlen underflow */
3990 memset(&desc, 0, sizeof(desc));
3992 trunc_len = (uint16_t) ntohs(cp->trunc_len);
3993 if (trunc_len > limit) {
3996 /* now the chunks themselves */
3997 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
3998 desc.chunk_type = ch->chunk_type;
3999 /* get amount we need to move */
4000 at = ntohs(ch->chunk_length);
4001 if (at < sizeof(struct sctp_chunkhdr)) {
4002 /* corrupt chunk, maybe at the end? */
4003 SCTP_STAT_INCR(sctps_pdrpcrupt);
4006 if (trunc_len == 0) {
4007 /* we are supposed to have all of it */
4009 /* corrupt skip it */
4010 SCTP_STAT_INCR(sctps_pdrpcrupt);
4014 /* is there enough of it left ? */
4015 if (desc.chunk_type == SCTP_DATA) {
4016 if (chlen < (sizeof(struct sctp_data_chunk) +
4017 sizeof(desc.data_bytes))) {
4021 if (chlen < sizeof(struct sctp_chunkhdr)) {
4026 if (desc.chunk_type == SCTP_DATA) {
4027 /* can we get out the tsn? */
4028 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4029 SCTP_STAT_INCR(sctps_pdrpmbda);
4031 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4033 struct sctp_data_chunk *dcp;
4037 dcp = (struct sctp_data_chunk *)ch;
4038 ddp = (uint8_t *) (dcp + 1);
4039 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4040 desc.data_bytes[iii] = ddp[iii];
4042 desc.tsn_ifany = dcp->dp.tsn;
4044 /* nope we are done. */
4045 SCTP_STAT_INCR(sctps_pdrpnedat);
4049 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4050 SCTP_STAT_INCR(sctps_pdrpmbct);
4053 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4054 SCTP_STAT_INCR(sctps_pdrppdbrk);
4057 if (SCTP_SIZE32(at) > chlen) {
4060 chlen -= SCTP_SIZE32(at);
4061 if (chlen < sizeof(struct sctp_chunkhdr)) {
4062 /* done, none left */
4065 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4067 /* Now update any rwnd --- possibly */
4068 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4069 /* From a peer, we get a rwnd report */
4072 SCTP_STAT_INCR(sctps_pdrpfehos);
4074 bottle_bw = ntohl(cp->bottle_bw);
4075 on_queue = ntohl(cp->current_onq);
4076 if (bottle_bw && on_queue) {
4077 /* a rwnd report is in here */
4078 if (bottle_bw > on_queue)
4079 a_rwnd = bottle_bw - on_queue;
4084 stcb->asoc.peers_rwnd = 0;
4086 if (a_rwnd > stcb->asoc.total_flight) {
4087 stcb->asoc.peers_rwnd =
4088 a_rwnd - stcb->asoc.total_flight;
4090 stcb->asoc.peers_rwnd = 0;
4092 if (stcb->asoc.peers_rwnd <
4093 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4094 /* SWS sender side engages */
4095 stcb->asoc.peers_rwnd = 0;
4100 SCTP_STAT_INCR(sctps_pdrpfmbox);
4103 /* now middle boxes in sat networks get a cwnd bump */
4104 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4105 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4106 (stcb->asoc.sat_network)) {
4108 * This is debateable but for sat networks it makes sense
4109 * Note if a T3 timer has went off, we will prohibit any
4110 * changes to cwnd until we exit the t3 loss recovery.
4112 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4113 net, cp, &bottle_bw, &on_queue);
4118 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4119 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4120 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4121 * length of the complete packet outputs: - length: modified to remaining
4122 * length after control processing - netp: modified to new sctp_nets after
4123 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4124 * bad packet,...) otherwise return the tcb for this packet
4127 __attribute__((noinline))
4129 static struct sctp_tcb *
4130 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4131 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4132 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4133 uint32_t vrf_id, uint16_t port)
4135 struct sctp_association *asoc;
4137 int num_chunks = 0; /* number of control chunks processed */
4138 uint32_t chk_length;
4140 int abort_no_unlock = 0;
4143 * How big should this be, and should it be alloc'd? Lets try the
4144 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4145 * until we get into jumbo grams and such..
4147 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4148 struct sctp_tcb *locked_tcb = stcb;
4150 uint32_t auth_offset = 0, auth_len = 0;
4151 int auth_skipped = 0;
4154 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4159 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4160 iphlen, *offset, length, stcb);
4162 /* validate chunk header length... */
4163 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4164 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4165 ntohs(ch->chunk_length));
4167 SCTP_TCB_UNLOCK(locked_tcb);
4172 * validate the verification tag
4174 vtag_in = ntohl(sh->v_tag);
4177 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4179 if (ch->chunk_type == SCTP_INITIATION) {
4180 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4181 ntohs(ch->chunk_length), vtag_in);
4183 /* protocol error- silently discard... */
4184 SCTP_STAT_INCR(sctps_badvtag);
4186 SCTP_TCB_UNLOCK(locked_tcb);
4190 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4192 * If there is no stcb, skip the AUTH chunk and process
4193 * later after a stcb is found (to validate the lookup was
4196 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4198 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4199 /* save this chunk for later processing */
4201 auth_offset = *offset;
4202 auth_len = ntohs(ch->chunk_length);
4204 /* (temporarily) move past this chunk */
4205 *offset += SCTP_SIZE32(auth_len);
4206 if (*offset >= length) {
4207 /* no more data left in the mbuf chain */
4210 SCTP_TCB_UNLOCK(locked_tcb);
4214 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4215 sizeof(struct sctp_chunkhdr), chunk_buf);
4221 SCTP_TCB_UNLOCK(locked_tcb);
4225 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4226 goto process_control_chunks;
4229 * first check if it's an ASCONF with an unknown src addr we
4230 * need to look inside to find the association
4232 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4233 struct sctp_chunkhdr *asconf_ch = ch;
4234 uint32_t asconf_offset = 0, asconf_len = 0;
4236 /* inp's refcount may be reduced */
4237 SCTP_INP_INCR_REF(inp);
4239 asconf_offset = *offset;
4241 asconf_len = ntohs(asconf_ch->chunk_length);
4242 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4244 stcb = sctp_findassociation_ep_asconf(m, iphlen,
4245 *offset, sh, &inp, netp, vrf_id);
4248 asconf_offset += SCTP_SIZE32(asconf_len);
4249 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4250 sizeof(struct sctp_chunkhdr), chunk_buf);
4251 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4254 * reduce inp's refcount if not reduced in
4255 * sctp_findassociation_ep_asconf().
4257 SCTP_INP_DECR_REF(inp);
4262 /* now go back and verify any auth chunk to be sure */
4263 if (auth_skipped && (stcb != NULL)) {
4264 struct sctp_auth_chunk *auth;
4266 auth = (struct sctp_auth_chunk *)
4267 sctp_m_getptr(m, auth_offset,
4268 auth_len, chunk_buf);
4271 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4273 /* auth HMAC failed so dump it */
4276 SCTP_TCB_UNLOCK(locked_tcb);
4280 /* remaining chunks are HMAC checked */
4281 stcb->asoc.authenticated = 1;
4286 /* no association, so it's out of the blue... */
4287 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
4291 SCTP_TCB_UNLOCK(locked_tcb);
4296 /* ABORT and SHUTDOWN can use either v_tag... */
4297 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4298 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4299 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4300 if ((vtag_in == asoc->my_vtag) ||
4301 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4302 (vtag_in == asoc->peer_vtag))) {
4305 /* drop this packet... */
4306 SCTP_STAT_INCR(sctps_badvtag);
4308 SCTP_TCB_UNLOCK(locked_tcb);
4312 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4313 if (vtag_in != asoc->my_vtag) {
4315 * this could be a stale SHUTDOWN-ACK or the
4316 * peer never got the SHUTDOWN-COMPLETE and
4317 * is still hung; we have started a new asoc
4318 * but it won't complete until the shutdown
4322 SCTP_TCB_UNLOCK(locked_tcb);
4324 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4325 NULL, vrf_id, port);
4329 /* for all other chunks, vtag must match */
4330 if (vtag_in != asoc->my_vtag) {
4331 /* invalid vtag... */
4332 SCTPDBG(SCTP_DEBUG_INPUT3,
4333 "invalid vtag: %xh, expect %xh\n",
4334 vtag_in, asoc->my_vtag);
4335 SCTP_STAT_INCR(sctps_badvtag);
4337 SCTP_TCB_UNLOCK(locked_tcb);
4343 } /* end if !SCTP_COOKIE_ECHO */
4345 * process all control chunks...
4347 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4349 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4350 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4351 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4352 /* implied cookie-ack.. we must have lost the ack */
4353 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4354 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4355 stcb->asoc.overall_error_count,
4357 SCTP_FROM_SCTP_INPUT,
4360 stcb->asoc.overall_error_count = 0;
4361 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4364 process_control_chunks:
4365 while (IS_SCTP_CONTROL(ch)) {
4366 /* validate chunk length */
4367 chk_length = ntohs(ch->chunk_length);
4368 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4369 ch->chunk_type, chk_length);
4370 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4371 if (chk_length < sizeof(*ch) ||
4372 (*offset + (int)chk_length) > length) {
4375 SCTP_TCB_UNLOCK(locked_tcb);
4379 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4381 * INIT-ACK only gets the init ack "header" portion only
4382 * because we don't have to process the peer's COOKIE. All
4383 * others get a complete chunk.
4385 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4386 (ch->chunk_type == SCTP_INITIATION)) {
4387 /* get an init-ack chunk */
4388 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4389 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4393 SCTP_TCB_UNLOCK(locked_tcb);
4398 /* For cookies and all other chunks. */
4399 if (chk_length > sizeof(chunk_buf)) {
4401 * use just the size of the chunk buffer so
4402 * the front part of our chunks fit in
4403 * contiguous space up to the chunk buffer
4404 * size (508 bytes). For chunks that need to
4405 * get more than that they must use the
4406 * sctp_m_getptr() function or other means
4407 * (e.g. know how to parse mbuf chains).
4408 * Cookies do this already.
4410 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4411 (sizeof(chunk_buf) - 4),
4416 SCTP_TCB_UNLOCK(locked_tcb);
4421 /* We can fit it all */
4422 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4423 chk_length, chunk_buf);
4425 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4428 SCTP_TCB_UNLOCK(locked_tcb);
4435 /* Save off the last place we got a control from */
4437 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4439 * allow last_control to be NULL if
4440 * ASCONF... ASCONF processing will find the
4443 if ((netp != NULL) && (*netp != NULL))
4444 stcb->asoc.last_control_chunk_from = *netp;
4447 #ifdef SCTP_AUDITING_ENABLED
4448 sctp_audit_log(0xB0, ch->chunk_type);
4451 /* check to see if this chunk required auth, but isn't */
4452 if ((stcb != NULL) &&
4453 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4454 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4455 !stcb->asoc.authenticated) {
4456 /* "silently" ignore */
4457 SCTP_STAT_INCR(sctps_recvauthmissing);
4460 switch (ch->chunk_type) {
4461 case SCTP_INITIATION:
4462 /* must be first and only chunk */
4463 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4464 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4465 /* We are not interested anymore? */
4466 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4468 * collision case where we are
4469 * sending to them too
4474 SCTP_TCB_UNLOCK(locked_tcb);
4480 if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) ||
4482 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4485 SCTP_TCB_UNLOCK(locked_tcb);
4489 if ((stcb != NULL) &&
4490 (SCTP_GET_STATE(&stcb->asoc) ==
4491 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4492 sctp_send_shutdown_ack(stcb,
4493 stcb->asoc.primary_destination);
4495 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4497 SCTP_TCB_UNLOCK(locked_tcb);
4502 sctp_handle_init(m, iphlen, *offset, sh,
4503 (struct sctp_init_chunk *)ch, inp,
4504 stcb, *netp, &abort_no_unlock, vrf_id, port);
4506 if (abort_no_unlock)
4511 SCTP_TCB_UNLOCK(locked_tcb);
4515 case SCTP_PAD_CHUNK:
4517 case SCTP_INITIATION_ACK:
4518 /* must be first and only chunk */
4519 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4520 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4521 /* We are not interested anymore */
4522 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4526 SCTP_TCB_UNLOCK(locked_tcb);
4530 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4531 so = SCTP_INP_SO(inp);
4532 atomic_add_int(&stcb->asoc.refcnt, 1);
4533 SCTP_TCB_UNLOCK(stcb);
4534 SCTP_SOCKET_LOCK(so, 1);
4535 SCTP_TCB_LOCK(stcb);
4536 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4538 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4539 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4540 SCTP_SOCKET_UNLOCK(so, 1);
4546 if ((num_chunks > 1) ||
4547 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4550 SCTP_TCB_UNLOCK(locked_tcb);
4554 if ((netp) && (*netp)) {
4555 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4556 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4561 * Special case, I must call the output routine to
4562 * get the cookie echoed
4564 if (abort_no_unlock)
4567 if ((stcb) && ret == 0)
4568 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4571 SCTP_TCB_UNLOCK(locked_tcb);
4575 case SCTP_SELECTIVE_ACK:
4576 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4577 SCTP_STAT_INCR(sctps_recvsacks);
4579 struct sctp_sack_chunk *sack;
4581 uint32_t a_rwnd, cum_ack;
4585 if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) {
4586 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n");
4589 SCTP_TCB_UNLOCK(locked_tcb);
4593 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4595 * If we have sent a shutdown-ack, we will pay no
4596 * attention to a sack sent in to us since
4597 * we don't care anymore.
4601 sack = (struct sctp_sack_chunk *)ch;
4602 nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM;
4603 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4604 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4605 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4606 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4611 stcb->asoc.seen_a_sack_this_pkt = 1;
4612 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4614 ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
4615 (cum_ack == stcb->asoc.last_acked_seq)) &&
4616 (stcb->asoc.saw_sack_with_frags == 0) &&
4617 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4620 * We have a SIMPLE sack having no
4621 * prior segments and data on sent
4622 * queue to be acked.. Use the
4623 * faster path sack processing. We
4624 * also allow window update sacks
4625 * with no missing segments to go
4628 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
4632 sctp_handle_sack(m, *offset,
4633 sack, stcb, *netp, &abort_now, chk_length, a_rwnd);
4635 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4636 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4637 (stcb->asoc.stream_queue_cnt == 0)) {
4638 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4641 /* ABORT signal from sack processing */
4648 * EY - nr_sack: If the received chunk is an
4651 case SCTP_NR_SELECTIVE_ACK:
4652 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4653 SCTP_STAT_INCR(sctps_recvsacks);
4655 struct sctp_nr_sack_chunk *nr_sack;
4657 uint32_t a_rwnd, cum_ack;
4658 uint16_t num_seg, num_nr_seg;
4661 if ((stcb == NULL) || (chk_length < sizeof(struct sctp_nr_sack_chunk))) {
4662 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on nr_sack chunk, too small\n");
4666 SCTP_TCB_UNLOCK(locked_tcb);
4671 * EY nr_sacks have not been negotiated but
4672 * the peer end sent an nr_sack, silently
4675 if (!(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)) {
4678 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4680 * If we have sent a shutdown-ack, we will pay no
4681 * attention to a sack sent in to us since
4682 * we don't care anymore.
4684 goto ignore_nr_sack;
4686 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4687 nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM;
4689 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4690 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4691 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4692 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4693 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4698 stcb->asoc.seen_a_sack_this_pkt = 1;
4699 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4701 ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
4702 (cum_ack == stcb->asoc.last_acked_seq)) &&
4703 (stcb->asoc.saw_sack_with_frags == 0) &&
4704 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4707 * We have a SIMPLE sack having no
4708 * prior segments and data on sent
4709 * queue to be acked.. Use the
4710 * faster path sack processing. We
4711 * also allow window update sacks
4712 * with no missing segments to go
4715 sctp_express_handle_nr_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
4719 sctp_handle_nr_sack(m, *offset,
4720 nr_sack, stcb, *netp, &abort_now, chk_length, a_rwnd);
4722 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4723 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4724 (stcb->asoc.stream_queue_cnt == 0)) {
4725 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4728 /* ABORT signal from sack processing */
4735 case SCTP_HEARTBEAT_REQUEST:
4736 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4737 if ((stcb) && netp && *netp) {
4738 SCTP_STAT_INCR(sctps_recvheartbeat);
4739 sctp_send_heartbeat_ack(stcb, m, *offset,
4742 /* He's alive so give him credit */
4743 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4744 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4745 stcb->asoc.overall_error_count,
4747 SCTP_FROM_SCTP_INPUT,
4750 stcb->asoc.overall_error_count = 0;
4753 case SCTP_HEARTBEAT_ACK:
4754 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4755 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4759 SCTP_TCB_UNLOCK(locked_tcb);
4763 /* He's alive so give him credit */
4764 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4765 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4766 stcb->asoc.overall_error_count,
4768 SCTP_FROM_SCTP_INPUT,
4771 stcb->asoc.overall_error_count = 0;
4772 SCTP_STAT_INCR(sctps_recvheartbeatack);
4774 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4777 case SCTP_ABORT_ASSOCIATION:
4778 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4780 if ((stcb) && netp && *netp)
4781 sctp_handle_abort((struct sctp_abort_chunk *)ch,
4787 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4789 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
4792 SCTP_TCB_UNLOCK(locked_tcb);
4796 if (netp && *netp) {
4799 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4800 stcb, *netp, &abort_flag);
4807 case SCTP_SHUTDOWN_ACK:
4808 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
4809 if ((stcb) && (netp) && (*netp))
4810 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4815 case SCTP_OPERATION_ERROR:
4816 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
4817 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
4823 case SCTP_COOKIE_ECHO:
4824 SCTPDBG(SCTP_DEBUG_INPUT3,
4825 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
4826 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4829 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4830 /* We are not interested anymore */
4836 * First are we accepting? We do this again here
4837 * sincen it is possible that a previous endpoint
4838 * WAS listening responded to a INIT-ACK and then
4839 * closed. We opened and bound.. and are now no
4843 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
4844 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4845 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4847 struct sctp_paramhdr *phdr;
4849 oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4850 0, M_DONTWAIT, 1, MT_DATA);
4852 SCTP_BUF_LEN(oper) =
4853 sizeof(struct sctp_paramhdr);
4855 struct sctp_paramhdr *);
4857 htons(SCTP_CAUSE_OUT_OF_RESC);
4858 phdr->param_length =
4859 htons(sizeof(struct sctp_paramhdr));
4861 sctp_abort_association(inp, stcb, m,
4862 iphlen, sh, oper, vrf_id, port);
4867 struct mbuf *ret_buf;
4868 struct sctp_inpcb *linp;
4877 SCTP_ASOC_CREATE_LOCK(linp);
4881 sctp_handle_cookie_echo(m, iphlen,
4883 (struct sctp_cookie_echo_chunk *)ch,
4895 SCTP_ASOC_CREATE_UNLOCK(linp);
4897 if (ret_buf == NULL) {
4899 SCTP_TCB_UNLOCK(locked_tcb);
4901 SCTPDBG(SCTP_DEBUG_INPUT3,
4902 "GAK, null buffer\n");
4907 /* if AUTH skipped, see if it verified... */
4912 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4914 * Restart the timer if we have
4917 struct sctp_tmit_chunk *chk;
4919 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
4921 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4922 stcb->sctp_ep, stcb,
4928 case SCTP_COOKIE_ACK:
4929 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
4930 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
4932 SCTP_TCB_UNLOCK(locked_tcb);
4936 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4937 /* We are not interested anymore */
4938 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4941 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4942 so = SCTP_INP_SO(inp);
4943 atomic_add_int(&stcb->asoc.refcnt, 1);
4944 SCTP_TCB_UNLOCK(stcb);
4945 SCTP_SOCKET_LOCK(so, 1);
4946 SCTP_TCB_LOCK(stcb);
4947 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4949 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4950 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4951 SCTP_SOCKET_UNLOCK(so, 1);
4957 /* He's alive so give him credit */
4958 if ((stcb) && netp && *netp) {
4959 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4960 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4961 stcb->asoc.overall_error_count,
4963 SCTP_FROM_SCTP_INPUT,
4966 stcb->asoc.overall_error_count = 0;
4967 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
4971 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
4972 /* He's alive so give him credit */
4973 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
4976 SCTP_TCB_UNLOCK(locked_tcb);
4982 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4983 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4984 stcb->asoc.overall_error_count,
4986 SCTP_FROM_SCTP_INPUT,
4989 stcb->asoc.overall_error_count = 0;
4990 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
4995 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
4996 /* He's alive so give him credit */
4997 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5000 SCTP_TCB_UNLOCK(locked_tcb);
5006 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5007 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5008 stcb->asoc.overall_error_count,
5010 SCTP_FROM_SCTP_INPUT,
5013 stcb->asoc.overall_error_count = 0;
5014 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
5017 case SCTP_SHUTDOWN_COMPLETE:
5018 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
5019 /* must be first and only chunk */
5020 if ((num_chunks > 1) ||
5021 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5024 SCTP_TCB_UNLOCK(locked_tcb);
5028 if ((stcb) && netp && *netp) {
5029 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5036 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5037 /* He's alive so give him credit */
5039 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5040 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5041 stcb->asoc.overall_error_count,
5043 SCTP_FROM_SCTP_INPUT,
5046 stcb->asoc.overall_error_count = 0;
5047 sctp_handle_asconf(m, *offset,
5048 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5052 case SCTP_ASCONF_ACK:
5053 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5054 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5057 SCTP_TCB_UNLOCK(locked_tcb);
5062 if ((stcb) && netp && *netp) {
5063 /* 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_ack(m, *offset,
5073 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5074 if (abort_no_unlock)
5078 case SCTP_FORWARD_CUM_TSN:
5079 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5080 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5083 SCTP_TCB_UNLOCK(locked_tcb);
5088 /* He's alive so give him credit */
5092 stcb->asoc.overall_error_count = 0;
5093 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5094 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5095 stcb->asoc.overall_error_count,
5097 SCTP_FROM_SCTP_INPUT,
5101 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5102 /* We are not interested anymore */
5103 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5104 so = SCTP_INP_SO(inp);
5105 atomic_add_int(&stcb->asoc.refcnt, 1);
5106 SCTP_TCB_UNLOCK(stcb);
5107 SCTP_SOCKET_LOCK(so, 1);
5108 SCTP_TCB_LOCK(stcb);
5109 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5111 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5112 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5113 SCTP_SOCKET_UNLOCK(so, 1);
5118 sctp_handle_forward_tsn(stcb,
5119 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5124 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5125 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5126 stcb->asoc.overall_error_count,
5128 SCTP_FROM_SCTP_INPUT,
5131 stcb->asoc.overall_error_count = 0;
5136 case SCTP_STREAM_RESET:
5137 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5138 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5141 SCTP_TCB_UNLOCK(locked_tcb);
5146 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5147 /* We are not interested anymore */
5148 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5149 so = SCTP_INP_SO(inp);
5150 atomic_add_int(&stcb->asoc.refcnt, 1);
5151 SCTP_TCB_UNLOCK(stcb);
5152 SCTP_SOCKET_LOCK(so, 1);
5153 SCTP_TCB_LOCK(stcb);
5154 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5156 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5157 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5158 SCTP_SOCKET_UNLOCK(so, 1);
5163 if (stcb->asoc.peer_supports_strreset == 0) {
5165 * hmm, peer should have announced this, but
5166 * we will turn it on since he is sending us
5169 stcb->asoc.peer_supports_strreset = 1;
5171 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5172 /* stop processing */
5177 case SCTP_PACKET_DROPPED:
5178 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5179 /* re-get it all please */
5180 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5183 SCTP_TCB_UNLOCK(locked_tcb);
5188 if (ch && (stcb) && netp && (*netp)) {
5189 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5191 min(chk_length, (sizeof(chunk_buf) - 4)));
5196 case SCTP_AUTHENTICATION:
5197 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5198 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5202 /* save the first AUTH for later processing */
5203 if (auth_skipped == 0) {
5204 auth_offset = *offset;
5205 auth_len = chk_length;
5208 /* skip this chunk (temporarily) */
5211 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5212 (chk_length > (sizeof(struct sctp_auth_chunk) +
5213 SCTP_AUTH_DIGEST_LEN_MAX))) {
5216 SCTP_TCB_UNLOCK(locked_tcb);
5221 if (got_auth == 1) {
5222 /* skip this chunk... it's already auth'd */
5226 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5228 /* auth HMAC failed so dump the packet */
5232 /* remaining chunks are HMAC checked */
5233 stcb->asoc.authenticated = 1;
5239 /* it's an unknown chunk! */
5240 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5242 struct sctp_paramhdr *phd;
5244 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5245 0, M_DONTWAIT, 1, MT_DATA);
5247 phd = mtod(mm, struct sctp_paramhdr *);
5249 * We cheat and use param type since
5250 * we did not bother to define a
5251 * error cause struct. They are the
5252 * same basic format with different
5255 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5256 phd->param_length = htons(chk_length + sizeof(*phd));
5257 SCTP_BUF_LEN(mm) = sizeof(*phd);
5258 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
5260 if (SCTP_BUF_NEXT(mm)) {
5261 #ifdef SCTP_MBUF_LOGGING
5262 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5265 mat = SCTP_BUF_NEXT(mm);
5267 if (SCTP_BUF_IS_EXTENDED(mat)) {
5268 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5270 mat = SCTP_BUF_NEXT(mat);
5274 sctp_queue_op_err(stcb, mm);
5280 if ((ch->chunk_type & 0x80) == 0) {
5281 /* discard this packet */
5284 } /* else skip this bad chunk and continue... */
5286 } /* switch (ch->chunk_type) */
5290 /* get the next chunk */
5291 *offset += SCTP_SIZE32(chk_length);
5292 if (*offset >= length) {
5293 /* no more data left in the mbuf chain */
5296 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5297 sizeof(struct sctp_chunkhdr), chunk_buf);
5300 SCTP_TCB_UNLOCK(locked_tcb);
5307 if (asconf_cnt > 0 && stcb != NULL) {
5308 sctp_send_asconf_ack(stcb);
5315 * Process the ECN bits we have something set so we must look to see if it is
5316 * ECN(0) or ECN(1) or CE
5319 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
5322 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
5324 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
5326 * we only add to the nonce sum for ECT1, ECT0 does not
5327 * change the NS bit (that we have yet to find a way to send
5331 /* ECN Nonce stuff */
5332 stcb->asoc.receiver_nonce_sum++;
5333 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
5336 * Drag up the last_echo point if cumack is larger since we
5337 * don't want the point falling way behind by more than
5338 * 2^^31 and then having it be incorrect.
5340 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
5341 stcb->asoc.last_echo_tsn, MAX_TSN)) {
5342 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
5344 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
5346 * Drag up the last_echo point if cumack is larger since we
5347 * don't want the point falling way behind by more than
5348 * 2^^31 and then having it be incorrect.
5350 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
5351 stcb->asoc.last_echo_tsn, MAX_TSN)) {
5352 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
5358 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
5359 uint32_t high_tsn, uint8_t ecn_bits)
5361 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
5363 * we possibly must notify the sender that a congestion
5364 * window reduction is in order. We do this by adding a ECNE
5365 * chunk to the output chunk queue. The incoming CWR will
5366 * remove this chunk.
5368 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
5370 /* Yep, we need to add a ECNE */
5371 sctp_send_ecn_echo(stcb, net, high_tsn);
5372 stcb->asoc.last_echo_tsn = high_tsn;
5379 sctp_validate_no_locks(struct sctp_inpcb *inp)
5381 struct sctp_tcb *stcb;
5383 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5384 if (mtx_owned(&stcb->tcb_mtx)) {
5385 panic("Own lock on stcb at return from input");
5393 * common input chunk processing (v4 and v6)
5396 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5397 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5398 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5399 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5402 * Control chunk processing
5405 int fwd_tsn_seen = 0, data_processed = 0;
5406 struct mbuf *m = *mm;
5410 SCTP_STAT_INCR(sctps_recvdatagrams);
5411 #ifdef SCTP_AUDITING_ENABLED
5412 sctp_audit_log(0xE0, 1);
5413 sctp_auditing(0, inp, stcb, net);
5416 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5417 m, iphlen, offset, length, stcb);
5419 /* always clear this before beginning a packet */
5420 stcb->asoc.authenticated = 0;
5421 stcb->asoc.seen_a_sack_this_pkt = 0;
5422 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5423 stcb, stcb->asoc.state);
5425 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5426 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5428 * If we hit here, we had a ref count
5429 * up when the assoc was aborted and the
5430 * timer is clearing out the assoc, we should
5431 * NOT respond to any packet.. its OOTB.
5433 SCTP_TCB_UNLOCK(stcb);
5434 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5439 if (IS_SCTP_CONTROL(ch)) {
5440 /* process the control portion of the SCTP packet */
5441 /* sa_ignore NO_NULL_CHK */
5442 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5443 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5446 * This covers us if the cookie-echo was there and
5447 * it changes our INP.
5449 inp = stcb->sctp_ep;
5450 if ((net) && (port)) {
5451 if (net->port == 0) {
5452 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5459 * no control chunks, so pre-process DATA chunks (these
5460 * checks are taken care of by control processing)
5464 * if DATA only packet, and auth is required, then punt...
5465 * can't have authenticated without any AUTH (control)
5468 if ((stcb != NULL) &&
5469 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5470 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5471 /* "silently" ignore */
5472 SCTP_STAT_INCR(sctps_recvauthmissing);
5473 SCTP_TCB_UNLOCK(stcb);
5477 /* out of the blue DATA chunk */
5478 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5482 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5483 /* v_tag mismatch! */
5484 SCTP_STAT_INCR(sctps_badvtag);
5485 SCTP_TCB_UNLOCK(stcb);
5492 * no valid TCB for this packet, or we found it's a bad
5493 * packet while processing control, or we're done with this
5494 * packet (done or skip rest of data), so we drop it...
5499 * DATA chunk processing
5501 /* plow through the data chunks while length > offset */
5504 * Rest should be DATA only. Check authentication state if AUTH for
5507 if ((length > offset) &&
5509 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5510 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5511 !stcb->asoc.authenticated) {
5512 /* "silently" ignore */
5513 SCTP_STAT_INCR(sctps_recvauthmissing);
5514 SCTPDBG(SCTP_DEBUG_AUTH1,
5515 "Data chunk requires AUTH, skipped\n");
5518 if (length > offset) {
5522 * First check to make sure our state is correct. We would
5523 * not get here unless we really did have a tag, so we don't
5524 * abort if this happens, just dump the chunk silently.
5526 switch (SCTP_GET_STATE(&stcb->asoc)) {
5527 case SCTP_STATE_COOKIE_ECHOED:
5529 * we consider data with valid tags in this state
5530 * shows us the cookie-ack was lost. Imply it was
5533 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5534 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5535 stcb->asoc.overall_error_count,
5537 SCTP_FROM_SCTP_INPUT,
5540 stcb->asoc.overall_error_count = 0;
5541 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5543 case SCTP_STATE_COOKIE_WAIT:
5545 * We consider OOTB any data sent during asoc setup.
5547 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5549 SCTP_TCB_UNLOCK(stcb);
5551 /* sa_ignore NOTREACHED */
5553 case SCTP_STATE_EMPTY: /* should not happen */
5554 case SCTP_STATE_INUSE: /* should not happen */
5555 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5556 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5558 SCTP_TCB_UNLOCK(stcb);
5560 /* sa_ignore NOTREACHED */
5562 case SCTP_STATE_OPEN:
5563 case SCTP_STATE_SHUTDOWN_SENT:
5566 /* take care of ECN, part 1. */
5567 if (stcb->asoc.ecn_allowed &&
5568 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5569 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
5571 /* plow through the data chunks while length > offset */
5572 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5573 inp, stcb, net, &high_tsn);
5576 * The association aborted, NO UNLOCK needed since
5577 * the association is destroyed.
5583 /* take care of ecn part 2. */
5584 if (stcb->asoc.ecn_allowed &&
5585 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5586 sctp_process_ecn_marked_b(stcb, net, high_tsn,
5591 * Anything important needs to have been m_copy'ed in
5595 if ((data_processed == 0) && (fwd_tsn_seen)) {
5598 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
5599 stcb->asoc.cumulative_tsn, MAX_TSN)) {
5600 /* there was a gap before this data was processed */
5603 stcb->asoc.send_sack = 1;
5604 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
5606 /* Again, we aborted so NO UNLOCK needed */
5609 } else if (fwd_tsn_seen) {
5610 stcb->asoc.send_sack = 1;
5612 /* trigger send of any chunks in queue... */
5614 #ifdef SCTP_AUDITING_ENABLED
5615 sctp_audit_log(0xE0, 2);
5616 sctp_auditing(1, inp, stcb, net);
5618 SCTPDBG(SCTP_DEBUG_INPUT1,
5619 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5620 stcb->asoc.peers_rwnd,
5621 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5622 stcb->asoc.total_flight);
5623 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5625 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
5627 (stcb->asoc.peers_rwnd > 0 ||
5628 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5629 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5630 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5631 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5633 #ifdef SCTP_AUDITING_ENABLED
5634 sctp_audit_log(0xE0, 3);
5635 sctp_auditing(2, inp, stcb, net);
5637 SCTP_TCB_UNLOCK(stcb);
5640 sctp_validate_no_locks(inp);
5647 sctp_print_mbuf_chain(struct mbuf *m)
5649 for (; m; m = SCTP_BUF_NEXT(m)) {
5650 printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5651 if (SCTP_BUF_IS_EXTENDED(m))
5652 printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5659 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5661 #ifdef SCTP_MBUF_LOGGING
5667 uint32_t vrf_id = 0;
5671 struct sctp_inpcb *inp = NULL;
5673 uint32_t check, calc_check;
5674 struct sctp_nets *net;
5675 struct sctp_tcb *stcb = NULL;
5676 struct sctp_chunkhdr *ch;
5677 int refcount_up = 0;
5678 int length, mlen, offset;
5680 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5681 SCTP_RELEASE_PKT(i_pak);
5684 mlen = SCTP_HEADER_LEN(i_pak);
5686 m = SCTP_HEADER_TO_CHAIN(i_pak);
5689 SCTP_STAT_INCR(sctps_recvpackets);
5690 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5693 #ifdef SCTP_MBUF_LOGGING
5694 /* Log in any input mbufs */
5695 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5698 if (SCTP_BUF_IS_EXTENDED(mat)) {
5699 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5701 mat = SCTP_BUF_NEXT(mat);
5705 #ifdef SCTP_PACKET_LOGGING
5706 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5707 sctp_packet_log(m, mlen);
5710 * Must take out the iphlen, since mlen expects this (only effect lb
5716 * Get IP, SCTP, and first chunk header together in first mbuf.
5718 ip = mtod(m, struct ip *);
5719 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5720 if (SCTP_BUF_LEN(m) < offset) {
5721 if ((m = m_pullup(m, offset)) == 0) {
5722 SCTP_STAT_INCR(sctps_hdrops);
5725 ip = mtod(m, struct ip *);
5727 /* validate mbuf chain length with IP payload length */
5728 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5729 SCTP_STAT_INCR(sctps_hdrops);
5732 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5733 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5734 SCTPDBG(SCTP_DEBUG_INPUT1,
5735 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5737 /* SCTP does not allow broadcasts or multicasts */
5738 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5741 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5743 * We only look at broadcast if its a front state, All
5744 * others we will not have a tcb for anyway.
5748 /* validate SCTP checksum */
5749 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5750 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5752 if_name(m->m_pkthdr.rcvif),
5753 m->m_pkthdr.csum_flags);
5754 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5755 SCTP_STAT_INCR(sctps_recvhwcrc);
5756 goto sctp_skip_csum_4;
5758 check = sh->checksum; /* save incoming checksum */
5759 if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
5760 ((ip->ip_src.s_addr == ip->ip_dst.s_addr) ||
5761 (SCTP_IS_IT_LOOPBACK(m)))
5763 SCTP_STAT_INCR(sctps_recvnocrc);
5764 goto sctp_skip_csum_4;
5766 sh->checksum = 0; /* prepare for calc */
5767 calc_check = sctp_calculate_cksum(m, iphlen);
5768 SCTP_STAT_INCR(sctps_recvswcrc);
5769 if (calc_check != check) {
5770 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5771 calc_check, check, m, mlen, iphlen);
5773 stcb = sctp_findassociation_addr(m, iphlen,
5774 offset - sizeof(*ch),
5777 if ((net) && (port)) {
5778 if (net->port == 0) {
5779 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5783 if ((inp) && (stcb)) {
5784 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5785 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5786 } else if ((inp != NULL) && (stcb == NULL)) {
5789 SCTP_STAT_INCR(sctps_badsum);
5790 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5793 sh->checksum = calc_check;
5795 /* destination port of 0 is illegal, based on RFC2960. */
5796 if (sh->dest_port == 0) {
5797 SCTP_STAT_INCR(sctps_hdrops);
5801 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5802 * IP/SCTP/first chunk header...
5804 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
5805 sh, ch, &inp, &net, vrf_id);
5806 if ((net) && (port)) {
5807 if (net->port == 0) {
5808 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5812 /* inp's ref-count increased && stcb locked */
5814 struct sctp_init_chunk *init_chk, chunk_buf;
5816 SCTP_STAT_INCR(sctps_noport);
5819 * we use the bandwidth limiting to protect against sending
5820 * too many ABORTS all at once. In this case these count the
5821 * same as an ICMP message.
5823 if (badport_bandlim(0) < 0)
5825 #endif /* ICMP_BANDLIM */
5826 SCTPDBG(SCTP_DEBUG_INPUT1,
5827 "Sending a ABORT from packet entry!\n");
5828 if (ch->chunk_type == SCTP_INITIATION) {
5830 * we do a trick here to get the INIT tag, dig in
5831 * and get the tag from the INIT and put it in the
5834 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5835 iphlen + sizeof(*sh), sizeof(*init_chk),
5836 (uint8_t *) & chunk_buf);
5837 if (init_chk != NULL)
5838 sh->v_tag = init_chk->init.initiate_tag;
5840 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5841 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
5844 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5847 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
5848 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
5850 } else if (stcb == NULL) {
5855 * I very much doubt any of the IPSEC stuff will work but I have no
5856 * idea, so I will leave it in place.
5858 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5859 MODULE_GLOBAL(MOD_IPSEC, ipsec4stat).in_polvio++;
5860 SCTP_STAT_INCR(sctps_hdrops);
5866 * common chunk processing
5868 length = ip->ip_len + iphlen;
5869 offset -= sizeof(struct sctp_chunkhdr);
5871 ecn_bits = ip->ip_tos;
5873 /* sa_ignore NO_NULL_CHK */
5874 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
5875 inp, stcb, net, ecn_bits, vrf_id, port);
5876 /* inp's ref-count reduced && stcb unlocked */
5880 if ((inp) && (refcount_up)) {
5881 /* reduce ref-count */
5882 SCTP_INP_DECR_REF(inp);
5887 SCTP_TCB_UNLOCK(stcb);
5889 if ((inp) && (refcount_up)) {
5890 /* reduce ref-count */
5891 SCTP_INP_DECR_REF(inp);
5899 sctp_input(i_pak, off)
5903 sctp_input_with_port(i_pak, off, 0);