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  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions are met:
    6  *
    7  * a) Redistributions of source code must retain the above copyright notice,
    8  *   this list of conditions and the following disclaimer.
    9  *
   10  * b) Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in
   12  *   the documentation and/or other materials provided with the distribution.
   13  *
   14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
   15  *    contributors may be used to endorse or promote products derived
   16  *    from this software without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   28  * THE POSSIBILITY OF SUCH DAMAGE.
   29  */
   30 
   31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/7.4/sys/netinet/sctp_output.c 202716 2010-01-20 19:20:31Z tuexen $");
   35 
   36 #include <netinet/sctp_os.h>
   37 #include <sys/proc.h>
   38 #include <netinet/sctp_var.h>
   39 #include <netinet/sctp_sysctl.h>
   40 #include <netinet/sctp_header.h>
   41 #include <netinet/sctp_pcb.h>
   42 #include <netinet/sctputil.h>
   43 #include <netinet/sctp_output.h>
   44 #include <netinet/sctp_uio.h>
   45 #include <netinet/sctputil.h>
   46 #include <netinet/sctp_auth.h>
   47 #include <netinet/sctp_timer.h>
   48 #include <netinet/sctp_asconf.h>
   49 #include <netinet/sctp_indata.h>
   50 #include <netinet/sctp_bsd_addr.h>
   51 #include <netinet/sctp_input.h>
   52 #include <netinet/udp.h>
   53 #include <machine/in_cksum.h>
   54 
   55 
   56 
   57 #define SCTP_MAX_GAPS_INARRAY 4
   58 struct sack_track {
   59         uint8_t right_edge;     /* mergable on the right edge */
   60         uint8_t left_edge;      /* mergable on the left edge */
   61         uint8_t num_entries;
   62         uint8_t spare;
   63         struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
   64 };
   65 
   66 struct sack_track sack_array[256] = {
   67         {0, 0, 0, 0,            /* 0x00 */
   68                 {{0, 0},
   69                 {0, 0},
   70                 {0, 0},
   71                 {0, 0}
   72                 }
   73         },
   74         {1, 0, 1, 0,            /* 0x01 */
   75                 {{0, 0},
   76                 {0, 0},
   77                 {0, 0},
   78                 {0, 0}
   79                 }
   80         },
   81         {0, 0, 1, 0,            /* 0x02 */
   82                 {{1, 1},
   83                 {0, 0},
   84                 {0, 0},
   85                 {0, 0}
   86                 }
   87         },
   88         {1, 0, 1, 0,            /* 0x03 */
   89                 {{0, 1},
   90                 {0, 0},
   91                 {0, 0},
   92                 {0, 0}
   93                 }
   94         },
   95         {0, 0, 1, 0,            /* 0x04 */
   96                 {{2, 2},
   97                 {0, 0},
   98                 {0, 0},
   99                 {0, 0}
  100                 }
  101         },
  102         {1, 0, 2, 0,            /* 0x05 */
  103                 {{0, 0},
  104                 {2, 2},
  105                 {0, 0},
  106                 {0, 0}
  107                 }
  108         },
  109         {0, 0, 1, 0,            /* 0x06 */
  110                 {{1, 2},
  111                 {0, 0},
  112                 {0, 0},
  113                 {0, 0}
  114                 }
  115         },
  116         {1, 0, 1, 0,            /* 0x07 */
  117                 {{0, 2},
  118                 {0, 0},
  119                 {0, 0},
  120                 {0, 0}
  121                 }
  122         },
  123         {0, 0, 1, 0,            /* 0x08 */
  124                 {{3, 3},
  125                 {0, 0},
  126                 {0, 0},
  127                 {0, 0}
  128                 }
  129         },
  130         {1, 0, 2, 0,            /* 0x09 */
  131                 {{0, 0},
  132                 {3, 3},
  133                 {0, 0},
  134                 {0, 0}
  135                 }
  136         },
  137         {0, 0, 2, 0,            /* 0x0a */
  138                 {{1, 1},
  139                 {3, 3},
  140                 {0, 0},
  141                 {0, 0}
  142                 }
  143         },
  144         {1, 0, 2, 0,            /* 0x0b */
  145                 {{0, 1},
  146                 {3, 3},
  147                 {0, 0},
  148                 {0, 0}
  149                 }
  150         },
  151         {0, 0, 1, 0,            /* 0x0c */
  152                 {{2, 3},
  153                 {0, 0},
  154                 {0, 0},
  155                 {0, 0}
  156                 }
  157         },
  158         {1, 0, 2, 0,            /* 0x0d */
  159                 {{0, 0},
  160                 {2, 3},
  161                 {0, 0},
  162                 {0, 0}
  163                 }
  164         },
  165         {0, 0, 1, 0,            /* 0x0e */
  166                 {{1, 3},
  167                 {0, 0},
  168                 {0, 0},
  169                 {0, 0}
  170                 }
  171         },
  172         {1, 0, 1, 0,            /* 0x0f */
  173                 {{0, 3},
  174                 {0, 0},
  175                 {0, 0},
  176                 {0, 0}
  177                 }
  178         },
  179         {0, 0, 1, 0,            /* 0x10 */
  180                 {{4, 4},
  181                 {0, 0},
  182                 {0, 0},
  183                 {0, 0}
  184                 }
  185         },
  186         {1, 0, 2, 0,            /* 0x11 */
  187                 {{0, 0},
  188                 {4, 4},
  189                 {0, 0},
  190                 {0, 0}
  191                 }
  192         },
  193         {0, 0, 2, 0,            /* 0x12 */
  194                 {{1, 1},
  195                 {4, 4},
  196                 {0, 0},
  197                 {0, 0}
  198                 }
  199         },
  200         {1, 0, 2, 0,            /* 0x13 */
  201                 {{0, 1},
  202                 {4, 4},
  203                 {0, 0},
  204                 {0, 0}
  205                 }
  206         },
  207         {0, 0, 2, 0,            /* 0x14 */
  208                 {{2, 2},
  209                 {4, 4},
  210                 {0, 0},
  211                 {0, 0}
  212                 }
  213         },
  214         {1, 0, 3, 0,            /* 0x15 */
  215                 {{0, 0},
  216                 {2, 2},
  217                 {4, 4},
  218                 {0, 0}
  219                 }
  220         },
  221         {0, 0, 2, 0,            /* 0x16 */
  222                 {{1, 2},
  223                 {4, 4},
  224                 {0, 0},
  225                 {0, 0}
  226                 }
  227         },
  228         {1, 0, 2, 0,            /* 0x17 */
  229                 {{0, 2},
  230                 {4, 4},
  231                 {0, 0},
  232                 {0, 0}
  233                 }
  234         },
  235         {0, 0, 1, 0,            /* 0x18 */
  236                 {{3, 4},
  237                 {0, 0},
  238                 {0, 0},
  239                 {0, 0}
  240                 }
  241         },
  242         {1, 0, 2, 0,            /* 0x19 */
  243                 {{0, 0},
  244                 {3, 4},
  245                 {0, 0},
  246                 {0, 0}
  247                 }
  248         },
  249         {0, 0, 2, 0,            /* 0x1a */
  250                 {{1, 1},
  251                 {3, 4},
  252                 {0, 0},
  253                 {0, 0}
  254                 }
  255         },
  256         {1, 0, 2, 0,            /* 0x1b */
  257                 {{0, 1},
  258                 {3, 4},
  259                 {0, 0},
  260                 {0, 0}
  261                 }
  262         },
  263         {0, 0, 1, 0,            /* 0x1c */
  264                 {{2, 4},
  265                 {0, 0},
  266                 {0, 0},
  267                 {0, 0}
  268                 }
  269         },
  270         {1, 0, 2, 0,            /* 0x1d */
  271                 {{0, 0},
  272                 {2, 4},
  273                 {0, 0},
  274                 {0, 0}
  275                 }
  276         },
  277         {0, 0, 1, 0,            /* 0x1e */
  278                 {{1, 4},
  279                 {0, 0},
  280                 {0, 0},
  281                 {0, 0}
  282                 }
  283         },
  284         {1, 0, 1, 0,            /* 0x1f */
  285                 {{0, 4},
  286                 {0, 0},
  287                 {0, 0},
  288                 {0, 0}
  289                 }
  290         },
  291         {0, 0, 1, 0,            /* 0x20 */
  292                 {{5, 5},
  293                 {0, 0},
  294                 {0, 0},
  295                 {0, 0}
  296                 }
  297         },
  298         {1, 0, 2, 0,            /* 0x21 */
  299                 {{0, 0},
  300                 {5, 5},
  301                 {0, 0},
  302                 {0, 0}
  303                 }
  304         },
  305         {0, 0, 2, 0,            /* 0x22 */
  306                 {{1, 1},
  307                 {5, 5},
  308                 {0, 0},
  309                 {0, 0}
  310                 }
  311         },
  312         {1, 0, 2, 0,            /* 0x23 */
  313                 {{0, 1},
  314                 {5, 5},
  315                 {0, 0},
  316                 {0, 0}
  317                 }
  318         },
  319         {0, 0, 2, 0,            /* 0x24 */
  320                 {{2, 2},
  321                 {5, 5},
  322                 {0, 0},
  323                 {0, 0}
  324                 }
  325         },
  326         {1, 0, 3, 0,            /* 0x25 */
  327                 {{0, 0},
  328                 {2, 2},
  329                 {5, 5},
  330                 {0, 0}
  331                 }
  332         },
  333         {0, 0, 2, 0,            /* 0x26 */
  334                 {{1, 2},
  335                 {5, 5},
  336                 {0, 0},
  337                 {0, 0}
  338                 }
  339         },
  340         {1, 0, 2, 0,            /* 0x27 */
  341                 {{0, 2},
  342                 {5, 5},
  343                 {0, 0},
  344                 {0, 0}
  345                 }
  346         },
  347         {0, 0, 2, 0,            /* 0x28 */
  348                 {{3, 3},
  349                 {5, 5},
  350                 {0, 0},
  351                 {0, 0}
  352                 }
  353         },
  354         {1, 0, 3, 0,            /* 0x29 */
  355                 {{0, 0},
  356                 {3, 3},
  357                 {5, 5},
  358                 {0, 0}
  359                 }
  360         },
  361         {0, 0, 3, 0,            /* 0x2a */
  362                 {{1, 1},
  363                 {3, 3},
  364                 {5, 5},
  365                 {0, 0}
  366                 }
  367         },
  368         {1, 0, 3, 0,            /* 0x2b */
  369                 {{0, 1},
  370                 {3, 3},
  371                 {5, 5},
  372                 {0, 0}
  373                 }
  374         },
  375         {0, 0, 2, 0,            /* 0x2c */
  376                 {{2, 3},
  377                 {5, 5},
  378                 {0, 0},
  379                 {0, 0}
  380                 }
  381         },
  382         {1, 0, 3, 0,            /* 0x2d */
  383                 {{0, 0},
  384                 {2, 3},
  385                 {5, 5},
  386                 {0, 0}
  387                 }
  388         },
  389         {0, 0, 2, 0,            /* 0x2e */
  390                 {{1, 3},
  391                 {5, 5},
  392                 {0, 0},
  393                 {0, 0}
  394                 }
  395         },
  396         {1, 0, 2, 0,            /* 0x2f */
  397                 {{0, 3},
  398                 {5, 5},
  399                 {0, 0},
  400                 {0, 0}
  401                 }
  402         },
  403         {0, 0, 1, 0,            /* 0x30 */
  404                 {{4, 5},
  405                 {0, 0},
  406                 {0, 0},
  407                 {0, 0}
  408                 }
  409         },
  410         {1, 0, 2, 0,            /* 0x31 */
  411                 {{0, 0},
  412                 {4, 5},
  413                 {0, 0},
  414                 {0, 0}
  415                 }
  416         },
  417         {0, 0, 2, 0,            /* 0x32 */
  418                 {{1, 1},
  419                 {4, 5},
  420                 {0, 0},
  421                 {0, 0}
  422                 }
  423         },
  424         {1, 0, 2, 0,            /* 0x33 */
  425                 {{0, 1},
  426                 {4, 5},
  427                 {0, 0},
  428                 {0, 0}
  429                 }
  430         },
  431         {0, 0, 2, 0,            /* 0x34 */
  432                 {{2, 2},
  433                 {4, 5},
  434                 {0, 0},
  435                 {0, 0}
  436                 }
  437         },
  438         {1, 0, 3, 0,            /* 0x35 */
  439                 {{0, 0},
  440                 {2, 2},
  441                 {4, 5},
  442                 {0, 0}
  443                 }
  444         },
  445         {0, 0, 2, 0,            /* 0x36 */
  446                 {{1, 2},
  447                 {4, 5},
  448                 {0, 0},
  449                 {0, 0}
  450                 }
  451         },
  452         {1, 0, 2, 0,            /* 0x37 */
  453                 {{0, 2},
  454                 {4, 5},
  455                 {0, 0},
  456                 {0, 0}
  457                 }
  458         },
  459         {0, 0, 1, 0,            /* 0x38 */
  460                 {{3, 5},
  461                 {0, 0},
  462                 {0, 0},
  463                 {0, 0}
  464                 }
  465         },
  466         {1, 0, 2, 0,            /* 0x39 */
  467                 {{0, 0},
  468                 {3, 5},
  469                 {0, 0},
  470                 {0, 0}
  471                 }
  472         },
  473         {0, 0, 2, 0,            /* 0x3a */
  474                 {{1, 1},
  475                 {3, 5},
  476                 {0, 0},
  477                 {0, 0}
  478                 }
  479         },
  480         {1, 0, 2, 0,            /* 0x3b */
  481                 {{0, 1},
  482                 {3, 5},
  483                 {0, 0},
  484                 {0, 0}
  485                 }
  486         },
  487         {0, 0, 1, 0,            /* 0x3c */
  488                 {{2, 5},
  489                 {0, 0},
  490                 {0, 0},
  491                 {0, 0}
  492                 }
  493         },
  494         {1, 0, 2, 0,            /* 0x3d */
  495                 {{0, 0},
  496                 {2, 5},
  497                 {0, 0},
  498                 {0, 0}
  499                 }
  500         },
  501         {0, 0, 1, 0,            /* 0x3e */
  502                 {{1, 5},
  503                 {0, 0},
  504                 {0, 0},
  505                 {0, 0}
  506                 }
  507         },
  508         {1, 0, 1, 0,            /* 0x3f */
  509                 {{0, 5},
  510                 {0, 0},
  511                 {0, 0},
  512                 {0, 0}
  513                 }
  514         },
  515         {0, 0, 1, 0,            /* 0x40 */
  516                 {{6, 6},
  517                 {0, 0},
  518                 {0, 0},
  519                 {0, 0}
  520                 }
  521         },
  522         {1, 0, 2, 0,            /* 0x41 */
  523                 {{0, 0},
  524                 {6, 6},
  525                 {0, 0},
  526                 {0, 0}
  527                 }
  528         },
  529         {0, 0, 2, 0,            /* 0x42 */
  530                 {{1, 1},
  531                 {6, 6},
  532                 {0, 0},
  533                 {0, 0}
  534                 }
  535         },
  536         {1, 0, 2, 0,            /* 0x43 */
  537                 {{0, 1},
  538                 {6, 6},
  539                 {0, 0},
  540                 {0, 0}
  541                 }
  542         },
  543         {0, 0, 2, 0,            /* 0x44 */
  544                 {{2, 2},
  545                 {6, 6},
  546                 {0, 0},
  547                 {0, 0}
  548                 }
  549         },
  550         {1, 0, 3, 0,            /* 0x45 */
  551                 {{0, 0},
  552                 {2, 2},
  553                 {6, 6},
  554                 {0, 0}
  555                 }
  556         },
  557         {0, 0, 2, 0,            /* 0x46 */
  558                 {{1, 2},
  559                 {6, 6},
  560                 {0, 0},
  561                 {0, 0}
  562                 }
  563         },
  564         {1, 0, 2, 0,            /* 0x47 */
  565                 {{0, 2},
  566                 {6, 6},
  567                 {0, 0},
  568                 {0, 0}
  569                 }
  570         },
  571         {0, 0, 2, 0,            /* 0x48 */
  572                 {{3, 3},
  573                 {6, 6},
  574                 {0, 0},
  575                 {0, 0}
  576                 }
  577         },
  578         {1, 0, 3, 0,            /* 0x49 */
  579                 {{0, 0},
  580                 {3, 3},
  581                 {6, 6},
  582                 {0, 0}
  583                 }
  584         },
  585         {0, 0, 3, 0,            /* 0x4a */
  586                 {{1, 1},
  587                 {3, 3},
  588                 {6, 6},
  589                 {0, 0}
  590                 }
  591         },
  592         {1, 0, 3, 0,            /* 0x4b */
  593                 {{0, 1},
  594                 {3, 3},
  595                 {6, 6},
  596                 {0, 0}
  597                 }
  598         },
  599         {0, 0, 2, 0,            /* 0x4c */
  600                 {{2, 3},
  601                 {6, 6},
  602                 {0, 0},
  603                 {0, 0}
  604                 }
  605         },
  606         {1, 0, 3, 0,            /* 0x4d */
  607                 {{0, 0},
  608                 {2, 3},
  609                 {6, 6},
  610                 {0, 0}
  611                 }
  612         },
  613         {0, 0, 2, 0,            /* 0x4e */
  614                 {{1, 3},
  615                 {6, 6},
  616                 {0, 0},
  617                 {0, 0}
  618                 }
  619         },
  620         {1, 0, 2, 0,            /* 0x4f */
  621                 {{0, 3},
  622                 {6, 6},
  623                 {0, 0},
  624                 {0, 0}
  625                 }
  626         },
  627         {0, 0, 2, 0,            /* 0x50 */
  628                 {{4, 4},
  629                 {6, 6},
  630                 {0, 0},
  631                 {0, 0}
  632                 }
  633         },
  634         {1, 0, 3, 0,            /* 0x51 */
  635                 {{0, 0},
  636                 {4, 4},
  637                 {6, 6},
  638                 {0, 0}
  639                 }
  640         },
  641         {0, 0, 3, 0,            /* 0x52 */
  642                 {{1, 1},
  643                 {4, 4},
  644                 {6, 6},
  645                 {0, 0}
  646                 }
  647         },
  648         {1, 0, 3, 0,            /* 0x53 */
  649                 {{0, 1},
  650                 {4, 4},
  651                 {6, 6},
  652                 {0, 0}
  653                 }
  654         },
  655         {0, 0, 3, 0,            /* 0x54 */
  656                 {{2, 2},
  657                 {4, 4},
  658                 {6, 6},
  659                 {0, 0}
  660                 }
  661         },
  662         {1, 0, 4, 0,            /* 0x55 */
  663                 {{0, 0},
  664                 {2, 2},
  665                 {4, 4},
  666                 {6, 6}
  667                 }
  668         },
  669         {0, 0, 3, 0,            /* 0x56 */
  670                 {{1, 2},
  671                 {4, 4},
  672                 {6, 6},
  673                 {0, 0}
  674                 }
  675         },
  676         {1, 0, 3, 0,            /* 0x57 */
  677                 {{0, 2},
  678                 {4, 4},
  679                 {6, 6},
  680                 {0, 0}
  681                 }
  682         },
  683         {0, 0, 2, 0,            /* 0x58 */
  684                 {{3, 4},
  685                 {6, 6},
  686                 {0, 0},
  687                 {0, 0}
  688                 }
  689         },
  690         {1, 0, 3, 0,            /* 0x59 */
  691                 {{0, 0},
  692                 {3, 4},
  693                 {6, 6},
  694                 {0, 0}
  695                 }
  696         },
  697         {0, 0, 3, 0,            /* 0x5a */
  698                 {{1, 1},
  699                 {3, 4},
  700                 {6, 6},
  701                 {0, 0}
  702                 }
  703         },
  704         {1, 0, 3, 0,            /* 0x5b */
  705                 {{0, 1},
  706                 {3, 4},
  707                 {6, 6},
  708                 {0, 0}
  709                 }
  710         },
  711         {0, 0, 2, 0,            /* 0x5c */
  712                 {{2, 4},
  713                 {6, 6},
  714                 {0, 0},
  715                 {0, 0}
  716                 }
  717         },
  718         {1, 0, 3, 0,            /* 0x5d */
  719                 {{0, 0},
  720                 {2, 4},
  721                 {6, 6},
  722                 {0, 0}
  723                 }
  724         },
  725         {0, 0, 2, 0,            /* 0x5e */
  726                 {{1, 4},
  727                 {6, 6},
  728                 {0, 0},
  729                 {0, 0}
  730                 }
  731         },
  732         {1, 0, 2, 0,            /* 0x5f */
  733                 {{0, 4},
  734                 {6, 6},
  735                 {0, 0},
  736                 {0, 0}
  737                 }
  738         },
  739         {0, 0, 1, 0,            /* 0x60 */
  740                 {{5, 6},
  741                 {0, 0},
  742                 {0, 0},
  743                 {0, 0}
  744                 }
  745         },
  746         {1, 0, 2, 0,            /* 0x61 */
  747                 {{0, 0},
  748                 {5, 6},
  749                 {0, 0},
  750                 {0, 0}
  751                 }
  752         },
  753         {0, 0, 2, 0,            /* 0x62 */
  754                 {{1, 1},
  755                 {5, 6},
  756                 {0, 0},
  757                 {0, 0}
  758                 }
  759         },
  760         {1, 0, 2, 0,            /* 0x63 */
  761                 {{0, 1},
  762                 {5, 6},
  763                 {0, 0},
  764                 {0, 0}
  765                 }
  766         },
  767         {0, 0, 2, 0,            /* 0x64 */
  768                 {{2, 2},
  769                 {5, 6},
  770                 {0, 0},
  771                 {0, 0}
  772                 }
  773         },
  774         {1, 0, 3, 0,            /* 0x65 */
  775                 {{0, 0},
  776                 {2, 2},
  777                 {5, 6},
  778                 {0, 0}
  779                 }
  780         },
  781         {0, 0, 2, 0,            /* 0x66 */
  782                 {{1, 2},
  783                 {5, 6},
  784                 {0, 0},
  785                 {0, 0}
  786                 }
  787         },
  788         {1, 0, 2, 0,            /* 0x67 */
  789                 {{0, 2},
  790                 {5, 6},
  791                 {0, 0},
  792                 {0, 0}
  793                 }
  794         },
  795         {0, 0, 2, 0,            /* 0x68 */
  796                 {{3, 3},
  797                 {5, 6},
  798                 {0, 0},
  799                 {0, 0}
  800                 }
  801         },
  802         {1, 0, 3, 0,            /* 0x69 */
  803                 {{0, 0},
  804                 {3, 3},
  805                 {5, 6},
  806                 {0, 0}
  807                 }
  808         },
  809         {0, 0, 3, 0,            /* 0x6a */
  810                 {{1, 1},
  811                 {3, 3},
  812                 {5, 6},
  813                 {0, 0}
  814                 }
  815         },
  816         {1, 0, 3, 0,            /* 0x6b */
  817                 {{0, 1},
  818                 {3, 3},
  819                 {5, 6},
  820                 {0, 0}
  821                 }
  822         },
  823         {0, 0, 2, 0,            /* 0x6c */
  824                 {{2, 3},
  825                 {5, 6},
  826                 {0, 0},
  827                 {0, 0}
  828                 }
  829         },
  830         {1, 0, 3, 0,            /* 0x6d */
  831                 {{0, 0},
  832                 {2, 3},
  833                 {5, 6},
  834                 {0, 0}
  835                 }
  836         },
  837         {0, 0, 2, 0,            /* 0x6e */
  838                 {{1, 3},
  839                 {5, 6},
  840                 {0, 0},
  841                 {0, 0}
  842                 }
  843         },
  844         {1, 0, 2, 0,            /* 0x6f */
  845                 {{0, 3},
  846                 {5, 6},
  847                 {0, 0},
  848                 {0, 0}
  849                 }
  850         },
  851         {0, 0, 1, 0,            /* 0x70 */
  852                 {{4, 6},
  853                 {0, 0},
  854                 {0, 0},
  855                 {0, 0}
  856                 }
  857         },
  858         {1, 0, 2, 0,            /* 0x71 */
  859                 {{0, 0},
  860                 {4, 6},
  861                 {0, 0},
  862                 {0, 0}
  863                 }
  864         },
  865         {0, 0, 2, 0,            /* 0x72 */
  866                 {{1, 1},
  867                 {4, 6},
  868                 {0, 0},
  869                 {0, 0}
  870                 }
  871         },
  872         {1, 0, 2, 0,            /* 0x73 */
  873                 {{0, 1},
  874                 {4, 6},
  875                 {0, 0},
  876                 {0, 0}
  877                 }
  878         },
  879         {0, 0, 2, 0,            /* 0x74 */
  880                 {{2, 2},
  881                 {4, 6},
  882                 {0, 0},
  883                 {0, 0}
  884                 }
  885         },
  886         {1, 0, 3, 0,            /* 0x75 */
  887                 {{0, 0},
  888                 {2, 2},
  889                 {4, 6},
  890                 {0, 0}
  891                 }
  892         },
  893         {0, 0, 2, 0,            /* 0x76 */
  894                 {{1, 2},
  895                 {4, 6},
  896                 {0, 0},
  897                 {0, 0}
  898                 }
  899         },
  900         {1, 0, 2, 0,            /* 0x77 */
  901                 {{0, 2},
  902                 {4, 6},
  903                 {0, 0},
  904                 {0, 0}
  905                 }
  906         },
  907         {0, 0, 1, 0,            /* 0x78 */
  908                 {{3, 6},
  909                 {0, 0},
  910                 {0, 0},
  911                 {0, 0}
  912                 }
  913         },
  914         {1, 0, 2, 0,            /* 0x79 */
  915                 {{0, 0},
  916                 {3, 6},
  917                 {0, 0},
  918                 {0, 0}
  919                 }
  920         },
  921         {0, 0, 2, 0,            /* 0x7a */
  922                 {{1, 1},
  923                 {3, 6},
  924                 {0, 0},
  925                 {0, 0}
  926                 }
  927         },
  928         {1, 0, 2, 0,            /* 0x7b */
  929                 {{0, 1},
  930                 {3, 6},
  931                 {0, 0},
  932                 {0, 0}
  933                 }
  934         },
  935         {0, 0, 1, 0,            /* 0x7c */
  936                 {{2, 6},
  937                 {0, 0},
  938                 {0, 0},
  939                 {0, 0}
  940                 }
  941         },
  942         {1, 0, 2, 0,            /* 0x7d */
  943                 {{0, 0},
  944                 {2, 6},
  945                 {0, 0},
  946                 {0, 0}
  947                 }
  948         },
  949         {0, 0, 1, 0,            /* 0x7e */
  950                 {{1, 6},
  951                 {0, 0},
  952                 {0, 0},
  953                 {0, 0}
  954                 }
  955         },
  956         {1, 0, 1, 0,            /* 0x7f */
  957                 {{0, 6},
  958                 {0, 0},
  959                 {0, 0},
  960                 {0, 0}
  961                 }
  962         },
  963         {0, 1, 1, 0,            /* 0x80 */
  964                 {{7, 7},
  965                 {0, 0},
  966                 {0, 0},
  967                 {0, 0}
  968                 }
  969         },
  970         {1, 1, 2, 0,            /* 0x81 */
  971                 {{0, 0},
  972                 {7, 7},
  973                 {0, 0},
  974                 {0, 0}
  975                 }
  976         },
  977         {0, 1, 2, 0,            /* 0x82 */
  978                 {{1, 1},
  979                 {7, 7},
  980                 {0, 0},
  981                 {0, 0}
  982                 }
  983         },
  984         {1, 1, 2, 0,            /* 0x83 */
  985                 {{0, 1},
  986                 {7, 7},
  987                 {0, 0},
  988                 {0, 0}
  989                 }
  990         },
  991         {0, 1, 2, 0,            /* 0x84 */
  992                 {{2, 2},
  993                 {7, 7},
  994                 {0, 0},
  995                 {0, 0}
  996                 }
  997         },
  998         {1, 1, 3, 0,            /* 0x85 */
  999                 {{0, 0},
 1000                 {2, 2},
 1001                 {7, 7},
 1002                 {0, 0}
 1003                 }
 1004         },
 1005         {0, 1, 2, 0,            /* 0x86 */
 1006                 {{1, 2},
 1007                 {7, 7},
 1008                 {0, 0},
 1009                 {0, 0}
 1010                 }
 1011         },
 1012         {1, 1, 2, 0,            /* 0x87 */
 1013                 {{0, 2},
 1014                 {7, 7},
 1015                 {0, 0},
 1016                 {0, 0}
 1017                 }
 1018         },
 1019         {0, 1, 2, 0,            /* 0x88 */
 1020                 {{3, 3},
 1021                 {7, 7},
 1022                 {0, 0},
 1023                 {0, 0}
 1024                 }
 1025         },
 1026         {1, 1, 3, 0,            /* 0x89 */
 1027                 {{0, 0},
 1028                 {3, 3},
 1029                 {7, 7},
 1030                 {0, 0}
 1031                 }
 1032         },
 1033         {0, 1, 3, 0,            /* 0x8a */
 1034                 {{1, 1},
 1035                 {3, 3},
 1036                 {7, 7},
 1037                 {0, 0}
 1038                 }
 1039         },
 1040         {1, 1, 3, 0,            /* 0x8b */
 1041                 {{0, 1},
 1042                 {3, 3},
 1043                 {7, 7},
 1044                 {0, 0}
 1045                 }
 1046         },
 1047         {0, 1, 2, 0,            /* 0x8c */
 1048                 {{2, 3},
 1049                 {7, 7},
 1050                 {0, 0},
 1051                 {0, 0}
 1052                 }
 1053         },
 1054         {1, 1, 3, 0,            /* 0x8d */
 1055                 {{0, 0},
 1056                 {2, 3},
 1057                 {7, 7},
 1058                 {0, 0}
 1059                 }
 1060         },
 1061         {0, 1, 2, 0,            /* 0x8e */
 1062                 {{1, 3},
 1063                 {7, 7},
 1064                 {0, 0},
 1065                 {0, 0}
 1066                 }
 1067         },
 1068         {1, 1, 2, 0,            /* 0x8f */
 1069                 {{0, 3},
 1070                 {7, 7},
 1071                 {0, 0},
 1072                 {0, 0}
 1073                 }
 1074         },
 1075         {0, 1, 2, 0,            /* 0x90 */
 1076                 {{4, 4},
 1077                 {7, 7},
 1078                 {0, 0},
 1079                 {0, 0}
 1080                 }
 1081         },
 1082         {1, 1, 3, 0,            /* 0x91 */
 1083                 {{0, 0},
 1084                 {4, 4},
 1085                 {7, 7},
 1086                 {0, 0}
 1087                 }
 1088         },
 1089         {0, 1, 3, 0,            /* 0x92 */
 1090                 {{1, 1},
 1091                 {4, 4},
 1092                 {7, 7},
 1093                 {0, 0}
 1094                 }
 1095         },
 1096         {1, 1, 3, 0,            /* 0x93 */
 1097                 {{0, 1},
 1098                 {4, 4},
 1099                 {7, 7},
 1100                 {0, 0}
 1101                 }
 1102         },
 1103         {0, 1, 3, 0,            /* 0x94 */
 1104                 {{2, 2},
 1105                 {4, 4},
 1106                 {7, 7},
 1107                 {0, 0}
 1108                 }
 1109         },
 1110         {1, 1, 4, 0,            /* 0x95 */
 1111                 {{0, 0},
 1112                 {2, 2},
 1113                 {4, 4},
 1114                 {7, 7}
 1115                 }
 1116         },
 1117         {0, 1, 3, 0,            /* 0x96 */
 1118                 {{1, 2},
 1119                 {4, 4},
 1120                 {7, 7},
 1121                 {0, 0}
 1122                 }
 1123         },
 1124         {1, 1, 3, 0,            /* 0x97 */
 1125                 {{0, 2},
 1126                 {4, 4},
 1127                 {7, 7},
 1128                 {0, 0}
 1129                 }
 1130         },
 1131         {0, 1, 2, 0,            /* 0x98 */
 1132                 {{3, 4},
 1133                 {7, 7},
 1134                 {0, 0},
 1135                 {0, 0}
 1136                 }
 1137         },
 1138         {1, 1, 3, 0,            /* 0x99 */
 1139                 {{0, 0},
 1140                 {3, 4},
 1141                 {7, 7},
 1142                 {0, 0}
 1143                 }
 1144         },
 1145         {0, 1, 3, 0,            /* 0x9a */
 1146                 {{1, 1},
 1147                 {3, 4},
 1148                 {7, 7},
 1149                 {0, 0}
 1150                 }
 1151         },
 1152         {1, 1, 3, 0,            /* 0x9b */
 1153                 {{0, 1},
 1154                 {3, 4},
 1155                 {7, 7},
 1156                 {0, 0}
 1157                 }
 1158         },
 1159         {0, 1, 2, 0,            /* 0x9c */
 1160                 {{2, 4},
 1161                 {7, 7},
 1162                 {0, 0},
 1163                 {0, 0}
 1164                 }
 1165         },
 1166         {1, 1, 3, 0,            /* 0x9d */
 1167                 {{0, 0},
 1168                 {2, 4},
 1169                 {7, 7},
 1170                 {0, 0}
 1171                 }
 1172         },
 1173         {0, 1, 2, 0,            /* 0x9e */
 1174                 {{1, 4},
 1175                 {7, 7},
 1176                 {0, 0},
 1177                 {0, 0}
 1178                 }
 1179         },
 1180         {1, 1, 2, 0,            /* 0x9f */
 1181                 {{0, 4},
 1182                 {7, 7},
 1183                 {0, 0},
 1184                 {0, 0}
 1185                 }
 1186         },
 1187         {0, 1, 2, 0,            /* 0xa0 */
 1188                 {{5, 5},
 1189                 {7, 7},
 1190                 {0, 0},
 1191                 {0, 0}
 1192                 }
 1193         },
 1194         {1, 1, 3, 0,            /* 0xa1 */
 1195                 {{0, 0},
 1196                 {5, 5},
 1197                 {7, 7},
 1198                 {0, 0}
 1199                 }
 1200         },
 1201         {0, 1, 3, 0,            /* 0xa2 */
 1202                 {{1, 1},
 1203                 {5, 5},
 1204                 {7, 7},
 1205                 {0, 0}
 1206                 }
 1207         },
 1208         {1, 1, 3, 0,            /* 0xa3 */
 1209                 {{0, 1},
 1210                 {5, 5},
 1211                 {7, 7},
 1212                 {0, 0}
 1213                 }
 1214         },
 1215         {0, 1, 3, 0,            /* 0xa4 */
 1216                 {{2, 2},
 1217                 {5, 5},
 1218                 {7, 7},
 1219                 {0, 0}
 1220                 }
 1221         },
 1222         {1, 1, 4, 0,            /* 0xa5 */
 1223                 {{0, 0},
 1224                 {2, 2},
 1225                 {5, 5},
 1226                 {7, 7}
 1227                 }
 1228         },
 1229         {0, 1, 3, 0,            /* 0xa6 */
 1230                 {{1, 2},
 1231                 {5, 5},
 1232                 {7, 7},
 1233                 {0, 0}
 1234                 }
 1235         },
 1236         {1, 1, 3, 0,            /* 0xa7 */
 1237                 {{0, 2},
 1238                 {5, 5},
 1239                 {7, 7},
 1240                 {0, 0}
 1241                 }
 1242         },
 1243         {0, 1, 3, 0,            /* 0xa8 */
 1244                 {{3, 3},
 1245                 {5, 5},
 1246                 {7, 7},
 1247                 {0, 0}
 1248                 }
 1249         },
 1250         {1, 1, 4, 0,            /* 0xa9 */
 1251                 {{0, 0},
 1252                 {3, 3},
 1253                 {5, 5},
 1254                 {7, 7}
 1255                 }
 1256         },
 1257         {0, 1, 4, 0,            /* 0xaa */
 1258                 {{1, 1},
 1259                 {3, 3},
 1260                 {5, 5},
 1261                 {7, 7}
 1262                 }
 1263         },
 1264         {1, 1, 4, 0,            /* 0xab */
 1265                 {{0, 1},
 1266                 {3, 3},
 1267                 {5, 5},
 1268                 {7, 7}
 1269                 }
 1270         },
 1271         {0, 1, 3, 0,            /* 0xac */
 1272                 {{2, 3},
 1273                 {5, 5},
 1274                 {7, 7},
 1275                 {0, 0}
 1276                 }
 1277         },
 1278         {1, 1, 4, 0,            /* 0xad */
 1279                 {{0, 0},
 1280                 {2, 3},
 1281                 {5, 5},
 1282                 {7, 7}
 1283                 }
 1284         },
 1285         {0, 1, 3, 0,            /* 0xae */
 1286                 {{1, 3},
 1287                 {5, 5},
 1288                 {7, 7},
 1289                 {0, 0}
 1290                 }
 1291         },
 1292         {1, 1, 3, 0,            /* 0xaf */
 1293                 {{0, 3},
 1294                 {5, 5},
 1295                 {7, 7},
 1296                 {0, 0}
 1297                 }
 1298         },
 1299         {0, 1, 2, 0,            /* 0xb0 */
 1300                 {{4, 5},
 1301                 {7, 7},
 1302                 {0, 0},
 1303                 {0, 0}
 1304                 }
 1305         },
 1306         {1, 1, 3, 0,            /* 0xb1 */
 1307                 {{0, 0},
 1308                 {4, 5},
 1309                 {7, 7},
 1310                 {0, 0}
 1311                 }
 1312         },
 1313         {0, 1, 3, 0,            /* 0xb2 */
 1314                 {{1, 1},
 1315                 {4, 5},
 1316                 {7, 7},
 1317                 {0, 0}
 1318                 }
 1319         },
 1320         {1, 1, 3, 0,            /* 0xb3 */
 1321                 {{0, 1},
 1322                 {4, 5},
 1323                 {7, 7},
 1324                 {0, 0}
 1325                 }
 1326         },
 1327         {0, 1, 3, 0,            /* 0xb4 */
 1328                 {{2, 2},
 1329                 {4, 5},
 1330                 {7, 7},
 1331                 {0, 0}
 1332                 }
 1333         },
 1334         {1, 1, 4, 0,            /* 0xb5 */
 1335                 {{0, 0},
 1336                 {2, 2},
 1337                 {4, 5},
 1338                 {7, 7}
 1339                 }
 1340         },
 1341         {0, 1, 3, 0,            /* 0xb6 */
 1342                 {{1, 2},
 1343                 {4, 5},
 1344                 {7, 7},
 1345                 {0, 0}
 1346                 }
 1347         },
 1348         {1, 1, 3, 0,            /* 0xb7 */
 1349                 {{0, 2},
 1350                 {4, 5},
 1351                 {7, 7},
 1352                 {0, 0}
 1353                 }
 1354         },
 1355         {0, 1, 2, 0,            /* 0xb8 */
 1356                 {{3, 5},
 1357                 {7, 7},
 1358                 {0, 0},
 1359                 {0, 0}
 1360                 }
 1361         },
 1362         {1, 1, 3, 0,            /* 0xb9 */
 1363                 {{0, 0},
 1364                 {3, 5},
 1365                 {7, 7},
 1366                 {0, 0}
 1367                 }
 1368         },
 1369         {0, 1, 3, 0,            /* 0xba */
 1370                 {{1, 1},
 1371                 {3, 5},
 1372                 {7, 7},
 1373                 {0, 0}
 1374                 }
 1375         },
 1376         {1, 1, 3, 0,            /* 0xbb */
 1377                 {{0, 1},
 1378                 {3, 5},
 1379                 {7, 7},
 1380                 {0, 0}
 1381                 }
 1382         },
 1383         {0, 1, 2, 0,            /* 0xbc */
 1384                 {{2, 5},
 1385                 {7, 7},
 1386                 {0, 0},
 1387                 {0, 0}
 1388                 }
 1389         },
 1390         {1, 1, 3, 0,            /* 0xbd */
 1391                 {{0, 0},
 1392                 {2, 5},
 1393                 {7, 7},
 1394                 {0, 0}
 1395                 }
 1396         },
 1397         {0, 1, 2, 0,            /* 0xbe */
 1398                 {{1, 5},
 1399                 {7, 7},
 1400                 {0, 0},
 1401                 {0, 0}
 1402                 }
 1403         },
 1404         {1, 1, 2, 0,            /* 0xbf */
 1405                 {{0, 5},
 1406                 {7, 7},
 1407                 {0, 0},
 1408                 {0, 0}
 1409                 }
 1410         },
 1411         {0, 1, 1, 0,            /* 0xc0 */
 1412                 {{6, 7},
 1413                 {0, 0},
 1414                 {0, 0},
 1415                 {0, 0}
 1416                 }
 1417         },
 1418         {1, 1, 2, 0,            /* 0xc1 */
 1419                 {{0, 0},
 1420                 {6, 7},
 1421                 {0, 0},
 1422                 {0, 0}
 1423                 }
 1424         },
 1425         {0, 1, 2, 0,            /* 0xc2 */
 1426                 {{1, 1},
 1427                 {6, 7},
 1428                 {0, 0},
 1429                 {0, 0}
 1430                 }
 1431         },
 1432         {1, 1, 2, 0,            /* 0xc3 */
 1433                 {{0, 1},
 1434                 {6, 7},
 1435                 {0, 0},
 1436                 {0, 0}
 1437                 }
 1438         },
 1439         {0, 1, 2, 0,            /* 0xc4 */
 1440                 {{2, 2},
 1441                 {6, 7},
 1442                 {0, 0},
 1443                 {0, 0}
 1444                 }
 1445         },
 1446         {1, 1, 3, 0,            /* 0xc5 */
 1447                 {{0, 0},
 1448                 {2, 2},
 1449                 {6, 7},
 1450                 {0, 0}
 1451                 }
 1452         },
 1453         {0, 1, 2, 0,            /* 0xc6 */
 1454                 {{1, 2},
 1455                 {6, 7},
 1456                 {0, 0},
 1457                 {0, 0}
 1458                 }
 1459         },
 1460         {1, 1, 2, 0,            /* 0xc7 */
 1461                 {{0, 2},
 1462                 {6, 7},
 1463                 {0, 0},
 1464                 {0, 0}
 1465                 }
 1466         },
 1467         {0, 1, 2, 0,            /* 0xc8 */
 1468                 {{3, 3},
 1469                 {6, 7},
 1470                 {0, 0},
 1471                 {0, 0}
 1472                 }
 1473         },
 1474         {1, 1, 3, 0,            /* 0xc9 */
 1475                 {{0, 0},
 1476                 {3, 3},
 1477                 {6, 7},
 1478                 {0, 0}
 1479                 }
 1480         },
 1481         {0, 1, 3, 0,            /* 0xca */
 1482                 {{1, 1},
 1483                 {3, 3},
 1484                 {6, 7},
 1485                 {0, 0}
 1486                 }
 1487         },
 1488         {1, 1, 3, 0,            /* 0xcb */
 1489                 {{0, 1},
 1490                 {3, 3},
 1491                 {6, 7},
 1492                 {0, 0}
 1493                 }
 1494         },
 1495         {0, 1, 2, 0,            /* 0xcc */
 1496                 {{2, 3},
 1497                 {6, 7},
 1498                 {0, 0},
 1499                 {0, 0}
 1500                 }
 1501         },
 1502         {1, 1, 3, 0,            /* 0xcd */
 1503                 {{0, 0},
 1504                 {2, 3},
 1505                 {6, 7},
 1506                 {0, 0}
 1507                 }
 1508         },
 1509         {0, 1, 2, 0,            /* 0xce */
 1510                 {{1, 3},
 1511                 {6, 7},
 1512                 {0, 0},
 1513                 {0, 0}
 1514                 }
 1515         },
 1516         {1, 1, 2, 0,            /* 0xcf */
 1517                 {{0, 3},
 1518                 {6, 7},
 1519                 {0, 0},
 1520                 {0, 0}
 1521                 }
 1522         },
 1523         {0, 1, 2, 0,            /* 0xd0 */
 1524                 {{4, 4},
 1525                 {6, 7},
 1526                 {0, 0},
 1527                 {0, 0}
 1528                 }
 1529         },
 1530         {1, 1, 3, 0,            /* 0xd1 */
 1531                 {{0, 0},
 1532                 {4, 4},
 1533                 {6, 7},
 1534                 {0, 0}
 1535                 }
 1536         },
 1537         {0, 1, 3, 0,            /* 0xd2 */
 1538                 {{1, 1},
 1539                 {4, 4},
 1540                 {6, 7},
 1541                 {0, 0}
 1542                 }
 1543         },
 1544         {1, 1, 3, 0,            /* 0xd3 */
 1545                 {{0, 1},
 1546                 {4, 4},
 1547                 {6, 7},
 1548                 {0, 0}
 1549                 }
 1550         },
 1551         {0, 1, 3, 0,            /* 0xd4 */
 1552                 {{2, 2},
 1553                 {4, 4},
 1554                 {6, 7},
 1555                 {0, 0}
 1556                 }
 1557         },
 1558         {1, 1, 4, 0,            /* 0xd5 */
 1559                 {{0, 0},
 1560                 {2, 2},
 1561                 {4, 4},
 1562                 {6, 7}
 1563                 }
 1564         },
 1565         {0, 1, 3, 0,            /* 0xd6 */
 1566                 {{1, 2},
 1567                 {4, 4},
 1568                 {6, 7},
 1569                 {0, 0}
 1570                 }
 1571         },
 1572         {1, 1, 3, 0,            /* 0xd7 */
 1573                 {{0, 2},
 1574                 {4, 4},
 1575                 {6, 7},
 1576                 {0, 0}
 1577                 }
 1578         },
 1579         {0, 1, 2, 0,            /* 0xd8 */
 1580                 {{3, 4},
 1581                 {6, 7},
 1582                 {0, 0},
 1583                 {0, 0}
 1584                 }
 1585         },
 1586         {1, 1, 3, 0,            /* 0xd9 */
 1587                 {{0, 0},
 1588                 {3, 4},
 1589                 {6, 7},
 1590                 {0, 0}
 1591                 }
 1592         },
 1593         {0, 1, 3, 0,            /* 0xda */
 1594                 {{1, 1},
 1595                 {3, 4},
 1596                 {6, 7},
 1597                 {0, 0}
 1598                 }
 1599         },
 1600         {1, 1, 3, 0,            /* 0xdb */
 1601                 {{0, 1},
 1602                 {3, 4},
 1603                 {6, 7},
 1604                 {0, 0}
 1605                 }
 1606         },
 1607         {0, 1, 2, 0,            /* 0xdc */
 1608                 {{2, 4},
 1609                 {6, 7},
 1610                 {0, 0},
 1611                 {0, 0}
 1612                 }
 1613         },
 1614         {1, 1, 3, 0,            /* 0xdd */
 1615                 {{0, 0},
 1616                 {2, 4},
 1617                 {6, 7},
 1618                 {0, 0}
 1619                 }
 1620         },
 1621         {0, 1, 2, 0,            /* 0xde */
 1622                 {{1, 4},
 1623                 {6, 7},
 1624                 {0, 0},
 1625                 {0, 0}
 1626                 }
 1627         },
 1628         {1, 1, 2, 0,            /* 0xdf */
 1629                 {{0, 4},
 1630                 {6, 7},
 1631                 {0, 0},
 1632                 {0, 0}
 1633                 }
 1634         },
 1635         {0, 1, 1, 0,            /* 0xe0 */
 1636                 {{5, 7},
 1637                 {0, 0},
 1638                 {0, 0},
 1639                 {0, 0}
 1640                 }
 1641         },
 1642         {1, 1, 2, 0,            /* 0xe1 */
 1643                 {{0, 0},
 1644                 {5, 7},
 1645                 {0, 0},
 1646                 {0, 0}
 1647                 }
 1648         },
 1649         {0, 1, 2, 0,            /* 0xe2 */
 1650                 {{1, 1},
 1651                 {5, 7},
 1652                 {0, 0},
 1653                 {0, 0}
 1654                 }
 1655         },
 1656         {1, 1, 2, 0,            /* 0xe3 */
 1657                 {{0, 1},
 1658                 {5, 7},
 1659                 {0, 0},
 1660                 {0, 0}
 1661                 }
 1662         },
 1663         {0, 1, 2, 0,            /* 0xe4 */
 1664                 {{2, 2},
 1665                 {5, 7},
 1666                 {0, 0},
 1667                 {0, 0}
 1668                 }
 1669         },
 1670         {1, 1, 3, 0,            /* 0xe5 */
 1671                 {{0, 0},
 1672                 {2, 2},
 1673                 {5, 7},
 1674                 {0, 0}
 1675                 }
 1676         },
 1677         {0, 1, 2, 0,            /* 0xe6 */
 1678                 {{1, 2},
 1679                 {5, 7},
 1680                 {0, 0},
 1681                 {0, 0}
 1682                 }
 1683         },
 1684         {1, 1, 2, 0,            /* 0xe7 */
 1685                 {{0, 2},
 1686                 {5, 7},
 1687                 {0, 0},
 1688                 {0, 0}
 1689                 }
 1690         },
 1691         {0, 1, 2, 0,            /* 0xe8 */
 1692                 {{3, 3},
 1693                 {5, 7},
 1694                 {0, 0},
 1695                 {0, 0}
 1696                 }
 1697         },
 1698         {1, 1, 3, 0,            /* 0xe9 */
 1699                 {{0, 0},
 1700                 {3, 3},
 1701                 {5, 7},
 1702                 {0, 0}
 1703                 }
 1704         },
 1705         {0, 1, 3, 0,            /* 0xea */
 1706                 {{1, 1},
 1707                 {3, 3},
 1708                 {5, 7},
 1709                 {0, 0}
 1710                 }
 1711         },
 1712         {1, 1, 3, 0,            /* 0xeb */
 1713                 {{0, 1},
 1714                 {3, 3},
 1715                 {5, 7},
 1716                 {0, 0}
 1717                 }
 1718         },
 1719         {0, 1, 2, 0,            /* 0xec */
 1720                 {{2, 3},
 1721                 {5, 7},
 1722                 {0, 0},
 1723                 {0, 0}
 1724                 }
 1725         },
 1726         {1, 1, 3, 0,            /* 0xed */
 1727                 {{0, 0},
 1728                 {2, 3},
 1729                 {5, 7},
 1730                 {0, 0}
 1731                 }
 1732         },
 1733         {0, 1, 2, 0,            /* 0xee */
 1734                 {{1, 3},
 1735                 {5, 7},
 1736                 {0, 0},
 1737                 {0, 0}
 1738                 }
 1739         },
 1740         {1, 1, 2, 0,            /* 0xef */
 1741                 {{0, 3},
 1742                 {5, 7},
 1743                 {0, 0},
 1744                 {0, 0}
 1745                 }
 1746         },
 1747         {0, 1, 1, 0,            /* 0xf0 */
 1748                 {{4, 7},
 1749                 {0, 0},
 1750                 {0, 0},
 1751                 {0, 0}
 1752                 }
 1753         },
 1754         {1, 1, 2, 0,            /* 0xf1 */
 1755                 {{0, 0},
 1756                 {4, 7},
 1757                 {0, 0},
 1758                 {0, 0}
 1759                 }
 1760         },
 1761         {0, 1, 2, 0,            /* 0xf2 */
 1762                 {{1, 1},
 1763                 {4, 7},
 1764                 {0, 0},
 1765                 {0, 0}
 1766                 }
 1767         },
 1768         {1, 1, 2, 0,            /* 0xf3 */
 1769                 {{0, 1},
 1770                 {4, 7},
 1771                 {0, 0},
 1772                 {0, 0}
 1773                 }
 1774         },
 1775         {0, 1, 2, 0,            /* 0xf4 */
 1776                 {{2, 2},
 1777                 {4, 7},
 1778                 {0, 0},
 1779                 {0, 0}
 1780                 }
 1781         },
 1782         {1, 1, 3, 0,            /* 0xf5 */
 1783                 {{0, 0},
 1784                 {2, 2},
 1785                 {4, 7},
 1786                 {0, 0}
 1787                 }
 1788         },
 1789         {0, 1, 2, 0,            /* 0xf6 */
 1790                 {{1, 2},
 1791                 {4, 7},
 1792                 {0, 0},
 1793                 {0, 0}
 1794                 }
 1795         },
 1796         {1, 1, 2, 0,            /* 0xf7 */
 1797                 {{0, 2},
 1798                 {4, 7},
 1799                 {0, 0},
 1800                 {0, 0}
 1801                 }
 1802         },
 1803         {0, 1, 1, 0,            /* 0xf8 */
 1804                 {{3, 7},
 1805                 {0, 0},
 1806                 {0, 0},
 1807                 {0, 0}
 1808                 }
 1809         },
 1810         {1, 1, 2, 0,            /* 0xf9 */
 1811                 {{0, 0},
 1812                 {3, 7},
 1813                 {0, 0},
 1814                 {0, 0}
 1815                 }
 1816         },
 1817         {0, 1, 2, 0,            /* 0xfa */
 1818                 {{1, 1},
 1819                 {3, 7},
 1820                 {0, 0},
 1821                 {0, 0}
 1822                 }
 1823         },
 1824         {1, 1, 2, 0,            /* 0xfb */
 1825                 {{0, 1},
 1826                 {3, 7},
 1827                 {0, 0},
 1828                 {0, 0}
 1829                 }
 1830         },
 1831         {0, 1, 1, 0,            /* 0xfc */
 1832                 {{2, 7},
 1833                 {0, 0},
 1834                 {0, 0},
 1835                 {0, 0}
 1836                 }
 1837         },
 1838         {1, 1, 2, 0,            /* 0xfd */
 1839                 {{0, 0},
 1840                 {2, 7},
 1841                 {0, 0},
 1842                 {0, 0}
 1843                 }
 1844         },
 1845         {0, 1, 1, 0,            /* 0xfe */
 1846                 {{1, 7},
 1847                 {0, 0},
 1848                 {0, 0},
 1849                 {0, 0}
 1850                 }
 1851         },
 1852         {1, 1, 1, 0,            /* 0xff */
 1853                 {{0, 7},
 1854                 {0, 0},
 1855                 {0, 0},
 1856                 {0, 0}
 1857                 }
 1858         }
 1859 };
 1860 
 1861 /* EY  below are nr_sacks version of the preceeding two data structures, identical except their names */
 1862 #define SCTP_MAX_NR_GAPS_INARRAY 4
 1863 struct nr_sack_track {
 1864         uint8_t right_edge;     /* mergable on the right edge */
 1865         uint8_t left_edge;      /* mergable on the left edge */
 1866         uint8_t num_entries;
 1867         uint8_t spare;
 1868         struct sctp_nr_gap_ack_block nr_gaps[SCTP_MAX_NR_GAPS_INARRAY];
 1869 };
 1870 
 1871 struct nr_sack_track nr_sack_array[256] = {
 1872         {0, 0, 0, 0,            /* 0x00 */
 1873                 {{0, 0},
 1874                 {0, 0},
 1875                 {0, 0},
 1876                 {0, 0}
 1877                 }
 1878         },
 1879         {1, 0, 1, 0,            /* 0x01 */
 1880                 {{0, 0},
 1881                 {0, 0},
 1882                 {0, 0},
 1883                 {0, 0}
 1884                 }
 1885         },
 1886         {0, 0, 1, 0,            /* 0x02 */
 1887                 {{1, 1},
 1888                 {0, 0},
 1889                 {0, 0},
 1890                 {0, 0}
 1891                 }
 1892         },
 1893         {1, 0, 1, 0,            /* 0x03 */
 1894                 {{0, 1},
 1895                 {0, 0},
 1896                 {0, 0},
 1897                 {0, 0}
 1898                 }
 1899         },
 1900         {0, 0, 1, 0,            /* 0x04 */
 1901                 {{2, 2},
 1902                 {0, 0},
 1903                 {0, 0},
 1904                 {0, 0}
 1905                 }
 1906         },
 1907         {1, 0, 2, 0,            /* 0x05 */
 1908                 {{0, 0},
 1909                 {2, 2},
 1910                 {0, 0},
 1911                 {0, 0}
 1912                 }
 1913         },
 1914         {0, 0, 1, 0,            /* 0x06 */
 1915                 {{1, 2},
 1916                 {0, 0},
 1917                 {0, 0},
 1918                 {0, 0}
 1919                 }
 1920         },
 1921         {1, 0, 1, 0,            /* 0x07 */
 1922                 {{0, 2},
 1923                 {0, 0},
 1924                 {0, 0},
 1925                 {0, 0}
 1926                 }
 1927         },
 1928         {0, 0, 1, 0,            /* 0x08 */
 1929                 {{3, 3},
 1930                 {0, 0},
 1931                 {0, 0},
 1932                 {0, 0}
 1933                 }
 1934         },
 1935         {1, 0, 2, 0,            /* 0x09 */
 1936                 {{0, 0},
 1937                 {3, 3},
 1938                 {0, 0},
 1939                 {0, 0}
 1940                 }
 1941         },
 1942         {0, 0, 2, 0,            /* 0x0a */
 1943                 {{1, 1},
 1944                 {3, 3},
 1945                 {0, 0},
 1946                 {0, 0}
 1947                 }
 1948         },
 1949         {1, 0, 2, 0,            /* 0x0b */
 1950                 {{0, 1},
 1951                 {3, 3},
 1952                 {0, 0},
 1953                 {0, 0}
 1954                 }
 1955         },
 1956         {0, 0, 1, 0,            /* 0x0c */
 1957                 {{2, 3},
 1958                 {0, 0},
 1959                 {0, 0},
 1960                 {0, 0}
 1961                 }
 1962         },
 1963         {1, 0, 2, 0,            /* 0x0d */
 1964                 {{0, 0},
 1965                 {2, 3},
 1966                 {0, 0},
 1967                 {0, 0}
 1968                 }
 1969         },
 1970         {0, 0, 1, 0,            /* 0x0e */
 1971                 {{1, 3},
 1972                 {0, 0},
 1973                 {0, 0},
 1974                 {0, 0}
 1975                 }
 1976         },
 1977         {1, 0, 1, 0,            /* 0x0f */
 1978                 {{0, 3},
 1979                 {0, 0},
 1980                 {0, 0},
 1981                 {0, 0}
 1982                 }
 1983         },
 1984         {0, 0, 1, 0,            /* 0x10 */
 1985                 {{4, 4},
 1986                 {0, 0},
 1987                 {0, 0},
 1988                 {0, 0}
 1989                 }
 1990         },
 1991         {1, 0, 2, 0,            /* 0x11 */
 1992                 {{0, 0},
 1993                 {4, 4},
 1994                 {0, 0},
 1995                 {0, 0}
 1996                 }
 1997         },
 1998         {0, 0, 2, 0,            /* 0x12 */
 1999                 {{1, 1},
 2000                 {4, 4},
 2001                 {0, 0},
 2002                 {0, 0}
 2003                 }
 2004         },
 2005         {1, 0, 2, 0,            /* 0x13 */
 2006                 {{0, 1},
 2007                 {4, 4},
 2008                 {0, 0},
 2009                 {0, 0}
 2010                 }
 2011         },
 2012         {0, 0, 2, 0,            /* 0x14 */
 2013                 {{2, 2},
 2014                 {4, 4},
 2015                 {0, 0},
 2016                 {0, 0}
 2017                 }
 2018         },
 2019         {1, 0, 3, 0,            /* 0x15 */
 2020                 {{0, 0},
 2021                 {2, 2},
 2022                 {4, 4},
 2023                 {0, 0}
 2024                 }
 2025         },
 2026         {0, 0, 2, 0,            /* 0x16 */
 2027                 {{1, 2},
 2028                 {4, 4},
 2029                 {0, 0},
 2030                 {0, 0}
 2031                 }
 2032         },
 2033         {1, 0, 2, 0,            /* 0x17 */
 2034                 {{0, 2},
 2035                 {4, 4},
 2036                 {0, 0},
 2037                 {0, 0}
 2038                 }
 2039         },
 2040         {0, 0, 1, 0,            /* 0x18 */
 2041                 {{3, 4},
 2042                 {0, 0},
 2043                 {0, 0},
 2044                 {0, 0}
 2045                 }
 2046         },
 2047         {1, 0, 2, 0,            /* 0x19 */
 2048                 {{0, 0},
 2049                 {3, 4},
 2050                 {0, 0},
 2051                 {0, 0}
 2052                 }
 2053         },
 2054         {0, 0, 2, 0,            /* 0x1a */
 2055                 {{1, 1},
 2056                 {3, 4},
 2057                 {0, 0},
 2058                 {0, 0}
 2059                 }
 2060         },
 2061         {1, 0, 2, 0,            /* 0x1b */
 2062                 {{0, 1},
 2063                 {3, 4},
 2064                 {0, 0},
 2065                 {0, 0}
 2066                 }
 2067         },
 2068         {0, 0, 1, 0,            /* 0x1c */
 2069                 {{2, 4},
 2070                 {0, 0},
 2071                 {0, 0},
 2072                 {0, 0}
 2073                 }
 2074         },
 2075         {1, 0, 2, 0,            /* 0x1d */
 2076                 {{0, 0},
 2077                 {2, 4},
 2078                 {0, 0},
 2079                 {0, 0}
 2080                 }
 2081         },
 2082         {0, 0, 1, 0,            /* 0x1e */
 2083                 {{1, 4},
 2084                 {0, 0},
 2085                 {0, 0},
 2086                 {0, 0}
 2087                 }
 2088         },
 2089         {1, 0, 1, 0,            /* 0x1f */
 2090                 {{0, 4},
 2091                 {0, 0},
 2092                 {0, 0},
 2093                 {0, 0}
 2094                 }
 2095         },
 2096         {0, 0, 1, 0,            /* 0x20 */
 2097                 {{5, 5},
 2098                 {0, 0},
 2099                 {0, 0},
 2100                 {0, 0}
 2101                 }
 2102         },
 2103         {1, 0, 2, 0,            /* 0x21 */
 2104                 {{0, 0},
 2105                 {5, 5},
 2106                 {0, 0},
 2107                 {0, 0}
 2108                 }
 2109         },
 2110         {0, 0, 2, 0,            /* 0x22 */
 2111                 {{1, 1},
 2112                 {5, 5},
 2113                 {0, 0},
 2114                 {0, 0}
 2115                 }
 2116         },
 2117         {1, 0, 2, 0,            /* 0x23 */
 2118                 {{0, 1},
 2119                 {5, 5},
 2120                 {0, 0},
 2121                 {0, 0}
 2122                 }
 2123         },
 2124         {0, 0, 2, 0,            /* 0x24 */
 2125                 {{2, 2},
 2126                 {5, 5},
 2127                 {0, 0},
 2128                 {0, 0}
 2129                 }
 2130         },
 2131         {1, 0, 3, 0,            /* 0x25 */
 2132                 {{0, 0},
 2133                 {2, 2},
 2134                 {5, 5},
 2135                 {0, 0}
 2136                 }
 2137         },
 2138         {0, 0, 2, 0,            /* 0x26 */
 2139                 {{1, 2},
 2140                 {5, 5},
 2141                 {0, 0},
 2142                 {0, 0}
 2143                 }
 2144         },
 2145         {1, 0, 2, 0,            /* 0x27 */
 2146                 {{0, 2},
 2147                 {5, 5},
 2148                 {0, 0},
 2149                 {0, 0}
 2150                 }
 2151         },
 2152         {0, 0, 2, 0,            /* 0x28 */
 2153                 {{3, 3},
 2154                 {5, 5},
 2155                 {0, 0},
 2156                 {0, 0}
 2157                 }
 2158         },
 2159         {1, 0, 3, 0,            /* 0x29 */
 2160                 {{0, 0},
 2161                 {3, 3},
 2162                 {5, 5},
 2163                 {0, 0}
 2164                 }
 2165         },
 2166         {0, 0, 3, 0,            /* 0x2a */
 2167                 {{1, 1},
 2168                 {3, 3},
 2169                 {5, 5},
 2170                 {0, 0}
 2171                 }
 2172         },
 2173         {1, 0, 3, 0,            /* 0x2b */
 2174                 {{0, 1},
 2175                 {3, 3},
 2176                 {5, 5},
 2177                 {0, 0}
 2178                 }
 2179         },
 2180         {0, 0, 2, 0,            /* 0x2c */
 2181                 {{2, 3},
 2182                 {5, 5},
 2183                 {0, 0},
 2184                 {0, 0}
 2185                 }
 2186         },
 2187         {1, 0, 3, 0,            /* 0x2d */
 2188                 {{0, 0},
 2189                 {2, 3},
 2190                 {5, 5},
 2191                 {0, 0}
 2192                 }
 2193         },
 2194         {0, 0, 2, 0,            /* 0x2e */
 2195                 {{1, 3},
 2196                 {5, 5},
 2197                 {0, 0},
 2198                 {0, 0}
 2199                 }
 2200         },
 2201         {1, 0, 2, 0,            /* 0x2f */
 2202                 {{0, 3},
 2203                 {5, 5},
 2204                 {0, 0},
 2205                 {0, 0}
 2206                 }
 2207         },
 2208         {0, 0, 1, 0,            /* 0x30 */
 2209                 {{4, 5},
 2210                 {0, 0},
 2211                 {0, 0},
 2212                 {0, 0}
 2213                 }
 2214         },
 2215         {1, 0, 2, 0,            /* 0x31 */
 2216                 {{0, 0},
 2217                 {4, 5},
 2218                 {0, 0},
 2219                 {0, 0}
 2220                 }
 2221         },
 2222         {0, 0, 2, 0,            /* 0x32 */
 2223                 {{1, 1},
 2224                 {4, 5},
 2225                 {0, 0},
 2226                 {0, 0}
 2227                 }
 2228         },
 2229         {1, 0, 2, 0,            /* 0x33 */
 2230                 {{0, 1},
 2231                 {4, 5},
 2232                 {0, 0},
 2233                 {0, 0}
 2234                 }
 2235         },
 2236         {0, 0, 2, 0,            /* 0x34 */
 2237                 {{2, 2},
 2238                 {4, 5},
 2239                 {0, 0},
 2240                 {0, 0}
 2241                 }
 2242         },
 2243         {1, 0, 3, 0,            /* 0x35 */
 2244                 {{0, 0},
 2245                 {2, 2},
 2246                 {4, 5},
 2247                 {0, 0}
 2248                 }
 2249         },
 2250         {0, 0, 2, 0,            /* 0x36 */
 2251                 {{1, 2},
 2252                 {4, 5},
 2253                 {0, 0},
 2254                 {0, 0}
 2255                 }
 2256         },
 2257         {1, 0, 2, 0,            /* 0x37 */
 2258                 {{0, 2},
 2259                 {4, 5},
 2260                 {0, 0},
 2261                 {0, 0}
 2262                 }
 2263         },
 2264         {0, 0, 1, 0,            /* 0x38 */
 2265                 {{3, 5},
 2266                 {0, 0},
 2267                 {0, 0},
 2268                 {0, 0}
 2269                 }
 2270         },
 2271         {1, 0, 2, 0,            /* 0x39 */
 2272                 {{0, 0},
 2273                 {3, 5},
 2274                 {0, 0},
 2275                 {0, 0}
 2276                 }
 2277         },
 2278         {0, 0, 2, 0,            /* 0x3a */
 2279                 {{1, 1},
 2280                 {3, 5},
 2281                 {0, 0},
 2282                 {0, 0}
 2283                 }
 2284         },
 2285         {1, 0, 2, 0,            /* 0x3b */
 2286                 {{0, 1},
 2287                 {3, 5},
 2288                 {0, 0},
 2289                 {0, 0}
 2290                 }
 2291         },
 2292         {0, 0, 1, 0,            /* 0x3c */
 2293                 {{2, 5},
 2294                 {0, 0},
 2295                 {0, 0},
 2296                 {0, 0}
 2297                 }
 2298         },
 2299         {1, 0, 2, 0,            /* 0x3d */
 2300                 {{0, 0},
 2301                 {2, 5},
 2302                 {0, 0},
 2303                 {0, 0}
 2304                 }
 2305         },
 2306         {0, 0, 1, 0,            /* 0x3e */
 2307                 {{1, 5},
 2308                 {0, 0},
 2309                 {0, 0},
 2310                 {0, 0}
 2311                 }
 2312         },
 2313         {1, 0, 1, 0,            /* 0x3f */
 2314                 {{0, 5},
 2315                 {0, 0},
 2316                 {0, 0},
 2317                 {0, 0}
 2318                 }
 2319         },
 2320         {0, 0, 1, 0,            /* 0x40 */
 2321                 {{6, 6},
 2322                 {0, 0},
 2323                 {0, 0},
 2324                 {0, 0}
 2325                 }
 2326         },
 2327         {1, 0, 2, 0,            /* 0x41 */
 2328                 {{0, 0},
 2329                 {6, 6},
 2330                 {0, 0},
 2331                 {0, 0}
 2332                 }
 2333         },
 2334         {0, 0, 2, 0,            /* 0x42 */
 2335                 {{1, 1},
 2336                 {6, 6},
 2337                 {0, 0},
 2338                 {0, 0}
 2339                 }
 2340         },
 2341         {1, 0, 2, 0,            /* 0x43 */
 2342                 {{0, 1},
 2343                 {6, 6},
 2344                 {0, 0},
 2345                 {0, 0}
 2346                 }
 2347         },
 2348         {0, 0, 2, 0,            /* 0x44 */
 2349                 {{2, 2},
 2350                 {6, 6},
 2351                 {0, 0},
 2352                 {0, 0}
 2353                 }
 2354         },
 2355         {1, 0, 3, 0,            /* 0x45 */
 2356                 {{0, 0},
 2357                 {2, 2},
 2358                 {6, 6},
 2359                 {0, 0}
 2360                 }
 2361         },
 2362         {0, 0, 2, 0,            /* 0x46 */
 2363                 {{1, 2},
 2364                 {6, 6},
 2365                 {0, 0},
 2366                 {0, 0}
 2367                 }
 2368         },
 2369         {1, 0, 2, 0,            /* 0x47 */
 2370                 {{0, 2},
 2371                 {6, 6},
 2372                 {0, 0},
 2373                 {0, 0}
 2374                 }
 2375         },
 2376         {0, 0, 2, 0,            /* 0x48 */
 2377                 {{3, 3},
 2378                 {6, 6},
 2379                 {0, 0},
 2380                 {0, 0}
 2381                 }
 2382         },
 2383         {1, 0, 3, 0,            /* 0x49 */
 2384                 {{0, 0},
 2385                 {3, 3},
 2386                 {6, 6},
 2387                 {0, 0}
 2388                 }
 2389         },
 2390         {0, 0, 3, 0,            /* 0x4a */
 2391                 {{1, 1},
 2392                 {3, 3},
 2393                 {6, 6},
 2394                 {0, 0}
 2395                 }
 2396         },
 2397         {1, 0, 3, 0,            /* 0x4b */
 2398                 {{0, 1},
 2399                 {3, 3},
 2400                 {6, 6},
 2401                 {0, 0}
 2402                 }
 2403         },
 2404         {0, 0, 2, 0,            /* 0x4c */
 2405                 {{2, 3},
 2406                 {6, 6},
 2407                 {0, 0},
 2408                 {0, 0}
 2409                 }
 2410         },
 2411         {1, 0, 3, 0,            /* 0x4d */
 2412                 {{0, 0},
 2413                 {2, 3},
 2414                 {6, 6},
 2415                 {0, 0}
 2416                 }
 2417         },
 2418         {0, 0, 2, 0,            /* 0x4e */
 2419                 {{1, 3},
 2420                 {6, 6},
 2421                 {0, 0},
 2422                 {0, 0}
 2423                 }
 2424         },
 2425         {1, 0, 2, 0,            /* 0x4f */
 2426                 {{0, 3},
 2427                 {6, 6},
 2428                 {0, 0},
 2429                 {0, 0}
 2430                 }
 2431         },
 2432         {0, 0, 2, 0,            /* 0x50 */
 2433                 {{4, 4},
 2434                 {6, 6},
 2435                 {0, 0},
 2436                 {0, 0}
 2437                 }
 2438         },
 2439         {1, 0, 3, 0,            /* 0x51 */
 2440                 {{0, 0},
 2441                 {4, 4},
 2442                 {6, 6},
 2443                 {0, 0}
 2444                 }
 2445         },
 2446         {0, 0, 3, 0,            /* 0x52 */
 2447                 {{1, 1},
 2448                 {4, 4},
 2449                 {6, 6},
 2450                 {0, 0}
 2451                 }
 2452         },
 2453         {1, 0, 3, 0,            /* 0x53 */
 2454                 {{0, 1},
 2455                 {4, 4},
 2456                 {6, 6},
 2457                 {0, 0}
 2458                 }
 2459         },
 2460         {0, 0, 3, 0,            /* 0x54 */
 2461                 {{2, 2},
 2462                 {4, 4},
 2463                 {6, 6},
 2464                 {0, 0}
 2465                 }
 2466         },
 2467         {1, 0, 4, 0,            /* 0x55 */
 2468                 {{0, 0},
 2469                 {2, 2},
 2470                 {4, 4},
 2471                 {6, 6}
 2472                 }
 2473         },
 2474         {0, 0, 3, 0,            /* 0x56 */
 2475                 {{1, 2},
 2476                 {4, 4},
 2477                 {6, 6},
 2478                 {0, 0}
 2479                 }
 2480         },
 2481         {1, 0, 3, 0,            /* 0x57 */
 2482                 {{0, 2},
 2483                 {4, 4},
 2484                 {6, 6},
 2485                 {0, 0}
 2486                 }
 2487         },
 2488         {0, 0, 2, 0,            /* 0x58 */
 2489                 {{3, 4},
 2490                 {6, 6},
 2491                 {0, 0},
 2492                 {0, 0}
 2493                 }
 2494         },
 2495         {1, 0, 3, 0,            /* 0x59 */
 2496                 {{0, 0},
 2497                 {3, 4},
 2498                 {6, 6},
 2499                 {0, 0}
 2500                 }
 2501         },
 2502         {0, 0, 3, 0,            /* 0x5a */
 2503                 {{1, 1},
 2504                 {3, 4},
 2505                 {6, 6},
 2506                 {0, 0}
 2507                 }
 2508         },
 2509         {1, 0, 3, 0,            /* 0x5b */
 2510                 {{0, 1},
 2511                 {3, 4},
 2512                 {6, 6},
 2513                 {0, 0}
 2514                 }
 2515         },
 2516         {0, 0, 2, 0,            /* 0x5c */
 2517                 {{2, 4},
 2518                 {6, 6},
 2519                 {0, 0},
 2520                 {0, 0}
 2521                 }
 2522         },
 2523         {1, 0, 3, 0,            /* 0x5d */
 2524                 {{0, 0},
 2525                 {2, 4},
 2526                 {6, 6},
 2527                 {0, 0}
 2528                 }
 2529         },
 2530         {0, 0, 2, 0,            /* 0x5e */
 2531                 {{1, 4},
 2532                 {6, 6},
 2533                 {0, 0},
 2534                 {0, 0}
 2535                 }
 2536         },
 2537         {1, 0, 2, 0,            /* 0x5f */
 2538                 {{0, 4},
 2539                 {6, 6},
 2540                 {0, 0},
 2541                 {0, 0}
 2542                 }
 2543         },
 2544         {0, 0, 1, 0,            /* 0x60 */
 2545                 {{5, 6},
 2546                 {0, 0},
 2547                 {0, 0},
 2548                 {0, 0}
 2549                 }
 2550         },
 2551         {1, 0, 2, 0,            /* 0x61 */
 2552                 {{0, 0},
 2553                 {5, 6},
 2554                 {0, 0},
 2555                 {0, 0}
 2556                 }
 2557         },
 2558         {0, 0, 2, 0,            /* 0x62 */
 2559                 {{1, 1},
 2560                 {5, 6},
 2561                 {0, 0},
 2562                 {0, 0}
 2563                 }
 2564         },
 2565         {1, 0, 2, 0,            /* 0x63 */
 2566                 {{0, 1},
 2567                 {5, 6},
 2568                 {0, 0},
 2569                 {0, 0}
 2570                 }
 2571         },
 2572         {0, 0, 2, 0,            /* 0x64 */
 2573                 {{2, 2},
 2574                 {5, 6},
 2575                 {0, 0},
 2576                 {0, 0}
 2577                 }
 2578         },
 2579         {1, 0, 3, 0,            /* 0x65 */
 2580                 {{0, 0},
 2581                 {2, 2},
 2582                 {5, 6},
 2583                 {0, 0}
 2584                 }
 2585         },
 2586         {0, 0, 2, 0,            /* 0x66 */
 2587                 {{1, 2},
 2588                 {5, 6},
 2589                 {0, 0},
 2590                 {0, 0}
 2591                 }
 2592         },
 2593         {1, 0, 2, 0,            /* 0x67 */
 2594                 {{0, 2},
 2595                 {5, 6},
 2596                 {0, 0},
 2597                 {0, 0}
 2598                 }
 2599         },
 2600         {0, 0, 2, 0,            /* 0x68 */
 2601                 {{3, 3},
 2602                 {5, 6},
 2603                 {0, 0},
 2604                 {0, 0}
 2605                 }
 2606         },
 2607         {1, 0, 3, 0,            /* 0x69 */
 2608                 {{0, 0},
 2609                 {3, 3},
 2610                 {5, 6},
 2611                 {0, 0}
 2612                 }
 2613         },
 2614         {0, 0, 3, 0,            /* 0x6a */
 2615                 {{1, 1},
 2616                 {3, 3},
 2617                 {5, 6},
 2618                 {0, 0}
 2619                 }
 2620         },
 2621         {1, 0, 3, 0,            /* 0x6b */
 2622                 {{0, 1},
 2623                 {3, 3},
 2624                 {5, 6},
 2625                 {0, 0}
 2626                 }
 2627         },
 2628         {0, 0, 2, 0,            /* 0x6c */
 2629                 {{2, 3},
 2630                 {5, 6},
 2631                 {0, 0},
 2632                 {0, 0}
 2633                 }
 2634         },
 2635         {1, 0, 3, 0,            /* 0x6d */
 2636                 {{0, 0},
 2637                 {2, 3},
 2638                 {5, 6},
 2639                 {0, 0}
 2640                 }
 2641         },
 2642         {0, 0, 2, 0,            /* 0x6e */
 2643                 {{1, 3},
 2644                 {5, 6},
 2645                 {0, 0},
 2646                 {0, 0}
 2647                 }
 2648         },
 2649         {1, 0, 2, 0,            /* 0x6f */
 2650                 {{0, 3},
 2651                 {5, 6},
 2652                 {0, 0},
 2653                 {0, 0}
 2654                 }
 2655         },
 2656         {0, 0, 1, 0,            /* 0x70 */
 2657                 {{4, 6},
 2658                 {0, 0},
 2659                 {0, 0},
 2660                 {0, 0}
 2661                 }
 2662         },
 2663         {1, 0, 2, 0,            /* 0x71 */
 2664                 {{0, 0},
 2665                 {4, 6},
 2666                 {0, 0},
 2667                 {0, 0}
 2668                 }
 2669         },
 2670         {0, 0, 2, 0,            /* 0x72 */
 2671                 {{1, 1},
 2672                 {4, 6},
 2673                 {0, 0},
 2674                 {0, 0}
 2675                 }
 2676         },
 2677         {1, 0, 2, 0,            /* 0x73 */
 2678                 {{0, 1},
 2679                 {4, 6},
 2680                 {0, 0},
 2681                 {0, 0}
 2682                 }
 2683         },
 2684         {0, 0, 2, 0,            /* 0x74 */
 2685                 {{2, 2},
 2686                 {4, 6},
 2687                 {0, 0},
 2688                 {0, 0}
 2689                 }
 2690         },
 2691         {1, 0, 3, 0,            /* 0x75 */
 2692                 {{0, 0},
 2693                 {2, 2},
 2694                 {4, 6},
 2695                 {0, 0}
 2696                 }
 2697         },
 2698         {0, 0, 2, 0,            /* 0x76 */
 2699                 {{1, 2},
 2700                 {4, 6},
 2701                 {0, 0},
 2702                 {0, 0}
 2703                 }
 2704         },
 2705         {1, 0, 2, 0,            /* 0x77 */
 2706                 {{0, 2},
 2707                 {4, 6},
 2708                 {0, 0},
 2709                 {0, 0}
 2710                 }
 2711         },
 2712         {0, 0, 1, 0,            /* 0x78 */
 2713                 {{3, 6},
 2714                 {0, 0},
 2715                 {0, 0},
 2716                 {0, 0}
 2717                 }
 2718         },
 2719         {1, 0, 2, 0,            /* 0x79 */
 2720                 {{0, 0},
 2721                 {3, 6},
 2722                 {0, 0},
 2723                 {0, 0}
 2724                 }
 2725         },
 2726         {0, 0, 2, 0,            /* 0x7a */
 2727                 {{1, 1},
 2728                 {3, 6},
 2729                 {0, 0},
 2730                 {0, 0}
 2731                 }
 2732         },
 2733         {1, 0, 2, 0,            /* 0x7b */
 2734                 {{0, 1},
 2735                 {3, 6},
 2736                 {0, 0},
 2737                 {0, 0}
 2738                 }
 2739         },
 2740         {0, 0, 1, 0,            /* 0x7c */
 2741                 {{2, 6},
 2742                 {0, 0},
 2743                 {0, 0},
 2744                 {0, 0}
 2745                 }
 2746         },
 2747         {1, 0, 2, 0,            /* 0x7d */
 2748                 {{0, 0},
 2749                 {2, 6},
 2750                 {0, 0},
 2751                 {0, 0}
 2752                 }
 2753         },
 2754         {0, 0, 1, 0,            /* 0x7e */
 2755                 {{1, 6},
 2756                 {0, 0},
 2757                 {0, 0},
 2758                 {0, 0}
 2759                 }
 2760         },
 2761         {1, 0, 1, 0,            /* 0x7f */
 2762                 {{0, 6},
 2763                 {0, 0},
 2764                 {0, 0},
 2765                 {0, 0}
 2766                 }
 2767         },
 2768         {0, 1, 1, 0,            /* 0x80 */
 2769                 {{7, 7},
 2770                 {0, 0},
 2771                 {0, 0},
 2772                 {0, 0}
 2773                 }
 2774         },
 2775         {1, 1, 2, 0,            /* 0x81 */
 2776                 {{0, 0},
 2777                 {7, 7},
 2778                 {0, 0},
 2779                 {0, 0}
 2780                 }
 2781         },
 2782         {0, 1, 2, 0,            /* 0x82 */
 2783                 {{1, 1},
 2784                 {7, 7},
 2785                 {0, 0},
 2786                 {0, 0}
 2787                 }
 2788         },
 2789         {1, 1, 2, 0,            /* 0x83 */
 2790                 {{0, 1},
 2791                 {7, 7},
 2792                 {0, 0},
 2793                 {0, 0}
 2794                 }
 2795         },
 2796         {0, 1, 2, 0,            /* 0x84 */
 2797                 {{2, 2},
 2798                 {7, 7},
 2799                 {0, 0},
 2800                 {0, 0}
 2801                 }
 2802         },
 2803         {1, 1, 3, 0,            /* 0x85 */
 2804                 {{0, 0},
 2805                 {2, 2},
 2806                 {7, 7},
 2807                 {0, 0}
 2808                 }
 2809         },
 2810         {0, 1, 2, 0,            /* 0x86 */
 2811                 {{1, 2},
 2812                 {7, 7},
 2813                 {0, 0},
 2814                 {0, 0}
 2815                 }
 2816         },
 2817         {1, 1, 2, 0,            /* 0x87 */
 2818                 {{0, 2},
 2819                 {7, 7},
 2820                 {0, 0},
 2821                 {0, 0}
 2822                 }
 2823         },
 2824         {0, 1, 2, 0,            /* 0x88 */
 2825                 {{3, 3},
 2826                 {7, 7},
 2827                 {0, 0},
 2828                 {0, 0}
 2829                 }
 2830         },
 2831         {1, 1, 3, 0,            /* 0x89 */
 2832                 {{0, 0},
 2833                 {3, 3},
 2834                 {7, 7},
 2835                 {0, 0}
 2836                 }
 2837         },
 2838         {0, 1, 3, 0,            /* 0x8a */
 2839                 {{1, 1},
 2840                 {3, 3},
 2841                 {7, 7},
 2842                 {0, 0}
 2843                 }
 2844         },
 2845         {1, 1, 3, 0,            /* 0x8b */
 2846                 {{0, 1},
 2847                 {3, 3},
 2848                 {7, 7},
 2849                 {0, 0}
 2850                 }
 2851         },
 2852         {0, 1, 2, 0,            /* 0x8c */
 2853                 {{2, 3},
 2854                 {7, 7},
 2855                 {0, 0},
 2856                 {0, 0}
 2857                 }
 2858         },
 2859         {1, 1, 3, 0,            /* 0x8d */
 2860                 {{0, 0},
 2861                 {2, 3},
 2862                 {7, 7},
 2863                 {0, 0}
 2864                 }
 2865         },
 2866         {0, 1, 2, 0,            /* 0x8e */
 2867                 {{1, 3},
 2868                 {7, 7},
 2869                 {0, 0},
 2870                 {0, 0}
 2871                 }
 2872         },
 2873         {1, 1, 2, 0,            /* 0x8f */
 2874                 {{0, 3},
 2875                 {7, 7},
 2876                 {0, 0},
 2877                 {0, 0}
 2878                 }
 2879         },
 2880         {0, 1, 2, 0,            /* 0x90 */
 2881                 {{4, 4},
 2882                 {7, 7},
 2883                 {0, 0},
 2884                 {0, 0}
 2885                 }
 2886         },
 2887         {1, 1, 3, 0,            /* 0x91 */
 2888                 {{0, 0},
 2889                 {4, 4},
 2890                 {7, 7},
 2891                 {0, 0}
 2892                 }
 2893         },
 2894         {0, 1, 3, 0,            /* 0x92 */
 2895                 {{1, 1},
 2896                 {4, 4},
 2897                 {7, 7},
 2898                 {0, 0}
 2899                 }
 2900         },
 2901         {1, 1, 3, 0,            /* 0x93 */
 2902                 {{0, 1},
 2903                 {4, 4},
 2904                 {7, 7},
 2905                 {0, 0}
 2906                 }
 2907         },
 2908         {0, 1, 3, 0,            /* 0x94 */
 2909                 {{2, 2},
 2910                 {4, 4},
 2911                 {7, 7},
 2912                 {0, 0}
 2913                 }
 2914         },
 2915         {1, 1, 4, 0,            /* 0x95 */
 2916                 {{0, 0},
 2917                 {2, 2},
 2918                 {4, 4},
 2919                 {7, 7}
 2920                 }
 2921         },
 2922         {0, 1, 3, 0,            /* 0x96 */
 2923                 {{1, 2},
 2924                 {4, 4},
 2925                 {7, 7},
 2926                 {0, 0}
 2927                 }
 2928         },
 2929         {1, 1, 3, 0,            /* 0x97 */
 2930                 {{0, 2},
 2931                 {4, 4},
 2932                 {7, 7},
 2933                 {0, 0}
 2934                 }
 2935         },
 2936         {0, 1, 2, 0,            /* 0x98 */
 2937                 {{3, 4},
 2938                 {7, 7},
 2939                 {0, 0},
 2940                 {0, 0}
 2941                 }
 2942         },
 2943         {1, 1, 3, 0,            /* 0x99 */
 2944                 {{0, 0},
 2945                 {3, 4},
 2946                 {7, 7},
 2947                 {0, 0}
 2948                 }
 2949         },
 2950         {0, 1, 3, 0,            /* 0x9a */
 2951                 {{1, 1},
 2952                 {3, 4},
 2953                 {7, 7},
 2954                 {0, 0}
 2955                 }
 2956         },
 2957         {1, 1, 3, 0,            /* 0x9b */
 2958                 {{0, 1},
 2959                 {3, 4},
 2960                 {7, 7},
 2961                 {0, 0}
 2962                 }
 2963         },
 2964         {0, 1, 2, 0,            /* 0x9c */
 2965                 {{2, 4},
 2966                 {7, 7},
 2967                 {0, 0},
 2968                 {0, 0}
 2969                 }
 2970         },
 2971         {1, 1, 3, 0,            /* 0x9d */
 2972                 {{0, 0},
 2973                 {2, 4},
 2974                 {7, 7},
 2975                 {0, 0}
 2976                 }
 2977         },
 2978         {0, 1, 2, 0,            /* 0x9e */
 2979                 {{1, 4},
 2980                 {7, 7},
 2981                 {0, 0},
 2982                 {0, 0}
 2983                 }
 2984         },
 2985         {1, 1, 2, 0,            /* 0x9f */
 2986                 {{0, 4},
 2987                 {7, 7},
 2988                 {0, 0},
 2989                 {0, 0}
 2990                 }
 2991         },
 2992         {0, 1, 2, 0,            /* 0xa0 */
 2993                 {{5, 5},
 2994                 {7, 7},
 2995                 {0, 0},
 2996                 {0, 0}
 2997                 }
 2998         },
 2999         {1, 1, 3, 0,            /* 0xa1 */
 3000                 {{0, 0},
 3001                 {5, 5},
 3002                 {7, 7},
 3003                 {0, 0}
 3004                 }
 3005         },
 3006         {0, 1, 3, 0,            /* 0xa2 */
 3007                 {{1, 1},
 3008                 {5, 5},
 3009                 {7, 7},
 3010                 {0, 0}
 3011                 }
 3012         },
 3013         {1, 1, 3, 0,            /* 0xa3 */
 3014                 {{0, 1},
 3015                 {5, 5},
 3016                 {7, 7},
 3017                 {0, 0}
 3018                 }
 3019         },
 3020         {0, 1, 3, 0,            /* 0xa4 */
 3021                 {{2, 2},
 3022                 {5, 5},
 3023                 {7, 7},
 3024                 {0, 0}
 3025                 }
 3026         },
 3027         {1, 1, 4, 0,            /* 0xa5 */
 3028                 {{0, 0},
 3029                 {2, 2},
 3030                 {5, 5},
 3031                 {7, 7}
 3032                 }
 3033         },
 3034         {0, 1, 3, 0,            /* 0xa6 */
 3035                 {{1, 2},
 3036                 {5, 5},
 3037                 {7, 7},
 3038                 {0, 0}
 3039                 }
 3040         },
 3041         {1, 1, 3, 0,            /* 0xa7 */
 3042                 {{0, 2},
 3043                 {5, 5},
 3044                 {7, 7},
 3045                 {0, 0}
 3046                 }
 3047         },
 3048         {0, 1, 3, 0,            /* 0xa8 */
 3049                 {{3, 3},
 3050                 {5, 5},
 3051                 {7, 7},
 3052                 {0, 0}
 3053                 }
 3054         },
 3055         {1, 1, 4, 0,            /* 0xa9 */
 3056                 {{0, 0},
 3057                 {3, 3},
 3058                 {5, 5},
 3059                 {7, 7}
 3060                 }
 3061         },
 3062         {0, 1, 4, 0,            /* 0xaa */
 3063                 {{1, 1},
 3064                 {3, 3},
 3065                 {5, 5},
 3066                 {7, 7}
 3067                 }
 3068         },
 3069         {1, 1, 4, 0,            /* 0xab */
 3070                 {{0, 1},
 3071                 {3, 3},
 3072                 {5, 5},
 3073                 {7, 7}
 3074                 }
 3075         },
 3076         {0, 1, 3, 0,            /* 0xac */
 3077                 {{2, 3},
 3078                 {5, 5},
 3079                 {7, 7},
 3080                 {0, 0}
 3081                 }
 3082         },
 3083         {1, 1, 4, 0,            /* 0xad */
 3084                 {{0, 0},
 3085                 {2, 3},
 3086                 {5, 5},
 3087                 {7, 7}
 3088                 }
 3089         },
 3090         {0, 1, 3, 0,            /* 0xae */
 3091                 {{1, 3},
 3092                 {5, 5},
 3093                 {7, 7},
 3094                 {0, 0}
 3095                 }
 3096         },
 3097         {1, 1, 3, 0,            /* 0xaf */
 3098                 {{0, 3},
 3099                 {5, 5},
 3100                 {7, 7},
 3101                 {0, 0}
 3102                 }
 3103         },
 3104         {0, 1, 2, 0,            /* 0xb0 */
 3105                 {{4, 5},
 3106                 {7, 7},
 3107                 {0, 0},
 3108                 {0, 0}
 3109                 }
 3110         },
 3111         {1, 1, 3, 0,            /* 0xb1 */
 3112                 {{0, 0},
 3113                 {4, 5},
 3114                 {7, 7},
 3115                 {0, 0}
 3116                 }
 3117         },
 3118         {0, 1, 3, 0,            /* 0xb2 */
 3119                 {{1, 1},
 3120                 {4, 5},
 3121                 {7, 7},
 3122                 {0, 0}
 3123                 }
 3124         },
 3125         {1, 1, 3, 0,            /* 0xb3 */
 3126                 {{0, 1},
 3127                 {4, 5},
 3128                 {7, 7},
 3129                 {0, 0}
 3130                 }
 3131         },
 3132         {0, 1, 3, 0,            /* 0xb4 */
 3133                 {{2, 2},
 3134                 {4, 5},
 3135                 {7, 7},
 3136                 {0, 0}
 3137                 }
 3138         },
 3139         {1, 1, 4, 0,            /* 0xb5 */
 3140                 {{0, 0},
 3141                 {2, 2},
 3142                 {4, 5},
 3143                 {7, 7}
 3144                 }
 3145         },
 3146         {0, 1, 3, 0,            /* 0xb6 */
 3147                 {{1, 2},
 3148                 {4, 5},
 3149                 {7, 7},
 3150                 {0, 0}
 3151                 }
 3152         },
 3153         {1, 1, 3, 0,            /* 0xb7 */
 3154                 {{0, 2},
 3155                 {4, 5},
 3156                 {7, 7},
 3157                 {0, 0}
 3158                 }
 3159         },
 3160         {0, 1, 2, 0,            /* 0xb8 */
 3161                 {{3, 5},
 3162                 {7, 7},
 3163                 {0, 0},
 3164                 {0, 0}
 3165                 }
 3166         },
 3167         {1, 1, 3, 0,            /* 0xb9 */
 3168                 {{0, 0},
 3169                 {3, 5},
 3170                 {7, 7},
 3171                 {0, 0}
 3172                 }
 3173         },
 3174         {0, 1, 3, 0,            /* 0xba */
 3175                 {{1, 1},
 3176                 {3, 5},
 3177                 {7, 7},
 3178                 {0, 0}
 3179                 }
 3180         },
 3181         {1, 1, 3, 0,            /* 0xbb */
 3182                 {{0, 1},
 3183                 {3, 5},
 3184                 {7, 7},
 3185                 {0, 0}
 3186                 }
 3187         },
 3188         {0, 1, 2, 0,            /* 0xbc */
 3189                 {{2, 5},
 3190                 {7, 7},
 3191                 {0, 0},
 3192                 {0, 0}
 3193                 }
 3194         },
 3195         {1, 1, 3, 0,            /* 0xbd */
 3196                 {{0, 0},
 3197                 {2, 5},
 3198                 {7, 7},
 3199                 {0, 0}
 3200                 }
 3201         },
 3202         {0, 1, 2, 0,            /* 0xbe */
 3203                 {{1, 5},
 3204                 {7, 7},
 3205                 {0, 0},
 3206                 {0, 0}
 3207                 }
 3208         },
 3209         {1, 1, 2, 0,            /* 0xbf */
 3210                 {{0, 5},
 3211                 {7, 7},
 3212                 {0, 0},
 3213                 {0, 0}
 3214                 }
 3215         },
 3216         {0, 1, 1, 0,            /* 0xc0 */
 3217                 {{6, 7},
 3218                 {0, 0},
 3219                 {0, 0},
 3220                 {0, 0}
 3221                 }
 3222         },
 3223         {1, 1, 2, 0,            /* 0xc1 */
 3224                 {{0, 0},
 3225                 {6, 7},
 3226                 {0, 0},
 3227                 {0, 0}
 3228                 }
 3229         },
 3230         {0, 1, 2, 0,            /* 0xc2 */
 3231                 {{1, 1},
 3232                 {6, 7},
 3233                 {0, 0},
 3234                 {0, 0}
 3235                 }
 3236         },
 3237         {1, 1, 2, 0,            /* 0xc3 */
 3238                 {{0, 1},
 3239                 {6, 7},
 3240                 {0, 0},
 3241                 {0, 0}
 3242                 }
 3243         },
 3244         {0, 1, 2, 0,            /* 0xc4 */
 3245                 {{2, 2},
 3246                 {6, 7},
 3247                 {0, 0},
 3248                 {0, 0}
 3249                 }
 3250         },
 3251         {1, 1, 3, 0,            /* 0xc5 */
 3252                 {{0, 0},
 3253                 {2, 2},
 3254                 {6, 7},
 3255                 {0, 0}
 3256                 }
 3257         },
 3258         {0, 1, 2, 0,            /* 0xc6 */
 3259                 {{1, 2},
 3260                 {6, 7},
 3261                 {0, 0},
 3262                 {0, 0}
 3263                 }
 3264         },
 3265         {1, 1, 2, 0,            /* 0xc7 */
 3266                 {{0, 2},
 3267                 {6, 7},
 3268                 {0, 0},
 3269                 {0, 0}
 3270                 }
 3271         },
 3272         {0, 1, 2, 0,            /* 0xc8 */
 3273                 {{3, 3},
 3274                 {6, 7},
 3275                 {0, 0},
 3276                 {0, 0}
 3277                 }
 3278         },
 3279         {1, 1, 3, 0,            /* 0xc9 */
 3280                 {{0, 0},
 3281                 {3, 3},
 3282                 {6, 7},
 3283                 {0, 0}
 3284                 }
 3285         },
 3286         {0, 1, 3, 0,            /* 0xca */
 3287                 {{1, 1},
 3288                 {3, 3},
 3289                 {6, 7},
 3290                 {0, 0}
 3291                 }
 3292         },
 3293         {1, 1, 3, 0,            /* 0xcb */
 3294                 {{0, 1},
 3295                 {3, 3},
 3296                 {6, 7},
 3297                 {0, 0}
 3298                 }
 3299         },
 3300         {0, 1, 2, 0,            /* 0xcc */
 3301                 {{2, 3},
 3302                 {6, 7},
 3303                 {0, 0},
 3304                 {0, 0}
 3305                 }
 3306         },
 3307         {1, 1, 3, 0,            /* 0xcd */
 3308                 {{0, 0},
 3309                 {2, 3},
 3310                 {6, 7},
 3311                 {0, 0}
 3312                 }
 3313         },
 3314         {0, 1, 2, 0,            /* 0xce */
 3315                 {{1, 3},
 3316                 {6, 7},
 3317                 {0, 0},
 3318                 {0, 0}
 3319                 }
 3320         },
 3321         {1, 1, 2, 0,            /* 0xcf */
 3322                 {{0, 3},
 3323                 {6, 7},
 3324                 {0, 0},
 3325                 {0, 0}
 3326                 }
 3327         },
 3328         {0, 1, 2, 0,            /* 0xd0 */
 3329                 {{4, 4},
 3330                 {6, 7},
 3331                 {0, 0},
 3332                 {0, 0}
 3333                 }
 3334         },
 3335         {1, 1, 3, 0,            /* 0xd1 */
 3336                 {{0, 0},
 3337                 {4, 4},
 3338                 {6, 7},
 3339                 {0, 0}
 3340                 }
 3341         },
 3342         {0, 1, 3, 0,            /* 0xd2 */
 3343                 {{1, 1},
 3344                 {4, 4},
 3345                 {6, 7},
 3346                 {0, 0}
 3347                 }
 3348         },
 3349         {1, 1, 3, 0,            /* 0xd3 */
 3350                 {{0, 1},
 3351                 {4, 4},
 3352                 {6, 7},
 3353                 {0, 0}
 3354                 }
 3355         },
 3356         {0, 1, 3, 0,            /* 0xd4 */
 3357                 {{2, 2},
 3358                 {4, 4},
 3359                 {6, 7},
 3360                 {0, 0}
 3361                 }
 3362         },
 3363         {1, 1, 4, 0,            /* 0xd5 */
 3364                 {{0, 0},
 3365                 {2, 2},
 3366                 {4, 4},
 3367                 {6, 7}
 3368                 }
 3369         },
 3370         {0, 1, 3, 0,            /* 0xd6 */
 3371                 {{1, 2},
 3372                 {4, 4},
 3373                 {6, 7},
 3374                 {0, 0}
 3375                 }
 3376         },
 3377         {1, 1, 3, 0,            /* 0xd7 */
 3378                 {{0, 2},
 3379                 {4, 4},
 3380                 {6, 7},
 3381                 {0, 0}
 3382                 }
 3383         },
 3384         {0, 1, 2, 0,            /* 0xd8 */
 3385                 {{3, 4},
 3386                 {6, 7},
 3387                 {0, 0},
 3388                 {0, 0}
 3389                 }
 3390         },
 3391         {1, 1, 3, 0,            /* 0xd9 */
 3392                 {{0, 0},
 3393                 {3, 4},
 3394                 {6, 7},
 3395                 {0, 0}
 3396                 }
 3397         },
 3398         {0, 1, 3, 0,            /* 0xda */
 3399                 {{1, 1},
 3400                 {3, 4},
 3401                 {6, 7},
 3402                 {0, 0}
 3403                 }
 3404         },
 3405         {1, 1, 3, 0,            /* 0xdb */
 3406                 {{0, 1},
 3407                 {3, 4},
 3408                 {6, 7},
 3409                 {0, 0}
 3410                 }
 3411         },
 3412         {0, 1, 2, 0,            /* 0xdc */
 3413                 {{2, 4},
 3414                 {6, 7},
 3415                 {0, 0},
 3416                 {0, 0}
 3417                 }
 3418         },
 3419         {1, 1, 3, 0,            /* 0xdd */
 3420                 {{0, 0},
 3421                 {2, 4},
 3422                 {6, 7},
 3423                 {0, 0}
 3424                 }
 3425         },
 3426         {0, 1, 2, 0,            /* 0xde */
 3427                 {{1, 4},
 3428                 {6, 7},
 3429                 {0, 0},
 3430                 {0, 0}
 3431                 }
 3432         },
 3433         {1, 1, 2, 0,            /* 0xdf */
 3434                 {{0, 4},
 3435                 {6, 7},
 3436                 {0, 0},
 3437                 {0, 0}
 3438                 }
 3439         },
 3440         {0, 1, 1, 0,            /* 0xe0 */
 3441                 {{5, 7},
 3442                 {0, 0},
 3443                 {0, 0},
 3444                 {0, 0}
 3445                 }
 3446         },
 3447         {1, 1, 2, 0,            /* 0xe1 */
 3448                 {{0, 0},
 3449                 {5, 7},
 3450                 {0, 0},
 3451                 {0, 0}
 3452                 }
 3453         },
 3454         {0, 1, 2, 0,            /* 0xe2 */
 3455                 {{1, 1},
 3456                 {5, 7},
 3457                 {0, 0},
 3458                 {0, 0}
 3459                 }
 3460         },
 3461         {1, 1, 2, 0,            /* 0xe3 */
 3462                 {{0, 1},
 3463                 {5, 7},
 3464                 {0, 0},
 3465                 {0, 0}
 3466                 }
 3467         },
 3468         {0, 1, 2, 0,            /* 0xe4 */
 3469                 {{2, 2},
 3470                 {5, 7},
 3471                 {0, 0},
 3472                 {0, 0}
 3473                 }
 3474         },
 3475         {1, 1, 3, 0,            /* 0xe5 */
 3476                 {{0, 0},
 3477                 {2, 2},
 3478                 {5, 7},
 3479                 {0, 0}
 3480                 }
 3481         },
 3482         {0, 1, 2, 0,            /* 0xe6 */
 3483                 {{1, 2},
 3484                 {5, 7},
 3485                 {0, 0},
 3486                 {0, 0}
 3487                 }
 3488         },
 3489         {1, 1, 2, 0,            /* 0xe7 */
 3490                 {{0, 2},
 3491                 {5, 7},
 3492                 {0, 0},
 3493                 {0, 0}
 3494                 }
 3495         },
 3496         {0, 1, 2, 0,            /* 0xe8 */
 3497                 {{3, 3},
 3498                 {5, 7},
 3499                 {0, 0},
 3500                 {0, 0}
 3501                 }
 3502         },
 3503         {1, 1, 3, 0,            /* 0xe9 */
 3504                 {{0, 0},
 3505                 {3, 3},
 3506                 {5, 7},
 3507                 {0, 0}
 3508                 }
 3509         },
 3510         {0, 1, 3, 0,            /* 0xea */
 3511                 {{1, 1},
 3512                 {3, 3},
 3513                 {5, 7},
 3514                 {0, 0}
 3515                 }
 3516         },
 3517         {1, 1, 3, 0,            /* 0xeb */
 3518                 {{0, 1},
 3519                 {3, 3},
 3520                 {5, 7},
 3521                 {0, 0}
 3522                 }
 3523         },
 3524         {0, 1, 2, 0,            /* 0xec */
 3525                 {{2, 3},
 3526                 {5, 7},
 3527                 {0, 0},
 3528                 {0, 0}
 3529                 }
 3530         },
 3531         {1, 1, 3, 0,            /* 0xed */
 3532                 {{0, 0},
 3533                 {2, 3},
 3534                 {5, 7},
 3535                 {0, 0}
 3536                 }
 3537         },
 3538         {0, 1, 2, 0,            /* 0xee */
 3539                 {{1, 3},
 3540                 {5, 7},
 3541                 {0, 0},
 3542                 {0, 0}
 3543                 }
 3544         },
 3545         {1, 1, 2, 0,            /* 0xef */
 3546                 {{0, 3},
 3547                 {5, 7},
 3548                 {0, 0},
 3549                 {0, 0}
 3550                 }
 3551         },
 3552         {0, 1, 1, 0,            /* 0xf0 */
 3553                 {{4, 7},
 3554                 {0, 0},
 3555                 {0, 0},
 3556                 {0, 0}
 3557                 }
 3558         },
 3559         {1, 1, 2, 0,            /* 0xf1 */
 3560                 {{0, 0},
 3561                 {4, 7},
 3562                 {0, 0},
 3563                 {0, 0}
 3564                 }
 3565         },
 3566         {0, 1, 2, 0,            /* 0xf2 */
 3567                 {{1, 1},
 3568                 {4, 7},
 3569                 {0, 0},
 3570                 {0, 0}
 3571                 }
 3572         },
 3573         {1, 1, 2, 0,            /* 0xf3 */
 3574                 {{0, 1},
 3575                 {4, 7},
 3576                 {0, 0},
 3577                 {0, 0}
 3578                 }
 3579         },
 3580         {0, 1, 2, 0,            /* 0xf4 */
 3581                 {{2, 2},
 3582                 {4, 7},
 3583                 {0, 0},
 3584                 {0, 0}
 3585                 }
 3586         },
 3587         {1, 1, 3, 0,            /* 0xf5 */
 3588                 {{0, 0},
 3589                 {2, 2},
 3590                 {4, 7},
 3591                 {0, 0}
 3592                 }
 3593         },
 3594         {0, 1, 2, 0,            /* 0xf6 */
 3595                 {{1, 2},
 3596                 {4, 7},
 3597                 {0, 0},
 3598                 {0, 0}
 3599                 }
 3600         },
 3601         {1, 1, 2, 0,            /* 0xf7 */
 3602                 {{0, 2},
 3603                 {4, 7},
 3604                 {0, 0},
 3605                 {0, 0}
 3606                 }
 3607         },
 3608         {0, 1, 1, 0,            /* 0xf8 */
 3609                 {{3, 7},
 3610                 {0, 0},
 3611                 {0, 0},
 3612                 {0, 0}
 3613                 }
 3614         },
 3615         {1, 1, 2, 0,            /* 0xf9 */
 3616                 {{0, 0},
 3617                 {3, 7},
 3618                 {0, 0},
 3619                 {0, 0}
 3620                 }
 3621         },
 3622         {0, 1, 2, 0,            /* 0xfa */
 3623                 {{1, 1},
 3624                 {3, 7},
 3625                 {0, 0},
 3626                 {0, 0}
 3627                 }
 3628         },
 3629         {1, 1, 2, 0,            /* 0xfb */
 3630                 {{0, 1},
 3631                 {3, 7},
 3632                 {0, 0},
 3633                 {0, 0}
 3634                 }
 3635         },
 3636         {0, 1, 1, 0,            /* 0xfc */
 3637                 {{2, 7},
 3638                 {0, 0},
 3639                 {0, 0},
 3640                 {0, 0}
 3641                 }
 3642         },
 3643         {1, 1, 2, 0,            /* 0xfd */
 3644                 {{0, 0},
 3645                 {2, 7},
 3646                 {0, 0},
 3647                 {0, 0}
 3648                 }
 3649         },
 3650         {0, 1, 1, 0,            /* 0xfe */
 3651                 {{1, 7},
 3652                 {0, 0},
 3653                 {0, 0},
 3654                 {0, 0}
 3655                 }
 3656         },
 3657         {1, 1, 1, 0,            /* 0xff */
 3658                 {{0, 7},
 3659                 {0, 0},
 3660                 {0, 0},
 3661                 {0, 0}
 3662                 }
 3663         }
 3664 };
 3665 
 3666 
 3667 
 3668 int
 3669 sctp_is_address_in_scope(struct sctp_ifa *ifa,
 3670     int ipv4_addr_legal,
 3671     int ipv6_addr_legal,
 3672     int loopback_scope,
 3673     int ipv4_local_scope,
 3674     int local_scope,
 3675     int site_scope,
 3676     int do_update)
 3677 {
 3678         if ((loopback_scope == 0) &&
 3679             (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
 3680                 /*
 3681                  * skip loopback if not in scope *
 3682                  */
 3683                 return (0);
 3684         }
 3685         switch (ifa->address.sa.sa_family) {
 3686         case AF_INET:
 3687                 if (ipv4_addr_legal) {
 3688                         struct sockaddr_in *sin;
 3689 
 3690                         sin = (struct sockaddr_in *)&ifa->address.sin;
 3691                         if (sin->sin_addr.s_addr == 0) {
 3692                                 /* not in scope , unspecified */
 3693                                 return (0);
 3694                         }
 3695                         if ((ipv4_local_scope == 0) &&
 3696                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
 3697                                 /* private address not in scope */
 3698                                 return (0);
 3699                         }
 3700                 } else {
 3701                         return (0);
 3702                 }
 3703                 break;
 3704 #ifdef INET6
 3705         case AF_INET6:
 3706                 if (ipv6_addr_legal) {
 3707                         struct sockaddr_in6 *sin6;
 3708 
 3709                         /*
 3710                          * Must update the flags,  bummer, which means any
 3711                          * IFA locks must now be applied HERE <->
 3712                          */
 3713                         if (do_update) {
 3714                                 sctp_gather_internal_ifa_flags(ifa);
 3715                         }
 3716                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
 3717                                 return (0);
 3718                         }
 3719                         /* ok to use deprecated addresses? */
 3720                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
 3721                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 3722                                 /* skip unspecifed addresses */
 3723                                 return (0);
 3724                         }
 3725                         if (    /* (local_scope == 0) && */
 3726                             (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
 3727                                 return (0);
 3728                         }
 3729                         if ((site_scope == 0) &&
 3730                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
 3731                                 return (0);
 3732                         }
 3733                 } else {
 3734                         return (0);
 3735                 }
 3736                 break;
 3737 #endif
 3738         default:
 3739                 return (0);
 3740         }
 3741         return (1);
 3742 }
 3743 
 3744 static struct mbuf *
 3745 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
 3746 {
 3747         struct sctp_paramhdr *parmh;
 3748         struct mbuf *mret;
 3749         int len;
 3750 
 3751         if (ifa->address.sa.sa_family == AF_INET) {
 3752                 len = sizeof(struct sctp_ipv4addr_param);
 3753         } else if (ifa->address.sa.sa_family == AF_INET6) {
 3754                 len = sizeof(struct sctp_ipv6addr_param);
 3755         } else {
 3756                 /* unknown type */
 3757                 return (m);
 3758         }
 3759         if (M_TRAILINGSPACE(m) >= len) {
 3760                 /* easy side we just drop it on the end */
 3761                 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
 3762                 mret = m;
 3763         } else {
 3764                 /* Need more space */
 3765                 mret = m;
 3766                 while (SCTP_BUF_NEXT(mret) != NULL) {
 3767                         mret = SCTP_BUF_NEXT(mret);
 3768                 }
 3769                 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
 3770                 if (SCTP_BUF_NEXT(mret) == NULL) {
 3771                         /* We are hosed, can't add more addresses */
 3772                         return (m);
 3773                 }
 3774                 mret = SCTP_BUF_NEXT(mret);
 3775                 parmh = mtod(mret, struct sctp_paramhdr *);
 3776         }
 3777         /* now add the parameter */
 3778         switch (ifa->address.sa.sa_family) {
 3779         case AF_INET:
 3780                 {
 3781                         struct sctp_ipv4addr_param *ipv4p;
 3782                         struct sockaddr_in *sin;
 3783 
 3784                         sin = (struct sockaddr_in *)&ifa->address.sin;
 3785                         ipv4p = (struct sctp_ipv4addr_param *)parmh;
 3786                         parmh->param_type = htons(SCTP_IPV4_ADDRESS);
 3787                         parmh->param_length = htons(len);
 3788                         ipv4p->addr = sin->sin_addr.s_addr;
 3789                         SCTP_BUF_LEN(mret) += len;
 3790                         break;
 3791                 }
 3792 #ifdef INET6
 3793         case AF_INET6:
 3794                 {
 3795                         struct sctp_ipv6addr_param *ipv6p;
 3796                         struct sockaddr_in6 *sin6;
 3797 
 3798                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
 3799                         ipv6p = (struct sctp_ipv6addr_param *)parmh;
 3800                         parmh->param_type = htons(SCTP_IPV6_ADDRESS);
 3801                         parmh->param_length = htons(len);
 3802                         memcpy(ipv6p->addr, &sin6->sin6_addr,
 3803                             sizeof(ipv6p->addr));
 3804                         /* clear embedded scope in the address */
 3805                         in6_clearscope((struct in6_addr *)ipv6p->addr);
 3806                         SCTP_BUF_LEN(mret) += len;
 3807                         break;
 3808                 }
 3809 #endif
 3810         default:
 3811                 return (m);
 3812         }
 3813         return (mret);
 3814 }
 3815 
 3816 
 3817 struct mbuf *
 3818 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
 3819     struct mbuf *m_at, int cnt_inits_to)
 3820 {
 3821         struct sctp_vrf *vrf = NULL;
 3822         int cnt, limit_out = 0, total_count;
 3823         uint32_t vrf_id;
 3824 
 3825         vrf_id = inp->def_vrf_id;
 3826         SCTP_IPI_ADDR_RLOCK();
 3827         vrf = sctp_find_vrf(vrf_id);
 3828         if (vrf == NULL) {
 3829                 SCTP_IPI_ADDR_RUNLOCK();
 3830                 return (m_at);
 3831         }
 3832         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 3833                 struct sctp_ifa *sctp_ifap;
 3834                 struct sctp_ifn *sctp_ifnp;
 3835 
 3836                 cnt = cnt_inits_to;
 3837                 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
 3838                         limit_out = 1;
 3839                         cnt = SCTP_ADDRESS_LIMIT;
 3840                         goto skip_count;
 3841                 }
 3842                 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
 3843                         if ((scope->loopback_scope == 0) &&
 3844                             SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
 3845                                 /*
 3846                                  * Skip loopback devices if loopback_scope
 3847                                  * not set
 3848                                  */
 3849                                 continue;
 3850                         }
 3851                         LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
 3852                                 if (sctp_is_address_in_scope(sctp_ifap,
 3853                                     scope->ipv4_addr_legal,
 3854                                     scope->ipv6_addr_legal,
 3855                                     scope->loopback_scope,
 3856                                     scope->ipv4_local_scope,
 3857                                     scope->local_scope,
 3858                                     scope->site_scope, 1) == 0) {
 3859                                         continue;
 3860                                 }
 3861                                 cnt++;
 3862                                 if (cnt > SCTP_ADDRESS_LIMIT) {
 3863                                         break;
 3864                                 }
 3865                         }
 3866                         if (cnt > SCTP_ADDRESS_LIMIT) {
 3867                                 break;
 3868                         }
 3869                 }
 3870 skip_count:
 3871                 if (cnt > 1) {
 3872                         total_count = 0;
 3873                         LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
 3874                                 cnt = 0;
 3875                                 if ((scope->loopback_scope == 0) &&
 3876                                     SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
 3877                                         /*
 3878                                          * Skip loopback devices if
 3879                                          * loopback_scope not set
 3880                                          */
 3881                                         continue;
 3882                                 }
 3883                                 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
 3884                                         if (sctp_is_address_in_scope(sctp_ifap,
 3885                                             scope->ipv4_addr_legal,
 3886                                             scope->ipv6_addr_legal,
 3887                                             scope->loopback_scope,
 3888                                             scope->ipv4_local_scope,
 3889                                             scope->local_scope,
 3890                                             scope->site_scope, 0) == 0) {
 3891                                                 continue;
 3892                                         }
 3893                                         m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
 3894                                         if (limit_out) {
 3895                                                 cnt++;
 3896                                                 total_count++;
 3897                                                 if (cnt >= 2) {
 3898                                                         /*
 3899                                                          * two from each
 3900                                                          * address
 3901                                                          */
 3902                                                         break;
 3903                                                 }
 3904                                                 if (total_count > SCTP_ADDRESS_LIMIT) {
 3905                                                         /* No more addresses */
 3906                                                         break;
 3907                                                 }
 3908                                         }
 3909                                 }
 3910                         }
 3911                 }
 3912         } else {
 3913                 struct sctp_laddr *laddr;
 3914 
 3915                 cnt = cnt_inits_to;
 3916                 /* First, how many ? */
 3917                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 3918                         if (laddr->ifa == NULL) {
 3919                                 continue;
 3920                         }
 3921                         if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
 3922                                 /*
 3923                                  * Address being deleted by the system, dont
 3924                                  * list.
 3925                                  */
 3926                                 continue;
 3927                         if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 3928                                 /*
 3929                                  * Address being deleted on this ep don't
 3930                                  * list.
 3931                                  */
 3932                                 continue;
 3933                         }
 3934                         if (sctp_is_address_in_scope(laddr->ifa,
 3935                             scope->ipv4_addr_legal,
 3936                             scope->ipv6_addr_legal,
 3937                             scope->loopback_scope,
 3938                             scope->ipv4_local_scope,
 3939                             scope->local_scope,
 3940                             scope->site_scope, 1) == 0) {
 3941                                 continue;
 3942                         }
 3943                         cnt++;
 3944                 }
 3945                 if (cnt > SCTP_ADDRESS_LIMIT) {
 3946                         limit_out = 1;
 3947                 }
 3948                 /*
 3949                  * To get through a NAT we only list addresses if we have
 3950                  * more than one. That way if you just bind a single address
 3951                  * we let the source of the init dictate our address.
 3952                  */
 3953                 if (cnt > 1) {
 3954                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 3955                                 cnt = 0;
 3956                                 if (laddr->ifa == NULL) {
 3957                                         continue;
 3958                                 }
 3959                                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
 3960                                         continue;
 3961 
 3962                                 if (sctp_is_address_in_scope(laddr->ifa,
 3963                                     scope->ipv4_addr_legal,
 3964                                     scope->ipv6_addr_legal,
 3965                                     scope->loopback_scope,
 3966                                     scope->ipv4_local_scope,
 3967                                     scope->local_scope,
 3968                                     scope->site_scope, 0) == 0) {
 3969                                         continue;
 3970                                 }
 3971                                 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
 3972                                 cnt++;
 3973                                 if (cnt >= SCTP_ADDRESS_LIMIT) {
 3974                                         break;
 3975                                 }
 3976                         }
 3977                 }
 3978         }
 3979         SCTP_IPI_ADDR_RUNLOCK();
 3980         return (m_at);
 3981 }
 3982 
 3983 static struct sctp_ifa *
 3984 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
 3985     uint8_t dest_is_loop,
 3986     uint8_t dest_is_priv,
 3987     sa_family_t fam)
 3988 {
 3989         uint8_t dest_is_global = 0;
 3990 
 3991         /* dest_is_priv is true if destination is a private address */
 3992         /* dest_is_loop is true if destination is a loopback addresses */
 3993 
 3994         /*
 3995          * Here we determine if its a preferred address. A preferred address
 3996          * means it is the same scope or higher scope then the destination.
 3997          * L = loopback, P = private, G = global
 3998          * ----------------------------------------- src    |  dest | result
 3999          * ---------------------------------------- L     |    L  |    yes
 4000          * ----------------------------------------- P     |    L  |
 4001          * yes-v4 no-v6 ----------------------------------------- G     |
 4002          * L  |    yes-v4 no-v6 ----------------------------------------- L
 4003          * |    P  |    no ----------------------------------------- P     |
 4004          * P  |    yes ----------------------------------------- G     |
 4005          * P  |    no ----------------------------------------- L     |    G
 4006          * |    no ----------------------------------------- P     |    G  |
 4007          * no ----------------------------------------- G     |    G  |
 4008          * yes -----------------------------------------
 4009          */
 4010 
 4011         if (ifa->address.sa.sa_family != fam) {
 4012                 /* forget mis-matched family */
 4013                 return (NULL);
 4014         }
 4015         if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
 4016                 dest_is_global = 1;
 4017         }
 4018         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
 4019         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
 4020         /* Ok the address may be ok */
 4021         if (fam == AF_INET6) {
 4022                 /* ok to use deprecated addresses? no lets not! */
 4023                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
 4024                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
 4025                         return (NULL);
 4026                 }
 4027                 if (ifa->src_is_priv && !ifa->src_is_loop) {
 4028                         if (dest_is_loop) {
 4029                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
 4030                                 return (NULL);
 4031                         }
 4032                 }
 4033                 if (ifa->src_is_glob) {
 4034                         if (dest_is_loop) {
 4035                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
 4036                                 return (NULL);
 4037                         }
 4038                 }
 4039         }
 4040         /*
 4041          * Now that we know what is what, implement or table this could in
 4042          * theory be done slicker (it used to be), but this is
 4043          * straightforward and easier to validate :-)
 4044          */
 4045         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
 4046             ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
 4047         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
 4048             dest_is_loop, dest_is_priv, dest_is_global);
 4049 
 4050         if ((ifa->src_is_loop) && (dest_is_priv)) {
 4051                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
 4052                 return (NULL);
 4053         }
 4054         if ((ifa->src_is_glob) && (dest_is_priv)) {
 4055                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
 4056                 return (NULL);
 4057         }
 4058         if ((ifa->src_is_loop) && (dest_is_global)) {
 4059                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
 4060                 return (NULL);
 4061         }
 4062         if ((ifa->src_is_priv) && (dest_is_global)) {
 4063                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
 4064                 return (NULL);
 4065         }
 4066         SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
 4067         /* its a preferred address */
 4068         return (ifa);
 4069 }
 4070 
 4071 static struct sctp_ifa *
 4072 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
 4073     uint8_t dest_is_loop,
 4074     uint8_t dest_is_priv,
 4075     sa_family_t fam)
 4076 {
 4077         uint8_t dest_is_global = 0;
 4078 
 4079 
 4080         /*
 4081          * Here we determine if its a acceptable address. A acceptable
 4082          * address means it is the same scope or higher scope but we can
 4083          * allow for NAT which means its ok to have a global dest and a
 4084          * private src.
 4085          * 
 4086          * L = loopback, P = private, G = global
 4087          * ----------------------------------------- src    |  dest | result
 4088          * ----------------------------------------- L     |   L   |    yes
 4089          * ----------------------------------------- P     |   L   |
 4090          * yes-v4 no-v6 ----------------------------------------- G     |
 4091          * L   |    yes ----------------------------------------- L     |
 4092          * P   |    no ----------------------------------------- P     |   P
 4093          * |    yes ----------------------------------------- G     |   P
 4094          * |    yes - May not work -----------------------------------------
 4095          * L     |   G   |    no ----------------------------------------- P
 4096          * |   G   |    yes - May not work
 4097          * ----------------------------------------- G     |   G   |    yes
 4098          * -----------------------------------------
 4099          */
 4100 
 4101         if (ifa->address.sa.sa_family != fam) {
 4102                 /* forget non matching family */
 4103                 return (NULL);
 4104         }
 4105         /* Ok the address may be ok */
 4106         if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
 4107                 dest_is_global = 1;
 4108         }
 4109         if (fam == AF_INET6) {
 4110                 /* ok to use deprecated addresses? */
 4111                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
 4112                         return (NULL);
 4113                 }
 4114                 if (ifa->src_is_priv) {
 4115                         /* Special case, linklocal to loop */
 4116                         if (dest_is_loop)
 4117                                 return (NULL);
 4118                 }
 4119         }
 4120         /*
 4121          * Now that we know what is what, implement our table. This could in
 4122          * theory be done slicker (it used to be), but this is
 4123          * straightforward and easier to validate :-)
 4124          */
 4125         if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
 4126                 return (NULL);
 4127         }
 4128         if ((ifa->src_is_loop == 1) && (dest_is_global)) {
 4129                 return (NULL);
 4130         }
 4131         /* its an acceptable address */
 4132         return (ifa);
 4133 }
 4134 
 4135 int
 4136 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
 4137 {
 4138         struct sctp_laddr *laddr;
 4139 
 4140         if (stcb == NULL) {
 4141                 /* There are no restrictions, no TCB :-) */
 4142                 return (0);
 4143         }
 4144         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
 4145                 if (laddr->ifa == NULL) {
 4146                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
 4147                             __FUNCTION__);
 4148                         continue;
 4149                 }
 4150                 if (laddr->ifa == ifa) {
 4151                         /* Yes it is on the list */
 4152                         return (1);
 4153                 }
 4154         }
 4155         return (0);
 4156 }
 4157 
 4158 
 4159 int
 4160 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
 4161 {
 4162         struct sctp_laddr *laddr;
 4163 
 4164         if (ifa == NULL)
 4165                 return (0);
 4166         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 4167                 if (laddr->ifa == NULL) {
 4168                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
 4169                             __FUNCTION__);
 4170                         continue;
 4171                 }
 4172                 if ((laddr->ifa == ifa) && laddr->action == 0)
 4173                         /* same pointer */
 4174                         return (1);
 4175         }
 4176         return (0);
 4177 }
 4178 
 4179 
 4180 
 4181 static struct sctp_ifa *
 4182 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
 4183     sctp_route_t * ro,
 4184     uint32_t vrf_id,
 4185     int non_asoc_addr_ok,
 4186     uint8_t dest_is_priv,
 4187     uint8_t dest_is_loop,
 4188     sa_family_t fam)
 4189 {
 4190         struct sctp_laddr *laddr, *starting_point;
 4191         void *ifn;
 4192         int resettotop = 0;
 4193         struct sctp_ifn *sctp_ifn;
 4194         struct sctp_ifa *sctp_ifa, *sifa;
 4195         struct sctp_vrf *vrf;
 4196         uint32_t ifn_index;
 4197 
 4198         vrf = sctp_find_vrf(vrf_id);
 4199         if (vrf == NULL)
 4200                 return (NULL);
 4201 
 4202         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 4203         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
 4204         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
 4205         /*
 4206          * first question, is the ifn we will emit on in our list, if so, we
 4207          * want such an address. Note that we first looked for a preferred
 4208          * address.
 4209          */
 4210         if (sctp_ifn) {
 4211                 /* is a preferred one on the interface we route out? */
 4212                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 4213                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 4214                             (non_asoc_addr_ok == 0))
 4215                                 continue;
 4216                         sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
 4217                             dest_is_loop,
 4218                             dest_is_priv, fam);
 4219                         if (sifa == NULL)
 4220                                 continue;
 4221                         if (sctp_is_addr_in_ep(inp, sifa)) {
 4222                                 atomic_add_int(&sifa->refcount, 1);
 4223                                 return (sifa);
 4224                         }
 4225                 }
 4226         }
 4227         /*
 4228          * ok, now we now need to find one on the list of the addresses. We
 4229          * can't get one on the emitting interface so let's find first a
 4230          * preferred one. If not that an acceptable one otherwise... we
 4231          * return NULL.
 4232          */
 4233         starting_point = inp->next_addr_touse;
 4234 once_again:
 4235         if (inp->next_addr_touse == NULL) {
 4236                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
 4237                 resettotop = 1;
 4238         }
 4239         for (laddr = inp->next_addr_touse; laddr;
 4240             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 4241                 if (laddr->ifa == NULL) {
 4242                         /* address has been removed */
 4243                         continue;
 4244                 }
 4245                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 4246                         /* address is being deleted */
 4247                         continue;
 4248                 }
 4249                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
 4250                     dest_is_priv, fam);
 4251                 if (sifa == NULL)
 4252                         continue;
 4253                 atomic_add_int(&sifa->refcount, 1);
 4254                 return (sifa);
 4255         }
 4256         if (resettotop == 0) {
 4257                 inp->next_addr_touse = NULL;
 4258                 goto once_again;
 4259         }
 4260         inp->next_addr_touse = starting_point;
 4261         resettotop = 0;
 4262 once_again_too:
 4263         if (inp->next_addr_touse == NULL) {
 4264                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
 4265                 resettotop = 1;
 4266         }
 4267         /* ok, what about an acceptable address in the inp */
 4268         for (laddr = inp->next_addr_touse; laddr;
 4269             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 4270                 if (laddr->ifa == NULL) {
 4271                         /* address has been removed */
 4272                         continue;
 4273                 }
 4274                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 4275                         /* address is being deleted */
 4276                         continue;
 4277                 }
 4278                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
 4279                     dest_is_priv, fam);
 4280                 if (sifa == NULL)
 4281                         continue;
 4282                 atomic_add_int(&sifa->refcount, 1);
 4283                 return (sifa);
 4284         }
 4285         if (resettotop == 0) {
 4286                 inp->next_addr_touse = NULL;
 4287                 goto once_again_too;
 4288         }
 4289         /*
 4290          * no address bound can be a source for the destination we are in
 4291          * trouble
 4292          */
 4293         return (NULL);
 4294 }
 4295 
 4296 
 4297 
 4298 static struct sctp_ifa *
 4299 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
 4300     struct sctp_tcb *stcb,
 4301     struct sctp_nets *net,
 4302     sctp_route_t * ro,
 4303     uint32_t vrf_id,
 4304     uint8_t dest_is_priv,
 4305     uint8_t dest_is_loop,
 4306     int non_asoc_addr_ok,
 4307     sa_family_t fam)
 4308 {
 4309         struct sctp_laddr *laddr, *starting_point;
 4310         void *ifn;
 4311         struct sctp_ifn *sctp_ifn;
 4312         struct sctp_ifa *sctp_ifa, *sifa;
 4313         uint8_t start_at_beginning = 0;
 4314         struct sctp_vrf *vrf;
 4315         uint32_t ifn_index;
 4316 
 4317         /*
 4318          * first question, is the ifn we will emit on in our list, if so, we
 4319          * want that one.
 4320          */
 4321         vrf = sctp_find_vrf(vrf_id);
 4322         if (vrf == NULL)
 4323                 return (NULL);
 4324 
 4325         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 4326         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
 4327         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
 4328 
 4329         /*
 4330          * first question, is the ifn we will emit on in our list?  If so,
 4331          * we want that one. First we look for a preferred. Second, we go
 4332          * for an acceptable.
 4333          */
 4334         if (sctp_ifn) {
 4335                 /* first try for a preferred address on the ep */
 4336                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 4337                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
 4338                                 continue;
 4339                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
 4340                                 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
 4341                                 if (sifa == NULL)
 4342                                         continue;
 4343                                 if (((non_asoc_addr_ok == 0) &&
 4344                                     (sctp_is_addr_restricted(stcb, sifa))) ||
 4345                                     (non_asoc_addr_ok &&
 4346                                     (sctp_is_addr_restricted(stcb, sifa)) &&
 4347                                     (!sctp_is_addr_pending(stcb, sifa)))) {
 4348                                         /* on the no-no list */
 4349                                         continue;
 4350                                 }
 4351                                 atomic_add_int(&sifa->refcount, 1);
 4352                                 return (sifa);
 4353                         }
 4354                 }
 4355                 /* next try for an acceptable address on the ep */
 4356                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 4357                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
 4358                                 continue;
 4359                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
 4360                                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
 4361                                 if (sifa == NULL)
 4362                                         continue;
 4363                                 if (((non_asoc_addr_ok == 0) &&
 4364                                     (sctp_is_addr_restricted(stcb, sifa))) ||
 4365                                     (non_asoc_addr_ok &&
 4366                                     (sctp_is_addr_restricted(stcb, sifa)) &&
 4367                                     (!sctp_is_addr_pending(stcb, sifa)))) {
 4368                                         /* on the no-no list */
 4369                                         continue;
 4370                                 }
 4371                                 atomic_add_int(&sifa->refcount, 1);
 4372                                 return (sifa);
 4373                         }
 4374                 }
 4375 
 4376         }
 4377         /*
 4378          * if we can't find one like that then we must look at all addresses
 4379          * bound to pick one at first preferable then secondly acceptable.
 4380          */
 4381         starting_point = stcb->asoc.last_used_address;
 4382 sctp_from_the_top:
 4383         if (stcb->asoc.last_used_address == NULL) {
 4384                 start_at_beginning = 1;
 4385                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
 4386         }
 4387         /* search beginning with the last used address */
 4388         for (laddr = stcb->asoc.last_used_address; laddr;
 4389             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 4390                 if (laddr->ifa == NULL) {
 4391                         /* address has been removed */
 4392                         continue;
 4393                 }
 4394                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 4395                         /* address is being deleted */
 4396                         continue;
 4397                 }
 4398                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
 4399                 if (sifa == NULL)
 4400                         continue;
 4401                 if (((non_asoc_addr_ok == 0) &&
 4402                     (sctp_is_addr_restricted(stcb, sifa))) ||
 4403                     (non_asoc_addr_ok &&
 4404                     (sctp_is_addr_restricted(stcb, sifa)) &&
 4405                     (!sctp_is_addr_pending(stcb, sifa)))) {
 4406                         /* on the no-no list */
 4407                         continue;
 4408                 }
 4409                 stcb->asoc.last_used_address = laddr;
 4410                 atomic_add_int(&sifa->refcount, 1);
 4411                 return (sifa);
 4412         }
 4413         if (start_at_beginning == 0) {
 4414                 stcb->asoc.last_used_address = NULL;
 4415                 goto sctp_from_the_top;
 4416         }
 4417         /* now try for any higher scope than the destination */
 4418         stcb->asoc.last_used_address = starting_point;
 4419         start_at_beginning = 0;
 4420 sctp_from_the_top2:
 4421         if (stcb->asoc.last_used_address == NULL) {
 4422                 start_at_beginning = 1;
 4423                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
 4424         }
 4425         /* search beginning with the last used address */
 4426         for (laddr = stcb->asoc.last_used_address; laddr;
 4427             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
 4428                 if (laddr->ifa == NULL) {
 4429                         /* address has been removed */
 4430                         continue;
 4431                 }
 4432                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
 4433                         /* address is being deleted */
 4434                         continue;
 4435                 }
 4436                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
 4437                     dest_is_priv, fam);
 4438                 if (sifa == NULL)
 4439                         continue;
 4440                 if (((non_asoc_addr_ok == 0) &&
 4441                     (sctp_is_addr_restricted(stcb, sifa))) ||
 4442                     (non_asoc_addr_ok &&
 4443                     (sctp_is_addr_restricted(stcb, sifa)) &&
 4444                     (!sctp_is_addr_pending(stcb, sifa)))) {
 4445                         /* on the no-no list */
 4446                         continue;
 4447                 }
 4448                 stcb->asoc.last_used_address = laddr;
 4449                 atomic_add_int(&sifa->refcount, 1);
 4450                 return (sifa);
 4451         }
 4452         if (start_at_beginning == 0) {
 4453                 stcb->asoc.last_used_address = NULL;
 4454                 goto sctp_from_the_top2;
 4455         }
 4456         return (NULL);
 4457 }
 4458 
 4459 static struct sctp_ifa *
 4460 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
 4461     struct sctp_tcb *stcb,
 4462     int non_asoc_addr_ok,
 4463     uint8_t dest_is_loop,
 4464     uint8_t dest_is_priv,
 4465     int addr_wanted,
 4466     sa_family_t fam,
 4467     sctp_route_t * ro
 4468 )
 4469 {
 4470         struct sctp_ifa *ifa, *sifa;
 4471         int num_eligible_addr = 0;
 4472 
 4473 #ifdef INET6
 4474         struct sockaddr_in6 sin6, lsa6;
 4475 
 4476         if (fam == AF_INET6) {
 4477                 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
 4478                 (void)sa6_recoverscope(&sin6);
 4479         }
 4480 #endif                          /* INET6 */
 4481         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
 4482                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 4483                     (non_asoc_addr_ok == 0))
 4484                         continue;
 4485                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
 4486                     dest_is_priv, fam);
 4487                 if (sifa == NULL)
 4488                         continue;
 4489 #ifdef INET6
 4490                 if (fam == AF_INET6 &&
 4491                     dest_is_loop &&
 4492                     sifa->src_is_loop && sifa->src_is_priv) {
 4493                         /*
 4494                          * don't allow fe80::1 to be a src on loop ::1, we
 4495                          * don't list it to the peer so we will get an
 4496                          * abort.
 4497                          */
 4498                         continue;
 4499                 }
 4500                 if (fam == AF_INET6 &&
 4501                     IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
 4502                     IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
 4503                         /*
 4504                          * link-local <-> link-local must belong to the same
 4505                          * scope.
 4506                          */
 4507                         memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
 4508                         (void)sa6_recoverscope(&lsa6);
 4509                         if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
 4510                                 continue;
 4511                         }
 4512                 }
 4513 #endif                          /* INET6 */
 4514 
 4515                 /*
 4516                  * Check if the IPv6 address matches to next-hop. In the
 4517                  * mobile case, old IPv6 address may be not deleted from the
 4518                  * interface. Then, the interface has previous and new
 4519                  * addresses.  We should use one corresponding to the
 4520                  * next-hop.  (by micchie)
 4521                  */
 4522 #ifdef INET6
 4523                 if (stcb && fam == AF_INET6 &&
 4524                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
 4525                         if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
 4526                             == 0) {
 4527                                 continue;
 4528                         }
 4529                 }
 4530 #endif
 4531                 /* Avoid topologically incorrect IPv4 address */
 4532                 if (stcb && fam == AF_INET &&
 4533                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
 4534                         if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
 4535                                 continue;
 4536                         }
 4537                 }
 4538                 if (stcb) {
 4539                         if (((non_asoc_addr_ok == 0) &&
 4540                             (sctp_is_addr_restricted(stcb, sifa))) ||
 4541                             (non_asoc_addr_ok &&
 4542                             (sctp_is_addr_restricted(stcb, sifa)) &&
 4543                             (!sctp_is_addr_pending(stcb, sifa)))) {
 4544                                 /*
 4545                                  * It is restricted for some reason..
 4546                                  * probably not yet added.
 4547                                  */
 4548                                 continue;
 4549                         }
 4550                 }
 4551                 if (num_eligible_addr >= addr_wanted) {
 4552                         return (sifa);
 4553                 }
 4554                 num_eligible_addr++;
 4555         }
 4556         return (NULL);
 4557 }
 4558 
 4559 
 4560 static int
 4561 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
 4562     struct sctp_tcb *stcb,
 4563     int non_asoc_addr_ok,
 4564     uint8_t dest_is_loop,
 4565     uint8_t dest_is_priv,
 4566     sa_family_t fam)
 4567 {
 4568         struct sctp_ifa *ifa, *sifa;
 4569         int num_eligible_addr = 0;
 4570 
 4571         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
 4572                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 4573                     (non_asoc_addr_ok == 0)) {
 4574                         continue;
 4575                 }
 4576                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
 4577                     dest_is_priv, fam);
 4578                 if (sifa == NULL) {
 4579                         continue;
 4580                 }
 4581                 if (stcb) {
 4582                         if (((non_asoc_addr_ok == 0) &&
 4583                             (sctp_is_addr_restricted(stcb, sifa))) ||
 4584                             (non_asoc_addr_ok &&
 4585                             (sctp_is_addr_restricted(stcb, sifa)) &&
 4586                             (!sctp_is_addr_pending(stcb, sifa)))) {
 4587                                 /*
 4588                                  * It is restricted for some reason..
 4589                                  * probably not yet added.
 4590                                  */
 4591                                 continue;
 4592                         }
 4593                 }
 4594                 num_eligible_addr++;
 4595         }
 4596         return (num_eligible_addr);
 4597 }
 4598 
 4599 static struct sctp_ifa *
 4600 sctp_choose_boundall(struct sctp_inpcb *inp,
 4601     struct sctp_tcb *stcb,
 4602     struct sctp_nets *net,
 4603     sctp_route_t * ro,
 4604     uint32_t vrf_id,
 4605     uint8_t dest_is_priv,
 4606     uint8_t dest_is_loop,
 4607     int non_asoc_addr_ok,
 4608     sa_family_t fam)
 4609 {
 4610         int cur_addr_num = 0, num_preferred = 0;
 4611         void *ifn;
 4612         struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
 4613         struct sctp_ifa *sctp_ifa, *sifa;
 4614         uint32_t ifn_index;
 4615         struct sctp_vrf *vrf;
 4616 
 4617         /*-
 4618          * For boundall we can use any address in the association.
 4619          * If non_asoc_addr_ok is set we can use any address (at least in
 4620          * theory). So we look for preferred addresses first. If we find one,
 4621          * we use it. Otherwise we next try to get an address on the
 4622          * interface, which we should be able to do (unless non_asoc_addr_ok
 4623          * is false and we are routed out that way). In these cases where we
 4624          * can't use the address of the interface we go through all the
 4625          * ifn's looking for an address we can use and fill that in. Punting
 4626          * means we send back address 0, which will probably cause problems
 4627          * actually since then IP will fill in the address of the route ifn,
 4628          * which means we probably already rejected it.. i.e. here comes an
 4629          * abort :-<.
 4630          */
 4631         vrf = sctp_find_vrf(vrf_id);
 4632         if (vrf == NULL)
 4633                 return (NULL);
 4634 
 4635         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 4636         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
 4637         emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
 4638         if (sctp_ifn == NULL) {
 4639                 /* ?? We don't have this guy ?? */
 4640                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
 4641                 goto bound_all_plan_b;
 4642         }
 4643         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
 4644             ifn_index, sctp_ifn->ifn_name);
 4645 
 4646         if (net) {
 4647                 cur_addr_num = net->indx_of_eligible_next_to_use;
 4648         }
 4649         num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
 4650             stcb,
 4651             non_asoc_addr_ok,
 4652             dest_is_loop,
 4653             dest_is_priv, fam);
 4654         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
 4655             num_preferred, sctp_ifn->ifn_name);
 4656         if (num_preferred == 0) {
 4657                 /*
 4658                  * no eligible addresses, we must use some other interface
 4659                  * address if we can find one.
 4660                  */
 4661                 goto bound_all_plan_b;
 4662         }
 4663         /*
 4664          * Ok we have num_eligible_addr set with how many we can use, this
 4665          * may vary from call to call due to addresses being deprecated
 4666          * etc..
 4667          */
 4668         if (cur_addr_num >= num_preferred) {
 4669                 cur_addr_num = 0;
 4670         }
 4671         /*
 4672          * select the nth address from the list (where cur_addr_num is the
 4673          * nth) and 0 is the first one, 1 is the second one etc...
 4674          */
 4675         SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
 4676 
 4677         sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
 4678             dest_is_priv, cur_addr_num, fam, ro);
 4679 
 4680         /* if sctp_ifa is NULL something changed??, fall to plan b. */
 4681         if (sctp_ifa) {
 4682                 atomic_add_int(&sctp_ifa->refcount, 1);
 4683                 if (net) {
 4684                         /* save off where the next one we will want */
 4685                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
 4686                 }
 4687                 return (sctp_ifa);
 4688         }
 4689         /*
 4690          * plan_b: Look at all interfaces and find a preferred address. If
 4691          * no preferred fall through to plan_c.
 4692          */
 4693 bound_all_plan_b:
 4694         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
 4695         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
 4696                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
 4697                     sctp_ifn->ifn_name);
 4698                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
 4699                         /* wrong base scope */
 4700                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
 4701                         continue;
 4702                 }
 4703                 if ((sctp_ifn == looked_at) && looked_at) {
 4704                         /* already looked at this guy */
 4705                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
 4706                         continue;
 4707                 }
 4708                 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
 4709                     dest_is_loop, dest_is_priv, fam);
 4710                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
 4711                     "Found ifn:%p %d preferred source addresses\n",
 4712                     ifn, num_preferred);
 4713                 if (num_preferred == 0) {
 4714                         /* None on this interface. */
 4715                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
 4716                         continue;
 4717                 }
 4718                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
 4719                     "num preferred:%d on interface:%p cur_addr_num:%d\n",
 4720                     num_preferred, sctp_ifn, cur_addr_num);
 4721 
 4722                 /*
 4723                  * Ok we have num_eligible_addr set with how many we can
 4724                  * use, this may vary from call to call due to addresses
 4725                  * being deprecated etc..
 4726                  */
 4727                 if (cur_addr_num >= num_preferred) {
 4728                         cur_addr_num = 0;
 4729                 }
 4730                 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
 4731                     dest_is_priv, cur_addr_num, fam, ro);
 4732                 if (sifa == NULL)
 4733                         continue;
 4734                 if (net) {
 4735                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
 4736                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
 4737                             cur_addr_num);
 4738                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
 4739                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
 4740                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
 4741                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
 4742                 }
 4743                 atomic_add_int(&sifa->refcount, 1);
 4744                 return (sifa);
 4745 
 4746         }
 4747 
 4748         /* plan_c: do we have an acceptable address on the emit interface */
 4749         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
 4750         if (emit_ifn == NULL) {
 4751                 goto plan_d;
 4752         }
 4753         LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
 4754                 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 4755                     (non_asoc_addr_ok == 0))
 4756                         continue;
 4757                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
 4758                     dest_is_priv, fam);
 4759                 if (sifa == NULL)
 4760                         continue;
 4761                 if (stcb) {
 4762                         if (((non_asoc_addr_ok == 0) &&
 4763                             (sctp_is_addr_restricted(stcb, sifa))) ||
 4764                             (non_asoc_addr_ok &&
 4765                             (sctp_is_addr_restricted(stcb, sifa)) &&
 4766                             (!sctp_is_addr_pending(stcb, sifa)))) {
 4767                                 /*
 4768                                  * It is restricted for some reason..
 4769                                  * probably not yet added.
 4770                                  */
 4771                                 continue;
 4772                         }
 4773                 }
 4774                 atomic_add_int(&sifa->refcount, 1);
 4775                 return (sifa);
 4776         }
 4777 plan_d:
 4778         /*
 4779          * plan_d: We are in trouble. No preferred address on the emit
 4780          * interface. And not even a preferred address on all interfaces. Go
 4781          * out and see if we can find an acceptable address somewhere
 4782          * amongst all interfaces.
 4783          */
 4784         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
 4785         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
 4786                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
 4787                         /* wrong base scope */
 4788                         continue;
 4789                 }
 4790                 if ((sctp_ifn == looked_at) && looked_at)
 4791                         /* already looked at this guy */
 4792                         continue;
 4793 
 4794                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 4795                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
 4796                             (non_asoc_addr_ok == 0))
 4797                                 continue;
 4798                         sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
 4799                             dest_is_loop,
 4800                             dest_is_priv, fam);
 4801                         if (sifa == NULL)
 4802                                 continue;
 4803                         if (stcb) {
 4804                                 if (((non_asoc_addr_ok == 0) &&
 4805                                     (sctp_is_addr_restricted(stcb, sifa))) ||
 4806                                     (non_asoc_addr_ok &&
 4807                                     (sctp_is_addr_restricted(stcb, sifa)) &&
 4808                                     (!sctp_is_addr_pending(stcb, sifa)))) {
 4809                                         /*
 4810                                          * It is restricted for some
 4811                                          * reason.. probably not yet added.
 4812                                          */
 4813                                         continue;
 4814                                 }
 4815                         }
 4816                         atomic_add_int(&sifa->refcount, 1);
 4817                         return (sifa);
 4818                 }
 4819         }
 4820         /*
 4821          * Ok we can find NO address to source from that is not on our
 4822          * restricted list and non_asoc_address is NOT ok, or it is on our
 4823          * restricted list. We can't source to it :-(
 4824          */
 4825         return (NULL);
 4826 }
 4827 
 4828 
 4829 
 4830 /* tcb may be NULL */
 4831 struct sctp_ifa *
 4832 sctp_source_address_selection(struct sctp_inpcb *inp,
 4833     struct sctp_tcb *stcb,
 4834     sctp_route_t * ro,
 4835     struct sctp_nets *net,
 4836     int non_asoc_addr_ok, uint32_t vrf_id)
 4837 {
 4838         struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
 4839 
 4840 #ifdef INET6
 4841         struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
 4842 
 4843 #endif
 4844         struct sctp_ifa *answer;
 4845         uint8_t dest_is_priv, dest_is_loop;
 4846         sa_family_t fam;
 4847 
 4848         /*-
 4849          * Rules: - Find the route if needed, cache if I can. - Look at
 4850          * interface address in route, Is it in the bound list. If so we
 4851          * have the best source. - If not we must rotate amongst the
 4852          * addresses.
 4853          *
 4854          * Cavets and issues
 4855          *
 4856          * Do we need to pay attention to scope. We can have a private address
 4857          * or a global address we are sourcing or sending to. So if we draw
 4858          * it out
 4859          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
 4860          * For V4
 4861          *------------------------------------------
 4862          *      source     *      dest  *  result
 4863          * -----------------------------------------
 4864          * <a>  Private    *    Global  *  NAT
 4865          * -----------------------------------------
 4866          * <b>  Private    *    Private *  No problem
 4867          * -----------------------------------------
 4868          * <c>  Global     *    Private *  Huh, How will this work?
 4869          * -----------------------------------------
 4870          * <d>  Global     *    Global  *  No Problem
 4871          *------------------------------------------
 4872          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
 4873          * For V6
 4874          *------------------------------------------
 4875          *      source     *      dest  *  result
 4876          * -----------------------------------------
 4877          * <a>  Linklocal  *    Global  *
 4878          * -----------------------------------------
 4879          * <b>  Linklocal  * Linklocal  *  No problem
 4880          * -----------------------------------------
 4881          * <c>  Global     * Linklocal  *  Huh, How will this work?
 4882          * -----------------------------------------
 4883          * <d>  Global     *    Global  *  No Problem
 4884          *------------------------------------------
 4885          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
 4886          *
 4887          * And then we add to that what happens if there are multiple addresses
 4888          * assigned to an interface. Remember the ifa on a ifn is a linked
 4889          * list of addresses. So one interface can have more than one IP
 4890          * address. What happens if we have both a private and a global
 4891          * address? Do we then use context of destination to sort out which
 4892          * one is best? And what about NAT's sending P->G may get you a NAT
 4893          * translation, or should you select the G thats on the interface in
 4894          * preference.
 4895          *
 4896          * Decisions:
 4897          *
 4898          * - count the number of addresses on the interface.
 4899          * - if it is one, no problem except case <c>.
 4900          *   For <a> we will assume a NAT out there.
 4901          * - if there are more than one, then we need to worry about scope P
 4902          *   or G. We should prefer G -> G and P -> P if possible.
 4903          *   Then as a secondary fall back to mixed types G->P being a last
 4904          *   ditch one.
 4905          * - The above all works for bound all, but bound specific we need to
 4906          *   use the same concept but instead only consider the bound
 4907          *   addresses. If the bound set is NOT assigned to the interface then
 4908          *   we must use rotation amongst the bound addresses..
 4909          */
 4910         if (ro->ro_rt == NULL) {
 4911                 /*
 4912                  * Need a route to cache.
 4913                  */
 4914                 SCTP_RTALLOC(ro, vrf_id);
 4915         }
 4916         if (ro->ro_rt == NULL) {
 4917                 return (NULL);
 4918         }
 4919         fam = to->sin_family;
 4920         dest_is_priv = dest_is_loop = 0;
 4921         /* Setup our scopes for the destination */
 4922         switch (fam) {
 4923         case AF_INET:
 4924                 /* Scope based on outbound address */
 4925                 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
 4926                         dest_is_loop = 1;
 4927                         if (net != NULL) {
 4928                                 /* mark it as local */
 4929                                 net->addr_is_local = 1;
 4930                         }
 4931                 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
 4932                         dest_is_priv = 1;
 4933                 }
 4934                 break;
 4935 #ifdef INET6
 4936         case AF_INET6:
 4937                 /* Scope based on outbound address */
 4938                 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
 4939                     SCTP_ROUTE_IS_REAL_LOOP(ro)) {
 4940                         /*
 4941                          * If the address is a loopback address, which
 4942                          * consists of "::1" OR "fe80::1%lo0", we are
 4943                          * loopback scope. But we don't use dest_is_priv
 4944                          * (link local addresses).
 4945                          */
 4946                         dest_is_loop = 1;
 4947                         if (net != NULL) {
 4948                                 /* mark it as local */
 4949                                 net->addr_is_local = 1;
 4950                         }
 4951                 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
 4952                         dest_is_priv = 1;
 4953                 }
 4954                 break;
 4955 #endif
 4956         }
 4957         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
 4958         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
 4959         SCTP_IPI_ADDR_RLOCK();
 4960         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 4961                 /*
 4962                  * Bound all case
 4963                  */
 4964                 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
 4965                     dest_is_priv, dest_is_loop,
 4966                     non_asoc_addr_ok, fam);
 4967                 SCTP_IPI_ADDR_RUNLOCK();
 4968                 return (answer);
 4969         }
 4970         /*
 4971          * Subset bound case
 4972          */
 4973         if (stcb) {
 4974                 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
 4975                     vrf_id, dest_is_priv,
 4976                     dest_is_loop,
 4977                     non_asoc_addr_ok, fam);
 4978         } else {
 4979                 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
 4980                     non_asoc_addr_ok,
 4981                     dest_is_priv,
 4982                     dest_is_loop, fam);
 4983         }
 4984         SCTP_IPI_ADDR_RUNLOCK();
 4985         return (answer);
 4986 }
 4987 
 4988 static int
 4989 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
 4990 {
 4991         struct cmsghdr cmh;
 4992         int tlen, at;
 4993 
 4994         tlen = SCTP_BUF_LEN(control);
 4995         at = 0;
 4996         /*
 4997          * Independent of how many mbufs, find the c_type inside the control
 4998          * structure and copy out the data.
 4999          */
 5000         while (at < tlen) {
 5001                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
 5002                         /* not enough room for one more we are done. */
 5003                         return (0);
 5004                 }
 5005                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
 5006                 if (((int)cmh.cmsg_len + at) > tlen) {
 5007                         /*
 5008                          * this is real messed up since there is not enough
 5009                          * data here to cover the cmsg header. We are done.
 5010                          */
 5011                         return (0);
 5012                 }
 5013                 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
 5014                     (c_type == cmh.cmsg_type)) {
 5015                         /* found the one we want, copy it out */
 5016                         at += CMSG_ALIGN(sizeof(struct cmsghdr));
 5017                         if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
 5018                                 /*
 5019                                  * space of cmsg_len after header not big
 5020                                  * enough
 5021                                  */
 5022                                 return (0);
 5023                         }
 5024                         m_copydata(control, at, cpsize, data);
 5025                         return (1);
 5026                 } else {
 5027                         at += CMSG_ALIGN(cmh.cmsg_len);
 5028                         if (cmh.cmsg_len == 0) {
 5029                                 break;
 5030                         }
 5031                 }
 5032         }
 5033         /* not found */
 5034         return (0);
 5035 }
 5036 
 5037 static struct mbuf *
 5038 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
 5039     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
 5040 {
 5041         struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
 5042         struct sctp_state_cookie *stc;
 5043         struct sctp_paramhdr *ph;
 5044         uint8_t *foo;
 5045         int sig_offset;
 5046         uint16_t cookie_sz;
 5047 
 5048         mret = NULL;
 5049         mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
 5050             sizeof(struct sctp_paramhdr)), 0,
 5051             M_DONTWAIT, 1, MT_DATA);
 5052         if (mret == NULL) {
 5053                 return (NULL);
 5054         }
 5055         copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
 5056         if (copy_init == NULL) {
 5057                 sctp_m_freem(mret);
 5058                 return (NULL);
 5059         }
 5060 #ifdef SCTP_MBUF_LOGGING
 5061         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 5062                 struct mbuf *mat;
 5063 
 5064                 mat = copy_init;
 5065                 while (mat) {
 5066                         if (SCTP_BUF_IS_EXTENDED(mat)) {
 5067                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 5068                         }
 5069                         mat = SCTP_BUF_NEXT(mat);
 5070                 }
 5071         }
 5072 #endif
 5073         copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
 5074             M_DONTWAIT);
 5075         if (copy_initack == NULL) {
 5076                 sctp_m_freem(mret);
 5077                 sctp_m_freem(copy_init);
 5078                 return (NULL);
 5079         }
 5080 #ifdef SCTP_MBUF_LOGGING
 5081         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
 5082                 struct mbuf *mat;
 5083 
 5084                 mat = copy_initack;
 5085                 while (mat) {
 5086                         if (SCTP_BUF_IS_EXTENDED(mat)) {
 5087                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
 5088                         }
 5089                         mat = SCTP_BUF_NEXT(mat);
 5090                 }
 5091         }
 5092 #endif
 5093         /* easy side we just drop it on the end */
 5094         ph = mtod(mret, struct sctp_paramhdr *);
 5095         SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
 5096             sizeof(struct sctp_paramhdr);
 5097         stc = (struct sctp_state_cookie *)((caddr_t)ph +
 5098             sizeof(struct sctp_paramhdr));
 5099         ph->param_type = htons(SCTP_STATE_COOKIE);
 5100         ph->param_length = 0;   /* fill in at the end */
 5101         /* Fill in the stc cookie data */
 5102         memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
 5103 
 5104         /* tack the INIT and then the INIT-ACK onto the chain */
 5105         cookie_sz = 0;
 5106         m_at = mret;
 5107         for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 5108                 cookie_sz += SCTP_BUF_LEN(m_at);
 5109                 if (SCTP_BUF_NEXT(m_at) == NULL) {
 5110                         SCTP_BUF_NEXT(m_at) = copy_init;
 5111                         break;
 5112                 }
 5113         }
 5114 
 5115         for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 5116                 cookie_sz += SCTP_BUF_LEN(m_at);
 5117                 if (SCTP_BUF_NEXT(m_at) == NULL) {
 5118                         SCTP_BUF_NEXT(m_at) = copy_initack;
 5119                         break;
 5120                 }
 5121         }
 5122 
 5123         for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 5124                 cookie_sz += SCTP_BUF_LEN(m_at);
 5125                 if (SCTP_BUF_NEXT(m_at) == NULL) {
 5126                         break;
 5127                 }
 5128         }
 5129         sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
 5130         if (sig == NULL) {
 5131                 /* no space, so free the entire chain */
 5132                 sctp_m_freem(mret);
 5133                 return (NULL);
 5134         }
 5135         SCTP_BUF_LEN(sig) = 0;
 5136         SCTP_BUF_NEXT(m_at) = sig;
 5137         sig_offset = 0;
 5138         foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
 5139         memset(foo, 0, SCTP_SIGNATURE_SIZE);
 5140         *signature = foo;
 5141         SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
 5142         cookie_sz += SCTP_SIGNATURE_SIZE;
 5143         ph->param_length = htons(cookie_sz);
 5144         return (mret);
 5145 }
 5146 
 5147 
 5148 static uint8_t
 5149 sctp_get_ect(struct sctp_tcb *stcb,
 5150     struct sctp_tmit_chunk *chk)
 5151 {
 5152         uint8_t this_random;
 5153 
 5154         /* Huh? */
 5155         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
 5156                 return (0);
 5157 
 5158         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
 5159                 /* no nonce, always return ECT0 */
 5160                 return (SCTP_ECT0_BIT);
 5161 
 5162         if (stcb->asoc.peer_supports_ecn_nonce == 0) {
 5163                 /* Peer does NOT support it, so we send a ECT0 only */
 5164                 return (SCTP_ECT0_BIT);
 5165         }
 5166         if (chk == NULL)
 5167                 return (SCTP_ECT0_BIT);
 5168 
 5169         if ((stcb->asoc.hb_random_idx > 3) ||
 5170             ((stcb->asoc.hb_random_idx == 3) &&
 5171             (stcb->asoc.hb_ect_randombit > 7))) {
 5172                 uint32_t rndval;
 5173 
 5174 warp_drive_sa:
 5175                 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
 5176                 memcpy(stcb->asoc.hb_random_values, &rndval,
 5177                     sizeof(stcb->asoc.hb_random_values));
 5178                 this_random = stcb->asoc.hb_random_values[0];
 5179                 stcb->asoc.hb_random_idx = 0;
 5180                 stcb->asoc.hb_ect_randombit = 0;
 5181         } else {
 5182                 if (stcb->asoc.hb_ect_randombit > 7) {
 5183                         stcb->asoc.hb_ect_randombit = 0;
 5184                         stcb->asoc.hb_random_idx++;
 5185                         if (stcb->asoc.hb_random_idx > 3) {
 5186                                 goto warp_drive_sa;
 5187                         }
 5188                 }
 5189                 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
 5190         }
 5191         if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
 5192                 if (chk != NULL)
 5193                         /* ECN Nonce stuff */
 5194                         chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
 5195                 stcb->asoc.hb_ect_randombit++;
 5196                 return (SCTP_ECT1_BIT);
 5197         } else {
 5198                 stcb->asoc.hb_ect_randombit++;
 5199                 return (SCTP_ECT0_BIT);
 5200         }
 5201 }
 5202 
 5203 static int
 5204 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
 5205     struct sctp_tcb *stcb,      /* may be NULL */
 5206     struct sctp_nets *net,
 5207     struct sockaddr *to,
 5208     struct mbuf *m,
 5209     uint32_t auth_offset,
 5210     struct sctp_auth_chunk *auth,
 5211     uint16_t auth_keyid,
 5212     int nofragment_flag,
 5213     int ecn_ok,
 5214     struct sctp_tmit_chunk *chk,
 5215     int out_of_asoc_ok,
 5216     uint16_t port,
 5217     int so_locked,
 5218 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 5219     SCTP_UNUSED
 5220 #endif
 5221     union sctp_sockstore *over_addr
 5222 )
 5223 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
 5224 {
 5225         /*
 5226          * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
 5227          * header WITH an SCTPHDR but no IP header, endpoint inp and sa
 5228          * structure: - fill in the HMAC digest of any AUTH chunk in the
 5229          * packet. - calculate and fill in the SCTP checksum. - prepend an
 5230          * IP address header. - if boundall use INADDR_ANY. - if
 5231          * boundspecific do source address selection. - set fragmentation
 5232          * option for ipV4. - On return from IP output, check/adjust mtu
 5233          * size of output interface and smallest_mtu size as well.
 5234          */
 5235         /* Will need ifdefs around this */
 5236         struct mbuf *o_pak;
 5237         struct mbuf *newm;
 5238         struct sctphdr *sctphdr;
 5239         int packet_length;
 5240         uint32_t csum;
 5241         int ret;
 5242         uint32_t vrf_id;
 5243         sctp_route_t *ro = NULL;
 5244         struct udphdr *udp = NULL;
 5245 
 5246 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5247         struct socket *so = NULL;
 5248 
 5249 #endif
 5250 
 5251         if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
 5252                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
 5253                 sctp_m_freem(m);
 5254                 return (EFAULT);
 5255         }
 5256         if (stcb) {
 5257                 vrf_id = stcb->asoc.vrf_id;
 5258         } else {
 5259                 vrf_id = inp->def_vrf_id;
 5260         }
 5261 
 5262         /* fill in the HMAC digest for any AUTH chunk in the packet */
 5263         if ((auth != NULL) && (stcb != NULL)) {
 5264                 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
 5265         }
 5266         /* Calculate the csum and fill in the length of the packet */
 5267         sctphdr = mtod(m, struct sctphdr *);
 5268         if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
 5269             (stcb) &&
 5270             (to->sa_family == AF_INET) &&
 5271             (stcb->asoc.loopback_scope)) {
 5272                 sctphdr->checksum = 0;
 5273                 /*
 5274                  * This can probably now be taken out since my audit shows
 5275                  * no more bad pktlen's coming in. But we will wait a while
 5276                  * yet.
 5277                  */
 5278                 packet_length = sctp_calculate_len(m);
 5279         } else {
 5280                 sctphdr->checksum = 0;
 5281                 csum = sctp_calculate_sum(m, &packet_length, 0);
 5282                 sctphdr->checksum = csum;
 5283         }
 5284 
 5285         if (to->sa_family == AF_INET) {
 5286                 struct ip *ip = NULL;
 5287                 sctp_route_t iproute;
 5288                 uint8_t tos_value;
 5289 
 5290                 if (port) {
 5291                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
 5292                 } else {
 5293                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
 5294                 }
 5295                 if (newm == NULL) {
 5296                         sctp_m_freem(m);
 5297                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 5298                         return (ENOMEM);
 5299                 }
 5300                 if (port) {
 5301                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip) + sizeof(struct udphdr));
 5302                         SCTP_BUF_LEN(newm) = sizeof(struct ip) + sizeof(struct udphdr);
 5303                         packet_length += sizeof(struct ip) + sizeof(struct udphdr);
 5304                 } else {
 5305                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
 5306                         SCTP_BUF_LEN(newm) = sizeof(struct ip);
 5307                         packet_length += sizeof(struct ip);
 5308                 }
 5309                 SCTP_BUF_NEXT(newm) = m;
 5310                 m = newm;
 5311                 ip = mtod(m, struct ip *);
 5312                 ip->ip_v = IPVERSION;
 5313                 ip->ip_hl = (sizeof(struct ip) >> 2);
 5314                 if (net) {
 5315                         tos_value = net->tos_flowlabel & 0x000000ff;
 5316                 } else {
 5317                         tos_value = inp->ip_inp.inp.inp_ip_tos;
 5318                 }
 5319                 if ((nofragment_flag) && (port == 0)) {
 5320 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
 5321                         ip->ip_off = IP_DF;
 5322 #else
 5323                         ip->ip_off = htons(IP_DF);
 5324 #endif
 5325                 } else
 5326                         ip->ip_off = 0;
 5327 
 5328                 /* FreeBSD has a function for ip_id's */
 5329                 ip->ip_id = ip_newid();
 5330 
 5331                 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
 5332                 ip->ip_len = packet_length;
 5333                 if (stcb) {
 5334                         if ((stcb->asoc.ecn_allowed) && ecn_ok) {
 5335                                 /* Enable ECN */
 5336                                 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
 5337                         } else {
 5338                                 /* No ECN */
 5339                                 ip->ip_tos = (u_char)(tos_value & 0xfc);
 5340                         }
 5341                 } else {
 5342                         /* no association at all */
 5343                         ip->ip_tos = (tos_value & 0xfc);
 5344                 }
 5345                 if (port) {
 5346                         ip->ip_p = IPPROTO_UDP;
 5347                 } else {
 5348                         ip->ip_p = IPPROTO_SCTP;
 5349                 }
 5350                 ip->ip_sum = 0;
 5351                 if (net == NULL) {
 5352                         ro = &iproute;
 5353                         memset(&iproute, 0, sizeof(iproute));
 5354                         memcpy(&ro->ro_dst, to, to->sa_len);
 5355                 } else {
 5356                         ro = (sctp_route_t *) & net->ro;
 5357                 }
 5358                 /* Now the address selection part */
 5359                 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
 5360 
 5361                 /* call the routine to select the src address */
 5362                 if (net && out_of_asoc_ok == 0) {
 5363                         if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
 5364                                 sctp_free_ifa(net->ro._s_addr);
 5365                                 net->ro._s_addr = NULL;
 5366                                 net->src_addr_selected = 0;
 5367                                 if (ro->ro_rt) {
 5368                                         RTFREE(ro->ro_rt);
 5369                                         ro->ro_rt = NULL;
 5370                                 }
 5371                         }
 5372                         if (net->src_addr_selected == 0) {
 5373                                 /* Cache the source address */
 5374                                 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
 5375                                     ro, net, 0,
 5376                                     vrf_id);
 5377                                 net->src_addr_selected = 1;
 5378                         }
 5379                         if (net->ro._s_addr == NULL) {
 5380                                 /* No route to host */
 5381                                 net->src_addr_selected = 0;
 5382                                 goto no_route;
 5383                         }
 5384                         ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
 5385                 } else {
 5386                         if (over_addr == NULL) {
 5387                                 struct sctp_ifa *_lsrc;
 5388 
 5389                                 _lsrc = sctp_source_address_selection(inp, stcb, ro,
 5390                                     net,
 5391                                     out_of_asoc_ok,
 5392                                     vrf_id);
 5393                                 if (_lsrc == NULL) {
 5394                                         goto no_route;
 5395                                 }
 5396                                 ip->ip_src = _lsrc->address.sin.sin_addr;
 5397                                 sctp_free_ifa(_lsrc);
 5398                         } else {
 5399                                 ip->ip_src = over_addr->sin.sin_addr;
 5400                                 SCTP_RTALLOC(ro, vrf_id);
 5401                         }
 5402                 }
 5403                 if (port) {
 5404                         udp = (struct udphdr *)(ip + 1);
 5405                         udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
 5406                         udp->uh_dport = port;
 5407                         udp->uh_ulen = htons(packet_length - sizeof(struct ip));
 5408                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
 5409                 }
 5410                 /*
 5411                  * If source address selection fails and we find no route
 5412                  * then the ip_output should fail as well with a
 5413                  * NO_ROUTE_TO_HOST type error. We probably should catch
 5414                  * that somewhere and abort the association right away
 5415                  * (assuming this is an INIT being sent).
 5416                  */
 5417                 if ((ro->ro_rt == NULL)) {
 5418                         /*
 5419                          * src addr selection failed to find a route (or
 5420                          * valid source addr), so we can't get there from
 5421                          * here (yet)!
 5422                          */
 5423         no_route:
 5424                         SCTPDBG(SCTP_DEBUG_OUTPUT1,
 5425                             "%s: dropped packet - no valid source addr\n",
 5426                             __FUNCTION__);
 5427                         if (net) {
 5428                                 SCTPDBG(SCTP_DEBUG_OUTPUT1,
 5429                                     "Destination was ");
 5430                                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
 5431                                     &net->ro._l_addr.sa);
 5432                                 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
 5433                                         if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
 5434                                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
 5435                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
 5436                                                     stcb,
 5437                                                     SCTP_FAILED_THRESHOLD,
 5438                                                     (void *)net,
 5439                                                     so_locked);
 5440                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
 5441                                                 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
 5442                                                 /*
 5443                                                  * JRS 5/14/07 - If a
 5444                                                  * destination is
 5445                                                  * unreachable, the PF bit
 5446                                                  * is turned off.  This
 5447                                                  * allows an unambiguous use
 5448                                                  * of the PF bit for
 5449                                                  * destinations that are
 5450                                                  * reachable but potentially
 5451                                                  * failed. If the
 5452                                                  * destination is set to the
 5453                                                  * unreachable state, also
 5454                                                  * set the destination to
 5455                                                  * the PF state.
 5456                                                  */
 5457                                                 /*
 5458                                                  * Add debug message here if
 5459                                                  * destination is not in PF
 5460                                                  * state.
 5461                                                  */
 5462                                                 /*
 5463                                                  * Stop any running T3
 5464                                                  * timers here?
 5465                                                  */
 5466                                                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
 5467                                                         net->dest_state &= ~SCTP_ADDR_PF;
 5468                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
 5469                                                             net);
 5470                                                 }
 5471                                         }
 5472                                 }
 5473                                 if (stcb) {
 5474                                         if (net == stcb->asoc.primary_destination) {
 5475                                                 /* need a new primary */
 5476                                                 struct sctp_nets *alt;
 5477 
 5478                                                 alt = sctp_find_alternate_net(stcb, net, 0);
 5479                                                 if (alt != net) {
 5480                                                         if (sctp_set_primary_addr(stcb,
 5481                                                             (struct sockaddr *)NULL,
 5482                                                             alt) == 0) {
 5483                                                                 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
 5484                                                                 if (net->ro._s_addr) {
 5485                                                                         sctp_free_ifa(net->ro._s_addr);
 5486                                                                         net->ro._s_addr = NULL;
 5487                                                                 }
 5488                                                                 net->src_addr_selected = 0;
 5489                                                         }
 5490                                                 }
 5491                                         }
 5492                                 }
 5493                         }
 5494                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
 5495                         sctp_m_freem(m);
 5496                         return (EHOSTUNREACH);
 5497                 }
 5498                 if (ro != &iproute) {
 5499                         memcpy(&iproute, ro, sizeof(*ro));
 5500                 }
 5501                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
 5502                     (uint32_t) (ntohl(ip->ip_src.s_addr)));
 5503                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
 5504                     (uint32_t) (ntohl(ip->ip_dst.s_addr)));
 5505                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
 5506                     ro->ro_rt);
 5507 
 5508                 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
 5509                         /* failed to prepend data, give up */
 5510                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 5511                         sctp_m_freem(m);
 5512                         return (ENOMEM);
 5513                 }
 5514 #ifdef  SCTP_PACKET_LOGGING
 5515                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
 5516                         sctp_packet_log(m, packet_length);
 5517 #endif
 5518                 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
 5519                 if (port) {
 5520                         SCTP_ENABLE_UDP_CSUM(o_pak);
 5521                 }
 5522                 /* send it out.  table id is taken from stcb */
 5523 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5524                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 5525                         so = SCTP_INP_SO(inp);
 5526                         SCTP_SOCKET_UNLOCK(so, 0);
 5527                 }
 5528 #endif
 5529                 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
 5530 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5531                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 5532                         atomic_add_int(&stcb->asoc.refcnt, 1);
 5533                         SCTP_TCB_UNLOCK(stcb);
 5534                         SCTP_SOCKET_LOCK(so, 0);
 5535                         SCTP_TCB_LOCK(stcb);
 5536                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 5537                 }
 5538 #endif
 5539                 SCTP_STAT_INCR(sctps_sendpackets);
 5540                 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
 5541                 if (ret)
 5542                         SCTP_STAT_INCR(sctps_senderrors);
 5543 
 5544                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
 5545                 if (net == NULL) {
 5546                         /* free tempy routes */
 5547                         if (ro->ro_rt) {
 5548                                 RTFREE(ro->ro_rt);
 5549                                 ro->ro_rt = NULL;
 5550                         }
 5551                 } else {
 5552                         /* PMTU check versus smallest asoc MTU goes here */
 5553                         if ((ro->ro_rt != NULL) &&
 5554                             (net->ro._s_addr)) {
 5555                                 uint32_t mtu;
 5556 
 5557                                 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
 5558                                 if (net->port) {
 5559                                         mtu -= sizeof(struct udphdr);
 5560                                 }
 5561                                 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
 5562 #ifdef SCTP_PRINT_FOR_B_AND_M
 5563                                         SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
 5564 #endif
 5565                                         sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
 5566                                         net->mtu = mtu;
 5567                                 }
 5568                         } else if (ro->ro_rt == NULL) {
 5569                                 /* route was freed */
 5570                                 if (net->ro._s_addr &&
 5571                                     net->src_addr_selected) {
 5572                                         sctp_free_ifa(net->ro._s_addr);
 5573                                         net->ro._s_addr = NULL;
 5574                                 }
 5575                                 net->src_addr_selected = 0;
 5576                         }
 5577                 }
 5578                 return (ret);
 5579         }
 5580 #ifdef INET6
 5581         else if (to->sa_family == AF_INET6) {
 5582                 uint32_t flowlabel;
 5583                 struct ip6_hdr *ip6h;
 5584                 struct route_in6 ip6route;
 5585                 struct ifnet *ifp;
 5586                 u_char flowTop;
 5587                 uint16_t flowBottom;
 5588                 u_char tosBottom, tosTop;
 5589                 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
 5590                 int prev_scope = 0;
 5591                 struct sockaddr_in6 lsa6_storage;
 5592                 int error;
 5593                 u_short prev_port = 0;
 5594 
 5595                 if (net != NULL) {
 5596                         flowlabel = net->tos_flowlabel;
 5597                 } else {
 5598                         flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
 5599                 }
 5600 
 5601                 if (port) {
 5602                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
 5603                 } else {
 5604                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
 5605                 }
 5606                 if (newm == NULL) {
 5607                         sctp_m_freem(m);
 5608                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 5609                         return (ENOMEM);
 5610                 }
 5611                 if (port) {
 5612                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
 5613                         SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
 5614                         packet_length += sizeof(struct ip6_hdr) + sizeof(struct udphdr);
 5615                 } else {
 5616                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
 5617                         SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
 5618                         packet_length += sizeof(struct ip6_hdr);
 5619                 }
 5620                 SCTP_BUF_NEXT(newm) = m;
 5621                 m = newm;
 5622 
 5623                 ip6h = mtod(m, struct ip6_hdr *);
 5624                 /*
 5625                  * We assume here that inp_flow is in host byte order within
 5626                  * the TCB!
 5627                  */
 5628                 flowBottom = flowlabel & 0x0000ffff;
 5629                 flowTop = ((flowlabel & 0x000f0000) >> 16);
 5630                 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
 5631                 /* protect *sin6 from overwrite */
 5632                 sin6 = (struct sockaddr_in6 *)to;
 5633                 tmp = *sin6;
 5634                 sin6 = &tmp;
 5635 
 5636                 /* KAME hack: embed scopeid */
 5637                 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
 5638                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 5639                         return (EINVAL);
 5640                 }
 5641                 if (net == NULL) {
 5642                         memset(&ip6route, 0, sizeof(ip6route));
 5643                         ro = (sctp_route_t *) & ip6route;
 5644                         memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
 5645                 } else {
 5646                         ro = (sctp_route_t *) & net->ro;
 5647                 }
 5648                 if (stcb != NULL) {
 5649                         if ((stcb->asoc.ecn_allowed) && ecn_ok) {
 5650                                 /* Enable ECN */
 5651                                 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
 5652                         } else {
 5653                                 /* No ECN */
 5654                                 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
 5655                         }
 5656                 } else {
 5657                         /* we could get no asoc if it is a O-O-T-B packet */
 5658                         tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
 5659                 }
 5660                 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
 5661                 if (port) {
 5662                         ip6h->ip6_nxt = IPPROTO_UDP;
 5663                 } else {
 5664                         ip6h->ip6_nxt = IPPROTO_SCTP;
 5665                 }
 5666                 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
 5667                 ip6h->ip6_dst = sin6->sin6_addr;
 5668 
 5669                 /*
 5670                  * Add SRC address selection here: we can only reuse to a
 5671                  * limited degree the kame src-addr-sel, since we can try
 5672                  * their selection but it may not be bound.
 5673                  */
 5674                 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
 5675                 lsa6_tmp.sin6_family = AF_INET6;
 5676                 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
 5677                 lsa6 = &lsa6_tmp;
 5678                 if (net && out_of_asoc_ok == 0) {
 5679                         if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
 5680                                 sctp_free_ifa(net->ro._s_addr);
 5681                                 net->ro._s_addr = NULL;
 5682                                 net->src_addr_selected = 0;
 5683                                 if (ro->ro_rt) {
 5684                                         RTFREE(ro->ro_rt);
 5685                                         ro->ro_rt = NULL;
 5686                                 }
 5687                         }
 5688                         if (net->src_addr_selected == 0) {
 5689                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
 5690                                 /* KAME hack: embed scopeid */
 5691                                 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
 5692                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 5693                                         return (EINVAL);
 5694                                 }
 5695                                 /* Cache the source address */
 5696                                 net->ro._s_addr = sctp_source_address_selection(inp,
 5697                                     stcb,
 5698                                     ro,
 5699                                     net,
 5700                                     0,
 5701                                     vrf_id);
 5702                                 (void)sa6_recoverscope(sin6);
 5703                                 net->src_addr_selected = 1;
 5704                         }
 5705                         if (net->ro._s_addr == NULL) {
 5706                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
 5707                                 net->src_addr_selected = 0;
 5708                                 goto no_route;
 5709                         }
 5710                         lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
 5711                 } else {
 5712                         sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
 5713                         /* KAME hack: embed scopeid */
 5714                         if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
 5715                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
 5716                                 return (EINVAL);
 5717                         }
 5718                         if (over_addr == NULL) {
 5719                                 struct sctp_ifa *_lsrc;
 5720 
 5721                                 _lsrc = sctp_source_address_selection(inp, stcb, ro,
 5722                                     net,
 5723                                     out_of_asoc_ok,
 5724                                     vrf_id);
 5725                                 if (_lsrc == NULL) {
 5726                                         goto no_route;
 5727                                 }
 5728                                 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
 5729                                 sctp_free_ifa(_lsrc);
 5730                         } else {
 5731                                 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
 5732                                 SCTP_RTALLOC(ro, vrf_id);
 5733                         }
 5734                         (void)sa6_recoverscope(sin6);
 5735                 }
 5736                 lsa6->sin6_port = inp->sctp_lport;
 5737 
 5738                 if (ro->ro_rt == NULL) {
 5739                         /*
 5740                          * src addr selection failed to find a route (or
 5741                          * valid source addr), so we can't get there from
 5742                          * here!
 5743                          */
 5744                         goto no_route;
 5745                 }
 5746                 /*
 5747                  * XXX: sa6 may not have a valid sin6_scope_id in the
 5748                  * non-SCOPEDROUTING case.
 5749                  */
 5750                 bzero(&lsa6_storage, sizeof(lsa6_storage));
 5751                 lsa6_storage.sin6_family = AF_INET6;
 5752                 lsa6_storage.sin6_len = sizeof(lsa6_storage);
 5753                 lsa6_storage.sin6_addr = lsa6->sin6_addr;
 5754                 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
 5755                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
 5756                         sctp_m_freem(m);
 5757                         return (error);
 5758                 }
 5759                 /* XXX */
 5760                 lsa6_storage.sin6_addr = lsa6->sin6_addr;
 5761                 lsa6_storage.sin6_port = inp->sctp_lport;
 5762                 lsa6 = &lsa6_storage;
 5763                 ip6h->ip6_src = lsa6->sin6_addr;
 5764 
 5765                 if (port) {
 5766                         udp = (struct udphdr *)(ip6h + 1);
 5767                         udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
 5768                         udp->uh_dport = port;
 5769                         udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
 5770                         udp->uh_sum = 0;
 5771                 }
 5772                 /*
 5773                  * We set the hop limit now since there is a good chance
 5774                  * that our ro pointer is now filled
 5775                  */
 5776                 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
 5777                 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
 5778 
 5779 #ifdef SCTP_DEBUG
 5780                 /* Copy to be sure something bad is not happening */
 5781                 sin6->sin6_addr = ip6h->ip6_dst;
 5782                 lsa6->sin6_addr = ip6h->ip6_src;
 5783 #endif
 5784 
 5785                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
 5786                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
 5787                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
 5788                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
 5789                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
 5790                 if (net) {
 5791                         sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
 5792                         /* preserve the port and scope for link local send */
 5793                         prev_scope = sin6->sin6_scope_id;
 5794                         prev_port = sin6->sin6_port;
 5795                 }
 5796                 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
 5797                         /* failed to prepend data, give up */
 5798                         sctp_m_freem(m);
 5799                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
 5800                         return (ENOMEM);
 5801                 }
 5802 #ifdef  SCTP_PACKET_LOGGING
 5803                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
 5804                         sctp_packet_log(m, packet_length);
 5805 #endif
 5806                 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
 5807                 if (port) {
 5808                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
 5809                                 udp->uh_sum = 0xffff;
 5810                         }
 5811                 }
 5812                 /* send it out. table id is taken from stcb */
 5813 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5814                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 5815                         so = SCTP_INP_SO(inp);
 5816                         SCTP_SOCKET_UNLOCK(so, 0);
 5817                 }
 5818 #endif
 5819                 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
 5820 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
 5821                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
 5822                         atomic_add_int(&stcb->asoc.refcnt, 1);
 5823                         SCTP_TCB_UNLOCK(stcb);
 5824                         SCTP_SOCKET_LOCK(so, 0);
 5825                         SCTP_TCB_LOCK(stcb);
 5826                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
 5827                 }
 5828 #endif
 5829                 if (net) {
 5830                         /* for link local this must be done */
 5831                         sin6->sin6_scope_id = prev_scope;
 5832                         sin6->sin6_port = prev_port;
 5833                 }
 5834                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
 5835                 SCTP_STAT_INCR(sctps_sendpackets);
 5836                 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
 5837                 if (ret) {
 5838                         SCTP_STAT_INCR(sctps_senderrors);
 5839                 }
 5840                 if (net == NULL) {
 5841                         /* Now if we had a temp route free it */
 5842                         if (ro->ro_rt) {
 5843                                 RTFREE(ro->ro_rt);
 5844                         }
 5845                 } else {
 5846                         /* PMTU check versus smallest asoc MTU goes here */
 5847                         if (ro->ro_rt == NULL) {
 5848                                 /* Route was freed */
 5849                                 if (net->ro._s_addr &&
 5850                                     net->src_addr_selected) {
 5851                                         sctp_free_ifa(net->ro._s_addr);
 5852                                         net->ro._s_addr = NULL;
 5853                                 }
 5854                                 net->src_addr_selected = 0;
 5855                         }
 5856                         if ((ro->ro_rt != NULL) &&
 5857                             (net->ro._s_addr)) {
 5858                                 uint32_t mtu;
 5859 
 5860                                 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
 5861                                 if (mtu &&
 5862                                     (stcb->asoc.smallest_mtu > mtu)) {
 5863 #ifdef SCTP_PRINT_FOR_B_AND_M
 5864                                         SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
 5865                                             mtu);
 5866 #endif
 5867                                         sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
 5868                                         net->mtu = mtu;
 5869                                         if (net->port) {
 5870                                                 net->mtu -= sizeof(struct udphdr);
 5871                                         }
 5872                                 }
 5873                         } else if (ifp) {
 5874                                 if (ND_IFINFO(ifp)->linkmtu &&
 5875                                     (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
 5876 #ifdef SCTP_PRINT_FOR_B_AND_M
 5877                                         SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
 5878                                             ND_IFINFO(ifp)->linkmtu);
 5879 #endif
 5880                                         sctp_mtu_size_reset(inp,
 5881                                             &stcb->asoc,
 5882                                             ND_IFINFO(ifp)->linkmtu);
 5883                                 }
 5884                         }
 5885                 }
 5886                 return (ret);
 5887         }
 5888 #endif
 5889         else {
 5890                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
 5891                     ((struct sockaddr *)to)->sa_family);
 5892                 sctp_m_freem(m);
 5893                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
 5894                 return (EFAULT);
 5895         }
 5896 }
 5897 
 5898 
 5899 void
 5900 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
 5901 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
 5902     SCTP_UNUSED
 5903 #endif
 5904 )
 5905 {
 5906         struct mbuf *m, *m_at, *mp_last;
 5907         struct sctp_nets *net;
 5908         struct sctp_init_msg *initm;
 5909         struct sctp_supported_addr_param *sup_addr;
 5910         struct sctp_ecn_supported_param *ecn;
 5911         struct sctp_prsctp_supported_param *prsctp;
 5912         struct sctp_ecn_nonce_supported_param *ecn_nonce;
 5913         struct sctp_supported_chunk_types_param *pr_supported;
 5914         int cnt_inits_to = 0;
 5915         int padval, ret;
 5916         int num_ext;
 5917         int p_len;
 5918 
 5919         /* INIT's always go to the primary (and usually ONLY address) */
 5920         mp_last = NULL;
 5921         net = stcb->asoc.primary_destination;
 5922         if (net == NULL) {
 5923                 net = TAILQ_FIRST(&stcb->asoc.nets);
 5924                 if (net == NULL) {
 5925                         /* TSNH */
 5926                         return;
 5927                 }
 5928                 /* we confirm any address we send an INIT to */
 5929                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
 5930                 (void)sctp_set_primary_addr(stcb, NULL, net);
 5931         } else {
 5932                 /* we confirm any address we send an INIT to */
 5933                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
 5934         }
 5935         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
 5936 #ifdef INET6
 5937         if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
 5938                 /*
 5939                  * special hook, if we are sending to link local it will not
 5940                  * show up in our private address count.
 5941                  */
 5942                 struct sockaddr_in6 *sin6l;
 5943 
 5944                 sin6l = &net->ro._l_addr.sin6;
 5945                 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
 5946                         cnt_inits_to = 1;
 5947         }
 5948 #endif
 5949         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
 5950                 /* This case should not happen */
 5951                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
 5952                 return;
 5953         }
 5954         /* start the INIT timer */
 5955         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
 5956 
 5957         m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
 5958         if (m == NULL) {
 5959                 /* No memory, INIT timer will re-attempt. */
 5960                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
 5961                 return;
 5962         }
 5963         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
 5964         /*
 5965          * assume peer supports asconf in order to be able to queue local
 5966          * address changes while an INIT is in flight and before the assoc
 5967          * is established.
 5968          */
 5969         stcb->asoc.peer_supports_asconf = 1;
 5970         /* Now lets put the SCTP header in place */
 5971         initm = mtod(m, struct sctp_init_msg *);
 5972         initm->sh.src_port = inp->sctp_lport;
 5973         initm->sh.dest_port = stcb->rport;
 5974         initm->sh.v_tag = 0;
 5975         initm->sh.checksum = 0; /* calculate later */
 5976         /* now the chunk header */
 5977         initm->msg.ch.chunk_type = SCTP_INITIATION;
 5978         initm->msg.ch.chunk_flags = 0;
 5979         /* fill in later from mbuf we build */
 5980         initm->msg.ch.chunk_length = 0;
 5981         /* place in my tag */
 5982         initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
 5983         /* set up some of the credits. */
 5984         initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
 5985             SCTP_MINIMAL_RWND));
 5986 
 5987         initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
 5988         initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
 5989         initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
 5990         /* now the address restriction */
 5991         sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
 5992             sizeof(*initm));
 5993         sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
 5994 #ifdef INET6
 5995         /* we support 2 types: IPv6/IPv4 */
 5996         sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
 5997         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
 5998         sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
 5999 #else
 6000         /* we support 1 type: IPv4 */
 6001         sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
 6002         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
 6003         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
 6004 #endif
 6005         SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
 6006 
 6007         if (inp->sctp_ep.adaptation_layer_indicator) {
 6008                 struct sctp_adaptation_layer_indication *ali;
 6009 
 6010                 ali = (struct sctp_adaptation_layer_indication *)(
 6011                     (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
 6012                 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
 6013                 ali->ph.param_length = htons(sizeof(*ali));
 6014                 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
 6015                 SCTP_BUF_LEN(m) += sizeof(*ali);
 6016                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
 6017                     sizeof(*ali));
 6018         } else {
 6019                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
 6020                     sizeof(*sup_addr) + sizeof(uint16_t));
 6021         }
 6022 
 6023         /* now any cookie time extensions */
 6024         if (stcb->asoc.cookie_preserve_req) {
 6025                 struct sctp_cookie_perserve_param *cookie_preserve;
 6026 
 6027                 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
 6028                 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
 6029                 cookie_preserve->ph.param_length = htons(
 6030                     sizeof(*cookie_preserve));
 6031                 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
 6032                 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
 6033                 ecn = (struct sctp_ecn_supported_param *)(
 6034                     (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
 6035                 stcb->asoc.cookie_preserve_req = 0;
 6036         }
 6037         /* ECN parameter */
 6038         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
 6039                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
 6040                 ecn->ph.param_length = htons(sizeof(*ecn));
 6041                 SCTP_BUF_LEN(m) += sizeof(*ecn);
 6042                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
 6043                     sizeof(*ecn));
 6044         } else {
 6045                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
 6046         }
 6047         /* And now tell the peer we do pr-sctp */
 6048         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
 6049         prsctp->ph.param_length = htons(sizeof(*prsctp));
 6050         SCTP_BUF_LEN(m) += sizeof(*prsctp);
 6051 
 6052         /* And now tell the peer we do all the extensions */
 6053         pr_supported = (struct sctp_supported_chunk_types_param *)
 6054             ((caddr_t)prsctp + sizeof(*prsctp));
 6055         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
 6056         num_ext = 0;
 6057         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
 6058         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
 6059         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
 6060         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
 6061         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
 6062         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
 6063                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
 6064         /*
 6065          * EY  if the initiator supports nr_sacks, need to report that to
 6066          * responder in INIT chunk
 6067          */
 6068         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
 6069                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
 6070         p_len = sizeof(*pr_supported) + num_ext;
 6071         pr_supported->ph.param_length = htons(p_len);
 6072         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
 6073         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 6074 
 6075         /* ECN nonce: And now tell the peer we support ECN nonce */
 6076         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
 6077                 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
 6078                     ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
 6079                 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
 6080                 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
 6081                 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
 6082         }
 6083         if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
 6084                 /* Add NAT friendly parameter */
 6085                 struct sctp_paramhdr *ph;
 6086 
 6087                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 6088                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
 6089                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
 6090                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
 6091         }
 6092         /* add authentication parameters */
 6093         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
 6094                 struct sctp_auth_random *randp;
 6095                 struct sctp_auth_hmac_algo *hmacs;
 6096                 struct sctp_auth_chunk_list *chunks;
 6097 
 6098                 /* attach RANDOM parameter, if available */
 6099                 if (stcb->asoc.authinfo.random != NULL) {
 6100                         randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 6101                         p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
 6102 #ifdef SCTP_AUTH_DRAFT_04
 6103                         randp->ph.param_type = htons(SCTP_RANDOM);
 6104                         randp->ph.param_length = htons(p_len);
 6105                         bcopy(stcb->asoc.authinfo.random->key,
 6106                             randp->random_data,
 6107                             stcb->asoc.authinfo.random_len);
 6108 #else
 6109                         /* random key already contains the header */
 6110                         bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
 6111 #endif
 6112                         /* zero out any padding required */
 6113                         bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
 6114                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 6115                 }
 6116                 /* add HMAC_ALGO parameter */
 6117                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 6118                 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
 6119                     (uint8_t *) hmacs->hmac_ids);
 6120                 if (p_len > 0) {
 6121                         p_len += sizeof(*hmacs);
 6122                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
 6123                         hmacs->ph.param_length = htons(p_len);
 6124                         /* zero out any padding required */
 6125                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
 6126                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 6127                 }
 6128                 /* add CHUNKS parameter */
 6129                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 6130                 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
 6131                     chunks->chunk_types);
 6132                 if (p_len > 0) {
 6133                         p_len += sizeof(*chunks);
 6134                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
 6135                         chunks->ph.param_length = htons(p_len);
 6136                         /* zero out any padding required */
 6137                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
 6138                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 6139                 }
 6140         }
 6141         m_at = m;
 6142         /* now the addresses */
 6143         {
 6144                 struct sctp_scoping scp;
 6145 
 6146                 /*
 6147                  * To optimize this we could put the scoping stuff into a
 6148                  * structure and remove the individual uint8's from the
 6149                  * assoc structure. Then we could just sifa in the address
 6150                  * within the stcb.. but for now this is a quick hack to get
 6151                  * the address stuff teased apart.
 6152                  */
 6153                 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
 6154                 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
 6155                 scp.loopback_scope = stcb->asoc.loopback_scope;
 6156                 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
 6157                 scp.local_scope = stcb->asoc.local_scope;
 6158                 scp.site_scope = stcb->asoc.site_scope;
 6159 
 6160                 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
 6161         }
 6162 
 6163         /* calulate the size and update pkt header and chunk header */
 6164         p_len = 0;
 6165         for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
 6166                 if (SCTP_BUF_NEXT(m_at) == NULL)
 6167                         mp_last = m_at;
 6168                 p_len += SCTP_BUF_LEN(m_at);
 6169         }
 6170         initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
 6171         /*
 6172          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
 6173          * here since the timer will drive a retranmission.
 6174          */
 6175 
 6176         /* I don't expect this to execute but we will be safe here */
 6177         padval = p_len % 4;
 6178         if ((padval) && (mp_last)) {
 6179                 /*
 6180                  * The compiler worries that mp_last may not be set even
 6181                  * though I think it is impossible :-> however we add
 6182                  * mp_last here just in case.
 6183                  */
 6184                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
 6185                 if (ret) {
 6186                         /* Houston we have a problem, no space */
 6187                         sctp_m_freem(m);
 6188                         return;
 6189                 }
 6190                 p_len += padval;
 6191         }
 6192         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
 6193         ret = sctp_lowlevel_chunk_output(inp, stcb, net,
 6194             (struct sockaddr *)&net->ro._l_addr,
 6195             m, 0, NULL, 0, 0, 0, NULL, 0, net->port, so_locked, NULL);
 6196         SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
 6197         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 6198         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
 6199         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
 6200 }
 6201 
 6202 struct mbuf *
 6203 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
 6204     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
 6205 {
 6206         /*
 6207          * Given a mbuf containing an INIT or INIT-ACK with the param_offset
 6208          * being equal to the beginning of the params i.e. (iphlen +
 6209          * sizeof(struct sctp_init_msg) parse through the parameters to the
 6210          * end of the mbuf verifying that all parameters are known.
 6211          * 
 6212          * For unknown parameters build and return a mbuf with
 6213          * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
 6214          * processing this chunk stop, and set *abort_processing to 1.
 6215          * 
 6216          * By having param_offset be pre-set to where parameters begin it is
 6217          * hoped that this routine may be reused in the future by new
 6218          * features.
 6219          */
 6220         struct sctp_paramhdr *phdr, params;
 6221 
 6222         struct mbuf *mat, *op_err;
 6223         char tempbuf[SCTP_PARAM_BUFFER_SIZE];
 6224         int at, limit, pad_needed;
 6225         uint16_t ptype, plen, padded_size;
 6226         int err_at;
 6227 
 6228         *abort_processing = 0;
 6229         mat = in_initpkt;
 6230         err_at = 0;
 6231         limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
 6232         at = param_offset;
 6233         op_err = NULL;
 6234         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
 6235         phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
 6236         while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
 6237                 ptype = ntohs(phdr->param_type);
 6238                 plen = ntohs(phdr->param_length);
 6239                 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
 6240                         /* wacked parameter */
 6241                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
 6242                         goto invalid_size;
 6243                 }
 6244                 limit -= SCTP_SIZE32(plen);
 6245                 /*-
 6246                  * All parameters for all chunks that we know/understand are
 6247                  * listed here. We process them other places and make
 6248                  * appropriate stop actions per the upper bits. However this
 6249                  * is the generic routine processor's can call to get back
 6250                  * an operr.. to either incorporate (init-ack) or send.
 6251                  */
 6252                 padded_size = SCTP_SIZE32(plen);
 6253                 switch (ptype) {
 6254                         /* Param's with variable size */
 6255                 case SCTP_HEARTBEAT_INFO:
 6256                 case SCTP_STATE_COOKIE:
 6257                 case SCTP_UNRECOG_PARAM:
 6258                 case SCTP_ERROR_CAUSE_IND:
 6259                         /* ok skip fwd */
 6260                         at += padded_size;
 6261                         break;
 6262                         /* Param's with variable size within a range */
 6263                 case SCTP_CHUNK_LIST:
 6264                 case SCTP_SUPPORTED_CHUNK_EXT:
 6265                         if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
 6266                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
 6267                                 goto invalid_size;
 6268                         }
 6269                         at += padded_size;
 6270                         break;
 6271                 case SCTP_SUPPORTED_ADDRTYPE:
 6272                         if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
 6273                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
 6274                                 goto invalid_size;
 6275                         }
 6276                         at += padded_size;
 6277                         break;
 6278                 case SCTP_RANDOM:
 6279                         if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
 6280                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
 6281                                 goto invalid_size;
 6282                         }
 6283                         at += padded_size;
 6284                         break;
 6285                 case SCTP_SET_PRIM_ADDR:
 6286                 case SCTP_DEL_IP_ADDRESS:
 6287                 case SCTP_ADD_IP_ADDRESS:
 6288                         if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
 6289                             (padded_size != sizeof(struct sctp_asconf_addr_param))) {
 6290                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
 6291                                 goto invalid_size;
 6292                         }
 6293                         at += padded_size;
 6294                         break;
 6295                         /* Param's with a fixed size */
 6296                 case SCTP_IPV4_ADDRESS:
 6297                         if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
 6298                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
 6299                                 goto invalid_size;
 6300                         }
 6301                         at += padded_size;
 6302                         break;
 6303                 case SCTP_IPV6_ADDRESS:
 6304                         if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
 6305                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
 6306                                 goto invalid_size;
 6307                         }
 6308                         at += padded_size;
 6309                         break;
 6310                 case SCTP_COOKIE_PRESERVE:
 6311                         if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
 6312                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
 6313                                 goto invalid_size;
 6314                         }
 6315                         at += padded_size;
 6316                         break;
 6317                 case SCTP_HAS_NAT_SUPPORT:
 6318                         *nat_friendly = 1;
 6319                         /* fall through */
 6320                 case SCTP_ECN_NONCE_SUPPORTED:
 6321                 case SCTP_PRSCTP_SUPPORTED:
 6322 
 6323                         if (padded_size != sizeof(struct sctp_paramhdr)) {
 6324                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
 6325                                 goto invalid_size;
 6326                         }
 6327                         at += padded_size;
 6328                         break;
 6329                 case SCTP_ECN_CAPABLE:
 6330                         if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
 6331                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
 6332                                 goto invalid_size;
 6333                         }
 6334                         at += padded_size;
 6335                         break;
 6336                 case SCTP_ULP_ADAPTATION:
 6337                         if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
 6338                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
 6339                                 goto invalid_size;
 6340                         }
 6341                         at += padded_size;
 6342                         break;
 6343                 case SCTP_SUCCESS_REPORT:
 6344                         if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
 6345                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
 6346                                 goto invalid_size;
 6347                         }
 6348                         at += padded_size;
 6349                         break;
 6350                 case SCTP_HOSTNAME_ADDRESS:
 6351                         {
 6352                                 /* We can NOT handle HOST NAME addresses!! */
 6353                                 int l_len;
 6354 
 6355                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
 6356                                 *abort_processing = 1;
 6357                                 if (op_err == NULL) {
 6358                                         /* Ok need to try to get a mbuf */
 6359 #ifdef INET6
 6360                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 6361 #else
 6362                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 6363 #endif
 6364                                         l_len += plen;
 6365                                         l_len += sizeof(struct sctp_paramhdr);
 6366                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
 6367                                         if (op_err) {
 6368                                                 SCTP_BUF_LEN(op_err) = 0;
 6369                                                 /*
 6370                                                  * pre-reserve space for ip
 6371                                                  * and sctp header  and
 6372                                                  * chunk hdr
 6373                                                  */
 6374 #ifdef INET6
 6375                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
 6376 #else
 6377                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
 6378 #endif
 6379                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
 6380                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
 6381                                         }
 6382                                 }
 6383                                 if (op_err) {
 6384                                         /* If we have space */
 6385                                         struct sctp_paramhdr s;
 6386 
 6387                                         if (err_at % 4) {
 6388                                                 uint32_t cpthis = 0;
 6389 
 6390                                                 pad_needed = 4 - (err_at % 4);
 6391                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
 6392                                                 err_at += pad_needed;
 6393                                         }
 6394                                         s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
 6395                                         s.param_length = htons(sizeof(s) + plen);
 6396                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
 6397                                         err_at += sizeof(s);
 6398                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
 6399                                         if (phdr == NULL) {
 6400                                                 sctp_m_freem(op_err);
 6401                                                 /*
 6402                                                  * we are out of memory but
 6403                                                  * we still need to have a
 6404                                                  * look at what to do (the
 6405                                                  * system is in trouble
 6406                                                  * though).
 6407                                                  */
 6408                                                 return (NULL);
 6409                                         }
 6410                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
 6411                                         err_at += plen;
 6412                                 }
 6413                                 return (op_err);
 6414                                 break;
 6415                         }
 6416                 default:
 6417                         /*
 6418                          * we do not recognize the parameter figure out what
 6419                          * we do.
 6420                          */
 6421                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
 6422                         if ((ptype & 0x4000) == 0x4000) {
 6423                                 /* Report bit is set?? */
 6424                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
 6425                                 if (op_err == NULL) {
 6426                                         int l_len;
 6427 
 6428                                         /* Ok need to try to get an mbuf */
 6429 #ifdef INET6
 6430                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 6431 #else
 6432                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 6433 #endif
 6434                                         l_len += plen;
 6435                                         l_len += sizeof(struct sctp_paramhdr);
 6436                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
 6437                                         if (op_err) {
 6438                                                 SCTP_BUF_LEN(op_err) = 0;
 6439 #ifdef INET6
 6440                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
 6441 #else
 6442                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
 6443 #endif
 6444                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
 6445                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
 6446                                         }
 6447                                 }
 6448                                 if (op_err) {
 6449                                         /* If we have space */
 6450                                         struct sctp_paramhdr s;
 6451 
 6452                                         if (err_at % 4) {
 6453                                                 uint32_t cpthis = 0;
 6454 
 6455                                                 pad_needed = 4 - (err_at % 4);
 6456                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
 6457                                                 err_at += pad_needed;
 6458                                         }
 6459                                         s.param_type = htons(SCTP_UNRECOG_PARAM);
 6460                                         s.param_length = htons(sizeof(s) + plen);
 6461                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
 6462                                         err_at += sizeof(s);
 6463                                         if (plen > sizeof(tempbuf)) {
 6464                                                 plen = sizeof(tempbuf);
 6465                                         }
 6466                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
 6467                                         if (phdr == NULL) {
 6468                                                 sctp_m_freem(op_err);
 6469                                                 /*
 6470                                                  * we are out of memory but
 6471                                                  * we still need to have a
 6472                                                  * look at what to do (the
 6473                                                  * system is in trouble
 6474                                                  * though).
 6475                                                  */
 6476                                                 op_err = NULL;
 6477                                                 goto more_processing;
 6478                                         }
 6479                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
 6480                                         err_at += plen;
 6481                                 }
 6482                         }
 6483         more_processing:
 6484                         if ((ptype & 0x8000) == 0x0000) {
 6485                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
 6486                                 return (op_err);
 6487                         } else {
 6488                                 /* skip this chunk and continue processing */
 6489                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
 6490                                 at += SCTP_SIZE32(plen);
 6491                         }
 6492                         break;
 6493 
 6494                 }
 6495                 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
 6496         }
 6497         return (op_err);
 6498 invalid_size:
 6499         SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
 6500         *abort_processing = 1;
 6501         if ((op_err == NULL) && phdr) {
 6502                 int l_len;
 6503 
 6504 #ifdef INET6
 6505                 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 6506 #else
 6507                 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
 6508 #endif
 6509                 l_len += (2 * sizeof(struct sctp_paramhdr));
 6510                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
 6511                 if (op_err) {
 6512                         SCTP_BUF_LEN(op_err) = 0;
 6513 #ifdef INET6
 6514                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
 6515 #else
 6516                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
 6517 #endif
 6518                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
 6519                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
 6520                 }
 6521         }
 6522         if ((op_err) && phdr) {
 6523                 struct sctp_paramhdr s;
 6524 
 6525                 if (err_at % 4) {
 6526                         uint32_t cpthis = 0;
 6527 
 6528                         pad_needed = 4 - (err_at % 4);
 6529                         m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
 6530                         err_at += pad_needed;
 6531                 }
 6532                 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
 6533                 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
 6534                 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
 6535                 err_at += sizeof(s);
 6536                 /* Only copy back the p-hdr that caused the issue */
 6537                 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
 6538         }
 6539         return (op_err);
 6540 }
 6541 
 6542 static int
 6543 sctp_are_there_new_addresses(struct sctp_association *asoc,
 6544     struct mbuf *in_initpkt, int iphlen, int offset)
 6545 {
 6546         /*
 6547          * Given a INIT packet, look through the packet to verify that there
 6548          * are NO new addresses. As we go through the parameters add reports
 6549          * of any un-understood parameters that require an error.  Also we
 6550          * must return (1) to drop the packet if we see a un-understood
 6551          * parameter that tells us to drop the chunk.
 6552          */
 6553         struct sockaddr_in sin4, *sa4;
 6554 
 6555 #ifdef INET6
 6556         struct sockaddr_in6 sin6, *sa6;
 6557 
 6558 #endif
 6559         struct sockaddr *sa_touse;
 6560         struct sockaddr *sa;
 6561         struct sctp_paramhdr *phdr, params;
 6562         struct ip *iph;
 6563 
 6564 #ifdef INET6
 6565         struct ip6_hdr *ip6h;
 6566 
 6567 #endif
 6568         struct mbuf *mat;
 6569         uint16_t ptype, plen;
 6570         int err_at;
 6571         uint8_t fnd;
 6572         struct sctp_nets *net;
 6573 
 6574         memset(&sin4, 0, sizeof(sin4));
 6575 #ifdef INET6
 6576         memset(&sin6, 0, sizeof(sin6));
 6577 #endif
 6578         sin4.sin_family = AF_INET;
 6579         sin4.sin_len = sizeof(sin4);
 6580 #ifdef INET6
 6581         sin6.sin6_family = AF_INET6;
 6582         sin6.sin6_len = sizeof(sin6);
 6583 #endif
 6584         sa_touse = NULL;
 6585         /* First what about the src address of the pkt ? */
 6586         iph = mtod(in_initpkt, struct ip *);
 6587         switch (iph->ip_v) {
 6588         case IPVERSION:
 6589                 /* source addr is IPv4 */
 6590                 sin4.sin_addr = iph->ip_src;
 6591                 sa_touse = (struct sockaddr *)&sin4;
 6592                 break;
 6593 #ifdef INET6
 6594         case IPV6_VERSION >> 4:
 6595                 /* source addr is IPv6 */
 6596                 ip6h = mtod(in_initpkt, struct ip6_hdr *);
 6597                 sin6.sin6_addr = ip6h->ip6_src;
 6598                 sa_touse = (struct sockaddr *)&sin6;
 6599                 break;
 6600 #endif
 6601         default:
 6602                 return (1);
 6603         }
 6604 
 6605         fnd = 0;
 6606         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
 6607                 sa = (struct sockaddr *)&net->ro._l_addr;
 6608                 if (sa->sa_family == sa_touse->sa_family) {
 6609                         if (sa->sa_family == AF_INET) {
 6610                                 sa4 = (struct sockaddr_in *)sa;
 6611                                 if (sa4->sin_addr.s_addr ==
 6612                                     sin4.sin_addr.s_addr) {
 6613                                         fnd = 1;
 6614                                         break;
 6615                                 }
 6616                         }
 6617 #ifdef INET6
 6618                         if (sa->sa_family == AF_INET6) {
 6619                                 sa6 = (struct sockaddr_in6 *)sa;
 6620                                 if (SCTP6_ARE_ADDR_EQUAL(sa6,
 6621                                     &sin6)) {
 6622                                         fnd = 1;
 6623                                         break;
 6624                                 }
 6625                         }
 6626 #endif
 6627                 }
 6628         }
 6629         if (fnd == 0) {
 6630                 /* New address added! no need to look futher. */
 6631                 return (1);
 6632         }
 6633         /* Ok so far lets munge through the rest of the packet */
 6634         mat = in_initpkt;
 6635         err_at = 0;
 6636         sa_touse = NULL;
 6637         offset += sizeof(struct sctp_init_chunk);
 6638         phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
 6639         while (phdr) {
 6640                 ptype = ntohs(phdr->param_type);
 6641                 plen = ntohs(phdr->param_length);
 6642                 if (ptype == SCTP_IPV4_ADDRESS) {
 6643                         struct sctp_ipv4addr_param *p4, p4_buf;
 6644 
 6645                         phdr = sctp_get_next_param(mat, offset,
 6646                             (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
 6647                         if (plen != sizeof(struct sctp_ipv4addr_param) ||
 6648                             phdr == NULL) {
 6649                                 return (1);
 6650                         }
 6651                         p4 = (struct sctp_ipv4addr_param *)phdr;
 6652                         sin4.sin_addr.s_addr = p4->addr;
 6653                         sa_touse = (struct sockaddr *)&sin4;
 6654                 } else if (ptype == SCTP_IPV6_ADDRESS) {
 6655                         struct sctp_ipv6addr_param *p6, p6_buf;
 6656 
 6657                         phdr = sctp_get_next_param(mat, offset,
 6658                             (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
 6659                         if (plen != sizeof(struct sctp_ipv6addr_param) ||
 6660                             phdr == NULL) {
 6661                                 return (1);
 6662                         }
 6663                         p6 = (struct sctp_ipv6addr_param *)phdr;
 6664 #ifdef INET6
 6665                         memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
 6666                             sizeof(p6->addr));
 6667 #endif
 6668                         sa_touse = (struct sockaddr *)&sin4;
 6669                 }
 6670                 if (sa_touse) {
 6671                         /* ok, sa_touse points to one to check */
 6672                         fnd = 0;
 6673                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
 6674                                 sa = (struct sockaddr *)&net->ro._l_addr;
 6675                                 if (sa->sa_family != sa_touse->sa_family) {
 6676                                         continue;
 6677                                 }
 6678                                 if (sa->sa_family == AF_INET) {
 6679                                         sa4 = (struct sockaddr_in *)sa;
 6680                                         if (sa4->sin_addr.s_addr ==
 6681                                             sin4.sin_addr.s_addr) {
 6682                                                 fnd = 1;
 6683                                                 break;
 6684                                         }
 6685                                 }
 6686 #ifdef INET6
 6687                                 if (sa->sa_family == AF_INET6) {
 6688                                         sa6 = (struct sockaddr_in6 *)sa;
 6689                                         if (SCTP6_ARE_ADDR_EQUAL(
 6690                                             sa6, &sin6)) {
 6691                                                 fnd = 1;
 6692                                                 break;
 6693                                         }
 6694                                 }
 6695 #endif
 6696                         }
 6697                         if (!fnd) {
 6698                                 /* New addr added! no need to look further */
 6699                                 return (1);
 6700                         }
 6701                 }
 6702                 offset += SCTP_SIZE32(plen);
 6703                 phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
 6704         }
 6705         return (0);
 6706 }
 6707 
 6708 /*
 6709  * Given a MBUF chain that was sent into us containing an INIT. Build a
 6710  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
 6711  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
 6712  * message (i.e. the struct sctp_init_msg).
 6713  */
 6714 void
 6715 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
 6716     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
 6717     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
 6718 {
 6719         struct sctp_association *asoc;
 6720         struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
 6721         struct sctp_init_msg *initackm_out;
 6722         struct sctp_ecn_supported_param *ecn;
 6723         struct sctp_prsctp_supported_param *prsctp;
 6724         struct sctp_ecn_nonce_supported_param *ecn_nonce;
 6725         struct sctp_supported_chunk_types_param *pr_supported;
 6726         union sctp_sockstore store, store1, *over_addr;
 6727         struct sockaddr_in *sin, *to_sin;
 6728 
 6729 #ifdef INET6
 6730         struct sockaddr_in6 *sin6, *to_sin6;
 6731 
 6732 #endif
 6733         struct ip *iph;
 6734 
 6735 #ifdef INET6
 6736         struct ip6_hdr *ip6;
 6737 
 6738 #endif
 6739         struct sockaddr *to;
 6740         struct sctp_state_cookie stc;
 6741         struct sctp_nets *net = NULL;
 6742         uint8_t *signature = NULL;
 6743         int cnt_inits_to = 0;
 6744         uint16_t his_limit, i_want;
 6745         int abort_flag, padval;
 6746         int num_ext;
 6747         int p_len;
 6748         int nat_friendly = 0;
 6749         struct socket *so;
 6750 
 6751         if (stcb)
 6752                 asoc = &stcb->asoc;
 6753         else
 6754                 asoc = NULL;
 6755         mp_last = NULL;
 6756         if ((asoc != NULL) &&
 6757             (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
 6758             (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
 6759                 /* new addresses, out of here in non-cookie-wait states */
 6760                 /*
 6761                  * Send a ABORT, we don't add the new address error clause
 6762                  * though we even set the T bit and copy in the 0 tag.. this
 6763                  * looks no different than if no listener was present.
 6764                  */
 6765                 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
 6766                 return;
 6767         }
 6768         abort_flag = 0;
 6769         op_err = sctp_arethere_unrecognized_parameters(init_pkt,
 6770             (offset + sizeof(struct sctp_init_chunk)),
 6771             &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
 6772         if (abort_flag) {
 6773 do_a_abort:
 6774                 sctp_send_abort(init_pkt, iphlen, sh,
 6775                     init_chk->init.initiate_tag, op_err, vrf_id, port);
 6776                 return;
 6777         }
 6778         m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
 6779         if (m == NULL) {
 6780                 /* No memory, INIT timer will re-attempt. */
 6781                 if (op_err)
 6782                         sctp_m_freem(op_err);
 6783                 return;
 6784         }
 6785         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
 6786 
 6787         /* the time I built cookie */
 6788         (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
 6789 
 6790         /* populate any tie tags */
 6791         if (asoc != NULL) {
 6792                 /* unlock before tag selections */
 6793                 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
 6794                 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
 6795                 stc.cookie_life = asoc->cookie_life;
 6796                 net = asoc->primary_destination;
 6797         } else {
 6798                 stc.tie_tag_my_vtag = 0;
 6799                 stc.tie_tag_peer_vtag = 0;
 6800                 /* life I will award this cookie */
 6801                 stc.cookie_life = inp->sctp_ep.def_cookie_life;
 6802         }
 6803 
 6804         /* copy in the ports for later check */
 6805         stc.myport = sh->dest_port;
 6806         stc.peerport = sh->src_port;
 6807 
 6808         /*
 6809          * If we wanted to honor cookie life extentions, we would add to
 6810          * stc.cookie_life. For now we should NOT honor any extension
 6811          */
 6812         stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
 6813         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 6814                 struct inpcb *in_inp;
 6815 
 6816                 /* Its a V6 socket */
 6817                 in_inp = (struct inpcb *)inp;
 6818                 stc.ipv6_addr_legal = 1;
 6819                 /* Now look at the binding flag to see if V4 will be legal */
 6820                 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
 6821                         stc.ipv4_addr_legal = 1;
 6822                 } else {
 6823                         /* V4 addresses are NOT legal on the association */
 6824                         stc.ipv4_addr_legal = 0;
 6825                 }
 6826         } else {
 6827                 /* Its a V4 socket, no - V6 */
 6828                 stc.ipv4_addr_legal = 1;
 6829                 stc.ipv6_addr_legal = 0;
 6830         }
 6831 
 6832 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
 6833         stc.ipv4_scope = 1;
 6834 #else
 6835         stc.ipv4_scope = 0;
 6836 #endif
 6837         /* now for scope setup */
 6838         memset((caddr_t)&store, 0, sizeof(store));
 6839         memset((caddr_t)&store1, 0, sizeof(store1));
 6840         sin = &store.sin;
 6841         to_sin = &store1.sin;
 6842 #ifdef INET6
 6843         sin6 = &store.sin6;
 6844         to_sin6 = &store1.sin6;
 6845 #endif
 6846         iph = mtod(init_pkt, struct ip *);
 6847         /* establish the to_addr's */
 6848         switch (iph->ip_v) {
 6849         case IPVERSION:
 6850                 to_sin->sin_port = sh->dest_port;
 6851                 to_sin->sin_family = AF_INET;
 6852                 to_sin->sin_len = sizeof(struct sockaddr_in);
 6853                 to_sin->sin_addr = iph->ip_dst;
 6854                 break;
 6855 #ifdef INET6
 6856         case IPV6_VERSION >> 4:
 6857                 ip6 = mtod(init_pkt, struct ip6_hdr *);
 6858                 to_sin6->sin6_addr = ip6->ip6_dst;
 6859                 to_sin6->sin6_scope_id = 0;
 6860                 to_sin6->sin6_port = sh->dest_port;
 6861                 to_sin6->sin6_family = AF_INET6;
 6862                 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
 6863                 break;
 6864 #endif
 6865         default:
 6866                 goto do_a_abort;
 6867                 break;
 6868         };
 6869 
 6870         if (net == NULL) {
 6871                 to = (struct sockaddr *)&store;
 6872                 switch (iph->ip_v) {
 6873                 case IPVERSION:
 6874                         {
 6875                                 sin->sin_family = AF_INET;
 6876                                 sin->sin_len = sizeof(struct sockaddr_in);
 6877                                 sin->sin_port = sh->src_port;
 6878                                 sin->sin_addr = iph->ip_src;
 6879                                 /* lookup address */
 6880                                 stc.address[0] = sin->sin_addr.s_addr;
 6881                                 stc.address[1] = 0;
 6882                                 stc.address[2] = 0;
 6883                                 stc.address[3] = 0;
 6884                                 stc.addr_type = SCTP_IPV4_ADDRESS;
 6885                                 /* local from address */
 6886                                 stc.laddress[0] = to_sin->sin_addr.s_addr;
 6887                                 stc.laddress[1] = 0;
 6888                                 stc.laddress[2] = 0;
 6889                                 stc.laddress[3] = 0;
 6890                                 stc.laddr_type = SCTP_IPV4_ADDRESS;
 6891                                 /* scope_id is only for v6 */
 6892                                 stc.scope_id = 0;
 6893 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
 6894                                 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
 6895                                         stc.ipv4_scope = 1;
 6896                                 }
 6897 #else
 6898                                 stc.ipv4_scope = 1;
 6899 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
 6900                                 /* Must use the address in this case */
 6901                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
 6902                                         stc.loopback_scope = 1;
 6903                                         stc.ipv4_scope = 1;
 6904                                         stc.site_scope = 1;
 6905                                         stc.local_scope = 0;
 6906                                 }
 6907                                 break;
 6908                         }
 6909 #ifdef INET6
 6910                 case IPV6_VERSION >> 4:
 6911                         {
 6912                                 ip6 = mtod(init_pkt, struct ip6_hdr *);
 6913                                 sin6->sin6_family = AF_INET6;
 6914                                 sin6->sin6_len = sizeof(struct sockaddr_in6);
 6915                                 sin6->sin6_port = sh->src_port;
 6916                                 sin6->sin6_addr = ip6->ip6_src;
 6917                                 /* lookup address */
 6918                                 memcpy(&stc.address, &sin6->sin6_addr,
 6919                                     sizeof(struct in6_addr));
 6920                                 sin6->sin6_scope_id = 0;
 6921                                 stc.addr_type = SCTP_IPV6_ADDRESS;
 6922                                 stc.scope_id = 0;
 6923                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
 6924                                         /*
 6925                                          * FIX ME: does this have scope from
 6926                                          * rcvif?
 6927                                          */
 6928                                         (void)sa6_recoverscope(sin6);
 6929                                         stc.scope_id = sin6->sin6_scope_id;
 6930                                         sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
 6931                                         stc.loopback_scope = 1;
 6932                                         stc.local_scope = 0;
 6933                                         stc.site_scope = 1;
 6934                                         stc.ipv4_scope = 1;
 6935                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
 6936                                         /*
 6937                                          * If the new destination is a
 6938                                          * LINK_LOCAL we must have common
 6939                                          * both site and local scope. Don't
 6940                                          * set local scope though since we
 6941                                          * must depend on the source to be
 6942                                          * added implicitly. We cannot
 6943                                          * assure just because we share one
 6944                                          * link that all links are common.
 6945                                          */
 6946                                         stc.local_scope = 0;
 6947                                         stc.site_scope = 1;
 6948                                         stc.ipv4_scope = 1;
 6949                                         /*
 6950                                          * we start counting for the private
 6951                                          * address stuff at 1. since the
 6952                                          * link local we source from won't
 6953                                          * show up in our scoped count.
 6954                                          */
 6955                                         cnt_inits_to = 1;
 6956                                         /*
 6957                                          * pull out the scope_id from
 6958                                          * incoming pkt
 6959                                          */
 6960                                         /*
 6961                                          * FIX ME: does this have scope from
 6962                                          * rcvif?
 6963                                          */
 6964                                         (void)sa6_recoverscope(sin6);
 6965                                         stc.scope_id = sin6->sin6_scope_id;
 6966                                         sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
 6967                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
 6968                                         /*
 6969                                          * If the new destination is
 6970                                          * SITE_LOCAL then we must have site
 6971                                          * scope in common.
 6972                                          */
 6973                                         stc.site_scope = 1;
 6974                                 }
 6975                                 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
 6976                                 stc.laddr_type = SCTP_IPV6_ADDRESS;
 6977                                 break;
 6978                         }
 6979 #endif
 6980                 default:
 6981                         /* TSNH */
 6982                         goto do_a_abort;
 6983                         break;
 6984                 }
 6985         } else {
 6986                 /* set the scope per the existing tcb */
 6987 
 6988 #ifdef INET6
 6989                 struct sctp_nets *lnet;
 6990 
 6991 #endif
 6992 
 6993                 stc.loopback_scope = asoc->loopback_scope;
 6994                 stc.ipv4_scope = asoc->ipv4_local_scope;
 6995                 stc.site_scope = asoc->site_scope;
 6996                 stc.local_scope = asoc->local_scope;
 6997 #ifdef INET6
 6998                 /* Why do we not consider IPv4 LL addresses? */
 6999                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
 7000                         if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
 7001                                 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
 7002                                         /*
 7003                                          * if we have a LL address, start
 7004                                          * counting at 1.
 7005                                          */
 7006                                         cnt_inits_to = 1;
 7007                                 }
 7008                         }
 7009                 }
 7010 #endif
 7011                 /* use the net pointer */
 7012                 to = (struct sockaddr *)&net->ro._l_addr;
 7013                 switch (to->sa_family) {
 7014                 case AF_INET:
 7015                         sin = (struct sockaddr_in *)to;
 7016                         stc.address[0] = sin->sin_addr.s_addr;
 7017                         stc.address[1] = 0;
 7018                         stc.address[2] = 0;
 7019                         stc.address[3] = 0;
 7020                         stc.addr_type = SCTP_IPV4_ADDRESS;
 7021                         if (net->src_addr_selected == 0) {
 7022                                 /*
 7023                                  * strange case here, the INIT should have
 7024                                  * did the selection.
 7025                                  */
 7026                                 net->ro._s_addr = sctp_source_address_selection(inp,
 7027                                     stcb, (sctp_route_t *) & net->ro,
 7028                                     net, 0, vrf_id);
 7029                                 if (net->ro._s_addr == NULL)
 7030                                         return;
 7031 
 7032                                 net->src_addr_selected = 1;
 7033 
 7034                         }
 7035                         stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
 7036                         stc.laddress[1] = 0;
 7037                         stc.laddress[2] = 0;
 7038                         stc.laddress[3] = 0;
 7039                         stc.laddr_type = SCTP_IPV4_ADDRESS;
 7040                         break;
 7041 #ifdef INET6
 7042                 case AF_INET6:
 7043                         sin6 = (struct sockaddr_in6 *)to;
 7044                         memcpy(&stc.address, &sin6->sin6_addr,
 7045                             sizeof(struct in6_addr));
 7046                         stc.addr_type = SCTP_IPV6_ADDRESS;
 7047                         if (net->src_addr_selected == 0) {
 7048                                 /*
 7049                                  * strange case here, the INIT should have
 7050                                  * did the selection.
 7051                                  */
 7052                                 net->ro._s_addr = sctp_source_address_selection(inp,
 7053                                     stcb, (sctp_route_t *) & net->ro,
 7054                                     net, 0, vrf_id);
 7055                                 if (net->ro._s_addr == NULL)
 7056                                         return;
 7057 
 7058                                 net->src_addr_selected = 1;
 7059                         }
 7060                         memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
 7061                             sizeof(struct in6_addr));
 7062                         stc.laddr_type = SCTP_IPV6_ADDRESS;
 7063                         break;
 7064 #endif
 7065                 }
 7066         }
 7067         /* Now lets put the SCTP header in place */
 7068         initackm_out = mtod(m, struct sctp_init_msg *);
 7069         initackm_out->sh.src_port = inp->sctp_lport;
 7070         initackm_out->sh.dest_port = sh->src_port;
 7071         initackm_out->sh.v_tag = init_chk->init.initiate_tag;
 7072         /* Save it off for quick ref */
 7073         stc.peers_vtag = init_chk->init.initiate_tag;
 7074         initackm_out->sh.checksum = 0;  /* calculate later */
 7075         /* who are we */
 7076         memcpy(stc.identification, SCTP_VERSION_STRING,
 7077             min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
 7078         /* now the chunk header */
 7079         initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
 7080         initackm_out->msg.ch.chunk_flags = 0;
 7081         /* fill in later from mbuf we build */
 7082         initackm_out->msg.ch.chunk_length = 0;
 7083         /* place in my tag */
 7084         if ((asoc != NULL) &&
 7085             ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
 7086             (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
 7087             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
 7088                 /* re-use the v-tags and init-seq here */
 7089                 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
 7090                 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
 7091         } else {
 7092                 uint32_t vtag, itsn;
 7093 
 7094                 if (hold_inp_lock) {
 7095                         SCTP_INP_INCR_REF(inp);
 7096                         SCTP_INP_RUNLOCK(inp);
 7097                 }
 7098                 if (asoc) {
 7099                         atomic_add_int(&asoc->refcnt, 1);
 7100                         SCTP_TCB_UNLOCK(stcb);
 7101         new_tag:
 7102                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
 7103                         if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
 7104                                 /*
 7105                                  * Got a duplicate vtag on some guy behind a
 7106                                  * nat make sure we don't use it.
 7107                                  */
 7108                                 goto new_tag;
 7109                         }
 7110                         initackm_out->msg.init.initiate_tag = htonl(vtag);
 7111                         /* get a TSN to use too */
 7112                         itsn = sctp_select_initial_TSN(&inp->sctp_ep);
 7113                         initackm_out->msg.init.initial_tsn = htonl(itsn);
 7114                         SCTP_TCB_LOCK(stcb);
 7115                         atomic_add_int(&asoc->refcnt, -1);
 7116                 } else {
 7117                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
 7118                         initackm_out->msg.init.initiate_tag = htonl(vtag);
 7119                         /* get a TSN to use too */
 7120                         initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
 7121                 }
 7122                 if (hold_inp_lock) {
 7123                         SCTP_INP_RLOCK(inp);
 7124                         SCTP_INP_DECR_REF(inp);
 7125                 }
 7126         }
 7127         /* save away my tag to */
 7128         stc.my_vtag = initackm_out->msg.init.initiate_tag;
 7129 
 7130         /* set up some of the credits. */
 7131         so = inp->sctp_socket;
 7132         if (so == NULL) {
 7133                 /* memory problem */
 7134                 sctp_m_freem(m);
 7135                 return;
 7136         } else {
 7137                 initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
 7138         }
 7139         /* set what I want */
 7140         his_limit = ntohs(init_chk->init.num_inbound_streams);
 7141         /* choose what I want */
 7142         if (asoc != NULL) {
 7143                 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
 7144                         i_want = asoc->streamoutcnt;
 7145                 } else {
 7146                         i_want = inp->sctp_ep.pre_open_stream_count;
 7147                 }
 7148         } else {
 7149                 i_want = inp->sctp_ep.pre_open_stream_count;
 7150         }
 7151         if (his_limit < i_want) {
 7152                 /* I Want more :< */
 7153                 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
 7154         } else {
 7155                 /* I can have what I want :> */
 7156                 initackm_out->msg.init.num_outbound_streams = htons(i_want);
 7157         }
 7158         /* tell him his limt. */
 7159         initackm_out->msg.init.num_inbound_streams =
 7160             htons(inp->sctp_ep.max_open_streams_intome);
 7161         /* setup the ECN pointer */
 7162 
 7163         if (inp->sctp_ep.adaptation_layer_indicator) {
 7164                 struct sctp_adaptation_layer_indication *ali;
 7165 
 7166                 ali = (struct sctp_adaptation_layer_indication *)(
 7167                     (caddr_t)initackm_out + sizeof(*initackm_out));
 7168                 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
 7169                 ali->ph.param_length = htons(sizeof(*ali));
 7170                 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
 7171                 SCTP_BUF_LEN(m) += sizeof(*ali);
 7172                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
 7173                     sizeof(*ali));
 7174         } else {
 7175                 ecn = (struct sctp_ecn_supported_param *)(
 7176                     (caddr_t)initackm_out + sizeof(*initackm_out));
 7177         }
 7178 
 7179         /* ECN parameter */
 7180         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
 7181                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
 7182                 ecn->ph.param_length = htons(sizeof(*ecn));
 7183                 SCTP_BUF_LEN(m) += sizeof(*ecn);
 7184 
 7185                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
 7186                     sizeof(*ecn));
 7187         } else {
 7188                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
 7189         }
 7190         /* And now tell the peer we do  pr-sctp */
 7191         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
 7192         prsctp->ph.param_length = htons(sizeof(*prsctp));
 7193         SCTP_BUF_LEN(m) += sizeof(*prsctp);
 7194         if (nat_friendly) {
 7195                 /* Add NAT friendly parameter */
 7196                 struct sctp_paramhdr *ph;
 7197 
 7198                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 7199                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
 7200                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
 7201                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
 7202         }
 7203         /* And now tell the peer we do all the extensions */
 7204         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 7205         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
 7206         num_ext = 0;
 7207         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
 7208         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
 7209         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
 7210         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
 7211         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
 7212         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
 7213                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
 7214         /*
 7215          * EY  if the sysctl variable is set, tell the assoc. initiator that
 7216          * we do nr_sack
 7217          */
 7218         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
 7219                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
 7220         p_len = sizeof(*pr_supported) + num_ext;
 7221         pr_supported->ph.param_length = htons(p_len);
 7222         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
 7223         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 7224 
 7225         /* ECN nonce: And now tell the peer we support ECN nonce */
 7226         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
 7227                 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
 7228                     ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
 7229                 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
 7230                 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
 7231                 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
 7232         }
 7233         /* add authentication parameters */
 7234         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
 7235                 struct sctp_auth_random *randp;
 7236                 struct sctp_auth_hmac_algo *hmacs;
 7237                 struct sctp_auth_chunk_list *chunks;
 7238                 uint16_t random_len;
 7239 
 7240                 /* generate and add RANDOM parameter */
 7241                 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
 7242                 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 7243                 randp->ph.param_type = htons(SCTP_RANDOM);
 7244                 p_len = sizeof(*randp) + random_len;
 7245                 randp->ph.param_length = htons(p_len);
 7246                 SCTP_READ_RANDOM(randp->random_data, random_len);
 7247                 /* zero out any padding required */
 7248                 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
 7249                 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 7250 
 7251                 /* add HMAC_ALGO parameter */
 7252                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 7253                 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
 7254                     (uint8_t *) hmacs->hmac_ids);
 7255                 if (p_len > 0) {
 7256                         p_len += sizeof(*hmacs);
 7257                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
 7258                         hmacs->ph.param_length = htons(p_len);
 7259                         /* zero out any padding required */
 7260                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
 7261                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 7262                 }
 7263                 /* add CHUNKS parameter */
 7264                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
 7265                 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
 7266                     chunks->chunk_types);
 7267                 if (p_len > 0) {
 7268                         p_len += sizeof(*chunks);
 7269                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
 7270                         chunks->ph.param_length = htons(p_len);
 7271                         /* zero out any padding required */
 7272                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
 7273                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
 7274                 }
 7275         }
 7276         m_at = m;
 7277         /* now the addresses */
 7278         {
 7279                 struct sctp_scoping scp;
 7280 
 7281                 /*
 7282                  * To optimize this we could put the scoping stuff into a
 7283                  * structure and remove the individual uint8's from the stc
 7284                  * structure. Then we could just sifa in the address within
 7285                  * the stc.. but for now this is a quick hack to get the
 7286                  * address stuff teased apart.
 7287                  */
 7288                 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
 7289                 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
 7290                 scp.loopback_scope = stc.loopback_scope;
 7291                 scp.ipv4_local_scope = stc.ipv4_scope;
 7292                 scp.local_scope = stc.local_scope;
 7293                 scp.site_scope = stc.site_scope;
 7294                 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
 7295         }
 7296 
 7297         /* tack on the operational error if present */
 7298         if (op_err) {
 7299                 struct mbuf *ol;
 7300                 int llen;
 7301 
 7302                 llen = 0;
 7303                 ol = op_err;
 7304                 while (ol) {
 7305                         llen += SCTP_BUF_LEN(ol);
 7306                         ol = SCTP_BUF_NEXT(ol);
 7307                 }
 7308                 if (llen % 4) {
 7309                         /* must add a pad to the param */
 7310                         uint32_t cpthis = 0;
 7311                         int padlen;
 7312 
 7313                         padlen = 4 - (llen % 4);
 7314                         m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
 7315                 }
 7316                 while (SCTP_BUF_NEXT(m_at) != NULL) {
 7317                         m_at = SCTP_BUF_NEXT(m_at);
 7318                 }
 7319                 SCTP_BUF_NEXT(m_at) = op_err;
 7320                 while (SCTP_BUF_NEXT(m_at) != NULL) {
 7321                         m_at = SCTP_BUF_NEXT(m_at);
 7322                 }
 7323         }
 7324         /* pre-calulate the size and update pkt header and chunk header */
 7325         p_len = 0;
 7326         for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
 7327                 p_len += SCTP_BUF_LEN(m_tmp);
 7328                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
 7329                         /* m_tmp should now point to last one */
 7330                         break;
 7331                 }
 7332         }
 7333 
 7334         /* Now we must build a cookie */
 7335         m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
 7336             sizeof(struct sctphdr), &stc, &signature);
 7337         if (m_cookie == NULL) {
 7338                 /* memory problem */
 7339                 sctp_m_freem(m);
 7340                 return;
 7341         }
 7342         /* Now append the cookie to the end and update the space/size */
 7343         SCTP_BUF_NEXT(m_tmp) = m_cookie;
 7344 
 7345         for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
 7346                 p_len += SCTP_BUF_LEN(m_tmp);
 7347                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
 7348                         /* m_tmp should now point to last one */
 7349                         mp_last = m_tmp;
 7350                         break;
 7351                 }
 7352         }
 7353         /*
 7354          * Place in the size, but we don't include the last pad (if any) in
 7355          * the INIT-ACK.
 7356          */
 7357         initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
 7358 
 7359         /*
 7360          * Time to sign the cookie, we don't sign over the cookie signature
 7361          * though thus we set trailer.
 7362          */
 7363         (void)sctp_hmac_m(SCTP_HMAC,
 7364             (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
 7365             SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
 7366             (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
 7367         /*
 7368          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
 7369          * here since the timer will drive a retranmission.
 7370          */
 7371         padval = p_len % 4;
 7372         if ((padval) && (mp_last)) {
 7373                 /* see my previous comments on mp_last */
 7374                 int ret;
 7375 
 7376                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
 7377                 if (ret) {
 7378                         /* Houston we have a problem, no space */
 7379                         sctp_m_freem(m);
 7380                         return;
 7381                 }
 7382                 p_len += padval;
 7383         }
 7384         if (stc.loopback_scope) {
 7385                 over_addr = &store1;
 7386         } else {
 7387                 over_addr = NULL;
 7388 
 7389         }
 7390 
 7391         (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
 7392             0, NULL, 0, port, SCTP_SO_NOT_LOCKED, over_addr);
 7393         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
 7394 }
 7395 
 7396 
 7397 void
 7398 sctp_insert_on_wheel(struct sctp_tcb *stcb,
 7399     struct sctp_association *asoc,
 7400     struct sctp_stream_out *strq, int holds_lock)
 7401 {
 7402         struct sctp_stream_out *stre, *strn;
 7403 
 7404         if (holds_lock == 0) {
 7405                 SCTP_TCB_SEND_LOCK(stcb);
 7406         }
 7407         if ((strq->next_spoke.tqe_next) ||
 7408             (strq->next_spoke.tqe_prev)) {
 7409                 /* already on wheel */
 7410                 goto outof_here;
 7411         }
 7412         stre = TAILQ_FIRST(&asoc->out_wheel);
 7413         if (stre == NULL) {
 7414                 /* only one on wheel */
 7415                 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
 7416                 goto outof_here;
 7417         }
 7418         for (; stre; stre = strn) {
 7419                 strn = TAILQ_NEXT(stre, next_spoke);
 7420                 if (stre->stream_no > strq->stream_no) {
 7421                         TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
 7422                         goto outof_here;
 7423                 } else if (stre->stream_no == strq->stream_no) {
 7424                         /* huh, should not happen */
 7425                         goto outof_here;
 7426                 } else if (strn == NULL) {
 7427                         /* next one is null */
 7428                         TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
 7429                             next_spoke);
 7430                 }
 7431         }
 7432 outof_here:
 7433         if (holds_lock == 0) {
 7434                 SCTP_TCB_SEND_UNLOCK(stcb);
 7435         }
 7436 }
 7437 
 7438 static void
 7439 sctp_remove_from_wheel(struct sctp_tcb *stcb,
 7440     struct sctp_association *asoc,
 7441     struct sctp_stream_out *strq)
 7442 {
 7443         /* take off and then setup so we know it is not on the wheel */
 7444         SCTP_TCB_SEND_LOCK(stcb);
 7445         if (TAILQ_FIRST(&strq->outqueue)) {
 7446                 /* more was added */
 7447                 SCTP_TCB_SEND_UNLOCK(stcb);
 7448                 return;
 7449         }
 7450         TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
 7451         strq->next_spoke.tqe_next = NULL;
 7452         strq->next_spoke.tqe_prev = NULL;
 7453         SCTP_TCB_SEND_UNLOCK(stcb);
 7454 }
 7455 
 7456 static void
 7457 sctp_prune_prsctp(struct sctp_tcb *stcb,
 7458     struct sctp_association *asoc,
 7459     struct sctp_sndrcvinfo *srcv,
 7460     int dataout)
 7461 {
 7462         int freed_spc = 0;
 7463         struct sctp_tmit_chunk *chk, *nchk;
 7464 
 7465         SCTP_TCB_LOCK_ASSERT(stcb);
 7466         if ((asoc->peer_supports_prsctp) &&
 7467             (asoc->sent_queue_cnt_removeable > 0)) {
 7468                 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
 7469                         /*
 7470                          * Look for chunks marked with the PR_SCTP flag AND
 7471                          * the buffer space flag. If the one being sent is
 7472                          * equal or greater priority then purge the old one
 7473                          * and free some space.
 7474                          */
 7475                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
 7476                                 /*
 7477                                  * This one is PR-SCTP AND buffer space
 7478                                  * limited type
 7479                                  */
 7480                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
 7481                                         /*
 7482                                          * Lower numbers equates to higher
 7483                                          * priority so if the one we are
 7484                                          * looking at has a larger or equal
 7485                                          * priority we want to drop the data
 7486                                          * and NOT retransmit it.
 7487                                          */
 7488                                         if (chk->data) {
 7489                                                 /*
 7490                                                  * We release the book_size
 7491                                                  * if the mbuf is here
 7492                                                  */
 7493                                                 int ret_spc;
 7494                                                 int cause;
 7495 
 7496                                                 if (chk->sent > SCTP_DATAGRAM_UNSENT)
 7497                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
 7498                                                 else
 7499                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
 7500                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
 7501                                                     cause,
 7502                                                     &asoc->sent_queue, SCTP_SO_LOCKED);
 7503                                                 freed_spc += ret_spc;
 7504                                                 if (freed_spc >= dataout) {
 7505                                                         return;
 7506                                                 }
 7507                                         }       /* if chunk was present */
 7508                                 }       /* if of sufficent priority */
 7509                         }       /* if chunk has enabled */
 7510                 }               /* tailqforeach */
 7511 
 7512                 chk = TAILQ_FIRST(&asoc->send_queue);
 7513                 while (chk) {
 7514                         nchk = TAILQ_NEXT(chk, sctp_next);
 7515                         /* Here we must move to the sent queue and mark */
 7516                         if (PR_SCTP_TTL_ENABLED(chk->flags)) {
 7517                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
 7518                                         if (chk->data) {
 7519                                                 /*
 7520                                                  * We release the book_size
 7521                                                  * if the mbuf is here
 7522                                                  */
 7523                                                 int ret_spc;
 7524 
 7525                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
 7526                                                     SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
 7527                                                     &asoc->send_queue, SCTP_SO_LOCKED);
 7528 
 7529                                                 freed_spc += ret_spc;
 7530                                                 if (freed_spc >= dataout) {
 7531                                                         return;
 7532                                                 }
 7533                                         }       /* end if chk->data */
 7534                                 }       /* end if right class */
 7535                         }       /* end if chk pr-sctp */
 7536                         chk = nchk;
 7537                 }               /* end while (chk) */
 7538         }                       /* if enabled in asoc */
 7539 }
 7540 
 7541 int
 7542 sctp_get_frag_point(struct sctp_tcb *stcb,
 7543     struct sctp_association *asoc)
 7544 {
 7545         int siz, ovh;
 7546 
 7547         /*
 7548          * For endpoints that have both v6 and v4 addresses we must reserve
 7549          * room for the ipv6 header, for those that are only dealing with V4
 7550          * we use a larger frag point.
 7551          */
 7552         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 7553                 ovh = SCTP_MED_OVERHEAD;
 7554         } else {
 7555                 ovh = SCTP_MED_V4_OVERHEAD;
 7556         }
 7557 
 7558         if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
 7559                 siz = asoc->smallest_mtu - ovh;
 7560         else
 7561                 siz = (stcb->asoc.sctp_frag_point - ovh);
 7562         /*
 7563          * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
 7564          */
 7565         /* A data chunk MUST fit in a cluster */
 7566         /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
 7567         /* } */
 7568 
 7569         /* adjust for an AUTH chunk if DATA requires auth */
 7570         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
 7571                 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
 7572 
 7573         if (siz % 4) {
 7574                 /* make it an even word boundary please */
 7575                 siz -= (siz % 4);
 7576         }
 7577         return (siz);
 7578 }
 7579 
 7580 static void
 7581 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
 7582     struct sctp_stream_queue_pending *sp)
 7583 {
 7584         sp->pr_sctp_on = 0;
 7585         if (stcb->asoc.peer_supports_prsctp) {
 7586                 /*
 7587                  * We assume that the user wants PR_SCTP_TTL if the user
 7588                  * provides a positive lifetime but does not specify any
 7589                  * PR_SCTP policy. This is a BAD assumption and causes
 7590                  * problems at least with the U-Vancovers MPI folks. I will
 7591                  * change this to be no policy means NO PR-SCTP.
 7592                  */
 7593                 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
 7594                         sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
 7595                         sp->pr_sctp_on = 1;
 7596                 } else {
 7597                         return;
 7598                 }
 7599                 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
 7600                 case CHUNK_FLAGS_PR_SCTP_BUF:
 7601                         /*
 7602                          * Time to live is a priority stored in tv_sec when
 7603                          * doing the buffer drop thing.
 7604                          */
 7605                         sp->ts.tv_sec = sp->timetolive;
 7606                         sp->ts.tv_usec = 0;
 7607                         break;
 7608                 case CHUNK_FLAGS_PR_SCTP_TTL:
 7609                         {
 7610                                 struct timeval tv;
 7611 
 7612                                 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
 7613                                 tv.tv_sec = sp->timetolive / 1000;
 7614                                 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
 7615                                 /*
 7616                                  * TODO sctp_constants.h needs alternative
 7617                                  * time macros when _KERNEL is undefined.
 7618                                  */
 7619                                 timevaladd(&sp->ts, &tv);
 7620                         }
 7621                         break;
 7622                 case CHUNK_FLAGS_PR_SCTP_RTX:
 7623                         /*
 7624                          * Time to live is a the number or retransmissions
 7625                          * stored in tv_sec.
 7626                          */
 7627                         sp->ts.tv_sec = sp->timetolive;
 7628                         sp->ts.tv_usec = 0;
 7629                         break;
 7630                 default:
 7631