The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/sctp_input.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
    5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
    6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions are met:
   10  *
   11  * a) Redistributions of source code must retain the above copyright notice,
   12  *    this list of conditions and the following disclaimer.
   13  *
   14  * b) Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in
   16  *    the documentation and/or other materials provided with the distribution.
   17  *
   18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
   19  *    contributors may be used to endorse or promote products derived
   20  *    from this software without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   32  * THE POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/12.0/sys/netinet/sctp_input.c 339042 2018-10-01 14:05:31Z tuexen $");
   37 
   38 #include <netinet/sctp_os.h>
   39 #include <netinet/sctp_var.h>
   40 #include <netinet/sctp_sysctl.h>
   41 #include <netinet/sctp_pcb.h>
   42 #include <netinet/sctp_header.h>
   43 #include <netinet/sctputil.h>
   44 #include <netinet/sctp_output.h>
   45 #include <netinet/sctp_input.h>
   46 #include <netinet/sctp_auth.h>
   47 #include <netinet/sctp_indata.h>
   48 #include <netinet/sctp_asconf.h>
   49 #include <netinet/sctp_bsd_addr.h>
   50 #include <netinet/sctp_timer.h>
   51 #include <netinet/sctp_crc32.h>
   52 #if defined(INET) || defined(INET6)
   53 #include <netinet/udp.h>
   54 #endif
   55 #include <netinet/in_kdtrace.h>
   56 #include <sys/smp.h>
   57 
   58 
   59 
   60 static void
   61 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
   62 {
   63         struct sctp_nets *net;
   64 
   65         /*
   66          * This now not only stops all cookie timers it also stops any INIT
   67          * timers as well. This will make sure that the timers are stopped
   68          * in all collision cases.
   69          */
   70         SCTP_TCB_LOCK_ASSERT(stcb);
   71         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   72                 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
   73                         sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
   74                             stcb->sctp_ep,
   75                             stcb,
   76                             net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
   77                 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
   78                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
   79                             stcb->sctp_ep,
   80                             stcb,
   81                             net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
   82                 }
   83         }
   84 }
   85 
   86 /* INIT handler */
   87 static void
   88 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
   89     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
   90     struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
   91     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
   92     uint8_t mflowtype, uint32_t mflowid,
   93     uint32_t vrf_id, uint16_t port)
   94 {
   95         struct sctp_init *init;
   96         struct mbuf *op_err;
   97 
   98         SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
   99             (void *)stcb);
  100         if (stcb == NULL) {
  101                 SCTP_INP_RLOCK(inp);
  102         }
  103         /* validate length */
  104         if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
  105                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
  106                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
  107                     mflowtype, mflowid,
  108                     vrf_id, port);
  109                 if (stcb)
  110                         *abort_no_unlock = 1;
  111                 goto outnow;
  112         }
  113         /* validate parameters */
  114         init = &cp->init;
  115         if (init->initiate_tag == 0) {
  116                 /* protocol error... send abort */
  117                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
  118                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
  119                     mflowtype, mflowid,
  120                     vrf_id, port);
  121                 if (stcb)
  122                         *abort_no_unlock = 1;
  123                 goto outnow;
  124         }
  125         if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
  126                 /* invalid parameter... send abort */
  127                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
  128                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
  129                     mflowtype, mflowid,
  130                     vrf_id, port);
  131                 if (stcb)
  132                         *abort_no_unlock = 1;
  133                 goto outnow;
  134         }
  135         if (init->num_inbound_streams == 0) {
  136                 /* protocol error... send abort */
  137                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
  138                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
  139                     mflowtype, mflowid,
  140                     vrf_id, port);
  141                 if (stcb)
  142                         *abort_no_unlock = 1;
  143                 goto outnow;
  144         }
  145         if (init->num_outbound_streams == 0) {
  146                 /* protocol error... send abort */
  147                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
  148                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
  149                     mflowtype, mflowid,
  150                     vrf_id, port);
  151                 if (stcb)
  152                         *abort_no_unlock = 1;
  153                 goto outnow;
  154         }
  155         if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
  156             offset + ntohs(cp->ch.chunk_length))) {
  157                 /* auth parameter(s) error... send abort */
  158                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  159                     "Problem with AUTH parameters");
  160                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
  161                     mflowtype, mflowid,
  162                     vrf_id, port);
  163                 if (stcb)
  164                         *abort_no_unlock = 1;
  165                 goto outnow;
  166         }
  167         /* We are only accepting if we have a listening socket. */
  168         if ((stcb == NULL) &&
  169             ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
  170             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  171             (!SCTP_IS_LISTENING(inp)))) {
  172                 /*
  173                  * FIX ME ?? What about TCP model and we have a
  174                  * match/restart case? Actually no fix is needed. the lookup
  175                  * will always find the existing assoc so stcb would not be
  176                  * NULL. It may be questionable to do this since we COULD
  177                  * just send back the INIT-ACK and hope that the app did
  178                  * accept()'s by the time the COOKIE was sent. But there is
  179                  * a price to pay for COOKIE generation and I don't want to
  180                  * pay it on the chance that the app will actually do some
  181                  * accepts(). The App just looses and should NOT be in this
  182                  * state :-)
  183                  */
  184                 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
  185                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  186                             "No listener");
  187                         sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
  188                             mflowtype, mflowid, inp->fibnum,
  189                             vrf_id, port);
  190                 }
  191                 goto outnow;
  192         }
  193         if ((stcb != NULL) &&
  194             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  195                 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
  196                 sctp_send_shutdown_ack(stcb, NULL);
  197                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
  198         } else {
  199                 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
  200                 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
  201                     src, dst, sh, cp,
  202                     mflowtype, mflowid,
  203                     vrf_id, port);
  204         }
  205 outnow:
  206         if (stcb == NULL) {
  207                 SCTP_INP_RUNLOCK(inp);
  208         }
  209 }
  210 
  211 /*
  212  * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
  213  */
  214 
  215 int
  216 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
  217 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
  218     SCTP_UNUSED
  219 #endif
  220 )
  221 {
  222         int unsent_data;
  223         unsigned int i;
  224         struct sctp_stream_queue_pending *sp;
  225         struct sctp_association *asoc;
  226 
  227         /*
  228          * This function returns if any stream has true unsent data on it.
  229          * Note that as it looks through it will clean up any places that
  230          * have old data that has been sent but left at top of stream queue.
  231          */
  232         asoc = &stcb->asoc;
  233         unsent_data = 0;
  234         SCTP_TCB_SEND_LOCK(stcb);
  235         if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
  236                 /* Check to see if some data queued */
  237                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  238                         /* sa_ignore FREED_MEMORY */
  239                         sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
  240                         if (sp == NULL) {
  241                                 continue;
  242                         }
  243                         if ((sp->msg_is_complete) &&
  244                             (sp->length == 0) &&
  245                             (sp->sender_all_done)) {
  246                                 /*
  247                                  * We are doing differed cleanup. Last time
  248                                  * through when we took all the data the
  249                                  * sender_all_done was not set.
  250                                  */
  251                                 if (sp->put_last_out == 0) {
  252                                         SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
  253                                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
  254                                             sp->sender_all_done,
  255                                             sp->length,
  256                                             sp->msg_is_complete,
  257                                             sp->put_last_out);
  258                                 }
  259                                 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
  260                                 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
  261                                 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
  262                                 if (sp->net) {
  263                                         sctp_free_remote_addr(sp->net);
  264                                         sp->net = NULL;
  265                                 }
  266                                 if (sp->data) {
  267                                         sctp_m_freem(sp->data);
  268                                         sp->data = NULL;
  269                                 }
  270                                 sctp_free_a_strmoq(stcb, sp, so_locked);
  271                                 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
  272                                         unsent_data++;
  273                                 }
  274                         } else {
  275                                 unsent_data++;
  276                         }
  277                         if (unsent_data > 0) {
  278                                 break;
  279                         }
  280                 }
  281         }
  282         SCTP_TCB_SEND_UNLOCK(stcb);
  283         return (unsent_data);
  284 }
  285 
  286 static int
  287 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
  288 {
  289         struct sctp_init *init;
  290         struct sctp_association *asoc;
  291         struct sctp_nets *lnet;
  292         unsigned int i;
  293 
  294         init = &cp->init;
  295         asoc = &stcb->asoc;
  296         /* save off parameters */
  297         asoc->peer_vtag = ntohl(init->initiate_tag);
  298         asoc->peers_rwnd = ntohl(init->a_rwnd);
  299         /* init tsn's */
  300         asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
  301 
  302         if (!TAILQ_EMPTY(&asoc->nets)) {
  303                 /* update any ssthresh's that may have a default */
  304                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
  305                         lnet->ssthresh = asoc->peers_rwnd;
  306                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
  307                                 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
  308                         }
  309 
  310                 }
  311         }
  312         SCTP_TCB_SEND_LOCK(stcb);
  313         if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
  314                 unsigned int newcnt;
  315                 struct sctp_stream_out *outs;
  316                 struct sctp_stream_queue_pending *sp, *nsp;
  317                 struct sctp_tmit_chunk *chk, *nchk;
  318 
  319                 /* abandon the upper streams */
  320                 newcnt = ntohs(init->num_inbound_streams);
  321                 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
  322                         if (chk->rec.data.sid >= newcnt) {
  323                                 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
  324                                 asoc->send_queue_cnt--;
  325                                 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
  326                                         asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
  327 #ifdef INVARIANTS
  328                                 } else {
  329                                         panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
  330 #endif
  331                                 }
  332                                 if (chk->data != NULL) {
  333                                         sctp_free_bufspace(stcb, asoc, chk, 1);
  334                                         sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
  335                                             0, chk, SCTP_SO_NOT_LOCKED);
  336                                         if (chk->data) {
  337                                                 sctp_m_freem(chk->data);
  338                                                 chk->data = NULL;
  339                                         }
  340                                 }
  341                                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  342                                 /* sa_ignore FREED_MEMORY */
  343                         }
  344                 }
  345                 if (asoc->strmout) {
  346                         for (i = newcnt; i < asoc->pre_open_streams; i++) {
  347                                 outs = &asoc->strmout[i];
  348                                 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
  349                                         atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
  350                                         TAILQ_REMOVE(&outs->outqueue, sp, next);
  351                                         stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
  352                                         sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
  353                                             stcb, 0, sp, SCTP_SO_NOT_LOCKED);
  354                                         if (sp->data) {
  355                                                 sctp_m_freem(sp->data);
  356                                                 sp->data = NULL;
  357                                         }
  358                                         if (sp->net) {
  359                                                 sctp_free_remote_addr(sp->net);
  360                                                 sp->net = NULL;
  361                                         }
  362                                         /* Free the chunk */
  363                                         sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
  364                                         /* sa_ignore FREED_MEMORY */
  365                                 }
  366                                 outs->state = SCTP_STREAM_CLOSED;
  367                         }
  368                 }
  369                 /* cut back the count */
  370                 asoc->pre_open_streams = newcnt;
  371         }
  372         SCTP_TCB_SEND_UNLOCK(stcb);
  373         asoc->streamoutcnt = asoc->pre_open_streams;
  374         if (asoc->strmout) {
  375                 for (i = 0; i < asoc->streamoutcnt; i++) {
  376                         asoc->strmout[i].state = SCTP_STREAM_OPEN;
  377                 }
  378         }
  379         /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
  380         asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
  381         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
  382                 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
  383         }
  384         /* This is the next one we expect */
  385         asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
  386 
  387         asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
  388         asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
  389 
  390         asoc->advanced_peer_ack_point = asoc->last_acked_seq;
  391         /* open the requested streams */
  392 
  393         if (asoc->strmin != NULL) {
  394                 /* Free the old ones */
  395                 for (i = 0; i < asoc->streamincnt; i++) {
  396                         sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
  397                         sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
  398                 }
  399                 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
  400         }
  401         if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
  402                 asoc->streamincnt = ntohs(init->num_outbound_streams);
  403         } else {
  404                 asoc->streamincnt = asoc->max_inbound_streams;
  405         }
  406         SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
  407             sizeof(struct sctp_stream_in), SCTP_M_STRMI);
  408         if (asoc->strmin == NULL) {
  409                 /* we didn't get memory for the streams! */
  410                 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
  411                 return (-1);
  412         }
  413         for (i = 0; i < asoc->streamincnt; i++) {
  414                 asoc->strmin[i].sid = i;
  415                 asoc->strmin[i].last_mid_delivered = 0xffffffff;
  416                 TAILQ_INIT(&asoc->strmin[i].inqueue);
  417                 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
  418                 asoc->strmin[i].pd_api_started = 0;
  419                 asoc->strmin[i].delivery_started = 0;
  420         }
  421         /*
  422          * load_address_from_init will put the addresses into the
  423          * association when the COOKIE is processed or the INIT-ACK is
  424          * processed. Both types of COOKIE's existing and new call this
  425          * routine. It will remove addresses that are no longer in the
  426          * association (for the restarting case where addresses are
  427          * removed). Up front when the INIT arrives we will discard it if it
  428          * is a restart and new addresses have been added.
  429          */
  430         /* sa_ignore MEMLEAK */
  431         return (0);
  432 }
  433 
  434 /*
  435  * INIT-ACK message processing/consumption returns value < 0 on error
  436  */
  437 static int
  438 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
  439     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
  440     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
  441     struct sctp_nets *net, int *abort_no_unlock,
  442     uint8_t mflowtype, uint32_t mflowid,
  443     uint32_t vrf_id)
  444 {
  445         struct sctp_association *asoc;
  446         struct mbuf *op_err;
  447         int retval, abort_flag;
  448         uint32_t initack_limit;
  449         int nat_friendly = 0;
  450 
  451         /* First verify that we have no illegal param's */
  452         abort_flag = 0;
  453 
  454         op_err = sctp_arethere_unrecognized_parameters(m,
  455             (offset + sizeof(struct sctp_init_chunk)),
  456             &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
  457         if (abort_flag) {
  458                 /* Send an abort and notify peer */
  459                 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
  460                 *abort_no_unlock = 1;
  461                 return (-1);
  462         }
  463         asoc = &stcb->asoc;
  464         asoc->peer_supports_nat = (uint8_t)nat_friendly;
  465         /* process the peer's parameters in the INIT-ACK */
  466         retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
  467         if (retval < 0) {
  468                 return (retval);
  469         }
  470         initack_limit = offset + ntohs(cp->ch.chunk_length);
  471         /* load all addresses */
  472         if ((retval = sctp_load_addresses_from_init(stcb, m,
  473             (offset + sizeof(struct sctp_init_chunk)), initack_limit,
  474             src, dst, NULL, stcb->asoc.port))) {
  475                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  476                     "Problem with address parameters");
  477                 SCTPDBG(SCTP_DEBUG_INPUT1,
  478                     "Load addresses from INIT causes an abort %d\n",
  479                     retval);
  480                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
  481                     src, dst, sh, op_err,
  482                     mflowtype, mflowid,
  483                     vrf_id, net->port);
  484                 *abort_no_unlock = 1;
  485                 return (-1);
  486         }
  487         /* if the peer doesn't support asconf, flush the asconf queue */
  488         if (asoc->asconf_supported == 0) {
  489                 struct sctp_asconf_addr *param, *nparam;
  490 
  491                 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
  492                         TAILQ_REMOVE(&asoc->asconf_queue, param, next);
  493                         SCTP_FREE(param, SCTP_M_ASC_ADDR);
  494                 }
  495         }
  496 
  497         stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
  498             stcb->asoc.local_hmacs);
  499         if (op_err) {
  500                 sctp_queue_op_err(stcb, op_err);
  501                 /* queuing will steal away the mbuf chain to the out queue */
  502                 op_err = NULL;
  503         }
  504         /* extract the cookie and queue it to "echo" it back... */
  505         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
  506                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
  507                     stcb->asoc.overall_error_count,
  508                     0,
  509                     SCTP_FROM_SCTP_INPUT,
  510                     __LINE__);
  511         }
  512         stcb->asoc.overall_error_count = 0;
  513         net->error_count = 0;
  514 
  515         /*
  516          * Cancel the INIT timer, We do this first before queueing the
  517          * cookie. We always cancel at the primary to assue that we are
  518          * canceling the timer started by the INIT which always goes to the
  519          * primary.
  520          */
  521         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
  522             asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
  523 
  524         /* calculate the RTO */
  525         net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
  526             SCTP_RTT_FROM_NON_DATA);
  527         retval = sctp_send_cookie_echo(m, offset, stcb, net);
  528         if (retval < 0) {
  529                 /*
  530                  * No cookie, we probably should send a op error. But in any
  531                  * case if there is no cookie in the INIT-ACK, we can
  532                  * abandon the peer, its broke.
  533                  */
  534                 if (retval == -3) {
  535                         uint16_t len;
  536 
  537                         len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
  538                         /* We abort with an error of missing mandatory param */
  539                         op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
  540                         if (op_err != NULL) {
  541                                 struct sctp_error_missing_param *cause;
  542 
  543                                 SCTP_BUF_LEN(op_err) = len;
  544                                 cause = mtod(op_err, struct sctp_error_missing_param *);
  545                                 /* Subtract the reserved param */
  546                                 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
  547                                 cause->cause.length = htons(len);
  548                                 cause->num_missing_params = htonl(1);
  549                                 cause->type[0] = htons(SCTP_STATE_COOKIE);
  550                         }
  551                         sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
  552                             src, dst, sh, op_err,
  553                             mflowtype, mflowid,
  554                             vrf_id, net->port);
  555                         *abort_no_unlock = 1;
  556                 }
  557                 return (retval);
  558         }
  559 
  560         return (0);
  561 }
  562 
  563 static void
  564 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
  565     struct sctp_tcb *stcb, struct sctp_nets *net)
  566 {
  567         union sctp_sockstore store;
  568         struct sctp_nets *r_net, *f_net;
  569         struct timeval tv;
  570         int req_prim = 0;
  571         uint16_t old_error_counter;
  572 
  573         if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
  574                 /* Invalid length */
  575                 return;
  576         }
  577 
  578         memset(&store, 0, sizeof(store));
  579         switch (cp->heartbeat.hb_info.addr_family) {
  580 #ifdef INET
  581         case AF_INET:
  582                 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
  583                         store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
  584                         store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
  585                         store.sin.sin_port = stcb->rport;
  586                         memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
  587                             sizeof(store.sin.sin_addr));
  588                 } else {
  589                         return;
  590                 }
  591                 break;
  592 #endif
  593 #ifdef INET6
  594         case AF_INET6:
  595                 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
  596                         store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
  597                         store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
  598                         store.sin6.sin6_port = stcb->rport;
  599                         memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
  600                 } else {
  601                         return;
  602                 }
  603                 break;
  604 #endif
  605         default:
  606                 return;
  607         }
  608         r_net = sctp_findnet(stcb, &store.sa);
  609         if (r_net == NULL) {
  610                 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
  611                 return;
  612         }
  613         if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
  614             (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
  615             (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
  616                 /*
  617                  * If the its a HB and it's random value is correct when can
  618                  * confirm the destination.
  619                  */
  620                 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
  621                 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
  622                         stcb->asoc.primary_destination = r_net;
  623                         r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
  624                         f_net = TAILQ_FIRST(&stcb->asoc.nets);
  625                         if (f_net != r_net) {
  626                                 /*
  627                                  * first one on the list is NOT the primary
  628                                  * sctp_cmpaddr() is much more efficient if
  629                                  * the primary is the first on the list,
  630                                  * make it so.
  631                                  */
  632                                 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
  633                                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
  634                         }
  635                         req_prim = 1;
  636                 }
  637                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
  638                     stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
  639                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
  640                     r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
  641                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
  642         }
  643         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
  644                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
  645                     stcb->asoc.overall_error_count,
  646                     0,
  647                     SCTP_FROM_SCTP_INPUT,
  648                     __LINE__);
  649         }
  650         stcb->asoc.overall_error_count = 0;
  651         old_error_counter = r_net->error_count;
  652         r_net->error_count = 0;
  653         r_net->hb_responded = 1;
  654         tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
  655         tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
  656         /* Now lets do a RTO with this */
  657         r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
  658             SCTP_RTT_FROM_NON_DATA);
  659         if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
  660                 r_net->dest_state |= SCTP_ADDR_REACHABLE;
  661                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
  662                     0, (void *)r_net, SCTP_SO_NOT_LOCKED);
  663         }
  664         if (r_net->dest_state & SCTP_ADDR_PF) {
  665                 r_net->dest_state &= ~SCTP_ADDR_PF;
  666                 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
  667         }
  668         if (old_error_counter > 0) {
  669                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
  670                     stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
  671                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
  672         }
  673         if (r_net == stcb->asoc.primary_destination) {
  674                 if (stcb->asoc.alternate) {
  675                         /* release the alternate, primary is good */
  676                         sctp_free_remote_addr(stcb->asoc.alternate);
  677                         stcb->asoc.alternate = NULL;
  678                 }
  679         }
  680         /* Mobility adaptation */
  681         if (req_prim) {
  682                 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
  683                     SCTP_MOBILITY_BASE) ||
  684                     sctp_is_mobility_feature_on(stcb->sctp_ep,
  685                     SCTP_MOBILITY_FASTHANDOFF)) &&
  686                     sctp_is_mobility_feature_on(stcb->sctp_ep,
  687                     SCTP_MOBILITY_PRIM_DELETED)) {
  688 
  689                         sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
  690                             stcb->sctp_ep, stcb, NULL,
  691                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
  692                         if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  693                             SCTP_MOBILITY_FASTHANDOFF)) {
  694                                 sctp_assoc_immediate_retrans(stcb,
  695                                     stcb->asoc.primary_destination);
  696                         }
  697                         if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  698                             SCTP_MOBILITY_BASE)) {
  699                                 sctp_move_chunks_from_net(stcb,
  700                                     stcb->asoc.deleted_primary);
  701                         }
  702                         sctp_delete_prim_timer(stcb->sctp_ep, stcb,
  703                             stcb->asoc.deleted_primary);
  704                 }
  705         }
  706 }
  707 
  708 static int
  709 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
  710 {
  711         /*
  712          * return 0 means we want you to proceed with the abort non-zero
  713          * means no abort processing
  714          */
  715         struct sctpasochead *head;
  716 
  717         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  718             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  719                 atomic_add_int(&stcb->asoc.refcnt, 1);
  720                 SCTP_TCB_UNLOCK(stcb);
  721                 SCTP_INP_INFO_WLOCK();
  722                 SCTP_TCB_LOCK(stcb);
  723                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
  724         }
  725         if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
  726                 /* generate a new vtag and send init */
  727                 LIST_REMOVE(stcb, sctp_asocs);
  728                 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
  729                 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
  730                 /*
  731                  * put it in the bucket in the vtag hash of assoc's for the
  732                  * system
  733                  */
  734                 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
  735                 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
  736                 SCTP_INP_INFO_WUNLOCK();
  737                 return (1);
  738         }
  739         if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
  740                 /*
  741                  * treat like a case where the cookie expired i.e.: - dump
  742                  * current cookie. - generate a new vtag. - resend init.
  743                  */
  744                 /* generate a new vtag and send init */
  745                 LIST_REMOVE(stcb, sctp_asocs);
  746                 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
  747                 sctp_stop_all_cookie_timers(stcb);
  748                 sctp_toss_old_cookies(stcb, &stcb->asoc);
  749                 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
  750                 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
  751                 /*
  752                  * put it in the bucket in the vtag hash of assoc's for the
  753                  * system
  754                  */
  755                 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
  756                 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
  757                 SCTP_INP_INFO_WUNLOCK();
  758                 return (1);
  759         }
  760         return (0);
  761 }
  762 
  763 static int
  764 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
  765     struct sctp_nets *net)
  766 {
  767         /*
  768          * return 0 means we want you to proceed with the abort non-zero
  769          * means no abort processing
  770          */
  771         if (stcb->asoc.auth_supported == 0) {
  772                 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
  773                 return (0);
  774         }
  775         sctp_asconf_send_nat_state_update(stcb, net);
  776         return (1);
  777 }
  778 
  779 
  780 /* Returns 1 if the stcb was aborted, 0 otherwise */
  781 static int
  782 sctp_handle_abort(struct sctp_abort_chunk *abort,
  783     struct sctp_tcb *stcb, struct sctp_nets *net)
  784 {
  785 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  786         struct socket *so;
  787 #endif
  788         uint16_t len;
  789         uint16_t error;
  790 
  791         SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
  792         if (stcb == NULL)
  793                 return (0);
  794 
  795         len = ntohs(abort->ch.chunk_length);
  796         if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
  797                 /*
  798                  * Need to check the cause codes for our two magic nat
  799                  * aborts which don't kill the assoc necessarily.
  800                  */
  801                 struct sctp_error_cause *cause;
  802 
  803                 cause = (struct sctp_error_cause *)(abort + 1);
  804                 error = ntohs(cause->code);
  805                 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
  806                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
  807                             abort->ch.chunk_flags);
  808                         if (sctp_handle_nat_colliding_state(stcb)) {
  809                                 return (0);
  810                         }
  811                 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
  812                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
  813                             abort->ch.chunk_flags);
  814                         if (sctp_handle_nat_missing_state(stcb, net)) {
  815                                 return (0);
  816                         }
  817                 }
  818         } else {
  819                 error = 0;
  820         }
  821         /* stop any receive timers */
  822         sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
  823             SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
  824         /* notify user of the abort and clean up... */
  825         sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
  826         /* free the tcb */
  827         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  828         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  829             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  830                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  831         }
  832 #ifdef SCTP_ASOCLOG_OF_TSNS
  833         sctp_print_out_track_log(stcb);
  834 #endif
  835 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  836         so = SCTP_INP_SO(stcb->sctp_ep);
  837         atomic_add_int(&stcb->asoc.refcnt, 1);
  838         SCTP_TCB_UNLOCK(stcb);
  839         SCTP_SOCKET_LOCK(so, 1);
  840         SCTP_TCB_LOCK(stcb);
  841         atomic_subtract_int(&stcb->asoc.refcnt, 1);
  842 #endif
  843         SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
  844         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
  845             SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
  846 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  847         SCTP_SOCKET_UNLOCK(so, 1);
  848 #endif
  849         SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
  850         return (1);
  851 }
  852 
  853 static void
  854 sctp_start_net_timers(struct sctp_tcb *stcb)
  855 {
  856         uint32_t cnt_hb_sent;
  857         struct sctp_nets *net;
  858 
  859         cnt_hb_sent = 0;
  860         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  861                 /*
  862                  * For each network start: 1) A pmtu timer. 2) A HB timer 3)
  863                  * If the dest in unconfirmed send a hb as well if under
  864                  * max_hb_burst have been sent.
  865                  */
  866                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
  867                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
  868                 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
  869                     (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
  870                         sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
  871                         cnt_hb_sent++;
  872                 }
  873         }
  874         if (cnt_hb_sent) {
  875                 sctp_chunk_output(stcb->sctp_ep, stcb,
  876                     SCTP_OUTPUT_FROM_COOKIE_ACK,
  877                     SCTP_SO_NOT_LOCKED);
  878         }
  879 }
  880 
  881 
  882 static void
  883 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
  884     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
  885 {
  886         struct sctp_association *asoc;
  887         int some_on_streamwheel;
  888         int old_state;
  889 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  890         struct socket *so;
  891 #endif
  892 
  893         SCTPDBG(SCTP_DEBUG_INPUT2,
  894             "sctp_handle_shutdown: handling SHUTDOWN\n");
  895         if (stcb == NULL)
  896                 return;
  897         asoc = &stcb->asoc;
  898         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  899             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  900                 return;
  901         }
  902         if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
  903                 /* Shutdown NOT the expected size */
  904                 return;
  905         }
  906         old_state = SCTP_GET_STATE(stcb);
  907         sctp_update_acked(stcb, cp, abort_flag);
  908         if (*abort_flag) {
  909                 return;
  910         }
  911         if (asoc->control_pdapi) {
  912                 /*
  913                  * With a normal shutdown we assume the end of last record.
  914                  */
  915                 SCTP_INP_READ_LOCK(stcb->sctp_ep);
  916                 if (asoc->control_pdapi->on_strm_q) {
  917                         struct sctp_stream_in *strm;
  918 
  919                         strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
  920                         if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
  921                                 /* Unordered */
  922                                 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
  923                                 asoc->control_pdapi->on_strm_q = 0;
  924                         } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
  925                                 /* Ordered */
  926                                 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
  927                                 asoc->control_pdapi->on_strm_q = 0;
  928 #ifdef INVARIANTS
  929                         } else {
  930                                 panic("Unknown state on ctrl:%p on_strm_q:%d",
  931                                     asoc->control_pdapi,
  932                                     asoc->control_pdapi->on_strm_q);
  933 #endif
  934                         }
  935                 }
  936                 asoc->control_pdapi->end_added = 1;
  937                 asoc->control_pdapi->pdapi_aborted = 1;
  938                 asoc->control_pdapi = NULL;
  939                 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
  940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  941                 so = SCTP_INP_SO(stcb->sctp_ep);
  942                 atomic_add_int(&stcb->asoc.refcnt, 1);
  943                 SCTP_TCB_UNLOCK(stcb);
  944                 SCTP_SOCKET_LOCK(so, 1);
  945                 SCTP_TCB_LOCK(stcb);
  946                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
  947                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
  948                         /* assoc was freed while we were unlocked */
  949                         SCTP_SOCKET_UNLOCK(so, 1);
  950                         return;
  951                 }
  952 #endif
  953                 if (stcb->sctp_socket) {
  954                         sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
  955                 }
  956 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  957                 SCTP_SOCKET_UNLOCK(so, 1);
  958 #endif
  959         }
  960         /* goto SHUTDOWN_RECEIVED state to block new requests */
  961         if (stcb->sctp_socket) {
  962                 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
  963                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
  964                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
  965                         SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
  966                         /*
  967                          * notify upper layer that peer has initiated a
  968                          * shutdown
  969                          */
  970                         sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
  971 
  972                         /* reset time */
  973                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
  974                 }
  975         }
  976         if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
  977                 /*
  978                  * stop the shutdown timer, since we WILL move to
  979                  * SHUTDOWN-ACK-SENT.
  980                  */
  981                 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
  982                     net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
  983         }
  984         /* Now is there unsent data on a stream somewhere? */
  985         some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
  986 
  987         if (!TAILQ_EMPTY(&asoc->send_queue) ||
  988             !TAILQ_EMPTY(&asoc->sent_queue) ||
  989             some_on_streamwheel) {
  990                 /* By returning we will push more data out */
  991                 return;
  992         } else {
  993                 /* no outstanding data to send, so move on... */
  994                 /* send SHUTDOWN-ACK */
  995                 /* move to SHUTDOWN-ACK-SENT state */
  996                 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  997                     (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  998                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  999                 }
 1000                 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
 1001                         SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
 1002                         sctp_stop_timers_for_shutdown(stcb);
 1003                         sctp_send_shutdown_ack(stcb, net);
 1004                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
 1005                             stcb->sctp_ep, stcb, net);
 1006                 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
 1007                         sctp_send_shutdown_ack(stcb, net);
 1008                 }
 1009         }
 1010 }
 1011 
 1012 static void
 1013 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
 1014     struct sctp_tcb *stcb,
 1015     struct sctp_nets *net)
 1016 {
 1017         struct sctp_association *asoc;
 1018 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1019         struct socket *so;
 1020 
 1021         so = SCTP_INP_SO(stcb->sctp_ep);
 1022 #endif
 1023         SCTPDBG(SCTP_DEBUG_INPUT2,
 1024             "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
 1025         if (stcb == NULL)
 1026                 return;
 1027 
 1028         asoc = &stcb->asoc;
 1029         /* process according to association state */
 1030         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
 1031             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
 1032                 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
 1033                 sctp_send_shutdown_complete(stcb, net, 1);
 1034                 SCTP_TCB_UNLOCK(stcb);
 1035                 return;
 1036         }
 1037         if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
 1038             (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
 1039                 /* unexpected SHUTDOWN-ACK... so ignore... */
 1040                 SCTP_TCB_UNLOCK(stcb);
 1041                 return;
 1042         }
 1043         if (asoc->control_pdapi) {
 1044                 /*
 1045                  * With a normal shutdown we assume the end of last record.
 1046                  */
 1047                 SCTP_INP_READ_LOCK(stcb->sctp_ep);
 1048                 asoc->control_pdapi->end_added = 1;
 1049                 asoc->control_pdapi->pdapi_aborted = 1;
 1050                 asoc->control_pdapi = NULL;
 1051                 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
 1052 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1053                 atomic_add_int(&stcb->asoc.refcnt, 1);
 1054                 SCTP_TCB_UNLOCK(stcb);
 1055                 SCTP_SOCKET_LOCK(so, 1);
 1056                 SCTP_TCB_LOCK(stcb);
 1057                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 1058                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
 1059                         /* assoc was freed while we were unlocked */
 1060                         SCTP_SOCKET_UNLOCK(so, 1);
 1061                         return;
 1062                 }
 1063 #endif
 1064                 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
 1065 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1066                 SCTP_SOCKET_UNLOCK(so, 1);
 1067 #endif
 1068         }
 1069 #ifdef INVARIANTS
 1070         if (!TAILQ_EMPTY(&asoc->send_queue) ||
 1071             !TAILQ_EMPTY(&asoc->sent_queue) ||
 1072             sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
 1073                 panic("Queues are not empty when handling SHUTDOWN-ACK");
 1074         }
 1075 #endif
 1076         /* stop the timer */
 1077         sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
 1078             SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
 1079         /* send SHUTDOWN-COMPLETE */
 1080         sctp_send_shutdown_complete(stcb, net, 0);
 1081         /* notify upper layer protocol */
 1082         if (stcb->sctp_socket) {
 1083                 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 1084                     (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
 1085                         stcb->sctp_socket->so_snd.sb_cc = 0;
 1086                 }
 1087                 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
 1088         }
 1089         SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
 1090         /* free the TCB but first save off the ep */
 1091 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1092         atomic_add_int(&stcb->asoc.refcnt, 1);
 1093         SCTP_TCB_UNLOCK(stcb);
 1094         SCTP_SOCKET_LOCK(so, 1);
 1095         SCTP_TCB_LOCK(stcb);
 1096         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 1097 #endif
 1098         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
 1099             SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
 1100 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1101         SCTP_SOCKET_UNLOCK(so, 1);
 1102 #endif
 1103 }
 1104 
 1105 static void
 1106 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type,
 1107     struct sctp_nets *net)
 1108 {
 1109         switch (chunk_type) {
 1110         case SCTP_ASCONF_ACK:
 1111         case SCTP_ASCONF:
 1112                 sctp_asconf_cleanup(stcb, net);
 1113                 break;
 1114         case SCTP_IFORWARD_CUM_TSN:
 1115         case SCTP_FORWARD_CUM_TSN:
 1116                 stcb->asoc.prsctp_supported = 0;
 1117                 break;
 1118         default:
 1119                 SCTPDBG(SCTP_DEBUG_INPUT2,
 1120                     "Peer does not support chunk type %d (0x%x).\n",
 1121                     chunk_type, chunk_type);
 1122                 break;
 1123         }
 1124 }
 1125 
 1126 /*
 1127  * Skip past the param header and then we will find the param that caused the
 1128  * problem.  There are a number of param's in a ASCONF OR the prsctp param
 1129  * these will turn of specific features.
 1130  * XXX: Is this the right thing to do?
 1131  */
 1132 static void
 1133 sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
 1134 {
 1135         switch (parameter_type) {
 1136                 /* pr-sctp draft */
 1137         case SCTP_PRSCTP_SUPPORTED:
 1138                 stcb->asoc.prsctp_supported = 0;
 1139                 break;
 1140         case SCTP_SUPPORTED_CHUNK_EXT:
 1141                 break;
 1142                 /* draft-ietf-tsvwg-addip-sctp */
 1143         case SCTP_HAS_NAT_SUPPORT:
 1144                 stcb->asoc.peer_supports_nat = 0;
 1145                 break;
 1146         case SCTP_ADD_IP_ADDRESS:
 1147         case SCTP_DEL_IP_ADDRESS:
 1148         case SCTP_SET_PRIM_ADDR:
 1149                 stcb->asoc.asconf_supported = 0;
 1150                 break;
 1151         case SCTP_SUCCESS_REPORT:
 1152         case SCTP_ERROR_CAUSE_IND:
 1153                 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
 1154                 SCTPDBG(SCTP_DEBUG_INPUT2,
 1155                     "Turning off ASCONF to this strange peer\n");
 1156                 stcb->asoc.asconf_supported = 0;
 1157                 break;
 1158         default:
 1159                 SCTPDBG(SCTP_DEBUG_INPUT2,
 1160                     "Peer does not support param type %d (0x%x)??\n",
 1161                     parameter_type, parameter_type);
 1162                 break;
 1163         }
 1164 }
 1165 
 1166 static int
 1167 sctp_handle_error(struct sctp_chunkhdr *ch,
 1168     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
 1169 {
 1170         struct sctp_error_cause *cause;
 1171         struct sctp_association *asoc;
 1172         uint32_t remaining_length, adjust;
 1173         uint16_t code, cause_code, cause_length;
 1174 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1175         struct socket *so;
 1176 #endif
 1177 
 1178         /* parse through all of the errors and process */
 1179         asoc = &stcb->asoc;
 1180         cause = (struct sctp_error_cause *)((caddr_t)ch +
 1181             sizeof(struct sctp_chunkhdr));
 1182         remaining_length = ntohs(ch->chunk_length);
 1183         if (remaining_length > limit) {
 1184                 remaining_length = limit;
 1185         }
 1186         if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
 1187                 remaining_length -= sizeof(struct sctp_chunkhdr);
 1188         } else {
 1189                 remaining_length = 0;
 1190         }
 1191         code = 0;
 1192         while (remaining_length >= sizeof(struct sctp_error_cause)) {
 1193                 /* Process an Error Cause */
 1194                 cause_code = ntohs(cause->code);
 1195                 cause_length = ntohs(cause->length);
 1196                 if ((cause_length > remaining_length) || (cause_length == 0)) {
 1197                         /* Invalid cause length, possibly due to truncation. */
 1198                         SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
 1199                             remaining_length, cause_length);
 1200                         return (0);
 1201                 }
 1202                 if (code == 0) {
 1203                         /* report the first error cause */
 1204                         code = cause_code;
 1205                 }
 1206                 switch (cause_code) {
 1207                 case SCTP_CAUSE_INVALID_STREAM:
 1208                 case SCTP_CAUSE_MISSING_PARAM:
 1209                 case SCTP_CAUSE_INVALID_PARAM:
 1210                 case SCTP_CAUSE_NO_USER_DATA:
 1211                         SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
 1212                             cause_code);
 1213                         break;
 1214                 case SCTP_CAUSE_NAT_COLLIDING_STATE:
 1215                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags: %x\n",
 1216                             ch->chunk_flags);
 1217                         if (sctp_handle_nat_colliding_state(stcb)) {
 1218                                 return (0);
 1219                         }
 1220                         break;
 1221                 case SCTP_CAUSE_NAT_MISSING_STATE:
 1222                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags: %x\n",
 1223                             ch->chunk_flags);
 1224                         if (sctp_handle_nat_missing_state(stcb, net)) {
 1225                                 return (0);
 1226                         }
 1227                         break;
 1228                 case SCTP_CAUSE_STALE_COOKIE:
 1229                         /*
 1230                          * We only act if we have echoed a cookie and are
 1231                          * waiting.
 1232                          */
 1233                         if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
 1234                             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
 1235                                 struct sctp_error_stale_cookie *stale_cookie;
 1236 
 1237                                 stale_cookie = (struct sctp_error_stale_cookie *)cause;
 1238                                 asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time);
 1239                                 /* Double it to be more robust on RTX */
 1240                                 if (asoc->cookie_preserve_req <= UINT32_MAX / 2) {
 1241                                         asoc->cookie_preserve_req *= 2;
 1242                                 } else {
 1243                                         asoc->cookie_preserve_req = UINT32_MAX;
 1244                                 }
 1245                                 asoc->stale_cookie_count++;
 1246                                 if (asoc->stale_cookie_count >
 1247                                     asoc->max_init_times) {
 1248                                         sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
 1249                                         /* now free the asoc */
 1250 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1251                                         so = SCTP_INP_SO(stcb->sctp_ep);
 1252                                         atomic_add_int(&stcb->asoc.refcnt, 1);
 1253                                         SCTP_TCB_UNLOCK(stcb);
 1254                                         SCTP_SOCKET_LOCK(so, 1);
 1255                                         SCTP_TCB_LOCK(stcb);
 1256                                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 1257 #endif
 1258                                         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
 1259                                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
 1260 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1261                                         SCTP_SOCKET_UNLOCK(so, 1);
 1262 #endif
 1263                                         return (-1);
 1264                                 }
 1265                                 /* blast back to INIT state */
 1266                                 sctp_toss_old_cookies(stcb, &stcb->asoc);
 1267                                 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
 1268                                 sctp_stop_all_cookie_timers(stcb);
 1269                                 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
 1270                         }
 1271                         break;
 1272                 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
 1273                         /*
 1274                          * Nothing we can do here, we don't do hostname
 1275                          * addresses so if the peer does not like my IPv6
 1276                          * (or IPv4 for that matter) it does not matter. If
 1277                          * they don't support that type of address, they can
 1278                          * NOT possibly get that packet type... i.e. with no
 1279                          * IPv6 you can't receive a IPv6 packet. so we can
 1280                          * safely ignore this one. If we ever added support
 1281                          * for HOSTNAME Addresses, then we would need to do
 1282                          * something here.
 1283                          */
 1284                         break;
 1285                 case SCTP_CAUSE_UNRECOG_CHUNK:
 1286                         if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
 1287                                 struct sctp_error_unrecognized_chunk *unrec_chunk;
 1288 
 1289                                 unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
 1290                                 sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type, net);
 1291                         }
 1292                         break;
 1293                 case SCTP_CAUSE_UNRECOG_PARAM:
 1294                         /* XXX: We only consider the first parameter */
 1295                         if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
 1296                                 struct sctp_paramhdr *unrec_parameter;
 1297 
 1298                                 unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
 1299                                 sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
 1300                         }
 1301                         break;
 1302                 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
 1303                         /*
 1304                          * We ignore this since the timer will drive out a
 1305                          * new cookie anyway and there timer will drive us
 1306                          * to send a SHUTDOWN_COMPLETE. We can't send one
 1307                          * here since we don't have their tag.
 1308                          */
 1309                         break;
 1310                 case SCTP_CAUSE_DELETING_LAST_ADDR:
 1311                 case SCTP_CAUSE_RESOURCE_SHORTAGE:
 1312                 case SCTP_CAUSE_DELETING_SRC_ADDR:
 1313                         /*
 1314                          * We should NOT get these here, but in a
 1315                          * ASCONF-ACK.
 1316                          */
 1317                         SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
 1318                             cause_code);
 1319                         break;
 1320                 case SCTP_CAUSE_OUT_OF_RESC:
 1321                         /*
 1322                          * And what, pray tell do we do with the fact that
 1323                          * the peer is out of resources? Not really sure we
 1324                          * could do anything but abort. I suspect this
 1325                          * should have came WITH an abort instead of in a
 1326                          * OP-ERROR.
 1327                          */
 1328                         break;
 1329                 default:
 1330                         SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
 1331                             cause_code);
 1332                         break;
 1333                 }
 1334                 adjust = SCTP_SIZE32(cause_length);
 1335                 if (remaining_length >= adjust) {
 1336                         remaining_length -= adjust;
 1337                 } else {
 1338                         remaining_length = 0;
 1339                 }
 1340                 cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
 1341         }
 1342         sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
 1343         return (0);
 1344 }
 1345 
 1346 static int
 1347 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
 1348     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
 1349     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
 1350     struct sctp_nets *net, int *abort_no_unlock,
 1351     uint8_t mflowtype, uint32_t mflowid,
 1352     uint32_t vrf_id)
 1353 {
 1354         struct sctp_init_ack *init_ack;
 1355         struct mbuf *op_err;
 1356 
 1357         SCTPDBG(SCTP_DEBUG_INPUT2,
 1358             "sctp_handle_init_ack: handling INIT-ACK\n");
 1359 
 1360         if (stcb == NULL) {
 1361                 SCTPDBG(SCTP_DEBUG_INPUT2,
 1362                     "sctp_handle_init_ack: TCB is null\n");
 1363                 return (-1);
 1364         }
 1365         if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
 1366                 /* Invalid length */
 1367                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
 1368                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
 1369                     src, dst, sh, op_err,
 1370                     mflowtype, mflowid,
 1371                     vrf_id, net->port);
 1372                 *abort_no_unlock = 1;
 1373                 return (-1);
 1374         }
 1375         init_ack = &cp->init;
 1376         /* validate parameters */
 1377         if (init_ack->initiate_tag == 0) {
 1378                 /* protocol error... send an abort */
 1379                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
 1380                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
 1381                     src, dst, sh, op_err,
 1382                     mflowtype, mflowid,
 1383                     vrf_id, net->port);
 1384                 *abort_no_unlock = 1;
 1385                 return (-1);
 1386         }
 1387         if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
 1388                 /* protocol error... send an abort */
 1389                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
 1390                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
 1391                     src, dst, sh, op_err,
 1392                     mflowtype, mflowid,
 1393                     vrf_id, net->port);
 1394                 *abort_no_unlock = 1;
 1395                 return (-1);
 1396         }
 1397         if (init_ack->num_inbound_streams == 0) {
 1398                 /* protocol error... send an abort */
 1399                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
 1400                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
 1401                     src, dst, sh, op_err,
 1402                     mflowtype, mflowid,
 1403                     vrf_id, net->port);
 1404                 *abort_no_unlock = 1;
 1405                 return (-1);
 1406         }
 1407         if (init_ack->num_outbound_streams == 0) {
 1408                 /* protocol error... send an abort */
 1409                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
 1410                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
 1411                     src, dst, sh, op_err,
 1412                     mflowtype, mflowid,
 1413                     vrf_id, net->port);
 1414                 *abort_no_unlock = 1;
 1415                 return (-1);
 1416         }
 1417         /* process according to association state... */
 1418         switch (SCTP_GET_STATE(stcb)) {
 1419         case SCTP_STATE_COOKIE_WAIT:
 1420                 /* this is the expected state for this chunk */
 1421                 /* process the INIT-ACK parameters */
 1422                 if (stcb->asoc.primary_destination->dest_state &
 1423                     SCTP_ADDR_UNCONFIRMED) {
 1424                         /*
 1425                          * The primary is where we sent the INIT, we can
 1426                          * always consider it confirmed when the INIT-ACK is
 1427                          * returned. Do this before we load addresses
 1428                          * though.
 1429                          */
 1430                         stcb->asoc.primary_destination->dest_state &=
 1431                             ~SCTP_ADDR_UNCONFIRMED;
 1432                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
 1433                             stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
 1434                 }
 1435                 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
 1436                     net, abort_no_unlock,
 1437                     mflowtype, mflowid,
 1438                     vrf_id) < 0) {
 1439                         /* error in parsing parameters */
 1440                         return (-1);
 1441                 }
 1442                 /* update our state */
 1443                 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
 1444                 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
 1445 
 1446                 /* reset the RTO calc */
 1447                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
 1448                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
 1449                             stcb->asoc.overall_error_count,
 1450                             0,
 1451                             SCTP_FROM_SCTP_INPUT,
 1452                             __LINE__);
 1453                 }
 1454                 stcb->asoc.overall_error_count = 0;
 1455                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
 1456                 /*
 1457                  * collapse the init timer back in case of a exponential
 1458                  * backoff
 1459                  */
 1460                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
 1461                     stcb, net);
 1462                 /*
 1463                  * the send at the end of the inbound data processing will
 1464                  * cause the cookie to be sent
 1465                  */
 1466                 break;
 1467         case SCTP_STATE_SHUTDOWN_SENT:
 1468                 /* incorrect state... discard */
 1469                 break;
 1470         case SCTP_STATE_COOKIE_ECHOED:
 1471                 /* incorrect state... discard */
 1472                 break;
 1473         case SCTP_STATE_OPEN:
 1474                 /* incorrect state... discard */
 1475                 break;
 1476         case SCTP_STATE_EMPTY:
 1477         case SCTP_STATE_INUSE:
 1478         default:
 1479                 /* incorrect state... discard */
 1480                 return (-1);
 1481                 break;
 1482         }
 1483         SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
 1484         return (0);
 1485 }
 1486 
 1487 static struct sctp_tcb *
 1488 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
 1489     struct sockaddr *src, struct sockaddr *dst,
 1490     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
 1491     struct sctp_inpcb *inp, struct sctp_nets **netp,
 1492     struct sockaddr *init_src, int *notification,
 1493     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
 1494     uint8_t mflowtype, uint32_t mflowid,
 1495     uint32_t vrf_id, uint16_t port);
 1496 
 1497 
 1498 /*
 1499  * handle a state cookie for an existing association m: input packet mbuf
 1500  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
 1501  * "split" mbuf and the cookie signature does not exist offset: offset into
 1502  * mbuf to the cookie-echo chunk
 1503  */
 1504 static struct sctp_tcb *
 1505 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
 1506     struct sockaddr *src, struct sockaddr *dst,
 1507     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
 1508     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
 1509     struct sockaddr *init_src, int *notification,
 1510     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
 1511     uint8_t mflowtype, uint32_t mflowid,
 1512     uint32_t vrf_id, uint16_t port)
 1513 {
 1514         struct sctp_association *asoc;
 1515         struct sctp_init_chunk *init_cp, init_buf;
 1516         struct sctp_init_ack_chunk *initack_cp, initack_buf;
 1517         struct sctp_nets *net;
 1518         struct mbuf *op_err;
 1519         struct timeval old;
 1520         int init_offset, initack_offset, i;
 1521         int retval;
 1522         int spec_flag = 0;
 1523         uint32_t how_indx;
 1524 #if defined(SCTP_DETAILED_STR_STATS)
 1525         int j;
 1526 #endif
 1527 
 1528         net = *netp;
 1529         /* I know that the TCB is non-NULL from the caller */
 1530         asoc = &stcb->asoc;
 1531         for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
 1532                 if (asoc->cookie_how[how_indx] == 0)
 1533                         break;
 1534         }
 1535         if (how_indx < sizeof(asoc->cookie_how)) {
 1536                 asoc->cookie_how[how_indx] = 1;
 1537         }
 1538         if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
 1539                 /* SHUTDOWN came in after sending INIT-ACK */
 1540                 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
 1541                 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
 1542                 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
 1543                     mflowtype, mflowid, inp->fibnum,
 1544                     vrf_id, net->port);
 1545                 if (how_indx < sizeof(asoc->cookie_how))
 1546                         asoc->cookie_how[how_indx] = 2;
 1547                 return (NULL);
 1548         }
 1549         /*
 1550          * find and validate the INIT chunk in the cookie (peer's info) the
 1551          * INIT should start after the cookie-echo header struct (chunk
 1552          * header, state cookie header struct)
 1553          */
 1554         init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
 1555 
 1556         init_cp = (struct sctp_init_chunk *)
 1557             sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
 1558             (uint8_t *)&init_buf);
 1559         if (init_cp == NULL) {
 1560                 /* could not pull a INIT chunk in cookie */
 1561                 return (NULL);
 1562         }
 1563         if (init_cp->ch.chunk_type != SCTP_INITIATION) {
 1564                 return (NULL);
 1565         }
 1566         /*
 1567          * find and validate the INIT-ACK chunk in the cookie (my info) the
 1568          * INIT-ACK follows the INIT chunk
 1569          */
 1570         initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
 1571         initack_cp = (struct sctp_init_ack_chunk *)
 1572             sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
 1573             (uint8_t *)&initack_buf);
 1574         if (initack_cp == NULL) {
 1575                 /* could not pull INIT-ACK chunk in cookie */
 1576                 return (NULL);
 1577         }
 1578         if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
 1579                 return (NULL);
 1580         }
 1581         if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
 1582             (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
 1583                 /*
 1584                  * case D in Section 5.2.4 Table 2: MMAA process accordingly
 1585                  * to get into the OPEN state
 1586                  */
 1587                 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
 1588                         /*-
 1589                          * Opps, this means that we somehow generated two vtag's
 1590                          * the same. I.e. we did:
 1591                          *  Us               Peer
 1592                          *   <---INIT(tag=a)------
 1593                          *   ----INIT-ACK(tag=t)-->
 1594                          *   ----INIT(tag=t)------> *1
 1595                          *   <---INIT-ACK(tag=a)---
 1596                          *   <----CE(tag=t)------------- *2
 1597                          *
 1598                          * At point *1 we should be generating a different
 1599                          * tag t'. Which means we would throw away the CE and send
 1600                          * ours instead. Basically this is case C (throw away side).
 1601                          */
 1602                         if (how_indx < sizeof(asoc->cookie_how))
 1603                                 asoc->cookie_how[how_indx] = 17;
 1604                         return (NULL);
 1605 
 1606                 }
 1607                 switch (SCTP_GET_STATE(stcb)) {
 1608                 case SCTP_STATE_COOKIE_WAIT:
 1609                 case SCTP_STATE_COOKIE_ECHOED:
 1610                         /*
 1611                          * INIT was sent but got a COOKIE_ECHO with the
 1612                          * correct tags... just accept it...but we must
 1613                          * process the init so that we can make sure we have
 1614                          * the right seq no's.
 1615                          */
 1616                         /* First we must process the INIT !! */
 1617                         retval = sctp_process_init(init_cp, stcb);
 1618                         if (retval < 0) {
 1619                                 if (how_indx < sizeof(asoc->cookie_how))
 1620                                         asoc->cookie_how[how_indx] = 3;
 1621                                 return (NULL);
 1622                         }
 1623                         /* we have already processed the INIT so no problem */
 1624                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
 1625                             stcb, net,
 1626                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
 1627                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
 1628                             stcb, net,
 1629                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
 1630                         /* update current state */
 1631                         if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
 1632                                 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
 1633                         else
 1634                                 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
 1635 
 1636                         SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
 1637                         if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
 1638                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
 1639                                     stcb->sctp_ep, stcb, asoc->primary_destination);
 1640                         }
 1641                         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
 1642                         sctp_stop_all_cookie_timers(stcb);
 1643                         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 1644                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
 1645                             (!SCTP_IS_LISTENING(inp))) {
 1646 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1647                                 struct socket *so;
 1648 #endif
 1649                                 /*
 1650                                  * Here is where collision would go if we
 1651                                  * did a connect() and instead got a
 1652                                  * init/init-ack/cookie done before the
 1653                                  * init-ack came back..
 1654                                  */
 1655                                 stcb->sctp_ep->sctp_flags |=
 1656                                     SCTP_PCB_FLAGS_CONNECTED;
 1657 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1658                                 so = SCTP_INP_SO(stcb->sctp_ep);
 1659                                 atomic_add_int(&stcb->asoc.refcnt, 1);
 1660                                 SCTP_TCB_UNLOCK(stcb);
 1661                                 SCTP_SOCKET_LOCK(so, 1);
 1662                                 SCTP_TCB_LOCK(stcb);
 1663                                 atomic_add_int(&stcb->asoc.refcnt, -1);
 1664                                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
 1665                                         SCTP_SOCKET_UNLOCK(so, 1);
 1666                                         return (NULL);
 1667                                 }
 1668 #endif
 1669                                 soisconnected(stcb->sctp_socket);
 1670 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1671                                 SCTP_SOCKET_UNLOCK(so, 1);
 1672 #endif
 1673                         }
 1674                         /* notify upper layer */
 1675                         *notification = SCTP_NOTIFY_ASSOC_UP;
 1676                         /*
 1677                          * since we did not send a HB make sure we don't
 1678                          * double things
 1679                          */
 1680                         old.tv_sec = cookie->time_entered.tv_sec;
 1681                         old.tv_usec = cookie->time_entered.tv_usec;
 1682                         net->hb_responded = 1;
 1683                         net->RTO = sctp_calculate_rto(stcb, asoc, net,
 1684                             &old,
 1685                             SCTP_RTT_FROM_NON_DATA);
 1686 
 1687                         if (stcb->asoc.sctp_autoclose_ticks &&
 1688                             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
 1689                                 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
 1690                                     inp, stcb, NULL);
 1691                         }
 1692                         break;
 1693                 default:
 1694                         /*
 1695                          * we're in the OPEN state (or beyond), so peer must
 1696                          * have simply lost the COOKIE-ACK
 1697                          */
 1698                         break;
 1699                 }               /* end switch */
 1700                 sctp_stop_all_cookie_timers(stcb);
 1701                 /*
 1702                  * We ignore the return code here.. not sure if we should
 1703                  * somehow abort.. but we do have an existing asoc. This
 1704                  * really should not fail.
 1705                  */
 1706                 if (sctp_load_addresses_from_init(stcb, m,
 1707                     init_offset + sizeof(struct sctp_init_chunk),
 1708                     initack_offset, src, dst, init_src, stcb->asoc.port)) {
 1709                         if (how_indx < sizeof(asoc->cookie_how))
 1710                                 asoc->cookie_how[how_indx] = 4;
 1711                         return (NULL);
 1712                 }
 1713                 /* respond with a COOKIE-ACK */
 1714                 sctp_toss_old_cookies(stcb, asoc);
 1715                 sctp_send_cookie_ack(stcb);
 1716                 if (how_indx < sizeof(asoc->cookie_how))
 1717                         asoc->cookie_how[how_indx] = 5;
 1718                 return (stcb);
 1719         }
 1720 
 1721         if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
 1722             ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
 1723             cookie->tie_tag_my_vtag == 0 &&
 1724             cookie->tie_tag_peer_vtag == 0) {
 1725                 /*
 1726                  * case C in Section 5.2.4 Table 2: XMOO silently discard
 1727                  */
 1728                 if (how_indx < sizeof(asoc->cookie_how))
 1729                         asoc->cookie_how[how_indx] = 6;
 1730                 return (NULL);
 1731         }
 1732         /*
 1733          * If nat support, and the below and stcb is established, send back
 1734          * a ABORT(colliding state) if we are established.
 1735          */
 1736         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
 1737             (asoc->peer_supports_nat) &&
 1738             ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
 1739             ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
 1740             (asoc->peer_vtag == 0)))) {
 1741                 /*
 1742                  * Special case - Peer's support nat. We may have two init's
 1743                  * that we gave out the same tag on since one was not
 1744                  * established.. i.e. we get INIT from host-1 behind the nat
 1745                  * and we respond tag-a, we get a INIT from host-2 behind
 1746                  * the nat and we get tag-a again. Then we bring up host-1
 1747                  * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
 1748                  * Now we have colliding state. We must send an abort here
 1749                  * with colliding state indication.
 1750                  */
 1751                 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
 1752                 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
 1753                     mflowtype, mflowid, inp->fibnum,
 1754                     vrf_id, port);
 1755                 return (NULL);
 1756         }
 1757         if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
 1758             ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
 1759             (asoc->peer_vtag == 0))) {
 1760                 /*
 1761                  * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
 1762                  * should be ok, re-accept peer info
 1763                  */
 1764                 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
 1765                         /*
 1766                          * Extension of case C. If we hit this, then the
 1767                          * random number generator returned the same vtag
 1768                          * when we first sent our INIT-ACK and when we later
 1769                          * sent our INIT. The side with the seq numbers that
 1770                          * are different will be the one that normnally
 1771                          * would have hit case C. This in effect "extends"
 1772                          * our vtags in this collision case to be 64 bits.
 1773                          * The same collision could occur aka you get both
 1774                          * vtag and seq number the same twice in a row.. but
 1775                          * is much less likely. If it did happen then we
 1776                          * would proceed through and bring up the assoc.. we
 1777                          * may end up with the wrong stream setup however..
 1778                          * which would be bad.. but there is no way to
 1779                          * tell.. until we send on a stream that does not
 1780                          * exist :-)
 1781                          */
 1782                         if (how_indx < sizeof(asoc->cookie_how))
 1783                                 asoc->cookie_how[how_indx] = 7;
 1784 
 1785                         return (NULL);
 1786                 }
 1787                 if (how_indx < sizeof(asoc->cookie_how))
 1788                         asoc->cookie_how[how_indx] = 8;
 1789                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
 1790                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
 1791                 sctp_stop_all_cookie_timers(stcb);
 1792                 /*
 1793                  * since we did not send a HB make sure we don't double
 1794                  * things
 1795                  */
 1796                 net->hb_responded = 1;
 1797                 if (stcb->asoc.sctp_autoclose_ticks &&
 1798                     sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
 1799                         sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
 1800                             NULL);
 1801                 }
 1802                 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
 1803                 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
 1804 
 1805                 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
 1806                         /*
 1807                          * Ok the peer probably discarded our data (if we
 1808                          * echoed a cookie+data). So anything on the
 1809                          * sent_queue should be marked for retransmit, we
 1810                          * may not get something to kick us so it COULD
 1811                          * still take a timeout to move these.. but it can't
 1812                          * hurt to mark them.
 1813                          */
 1814                         struct sctp_tmit_chunk *chk;
 1815 
 1816                         TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
 1817                                 if (chk->sent < SCTP_DATAGRAM_RESEND) {
 1818                                         chk->sent = SCTP_DATAGRAM_RESEND;
 1819                                         sctp_flight_size_decrease(chk);
 1820                                         sctp_total_flight_decrease(stcb, chk);
 1821                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
 1822                                         spec_flag++;
 1823                                 }
 1824                         }
 1825 
 1826                 }
 1827                 /* process the INIT info (peer's info) */
 1828                 retval = sctp_process_init(init_cp, stcb);
 1829                 if (retval < 0) {
 1830                         if (how_indx < sizeof(asoc->cookie_how))
 1831                                 asoc->cookie_how[how_indx] = 9;
 1832                         return (NULL);
 1833                 }
 1834                 if (sctp_load_addresses_from_init(stcb, m,
 1835                     init_offset + sizeof(struct sctp_init_chunk),
 1836                     initack_offset, src, dst, init_src, stcb->asoc.port)) {
 1837                         if (how_indx < sizeof(asoc->cookie_how))
 1838                                 asoc->cookie_how[how_indx] = 10;
 1839                         return (NULL);
 1840                 }
 1841                 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
 1842                     (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
 1843                         *notification = SCTP_NOTIFY_ASSOC_UP;
 1844 
 1845                         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 1846                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
 1847                             (!SCTP_IS_LISTENING(inp))) {
 1848 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1849                                 struct socket *so;
 1850 #endif
 1851                                 stcb->sctp_ep->sctp_flags |=
 1852                                     SCTP_PCB_FLAGS_CONNECTED;
 1853 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1854                                 so = SCTP_INP_SO(stcb->sctp_ep);
 1855                                 atomic_add_int(&stcb->asoc.refcnt, 1);
 1856                                 SCTP_TCB_UNLOCK(stcb);
 1857                                 SCTP_SOCKET_LOCK(so, 1);
 1858                                 SCTP_TCB_LOCK(stcb);
 1859                                 atomic_add_int(&stcb->asoc.refcnt, -1);
 1860                                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
 1861                                         SCTP_SOCKET_UNLOCK(so, 1);
 1862                                         return (NULL);
 1863                                 }
 1864 #endif
 1865                                 soisconnected(stcb->sctp_socket);
 1866 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1867                                 SCTP_SOCKET_UNLOCK(so, 1);
 1868 #endif
 1869                         }
 1870                         if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
 1871                                 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
 1872                         else
 1873                                 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
 1874                         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
 1875                 } else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
 1876                         SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
 1877                 } else {
 1878                         SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
 1879                 }
 1880                 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
 1881                 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
 1882                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
 1883                             stcb->sctp_ep, stcb, asoc->primary_destination);
 1884                 }
 1885                 sctp_stop_all_cookie_timers(stcb);
 1886                 sctp_toss_old_cookies(stcb, asoc);
 1887                 sctp_send_cookie_ack(stcb);
 1888                 if (spec_flag) {
 1889                         /*
 1890                          * only if we have retrans set do we do this. What
 1891                          * this call does is get only the COOKIE-ACK out and
 1892                          * then when we return the normal call to
 1893                          * sctp_chunk_output will get the retrans out behind
 1894                          * this.
 1895                          */
 1896                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
 1897                 }
 1898                 if (how_indx < sizeof(asoc->cookie_how))
 1899                         asoc->cookie_how[how_indx] = 11;
 1900 
 1901                 return (stcb);
 1902         }
 1903         if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
 1904             ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
 1905             cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
 1906             cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
 1907             cookie->tie_tag_peer_vtag != 0) {
 1908                 struct sctpasochead *head;
 1909 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1910                 struct socket *so;
 1911 #endif
 1912 
 1913                 if (asoc->peer_supports_nat) {
 1914                         /*
 1915                          * This is a gross gross hack. Just call the
 1916                          * cookie_new code since we are allowing a duplicate
 1917                          * association. I hope this works...
 1918                          */
 1919                         return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
 1920                             sh, cookie, cookie_len,
 1921                             inp, netp, init_src, notification,
 1922                             auth_skipped, auth_offset, auth_len,
 1923                             mflowtype, mflowid,
 1924                             vrf_id, port));
 1925                 }
 1926                 /*
 1927                  * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
 1928                  */
 1929                 /* temp code */
 1930                 if (how_indx < sizeof(asoc->cookie_how))
 1931                         asoc->cookie_how[how_indx] = 12;
 1932                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
 1933                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
 1934                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
 1935                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
 1936 
 1937                 /* notify upper layer */
 1938                 *notification = SCTP_NOTIFY_ASSOC_RESTART;
 1939                 atomic_add_int(&stcb->asoc.refcnt, 1);
 1940                 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
 1941                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
 1942                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
 1943                         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
 1944                 }
 1945                 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
 1946                         SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
 1947                 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
 1948                         SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
 1949                 }
 1950                 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
 1951                         SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
 1952                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
 1953                             stcb->sctp_ep, stcb, asoc->primary_destination);
 1954 
 1955                 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
 1956                         /* move to OPEN state, if not in SHUTDOWN_SENT */
 1957                         SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
 1958                 }
 1959                 asoc->pre_open_streams =
 1960                     ntohs(initack_cp->init.num_outbound_streams);
 1961                 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
 1962                 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
 1963                 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
 1964 
 1965                 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
 1966 
 1967                 asoc->str_reset_seq_in = asoc->init_seq_number;
 1968 
 1969                 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
 1970                 if (asoc->mapping_array) {
 1971                         memset(asoc->mapping_array, 0,
 1972                             asoc->mapping_array_size);
 1973                 }
 1974                 if (asoc->nr_mapping_array) {
 1975                         memset(asoc->nr_mapping_array, 0,
 1976                             asoc->mapping_array_size);
 1977                 }
 1978                 SCTP_TCB_UNLOCK(stcb);
 1979 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 1980                 so = SCTP_INP_SO(stcb->sctp_ep);
 1981                 SCTP_SOCKET_LOCK(so, 1);
 1982 #endif
 1983                 SCTP_INP_INFO_WLOCK();
 1984                 SCTP_INP_WLOCK(stcb->sctp_ep);
 1985                 SCTP_TCB_LOCK(stcb);
 1986                 atomic_add_int(&stcb->asoc.refcnt, -1);
 1987                 /* send up all the data */
 1988                 SCTP_TCB_SEND_LOCK(stcb);
 1989 
 1990                 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
 1991                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
 1992                         stcb->asoc.strmout[i].chunks_on_queues = 0;
 1993 #if defined(SCTP_DETAILED_STR_STATS)
 1994                         for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
 1995                                 asoc->strmout[i].abandoned_sent[j] = 0;
 1996                                 asoc->strmout[i].abandoned_unsent[j] = 0;
 1997                         }
 1998 #else
 1999                         asoc->strmout[i].abandoned_sent[0] = 0;
 2000                         asoc->strmout[i].abandoned_unsent[0] = 0;
 2001 #endif
 2002                         stcb->asoc.strmout[i].sid = i;
 2003                         stcb->asoc.strmout[i].next_mid_ordered = 0;
 2004                         stcb->asoc.strmout[i].next_mid_unordered = 0;
 2005                         stcb->asoc.strmout[i].last_msg_incomplete = 0;
 2006                 }
 2007                 /* process the INIT-ACK info (my info) */
 2008                 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
 2009                 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
 2010 
 2011                 /* pull from vtag hash */
 2012                 LIST_REMOVE(stcb, sctp_asocs);
 2013                 /* re-insert to new vtag position */
 2014                 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
 2015                     SCTP_BASE_INFO(hashasocmark))];
 2016                 /*
 2017                  * put it in the bucket in the vtag hash of assoc's for the
 2018                  * system
 2019                  */
 2020                 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
 2021 
 2022                 SCTP_TCB_SEND_UNLOCK(stcb);
 2023                 SCTP_INP_WUNLOCK(stcb->sctp_ep);
 2024                 SCTP_INP_INFO_WUNLOCK();
 2025 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2026                 SCTP_SOCKET_UNLOCK(so, 1);
 2027 #endif
 2028                 asoc->total_flight = 0;
 2029                 asoc->total_flight_count = 0;
 2030                 /* process the INIT info (peer's info) */
 2031                 retval = sctp_process_init(init_cp, stcb);
 2032                 if (retval < 0) {
 2033                         if (how_indx < sizeof(asoc->cookie_how))
 2034                                 asoc->cookie_how[how_indx] = 13;
 2035 
 2036                         return (NULL);
 2037                 }
 2038                 /*
 2039                  * since we did not send a HB make sure we don't double
 2040                  * things
 2041                  */
 2042                 net->hb_responded = 1;
 2043 
 2044                 if (sctp_load_addresses_from_init(stcb, m,
 2045                     init_offset + sizeof(struct sctp_init_chunk),
 2046                     initack_offset, src, dst, init_src, stcb->asoc.port)) {
 2047                         if (how_indx < sizeof(asoc->cookie_how))
 2048                                 asoc->cookie_how[how_indx] = 14;
 2049 
 2050                         return (NULL);
 2051                 }
 2052                 /* respond with a COOKIE-ACK */
 2053                 sctp_stop_all_cookie_timers(stcb);
 2054                 sctp_toss_old_cookies(stcb, asoc);
 2055                 sctp_send_cookie_ack(stcb);
 2056                 if (how_indx < sizeof(asoc->cookie_how))
 2057                         asoc->cookie_how[how_indx] = 15;
 2058 
 2059                 return (stcb);
 2060         }
 2061         if (how_indx < sizeof(asoc->cookie_how))
 2062                 asoc->cookie_how[how_indx] = 16;
 2063         /* all other cases... */
 2064         return (NULL);
 2065 }
 2066 
 2067 
 2068 /*
 2069  * handle a state cookie for a new association m: input packet mbuf chain--
 2070  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
 2071  * and the cookie signature does not exist offset: offset into mbuf to the
 2072  * cookie-echo chunk length: length of the cookie chunk to: where the init
 2073  * was from returns a new TCB
 2074  */
 2075 static struct sctp_tcb *
 2076 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
 2077     struct sockaddr *src, struct sockaddr *dst,
 2078     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
 2079     struct sctp_inpcb *inp, struct sctp_nets **netp,
 2080     struct sockaddr *init_src, int *notification,
 2081     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
 2082     uint8_t mflowtype, uint32_t mflowid,
 2083     uint32_t vrf_id, uint16_t port)
 2084 {
 2085         struct sctp_tcb *stcb;
 2086         struct sctp_init_chunk *init_cp, init_buf;
 2087         struct sctp_init_ack_chunk *initack_cp, initack_buf;
 2088         union sctp_sockstore store;
 2089         struct sctp_association *asoc;
 2090         int init_offset, initack_offset, initack_limit;
 2091         int retval;
 2092         int error = 0;
 2093         uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
 2094 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2095         struct socket *so;
 2096 
 2097         so = SCTP_INP_SO(inp);
 2098 #endif
 2099 
 2100         /*
 2101          * find and validate the INIT chunk in the cookie (peer's info) the
 2102          * INIT should start after the cookie-echo header struct (chunk
 2103          * header, state cookie header struct)
 2104          */
 2105         init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
 2106         init_cp = (struct sctp_init_chunk *)
 2107             sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
 2108             (uint8_t *)&init_buf);
 2109         if (init_cp == NULL) {
 2110                 /* could not pull a INIT chunk in cookie */
 2111                 SCTPDBG(SCTP_DEBUG_INPUT1,
 2112                     "process_cookie_new: could not pull INIT chunk hdr\n");
 2113                 return (NULL);
 2114         }
 2115         if (init_cp->ch.chunk_type != SCTP_INITIATION) {
 2116                 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
 2117                 return (NULL);
 2118         }
 2119         initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
 2120         /*
 2121          * find and validate the INIT-ACK chunk in the cookie (my info) the
 2122          * INIT-ACK follows the INIT chunk
 2123          */
 2124         initack_cp = (struct sctp_init_ack_chunk *)
 2125             sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
 2126             (uint8_t *)&initack_buf);
 2127         if (initack_cp == NULL) {
 2128                 /* could not pull INIT-ACK chunk in cookie */
 2129                 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
 2130                 return (NULL);
 2131         }
 2132         if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
 2133                 return (NULL);
 2134         }
 2135         /*
 2136          * NOTE: We can't use the INIT_ACK's chk_length to determine the
 2137          * "initack_limit" value.  This is because the chk_length field
 2138          * includes the length of the cookie, but the cookie is omitted when
 2139          * the INIT and INIT_ACK are tacked onto the cookie...
 2140          */
 2141         initack_limit = offset + cookie_len;
 2142 
 2143         /*
 2144          * now that we know the INIT/INIT-ACK are in place, create a new TCB
 2145          * and popluate
 2146          */
 2147 
 2148         /*
 2149          * Here we do a trick, we set in NULL for the proc/thread argument.
 2150          * We do this since in effect we only use the p argument when the
 2151          * socket is unbound and we must do an implicit bind. Since we are
 2152          * getting a cookie, we cannot be unbound.
 2153          */
 2154         stcb = sctp_aloc_assoc(inp, init_src, &error,
 2155             ntohl(initack_cp->init.initiate_tag), vrf_id,
 2156             ntohs(initack_cp->init.num_outbound_streams),
 2157             port,
 2158             (struct thread *)NULL
 2159             );
 2160         if (stcb == NULL) {
 2161                 struct mbuf *op_err;
 2162 
 2163                 /* memory problem? */
 2164                 SCTPDBG(SCTP_DEBUG_INPUT1,
 2165                     "process_cookie_new: no room for another TCB!\n");
 2166                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
 2167                 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
 2168                     src, dst, sh, op_err,
 2169                     mflowtype, mflowid,
 2170                     vrf_id, port);
 2171                 return (NULL);
 2172         }
 2173         /* get the correct sctp_nets */
 2174         if (netp)
 2175                 *netp = sctp_findnet(stcb, init_src);
 2176 
 2177         asoc = &stcb->asoc;
 2178         /* get scope variables out of cookie */
 2179         asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
 2180         asoc->scope.site_scope = cookie->site_scope;
 2181         asoc->scope.local_scope = cookie->local_scope;
 2182         asoc->scope.loopback_scope = cookie->loopback_scope;
 2183 
 2184         if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
 2185             (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
 2186                 struct mbuf *op_err;
 2187 
 2188                 /*
 2189                  * Houston we have a problem. The EP changed while the
 2190                  * cookie was in flight. Only recourse is to abort the
 2191                  * association.
 2192                  */
 2193                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
 2194                 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
 2195                     src, dst, sh, op_err,
 2196                     mflowtype, mflowid,
 2197                     vrf_id, port);
 2198 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2199                 atomic_add_int(&stcb->asoc.refcnt, 1);
 2200                 SCTP_TCB_UNLOCK(stcb);
 2201                 SCTP_SOCKET_LOCK(so, 1);
 2202                 SCTP_TCB_LOCK(stcb);
 2203                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 2204 #endif
 2205                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 2206                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
 2207 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2208                 SCTP_SOCKET_UNLOCK(so, 1);
 2209 #endif
 2210                 return (NULL);
 2211         }
 2212         /* process the INIT-ACK info (my info) */
 2213         asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
 2214         asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
 2215         asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
 2216         asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
 2217         asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
 2218         asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
 2219         asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
 2220         asoc->str_reset_seq_in = asoc->init_seq_number;
 2221 
 2222         asoc->advanced_peer_ack_point = asoc->last_acked_seq;
 2223 
 2224         /* process the INIT info (peer's info) */
 2225         if (netp)
 2226                 retval = sctp_process_init(init_cp, stcb);
 2227         else
 2228                 retval = 0;
 2229         if (retval < 0) {
 2230 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2231                 atomic_add_int(&stcb->asoc.refcnt, 1);
 2232                 SCTP_TCB_UNLOCK(stcb);
 2233                 SCTP_SOCKET_LOCK(so, 1);
 2234                 SCTP_TCB_LOCK(stcb);
 2235                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 2236 #endif
 2237                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 2238                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
 2239 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2240                 SCTP_SOCKET_UNLOCK(so, 1);
 2241 #endif
 2242                 return (NULL);
 2243         }
 2244         /* load all addresses */
 2245         if (sctp_load_addresses_from_init(stcb, m,
 2246             init_offset + sizeof(struct sctp_init_chunk), initack_offset,
 2247             src, dst, init_src, port)) {
 2248 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2249                 atomic_add_int(&stcb->asoc.refcnt, 1);
 2250                 SCTP_TCB_UNLOCK(stcb);
 2251                 SCTP_SOCKET_LOCK(so, 1);
 2252                 SCTP_TCB_LOCK(stcb);
 2253                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 2254 #endif
 2255                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 2256                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
 2257 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2258                 SCTP_SOCKET_UNLOCK(so, 1);
 2259 #endif
 2260                 return (NULL);
 2261         }
 2262         /*
 2263          * verify any preceding AUTH chunk that was skipped
 2264          */
 2265         /* pull the local authentication parameters from the cookie/init-ack */
 2266         sctp_auth_get_cookie_params(stcb, m,
 2267             initack_offset + sizeof(struct sctp_init_ack_chunk),
 2268             initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
 2269         if (auth_skipped) {
 2270                 struct sctp_auth_chunk *auth;
 2271 
 2272                 auth = (struct sctp_auth_chunk *)
 2273                     sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
 2274                 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
 2275                         /* auth HMAC failed, dump the assoc and packet */
 2276                         SCTPDBG(SCTP_DEBUG_AUTH1,
 2277                             "COOKIE-ECHO: AUTH failed\n");
 2278 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2279                         atomic_add_int(&stcb->asoc.refcnt, 1);
 2280                         SCTP_TCB_UNLOCK(stcb);
 2281                         SCTP_SOCKET_LOCK(so, 1);
 2282                         SCTP_TCB_LOCK(stcb);
 2283                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 2284 #endif
 2285                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 2286                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
 2287 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2288                         SCTP_SOCKET_UNLOCK(so, 1);
 2289 #endif
 2290                         return (NULL);
 2291                 } else {
 2292                         /* remaining chunks checked... good to go */
 2293                         stcb->asoc.authenticated = 1;
 2294                 }
 2295         }
 2296 
 2297         /*
 2298          * if we're doing ASCONFs, check to see if we have any new local
 2299          * addresses that need to get added to the peer (eg. addresses
 2300          * changed while cookie echo in flight).  This needs to be done
 2301          * after we go to the OPEN state to do the correct asconf
 2302          * processing. else, make sure we have the correct addresses in our
 2303          * lists
 2304          */
 2305 
 2306         /* warning, we re-use sin, sin6, sa_store here! */
 2307         /* pull in local_address (our "from" address) */
 2308         switch (cookie->laddr_type) {
 2309 #ifdef INET
 2310         case SCTP_IPV4_ADDRESS:
 2311                 /* source addr is IPv4 */
 2312                 memset(&store.sin, 0, sizeof(struct sockaddr_in));
 2313                 store.sin.sin_family = AF_INET;
 2314                 store.sin.sin_len = sizeof(struct sockaddr_in);
 2315                 store.sin.sin_addr.s_addr = cookie->laddress[0];
 2316                 break;
 2317 #endif
 2318 #ifdef INET6
 2319         case SCTP_IPV6_ADDRESS:
 2320                 /* source addr is IPv6 */
 2321                 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
 2322                 store.sin6.sin6_family = AF_INET6;
 2323                 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
 2324                 store.sin6.sin6_scope_id = cookie->scope_id;
 2325                 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
 2326                 break;
 2327 #endif
 2328         default:
 2329 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2330                 atomic_add_int(&stcb->asoc.refcnt, 1);
 2331                 SCTP_TCB_UNLOCK(stcb);
 2332                 SCTP_SOCKET_LOCK(so, 1);
 2333                 SCTP_TCB_LOCK(stcb);
 2334                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 2335 #endif
 2336                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 2337                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
 2338 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2339                 SCTP_SOCKET_UNLOCK(so, 1);
 2340 #endif
 2341                 return (NULL);
 2342         }
 2343 
 2344         /* update current state */
 2345         SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
 2346         SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
 2347         if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
 2348                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
 2349                     stcb->sctp_ep, stcb, asoc->primary_destination);
 2350         }
 2351         sctp_stop_all_cookie_timers(stcb);
 2352         SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
 2353         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
 2354 
 2355         /* set up to notify upper layer */
 2356         *notification = SCTP_NOTIFY_ASSOC_UP;
 2357         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 2358             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
 2359             (!SCTP_IS_LISTENING(inp))) {
 2360                 /*
 2361                  * This is an endpoint that called connect() how it got a
 2362                  * cookie that is NEW is a bit of a mystery. It must be that
 2363                  * the INIT was sent, but before it got there.. a complete
 2364                  * INIT/INIT-ACK/COOKIE arrived. But of course then it
 2365                  * should have went to the other code.. not here.. oh well..
 2366                  * a bit of protection is worth having..
 2367                  */
 2368                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
 2369 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2370                 atomic_add_int(&stcb->asoc.refcnt, 1);
 2371                 SCTP_TCB_UNLOCK(stcb);
 2372                 SCTP_SOCKET_LOCK(so, 1);
 2373                 SCTP_TCB_LOCK(stcb);
 2374                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 2375                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
 2376                         SCTP_SOCKET_UNLOCK(so, 1);
 2377                         return (NULL);
 2378                 }
 2379 #endif
 2380                 soisconnected(stcb->sctp_socket);
 2381 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2382                 SCTP_SOCKET_UNLOCK(so, 1);
 2383 #endif
 2384         } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
 2385             (SCTP_IS_LISTENING(inp))) {
 2386                 /*
 2387                  * We don't want to do anything with this one. Since it is
 2388                  * the listening guy. The timer will get started for
 2389                  * accepted connections in the caller.
 2390                  */
 2391                 ;
 2392         }
 2393         /* since we did not send a HB make sure we don't double things */
 2394         if ((netp) && (*netp))
 2395                 (*netp)->hb_responded = 1;
 2396 
 2397         if (stcb->asoc.sctp_autoclose_ticks &&
 2398             sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
 2399                 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
 2400         }
 2401         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
 2402         if ((netp != NULL) && (*netp != NULL)) {
 2403                 struct timeval old;
 2404 
 2405                 /* calculate the RTT and set the encaps port */
 2406                 old.tv_sec = cookie->time_entered.tv_sec;
 2407                 old.tv_usec = cookie->time_entered.tv_usec;
 2408                 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
 2409                     &old, SCTP_RTT_FROM_NON_DATA);
 2410         }
 2411         /* respond with a COOKIE-ACK */
 2412         sctp_send_cookie_ack(stcb);
 2413 
 2414         /*
 2415          * check the address lists for any ASCONFs that need to be sent
 2416          * AFTER the cookie-ack is sent
 2417          */
 2418         sctp_check_address_list(stcb, m,
 2419             initack_offset + sizeof(struct sctp_init_ack_chunk),
 2420             initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
 2421             &store.sa, cookie->local_scope, cookie->site_scope,
 2422             cookie->ipv4_scope, cookie->loopback_scope);
 2423 
 2424 
 2425         return (stcb);
 2426 }
 2427 
 2428 /*
 2429  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
 2430  * we NEED to make sure we are not already using the vtag. If so we
 2431  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
 2432         head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
 2433                                                             SCTP_BASE_INFO(hashasocmark))];
 2434         LIST_FOREACH(stcb, head, sctp_asocs) {
 2435                 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep))  {
 2436                        -- SEND ABORT - TRY AGAIN --
 2437                 }
 2438         }
 2439 */
 2440 
 2441 /*
 2442  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
 2443  * existing (non-NULL) TCB
 2444  */
 2445 static struct mbuf *
 2446 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
 2447     struct sockaddr *src, struct sockaddr *dst,
 2448     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
 2449     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
 2450     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
 2451     struct sctp_tcb **locked_tcb,
 2452     uint8_t mflowtype, uint32_t mflowid,
 2453     uint32_t vrf_id, uint16_t port)
 2454 {
 2455         struct sctp_state_cookie *cookie;
 2456         struct sctp_tcb *l_stcb = *stcb;
 2457         struct sctp_inpcb *l_inp;
 2458         struct sockaddr *to;
 2459         struct sctp_pcb *ep;
 2460         struct mbuf *m_sig;
 2461         uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
 2462         uint8_t *sig;
 2463         uint8_t cookie_ok = 0;
 2464         unsigned int sig_offset, cookie_offset;
 2465         unsigned int cookie_len;
 2466         struct timeval now;
 2467         struct timeval time_expires;
 2468         int notification = 0;
 2469         struct sctp_nets *netl;
 2470         int had_a_existing_tcb = 0;
 2471         int send_int_conf = 0;
 2472 #ifdef INET
 2473         struct sockaddr_in sin;
 2474 #endif
 2475 #ifdef INET6
 2476         struct sockaddr_in6 sin6;
 2477 #endif
 2478 
 2479         SCTPDBG(SCTP_DEBUG_INPUT2,
 2480             "sctp_handle_cookie: handling COOKIE-ECHO\n");
 2481 
 2482         if (inp_p == NULL) {
 2483                 return (NULL);
 2484         }
 2485         cookie = &cp->cookie;
 2486         cookie_offset = offset + sizeof(struct sctp_chunkhdr);
 2487         cookie_len = ntohs(cp->ch.chunk_length);
 2488 
 2489         if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
 2490             sizeof(struct sctp_init_chunk) +
 2491             sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
 2492                 /* cookie too small */
 2493                 return (NULL);
 2494         }
 2495         if ((cookie->peerport != sh->src_port) ||
 2496             (cookie->myport != sh->dest_port) ||
 2497             (cookie->my_vtag != sh->v_tag)) {
 2498                 /*
 2499                  * invalid ports or bad tag.  Note that we always leave the
 2500                  * v_tag in the header in network order and when we stored
 2501                  * it in the my_vtag slot we also left it in network order.
 2502                  * This maintains the match even though it may be in the
 2503                  * opposite byte order of the machine :->
 2504                  */
 2505                 return (NULL);
 2506         }
 2507         /*
 2508          * split off the signature into its own mbuf (since it should not be
 2509          * calculated in the sctp_hmac_m() call).
 2510          */
 2511         sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
 2512         m_sig = m_split(m, sig_offset, M_NOWAIT);
 2513         if (m_sig == NULL) {
 2514                 /* out of memory or ?? */
 2515                 return (NULL);
 2516         }
 2517 #ifdef SCTP_MBUF_LOGGING
 2518         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 2519                 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
 2520         }
 2521 #endif
 2522 
 2523         /*
 2524          * compute the signature/digest for the cookie
 2525          */
 2526         ep = &(*inp_p)->sctp_ep;
 2527         l_inp = *inp_p;
 2528         if (l_stcb) {
 2529                 SCTP_TCB_UNLOCK(l_stcb);
 2530         }
 2531         SCTP_INP_RLOCK(l_inp);
 2532         if (l_stcb) {
 2533                 SCTP_TCB_LOCK(l_stcb);
 2534         }
 2535         /* which cookie is it? */
 2536         if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
 2537             (ep->current_secret_number != ep->last_secret_number)) {
 2538                 /* it's the old cookie */
 2539                 (void)sctp_hmac_m(SCTP_HMAC,
 2540                     (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
 2541                     SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
 2542         } else {
 2543                 /* it's the current cookie */
 2544                 (void)sctp_hmac_m(SCTP_HMAC,
 2545                     (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
 2546                     SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
 2547         }
 2548         /* get the signature */
 2549         SCTP_INP_RUNLOCK(l_inp);
 2550         sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
 2551         if (sig == NULL) {
 2552                 /* couldn't find signature */
 2553                 sctp_m_freem(m_sig);
 2554                 return (NULL);
 2555         }
 2556         /* compare the received digest with the computed digest */
 2557         if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
 2558                 /* try the old cookie? */
 2559                 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
 2560                     (ep->current_secret_number != ep->last_secret_number)) {
 2561                         /* compute digest with old */
 2562                         (void)sctp_hmac_m(SCTP_HMAC,
 2563                             (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
 2564                             SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
 2565                         /* compare */
 2566                         if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
 2567                                 cookie_ok = 1;
 2568                 }
 2569         } else {
 2570                 cookie_ok = 1;
 2571         }
 2572 
 2573         /*
 2574          * Now before we continue we must reconstruct our mbuf so that
 2575          * normal processing of any other chunks will work.
 2576          */
 2577         {
 2578                 struct mbuf *m_at;
 2579 
 2580                 m_at = m;
 2581                 while (SCTP_BUF_NEXT(m_at) != NULL) {
 2582                         m_at = SCTP_BUF_NEXT(m_at);
 2583                 }
 2584                 SCTP_BUF_NEXT(m_at) = m_sig;
 2585         }
 2586 
 2587         if (cookie_ok == 0) {
 2588                 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
 2589                 SCTPDBG(SCTP_DEBUG_INPUT2,
 2590                     "offset = %u, cookie_offset = %u, sig_offset = %u\n",
 2591                     (uint32_t)offset, cookie_offset, sig_offset);
 2592                 return (NULL);
 2593         }
 2594 
 2595         /*
 2596          * check the cookie timestamps to be sure it's not stale
 2597          */
 2598         (void)SCTP_GETTIME_TIMEVAL(&now);
 2599         /* Expire time is in Ticks, so we convert to seconds */
 2600         time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
 2601         time_expires.tv_usec = cookie->time_entered.tv_usec;
 2602         if (timevalcmp(&now, &time_expires, >)) {
 2603                 /* cookie is stale! */
 2604                 struct mbuf *op_err;
 2605                 struct sctp_error_stale_cookie *cause;
 2606                 struct timeval diff;
 2607                 uint32_t staleness;
 2608 
 2609                 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
 2610                     0, M_NOWAIT, 1, MT_DATA);
 2611                 if (op_err == NULL) {
 2612                         /* FOOBAR */
 2613                         return (NULL);
 2614                 }
 2615                 /* Set the len */
 2616                 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
 2617                 cause = mtod(op_err, struct sctp_error_stale_cookie *);
 2618                 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
 2619                 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
 2620                     (sizeof(uint32_t))));
 2621                 diff = now;
 2622                 timevalsub(&diff, &time_expires);
 2623                 if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
 2624                         staleness = UINT32_MAX;
 2625                 } else {
 2626                         staleness = diff.tv_sec * 1000000;
 2627                 }
 2628                 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
 2629                         staleness += diff.tv_usec;
 2630                 } else {
 2631                         staleness = UINT32_MAX;
 2632                 }
 2633                 cause->stale_time = htonl(staleness);
 2634                 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
 2635                     mflowtype, mflowid, l_inp->fibnum,
 2636                     vrf_id, port);
 2637                 return (NULL);
 2638         }
 2639         /*
 2640          * Now we must see with the lookup address if we have an existing
 2641          * asoc. This will only happen if we were in the COOKIE-WAIT state
 2642          * and a INIT collided with us and somewhere the peer sent the
 2643          * cookie on another address besides the single address our assoc
 2644          * had for him. In this case we will have one of the tie-tags set at
 2645          * least AND the address field in the cookie can be used to look it
 2646          * up.
 2647          */
 2648         to = NULL;
 2649         switch (cookie->addr_type) {
 2650 #ifdef INET6
 2651         case SCTP_IPV6_ADDRESS:
 2652                 memset(&sin6, 0, sizeof(sin6));
 2653                 sin6.sin6_family = AF_INET6;
 2654                 sin6.sin6_len = sizeof(sin6);
 2655                 sin6.sin6_port = sh->src_port;
 2656                 sin6.sin6_scope_id = cookie->scope_id;
 2657                 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
 2658                     sizeof(sin6.sin6_addr.s6_addr));
 2659                 to = (struct sockaddr *)&sin6;
 2660                 break;
 2661 #endif
 2662 #ifdef INET
 2663         case SCTP_IPV4_ADDRESS:
 2664                 memset(&sin, 0, sizeof(sin));
 2665                 sin.sin_family = AF_INET;
 2666                 sin.sin_len = sizeof(sin);
 2667                 sin.sin_port = sh->src_port;
 2668                 sin.sin_addr.s_addr = cookie->address[0];
 2669                 to = (struct sockaddr *)&sin;
 2670                 break;
 2671 #endif
 2672         default:
 2673                 /* This should not happen */
 2674                 return (NULL);
 2675         }
 2676         if (*stcb == NULL) {
 2677                 /* Yep, lets check */
 2678                 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
 2679                 if (*stcb == NULL) {
 2680                         /*
 2681                          * We should have only got back the same inp. If we
 2682                          * got back a different ep we have a problem. The
 2683                          * original findep got back l_inp and now
 2684                          */
 2685                         if (l_inp != *inp_p) {
 2686                                 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
 2687                         }
 2688                 } else {
 2689                         if (*locked_tcb == NULL) {
 2690                                 /*
 2691                                  * In this case we found the assoc only
 2692                                  * after we locked the create lock. This
 2693                                  * means we are in a colliding case and we
 2694                                  * must make sure that we unlock the tcb if
 2695                                  * its one of the cases where we throw away
 2696                                  * the incoming packets.
 2697                                  */
 2698                                 *locked_tcb = *stcb;
 2699 
 2700                                 /*
 2701                                  * We must also increment the inp ref count
 2702                                  * since the ref_count flags was set when we
 2703                                  * did not find the TCB, now we found it
 2704                                  * which reduces the refcount.. we must
 2705                                  * raise it back out to balance it all :-)
 2706                                  */
 2707                                 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
 2708                                 if ((*stcb)->sctp_ep != l_inp) {
 2709                                         SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
 2710                                             (void *)(*stcb)->sctp_ep, (void *)l_inp);
 2711                                 }
 2712                         }
 2713                 }
 2714         }
 2715 
 2716         cookie_len -= SCTP_SIGNATURE_SIZE;
 2717         if (*stcb == NULL) {
 2718                 /* this is the "normal" case... get a new TCB */
 2719                 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
 2720                     cookie, cookie_len, *inp_p,
 2721                     netp, to, &notification,
 2722                     auth_skipped, auth_offset, auth_len,
 2723                     mflowtype, mflowid,
 2724                     vrf_id, port);
 2725         } else {
 2726                 /* this is abnormal... cookie-echo on existing TCB */
 2727                 had_a_existing_tcb = 1;
 2728                 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
 2729                     src, dst, sh,
 2730                     cookie, cookie_len, *inp_p, *stcb, netp, to,
 2731                     &notification, auth_skipped, auth_offset, auth_len,
 2732                     mflowtype, mflowid,
 2733                     vrf_id, port);
 2734         }
 2735 
 2736         if (*stcb == NULL) {
 2737                 /* still no TCB... must be bad cookie-echo */
 2738                 return (NULL);
 2739         }
 2740         if (*netp != NULL) {
 2741                 (*netp)->flowtype = mflowtype;
 2742                 (*netp)->flowid = mflowid;
 2743         }
 2744         /*
 2745          * Ok, we built an association so confirm the address we sent the
 2746          * INIT-ACK to.
 2747          */
 2748         netl = sctp_findnet(*stcb, to);
 2749         /*
 2750          * This code should in theory NOT run but
 2751          */
 2752         if (netl == NULL) {
 2753                 /* TSNH! Huh, why do I need to add this address here? */
 2754                 if (sctp_add_remote_addr(*stcb, to, NULL, port,
 2755                     SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
 2756                         return (NULL);
 2757                 }
 2758                 netl = sctp_findnet(*stcb, to);
 2759         }
 2760         if (netl) {
 2761                 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
 2762                         netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
 2763                         (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
 2764                             netl);
 2765                         send_int_conf = 1;
 2766                 }
 2767         }
 2768         sctp_start_net_timers(*stcb);
 2769         if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
 2770                 if (!had_a_existing_tcb ||
 2771                     (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
 2772                         /*
 2773                          * If we have a NEW cookie or the connect never
 2774                          * reached the connected state during collision we
 2775                          * must do the TCP accept thing.
 2776                          */
 2777                         struct socket *so, *oso;
 2778                         struct sctp_inpcb *inp;
 2779 
 2780                         if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
 2781                                 /*
 2782                                  * For a restart we will keep the same
 2783                                  * socket, no need to do anything. I THINK!!
 2784                                  */
 2785                                 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
 2786                                 if (send_int_conf) {
 2787                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
 2788                                             (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
 2789                                 }
 2790                                 return (m);
 2791                         }
 2792                         oso = (*inp_p)->sctp_socket;
 2793                         atomic_add_int(&(*stcb)->asoc.refcnt, 1);
 2794                         SCTP_TCB_UNLOCK((*stcb));
 2795                         CURVNET_SET(oso->so_vnet);
 2796                         so = sonewconn(oso, 0
 2797                             );
 2798                         CURVNET_RESTORE();
 2799                         SCTP_TCB_LOCK((*stcb));
 2800                         atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
 2801 
 2802                         if (so == NULL) {
 2803                                 struct mbuf *op_err;
 2804 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2805                                 struct socket *pcb_so;
 2806 #endif
 2807                                 /* Too many sockets */
 2808                                 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
 2809                                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
 2810                                 sctp_abort_association(*inp_p, NULL, m, iphlen,
 2811                                     src, dst, sh, op_err,
 2812                                     mflowtype, mflowid,
 2813                                     vrf_id, port);
 2814 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2815                                 pcb_so = SCTP_INP_SO(*inp_p);
 2816                                 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
 2817                                 SCTP_TCB_UNLOCK((*stcb));
 2818                                 SCTP_SOCKET_LOCK(pcb_so, 1);
 2819                                 SCTP_TCB_LOCK((*stcb));
 2820                                 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
 2821 #endif
 2822                                 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
 2823                                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
 2824 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2825                                 SCTP_SOCKET_UNLOCK(pcb_so, 1);
 2826 #endif
 2827                                 return (NULL);
 2828                         }
 2829                         inp = (struct sctp_inpcb *)so->so_pcb;
 2830                         SCTP_INP_INCR_REF(inp);
 2831                         /*
 2832                          * We add the unbound flag here so that if we get an
 2833                          * soabort() before we get the move_pcb done, we
 2834                          * will properly cleanup.
 2835                          */
 2836                         inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
 2837                             SCTP_PCB_FLAGS_CONNECTED |
 2838                             SCTP_PCB_FLAGS_IN_TCPPOOL |
 2839                             SCTP_PCB_FLAGS_UNBOUND |
 2840                             (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
 2841                             SCTP_PCB_FLAGS_DONT_WAKE);
 2842                         inp->sctp_features = (*inp_p)->sctp_features;
 2843                         inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
 2844                         inp->sctp_socket = so;
 2845                         inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
 2846                         inp->max_cwnd = (*inp_p)->max_cwnd;
 2847                         inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
 2848                         inp->ecn_supported = (*inp_p)->ecn_supported;
 2849                         inp->prsctp_supported = (*inp_p)->prsctp_supported;
 2850                         inp->auth_supported = (*inp_p)->auth_supported;
 2851                         inp->asconf_supported = (*inp_p)->asconf_supported;
 2852                         inp->reconfig_supported = (*inp_p)->reconfig_supported;
 2853                         inp->nrsack_supported = (*inp_p)->nrsack_supported;
 2854                         inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
 2855                         inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
 2856                         inp->sctp_context = (*inp_p)->sctp_context;
 2857                         inp->local_strreset_support = (*inp_p)->local_strreset_support;
 2858                         inp->fibnum = (*inp_p)->fibnum;
 2859                         inp->inp_starting_point_for_iterator = NULL;
 2860                         /*
 2861                          * copy in the authentication parameters from the
 2862                          * original endpoint
 2863                          */
 2864                         if (inp->sctp_ep.local_hmacs)
 2865                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
 2866                         inp->sctp_ep.local_hmacs =
 2867                             sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
 2868                         if (inp->sctp_ep.local_auth_chunks)
 2869                                 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
 2870                         inp->sctp_ep.local_auth_chunks =
 2871                             sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
 2872 
 2873                         /*
 2874                          * Now we must move it from one hash table to
 2875                          * another and get the tcb in the right place.
 2876                          */
 2877 
 2878                         /*
 2879                          * This is where the one-2-one socket is put into
 2880                          * the accept state waiting for the accept!
 2881                          */
 2882                         if (*stcb) {
 2883                                 SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
 2884                         }
 2885                         sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
 2886 
 2887                         atomic_add_int(&(*stcb)->asoc.refcnt, 1);
 2888                         SCTP_TCB_UNLOCK((*stcb));
 2889 
 2890                         sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
 2891                             0);
 2892                         SCTP_TCB_LOCK((*stcb));
 2893                         atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
 2894 
 2895 
 2896                         /*
 2897                          * now we must check to see if we were aborted while
 2898                          * the move was going on and the lock/unlock
 2899                          * happened.
 2900                          */
 2901                         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
 2902                                 /*
 2903                                  * yep it was, we leave the assoc attached
 2904                                  * to the socket since the sctp_inpcb_free()
 2905                                  * call will send an abort for us.
 2906                                  */
 2907                                 SCTP_INP_DECR_REF(inp);
 2908                                 return (NULL);
 2909                         }
 2910                         SCTP_INP_DECR_REF(inp);
 2911                         /* Switch over to the new guy */
 2912                         *inp_p = inp;
 2913                         sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
 2914                         if (send_int_conf) {
 2915                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
 2916                                     (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
 2917                         }
 2918 
 2919                         /*
 2920                          * Pull it from the incomplete queue and wake the
 2921                          * guy
 2922                          */
 2923 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2924                         atomic_add_int(&(*stcb)->asoc.refcnt, 1);
 2925                         SCTP_TCB_UNLOCK((*stcb));
 2926                         SCTP_SOCKET_LOCK(so, 1);
 2927 #endif
 2928                         soisconnected(so);
 2929 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2930                         SCTP_TCB_LOCK((*stcb));
 2931                         atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
 2932                         SCTP_SOCKET_UNLOCK(so, 1);
 2933 #endif
 2934                         return (m);
 2935                 }
 2936         }
 2937         if (notification) {
 2938                 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
 2939         }
 2940         if (send_int_conf) {
 2941                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
 2942                     (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
 2943         }
 2944         return (m);
 2945 }
 2946 
 2947 static void
 2948 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
 2949     struct sctp_tcb *stcb, struct sctp_nets *net)
 2950 {
 2951         /* cp must not be used, others call this without a c-ack :-) */
 2952         struct sctp_association *asoc;
 2953 
 2954         SCTPDBG(SCTP_DEBUG_INPUT2,
 2955             "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
 2956         if ((stcb == NULL) || (net == NULL)) {
 2957                 return;
 2958         }
 2959 
 2960         asoc = &stcb->asoc;
 2961         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
 2962                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
 2963                     asoc->overall_error_count,
 2964                     0,
 2965                     SCTP_FROM_SCTP_INPUT,
 2966                     __LINE__);
 2967         }
 2968         asoc->overall_error_count = 0;
 2969         sctp_stop_all_cookie_timers(stcb);
 2970         /* process according to association state */
 2971         if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
 2972                 /* state change only needed when I am in right state */
 2973                 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
 2974                 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
 2975                 sctp_start_net_timers(stcb);
 2976                 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
 2977                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
 2978                             stcb->sctp_ep, stcb, asoc->primary_destination);
 2979 
 2980                 }
 2981                 /* update RTO */
 2982                 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
 2983                 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
 2984                 if (asoc->overall_error_count == 0) {
 2985                         net->RTO = sctp_calculate_rto(stcb, asoc, net,
 2986                             &asoc->time_entered,
 2987                             SCTP_RTT_FROM_NON_DATA);
 2988                 }
 2989                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
 2990                 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
 2991                 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 2992                     (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
 2993 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2994                         struct socket *so;
 2995 
 2996 #endif
 2997                         stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
 2998 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 2999                         so = SCTP_INP_SO(stcb->sctp_ep);
 3000                         atomic_add_int(&stcb->asoc.refcnt, 1);
 3001                         SCTP_TCB_UNLOCK(stcb);
 3002                         SCTP_SOCKET_LOCK(so, 1);
 3003                         SCTP_TCB_LOCK(stcb);
 3004                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 3005 #endif
 3006                         if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
 3007                                 soisconnected(stcb->sctp_socket);
 3008                         }
 3009 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 3010                         SCTP_SOCKET_UNLOCK(so, 1);
 3011 #endif
 3012                 }
 3013                 /*
 3014                  * since we did not send a HB make sure we don't double
 3015                  * things
 3016                  */
 3017                 net->hb_responded = 1;
 3018 
 3019                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
 3020                         /*
 3021                          * We don't need to do the asconf thing, nor hb or
 3022                          * autoclose if the socket is closed.
 3023                          */
 3024                         goto closed_socket;
 3025                 }
 3026 
 3027                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
 3028                     stcb, net);
 3029 
 3030 
 3031                 if (stcb->asoc.sctp_autoclose_ticks &&
 3032                     sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
 3033                         sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
 3034                             stcb->sctp_ep, stcb, NULL);
 3035                 }
 3036                 /*
 3037                  * send ASCONF if parameters are pending and ASCONFs are
 3038                  * allowed (eg. addresses changed when init/cookie echo were
 3039                  * in flight)
 3040                  */
 3041                 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
 3042                     (stcb->asoc.asconf_supported == 1) &&
 3043                     (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
 3044 #ifdef SCTP_TIMER_BASED_ASCONF
 3045                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
 3046                             stcb->sctp_ep, stcb,
 3047                             stcb->asoc.primary_destination);
 3048 #else
 3049                         sctp_send_asconf(stcb, stcb->asoc.primary_destination,
 3050                             SCTP_ADDR_NOT_LOCKED);
 3051 #endif
 3052                 }
 3053         }
 3054 closed_socket:
 3055         /* Toss the cookie if I can */
 3056         sctp_toss_old_cookies(stcb, asoc);
 3057         if (!TAILQ_EMPTY(&asoc->sent_queue)) {
 3058                 /* Restart the timer if we have pending data */
 3059                 struct sctp_tmit_chunk *chk;
 3060 
 3061                 chk = TAILQ_FIRST(&asoc->sent_queue);
 3062                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
 3063         }
 3064 }
 3065 
 3066 static void
 3067 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
 3068     struct sctp_tcb *stcb)
 3069 {
 3070         struct sctp_nets *net;
 3071         struct sctp_tmit_chunk *lchk;
 3072         struct sctp_ecne_chunk bkup;
 3073         uint8_t override_bit;
 3074         uint32_t tsn, window_data_tsn;
 3075         int len;
 3076         unsigned int pkt_cnt;
 3077 
 3078         len = ntohs(cp->ch.chunk_length);
 3079         if ((len != sizeof(struct sctp_ecne_chunk)) &&
 3080             (len != sizeof(struct old_sctp_ecne_chunk))) {
 3081                 return;
 3082         }
 3083         if (len == sizeof(struct old_sctp_ecne_chunk)) {
 3084                 /* Its the old format */
 3085                 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
 3086                 bkup.num_pkts_since_cwr = htonl(1);
 3087                 cp = &bkup;
 3088         }
 3089         SCTP_STAT_INCR(sctps_recvecne);
 3090         tsn = ntohl(cp->tsn);
 3091         pkt_cnt = ntohl(cp->num_pkts_since_cwr);
 3092         lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
 3093         if (lchk == NULL) {
 3094                 window_data_tsn = stcb->asoc.sending_seq - 1;
 3095         } else {
 3096                 window_data_tsn = lchk->rec.data.tsn;
 3097         }
 3098 
 3099         /* Find where it was sent to if possible. */
 3100         net = NULL;
 3101         TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
 3102                 if (lchk->rec.data.tsn == tsn) {
 3103                         net = lchk->whoTo;
 3104                         net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
 3105                         break;
 3106                 }
 3107                 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
 3108                         break;
 3109                 }
 3110         }
 3111         if (net == NULL) {
 3112                 /*
 3113                  * What to do. A previous send of a CWR was possibly lost.
 3114                  * See how old it is, we may have it marked on the actual
 3115                  * net.
 3116                  */
 3117                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 3118                         if (tsn == net->last_cwr_tsn) {
 3119                                 /* Found him, send it off */
 3120                                 break;
 3121                         }
 3122                 }
 3123                 if (net == NULL) {
 3124                         /*
 3125                          * If we reach here, we need to send a special CWR
 3126                          * that says hey, we did this a long time ago and
 3127                          * you lost the response.
 3128                          */
 3129                         net = TAILQ_FIRST(&stcb->asoc.nets);
 3130                         if (net == NULL) {
 3131                                 /* TSNH */
 3132                                 return;
 3133                         }
 3134                         override_bit = SCTP_CWR_REDUCE_OVERRIDE;
 3135                 } else {
 3136                         override_bit = 0;
 3137                 }
 3138         } else {
 3139                 override_bit = 0;
 3140         }
 3141         if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
 3142             ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
 3143                 /*
 3144                  * JRS - Use the congestion control given in the pluggable
 3145                  * CC module
 3146                  */
 3147                 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
 3148                 /*
 3149                  * We reduce once every RTT. So we will only lower cwnd at
 3150                  * the next sending seq i.e. the window_data_tsn
 3151                  */
 3152                 net->cwr_window_tsn = window_data_tsn;
 3153                 net->ecn_ce_pkt_cnt += pkt_cnt;
 3154                 net->lost_cnt = pkt_cnt;
 3155                 net->last_cwr_tsn = tsn;
 3156         } else {
 3157                 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
 3158                 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
 3159                     ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
 3160                         /*
 3161                          * Another loss in the same window update how many
 3162                          * marks/packets lost we have had.
 3163                          */
 3164                         int cnt = 1;
 3165 
 3166                         if (pkt_cnt > net->lost_cnt) {
 3167                                 /* Should be the case */
 3168                                 cnt = (pkt_cnt - net->lost_cnt);
 3169                                 net->ecn_ce_pkt_cnt += cnt;
 3170                         }
 3171                         net->lost_cnt = pkt_cnt;
 3172                         net->last_cwr_tsn = tsn;
 3173                         /*
 3174                          * Most CC functions will ignore this call, since we
 3175                          * are in-window yet of the initial CE the peer saw.
 3176                          */
 3177                         stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
 3178                 }
 3179         }
 3180         /*
 3181          * We always send a CWR this way if our previous one was lost our
 3182          * peer will get an update, or if it is not time again to reduce we
 3183          * still get the cwr to the peer. Note we set the override when we
 3184          * could not find the TSN on the chunk or the destination network.
 3185          */
 3186         sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
 3187 }
 3188 
 3189 static void
 3190 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
 3191 {
 3192         /*
 3193          * Here we get a CWR from the peer. We must look in the outqueue and
 3194          * make sure that we have a covered ECNE in the control chunk part.
 3195          * If so remove it.
 3196          */
 3197         struct sctp_tmit_chunk *chk, *nchk;
 3198         struct sctp_ecne_chunk *ecne;
 3199         int override;
 3200         uint32_t cwr_tsn;
 3201 
 3202         cwr_tsn = ntohl(cp->tsn);
 3203         override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
 3204         TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
 3205                 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
 3206                         continue;
 3207                 }
 3208                 if ((override == 0) && (chk->whoTo != net)) {
 3209                         /* Must be from the right src unless override is set */
 3210                         continue;
 3211                 }
 3212                 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
 3213                 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
 3214                         /* this covers this ECNE, we can remove it */
 3215                         stcb->asoc.ecn_echo_cnt_onq--;
 3216                         TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
 3217                             sctp_next);
 3218                         stcb->asoc.ctrl_queue_cnt--;
 3219                         sctp_m_freem(chk->data);
 3220                         chk->data = NULL;
 3221                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
 3222                         if (override == 0) {
 3223                                 break;
 3224                         }
 3225                 }
 3226         }
 3227 }
 3228 
 3229 static void
 3230 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
 3231     struct sctp_tcb *stcb, struct sctp_nets *net)
 3232 {
 3233 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 3234         struct socket *so;
 3235 #endif
 3236 
 3237         SCTPDBG(SCTP_DEBUG_INPUT2,
 3238             "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
 3239         if (stcb == NULL)
 3240                 return;
 3241 
 3242         /* process according to association state */
 3243         if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
 3244                 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
 3245                 SCTPDBG(SCTP_DEBUG_INPUT2,
 3246                     "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
 3247                 SCTP_TCB_UNLOCK(stcb);
 3248                 return;
 3249         }
 3250         /* notify upper layer protocol */
 3251         if (stcb->sctp_socket) {
 3252                 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
 3253         }
 3254 #ifdef INVARIANTS
 3255         if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
 3256             !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
 3257             sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
 3258                 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
 3259         }
 3260 #endif
 3261         /* stop the timer */
 3262         sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
 3263             SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
 3264         SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
 3265         /* free the TCB */
 3266         SCTPDBG(SCTP_DEBUG_INPUT2,
 3267             "sctp_handle_shutdown_complete: calls free-asoc\n");
 3268 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 3269         so = SCTP_INP_SO(stcb->sctp_ep);
 3270         atomic_add_int(&stcb->asoc.refcnt, 1);
 3271         SCTP_TCB_UNLOCK(stcb);
 3272         SCTP_SOCKET_LOCK(so, 1);
 3273         SCTP_TCB_LOCK(stcb);
 3274         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 3275 #endif
 3276         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
 3277             SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
 3278 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 3279         SCTP_SOCKET_UNLOCK(so, 1);
 3280 #endif
 3281         return;
 3282 }
 3283 
 3284 static int
 3285 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
 3286     struct sctp_nets *net, uint8_t flg)
 3287 {
 3288         switch (desc->chunk_type) {
 3289         case SCTP_DATA:
 3290                 /* find the tsn to resend (possibly */
 3291                 {
 3292                         uint32_t tsn;
 3293                         struct sctp_tmit_chunk *tp1;
 3294 
 3295                         tsn = ntohl(desc->tsn_ifany);
 3296                         TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
 3297                                 if (tp1->rec.data.tsn == tsn) {
 3298                                         /* found it */
 3299                                         break;
 3300                                 }
 3301                                 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
 3302                                         /* not found */
 3303                                         tp1 = NULL;
 3304                                         break;
 3305                                 }
 3306                         }
 3307                         if (tp1 == NULL) {
 3308                                 /*
 3309                                  * Do it the other way , aka without paying
 3310                                  * attention to queue seq order.
 3311                                  */
 3312                                 SCTP_STAT_INCR(sctps_pdrpdnfnd);
 3313                                 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
 3314                                         if (tp1->rec.data.tsn == tsn) {
 3315                                                 /* found it */
 3316                                                 break;
 3317                                         }
 3318                                 }
 3319                         }
 3320                         if (tp1 == NULL) {
 3321                                 SCTP_STAT_INCR(sctps_pdrptsnnf);
 3322                         }
 3323                         if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
 3324                                 uint8_t *ddp;
 3325 
 3326                                 if (((flg & SCTP_BADCRC) == 0) &&
 3327                                     ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
 3328                                         return (0);
 3329                                 }
 3330                                 if ((stcb->asoc.peers_rwnd == 0) &&
 3331                                     ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
 3332                                         SCTP_STAT_INCR(sctps_pdrpdiwnp);
 3333                                         return (0);
 3334                                 }
 3335                                 if (stcb->asoc.peers_rwnd == 0 &&
 3336                                     (flg & SCTP_FROM_MIDDLE_BOX)) {
 3337                                         SCTP_STAT_INCR(sctps_pdrpdizrw);
 3338                                         return (0);
 3339                                 }
 3340                                 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
 3341                                     sizeof(struct sctp_data_chunk));
 3342                                 {
 3343                                         unsigned int iii;
 3344 
 3345                                         for (iii = 0; iii < sizeof(desc->data_bytes);
 3346                                             iii++) {
 3347                                                 if (ddp[iii] != desc->data_bytes[iii]) {
 3348                                                         SCTP_STAT_INCR(sctps_pdrpbadd);
 3349                                                         return (-1);
 3350                                                 }
 3351                                         }
 3352                                 }
 3353 
 3354                                 if (tp1->do_rtt) {
 3355                                         /*
 3356                                          * this guy had a RTO calculation
 3357                                          * pending on it, cancel it
 3358                                          */
 3359                                         if (tp1->whoTo->rto_needed == 0) {
 3360                                                 tp1->whoTo->rto_needed = 1;
 3361                                         }
 3362                                         tp1->do_rtt = 0;
 3363                                 }
 3364                                 SCTP_STAT_INCR(sctps_pdrpmark);
 3365                                 if (tp1->sent != SCTP_DATAGRAM_RESEND)
 3366                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
 3367                                 /*
 3368                                  * mark it as if we were doing a FR, since
 3369                                  * we will be getting gap ack reports behind
 3370                                  * the info from the router.
 3371                                  */
 3372                                 tp1->rec.data.doing_fast_retransmit = 1;
 3373                                 /*
 3374                                  * mark the tsn with what sequences can
 3375                                  * cause a new FR.
 3376                                  */
 3377                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
 3378                                         tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
 3379                                 } else {
 3380                                         tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
 3381                                 }
 3382 
 3383                                 /* restart the timer */
 3384                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
 3385                                     stcb, tp1->whoTo,
 3386                                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
 3387                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
 3388                                     stcb, tp1->whoTo);
 3389 
 3390                                 /* fix counts and things */
 3391                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
 3392                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
 3393                                             tp1->whoTo->flight_size,
 3394                                             tp1->book_size,
 3395                                             (uint32_t)(uintptr_t)stcb,
 3396                                             tp1->rec.data.tsn);
 3397                                 }
 3398                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
 3399                                         sctp_flight_size_decrease(tp1);
 3400                                         sctp_total_flight_decrease(stcb, tp1);
 3401                                 }
 3402                                 tp1->sent = SCTP_DATAGRAM_RESEND;
 3403                         } {
 3404                                 /* audit code */
 3405                                 unsigned int audit;
 3406 
 3407                                 audit = 0;
 3408                                 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
 3409                                         if (tp1->sent == SCTP_DATAGRAM_RESEND)
 3410                                                 audit++;
 3411                                 }
 3412                                 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
 3413                                     sctp_next) {
 3414                                         if (tp1->sent == SCTP_DATAGRAM_RESEND)
 3415                                                 audit++;
 3416                                 }
 3417                                 if (audit != stcb->asoc.sent_queue_retran_cnt) {
 3418                                         SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
 3419                                             audit, stcb->asoc.sent_queue_retran_cnt);
 3420 #ifndef SCTP_AUDITING_ENABLED
 3421                                         stcb->asoc.sent_queue_retran_cnt = audit;
 3422 #endif
 3423                                 }
 3424                         }
 3425                 }
 3426                 break;
 3427         case SCTP_ASCONF:
 3428                 {
 3429                         struct sctp_tmit_chunk *asconf;
 3430 
 3431                         TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
 3432                             sctp_next) {
 3433                                 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
 3434                                         break;
 3435                                 }
 3436                         }
 3437                         if (asconf) {
 3438                                 if (asconf->sent != SCTP_DATAGRAM_RESEND)
 3439                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
 3440                                 asconf->sent = SCTP_DATAGRAM_RESEND;
 3441                                 asconf->snd_count--;
 3442                         }
 3443                 }
 3444                 break;
 3445         case SCTP_INITIATION:
 3446                 /* resend the INIT */
 3447                 stcb->asoc.dropped_special_cnt++;
 3448                 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
 3449                         /*
 3450                          * If we can get it in, in a few attempts we do
 3451                          * this, otherwise we let the timer fire.
 3452                          */
 3453                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
 3454                             stcb, net,
 3455                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
 3456                         sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
 3457                 }
 3458                 break;
 3459         case SCTP_SELECTIVE_ACK:
 3460         case SCTP_NR_SELECTIVE_ACK:
 3461                 /* resend the sack */
 3462                 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
 3463                 break;
 3464         case SCTP_HEARTBEAT_REQUEST:
 3465                 /* resend a demand HB */
 3466                 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
 3467                         /*
 3468                          * Only retransmit if we KNOW we wont destroy the
 3469                          * tcb
 3470                          */
 3471                         sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
 3472                 }
 3473                 break;
 3474         case SCTP_SHUTDOWN:
 3475                 sctp_send_shutdown(stcb, net);
 3476                 break;
 3477         case SCTP_SHUTDOWN_ACK:
 3478                 sctp_send_shutdown_ack(stcb, net);
 3479                 break;
 3480         case SCTP_COOKIE_ECHO:
 3481                 {
 3482                         struct sctp_tmit_chunk *cookie;
 3483 
 3484                         cookie = NULL;
 3485                         TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
 3486                             sctp_next) {
 3487                                 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
 3488                                         break;
 3489                                 }
 3490                         }
 3491                         if (cookie) {
 3492                                 if (cookie->sent != SCTP_DATAGRAM_RESEND)
 3493                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
 3494                                 cookie->sent = SCTP_DATAGRAM_RESEND;
 3495                                 sctp_stop_all_cookie_timers(stcb);
 3496                         }
 3497                 }
 3498                 break;
 3499         case SCTP_COOKIE_ACK:
 3500                 sctp_send_cookie_ack(stcb);
 3501                 break;
 3502         case SCTP_ASCONF_ACK:
 3503                 /* resend last asconf ack */
 3504                 sctp_send_asconf_ack(stcb);
 3505                 break;
 3506         case SCTP_IFORWARD_CUM_TSN:
 3507         case SCTP_FORWARD_CUM_TSN:
 3508                 send_forward_tsn(stcb, &stcb->asoc);
 3509                 break;
 3510                 /* can't do anything with these */
 3511         case SCTP_PACKET_DROPPED:
 3512         case SCTP_INITIATION_ACK:       /* this should not happen */
 3513         case SCTP_HEARTBEAT_ACK:
 3514         case SCTP_ABORT_ASSOCIATION:
 3515         case SCTP_OPERATION_ERROR:
 3516         case SCTP_SHUTDOWN_COMPLETE:
 3517         case SCTP_ECN_ECHO:
 3518         case SCTP_ECN_CWR:
 3519         default:
 3520                 break;
 3521         }
 3522         return (0);
 3523 }
 3524 
 3525 void
 3526 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
 3527 {
 3528         uint32_t i;
 3529         uint16_t temp;
 3530 
 3531         /*
 3532          * We set things to 0xffffffff since this is the last delivered
 3533          * sequence and we will be sending in 0 after the reset.
 3534          */
 3535 
 3536         if (number_entries) {
 3537                 for (i = 0; i < number_entries; i++) {
 3538                         temp = ntohs(list[i]);
 3539                         if (temp >= stcb->asoc.streamincnt) {
 3540                                 continue;
 3541                         }
 3542                         stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
 3543                 }
 3544         } else {
 3545                 list = NULL;
 3546                 for (i = 0; i < stcb->asoc.streamincnt; i++) {
 3547                         stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
 3548                 }
 3549         }
 3550         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
 3551 }
 3552 
 3553 static void
 3554 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
 3555 {
 3556         uint32_t i;
 3557         uint16_t temp;
 3558 
 3559         if (number_entries > 0) {
 3560                 for (i = 0; i < number_entries; i++) {
 3561                         temp = ntohs(list[i]);
 3562                         if (temp >= stcb->asoc.streamoutcnt) {
 3563                                 /* no such stream */
 3564                                 continue;
 3565                         }
 3566                         stcb->asoc.strmout[temp].next_mid_ordered = 0;
 3567                         stcb->asoc.strmout[temp].next_mid_unordered = 0;
 3568                 }
 3569         } else {
 3570                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
 3571                         stcb->asoc.strmout[i].next_mid_ordered = 0;
 3572                         stcb->asoc.strmout[i].next_mid_unordered = 0;
 3573                 }
 3574         }
 3575         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
 3576 }
 3577 
 3578 static void
 3579 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
 3580 {
 3581         uint32_t i;
 3582         uint16_t temp;
 3583 
 3584         if (number_entries > 0) {
 3585                 for (i = 0; i < number_entries; i++) {
 3586                         temp = ntohs(list[i]);
 3587                         if (temp >= stcb->asoc.streamoutcnt) {
 3588                                 /* no such stream */
 3589                                 continue;
 3590                         }
 3591                         stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
 3592                 }
 3593         } else {
 3594                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
 3595                         stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
 3596                 }
 3597         }
 3598 }
 3599 
 3600 
 3601 struct sctp_stream_reset_request *
 3602 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
 3603 {
 3604         struct sctp_association *asoc;
 3605         struct sctp_chunkhdr *ch;
 3606         struct sctp_stream_reset_request *r;
 3607         struct sctp_tmit_chunk *chk;
 3608         int len, clen;
 3609 
 3610         asoc = &stcb->asoc;
 3611         if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
 3612                 asoc->stream_reset_outstanding = 0;
 3613                 return (NULL);
 3614         }
 3615         if (stcb->asoc.str_reset == NULL) {
 3616                 asoc->stream_reset_outstanding = 0;
 3617                 return (NULL);
 3618         }
 3619         chk = stcb->asoc.str_reset;
 3620         if (chk->data == NULL) {
 3621                 return (NULL);
 3622         }
 3623         if (bchk) {
 3624                 /* he wants a copy of the chk pointer */
 3625                 *bchk = chk;
 3626         }
 3627         clen = chk->send_size;
 3628         ch = mtod(chk->data, struct sctp_chunkhdr *);
 3629         r = (struct sctp_stream_reset_request *)(ch + 1);
 3630         if (ntohl(r->request_seq) == seq) {
 3631                 /* found it */
 3632                 return (r);
 3633         }
 3634         len = SCTP_SIZE32(ntohs(r->ph.param_length));
 3635         if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
 3636                 /* move to the next one, there can only be a max of two */
 3637                 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
 3638                 if (ntohl(r->request_seq) == seq) {
 3639                         return (r);
 3640                 }
 3641         }
 3642         /* that seq is not here */
 3643         return (NULL);
 3644 }
 3645 
 3646 static void
 3647 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
 3648 {
 3649         struct sctp_association *asoc;
 3650         struct sctp_tmit_chunk *chk;
 3651 
 3652         asoc = &stcb->asoc;
 3653         chk = asoc->str_reset;
 3654         if (chk == NULL) {
 3655                 return;
 3656         }
 3657         asoc->str_reset = NULL;
 3658         sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
 3659             chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
 3660         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
 3661         asoc->ctrl_queue_cnt--;
 3662         if (chk->data) {
 3663                 sctp_m_freem(chk->data);
 3664                 chk->data = NULL;
 3665         }
 3666         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
 3667 }
 3668 
 3669 
 3670 static int
 3671 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
 3672     uint32_t seq, uint32_t action,
 3673     struct sctp_stream_reset_response *respin)
 3674 {
 3675         uint16_t type;
 3676         int lparam_len;
 3677         struct sctp_association *asoc = &stcb->asoc;
 3678         struct sctp_tmit_chunk *chk;
 3679         struct sctp_stream_reset_request *req_param;
 3680         struct sctp_stream_reset_out_request *req_out_param;
 3681         struct sctp_stream_reset_in_request *req_in_param;
 3682         uint32_t number_entries;
 3683 
 3684         if (asoc->stream_reset_outstanding == 0) {
 3685                 /* duplicate */
 3686                 return (0);
 3687         }
 3688         if (seq == stcb->asoc.str_reset_seq_out) {
 3689                 req_param = sctp_find_stream_reset(stcb, seq, &chk);
 3690                 if (req_param != NULL) {
 3691                         stcb->asoc.str_reset_seq_out++;
 3692                         type = ntohs(req_param->ph.param_type);
 3693                         lparam_len = ntohs(req_param->ph.param_length);
 3694                         if (type == SCTP_STR_RESET_OUT_REQUEST) {
 3695                                 int no_clear = 0;
 3696 
 3697                                 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
 3698                                 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
 3699                                 asoc->stream_reset_out_is_outstanding = 0;
 3700                                 if (asoc->stream_reset_outstanding)
 3701                                         asoc->stream_reset_outstanding--;
 3702                                 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
 3703                                         /* do it */
 3704                                         sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
 3705                                 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
 3706                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
 3707                                 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
 3708                                         /*
 3709                                          * Set it up so we don't stop
 3710                                          * retransmitting
 3711                                          */
 3712                                         asoc->stream_reset_outstanding++;
 3713                                         stcb->asoc.str_reset_seq_out--;
 3714                                         asoc->stream_reset_out_is_outstanding = 1;
 3715                                         no_clear = 1;
 3716                                 } else {
 3717                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
 3718                                 }
 3719                                 if (no_clear == 0) {
 3720                                         sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
 3721                                 }
 3722                         } else if (type == SCTP_STR_RESET_IN_REQUEST) {
 3723                                 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
 3724                                 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
 3725                                 if (asoc->stream_reset_outstanding)
 3726                                         asoc->stream_reset_outstanding--;
 3727                                 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
 3728                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
 3729                                             number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
 3730                                 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
 3731                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
 3732                                             number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
 3733                                 }
 3734                         } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
 3735                                 /* Ok we now may have more streams */
 3736                                 int num_stream;
 3737 
 3738                                 num_stream = stcb->asoc.strm_pending_add_size;
 3739                                 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
 3740                                         /* TSNH */
 3741                                         num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
 3742                                 }
 3743                                 stcb->asoc.strm_pending_add_size = 0;
 3744                                 if (asoc->stream_reset_outstanding)
 3745                                         asoc->stream_reset_outstanding--;
 3746                                 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
 3747                                         /* Put the new streams into effect */
 3748                                         int i;
 3749 
 3750                                         for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
 3751                                                 asoc->strmout[i].state = SCTP_STREAM_OPEN;
 3752                                         }
 3753                                         asoc->streamoutcnt += num_stream;
 3754                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
 3755                                 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
 3756                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
 3757                                             SCTP_STREAM_CHANGE_DENIED);
 3758                                 } else {
 3759                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
 3760                                             SCTP_STREAM_CHANGE_FAILED);
 3761                                 }
 3762                         } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
 3763                                 if (asoc->stream_reset_outstanding)
 3764                                         asoc->stream_reset_outstanding--;
 3765                                 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
 3766                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
 3767                                             SCTP_STREAM_CHANGE_DENIED);
 3768                                 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
 3769                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
 3770                                             SCTP_STREAM_CHANGE_FAILED);
 3771                                 }
 3772                         } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
 3773                                 /**
 3774                                  * a) Adopt the new in tsn.
 3775                                  * b) reset the map
 3776                                  * c) Adopt the new out-tsn
 3777                                  */
 3778                                 struct sctp_stream_reset_response_tsn *resp;
 3779                                 struct sctp_forward_tsn_chunk fwdtsn;
 3780                                 int abort_flag = 0;
 3781 
 3782                                 if (respin == NULL) {
 3783                                         /* huh ? */
 3784                                         return (0);
 3785                                 }
 3786                                 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
 3787                                         return (0);
 3788                                 }
 3789                                 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
 3790                                         resp = (struct sctp_stream_reset_response_tsn *)respin;
 3791                                         asoc->stream_reset_outstanding--;
 3792                                         fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
 3793                                         fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
 3794                                         fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
 3795                                         sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
 3796                                         if (abort_flag) {
 3797                                                 return (1);
 3798                                         }
 3799                                         stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
 3800                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
 3801                                                 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
 3802                                         }
 3803 
 3804                                         stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
 3805                                         stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
 3806                                         memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
 3807 
 3808                                         stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
 3809                                         memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
 3810 
 3811                                         stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
 3812                                         stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
 3813 
 3814                                         sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
 3815                                         sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
 3816                                         sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
 3817                                 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
 3818                                         sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
 3819                                             SCTP_ASSOC_RESET_DENIED);
 3820                                 } else {
 3821                                         sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
 3822                                             SCTP_ASSOC_RESET_FAILED);
 3823                                 }
 3824                         }
 3825                         /* get rid of the request and get the request flags */
 3826                         if (asoc->stream_reset_outstanding == 0) {
 3827                                 sctp_clean_up_stream_reset(stcb);
 3828                         }
 3829                 }
 3830         }
 3831         if (asoc->stream_reset_outstanding == 0) {
 3832                 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
 3833         }
 3834         return (0);
 3835 }
 3836 
 3837 static void
 3838 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
 3839     struct sctp_tmit_chunk *chk,
 3840     struct sctp_stream_reset_in_request *req, int trunc)
 3841 {
 3842         uint32_t seq;
 3843         int len, i;
 3844         int number_entries;
 3845         uint16_t temp;
 3846 
 3847         /*
 3848          * peer wants me to send a str-reset to him for my outgoing seq's if
 3849          * seq_in is right.
 3850          */
 3851         struct sctp_association *asoc = &stcb->asoc;
 3852 
 3853         seq = ntohl(req->request_seq);
 3854         if (asoc->str_reset_seq_in == seq) {
 3855                 asoc->last_reset_action[1] = asoc->last_reset_action[0];
 3856                 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
 3857                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 3858                 } else if (trunc) {
 3859                         /* Can't do it, since they exceeded our buffer size  */
 3860                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 3861                 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
 3862                         len = ntohs(req->ph.param_length);
 3863                         number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
 3864                         if (number_entries) {
 3865                                 for (i = 0; i < number_entries; i++) {
 3866                                         temp = ntohs(req->list_of_streams[i]);
 3867                                         if (temp >= stcb->asoc.streamoutcnt) {
 3868                                                 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 3869                                                 goto bad_boy;
 3870                                         }
 3871                                         req->list_of_streams[i] = temp;
 3872                                 }
 3873                                 for (i = 0; i < number_entries; i++) {
 3874                                         if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
 3875                                                 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
 3876                                         }
 3877                                 }
 3878                         } else {
 3879                                 /* Its all */
 3880                                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
 3881                                         if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
 3882                                                 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
 3883                                 }
 3884                         }
 3885                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
 3886                 } else {
 3887                         /* Can't do it, since we have sent one out */
 3888                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
 3889                 }
 3890 bad_boy:
 3891                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 3892                 asoc->str_reset_seq_in++;
 3893         } else if (asoc->str_reset_seq_in - 1 == seq) {
 3894                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 3895         } else if (asoc->str_reset_seq_in - 2 == seq) {
 3896                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
 3897         } else {
 3898                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
 3899         }
 3900         sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
 3901 }
 3902 
 3903 static int
 3904 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
 3905     struct sctp_tmit_chunk *chk,
 3906     struct sctp_stream_reset_tsn_request *req)
 3907 {
 3908         /* reset all in and out and update the tsn */
 3909         /*
 3910          * A) reset my str-seq's on in and out. B) Select a receive next,
 3911          * and set cum-ack to it. Also process this selected number as a
 3912          * fwd-tsn as well. C) set in the response my next sending seq.
 3913          */
 3914         struct sctp_forward_tsn_chunk fwdtsn;
 3915         struct sctp_association *asoc = &stcb->asoc;
 3916         int abort_flag = 0;
 3917         uint32_t seq;
 3918 
 3919         seq = ntohl(req->request_seq);
 3920         if (asoc->str_reset_seq_in == seq) {
 3921                 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
 3922                 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
 3923                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 3924                 } else {
 3925                         fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
 3926                         fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
 3927                         fwdtsn.ch.chunk_flags = 0;
 3928                         fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
 3929                         sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
 3930                         if (abort_flag) {
 3931                                 return (1);
 3932                         }
 3933                         asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
 3934                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
 3935                                 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
 3936                         }
 3937                         asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
 3938                         asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
 3939                         memset(asoc->mapping_array, 0, asoc->mapping_array_size);
 3940                         asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
 3941                         memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
 3942                         atomic_add_int(&asoc->sending_seq, 1);
 3943                         /* save off historical data for retrans */
 3944                         asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
 3945                         asoc->last_sending_seq[0] = asoc->sending_seq;
 3946                         asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
 3947                         asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
 3948                         sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
 3949                         sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
 3950                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
 3951                         sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
 3952                 }
 3953                 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
 3954                     asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
 3955                 asoc->str_reset_seq_in++;
 3956         } else if (asoc->str_reset_seq_in - 1 == seq) {
 3957                 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
 3958                     asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
 3959         } else if (asoc->str_reset_seq_in - 2 == seq) {
 3960                 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
 3961                     asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
 3962         } else {
 3963                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
 3964         }
 3965         return (0);
 3966 }
 3967 
 3968 static void
 3969 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
 3970     struct sctp_tmit_chunk *chk,
 3971     struct sctp_stream_reset_out_request *req, int trunc)
 3972 {
 3973         uint32_t seq, tsn;
 3974         int number_entries, len;
 3975         struct sctp_association *asoc = &stcb->asoc;
 3976 
 3977         seq = ntohl(req->request_seq);
 3978 
 3979         /* now if its not a duplicate we process it */
 3980         if (asoc->str_reset_seq_in == seq) {
 3981                 len = ntohs(req->ph.param_length);
 3982                 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
 3983                 /*
 3984                  * the sender is resetting, handle the list issue.. we must
 3985                  * a) verify if we can do the reset, if so no problem b) If
 3986                  * we can't do the reset we must copy the request. c) queue
 3987                  * it, and setup the data in processor to trigger it off
 3988                  * when needed and dequeue all the queued data.
 3989                  */
 3990                 tsn = ntohl(req->send_reset_at_tsn);
 3991 
 3992                 /* move the reset action back one */
 3993                 asoc->last_reset_action[1] = asoc->last_reset_action[0];
 3994                 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
 3995                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 3996                 } else if (trunc) {
 3997                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 3998                 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
 3999                         /* we can do it now */
 4000                         sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
 4001                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
 4002                 } else {
 4003                         /*
 4004                          * we must queue it up and thus wait for the TSN's
 4005                          * to arrive that are at or before tsn
 4006                          */
 4007                         struct sctp_stream_reset_list *liste;
 4008                         int siz;
 4009 
 4010                         siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
 4011                         SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
 4012                             siz, SCTP_M_STRESET);
 4013                         if (liste == NULL) {
 4014                                 /* gak out of memory */
 4015                                 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 4016                                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 4017                                 return;
 4018                         }
 4019                         liste->seq = seq;
 4020                         liste->tsn = tsn;
 4021                         liste->number_entries = number_entries;
 4022                         memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
 4023                         TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
 4024                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
 4025                 }
 4026                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 4027                 asoc->str_reset_seq_in++;
 4028         } else if ((asoc->str_reset_seq_in - 1) == seq) {
 4029                 /*
 4030                  * one seq back, just echo back last action since my
 4031                  * response was lost.
 4032                  */
 4033                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 4034         } else if ((asoc->str_reset_seq_in - 2) == seq) {
 4035                 /*
 4036                  * two seq back, just echo back last action since my
 4037                  * response was lost.
 4038                  */
 4039                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
 4040         } else {
 4041                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
 4042         }
 4043 }
 4044 
 4045 static void
 4046 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
 4047     struct sctp_stream_reset_add_strm *str_add)
 4048 {
 4049         /*
 4050          * Peer is requesting to add more streams. If its within our
 4051          * max-streams we will allow it.
 4052          */
 4053         uint32_t num_stream, i;
 4054         uint32_t seq;
 4055         struct sctp_association *asoc = &stcb->asoc;
 4056         struct sctp_queued_to_read *ctl, *nctl;
 4057 
 4058         /* Get the number. */
 4059         seq = ntohl(str_add->request_seq);
 4060         num_stream = ntohs(str_add->number_of_streams);
 4061         /* Now what would be the new total? */
 4062         if (asoc->str_reset_seq_in == seq) {
 4063                 num_stream += stcb->asoc.streamincnt;
 4064                 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
 4065                 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
 4066                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 4067                 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
 4068                     (num_stream > 0xffff)) {
 4069                         /* We must reject it they ask for to many */
 4070         denied:
 4071                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 4072                 } else {
 4073                         /* Ok, we can do that :-) */
 4074                         struct sctp_stream_in *oldstrm;
 4075 
 4076                         /* save off the old */
 4077                         oldstrm = stcb->asoc.strmin;
 4078                         SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
 4079                             (num_stream * sizeof(struct sctp_stream_in)),
 4080                             SCTP_M_STRMI);
 4081                         if (stcb->asoc.strmin == NULL) {
 4082                                 stcb->asoc.strmin = oldstrm;
 4083                                 goto denied;
 4084                         }
 4085                         /* copy off the old data */
 4086                         for (i = 0; i < stcb->asoc.streamincnt; i++) {
 4087                                 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
 4088                                 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
 4089                                 stcb->asoc.strmin[i].sid = i;
 4090                                 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
 4091                                 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
 4092                                 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
 4093                                 /* now anything on those queues? */
 4094                                 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
 4095                                         TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
 4096                                         TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
 4097                                 }
 4098                                 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
 4099                                         TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
 4100                                         TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
 4101                                 }
 4102                         }
 4103                         /* Init the new streams */
 4104                         for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
 4105                                 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
 4106                                 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
 4107                                 stcb->asoc.strmin[i].sid = i;
 4108                                 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
 4109                                 stcb->asoc.strmin[i].pd_api_started = 0;
 4110                                 stcb->asoc.strmin[i].delivery_started = 0;
 4111                         }
 4112                         SCTP_FREE(oldstrm, SCTP_M_STRMI);
 4113                         /* update the size */
 4114                         stcb->asoc.streamincnt = num_stream;
 4115                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
 4116                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
 4117                 }
 4118                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 4119                 asoc->str_reset_seq_in++;
 4120         } else if ((asoc->str_reset_seq_in - 1) == seq) {
 4121                 /*
 4122                  * one seq back, just echo back last action since my
 4123                  * response was lost.
 4124                  */
 4125                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 4126         } else if ((asoc->str_reset_seq_in - 2) == seq) {
 4127                 /*
 4128                  * two seq back, just echo back last action since my
 4129                  * response was lost.
 4130                  */
 4131                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
 4132         } else {
 4133                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
 4134 
 4135         }
 4136 }
 4137 
 4138 static void
 4139 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
 4140     struct sctp_stream_reset_add_strm *str_add)
 4141 {
 4142         /*
 4143          * Peer is requesting to add more streams. If its within our
 4144          * max-streams we will allow it.
 4145          */
 4146         uint16_t num_stream;
 4147         uint32_t seq;
 4148         struct sctp_association *asoc = &stcb->asoc;
 4149 
 4150         /* Get the number. */
 4151         seq = ntohl(str_add->request_seq);
 4152         num_stream = ntohs(str_add->number_of_streams);
 4153         /* Now what would be the new total? */
 4154         if (asoc->str_reset_seq_in == seq) {
 4155                 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
 4156                 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
 4157                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 4158                 } else if (stcb->asoc.stream_reset_outstanding) {
 4159                         /* We must reject it we have something pending */
 4160                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
 4161                 } else {
 4162                         /* Ok, we can do that :-) */
 4163                         int mychk;
 4164 
 4165                         mychk = stcb->asoc.streamoutcnt;
 4166                         mychk += num_stream;
 4167                         if (mychk < 0x10000) {
 4168                                 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
 4169                                 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
 4170                                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 4171                                 }
 4172                         } else {
 4173                                 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
 4174                         }
 4175                 }
 4176                 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
 4177                 asoc->str_reset_seq_in++;
 4178         } else if ((asoc->str_reset_seq_in - 1) == seq) {
 4179                 /*
 4180                  * one seq back, just echo back last action since my
 4181                  * response was lost.
 4182                  */
 4183                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
 4184         } else if ((asoc->str_reset_seq_in - 2) == seq) {
 4185                 /*
 4186                  * two seq back, just echo back last action since my
 4187                  * response was lost.
 4188                  */
 4189                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
 4190         } else {
 4191                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
 4192         }
 4193 }
 4194 
 4195 #ifdef __GNUC__
 4196 __attribute__((noinline))
 4197 #endif
 4198 static int
 4199 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
 4200     struct sctp_chunkhdr *ch_req)
 4201 {
 4202         uint16_t remaining_length, param_len, ptype;
 4203         struct sctp_paramhdr pstore;
 4204         uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
 4205         uint32_t seq = 0;
 4206         int num_req = 0;
 4207         int trunc = 0;
 4208         struct sctp_tmit_chunk *chk;
 4209         struct sctp_chunkhdr *ch;
 4210         struct sctp_paramhdr *ph;
 4211         int ret_code = 0;
 4212         int num_param = 0;
 4213 
 4214         /* now it may be a reset or a reset-response */
 4215         remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
 4216 
 4217         /* setup for adding the response */
 4218         sctp_alloc_a_chunk(stcb, chk);
 4219         if (chk == NULL) {
 4220                 return (ret_code);
 4221         }
 4222         chk->copy_by_ref = 0;
 4223         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
 4224         chk->rec.chunk_id.can_take_data = 0;
 4225         chk->flags = 0;
 4226         chk->asoc = &stcb->asoc;
 4227         chk->no_fr_allowed = 0;
 4228         chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
 4229         chk->book_size_scale = 0;
 4230         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
 4231         if (chk->data == NULL) {
 4232 strres_nochunk:
 4233                 if (chk->data) {
 4234                         sctp_m_freem(chk->data);
 4235                         chk->data = NULL;
 4236                 }
 4237                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
 4238                 return (ret_code);
 4239         }
 4240         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
 4241 
 4242         /* setup chunk parameters */
 4243         chk->sent = SCTP_DATAGRAM_UNSENT;
 4244         chk->snd_count = 0;
 4245         chk->whoTo = NULL;
 4246 
 4247         ch = mtod(chk->data, struct sctp_chunkhdr *);
 4248         ch->chunk_type = SCTP_STREAM_RESET;
 4249         ch->chunk_flags = 0;
 4250         ch->chunk_length = htons(chk->send_size);
 4251         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
 4252         offset += sizeof(struct sctp_chunkhdr);
 4253         while (remaining_length >= sizeof(struct sctp_paramhdr)) {
 4254                 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
 4255                 if (ph == NULL) {
 4256                         /* TSNH */
 4257                         break;
 4258                 }
 4259                 param_len = ntohs(ph->param_length);
 4260                 if ((param_len > remaining_length) ||
 4261                     (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
 4262                         /* bad parameter length */
 4263                         break;
 4264                 }
 4265                 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
 4266                     (uint8_t *)&cstore);
 4267                 if (ph == NULL) {
 4268                         /* TSNH */
 4269                         break;
 4270                 }
 4271                 ptype = ntohs(ph->param_type);
 4272                 num_param++;
 4273                 if (param_len > sizeof(cstore)) {
 4274                         trunc = 1;
 4275                 } else {
 4276                         trunc = 0;
 4277                 }
 4278                 if (num_param > SCTP_MAX_RESET_PARAMS) {
 4279                         /* hit the max of parameters already sorry.. */
 4280                         break;
 4281                 }
 4282                 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
 4283                         struct sctp_stream_reset_out_request *req_out;
 4284 
 4285                         if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
 4286                                 break;
 4287                         }
 4288                         req_out = (struct sctp_stream_reset_out_request *)ph;
 4289                         num_req++;
 4290                         if (stcb->asoc.stream_reset_outstanding) {
 4291                                 seq = ntohl(req_out->response_seq);
 4292                                 if (seq == stcb->asoc.str_reset_seq_out) {
 4293                                         /* implicit ack */
 4294                                         (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
 4295                                 }
 4296                         }
 4297                         sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
 4298                 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
 4299                         struct sctp_stream_reset_add_strm *str_add;
 4300 
 4301                         if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
 4302                                 break;
 4303                         }
 4304                         str_add = (struct sctp_stream_reset_add_strm *)ph;
 4305                         num_req++;
 4306                         sctp_handle_str_reset_add_strm(stcb, chk, str_add);
 4307                 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
 4308                         struct sctp_stream_reset_add_strm *str_add;
 4309 
 4310                         if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
 4311                                 break;
 4312                         }
 4313                         str_add = (struct sctp_stream_reset_add_strm *)ph;
 4314                         num_req++;
 4315                         sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
 4316                 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
 4317                         struct sctp_stream_reset_in_request *req_in;
 4318 
 4319                         num_req++;
 4320                         req_in = (struct sctp_stream_reset_in_request *)ph;
 4321                         sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
 4322                 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
 4323                         struct sctp_stream_reset_tsn_request *req_tsn;
 4324 
 4325                         num_req++;
 4326                         req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
 4327                         if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
 4328                                 ret_code = 1;
 4329                                 goto strres_nochunk;
 4330                         }
 4331                         /* no more */
 4332                         break;
 4333                 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
 4334                         struct sctp_stream_reset_response *resp;
 4335                         uint32_t result;
 4336 
 4337                         if (param_len < sizeof(struct sctp_stream_reset_response)) {
 4338                                 break;
 4339                         }
 4340                         resp = (struct sctp_stream_reset_response *)ph;
 4341                         seq = ntohl(resp->response_seq);
 4342                         result = ntohl(resp->result);
 4343                         if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
 4344                                 ret_code = 1;
 4345                                 goto strres_nochunk;
 4346                         }
 4347                 } else {
 4348                         break;
 4349                 }
 4350                 offset += SCTP_SIZE32(param_len);
 4351                 if (remaining_length >= SCTP_SIZE32(param_len)) {
 4352                         remaining_length -= SCTP_SIZE32(param_len);
 4353                 } else {
 4354                         remaining_length = 0;
 4355                 }
 4356         }
 4357         if (num_req == 0) {
 4358                 /* we have no response free the stuff */
 4359                 goto strres_nochunk;
 4360         }
 4361         /* ok we have a chunk to link in */
 4362         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
 4363             chk,
 4364             sctp_next);
 4365         stcb->asoc.ctrl_queue_cnt++;
 4366         return (ret_code);
 4367 }
 4368 
 4369 /*
 4370  * Handle a router or endpoints report of a packet loss, there are two ways
 4371  * to handle this, either we get the whole packet and must disect it
 4372  * ourselves (possibly with truncation and or corruption) or it is a summary
 4373  * from a middle box that did the disectting for us.
 4374  */
 4375 static void
 4376 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
 4377     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
 4378 {
 4379         uint32_t bottle_bw, on_queue;
 4380         uint16_t trunc_len;
 4381         unsigned int chlen;
 4382         unsigned int at;
 4383         struct sctp_chunk_desc desc;
 4384         struct sctp_chunkhdr *ch;
 4385 
 4386         chlen = ntohs(cp->ch.chunk_length);
 4387         chlen -= sizeof(struct sctp_pktdrop_chunk);
 4388         /* XXX possible chlen underflow */
 4389         if (chlen == 0) {
 4390                 ch = NULL;
 4391                 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
 4392                         SCTP_STAT_INCR(sctps_pdrpbwrpt);
 4393         } else {
 4394                 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
 4395                 chlen -= sizeof(struct sctphdr);
 4396                 /* XXX possible chlen underflow */
 4397                 memset(&desc, 0, sizeof(desc));
 4398         }
 4399         trunc_len = (uint16_t)ntohs(cp->trunc_len);
 4400         if (trunc_len > limit) {
 4401                 trunc_len = limit;
 4402         }
 4403 
 4404         /* now the chunks themselves */
 4405         while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
 4406                 desc.chunk_type = ch->chunk_type;
 4407                 /* get amount we need to move */
 4408                 at = ntohs(ch->chunk_length);
 4409                 if (at < sizeof(struct sctp_chunkhdr)) {
 4410                         /* corrupt chunk, maybe at the end? */
 4411                         SCTP_STAT_INCR(sctps_pdrpcrupt);
 4412                         break;
 4413                 }
 4414                 if (trunc_len == 0) {
 4415                         /* we are supposed to have all of it */
 4416                         if (at > chlen) {
 4417                                 /* corrupt skip it */
 4418                                 SCTP_STAT_INCR(sctps_pdrpcrupt);
 4419                                 break;
 4420                         }
 4421                 } else {
 4422                         /* is there enough of it left ? */
 4423                         if (desc.chunk_type == SCTP_DATA) {
 4424                                 if (chlen < (sizeof(struct sctp_data_chunk) +
 4425                                     sizeof(desc.data_bytes))) {
 4426                                         break;
 4427                                 }
 4428                         } else {
 4429                                 if (chlen < sizeof(struct sctp_chunkhdr)) {
 4430                                         break;
 4431                                 }
 4432                         }
 4433                 }
 4434                 if (desc.chunk_type == SCTP_DATA) {
 4435                         /* can we get out the tsn? */
 4436                         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
 4437                                 SCTP_STAT_INCR(sctps_pdrpmbda);
 4438 
 4439                         if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
 4440                                 /* yep */
 4441                                 struct sctp_data_chunk *dcp;
 4442                                 uint8_t *ddp;
 4443                                 unsigned int iii;
 4444 
 4445                                 dcp = (struct sctp_data_chunk *)ch;
 4446                                 ddp = (uint8_t *)(dcp + 1);
 4447                                 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
 4448                                         desc.data_bytes[iii] = ddp[iii];
 4449                                 }
 4450                                 desc.tsn_ifany = dcp->dp.tsn;
 4451                         } else {
 4452                                 /* nope we are done. */
 4453                                 SCTP_STAT_INCR(sctps_pdrpnedat);
 4454                                 break;
 4455                         }
 4456                 } else {
 4457                         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
 4458                                 SCTP_STAT_INCR(sctps_pdrpmbct);
 4459                 }
 4460 
 4461                 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
 4462                         SCTP_STAT_INCR(sctps_pdrppdbrk);
 4463                         break;
 4464                 }
 4465                 if (SCTP_SIZE32(at) > chlen) {
 4466                         break;
 4467                 }
 4468                 chlen -= SCTP_SIZE32(at);
 4469                 if (chlen < sizeof(struct sctp_chunkhdr)) {
 4470                         /* done, none left */
 4471                         break;
 4472                 }
 4473                 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
 4474         }
 4475         /* Now update any rwnd --- possibly */
 4476         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
 4477                 /* From a peer, we get a rwnd report */
 4478                 uint32_t a_rwnd;
 4479 
 4480                 SCTP_STAT_INCR(sctps_pdrpfehos);
 4481 
 4482                 bottle_bw = ntohl(cp->bottle_bw);
 4483                 on_queue = ntohl(cp->current_onq);
 4484                 if (bottle_bw && on_queue) {
 4485                         /* a rwnd report is in here */
 4486                         if (bottle_bw > on_queue)
 4487                                 a_rwnd = bottle_bw - on_queue;
 4488                         else
 4489                                 a_rwnd = 0;
 4490 
 4491                         if (a_rwnd == 0)
 4492                                 stcb->asoc.peers_rwnd = 0;
 4493                         else {
 4494                                 if (a_rwnd > stcb->asoc.total_flight) {
 4495                                         stcb->asoc.peers_rwnd =
 4496                                             a_rwnd - stcb->asoc.total_flight;
 4497                                 } else {
 4498                                         stcb->asoc.peers_rwnd = 0;
 4499                                 }
 4500                                 if (stcb->asoc.peers_rwnd <
 4501                                     stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
 4502                                         /* SWS sender side engages */
 4503                                         stcb->asoc.peers_rwnd = 0;
 4504                                 }
 4505                         }
 4506                 }
 4507         } else {
 4508                 SCTP_STAT_INCR(sctps_pdrpfmbox);
 4509         }
 4510 
 4511         /* now middle boxes in sat networks get a cwnd bump */
 4512         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
 4513             (stcb->asoc.sat_t3_loss_recovery == 0) &&
 4514             (stcb->asoc.sat_network)) {
 4515                 /*
 4516                  * This is debatable but for sat networks it makes sense
 4517                  * Note if a T3 timer has went off, we will prohibit any
 4518                  * changes to cwnd until we exit the t3 loss recovery.
 4519                  */
 4520                 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
 4521                     net, cp, &bottle_bw, &on_queue);
 4522         }
 4523 }
 4524 
 4525 /*
 4526  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
 4527  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
 4528  * offset: offset into the mbuf chain to first chunkhdr - length: is the
 4529  * length of the complete packet outputs: - length: modified to remaining
 4530  * length after control processing - netp: modified to new sctp_nets after
 4531  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
 4532  * bad packet,...) otherwise return the tcb for this packet
 4533  */
 4534 #ifdef __GNUC__
 4535 __attribute__((noinline))
 4536 #endif
 4537 static struct sctp_tcb *
 4538 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
 4539     struct sockaddr *src, struct sockaddr *dst,
 4540     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
 4541     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
 4542     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
 4543     uint32_t vrf_id, uint16_t port)
 4544 {
 4545         struct sctp_association *asoc;
 4546         struct mbuf *op_err;
 4547         char msg[SCTP_DIAG_INFO_LEN];
 4548         uint32_t vtag_in;
 4549         int num_chunks = 0;     /* number of control chunks processed */
 4550         uint32_t chk_length, contiguous;
 4551         int ret;
 4552         int abort_no_unlock = 0;
 4553         int ecne_seen = 0;
 4554 
 4555         /*
 4556          * How big should this be, and should it be alloc'd? Lets try the
 4557          * d-mtu-ceiling for now (2k) and that should hopefully work ...
 4558          * until we get into jumbo grams and such..
 4559          */
 4560         uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
 4561         int got_auth = 0;
 4562         uint32_t auth_offset = 0, auth_len = 0;
 4563         int auth_skipped = 0;
 4564         int asconf_cnt = 0;
 4565 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 4566         struct socket *so;
 4567 #endif
 4568 
 4569         SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
 4570             iphlen, *offset, length, (void *)stcb);
 4571 
 4572         if (stcb) {
 4573                 SCTP_TCB_LOCK_ASSERT(stcb);
 4574         }
 4575         /* validate chunk header length... */
 4576         if (ntohs(ch->chunk_length) < sizeof(*ch)) {
 4577                 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
 4578                     ntohs(ch->chunk_length));
 4579                 *offset = length;
 4580                 return (stcb);
 4581         }
 4582         /*
 4583          * validate the verification tag
 4584          */
 4585         vtag_in = ntohl(sh->v_tag);
 4586 
 4587         if (ch->chunk_type == SCTP_INITIATION) {
 4588                 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
 4589                     ntohs(ch->chunk_length), vtag_in);
 4590                 if (vtag_in != 0) {
 4591                         /* protocol error- silently discard... */
 4592                         SCTP_STAT_INCR(sctps_badvtag);
 4593                         if (stcb != NULL) {
 4594                                 SCTP_TCB_UNLOCK(stcb);
 4595                         }
 4596                         return (NULL);
 4597                 }
 4598         } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
 4599                 /*
 4600                  * If there is no stcb, skip the AUTH chunk and process
 4601                  * later after a stcb is found (to validate the lookup was
 4602                  * valid.
 4603                  */
 4604                 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
 4605                     (stcb == NULL) &&
 4606                     (inp->auth_supported == 1)) {
 4607                         /* save this chunk for later processing */
 4608                         auth_skipped = 1;
 4609                         auth_offset = *offset;
 4610                         auth_len = ntohs(ch->chunk_length);
 4611 
 4612                         /* (temporarily) move past this chunk */
 4613                         *offset += SCTP_SIZE32(auth_len);
 4614                         if (*offset >= length) {
 4615                                 /* no more data left in the mbuf chain */
 4616                                 *offset = length;
 4617                                 return (NULL);
 4618                         }
 4619                         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
 4620                             sizeof(struct sctp_chunkhdr), chunk_buf);
 4621                 }
 4622                 if (ch == NULL) {
 4623                         /* Help */
 4624                         *offset = length;
 4625                         return (stcb);
 4626                 }
 4627                 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
 4628                         goto process_control_chunks;
 4629                 }
 4630                 /*
 4631                  * first check if it's an ASCONF with an unknown src addr we
 4632                  * need to look inside to find the association
 4633                  */
 4634                 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
 4635                         struct sctp_chunkhdr *asconf_ch = ch;
 4636                         uint32_t asconf_offset = 0, asconf_len = 0;
 4637 
 4638                         /* inp's refcount may be reduced */
 4639                         SCTP_INP_INCR_REF(inp);
 4640 
 4641                         asconf_offset = *offset;
 4642                         do {
 4643                                 asconf_len = ntohs(asconf_ch->chunk_length);
 4644                                 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
 4645                                         break;
 4646                                 stcb = sctp_findassociation_ep_asconf(m,
 4647                                     *offset,
 4648                                     dst,
 4649                                     sh, &inp, netp, vrf_id);
 4650                                 if (stcb != NULL)
 4651                                         break;
 4652                                 asconf_offset += SCTP_SIZE32(asconf_len);
 4653                                 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
 4654                                     sizeof(struct sctp_chunkhdr), chunk_buf);
 4655                         } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
 4656                         if (stcb == NULL) {
 4657                                 /*
 4658                                  * reduce inp's refcount if not reduced in
 4659                                  * sctp_findassociation_ep_asconf().
 4660                                  */
 4661                                 SCTP_INP_DECR_REF(inp);
 4662                         }
 4663 
 4664                         /* now go back and verify any auth chunk to be sure */
 4665                         if (auth_skipped && (stcb != NULL)) {
 4666                                 struct sctp_auth_chunk *auth;
 4667 
 4668                                 auth = (struct sctp_auth_chunk *)
 4669                                     sctp_m_getptr(m, auth_offset,
 4670                                     auth_len, chunk_buf);
 4671                                 got_auth = 1;
 4672                                 auth_skipped = 0;
 4673                                 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
 4674                                     auth_offset)) {
 4675                                         /* auth HMAC failed so dump it */
 4676                                         *offset = length;
 4677                                         return (stcb);
 4678                                 } else {
 4679                                         /* remaining chunks are HMAC checked */
 4680                                         stcb->asoc.authenticated = 1;
 4681                                 }
 4682                         }
 4683                 }
 4684                 if (stcb == NULL) {
 4685                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
 4686                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
 4687                             msg);
 4688                         /* no association, so it's out of the blue... */
 4689                         sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
 4690                             mflowtype, mflowid, inp->fibnum,
 4691                             vrf_id, port);
 4692                         *offset = length;
 4693                         return (NULL);
 4694                 }
 4695                 asoc = &stcb->asoc;
 4696                 /* ABORT and SHUTDOWN can use either v_tag... */
 4697                 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
 4698                     (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
 4699                     (ch->chunk_type == SCTP_PACKET_DROPPED)) {
 4700                         /* Take the T-bit always into account. */
 4701                         if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
 4702                             (vtag_in == asoc->my_vtag)) ||
 4703                             (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
 4704                             (asoc->peer_vtag != htonl(0)) &&
 4705                             (vtag_in == asoc->peer_vtag))) {
 4706                                 /* this is valid */
 4707                         } else {
 4708                                 /* drop this packet... */
 4709                                 SCTP_STAT_INCR(sctps_badvtag);
 4710                                 if (stcb != NULL) {
 4711                                         SCTP_TCB_UNLOCK(stcb);
 4712                                 }
 4713                                 return (NULL);
 4714                         }
 4715                 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
 4716                         if (vtag_in != asoc->my_vtag) {
 4717                                 /*
 4718                                  * this could be a stale SHUTDOWN-ACK or the
 4719                                  * peer never got the SHUTDOWN-COMPLETE and
 4720                                  * is still hung; we have started a new asoc
 4721                                  * but it won't complete until the shutdown
 4722                                  * is completed
 4723                                  */
 4724                                 if (stcb != NULL) {
 4725                                         SCTP_TCB_UNLOCK(stcb);
 4726                                 }
 4727                                 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
 4728                                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
 4729                                     msg);
 4730                                 sctp_handle_ootb(m, iphlen, *offset, src, dst,
 4731                                     sh, inp, op_err,
 4732                                     mflowtype, mflowid, fibnum,
 4733                                     vrf_id, port);
 4734                                 return (NULL);
 4735                         }
 4736                 } else {
 4737                         /* for all other chunks, vtag must match */
 4738                         if (vtag_in != asoc->my_vtag) {
 4739                                 /* invalid vtag... */
 4740                                 SCTPDBG(SCTP_DEBUG_INPUT3,
 4741                                     "invalid vtag: %xh, expect %xh\n",
 4742                                     vtag_in, asoc->my_vtag);
 4743                                 SCTP_STAT_INCR(sctps_badvtag);
 4744                                 if (stcb != NULL) {
 4745                                         SCTP_TCB_UNLOCK(stcb);
 4746                                 }
 4747                                 *offset = length;
 4748                                 return (NULL);
 4749                         }
 4750                 }
 4751         }                       /* end if !SCTP_COOKIE_ECHO */
 4752         /*
 4753          * process all control chunks...
 4754          */
 4755         if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
 4756             (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
 4757             (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
 4758             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
 4759                 /* implied cookie-ack.. we must have lost the ack */
 4760                 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
 4761                     *netp);
 4762         }
 4763 
 4764 process_control_chunks:
 4765         while (IS_SCTP_CONTROL(ch)) {
 4766                 /* validate chunk length */
 4767                 chk_length = ntohs(ch->chunk_length);
 4768                 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
 4769                     ch->chunk_type, chk_length);
 4770                 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
 4771                 if (chk_length < sizeof(*ch) ||
 4772                     (*offset + (int)chk_length) > length) {
 4773                         *offset = length;
 4774                         return (stcb);
 4775                 }
 4776                 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
 4777                 /*
 4778                  * INIT and INIT-ACK only gets the init ack "header" portion
 4779                  * only because we don't have to process the peer's COOKIE.
 4780                  * All others get a complete chunk.
 4781                  */
 4782                 switch (ch->chunk_type) {
 4783                 case SCTP_INITIATION:
 4784                         contiguous = sizeof(struct sctp_init_chunk);
 4785                         break;
 4786                 case SCTP_INITIATION_ACK:
 4787                         contiguous = sizeof(struct sctp_init_ack_chunk);
 4788                         break;
 4789                 default:
 4790                         contiguous = min(chk_length, sizeof(chunk_buf));
 4791                         break;
 4792                 }
 4793                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
 4794                     contiguous,
 4795                     chunk_buf);
 4796                 if (ch == NULL) {
 4797                         *offset = length;
 4798                         if (stcb != NULL) {
 4799                                 SCTP_TCB_UNLOCK(stcb);
 4800                         }
 4801                         return (NULL);
 4802                 }
 4803 
 4804                 num_chunks++;
 4805                 /* Save off the last place we got a control from */
 4806                 if (stcb != NULL) {
 4807                         if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
 4808                                 /*
 4809                                  * allow last_control to be NULL if
 4810                                  * ASCONF... ASCONF processing will find the
 4811                                  * right net later
 4812                                  */
 4813                                 if ((netp != NULL) && (*netp != NULL))
 4814                                         stcb->asoc.last_control_chunk_from = *netp;
 4815                         }
 4816                 }
 4817 #ifdef SCTP_AUDITING_ENABLED
 4818                 sctp_audit_log(0xB0, ch->chunk_type);
 4819 #endif
 4820 
 4821                 /* check to see if this chunk required auth, but isn't */
 4822                 if ((stcb != NULL) &&
 4823                     sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
 4824                     !stcb->asoc.authenticated) {
 4825                         /* "silently" ignore */
 4826                         SCTP_STAT_INCR(sctps_recvauthmissing);
 4827                         goto next_chunk;
 4828                 }
 4829                 switch (ch->chunk_type) {
 4830                 case SCTP_INITIATION:
 4831                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
 4832                         /* The INIT chunk must be the only chunk. */
 4833                         if ((num_chunks > 1) ||
 4834                             (length - *offset > (int)SCTP_SIZE32(chk_length))) {
 4835                                 /* RFC 4960 requires that no ABORT is sent */
 4836                                 *offset = length;
 4837                                 if (stcb != NULL) {
 4838                                         SCTP_TCB_UNLOCK(stcb);
 4839                                 }
 4840                                 return (NULL);
 4841                         }
 4842                         /* Honor our resource limit. */
 4843                         if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
 4844                                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
 4845                                 sctp_abort_association(inp, stcb, m, iphlen,
 4846                                     src, dst, sh, op_err,
 4847                                     mflowtype, mflowid,
 4848                                     vrf_id, port);
 4849                                 *offset = length;
 4850                                 return (NULL);
 4851                         }
 4852                         sctp_handle_init(m, iphlen, *offset, src, dst, sh,
 4853                             (struct sctp_init_chunk *)ch, inp,
 4854                             stcb, *netp, &abort_no_unlock,
 4855                             mflowtype, mflowid,
 4856                             vrf_id, port);
 4857                         *offset = length;
 4858                         if ((!abort_no_unlock) && (stcb != NULL)) {
 4859                                 SCTP_TCB_UNLOCK(stcb);
 4860                         }
 4861                         return (NULL);
 4862                         break;
 4863                 case SCTP_PAD_CHUNK:
 4864                         break;
 4865                 case SCTP_INITIATION_ACK:
 4866                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
 4867                         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
 4868                                 /* We are not interested anymore */
 4869                                 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
 4870                                         ;
 4871                                 } else {
 4872                                         *offset = length;
 4873                                         if (stcb != NULL) {
 4874 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 4875                                                 so = SCTP_INP_SO(inp);
 4876                                                 atomic_add_int(&stcb->asoc.refcnt, 1);
 4877                                                 SCTP_TCB_UNLOCK(stcb);
 4878                                                 SCTP_SOCKET_LOCK(so, 1);
 4879                                                 SCTP_TCB_LOCK(stcb);
 4880                                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 4881 #endif
 4882                                                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 4883                                                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
 4884 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 4885                                                 SCTP_SOCKET_UNLOCK(so, 1);
 4886 #endif
 4887                                         }
 4888                                         return (NULL);
 4889                                 }
 4890                         }
 4891                         /* The INIT-ACK chunk must be the only chunk. */
 4892                         if ((num_chunks > 1) ||
 4893                             (length - *offset > (int)SCTP_SIZE32(chk_length))) {
 4894                                 *offset = length;
 4895                                 return (stcb);
 4896                         }
 4897                         if ((netp != NULL) && (*netp != NULL)) {
 4898                                 ret = sctp_handle_init_ack(m, iphlen, *offset,
 4899                                     src, dst, sh,
 4900                                     (struct sctp_init_ack_chunk *)ch,
 4901                                     stcb, *netp,
 4902                                     &abort_no_unlock,
 4903                                     mflowtype, mflowid,
 4904                                     vrf_id);
 4905                         } else {
 4906                                 ret = -1;
 4907                         }
 4908                         *offset = length;
 4909                         if (abort_no_unlock) {
 4910                                 return (NULL);
 4911                         }
 4912                         /*
 4913                          * Special case, I must call the output routine to
 4914                          * get the cookie echoed
 4915                          */
 4916                         if ((stcb != NULL) && (ret == 0)) {
 4917                                 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
 4918                         }
 4919                         return (stcb);
 4920                         break;
 4921                 case SCTP_SELECTIVE_ACK:
 4922                 case SCTP_NR_SELECTIVE_ACK:
 4923                         {
 4924                                 int abort_now = 0;
 4925                                 uint32_t a_rwnd, cum_ack;
 4926                                 uint16_t num_seg, num_nr_seg, num_dup;
 4927                                 uint8_t flags;
 4928                                 int offset_seg, offset_dup;
 4929 
 4930                                 SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
 4931                                     ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
 4932                                 SCTP_STAT_INCR(sctps_recvsacks);
 4933                                 if (stcb == NULL) {
 4934                                         SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
 4935                                             (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
 4936                                         break;
 4937                                 }
 4938                                 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
 4939                                         if (chk_length < sizeof(struct sctp_sack_chunk)) {
 4940                                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
 4941                                                 break;
 4942                                         }
 4943                                 } else {
 4944                                         if (stcb->asoc.nrsack_supported == 0) {
 4945                                                 goto unknown_chunk;
 4946                                         }
 4947                                         if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
 4948                                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
 4949                                                 break;
 4950                                         }
 4951                                 }
 4952                                 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
 4953                                         /*-
 4954                                          * If we have sent a shutdown-ack, we will pay no
 4955                                          * attention to a sack sent in to us since
 4956                                          * we don't care anymore.
 4957                                          */
 4958                                         break;
 4959                                 }
 4960                                 flags = ch->chunk_flags;
 4961                                 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
 4962                                         struct sctp_sack_chunk *sack;
 4963 
 4964                                         sack = (struct sctp_sack_chunk *)ch;
 4965                                         cum_ack = ntohl(sack->sack.cum_tsn_ack);
 4966                                         num_seg = ntohs(sack->sack.num_gap_ack_blks);
 4967                                         num_nr_seg = 0;
 4968                                         num_dup = ntohs(sack->sack.num_dup_tsns);
 4969                                         a_rwnd = ntohl(sack->sack.a_rwnd);
 4970                                         if (sizeof(struct sctp_sack_chunk) +
 4971                                             num_seg * sizeof(struct sctp_gap_ack_block) +
 4972                                             num_dup * sizeof(uint32_t) != chk_length) {
 4973                                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
 4974                                                 break;
 4975                                         }
 4976                                         offset_seg = *offset + sizeof(struct sctp_sack_chunk);
 4977                                         offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
 4978                                 } else {
 4979                                         struct sctp_nr_sack_chunk *nr_sack;
 4980 
 4981                                         nr_sack = (struct sctp_nr_sack_chunk *)ch;
 4982                                         cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
 4983                                         num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
 4984                                         num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
 4985                                         num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
 4986                                         a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
 4987                                         if (sizeof(struct sctp_nr_sack_chunk) +
 4988                                             (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
 4989                                             num_dup * sizeof(uint32_t) != chk_length) {
 4990                                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
 4991                                                 break;
 4992                                         }
 4993                                         offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
 4994                                         offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
 4995                                 }
 4996                                 SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
 4997                                     (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
 4998                                     cum_ack, num_seg, a_rwnd);
 4999                                 stcb->asoc.seen_a_sack_this_pkt = 1;
 5000                                 if ((stcb->asoc.pr_sctp_cnt == 0) &&
 5001                                     (num_seg == 0) && (num_nr_seg == 0) &&
 5002                                     SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
 5003                                     (stcb->asoc.saw_sack_with_frags == 0) &&
 5004                                     (stcb->asoc.saw_sack_with_nr_frags == 0) &&
 5005                                     (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
 5006                                         /*
 5007                                          * We have a SIMPLE sack having no
 5008                                          * prior segments and data on sent
 5009                                          * queue to be acked. Use the faster
 5010                                          * path sack processing. We also
 5011                                          * allow window update sacks with no
 5012                                          * missing segments to go this way
 5013                                          * too.
 5014                                          */
 5015                                         sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
 5016                                             &abort_now, ecne_seen);
 5017                                 } else {
 5018                                         if ((netp != NULL) && (*netp != NULL)) {
 5019                                                 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
 5020                                                     num_seg, num_nr_seg, num_dup, &abort_now, flags,
 5021                                                     cum_ack, a_rwnd, ecne_seen);
 5022                                         }
 5023                                 }
 5024                                 if (abort_now) {
 5025                                         /* ABORT signal from sack processing */
 5026                                         *offset = length;
 5027                                         return (NULL);
 5028                                 }
 5029                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
 5030                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
 5031                                     (stcb->asoc.stream_queue_cnt == 0)) {
 5032                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
 5033                                 }
 5034                                 break;
 5035                         }
 5036                 case SCTP_HEARTBEAT_REQUEST:
 5037                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
 5038                         if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
 5039                                 SCTP_STAT_INCR(sctps_recvheartbeat);
 5040                                 sctp_send_heartbeat_ack(stcb, m, *offset,
 5041                                     chk_length, *netp);
 5042                         }
 5043                         break;
 5044                 case SCTP_HEARTBEAT_ACK:
 5045                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
 5046                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
 5047                                 /* Its not ours */
 5048                                 *offset = length;
 5049                                 return (stcb);
 5050                         }
 5051                         SCTP_STAT_INCR(sctps_recvheartbeatack);
 5052                         if ((netp != NULL) && (*netp != NULL)) {
 5053                                 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
 5054                                     stcb, *netp);
 5055                         }
 5056                         break;
 5057                 case SCTP_ABORT_ASSOCIATION:
 5058                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
 5059                             (void *)stcb);
 5060                         *offset = length;
 5061                         if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
 5062                                 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
 5063                                         return (NULL);
 5064                                 } else {
 5065                                         return (stcb);
 5066                                 }
 5067                         } else {
 5068                                 return (NULL);
 5069                         }
 5070                         break;
 5071                 case SCTP_SHUTDOWN:
 5072                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
 5073                             (void *)stcb);
 5074                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
 5075                                 *offset = length;
 5076                                 return (stcb);
 5077                         }
 5078                         if ((netp != NULL) && (*netp != NULL)) {
 5079                                 int abort_flag = 0;
 5080 
 5081                                 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
 5082                                     stcb, *netp, &abort_flag);
 5083                                 if (abort_flag) {
 5084                                         *offset = length;
 5085                                         return (NULL);
 5086                                 }
 5087                         }
 5088                         break;
 5089                 case SCTP_SHUTDOWN_ACK:
 5090                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
 5091                         if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
 5092                                 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
 5093                         }
 5094                         *offset = length;
 5095                         return (NULL);
 5096                         break;
 5097                 case SCTP_OPERATION_ERROR:
 5098                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
 5099                         if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
 5100                             sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
 5101                                 *offset = length;
 5102                                 return (NULL);
 5103                         }
 5104                         break;
 5105                 case SCTP_COOKIE_ECHO:
 5106                         SCTPDBG(SCTP_DEBUG_INPUT3,
 5107                             "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
 5108                         if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
 5109                                 ;
 5110                         } else {
 5111                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
 5112                                         /* We are not interested anymore */
 5113                         abend:
 5114                                         if (stcb != NULL) {
 5115                                                 SCTP_TCB_UNLOCK(stcb);
 5116                                         }
 5117                                         *offset = length;
 5118                                         return (NULL);
 5119                                 }
 5120                         }
 5121                         /*-
 5122                          * First are we accepting? We do this again here
 5123                          * since it is possible that a previous endpoint WAS
 5124                          * listening responded to a INIT-ACK and then
 5125                          * closed. We opened and bound.. and are now no
 5126                          * longer listening.
 5127                          *
 5128                          * XXXGL: notes on checking listen queue length.
 5129                          * 1) SCTP_IS_LISTENING() doesn't necessarily mean
 5130                          *    SOLISTENING(), because a listening "UDP type"
 5131                          *    socket isn't listening in terms of the socket
 5132                          *    layer.  It is a normal data flow socket, that
 5133                          *    can fork off new connections.  Thus, we should
 5134                          *    look into sol_qlen only in case we are !UDP.
 5135                          * 2) Checking sol_qlen in general requires locking
 5136                          *    the socket, and this code lacks that.
 5137                          */
 5138                         if ((stcb == NULL) &&
 5139                             (!SCTP_IS_LISTENING(inp) ||
 5140                             (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
 5141                             inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
 5142                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
 5143                                     (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
 5144                                         op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
 5145                                         sctp_abort_association(inp, stcb, m, iphlen,
 5146                                             src, dst, sh, op_err,
 5147                                             mflowtype, mflowid,
 5148                                             vrf_id, port);
 5149                                 }
 5150                                 *offset = length;
 5151                                 return (NULL);
 5152                         } else {
 5153                                 struct mbuf *ret_buf;
 5154                                 struct sctp_inpcb *linp;
 5155 
 5156                                 if (stcb) {
 5157                                         linp = NULL;
 5158                                 } else {
 5159                                         linp = inp;
 5160                                 }
 5161 
 5162                                 if (linp != NULL) {
 5163                                         SCTP_ASOC_CREATE_LOCK(linp);
 5164                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
 5165                                             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
 5166                                                 SCTP_ASOC_CREATE_UNLOCK(linp);
 5167                                                 goto abend;
 5168                                         }
 5169                                 }
 5170 
 5171                                 if (netp != NULL) {
 5172                                         struct sctp_tcb *locked_stcb;
 5173 
 5174                                         locked_stcb = stcb;
 5175                                         ret_buf =
 5176                                             sctp_handle_cookie_echo(m, iphlen,
 5177                                             *offset,
 5178                                             src, dst,
 5179                                             sh,
 5180                                             (struct sctp_cookie_echo_chunk *)ch,
 5181                                             &inp, &stcb, netp,
 5182                                             auth_skipped,
 5183                                             auth_offset,
 5184                                             auth_len,
 5185                                             &locked_stcb,
 5186                                             mflowtype,
 5187                                             mflowid,
 5188                                             vrf_id,
 5189                                             port);
 5190                                         if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
 5191                                                 SCTP_TCB_UNLOCK(locked_stcb);
 5192                                         }
 5193                                         if (stcb != NULL) {
 5194                                                 SCTP_TCB_LOCK_ASSERT(stcb);
 5195                                         }
 5196                                 } else {
 5197                                         ret_buf = NULL;
 5198                                 }
 5199                                 if (linp != NULL) {
 5200                                         SCTP_ASOC_CREATE_UNLOCK(linp);
 5201                                 }
 5202                                 if (ret_buf == NULL) {
 5203                                         if (stcb != NULL) {
 5204                                                 SCTP_TCB_UNLOCK(stcb);
 5205                                         }
 5206                                         SCTPDBG(SCTP_DEBUG_INPUT3,
 5207                                             "GAK, null buffer\n");
 5208                                         *offset = length;
 5209                                         return (NULL);
 5210                                 }
 5211                                 /* if AUTH skipped, see if it verified... */
 5212                                 if (auth_skipped) {
 5213                                         got_auth = 1;
 5214                                         auth_skipped = 0;
 5215                                 }
 5216                                 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
 5217                                         /*
 5218                                          * Restart the timer if we have
 5219                                          * pending data
 5220                                          */
 5221                                         struct sctp_tmit_chunk *chk;
 5222 
 5223                                         chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
 5224                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
 5225                                 }
 5226                         }
 5227                         break;
 5228                 case SCTP_COOKIE_ACK:
 5229                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
 5230                         if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
 5231                                 return (stcb);
 5232                         }
 5233                         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
 5234                                 /* We are not interested anymore */
 5235                                 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
 5236                                         ;
 5237                                 } else if (stcb) {
 5238 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5239                                         so = SCTP_INP_SO(inp);
 5240                                         atomic_add_int(&stcb->asoc.refcnt, 1);
 5241                                         SCTP_TCB_UNLOCK(stcb);
 5242                                         SCTP_SOCKET_LOCK(so, 1);
 5243                                         SCTP_TCB_LOCK(stcb);
 5244                                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 5245 #endif
 5246                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 5247                                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
 5248 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5249                                         SCTP_SOCKET_UNLOCK(so, 1);
 5250 #endif
 5251                                         *offset = length;
 5252                                         return (NULL);
 5253                                 }
 5254                         }
 5255                         if ((netp != NULL) && (*netp != NULL)) {
 5256                                 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
 5257                         }
 5258                         break;
 5259                 case SCTP_ECN_ECHO:
 5260                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
 5261                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
 5262                                 /* Its not ours */
 5263                                 *offset = length;
 5264                                 return (stcb);
 5265                         }
 5266                         if (stcb->asoc.ecn_supported == 0) {
 5267                                 goto unknown_chunk;
 5268                         }
 5269                         sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
 5270                         ecne_seen = 1;
 5271                         break;
 5272                 case SCTP_ECN_CWR:
 5273                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
 5274                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
 5275                                 *offset = length;
 5276                                 return (stcb);
 5277                         }
 5278                         if (stcb->asoc.ecn_supported == 0) {
 5279                                 goto unknown_chunk;
 5280                         }
 5281                         sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
 5282                         break;
 5283                 case SCTP_SHUTDOWN_COMPLETE:
 5284                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
 5285                         /* must be first and only chunk */
 5286                         if ((num_chunks > 1) ||
 5287                             (length - *offset > (int)SCTP_SIZE32(chk_length))) {
 5288                                 *offset = length;
 5289                                 return (stcb);
 5290                         }
 5291                         if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
 5292                                 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
 5293                                     stcb, *netp);
 5294                         }
 5295                         *offset = length;
 5296                         return (NULL);
 5297                         break;
 5298                 case SCTP_ASCONF:
 5299                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
 5300                         if (stcb != NULL) {
 5301                                 if (stcb->asoc.asconf_supported == 0) {
 5302                                         goto unknown_chunk;
 5303                                 }
 5304                                 sctp_handle_asconf(m, *offset, src,
 5305                                     (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
 5306                                 asconf_cnt++;
 5307                         }
 5308                         break;
 5309                 case SCTP_ASCONF_ACK:
 5310                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
 5311                         if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
 5312                                 /* Its not ours */
 5313                                 *offset = length;
 5314                                 return (stcb);
 5315                         }
 5316                         if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
 5317                                 if (stcb->asoc.asconf_supported == 0) {
 5318                                         goto unknown_chunk;
 5319                                 }
 5320                                 /* He's alive so give him credit */
 5321                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
 5322                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
 5323                                             stcb->asoc.overall_error_count,
 5324                                             0,
 5325                                             SCTP_FROM_SCTP_INPUT,
 5326                                             __LINE__);
 5327                                 }
 5328                                 stcb->asoc.overall_error_count = 0;
 5329                                 sctp_handle_asconf_ack(m, *offset,
 5330                                     (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
 5331                                 if (abort_no_unlock)
 5332                                         return (NULL);
 5333                         }
 5334                         break;
 5335                 case SCTP_FORWARD_CUM_TSN:
 5336                 case SCTP_IFORWARD_CUM_TSN:
 5337                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
 5338                         if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
 5339                                 /* Its not ours */
 5340                                 *offset = length;
 5341                                 return (stcb);
 5342                         }
 5343 
 5344                         if (stcb != NULL) {
 5345                                 int abort_flag = 0;
 5346 
 5347                                 if (stcb->asoc.prsctp_supported == 0) {
 5348                                         goto unknown_chunk;
 5349                                 }
 5350                                 *fwd_tsn_seen = 1;
 5351                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
 5352                                         /* We are not interested anymore */
 5353 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5354                                         so = SCTP_INP_SO(inp);
 5355                                         atomic_add_int(&stcb->asoc.refcnt, 1);
 5356                                         SCTP_TCB_UNLOCK(stcb);
 5357                                         SCTP_SOCKET_LOCK(so, 1);
 5358                                         SCTP_TCB_LOCK(stcb);
 5359                                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 5360 #endif
 5361                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
 5362                                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
 5363 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5364                                         SCTP_SOCKET_UNLOCK(so, 1);
 5365 #endif
 5366                                         *offset = length;
 5367                                         return (NULL);
 5368                                 }
 5369                                 /*
 5370                                  * For sending a SACK this looks like DATA
 5371                                  * chunks.
 5372                                  */
 5373                                 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
 5374                                 sctp_handle_forward_tsn(stcb,
 5375                                     (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
 5376                                 if (abort_flag) {
 5377                                         *offset = length;
 5378                                         return (NULL);
 5379                                 }
 5380                         }
 5381                         break;
 5382                 case SCTP_STREAM_RESET:
 5383                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
 5384                         if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
 5385                                 /* Its not ours */
 5386                                 *offset = length;
 5387                                 return (stcb);
 5388                         }
 5389                         if (stcb->asoc.reconfig_supported == 0) {
 5390                                 goto unknown_chunk;
 5391                         }
 5392                         if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
 5393                                 /* stop processing */
 5394                                 *offset = length;
 5395                                 return (NULL);
 5396                         }
 5397                         break;
 5398                 case SCTP_PACKET_DROPPED:
 5399                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
 5400                         /* re-get it all please */
 5401                         if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
 5402                                 /* Its not ours */
 5403                                 *offset = length;
 5404                                 return (stcb);
 5405                         }
 5406 
 5407                         if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
 5408                                 if (stcb->asoc.pktdrop_supported == 0) {
 5409                                         goto unknown_chunk;
 5410                                 }
 5411                                 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
 5412                                     stcb, *netp,
 5413                                     min(chk_length, contiguous));
 5414                         }
 5415                         break;
 5416                 case SCTP_AUTHENTICATION:
 5417                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
 5418                         if (stcb == NULL) {
 5419                                 /* save the first AUTH for later processing */
 5420                                 if (auth_skipped == 0) {
 5421                                         auth_offset = *offset;
 5422                                         auth_len = chk_length;
 5423                                         auth_skipped = 1;
 5424                                 }
 5425                                 /* skip this chunk (temporarily) */
 5426                                 goto next_chunk;
 5427                         }
 5428                         if (stcb->asoc.auth_supported == 0) {
 5429                                 goto unknown_chunk;
 5430                         }
 5431                         if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
 5432                             (chk_length > (sizeof(struct sctp_auth_chunk) +
 5433                             SCTP_AUTH_DIGEST_LEN_MAX))) {
 5434                                 /* Its not ours */
 5435                                 *offset = length;
 5436                                 return (stcb);
 5437                         }
 5438                         if (got_auth == 1) {
 5439                                 /* skip this chunk... it's already auth'd */
 5440                                 goto next_chunk;
 5441                         }
 5442                         got_auth = 1;
 5443                         if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
 5444                             m, *offset)) {
 5445                                 /* auth HMAC failed so dump the packet */
 5446                                 *offset = length;
 5447                                 return (stcb);
 5448                         } else {
 5449                                 /* remaining chunks are HMAC checked */
 5450                                 stcb->asoc.authenticated = 1;
 5451                         }
 5452                         break;
 5453 
 5454                 default:
 5455         unknown_chunk:
 5456                         /* it's an unknown chunk! */
 5457                         if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
 5458                                 struct sctp_gen_error_cause *cause;
 5459                                 int len;
 5460 
 5461                                 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
 5462                                     0, M_NOWAIT, 1, MT_DATA);
 5463                                 if (op_err != NULL) {
 5464                                         len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
 5465                                         cause = mtod(op_err, struct sctp_gen_error_cause *);
 5466                                         cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
 5467                                         cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
 5468                                         SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
 5469                                         SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
 5470                                         if (SCTP_BUF_NEXT(op_err) != NULL) {
 5471 #ifdef SCTP_MBUF_LOGGING
 5472                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 5473                                                         sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
 5474                                                 }
 5475 #endif
 5476                                                 sctp_queue_op_err(stcb, op_err);
 5477                                         } else {
 5478                                                 sctp_m_freem(op_err);
 5479                                         }
 5480                                 }
 5481                         }
 5482                         if ((ch->chunk_type & 0x80) == 0) {
 5483                                 /* discard this packet */
 5484                                 *offset = length;
 5485                                 return (stcb);
 5486                         }       /* else skip this bad chunk and continue... */
 5487                         break;
 5488                 }               /* switch (ch->chunk_type) */
 5489 
 5490 
 5491 next_chunk:
 5492                 /* get the next chunk */
 5493                 *offset += SCTP_SIZE32(chk_length);
 5494                 if (*offset >= length) {
 5495                         /* no more data left in the mbuf chain */
 5496                         break;
 5497                 }
 5498                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
 5499                     sizeof(struct sctp_chunkhdr), chunk_buf);
 5500                 if (ch == NULL) {
 5501                         *offset = length;
 5502                         return (stcb);
 5503                 }
 5504         }                       /* while */
 5505 
 5506         if ((asconf_cnt > 0) && (stcb != NULL)) {
 5507                 sctp_send_asconf_ack(stcb);
 5508         }
 5509         return (stcb);
 5510 }
 5511 
 5512 
 5513 /*
 5514  * common input chunk processing (v4 and v6)
 5515  */
 5516 void
 5517 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
 5518     struct sockaddr *src, struct sockaddr *dst,
 5519     struct sctphdr *sh, struct sctp_chunkhdr *ch,
 5520     uint8_t compute_crc,
 5521     uint8_t ecn_bits,
 5522     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
 5523     uint32_t vrf_id, uint16_t port)
 5524 {
 5525         uint32_t high_tsn;
 5526         int fwd_tsn_seen = 0, data_processed = 0;
 5527         struct mbuf *m = *mm, *op_err;
 5528         char msg[SCTP_DIAG_INFO_LEN];
 5529         int un_sent;
 5530         int cnt_ctrl_ready = 0;
 5531         struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
 5532         struct sctp_tcb *stcb = NULL;
 5533         struct sctp_nets *net = NULL;
 5534 
 5535         SCTP_STAT_INCR(sctps_recvdatagrams);
 5536 #ifdef SCTP_AUDITING_ENABLED
 5537         sctp_audit_log(0xE0, 1);
 5538         sctp_auditing(0, inp, stcb, net);
 5539 #endif
 5540         if (compute_crc != 0) {
 5541                 uint32_t check, calc_check;
 5542 
 5543                 check = sh->checksum;
 5544                 sh->checksum = 0;
 5545                 calc_check = sctp_calculate_cksum(m, iphlen);
 5546                 sh->checksum = check;
 5547                 if (calc_check != check) {
 5548                         SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
 5549                             calc_check, check, (void *)m, length, iphlen);
 5550                         stcb = sctp_findassociation_addr(m, offset, src, dst,
 5551                             sh, ch, &inp, &net, vrf_id);
 5552 #if defined(INET) || defined(INET6)
 5553                         if ((ch->chunk_type != SCTP_INITIATION) &&
 5554                             (net != NULL) && (net->port != port)) {
 5555                                 if (net->port == 0) {
 5556                                         /* UDP encapsulation turned on. */
 5557                                         net->mtu -= sizeof(struct udphdr);
 5558                                         if (stcb->asoc.smallest_mtu > net->mtu) {
 5559                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
 5560                                         }
 5561                                 } else if (port == 0) {
 5562                                         /* UDP encapsulation turned off. */
 5563                                         net->mtu += sizeof(struct udphdr);
 5564                                         /* XXX Update smallest_mtu */
 5565                                 }
 5566                                 net->port = port;
 5567                         }
 5568 #endif
 5569                         if (net != NULL) {
 5570                                 net->flowtype = mflowtype;
 5571                                 net->flowid = mflowid;
 5572                         }
 5573                         SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
 5574                         if ((inp != NULL) && (stcb != NULL)) {
 5575                                 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
 5576                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
 5577                         } else if ((inp != NULL) && (stcb == NULL)) {
 5578                                 inp_decr = inp;
 5579                         }
 5580                         SCTP_STAT_INCR(sctps_badsum);
 5581                         SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
 5582                         goto out;
 5583                 }
 5584         }
 5585         /* Destination port of 0 is illegal, based on RFC4960. */
 5586         if (sh->dest_port == 0) {
 5587                 SCTP_STAT_INCR(sctps_hdrops);
 5588                 goto out;
 5589         }
 5590         stcb = sctp_findassociation_addr(m, offset, src, dst,
 5591             sh, ch, &inp, &net, vrf_id);
 5592 #if defined(INET) || defined(INET6)
 5593         if ((ch->chunk_type != SCTP_INITIATION) &&
 5594             (net != NULL) && (net->port != port)) {
 5595                 if (net->port == 0) {
 5596                         /* UDP encapsulation turned on. */
 5597                         net->mtu -= sizeof(struct udphdr);
 5598                         if (stcb->asoc.smallest_mtu > net->mtu) {
 5599                                 sctp_pathmtu_adjustment(stcb, net->mtu);
 5600                         }
 5601                 } else if (port == 0) {
 5602                         /* UDP encapsulation turned off. */
 5603                         net->mtu += sizeof(struct udphdr);
 5604                         /* XXX Update smallest_mtu */
 5605                 }
 5606                 net->port = port;
 5607         }
 5608 #endif
 5609         if (net != NULL) {
 5610                 net->flowtype = mflowtype;
 5611                 net->flowid = mflowid;
 5612         }
 5613         if (inp == NULL) {
 5614                 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
 5615                 SCTP_STAT_INCR(sctps_noport);
 5616                 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
 5617                         goto out;
 5618                 }
 5619                 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
 5620                         sctp_send_shutdown_complete2(src, dst, sh,
 5621                             mflowtype, mflowid, fibnum,
 5622                             vrf_id, port);
 5623                         goto out;
 5624                 }
 5625                 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
 5626                         goto out;
 5627                 }
 5628                 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
 5629                         if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
 5630                             ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
 5631                             (ch->chunk_type != SCTP_INIT))) {
 5632                                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
 5633                                     "Out of the blue");
 5634                                 sctp_send_abort(m, iphlen, src, dst,
 5635                                     sh, 0, op_err,
 5636                                     mflowtype, mflowid, fibnum,
 5637                                     vrf_id, port);
 5638                         }
 5639                 }
 5640                 goto out;
 5641         } else if (stcb == NULL) {
 5642                 inp_decr = inp;
 5643         }
 5644         SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
 5645             (void *)m, iphlen, offset, length, (void *)stcb);
 5646         if (stcb) {
 5647                 /* always clear this before beginning a packet */
 5648                 stcb->asoc.authenticated = 0;
 5649                 stcb->asoc.seen_a_sack_this_pkt = 0;
 5650                 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
 5651                     (void *)stcb, stcb->asoc.state);
 5652 
 5653                 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
 5654                     (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
 5655                         /*-
 5656                          * If we hit here, we had a ref count
 5657                          * up when the assoc was aborted and the
 5658                          * timer is clearing out the assoc, we should
 5659                          * NOT respond to any packet.. its OOTB.
 5660                          */
 5661                         SCTP_TCB_UNLOCK(stcb);
 5662                         stcb = NULL;
 5663                         SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
 5664                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
 5665                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
 5666                             msg);
 5667                         sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
 5668                             mflowtype, mflowid, inp->fibnum,
 5669                             vrf_id, port);
 5670                         goto out;
 5671                 }
 5672         }
 5673         if (IS_SCTP_CONTROL(ch)) {
 5674                 /* process the control portion of the SCTP packet */
 5675                 /* sa_ignore NO_NULL_CHK */
 5676                 stcb = sctp_process_control(m, iphlen, &offset, length,
 5677                     src, dst, sh, ch,
 5678                     inp, stcb, &net, &fwd_tsn_seen,
 5679                     mflowtype, mflowid, fibnum,
 5680                     vrf_id, port);
 5681                 if (stcb) {
 5682                         /*
 5683                          * This covers us if the cookie-echo was there and
 5684                          * it changes our INP.
 5685                          */
 5686                         inp = stcb->sctp_ep;
 5687 #if defined(INET) || defined(INET6)
 5688                         if ((ch->chunk_type != SCTP_INITIATION) &&
 5689                             (net != NULL) && (net->port != port)) {
 5690                                 if (net->port == 0) {
 5691                                         /* UDP encapsulation turned on. */
 5692                                         net->mtu -= sizeof(struct udphdr);
 5693                                         if (stcb->asoc.smallest_mtu > net->mtu) {
 5694                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
 5695                                         }
 5696                                 } else if (port == 0) {
 5697                                         /* UDP encapsulation turned off. */
 5698                                         net->mtu += sizeof(struct udphdr);
 5699                                         /* XXX Update smallest_mtu */
 5700                                 }
 5701                                 net->port = port;
 5702                         }
 5703 #endif
 5704                 }
 5705         } else {
 5706                 /*
 5707                  * no control chunks, so pre-process DATA chunks (these
 5708                  * checks are taken care of by control processing)
 5709                  */
 5710 
 5711                 /*
 5712                  * if DATA only packet, and auth is required, then punt...
 5713                  * can't have authenticated without any AUTH (control)
 5714                  * chunks
 5715                  */
 5716                 if ((stcb != NULL) &&
 5717                     sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
 5718                         /* "silently" ignore */
 5719                         SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
 5720                         SCTP_STAT_INCR(sctps_recvauthmissing);
 5721                         goto out;
 5722                 }
 5723                 if (stcb == NULL) {
 5724                         /* out of the blue DATA chunk */
 5725                         SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
 5726                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
 5727                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
 5728                             msg);
 5729                         sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
 5730                             mflowtype, mflowid, fibnum,
 5731                             vrf_id, port);
 5732                         goto out;
 5733                 }
 5734                 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
 5735                         /* v_tag mismatch! */
 5736                         SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
 5737                         SCTP_STAT_INCR(sctps_badvtag);
 5738                         goto out;
 5739                 }
 5740         }
 5741 
 5742         SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
 5743         if (stcb == NULL) {
 5744                 /*
 5745                  * no valid TCB for this packet, or we found it's a bad
 5746                  * packet while processing control, or we're done with this
 5747                  * packet (done or skip rest of data), so we drop it...
 5748                  */
 5749                 goto out;
 5750         }
 5751 
 5752         /*
 5753          * DATA chunk processing
 5754          */
 5755         /* plow through the data chunks while length > offset */
 5756 
 5757         /*
 5758          * Rest should be DATA only.  Check authentication state if AUTH for
 5759          * DATA is required.
 5760          */
 5761         if ((length > offset) &&
 5762             (stcb != NULL) &&
 5763             sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
 5764             !stcb->asoc.authenticated) {
 5765                 /* "silently" ignore */
 5766                 SCTP_STAT_INCR(sctps_recvauthmissing);
 5767                 SCTPDBG(SCTP_DEBUG_AUTH1,
 5768                     "Data chunk requires AUTH, skipped\n");
 5769                 goto trigger_send;
 5770         }
 5771         if (length > offset) {
 5772                 int retval;
 5773 
 5774                 /*
 5775                  * First check to make sure our state is correct. We would
 5776                  * not get here unless we really did have a tag, so we don't
 5777                  * abort if this happens, just dump the chunk silently.
 5778                  */
 5779                 switch (SCTP_GET_STATE(stcb)) {
 5780                 case SCTP_STATE_COOKIE_ECHOED:
 5781                         /*
 5782                          * we consider data with valid tags in this state
 5783                          * shows us the cookie-ack was lost. Imply it was
 5784                          * there.
 5785                          */
 5786                         sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
 5787                         break;
 5788                 case SCTP_STATE_COOKIE_WAIT:
 5789                         /*
 5790                          * We consider OOTB any data sent during asoc setup.
 5791                          */
 5792                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
 5793                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
 5794                             msg);
 5795                         sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
 5796                             mflowtype, mflowid, inp->fibnum,
 5797                             vrf_id, port);
 5798                         goto out;
 5799                         /* sa_ignore NOTREACHED */
 5800                         break;
 5801                 case SCTP_STATE_EMPTY:  /* should not happen */
 5802                 case SCTP_STATE_INUSE:  /* should not happen */
 5803                 case SCTP_STATE_SHUTDOWN_RECEIVED:      /* This is a peer error */
 5804                 case SCTP_STATE_SHUTDOWN_ACK_SENT:
 5805                 default:
 5806                         goto out;
 5807                         /* sa_ignore NOTREACHED */
 5808                         break;
 5809                 case SCTP_STATE_OPEN:
 5810                 case SCTP_STATE_SHUTDOWN_SENT:
 5811                         break;
 5812                 }
 5813                 /* plow through the data chunks while length > offset */
 5814                 retval = sctp_process_data(mm, iphlen, &offset, length,
 5815                     inp, stcb, net, &high_tsn);
 5816                 if (retval == 2) {
 5817                         /*
 5818                          * The association aborted, NO UNLOCK needed since
 5819                          * the association is destroyed.
 5820                          */
 5821                         stcb = NULL;
 5822                         goto out;
 5823                 }
 5824                 data_processed = 1;
 5825                 /*
 5826                  * Anything important needs to have been m_copy'ed in
 5827                  * process_data
 5828                  */
 5829         }
 5830 
 5831         /* take care of ecn */
 5832         if ((data_processed == 1) &&
 5833             (stcb->asoc.ecn_supported == 1) &&
 5834             ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
 5835                 /* Yep, we need to add a ECNE */
 5836                 sctp_send_ecn_echo(stcb, net, high_tsn);
 5837         }
 5838 
 5839         if ((data_processed == 0) && (fwd_tsn_seen)) {
 5840                 int was_a_gap;
 5841                 uint32_t highest_tsn;
 5842 
 5843                 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
 5844                         highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
 5845                 } else {
 5846                         highest_tsn = stcb->asoc.highest_tsn_inside_map;
 5847                 }
 5848                 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
 5849                 stcb->asoc.send_sack = 1;
 5850                 sctp_sack_check(stcb, was_a_gap);
 5851         } else if (fwd_tsn_seen) {
 5852                 stcb->asoc.send_sack = 1;
 5853         }
 5854         /* trigger send of any chunks in queue... */
 5855 trigger_send:
 5856 #ifdef SCTP_AUDITING_ENABLED
 5857         sctp_audit_log(0xE0, 2);
 5858         sctp_auditing(1, inp, stcb, net);
 5859 #endif
 5860         SCTPDBG(SCTP_DEBUG_INPUT1,
 5861             "Check for chunk output prw:%d tqe:%d tf=%d\n",
 5862             stcb->asoc.peers_rwnd,
 5863             TAILQ_EMPTY(&stcb->asoc.control_send_queue),
 5864             stcb->asoc.total_flight);
 5865         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
 5866         if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
 5867                 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
 5868         }
 5869         if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
 5870             cnt_ctrl_ready ||
 5871             stcb->asoc.trigger_reset ||
 5872             ((un_sent) &&
 5873             (stcb->asoc.peers_rwnd > 0 ||
 5874             (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
 5875                 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
 5876                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
 5877                 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
 5878         }
 5879 #ifdef SCTP_AUDITING_ENABLED
 5880         sctp_audit_log(0xE0, 3);
 5881         sctp_auditing(2, inp, stcb, net);
 5882 #endif
 5883 out:
 5884         if (stcb != NULL) {
 5885                 SCTP_TCB_UNLOCK(stcb);
 5886         }
 5887         if (inp_decr != NULL) {
 5888                 /* reduce ref-count */
 5889                 SCTP_INP_WLOCK(inp_decr);
 5890                 SCTP_INP_DECR_REF(inp_decr);
 5891                 SCTP_INP_WUNLOCK(inp_decr);
 5892         }
 5893         return;
 5894 }
 5895 
 5896 #ifdef INET
 5897 void
 5898 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
 5899 {
 5900         struct mbuf *m;
 5901         int iphlen;
 5902         uint32_t vrf_id = 0;
 5903         uint8_t ecn_bits;
 5904         struct sockaddr_in src, dst;
 5905         struct ip *ip;
 5906         struct sctphdr *sh;
 5907         struct sctp_chunkhdr *ch;
 5908         int length, offset;
 5909         uint8_t compute_crc;
 5910         uint32_t mflowid;
 5911         uint8_t mflowtype;
 5912         uint16_t fibnum;
 5913 
 5914         iphlen = off;
 5915         if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
 5916                 SCTP_RELEASE_PKT(i_pak);
 5917                 return;
 5918         }
 5919         m = SCTP_HEADER_TO_CHAIN(i_pak);
 5920 #ifdef SCTP_MBUF_LOGGING
 5921         /* Log in any input mbufs */
 5922         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 5923                 sctp_log_mbc(m, SCTP_MBUF_INPUT);
 5924         }
 5925 #endif
 5926 #ifdef SCTP_PACKET_LOGGING
 5927         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
 5928                 sctp_packet_log(m);
 5929         }
 5930 #endif
 5931         SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
 5932             "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
 5933             m->m_pkthdr.len,
 5934             if_name(m->m_pkthdr.rcvif),
 5935             (int)m->m_pkthdr.csum_flags, CSUM_BITS);
 5936         mflowid = m->m_pkthdr.flowid;
 5937         mflowtype = M_HASHTYPE_GET(m);
 5938         fibnum = M_GETFIB(m);
 5939         SCTP_STAT_INCR(sctps_recvpackets);
 5940         SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
 5941         /* Get IP, SCTP, and first chunk header together in the first mbuf. */
 5942         offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 5943         if (SCTP_BUF_LEN(m) < offset) {
 5944                 if ((m = m_pullup(m, offset)) == NULL) {
 5945                         SCTP_STAT_INCR(sctps_hdrops);
 5946                         return;
 5947                 }
 5948         }
 5949         ip = mtod(m, struct ip *);
 5950         sh = (struct sctphdr *)((caddr_t)ip + iphlen);
 5951         ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
 5952         offset -= sizeof(struct sctp_chunkhdr);
 5953         memset(&src, 0, sizeof(struct sockaddr_in));
 5954         src.sin_family = AF_INET;
 5955         src.sin_len = sizeof(struct sockaddr_in);
 5956         src.sin_port = sh->src_port;
 5957         src.sin_addr = ip->ip_src;
 5958         memset(&dst, 0, sizeof(struct sockaddr_in));
 5959         dst.sin_family = AF_INET;
 5960         dst.sin_len = sizeof(struct sockaddr_in);
 5961         dst.sin_port = sh->dest_port;
 5962         dst.sin_addr = ip->ip_dst;
 5963         length = ntohs(ip->ip_len);
 5964         /* Validate mbuf chain length with IP payload length. */
 5965         if (SCTP_HEADER_LEN(m) != length) {
 5966                 SCTPDBG(SCTP_DEBUG_INPUT1,
 5967                     "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
 5968                 SCTP_STAT_INCR(sctps_hdrops);
 5969                 goto out;
 5970         }
 5971         /* SCTP does not allow broadcasts or multicasts */
 5972         if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
 5973                 goto out;
 5974         }
 5975         if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
 5976                 goto out;
 5977         }
 5978         ecn_bits = ip->ip_tos;
 5979         if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
 5980                 SCTP_STAT_INCR(sctps_recvhwcrc);
 5981                 compute_crc = 0;
 5982         } else {
 5983                 SCTP_STAT_INCR(sctps_recvswcrc);
 5984                 compute_crc = 1;
 5985         }
 5986         sctp_common_input_processing(&m, iphlen, offset, length,
 5987             (struct sockaddr *)&src,
 5988             (struct sockaddr *)&dst,
 5989             sh, ch,
 5990             compute_crc,
 5991             ecn_bits,
 5992             mflowtype, mflowid, fibnum,
 5993             vrf_id, port);
 5994 out:
 5995         if (m) {
 5996                 sctp_m_freem(m);
 5997         }
 5998         return;
 5999 }
 6000 
 6001 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
 6002 extern int *sctp_cpuarry;
 6003 #endif
 6004 
 6005 int
 6006 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
 6007 {
 6008         struct mbuf *m;
 6009         int off;
 6010 
 6011         m = *mp;
 6012         off = *offp;
 6013 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
 6014         if (mp_ncpus > 1) {
 6015                 struct ip *ip;
 6016                 struct sctphdr *sh;
 6017                 int offset;
 6018                 int cpu_to_use;
 6019                 uint32_t flowid, tag;
 6020 
 6021                 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
 6022                         flowid = m->m_pkthdr.flowid;
 6023                 } else {
 6024                         /*
 6025                          * No flow id built by lower layers fix it so we
 6026                          * create one.
 6027                          */
 6028                         offset = off + sizeof(struct sctphdr);
 6029                         if (SCTP_BUF_LEN(m) < offset) {
 6030                                 if ((m = m_pullup(m, offset)) == NULL) {
 6031                                         SCTP_STAT_INCR(sctps_hdrops);
 6032                                         return (IPPROTO_DONE);
 6033                                 }
 6034                         }
 6035                         ip = mtod(m, struct ip *);
 6036                         sh = (struct sctphdr *)((caddr_t)ip + off);
 6037                         tag = htonl(sh->v_tag);
 6038                         flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
 6039                         m->m_pkthdr.flowid = flowid;
 6040                         M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
 6041                 }
 6042                 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
 6043                 sctp_queue_to_mcore(m, off, cpu_to_use);
 6044                 return (IPPROTO_DONE);
 6045         }
 6046 #endif
 6047         sctp_input_with_port(m, off, 0);
 6048         return (IPPROTO_DONE);
 6049 }
 6050 #endif

Cache object: 7974fd4b0b398463f5fe9d61a42cb50b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.