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_output.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  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
    4  * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions are met:
    8  *
    9  * a) Redistributions of source code must retain the above copyright notice,
   10  *    this list of conditions and the following disclaimer.
   11  *
   12  * b) Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in
   14  *    the documentation and/or other materials provided with the distribution.
   15  *
   16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
   17  *    contributors may be used to endorse or promote products derived
   18  *    from this software without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   30  * THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/8.3/sys/netinet/sctp_output.c 254632 2013-08-22 00:51:56Z delphij $");
   37 
   38 #include <netinet/sctp_os.h>
   39 #include <sys/proc.h>
   40 #include <netinet/sctp_var.h>
   41 #include <netinet/sctp_sysctl.h>
   42 #include <netinet/sctp_header.h>
   43 #include <netinet/sctp_pcb.h>
   44 #include <netinet/sctputil.h>
   45 #include <netinet/sctp_output.h>
   46 #include <netinet/sctp_uio.h>
   47 #include <netinet/sctputil.h>
   48 #include <netinet/sctp_auth.h>
   49 #include <netinet/sctp_timer.h>
   50 #include <netinet/sctp_asconf.h>
   51 #include <netinet/sctp_indata.h>
   52 #include <netinet/sctp_bsd_addr.h>
   53 #include <netinet/sctp_input.h>
   54 #include <netinet/sctp_crc32.h>
   55 #include <netinet/udp.h>
   56 #include <machine/in_cksum.h>
   57 
   58 
   59 
   60 #define SCTP_MAX_GAPS_INARRAY 4
   61 struct sack_track {
   62         uint8_t right_edge;     /* mergable on the right edge */
   63         uint8_t left_edge;      /* mergable on the left edge */
   64         uint8_t num_entries;
   65         uint8_t spare;
   66         struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
   67 };
   68 
   69 struct sack_track sack_array[256] = {
   70         {0, 0, 0, 0,            /* 0x00 */
   71                 {{0, 0},
   72                 {0, 0},
   73                 {0, 0},
   74                 {0, 0}
   75                 }
   76         },
   77         {1, 0, 1, 0,            /* 0x01 */
   78                 {{0, 0},
   79                 {0, 0},
   80                 {0, 0},
   81                 {0, 0}
   82                 }
   83         },
   84         {0, 0, 1, 0,            /* 0x02 */
   85                 {{1, 1},
   86                 {0, 0},
   87                 {0, 0},
   88                 {0, 0}
   89                 }
   90         },
   91         {1, 0, 1, 0,            /* 0x03 */
   92                 {{0, 1},
   93                 {0, 0},
   94                 {0, 0},
   95                 {0, 0}
   96                 }
   97         },
   98         {0, 0, 1, 0,            /* 0x04 */
   99                 {{2, 2},
  100                 {0, 0},
  101                 {0, 0},
  102                 {0, 0}
  103                 }
  104         },
  105         {1, 0, 2, 0,            /* 0x05 */
  106                 {{0, 0},
  107                 {2, 2},
  108                 {0, 0},
  109                 {0, 0}
  110                 }
  111         },
  112         {0, 0, 1, 0,            /* 0x06 */
  113                 {{1, 2},
  114                 {0, 0},
  115                 {0, 0},
  116                 {0, 0}
  117                 }
  118         },
  119         {1, 0, 1, 0,            /* 0x07 */
  120                 {{0, 2},
  121                 {0, 0},
  122                 {0, 0},
  123                 {0, 0}
  124                 }
  125         },
  126         {0, 0, 1, 0,            /* 0x08 */
  127                 {{3, 3},
  128                 {0, 0},
  129                 {0, 0},
  130                 {0, 0}
  131                 }
  132         },
  133         {1, 0, 2, 0,            /* 0x09 */
  134                 {{0, 0},
  135                 {3, 3},
  136                 {0, 0},
  137                 {0, 0}
  138                 }
  139         },
  140         {0, 0, 2, 0,            /* 0x0a */
  141                 {{1, 1},
  142                 {3, 3},
  143                 {0, 0},
  144                 {0, 0}
  145                 }
  146         },
  147         {1, 0, 2, 0,            /* 0x0b */
  148                 {{0, 1},
  149                 {3, 3},
  150                 {0, 0},
  151                 {0, 0}
  152                 }
  153         },
  154         {0, 0, 1, 0,            /* 0x0c */
  155                 {{2, 3},
  156                 {0, 0},
  157                 {0, 0},
  158                 {0, 0}
  159                 }
  160         },
  161         {1, 0, 2, 0,            /* 0x0d */
  162                 {{0, 0},
  163                 {2, 3},
  164                 {0, 0},
  165                 {0, 0}
  166                 }
  167         },
  168         {0, 0, 1, 0,            /* 0x0e */
  169                 {{1, 3},
  170                 {0, 0},
  171                 {0, 0},
  172                 {0, 0}
  173                 }
  174         },
  175         {1, 0, 1, 0,            /* 0x0f */
  176                 {{0, 3},
  177                 {0, 0},
  178                 {0, 0},
  179                 {0, 0}
  180                 }
  181         },
  182         {0, 0, 1, 0,            /* 0x10 */
  183                 {{4, 4},
  184                 {0, 0},
  185                 {0, 0},
  186                 {0, 0}
  187                 }
  188         },
  189         {1, 0, 2, 0,            /* 0x11 */
  190                 {{0, 0},
  191                 {4, 4},
  192                 {0, 0},
  193                 {0, 0}
  194                 }
  195         },
  196         {0, 0, 2, 0,            /* 0x12 */
  197                 {{1, 1},
  198                 {4, 4},
  199                 {0, 0},
  200                 {0, 0}
  201                 }
  202         },
  203         {1, 0, 2, 0,            /* 0x13 */
  204                 {{0, 1},
  205                 {4, 4},
  206                 {0, 0},
  207                 {0, 0}
  208                 }
  209         },
  210         {0, 0, 2, 0,            /* 0x14 */
  211                 {{2, 2},
  212                 {4, 4},
  213                 {0, 0},
  214                 {0, 0}
  215                 }
  216         },
  217         {1, 0, 3, 0,            /* 0x15 */
  218                 {{0, 0},
  219                 {2, 2},
  220                 {4, 4},
  221                 {0, 0}
  222                 }
  223         },
  224         {0, 0, 2, 0,            /* 0x16 */
  225                 {{1, 2},
  226                 {4, 4},
  227                 {0, 0},
  228                 {0, 0}
  229                 }
  230         },
  231         {1, 0, 2, 0,            /* 0x17 */
  232                 {{0, 2},
  233                 {4, 4},
  234                 {0, 0},
  235                 {0, 0}
  236                 }
  237         },
  238         {0, 0, 1, 0,            /* 0x18 */
  239                 {{3, 4},
  240                 {0, 0},
  241                 {0, 0},
  242                 {0, 0}
  243                 }
  244         },
  245         {1, 0, 2, 0,            /* 0x19 */
  246                 {{0, 0},
  247                 {3, 4},
  248                 {0, 0},
  249                 {0, 0}
  250                 }
  251         },
  252         {0, 0, 2, 0,            /* 0x1a */
  253                 {{1, 1},
  254                 {3, 4},
  255                 {0, 0},
  256                 {0, 0}
  257                 }
  258         },
  259         {1, 0, 2, 0,            /* 0x1b */
  260                 {{0, 1},
  261                 {3, 4},
  262                 {0, 0},
  263                 {0, 0}
  264                 }
  265         },
  266         {0, 0, 1, 0,            /* 0x1c */
  267                 {{2, 4},
  268                 {0, 0},
  269                 {0, 0},
  270                 {0, 0}
  271                 }
  272         },
  273         {1, 0, 2, 0,            /* 0x1d */
  274                 {{0, 0},
  275                 {2, 4},
  276                 {0, 0},
  277                 {0, 0}
  278                 }
  279         },
  280         {0, 0, 1, 0,            /* 0x1e */
  281                 {{1, 4},
  282                 {0, 0},
  283                 {0, 0},
  284                 {0, 0}
  285                 }
  286         },
  287         {1, 0, 1, 0,            /* 0x1f */
  288                 {{0, 4},
  289                 {0, 0},
  290                 {0, 0},
  291                 {0, 0}
  292                 }
  293         },
  294         {0, 0, 1, 0,            /* 0x20 */
  295                 {{5, 5},
  296                 {0, 0},
  297                 {0, 0},
  298                 {0, 0}
  299                 }
  300         },
  301         {1, 0, 2, 0,            /* 0x21 */
  302                 {{0, 0},
  303                 {5, 5},
  304                 {0, 0},
  305                 {0, 0}
  306                 }
  307         },
  308         {0, 0, 2, 0,            /* 0x22 */
  309                 {{1, 1},
  310                 {5, 5},
  311                 {0, 0},
  312                 {0, 0}
  313                 }
  314         },
  315         {1, 0, 2, 0,            /* 0x23 */
  316                 {{0, 1},
  317                 {5, 5},
  318                 {0, 0},
  319                 {0, 0}
  320                 }
  321         },
  322         {0, 0, 2, 0,            /* 0x24 */
  323                 {{2, 2},
  324                 {5, 5},
  325                 {0, 0},
  326                 {0, 0}
  327                 }
  328         },
  329         {1, 0, 3, 0,            /* 0x25 */
  330                 {{0, 0},
  331                 {2, 2},
  332                 {5, 5},
  333                 {0, 0}
  334                 }
  335         },
  336         {0, 0, 2, 0,            /* 0x26 */
  337                 {{1, 2},
  338                 {5, 5},
  339                 {0, 0},
  340                 {0, 0}
  341                 }
  342         },
  343         {1, 0, 2, 0,            /* 0x27 */
  344                 {{0, 2},
  345                 {5, 5},
  346                 {0, 0},
  347                 {0, 0}
  348                 }
  349         },
  350         {0, 0, 2, 0,            /* 0x28 */
  351                 {{3, 3},
  352                 {5, 5},
  353                 {0, 0},
  354                 {0, 0}
  355                 }
  356         },
  357         {1, 0, 3, 0,            /* 0x29 */
  358                 {{0, 0},
  359                 {3, 3},
  360                 {5, 5},
  361                 {0, 0}
  362                 }
  363         },
  364         {0, 0, 3, 0,            /* 0x2a */
  365                 {{1, 1},
  366                 {3, 3},
  367                 {5, 5},
  368                 {0, 0}
  369                 }
  370         },
  371         {1, 0, 3, 0,            /* 0x2b */
  372                 {{0, 1},
  373                 {3, 3},
  374                 {5, 5},
  375                 {0, 0}
  376                 }
  377         },
  378         {0, 0, 2, 0,            /* 0x2c */
  379                 {{2, 3},
  380                 {5, 5},
  381                 {0, 0},
  382                 {0, 0}
  383                 }
  384         },
  385         {1, 0, 3, 0,            /* 0x2d */
  386                 {{0, 0},
  387                 {2, 3},
  388                 {5, 5},
  389                 {0, 0}
  390                 }
  391         },
  392         {0, 0, 2, 0,            /* 0x2e */
  393                 {{1, 3},
  394                 {5, 5},
  395                 {0, 0},
  396                 {0, 0}
  397                 }
  398         },
  399         {1, 0, 2, 0,            /* 0x2f */
  400                 {{0, 3},
  401                 {5, 5},
  402                 {0, 0},
  403                 {0, 0}
  404                 }
  405         },
  406         {0, 0, 1, 0,            /* 0x30 */
  407                 {{4, 5},
  408                 {0, 0},
  409                 {0, 0},
  410                 {0, 0}
  411                 }
  412         },
  413         {1, 0, 2, 0,            /* 0x31 */
  414                 {{0, 0},
  415                 {4, 5},
  416                 {0, 0},
  417                 {0, 0}
  418                 }
  419         },
  420         {0, 0, 2, 0,            /* 0x32 */
  421                 {{1, 1},
  422                 {4, 5},
  423                 {0, 0},
  424                 {0, 0}
  425                 }
  426         },
  427         {1, 0, 2, 0,            /* 0x33 */
  428                 {{0, 1},
  429                 {4, 5},
  430                 {0, 0},
  431                 {0, 0}
  432                 }
  433         },
  434         {0, 0, 2, 0,            /* 0x34 */
  435                 {{2, 2},
  436                 {4, 5},
  437                 {0, 0},
  438                 {0, 0}
  439                 }
  440         },
  441         {1, 0, 3, 0,            /* 0x35 */
  442                 {{0, 0},
  443                 {2, 2},
  444                 {4, 5},
  445                 {0, 0}
  446                 }
  447         },
  448         {0, 0, 2, 0,            /* 0x36 */
  449                 {{1, 2},
  450                 {4, 5},
  451                 {0, 0},
  452                 {0, 0}
  453                 }
  454         },
  455         {1, 0, 2, 0,            /* 0x37 */
  456                 {{0, 2},
  457                 {4, 5},
  458                 {0, 0},
  459                 {0, 0}
  460                 }
  461         },
  462         {0, 0, 1, 0,            /* 0x38 */
  463                 {{3, 5},
  464                 {0, 0},
  465                 {0, 0},
  466                 {0, 0}
  467                 }
  468         },
  469         {1, 0, 2, 0,            /* 0x39 */
  470                 {{0, 0},
  471                 {3, 5},
  472                 {0, 0},
  473                 {0, 0}
  474                 }
  475         },
  476         {0, 0, 2, 0,            /* 0x3a */
  477                 {{1, 1},
  478                 {3, 5},
  479                 {0, 0},
  480                 {0, 0}
  481                 }
  482         },
  483         {1, 0, 2, 0,            /* 0x3b */
  484                 {{0, 1},
  485                 {3, 5},
  486                 {0, 0},
  487                 {0, 0}
  488                 }
  489         },
  490         {0, 0, 1, 0,            /* 0x3c */
  491                 {{2, 5},
  492                 {0, 0},
  493                 {0, 0},
  494                 {0, 0}
  495                 }
  496         },
  497         {1, 0, 2, 0,            /* 0x3d */
  498                 {{0, 0},
  499                 {2, 5},
  500                 {0, 0},
  501                 {0, 0}
  502                 }
  503         },
  504         {0, 0, 1, 0,            /* 0x3e */
  505                 {{1, 5},
  506                 {0, 0},
  507                 {0, 0},
  508                 {0, 0}
  509                 }
  510         },
  511         {1, 0, 1, 0,            /* 0x3f */
  512                 {{0, 5},
  513                 {0, 0},
  514                 {0, 0},
  515                 {0, 0}
  516                 }
  517         },
  518         {0, 0, 1, 0,            /* 0x40 */
  519                 {{6, 6},
  520                 {0, 0},
  521                 {0, 0},
  522                 {0, 0}
  523                 }
  524         },
  525         {1, 0, 2, 0,            /* 0x41 */
  526                 {{0, 0},
  527                 {6, 6},
  528                 {0, 0},
  529                 {0, 0}
  530                 }
  531         },
  532         {0, 0, 2, 0,            /* 0x42 */
  533                 {{1, 1},
  534                 {6, 6},
  535                 {0, 0},
  536                 {0, 0}
  537                 }
  538         },
  539         {1, 0, 2, 0,            /* 0x43 */
  540                 {{0, 1},
  541                 {6, 6},
  542                 {0, 0},
  543                 {0, 0}
  544                 }
  545         },
  546         {0, 0, 2, 0,            /* 0x44 */
  547                 {{2, 2},
  548                 {6, 6},
  549                 {0, 0},
  550                 {0, 0}
  551                 }
  552         },
  553         {1, 0, 3, 0,            /* 0x45 */
  554                 {{0, 0},
  555                 {2, 2},
  556                 {6, 6},
  557                 {0, 0}
  558                 }
  559         },
  560         {0, 0, 2, 0,            /* 0x46 */
  561                 {{1, 2},
  562                 {6, 6},
  563                 {0, 0},
  564                 {0, 0}
  565                 }
  566         },
  567         {1, 0, 2, 0,            /* 0x47 */
  568                 {{0, 2},
  569                 {6, 6},
  570                 {0, 0},
  571                 {0, 0}
  572                 }
  573         },
  574         {0, 0, 2, 0,            /* 0x48 */
  575                 {{3, 3},
  576                 {6, 6},
  577                 {0, 0},
  578                 {0, 0}
  579                 }
  580         },
  581         {1, 0, 3, 0,            /* 0x49 */
  582                 {{0, 0},
  583                 {3, 3},
  584                 {6, 6},
  585                 {0, 0}
  586                 }
  587         },
  588         {0, 0, 3, 0,            /* 0x4a */
  589                 {{1, 1},
  590                 {3, 3},
  591                 {6, 6},
  592                 {0, 0}
  593                 }
  594         },
  595         {1, 0, 3, 0,            /* 0x4b */
  596                 {{0, 1},
  597                 {3, 3},
  598                 {6, 6},
  599                 {0, 0}
  600                 }
  601         },
  602         {0, 0, 2, 0,            /* 0x4c */
  603                 {{2, 3},
  604                 {6, 6},
  605                 {0, 0},
  606                 {0, 0}
  607                 }
  608         },
  609         {1, 0, 3, 0,            /* 0x4d */
  610                 {{0, 0},
  611                 {2, 3},
  612                 {6, 6},
  613                 {0, 0}
  614                 }
  615         },
  616         {0, 0, 2, 0,            /* 0x4e */
  617                 {{1, 3},
  618                 {6, 6},
  619                 {0, 0},
  620                 {0, 0}
  621                 }
  622         },
  623         {1, 0, 2, 0,            /* 0x4f */
  624                 {{0, 3},
  625                 {6, 6},
  626                 {0, 0},
  627                 {0, 0}
  628                 }
  629         },
  630         {0, 0, 2, 0,            /* 0x50 */
  631                 {{4, 4},
  632                 {6, 6},
  633                 {0, 0},
  634                 {0, 0}
  635                 }
  636         },
  637         {1, 0, 3, 0,            /* 0x51 */
  638                 {{0, 0},
  639                 {4, 4},
  640                 {6, 6},
  641                 {0, 0}
  642                 }
  643         },
  644         {0, 0, 3, 0,            /* 0x52 */
  645                 {{1, 1},
  646                 {4, 4},
  647                 {6, 6},
  648                 {0, 0}
  649                 }
  650         },
  651         {1, 0, 3, 0,            /* 0x53 */
  652                 {{0, 1},
  653                 {4, 4},
  654                 {6, 6},
  655                 {0, 0}
  656                 }
  657         },
  658         {0, 0, 3, 0,            /* 0x54 */
  659                 {{2, 2},
  660                 {4, 4},
  661                 {6, 6},
  662                 {0, 0}
  663                 }
  664         },
  665         {1, 0, 4, 0,            /* 0x55 */
  666                 {{0, 0},
  667                 {2, 2},
  668                 {4, 4},
  669                 {6, 6}
  670                 }
  671         },
  672         {0, 0, 3, 0,            /* 0x56 */
  673                 {{1, 2},
  674                 {4, 4},
  675                 {6, 6},
  676                 {0, 0}
  677                 }
  678         },
  679         {1, 0, 3, 0,            /* 0x57 */
  680                 {{0, 2},
  681                 {4, 4},
  682                 {6, 6},
  683                 {0, 0}
  684                 }
  685         },
  686         {0, 0, 2, 0,            /* 0x58 */
  687                 {{3, 4},
  688                 {6, 6},
  689                 {0, 0},
  690                 {0, 0}
  691                 }
  692         },
  693         {1, 0, 3, 0,            /* 0x59 */
  694                 {{0, 0},
  695                 {3, 4},
  696                 {6, 6},
  697                 {0, 0}
  698                 }
  699         },
  700         {0, 0, 3, 0,            /* 0x5a */
  701                 {{1, 1},
  702                 {3, 4},
  703                 {6, 6},
  704                 {0, 0}
  705                 }
  706         },
  707         {1, 0, 3, 0,            /* 0x5b */
  708                 {{0, 1},
  709                 {3, 4},
  710                 {6, 6},
  711                 {0, 0}
  712                 }
  713         },
  714         {0, 0, 2, 0,            /* 0x5c */
  715                 {{2, 4},
  716                 {6, 6},
  717                 {0, 0},
  718                 {0, 0}
  719                 }
  720         },
  721         {1, 0, 3, 0,            /* 0x5d */
  722                 {{0, 0},
  723                 {2, 4},
  724                 {6, 6},
  725                 {0, 0}
  726                 }
  727         },
  728         {0, 0, 2, 0,            /* 0x5e */
  729                 {{1, 4},
  730                 {6, 6},
  731                 {0, 0},
  732                 {0, 0}
  733                 }
  734         },
  735         {1, 0, 2, 0,            /* 0x5f */
  736                 {{0, 4},
  737                 {6, 6},
  738                 {0, 0},
  739                 {0, 0}
  740                 }
  741         },
  742         {0, 0, 1, 0,            /* 0x60 */
  743                 {{5, 6},
  744                 {0, 0},
  745                 {0, 0},
  746                 {0, 0}
  747                 }
  748         },
  749         {1, 0, 2, 0,            /* 0x61 */
  750                 {{0, 0},
  751                 {5, 6},
  752                 {0, 0},
  753                 {0, 0}
  754                 }
  755         },
  756         {0, 0, 2, 0,            /* 0x62 */
  757                 {{1, 1},
  758                 {5, 6},
  759                 {0, 0},
  760                 {0, 0}
  761                 }
  762         },
  763         {1, 0, 2, 0,            /* 0x63 */
  764                 {{0, 1},
  765                 {5, 6},
  766                 {0, 0},
  767                 {0, 0}
  768                 }
  769         },
  770         {0, 0, 2, 0,            /* 0x64 */
  771                 {{2, 2},
  772                 {5, 6},
  773                 {0, 0},
  774                 {0, 0}
  775                 }
  776         },
  777         {1, 0, 3, 0,            /* 0x65 */
  778                 {{0, 0},
  779                 {2, 2},
  780                 {5, 6},
  781                 {0, 0}
  782                 }
  783         },
  784         {0, 0, 2, 0,            /* 0x66 */
  785                 {{1, 2},
  786                 {5, 6},
  787                 {0, 0},
  788                 {0, 0}
  789                 }
  790         },
  791         {1, 0, 2, 0,            /* 0x67 */
  792                 {{0, 2},
  793                 {5, 6},
  794                 {0, 0},
  795                 {0, 0}
  796                 }
  797         },
  798         {0, 0, 2, 0,            /* 0x68 */
  799                 {{3, 3},
  800                 {5, 6},
  801                 {0, 0},
  802                 {0, 0}
  803                 }
  804         },
  805         {1, 0, 3, 0,            /* 0x69 */
  806                 {{0, 0},
  807                 {3, 3},
  808                 {5, 6},
  809                 {0, 0}
  810                 }
  811         },
  812         {0, 0, 3, 0,            /* 0x6a */
  813                 {{1, 1},
  814                 {3, 3},
  815                 {5, 6},
  816                 {0, 0}
  817                 }
  818         },
  819         {1, 0, 3, 0,            /* 0x6b */
  820                 {{0, 1},
  821                 {3, 3},
  822                 {5, 6},
  823                 {0, 0}
  824                 }
  825         },
  826         {0, 0, 2, 0,            /* 0x6c */
  827                 {{2, 3},
  828                 {5, 6},
  829                 {0, 0},
  830                 {0, 0}
  831                 }
  832         },
  833         {1, 0, 3, 0,            /* 0x6d */
  834                 {{0, 0},
  835                 {2, 3},
  836                 {5, 6},
  837                 {0, 0}
  838                 }
  839         },
  840         {0, 0, 2, 0,            /* 0x6e */
  841                 {{1, 3},
  842                 {5, 6},
  843                 {0, 0},
  844                 {0, 0}
  845                 }
  846         },
  847         {1, 0, 2, 0,            /* 0x6f */
  848                 {{0, 3},
  849                 {5, 6},
  850                 {0, 0},
  851                 {0, 0}
  852                 }
  853         },
  854         {0, 0, 1, 0,            /* 0x70 */
  855                 {{4, 6},
  856                 {0, 0},
  857                 {0, 0},
  858                 {0, 0}
  859                 }
  860         },
  861         {1, 0, 2, 0,            /* 0x71 */
  862                 {{0, 0},
  863                 {4, 6},
  864                 {0, 0},
  865                 {0, 0}
  866                 }
  867         },
  868         {0, 0, 2, 0,            /* 0x72 */
  869                 {{1, 1},
  870                 {4, 6},
  871                 {0, 0},
  872                 {0, 0}
  873                 }
  874         },
  875         {1, 0, 2, 0,            /* 0x73 */
  876                 {{0, 1},
  877                 {4, 6},
  878                 {0, 0},
  879                 {0, 0}
  880                 }
  881         },
  882         {0, 0, 2, 0,            /* 0x74 */
  883                 {{2, 2},
  884                 {4, 6},
  885                 {0, 0},
  886                 {0, 0}
  887                 }
  888         },
  889         {1, 0, 3, 0,            /* 0x75 */
  890                 {{0, 0},
  891                 {2, 2},
  892                 {4, 6},
  893                 {0, 0}
  894                 }
  895         },
  896         {0, 0, 2, 0,            /* 0x76 */
  897                 {{1, 2},
  898                 {4, 6},
  899                 {0, 0},
  900                 {0, 0}
  901                 }
  902         },
  903         {1, 0, 2, 0,            /* 0x77 */
  904                 {{0, 2},
  905                 {4, 6},
  906                 {0, 0},
  907                 {0, 0}
  908                 }
  909         },
  910         {0, 0, 1, 0,            /* 0x78 */
  911                 {{3, 6},
  912                 {0, 0},
  913                 {0, 0},
  914                 {0, 0}
  915                 }
  916         },
  917         {1, 0, 2, 0,            /* 0x79 */
  918                 {{0, 0},
  919                 {3, 6},
  920                 {0, 0},
  921                 {0, 0}
  922                 }
  923         },
  924         {0, 0, 2, 0,            /* 0x7a */
  925                 {{1, 1},
  926                 {3, 6},
  927                 {0, 0},
  928                 {0, 0}
  929                 }
  930         },
  931         {1, 0, 2, 0,            /* 0x7b */
  932                 {{0, 1},
  933                 {3, 6},
  934                 {0, 0},
  935                 {0, 0}
  936                 }
  937         },
  938         {0, 0, 1, 0,            /* 0x7c */
  939                 {{2, 6},
  940                 {0, 0},
  941                 {0, 0},
  942                 {0, 0}
  943                 }
  944         },
  945         {1, 0, 2, 0,            /* 0x7d */
  946                 {{0, 0},
  947                 {2, 6},
  948                 {0, 0},
  949                 {0, 0}
  950                 }
  951         },
  952         {0, 0, 1, 0,            /* 0x7e */
  953                 {{1, 6},
  954                 {0, 0},
  955                 {0, 0},
  956                 {0, 0}
  957                 }
  958         },
  959         {1, 0, 1, 0,            /* 0x7f */
  960                 {{0, 6},
  961                 {0, 0},
  962                 {0, 0},
  963                 {0, 0}
  964                 }
  965         },
  966         {0, 1, 1, 0,            /* 0x80 */
  967                 {{7, 7},
  968                 {0, 0},
  969                 {0, 0},
  970                 {0, 0}
  971                 }
  972         },
  973         {1, 1, 2, 0,            /* 0x81 */
  974                 {{0, 0},
  975                 {7, 7},
  976                 {0, 0},
  977                 {0, 0}
  978                 }
  979         },
  980         {0, 1, 2, 0,            /* 0x82 */
  981                 {{1, 1},
  982                 {7, 7},
  983                 {0, 0},
  984                 {0, 0}
  985                 }
  986         },
  987         {1, 1, 2, 0,            /* 0x83 */
  988                 {{0, 1},
  989                 {7, 7},
  990                 {0, 0},
  991                 {0, 0}
  992                 }
  993         },
  994         {0, 1, 2, 0,            /* 0x84 */
  995                 {{2, 2},
  996                 {7, 7},
  997                 {0, 0},
  998                 {0, 0}
  999                 }
 1000         },
 1001         {1, 1, 3, 0,            /* 0x85 */
 1002                 {{0, 0},
 1003                 {2, 2},
 1004                 {7, 7},
 1005                 {0, 0}
 1006                 }
 1007         },
 1008         {0, 1, 2, 0,            /* 0x86 */
 1009                 {{1, 2},
 1010                 {7, 7},
 1011                 {0, 0},
 1012                 {0, 0}
 1013                 }
 1014         },
 1015         {1, 1, 2, 0,            /* 0x87 */
 1016                 {{0, 2},
 1017                 {7, 7},
 1018                 {0, 0},
 1019                 {0, 0}
 1020                 }
 1021         },
 1022         {0, 1, 2, 0,            /* 0x88 */
 1023                 {{3, 3},
 1024                 {7, 7},
 1025                 {0, 0},
 1026                 {0, 0}
 1027                 }
 1028         },
 1029         {1, 1, 3, 0,            /* 0x89 */
 1030                 {{0, 0},
 1031                 {3, 3},
 1032                 {7, 7},
 1033                 {0, 0}
 1034                 }
 1035         },
 1036         {0, 1, 3, 0,            /* 0x8a */
 1037                 {{1, 1},
 1038                 {3, 3},
 1039                 {7, 7},
 1040                 {0, 0}
 1041                 }
 1042         },
 1043         {1, 1, 3, 0,            /* 0x8b */
 1044                 {{0, 1},
 1045                 {3, 3},
 1046                 {7, 7},
 1047                 {0, 0}
 1048                 }
 1049         },
 1050         {0, 1, 2, 0,            /* 0x8c */
 1051                 {{2, 3},
 1052                 {7, 7},
 1053                 {0, 0},
 1054                 {0, 0}
 1055                 }
 1056         },
 1057         {1, 1, 3, 0,            /* 0x8d */
 1058                 {{0, 0},
 1059                 {2, 3},
 1060                 {7, 7},
 1061                 {0, 0}
 1062                 }
 1063         },
 1064         {0, 1, 2, 0,            /* 0x8e */
 1065                 {{1, 3},
 1066                 {7, 7},
 1067                 {0, 0},
 1068                 {0, 0}
 1069                 }
 1070         },
 1071         {1, 1, 2, 0,            /* 0x8f */
 1072                 {{0, 3},
 1073                 {7, 7},
 1074                 {0, 0},
 1075                 {0, 0}
 1076                 }
 1077         },
 1078         {0, 1, 2, 0,            /* 0x90 */
 1079                 {{4, 4},
 1080                 {7, 7},
 1081                 {0, 0},
 1082                 {0, 0}
 1083                 }
 1084         },
 1085         {1, 1, 3, 0,            /* 0x91 */
 1086                 {{0, 0},
 1087                 {4, 4},
 1088                 {7, 7},
 1089                 {0, 0}
 1090                 }
 1091         },
 1092         {0, 1, 3, 0,            /* 0x92 */
 1093                 {{1, 1},
 1094                 {4, 4},
 1095                 {7, 7},
 1096                 {0, 0}
 1097                 }
 1098         },
 1099         {1, 1, 3, 0,            /* 0x93 */
 1100                 {{0, 1},
 1101                 {4, 4},
 1102                 {7, 7},
 1103                 {0, 0}
 1104                 }
 1105         },
 1106         {0, 1, 3, 0,            /* 0x94 */
 1107                 {{2, 2},
 1108                 {4, 4},
 1109                 {7, 7},
 1110                 {0, 0}
 1111                 }
 1112         },
 1113         {1, 1, 4, 0,            /* 0x95 */
 1114                 {{0, 0},
 1115                 {2, 2},
 1116                 {4, 4},
 1117                 {7, 7}
 1118                 }
 1119         },
 1120         {0, 1, 3, 0,            /* 0x96 */
 1121                 {{1, 2},
 1122                 {4, 4},
 1123                 {7, 7},
 1124                 {0, 0}
 1125                 }
 1126         },
 1127         {1, 1, 3, 0,            /* 0x97 */
 1128                 {{0, 2},
 1129                 {4, 4},
 1130                 {7, 7},
 1131                 {0, 0}
 1132                 }
 1133         },
 1134         {0, 1, 2, 0,            /* 0x98 */
 1135                 {{3, 4},
 1136                 {7, 7},
 1137                 {0, 0},
 1138                 {0, 0}
 1139                 }
 1140         },
 1141         {1, 1, 3, 0,            /* 0x99 */
 1142                 {{0, 0},
 1143                 {3, 4},
 1144                 {7, 7},
 1145                 {0, 0}
 1146                 }
 1147         },
 1148         {0, 1, 3, 0,            /* 0x9a */
 1149                 {{1, 1},
 1150                 {3, 4},
 1151                 {7, 7},
 1152                 {0, 0}
 1153                 }
 1154         },
 1155         {1, 1, 3, 0,            /* 0x9b */
 1156                 {{0, 1},
 1157                 {3, 4},
 1158                 {7, 7},
 1159                 {0, 0}
 1160                 }
 1161         },
 1162         {0, 1, 2, 0,            /* 0x9c */
 1163                 {{2, 4},
 1164                 {7, 7},
 1165                 {0, 0},
 1166                 {0, 0}
 1167                 }
 1168         },
 1169         {1, 1, 3, 0,            /* 0x9d */
 1170                 {{0, 0},
 1171                 {2, 4},
 1172                 {7, 7},
 1173                 {0, 0}
 1174                 }
 1175         },
 1176         {0, 1, 2, 0,            /* 0x9e */
 1177                 {{1, 4},
 1178                 {7, 7},
 1179                 {0, 0},
 1180                 {0, 0}
 1181                 }
 1182         },
 1183         {1, 1, 2, 0,            /* 0x9f */
 1184                 {{0, 4},
 1185                 {7, 7},
 1186                 {0, 0},
 1187                 {0, 0}
 1188                 }
 1189         },
 1190         {0, 1, 2, 0,            /* 0xa0 */
 1191                 {{5, 5},
 1192                 {7, 7},
 1193                 {0, 0},
 1194                 {0, 0}
 1195                 }
 1196         },
 1197         {1, 1, 3, 0,            /* 0xa1 */
 1198                 {{0, 0},
 1199                 {5, 5},
 1200                 {7, 7},
 1201                 {0, 0}
 1202                 }
 1203         },
 1204         {0, 1, 3, 0,            /* 0xa2 */
 1205                 {{1, 1},
 1206                 {5, 5},
 1207                 {7, 7},
 1208                 {0, 0}
 1209                 }
 1210         },
 1211         {1, 1, 3, 0,            /* 0xa3 */
 1212                 {{0, 1},
 1213                 {5, 5},
 1214                 {7, 7},
 1215                 {0, 0}
 1216                 }
 1217         },
 1218         {0, 1, 3, 0,            /* 0xa4 */
 1219                 {{2, 2},
 1220                 {5, 5},
 1221                 {7, 7},
 1222                 {0, 0}
 1223                 }
 1224         },
 1225         {1, 1, 4, 0,            /* 0xa5 */
 1226                 {{0, 0},
 1227                 {2, 2},
 1228                 {5, 5},
 1229                 {7, 7}
 1230                 }
 1231         },
 1232         {0, 1, 3, 0,            /* 0xa6 */
 1233                 {{1, 2},
 1234                 {5, 5},
 1235                 {7, 7},
 1236                 {0, 0}
 1237                 }
 1238         },
 1239         {1, 1, 3, 0,            /* 0xa7 */
 1240                 {{0, 2},
 1241                 {5, 5},
 1242                 {7, 7},
 1243                 {0, 0}
 1244                 }
 1245         },
 1246         {0, 1, 3, 0,            /* 0xa8 */
 1247                 {{3, 3},
 1248                 {5, 5},
 1249                 {7, 7},
 1250                 {0, 0}
 1251                 }
 1252         },
 1253         {1, 1, 4, 0,            /* 0xa9 */
 1254                 {{0, 0},
 1255                 {3, 3},
 1256                 {5, 5},
 1257                 {7, 7}
 1258                 }
 1259         },
 1260         {0, 1, 4, 0,            /* 0xaa */
 1261                 {{1, 1},
 1262                 {3, 3},
 1263                 {5, 5},
 1264                 {7, 7}
 1265                 }
 1266         },
 1267         {1, 1, 4, 0,            /* 0xab */
 1268                 {{0, 1},
 1269                 {3, 3},
 1270                 {5, 5},
 1271                 {7, 7}
 1272                 }
 1273         },
 1274         {0, 1, 3, 0,            /* 0xac */
 1275                 {{2, 3},
 1276                 {5, 5},
 1277                 {7, 7},
 1278                 {0, 0}
 1279                 }
 1280         },
 1281         {1, 1, 4, 0,            /* 0xad */
 1282                 {{0, 0},
 1283                 {2, 3},
 1284                 {5, 5},
 1285                 {7, 7}
 1286                 }
 1287         },
 1288         {0, 1, 3, 0,            /* 0xae */
 1289                 {{1, 3},
 1290                 {5, 5},
 1291                 {7, 7},
 1292                 {0, 0}
 1293                 }
 1294         },
 1295         {1, 1, 3, 0,            /* 0xaf */
 1296                 {{0, 3},
 1297                 {5, 5},
 1298                 {7, 7},
 1299                 {0, 0}
 1300                 }
 1301         },
 1302         {0, 1, 2, 0,            /* 0xb0 */
 1303                 {{4, 5},
 1304                 {7, 7},
 1305                 {0, 0},
 1306                 {0, 0}
 1307                 }
 1308         },
 1309         {1, 1, 3, 0,            /* 0xb1 */
 1310                 {{0, 0},
 1311                 {4, 5},
 1312                 {7, 7},
 1313                 {0, 0}
 1314                 }
 1315         },
 1316         {0, 1, 3, 0,            /* 0xb2 */
 1317                 {{1, 1},
 1318                 {4, 5},
 1319                 {7, 7},
 1320                 {0, 0}
 1321                 }
 1322         },
 1323         {1, 1, 3, 0,            /* 0xb3 */
 1324                 {{0, 1},
 1325                 {4, 5},
 1326                 {7, 7},
 1327                 {0, 0}
 1328                 }
 1329         },
 1330         {0, 1, 3, 0,            /* 0xb4 */
 1331                 {{2, 2},
 1332                 {4, 5},
 1333                 {7, 7},
 1334                 {0, 0}
 1335                 }
 1336         },
 1337         {1, 1, 4, 0,            /* 0xb5 */
 1338                 {{0, 0},
 1339                 {2, 2},
 1340                 {4, 5},
 1341                 {7, 7}
 1342                 }
 1343         },
 1344         {0, 1, 3, 0,            /* 0xb6 */
 1345                 {{1, 2},
 1346                 {4, 5},
 1347                 {7, 7},
 1348                 {0, 0}
 1349                 }
 1350         },
 1351         {1, 1, 3, 0,            /* 0xb7 */
 1352                 {{0, 2},
 1353                 {4, 5},
 1354                 {7, 7},
 1355                 {0, 0}
 1356                 }
 1357         },
 1358         {0, 1, 2, 0,            /* 0xb8 */
 1359                 {{3, 5},
 1360                 {7, 7},
 1361                 {0, 0},
 1362                 {0, 0}
 1363                 }
 1364         },
 1365         {1, 1, 3, 0,            /* 0xb9 */
 1366                 {{0, 0},
 1367                 {3, 5},
 1368                 {7, 7},
 1369                 {0, 0}
 1370                 }
 1371         },
 1372         {0, 1, 3, 0,            /* 0xba */
 1373                 {{1, 1},
 1374                 {3, 5},
 1375                 {7, 7},
 1376                 {0, 0}
 1377                 }
 1378         },
 1379         {1, 1, 3, 0,            /* 0xbb */
 1380                 {{0, 1},
 1381                 {3, 5},
 1382                 {7, 7},
 1383                 {0, 0}
 1384                 }
 1385         },
 1386         {0, 1, 2, 0,            /* 0xbc */
 1387                 {{2, 5},
 1388                 {7, 7},
 1389                 {0, 0},
 1390                 {0, 0}
 1391                 }
 1392         },
 1393         {1, 1, 3, 0,            /* 0xbd */
 1394                 {{0, 0},
 1395                 {2, 5},
 1396                 {7, 7},
 1397                 {0, 0}
 1398                 }
 1399         },
 1400         {0, 1, 2, 0,            /* 0xbe */
 1401                 {{1, 5},
 1402                 {7, 7},
 1403                 {0, 0},
 1404                 {0, 0}
 1405                 }
 1406         },
 1407         {1, 1, 2, 0,            /* 0xbf */
 1408                 {{0, 5},
 1409                 {7, 7},
 1410                 {0, 0},
 1411                 {0, 0}
 1412                 }
 1413         },
 1414         {0, 1, 1, 0,            /* 0xc0 */
 1415                 {{6, 7},
 1416                 {0, 0},
 1417                 {0, 0},
 1418                 {0, 0}
 1419                 }
 1420         },
 1421         {1, 1, 2, 0,            /* 0xc1 */
 1422                 {{0, 0},
 1423                 {6, 7},
 1424                 {0, 0},
 1425                 {0, 0}
 1426                 }
 1427         },
 1428         {0, 1, 2, 0,            /* 0xc2 */
 1429                 {{1, 1},
 1430                 {6, 7},
 1431                 {0, 0},
 1432                 {0, 0}
 1433                 }
 1434         },
 1435         {1, 1, 2, 0,            /* 0xc3 */
 1436                 {{0, 1},
 1437                 {6, 7},
 1438                 {0, 0},
 1439                 {0, 0}
 1440                 }
 1441         },
 1442         {0, 1, 2, 0,            /* 0xc4 */
 1443                 {{2, 2},
 1444                 {6, 7},
 1445                 {0, 0},
 1446                 {0, 0}
 1447                 }
 1448         },
 1449         {1, 1, 3, 0,            /* 0xc5 */
 1450                 {{0, 0},
 1451                 {2, 2},
 1452                 {6, 7},
 1453                 {0, 0}
 1454                 }
 1455         },
 1456         {0, 1, 2, 0,            /* 0xc6 */
 1457                 {{1, 2},
 1458                 {6, 7},
 1459                 {0, 0},
 1460                 {0, 0}
 1461                 }
 1462         },
 1463         {1, 1, 2, 0,            /* 0xc7 */
 1464                 {{0, 2},
 1465                 {6, 7},
 1466                 {0, 0},
 1467                 {0, 0}
 1468                 }
 1469         },
 1470         {0, 1, 2, 0,            /* 0xc8 */
 1471                 {{3, 3},
 1472                 {6, 7},
 1473                 {0, 0},
 1474                 {0, 0}
 1475                 }
 1476         },
 1477         {1, 1, 3, 0,            /* 0xc9 */
 1478                 {{0, 0},
 1479                 {3, 3},
 1480                 {6, 7},
 1481                 {0, 0}
 1482                 }
 1483         },
 1484         {0, 1, 3, 0,            /* 0xca */
 1485                 {{1, 1},
 1486                 {3, 3},
 1487                 {6, 7},
 1488                 {0, 0}
 1489                 }
 1490         },
 1491         {1, 1, 3, 0,            /* 0xcb */
 1492                 {{0, 1},
 1493                 {3, 3},
 1494                 {6, 7},
 1495                 {0, 0}
 1496                 }
 1497         },
 1498         {0, 1, 2, 0,            /* 0xcc */
 1499                 {{2, 3},
 1500                 {6, 7},
 1501                 {0, 0},
 1502                 {0, 0}
 1503                 }
 1504         },
 1505         {1, 1, 3, 0,            /* 0xcd */
 1506                 {{0, 0},
 1507                 {2, 3},
 1508                 {6, 7},
 1509                 {0, 0}
 1510                 }
 1511         },
 1512         {0, 1, 2, 0,            /* 0xce */
 1513                 {{1, 3},
 1514                 {6, 7},
 1515                 {0, 0},
 1516                 {0, 0}
 1517                 }
 1518         },
 1519         {1, 1, 2, 0,            /* 0xcf */
 1520                 {{0, 3},
 1521                 {6, 7},
 1522                 {0, 0},
 1523                 {0, 0}
 1524                 }
 1525         },
 1526         {0, 1, 2, 0,            /* 0xd0 */
 1527                 {{4, 4},
 1528                 {6, 7},
 1529                 {0, 0},
 1530                 {0, 0}
 1531                 }
 1532         },
 1533         {1, 1, 3, 0,            /* 0xd1 */
 1534                 {{0, 0},
 1535                 {4, 4},
 1536                 {6, 7},
 1537                 {0, 0}
 1538                 }
 1539         },
 1540         {0, 1, 3, 0,            /* 0xd2 */
 1541                 {{1, 1},
 1542                 {4, 4},
 1543                 {6, 7},
 1544                 {0, 0}
 1545                 }
 1546         },
 1547         {1, 1, 3, 0,            /* 0xd3 */
 1548                 {{0, 1},
 1549                 {4, 4},
 1550                 {6, 7},
 1551                 {0, 0}
 1552                 }
 1553         },
 1554         {0, 1, 3, 0,            /* 0xd4 */
 1555                 {{2, 2},
 1556                 {4, 4},
 1557                 {6, 7},
 1558                 {0, 0}
 1559                 }
 1560         },
 1561         {1, 1, 4, 0,            /* 0xd5 */
 1562                 {{0, 0},
 1563                 {2, 2},
 1564                 {4, 4},
 1565                 {6, 7}
 1566                 }
 1567         },
 1568         {0, 1, 3, 0,            /* 0xd6 */
 1569                 {{1, 2},
 1570                 {4, 4},
 1571                 {6, 7},
 1572                 {0, 0}
 1573                 }
 1574         },
 1575         {1, 1, 3, 0,            /* 0xd7 */
 1576                 {{0, 2},
 1577                 {4, 4},
 1578                 {6, 7},
 1579                 {0, 0}
 1580                 }
 1581         },
 1582         {0, 1, 2, 0,            /* 0xd8 */
 1583                 {{3, 4},
 1584                 {6, 7},
 1585                 {0, 0},
 1586                 {0, 0}
 1587                 }
 1588         },
 1589         {1, 1, 3, 0,            /* 0xd9 */
 1590                 {{0, 0},
 1591                 {3, 4},
 1592                 {6, 7},
 1593                 {0, 0}
 1594                 }
 1595         },
 1596         {0, 1, 3, 0,            /* 0xda */
 1597                 {{1, 1},
 1598                 {3, 4},
 1599                 {6, 7},
 1600                 {0, 0}
 1601                 }
 1602         },
 1603         {1, 1, 3, 0,            /* 0xdb */
 1604                 {{0, 1},
 1605                 {3, 4},
 1606                 {6, 7},
 1607                 {0, 0}
 1608                 }
 1609         },
 1610         {0, 1, 2, 0,            /* 0xdc */
 1611                 {{2, 4},
 1612                 {6, 7},
 1613                 {0, 0},
 1614                 {0, 0}
 1615                 }
 1616         },
 1617         {1, 1, 3, 0,            /* 0xdd */
 1618                 {{0, 0},
 1619                 {2, 4},
 1620                 {6, 7},
 1621                 {0, 0}
 1622                 }
 1623         },
 1624         {0, 1, 2, 0,            /* 0xde */
 1625                 {{1, 4},
 1626                 {6, 7},
 1627                 {0, 0},
 1628                 {0, 0}
 1629                 }
 1630         },
 1631         {1, 1, 2, 0,            /* 0xdf */
 1632                 {{0, 4},
 1633                 {6, 7},
 1634                 {0, 0},
 1635                 {0, 0}
 1636                 }
 1637         },
 1638         {0, 1, 1, 0,            /* 0xe0 */
 1639                 {{5, 7},
 1640                 {0, 0},
 1641                 {0, 0},
 1642                 {0, 0}
 1643                 }
 1644         },
 1645         {1, 1, 2, 0,            /* 0xe1 */
 1646                 {{0, 0},
 1647                 {5, 7},
 1648                 {0, 0},
 1649                 {0, 0}
 1650                 }
 1651         },
 1652         {0, 1, 2, 0,            /* 0xe2 */
 1653                 {{1, 1},
 1654                 {5, 7},
 1655                 {0, 0},
 1656                 {0, 0}
 1657                 }
 1658         },
 1659         {1, 1, 2, 0,            /* 0xe3 */
 1660                 {{0, 1},
 1661                 {5, 7},
 1662                 {0, 0},
 1663                 {0, 0}
 1664                 }
 1665         },
 1666         {0, 1, 2, 0,            /* 0xe4 */
 1667                 {{2, 2},
 1668                 {5, 7},
 1669                 {0, 0},
 1670                 {0, 0}
 1671                 }
 1672         },
 1673         {1, 1, 3, 0,            /* 0xe5 */
 1674                 {{0, 0},
 1675                 {2, 2},
 1676                 {5, 7},
 1677                 {0, 0}
 1678                 }
 1679         },
 1680         {0, 1, 2, 0,            /* 0xe6 */
 1681                 {{1, 2},
 1682                 {5, 7},
 1683                 {0, 0},
 1684                 {0, 0}
 1685                 }
 1686         },
 1687         {1, 1, 2, 0,            /* 0xe7 */
 1688                 {{0, 2},
 1689                 {5, 7},
 1690                 {0, 0},
 1691                 {0, 0}
 1692                 }
 1693         },
 1694         {0, 1, 2, 0,            /* 0xe8 */
 1695                 {{3, 3},
 1696                 {5, 7},
 1697                 {0, 0},
 1698                 {0, 0}
 1699                 }
 1700         },
 1701         {1, 1, 3, 0,            /* 0xe9 */
 1702                 {{0, 0},
 1703                 {3, 3},
 1704                 {5, 7},
 1705                 {0, 0}
 1706                 }
 1707         },
 1708         {0, 1, 3, 0,            /* 0xea */
 1709                 {{1, 1},
 1710                 {3, 3},
 1711                 {5, 7},
 1712                 {0, 0}
 1713                 }
 1714         },
 1715         {1, 1, 3, 0,            /* 0xeb */
 1716                 {{0, 1},
 1717                 {3, 3},
 1718                 {5, 7},
 1719                 {0, 0}
 1720                 }
 1721         },
 1722         {0, 1, 2, 0,            /* 0xec */
 1723                 {{2, 3},
 1724                 {5, 7},
 1725                 {0, 0},
 1726                 {0, 0}
 1727                 }
 1728         },
 1729         {1, 1, 3, 0,            /* 0xed */
 1730                 {{0, 0},
 1731                 {2, 3},
 1732                 {5, 7},
 1733                 {0, 0}
 1734                 }
 1735         },
 1736         {0, 1, 2, 0,            /* 0xee */
 1737                 {{1, 3},
 1738                 {5, 7},
 1739                 {0, 0},
 1740                 {0, 0}
 1741                 }
 1742         },
 1743         {1, 1, 2, 0,            /* 0xef */
 1744                 {{0, 3},
 1745                 {5, 7},
 1746                 {0, 0},
 1747                 {0, 0}
 1748                 }
 1749         },
 1750         {0, 1, 1, 0,            /* 0xf0 */
 1751                 {{4, 7},
 1752                 {0, 0},
 1753                 {0, 0},
 1754                 {0, 0}
 1755                 }
 1756         },
 1757         {1, 1, 2, 0,            /* 0xf1 */
 1758                 {{0, 0},
 1759                 {4, 7},
 1760                 {0, 0},
 1761                 {0, 0}
 1762                 }
 1763         },
 1764         {0, 1, 2, 0,            /* 0xf2 */
 1765                 {{1, 1},
 1766                 {4, 7},
 1767                 {0, 0},
 1768                 {0, 0}
 1769                 }
 1770         },
 1771         {1, 1, 2, 0,            /* 0xf3 */
 1772                 {{0, 1},
 1773                 {4, 7},
 1774                 {0, 0},
 1775                 {0, 0}
 1776                 }
 1777         },
 1778         {0, 1, 2, 0,            /* 0xf4 */
 1779                 {{2, 2},
 1780                 {4, 7},
 1781                 {0, 0},
 1782                 {0, 0}
 1783                 }
 1784         },
 1785         {1, 1, 3, 0,            /* 0xf5 */
 1786                 {{0, 0},
 1787                 {2, 2},
 1788                 {4, 7},
 1789                 {0, 0}
 1790                 }
 1791         },
 1792         {0, 1, 2, 0,            /* 0xf6 */
 1793                 {{1, 2},
 1794                 {4, 7},
 1795                 {0, 0},
 1796                 {0, 0}
 1797                 }
 1798         },
 1799         {1, 1, 2, 0,            /* 0xf7 */
 1800                 {{0, 2},
 1801                 {4, 7},
 1802                 {0, 0},
 1803                 {0, 0}
 1804                 }
 1805         },
 1806         {0, 1, 1, 0,            /* 0xf8 */
 1807                 {{3, 7},
 1808                 {0, 0},
 1809                 {0, 0},
 1810                 {0, 0}
 1811                 }
 1812         },
 1813         {1, 1, 2, 0,            /* 0xf9 */
 1814                 {{0, 0},
 1815                 {3, 7},
 1816                 {0, 0},
 1817                 {0, 0}
 1818                 }
 1819         },
 1820         {0, 1, 2, 0,            /* 0xfa */
 1821                 {{1, 1},
 1822                 {3, 7},
 1823                 {0, 0},
 1824                 {0, 0}
 1825                 }
 1826         },
 1827         {1, 1, 2, 0,            /* 0xfb */
 1828                 {{0, 1},
 1829                 {3, 7},
 1830                 {0, 0},
 1831                 {0, 0}
 1832                 }
 1833         },
 1834         {0, 1, 1, 0,            /* 0xfc */
 1835                 {{2, 7},
 1836                 {0, 0},
 1837                 {0, 0},
 1838                 {0, 0}
 1839                 }
 1840         },
 1841         {1, 1, 2, 0,            /* 0xfd */
 1842                 {{0, 0},
 1843                 {2, 7},
 1844                 {0, 0},
 1845                 {0, 0}
 1846                 }
 1847         },
 1848         {0, 1, 1, 0,            /* 0xfe */
 1849                 {{1, 7},
 1850                 {0, 0},
 1851                 {0, 0},
 1852                 {0, 0}
 1853                 }
 1854         },
 1855         {1, 1, 1, 0,            /* 0xff */
 1856                 {{0, 7},
 1857                 {0, 0},
 1858                 {0, 0},
 1859                 {0, 0}
 1860                 }
 1861         }
 1862 };
 1863 
 1864 
 1865 int
 1866 sctp_is_address_in_scope(struct sctp_ifa *ifa,
 1867     int ipv4_addr_legal,
 1868     int ipv6_addr_legal,
 1869     int loopback_scope,
 1870     int ipv4_local_scope,
 1871     int local_scope SCTP_UNUSED,/* XXX */
 1872     int site_scope,
 1873     int do_update)
 1874 {
 1875         if ((loopback_scope == 0) &&
 1876             (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
 1877                 /*
 1878                  * skip loopback if not in scope *
 1879                  */
 1880                 return (0);
 1881         }
 1882         switch (ifa->address.sa.sa_family) {
 1883 #ifdef INET
 1884         case AF_INET:
 1885                 if (ipv4_addr_legal) {
 1886                         struct sockaddr_in *sin;
 1887 
 1888                         sin = (struct sockaddr_in *)&ifa->address.sin;
 1889                         if (sin->sin_addr.s_addr == 0) {
 1890                                 /* not in scope , unspecified */
 1891                                 return (0);
 1892                         }
 1893                         if ((ipv4_local_scope == 0) &&
 1894                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
 1895                                 /* private address not in scope */
 1896                                 return (0);
 1897                         }
 1898                 } else {
 1899                         return (0);
 1900                 }
 1901                 break;
 1902 #endif
 1903 #ifdef INET6
 1904         case AF_INET6:
 1905                 if (ipv6_addr_legal) {
 1906                         struct sockaddr_in6 *sin6;
 1907 
 1908                         /*
 1909                          * Must update the flags,  bummer, which means any
 1910                          * IFA locks must now be applied HERE <->
 1911                          */
 1912                         if (do_update) {
 1913                                 sctp_gather_internal_ifa_flags(ifa);
 1914                         }
 1915                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
 1916                                 return (0);
 1917                         }
 1918                         /* ok to use deprecated addresses? */
 1919                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
 1920                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 1921                                 /* skip unspecifed addresses */
 1922                                 return (0);
 1923                         }
 1924                         if (    /* (local_scope == 0) && */
 1925                             (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
 1926                                 return (0);
 1927                         }
 1928                         if ((site_scope == 0) &&
 1929                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
 1930                                 return (0);
 1931                         }
 1932                 } else {
 1933                         return (0);
 1934                 }
 1935                 break;
 1936 #endif
 1937         default:
 1938                 return (0);
 1939         }
 1940         return (1);
 1941 }
 1942 
 1943 static struct mbuf *
 1944 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
 1945 {
 1946         struct sctp_paramhdr *parmh;
 1947         struct mbuf *mret;
 1948         int len;
 1949 
 1950         switch (ifa->address.sa.sa_family) {
 1951 #ifdef INET
 1952         case AF_INET:
 1953                 len = sizeof(struct sctp_ipv4addr_param);
 1954                 break;
 1955 #endif
 1956 #ifdef INET6
 1957         case AF_INET6:
 1958                 len = sizeof(struct sctp_ipv6addr_param);
 1959                 break;
 1960 #endif
 1961         default:
 1962                 return (m);
 1963         }
 1964         if (M_TRAILINGSPACE(m) >= len) {
 1965                 /* easy side we just drop it on the end */
 1966                 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
 1967                 mret = m;
 1968         } else {
 1969                 /* Need more space */
 1970                 mret = m;
 1971                 while (SCTP_BUF_NEXT(mret) != NULL) {
 1972                         mret = SCTP_BUF_NEXT(mret);
 1973                 }
 1974                 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
 1975                 if (SCTP_BUF_NEXT(mret) == NULL) {
 1976                         /* We are hosed, can't add more addresses */
 1977                         return (m);
 1978                 }
 1979                 mret = SCTP_BUF_NEXT(mret);
 1980                 parmh = mtod(mret, struct sctp_paramhdr *);
 1981         }
 1982         /* now add the parameter */
 1983         switch (ifa->address.sa.sa_family) {
 1984 #ifdef INET
 1985         case AF_INET:
 1986                 {
 1987                         struct sctp_ipv4addr_param *ipv4p;
 1988                         struct sockaddr_in *sin;
 1989 
 1990                         sin = (struct sockaddr_in *)&ifa->address.sin;
 1991                         ipv4p = (struct sctp_ipv4addr_param *)parmh;
 1992                         parmh->param_type = htons(SCTP_IPV4_ADDRESS);
 1993                         parmh->param_length = htons(len);
 1994                         ipv4p->addr = sin->sin_addr.s_addr;
 1995                         SCTP_BUF_LEN(mret) += len;
 1996                         break;
 1997                 }
 1998 #endif
 1999 #ifdef INET6
 2000         case AF_INET6:
 2001                 {
 2002                         struct sctp_ipv6addr_param *ipv6p;
 2003                         struct sockaddr_in6 *sin6;
 2004 
 2005                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
 2006                         ipv6p = (struct sctp_ipv6addr_param *)parmh;
 2007                         parmh->param_type = htons(SCTP_IPV6_ADDRESS);
 2008                         parmh->param_length = htons(len);
 2009                         memcpy(ipv6p->addr, &sin6->sin6_addr,
 2010                             sizeof(ipv6p->addr));
 2011                         /* clear embedded scope in the address */
 2012                         in6_clearscope((struct in6_addr *)ipv6p->addr);
 2013                         SCTP_BUF_LEN(mret) += len;
 2014                         break;
 2015                 }
 2016 #endif
 2017         default:
 2018                 return (m);
 2019         }
 2020         return (mret);
 2021 }
 2022 
 2023 
 2024 struct mbuf *
 2025 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
 2026     struct sctp_scoping *scope,
 2027     struct mbuf *m_at, int cnt_inits_to)
 2028 {
 2029         struct sctp_vrf *vrf = NULL;
 2030         int cnt, limit_out = 0, total_count;
 2031         uint32_t vrf_id;
 2032 
 2033         vrf_id = inp->def_vrf_id;
 2034         SCTP_IPI_ADDR_RLOCK();
 2035         vrf = sctp_find_vrf(vrf_id);
 2036         if (vrf == NULL) {
 2037                 SCTP_IPI_ADDR_RUNLOCK();
 2038                 return (m_at);
 2039         }
 2040         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 2041                 struct sctp_ifa *sctp_ifap;
 2042                 struct sctp_ifn *sctp_ifnp;
 2043 
 2044                 cnt = cnt_inits_to;
 2045                 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
 2046                         limit_out = 1;
 2047                         cnt = SCTP_ADDRESS_LIMIT;
 2048                         goto skip_count;
 2049                 }
 2050                 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
 2051                         if ((scope->loopback_scope == 0) &&
 2052                             SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
 2053                                 /*
 2054                                  * Skip loopback devices if loopback_scope
 2055                                  * not set
 2056                                  */
 2057                                 continue;
 2058                         }
 2059                         LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
 2060                                 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
 2061                                         continue;
 2062                                 }
 2063                                 if (sctp_is_address_in_scope(sctp_ifap,
 2064                                     scope->ipv4_addr_legal,
 2065                                     scope->ipv6_addr_legal,
 2066                                     scope->loopback_scope,
 2067                                     scope->ipv4_local_scope,
 2068                                     scope->local_scope,
 2069                                     scope->site_scope, 1) == 0) {
 2070                                         continue;
 2071                                 }
 2072                                 cnt++;
 2073                                 if (cnt > SCTP_ADDRESS_LIMIT) {
 2074                                         break;
 2075                                 }
 2076                         }
 2077                         if (cnt > SCTP_ADDRESS_LIMIT) {
 2078                                 break;
 2079                         }
 2080                 }
 2081 skip_count:
 2082                 if (cnt > 1) {
 2083                         total_count = 0;
 2084                         LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
 2085                                 cnt = 0;
 2086                                 if ((scope->loopback_scope == 0) &&
 2087                                     SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
 2088                                         /*
 2089                                          * Skip loopback devices if
 2090                                          * loopback_scope not set
 2091                                          */
 2092                                         continue;
 2093                                 }
 2094                                 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
 2095                                         if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
 2096                                                 continue;
 2097                                         }
 2098                                         if (sctp_is_address_in_scope(sctp_ifap,
 2099                                             scope->ipv4_addr_legal,
 2100                                             scope->ipv6_addr_legal,
 2101                                             scope->loopback_scope,
 2102                                             scope->ipv4_local_scope,
 2103                                             scope->local_scope,
 2104                                             scope->site_scope, 0) == 0) {
 2105                                                 continue;
 2106                                         }
 2107                                         m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
 2108                                         if (limit_out) {
 2109                                                 cnt++;
 2110                                                 total_count++;
 2111                                                 if (cnt >= 2) {
 2112                                                         /*
 2113                                                          * two from each
 2114                                                          * address
 2115                                                          */
 2116                                                         break;
 2117                                                 }
 2118                                                 if (total_count > SCTP_ADDRESS_LIMIT) {
 2119                                                         /* No more addresses */
 2120                                                         break;
 2121                                                 }
 2122                                         }
 2123                                 }
 2124                         }
 2125                 }
 2126         } else {
 2127                 struct sctp_laddr *laddr;
 2128 
 2129                 cnt = cnt_inits_to;
 2130                 /* First, how many ? */
 2131                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 2132                         if (laddr->ifa == NULL) {
 2133                                 continue;
 2134                         }
 2135                         if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
 2136                                 /*
 2137                                  * Address being deleted by the system, dont
 2138                                  * list.
 2139                                  */
 2140                                 continue;
 2141                         if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 2142                                 /*
 2143                                  * Address being deleted on this ep don't
 2144                                  * list.
 2145                                  */
 2146                                 continue;
 2147                         }
 2148                         if (sctp_is_address_in_scope(laddr->ifa,
 2149                             scope->ipv4_addr_legal,
 2150                             scope->ipv6_addr_legal,
 2151                             scope->loopback_scope,
 2152                             scope->ipv4_local_scope,
 2153                             scope->local_scope,
 2154                             scope->site_scope, 1) == 0) {
 2155                                 continue;
 2156                         }
 2157                         cnt++;
 2158                 }
 2159                 /*
 2160                  * To get through a NAT we only list addresses if we have
 2161                  * more than one. That way if you just bind a single address
 2162                  * we let the source of the init dictate our address.
 2163                  */
 2164                 if (cnt > 1) {
 2165                         cnt = cnt_inits_to;
 2166                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 2167                                 if (laddr->ifa == NULL) {
 2168                                         continue;
 2169                                 }
 2170                                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
 2171                                         continue;
 2172                                 }
 2173                                 if (sctp_is_address_in_scope(laddr->ifa,
 2174                                     scope->ipv4_addr_legal,
 2175                                     scope->ipv6_addr_legal,
 2176                                     scope->loopback_scope,
 2177                                     scope->ipv4_local_scope,
 2178                                     scope->local_scope,
 2179                                     scope->site_scope, 0) == 0) {
 2180                                         continue;
 2181                                 }
 2182                                 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
 2183                                 cnt++;
 2184                                 if (cnt >= SCTP_ADDRESS_LIMIT) {
 2185                                         break;
 2186                                 }
 2187                         }
 2188                 }
 2189         }
 2190         SCTP_IPI_ADDR_RUNLOCK();
 2191         return (m_at);
 2192 }
 2193 
 2194 static struct sctp_ifa *
 2195 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
 2196     uint8_t dest_is_loop,
 2197     uint8_t dest_is_priv,
 2198     sa_family_t fam)
 2199 {
 2200         uint8_t dest_is_global = 0;
 2201 
 2202         /* dest_is_priv is true if destination is a private address */
 2203         /* dest_is_loop is true if destination is a loopback addresses */
 2204 
 2205         /**
 2206          * Here we determine if its a preferred address. A preferred address
 2207          * means it is the same scope or higher scope then the destination.
 2208          * L = loopback, P = private, G = global
 2209          * -----------------------------------------
 2210          *    src    |  dest | result
 2211          *  ----------------------------------------
 2212          *     L     |    L  |    yes
 2213          *  -----------------------------------------
 2214          *     P     |    L  |    yes-v4 no-v6
 2215          *  -----------------------------------------
 2216          *     G     |    L  |    yes-v4 no-v6
 2217          *  -----------------------------------------
 2218          *     L     |    P  |    no
 2219          *  -----------------------------------------
 2220          *     P     |    P  |    yes
 2221          *  -----------------------------------------
 2222          *     G     |    P  |    no
 2223          *   -----------------------------------------
 2224          *     L     |    G  |    no
 2225          *   -----------------------------------------
 2226          *     P     |    G  |    no
 2227          *    -----------------------------------------
 2228          *     G     |    G  |    yes
 2229          *    -----------------------------------------
 2230          */
 2231 
 2232         if (ifa->address.sa.sa_family != fam) {
 2233                 /* forget mis-matched family */
 2234                 return (NULL);
 2235         }
 2236         if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
 2237                 dest_is_global = 1;
 2238         }
 2239         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
 2240         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
 2241         /* Ok the address may be ok */
 2242 #ifdef INET6
 2243         if (fam == AF_INET6) {
 2244                 /* ok to use deprecated addresses? no lets not! */
 2245                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
 2246                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
 2247                         return (NULL);
 2248                 }
 2249                 if (ifa->src_is_priv && !ifa->src_is_loop) {
 2250                         if (dest_is_loop) {
 2251                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
 2252                                 return (NULL);
 2253                         }
 2254                 }
 2255                 if (ifa->src_is_glob) {
 2256                         if (dest_is_loop) {
 2257                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
 2258                                 return (NULL);
 2259                         }
 2260                 }
 2261         }
 2262 #endif
 2263         /*
 2264          * Now that we know what is what, implement or table this could in
 2265          * theory be done slicker (it used to be), but this is
 2266          * straightforward and easier to validate :-)
 2267          */
 2268         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
 2269             ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
 2270         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
 2271             dest_is_loop, dest_is_priv, dest_is_global);
 2272 
 2273         if ((ifa->src_is_loop) && (dest_is_priv)) {
 2274                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
 2275                 return (NULL);
 2276         }
 2277         if ((ifa->src_is_glob) && (dest_is_priv)) {
 2278                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
 2279                 return (NULL);
 2280         }
 2281         if ((ifa->src_is_loop) && (dest_is_global)) {
 2282                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
 2283                 return (NULL);
 2284         }
 2285         if ((ifa->src_is_priv) && (dest_is_global)) {
 2286                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
 2287                 return (NULL);
 2288         }
 2289         SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
 2290         /* its a preferred address */
 2291         return (ifa);
 2292 }
 2293 
 2294 static struct sctp_ifa *
 2295 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
 2296     uint8_t dest_is_loop,
 2297     uint8_t dest_is_priv,
 2298     sa_family_t fam)
 2299 {
 2300         uint8_t dest_is_global = 0;
 2301 
 2302         /**
 2303          * Here we determine if its a acceptable address. A acceptable
 2304          * address means it is the same scope or higher scope but we can
 2305          * allow for NAT which means its ok to have a global dest and a
 2306          * private src.
 2307          *
 2308          * L = loopback, P = private, G = global
 2309          * -----------------------------------------
 2310          *  src    |  dest | result
 2311          * -----------------------------------------
 2312          *   L     |   L   |    yes
 2313          *  -----------------------------------------
 2314          *   P     |   L   |    yes-v4 no-v6
 2315          *  -----------------------------------------
 2316          *   G     |   L   |    yes
 2317          * -----------------------------------------
 2318          *   L     |   P   |    no
 2319          * -----------------------------------------
 2320          *   P     |   P   |    yes
 2321          * -----------------------------------------
 2322          *   G     |   P   |    yes - May not work
 2323          * -----------------------------------------
 2324          *   L     |   G   |    no
 2325          * -----------------------------------------
 2326          *   P     |   G   |    yes - May not work
 2327          * -----------------------------------------
 2328          *   G     |   G   |    yes
 2329          * -----------------------------------------
 2330          */
 2331 
 2332         if (ifa->address.sa.sa_family != fam) {
 2333                 /* forget non matching family */
 2334                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
 2335                     ifa->address.sa.sa_family, fam);
 2336                 return (NULL);
 2337         }
 2338         /* Ok the address may be ok */
 2339         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
 2340         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
 2341             dest_is_loop, dest_is_priv);
 2342         if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
 2343                 dest_is_global = 1;
 2344         }
 2345 #ifdef INET6
 2346         if (fam == AF_INET6) {
 2347                 /* ok to use deprecated addresses? */
 2348                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
 2349                         return (NULL);
 2350                 }
 2351                 if (ifa->src_is_priv) {
 2352                         /* Special case, linklocal to loop */
 2353                         if (dest_is_loop)
 2354                                 return (NULL);
 2355                 }
 2356         }
 2357 #endif
 2358         /*
 2359          * Now that we know what is what, implement our table. This could in
 2360          * theory be done slicker (it used to be), but this is
 2361          * straightforward and easier to validate :-)
 2362          */
 2363         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
 2364             ifa->src_is_loop,
 2365             dest_is_priv);
 2366         if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
 2367                 return (NULL);
 2368         }
 2369         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
 2370             ifa->src_is_loop,
 2371             dest_is_global);
 2372         if ((ifa->src_is_loop == 1) && (dest_is_global)) {
 2373                 return (NULL);
 2374         }
 2375         SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
 2376         /* its an acceptable address */
 2377         return (ifa);
 2378 }
 2379 
 2380 int
 2381 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
 2382 {
 2383         struct sctp_laddr *laddr;
 2384 
 2385         if (stcb == NULL) {
 2386                 /* There are no restrictions, no TCB :-) */
 2387                 return (0);
 2388         }
 2389         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
 2390                 if (laddr->ifa == NULL) {
 2391                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
 2392                             __FUNCTION__);
 2393                         continue;
 2394                 }
 2395                 if (laddr->ifa == ifa) {
 2396                         /* Yes it is on the list */
 2397                         return (1);
 2398                 }
 2399         }
 2400         return (0);
 2401 }
 2402 
 2403 
 2404 int
 2405 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
 2406 {
 2407         struct sctp_laddr *laddr;
 2408 
 2409         if (ifa == NULL)
 2410                 return (0);
 2411         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 2412                 if (laddr->ifa == NULL) {
 2413                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
 2414                             __FUNCTION__);
 2415                         continue;
 2416                 }
 2417                 if ((laddr->ifa == ifa) && laddr->action == 0)
 2418                         /* same pointer */
 2419                         return (1);
 2420         }
 2421         return (0);
 2422 }
 2423 
 2424 
 2425 
 2426 static struct sctp_ifa *
 2427 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
 2428     sctp_route_t * ro,
 2429     uint32_t vrf_id,
 2430     int non_asoc_addr_ok,
 2431     uint8_t dest_is_priv,
 2432     uint8_t dest_is_loop,
 2433     sa_family_t fam)
 2434 {
 2435         struct sctp_laddr *laddr, *starting_point;
 2436         void *ifn;
 2437         int resettotop = 0;
 2438         struct sctp_ifn *sctp_ifn;
 2439         struct sctp_ifa *sctp_ifa, *sifa;
 2440         struct sctp_vrf *vrf;
 2441         uint32_t ifn_index;
 2442 
 2443         vrf = sctp_find_vrf(vrf_id);
 2444         if (vrf == NULL)
 2445                 return (NULL);
 2446 
 2447         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 2448         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
 2449         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
 2450         /*
 2451          * first question, is the ifn we will emit on in our list, if so, we
 2452          * want such an address. Note that we first looked for a preferred
 2453          * address.
 2454          */
 2455         if (sctp_ifn) {
 2456                 /* is a preferred one on the interface we route out? */
 2457                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 2458                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 2459                             (non_asoc_addr_ok == 0))
 2460                                 continue;
 2461                         sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
 2462                             dest_is_loop,
 2463                             dest_is_priv, fam);
 2464                         if (sifa == NULL)
 2465                                 continue;
 2466                         if (sctp_is_addr_in_ep(inp, sifa)) {
 2467                                 atomic_add_int(&sifa->refcount, 1);
 2468                                 return (sifa);
 2469                         }
 2470                 }
 2471         }
 2472         /*
 2473          * ok, now we now need to find one on the list of the addresses. We
 2474          * can't get one on the emitting interface so let's find first a
 2475          * preferred one. If not that an acceptable one otherwise... we
 2476          * return NULL.
 2477          */
 2478         starting_point = inp->next_addr_touse;
 2479 once_again:
 2480         if (inp->next_addr_touse == NULL) {
 2481                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
 2482                 resettotop = 1;
 2483         }
 2484         for (laddr = inp->next_addr_touse; laddr;
 2485             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 2486                 if (laddr->ifa == NULL) {
 2487                         /* address has been removed */
 2488                         continue;
 2489                 }
 2490                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 2491                         /* address is being deleted */
 2492                         continue;
 2493                 }
 2494                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
 2495                     dest_is_priv, fam);
 2496                 if (sifa == NULL)
 2497                         continue;
 2498                 atomic_add_int(&sifa->refcount, 1);
 2499                 return (sifa);
 2500         }
 2501         if (resettotop == 0) {
 2502                 inp->next_addr_touse = NULL;
 2503                 goto once_again;
 2504         }
 2505         inp->next_addr_touse = starting_point;
 2506         resettotop = 0;
 2507 once_again_too:
 2508         if (inp->next_addr_touse == NULL) {
 2509                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
 2510                 resettotop = 1;
 2511         }
 2512         /* ok, what about an acceptable address in the inp */
 2513         for (laddr = inp->next_addr_touse; laddr;
 2514             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 2515                 if (laddr->ifa == NULL) {
 2516                         /* address has been removed */
 2517                         continue;
 2518                 }
 2519                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 2520                         /* address is being deleted */
 2521                         continue;
 2522                 }
 2523                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
 2524                     dest_is_priv, fam);
 2525                 if (sifa == NULL)
 2526                         continue;
 2527                 atomic_add_int(&sifa->refcount, 1);
 2528                 return (sifa);
 2529         }
 2530         if (resettotop == 0) {
 2531                 inp->next_addr_touse = NULL;
 2532                 goto once_again_too;
 2533         }
 2534         /*
 2535          * no address bound can be a source for the destination we are in
 2536          * trouble
 2537          */
 2538         return (NULL);
 2539 }
 2540 
 2541 
 2542 
 2543 static struct sctp_ifa *
 2544 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
 2545     struct sctp_tcb *stcb,
 2546     sctp_route_t * ro,
 2547     uint32_t vrf_id,
 2548     uint8_t dest_is_priv,
 2549     uint8_t dest_is_loop,
 2550     int non_asoc_addr_ok,
 2551     sa_family_t fam)
 2552 {
 2553         struct sctp_laddr *laddr, *starting_point;
 2554         void *ifn;
 2555         struct sctp_ifn *sctp_ifn;
 2556         struct sctp_ifa *sctp_ifa, *sifa;
 2557         uint8_t start_at_beginning = 0;
 2558         struct sctp_vrf *vrf;
 2559         uint32_t ifn_index;
 2560 
 2561         /*
 2562          * first question, is the ifn we will emit on in our list, if so, we
 2563          * want that one.
 2564          */
 2565         vrf = sctp_find_vrf(vrf_id);
 2566         if (vrf == NULL)
 2567                 return (NULL);
 2568 
 2569         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 2570         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
 2571         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
 2572 
 2573         /*
 2574          * first question, is the ifn we will emit on in our list?  If so,
 2575          * we want that one. First we look for a preferred. Second, we go
 2576          * for an acceptable.
 2577          */
 2578         if (sctp_ifn) {
 2579                 /* first try for a preferred address on the ep */
 2580                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 2581                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
 2582                                 continue;
 2583                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
 2584                                 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
 2585                                 if (sifa == NULL)
 2586                                         continue;
 2587                                 if (((non_asoc_addr_ok == 0) &&
 2588                                     (sctp_is_addr_restricted(stcb, sifa))) ||
 2589                                     (non_asoc_addr_ok &&
 2590                                     (sctp_is_addr_restricted(stcb, sifa)) &&
 2591                                     (!sctp_is_addr_pending(stcb, sifa)))) {
 2592                                         /* on the no-no list */
 2593                                         continue;
 2594                                 }
 2595                                 atomic_add_int(&sifa->refcount, 1);
 2596                                 return (sifa);
 2597                         }
 2598                 }
 2599                 /* next try for an acceptable address on the ep */
 2600                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 2601                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
 2602                                 continue;
 2603                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
 2604                                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
 2605                                 if (sifa == NULL)
 2606                                         continue;
 2607                                 if (((non_asoc_addr_ok == 0) &&
 2608                                     (sctp_is_addr_restricted(stcb, sifa))) ||
 2609                                     (non_asoc_addr_ok &&
 2610                                     (sctp_is_addr_restricted(stcb, sifa)) &&
 2611                                     (!sctp_is_addr_pending(stcb, sifa)))) {
 2612                                         /* on the no-no list */
 2613                                         continue;
 2614                                 }
 2615                                 atomic_add_int(&sifa->refcount, 1);
 2616                                 return (sifa);
 2617                         }
 2618                 }
 2619 
 2620         }
 2621         /*
 2622          * if we can't find one like that then we must look at all addresses
 2623          * bound to pick one at first preferable then secondly acceptable.
 2624          */
 2625         starting_point = stcb->asoc.last_used_address;
 2626 sctp_from_the_top:
 2627         if (stcb->asoc.last_used_address == NULL) {
 2628                 start_at_beginning = 1;
 2629                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
 2630         }
 2631         /* search beginning with the last used address */
 2632         for (laddr = stcb->asoc.last_used_address; laddr;
 2633             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 2634                 if (laddr->ifa == NULL) {
 2635                         /* address has been removed */
 2636                         continue;
 2637                 }
 2638                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 2639                         /* address is being deleted */
 2640                         continue;
 2641                 }
 2642                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
 2643                 if (sifa == NULL)
 2644                         continue;
 2645                 if (((non_asoc_addr_ok == 0) &&
 2646                     (sctp_is_addr_restricted(stcb, sifa))) ||
 2647                     (non_asoc_addr_ok &&
 2648                     (sctp_is_addr_restricted(stcb, sifa)) &&
 2649                     (!sctp_is_addr_pending(stcb, sifa)))) {
 2650                         /* on the no-no list */
 2651                         continue;
 2652                 }
 2653                 stcb->asoc.last_used_address = laddr;
 2654                 atomic_add_int(&sifa->refcount, 1);
 2655                 return (sifa);
 2656         }
 2657         if (start_at_beginning == 0) {
 2658                 stcb->asoc.last_used_address = NULL;
 2659                 goto sctp_from_the_top;
 2660         }
 2661         /* now try for any higher scope than the destination */
 2662         stcb->asoc.last_used_address = starting_point;
 2663         start_at_beginning = 0;
 2664 sctp_from_the_top2:
 2665         if (stcb->asoc.last_used_address == NULL) {
 2666                 start_at_beginning = 1;
 2667                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
 2668         }
 2669         /* search beginning with the last used address */
 2670         for (laddr = stcb->asoc.last_used_address; laddr;
 2671             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 2672                 if (laddr->ifa == NULL) {
 2673                         /* address has been removed */
 2674                         continue;
 2675                 }
 2676                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 2677                         /* address is being deleted */
 2678                         continue;
 2679                 }
 2680                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
 2681                     dest_is_priv, fam);
 2682                 if (sifa == NULL)
 2683                         continue;
 2684                 if (((non_asoc_addr_ok == 0) &&
 2685                     (sctp_is_addr_restricted(stcb, sifa))) ||
 2686                     (non_asoc_addr_ok &&
 2687                     (sctp_is_addr_restricted(stcb, sifa)) &&
 2688                     (!sctp_is_addr_pending(stcb, sifa)))) {
 2689                         /* on the no-no list */
 2690                         continue;
 2691                 }
 2692                 stcb->asoc.last_used_address = laddr;
 2693                 atomic_add_int(&sifa->refcount, 1);
 2694                 return (sifa);
 2695         }
 2696         if (start_at_beginning == 0) {
 2697                 stcb->asoc.last_used_address = NULL;
 2698                 goto sctp_from_the_top2;
 2699         }
 2700         return (NULL);
 2701 }
 2702 
 2703 static struct sctp_ifa *
 2704 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
 2705     struct sctp_tcb *stcb,
 2706     int non_asoc_addr_ok,
 2707     uint8_t dest_is_loop,
 2708     uint8_t dest_is_priv,
 2709     int addr_wanted,
 2710     sa_family_t fam,
 2711     sctp_route_t * ro
 2712 )
 2713 {
 2714         struct sctp_ifa *ifa, *sifa;
 2715         int num_eligible_addr = 0;
 2716 
 2717 #ifdef INET6
 2718         struct sockaddr_in6 sin6, lsa6;
 2719 
 2720         if (fam == AF_INET6) {
 2721                 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
 2722                 (void)sa6_recoverscope(&sin6);
 2723         }
 2724 #endif                          /* INET6 */
 2725         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
 2726                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 2727                     (non_asoc_addr_ok == 0))
 2728                         continue;
 2729                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
 2730                     dest_is_priv, fam);
 2731                 if (sifa == NULL)
 2732                         continue;
 2733 #ifdef INET6
 2734                 if (fam == AF_INET6 &&
 2735                     dest_is_loop &&
 2736                     sifa->src_is_loop && sifa->src_is_priv) {
 2737                         /*
 2738                          * don't allow fe80::1 to be a src on loop ::1, we
 2739                          * don't list it to the peer so we will get an
 2740                          * abort.
 2741                          */
 2742                         continue;
 2743                 }
 2744                 if (fam == AF_INET6 &&
 2745                     IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
 2746                     IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
 2747                         /*
 2748                          * link-local <-> link-local must belong to the same
 2749                          * scope.
 2750                          */
 2751                         memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
 2752                         (void)sa6_recoverscope(&lsa6);
 2753                         if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
 2754                                 continue;
 2755                         }
 2756                 }
 2757 #endif                          /* INET6 */
 2758 
 2759                 /*
 2760                  * Check if the IPv6 address matches to next-hop. In the
 2761                  * mobile case, old IPv6 address may be not deleted from the
 2762                  * interface. Then, the interface has previous and new
 2763                  * addresses.  We should use one corresponding to the
 2764                  * next-hop.  (by micchie)
 2765                  */
 2766 #ifdef INET6
 2767                 if (stcb && fam == AF_INET6 &&
 2768                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
 2769                         if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
 2770                             == 0) {
 2771                                 continue;
 2772                         }
 2773                 }
 2774 #endif
 2775 #ifdef INET
 2776                 /* Avoid topologically incorrect IPv4 address */
 2777                 if (stcb && fam == AF_INET &&
 2778                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
 2779                         if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
 2780                                 continue;
 2781                         }
 2782                 }
 2783 #endif
 2784                 if (stcb) {
 2785                         if (sctp_is_address_in_scope(ifa,
 2786                             stcb->asoc.ipv4_addr_legal,
 2787                             stcb->asoc.ipv6_addr_legal,
 2788                             stcb->asoc.loopback_scope,
 2789                             stcb->asoc.ipv4_local_scope,
 2790                             stcb->asoc.local_scope,
 2791                             stcb->asoc.site_scope, 0) == 0) {
 2792                                 continue;
 2793                         }
 2794                         if (((non_asoc_addr_ok == 0) &&
 2795                             (sctp_is_addr_restricted(stcb, sifa))) ||
 2796                             (non_asoc_addr_ok &&
 2797                             (sctp_is_addr_restricted(stcb, sifa)) &&
 2798                             (!sctp_is_addr_pending(stcb, sifa)))) {
 2799                                 /*
 2800                                  * It is restricted for some reason..
 2801                                  * probably not yet added.
 2802                                  */
 2803                                 continue;
 2804                         }
 2805                 }
 2806                 if (num_eligible_addr >= addr_wanted) {
 2807                         return (sifa);
 2808                 }
 2809                 num_eligible_addr++;
 2810         }
 2811         return (NULL);
 2812 }
 2813 
 2814 
 2815 static int
 2816 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
 2817     struct sctp_tcb *stcb,
 2818     int non_asoc_addr_ok,
 2819     uint8_t dest_is_loop,
 2820     uint8_t dest_is_priv,
 2821     sa_family_t fam)
 2822 {
 2823         struct sctp_ifa *ifa, *sifa;
 2824         int num_eligible_addr = 0;
 2825 
 2826         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
 2827                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 2828                     (non_asoc_addr_ok == 0)) {
 2829                         continue;
 2830                 }
 2831                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
 2832                     dest_is_priv, fam);
 2833                 if (sifa == NULL) {
 2834                         continue;
 2835                 }
 2836                 if (stcb) {
 2837                         if (sctp_is_address_in_scope(ifa,
 2838                             stcb->asoc.ipv4_addr_legal,
 2839                             stcb->asoc.ipv6_addr_legal,
 2840                             stcb->asoc.loopback_scope,
 2841                             stcb->asoc.ipv4_local_scope,
 2842                             stcb->asoc.local_scope,
 2843                             stcb->asoc.site_scope, 0) == 0) {
 2844                                 continue;
 2845                         }
 2846                         if (((non_asoc_addr_ok == 0) &&
 2847                             (sctp_is_addr_restricted(stcb, sifa))) ||
 2848                             (non_asoc_addr_ok &&
 2849                             (sctp_is_addr_restricted(stcb, sifa)) &&
 2850                             (!sctp_is_addr_pending(stcb, sifa)))) {
 2851                                 /*
 2852                                  * It is restricted for some reason..
 2853                                  * probably not yet added.
 2854                                  */
 2855                                 continue;
 2856                         }
 2857                 }
 2858                 num_eligible_addr++;
 2859         }
 2860         return (num_eligible_addr);
 2861 }
 2862 
 2863 static struct sctp_ifa *
 2864 sctp_choose_boundall(struct sctp_tcb *stcb,
 2865     struct sctp_nets *net,
 2866     sctp_route_t * ro,
 2867     uint32_t vrf_id,
 2868     uint8_t dest_is_priv,
 2869     uint8_t dest_is_loop,
 2870     int non_asoc_addr_ok,
 2871     sa_family_t fam)
 2872 {
 2873         int cur_addr_num = 0, num_preferred = 0;
 2874         void *ifn;
 2875         struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
 2876         struct sctp_ifa *sctp_ifa, *sifa;
 2877         uint32_t ifn_index;
 2878         struct sctp_vrf *vrf;
 2879 
 2880 #ifdef INET
 2881         int retried = 0;
 2882 
 2883 #endif
 2884 
 2885         /*-
 2886          * For boundall we can use any address in the association.
 2887          * If non_asoc_addr_ok is set we can use any address (at least in
 2888          * theory). So we look for preferred addresses first. If we find one,
 2889          * we use it. Otherwise we next try to get an address on the
 2890          * interface, which we should be able to do (unless non_asoc_addr_ok
 2891          * is false and we are routed out that way). In these cases where we
 2892          * can't use the address of the interface we go through all the
 2893          * ifn's looking for an address we can use and fill that in. Punting
 2894          * means we send back address 0, which will probably cause problems
 2895          * actually since then IP will fill in the address of the route ifn,
 2896          * which means we probably already rejected it.. i.e. here comes an
 2897          * abort :-<.
 2898          */
 2899         vrf = sctp_find_vrf(vrf_id);
 2900         if (vrf == NULL)
 2901                 return (NULL);
 2902 
 2903         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 2904         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
 2905         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
 2906         emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
 2907         if (sctp_ifn == NULL) {
 2908                 /* ?? We don't have this guy ?? */
 2909                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
 2910                 goto bound_all_plan_b;
 2911         }
 2912         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
 2913             ifn_index, sctp_ifn->ifn_name);
 2914 
 2915         if (net) {
 2916                 cur_addr_num = net->indx_of_eligible_next_to_use;
 2917         }
 2918         num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
 2919             stcb,
 2920             non_asoc_addr_ok,
 2921             dest_is_loop,
 2922             dest_is_priv, fam);
 2923         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
 2924             num_preferred, sctp_ifn->ifn_name);
 2925         if (num_preferred == 0) {
 2926                 /*
 2927                  * no eligible addresses, we must use some other interface
 2928                  * address if we can find one.
 2929                  */
 2930                 goto bound_all_plan_b;
 2931         }
 2932         /*
 2933          * Ok we have num_eligible_addr set with how many we can use, this
 2934          * may vary from call to call due to addresses being deprecated
 2935          * etc..
 2936          */
 2937         if (cur_addr_num >= num_preferred) {
 2938                 cur_addr_num = 0;
 2939         }
 2940         /*
 2941          * select the nth address from the list (where cur_addr_num is the
 2942          * nth) and 0 is the first one, 1 is the second one etc...
 2943          */
 2944         SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
 2945 
 2946         sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
 2947             dest_is_priv, cur_addr_num, fam, ro);
 2948 
 2949         /* if sctp_ifa is NULL something changed??, fall to plan b. */
 2950         if (sctp_ifa) {
 2951                 atomic_add_int(&sctp_ifa->refcount, 1);
 2952                 if (net) {
 2953                         /* save off where the next one we will want */
 2954                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
 2955                 }
 2956                 return (sctp_ifa);
 2957         }
 2958         /*
 2959          * plan_b: Look at all interfaces and find a preferred address. If
 2960          * no preferred fall through to plan_c.
 2961          */
 2962 bound_all_plan_b:
 2963         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
 2964         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
 2965                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
 2966                     sctp_ifn->ifn_name);
 2967                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
 2968                         /* wrong base scope */
 2969                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
 2970                         continue;
 2971                 }
 2972                 if ((sctp_ifn == looked_at) && looked_at) {
 2973                         /* already looked at this guy */
 2974                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
 2975                         continue;
 2976                 }
 2977                 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
 2978                     dest_is_loop, dest_is_priv, fam);
 2979                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
 2980                     "Found ifn:%p %d preferred source addresses\n",
 2981                     ifn, num_preferred);
 2982                 if (num_preferred == 0) {
 2983                         /* None on this interface. */
 2984                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
 2985                         continue;
 2986                 }
 2987                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
 2988                     "num preferred:%d on interface:%p cur_addr_num:%d\n",
 2989                     num_preferred, sctp_ifn, cur_addr_num);
 2990 
 2991                 /*
 2992                  * Ok we have num_eligible_addr set with how many we can
 2993                  * use, this may vary from call to call due to addresses
 2994                  * being deprecated etc..
 2995                  */
 2996                 if (cur_addr_num >= num_preferred) {
 2997                         cur_addr_num = 0;
 2998                 }
 2999                 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
 3000                     dest_is_priv, cur_addr_num, fam, ro);
 3001                 if (sifa == NULL)
 3002                         continue;
 3003                 if (net) {
 3004                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
 3005                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
 3006                             cur_addr_num);
 3007                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
 3008                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
 3009                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
 3010                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
 3011                 }
 3012                 atomic_add_int(&sifa->refcount, 1);
 3013                 return (sifa);
 3014         }
 3015 #ifdef INET
 3016 again_with_private_addresses_allowed:
 3017 #endif
 3018         /* plan_c: do we have an acceptable address on the emit interface */
 3019         sifa = NULL;
 3020         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
 3021         if (emit_ifn == NULL) {
 3022                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
 3023                 goto plan_d;
 3024         }
 3025         LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
 3026                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", sctp_ifa);
 3027                 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 3028                     (non_asoc_addr_ok == 0)) {
 3029                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
 3030                         continue;
 3031                 }
 3032                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
 3033                     dest_is_priv, fam);
 3034                 if (sifa == NULL) {
 3035                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
 3036                         continue;
 3037                 }
 3038                 if (stcb) {
 3039                         if (sctp_is_address_in_scope(sifa,
 3040                             stcb->asoc.ipv4_addr_legal,
 3041                             stcb->asoc.ipv6_addr_legal,
 3042                             stcb->asoc.loopback_scope,
 3043                             stcb->asoc.ipv4_local_scope,
 3044                             stcb->asoc.local_scope,
 3045                             stcb->asoc.site_scope, 0) == 0) {
 3046                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
 3047                                 sifa = NULL;
 3048                                 continue;
 3049                         }
 3050                         if (((non_asoc_addr_ok == 0) &&
 3051                             (sctp_is_addr_restricted(stcb, sifa))) ||
 3052                             (non_asoc_addr_ok &&
 3053                             (sctp_is_addr_restricted(stcb, sifa)) &&
 3054                             (!sctp_is_addr_pending(stcb, sifa)))) {
 3055                                 /*
 3056                                  * It is restricted for some reason..
 3057                                  * probably not yet added.
 3058                                  */
 3059                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n");
 3060                                 sifa = NULL;
 3061                                 continue;
 3062                         }
 3063                 } else {
 3064                         printf("Stcb is null - no print\n");
 3065                 }
 3066                 atomic_add_int(&sifa->refcount, 1);
 3067                 goto out;
 3068         }
 3069 plan_d:
 3070         /*
 3071          * plan_d: We are in trouble. No preferred address on the emit
 3072          * interface. And not even a preferred address on all interfaces. Go
 3073          * out and see if we can find an acceptable address somewhere
 3074          * amongst all interfaces.
 3075          */
 3076         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", looked_at);
 3077         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
 3078                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
 3079                         /* wrong base scope */
 3080                         continue;
 3081                 }
 3082                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 3083                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 3084                             (non_asoc_addr_ok == 0))
 3085                                 continue;
 3086                         sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
 3087                             dest_is_loop,
 3088                             dest_is_priv, fam);
 3089                         if (sifa == NULL)
 3090                                 continue;
 3091                         if (stcb) {
 3092                                 if (sctp_is_address_in_scope(sifa,
 3093                                     stcb->asoc.ipv4_addr_legal,
 3094                                     stcb->asoc.ipv6_addr_legal,
 3095                                     stcb->asoc.loopback_scope,
 3096                                     stcb->asoc.ipv4_local_scope,
 3097                                     stcb->asoc.local_scope,
 3098                                     stcb->asoc.site_scope, 0) == 0) {
 3099                                         sifa = NULL;
 3100                                         continue;
 3101                                 }
 3102                                 if (((non_asoc_addr_ok == 0) &&
 3103                                     (sctp_is_addr_restricted(stcb, sifa))) ||
 3104                                     (non_asoc_addr_ok &&
 3105                                     (sctp_is_addr_restricted(stcb, sifa)) &&
 3106                                     (!sctp_is_addr_pending(stcb, sifa)))) {
 3107                                         /*
 3108                                          * It is restricted for some
 3109                                          * reason.. probably not yet added.
 3110                                          */
 3111                                         sifa = NULL;
 3112                                         continue;
 3113                                 }
 3114                         }
 3115                         goto out;
 3116                 }
 3117         }
 3118 #ifdef INET
 3119         if ((retried == 0) && (stcb->asoc.ipv4_local_scope == 0)) {
 3120                 stcb->asoc.ipv4_local_scope = 1;
 3121                 retried = 1;
 3122                 goto again_with_private_addresses_allowed;
 3123         } else if (retried == 1) {
 3124                 stcb->asoc.ipv4_local_scope = 0;
 3125         }
 3126 #endif
 3127 out:
 3128 #ifdef INET
 3129         if (sifa) {
 3130                 if (retried == 1) {
 3131                         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
 3132                                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
 3133                                         /* wrong base scope */
 3134                                         continue;
 3135                                 }
 3136                                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 3137                                         struct sctp_ifa *tmp_sifa;
 3138 
 3139                                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 3140                                             (non_asoc_addr_ok == 0))
 3141                                                 continue;
 3142                                         tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
 3143                                             dest_is_loop,
 3144                                             dest_is_priv, fam);
 3145                                         if (tmp_sifa == NULL) {
 3146                                                 continue;
 3147                                         }
 3148                                         if (tmp_sifa == sifa) {
 3149                                                 continue;
 3150                                         }
 3151                                         if (stcb) {
 3152                                                 if (sctp_is_address_in_scope(tmp_sifa,
 3153                                                     stcb->asoc.ipv4_addr_legal,
 3154                                                     stcb->asoc.ipv6_addr_legal,
 3155                                                     stcb->asoc.loopback_scope,
 3156                                                     stcb->asoc.ipv4_local_scope,
 3157                                                     stcb->asoc.local_scope,
 3158                                                     stcb->asoc.site_scope, 0) == 0) {
 3159                                                         continue;
 3160                                                 }
 3161                                                 if (((non_asoc_addr_ok == 0) &&
 3162                                                     (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
 3163                                                     (non_asoc_addr_ok &&
 3164                                                     (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
 3165                                                     (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
 3166                                                         /*
 3167                                                          * It is restricted
 3168                                                          * for some reason..
 3169                                                          * probably not yet
 3170                                                          * added.
 3171                                                          */
 3172                                                         continue;
 3173                                                 }
 3174                                         }
 3175                                         if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
 3176                                             (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
 3177                                                 sctp_add_local_addr_restricted(stcb, tmp_sifa);
 3178                                         }
 3179                                 }
 3180                         }
 3181                 }
 3182                 atomic_add_int(&sifa->refcount, 1);
 3183         }
 3184 #endif
 3185         return (sifa);
 3186 }
 3187 
 3188 
 3189 
 3190 /* tcb may be NULL */
 3191 struct sctp_ifa *
 3192 sctp_source_address_selection(struct sctp_inpcb *inp,
 3193     struct sctp_tcb *stcb,
 3194     sctp_route_t * ro,
 3195     struct sctp_nets *net,
 3196     int non_asoc_addr_ok, uint32_t vrf_id)
 3197 {
 3198         struct sctp_ifa *answer;
 3199         uint8_t dest_is_priv, dest_is_loop;
 3200         sa_family_t fam;
 3201 
 3202 #ifdef INET
 3203         struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
 3204 
 3205 #endif
 3206 #ifdef INET6
 3207         struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
 3208 
 3209 #endif
 3210 
 3211         /**
 3212          * Rules: - Find the route if needed, cache if I can. - Look at
 3213          * interface address in route, Is it in the bound list. If so we
 3214          * have the best source. - If not we must rotate amongst the
 3215          * addresses.
 3216          *
 3217          * Cavets and issues
 3218          *
 3219          * Do we need to pay attention to scope. We can have a private address
 3220          * or a global address we are sourcing or sending to. So if we draw
 3221          * it out
 3222          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
 3223          * For V4
 3224          * ------------------------------------------
 3225          *      source     *      dest  *  result
 3226          * -----------------------------------------
 3227          * <a>  Private    *    Global  *  NAT
 3228          * -----------------------------------------
 3229          * <b>  Private    *    Private *  No problem
 3230          * -----------------------------------------
 3231          * <c>  Global     *    Private *  Huh, How will this work?
 3232          * -----------------------------------------
 3233          * <d>  Global     *    Global  *  No Problem
 3234          *------------------------------------------
 3235          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
 3236          * For V6
 3237          *------------------------------------------
 3238          *      source     *      dest  *  result
 3239          * -----------------------------------------
 3240          * <a>  Linklocal  *    Global  *
 3241          * -----------------------------------------
 3242          * <b>  Linklocal  * Linklocal  *  No problem
 3243          * -----------------------------------------
 3244          * <c>  Global     * Linklocal  *  Huh, How will this work?
 3245          * -----------------------------------------
 3246          * <d>  Global     *    Global  *  No Problem
 3247          *------------------------------------------
 3248          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
 3249          *
 3250          * And then we add to that what happens if there are multiple addresses
 3251          * assigned to an interface. Remember the ifa on a ifn is a linked
 3252          * list of addresses. So one interface can have more than one IP
 3253          * address. What happens if we have both a private and a global
 3254          * address? Do we then use context of destination to sort out which
 3255          * one is best? And what about NAT's sending P->G may get you a NAT
 3256          * translation, or should you select the G thats on the interface in
 3257          * preference.
 3258          *
 3259          * Decisions:
 3260          *
 3261          * - count the number of addresses on the interface.
 3262          * - if it is one, no problem except case <c>.
 3263          *   For <a> we will assume a NAT out there.
 3264          * - if there are more than one, then we need to worry about scope P
 3265          *   or G. We should prefer G -> G and P -> P if possible.
 3266          *   Then as a secondary fall back to mixed types G->P being a last
 3267          *   ditch one.
 3268          * - The above all works for bound all, but bound specific we need to
 3269          *   use the same concept but instead only consider the bound
 3270          *   addresses. If the bound set is NOT assigned to the interface then
 3271          *   we must use rotation amongst the bound addresses..
 3272          */
 3273         if (ro->ro_rt == NULL) {
 3274                 /*
 3275                  * Need a route to cache.
 3276                  */
 3277                 SCTP_RTALLOC(ro, vrf_id);
 3278         }
 3279         if (ro->ro_rt == NULL) {
 3280                 return (NULL);
 3281         }
 3282         fam = ro->ro_dst.sa_family;
 3283         dest_is_priv = dest_is_loop = 0;
 3284         /* Setup our scopes for the destination */
 3285         switch (fam) {
 3286 #ifdef INET
 3287         case AF_INET:
 3288                 /* Scope based on outbound address */
 3289                 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
 3290                         dest_is_loop = 1;
 3291                         if (net != NULL) {
 3292                                 /* mark it as local */
 3293                                 net->addr_is_local = 1;
 3294                         }
 3295                 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
 3296                         dest_is_priv = 1;
 3297                 }
 3298                 break;
 3299 #endif
 3300 #ifdef INET6
 3301         case AF_INET6:
 3302                 /* Scope based on outbound address */
 3303                 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
 3304                     SCTP_ROUTE_IS_REAL_LOOP(ro)) {
 3305                         /*
 3306                          * If the address is a loopback address, which
 3307                          * consists of "::1" OR "fe80::1%lo0", we are
 3308                          * loopback scope. But we don't use dest_is_priv
 3309                          * (link local addresses).
 3310                          */
 3311                         dest_is_loop = 1;
 3312                         if (net != NULL) {
 3313                                 /* mark it as local */
 3314                                 net->addr_is_local = 1;
 3315                         }
 3316                 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
 3317                         dest_is_priv = 1;
 3318                 }
 3319                 break;
 3320 #endif
 3321         }
 3322         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
 3323         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
 3324         SCTP_IPI_ADDR_RLOCK();
 3325         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 3326                 /*
 3327                  * Bound all case
 3328                  */
 3329                 answer = sctp_choose_boundall(stcb, net, ro, vrf_id,
 3330                     dest_is_priv, dest_is_loop,
 3331                     non_asoc_addr_ok, fam);
 3332                 SCTP_IPI_ADDR_RUNLOCK();
 3333                 return (answer);
 3334         }
 3335         /*
 3336          * Subset bound case
 3337          */
 3338         if (stcb) {
 3339                 answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
 3340                     vrf_id, dest_is_priv,
 3341                     dest_is_loop,
 3342                     non_asoc_addr_ok, fam);
 3343         } else {
 3344                 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
 3345                     non_asoc_addr_ok,
 3346                     dest_is_priv,
 3347                     dest_is_loop, fam);
 3348         }
 3349         SCTP_IPI_ADDR_RUNLOCK();
 3350         return (answer);
 3351 }
 3352 
 3353 static int
 3354 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
 3355 {
 3356         struct cmsghdr cmh;
 3357         int tlen, at, found;
 3358         struct sctp_sndinfo sndinfo;
 3359         struct sctp_prinfo prinfo;
 3360         struct sctp_authinfo authinfo;
 3361 
 3362         tlen = SCTP_BUF_LEN(control);
 3363         at = 0;
 3364         found = 0;
 3365         /*
 3366          * Independent of how many mbufs, find the c_type inside the control
 3367          * structure and copy out the data.
 3368          */
 3369         while (at < tlen) {
 3370                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
 3371                         /* There is not enough room for one more. */
 3372                         return (found);
 3373                 }
 3374                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
 3375                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
 3376                         /* We dont't have a complete CMSG header. */
 3377                         return (found);
 3378                 }
 3379                 if (((int)cmh.cmsg_len + at) > tlen) {
 3380                         /* We don't have the complete CMSG. */
 3381                         return (found);
 3382                 }
 3383                 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
 3384                     ((c_type == cmh.cmsg_type) ||
 3385                     ((c_type == SCTP_SNDRCV) &&
 3386                     ((cmh.cmsg_type == SCTP_SNDINFO) ||
 3387                     (cmh.cmsg_type == SCTP_PRINFO) ||
 3388                     (cmh.cmsg_type == SCTP_AUTHINFO))))) {
 3389                         if (c_type == cmh.cmsg_type) {
 3390                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
 3391                                         return (found);
 3392                                 }
 3393                                 /* It is exactly what we want. Copy it out. */
 3394                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), cpsize, (caddr_t)data);
 3395                                 return (1);
 3396                         } else {
 3397                                 struct sctp_sndrcvinfo *sndrcvinfo;
 3398 
 3399                                 sndrcvinfo = (struct sctp_sndrcvinfo *)data;
 3400                                 if (found == 0) {
 3401                                         if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
 3402                                                 return (found);
 3403                                         }
 3404                                         memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
 3405                                 }
 3406                                 switch (cmh.cmsg_type) {
 3407                                 case SCTP_SNDINFO:
 3408                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_sndinfo)) {
 3409                                                 return (found);
 3410                                         }
 3411                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
 3412                                         sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
 3413                                         sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
 3414                                         sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
 3415                                         sndrcvinfo->sinfo_context = sndinfo.snd_context;
 3416                                         sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
 3417                                         break;
 3418                                 case SCTP_PRINFO:
 3419                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_prinfo)) {
 3420                                                 return (found);
 3421                                         }
 3422                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
 3423                                         sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
 3424                                         sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
 3425                                         break;
 3426                                 case SCTP_AUTHINFO:
 3427                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_authinfo)) {
 3428                                                 return (found);
 3429                                         }
 3430                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
 3431                                         sndrcvinfo->sinfo_keynumber_valid = 1;
 3432                                         sndrcvinfo->sinfo_keynumber = authinfo.auth_keyid;
 3433                                         break;
 3434                                 default:
 3435                                         return (found);
 3436                                 }
 3437                                 found = 1;
 3438                         }
 3439                 }
 3440                 at += CMSG_ALIGN(cmh.cmsg_len);
 3441         }
 3442         return (found);
 3443 }
 3444 
 3445 static int
 3446 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
 3447 {
 3448         struct cmsghdr cmh;
 3449         int tlen, at;
 3450         struct sctp_initmsg initmsg;
 3451 
 3452 #ifdef INET
 3453         struct sockaddr_in sin;
 3454 
 3455 #endif
 3456 #ifdef INET6
 3457         struct sockaddr_in6 sin6;
 3458 
 3459 #endif
 3460 
 3461         tlen = SCTP_BUF_LEN(control);
 3462         at = 0;
 3463         while (at < tlen) {
 3464                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
 3465                         /* There is not enough room for one more. */
 3466                         *error = EINVAL;
 3467                         return (1);
 3468                 }
 3469                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
 3470                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
 3471                         /* We dont't have a complete CMSG header. */
 3472                         *error = EINVAL;
 3473                         return (1);
 3474                 }
 3475                 if (((int)cmh.cmsg_len + at) > tlen) {
 3476                         /* We don't have the complete CMSG. */
 3477                         *error = EINVAL;
 3478                         return (1);
 3479                 }
 3480                 if (cmh.cmsg_level == IPPROTO_SCTP) {
 3481                         switch (cmh.cmsg_type) {
 3482                         case SCTP_INIT:
 3483                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_initmsg)) {
 3484                                         *error = EINVAL;
 3485                                         return (1);
 3486                                 }
 3487                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
 3488                                 if (initmsg.sinit_max_attempts)
 3489                                         stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
 3490                                 if (initmsg.sinit_num_ostreams)
 3491                                         stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
 3492                                 if (initmsg.sinit_max_instreams)
 3493                                         stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
 3494                                 if (initmsg.sinit_max_init_timeo)
 3495                                         stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
 3496                                 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
 3497                                         struct sctp_stream_out *tmp_str;
 3498                                         unsigned int i;
 3499 
 3500                                         /* Default is NOT correct */
 3501                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
 3502                                             stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
 3503                                         SCTP_TCB_UNLOCK(stcb);
 3504                                         SCTP_MALLOC(tmp_str,
 3505                                             struct sctp_stream_out *,
 3506                                             (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
 3507                                             SCTP_M_STRMO);
 3508                                         SCTP_TCB_LOCK(stcb);
 3509                                         if (tmp_str != NULL) {
 3510                                                 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
 3511                                                 stcb->asoc.strmout = tmp_str;
 3512                                                 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
 3513                                         } else {
 3514                                                 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
 3515                                         }
 3516                                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
 3517                                                 stcb->asoc.strmout[i].next_sequence_sent = 0;
 3518                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
 3519                                                 stcb->asoc.strmout[i].stream_no = i;
 3520                                                 stcb->asoc.strmout[i].last_msg_incomplete = 0;
 3521                                                 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
 3522                                         }
 3523                                 }
 3524                                 break;
 3525 #ifdef INET
 3526                         case SCTP_DSTADDRV4:
 3527                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
 3528                                         *error = EINVAL;
 3529                                         return (1);
 3530                                 }
 3531                                 memset(&sin, 0, sizeof(struct sockaddr_in));
 3532                                 sin.sin_family = AF_INET;
 3533                                 sin.sin_len = sizeof(struct sockaddr_in);
 3534                                 sin.sin_port = stcb->rport;
 3535                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
 3536                                 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
 3537                                     (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
 3538                                     IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
 3539                                         *error = EINVAL;
 3540                                         return (1);
 3541                                 }
 3542                                 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
 3543                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
 3544                                         *error = ENOBUFS;
 3545                                         return (1);
 3546                                 }
 3547                                 break;
 3548 #endif
 3549 #ifdef INET6
 3550                         case SCTP_DSTADDRV6:
 3551                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
 3552                                         *error = EINVAL;
 3553                                         return (1);
 3554                                 }
 3555                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
 3556                                 sin6.sin6_family = AF_INET6;
 3557                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
 3558                                 sin6.sin6_port = stcb->rport;
 3559                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
 3560                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
 3561                                     IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
 3562                                         *error = EINVAL;
 3563                                         return (1);
 3564                                 }
 3565 #ifdef INET
 3566                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
 3567                                         in6_sin6_2_sin(&sin, &sin6);
 3568                                         if ((sin.sin_addr.s_addr == INADDR_ANY) ||
 3569                                             (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
 3570                                             IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
 3571                                                 *error = EINVAL;
 3572                                                 return (1);
 3573                                         }
 3574                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
 3575                                             SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
 3576                                                 *error = ENOBUFS;
 3577                                                 return (1);
 3578                                         }
 3579                                 } else
 3580 #endif
 3581                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL,
 3582                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
 3583                                         *error = ENOBUFS;
 3584                                         return (1);
 3585                                 }
 3586                                 break;
 3587 #endif
 3588                         default:
 3589                                 break;
 3590                         }
 3591                 }
 3592                 at += CMSG_ALIGN(cmh.cmsg_len);
 3593         }
 3594         return (0);
 3595 }
 3596 
 3597 static struct sctp_tcb *
 3598 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
 3599     in_port_t port,
 3600     struct mbuf *control,
 3601     struct sctp_nets **net_p,
 3602     int *error)
 3603 {
 3604         struct cmsghdr cmh;
 3605         int tlen, at;
 3606         struct sctp_tcb *stcb;
 3607         struct sockaddr *addr;
 3608 
 3609 #ifdef INET
 3610         struct sockaddr_in sin;
 3611 
 3612 #endif
 3613 #ifdef INET6
 3614         struct sockaddr_in6 sin6;
 3615 
 3616 #endif
 3617 
 3618         tlen = SCTP_BUF_LEN(control);
 3619         at = 0;
 3620         while (at < tlen) {
 3621                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
 3622                         /* There is not enough room for one more. */
 3623                         *error = EINVAL;
 3624                         return (NULL);
 3625                 }
 3626                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
 3627                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
 3628                         /* We dont't have a complete CMSG header. */
 3629                         *error = EINVAL;
 3630                         return (NULL);
 3631                 }
 3632                 if (((int)cmh.cmsg_len + at) > tlen) {
 3633                         /* We don't have the complete CMSG. */
 3634                         *error = EINVAL;
 3635                         return (NULL);
 3636                 }
 3637                 if (cmh.cmsg_level == IPPROTO_SCTP) {
 3638                         switch (cmh.cmsg_type) {
 3639 #ifdef INET
 3640                         case SCTP_DSTADDRV4:
 3641                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
 3642                                         *error = EINVAL;
 3643                                         return (NULL);
 3644                                 }
 3645                                 memset(&sin, 0, sizeof(struct sockaddr_in));
 3646                                 sin.sin_family = AF_INET;
 3647                                 sin.sin_len = sizeof(struct sockaddr_in);
 3648                                 sin.sin_port = port;
 3649                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
 3650                                 addr = (struct sockaddr *)&sin;
 3651                                 break;
 3652 #endif
 3653 #ifdef INET6
 3654                         case SCTP_DSTADDRV6:
 3655                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
 3656                                         *error = EINVAL;
 3657                                         return (NULL);
 3658                                 }
 3659                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
 3660                                 sin6.sin6_family = AF_INET6;
 3661                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
 3662                                 sin6.sin6_port = port;
 3663                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
 3664 #ifdef INET
 3665                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
 3666                                         in6_sin6_2_sin(&sin, &sin6);
 3667                                         addr = (struct sockaddr *)&sin;
 3668                                 } else
 3669 #endif
 3670                                         addr = (struct sockaddr *)&sin6;
 3671                                 break;
 3672 #endif
 3673                         default:
 3674                                 addr = NULL;
 3675                                 break;
 3676                         }
 3677                         if (addr) {
 3678                                 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
 3679                                 if (stcb != NULL) {
 3680                                         return (stcb);
 3681                                 }
 3682                         }
 3683                 }
 3684                 at += CMSG_ALIGN(cmh.cmsg_len);
 3685         }
 3686         return (NULL);
 3687 }
 3688 
 3689 static struct mbuf *
 3690 sctp_add_cookie(struct mbuf *init, int init_offset,
 3691     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
 3692 {
 3693         struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
 3694         struct sctp_state_cookie *stc;
 3695         struct sctp_paramhdr *ph;
 3696         uint8_t *foo;
 3697         int sig_offset;
 3698         uint16_t cookie_sz;
 3699 
 3700         mret = NULL;
 3701         mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
 3702             sizeof(struct sctp_paramhdr)), 0,
 3703             M_DONTWAIT, 1, MT_DATA);
 3704         if (mret == NULL) {
 3705                 return (NULL);
 3706         }
 3707         copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
 3708         if (copy_init == NULL) {
 3709                 sctp_m_freem(mret);
 3710                 return (NULL);
 3711         }
 3712 #ifdef SCTP_MBUF_LOGGING
 3713         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 3714                 struct mbuf *mat;
 3715 
 3716                 mat = copy_init;
 3717                 while (mat) {
 3718                         if (SCTP_BUF_IS_EXTENDED(mat)) {
 3719                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 3720                         }
 3721                         mat = SCTP_BUF_NEXT(mat);
 3722                 }
 3723         }
 3724 #endif
 3725         copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
 3726             M_DONTWAIT);
 3727         if (copy_initack == NULL) {
 3728                 sctp_m_freem(mret);
 3729                 sctp_m_freem(copy_init);
 3730                 return (NULL);
 3731         }
 3732 #ifdef SCTP_MBUF_LOGGING
 3733         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 3734                 struct mbuf *mat;
 3735 
 3736                 mat = copy_initack;
 3737                 while (mat) {
 3738                         if (SCTP_BUF_IS_EXTENDED(mat)) {
 3739                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 3740                         }
 3741                         mat = SCTP_BUF_NEXT(mat);
 3742                 }
 3743         }
 3744 #endif
 3745         /* easy side we just drop it on the end */
 3746         ph = mtod(mret, struct sctp_paramhdr *);
 3747         SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
 3748             sizeof(struct sctp_paramhdr);
 3749         stc = (struct sctp_state_cookie *)((caddr_t)ph +
 3750             sizeof(struct sctp_paramhdr));
 3751         ph->param_type = htons(SCTP_STATE_COOKIE);
 3752         ph->param_length = 0;   /* fill in at the end */
 3753         /* Fill in the stc cookie data */
 3754         memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
 3755 
 3756         /* tack the INIT and then the INIT-ACK onto the chain */
 3757         cookie_sz = 0;
 3758         for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 3759                 cookie_sz += SCTP_BUF_LEN(m_at);
 3760                 if (SCTP_BUF_NEXT(m_at) == NULL) {
 3761                         SCTP_BUF_NEXT(m_at) = copy_init;
 3762                         break;
 3763                 }
 3764         }
 3765         for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 3766                 cookie_sz += SCTP_BUF_LEN(m_at);
 3767                 if (SCTP_BUF_NEXT(m_at) == NULL) {
 3768                         SCTP_BUF_NEXT(m_at) = copy_initack;
 3769                         break;
 3770                 }
 3771         }
 3772         for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 3773                 cookie_sz += SCTP_BUF_LEN(m_at);
 3774                 if (SCTP_BUF_NEXT(m_at) == NULL) {
 3775                         break;
 3776                 }
 3777         }
 3778         sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
 3779         if (sig == NULL) {
 3780                 /* no space, so free the entire chain */
 3781                 sctp_m_freem(mret);
 3782                 return (NULL);
 3783         }
 3784         SCTP_BUF_LEN(sig) = 0;
 3785         SCTP_BUF_NEXT(m_at) = sig;
 3786         sig_offset = 0;
 3787         foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
 3788         memset(foo, 0, SCTP_SIGNATURE_SIZE);
 3789         *signature = foo;
 3790         SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
 3791         cookie_sz += SCTP_SIGNATURE_SIZE;
 3792         ph->param_length = htons(cookie_sz);
 3793         return (mret);
 3794 }
 3795 
 3796 
 3797 static uint8_t
 3798 sctp_get_ect(struct sctp_tcb *stcb)
 3799 {
 3800         if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) {
 3801                 return (SCTP_ECT0_BIT);
 3802         } else {
 3803                 return (0);
 3804         }
 3805 }
 3806 
 3807 static void
 3808 sctp_handle_no_route(struct sctp_tcb *stcb,
 3809     struct sctp_nets *net,
 3810     int so_locked)
 3811 {
 3812         SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
 3813 
 3814         if (net) {
 3815                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
 3816                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
 3817                 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
 3818                         if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
 3819                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
 3820                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
 3821                                     stcb,
 3822                                     SCTP_FAILED_THRESHOLD,
 3823                                     (void *)net,
 3824                                     so_locked);
 3825                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
 3826                                 net->dest_state &= ~SCTP_ADDR_PF;
 3827                         }
 3828                 }
 3829                 if (stcb) {
 3830                         if (net == stcb->asoc.primary_destination) {
 3831                                 /* need a new primary */
 3832                                 struct sctp_nets *alt;
 3833 
 3834                                 alt = sctp_find_alternate_net(stcb, net, 0);
 3835                                 if (alt != net) {
 3836                                         if (stcb->asoc.alternate) {
 3837                                                 sctp_free_remote_addr(stcb->asoc.alternate);
 3838                                         }
 3839                                         stcb->asoc.alternate = alt;
 3840                                         atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
 3841                                         if (net->ro._s_addr) {
 3842                                                 sctp_free_ifa(net->ro._s_addr);
 3843                                                 net->ro._s_addr = NULL;
 3844                                         }
 3845                                         net->src_addr_selected = 0;
 3846                                 }
 3847                         }
 3848                 }
 3849         }
 3850 }
 3851 
 3852 static int
 3853 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
 3854     struct sctp_tcb *stcb,      /* may be NULL */
 3855     struct sctp_nets *net,
 3856     struct sockaddr *to,
 3857     struct mbuf *m,
 3858     uint32_t auth_offset,
 3859     struct sctp_auth_chunk *auth,
 3860     uint16_t auth_keyid,
 3861     int nofragment_flag,
 3862     int ecn_ok,
 3863     int out_of_asoc_ok,
 3864     uint16_t src_port,
 3865     uint16_t dest_port,
 3866     uint32_t v_tag,
 3867     uint16_t port,
 3868 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 3869     int so_locked SCTP_UNUSED,
 3870 #else
 3871     int so_locked,
 3872 #endif
 3873     union sctp_sockstore *over_addr,
 3874     struct mbuf *init
 3875 )
 3876 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
 3877 {
 3878         /**
 3879          * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
 3880          * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
 3881          * - fill in the HMAC digest of any AUTH chunk in the packet.
 3882          * - calculate and fill in the SCTP checksum.
 3883          * - prepend an IP address header.
 3884          * - if boundall use INADDR_ANY.
 3885          * - if boundspecific do source address selection.
 3886          * - set fragmentation option for ipV4.
 3887          * - On return from IP output, check/adjust mtu size of output
 3888          *   interface and smallest_mtu size as well.
 3889          */
 3890         /* Will need ifdefs around this */
 3891         struct mbuf *o_pak;
 3892         struct mbuf *newm;
 3893         struct sctphdr *sctphdr;
 3894         int packet_length;
 3895         int ret;
 3896         uint32_t vrf_id;
 3897         sctp_route_t *ro = NULL;
 3898         struct udphdr *udp = NULL;
 3899         uint8_t tos_value;
 3900 
 3901 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 3902         struct socket *so = NULL;
 3903 
 3904 #endif
 3905 
 3906         if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
 3907                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
 3908                 sctp_m_freem(m);
 3909                 return (EFAULT);
 3910         }
 3911         if (stcb) {
 3912                 vrf_id = stcb->asoc.vrf_id;
 3913         } else {
 3914                 vrf_id = inp->def_vrf_id;
 3915         }
 3916 
 3917         /* fill in the HMAC digest for any AUTH chunk in the packet */
 3918         if ((auth != NULL) && (stcb != NULL)) {
 3919                 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
 3920         }
 3921         if (net) {
 3922                 tos_value = net->dscp;
 3923         } else if (stcb) {
 3924                 tos_value = stcb->asoc.default_dscp;
 3925         } else {
 3926                 tos_value = inp->sctp_ep.default_dscp;
 3927         }
 3928 
 3929         switch (to->sa_family) {
 3930 #ifdef INET
 3931         case AF_INET:
 3932                 {
 3933                         struct ip *ip = NULL;
 3934                         sctp_route_t iproute;
 3935                         int len;
 3936 
 3937                         len = sizeof(struct ip) + sizeof(struct sctphdr);
 3938                         if (port) {
 3939                                 len += sizeof(struct udphdr);
 3940                         }
 3941                         newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
 3942                         if (newm == NULL) {
 3943                                 sctp_m_freem(m);
 3944                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 3945                                 return (ENOMEM);
 3946                         }
 3947                         SCTP_ALIGN_TO_END(newm, len);
 3948                         SCTP_BUF_LEN(newm) = len;
 3949                         SCTP_BUF_NEXT(newm) = m;
 3950                         m = newm;
 3951                         if (net != NULL) {
 3952 #ifdef INVARIANTS
 3953                                 if (net->flowidset == 0) {
 3954                                         panic("Flow ID not set");
 3955                                 }
 3956 #endif
 3957                                 m->m_pkthdr.flowid = net->flowid;
 3958                                 m->m_flags |= M_FLOWID;
 3959                         } else {
 3960                                 if ((init != NULL) && (init->m_flags & M_FLOWID)) {
 3961                                         m->m_pkthdr.flowid = init->m_pkthdr.flowid;
 3962                                         m->m_flags |= M_FLOWID;
 3963                                 }
 3964                         }
 3965                         packet_length = sctp_calculate_len(m);
 3966                         ip = mtod(m, struct ip *);
 3967                         ip->ip_v = IPVERSION;
 3968                         ip->ip_hl = (sizeof(struct ip) >> 2);
 3969                         if (tos_value == 0) {
 3970                                 /*
 3971                                  * This means especially, that it is not set
 3972                                  * at the SCTP layer. So use the value from
 3973                                  * the IP layer.
 3974                                  */
 3975                                 tos_value = inp->ip_inp.inp.inp_ip_tos;
 3976                         }
 3977                         tos_value &= 0xfc;
 3978                         if (ecn_ok) {
 3979                                 tos_value |= sctp_get_ect(stcb);
 3980                         }
 3981                         if ((nofragment_flag) && (port == 0)) {
 3982                                 ip->ip_off = IP_DF;
 3983                         } else
 3984                                 ip->ip_off = 0;
 3985 
 3986                         /* FreeBSD has a function for ip_id's */
 3987                         ip->ip_id = ip_newid();
 3988 
 3989                         ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
 3990                         ip->ip_len = packet_length;
 3991                         ip->ip_tos = tos_value;
 3992                         if (port) {
 3993                                 ip->ip_p = IPPROTO_UDP;
 3994                         } else {
 3995                                 ip->ip_p = IPPROTO_SCTP;
 3996                         }
 3997                         ip->ip_sum = 0;
 3998                         if (net == NULL) {
 3999                                 ro = &iproute;
 4000                                 memset(&iproute, 0, sizeof(iproute));
 4001                                 memcpy(&ro->ro_dst, to, to->sa_len);
 4002                         } else {
 4003                                 ro = (sctp_route_t *) & net->ro;
 4004                         }
 4005                         /* Now the address selection part */
 4006                         ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
 4007 
 4008                         /* call the routine to select the src address */
 4009                         if (net && out_of_asoc_ok == 0) {
 4010                                 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
 4011                                         sctp_free_ifa(net->ro._s_addr);
 4012                                         net->ro._s_addr = NULL;
 4013                                         net->src_addr_selected = 0;
 4014                                         if (ro->ro_rt) {
 4015                                                 RTFREE(ro->ro_rt);
 4016                                                 ro->ro_rt = NULL;
 4017                                         }
 4018                                 }
 4019                                 if (net->src_addr_selected == 0) {
 4020                                         /* Cache the source address */
 4021                                         net->ro._s_addr = sctp_source_address_selection(inp, stcb,
 4022                                             ro, net, 0,
 4023                                             vrf_id);
 4024                                         net->src_addr_selected = 1;
 4025                                 }
 4026                                 if (net->ro._s_addr == NULL) {
 4027                                         /* No route to host */
 4028                                         net->src_addr_selected = 0;
 4029                                         sctp_handle_no_route(stcb, net, so_locked);
 4030                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4031                                         sctp_m_freem(m);
 4032                                         return (EHOSTUNREACH);
 4033                                 }
 4034                                 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
 4035                         } else {
 4036                                 if (over_addr == NULL) {
 4037                                         struct sctp_ifa *_lsrc;
 4038 
 4039                                         _lsrc = sctp_source_address_selection(inp, stcb, ro,
 4040                                             net,
 4041                                             out_of_asoc_ok,
 4042                                             vrf_id);
 4043                                         if (_lsrc == NULL) {
 4044                                                 sctp_handle_no_route(stcb, net, so_locked);
 4045                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4046                                                 sctp_m_freem(m);
 4047                                                 return (EHOSTUNREACH);
 4048                                         }
 4049                                         ip->ip_src = _lsrc->address.sin.sin_addr;
 4050                                         sctp_free_ifa(_lsrc);
 4051                                 } else {
 4052                                         ip->ip_src = over_addr->sin.sin_addr;
 4053                                         SCTP_RTALLOC(ro, vrf_id);
 4054                                 }
 4055                         }
 4056                         if (port) {
 4057                                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
 4058                                         sctp_handle_no_route(stcb, net, so_locked);
 4059                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4060                                         sctp_m_freem(m);
 4061                                         return (EHOSTUNREACH);
 4062                                 }
 4063                                 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
 4064                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
 4065                                 udp->uh_dport = port;
 4066                                 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
 4067                                 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
 4068                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
 4069                         } else {
 4070                                 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
 4071                         }
 4072 
 4073                         sctphdr->src_port = src_port;
 4074                         sctphdr->dest_port = dest_port;
 4075                         sctphdr->v_tag = v_tag;
 4076                         sctphdr->checksum = 0;
 4077 
 4078                         /*
 4079                          * If source address selection fails and we find no
 4080                          * route then the ip_output should fail as well with
 4081                          * a NO_ROUTE_TO_HOST type error. We probably should
 4082                          * catch that somewhere and abort the association
 4083                          * right away (assuming this is an INIT being sent).
 4084                          */
 4085                         if (ro->ro_rt == NULL) {
 4086                                 /*
 4087                                  * src addr selection failed to find a route
 4088                                  * (or valid source addr), so we can't get
 4089                                  * there from here (yet)!
 4090                                  */
 4091                                 sctp_handle_no_route(stcb, net, so_locked);
 4092                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4093                                 sctp_m_freem(m);
 4094                                 return (EHOSTUNREACH);
 4095                         }
 4096                         if (ro != &iproute) {
 4097                                 memcpy(&iproute, ro, sizeof(*ro));
 4098                         }
 4099                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
 4100                             (uint32_t) (ntohl(ip->ip_src.s_addr)));
 4101                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
 4102                             (uint32_t) (ntohl(ip->ip_dst.s_addr)));
 4103                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
 4104                             ro->ro_rt);
 4105 
 4106                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
 4107                                 /* failed to prepend data, give up */
 4108                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 4109                                 sctp_m_freem(m);
 4110                                 return (ENOMEM);
 4111                         }
 4112 #ifdef  SCTP_PACKET_LOGGING
 4113                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
 4114                                 sctp_packet_log(m, packet_length);
 4115 #endif
 4116                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
 4117                         if (port) {
 4118 #if defined(SCTP_WITH_NO_CSUM)
 4119                                 SCTP_STAT_INCR(sctps_sendnocrc);
 4120 #else
 4121                                 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
 4122                                     (stcb) &&
 4123                                     (stcb->asoc.loopback_scope))) {
 4124                                         sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
 4125                                         SCTP_STAT_INCR(sctps_sendswcrc);
 4126                                 } else {
 4127                                         SCTP_STAT_INCR(sctps_sendnocrc);
 4128                                 }
 4129 #endif
 4130                                 SCTP_ENABLE_UDP_CSUM(o_pak);
 4131                         } else {
 4132 #if defined(SCTP_WITH_NO_CSUM)
 4133                                 SCTP_STAT_INCR(sctps_sendnocrc);
 4134 #else
 4135                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
 4136                                 m->m_pkthdr.csum_data = 0;
 4137                                 SCTP_STAT_INCR(sctps_sendhwcrc);
 4138 #endif
 4139                         }
 4140                         /* send it out.  table id is taken from stcb */
 4141 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 4142                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 4143                                 so = SCTP_INP_SO(inp);
 4144                                 SCTP_SOCKET_UNLOCK(so, 0);
 4145                         }
 4146 #endif
 4147                         SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
 4148 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 4149                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 4150                                 atomic_add_int(&stcb->asoc.refcnt, 1);
 4151                                 SCTP_TCB_UNLOCK(stcb);
 4152                                 SCTP_SOCKET_LOCK(so, 0);
 4153                                 SCTP_TCB_LOCK(stcb);
 4154                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 4155                         }
 4156 #endif
 4157                         SCTP_STAT_INCR(sctps_sendpackets);
 4158                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
 4159                         if (ret)
 4160                                 SCTP_STAT_INCR(sctps_senderrors);
 4161 
 4162                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
 4163                         if (net == NULL) {
 4164                                 /* free tempy routes */
 4165                                 if (ro->ro_rt) {
 4166                                         RTFREE(ro->ro_rt);
 4167                                         ro->ro_rt = NULL;
 4168                                 }
 4169                         } else {
 4170                                 /*
 4171                                  * PMTU check versus smallest asoc MTU goes
 4172                                  * here
 4173                                  */
 4174                                 if ((ro->ro_rt != NULL) &&
 4175                                     (net->ro._s_addr)) {
 4176                                         uint32_t mtu;
 4177 
 4178                                         mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
 4179                                         if (net->port) {
 4180                                                 mtu -= sizeof(struct udphdr);
 4181                                         }
 4182                                         if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
 4183                                                 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
 4184                                                 net->mtu = mtu;
 4185                                         }
 4186                                 } else if (ro->ro_rt == NULL) {
 4187                                         /* route was freed */
 4188                                         if (net->ro._s_addr &&
 4189                                             net->src_addr_selected) {
 4190                                                 sctp_free_ifa(net->ro._s_addr);
 4191                                                 net->ro._s_addr = NULL;
 4192                                         }
 4193                                         net->src_addr_selected = 0;
 4194                                 }
 4195                         }
 4196                         return (ret);
 4197                 }
 4198 #endif
 4199 #ifdef INET6
 4200         case AF_INET6:
 4201                 {
 4202                         uint32_t flowlabel, flowinfo;
 4203                         struct ip6_hdr *ip6h;
 4204                         struct route_in6 ip6route;
 4205                         struct ifnet *ifp;
 4206                         struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
 4207                         int prev_scope = 0;
 4208                         struct sockaddr_in6 lsa6_storage;
 4209                         int error;
 4210                         u_short prev_port = 0;
 4211                         int len;
 4212 
 4213                         if (net) {
 4214                                 flowlabel = net->flowlabel;
 4215                         } else if (stcb) {
 4216                                 flowlabel = stcb->asoc.default_flowlabel;
 4217                         } else {
 4218                                 flowlabel = inp->sctp_ep.default_flowlabel;
 4219                         }
 4220                         if (flowlabel == 0) {
 4221                                 /*
 4222                                  * This means especially, that it is not set
 4223                                  * at the SCTP layer. So use the value from
 4224                                  * the IP layer.
 4225                                  */
 4226                                 flowlabel = ntohl(((struct in6pcb *)inp)->in6p_flowinfo);
 4227                         }
 4228                         flowlabel &= 0x000fffff;
 4229                         len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
 4230                         if (port) {
 4231                                 len += sizeof(struct udphdr);
 4232                         }
 4233                         newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
 4234                         if (newm == NULL) {
 4235                                 sctp_m_freem(m);
 4236                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 4237                                 return (ENOMEM);
 4238                         }
 4239                         SCTP_ALIGN_TO_END(newm, len);
 4240                         SCTP_BUF_LEN(newm) = len;
 4241                         SCTP_BUF_NEXT(newm) = m;
 4242                         m = newm;
 4243                         if (net != NULL) {
 4244 #ifdef INVARIANTS
 4245                                 if (net->flowidset == 0) {
 4246                                         panic("Flow ID not set");
 4247                                 }
 4248 #endif
 4249                                 m->m_pkthdr.flowid = net->flowid;
 4250                                 m->m_flags |= M_FLOWID;
 4251                         } else {
 4252                                 if ((init != NULL) && (init->m_flags & M_FLOWID)) {
 4253                                         m->m_pkthdr.flowid = init->m_pkthdr.flowid;
 4254                                         m->m_flags |= M_FLOWID;
 4255                                 }
 4256                         }
 4257                         packet_length = sctp_calculate_len(m);
 4258 
 4259                         ip6h = mtod(m, struct ip6_hdr *);
 4260                         /* protect *sin6 from overwrite */
 4261                         sin6 = (struct sockaddr_in6 *)to;
 4262                         tmp = *sin6;
 4263                         sin6 = &tmp;
 4264 
 4265                         /* KAME hack: embed scopeid */
 4266                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
 4267                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 4268                                 return (EINVAL);
 4269                         }
 4270                         if (net == NULL) {
 4271                                 memset(&ip6route, 0, sizeof(ip6route));
 4272                                 ro = (sctp_route_t *) & ip6route;
 4273                                 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
 4274                         } else {
 4275                                 ro = (sctp_route_t *) & net->ro;
 4276                         }
 4277                         /*
 4278                          * We assume here that inp_flow is in host byte
 4279                          * order within the TCB!
 4280                          */
 4281                         if (tos_value == 0) {
 4282                                 /*
 4283                                  * This means especially, that it is not set
 4284                                  * at the SCTP layer. So use the value from
 4285                                  * the IP layer.
 4286                                  */
 4287                                 tos_value = (ntohl(((struct in6pcb *)inp)->in6p_flowinfo) >> 20) & 0xff;
 4288                         }
 4289                         tos_value &= 0xfc;
 4290                         if (ecn_ok) {
 4291                                 tos_value |= sctp_get_ect(stcb);
 4292                         }
 4293                         flowinfo = 0x06;
 4294                         flowinfo <<= 8;
 4295                         flowinfo |= tos_value;
 4296                         flowinfo <<= 20;
 4297                         flowinfo |= flowlabel;
 4298                         ip6h->ip6_flow = htonl(flowinfo);
 4299                         if (port) {
 4300                                 ip6h->ip6_nxt = IPPROTO_UDP;
 4301                         } else {
 4302                                 ip6h->ip6_nxt = IPPROTO_SCTP;
 4303                         }
 4304                         ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
 4305                         ip6h->ip6_dst = sin6->sin6_addr;
 4306 
 4307                         /*
 4308                          * Add SRC address selection here: we can only reuse
 4309                          * to a limited degree the kame src-addr-sel, since
 4310                          * we can try their selection but it may not be
 4311                          * bound.
 4312                          */
 4313                         bzero(&lsa6_tmp, sizeof(lsa6_tmp));
 4314                         lsa6_tmp.sin6_family = AF_INET6;
 4315                         lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
 4316                         lsa6 = &lsa6_tmp;
 4317                         if (net && out_of_asoc_ok == 0) {
 4318                                 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
 4319                                         sctp_free_ifa(net->ro._s_addr);
 4320                                         net->ro._s_addr = NULL;
 4321                                         net->src_addr_selected = 0;
 4322                                         if (ro->ro_rt) {
 4323                                                 RTFREE(ro->ro_rt);
 4324                                                 ro->ro_rt = NULL;
 4325                                         }
 4326                                 }
 4327                                 if (net->src_addr_selected == 0) {
 4328                                         sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
 4329                                         /* KAME hack: embed scopeid */
 4330                                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
 4331                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 4332                                                 return (EINVAL);
 4333                                         }
 4334                                         /* Cache the source address */
 4335                                         net->ro._s_addr = sctp_source_address_selection(inp,
 4336                                             stcb,
 4337                                             ro,
 4338                                             net,
 4339                                             0,
 4340                                             vrf_id);
 4341                                         (void)sa6_recoverscope(sin6);
 4342                                         net->src_addr_selected = 1;
 4343                                 }
 4344                                 if (net->ro._s_addr == NULL) {
 4345                                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
 4346                                         net->src_addr_selected = 0;
 4347                                         sctp_handle_no_route(stcb, net, so_locked);
 4348                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4349                                         sctp_m_freem(m);
 4350                                         return (EHOSTUNREACH);
 4351                                 }
 4352                                 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
 4353                         } else {
 4354                                 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
 4355                                 /* KAME hack: embed scopeid */
 4356                                 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
 4357                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 4358                                         return (EINVAL);
 4359                                 }
 4360                                 if (over_addr == NULL) {
 4361                                         struct sctp_ifa *_lsrc;
 4362 
 4363                                         _lsrc = sctp_source_address_selection(inp, stcb, ro,
 4364                                             net,
 4365                                             out_of_asoc_ok,
 4366                                             vrf_id);
 4367                                         if (_lsrc == NULL) {
 4368                                                 sctp_handle_no_route(stcb, net, so_locked);
 4369                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4370                                                 sctp_m_freem(m);
 4371                                                 return (EHOSTUNREACH);
 4372                                         }
 4373                                         lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
 4374                                         sctp_free_ifa(_lsrc);
 4375                                 } else {
 4376                                         lsa6->sin6_addr = over_addr->sin6.sin6_addr;
 4377                                         SCTP_RTALLOC(ro, vrf_id);
 4378                                 }
 4379                                 (void)sa6_recoverscope(sin6);
 4380                         }
 4381                         lsa6->sin6_port = inp->sctp_lport;
 4382 
 4383                         if (ro->ro_rt == NULL) {
 4384                                 /*
 4385                                  * src addr selection failed to find a route
 4386                                  * (or valid source addr), so we can't get
 4387                                  * there from here!
 4388                                  */
 4389                                 sctp_handle_no_route(stcb, net, so_locked);
 4390                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4391                                 sctp_m_freem(m);
 4392                                 return (EHOSTUNREACH);
 4393                         }
 4394                         /*
 4395                          * XXX: sa6 may not have a valid sin6_scope_id in
 4396                          * the non-SCOPEDROUTING case.
 4397                          */
 4398                         bzero(&lsa6_storage, sizeof(lsa6_storage));
 4399                         lsa6_storage.sin6_family = AF_INET6;
 4400                         lsa6_storage.sin6_len = sizeof(lsa6_storage);
 4401                         lsa6_storage.sin6_addr = lsa6->sin6_addr;
 4402                         if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
 4403                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
 4404                                 sctp_m_freem(m);
 4405                                 return (error);
 4406                         }
 4407                         /* XXX */
 4408                         lsa6_storage.sin6_addr = lsa6->sin6_addr;
 4409                         lsa6_storage.sin6_port = inp->sctp_lport;
 4410                         lsa6 = &lsa6_storage;
 4411                         ip6h->ip6_src = lsa6->sin6_addr;
 4412 
 4413                         if (port) {
 4414                                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
 4415                                         sctp_handle_no_route(stcb, net, so_locked);
 4416                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 4417                                         sctp_m_freem(m);
 4418                                         return (EHOSTUNREACH);
 4419                                 }
 4420                                 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
 4421                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
 4422                                 udp->uh_dport = port;
 4423                                 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
 4424                                 udp->uh_sum = 0;
 4425                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
 4426                         } else {
 4427                                 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
 4428                         }
 4429 
 4430                         sctphdr->src_port = src_port;
 4431                         sctphdr->dest_port = dest_port;
 4432                         sctphdr->v_tag = v_tag;
 4433                         sctphdr->checksum = 0;
 4434 
 4435                         /*
 4436                          * We set the hop limit now since there is a good
 4437                          * chance that our ro pointer is now filled
 4438                          */
 4439                         ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
 4440                         ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 4441 
 4442 #ifdef SCTP_DEBUG
 4443                         /* Copy to be sure something bad is not happening */
 4444                         sin6->sin6_addr = ip6h->ip6_dst;
 4445                         lsa6->sin6_addr = ip6h->ip6_src;
 4446 #endif
 4447 
 4448                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
 4449                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
 4450                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
 4451                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
 4452                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
 4453                         if (net) {
 4454                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
 4455                                 /*
 4456                                  * preserve the port and scope for link
 4457                                  * local send
 4458                                  */
 4459                                 prev_scope = sin6->sin6_scope_id;
 4460                                 prev_port = sin6->sin6_port;
 4461                         }
 4462                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
 4463                                 /* failed to prepend data, give up */
 4464                                 sctp_m_freem(m);
 4465                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 4466                                 return (ENOMEM);
 4467                         }
 4468 #ifdef  SCTP_PACKET_LOGGING
 4469                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
 4470                                 sctp_packet_log(m, packet_length);
 4471 #endif
 4472                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
 4473                         if (port) {
 4474 #if defined(SCTP_WITH_NO_CSUM)
 4475                                 SCTP_STAT_INCR(sctps_sendnocrc);
 4476 #else
 4477                                 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
 4478                                     (stcb) &&
 4479                                     (stcb->asoc.loopback_scope))) {
 4480                                         sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
 4481                                         SCTP_STAT_INCR(sctps_sendswcrc);
 4482                                 } else {
 4483                                         SCTP_STAT_INCR(sctps_sendnocrc);
 4484                                 }
 4485 #endif
 4486                                 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
 4487                                         udp->uh_sum = 0xffff;
 4488                                 }
 4489                         } else {
 4490 #if defined(SCTP_WITH_NO_CSUM)
 4491                                 SCTP_STAT_INCR(sctps_sendnocrc);
 4492 #else
 4493                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
 4494                                 m->m_pkthdr.csum_data = 0;
 4495                                 SCTP_STAT_INCR(sctps_sendhwcrc);
 4496 #endif
 4497                         }
 4498                         /* send it out. table id is taken from stcb */
 4499 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 4500                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 4501                                 so = SCTP_INP_SO(inp);
 4502                                 SCTP_SOCKET_UNLOCK(so, 0);
 4503                         }
 4504 #endif
 4505                         SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
 4506 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 4507                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 4508                                 atomic_add_int(&stcb->asoc.refcnt, 1);
 4509                                 SCTP_TCB_UNLOCK(stcb);
 4510                                 SCTP_SOCKET_LOCK(so, 0);
 4511                                 SCTP_TCB_LOCK(stcb);
 4512                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
 4513                         }
 4514 #endif
 4515                         if (net) {
 4516                                 /* for link local this must be done */
 4517                                 sin6->sin6_scope_id = prev_scope;
 4518                                 sin6->sin6_port = prev_port;
 4519                         }
 4520                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
 4521                         SCTP_STAT_INCR(sctps_sendpackets);
 4522                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
 4523                         if (ret) {
 4524                                 SCTP_STAT_INCR(sctps_senderrors);
 4525                         }
 4526                         if (net == NULL) {
 4527                                 /* Now if we had a temp route free it */
 4528                                 if (ro->ro_rt) {
 4529                                         RTFREE(ro->ro_rt);
 4530                                 }
 4531                         } else {
 4532                                 /*
 4533                                  * PMTU check versus smallest asoc MTU goes
 4534                                  * here
 4535                                  */
 4536                                 if (ro->ro_rt == NULL) {
 4537                                         /* Route was freed */
 4538                                         if (net->ro._s_addr &&
 4539                                             net->src_addr_selected) {
 4540                                                 sctp_free_ifa(net->ro._s_addr);
 4541                                                 net->ro._s_addr = NULL;
 4542                                         }
 4543                                         net->src_addr_selected = 0;
 4544                                 }
 4545                                 if ((ro->ro_rt != NULL) &&
 4546                                     (net->ro._s_addr)) {
 4547                                         uint32_t mtu;
 4548 
 4549                                         mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
 4550                                         if (mtu &&
 4551                                             (stcb->asoc.smallest_mtu > mtu)) {
 4552                                                 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
 4553                                                 net->mtu = mtu;
 4554                                                 if (net->port) {
 4555                                                         net->mtu -= sizeof(struct udphdr);
 4556                                                 }
 4557                                         }
 4558                                 } else if (ifp) {
 4559                                         if (ND_IFINFO(ifp)->linkmtu &&
 4560                                             (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
 4561                                                 sctp_mtu_size_reset(inp,
 4562                                                     &stcb->asoc,
 4563                                                     ND_IFINFO(ifp)->linkmtu);
 4564                                         }
 4565                                 }
 4566                         }
 4567                         return (ret);
 4568                 }
 4569 #endif
 4570         default:
 4571                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
 4572                     ((struct sockaddr *)to)->sa_family);
 4573                 sctp_m_freem(m);
 4574                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
 4575                 return (EFAULT);
 4576         }
 4577 }
 4578 
 4579 
 4580 void
 4581 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
 4582 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 4583     SCTP_UNUSED
 4584 #endif
 4585 )
 4586 {
 4587         struct mbuf *m, *m_at, *mp_last;
 4588         struct sctp_nets *net;
 4589         struct sctp_init_chunk *init;
 4590         struct sctp_supported_addr_param *sup_addr;
 4591         struct sctp_adaptation_layer_indication *ali;
 4592         struct sctp_ecn_supported_param *ecn;
 4593         struct sctp_prsctp_supported_param *prsctp;
 4594         struct sctp_supported_chunk_types_param *pr_supported;
 4595         int cnt_inits_to = 0;
 4596         int padval, ret;
 4597         int num_ext;
 4598         int p_len;
 4599 
 4600         /* INIT's always go to the primary (and usually ONLY address) */
 4601         mp_last = NULL;
 4602         net = stcb->asoc.primary_destination;
 4603         if (net == NULL) {
 4604                 net = TAILQ_FIRST(&stcb->asoc.nets);
 4605                 if (net == NULL) {
 4606                         /* TSNH */
 4607                         return;
 4608                 }
 4609                 /* we confirm any address we send an INIT to */
 4610                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
 4611                 (void)sctp_set_primary_addr(stcb, NULL, net);
 4612         } else {
 4613                 /* we confirm any address we send an INIT to */
 4614                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
 4615         }
 4616         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
 4617 #ifdef INET6
 4618         if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
 4619                 /*
 4620                  * special hook, if we are sending to link local it will not
 4621                  * show up in our private address count.
 4622                  */
 4623                 struct sockaddr_in6 *sin6l;
 4624 
 4625                 sin6l = &net->ro._l_addr.sin6;
 4626                 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
 4627                         cnt_inits_to = 1;
 4628         }
 4629 #endif
 4630         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
 4631                 /* This case should not happen */
 4632                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
 4633                 return;
 4634         }
 4635         /* start the INIT timer */
 4636         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
 4637 
 4638         m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
 4639         if (m == NULL) {
 4640                 /* No memory, INIT timer will re-attempt. */
 4641                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
 4642                 return;
 4643         }
 4644         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
 4645         /*
 4646          * assume peer supports asconf in order to be able to queue local
 4647          * address changes while an INIT is in flight and before the assoc
 4648          * is established.
 4649          */
 4650         stcb->asoc.peer_supports_asconf = 1;
 4651         /* Now lets put the SCTP header in place */
 4652         init = mtod(m, struct sctp_init_chunk *);
 4653         /* now the chunk header */
 4654         init->ch.chunk_type = SCTP_INITIATION;
 4655         init->ch.chunk_flags = 0;
 4656         /* fill in later from mbuf we build */
 4657         init->ch.chunk_length = 0;
 4658         /* place in my tag */
 4659         init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
 4660         /* set up some of the credits. */
 4661         init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
 4662             SCTP_MINIMAL_RWND));
 4663 
 4664         init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
 4665         init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
 4666         init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
 4667         /* now the address restriction */
 4668         /* XXX Should we take the address family of the socket into account? */
 4669         sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
 4670             sizeof(*init));
 4671         sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
 4672 #ifdef INET6
 4673 #ifdef INET
 4674         /* we support 2 types: IPv4/IPv6 */
 4675         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + 2 * sizeof(uint16_t));
 4676         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
 4677         sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
 4678 #else
 4679         /* we support 1 type: IPv6 */
 4680         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
 4681         sup_addr->addr_type[0] = htons(SCTP_IPV6_ADDRESS);
 4682         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
 4683 #endif
 4684 #else
 4685         /* we support 1 type: IPv4 */
 4686         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
 4687         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
 4688         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
 4689 #endif
 4690         SCTP_BUF_LEN(m) += sizeof(struct sctp_supported_addr_param);
 4691         /* adaptation layer indication parameter */
 4692         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(struct sctp_supported_addr_param));
 4693         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
 4694         ali->ph.param_length = htons(sizeof(*ali));
 4695         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
 4696         SCTP_BUF_LEN(m) += sizeof(*ali);
 4697         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
 4698 
 4699         if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
 4700                 /* Add NAT friendly parameter */
 4701                 struct sctp_paramhdr *ph;
 4702 
 4703                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 4704                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
 4705                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
 4706                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
 4707                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
 4708         }
 4709         /* now any cookie time extensions */
 4710         if (stcb->asoc.cookie_preserve_req) {
 4711                 struct sctp_cookie_perserve_param *cookie_preserve;
 4712 
 4713                 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
 4714                 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
 4715                 cookie_preserve->ph.param_length = htons(
 4716                     sizeof(*cookie_preserve));
 4717                 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
 4718                 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
 4719                 ecn = (struct sctp_ecn_supported_param *)(
 4720                     (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
 4721                 stcb->asoc.cookie_preserve_req = 0;
 4722         }
 4723         /* ECN parameter */
 4724         if (stcb->asoc.ecn_allowed == 1) {
 4725                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
 4726                 ecn->ph.param_length = htons(sizeof(*ecn));
 4727                 SCTP_BUF_LEN(m) += sizeof(*ecn);
 4728                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
 4729                     sizeof(*ecn));
 4730         } else {
 4731                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
 4732         }
 4733         /* And now tell the peer we do pr-sctp */
 4734         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
 4735         prsctp->ph.param_length = htons(sizeof(*prsctp));
 4736         SCTP_BUF_LEN(m) += sizeof(*prsctp);
 4737 
 4738         /* And now tell the peer we do all the extensions */
 4739         pr_supported = (struct sctp_supported_chunk_types_param *)
 4740             ((caddr_t)prsctp + sizeof(*prsctp));
 4741         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
 4742         num_ext = 0;
 4743         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
 4744         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
 4745         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
 4746         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
 4747         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
 4748         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
 4749                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
 4750         }
 4751         if (stcb->asoc.sctp_nr_sack_on_off == 1) {
 4752                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
 4753         }
 4754         p_len = sizeof(*pr_supported) + num_ext;
 4755         pr_supported->ph.param_length = htons(p_len);
 4756         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
 4757         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 4758 
 4759 
 4760         /* add authentication parameters */
 4761         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
 4762                 struct sctp_auth_random *randp;
 4763                 struct sctp_auth_hmac_algo *hmacs;
 4764                 struct sctp_auth_chunk_list *chunks;
 4765 
 4766                 /* attach RANDOM parameter, if available */
 4767                 if (stcb->asoc.authinfo.random != NULL) {
 4768                         randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 4769                         p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
 4770                         /* random key already contains the header */
 4771                         bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
 4772                         /* zero out any padding required */
 4773                         bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
 4774                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 4775                 }
 4776                 /* add HMAC_ALGO parameter */
 4777                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 4778                 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
 4779                     (uint8_t *) hmacs->hmac_ids);
 4780                 if (p_len > 0) {
 4781                         p_len += sizeof(*hmacs);
 4782                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
 4783                         hmacs->ph.param_length = htons(p_len);
 4784                         /* zero out any padding required */
 4785                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
 4786                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 4787                 }
 4788                 /* add CHUNKS parameter */
 4789                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 4790                 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
 4791                     chunks->chunk_types);
 4792                 if (p_len > 0) {
 4793                         p_len += sizeof(*chunks);
 4794                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
 4795                         chunks->ph.param_length = htons(p_len);
 4796                         /* zero out any padding required */
 4797                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
 4798                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 4799                 }
 4800         }
 4801         /* now the addresses */
 4802         {
 4803                 struct sctp_scoping scp;
 4804 
 4805                 /*
 4806                  * To optimize this we could put the scoping stuff into a
 4807                  * structure and remove the individual uint8's from the
 4808                  * assoc structure. Then we could just sifa in the address
 4809                  * within the stcb. But for now this is a quick hack to get
 4810                  * the address stuff teased apart.
 4811                  */
 4812 
 4813                 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
 4814                 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
 4815                 scp.loopback_scope = stcb->asoc.loopback_scope;
 4816                 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
 4817                 scp.local_scope = stcb->asoc.local_scope;
 4818                 scp.site_scope = stcb->asoc.site_scope;
 4819 
 4820                 sctp_add_addresses_to_i_ia(inp, stcb, &scp, m, cnt_inits_to);
 4821         }
 4822 
 4823         /* calulate the size and update pkt header and chunk header */
 4824         p_len = 0;
 4825         for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 4826                 if (SCTP_BUF_NEXT(m_at) == NULL)
 4827                         mp_last = m_at;
 4828                 p_len += SCTP_BUF_LEN(m_at);
 4829         }
 4830         init->ch.chunk_length = htons(p_len);
 4831         /*
 4832          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
 4833          * here since the timer will drive a retranmission.
 4834          */
 4835 
 4836         /* I don't expect this to execute but we will be safe here */
 4837         padval = p_len % 4;
 4838         if ((padval) && (mp_last)) {
 4839                 /*
 4840                  * The compiler worries that mp_last may not be set even
 4841                  * though I think it is impossible :-> however we add
 4842                  * mp_last here just in case.
 4843                  */
 4844                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
 4845                 if (ret) {
 4846                         /* Houston we have a problem, no space */
 4847                         sctp_m_freem(m);
 4848                         return;
 4849                 }
 4850         }
 4851         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
 4852         ret = sctp_lowlevel_chunk_output(inp, stcb, net,
 4853             (struct sockaddr *)&net->ro._l_addr,
 4854             m, 0, NULL, 0, 0, 0, 0,
 4855             inp->sctp_lport, stcb->rport, htonl(0),
 4856             net->port, so_locked, NULL, NULL);
 4857         SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
 4858         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 4859         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 4860 }
 4861 
 4862 struct mbuf *
 4863 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
 4864     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
 4865 {
 4866         /*
 4867          * Given a mbuf containing an INIT or INIT-ACK with the param_offset
 4868          * being equal to the beginning of the params i.e. (iphlen +
 4869          * sizeof(struct sctp_init_msg) parse through the parameters to the
 4870          * end of the mbuf verifying that all parameters are known.
 4871          * 
 4872          * For unknown parameters build and return a mbuf with
 4873          * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
 4874          * processing this chunk stop, and set *abort_processing to 1.
 4875          * 
 4876          * By having param_offset be pre-set to where parameters begin it is
 4877          * hoped that this routine may be reused in the future by new
 4878          * features.
 4879          */
 4880         struct sctp_paramhdr *phdr, params;
 4881 
 4882         struct mbuf *mat, *op_err;
 4883         char tempbuf[SCTP_PARAM_BUFFER_SIZE];
 4884         int at, limit, pad_needed;
 4885         uint16_t ptype, plen, padded_size;
 4886         int err_at;
 4887 
 4888         *abort_processing = 0;
 4889         mat = in_initpkt;
 4890         err_at = 0;
 4891         limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
 4892         at = param_offset;
 4893         op_err = NULL;
 4894         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
 4895         phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
 4896         while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
 4897                 ptype = ntohs(phdr->param_type);
 4898                 plen = ntohs(phdr->param_length);
 4899                 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
 4900                         /* wacked parameter */
 4901                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
 4902                         goto invalid_size;
 4903                 }
 4904                 limit -= SCTP_SIZE32(plen);
 4905                 /*-
 4906                  * All parameters for all chunks that we know/understand are
 4907                  * listed here. We process them other places and make
 4908                  * appropriate stop actions per the upper bits. However this
 4909                  * is the generic routine processor's can call to get back
 4910                  * an operr.. to either incorporate (init-ack) or send.
 4911                  */
 4912                 padded_size = SCTP_SIZE32(plen);
 4913                 switch (ptype) {
 4914                         /* Param's with variable size */
 4915                 case SCTP_HEARTBEAT_INFO:
 4916                 case SCTP_STATE_COOKIE:
 4917                 case SCTP_UNRECOG_PARAM:
 4918                 case SCTP_ERROR_CAUSE_IND:
 4919                         /* ok skip fwd */
 4920                         at += padded_size;
 4921                         break;
 4922                         /* Param's with variable size within a range */
 4923                 case SCTP_CHUNK_LIST:
 4924                 case SCTP_SUPPORTED_CHUNK_EXT:
 4925                         if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
 4926                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
 4927                                 goto invalid_size;
 4928                         }
 4929                         at += padded_size;
 4930                         break;
 4931                 case SCTP_SUPPORTED_ADDRTYPE:
 4932                         if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
 4933                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
 4934                                 goto invalid_size;
 4935                         }
 4936                         at += padded_size;
 4937                         break;
 4938                 case SCTP_RANDOM:
 4939                         if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
 4940                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
 4941                                 goto invalid_size;
 4942                         }
 4943                         at += padded_size;
 4944                         break;
 4945                 case SCTP_SET_PRIM_ADDR:
 4946                 case SCTP_DEL_IP_ADDRESS:
 4947                 case SCTP_ADD_IP_ADDRESS:
 4948                         if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
 4949                             (padded_size != sizeof(struct sctp_asconf_addr_param))) {
 4950                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
 4951                                 goto invalid_size;
 4952                         }
 4953                         at += padded_size;
 4954                         break;
 4955                         /* Param's with a fixed size */
 4956                 case SCTP_IPV4_ADDRESS:
 4957                         if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
 4958                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
 4959                                 goto invalid_size;
 4960                         }
 4961                         at += padded_size;
 4962                         break;
 4963                 case SCTP_IPV6_ADDRESS:
 4964                         if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
 4965                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
 4966                                 goto invalid_size;
 4967                         }
 4968                         at += padded_size;
 4969                         break;
 4970                 case SCTP_COOKIE_PRESERVE:
 4971                         if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
 4972                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
 4973                                 goto invalid_size;
 4974                         }
 4975                         at += padded_size;
 4976                         break;
 4977                 case SCTP_HAS_NAT_SUPPORT:
 4978                         *nat_friendly = 1;
 4979                         /* fall through */
 4980                 case SCTP_PRSCTP_SUPPORTED:
 4981 
 4982                         if (padded_size != sizeof(struct sctp_paramhdr)) {
 4983                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
 4984                                 goto invalid_size;
 4985                         }
 4986                         at += padded_size;
 4987                         break;
 4988                 case SCTP_ECN_CAPABLE:
 4989                         if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
 4990                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
 4991                                 goto invalid_size;
 4992                         }
 4993                         at += padded_size;
 4994                         break;
 4995                 case SCTP_ULP_ADAPTATION:
 4996                         if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
 4997                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
 4998                                 goto invalid_size;
 4999                         }
 5000                         at += padded_size;
 5001                         break;
 5002                 case SCTP_SUCCESS_REPORT:
 5003                         if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
 5004                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
 5005                                 goto invalid_size;
 5006                         }
 5007                         at += padded_size;
 5008                         break;
 5009                 case SCTP_HOSTNAME_ADDRESS:
 5010                         {
 5011                                 /* We can NOT handle HOST NAME addresses!! */
 5012                                 int l_len;
 5013 
 5014                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
 5015                                 *abort_processing = 1;
 5016                                 if (op_err == NULL) {
 5017                                         /* Ok need to try to get a mbuf */
 5018 #ifdef INET6
 5019                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 5020 #else
 5021                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 5022 #endif
 5023                                         l_len += plen;
 5024                                         l_len += sizeof(struct sctp_paramhdr);
 5025                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
 5026                                         if (op_err) {
 5027                                                 SCTP_BUF_LEN(op_err) = 0;
 5028                                                 /*
 5029                                                  * pre-reserve space for ip
 5030                                                  * and sctp header  and
 5031                                                  * chunk hdr
 5032                                                  */
 5033 #ifdef INET6
 5034                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
 5035 #else
 5036                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
 5037 #endif
 5038                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
 5039                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
 5040                                         }
 5041                                 }
 5042                                 if (op_err) {
 5043                                         /* If we have space */
 5044                                         struct sctp_paramhdr s;
 5045 
 5046                                         if (err_at % 4) {
 5047                                                 uint32_t cpthis = 0;
 5048 
 5049                                                 pad_needed = 4 - (err_at % 4);
 5050                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
 5051                                                 err_at += pad_needed;
 5052                                         }
 5053                                         s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
 5054                                         s.param_length = htons(sizeof(s) + plen);
 5055                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
 5056                                         err_at += sizeof(s);
 5057                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
 5058                                         if (phdr == NULL) {
 5059                                                 sctp_m_freem(op_err);
 5060                                                 /*
 5061                                                  * we are out of memory but
 5062                                                  * we still need to have a
 5063                                                  * look at what to do (the
 5064                                                  * system is in trouble
 5065                                                  * though).
 5066                                                  */
 5067                                                 return (NULL);
 5068                                         }
 5069                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
 5070                                 }
 5071                                 return (op_err);
 5072                                 break;
 5073                         }
 5074                 default:
 5075                         /*
 5076                          * we do not recognize the parameter figure out what
 5077                          * we do.
 5078                          */
 5079                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
 5080                         if ((ptype & 0x4000) == 0x4000) {
 5081                                 /* Report bit is set?? */
 5082                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
 5083                                 if (op_err == NULL) {
 5084                                         int l_len;
 5085 
 5086                                         /* Ok need to try to get an mbuf */
 5087 #ifdef INET6
 5088                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 5089 #else
 5090                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 5091 #endif
 5092                                         l_len += plen;
 5093                                         l_len += sizeof(struct sctp_paramhdr);
 5094                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
 5095                                         if (op_err) {
 5096                                                 SCTP_BUF_LEN(op_err) = 0;
 5097 #ifdef INET6
 5098                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
 5099 #else
 5100                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
 5101 #endif
 5102                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
 5103                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
 5104                                         }
 5105                                 }
 5106                                 if (op_err) {
 5107                                         /* If we have space */
 5108                                         struct sctp_paramhdr s;
 5109 
 5110                                         if (err_at % 4) {
 5111                                                 uint32_t cpthis = 0;
 5112 
 5113                                                 pad_needed = 4 - (err_at % 4);
 5114                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
 5115                                                 err_at += pad_needed;
 5116                                         }
 5117                                         s.param_type = htons(SCTP_UNRECOG_PARAM);
 5118                                         s.param_length = htons(sizeof(s) + plen);
 5119                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
 5120                                         err_at += sizeof(s);
 5121                                         if (plen > sizeof(tempbuf)) {
 5122                                                 plen = sizeof(tempbuf);
 5123                                         }
 5124                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
 5125                                         if (phdr == NULL) {
 5126                                                 sctp_m_freem(op_err);
 5127                                                 /*
 5128                                                  * we are out of memory but
 5129                                                  * we still need to have a
 5130                                                  * look at what to do (the
 5131                                                  * system is in trouble
 5132                                                  * though).
 5133                                                  */
 5134                                                 op_err = NULL;
 5135                                                 goto more_processing;
 5136                                         }
 5137                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
 5138                                         err_at += plen;
 5139                                 }
 5140                         }
 5141         more_processing:
 5142                         if ((ptype & 0x8000) == 0x0000) {
 5143                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
 5144                                 return (op_err);
 5145                         } else {
 5146                                 /* skip this chunk and continue processing */
 5147                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
 5148                                 at += SCTP_SIZE32(plen);
 5149                         }
 5150                         break;
 5151 
 5152                 }
 5153                 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
 5154         }
 5155         return (op_err);
 5156 invalid_size:
 5157         SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
 5158         *abort_processing = 1;
 5159         if ((op_err == NULL) && phdr) {
 5160                 int l_len;
 5161 
 5162 #ifdef INET6
 5163                 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 5164 #else
 5165                 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 5166 #endif
 5167                 l_len += (2 * sizeof(struct sctp_paramhdr));
 5168                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
 5169                 if (op_err) {
 5170                         SCTP_BUF_LEN(op_err) = 0;
 5171 #ifdef INET6
 5172                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
 5173 #else
 5174                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
 5175 #endif
 5176                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
 5177                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
 5178                 }
 5179         }
 5180         if ((op_err) && phdr) {
 5181                 struct sctp_paramhdr s;
 5182 
 5183                 if (err_at % 4) {
 5184                         uint32_t cpthis = 0;
 5185 
 5186                         pad_needed = 4 - (err_at % 4);
 5187                         m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
 5188                         err_at += pad_needed;
 5189                 }
 5190                 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
 5191                 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
 5192                 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
 5193                 err_at += sizeof(s);
 5194                 /* Only copy back the p-hdr that caused the issue */
 5195                 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
 5196         }
 5197         return (op_err);
 5198 }
 5199 
 5200 static int
 5201 sctp_are_there_new_addresses(struct sctp_association *asoc,
 5202     struct mbuf *in_initpkt, int offset)
 5203 {
 5204         /*
 5205          * Given a INIT packet, look through the packet to verify that there
 5206          * are NO new addresses. As we go through the parameters add reports
 5207          * of any un-understood parameters that require an error.  Also we
 5208          * must return (1) to drop the packet if we see a un-understood
 5209          * parameter that tells us to drop the chunk.
 5210          */
 5211         struct sockaddr *sa_touse;
 5212         struct sockaddr *sa;
 5213         struct sctp_paramhdr *phdr, params;
 5214         uint16_t ptype, plen;
 5215         uint8_t fnd;
 5216         struct sctp_nets *net;
 5217         struct ip *iph;
 5218 
 5219 #ifdef INET
 5220         struct sockaddr_in sin4, *sa4;
 5221 
 5222 #endif
 5223 #ifdef INET6
 5224         struct sockaddr_in6 sin6, *sa6;
 5225         struct ip6_hdr *ip6h;
 5226 
 5227 #endif
 5228 
 5229 #ifdef INET
 5230         memset(&sin4, 0, sizeof(sin4));
 5231         sin4.sin_family = AF_INET;
 5232         sin4.sin_len = sizeof(sin4);
 5233 #endif
 5234 #ifdef INET6
 5235         memset(&sin6, 0, sizeof(sin6));
 5236         sin6.sin6_family = AF_INET6;
 5237         sin6.sin6_len = sizeof(sin6);
 5238 #endif
 5239         sa_touse = NULL;
 5240         /* First what about the src address of the pkt ? */
 5241         iph = mtod(in_initpkt, struct ip *);
 5242         switch (iph->ip_v) {
 5243 #ifdef INET
 5244         case IPVERSION:
 5245                 /* source addr is IPv4 */
 5246                 sin4.sin_addr = iph->ip_src;
 5247                 sa_touse = (struct sockaddr *)&sin4;
 5248                 break;
 5249 #endif
 5250 #ifdef INET6
 5251         case IPV6_VERSION >> 4:
 5252                 /* source addr is IPv6 */
 5253                 ip6h = mtod(in_initpkt, struct ip6_hdr *);
 5254                 sin6.sin6_addr = ip6h->ip6_src;
 5255                 sa_touse = (struct sockaddr *)&sin6;
 5256                 break;
 5257 #endif
 5258         default:
 5259                 return (1);
 5260         }
 5261 
 5262         fnd = 0;
 5263         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
 5264                 sa = (struct sockaddr *)&net->ro._l_addr;
 5265                 if (sa->sa_family == sa_touse->sa_family) {
 5266 #ifdef INET
 5267                         if (sa->sa_family == AF_INET) {
 5268                                 sa4 = (struct sockaddr_in *)sa;
 5269                                 if (sa4->sin_addr.s_addr == sin4.sin_addr.s_addr) {
 5270                                         fnd = 1;
 5271                                         break;
 5272                                 }
 5273                         }
 5274 #endif
 5275 #ifdef INET6
 5276                         if (sa->sa_family == AF_INET6) {
 5277                                 sa6 = (struct sockaddr_in6 *)sa;
 5278                                 if (SCTP6_ARE_ADDR_EQUAL(sa6, &sin6)) {
 5279                                         fnd = 1;
 5280                                         break;
 5281                                 }
 5282                         }
 5283 #endif
 5284                 }
 5285         }
 5286         if (fnd == 0) {
 5287                 /* New address added! no need to look futher. */
 5288                 return (1);
 5289         }
 5290         /* Ok so far lets munge through the rest of the packet */
 5291         offset += sizeof(struct sctp_init_chunk);
 5292         phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
 5293         while (phdr) {
 5294                 sa_touse = NULL;
 5295                 ptype = ntohs(phdr->param_type);
 5296                 plen = ntohs(phdr->param_length);
 5297                 switch (ptype) {
 5298 #ifdef INET
 5299                 case SCTP_IPV4_ADDRESS:
 5300                         {
 5301                                 struct sctp_ipv4addr_param *p4, p4_buf;
 5302 
 5303                                 phdr = sctp_get_next_param(in_initpkt, offset,
 5304                                     (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
 5305                                 if (plen != sizeof(struct sctp_ipv4addr_param) ||
 5306                                     phdr == NULL) {
 5307                                         return (1);
 5308                                 }
 5309                                 p4 = (struct sctp_ipv4addr_param *)phdr;
 5310                                 sin4.sin_addr.s_addr = p4->addr;
 5311                                 sa_touse = (struct sockaddr *)&sin4;
 5312                                 break;
 5313                         }
 5314 #endif
 5315 #ifdef INET6
 5316                 case SCTP_IPV6_ADDRESS:
 5317                         {
 5318                                 struct sctp_ipv6addr_param *p6, p6_buf;
 5319 
 5320                                 phdr = sctp_get_next_param(in_initpkt, offset,
 5321                                     (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
 5322                                 if (plen != sizeof(struct sctp_ipv6addr_param) ||
 5323                                     phdr == NULL) {
 5324                                         return (1);
 5325                                 }
 5326                                 p6 = (struct sctp_ipv6addr_param *)phdr;
 5327                                 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
 5328                                     sizeof(p6->addr));
 5329                                 sa_touse = (struct sockaddr *)&sin6;
 5330                                 break;
 5331                         }
 5332 #endif
 5333                 default:
 5334                         sa_touse = NULL;
 5335                         break;
 5336                 }
 5337                 if (sa_touse) {
 5338                         /* ok, sa_touse points to one to check */
 5339                         fnd = 0;
 5340                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
 5341                                 sa = (struct sockaddr *)&net->ro._l_addr;
 5342                                 if (sa->sa_family != sa_touse->sa_family) {
 5343                                         continue;
 5344                                 }
 5345 #ifdef INET
 5346                                 if (sa->sa_family == AF_INET) {
 5347                                         sa4 = (struct sockaddr_in *)sa;
 5348                                         if (sa4->sin_addr.s_addr ==
 5349                                             sin4.sin_addr.s_addr) {
 5350                                                 fnd = 1;
 5351                                                 break;
 5352                                         }
 5353                                 }
 5354 #endif
 5355 #ifdef INET6
 5356                                 if (sa->sa_family == AF_INET6) {
 5357                                         sa6 = (struct sockaddr_in6 *)sa;
 5358                                         if (SCTP6_ARE_ADDR_EQUAL(
 5359                                             sa6, &sin6)) {
 5360                                                 fnd = 1;
 5361                                                 break;
 5362                                         }
 5363                                 }
 5364 #endif
 5365                         }
 5366                         if (!fnd) {
 5367                                 /* New addr added! no need to look further */
 5368                                 return (1);
 5369                         }
 5370                 }
 5371                 offset += SCTP_SIZE32(plen);
 5372                 phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
 5373         }
 5374         return (0);
 5375 }
 5376 
 5377 /*
 5378  * Given a MBUF chain that was sent into us containing an INIT. Build a
 5379  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
 5380  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
 5381  * message (i.e. the struct sctp_init_msg).
 5382  */
 5383 void
 5384 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
 5385     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
 5386     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
 5387 {
 5388         struct sctp_association *asoc;
 5389         struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
 5390         struct sctp_init_ack_chunk *initack;
 5391         struct sctp_adaptation_layer_indication *ali;
 5392         struct sctp_ecn_supported_param *ecn;
 5393         struct sctp_prsctp_supported_param *prsctp;
 5394         struct sctp_supported_chunk_types_param *pr_supported;
 5395         union sctp_sockstore store, store1, *over_addr;
 5396 
 5397 #ifdef INET
 5398         struct sockaddr_in *sin, *to_sin;
 5399 
 5400 #endif
 5401 #ifdef INET6
 5402         struct sockaddr_in6 *sin6, *to_sin6;
 5403 
 5404 #endif
 5405         struct ip *iph;
 5406 
 5407 #ifdef INET6
 5408         struct ip6_hdr *ip6;
 5409 
 5410 #endif
 5411         struct sockaddr *to;
 5412         struct sctp_state_cookie stc;
 5413         struct sctp_nets *net = NULL;
 5414         uint8_t *signature = NULL;
 5415         int cnt_inits_to = 0;
 5416         uint16_t his_limit, i_want;
 5417         int abort_flag, padval;
 5418         int num_ext;
 5419         int p_len;
 5420         int nat_friendly = 0;
 5421         struct socket *so;
 5422 
 5423         if (stcb)
 5424                 asoc = &stcb->asoc;
 5425         else
 5426                 asoc = NULL;
 5427         mp_last = NULL;
 5428         if ((asoc != NULL) &&
 5429             (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
 5430             (sctp_are_there_new_addresses(asoc, init_pkt, offset))) {
 5431                 /* new addresses, out of here in non-cookie-wait states */
 5432                 /*
 5433                  * Send a ABORT, we don't add the new address error clause
 5434                  * though we even set the T bit and copy in the 0 tag.. this
 5435                  * looks no different than if no listener was present.
 5436                  */
 5437                 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
 5438                 return;
 5439         }
 5440         abort_flag = 0;
 5441         op_err = sctp_arethere_unrecognized_parameters(init_pkt,
 5442             (offset + sizeof(struct sctp_init_chunk)),
 5443             &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
 5444         if (abort_flag) {
 5445 do_a_abort:
 5446                 sctp_send_abort(init_pkt, iphlen, sh,
 5447                     init_chk->init.initiate_tag, op_err, vrf_id, port);
 5448                 return;
 5449         }
 5450         m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
 5451         if (m == NULL) {
 5452                 /* No memory, INIT timer will re-attempt. */
 5453                 if (op_err)
 5454                         sctp_m_freem(op_err);
 5455                 return;
 5456         }
 5457         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
 5458 
 5459         /*
 5460          * We might not overwrite the identification[] completely and on
 5461          * some platforms time_entered will contain some padding. Therefore
 5462          * zero out the cookie to avoid putting uninitialized memory on the
 5463          * wire.
 5464          */
 5465         memset(&stc, 0, sizeof(struct sctp_state_cookie));
 5466 
 5467         /* the time I built cookie */
 5468         (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
 5469 
 5470         /* populate any tie tags */
 5471         if (asoc != NULL) {
 5472                 /* unlock before tag selections */
 5473                 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
 5474                 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
 5475                 stc.cookie_life = asoc->cookie_life;
 5476                 net = asoc->primary_destination;
 5477         } else {
 5478                 stc.tie_tag_my_vtag = 0;
 5479                 stc.tie_tag_peer_vtag = 0;
 5480                 /* life I will award this cookie */
 5481                 stc.cookie_life = inp->sctp_ep.def_cookie_life;
 5482         }
 5483 
 5484         /* copy in the ports for later check */
 5485         stc.myport = sh->dest_port;
 5486         stc.peerport = sh->src_port;
 5487 
 5488         /*
 5489          * If we wanted to honor cookie life extentions, we would add to
 5490          * stc.cookie_life. For now we should NOT honor any extension
 5491          */
 5492         stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
 5493         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 5494                 struct inpcb *in_inp;
 5495 
 5496                 /* Its a V6 socket */
 5497                 in_inp = (struct inpcb *)inp;
 5498                 stc.ipv6_addr_legal = 1;
 5499                 /* Now look at the binding flag to see if V4 will be legal */
 5500                 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
 5501                         stc.ipv4_addr_legal = 1;
 5502                 } else {
 5503                         /* V4 addresses are NOT legal on the association */
 5504                         stc.ipv4_addr_legal = 0;
 5505                 }
 5506         } else {
 5507                 /* Its a V4 socket, no - V6 */
 5508                 stc.ipv4_addr_legal = 1;
 5509                 stc.ipv6_addr_legal = 0;
 5510         }
 5511 
 5512 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
 5513         stc.ipv4_scope = 1;
 5514 #else
 5515         stc.ipv4_scope = 0;
 5516 #endif
 5517         /* now for scope setup */
 5518         memset((caddr_t)&store, 0, sizeof(store));
 5519         memset((caddr_t)&store1, 0, sizeof(store1));
 5520 #ifdef INET
 5521         sin = &store.sin;
 5522         to_sin = &store1.sin;
 5523 #endif
 5524 #ifdef INET6
 5525         sin6 = &store.sin6;
 5526         to_sin6 = &store1.sin6;
 5527 #endif
 5528         iph = mtod(init_pkt, struct ip *);
 5529         /* establish the to_addr's */
 5530         switch (iph->ip_v) {
 5531 #ifdef INET
 5532         case IPVERSION:
 5533                 to_sin->sin_port = sh->dest_port;
 5534                 to_sin->sin_family = AF_INET;
 5535                 to_sin->sin_len = sizeof(struct sockaddr_in);
 5536                 to_sin->sin_addr = iph->ip_dst;
 5537                 break;
 5538 #endif
 5539 #ifdef INET6
 5540         case IPV6_VERSION >> 4:
 5541                 ip6 = mtod(init_pkt, struct ip6_hdr *);
 5542                 to_sin6->sin6_addr = ip6->ip6_dst;
 5543                 to_sin6->sin6_scope_id = 0;
 5544                 to_sin6->sin6_port = sh->dest_port;
 5545                 to_sin6->sin6_family = AF_INET6;
 5546                 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
 5547                 break;
 5548 #endif
 5549         default:
 5550                 goto do_a_abort;
 5551                 break;
 5552         }
 5553 
 5554         if (net == NULL) {
 5555                 to = (struct sockaddr *)&store;
 5556                 switch (iph->ip_v) {
 5557 #ifdef INET
 5558                 case IPVERSION:
 5559                         {
 5560                                 sin->sin_family = AF_INET;
 5561                                 sin->sin_len = sizeof(struct sockaddr_in);
 5562                                 sin->sin_port = sh->src_port;
 5563                                 sin->sin_addr = iph->ip_src;
 5564                                 /* lookup address */
 5565                                 stc.address[0] = sin->sin_addr.s_addr;
 5566                                 stc.address[1] = 0;
 5567                                 stc.address[2] = 0;
 5568                                 stc.address[3] = 0;
 5569                                 stc.addr_type = SCTP_IPV4_ADDRESS;
 5570                                 /* local from address */
 5571                                 stc.laddress[0] = to_sin->sin_addr.s_addr;
 5572                                 stc.laddress[1] = 0;
 5573                                 stc.laddress[2] = 0;
 5574                                 stc.laddress[3] = 0;
 5575                                 stc.laddr_type = SCTP_IPV4_ADDRESS;
 5576                                 /* scope_id is only for v6 */
 5577                                 stc.scope_id = 0;
 5578 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
 5579                                 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
 5580                                         stc.ipv4_scope = 1;
 5581                                 }
 5582 #else
 5583                                 stc.ipv4_scope = 1;
 5584 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
 5585                                 /* Must use the address in this case */
 5586                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
 5587                                         stc.loopback_scope = 1;
 5588                                         stc.ipv4_scope = 1;
 5589                                         stc.site_scope = 1;
 5590                                         stc.local_scope = 0;
 5591                                 }
 5592                                 break;
 5593                         }
 5594 #endif
 5595 #ifdef INET6
 5596                 case IPV6_VERSION >> 4:
 5597                         {
 5598                                 ip6 = mtod(init_pkt, struct ip6_hdr *);
 5599                                 sin6->sin6_family = AF_INET6;
 5600                                 sin6->sin6_len = sizeof(struct sockaddr_in6);
 5601                                 sin6->sin6_port = sh->src_port;
 5602                                 sin6->sin6_addr = ip6->ip6_src;
 5603                                 /* lookup address */
 5604                                 memcpy(&stc.address, &sin6->sin6_addr,
 5605                                     sizeof(struct in6_addr));
 5606                                 sin6->sin6_scope_id = 0;
 5607                                 stc.addr_type = SCTP_IPV6_ADDRESS;
 5608                                 stc.scope_id = 0;
 5609                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
 5610                                         /*
 5611                                          * FIX ME: does this have scope from
 5612                                          * rcvif?
 5613                                          */
 5614                                         (void)sa6_recoverscope(sin6);
 5615                                         stc.scope_id = sin6->sin6_scope_id;
 5616                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
 5617                                         stc.loopback_scope = 1;
 5618                                         stc.local_scope = 0;
 5619                                         stc.site_scope = 1;
 5620                                         stc.ipv4_scope = 1;
 5621                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
 5622                                         /*
 5623                                          * If the new destination is a
 5624                                          * LINK_LOCAL we must have common
 5625                                          * both site and local scope. Don't
 5626                                          * set local scope though since we
 5627                                          * must depend on the source to be
 5628                                          * added implicitly. We cannot
 5629                                          * assure just because we share one
 5630                                          * link that all links are common.
 5631                                          */
 5632                                         stc.local_scope = 0;
 5633                                         stc.site_scope = 1;
 5634                                         stc.ipv4_scope = 1;
 5635                                         /*
 5636                                          * we start counting for the private
 5637                                          * address stuff at 1. since the
 5638                                          * link local we source from won't
 5639                                          * show up in our scoped count.
 5640                                          */
 5641                                         cnt_inits_to = 1;
 5642                                         /*
 5643                                          * pull out the scope_id from
 5644                                          * incoming pkt
 5645                                          */
 5646                                         /*
 5647                                          * FIX ME: does this have scope from
 5648                                          * rcvif?
 5649                                          */
 5650                                         (void)sa6_recoverscope(sin6);
 5651                                         stc.scope_id = sin6->sin6_scope_id;
 5652                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
 5653                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
 5654                                         /*
 5655                                          * If the new destination is
 5656                                          * SITE_LOCAL then we must have site
 5657                                          * scope in common.
 5658                                          */
 5659                                         stc.site_scope = 1;
 5660                                 }
 5661                                 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
 5662                                 stc.laddr_type = SCTP_IPV6_ADDRESS;
 5663                                 break;
 5664                         }
 5665 #endif
 5666                 default:
 5667                         /* TSNH */
 5668                         goto do_a_abort;
 5669                         break;
 5670                 }
 5671         } else {
 5672                 /* set the scope per the existing tcb */
 5673 
 5674 #ifdef INET6
 5675                 struct sctp_nets *lnet;
 5676 
 5677 #endif
 5678 
 5679                 stc.loopback_scope = asoc->loopback_scope;
 5680                 stc.ipv4_scope = asoc->ipv4_local_scope;
 5681                 stc.site_scope = asoc->site_scope;
 5682                 stc.local_scope = asoc->local_scope;
 5683 #ifdef INET6
 5684                 /* Why do we not consider IPv4 LL addresses? */
 5685                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
 5686                         if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
 5687                                 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
 5688                                         /*
 5689                                          * if we have a LL address, start
 5690                                          * counting at 1.
 5691                                          */
 5692                                         cnt_inits_to = 1;
 5693                                 }
 5694                         }
 5695                 }
 5696 #endif
 5697                 /* use the net pointer */
 5698                 to = (struct sockaddr *)&net->ro._l_addr;
 5699                 switch (to->sa_family) {
 5700 #ifdef INET
 5701                 case AF_INET:
 5702                         sin = (struct sockaddr_in *)to;
 5703                         stc.address[0] = sin->sin_addr.s_addr;
 5704                         stc.address[1] = 0;
 5705                         stc.address[2] = 0;
 5706                         stc.address[3] = 0;
 5707                         stc.addr_type = SCTP_IPV4_ADDRESS;
 5708                         if (net->src_addr_selected == 0) {
 5709                                 /*
 5710                                  * strange case here, the INIT should have
 5711                                  * did the selection.
 5712                                  */
 5713                                 net->ro._s_addr = sctp_source_address_selection(inp,
 5714                                     stcb, (sctp_route_t *) & net->ro,
 5715                                     net, 0, vrf_id);
 5716                                 if (net->ro._s_addr == NULL)
 5717                                         return;
 5718 
 5719                                 net->src_addr_selected = 1;
 5720 
 5721                         }
 5722                         stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
 5723                         stc.laddress[1] = 0;
 5724                         stc.laddress[2] = 0;
 5725                         stc.laddress[3] = 0;
 5726                         stc.laddr_type = SCTP_IPV4_ADDRESS;
 5727                         /* scope_id is only for v6 */
 5728                         stc.scope_id = 0;
 5729                         break;
 5730 #endif
 5731 #ifdef INET6
 5732                 case AF_INET6:
 5733                         sin6 = (struct sockaddr_in6 *)to;
 5734                         memcpy(&stc.address, &sin6->sin6_addr,
 5735                             sizeof(struct in6_addr));
 5736                         stc.addr_type = SCTP_IPV6_ADDRESS;
 5737                         stc.scope_id = sin6->sin6_scope_id;
 5738                         if (net->src_addr_selected == 0) {
 5739                                 /*
 5740                                  * strange case here, the INIT should have
 5741                                  * did the selection.
 5742                                  */
 5743                                 net->ro._s_addr = sctp_source_address_selection(inp,
 5744                                     stcb, (sctp_route_t *) & net->ro,
 5745                                     net, 0, vrf_id);
 5746                                 if (net->ro._s_addr == NULL)
 5747                                         return;
 5748 
 5749                                 net->src_addr_selected = 1;
 5750                         }
 5751                         memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
 5752                             sizeof(struct in6_addr));
 5753                         stc.laddr_type = SCTP_IPV6_ADDRESS;
 5754                         break;
 5755 #endif
 5756                 }
 5757         }
 5758         /* Now lets put the SCTP header in place */
 5759         initack = mtod(m, struct sctp_init_ack_chunk *);
 5760         /* Save it off for quick ref */
 5761         stc.peers_vtag = init_chk->init.initiate_tag;
 5762         /* who are we */
 5763         memcpy(stc.identification, SCTP_VERSION_STRING,
 5764             min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
 5765         memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
 5766         /* now the chunk header */
 5767         initack->ch.chunk_type = SCTP_INITIATION_ACK;
 5768         initack->ch.chunk_flags = 0;
 5769         /* fill in later from mbuf we build */
 5770         initack->ch.chunk_length = 0;
 5771         /* place in my tag */
 5772         if ((asoc != NULL) &&
 5773             ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
 5774             (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
 5775             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
 5776                 /* re-use the v-tags and init-seq here */
 5777                 initack->init.initiate_tag = htonl(asoc->my_vtag);
 5778                 initack->init.initial_tsn = htonl(asoc->init_seq_number);
 5779         } else {
 5780                 uint32_t vtag, itsn;
 5781 
 5782                 if (hold_inp_lock) {
 5783                         SCTP_INP_INCR_REF(inp);
 5784                         SCTP_INP_RUNLOCK(inp);
 5785                 }
 5786                 if (asoc) {
 5787                         atomic_add_int(&asoc->refcnt, 1);
 5788                         SCTP_TCB_UNLOCK(stcb);
 5789         new_tag:
 5790                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
 5791                         if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
 5792                                 /*
 5793                                  * Got a duplicate vtag on some guy behind a
 5794                                  * nat make sure we don't use it.
 5795                                  */
 5796                                 goto new_tag;
 5797                         }
 5798                         initack->init.initiate_tag = htonl(vtag);
 5799                         /* get a TSN to use too */
 5800                         itsn = sctp_select_initial_TSN(&inp->sctp_ep);
 5801                         initack->init.initial_tsn = htonl(itsn);
 5802                         SCTP_TCB_LOCK(stcb);
 5803                         atomic_add_int(&asoc->refcnt, -1);
 5804                 } else {
 5805                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
 5806                         initack->init.initiate_tag = htonl(vtag);
 5807                         /* get a TSN to use too */
 5808                         initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
 5809                 }
 5810                 if (hold_inp_lock) {
 5811                         SCTP_INP_RLOCK(inp);
 5812                         SCTP_INP_DECR_REF(inp);
 5813                 }
 5814         }
 5815         /* save away my tag to */
 5816         stc.my_vtag = initack->init.initiate_tag;
 5817 
 5818         /* set up some of the credits. */
 5819         so = inp->sctp_socket;
 5820         if (so == NULL) {
 5821                 /* memory problem */
 5822                 sctp_m_freem(m);
 5823                 return;
 5824         } else {
 5825                 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
 5826         }
 5827         /* set what I want */
 5828         his_limit = ntohs(init_chk->init.num_inbound_streams);
 5829         /* choose what I want */
 5830         if (asoc != NULL) {
 5831                 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
 5832                         i_want = asoc->streamoutcnt;
 5833                 } else {
 5834                         i_want = inp->sctp_ep.pre_open_stream_count;
 5835                 }
 5836         } else {
 5837                 i_want = inp->sctp_ep.pre_open_stream_count;
 5838         }
 5839         if (his_limit < i_want) {
 5840                 /* I Want more :< */
 5841                 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
 5842         } else {
 5843                 /* I can have what I want :> */
 5844                 initack->init.num_outbound_streams = htons(i_want);
 5845         }
 5846         /* tell him his limt. */
 5847         initack->init.num_inbound_streams =
 5848             htons(inp->sctp_ep.max_open_streams_intome);
 5849 
 5850         /* adaptation layer indication parameter */
 5851         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
 5852         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
 5853         ali->ph.param_length = htons(sizeof(*ali));
 5854         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
 5855         SCTP_BUF_LEN(m) += sizeof(*ali);
 5856         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
 5857 
 5858         /* ECN parameter */
 5859         if (((asoc != NULL) && (asoc->ecn_allowed == 1)) ||
 5860             (inp->sctp_ecn_enable == 1)) {
 5861                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
 5862                 ecn->ph.param_length = htons(sizeof(*ecn));
 5863                 SCTP_BUF_LEN(m) += sizeof(*ecn);
 5864 
 5865                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
 5866                     sizeof(*ecn));
 5867         } else {
 5868                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
 5869         }
 5870         /* And now tell the peer we do  pr-sctp */
 5871         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
 5872         prsctp->ph.param_length = htons(sizeof(*prsctp));
 5873         SCTP_BUF_LEN(m) += sizeof(*prsctp);
 5874         if (nat_friendly) {
 5875                 /* Add NAT friendly parameter */
 5876                 struct sctp_paramhdr *ph;
 5877 
 5878                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 5879                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
 5880                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
 5881                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
 5882         }
 5883         /* And now tell the peer we do all the extensions */
 5884         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 5885         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
 5886         num_ext = 0;
 5887         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
 5888         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
 5889         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
 5890         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
 5891         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
 5892         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
 5893                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
 5894         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
 5895                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
 5896         p_len = sizeof(*pr_supported) + num_ext;
 5897         pr_supported->ph.param_length = htons(p_len);
 5898         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
 5899         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 5900 
 5901         /* add authentication parameters */
 5902         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
 5903                 struct sctp_auth_random *randp;
 5904                 struct sctp_auth_hmac_algo *hmacs;
 5905                 struct sctp_auth_chunk_list *chunks;
 5906                 uint16_t random_len;
 5907 
 5908                 /* generate and add RANDOM parameter */
 5909                 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
 5910                 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 5911                 randp->ph.param_type = htons(SCTP_RANDOM);
 5912                 p_len = sizeof(*randp) + random_len;
 5913                 randp->ph.param_length = htons(p_len);
 5914                 SCTP_READ_RANDOM(randp->random_data, random_len);
 5915                 /* zero out any padding required */
 5916                 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
 5917                 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 5918 
 5919                 /* add HMAC_ALGO parameter */
 5920                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 5921                 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
 5922                     (uint8_t *) hmacs->hmac_ids);
 5923                 if (p_len > 0) {
 5924                         p_len += sizeof(*hmacs);
 5925                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
 5926                         hmacs->ph.param_length = htons(p_len);
 5927                         /* zero out any padding required */
 5928                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
 5929                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 5930                 }
 5931                 /* add CHUNKS parameter */
 5932                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 5933                 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
 5934                     chunks->chunk_types);
 5935                 if (p_len > 0) {
 5936                         p_len += sizeof(*chunks);
 5937                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
 5938                         chunks->ph.param_length = htons(p_len);
 5939                         /* zero out any padding required */
 5940                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
 5941                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 5942                 }
 5943         }
 5944         m_at = m;
 5945         /* now the addresses */
 5946         {
 5947                 struct sctp_scoping scp;
 5948 
 5949                 /*
 5950                  * To optimize this we could put the scoping stuff into a
 5951                  * structure and remove the individual uint8's from the stc
 5952                  * structure. Then we could just sifa in the address within
 5953                  * the stc.. but for now this is a quick hack to get the
 5954                  * address stuff teased apart.
 5955                  */
 5956                 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
 5957                 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
 5958                 scp.loopback_scope = stc.loopback_scope;
 5959                 scp.ipv4_local_scope = stc.ipv4_scope;
 5960                 scp.local_scope = stc.local_scope;
 5961                 scp.site_scope = stc.site_scope;
 5962                 m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
 5963         }
 5964 
 5965         /* tack on the operational error if present */
 5966         if (op_err) {
 5967                 struct mbuf *ol;
 5968                 int llen;
 5969 
 5970                 llen = 0;
 5971                 ol = op_err;
 5972 
 5973                 while (ol) {
 5974                         llen += SCTP_BUF_LEN(ol);
 5975                         ol = SCTP_BUF_NEXT(ol);
 5976                 }
 5977                 if (llen % 4) {
 5978                         /* must add a pad to the param */
 5979                         uint32_t cpthis = 0;
 5980                         int padlen;
 5981 
 5982                         padlen = 4 - (llen % 4);
 5983                         m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
 5984                 }
 5985                 while (SCTP_BUF_NEXT(m_at) != NULL) {
 5986                         m_at = SCTP_BUF_NEXT(m_at);
 5987                 }
 5988                 SCTP_BUF_NEXT(m_at) = op_err;
 5989                 while (SCTP_BUF_NEXT(m_at) != NULL) {
 5990                         m_at = SCTP_BUF_NEXT(m_at);
 5991                 }
 5992         }
 5993         /* pre-calulate the size and update pkt header and chunk header */
 5994         p_len = 0;
 5995         for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
 5996                 p_len += SCTP_BUF_LEN(m_tmp);
 5997                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
 5998                         /* m_tmp should now point to last one */
 5999                         break;
 6000                 }
 6001         }
 6002 
 6003         /* Now we must build a cookie */
 6004         m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
 6005         if (m_cookie == NULL) {
 6006                 /* memory problem */
 6007                 sctp_m_freem(m);
 6008                 return;
 6009         }
 6010         /* Now append the cookie to the end and update the space/size */
 6011         SCTP_BUF_NEXT(m_tmp) = m_cookie;
 6012 
 6013         for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
 6014                 p_len += SCTP_BUF_LEN(m_tmp);
 6015                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
 6016                         /* m_tmp should now point to last one */
 6017                         mp_last = m_tmp;
 6018                         break;
 6019                 }
 6020         }
 6021         /*
 6022          * Place in the size, but we don't include the last pad (if any) in
 6023          * the INIT-ACK.
 6024          */
 6025         initack->ch.chunk_length = htons(p_len);
 6026 
 6027         /*
 6028          * Time to sign the cookie, we don't sign over the cookie signature
 6029          * though thus we set trailer.
 6030          */
 6031         (void)sctp_hmac_m(SCTP_HMAC,
 6032             (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
 6033             SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
 6034             (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
 6035         /*
 6036          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
 6037          * here since the timer will drive a retranmission.
 6038          */
 6039         padval = p_len % 4;
 6040         if ((padval) && (mp_last)) {
 6041                 /* see my previous comments on mp_last */
 6042                 if (sctp_add_pad_tombuf(mp_last, (4 - padval))) {
 6043                         /* Houston we have a problem, no space */
 6044                         sctp_m_freem(m);
 6045                         return;
 6046                 }
 6047         }
 6048         if (stc.loopback_scope) {
 6049                 over_addr = &store1;
 6050         } else {
 6051                 over_addr = NULL;
 6052         }
 6053 
 6054         (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
 6055             0, 0,
 6056             inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
 6057             port, SCTP_SO_NOT_LOCKED, over_addr, init_pkt);
 6058         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 6059 }
 6060 
 6061 
 6062 static void
 6063 sctp_prune_prsctp(struct sctp_tcb *stcb,
 6064     struct sctp_association *asoc,
 6065     struct sctp_sndrcvinfo *srcv,
 6066     int dataout)
 6067 {
 6068         int freed_spc = 0;
 6069         struct sctp_tmit_chunk *chk, *nchk;
 6070 
 6071         SCTP_TCB_LOCK_ASSERT(stcb);
 6072         if ((asoc->peer_supports_prsctp) &&
 6073             (asoc->sent_queue_cnt_removeable > 0)) {
 6074                 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
 6075                         /*
 6076                          * Look for chunks marked with the PR_SCTP flag AND
 6077                          * the buffer space flag. If the one being sent is
 6078                          * equal or greater priority then purge the old one
 6079                          * and free some space.
 6080                          */
 6081                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
 6082                                 /*
 6083                                  * This one is PR-SCTP AND buffer space
 6084                                  * limited type
 6085                                  */
 6086                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
 6087                                         /*
 6088                                          * Lower numbers equates to higher
 6089                                          * priority so if the one we are
 6090                                          * looking at has a larger or equal
 6091                                          * priority we want to drop the data
 6092                                          * and NOT retransmit it.
 6093                                          */
 6094                                         if (chk->data) {
 6095                                                 /*
 6096                                                  * We release the book_size
 6097                                                  * if the mbuf is here
 6098                                                  */
 6099                                                 int ret_spc;
 6100                                                 int cause;
 6101 
 6102                                                 if (chk->sent > SCTP_DATAGRAM_UNSENT)
 6103                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
 6104                                                 else
 6105                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
 6106                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
 6107                                                     cause,
 6108                                                     SCTP_SO_LOCKED);
 6109                                                 freed_spc += ret_spc;
 6110                                                 if (freed_spc >= dataout) {
 6111                                                         return;
 6112                                                 }
 6113                                         }       /* if chunk was present */
 6114                                 }       /* if of sufficent priority */
 6115                         }       /* if chunk has enabled */
 6116                 }               /* tailqforeach */
 6117 
 6118                 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
 6119                         /* Here we must move to the sent queue and mark */
 6120                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
 6121                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
 6122                                         if (chk->data) {
 6123                                                 /*
 6124                                                  * We release the book_size
 6125                                                  * if the mbuf is here
 6126                                                  */
 6127                                                 int ret_spc;
 6128 
 6129                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
 6130                                                     SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
 6131                                                     SCTP_SO_LOCKED);
 6132 
 6133                                                 freed_spc += ret_spc;
 6134                                                 if (freed_spc >= dataout) {
 6135                                                         return;
 6136                                                 }
 6137                                         }       /* end if chk->data */
 6138                                 }       /* end if right class */
 6139                         }       /* end if chk pr-sctp */
 6140                 }               /* tailqforeachsafe (chk) */
 6141         }                       /* if enabled in asoc */
 6142 }
 6143 
 6144 int
 6145 sctp_get_frag_point(struct sctp_tcb *stcb,
 6146     struct sctp_association *asoc)
 6147 {
 6148         int siz, ovh;
 6149 
 6150         /*
 6151          * For endpoints that have both v6 and v4 addresses we must reserve
 6152          * room for the ipv6 header, for those that are only dealing with V4
 6153          * we use a larger frag point.
 6154          */
 6155         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 6156                 ovh = SCTP_MED_OVERHEAD;
 6157         } else {
 6158                 ovh = SCTP_MED_V4_OVERHEAD;
 6159         }
 6160 
 6161         if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
 6162                 siz = asoc->smallest_mtu - ovh;
 6163         else
 6164                 siz = (stcb->asoc.sctp_frag_point - ovh);
 6165         /*
 6166          * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
 6167          */
 6168         /* A data chunk MUST fit in a cluster */
 6169         /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
 6170         /* } */
 6171 
 6172         /* adjust for an AUTH chunk if DATA requires auth */
 6173         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
 6174                 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
 6175 
 6176         if (siz % 4) {
 6177                 /* make it an even word boundary please */
 6178                 siz -= (siz % 4);
 6179         }
 6180         return (siz);
 6181 }
 6182 
 6183 static void
 6184 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
 6185 {
 6186         sp->pr_sctp_on = 0;
 6187         /*
 6188          * We assume that the user wants PR_SCTP_TTL if the user provides a
 6189          * positive lifetime but does not specify any PR_SCTP policy. This
 6190          * is a BAD assumption and causes problems at least with the
 6191          * U-Vancovers MPI folks. I will change this to be no policy means
 6192          * NO PR-SCTP.
 6193          */
 6194         if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
 6195                 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
 6196                 sp->pr_sctp_on = 1;
 6197         } else {
 6198                 return;
 6199         }
 6200         switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
 6201         case CHUNK_FLAGS_PR_SCTP_BUF:
 6202                 /*
 6203                  * Time to live is a priority stored in tv_sec when doing
 6204                  * the buffer drop thing.
 6205                  */
 6206                 sp->ts.tv_sec = sp->timetolive;
 6207                 sp->ts.tv_usec = 0;
 6208                 break;
 6209         case CHUNK_FLAGS_PR_SCTP_TTL:
 6210                 {
 6211                         struct timeval tv;
 6212 
 6213                         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
 6214                         tv.tv_sec = sp->timetolive / 1000;
 6215                         tv.tv_usec = (sp->timetolive * 1000) % 1000000;
 6216                         /*
 6217                          * TODO sctp_constants.h needs alternative time
 6218                          * macros when _KERNEL is undefined.
 6219                          */
 6220                         timevaladd(&sp->ts, &tv);
 6221                 }
 6222                 break;
 6223         case CHUNK_FLAGS_PR_SCTP_RTX:
 6224                 /*
 6225                  * Time to live is a the number or retransmissions stored in
 6226                  * tv_sec.
 6227                  */
 6228                 sp->ts.tv_sec = sp->timetolive;
 6229                 sp->ts.tv_usec = 0;
 6230                 break;
 6231         default:
 6232                 SCTPDBG(SCTP_DEBUG_USRREQ1,
 6233                     "Unknown PR_SCTP policy %u.\n",
 6234                     PR_SCTP_POLICY(sp->sinfo_flags));
 6235                 break;
 6236         }
 6237 }
 6238 
 6239 static int
 6240 sctp_msg_append(struct sctp_tcb *stcb,
 6241     struct sctp_nets *net,
 6242     struct mbuf *m,
 6243     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
 6244 {
 6245         int error = 0;
 6246         struct mbuf *at;
 6247         struct sctp_stream_queue_pending *sp = NULL;
 6248         struct sctp_stream_out *strm;
 6249 
 6250         /*
 6251          * Given an mbuf chain, put it into the association send queue and
 6252          * place it on the wheel
 6253          */
 6254         if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
 6255                 /* Invalid stream number */
 6256                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 6257                 error = EINVAL;
 6258                 goto out_now;
 6259         }
 6260         if ((stcb->asoc.stream_locked) &&
 6261             (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
 6262                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 6263                 error = EINVAL;
 6264                 goto out_now;
 6265         }
 6266         strm = &stcb->asoc.strmout[srcv->sinfo_stream];
 6267         /* Now can we send this? */
 6268         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
 6269             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
 6270             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
 6271             (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
 6272                 /* got data while shutting down */
 6273                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
 6274                 error = ECONNRESET;
 6275                 goto out_now;
 6276         }
 6277         sctp_alloc_a_strmoq(stcb, sp);
 6278         if (sp == NULL) {
 6279                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 6280                 error = ENOMEM;
 6281                 goto out_now;
 6282         }
 6283         sp->sinfo_flags = srcv->sinfo_flags;
 6284         sp->timetolive = srcv->sinfo_timetolive;
 6285         sp->ppid = srcv->sinfo_ppid;
 6286         sp->context = srcv->sinfo_context;
 6287         sp->strseq = 0;
 6288         if (sp->sinfo_flags & SCTP_ADDR_OVER) {
 6289                 sp->net = net;
 6290                 atomic_add_int(&sp->net->ref_count, 1);
 6291         } else {
 6292                 sp->net = NULL;
 6293         }
 6294         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
 6295         sp->stream = srcv->sinfo_stream;
 6296         sp->msg_is_complete = 1;
 6297         sp->sender_all_done = 1;
 6298         sp->some_taken = 0;
 6299         sp->data = m;
 6300         sp->tail_mbuf = NULL;
 6301         sctp_set_prsctp_policy(sp);
 6302         /*
 6303          * We could in theory (for sendall) sifa the length in, but we would
 6304          * still have to hunt through the chain since we need to setup the
 6305          * tail_mbuf
 6306          */
 6307         sp->length = 0;
 6308         for (at = m; at; at = SCTP_BUF_NEXT(at)) {
 6309                 if (SCTP_BUF_NEXT(at) == NULL)
 6310                         sp->tail_mbuf = at;
 6311                 sp->length += SCTP_BUF_LEN(at);
 6312         }
 6313         if (srcv->sinfo_keynumber_valid) {
 6314                 sp->auth_keyid = srcv->sinfo_keynumber;
 6315         } else {
 6316                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
 6317         }
 6318         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
 6319                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
 6320                 sp->holds_key_ref = 1;
 6321         }
 6322         if (hold_stcb_lock == 0) {
 6323                 SCTP_TCB_SEND_LOCK(stcb);
 6324         }
 6325         sctp_snd_sb_alloc(stcb, sp->length);
 6326         atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
 6327         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
 6328         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
 6329                 sp->strseq = strm->next_sequence_sent;
 6330                 strm->next_sequence_sent++;
 6331         }
 6332         stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1);
 6333         m = NULL;
 6334         if (hold_stcb_lock == 0) {
 6335                 SCTP_TCB_SEND_UNLOCK(stcb);
 6336         }
 6337 out_now:
 6338         if (m) {
 6339                 sctp_m_freem(m);
 6340         }
 6341         return (error);
 6342 }
 6343 
 6344 
 6345 static struct mbuf *
 6346 sctp_copy_mbufchain(struct mbuf *clonechain,
 6347     struct mbuf *outchain,
 6348     struct mbuf **endofchain,
 6349     int can_take_mbuf,
 6350     int sizeofcpy,
 6351     uint8_t copy_by_ref)
 6352 {
 6353         struct mbuf *m;
 6354         struct mbuf *appendchain;
 6355         caddr_t cp;
 6356         int len;
 6357 
 6358         if (endofchain == NULL) {
 6359                 /* error */
 6360 error_out:
 6361                 if (outchain)
 6362                         sctp_m_freem(outchain);
 6363                 return (NULL);
 6364         }
 6365         if (can_take_mbuf) {
 6366                 appendchain = clonechain;
 6367         } else {
 6368                 if (!copy_by_ref &&
 6369                     (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
 6370                     ) {
 6371                         /* Its not in a cluster */
 6372                         if (*endofchain == NULL) {
 6373                                 /* lets get a mbuf cluster */
 6374                                 if (outchain == NULL) {
 6375                                         /* This is the general case */
 6376                         new_mbuf:
 6377                                         outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
 6378                                         if (outchain == NULL) {
 6379                                                 goto error_out;
 6380                                         }
 6381                                         SCTP_BUF_LEN(outchain) = 0;
 6382                                         *endofchain = outchain;
 6383                                         /* get the prepend space */
 6384                                         SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
 6385                                 } else {
 6386                                         /*
 6387                                          * We really should not get a NULL
 6388                                          * in endofchain
 6389                                          */
 6390                                         /* find end */
 6391                                         m = outchain;
 6392                                         while (m) {
 6393                                                 if (SCTP_BUF_NEXT(m) == NULL) {
 6394                                                         *endofchain = m;
 6395                                                         break;
 6396                                                 }
 6397                                                 m = SCTP_BUF_NEXT(m);
 6398                                         }
 6399                                         /* sanity */
 6400                                         if (*endofchain == NULL) {
 6401                                                 /*
 6402                                                  * huh, TSNH XXX maybe we
 6403                                                  * should panic
 6404                                                  */
 6405                                                 sctp_m_freem(outchain);
 6406                                                 goto new_mbuf;
 6407                                         }
 6408                                 }
 6409                                 /* get the new end of length */
 6410                                 len = M_TRAILINGSPACE(*endofchain);
 6411                         } else {
 6412                                 /* how much is left at the end? */
 6413                                 len = M_TRAILINGSPACE(*endofchain);
 6414                         }
 6415                         /* Find the end of the data, for appending */
 6416                         cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
 6417 
 6418                         /* Now lets copy it out */
 6419                         if (len >= sizeofcpy) {
 6420                                 /* It all fits, copy it in */
 6421                                 m_copydata(clonechain, 0, sizeofcpy, cp);
 6422                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
 6423                         } else {
 6424                                 /* fill up the end of the chain */
 6425                                 if (len > 0) {
 6426                                         m_copydata(clonechain, 0, len, cp);
 6427                                         SCTP_BUF_LEN((*endofchain)) += len;
 6428                                         /* now we need another one */
 6429                                         sizeofcpy -= len;
 6430                                 }
 6431                                 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
 6432                                 if (m == NULL) {
 6433                                         /* We failed */
 6434                                         goto error_out;
 6435                                 }
 6436                                 SCTP_BUF_NEXT((*endofchain)) = m;
 6437                                 *endofchain = m;
 6438                                 cp = mtod((*endofchain), caddr_t);
 6439                                 m_copydata(clonechain, len, sizeofcpy, cp);
 6440                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
 6441                         }
 6442                         return (outchain);
 6443                 } else {
 6444                         /* copy the old fashion way */
 6445                         appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
 6446 #ifdef SCTP_MBUF_LOGGING
 6447                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 6448                                 struct mbuf *mat;
 6449 
 6450                                 mat = appendchain;
 6451                                 while (mat) {
 6452                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
 6453                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 6454                                         }
 6455                                         mat = SCTP_BUF_NEXT(mat);
 6456                                 }
 6457                         }
 6458 #endif
 6459                 }
 6460         }
 6461         if (appendchain == NULL) {
 6462                 /* error */
 6463                 if (outchain)
 6464                         sctp_m_freem(outchain);
 6465                 return (NULL);
 6466         }
 6467         if (outchain) {
 6468                 /* tack on to the end */
 6469                 if (*endofchain != NULL) {
 6470                         SCTP_BUF_NEXT(((*endofchain))) = appendchain;
 6471                 } else {
 6472                         m = outchain;
 6473                         while (m) {
 6474                                 if (SCTP_BUF_NEXT(m) == NULL) {
 6475                                         SCTP_BUF_NEXT(m) = appendchain;
 6476                                         break;
 6477                                 }
 6478                                 m = SCTP_BUF_NEXT(m);
 6479                         }
 6480                 }
 6481                 /*
 6482                  * save off the end and update the end-chain postion
 6483                  */
 6484                 m = appendchain;
 6485                 while (m) {
 6486                         if (SCTP_BUF_NEXT(m) == NULL) {
 6487                                 *endofchain = m;
 6488                                 break;
 6489                         }
 6490                         m = SCTP_BUF_NEXT(m);
 6491                 }
 6492                 return (outchain);
 6493         } else {
 6494                 /* save off the end and update the end-chain postion */
 6495                 m = appendchain;
 6496                 while (m) {
 6497                         if (SCTP_BUF_NEXT(m) == NULL) {
 6498                                 *endofchain = m;
 6499                                 break;
 6500                         }
 6501                         m = SCTP_BUF_NEXT(m);
 6502                 }
 6503                 return (appendchain);
 6504         }
 6505 }
 6506 
 6507 int
 6508 sctp_med_chunk_output(struct sctp_inpcb *inp,
 6509     struct sctp_tcb *stcb,
 6510     struct sctp_association *asoc,
 6511     int *num_out,
 6512     int *reason_code,
 6513     int control_only, int from_where,
 6514     struct timeval *now, int *now_filled, int frag_point, int so_locked
 6515 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 6516     SCTP_UNUSED
 6517 #endif
 6518 );
 6519 
 6520 static void
 6521 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
 6522     uint32_t val SCTP_UNUSED)
 6523 {
 6524         struct sctp_copy_all *ca;
 6525         struct mbuf *m;
 6526         int ret = 0;
 6527         int added_control = 0;
 6528         int un_sent, do_chunk_output = 1;
 6529         struct sctp_association *asoc;
 6530         struct sctp_nets *net;
 6531 
 6532         ca = (struct sctp_copy_all *)ptr;
 6533         if (ca->m == NULL) {
 6534                 return;
 6535         }
 6536         if (ca->inp != inp) {
 6537                 /* TSNH */
 6538                 return;
 6539         }
 6540         if ((ca->m) && ca->sndlen) {
 6541                 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
 6542                 if (m == NULL) {
 6543                         /* can't copy so we are done */
 6544                         ca->cnt_failed++;
 6545                         return;
 6546                 }
 6547 #ifdef SCTP_MBUF_LOGGING
 6548                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 6549                         struct mbuf *mat;
 6550 
 6551                         mat = m;
 6552                         while (mat) {
 6553                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
 6554                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 6555                                 }
 6556                                 mat = SCTP_BUF_NEXT(mat);
 6557                         }
 6558                 }
 6559 #endif
 6560         } else {
 6561                 m = NULL;
 6562         }
 6563         SCTP_TCB_LOCK_ASSERT(stcb);
 6564         if (stcb->asoc.alternate) {
 6565                 net = stcb->asoc.alternate;
 6566         } else {
 6567                 net = stcb->asoc.primary_destination;
 6568         }
 6569         if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
 6570                 /* Abort this assoc with m as the user defined reason */
 6571                 if (m) {
 6572                         struct sctp_paramhdr *ph;
 6573 
 6574                         SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
 6575                         if (m) {
 6576                                 ph = mtod(m, struct sctp_paramhdr *);
 6577                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
 6578                                 ph->param_length = htons(ca->sndlen);
 6579                         }
 6580                         /*
 6581                          * We add one here to keep the assoc from
 6582                          * dis-appearing on us.
 6583                          */
 6584                         atomic_add_int(&stcb->asoc.refcnt, 1);
 6585                         sctp_abort_an_association(inp, stcb,
 6586                             SCTP_RESPONSE_TO_USER_REQ,
 6587                             m, SCTP_SO_NOT_LOCKED);
 6588                         /*
 6589                          * sctp_abort_an_association calls sctp_free_asoc()
 6590                          * free association will NOT free it since we
 6591                          * incremented the refcnt .. we do this to prevent
 6592                          * it being freed and things getting tricky since we
 6593                          * could end up (from free_asoc) calling inpcb_free
 6594                          * which would get a recursive lock call to the
 6595                          * iterator lock.. But as a consequence of that the
 6596                          * stcb will return to us un-locked.. since
 6597                          * free_asoc returns with either no TCB or the TCB
 6598                          * unlocked, we must relock.. to unlock in the
 6599                          * iterator timer :-0
 6600                          */
 6601                         SCTP_TCB_LOCK(stcb);
 6602                         atomic_add_int(&stcb->asoc.refcnt, -1);
 6603                         goto no_chunk_output;
 6604                 }
 6605         } else {
 6606                 if (m) {
 6607                         ret = sctp_msg_append(stcb, net, m,
 6608                             &ca->sndrcv, 1);
 6609                 }
 6610                 asoc = &stcb->asoc;
 6611                 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
 6612                         /* shutdown this assoc */
 6613                         int cnt;
 6614 
 6615                         cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
 6616 
 6617                         if (TAILQ_EMPTY(&asoc->send_queue) &&
 6618                             TAILQ_EMPTY(&asoc->sent_queue) &&
 6619                             (cnt == 0)) {
 6620                                 if (asoc->locked_on_sending) {
 6621                                         goto abort_anyway;
 6622                                 }
 6623                                 /*
 6624                                  * there is nothing queued to send, so I'm
 6625                                  * done...
 6626                                  */
 6627                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
 6628                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
 6629                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
 6630                                         /*
 6631                                          * only send SHUTDOWN the first time
 6632                                          * through
 6633                                          */
 6634                                         sctp_send_shutdown(stcb, net);
 6635                                         if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
 6636                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
 6637                                         }
 6638                                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
 6639                                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
 6640                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
 6641                                             net);
 6642                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
 6643                                             asoc->primary_destination);
 6644                                         added_control = 1;
 6645                                         do_chunk_output = 0;
 6646                                 }
 6647                         } else {
 6648                                 /*
 6649                                  * we still got (or just got) data to send,
 6650                                  * so set SHUTDOWN_PENDING
 6651                                  */
 6652                                 /*
 6653                                  * XXX sockets draft says that SCTP_EOF
 6654                                  * should be sent with no data.  currently,
 6655                                  * we will allow user data to be sent first
 6656                                  * and move to SHUTDOWN-PENDING
 6657                                  */
 6658                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
 6659                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
 6660                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
 6661                                         if (asoc->locked_on_sending) {
 6662                                                 /*
 6663                                                  * Locked to send out the
 6664                                                  * data
 6665                                                  */
 6666                                                 struct sctp_stream_queue_pending *sp;
 6667 
 6668                                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
 6669                                                 if (sp) {
 6670                                                         if ((sp->length == 0) && (sp->msg_is_complete == 0))
 6671                                                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
 6672                                                 }
 6673                                         }
 6674                                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
 6675                                         if (TAILQ_EMPTY(&asoc->send_queue) &&
 6676                                             TAILQ_EMPTY(&asoc->sent_queue) &&
 6677                                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
 6678                                 abort_anyway:
 6679                                                 atomic_add_int(&stcb->asoc.refcnt, 1);
 6680                                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
 6681                                                     SCTP_RESPONSE_TO_USER_REQ,
 6682                                                     NULL, SCTP_SO_NOT_LOCKED);
 6683                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
 6684                                                 goto no_chunk_output;
 6685                                         }
 6686                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
 6687                                             asoc->primary_destination);
 6688                                 }
 6689                         }
 6690 
 6691                 }
 6692         }
 6693         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
 6694             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
 6695 
 6696         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
 6697             (stcb->asoc.total_flight > 0) &&
 6698             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
 6699             ) {
 6700                 do_chunk_output = 0;
 6701         }
 6702         if (do_chunk_output)
 6703                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
 6704         else if (added_control) {
 6705                 int num_out = 0, reason = 0, now_filled = 0;
 6706                 struct timeval now;
 6707                 int frag_point;
 6708 
 6709                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
 6710                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
 6711                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
 6712         }
 6713 no_chunk_output:
 6714         if (ret) {
 6715                 ca->cnt_failed++;
 6716         } else {
 6717                 ca->cnt_sent++;
 6718         }
 6719 }
 6720 
 6721 static void
 6722 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
 6723 {
 6724         struct sctp_copy_all *ca;
 6725 
 6726         ca = (struct sctp_copy_all *)ptr;
 6727         /*
 6728          * Do a notify here? Kacheong suggests that the notify be done at
 6729          * the send time.. so you would push up a notification if any send
 6730          * failed. Don't know if this is feasable since the only failures we
 6731          * have is "memory" related and if you cannot get an mbuf to send
 6732          * the data you surely can't get an mbuf to send up to notify the
 6733          * user you can't send the data :->
 6734          */
 6735 
 6736         /* now free everything */
 6737         sctp_m_freem(ca->m);
 6738         SCTP_FREE(ca, SCTP_M_COPYAL);
 6739 }
 6740 
 6741 
 6742 #define MC_ALIGN(m, len) do {                                           \
 6743         SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
 6744 } while (0)
 6745 
 6746 
 6747 
 6748 static struct mbuf *
 6749 sctp_copy_out_all(struct uio *uio, int len)
 6750 {
 6751         struct mbuf *ret, *at;
 6752         int left, willcpy, cancpy, error;
 6753 
 6754         ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
 6755         if (ret == NULL) {
 6756                 /* TSNH */
 6757                 return (NULL);
 6758         }
 6759         left = len;
 6760         SCTP_BUF_LEN(ret) = 0;
 6761         /* save space for the data chunk header */
 6762         cancpy = M_TRAILINGSPACE(ret);
 6763         willcpy = min(cancpy, left);
 6764         at = ret;
 6765         while (left > 0) {
 6766                 /* Align data to the end */
 6767                 error = uiomove(mtod(at, caddr_t), willcpy, uio);
 6768                 if (error) {
 6769         err_out_now:
 6770                         sctp_m_freem(at);
 6771                         return (NULL);
 6772                 }
 6773                 SCTP_BUF_LEN(at) = willcpy;
 6774                 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
 6775                 left -= willcpy;
 6776                 if (left > 0) {
 6777                         SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
 6778                         if (SCTP_BUF_NEXT(at) == NULL) {
 6779                                 goto err_out_now;
 6780                         }
 6781                         at = SCTP_BUF_NEXT(at);
 6782                         SCTP_BUF_LEN(at) = 0;
 6783                         cancpy = M_TRAILINGSPACE(at);
 6784                         willcpy = min(cancpy, left);
 6785                 }
 6786         }
 6787         return (ret);
 6788 }
 6789 
 6790 static int
 6791 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
 6792     struct sctp_sndrcvinfo *srcv)
 6793 {
 6794         int ret;
 6795         struct sctp_copy_all *ca;
 6796 
 6797         SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
 6798             SCTP_M_COPYAL);
 6799         if (ca == NULL) {
 6800                 sctp_m_freem(m);
 6801                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 6802                 return (ENOMEM);
 6803         }
 6804         memset(ca, 0, sizeof(struct sctp_copy_all));
 6805 
 6806         ca->inp = inp;
 6807         if (srcv) {
 6808                 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
 6809         }
 6810         /*
 6811          * take off the sendall flag, it would be bad if we failed to do
 6812          * this :-0
 6813          */
 6814         ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
 6815         /* get length and mbuf chain */
 6816         if (uio) {
 6817                 ca->sndlen = uio->uio_resid;
 6818                 ca->m = sctp_copy_out_all(uio, ca->sndlen);
 6819                 if (ca->m == NULL) {
 6820                         SCTP_FREE(ca, SCTP_M_COPYAL);
 6821                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 6822                         return (ENOMEM);
 6823                 }
 6824         } else {
 6825                 /* Gather the length of the send */
 6826                 struct mbuf *mat;
 6827 
 6828                 mat = m;
 6829                 ca->sndlen = 0;
 6830                 while (m) {
 6831                         ca->sndlen += SCTP_BUF_LEN(m);
 6832                         m = SCTP_BUF_NEXT(m);
 6833                 }
 6834                 ca->m = mat;
 6835         }
 6836         ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
 6837             SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
 6838             SCTP_ASOC_ANY_STATE,
 6839             (void *)ca, 0,
 6840             sctp_sendall_completes, inp, 1);
 6841         if (ret) {
 6842                 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
 6843                 SCTP_FREE(ca, SCTP_M_COPYAL);
 6844                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
 6845                 return (EFAULT);
 6846         }
 6847         return (0);
 6848 }
 6849 
 6850 
 6851 void
 6852 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
 6853 {
 6854         struct sctp_tmit_chunk *chk, *nchk;
 6855 
 6856         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
 6857                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
 6858                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
 6859                         if (chk->data) {
 6860                                 sctp_m_freem(chk->data);
 6861                                 chk->data = NULL;
 6862                         }
 6863                         asoc->ctrl_queue_cnt--;
 6864                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
 6865                 }
 6866         }
 6867 }
 6868 
 6869 void
 6870 sctp_toss_old_asconf(struct sctp_tcb *stcb)
 6871 {
 6872         struct sctp_association *asoc;
 6873         struct sctp_tmit_chunk *chk, *nchk;
 6874         struct sctp_asconf_chunk *acp;
 6875 
 6876         asoc = &stcb->asoc;
 6877         TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
 6878                 /* find SCTP_ASCONF chunk in queue */
 6879                 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
 6880                         if (chk->data) {
 6881                                 acp = mtod(chk->data, struct sctp_asconf_chunk *);
 6882                                 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
 6883                                         /* Not Acked yet */
 6884                                         break;
 6885                                 }
 6886                         }
 6887                         TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
 6888                         if (chk->data) {
 6889                                 sctp_m_freem(chk->data);
 6890                                 chk->data = NULL;
 6891                         }
 6892                         asoc->ctrl_queue_cnt--;
 6893                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
 6894                 }
 6895         }
 6896 }
 6897 
 6898 
 6899 static void
 6900 sctp_clean_up_datalist(struct sctp_tcb *stcb,
 6901     struct sctp_association *asoc,
 6902     struct sctp_tmit_chunk **data_list,
 6903     int bundle_at,
 6904     struct sctp_nets *net)
 6905 {
 6906         int i;
 6907         struct sctp_tmit_chunk *tp1;
 6908 
 6909         for (i = 0; i < bundle_at; i++) {
 6910                 /* off of the send queue */
 6911                 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
 6912                 asoc->send_queue_cnt--;
 6913                 if (i > 0) {
 6914                         /*
 6915                          * Any chunk NOT 0 you zap the time chunk 0 gets
 6916                          * zapped or set based on if a RTO measurment is
 6917                          * needed.
 6918                          */
 6919                         data_list[i]->do_rtt = 0;
 6920                 }
 6921                 /* record time */
 6922                 data_list[i]->sent_rcv_time = net->last_sent_time;
 6923                 data_list[i]->rec.data.cwnd_at_send = net->cwnd;
 6924                 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
 6925                 if (data_list[i]->whoTo == NULL) {
 6926                         data_list[i]->whoTo = net;
 6927                         atomic_add_int(&net->ref_count, 1);
 6928                 }
 6929                 /* on to the sent queue */
 6930                 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
 6931                 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
 6932                         struct sctp_tmit_chunk *tpp;
 6933 
 6934                         /* need to move back */
 6935         back_up_more:
 6936                         tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
 6937                         if (tpp == NULL) {
 6938                                 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
 6939                                 goto all_done;
 6940                         }
 6941                         tp1 = tpp;
 6942                         if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
 6943                                 goto back_up_more;
 6944                         }
 6945                         TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
 6946                 } else {
 6947                         TAILQ_INSERT_TAIL(&asoc->sent_queue,
 6948                             data_list[i],
 6949                             sctp_next);
 6950                 }
 6951 all_done:
 6952                 /* This does not lower until the cum-ack passes it */
 6953                 asoc->sent_queue_cnt++;
 6954                 if ((asoc->peers_rwnd <= 0) &&
 6955                     (asoc->total_flight == 0) &&
 6956                     (bundle_at == 1)) {
 6957                         /* Mark the chunk as being a window probe */
 6958                         SCTP_STAT_INCR(sctps_windowprobed);
 6959                 }
 6960 #ifdef SCTP_AUDITING_ENABLED
 6961                 sctp_audit_log(0xC2, 3);
 6962 #endif
 6963                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
 6964                 data_list[i]->snd_count = 1;
 6965                 data_list[i]->rec.data.chunk_was_revoked = 0;
 6966                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
 6967                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
 6968                             data_list[i]->whoTo->flight_size,
 6969                             data_list[i]->book_size,
 6970                             (uintptr_t) data_list[i]->whoTo,
 6971                             data_list[i]->rec.data.TSN_seq);
 6972                 }
 6973                 sctp_flight_size_increase(data_list[i]);
 6974                 sctp_total_flight_increase(stcb, data_list[i]);
 6975                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
 6976                         sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
 6977                             asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
 6978                 }
 6979                 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
 6980                     (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
 6981                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
 6982                         /* SWS sender side engages */
 6983                         asoc->peers_rwnd = 0;
 6984                 }
 6985         }
 6986         if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
 6987                 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
 6988         }
 6989 }
 6990 
 6991 static void
 6992 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked
 6993 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 6994     SCTP_UNUSED
 6995 #endif
 6996 )
 6997 {
 6998         struct sctp_tmit_chunk *chk, *nchk;
 6999 
 7000         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
 7001                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
 7002                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
 7003                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
 7004                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
 7005                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
 7006                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
 7007                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
 7008                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
 7009                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
 7010                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
 7011                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
 7012                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
 7013                         /* Stray chunks must be cleaned up */
 7014         clean_up_anyway:
 7015                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
 7016                         if (chk->data) {
 7017                                 sctp_m_freem(chk->data);
 7018                                 chk->data = NULL;
 7019                         }
 7020                         asoc->ctrl_queue_cnt--;
 7021                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
 7022                                 asoc->fwd_tsn_cnt--;
 7023                         sctp_free_a_chunk(stcb, chk, so_locked);
 7024                 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
 7025                         /* special handling, we must look into the param */
 7026                         if (chk != asoc->str_reset) {
 7027                                 goto clean_up_anyway;
 7028                         }
 7029                 }
 7030         }
 7031 }
 7032 
 7033 
 7034 static int
 7035 sctp_can_we_split_this(struct sctp_tcb *stcb,
 7036     uint32_t length,
 7037     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
 7038 {
 7039         /*
 7040          * Make a decision on if I should split a msg into multiple parts.
 7041          * This is only asked of incomplete messages.
 7042          */
 7043         if (eeor_on) {
 7044                 /*
 7045                  * If we are doing EEOR we need to always send it if its the
 7046                  * entire thing, since it might be all the guy is putting in
 7047                  * the hopper.
 7048                  */
 7049                 if (goal_mtu >= length) {
 7050                         /*-
 7051                          * If we have data outstanding,
 7052                          * we get another chance when the sack
 7053                          * arrives to transmit - wait for more data
 7054                          */
 7055                         if (stcb->asoc.total_flight == 0) {
 7056                                 /*
 7057                                  * If nothing is in flight, we zero the
 7058                                  * packet counter.
 7059                                  */
 7060                                 return (length);
 7061                         }
 7062                         return (0);
 7063 
 7064                 } else {
 7065                         /* You can fill the rest */
 7066                         return (goal_mtu);
 7067                 }
 7068         }
 7069         /*-
 7070          * For those strange folk that make the send buffer
 7071          * smaller than our fragmentation point, we can't
 7072          * get a full msg in so we have to allow splitting.
 7073          */
 7074         if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
 7075                 return (length);
 7076         }
 7077         if ((length <= goal_mtu) ||
 7078             ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
 7079                 /* Sub-optimial residual don't split in non-eeor mode. */
 7080                 return (0);
 7081         }
 7082         /*
 7083          * If we reach here length is larger than the goal_mtu. Do we wish
 7084          * to split it for the sake of packet putting together?
 7085          */
 7086         if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
 7087                 /* Its ok to split it */
 7088                 return (min(goal_mtu, frag_point));
 7089         }
 7090         /* Nope, can't split */
 7091         return (0);
 7092 
 7093 }
 7094 
 7095 static uint32_t
 7096 sctp_move_to_outqueue(struct sctp_tcb *stcb,
 7097     struct sctp_stream_out *strq,
 7098     uint32_t goal_mtu,
 7099     uint32_t frag_point,
 7100     int *locked,
 7101     int *giveup,
 7102     int eeor_mode,
 7103     int *bail,
 7104     int so_locked
 7105 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 7106     SCTP_UNUSED
 7107 #endif
 7108 )
 7109 {
 7110         /* Move from the stream to the send_queue keeping track of the total */
 7111         struct sctp_association *asoc;
 7112         struct sctp_stream_queue_pending *sp;
 7113         struct sctp_tmit_chunk *chk;
 7114         struct sctp_data_chunk *dchkh;
 7115         uint32_t to_move, length;
 7116         uint8_t rcv_flags = 0;
 7117         uint8_t some_taken;
 7118         uint8_t send_lock_up = 0;
 7119 
 7120         SCTP_TCB_LOCK_ASSERT(stcb);
 7121         asoc = &stcb->asoc;
 7122 one_more_time:
 7123         /* sa_ignore FREED_MEMORY */
 7124         sp = TAILQ_FIRST(&strq->outqueue);
 7125         if (sp == NULL) {
 7126                 *locked = 0;
 7127                 if (send_lock_up == 0) {
 7128                         SCTP_TCB_SEND_LOCK(stcb);
 7129                         send_lock_up = 1;
 7130                 }
 7131                 sp = TAILQ_FIRST(&strq->outqueue);
 7132                 if (sp) {
 7133                         goto one_more_time;
 7134                 }
 7135                 if (strq->last_msg_incomplete) {
 7136                         SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
 7137                             strq->stream_no,
 7138                             strq->last_msg_incomplete);
 7139                         strq->last_msg_incomplete = 0;
 7140                 }
 7141                 to_move = 0;
 7142                 if (send_lock_up) {
 7143                         SCTP_TCB_SEND_UNLOCK(stcb);
 7144                         send_lock_up = 0;
 7145                 }
 7146                 goto out_of;
 7147         }
 7148         if ((sp->msg_is_complete) && (sp->length == 0)) {
 7149                 if (sp->sender_all_done) {
 7150                         /*
 7151                          * We are doing differed cleanup. Last time through
 7152                          * when we took all the data the sender_all_done was
 7153                          * not set.
 7154                          */
 7155                         if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
 7156                                 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
 7157                                 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
 7158                                     sp->sender_all_done,
 7159                                     sp->length,
 7160                                     sp->msg_is_complete,
 7161                                     sp->put_last_out,
 7162                                     send_lock_up);
 7163                         }
 7164                         if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
 7165                                 SCTP_TCB_SEND_LOCK(stcb);
 7166                                 send_lock_up = 1;
 7167                         }
 7168                         atomic_subtract_int(&asoc->stream_queue_cnt, 1);
 7169                         TAILQ_REMOVE(&strq->outqueue, sp, next);
 7170                         stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
 7171                         if (sp->net) {
 7172                                 sctp_free_remote_addr(sp->net);
 7173                                 sp->net = NULL;
 7174                         }
 7175                         if (sp->data) {
 7176                                 sctp_m_freem(sp->data);
 7177                                 sp->data = NULL;
 7178                         }
 7179                         sctp_free_a_strmoq(stcb, sp, so_locked);
 7180                         /* we can't be locked to it */
 7181                         *locked = 0;
 7182                         stcb->asoc.locked_on_sending = NULL;
 7183                         if (send_lock_up) {
 7184                                 SCTP_TCB_SEND_UNLOCK(stcb);
 7185                                 send_lock_up = 0;
 7186                         }
 7187                         /* back to get the next msg */
 7188                         goto one_more_time;
 7189                 } else {
 7190                         /*
 7191                          * sender just finished this but still holds a
 7192                          * reference
 7193                          */
 7194                         *locked = 1;
 7195                         *giveup = 1;
 7196                         to_move = 0;
 7197                         goto out_of;
 7198                 }
 7199         } else {
 7200                 /* is there some to get */
 7201                 if (sp->length == 0) {
 7202                         /* no */
 7203                         *locked = 1;
 7204                         *giveup = 1;
 7205                         to_move = 0;
 7206                         goto out_of;
 7207                 } else if (sp->discard_rest) {
 7208                         if (send_lock_up == 0) {
 7209                                 SCTP_TCB_SEND_LOCK(stcb);
 7210                                 send_lock_up = 1;
 7211                         }
 7212                         /* Whack down the size */
 7213                         atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
 7214                         if ((stcb->sctp_socket != NULL) && \
 7215                             ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 7216                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
 7217                                 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
 7218                         }
 7219                         if (sp->data) {
 7220                                 sctp_m_freem(sp->data);
 7221                                 sp->data = NULL;
 7222                                 sp->tail_mbuf = NULL;
 7223                         }
 7224                         sp->length = 0;
 7225                         sp->some_taken = 1;
 7226                         *locked = 1;
 7227                         *giveup = 1;
 7228                         to_move = 0;
 7229                         goto out_of;
 7230                 }
 7231         }
 7232         some_taken = sp->some_taken;
 7233         if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
 7234                 sp->msg_is_complete = 1;
 7235         }
 7236 re_look:
 7237         length = sp->length;
 7238         if (sp->msg_is_complete) {
 7239                 /* The message is complete */
 7240                 to_move = min(length, frag_point);
 7241                 if (to_move == length) {
 7242                         /* All of it fits in the MTU */
 7243                         if (sp->some_taken) {
 7244                                 rcv_flags |= SCTP_DATA_LAST_FRAG;
 7245                                 sp->put_last_out = 1;
 7246                         } else {
 7247                                 rcv_flags |= SCTP_DATA_NOT_FRAG;
 7248                                 sp->put_last_out = 1;
 7249                         }
 7250                 } else {
 7251                         /* Not all of it fits, we fragment */
 7252                         if (sp->some_taken == 0) {
 7253                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
 7254                         }
 7255                         sp->some_taken = 1;
 7256                 }
 7257         } else {
 7258                 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
 7259                 if (to_move) {
 7260                         /*-
 7261                          * We use a snapshot of length in case it
 7262                          * is expanding during the compare.
 7263                          */
 7264                         uint32_t llen;
 7265 
 7266                         llen = length;
 7267                         if (to_move >= llen) {
 7268                                 to_move = llen;
 7269                                 if (send_lock_up == 0) {
 7270                                         /*-
 7271                                          * We are taking all of an incomplete msg
 7272                                          * thus we need a send lock.
 7273                                          */
 7274                                         SCTP_TCB_SEND_LOCK(stcb);
 7275                                         send_lock_up = 1;
 7276                                         if (sp->msg_is_complete) {
 7277                                                 /*
 7278                                                  * the sender finished the
 7279                                                  * msg
 7280                                                  */
 7281                                                 goto re_look;
 7282                                         }
 7283                                 }
 7284                         }
 7285                         if (sp->some_taken == 0) {
 7286                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
 7287                                 sp->some_taken = 1;
 7288                         }
 7289                 } else {
 7290                         /* Nothing to take. */
 7291                         if (sp->some_taken) {
 7292                                 *locked = 1;
 7293                         }
 7294                         *giveup = 1;
 7295                         to_move = 0;
 7296                         goto out_of;
 7297                 }
 7298         }
 7299 
 7300         /* If we reach here, we can copy out a chunk */
 7301         sctp_alloc_a_chunk(stcb, chk);
 7302         if (chk == NULL) {
 7303                 /* No chunk memory */
 7304                 *giveup = 1;
 7305                 to_move = 0;
 7306                 goto out_of;
 7307         }
 7308         /*
 7309          * Setup for unordered if needed by looking at the user sent info
 7310          * flags.
 7311          */
 7312         if (sp->sinfo_flags & SCTP_UNORDERED) {
 7313                 rcv_flags |= SCTP_DATA_UNORDERED;
 7314         }
 7315         if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
 7316             ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
 7317                 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
 7318         }
 7319         /* clear out the chunk before setting up */
 7320         memset(chk, 0, sizeof(*chk));
 7321         chk->rec.data.rcv_flags = rcv_flags;
 7322 
 7323         if (to_move >= length) {
 7324                 /* we think we can steal the whole thing */
 7325                 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
 7326                         SCTP_TCB_SEND_LOCK(stcb);
 7327                         send_lock_up = 1;
 7328                 }
 7329                 if (to_move < sp->length) {
 7330                         /* bail, it changed */
 7331                         goto dont_do_it;
 7332                 }
 7333                 chk->data = sp->data;
 7334                 chk->last_mbuf = sp->tail_mbuf;
 7335                 /* register the stealing */
 7336                 sp->data = sp->tail_mbuf = NULL;
 7337         } else {
 7338                 struct mbuf *m;
 7339 
 7340 dont_do_it:
 7341                 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
 7342                 chk->last_mbuf = NULL;
 7343                 if (chk->data == NULL) {
 7344                         sp->some_taken = some_taken;
 7345                         sctp_free_a_chunk(stcb, chk, so_locked);
 7346                         *bail = 1;
 7347                         to_move = 0;
 7348                         goto out_of;
 7349                 }
 7350 #ifdef SCTP_MBUF_LOGGING
 7351                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 7352                         struct mbuf *mat;
 7353 
 7354                         mat = chk->data;
 7355                         while (mat) {
 7356                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
 7357                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 7358                                 }
 7359                                 mat = SCTP_BUF_NEXT(mat);
 7360                         }
 7361                 }
 7362 #endif
 7363                 /* Pull off the data */
 7364                 m_adj(sp->data, to_move);
 7365                 /* Now lets work our way down and compact it */
 7366                 m = sp->data;
 7367                 while (m && (SCTP_BUF_LEN(m) == 0)) {
 7368                         sp->data = SCTP_BUF_NEXT(m);
 7369                         SCTP_BUF_NEXT(m) = NULL;
 7370                         if (sp->tail_mbuf == m) {
 7371                                 /*-
 7372                                  * Freeing tail? TSNH since
 7373                                  * we supposedly were taking less
 7374                                  * than the sp->length.
 7375                                  */
 7376 #ifdef INVARIANTS
 7377                                 panic("Huh, freing tail? - TSNH");
 7378 #else
 7379                                 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
 7380                                 sp->tail_mbuf = sp->data = NULL;
 7381                                 sp->length = 0;
 7382 #endif
 7383 
 7384                         }
 7385                         sctp_m_free(m);
 7386                         m = sp->data;
 7387                 }
 7388         }
 7389         if (SCTP_BUF_IS_EXTENDED(chk->data)) {
 7390                 chk->copy_by_ref = 1;
 7391         } else {
 7392                 chk->copy_by_ref = 0;
 7393         }
 7394         /*
 7395          * get last_mbuf and counts of mb useage This is ugly but hopefully
 7396          * its only one mbuf.
 7397          */
 7398         if (chk->last_mbuf == NULL) {
 7399                 chk->last_mbuf = chk->data;
 7400                 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
 7401                         chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
 7402                 }
 7403         }
 7404         if (to_move > length) {
 7405                 /*- This should not happen either
 7406                  * since we always lower to_move to the size
 7407                  * of sp->length if its larger.
 7408                  */
 7409 #ifdef INVARIANTS
 7410                 panic("Huh, how can to_move be larger?");
 7411 #else
 7412                 SCTP_PRINTF("Huh, how can to_move be larger?\n");
 7413                 sp->length = 0;
 7414 #endif
 7415         } else {
 7416                 atomic_subtract_int(&sp->length, to_move);
 7417         }
 7418         if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
 7419                 /* Not enough room for a chunk header, get some */
 7420                 struct mbuf *m;
 7421 
 7422                 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
 7423                 if (m == NULL) {
 7424                         /*
 7425                          * we're in trouble here. _PREPEND below will free
 7426                          * all the data if there is no leading space, so we
 7427                          * must put the data back and restore.
 7428                          */
 7429                         if (send_lock_up == 0) {
 7430                                 SCTP_TCB_SEND_LOCK(stcb);
 7431                                 send_lock_up = 1;
 7432                         }
 7433                         if (chk->data == NULL) {
 7434                                 /* unsteal the data */
 7435                                 sp->data = chk->data;
 7436                                 sp->tail_mbuf = chk->last_mbuf;
 7437                         } else {
 7438                                 struct mbuf *m_tmp;
 7439 
 7440                                 /* reassemble the data */
 7441                                 m_tmp = sp->data;
 7442                                 sp->data = chk->data;
 7443                                 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
 7444                         }
 7445                         sp->some_taken = some_taken;
 7446                         atomic_add_int(&sp->length, to_move);
 7447                         chk->data = NULL;
 7448                         *bail = 1;
 7449                         sctp_free_a_chunk(stcb, chk, so_locked);
 7450                         to_move = 0;
 7451                         goto out_of;
 7452                 } else {
 7453                         SCTP_BUF_LEN(m) = 0;
 7454                         SCTP_BUF_NEXT(m) = chk->data;
 7455                         chk->data = m;
 7456                         M_ALIGN(chk->data, 4);
 7457                 }
 7458         }
 7459         SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
 7460         if (chk->data == NULL) {
 7461                 /* HELP, TSNH since we assured it would not above? */
 7462 #ifdef INVARIANTS
 7463                 panic("prepend failes HELP?");
 7464 #else
 7465                 SCTP_PRINTF("prepend fails HELP?\n");
 7466                 sctp_free_a_chunk(stcb, chk, so_locked);
 7467 #endif
 7468                 *bail = 1;
 7469                 to_move = 0;
 7470                 goto out_of;
 7471         }
 7472         sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
 7473         chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
 7474         chk->book_size_scale = 0;
 7475         chk->sent = SCTP_DATAGRAM_UNSENT;
 7476 
 7477         chk->flags = 0;
 7478         chk->asoc = &stcb->asoc;
 7479         chk->pad_inplace = 0;
 7480         chk->no_fr_allowed = 0;
 7481         chk->rec.data.stream_seq = sp->strseq;
 7482         chk->rec.data.stream_number = sp->stream;
 7483         chk->rec.data.payloadtype = sp->ppid;
 7484         chk->rec.data.context = sp->context;
 7485         chk->rec.data.doing_fast_retransmit = 0;
 7486 
 7487         chk->rec.data.timetodrop = sp->ts;
 7488         chk->flags = sp->act_flags;
 7489 
 7490         if (sp->net) {
 7491                 chk->whoTo = sp->net;
 7492                 atomic_add_int(&chk->whoTo->ref_count, 1);
 7493         } else
 7494                 chk->whoTo = NULL;
 7495 
 7496         if (sp->holds_key_ref) {
 7497                 chk->auth_keyid = sp->auth_keyid;
 7498                 sctp_auth_key_acquire(stcb, chk->auth_keyid);
 7499                 chk->holds_key_ref = 1;
 7500         }
 7501         chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
 7502         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
 7503                 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
 7504                     (uintptr_t) stcb, sp->length,
 7505                     (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
 7506                     chk->rec.data.TSN_seq);
 7507         }
 7508         dchkh = mtod(chk->data, struct sctp_data_chunk *);
 7509         /*
 7510          * Put the rest of the things in place now. Size was done earlier in
 7511          * previous loop prior to padding.
 7512          */
 7513 
 7514 #ifdef SCTP_ASOCLOG_OF_TSNS
 7515         SCTP_TCB_LOCK_ASSERT(stcb);
 7516         if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
 7517                 asoc->tsn_out_at = 0;
 7518                 asoc->tsn_out_wrapped = 1;
 7519         }
 7520         asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
 7521         asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
 7522         asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
 7523         asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
 7524         asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
 7525         asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
 7526         asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
 7527         asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
 7528         asoc->tsn_out_at++;
 7529 #endif
 7530 
 7531         dchkh->ch.chunk_type = SCTP_DATA;
 7532         dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
 7533         dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
 7534         dchkh->dp.stream_id = htons(strq->stream_no);
 7535         dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
 7536         dchkh->dp.protocol_id = chk->rec.data.payloadtype;
 7537         dchkh->ch.chunk_length = htons(chk->send_size);
 7538         /* Now advance the chk->send_size by the actual pad needed. */
 7539         if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
 7540                 /* need a pad */
 7541                 struct mbuf *lm;
 7542                 int pads;
 7543 
 7544                 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
 7545                 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
 7546                         chk->pad_inplace = 1;
 7547                 }
 7548                 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
 7549                         /* pad added an mbuf */
 7550                         chk->last_mbuf = lm;
 7551                 }
 7552                 chk->send_size += pads;
 7553         }
 7554         /* We only re-set the policy if it is on */
 7555         if (sp->pr_sctp_on) {
 7556                 sctp_set_prsctp_policy(sp);
 7557                 asoc->pr_sctp_cnt++;
 7558                 chk->pr_sctp_on = 1;
 7559         } else {
 7560                 chk->pr_sctp_on = 0;
 7561         }
 7562         if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
 7563                 /* All done pull and kill the message */
 7564                 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
 7565                 if (sp->put_last_out == 0) {
 7566                         SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
 7567                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
 7568                             sp->sender_all_done,
 7569                             sp->length,
 7570                             sp->msg_is_complete,
 7571                             sp->put_last_out,
 7572                             send_lock_up);
 7573                 }
 7574                 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
 7575                         SCTP_TCB_SEND_LOCK(stcb);
 7576                         send_lock_up = 1;
 7577                 }
 7578                 TAILQ_REMOVE(&strq->outqueue, sp, next);
 7579                 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
 7580                 if (sp->net) {
 7581                         sctp_free_remote_addr(sp->net);
 7582                         sp->net = NULL;
 7583                 }
 7584                 if (sp->data) {
 7585                         sctp_m_freem(sp->data);
 7586                         sp->data = NULL;
 7587                 }
 7588                 sctp_free_a_strmoq(stcb, sp, so_locked);
 7589 
 7590                 /* we can't be locked to it */
 7591                 *locked = 0;
 7592                 stcb->asoc.locked_on_sending = NULL;
 7593         } else {
 7594                 /* more to go, we are locked */
 7595                 *locked = 1;
 7596         }
 7597         asoc->chunks_on_out_queue++;
 7598         TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
 7599         asoc->send_queue_cnt++;
 7600 out_of:
 7601         if (send_lock_up) {
 7602                 SCTP_TCB_SEND_UNLOCK(stcb);
 7603         }
 7604         return (to_move);
 7605 }
 7606 
 7607 
 7608 static void
 7609 sctp_fill_outqueue(struct sctp_tcb *stcb,
 7610     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked
 7611 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 7612     SCTP_UNUSED
 7613 #endif
 7614 )
 7615 {
 7616         struct sctp_association *asoc;
 7617         struct sctp_stream_out *strq;
 7618         int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
 7619         int locked, giveup;
 7620 
 7621         SCTP_TCB_LOCK_ASSERT(stcb);
 7622         asoc = &stcb->asoc;
 7623         switch (net->ro._l_addr.sa.sa_family) {
 7624 #ifdef INET
 7625         case AF_INET:
 7626                 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
 7627                 break;
 7628 #endif
 7629 #ifdef INET6
 7630         case AF_INET6:
 7631                 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
 7632                 break;
 7633 #endif
 7634         default:
 7635                 /* TSNH */
 7636                 goal_mtu = net->mtu;
 7637                 break;
 7638         }
 7639         /* Need an allowance for the data chunk header too */
 7640         goal_mtu -= sizeof(struct sctp_data_chunk);
 7641 
 7642         /* must make even word boundary */
 7643         goal_mtu &= 0xfffffffc;
 7644         if (asoc->locked_on_sending) {
 7645                 /* We are stuck on one stream until the message completes. */
 7646                 strq = asoc->locked_on_sending;
 7647                 locked = 1;
 7648         } else {
 7649                 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
 7650                 locked = 0;
 7651         }
 7652         while ((goal_mtu > 0) && strq) {
 7653                 giveup = 0;
 7654                 bail = 0;
 7655                 moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked,
 7656                     &giveup, eeor_mode, &bail, so_locked);
 7657                 if (moved_how_much)
 7658                         stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much);
 7659 
 7660                 if (locked) {
 7661                         asoc->locked_on_sending = strq;
 7662                         if ((moved_how_much == 0) || (giveup) || bail)
 7663                                 /* no more to move for now */
 7664                                 break;
 7665                 } else {
 7666                         asoc->locked_on_sending = NULL;
 7667                         if ((giveup) || bail) {
 7668                                 break;
 7669                         }
 7670                         strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
 7671                         if (strq == NULL) {
 7672                                 break;
 7673                         }
 7674                 }
 7675                 total_moved += moved_how_much;
 7676                 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
 7677                 goal_mtu &= 0xfffffffc;
 7678         }
 7679         if (bail)
 7680                 *quit_now = 1;
 7681 
 7682         stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
 7683 
 7684         if (total_moved == 0) {
 7685                 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
 7686                     (net == stcb->asoc.primary_destination)) {
 7687                         /* ran dry for primary network net */
 7688                         SCTP_STAT_INCR(sctps_primary_randry);
 7689                 } else if (stcb->asoc.sctp_cmt_on_off > 0) {
 7690                         /* ran dry with CMT on */
 7691                         SCTP_STAT_INCR(sctps_cmt_randry);
 7692                 }
 7693         }
 7694 }
 7695 
 7696 void
 7697 sctp_fix_ecn_echo(struct sctp_association *asoc)
 7698 {
 7699         struct sctp_tmit_chunk *chk;
 7700 
 7701         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
 7702                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
 7703                         chk->sent = SCTP_DATAGRAM_UNSENT;
 7704                 }
 7705         }
 7706 }
 7707 
 7708 void
 7709 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
 7710 {
 7711         struct sctp_association *asoc;
 7712         struct sctp_tmit_chunk *chk;
 7713         struct sctp_stream_queue_pending *sp;
 7714         unsigned int i;
 7715 
 7716         if (net == NULL) {
 7717                 return;
 7718         }
 7719         asoc = &stcb->asoc;
 7720         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
 7721                 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
 7722                         if (sp->net == net) {
 7723                                 sctp_free_remote_addr(sp->net);
 7724                                 sp->net = NULL;
 7725                         }
 7726                 }
 7727         }
 7728         TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
 7729                 if (chk->whoTo == net) {
 7730                         sctp_free_remote_addr(chk->whoTo);
 7731                         chk->whoTo = NULL;
 7732                 }
 7733         }
 7734 }
 7735 
 7736 int
 7737 sctp_med_chunk_output(struct sctp_inpcb *inp,
 7738     struct sctp_tcb *stcb,
 7739     struct sctp_association *asoc,
 7740     int *num_out,
 7741     int *reason_code,
 7742     int control_only, int from_where,
 7743     struct timeval *now, int *now_filled, int frag_point, int so_locked
 7744 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 7745     SCTP_UNUSED
 7746 #endif
 7747 )
 7748 {
 7749         /*
 7750          * Ok this is the generic chunk service queue. we must do the
 7751          * following: - Service the stream queue that is next, moving any
 7752          * message (note I must get a complete message i.e. FIRST/MIDDLE and
 7753          * LAST to the out queue in one pass) and assigning TSN's - Check to
 7754          * see if the cwnd/rwnd allows any output, if so we go ahead and
 7755          * fomulate and send the low level chunks. Making sure to combine
 7756          * any control in the control chunk queue also.
 7757          */
 7758         struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
 7759         struct mbuf *outchain, *endoutchain;
 7760         struct sctp_tmit_chunk *chk, *nchk;
 7761 
 7762         /* temp arrays for unlinking */
 7763         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
 7764         int no_fragmentflg, error;
 7765         unsigned int max_rwnd_per_dest, max_send_per_dest;
 7766         int one_chunk, hbflag, skip_data_for_this_net;
 7767         int asconf, cookie, no_out_cnt;
 7768         int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
 7769         unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
 7770         int tsns_sent = 0;
 7771         uint32_t auth_offset = 0;
 7772         struct sctp_auth_chunk *auth = NULL;
 7773         uint16_t auth_keyid;
 7774         int override_ok = 1;
 7775         int skip_fill_up = 0;
 7776         int data_auth_reqd = 0;
 7777 
 7778         /*
 7779          * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
 7780          * destination.
 7781          */
 7782         int quit_now = 0;
 7783 
 7784         *num_out = 0;
 7785         auth_keyid = stcb->asoc.authinfo.active_keyid;
 7786 
 7787         if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
 7788             (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
 7789             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
 7790                 eeor_mode = 1;
 7791         } else {
 7792                 eeor_mode = 0;
 7793         }
 7794         ctl_cnt = no_out_cnt = asconf = cookie = 0;
 7795         /*
 7796          * First lets prime the pump. For each destination, if there is room
 7797          * in the flight size, attempt to pull an MTU's worth out of the
 7798          * stream queues into the general send_queue
 7799          */
 7800 #ifdef SCTP_AUDITING_ENABLED
 7801         sctp_audit_log(0xC2, 2);
 7802 #endif
 7803         SCTP_TCB_LOCK_ASSERT(stcb);
 7804         hbflag = 0;
 7805         if ((control_only) || (asoc->stream_reset_outstanding))
 7806                 no_data_chunks = 1;
 7807         else
 7808                 no_data_chunks = 0;
 7809 
 7810         /* Nothing to possible to send? */
 7811         if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
 7812             (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
 7813             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
 7814             TAILQ_EMPTY(&asoc->send_queue) &&
 7815             stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
 7816 nothing_to_send:
 7817                 *reason_code = 9;
 7818                 return (0);
 7819         }
 7820         if (asoc->peers_rwnd == 0) {
 7821                 /* No room in peers rwnd */
 7822                 *reason_code = 1;
 7823                 if (asoc->total_flight > 0) {
 7824                         /* we are allowed one chunk in flight */
 7825                         no_data_chunks = 1;
 7826                 }
 7827         }
 7828         if (stcb->asoc.ecn_echo_cnt_onq) {
 7829                 /* Record where a sack goes, if any */
 7830                 if (no_data_chunks &&
 7831                     (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
 7832                         /* Nothing but ECNe to send - we don't do that */
 7833                         goto nothing_to_send;
 7834                 }
 7835                 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
 7836                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
 7837                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
 7838                                 sack_goes_to = chk->whoTo;
 7839                                 break;
 7840                         }
 7841                 }
 7842         }
 7843         max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
 7844         if (stcb->sctp_socket)
 7845                 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
 7846         else
 7847                 max_send_per_dest = 0;
 7848         if (no_data_chunks == 0) {
 7849                 /* How many non-directed chunks are there? */
 7850                 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
 7851                         if (chk->whoTo == NULL) {
 7852                                 /*
 7853                                  * We already have non-directed chunks on
 7854                                  * the queue, no need to do a fill-up.
 7855                                  */
 7856                                 skip_fill_up = 1;
 7857                                 break;
 7858                         }
 7859                 }
 7860 
 7861         }
 7862         if ((no_data_chunks == 0) &&
 7863             (skip_fill_up == 0) &&
 7864             (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
 7865                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
 7866                         /*
 7867                          * This for loop we are in takes in each net, if
 7868                          * its's got space in cwnd and has data sent to it
 7869                          * (when CMT is off) then it calls
 7870                          * sctp_fill_outqueue for the net. This gets data on
 7871                          * the send queue for that network.
 7872                          * 
 7873                          * In sctp_fill_outqueue TSN's are assigned and data is
 7874                          * copied out of the stream buffers. Note mostly
 7875                          * copy by reference (we hope).
 7876                          */
 7877                         net->window_probe = 0;
 7878                         if ((net != stcb->asoc.alternate) &&
 7879                             ((net->dest_state & SCTP_ADDR_PF) ||
 7880                             (!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
 7881                             (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
 7882                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
 7883                                         sctp_log_cwnd(stcb, net, 1,
 7884                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
 7885                                 }
 7886                                 continue;
 7887                         }
 7888                         if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
 7889                             (net->flight_size == 0)) {
 7890                                 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
 7891                         }
 7892                         if (net->flight_size >= net->cwnd) {
 7893                                 /* skip this network, no room - can't fill */
 7894                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
 7895                                         sctp_log_cwnd(stcb, net, 3,
 7896                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
 7897                                 }
 7898                                 continue;
 7899                         }
 7900                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
 7901                                 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
 7902                         }
 7903                         sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
 7904                         if (quit_now) {
 7905                                 /* memory alloc failure */
 7906                                 no_data_chunks = 1;
 7907                                 break;
 7908                         }
 7909                 }
 7910         }
 7911         /* now service each destination and send out what we can for it */
 7912         /* Nothing to send? */
 7913         if (TAILQ_EMPTY(&asoc->control_send_queue) &&
 7914             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
 7915             TAILQ_EMPTY(&asoc->send_queue)) {
 7916                 *reason_code = 8;
 7917                 return (0);
 7918         }
 7919         if (asoc->sctp_cmt_on_off > 0) {
 7920                 /* get the last start point */
 7921                 start_at = asoc->last_net_cmt_send_started;
 7922                 if (start_at == NULL) {
 7923                         /* null so to beginning */
 7924                         start_at = TAILQ_FIRST(&asoc->nets);
 7925                 } else {
 7926                         start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
 7927                         if (start_at == NULL) {
 7928                                 start_at = TAILQ_FIRST(&asoc->nets);
 7929                         }
 7930                 }
 7931                 asoc->last_net_cmt_send_started = start_at;
 7932         } else {
 7933                 start_at = TAILQ_FIRST(&asoc->nets);
 7934         }
 7935         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
 7936                 if (chk->whoTo == NULL) {
 7937                         if (asoc->alternate) {
 7938                                 chk->whoTo = asoc->alternate;
 7939                         } else {
 7940                                 chk->whoTo = asoc->primary_destination;
 7941                         }
 7942                         atomic_add_int(&chk->whoTo->ref_count, 1);
 7943                 }
 7944         }
 7945         old_start_at = NULL;
 7946 again_one_more_time:
 7947         for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
 7948                 /* how much can we send? */
 7949                 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
 7950                 if (old_start_at && (old_start_at == net)) {
 7951                         /* through list ocmpletely. */
 7952                         break;
 7953                 }
 7954                 tsns_sent = 0xa;
 7955                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
 7956                     TAILQ_EMPTY(&asoc->asconf_send_queue) &&
 7957                     (net->flight_size >= net->cwnd)) {
 7958                         /*
 7959                          * Nothing on control or asconf and flight is full,
 7960                          * we can skip even in the CMT case.
 7961                          */
 7962                         continue;
 7963                 }
 7964                 bundle_at = 0;
 7965                 endoutchain = outchain = NULL;
 7966                 no_fragmentflg = 1;
 7967                 one_chunk = 0;
 7968                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
 7969                         skip_data_for_this_net = 1;
 7970                 } else {
 7971                         skip_data_for_this_net = 0;
 7972                 }
 7973                 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
 7974                         /*
 7975                          * if we have a route and an ifp check to see if we
 7976                          * have room to send to this guy
 7977                          */
 7978                         struct ifnet *ifp;
 7979 
 7980                         ifp = net->ro.ro_rt->rt_ifp;
 7981                         if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
 7982                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
 7983                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
 7984                                         sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
 7985                                 }
 7986                                 continue;
 7987                         }
 7988                 }
 7989                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
 7990 #ifdef INET
 7991                 case AF_INET:
 7992                         mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
 7993                         break;
 7994 #endif
 7995 #ifdef INET6
 7996                 case AF_INET6:
 7997                         mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
 7998                         break;
 7999 #endif
 8000                 default:
 8001                         /* TSNH */
 8002                         mtu = net->mtu;
 8003                         break;
 8004                 }
 8005                 mx_mtu = mtu;
 8006                 to_out = 0;
 8007                 if (mtu > asoc->peers_rwnd) {
 8008                         if (asoc->total_flight > 0) {
 8009                                 /* We have a packet in flight somewhere */
 8010                                 r_mtu = asoc->peers_rwnd;
 8011                         } else {
 8012                                 /* We are always allowed to send one MTU out */
 8013                                 one_chunk = 1;
 8014                                 r_mtu = mtu;
 8015                         }
 8016                 } else {
 8017                         r_mtu = mtu;
 8018                 }
 8019                 /************************/
 8020                 /* ASCONF transmission */
 8021                 /************************/
 8022                 /* Now first lets go through the asconf queue */
 8023                 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
 8024                         if (chk->rec.chunk_id.id != SCTP_ASCONF) {
 8025                                 continue;
 8026                         }
 8027                         if (chk->whoTo == NULL) {
 8028                                 if (asoc->alternate == NULL) {
 8029                                         if (asoc->primary_destination != net) {
 8030                                                 break;
 8031                                         }
 8032                                 } else {
 8033                                         if (asoc->alternate != net) {
 8034                                                 break;
 8035                                         }
 8036                                 }
 8037                         } else {
 8038                                 if (chk->whoTo != net) {
 8039                                         break;
 8040                                 }
 8041                         }
 8042                         if (chk->data == NULL) {
 8043                                 break;
 8044                         }
 8045                         if (chk->sent != SCTP_DATAGRAM_UNSENT &&
 8046                             chk->sent != SCTP_DATAGRAM_RESEND) {
 8047                                 break;
 8048                         }
 8049                         /*
 8050                          * if no AUTH is yet included and this chunk
 8051                          * requires it, make sure to account for it.  We
 8052                          * don't apply the size until the AUTH chunk is
 8053                          * actually added below in case there is no room for
 8054                          * this chunk. NOTE: we overload the use of "omtu"
 8055                          * here
 8056                          */
 8057                         if ((auth == NULL) &&
 8058                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
 8059                             stcb->asoc.peer_auth_chunks)) {
 8060                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
 8061                         } else
 8062                                 omtu = 0;
 8063                         /* Here we do NOT factor the r_mtu */
 8064                         if ((chk->send_size < (int)(mtu - omtu)) ||
 8065                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
 8066                                 /*
 8067                                  * We probably should glom the mbuf chain
 8068                                  * from the chk->data for control but the
 8069                                  * problem is it becomes yet one more level
 8070                                  * of tracking to do if for some reason
 8071                                  * output fails. Then I have got to
 8072                                  * reconstruct the merged control chain.. el
 8073                                  * yucko.. for now we take the easy way and
 8074                                  * do the copy
 8075                                  */
 8076                                 /*
 8077                                  * Add an AUTH chunk, if chunk requires it
 8078                                  * save the offset into the chain for AUTH
 8079                                  */
 8080                                 if ((auth == NULL) &&
 8081                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
 8082                                     stcb->asoc.peer_auth_chunks))) {
 8083                                         outchain = sctp_add_auth_chunk(outchain,
 8084                                             &endoutchain,
 8085                                             &auth,
 8086                                             &auth_offset,
 8087                                             stcb,
 8088                                             chk->rec.chunk_id.id);
 8089                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 8090                                 }
 8091                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
 8092                                     (int)chk->rec.chunk_id.can_take_data,
 8093                                     chk->send_size, chk->copy_by_ref);
 8094                                 if (outchain == NULL) {
 8095                                         *reason_code = 8;
 8096                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 8097                                         return (ENOMEM);
 8098                                 }
 8099                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 8100                                 /* update our MTU size */
 8101                                 if (mtu > (chk->send_size + omtu))
 8102                                         mtu -= (chk->send_size + omtu);
 8103                                 else
 8104                                         mtu = 0;
 8105                                 to_out += (chk->send_size + omtu);
 8106                                 /* Do clear IP_DF ? */
 8107                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
 8108                                         no_fragmentflg = 0;
 8109                                 }
 8110                                 if (chk->rec.chunk_id.can_take_data)
 8111                                         chk->data = NULL;
 8112                                 /*
 8113                                  * set hb flag since we can use these for
 8114                                  * RTO
 8115                                  */
 8116                                 hbflag = 1;
 8117                                 asconf = 1;
 8118                                 /*
 8119                                  * should sysctl this: don't bundle data
 8120                                  * with ASCONF since it requires AUTH
 8121                                  */
 8122                                 no_data_chunks = 1;
 8123                                 chk->sent = SCTP_DATAGRAM_SENT;
 8124                                 if (chk->whoTo == NULL) {
 8125                                         chk->whoTo = net;
 8126                                         atomic_add_int(&net->ref_count, 1);
 8127                                 }
 8128                                 chk->snd_count++;
 8129                                 if (mtu == 0) {
 8130                                         /*
 8131                                          * Ok we are out of room but we can
 8132                                          * output without effecting the
 8133                                          * flight size since this little guy
 8134                                          * is a control only packet.
 8135                                          */
 8136                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
 8137                                         /*
 8138                                          * do NOT clear the asconf flag as
 8139                                          * it is used to do appropriate
 8140                                          * source address selection.
 8141                                          */
 8142                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
 8143                                             (struct sockaddr *)&net->ro._l_addr,
 8144                                             outchain, auth_offset, auth,
 8145                                             stcb->asoc.authinfo.active_keyid,
 8146                                             no_fragmentflg, 0, asconf,
 8147                                             inp->sctp_lport, stcb->rport,
 8148                                             htonl(stcb->asoc.peer_vtag),
 8149                                             net->port, so_locked, NULL, NULL))) {
 8150                                                 if (error == ENOBUFS) {
 8151                                                         asoc->ifp_had_enobuf = 1;
 8152                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
 8153                                                 }
 8154                                                 if (from_where == 0) {
 8155                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
 8156                                                 }
 8157                                                 if (*now_filled == 0) {
 8158                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 8159                                                         *now_filled = 1;
 8160                                                         *now = net->last_sent_time;
 8161                                                 } else {
 8162                                                         net->last_sent_time = *now;
 8163                                                 }
 8164                                                 hbflag = 0;
 8165                                                 /* error, could not output */
 8166                                                 if (error == EHOSTUNREACH) {
 8167                                                         /*
 8168                                                          * Destination went
 8169                                                          * unreachable
 8170                                                          * during this send
 8171                                                          */
 8172                                                         sctp_move_chunks_from_net(stcb, net);
 8173                                                 }
 8174                                                 *reason_code = 7;
 8175                                                 continue;
 8176                                         } else
 8177                                                 asoc->ifp_had_enobuf = 0;
 8178                                         if (*now_filled == 0) {
 8179                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 8180                                                 *now_filled = 1;
 8181                                                 *now = net->last_sent_time;
 8182                                         } else {
 8183                                                 net->last_sent_time = *now;
 8184                                         }
 8185                                         hbflag = 0;
 8186                                         /*
 8187                                          * increase the number we sent, if a
 8188                                          * cookie is sent we don't tell them
 8189                                          * any was sent out.
 8190                                          */
 8191                                         outchain = endoutchain = NULL;
 8192                                         auth = NULL;
 8193                                         auth_offset = 0;
 8194                                         if (!no_out_cnt)
 8195                                                 *num_out += ctl_cnt;
 8196                                         /* recalc a clean slate and setup */
 8197                                         switch (net->ro._l_addr.sa.sa_family) {
 8198 #ifdef INET
 8199                                         case AF_INET:
 8200                                                 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
 8201                                                 break;
 8202 #endif
 8203 #ifdef INET6
 8204                                         case AF_INET6:
 8205                                                 mtu = net->mtu - SCTP_MIN_OVERHEAD;
 8206                                                 break;
 8207 #endif
 8208                                         default:
 8209                                                 /* TSNH */
 8210                                                 mtu = net->mtu;
 8211                                                 break;
 8212                                         }
 8213                                         to_out = 0;
 8214                                         no_fragmentflg = 1;
 8215                                 }
 8216                         }
 8217                 }
 8218                 /************************/
 8219                 /* Control transmission */
 8220                 /************************/
 8221                 /* Now first lets go through the control queue */
 8222                 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
 8223                         if ((sack_goes_to) &&
 8224                             (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
 8225                             (chk->whoTo != sack_goes_to)) {
 8226                                 /*
 8227                                  * if we have a sack in queue, and we are
 8228                                  * looking at an ecn echo that is NOT queued
 8229                                  * to where the sack is going..
 8230                                  */
 8231                                 if (chk->whoTo == net) {
 8232                                         /*
 8233                                          * Don't transmit it to where its
 8234                                          * going (current net)
 8235                                          */
 8236                                         continue;
 8237                                 } else if (sack_goes_to == net) {
 8238                                         /*
 8239                                          * But do transmit it to this
 8240                                          * address
 8241                                          */
 8242                                         goto skip_net_check;
 8243                                 }
 8244                         }
 8245                         if (chk->whoTo == NULL) {
 8246                                 if (asoc->alternate == NULL) {
 8247                                         if (asoc->primary_destination != net) {
 8248                                                 continue;
 8249                                         }
 8250                                 } else {
 8251                                         if (asoc->alternate != net) {
 8252                                                 continue;
 8253                                         }
 8254                                 }
 8255                         } else {
 8256                                 if (chk->whoTo != net) {
 8257                                         continue;
 8258                                 }
 8259                         }
 8260         skip_net_check:
 8261                         if (chk->data == NULL) {
 8262                                 continue;
 8263                         }
 8264                         if (chk->sent != SCTP_DATAGRAM_UNSENT) {
 8265                                 /*
 8266                                  * It must be unsent. Cookies and ASCONF's
 8267                                  * hang around but there timers will force
 8268                                  * when marked for resend.
 8269                                  */
 8270                                 continue;
 8271                         }
 8272                         /*
 8273                          * if no AUTH is yet included and this chunk
 8274                          * requires it, make sure to account for it.  We
 8275                          * don't apply the size until the AUTH chunk is
 8276                          * actually added below in case there is no room for
 8277                          * this chunk. NOTE: we overload the use of "omtu"
 8278                          * here
 8279                          */
 8280                         if ((auth == NULL) &&
 8281                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
 8282                             stcb->asoc.peer_auth_chunks)) {
 8283                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
 8284                         } else
 8285                                 omtu = 0;
 8286                         /* Here we do NOT factor the r_mtu */
 8287                         if ((chk->send_size <= (int)(mtu - omtu)) ||
 8288                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
 8289                                 /*
 8290                                  * We probably should glom the mbuf chain
 8291                                  * from the chk->data for control but the
 8292                                  * problem is it becomes yet one more level
 8293                                  * of tracking to do if for some reason
 8294                                  * output fails. Then I have got to
 8295                                  * reconstruct the merged control chain.. el
 8296                                  * yucko.. for now we take the easy way and
 8297                                  * do the copy
 8298                                  */
 8299                                 /*
 8300                                  * Add an AUTH chunk, if chunk requires it
 8301                                  * save the offset into the chain for AUTH
 8302                                  */
 8303                                 if ((auth == NULL) &&
 8304                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
 8305                                     stcb->asoc.peer_auth_chunks))) {
 8306                                         outchain = sctp_add_auth_chunk(outchain,
 8307                                             &endoutchain,
 8308                                             &auth,
 8309                                             &auth_offset,
 8310                                             stcb,
 8311                                             chk->rec.chunk_id.id);
 8312                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 8313                                 }
 8314                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
 8315                                     (int)chk->rec.chunk_id.can_take_data,
 8316                                     chk->send_size, chk->copy_by_ref);
 8317                                 if (outchain == NULL) {
 8318                                         *reason_code = 8;
 8319                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 8320                                         return (ENOMEM);
 8321                                 }
 8322                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 8323                                 /* update our MTU size */
 8324                                 if (mtu > (chk->send_size + omtu))
 8325                                         mtu -= (chk->send_size + omtu);
 8326                                 else
 8327                                         mtu = 0;
 8328                                 to_out += (chk->send_size + omtu);
 8329                                 /* Do clear IP_DF ? */
 8330                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
 8331                                         no_fragmentflg = 0;
 8332                                 }
 8333                                 if (chk->rec.chunk_id.can_take_data)
 8334                                         chk->data = NULL;
 8335                                 /* Mark things to be removed, if needed */
 8336                                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
 8337                                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
 8338                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
 8339                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
 8340                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
 8341                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
 8342                                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
 8343                                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
 8344                                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
 8345                                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
 8346                                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
 8347                                         if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
 8348                                                 hbflag = 1;
 8349                                         }
 8350                                         /* remove these chunks at the end */
 8351                                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
 8352                                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
 8353                                                 /* turn off the timer */
 8354                                                 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
 8355                                                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
 8356                                                             inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
 8357                                                 }
 8358                                         }
 8359                                         ctl_cnt++;
 8360                                 } else {
 8361                                         /*
 8362                                          * Other chunks, since they have
 8363                                          * timers running (i.e. COOKIE) we
 8364                                          * just "trust" that it gets sent or
 8365                                          * retransmitted.
 8366                                          */
 8367                                         ctl_cnt++;
 8368                                         if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
 8369                                                 cookie = 1;
 8370                                                 no_out_cnt = 1;
 8371                                         } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
 8372                                                 /*
 8373                                                  * Increment ecne send count
 8374                                                  * here this means we may be
 8375                                                  * over-zealous in our
 8376                                                  * counting if the send
 8377                                                  * fails, but its the best
 8378                                                  * place to do it (we used
 8379                                                  * to do it in the queue of
 8380                                                  * the chunk, but that did
 8381                                                  * not tell how many times
 8382                                                  * it was sent.
 8383                                                  */
 8384                                                 SCTP_STAT_INCR(sctps_sendecne);
 8385                                         }
 8386                                         chk->sent = SCTP_DATAGRAM_SENT;
 8387                                         if (chk->whoTo == NULL) {
 8388                                                 chk->whoTo = net;
 8389                                                 atomic_add_int(&net->ref_count, 1);
 8390                                         }
 8391                                         chk->snd_count++;
 8392                                 }
 8393                                 if (mtu == 0) {
 8394                                         /*
 8395                                          * Ok we are out of room but we can
 8396                                          * output without effecting the
 8397                                          * flight size since this little guy
 8398                                          * is a control only packet.
 8399                                          */
 8400                                         if (asconf) {
 8401                                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
 8402                                                 /*
 8403                                                  * do NOT clear the asconf
 8404                                                  * flag as it is used to do
 8405                                                  * appropriate source
 8406                                                  * address selection.
 8407                                                  */
 8408                                         }
 8409                                         if (cookie) {
 8410                                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
 8411                                                 cookie = 0;
 8412                                         }
 8413                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
 8414                                             (struct sockaddr *)&net->ro._l_addr,
 8415                                             outchain,
 8416                                             auth_offset, auth,
 8417                                             stcb->asoc.authinfo.active_keyid,
 8418                                             no_fragmentflg, 0, asconf,
 8419                                             inp->sctp_lport, stcb->rport,
 8420                                             htonl(stcb->asoc.peer_vtag),
 8421                                             net->port, so_locked, NULL, NULL))) {
 8422                                                 if (error == ENOBUFS) {
 8423                                                         asoc->ifp_had_enobuf = 1;
 8424                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
 8425                                                 }
 8426                                                 if (from_where == 0) {
 8427                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
 8428                                                 }
 8429                                                 /* error, could not output */
 8430                                                 if (hbflag) {
 8431                                                         if (*now_filled == 0) {
 8432                                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 8433                                                                 *now_filled = 1;
 8434                                                                 *now = net->last_sent_time;
 8435                                                         } else {
 8436                                                                 net->last_sent_time = *now;
 8437                                                         }
 8438                                                         hbflag = 0;
 8439                                                 }
 8440                                                 if (error == EHOSTUNREACH) {
 8441                                                         /*
 8442                                                          * Destination went
 8443                                                          * unreachable
 8444                                                          * during this send
 8445                                                          */
 8446                                                         sctp_move_chunks_from_net(stcb, net);
 8447                                                 }
 8448                                                 *reason_code = 7;
 8449                                                 continue;
 8450                                         } else
 8451                                                 asoc->ifp_had_enobuf = 0;
 8452                                         /* Only HB or ASCONF advances time */
 8453                                         if (hbflag) {
 8454                                                 if (*now_filled == 0) {
 8455                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 8456                                                         *now_filled = 1;
 8457                                                         *now = net->last_sent_time;
 8458                                                 } else {
 8459                                                         net->last_sent_time = *now;
 8460                                                 }
 8461                                                 hbflag = 0;
 8462                                         }
 8463                                         /*
 8464                                          * increase the number we sent, if a
 8465                                          * cookie is sent we don't tell them
 8466                                          * any was sent out.
 8467                                          */
 8468                                         outchain = endoutchain = NULL;
 8469                                         auth = NULL;
 8470                                         auth_offset = 0;
 8471                                         if (!no_out_cnt)
 8472                                                 *num_out += ctl_cnt;
 8473                                         /* recalc a clean slate and setup */
 8474                                         switch (net->ro._l_addr.sa.sa_family) {
 8475 #ifdef INET
 8476                                         case AF_INET:
 8477                                                 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
 8478                                                 break;
 8479 #endif
 8480 #ifdef INET6
 8481                                         case AF_INET6:
 8482                                                 mtu = net->mtu - SCTP_MIN_OVERHEAD;
 8483                                                 break;
 8484 #endif
 8485                                         default:
 8486                                                 /* TSNH */
 8487                                                 mtu = net->mtu;
 8488                                                 break;
 8489                                         }
 8490                                         to_out = 0;
 8491                                         no_fragmentflg = 1;
 8492                                 }
 8493                         }
 8494                 }
 8495                 /* JRI: if dest is in PF state, do not send data to it */
 8496                 if ((asoc->sctp_cmt_on_off > 0) &&
 8497                     (net != stcb->asoc.alternate) &&
 8498                     (net->dest_state & SCTP_ADDR_PF)) {
 8499                         goto no_data_fill;
 8500                 }
 8501                 if (net->flight_size >= net->cwnd) {
 8502                         goto no_data_fill;
 8503                 }
 8504                 if ((asoc->sctp_cmt_on_off > 0) &&
 8505                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
 8506                     (net->flight_size > max_rwnd_per_dest)) {
 8507                         goto no_data_fill;
 8508                 }
 8509                 /*
 8510                  * We need a specific accounting for the usage of the send
 8511                  * buffer. We also need to check the number of messages per
 8512                  * net. For now, this is better than nothing and it disabled
 8513                  * by default...
 8514                  */
 8515                 if ((asoc->sctp_cmt_on_off > 0) &&
 8516                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
 8517                     (max_send_per_dest > 0) &&
 8518                     (net->flight_size > max_send_per_dest)) {
 8519                         goto no_data_fill;
 8520                 }
 8521                 /*********************/
 8522                 /* Data transmission */
 8523                 /*********************/
 8524                 /*
 8525                  * if AUTH for DATA is required and no AUTH has been added
 8526                  * yet, account for this in the mtu now... if no data can be
 8527                  * bundled, this adjustment won't matter anyways since the
 8528                  * packet will be going out...
 8529                  */
 8530                 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
 8531                     stcb->asoc.peer_auth_chunks);
 8532                 if (data_auth_reqd && (auth == NULL)) {
 8533                         mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
 8534                 }
 8535                 /* now lets add any data within the MTU constraints */
 8536                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
 8537                 case AF_INET:
 8538                         if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
 8539                                 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
 8540                         else
 8541                                 omtu = 0;
 8542                         break;
 8543 #ifdef INET6
 8544                 case AF_INET6:
 8545                         if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
 8546                                 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
 8547                         else
 8548                                 omtu = 0;
 8549                         break;
 8550 #endif
 8551                 default:
 8552                         /* TSNH */
 8553                         omtu = 0;
 8554                         break;
 8555                 }
 8556                 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
 8557                     (skip_data_for_this_net == 0)) ||
 8558                     (cookie)) {
 8559                         TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
 8560                                 if (no_data_chunks) {
 8561                                         /* let only control go out */
 8562                                         *reason_code = 1;
 8563                                         break;
 8564                                 }
 8565                                 if (net->flight_size >= net->cwnd) {
 8566                                         /* skip this net, no room for data */
 8567                                         *reason_code = 2;
 8568                                         break;
 8569                                 }
 8570                                 if ((chk->whoTo != NULL) &&
 8571                                     (chk->whoTo != net)) {
 8572                                         /* Don't send the chunk on this net */
 8573                                         continue;
 8574                                 }
 8575                                 if (asoc->sctp_cmt_on_off == 0) {
 8576                                         if ((asoc->alternate) &&
 8577                                             (asoc->alternate != net) &&
 8578                                             (chk->whoTo == NULL)) {
 8579                                                 continue;
 8580                                         } else if ((net != asoc->primary_destination) &&
 8581                                                     (asoc->alternate == NULL) &&
 8582                                             (chk->whoTo == NULL)) {
 8583                                                 continue;
 8584                                         }
 8585                                 }
 8586                                 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
 8587                                         /*-
 8588                                          * strange, we have a chunk that is
 8589                                          * to big for its destination and
 8590                                          * yet no fragment ok flag.
 8591                                          * Something went wrong when the
 8592                                          * PMTU changed...we did not mark
 8593                                          * this chunk for some reason?? I
 8594                                          * will fix it here by letting IP
 8595                                          * fragment it for now and printing
 8596                                          * a warning. This really should not
 8597                                          * happen ...
 8598                                          */
 8599                                         SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
 8600                                             chk->send_size, mtu);
 8601                                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
 8602                                 }
 8603                                 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
 8604                                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
 8605                                         struct sctp_data_chunk *dchkh;
 8606 
 8607                                         dchkh = mtod(chk->data, struct sctp_data_chunk *);
 8608                                         dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
 8609                                 }
 8610                                 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
 8611                                     ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
 8612                                         /* ok we will add this one */
 8613 
 8614                                         /*
 8615                                          * Add an AUTH chunk, if chunk
 8616                                          * requires it, save the offset into
 8617                                          * the chain for AUTH
 8618                                          */
 8619                                         if (data_auth_reqd) {
 8620                                                 if (auth == NULL) {
 8621                                                         outchain = sctp_add_auth_chunk(outchain,
 8622                                                             &endoutchain,
 8623                                                             &auth,
 8624                                                             &auth_offset,
 8625                                                             stcb,
 8626                                                             SCTP_DATA);
 8627                                                         auth_keyid = chk->auth_keyid;
 8628                                                         override_ok = 0;
 8629                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 8630                                                 } else if (override_ok) {
 8631                                                         /*
 8632                                                          * use this data's
 8633                                                          * keyid
 8634                                                          */
 8635                                                         auth_keyid = chk->auth_keyid;
 8636                                                         override_ok = 0;
 8637                                                 } else if (auth_keyid != chk->auth_keyid) {
 8638                                                         /*
 8639                                                          * different keyid,
 8640                                                          * so done bundling
 8641                                                          */
 8642                                                         break;
 8643                                                 }
 8644                                         }
 8645                                         outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
 8646                                             chk->send_size, chk->copy_by_ref);
 8647                                         if (outchain == NULL) {
 8648                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
 8649                                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
 8650                                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
 8651                                                 }
 8652                                                 *reason_code = 3;
 8653                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 8654                                                 return (ENOMEM);
 8655                                         }
 8656                                         /* upate our MTU size */
 8657                                         /* Do clear IP_DF ? */
 8658                                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
 8659                                                 no_fragmentflg = 0;
 8660                                         }
 8661                                         /* unsigned subtraction of mtu */
 8662                                         if (mtu > chk->send_size)
 8663                                                 mtu -= chk->send_size;
 8664                                         else
 8665                                                 mtu = 0;
 8666                                         /* unsigned subtraction of r_mtu */
 8667                                         if (r_mtu > chk->send_size)
 8668                                                 r_mtu -= chk->send_size;
 8669                                         else
 8670                                                 r_mtu = 0;
 8671 
 8672                                         to_out += chk->send_size;
 8673                                         if ((to_out > mx_mtu) && no_fragmentflg) {
 8674 #ifdef INVARIANTS
 8675                                                 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
 8676 #else
 8677                                                 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
 8678                                                     mx_mtu, to_out);
 8679 #endif
 8680                                         }
 8681                                         chk->window_probe = 0;
 8682                                         data_list[bundle_at++] = chk;
 8683                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
 8684                                                 break;
 8685                                         }
 8686                                         if (chk->sent == SCTP_DATAGRAM_UNSENT) {
 8687                                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
 8688                                                         SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
 8689                                                 } else {
 8690                                                         SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
 8691                                                 }
 8692                                                 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
 8693                                                     ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
 8694                                                         /*
 8695                                                          * Count number of
 8696                                                          * user msg's that
 8697                                                          * were fragmented
 8698                                                          * we do this by
 8699                                                          * counting when we
 8700                                                          * see a LAST
 8701                                                          * fragment only.
 8702                                                          */
 8703                                                         SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
 8704                                         }
 8705                                         if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
 8706                                                 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
 8707                                                         data_list[0]->window_probe = 1;
 8708                                                         net->window_probe = 1;
 8709                                                 }
 8710                                                 break;
 8711                                         }
 8712                                 } else {
 8713                                         /*
 8714                                          * Must be sent in order of the
 8715                                          * TSN's (on a network)
 8716                                          */
 8717                                         break;
 8718                                 }
 8719                         }       /* for (chunk gather loop for this net) */
 8720                 }               /* if asoc.state OPEN */
 8721 no_data_fill:
 8722                 /* Is there something to send for this destination? */
 8723                 if (outchain) {
 8724                         /* We may need to start a control timer or two */
 8725                         if (asconf) {
 8726                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
 8727                                     stcb, net);
 8728                                 /*
 8729                                  * do NOT clear the asconf flag as it is
 8730                                  * used to do appropriate source address
 8731                                  * selection.
 8732                                  */
 8733                         }
 8734                         if (cookie) {
 8735                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
 8736                                 cookie = 0;
 8737                         }
 8738                         /* must start a send timer if data is being sent */
 8739                         if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
 8740                                 /*
 8741                                  * no timer running on this destination
 8742                                  * restart it.
 8743                                  */
 8744                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
 8745                         }
 8746                         /* Now send it, if there is anything to send :> */
 8747                         if ((error = sctp_lowlevel_chunk_output(inp,
 8748                             stcb,
 8749                             net,
 8750                             (struct sockaddr *)&net->ro._l_addr,
 8751                             outchain,
 8752                             auth_offset,
 8753                             auth,
 8754                             auth_keyid,
 8755                             no_fragmentflg,
 8756                             bundle_at,
 8757                             asconf,
 8758                             inp->sctp_lport, stcb->rport,
 8759                             htonl(stcb->asoc.peer_vtag),
 8760                             net->port, so_locked, NULL, NULL))) {
 8761                                 /* error, we could not output */
 8762                                 if (error == ENOBUFS) {
 8763                                         SCTP_STAT_INCR(sctps_lowlevelerr);
 8764                                         asoc->ifp_had_enobuf = 1;
 8765                                 }
 8766                                 if (from_where == 0) {
 8767                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
 8768                                 }
 8769                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
 8770                                 if (hbflag) {
 8771                                         if (*now_filled == 0) {
 8772                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 8773                                                 *now_filled = 1;
 8774                                                 *now = net->last_sent_time;
 8775                                         } else {
 8776                                                 net->last_sent_time = *now;
 8777                                         }
 8778                                         hbflag = 0;
 8779                                 }
 8780                                 if (error == EHOSTUNREACH) {
 8781                                         /*
 8782                                          * Destination went unreachable
 8783                                          * during this send
 8784                                          */
 8785                                         sctp_move_chunks_from_net(stcb, net);
 8786                                 }
 8787                                 *reason_code = 6;
 8788                                 /*-
 8789                                  * I add this line to be paranoid. As far as
 8790                                  * I can tell the continue, takes us back to
 8791                                  * the top of the for, but just to make sure
 8792                                  * I will reset these again here.
 8793                                  */
 8794                                 ctl_cnt = bundle_at = 0;
 8795                                 continue;       /* This takes us back to the
 8796                                                  * for() for the nets. */
 8797                         } else {
 8798                                 asoc->ifp_had_enobuf = 0;
 8799                         }
 8800                         endoutchain = NULL;
 8801                         auth = NULL;
 8802                         auth_offset = 0;
 8803                         if (bundle_at || hbflag) {
 8804                                 /* For data/asconf and hb set time */
 8805                                 if (*now_filled == 0) {
 8806                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 8807                                         *now_filled = 1;
 8808                                         *now = net->last_sent_time;
 8809                                 } else {
 8810                                         net->last_sent_time = *now;
 8811                                 }
 8812                         }
 8813                         if (!no_out_cnt) {
 8814                                 *num_out += (ctl_cnt + bundle_at);
 8815                         }
 8816                         if (bundle_at) {
 8817                                 /* setup for a RTO measurement */
 8818                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
 8819                                 /* fill time if not already filled */
 8820                                 if (*now_filled == 0) {
 8821                                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
 8822                                         *now_filled = 1;
 8823                                         *now = asoc->time_last_sent;
 8824                                 } else {
 8825                                         asoc->time_last_sent = *now;
 8826                                 }
 8827                                 if (net->rto_needed) {
 8828                                         data_list[0]->do_rtt = 1;
 8829                                         net->rto_needed = 0;
 8830                                 }
 8831                                 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
 8832                                 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
 8833                         }
 8834                         if (one_chunk) {
 8835                                 break;
 8836                         }
 8837                 }
 8838                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
 8839                         sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
 8840                 }
 8841         }
 8842         if (old_start_at == NULL) {
 8843                 old_start_at = start_at;
 8844                 start_at = TAILQ_FIRST(&asoc->nets);
 8845                 if (old_start_at)
 8846                         goto again_one_more_time;
 8847         }
 8848         /*
 8849          * At the end there should be no NON timed chunks hanging on this
 8850          * queue.
 8851          */
 8852         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
 8853                 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
 8854         }
 8855         if ((*num_out == 0) && (*reason_code == 0)) {
 8856                 *reason_code = 4;
 8857         } else {
 8858                 *reason_code = 5;
 8859         }
 8860         sctp_clean_up_ctl(stcb, asoc, so_locked);
 8861         return (0);
 8862 }
 8863 
 8864 void
 8865 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
 8866 {
 8867         /*-
 8868          * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
 8869          * the control chunk queue.
 8870          */
 8871         struct sctp_chunkhdr *hdr;
 8872         struct sctp_tmit_chunk *chk;
 8873         struct mbuf *mat;
 8874 
 8875         SCTP_TCB_LOCK_ASSERT(stcb);
 8876         sctp_alloc_a_chunk(stcb, chk);
 8877         if (chk == NULL) {
 8878                 /* no memory */
 8879                 sctp_m_freem(op_err);
 8880                 return;
 8881         }
 8882         chk->copy_by_ref = 0;
 8883         SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
 8884         if (op_err == NULL) {
 8885                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
 8886                 return;
 8887         }
 8888         chk->send_size = 0;
 8889         mat = op_err;
 8890         while (mat != NULL) {
 8891                 chk->send_size += SCTP_BUF_LEN(mat);
 8892                 mat = SCTP_BUF_NEXT(mat);
 8893         }
 8894         chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
 8895         chk->rec.chunk_id.can_take_data = 1;
 8896         chk->sent = SCTP_DATAGRAM_UNSENT;
 8897         chk->snd_count = 0;
 8898         chk->flags = 0;
 8899         chk->asoc = &stcb->asoc;
 8900         chk->data = op_err;
 8901         chk->whoTo = NULL;
 8902         hdr = mtod(op_err, struct sctp_chunkhdr *);
 8903         hdr->chunk_type = SCTP_OPERATION_ERROR;
 8904         hdr->chunk_flags = 0;
 8905         hdr->chunk_length = htons(chk->send_size);
 8906         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
 8907             chk,
 8908             sctp_next);
 8909         chk->asoc->ctrl_queue_cnt++;
 8910 }
 8911 
 8912 int
 8913 sctp_send_cookie_echo(struct mbuf *m,
 8914     int offset,
 8915     struct sctp_tcb *stcb,
 8916     struct sctp_nets *net)
 8917 {
 8918         /*-
 8919          * pull out the cookie and put it at the front of the control chunk
 8920          * queue.
 8921          */
 8922         int at;
 8923         struct mbuf *cookie;
 8924         struct sctp_paramhdr parm, *phdr;
 8925         struct sctp_chunkhdr *hdr;
 8926         struct sctp_tmit_chunk *chk;
 8927         uint16_t ptype, plen;
 8928 
 8929         /* First find the cookie in the param area */
 8930         cookie = NULL;
 8931         at = offset + sizeof(struct sctp_init_chunk);
 8932 
 8933         SCTP_TCB_LOCK_ASSERT(stcb);
 8934         do {
 8935                 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
 8936                 if (phdr == NULL) {
 8937                         return (-3);
 8938                 }
 8939                 ptype = ntohs(phdr->param_type);
 8940                 plen = ntohs(phdr->param_length);
 8941                 if (ptype == SCTP_STATE_COOKIE) {
 8942                         int pad;
 8943 
 8944                         /* found the cookie */
 8945                         if ((pad = (plen % 4))) {
 8946                                 plen += 4 - pad;
 8947                         }
 8948                         cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
 8949                         if (cookie == NULL) {
 8950                                 /* No memory */
 8951                                 return (-2);
 8952                         }
 8953 #ifdef SCTP_MBUF_LOGGING
 8954                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 8955                                 struct mbuf *mat;
 8956 
 8957                                 mat = cookie;
 8958                                 while (mat) {
 8959                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
 8960                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 8961                                         }
 8962                                         mat = SCTP_BUF_NEXT(mat);
 8963                                 }
 8964                         }
 8965 #endif
 8966                         break;
 8967                 }
 8968                 at += SCTP_SIZE32(plen);
 8969         } while (phdr);
 8970         if (cookie == NULL) {
 8971                 /* Did not find the cookie */
 8972                 return (-3);
 8973         }
 8974         /* ok, we got the cookie lets change it into a cookie echo chunk */
 8975 
 8976         /* first the change from param to cookie */
 8977         hdr = mtod(cookie, struct sctp_chunkhdr *);
 8978         hdr->chunk_type = SCTP_COOKIE_ECHO;
 8979         hdr->chunk_flags = 0;
 8980         /* get the chunk stuff now and place it in the FRONT of the queue */
 8981         sctp_alloc_a_chunk(stcb, chk);
 8982         if (chk == NULL) {
 8983                 /* no memory */
 8984                 sctp_m_freem(cookie);
 8985                 return (-5);
 8986         }
 8987         chk->copy_by_ref = 0;
 8988         chk->send_size = plen;
 8989         chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
 8990         chk->rec.chunk_id.can_take_data = 0;
 8991         chk->sent = SCTP_DATAGRAM_UNSENT;
 8992         chk->snd_count = 0;
 8993         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
 8994         chk->asoc = &stcb->asoc;
 8995         chk->data = cookie;
 8996         chk->whoTo = net;
 8997         atomic_add_int(&chk->whoTo->ref_count, 1);
 8998         TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
 8999         chk->asoc->ctrl_queue_cnt++;
 9000         return (0);
 9001 }
 9002 
 9003 void
 9004 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
 9005     struct mbuf *m,
 9006     int offset,
 9007     int chk_length,
 9008     struct sctp_nets *net)
 9009 {
 9010         /*
 9011          * take a HB request and make it into a HB ack and send it.
 9012          */
 9013         struct mbuf *outchain;
 9014         struct sctp_chunkhdr *chdr;
 9015         struct sctp_tmit_chunk *chk;
 9016 
 9017 
 9018         if (net == NULL)
 9019                 /* must have a net pointer */
 9020                 return;
 9021 
 9022         outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
 9023         if (outchain == NULL) {
 9024                 /* gak out of memory */
 9025                 return;
 9026         }
 9027 #ifdef SCTP_MBUF_LOGGING
 9028         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 9029                 struct mbuf *mat;
 9030 
 9031                 mat = outchain;
 9032                 while (mat) {
 9033                         if (SCTP_BUF_IS_EXTENDED(mat)) {
 9034                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 9035                         }
 9036                         mat = SCTP_BUF_NEXT(mat);
 9037                 }
 9038         }
 9039 #endif
 9040         chdr = mtod(outchain, struct sctp_chunkhdr *);
 9041         chdr->chunk_type = SCTP_HEARTBEAT_ACK;
 9042         chdr->chunk_flags = 0;
 9043         if (chk_length % 4) {
 9044                 /* need pad */
 9045                 uint32_t cpthis = 0;
 9046                 int padlen;
 9047 
 9048                 padlen = 4 - (chk_length % 4);
 9049                 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
 9050         }
 9051         sctp_alloc_a_chunk(stcb, chk);
 9052         if (chk == NULL) {
 9053                 /* no memory */
 9054                 sctp_m_freem(outchain);
 9055                 return;
 9056         }
 9057         chk->copy_by_ref = 0;
 9058         chk->send_size = chk_length;
 9059         chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
 9060         chk->rec.chunk_id.can_take_data = 1;
 9061         chk->sent = SCTP_DATAGRAM_UNSENT;
 9062         chk->snd_count = 0;
 9063         chk->flags = 0;
 9064         chk->asoc = &stcb->asoc;
 9065         chk->data = outchain;
 9066         chk->whoTo = net;
 9067         atomic_add_int(&chk->whoTo->ref_count, 1);
 9068         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
 9069         chk->asoc->ctrl_queue_cnt++;
 9070 }
 9071 
 9072 void
 9073 sctp_send_cookie_ack(struct sctp_tcb *stcb)
 9074 {
 9075         /* formulate and queue a cookie-ack back to sender */
 9076         struct mbuf *cookie_ack;
 9077         struct sctp_chunkhdr *hdr;
 9078         struct sctp_tmit_chunk *chk;
 9079 
 9080         cookie_ack = NULL;
 9081         SCTP_TCB_LOCK_ASSERT(stcb);
 9082 
 9083         cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
 9084         if (cookie_ack == NULL) {
 9085                 /* no mbuf's */
 9086                 return;
 9087         }
 9088         SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
 9089         sctp_alloc_a_chunk(stcb, chk);
 9090         if (chk == NULL) {
 9091                 /* no memory */
 9092                 sctp_m_freem(cookie_ack);
 9093                 return;
 9094         }
 9095         chk->copy_by_ref = 0;
 9096         chk->send_size = sizeof(struct sctp_chunkhdr);
 9097         chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
 9098         chk->rec.chunk_id.can_take_data = 1;
 9099         chk->sent = SCTP_DATAGRAM_UNSENT;
 9100         chk->snd_count = 0;
 9101         chk->flags = 0;
 9102         chk->asoc = &stcb->asoc;
 9103         chk->data = cookie_ack;
 9104         if (chk->asoc->last_control_chunk_from != NULL) {
 9105                 chk->whoTo = chk->asoc->last_control_chunk_from;
 9106                 atomic_add_int(&chk->whoTo->ref_count, 1);
 9107         } else {
 9108                 chk->whoTo = NULL;
 9109         }
 9110         hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
 9111         hdr->chunk_type = SCTP_COOKIE_ACK;
 9112         hdr->chunk_flags = 0;
 9113         hdr->chunk_length = htons(chk->send_size);
 9114         SCTP_BUF_LEN(cookie_ack) = chk->send_size;
 9115         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
 9116         chk->asoc->ctrl_queue_cnt++;
 9117         return;
 9118 }
 9119 
 9120 
 9121 void
 9122 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
 9123 {
 9124         /* formulate and queue a SHUTDOWN-ACK back to the sender */
 9125         struct mbuf *m_shutdown_ack;
 9126         struct sctp_shutdown_ack_chunk *ack_cp;
 9127         struct sctp_tmit_chunk *chk;
 9128 
 9129         m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
 9130         if (m_shutdown_ack == NULL) {
 9131                 /* no mbuf's */
 9132                 return;
 9133         }
 9134         SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
 9135         sctp_alloc_a_chunk(stcb, chk);
 9136         if (chk == NULL) {
 9137                 /* no memory */
 9138                 sctp_m_freem(m_shutdown_ack);
 9139                 return;
 9140         }
 9141         chk->copy_by_ref = 0;
 9142         chk->send_size = sizeof(struct sctp_chunkhdr);
 9143         chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
 9144         chk->rec.chunk_id.can_take_data = 1;
 9145         chk->sent = SCTP_DATAGRAM_UNSENT;
 9146         chk->snd_count = 0;
 9147         chk->flags = 0;
 9148         chk->asoc = &stcb->asoc;
 9149         chk->data = m_shutdown_ack;
 9150         chk->whoTo = net;
 9151         if (chk->whoTo) {
 9152                 atomic_add_int(&chk->whoTo->ref_count, 1);
 9153         }
 9154         ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
 9155         ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
 9156         ack_cp->ch.chunk_flags = 0;
 9157         ack_cp->ch.chunk_length = htons(chk->send_size);
 9158         SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
 9159         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
 9160         chk->asoc->ctrl_queue_cnt++;
 9161         return;
 9162 }
 9163 
 9164 void
 9165 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
 9166 {
 9167         /* formulate and queue a SHUTDOWN to the sender */
 9168         struct mbuf *m_shutdown;
 9169         struct sctp_shutdown_chunk *shutdown_cp;
 9170         struct sctp_tmit_chunk *chk;
 9171 
 9172         m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
 9173         if (m_shutdown == NULL) {
 9174                 /* no mbuf's */
 9175                 return;
 9176         }
 9177         SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
 9178         sctp_alloc_a_chunk(stcb, chk);
 9179         if (chk == NULL) {
 9180                 /* no memory */
 9181                 sctp_m_freem(m_shutdown);
 9182                 return;
 9183         }
 9184         chk->copy_by_ref = 0;
 9185         chk->send_size = sizeof(struct sctp_shutdown_chunk);
 9186         chk->rec.chunk_id.id = SCTP_SHUTDOWN;
 9187         chk->rec.chunk_id.can_take_data = 1;
 9188         chk->sent = SCTP_DATAGRAM_UNSENT;
 9189         chk->snd_count = 0;
 9190         chk->flags = 0;
 9191         chk->asoc = &stcb->asoc;
 9192         chk->data = m_shutdown;
 9193         chk->whoTo = net;
 9194         if (chk->whoTo) {
 9195                 atomic_add_int(&chk->whoTo->ref_count, 1);
 9196         }
 9197         shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
 9198         shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
 9199         shutdown_cp->ch.chunk_flags = 0;
 9200         shutdown_cp->ch.chunk_length = htons(chk->send_size);
 9201         shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
 9202         SCTP_BUF_LEN(m_shutdown) = chk->send_size;
 9203         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
 9204         chk->asoc->ctrl_queue_cnt++;
 9205         return;
 9206 }
 9207 
 9208 void
 9209 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
 9210 {
 9211         /*
 9212          * formulate and queue an ASCONF to the peer. ASCONF parameters
 9213          * should be queued on the assoc queue.
 9214          */
 9215         struct sctp_tmit_chunk *chk;
 9216         struct mbuf *m_asconf;
 9217         int len;
 9218 
 9219         SCTP_TCB_LOCK_ASSERT(stcb);
 9220 
 9221         if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
 9222             (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
 9223                 /* can't send a new one if there is one in flight already */
 9224                 return;
 9225         }
 9226         /* compose an ASCONF chunk, maximum length is PMTU */
 9227         m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
 9228         if (m_asconf == NULL) {
 9229                 return;
 9230         }
 9231         sctp_alloc_a_chunk(stcb, chk);
 9232         if (chk == NULL) {
 9233                 /* no memory */
 9234                 sctp_m_freem(m_asconf);
 9235                 return;
 9236         }
 9237         chk->copy_by_ref = 0;
 9238         chk->data = m_asconf;
 9239         chk->send_size = len;
 9240         chk->rec.chunk_id.id = SCTP_ASCONF;
 9241         chk->rec.chunk_id.can_take_data = 0;
 9242         chk->sent = SCTP_DATAGRAM_UNSENT;
 9243         chk->snd_count = 0;
 9244         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
 9245         chk->asoc = &stcb->asoc;
 9246         chk->whoTo = net;
 9247         if (chk->whoTo) {
 9248                 atomic_add_int(&chk->whoTo->ref_count, 1);
 9249         }
 9250         TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
 9251         chk->asoc->ctrl_queue_cnt++;
 9252         return;
 9253 }
 9254 
 9255 void
 9256 sctp_send_asconf_ack(struct sctp_tcb *stcb)
 9257 {
 9258         /*
 9259          * formulate and queue a asconf-ack back to sender. the asconf-ack
 9260          * must be stored in the tcb.
 9261          */
 9262         struct sctp_tmit_chunk *chk;
 9263         struct sctp_asconf_ack *ack, *latest_ack;
 9264         struct mbuf *m_ack;
 9265         struct sctp_nets *net = NULL;
 9266 
 9267         SCTP_TCB_LOCK_ASSERT(stcb);
 9268         /* Get the latest ASCONF-ACK */
 9269         latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
 9270         if (latest_ack == NULL) {
 9271                 return;
 9272         }
 9273         if (latest_ack->last_sent_to != NULL &&
 9274             latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
 9275                 /* we're doing a retransmission */
 9276                 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
 9277                 if (net == NULL) {
 9278                         /* no alternate */
 9279                         if (stcb->asoc.last_control_chunk_from == NULL) {
 9280                                 if (stcb->asoc.alternate) {
 9281                                         net = stcb->asoc.alternate;
 9282                                 } else {
 9283                                         net = stcb->asoc.primary_destination;
 9284                                 }
 9285                         } else {
 9286                                 net = stcb->asoc.last_control_chunk_from;
 9287                         }
 9288                 }
 9289         } else {
 9290                 /* normal case */
 9291                 if (stcb->asoc.last_control_chunk_from == NULL) {
 9292                         if (stcb->asoc.alternate) {
 9293                                 net = stcb->asoc.alternate;
 9294                         } else {
 9295                                 net = stcb->asoc.primary_destination;
 9296                         }
 9297                 } else {
 9298                         net = stcb->asoc.last_control_chunk_from;
 9299                 }
 9300         }
 9301         latest_ack->last_sent_to = net;
 9302 
 9303         TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
 9304                 if (ack->data == NULL) {
 9305                         continue;
 9306                 }
 9307                 /* copy the asconf_ack */
 9308                 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
 9309                 if (m_ack == NULL) {
 9310                         /* couldn't copy it */
 9311                         return;
 9312                 }
 9313 #ifdef SCTP_MBUF_LOGGING
 9314                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 9315                         struct mbuf *mat;
 9316 
 9317                         mat = m_ack;
 9318                         while (mat) {
 9319                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
 9320                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 9321                                 }
 9322                                 mat = SCTP_BUF_NEXT(mat);
 9323                         }
 9324                 }
 9325 #endif
 9326 
 9327                 sctp_alloc_a_chunk(stcb, chk);
 9328                 if (chk == NULL) {
 9329                         /* no memory */
 9330                         if (m_ack)
 9331                                 sctp_m_freem(m_ack);
 9332                         return;
 9333                 }
 9334                 chk->copy_by_ref = 0;
 9335 
 9336                 chk->whoTo = net;
 9337                 if (chk->whoTo) {
 9338                         atomic_add_int(&chk->whoTo->ref_count, 1);
 9339                 }
 9340                 chk->data = m_ack;
 9341                 chk->send_size = 0;
 9342                 /* Get size */
 9343                 chk->send_size = ack->len;
 9344                 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
 9345                 chk->rec.chunk_id.can_take_data = 1;
 9346                 chk->sent = SCTP_DATAGRAM_UNSENT;
 9347                 chk->snd_count = 0;
 9348                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;  /* XXX */
 9349                 chk->asoc = &stcb->asoc;
 9350 
 9351                 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
 9352                 chk->asoc->ctrl_queue_cnt++;
 9353         }
 9354         return;
 9355 }
 9356 
 9357 
 9358 static int
 9359 sctp_chunk_retransmission(struct sctp_inpcb *inp,
 9360     struct sctp_tcb *stcb,
 9361     struct sctp_association *asoc,
 9362     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
 9363 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 9364     SCTP_UNUSED
 9365 #endif
 9366 )
 9367 {
 9368         /*-
 9369          * send out one MTU of retransmission. If fast_retransmit is
 9370          * happening we ignore the cwnd. Otherwise we obey the cwnd and
 9371          * rwnd. For a Cookie or Asconf in the control chunk queue we
 9372          * retransmit them by themselves.
 9373          *
 9374          * For data chunks we will pick out the lowest TSN's in the sent_queue
 9375          * marked for resend and bundle them all together (up to a MTU of
 9376          * destination). The address to send to should have been
 9377          * selected/changed where the retransmission was marked (i.e. in FR
 9378          * or t3-timeout routines).
 9379          */
 9380         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
 9381         struct sctp_tmit_chunk *chk, *fwd;
 9382         struct mbuf *m, *endofchain;
 9383         struct sctp_nets *net = NULL;
 9384         uint32_t tsns_sent = 0;
 9385         int no_fragmentflg, bundle_at, cnt_thru;
 9386         unsigned int mtu;
 9387         int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
 9388         struct sctp_auth_chunk *auth = NULL;
 9389         uint32_t auth_offset = 0;
 9390         uint16_t auth_keyid;
 9391         int override_ok = 1;
 9392         int data_auth_reqd = 0;
 9393         uint32_t dmtu = 0;
 9394 
 9395         SCTP_TCB_LOCK_ASSERT(stcb);
 9396         tmr_started = ctl_cnt = bundle_at = error = 0;
 9397         no_fragmentflg = 1;
 9398         fwd_tsn = 0;
 9399         *cnt_out = 0;
 9400         fwd = NULL;
 9401         endofchain = m = NULL;
 9402         auth_keyid = stcb->asoc.authinfo.active_keyid;
 9403 #ifdef SCTP_AUDITING_ENABLED
 9404         sctp_audit_log(0xC3, 1);
 9405 #endif
 9406         if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
 9407             (TAILQ_EMPTY(&asoc->control_send_queue))) {
 9408                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
 9409                     asoc->sent_queue_retran_cnt);
 9410                 asoc->sent_queue_cnt = 0;
 9411                 asoc->sent_queue_cnt_removeable = 0;
 9412                 /* send back 0/0 so we enter normal transmission */
 9413                 *cnt_out = 0;
 9414                 return (0);
 9415         }
 9416         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
 9417                 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
 9418                     (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
 9419                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
 9420                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
 9421                                 continue;
 9422                         }
 9423                         if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
 9424                                 if (chk != asoc->str_reset) {
 9425                                         /*
 9426                                          * not eligible for retran if its
 9427                                          * not ours
 9428                                          */
 9429                                         continue;
 9430                                 }
 9431                         }
 9432                         ctl_cnt++;
 9433                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
 9434                                 fwd_tsn = 1;
 9435                         }
 9436                         /*
 9437                          * Add an AUTH chunk, if chunk requires it save the
 9438                          * offset into the chain for AUTH
 9439                          */
 9440                         if ((auth == NULL) &&
 9441                             (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
 9442                             stcb->asoc.peer_auth_chunks))) {
 9443                                 m = sctp_add_auth_chunk(m, &endofchain,
 9444                                     &auth, &auth_offset,
 9445                                     stcb,
 9446                                     chk->rec.chunk_id.id);
 9447                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 9448                         }
 9449                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
 9450                         break;
 9451                 }
 9452         }
 9453         one_chunk = 0;
 9454         cnt_thru = 0;
 9455         /* do we have control chunks to retransmit? */
 9456         if (m != NULL) {
 9457                 /* Start a timer no matter if we suceed or fail */
 9458                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
 9459                         sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
 9460                 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
 9461                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
 9462                 chk->snd_count++;       /* update our count */
 9463                 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
 9464                     (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
 9465                     auth_offset, auth, stcb->asoc.authinfo.active_keyid,
 9466                     no_fragmentflg, 0, 0,
 9467                     inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
 9468                     chk->whoTo->port, so_locked, NULL, NULL))) {
 9469                         SCTP_STAT_INCR(sctps_lowlevelerr);
 9470                         return (error);
 9471                 }
 9472                 endofchain = NULL;
 9473                 auth = NULL;
 9474                 auth_offset = 0;
 9475                 /*
 9476                  * We don't want to mark the net->sent time here since this
 9477                  * we use this for HB and retrans cannot measure RTT
 9478                  */
 9479                 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
 9480                 *cnt_out += 1;
 9481                 chk->sent = SCTP_DATAGRAM_SENT;
 9482                 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
 9483                 if (fwd_tsn == 0) {
 9484                         return (0);
 9485                 } else {
 9486                         /* Clean up the fwd-tsn list */
 9487                         sctp_clean_up_ctl(stcb, asoc, so_locked);
 9488                         return (0);
 9489                 }
 9490         }
 9491         /*
 9492          * Ok, it is just data retransmission we need to do or that and a
 9493          * fwd-tsn with it all.
 9494          */
 9495         if (TAILQ_EMPTY(&asoc->sent_queue)) {
 9496                 return (SCTP_RETRAN_DONE);
 9497         }
 9498         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
 9499             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
 9500                 /* not yet open, resend the cookie and that is it */
 9501                 return (1);
 9502         }
 9503 #ifdef SCTP_AUDITING_ENABLED
 9504         sctp_auditing(20, inp, stcb, NULL);
 9505 #endif
 9506         data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
 9507         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
 9508                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
 9509                         /* No, not sent to this net or not ready for rtx */
 9510                         continue;
 9511                 }
 9512                 if (chk->data == NULL) {
 9513                         printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
 9514                             chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
 9515                         continue;
 9516                 }
 9517                 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
 9518                     (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
 9519                         /* Gak, we have exceeded max unlucky retran, abort! */
 9520                         SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
 9521                             chk->snd_count,
 9522                             SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
 9523                         atomic_add_int(&stcb->asoc.refcnt, 1);
 9524                         sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
 9525                         SCTP_TCB_LOCK(stcb);
 9526                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 9527                         return (SCTP_RETRAN_EXIT);
 9528                 }
 9529                 /* pick up the net */
 9530                 net = chk->whoTo;
 9531                 switch (net->ro._l_addr.sa.sa_family) {
 9532 #ifdef INET
 9533                 case AF_INET:
 9534                         mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
 9535                         break;
 9536 #endif
 9537 #ifdef INET6
 9538                 case AF_INET6:
 9539                         mtu = net->mtu - SCTP_MIN_OVERHEAD;
 9540                         break;
 9541 #endif
 9542                 default:
 9543                         /* TSNH */
 9544                         mtu = net->mtu;
 9545                         break;
 9546                 }
 9547 
 9548                 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
 9549                         /* No room in peers rwnd */
 9550                         uint32_t tsn;
 9551 
 9552                         tsn = asoc->last_acked_seq + 1;
 9553                         if (tsn == chk->rec.data.TSN_seq) {
 9554                                 /*
 9555                                  * we make a special exception for this
 9556                                  * case. The peer has no rwnd but is missing
 9557                                  * the lowest chunk.. which is probably what
 9558                                  * is holding up the rwnd.
 9559                                  */
 9560                                 goto one_chunk_around;
 9561                         }
 9562                         return (1);
 9563                 }
 9564 one_chunk_around:
 9565                 if (asoc->peers_rwnd < mtu) {
 9566                         one_chunk = 1;
 9567                         if ((asoc->peers_rwnd == 0) &&
 9568                             (asoc->total_flight == 0)) {
 9569                                 chk->window_probe = 1;
 9570                                 chk->whoTo->window_probe = 1;
 9571                         }
 9572                 }
 9573 #ifdef SCTP_AUDITING_ENABLED
 9574                 sctp_audit_log(0xC3, 2);
 9575 #endif
 9576                 bundle_at = 0;
 9577                 m = NULL;
 9578                 net->fast_retran_ip = 0;
 9579                 if (chk->rec.data.doing_fast_retransmit == 0) {
 9580                         /*
 9581                          * if no FR in progress skip destination that have
 9582                          * flight_size > cwnd.
 9583                          */
 9584                         if (net->flight_size >= net->cwnd) {
 9585                                 continue;
 9586                         }
 9587                 } else {
 9588                         /*
 9589                          * Mark the destination net to have FR recovery
 9590                          * limits put on it.
 9591                          */
 9592                         *fr_done = 1;
 9593                         net->fast_retran_ip = 1;
 9594                 }
 9595 
 9596                 /*
 9597                  * if no AUTH is yet included and this chunk requires it,
 9598                  * make sure to account for it.  We don't apply the size
 9599                  * until the AUTH chunk is actually added below in case
 9600                  * there is no room for this chunk.
 9601                  */
 9602                 if (data_auth_reqd && (auth == NULL)) {
 9603                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
 9604                 } else
 9605                         dmtu = 0;
 9606 
 9607                 if ((chk->send_size <= (mtu - dmtu)) ||
 9608                     (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
 9609                         /* ok we will add this one */
 9610                         if (data_auth_reqd) {
 9611                                 if (auth == NULL) {
 9612                                         m = sctp_add_auth_chunk(m,
 9613                                             &endofchain,
 9614                                             &auth,
 9615                                             &auth_offset,
 9616                                             stcb,
 9617                                             SCTP_DATA);
 9618                                         auth_keyid = chk->auth_keyid;
 9619                                         override_ok = 0;
 9620                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 9621                                 } else if (override_ok) {
 9622                                         auth_keyid = chk->auth_keyid;
 9623                                         override_ok = 0;
 9624                                 } else if (chk->auth_keyid != auth_keyid) {
 9625                                         /* different keyid, so done bundling */
 9626                                         break;
 9627                                 }
 9628                         }
 9629                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
 9630                         if (m == NULL) {
 9631                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 9632                                 return (ENOMEM);
 9633                         }
 9634                         /* Do clear IP_DF ? */
 9635                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
 9636                                 no_fragmentflg = 0;
 9637                         }
 9638                         /* upate our MTU size */
 9639                         if (mtu > (chk->send_size + dmtu))
 9640                                 mtu -= (chk->send_size + dmtu);
 9641                         else
 9642                                 mtu = 0;
 9643                         data_list[bundle_at++] = chk;
 9644                         if (one_chunk && (asoc->total_flight <= 0)) {
 9645                                 SCTP_STAT_INCR(sctps_windowprobed);
 9646                         }
 9647                 }
 9648                 if (one_chunk == 0) {
 9649                         /*
 9650                          * now are there anymore forward from chk to pick
 9651                          * up?
 9652                          */
 9653                         for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
 9654                                 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
 9655                                         /* Nope, not for retran */
 9656                                         continue;
 9657                                 }
 9658                                 if (fwd->whoTo != net) {
 9659                                         /* Nope, not the net in question */
 9660                                         continue;
 9661                                 }
 9662                                 if (data_auth_reqd && (auth == NULL)) {
 9663                                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
 9664                                 } else
 9665                                         dmtu = 0;
 9666                                 if (fwd->send_size <= (mtu - dmtu)) {
 9667                                         if (data_auth_reqd) {
 9668                                                 if (auth == NULL) {
 9669                                                         m = sctp_add_auth_chunk(m,
 9670                                                             &endofchain,
 9671                                                             &auth,
 9672                                                             &auth_offset,
 9673                                                             stcb,
 9674                                                             SCTP_DATA);
 9675                                                         auth_keyid = fwd->auth_keyid;
 9676                                                         override_ok = 0;
 9677                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 9678                                                 } else if (override_ok) {
 9679                                                         auth_keyid = fwd->auth_keyid;
 9680                                                         override_ok = 0;
 9681                                                 } else if (fwd->auth_keyid != auth_keyid) {
 9682                                                         /*
 9683                                                          * different keyid,
 9684                                                          * so done bundling
 9685                                                          */
 9686                                                         break;
 9687                                                 }
 9688                                         }
 9689                                         m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
 9690                                         if (m == NULL) {
 9691                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 9692                                                 return (ENOMEM);
 9693                                         }
 9694                                         /* Do clear IP_DF ? */
 9695                                         if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
 9696                                                 no_fragmentflg = 0;
 9697                                         }
 9698                                         /* upate our MTU size */
 9699                                         if (mtu > (fwd->send_size + dmtu))
 9700                                                 mtu -= (fwd->send_size + dmtu);
 9701                                         else
 9702                                                 mtu = 0;
 9703                                         data_list[bundle_at++] = fwd;
 9704                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
 9705                                                 break;
 9706                                         }
 9707                                 } else {
 9708                                         /* can't fit so we are done */
 9709                                         break;
 9710                                 }
 9711                         }
 9712                 }
 9713                 /* Is there something to send for this destination? */
 9714                 if (m) {
 9715                         /*
 9716                          * No matter if we fail/or suceed we should start a
 9717                          * timer. A failure is like a lost IP packet :-)
 9718                          */
 9719                         if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
 9720                                 /*
 9721                                  * no timer running on this destination
 9722                                  * restart it.
 9723                                  */
 9724                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
 9725                                 tmr_started = 1;
 9726                         }
 9727                         /* Now lets send it, if there is anything to send :> */
 9728                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
 9729                             (struct sockaddr *)&net->ro._l_addr, m,
 9730                             auth_offset, auth, auth_keyid,
 9731                             no_fragmentflg, 0, 0,
 9732                             inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
 9733                             net->port, so_locked, NULL, NULL))) {
 9734                                 /* error, we could not output */
 9735                                 SCTP_STAT_INCR(sctps_lowlevelerr);
 9736                                 return (error);
 9737                         }
 9738                         endofchain = NULL;
 9739                         auth = NULL;
 9740                         auth_offset = 0;
 9741                         /* For HB's */
 9742                         /*
 9743                          * We don't want to mark the net->sent time here
 9744                          * since this we use this for HB and retrans cannot
 9745                          * measure RTT
 9746                          */
 9747                         /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
 9748 
 9749                         /* For auto-close */
 9750                         cnt_thru++;
 9751                         if (*now_filled == 0) {
 9752                                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
 9753                                 *now = asoc->time_last_sent;
 9754                                 *now_filled = 1;
 9755                         } else {
 9756                                 asoc->time_last_sent = *now;
 9757                         }
 9758                         *cnt_out += bundle_at;
 9759 #ifdef SCTP_AUDITING_ENABLED
 9760                         sctp_audit_log(0xC4, bundle_at);
 9761 #endif
 9762                         if (bundle_at) {
 9763                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
 9764                         }
 9765                         for (i = 0; i < bundle_at; i++) {
 9766                                 SCTP_STAT_INCR(sctps_sendretransdata);
 9767                                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
 9768                                 /*
 9769                                  * When we have a revoked data, and we
 9770                                  * retransmit it, then we clear the revoked
 9771                                  * flag since this flag dictates if we
 9772                                  * subtracted from the fs
 9773                                  */
 9774                                 if (data_list[i]->rec.data.chunk_was_revoked) {
 9775                                         /* Deflate the cwnd */
 9776                                         data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
 9777                                         data_list[i]->rec.data.chunk_was_revoked = 0;
 9778                                 }
 9779                                 data_list[i]->snd_count++;
 9780                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
 9781                                 /* record the time */
 9782                                 data_list[i]->sent_rcv_time = asoc->time_last_sent;
 9783                                 if (data_list[i]->book_size_scale) {
 9784                                         /*
 9785                                          * need to double the book size on
 9786                                          * this one
 9787                                          */
 9788                                         data_list[i]->book_size_scale = 0;
 9789                                         /*
 9790                                          * Since we double the booksize, we
 9791                                          * must also double the output queue
 9792                                          * size, since this get shrunk when
 9793                                          * we free by this amount.
 9794                                          */
 9795                                         atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
 9796                                         data_list[i]->book_size *= 2;
 9797 
 9798 
 9799                                 } else {
 9800                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
 9801                                                 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
 9802                                                     asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
 9803                                         }
 9804                                         asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
 9805                                             (uint32_t) (data_list[i]->send_size +
 9806                                             SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
 9807                                 }
 9808                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
 9809                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
 9810                                             data_list[i]->whoTo->flight_size,
 9811                                             data_list[i]->book_size,
 9812                                             (uintptr_t) data_list[i]->whoTo,
 9813                                             data_list[i]->rec.data.TSN_seq);
 9814                                 }
 9815                                 sctp_flight_size_increase(data_list[i]);
 9816                                 sctp_total_flight_increase(stcb, data_list[i]);
 9817                                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
 9818                                         /* SWS sender side engages */
 9819                                         asoc->peers_rwnd = 0;
 9820                                 }
 9821                                 if ((i == 0) &&
 9822                                     (data_list[i]->rec.data.doing_fast_retransmit)) {
 9823                                         SCTP_STAT_INCR(sctps_sendfastretrans);
 9824                                         if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
 9825                                             (tmr_started == 0)) {
 9826                                                 /*-
 9827                                                  * ok we just fast-retrans'd
 9828                                                  * the lowest TSN, i.e the
 9829                                                  * first on the list. In
 9830                                                  * this case we want to give
 9831                                                  * some more time to get a
 9832                                                  * SACK back without a
 9833                                                  * t3-expiring.
 9834                                                  */
 9835                                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
 9836                                                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
 9837                                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
 9838                                         }
 9839                                 }
 9840                         }
 9841                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
 9842                                 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
 9843                         }
 9844 #ifdef SCTP_AUDITING_ENABLED
 9845                         sctp_auditing(21, inp, stcb, NULL);
 9846 #endif
 9847                 } else {
 9848                         /* None will fit */
 9849                         return (1);
 9850                 }
 9851                 if (asoc->sent_queue_retran_cnt <= 0) {
 9852                         /* all done we have no more to retran */
 9853                         asoc->sent_queue_retran_cnt = 0;
 9854                         break;
 9855                 }
 9856                 if (one_chunk) {
 9857                         /* No more room in rwnd */
 9858                         return (1);
 9859                 }
 9860                 /* stop the for loop here. we sent out a packet */
 9861                 break;
 9862         }
 9863         return (0);
 9864 }
 9865 
 9866 static void
 9867 sctp_timer_validation(struct sctp_inpcb *inp,
 9868     struct sctp_tcb *stcb,
 9869     struct sctp_association *asoc)
 9870 {
 9871         struct sctp_nets *net;
 9872 
 9873         /* Validate that a timer is running somewhere */
 9874         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
 9875                 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
 9876                         /* Here is a timer */
 9877                         return;
 9878                 }
 9879         }
 9880         SCTP_TCB_LOCK_ASSERT(stcb);
 9881         /* Gak, we did not have a timer somewhere */
 9882         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
 9883         if (asoc->alternate) {
 9884                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
 9885         } else {
 9886                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
 9887         }
 9888         return;
 9889 }
 9890 
 9891 void
 9892 sctp_chunk_output(struct sctp_inpcb *inp,
 9893     struct sctp_tcb *stcb,
 9894     int from_where,
 9895     int so_locked
 9896 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 9897     SCTP_UNUSED
 9898 #endif
 9899 )
 9900 {
 9901         /*-
 9902          * Ok this is the generic chunk service queue. we must do the
 9903          * following:
 9904          * - See if there are retransmits pending, if so we must
 9905          *   do these first.
 9906          * - Service the stream queue that is next, moving any
 9907          *   message (note I must get a complete message i.e.
 9908          *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
 9909          *   TSN's
 9910          * - Check to see if the cwnd/rwnd allows any output, if so we
 9911          *   go ahead and fomulate and send the low level chunks. Making sure
 9912          *   to combine any control in the control chunk queue also.
 9913          */
 9914         struct sctp_association *asoc;
 9915         struct sctp_nets *net;
 9916         int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0;
 9917         unsigned int burst_cnt = 0;
 9918         struct timeval now;
 9919         int now_filled = 0;
 9920         int nagle_on;
 9921         int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
 9922         int un_sent = 0;
 9923         int fr_done;
 9924         unsigned int tot_frs = 0;
 9925 
 9926         asoc = &stcb->asoc;
 9927         /* The Nagle algorithm is only applied when handling a send call. */
 9928         if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
 9929                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
 9930                         nagle_on = 0;
 9931                 } else {
 9932                         nagle_on = 1;
 9933                 }
 9934         } else {
 9935                 nagle_on = 0;
 9936         }
 9937         SCTP_TCB_LOCK_ASSERT(stcb);
 9938 
 9939         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
 9940 
 9941         if ((un_sent <= 0) &&
 9942             (TAILQ_EMPTY(&asoc->control_send_queue)) &&
 9943             (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
 9944             (asoc->sent_queue_retran_cnt == 0)) {
 9945                 /* Nothing to do unless there is something to be sent left */
 9946                 return;
 9947         }
 9948         /*
 9949          * Do we have something to send, data or control AND a sack timer
 9950          * running, if so piggy-back the sack.
 9951          */
 9952         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
 9953                 sctp_send_sack(stcb, so_locked);
 9954                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
 9955         }
 9956         while (asoc->sent_queue_retran_cnt) {
 9957                 /*-
 9958                  * Ok, it is retransmission time only, we send out only ONE
 9959                  * packet with a single call off to the retran code.
 9960                  */
 9961                 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
 9962                         /*-
 9963                          * Special hook for handling cookiess discarded
 9964                          * by peer that carried data. Send cookie-ack only
 9965                          * and then the next call with get the retran's.
 9966                          */
 9967                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
 9968                             from_where,
 9969                             &now, &now_filled, frag_point, so_locked);
 9970                         return;
 9971                 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
 9972                         /* if its not from a HB then do it */
 9973                         fr_done = 0;
 9974                         ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
 9975                         if (fr_done) {
 9976                                 tot_frs++;
 9977                         }
 9978                 } else {
 9979                         /*
 9980                          * its from any other place, we don't allow retran
 9981                          * output (only control)
 9982                          */
 9983                         ret = 1;
 9984                 }
 9985                 if (ret > 0) {
 9986                         /* Can't send anymore */
 9987                         /*-
 9988                          * now lets push out control by calling med-level
 9989                          * output once. this assures that we WILL send HB's
 9990                          * if queued too.
 9991                          */
 9992                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
 9993                             from_where,
 9994                             &now, &now_filled, frag_point, so_locked);
 9995 #ifdef SCTP_AUDITING_ENABLED
 9996                         sctp_auditing(8, inp, stcb, NULL);
 9997 #endif
 9998                         sctp_timer_validation(inp, stcb, asoc);
 9999                         return;
10000                 }
10001                 if (ret < 0) {
10002                         /*-
10003                          * The count was off.. retran is not happening so do
10004                          * the normal retransmission.
10005                          */
10006 #ifdef SCTP_AUDITING_ENABLED
10007                         sctp_auditing(9, inp, stcb, NULL);
10008 #endif
10009                         if (ret == SCTP_RETRAN_EXIT) {
10010                                 return;
10011                         }
10012                         break;
10013                 }
10014                 if (from_where == SCTP_OUTPUT_FROM_T3) {
10015                         /* Only one transmission allowed out of a timeout */
10016 #ifdef SCTP_AUDITING_ENABLED
10017                         sctp_auditing(10, inp, stcb, NULL);
10018 #endif
10019                         /* Push out any control */
10020                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10021                             &now, &now_filled, frag_point, so_locked);
10022                         return;
10023                 }
10024                 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10025                         /* Hit FR burst limit */
10026                         return;
10027                 }
10028                 if ((num_out == 0) && (ret == 0)) {
10029                         /* No more retrans to send */
10030                         break;
10031                 }
10032         }
10033 #ifdef SCTP_AUDITING_ENABLED
10034         sctp_auditing(12, inp, stcb, NULL);
10035 #endif
10036         /* Check for bad destinations, if they exist move chunks around. */
10037         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10038                 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
10039                         /*-
10040                          * if possible move things off of this address we
10041                          * still may send below due to the dormant state but
10042                          * we try to find an alternate address to send to
10043                          * and if we have one we move all queued data on the
10044                          * out wheel to this alternate address.
10045                          */
10046                         if (net->ref_count > 1)
10047                                 sctp_move_chunks_from_net(stcb, net);
10048                 } else {
10049                         /*-
10050                          * if ((asoc->sat_network) || (net->addr_is_local))
10051                          * { burst_limit = asoc->max_burst *
10052                          * SCTP_SAT_NETWORK_BURST_INCR; }
10053                          */
10054                         if (asoc->max_burst > 0) {
10055                                 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10056                                         if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10057                                                 /*
10058                                                  * JRS - Use the congestion
10059                                                  * control given in the
10060                                                  * congestion control module
10061                                                  */
10062                                                 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10063                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10064                                                         sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10065                                                 }
10066                                                 SCTP_STAT_INCR(sctps_maxburstqueued);
10067                                         }
10068                                         net->fast_retran_ip = 0;
10069                                 } else {
10070                                         if (net->flight_size == 0) {
10071                                                 /*
10072                                                  * Should be decaying the
10073                                                  * cwnd here
10074                                                  */
10075                                                 ;
10076                                         }
10077                                 }
10078                         }
10079                 }
10080 
10081         }
10082         burst_cnt = 0;
10083         do {
10084                 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10085                     &reason_code, 0, from_where,
10086                     &now, &now_filled, frag_point, so_locked);
10087                 if (error) {
10088                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10089                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10090                                 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10091                         }
10092                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10093                                 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10094                                 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10095                         }
10096                         break;
10097                 }
10098                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10099 
10100                 tot_out += num_out;
10101                 burst_cnt++;
10102                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10103                         sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10104                         if (num_out == 0) {
10105                                 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10106                         }
10107                 }
10108                 if (nagle_on) {
10109                         /*
10110                          * When the Nagle algorithm is used, look at how
10111                          * much is unsent, then if its smaller than an MTU
10112                          * and we have data in flight we stop, except if we
10113                          * are handling a fragmented user message.
10114                          */
10115                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10116                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
10117                         if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10118                             (stcb->asoc.total_flight > 0) &&
10119                             ((stcb->asoc.locked_on_sending == NULL) ||
10120                             sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
10121                                 break;
10122                         }
10123                 }
10124                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10125                     TAILQ_EMPTY(&asoc->send_queue) &&
10126                     stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
10127                         /* Nothing left to send */
10128                         break;
10129                 }
10130                 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10131                         /* Nothing left to send */
10132                         break;
10133                 }
10134         } while (num_out &&
10135             ((asoc->max_burst == 0) ||
10136             SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10137             (burst_cnt < asoc->max_burst)));
10138 
10139         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10140                 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10141                         SCTP_STAT_INCR(sctps_maxburstqueued);
10142                         asoc->burst_limit_applied = 1;
10143                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10144                                 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10145                         }
10146                 } else {
10147                         asoc->burst_limit_applied = 0;
10148                 }
10149         }
10150         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10151                 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10152         }
10153         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10154             tot_out);
10155 
10156         /*-
10157          * Now we need to clean up the control chunk chain if a ECNE is on
10158          * it. It must be marked as UNSENT again so next call will continue
10159          * to send it until such time that we get a CWR, to remove it.
10160          */
10161         if (stcb->asoc.ecn_echo_cnt_onq)
10162                 sctp_fix_ecn_echo(asoc);
10163         return;
10164 }
10165 
10166 
10167 int
10168 sctp_output(
10169     struct sctp_inpcb *inp,
10170     struct mbuf *m,
10171     struct sockaddr *addr,
10172     struct mbuf *control,
10173     struct thread *p,
10174     int flags)
10175 {
10176         if (inp == NULL) {
10177                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10178                 return (EINVAL);
10179         }
10180         if (inp->sctp_socket == NULL) {
10181                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10182                 return (EINVAL);
10183         }
10184         return (sctp_sosend(inp->sctp_socket,
10185             addr,
10186             (struct uio *)NULL,
10187             m,
10188             control,
10189             flags, p
10190             ));
10191 }
10192 
10193 void
10194 send_forward_tsn(struct sctp_tcb *stcb,
10195     struct sctp_association *asoc)
10196 {
10197         struct sctp_tmit_chunk *chk;
10198         struct sctp_forward_tsn_chunk *fwdtsn;
10199         uint32_t advance_peer_ack_point;
10200 
10201         SCTP_TCB_LOCK_ASSERT(stcb);
10202         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10203                 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10204                         /* mark it to unsent */
10205                         chk->sent = SCTP_DATAGRAM_UNSENT;
10206                         chk->snd_count = 0;
10207                         /* Do we correct its output location? */
10208                         if (chk->whoTo) {
10209                                 sctp_free_remote_addr(chk->whoTo);
10210                                 chk->whoTo = NULL;
10211                         }
10212                         goto sctp_fill_in_rest;
10213                 }
10214         }
10215         /* Ok if we reach here we must build one */
10216         sctp_alloc_a_chunk(stcb, chk);
10217         if (chk == NULL) {
10218                 return;
10219         }
10220         asoc->fwd_tsn_cnt++;
10221         chk->copy_by_ref = 0;
10222         chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10223         chk->rec.chunk_id.can_take_data = 0;
10224         chk->asoc = asoc;
10225         chk->whoTo = NULL;
10226         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10227         if (chk->data == NULL) {
10228                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10229                 return;
10230         }
10231         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10232         chk->sent = SCTP_DATAGRAM_UNSENT;
10233         chk->snd_count = 0;
10234         TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10235         asoc->ctrl_queue_cnt++;
10236 sctp_fill_in_rest:
10237         /*-
10238          * Here we go through and fill out the part that deals with
10239          * stream/seq of the ones we skip.
10240          */
10241         SCTP_BUF_LEN(chk->data) = 0;
10242         {
10243                 struct sctp_tmit_chunk *at, *tp1, *last;
10244                 struct sctp_strseq *strseq;
10245                 unsigned int cnt_of_space, i, ovh;
10246                 unsigned int space_needed;
10247                 unsigned int cnt_of_skipped = 0;
10248 
10249                 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10250                         if (at->sent != SCTP_FORWARD_TSN_SKIP) {
10251                                 /* no more to look at */
10252                                 break;
10253                         }
10254                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10255                                 /* We don't report these */
10256                                 continue;
10257                         }
10258                         cnt_of_skipped++;
10259                 }
10260                 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10261                     (cnt_of_skipped * sizeof(struct sctp_strseq)));
10262 
10263                 cnt_of_space = M_TRAILINGSPACE(chk->data);
10264 
10265                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10266                         ovh = SCTP_MIN_OVERHEAD;
10267                 } else {
10268                         ovh = SCTP_MIN_V4_OVERHEAD;
10269                 }
10270                 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10271                         /* trim to a mtu size */
10272                         cnt_of_space = asoc->smallest_mtu - ovh;
10273                 }
10274                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10275                         sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10276                             0xff, 0, cnt_of_skipped,
10277                             asoc->advanced_peer_ack_point);
10278 
10279                 }
10280                 advance_peer_ack_point = asoc->advanced_peer_ack_point;
10281                 if (cnt_of_space < space_needed) {
10282                         /*-
10283                          * ok we must trim down the chunk by lowering the
10284                          * advance peer ack point.
10285                          */
10286                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10287                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10288                                     0xff, 0xff, cnt_of_space,
10289                                     space_needed);
10290                         }
10291                         cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10292                         cnt_of_skipped /= sizeof(struct sctp_strseq);
10293                         /*-
10294                          * Go through and find the TSN that will be the one
10295                          * we report.
10296                          */
10297                         at = TAILQ_FIRST(&asoc->sent_queue);
10298                         if (at != NULL) {
10299                                 for (i = 0; i < cnt_of_skipped; i++) {
10300                                         tp1 = TAILQ_NEXT(at, sctp_next);
10301                                         if (tp1 == NULL) {
10302                                                 break;
10303                                         }
10304                                         at = tp1;
10305                                 }
10306                         }
10307                         if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10308                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10309                                     0xff, cnt_of_skipped, at->rec.data.TSN_seq,
10310                                     asoc->advanced_peer_ack_point);
10311                         }
10312                         last = at;
10313                         /*-
10314                          * last now points to last one I can report, update
10315                          * peer ack point
10316                          */
10317                         if (last)
10318                                 advance_peer_ack_point = last->rec.data.TSN_seq;
10319                         space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10320                             cnt_of_skipped * sizeof(struct sctp_strseq);
10321                 }
10322                 chk->send_size = space_needed;
10323                 /* Setup the chunk */
10324                 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10325                 fwdtsn->ch.chunk_length = htons(chk->send_size);
10326                 fwdtsn->ch.chunk_flags = 0;
10327                 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10328                 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10329                 SCTP_BUF_LEN(chk->data) = chk->send_size;
10330                 fwdtsn++;
10331                 /*-
10332                  * Move pointer to after the fwdtsn and transfer to the
10333                  * strseq pointer.
10334                  */
10335                 strseq = (struct sctp_strseq *)fwdtsn;
10336                 /*-
10337                  * Now populate the strseq list. This is done blindly
10338                  * without pulling out duplicate stream info. This is
10339                  * inefficent but won't harm the process since the peer will
10340                  * look at these in sequence and will thus release anything.
10341                  * It could mean we exceed the PMTU and chop off some that
10342                  * we could have included.. but this is unlikely (aka 1432/4
10343                  * would mean 300+ stream seq's would have to be reported in
10344                  * one FWD-TSN. With a bit of work we can later FIX this to
10345                  * optimize and pull out duplcates.. but it does add more
10346                  * overhead. So for now... not!
10347                  */
10348                 at = TAILQ_FIRST(&asoc->sent_queue);
10349                 for (i = 0; i < cnt_of_skipped; i++) {
10350                         tp1 = TAILQ_NEXT(at, sctp_next);
10351                         if (tp1 == NULL)
10352                                 break;
10353                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10354                                 /* We don't report these */
10355                                 i--;
10356                                 at = tp1;
10357                                 continue;
10358                         }
10359                         if (at->rec.data.TSN_seq == advance_peer_ack_point) {
10360                                 at->rec.data.fwd_tsn_cnt = 0;
10361                         }
10362                         strseq->stream = ntohs(at->rec.data.stream_number);
10363                         strseq->sequence = ntohs(at->rec.data.stream_seq);
10364                         strseq++;
10365                         at = tp1;
10366                 }
10367         }
10368         return;
10369 }
10370 
10371 void
10372 sctp_send_sack(struct sctp_tcb *stcb, int so_locked
10373 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10374     SCTP_UNUSED
10375 #endif
10376 )
10377 {
10378         /*-
10379          * Queue up a SACK or NR-SACK in the control queue.
10380          * We must first check to see if a SACK or NR-SACK is
10381          * somehow on the control queue.
10382          * If so, we will take and and remove the old one.
10383          */
10384         struct sctp_association *asoc;
10385         struct sctp_tmit_chunk *chk, *a_chk;
10386         struct sctp_sack_chunk *sack;
10387         struct sctp_nr_sack_chunk *nr_sack;
10388         struct sctp_gap_ack_block *gap_descriptor;
10389         struct sack_track *selector;
10390         int mergeable = 0;
10391         int offset;
10392         caddr_t limit;
10393         uint32_t *dup;
10394         int limit_reached = 0;
10395         unsigned int i, siz, j;
10396         unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10397         int num_dups = 0;
10398         int space_req;
10399         uint32_t highest_tsn;
10400         uint8_t flags;
10401         uint8_t type;
10402         uint8_t tsn_map;
10403 
10404         if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
10405             (stcb->asoc.peer_supports_nr_sack == 1)) {
10406                 type = SCTP_NR_SELECTIVE_ACK;
10407         } else {
10408                 type = SCTP_SELECTIVE_ACK;
10409         }
10410         a_chk = NULL;
10411         asoc = &stcb->asoc;
10412         SCTP_TCB_LOCK_ASSERT(stcb);
10413         if (asoc->last_data_chunk_from == NULL) {
10414                 /* Hmm we never received anything */
10415                 return;
10416         }
10417         sctp_slide_mapping_arrays(stcb);
10418         sctp_set_rwnd(stcb, asoc);
10419         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10420                 if (chk->rec.chunk_id.id == type) {
10421                         /* Hmm, found a sack already on queue, remove it */
10422                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10423                         asoc->ctrl_queue_cnt--;
10424                         a_chk = chk;
10425                         if (a_chk->data) {
10426                                 sctp_m_freem(a_chk->data);
10427                                 a_chk->data = NULL;
10428                         }
10429                         if (a_chk->whoTo) {
10430                                 sctp_free_remote_addr(a_chk->whoTo);
10431                                 a_chk->whoTo = NULL;
10432                         }
10433                         break;
10434                 }
10435         }
10436         if (a_chk == NULL) {
10437                 sctp_alloc_a_chunk(stcb, a_chk);
10438                 if (a_chk == NULL) {
10439                         /* No memory so we drop the idea, and set a timer */
10440                         if (stcb->asoc.delayed_ack) {
10441                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10442                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10443                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10444                                     stcb->sctp_ep, stcb, NULL);
10445                         } else {
10446                                 stcb->asoc.send_sack = 1;
10447                         }
10448                         return;
10449                 }
10450                 a_chk->copy_by_ref = 0;
10451                 a_chk->rec.chunk_id.id = type;
10452                 a_chk->rec.chunk_id.can_take_data = 1;
10453         }
10454         /* Clear our pkt counts */
10455         asoc->data_pkts_seen = 0;
10456 
10457         a_chk->asoc = asoc;
10458         a_chk->snd_count = 0;
10459         a_chk->send_size = 0;   /* fill in later */
10460         a_chk->sent = SCTP_DATAGRAM_UNSENT;
10461         a_chk->whoTo = NULL;
10462 
10463         if ((asoc->numduptsns) ||
10464             (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE))) {
10465                 /*-
10466                  * Ok, we have some duplicates or the destination for the
10467                  * sack is unreachable, lets see if we can select an
10468                  * alternate than asoc->last_data_chunk_from
10469                  */
10470                 if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) &&
10471                     (asoc->used_alt_onsack > asoc->numnets)) {
10472                         /* We used an alt last time, don't this time */
10473                         a_chk->whoTo = NULL;
10474                 } else {
10475                         asoc->used_alt_onsack++;
10476                         a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10477                 }
10478                 if (a_chk->whoTo == NULL) {
10479                         /* Nope, no alternate */
10480                         a_chk->whoTo = asoc->last_data_chunk_from;
10481                         asoc->used_alt_onsack = 0;
10482                 }
10483         } else {
10484                 /*
10485                  * No duplicates so we use the last place we received data
10486                  * from.
10487                  */
10488                 asoc->used_alt_onsack = 0;
10489                 a_chk->whoTo = asoc->last_data_chunk_from;
10490         }
10491         if (a_chk->whoTo) {
10492                 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10493         }
10494         if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10495                 highest_tsn = asoc->highest_tsn_inside_map;
10496         } else {
10497                 highest_tsn = asoc->highest_tsn_inside_nr_map;
10498         }
10499         if (highest_tsn == asoc->cumulative_tsn) {
10500                 /* no gaps */
10501                 if (type == SCTP_SELECTIVE_ACK) {
10502                         space_req = sizeof(struct sctp_sack_chunk);
10503                 } else {
10504                         space_req = sizeof(struct sctp_nr_sack_chunk);
10505                 }
10506         } else {
10507                 /* gaps get a cluster */
10508                 space_req = MCLBYTES;
10509         }
10510         /* Ok now lets formulate a MBUF with our sack */
10511         a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10512         if ((a_chk->data == NULL) ||
10513             (a_chk->whoTo == NULL)) {
10514                 /* rats, no mbuf memory */
10515                 if (a_chk->data) {
10516                         /* was a problem with the destination */
10517                         sctp_m_freem(a_chk->data);
10518                         a_chk->data = NULL;
10519                 }
10520                 sctp_free_a_chunk(stcb, a_chk, so_locked);
10521                 /* sa_ignore NO_NULL_CHK */
10522                 if (stcb->asoc.delayed_ack) {
10523                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10524                             stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10525                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10526                             stcb->sctp_ep, stcb, NULL);
10527                 } else {
10528                         stcb->asoc.send_sack = 1;
10529                 }
10530                 return;
10531         }
10532         /* ok, lets go through and fill it in */
10533         SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10534         space = M_TRAILINGSPACE(a_chk->data);
10535         if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10536                 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10537         }
10538         limit = mtod(a_chk->data, caddr_t);
10539         limit += space;
10540 
10541         flags = 0;
10542 
10543         if ((asoc->sctp_cmt_on_off > 0) &&
10544             SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10545                 /*-
10546                  * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10547                  * received, then set high bit to 1, else 0. Reset
10548                  * pkts_rcvd.
10549                  */
10550                 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10551                 asoc->cmt_dac_pkts_rcvd = 0;
10552         }
10553 #ifdef SCTP_ASOCLOG_OF_TSNS
10554         stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10555         stcb->asoc.cumack_log_atsnt++;
10556         if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10557                 stcb->asoc.cumack_log_atsnt = 0;
10558         }
10559 #endif
10560         /* reset the readers interpretation */
10561         stcb->freed_by_sorcv_sincelast = 0;
10562 
10563         if (type == SCTP_SELECTIVE_ACK) {
10564                 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10565                 nr_sack = NULL;
10566                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10567                 if (highest_tsn > asoc->mapping_array_base_tsn) {
10568                         siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10569                 } else {
10570                         siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10571                 }
10572         } else {
10573                 sack = NULL;
10574                 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10575                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10576                 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10577                         siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10578                 } else {
10579                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10580                 }
10581         }
10582 
10583         if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10584                 offset = 1;
10585         } else {
10586                 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10587         }
10588         if (((type == SCTP_SELECTIVE_ACK) &&
10589             SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10590             ((type == SCTP_NR_SELECTIVE_ACK) &&
10591             SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10592                 /* we have a gap .. maybe */
10593                 for (i = 0; i < siz; i++) {
10594                         tsn_map = asoc->mapping_array[i];
10595                         if (type == SCTP_SELECTIVE_ACK) {
10596                                 tsn_map |= asoc->nr_mapping_array[i];
10597                         }
10598                         if (i == 0) {
10599                                 /*
10600                                  * Clear all bits corresponding to TSNs
10601                                  * smaller or equal to the cumulative TSN.
10602                                  */
10603                                 tsn_map &= (~0 << (1 - offset));
10604                         }
10605                         selector = &sack_array[tsn_map];
10606                         if (mergeable && selector->right_edge) {
10607                                 /*
10608                                  * Backup, left and right edges were ok to
10609                                  * merge.
10610                                  */
10611                                 num_gap_blocks--;
10612                                 gap_descriptor--;
10613                         }
10614                         if (selector->num_entries == 0)
10615                                 mergeable = 0;
10616                         else {
10617                                 for (j = 0; j < selector->num_entries; j++) {
10618                                         if (mergeable && selector->right_edge) {
10619                                                 /*
10620                                                  * do a merge by NOT setting
10621                                                  * the left side
10622                                                  */
10623                                                 mergeable = 0;
10624                                         } else {
10625                                                 /*
10626                                                  * no merge, set the left
10627                                                  * side
10628                                                  */
10629                                                 mergeable = 0;
10630                                                 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10631                                         }
10632                                         gap_descriptor->end = htons((selector->gaps[j].end + offset));
10633                                         num_gap_blocks++;
10634                                         gap_descriptor++;
10635                                         if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10636                                                 /* no more room */
10637                                                 limit_reached = 1;
10638                                                 break;
10639                                         }
10640                                 }
10641                                 if (selector->left_edge) {
10642                                         mergeable = 1;
10643                                 }
10644                         }
10645                         if (limit_reached) {
10646                                 /* Reached the limit stop */
10647                                 break;
10648                         }
10649                         offset += 8;
10650                 }
10651         }
10652         if ((type == SCTP_NR_SELECTIVE_ACK) &&
10653             (limit_reached == 0)) {
10654 
10655                 mergeable = 0;
10656 
10657                 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10658                         siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10659                 } else {
10660                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10661                 }
10662 
10663                 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10664                         offset = 1;
10665                 } else {
10666                         offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10667                 }
10668                 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10669                         /* we have a gap .. maybe */
10670                         for (i = 0; i < siz; i++) {
10671                                 tsn_map = asoc->nr_mapping_array[i];
10672                                 if (i == 0) {
10673                                         /*
10674                                          * Clear all bits corresponding to
10675                                          * TSNs smaller or equal to the
10676                                          * cumulative TSN.
10677                                          */
10678                                         tsn_map &= (~0 << (1 - offset));
10679                                 }
10680                                 selector = &sack_array[tsn_map];
10681                                 if (mergeable && selector->right_edge) {
10682                                         /*
10683                                          * Backup, left and right edges were
10684                                          * ok to merge.
10685                                          */
10686                                         num_nr_gap_blocks--;
10687                                         gap_descriptor--;
10688                                 }
10689                                 if (selector->num_entries == 0)
10690                                         mergeable = 0;
10691                                 else {
10692                                         for (j = 0; j < selector->num_entries; j++) {
10693                                                 if (mergeable && selector->right_edge) {
10694                                                         /*
10695                                                          * do a merge by NOT
10696                                                          * setting the left
10697                                                          * side
10698                                                          */
10699                                                         mergeable = 0;
10700                                                 } else {
10701                                                         /*
10702                                                          * no merge, set the
10703                                                          * left side
10704                                                          */
10705                                                         mergeable = 0;
10706                                                         gap_descriptor->start = htons((selector->gaps[j].start + offset));
10707                                                 }
10708                                                 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10709                                                 num_nr_gap_blocks++;
10710                                                 gap_descriptor++;
10711                                                 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10712                                                         /* no more room */
10713                                                         limit_reached = 1;
10714                                                         break;
10715                                                 }
10716                                         }
10717                                         if (selector->left_edge) {
10718                                                 mergeable = 1;
10719                                         }
10720                                 }
10721                                 if (limit_reached) {
10722                                         /* Reached the limit stop */
10723                                         break;
10724                                 }
10725                                 offset += 8;
10726                         }
10727                 }
10728         }
10729         /* now we must add any dups we are going to report. */
10730         if ((limit_reached == 0) && (asoc->numduptsns)) {
10731                 dup = (uint32_t *) gap_descriptor;
10732                 for (i = 0; i < asoc->numduptsns; i++) {
10733                         *dup = htonl(asoc->dup_tsns[i]);
10734                         dup++;
10735                         num_dups++;
10736                         if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10737                                 /* no more room */
10738                                 break;
10739                         }
10740                 }
10741                 asoc->numduptsns = 0;
10742         }
10743         /*
10744          * now that the chunk is prepared queue it to the control chunk
10745          * queue.
10746          */
10747         if (type == SCTP_SELECTIVE_ACK) {
10748                 a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10749                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10750                     num_dups * sizeof(int32_t);
10751                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10752                 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10753                 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10754                 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10755                 sack->sack.num_dup_tsns = htons(num_dups);
10756                 sack->ch.chunk_type = type;
10757                 sack->ch.chunk_flags = flags;
10758                 sack->ch.chunk_length = htons(a_chk->send_size);
10759         } else {
10760                 a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10761                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10762                     num_dups * sizeof(int32_t);
10763                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10764                 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10765                 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10766                 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10767                 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10768                 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10769                 nr_sack->nr_sack.reserved = 0;
10770                 nr_sack->ch.chunk_type = type;
10771                 nr_sack->ch.chunk_flags = flags;
10772                 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10773         }
10774         TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10775         asoc->my_last_reported_rwnd = asoc->my_rwnd;
10776         asoc->ctrl_queue_cnt++;
10777         asoc->send_sack = 0;
10778         SCTP_STAT_INCR(sctps_sendsacks);
10779         return;
10780 }
10781 
10782 void
10783 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10784 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10785     SCTP_UNUSED
10786 #endif
10787 )
10788 {
10789         struct mbuf *m_abort;
10790         struct mbuf *m_out = NULL, *m_end = NULL;
10791         struct sctp_abort_chunk *abort = NULL;
10792         int sz;
10793         uint32_t auth_offset = 0;
10794         struct sctp_auth_chunk *auth = NULL;
10795         struct sctp_nets *net;
10796 
10797         /*-
10798          * Add an AUTH chunk, if chunk requires it and save the offset into
10799          * the chain for AUTH
10800          */
10801         if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10802             stcb->asoc.peer_auth_chunks)) {
10803                 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10804                     stcb, SCTP_ABORT_ASSOCIATION);
10805                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10806         }
10807         SCTP_TCB_LOCK_ASSERT(stcb);
10808         m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10809         if (m_abort == NULL) {
10810                 /* no mbuf's */
10811                 if (m_out)
10812                         sctp_m_freem(m_out);
10813                 return;
10814         }
10815         /* link in any error */
10816         SCTP_BUF_NEXT(m_abort) = operr;
10817         sz = 0;
10818         if (operr) {
10819                 struct mbuf *n;
10820 
10821                 n = operr;
10822                 while (n) {
10823                         sz += SCTP_BUF_LEN(n);
10824                         n = SCTP_BUF_NEXT(n);
10825                 }
10826         }
10827         SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10828         if (m_out == NULL) {
10829                 /* NO Auth chunk prepended, so reserve space in front */
10830                 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10831                 m_out = m_abort;
10832         } else {
10833                 /* Put AUTH chunk at the front of the chain */
10834                 SCTP_BUF_NEXT(m_end) = m_abort;
10835         }
10836         if (stcb->asoc.alternate) {
10837                 net = stcb->asoc.alternate;
10838         } else {
10839                 net = stcb->asoc.primary_destination;
10840         }
10841         /* fill in the ABORT chunk */
10842         abort = mtod(m_abort, struct sctp_abort_chunk *);
10843         abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10844         abort->ch.chunk_flags = 0;
10845         abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10846 
10847         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10848             (struct sockaddr *)&net->ro._l_addr,
10849             m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10850             stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10851             stcb->asoc.primary_destination->port, so_locked, NULL, NULL);
10852         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10853 }
10854 
10855 void
10856 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10857     struct sctp_nets *net,
10858     int reflect_vtag)
10859 {
10860         /* formulate and SEND a SHUTDOWN-COMPLETE */
10861         struct mbuf *m_shutdown_comp;
10862         struct sctp_shutdown_complete_chunk *shutdown_complete;
10863         uint32_t vtag;
10864         uint8_t flags;
10865 
10866         m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10867         if (m_shutdown_comp == NULL) {
10868                 /* no mbuf's */
10869                 return;
10870         }
10871         if (reflect_vtag) {
10872                 flags = SCTP_HAD_NO_TCB;
10873                 vtag = stcb->asoc.my_vtag;
10874         } else {
10875                 flags = 0;
10876                 vtag = stcb->asoc.peer_vtag;
10877         }
10878         shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10879         shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10880         shutdown_complete->ch.chunk_flags = flags;
10881         shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10882         SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10883         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10884             (struct sockaddr *)&net->ro._l_addr,
10885             m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
10886             stcb->sctp_ep->sctp_lport, stcb->rport,
10887             htonl(vtag),
10888             net->port, SCTP_SO_NOT_LOCKED, NULL, NULL);
10889         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10890         return;
10891 }
10892 
10893 void
10894 sctp_send_shutdown_complete2(struct mbuf *m, struct sctphdr *sh,
10895     uint32_t vrf_id, uint16_t port)
10896 {
10897         /* formulate and SEND a SHUTDOWN-COMPLETE */
10898         struct mbuf *o_pak;
10899         struct mbuf *mout;
10900         struct ip *iph;
10901         struct udphdr *udp = NULL;
10902         int offset_out, len, mlen;
10903         struct sctp_shutdown_complete_msg *comp_cp;
10904 
10905 #ifdef INET
10906         struct ip *iph_out;
10907 
10908 #endif
10909 #ifdef INET6
10910         struct ip6_hdr *ip6, *ip6_out;
10911 
10912 #endif
10913 
10914         iph = mtod(m, struct ip *);
10915         switch (iph->ip_v) {
10916 #ifdef INET
10917         case IPVERSION:
10918                 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10919                 break;
10920 #endif
10921 #ifdef INET6
10922         case IPV6_VERSION >> 4:
10923                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10924                 break;
10925 #endif
10926         default:
10927                 return;
10928         }
10929         if (port) {
10930                 len += sizeof(struct udphdr);
10931         }
10932         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10933         if (mout == NULL) {
10934                 return;
10935         }
10936         SCTP_BUF_RESV_UF(mout, max_linkhdr);
10937         SCTP_BUF_LEN(mout) = len;
10938         SCTP_BUF_NEXT(mout) = NULL;
10939         if (m->m_flags & M_FLOWID) {
10940                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
10941                 mout->m_flags |= M_FLOWID;
10942         }
10943 #ifdef INET
10944         iph_out = NULL;
10945 #endif
10946 #ifdef INET6
10947         ip6_out = NULL;
10948 #endif
10949         offset_out = 0;
10950 
10951         switch (iph->ip_v) {
10952 #ifdef INET
10953         case IPVERSION:
10954                 iph_out = mtod(mout, struct ip *);
10955 
10956                 /* Fill in the IP header for the ABORT */
10957                 iph_out->ip_v = IPVERSION;
10958                 iph_out->ip_hl = (sizeof(struct ip) / 4);
10959                 iph_out->ip_tos = (u_char)0;
10960                 iph_out->ip_id = 0;
10961                 iph_out->ip_off = 0;
10962                 iph_out->ip_ttl = MAXTTL;
10963                 if (port) {
10964                         iph_out->ip_p = IPPROTO_UDP;
10965                 } else {
10966                         iph_out->ip_p = IPPROTO_SCTP;
10967                 }
10968                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10969                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10970 
10971                 /* let IP layer calculate this */
10972                 iph_out->ip_sum = 0;
10973                 offset_out += sizeof(*iph_out);
10974                 comp_cp = (struct sctp_shutdown_complete_msg *)(
10975                     (caddr_t)iph_out + offset_out);
10976                 break;
10977 #endif
10978 #ifdef INET6
10979         case IPV6_VERSION >> 4:
10980                 ip6 = (struct ip6_hdr *)iph;
10981                 ip6_out = mtod(mout, struct ip6_hdr *);
10982 
10983                 /* Fill in the IPv6 header for the ABORT */
10984                 ip6_out->ip6_flow = ip6->ip6_flow;
10985                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10986                 if (port) {
10987                         ip6_out->ip6_nxt = IPPROTO_UDP;
10988                 } else {
10989                         ip6_out->ip6_nxt = IPPROTO_SCTP;
10990                 }
10991                 ip6_out->ip6_src = ip6->ip6_dst;
10992                 ip6_out->ip6_dst = ip6->ip6_src;
10993                 /*
10994                  * ?? The old code had both the iph len + payload, I think
10995                  * this is wrong and would never have worked
10996                  */
10997                 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10998                 offset_out += sizeof(*ip6_out);
10999                 comp_cp = (struct sctp_shutdown_complete_msg *)(
11000                     (caddr_t)ip6_out + offset_out);
11001                 break;
11002 #endif                          /* INET6 */
11003         default:
11004                 /* Currently not supported. */
11005                 sctp_m_freem(mout);
11006                 return;
11007         }
11008         if (port) {
11009                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11010                         sctp_m_freem(mout);
11011                         return;
11012                 }
11013                 udp = (struct udphdr *)comp_cp;
11014                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11015                 udp->uh_dport = port;
11016                 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
11017 #ifdef INET
11018                 if (iph_out)
11019                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11020 #endif
11021                 offset_out += sizeof(struct udphdr);
11022                 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
11023         }
11024         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11025                 /* no mbuf's */
11026                 sctp_m_freem(mout);
11027                 return;
11028         }
11029         /* Now copy in and fill in the ABORT tags etc. */
11030         comp_cp->sh.src_port = sh->dest_port;
11031         comp_cp->sh.dest_port = sh->src_port;
11032         comp_cp->sh.checksum = 0;
11033         comp_cp->sh.v_tag = sh->v_tag;
11034         comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
11035         comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
11036         comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
11037 
11038 #ifdef INET
11039         if (iph_out != NULL) {
11040                 sctp_route_t ro;
11041                 int ret;
11042 
11043                 mlen = SCTP_BUF_LEN(mout);
11044                 bzero(&ro, sizeof ro);
11045                 /* set IPv4 length */
11046                 iph_out->ip_len = mlen;
11047 #ifdef  SCTP_PACKET_LOGGING
11048                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11049                         sctp_packet_log(mout, mlen);
11050 #endif
11051                 if (port) {
11052 #if defined(SCTP_WITH_NO_CSUM)
11053                         SCTP_STAT_INCR(sctps_sendnocrc);
11054 #else
11055                         comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
11056                         SCTP_STAT_INCR(sctps_sendswcrc);
11057 #endif
11058                         SCTP_ENABLE_UDP_CSUM(mout);
11059                 } else {
11060 #if defined(SCTP_WITH_NO_CSUM)
11061                         SCTP_STAT_INCR(sctps_sendnocrc);
11062 #else
11063                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11064                         mout->m_pkthdr.csum_data = 0;
11065                         SCTP_STAT_INCR(sctps_sendhwcrc);
11066 #endif
11067                 }
11068                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11069                 /* out it goes */
11070                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11071 
11072                 /* Free the route if we got one back */
11073                 if (ro.ro_rt)
11074                         RTFREE(ro.ro_rt);
11075         }
11076 #endif
11077 #ifdef INET6
11078         if (ip6_out != NULL) {
11079                 struct route_in6 ro;
11080                 int ret;
11081                 struct ifnet *ifp = NULL;
11082 
11083                 bzero(&ro, sizeof(ro));
11084                 mlen = SCTP_BUF_LEN(mout);
11085 #ifdef  SCTP_PACKET_LOGGING
11086                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11087                         sctp_packet_log(mout, mlen);
11088 #endif
11089                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11090                 if (port) {
11091 #if defined(SCTP_WITH_NO_CSUM)
11092                         SCTP_STAT_INCR(sctps_sendnocrc);
11093 #else
11094                         comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11095                         SCTP_STAT_INCR(sctps_sendswcrc);
11096 #endif
11097                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
11098                                 udp->uh_sum = 0xffff;
11099                         }
11100                 } else {
11101 #if defined(SCTP_WITH_NO_CSUM)
11102                         SCTP_STAT_INCR(sctps_sendnocrc);
11103 #else
11104                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11105                         mout->m_pkthdr.csum_data = 0;
11106                         SCTP_STAT_INCR(sctps_sendhwcrc);
11107 #endif
11108                 }
11109                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11110 
11111                 /* Free the route if we got one back */
11112                 if (ro.ro_rt)
11113                         RTFREE(ro.ro_rt);
11114         }
11115 #endif
11116         SCTP_STAT_INCR(sctps_sendpackets);
11117         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11118         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11119         return;
11120 
11121 }
11122 
11123 void
11124 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked
11125 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11126     SCTP_UNUSED
11127 #endif
11128 )
11129 {
11130         struct sctp_tmit_chunk *chk;
11131         struct sctp_heartbeat_chunk *hb;
11132         struct timeval now;
11133 
11134         SCTP_TCB_LOCK_ASSERT(stcb);
11135         if (net == NULL) {
11136                 return;
11137         }
11138         (void)SCTP_GETTIME_TIMEVAL(&now);
11139         switch (net->ro._l_addr.sa.sa_family) {
11140 #ifdef INET
11141         case AF_INET:
11142                 break;
11143 #endif
11144 #ifdef INET6
11145         case AF_INET6:
11146                 break;
11147 #endif
11148         default:
11149                 return;
11150         }
11151         sctp_alloc_a_chunk(stcb, chk);
11152         if (chk == NULL) {
11153                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11154                 return;
11155         }
11156         chk->copy_by_ref = 0;
11157         chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11158         chk->rec.chunk_id.can_take_data = 1;
11159         chk->asoc = &stcb->asoc;
11160         chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11161 
11162         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11163         if (chk->data == NULL) {
11164                 sctp_free_a_chunk(stcb, chk, so_locked);
11165                 return;
11166         }
11167         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11168         SCTP_BUF_LEN(chk->data) = chk->send_size;
11169         chk->sent = SCTP_DATAGRAM_UNSENT;
11170         chk->snd_count = 0;
11171         chk->whoTo = net;
11172         atomic_add_int(&chk->whoTo->ref_count, 1);
11173         /* Now we have a mbuf that we can fill in with the details */
11174         hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11175         memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11176         /* fill out chunk header */
11177         hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11178         hb->ch.chunk_flags = 0;
11179         hb->ch.chunk_length = htons(chk->send_size);
11180         /* Fill out hb parameter */
11181         hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11182         hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11183         hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11184         hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11185         /* Did our user request this one, put it in */
11186         hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family;
11187         hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11188         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11189                 /*
11190                  * we only take from the entropy pool if the address is not
11191                  * confirmed.
11192                  */
11193                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11194                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11195         } else {
11196                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11197                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11198         }
11199         switch (net->ro._l_addr.sa.sa_family) {
11200 #ifdef INET
11201         case AF_INET:
11202                 memcpy(hb->heartbeat.hb_info.address,
11203                     &net->ro._l_addr.sin.sin_addr,
11204                     sizeof(net->ro._l_addr.sin.sin_addr));
11205                 break;
11206 #endif
11207 #ifdef INET6
11208         case AF_INET6:
11209                 memcpy(hb->heartbeat.hb_info.address,
11210                     &net->ro._l_addr.sin6.sin6_addr,
11211                     sizeof(net->ro._l_addr.sin6.sin6_addr));
11212                 break;
11213 #endif
11214         default:
11215                 return;
11216                 break;
11217         }
11218         net->hb_responded = 0;
11219         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11220         stcb->asoc.ctrl_queue_cnt++;
11221         SCTP_STAT_INCR(sctps_sendheartbeat);
11222         return;
11223 }
11224 
11225 void
11226 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11227     uint32_t high_tsn)
11228 {
11229         struct sctp_association *asoc;
11230         struct sctp_ecne_chunk *ecne;
11231         struct sctp_tmit_chunk *chk;
11232 
11233         if (net == NULL) {
11234                 return;
11235         }
11236         asoc = &stcb->asoc;
11237         SCTP_TCB_LOCK_ASSERT(stcb);
11238         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11239                 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11240                         /* found a previous ECN_ECHO update it if needed */
11241                         uint32_t cnt, ctsn;
11242 
11243                         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11244                         ctsn = ntohl(ecne->tsn);
11245                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11246                                 ecne->tsn = htonl(high_tsn);
11247                                 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11248                         }
11249                         cnt = ntohl(ecne->num_pkts_since_cwr);
11250                         cnt++;
11251                         ecne->num_pkts_since_cwr = htonl(cnt);
11252                         return;
11253                 }
11254         }
11255         /* nope could not find one to update so we must build one */
11256         sctp_alloc_a_chunk(stcb, chk);
11257         if (chk == NULL) {
11258                 return;
11259         }
11260         chk->copy_by_ref = 0;
11261         SCTP_STAT_INCR(sctps_queue_upd_ecne);
11262         chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11263         chk->rec.chunk_id.can_take_data = 0;
11264         chk->asoc = &stcb->asoc;
11265         chk->send_size = sizeof(struct sctp_ecne_chunk);
11266         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11267         if (chk->data == NULL) {
11268                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11269                 return;
11270         }
11271         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11272         SCTP_BUF_LEN(chk->data) = chk->send_size;
11273         chk->sent = SCTP_DATAGRAM_UNSENT;
11274         chk->snd_count = 0;
11275         chk->whoTo = net;
11276         atomic_add_int(&chk->whoTo->ref_count, 1);
11277 
11278         stcb->asoc.ecn_echo_cnt_onq++;
11279         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11280         ecne->ch.chunk_type = SCTP_ECN_ECHO;
11281         ecne->ch.chunk_flags = 0;
11282         ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11283         ecne->tsn = htonl(high_tsn);
11284         ecne->num_pkts_since_cwr = htonl(1);
11285         TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11286         asoc->ctrl_queue_cnt++;
11287 }
11288 
11289 void
11290 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11291     struct mbuf *m, int iphlen, int bad_crc)
11292 {
11293         struct sctp_association *asoc;
11294         struct sctp_pktdrop_chunk *drp;
11295         struct sctp_tmit_chunk *chk;
11296         uint8_t *datap;
11297         int len;
11298         int was_trunc = 0;
11299         struct ip *iph;
11300 
11301 #ifdef INET6
11302         struct ip6_hdr *ip6h;
11303 
11304 #endif
11305         int fullsz = 0, extra = 0;
11306         long spc;
11307         int offset;
11308         struct sctp_chunkhdr *ch, chunk_buf;
11309         unsigned int chk_length;
11310 
11311         if (!stcb) {
11312                 return;
11313         }
11314         asoc = &stcb->asoc;
11315         SCTP_TCB_LOCK_ASSERT(stcb);
11316         if (asoc->peer_supports_pktdrop == 0) {
11317                 /*-
11318                  * peer must declare support before I send one.
11319                  */
11320                 return;
11321         }
11322         if (stcb->sctp_socket == NULL) {
11323                 return;
11324         }
11325         sctp_alloc_a_chunk(stcb, chk);
11326         if (chk == NULL) {
11327                 return;
11328         }
11329         chk->copy_by_ref = 0;
11330         iph = mtod(m, struct ip *);
11331         if (iph == NULL) {
11332                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11333                 return;
11334         }
11335         switch (iph->ip_v) {
11336 #ifdef INET
11337         case IPVERSION:
11338                 /* IPv4 */
11339                 len = chk->send_size = iph->ip_len;
11340                 break;
11341 #endif
11342 #ifdef INET6
11343         case IPV6_VERSION >> 4:
11344                 /* IPv6 */
11345                 ip6h = mtod(m, struct ip6_hdr *);
11346                 len = chk->send_size = htons(ip6h->ip6_plen);
11347                 break;
11348 #endif
11349         default:
11350                 return;
11351         }
11352         /* Validate that we do not have an ABORT in here. */
11353         offset = iphlen + sizeof(struct sctphdr);
11354         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11355             sizeof(*ch), (uint8_t *) & chunk_buf);
11356         while (ch != NULL) {
11357                 chk_length = ntohs(ch->chunk_length);
11358                 if (chk_length < sizeof(*ch)) {
11359                         /* break to abort land */
11360                         break;
11361                 }
11362                 switch (ch->chunk_type) {
11363                 case SCTP_PACKET_DROPPED:
11364                 case SCTP_ABORT_ASSOCIATION:
11365                 case SCTP_INITIATION_ACK:
11366                         /**
11367                          * We don't respond with an PKT-DROP to an ABORT
11368                          * or PKT-DROP. We also do not respond to an
11369                          * INIT-ACK, because we can't know if the initiation
11370                          * tag is correct or not.
11371                          */
11372                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11373                         return;
11374                 default:
11375                         break;
11376                 }
11377                 offset += SCTP_SIZE32(chk_length);
11378                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11379                     sizeof(*ch), (uint8_t *) & chunk_buf);
11380         }
11381 
11382         if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11383             min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11384                 /*
11385                  * only send 1 mtu worth, trim off the excess on the end.
11386                  */
11387                 fullsz = len - extra;
11388                 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11389                 was_trunc = 1;
11390         }
11391         chk->asoc = &stcb->asoc;
11392         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11393         if (chk->data == NULL) {
11394 jump_out:
11395                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11396                 return;
11397         }
11398         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11399         drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11400         if (drp == NULL) {
11401                 sctp_m_freem(chk->data);
11402                 chk->data = NULL;
11403                 goto jump_out;
11404         }
11405         chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11406             sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11407         chk->book_size_scale = 0;
11408         if (was_trunc) {
11409                 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11410                 drp->trunc_len = htons(fullsz);
11411                 /*
11412                  * Len is already adjusted to size minus overhead above take
11413                  * out the pkt_drop chunk itself from it.
11414                  */
11415                 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11416                 len = chk->send_size;
11417         } else {
11418                 /* no truncation needed */
11419                 drp->ch.chunk_flags = 0;
11420                 drp->trunc_len = htons(0);
11421         }
11422         if (bad_crc) {
11423                 drp->ch.chunk_flags |= SCTP_BADCRC;
11424         }
11425         chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11426         SCTP_BUF_LEN(chk->data) = chk->send_size;
11427         chk->sent = SCTP_DATAGRAM_UNSENT;
11428         chk->snd_count = 0;
11429         if (net) {
11430                 /* we should hit here */
11431                 chk->whoTo = net;
11432                 atomic_add_int(&chk->whoTo->ref_count, 1);
11433         } else {
11434                 chk->whoTo = NULL;
11435         }
11436         chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11437         chk->rec.chunk_id.can_take_data = 1;
11438         drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11439         drp->ch.chunk_length = htons(chk->send_size);
11440         spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11441         if (spc < 0) {
11442                 spc = 0;
11443         }
11444         drp->bottle_bw = htonl(spc);
11445         if (asoc->my_rwnd) {
11446                 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11447                     asoc->size_on_all_streams +
11448                     asoc->my_rwnd_control_len +
11449                     stcb->sctp_socket->so_rcv.sb_cc);
11450         } else {
11451                 /*-
11452                  * If my rwnd is 0, possibly from mbuf depletion as well as
11453                  * space used, tell the peer there is NO space aka onq == bw
11454                  */
11455                 drp->current_onq = htonl(spc);
11456         }
11457         drp->reserved = 0;
11458         datap = drp->data;
11459         m_copydata(m, iphlen, len, (caddr_t)datap);
11460         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11461         asoc->ctrl_queue_cnt++;
11462 }
11463 
11464 void
11465 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11466 {
11467         struct sctp_association *asoc;
11468         struct sctp_cwr_chunk *cwr;
11469         struct sctp_tmit_chunk *chk;
11470 
11471         asoc = &stcb->asoc;
11472         SCTP_TCB_LOCK_ASSERT(stcb);
11473         if (net == NULL) {
11474                 return;
11475         }
11476         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11477                 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11478                         /*
11479                          * found a previous CWR queued to same destination
11480                          * update it if needed
11481                          */
11482                         uint32_t ctsn;
11483 
11484                         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11485                         ctsn = ntohl(cwr->tsn);
11486                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11487                                 cwr->tsn = htonl(high_tsn);
11488                         }
11489                         if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11490                                 /* Make sure override is carried */
11491                                 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11492                         }
11493                         return;
11494                 }
11495         }
11496         sctp_alloc_a_chunk(stcb, chk);
11497         if (chk == NULL) {
11498                 return;
11499         }
11500         chk->copy_by_ref = 0;
11501         chk->rec.chunk_id.id = SCTP_ECN_CWR;
11502         chk->rec.chunk_id.can_take_data = 1;
11503         chk->asoc = &stcb->asoc;
11504         chk->send_size = sizeof(struct sctp_cwr_chunk);
11505         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11506         if (chk->data == NULL) {
11507                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11508                 return;
11509         }
11510         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11511         SCTP_BUF_LEN(chk->data) = chk->send_size;
11512         chk->sent = SCTP_DATAGRAM_UNSENT;
11513         chk->snd_count = 0;
11514         chk->whoTo = net;
11515         atomic_add_int(&chk->whoTo->ref_count, 1);
11516         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11517         cwr->ch.chunk_type = SCTP_ECN_CWR;
11518         cwr->ch.chunk_flags = override;
11519         cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11520         cwr->tsn = htonl(high_tsn);
11521         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11522         asoc->ctrl_queue_cnt++;
11523 }
11524 
11525 void
11526 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11527     int number_entries, uint16_t * list,
11528     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11529 {
11530         int len, old_len, i;
11531         struct sctp_stream_reset_out_request *req_out;
11532         struct sctp_chunkhdr *ch;
11533 
11534         ch = mtod(chk->data, struct sctp_chunkhdr *);
11535 
11536 
11537         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11538 
11539         /* get to new offset for the param. */
11540         req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11541         /* now how long will this param be? */
11542         len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11543         req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11544         req_out->ph.param_length = htons(len);
11545         req_out->request_seq = htonl(seq);
11546         req_out->response_seq = htonl(resp_seq);
11547         req_out->send_reset_at_tsn = htonl(last_sent);
11548         if (number_entries) {
11549                 for (i = 0; i < number_entries; i++) {
11550                         req_out->list_of_streams[i] = htons(list[i]);
11551                 }
11552         }
11553         if (SCTP_SIZE32(len) > len) {
11554                 /*-
11555                  * Need to worry about the pad we may end up adding to the
11556                  * end. This is easy since the struct is either aligned to 4
11557                  * bytes or 2 bytes off.
11558                  */
11559                 req_out->list_of_streams[number_entries] = 0;
11560         }
11561         /* now fix the chunk length */
11562         ch->chunk_length = htons(len + old_len);
11563         chk->book_size = len + old_len;
11564         chk->book_size_scale = 0;
11565         chk->send_size = SCTP_SIZE32(chk->book_size);
11566         SCTP_BUF_LEN(chk->data) = chk->send_size;
11567         return;
11568 }
11569 
11570 
11571 void
11572 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11573     int number_entries, uint16_t * list,
11574     uint32_t seq)
11575 {
11576         int len, old_len, i;
11577         struct sctp_stream_reset_in_request *req_in;
11578         struct sctp_chunkhdr *ch;
11579 
11580         ch = mtod(chk->data, struct sctp_chunkhdr *);
11581 
11582 
11583         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11584 
11585         /* get to new offset for the param. */
11586         req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11587         /* now how long will this param be? */
11588         len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11589         req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11590         req_in->ph.param_length = htons(len);
11591         req_in->request_seq = htonl(seq);
11592         if (number_entries) {
11593                 for (i = 0; i < number_entries; i++) {
11594                         req_in->list_of_streams[i] = htons(list[i]);
11595                 }
11596         }
11597         if (SCTP_SIZE32(len) > len) {
11598                 /*-
11599                  * Need to worry about the pad we may end up adding to the
11600                  * end. This is easy since the struct is either aligned to 4
11601                  * bytes or 2 bytes off.
11602                  */
11603                 req_in->list_of_streams[number_entries] = 0;
11604         }
11605         /* now fix the chunk length */
11606         ch->chunk_length = htons(len + old_len);
11607         chk->book_size = len + old_len;
11608         chk->book_size_scale = 0;
11609         chk->send_size = SCTP_SIZE32(chk->book_size);
11610         SCTP_BUF_LEN(chk->data) = chk->send_size;
11611         return;
11612 }
11613 
11614 
11615 void
11616 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11617     uint32_t seq)
11618 {
11619         int len, old_len;
11620         struct sctp_stream_reset_tsn_request *req_tsn;
11621         struct sctp_chunkhdr *ch;
11622 
11623         ch = mtod(chk->data, struct sctp_chunkhdr *);
11624 
11625 
11626         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11627 
11628         /* get to new offset for the param. */
11629         req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11630         /* now how long will this param be? */
11631         len = sizeof(struct sctp_stream_reset_tsn_request);
11632         req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11633         req_tsn->ph.param_length = htons(len);
11634         req_tsn->request_seq = htonl(seq);
11635 
11636         /* now fix the chunk length */
11637         ch->chunk_length = htons(len + old_len);
11638         chk->send_size = len + old_len;
11639         chk->book_size = SCTP_SIZE32(chk->send_size);
11640         chk->book_size_scale = 0;
11641         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11642         return;
11643 }
11644 
11645 void
11646 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11647     uint32_t resp_seq, uint32_t result)
11648 {
11649         int len, old_len;
11650         struct sctp_stream_reset_response *resp;
11651         struct sctp_chunkhdr *ch;
11652 
11653         ch = mtod(chk->data, struct sctp_chunkhdr *);
11654 
11655 
11656         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11657 
11658         /* get to new offset for the param. */
11659         resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11660         /* now how long will this param be? */
11661         len = sizeof(struct sctp_stream_reset_response);
11662         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11663         resp->ph.param_length = htons(len);
11664         resp->response_seq = htonl(resp_seq);
11665         resp->result = ntohl(result);
11666 
11667         /* now fix the chunk length */
11668         ch->chunk_length = htons(len + old_len);
11669         chk->book_size = len + old_len;
11670         chk->book_size_scale = 0;
11671         chk->send_size = SCTP_SIZE32(chk->book_size);
11672         SCTP_BUF_LEN(chk->data) = chk->send_size;
11673         return;
11674 
11675 }
11676 
11677 
11678 void
11679 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11680     uint32_t resp_seq, uint32_t result,
11681     uint32_t send_una, uint32_t recv_next)
11682 {
11683         int len, old_len;
11684         struct sctp_stream_reset_response_tsn *resp;
11685         struct sctp_chunkhdr *ch;
11686 
11687         ch = mtod(chk->data, struct sctp_chunkhdr *);
11688 
11689 
11690         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11691 
11692         /* get to new offset for the param. */
11693         resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11694         /* now how long will this param be? */
11695         len = sizeof(struct sctp_stream_reset_response_tsn);
11696         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11697         resp->ph.param_length = htons(len);
11698         resp->response_seq = htonl(resp_seq);
11699         resp->result = htonl(result);
11700         resp->senders_next_tsn = htonl(send_una);
11701         resp->receivers_next_tsn = htonl(recv_next);
11702 
11703         /* now fix the chunk length */
11704         ch->chunk_length = htons(len + old_len);
11705         chk->book_size = len + old_len;
11706         chk->send_size = SCTP_SIZE32(chk->book_size);
11707         chk->book_size_scale = 0;
11708         SCTP_BUF_LEN(chk->data) = chk->send_size;
11709         return;
11710 }
11711 
11712 static void
11713 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11714     uint32_t seq,
11715     uint16_t adding)
11716 {
11717         int len, old_len;
11718         struct sctp_chunkhdr *ch;
11719         struct sctp_stream_reset_add_strm *addstr;
11720 
11721         ch = mtod(chk->data, struct sctp_chunkhdr *);
11722         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11723 
11724         /* get to new offset for the param. */
11725         addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11726         /* now how long will this param be? */
11727         len = sizeof(struct sctp_stream_reset_add_strm);
11728 
11729         /* Fill it out. */
11730         addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11731         addstr->ph.param_length = htons(len);
11732         addstr->request_seq = htonl(seq);
11733         addstr->number_of_streams = htons(adding);
11734         addstr->reserved = 0;
11735 
11736         /* now fix the chunk length */
11737         ch->chunk_length = htons(len + old_len);
11738         chk->send_size = len + old_len;
11739         chk->book_size = SCTP_SIZE32(chk->send_size);
11740         chk->book_size_scale = 0;
11741         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11742         return;
11743 }
11744 
11745 int
11746 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11747     int number_entries, uint16_t * list,
11748     uint8_t send_out_req,
11749     uint32_t resp_seq,
11750     uint8_t send_in_req,
11751     uint8_t send_tsn_req,
11752     uint8_t add_stream,
11753     uint16_t adding
11754 )
11755 {
11756 
11757         struct sctp_association *asoc;
11758         struct sctp_tmit_chunk *chk;
11759         struct sctp_chunkhdr *ch;
11760         uint32_t seq;
11761 
11762         asoc = &stcb->asoc;
11763         if (asoc->stream_reset_outstanding) {
11764                 /*-
11765                  * Already one pending, must get ACK back to clear the flag.
11766                  */
11767                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11768                 return (EBUSY);
11769         }
11770         if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11771             (add_stream == 0)) {
11772                 /* nothing to do */
11773                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11774                 return (EINVAL);
11775         }
11776         if (send_tsn_req && (send_out_req || send_in_req)) {
11777                 /* error, can't do that */
11778                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11779                 return (EINVAL);
11780         }
11781         sctp_alloc_a_chunk(stcb, chk);
11782         if (chk == NULL) {
11783                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11784                 return (ENOMEM);
11785         }
11786         chk->copy_by_ref = 0;
11787         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11788         chk->rec.chunk_id.can_take_data = 0;
11789         chk->asoc = &stcb->asoc;
11790         chk->book_size = sizeof(struct sctp_chunkhdr);
11791         chk->send_size = SCTP_SIZE32(chk->book_size);
11792         chk->book_size_scale = 0;
11793 
11794         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11795         if (chk->data == NULL) {
11796                 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11797                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11798                 return (ENOMEM);
11799         }
11800         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11801 
11802         /* setup chunk parameters */
11803         chk->sent = SCTP_DATAGRAM_UNSENT;
11804         chk->snd_count = 0;
11805         if (stcb->asoc.alternate) {
11806                 chk->whoTo = stcb->asoc.alternate;
11807         } else {
11808                 chk->whoTo = stcb->asoc.primary_destination;
11809         }
11810         atomic_add_int(&chk->whoTo->ref_count, 1);
11811         ch = mtod(chk->data, struct sctp_chunkhdr *);
11812         ch->chunk_type = SCTP_STREAM_RESET;
11813         ch->chunk_flags = 0;
11814         ch->chunk_length = htons(chk->book_size);
11815         SCTP_BUF_LEN(chk->data) = chk->send_size;
11816 
11817         seq = stcb->asoc.str_reset_seq_out;
11818         if (send_out_req) {
11819                 sctp_add_stream_reset_out(chk, number_entries, list,
11820                     seq, resp_seq, (stcb->asoc.sending_seq - 1));
11821                 asoc->stream_reset_out_is_outstanding = 1;
11822                 seq++;
11823                 asoc->stream_reset_outstanding++;
11824         }
11825         if (add_stream) {
11826                 sctp_add_a_stream(chk, seq, adding);
11827                 seq++;
11828                 asoc->stream_reset_outstanding++;
11829         }
11830         if (send_in_req) {
11831                 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11832                 asoc->stream_reset_outstanding++;
11833         }
11834         if (send_tsn_req) {
11835                 sctp_add_stream_reset_tsn(chk, seq);
11836                 asoc->stream_reset_outstanding++;
11837         }
11838         asoc->str_reset = chk;
11839 
11840         /* insert the chunk for sending */
11841         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11842             chk,
11843             sctp_next);
11844         asoc->ctrl_queue_cnt++;
11845         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11846         return (0);
11847 }
11848 
11849 void
11850 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11851     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11852 {
11853         /*-
11854          * Formulate the abort message, and send it back down.
11855          */
11856         struct mbuf *o_pak;
11857         struct mbuf *mout;
11858         struct sctp_abort_msg *abm;
11859         struct ip *iph;
11860         struct udphdr *udp;
11861         int iphlen_out, len;
11862 
11863 #ifdef INET
11864         struct ip *iph_out;
11865 
11866 #endif
11867 #ifdef INET6
11868         struct ip6_hdr *ip6, *ip6_out;
11869 
11870 #endif
11871 
11872         /* don't respond to ABORT with ABORT */
11873         if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11874                 if (err_cause)
11875                         sctp_m_freem(err_cause);
11876                 return;
11877         }
11878         iph = mtod(m, struct ip *);
11879         switch (iph->ip_v) {
11880 #ifdef INET
11881         case IPVERSION:
11882                 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11883                 break;
11884 #endif
11885 #ifdef INET6
11886         case IPV6_VERSION >> 4:
11887                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11888                 break;
11889 #endif
11890         default:
11891                 if (err_cause) {
11892                         sctp_m_freem(err_cause);
11893                 }
11894                 return;
11895         }
11896         if (port) {
11897                 len += sizeof(struct udphdr);
11898         }
11899         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11900         if (mout == NULL) {
11901                 if (err_cause) {
11902                         sctp_m_freem(err_cause);
11903                 }
11904                 return;
11905         }
11906         SCTP_BUF_RESV_UF(mout, max_linkhdr);
11907         SCTP_BUF_LEN(mout) = len;
11908         SCTP_BUF_NEXT(mout) = err_cause;
11909         if (m->m_flags & M_FLOWID) {
11910                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
11911                 mout->m_flags |= M_FLOWID;
11912         }
11913 #ifdef INET
11914         iph_out = NULL;
11915 #endif
11916 #ifdef INET6
11917         ip6_out = NULL;
11918 #endif
11919         switch (iph->ip_v) {
11920 #ifdef INET
11921         case IPVERSION:
11922                 iph_out = mtod(mout, struct ip *);
11923 
11924                 /* Fill in the IP header for the ABORT */
11925                 iph_out->ip_v = IPVERSION;
11926                 iph_out->ip_hl = (sizeof(struct ip) / 4);
11927                 iph_out->ip_tos = (u_char)0;
11928                 iph_out->ip_id = 0;
11929                 iph_out->ip_off = 0;
11930                 iph_out->ip_ttl = MAXTTL;
11931                 if (port) {
11932                         iph_out->ip_p = IPPROTO_UDP;
11933                 } else {
11934                         iph_out->ip_p = IPPROTO_SCTP;
11935                 }
11936                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11937                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11938                 /* let IP layer calculate this */
11939                 iph_out->ip_sum = 0;
11940 
11941                 iphlen_out = sizeof(*iph_out);
11942                 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11943                 break;
11944 #endif
11945 #ifdef INET6
11946         case IPV6_VERSION >> 4:
11947                 ip6 = (struct ip6_hdr *)iph;
11948                 ip6_out = mtod(mout, struct ip6_hdr *);
11949 
11950                 /* Fill in the IP6 header for the ABORT */
11951                 ip6_out->ip6_flow = ip6->ip6_flow;
11952                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11953                 if (port) {
11954                         ip6_out->ip6_nxt = IPPROTO_UDP;
11955                 } else {
11956                         ip6_out->ip6_nxt = IPPROTO_SCTP;
11957                 }
11958                 ip6_out->ip6_src = ip6->ip6_dst;
11959                 ip6_out->ip6_dst = ip6->ip6_src;
11960 
11961                 iphlen_out = sizeof(*ip6_out);
11962                 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11963                 break;
11964 #endif                          /* INET6 */
11965         default:
11966                 /* Currently not supported */
11967                 sctp_m_freem(mout);
11968                 return;
11969         }
11970 
11971         udp = (struct udphdr *)abm;
11972         if (port) {
11973                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11974                         sctp_m_freem(mout);
11975                         return;
11976                 }
11977                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11978                 udp->uh_dport = port;
11979                 /* set udp->uh_ulen later */
11980                 udp->uh_sum = 0;
11981                 iphlen_out += sizeof(struct udphdr);
11982                 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11983         }
11984         abm->sh.src_port = sh->dest_port;
11985         abm->sh.dest_port = sh->src_port;
11986         abm->sh.checksum = 0;
11987         if (vtag == 0) {
11988                 abm->sh.v_tag = sh->v_tag;
11989                 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11990         } else {
11991                 abm->sh.v_tag = htonl(vtag);
11992                 abm->msg.ch.chunk_flags = 0;
11993         }
11994         abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11995 
11996         if (err_cause) {
11997                 struct mbuf *m_tmp = err_cause;
11998                 int err_len = 0;
11999 
12000                 /* get length of the err_cause chain */
12001                 while (m_tmp != NULL) {
12002                         err_len += SCTP_BUF_LEN(m_tmp);
12003                         m_tmp = SCTP_BUF_NEXT(m_tmp);
12004                 }
12005                 len = SCTP_BUF_LEN(mout) + err_len;
12006                 if (err_len % 4) {
12007                         /* need pad at end of chunk */
12008                         uint32_t cpthis = 0;
12009                         int padlen;
12010 
12011                         padlen = 4 - (len % 4);
12012                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12013                         len += padlen;
12014                 }
12015                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
12016         } else {
12017                 len = SCTP_BUF_LEN(mout);
12018                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
12019         }
12020 
12021         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12022                 /* no mbuf's */
12023                 sctp_m_freem(mout);
12024                 return;
12025         }
12026 #ifdef INET
12027         if (iph_out != NULL) {
12028                 sctp_route_t ro;
12029                 int ret;
12030 
12031                 /* zap the stack pointer to the route */
12032                 bzero(&ro, sizeof ro);
12033                 if (port) {
12034                         udp->uh_ulen = htons(len - sizeof(struct ip));
12035                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12036                 }
12037                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
12038                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
12039                 /* set IPv4 length */
12040                 iph_out->ip_len = len;
12041                 /* out it goes */
12042 #ifdef  SCTP_PACKET_LOGGING
12043                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12044                         sctp_packet_log(mout, len);
12045 #endif
12046                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12047                 if (port) {
12048 #if defined(SCTP_WITH_NO_CSUM)
12049                         SCTP_STAT_INCR(sctps_sendnocrc);
12050 #else
12051                         abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
12052                         SCTP_STAT_INCR(sctps_sendswcrc);
12053 #endif
12054                         SCTP_ENABLE_UDP_CSUM(o_pak);
12055                 } else {
12056 #if defined(SCTP_WITH_NO_CSUM)
12057                         SCTP_STAT_INCR(sctps_sendnocrc);
12058 #else
12059                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12060                         mout->m_pkthdr.csum_data = 0;
12061                         SCTP_STAT_INCR(sctps_sendhwcrc);
12062 #endif
12063                 }
12064                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12065 
12066                 /* Free the route if we got one back */
12067                 if (ro.ro_rt)
12068                         RTFREE(ro.ro_rt);
12069         }
12070 #endif
12071 #ifdef INET6
12072         if (ip6_out != NULL) {
12073                 struct route_in6 ro;
12074                 int ret;
12075                 struct ifnet *ifp = NULL;
12076 
12077                 /* zap the stack pointer to the route */
12078                 bzero(&ro, sizeof(ro));
12079                 if (port) {
12080                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12081                 }
12082                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
12083                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
12084                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12085 #ifdef  SCTP_PACKET_LOGGING
12086                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12087                         sctp_packet_log(mout, len);
12088 #endif
12089                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12090                 if (port) {
12091 #if defined(SCTP_WITH_NO_CSUM)
12092                         SCTP_STAT_INCR(sctps_sendnocrc);
12093 #else
12094                         abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12095                         SCTP_STAT_INCR(sctps_sendswcrc);
12096 #endif
12097                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12098                                 udp->uh_sum = 0xffff;
12099                         }
12100                 } else {
12101 #if defined(SCTP_WITH_NO_CSUM)
12102                         SCTP_STAT_INCR(sctps_sendnocrc);
12103 #else
12104                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12105                         mout->m_pkthdr.csum_data = 0;
12106                         SCTP_STAT_INCR(sctps_sendhwcrc);
12107 #endif
12108                 }
12109                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12110 
12111                 /* Free the route if we got one back */
12112                 if (ro.ro_rt)
12113                         RTFREE(ro.ro_rt);
12114         }
12115 #endif
12116         SCTP_STAT_INCR(sctps_sendpackets);
12117         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12118         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12119 }
12120 
12121 void
12122 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
12123     uint32_t vrf_id, uint16_t port)
12124 {
12125         struct mbuf *o_pak;
12126         struct sctphdr *sh, *sh_out;
12127         struct sctp_chunkhdr *ch;
12128         struct ip *iph;
12129         struct udphdr *udp = NULL;
12130         struct mbuf *mout;
12131         int iphlen_out, len;
12132 
12133 #ifdef INET
12134         struct ip *iph_out;
12135 
12136 #endif
12137 #ifdef INET6
12138         struct ip6_hdr *ip6, *ip6_out;
12139 
12140 #endif
12141 
12142         iph = mtod(m, struct ip *);
12143         sh = (struct sctphdr *)((caddr_t)iph + iphlen);
12144         switch (iph->ip_v) {
12145 #ifdef INET
12146         case IPVERSION:
12147                 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12148                 break;
12149 #endif
12150 #ifdef INET6
12151         case IPV6_VERSION >> 4:
12152                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12153                 break;
12154 #endif
12155         default:
12156                 if (scm) {
12157                         sctp_m_freem(scm);
12158                 }
12159                 return;
12160         }
12161         if (port) {
12162                 len += sizeof(struct udphdr);
12163         }
12164         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12165         if (mout == NULL) {
12166                 if (scm) {
12167                         sctp_m_freem(scm);
12168                 }
12169                 return;
12170         }
12171         SCTP_BUF_RESV_UF(mout, max_linkhdr);
12172         SCTP_BUF_LEN(mout) = len;
12173         SCTP_BUF_NEXT(mout) = scm;
12174         if (m->m_flags & M_FLOWID) {
12175                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
12176                 mout->m_flags |= M_FLOWID;
12177         }
12178 #ifdef INET
12179         iph_out = NULL;
12180 #endif
12181 #ifdef INET6
12182         ip6_out = NULL;
12183 #endif
12184         switch (iph->ip_v) {
12185 #ifdef INET
12186         case IPVERSION:
12187                 iph_out = mtod(mout, struct ip *);
12188 
12189                 /* Fill in the IP header for the ABORT */
12190                 iph_out->ip_v = IPVERSION;
12191                 iph_out->ip_hl = (sizeof(struct ip) / 4);
12192                 iph_out->ip_tos = (u_char)0;
12193                 iph_out->ip_id = 0;
12194                 iph_out->ip_off = 0;
12195                 iph_out->ip_ttl = MAXTTL;
12196                 if (port) {
12197                         iph_out->ip_p = IPPROTO_UDP;
12198                 } else {
12199                         iph_out->ip_p = IPPROTO_SCTP;
12200                 }
12201                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12202                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12203                 /* let IP layer calculate this */
12204                 iph_out->ip_sum = 0;
12205 
12206                 iphlen_out = sizeof(struct ip);
12207                 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
12208                 break;
12209 #endif
12210 #ifdef INET6
12211         case IPV6_VERSION >> 4:
12212                 ip6 = (struct ip6_hdr *)iph;
12213                 ip6_out = mtod(mout, struct ip6_hdr *);
12214 
12215                 /* Fill in the IP6 header for the ABORT */
12216                 ip6_out->ip6_flow = ip6->ip6_flow;
12217                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12218                 if (port) {
12219                         ip6_out->ip6_nxt = IPPROTO_UDP;
12220                 } else {
12221                         ip6_out->ip6_nxt = IPPROTO_SCTP;
12222                 }
12223                 ip6_out->ip6_src = ip6->ip6_dst;
12224                 ip6_out->ip6_dst = ip6->ip6_src;
12225 
12226                 iphlen_out = sizeof(struct ip6_hdr);
12227                 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
12228                 break;
12229 #endif                          /* INET6 */
12230         default:
12231                 /* Currently not supported */
12232                 sctp_m_freem(mout);
12233                 return;
12234         }
12235 
12236         udp = (struct udphdr *)sh_out;
12237         if (port) {
12238                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
12239                         sctp_m_freem(mout);
12240                         return;
12241                 }
12242                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12243                 udp->uh_dport = port;
12244                 /* set udp->uh_ulen later */
12245                 udp->uh_sum = 0;
12246                 iphlen_out += sizeof(struct udphdr);
12247                 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12248         }
12249         sh_out->src_port = sh->dest_port;
12250         sh_out->dest_port = sh->src_port;
12251         sh_out->v_tag = vtag;
12252         sh_out->checksum = 0;
12253 
12254         ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12255         ch->chunk_type = SCTP_OPERATION_ERROR;
12256         ch->chunk_flags = 0;
12257 
12258         if (scm) {
12259                 struct mbuf *m_tmp = scm;
12260                 int cause_len = 0;
12261 
12262                 /* get length of the err_cause chain */
12263                 while (m_tmp != NULL) {
12264                         cause_len += SCTP_BUF_LEN(m_tmp);
12265                         m_tmp = SCTP_BUF_NEXT(m_tmp);
12266                 }
12267                 len = SCTP_BUF_LEN(mout) + cause_len;
12268                 if (cause_len % 4) {
12269                         /* need pad at end of chunk */
12270                         uint32_t cpthis = 0;
12271                         int padlen;
12272 
12273                         padlen = 4 - (len % 4);
12274                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12275                         len += padlen;
12276                 }
12277                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12278         } else {
12279                 len = SCTP_BUF_LEN(mout);
12280                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12281         }
12282 
12283         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12284                 /* no mbuf's */
12285                 sctp_m_freem(mout);
12286                 return;
12287         }
12288 #ifdef INET
12289         if (iph_out != NULL) {
12290                 sctp_route_t ro;
12291                 int ret;
12292 
12293                 /* zap the stack pointer to the route */
12294                 bzero(&ro, sizeof ro);
12295                 if (port) {
12296                         udp->uh_ulen = htons(len - sizeof(struct ip));
12297                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12298                 }
12299                 /* set IPv4 length */
12300                 iph_out->ip_len = len;
12301                 /* out it goes */
12302 #ifdef  SCTP_PACKET_LOGGING
12303                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12304                         sctp_packet_log(mout, len);
12305 #endif
12306                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12307                 if (port) {
12308 #if defined(SCTP_WITH_NO_CSUM)
12309                         SCTP_STAT_INCR(sctps_sendnocrc);
12310 #else
12311                         sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12312                         SCTP_STAT_INCR(sctps_sendswcrc);
12313 #endif
12314                         SCTP_ENABLE_UDP_CSUM(o_pak);
12315                 } else {
12316 #if defined(SCTP_WITH_NO_CSUM)
12317                         SCTP_STAT_INCR(sctps_sendnocrc);
12318 #else
12319                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12320                         mout->m_pkthdr.csum_data = 0;
12321                         SCTP_STAT_INCR(sctps_sendhwcrc);
12322 #endif
12323                 }
12324                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12325 
12326                 /* Free the route if we got one back */
12327                 if (ro.ro_rt)
12328                         RTFREE(ro.ro_rt);
12329         }
12330 #endif
12331 #ifdef INET6
12332         if (ip6_out != NULL) {
12333                 struct route_in6 ro;
12334                 int ret;
12335                 struct ifnet *ifp = NULL;
12336 
12337                 /* zap the stack pointer to the route */
12338                 bzero(&ro, sizeof(ro));
12339                 if (port) {
12340                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12341                 }
12342                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12343 #ifdef  SCTP_PACKET_LOGGING
12344                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12345                         sctp_packet_log(mout, len);
12346 #endif
12347                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12348                 if (port) {
12349 #if defined(SCTP_WITH_NO_CSUM)
12350                         SCTP_STAT_INCR(sctps_sendnocrc);
12351 #else
12352                         sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12353                         SCTP_STAT_INCR(sctps_sendswcrc);
12354 #endif
12355                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12356                                 udp->uh_sum = 0xffff;
12357                         }
12358                 } else {
12359 #if defined(SCTP_WITH_NO_CSUM)
12360                         SCTP_STAT_INCR(sctps_sendnocrc);
12361 #else
12362                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12363                         mout->m_pkthdr.csum_data = 0;
12364                         SCTP_STAT_INCR(sctps_sendhwcrc);
12365 #endif
12366                 }
12367                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12368 
12369                 /* Free the route if we got one back */
12370                 if (ro.ro_rt)
12371                         RTFREE(ro.ro_rt);
12372         }
12373 #endif
12374         SCTP_STAT_INCR(sctps_sendpackets);
12375         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12376         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12377 }
12378 
12379 static struct mbuf *
12380 sctp_copy_resume(struct uio *uio,
12381     int max_send_len,
12382     int user_marks_eor,
12383     int *error,
12384     uint32_t * sndout,
12385     struct mbuf **new_tail)
12386 {
12387         struct mbuf *m;
12388 
12389         m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12390             (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12391         if (m == NULL) {
12392                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12393                 *error = ENOMEM;
12394         } else {
12395                 *sndout = m_length(m, NULL);
12396                 *new_tail = m_last(m);
12397         }
12398         return (m);
12399 }
12400 
12401 static int
12402 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12403     struct uio *uio,
12404     int resv_upfront)
12405 {
12406         int left;
12407 
12408         left = sp->length;
12409         sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12410             resv_upfront, 0);
12411         if (sp->data == NULL) {
12412                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12413                 return (ENOMEM);
12414         }
12415         sp->tail_mbuf = m_last(sp->data);
12416         return (0);
12417 }
12418 
12419 
12420 
12421 static struct sctp_stream_queue_pending *
12422 sctp_copy_it_in(struct sctp_tcb *stcb,
12423     struct sctp_association *asoc,
12424     struct sctp_sndrcvinfo *srcv,
12425     struct uio *uio,
12426     struct sctp_nets *net,
12427     int max_send_len,
12428     int user_marks_eor,
12429     int *error)
12430 {
12431         /*-
12432          * This routine must be very careful in its work. Protocol
12433          * processing is up and running so care must be taken to spl...()
12434          * when you need to do something that may effect the stcb/asoc. The
12435          * sb is locked however. When data is copied the protocol processing
12436          * should be enabled since this is a slower operation...
12437          */
12438         struct sctp_stream_queue_pending *sp = NULL;
12439         int resv_in_first;
12440 
12441         *error = 0;
12442         /* Now can we send this? */
12443         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12444             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12445             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12446             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12447                 /* got data while shutting down */
12448                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12449                 *error = ECONNRESET;
12450                 goto out_now;
12451         }
12452         sctp_alloc_a_strmoq(stcb, sp);
12453         if (sp == NULL) {
12454                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12455                 *error = ENOMEM;
12456                 goto out_now;
12457         }
12458         sp->act_flags = 0;
12459         sp->sender_all_done = 0;
12460         sp->sinfo_flags = srcv->sinfo_flags;
12461         sp->timetolive = srcv->sinfo_timetolive;
12462         sp->ppid = srcv->sinfo_ppid;
12463         sp->context = srcv->sinfo_context;
12464         sp->strseq = 0;
12465         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12466 
12467         sp->stream = srcv->sinfo_stream;
12468         sp->length = min(uio->uio_resid, max_send_len);
12469         if ((sp->length == (uint32_t) uio->uio_resid) &&
12470             ((user_marks_eor == 0) ||
12471             (srcv->sinfo_flags & SCTP_EOF) ||
12472             (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12473                 sp->msg_is_complete = 1;
12474         } else {
12475                 sp->msg_is_complete = 0;
12476         }
12477         sp->sender_all_done = 0;
12478         sp->some_taken = 0;
12479         sp->put_last_out = 0;
12480         resv_in_first = sizeof(struct sctp_data_chunk);
12481         sp->data = sp->tail_mbuf = NULL;
12482         if (sp->length == 0) {
12483                 *error = 0;
12484                 goto skip_copy;
12485         }
12486         if (srcv->sinfo_keynumber_valid) {
12487                 sp->auth_keyid = srcv->sinfo_keynumber;
12488         } else {
12489                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12490         }
12491         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12492                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
12493                 sp->holds_key_ref = 1;
12494         }
12495         *error = sctp_copy_one(sp, uio, resv_in_first);
12496 skip_copy:
12497         if (*error) {
12498                 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12499                 sp = NULL;
12500         } else {
12501                 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12502                         sp->net = net;
12503                         atomic_add_int(&sp->net->ref_count, 1);
12504                 } else {
12505                         sp->net = NULL;
12506                 }
12507                 sctp_set_prsctp_policy(sp);
12508         }
12509 out_now:
12510         return (sp);
12511 }
12512 
12513 
12514 int
12515 sctp_sosend(struct socket *so,
12516     struct sockaddr *addr,
12517     struct uio *uio,
12518     struct mbuf *top,
12519     struct mbuf *control,
12520     int flags,
12521     struct thread *p
12522 )
12523 {
12524         int error, use_sndinfo = 0;
12525         struct sctp_sndrcvinfo sndrcvninfo;
12526         struct sockaddr *addr_to_use;
12527 
12528 #if defined(INET) && defined(INET6)
12529         struct sockaddr_in sin;
12530 
12531 #endif
12532 
12533         if (control) {
12534                 /* process cmsg snd/rcv info (maybe a assoc-id) */
12535                 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control,
12536                     sizeof(sndrcvninfo))) {
12537                         /* got one */
12538                         use_sndinfo = 1;
12539                 }
12540         }
12541         addr_to_use = addr;
12542 #if defined(INET) && defined(INET6)
12543         if ((addr) && (addr->sa_family == AF_INET6)) {
12544                 struct sockaddr_in6 *sin6;
12545 
12546                 sin6 = (struct sockaddr_in6 *)addr;
12547                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12548                         in6_sin6_2_sin(&sin, sin6);
12549                         addr_to_use = (struct sockaddr *)&sin;
12550                 }
12551         }
12552 #endif
12553         error = sctp_lower_sosend(so, addr_to_use, uio, top,
12554             control,
12555             flags,
12556             use_sndinfo ? &sndrcvninfo : NULL
12557             ,p
12558             );
12559         return (error);
12560 }
12561 
12562 
12563 int
12564 sctp_lower_sosend(struct socket *so,
12565     struct sockaddr *addr,
12566     struct uio *uio,
12567     struct mbuf *i_pak,
12568     struct mbuf *control,
12569     int flags,
12570     struct sctp_sndrcvinfo *srcv
12571     ,
12572     struct thread *p
12573 )
12574 {
12575         unsigned int sndlen = 0, max_len;
12576         int error, len;
12577         struct mbuf *top = NULL;
12578         int queue_only = 0, queue_only_for_init = 0;
12579         int free_cnt_applied = 0;
12580         int un_sent;
12581         int now_filled = 0;
12582         unsigned int inqueue_bytes = 0;
12583         struct sctp_block_entry be;
12584         struct sctp_inpcb *inp;
12585         struct sctp_tcb *stcb = NULL;
12586         struct timeval now;
12587         struct sctp_nets *net;
12588         struct sctp_association *asoc;
12589         struct sctp_inpcb *t_inp;
12590         int user_marks_eor;
12591         int create_lock_applied = 0;
12592         int nagle_applies = 0;
12593         int some_on_control = 0;
12594         int got_all_of_the_send = 0;
12595         int hold_tcblock = 0;
12596         int non_blocking = 0;
12597         uint32_t local_add_more, local_soresv = 0;
12598         uint16_t port;
12599         uint16_t sinfo_flags;
12600         sctp_assoc_t sinfo_assoc_id;
12601 
12602         error = 0;
12603         net = NULL;
12604         stcb = NULL;
12605         asoc = NULL;
12606 
12607         t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12608         if (inp == NULL) {
12609                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12610                 error = EINVAL;
12611                 if (i_pak) {
12612                         SCTP_RELEASE_PKT(i_pak);
12613                 }
12614                 return (error);
12615         }
12616         if ((uio == NULL) && (i_pak == NULL)) {
12617                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12618                 return (EINVAL);
12619         }
12620         user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12621         atomic_add_int(&inp->total_sends, 1);
12622         if (uio) {
12623                 if (uio->uio_resid < 0) {
12624                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12625                         return (EINVAL);
12626                 }
12627                 sndlen = uio->uio_resid;
12628         } else {
12629                 top = SCTP_HEADER_TO_CHAIN(i_pak);
12630                 sndlen = SCTP_HEADER_LEN(i_pak);
12631         }
12632         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12633             addr,
12634             sndlen);
12635         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12636             (inp->sctp_socket->so_qlimit)) {
12637                 /* The listener can NOT send */
12638                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12639                 error = ENOTCONN;
12640                 goto out_unlocked;
12641         }
12642         /**
12643          * Pre-screen address, if one is given the sin-len
12644          * must be set correctly!
12645          */
12646         if (addr) {
12647                 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12648 
12649                 switch (raddr->sa.sa_family) {
12650 #if defined(INET)
12651                 case AF_INET:
12652                         if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12653                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12654                                 error = EINVAL;
12655                                 goto out_unlocked;
12656                         }
12657                         port = raddr->sin.sin_port;
12658                         break;
12659 #endif
12660 #if defined(INET6)
12661                 case AF_INET6:
12662                         if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12663                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12664                                 error = EINVAL;
12665                                 goto out_unlocked;
12666                         }
12667                         port = raddr->sin6.sin6_port;
12668                         break;
12669 #endif
12670                 default:
12671                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12672                         error = EAFNOSUPPORT;
12673                         goto out_unlocked;
12674                 }
12675         } else
12676                 port = 0;
12677 
12678         if (srcv) {
12679                 sinfo_flags = srcv->sinfo_flags;
12680                 sinfo_assoc_id = srcv->sinfo_assoc_id;
12681                 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12682                     PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12683                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12684                         error = EINVAL;
12685                         goto out_unlocked;
12686                 }
12687                 if (srcv->sinfo_flags)
12688                         SCTP_STAT_INCR(sctps_sends_with_flags);
12689         } else {
12690                 sinfo_flags = inp->def_send.sinfo_flags;
12691                 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12692         }
12693         if (sinfo_flags & SCTP_SENDALL) {
12694                 /* its a sendall */
12695                 error = sctp_sendall(inp, uio, top, srcv);
12696                 top = NULL;
12697                 goto out_unlocked;
12698         }
12699         if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12700                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12701                 error = EINVAL;
12702                 goto out_unlocked;
12703         }
12704         /* now we must find the assoc */
12705         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12706             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12707                 SCTP_INP_RLOCK(inp);
12708                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12709                 if (stcb) {
12710                         SCTP_TCB_LOCK(stcb);
12711                         hold_tcblock = 1;
12712                 }
12713                 SCTP_INP_RUNLOCK(inp);
12714         } else if (sinfo_assoc_id) {
12715                 stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12716         } else if (addr) {
12717                 /*-
12718                  * Since we did not use findep we must
12719                  * increment it, and if we don't find a tcb
12720                  * decrement it.
12721                  */
12722                 SCTP_INP_WLOCK(inp);
12723                 SCTP_INP_INCR_REF(inp);
12724                 SCTP_INP_WUNLOCK(inp);
12725                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12726                 if (stcb == NULL) {
12727                         SCTP_INP_WLOCK(inp);
12728                         SCTP_INP_DECR_REF(inp);
12729                         SCTP_INP_WUNLOCK(inp);
12730                 } else {
12731                         hold_tcblock = 1;
12732                 }
12733         }
12734         if ((stcb == NULL) && (addr)) {
12735                 /* Possible implicit send? */
12736                 SCTP_ASOC_CREATE_LOCK(inp);
12737                 create_lock_applied = 1;
12738                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12739                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12740                         /* Should I really unlock ? */
12741                         SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12742                         error = EINVAL;
12743                         goto out_unlocked;
12744 
12745                 }
12746                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12747                     (addr->sa_family == AF_INET6)) {
12748                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12749                         error = EINVAL;
12750                         goto out_unlocked;
12751                 }
12752                 SCTP_INP_WLOCK(inp);
12753                 SCTP_INP_INCR_REF(inp);
12754                 SCTP_INP_WUNLOCK(inp);
12755                 /* With the lock applied look again */
12756                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12757                 if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12758                         stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12759                 }
12760                 if (stcb == NULL) {
12761                         SCTP_INP_WLOCK(inp);
12762                         SCTP_INP_DECR_REF(inp);
12763                         SCTP_INP_WUNLOCK(inp);
12764                 } else {
12765                         hold_tcblock = 1;
12766                 }
12767                 if (error) {
12768                         goto out_unlocked;
12769                 }
12770                 if (t_inp != inp) {
12771                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12772                         error = ENOTCONN;
12773                         goto out_unlocked;
12774                 }
12775         }
12776         if (stcb == NULL) {
12777                 if (addr == NULL) {
12778                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12779                         error = ENOENT;
12780                         goto out_unlocked;
12781                 } else {
12782                         /* We must go ahead and start the INIT process */
12783                         uint32_t vrf_id;
12784 
12785                         if ((sinfo_flags & SCTP_ABORT) ||
12786                             ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12787                                 /*-
12788                                  * User asks to abort a non-existant assoc,
12789                                  * or EOF a non-existant assoc with no data
12790                                  */
12791                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12792                                 error = ENOENT;
12793                                 goto out_unlocked;
12794                         }
12795                         /* get an asoc/stcb struct */
12796                         vrf_id = inp->def_vrf_id;
12797 #ifdef INVARIANTS
12798                         if (create_lock_applied == 0) {
12799                                 panic("Error, should hold create lock and I don't?");
12800                         }
12801 #endif
12802                         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12803                             p
12804                             );
12805                         if (stcb == NULL) {
12806                                 /* Error is setup for us in the call */
12807                                 goto out_unlocked;
12808                         }
12809                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
12810                                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
12811                                 /*
12812                                  * Set the connected flag so we can queue
12813                                  * data
12814                                  */
12815                                 soisconnecting(so);
12816                         }
12817                         hold_tcblock = 1;
12818                         if (create_lock_applied) {
12819                                 SCTP_ASOC_CREATE_UNLOCK(inp);
12820                                 create_lock_applied = 0;
12821                         } else {
12822                                 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12823                         }
12824                         /*
12825                          * Turn on queue only flag to prevent data from
12826                          * being sent
12827                          */
12828                         queue_only = 1;
12829                         asoc = &stcb->asoc;
12830                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12831                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12832 
12833                         /* initialize authentication params for the assoc */
12834                         sctp_initialize_auth_params(inp, stcb);
12835 
12836                         if (control) {
12837                                 if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12838                                         sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_7);
12839                                         hold_tcblock = 0;
12840                                         stcb = NULL;
12841                                         goto out_unlocked;
12842                                 }
12843                         }
12844                         /* out with the INIT */
12845                         queue_only_for_init = 1;
12846                         /*-
12847                          * we may want to dig in after this call and adjust the MTU
12848                          * value. It defaulted to 1500 (constant) but the ro
12849                          * structure may now have an update and thus we may need to
12850                          * change it BEFORE we append the message.
12851                          */
12852                 }
12853         } else
12854                 asoc = &stcb->asoc;
12855         if (srcv == NULL)
12856                 srcv = (struct sctp_sndrcvinfo *)&asoc->def_send;
12857         if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12858                 if (addr)
12859                         net = sctp_findnet(stcb, addr);
12860                 else
12861                         net = NULL;
12862                 if ((net == NULL) ||
12863                     ((port != 0) && (port != stcb->rport))) {
12864                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12865                         error = EINVAL;
12866                         goto out_unlocked;
12867                 }
12868         } else {
12869                 if (stcb->asoc.alternate) {
12870                         net = stcb->asoc.alternate;
12871                 } else {
12872                         net = stcb->asoc.primary_destination;
12873                 }
12874         }
12875         atomic_add_int(&stcb->total_sends, 1);
12876         /* Keep the stcb from being freed under our feet */
12877         atomic_add_int(&asoc->refcnt, 1);
12878         free_cnt_applied = 1;
12879 
12880         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12881                 if (sndlen > asoc->smallest_mtu) {
12882                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12883                         error = EMSGSIZE;
12884                         goto out_unlocked;
12885                 }
12886         }
12887         if (SCTP_SO_IS_NBIO(so)
12888             || (flags & MSG_NBIO)
12889             ) {
12890                 non_blocking = 1;
12891         }
12892         /* would we block? */
12893         if (non_blocking) {
12894                 if (hold_tcblock == 0) {
12895                         SCTP_TCB_LOCK(stcb);
12896                         hold_tcblock = 1;
12897                 }
12898                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12899                 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12900                     (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12901                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12902                         if (sndlen > SCTP_SB_LIMIT_SND(so))
12903                                 error = EMSGSIZE;
12904                         else
12905                                 error = EWOULDBLOCK;
12906                         goto out_unlocked;
12907                 }
12908                 stcb->asoc.sb_send_resv += sndlen;
12909                 SCTP_TCB_UNLOCK(stcb);
12910                 hold_tcblock = 0;
12911         } else {
12912                 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12913         }
12914         local_soresv = sndlen;
12915         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12916                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12917                 error = ECONNRESET;
12918                 goto out_unlocked;
12919         }
12920         if (create_lock_applied) {
12921                 SCTP_ASOC_CREATE_UNLOCK(inp);
12922                 create_lock_applied = 0;
12923         }
12924         if (asoc->stream_reset_outstanding) {
12925                 /*
12926                  * Can't queue any data while stream reset is underway.
12927                  */
12928                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12929                 error = EAGAIN;
12930                 goto out_unlocked;
12931         }
12932         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12933             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12934                 queue_only = 1;
12935         }
12936         /* we are now done with all control */
12937         if (control) {
12938                 sctp_m_freem(control);
12939                 control = NULL;
12940         }
12941         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12942             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12943             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12944             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12945                 if (srcv->sinfo_flags & SCTP_ABORT) {
12946                         ;
12947                 } else {
12948                         SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12949                         error = ECONNRESET;
12950                         goto out_unlocked;
12951                 }
12952         }
12953         /* Ok, we will attempt a msgsnd :> */
12954         if (p) {
12955                 p->td_ru.ru_msgsnd++;
12956         }
12957         /* Are we aborting? */
12958         if (srcv->sinfo_flags & SCTP_ABORT) {
12959                 struct mbuf *mm;
12960                 int tot_demand, tot_out = 0, max_out;
12961 
12962                 SCTP_STAT_INCR(sctps_sends_with_abort);
12963                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12964                     (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12965                         /* It has to be up before we abort */
12966                         /* how big is the user initiated abort? */
12967                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12968                         error = EINVAL;
12969                         goto out;
12970                 }
12971                 if (hold_tcblock) {
12972                         SCTP_TCB_UNLOCK(stcb);
12973                         hold_tcblock = 0;
12974                 }
12975                 if (top) {
12976                         struct mbuf *cntm = NULL;
12977 
12978                         mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_WAIT, 1, MT_DATA);
12979                         if (sndlen != 0) {
12980                                 for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
12981                                         tot_out += SCTP_BUF_LEN(cntm);
12982                                 }
12983                         }
12984                 } else {
12985                         /* Must fit in a MTU */
12986                         tot_out = sndlen;
12987                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12988                         if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12989                                 /* To big */
12990                                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12991                                 error = EMSGSIZE;
12992                                 goto out;
12993                         }
12994                         mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12995                 }
12996                 if (mm == NULL) {
12997                         SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12998                         error = ENOMEM;
12999                         goto out;
13000                 }
13001                 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
13002                 max_out -= sizeof(struct sctp_abort_msg);
13003                 if (tot_out > max_out) {
13004                         tot_out = max_out;
13005                 }
13006                 if (mm) {
13007                         struct sctp_paramhdr *ph;
13008 
13009                         /* now move forward the data pointer */
13010                         ph = mtod(mm, struct sctp_paramhdr *);
13011                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
13012                         ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
13013                         ph++;
13014                         SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
13015                         if (top == NULL) {
13016                                 error = uiomove((caddr_t)ph, (int)tot_out, uio);
13017                                 if (error) {
13018                                         /*-
13019                                          * Here if we can't get his data we
13020                                          * still abort we just don't get to
13021                                          * send the users note :-0
13022                                          */
13023                                         sctp_m_freem(mm);
13024                                         mm = NULL;
13025                                 }
13026                         } else {
13027                                 if (sndlen != 0) {
13028                                         SCTP_BUF_NEXT(mm) = top;
13029                                 }
13030                         }
13031                 }
13032                 if (hold_tcblock == 0) {
13033                         SCTP_TCB_LOCK(stcb);
13034                 }
13035                 atomic_add_int(&stcb->asoc.refcnt, -1);
13036                 free_cnt_applied = 0;
13037                 /* release this lock, otherwise we hang on ourselves */
13038                 sctp_abort_an_association(stcb->sctp_ep, stcb,
13039                     SCTP_RESPONSE_TO_USER_REQ,
13040                     mm, SCTP_SO_LOCKED);
13041                 /* now relock the stcb so everything is sane */
13042                 hold_tcblock = 0;
13043                 stcb = NULL;
13044                 /*
13045                  * In this case top is already chained to mm avoid double
13046                  * free, since we free it below if top != NULL and driver
13047                  * would free it after sending the packet out
13048                  */
13049                 if (sndlen != 0) {
13050                         top = NULL;
13051                 }
13052                 goto out_unlocked;
13053         }
13054         /* Calculate the maximum we can send */
13055         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13056         if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13057                 if (non_blocking) {
13058                         /* we already checked for non-blocking above. */
13059                         max_len = sndlen;
13060                 } else {
13061                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13062                 }
13063         } else {
13064                 max_len = 0;
13065         }
13066         if (hold_tcblock) {
13067                 SCTP_TCB_UNLOCK(stcb);
13068                 hold_tcblock = 0;
13069         }
13070         /* Is the stream no. valid? */
13071         if (srcv->sinfo_stream >= asoc->streamoutcnt) {
13072                 /* Invalid stream number */
13073                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13074                 error = EINVAL;
13075                 goto out_unlocked;
13076         }
13077         if (asoc->strmout == NULL) {
13078                 /* huh? software error */
13079                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
13080                 error = EFAULT;
13081                 goto out_unlocked;
13082         }
13083         /* Unless E_EOR mode is on, we must make a send FIT in one call. */
13084         if ((user_marks_eor == 0) &&
13085             (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13086                 /* It will NEVER fit */
13087                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13088                 error = EMSGSIZE;
13089                 goto out_unlocked;
13090         }
13091         if ((uio == NULL) && user_marks_eor) {
13092                 /*-
13093                  * We do not support eeor mode for
13094                  * sending with mbuf chains (like sendfile).
13095                  */
13096                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13097                 error = EINVAL;
13098                 goto out_unlocked;
13099         }
13100         if (user_marks_eor) {
13101                 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13102         } else {
13103                 /*-
13104                  * For non-eeor the whole message must fit in
13105                  * the socket send buffer.
13106                  */
13107                 local_add_more = sndlen;
13108         }
13109         len = 0;
13110         if (non_blocking) {
13111                 goto skip_preblock;
13112         }
13113         if (((max_len <= local_add_more) &&
13114             (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13115             (max_len == 0) ||
13116             ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13117                 /* No room right now ! */
13118                 SOCKBUF_LOCK(&so->so_snd);
13119                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13120                 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13121                     ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13122                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13123                             (unsigned int)SCTP_SB_LIMIT_SND(so),
13124                             inqueue_bytes,
13125                             local_add_more,
13126                             stcb->asoc.stream_queue_cnt,
13127                             stcb->asoc.chunks_on_out_queue,
13128                             SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13129                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13130                                 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
13131                         }
13132                         be.error = 0;
13133                         stcb->block_entry = &be;
13134                         error = sbwait(&so->so_snd);
13135                         stcb->block_entry = NULL;
13136                         if (error || so->so_error || be.error) {
13137                                 if (error == 0) {
13138                                         if (so->so_error)
13139                                                 error = so->so_error;
13140                                         if (be.error) {
13141                                                 error = be.error;
13142                                         }
13143                                 }
13144                                 SOCKBUF_UNLOCK(&so->so_snd);
13145                                 goto out_unlocked;
13146                         }
13147                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13148                                 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13149                                     asoc, stcb->asoc.total_output_queue_size);
13150                         }
13151                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13152                                 goto out_unlocked;
13153                         }
13154                         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13155                 }
13156                 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13157                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13158                 } else {
13159                         max_len = 0;
13160                 }
13161                 SOCKBUF_UNLOCK(&so->so_snd);
13162         }
13163 skip_preblock:
13164         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13165                 goto out_unlocked;
13166         }
13167         /*
13168          * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13169          * case NOTE: uio will be null when top/mbuf is passed
13170          */
13171         if (sndlen == 0) {
13172                 if (srcv->sinfo_flags & SCTP_EOF) {
13173                         got_all_of_the_send = 1;
13174                         goto dataless_eof;
13175                 } else {
13176                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13177                         error = EINVAL;
13178                         goto out;
13179                 }
13180         }
13181         if (top == NULL) {
13182                 struct sctp_stream_queue_pending *sp;
13183                 struct sctp_stream_out *strm;
13184                 uint32_t sndout;
13185 
13186                 SCTP_TCB_SEND_LOCK(stcb);
13187                 if ((asoc->stream_locked) &&
13188                     (asoc->stream_locked_on != srcv->sinfo_stream)) {
13189                         SCTP_TCB_SEND_UNLOCK(stcb);
13190                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13191                         error = EINVAL;
13192                         goto out;
13193                 }
13194                 SCTP_TCB_SEND_UNLOCK(stcb);
13195 
13196                 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13197                 if (strm->last_msg_incomplete == 0) {
13198         do_a_copy_in:
13199                         sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error);
13200                         if ((sp == NULL) || (error)) {
13201                                 goto out;
13202                         }
13203                         SCTP_TCB_SEND_LOCK(stcb);
13204                         if (sp->msg_is_complete) {
13205                                 strm->last_msg_incomplete = 0;
13206                                 asoc->stream_locked = 0;
13207                         } else {
13208                                 /*
13209                                  * Just got locked to this guy in case of an
13210                                  * interrupt.
13211                                  */
13212                                 strm->last_msg_incomplete = 1;
13213                                 asoc->stream_locked = 1;
13214                                 asoc->stream_locked_on = srcv->sinfo_stream;
13215                                 sp->sender_all_done = 0;
13216                         }
13217                         sctp_snd_sb_alloc(stcb, sp->length);
13218                         atomic_add_int(&asoc->stream_queue_cnt, 1);
13219                         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13220                                 sp->strseq = strm->next_sequence_sent;
13221                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13222                                         sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13223                                             (uintptr_t) stcb, sp->length,
13224                                             (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13225                                 }
13226                                 strm->next_sequence_sent++;
13227                         } else {
13228                                 SCTP_STAT_INCR(sctps_sends_with_unord);
13229                         }
13230                         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13231                         stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1);
13232                         SCTP_TCB_SEND_UNLOCK(stcb);
13233                 } else {
13234                         SCTP_TCB_SEND_LOCK(stcb);
13235                         sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13236                         SCTP_TCB_SEND_UNLOCK(stcb);
13237                         if (sp == NULL) {
13238                                 /* ???? Huh ??? last msg is gone */
13239 #ifdef INVARIANTS
13240                                 panic("Warning: Last msg marked incomplete, yet nothing left?");
13241 #else
13242                                 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13243                                 strm->last_msg_incomplete = 0;
13244 #endif
13245                                 goto do_a_copy_in;
13246 
13247                         }
13248                 }
13249                 while (uio->uio_resid > 0) {
13250                         /* How much room do we have? */
13251                         struct mbuf *new_tail, *mm;
13252 
13253                         if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13254                                 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13255                         else
13256                                 max_len = 0;
13257 
13258                         if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13259                             (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13260                             (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13261                                 sndout = 0;
13262                                 new_tail = NULL;
13263                                 if (hold_tcblock) {
13264                                         SCTP_TCB_UNLOCK(stcb);
13265                                         hold_tcblock = 0;
13266                                 }
13267                                 mm = sctp_copy_resume(uio, max_len, user_marks_eor, &error, &sndout, &new_tail);
13268                                 if ((mm == NULL) || error) {
13269                                         if (mm) {
13270                                                 sctp_m_freem(mm);
13271                                         }
13272                                         goto out;
13273                                 }
13274                                 /* Update the mbuf and count */
13275                                 SCTP_TCB_SEND_LOCK(stcb);
13276                                 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13277                                         /*
13278                                          * we need to get out. Peer probably
13279                                          * aborted.
13280                                          */
13281                                         sctp_m_freem(mm);
13282                                         if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13283                                                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13284                                                 error = ECONNRESET;
13285                                         }
13286                                         SCTP_TCB_SEND_UNLOCK(stcb);
13287                                         goto out;
13288                                 }
13289                                 if (sp->tail_mbuf) {
13290                                         /* tack it to the end */
13291                                         SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13292                                         sp->tail_mbuf = new_tail;
13293                                 } else {
13294                                         /* A stolen mbuf */
13295                                         sp->data = mm;
13296                                         sp->tail_mbuf = new_tail;
13297                                 }
13298                                 sctp_snd_sb_alloc(stcb, sndout);
13299                                 atomic_add_int(&sp->length, sndout);
13300                                 len += sndout;
13301 
13302                                 /* Did we reach EOR? */
13303                                 if ((uio->uio_resid == 0) &&
13304                                     ((user_marks_eor == 0) ||
13305                                     (srcv->sinfo_flags & SCTP_EOF) ||
13306                                     (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13307                                         sp->msg_is_complete = 1;
13308                                 } else {
13309                                         sp->msg_is_complete = 0;
13310                                 }
13311                                 SCTP_TCB_SEND_UNLOCK(stcb);
13312                         }
13313                         if (uio->uio_resid == 0) {
13314                                 /* got it all? */
13315                                 continue;
13316                         }
13317                         /* PR-SCTP? */
13318                         if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13319                                 /*
13320                                  * This is ugly but we must assure locking
13321                                  * order
13322                                  */
13323                                 if (hold_tcblock == 0) {
13324                                         SCTP_TCB_LOCK(stcb);
13325                                         hold_tcblock = 1;
13326                                 }
13327                                 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13328                                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13329                                 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13330                                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13331                                 else
13332                                         max_len = 0;
13333                                 if (max_len > 0) {
13334                                         continue;
13335                                 }
13336                                 SCTP_TCB_UNLOCK(stcb);
13337                                 hold_tcblock = 0;
13338                         }
13339                         /* wait for space now */
13340                         if (non_blocking) {
13341                                 /* Non-blocking io in place out */
13342                                 goto skip_out_eof;
13343                         }
13344                         /* What about the INIT, send it maybe */
13345                         if (queue_only_for_init) {
13346                                 if (hold_tcblock == 0) {
13347                                         SCTP_TCB_LOCK(stcb);
13348                                         hold_tcblock = 1;
13349                                 }
13350                                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13351                                         /* a collision took us forward? */
13352                                         queue_only = 0;
13353                                 } else {
13354                                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13355                                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13356                                         queue_only = 1;
13357                                 }
13358                         }
13359                         if ((net->flight_size > net->cwnd) &&
13360                             (asoc->sctp_cmt_on_off == 0)) {
13361                                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13362                                 queue_only = 1;
13363                         } else if (asoc->ifp_had_enobuf) {
13364                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13365                                 if (net->flight_size > (2 * net->mtu)) {
13366                                         queue_only = 1;
13367                                 }
13368                                 asoc->ifp_had_enobuf = 0;
13369                         }
13370                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13371                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13372                         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13373                             (stcb->asoc.total_flight > 0) &&
13374                             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13375                             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13376 
13377                                 /*-
13378                                  * Ok, Nagle is set on and we have data outstanding.
13379                                  * Don't send anything and let SACKs drive out the
13380                                  * data unless wen have a "full" segment to send.
13381                                  */
13382                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13383                                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13384                                 }
13385                                 SCTP_STAT_INCR(sctps_naglequeued);
13386                                 nagle_applies = 1;
13387                         } else {
13388                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13389                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13390                                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13391                                 }
13392                                 SCTP_STAT_INCR(sctps_naglesent);
13393                                 nagle_applies = 0;
13394                         }
13395                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13396 
13397                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13398                                     nagle_applies, un_sent);
13399                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13400                                     stcb->asoc.total_flight,
13401                                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13402                         }
13403                         if (queue_only_for_init)
13404                                 queue_only_for_init = 0;
13405                         if ((queue_only == 0) && (nagle_applies == 0)) {
13406                                 /*-
13407                                  * need to start chunk output
13408                                  * before blocking.. note that if
13409                                  * a lock is already applied, then
13410                                  * the input via the net is happening
13411                                  * and I don't need to start output :-D
13412                                  */
13413                                 if (hold_tcblock == 0) {
13414                                         if (SCTP_TCB_TRYLOCK(stcb)) {
13415                                                 hold_tcblock = 1;
13416                                                 sctp_chunk_output(inp,
13417                                                     stcb,
13418                                                     SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13419                                         }
13420                                 } else {
13421                                         sctp_chunk_output(inp,
13422                                             stcb,
13423                                             SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13424                                 }
13425                                 if (hold_tcblock == 1) {
13426                                         SCTP_TCB_UNLOCK(stcb);
13427                                         hold_tcblock = 0;
13428                                 }
13429                         }
13430                         SOCKBUF_LOCK(&so->so_snd);
13431                         /*-
13432                          * This is a bit strange, but I think it will
13433                          * work. The total_output_queue_size is locked and
13434                          * protected by the TCB_LOCK, which we just released.
13435                          * There is a race that can occur between releasing it
13436                          * above, and me getting the socket lock, where sacks
13437                          * come in but we have not put the SB_WAIT on the
13438                          * so_snd buffer to get the wakeup. After the LOCK
13439                          * is applied the sack_processing will also need to
13440                          * LOCK the so->so_snd to do the actual sowwakeup(). So
13441                          * once we have the socket buffer lock if we recheck the
13442                          * size we KNOW we will get to sleep safely with the
13443                          * wakeup flag in place.
13444                          */
13445                         if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13446                             min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13447                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13448                                         sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13449                                             asoc, uio->uio_resid);
13450                                 }
13451                                 be.error = 0;
13452                                 stcb->block_entry = &be;
13453                                 error = sbwait(&so->so_snd);
13454                                 stcb->block_entry = NULL;
13455 
13456                                 if (error || so->so_error || be.error) {
13457                                         if (error == 0) {
13458                                                 if (so->so_error)
13459                                                         error = so->so_error;
13460                                                 if (be.error) {
13461                                                         error = be.error;
13462                                                 }
13463                                         }
13464                                         SOCKBUF_UNLOCK(&so->so_snd);
13465                                         goto out_unlocked;
13466                                 }
13467                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13468                                         sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13469                                             asoc, stcb->asoc.total_output_queue_size);
13470                                 }
13471                         }
13472                         SOCKBUF_UNLOCK(&so->so_snd);
13473                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13474                                 goto out_unlocked;
13475                         }
13476                 }
13477                 SCTP_TCB_SEND_LOCK(stcb);
13478                 if (sp) {
13479                         if (sp->msg_is_complete == 0) {
13480                                 strm->last_msg_incomplete = 1;
13481                                 asoc->stream_locked = 1;
13482                                 asoc->stream_locked_on = srcv->sinfo_stream;
13483                         } else {
13484                                 sp->sender_all_done = 1;
13485                                 strm->last_msg_incomplete = 0;
13486                                 asoc->stream_locked = 0;
13487                         }
13488                 } else {
13489                         SCTP_PRINTF("Huh no sp TSNH?\n");
13490                         strm->last_msg_incomplete = 0;
13491                         asoc->stream_locked = 0;
13492                 }
13493                 SCTP_TCB_SEND_UNLOCK(stcb);
13494                 if (uio->uio_resid == 0) {
13495                         got_all_of_the_send = 1;
13496                 }
13497         } else {
13498                 /* We send in a 0, since we do NOT have any locks */
13499                 error = sctp_msg_append(stcb, net, top, srcv, 0);
13500                 top = NULL;
13501                 if (srcv->sinfo_flags & SCTP_EOF) {
13502                         /*
13503                          * This should only happen for Panda for the mbuf
13504                          * send case, which does NOT yet support EEOR mode.
13505                          * Thus, we can just set this flag to do the proper
13506                          * EOF handling.
13507                          */
13508                         got_all_of_the_send = 1;
13509                 }
13510         }
13511         if (error) {
13512                 goto out;
13513         }
13514 dataless_eof:
13515         /* EOF thing ? */
13516         if ((srcv->sinfo_flags & SCTP_EOF) &&
13517             (got_all_of_the_send == 1) &&
13518             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13519                 int cnt;
13520 
13521                 SCTP_STAT_INCR(sctps_sends_with_eof);
13522                 error = 0;
13523                 if (hold_tcblock == 0) {
13524                         SCTP_TCB_LOCK(stcb);
13525                         hold_tcblock = 1;
13526                 }
13527                 cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED);
13528                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13529                     TAILQ_EMPTY(&asoc->sent_queue) &&
13530                     (cnt == 0)) {
13531                         if (asoc->locked_on_sending) {
13532                                 goto abort_anyway;
13533                         }
13534                         /* there is nothing queued to send, so I'm done... */
13535                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13536                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13537                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13538                                 struct sctp_nets *netp;
13539 
13540                                 if (stcb->asoc.alternate) {
13541                                         netp = stcb->asoc.alternate;
13542                                 } else {
13543                                         netp = stcb->asoc.primary_destination;
13544                                 }
13545                                 /* only send SHUTDOWN the first time through */
13546                                 sctp_send_shutdown(stcb, netp);
13547                                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13548                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13549                                 }
13550                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13551                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13552                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13553                                     netp);
13554                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13555                                     asoc->primary_destination);
13556                         }
13557                 } else {
13558                         /*-
13559                          * we still got (or just got) data to send, so set
13560                          * SHUTDOWN_PENDING
13561                          */
13562                         /*-
13563                          * XXX sockets draft says that SCTP_EOF should be
13564                          * sent with no data.  currently, we will allow user
13565                          * data to be sent first and move to
13566                          * SHUTDOWN-PENDING
13567                          */
13568                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13569                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13570                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13571                                 if (hold_tcblock == 0) {
13572                                         SCTP_TCB_LOCK(stcb);
13573                                         hold_tcblock = 1;
13574                                 }
13575                                 if (asoc->locked_on_sending) {
13576                                         /* Locked to send out the data */
13577                                         struct sctp_stream_queue_pending *sp;
13578 
13579                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13580                                         if (sp) {
13581                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13582                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13583                                         }
13584                                 }
13585                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13586                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13587                                     TAILQ_EMPTY(&asoc->sent_queue) &&
13588                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13589                         abort_anyway:
13590                                         if (free_cnt_applied) {
13591                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
13592                                                 free_cnt_applied = 0;
13593                                         }
13594                                         sctp_abort_an_association(stcb->sctp_ep, stcb,
13595                                             SCTP_RESPONSE_TO_USER_REQ,
13596                                             NULL, SCTP_SO_LOCKED);
13597                                         /*
13598                                          * now relock the stcb so everything
13599                                          * is sane
13600                                          */
13601                                         hold_tcblock = 0;
13602                                         stcb = NULL;
13603                                         goto out;
13604                                 }
13605                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13606                                     asoc->primary_destination);
13607                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13608                         }
13609                 }
13610         }
13611 skip_out_eof:
13612         if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13613                 some_on_control = 1;
13614         }
13615         if (queue_only_for_init) {
13616                 if (hold_tcblock == 0) {
13617                         SCTP_TCB_LOCK(stcb);
13618                         hold_tcblock = 1;
13619                 }
13620                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13621                         /* a collision took us forward? */
13622                         queue_only = 0;
13623                 } else {
13624                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13625                         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13626                         queue_only = 1;
13627                 }
13628         }
13629         if ((net->flight_size > net->cwnd) &&
13630             (stcb->asoc.sctp_cmt_on_off == 0)) {
13631                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13632                 queue_only = 1;
13633         } else if (asoc->ifp_had_enobuf) {
13634                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13635                 if (net->flight_size > (2 * net->mtu)) {
13636                         queue_only = 1;
13637                 }
13638                 asoc->ifp_had_enobuf = 0;
13639         }
13640         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13641             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13642         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13643             (stcb->asoc.total_flight > 0) &&
13644             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13645             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13646                 /*-
13647                  * Ok, Nagle is set on and we have data outstanding.
13648                  * Don't send anything and let SACKs drive out the
13649                  * data unless wen have a "full" segment to send.
13650                  */
13651                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13652                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13653                 }
13654                 SCTP_STAT_INCR(sctps_naglequeued);
13655                 nagle_applies = 1;
13656         } else {
13657                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13658                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13659                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13660                 }
13661                 SCTP_STAT_INCR(sctps_naglesent);
13662                 nagle_applies = 0;
13663         }
13664         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13665                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13666                     nagle_applies, un_sent);
13667                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13668                     stcb->asoc.total_flight,
13669                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13670         }
13671         if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13672                 /* we can attempt to send too. */
13673                 if (hold_tcblock == 0) {
13674                         /*
13675                          * If there is activity recv'ing sacks no need to
13676                          * send
13677                          */
13678                         if (SCTP_TCB_TRYLOCK(stcb)) {
13679                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13680                                 hold_tcblock = 1;
13681                         }
13682                 } else {
13683                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13684                 }
13685         } else if ((queue_only == 0) &&
13686                     (stcb->asoc.peers_rwnd == 0) &&
13687             (stcb->asoc.total_flight == 0)) {
13688                 /* We get to have a probe outstanding */
13689                 if (hold_tcblock == 0) {
13690                         hold_tcblock = 1;
13691                         SCTP_TCB_LOCK(stcb);
13692                 }
13693                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13694         } else if (some_on_control) {
13695                 int num_out, reason, frag_point;
13696 
13697                 /* Here we do control only */
13698                 if (hold_tcblock == 0) {
13699                         hold_tcblock = 1;
13700                         SCTP_TCB_LOCK(stcb);
13701                 }
13702                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13703                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13704                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13705         }
13706         SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13707             queue_only, stcb->asoc.peers_rwnd, un_sent,
13708             stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13709             stcb->asoc.total_output_queue_size, error);
13710 
13711 out:
13712 out_unlocked:
13713 
13714         if (local_soresv && stcb) {
13715                 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13716         }
13717         if (create_lock_applied) {
13718                 SCTP_ASOC_CREATE_UNLOCK(inp);
13719         }
13720         if ((stcb) && hold_tcblock) {
13721                 SCTP_TCB_UNLOCK(stcb);
13722         }
13723         if (stcb && free_cnt_applied) {
13724                 atomic_add_int(&stcb->asoc.refcnt, -1);
13725         }
13726 #ifdef INVARIANTS
13727         if (stcb) {
13728                 if (mtx_owned(&stcb->tcb_mtx)) {
13729                         panic("Leaving with tcb mtx owned?");
13730                 }
13731                 if (mtx_owned(&stcb->tcb_send_mtx)) {
13732                         panic("Leaving with tcb send mtx owned?");
13733                 }
13734         }
13735 #endif
13736 #ifdef INVARIANTS
13737         if (inp) {
13738                 sctp_validate_no_locks(inp);
13739         } else {
13740                 printf("Warning - inp is NULL so cant validate locks\n");
13741         }
13742 #endif
13743         if (top) {
13744                 sctp_m_freem(top);
13745         }
13746         if (control) {
13747                 sctp_m_freem(control);
13748         }
13749         return (error);
13750 }
13751 
13752 
13753 /*
13754  * generate an AUTHentication chunk, if required
13755  */
13756 struct mbuf *
13757 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13758     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13759     struct sctp_tcb *stcb, uint8_t chunk)
13760 {
13761         struct mbuf *m_auth;
13762         struct sctp_auth_chunk *auth;
13763         int chunk_len;
13764         struct mbuf *cn;
13765 
13766         if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13767             (stcb == NULL))
13768                 return (m);
13769 
13770         /* sysctl disabled auth? */
13771         if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13772                 return (m);
13773 
13774         /* peer doesn't do auth... */
13775         if (!stcb->asoc.peer_supports_auth) {
13776                 return (m);
13777         }
13778         /* does the requested chunk require auth? */
13779         if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13780                 return (m);
13781         }
13782         m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13783         if (m_auth == NULL) {
13784                 /* no mbuf's */
13785                 return (m);
13786         }
13787         /* reserve some space if this will be the first mbuf */
13788         if (m == NULL)
13789                 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13790         /* fill in the AUTH chunk details */
13791         auth = mtod(m_auth, struct sctp_auth_chunk *);
13792         bzero(auth, sizeof(*auth));
13793         auth->ch.chunk_type = SCTP_AUTHENTICATION;
13794         auth->ch.chunk_flags = 0;
13795         chunk_len = sizeof(*auth) +
13796             sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13797         auth->ch.chunk_length = htons(chunk_len);
13798         auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13799         /* key id and hmac digest will be computed and filled in upon send */
13800 
13801         /* save the offset where the auth was inserted into the chain */
13802         *offset = 0;
13803         for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13804                 *offset += SCTP_BUF_LEN(cn);
13805         }
13806 
13807         /* update length and return pointer to the auth chunk */
13808         SCTP_BUF_LEN(m_auth) = chunk_len;
13809         m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13810         if (auth_ret != NULL)
13811                 *auth_ret = auth;
13812 
13813         return (m);
13814 }
13815 
13816 #ifdef INET6
13817 int
13818 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13819 {
13820         struct nd_prefix *pfx = NULL;
13821         struct nd_pfxrouter *pfxrtr = NULL;
13822         struct sockaddr_in6 gw6;
13823 
13824         if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13825                 return (0);
13826 
13827         /* get prefix entry of address */
13828         LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13829                 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13830                         continue;
13831                 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13832                     &src6->sin6_addr, &pfx->ndpr_mask))
13833                         break;
13834         }
13835         /* no prefix entry in the prefix list */
13836         if (pfx == NULL) {
13837                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13838                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13839                 return (0);
13840         }
13841         SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13842         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13843 
13844         /* search installed gateway from prefix entry */
13845         for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13846             pfxrtr->pfr_next) {
13847                 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13848                 gw6.sin6_family = AF_INET6;
13849                 gw6.sin6_len = sizeof(struct sockaddr_in6);
13850                 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13851                     sizeof(struct in6_addr));
13852                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13853                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13854                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13855                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13856                 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13857                     ro->ro_rt->rt_gateway)) {
13858                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13859                         return (1);
13860                 }
13861         }
13862         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13863         return (0);
13864 }
13865 
13866 #endif
13867 
13868 int
13869 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13870 {
13871 #ifdef INET
13872         struct sockaddr_in *sin, *mask;
13873         struct ifaddr *ifa;
13874         struct in_addr srcnetaddr, gwnetaddr;
13875 
13876         if (ro == NULL || ro->ro_rt == NULL ||
13877             sifa->address.sa.sa_family != AF_INET) {
13878                 return (0);
13879         }
13880         ifa = (struct ifaddr *)sifa->ifa;
13881         mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13882         sin = (struct sockaddr_in *)&sifa->address.sin;
13883         srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13884         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13885         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13886         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13887 
13888         sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13889         gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13890         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13891         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13892         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13893         if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13894                 return (1);
13895         }
13896 #endif
13897         return (0);
13898 }

Cache object: 60a6e3d2e6f2e74328ea863be80b1fe6


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