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

Cache object: f21986742d2d14d8774d9cd9d3b784ca


[ 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.