Print this page
    
915 ill_taskq_dispatch() race condition
        (originally Joyent OS-119 panic from mutex_vector_exit)
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/inet/ip/ip.c
          +++ new/usr/src/uts/common/inet/ip/ip.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  
    | 
      ↓ open down ↓ | 
    14 lines elided | 
    
      ↑ open up ↑ | 
  
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright (c) 1990 Mentat Inc.
       25 + * Copyright (c) 2011 Joyent, Inc. All rights reserved.
  25   26   */
  26   27  
  27   28  #include <sys/types.h>
  28   29  #include <sys/stream.h>
  29   30  #include <sys/dlpi.h>
  30   31  #include <sys/stropts.h>
  31   32  #include <sys/sysmacros.h>
  32   33  #include <sys/strsubr.h>
  33   34  #include <sys/strlog.h>
  34   35  #include <sys/strsun.h>
  35   36  #include <sys/zone.h>
  36   37  #define _SUN_TPI_VERSION 2
  37   38  #include <sys/tihdr.h>
  38   39  #include <sys/xti_inet.h>
  39   40  #include <sys/ddi.h>
  40   41  #include <sys/suntpi.h>
  41   42  #include <sys/cmn_err.h>
  42   43  #include <sys/debug.h>
  43   44  #include <sys/kobj.h>
  44   45  #include <sys/modctl.h>
  45   46  #include <sys/atomic.h>
  46   47  #include <sys/policy.h>
  47   48  #include <sys/priv.h>
  48   49  #include <sys/taskq.h>
  49   50  
  50   51  #include <sys/systm.h>
  51   52  #include <sys/param.h>
  52   53  #include <sys/kmem.h>
  53   54  #include <sys/sdt.h>
  54   55  #include <sys/socket.h>
  55   56  #include <sys/vtrace.h>
  56   57  #include <sys/isa_defs.h>
  57   58  #include <sys/mac.h>
  58   59  #include <net/if.h>
  59   60  #include <net/if_arp.h>
  60   61  #include <net/route.h>
  61   62  #include <sys/sockio.h>
  62   63  #include <netinet/in.h>
  63   64  #include <net/if_dl.h>
  64   65  
  65   66  #include <inet/common.h>
  66   67  #include <inet/mi.h>
  67   68  #include <inet/mib2.h>
  68   69  #include <inet/nd.h>
  69   70  #include <inet/arp.h>
  70   71  #include <inet/snmpcom.h>
  71   72  #include <inet/optcom.h>
  72   73  #include <inet/kstatcom.h>
  73   74  
  74   75  #include <netinet/igmp_var.h>
  75   76  #include <netinet/ip6.h>
  76   77  #include <netinet/icmp6.h>
  77   78  #include <netinet/sctp.h>
  78   79  
  79   80  #include <inet/ip.h>
  80   81  #include <inet/ip_impl.h>
  81   82  #include <inet/ip6.h>
  82   83  #include <inet/ip6_asp.h>
  83   84  #include <inet/tcp.h>
  84   85  #include <inet/tcp_impl.h>
  85   86  #include <inet/ip_multi.h>
  86   87  #include <inet/ip_if.h>
  87   88  #include <inet/ip_ire.h>
  88   89  #include <inet/ip_ftable.h>
  89   90  #include <inet/ip_rts.h>
  90   91  #include <inet/ip_ndp.h>
  91   92  #include <inet/ip_listutils.h>
  92   93  #include <netinet/igmp.h>
  93   94  #include <netinet/ip_mroute.h>
  94   95  #include <inet/ipp_common.h>
  95   96  
  96   97  #include <net/pfkeyv2.h>
  97   98  #include <inet/sadb.h>
  98   99  #include <inet/ipsec_impl.h>
  99  100  #include <inet/iptun/iptun_impl.h>
 100  101  #include <inet/ipdrop.h>
 101  102  #include <inet/ip_netinfo.h>
 102  103  #include <inet/ilb_ip.h>
 103  104  
 104  105  #include <sys/ethernet.h>
 105  106  #include <net/if_types.h>
 106  107  #include <sys/cpuvar.h>
 107  108  
 108  109  #include <ipp/ipp.h>
 109  110  #include <ipp/ipp_impl.h>
 110  111  #include <ipp/ipgpc/ipgpc.h>
 111  112  
 112  113  #include <sys/pattr.h>
 113  114  #include <inet/ipclassifier.h>
 114  115  #include <inet/sctp_ip.h>
 115  116  #include <inet/sctp/sctp_impl.h>
 116  117  #include <inet/udp_impl.h>
 117  118  #include <inet/rawip_impl.h>
 118  119  #include <inet/rts_impl.h>
 119  120  
 120  121  #include <sys/tsol/label.h>
 121  122  #include <sys/tsol/tnet.h>
 122  123  
 123  124  #include <sys/squeue_impl.h>
 124  125  #include <inet/ip_arp.h>
 125  126  
 126  127  #include <sys/clock_impl.h>     /* For LBOLT_FASTPATH{,64} */
 127  128  
 128  129  /*
 129  130   * Values for squeue switch:
 130  131   * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
 131  132   * IP_SQUEUE_ENTER: SQ_PROCESS
 132  133   * IP_SQUEUE_FILL: SQ_FILL
 133  134   */
 134  135  int ip_squeue_enter = IP_SQUEUE_ENTER;  /* Setable in /etc/system */
 135  136  
 136  137  int ip_squeue_flag;
 137  138  
 138  139  /*
 139  140   * Setable in /etc/system
 140  141   */
 141  142  int ip_poll_normal_ms = 100;
 142  143  int ip_poll_normal_ticks = 0;
 143  144  int ip_modclose_ackwait_ms = 3000;
 144  145  
 145  146  /*
 146  147   * It would be nice to have these present only in DEBUG systems, but the
 147  148   * current design of the global symbol checking logic requires them to be
 148  149   * unconditionally present.
 149  150   */
 150  151  uint_t ip_thread_data;                  /* TSD key for debug support */
 151  152  krwlock_t ip_thread_rwlock;
 152  153  list_t  ip_thread_list;
 153  154  
 154  155  /*
 155  156   * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
 156  157   */
 157  158  
 158  159  struct listptr_s {
 159  160          mblk_t  *lp_head;       /* pointer to the head of the list */
 160  161          mblk_t  *lp_tail;       /* pointer to the tail of the list */
 161  162  };
 162  163  
 163  164  typedef struct listptr_s listptr_t;
 164  165  
 165  166  /*
 166  167   * This is used by ip_snmp_get_mib2_ip_route_media and
 167  168   * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
 168  169   */
 169  170  typedef struct iproutedata_s {
 170  171          uint_t          ird_idx;
 171  172          uint_t          ird_flags;      /* see below */
 172  173          listptr_t       ird_route;      /* ipRouteEntryTable */
 173  174          listptr_t       ird_netmedia;   /* ipNetToMediaEntryTable */
 174  175          listptr_t       ird_attrs;      /* ipRouteAttributeTable */
 175  176  } iproutedata_t;
 176  177  
 177  178  /* Include ire_testhidden and IRE_IF_CLONE routes */
 178  179  #define IRD_REPORT_ALL  0x01
 179  180  
 180  181  /*
 181  182   * Cluster specific hooks. These should be NULL when booted as a non-cluster
 182  183   */
 183  184  
 184  185  /*
 185  186   * Hook functions to enable cluster networking
 186  187   * On non-clustered systems these vectors must always be NULL.
 187  188   *
 188  189   * Hook function to Check ip specified ip address is a shared ip address
 189  190   * in the cluster
 190  191   *
 191  192   */
 192  193  int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol,
 193  194      sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL;
 194  195  
 195  196  /*
 196  197   * Hook function to generate cluster wide ip fragment identifier
 197  198   */
 198  199  uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol,
 199  200      sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp,
 200  201      void *args) = NULL;
 201  202  
 202  203  /*
 203  204   * Hook function to generate cluster wide SPI.
 204  205   */
 205  206  void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t,
 206  207      void *) = NULL;
 207  208  
 208  209  /*
 209  210   * Hook function to verify if the SPI is already utlized.
 210  211   */
 211  212  
 212  213  int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
 213  214  
 214  215  /*
 215  216   * Hook function to delete the SPI from the cluster wide repository.
 216  217   */
 217  218  
 218  219  void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
 219  220  
 220  221  /*
 221  222   * Hook function to inform the cluster when packet received on an IDLE SA
 222  223   */
 223  224  
 224  225  void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t,
 225  226      in6_addr_t, in6_addr_t, void *) = NULL;
 226  227  
 227  228  /*
 228  229   * Synchronization notes:
 229  230   *
 230  231   * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
 231  232   * MT level protection given by STREAMS. IP uses a combination of its own
 232  233   * internal serialization mechanism and standard Solaris locking techniques.
 233  234   * The internal serialization is per phyint.  This is used to serialize
 234  235   * plumbing operations, IPMP operations, most set ioctls, etc.
 235  236   *
 236  237   * Plumbing is a long sequence of operations involving message
 237  238   * exchanges between IP, ARP and device drivers. Many set ioctls are typically
 238  239   * involved in plumbing operations. A natural model is to serialize these
 239  240   * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
 240  241   * parallel without any interference. But various set ioctls on hme0 are best
 241  242   * serialized, along with IPMP operations and processing of DLPI control
 242  243   * messages received from drivers on a per phyint basis. This serialization is
 243  244   * provided by the ipsq_t and primitives operating on this. Details can
 244  245   * be found in ip_if.c above the core primitives operating on ipsq_t.
 245  246   *
 246  247   * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
 247  248   * Simiarly lookup of an ire by a thread also returns a refheld ire.
 248  249   * In addition ipif's and ill's referenced by the ire are also indirectly
 249  250   * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
 250  251   * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
 251  252   * address of an ipif has to go through the ipsq_t. This ensures that only
 252  253   * one such exclusive operation proceeds at any time on the ipif. It then
 253  254   * waits for all refcnts
 254  255   * associated with this ipif to come down to zero. The address is changed
 255  256   * only after the ipif has been quiesced. Then the ipif is brought up again.
 256  257   * More details are described above the comment in ip_sioctl_flags.
 257  258   *
 258  259   * Packet processing is based mostly on IREs and are fully multi-threaded
 259  260   * using standard Solaris MT techniques.
 260  261   *
 261  262   * There are explicit locks in IP to handle:
 262  263   * - The ip_g_head list maintained by mi_open_link() and friends.
 263  264   *
 264  265   * - The reassembly data structures (one lock per hash bucket)
 265  266   *
 266  267   * - conn_lock is meant to protect conn_t fields. The fields actually
 267  268   *   protected by conn_lock are documented in the conn_t definition.
 268  269   *
 269  270   * - ire_lock to protect some of the fields of the ire, IRE tables
 270  271   *   (one lock per hash bucket). Refer to ip_ire.c for details.
 271  272   *
 272  273   * - ndp_g_lock and ncec_lock for protecting NCEs.
 273  274   *
 274  275   * - ill_lock protects fields of the ill and ipif. Details in ip.h
 275  276   *
 276  277   * - ill_g_lock: This is a global reader/writer lock. Protects the following
 277  278   *      * The AVL tree based global multi list of all ills.
 278  279   *      * The linked list of all ipifs of an ill
 279  280   *      * The <ipsq-xop> mapping
 280  281   *      * <ill-phyint> association
 281  282   *   Insertion/deletion of an ill in the system, insertion/deletion of an ipif
 282  283   *   into an ill, changing the <ipsq-xop> mapping of an ill, changing the
 283  284   *   <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
 284  285   *   writer for the actual duration of the insertion/deletion/change.
 285  286   *
 286  287   * - ill_lock:  This is a per ill mutex.
 287  288   *   It protects some members of the ill_t struct; see ip.h for details.
 288  289   *   It also protects the <ill-phyint> assoc.
 289  290   *   It also protects the list of ipifs hanging off the ill.
 290  291   *
 291  292   * - ipsq_lock: This is a per ipsq_t mutex lock.
 292  293   *   This protects some members of the ipsq_t struct; see ip.h for details.
 293  294   *   It also protects the <ipsq-ipxop> mapping
 294  295   *
 295  296   * - ipx_lock: This is a per ipxop_t mutex lock.
 296  297   *   This protects some members of the ipxop_t struct; see ip.h for details.
 297  298   *
 298  299   * - phyint_lock: This is a per phyint mutex lock. Protects just the
 299  300   *   phyint_flags
 300  301   *
 301  302   * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
 302  303   *   This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
 303  304   *   uniqueness check also done atomically.
 304  305   *
 305  306   * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
 306  307   *   group list linked by ill_usesrc_grp_next. It also protects the
 307  308   *   ill_usesrc_ifindex field. It is taken as a writer when a member of the
 308  309   *   group is being added or deleted.  This lock is taken as a reader when
 309  310   *   walking the list/group(eg: to get the number of members in a usesrc group).
 310  311   *   Note, it is only necessary to take this lock if the ill_usesrc_grp_next
 311  312   *   field is changing state i.e from NULL to non-NULL or vice-versa. For
 312  313   *   example, it is not necessary to take this lock in the initial portion
 313  314   *   of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
 314  315   *   operations are executed exclusively and that ensures that the "usesrc
 315  316   *   group state" cannot change. The "usesrc group state" change can happen
 316  317   *   only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
 317  318   *
 318  319   * Changing <ill-phyint>, <ipsq-xop> assocications:
 319  320   *
 320  321   * To change the <ill-phyint> association, the ill_g_lock must be held
 321  322   * as writer, and the ill_locks of both the v4 and v6 instance of the ill
 322  323   * must be held.
 323  324   *
 324  325   * To change the <ipsq-xop> association, the ill_g_lock must be held as
 325  326   * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
 326  327   * This is only done when ills are added or removed from IPMP groups.
 327  328   *
 328  329   * To add or delete an ipif from the list of ipifs hanging off the ill,
 329  330   * ill_g_lock (writer) and ill_lock must be held and the thread must be
 330  331   * a writer on the associated ipsq.
 331  332   *
 332  333   * To add or delete an ill to the system, the ill_g_lock must be held as
 333  334   * writer and the thread must be a writer on the associated ipsq.
 334  335   *
 335  336   * To add or delete an ilm to an ill, the ill_lock must be held and the thread
 336  337   * must be a writer on the associated ipsq.
 337  338   *
 338  339   * Lock hierarchy
 339  340   *
 340  341   * Some lock hierarchy scenarios are listed below.
 341  342   *
 342  343   * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
 343  344   * ill_g_lock -> ill_lock(s) -> phyint_lock
 344  345   * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
 345  346   * ill_g_lock -> ip_addr_avail_lock
 346  347   * conn_lock -> irb_lock -> ill_lock -> ire_lock
 347  348   * ill_g_lock -> ip_g_nd_lock
 348  349   * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
 349  350   * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
 350  351   * arl_lock -> ill_lock
 351  352   * ips_ire_dep_lock -> irb_lock
 352  353   *
 353  354   * When more than 1 ill lock is needed to be held, all ill lock addresses
 354  355   * are sorted on address and locked starting from highest addressed lock
 355  356   * downward.
 356  357   *
 357  358   * Multicast scenarios
 358  359   * ips_ill_g_lock -> ill_mcast_lock
 359  360   * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
 360  361   * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
 361  362   * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
 362  363   * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
 363  364   * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
 364  365   *
 365  366   * IPsec scenarios
 366  367   *
 367  368   * ipsa_lock -> ill_g_lock -> ill_lock
 368  369   * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
 369  370   *
 370  371   * Trusted Solaris scenarios
 371  372   *
 372  373   * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
 373  374   * igsa_lock -> gcdb_lock
 374  375   * gcgrp_rwlock -> ire_lock
 375  376   * gcgrp_rwlock -> gcdb_lock
 376  377   *
 377  378   * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
 378  379   *
 379  380   * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
 380  381   * sq_lock -> conn_lock -> QLOCK(q)
 381  382   * ill_lock -> ft_lock -> fe_lock
 382  383   *
 383  384   * Routing/forwarding table locking notes:
 384  385   *
 385  386   * Lock acquisition order: Radix tree lock, irb_lock.
 386  387   * Requirements:
 387  388   * i.  Walker must not hold any locks during the walker callback.
 388  389   * ii  Walker must not see a truncated tree during the walk because of any node
 389  390   *     deletion.
 390  391   * iii Existing code assumes ire_bucket is valid if it is non-null and is used
 391  392   *     in many places in the code to walk the irb list. Thus even if all the
 392  393   *     ires in a bucket have been deleted, we still can't free the radix node
 393  394   *     until the ires have actually been inactive'd (freed).
 394  395   *
 395  396   * Tree traversal - Need to hold the global tree lock in read mode.
 396  397   * Before dropping the global tree lock, need to either increment the ire_refcnt
 397  398   * to ensure that the radix node can't be deleted.
 398  399   *
 399  400   * Tree add - Need to hold the global tree lock in write mode to add a
 400  401   * radix node. To prevent the node from being deleted, increment the
 401  402   * irb_refcnt, after the node is added to the tree. The ire itself is
 402  403   * added later while holding the irb_lock, but not the tree lock.
 403  404   *
 404  405   * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
 405  406   * All associated ires must be inactive (i.e. freed), and irb_refcnt
 406  407   * must be zero.
 407  408   *
 408  409   * Walker - Increment irb_refcnt before calling the walker callback. Hold the
 409  410   * global tree lock (read mode) for traversal.
 410  411   *
 411  412   * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
 412  413   * hence we will acquire irb_lock while holding ips_ire_dep_lock.
 413  414   *
 414  415   * IPsec notes :
 415  416   *
 416  417   * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
 417  418   * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
 418  419   * ip_xmit_attr_t has the
 419  420   * information used by the IPsec code for applying the right level of
 420  421   * protection. The information initialized by IP in the ip_xmit_attr_t
 421  422   * is determined by the per-socket policy or global policy in the system.
 422  423   * For inbound datagrams, the ip_recv_attr_t
 423  424   * starts out with nothing in it. It gets filled
 424  425   * with the right information if it goes through the AH/ESP code, which
 425  426   * happens if the incoming packet is secure. The information initialized
 426  427   * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
 427  428   * the policy requirements needed by per-socket policy or global policy
 428  429   * is met or not.
 429  430   *
 430  431   * For fully connected sockets i.e dst, src [addr, port] is known,
 431  432   * conn_policy_cached is set indicating that policy has been cached.
 432  433   * conn_in_enforce_policy may or may not be set depending on whether
 433  434   * there is a global policy match or per-socket policy match.
 434  435   * Policy inheriting happpens in ip_policy_set once the destination is known.
 435  436   * Once the right policy is set on the conn_t, policy cannot change for
 436  437   * this socket. This makes life simpler for TCP (UDP ?) where
 437  438   * re-transmissions go out with the same policy. For symmetry, policy
 438  439   * is cached for fully connected UDP sockets also. Thus if policy is cached,
 439  440   * it also implies that policy is latched i.e policy cannot change
 440  441   * on these sockets. As we have the right policy on the conn, we don't
 441  442   * have to lookup global policy for every outbound and inbound datagram
 442  443   * and thus serving as an optimization. Note that a global policy change
 443  444   * does not affect fully connected sockets if they have policy. If fully
 444  445   * connected sockets did not have any policy associated with it, global
 445  446   * policy change may affect them.
 446  447   *
 447  448   * IP Flow control notes:
 448  449   * ---------------------
 449  450   * Non-TCP streams are flow controlled by IP. The way this is accomplished
 450  451   * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
 451  452   * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
 452  453   * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
 453  454   * functions.
 454  455   *
 455  456   * Per Tx ring udp flow control:
 456  457   * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
 457  458   * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
 458  459   *
 459  460   * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
 460  461   * To achieve best performance, outgoing traffic need to be fanned out among
 461  462   * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
 462  463   * traffic out of the NIC and it takes a fanout hint. UDP connections pass
 463  464   * the address of connp as fanout hint to mac_tx(). Under flow controlled
 464  465   * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
 465  466   * cookie points to a specific Tx ring that is blocked. The cookie is used to
 466  467   * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
 467  468   * point to drain_lists (idl_t's). These drain list will store the blocked UDP
 468  469   * connp's. The drain list is not a single list but a configurable number of
 469  470   * lists.
 470  471   *
 471  472   * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
 472  473   * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
 473  474   * which is equal to 128. This array in turn contains a pointer to idl_t[],
 474  475   * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
 475  476   * list will point to the list of connp's that are flow controlled.
 476  477   *
 477  478   *                      ---------------   -------   -------   -------
 478  479   *                   |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
 479  480   *                   |  ---------------   -------   -------   -------
 480  481   *                   |  ---------------   -------   -------   -------
 481  482   *                   |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
 482  483   * ----------------  |  ---------------   -------   -------   -------
 483  484   * |idl_tx_list[0]|->|  ---------------   -------   -------   -------
 484  485   * ----------------  |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
 485  486   *                   |  ---------------   -------   -------   -------
 486  487   *                   .        .              .         .         .
 487  488   *                   |  ---------------   -------   -------   -------
 488  489   *                   |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
 489  490   *                      ---------------   -------   -------   -------
 490  491   *                      ---------------   -------   -------   -------
 491  492   *                   |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
 492  493   *                   |  ---------------   -------   -------   -------
 493  494   *                   |  ---------------   -------   -------   -------
 494  495   * ----------------  |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
 495  496   * |idl_tx_list[1]|->|  ---------------   -------   -------   -------
 496  497   * ----------------  |        .              .         .         .
 497  498   *                   |  ---------------   -------   -------   -------
 498  499   *                   |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
 499  500   *                      ---------------   -------   -------   -------
 500  501   *     .....
 501  502   * ----------------
 502  503   * |idl_tx_list[n]|-> ...
 503  504   * ----------------
 504  505   *
 505  506   * When mac_tx() returns a cookie, the cookie is hashed into an index into
 506  507   * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
 507  508   * to insert the conn onto.  conn_drain_insert() asserts flow control for the
 508  509   * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
 509  510   * Further, conn_blocked is set to indicate that the conn is blocked.
 510  511   *
 511  512   * GLDv3 calls ill_flow_enable() when flow control is relieved.  The cookie
 512  513   * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
 513  514   * is again hashed to locate the appropriate idl_tx_list, which is then
 514  515   * drained via conn_walk_drain().  conn_walk_drain() goes through each conn in
 515  516   * the drain list and calls conn_drain_remove() to clear flow control (via
 516  517   * calling su_txq_full() or clearing QFULL), and remove the conn from the
 517  518   * drain list.
 518  519   *
 519  520   * Note that the drain list is not a single list but a (configurable) array of
 520  521   * lists (8 elements by default).  Synchronization between drain insertion and
 521  522   * flow control wakeup is handled by using idl_txl->txl_lock, and only
 522  523   * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
 523  524   *
 524  525   * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
 525  526   * On the send side, if the packet cannot be sent down to the driver by IP
 526  527   * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
 527  528   * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
 528  529   * the 0'th drain list.  When ip_wsrv() runs on the ill_wq because flow
 529  530   * control has been relieved, the blocked conns in the 0'th drain list are
 530  531   * drained as in the non-STREAMS case.
 531  532   *
 532  533   * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
 533  534   * is done when the conn is inserted into the drain list (conn_drain_insert())
 534  535   * and cleared when the conn is removed from the it (conn_drain_remove()).
 535  536   *
 536  537   * IPQOS notes:
 537  538   *
 538  539   * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
 539  540   * and IPQoS modules. IPPF includes hooks in IP at different control points
 540  541   * (callout positions) which direct packets to IPQoS modules for policy
 541  542   * processing. Policies, if present, are global.
 542  543   *
 543  544   * The callout positions are located in the following paths:
 544  545   *              o local_in (packets destined for this host)
 545  546   *              o local_out (packets orginating from this host )
 546  547   *              o fwd_in  (packets forwarded by this m/c - inbound)
 547  548   *              o fwd_out (packets forwarded by this m/c - outbound)
 548  549   * Hooks at these callout points can be enabled/disabled using the ndd variable
 549  550   * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
 550  551   * By default all the callout positions are enabled.
 551  552   *
 552  553   * Outbound (local_out)
 553  554   * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
 554  555   *
 555  556   * Inbound (local_in)
 556  557   * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
 557  558   *
 558  559   * Forwarding (in and out)
 559  560   * Hooks are placed in ire_recv_forward_v4/v6.
 560  561   *
 561  562   * IP Policy Framework processing (IPPF processing)
 562  563   * Policy processing for a packet is initiated by ip_process, which ascertains
 563  564   * that the classifier (ipgpc) is loaded and configured, failing which the
 564  565   * packet resumes normal processing in IP. If the clasifier is present, the
 565  566   * packet is acted upon by one or more IPQoS modules (action instances), per
 566  567   * filters configured in ipgpc and resumes normal IP processing thereafter.
 567  568   * An action instance can drop a packet in course of its processing.
 568  569   *
 569  570   * Zones notes:
 570  571   *
 571  572   * The partitioning rules for networking are as follows:
 572  573   * 1) Packets coming from a zone must have a source address belonging to that
 573  574   * zone.
 574  575   * 2) Packets coming from a zone can only be sent on a physical interface on
 575  576   * which the zone has an IP address.
 576  577   * 3) Between two zones on the same machine, packet delivery is only allowed if
 577  578   * there's a matching route for the destination and zone in the forwarding
 578  579   * table.
 579  580   * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
 580  581   * different zones can bind to the same port with the wildcard address
 581  582   * (INADDR_ANY).
 582  583   *
 583  584   * The granularity of interface partitioning is at the logical interface level.
 584  585   * Therefore, every zone has its own IP addresses, and incoming packets can be
 585  586   * attributed to a zone unambiguously. A logical interface is placed into a zone
 586  587   * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
 587  588   * structure. Rule (1) is implemented by modifying the source address selection
 588  589   * algorithm so that the list of eligible addresses is filtered based on the
 589  590   * sending process zone.
 590  591   *
 591  592   * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
 592  593   * across all zones, depending on their type. Here is the break-up:
 593  594   *
 594  595   * IRE type                             Shared/exclusive
 595  596   * --------                             ----------------
 596  597   * IRE_BROADCAST                        Exclusive
 597  598   * IRE_DEFAULT (default routes)         Shared (*)
 598  599   * IRE_LOCAL                            Exclusive (x)
 599  600   * IRE_LOOPBACK                         Exclusive
 600  601   * IRE_PREFIX (net routes)              Shared (*)
 601  602   * IRE_IF_NORESOLVER (interface routes) Exclusive
 602  603   * IRE_IF_RESOLVER (interface routes)   Exclusive
 603  604   * IRE_IF_CLONE (interface routes)      Exclusive
 604  605   * IRE_HOST (host routes)               Shared (*)
 605  606   *
 606  607   * (*) A zone can only use a default or off-subnet route if the gateway is
 607  608   * directly reachable from the zone, that is, if the gateway's address matches
 608  609   * one of the zone's logical interfaces.
 609  610   *
 610  611   * (x) IRE_LOCAL are handled a bit differently.
 611  612   * When ip_restrict_interzone_loopback is set (the default),
 612  613   * ire_route_recursive restricts loopback using an IRE_LOCAL
 613  614   * between zone to the case when L2 would have conceptually looped the packet
 614  615   * back, i.e. the loopback which is required since neither Ethernet drivers
 615  616   * nor Ethernet hardware loops them back. This is the case when the normal
 616  617   * routes (ignoring IREs with different zoneids) would send out the packet on
 617  618   * the same ill as the ill with which is IRE_LOCAL is associated.
 618  619   *
 619  620   * Multiple zones can share a common broadcast address; typically all zones
 620  621   * share the 255.255.255.255 address. Incoming as well as locally originated
 621  622   * broadcast packets must be dispatched to all the zones on the broadcast
 622  623   * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
 623  624   * since some zones may not be on the 10.16.72/24 network. To handle this, each
 624  625   * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
 625  626   * sent to every zone that has an IRE_BROADCAST entry for the destination
 626  627   * address on the input ill, see ip_input_broadcast().
 627  628   *
 628  629   * Applications in different zones can join the same multicast group address.
 629  630   * The same logic applies for multicast as for broadcast. ip_input_multicast
 630  631   * dispatches packets to all zones that have members on the physical interface.
 631  632   */
 632  633  
 633  634  /*
 634  635   * Squeue Fanout flags:
 635  636   *      0: No fanout.
 636  637   *      1: Fanout across all squeues
 637  638   */
 638  639  boolean_t       ip_squeue_fanout = 0;
 639  640  
 640  641  /*
 641  642   * Maximum dups allowed per packet.
 642  643   */
 643  644  uint_t ip_max_frag_dups = 10;
 644  645  
 645  646  static int      ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
 646  647                      cred_t *credp, boolean_t isv6);
 647  648  static mblk_t   *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
 648  649  
 649  650  static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
 650  651  static void     icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
 651  652  static void     icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
 652  653      ip_recv_attr_t *);
 653  654  static void     icmp_options_update(ipha_t *);
 654  655  static void     icmp_param_problem(mblk_t *, uint8_t,  ip_recv_attr_t *);
 655  656  static void     icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
 656  657  static mblk_t   *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
 657  658  static void     icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
 658  659      ip_recv_attr_t *);
 659  660  static void     icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
 660  661  static void     icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
 661  662      ip_recv_attr_t *);
 662  663  
 663  664  mblk_t          *ip_dlpi_alloc(size_t, t_uscalar_t);
 664  665  char            *ip_dot_addr(ipaddr_t, char *);
 665  666  mblk_t          *ip_carve_mp(mblk_t **, ssize_t);
 666  667  int             ip_close(queue_t *, int);
 667  668  static char     *ip_dot_saddr(uchar_t *, char *);
 668  669  static void     ip_lrput(queue_t *, mblk_t *);
 669  670  ipaddr_t        ip_net_mask(ipaddr_t);
 670  671  char            *ip_nv_lookup(nv_t *, int);
 671  672  void    ip_rput(queue_t *, mblk_t *);
 672  673  static void     ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
 673  674                      void *dummy_arg);
 674  675  int             ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
 675  676  static mblk_t   *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
 676  677                      mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
 677  678  static mblk_t   *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
 678  679                      ip_stack_t *, boolean_t);
 679  680  static mblk_t   *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
 680  681                      boolean_t);
 681  682  static mblk_t   *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
 682  683  static mblk_t   *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
 683  684  static mblk_t   *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
 684  685  static mblk_t   *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
 685  686  static mblk_t   *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
 686  687                      ip_stack_t *ipst, boolean_t);
 687  688  static mblk_t   *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
 688  689                      ip_stack_t *ipst, boolean_t);
 689  690  static mblk_t   *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
 690  691                      ip_stack_t *ipst);
 691  692  static mblk_t   *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
 692  693                      ip_stack_t *ipst);
 693  694  static mblk_t   *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
 694  695                      ip_stack_t *ipst);
 695  696  static mblk_t   *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
 696  697                      ip_stack_t *ipst);
 697  698  static mblk_t   *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
 698  699                      ip_stack_t *ipst);
 699  700  static mblk_t   *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
 700  701                      ip_stack_t *ipst);
 701  702  static mblk_t   *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
 702  703                      ip_stack_t *ipst);
 703  704  static mblk_t   *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
 704  705                      ip_stack_t *ipst);
 705  706  static void     ip_snmp_get2_v4(ire_t *, iproutedata_t *);
 706  707  static void     ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
 707  708  static int      ip_snmp_get2_v4_media(ncec_t *, iproutedata_t *);
 708  709  static int      ip_snmp_get2_v6_media(ncec_t *, iproutedata_t *);
 709  710  int             ip_snmp_set(queue_t *, int, int, uchar_t *, int);
 710  711  
 711  712  static mblk_t   *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
 712  713                      mblk_t *);
 713  714  
 714  715  static void     conn_drain_init(ip_stack_t *);
 715  716  static void     conn_drain_fini(ip_stack_t *);
 716  717  static void     conn_drain(conn_t *connp, boolean_t closing);
 717  718  
 718  719  static void     conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
 719  720  static void     conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
 720  721  
 721  722  static void     *ip_stack_init(netstackid_t stackid, netstack_t *ns);
 722  723  static void     ip_stack_shutdown(netstackid_t stackid, void *arg);
 723  724  static void     ip_stack_fini(netstackid_t stackid, void *arg);
 724  725  
 725  726  static int      ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
 726  727      const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
 727  728      ire_t *, conn_t *, boolean_t, const in6_addr_t *,  mcast_record_t,
 728  729      const in6_addr_t *);
 729  730  
 730  731  static int      ip_squeue_switch(int);
 731  732  
 732  733  static void     *ip_kstat_init(netstackid_t, ip_stack_t *);
 733  734  static void     ip_kstat_fini(netstackid_t, kstat_t *);
 734  735  static int      ip_kstat_update(kstat_t *kp, int rw);
 735  736  static void     *icmp_kstat_init(netstackid_t);
 736  737  static void     icmp_kstat_fini(netstackid_t, kstat_t *);
 737  738  static int      icmp_kstat_update(kstat_t *kp, int rw);
 738  739  static void     *ip_kstat2_init(netstackid_t, ip_stat_t *);
 739  740  static void     ip_kstat2_fini(netstackid_t, kstat_t *);
 740  741  
 741  742  static void     ipobs_init(ip_stack_t *);
 742  743  static void     ipobs_fini(ip_stack_t *);
 743  744  
 744  745  static int      ip_tp_cpu_update(cpu_setup_t, int, void *);
 745  746  
 746  747  ipaddr_t        ip_g_all_ones = IP_HOST_MASK;
 747  748  
 748  749  static long ip_rput_pullups;
 749  750  int     dohwcksum = 1;  /* use h/w cksum if supported by the hardware */
 750  751  
 751  752  vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
 752  753  vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
 753  754  
 754  755  int     ip_debug;
 755  756  
 756  757  /*
 757  758   * Multirouting/CGTP stuff
 758  759   */
 759  760  int     ip_cgtp_filter_rev = CGTP_FILTER_REV;   /* CGTP hooks version */
 760  761  
 761  762  /*
 762  763   * IP tunables related declarations. Definitions are in ip_tunables.c
 763  764   */
 764  765  extern mod_prop_info_t ip_propinfo_tbl[];
 765  766  extern int ip_propinfo_count;
 766  767  
 767  768  /*
 768  769   * Table of IP ioctls encoding the various properties of the ioctl and
 769  770   * indexed based on the last byte of the ioctl command. Occasionally there
 770  771   * is a clash, and there is more than 1 ioctl with the same last byte.
 771  772   * In such a case 1 ioctl is encoded in the ndx table and the remaining
 772  773   * ioctls are encoded in the misc table. An entry in the ndx table is
 773  774   * retrieved by indexing on the last byte of the ioctl command and comparing
 774  775   * the ioctl command with the value in the ndx table. In the event of a
 775  776   * mismatch the misc table is then searched sequentially for the desired
 776  777   * ioctl command.
 777  778   *
 778  779   * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
 779  780   */
 780  781  ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
 781  782          /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 782  783          /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 783  784          /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 784  785          /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 785  786          /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 786  787          /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 787  788          /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 788  789          /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 789  790          /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 790  791          /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 791  792  
 792  793          /* 010 */ { SIOCADDRT,  sizeof (struct rtentry), IPI_PRIV,
 793  794                          MISC_CMD, ip_siocaddrt, NULL },
 794  795          /* 011 */ { SIOCDELRT,  sizeof (struct rtentry), IPI_PRIV,
 795  796                          MISC_CMD, ip_siocdelrt, NULL },
 796  797  
 797  798          /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 798  799                          IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
 799  800          /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
 800  801                          IF_CMD, ip_sioctl_get_addr, NULL },
 801  802  
 802  803          /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 803  804                          IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
 804  805          /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
 805  806                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
 806  807  
 807  808          /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
 808  809                          IPI_PRIV | IPI_WR,
 809  810                          IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
 810  811          /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
 811  812                          IPI_MODOK | IPI_GET_CMD,
 812  813                          IF_CMD, ip_sioctl_get_flags, NULL },
 813  814  
 814  815          /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 815  816          /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 816  817  
 817  818          /* copyin size cannot be coded for SIOCGIFCONF */
 818  819          /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
 819  820                          MISC_CMD, ip_sioctl_get_ifconf, NULL },
 820  821  
 821  822          /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 822  823                          IF_CMD, ip_sioctl_mtu, NULL },
 823  824          /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
 824  825                          IF_CMD, ip_sioctl_get_mtu, NULL },
 825  826          /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
 826  827                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
 827  828          /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 828  829                          IF_CMD, ip_sioctl_brdaddr, NULL },
 829  830          /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
 830  831                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
 831  832          /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 832  833                          IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
 833  834          /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
 834  835                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
 835  836          /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
 836  837                          IF_CMD, ip_sioctl_metric, NULL },
 837  838          /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 838  839  
 839  840          /* See 166-168 below for extended SIOC*XARP ioctls */
 840  841          /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
 841  842                          ARP_CMD, ip_sioctl_arp, NULL },
 842  843          /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
 843  844                          ARP_CMD, ip_sioctl_arp, NULL },
 844  845          /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
 845  846                          ARP_CMD, ip_sioctl_arp, NULL },
 846  847  
 847  848          /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 848  849          /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 849  850          /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 850  851          /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 851  852          /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 852  853          /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 853  854          /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 854  855          /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 855  856          /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 856  857          /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 857  858          /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 858  859          /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 859  860          /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 860  861          /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 861  862          /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 862  863          /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 863  864          /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 864  865          /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 865  866          /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 866  867          /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 867  868          /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 868  869  
 869  870          /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
 870  871                          MISC_CMD, if_unitsel, if_unitsel_restart },
 871  872  
 872  873          /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 873  874          /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 874  875          /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 875  876          /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 876  877          /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 877  878          /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 878  879          /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 879  880          /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 880  881          /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 881  882          /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 882  883          /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 883  884          /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 884  885          /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 885  886          /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 886  887          /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 887  888          /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 888  889          /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 889  890          /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 890  891  
 891  892          /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
 892  893                          IPI_PRIV | IPI_WR | IPI_MODOK,
 893  894                          IF_CMD, ip_sioctl_sifname, NULL },
 894  895  
 895  896          /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 896  897          /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 897  898          /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 898  899          /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 899  900          /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 900  901          /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 901  902          /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 902  903          /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 903  904          /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 904  905          /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 905  906          /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 906  907          /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 907  908          /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 908  909  
 909  910          /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
 910  911                          MISC_CMD, ip_sioctl_get_ifnum, NULL },
 911  912          /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
 912  913                          IF_CMD, ip_sioctl_get_muxid, NULL },
 913  914          /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
 914  915                          IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
 915  916  
 916  917          /* Both if and lif variants share same func */
 917  918          /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
 918  919                          IF_CMD, ip_sioctl_get_lifindex, NULL },
 919  920          /* Both if and lif variants share same func */
 920  921          /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
 921  922                          IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
 922  923  
 923  924          /* copyin size cannot be coded for SIOCGIFCONF */
 924  925          /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
 925  926                          MISC_CMD, ip_sioctl_get_ifconf, NULL },
 926  927          /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 927  928          /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 928  929          /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 929  930          /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 930  931          /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 931  932          /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 932  933          /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 933  934          /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 934  935          /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 935  936          /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 936  937          /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 937  938          /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 938  939          /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 939  940          /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 940  941          /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 941  942          /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 942  943          /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 943  944  
 944  945          /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
 945  946                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
 946  947                          ip_sioctl_removeif_restart },
 947  948          /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
 948  949                          IPI_GET_CMD | IPI_PRIV | IPI_WR,
 949  950                          LIF_CMD, ip_sioctl_addif, NULL },
 950  951  #define SIOCLIFADDR_NDX 112
 951  952          /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 952  953                          LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
 953  954          /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
 954  955                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
 955  956          /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 956  957                          LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
 957  958          /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
 958  959                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
 959  960          /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
 960  961                          IPI_PRIV | IPI_WR,
 961  962                          LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
 962  963          /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
 963  964                          IPI_GET_CMD | IPI_MODOK,
 964  965                          LIF_CMD, ip_sioctl_get_flags, NULL },
 965  966  
 966  967          /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 967  968          /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 968  969  
 969  970          /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
 970  971                          ip_sioctl_get_lifconf, NULL },
 971  972          /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 972  973                          LIF_CMD, ip_sioctl_mtu, NULL },
 973  974          /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
 974  975                          LIF_CMD, ip_sioctl_get_mtu, NULL },
 975  976          /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
 976  977                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
 977  978          /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 978  979                          LIF_CMD, ip_sioctl_brdaddr, NULL },
 979  980          /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
 980  981                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
 981  982          /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 982  983                          LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
 983  984          /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
 984  985                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
 985  986          /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 986  987                          LIF_CMD, ip_sioctl_metric, NULL },
 987  988          /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
 988  989                          IPI_PRIV | IPI_WR | IPI_MODOK,
 989  990                          LIF_CMD, ip_sioctl_slifname,
 990  991                          ip_sioctl_slifname_restart },
 991  992  
 992  993          /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
 993  994                          MISC_CMD, ip_sioctl_get_lifnum, NULL },
 994  995          /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
 995  996                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
 996  997          /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
 997  998                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
 998  999          /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
 999 1000                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
1000 1001          /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1001 1002                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
1002 1003          /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1003 1004                          LIF_CMD, ip_sioctl_token, NULL },
1004 1005          /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1005 1006                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
1006 1007          /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1007 1008                          LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1008 1009          /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1009 1010                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
1010 1011          /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1011 1012                          LIF_CMD, ip_sioctl_lnkinfo, NULL },
1012 1013  
1013 1014          /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1014 1015                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1015 1016          /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1016 1017                          LIF_CMD, ip_siocdelndp_v6, NULL },
1017 1018          /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1018 1019                          LIF_CMD, ip_siocqueryndp_v6, NULL },
1019 1020          /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1020 1021                          LIF_CMD, ip_siocsetndp_v6, NULL },
1021 1022          /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1022 1023                          MISC_CMD, ip_sioctl_tmyaddr, NULL },
1023 1024          /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1024 1025                          MISC_CMD, ip_sioctl_tonlink, NULL },
1025 1026          /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1026 1027                          MISC_CMD, ip_sioctl_tmysite, NULL },
1027 1028          /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1028 1029          /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1029 1030          /* IPSECioctls handled in ip_sioctl_copyin_setup itself */
1030 1031          /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1031 1032          /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1032 1033          /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1033 1034          /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1034 1035  
1035 1036          /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1036 1037  
1037 1038          /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
1038 1039                          LIF_CMD, ip_sioctl_get_binding, NULL },
1039 1040          /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1040 1041                          IPI_PRIV | IPI_WR,
1041 1042                          LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1042 1043          /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1043 1044                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
1044 1045          /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
1045 1046                          IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
1046 1047  
1047 1048          /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1048 1049          /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1049 1050          /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1050 1051          /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1051 1052  
1052 1053          /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1053 1054  
1054 1055          /* These are handled in ip_sioctl_copyin_setup itself */
1055 1056          /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1056 1057                          MISC_CMD, NULL, NULL },
1057 1058          /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1058 1059                          MISC_CMD, NULL, NULL },
1059 1060          /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1060 1061  
1061 1062          /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1062 1063                          ip_sioctl_get_lifconf, NULL },
1063 1064  
1064 1065          /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1065 1066                          XARP_CMD, ip_sioctl_arp, NULL },
1066 1067          /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1067 1068                          XARP_CMD, ip_sioctl_arp, NULL },
1068 1069          /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1069 1070                          XARP_CMD, ip_sioctl_arp, NULL },
1070 1071  
1071 1072          /* SIOCPOPSOCKFS is not handled by IP */
1072 1073          /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1073 1074  
1074 1075          /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1075 1076                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1076 1077          /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1077 1078                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1078 1079                          ip_sioctl_slifzone_restart },
1079 1080          /* 172-174 are SCTP ioctls and not handled by IP */
1080 1081          /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1081 1082          /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1082 1083          /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1083 1084          /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1084 1085                          IPI_GET_CMD, LIF_CMD,
1085 1086                          ip_sioctl_get_lifusesrc, 0 },
1086 1087          /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1087 1088                          IPI_PRIV | IPI_WR,
1088 1089                          LIF_CMD, ip_sioctl_slifusesrc,
1089 1090                          NULL },
1090 1091          /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1091 1092                          ip_sioctl_get_lifsrcof, NULL },
1092 1093          /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1093 1094                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1094 1095          /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1095 1096                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1096 1097          /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1097 1098                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1098 1099          /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1099 1100                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1100 1101          /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1101 1102          /* SIOCSENABLESDP is handled by SDP */
1102 1103          /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1103 1104          /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1104 1105          /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1105 1106                          IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1106 1107          /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1107 1108          /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1108 1109                          ip_sioctl_ilb_cmd, NULL },
1109 1110          /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL },
1110 1111          /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL},
1111 1112          /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1112 1113                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1113 1114          /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1114 1115                          LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1115 1116          /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1116 1117                          LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1117 1118  };
1118 1119  
1119 1120  int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1120 1121  
1121 1122  ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1122 1123          { I_LINK,       0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1123 1124          { I_UNLINK,     0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1124 1125          { I_PLINK,      0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1125 1126          { I_PUNLINK,    0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1126 1127          { ND_GET,       0, 0, 0, NULL, NULL },
1127 1128          { ND_SET,       0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1128 1129          { IP_IOCTL,     0, 0, 0, NULL, NULL },
1129 1130          { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1130 1131                  MISC_CMD, mrt_ioctl},
1131 1132          { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1132 1133                  MISC_CMD, mrt_ioctl},
1133 1134          { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1134 1135                  MISC_CMD, mrt_ioctl}
1135 1136  };
1136 1137  
1137 1138  int ip_misc_ioctl_count =
1138 1139      sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1139 1140  
1140 1141  int     conn_drain_nthreads;            /* Number of drainers reqd. */
1141 1142                                          /* Settable in /etc/system */
1142 1143  /* Defined in ip_ire.c */
1143 1144  extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1144 1145  extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1145 1146  extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1146 1147  
1147 1148  static nv_t     ire_nv_arr[] = {
1148 1149          { IRE_BROADCAST, "BROADCAST" },
1149 1150          { IRE_LOCAL, "LOCAL" },
1150 1151          { IRE_LOOPBACK, "LOOPBACK" },
1151 1152          { IRE_DEFAULT, "DEFAULT" },
1152 1153          { IRE_PREFIX, "PREFIX" },
1153 1154          { IRE_IF_NORESOLVER, "IF_NORESOL" },
1154 1155          { IRE_IF_RESOLVER, "IF_RESOLV" },
1155 1156          { IRE_IF_CLONE, "IF_CLONE" },
1156 1157          { IRE_HOST, "HOST" },
1157 1158          { IRE_MULTICAST, "MULTICAST" },
1158 1159          { IRE_NOROUTE, "NOROUTE" },
1159 1160          { 0 }
1160 1161  };
1161 1162  
1162 1163  nv_t    *ire_nv_tbl = ire_nv_arr;
1163 1164  
1164 1165  /* Simple ICMP IP Header Template */
1165 1166  static ipha_t icmp_ipha = {
1166 1167          IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1167 1168  };
1168 1169  
1169 1170  struct module_info ip_mod_info = {
1170 1171          IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1171 1172          IP_MOD_LOWAT
1172 1173  };
1173 1174  
1174 1175  /*
1175 1176   * Duplicate static symbols within a module confuses mdb; so we avoid the
1176 1177   * problem by making the symbols here distinct from those in udp.c.
1177 1178   */
1178 1179  
1179 1180  /*
1180 1181   * Entry points for IP as a device and as a module.
1181 1182   * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1182 1183   */
1183 1184  static struct qinit iprinitv4 = {
1184 1185          (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL,
1185 1186          &ip_mod_info
1186 1187  };
1187 1188  
1188 1189  struct qinit iprinitv6 = {
1189 1190          (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL,
1190 1191          &ip_mod_info
1191 1192  };
1192 1193  
1193 1194  static struct qinit ipwinit = {
1194 1195          (pfi_t)ip_wput_nondata, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1195 1196          &ip_mod_info
1196 1197  };
1197 1198  
1198 1199  static struct qinit iplrinit = {
1199 1200          (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL,
1200 1201          &ip_mod_info
1201 1202  };
1202 1203  
1203 1204  static struct qinit iplwinit = {
1204 1205          (pfi_t)ip_lwput, NULL, NULL, NULL, NULL,
1205 1206          &ip_mod_info
1206 1207  };
1207 1208  
1208 1209  /* For AF_INET aka /dev/ip */
1209 1210  struct streamtab ipinfov4 = {
1210 1211          &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1211 1212  };
1212 1213  
1213 1214  /* For AF_INET6 aka /dev/ip6 */
1214 1215  struct streamtab ipinfov6 = {
1215 1216          &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1216 1217  };
1217 1218  
1218 1219  #ifdef  DEBUG
1219 1220  boolean_t skip_sctp_cksum = B_FALSE;
1220 1221  #endif
1221 1222  
1222 1223  /*
1223 1224   * Generate an ICMP fragmentation needed message.
1224 1225   * When called from ip_output side a minimal ip_recv_attr_t needs to be
1225 1226   * constructed by the caller.
1226 1227   */
1227 1228  void
1228 1229  icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1229 1230  {
1230 1231          icmph_t icmph;
1231 1232          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
1232 1233  
1233 1234          mp = icmp_pkt_err_ok(mp, ira);
1234 1235          if (mp == NULL)
1235 1236                  return;
1236 1237  
1237 1238          bzero(&icmph, sizeof (icmph_t));
1238 1239          icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1239 1240          icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1240 1241          icmph.icmph_du_mtu = htons((uint16_t)mtu);
1241 1242          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1242 1243          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1243 1244  
1244 1245          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1245 1246  }
1246 1247  
1247 1248  /*
1248 1249   * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1249 1250   * If the ICMP message is consumed by IP, i.e., it should not be delivered
1250 1251   * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1251 1252   * Likewise, if the ICMP error is misformed (too short, etc), then it
1252 1253   * returns NULL. The caller uses this to determine whether or not to send
1253 1254   * to raw sockets.
1254 1255   *
1255 1256   * All error messages are passed to the matching transport stream.
1256 1257   *
1257 1258   * The following cases are handled by icmp_inbound:
1258 1259   * 1) It needs to send a reply back and possibly delivering it
1259 1260   *    to the "interested" upper clients.
1260 1261   * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1261 1262   * 3) It needs to change some values in IP only.
1262 1263   * 4) It needs to change some values in IP and upper layers e.g TCP
1263 1264   *    by delivering an error to the upper layers.
1264 1265   *
1265 1266   * We handle the above three cases in the context of IPsec in the
1266 1267   * following way :
1267 1268   *
1268 1269   * 1) Send the reply back in the same way as the request came in.
1269 1270   *    If it came in encrypted, it goes out encrypted. If it came in
1270 1271   *    clear, it goes out in clear. Thus, this will prevent chosen
1271 1272   *    plain text attack.
1272 1273   * 2) The client may or may not expect things to come in secure.
1273 1274   *    If it comes in secure, the policy constraints are checked
1274 1275   *    before delivering it to the upper layers. If it comes in
1275 1276   *    clear, ipsec_inbound_accept_clear will decide whether to
1276 1277   *    accept this in clear or not. In both the cases, if the returned
1277 1278   *    message (IP header + 8 bytes) that caused the icmp message has
1278 1279   *    AH/ESP headers, it is sent up to AH/ESP for validation before
1279 1280   *    sending up. If there are only 8 bytes of returned message, then
1280 1281   *    upper client will not be notified.
1281 1282   * 3) Check with global policy to see whether it matches the constaints.
1282 1283   *    But this will be done only if icmp_accept_messages_in_clear is
1283 1284   *    zero.
1284 1285   * 4) If we need to change both in IP and ULP, then the decision taken
1285 1286   *    while affecting the values in IP and while delivering up to TCP
1286 1287   *    should be the same.
1287 1288   *
1288 1289   *      There are two cases.
1289 1290   *
1290 1291   *      a) If we reject data at the IP layer (ipsec_check_global_policy()
1291 1292   *         failed), we will not deliver it to the ULP, even though they
1292 1293   *         are *willing* to accept in *clear*. This is fine as our global
1293 1294   *         disposition to icmp messages asks us reject the datagram.
1294 1295   *
1295 1296   *      b) If we accept data at the IP layer (ipsec_check_global_policy()
1296 1297   *         succeeded or icmp_accept_messages_in_clear is 1), and not able
1297 1298   *         to deliver it to ULP (policy failed), it can lead to
1298 1299   *         consistency problems. The cases known at this time are
1299 1300   *         ICMP_DESTINATION_UNREACHABLE  messages with following code
1300 1301   *         values :
1301 1302   *
1302 1303   *         - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1303 1304   *           and Upper layer rejects. Then the communication will
1304 1305   *           come to a stop. This is solved by making similar decisions
1305 1306   *           at both levels. Currently, when we are unable to deliver
1306 1307   *           to the Upper Layer (due to policy failures) while IP has
1307 1308   *           adjusted dce_pmtu, the next outbound datagram would
1308 1309   *           generate a local ICMP_FRAGMENTATION_NEEDED message - which
1309 1310   *           will be with the right level of protection. Thus the right
1310 1311   *           value will be communicated even if we are not able to
1311 1312   *           communicate when we get from the wire initially. But this
1312 1313   *           assumes there would be at least one outbound datagram after
1313 1314   *           IP has adjusted its dce_pmtu value. To make things
1314 1315   *           simpler, we accept in clear after the validation of
1315 1316   *           AH/ESP headers.
1316 1317   *
1317 1318   *         - Other ICMP ERRORS : We may not be able to deliver it to the
1318 1319   *           upper layer depending on the level of protection the upper
1319 1320   *           layer expects and the disposition in ipsec_inbound_accept_clear().
1320 1321   *           ipsec_inbound_accept_clear() decides whether a given ICMP error
1321 1322   *           should be accepted in clear when the Upper layer expects secure.
1322 1323   *           Thus the communication may get aborted by some bad ICMP
1323 1324   *           packets.
1324 1325   */
1325 1326  mblk_t *
1326 1327  icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1327 1328  {
1328 1329          icmph_t         *icmph;
1329 1330          ipha_t          *ipha;          /* Outer header */
1330 1331          int             ip_hdr_length;  /* Outer header length */
1331 1332          boolean_t       interested;
1332 1333          ipif_t          *ipif;
1333 1334          uint32_t        ts;
1334 1335          uint32_t        *tsp;
1335 1336          timestruc_t     now;
1336 1337          ill_t           *ill = ira->ira_ill;
1337 1338          ip_stack_t      *ipst = ill->ill_ipst;
1338 1339          zoneid_t        zoneid = ira->ira_zoneid;
1339 1340          int             len_needed;
1340 1341          mblk_t          *mp_ret = NULL;
1341 1342  
1342 1343          ipha = (ipha_t *)mp->b_rptr;
1343 1344  
1344 1345          BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1345 1346  
1346 1347          ip_hdr_length = ira->ira_ip_hdr_length;
1347 1348          if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1348 1349                  if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1349 1350                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1350 1351                          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1351 1352                          freemsg(mp);
1352 1353                          return (NULL);
1353 1354                  }
1354 1355                  /* Last chance to get real. */
1355 1356                  ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1356 1357                  if (ipha == NULL) {
1357 1358                          BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1358 1359                          freemsg(mp);
1359 1360                          return (NULL);
1360 1361                  }
1361 1362          }
1362 1363  
1363 1364          /* The IP header will always be a multiple of four bytes */
1364 1365          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1365 1366          ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1366 1367              icmph->icmph_code));
1367 1368  
1368 1369          /*
1369 1370           * We will set "interested" to "true" if we should pass a copy to
1370 1371           * the transport or if we handle the packet locally.
1371 1372           */
1372 1373          interested = B_FALSE;
1373 1374          switch (icmph->icmph_type) {
1374 1375          case ICMP_ECHO_REPLY:
1375 1376                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1376 1377                  break;
1377 1378          case ICMP_DEST_UNREACHABLE:
1378 1379                  if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1379 1380                          BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1380 1381                  interested = B_TRUE;    /* Pass up to transport */
1381 1382                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1382 1383                  break;
1383 1384          case ICMP_SOURCE_QUENCH:
1384 1385                  interested = B_TRUE;    /* Pass up to transport */
1385 1386                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1386 1387                  break;
1387 1388          case ICMP_REDIRECT:
1388 1389                  if (!ipst->ips_ip_ignore_redirect)
1389 1390                          interested = B_TRUE;
1390 1391                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1391 1392                  break;
1392 1393          case ICMP_ECHO_REQUEST:
1393 1394                  /*
1394 1395                   * Whether to respond to echo requests that come in as IP
1395 1396                   * broadcasts or as IP multicast is subject to debate
1396 1397                   * (what isn't?).  We aim to please, you pick it.
1397 1398                   * Default is do it.
1398 1399                   */
1399 1400                  if (ira->ira_flags & IRAF_MULTICAST) {
1400 1401                          /* multicast: respond based on tunable */
1401 1402                          interested = ipst->ips_ip_g_resp_to_echo_mcast;
1402 1403                  } else if (ira->ira_flags & IRAF_BROADCAST) {
1403 1404                          /* broadcast: respond based on tunable */
1404 1405                          interested = ipst->ips_ip_g_resp_to_echo_bcast;
1405 1406                  } else {
1406 1407                          /* unicast: always respond */
1407 1408                          interested = B_TRUE;
1408 1409                  }
1409 1410                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1410 1411                  if (!interested) {
1411 1412                          /* We never pass these to RAW sockets */
1412 1413                          freemsg(mp);
1413 1414                          return (NULL);
1414 1415                  }
1415 1416  
1416 1417                  /* Check db_ref to make sure we can modify the packet. */
1417 1418                  if (mp->b_datap->db_ref > 1) {
1418 1419                          mblk_t  *mp1;
1419 1420  
1420 1421                          mp1 = copymsg(mp);
1421 1422                          freemsg(mp);
1422 1423                          if (!mp1) {
1423 1424                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1424 1425                                  return (NULL);
1425 1426                          }
1426 1427                          mp = mp1;
1427 1428                          ipha = (ipha_t *)mp->b_rptr;
1428 1429                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1429 1430                  }
1430 1431                  icmph->icmph_type = ICMP_ECHO_REPLY;
1431 1432                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1432 1433                  icmp_send_reply_v4(mp, ipha, icmph, ira);
1433 1434                  return (NULL);
1434 1435  
1435 1436          case ICMP_ROUTER_ADVERTISEMENT:
1436 1437          case ICMP_ROUTER_SOLICITATION:
1437 1438                  break;
1438 1439          case ICMP_TIME_EXCEEDED:
1439 1440                  interested = B_TRUE;    /* Pass up to transport */
1440 1441                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1441 1442                  break;
1442 1443          case ICMP_PARAM_PROBLEM:
1443 1444                  interested = B_TRUE;    /* Pass up to transport */
1444 1445                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1445 1446                  break;
1446 1447          case ICMP_TIME_STAMP_REQUEST:
1447 1448                  /* Response to Time Stamp Requests is local policy. */
1448 1449                  if (ipst->ips_ip_g_resp_to_timestamp) {
1449 1450                          if (ira->ira_flags & IRAF_MULTIBROADCAST)
1450 1451                                  interested =
1451 1452                                      ipst->ips_ip_g_resp_to_timestamp_bcast;
1452 1453                          else
1453 1454                                  interested = B_TRUE;
1454 1455                  }
1455 1456                  if (!interested) {
1456 1457                          /* We never pass these to RAW sockets */
1457 1458                          freemsg(mp);
1458 1459                          return (NULL);
1459 1460                  }
1460 1461  
1461 1462                  /* Make sure we have enough of the packet */
1462 1463                  len_needed = ip_hdr_length + ICMPH_SIZE +
1463 1464                      3 * sizeof (uint32_t);
1464 1465  
1465 1466                  if (mp->b_wptr - mp->b_rptr < len_needed) {
1466 1467                          ipha = ip_pullup(mp, len_needed, ira);
1467 1468                          if (ipha == NULL) {
1468 1469                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1469 1470                                  ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1470 1471                                      mp, ill);
1471 1472                                  freemsg(mp);
1472 1473                                  return (NULL);
1473 1474                          }
1474 1475                          /* Refresh following the pullup. */
1475 1476                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1476 1477                  }
1477 1478                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1478 1479                  /* Check db_ref to make sure we can modify the packet. */
1479 1480                  if (mp->b_datap->db_ref > 1) {
1480 1481                          mblk_t  *mp1;
1481 1482  
1482 1483                          mp1 = copymsg(mp);
1483 1484                          freemsg(mp);
1484 1485                          if (!mp1) {
1485 1486                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1486 1487                                  return (NULL);
1487 1488                          }
1488 1489                          mp = mp1;
1489 1490                          ipha = (ipha_t *)mp->b_rptr;
1490 1491                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1491 1492                  }
1492 1493                  icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1493 1494                  tsp = (uint32_t *)&icmph[1];
1494 1495                  tsp++;          /* Skip past 'originate time' */
1495 1496                  /* Compute # of milliseconds since midnight */
1496 1497                  gethrestime(&now);
1497 1498                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1498 1499                      now.tv_nsec / (NANOSEC / MILLISEC);
1499 1500                  *tsp++ = htonl(ts);     /* Lay in 'receive time' */
1500 1501                  *tsp++ = htonl(ts);     /* Lay in 'send time' */
1501 1502                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1502 1503                  icmp_send_reply_v4(mp, ipha, icmph, ira);
1503 1504                  return (NULL);
1504 1505  
1505 1506          case ICMP_TIME_STAMP_REPLY:
1506 1507                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1507 1508                  break;
1508 1509          case ICMP_INFO_REQUEST:
1509 1510                  /* Per RFC 1122 3.2.2.7, ignore this. */
1510 1511          case ICMP_INFO_REPLY:
1511 1512                  break;
1512 1513          case ICMP_ADDRESS_MASK_REQUEST:
1513 1514                  if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1514 1515                          interested =
1515 1516                              ipst->ips_ip_respond_to_address_mask_broadcast;
1516 1517                  } else {
1517 1518                          interested = B_TRUE;
1518 1519                  }
1519 1520                  if (!interested) {
1520 1521                          /* We never pass these to RAW sockets */
1521 1522                          freemsg(mp);
1522 1523                          return (NULL);
1523 1524                  }
1524 1525                  len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1525 1526                  if (mp->b_wptr - mp->b_rptr < len_needed) {
1526 1527                          ipha = ip_pullup(mp, len_needed, ira);
1527 1528                          if (ipha == NULL) {
1528 1529                                  BUMP_MIB(ill->ill_ip_mib,
1529 1530                                      ipIfStatsInTruncatedPkts);
1530 1531                                  ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1531 1532                                      ill);
1532 1533                                  freemsg(mp);
1533 1534                                  return (NULL);
1534 1535                          }
1535 1536                          /* Refresh following the pullup. */
1536 1537                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1537 1538                  }
1538 1539                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1539 1540                  /* Check db_ref to make sure we can modify the packet. */
1540 1541                  if (mp->b_datap->db_ref > 1) {
1541 1542                          mblk_t  *mp1;
1542 1543  
1543 1544                          mp1 = copymsg(mp);
1544 1545                          freemsg(mp);
1545 1546                          if (!mp1) {
1546 1547                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1547 1548                                  return (NULL);
1548 1549                          }
1549 1550                          mp = mp1;
1550 1551                          ipha = (ipha_t *)mp->b_rptr;
1551 1552                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1552 1553                  }
1553 1554                  /*
1554 1555                   * Need the ipif with the mask be the same as the source
1555 1556                   * address of the mask reply. For unicast we have a specific
1556 1557                   * ipif. For multicast/broadcast we only handle onlink
1557 1558                   * senders, and use the source address to pick an ipif.
1558 1559                   */
1559 1560                  ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1560 1561                  if (ipif == NULL) {
1561 1562                          /* Broadcast or multicast */
1562 1563                          ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1563 1564                          if (ipif == NULL) {
1564 1565                                  freemsg(mp);
1565 1566                                  return (NULL);
1566 1567                          }
1567 1568                  }
1568 1569                  icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1569 1570                  bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1570 1571                  ipif_refrele(ipif);
1571 1572                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1572 1573                  icmp_send_reply_v4(mp, ipha, icmph, ira);
1573 1574                  return (NULL);
1574 1575  
1575 1576          case ICMP_ADDRESS_MASK_REPLY:
1576 1577                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1577 1578                  break;
1578 1579          default:
1579 1580                  interested = B_TRUE;    /* Pass up to transport */
1580 1581                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1581 1582                  break;
1582 1583          }
1583 1584          /*
1584 1585           * See if there is an ICMP client to avoid an extra copymsg/freemsg
1585 1586           * if there isn't one.
1586 1587           */
1587 1588          if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1588 1589                  /* If there is an ICMP client and we want one too, copy it. */
1589 1590  
1590 1591                  if (!interested) {
1591 1592                          /* Caller will deliver to RAW sockets */
1592 1593                          return (mp);
1593 1594                  }
1594 1595                  mp_ret = copymsg(mp);
1595 1596                  if (mp_ret == NULL) {
1596 1597                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1597 1598                          ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1598 1599                  }
1599 1600          } else if (!interested) {
1600 1601                  /* Neither we nor raw sockets are interested. Drop packet now */
1601 1602                  freemsg(mp);
1602 1603                  return (NULL);
1603 1604          }
1604 1605  
1605 1606          /*
1606 1607           * ICMP error or redirect packet. Make sure we have enough of
1607 1608           * the header and that db_ref == 1 since we might end up modifying
1608 1609           * the packet.
1609 1610           */
1610 1611          if (mp->b_cont != NULL) {
1611 1612                  if (ip_pullup(mp, -1, ira) == NULL) {
1612 1613                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1613 1614                          ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1614 1615                              mp, ill);
1615 1616                          freemsg(mp);
1616 1617                          return (mp_ret);
1617 1618                  }
1618 1619          }
1619 1620  
1620 1621          if (mp->b_datap->db_ref > 1) {
1621 1622                  mblk_t  *mp1;
1622 1623  
1623 1624                  mp1 = copymsg(mp);
1624 1625                  if (mp1 == NULL) {
1625 1626                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1626 1627                          ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1627 1628                          freemsg(mp);
1628 1629                          return (mp_ret);
1629 1630                  }
1630 1631                  freemsg(mp);
1631 1632                  mp = mp1;
1632 1633          }
1633 1634  
1634 1635          /*
1635 1636           * In case mp has changed, verify the message before any further
1636 1637           * processes.
1637 1638           */
1638 1639          ipha = (ipha_t *)mp->b_rptr;
1639 1640          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1640 1641          if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1641 1642                  freemsg(mp);
1642 1643                  return (mp_ret);
1643 1644          }
1644 1645  
1645 1646          switch (icmph->icmph_type) {
1646 1647          case ICMP_REDIRECT:
1647 1648                  icmp_redirect_v4(mp, ipha, icmph, ira);
1648 1649                  break;
1649 1650          case ICMP_DEST_UNREACHABLE:
1650 1651                  if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1651 1652                          /* Update DCE and adjust MTU is icmp header if needed */
1652 1653                          icmp_inbound_too_big_v4(icmph, ira);
1653 1654                  }
1654 1655                  /* FALLTHRU */
1655 1656          default:
1656 1657                  icmp_inbound_error_fanout_v4(mp, icmph, ira);
1657 1658                  break;
1658 1659          }
1659 1660          return (mp_ret);
1660 1661  }
1661 1662  
1662 1663  /*
1663 1664   * Send an ICMP echo, timestamp or address mask reply.
1664 1665   * The caller has already updated the payload part of the packet.
1665 1666   * We handle the ICMP checksum, IP source address selection and feed
1666 1667   * the packet into ip_output_simple.
1667 1668   */
1668 1669  static void
1669 1670  icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1670 1671      ip_recv_attr_t *ira)
1671 1672  {
1672 1673          uint_t          ip_hdr_length = ira->ira_ip_hdr_length;
1673 1674          ill_t           *ill = ira->ira_ill;
1674 1675          ip_stack_t      *ipst = ill->ill_ipst;
1675 1676          ip_xmit_attr_t  ixas;
1676 1677  
1677 1678          /* Send out an ICMP packet */
1678 1679          icmph->icmph_checksum = 0;
1679 1680          icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1680 1681          /* Reset time to live. */
1681 1682          ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1682 1683          {
1683 1684                  /* Swap source and destination addresses */
1684 1685                  ipaddr_t tmp;
1685 1686  
1686 1687                  tmp = ipha->ipha_src;
1687 1688                  ipha->ipha_src = ipha->ipha_dst;
1688 1689                  ipha->ipha_dst = tmp;
1689 1690          }
1690 1691          ipha->ipha_ident = 0;
1691 1692          if (!IS_SIMPLE_IPH(ipha))
1692 1693                  icmp_options_update(ipha);
1693 1694  
1694 1695          bzero(&ixas, sizeof (ixas));
1695 1696          ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1696 1697          ixas.ixa_zoneid = ira->ira_zoneid;
1697 1698          ixas.ixa_cred = kcred;
1698 1699          ixas.ixa_cpid = NOPID;
1699 1700          ixas.ixa_tsl = ira->ira_tsl;    /* Behave as a multi-level responder */
1700 1701          ixas.ixa_ifindex = 0;
1701 1702          ixas.ixa_ipst = ipst;
1702 1703          ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1703 1704  
1704 1705          if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1705 1706                  /*
1706 1707                   * This packet should go out the same way as it
1707 1708                   * came in i.e in clear, independent of the IPsec policy
1708 1709                   * for transmitting packets.
1709 1710                   */
1710 1711                  ixas.ixa_flags |= IXAF_NO_IPSEC;
1711 1712          } else {
1712 1713                  if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1713 1714                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1714 1715                          /* Note: mp already consumed and ip_drop_packet done */
1715 1716                          return;
1716 1717                  }
1717 1718          }
1718 1719          if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1719 1720                  /*
1720 1721                   * Not one or our addresses (IRE_LOCALs), thus we let
1721 1722                   * ip_output_simple pick the source.
1722 1723                   */
1723 1724                  ipha->ipha_src = INADDR_ANY;
1724 1725                  ixas.ixa_flags |= IXAF_SET_SOURCE;
1725 1726          }
1726 1727          /* Should we send with DF and use dce_pmtu? */
1727 1728          if (ipst->ips_ipv4_icmp_return_pmtu) {
1728 1729                  ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1729 1730                  ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1730 1731          }
1731 1732  
1732 1733          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1733 1734  
1734 1735          (void) ip_output_simple(mp, &ixas);
1735 1736          ixa_cleanup(&ixas);
1736 1737  }
1737 1738  
1738 1739  /*
1739 1740   * Verify the ICMP messages for either for ICMP error or redirect packet.
1740 1741   * The caller should have fully pulled up the message. If it's a redirect
1741 1742   * packet, only basic checks on IP header will be done; otherwise, verify
1742 1743   * the packet by looking at the included ULP header.
1743 1744   *
1744 1745   * Called before icmp_inbound_error_fanout_v4 is called.
1745 1746   */
1746 1747  static boolean_t
1747 1748  icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1748 1749  {
1749 1750          ill_t           *ill = ira->ira_ill;
1750 1751          int             hdr_length;
1751 1752          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
1752 1753          conn_t          *connp;
1753 1754          ipha_t          *ipha;  /* Inner IP header */
1754 1755  
1755 1756          ipha = (ipha_t *)&icmph[1];
1756 1757          if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1757 1758                  goto truncated;
1758 1759  
1759 1760          hdr_length = IPH_HDR_LENGTH(ipha);
1760 1761  
1761 1762          if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1762 1763                  goto discard_pkt;
1763 1764  
1764 1765          if (hdr_length < sizeof (ipha_t))
1765 1766                  goto truncated;
1766 1767  
1767 1768          if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1768 1769                  goto truncated;
1769 1770  
1770 1771          /*
1771 1772           * Stop here for ICMP_REDIRECT.
1772 1773           */
1773 1774          if (icmph->icmph_type == ICMP_REDIRECT)
1774 1775                  return (B_TRUE);
1775 1776  
1776 1777          /*
1777 1778           * ICMP errors only.
1778 1779           */
1779 1780          switch (ipha->ipha_protocol) {
1780 1781          case IPPROTO_UDP:
1781 1782                  /*
1782 1783                   * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1783 1784                   * transport header.
1784 1785                   */
1785 1786                  if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1786 1787                      mp->b_wptr)
1787 1788                          goto truncated;
1788 1789                  break;
1789 1790          case IPPROTO_TCP: {
1790 1791                  tcpha_t         *tcpha;
1791 1792  
1792 1793                  /*
1793 1794                   * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1794 1795                   * transport header.
1795 1796                   */
1796 1797                  if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1797 1798                      mp->b_wptr)
1798 1799                          goto truncated;
1799 1800  
1800 1801                  tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1801 1802                  connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1802 1803                      ipst);
1803 1804                  if (connp == NULL)
1804 1805                          goto discard_pkt;
1805 1806  
1806 1807                  if ((connp->conn_verifyicmp != NULL) &&
1807 1808                      !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1808 1809                          CONN_DEC_REF(connp);
1809 1810                          goto discard_pkt;
1810 1811                  }
1811 1812                  CONN_DEC_REF(connp);
1812 1813                  break;
1813 1814          }
1814 1815          case IPPROTO_SCTP:
1815 1816                  /*
1816 1817                   * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1817 1818                   * transport header.
1818 1819                   */
1819 1820                  if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1820 1821                      mp->b_wptr)
1821 1822                          goto truncated;
1822 1823                  break;
1823 1824          case IPPROTO_ESP:
1824 1825          case IPPROTO_AH:
1825 1826                  break;
1826 1827          case IPPROTO_ENCAP:
1827 1828                  if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1828 1829                      mp->b_wptr)
1829 1830                          goto truncated;
1830 1831                  break;
1831 1832          default:
1832 1833                  break;
1833 1834          }
1834 1835  
1835 1836          return (B_TRUE);
1836 1837  
1837 1838  discard_pkt:
1838 1839          /* Bogus ICMP error. */
1839 1840          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1840 1841          return (B_FALSE);
1841 1842  
1842 1843  truncated:
1843 1844          /* We pulled up everthing already. Must be truncated */
1844 1845          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1845 1846          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1846 1847          return (B_FALSE);
1847 1848  }
1848 1849  
1849 1850  /* Table from RFC 1191 */
1850 1851  static int icmp_frag_size_table[] =
1851 1852  { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1852 1853  
1853 1854  /*
1854 1855   * Process received ICMP Packet too big.
1855 1856   * Just handles the DCE create/update, including using the above table of
1856 1857   * PMTU guesses. The caller is responsible for validating the packet before
1857 1858   * passing it in and also to fanout the ICMP error to any matching transport
1858 1859   * conns. Assumes the message has been fully pulled up and verified.
1859 1860   *
1860 1861   * Before getting here, the caller has called icmp_inbound_verify_v4()
1861 1862   * that should have verified with ULP to prevent undoing the changes we're
1862 1863   * going to make to DCE. For example, TCP might have verified that the packet
1863 1864   * which generated error is in the send window.
1864 1865   *
1865 1866   * In some cases modified this MTU in the ICMP header packet; the caller
1866 1867   * should pass to the matching ULP after this returns.
1867 1868   */
1868 1869  static void
1869 1870  icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1870 1871  {
1871 1872          dce_t           *dce;
1872 1873          int             old_mtu;
1873 1874          int             mtu, orig_mtu;
1874 1875          ipaddr_t        dst;
1875 1876          boolean_t       disable_pmtud;
1876 1877          ill_t           *ill = ira->ira_ill;
1877 1878          ip_stack_t      *ipst = ill->ill_ipst;
1878 1879          uint_t          hdr_length;
1879 1880          ipha_t          *ipha;
1880 1881  
1881 1882          /* Caller already pulled up everything. */
1882 1883          ipha = (ipha_t *)&icmph[1];
1883 1884          ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1884 1885              icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1885 1886          ASSERT(ill != NULL);
1886 1887  
1887 1888          hdr_length = IPH_HDR_LENGTH(ipha);
1888 1889  
1889 1890          /*
1890 1891           * We handle path MTU for source routed packets since the DCE
1891 1892           * is looked up using the final destination.
1892 1893           */
1893 1894          dst = ip_get_dst(ipha);
1894 1895  
1895 1896          dce = dce_lookup_and_add_v4(dst, ipst);
1896 1897          if (dce == NULL) {
1897 1898                  /* Couldn't add a unique one - ENOMEM */
1898 1899                  ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1899 1900                      ntohl(dst)));
1900 1901                  return;
1901 1902          }
1902 1903  
1903 1904          /* Check for MTU discovery advice as described in RFC 1191 */
1904 1905          mtu = ntohs(icmph->icmph_du_mtu);
1905 1906          orig_mtu = mtu;
1906 1907          disable_pmtud = B_FALSE;
1907 1908  
1908 1909          mutex_enter(&dce->dce_lock);
1909 1910          if (dce->dce_flags & DCEF_PMTU)
1910 1911                  old_mtu = dce->dce_pmtu;
1911 1912          else
1912 1913                  old_mtu = ill->ill_mtu;
1913 1914  
1914 1915          if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1915 1916                  uint32_t length;
1916 1917                  int     i;
1917 1918  
1918 1919                  /*
1919 1920                   * Use the table from RFC 1191 to figure out
1920 1921                   * the next "plateau" based on the length in
1921 1922                   * the original IP packet.
1922 1923                   */
1923 1924                  length = ntohs(ipha->ipha_length);
1924 1925                  DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1925 1926                      uint32_t, length);
1926 1927                  if (old_mtu <= length &&
1927 1928                      old_mtu >= length - hdr_length) {
1928 1929                          /*
1929 1930                           * Handle broken BSD 4.2 systems that
1930 1931                           * return the wrong ipha_length in ICMP
1931 1932                           * errors.
1932 1933                           */
1933 1934                          ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1934 1935                              length, old_mtu));
1935 1936                          length -= hdr_length;
1936 1937                  }
1937 1938                  for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1938 1939                          if (length > icmp_frag_size_table[i])
1939 1940                                  break;
1940 1941                  }
1941 1942                  if (i == A_CNT(icmp_frag_size_table)) {
1942 1943                          /* Smaller than IP_MIN_MTU! */
1943 1944                          ip1dbg(("Too big for packet size %d\n",
1944 1945                              length));
1945 1946                          disable_pmtud = B_TRUE;
1946 1947                          mtu = ipst->ips_ip_pmtu_min;
1947 1948                  } else {
1948 1949                          mtu = icmp_frag_size_table[i];
1949 1950                          ip1dbg(("Calculated mtu %d, packet size %d, "
1950 1951                              "before %d\n", mtu, length, old_mtu));
1951 1952                          if (mtu < ipst->ips_ip_pmtu_min) {
1952 1953                                  mtu = ipst->ips_ip_pmtu_min;
1953 1954                                  disable_pmtud = B_TRUE;
1954 1955                          }
1955 1956                  }
1956 1957          }
1957 1958          if (disable_pmtud)
1958 1959                  dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1959 1960          else
1960 1961                  dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1961 1962  
1962 1963          dce->dce_pmtu = MIN(old_mtu, mtu);
1963 1964          /* Prepare to send the new max frag size for the ULP. */
1964 1965          icmph->icmph_du_zero = 0;
1965 1966          icmph->icmph_du_mtu =  htons((uint16_t)dce->dce_pmtu);
1966 1967          DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1967 1968              dce, int, orig_mtu, int, mtu);
1968 1969  
1969 1970          /* We now have a PMTU for sure */
1970 1971          dce->dce_flags |= DCEF_PMTU;
1971 1972          dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1972 1973          mutex_exit(&dce->dce_lock);
1973 1974          /*
1974 1975           * After dropping the lock the new value is visible to everyone.
1975 1976           * Then we bump the generation number so any cached values reinspect
1976 1977           * the dce_t.
1977 1978           */
1978 1979          dce_increment_generation(dce);
1979 1980          dce_refrele(dce);
1980 1981  }
1981 1982  
1982 1983  /*
1983 1984   * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1984 1985   * calls this function.
1985 1986   */
1986 1987  static mblk_t *
1987 1988  icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1988 1989  {
1989 1990          int length;
1990 1991  
1991 1992          ASSERT(mp->b_datap->db_type == M_DATA);
1992 1993  
1993 1994          /* icmp_inbound_v4 has already pulled up the whole error packet */
1994 1995          ASSERT(mp->b_cont == NULL);
1995 1996  
1996 1997          /*
1997 1998           * The length that we want to overlay is the inner header
1998 1999           * and what follows it.
1999 2000           */
2000 2001          length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
2001 2002  
2002 2003          /*
2003 2004           * Overlay the inner header and whatever follows it over the
2004 2005           * outer header.
2005 2006           */
2006 2007          bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2007 2008  
2008 2009          /* Adjust for what we removed */
2009 2010          mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
2010 2011          return (mp);
2011 2012  }
2012 2013  
2013 2014  /*
2014 2015   * Try to pass the ICMP message upstream in case the ULP cares.
2015 2016   *
2016 2017   * If the packet that caused the ICMP error is secure, we send
2017 2018   * it to AH/ESP to make sure that the attached packet has a
2018 2019   * valid association. ipha in the code below points to the
2019 2020   * IP header of the packet that caused the error.
2020 2021   *
2021 2022   * For IPsec cases, we let the next-layer-up (which has access to
2022 2023   * cached policy on the conn_t, or can query the SPD directly)
2023 2024   * subtract out any IPsec overhead if they must.  We therefore make no
2024 2025   * adjustments here for IPsec overhead.
2025 2026   *
2026 2027   * IFN could have been generated locally or by some router.
2027 2028   *
2028 2029   * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
2029 2030   * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
2030 2031   *          This happens because IP adjusted its value of MTU on an
2031 2032   *          earlier IFN message and could not tell the upper layer,
2032 2033   *          the new adjusted value of MTU e.g. Packet was encrypted
2033 2034   *          or there was not enough information to fanout to upper
2034 2035   *          layers. Thus on the next outbound datagram, ire_send_wire
2035 2036   *          generates the IFN, where IPsec processing has *not* been
2036 2037   *          done.
2037 2038   *
2038 2039   *          Note that we retain ixa_fragsize across IPsec thus once
2039 2040   *          we have picking ixa_fragsize and entered ipsec_out_process we do
2040 2041   *          no change the fragsize even if the path MTU changes before
2041 2042   *          we reach ip_output_post_ipsec.
2042 2043   *
2043 2044   *          In the local case, IRAF_LOOPBACK will be set indicating
2044 2045   *          that IFN was generated locally.
2045 2046   *
2046 2047   * ROUTER : IFN could be secure or non-secure.
2047 2048   *
2048 2049   *          * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2049 2050   *            packet in error has AH/ESP headers to validate the AH/ESP
2050 2051   *            headers. AH/ESP will verify whether there is a valid SA or
2051 2052   *            not and send it back. We will fanout again if we have more
2052 2053   *            data in the packet.
2053 2054   *
2054 2055   *            If the packet in error does not have AH/ESP, we handle it
2055 2056   *            like any other case.
2056 2057   *
2057 2058   *          * NON_SECURE : If the packet in error has AH/ESP headers, we send it
2058 2059   *            up to AH/ESP for validation. AH/ESP will verify whether there is a
2059 2060   *            valid SA or not and send it back. We will fanout again if
2060 2061   *            we have more data in the packet.
2061 2062   *
2062 2063   *            If the packet in error does not have AH/ESP, we handle it
2063 2064   *            like any other case.
2064 2065   *
2065 2066   * The caller must have called icmp_inbound_verify_v4.
2066 2067   */
2067 2068  static void
2068 2069  icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2069 2070  {
2070 2071          uint16_t        *up;    /* Pointer to ports in ULP header */
2071 2072          uint32_t        ports;  /* reversed ports for fanout */
2072 2073          ipha_t          ripha;  /* With reversed addresses */
2073 2074          ipha_t          *ipha;  /* Inner IP header */
2074 2075          uint_t          hdr_length; /* Inner IP header length */
2075 2076          tcpha_t         *tcpha;
2076 2077          conn_t          *connp;
2077 2078          ill_t           *ill = ira->ira_ill;
2078 2079          ip_stack_t      *ipst = ill->ill_ipst;
2079 2080          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
2080 2081          ill_t           *rill = ira->ira_rill;
2081 2082  
2082 2083          /* Caller already pulled up everything. */
2083 2084          ipha = (ipha_t *)&icmph[1];
2084 2085          ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2085 2086          ASSERT(mp->b_cont == NULL);
2086 2087  
2087 2088          hdr_length = IPH_HDR_LENGTH(ipha);
2088 2089          ira->ira_protocol = ipha->ipha_protocol;
2089 2090  
2090 2091          /*
2091 2092           * We need a separate IP header with the source and destination
2092 2093           * addresses reversed to do fanout/classification because the ipha in
2093 2094           * the ICMP error is in the form we sent it out.
2094 2095           */
2095 2096          ripha.ipha_src = ipha->ipha_dst;
2096 2097          ripha.ipha_dst = ipha->ipha_src;
2097 2098          ripha.ipha_protocol = ipha->ipha_protocol;
2098 2099          ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2099 2100  
2100 2101          ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2101 2102              ripha.ipha_protocol, ntohl(ipha->ipha_src),
2102 2103              ntohl(ipha->ipha_dst),
2103 2104              icmph->icmph_type, icmph->icmph_code));
2104 2105  
2105 2106          switch (ipha->ipha_protocol) {
2106 2107          case IPPROTO_UDP:
2107 2108                  up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2108 2109  
2109 2110                  /* Attempt to find a client stream based on port. */
2110 2111                  ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2111 2112                      ntohs(up[0]), ntohs(up[1])));
2112 2113  
2113 2114                  /* Note that we send error to all matches. */
2114 2115                  ira->ira_flags |= IRAF_ICMP_ERROR;
2115 2116                  ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2116 2117                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2117 2118                  return;
2118 2119  
2119 2120          case IPPROTO_TCP:
2120 2121                  /*
2121 2122                   * Find a TCP client stream for this packet.
2122 2123                   * Note that we do a reverse lookup since the header is
2123 2124                   * in the form we sent it out.
2124 2125                   */
2125 2126                  tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2126 2127                  connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2127 2128                      ipst);
2128 2129                  if (connp == NULL)
2129 2130                          goto discard_pkt;
2130 2131  
2131 2132                  if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2132 2133                      (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2133 2134                          mp = ipsec_check_inbound_policy(mp, connp,
2134 2135                              ipha, NULL, ira);
2135 2136                          if (mp == NULL) {
2136 2137                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2137 2138                                  /* Note that mp is NULL */
2138 2139                                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
2139 2140                                  CONN_DEC_REF(connp);
2140 2141                                  return;
2141 2142                          }
2142 2143                  }
2143 2144  
2144 2145                  ira->ira_flags |= IRAF_ICMP_ERROR;
2145 2146                  ira->ira_ill = ira->ira_rill = NULL;
2146 2147                  if (IPCL_IS_TCP(connp)) {
2147 2148                          SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2148 2149                              connp->conn_recvicmp, connp, ira, SQ_FILL,
2149 2150                              SQTAG_TCP_INPUT_ICMP_ERR);
2150 2151                  } else {
2151 2152                          /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2152 2153                          (connp->conn_recv)(connp, mp, NULL, ira);
2153 2154                          CONN_DEC_REF(connp);
2154 2155                  }
2155 2156                  ira->ira_ill = ill;
2156 2157                  ira->ira_rill = rill;
2157 2158                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2158 2159                  return;
2159 2160  
2160 2161          case IPPROTO_SCTP:
2161 2162                  up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2162 2163                  /* Find a SCTP client stream for this packet. */
2163 2164                  ((uint16_t *)&ports)[0] = up[1];
2164 2165                  ((uint16_t *)&ports)[1] = up[0];
2165 2166  
2166 2167                  ira->ira_flags |= IRAF_ICMP_ERROR;
2167 2168                  ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2168 2169                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2169 2170                  return;
2170 2171  
2171 2172          case IPPROTO_ESP:
2172 2173          case IPPROTO_AH:
2173 2174                  if (!ipsec_loaded(ipss)) {
2174 2175                          ip_proto_not_sup(mp, ira);
2175 2176                          return;
2176 2177                  }
2177 2178  
2178 2179                  if (ipha->ipha_protocol == IPPROTO_ESP)
2179 2180                          mp = ipsecesp_icmp_error(mp, ira);
2180 2181                  else
2181 2182                          mp = ipsecah_icmp_error(mp, ira);
2182 2183                  if (mp == NULL)
2183 2184                          return;
2184 2185  
2185 2186                  /* Just in case ipsec didn't preserve the NULL b_cont */
2186 2187                  if (mp->b_cont != NULL) {
2187 2188                          if (!pullupmsg(mp, -1))
2188 2189                                  goto discard_pkt;
2189 2190                  }
2190 2191  
2191 2192                  /*
2192 2193                   * Note that ira_pktlen and ira_ip_hdr_length are no longer
2193 2194                   * correct, but we don't use them any more here.
2194 2195                   *
2195 2196                   * If succesful, the mp has been modified to not include
2196 2197                   * the ESP/AH header so we can fanout to the ULP's icmp
2197 2198                   * error handler.
2198 2199                   */
2199 2200                  if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2200 2201                          goto truncated;
2201 2202  
2202 2203                  /* Verify the modified message before any further processes. */
2203 2204                  ipha = (ipha_t *)mp->b_rptr;
2204 2205                  hdr_length = IPH_HDR_LENGTH(ipha);
2205 2206                  icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2206 2207                  if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2207 2208                          freemsg(mp);
2208 2209                          return;
2209 2210                  }
2210 2211  
2211 2212                  icmp_inbound_error_fanout_v4(mp, icmph, ira);
2212 2213                  return;
2213 2214  
2214 2215          case IPPROTO_ENCAP: {
2215 2216                  /* Look for self-encapsulated packets that caused an error */
2216 2217                  ipha_t *in_ipha;
2217 2218  
2218 2219                  /*
2219 2220                   * Caller has verified that length has to be
2220 2221                   * at least the size of IP header.
2221 2222                   */
2222 2223                  ASSERT(hdr_length >= sizeof (ipha_t));
2223 2224                  /*
2224 2225                   * Check the sanity of the inner IP header like
2225 2226                   * we did for the outer header.
2226 2227                   */
2227 2228                  in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2228 2229                  if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2229 2230                          goto discard_pkt;
2230 2231                  }
2231 2232                  if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2232 2233                          goto discard_pkt;
2233 2234                  }
2234 2235                  /* Check for Self-encapsulated tunnels */
2235 2236                  if (in_ipha->ipha_src == ipha->ipha_src &&
2236 2237                      in_ipha->ipha_dst == ipha->ipha_dst) {
2237 2238  
2238 2239                          mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2239 2240                              in_ipha);
2240 2241                          if (mp == NULL)
2241 2242                                  goto discard_pkt;
2242 2243  
2243 2244                          /*
2244 2245                           * Just in case self_encap didn't preserve the NULL
2245 2246                           * b_cont
2246 2247                           */
2247 2248                          if (mp->b_cont != NULL) {
2248 2249                                  if (!pullupmsg(mp, -1))
2249 2250                                          goto discard_pkt;
2250 2251                          }
2251 2252                          /*
2252 2253                           * Note that ira_pktlen and ira_ip_hdr_length are no
2253 2254                           * longer correct, but we don't use them any more here.
2254 2255                           */
2255 2256                          if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2256 2257                                  goto truncated;
2257 2258  
2258 2259                          /*
2259 2260                           * Verify the modified message before any further
2260 2261                           * processes.
2261 2262                           */
2262 2263                          ipha = (ipha_t *)mp->b_rptr;
2263 2264                          hdr_length = IPH_HDR_LENGTH(ipha);
2264 2265                          icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2265 2266                          if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2266 2267                                  freemsg(mp);
2267 2268                                  return;
2268 2269                          }
2269 2270  
2270 2271                          /*
2271 2272                           * The packet in error is self-encapsualted.
2272 2273                           * And we are finding it further encapsulated
2273 2274                           * which we could not have possibly generated.
2274 2275                           */
2275 2276                          if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2276 2277                                  goto discard_pkt;
2277 2278                          }
2278 2279                          icmp_inbound_error_fanout_v4(mp, icmph, ira);
2279 2280                          return;
2280 2281                  }
2281 2282                  /* No self-encapsulated */
2282 2283                  /* FALLTHRU */
2283 2284          }
2284 2285          case IPPROTO_IPV6:
2285 2286                  if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2286 2287                      &ripha.ipha_dst, ipst)) != NULL) {
2287 2288                          ira->ira_flags |= IRAF_ICMP_ERROR;
2288 2289                          connp->conn_recvicmp(connp, mp, NULL, ira);
2289 2290                          CONN_DEC_REF(connp);
2290 2291                          ira->ira_flags &= ~IRAF_ICMP_ERROR;
2291 2292                          return;
2292 2293                  }
2293 2294                  /*
2294 2295                   * No IP tunnel is interested, fallthrough and see
2295 2296                   * if a raw socket will want it.
2296 2297                   */
2297 2298                  /* FALLTHRU */
2298 2299          default:
2299 2300                  ira->ira_flags |= IRAF_ICMP_ERROR;
2300 2301                  ip_fanout_proto_v4(mp, &ripha, ira);
2301 2302                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2302 2303                  return;
2303 2304          }
2304 2305          /* NOTREACHED */
2305 2306  discard_pkt:
2306 2307          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2307 2308          ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2308 2309          ip_drop_input("ipIfStatsInDiscards", mp, ill);
2309 2310          freemsg(mp);
2310 2311          return;
2311 2312  
2312 2313  truncated:
2313 2314          /* We pulled up everthing already. Must be truncated */
2314 2315          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2315 2316          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2316 2317          freemsg(mp);
2317 2318  }
2318 2319  
2319 2320  /*
2320 2321   * Common IP options parser.
2321 2322   *
2322 2323   * Setup routine: fill in *optp with options-parsing state, then
2323 2324   * tail-call ipoptp_next to return the first option.
2324 2325   */
2325 2326  uint8_t
2326 2327  ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2327 2328  {
2328 2329          uint32_t totallen; /* total length of all options */
2329 2330  
2330 2331          totallen = ipha->ipha_version_and_hdr_length -
2331 2332              (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2332 2333          totallen <<= 2;
2333 2334          optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2334 2335          optp->ipoptp_end = optp->ipoptp_next + totallen;
2335 2336          optp->ipoptp_flags = 0;
2336 2337          return (ipoptp_next(optp));
2337 2338  }
2338 2339  
2339 2340  /* Like above but without an ipha_t */
2340 2341  uint8_t
2341 2342  ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2342 2343  {
2343 2344          optp->ipoptp_next = opt;
2344 2345          optp->ipoptp_end = optp->ipoptp_next + totallen;
2345 2346          optp->ipoptp_flags = 0;
2346 2347          return (ipoptp_next(optp));
2347 2348  }
2348 2349  
2349 2350  /*
2350 2351   * Common IP options parser: extract next option.
2351 2352   */
2352 2353  uint8_t
2353 2354  ipoptp_next(ipoptp_t *optp)
2354 2355  {
2355 2356          uint8_t *end = optp->ipoptp_end;
2356 2357          uint8_t *cur = optp->ipoptp_next;
2357 2358          uint8_t opt, len, pointer;
2358 2359  
2359 2360          /*
2360 2361           * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2361 2362           * has been corrupted.
2362 2363           */
2363 2364          ASSERT(cur <= end);
2364 2365  
2365 2366          if (cur == end)
2366 2367                  return (IPOPT_EOL);
2367 2368  
2368 2369          opt = cur[IPOPT_OPTVAL];
2369 2370  
2370 2371          /*
2371 2372           * Skip any NOP options.
2372 2373           */
2373 2374          while (opt == IPOPT_NOP) {
2374 2375                  cur++;
2375 2376                  if (cur == end)
2376 2377                          return (IPOPT_EOL);
2377 2378                  opt = cur[IPOPT_OPTVAL];
2378 2379          }
2379 2380  
2380 2381          if (opt == IPOPT_EOL)
2381 2382                  return (IPOPT_EOL);
2382 2383  
2383 2384          /*
2384 2385           * Option requiring a length.
2385 2386           */
2386 2387          if ((cur + 1) >= end) {
2387 2388                  optp->ipoptp_flags |= IPOPTP_ERROR;
2388 2389                  return (IPOPT_EOL);
2389 2390          }
2390 2391          len = cur[IPOPT_OLEN];
2391 2392          if (len < 2) {
2392 2393                  optp->ipoptp_flags |= IPOPTP_ERROR;
2393 2394                  return (IPOPT_EOL);
2394 2395          }
2395 2396          optp->ipoptp_cur = cur;
2396 2397          optp->ipoptp_len = len;
2397 2398          optp->ipoptp_next = cur + len;
2398 2399          if (cur + len > end) {
2399 2400                  optp->ipoptp_flags |= IPOPTP_ERROR;
2400 2401                  return (IPOPT_EOL);
2401 2402          }
2402 2403  
2403 2404          /*
2404 2405           * For the options which require a pointer field, make sure
2405 2406           * its there, and make sure it points to either something
2406 2407           * inside this option, or the end of the option.
2407 2408           */
2408 2409          switch (opt) {
2409 2410          case IPOPT_RR:
2410 2411          case IPOPT_TS:
2411 2412          case IPOPT_LSRR:
2412 2413          case IPOPT_SSRR:
2413 2414                  if (len <= IPOPT_OFFSET) {
2414 2415                          optp->ipoptp_flags |= IPOPTP_ERROR;
2415 2416                          return (opt);
2416 2417                  }
2417 2418                  pointer = cur[IPOPT_OFFSET];
2418 2419                  if (pointer - 1 > len) {
2419 2420                          optp->ipoptp_flags |= IPOPTP_ERROR;
2420 2421                          return (opt);
2421 2422                  }
2422 2423                  break;
2423 2424          }
2424 2425  
2425 2426          /*
2426 2427           * Sanity check the pointer field based on the type of the
2427 2428           * option.
2428 2429           */
2429 2430          switch (opt) {
2430 2431          case IPOPT_RR:
2431 2432          case IPOPT_SSRR:
2432 2433          case IPOPT_LSRR:
2433 2434                  if (pointer < IPOPT_MINOFF_SR)
2434 2435                          optp->ipoptp_flags |= IPOPTP_ERROR;
2435 2436                  break;
2436 2437          case IPOPT_TS:
2437 2438                  if (pointer < IPOPT_MINOFF_IT)
2438 2439                          optp->ipoptp_flags |= IPOPTP_ERROR;
2439 2440                  /*
2440 2441                   * Note that the Internet Timestamp option also
2441 2442                   * contains two four bit fields (the Overflow field,
2442 2443                   * and the Flag field), which follow the pointer
2443 2444                   * field.  We don't need to check that these fields
2444 2445                   * fall within the length of the option because this
2445 2446                   * was implicitely done above.  We've checked that the
2446 2447                   * pointer value is at least IPOPT_MINOFF_IT, and that
2447 2448                   * it falls within the option.  Since IPOPT_MINOFF_IT >
2448 2449                   * IPOPT_POS_OV_FLG, we don't need the explicit check.
2449 2450                   */
2450 2451                  ASSERT(len > IPOPT_POS_OV_FLG);
2451 2452                  break;
2452 2453          }
2453 2454  
2454 2455          return (opt);
2455 2456  }
2456 2457  
2457 2458  /*
2458 2459   * Use the outgoing IP header to create an IP_OPTIONS option the way
2459 2460   * it was passed down from the application.
2460 2461   *
2461 2462   * This is compatible with BSD in that it returns
2462 2463   * the reverse source route with the final destination
2463 2464   * as the last entry. The first 4 bytes of the option
2464 2465   * will contain the final destination.
2465 2466   */
2466 2467  int
2467 2468  ip_opt_get_user(conn_t *connp, uchar_t *buf)
2468 2469  {
2469 2470          ipoptp_t        opts;
2470 2471          uchar_t         *opt;
2471 2472          uint8_t         optval;
2472 2473          uint8_t         optlen;
2473 2474          uint32_t        len = 0;
2474 2475          uchar_t         *buf1 = buf;
2475 2476          uint32_t        totallen;
2476 2477          ipaddr_t        dst;
2477 2478          ip_pkt_t        *ipp = &connp->conn_xmit_ipp;
2478 2479  
2479 2480          if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2480 2481                  return (0);
2481 2482  
2482 2483          totallen = ipp->ipp_ipv4_options_len;
2483 2484          if (totallen & 0x3)
2484 2485                  return (0);
2485 2486  
2486 2487          buf += IP_ADDR_LEN;     /* Leave room for final destination */
2487 2488          len += IP_ADDR_LEN;
2488 2489          bzero(buf1, IP_ADDR_LEN);
2489 2490  
2490 2491          dst = connp->conn_faddr_v4;
2491 2492  
2492 2493          for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2493 2494              optval != IPOPT_EOL;
2494 2495              optval = ipoptp_next(&opts)) {
2495 2496                  int     off;
2496 2497  
2497 2498                  opt = opts.ipoptp_cur;
2498 2499                  if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2499 2500                          break;
2500 2501                  }
2501 2502                  optlen = opts.ipoptp_len;
2502 2503  
2503 2504                  switch (optval) {
2504 2505                  case IPOPT_SSRR:
2505 2506                  case IPOPT_LSRR:
2506 2507  
2507 2508                          /*
2508 2509                           * Insert destination as the first entry in the source
2509 2510                           * route and move down the entries on step.
2510 2511                           * The last entry gets placed at buf1.
2511 2512                           */
2512 2513                          buf[IPOPT_OPTVAL] = optval;
2513 2514                          buf[IPOPT_OLEN] = optlen;
2514 2515                          buf[IPOPT_OFFSET] = optlen;
2515 2516  
2516 2517                          off = optlen - IP_ADDR_LEN;
2517 2518                          if (off < 0) {
2518 2519                                  /* No entries in source route */
2519 2520                                  break;
2520 2521                          }
2521 2522                          /* Last entry in source route if not already set */
2522 2523                          if (dst == INADDR_ANY)
2523 2524                                  bcopy(opt + off, buf1, IP_ADDR_LEN);
2524 2525                          off -= IP_ADDR_LEN;
2525 2526  
2526 2527                          while (off > 0) {
2527 2528                                  bcopy(opt + off,
2528 2529                                      buf + off + IP_ADDR_LEN,
2529 2530                                      IP_ADDR_LEN);
2530 2531                                  off -= IP_ADDR_LEN;
2531 2532                          }
2532 2533                          /* ipha_dst into first slot */
2533 2534                          bcopy(&dst, buf + off + IP_ADDR_LEN,
2534 2535                              IP_ADDR_LEN);
2535 2536                          buf += optlen;
2536 2537                          len += optlen;
2537 2538                          break;
2538 2539  
2539 2540                  default:
2540 2541                          bcopy(opt, buf, optlen);
2541 2542                          buf += optlen;
2542 2543                          len += optlen;
2543 2544                          break;
2544 2545                  }
2545 2546          }
2546 2547  done:
2547 2548          /* Pad the resulting options */
2548 2549          while (len & 0x3) {
2549 2550                  *buf++ = IPOPT_EOL;
2550 2551                  len++;
2551 2552          }
2552 2553          return (len);
2553 2554  }
2554 2555  
2555 2556  /*
2556 2557   * Update any record route or timestamp options to include this host.
2557 2558   * Reverse any source route option.
2558 2559   * This routine assumes that the options are well formed i.e. that they
2559 2560   * have already been checked.
2560 2561   */
2561 2562  static void
2562 2563  icmp_options_update(ipha_t *ipha)
2563 2564  {
2564 2565          ipoptp_t        opts;
2565 2566          uchar_t         *opt;
2566 2567          uint8_t         optval;
2567 2568          ipaddr_t        src;            /* Our local address */
2568 2569          ipaddr_t        dst;
2569 2570  
2570 2571          ip2dbg(("icmp_options_update\n"));
2571 2572          src = ipha->ipha_src;
2572 2573          dst = ipha->ipha_dst;
2573 2574  
2574 2575          for (optval = ipoptp_first(&opts, ipha);
2575 2576              optval != IPOPT_EOL;
2576 2577              optval = ipoptp_next(&opts)) {
2577 2578                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2578 2579                  opt = opts.ipoptp_cur;
2579 2580                  ip2dbg(("icmp_options_update: opt %d, len %d\n",
2580 2581                      optval, opts.ipoptp_len));
2581 2582                  switch (optval) {
2582 2583                          int off1, off2;
2583 2584                  case IPOPT_SSRR:
2584 2585                  case IPOPT_LSRR:
2585 2586                          /*
2586 2587                           * Reverse the source route.  The first entry
2587 2588                           * should be the next to last one in the current
2588 2589                           * source route (the last entry is our address).
2589 2590                           * The last entry should be the final destination.
2590 2591                           */
2591 2592                          off1 = IPOPT_MINOFF_SR - 1;
2592 2593                          off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2593 2594                          if (off2 < 0) {
2594 2595                                  /* No entries in source route */
2595 2596                                  ip1dbg((
2596 2597                                      "icmp_options_update: bad src route\n"));
2597 2598                                  break;
2598 2599                          }
2599 2600                          bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2600 2601                          bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2601 2602                          bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2602 2603                          off2 -= IP_ADDR_LEN;
2603 2604  
2604 2605                          while (off1 < off2) {
2605 2606                                  bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2606 2607                                  bcopy((char *)opt + off2, (char *)opt + off1,
2607 2608                                      IP_ADDR_LEN);
2608 2609                                  bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2609 2610                                  off1 += IP_ADDR_LEN;
2610 2611                                  off2 -= IP_ADDR_LEN;
2611 2612                          }
2612 2613                          opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2613 2614                          break;
2614 2615                  }
2615 2616          }
2616 2617  }
2617 2618  
2618 2619  /*
2619 2620   * Process received ICMP Redirect messages.
2620 2621   * Assumes the caller has verified that the headers are in the pulled up mblk.
2621 2622   * Consumes mp.
2622 2623   */
2623 2624  static void
2624 2625  icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2625 2626  {
2626 2627          ire_t           *ire, *nire;
2627 2628          ire_t           *prev_ire;
2628 2629          ipaddr_t        src, dst, gateway;
2629 2630          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
2630 2631          ipha_t          *inner_ipha;    /* Inner IP header */
2631 2632  
2632 2633          /* Caller already pulled up everything. */
2633 2634          inner_ipha = (ipha_t *)&icmph[1];
2634 2635          src = ipha->ipha_src;
2635 2636          dst = inner_ipha->ipha_dst;
2636 2637          gateway = icmph->icmph_rd_gateway;
2637 2638          /* Make sure the new gateway is reachable somehow. */
2638 2639          ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2639 2640              ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
2640 2641          /*
2641 2642           * Make sure we had a route for the dest in question and that
2642 2643           * that route was pointing to the old gateway (the source of the
2643 2644           * redirect packet.)
2644 2645           * We do longest match and then compare ire_gateway_addr below.
2645 2646           */
2646 2647          prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2647 2648              NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
2648 2649          /*
2649 2650           * Check that
2650 2651           *      the redirect was not from ourselves
2651 2652           *      the new gateway and the old gateway are directly reachable
2652 2653           */
2653 2654          if (prev_ire == NULL || ire == NULL ||
2654 2655              (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2655 2656              (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2656 2657              !(ire->ire_type & IRE_IF_ALL) ||
2657 2658              prev_ire->ire_gateway_addr != src) {
2658 2659                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2659 2660                  ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2660 2661                  freemsg(mp);
2661 2662                  if (ire != NULL)
2662 2663                          ire_refrele(ire);
2663 2664                  if (prev_ire != NULL)
2664 2665                          ire_refrele(prev_ire);
2665 2666                  return;
2666 2667          }
2667 2668  
2668 2669          ire_refrele(prev_ire);
2669 2670          ire_refrele(ire);
2670 2671  
2671 2672          /*
2672 2673           * TODO: more precise handling for cases 0, 2, 3, the latter two
2673 2674           * require TOS routing
2674 2675           */
2675 2676          switch (icmph->icmph_code) {
2676 2677          case 0:
2677 2678          case 1:
2678 2679                  /* TODO: TOS specificity for cases 2 and 3 */
2679 2680          case 2:
2680 2681          case 3:
2681 2682                  break;
2682 2683          default:
2683 2684                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2684 2685                  ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2685 2686                  freemsg(mp);
2686 2687                  return;
2687 2688          }
2688 2689          /*
2689 2690           * Create a Route Association.  This will allow us to remember that
2690 2691           * someone we believe told us to use the particular gateway.
2691 2692           */
2692 2693          ire = ire_create(
2693 2694              (uchar_t *)&dst,                    /* dest addr */
2694 2695              (uchar_t *)&ip_g_all_ones,          /* mask */
2695 2696              (uchar_t *)&gateway,                /* gateway addr */
2696 2697              IRE_HOST,
2697 2698              NULL,                               /* ill */
2698 2699              ALL_ZONES,
2699 2700              (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2700 2701              NULL,                               /* tsol_gc_t */
2701 2702              ipst);
2702 2703  
2703 2704          if (ire == NULL) {
2704 2705                  freemsg(mp);
2705 2706                  return;
2706 2707          }
2707 2708          nire = ire_add(ire);
2708 2709          /* Check if it was a duplicate entry */
2709 2710          if (nire != NULL && nire != ire) {
2710 2711                  ASSERT(nire->ire_identical_ref > 1);
2711 2712                  ire_delete(nire);
2712 2713                  ire_refrele(nire);
2713 2714                  nire = NULL;
2714 2715          }
2715 2716          ire = nire;
2716 2717          if (ire != NULL) {
2717 2718                  ire_refrele(ire);               /* Held in ire_add */
2718 2719  
2719 2720                  /* tell routing sockets that we received a redirect */
2720 2721                  ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2721 2722                      (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2722 2723                      (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2723 2724          }
2724 2725  
2725 2726          /*
2726 2727           * Delete any existing IRE_HOST type redirect ires for this destination.
2727 2728           * This together with the added IRE has the effect of
2728 2729           * modifying an existing redirect.
2729 2730           */
2730 2731          prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2731 2732              ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2732 2733          if (prev_ire != NULL) {
2733 2734                  if (prev_ire ->ire_flags & RTF_DYNAMIC)
2734 2735                          ire_delete(prev_ire);
2735 2736                  ire_refrele(prev_ire);
2736 2737          }
2737 2738  
2738 2739          freemsg(mp);
2739 2740  }
2740 2741  
2741 2742  /*
2742 2743   * Generate an ICMP parameter problem message.
2743 2744   * When called from ip_output side a minimal ip_recv_attr_t needs to be
2744 2745   * constructed by the caller.
2745 2746   */
2746 2747  static void
2747 2748  icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2748 2749  {
2749 2750          icmph_t icmph;
2750 2751          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
2751 2752  
2752 2753          mp = icmp_pkt_err_ok(mp, ira);
2753 2754          if (mp == NULL)
2754 2755                  return;
2755 2756  
2756 2757          bzero(&icmph, sizeof (icmph_t));
2757 2758          icmph.icmph_type = ICMP_PARAM_PROBLEM;
2758 2759          icmph.icmph_pp_ptr = ptr;
2759 2760          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2760 2761          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2761 2762  }
2762 2763  
2763 2764  /*
2764 2765   * Build and ship an IPv4 ICMP message using the packet data in mp, and
2765 2766   * the ICMP header pointed to by "stuff".  (May be called as writer.)
2766 2767   * Note: assumes that icmp_pkt_err_ok has been called to verify that
2767 2768   * an icmp error packet can be sent.
2768 2769   * Assigns an appropriate source address to the packet. If ipha_dst is
2769 2770   * one of our addresses use it for source. Otherwise let ip_output_simple
2770 2771   * pick the source address.
2771 2772   */
2772 2773  static void
2773 2774  icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2774 2775  {
2775 2776          ipaddr_t dst;
2776 2777          icmph_t *icmph;
2777 2778          ipha_t  *ipha;
2778 2779          uint_t  len_needed;
2779 2780          size_t  msg_len;
2780 2781          mblk_t  *mp1;
2781 2782          ipaddr_t src;
2782 2783          ire_t   *ire;
2783 2784          ip_xmit_attr_t ixas;
2784 2785          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2785 2786  
2786 2787          ipha = (ipha_t *)mp->b_rptr;
2787 2788  
2788 2789          bzero(&ixas, sizeof (ixas));
2789 2790          ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2790 2791          ixas.ixa_zoneid = ira->ira_zoneid;
2791 2792          ixas.ixa_ifindex = 0;
2792 2793          ixas.ixa_ipst = ipst;
2793 2794          ixas.ixa_cred = kcred;
2794 2795          ixas.ixa_cpid = NOPID;
2795 2796          ixas.ixa_tsl = ira->ira_tsl;    /* Behave as a multi-level responder */
2796 2797          ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2797 2798  
2798 2799          if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2799 2800                  /*
2800 2801                   * Apply IPsec based on how IPsec was applied to
2801 2802                   * the packet that had the error.
2802 2803                   *
2803 2804                   * If it was an outbound packet that caused the ICMP
2804 2805                   * error, then the caller will have setup the IRA
2805 2806                   * appropriately.
2806 2807                   */
2807 2808                  if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2808 2809                          BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2809 2810                          /* Note: mp already consumed and ip_drop_packet done */
2810 2811                          return;
2811 2812                  }
2812 2813          } else {
2813 2814                  /*
2814 2815                   * This is in clear. The icmp message we are building
2815 2816                   * here should go out in clear, independent of our policy.
2816 2817                   */
2817 2818                  ixas.ixa_flags |= IXAF_NO_IPSEC;
2818 2819          }
2819 2820  
2820 2821          /* Remember our eventual destination */
2821 2822          dst = ipha->ipha_src;
2822 2823  
2823 2824          /*
2824 2825           * If the packet was for one of our unicast addresses, make
2825 2826           * sure we respond with that as the source. Otherwise
2826 2827           * have ip_output_simple pick the source address.
2827 2828           */
2828 2829          ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2829 2830              (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL,
2830 2831              MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2831 2832          if (ire != NULL) {
2832 2833                  ire_refrele(ire);
2833 2834                  src = ipha->ipha_dst;
2834 2835          } else {
2835 2836                  src = INADDR_ANY;
2836 2837                  ixas.ixa_flags |= IXAF_SET_SOURCE;
2837 2838          }
2838 2839  
2839 2840          /*
2840 2841           * Check if we can send back more then 8 bytes in addition to
2841 2842           * the IP header.  We try to send 64 bytes of data and the internal
2842 2843           * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2843 2844           */
2844 2845          len_needed = IPH_HDR_LENGTH(ipha);
2845 2846          if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2846 2847              ipha->ipha_protocol == IPPROTO_IPV6) {
2847 2848                  if (!pullupmsg(mp, -1)) {
2848 2849                          BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2849 2850                          ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2850 2851                          freemsg(mp);
2851 2852                          return;
2852 2853                  }
2853 2854                  ipha = (ipha_t *)mp->b_rptr;
2854 2855  
2855 2856                  if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2856 2857                          len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2857 2858                              len_needed));
2858 2859                  } else {
2859 2860                          ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2860 2861  
2861 2862                          ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2862 2863                          len_needed += ip_hdr_length_v6(mp, ip6h);
2863 2864                  }
2864 2865          }
2865 2866          len_needed += ipst->ips_ip_icmp_return;
2866 2867          msg_len = msgdsize(mp);
2867 2868          if (msg_len > len_needed) {
2868 2869                  (void) adjmsg(mp, len_needed - msg_len);
2869 2870                  msg_len = len_needed;
2870 2871          }
2871 2872          mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2872 2873          if (mp1 == NULL) {
2873 2874                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2874 2875                  freemsg(mp);
2875 2876                  return;
2876 2877          }
2877 2878          mp1->b_cont = mp;
2878 2879          mp = mp1;
2879 2880  
2880 2881          /*
2881 2882           * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2882 2883           * node generates be accepted in peace by all on-host destinations.
2883 2884           * If we do NOT assume that all on-host destinations trust
2884 2885           * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2885 2886           * (Look for IXAF_TRUSTED_ICMP).
2886 2887           */
2887 2888          ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2888 2889  
2889 2890          ipha = (ipha_t *)mp->b_rptr;
2890 2891          mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2891 2892          *ipha = icmp_ipha;
2892 2893          ipha->ipha_src = src;
2893 2894          ipha->ipha_dst = dst;
2894 2895          ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2895 2896          msg_len += sizeof (icmp_ipha) + len;
2896 2897          if (msg_len > IP_MAXPACKET) {
2897 2898                  (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2898 2899                  msg_len = IP_MAXPACKET;
2899 2900          }
2900 2901          ipha->ipha_length = htons((uint16_t)msg_len);
2901 2902          icmph = (icmph_t *)&ipha[1];
2902 2903          bcopy(stuff, icmph, len);
2903 2904          icmph->icmph_checksum = 0;
2904 2905          icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2905 2906          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2906 2907  
2907 2908          (void) ip_output_simple(mp, &ixas);
2908 2909          ixa_cleanup(&ixas);
2909 2910  }
2910 2911  
2911 2912  /*
2912 2913   * Determine if an ICMP error packet can be sent given the rate limit.
2913 2914   * The limit consists of an average frequency (icmp_pkt_err_interval measured
2914 2915   * in milliseconds) and a burst size. Burst size number of packets can
2915 2916   * be sent arbitrarely closely spaced.
2916 2917   * The state is tracked using two variables to implement an approximate
2917 2918   * token bucket filter:
2918 2919   *      icmp_pkt_err_last - lbolt value when the last burst started
2919 2920   *      icmp_pkt_err_sent - number of packets sent in current burst
2920 2921   */
2921 2922  boolean_t
2922 2923  icmp_err_rate_limit(ip_stack_t *ipst)
2923 2924  {
2924 2925          clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2925 2926          uint_t refilled; /* Number of packets refilled in tbf since last */
2926 2927          /* Guard against changes by loading into local variable */
2927 2928          uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2928 2929  
2929 2930          if (err_interval == 0)
2930 2931                  return (B_FALSE);
2931 2932  
2932 2933          if (ipst->ips_icmp_pkt_err_last > now) {
2933 2934                  /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2934 2935                  ipst->ips_icmp_pkt_err_last = 0;
2935 2936                  ipst->ips_icmp_pkt_err_sent = 0;
2936 2937          }
2937 2938          /*
2938 2939           * If we are in a burst update the token bucket filter.
2939 2940           * Update the "last" time to be close to "now" but make sure
2940 2941           * we don't loose precision.
2941 2942           */
2942 2943          if (ipst->ips_icmp_pkt_err_sent != 0) {
2943 2944                  refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2944 2945                  if (refilled > ipst->ips_icmp_pkt_err_sent) {
2945 2946                          ipst->ips_icmp_pkt_err_sent = 0;
2946 2947                  } else {
2947 2948                          ipst->ips_icmp_pkt_err_sent -= refilled;
2948 2949                          ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2949 2950                  }
2950 2951          }
2951 2952          if (ipst->ips_icmp_pkt_err_sent == 0) {
2952 2953                  /* Start of new burst */
2953 2954                  ipst->ips_icmp_pkt_err_last = now;
2954 2955          }
2955 2956          if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2956 2957                  ipst->ips_icmp_pkt_err_sent++;
2957 2958                  ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2958 2959                      ipst->ips_icmp_pkt_err_sent));
2959 2960                  return (B_FALSE);
2960 2961          }
2961 2962          ip1dbg(("icmp_err_rate_limit: dropped\n"));
2962 2963          return (B_TRUE);
2963 2964  }
2964 2965  
2965 2966  /*
2966 2967   * Check if it is ok to send an IPv4 ICMP error packet in
2967 2968   * response to the IPv4 packet in mp.
2968 2969   * Free the message and return null if no
2969 2970   * ICMP error packet should be sent.
2970 2971   */
2971 2972  static mblk_t *
2972 2973  icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2973 2974  {
2974 2975          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
2975 2976          icmph_t *icmph;
2976 2977          ipha_t  *ipha;
2977 2978          uint_t  len_needed;
2978 2979  
2979 2980          if (!mp)
2980 2981                  return (NULL);
2981 2982          ipha = (ipha_t *)mp->b_rptr;
2982 2983          if (ip_csum_hdr(ipha)) {
2983 2984                  BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2984 2985                  ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2985 2986                  freemsg(mp);
2986 2987                  return (NULL);
2987 2988          }
2988 2989          if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2989 2990              ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2990 2991              CLASSD(ipha->ipha_dst) ||
2991 2992              CLASSD(ipha->ipha_src) ||
2992 2993              (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2993 2994                  /* Note: only errors to the fragment with offset 0 */
2994 2995                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2995 2996                  freemsg(mp);
2996 2997                  return (NULL);
2997 2998          }
2998 2999          if (ipha->ipha_protocol == IPPROTO_ICMP) {
2999 3000                  /*
3000 3001                   * Check the ICMP type.  RFC 1122 sez:  don't send ICMP
3001 3002                   * errors in response to any ICMP errors.
3002 3003                   */
3003 3004                  len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3004 3005                  if (mp->b_wptr - mp->b_rptr < len_needed) {
3005 3006                          if (!pullupmsg(mp, len_needed)) {
3006 3007                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3007 3008                                  freemsg(mp);
3008 3009                                  return (NULL);
3009 3010                          }
3010 3011                          ipha = (ipha_t *)mp->b_rptr;
3011 3012                  }
3012 3013                  icmph = (icmph_t *)
3013 3014                      (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3014 3015                  switch (icmph->icmph_type) {
3015 3016                  case ICMP_DEST_UNREACHABLE:
3016 3017                  case ICMP_SOURCE_QUENCH:
3017 3018                  case ICMP_TIME_EXCEEDED:
3018 3019                  case ICMP_PARAM_PROBLEM:
3019 3020                  case ICMP_REDIRECT:
3020 3021                          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3021 3022                          freemsg(mp);
3022 3023                          return (NULL);
3023 3024                  default:
3024 3025                          break;
3025 3026                  }
3026 3027          }
3027 3028          /*
3028 3029           * If this is a labeled system, then check to see if we're allowed to
3029 3030           * send a response to this particular sender.  If not, then just drop.
3030 3031           */
3031 3032          if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
3032 3033                  ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3033 3034                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3034 3035                  freemsg(mp);
3035 3036                  return (NULL);
3036 3037          }
3037 3038          if (icmp_err_rate_limit(ipst)) {
3038 3039                  /*
3039 3040                   * Only send ICMP error packets every so often.
3040 3041                   * This should be done on a per port/source basis,
3041 3042                   * but for now this will suffice.
3042 3043                   */
3043 3044                  freemsg(mp);
3044 3045                  return (NULL);
3045 3046          }
3046 3047          return (mp);
3047 3048  }
3048 3049  
3049 3050  /*
3050 3051   * Called when a packet was sent out the same link that it arrived on.
3051 3052   * Check if it is ok to send a redirect and then send it.
3052 3053   */
3053 3054  void
3054 3055  ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
3055 3056      ip_recv_attr_t *ira)
3056 3057  {
3057 3058          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
3058 3059          ipaddr_t        src, nhop;
3059 3060          mblk_t          *mp1;
3060 3061          ire_t           *nhop_ire;
3061 3062  
3062 3063          /*
3063 3064           * Check the source address to see if it originated
3064 3065           * on the same logical subnet it is going back out on.
3065 3066           * If so, we should be able to send it a redirect.
3066 3067           * Avoid sending a redirect if the destination
3067 3068           * is directly connected (i.e., we matched an IRE_ONLINK),
3068 3069           * or if the packet was source routed out this interface.
3069 3070           *
3070 3071           * We avoid sending a redirect if the
3071 3072           * destination is directly connected
3072 3073           * because it is possible that multiple
3073 3074           * IP subnets may have been configured on
3074 3075           * the link, and the source may not
3075 3076           * be on the same subnet as ip destination,
3076 3077           * even though they are on the same
3077 3078           * physical link.
3078 3079           */
3079 3080          if ((ire->ire_type & IRE_ONLINK) ||
3080 3081              ip_source_routed(ipha, ipst))
3081 3082                  return;
3082 3083  
3083 3084          nhop_ire = ire_nexthop(ire);
3084 3085          if (nhop_ire == NULL)
3085 3086                  return;
3086 3087  
3087 3088          nhop = nhop_ire->ire_addr;
3088 3089  
3089 3090          if (nhop_ire->ire_type & IRE_IF_CLONE) {
3090 3091                  ire_t   *ire2;
3091 3092  
3092 3093                  /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3093 3094                  mutex_enter(&nhop_ire->ire_lock);
3094 3095                  ire2 = nhop_ire->ire_dep_parent;
3095 3096                  if (ire2 != NULL)
3096 3097                          ire_refhold(ire2);
3097 3098                  mutex_exit(&nhop_ire->ire_lock);
3098 3099                  ire_refrele(nhop_ire);
3099 3100                  nhop_ire = ire2;
3100 3101          }
3101 3102          if (nhop_ire == NULL)
3102 3103                  return;
3103 3104  
3104 3105          ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3105 3106  
3106 3107          src = ipha->ipha_src;
3107 3108  
3108 3109          /*
3109 3110           * We look at the interface ire for the nexthop,
3110 3111           * to see if ipha_src is in the same subnet
3111 3112           * as the nexthop.
3112 3113           */
3113 3114          if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3114 3115                  /*
3115 3116                   * The source is directly connected.
3116 3117                   */
3117 3118                  mp1 = copymsg(mp);
3118 3119                  if (mp1 != NULL) {
3119 3120                          icmp_send_redirect(mp1, nhop, ira);
3120 3121                  }
3121 3122          }
3122 3123          ire_refrele(nhop_ire);
3123 3124  }
3124 3125  
3125 3126  /*
3126 3127   * Generate an ICMP redirect message.
3127 3128   */
3128 3129  static void
3129 3130  icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3130 3131  {
3131 3132          icmph_t icmph;
3132 3133          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3133 3134  
3134 3135          mp = icmp_pkt_err_ok(mp, ira);
3135 3136          if (mp == NULL)
3136 3137                  return;
3137 3138  
3138 3139          bzero(&icmph, sizeof (icmph_t));
3139 3140          icmph.icmph_type = ICMP_REDIRECT;
3140 3141          icmph.icmph_code = 1;
3141 3142          icmph.icmph_rd_gateway = gateway;
3142 3143          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3143 3144          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3144 3145  }
3145 3146  
3146 3147  /*
3147 3148   * Generate an ICMP time exceeded message.
3148 3149   */
3149 3150  void
3150 3151  icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3151 3152  {
3152 3153          icmph_t icmph;
3153 3154          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3154 3155  
3155 3156          mp = icmp_pkt_err_ok(mp, ira);
3156 3157          if (mp == NULL)
3157 3158                  return;
3158 3159  
3159 3160          bzero(&icmph, sizeof (icmph_t));
3160 3161          icmph.icmph_type = ICMP_TIME_EXCEEDED;
3161 3162          icmph.icmph_code = code;
3162 3163          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3163 3164          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3164 3165  }
3165 3166  
3166 3167  /*
3167 3168   * Generate an ICMP unreachable message.
3168 3169   * When called from ip_output side a minimal ip_recv_attr_t needs to be
3169 3170   * constructed by the caller.
3170 3171   */
3171 3172  void
3172 3173  icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3173 3174  {
3174 3175          icmph_t icmph;
3175 3176          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3176 3177  
3177 3178          mp = icmp_pkt_err_ok(mp, ira);
3178 3179          if (mp == NULL)
3179 3180                  return;
3180 3181  
3181 3182          bzero(&icmph, sizeof (icmph_t));
3182 3183          icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3183 3184          icmph.icmph_code = code;
3184 3185          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3185 3186          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3186 3187  }
3187 3188  
3188 3189  /*
3189 3190   * Latch in the IPsec state for a stream based the policy in the listener
3190 3191   * and the actions in the ip_recv_attr_t.
3191 3192   * Called directly from TCP and SCTP.
3192 3193   */
3193 3194  boolean_t
3194 3195  ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3195 3196  {
3196 3197          ASSERT(lconnp->conn_policy != NULL);
3197 3198          ASSERT(connp->conn_policy == NULL);
3198 3199  
3199 3200          IPPH_REFHOLD(lconnp->conn_policy);
3200 3201          connp->conn_policy = lconnp->conn_policy;
3201 3202  
3202 3203          if (ira->ira_ipsec_action != NULL) {
3203 3204                  if (connp->conn_latch == NULL) {
3204 3205                          connp->conn_latch = iplatch_create();
3205 3206                          if (connp->conn_latch == NULL)
3206 3207                                  return (B_FALSE);
3207 3208                  }
3208 3209                  ipsec_latch_inbound(connp, ira);
3209 3210          }
3210 3211          return (B_TRUE);
3211 3212  }
3212 3213  
3213 3214  /*
3214 3215   * Verify whether or not the IP address is a valid local address.
3215 3216   * Could be a unicast, including one for a down interface.
3216 3217   * If allow_mcbc then a multicast or broadcast address is also
3217 3218   * acceptable.
3218 3219   *
3219 3220   * In the case of a broadcast/multicast address, however, the
3220 3221   * upper protocol is expected to reset the src address
3221 3222   * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3222 3223   * no packets are emitted with broadcast/multicast address as
3223 3224   * source address (that violates hosts requirements RFC 1122)
3224 3225   * The addresses valid for bind are:
3225 3226   *      (1) - INADDR_ANY (0)
3226 3227   *      (2) - IP address of an UP interface
3227 3228   *      (3) - IP address of a DOWN interface
3228 3229   *      (4) - valid local IP broadcast addresses. In this case
3229 3230   *      the conn will only receive packets destined to
3230 3231   *      the specified broadcast address.
3231 3232   *      (5) - a multicast address. In this case
3232 3233   *      the conn will only receive packets destined to
3233 3234   *      the specified multicast address. Note: the
3234 3235   *      application still has to issue an
3235 3236   *      IP_ADD_MEMBERSHIP socket option.
3236 3237   *
3237 3238   * In all the above cases, the bound address must be valid in the current zone.
3238 3239   * When the address is loopback, multicast or broadcast, there might be many
3239 3240   * matching IREs so bind has to look up based on the zone.
3240 3241   */
3241 3242  ip_laddr_t
3242 3243  ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3243 3244      ip_stack_t *ipst, boolean_t allow_mcbc)
3244 3245  {
3245 3246          ire_t *src_ire;
3246 3247  
3247 3248          ASSERT(src_addr != INADDR_ANY);
3248 3249  
3249 3250          src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3250 3251              NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3251 3252  
3252 3253          /*
3253 3254           * If an address other than in6addr_any is requested,
3254 3255           * we verify that it is a valid address for bind
3255 3256           * Note: Following code is in if-else-if form for
3256 3257           * readability compared to a condition check.
3257 3258           */
3258 3259          if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3259 3260                  /*
3260 3261                   * (2) Bind to address of local UP interface
3261 3262                   */
3262 3263                  ire_refrele(src_ire);
3263 3264                  return (IPVL_UNICAST_UP);
3264 3265          } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3265 3266                  /*
3266 3267                   * (4) Bind to broadcast address
3267 3268                   */
3268 3269                  ire_refrele(src_ire);
3269 3270                  if (allow_mcbc)
3270 3271                          return (IPVL_BCAST);
3271 3272                  else
3272 3273                          return (IPVL_BAD);
3273 3274          } else if (CLASSD(src_addr)) {
3274 3275                  /* (5) bind to multicast address. */
3275 3276                  if (src_ire != NULL)
3276 3277                          ire_refrele(src_ire);
3277 3278  
3278 3279                  if (allow_mcbc)
3279 3280                          return (IPVL_MCAST);
3280 3281                  else
3281 3282                          return (IPVL_BAD);
3282 3283          } else {
3283 3284                  ipif_t *ipif;
3284 3285  
3285 3286                  /*
3286 3287                   * (3) Bind to address of local DOWN interface?
3287 3288                   * (ipif_lookup_addr() looks up all interfaces
3288 3289                   * but we do not get here for UP interfaces
3289 3290                   * - case (2) above)
3290 3291                   */
3291 3292                  if (src_ire != NULL)
3292 3293                          ire_refrele(src_ire);
3293 3294  
3294 3295                  ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3295 3296                  if (ipif == NULL)
3296 3297                          return (IPVL_BAD);
3297 3298  
3298 3299                  /* Not a useful source? */
3299 3300                  if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3300 3301                          ipif_refrele(ipif);
3301 3302                          return (IPVL_BAD);
3302 3303                  }
3303 3304                  ipif_refrele(ipif);
3304 3305                  return (IPVL_UNICAST_DOWN);
3305 3306          }
3306 3307  }
3307 3308  
3308 3309  /*
3309 3310   * Insert in the bind fanout for IPv4 and IPv6.
3310 3311   * The caller should already have used ip_laddr_verify_v*() before calling
3311 3312   * this.
3312 3313   */
3313 3314  int
3314 3315  ip_laddr_fanout_insert(conn_t *connp)
3315 3316  {
3316 3317          int             error;
3317 3318  
3318 3319          /*
3319 3320           * Allow setting new policies. For example, disconnects result
3320 3321           * in us being called. As we would have set conn_policy_cached
3321 3322           * to B_TRUE before, we should set it to B_FALSE, so that policy
3322 3323           * can change after the disconnect.
3323 3324           */
3324 3325          connp->conn_policy_cached = B_FALSE;
3325 3326  
3326 3327          error = ipcl_bind_insert(connp);
3327 3328          if (error != 0) {
3328 3329                  if (connp->conn_anon_port) {
3329 3330                          (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
3330 3331                              connp->conn_mlp_type, connp->conn_proto,
3331 3332                              ntohs(connp->conn_lport), B_FALSE);
3332 3333                  }
3333 3334                  connp->conn_mlp_type = mlptSingle;
3334 3335          }
3335 3336          return (error);
3336 3337  }
3337 3338  
3338 3339  /*
3339 3340   * Verify that both the source and destination addresses are valid. If
3340 3341   * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3341 3342   * i.e. have no route to it.  Protocols like TCP want to verify destination
3342 3343   * reachability, while tunnels do not.
3343 3344   *
3344 3345   * Determine the route, the interface, and (optionally) the source address
3345 3346   * to use to reach a given destination.
3346 3347   * Note that we allow connect to broadcast and multicast addresses when
3347 3348   * IPDF_ALLOW_MCBC is set.
3348 3349   * first_hop and dst_addr are normally the same, but if source routing
3349 3350   * they will differ; in that case the first_hop is what we'll use for the
3350 3351   * routing lookup but the dce and label checks will be done on dst_addr,
3351 3352   *
3352 3353   * If uinfo is set, then we fill in the best available information
3353 3354   * we have for the destination. This is based on (in priority order) any
3354 3355   * metrics and path MTU stored in a dce_t, route metrics, and finally the
3355 3356   * ill_mtu/ill_mc_mtu.
3356 3357   *
3357 3358   * Tsol note: If we have a source route then dst_addr != firsthop. But we
3358 3359   * always do the label check on dst_addr.
3359 3360   */
3360 3361  int
3361 3362  ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3362 3363      ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode)
3363 3364  {
3364 3365          ire_t           *ire = NULL;
3365 3366          int             error = 0;
3366 3367          ipaddr_t        setsrc;                         /* RTF_SETSRC */
3367 3368          zoneid_t        zoneid = ixa->ixa_zoneid;       /* Honors SO_ALLZONES */
3368 3369          ip_stack_t      *ipst = ixa->ixa_ipst;
3369 3370          dce_t           *dce;
3370 3371          uint_t          pmtu;
3371 3372          uint_t          generation;
3372 3373          nce_t           *nce;
3373 3374          ill_t           *ill = NULL;
3374 3375          boolean_t       multirt = B_FALSE;
3375 3376  
3376 3377          ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3377 3378  
3378 3379          /*
3379 3380           * We never send to zero; the ULPs map it to the loopback address.
3380 3381           * We can't allow it since we use zero to mean unitialized in some
3381 3382           * places.
3382 3383           */
3383 3384          ASSERT(dst_addr != INADDR_ANY);
3384 3385  
3385 3386          if (is_system_labeled()) {
3386 3387                  ts_label_t *tsl = NULL;
3387 3388  
3388 3389                  error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION,
3389 3390                      mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl);
3390 3391                  if (error != 0)
3391 3392                          return (error);
3392 3393                  if (tsl != NULL) {
3393 3394                          /* Update the label */
3394 3395                          ip_xmit_attr_replace_tsl(ixa, tsl);
3395 3396                  }
3396 3397          }
3397 3398  
3398 3399          setsrc = INADDR_ANY;
3399 3400          /*
3400 3401           * Select a route; For IPMP interfaces, we would only select
3401 3402           * a "hidden" route (i.e., going through a specific under_ill)
3402 3403           * if ixa_ifindex has been specified.
3403 3404           */
3404 3405          ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3405 3406              &generation, &setsrc, &error, &multirt);
3406 3407          ASSERT(ire != NULL);    /* IRE_NOROUTE if none found */
3407 3408          if (error != 0)
3408 3409                  goto bad_addr;
3409 3410  
3410 3411          /*
3411 3412           * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3412 3413           * If IPDF_VERIFY_DST is set, the destination must be reachable;
3413 3414           * Otherwise the destination needn't be reachable.
3414 3415           *
3415 3416           * If we match on a reject or black hole, then we've got a
3416 3417           * local failure.  May as well fail out the connect() attempt,
3417 3418           * since it's never going to succeed.
3418 3419           */
3419 3420          if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3420 3421                  /*
3421 3422                   * If we're verifying destination reachability, we always want
3422 3423                   * to complain here.
3423 3424                   *
3424 3425                   * If we're not verifying destination reachability but the
3425 3426                   * destination has a route, we still want to fail on the
3426 3427                   * temporary address and broadcast address tests.
3427 3428                   *
3428 3429                   * In both cases do we let the code continue so some reasonable
3429 3430                   * information is returned to the caller. That enables the
3430 3431                   * caller to use (and even cache) the IRE. conn_ip_ouput will
3431 3432                   * use the generation mismatch path to check for the unreachable
3432 3433                   * case thereby avoiding any specific check in the main path.
3433 3434                   */
3434 3435                  ASSERT(generation == IRE_GENERATION_VERIFY);
3435 3436                  if (flags & IPDF_VERIFY_DST) {
3436 3437                          /*
3437 3438                           * Set errno but continue to set up ixa_ire to be
3438 3439                           * the RTF_REJECT|RTF_BLACKHOLE IRE.
3439 3440                           * That allows callers to use ip_output to get an
3440 3441                           * ICMP error back.
3441 3442                           */
3442 3443                          if (!(ire->ire_type & IRE_HOST))
3443 3444                                  error = ENETUNREACH;
3444 3445                          else
3445 3446                                  error = EHOSTUNREACH;
3446 3447                  }
3447 3448          }
3448 3449  
3449 3450          if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3450 3451              !(flags & IPDF_ALLOW_MCBC)) {
3451 3452                  ire_refrele(ire);
3452 3453                  ire = ire_reject(ipst, B_FALSE);
3453 3454                  generation = IRE_GENERATION_VERIFY;
3454 3455                  error = ENETUNREACH;
3455 3456          }
3456 3457  
3457 3458          /* Cache things */
3458 3459          if (ixa->ixa_ire != NULL)
3459 3460                  ire_refrele_notr(ixa->ixa_ire);
3460 3461  #ifdef DEBUG
3461 3462          ire_refhold_notr(ire);
3462 3463          ire_refrele(ire);
3463 3464  #endif
3464 3465          ixa->ixa_ire = ire;
3465 3466          ixa->ixa_ire_generation = generation;
3466 3467  
3467 3468          /*
3468 3469           * Ensure that ixa_dce is always set any time that ixa_ire is set,
3469 3470           * since some callers will send a packet to conn_ip_output() even if
3470 3471           * there's an error.
3471 3472           */
3472 3473          if (flags & IPDF_UNIQUE_DCE) {
3473 3474                  /* Fallback to the default dce if allocation fails */
3474 3475                  dce = dce_lookup_and_add_v4(dst_addr, ipst);
3475 3476                  if (dce != NULL)
3476 3477                          generation = dce->dce_generation;
3477 3478                  else
3478 3479                          dce = dce_lookup_v4(dst_addr, ipst, &generation);
3479 3480          } else {
3480 3481                  dce = dce_lookup_v4(dst_addr, ipst, &generation);
3481 3482          }
3482 3483          ASSERT(dce != NULL);
3483 3484          if (ixa->ixa_dce != NULL)
3484 3485                  dce_refrele_notr(ixa->ixa_dce);
3485 3486  #ifdef DEBUG
3486 3487          dce_refhold_notr(dce);
3487 3488          dce_refrele(dce);
3488 3489  #endif
3489 3490          ixa->ixa_dce = dce;
3490 3491          ixa->ixa_dce_generation = generation;
3491 3492  
3492 3493          /*
3493 3494           * For multicast with multirt we have a flag passed back from
3494 3495           * ire_lookup_multi_ill_v4 since we don't have an IRE for each
3495 3496           * possible multicast address.
3496 3497           * We also need a flag for multicast since we can't check
3497 3498           * whether RTF_MULTIRT is set in ixa_ire for multicast.
3498 3499           */
3499 3500          if (multirt) {
3500 3501                  ixa->ixa_postfragfn = ip_postfrag_multirt_v4;
3501 3502                  ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST;
3502 3503          } else {
3503 3504                  ixa->ixa_postfragfn = ire->ire_postfragfn;
3504 3505                  ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST;
3505 3506          }
3506 3507          if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3507 3508                  /* Get an nce to cache. */
3508 3509                  nce = ire_to_nce(ire, firsthop, NULL);
3509 3510                  if (nce == NULL) {
3510 3511                          /* Allocation failure? */
3511 3512                          ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3512 3513                  } else {
3513 3514                          if (ixa->ixa_nce != NULL)
3514 3515                                  nce_refrele(ixa->ixa_nce);
3515 3516                          ixa->ixa_nce = nce;
3516 3517                  }
3517 3518          }
3518 3519  
3519 3520          /*
3520 3521           * If the source address is a loopback address, the
3521 3522           * destination had best be local or multicast.
3522 3523           * If we are sending to an IRE_LOCAL using a loopback source then
3523 3524           * it had better be the same zoneid.
3524 3525           */
3525 3526          if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3526 3527                  if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3527 3528                          ire = NULL;     /* Stored in ixa_ire */
3528 3529                          error = EADDRNOTAVAIL;
3529 3530                          goto bad_addr;
3530 3531                  }
3531 3532                  if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3532 3533                          ire = NULL;     /* Stored in ixa_ire */
3533 3534                          error = EADDRNOTAVAIL;
3534 3535                          goto bad_addr;
3535 3536                  }
3536 3537          }
3537 3538          if (ire->ire_type & IRE_BROADCAST) {
3538 3539                  /*
3539 3540                   * If the ULP didn't have a specified source, then we
3540 3541                   * make sure we reselect the source when sending
3541 3542                   * broadcasts out different interfaces.
3542 3543                   */
3543 3544                  if (flags & IPDF_SELECT_SRC)
3544 3545                          ixa->ixa_flags |= IXAF_SET_SOURCE;
3545 3546                  else
3546 3547                          ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3547 3548          }
3548 3549  
3549 3550          /*
3550 3551           * Does the caller want us to pick a source address?
3551 3552           */
3552 3553          if (flags & IPDF_SELECT_SRC) {
3553 3554                  ipaddr_t        src_addr;
3554 3555  
3555 3556                  /*
3556 3557                   * We use use ire_nexthop_ill to avoid the under ipmp
3557 3558                   * interface for source address selection. Note that for ipmp
3558 3559                   * probe packets, ixa_ifindex would have been specified, and
3559 3560                   * the ip_select_route() invocation would have picked an ire
3560 3561                   * will ire_ill pointing at an under interface.
3561 3562                   */
3562 3563                  ill = ire_nexthop_ill(ire);
3563 3564  
3564 3565                  /* If unreachable we have no ill but need some source */
3565 3566                  if (ill == NULL) {
3566 3567                          src_addr = htonl(INADDR_LOOPBACK);
3567 3568                          /* Make sure we look for a better source address */
3568 3569                          generation = SRC_GENERATION_VERIFY;
3569 3570                  } else {
3570 3571                          error = ip_select_source_v4(ill, setsrc, dst_addr,
3571 3572                              ixa->ixa_multicast_ifaddr, zoneid,
3572 3573                              ipst, &src_addr, &generation, NULL);
3573 3574                          if (error != 0) {
3574 3575                                  ire = NULL;     /* Stored in ixa_ire */
3575 3576                                  goto bad_addr;
3576 3577                          }
3577 3578                  }
3578 3579  
3579 3580                  /*
3580 3581                   * We allow the source address to to down.
3581 3582                   * However, we check that we don't use the loopback address
3582 3583                   * as a source when sending out on the wire.
3583 3584                   */
3584 3585                  if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3585 3586                      !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3586 3587                      !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3587 3588                          ire = NULL;     /* Stored in ixa_ire */
3588 3589                          error = EADDRNOTAVAIL;
3589 3590                          goto bad_addr;
3590 3591                  }
3591 3592  
3592 3593                  *src_addrp = src_addr;
3593 3594                  ixa->ixa_src_generation = generation;
3594 3595          }
3595 3596  
3596 3597          /*
3597 3598           * Make sure we don't leave an unreachable ixa_nce in place
3598 3599           * since ip_select_route is used when we unplumb i.e., remove
3599 3600           * references on ixa_ire, ixa_nce, and ixa_dce.
3600 3601           */
3601 3602          nce = ixa->ixa_nce;
3602 3603          if (nce != NULL && nce->nce_is_condemned) {
3603 3604                  nce_refrele(nce);
3604 3605                  ixa->ixa_nce = NULL;
3605 3606                  ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3606 3607          }
3607 3608  
3608 3609          /*
3609 3610           * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3610 3611           * However, we can't do it for IPv4 multicast or broadcast.
3611 3612           */
3612 3613          if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3613 3614                  ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3614 3615  
3615 3616          /*
3616 3617           * Set initial value for fragmentation limit. Either conn_ip_output
3617 3618           * or ULP might updates it when there are routing changes.
3618 3619           * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3619 3620           */
3620 3621          pmtu = ip_get_pmtu(ixa);
3621 3622          ixa->ixa_fragsize = pmtu;
3622 3623          /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3623 3624          if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3624 3625                  ixa->ixa_pmtu = pmtu;
3625 3626  
3626 3627          /*
3627 3628           * Extract information useful for some transports.
3628 3629           * First we look for DCE metrics. Then we take what we have in
3629 3630           * the metrics in the route, where the offlink is used if we have
3630 3631           * one.
3631 3632           */
3632 3633          if (uinfo != NULL) {
3633 3634                  bzero(uinfo, sizeof (*uinfo));
3634 3635  
3635 3636                  if (dce->dce_flags & DCEF_UINFO)
3636 3637                          *uinfo = dce->dce_uinfo;
3637 3638  
3638 3639                  rts_merge_metrics(uinfo, &ire->ire_metrics);
3639 3640  
3640 3641                  /* Allow ire_metrics to decrease the path MTU from above */
3641 3642                  if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3642 3643                          uinfo->iulp_mtu = pmtu;
3643 3644  
3644 3645                  uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3645 3646                  uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3646 3647                  uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3647 3648          }
3648 3649  
3649 3650          if (ill != NULL)
3650 3651                  ill_refrele(ill);
3651 3652  
3652 3653          return (error);
3653 3654  
3654 3655  bad_addr:
3655 3656          if (ire != NULL)
3656 3657                  ire_refrele(ire);
3657 3658  
3658 3659          if (ill != NULL)
3659 3660                  ill_refrele(ill);
3660 3661  
3661 3662          /*
3662 3663           * Make sure we don't leave an unreachable ixa_nce in place
3663 3664           * since ip_select_route is used when we unplumb i.e., remove
3664 3665           * references on ixa_ire, ixa_nce, and ixa_dce.
3665 3666           */
3666 3667          nce = ixa->ixa_nce;
3667 3668          if (nce != NULL && nce->nce_is_condemned) {
3668 3669                  nce_refrele(nce);
3669 3670                  ixa->ixa_nce = NULL;
3670 3671                  ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3671 3672          }
3672 3673  
3673 3674          return (error);
3674 3675  }
3675 3676  
3676 3677  
3677 3678  /*
3678 3679   * Get the base MTU for the case when path MTU discovery is not used.
3679 3680   * Takes the MTU of the IRE into account.
3680 3681   */
3681 3682  uint_t
3682 3683  ip_get_base_mtu(ill_t *ill, ire_t *ire)
3683 3684  {
3684 3685          uint_t mtu;
3685 3686          uint_t iremtu = ire->ire_metrics.iulp_mtu;
3686 3687  
3687 3688          if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3688 3689                  mtu = ill->ill_mc_mtu;
3689 3690          else
3690 3691                  mtu = ill->ill_mtu;
3691 3692  
3692 3693          if (iremtu != 0 && iremtu < mtu)
3693 3694                  mtu = iremtu;
3694 3695  
3695 3696          return (mtu);
3696 3697  }
3697 3698  
3698 3699  /*
3699 3700   * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3700 3701   * Assumes that ixa_ire, dce, and nce have already been set up.
3701 3702   *
3702 3703   * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3703 3704   * We avoid path MTU discovery if it is disabled with ndd.
3704 3705   * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3705 3706   *
3706 3707   * NOTE: We also used to turn it off for source routed packets. That
3707 3708   * is no longer required since the dce is per final destination.
3708 3709   */
3709 3710  uint_t
3710 3711  ip_get_pmtu(ip_xmit_attr_t *ixa)
3711 3712  {
3712 3713          ip_stack_t      *ipst = ixa->ixa_ipst;
3713 3714          dce_t           *dce;
3714 3715          nce_t           *nce;
3715 3716          ire_t           *ire;
3716 3717          uint_t          pmtu;
3717 3718  
3718 3719          ire = ixa->ixa_ire;
3719 3720          dce = ixa->ixa_dce;
3720 3721          nce = ixa->ixa_nce;
3721 3722  
3722 3723          /*
3723 3724           * If path MTU discovery has been turned off by ndd, then we ignore
3724 3725           * any dce_pmtu and for IPv4 we will not set DF.
3725 3726           */
3726 3727          if (!ipst->ips_ip_path_mtu_discovery)
3727 3728                  ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3728 3729  
3729 3730          pmtu = IP_MAXPACKET;
3730 3731          /*
3731 3732           * Decide whether whether IPv4 sets DF
3732 3733           * For IPv6 "no DF" means to use the 1280 mtu
3733 3734           */
3734 3735          if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3735 3736                  ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3736 3737          } else {
3737 3738                  ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3738 3739                  if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3739 3740                          pmtu = IPV6_MIN_MTU;
3740 3741          }
3741 3742  
3742 3743          /* Check if the PMTU is to old before we use it */
3743 3744          if ((dce->dce_flags & DCEF_PMTU) &&
3744 3745              TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3745 3746              ipst->ips_ip_pathmtu_interval) {
3746 3747                  /*
3747 3748                   * Older than 20 minutes. Drop the path MTU information.
3748 3749                   */
3749 3750                  mutex_enter(&dce->dce_lock);
3750 3751                  dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3751 3752                  dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3752 3753                  mutex_exit(&dce->dce_lock);
3753 3754                  dce_increment_generation(dce);
3754 3755          }
3755 3756  
3756 3757          /* The metrics on the route can lower the path MTU */
3757 3758          if (ire->ire_metrics.iulp_mtu != 0 &&
3758 3759              ire->ire_metrics.iulp_mtu < pmtu)
3759 3760                  pmtu = ire->ire_metrics.iulp_mtu;
3760 3761  
3761 3762          /*
3762 3763           * If the path MTU is smaller than some minimum, we still use dce_pmtu
3763 3764           * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3764 3765           * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3765 3766           */
3766 3767          if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3767 3768                  if (dce->dce_flags & DCEF_PMTU) {
3768 3769                          if (dce->dce_pmtu < pmtu)
3769 3770                                  pmtu = dce->dce_pmtu;
3770 3771  
3771 3772                          if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3772 3773                                  ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3773 3774                                  ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3774 3775                          } else {
3775 3776                                  ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3776 3777                                  ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3777 3778                          }
3778 3779                  } else {
3779 3780                          ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3780 3781                          ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3781 3782                  }
3782 3783          }
3783 3784  
3784 3785          /*
3785 3786           * If we have an IRE_LOCAL we use the loopback mtu instead of
3786 3787           * the ill for going out the wire i.e., IRE_LOCAL gets the same
3787 3788           * mtu as IRE_LOOPBACK.
3788 3789           */
3789 3790          if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3790 3791                  uint_t loopback_mtu;
3791 3792  
3792 3793                  loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3793 3794                      ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3794 3795  
3795 3796                  if (loopback_mtu < pmtu)
3796 3797                          pmtu = loopback_mtu;
3797 3798          } else if (nce != NULL) {
3798 3799                  /*
3799 3800                   * Make sure we don't exceed the interface MTU.
3800 3801                   * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3801 3802                   * an ill. We'd use the above IP_MAXPACKET in that case just
3802 3803                   * to tell the transport something larger than zero.
3803 3804                   */
3804 3805                  if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3805 3806                          if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3806 3807                                  pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3807 3808                          if (nce->nce_common->ncec_ill != nce->nce_ill &&
3808 3809                              nce->nce_ill->ill_mc_mtu < pmtu) {
3809 3810                                  /*
3810 3811                                   * for interfaces in an IPMP group, the mtu of
3811 3812                                   * the nce_ill (under_ill) could be different
3812 3813                                   * from the mtu of the ncec_ill, so we take the
3813 3814                                   * min of the two.
3814 3815                                   */
3815 3816                                  pmtu = nce->nce_ill->ill_mc_mtu;
3816 3817                          }
3817 3818                  } else {
3818 3819                          if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3819 3820                                  pmtu = nce->nce_common->ncec_ill->ill_mtu;
3820 3821                          if (nce->nce_common->ncec_ill != nce->nce_ill &&
3821 3822                              nce->nce_ill->ill_mtu < pmtu) {
3822 3823                                  /*
3823 3824                                   * for interfaces in an IPMP group, the mtu of
3824 3825                                   * the nce_ill (under_ill) could be different
3825 3826                                   * from the mtu of the ncec_ill, so we take the
3826 3827                                   * min of the two.
3827 3828                                   */
3828 3829                                  pmtu = nce->nce_ill->ill_mtu;
3829 3830                          }
3830 3831                  }
3831 3832          }
3832 3833  
3833 3834          /*
3834 3835           * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3835 3836           * Only applies to IPv6.
3836 3837           */
3837 3838          if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3838 3839                  if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3839 3840                          switch (ixa->ixa_use_min_mtu) {
3840 3841                          case IPV6_USE_MIN_MTU_MULTICAST:
3841 3842                                  if (ire->ire_type & IRE_MULTICAST)
3842 3843                                          pmtu = IPV6_MIN_MTU;
3843 3844                                  break;
3844 3845                          case IPV6_USE_MIN_MTU_ALWAYS:
3845 3846                                  pmtu = IPV6_MIN_MTU;
3846 3847                                  break;
3847 3848                          case IPV6_USE_MIN_MTU_NEVER:
3848 3849                                  break;
3849 3850                          }
3850 3851                  } else {
3851 3852                          /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3852 3853                          if (ire->ire_type & IRE_MULTICAST)
3853 3854                                  pmtu = IPV6_MIN_MTU;
3854 3855                  }
3855 3856          }
3856 3857  
3857 3858          /*
3858 3859           * After receiving an ICMPv6 "packet too big" message with a
3859 3860           * MTU < 1280, and for multirouted IPv6 packets, the IP layer
3860 3861           * will insert a 8-byte fragment header in every packet. We compensate
3861 3862           * for those cases by returning a smaller path MTU to the ULP.
3862 3863           *
3863 3864           * In the case of CGTP then ip_output will add a fragment header.
3864 3865           * Make sure there is room for it by telling a smaller number
3865 3866           * to the transport.
3866 3867           *
3867 3868           * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here
3868 3869           * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu()
3869 3870           * which is the size of the packets it can send.
3870 3871           */
3871 3872          if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3872 3873                  if ((dce->dce_flags & DCEF_TOO_SMALL_PMTU) ||
3873 3874                      (ire->ire_flags & RTF_MULTIRT) ||
3874 3875                      (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) {
3875 3876                          pmtu -= sizeof (ip6_frag_t);
3876 3877                          ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR;
3877 3878                  }
3878 3879          }
3879 3880  
3880 3881          return (pmtu);
3881 3882  }
3882 3883  
3883 3884  /*
3884 3885   * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3885 3886   * the final piece where we don't.  Return a pointer to the first mblk in the
3886 3887   * result, and update the pointer to the next mblk to chew on.  If anything
3887 3888   * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3888 3889   * NULL pointer.
3889 3890   */
3890 3891  mblk_t *
3891 3892  ip_carve_mp(mblk_t **mpp, ssize_t len)
3892 3893  {
3893 3894          mblk_t  *mp0;
3894 3895          mblk_t  *mp1;
3895 3896          mblk_t  *mp2;
3896 3897  
3897 3898          if (!len || !mpp || !(mp0 = *mpp))
3898 3899                  return (NULL);
3899 3900          /* If we aren't going to consume the first mblk, we need a dup. */
3900 3901          if (mp0->b_wptr - mp0->b_rptr > len) {
3901 3902                  mp1 = dupb(mp0);
3902 3903                  if (mp1) {
3903 3904                          /* Partition the data between the two mblks. */
3904 3905                          mp1->b_wptr = mp1->b_rptr + len;
3905 3906                          mp0->b_rptr = mp1->b_wptr;
3906 3907                          /*
3907 3908                           * after adjustments if mblk not consumed is now
3908 3909                           * unaligned, try to align it. If this fails free
3909 3910                           * all messages and let upper layer recover.
3910 3911                           */
3911 3912                          if (!OK_32PTR(mp0->b_rptr)) {
3912 3913                                  if (!pullupmsg(mp0, -1)) {
3913 3914                                          freemsg(mp0);
3914 3915                                          freemsg(mp1);
3915 3916                                          *mpp = NULL;
3916 3917                                          return (NULL);
3917 3918                                  }
3918 3919                          }
3919 3920                  }
3920 3921                  return (mp1);
3921 3922          }
3922 3923          /* Eat through as many mblks as we need to get len bytes. */
3923 3924          len -= mp0->b_wptr - mp0->b_rptr;
3924 3925          for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3925 3926                  if (mp2->b_wptr - mp2->b_rptr > len) {
3926 3927                          /*
3927 3928                           * We won't consume the entire last mblk.  Like
3928 3929                           * above, dup and partition it.
3929 3930                           */
3930 3931                          mp1->b_cont = dupb(mp2);
3931 3932                          mp1 = mp1->b_cont;
3932 3933                          if (!mp1) {
3933 3934                                  /*
3934 3935                                   * Trouble.  Rather than go to a lot of
3935 3936                                   * trouble to clean up, we free the messages.
3936 3937                                   * This won't be any worse than losing it on
3937 3938                                   * the wire.
3938 3939                                   */
3939 3940                                  freemsg(mp0);
3940 3941                                  freemsg(mp2);
3941 3942                                  *mpp = NULL;
3942 3943                                  return (NULL);
3943 3944                          }
3944 3945                          mp1->b_wptr = mp1->b_rptr + len;
3945 3946                          mp2->b_rptr = mp1->b_wptr;
3946 3947                          /*
3947 3948                           * after adjustments if mblk not consumed is now
3948 3949                           * unaligned, try to align it. If this fails free
3949 3950                           * all messages and let upper layer recover.
3950 3951                           */
3951 3952                          if (!OK_32PTR(mp2->b_rptr)) {
3952 3953                                  if (!pullupmsg(mp2, -1)) {
3953 3954                                          freemsg(mp0);
3954 3955                                          freemsg(mp2);
3955 3956                                          *mpp = NULL;
3956 3957                                          return (NULL);
3957 3958                                  }
3958 3959                          }
3959 3960                          *mpp = mp2;
3960 3961                          return (mp0);
3961 3962                  }
3962 3963                  /* Decrement len by the amount we just got. */
3963 3964                  len -= mp2->b_wptr - mp2->b_rptr;
3964 3965          }
3965 3966          /*
3966 3967           * len should be reduced to zero now.  If not our caller has
3967 3968           * screwed up.
3968 3969           */
3969 3970          if (len) {
3970 3971                  /* Shouldn't happen! */
3971 3972                  freemsg(mp0);
3972 3973                  *mpp = NULL;
3973 3974                  return (NULL);
3974 3975          }
3975 3976          /*
3976 3977           * We consumed up to exactly the end of an mblk.  Detach the part
3977 3978           * we are returning from the rest of the chain.
3978 3979           */
3979 3980          mp1->b_cont = NULL;
3980 3981          *mpp = mp2;
3981 3982          return (mp0);
3982 3983  }
3983 3984  
3984 3985  /* The ill stream is being unplumbed. Called from ip_close */
3985 3986  int
3986 3987  ip_modclose(ill_t *ill)
3987 3988  {
3988 3989          boolean_t success;
3989 3990          ipsq_t  *ipsq;
3990 3991          ipif_t  *ipif;
3991 3992          queue_t *q = ill->ill_rq;
3992 3993          ip_stack_t      *ipst = ill->ill_ipst;
3993 3994          int     i;
3994 3995          arl_ill_common_t *ai = ill->ill_common;
3995 3996  
3996 3997          /*
3997 3998           * The punlink prior to this may have initiated a capability
3998 3999           * negotiation. But ipsq_enter will block until that finishes or
3999 4000           * times out.
4000 4001           */
4001 4002          success = ipsq_enter(ill, B_FALSE, NEW_OP);
4002 4003  
4003 4004          /*
4004 4005           * Open/close/push/pop is guaranteed to be single threaded
4005 4006           * per stream by STREAMS. FS guarantees that all references
4006 4007           * from top are gone before close is called. So there can't
4007 4008           * be another close thread that has set CONDEMNED on this ill.
4008 4009           * and cause ipsq_enter to return failure.
4009 4010           */
4010 4011          ASSERT(success);
4011 4012          ipsq = ill->ill_phyint->phyint_ipsq;
4012 4013  
4013 4014          /*
4014 4015           * Mark it condemned. No new reference will be made to this ill.
4015 4016           * Lookup functions will return an error. Threads that try to
4016 4017           * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
4017 4018           * that the refcnt will drop down to zero.
4018 4019           */
4019 4020          mutex_enter(&ill->ill_lock);
4020 4021          ill->ill_state_flags |= ILL_CONDEMNED;
4021 4022          for (ipif = ill->ill_ipif; ipif != NULL;
4022 4023              ipif = ipif->ipif_next) {
4023 4024                  ipif->ipif_state_flags |= IPIF_CONDEMNED;
4024 4025          }
4025 4026          /*
4026 4027           * Wake up anybody waiting to enter the ipsq. ipsq_enter
4027 4028           * returns  error if ILL_CONDEMNED is set
4028 4029           */
4029 4030          cv_broadcast(&ill->ill_cv);
4030 4031          mutex_exit(&ill->ill_lock);
4031 4032  
4032 4033          /*
4033 4034           * Send all the deferred DLPI messages downstream which came in
4034 4035           * during the small window right before ipsq_enter(). We do this
4035 4036           * without waiting for the ACKs because all the ACKs for M_PROTO
4036 4037           * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
4037 4038           */
4038 4039          ill_dlpi_send_deferred(ill);
4039 4040  
4040 4041          /*
4041 4042           * Shut down fragmentation reassembly.
4042 4043           * ill_frag_timer won't start a timer again.
4043 4044           * Now cancel any existing timer
4044 4045           */
4045 4046          (void) untimeout(ill->ill_frag_timer_id);
4046 4047          (void) ill_frag_timeout(ill, 0);
4047 4048  
4048 4049          /*
4049 4050           * Call ill_delete to bring down the ipifs, ilms and ill on
4050 4051           * this ill. Then wait for the refcnts to drop to zero.
4051 4052           * ill_is_freeable checks whether the ill is really quiescent.
4052 4053           * Then make sure that threads that are waiting to enter the
4053 4054           * ipsq have seen the error returned by ipsq_enter and have
4054 4055           * gone away. Then we call ill_delete_tail which does the
4055 4056           * DL_UNBIND_REQ with the driver and then qprocsoff.
4056 4057           */
4057 4058          ill_delete(ill);
4058 4059          mutex_enter(&ill->ill_lock);
4059 4060          while (!ill_is_freeable(ill))
4060 4061                  cv_wait(&ill->ill_cv, &ill->ill_lock);
4061 4062  
4062 4063          while (ill->ill_waiters)
4063 4064                  cv_wait(&ill->ill_cv, &ill->ill_lock);
4064 4065  
4065 4066          mutex_exit(&ill->ill_lock);
4066 4067  
4067 4068          /*
4068 4069           * ill_delete_tail drops reference on ill_ipst, but we need to keep
4069 4070           * it held until the end of the function since the cleanup
4070 4071           * below needs to be able to use the ip_stack_t.
4071 4072           */
4072 4073          netstack_hold(ipst->ips_netstack);
4073 4074  
4074 4075          /* qprocsoff is done via ill_delete_tail */
4075 4076          ill_delete_tail(ill);
4076 4077          /*
4077 4078           * synchronously wait for arp stream to unbind. After this, we
4078 4079           * cannot get any data packets up from the driver.
4079 4080           */
4080 4081          arp_unbind_complete(ill);
4081 4082          ASSERT(ill->ill_ipst == NULL);
4082 4083  
4083 4084          /*
4084 4085           * Walk through all conns and qenable those that have queued data.
4085 4086           * Close synchronization needs this to
4086 4087           * be done to ensure that all upper layers blocked
4087 4088           * due to flow control to the closing device
4088 4089           * get unblocked.
4089 4090           */
4090 4091          ip1dbg(("ip_wsrv: walking\n"));
4091 4092          for (i = 0; i < TX_FANOUT_SIZE; i++) {
4092 4093                  conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
4093 4094          }
4094 4095  
4095 4096          /*
4096 4097           * ai can be null if this is an IPv6 ill, or if the IPv4
4097 4098           * stream is being torn down before ARP was plumbed (e.g.,
4098 4099           * /sbin/ifconfig plumbing a stream twice, and encountering
4099 4100           * an error
4100 4101           */
4101 4102          if (ai != NULL) {
4102 4103                  ASSERT(!ill->ill_isv6);
4103 4104                  mutex_enter(&ai->ai_lock);
4104 4105                  ai->ai_ill = NULL;
4105 4106                  if (ai->ai_arl == NULL) {
4106 4107                          mutex_destroy(&ai->ai_lock);
4107 4108                          kmem_free(ai, sizeof (*ai));
4108 4109                  } else {
4109 4110                          cv_signal(&ai->ai_ill_unplumb_done);
4110 4111                          mutex_exit(&ai->ai_lock);
4111 4112                  }
4112 4113          }
4113 4114  
4114 4115          mutex_enter(&ipst->ips_ip_mi_lock);
4115 4116          mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
4116 4117          mutex_exit(&ipst->ips_ip_mi_lock);
4117 4118  
4118 4119          /*
4119 4120           * credp could be null if the open didn't succeed and ip_modopen
4120 4121           * itself calls ip_close.
4121 4122           */
4122 4123          if (ill->ill_credp != NULL)
4123 4124                  crfree(ill->ill_credp);
4124 4125  
4125 4126          mutex_destroy(&ill->ill_saved_ire_lock);
4126 4127          mutex_destroy(&ill->ill_lock);
4127 4128          rw_destroy(&ill->ill_mcast_lock);
4128 4129          mutex_destroy(&ill->ill_mcast_serializer);
4129 4130          list_destroy(&ill->ill_nce);
4130 4131  
4131 4132          /*
4132 4133           * Now we are done with the module close pieces that
4133 4134           * need the netstack_t.
4134 4135           */
4135 4136          netstack_rele(ipst->ips_netstack);
4136 4137  
4137 4138          mi_close_free((IDP)ill);
4138 4139          q->q_ptr = WR(q)->q_ptr = NULL;
4139 4140  
4140 4141          ipsq_exit(ipsq);
4141 4142  
4142 4143          return (0);
4143 4144  }
4144 4145  
4145 4146  /*
4146 4147   * This is called as part of close() for IP, UDP, ICMP, and RTS
4147 4148   * in order to quiesce the conn.
4148 4149   */
4149 4150  void
4150 4151  ip_quiesce_conn(conn_t *connp)
4151 4152  {
4152 4153          boolean_t       drain_cleanup_reqd = B_FALSE;
4153 4154          boolean_t       conn_ioctl_cleanup_reqd = B_FALSE;
4154 4155          boolean_t       ilg_cleanup_reqd = B_FALSE;
4155 4156          ip_stack_t      *ipst;
4156 4157  
4157 4158          ASSERT(!IPCL_IS_TCP(connp));
4158 4159          ipst = connp->conn_netstack->netstack_ip;
4159 4160  
4160 4161          /*
4161 4162           * Mark the conn as closing, and this conn must not be
4162 4163           * inserted in future into any list. Eg. conn_drain_insert(),
4163 4164           * won't insert this conn into the conn_drain_list.
4164 4165           *
4165 4166           * conn_idl, and conn_ilg cannot get set henceforth.
4166 4167           */
4167 4168          mutex_enter(&connp->conn_lock);
4168 4169          ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4169 4170          connp->conn_state_flags |= CONN_CLOSING;
4170 4171          if (connp->conn_idl != NULL)
4171 4172                  drain_cleanup_reqd = B_TRUE;
4172 4173          if (connp->conn_oper_pending_ill != NULL)
4173 4174                  conn_ioctl_cleanup_reqd = B_TRUE;
4174 4175          if (connp->conn_dhcpinit_ill != NULL) {
4175 4176                  ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4176 4177                  atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4177 4178                  ill_set_inputfn(connp->conn_dhcpinit_ill);
4178 4179                  connp->conn_dhcpinit_ill = NULL;
4179 4180          }
4180 4181          if (connp->conn_ilg != NULL)
4181 4182                  ilg_cleanup_reqd = B_TRUE;
4182 4183          mutex_exit(&connp->conn_lock);
4183 4184  
4184 4185          if (conn_ioctl_cleanup_reqd)
4185 4186                  conn_ioctl_cleanup(connp);
4186 4187  
4187 4188          if (is_system_labeled() && connp->conn_anon_port) {
4188 4189                  (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4189 4190                      connp->conn_mlp_type, connp->conn_proto,
4190 4191                      ntohs(connp->conn_lport), B_FALSE);
4191 4192                  connp->conn_anon_port = 0;
4192 4193          }
4193 4194          connp->conn_mlp_type = mlptSingle;
4194 4195  
4195 4196          /*
4196 4197           * Remove this conn from any fanout list it is on.
4197 4198           * and then wait for any threads currently operating
4198 4199           * on this endpoint to finish
4199 4200           */
4200 4201          ipcl_hash_remove(connp);
4201 4202  
4202 4203          /*
4203 4204           * Remove this conn from the drain list, and do any other cleanup that
4204 4205           * may be required.  (TCP conns are never flow controlled, and
4205 4206           * conn_idl will be NULL.)
4206 4207           */
4207 4208          if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4208 4209                  idl_t *idl = connp->conn_idl;
4209 4210  
4210 4211                  mutex_enter(&idl->idl_lock);
4211 4212                  conn_drain(connp, B_TRUE);
4212 4213                  mutex_exit(&idl->idl_lock);
4213 4214          }
4214 4215  
4215 4216          if (connp == ipst->ips_ip_g_mrouter)
4216 4217                  (void) ip_mrouter_done(ipst);
4217 4218  
4218 4219          if (ilg_cleanup_reqd)
4219 4220                  ilg_delete_all(connp);
4220 4221  
4221 4222          /*
4222 4223           * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4223 4224           * callers from write side can't be there now because close
4224 4225           * is in progress. The only other caller is ipcl_walk
4225 4226           * which checks for the condemned flag.
4226 4227           */
4227 4228          mutex_enter(&connp->conn_lock);
4228 4229          connp->conn_state_flags |= CONN_CONDEMNED;
4229 4230          while (connp->conn_ref != 1)
4230 4231                  cv_wait(&connp->conn_cv, &connp->conn_lock);
4231 4232          connp->conn_state_flags |= CONN_QUIESCED;
4232 4233          mutex_exit(&connp->conn_lock);
4233 4234  }
4234 4235  
4235 4236  /* ARGSUSED */
4236 4237  int
4237 4238  ip_close(queue_t *q, int flags)
4238 4239  {
4239 4240          conn_t          *connp;
4240 4241  
4241 4242          /*
4242 4243           * Call the appropriate delete routine depending on whether this is
4243 4244           * a module or device.
4244 4245           */
4245 4246          if (WR(q)->q_next != NULL) {
4246 4247                  /* This is a module close */
4247 4248                  return (ip_modclose((ill_t *)q->q_ptr));
4248 4249          }
4249 4250  
4250 4251          connp = q->q_ptr;
4251 4252          ip_quiesce_conn(connp);
4252 4253  
4253 4254          qprocsoff(q);
4254 4255  
4255 4256          /*
4256 4257           * Now we are truly single threaded on this stream, and can
4257 4258           * delete the things hanging off the connp, and finally the connp.
4258 4259           * We removed this connp from the fanout list, it cannot be
4259 4260           * accessed thru the fanouts, and we already waited for the
4260 4261           * conn_ref to drop to 0. We are already in close, so
4261 4262           * there cannot be any other thread from the top. qprocsoff
4262 4263           * has completed, and service has completed or won't run in
4263 4264           * future.
4264 4265           */
4265 4266          ASSERT(connp->conn_ref == 1);
4266 4267  
4267 4268          inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4268 4269  
4269 4270          connp->conn_ref--;
4270 4271          ipcl_conn_destroy(connp);
4271 4272  
4272 4273          q->q_ptr = WR(q)->q_ptr = NULL;
4273 4274          return (0);
4274 4275  }
4275 4276  
4276 4277  /*
4277 4278   * Wapper around putnext() so that ip_rts_request can merely use
4278 4279   * conn_recv.
4279 4280   */
4280 4281  /*ARGSUSED2*/
4281 4282  static void
4282 4283  ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4283 4284  {
4284 4285          conn_t *connp = (conn_t *)arg1;
4285 4286  
4286 4287          putnext(connp->conn_rq, mp);
4287 4288  }
4288 4289  
4289 4290  /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4290 4291  /* ARGSUSED */
4291 4292  static void
4292 4293  ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4293 4294  {
4294 4295          freemsg(mp);
4295 4296  }
4296 4297  
4297 4298  /*
4298 4299   * Called when the module is about to be unloaded
4299 4300   */
4300 4301  void
4301 4302  ip_ddi_destroy(void)
4302 4303  {
4303 4304          /* This needs to be called before destroying any transports. */
4304 4305          mutex_enter(&cpu_lock);
4305 4306          unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4306 4307          mutex_exit(&cpu_lock);
4307 4308  
4308 4309          tnet_fini();
4309 4310  
4310 4311          icmp_ddi_g_destroy();
4311 4312          rts_ddi_g_destroy();
4312 4313          udp_ddi_g_destroy();
4313 4314          sctp_ddi_g_destroy();
4314 4315          tcp_ddi_g_destroy();
4315 4316          ilb_ddi_g_destroy();
4316 4317          dce_g_destroy();
4317 4318          ipsec_policy_g_destroy();
4318 4319          ipcl_g_destroy();
4319 4320          ip_net_g_destroy();
4320 4321          ip_ire_g_fini();
4321 4322          inet_minor_destroy(ip_minor_arena_sa);
4322 4323  #if defined(_LP64)
4323 4324          inet_minor_destroy(ip_minor_arena_la);
4324 4325  #endif
4325 4326  
4326 4327  #ifdef DEBUG
4327 4328          list_destroy(&ip_thread_list);
4328 4329          rw_destroy(&ip_thread_rwlock);
4329 4330          tsd_destroy(&ip_thread_data);
4330 4331  #endif
4331 4332  
4332 4333          netstack_unregister(NS_IP);
4333 4334  }
4334 4335  
4335 4336  /*
4336 4337   * First step in cleanup.
4337 4338   */
4338 4339  /* ARGSUSED */
4339 4340  static void
4340 4341  ip_stack_shutdown(netstackid_t stackid, void *arg)
4341 4342  {
4342 4343          ip_stack_t *ipst = (ip_stack_t *)arg;
4343 4344  
4344 4345  #ifdef NS_DEBUG
4345 4346          printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4346 4347  #endif
4347 4348  
4348 4349          /*
4349 4350           * Perform cleanup for special interfaces (loopback and IPMP).
4350 4351           */
4351 4352          ip_interface_cleanup(ipst);
4352 4353  
4353 4354          /*
4354 4355           * The *_hook_shutdown()s start the process of notifying any
4355 4356           * consumers that things are going away.... nothing is destroyed.
4356 4357           */
4357 4358          ipv4_hook_shutdown(ipst);
4358 4359          ipv6_hook_shutdown(ipst);
4359 4360          arp_hook_shutdown(ipst);
4360 4361  
4361 4362          mutex_enter(&ipst->ips_capab_taskq_lock);
4362 4363          ipst->ips_capab_taskq_quit = B_TRUE;
4363 4364          cv_signal(&ipst->ips_capab_taskq_cv);
4364 4365          mutex_exit(&ipst->ips_capab_taskq_lock);
4365 4366  }
4366 4367  
4367 4368  /*
4368 4369   * Free the IP stack instance.
4369 4370   */
4370 4371  static void
4371 4372  ip_stack_fini(netstackid_t stackid, void *arg)
4372 4373  {
4373 4374          ip_stack_t *ipst = (ip_stack_t *)arg;
4374 4375          int ret;
4375 4376  
4376 4377  #ifdef NS_DEBUG
4377 4378          printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4378 4379  #endif
4379 4380          /*
4380 4381           * At this point, all of the notifications that the events and
4381 4382           * protocols are going away have been run, meaning that we can
4382 4383           * now set about starting to clean things up.
4383 4384           */
4384 4385          ipobs_fini(ipst);
4385 4386          ipv4_hook_destroy(ipst);
4386 4387          ipv6_hook_destroy(ipst);
4387 4388          arp_hook_destroy(ipst);
4388 4389          ip_net_destroy(ipst);
4389 4390  
4390 4391          ipmp_destroy(ipst);
4391 4392  
4392 4393          ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4393 4394          ipst->ips_ip_mibkp = NULL;
4394 4395          icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4395 4396          ipst->ips_icmp_mibkp = NULL;
4396 4397          ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4397 4398          ipst->ips_ip_kstat = NULL;
4398 4399          bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4399 4400          ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4400 4401          ipst->ips_ip6_kstat = NULL;
4401 4402          bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4402 4403  
4403 4404          kmem_free(ipst->ips_propinfo_tbl,
4404 4405              ip_propinfo_count * sizeof (mod_prop_info_t));
4405 4406          ipst->ips_propinfo_tbl = NULL;
4406 4407  
4407 4408          dce_stack_destroy(ipst);
4408 4409          ip_mrouter_stack_destroy(ipst);
4409 4410  
4410 4411          ret = untimeout(ipst->ips_igmp_timeout_id);
4411 4412          if (ret == -1) {
4412 4413                  ASSERT(ipst->ips_igmp_timeout_id == 0);
4413 4414          } else {
4414 4415                  ASSERT(ipst->ips_igmp_timeout_id != 0);
4415 4416                  ipst->ips_igmp_timeout_id = 0;
4416 4417          }
4417 4418          ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4418 4419          if (ret == -1) {
4419 4420                  ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4420 4421          } else {
4421 4422                  ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4422 4423                  ipst->ips_igmp_slowtimeout_id = 0;
4423 4424          }
4424 4425          ret = untimeout(ipst->ips_mld_timeout_id);
4425 4426          if (ret == -1) {
4426 4427                  ASSERT(ipst->ips_mld_timeout_id == 0);
4427 4428          } else {
4428 4429                  ASSERT(ipst->ips_mld_timeout_id != 0);
4429 4430                  ipst->ips_mld_timeout_id = 0;
4430 4431          }
4431 4432          ret = untimeout(ipst->ips_mld_slowtimeout_id);
4432 4433          if (ret == -1) {
4433 4434                  ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4434 4435          } else {
4435 4436                  ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4436 4437                  ipst->ips_mld_slowtimeout_id = 0;
4437 4438          }
4438 4439  
4439 4440          ip_ire_fini(ipst);
4440 4441          ip6_asp_free(ipst);
4441 4442          conn_drain_fini(ipst);
4442 4443          ipcl_destroy(ipst);
4443 4444  
4444 4445          mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4445 4446          mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4446 4447          kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4447 4448          ipst->ips_ndp4 = NULL;
4448 4449          kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4449 4450          ipst->ips_ndp6 = NULL;
4450 4451  
4451 4452          if (ipst->ips_loopback_ksp != NULL) {
4452 4453                  kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4453 4454                  ipst->ips_loopback_ksp = NULL;
4454 4455          }
4455 4456  
4456 4457          mutex_destroy(&ipst->ips_capab_taskq_lock);
4457 4458          cv_destroy(&ipst->ips_capab_taskq_cv);
4458 4459  
4459 4460          rw_destroy(&ipst->ips_srcid_lock);
4460 4461  
4461 4462          mutex_destroy(&ipst->ips_ip_mi_lock);
4462 4463          rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4463 4464  
4464 4465          mutex_destroy(&ipst->ips_igmp_timer_lock);
4465 4466          mutex_destroy(&ipst->ips_mld_timer_lock);
4466 4467          mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4467 4468          mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4468 4469          mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4469 4470          rw_destroy(&ipst->ips_ill_g_lock);
4470 4471  
4471 4472          kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4472 4473          ipst->ips_phyint_g_list = NULL;
4473 4474          kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4474 4475          ipst->ips_ill_g_heads = NULL;
4475 4476  
4476 4477          ldi_ident_release(ipst->ips_ldi_ident);
4477 4478          kmem_free(ipst, sizeof (*ipst));
4478 4479  }
4479 4480  
4480 4481  /*
4481 4482   * This function is called from the TSD destructor, and is used to debug
4482 4483   * reference count issues in IP. See block comment in <inet/ip_if.h> for
4483 4484   * details.
4484 4485   */
4485 4486  static void
4486 4487  ip_thread_exit(void *phash)
4487 4488  {
4488 4489          th_hash_t *thh = phash;
4489 4490  
4490 4491          rw_enter(&ip_thread_rwlock, RW_WRITER);
4491 4492          list_remove(&ip_thread_list, thh);
4492 4493          rw_exit(&ip_thread_rwlock);
4493 4494          mod_hash_destroy_hash(thh->thh_hash);
4494 4495          kmem_free(thh, sizeof (*thh));
4495 4496  }
4496 4497  
4497 4498  /*
4498 4499   * Called when the IP kernel module is loaded into the kernel
4499 4500   */
4500 4501  void
4501 4502  ip_ddi_init(void)
4502 4503  {
4503 4504          ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4504 4505  
4505 4506          /*
4506 4507           * For IP and TCP the minor numbers should start from 2 since we have 4
4507 4508           * initial devices: ip, ip6, tcp, tcp6.
4508 4509           */
4509 4510          /*
4510 4511           * If this is a 64-bit kernel, then create two separate arenas -
4511 4512           * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4512 4513           * other for socket apps in the range 2^^18 through 2^^32-1.
4513 4514           */
4514 4515          ip_minor_arena_la = NULL;
4515 4516          ip_minor_arena_sa = NULL;
4516 4517  #if defined(_LP64)
4517 4518          if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4518 4519              INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4519 4520                  cmn_err(CE_PANIC,
4520 4521                      "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4521 4522          }
4522 4523          if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4523 4524              MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4524 4525                  cmn_err(CE_PANIC,
4525 4526                      "ip_ddi_init: ip_minor_arena_la creation failed\n");
4526 4527          }
4527 4528  #else
4528 4529          if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4529 4530              INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4530 4531                  cmn_err(CE_PANIC,
4531 4532                      "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4532 4533          }
4533 4534  #endif
4534 4535          ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4535 4536  
4536 4537          ipcl_g_init();
4537 4538          ip_ire_g_init();
4538 4539          ip_net_g_init();
4539 4540  
4540 4541  #ifdef DEBUG
4541 4542          tsd_create(&ip_thread_data, ip_thread_exit);
4542 4543          rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4543 4544          list_create(&ip_thread_list, sizeof (th_hash_t),
4544 4545              offsetof(th_hash_t, thh_link));
4545 4546  #endif
4546 4547          ipsec_policy_g_init();
4547 4548          tcp_ddi_g_init();
4548 4549          sctp_ddi_g_init();
4549 4550          dce_g_init();
4550 4551  
4551 4552          /*
4552 4553           * We want to be informed each time a stack is created or
4553 4554           * destroyed in the kernel, so we can maintain the
4554 4555           * set of udp_stack_t's.
4555 4556           */
4556 4557          netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4557 4558              ip_stack_fini);
4558 4559  
4559 4560          tnet_init();
4560 4561  
4561 4562          udp_ddi_g_init();
4562 4563          rts_ddi_g_init();
4563 4564          icmp_ddi_g_init();
4564 4565          ilb_ddi_g_init();
4565 4566  
4566 4567          /* This needs to be called after all transports are initialized. */
4567 4568          mutex_enter(&cpu_lock);
4568 4569          register_cpu_setup_func(ip_tp_cpu_update, NULL);
4569 4570          mutex_exit(&cpu_lock);
4570 4571  }
4571 4572  
4572 4573  /*
4573 4574   * Initialize the IP stack instance.
4574 4575   */
4575 4576  static void *
4576 4577  ip_stack_init(netstackid_t stackid, netstack_t *ns)
4577 4578  {
4578 4579          ip_stack_t      *ipst;
4579 4580          size_t          arrsz;
4580 4581          major_t         major;
4581 4582  
4582 4583  #ifdef NS_DEBUG
4583 4584          printf("ip_stack_init(stack %d)\n", stackid);
4584 4585  #endif
4585 4586  
4586 4587          ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4587 4588          ipst->ips_netstack = ns;
4588 4589  
4589 4590          ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4590 4591              KM_SLEEP);
4591 4592          ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4592 4593              KM_SLEEP);
4593 4594          ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4594 4595          ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4595 4596          mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4596 4597          mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4597 4598  
4598 4599          mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4599 4600          ipst->ips_igmp_deferred_next = INFINITY;
4600 4601          mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4601 4602          ipst->ips_mld_deferred_next = INFINITY;
4602 4603          mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4603 4604          mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4604 4605          mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4605 4606          mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4606 4607          rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4607 4608          rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4608 4609  
4609 4610          ipcl_init(ipst);
4610 4611          ip_ire_init(ipst);
4611 4612          ip6_asp_init(ipst);
4612 4613          ipif_init(ipst);
4613 4614          conn_drain_init(ipst);
4614 4615          ip_mrouter_stack_init(ipst);
4615 4616          dce_stack_init(ipst);
4616 4617  
4617 4618          ipst->ips_ip_multirt_log_interval = 1000;
4618 4619  
4619 4620          ipst->ips_ill_index = 1;
4620 4621  
4621 4622          ipst->ips_saved_ip_forwarding = -1;
4622 4623          ipst->ips_reg_vif_num = ALL_VIFS;       /* Index to Register vif */
4623 4624  
4624 4625          arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4625 4626          ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4626 4627          bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4627 4628  
4628 4629          ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4629 4630          ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4630 4631          ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4631 4632          ipst->ips_ip6_kstat =
4632 4633              ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4633 4634  
4634 4635          ipst->ips_ip_src_id = 1;
4635 4636          rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4636 4637  
4637 4638          ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4638 4639  
  
    | 
      ↓ open down ↓ | 
    4604 lines elided | 
    
      ↑ open up ↑ | 
  
4639 4640          ip_net_init(ipst, ns);
4640 4641          ipv4_hook_init(ipst);
4641 4642          ipv6_hook_init(ipst);
4642 4643          arp_hook_init(ipst);
4643 4644          ipmp_init(ipst);
4644 4645          ipobs_init(ipst);
4645 4646  
4646 4647          /*
4647 4648           * Create the taskq dispatcher thread and initialize related stuff.
4648 4649           */
4649      -        ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4650      -            ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4651 4650          mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4652 4651          cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
     4652 +        ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
     4653 +            ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4653 4654  
4654 4655          major = mod_name_to_major(INET_NAME);
4655 4656          (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4656 4657          return (ipst);
4657 4658  }
4658 4659  
4659 4660  /*
4660 4661   * Allocate and initialize a DLPI template of the specified length.  (May be
4661 4662   * called as writer.)
4662 4663   */
4663 4664  mblk_t *
4664 4665  ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4665 4666  {
4666 4667          mblk_t  *mp;
4667 4668  
4668 4669          mp = allocb(len, BPRI_MED);
4669 4670          if (!mp)
4670 4671                  return (NULL);
4671 4672  
4672 4673          /*
4673 4674           * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4674 4675           * of which we don't seem to use) are sent with M_PCPROTO, and
4675 4676           * that other DLPI are M_PROTO.
4676 4677           */
4677 4678          if (prim == DL_INFO_REQ) {
4678 4679                  mp->b_datap->db_type = M_PCPROTO;
4679 4680          } else {
4680 4681                  mp->b_datap->db_type = M_PROTO;
4681 4682          }
4682 4683  
4683 4684          mp->b_wptr = mp->b_rptr + len;
4684 4685          bzero(mp->b_rptr, len);
4685 4686          ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4686 4687          return (mp);
4687 4688  }
4688 4689  
4689 4690  /*
4690 4691   * Allocate and initialize a DLPI notification.  (May be called as writer.)
4691 4692   */
4692 4693  mblk_t *
4693 4694  ip_dlnotify_alloc(uint_t notification, uint_t data)
4694 4695  {
4695 4696          dl_notify_ind_t *notifyp;
4696 4697          mblk_t          *mp;
4697 4698  
4698 4699          if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4699 4700                  return (NULL);
4700 4701  
4701 4702          notifyp = (dl_notify_ind_t *)mp->b_rptr;
4702 4703          notifyp->dl_notification = notification;
4703 4704          notifyp->dl_data = data;
4704 4705          return (mp);
4705 4706  }
4706 4707  
4707 4708  mblk_t *
4708 4709  ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4709 4710  {
4710 4711          dl_notify_ind_t *notifyp;
4711 4712          mblk_t          *mp;
4712 4713  
4713 4714          if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4714 4715                  return (NULL);
4715 4716  
4716 4717          notifyp = (dl_notify_ind_t *)mp->b_rptr;
4717 4718          notifyp->dl_notification = notification;
4718 4719          notifyp->dl_data1 = data1;
4719 4720          notifyp->dl_data2 = data2;
4720 4721          return (mp);
4721 4722  }
4722 4723  
4723 4724  /*
4724 4725   * Debug formatting routine.  Returns a character string representation of the
4725 4726   * addr in buf, of the form xxx.xxx.xxx.xxx.  This routine takes the address
4726 4727   * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4727 4728   *
4728 4729   * Once the ndd table-printing interfaces are removed, this can be changed to
4729 4730   * standard dotted-decimal form.
4730 4731   */
4731 4732  char *
4732 4733  ip_dot_addr(ipaddr_t addr, char *buf)
4733 4734  {
4734 4735          uint8_t *ap = (uint8_t *)&addr;
4735 4736  
4736 4737          (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4737 4738              ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4738 4739          return (buf);
4739 4740  }
4740 4741  
4741 4742  /*
4742 4743   * Write the given MAC address as a printable string in the usual colon-
4743 4744   * separated format.
4744 4745   */
4745 4746  const char *
4746 4747  mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4747 4748  {
4748 4749          char *bp;
4749 4750  
4750 4751          if (alen == 0 || buflen < 4)
4751 4752                  return ("?");
4752 4753          bp = buf;
4753 4754          for (;;) {
4754 4755                  /*
4755 4756                   * If there are more MAC address bytes available, but we won't
4756 4757                   * have any room to print them, then add "..." to the string
4757 4758                   * instead.  See below for the 'magic number' explanation.
4758 4759                   */
4759 4760                  if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4760 4761                          (void) strcpy(bp, "...");
4761 4762                          break;
4762 4763                  }
4763 4764                  (void) sprintf(bp, "%02x", *addr++);
4764 4765                  bp += 2;
4765 4766                  if (--alen == 0)
4766 4767                          break;
4767 4768                  *bp++ = ':';
4768 4769                  buflen -= 3;
4769 4770                  /*
4770 4771                   * At this point, based on the first 'if' statement above,
4771 4772                   * either alen == 1 and buflen >= 3, or alen > 1 and
4772 4773                   * buflen >= 4.  The first case leaves room for the final "xx"
4773 4774                   * number and trailing NUL byte.  The second leaves room for at
4774 4775                   * least "...".  Thus the apparently 'magic' numbers chosen for
4775 4776                   * that statement.
4776 4777                   */
4777 4778          }
4778 4779          return (buf);
4779 4780  }
4780 4781  
4781 4782  /*
4782 4783   * Called when it is conceptually a ULP that would sent the packet
4783 4784   * e.g., port unreachable and protocol unreachable. Check that the packet
4784 4785   * would have passed the IPsec global policy before sending the error.
4785 4786   *
4786 4787   * Send an ICMP error after patching up the packet appropriately.
4787 4788   * Uses ip_drop_input and bumps the appropriate MIB.
4788 4789   */
4789 4790  void
4790 4791  ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4791 4792      ip_recv_attr_t *ira)
4792 4793  {
4793 4794          ipha_t          *ipha;
4794 4795          boolean_t       secure;
4795 4796          ill_t           *ill = ira->ira_ill;
4796 4797          ip_stack_t      *ipst = ill->ill_ipst;
4797 4798          netstack_t      *ns = ipst->ips_netstack;
4798 4799          ipsec_stack_t   *ipss = ns->netstack_ipsec;
4799 4800  
4800 4801          secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4801 4802  
4802 4803          /*
4803 4804           * We are generating an icmp error for some inbound packet.
4804 4805           * Called from all ip_fanout_(udp, tcp, proto) functions.
4805 4806           * Before we generate an error, check with global policy
4806 4807           * to see whether this is allowed to enter the system. As
4807 4808           * there is no "conn", we are checking with global policy.
4808 4809           */
4809 4810          ipha = (ipha_t *)mp->b_rptr;
4810 4811          if (secure || ipss->ipsec_inbound_v4_policy_present) {
4811 4812                  mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4812 4813                  if (mp == NULL)
4813 4814                          return;
4814 4815          }
4815 4816  
4816 4817          /* We never send errors for protocols that we do implement */
4817 4818          if (ira->ira_protocol == IPPROTO_ICMP ||
4818 4819              ira->ira_protocol == IPPROTO_IGMP) {
4819 4820                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4820 4821                  ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4821 4822                  freemsg(mp);
4822 4823                  return;
4823 4824          }
4824 4825          /*
4825 4826           * Have to correct checksum since
4826 4827           * the packet might have been
4827 4828           * fragmented and the reassembly code in ip_rput
4828 4829           * does not restore the IP checksum.
4829 4830           */
4830 4831          ipha->ipha_hdr_checksum = 0;
4831 4832          ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4832 4833  
4833 4834          switch (icmp_type) {
4834 4835          case ICMP_DEST_UNREACHABLE:
4835 4836                  switch (icmp_code) {
4836 4837                  case ICMP_PROTOCOL_UNREACHABLE:
4837 4838                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4838 4839                          ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4839 4840                          break;
4840 4841                  case ICMP_PORT_UNREACHABLE:
4841 4842                          BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4842 4843                          ip_drop_input("ipIfStatsNoPorts", mp, ill);
4843 4844                          break;
4844 4845                  }
4845 4846  
4846 4847                  icmp_unreachable(mp, icmp_code, ira);
4847 4848                  break;
4848 4849          default:
4849 4850  #ifdef DEBUG
4850 4851                  panic("ip_fanout_send_icmp_v4: wrong type");
4851 4852                  /*NOTREACHED*/
4852 4853  #else
4853 4854                  freemsg(mp);
4854 4855                  break;
4855 4856  #endif
4856 4857          }
4857 4858  }
4858 4859  
4859 4860  /*
4860 4861   * Used to send an ICMP error message when a packet is received for
4861 4862   * a protocol that is not supported. The mblk passed as argument
4862 4863   * is consumed by this function.
4863 4864   */
4864 4865  void
4865 4866  ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4866 4867  {
4867 4868          ipha_t          *ipha;
4868 4869  
4869 4870          ipha = (ipha_t *)mp->b_rptr;
4870 4871          if (ira->ira_flags & IRAF_IS_IPV4) {
4871 4872                  ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4872 4873                  ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4873 4874                      ICMP_PROTOCOL_UNREACHABLE, ira);
4874 4875          } else {
4875 4876                  ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4876 4877                  ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4877 4878                      ICMP6_PARAMPROB_NEXTHEADER, ira);
4878 4879          }
4879 4880  }
4880 4881  
4881 4882  /*
4882 4883   * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4883 4884   * Handles IPv4 and IPv6.
4884 4885   * We are responsible for disposing of mp, such as by freemsg() or putnext()
4885 4886   * Caller is responsible for dropping references to the conn.
4886 4887   */
4887 4888  void
4888 4889  ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4889 4890      ip_recv_attr_t *ira)
4890 4891  {
4891 4892          ill_t           *ill = ira->ira_ill;
4892 4893          ip_stack_t      *ipst = ill->ill_ipst;
4893 4894          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
4894 4895          boolean_t       secure;
4895 4896          uint_t          protocol = ira->ira_protocol;
4896 4897          iaflags_t       iraflags = ira->ira_flags;
4897 4898          queue_t         *rq;
4898 4899  
4899 4900          secure = iraflags & IRAF_IPSEC_SECURE;
4900 4901  
4901 4902          rq = connp->conn_rq;
4902 4903          if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4903 4904                  switch (protocol) {
4904 4905                  case IPPROTO_ICMPV6:
4905 4906                          BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4906 4907                          break;
4907 4908                  case IPPROTO_ICMP:
4908 4909                          BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4909 4910                          break;
4910 4911                  default:
4911 4912                          BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4912 4913                          break;
4913 4914                  }
4914 4915                  freemsg(mp);
4915 4916                  return;
4916 4917          }
4917 4918  
4918 4919          ASSERT(!(IPCL_IS_IPTUN(connp)));
4919 4920  
4920 4921          if (((iraflags & IRAF_IS_IPV4) ?
4921 4922              CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4922 4923              CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4923 4924              secure) {
4924 4925                  mp = ipsec_check_inbound_policy(mp, connp, ipha,
4925 4926                      ip6h, ira);
4926 4927                  if (mp == NULL) {
4927 4928                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4928 4929                          /* Note that mp is NULL */
4929 4930                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
4930 4931                          return;
4931 4932                  }
4932 4933          }
4933 4934  
4934 4935          if (iraflags & IRAF_ICMP_ERROR) {
4935 4936                  (connp->conn_recvicmp)(connp, mp, NULL, ira);
4936 4937          } else {
4937 4938                  ill_t *rill = ira->ira_rill;
4938 4939  
4939 4940                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4940 4941                  ira->ira_ill = ira->ira_rill = NULL;
4941 4942                  /* Send it upstream */
4942 4943                  (connp->conn_recv)(connp, mp, NULL, ira);
4943 4944                  ira->ira_ill = ill;
4944 4945                  ira->ira_rill = rill;
4945 4946          }
4946 4947  }
4947 4948  
4948 4949  /*
4949 4950   * Handle protocols with which IP is less intimate.  There
4950 4951   * can be more than one stream bound to a particular
4951 4952   * protocol.  When this is the case, normally each one gets a copy
4952 4953   * of any incoming packets.
4953 4954   *
4954 4955   * IPsec NOTE :
4955 4956   *
4956 4957   * Don't allow a secure packet going up a non-secure connection.
4957 4958   * We don't allow this because
4958 4959   *
4959 4960   * 1) Reply might go out in clear which will be dropped at
4960 4961   *    the sending side.
4961 4962   * 2) If the reply goes out in clear it will give the
4962 4963   *    adversary enough information for getting the key in
4963 4964   *    most of the cases.
4964 4965   *
4965 4966   * Moreover getting a secure packet when we expect clear
4966 4967   * implies that SA's were added without checking for
4967 4968   * policy on both ends. This should not happen once ISAKMP
4968 4969   * is used to negotiate SAs as SAs will be added only after
4969 4970   * verifying the policy.
4970 4971   *
4971 4972   * Zones notes:
4972 4973   * Earlier in ip_input on a system with multiple shared-IP zones we
4973 4974   * duplicate the multicast and broadcast packets and send them up
4974 4975   * with each explicit zoneid that exists on that ill.
4975 4976   * This means that here we can match the zoneid with SO_ALLZONES being special.
4976 4977   */
4977 4978  void
4978 4979  ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
4979 4980  {
4980 4981          mblk_t          *mp1;
4981 4982          ipaddr_t        laddr;
4982 4983          conn_t          *connp, *first_connp, *next_connp;
4983 4984          connf_t         *connfp;
4984 4985          ill_t           *ill = ira->ira_ill;
4985 4986          ip_stack_t      *ipst = ill->ill_ipst;
4986 4987  
4987 4988          laddr = ipha->ipha_dst;
4988 4989  
4989 4990          connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
4990 4991          mutex_enter(&connfp->connf_lock);
4991 4992          connp = connfp->connf_head;
4992 4993          for (connp = connfp->connf_head; connp != NULL;
4993 4994              connp = connp->conn_next) {
4994 4995                  /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
4995 4996                  if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
4996 4997                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
4997 4998                      tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) {
4998 4999                          break;
4999 5000                  }
5000 5001          }
5001 5002  
5002 5003          if (connp == NULL) {
5003 5004                  /*
5004 5005                   * No one bound to these addresses.  Is
5005 5006                   * there a client that wants all
5006 5007                   * unclaimed datagrams?
5007 5008                   */
5008 5009                  mutex_exit(&connfp->connf_lock);
5009 5010                  ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
5010 5011                      ICMP_PROTOCOL_UNREACHABLE, ira);
5011 5012                  return;
5012 5013          }
5013 5014  
5014 5015          ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5015 5016  
5016 5017          CONN_INC_REF(connp);
5017 5018          first_connp = connp;
5018 5019          connp = connp->conn_next;
5019 5020  
5020 5021          for (;;) {
5021 5022                  while (connp != NULL) {
5022 5023                          /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5023 5024                          if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5024 5025                              (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5025 5026                              tsol_receive_local(mp, &laddr, IPV4_VERSION,
5026 5027                              ira, connp)))
5027 5028                                  break;
5028 5029                          connp = connp->conn_next;
5029 5030                  }
5030 5031  
5031 5032                  if (connp == NULL) {
5032 5033                          /* No more interested clients */
5033 5034                          connp = first_connp;
5034 5035                          break;
5035 5036                  }
5036 5037                  if (((mp1 = dupmsg(mp)) == NULL) &&
5037 5038                      ((mp1 = copymsg(mp)) == NULL)) {
5038 5039                          /* Memory allocation failed */
5039 5040                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5040 5041                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
5041 5042                          connp = first_connp;
5042 5043                          break;
5043 5044                  }
5044 5045  
5045 5046                  CONN_INC_REF(connp);
5046 5047                  mutex_exit(&connfp->connf_lock);
5047 5048  
5048 5049                  ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
5049 5050                      ira);
5050 5051  
5051 5052                  mutex_enter(&connfp->connf_lock);
5052 5053                  /* Follow the next pointer before releasing the conn. */
5053 5054                  next_connp = connp->conn_next;
5054 5055                  CONN_DEC_REF(connp);
5055 5056                  connp = next_connp;
5056 5057          }
5057 5058  
5058 5059          /* Last one.  Send it upstream. */
5059 5060          mutex_exit(&connfp->connf_lock);
5060 5061  
5061 5062          ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
5062 5063  
5063 5064          CONN_DEC_REF(connp);
5064 5065  }
5065 5066  
5066 5067  /*
5067 5068   * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
5068 5069   * pass it along to ESP if the SPI is non-zero.  Returns the mblk if the mblk
5069 5070   * is not consumed.
5070 5071   *
5071 5072   * One of three things can happen, all of which affect the passed-in mblk:
5072 5073   *
5073 5074   * 1.) The packet is stock UDP and gets its zero-SPI stripped.  Return mblk..
5074 5075   *
5075 5076   * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
5076 5077   *     ESP packet, and is passed along to ESP for consumption.  Return NULL.
5077 5078   *
5078 5079   * 3.) The packet is an ESP-in-UDP Keepalive.  Drop it and return NULL.
5079 5080   */
5080 5081  mblk_t *
5081 5082  zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
5082 5083  {
5083 5084          int shift, plen, iph_len;
5084 5085          ipha_t *ipha;
5085 5086          udpha_t *udpha;
5086 5087          uint32_t *spi;
5087 5088          uint32_t esp_ports;
5088 5089          uint8_t *orptr;
5089 5090          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
5090 5091          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
5091 5092  
5092 5093          ipha = (ipha_t *)mp->b_rptr;
5093 5094          iph_len = ira->ira_ip_hdr_length;
5094 5095          plen = ira->ira_pktlen;
5095 5096  
5096 5097          if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
5097 5098                  /*
5098 5099                   * Most likely a keepalive for the benefit of an intervening
5099 5100                   * NAT.  These aren't for us, per se, so drop it.
5100 5101                   *
5101 5102                   * RFC 3947/8 doesn't say for sure what to do for 2-3
5102 5103                   * byte packets (keepalives are 1-byte), but we'll drop them
5103 5104                   * also.
5104 5105                   */
5105 5106                  ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5106 5107                      DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
5107 5108                  return (NULL);
5108 5109          }
5109 5110  
5110 5111          if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
5111 5112                  /* might as well pull it all up - it might be ESP. */
5112 5113                  if (!pullupmsg(mp, -1)) {
5113 5114                          ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5114 5115                              DROPPER(ipss, ipds_esp_nomem),
5115 5116                              &ipss->ipsec_dropper);
5116 5117                          return (NULL);
5117 5118                  }
5118 5119  
5119 5120                  ipha = (ipha_t *)mp->b_rptr;
5120 5121          }
5121 5122          spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
5122 5123          if (*spi == 0) {
5123 5124                  /* UDP packet - remove 0-spi. */
5124 5125                  shift = sizeof (uint32_t);
5125 5126          } else {
5126 5127                  /* ESP-in-UDP packet - reduce to ESP. */
5127 5128                  ipha->ipha_protocol = IPPROTO_ESP;
5128 5129                  shift = sizeof (udpha_t);
5129 5130          }
5130 5131  
5131 5132          /* Fix IP header */
5132 5133          ira->ira_pktlen = (plen - shift);
5133 5134          ipha->ipha_length = htons(ira->ira_pktlen);
5134 5135          ipha->ipha_hdr_checksum = 0;
5135 5136  
5136 5137          orptr = mp->b_rptr;
5137 5138          mp->b_rptr += shift;
5138 5139  
5139 5140          udpha = (udpha_t *)(orptr + iph_len);
5140 5141          if (*spi == 0) {
5141 5142                  ASSERT((uint8_t *)ipha == orptr);
5142 5143                  udpha->uha_length = htons(plen - shift - iph_len);
5143 5144                  iph_len += sizeof (udpha_t);    /* For the call to ovbcopy(). */
5144 5145                  esp_ports = 0;
5145 5146          } else {
5146 5147                  esp_ports = *((uint32_t *)udpha);
5147 5148                  ASSERT(esp_ports != 0);
5148 5149          }
5149 5150          ovbcopy(orptr, orptr + shift, iph_len);
5150 5151          if (esp_ports != 0) /* Punt up for ESP processing. */ {
5151 5152                  ipha = (ipha_t *)(orptr + shift);
5152 5153  
5153 5154                  ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5154 5155                  ira->ira_esp_udp_ports = esp_ports;
5155 5156                  ip_fanout_v4(mp, ipha, ira);
5156 5157                  return (NULL);
5157 5158          }
5158 5159          return (mp);
5159 5160  }
5160 5161  
5161 5162  /*
5162 5163   * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5163 5164   * Handles IPv4 and IPv6.
5164 5165   * We are responsible for disposing of mp, such as by freemsg() or putnext()
5165 5166   * Caller is responsible for dropping references to the conn.
5166 5167   */
5167 5168  void
5168 5169  ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5169 5170      ip_recv_attr_t *ira)
5170 5171  {
5171 5172          ill_t           *ill = ira->ira_ill;
5172 5173          ip_stack_t      *ipst = ill->ill_ipst;
5173 5174          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
5174 5175          boolean_t       secure;
5175 5176          iaflags_t       iraflags = ira->ira_flags;
5176 5177  
5177 5178          secure = iraflags & IRAF_IPSEC_SECURE;
5178 5179  
5179 5180          if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5180 5181              !canputnext(connp->conn_rq)) {
5181 5182                  BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5182 5183                  freemsg(mp);
5183 5184                  return;
5184 5185          }
5185 5186  
5186 5187          if (((iraflags & IRAF_IS_IPV4) ?
5187 5188              CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5188 5189              CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5189 5190              secure) {
5190 5191                  mp = ipsec_check_inbound_policy(mp, connp, ipha,
5191 5192                      ip6h, ira);
5192 5193                  if (mp == NULL) {
5193 5194                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5194 5195                          /* Note that mp is NULL */
5195 5196                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
5196 5197                          return;
5197 5198                  }
5198 5199          }
5199 5200  
5200 5201          /*
5201 5202           * Since this code is not used for UDP unicast we don't need a NAT_T
5202 5203           * check. Only ip_fanout_v4 has that check.
5203 5204           */
5204 5205          if (ira->ira_flags & IRAF_ICMP_ERROR) {
5205 5206                  (connp->conn_recvicmp)(connp, mp, NULL, ira);
5206 5207          } else {
5207 5208                  ill_t *rill = ira->ira_rill;
5208 5209  
5209 5210                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5210 5211                  ira->ira_ill = ira->ira_rill = NULL;
5211 5212                  /* Send it upstream */
5212 5213                  (connp->conn_recv)(connp, mp, NULL, ira);
5213 5214                  ira->ira_ill = ill;
5214 5215                  ira->ira_rill = rill;
5215 5216          }
5216 5217  }
5217 5218  
5218 5219  /*
5219 5220   * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5220 5221   * (Unicast fanout is handled in ip_input_v4.)
5221 5222   *
5222 5223   * If SO_REUSEADDR is set all multicast and broadcast packets
5223 5224   * will be delivered to all conns bound to the same port.
5224 5225   *
5225 5226   * If there is at least one matching AF_INET receiver, then we will
5226 5227   * ignore any AF_INET6 receivers.
5227 5228   * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5228 5229   * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5229 5230   * packets.
5230 5231   *
5231 5232   * Zones notes:
5232 5233   * Earlier in ip_input on a system with multiple shared-IP zones we
5233 5234   * duplicate the multicast and broadcast packets and send them up
5234 5235   * with each explicit zoneid that exists on that ill.
5235 5236   * This means that here we can match the zoneid with SO_ALLZONES being special.
5236 5237   */
5237 5238  void
5238 5239  ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5239 5240      ip_recv_attr_t *ira)
5240 5241  {
5241 5242          ipaddr_t        laddr;
5242 5243          in6_addr_t      v6faddr;
5243 5244          conn_t          *connp;
5244 5245          connf_t         *connfp;
5245 5246          ipaddr_t        faddr;
5246 5247          ill_t           *ill = ira->ira_ill;
5247 5248          ip_stack_t      *ipst = ill->ill_ipst;
5248 5249  
5249 5250          ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5250 5251  
5251 5252          laddr = ipha->ipha_dst;
5252 5253          faddr = ipha->ipha_src;
5253 5254  
5254 5255          connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5255 5256          mutex_enter(&connfp->connf_lock);
5256 5257          connp = connfp->connf_head;
5257 5258  
5258 5259          /*
5259 5260           * If SO_REUSEADDR has been set on the first we send the
5260 5261           * packet to all clients that have joined the group and
5261 5262           * match the port.
5262 5263           */
5263 5264          while (connp != NULL) {
5264 5265                  if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5265 5266                      conn_wantpacket(connp, ira, ipha) &&
5266 5267                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5267 5268                      tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5268 5269                          break;
5269 5270                  connp = connp->conn_next;
5270 5271          }
5271 5272  
5272 5273          if (connp == NULL)
5273 5274                  goto notfound;
5274 5275  
5275 5276          CONN_INC_REF(connp);
5276 5277  
5277 5278          if (connp->conn_reuseaddr) {
5278 5279                  conn_t          *first_connp = connp;
5279 5280                  conn_t          *next_connp;
5280 5281                  mblk_t          *mp1;
5281 5282  
5282 5283                  connp = connp->conn_next;
5283 5284                  for (;;) {
5284 5285                          while (connp != NULL) {
5285 5286                                  if (IPCL_UDP_MATCH(connp, lport, laddr,
5286 5287                                      fport, faddr) &&
5287 5288                                      conn_wantpacket(connp, ira, ipha) &&
5288 5289                                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5289 5290                                      tsol_receive_local(mp, &laddr, IPV4_VERSION,
5290 5291                                      ira, connp)))
5291 5292                                          break;
5292 5293                                  connp = connp->conn_next;
5293 5294                          }
5294 5295                          if (connp == NULL) {
5295 5296                                  /* No more interested clients */
5296 5297                                  connp = first_connp;
5297 5298                                  break;
5298 5299                          }
5299 5300                          if (((mp1 = dupmsg(mp)) == NULL) &&
5300 5301                              ((mp1 = copymsg(mp)) == NULL)) {
5301 5302                                  /* Memory allocation failed */
5302 5303                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5303 5304                                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
5304 5305                                  connp = first_connp;
5305 5306                                  break;
5306 5307                          }
5307 5308                          CONN_INC_REF(connp);
5308 5309                          mutex_exit(&connfp->connf_lock);
5309 5310  
5310 5311                          IP_STAT(ipst, ip_udp_fanmb);
5311 5312                          ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5312 5313                              NULL, ira);
5313 5314                          mutex_enter(&connfp->connf_lock);
5314 5315                          /* Follow the next pointer before releasing the conn */
5315 5316                          next_connp = connp->conn_next;
5316 5317                          CONN_DEC_REF(connp);
5317 5318                          connp = next_connp;
5318 5319                  }
5319 5320          }
5320 5321  
5321 5322          /* Last one.  Send it upstream. */
5322 5323          mutex_exit(&connfp->connf_lock);
5323 5324          IP_STAT(ipst, ip_udp_fanmb);
5324 5325          ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5325 5326          CONN_DEC_REF(connp);
5326 5327          return;
5327 5328  
5328 5329  notfound:
5329 5330          mutex_exit(&connfp->connf_lock);
5330 5331          /*
5331 5332           * IPv6 endpoints bound to multicast IPv4-mapped addresses
5332 5333           * have already been matched above, since they live in the IPv4
5333 5334           * fanout tables. This implies we only need to
5334 5335           * check for IPv6 in6addr_any endpoints here.
5335 5336           * Thus we compare using ipv6_all_zeros instead of the destination
5336 5337           * address, except for the multicast group membership lookup which
5337 5338           * uses the IPv4 destination.
5338 5339           */
5339 5340          IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5340 5341          connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5341 5342          mutex_enter(&connfp->connf_lock);
5342 5343          connp = connfp->connf_head;
5343 5344          /*
5344 5345           * IPv4 multicast packet being delivered to an AF_INET6
5345 5346           * in6addr_any endpoint.
5346 5347           * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5347 5348           * and not conn_wantpacket_v6() since any multicast membership is
5348 5349           * for an IPv4-mapped multicast address.
5349 5350           */
5350 5351          while (connp != NULL) {
5351 5352                  if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5352 5353                      fport, v6faddr) &&
5353 5354                      conn_wantpacket(connp, ira, ipha) &&
5354 5355                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5355 5356                      tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5356 5357                          break;
5357 5358                  connp = connp->conn_next;
5358 5359          }
5359 5360  
5360 5361          if (connp == NULL) {
5361 5362                  /*
5362 5363                   * No one bound to this port.  Is
5363 5364                   * there a client that wants all
5364 5365                   * unclaimed datagrams?
5365 5366                   */
5366 5367                  mutex_exit(&connfp->connf_lock);
5367 5368  
5368 5369                  if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5369 5370                      NULL) {
5370 5371                          ASSERT(ira->ira_protocol == IPPROTO_UDP);
5371 5372                          ip_fanout_proto_v4(mp, ipha, ira);
5372 5373                  } else {
5373 5374                          /*
5374 5375                           * We used to attempt to send an icmp error here, but
5375 5376                           * since this is known to be a multicast packet
5376 5377                           * and we don't send icmp errors in response to
5377 5378                           * multicast, just drop the packet and give up sooner.
5378 5379                           */
5379 5380                          BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5380 5381                          freemsg(mp);
5381 5382                  }
5382 5383                  return;
5383 5384          }
5384 5385          ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5385 5386  
5386 5387          /*
5387 5388           * If SO_REUSEADDR has been set on the first we send the
5388 5389           * packet to all clients that have joined the group and
5389 5390           * match the port.
5390 5391           */
5391 5392          if (connp->conn_reuseaddr) {
5392 5393                  conn_t          *first_connp = connp;
5393 5394                  conn_t          *next_connp;
5394 5395                  mblk_t          *mp1;
5395 5396  
5396 5397                  CONN_INC_REF(connp);
5397 5398                  connp = connp->conn_next;
5398 5399                  for (;;) {
5399 5400                          while (connp != NULL) {
5400 5401                                  if (IPCL_UDP_MATCH_V6(connp, lport,
5401 5402                                      ipv6_all_zeros, fport, v6faddr) &&
5402 5403                                      conn_wantpacket(connp, ira, ipha) &&
5403 5404                                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5404 5405                                      tsol_receive_local(mp, &laddr, IPV4_VERSION,
5405 5406                                      ira, connp)))
5406 5407                                          break;
5407 5408                                  connp = connp->conn_next;
5408 5409                          }
5409 5410                          if (connp == NULL) {
5410 5411                                  /* No more interested clients */
5411 5412                                  connp = first_connp;
5412 5413                                  break;
5413 5414                          }
5414 5415                          if (((mp1 = dupmsg(mp)) == NULL) &&
5415 5416                              ((mp1 = copymsg(mp)) == NULL)) {
5416 5417                                  /* Memory allocation failed */
5417 5418                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5418 5419                                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
5419 5420                                  connp = first_connp;
5420 5421                                  break;
5421 5422                          }
5422 5423                          CONN_INC_REF(connp);
5423 5424                          mutex_exit(&connfp->connf_lock);
5424 5425  
5425 5426                          IP_STAT(ipst, ip_udp_fanmb);
5426 5427                          ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5427 5428                              NULL, ira);
5428 5429                          mutex_enter(&connfp->connf_lock);
5429 5430                          /* Follow the next pointer before releasing the conn */
5430 5431                          next_connp = connp->conn_next;
5431 5432                          CONN_DEC_REF(connp);
5432 5433                          connp = next_connp;
5433 5434                  }
5434 5435          }
5435 5436  
5436 5437          /* Last one.  Send it upstream. */
5437 5438          mutex_exit(&connfp->connf_lock);
5438 5439          IP_STAT(ipst, ip_udp_fanmb);
5439 5440          ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5440 5441          CONN_DEC_REF(connp);
5441 5442  }
5442 5443  
5443 5444  /*
5444 5445   * Split an incoming packet's IPv4 options into the label and the other options.
5445 5446   * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5446 5447   * clearing out any leftover label or options.
5447 5448   * Otherwise it just makes ipp point into the packet.
5448 5449   *
5449 5450   * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5450 5451   */
5451 5452  int
5452 5453  ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5453 5454  {
5454 5455          uchar_t         *opt;
5455 5456          uint32_t        totallen;
5456 5457          uint32_t        optval;
5457 5458          uint32_t        optlen;
5458 5459  
5459 5460          ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5460 5461          ipp->ipp_hoplimit = ipha->ipha_ttl;
5461 5462          ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5462 5463          IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5463 5464  
5464 5465          /*
5465 5466           * Get length (in 4 byte octets) of IP header options.
5466 5467           */
5467 5468          totallen = ipha->ipha_version_and_hdr_length -
5468 5469              (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5469 5470  
5470 5471          if (totallen == 0) {
5471 5472                  if (!allocate)
5472 5473                          return (0);
5473 5474  
5474 5475                  /* Clear out anything from a previous packet */
5475 5476                  if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5476 5477                          kmem_free(ipp->ipp_ipv4_options,
5477 5478                              ipp->ipp_ipv4_options_len);
5478 5479                          ipp->ipp_ipv4_options = NULL;
5479 5480                          ipp->ipp_ipv4_options_len = 0;
5480 5481                          ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5481 5482                  }
5482 5483                  if (ipp->ipp_fields & IPPF_LABEL_V4) {
5483 5484                          kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5484 5485                          ipp->ipp_label_v4 = NULL;
5485 5486                          ipp->ipp_label_len_v4 = 0;
5486 5487                          ipp->ipp_fields &= ~IPPF_LABEL_V4;
5487 5488                  }
5488 5489                  return (0);
5489 5490          }
5490 5491  
5491 5492          totallen <<= 2;
5492 5493          opt = (uchar_t *)&ipha[1];
5493 5494          if (!is_system_labeled()) {
5494 5495  
5495 5496          copyall:
5496 5497                  if (!allocate) {
5497 5498                          if (totallen != 0) {
5498 5499                                  ipp->ipp_ipv4_options = opt;
5499 5500                                  ipp->ipp_ipv4_options_len = totallen;
5500 5501                                  ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5501 5502                          }
5502 5503                          return (0);
5503 5504                  }
5504 5505                  /* Just copy all of options */
5505 5506                  if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5506 5507                          if (totallen == ipp->ipp_ipv4_options_len) {
5507 5508                                  bcopy(opt, ipp->ipp_ipv4_options, totallen);
5508 5509                                  return (0);
5509 5510                          }
5510 5511                          kmem_free(ipp->ipp_ipv4_options,
5511 5512                              ipp->ipp_ipv4_options_len);
5512 5513                          ipp->ipp_ipv4_options = NULL;
5513 5514                          ipp->ipp_ipv4_options_len = 0;
5514 5515                          ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5515 5516                  }
5516 5517                  if (totallen == 0)
5517 5518                          return (0);
5518 5519  
5519 5520                  ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5520 5521                  if (ipp->ipp_ipv4_options == NULL)
5521 5522                          return (ENOMEM);
5522 5523                  ipp->ipp_ipv4_options_len = totallen;
5523 5524                  ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5524 5525                  bcopy(opt, ipp->ipp_ipv4_options, totallen);
5525 5526                  return (0);
5526 5527          }
5527 5528  
5528 5529          if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) {
5529 5530                  kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5530 5531                  ipp->ipp_label_v4 = NULL;
5531 5532                  ipp->ipp_label_len_v4 = 0;
5532 5533                  ipp->ipp_fields &= ~IPPF_LABEL_V4;
5533 5534          }
5534 5535  
5535 5536          /*
5536 5537           * Search for CIPSO option.
5537 5538           * We assume CIPSO is first in options if it is present.
5538 5539           * If it isn't, then ipp_opt_ipv4_options will not include the options
5539 5540           * prior to the CIPSO option.
5540 5541           */
5541 5542          while (totallen != 0) {
5542 5543                  switch (optval = opt[IPOPT_OPTVAL]) {
5543 5544                  case IPOPT_EOL:
5544 5545                          return (0);
5545 5546                  case IPOPT_NOP:
5546 5547                          optlen = 1;
5547 5548                          break;
5548 5549                  default:
5549 5550                          if (totallen <= IPOPT_OLEN)
5550 5551                                  return (EINVAL);
5551 5552                          optlen = opt[IPOPT_OLEN];
5552 5553                          if (optlen < 2)
5553 5554                                  return (EINVAL);
5554 5555                  }
5555 5556                  if (optlen > totallen)
5556 5557                          return (EINVAL);
5557 5558  
5558 5559                  switch (optval) {
5559 5560                  case IPOPT_COMSEC:
5560 5561                          if (!allocate) {
5561 5562                                  ipp->ipp_label_v4 = opt;
5562 5563                                  ipp->ipp_label_len_v4 = optlen;
5563 5564                                  ipp->ipp_fields |= IPPF_LABEL_V4;
5564 5565                          } else {
5565 5566                                  ipp->ipp_label_v4 = kmem_alloc(optlen,
5566 5567                                      KM_NOSLEEP);
5567 5568                                  if (ipp->ipp_label_v4 == NULL)
5568 5569                                          return (ENOMEM);
5569 5570                                  ipp->ipp_label_len_v4 = optlen;
5570 5571                                  ipp->ipp_fields |= IPPF_LABEL_V4;
5571 5572                                  bcopy(opt, ipp->ipp_label_v4, optlen);
5572 5573                          }
5573 5574                          totallen -= optlen;
5574 5575                          opt += optlen;
5575 5576  
5576 5577                          /* Skip padding bytes until we get to a multiple of 4 */
5577 5578                          while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) {
5578 5579                                  totallen--;
5579 5580                                  opt++;
5580 5581                          }
5581 5582                          /* Remaining as ipp_ipv4_options */
5582 5583                          goto copyall;
5583 5584                  }
5584 5585                  totallen -= optlen;
5585 5586                  opt += optlen;
5586 5587          }
5587 5588          /* No CIPSO found; return everything as ipp_ipv4_options */
5588 5589          totallen = ipha->ipha_version_and_hdr_length -
5589 5590              (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5590 5591          totallen <<= 2;
5591 5592          opt = (uchar_t *)&ipha[1];
5592 5593          goto copyall;
5593 5594  }
5594 5595  
5595 5596  /*
5596 5597   * Efficient versions of lookup for an IRE when we only
5597 5598   * match the address.
5598 5599   * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5599 5600   * Does not handle multicast addresses.
5600 5601   */
5601 5602  uint_t
5602 5603  ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5603 5604  {
5604 5605          ire_t *ire;
5605 5606          uint_t result;
5606 5607  
5607 5608          ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5608 5609          ASSERT(ire != NULL);
5609 5610          if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5610 5611                  result = IRE_NOROUTE;
5611 5612          else
5612 5613                  result = ire->ire_type;
5613 5614          ire_refrele(ire);
5614 5615          return (result);
5615 5616  }
5616 5617  
5617 5618  /*
5618 5619   * Efficient versions of lookup for an IRE when we only
5619 5620   * match the address.
5620 5621   * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5621 5622   * Does not handle multicast addresses.
5622 5623   */
5623 5624  uint_t
5624 5625  ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5625 5626  {
5626 5627          ire_t *ire;
5627 5628          uint_t result;
5628 5629  
5629 5630          ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5630 5631          ASSERT(ire != NULL);
5631 5632          if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5632 5633                  result = IRE_NOROUTE;
5633 5634          else
5634 5635                  result = ire->ire_type;
5635 5636          ire_refrele(ire);
5636 5637          return (result);
5637 5638  }
5638 5639  
5639 5640  /*
5640 5641   * Nobody should be sending
5641 5642   * packets up this stream
5642 5643   */
5643 5644  static void
5644 5645  ip_lrput(queue_t *q, mblk_t *mp)
5645 5646  {
5646 5647          switch (mp->b_datap->db_type) {
5647 5648          case M_FLUSH:
5648 5649                  /* Turn around */
5649 5650                  if (*mp->b_rptr & FLUSHW) {
5650 5651                          *mp->b_rptr &= ~FLUSHR;
5651 5652                          qreply(q, mp);
5652 5653                          return;
5653 5654                  }
5654 5655                  break;
5655 5656          }
5656 5657          freemsg(mp);
5657 5658  }
5658 5659  
5659 5660  /* Nobody should be sending packets down this stream */
5660 5661  /* ARGSUSED */
5661 5662  void
5662 5663  ip_lwput(queue_t *q, mblk_t *mp)
5663 5664  {
5664 5665          freemsg(mp);
5665 5666  }
5666 5667  
5667 5668  /*
5668 5669   * Move the first hop in any source route to ipha_dst and remove that part of
5669 5670   * the source route.  Called by other protocols.  Errors in option formatting
5670 5671   * are ignored - will be handled by ip_output_options. Return the final
5671 5672   * destination (either ipha_dst or the last entry in a source route.)
5672 5673   */
5673 5674  ipaddr_t
5674 5675  ip_massage_options(ipha_t *ipha, netstack_t *ns)
5675 5676  {
5676 5677          ipoptp_t        opts;
5677 5678          uchar_t         *opt;
5678 5679          uint8_t         optval;
5679 5680          uint8_t         optlen;
5680 5681          ipaddr_t        dst;
5681 5682          int             i;
5682 5683          ip_stack_t      *ipst = ns->netstack_ip;
5683 5684  
5684 5685          ip2dbg(("ip_massage_options\n"));
5685 5686          dst = ipha->ipha_dst;
5686 5687          for (optval = ipoptp_first(&opts, ipha);
5687 5688              optval != IPOPT_EOL;
5688 5689              optval = ipoptp_next(&opts)) {
5689 5690                  opt = opts.ipoptp_cur;
5690 5691                  switch (optval) {
5691 5692                          uint8_t off;
5692 5693                  case IPOPT_SSRR:
5693 5694                  case IPOPT_LSRR:
5694 5695                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5695 5696                                  ip1dbg(("ip_massage_options: bad src route\n"));
5696 5697                                  break;
5697 5698                          }
5698 5699                          optlen = opts.ipoptp_len;
5699 5700                          off = opt[IPOPT_OFFSET];
5700 5701                          off--;
5701 5702                  redo_srr:
5702 5703                          if (optlen < IP_ADDR_LEN ||
5703 5704                              off > optlen - IP_ADDR_LEN) {
5704 5705                                  /* End of source route */
5705 5706                                  ip1dbg(("ip_massage_options: end of SR\n"));
5706 5707                                  break;
5707 5708                          }
5708 5709                          bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5709 5710                          ip1dbg(("ip_massage_options: next hop 0x%x\n",
5710 5711                              ntohl(dst)));
5711 5712                          /*
5712 5713                           * Check if our address is present more than
5713 5714                           * once as consecutive hops in source route.
5714 5715                           * XXX verify per-interface ip_forwarding
5715 5716                           * for source route?
5716 5717                           */
5717 5718                          if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5718 5719                                  off += IP_ADDR_LEN;
5719 5720                                  goto redo_srr;
5720 5721                          }
5721 5722                          if (dst == htonl(INADDR_LOOPBACK)) {
5722 5723                                  ip1dbg(("ip_massage_options: loopback addr in "
5723 5724                                      "source route!\n"));
5724 5725                                  break;
5725 5726                          }
5726 5727                          /*
5727 5728                           * Update ipha_dst to be the first hop and remove the
5728 5729                           * first hop from the source route (by overwriting
5729 5730                           * part of the option with NOP options).
5730 5731                           */
5731 5732                          ipha->ipha_dst = dst;
5732 5733                          /* Put the last entry in dst */
5733 5734                          off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5734 5735                              3;
5735 5736                          bcopy(&opt[off], &dst, IP_ADDR_LEN);
5736 5737  
5737 5738                          ip1dbg(("ip_massage_options: last hop 0x%x\n",
5738 5739                              ntohl(dst)));
5739 5740                          /* Move down and overwrite */
5740 5741                          opt[IP_ADDR_LEN] = opt[0];
5741 5742                          opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5742 5743                          opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5743 5744                          for (i = 0; i < IP_ADDR_LEN; i++)
5744 5745                                  opt[i] = IPOPT_NOP;
5745 5746                          break;
5746 5747                  }
5747 5748          }
5748 5749          return (dst);
5749 5750  }
5750 5751  
5751 5752  /*
5752 5753   * Return the network mask
5753 5754   * associated with the specified address.
5754 5755   */
5755 5756  ipaddr_t
5756 5757  ip_net_mask(ipaddr_t addr)
5757 5758  {
5758 5759          uchar_t *up = (uchar_t *)&addr;
5759 5760          ipaddr_t mask = 0;
5760 5761          uchar_t *maskp = (uchar_t *)&mask;
5761 5762  
5762 5763  #if defined(__i386) || defined(__amd64)
5763 5764  #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5764 5765  #endif
5765 5766  #ifdef  TOTALLY_BRAIN_DAMAGED_C_COMPILER
5766 5767          maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5767 5768  #endif
5768 5769          if (CLASSD(addr)) {
5769 5770                  maskp[0] = 0xF0;
5770 5771                  return (mask);
5771 5772          }
5772 5773  
5773 5774          /* We assume Class E default netmask to be 32 */
5774 5775          if (CLASSE(addr))
5775 5776                  return (0xffffffffU);
5776 5777  
5777 5778          if (addr == 0)
5778 5779                  return (0);
5779 5780          maskp[0] = 0xFF;
5780 5781          if ((up[0] & 0x80) == 0)
5781 5782                  return (mask);
5782 5783  
5783 5784          maskp[1] = 0xFF;
5784 5785          if ((up[0] & 0xC0) == 0x80)
5785 5786                  return (mask);
5786 5787  
5787 5788          maskp[2] = 0xFF;
5788 5789          if ((up[0] & 0xE0) == 0xC0)
5789 5790                  return (mask);
5790 5791  
5791 5792          /* Otherwise return no mask */
5792 5793          return ((ipaddr_t)0);
5793 5794  }
5794 5795  
5795 5796  /* Name/Value Table Lookup Routine */
5796 5797  char *
5797 5798  ip_nv_lookup(nv_t *nv, int value)
5798 5799  {
5799 5800          if (!nv)
5800 5801                  return (NULL);
5801 5802          for (; nv->nv_name; nv++) {
5802 5803                  if (nv->nv_value == value)
5803 5804                          return (nv->nv_name);
5804 5805          }
5805 5806          return ("unknown");
5806 5807  }
5807 5808  
5808 5809  static int
5809 5810  ip_wait_for_info_ack(ill_t *ill)
5810 5811  {
5811 5812          int err;
5812 5813  
5813 5814          mutex_enter(&ill->ill_lock);
5814 5815          while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5815 5816                  /*
5816 5817                   * Return value of 0 indicates a pending signal.
5817 5818                   */
5818 5819                  err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5819 5820                  if (err == 0) {
5820 5821                          mutex_exit(&ill->ill_lock);
5821 5822                          return (EINTR);
5822 5823                  }
5823 5824          }
5824 5825          mutex_exit(&ill->ill_lock);
5825 5826          /*
5826 5827           * ip_rput_other could have set an error  in ill_error on
5827 5828           * receipt of M_ERROR.
5828 5829           */
5829 5830          return (ill->ill_error);
5830 5831  }
5831 5832  
5832 5833  /*
5833 5834   * This is a module open, i.e. this is a control stream for access
5834 5835   * to a DLPI device.  We allocate an ill_t as the instance data in
5835 5836   * this case.
5836 5837   */
5837 5838  static int
5838 5839  ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5839 5840  {
5840 5841          ill_t   *ill;
5841 5842          int     err;
5842 5843          zoneid_t zoneid;
5843 5844          netstack_t *ns;
5844 5845          ip_stack_t *ipst;
5845 5846  
5846 5847          /*
5847 5848           * Prevent unprivileged processes from pushing IP so that
5848 5849           * they can't send raw IP.
5849 5850           */
5850 5851          if (secpolicy_net_rawaccess(credp) != 0)
5851 5852                  return (EPERM);
5852 5853  
5853 5854          ns = netstack_find_by_cred(credp);
5854 5855          ASSERT(ns != NULL);
5855 5856          ipst = ns->netstack_ip;
5856 5857          ASSERT(ipst != NULL);
5857 5858  
5858 5859          /*
5859 5860           * For exclusive stacks we set the zoneid to zero
5860 5861           * to make IP operate as if in the global zone.
5861 5862           */
5862 5863          if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5863 5864                  zoneid = GLOBAL_ZONEID;
5864 5865          else
5865 5866                  zoneid = crgetzoneid(credp);
5866 5867  
5867 5868          ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5868 5869          q->q_ptr = WR(q)->q_ptr = ill;
5869 5870          ill->ill_ipst = ipst;
5870 5871          ill->ill_zoneid = zoneid;
5871 5872  
5872 5873          /*
5873 5874           * ill_init initializes the ill fields and then sends down
5874 5875           * down a DL_INFO_REQ after calling qprocson.
5875 5876           */
5876 5877          err = ill_init(q, ill);
5877 5878  
5878 5879          if (err != 0) {
5879 5880                  mi_free(ill);
5880 5881                  netstack_rele(ipst->ips_netstack);
5881 5882                  q->q_ptr = NULL;
5882 5883                  WR(q)->q_ptr = NULL;
5883 5884                  return (err);
5884 5885          }
5885 5886  
5886 5887          /*
5887 5888           * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5888 5889           *
5889 5890           * ill_init initializes the ipsq marking this thread as
5890 5891           * writer
5891 5892           */
5892 5893          ipsq_exit(ill->ill_phyint->phyint_ipsq);
5893 5894          err = ip_wait_for_info_ack(ill);
5894 5895          if (err == 0)
5895 5896                  ill->ill_credp = credp;
5896 5897          else
5897 5898                  goto fail;
5898 5899  
5899 5900          crhold(credp);
5900 5901  
5901 5902          mutex_enter(&ipst->ips_ip_mi_lock);
5902 5903          err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5903 5904              sflag, credp);
5904 5905          mutex_exit(&ipst->ips_ip_mi_lock);
5905 5906  fail:
5906 5907          if (err) {
5907 5908                  (void) ip_close(q, 0);
5908 5909                  return (err);
5909 5910          }
5910 5911          return (0);
5911 5912  }
5912 5913  
5913 5914  /* For /dev/ip aka AF_INET open */
5914 5915  int
5915 5916  ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5916 5917  {
5917 5918          return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5918 5919  }
5919 5920  
5920 5921  /* For /dev/ip6 aka AF_INET6 open */
5921 5922  int
5922 5923  ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5923 5924  {
5924 5925          return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5925 5926  }
5926 5927  
5927 5928  /* IP open routine. */
5928 5929  int
5929 5930  ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5930 5931      boolean_t isv6)
5931 5932  {
5932 5933          conn_t          *connp;
5933 5934          major_t         maj;
5934 5935          zoneid_t        zoneid;
5935 5936          netstack_t      *ns;
5936 5937          ip_stack_t      *ipst;
5937 5938  
5938 5939          /* Allow reopen. */
5939 5940          if (q->q_ptr != NULL)
5940 5941                  return (0);
5941 5942  
5942 5943          if (sflag & MODOPEN) {
5943 5944                  /* This is a module open */
5944 5945                  return (ip_modopen(q, devp, flag, sflag, credp));
5945 5946          }
5946 5947  
5947 5948          if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5948 5949                  /*
5949 5950                   * Non streams based socket looking for a stream
5950 5951                   * to access IP
5951 5952                   */
5952 5953                  return (ip_helper_stream_setup(q, devp, flag, sflag,
5953 5954                      credp, isv6));
5954 5955          }
5955 5956  
5956 5957          ns = netstack_find_by_cred(credp);
5957 5958          ASSERT(ns != NULL);
5958 5959          ipst = ns->netstack_ip;
5959 5960          ASSERT(ipst != NULL);
5960 5961  
5961 5962          /*
5962 5963           * For exclusive stacks we set the zoneid to zero
5963 5964           * to make IP operate as if in the global zone.
5964 5965           */
5965 5966          if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5966 5967                  zoneid = GLOBAL_ZONEID;
5967 5968          else
5968 5969                  zoneid = crgetzoneid(credp);
5969 5970  
5970 5971          /*
5971 5972           * We are opening as a device. This is an IP client stream, and we
5972 5973           * allocate an conn_t as the instance data.
5973 5974           */
5974 5975          connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
5975 5976  
5976 5977          /*
5977 5978           * ipcl_conn_create did a netstack_hold. Undo the hold that was
5978 5979           * done by netstack_find_by_cred()
5979 5980           */
5980 5981          netstack_rele(ipst->ips_netstack);
5981 5982  
5982 5983          connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
5983 5984          /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
5984 5985          connp->conn_ixa->ixa_zoneid = zoneid;
5985 5986          connp->conn_zoneid = zoneid;
5986 5987  
5987 5988          connp->conn_rq = q;
5988 5989          q->q_ptr = WR(q)->q_ptr = connp;
5989 5990  
5990 5991          /* Minor tells us which /dev entry was opened */
5991 5992          if (isv6) {
5992 5993                  connp->conn_family = AF_INET6;
5993 5994                  connp->conn_ipversion = IPV6_VERSION;
5994 5995                  connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
5995 5996                  connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
5996 5997          } else {
5997 5998                  connp->conn_family = AF_INET;
5998 5999                  connp->conn_ipversion = IPV4_VERSION;
5999 6000                  connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
6000 6001          }
6001 6002  
6002 6003          if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
6003 6004              ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
6004 6005                  connp->conn_minor_arena = ip_minor_arena_la;
6005 6006          } else {
6006 6007                  /*
6007 6008                   * Either minor numbers in the large arena were exhausted
6008 6009                   * or a non socket application is doing the open.
6009 6010                   * Try to allocate from the small arena.
6010 6011                   */
6011 6012                  if ((connp->conn_dev =
6012 6013                      inet_minor_alloc(ip_minor_arena_sa)) == 0) {
6013 6014                          /* CONN_DEC_REF takes care of netstack_rele() */
6014 6015                          q->q_ptr = WR(q)->q_ptr = NULL;
6015 6016                          CONN_DEC_REF(connp);
6016 6017                          return (EBUSY);
6017 6018                  }
6018 6019                  connp->conn_minor_arena = ip_minor_arena_sa;
6019 6020          }
6020 6021  
6021 6022          maj = getemajor(*devp);
6022 6023          *devp = makedevice(maj, (minor_t)connp->conn_dev);
6023 6024  
6024 6025          /*
6025 6026           * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
6026 6027           */
6027 6028          connp->conn_cred = credp;
6028 6029          connp->conn_cpid = curproc->p_pid;
6029 6030          /* Cache things in ixa without an extra refhold */
6030 6031          ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
6031 6032          connp->conn_ixa->ixa_cred = connp->conn_cred;
6032 6033          connp->conn_ixa->ixa_cpid = connp->conn_cpid;
6033 6034          if (is_system_labeled())
6034 6035                  connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6035 6036  
6036 6037          /*
6037 6038           * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
6038 6039           */
6039 6040          connp->conn_recv = ip_conn_input;
6040 6041          connp->conn_recvicmp = ip_conn_input_icmp;
6041 6042  
6042 6043          crhold(connp->conn_cred);
6043 6044  
6044 6045          /*
6045 6046           * If the caller has the process-wide flag set, then default to MAC
6046 6047           * exempt mode.  This allows read-down to unlabeled hosts.
6047 6048           */
6048 6049          if (getpflags(NET_MAC_AWARE, credp) != 0)
6049 6050                  connp->conn_mac_mode = CONN_MAC_AWARE;
6050 6051  
6051 6052          connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
6052 6053  
6053 6054          connp->conn_rq = q;
6054 6055          connp->conn_wq = WR(q);
6055 6056  
6056 6057          /* Non-zero default values */
6057 6058          connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
6058 6059  
6059 6060          /*
6060 6061           * Make the conn globally visible to walkers
6061 6062           */
6062 6063          ASSERT(connp->conn_ref == 1);
6063 6064          mutex_enter(&connp->conn_lock);
6064 6065          connp->conn_state_flags &= ~CONN_INCIPIENT;
6065 6066          mutex_exit(&connp->conn_lock);
6066 6067  
6067 6068          qprocson(q);
6068 6069  
6069 6070          return (0);
6070 6071  }
6071 6072  
6072 6073  /*
6073 6074   * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
6074 6075   * all of them are copied to the conn_t. If the req is "zero", the policy is
6075 6076   * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
6076 6077   * fields.
6077 6078   * We keep only the latest setting of the policy and thus policy setting
6078 6079   * is not incremental/cumulative.
6079 6080   *
6080 6081   * Requests to set policies with multiple alternative actions will
6081 6082   * go through a different API.
6082 6083   */
6083 6084  int
6084 6085  ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
6085 6086  {
6086 6087          uint_t ah_req = 0;
6087 6088          uint_t esp_req = 0;
6088 6089          uint_t se_req = 0;
6089 6090          ipsec_act_t *actp = NULL;
6090 6091          uint_t nact;
6091 6092          ipsec_policy_head_t *ph;
6092 6093          boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
6093 6094          int error = 0;
6094 6095          netstack_t      *ns = connp->conn_netstack;
6095 6096          ip_stack_t      *ipst = ns->netstack_ip;
6096 6097          ipsec_stack_t   *ipss = ns->netstack_ipsec;
6097 6098  
6098 6099  #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
6099 6100  
6100 6101          /*
6101 6102           * The IP_SEC_OPT option does not allow variable length parameters,
6102 6103           * hence a request cannot be NULL.
6103 6104           */
6104 6105          if (req == NULL)
6105 6106                  return (EINVAL);
6106 6107  
6107 6108          ah_req = req->ipsr_ah_req;
6108 6109          esp_req = req->ipsr_esp_req;
6109 6110          se_req = req->ipsr_self_encap_req;
6110 6111  
6111 6112          /* Don't allow setting self-encap without one or more of AH/ESP. */
6112 6113          if (se_req != 0 && esp_req == 0 && ah_req == 0)
6113 6114                  return (EINVAL);
6114 6115  
6115 6116          /*
6116 6117           * Are we dealing with a request to reset the policy (i.e.
6117 6118           * zero requests).
6118 6119           */
6119 6120          is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
6120 6121              (esp_req & REQ_MASK) == 0 &&
6121 6122              (se_req & REQ_MASK) == 0);
6122 6123  
6123 6124          if (!is_pol_reset) {
6124 6125                  /*
6125 6126                   * If we couldn't load IPsec, fail with "protocol
6126 6127                   * not supported".
6127 6128                   * IPsec may not have been loaded for a request with zero
6128 6129                   * policies, so we don't fail in this case.
6129 6130                   */
6130 6131                  mutex_enter(&ipss->ipsec_loader_lock);
6131 6132                  if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
6132 6133                          mutex_exit(&ipss->ipsec_loader_lock);
6133 6134                          return (EPROTONOSUPPORT);
6134 6135                  }
6135 6136                  mutex_exit(&ipss->ipsec_loader_lock);
6136 6137  
6137 6138                  /*
6138 6139                   * Test for valid requests. Invalid algorithms
6139 6140                   * need to be tested by IPsec code because new
6140 6141                   * algorithms can be added dynamically.
6141 6142                   */
6142 6143                  if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6143 6144                      (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6144 6145                      (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
6145 6146                          return (EINVAL);
6146 6147                  }
6147 6148  
6148 6149                  /*
6149 6150                   * Only privileged users can issue these
6150 6151                   * requests.
6151 6152                   */
6152 6153                  if (((ah_req & IPSEC_PREF_NEVER) ||
6153 6154                      (esp_req & IPSEC_PREF_NEVER) ||
6154 6155                      (se_req & IPSEC_PREF_NEVER)) &&
6155 6156                      secpolicy_ip_config(cr, B_FALSE) != 0) {
6156 6157                          return (EPERM);
6157 6158                  }
6158 6159  
6159 6160                  /*
6160 6161                   * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
6161 6162                   * are mutually exclusive.
6162 6163                   */
6163 6164                  if (((ah_req & REQ_MASK) == REQ_MASK) ||
6164 6165                      ((esp_req & REQ_MASK) == REQ_MASK) ||
6165 6166                      ((se_req & REQ_MASK) == REQ_MASK)) {
6166 6167                          /* Both of them are set */
6167 6168                          return (EINVAL);
6168 6169                  }
6169 6170          }
6170 6171  
6171 6172          ASSERT(MUTEX_HELD(&connp->conn_lock));
6172 6173  
6173 6174          /*
6174 6175           * If we have already cached policies in conn_connect(), don't
6175 6176           * let them change now. We cache policies for connections
6176 6177           * whose src,dst [addr, port] is known.
6177 6178           */
6178 6179          if (connp->conn_policy_cached) {
6179 6180                  return (EINVAL);
6180 6181          }
6181 6182  
6182 6183          /*
6183 6184           * We have a zero policies, reset the connection policy if already
6184 6185           * set. This will cause the connection to inherit the
6185 6186           * global policy, if any.
6186 6187           */
6187 6188          if (is_pol_reset) {
6188 6189                  if (connp->conn_policy != NULL) {
6189 6190                          IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
6190 6191                          connp->conn_policy = NULL;
6191 6192                  }
6192 6193                  connp->conn_in_enforce_policy = B_FALSE;
6193 6194                  connp->conn_out_enforce_policy = B_FALSE;
6194 6195                  return (0);
6195 6196          }
6196 6197  
6197 6198          ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
6198 6199              ipst->ips_netstack);
6199 6200          if (ph == NULL)
6200 6201                  goto enomem;
6201 6202  
6202 6203          ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
6203 6204          if (actp == NULL)
6204 6205                  goto enomem;
6205 6206  
6206 6207          /*
6207 6208           * Always insert IPv4 policy entries, since they can also apply to
6208 6209           * ipv6 sockets being used in ipv4-compat mode.
6209 6210           */
6210 6211          if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6211 6212              IPSEC_TYPE_INBOUND, ns))
6212 6213                  goto enomem;
6213 6214          is_pol_inserted = B_TRUE;
6214 6215          if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6215 6216              IPSEC_TYPE_OUTBOUND, ns))
6216 6217                  goto enomem;
6217 6218  
6218 6219          /*
6219 6220           * We're looking at a v6 socket, also insert the v6-specific
6220 6221           * entries.
6221 6222           */
6222 6223          if (connp->conn_family == AF_INET6) {
6223 6224                  if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6224 6225                      IPSEC_TYPE_INBOUND, ns))
6225 6226                          goto enomem;
6226 6227                  if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6227 6228                      IPSEC_TYPE_OUTBOUND, ns))
6228 6229                          goto enomem;
6229 6230          }
6230 6231  
6231 6232          ipsec_actvec_free(actp, nact);
6232 6233  
6233 6234          /*
6234 6235           * If the requests need security, set enforce_policy.
6235 6236           * If the requests are IPSEC_PREF_NEVER, one should
6236 6237           * still set conn_out_enforce_policy so that ip_set_destination
6237 6238           * marks the ip_xmit_attr_t appropriatly. This is needed so that
6238 6239           * for connections that we don't cache policy in at connect time,
6239 6240           * if global policy matches in ip_output_attach_policy, we
6240 6241           * don't wrongly inherit global policy. Similarly, we need
6241 6242           * to set conn_in_enforce_policy also so that we don't verify
6242 6243           * policy wrongly.
6243 6244           */
6244 6245          if ((ah_req & REQ_MASK) != 0 ||
6245 6246              (esp_req & REQ_MASK) != 0 ||
6246 6247              (se_req & REQ_MASK) != 0) {
6247 6248                  connp->conn_in_enforce_policy = B_TRUE;
6248 6249                  connp->conn_out_enforce_policy = B_TRUE;
6249 6250          }
6250 6251  
6251 6252          return (error);
6252 6253  #undef REQ_MASK
6253 6254  
6254 6255          /*
6255 6256           * Common memory-allocation-failure exit path.
6256 6257           */
6257 6258  enomem:
6258 6259          if (actp != NULL)
6259 6260                  ipsec_actvec_free(actp, nact);
6260 6261          if (is_pol_inserted)
6261 6262                  ipsec_polhead_flush(ph, ns);
6262 6263          return (ENOMEM);
6263 6264  }
6264 6265  
6265 6266  /*
6266 6267   * Set socket options for joining and leaving multicast groups.
6267 6268   * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6268 6269   * The caller has already check that the option name is consistent with
6269 6270   * the address family of the socket.
6270 6271   */
6271 6272  int
6272 6273  ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6273 6274      uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6274 6275  {
6275 6276          int             *i1 = (int *)invalp;
6276 6277          int             error = 0;
6277 6278          ip_stack_t      *ipst = connp->conn_netstack->netstack_ip;
6278 6279          struct ip_mreq  *v4_mreqp;
6279 6280          struct ipv6_mreq *v6_mreqp;
6280 6281          struct group_req *greqp;
6281 6282          ire_t *ire;
6282 6283          boolean_t done = B_FALSE;
6283 6284          ipaddr_t ifaddr;
6284 6285          in6_addr_t v6group;
6285 6286          uint_t ifindex;
6286 6287          boolean_t mcast_opt = B_TRUE;
6287 6288          mcast_record_t fmode;
6288 6289          int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6289 6290              ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6290 6291  
6291 6292          switch (name) {
6292 6293          case IP_ADD_MEMBERSHIP:
6293 6294          case IPV6_JOIN_GROUP:
6294 6295                  mcast_opt = B_FALSE;
6295 6296                  /* FALLTHRU */
6296 6297          case MCAST_JOIN_GROUP:
6297 6298                  fmode = MODE_IS_EXCLUDE;
6298 6299                  optfn = ip_opt_add_group;
6299 6300                  break;
6300 6301  
6301 6302          case IP_DROP_MEMBERSHIP:
6302 6303          case IPV6_LEAVE_GROUP:
6303 6304                  mcast_opt = B_FALSE;
6304 6305                  /* FALLTHRU */
6305 6306          case MCAST_LEAVE_GROUP:
6306 6307                  fmode = MODE_IS_INCLUDE;
6307 6308                  optfn = ip_opt_delete_group;
6308 6309                  break;
6309 6310          default:
6310 6311                  ASSERT(0);
6311 6312          }
6312 6313  
6313 6314          if (mcast_opt) {
6314 6315                  struct sockaddr_in *sin;
6315 6316                  struct sockaddr_in6 *sin6;
6316 6317  
6317 6318                  greqp = (struct group_req *)i1;
6318 6319                  if (greqp->gr_group.ss_family == AF_INET) {
6319 6320                          sin = (struct sockaddr_in *)&(greqp->gr_group);
6320 6321                          IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6321 6322                  } else {
6322 6323                          if (!inet6)
6323 6324                                  return (EINVAL);        /* Not on INET socket */
6324 6325  
6325 6326                          sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6326 6327                          v6group = sin6->sin6_addr;
6327 6328                  }
6328 6329                  ifaddr = INADDR_ANY;
6329 6330                  ifindex = greqp->gr_interface;
6330 6331          } else if (inet6) {
6331 6332                  v6_mreqp = (struct ipv6_mreq *)i1;
6332 6333                  v6group = v6_mreqp->ipv6mr_multiaddr;
6333 6334                  ifaddr = INADDR_ANY;
6334 6335                  ifindex = v6_mreqp->ipv6mr_interface;
6335 6336          } else {
6336 6337                  v4_mreqp = (struct ip_mreq *)i1;
6337 6338                  IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6338 6339                  ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6339 6340                  ifindex = 0;
6340 6341          }
6341 6342  
6342 6343          /*
6343 6344           * In the multirouting case, we need to replicate
6344 6345           * the request on all interfaces that will take part
6345 6346           * in replication.  We do so because multirouting is
6346 6347           * reflective, thus we will probably receive multi-
6347 6348           * casts on those interfaces.
6348 6349           * The ip_multirt_apply_membership() succeeds if
6349 6350           * the operation succeeds on at least one interface.
6350 6351           */
6351 6352          if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6352 6353                  ipaddr_t group;
6353 6354  
6354 6355                  IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6355 6356  
6356 6357                  ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6357 6358                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6358 6359                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6359 6360          } else {
6360 6361                  ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6361 6362                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6362 6363                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6363 6364          }
6364 6365          if (ire != NULL) {
6365 6366                  if (ire->ire_flags & RTF_MULTIRT) {
6366 6367                          error = ip_multirt_apply_membership(optfn, ire, connp,
6367 6368                              checkonly, &v6group, fmode, &ipv6_all_zeros);
6368 6369                          done = B_TRUE;
6369 6370                  }
6370 6371                  ire_refrele(ire);
6371 6372          }
6372 6373  
6373 6374          if (!done) {
6374 6375                  error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6375 6376                      fmode, &ipv6_all_zeros);
6376 6377          }
6377 6378          return (error);
6378 6379  }
6379 6380  
6380 6381  /*
6381 6382   * Set socket options for joining and leaving multicast groups
6382 6383   * for specific sources.
6383 6384   * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6384 6385   * The caller has already check that the option name is consistent with
6385 6386   * the address family of the socket.
6386 6387   */
6387 6388  int
6388 6389  ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6389 6390      uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6390 6391  {
6391 6392          int             *i1 = (int *)invalp;
6392 6393          int             error = 0;
6393 6394          ip_stack_t      *ipst = connp->conn_netstack->netstack_ip;
6394 6395          struct ip_mreq_source *imreqp;
6395 6396          struct group_source_req *gsreqp;
6396 6397          in6_addr_t v6group, v6src;
6397 6398          uint32_t ifindex;
6398 6399          ipaddr_t ifaddr;
6399 6400          boolean_t mcast_opt = B_TRUE;
6400 6401          mcast_record_t fmode;
6401 6402          ire_t *ire;
6402 6403          boolean_t done = B_FALSE;
6403 6404          int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6404 6405              ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6405 6406  
6406 6407          switch (name) {
6407 6408          case IP_BLOCK_SOURCE:
6408 6409                  mcast_opt = B_FALSE;
6409 6410                  /* FALLTHRU */
6410 6411          case MCAST_BLOCK_SOURCE:
6411 6412                  fmode = MODE_IS_EXCLUDE;
6412 6413                  optfn = ip_opt_add_group;
6413 6414                  break;
6414 6415  
6415 6416          case IP_UNBLOCK_SOURCE:
6416 6417                  mcast_opt = B_FALSE;
6417 6418                  /* FALLTHRU */
6418 6419          case MCAST_UNBLOCK_SOURCE:
6419 6420                  fmode = MODE_IS_EXCLUDE;
6420 6421                  optfn = ip_opt_delete_group;
6421 6422                  break;
6422 6423  
6423 6424          case IP_ADD_SOURCE_MEMBERSHIP:
6424 6425                  mcast_opt = B_FALSE;
6425 6426                  /* FALLTHRU */
6426 6427          case MCAST_JOIN_SOURCE_GROUP:
6427 6428                  fmode = MODE_IS_INCLUDE;
6428 6429                  optfn = ip_opt_add_group;
6429 6430                  break;
6430 6431  
6431 6432          case IP_DROP_SOURCE_MEMBERSHIP:
6432 6433                  mcast_opt = B_FALSE;
6433 6434                  /* FALLTHRU */
6434 6435          case MCAST_LEAVE_SOURCE_GROUP:
6435 6436                  fmode = MODE_IS_INCLUDE;
6436 6437                  optfn = ip_opt_delete_group;
6437 6438                  break;
6438 6439          default:
6439 6440                  ASSERT(0);
6440 6441          }
6441 6442  
6442 6443          if (mcast_opt) {
6443 6444                  gsreqp = (struct group_source_req *)i1;
6444 6445                  ifindex = gsreqp->gsr_interface;
6445 6446                  if (gsreqp->gsr_group.ss_family == AF_INET) {
6446 6447                          struct sockaddr_in *s;
6447 6448                          s = (struct sockaddr_in *)&gsreqp->gsr_group;
6448 6449                          IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6449 6450                          s = (struct sockaddr_in *)&gsreqp->gsr_source;
6450 6451                          IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6451 6452                  } else {
6452 6453                          struct sockaddr_in6 *s6;
6453 6454  
6454 6455                          if (!inet6)
6455 6456                                  return (EINVAL);        /* Not on INET socket */
6456 6457  
6457 6458                          s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6458 6459                          v6group = s6->sin6_addr;
6459 6460                          s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6460 6461                          v6src = s6->sin6_addr;
6461 6462                  }
6462 6463                  ifaddr = INADDR_ANY;
6463 6464          } else {
6464 6465                  imreqp = (struct ip_mreq_source *)i1;
6465 6466                  IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6466 6467                  IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6467 6468                  ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6468 6469                  ifindex = 0;
6469 6470          }
6470 6471  
6471 6472          /*
6472 6473           * Handle src being mapped INADDR_ANY by changing it to unspecified.
6473 6474           */
6474 6475          if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6475 6476                  v6src = ipv6_all_zeros;
6476 6477  
6477 6478          /*
6478 6479           * In the multirouting case, we need to replicate
6479 6480           * the request as noted in the mcast cases above.
6480 6481           */
6481 6482          if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6482 6483                  ipaddr_t group;
6483 6484  
6484 6485                  IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6485 6486  
6486 6487                  ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6487 6488                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6488 6489                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6489 6490          } else {
6490 6491                  ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6491 6492                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6492 6493                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6493 6494          }
6494 6495          if (ire != NULL) {
6495 6496                  if (ire->ire_flags & RTF_MULTIRT) {
6496 6497                          error = ip_multirt_apply_membership(optfn, ire, connp,
6497 6498                              checkonly, &v6group, fmode, &v6src);
6498 6499                          done = B_TRUE;
6499 6500                  }
6500 6501                  ire_refrele(ire);
6501 6502          }
6502 6503          if (!done) {
6503 6504                  error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6504 6505                      fmode, &v6src);
6505 6506          }
6506 6507          return (error);
6507 6508  }
6508 6509  
6509 6510  /*
6510 6511   * Given a destination address and a pointer to where to put the information
6511 6512   * this routine fills in the mtuinfo.
6512 6513   * The socket must be connected.
6513 6514   * For sctp conn_faddr is the primary address.
6514 6515   */
6515 6516  int
6516 6517  ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6517 6518  {
6518 6519          uint32_t        pmtu = IP_MAXPACKET;
6519 6520          uint_t          scopeid;
6520 6521  
6521 6522          if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6522 6523                  return (-1);
6523 6524  
6524 6525          /* In case we never sent or called ip_set_destination_v4/v6 */
6525 6526          if (ixa->ixa_ire != NULL)
6526 6527                  pmtu = ip_get_pmtu(ixa);
6527 6528  
6528 6529          if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6529 6530                  scopeid = ixa->ixa_scopeid;
6530 6531          else
6531 6532                  scopeid = 0;
6532 6533  
6533 6534          bzero(mtuinfo, sizeof (*mtuinfo));
6534 6535          mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6535 6536          mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6536 6537          mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6537 6538          mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6538 6539          mtuinfo->ip6m_mtu = pmtu;
6539 6540  
6540 6541          return (sizeof (struct ip6_mtuinfo));
6541 6542  }
6542 6543  
6543 6544  /*
6544 6545   * When the src multihoming is changed from weak to [strong, preferred]
6545 6546   * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6546 6547   * and identify routes that were created by user-applications in the
6547 6548   * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6548 6549   * currently defined. These routes are then 'rebound', i.e., their ire_ill
6549 6550   * is selected by finding an interface route for the gateway.
6550 6551   */
6551 6552  /* ARGSUSED */
6552 6553  void
6553 6554  ip_ire_rebind_walker(ire_t *ire, void *notused)
6554 6555  {
6555 6556          if (!ire->ire_unbound || ire->ire_ill != NULL)
6556 6557                  return;
6557 6558          ire_rebind(ire);
6558 6559          ire_delete(ire);
6559 6560  }
6560 6561  
6561 6562  /*
6562 6563   * When the src multihoming is changed from  [strong, preferred] to weak,
6563 6564   * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6564 6565   * set any entries that were created by user-applications in the unbound state
6565 6566   * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6566 6567   */
6567 6568  /* ARGSUSED */
6568 6569  void
6569 6570  ip_ire_unbind_walker(ire_t *ire, void *notused)
6570 6571  {
6571 6572          ire_t *new_ire;
6572 6573  
6573 6574          if (!ire->ire_unbound || ire->ire_ill == NULL)
6574 6575                  return;
6575 6576          if (ire->ire_ipversion == IPV6_VERSION) {
6576 6577                  new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6577 6578                      &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6578 6579                      ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6579 6580          } else {
6580 6581                  new_ire = ire_create((uchar_t *)&ire->ire_addr,
6581 6582                      (uchar_t *)&ire->ire_mask,
6582 6583                      (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6583 6584                      ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6584 6585          }
6585 6586          if (new_ire == NULL)
6586 6587                  return;
6587 6588          new_ire->ire_unbound = B_TRUE;
6588 6589          /*
6589 6590           * The bound ire must first be deleted so that we don't return
6590 6591           * the existing one on the attempt to add the unbound new_ire.
6591 6592           */
6592 6593          ire_delete(ire);
6593 6594          new_ire = ire_add(new_ire);
6594 6595          if (new_ire != NULL)
6595 6596                  ire_refrele(new_ire);
6596 6597  }
6597 6598  
6598 6599  /*
6599 6600   * When the settings of ip*_strict_src_multihoming tunables are changed,
6600 6601   * all cached routes need to be recomputed. This recomputation needs to be
6601 6602   * done when going from weaker to stronger modes so that the cached ire
6602 6603   * for the connection does not violate the current ip*_strict_src_multihoming
6603 6604   * setting. It also needs to be done when going from stronger to weaker modes,
6604 6605   * so that we fall back to matching on the longest-matching-route (as opposed
6605 6606   * to a shorter match that may have been selected in the strong mode
6606 6607   * to satisfy src_multihoming settings).
6607 6608   *
6608 6609   * The cached ixa_ire entires for all conn_t entries are marked as
6609 6610   * "verify" so that they will be recomputed for the next packet.
6610 6611   */
6611 6612  void
6612 6613  conn_ire_revalidate(conn_t *connp, void *arg)
6613 6614  {
6614 6615          boolean_t isv6 = (boolean_t)arg;
6615 6616  
6616 6617          if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6617 6618              (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6618 6619                  return;
6619 6620          connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6620 6621  }
6621 6622  
6622 6623  /*
6623 6624   * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6624 6625   * When an ipf is passed here for the first time, if
6625 6626   * we already have in-order fragments on the queue, we convert from the fast-
6626 6627   * path reassembly scheme to the hard-case scheme.  From then on, additional
6627 6628   * fragments are reassembled here.  We keep track of the start and end offsets
6628 6629   * of each piece, and the number of holes in the chain.  When the hole count
6629 6630   * goes to zero, we are done!
6630 6631   *
6631 6632   * The ipf_count will be updated to account for any mblk(s) added (pointed to
6632 6633   * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6633 6634   * ipfb_count and ill_frag_count by the difference of ipf_count before and
6634 6635   * after the call to ip_reassemble().
6635 6636   */
6636 6637  int
6637 6638  ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6638 6639      size_t msg_len)
6639 6640  {
6640 6641          uint_t  end;
6641 6642          mblk_t  *next_mp;
6642 6643          mblk_t  *mp1;
6643 6644          uint_t  offset;
6644 6645          boolean_t incr_dups = B_TRUE;
6645 6646          boolean_t offset_zero_seen = B_FALSE;
6646 6647          boolean_t pkt_boundary_checked = B_FALSE;
6647 6648  
6648 6649          /* If start == 0 then ipf_nf_hdr_len has to be set. */
6649 6650          ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6650 6651  
6651 6652          /* Add in byte count */
6652 6653          ipf->ipf_count += msg_len;
6653 6654          if (ipf->ipf_end) {
6654 6655                  /*
6655 6656                   * We were part way through in-order reassembly, but now there
6656 6657                   * is a hole.  We walk through messages already queued, and
6657 6658                   * mark them for hard case reassembly.  We know that up till
6658 6659                   * now they were in order starting from offset zero.
6659 6660                   */
6660 6661                  offset = 0;
6661 6662                  for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6662 6663                          IP_REASS_SET_START(mp1, offset);
6663 6664                          if (offset == 0) {
6664 6665                                  ASSERT(ipf->ipf_nf_hdr_len != 0);
6665 6666                                  offset = -ipf->ipf_nf_hdr_len;
6666 6667                          }
6667 6668                          offset += mp1->b_wptr - mp1->b_rptr;
6668 6669                          IP_REASS_SET_END(mp1, offset);
6669 6670                  }
6670 6671                  /* One hole at the end. */
6671 6672                  ipf->ipf_hole_cnt = 1;
6672 6673                  /* Brand it as a hard case, forever. */
6673 6674                  ipf->ipf_end = 0;
6674 6675          }
6675 6676          /* Walk through all the new pieces. */
6676 6677          do {
6677 6678                  end = start + (mp->b_wptr - mp->b_rptr);
6678 6679                  /*
6679 6680                   * If start is 0, decrease 'end' only for the first mblk of
6680 6681                   * the fragment. Otherwise 'end' can get wrong value in the
6681 6682                   * second pass of the loop if first mblk is exactly the
6682 6683                   * size of ipf_nf_hdr_len.
6683 6684                   */
6684 6685                  if (start == 0 && !offset_zero_seen) {
6685 6686                          /* First segment */
6686 6687                          ASSERT(ipf->ipf_nf_hdr_len != 0);
6687 6688                          end -= ipf->ipf_nf_hdr_len;
6688 6689                          offset_zero_seen = B_TRUE;
6689 6690                  }
6690 6691                  next_mp = mp->b_cont;
6691 6692                  /*
6692 6693                   * We are checking to see if there is any interesing data
6693 6694                   * to process.  If there isn't and the mblk isn't the
6694 6695                   * one which carries the unfragmentable header then we
6695 6696                   * drop it.  It's possible to have just the unfragmentable
6696 6697                   * header come through without any data.  That needs to be
6697 6698                   * saved.
6698 6699                   *
6699 6700                   * If the assert at the top of this function holds then the
6700 6701                   * term "ipf->ipf_nf_hdr_len != 0" isn't needed.  This code
6701 6702                   * is infrequently traveled enough that the test is left in
6702 6703                   * to protect against future code changes which break that
6703 6704                   * invariant.
6704 6705                   */
6705 6706                  if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6706 6707                          /* Empty.  Blast it. */
6707 6708                          IP_REASS_SET_START(mp, 0);
6708 6709                          IP_REASS_SET_END(mp, 0);
6709 6710                          /*
6710 6711                           * If the ipf points to the mblk we are about to free,
6711 6712                           * update ipf to point to the next mblk (or NULL
6712 6713                           * if none).
6713 6714                           */
6714 6715                          if (ipf->ipf_mp->b_cont == mp)
6715 6716                                  ipf->ipf_mp->b_cont = next_mp;
6716 6717                          freeb(mp);
6717 6718                          continue;
6718 6719                  }
6719 6720                  mp->b_cont = NULL;
6720 6721                  IP_REASS_SET_START(mp, start);
6721 6722                  IP_REASS_SET_END(mp, end);
6722 6723                  if (!ipf->ipf_tail_mp) {
6723 6724                          ipf->ipf_tail_mp = mp;
6724 6725                          ipf->ipf_mp->b_cont = mp;
6725 6726                          if (start == 0 || !more) {
6726 6727                                  ipf->ipf_hole_cnt = 1;
6727 6728                                  /*
6728 6729                                   * if the first fragment comes in more than one
6729 6730                                   * mblk, this loop will be executed for each
6730 6731                                   * mblk. Need to adjust hole count so exiting
6731 6732                                   * this routine will leave hole count at 1.
6732 6733                                   */
6733 6734                                  if (next_mp)
6734 6735                                          ipf->ipf_hole_cnt++;
6735 6736                          } else
6736 6737                                  ipf->ipf_hole_cnt = 2;
6737 6738                          continue;
6738 6739                  } else if (ipf->ipf_last_frag_seen && !more &&
6739 6740                      !pkt_boundary_checked) {
6740 6741                          /*
6741 6742                           * We check datagram boundary only if this fragment
6742 6743                           * claims to be the last fragment and we have seen a
6743 6744                           * last fragment in the past too. We do this only
6744 6745                           * once for a given fragment.
6745 6746                           *
6746 6747                           * start cannot be 0 here as fragments with start=0
6747 6748                           * and MF=0 gets handled as a complete packet. These
6748 6749                           * fragments should not reach here.
6749 6750                           */
6750 6751  
6751 6752                          if (start + msgdsize(mp) !=
6752 6753                              IP_REASS_END(ipf->ipf_tail_mp)) {
6753 6754                                  /*
6754 6755                                   * We have two fragments both of which claim
6755 6756                                   * to be the last fragment but gives conflicting
6756 6757                                   * information about the whole datagram size.
6757 6758                                   * Something fishy is going on. Drop the
6758 6759                                   * fragment and free up the reassembly list.
6759 6760                                   */
6760 6761                                  return (IP_REASS_FAILED);
6761 6762                          }
6762 6763  
6763 6764                          /*
6764 6765                           * We shouldn't come to this code block again for this
6765 6766                           * particular fragment.
6766 6767                           */
6767 6768                          pkt_boundary_checked = B_TRUE;
6768 6769                  }
6769 6770  
6770 6771                  /* New stuff at or beyond tail? */
6771 6772                  offset = IP_REASS_END(ipf->ipf_tail_mp);
6772 6773                  if (start >= offset) {
6773 6774                          if (ipf->ipf_last_frag_seen) {
6774 6775                                  /* current fragment is beyond last fragment */
6775 6776                                  return (IP_REASS_FAILED);
6776 6777                          }
6777 6778                          /* Link it on end. */
6778 6779                          ipf->ipf_tail_mp->b_cont = mp;
6779 6780                          ipf->ipf_tail_mp = mp;
6780 6781                          if (more) {
6781 6782                                  if (start != offset)
6782 6783                                          ipf->ipf_hole_cnt++;
6783 6784                          } else if (start == offset && next_mp == NULL)
6784 6785                                          ipf->ipf_hole_cnt--;
6785 6786                          continue;
6786 6787                  }
6787 6788                  mp1 = ipf->ipf_mp->b_cont;
6788 6789                  offset = IP_REASS_START(mp1);
6789 6790                  /* New stuff at the front? */
6790 6791                  if (start < offset) {
6791 6792                          if (start == 0) {
6792 6793                                  if (end >= offset) {
6793 6794                                          /* Nailed the hole at the begining. */
6794 6795                                          ipf->ipf_hole_cnt--;
6795 6796                                  }
6796 6797                          } else if (end < offset) {
6797 6798                                  /*
6798 6799                                   * A hole, stuff, and a hole where there used
6799 6800                                   * to be just a hole.
6800 6801                                   */
6801 6802                                  ipf->ipf_hole_cnt++;
6802 6803                          }
6803 6804                          mp->b_cont = mp1;
6804 6805                          /* Check for overlap. */
6805 6806                          while (end > offset) {
6806 6807                                  if (end < IP_REASS_END(mp1)) {
6807 6808                                          mp->b_wptr -= end - offset;
6808 6809                                          IP_REASS_SET_END(mp, offset);
6809 6810                                          BUMP_MIB(ill->ill_ip_mib,
6810 6811                                              ipIfStatsReasmPartDups);
6811 6812                                          break;
6812 6813                                  }
6813 6814                                  /* Did we cover another hole? */
6814 6815                                  if ((mp1->b_cont &&
6815 6816                                      IP_REASS_END(mp1) !=
6816 6817                                      IP_REASS_START(mp1->b_cont) &&
6817 6818                                      end >= IP_REASS_START(mp1->b_cont)) ||
6818 6819                                      (!ipf->ipf_last_frag_seen && !more)) {
6819 6820                                          ipf->ipf_hole_cnt--;
6820 6821                                  }
6821 6822                                  /* Clip out mp1. */
6822 6823                                  if ((mp->b_cont = mp1->b_cont) == NULL) {
6823 6824                                          /*
6824 6825                                           * After clipping out mp1, this guy
6825 6826                                           * is now hanging off the end.
6826 6827                                           */
6827 6828                                          ipf->ipf_tail_mp = mp;
6828 6829                                  }
6829 6830                                  IP_REASS_SET_START(mp1, 0);
6830 6831                                  IP_REASS_SET_END(mp1, 0);
6831 6832                                  /* Subtract byte count */
6832 6833                                  ipf->ipf_count -= mp1->b_datap->db_lim -
6833 6834                                      mp1->b_datap->db_base;
6834 6835                                  freeb(mp1);
6835 6836                                  BUMP_MIB(ill->ill_ip_mib,
6836 6837                                      ipIfStatsReasmPartDups);
6837 6838                                  mp1 = mp->b_cont;
6838 6839                                  if (!mp1)
6839 6840                                          break;
6840 6841                                  offset = IP_REASS_START(mp1);
6841 6842                          }
6842 6843                          ipf->ipf_mp->b_cont = mp;
6843 6844                          continue;
6844 6845                  }
6845 6846                  /*
6846 6847                   * The new piece starts somewhere between the start of the head
6847 6848                   * and before the end of the tail.
6848 6849                   */
6849 6850                  for (; mp1; mp1 = mp1->b_cont) {
6850 6851                          offset = IP_REASS_END(mp1);
6851 6852                          if (start < offset) {
6852 6853                                  if (end <= offset) {
6853 6854                                          /* Nothing new. */
6854 6855                                          IP_REASS_SET_START(mp, 0);
6855 6856                                          IP_REASS_SET_END(mp, 0);
6856 6857                                          /* Subtract byte count */
6857 6858                                          ipf->ipf_count -= mp->b_datap->db_lim -
6858 6859                                              mp->b_datap->db_base;
6859 6860                                          if (incr_dups) {
6860 6861                                                  ipf->ipf_num_dups++;
6861 6862                                                  incr_dups = B_FALSE;
6862 6863                                          }
6863 6864                                          freeb(mp);
6864 6865                                          BUMP_MIB(ill->ill_ip_mib,
6865 6866                                              ipIfStatsReasmDuplicates);
6866 6867                                          break;
6867 6868                                  }
6868 6869                                  /*
6869 6870                                   * Trim redundant stuff off beginning of new
6870 6871                                   * piece.
6871 6872                                   */
6872 6873                                  IP_REASS_SET_START(mp, offset);
6873 6874                                  mp->b_rptr += offset - start;
6874 6875                                  BUMP_MIB(ill->ill_ip_mib,
6875 6876                                      ipIfStatsReasmPartDups);
6876 6877                                  start = offset;
6877 6878                                  if (!mp1->b_cont) {
6878 6879                                          /*
6879 6880                                           * After trimming, this guy is now
6880 6881                                           * hanging off the end.
6881 6882                                           */
6882 6883                                          mp1->b_cont = mp;
6883 6884                                          ipf->ipf_tail_mp = mp;
6884 6885                                          if (!more) {
6885 6886                                                  ipf->ipf_hole_cnt--;
6886 6887                                          }
6887 6888                                          break;
6888 6889                                  }
6889 6890                          }
6890 6891                          if (start >= IP_REASS_START(mp1->b_cont))
6891 6892                                  continue;
6892 6893                          /* Fill a hole */
6893 6894                          if (start > offset)
6894 6895                                  ipf->ipf_hole_cnt++;
6895 6896                          mp->b_cont = mp1->b_cont;
6896 6897                          mp1->b_cont = mp;
6897 6898                          mp1 = mp->b_cont;
6898 6899                          offset = IP_REASS_START(mp1);
6899 6900                          if (end >= offset) {
6900 6901                                  ipf->ipf_hole_cnt--;
6901 6902                                  /* Check for overlap. */
6902 6903                                  while (end > offset) {
6903 6904                                          if (end < IP_REASS_END(mp1)) {
6904 6905                                                  mp->b_wptr -= end - offset;
6905 6906                                                  IP_REASS_SET_END(mp, offset);
6906 6907                                                  /*
6907 6908                                                   * TODO we might bump
6908 6909                                                   * this up twice if there is
6909 6910                                                   * overlap at both ends.
6910 6911                                                   */
6911 6912                                                  BUMP_MIB(ill->ill_ip_mib,
6912 6913                                                      ipIfStatsReasmPartDups);
6913 6914                                                  break;
6914 6915                                          }
6915 6916                                          /* Did we cover another hole? */
6916 6917                                          if ((mp1->b_cont &&
6917 6918                                              IP_REASS_END(mp1)
6918 6919                                              != IP_REASS_START(mp1->b_cont) &&
6919 6920                                              end >=
6920 6921                                              IP_REASS_START(mp1->b_cont)) ||
6921 6922                                              (!ipf->ipf_last_frag_seen &&
6922 6923                                              !more)) {
6923 6924                                                  ipf->ipf_hole_cnt--;
6924 6925                                          }
6925 6926                                          /* Clip out mp1. */
6926 6927                                          if ((mp->b_cont = mp1->b_cont) ==
6927 6928                                              NULL) {
6928 6929                                                  /*
6929 6930                                                   * After clipping out mp1,
6930 6931                                                   * this guy is now hanging
6931 6932                                                   * off the end.
6932 6933                                                   */
6933 6934                                                  ipf->ipf_tail_mp = mp;
6934 6935                                          }
6935 6936                                          IP_REASS_SET_START(mp1, 0);
6936 6937                                          IP_REASS_SET_END(mp1, 0);
6937 6938                                          /* Subtract byte count */
6938 6939                                          ipf->ipf_count -=
6939 6940                                              mp1->b_datap->db_lim -
6940 6941                                              mp1->b_datap->db_base;
6941 6942                                          freeb(mp1);
6942 6943                                          BUMP_MIB(ill->ill_ip_mib,
6943 6944                                              ipIfStatsReasmPartDups);
6944 6945                                          mp1 = mp->b_cont;
6945 6946                                          if (!mp1)
6946 6947                                                  break;
6947 6948                                          offset = IP_REASS_START(mp1);
6948 6949                                  }
6949 6950                          }
6950 6951                          break;
6951 6952                  }
6952 6953          } while (start = end, mp = next_mp);
6953 6954  
6954 6955          /* Fragment just processed could be the last one. Remember this fact */
6955 6956          if (!more)
6956 6957                  ipf->ipf_last_frag_seen = B_TRUE;
6957 6958  
6958 6959          /* Still got holes? */
6959 6960          if (ipf->ipf_hole_cnt)
6960 6961                  return (IP_REASS_PARTIAL);
6961 6962          /* Clean up overloaded fields to avoid upstream disasters. */
6962 6963          for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6963 6964                  IP_REASS_SET_START(mp1, 0);
6964 6965                  IP_REASS_SET_END(mp1, 0);
6965 6966          }
6966 6967          return (IP_REASS_COMPLETE);
6967 6968  }
6968 6969  
6969 6970  /*
6970 6971   * Fragmentation reassembly.  Each ILL has a hash table for
6971 6972   * queuing packets undergoing reassembly for all IPIFs
6972 6973   * associated with the ILL.  The hash is based on the packet
6973 6974   * IP ident field.  The ILL frag hash table was allocated
6974 6975   * as a timer block at the time the ILL was created.  Whenever
6975 6976   * there is anything on the reassembly queue, the timer will
6976 6977   * be running.  Returns the reassembled packet if reassembly completes.
6977 6978   */
6978 6979  mblk_t *
6979 6980  ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
6980 6981  {
6981 6982          uint32_t        frag_offset_flags;
6982 6983          mblk_t          *t_mp;
6983 6984          ipaddr_t        dst;
6984 6985          uint8_t         proto = ipha->ipha_protocol;
6985 6986          uint32_t        sum_val;
6986 6987          uint16_t        sum_flags;
6987 6988          ipf_t           *ipf;
6988 6989          ipf_t           **ipfp;
6989 6990          ipfb_t          *ipfb;
6990 6991          uint16_t        ident;
6991 6992          uint32_t        offset;
6992 6993          ipaddr_t        src;
6993 6994          uint_t          hdr_length;
6994 6995          uint32_t        end;
6995 6996          mblk_t          *mp1;
6996 6997          mblk_t          *tail_mp;
6997 6998          size_t          count;
6998 6999          size_t          msg_len;
6999 7000          uint8_t         ecn_info = 0;
7000 7001          uint32_t        packet_size;
7001 7002          boolean_t       pruned = B_FALSE;
7002 7003          ill_t           *ill = ira->ira_ill;
7003 7004          ip_stack_t      *ipst = ill->ill_ipst;
7004 7005  
7005 7006          /*
7006 7007           * Drop the fragmented as early as possible, if
7007 7008           * we don't have resource(s) to re-assemble.
7008 7009           */
7009 7010          if (ipst->ips_ip_reass_queue_bytes == 0) {
7010 7011                  freemsg(mp);
7011 7012                  return (NULL);
7012 7013          }
7013 7014  
7014 7015          /* Check for fragmentation offset; return if there's none */
7015 7016          if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
7016 7017              (IPH_MF | IPH_OFFSET)) == 0)
7017 7018                  return (mp);
7018 7019  
7019 7020          /*
7020 7021           * We utilize hardware computed checksum info only for UDP since
7021 7022           * IP fragmentation is a normal occurrence for the protocol.  In
7022 7023           * addition, checksum offload support for IP fragments carrying
7023 7024           * UDP payload is commonly implemented across network adapters.
7024 7025           */
7025 7026          ASSERT(ira->ira_rill != NULL);
7026 7027          if (proto == IPPROTO_UDP && dohwcksum &&
7027 7028              ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
7028 7029              (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
7029 7030                  mblk_t *mp1 = mp->b_cont;
7030 7031                  int32_t len;
7031 7032  
7032 7033                  /* Record checksum information from the packet */
7033 7034                  sum_val = (uint32_t)DB_CKSUM16(mp);
7034 7035                  sum_flags = DB_CKSUMFLAGS(mp);
7035 7036  
7036 7037                  /* IP payload offset from beginning of mblk */
7037 7038                  offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
7038 7039  
7039 7040                  if ((sum_flags & HCK_PARTIALCKSUM) &&
7040 7041                      (mp1 == NULL || mp1->b_cont == NULL) &&
7041 7042                      offset >= DB_CKSUMSTART(mp) &&
7042 7043                      ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
7043 7044                          uint32_t adj;
7044 7045                          /*
7045 7046                           * Partial checksum has been calculated by hardware
7046 7047                           * and attached to the packet; in addition, any
7047 7048                           * prepended extraneous data is even byte aligned.
7048 7049                           * If any such data exists, we adjust the checksum;
7049 7050                           * this would also handle any postpended data.
7050 7051                           */
7051 7052                          IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
7052 7053                              mp, mp1, len, adj);
7053 7054  
7054 7055                          /* One's complement subtract extraneous checksum */
7055 7056                          if (adj >= sum_val)
7056 7057                                  sum_val = ~(adj - sum_val) & 0xFFFF;
7057 7058                          else
7058 7059                                  sum_val -= adj;
7059 7060                  }
7060 7061          } else {
7061 7062                  sum_val = 0;
7062 7063                  sum_flags = 0;
7063 7064          }
7064 7065  
7065 7066          /* Clear hardware checksumming flag */
7066 7067          DB_CKSUMFLAGS(mp) = 0;
7067 7068  
7068 7069          ident = ipha->ipha_ident;
7069 7070          offset = (frag_offset_flags << 3) & 0xFFFF;
7070 7071          src = ipha->ipha_src;
7071 7072          dst = ipha->ipha_dst;
7072 7073          hdr_length = IPH_HDR_LENGTH(ipha);
7073 7074          end = ntohs(ipha->ipha_length) - hdr_length;
7074 7075  
7075 7076          /* If end == 0 then we have a packet with no data, so just free it */
7076 7077          if (end == 0) {
7077 7078                  freemsg(mp);
7078 7079                  return (NULL);
7079 7080          }
7080 7081  
7081 7082          /* Record the ECN field info. */
7082 7083          ecn_info = (ipha->ipha_type_of_service & 0x3);
7083 7084          if (offset != 0) {
7084 7085                  /*
7085 7086                   * If this isn't the first piece, strip the header, and
7086 7087                   * add the offset to the end value.
7087 7088                   */
7088 7089                  mp->b_rptr += hdr_length;
7089 7090                  end += offset;
7090 7091          }
7091 7092  
7092 7093          /* Handle vnic loopback of fragments */
7093 7094          if (mp->b_datap->db_ref > 2)
7094 7095                  msg_len = 0;
7095 7096          else
7096 7097                  msg_len = MBLKSIZE(mp);
7097 7098  
7098 7099          tail_mp = mp;
7099 7100          while (tail_mp->b_cont != NULL) {
7100 7101                  tail_mp = tail_mp->b_cont;
7101 7102                  if (tail_mp->b_datap->db_ref <= 2)
7102 7103                          msg_len += MBLKSIZE(tail_mp);
7103 7104          }
7104 7105  
7105 7106          /* If the reassembly list for this ILL will get too big, prune it */
7106 7107          if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
7107 7108              ipst->ips_ip_reass_queue_bytes) {
7108 7109                  DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
7109 7110                      uint_t, ill->ill_frag_count,
7110 7111                      uint_t, ipst->ips_ip_reass_queue_bytes);
7111 7112                  ill_frag_prune(ill,
7112 7113                      (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
7113 7114                      (ipst->ips_ip_reass_queue_bytes - msg_len));
7114 7115                  pruned = B_TRUE;
7115 7116          }
7116 7117  
7117 7118          ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
7118 7119          mutex_enter(&ipfb->ipfb_lock);
7119 7120  
7120 7121          ipfp = &ipfb->ipfb_ipf;
7121 7122          /* Try to find an existing fragment queue for this packet. */
7122 7123          for (;;) {
7123 7124                  ipf = ipfp[0];
7124 7125                  if (ipf != NULL) {
7125 7126                          /*
7126 7127                           * It has to match on ident and src/dst address.
7127 7128                           */
7128 7129                          if (ipf->ipf_ident == ident &&
7129 7130                              ipf->ipf_src == src &&
7130 7131                              ipf->ipf_dst == dst &&
7131 7132                              ipf->ipf_protocol == proto) {
7132 7133                                  /*
7133 7134                                   * If we have received too many
7134 7135                                   * duplicate fragments for this packet
7135 7136                                   * free it.
7136 7137                                   */
7137 7138                                  if (ipf->ipf_num_dups > ip_max_frag_dups) {
7138 7139                                          ill_frag_free_pkts(ill, ipfb, ipf, 1);
7139 7140                                          freemsg(mp);
7140 7141                                          mutex_exit(&ipfb->ipfb_lock);
7141 7142                                          return (NULL);
7142 7143                                  }
7143 7144                                  /* Found it. */
7144 7145                                  break;
7145 7146                          }
7146 7147                          ipfp = &ipf->ipf_hash_next;
7147 7148                          continue;
7148 7149                  }
7149 7150  
7150 7151                  /*
7151 7152                   * If we pruned the list, do we want to store this new
7152 7153                   * fragment?. We apply an optimization here based on the
7153 7154                   * fact that most fragments will be received in order.
7154 7155                   * So if the offset of this incoming fragment is zero,
7155 7156                   * it is the first fragment of a new packet. We will
7156 7157                   * keep it.  Otherwise drop the fragment, as we have
7157 7158                   * probably pruned the packet already (since the
7158 7159                   * packet cannot be found).
7159 7160                   */
7160 7161                  if (pruned && offset != 0) {
7161 7162                          mutex_exit(&ipfb->ipfb_lock);
7162 7163                          freemsg(mp);
7163 7164                          return (NULL);
7164 7165                  }
7165 7166  
7166 7167                  if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst))  {
7167 7168                          /*
7168 7169                           * Too many fragmented packets in this hash
7169 7170                           * bucket. Free the oldest.
7170 7171                           */
7171 7172                          ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
7172 7173                  }
7173 7174  
7174 7175                  /* New guy.  Allocate a frag message. */
7175 7176                  mp1 = allocb(sizeof (*ipf), BPRI_MED);
7176 7177                  if (mp1 == NULL) {
7177 7178                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7178 7179                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7179 7180                          freemsg(mp);
7180 7181  reass_done:
7181 7182                          mutex_exit(&ipfb->ipfb_lock);
7182 7183                          return (NULL);
7183 7184                  }
7184 7185  
7185 7186                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
7186 7187                  mp1->b_cont = mp;
7187 7188  
7188 7189                  /* Initialize the fragment header. */
7189 7190                  ipf = (ipf_t *)mp1->b_rptr;
7190 7191                  ipf->ipf_mp = mp1;
7191 7192                  ipf->ipf_ptphn = ipfp;
7192 7193                  ipfp[0] = ipf;
7193 7194                  ipf->ipf_hash_next = NULL;
7194 7195                  ipf->ipf_ident = ident;
7195 7196                  ipf->ipf_protocol = proto;
7196 7197                  ipf->ipf_src = src;
7197 7198                  ipf->ipf_dst = dst;
7198 7199                  ipf->ipf_nf_hdr_len = 0;
7199 7200                  /* Record reassembly start time. */
7200 7201                  ipf->ipf_timestamp = gethrestime_sec();
7201 7202                  /* Record ipf generation and account for frag header */
7202 7203                  ipf->ipf_gen = ill->ill_ipf_gen++;
7203 7204                  ipf->ipf_count = MBLKSIZE(mp1);
7204 7205                  ipf->ipf_last_frag_seen = B_FALSE;
7205 7206                  ipf->ipf_ecn = ecn_info;
7206 7207                  ipf->ipf_num_dups = 0;
7207 7208                  ipfb->ipfb_frag_pkts++;
7208 7209                  ipf->ipf_checksum = 0;
7209 7210                  ipf->ipf_checksum_flags = 0;
7210 7211  
7211 7212                  /* Store checksum value in fragment header */
7212 7213                  if (sum_flags != 0) {
7213 7214                          sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7214 7215                          sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7215 7216                          ipf->ipf_checksum = sum_val;
7216 7217                          ipf->ipf_checksum_flags = sum_flags;
7217 7218                  }
7218 7219  
7219 7220                  /*
7220 7221                   * We handle reassembly two ways.  In the easy case,
7221 7222                   * where all the fragments show up in order, we do
7222 7223                   * minimal bookkeeping, and just clip new pieces on
7223 7224                   * the end.  If we ever see a hole, then we go off
7224 7225                   * to ip_reassemble which has to mark the pieces and
7225 7226                   * keep track of the number of holes, etc.  Obviously,
7226 7227                   * the point of having both mechanisms is so we can
7227 7228                   * handle the easy case as efficiently as possible.
7228 7229                   */
7229 7230                  if (offset == 0) {
7230 7231                          /* Easy case, in-order reassembly so far. */
7231 7232                          ipf->ipf_count += msg_len;
7232 7233                          ipf->ipf_tail_mp = tail_mp;
7233 7234                          /*
7234 7235                           * Keep track of next expected offset in
7235 7236                           * ipf_end.
7236 7237                           */
7237 7238                          ipf->ipf_end = end;
7238 7239                          ipf->ipf_nf_hdr_len = hdr_length;
7239 7240                  } else {
7240 7241                          /* Hard case, hole at the beginning. */
7241 7242                          ipf->ipf_tail_mp = NULL;
7242 7243                          /*
7243 7244                           * ipf_end == 0 means that we have given up
7244 7245                           * on easy reassembly.
7245 7246                           */
7246 7247                          ipf->ipf_end = 0;
7247 7248  
7248 7249                          /* Forget checksum offload from now on */
7249 7250                          ipf->ipf_checksum_flags = 0;
7250 7251  
7251 7252                          /*
7252 7253                           * ipf_hole_cnt is set by ip_reassemble.
7253 7254                           * ipf_count is updated by ip_reassemble.
7254 7255                           * No need to check for return value here
7255 7256                           * as we don't expect reassembly to complete
7256 7257                           * or fail for the first fragment itself.
7257 7258                           */
7258 7259                          (void) ip_reassemble(mp, ipf,
7259 7260                              (frag_offset_flags & IPH_OFFSET) << 3,
7260 7261                              (frag_offset_flags & IPH_MF), ill, msg_len);
7261 7262                  }
7262 7263                  /* Update per ipfb and ill byte counts */
7263 7264                  ipfb->ipfb_count += ipf->ipf_count;
7264 7265                  ASSERT(ipfb->ipfb_count > 0);   /* Wraparound */
7265 7266                  atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7266 7267                  /* If the frag timer wasn't already going, start it. */
7267 7268                  mutex_enter(&ill->ill_lock);
7268 7269                  ill_frag_timer_start(ill);
7269 7270                  mutex_exit(&ill->ill_lock);
7270 7271                  goto reass_done;
7271 7272          }
7272 7273  
7273 7274          /*
7274 7275           * If the packet's flag has changed (it could be coming up
7275 7276           * from an interface different than the previous, therefore
7276 7277           * possibly different checksum capability), then forget about
7277 7278           * any stored checksum states.  Otherwise add the value to
7278 7279           * the existing one stored in the fragment header.
7279 7280           */
7280 7281          if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7281 7282                  sum_val += ipf->ipf_checksum;
7282 7283                  sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7283 7284                  sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7284 7285                  ipf->ipf_checksum = sum_val;
7285 7286          } else if (ipf->ipf_checksum_flags != 0) {
7286 7287                  /* Forget checksum offload from now on */
7287 7288                  ipf->ipf_checksum_flags = 0;
7288 7289          }
7289 7290  
7290 7291          /*
7291 7292           * We have a new piece of a datagram which is already being
7292 7293           * reassembled.  Update the ECN info if all IP fragments
7293 7294           * are ECN capable.  If there is one which is not, clear
7294 7295           * all the info.  If there is at least one which has CE
7295 7296           * code point, IP needs to report that up to transport.
7296 7297           */
7297 7298          if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7298 7299                  if (ecn_info == IPH_ECN_CE)
7299 7300                          ipf->ipf_ecn = IPH_ECN_CE;
7300 7301          } else {
7301 7302                  ipf->ipf_ecn = IPH_ECN_NECT;
7302 7303          }
7303 7304          if (offset && ipf->ipf_end == offset) {
7304 7305                  /* The new fragment fits at the end */
7305 7306                  ipf->ipf_tail_mp->b_cont = mp;
7306 7307                  /* Update the byte count */
7307 7308                  ipf->ipf_count += msg_len;
7308 7309                  /* Update per ipfb and ill byte counts */
7309 7310                  ipfb->ipfb_count += msg_len;
7310 7311                  ASSERT(ipfb->ipfb_count > 0);   /* Wraparound */
7311 7312                  atomic_add_32(&ill->ill_frag_count, msg_len);
7312 7313                  if (frag_offset_flags & IPH_MF) {
7313 7314                          /* More to come. */
7314 7315                          ipf->ipf_end = end;
7315 7316                          ipf->ipf_tail_mp = tail_mp;
7316 7317                          goto reass_done;
7317 7318                  }
7318 7319          } else {
7319 7320                  /* Go do the hard cases. */
7320 7321                  int ret;
7321 7322  
7322 7323                  if (offset == 0)
7323 7324                          ipf->ipf_nf_hdr_len = hdr_length;
7324 7325  
7325 7326                  /* Save current byte count */
7326 7327                  count = ipf->ipf_count;
7327 7328                  ret = ip_reassemble(mp, ipf,
7328 7329                      (frag_offset_flags & IPH_OFFSET) << 3,
7329 7330                      (frag_offset_flags & IPH_MF), ill, msg_len);
7330 7331                  /* Count of bytes added and subtracted (freeb()ed) */
7331 7332                  count = ipf->ipf_count - count;
7332 7333                  if (count) {
7333 7334                          /* Update per ipfb and ill byte counts */
7334 7335                          ipfb->ipfb_count += count;
7335 7336                          ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7336 7337                          atomic_add_32(&ill->ill_frag_count, count);
7337 7338                  }
7338 7339                  if (ret == IP_REASS_PARTIAL) {
7339 7340                          goto reass_done;
7340 7341                  } else if (ret == IP_REASS_FAILED) {
7341 7342                          /* Reassembly failed. Free up all resources */
7342 7343                          ill_frag_free_pkts(ill, ipfb, ipf, 1);
7343 7344                          for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7344 7345                                  IP_REASS_SET_START(t_mp, 0);
7345 7346                                  IP_REASS_SET_END(t_mp, 0);
7346 7347                          }
7347 7348                          freemsg(mp);
7348 7349                          goto reass_done;
7349 7350                  }
7350 7351                  /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7351 7352          }
7352 7353          /*
7353 7354           * We have completed reassembly.  Unhook the frag header from
7354 7355           * the reassembly list.
7355 7356           *
7356 7357           * Before we free the frag header, record the ECN info
7357 7358           * to report back to the transport.
7358 7359           */
7359 7360          ecn_info = ipf->ipf_ecn;
7360 7361          BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7361 7362          ipfp = ipf->ipf_ptphn;
7362 7363  
7363 7364          /* We need to supply these to caller */
7364 7365          if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7365 7366                  sum_val = ipf->ipf_checksum;
7366 7367          else
7367 7368                  sum_val = 0;
7368 7369  
7369 7370          mp1 = ipf->ipf_mp;
7370 7371          count = ipf->ipf_count;
7371 7372          ipf = ipf->ipf_hash_next;
7372 7373          if (ipf != NULL)
7373 7374                  ipf->ipf_ptphn = ipfp;
7374 7375          ipfp[0] = ipf;
7375 7376          atomic_add_32(&ill->ill_frag_count, -count);
7376 7377          ASSERT(ipfb->ipfb_count >= count);
7377 7378          ipfb->ipfb_count -= count;
7378 7379          ipfb->ipfb_frag_pkts--;
7379 7380          mutex_exit(&ipfb->ipfb_lock);
7380 7381          /* Ditch the frag header. */
7381 7382          mp = mp1->b_cont;
7382 7383  
7383 7384          freeb(mp1);
7384 7385  
7385 7386          /* Restore original IP length in header. */
7386 7387          packet_size = (uint32_t)msgdsize(mp);
7387 7388          if (packet_size > IP_MAXPACKET) {
7388 7389                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7389 7390                  ip_drop_input("Reassembled packet too large", mp, ill);
7390 7391                  freemsg(mp);
7391 7392                  return (NULL);
7392 7393          }
7393 7394  
7394 7395          if (DB_REF(mp) > 1) {
7395 7396                  mblk_t *mp2 = copymsg(mp);
7396 7397  
7397 7398                  if (mp2 == NULL) {
7398 7399                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7399 7400                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7400 7401                          freemsg(mp);
7401 7402                          return (NULL);
7402 7403                  }
7403 7404                  freemsg(mp);
7404 7405                  mp = mp2;
7405 7406          }
7406 7407          ipha = (ipha_t *)mp->b_rptr;
7407 7408  
7408 7409          ipha->ipha_length = htons((uint16_t)packet_size);
7409 7410          /* We're now complete, zip the frag state */
7410 7411          ipha->ipha_fragment_offset_and_flags = 0;
7411 7412          /* Record the ECN info. */
7412 7413          ipha->ipha_type_of_service &= 0xFC;
7413 7414          ipha->ipha_type_of_service |= ecn_info;
7414 7415  
7415 7416          /* Update the receive attributes */
7416 7417          ira->ira_pktlen = packet_size;
7417 7418          ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7418 7419  
7419 7420          /* Reassembly is successful; set checksum information in packet */
7420 7421          DB_CKSUM16(mp) = (uint16_t)sum_val;
7421 7422          DB_CKSUMFLAGS(mp) = sum_flags;
7422 7423          DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7423 7424  
7424 7425          return (mp);
7425 7426  }
7426 7427  
7427 7428  /*
7428 7429   * Pullup function that should be used for IP input in order to
7429 7430   * ensure we do not loose the L2 source address; we need the l2 source
7430 7431   * address for IP_RECVSLLA and for ndp_input.
7431 7432   *
7432 7433   * We return either NULL or b_rptr.
7433 7434   */
7434 7435  void *
7435 7436  ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7436 7437  {
7437 7438          ill_t           *ill = ira->ira_ill;
7438 7439  
7439 7440          if (ip_rput_pullups++ == 0) {
7440 7441                  (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7441 7442                      "ip_pullup: %s forced us to "
7442 7443                      " pullup pkt, hdr len %ld, hdr addr %p",
7443 7444                      ill->ill_name, len, (void *)mp->b_rptr);
7444 7445          }
7445 7446          if (!(ira->ira_flags & IRAF_L2SRC_SET))
7446 7447                  ip_setl2src(mp, ira, ira->ira_rill);
7447 7448          ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7448 7449          if (!pullupmsg(mp, len))
7449 7450                  return (NULL);
7450 7451          else
7451 7452                  return (mp->b_rptr);
7452 7453  }
7453 7454  
7454 7455  /*
7455 7456   * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7456 7457   * When called from the ULP ira_rill will be NULL hence the caller has to
7457 7458   * pass in the ill.
7458 7459   */
7459 7460  /* ARGSUSED */
7460 7461  void
7461 7462  ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7462 7463  {
7463 7464          const uchar_t *addr;
7464 7465          int alen;
7465 7466  
7466 7467          if (ira->ira_flags & IRAF_L2SRC_SET)
7467 7468                  return;
7468 7469  
7469 7470          ASSERT(ill != NULL);
7470 7471          alen = ill->ill_phys_addr_length;
7471 7472          ASSERT(alen <= sizeof (ira->ira_l2src));
7472 7473          if (ira->ira_mhip != NULL &&
7473 7474              (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7474 7475                  bcopy(addr, ira->ira_l2src, alen);
7475 7476          } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7476 7477              (addr = ill->ill_phys_addr) != NULL) {
7477 7478                  bcopy(addr, ira->ira_l2src, alen);
7478 7479          } else {
7479 7480                  bzero(ira->ira_l2src, alen);
7480 7481          }
7481 7482          ira->ira_flags |= IRAF_L2SRC_SET;
7482 7483  }
7483 7484  
7484 7485  /*
7485 7486   * check ip header length and align it.
7486 7487   */
7487 7488  mblk_t *
7488 7489  ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7489 7490  {
7490 7491          ill_t   *ill = ira->ira_ill;
7491 7492          ssize_t len;
7492 7493  
7493 7494          len = MBLKL(mp);
7494 7495  
7495 7496          if (!OK_32PTR(mp->b_rptr))
7496 7497                  IP_STAT(ill->ill_ipst, ip_notaligned);
7497 7498          else
7498 7499                  IP_STAT(ill->ill_ipst, ip_recv_pullup);
7499 7500  
7500 7501          /* Guard against bogus device drivers */
7501 7502          if (len < 0) {
7502 7503                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7503 7504                  ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7504 7505                  freemsg(mp);
7505 7506                  return (NULL);
7506 7507          }
7507 7508  
7508 7509          if (len == 0) {
7509 7510                  /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7510 7511                  mblk_t *mp1 = mp->b_cont;
7511 7512  
7512 7513                  if (!(ira->ira_flags & IRAF_L2SRC_SET))
7513 7514                          ip_setl2src(mp, ira, ira->ira_rill);
7514 7515                  ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7515 7516  
7516 7517                  freeb(mp);
7517 7518                  mp = mp1;
7518 7519                  if (mp == NULL)
7519 7520                          return (NULL);
7520 7521  
7521 7522                  if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7522 7523                          return (mp);
7523 7524          }
7524 7525          if (ip_pullup(mp, min_size, ira) == NULL) {
7525 7526                  if (msgdsize(mp) < min_size) {
7526 7527                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7527 7528                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7528 7529                  } else {
7529 7530                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7530 7531                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7531 7532                  }
7532 7533                  freemsg(mp);
7533 7534                  return (NULL);
7534 7535          }
7535 7536          return (mp);
7536 7537  }
7537 7538  
7538 7539  /*
7539 7540   * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7540 7541   */
7541 7542  mblk_t *
7542 7543  ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7543 7544      uint_t min_size, ip_recv_attr_t *ira)
7544 7545  {
7545 7546          ill_t   *ill = ira->ira_ill;
7546 7547  
7547 7548          /*
7548 7549           * Make sure we have data length consistent
7549 7550           * with the IP header.
7550 7551           */
7551 7552          if (mp->b_cont == NULL) {
7552 7553                  /* pkt_len is based on ipha_len, not the mblk length */
7553 7554                  if (pkt_len < min_size) {
7554 7555                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7555 7556                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7556 7557                          freemsg(mp);
7557 7558                          return (NULL);
7558 7559                  }
7559 7560                  if (len < 0) {
7560 7561                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7561 7562                          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7562 7563                          freemsg(mp);
7563 7564                          return (NULL);
7564 7565                  }
7565 7566                  /* Drop any pad */
7566 7567                  mp->b_wptr = rptr + pkt_len;
7567 7568          } else if ((len += msgdsize(mp->b_cont)) != 0) {
7568 7569                  ASSERT(pkt_len >= min_size);
7569 7570                  if (pkt_len < min_size) {
7570 7571                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7571 7572                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7572 7573                          freemsg(mp);
7573 7574                          return (NULL);
7574 7575                  }
7575 7576                  if (len < 0) {
7576 7577                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7577 7578                          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7578 7579                          freemsg(mp);
7579 7580                          return (NULL);
7580 7581                  }
7581 7582                  /* Drop any pad */
7582 7583                  (void) adjmsg(mp, -len);
7583 7584                  /*
7584 7585                   * adjmsg may have freed an mblk from the chain, hence
7585 7586                   * invalidate any hw checksum here. This will force IP to
7586 7587                   * calculate the checksum in sw, but only for this packet.
7587 7588                   */
7588 7589                  DB_CKSUMFLAGS(mp) = 0;
7589 7590                  IP_STAT(ill->ill_ipst, ip_multimblk);
7590 7591          }
7591 7592          return (mp);
7592 7593  }
7593 7594  
7594 7595  /*
7595 7596   * Check that the IPv4 opt_len is consistent with the packet and pullup
7596 7597   * the options.
7597 7598   */
7598 7599  mblk_t *
7599 7600  ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7600 7601      ip_recv_attr_t *ira)
7601 7602  {
7602 7603          ill_t   *ill = ira->ira_ill;
7603 7604          ssize_t len;
7604 7605  
7605 7606          /* Assume no IPv6 packets arrive over the IPv4 queue */
7606 7607          if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7607 7608                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7608 7609                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7609 7610                  ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7610 7611                  freemsg(mp);
7611 7612                  return (NULL);
7612 7613          }
7613 7614  
7614 7615          if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7615 7616                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7616 7617                  ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7617 7618                  freemsg(mp);
7618 7619                  return (NULL);
7619 7620          }
7620 7621          /*
7621 7622           * Recompute complete header length and make sure we
7622 7623           * have access to all of it.
7623 7624           */
7624 7625          len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7625 7626          if (len > (mp->b_wptr - mp->b_rptr)) {
7626 7627                  if (len > pkt_len) {
7627 7628                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7628 7629                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7629 7630                          freemsg(mp);
7630 7631                          return (NULL);
7631 7632                  }
7632 7633                  if (ip_pullup(mp, len, ira) == NULL) {
7633 7634                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7634 7635                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7635 7636                          freemsg(mp);
7636 7637                          return (NULL);
7637 7638                  }
7638 7639          }
7639 7640          return (mp);
7640 7641  }
7641 7642  
7642 7643  /*
7643 7644   * Returns a new ire, or the same ire, or NULL.
7644 7645   * If a different IRE is returned, then it is held; the caller
7645 7646   * needs to release it.
7646 7647   * In no case is there any hold/release on the ire argument.
7647 7648   */
7648 7649  ire_t *
7649 7650  ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7650 7651  {
7651 7652          ire_t           *new_ire;
7652 7653          ill_t           *ire_ill;
7653 7654          uint_t          ifindex;
7654 7655          ip_stack_t      *ipst = ill->ill_ipst;
7655 7656          boolean_t       strict_check = B_FALSE;
7656 7657  
7657 7658          /*
7658 7659           * IPMP common case: if IRE and ILL are in the same group, there's no
7659 7660           * issue (e.g. packet received on an underlying interface matched an
7660 7661           * IRE_LOCAL on its associated group interface).
7661 7662           */
7662 7663          ASSERT(ire->ire_ill != NULL);
7663 7664          if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7664 7665                  return (ire);
7665 7666  
7666 7667          /*
7667 7668           * Do another ire lookup here, using the ingress ill, to see if the
7668 7669           * interface is in a usesrc group.
7669 7670           * As long as the ills belong to the same group, we don't consider
7670 7671           * them to be arriving on the wrong interface. Thus, if the switch
7671 7672           * is doing inbound load spreading, we won't drop packets when the
7672 7673           * ip*_strict_dst_multihoming switch is on.
7673 7674           * We also need to check for IPIF_UNNUMBERED point2point interfaces
7674 7675           * where the local address may not be unique. In this case we were
7675 7676           * at the mercy of the initial ire lookup and the IRE_LOCAL it
7676 7677           * actually returned. The new lookup, which is more specific, should
7677 7678           * only find the IRE_LOCAL associated with the ingress ill if one
7678 7679           * exists.
7679 7680           */
7680 7681          if (ire->ire_ipversion == IPV4_VERSION) {
7681 7682                  if (ipst->ips_ip_strict_dst_multihoming)
7682 7683                          strict_check = B_TRUE;
7683 7684                  new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7684 7685                      IRE_LOCAL, ill, ALL_ZONES, NULL,
7685 7686                      (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7686 7687          } else {
7687 7688                  ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7688 7689                  if (ipst->ips_ipv6_strict_dst_multihoming)
7689 7690                          strict_check = B_TRUE;
7690 7691                  new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7691 7692                      IRE_LOCAL, ill, ALL_ZONES, NULL,
7692 7693                      (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7693 7694          }
7694 7695          /*
7695 7696           * If the same ire that was returned in ip_input() is found then this
7696 7697           * is an indication that usesrc groups are in use. The packet
7697 7698           * arrived on a different ill in the group than the one associated with
7698 7699           * the destination address.  If a different ire was found then the same
7699 7700           * IP address must be hosted on multiple ills. This is possible with
7700 7701           * unnumbered point2point interfaces. We switch to use this new ire in
7701 7702           * order to have accurate interface statistics.
7702 7703           */
7703 7704          if (new_ire != NULL) {
7704 7705                  /* Note: held in one case but not the other? Caller handles */
7705 7706                  if (new_ire != ire)
7706 7707                          return (new_ire);
7707 7708                  /* Unchanged */
7708 7709                  ire_refrele(new_ire);
7709 7710                  return (ire);
7710 7711          }
7711 7712  
7712 7713          /*
7713 7714           * Chase pointers once and store locally.
7714 7715           */
7715 7716          ASSERT(ire->ire_ill != NULL);
7716 7717          ire_ill = ire->ire_ill;
7717 7718          ifindex = ill->ill_usesrc_ifindex;
7718 7719  
7719 7720          /*
7720 7721           * Check if it's a legal address on the 'usesrc' interface.
7721 7722           * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7722 7723           * can just check phyint_ifindex.
7723 7724           */
7724 7725          if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7725 7726                  return (ire);
7726 7727          }
7727 7728  
7728 7729          /*
7729 7730           * If the ip*_strict_dst_multihoming switch is on then we can
7730 7731           * only accept this packet if the interface is marked as routing.
7731 7732           */
7732 7733          if (!(strict_check))
7733 7734                  return (ire);
7734 7735  
7735 7736          if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7736 7737                  return (ire);
7737 7738          }
7738 7739          return (NULL);
7739 7740  }
7740 7741  
7741 7742  /*
7742 7743   * This function is used to construct a mac_header_info_s from a
7743 7744   * DL_UNITDATA_IND message.
7744 7745   * The address fields in the mhi structure points into the message,
7745 7746   * thus the caller can't use those fields after freeing the message.
7746 7747   *
7747 7748   * We determine whether the packet received is a non-unicast packet
7748 7749   * and in doing so, determine whether or not it is broadcast vs multicast.
7749 7750   * For it to be a broadcast packet, we must have the appropriate mblk_t
7750 7751   * hanging off the ill_t.  If this is either not present or doesn't match
7751 7752   * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7752 7753   * to be multicast.  Thus NICs that have no broadcast address (or no
7753 7754   * capability for one, such as point to point links) cannot return as
7754 7755   * the packet being broadcast.
7755 7756   */
7756 7757  void
7757 7758  ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7758 7759  {
7759 7760          dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7760 7761          mblk_t *bmp;
7761 7762          uint_t extra_offset;
7762 7763  
7763 7764          bzero(mhip, sizeof (struct mac_header_info_s));
7764 7765  
7765 7766          mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7766 7767  
7767 7768          if (ill->ill_sap_length < 0)
7768 7769                  extra_offset = 0;
7769 7770          else
7770 7771                  extra_offset = ill->ill_sap_length;
7771 7772  
7772 7773          mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7773 7774              extra_offset;
7774 7775          mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7775 7776              extra_offset;
7776 7777  
7777 7778          if (!ind->dl_group_address)
7778 7779                  return;
7779 7780  
7780 7781          /* Multicast or broadcast */
7781 7782          mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7782 7783  
7783 7784          if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7784 7785              ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7785 7786              (bmp = ill->ill_bcast_mp) != NULL) {
7786 7787                  dl_unitdata_req_t *dlur;
7787 7788                  uint8_t *bphys_addr;
7788 7789  
7789 7790                  dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7790 7791                  bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7791 7792                      extra_offset;
7792 7793  
7793 7794                  if (bcmp(mhip->mhi_daddr, bphys_addr,
7794 7795                      ind->dl_dest_addr_length) == 0)
7795 7796                          mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7796 7797          }
7797 7798  }
7798 7799  
7799 7800  /*
7800 7801   * This function is used to construct a mac_header_info_s from a
7801 7802   * M_DATA fastpath message from a DLPI driver.
7802 7803   * The address fields in the mhi structure points into the message,
7803 7804   * thus the caller can't use those fields after freeing the message.
7804 7805   *
7805 7806   * We determine whether the packet received is a non-unicast packet
7806 7807   * and in doing so, determine whether or not it is broadcast vs multicast.
7807 7808   * For it to be a broadcast packet, we must have the appropriate mblk_t
7808 7809   * hanging off the ill_t.  If this is either not present or doesn't match
7809 7810   * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7810 7811   * to be multicast.  Thus NICs that have no broadcast address (or no
7811 7812   * capability for one, such as point to point links) cannot return as
7812 7813   * the packet being broadcast.
7813 7814   */
7814 7815  void
7815 7816  ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7816 7817  {
7817 7818          mblk_t *bmp;
7818 7819          struct ether_header *pether;
7819 7820  
7820 7821          bzero(mhip, sizeof (struct mac_header_info_s));
7821 7822  
7822 7823          mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7823 7824  
7824 7825          pether = (struct ether_header *)((char *)mp->b_rptr
7825 7826              - sizeof (struct ether_header));
7826 7827  
7827 7828          /*
7828 7829           * Make sure the interface is an ethernet type, since we don't
7829 7830           * know the header format for anything but Ethernet. Also make
7830 7831           * sure we are pointing correctly above db_base.
7831 7832           */
7832 7833          if (ill->ill_type != IFT_ETHER)
7833 7834                  return;
7834 7835  
7835 7836  retry:
7836 7837          if ((uchar_t *)pether < mp->b_datap->db_base)
7837 7838                  return;
7838 7839  
7839 7840          /* Is there a VLAN tag? */
7840 7841          if (ill->ill_isv6) {
7841 7842                  if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7842 7843                          pether = (struct ether_header *)((char *)pether - 4);
7843 7844                          goto retry;
7844 7845                  }
7845 7846          } else {
7846 7847                  if (pether->ether_type != htons(ETHERTYPE_IP)) {
7847 7848                          pether = (struct ether_header *)((char *)pether - 4);
7848 7849                          goto retry;
7849 7850                  }
7850 7851          }
7851 7852          mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7852 7853          mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7853 7854  
7854 7855          if (!(mhip->mhi_daddr[0] & 0x01))
7855 7856                  return;
7856 7857  
7857 7858          /* Multicast or broadcast */
7858 7859          mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7859 7860  
7860 7861          if ((bmp = ill->ill_bcast_mp) != NULL) {
7861 7862                  dl_unitdata_req_t *dlur;
7862 7863                  uint8_t *bphys_addr;
7863 7864                  uint_t  addrlen;
7864 7865  
7865 7866                  dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7866 7867                  addrlen = dlur->dl_dest_addr_length;
7867 7868                  if (ill->ill_sap_length < 0) {
7868 7869                          bphys_addr = (uchar_t *)dlur +
7869 7870                              dlur->dl_dest_addr_offset;
7870 7871                          addrlen += ill->ill_sap_length;
7871 7872                  } else {
7872 7873                          bphys_addr = (uchar_t *)dlur +
7873 7874                              dlur->dl_dest_addr_offset +
7874 7875                              ill->ill_sap_length;
7875 7876                          addrlen -= ill->ill_sap_length;
7876 7877                  }
7877 7878                  if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7878 7879                          mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7879 7880          }
7880 7881  }
7881 7882  
7882 7883  /*
7883 7884   * Handle anything but M_DATA messages
7884 7885   * We see the DL_UNITDATA_IND which are part
7885 7886   * of the data path, and also the other messages from the driver.
7886 7887   */
7887 7888  void
7888 7889  ip_rput_notdata(ill_t *ill, mblk_t *mp)
7889 7890  {
7890 7891          mblk_t          *first_mp;
7891 7892          struct iocblk   *iocp;
7892 7893          struct mac_header_info_s mhi;
7893 7894  
7894 7895          switch (DB_TYPE(mp)) {
7895 7896          case M_PROTO:
7896 7897          case M_PCPROTO: {
7897 7898                  if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7898 7899                      DL_UNITDATA_IND) {
7899 7900                          /* Go handle anything other than data elsewhere. */
7900 7901                          ip_rput_dlpi(ill, mp);
7901 7902                          return;
7902 7903                  }
7903 7904  
7904 7905                  first_mp = mp;
7905 7906                  mp = first_mp->b_cont;
7906 7907                  first_mp->b_cont = NULL;
7907 7908  
7908 7909                  if (mp == NULL) {
7909 7910                          freeb(first_mp);
7910 7911                          return;
7911 7912                  }
7912 7913                  ip_dlur_to_mhi(ill, first_mp, &mhi);
7913 7914                  if (ill->ill_isv6)
7914 7915                          ip_input_v6(ill, NULL, mp, &mhi);
7915 7916                  else
7916 7917                          ip_input(ill, NULL, mp, &mhi);
7917 7918  
7918 7919                  /* Ditch the DLPI header. */
7919 7920                  freeb(first_mp);
7920 7921                  return;
7921 7922          }
7922 7923          case M_IOCACK:
7923 7924                  iocp = (struct iocblk *)mp->b_rptr;
7924 7925                  switch (iocp->ioc_cmd) {
7925 7926                  case DL_IOC_HDR_INFO:
7926 7927                          ill_fastpath_ack(ill, mp);
7927 7928                          return;
7928 7929                  default:
7929 7930                          putnext(ill->ill_rq, mp);
7930 7931                          return;
7931 7932                  }
7932 7933                  /* FALLTHRU */
7933 7934          case M_ERROR:
7934 7935          case M_HANGUP:
7935 7936                  mutex_enter(&ill->ill_lock);
7936 7937                  if (ill->ill_state_flags & ILL_CONDEMNED) {
7937 7938                          mutex_exit(&ill->ill_lock);
7938 7939                          freemsg(mp);
7939 7940                          return;
7940 7941                  }
7941 7942                  ill_refhold_locked(ill);
7942 7943                  mutex_exit(&ill->ill_lock);
7943 7944                  qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7944 7945                      B_FALSE);
7945 7946                  return;
7946 7947          case M_CTL:
7947 7948                  putnext(ill->ill_rq, mp);
7948 7949                  return;
7949 7950          case M_IOCNAK:
7950 7951                  ip1dbg(("got iocnak "));
7951 7952                  iocp = (struct iocblk *)mp->b_rptr;
7952 7953                  switch (iocp->ioc_cmd) {
7953 7954                  case DL_IOC_HDR_INFO:
7954 7955                          ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7955 7956                          return;
7956 7957                  default:
7957 7958                          break;
7958 7959                  }
7959 7960                  /* FALLTHRU */
7960 7961          default:
7961 7962                  putnext(ill->ill_rq, mp);
7962 7963                  return;
7963 7964          }
7964 7965  }
7965 7966  
7966 7967  /* Read side put procedure.  Packets coming from the wire arrive here. */
7967 7968  void
7968 7969  ip_rput(queue_t *q, mblk_t *mp)
7969 7970  {
7970 7971          ill_t   *ill;
7971 7972          union DL_primitives *dl;
7972 7973  
7973 7974          ill = (ill_t *)q->q_ptr;
7974 7975  
7975 7976          if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
7976 7977                  /*
7977 7978                   * If things are opening or closing, only accept high-priority
7978 7979                   * DLPI messages.  (On open ill->ill_ipif has not yet been
7979 7980                   * created; on close, things hanging off the ill may have been
7980 7981                   * freed already.)
7981 7982                   */
7982 7983                  dl = (union DL_primitives *)mp->b_rptr;
7983 7984                  if (DB_TYPE(mp) != M_PCPROTO ||
7984 7985                      dl->dl_primitive == DL_UNITDATA_IND) {
7985 7986                          inet_freemsg(mp);
7986 7987                          return;
7987 7988                  }
7988 7989          }
7989 7990          if (DB_TYPE(mp) == M_DATA) {
7990 7991                  struct mac_header_info_s mhi;
7991 7992  
7992 7993                  ip_mdata_to_mhi(ill, mp, &mhi);
7993 7994                  ip_input(ill, NULL, mp, &mhi);
7994 7995          } else {
7995 7996                  ip_rput_notdata(ill, mp);
7996 7997          }
7997 7998  }
7998 7999  
7999 8000  /*
8000 8001   * Move the information to a copy.
8001 8002   */
8002 8003  mblk_t *
8003 8004  ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
8004 8005  {
8005 8006          mblk_t          *mp1;
8006 8007          ill_t           *ill = ira->ira_ill;
8007 8008          ip_stack_t      *ipst = ill->ill_ipst;
8008 8009  
8009 8010          IP_STAT(ipst, ip_db_ref);
8010 8011  
8011 8012          /* Make sure we have ira_l2src before we loose the original mblk */
8012 8013          if (!(ira->ira_flags & IRAF_L2SRC_SET))
8013 8014                  ip_setl2src(mp, ira, ira->ira_rill);
8014 8015  
8015 8016          mp1 = copymsg(mp);
8016 8017          if (mp1 == NULL) {
8017 8018                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8018 8019                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
8019 8020                  freemsg(mp);
8020 8021                  return (NULL);
8021 8022          }
8022 8023          /* preserve the hardware checksum flags and data, if present */
8023 8024          if (DB_CKSUMFLAGS(mp) != 0) {
8024 8025                  DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
8025 8026                  DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
8026 8027                  DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
8027 8028                  DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
8028 8029                  DB_CKSUM16(mp1) = DB_CKSUM16(mp);
8029 8030          }
8030 8031          freemsg(mp);
8031 8032          return (mp1);
8032 8033  }
8033 8034  
8034 8035  static void
8035 8036  ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
8036 8037      t_uscalar_t err)
8037 8038  {
8038 8039          if (dl_err == DL_SYSERR) {
8039 8040                  (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8040 8041                      "%s: %s failed: DL_SYSERR (errno %u)\n",
8041 8042                      ill->ill_name, dl_primstr(prim), err);
8042 8043                  return;
8043 8044          }
8044 8045  
8045 8046          (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8046 8047              "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
8047 8048              dl_errstr(dl_err));
8048 8049  }
8049 8050  
8050 8051  /*
8051 8052   * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
8052 8053   * than DL_UNITDATA_IND messages. If we need to process this message
8053 8054   * exclusively, we call qwriter_ip, in which case we also need to call
8054 8055   * ill_refhold before that, since qwriter_ip does an ill_refrele.
8055 8056   */
8056 8057  void
8057 8058  ip_rput_dlpi(ill_t *ill, mblk_t *mp)
8058 8059  {
8059 8060          dl_ok_ack_t     *dloa = (dl_ok_ack_t *)mp->b_rptr;
8060 8061          dl_error_ack_t  *dlea = (dl_error_ack_t *)dloa;
8061 8062          queue_t         *q = ill->ill_rq;
8062 8063          t_uscalar_t     prim = dloa->dl_primitive;
8063 8064          t_uscalar_t     reqprim = DL_PRIM_INVAL;
8064 8065  
8065 8066          DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
8066 8067              char *, dl_primstr(prim), ill_t *, ill);
8067 8068          ip1dbg(("ip_rput_dlpi"));
8068 8069  
8069 8070          /*
8070 8071           * If we received an ACK but didn't send a request for it, then it
8071 8072           * can't be part of any pending operation; discard up-front.
8072 8073           */
8073 8074          switch (prim) {
8074 8075          case DL_ERROR_ACK:
8075 8076                  reqprim = dlea->dl_error_primitive;
8076 8077                  ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
8077 8078                      "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
8078 8079                      reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
8079 8080                      dlea->dl_unix_errno));
8080 8081                  break;
8081 8082          case DL_OK_ACK:
8082 8083                  reqprim = dloa->dl_correct_primitive;
8083 8084                  break;
8084 8085          case DL_INFO_ACK:
8085 8086                  reqprim = DL_INFO_REQ;
8086 8087                  break;
8087 8088          case DL_BIND_ACK:
8088 8089                  reqprim = DL_BIND_REQ;
8089 8090                  break;
8090 8091          case DL_PHYS_ADDR_ACK:
8091 8092                  reqprim = DL_PHYS_ADDR_REQ;
8092 8093                  break;
8093 8094          case DL_NOTIFY_ACK:
8094 8095                  reqprim = DL_NOTIFY_REQ;
8095 8096                  break;
8096 8097          case DL_CAPABILITY_ACK:
8097 8098                  reqprim = DL_CAPABILITY_REQ;
8098 8099                  break;
8099 8100          }
8100 8101  
8101 8102          if (prim != DL_NOTIFY_IND) {
8102 8103                  if (reqprim == DL_PRIM_INVAL ||
8103 8104                      !ill_dlpi_pending(ill, reqprim)) {
8104 8105                          /* Not a DLPI message we support or expected */
8105 8106                          freemsg(mp);
8106 8107                          return;
8107 8108                  }
8108 8109                  ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
8109 8110                      dl_primstr(reqprim)));
8110 8111          }
8111 8112  
8112 8113          switch (reqprim) {
8113 8114          case DL_UNBIND_REQ:
8114 8115                  /*
8115 8116                   * NOTE: we mark the unbind as complete even if we got a
8116 8117                   * DL_ERROR_ACK, since there's not much else we can do.
8117 8118                   */
8118 8119                  mutex_enter(&ill->ill_lock);
8119 8120                  ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
8120 8121                  cv_signal(&ill->ill_cv);
8121 8122                  mutex_exit(&ill->ill_lock);
8122 8123                  break;
8123 8124  
8124 8125          case DL_ENABMULTI_REQ:
8125 8126                  if (prim == DL_OK_ACK) {
8126 8127                          if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8127 8128                                  ill->ill_dlpi_multicast_state = IDS_OK;
8128 8129                  }
8129 8130                  break;
8130 8131          }
8131 8132  
8132 8133          /*
8133 8134           * The message is one we're waiting for (or DL_NOTIFY_IND), but we
8134 8135           * need to become writer to continue to process it.  Because an
8135 8136           * exclusive operation doesn't complete until replies to all queued
8136 8137           * DLPI messages have been received, we know we're in the middle of an
8137 8138           * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
8138 8139           *
8139 8140           * As required by qwriter_ip(), we refhold the ill; it will refrele.
8140 8141           * Since this is on the ill stream we unconditionally bump up the
8141 8142           * refcount without doing ILL_CAN_LOOKUP().
8142 8143           */
8143 8144          ill_refhold(ill);
8144 8145          if (prim == DL_NOTIFY_IND)
8145 8146                  qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
8146 8147          else
8147 8148                  qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
8148 8149  }
8149 8150  
8150 8151  /*
8151 8152   * Handling of DLPI messages that require exclusive access to the ipsq.
8152 8153   *
8153 8154   * Need to do ipsq_pending_mp_get on ioctl completion, which could
8154 8155   * happen here. (along with mi_copy_done)
8155 8156   */
8156 8157  /* ARGSUSED */
8157 8158  static void
8158 8159  ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8159 8160  {
8160 8161          dl_ok_ack_t     *dloa = (dl_ok_ack_t *)mp->b_rptr;
8161 8162          dl_error_ack_t  *dlea = (dl_error_ack_t *)dloa;
8162 8163          int             err = 0;
8163 8164          ill_t           *ill = (ill_t *)q->q_ptr;
8164 8165          ipif_t          *ipif = NULL;
8165 8166          mblk_t          *mp1 = NULL;
8166 8167          conn_t          *connp = NULL;
8167 8168          t_uscalar_t     paddrreq;
8168 8169          mblk_t          *mp_hw;
8169 8170          boolean_t       success;
8170 8171          boolean_t       ioctl_aborted = B_FALSE;
8171 8172          boolean_t       log = B_TRUE;
8172 8173  
8173 8174          DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
8174 8175              char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
8175 8176  
8176 8177          ip1dbg(("ip_rput_dlpi_writer .."));
8177 8178          ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
8178 8179          ASSERT(IAM_WRITER_ILL(ill));
8179 8180  
8180 8181          ipif = ipsq->ipsq_xop->ipx_pending_ipif;
8181 8182          /*
8182 8183           * The current ioctl could have been aborted by the user and a new
8183 8184           * ioctl to bring up another ill could have started. We could still
8184 8185           * get a response from the driver later.
8185 8186           */
8186 8187          if (ipif != NULL && ipif->ipif_ill != ill)
8187 8188                  ioctl_aborted = B_TRUE;
8188 8189  
8189 8190          switch (dloa->dl_primitive) {
8190 8191          case DL_ERROR_ACK:
8191 8192                  ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
8192 8193                      dl_primstr(dlea->dl_error_primitive)));
8193 8194  
8194 8195                  DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
8195 8196                      char *, dl_primstr(dlea->dl_error_primitive),
8196 8197                      ill_t *, ill);
8197 8198  
8198 8199                  switch (dlea->dl_error_primitive) {
8199 8200                  case DL_DISABMULTI_REQ:
8200 8201                          ill_dlpi_done(ill, dlea->dl_error_primitive);
8201 8202                          break;
8202 8203                  case DL_PROMISCON_REQ:
8203 8204                  case DL_PROMISCOFF_REQ:
8204 8205                  case DL_UNBIND_REQ:
8205 8206                  case DL_ATTACH_REQ:
8206 8207                  case DL_INFO_REQ:
8207 8208                          ill_dlpi_done(ill, dlea->dl_error_primitive);
8208 8209                          break;
8209 8210                  case DL_NOTIFY_REQ:
8210 8211                          ill_dlpi_done(ill, DL_NOTIFY_REQ);
8211 8212                          log = B_FALSE;
8212 8213                          break;
8213 8214                  case DL_PHYS_ADDR_REQ:
8214 8215                          /*
8215 8216                           * For IPv6 only, there are two additional
8216 8217                           * phys_addr_req's sent to the driver to get the
8217 8218                           * IPv6 token and lla. This allows IP to acquire
8218 8219                           * the hardware address format for a given interface
8219 8220                           * without having built in knowledge of the hardware
8220 8221                           * address. ill_phys_addr_pend keeps track of the last
8221 8222                           * DL_PAR sent so we know which response we are
8222 8223                           * dealing with. ill_dlpi_done will update
8223 8224                           * ill_phys_addr_pend when it sends the next req.
8224 8225                           * We don't complete the IOCTL until all three DL_PARs
8225 8226                           * have been attempted, so set *_len to 0 and break.
8226 8227                           */
8227 8228                          paddrreq = ill->ill_phys_addr_pend;
8228 8229                          ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8229 8230                          if (paddrreq == DL_IPV6_TOKEN) {
8230 8231                                  ill->ill_token_length = 0;
8231 8232                                  log = B_FALSE;
8232 8233                                  break;
8233 8234                          } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8234 8235                                  ill->ill_nd_lla_len = 0;
8235 8236                                  log = B_FALSE;
8236 8237                                  break;
8237 8238                          }
8238 8239                          /*
8239 8240                           * Something went wrong with the DL_PHYS_ADDR_REQ.
8240 8241                           * We presumably have an IOCTL hanging out waiting
8241 8242                           * for completion. Find it and complete the IOCTL
8242 8243                           * with the error noted.
8243 8244                           * However, ill_dl_phys was called on an ill queue
8244 8245                           * (from SIOCSLIFNAME), thus conn_pending_ill is not
8245 8246                           * set. But the ioctl is known to be pending on ill_wq.
8246 8247                           */
8247 8248                          if (!ill->ill_ifname_pending)
8248 8249                                  break;
8249 8250                          ill->ill_ifname_pending = 0;
8250 8251                          if (!ioctl_aborted)
8251 8252                                  mp1 = ipsq_pending_mp_get(ipsq, &connp);
8252 8253                          if (mp1 != NULL) {
8253 8254                                  /*
8254 8255                                   * This operation (SIOCSLIFNAME) must have
8255 8256                                   * happened on the ill. Assert there is no conn
8256 8257                                   */
8257 8258                                  ASSERT(connp == NULL);
8258 8259                                  q = ill->ill_wq;
8259 8260                          }
8260 8261                          break;
8261 8262                  case DL_BIND_REQ:
8262 8263                          ill_dlpi_done(ill, DL_BIND_REQ);
8263 8264                          if (ill->ill_ifname_pending)
8264 8265                                  break;
8265 8266                          mutex_enter(&ill->ill_lock);
8266 8267                          ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8267 8268                          mutex_exit(&ill->ill_lock);
8268 8269                          /*
8269 8270                           * Something went wrong with the bind.  We presumably
8270 8271                           * have an IOCTL hanging out waiting for completion.
8271 8272                           * Find it, take down the interface that was coming
8272 8273                           * up, and complete the IOCTL with the error noted.
8273 8274                           */
8274 8275                          if (!ioctl_aborted)
8275 8276                                  mp1 = ipsq_pending_mp_get(ipsq, &connp);
8276 8277                          if (mp1 != NULL) {
8277 8278                                  /*
8278 8279                                   * This might be a result of a DL_NOTE_REPLUMB
8279 8280                                   * notification. In that case, connp is NULL.
8280 8281                                   */
8281 8282                                  if (connp != NULL)
8282 8283                                          q = CONNP_TO_WQ(connp);
8283 8284  
8284 8285                                  (void) ipif_down(ipif, NULL, NULL);
8285 8286                                  /* error is set below the switch */
8286 8287                          }
8287 8288                          break;
8288 8289                  case DL_ENABMULTI_REQ:
8289 8290                          ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8290 8291  
8291 8292                          if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8292 8293                                  ill->ill_dlpi_multicast_state = IDS_FAILED;
8293 8294                          if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8294 8295  
8295 8296                                  printf("ip: joining multicasts failed (%d)"
8296 8297                                      " on %s - will use link layer "
8297 8298                                      "broadcasts for multicast\n",
8298 8299                                      dlea->dl_errno, ill->ill_name);
8299 8300  
8300 8301                                  /*
8301 8302                                   * Set up for multi_bcast; We are the
8302 8303                                   * writer, so ok to access ill->ill_ipif
8303 8304                                   * without any lock.
8304 8305                                   */
8305 8306                                  mutex_enter(&ill->ill_phyint->phyint_lock);
8306 8307                                  ill->ill_phyint->phyint_flags |=
8307 8308                                      PHYI_MULTI_BCAST;
8308 8309                                  mutex_exit(&ill->ill_phyint->phyint_lock);
8309 8310  
8310 8311                          }
8311 8312                          freemsg(mp);    /* Don't want to pass this up */
8312 8313                          return;
8313 8314                  case DL_CAPABILITY_REQ:
8314 8315                          ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8315 8316                              "DL_CAPABILITY REQ\n"));
8316 8317                          if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8317 8318                                  ill->ill_dlpi_capab_state = IDCS_FAILED;
8318 8319                          ill_capability_done(ill);
8319 8320                          freemsg(mp);
8320 8321                          return;
8321 8322                  }
8322 8323                  /*
8323 8324                   * Note the error for IOCTL completion (mp1 is set when
8324 8325                   * ready to complete ioctl). If ill_ifname_pending_err is
8325 8326                   * set, an error occured during plumbing (ill_ifname_pending),
8326 8327                   * so we want to report that error.
8327 8328                   *
8328 8329                   * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8329 8330                   * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8330 8331                   * expected to get errack'd if the driver doesn't support
8331 8332                   * these flags (e.g. ethernet). log will be set to B_FALSE
8332 8333                   * if these error conditions are encountered.
8333 8334                   */
8334 8335                  if (mp1 != NULL) {
8335 8336                          if (ill->ill_ifname_pending_err != 0)  {
8336 8337                                  err = ill->ill_ifname_pending_err;
8337 8338                                  ill->ill_ifname_pending_err = 0;
8338 8339                          } else {
8339 8340                                  err = dlea->dl_unix_errno ?
8340 8341                                      dlea->dl_unix_errno : ENXIO;
8341 8342                          }
8342 8343                  /*
8343 8344                   * If we're plumbing an interface and an error hasn't already
8344 8345                   * been saved, set ill_ifname_pending_err to the error passed
8345 8346                   * up. Ignore the error if log is B_FALSE (see comment above).
8346 8347                   */
8347 8348                  } else if (log && ill->ill_ifname_pending &&
8348 8349                      ill->ill_ifname_pending_err == 0) {
8349 8350                          ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8350 8351                              dlea->dl_unix_errno : ENXIO;
8351 8352                  }
8352 8353  
8353 8354                  if (log)
8354 8355                          ip_dlpi_error(ill, dlea->dl_error_primitive,
8355 8356                              dlea->dl_errno, dlea->dl_unix_errno);
8356 8357                  break;
8357 8358          case DL_CAPABILITY_ACK:
8358 8359                  ill_capability_ack(ill, mp);
8359 8360                  /*
8360 8361                   * The message has been handed off to ill_capability_ack
8361 8362                   * and must not be freed below
8362 8363                   */
8363 8364                  mp = NULL;
8364 8365                  break;
8365 8366  
8366 8367          case DL_INFO_ACK:
8367 8368                  /* Call a routine to handle this one. */
8368 8369                  ill_dlpi_done(ill, DL_INFO_REQ);
8369 8370                  ip_ll_subnet_defaults(ill, mp);
8370 8371                  ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8371 8372                  return;
8372 8373          case DL_BIND_ACK:
8373 8374                  /*
8374 8375                   * We should have an IOCTL waiting on this unless
8375 8376                   * sent by ill_dl_phys, in which case just return
8376 8377                   */
8377 8378                  ill_dlpi_done(ill, DL_BIND_REQ);
8378 8379  
8379 8380                  if (ill->ill_ifname_pending) {
8380 8381                          DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8381 8382                              ill_t *, ill, mblk_t *, mp);
8382 8383                          break;
8383 8384                  }
8384 8385                  mutex_enter(&ill->ill_lock);
8385 8386                  ill->ill_dl_up = 1;
8386 8387                  ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8387 8388                  mutex_exit(&ill->ill_lock);
8388 8389  
8389 8390                  if (!ioctl_aborted)
8390 8391                          mp1 = ipsq_pending_mp_get(ipsq, &connp);
8391 8392                  if (mp1 == NULL) {
8392 8393                          DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8393 8394                          break;
8394 8395                  }
8395 8396                  /*
8396 8397                   * mp1 was added by ill_dl_up(). if that is a result of
8397 8398                   * a DL_NOTE_REPLUMB notification, connp could be NULL.
8398 8399                   */
8399 8400                  if (connp != NULL)
8400 8401                          q = CONNP_TO_WQ(connp);
8401 8402                  /*
8402 8403                   * We are exclusive. So nothing can change even after
8403 8404                   * we get the pending mp.
8404 8405                   */
8405 8406                  ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8406 8407                  DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8407 8408                  ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8408 8409  
8409 8410                  /*
8410 8411                   * Now bring up the resolver; when that is complete, we'll
8411 8412                   * create IREs.  Note that we intentionally mirror what
8412 8413                   * ipif_up() would have done, because we got here by way of
8413 8414                   * ill_dl_up(), which stopped ipif_up()'s processing.
8414 8415                   */
8415 8416                  if (ill->ill_isv6) {
8416 8417                          /*
8417 8418                           * v6 interfaces.
8418 8419                           * Unlike ARP which has to do another bind
8419 8420                           * and attach, once we get here we are
8420 8421                           * done with NDP
8421 8422                           */
8422 8423                          (void) ipif_resolver_up(ipif, Res_act_initial);
8423 8424                          if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8424 8425                                  err = ipif_up_done_v6(ipif);
8425 8426                  } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8426 8427                          /*
8427 8428                           * ARP and other v4 external resolvers.
8428 8429                           * Leave the pending mblk intact so that
8429 8430                           * the ioctl completes in ip_rput().
8430 8431                           */
8431 8432                          if (connp != NULL)
8432 8433                                  mutex_enter(&connp->conn_lock);
8433 8434                          mutex_enter(&ill->ill_lock);
8434 8435                          success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8435 8436                          mutex_exit(&ill->ill_lock);
8436 8437                          if (connp != NULL)
8437 8438                                  mutex_exit(&connp->conn_lock);
8438 8439                          if (success) {
8439 8440                                  err = ipif_resolver_up(ipif, Res_act_initial);
8440 8441                                  if (err == EINPROGRESS) {
8441 8442                                          freemsg(mp);
8442 8443                                          return;
8443 8444                                  }
8444 8445                                  mp1 = ipsq_pending_mp_get(ipsq, &connp);
8445 8446                          } else {
8446 8447                                  /* The conn has started closing */
8447 8448                                  err = EINTR;
8448 8449                          }
8449 8450                  } else {
8450 8451                          /*
8451 8452                           * This one is complete. Reply to pending ioctl.
8452 8453                           */
8453 8454                          (void) ipif_resolver_up(ipif, Res_act_initial);
8454 8455                          err = ipif_up_done(ipif);
8455 8456                  }
8456 8457  
8457 8458                  if ((err == 0) && (ill->ill_up_ipifs)) {
8458 8459                          err = ill_up_ipifs(ill, q, mp1);
8459 8460                          if (err == EINPROGRESS) {
8460 8461                                  freemsg(mp);
8461 8462                                  return;
8462 8463                          }
8463 8464                  }
8464 8465  
8465 8466                  /*
8466 8467                   * If we have a moved ipif to bring up, and everything has
8467 8468                   * succeeded to this point, bring it up on the IPMP ill.
8468 8469                   * Otherwise, leave it down -- the admin can try to bring it
8469 8470                   * up by hand if need be.
8470 8471                   */
8471 8472                  if (ill->ill_move_ipif != NULL) {
8472 8473                          if (err != 0) {
8473 8474                                  ill->ill_move_ipif = NULL;
8474 8475                          } else {
8475 8476                                  ipif = ill->ill_move_ipif;
8476 8477                                  ill->ill_move_ipif = NULL;
8477 8478                                  err = ipif_up(ipif, q, mp1);
8478 8479                                  if (err == EINPROGRESS) {
8479 8480                                          freemsg(mp);
8480 8481                                          return;
8481 8482                                  }
8482 8483                          }
8483 8484                  }
8484 8485                  break;
8485 8486  
8486 8487          case DL_NOTIFY_IND: {
8487 8488                  dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8488 8489                  uint_t orig_mtu, orig_mc_mtu;
8489 8490  
8490 8491                  switch (notify->dl_notification) {
8491 8492                  case DL_NOTE_PHYS_ADDR:
8492 8493                          err = ill_set_phys_addr(ill, mp);
8493 8494                          break;
8494 8495  
8495 8496                  case DL_NOTE_REPLUMB:
8496 8497                          /*
8497 8498                           * Directly return after calling ill_replumb().
8498 8499                           * Note that we should not free mp as it is reused
8499 8500                           * in the ill_replumb() function.
8500 8501                           */
8501 8502                          err = ill_replumb(ill, mp);
8502 8503                          return;
8503 8504  
8504 8505                  case DL_NOTE_FASTPATH_FLUSH:
8505 8506                          nce_flush(ill, B_FALSE);
8506 8507                          break;
8507 8508  
8508 8509                  case DL_NOTE_SDU_SIZE:
8509 8510                  case DL_NOTE_SDU_SIZE2:
8510 8511                          /*
8511 8512                           * The dce and fragmentation code can cope with
8512 8513                           * this changing while packets are being sent.
8513 8514                           * When packets are sent ip_output will discover
8514 8515                           * a change.
8515 8516                           *
8516 8517                           * Change the MTU size of the interface.
8517 8518                           */
8518 8519                          mutex_enter(&ill->ill_lock);
8519 8520                          orig_mtu = ill->ill_mtu;
8520 8521                          orig_mc_mtu = ill->ill_mc_mtu;
8521 8522                          switch (notify->dl_notification) {
8522 8523                          case DL_NOTE_SDU_SIZE:
8523 8524                                  ill->ill_current_frag =
8524 8525                                      (uint_t)notify->dl_data;
8525 8526                                  ill->ill_mc_mtu = (uint_t)notify->dl_data;
8526 8527                                  break;
8527 8528                          case DL_NOTE_SDU_SIZE2:
8528 8529                                  ill->ill_current_frag =
8529 8530                                      (uint_t)notify->dl_data1;
8530 8531                                  ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8531 8532                                  break;
8532 8533                          }
8533 8534                          if (ill->ill_current_frag > ill->ill_max_frag)
8534 8535                                  ill->ill_max_frag = ill->ill_current_frag;
8535 8536  
8536 8537                          if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8537 8538                                  ill->ill_mtu = ill->ill_current_frag;
8538 8539  
8539 8540                                  /*
8540 8541                                   * If ill_user_mtu was set (via
8541 8542                                   * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8542 8543                                   */
8543 8544                                  if (ill->ill_user_mtu != 0 &&
8544 8545                                      ill->ill_user_mtu < ill->ill_mtu)
8545 8546                                          ill->ill_mtu = ill->ill_user_mtu;
8546 8547  
8547 8548                                  if (ill->ill_user_mtu != 0 &&
8548 8549                                      ill->ill_user_mtu < ill->ill_mc_mtu)
8549 8550                                          ill->ill_mc_mtu = ill->ill_user_mtu;
8550 8551  
8551 8552                                  if (ill->ill_isv6) {
8552 8553                                          if (ill->ill_mtu < IPV6_MIN_MTU)
8553 8554                                                  ill->ill_mtu = IPV6_MIN_MTU;
8554 8555                                          if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8555 8556                                                  ill->ill_mc_mtu = IPV6_MIN_MTU;
8556 8557                                  } else {
8557 8558                                          if (ill->ill_mtu < IP_MIN_MTU)
8558 8559                                                  ill->ill_mtu = IP_MIN_MTU;
8559 8560                                          if (ill->ill_mc_mtu < IP_MIN_MTU)
8560 8561                                                  ill->ill_mc_mtu = IP_MIN_MTU;
8561 8562                                  }
8562 8563                          } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8563 8564                                  ill->ill_mc_mtu = ill->ill_mtu;
8564 8565                          }
8565 8566  
8566 8567                          mutex_exit(&ill->ill_lock);
8567 8568                          /*
8568 8569                           * Make sure all dce_generation checks find out
8569 8570                           * that ill_mtu/ill_mc_mtu has changed.
8570 8571                           */
8571 8572                          if (orig_mtu != ill->ill_mtu ||
8572 8573                              orig_mc_mtu != ill->ill_mc_mtu) {
8573 8574                                  dce_increment_all_generations(ill->ill_isv6,
8574 8575                                      ill->ill_ipst);
8575 8576                          }
8576 8577  
8577 8578                          /*
8578 8579                           * Refresh IPMP meta-interface MTU if necessary.
8579 8580                           */
8580 8581                          if (IS_UNDER_IPMP(ill))
8581 8582                                  ipmp_illgrp_refresh_mtu(ill->ill_grp);
8582 8583                          break;
8583 8584  
8584 8585                  case DL_NOTE_LINK_UP:
8585 8586                  case DL_NOTE_LINK_DOWN: {
8586 8587                          /*
8587 8588                           * We are writer. ill / phyint / ipsq assocs stable.
8588 8589                           * The RUNNING flag reflects the state of the link.
8589 8590                           */
8590 8591                          phyint_t *phyint = ill->ill_phyint;
8591 8592                          uint64_t new_phyint_flags;
8592 8593                          boolean_t changed = B_FALSE;
8593 8594                          boolean_t went_up;
8594 8595  
8595 8596                          went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8596 8597                          mutex_enter(&phyint->phyint_lock);
8597 8598  
8598 8599                          new_phyint_flags = went_up ?
8599 8600                              phyint->phyint_flags | PHYI_RUNNING :
8600 8601                              phyint->phyint_flags & ~PHYI_RUNNING;
8601 8602  
8602 8603                          if (IS_IPMP(ill)) {
8603 8604                                  new_phyint_flags = went_up ?
8604 8605                                      new_phyint_flags & ~PHYI_FAILED :
8605 8606                                      new_phyint_flags | PHYI_FAILED;
8606 8607                          }
8607 8608  
8608 8609                          if (new_phyint_flags != phyint->phyint_flags) {
8609 8610                                  phyint->phyint_flags = new_phyint_flags;
8610 8611                                  changed = B_TRUE;
8611 8612                          }
8612 8613                          mutex_exit(&phyint->phyint_lock);
8613 8614                          /*
8614 8615                           * ill_restart_dad handles the DAD restart and routing
8615 8616                           * socket notification logic.
8616 8617                           */
8617 8618                          if (changed) {
8618 8619                                  ill_restart_dad(phyint->phyint_illv4, went_up);
8619 8620                                  ill_restart_dad(phyint->phyint_illv6, went_up);
8620 8621                          }
8621 8622                          break;
8622 8623                  }
8623 8624                  case DL_NOTE_PROMISC_ON_PHYS: {
8624 8625                          phyint_t *phyint = ill->ill_phyint;
8625 8626  
8626 8627                          mutex_enter(&phyint->phyint_lock);
8627 8628                          phyint->phyint_flags |= PHYI_PROMISC;
8628 8629                          mutex_exit(&phyint->phyint_lock);
8629 8630                          break;
8630 8631                  }
8631 8632                  case DL_NOTE_PROMISC_OFF_PHYS: {
8632 8633                          phyint_t *phyint = ill->ill_phyint;
8633 8634  
8634 8635                          mutex_enter(&phyint->phyint_lock);
8635 8636                          phyint->phyint_flags &= ~PHYI_PROMISC;
8636 8637                          mutex_exit(&phyint->phyint_lock);
8637 8638                          break;
8638 8639                  }
8639 8640                  case DL_NOTE_CAPAB_RENEG:
8640 8641                          /*
8641 8642                           * Something changed on the driver side.
8642 8643                           * It wants us to renegotiate the capabilities
8643 8644                           * on this ill. One possible cause is the aggregation
8644 8645                           * interface under us where a port got added or
8645 8646                           * went away.
8646 8647                           *
8647 8648                           * If the capability negotiation is already done
8648 8649                           * or is in progress, reset the capabilities and
8649 8650                           * mark the ill's ill_capab_reneg to be B_TRUE,
8650 8651                           * so that when the ack comes back, we can start
8651 8652                           * the renegotiation process.
8652 8653                           *
8653 8654                           * Note that if ill_capab_reneg is already B_TRUE
8654 8655                           * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8655 8656                           * the capability resetting request has been sent
8656 8657                           * and the renegotiation has not been started yet;
8657 8658                           * nothing needs to be done in this case.
8658 8659                           */
8659 8660                          ipsq_current_start(ipsq, ill->ill_ipif, 0);
8660 8661                          ill_capability_reset(ill, B_TRUE);
8661 8662                          ipsq_current_finish(ipsq);
8662 8663                          break;
8663 8664  
8664 8665                  case DL_NOTE_ALLOWED_IPS:
8665 8666                          ill_set_allowed_ips(ill, mp);
8666 8667                          break;
8667 8668                  default:
8668 8669                          ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8669 8670                              "type 0x%x for DL_NOTIFY_IND\n",
8670 8671                              notify->dl_notification));
8671 8672                          break;
8672 8673                  }
8673 8674  
8674 8675                  /*
8675 8676                   * As this is an asynchronous operation, we
8676 8677                   * should not call ill_dlpi_done
8677 8678                   */
8678 8679                  break;
8679 8680          }
8680 8681          case DL_NOTIFY_ACK: {
8681 8682                  dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8682 8683  
8683 8684                  if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8684 8685                          ill->ill_note_link = 1;
8685 8686                  ill_dlpi_done(ill, DL_NOTIFY_REQ);
8686 8687                  break;
8687 8688          }
8688 8689          case DL_PHYS_ADDR_ACK: {
8689 8690                  /*
8690 8691                   * As part of plumbing the interface via SIOCSLIFNAME,
8691 8692                   * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8692 8693                   * whose answers we receive here.  As each answer is received,
8693 8694                   * we call ill_dlpi_done() to dispatch the next request as
8694 8695                   * we're processing the current one.  Once all answers have
8695 8696                   * been received, we use ipsq_pending_mp_get() to dequeue the
8696 8697                   * outstanding IOCTL and reply to it.  (Because ill_dl_phys()
8697 8698                   * is invoked from an ill queue, conn_oper_pending_ill is not
8698 8699                   * available, but we know the ioctl is pending on ill_wq.)
8699 8700                   */
8700 8701                  uint_t  paddrlen, paddroff;
8701 8702                  uint8_t *addr;
8702 8703  
8703 8704                  paddrreq = ill->ill_phys_addr_pend;
8704 8705                  paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8705 8706                  paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8706 8707                  addr = mp->b_rptr + paddroff;
8707 8708  
8708 8709                  ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8709 8710                  if (paddrreq == DL_IPV6_TOKEN) {
8710 8711                          /*
8711 8712                           * bcopy to low-order bits of ill_token
8712 8713                           *
8713 8714                           * XXX Temporary hack - currently, all known tokens
8714 8715                           * are 64 bits, so I'll cheat for the moment.
8715 8716                           */
8716 8717                          bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8717 8718                          ill->ill_token_length = paddrlen;
8718 8719                          break;
8719 8720                  } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8720 8721                          ASSERT(ill->ill_nd_lla_mp == NULL);
8721 8722                          ill_set_ndmp(ill, mp, paddroff, paddrlen);
8722 8723                          mp = NULL;
8723 8724                          break;
8724 8725                  } else if (paddrreq == DL_CURR_DEST_ADDR) {
8725 8726                          ASSERT(ill->ill_dest_addr_mp == NULL);
8726 8727                          ill->ill_dest_addr_mp = mp;
8727 8728                          ill->ill_dest_addr = addr;
8728 8729                          mp = NULL;
8729 8730                          if (ill->ill_isv6) {
8730 8731                                  ill_setdesttoken(ill);
8731 8732                                  ipif_setdestlinklocal(ill->ill_ipif);
8732 8733                          }
8733 8734                          break;
8734 8735                  }
8735 8736  
8736 8737                  ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8737 8738                  ASSERT(ill->ill_phys_addr_mp == NULL);
8738 8739                  if (!ill->ill_ifname_pending)
8739 8740                          break;
8740 8741                  ill->ill_ifname_pending = 0;
8741 8742                  if (!ioctl_aborted)
8742 8743                          mp1 = ipsq_pending_mp_get(ipsq, &connp);
8743 8744                  if (mp1 != NULL) {
8744 8745                          ASSERT(connp == NULL);
8745 8746                          q = ill->ill_wq;
8746 8747                  }
8747 8748                  /*
8748 8749                   * If any error acks received during the plumbing sequence,
8749 8750                   * ill_ifname_pending_err will be set. Break out and send up
8750 8751                   * the error to the pending ioctl.
8751 8752                   */
8752 8753                  if (ill->ill_ifname_pending_err != 0) {
8753 8754                          err = ill->ill_ifname_pending_err;
8754 8755                          ill->ill_ifname_pending_err = 0;
8755 8756                          break;
8756 8757                  }
8757 8758  
8758 8759                  ill->ill_phys_addr_mp = mp;
8759 8760                  ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8760 8761                  mp = NULL;
8761 8762  
8762 8763                  /*
8763 8764                   * If paddrlen or ill_phys_addr_length is zero, the DLPI
8764 8765                   * provider doesn't support physical addresses.  We check both
8765 8766                   * paddrlen and ill_phys_addr_length because sppp (PPP) does
8766 8767                   * not have physical addresses, but historically adversises a
8767 8768                   * physical address length of 0 in its DL_INFO_ACK, but 6 in
8768 8769                   * its DL_PHYS_ADDR_ACK.
8769 8770                   */
8770 8771                  if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8771 8772                          ill->ill_phys_addr = NULL;
8772 8773                  } else if (paddrlen != ill->ill_phys_addr_length) {
8773 8774                          ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8774 8775                              paddrlen, ill->ill_phys_addr_length));
8775 8776                          err = EINVAL;
8776 8777                          break;
8777 8778                  }
8778 8779  
8779 8780                  if (ill->ill_nd_lla_mp == NULL) {
8780 8781                          if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8781 8782                                  err = ENOMEM;
8782 8783                                  break;
8783 8784                          }
8784 8785                          ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8785 8786                  }
8786 8787  
8787 8788                  if (ill->ill_isv6) {
8788 8789                          ill_setdefaulttoken(ill);
8789 8790                          ipif_setlinklocal(ill->ill_ipif);
8790 8791                  }
8791 8792                  break;
8792 8793          }
8793 8794          case DL_OK_ACK:
8794 8795                  ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8795 8796                      dl_primstr((int)dloa->dl_correct_primitive),
8796 8797                      dloa->dl_correct_primitive));
8797 8798                  DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8798 8799                      char *, dl_primstr(dloa->dl_correct_primitive),
8799 8800                      ill_t *, ill);
8800 8801  
8801 8802                  switch (dloa->dl_correct_primitive) {
8802 8803                  case DL_ENABMULTI_REQ:
8803 8804                  case DL_DISABMULTI_REQ:
8804 8805                          ill_dlpi_done(ill, dloa->dl_correct_primitive);
8805 8806                          break;
8806 8807                  case DL_PROMISCON_REQ:
8807 8808                  case DL_PROMISCOFF_REQ:
8808 8809                  case DL_UNBIND_REQ:
8809 8810                  case DL_ATTACH_REQ:
8810 8811                          ill_dlpi_done(ill, dloa->dl_correct_primitive);
8811 8812                          break;
8812 8813                  }
8813 8814                  break;
8814 8815          default:
8815 8816                  break;
8816 8817          }
8817 8818  
8818 8819          freemsg(mp);
8819 8820          if (mp1 == NULL)
8820 8821                  return;
8821 8822  
8822 8823          /*
8823 8824           * The operation must complete without EINPROGRESS since
8824 8825           * ipsq_pending_mp_get() has removed the mblk (mp1).  Otherwise,
8825 8826           * the operation will be stuck forever inside the IPSQ.
8826 8827           */
8827 8828          ASSERT(err != EINPROGRESS);
8828 8829  
8829 8830          DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8830 8831              int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8831 8832              ipif_t *, NULL);
8832 8833  
8833 8834          switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8834 8835          case 0:
8835 8836                  ipsq_current_finish(ipsq);
8836 8837                  break;
8837 8838  
8838 8839          case SIOCSLIFNAME:
8839 8840          case IF_UNITSEL: {
8840 8841                  ill_t *ill_other = ILL_OTHER(ill);
8841 8842  
8842 8843                  /*
8843 8844                   * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8844 8845                   * ill has a peer which is in an IPMP group, then place ill
8845 8846                   * into the same group.  One catch: although ifconfig plumbs
8846 8847                   * the appropriate IPMP meta-interface prior to plumbing this
8847 8848                   * ill, it is possible for multiple ifconfig applications to
8848 8849                   * race (or for another application to adjust plumbing), in
8849 8850                   * which case the IPMP meta-interface we need will be missing.
8850 8851                   * If so, kick the phyint out of the group.
8851 8852                   */
8852 8853                  if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8853 8854                          ipmp_grp_t      *grp = ill->ill_phyint->phyint_grp;
8854 8855                          ipmp_illgrp_t   *illg;
8855 8856  
8856 8857                          illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8857 8858                          if (illg == NULL)
8858 8859                                  ipmp_phyint_leave_grp(ill->ill_phyint);
8859 8860                          else
8860 8861                                  ipmp_ill_join_illgrp(ill, illg);
8861 8862                  }
8862 8863  
8863 8864                  if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8864 8865                          ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8865 8866                  else
8866 8867                          ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8867 8868                  break;
8868 8869          }
8869 8870          case SIOCLIFADDIF:
8870 8871                  ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8871 8872                  break;
8872 8873  
8873 8874          default:
8874 8875                  ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8875 8876                  break;
8876 8877          }
8877 8878  }
8878 8879  
8879 8880  /*
8880 8881   * ip_rput_other is called by ip_rput to handle messages modifying the global
8881 8882   * state in IP.  If 'ipsq' is non-NULL, caller is writer on it.
8882 8883   */
8883 8884  /* ARGSUSED */
8884 8885  void
8885 8886  ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8886 8887  {
8887 8888          ill_t           *ill = q->q_ptr;
8888 8889          struct iocblk   *iocp;
8889 8890  
8890 8891          ip1dbg(("ip_rput_other "));
8891 8892          if (ipsq != NULL) {
8892 8893                  ASSERT(IAM_WRITER_IPSQ(ipsq));
8893 8894                  ASSERT(ipsq->ipsq_xop ==
8894 8895                      ill->ill_phyint->phyint_ipsq->ipsq_xop);
8895 8896          }
8896 8897  
8897 8898          switch (mp->b_datap->db_type) {
8898 8899          case M_ERROR:
8899 8900          case M_HANGUP:
8900 8901                  /*
8901 8902                   * The device has a problem.  We force the ILL down.  It can
8902 8903                   * be brought up again manually using SIOCSIFFLAGS (via
8903 8904                   * ifconfig or equivalent).
8904 8905                   */
8905 8906                  ASSERT(ipsq != NULL);
8906 8907                  if (mp->b_rptr < mp->b_wptr)
8907 8908                          ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8908 8909                  if (ill->ill_error == 0)
8909 8910                          ill->ill_error = ENXIO;
8910 8911                  if (!ill_down_start(q, mp))
8911 8912                          return;
8912 8913                  ipif_all_down_tail(ipsq, q, mp, NULL);
8913 8914                  break;
8914 8915          case M_IOCNAK: {
8915 8916                  iocp = (struct iocblk *)mp->b_rptr;
8916 8917  
8917 8918                  ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8918 8919                  /*
8919 8920                   * If this was the first attempt, turn off the fastpath
8920 8921                   * probing.
8921 8922                   */
8922 8923                  mutex_enter(&ill->ill_lock);
8923 8924                  if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8924 8925                          ill->ill_dlpi_fastpath_state = IDS_FAILED;
8925 8926                          mutex_exit(&ill->ill_lock);
8926 8927                          /*
8927 8928                           * don't flush the nce_t entries: we use them
8928 8929                           * as an index to the ncec itself.
8929 8930                           */
8930 8931                          ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8931 8932                              ill->ill_name));
8932 8933                  } else {
8933 8934                          mutex_exit(&ill->ill_lock);
8934 8935                  }
8935 8936                  freemsg(mp);
8936 8937                  break;
8937 8938          }
8938 8939          default:
8939 8940                  ASSERT(0);
8940 8941                  break;
8941 8942          }
8942 8943  }
8943 8944  
8944 8945  /*
8945 8946   * Update any source route, record route or timestamp options
8946 8947   * When it fails it has consumed the message and BUMPed the MIB.
8947 8948   */
8948 8949  boolean_t
8949 8950  ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8950 8951      ip_recv_attr_t *ira)
8951 8952  {
8952 8953          ipoptp_t        opts;
8953 8954          uchar_t         *opt;
8954 8955          uint8_t         optval;
8955 8956          uint8_t         optlen;
8956 8957          ipaddr_t        dst;
8957 8958          ipaddr_t        ifaddr;
8958 8959          uint32_t        ts;
8959 8960          timestruc_t     now;
8960 8961          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
8961 8962  
8962 8963          ip2dbg(("ip_forward_options\n"));
8963 8964          dst = ipha->ipha_dst;
8964 8965          for (optval = ipoptp_first(&opts, ipha);
8965 8966              optval != IPOPT_EOL;
8966 8967              optval = ipoptp_next(&opts)) {
8967 8968                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8968 8969                  opt = opts.ipoptp_cur;
8969 8970                  optlen = opts.ipoptp_len;
8970 8971                  ip2dbg(("ip_forward_options: opt %d, len %d\n",
8971 8972                      optval, opts.ipoptp_len));
8972 8973                  switch (optval) {
8973 8974                          uint32_t off;
8974 8975                  case IPOPT_SSRR:
8975 8976                  case IPOPT_LSRR:
8976 8977                          /* Check if adminstratively disabled */
8977 8978                          if (!ipst->ips_ip_forward_src_routed) {
8978 8979                                  BUMP_MIB(dst_ill->ill_ip_mib,
8979 8980                                      ipIfStatsForwProhibits);
8980 8981                                  ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
8981 8982                                      mp, dst_ill);
8982 8983                                  icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
8983 8984                                      ira);
8984 8985                                  return (B_FALSE);
8985 8986                          }
8986 8987                          if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
8987 8988                                  /*
8988 8989                                   * Must be partial since ip_input_options
8989 8990                                   * checked for strict.
8990 8991                                   */
8991 8992                                  break;
8992 8993                          }
8993 8994                          off = opt[IPOPT_OFFSET];
8994 8995                          off--;
8995 8996                  redo_srr:
8996 8997                          if (optlen < IP_ADDR_LEN ||
8997 8998                              off > optlen - IP_ADDR_LEN) {
8998 8999                                  /* End of source route */
8999 9000                                  ip1dbg((
9000 9001                                      "ip_forward_options: end of SR\n"));
9001 9002                                  break;
9002 9003                          }
9003 9004                          /* Pick a reasonable address on the outbound if */
9004 9005                          ASSERT(dst_ill != NULL);
9005 9006                          if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9006 9007                              INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9007 9008                              NULL) != 0) {
9008 9009                                  /* No source! Shouldn't happen */
9009 9010                                  ifaddr = INADDR_ANY;
9010 9011                          }
9011 9012                          bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9012 9013                          bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9013 9014                          ip1dbg(("ip_forward_options: next hop 0x%x\n",
9014 9015                              ntohl(dst)));
9015 9016  
9016 9017                          /*
9017 9018                           * Check if our address is present more than
9018 9019                           * once as consecutive hops in source route.
9019 9020                           */
9020 9021                          if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9021 9022                                  off += IP_ADDR_LEN;
9022 9023                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9023 9024                                  goto redo_srr;
9024 9025                          }
9025 9026                          ipha->ipha_dst = dst;
9026 9027                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9027 9028                          break;
9028 9029                  case IPOPT_RR:
9029 9030                          off = opt[IPOPT_OFFSET];
9030 9031                          off--;
9031 9032                          if (optlen < IP_ADDR_LEN ||
9032 9033                              off > optlen - IP_ADDR_LEN) {
9033 9034                                  /* No more room - ignore */
9034 9035                                  ip1dbg((
9035 9036                                      "ip_forward_options: end of RR\n"));
9036 9037                                  break;
9037 9038                          }
9038 9039                          /* Pick a reasonable address on the outbound if */
9039 9040                          ASSERT(dst_ill != NULL);
9040 9041                          if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9041 9042                              INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9042 9043                              NULL) != 0) {
9043 9044                                  /* No source! Shouldn't happen */
9044 9045                                  ifaddr = INADDR_ANY;
9045 9046                          }
9046 9047                          bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9047 9048                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9048 9049                          break;
9049 9050                  case IPOPT_TS:
9050 9051                          /* Insert timestamp if there is room */
9051 9052                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9052 9053                          case IPOPT_TS_TSONLY:
9053 9054                                  off = IPOPT_TS_TIMELEN;
9054 9055                                  break;
9055 9056                          case IPOPT_TS_PRESPEC:
9056 9057                          case IPOPT_TS_PRESPEC_RFC791:
9057 9058                                  /* Verify that the address matched */
9058 9059                                  off = opt[IPOPT_OFFSET] - 1;
9059 9060                                  bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9060 9061                                  if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9061 9062                                          /* Not for us */
9062 9063                                          break;
9063 9064                                  }
9064 9065                                  /* FALLTHRU */
9065 9066                          case IPOPT_TS_TSANDADDR:
9066 9067                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9067 9068                                  break;
9068 9069                          default:
9069 9070                                  /*
9070 9071                                   * ip_*put_options should have already
9071 9072                                   * dropped this packet.
9072 9073                                   */
9073 9074                                  cmn_err(CE_PANIC, "ip_forward_options: "
9074 9075                                      "unknown IT - bug in ip_input_options?\n");
9075 9076                                  return (B_TRUE);        /* Keep "lint" happy */
9076 9077                          }
9077 9078                          if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9078 9079                                  /* Increase overflow counter */
9079 9080                                  off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9080 9081                                  opt[IPOPT_POS_OV_FLG] =
9081 9082                                      (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9082 9083                                      (off << 4));
9083 9084                                  break;
9084 9085                          }
9085 9086                          off = opt[IPOPT_OFFSET] - 1;
9086 9087                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9087 9088                          case IPOPT_TS_PRESPEC:
9088 9089                          case IPOPT_TS_PRESPEC_RFC791:
9089 9090                          case IPOPT_TS_TSANDADDR:
9090 9091                                  /* Pick a reasonable addr on the outbound if */
9091 9092                                  ASSERT(dst_ill != NULL);
9092 9093                                  if (ip_select_source_v4(dst_ill, INADDR_ANY,
9093 9094                                      dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
9094 9095                                      NULL, NULL) != 0) {
9095 9096                                          /* No source! Shouldn't happen */
9096 9097                                          ifaddr = INADDR_ANY;
9097 9098                                  }
9098 9099                                  bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9099 9100                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9100 9101                                  /* FALLTHRU */
9101 9102                          case IPOPT_TS_TSONLY:
9102 9103                                  off = opt[IPOPT_OFFSET] - 1;
9103 9104                                  /* Compute # of milliseconds since midnight */
9104 9105                                  gethrestime(&now);
9105 9106                                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9106 9107                                      now.tv_nsec / (NANOSEC / MILLISEC);
9107 9108                                  bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9108 9109                                  opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9109 9110                                  break;
9110 9111                          }
9111 9112                          break;
9112 9113                  }
9113 9114          }
9114 9115          return (B_TRUE);
9115 9116  }
9116 9117  
9117 9118  /*
9118 9119   * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
9119 9120   * returns 'true' if there are still fragments left on the queue, in
9120 9121   * which case we restart the timer.
9121 9122   */
9122 9123  void
9123 9124  ill_frag_timer(void *arg)
9124 9125  {
9125 9126          ill_t   *ill = (ill_t *)arg;
9126 9127          boolean_t frag_pending;
9127 9128          ip_stack_t *ipst = ill->ill_ipst;
9128 9129          time_t  timeout;
9129 9130  
9130 9131          mutex_enter(&ill->ill_lock);
9131 9132          ASSERT(!ill->ill_fragtimer_executing);
9132 9133          if (ill->ill_state_flags & ILL_CONDEMNED) {
9133 9134                  ill->ill_frag_timer_id = 0;
9134 9135                  mutex_exit(&ill->ill_lock);
9135 9136                  return;
9136 9137          }
9137 9138          ill->ill_fragtimer_executing = 1;
9138 9139          mutex_exit(&ill->ill_lock);
9139 9140  
9140 9141          timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9141 9142              ipst->ips_ip_reassembly_timeout);
9142 9143  
9143 9144          frag_pending = ill_frag_timeout(ill, timeout);
9144 9145  
9145 9146          /*
9146 9147           * Restart the timer, if we have fragments pending or if someone
9147 9148           * wanted us to be scheduled again.
9148 9149           */
9149 9150          mutex_enter(&ill->ill_lock);
9150 9151          ill->ill_fragtimer_executing = 0;
9151 9152          ill->ill_frag_timer_id = 0;
9152 9153          if (frag_pending || ill->ill_fragtimer_needrestart)
9153 9154                  ill_frag_timer_start(ill);
9154 9155          mutex_exit(&ill->ill_lock);
9155 9156  }
9156 9157  
9157 9158  void
9158 9159  ill_frag_timer_start(ill_t *ill)
9159 9160  {
9160 9161          ip_stack_t *ipst = ill->ill_ipst;
9161 9162          clock_t timeo_ms;
9162 9163  
9163 9164          ASSERT(MUTEX_HELD(&ill->ill_lock));
9164 9165  
9165 9166          /* If the ill is closing or opening don't proceed */
9166 9167          if (ill->ill_state_flags & ILL_CONDEMNED)
9167 9168                  return;
9168 9169  
9169 9170          if (ill->ill_fragtimer_executing) {
9170 9171                  /*
9171 9172                   * ill_frag_timer is currently executing. Just record the
9172 9173                   * the fact that we want the timer to be restarted.
9173 9174                   * ill_frag_timer will post a timeout before it returns,
9174 9175                   * ensuring it will be called again.
9175 9176                   */
9176 9177                  ill->ill_fragtimer_needrestart = 1;
9177 9178                  return;
9178 9179          }
9179 9180  
9180 9181          if (ill->ill_frag_timer_id == 0) {
9181 9182                  timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9182 9183                      ipst->ips_ip_reassembly_timeout) * SECONDS;
9183 9184  
9184 9185                  /*
9185 9186                   * The timer is neither running nor is the timeout handler
9186 9187                   * executing. Post a timeout so that ill_frag_timer will be
9187 9188                   * called
9188 9189                   */
9189 9190                  ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
9190 9191                      MSEC_TO_TICK(timeo_ms >> 1));
9191 9192                  ill->ill_fragtimer_needrestart = 0;
9192 9193          }
9193 9194  }
9194 9195  
9195 9196  /*
9196 9197   * Update any source route, record route or timestamp options.
9197 9198   * Check that we are at end of strict source route.
9198 9199   * The options have already been checked for sanity in ip_input_options().
9199 9200   */
9200 9201  boolean_t
9201 9202  ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
9202 9203  {
9203 9204          ipoptp_t        opts;
9204 9205          uchar_t         *opt;
9205 9206          uint8_t         optval;
9206 9207          uint8_t         optlen;
9207 9208          ipaddr_t        dst;
9208 9209          ipaddr_t        ifaddr;
9209 9210          uint32_t        ts;
9210 9211          timestruc_t     now;
9211 9212          ill_t           *ill = ira->ira_ill;
9212 9213          ip_stack_t      *ipst = ill->ill_ipst;
9213 9214  
9214 9215          ip2dbg(("ip_input_local_options\n"));
9215 9216  
9216 9217          for (optval = ipoptp_first(&opts, ipha);
9217 9218              optval != IPOPT_EOL;
9218 9219              optval = ipoptp_next(&opts)) {
9219 9220                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9220 9221                  opt = opts.ipoptp_cur;
9221 9222                  optlen = opts.ipoptp_len;
9222 9223                  ip2dbg(("ip_input_local_options: opt %d, len %d\n",
9223 9224                      optval, optlen));
9224 9225                  switch (optval) {
9225 9226                          uint32_t off;
9226 9227                  case IPOPT_SSRR:
9227 9228                  case IPOPT_LSRR:
9228 9229                          off = opt[IPOPT_OFFSET];
9229 9230                          off--;
9230 9231                          if (optlen < IP_ADDR_LEN ||
9231 9232                              off > optlen - IP_ADDR_LEN) {
9232 9233                                  /* End of source route */
9233 9234                                  ip1dbg(("ip_input_local_options: end of SR\n"));
9234 9235                                  break;
9235 9236                          }
9236 9237                          /*
9237 9238                           * This will only happen if two consecutive entries
9238 9239                           * in the source route contains our address or if
9239 9240                           * it is a packet with a loose source route which
9240 9241                           * reaches us before consuming the whole source route
9241 9242                           */
9242 9243                          ip1dbg(("ip_input_local_options: not end of SR\n"));
9243 9244                          if (optval == IPOPT_SSRR) {
9244 9245                                  goto bad_src_route;
9245 9246                          }
9246 9247                          /*
9247 9248                           * Hack: instead of dropping the packet truncate the
9248 9249                           * source route to what has been used by filling the
9249 9250                           * rest with IPOPT_NOP.
9250 9251                           */
9251 9252                          opt[IPOPT_OLEN] = (uint8_t)off;
9252 9253                          while (off < optlen) {
9253 9254                                  opt[off++] = IPOPT_NOP;
9254 9255                          }
9255 9256                          break;
9256 9257                  case IPOPT_RR:
9257 9258                          off = opt[IPOPT_OFFSET];
9258 9259                          off--;
9259 9260                          if (optlen < IP_ADDR_LEN ||
9260 9261                              off > optlen - IP_ADDR_LEN) {
9261 9262                                  /* No more room - ignore */
9262 9263                                  ip1dbg((
9263 9264                                      "ip_input_local_options: end of RR\n"));
9264 9265                                  break;
9265 9266                          }
9266 9267                          /* Pick a reasonable address on the outbound if */
9267 9268                          if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9268 9269                              INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9269 9270                              NULL) != 0) {
9270 9271                                  /* No source! Shouldn't happen */
9271 9272                                  ifaddr = INADDR_ANY;
9272 9273                          }
9273 9274                          bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9274 9275                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9275 9276                          break;
9276 9277                  case IPOPT_TS:
9277 9278                          /* Insert timestamp if there is romm */
9278 9279                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9279 9280                          case IPOPT_TS_TSONLY:
9280 9281                                  off = IPOPT_TS_TIMELEN;
9281 9282                                  break;
9282 9283                          case IPOPT_TS_PRESPEC:
9283 9284                          case IPOPT_TS_PRESPEC_RFC791:
9284 9285                                  /* Verify that the address matched */
9285 9286                                  off = opt[IPOPT_OFFSET] - 1;
9286 9287                                  bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9287 9288                                  if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9288 9289                                          /* Not for us */
9289 9290                                          break;
9290 9291                                  }
9291 9292                                  /* FALLTHRU */
9292 9293                          case IPOPT_TS_TSANDADDR:
9293 9294                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9294 9295                                  break;
9295 9296                          default:
9296 9297                                  /*
9297 9298                                   * ip_*put_options should have already
9298 9299                                   * dropped this packet.
9299 9300                                   */
9300 9301                                  cmn_err(CE_PANIC, "ip_input_local_options: "
9301 9302                                      "unknown IT - bug in ip_input_options?\n");
9302 9303                                  return (B_TRUE);        /* Keep "lint" happy */
9303 9304                          }
9304 9305                          if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9305 9306                                  /* Increase overflow counter */
9306 9307                                  off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9307 9308                                  opt[IPOPT_POS_OV_FLG] =
9308 9309                                      (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9309 9310                                      (off << 4));
9310 9311                                  break;
9311 9312                          }
9312 9313                          off = opt[IPOPT_OFFSET] - 1;
9313 9314                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9314 9315                          case IPOPT_TS_PRESPEC:
9315 9316                          case IPOPT_TS_PRESPEC_RFC791:
9316 9317                          case IPOPT_TS_TSANDADDR:
9317 9318                                  /* Pick a reasonable addr on the outbound if */
9318 9319                                  if (ip_select_source_v4(ill, INADDR_ANY,
9319 9320                                      ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9320 9321                                      &ifaddr, NULL, NULL) != 0) {
9321 9322                                          /* No source! Shouldn't happen */
9322 9323                                          ifaddr = INADDR_ANY;
9323 9324                                  }
9324 9325                                  bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9325 9326                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9326 9327                                  /* FALLTHRU */
9327 9328                          case IPOPT_TS_TSONLY:
9328 9329                                  off = opt[IPOPT_OFFSET] - 1;
9329 9330                                  /* Compute # of milliseconds since midnight */
9330 9331                                  gethrestime(&now);
9331 9332                                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9332 9333                                      now.tv_nsec / (NANOSEC / MILLISEC);
9333 9334                                  bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9334 9335                                  opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9335 9336                                  break;
9336 9337                          }
9337 9338                          break;
9338 9339                  }
9339 9340          }
9340 9341          return (B_TRUE);
9341 9342  
9342 9343  bad_src_route:
9343 9344          /* make sure we clear any indication of a hardware checksum */
9344 9345          DB_CKSUMFLAGS(mp) = 0;
9345 9346          ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9346 9347          icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9347 9348          return (B_FALSE);
9348 9349  
9349 9350  }
9350 9351  
9351 9352  /*
9352 9353   * Process IP options in an inbound packet.  Always returns the nexthop.
9353 9354   * Normally this is the passed in nexthop, but if there is an option
9354 9355   * that effects the nexthop (such as a source route) that will be returned.
9355 9356   * Sets *errorp if there is an error, in which case an ICMP error has been sent
9356 9357   * and mp freed.
9357 9358   */
9358 9359  ipaddr_t
9359 9360  ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9360 9361      ip_recv_attr_t *ira, int *errorp)
9361 9362  {
9362 9363          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
9363 9364          ipoptp_t        opts;
9364 9365          uchar_t         *opt;
9365 9366          uint8_t         optval;
9366 9367          uint8_t         optlen;
9367 9368          intptr_t        code = 0;
9368 9369          ire_t           *ire;
9369 9370  
9370 9371          ip2dbg(("ip_input_options\n"));
9371 9372          *errorp = 0;
9372 9373          for (optval = ipoptp_first(&opts, ipha);
9373 9374              optval != IPOPT_EOL;
9374 9375              optval = ipoptp_next(&opts)) {
9375 9376                  opt = opts.ipoptp_cur;
9376 9377                  optlen = opts.ipoptp_len;
9377 9378                  ip2dbg(("ip_input_options: opt %d, len %d\n",
9378 9379                      optval, optlen));
9379 9380                  /*
9380 9381                   * Note: we need to verify the checksum before we
9381 9382                   * modify anything thus this routine only extracts the next
9382 9383                   * hop dst from any source route.
9383 9384                   */
9384 9385                  switch (optval) {
9385 9386                          uint32_t off;
9386 9387                  case IPOPT_SSRR:
9387 9388                  case IPOPT_LSRR:
9388 9389                          if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9389 9390                                  if (optval == IPOPT_SSRR) {
9390 9391                                          ip1dbg(("ip_input_options: not next"
9391 9392                                              " strict source route 0x%x\n",
9392 9393                                              ntohl(dst)));
9393 9394                                          code = (char *)&ipha->ipha_dst -
9394 9395                                              (char *)ipha;
9395 9396                                          goto param_prob; /* RouterReq's */
9396 9397                                  }
9397 9398                                  ip2dbg(("ip_input_options: "
9398 9399                                      "not next source route 0x%x\n",
9399 9400                                      ntohl(dst)));
9400 9401                                  break;
9401 9402                          }
9402 9403  
9403 9404                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9404 9405                                  ip1dbg((
9405 9406                                      "ip_input_options: bad option offset\n"));
9406 9407                                  code = (char *)&opt[IPOPT_OLEN] -
9407 9408                                      (char *)ipha;
9408 9409                                  goto param_prob;
9409 9410                          }
9410 9411                          off = opt[IPOPT_OFFSET];
9411 9412                          off--;
9412 9413                  redo_srr:
9413 9414                          if (optlen < IP_ADDR_LEN ||
9414 9415                              off > optlen - IP_ADDR_LEN) {
9415 9416                                  /* End of source route */
9416 9417                                  ip1dbg(("ip_input_options: end of SR\n"));
9417 9418                                  break;
9418 9419                          }
9419 9420                          bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9420 9421                          ip1dbg(("ip_input_options: next hop 0x%x\n",
9421 9422                              ntohl(dst)));
9422 9423  
9423 9424                          /*
9424 9425                           * Check if our address is present more than
9425 9426                           * once as consecutive hops in source route.
9426 9427                           * XXX verify per-interface ip_forwarding
9427 9428                           * for source route?
9428 9429                           */
9429 9430                          if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9430 9431                                  off += IP_ADDR_LEN;
9431 9432                                  goto redo_srr;
9432 9433                          }
9433 9434  
9434 9435                          if (dst == htonl(INADDR_LOOPBACK)) {
9435 9436                                  ip1dbg(("ip_input_options: loopback addr in "
9436 9437                                      "source route!\n"));
9437 9438                                  goto bad_src_route;
9438 9439                          }
9439 9440                          /*
9440 9441                           * For strict: verify that dst is directly
9441 9442                           * reachable.
9442 9443                           */
9443 9444                          if (optval == IPOPT_SSRR) {
9444 9445                                  ire = ire_ftable_lookup_v4(dst, 0, 0,
9445 9446                                      IRE_INTERFACE, NULL, ALL_ZONES,
9446 9447                                      ira->ira_tsl,
9447 9448                                      MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
9448 9449                                      NULL);
9449 9450                                  if (ire == NULL) {
9450 9451                                          ip1dbg(("ip_input_options: SSRR not "
9451 9452                                              "directly reachable: 0x%x\n",
9452 9453                                              ntohl(dst)));
9453 9454                                          goto bad_src_route;
9454 9455                                  }
9455 9456                                  ire_refrele(ire);
9456 9457                          }
9457 9458                          /*
9458 9459                           * Defer update of the offset and the record route
9459 9460                           * until the packet is forwarded.
9460 9461                           */
9461 9462                          break;
9462 9463                  case IPOPT_RR:
9463 9464                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9464 9465                                  ip1dbg((
9465 9466                                      "ip_input_options: bad option offset\n"));
9466 9467                                  code = (char *)&opt[IPOPT_OLEN] -
9467 9468                                      (char *)ipha;
9468 9469                                  goto param_prob;
9469 9470                          }
9470 9471                          break;
9471 9472                  case IPOPT_TS:
9472 9473                          /*
9473 9474                           * Verify that length >= 5 and that there is either
9474 9475                           * room for another timestamp or that the overflow
9475 9476                           * counter is not maxed out.
9476 9477                           */
9477 9478                          code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9478 9479                          if (optlen < IPOPT_MINLEN_IT) {
9479 9480                                  goto param_prob;
9480 9481                          }
9481 9482                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9482 9483                                  ip1dbg((
9483 9484                                      "ip_input_options: bad option offset\n"));
9484 9485                                  code = (char *)&opt[IPOPT_OFFSET] -
9485 9486                                      (char *)ipha;
9486 9487                                  goto param_prob;
9487 9488                          }
9488 9489                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9489 9490                          case IPOPT_TS_TSONLY:
9490 9491                                  off = IPOPT_TS_TIMELEN;
9491 9492                                  break;
9492 9493                          case IPOPT_TS_TSANDADDR:
9493 9494                          case IPOPT_TS_PRESPEC:
9494 9495                          case IPOPT_TS_PRESPEC_RFC791:
9495 9496                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9496 9497                                  break;
9497 9498                          default:
9498 9499                                  code = (char *)&opt[IPOPT_POS_OV_FLG] -
9499 9500                                      (char *)ipha;
9500 9501                                  goto param_prob;
9501 9502                          }
9502 9503                          if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9503 9504                              (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9504 9505                                  /*
9505 9506                                   * No room and the overflow counter is 15
9506 9507                                   * already.
9507 9508                                   */
9508 9509                                  goto param_prob;
9509 9510                          }
9510 9511                          break;
9511 9512                  }
9512 9513          }
9513 9514  
9514 9515          if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9515 9516                  return (dst);
9516 9517          }
9517 9518  
9518 9519          ip1dbg(("ip_input_options: error processing IP options."));
9519 9520          code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9520 9521  
9521 9522  param_prob:
9522 9523          /* make sure we clear any indication of a hardware checksum */
9523 9524          DB_CKSUMFLAGS(mp) = 0;
9524 9525          ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9525 9526          icmp_param_problem(mp, (uint8_t)code, ira);
9526 9527          *errorp = -1;
9527 9528          return (dst);
9528 9529  
9529 9530  bad_src_route:
9530 9531          /* make sure we clear any indication of a hardware checksum */
9531 9532          DB_CKSUMFLAGS(mp) = 0;
9532 9533          ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9533 9534          icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9534 9535          *errorp = -1;
9535 9536          return (dst);
9536 9537  }
9537 9538  
9538 9539  /*
9539 9540   * IP & ICMP info in >=14 msg's ...
9540 9541   *  - ip fixed part (mib2_ip_t)
9541 9542   *  - icmp fixed part (mib2_icmp_t)
9542 9543   *  - ipAddrEntryTable (ip 20)          all IPv4 ipifs
9543 9544   *  - ipRouteEntryTable (ip 21)         all IPv4 IREs
9544 9545   *  - ipNetToMediaEntryTable (ip 22)    all IPv4 Neighbor Cache entries
9545 9546   *  - ipRouteAttributeTable (ip 102)    labeled routes
9546 9547   *  - ip multicast membership (ip_member_t)
9547 9548   *  - ip multicast source filtering (ip_grpsrc_t)
9548 9549   *  - igmp fixed part (struct igmpstat)
9549 9550   *  - multicast routing stats (struct mrtstat)
9550 9551   *  - multicast routing vifs (array of struct vifctl)
9551 9552   *  - multicast routing routes (array of struct mfcctl)
9552 9553   *  - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9553 9554   *                                      One per ill plus one generic
9554 9555   *  - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9555 9556   *                                      One per ill plus one generic
9556 9557   *  - ipv6RouteEntry                    all IPv6 IREs
9557 9558   *  - ipv6RouteAttributeTable (ip6 102) labeled routes
9558 9559   *  - ipv6NetToMediaEntry               all IPv6 Neighbor Cache entries
9559 9560   *  - ipv6AddrEntry                     all IPv6 ipifs
9560 9561   *  - ipv6 multicast membership (ipv6_member_t)
9561 9562   *  - ipv6 multicast source filtering (ipv6_grpsrc_t)
9562 9563   *
9563 9564   * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9564 9565   * already filled in by the caller.
9565 9566   * If legacy_req is true then MIB structures needs to be truncated to their
9566 9567   * legacy sizes before being returned.
9567 9568   * Return value of 0 indicates that no messages were sent and caller
9568 9569   * should free mpctl.
9569 9570   */
9570 9571  int
9571 9572  ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9572 9573  {
9573 9574          ip_stack_t *ipst;
9574 9575          sctp_stack_t *sctps;
9575 9576  
9576 9577          if (q->q_next != NULL) {
9577 9578                  ipst = ILLQ_TO_IPST(q);
9578 9579          } else {
9579 9580                  ipst = CONNQ_TO_IPST(q);
9580 9581          }
9581 9582          ASSERT(ipst != NULL);
9582 9583          sctps = ipst->ips_netstack->netstack_sctp;
9583 9584  
9584 9585          if (mpctl == NULL || mpctl->b_cont == NULL) {
9585 9586                  return (0);
9586 9587          }
9587 9588  
9588 9589          /*
9589 9590           * For the purposes of the (broken) packet shell use
9590 9591           * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9591 9592           * to make TCP and UDP appear first in the list of mib items.
9592 9593           * TBD: We could expand this and use it in netstat so that
9593 9594           * the kernel doesn't have to produce large tables (connections,
9594 9595           * routes, etc) when netstat only wants the statistics or a particular
9595 9596           * table.
9596 9597           */
9597 9598          if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9598 9599                  if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9599 9600                          return (1);
9600 9601                  }
9601 9602          }
9602 9603  
9603 9604          if (level != MIB2_TCP) {
9604 9605                  if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9605 9606                          return (1);
9606 9607                  }
9607 9608          }
9608 9609  
9609 9610          if (level != MIB2_UDP) {
9610 9611                  if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9611 9612                          return (1);
9612 9613                  }
9613 9614          }
9614 9615  
9615 9616          if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9616 9617              ipst, legacy_req)) == NULL) {
9617 9618                  return (1);
9618 9619          }
9619 9620  
9620 9621          if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9621 9622              legacy_req)) == NULL) {
9622 9623                  return (1);
9623 9624          }
9624 9625  
9625 9626          if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9626 9627                  return (1);
9627 9628          }
9628 9629  
9629 9630          if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9630 9631                  return (1);
9631 9632          }
9632 9633  
9633 9634          if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9634 9635                  return (1);
9635 9636          }
9636 9637  
9637 9638          if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9638 9639                  return (1);
9639 9640          }
9640 9641  
9641 9642          if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9642 9643              legacy_req)) == NULL) {
9643 9644                  return (1);
9644 9645          }
9645 9646  
9646 9647          if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9647 9648              legacy_req)) == NULL) {
9648 9649                  return (1);
9649 9650          }
9650 9651  
9651 9652          if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9652 9653                  return (1);
9653 9654          }
9654 9655  
9655 9656          if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9656 9657                  return (1);
9657 9658          }
9658 9659  
9659 9660          if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9660 9661                  return (1);
9661 9662          }
9662 9663  
9663 9664          if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9664 9665                  return (1);
9665 9666          }
9666 9667  
9667 9668          if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9668 9669                  return (1);
9669 9670          }
9670 9671  
9671 9672          if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9672 9673                  return (1);
9673 9674          }
9674 9675  
9675 9676          mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9676 9677          if (mpctl == NULL)
9677 9678                  return (1);
9678 9679  
9679 9680          mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9680 9681          if (mpctl == NULL)
9681 9682                  return (1);
9682 9683  
9683 9684          if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9684 9685                  return (1);
9685 9686          }
9686 9687          if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9687 9688                  return (1);
9688 9689          }
9689 9690          freemsg(mpctl);
9690 9691          return (1);
9691 9692  }
9692 9693  
9693 9694  /* Get global (legacy) IPv4 statistics */
9694 9695  static mblk_t *
9695 9696  ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9696 9697      ip_stack_t *ipst, boolean_t legacy_req)
9697 9698  {
9698 9699          mib2_ip_t               old_ip_mib;
9699 9700          struct opthdr           *optp;
9700 9701          mblk_t                  *mp2ctl;
9701 9702          mib2_ipAddrEntry_t      mae;
9702 9703  
9703 9704          /*
9704 9705           * make a copy of the original message
9705 9706           */
9706 9707          mp2ctl = copymsg(mpctl);
9707 9708  
9708 9709          /* fixed length IP structure... */
9709 9710          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9710 9711          optp->level = MIB2_IP;
9711 9712          optp->name = 0;
9712 9713          SET_MIB(old_ip_mib.ipForwarding,
9713 9714              (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9714 9715          SET_MIB(old_ip_mib.ipDefaultTTL,
9715 9716              (uint32_t)ipst->ips_ip_def_ttl);
9716 9717          SET_MIB(old_ip_mib.ipReasmTimeout,
9717 9718              ipst->ips_ip_reassembly_timeout);
9718 9719          SET_MIB(old_ip_mib.ipAddrEntrySize,
9719 9720              (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9720 9721              sizeof (mib2_ipAddrEntry_t));
9721 9722          SET_MIB(old_ip_mib.ipRouteEntrySize,
9722 9723              sizeof (mib2_ipRouteEntry_t));
9723 9724          SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9724 9725              sizeof (mib2_ipNetToMediaEntry_t));
9725 9726          SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9726 9727          SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9727 9728          SET_MIB(old_ip_mib.ipRouteAttributeSize,
9728 9729              sizeof (mib2_ipAttributeEntry_t));
9729 9730          SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t));
9730 9731          SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9731 9732  
9732 9733          /*
9733 9734           * Grab the statistics from the new IP MIB
9734 9735           */
9735 9736          SET_MIB(old_ip_mib.ipInReceives,
9736 9737              (uint32_t)ipmib->ipIfStatsHCInReceives);
9737 9738          SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9738 9739          SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9739 9740          SET_MIB(old_ip_mib.ipForwDatagrams,
9740 9741              (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9741 9742          SET_MIB(old_ip_mib.ipInUnknownProtos,
9742 9743              ipmib->ipIfStatsInUnknownProtos);
9743 9744          SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9744 9745          SET_MIB(old_ip_mib.ipInDelivers,
9745 9746              (uint32_t)ipmib->ipIfStatsHCInDelivers);
9746 9747          SET_MIB(old_ip_mib.ipOutRequests,
9747 9748              (uint32_t)ipmib->ipIfStatsHCOutRequests);
9748 9749          SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9749 9750          SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9750 9751          SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9751 9752          SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9752 9753          SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9753 9754          SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9754 9755          SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9755 9756          SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9756 9757  
9757 9758          /* ipRoutingDiscards is not being used */
9758 9759          SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9759 9760          SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9760 9761          SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9761 9762          SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9762 9763          SET_MIB(old_ip_mib.ipReasmDuplicates,
9763 9764              ipmib->ipIfStatsReasmDuplicates);
9764 9765          SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9765 9766          SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9766 9767          SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9767 9768          SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9768 9769          SET_MIB(old_ip_mib.rawipInOverflows,
9769 9770              ipmib->rawipIfStatsInOverflows);
9770 9771  
9771 9772          SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9772 9773          SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9773 9774          SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9774 9775          SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9775 9776          SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9776 9777              ipmib->ipIfStatsOutSwitchIPVersion);
9777 9778  
9778 9779          if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9779 9780              (int)sizeof (old_ip_mib))) {
9780 9781                  ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9781 9782                      (uint_t)sizeof (old_ip_mib)));
9782 9783          }
9783 9784  
9784 9785          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9785 9786          ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9786 9787              (int)optp->level, (int)optp->name, (int)optp->len));
9787 9788          qreply(q, mpctl);
9788 9789          return (mp2ctl);
9789 9790  }
9790 9791  
9791 9792  /* Per interface IPv4 statistics */
9792 9793  static mblk_t *
9793 9794  ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9794 9795      boolean_t legacy_req)
9795 9796  {
9796 9797          struct opthdr           *optp;
9797 9798          mblk_t                  *mp2ctl;
9798 9799          ill_t                   *ill;
9799 9800          ill_walk_context_t      ctx;
9800 9801          mblk_t                  *mp_tail = NULL;
9801 9802          mib2_ipIfStatsEntry_t   global_ip_mib;
9802 9803          mib2_ipAddrEntry_t      mae;
9803 9804  
9804 9805          /*
9805 9806           * Make a copy of the original message
9806 9807           */
9807 9808          mp2ctl = copymsg(mpctl);
9808 9809  
9809 9810          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9810 9811          optp->level = MIB2_IP;
9811 9812          optp->name = MIB2_IP_TRAFFIC_STATS;
9812 9813          /* Include "unknown interface" ip_mib */
9813 9814          ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9814 9815          ipst->ips_ip_mib.ipIfStatsIfIndex =
9815 9816              MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9816 9817          SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9817 9818              (ipst->ips_ip_forwarding ? 1 : 2));
9818 9819          SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9819 9820              (uint32_t)ipst->ips_ip_def_ttl);
9820 9821          SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9821 9822              sizeof (mib2_ipIfStatsEntry_t));
9822 9823          SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9823 9824              sizeof (mib2_ipAddrEntry_t));
9824 9825          SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9825 9826              sizeof (mib2_ipRouteEntry_t));
9826 9827          SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9827 9828              sizeof (mib2_ipNetToMediaEntry_t));
9828 9829          SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9829 9830              sizeof (ip_member_t));
9830 9831          SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9831 9832              sizeof (ip_grpsrc_t));
9832 9833  
9833 9834          bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9834 9835  
9835 9836          if (legacy_req) {
9836 9837                  SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9837 9838                      LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9838 9839          }
9839 9840  
9840 9841          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9841 9842              (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9842 9843                  ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9843 9844                      "failed to allocate %u bytes\n",
9844 9845                      (uint_t)sizeof (global_ip_mib)));
9845 9846          }
9846 9847  
9847 9848          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9848 9849          ill = ILL_START_WALK_V4(&ctx, ipst);
9849 9850          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9850 9851                  ill->ill_ip_mib->ipIfStatsIfIndex =
9851 9852                      ill->ill_phyint->phyint_ifindex;
9852 9853                  SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9853 9854                      (ipst->ips_ip_forwarding ? 1 : 2));
9854 9855                  SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9855 9856                      (uint32_t)ipst->ips_ip_def_ttl);
9856 9857  
9857 9858                  ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9858 9859                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9859 9860                      (char *)ill->ill_ip_mib,
9860 9861                      (int)sizeof (*ill->ill_ip_mib))) {
9861 9862                          ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9862 9863                              "failed to allocate %u bytes\n",
9863 9864                              (uint_t)sizeof (*ill->ill_ip_mib)));
9864 9865                  }
9865 9866          }
9866 9867          rw_exit(&ipst->ips_ill_g_lock);
9867 9868  
9868 9869          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9869 9870          ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9870 9871              "level %d, name %d, len %d\n",
9871 9872              (int)optp->level, (int)optp->name, (int)optp->len));
9872 9873          qreply(q, mpctl);
9873 9874  
9874 9875          if (mp2ctl == NULL)
9875 9876                  return (NULL);
9876 9877  
9877 9878          return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9878 9879              legacy_req));
9879 9880  }
9880 9881  
9881 9882  /* Global IPv4 ICMP statistics */
9882 9883  static mblk_t *
9883 9884  ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9884 9885  {
9885 9886          struct opthdr           *optp;
9886 9887          mblk_t                  *mp2ctl;
9887 9888  
9888 9889          /*
9889 9890           * Make a copy of the original message
9890 9891           */
9891 9892          mp2ctl = copymsg(mpctl);
9892 9893  
9893 9894          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9894 9895          optp->level = MIB2_ICMP;
9895 9896          optp->name = 0;
9896 9897          if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9897 9898              (int)sizeof (ipst->ips_icmp_mib))) {
9898 9899                  ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9899 9900                      (uint_t)sizeof (ipst->ips_icmp_mib)));
9900 9901          }
9901 9902          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9902 9903          ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9903 9904              (int)optp->level, (int)optp->name, (int)optp->len));
9904 9905          qreply(q, mpctl);
9905 9906          return (mp2ctl);
9906 9907  }
9907 9908  
9908 9909  /* Global IPv4 IGMP statistics */
9909 9910  static mblk_t *
9910 9911  ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9911 9912  {
9912 9913          struct opthdr           *optp;
9913 9914          mblk_t                  *mp2ctl;
9914 9915  
9915 9916          /*
9916 9917           * make a copy of the original message
9917 9918           */
9918 9919          mp2ctl = copymsg(mpctl);
9919 9920  
9920 9921          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9921 9922          optp->level = EXPER_IGMP;
9922 9923          optp->name = 0;
9923 9924          if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9924 9925              (int)sizeof (ipst->ips_igmpstat))) {
9925 9926                  ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9926 9927                      (uint_t)sizeof (ipst->ips_igmpstat)));
9927 9928          }
9928 9929          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9929 9930          ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9930 9931              (int)optp->level, (int)optp->name, (int)optp->len));
9931 9932          qreply(q, mpctl);
9932 9933          return (mp2ctl);
9933 9934  }
9934 9935  
9935 9936  /* Global IPv4 Multicast Routing statistics */
9936 9937  static mblk_t *
9937 9938  ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9938 9939  {
9939 9940          struct opthdr           *optp;
9940 9941          mblk_t                  *mp2ctl;
9941 9942  
9942 9943          /*
9943 9944           * make a copy of the original message
9944 9945           */
9945 9946          mp2ctl = copymsg(mpctl);
9946 9947  
9947 9948          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9948 9949          optp->level = EXPER_DVMRP;
9949 9950          optp->name = 0;
9950 9951          if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9951 9952                  ip0dbg(("ip_mroute_stats: failed\n"));
9952 9953          }
9953 9954          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9954 9955          ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9955 9956              (int)optp->level, (int)optp->name, (int)optp->len));
9956 9957          qreply(q, mpctl);
9957 9958          return (mp2ctl);
9958 9959  }
9959 9960  
9960 9961  /* IPv4 address information */
9961 9962  static mblk_t *
9962 9963  ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9963 9964      boolean_t legacy_req)
9964 9965  {
9965 9966          struct opthdr           *optp;
9966 9967          mblk_t                  *mp2ctl;
9967 9968          mblk_t                  *mp_tail = NULL;
9968 9969          ill_t                   *ill;
9969 9970          ipif_t                  *ipif;
9970 9971          uint_t                  bitval;
9971 9972          mib2_ipAddrEntry_t      mae;
9972 9973          size_t                  mae_size;
9973 9974          zoneid_t                zoneid;
9974 9975          ill_walk_context_t      ctx;
9975 9976  
9976 9977          /*
9977 9978           * make a copy of the original message
9978 9979           */
9979 9980          mp2ctl = copymsg(mpctl);
9980 9981  
9981 9982          mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9982 9983              sizeof (mib2_ipAddrEntry_t);
9983 9984  
9984 9985          /* ipAddrEntryTable */
9985 9986  
9986 9987          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9987 9988          optp->level = MIB2_IP;
9988 9989          optp->name = MIB2_IP_ADDR;
9989 9990          zoneid = Q_TO_CONN(q)->conn_zoneid;
9990 9991  
9991 9992          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9992 9993          ill = ILL_START_WALK_V4(&ctx, ipst);
9993 9994          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9994 9995                  for (ipif = ill->ill_ipif; ipif != NULL;
9995 9996                      ipif = ipif->ipif_next) {
9996 9997                          if (ipif->ipif_zoneid != zoneid &&
9997 9998                              ipif->ipif_zoneid != ALL_ZONES)
9998 9999                                  continue;
9999 10000                          /* Sum of count from dead IRE_LO* and our current */
10000 10001                          mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10001 10002                          if (ipif->ipif_ire_local != NULL) {
10002 10003                                  mae.ipAdEntInfo.ae_ibcnt +=
10003 10004                                      ipif->ipif_ire_local->ire_ib_pkt_count;
10004 10005                          }
10005 10006                          mae.ipAdEntInfo.ae_obcnt = 0;
10006 10007                          mae.ipAdEntInfo.ae_focnt = 0;
10007 10008  
10008 10009                          ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
10009 10010                              OCTET_LENGTH);
10010 10011                          mae.ipAdEntIfIndex.o_length =
10011 10012                              mi_strlen(mae.ipAdEntIfIndex.o_bytes);
10012 10013                          mae.ipAdEntAddr = ipif->ipif_lcl_addr;
10013 10014                          mae.ipAdEntNetMask = ipif->ipif_net_mask;
10014 10015                          mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
10015 10016                          mae.ipAdEntInfo.ae_subnet_len =
10016 10017                              ip_mask_to_plen(ipif->ipif_net_mask);
10017 10018                          mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
10018 10019                          for (bitval = 1;
10019 10020                              bitval &&
10020 10021                              !(bitval & ipif->ipif_brd_addr);
10021 10022                              bitval <<= 1)
10022 10023                                  noop;
10023 10024                          mae.ipAdEntBcastAddr = bitval;
10024 10025                          mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
10025 10026                          mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10026 10027                          mae.ipAdEntInfo.ae_metric  = ipif->ipif_ill->ill_metric;
10027 10028                          mae.ipAdEntInfo.ae_broadcast_addr =
10028 10029                              ipif->ipif_brd_addr;
10029 10030                          mae.ipAdEntInfo.ae_pp_dst_addr =
10030 10031                              ipif->ipif_pp_dst_addr;
10031 10032                          mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
10032 10033                              ill->ill_flags | ill->ill_phyint->phyint_flags;
10033 10034                          mae.ipAdEntRetransmitTime =
10034 10035                              ill->ill_reachable_retrans_time;
10035 10036  
10036 10037                          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10037 10038                              (char *)&mae, (int)mae_size)) {
10038 10039                                  ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
10039 10040                                      "allocate %u bytes\n", (uint_t)mae_size));
10040 10041                          }
10041 10042                  }
10042 10043          }
10043 10044          rw_exit(&ipst->ips_ill_g_lock);
10044 10045  
10045 10046          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10046 10047          ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
10047 10048              (int)optp->level, (int)optp->name, (int)optp->len));
10048 10049          qreply(q, mpctl);
10049 10050          return (mp2ctl);
10050 10051  }
10051 10052  
10052 10053  /* IPv6 address information */
10053 10054  static mblk_t *
10054 10055  ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10055 10056      boolean_t legacy_req)
10056 10057  {
10057 10058          struct opthdr           *optp;
10058 10059          mblk_t                  *mp2ctl;
10059 10060          mblk_t                  *mp_tail = NULL;
10060 10061          ill_t                   *ill;
10061 10062          ipif_t                  *ipif;
10062 10063          mib2_ipv6AddrEntry_t    mae6;
10063 10064          size_t                  mae6_size;
10064 10065          zoneid_t                zoneid;
10065 10066          ill_walk_context_t      ctx;
10066 10067  
10067 10068          /*
10068 10069           * make a copy of the original message
10069 10070           */
10070 10071          mp2ctl = copymsg(mpctl);
10071 10072  
10072 10073          mae6_size = (legacy_req) ?
10073 10074              LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
10074 10075              sizeof (mib2_ipv6AddrEntry_t);
10075 10076  
10076 10077          /* ipv6AddrEntryTable */
10077 10078  
10078 10079          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10079 10080          optp->level = MIB2_IP6;
10080 10081          optp->name = MIB2_IP6_ADDR;
10081 10082          zoneid = Q_TO_CONN(q)->conn_zoneid;
10082 10083  
10083 10084          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10084 10085          ill = ILL_START_WALK_V6(&ctx, ipst);
10085 10086          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10086 10087                  for (ipif = ill->ill_ipif; ipif != NULL;
10087 10088                      ipif = ipif->ipif_next) {
10088 10089                          if (ipif->ipif_zoneid != zoneid &&
10089 10090                              ipif->ipif_zoneid != ALL_ZONES)
10090 10091                                  continue;
10091 10092                          /* Sum of count from dead IRE_LO* and our current */
10092 10093                          mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10093 10094                          if (ipif->ipif_ire_local != NULL) {
10094 10095                                  mae6.ipv6AddrInfo.ae_ibcnt +=
10095 10096                                      ipif->ipif_ire_local->ire_ib_pkt_count;
10096 10097                          }
10097 10098                          mae6.ipv6AddrInfo.ae_obcnt = 0;
10098 10099                          mae6.ipv6AddrInfo.ae_focnt = 0;
10099 10100  
10100 10101                          ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
10101 10102                              OCTET_LENGTH);
10102 10103                          mae6.ipv6AddrIfIndex.o_length =
10103 10104                              mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
10104 10105                          mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
10105 10106                          mae6.ipv6AddrPfxLength =
10106 10107                              ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10107 10108                          mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
10108 10109                          mae6.ipv6AddrInfo.ae_subnet_len =
10109 10110                              mae6.ipv6AddrPfxLength;
10110 10111                          mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
10111 10112  
10112 10113                          /* Type: stateless(1), stateful(2), unknown(3) */
10113 10114                          if (ipif->ipif_flags & IPIF_ADDRCONF)
10114 10115                                  mae6.ipv6AddrType = 1;
10115 10116                          else
10116 10117                                  mae6.ipv6AddrType = 2;
10117 10118                          /* Anycast: true(1), false(2) */
10118 10119                          if (ipif->ipif_flags & IPIF_ANYCAST)
10119 10120                                  mae6.ipv6AddrAnycastFlag = 1;
10120 10121                          else
10121 10122                                  mae6.ipv6AddrAnycastFlag = 2;
10122 10123  
10123 10124                          /*
10124 10125                           * Address status: preferred(1), deprecated(2),
10125 10126                           * invalid(3), inaccessible(4), unknown(5)
10126 10127                           */
10127 10128                          if (ipif->ipif_flags & IPIF_NOLOCAL)
10128 10129                                  mae6.ipv6AddrStatus = 3;
10129 10130                          else if (ipif->ipif_flags & IPIF_DEPRECATED)
10130 10131                                  mae6.ipv6AddrStatus = 2;
10131 10132                          else
10132 10133                                  mae6.ipv6AddrStatus = 1;
10133 10134                          mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10134 10135                          mae6.ipv6AddrInfo.ae_metric  =
10135 10136                              ipif->ipif_ill->ill_metric;
10136 10137                          mae6.ipv6AddrInfo.ae_pp_dst_addr =
10137 10138                              ipif->ipif_v6pp_dst_addr;
10138 10139                          mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
10139 10140                              ill->ill_flags | ill->ill_phyint->phyint_flags;
10140 10141                          mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
10141 10142                          mae6.ipv6AddrIdentifier = ill->ill_token;
10142 10143                          mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
10143 10144                          mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
10144 10145                          mae6.ipv6AddrRetransmitTime =
10145 10146                              ill->ill_reachable_retrans_time;
10146 10147                          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10147 10148                              (char *)&mae6, (int)mae6_size)) {
10148 10149                                  ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
10149 10150                                      "allocate %u bytes\n",
10150 10151                                      (uint_t)mae6_size));
10151 10152                          }
10152 10153                  }
10153 10154          }
10154 10155          rw_exit(&ipst->ips_ill_g_lock);
10155 10156  
10156 10157          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10157 10158          ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
10158 10159              (int)optp->level, (int)optp->name, (int)optp->len));
10159 10160          qreply(q, mpctl);
10160 10161          return (mp2ctl);
10161 10162  }
10162 10163  
10163 10164  /* IPv4 multicast group membership. */
10164 10165  static mblk_t *
10165 10166  ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10166 10167  {
10167 10168          struct opthdr           *optp;
10168 10169          mblk_t                  *mp2ctl;
10169 10170          ill_t                   *ill;
10170 10171          ipif_t                  *ipif;
10171 10172          ilm_t                   *ilm;
10172 10173          ip_member_t             ipm;
10173 10174          mblk_t                  *mp_tail = NULL;
10174 10175          ill_walk_context_t      ctx;
10175 10176          zoneid_t                zoneid;
10176 10177  
10177 10178          /*
10178 10179           * make a copy of the original message
10179 10180           */
10180 10181          mp2ctl = copymsg(mpctl);
10181 10182          zoneid = Q_TO_CONN(q)->conn_zoneid;
10182 10183  
10183 10184          /* ipGroupMember table */
10184 10185          optp = (struct opthdr *)&mpctl->b_rptr[
10185 10186              sizeof (struct T_optmgmt_ack)];
10186 10187          optp->level = MIB2_IP;
10187 10188          optp->name = EXPER_IP_GROUP_MEMBERSHIP;
10188 10189  
10189 10190          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10190 10191          ill = ILL_START_WALK_V4(&ctx, ipst);
10191 10192          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10192 10193                  /* Make sure the ill isn't going away. */
10193 10194                  if (!ill_check_and_refhold(ill))
10194 10195                          continue;
10195 10196                  rw_exit(&ipst->ips_ill_g_lock);
10196 10197                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10197 10198                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10198 10199                          if (ilm->ilm_zoneid != zoneid &&
10199 10200                              ilm->ilm_zoneid != ALL_ZONES)
10200 10201                                  continue;
10201 10202  
10202 10203                          /* Is there an ipif for ilm_ifaddr? */
10203 10204                          for (ipif = ill->ill_ipif; ipif != NULL;
10204 10205                              ipif = ipif->ipif_next) {
10205 10206                                  if (!IPIF_IS_CONDEMNED(ipif) &&
10206 10207                                      ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10207 10208                                      ilm->ilm_ifaddr != INADDR_ANY)
10208 10209                                          break;
10209 10210                          }
10210 10211                          if (ipif != NULL) {
10211 10212                                  ipif_get_name(ipif,
10212 10213                                      ipm.ipGroupMemberIfIndex.o_bytes,
10213 10214                                      OCTET_LENGTH);
10214 10215                          } else {
10215 10216                                  ill_get_name(ill,
10216 10217                                      ipm.ipGroupMemberIfIndex.o_bytes,
10217 10218                                      OCTET_LENGTH);
10218 10219                          }
10219 10220                          ipm.ipGroupMemberIfIndex.o_length =
10220 10221                              mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
10221 10222  
10222 10223                          ipm.ipGroupMemberAddress = ilm->ilm_addr;
10223 10224                          ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
10224 10225                          ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
10225 10226                          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10226 10227                              (char *)&ipm, (int)sizeof (ipm))) {
10227 10228                                  ip1dbg(("ip_snmp_get_mib2_ip_group: "
10228 10229                                      "failed to allocate %u bytes\n",
10229 10230                                      (uint_t)sizeof (ipm)));
10230 10231                          }
10231 10232                  }
10232 10233                  rw_exit(&ill->ill_mcast_lock);
10233 10234                  ill_refrele(ill);
10234 10235                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10235 10236          }
10236 10237          rw_exit(&ipst->ips_ill_g_lock);
10237 10238          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10238 10239          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10239 10240              (int)optp->level, (int)optp->name, (int)optp->len));
10240 10241          qreply(q, mpctl);
10241 10242          return (mp2ctl);
10242 10243  }
10243 10244  
10244 10245  /* IPv6 multicast group membership. */
10245 10246  static mblk_t *
10246 10247  ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10247 10248  {
10248 10249          struct opthdr           *optp;
10249 10250          mblk_t                  *mp2ctl;
10250 10251          ill_t                   *ill;
10251 10252          ilm_t                   *ilm;
10252 10253          ipv6_member_t           ipm6;
10253 10254          mblk_t                  *mp_tail = NULL;
10254 10255          ill_walk_context_t      ctx;
10255 10256          zoneid_t                zoneid;
10256 10257  
10257 10258          /*
10258 10259           * make a copy of the original message
10259 10260           */
10260 10261          mp2ctl = copymsg(mpctl);
10261 10262          zoneid = Q_TO_CONN(q)->conn_zoneid;
10262 10263  
10263 10264          /* ip6GroupMember table */
10264 10265          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10265 10266          optp->level = MIB2_IP6;
10266 10267          optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10267 10268  
10268 10269          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10269 10270          ill = ILL_START_WALK_V6(&ctx, ipst);
10270 10271          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10271 10272                  /* Make sure the ill isn't going away. */
10272 10273                  if (!ill_check_and_refhold(ill))
10273 10274                          continue;
10274 10275                  rw_exit(&ipst->ips_ill_g_lock);
10275 10276                  /*
10276 10277                   * Normally we don't have any members on under IPMP interfaces.
10277 10278                   * We report them as a debugging aid.
10278 10279                   */
10279 10280                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10280 10281                  ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10281 10282                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10282 10283                          if (ilm->ilm_zoneid != zoneid &&
10283 10284                              ilm->ilm_zoneid != ALL_ZONES)
10284 10285                                  continue;       /* not this zone */
10285 10286                          ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10286 10287                          ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10287 10288                          ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10288 10289                          if (!snmp_append_data2(mpctl->b_cont,
10289 10290                              &mp_tail,
10290 10291                              (char *)&ipm6, (int)sizeof (ipm6))) {
10291 10292                                  ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10292 10293                                      "failed to allocate %u bytes\n",
10293 10294                                      (uint_t)sizeof (ipm6)));
10294 10295                          }
10295 10296                  }
10296 10297                  rw_exit(&ill->ill_mcast_lock);
10297 10298                  ill_refrele(ill);
10298 10299                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10299 10300          }
10300 10301          rw_exit(&ipst->ips_ill_g_lock);
10301 10302  
10302 10303          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10303 10304          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10304 10305              (int)optp->level, (int)optp->name, (int)optp->len));
10305 10306          qreply(q, mpctl);
10306 10307          return (mp2ctl);
10307 10308  }
10308 10309  
10309 10310  /* IP multicast filtered sources */
10310 10311  static mblk_t *
10311 10312  ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10312 10313  {
10313 10314          struct opthdr           *optp;
10314 10315          mblk_t                  *mp2ctl;
10315 10316          ill_t                   *ill;
10316 10317          ipif_t                  *ipif;
10317 10318          ilm_t                   *ilm;
10318 10319          ip_grpsrc_t             ips;
10319 10320          mblk_t                  *mp_tail = NULL;
10320 10321          ill_walk_context_t      ctx;
10321 10322          zoneid_t                zoneid;
10322 10323          int                     i;
10323 10324          slist_t                 *sl;
10324 10325  
10325 10326          /*
10326 10327           * make a copy of the original message
10327 10328           */
10328 10329          mp2ctl = copymsg(mpctl);
10329 10330          zoneid = Q_TO_CONN(q)->conn_zoneid;
10330 10331  
10331 10332          /* ipGroupSource table */
10332 10333          optp = (struct opthdr *)&mpctl->b_rptr[
10333 10334              sizeof (struct T_optmgmt_ack)];
10334 10335          optp->level = MIB2_IP;
10335 10336          optp->name = EXPER_IP_GROUP_SOURCES;
10336 10337  
10337 10338          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10338 10339          ill = ILL_START_WALK_V4(&ctx, ipst);
10339 10340          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10340 10341                  /* Make sure the ill isn't going away. */
10341 10342                  if (!ill_check_and_refhold(ill))
10342 10343                          continue;
10343 10344                  rw_exit(&ipst->ips_ill_g_lock);
10344 10345                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10345 10346                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10346 10347                          sl = ilm->ilm_filter;
10347 10348                          if (ilm->ilm_zoneid != zoneid &&
10348 10349                              ilm->ilm_zoneid != ALL_ZONES)
10349 10350                                  continue;
10350 10351                          if (SLIST_IS_EMPTY(sl))
10351 10352                                  continue;
10352 10353  
10353 10354                          /* Is there an ipif for ilm_ifaddr? */
10354 10355                          for (ipif = ill->ill_ipif; ipif != NULL;
10355 10356                              ipif = ipif->ipif_next) {
10356 10357                                  if (!IPIF_IS_CONDEMNED(ipif) &&
10357 10358                                      ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10358 10359                                      ilm->ilm_ifaddr != INADDR_ANY)
10359 10360                                          break;
10360 10361                          }
10361 10362                          if (ipif != NULL) {
10362 10363                                  ipif_get_name(ipif,
10363 10364                                      ips.ipGroupSourceIfIndex.o_bytes,
10364 10365                                      OCTET_LENGTH);
10365 10366                          } else {
10366 10367                                  ill_get_name(ill,
10367 10368                                      ips.ipGroupSourceIfIndex.o_bytes,
10368 10369                                      OCTET_LENGTH);
10369 10370                          }
10370 10371                          ips.ipGroupSourceIfIndex.o_length =
10371 10372                              mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10372 10373  
10373 10374                          ips.ipGroupSourceGroup = ilm->ilm_addr;
10374 10375                          for (i = 0; i < sl->sl_numsrc; i++) {
10375 10376                                  if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10376 10377                                          continue;
10377 10378                                  IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10378 10379                                      ips.ipGroupSourceAddress);
10379 10380                                  if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10380 10381                                      (char *)&ips, (int)sizeof (ips)) == 0) {
10381 10382                                          ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10382 10383                                              " failed to allocate %u bytes\n",
10383 10384                                              (uint_t)sizeof (ips)));
10384 10385                                  }
10385 10386                          }
10386 10387                  }
10387 10388                  rw_exit(&ill->ill_mcast_lock);
10388 10389                  ill_refrele(ill);
10389 10390                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10390 10391          }
10391 10392          rw_exit(&ipst->ips_ill_g_lock);
10392 10393          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10393 10394          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10394 10395              (int)optp->level, (int)optp->name, (int)optp->len));
10395 10396          qreply(q, mpctl);
10396 10397          return (mp2ctl);
10397 10398  }
10398 10399  
10399 10400  /* IPv6 multicast filtered sources. */
10400 10401  static mblk_t *
10401 10402  ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10402 10403  {
10403 10404          struct opthdr           *optp;
10404 10405          mblk_t                  *mp2ctl;
10405 10406          ill_t                   *ill;
10406 10407          ilm_t                   *ilm;
10407 10408          ipv6_grpsrc_t           ips6;
10408 10409          mblk_t                  *mp_tail = NULL;
10409 10410          ill_walk_context_t      ctx;
10410 10411          zoneid_t                zoneid;
10411 10412          int                     i;
10412 10413          slist_t                 *sl;
10413 10414  
10414 10415          /*
10415 10416           * make a copy of the original message
10416 10417           */
10417 10418          mp2ctl = copymsg(mpctl);
10418 10419          zoneid = Q_TO_CONN(q)->conn_zoneid;
10419 10420  
10420 10421          /* ip6GroupMember table */
10421 10422          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10422 10423          optp->level = MIB2_IP6;
10423 10424          optp->name = EXPER_IP6_GROUP_SOURCES;
10424 10425  
10425 10426          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10426 10427          ill = ILL_START_WALK_V6(&ctx, ipst);
10427 10428          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10428 10429                  /* Make sure the ill isn't going away. */
10429 10430                  if (!ill_check_and_refhold(ill))
10430 10431                          continue;
10431 10432                  rw_exit(&ipst->ips_ill_g_lock);
10432 10433                  /*
10433 10434                   * Normally we don't have any members on under IPMP interfaces.
10434 10435                   * We report them as a debugging aid.
10435 10436                   */
10436 10437                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10437 10438                  ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10438 10439                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10439 10440                          sl = ilm->ilm_filter;
10440 10441                          if (ilm->ilm_zoneid != zoneid &&
10441 10442                              ilm->ilm_zoneid != ALL_ZONES)
10442 10443                                  continue;
10443 10444                          if (SLIST_IS_EMPTY(sl))
10444 10445                                  continue;
10445 10446                          ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10446 10447                          for (i = 0; i < sl->sl_numsrc; i++) {
10447 10448                                  ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10448 10449                                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10449 10450                                      (char *)&ips6, (int)sizeof (ips6))) {
10450 10451                                          ip1dbg(("ip_snmp_get_mib2_ip6_"
10451 10452                                              "group_src: failed to allocate "
10452 10453                                              "%u bytes\n",
10453 10454                                              (uint_t)sizeof (ips6)));
10454 10455                                  }
10455 10456                          }
10456 10457                  }
10457 10458                  rw_exit(&ill->ill_mcast_lock);
10458 10459                  ill_refrele(ill);
10459 10460                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10460 10461          }
10461 10462          rw_exit(&ipst->ips_ill_g_lock);
10462 10463  
10463 10464          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10464 10465          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10465 10466              (int)optp->level, (int)optp->name, (int)optp->len));
10466 10467          qreply(q, mpctl);
10467 10468          return (mp2ctl);
10468 10469  }
10469 10470  
10470 10471  /* Multicast routing virtual interface table. */
10471 10472  static mblk_t *
10472 10473  ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10473 10474  {
10474 10475          struct opthdr           *optp;
10475 10476          mblk_t                  *mp2ctl;
10476 10477  
10477 10478          /*
10478 10479           * make a copy of the original message
10479 10480           */
10480 10481          mp2ctl = copymsg(mpctl);
10481 10482  
10482 10483          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10483 10484          optp->level = EXPER_DVMRP;
10484 10485          optp->name = EXPER_DVMRP_VIF;
10485 10486          if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10486 10487                  ip0dbg(("ip_mroute_vif: failed\n"));
10487 10488          }
10488 10489          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10489 10490          ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10490 10491              (int)optp->level, (int)optp->name, (int)optp->len));
10491 10492          qreply(q, mpctl);
10492 10493          return (mp2ctl);
10493 10494  }
10494 10495  
10495 10496  /* Multicast routing table. */
10496 10497  static mblk_t *
10497 10498  ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10498 10499  {
10499 10500          struct opthdr           *optp;
10500 10501          mblk_t                  *mp2ctl;
10501 10502  
10502 10503          /*
10503 10504           * make a copy of the original message
10504 10505           */
10505 10506          mp2ctl = copymsg(mpctl);
10506 10507  
10507 10508          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10508 10509          optp->level = EXPER_DVMRP;
10509 10510          optp->name = EXPER_DVMRP_MRT;
10510 10511          if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10511 10512                  ip0dbg(("ip_mroute_mrt: failed\n"));
10512 10513          }
10513 10514          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10514 10515          ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10515 10516              (int)optp->level, (int)optp->name, (int)optp->len));
10516 10517          qreply(q, mpctl);
10517 10518          return (mp2ctl);
10518 10519  }
10519 10520  
10520 10521  /*
10521 10522   * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable
10522 10523   * in one IRE walk.
10523 10524   */
10524 10525  static mblk_t *
10525 10526  ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10526 10527      ip_stack_t *ipst)
10527 10528  {
10528 10529          struct opthdr   *optp;
10529 10530          mblk_t          *mp2ctl;        /* Returned */
10530 10531          mblk_t          *mp3ctl;        /* nettomedia */
10531 10532          mblk_t          *mp4ctl;        /* routeattrs */
10532 10533          iproutedata_t   ird;
10533 10534          zoneid_t        zoneid;
10534 10535  
10535 10536          /*
10536 10537           * make copies of the original message
10537 10538           *      - mp2ctl is returned unchanged to the caller for his use
10538 10539           *      - mpctl is sent upstream as ipRouteEntryTable
10539 10540           *      - mp3ctl is sent upstream as ipNetToMediaEntryTable
10540 10541           *      - mp4ctl is sent upstream as ipRouteAttributeTable
10541 10542           */
10542 10543          mp2ctl = copymsg(mpctl);
10543 10544          mp3ctl = copymsg(mpctl);
10544 10545          mp4ctl = copymsg(mpctl);
10545 10546          if (mp3ctl == NULL || mp4ctl == NULL) {
10546 10547                  freemsg(mp4ctl);
10547 10548                  freemsg(mp3ctl);
10548 10549                  freemsg(mp2ctl);
10549 10550                  freemsg(mpctl);
10550 10551                  return (NULL);
10551 10552          }
10552 10553  
10553 10554          bzero(&ird, sizeof (ird));
10554 10555  
10555 10556          ird.ird_route.lp_head = mpctl->b_cont;
10556 10557          ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10557 10558          ird.ird_attrs.lp_head = mp4ctl->b_cont;
10558 10559          /*
10559 10560           * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10560 10561           * then also include ire_testhidden IREs and IRE_IF_CLONE.  This is
10561 10562           * intended a temporary solution until a proper MIB API is provided
10562 10563           * that provides complete filtering/caller-opt-in.
10563 10564           */
10564 10565          if (level == EXPER_IP_AND_ALL_IRES)
10565 10566                  ird.ird_flags |= IRD_REPORT_ALL;
10566 10567  
10567 10568          zoneid = Q_TO_CONN(q)->conn_zoneid;
10568 10569          ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10569 10570  
10570 10571          /* ipRouteEntryTable in mpctl */
10571 10572          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10572 10573          optp->level = MIB2_IP;
10573 10574          optp->name = MIB2_IP_ROUTE;
10574 10575          optp->len = msgdsize(ird.ird_route.lp_head);
10575 10576          ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10576 10577              (int)optp->level, (int)optp->name, (int)optp->len));
10577 10578          qreply(q, mpctl);
10578 10579  
10579 10580          /* ipNetToMediaEntryTable in mp3ctl */
10580 10581          ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10581 10582  
10582 10583          optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10583 10584          optp->level = MIB2_IP;
10584 10585          optp->name = MIB2_IP_MEDIA;
10585 10586          optp->len = msgdsize(ird.ird_netmedia.lp_head);
10586 10587          ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10587 10588              (int)optp->level, (int)optp->name, (int)optp->len));
10588 10589          qreply(q, mp3ctl);
10589 10590  
10590 10591          /* ipRouteAttributeTable in mp4ctl */
10591 10592          optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10592 10593          optp->level = MIB2_IP;
10593 10594          optp->name = EXPER_IP_RTATTR;
10594 10595          optp->len = msgdsize(ird.ird_attrs.lp_head);
10595 10596          ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10596 10597              (int)optp->level, (int)optp->name, (int)optp->len));
10597 10598          if (optp->len == 0)
10598 10599                  freemsg(mp4ctl);
10599 10600          else
10600 10601                  qreply(q, mp4ctl);
10601 10602  
10602 10603          return (mp2ctl);
10603 10604  }
10604 10605  
10605 10606  /*
10606 10607   * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and
10607 10608   * ipv6NetToMediaEntryTable in an NDP walk.
10608 10609   */
10609 10610  static mblk_t *
10610 10611  ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10611 10612      ip_stack_t *ipst)
10612 10613  {
10613 10614          struct opthdr   *optp;
10614 10615          mblk_t          *mp2ctl;        /* Returned */
10615 10616          mblk_t          *mp3ctl;        /* nettomedia */
10616 10617          mblk_t          *mp4ctl;        /* routeattrs */
10617 10618          iproutedata_t   ird;
10618 10619          zoneid_t        zoneid;
10619 10620  
10620 10621          /*
10621 10622           * make copies of the original message
10622 10623           *      - mp2ctl is returned unchanged to the caller for his use
10623 10624           *      - mpctl is sent upstream as ipv6RouteEntryTable
10624 10625           *      - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10625 10626           *      - mp4ctl is sent upstream as ipv6RouteAttributeTable
10626 10627           */
10627 10628          mp2ctl = copymsg(mpctl);
10628 10629          mp3ctl = copymsg(mpctl);
10629 10630          mp4ctl = copymsg(mpctl);
10630 10631          if (mp3ctl == NULL || mp4ctl == NULL) {
10631 10632                  freemsg(mp4ctl);
10632 10633                  freemsg(mp3ctl);
10633 10634                  freemsg(mp2ctl);
10634 10635                  freemsg(mpctl);
10635 10636                  return (NULL);
10636 10637          }
10637 10638  
10638 10639          bzero(&ird, sizeof (ird));
10639 10640  
10640 10641          ird.ird_route.lp_head = mpctl->b_cont;
10641 10642          ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10642 10643          ird.ird_attrs.lp_head = mp4ctl->b_cont;
10643 10644          /*
10644 10645           * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10645 10646           * then also include ire_testhidden IREs and IRE_IF_CLONE.  This is
10646 10647           * intended a temporary solution until a proper MIB API is provided
10647 10648           * that provides complete filtering/caller-opt-in.
10648 10649           */
10649 10650          if (level == EXPER_IP_AND_ALL_IRES)
10650 10651                  ird.ird_flags |= IRD_REPORT_ALL;
10651 10652  
10652 10653          zoneid = Q_TO_CONN(q)->conn_zoneid;
10653 10654          ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10654 10655  
10655 10656          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10656 10657          optp->level = MIB2_IP6;
10657 10658          optp->name = MIB2_IP6_ROUTE;
10658 10659          optp->len = msgdsize(ird.ird_route.lp_head);
10659 10660          ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10660 10661              (int)optp->level, (int)optp->name, (int)optp->len));
10661 10662          qreply(q, mpctl);
10662 10663  
10663 10664          /* ipv6NetToMediaEntryTable in mp3ctl */
10664 10665          ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10665 10666  
10666 10667          optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10667 10668          optp->level = MIB2_IP6;
10668 10669          optp->name = MIB2_IP6_MEDIA;
10669 10670          optp->len = msgdsize(ird.ird_netmedia.lp_head);
10670 10671          ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10671 10672              (int)optp->level, (int)optp->name, (int)optp->len));
10672 10673          qreply(q, mp3ctl);
10673 10674  
10674 10675          /* ipv6RouteAttributeTable in mp4ctl */
10675 10676          optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10676 10677          optp->level = MIB2_IP6;
10677 10678          optp->name = EXPER_IP_RTATTR;
10678 10679          optp->len = msgdsize(ird.ird_attrs.lp_head);
10679 10680          ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10680 10681              (int)optp->level, (int)optp->name, (int)optp->len));
10681 10682          if (optp->len == 0)
10682 10683                  freemsg(mp4ctl);
10683 10684          else
10684 10685                  qreply(q, mp4ctl);
10685 10686  
10686 10687          return (mp2ctl);
10687 10688  }
10688 10689  
10689 10690  /*
10690 10691   * IPv6 mib: One per ill
10691 10692   */
10692 10693  static mblk_t *
10693 10694  ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10694 10695      boolean_t legacy_req)
10695 10696  {
10696 10697          struct opthdr           *optp;
10697 10698          mblk_t                  *mp2ctl;
10698 10699          ill_t                   *ill;
10699 10700          ill_walk_context_t      ctx;
10700 10701          mblk_t                  *mp_tail = NULL;
10701 10702          mib2_ipv6AddrEntry_t    mae6;
10702 10703          mib2_ipIfStatsEntry_t   *ise;
10703 10704          size_t                  ise_size, iae_size;
10704 10705  
10705 10706          /*
10706 10707           * Make a copy of the original message
10707 10708           */
10708 10709          mp2ctl = copymsg(mpctl);
10709 10710  
10710 10711          /* fixed length IPv6 structure ... */
10711 10712  
10712 10713          if (legacy_req) {
10713 10714                  ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10714 10715                      mib2_ipIfStatsEntry_t);
10715 10716                  iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10716 10717          } else {
10717 10718                  ise_size = sizeof (mib2_ipIfStatsEntry_t);
10718 10719                  iae_size = sizeof (mib2_ipv6AddrEntry_t);
10719 10720          }
10720 10721  
10721 10722          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10722 10723          optp->level = MIB2_IP6;
10723 10724          optp->name = 0;
10724 10725          /* Include "unknown interface" ip6_mib */
10725 10726          ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10726 10727          ipst->ips_ip6_mib.ipIfStatsIfIndex =
10727 10728              MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10728 10729          SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10729 10730              ipst->ips_ipv6_forwarding ? 1 : 2);
10730 10731          SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10731 10732              ipst->ips_ipv6_def_hops);
10732 10733          SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10733 10734              sizeof (mib2_ipIfStatsEntry_t));
10734 10735          SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10735 10736              sizeof (mib2_ipv6AddrEntry_t));
10736 10737          SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10737 10738              sizeof (mib2_ipv6RouteEntry_t));
10738 10739          SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10739 10740              sizeof (mib2_ipv6NetToMediaEntry_t));
10740 10741          SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10741 10742              sizeof (ipv6_member_t));
10742 10743          SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10743 10744              sizeof (ipv6_grpsrc_t));
10744 10745  
10745 10746          /*
10746 10747           * Synchronize 64- and 32-bit counters
10747 10748           */
10748 10749          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10749 10750              ipIfStatsHCInReceives);
10750 10751          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10751 10752              ipIfStatsHCInDelivers);
10752 10753          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10753 10754              ipIfStatsHCOutRequests);
10754 10755          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10755 10756              ipIfStatsHCOutForwDatagrams);
10756 10757          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10757 10758              ipIfStatsHCOutMcastPkts);
10758 10759          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10759 10760              ipIfStatsHCInMcastPkts);
10760 10761  
10761 10762          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10762 10763              (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10763 10764                  ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10764 10765                      (uint_t)ise_size));
10765 10766          } else if (legacy_req) {
10766 10767                  /* Adjust the EntrySize fields for legacy requests. */
10767 10768                  ise =
10768 10769                      (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10769 10770                  SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10770 10771                  SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10771 10772          }
10772 10773  
10773 10774          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10774 10775          ill = ILL_START_WALK_V6(&ctx, ipst);
10775 10776          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10776 10777                  ill->ill_ip_mib->ipIfStatsIfIndex =
10777 10778                      ill->ill_phyint->phyint_ifindex;
10778 10779                  SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10779 10780                      ipst->ips_ipv6_forwarding ? 1 : 2);
10780 10781                  SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10781 10782                      ill->ill_max_hops);
10782 10783  
10783 10784                  /*
10784 10785                   * Synchronize 64- and 32-bit counters
10785 10786                   */
10786 10787                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10787 10788                      ipIfStatsHCInReceives);
10788 10789                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10789 10790                      ipIfStatsHCInDelivers);
10790 10791                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10791 10792                      ipIfStatsHCOutRequests);
10792 10793                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10793 10794                      ipIfStatsHCOutForwDatagrams);
10794 10795                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10795 10796                      ipIfStatsHCOutMcastPkts);
10796 10797                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10797 10798                      ipIfStatsHCInMcastPkts);
10798 10799  
10799 10800                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10800 10801                      (char *)ill->ill_ip_mib, (int)ise_size)) {
10801 10802                          ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10802 10803                          "%u bytes\n", (uint_t)ise_size));
10803 10804                  } else if (legacy_req) {
10804 10805                          /* Adjust the EntrySize fields for legacy requests. */
10805 10806                          ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10806 10807                              (int)ise_size);
10807 10808                          SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10808 10809                          SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10809 10810                  }
10810 10811          }
10811 10812          rw_exit(&ipst->ips_ill_g_lock);
10812 10813  
10813 10814          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10814 10815          ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10815 10816              (int)optp->level, (int)optp->name, (int)optp->len));
10816 10817          qreply(q, mpctl);
10817 10818          return (mp2ctl);
10818 10819  }
10819 10820  
10820 10821  /*
10821 10822   * ICMPv6 mib: One per ill
10822 10823   */
10823 10824  static mblk_t *
10824 10825  ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10825 10826  {
10826 10827          struct opthdr           *optp;
10827 10828          mblk_t                  *mp2ctl;
10828 10829          ill_t                   *ill;
10829 10830          ill_walk_context_t      ctx;
10830 10831          mblk_t                  *mp_tail = NULL;
10831 10832          /*
10832 10833           * Make a copy of the original message
10833 10834           */
10834 10835          mp2ctl = copymsg(mpctl);
10835 10836  
10836 10837          /* fixed length ICMPv6 structure ... */
10837 10838  
10838 10839          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10839 10840          optp->level = MIB2_ICMP6;
10840 10841          optp->name = 0;
10841 10842          /* Include "unknown interface" icmp6_mib */
10842 10843          ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10843 10844              MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10844 10845          ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10845 10846              sizeof (mib2_ipv6IfIcmpEntry_t);
10846 10847          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10847 10848              (char *)&ipst->ips_icmp6_mib,
10848 10849              (int)sizeof (ipst->ips_icmp6_mib))) {
10849 10850                  ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10850 10851                      (uint_t)sizeof (ipst->ips_icmp6_mib)));
10851 10852          }
10852 10853  
10853 10854          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10854 10855          ill = ILL_START_WALK_V6(&ctx, ipst);
10855 10856          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10856 10857                  ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10857 10858                      ill->ill_phyint->phyint_ifindex;
10858 10859                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10859 10860                      (char *)ill->ill_icmp6_mib,
10860 10861                      (int)sizeof (*ill->ill_icmp6_mib))) {
10861 10862                          ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10862 10863                              "%u bytes\n",
10863 10864                              (uint_t)sizeof (*ill->ill_icmp6_mib)));
10864 10865                  }
10865 10866          }
10866 10867          rw_exit(&ipst->ips_ill_g_lock);
10867 10868  
10868 10869          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10869 10870          ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10870 10871              (int)optp->level, (int)optp->name, (int)optp->len));
10871 10872          qreply(q, mpctl);
10872 10873          return (mp2ctl);
10873 10874  }
10874 10875  
10875 10876  /*
10876 10877   * ire_walk routine to create both ipRouteEntryTable and
10877 10878   * ipRouteAttributeTable in one IRE walk
10878 10879   */
10879 10880  static void
10880 10881  ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10881 10882  {
10882 10883          ill_t                           *ill;
10883 10884          mib2_ipRouteEntry_t             *re;
10884 10885          mib2_ipAttributeEntry_t         iaes;
10885 10886          tsol_ire_gw_secattr_t           *attrp;
10886 10887          tsol_gc_t                       *gc = NULL;
10887 10888          tsol_gcgrp_t                    *gcgrp = NULL;
10888 10889          ip_stack_t                      *ipst = ire->ire_ipst;
10889 10890  
10890 10891          ASSERT(ire->ire_ipversion == IPV4_VERSION);
10891 10892  
10892 10893          if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10893 10894                  if (ire->ire_testhidden)
10894 10895                          return;
10895 10896                  if (ire->ire_type & IRE_IF_CLONE)
10896 10897                          return;
10897 10898          }
10898 10899  
10899 10900          if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10900 10901                  return;
10901 10902  
10902 10903          if ((attrp = ire->ire_gw_secattr) != NULL) {
10903 10904                  mutex_enter(&attrp->igsa_lock);
10904 10905                  if ((gc = attrp->igsa_gc) != NULL) {
10905 10906                          gcgrp = gc->gc_grp;
10906 10907                          ASSERT(gcgrp != NULL);
10907 10908                          rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
10908 10909                  }
10909 10910                  mutex_exit(&attrp->igsa_lock);
10910 10911          }
10911 10912          /*
10912 10913           * Return all IRE types for route table... let caller pick and choose
10913 10914           */
10914 10915          re->ipRouteDest = ire->ire_addr;
10915 10916          ill = ire->ire_ill;
10916 10917          re->ipRouteIfIndex.o_length = 0;
10917 10918          if (ill != NULL) {
10918 10919                  ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10919 10920                  re->ipRouteIfIndex.o_length =
10920 10921                      mi_strlen(re->ipRouteIfIndex.o_bytes);
10921 10922          }
10922 10923          re->ipRouteMetric1 = -1;
10923 10924          re->ipRouteMetric2 = -1;
10924 10925          re->ipRouteMetric3 = -1;
10925 10926          re->ipRouteMetric4 = -1;
10926 10927  
10927 10928          re->ipRouteNextHop = ire->ire_gateway_addr;
10928 10929          /* indirect(4), direct(3), or invalid(2) */
10929 10930          if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10930 10931                  re->ipRouteType = 2;
10931 10932          else if (ire->ire_type & IRE_ONLINK)
10932 10933                  re->ipRouteType = 3;
10933 10934          else
10934 10935                  re->ipRouteType = 4;
10935 10936  
10936 10937          re->ipRouteProto = -1;
10937 10938          re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10938 10939          re->ipRouteMask = ire->ire_mask;
10939 10940          re->ipRouteMetric5 = -1;
10940 10941          re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10941 10942          if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10942 10943                  re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10943 10944  
10944 10945          re->ipRouteInfo.re_frag_flag    = 0;
10945 10946          re->ipRouteInfo.re_rtt          = 0;
10946 10947          re->ipRouteInfo.re_src_addr     = 0;
10947 10948          re->ipRouteInfo.re_ref          = ire->ire_refcnt;
10948 10949          re->ipRouteInfo.re_obpkt        = ire->ire_ob_pkt_count;
10949 10950          re->ipRouteInfo.re_ibpkt        = ire->ire_ib_pkt_count;
10950 10951          re->ipRouteInfo.re_flags        = ire->ire_flags;
10951 10952  
10952 10953          /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10953 10954          if (ire->ire_type & IRE_INTERFACE) {
10954 10955                  ire_t *child;
10955 10956  
10956 10957                  rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10957 10958                  child = ire->ire_dep_children;
10958 10959                  while (child != NULL) {
10959 10960                          re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10960 10961                          re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10961 10962                          child = child->ire_dep_sib_next;
10962 10963                  }
10963 10964                  rw_exit(&ipst->ips_ire_dep_lock);
10964 10965          }
10965 10966  
10966 10967          if (ire->ire_flags & RTF_DYNAMIC) {
10967 10968                  re->ipRouteInfo.re_ire_type     = IRE_HOST_REDIRECT;
10968 10969          } else {
10969 10970                  re->ipRouteInfo.re_ire_type     = ire->ire_type;
10970 10971          }
10971 10972  
10972 10973          if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
10973 10974              (char *)re, (int)sizeof (*re))) {
10974 10975                  ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
10975 10976                      (uint_t)sizeof (*re)));
10976 10977          }
10977 10978  
10978 10979          if (gc != NULL) {
10979 10980                  iaes.iae_routeidx = ird->ird_idx;
10980 10981                  iaes.iae_doi = gc->gc_db->gcdb_doi;
10981 10982                  iaes.iae_slrange = gc->gc_db->gcdb_slrange;
10982 10983  
10983 10984                  if (!snmp_append_data2(ird->ird_attrs.lp_head,
10984 10985                      &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
10985 10986                          ip1dbg(("ip_snmp_get2_v4: failed to allocate %u "
10986 10987                              "bytes\n", (uint_t)sizeof (iaes)));
10987 10988                  }
10988 10989          }
10989 10990  
10990 10991          /* bump route index for next pass */
10991 10992          ird->ird_idx++;
10992 10993  
10993 10994          kmem_free(re, sizeof (*re));
10994 10995          if (gcgrp != NULL)
10995 10996                  rw_exit(&gcgrp->gcgrp_rwlock);
10996 10997  }
10997 10998  
10998 10999  /*
10999 11000   * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
11000 11001   */
11001 11002  static void
11002 11003  ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
11003 11004  {
11004 11005          ill_t                           *ill;
11005 11006          mib2_ipv6RouteEntry_t           *re;
11006 11007          mib2_ipAttributeEntry_t         iaes;
11007 11008          tsol_ire_gw_secattr_t           *attrp;
11008 11009          tsol_gc_t                       *gc = NULL;
11009 11010          tsol_gcgrp_t                    *gcgrp = NULL;
11010 11011          ip_stack_t                      *ipst = ire->ire_ipst;
11011 11012  
11012 11013          ASSERT(ire->ire_ipversion == IPV6_VERSION);
11013 11014  
11014 11015          if (!(ird->ird_flags & IRD_REPORT_ALL)) {
11015 11016                  if (ire->ire_testhidden)
11016 11017                          return;
11017 11018                  if (ire->ire_type & IRE_IF_CLONE)
11018 11019                          return;
11019 11020          }
11020 11021  
11021 11022          if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
11022 11023                  return;
11023 11024  
11024 11025          if ((attrp = ire->ire_gw_secattr) != NULL) {
11025 11026                  mutex_enter(&attrp->igsa_lock);
11026 11027                  if ((gc = attrp->igsa_gc) != NULL) {
11027 11028                          gcgrp = gc->gc_grp;
11028 11029                          ASSERT(gcgrp != NULL);
11029 11030                          rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
11030 11031                  }
11031 11032                  mutex_exit(&attrp->igsa_lock);
11032 11033          }
11033 11034          /*
11034 11035           * Return all IRE types for route table... let caller pick and choose
11035 11036           */
11036 11037          re->ipv6RouteDest = ire->ire_addr_v6;
11037 11038          re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
11038 11039          re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
11039 11040          re->ipv6RouteIfIndex.o_length = 0;
11040 11041          ill = ire->ire_ill;
11041 11042          if (ill != NULL) {
11042 11043                  ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
11043 11044                  re->ipv6RouteIfIndex.o_length =
11044 11045                      mi_strlen(re->ipv6RouteIfIndex.o_bytes);
11045 11046          }
11046 11047  
11047 11048          ASSERT(!(ire->ire_type & IRE_BROADCAST));
11048 11049  
11049 11050          mutex_enter(&ire->ire_lock);
11050 11051          re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
11051 11052          mutex_exit(&ire->ire_lock);
11052 11053  
11053 11054          /* remote(4), local(3), or discard(2) */
11054 11055          if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11055 11056                  re->ipv6RouteType = 2;
11056 11057          else if (ire->ire_type & IRE_ONLINK)
11057 11058                  re->ipv6RouteType = 3;
11058 11059          else
11059 11060                  re->ipv6RouteType = 4;
11060 11061  
11061 11062          re->ipv6RouteProtocol   = -1;
11062 11063          re->ipv6RoutePolicy     = 0;
11063 11064          re->ipv6RouteAge        = gethrestime_sec() - ire->ire_create_time;
11064 11065          re->ipv6RouteNextHopRDI = 0;
11065 11066          re->ipv6RouteWeight     = 0;
11066 11067          re->ipv6RouteMetric     = 0;
11067 11068          re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11068 11069          if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
11069 11070                  re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11070 11071  
11071 11072          re->ipv6RouteInfo.re_frag_flag  = 0;
11072 11073          re->ipv6RouteInfo.re_rtt        = 0;
11073 11074          re->ipv6RouteInfo.re_src_addr   = ipv6_all_zeros;
11074 11075          re->ipv6RouteInfo.re_obpkt      = ire->ire_ob_pkt_count;
11075 11076          re->ipv6RouteInfo.re_ibpkt      = ire->ire_ib_pkt_count;
11076 11077          re->ipv6RouteInfo.re_ref        = ire->ire_refcnt;
11077 11078          re->ipv6RouteInfo.re_flags      = ire->ire_flags;
11078 11079  
11079 11080          /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11080 11081          if (ire->ire_type & IRE_INTERFACE) {
11081 11082                  ire_t *child;
11082 11083  
11083 11084                  rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11084 11085                  child = ire->ire_dep_children;
11085 11086                  while (child != NULL) {
11086 11087                          re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
11087 11088                          re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11088 11089                          child = child->ire_dep_sib_next;
11089 11090                  }
11090 11091                  rw_exit(&ipst->ips_ire_dep_lock);
11091 11092          }
11092 11093          if (ire->ire_flags & RTF_DYNAMIC) {
11093 11094                  re->ipv6RouteInfo.re_ire_type   = IRE_HOST_REDIRECT;
11094 11095          } else {
11095 11096                  re->ipv6RouteInfo.re_ire_type   = ire->ire_type;
11096 11097          }
11097 11098  
11098 11099          if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11099 11100              (char *)re, (int)sizeof (*re))) {
11100 11101                  ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
11101 11102                      (uint_t)sizeof (*re)));
11102 11103          }
11103 11104  
11104 11105          if (gc != NULL) {
11105 11106                  iaes.iae_routeidx = ird->ird_idx;
11106 11107                  iaes.iae_doi = gc->gc_db->gcdb_doi;
11107 11108                  iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11108 11109  
11109 11110                  if (!snmp_append_data2(ird->ird_attrs.lp_head,
11110 11111                      &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11111 11112                          ip1dbg(("ip_snmp_get2_v6: failed to allocate %u "
11112 11113                              "bytes\n", (uint_t)sizeof (iaes)));
11113 11114                  }
11114 11115          }
11115 11116  
11116 11117          /* bump route index for next pass */
11117 11118          ird->ird_idx++;
11118 11119  
11119 11120          kmem_free(re, sizeof (*re));
11120 11121          if (gcgrp != NULL)
11121 11122                  rw_exit(&gcgrp->gcgrp_rwlock);
11122 11123  }
11123 11124  
11124 11125  /*
11125 11126   * ncec_walk routine to create ipv6NetToMediaEntryTable
11126 11127   */
11127 11128  static int
11128 11129  ip_snmp_get2_v6_media(ncec_t *ncec, iproutedata_t *ird)
11129 11130  {
11130 11131          ill_t                           *ill;
11131 11132          mib2_ipv6NetToMediaEntry_t      ntme;
11132 11133  
11133 11134          ill = ncec->ncec_ill;
11134 11135          /* skip arpce entries, and loopback ncec entries */
11135 11136          if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
11136 11137                  return (0);
11137 11138          /*
11138 11139           * Neighbor cache entry attached to IRE with on-link
11139 11140           * destination.
11140 11141           * We report all IPMP groups on ncec_ill which is normally the upper.
11141 11142           */
11142 11143          ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
11143 11144          ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
11144 11145          ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
11145 11146          if (ncec->ncec_lladdr != NULL) {
11146 11147                  bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
11147 11148                      ntme.ipv6NetToMediaPhysAddress.o_length);
11148 11149          }
11149 11150          /*
11150 11151           * Note: Returns ND_* states. Should be:
11151 11152           * reachable(1), stale(2), delay(3), probe(4),
11152 11153           * invalid(5), unknown(6)
11153 11154           */
11154 11155          ntme.ipv6NetToMediaState = ncec->ncec_state;
11155 11156          ntme.ipv6NetToMediaLastUpdated = 0;
11156 11157  
11157 11158          /* other(1), dynamic(2), static(3), local(4) */
11158 11159          if (NCE_MYADDR(ncec)) {
11159 11160                  ntme.ipv6NetToMediaType = 4;
11160 11161          } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
11161 11162                  ntme.ipv6NetToMediaType = 1; /* proxy */
11162 11163          } else if (ncec->ncec_flags & NCE_F_STATIC) {
11163 11164                  ntme.ipv6NetToMediaType = 3;
11164 11165          } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
11165 11166                  ntme.ipv6NetToMediaType = 1;
11166 11167          } else {
11167 11168                  ntme.ipv6NetToMediaType = 2;
11168 11169          }
11169 11170  
11170 11171          if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11171 11172              &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11172 11173                  ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
11173 11174                      (uint_t)sizeof (ntme)));
11174 11175          }
11175 11176          return (0);
11176 11177  }
11177 11178  
11178 11179  int
11179 11180  nce2ace(ncec_t *ncec)
11180 11181  {
11181 11182          int flags = 0;
11182 11183  
11183 11184          if (NCE_ISREACHABLE(ncec))
11184 11185                  flags |= ACE_F_RESOLVED;
11185 11186          if (ncec->ncec_flags & NCE_F_AUTHORITY)
11186 11187                  flags |= ACE_F_AUTHORITY;
11187 11188          if (ncec->ncec_flags & NCE_F_PUBLISH)
11188 11189                  flags |= ACE_F_PUBLISH;
11189 11190          if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
11190 11191                  flags |= ACE_F_PERMANENT;
11191 11192          if (NCE_MYADDR(ncec))
11192 11193                  flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
11193 11194          if (ncec->ncec_flags & NCE_F_UNVERIFIED)
11194 11195                  flags |= ACE_F_UNVERIFIED;
11195 11196          if (ncec->ncec_flags & NCE_F_AUTHORITY)
11196 11197                  flags |= ACE_F_AUTHORITY;
11197 11198          if (ncec->ncec_flags & NCE_F_DELAYED)
11198 11199                  flags |= ACE_F_DELAYED;
11199 11200          return (flags);
11200 11201  }
11201 11202  
11202 11203  /*
11203 11204   * ncec_walk routine to create ipNetToMediaEntryTable
11204 11205   */
11205 11206  static int
11206 11207  ip_snmp_get2_v4_media(ncec_t *ncec, iproutedata_t *ird)
11207 11208  {
11208 11209          ill_t                           *ill;
11209 11210          mib2_ipNetToMediaEntry_t        ntme;
11210 11211          const char                      *name = "unknown";
11211 11212          ipaddr_t                        ncec_addr;
11212 11213  
11213 11214          ill = ncec->ncec_ill;
11214 11215          if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
11215 11216              ill->ill_net_type == IRE_LOOPBACK)
11216 11217                  return (0);
11217 11218  
11218 11219          /* We report all IPMP groups on ncec_ill which is normally the upper. */
11219 11220          name = ill->ill_name;
11220 11221          /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
11221 11222          if (NCE_MYADDR(ncec)) {
11222 11223                  ntme.ipNetToMediaType = 4;
11223 11224          } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
11224 11225                  ntme.ipNetToMediaType = 1;
11225 11226          } else {
11226 11227                  ntme.ipNetToMediaType = 3;
11227 11228          }
11228 11229          ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
11229 11230          bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
11230 11231              ntme.ipNetToMediaIfIndex.o_length);
11231 11232  
11232 11233          IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
11233 11234          bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
11234 11235  
11235 11236          ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
11236 11237          ncec_addr = INADDR_BROADCAST;
11237 11238          bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
11238 11239              sizeof (ncec_addr));
11239 11240          /*
11240 11241           * map all the flags to the ACE counterpart.
11241 11242           */
11242 11243          ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
11243 11244  
11244 11245          ntme.ipNetToMediaPhysAddress.o_length =
11245 11246              MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
11246 11247  
11247 11248          if (!NCE_ISREACHABLE(ncec))
11248 11249                  ntme.ipNetToMediaPhysAddress.o_length = 0;
11249 11250          else {
11250 11251                  if (ncec->ncec_lladdr != NULL) {
11251 11252                          bcopy(ncec->ncec_lladdr,
11252 11253                              ntme.ipNetToMediaPhysAddress.o_bytes,
11253 11254                              ntme.ipNetToMediaPhysAddress.o_length);
11254 11255                  }
11255 11256          }
11256 11257  
11257 11258          if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11258 11259              &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11259 11260                  ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
11260 11261                      (uint_t)sizeof (ntme)));
11261 11262          }
11262 11263          return (0);
11263 11264  }
11264 11265  
11265 11266  /*
11266 11267   * return (0) if invalid set request, 1 otherwise, including non-tcp requests
11267 11268   */
11268 11269  /* ARGSUSED */
11269 11270  int
11270 11271  ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
11271 11272  {
11272 11273          switch (level) {
11273 11274          case MIB2_IP:
11274 11275          case MIB2_ICMP:
11275 11276                  switch (name) {
11276 11277                  default:
11277 11278                          break;
11278 11279                  }
11279 11280                  return (1);
11280 11281          default:
11281 11282                  return (1);
11282 11283          }
11283 11284  }
11284 11285  
11285 11286  /*
11286 11287   * When there exists both a 64- and 32-bit counter of a particular type
11287 11288   * (i.e., InReceives), only the 64-bit counters are added.
11288 11289   */
11289 11290  void
11290 11291  ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
11291 11292  {
11292 11293          UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
11293 11294          UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
11294 11295          UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
11295 11296          UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
11296 11297          UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
11297 11298          UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
11298 11299          UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
11299 11300          UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
11300 11301          UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
11301 11302          UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
11302 11303          UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
11303 11304          UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
11304 11305          UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
11305 11306          UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
11306 11307          UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
11307 11308          UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
11308 11309          UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
11309 11310          UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
11310 11311          UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
11311 11312          UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
11312 11313          UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
11313 11314          UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
11314 11315              o2->ipIfStatsInWrongIPVersion);
11315 11316          UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
11316 11317              o2->ipIfStatsInWrongIPVersion);
11317 11318          UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
11318 11319              o2->ipIfStatsOutSwitchIPVersion);
11319 11320          UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
11320 11321          UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
11321 11322          UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
11322 11323              o2->ipIfStatsHCInForwDatagrams);
11323 11324          UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
11324 11325          UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
11325 11326          UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
11326 11327              o2->ipIfStatsHCOutForwDatagrams);
11327 11328          UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
11328 11329          UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
11329 11330          UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
11330 11331          UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
11331 11332          UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
11332 11333          UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
11333 11334          UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11334 11335              o2->ipIfStatsHCOutMcastOctets);
11335 11336          UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11336 11337          UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11337 11338          UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11338 11339          UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11339 11340          UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11340 11341          UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11341 11342          UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11342 11343  }
11343 11344  
11344 11345  void
11345 11346  ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11346 11347  {
11347 11348          UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11348 11349          UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11349 11350          UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11350 11351          UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11351 11352          UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11352 11353          UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11353 11354          UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11354 11355          UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11355 11356          UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11356 11357          UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11357 11358              o2->ipv6IfIcmpInRouterSolicits);
11358 11359          UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11359 11360              o2->ipv6IfIcmpInRouterAdvertisements);
11360 11361          UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11361 11362              o2->ipv6IfIcmpInNeighborSolicits);
11362 11363          UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11363 11364              o2->ipv6IfIcmpInNeighborAdvertisements);
11364 11365          UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11365 11366          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11366 11367              o2->ipv6IfIcmpInGroupMembQueries);
11367 11368          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11368 11369              o2->ipv6IfIcmpInGroupMembResponses);
11369 11370          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11370 11371              o2->ipv6IfIcmpInGroupMembReductions);
11371 11372          UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11372 11373          UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11373 11374          UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11374 11375              o2->ipv6IfIcmpOutDestUnreachs);
11375 11376          UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11376 11377              o2->ipv6IfIcmpOutAdminProhibs);
11377 11378          UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11378 11379          UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11379 11380              o2->ipv6IfIcmpOutParmProblems);
11380 11381          UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11381 11382          UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11382 11383          UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11383 11384          UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11384 11385              o2->ipv6IfIcmpOutRouterSolicits);
11385 11386          UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11386 11387              o2->ipv6IfIcmpOutRouterAdvertisements);
11387 11388          UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11388 11389              o2->ipv6IfIcmpOutNeighborSolicits);
11389 11390          UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11390 11391              o2->ipv6IfIcmpOutNeighborAdvertisements);
11391 11392          UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11392 11393          UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11393 11394              o2->ipv6IfIcmpOutGroupMembQueries);
11394 11395          UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11395 11396              o2->ipv6IfIcmpOutGroupMembResponses);
11396 11397          UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11397 11398              o2->ipv6IfIcmpOutGroupMembReductions);
11398 11399          UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11399 11400          UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11400 11401          UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11401 11402              o2->ipv6IfIcmpInBadNeighborAdvertisements);
11402 11403          UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11403 11404              o2->ipv6IfIcmpInBadNeighborSolicitations);
11404 11405          UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11405 11406          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11406 11407              o2->ipv6IfIcmpInGroupMembTotal);
11407 11408          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11408 11409              o2->ipv6IfIcmpInGroupMembBadQueries);
11409 11410          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11410 11411              o2->ipv6IfIcmpInGroupMembBadReports);
11411 11412          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11412 11413              o2->ipv6IfIcmpInGroupMembOurReports);
11413 11414  }
11414 11415  
11415 11416  /*
11416 11417   * Called before the options are updated to check if this packet will
11417 11418   * be source routed from here.
11418 11419   * This routine assumes that the options are well formed i.e. that they
11419 11420   * have already been checked.
11420 11421   */
11421 11422  boolean_t
11422 11423  ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11423 11424  {
11424 11425          ipoptp_t        opts;
11425 11426          uchar_t         *opt;
11426 11427          uint8_t         optval;
11427 11428          uint8_t         optlen;
11428 11429          ipaddr_t        dst;
11429 11430  
11430 11431          if (IS_SIMPLE_IPH(ipha)) {
11431 11432                  ip2dbg(("not source routed\n"));
11432 11433                  return (B_FALSE);
11433 11434          }
11434 11435          dst = ipha->ipha_dst;
11435 11436          for (optval = ipoptp_first(&opts, ipha);
11436 11437              optval != IPOPT_EOL;
11437 11438              optval = ipoptp_next(&opts)) {
11438 11439                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11439 11440                  opt = opts.ipoptp_cur;
11440 11441                  optlen = opts.ipoptp_len;
11441 11442                  ip2dbg(("ip_source_routed: opt %d, len %d\n",
11442 11443                      optval, optlen));
11443 11444                  switch (optval) {
11444 11445                          uint32_t off;
11445 11446                  case IPOPT_SSRR:
11446 11447                  case IPOPT_LSRR:
11447 11448                          /*
11448 11449                           * If dst is one of our addresses and there are some
11449 11450                           * entries left in the source route return (true).
11450 11451                           */
11451 11452                          if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11452 11453                                  ip2dbg(("ip_source_routed: not next"
11453 11454                                      " source route 0x%x\n",
11454 11455                                      ntohl(dst)));
11455 11456                                  return (B_FALSE);
11456 11457                          }
11457 11458                          off = opt[IPOPT_OFFSET];
11458 11459                          off--;
11459 11460                          if (optlen < IP_ADDR_LEN ||
11460 11461                              off > optlen - IP_ADDR_LEN) {
11461 11462                                  /* End of source route */
11462 11463                                  ip1dbg(("ip_source_routed: end of SR\n"));
11463 11464                                  return (B_FALSE);
11464 11465                          }
11465 11466                          return (B_TRUE);
11466 11467                  }
11467 11468          }
11468 11469          ip2dbg(("not source routed\n"));
11469 11470          return (B_FALSE);
11470 11471  }
11471 11472  
11472 11473  /*
11473 11474   * ip_unbind is called by the transports to remove a conn from
11474 11475   * the fanout table.
11475 11476   */
11476 11477  void
11477 11478  ip_unbind(conn_t *connp)
11478 11479  {
11479 11480  
11480 11481          ASSERT(!MUTEX_HELD(&connp->conn_lock));
11481 11482  
11482 11483          if (is_system_labeled() && connp->conn_anon_port) {
11483 11484                  (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
11484 11485                      connp->conn_mlp_type, connp->conn_proto,
11485 11486                      ntohs(connp->conn_lport), B_FALSE);
11486 11487                  connp->conn_anon_port = 0;
11487 11488          }
11488 11489          connp->conn_mlp_type = mlptSingle;
11489 11490  
11490 11491          ipcl_hash_remove(connp);
11491 11492  }
11492 11493  
11493 11494  /*
11494 11495   * Used for deciding the MSS size for the upper layer. Thus
11495 11496   * we need to check the outbound policy values in the conn.
11496 11497   */
11497 11498  int
11498 11499  conn_ipsec_length(conn_t *connp)
11499 11500  {
11500 11501          ipsec_latch_t *ipl;
11501 11502  
11502 11503          ipl = connp->conn_latch;
11503 11504          if (ipl == NULL)
11504 11505                  return (0);
11505 11506  
11506 11507          if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11507 11508                  return (0);
11508 11509  
11509 11510          return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11510 11511  }
11511 11512  
11512 11513  /*
11513 11514   * Returns an estimate of the IPsec headers size. This is used if
11514 11515   * we don't want to call into IPsec to get the exact size.
11515 11516   */
11516 11517  int
11517 11518  ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11518 11519  {
11519 11520          ipsec_action_t *a;
11520 11521  
11521 11522          if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11522 11523                  return (0);
11523 11524  
11524 11525          a = ixa->ixa_ipsec_action;
11525 11526          if (a == NULL) {
11526 11527                  ASSERT(ixa->ixa_ipsec_policy != NULL);
11527 11528                  a = ixa->ixa_ipsec_policy->ipsp_act;
11528 11529          }
11529 11530          ASSERT(a != NULL);
11530 11531  
11531 11532          return (a->ipa_ovhd);
11532 11533  }
11533 11534  
11534 11535  /*
11535 11536   * If there are any source route options, return the true final
11536 11537   * destination. Otherwise, return the destination.
11537 11538   */
11538 11539  ipaddr_t
11539 11540  ip_get_dst(ipha_t *ipha)
11540 11541  {
11541 11542          ipoptp_t        opts;
11542 11543          uchar_t         *opt;
11543 11544          uint8_t         optval;
11544 11545          uint8_t         optlen;
11545 11546          ipaddr_t        dst;
11546 11547          uint32_t off;
11547 11548  
11548 11549          dst = ipha->ipha_dst;
11549 11550  
11550 11551          if (IS_SIMPLE_IPH(ipha))
11551 11552                  return (dst);
11552 11553  
11553 11554          for (optval = ipoptp_first(&opts, ipha);
11554 11555              optval != IPOPT_EOL;
11555 11556              optval = ipoptp_next(&opts)) {
11556 11557                  opt = opts.ipoptp_cur;
11557 11558                  optlen = opts.ipoptp_len;
11558 11559                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11559 11560                  switch (optval) {
11560 11561                  case IPOPT_SSRR:
11561 11562                  case IPOPT_LSRR:
11562 11563                          off = opt[IPOPT_OFFSET];
11563 11564                          /*
11564 11565                           * If one of the conditions is true, it means
11565 11566                           * end of options and dst already has the right
11566 11567                           * value.
11567 11568                           */
11568 11569                          if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11569 11570                                  off = optlen - IP_ADDR_LEN;
11570 11571                                  bcopy(&opt[off], &dst, IP_ADDR_LEN);
11571 11572                          }
11572 11573                          return (dst);
11573 11574                  default:
11574 11575                          break;
11575 11576                  }
11576 11577          }
11577 11578  
11578 11579          return (dst);
11579 11580  }
11580 11581  
11581 11582  /*
11582 11583   * Outbound IP fragmentation routine.
11583 11584   * Assumes the caller has checked whether or not fragmentation should
11584 11585   * be allowed. Here we copy the DF bit from the header to all the generated
11585 11586   * fragments.
11586 11587   */
11587 11588  int
11588 11589  ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11589 11590      uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11590 11591      zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11591 11592  {
11592 11593          int             i1;
11593 11594          int             hdr_len;
11594 11595          mblk_t          *hdr_mp;
11595 11596          ipha_t          *ipha;
11596 11597          int             ip_data_end;
11597 11598          int             len;
11598 11599          mblk_t          *mp = mp_orig;
11599 11600          int             offset;
11600 11601          ill_t           *ill = nce->nce_ill;
11601 11602          ip_stack_t      *ipst = ill->ill_ipst;
11602 11603          mblk_t          *carve_mp;
11603 11604          uint32_t        frag_flag;
11604 11605          uint_t          priority = mp->b_band;
11605 11606          int             error = 0;
11606 11607  
11607 11608          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11608 11609  
11609 11610          if (pkt_len != msgdsize(mp)) {
11610 11611                  ip0dbg(("Packet length mismatch: %d, %ld\n",
11611 11612                      pkt_len, msgdsize(mp)));
11612 11613                  freemsg(mp);
11613 11614                  return (EINVAL);
11614 11615          }
11615 11616  
11616 11617          if (max_frag == 0) {
11617 11618                  ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11618 11619                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11619 11620                  ip_drop_output("FragFails: zero max_frag", mp, ill);
11620 11621                  freemsg(mp);
11621 11622                  return (EINVAL);
11622 11623          }
11623 11624  
11624 11625          ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11625 11626          ipha = (ipha_t *)mp->b_rptr;
11626 11627          ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11627 11628          frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11628 11629  
11629 11630          /*
11630 11631           * Establish the starting offset.  May not be zero if we are fragging
11631 11632           * a fragment that is being forwarded.
11632 11633           */
11633 11634          offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11634 11635  
11635 11636          /* TODO why is this test needed? */
11636 11637          if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11637 11638                  /* TODO: notify ulp somehow */
11638 11639                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11639 11640                  ip_drop_output("FragFails: bad starting offset", mp, ill);
11640 11641                  freemsg(mp);
11641 11642                  return (EINVAL);
11642 11643          }
11643 11644  
11644 11645          hdr_len = IPH_HDR_LENGTH(ipha);
11645 11646          ipha->ipha_hdr_checksum = 0;
11646 11647  
11647 11648          /*
11648 11649           * Establish the number of bytes maximum per frag, after putting
11649 11650           * in the header.
11650 11651           */
11651 11652          len = (max_frag - hdr_len) & ~7;
11652 11653  
11653 11654          /* Get a copy of the header for the trailing frags */
11654 11655          hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11655 11656              mp);
11656 11657          if (hdr_mp == NULL) {
11657 11658                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11658 11659                  ip_drop_output("FragFails: no hdr_mp", mp, ill);
11659 11660                  freemsg(mp);
11660 11661                  return (ENOBUFS);
11661 11662          }
11662 11663  
11663 11664          /* Store the starting offset, with the MoreFrags flag. */
11664 11665          i1 = offset | IPH_MF | frag_flag;
11665 11666          ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11666 11667  
11667 11668          /* Establish the ending byte offset, based on the starting offset. */
11668 11669          offset <<= 3;
11669 11670          ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11670 11671  
11671 11672          /* Store the length of the first fragment in the IP header. */
11672 11673          i1 = len + hdr_len;
11673 11674          ASSERT(i1 <= IP_MAXPACKET);
11674 11675          ipha->ipha_length = htons((uint16_t)i1);
11675 11676  
11676 11677          /*
11677 11678           * Compute the IP header checksum for the first frag.  We have to
11678 11679           * watch out that we stop at the end of the header.
11679 11680           */
11680 11681          ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11681 11682  
11682 11683          /*
11683 11684           * Now carve off the first frag.  Note that this will include the
11684 11685           * original IP header.
11685 11686           */
11686 11687          if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11687 11688                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11688 11689                  ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11689 11690                  freeb(hdr_mp);
11690 11691                  freemsg(mp_orig);
11691 11692                  return (ENOBUFS);
11692 11693          }
11693 11694  
11694 11695          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11695 11696  
11696 11697          error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11697 11698              ixa_cookie);
11698 11699          if (error != 0 && error != EWOULDBLOCK) {
11699 11700                  /* No point in sending the other fragments */
11700 11701                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11701 11702                  ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11702 11703                  freeb(hdr_mp);
11703 11704                  freemsg(mp_orig);
11704 11705                  return (error);
11705 11706          }
11706 11707  
11707 11708          /* No need to redo state machine in loop */
11708 11709          ixaflags &= ~IXAF_REACH_CONF;
11709 11710  
11710 11711          /* Advance the offset to the second frag starting point. */
11711 11712          offset += len;
11712 11713          /*
11713 11714           * Update hdr_len from the copied header - there might be less options
11714 11715           * in the later fragments.
11715 11716           */
11716 11717          hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11717 11718          /* Loop until done. */
11718 11719          for (;;) {
11719 11720                  uint16_t        offset_and_flags;
11720 11721                  uint16_t        ip_len;
11721 11722  
11722 11723                  if (ip_data_end - offset > len) {
11723 11724                          /*
11724 11725                           * Carve off the appropriate amount from the original
11725 11726                           * datagram.
11726 11727                           */
11727 11728                          if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11728 11729                                  mp = NULL;
11729 11730                                  break;
11730 11731                          }
11731 11732                          /*
11732 11733                           * More frags after this one.  Get another copy
11733 11734                           * of the header.
11734 11735                           */
11735 11736                          if (carve_mp->b_datap->db_ref == 1 &&
11736 11737                              hdr_mp->b_wptr - hdr_mp->b_rptr <
11737 11738                              carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11738 11739                                  /* Inline IP header */
11739 11740                                  carve_mp->b_rptr -= hdr_mp->b_wptr -
11740 11741                                      hdr_mp->b_rptr;
11741 11742                                  bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11742 11743                                      hdr_mp->b_wptr - hdr_mp->b_rptr);
11743 11744                                  mp = carve_mp;
11744 11745                          } else {
11745 11746                                  if (!(mp = copyb(hdr_mp))) {
11746 11747                                          freemsg(carve_mp);
11747 11748                                          break;
11748 11749                                  }
11749 11750                                  /* Get priority marking, if any. */
11750 11751                                  mp->b_band = priority;
11751 11752                                  mp->b_cont = carve_mp;
11752 11753                          }
11753 11754                          ipha = (ipha_t *)mp->b_rptr;
11754 11755                          offset_and_flags = IPH_MF;
11755 11756                  } else {
11756 11757                          /*
11757 11758                           * Last frag.  Consume the header. Set len to
11758 11759                           * the length of this last piece.
11759 11760                           */
11760 11761                          len = ip_data_end - offset;
11761 11762  
11762 11763                          /*
11763 11764                           * Carve off the appropriate amount from the original
11764 11765                           * datagram.
11765 11766                           */
11766 11767                          if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11767 11768                                  mp = NULL;
11768 11769                                  break;
11769 11770                          }
11770 11771                          if (carve_mp->b_datap->db_ref == 1 &&
11771 11772                              hdr_mp->b_wptr - hdr_mp->b_rptr <
11772 11773                              carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11773 11774                                  /* Inline IP header */
11774 11775                                  carve_mp->b_rptr -= hdr_mp->b_wptr -
11775 11776                                      hdr_mp->b_rptr;
11776 11777                                  bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11777 11778                                      hdr_mp->b_wptr - hdr_mp->b_rptr);
11778 11779                                  mp = carve_mp;
11779 11780                                  freeb(hdr_mp);
11780 11781                                  hdr_mp = mp;
11781 11782                          } else {
11782 11783                                  mp = hdr_mp;
11783 11784                                  /* Get priority marking, if any. */
11784 11785                                  mp->b_band = priority;
11785 11786                                  mp->b_cont = carve_mp;
11786 11787                          }
11787 11788                          ipha = (ipha_t *)mp->b_rptr;
11788 11789                          /* A frag of a frag might have IPH_MF non-zero */
11789 11790                          offset_and_flags =
11790 11791                              ntohs(ipha->ipha_fragment_offset_and_flags) &
11791 11792                              IPH_MF;
11792 11793                  }
11793 11794                  offset_and_flags |= (uint16_t)(offset >> 3);
11794 11795                  offset_and_flags |= (uint16_t)frag_flag;
11795 11796                  /* Store the offset and flags in the IP header. */
11796 11797                  ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11797 11798  
11798 11799                  /* Store the length in the IP header. */
11799 11800                  ip_len = (uint16_t)(len + hdr_len);
11800 11801                  ipha->ipha_length = htons(ip_len);
11801 11802  
11802 11803                  /*
11803 11804                   * Set the IP header checksum.  Note that mp is just
11804 11805                   * the header, so this is easy to pass to ip_csum.
11805 11806                   */
11806 11807                  ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11807 11808  
11808 11809                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11809 11810  
11810 11811                  error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11811 11812                      nolzid, ixa_cookie);
11812 11813                  /* All done if we just consumed the hdr_mp. */
11813 11814                  if (mp == hdr_mp) {
11814 11815                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11815 11816                          return (error);
11816 11817                  }
11817 11818                  if (error != 0 && error != EWOULDBLOCK) {
11818 11819                          DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11819 11820                              mblk_t *, hdr_mp);
11820 11821                          /* No point in sending the other fragments */
11821 11822                          break;
11822 11823                  }
11823 11824  
11824 11825                  /* Otherwise, advance and loop. */
11825 11826                  offset += len;
11826 11827          }
11827 11828          /* Clean up following allocation failure. */
11828 11829          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11829 11830          ip_drop_output("FragFails: loop ended", NULL, ill);
11830 11831          if (mp != hdr_mp)
11831 11832                  freeb(hdr_mp);
11832 11833          if (mp != mp_orig)
11833 11834                  freemsg(mp_orig);
11834 11835          return (error);
11835 11836  }
11836 11837  
11837 11838  /*
11838 11839   * Copy the header plus those options which have the copy bit set
11839 11840   */
11840 11841  static mblk_t *
11841 11842  ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11842 11843      mblk_t *src)
11843 11844  {
11844 11845          mblk_t  *mp;
11845 11846          uchar_t *up;
11846 11847  
11847 11848          /*
11848 11849           * Quick check if we need to look for options without the copy bit
11849 11850           * set
11850 11851           */
11851 11852          mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11852 11853          if (!mp)
11853 11854                  return (mp);
11854 11855          mp->b_rptr += ipst->ips_ip_wroff_extra;
11855 11856          if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11856 11857                  bcopy(rptr, mp->b_rptr, hdr_len);
11857 11858                  mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11858 11859                  return (mp);
11859 11860          }
11860 11861          up  = mp->b_rptr;
11861 11862          bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11862 11863          up += IP_SIMPLE_HDR_LENGTH;
11863 11864          rptr += IP_SIMPLE_HDR_LENGTH;
11864 11865          hdr_len -= IP_SIMPLE_HDR_LENGTH;
11865 11866          while (hdr_len > 0) {
11866 11867                  uint32_t optval;
11867 11868                  uint32_t optlen;
11868 11869  
11869 11870                  optval = *rptr;
11870 11871                  if (optval == IPOPT_EOL)
11871 11872                          break;
11872 11873                  if (optval == IPOPT_NOP)
11873 11874                          optlen = 1;
11874 11875                  else
11875 11876                          optlen = rptr[1];
11876 11877                  if (optval & IPOPT_COPY) {
11877 11878                          bcopy(rptr, up, optlen);
11878 11879                          up += optlen;
11879 11880                  }
11880 11881                  rptr += optlen;
11881 11882                  hdr_len -= optlen;
11882 11883          }
11883 11884          /*
11884 11885           * Make sure that we drop an even number of words by filling
11885 11886           * with EOL to the next word boundary.
11886 11887           */
11887 11888          for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11888 11889              hdr_len & 0x3; hdr_len++)
11889 11890                  *up++ = IPOPT_EOL;
11890 11891          mp->b_wptr = up;
11891 11892          /* Update header length */
11892 11893          mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11893 11894          return (mp);
11894 11895  }
11895 11896  
11896 11897  /*
11897 11898   * Update any source route, record route, or timestamp options when
11898 11899   * sending a packet back to ourselves.
11899 11900   * Check that we are at end of strict source route.
11900 11901   * The options have been sanity checked by ip_output_options().
11901 11902   */
11902 11903  void
11903 11904  ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11904 11905  {
11905 11906          ipoptp_t        opts;
11906 11907          uchar_t         *opt;
11907 11908          uint8_t         optval;
11908 11909          uint8_t         optlen;
11909 11910          ipaddr_t        dst;
11910 11911          uint32_t        ts;
11911 11912          timestruc_t     now;
11912 11913  
11913 11914          for (optval = ipoptp_first(&opts, ipha);
11914 11915              optval != IPOPT_EOL;
11915 11916              optval = ipoptp_next(&opts)) {
11916 11917                  opt = opts.ipoptp_cur;
11917 11918                  optlen = opts.ipoptp_len;
11918 11919                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11919 11920                  switch (optval) {
11920 11921                          uint32_t off;
11921 11922                  case IPOPT_SSRR:
11922 11923                  case IPOPT_LSRR:
11923 11924                          off = opt[IPOPT_OFFSET];
11924 11925                          off--;
11925 11926                          if (optlen < IP_ADDR_LEN ||
11926 11927                              off > optlen - IP_ADDR_LEN) {
11927 11928                                  /* End of source route */
11928 11929                                  break;
11929 11930                          }
11930 11931                          /*
11931 11932                           * This will only happen if two consecutive entries
11932 11933                           * in the source route contains our address or if
11933 11934                           * it is a packet with a loose source route which
11934 11935                           * reaches us before consuming the whole source route
11935 11936                           */
11936 11937  
11937 11938                          if (optval == IPOPT_SSRR) {
11938 11939                                  return;
11939 11940                          }
11940 11941                          /*
11941 11942                           * Hack: instead of dropping the packet truncate the
11942 11943                           * source route to what has been used by filling the
11943 11944                           * rest with IPOPT_NOP.
11944 11945                           */
11945 11946                          opt[IPOPT_OLEN] = (uint8_t)off;
11946 11947                          while (off < optlen) {
11947 11948                                  opt[off++] = IPOPT_NOP;
11948 11949                          }
11949 11950                          break;
11950 11951                  case IPOPT_RR:
11951 11952                          off = opt[IPOPT_OFFSET];
11952 11953                          off--;
11953 11954                          if (optlen < IP_ADDR_LEN ||
11954 11955                              off > optlen - IP_ADDR_LEN) {
11955 11956                                  /* No more room - ignore */
11956 11957                                  ip1dbg((
11957 11958                                      "ip_output_local_options: end of RR\n"));
11958 11959                                  break;
11959 11960                          }
11960 11961                          dst = htonl(INADDR_LOOPBACK);
11961 11962                          bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11962 11963                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11963 11964                          break;
11964 11965                  case IPOPT_TS:
11965 11966                          /* Insert timestamp if there is romm */
11966 11967                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11967 11968                          case IPOPT_TS_TSONLY:
11968 11969                                  off = IPOPT_TS_TIMELEN;
11969 11970                                  break;
11970 11971                          case IPOPT_TS_PRESPEC:
11971 11972                          case IPOPT_TS_PRESPEC_RFC791:
11972 11973                                  /* Verify that the address matched */
11973 11974                                  off = opt[IPOPT_OFFSET] - 1;
11974 11975                                  bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
11975 11976                                  if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11976 11977                                          /* Not for us */
11977 11978                                          break;
11978 11979                                  }
11979 11980                                  /* FALLTHRU */
11980 11981                          case IPOPT_TS_TSANDADDR:
11981 11982                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
11982 11983                                  break;
11983 11984                          default:
11984 11985                                  /*
11985 11986                                   * ip_*put_options should have already
11986 11987                                   * dropped this packet.
11987 11988                                   */
11988 11989                                  cmn_err(CE_PANIC, "ip_output_local_options: "
11989 11990                                      "unknown IT - bug in ip_output_options?\n");
11990 11991                                  return; /* Keep "lint" happy */
11991 11992                          }
11992 11993                          if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
11993 11994                                  /* Increase overflow counter */
11994 11995                                  off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
11995 11996                                  opt[IPOPT_POS_OV_FLG] = (uint8_t)
11996 11997                                      (opt[IPOPT_POS_OV_FLG] & 0x0F) |
11997 11998                                      (off << 4);
11998 11999                                  break;
11999 12000                          }
12000 12001                          off = opt[IPOPT_OFFSET] - 1;
12001 12002                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12002 12003                          case IPOPT_TS_PRESPEC:
12003 12004                          case IPOPT_TS_PRESPEC_RFC791:
12004 12005                          case IPOPT_TS_TSANDADDR:
12005 12006                                  dst = htonl(INADDR_LOOPBACK);
12006 12007                                  bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12007 12008                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12008 12009                                  /* FALLTHRU */
12009 12010                          case IPOPT_TS_TSONLY:
12010 12011                                  off = opt[IPOPT_OFFSET] - 1;
12011 12012                                  /* Compute # of milliseconds since midnight */
12012 12013                                  gethrestime(&now);
12013 12014                                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
12014 12015                                      now.tv_nsec / (NANOSEC / MILLISEC);
12015 12016                                  bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
12016 12017                                  opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
12017 12018                                  break;
12018 12019                          }
12019 12020                          break;
12020 12021                  }
12021 12022          }
12022 12023  }
12023 12024  
12024 12025  /*
12025 12026   * Prepend an M_DATA fastpath header, and if none present prepend a
12026 12027   * DL_UNITDATA_REQ. Frees the mblk on failure.
12027 12028   *
12028 12029   * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
12029 12030   * If there is a change to them, the nce will be deleted (condemned) and
12030 12031   * a new nce_t will be created when packets are sent. Thus we need no locks
12031 12032   * to access those fields.
12032 12033   *
12033 12034   * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
12034 12035   * we place b_band in dl_priority.dl_max.
12035 12036   */
12036 12037  static mblk_t *
12037 12038  ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
12038 12039  {
12039 12040          uint_t  hlen;
12040 12041          mblk_t *mp1;
12041 12042          uint_t  priority;
12042 12043          uchar_t *rptr;
12043 12044  
12044 12045          rptr = mp->b_rptr;
12045 12046  
12046 12047          ASSERT(DB_TYPE(mp) == M_DATA);
12047 12048          priority = mp->b_band;
12048 12049  
12049 12050          ASSERT(nce != NULL);
12050 12051          if ((mp1 = nce->nce_fp_mp) != NULL) {
12051 12052                  hlen = MBLKL(mp1);
12052 12053                  /*
12053 12054                   * Check if we have enough room to prepend fastpath
12054 12055                   * header
12055 12056                   */
12056 12057                  if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
12057 12058                          rptr -= hlen;
12058 12059                          bcopy(mp1->b_rptr, rptr, hlen);
12059 12060                          /*
12060 12061                           * Set the b_rptr to the start of the link layer
12061 12062                           * header
12062 12063                           */
12063 12064                          mp->b_rptr = rptr;
12064 12065                          return (mp);
12065 12066                  }
12066 12067                  mp1 = copyb(mp1);
12067 12068                  if (mp1 == NULL) {
12068 12069                          ill_t *ill = nce->nce_ill;
12069 12070  
12070 12071                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12071 12072                          ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12072 12073                          freemsg(mp);
12073 12074                          return (NULL);
12074 12075                  }
12075 12076                  mp1->b_band = priority;
12076 12077                  mp1->b_cont = mp;
12077 12078                  DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
12078 12079                  DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
12079 12080                  DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
12080 12081                  DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
12081 12082                  DB_LSOMSS(mp1) = DB_LSOMSS(mp);
12082 12083                  DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
12083 12084                  /*
12084 12085                   * XXX disable ICK_VALID and compute checksum
12085 12086                   * here; can happen if nce_fp_mp changes and
12086 12087                   * it can't be copied now due to insufficient
12087 12088                   * space. (unlikely, fp mp can change, but it
12088 12089                   * does not increase in length)
12089 12090                   */
12090 12091                  return (mp1);
12091 12092          }
12092 12093          mp1 = copyb(nce->nce_dlur_mp);
12093 12094  
12094 12095          if (mp1 == NULL) {
12095 12096                  ill_t *ill = nce->nce_ill;
12096 12097  
12097 12098                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12098 12099                  ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12099 12100                  freemsg(mp);
12100 12101                  return (NULL);
12101 12102          }
12102 12103          mp1->b_cont = mp;
12103 12104          if (priority != 0) {
12104 12105                  mp1->b_band = priority;
12105 12106                  ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
12106 12107                      priority;
12107 12108          }
12108 12109          return (mp1);
12109 12110  #undef rptr
12110 12111  }
12111 12112  
12112 12113  /*
12113 12114   * Finish the outbound IPsec processing. This function is called from
12114 12115   * ipsec_out_process() if the IPsec packet was processed
12115 12116   * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12116 12117   * asynchronously.
12117 12118   *
12118 12119   * This is common to IPv4 and IPv6.
12119 12120   */
12120 12121  int
12121 12122  ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
12122 12123  {
12123 12124          iaflags_t       ixaflags = ixa->ixa_flags;
12124 12125          uint_t          pktlen;
12125 12126  
12126 12127  
12127 12128          /* AH/ESP don't update ixa_pktlen when they modify the packet */
12128 12129          if (ixaflags & IXAF_IS_IPV4) {
12129 12130                  ipha_t          *ipha = (ipha_t *)mp->b_rptr;
12130 12131  
12131 12132                  ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12132 12133                  pktlen = ntohs(ipha->ipha_length);
12133 12134          } else {
12134 12135                  ip6_t           *ip6h = (ip6_t *)mp->b_rptr;
12135 12136  
12136 12137                  ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12137 12138                  pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12138 12139          }
12139 12140  
12140 12141          /*
12141 12142           * We release any hard reference on the SAs here to make
12142 12143           * sure the SAs can be garbage collected. ipsr_sa has a soft reference
12143 12144           * on the SAs.
12144 12145           * If in the future we want the hard latching of the SAs in the
12145 12146           * ip_xmit_attr_t then we should remove this.
12146 12147           */
12147 12148          if (ixa->ixa_ipsec_esp_sa != NULL) {
12148 12149                  IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12149 12150                  ixa->ixa_ipsec_esp_sa = NULL;
12150 12151          }
12151 12152          if (ixa->ixa_ipsec_ah_sa != NULL) {
12152 12153                  IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12153 12154                  ixa->ixa_ipsec_ah_sa = NULL;
12154 12155          }
12155 12156  
12156 12157          /* Do we need to fragment? */
12157 12158          if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
12158 12159              pktlen > ixa->ixa_fragsize) {
12159 12160                  if (ixaflags & IXAF_IS_IPV4) {
12160 12161                          ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
12161 12162                          /*
12162 12163                           * We check for the DF case in ipsec_out_process
12163 12164                           * hence this only handles the non-DF case.
12164 12165                           */
12165 12166                          return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
12166 12167                              pktlen, ixa->ixa_fragsize,
12167 12168                              ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12168 12169                              ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
12169 12170                              &ixa->ixa_cookie));
12170 12171                  } else {
12171 12172                          mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
12172 12173                          if (mp == NULL) {
12173 12174                                  /* MIB and ip_drop_output already done */
12174 12175                                  return (ENOMEM);
12175 12176                          }
12176 12177                          pktlen += sizeof (ip6_frag_t);
12177 12178                          if (pktlen > ixa->ixa_fragsize) {
12178 12179                                  return (ip_fragment_v6(mp, ixa->ixa_nce,
12179 12180                                      ixa->ixa_flags, pktlen,
12180 12181                                      ixa->ixa_fragsize, ixa->ixa_xmit_hint,
12181 12182                                      ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
12182 12183                                      ixa->ixa_postfragfn, &ixa->ixa_cookie));
12183 12184                          }
12184 12185                  }
12185 12186          }
12186 12187          return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
12187 12188              pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12188 12189              ixa->ixa_no_loop_zoneid, NULL));
12189 12190  }
12190 12191  
12191 12192  /*
12192 12193   * Finish the inbound IPsec processing. This function is called from
12193 12194   * ipsec_out_process() if the IPsec packet was processed
12194 12195   * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12195 12196   * asynchronously.
12196 12197   *
12197 12198   * This is common to IPv4 and IPv6.
12198 12199   */
12199 12200  void
12200 12201  ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
12201 12202  {
12202 12203          iaflags_t       iraflags = ira->ira_flags;
12203 12204  
12204 12205          /* Length might have changed */
12205 12206          if (iraflags & IRAF_IS_IPV4) {
12206 12207                  ipha_t          *ipha = (ipha_t *)mp->b_rptr;
12207 12208  
12208 12209                  ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12209 12210                  ira->ira_pktlen = ntohs(ipha->ipha_length);
12210 12211                  ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
12211 12212                  ira->ira_protocol = ipha->ipha_protocol;
12212 12213  
12213 12214                  ip_fanout_v4(mp, ipha, ira);
12214 12215          } else {
12215 12216                  ip6_t           *ip6h = (ip6_t *)mp->b_rptr;
12216 12217                  uint8_t         *nexthdrp;
12217 12218  
12218 12219                  ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12219 12220                  ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12220 12221                  if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
12221 12222                      &nexthdrp)) {
12222 12223                          /* Malformed packet */
12223 12224                          BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
12224 12225                          ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
12225 12226                          freemsg(mp);
12226 12227                          return;
12227 12228                  }
12228 12229                  ira->ira_protocol = *nexthdrp;
12229 12230                  ip_fanout_v6(mp, ip6h, ira);
12230 12231          }
12231 12232  }
12232 12233  
12233 12234  /*
12234 12235   * Select which AH & ESP SA's to use (if any) for the outbound packet.
12235 12236   *
12236 12237   * If this function returns B_TRUE, the requested SA's have been filled
12237 12238   * into the ixa_ipsec_*_sa pointers.
12238 12239   *
12239 12240   * If the function returns B_FALSE, the packet has been "consumed", most
12240 12241   * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
12241 12242   *
12242 12243   * The SA references created by the protocol-specific "select"
12243 12244   * function will be released in ip_output_post_ipsec.
12244 12245   */
12245 12246  static boolean_t
12246 12247  ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
12247 12248  {
12248 12249          boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
12249 12250          ipsec_policy_t *pp;
12250 12251          ipsec_action_t *ap;
12251 12252  
12252 12253          ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12253 12254          ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12254 12255              (ixa->ixa_ipsec_action != NULL));
12255 12256  
12256 12257          ap = ixa->ixa_ipsec_action;
12257 12258          if (ap == NULL) {
12258 12259                  pp = ixa->ixa_ipsec_policy;
12259 12260                  ASSERT(pp != NULL);
12260 12261                  ap = pp->ipsp_act;
12261 12262                  ASSERT(ap != NULL);
12262 12263          }
12263 12264  
12264 12265          /*
12265 12266           * We have an action.  now, let's select SA's.
12266 12267           * A side effect of setting ixa_ipsec_*_sa is that it will
12267 12268           * be cached in the conn_t.
12268 12269           */
12269 12270          if (ap->ipa_want_esp) {
12270 12271                  if (ixa->ixa_ipsec_esp_sa == NULL) {
12271 12272                          need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
12272 12273                              IPPROTO_ESP);
12273 12274                  }
12274 12275                  ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
12275 12276          }
12276 12277  
12277 12278          if (ap->ipa_want_ah) {
12278 12279                  if (ixa->ixa_ipsec_ah_sa == NULL) {
12279 12280                          need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
12280 12281                              IPPROTO_AH);
12281 12282                  }
12282 12283                  ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
12283 12284                  /*
12284 12285                   * The ESP and AH processing order needs to be preserved
12285 12286                   * when both protocols are required (ESP should be applied
12286 12287                   * before AH for an outbound packet). Force an ESP ACQUIRE
12287 12288                   * when both ESP and AH are required, and an AH ACQUIRE
12288 12289                   * is needed.
12289 12290                   */
12290 12291                  if (ap->ipa_want_esp && need_ah_acquire)
12291 12292                          need_esp_acquire = B_TRUE;
12292 12293          }
12293 12294  
12294 12295          /*
12295 12296           * Send an ACQUIRE (extended, regular, or both) if we need one.
12296 12297           * Release SAs that got referenced, but will not be used until we
12297 12298           * acquire _all_ of the SAs we need.
12298 12299           */
12299 12300          if (need_ah_acquire || need_esp_acquire) {
12300 12301                  if (ixa->ixa_ipsec_ah_sa != NULL) {
12301 12302                          IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12302 12303                          ixa->ixa_ipsec_ah_sa = NULL;
12303 12304                  }
12304 12305                  if (ixa->ixa_ipsec_esp_sa != NULL) {
12305 12306                          IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12306 12307                          ixa->ixa_ipsec_esp_sa = NULL;
12307 12308                  }
12308 12309  
12309 12310                  sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
12310 12311                  return (B_FALSE);
12311 12312          }
12312 12313  
12313 12314          return (B_TRUE);
12314 12315  }
12315 12316  
12316 12317  /*
12317 12318   * Handle IPsec output processing.
12318 12319   * This function is only entered once for a given packet.
12319 12320   * We try to do things synchronously, but if we need to have user-level
12320 12321   * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
12321 12322   * will be completed
12322 12323   *  - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
12323 12324   *  - when asynchronous ESP is done it will do AH
12324 12325   *
12325 12326   * In all cases we come back in ip_output_post_ipsec() to fragment and
12326 12327   * send out the packet.
12327 12328   */
12328 12329  int
12329 12330  ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
12330 12331  {
12331 12332          ill_t           *ill = ixa->ixa_nce->nce_ill;
12332 12333          ip_stack_t      *ipst = ixa->ixa_ipst;
12333 12334          ipsec_stack_t   *ipss;
12334 12335          ipsec_policy_t  *pp;
12335 12336          ipsec_action_t  *ap;
12336 12337  
12337 12338          ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12338 12339  
12339 12340          ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12340 12341              (ixa->ixa_ipsec_action != NULL));
12341 12342  
12342 12343          ipss = ipst->ips_netstack->netstack_ipsec;
12343 12344          if (!ipsec_loaded(ipss)) {
12344 12345                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12345 12346                  ip_drop_packet(mp, B_TRUE, ill,
12346 12347                      DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12347 12348                      &ipss->ipsec_dropper);
12348 12349                  return (ENOTSUP);
12349 12350          }
12350 12351  
12351 12352          ap = ixa->ixa_ipsec_action;
12352 12353          if (ap == NULL) {
12353 12354                  pp = ixa->ixa_ipsec_policy;
12354 12355                  ASSERT(pp != NULL);
12355 12356                  ap = pp->ipsp_act;
12356 12357                  ASSERT(ap != NULL);
12357 12358          }
12358 12359  
12359 12360          /* Handle explicit drop action and bypass. */
12360 12361          switch (ap->ipa_act.ipa_type) {
12361 12362          case IPSEC_ACT_DISCARD:
12362 12363          case IPSEC_ACT_REJECT:
12363 12364                  ip_drop_packet(mp, B_FALSE, ill,
12364 12365                      DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12365 12366                  return (EHOSTUNREACH);  /* IPsec policy failure */
12366 12367          case IPSEC_ACT_BYPASS:
12367 12368                  return (ip_output_post_ipsec(mp, ixa));
12368 12369          }
12369 12370  
12370 12371          /*
12371 12372           * The order of processing is first insert a IP header if needed.
12372 12373           * Then insert the ESP header and then the AH header.
12373 12374           */
12374 12375          if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12375 12376                  /*
12376 12377                   * First get the outer IP header before sending
12377 12378                   * it to ESP.
12378 12379                   */
12379 12380                  ipha_t *oipha, *iipha;
12380 12381                  mblk_t *outer_mp, *inner_mp;
12381 12382  
12382 12383                  if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12383 12384                          (void) mi_strlog(ill->ill_rq, 0,
12384 12385                              SL_ERROR|SL_TRACE|SL_CONSOLE,
12385 12386                              "ipsec_out_process: "
12386 12387                              "Self-Encapsulation failed: Out of memory\n");
12387 12388                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12388 12389                          ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12389 12390                          freemsg(mp);
12390 12391                          return (ENOBUFS);
12391 12392                  }
12392 12393                  inner_mp = mp;
12393 12394                  ASSERT(inner_mp->b_datap->db_type == M_DATA);
12394 12395                  oipha = (ipha_t *)outer_mp->b_rptr;
12395 12396                  iipha = (ipha_t *)inner_mp->b_rptr;
12396 12397                  *oipha = *iipha;
12397 12398                  outer_mp->b_wptr += sizeof (ipha_t);
12398 12399                  oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12399 12400                      sizeof (ipha_t));
12400 12401                  oipha->ipha_protocol = IPPROTO_ENCAP;
12401 12402                  oipha->ipha_version_and_hdr_length =
12402 12403                      IP_SIMPLE_HDR_VERSION;
12403 12404                  oipha->ipha_hdr_checksum = 0;
12404 12405                  oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12405 12406                  outer_mp->b_cont = inner_mp;
12406 12407                  mp = outer_mp;
12407 12408  
12408 12409                  ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12409 12410          }
12410 12411  
12411 12412          /* If we need to wait for a SA then we can't return any errno */
12412 12413          if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12413 12414              (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12414 12415              !ipsec_out_select_sa(mp, ixa))
12415 12416                  return (0);
12416 12417  
12417 12418          /*
12418 12419           * By now, we know what SA's to use.  Toss over to ESP & AH
12419 12420           * to do the heavy lifting.
12420 12421           */
12421 12422          if (ap->ipa_want_esp) {
12422 12423                  ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12423 12424  
12424 12425                  mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12425 12426                  if (mp == NULL) {
12426 12427                          /*
12427 12428                           * Either it failed or is pending. In the former case
12428 12429                           * ipIfStatsInDiscards was increased.
12429 12430                           */
12430 12431                          return (0);
12431 12432                  }
12432 12433          }
12433 12434  
12434 12435          if (ap->ipa_want_ah) {
12435 12436                  ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12436 12437  
12437 12438                  mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12438 12439                  if (mp == NULL) {
12439 12440                          /*
12440 12441                           * Either it failed or is pending. In the former case
12441 12442                           * ipIfStatsInDiscards was increased.
12442 12443                           */
12443 12444                          return (0);
12444 12445                  }
12445 12446          }
12446 12447          /*
12447 12448           * We are done with IPsec processing. Send it over
12448 12449           * the wire.
12449 12450           */
12450 12451          return (ip_output_post_ipsec(mp, ixa));
12451 12452  }
12452 12453  
12453 12454  /*
12454 12455   * ioctls that go through a down/up sequence may need to wait for the down
12455 12456   * to complete. This involves waiting for the ire and ipif refcnts to go down
12456 12457   * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12457 12458   */
12458 12459  /* ARGSUSED */
12459 12460  void
12460 12461  ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12461 12462  {
12462 12463          struct iocblk *iocp;
12463 12464          mblk_t *mp1;
12464 12465          ip_ioctl_cmd_t *ipip;
12465 12466          int err;
12466 12467          sin_t   *sin;
12467 12468          struct lifreq *lifr;
12468 12469          struct ifreq *ifr;
12469 12470  
12470 12471          iocp = (struct iocblk *)mp->b_rptr;
12471 12472          ASSERT(ipsq != NULL);
12472 12473          /* Existence of mp1 verified in ip_wput_nondata */
12473 12474          mp1 = mp->b_cont->b_cont;
12474 12475          ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12475 12476          if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12476 12477                  /*
12477 12478                   * Special case where ipx_current_ipif is not set:
12478 12479                   * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12479 12480                   * We are here as were not able to complete the operation in
12480 12481                   * ipif_set_values because we could not become exclusive on
12481 12482                   * the new ipsq.
12482 12483                   */
12483 12484                  ill_t *ill = q->q_ptr;
12484 12485                  ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12485 12486          }
12486 12487          ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12487 12488  
12488 12489          if (ipip->ipi_cmd_type == IF_CMD) {
12489 12490                  /* This a old style SIOC[GS]IF* command */
12490 12491                  ifr = (struct ifreq *)mp1->b_rptr;
12491 12492                  sin = (sin_t *)&ifr->ifr_addr;
12492 12493          } else if (ipip->ipi_cmd_type == LIF_CMD) {
12493 12494                  /* This a new style SIOC[GS]LIF* command */
12494 12495                  lifr = (struct lifreq *)mp1->b_rptr;
12495 12496                  sin = (sin_t *)&lifr->lifr_addr;
12496 12497          } else {
12497 12498                  sin = NULL;
12498 12499          }
12499 12500  
12500 12501          err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12501 12502              q, mp, ipip, mp1->b_rptr);
12502 12503  
12503 12504          DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12504 12505              int, ipip->ipi_cmd,
12505 12506              ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12506 12507              ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12507 12508  
12508 12509          ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12509 12510  }
12510 12511  
12511 12512  /*
12512 12513   * ioctl processing
12513 12514   *
12514 12515   * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12515 12516   * the ioctl command in the ioctl tables, determines the copyin data size
12516 12517   * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12517 12518   *
12518 12519   * ioctl processing then continues when the M_IOCDATA makes its way down to
12519 12520   * ip_wput_nondata().  The ioctl is looked up again in the ioctl table, its
12520 12521   * associated 'conn' is refheld till the end of the ioctl and the general
12521 12522   * ioctl processing function ip_process_ioctl() is called to extract the
12522 12523   * arguments and process the ioctl.  To simplify extraction, ioctl commands
12523 12524   * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12524 12525   * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12525 12526   * is used to extract the ioctl's arguments.
12526 12527   *
12527 12528   * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12528 12529   * so goes thru the serialization primitive ipsq_try_enter. Then the
12529 12530   * appropriate function to handle the ioctl is called based on the entry in
12530 12531   * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12531 12532   * which also refreleases the 'conn' that was refheld at the start of the
12532 12533   * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12533 12534   *
12534 12535   * Many exclusive ioctls go thru an internal down up sequence as part of
12535 12536   * the operation. For example an attempt to change the IP address of an
12536 12537   * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12537 12538   * does all the cleanup such as deleting all ires that use this address.
12538 12539   * Then we need to wait till all references to the interface go away.
12539 12540   */
12540 12541  void
12541 12542  ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12542 12543  {
12543 12544          struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12544 12545          ip_ioctl_cmd_t *ipip = arg;
12545 12546          ip_extract_func_t *extract_funcp;
12546 12547          cmd_info_t ci;
12547 12548          int err;
12548 12549          boolean_t entered_ipsq = B_FALSE;
12549 12550  
12550 12551          ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12551 12552  
12552 12553          if (ipip == NULL)
12553 12554                  ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12554 12555  
12555 12556          /*
12556 12557           * SIOCLIFADDIF needs to go thru a special path since the
12557 12558           * ill may not exist yet. This happens in the case of lo0
12558 12559           * which is created using this ioctl.
12559 12560           */
12560 12561          if (ipip->ipi_cmd == SIOCLIFADDIF) {
12561 12562                  err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12562 12563                  DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12563 12564                      int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12564 12565                  ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12565 12566                  return;
12566 12567          }
12567 12568  
12568 12569          ci.ci_ipif = NULL;
12569 12570          switch (ipip->ipi_cmd_type) {
12570 12571          case MISC_CMD:
12571 12572          case MSFILT_CMD:
12572 12573                  /*
12573 12574                   * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12574 12575                   */
12575 12576                  if (ipip->ipi_cmd == IF_UNITSEL) {
12576 12577                          /* ioctl comes down the ill */
12577 12578                          ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12578 12579                          ipif_refhold(ci.ci_ipif);
12579 12580                  }
12580 12581                  err = 0;
12581 12582                  ci.ci_sin = NULL;
12582 12583                  ci.ci_sin6 = NULL;
12583 12584                  ci.ci_lifr = NULL;
12584 12585                  extract_funcp = NULL;
12585 12586                  break;
12586 12587  
12587 12588          case IF_CMD:
12588 12589          case LIF_CMD:
12589 12590                  extract_funcp = ip_extract_lifreq;
12590 12591                  break;
12591 12592  
12592 12593          case ARP_CMD:
12593 12594          case XARP_CMD:
12594 12595                  extract_funcp = ip_extract_arpreq;
12595 12596                  break;
12596 12597  
12597 12598          default:
12598 12599                  ASSERT(0);
12599 12600          }
12600 12601  
12601 12602          if (extract_funcp != NULL) {
12602 12603                  err = (*extract_funcp)(q, mp, ipip, &ci);
12603 12604                  if (err != 0) {
12604 12605                          DTRACE_PROBE4(ipif__ioctl,
12605 12606                              char *, "ip_process_ioctl finish err",
12606 12607                              int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12607 12608                          ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12608 12609                          return;
12609 12610                  }
12610 12611  
12611 12612                  /*
12612 12613                   * All of the extraction functions return a refheld ipif.
12613 12614                   */
12614 12615                  ASSERT(ci.ci_ipif != NULL);
12615 12616          }
12616 12617  
12617 12618          if (!(ipip->ipi_flags & IPI_WR)) {
12618 12619                  /*
12619 12620                   * A return value of EINPROGRESS means the ioctl is
12620 12621                   * either queued and waiting for some reason or has
12621 12622                   * already completed.
12622 12623                   */
12623 12624                  err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12624 12625                      ci.ci_lifr);
12625 12626                  if (ci.ci_ipif != NULL) {
12626 12627                          DTRACE_PROBE4(ipif__ioctl,
12627 12628                              char *, "ip_process_ioctl finish RD",
12628 12629                              int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12629 12630                              ipif_t *, ci.ci_ipif);
12630 12631                          ipif_refrele(ci.ci_ipif);
12631 12632                  } else {
12632 12633                          DTRACE_PROBE4(ipif__ioctl,
12633 12634                              char *, "ip_process_ioctl finish RD",
12634 12635                              int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12635 12636                  }
12636 12637                  ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12637 12638                  return;
12638 12639          }
12639 12640  
12640 12641          ASSERT(ci.ci_ipif != NULL);
12641 12642  
12642 12643          /*
12643 12644           * If ipsq is non-NULL, we are already being called exclusively
12644 12645           */
12645 12646          ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12646 12647          if (ipsq == NULL) {
12647 12648                  ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12648 12649                      NEW_OP, B_TRUE);
12649 12650                  if (ipsq == NULL) {
12650 12651                          ipif_refrele(ci.ci_ipif);
12651 12652                          return;
12652 12653                  }
12653 12654                  entered_ipsq = B_TRUE;
12654 12655          }
12655 12656          /*
12656 12657           * Release the ipif so that ipif_down and friends that wait for
12657 12658           * references to go away are not misled about the current ipif_refcnt
12658 12659           * values. We are writer so we can access the ipif even after releasing
12659 12660           * the ipif.
12660 12661           */
12661 12662          ipif_refrele(ci.ci_ipif);
12662 12663  
12663 12664          ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12664 12665  
12665 12666          /*
12666 12667           * A return value of EINPROGRESS means the ioctl is
12667 12668           * either queued and waiting for some reason or has
12668 12669           * already completed.
12669 12670           */
12670 12671          err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12671 12672  
12672 12673          DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12673 12674              int, ipip->ipi_cmd,
12674 12675              ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12675 12676              ipif_t *, ci.ci_ipif);
12676 12677          ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12677 12678  
12678 12679          if (entered_ipsq)
12679 12680                  ipsq_exit(ipsq);
12680 12681  }
12681 12682  
12682 12683  /*
12683 12684   * Complete the ioctl. Typically ioctls use the mi package and need to
12684 12685   * do mi_copyout/mi_copy_done.
12685 12686   */
12686 12687  void
12687 12688  ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12688 12689  {
12689 12690          conn_t  *connp = NULL;
12690 12691  
12691 12692          if (err == EINPROGRESS)
12692 12693                  return;
12693 12694  
12694 12695          if (CONN_Q(q)) {
12695 12696                  connp = Q_TO_CONN(q);
12696 12697                  ASSERT(connp->conn_ref >= 2);
12697 12698          }
12698 12699  
12699 12700          switch (mode) {
12700 12701          case COPYOUT:
12701 12702                  if (err == 0)
12702 12703                          mi_copyout(q, mp);
12703 12704                  else
12704 12705                          mi_copy_done(q, mp, err);
12705 12706                  break;
12706 12707  
12707 12708          case NO_COPYOUT:
12708 12709                  mi_copy_done(q, mp, err);
12709 12710                  break;
12710 12711  
12711 12712          default:
12712 12713                  ASSERT(mode == CONN_CLOSE);     /* aborted through CONN_CLOSE */
12713 12714                  break;
12714 12715          }
12715 12716  
12716 12717          /*
12717 12718           * The conn refhold and ioctlref placed on the conn at the start of the
12718 12719           * ioctl are released here.
12719 12720           */
12720 12721          if (connp != NULL) {
12721 12722                  CONN_DEC_IOCTLREF(connp);
12722 12723                  CONN_OPER_PENDING_DONE(connp);
12723 12724          }
12724 12725  
12725 12726          if (ipsq != NULL)
12726 12727                  ipsq_current_finish(ipsq);
12727 12728  }
12728 12729  
12729 12730  /* Handles all non data messages */
12730 12731  void
12731 12732  ip_wput_nondata(queue_t *q, mblk_t *mp)
12732 12733  {
12733 12734          mblk_t          *mp1;
12734 12735          struct iocblk   *iocp;
12735 12736          ip_ioctl_cmd_t  *ipip;
12736 12737          conn_t          *connp;
12737 12738          cred_t          *cr;
12738 12739          char            *proto_str;
12739 12740  
12740 12741          if (CONN_Q(q))
12741 12742                  connp = Q_TO_CONN(q);
12742 12743          else
12743 12744                  connp = NULL;
12744 12745  
12745 12746          switch (DB_TYPE(mp)) {
12746 12747          case M_IOCTL:
12747 12748                  /*
12748 12749                   * IOCTL processing begins in ip_sioctl_copyin_setup which
12749 12750                   * will arrange to copy in associated control structures.
12750 12751                   */
12751 12752                  ip_sioctl_copyin_setup(q, mp);
12752 12753                  return;
12753 12754          case M_IOCDATA:
12754 12755                  /*
12755 12756                   * Ensure that this is associated with one of our trans-
12756 12757                   * parent ioctls.  If it's not ours, discard it if we're
12757 12758                   * running as a driver, or pass it on if we're a module.
12758 12759                   */
12759 12760                  iocp = (struct iocblk *)mp->b_rptr;
12760 12761                  ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12761 12762                  if (ipip == NULL) {
12762 12763                          if (q->q_next == NULL) {
12763 12764                                  goto nak;
12764 12765                          } else {
12765 12766                                  putnext(q, mp);
12766 12767                          }
12767 12768                          return;
12768 12769                  }
12769 12770                  if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12770 12771                          /*
12771 12772                           * The ioctl is one we recognise, but is not consumed
12772 12773                           * by IP as a module and we are a module, so we drop
12773 12774                           */
12774 12775                          goto nak;
12775 12776                  }
12776 12777  
12777 12778                  /* IOCTL continuation following copyin or copyout. */
12778 12779                  if (mi_copy_state(q, mp, NULL) == -1) {
12779 12780                          /*
12780 12781                           * The copy operation failed.  mi_copy_state already
12781 12782                           * cleaned up, so we're out of here.
12782 12783                           */
12783 12784                          return;
12784 12785                  }
12785 12786                  /*
12786 12787                   * If we just completed a copy in, we become writer and
12787 12788                   * continue processing in ip_sioctl_copyin_done.  If it
12788 12789                   * was a copy out, we call mi_copyout again.  If there is
12789 12790                   * nothing more to copy out, it will complete the IOCTL.
12790 12791                   */
12791 12792                  if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12792 12793                          if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12793 12794                                  mi_copy_done(q, mp, EPROTO);
12794 12795                                  return;
12795 12796                          }
12796 12797                          /*
12797 12798                           * Check for cases that need more copying.  A return
12798 12799                           * value of 0 means a second copyin has been started,
12799 12800                           * so we return; a return value of 1 means no more
12800 12801                           * copying is needed, so we continue.
12801 12802                           */
12802 12803                          if (ipip->ipi_cmd_type == MSFILT_CMD &&
12803 12804                              MI_COPY_COUNT(mp) == 1) {
12804 12805                                  if (ip_copyin_msfilter(q, mp) == 0)
12805 12806                                          return;
12806 12807                          }
12807 12808                          /*
12808 12809                           * Refhold the conn, till the ioctl completes. This is
12809 12810                           * needed in case the ioctl ends up in the pending mp
12810 12811                           * list. Every mp in the ipx_pending_mp list must have
12811 12812                           * a refhold on the conn to resume processing. The
12812 12813                           * refhold is released when the ioctl completes
12813 12814                           * (whether normally or abnormally). An ioctlref is also
12814 12815                           * placed on the conn to prevent TCP from removing the
12815 12816                           * queue needed to send the ioctl reply back.
12816 12817                           * In all cases ip_ioctl_finish is called to finish
12817 12818                           * the ioctl and release the refholds.
12818 12819                           */
12819 12820                          if (connp != NULL) {
12820 12821                                  /* This is not a reentry */
12821 12822                                  CONN_INC_REF(connp);
12822 12823                                  CONN_INC_IOCTLREF(connp);
12823 12824                          } else {
12824 12825                                  if (!(ipip->ipi_flags & IPI_MODOK)) {
12825 12826                                          mi_copy_done(q, mp, EINVAL);
12826 12827                                          return;
12827 12828                                  }
12828 12829                          }
12829 12830  
12830 12831                          ip_process_ioctl(NULL, q, mp, ipip);
12831 12832  
12832 12833                  } else {
12833 12834                          mi_copyout(q, mp);
12834 12835                  }
12835 12836                  return;
12836 12837  
12837 12838          case M_IOCNAK:
12838 12839                  /*
12839 12840                   * The only way we could get here is if a resolver didn't like
12840 12841                   * an IOCTL we sent it.  This shouldn't happen.
12841 12842                   */
12842 12843                  (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12843 12844                      "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12844 12845                      ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12845 12846                  freemsg(mp);
12846 12847                  return;
12847 12848          case M_IOCACK:
12848 12849                  /* /dev/ip shouldn't see this */
12849 12850                  goto nak;
12850 12851          case M_FLUSH:
12851 12852                  if (*mp->b_rptr & FLUSHW)
12852 12853                          flushq(q, FLUSHALL);
12853 12854                  if (q->q_next) {
12854 12855                          putnext(q, mp);
12855 12856                          return;
12856 12857                  }
12857 12858                  if (*mp->b_rptr & FLUSHR) {
12858 12859                          *mp->b_rptr &= ~FLUSHW;
12859 12860                          qreply(q, mp);
12860 12861                          return;
12861 12862                  }
12862 12863                  freemsg(mp);
12863 12864                  return;
12864 12865          case M_CTL:
12865 12866                  break;
12866 12867          case M_PROTO:
12867 12868          case M_PCPROTO:
12868 12869                  /*
12869 12870                   * The only PROTO messages we expect are SNMP-related.
12870 12871                   */
12871 12872                  switch (((union T_primitives *)mp->b_rptr)->type) {
12872 12873                  case T_SVR4_OPTMGMT_REQ:
12873 12874                          ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12874 12875                              "flags %x\n",
12875 12876                              ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12876 12877  
12877 12878                          if (connp == NULL) {
12878 12879                                  proto_str = "T_SVR4_OPTMGMT_REQ";
12879 12880                                  goto protonak;
12880 12881                          }
12881 12882  
12882 12883                          /*
12883 12884                           * All Solaris components should pass a db_credp
12884 12885                           * for this TPI message, hence we ASSERT.
12885 12886                           * But in case there is some other M_PROTO that looks
12886 12887                           * like a TPI message sent by some other kernel
12887 12888                           * component, we check and return an error.
12888 12889                           */
12889 12890                          cr = msg_getcred(mp, NULL);
12890 12891                          ASSERT(cr != NULL);
12891 12892                          if (cr == NULL) {
12892 12893                                  mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12893 12894                                  if (mp != NULL)
12894 12895                                          qreply(q, mp);
12895 12896                                  return;
12896 12897                          }
12897 12898  
12898 12899                          if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12899 12900                                  proto_str = "Bad SNMPCOM request?";
12900 12901                                  goto protonak;
12901 12902                          }
12902 12903                          return;
12903 12904                  default:
12904 12905                          ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12905 12906                              (int)*(uint_t *)mp->b_rptr));
12906 12907                          freemsg(mp);
12907 12908                          return;
12908 12909                  }
12909 12910          default:
12910 12911                  break;
12911 12912          }
12912 12913          if (q->q_next) {
12913 12914                  putnext(q, mp);
12914 12915          } else
12915 12916                  freemsg(mp);
12916 12917          return;
12917 12918  
12918 12919  nak:
12919 12920          iocp->ioc_error = EINVAL;
12920 12921          mp->b_datap->db_type = M_IOCNAK;
12921 12922          iocp->ioc_count = 0;
12922 12923          qreply(q, mp);
12923 12924          return;
12924 12925  
12925 12926  protonak:
12926 12927          cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12927 12928          if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12928 12929                  qreply(q, mp);
12929 12930  }
12930 12931  
12931 12932  /*
12932 12933   * Process IP options in an outbound packet.  Verify that the nexthop in a
12933 12934   * strict source route is onlink.
12934 12935   * Returns non-zero if something fails in which case an ICMP error has been
12935 12936   * sent and mp freed.
12936 12937   *
12937 12938   * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12938 12939   */
12939 12940  int
12940 12941  ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12941 12942  {
12942 12943          ipoptp_t        opts;
12943 12944          uchar_t         *opt;
12944 12945          uint8_t         optval;
12945 12946          uint8_t         optlen;
12946 12947          ipaddr_t        dst;
12947 12948          intptr_t        code = 0;
12948 12949          ire_t           *ire;
12949 12950          ip_stack_t      *ipst = ixa->ixa_ipst;
12950 12951          ip_recv_attr_t  iras;
12951 12952  
12952 12953          ip2dbg(("ip_output_options\n"));
12953 12954  
12954 12955          dst = ipha->ipha_dst;
12955 12956          for (optval = ipoptp_first(&opts, ipha);
12956 12957              optval != IPOPT_EOL;
12957 12958              optval = ipoptp_next(&opts)) {
12958 12959                  opt = opts.ipoptp_cur;
12959 12960                  optlen = opts.ipoptp_len;
12960 12961                  ip2dbg(("ip_output_options: opt %d, len %d\n",
12961 12962                      optval, optlen));
12962 12963                  switch (optval) {
12963 12964                          uint32_t off;
12964 12965                  case IPOPT_SSRR:
12965 12966                  case IPOPT_LSRR:
12966 12967                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12967 12968                                  ip1dbg((
12968 12969                                      "ip_output_options: bad option offset\n"));
12969 12970                                  code = (char *)&opt[IPOPT_OLEN] -
12970 12971                                      (char *)ipha;
12971 12972                                  goto param_prob;
12972 12973                          }
12973 12974                          off = opt[IPOPT_OFFSET];
12974 12975                          ip1dbg(("ip_output_options: next hop 0x%x\n",
12975 12976                              ntohl(dst)));
12976 12977                          /*
12977 12978                           * For strict: verify that dst is directly
12978 12979                           * reachable.
12979 12980                           */
12980 12981                          if (optval == IPOPT_SSRR) {
12981 12982                                  ire = ire_ftable_lookup_v4(dst, 0, 0,
12982 12983                                      IRE_INTERFACE, NULL, ALL_ZONES,
12983 12984                                      ixa->ixa_tsl,
12984 12985                                      MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
12985 12986                                      NULL);
12986 12987                                  if (ire == NULL) {
12987 12988                                          ip1dbg(("ip_output_options: SSRR not"
12988 12989                                              " directly reachable: 0x%x\n",
12989 12990                                              ntohl(dst)));
12990 12991                                          goto bad_src_route;
12991 12992                                  }
12992 12993                                  ire_refrele(ire);
12993 12994                          }
12994 12995                          break;
12995 12996                  case IPOPT_RR:
12996 12997                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12997 12998                                  ip1dbg((
12998 12999                                      "ip_output_options: bad option offset\n"));
12999 13000                                  code = (char *)&opt[IPOPT_OLEN] -
13000 13001                                      (char *)ipha;
13001 13002                                  goto param_prob;
13002 13003                          }
13003 13004                          break;
13004 13005                  case IPOPT_TS:
13005 13006                          /*
13006 13007                           * Verify that length >=5 and that there is either
13007 13008                           * room for another timestamp or that the overflow
13008 13009                           * counter is not maxed out.
13009 13010                           */
13010 13011                          code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
13011 13012                          if (optlen < IPOPT_MINLEN_IT) {
13012 13013                                  goto param_prob;
13013 13014                          }
13014 13015                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13015 13016                                  ip1dbg((
13016 13017                                      "ip_output_options: bad option offset\n"));
13017 13018                                  code = (char *)&opt[IPOPT_OFFSET] -
13018 13019                                      (char *)ipha;
13019 13020                                  goto param_prob;
13020 13021                          }
13021 13022                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
13022 13023                          case IPOPT_TS_TSONLY:
13023 13024                                  off = IPOPT_TS_TIMELEN;
13024 13025                                  break;
13025 13026                          case IPOPT_TS_TSANDADDR:
13026 13027                          case IPOPT_TS_PRESPEC:
13027 13028                          case IPOPT_TS_PRESPEC_RFC791:
13028 13029                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
13029 13030                                  break;
13030 13031                          default:
13031 13032                                  code = (char *)&opt[IPOPT_POS_OV_FLG] -
13032 13033                                      (char *)ipha;
13033 13034                                  goto param_prob;
13034 13035                          }
13035 13036                          if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
13036 13037                              (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
13037 13038                                  /*
13038 13039                                   * No room and the overflow counter is 15
13039 13040                                   * already.
13040 13041                                   */
13041 13042                                  goto param_prob;
13042 13043                          }
13043 13044                          break;
13044 13045                  }
13045 13046          }
13046 13047  
13047 13048          if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
13048 13049                  return (0);
13049 13050  
13050 13051          ip1dbg(("ip_output_options: error processing IP options."));
13051 13052          code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
13052 13053  
13053 13054  param_prob:
13054 13055          bzero(&iras, sizeof (iras));
13055 13056          iras.ira_ill = iras.ira_rill = ill;
13056 13057          iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13057 13058          iras.ira_rifindex = iras.ira_ruifindex;
13058 13059          iras.ira_flags = IRAF_IS_IPV4;
13059 13060  
13060 13061          ip_drop_output("ip_output_options", mp, ill);
13061 13062          icmp_param_problem(mp, (uint8_t)code, &iras);
13062 13063          ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13063 13064          return (-1);
13064 13065  
13065 13066  bad_src_route:
13066 13067          bzero(&iras, sizeof (iras));
13067 13068          iras.ira_ill = iras.ira_rill = ill;
13068 13069          iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13069 13070          iras.ira_rifindex = iras.ira_ruifindex;
13070 13071          iras.ira_flags = IRAF_IS_IPV4;
13071 13072  
13072 13073          ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
13073 13074          icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
13074 13075          ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13075 13076          return (-1);
13076 13077  }
13077 13078  
13078 13079  /*
13079 13080   * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
13080 13081   * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
13081 13082   * thru /etc/system.
13082 13083   */
13083 13084  #define CONN_MAXDRAINCNT        64
13084 13085  
13085 13086  static void
13086 13087  conn_drain_init(ip_stack_t *ipst)
13087 13088  {
13088 13089          int i, j;
13089 13090          idl_tx_list_t *itl_tx;
13090 13091  
13091 13092          ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
13092 13093  
13093 13094          if ((ipst->ips_conn_drain_list_cnt == 0) ||
13094 13095              (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
13095 13096                  /*
13096 13097                   * Default value of the number of drainers is the
13097 13098                   * number of cpus, subject to maximum of 8 drainers.
13098 13099                   */
13099 13100                  if (boot_max_ncpus != -1)
13100 13101                          ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
13101 13102                  else
13102 13103                          ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
13103 13104          }
13104 13105  
13105 13106          ipst->ips_idl_tx_list =
13106 13107              kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
13107 13108          for (i = 0; i < TX_FANOUT_SIZE; i++) {
13108 13109                  itl_tx =  &ipst->ips_idl_tx_list[i];
13109 13110                  itl_tx->txl_drain_list =
13110 13111                      kmem_zalloc(ipst->ips_conn_drain_list_cnt *
13111 13112                      sizeof (idl_t), KM_SLEEP);
13112 13113                  mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
13113 13114                  for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
13114 13115                          mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
13115 13116                              MUTEX_DEFAULT, NULL);
13116 13117                          itl_tx->txl_drain_list[j].idl_itl = itl_tx;
13117 13118                  }
13118 13119          }
13119 13120  }
13120 13121  
13121 13122  static void
13122 13123  conn_drain_fini(ip_stack_t *ipst)
13123 13124  {
13124 13125          int i;
13125 13126          idl_tx_list_t *itl_tx;
13126 13127  
13127 13128          for (i = 0; i < TX_FANOUT_SIZE; i++) {
13128 13129                  itl_tx =  &ipst->ips_idl_tx_list[i];
13129 13130                  kmem_free(itl_tx->txl_drain_list,
13130 13131                      ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
13131 13132          }
13132 13133          kmem_free(ipst->ips_idl_tx_list,
13133 13134              TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
13134 13135          ipst->ips_idl_tx_list = NULL;
13135 13136  }
13136 13137  
13137 13138  /*
13138 13139   * Flow control has blocked us from proceeding.  Insert the given conn in one
13139 13140   * of the conn drain lists.  When flow control is unblocked, either ip_wsrv()
13140 13141   * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
13141 13142   * will call conn_walk_drain().  See the flow control notes at the top of this
13142 13143   * file for more details.
13143 13144   */
13144 13145  void
13145 13146  conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
13146 13147  {
13147 13148          idl_t   *idl = tx_list->txl_drain_list;
13148 13149          uint_t  index;
13149 13150          ip_stack_t      *ipst = connp->conn_netstack->netstack_ip;
13150 13151  
13151 13152          mutex_enter(&connp->conn_lock);
13152 13153          if (connp->conn_state_flags & CONN_CLOSING) {
13153 13154                  /*
13154 13155                   * The conn is closing as a result of which CONN_CLOSING
13155 13156                   * is set. Return.
13156 13157                   */
13157 13158                  mutex_exit(&connp->conn_lock);
13158 13159                  return;
13159 13160          } else if (connp->conn_idl == NULL) {
13160 13161                  /*
13161 13162                   * Assign the next drain list round robin. We dont' use
13162 13163                   * a lock, and thus it may not be strictly round robin.
13163 13164                   * Atomicity of load/stores is enough to make sure that
13164 13165                   * conn_drain_list_index is always within bounds.
13165 13166                   */
13166 13167                  index = tx_list->txl_drain_index;
13167 13168                  ASSERT(index < ipst->ips_conn_drain_list_cnt);
13168 13169                  connp->conn_idl = &tx_list->txl_drain_list[index];
13169 13170                  index++;
13170 13171                  if (index == ipst->ips_conn_drain_list_cnt)
13171 13172                          index = 0;
13172 13173                  tx_list->txl_drain_index = index;
13173 13174          } else {
13174 13175                  ASSERT(connp->conn_idl->idl_itl == tx_list);
13175 13176          }
13176 13177          mutex_exit(&connp->conn_lock);
13177 13178  
13178 13179          idl = connp->conn_idl;
13179 13180          mutex_enter(&idl->idl_lock);
13180 13181          if ((connp->conn_drain_prev != NULL) ||
13181 13182              (connp->conn_state_flags & CONN_CLOSING)) {
13182 13183                  /*
13183 13184                   * The conn is either already in the drain list or closing.
13184 13185                   * (We needed to check for CONN_CLOSING again since close can
13185 13186                   * sneak in between dropping conn_lock and acquiring idl_lock.)
13186 13187                   */
13187 13188                  mutex_exit(&idl->idl_lock);
13188 13189                  return;
13189 13190          }
13190 13191  
13191 13192          /*
13192 13193           * The conn is not in the drain list. Insert it at the
13193 13194           * tail of the drain list. The drain list is circular
13194 13195           * and doubly linked. idl_conn points to the 1st element
13195 13196           * in the list.
13196 13197           */
13197 13198          if (idl->idl_conn == NULL) {
13198 13199                  idl->idl_conn = connp;
13199 13200                  connp->conn_drain_next = connp;
13200 13201                  connp->conn_drain_prev = connp;
13201 13202          } else {
13202 13203                  conn_t *head = idl->idl_conn;
13203 13204  
13204 13205                  connp->conn_drain_next = head;
13205 13206                  connp->conn_drain_prev = head->conn_drain_prev;
13206 13207                  head->conn_drain_prev->conn_drain_next = connp;
13207 13208                  head->conn_drain_prev = connp;
13208 13209          }
13209 13210          /*
13210 13211           * For non streams based sockets assert flow control.
13211 13212           */
13212 13213          conn_setqfull(connp, NULL);
13213 13214          mutex_exit(&idl->idl_lock);
13214 13215  }
13215 13216  
13216 13217  static void
13217 13218  conn_drain_remove(conn_t *connp)
13218 13219  {
13219 13220          idl_t *idl = connp->conn_idl;
13220 13221  
13221 13222          if (idl != NULL) {
13222 13223                  /*
13223 13224                   * Remove ourself from the drain list.
13224 13225                   */
13225 13226                  if (connp->conn_drain_next == connp) {
13226 13227                          /* Singleton in the list */
13227 13228                          ASSERT(connp->conn_drain_prev == connp);
13228 13229                          idl->idl_conn = NULL;
13229 13230                  } else {
13230 13231                          connp->conn_drain_prev->conn_drain_next =
13231 13232                              connp->conn_drain_next;
13232 13233                          connp->conn_drain_next->conn_drain_prev =
13233 13234                              connp->conn_drain_prev;
13234 13235                          if (idl->idl_conn == connp)
13235 13236                                  idl->idl_conn = connp->conn_drain_next;
13236 13237                  }
13237 13238  
13238 13239                  /*
13239 13240                   * NOTE: because conn_idl is associated with a specific drain
13240 13241                   * list which in turn is tied to the index the TX ring
13241 13242                   * (txl_cookie) hashes to, and because the TX ring can change
13242 13243                   * over the lifetime of the conn_t, we must clear conn_idl so
13243 13244                   * a subsequent conn_drain_insert() will set conn_idl again
13244 13245                   * based on the latest txl_cookie.
13245 13246                   */
13246 13247                  connp->conn_idl = NULL;
13247 13248          }
13248 13249          connp->conn_drain_next = NULL;
13249 13250          connp->conn_drain_prev = NULL;
13250 13251  
13251 13252          conn_clrqfull(connp, NULL);
13252 13253          /*
13253 13254           * For streams based sockets open up flow control.
13254 13255           */
13255 13256          if (!IPCL_IS_NONSTR(connp))
13256 13257                  enableok(connp->conn_wq);
13257 13258  }
13258 13259  
13259 13260  /*
13260 13261   * This conn is closing, and we are called from ip_close. OR
13261 13262   * this conn is draining because flow-control on the ill has been relieved.
13262 13263   *
13263 13264   * We must also need to remove conn's on this idl from the list, and also
13264 13265   * inform the sockfs upcalls about the change in flow-control.
13265 13266   */
13266 13267  static void
13267 13268  conn_drain(conn_t *connp, boolean_t closing)
13268 13269  {
13269 13270          idl_t *idl;
13270 13271          conn_t *next_connp;
13271 13272  
13272 13273          /*
13273 13274           * connp->conn_idl is stable at this point, and no lock is needed
13274 13275           * to check it. If we are called from ip_close, close has already
13275 13276           * set CONN_CLOSING, thus freezing the value of conn_idl, and
13276 13277           * called us only because conn_idl is non-null. If we are called thru
13277 13278           * service, conn_idl could be null, but it cannot change because
13278 13279           * service is single-threaded per queue, and there cannot be another
13279 13280           * instance of service trying to call conn_drain_insert on this conn
13280 13281           * now.
13281 13282           */
13282 13283          ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
13283 13284  
13284 13285          /*
13285 13286           * If the conn doesn't exist or is not on a drain list, bail.
13286 13287           */
13287 13288          if (connp == NULL || connp->conn_idl == NULL ||
13288 13289              connp->conn_drain_prev == NULL) {
13289 13290                  return;
13290 13291          }
13291 13292  
13292 13293          idl = connp->conn_idl;
13293 13294          ASSERT(MUTEX_HELD(&idl->idl_lock));
13294 13295  
13295 13296          if (!closing) {
13296 13297                  next_connp = connp->conn_drain_next;
13297 13298                  while (next_connp != connp) {
13298 13299                          conn_t *delconnp = next_connp;
13299 13300  
13300 13301                          next_connp = next_connp->conn_drain_next;
13301 13302                          conn_drain_remove(delconnp);
13302 13303                  }
13303 13304                  ASSERT(connp->conn_drain_next == idl->idl_conn);
13304 13305          }
13305 13306          conn_drain_remove(connp);
13306 13307  }
13307 13308  
13308 13309  /*
13309 13310   * Write service routine. Shared perimeter entry point.
13310 13311   * The device queue's messages has fallen below the low water mark and STREAMS
13311 13312   * has backenabled the ill_wq. Send sockfs notification about flow-control on
13312 13313   * each waiting conn.
13313 13314   */
13314 13315  void
13315 13316  ip_wsrv(queue_t *q)
13316 13317  {
13317 13318          ill_t   *ill;
13318 13319  
13319 13320          ill = (ill_t *)q->q_ptr;
13320 13321          if (ill->ill_state_flags == 0) {
13321 13322                  ip_stack_t *ipst = ill->ill_ipst;
13322 13323  
13323 13324                  /*
13324 13325                   * The device flow control has opened up.
13325 13326                   * Walk through conn drain lists and qenable the
13326 13327                   * first conn in each list. This makes sense only
13327 13328                   * if the stream is fully plumbed and setup.
13328 13329                   * Hence the ill_state_flags check above.
13329 13330                   */
13330 13331                  ip1dbg(("ip_wsrv: walking\n"));
13331 13332                  conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
13332 13333                  enableok(ill->ill_wq);
13333 13334          }
13334 13335  }
13335 13336  
13336 13337  /*
13337 13338   * Callback to disable flow control in IP.
13338 13339   *
13339 13340   * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
13340 13341   * is enabled.
13341 13342   *
13342 13343   * When MAC_TX() is not able to send any more packets, dld sets its queue
13343 13344   * to QFULL and enable the STREAMS flow control. Later, when the underlying
13344 13345   * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13345 13346   * function and wakes up corresponding mac worker threads, which in turn
13346 13347   * calls this callback function, and disables flow control.
13347 13348   */
13348 13349  void
13349 13350  ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13350 13351  {
13351 13352          ill_t *ill = (ill_t *)arg;
13352 13353          ip_stack_t *ipst = ill->ill_ipst;
13353 13354          idl_tx_list_t *idl_txl;
13354 13355  
13355 13356          idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13356 13357          mutex_enter(&idl_txl->txl_lock);
13357 13358          /* add code to to set a flag to indicate idl_txl is enabled */
13358 13359          conn_walk_drain(ipst, idl_txl);
13359 13360          mutex_exit(&idl_txl->txl_lock);
13360 13361  }
13361 13362  
13362 13363  /*
13363 13364   * Flow control has been relieved and STREAMS has backenabled us; drain
13364 13365   * all the conn lists on `tx_list'.
13365 13366   */
13366 13367  static void
13367 13368  conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13368 13369  {
13369 13370          int i;
13370 13371          idl_t *idl;
13371 13372  
13372 13373          IP_STAT(ipst, ip_conn_walk_drain);
13373 13374  
13374 13375          for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13375 13376                  idl = &tx_list->txl_drain_list[i];
13376 13377                  mutex_enter(&idl->idl_lock);
13377 13378                  conn_drain(idl->idl_conn, B_FALSE);
13378 13379                  mutex_exit(&idl->idl_lock);
13379 13380          }
13380 13381  }
13381 13382  
13382 13383  /*
13383 13384   * Determine if the ill and multicast aspects of that packets
13384 13385   * "matches" the conn.
13385 13386   */
13386 13387  boolean_t
13387 13388  conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13388 13389  {
13389 13390          ill_t           *ill = ira->ira_rill;
13390 13391          zoneid_t        zoneid = ira->ira_zoneid;
13391 13392          uint_t          in_ifindex;
13392 13393          ipaddr_t        dst, src;
13393 13394  
13394 13395          dst = ipha->ipha_dst;
13395 13396          src = ipha->ipha_src;
13396 13397  
13397 13398          /*
13398 13399           * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13399 13400           * unicast, broadcast and multicast reception to
13400 13401           * conn_incoming_ifindex.
13401 13402           * conn_wantpacket is called for unicast, broadcast and
13402 13403           * multicast packets.
13403 13404           */
13404 13405          in_ifindex = connp->conn_incoming_ifindex;
13405 13406  
13406 13407          /* mpathd can bind to the under IPMP interface, which we allow */
13407 13408          if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13408 13409                  if (!IS_UNDER_IPMP(ill))
13409 13410                          return (B_FALSE);
13410 13411  
13411 13412                  if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13412 13413                          return (B_FALSE);
13413 13414          }
13414 13415  
13415 13416          if (!IPCL_ZONE_MATCH(connp, zoneid))
13416 13417                  return (B_FALSE);
13417 13418  
13418 13419          if (!(ira->ira_flags & IRAF_MULTICAST))
13419 13420                  return (B_TRUE);
13420 13421  
13421 13422          if (connp->conn_multi_router) {
13422 13423                  /* multicast packet and multicast router socket: send up */
13423 13424                  return (B_TRUE);
13424 13425          }
13425 13426  
13426 13427          if (ipha->ipha_protocol == IPPROTO_PIM ||
13427 13428              ipha->ipha_protocol == IPPROTO_RSVP)
13428 13429                  return (B_TRUE);
13429 13430  
13430 13431          return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13431 13432  }
13432 13433  
13433 13434  void
13434 13435  conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13435 13436  {
13436 13437          if (IPCL_IS_NONSTR(connp)) {
13437 13438                  (*connp->conn_upcalls->su_txq_full)
13438 13439                      (connp->conn_upper_handle, B_TRUE);
13439 13440                  if (flow_stopped != NULL)
13440 13441                          *flow_stopped = B_TRUE;
13441 13442          } else {
13442 13443                  queue_t *q = connp->conn_wq;
13443 13444  
13444 13445                  ASSERT(q != NULL);
13445 13446                  if (!(q->q_flag & QFULL)) {
13446 13447                          mutex_enter(QLOCK(q));
13447 13448                          if (!(q->q_flag & QFULL)) {
13448 13449                                  /* still need to set QFULL */
13449 13450                                  q->q_flag |= QFULL;
13450 13451                                  /* set flow_stopped to true under QLOCK */
13451 13452                                  if (flow_stopped != NULL)
13452 13453                                          *flow_stopped = B_TRUE;
13453 13454                                  mutex_exit(QLOCK(q));
13454 13455                          } else {
13455 13456                                  /* flow_stopped is left unchanged */
13456 13457                                  mutex_exit(QLOCK(q));
13457 13458                          }
13458 13459                  }
13459 13460          }
13460 13461  }
13461 13462  
13462 13463  void
13463 13464  conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13464 13465  {
13465 13466          if (IPCL_IS_NONSTR(connp)) {
13466 13467                  (*connp->conn_upcalls->su_txq_full)
13467 13468                      (connp->conn_upper_handle, B_FALSE);
13468 13469                  if (flow_stopped != NULL)
13469 13470                          *flow_stopped = B_FALSE;
13470 13471          } else {
13471 13472                  queue_t *q = connp->conn_wq;
13472 13473  
13473 13474                  ASSERT(q != NULL);
13474 13475                  if (q->q_flag & QFULL) {
13475 13476                          mutex_enter(QLOCK(q));
13476 13477                          if (q->q_flag & QFULL) {
13477 13478                                  q->q_flag &= ~QFULL;
13478 13479                                  /* set flow_stopped to false under QLOCK */
13479 13480                                  if (flow_stopped != NULL)
13480 13481                                          *flow_stopped = B_FALSE;
13481 13482                                  mutex_exit(QLOCK(q));
13482 13483                                  if (q->q_flag & QWANTW)
13483 13484                                          qbackenable(q, 0);
13484 13485                          } else {
13485 13486                                  /* flow_stopped is left unchanged */
13486 13487                                  mutex_exit(QLOCK(q));
13487 13488                          }
13488 13489                  }
13489 13490          }
13490 13491  
13491 13492          mutex_enter(&connp->conn_lock);
13492 13493          connp->conn_blocked = B_FALSE;
13493 13494          mutex_exit(&connp->conn_lock);
13494 13495  }
13495 13496  
13496 13497  /*
13497 13498   * Return the length in bytes of the IPv4 headers (base header, label, and
13498 13499   * other IP options) that will be needed based on the
13499 13500   * ip_pkt_t structure passed by the caller.
13500 13501   *
13501 13502   * The returned length does not include the length of the upper level
13502 13503   * protocol (ULP) header.
13503 13504   * The caller needs to check that the length doesn't exceed the max for IPv4.
13504 13505   */
13505 13506  int
13506 13507  ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13507 13508  {
13508 13509          int len;
13509 13510  
13510 13511          len = IP_SIMPLE_HDR_LENGTH;
13511 13512          if (ipp->ipp_fields & IPPF_LABEL_V4) {
13512 13513                  ASSERT(ipp->ipp_label_len_v4 != 0);
13513 13514                  /* We need to round up here */
13514 13515                  len += (ipp->ipp_label_len_v4 + 3) & ~3;
13515 13516          }
13516 13517  
13517 13518          if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13518 13519                  ASSERT(ipp->ipp_ipv4_options_len != 0);
13519 13520                  ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13520 13521                  len += ipp->ipp_ipv4_options_len;
13521 13522          }
13522 13523          return (len);
13523 13524  }
13524 13525  
13525 13526  /*
13526 13527   * All-purpose routine to build an IPv4 header with options based
13527 13528   * on the abstract ip_pkt_t.
13528 13529   *
13529 13530   * The caller has to set the source and destination address as well as
13530 13531   * ipha_length. The caller has to massage any source route and compensate
13531 13532   * for the ULP pseudo-header checksum due to the source route.
13532 13533   */
13533 13534  void
13534 13535  ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13535 13536      uint8_t protocol)
13536 13537  {
13537 13538          ipha_t  *ipha = (ipha_t *)buf;
13538 13539          uint8_t *cp;
13539 13540  
13540 13541          /* Initialize IPv4 header */
13541 13542          ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13542 13543          ipha->ipha_length = 0;  /* Caller will set later */
13543 13544          ipha->ipha_ident = 0;
13544 13545          ipha->ipha_fragment_offset_and_flags = 0;
13545 13546          ipha->ipha_ttl = ipp->ipp_unicast_hops;
13546 13547          ipha->ipha_protocol = protocol;
13547 13548          ipha->ipha_hdr_checksum = 0;
13548 13549  
13549 13550          if ((ipp->ipp_fields & IPPF_ADDR) &&
13550 13551              IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13551 13552                  ipha->ipha_src = ipp->ipp_addr_v4;
13552 13553  
13553 13554          cp = (uint8_t *)&ipha[1];
13554 13555          if (ipp->ipp_fields & IPPF_LABEL_V4) {
13555 13556                  ASSERT(ipp->ipp_label_len_v4 != 0);
13556 13557                  bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4);
13557 13558                  cp += ipp->ipp_label_len_v4;
13558 13559                  /* We need to round up here */
13559 13560                  while ((uintptr_t)cp & 0x3) {
13560 13561                          *cp++ = IPOPT_NOP;
13561 13562                  }
13562 13563          }
13563 13564  
13564 13565          if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13565 13566                  ASSERT(ipp->ipp_ipv4_options_len != 0);
13566 13567                  ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13567 13568                  bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13568 13569                  cp += ipp->ipp_ipv4_options_len;
13569 13570          }
13570 13571          ipha->ipha_version_and_hdr_length =
13571 13572              (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13572 13573  
13573 13574          ASSERT((int)(cp - buf) == buf_len);
13574 13575  }
13575 13576  
13576 13577  /* Allocate the private structure */
13577 13578  static int
13578 13579  ip_priv_alloc(void **bufp)
13579 13580  {
13580 13581          void    *buf;
13581 13582  
13582 13583          if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13583 13584                  return (ENOMEM);
13584 13585  
13585 13586          *bufp = buf;
13586 13587          return (0);
13587 13588  }
13588 13589  
13589 13590  /* Function to delete the private structure */
13590 13591  void
13591 13592  ip_priv_free(void *buf)
13592 13593  {
13593 13594          ASSERT(buf != NULL);
13594 13595          kmem_free(buf, sizeof (ip_priv_t));
13595 13596  }
13596 13597  
13597 13598  /*
13598 13599   * The entry point for IPPF processing.
13599 13600   * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13600 13601   * routine just returns.
13601 13602   *
13602 13603   * When called, ip_process generates an ipp_packet_t structure
13603 13604   * which holds the state information for this packet and invokes the
13604 13605   * the classifier (via ipp_packet_process). The classification, depending on
13605 13606   * configured filters, results in a list of actions for this packet. Invoking
13606 13607   * an action may cause the packet to be dropped, in which case we return NULL.
13607 13608   * proc indicates the callout position for
13608 13609   * this packet and ill is the interface this packet arrived on or will leave
13609 13610   * on (inbound and outbound resp.).
13610 13611   *
13611 13612   * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13612 13613   * on the ill corrsponding to the destination IP address.
13613 13614   */
13614 13615  mblk_t *
13615 13616  ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13616 13617  {
13617 13618          ip_priv_t       *priv;
13618 13619          ipp_action_id_t aid;
13619 13620          int             rc = 0;
13620 13621          ipp_packet_t    *pp;
13621 13622  
13622 13623          /* If the classifier is not loaded, return  */
13623 13624          if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13624 13625                  return (mp);
13625 13626          }
13626 13627  
13627 13628          ASSERT(mp != NULL);
13628 13629  
13629 13630          /* Allocate the packet structure */
13630 13631          rc = ipp_packet_alloc(&pp, "ip", aid);
13631 13632          if (rc != 0)
13632 13633                  goto drop;
13633 13634  
13634 13635          /* Allocate the private structure */
13635 13636          rc = ip_priv_alloc((void **)&priv);
13636 13637          if (rc != 0) {
13637 13638                  ipp_packet_free(pp);
13638 13639                  goto drop;
13639 13640          }
13640 13641          priv->proc = proc;
13641 13642          priv->ill_index = ill_get_upper_ifindex(rill);
13642 13643  
13643 13644          ipp_packet_set_private(pp, priv, ip_priv_free);
13644 13645          ipp_packet_set_data(pp, mp);
13645 13646  
13646 13647          /* Invoke the classifier */
13647 13648          rc = ipp_packet_process(&pp);
13648 13649          if (pp != NULL) {
13649 13650                  mp = ipp_packet_get_data(pp);
13650 13651                  ipp_packet_free(pp);
13651 13652                  if (rc != 0)
13652 13653                          goto drop;
13653 13654                  return (mp);
13654 13655          } else {
13655 13656                  /* No mp to trace in ip_drop_input/ip_drop_output  */
13656 13657                  mp = NULL;
13657 13658          }
13658 13659  drop:
13659 13660          if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13660 13661                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13661 13662                  ip_drop_input("ip_process", mp, ill);
13662 13663          } else {
13663 13664                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13664 13665                  ip_drop_output("ip_process", mp, ill);
13665 13666          }
13666 13667          freemsg(mp);
13667 13668          return (NULL);
13668 13669  }
13669 13670  
13670 13671  /*
13671 13672   * Propagate a multicast group membership operation (add/drop) on
13672 13673   * all the interfaces crossed by the related multirt routes.
13673 13674   * The call is considered successful if the operation succeeds
13674 13675   * on at least one interface.
13675 13676   *
13676 13677   * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the
13677 13678   * multicast addresses with the ire argument being the first one.
13678 13679   * We walk the bucket to find all the of those.
13679 13680   *
13680 13681   * Common to IPv4 and IPv6.
13681 13682   */
13682 13683  static int
13683 13684  ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
13684 13685      const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
13685 13686      ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group,
13686 13687      mcast_record_t fmode, const in6_addr_t *v6src)
13687 13688  {
13688 13689          ire_t           *ire_gw;
13689 13690          irb_t           *irb;
13690 13691          int             ifindex;
13691 13692          int             error = 0;
13692 13693          int             result;
13693 13694          ip_stack_t      *ipst = ire->ire_ipst;
13694 13695          ipaddr_t        group;
13695 13696          boolean_t       isv6;
13696 13697          int             match_flags;
13697 13698  
13698 13699          if (IN6_IS_ADDR_V4MAPPED(v6group)) {
13699 13700                  IN6_V4MAPPED_TO_IPADDR(v6group, group);
13700 13701                  isv6 = B_FALSE;
13701 13702          } else {
13702 13703                  isv6 = B_TRUE;
13703 13704          }
13704 13705  
13705 13706          irb = ire->ire_bucket;
13706 13707          ASSERT(irb != NULL);
13707 13708  
13708 13709          result = 0;
13709 13710          irb_refhold(irb);
13710 13711          for (; ire != NULL; ire = ire->ire_next) {
13711 13712                  if ((ire->ire_flags & RTF_MULTIRT) == 0)
13712 13713                          continue;
13713 13714  
13714 13715                  /* We handle -ifp routes by matching on the ill if set */
13715 13716                  match_flags = MATCH_IRE_TYPE;
13716 13717                  if (ire->ire_ill != NULL)
13717 13718                          match_flags |= MATCH_IRE_ILL;
13718 13719  
13719 13720                  if (isv6) {
13720 13721                          if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group))
13721 13722                                  continue;
13722 13723  
13723 13724                          ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6,
13724 13725                              0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13725 13726                              match_flags, 0, ipst, NULL);
13726 13727                  } else {
13727 13728                          if (ire->ire_addr != group)
13728 13729                                  continue;
13729 13730  
13730 13731                          ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr,
13731 13732                              0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13732 13733                              match_flags, 0, ipst, NULL);
13733 13734                  }
13734 13735                  /* No interface route exists for the gateway; skip this ire. */
13735 13736                  if (ire_gw == NULL)
13736 13737                          continue;
13737 13738                  if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
13738 13739                          ire_refrele(ire_gw);
13739 13740                          continue;
13740 13741                  }
13741 13742                  ASSERT(ire_gw->ire_ill != NULL);        /* IRE_INTERFACE */
13742 13743                  ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex;
13743 13744  
13744 13745                  /*
13745 13746                   * The operation is considered a success if
13746 13747                   * it succeeds at least once on any one interface.
13747 13748                   */
13748 13749                  error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex,
13749 13750                      fmode, v6src);
13750 13751                  if (error == 0)
13751 13752                          result = CGTP_MCAST_SUCCESS;
13752 13753  
13753 13754                  ire_refrele(ire_gw);
13754 13755          }
13755 13756          irb_refrele(irb);
13756 13757          /*
13757 13758           * Consider the call as successful if we succeeded on at least
13758 13759           * one interface. Otherwise, return the last encountered error.
13759 13760           */
13760 13761          return (result == CGTP_MCAST_SUCCESS ? 0 : error);
13761 13762  }
13762 13763  
13763 13764  /*
13764 13765   * Return the expected CGTP hooks version number.
13765 13766   */
13766 13767  int
13767 13768  ip_cgtp_filter_supported(void)
13768 13769  {
13769 13770          return (ip_cgtp_filter_rev);
13770 13771  }
13771 13772  
13772 13773  /*
13773 13774   * CGTP hooks can be registered by invoking this function.
13774 13775   * Checks that the version number matches.
13775 13776   */
13776 13777  int
13777 13778  ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops)
13778 13779  {
13779 13780          netstack_t *ns;
13780 13781          ip_stack_t *ipst;
13781 13782  
13782 13783          if (ops->cfo_filter_rev != CGTP_FILTER_REV)
13783 13784                  return (ENOTSUP);
13784 13785  
13785 13786          ns = netstack_find_by_stackid(stackid);
13786 13787          if (ns == NULL)
13787 13788                  return (EINVAL);
13788 13789          ipst = ns->netstack_ip;
13789 13790          ASSERT(ipst != NULL);
13790 13791  
13791 13792          if (ipst->ips_ip_cgtp_filter_ops != NULL) {
13792 13793                  netstack_rele(ns);
13793 13794                  return (EALREADY);
13794 13795          }
13795 13796  
13796 13797          ipst->ips_ip_cgtp_filter_ops = ops;
13797 13798  
13798 13799          ill_set_inputfn_all(ipst);
13799 13800  
13800 13801          netstack_rele(ns);
13801 13802          return (0);
13802 13803  }
13803 13804  
13804 13805  /*
13805 13806   * CGTP hooks can be unregistered by invoking this function.
13806 13807   * Returns ENXIO if there was no registration.
13807 13808   * Returns EBUSY if the ndd variable has not been turned off.
13808 13809   */
13809 13810  int
13810 13811  ip_cgtp_filter_unregister(netstackid_t stackid)
13811 13812  {
13812 13813          netstack_t *ns;
13813 13814          ip_stack_t *ipst;
13814 13815  
13815 13816          ns = netstack_find_by_stackid(stackid);
13816 13817          if (ns == NULL)
13817 13818                  return (EINVAL);
13818 13819          ipst = ns->netstack_ip;
13819 13820          ASSERT(ipst != NULL);
13820 13821  
13821 13822          if (ipst->ips_ip_cgtp_filter) {
13822 13823                  netstack_rele(ns);
13823 13824                  return (EBUSY);
13824 13825          }
13825 13826  
13826 13827          if (ipst->ips_ip_cgtp_filter_ops == NULL) {
13827 13828                  netstack_rele(ns);
13828 13829                  return (ENXIO);
13829 13830          }
13830 13831          ipst->ips_ip_cgtp_filter_ops = NULL;
13831 13832  
13832 13833          ill_set_inputfn_all(ipst);
13833 13834  
13834 13835          netstack_rele(ns);
13835 13836          return (0);
13836 13837  }
13837 13838  
13838 13839  /*
13839 13840   * Check whether there is a CGTP filter registration.
13840 13841   * Returns non-zero if there is a registration, otherwise returns zero.
13841 13842   * Note: returns zero if bad stackid.
13842 13843   */
13843 13844  int
13844 13845  ip_cgtp_filter_is_registered(netstackid_t stackid)
13845 13846  {
13846 13847          netstack_t *ns;
13847 13848          ip_stack_t *ipst;
13848 13849          int ret;
13849 13850  
13850 13851          ns = netstack_find_by_stackid(stackid);
13851 13852          if (ns == NULL)
13852 13853                  return (0);
13853 13854          ipst = ns->netstack_ip;
13854 13855          ASSERT(ipst != NULL);
13855 13856  
13856 13857          if (ipst->ips_ip_cgtp_filter_ops != NULL)
13857 13858                  ret = 1;
13858 13859          else
13859 13860                  ret = 0;
13860 13861  
13861 13862          netstack_rele(ns);
13862 13863          return (ret);
13863 13864  }
13864 13865  
13865 13866  static int
13866 13867  ip_squeue_switch(int val)
13867 13868  {
13868 13869          int rval;
13869 13870  
13870 13871          switch (val) {
13871 13872          case IP_SQUEUE_ENTER_NODRAIN:
13872 13873                  rval = SQ_NODRAIN;
13873 13874                  break;
13874 13875          case IP_SQUEUE_ENTER:
13875 13876                  rval = SQ_PROCESS;
13876 13877                  break;
13877 13878          case IP_SQUEUE_FILL:
13878 13879          default:
13879 13880                  rval = SQ_FILL;
13880 13881                  break;
13881 13882          }
13882 13883          return (rval);
13883 13884  }
13884 13885  
13885 13886  static void *
13886 13887  ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13887 13888  {
13888 13889          kstat_t *ksp;
13889 13890  
13890 13891          ip_stat_t template = {
13891 13892                  { "ip_udp_fannorm",             KSTAT_DATA_UINT64 },
13892 13893                  { "ip_udp_fanmb",               KSTAT_DATA_UINT64 },
13893 13894                  { "ip_recv_pullup",             KSTAT_DATA_UINT64 },
13894 13895                  { "ip_db_ref",                  KSTAT_DATA_UINT64 },
13895 13896                  { "ip_notaligned",              KSTAT_DATA_UINT64 },
13896 13897                  { "ip_multimblk",               KSTAT_DATA_UINT64 },
13897 13898                  { "ip_opt",                     KSTAT_DATA_UINT64 },
13898 13899                  { "ipsec_proto_ahesp",          KSTAT_DATA_UINT64 },
13899 13900                  { "ip_conn_flputbq",            KSTAT_DATA_UINT64 },
13900 13901                  { "ip_conn_walk_drain",         KSTAT_DATA_UINT64 },
13901 13902                  { "ip_out_sw_cksum",            KSTAT_DATA_UINT64 },
13902 13903                  { "ip_out_sw_cksum_bytes",      KSTAT_DATA_UINT64 },
13903 13904                  { "ip_in_sw_cksum",             KSTAT_DATA_UINT64 },
13904 13905                  { "ip_ire_reclaim_calls",       KSTAT_DATA_UINT64 },
13905 13906                  { "ip_ire_reclaim_deleted",     KSTAT_DATA_UINT64 },
13906 13907                  { "ip_nce_reclaim_calls",       KSTAT_DATA_UINT64 },
13907 13908                  { "ip_nce_reclaim_deleted",     KSTAT_DATA_UINT64 },
13908 13909                  { "ip_dce_reclaim_calls",       KSTAT_DATA_UINT64 },
13909 13910                  { "ip_dce_reclaim_deleted",     KSTAT_DATA_UINT64 },
13910 13911                  { "ip_tcp_in_full_hw_cksum_err",        KSTAT_DATA_UINT64 },
13911 13912                  { "ip_tcp_in_part_hw_cksum_err",        KSTAT_DATA_UINT64 },
13912 13913                  { "ip_tcp_in_sw_cksum_err",             KSTAT_DATA_UINT64 },
13913 13914                  { "ip_udp_in_full_hw_cksum_err",        KSTAT_DATA_UINT64 },
13914 13915                  { "ip_udp_in_part_hw_cksum_err",        KSTAT_DATA_UINT64 },
13915 13916                  { "ip_udp_in_sw_cksum_err",     KSTAT_DATA_UINT64 },
13916 13917                  { "conn_in_recvdstaddr",        KSTAT_DATA_UINT64 },
13917 13918                  { "conn_in_recvopts",           KSTAT_DATA_UINT64 },
13918 13919                  { "conn_in_recvif",             KSTAT_DATA_UINT64 },
13919 13920                  { "conn_in_recvslla",           KSTAT_DATA_UINT64 },
13920 13921                  { "conn_in_recvucred",          KSTAT_DATA_UINT64 },
13921 13922                  { "conn_in_recvttl",            KSTAT_DATA_UINT64 },
13922 13923                  { "conn_in_recvhopopts",        KSTAT_DATA_UINT64 },
13923 13924                  { "conn_in_recvhoplimit",       KSTAT_DATA_UINT64 },
13924 13925                  { "conn_in_recvdstopts",        KSTAT_DATA_UINT64 },
13925 13926                  { "conn_in_recvrthdrdstopts",   KSTAT_DATA_UINT64 },
13926 13927                  { "conn_in_recvrthdr",          KSTAT_DATA_UINT64 },
13927 13928                  { "conn_in_recvpktinfo",        KSTAT_DATA_UINT64 },
13928 13929                  { "conn_in_recvtclass",         KSTAT_DATA_UINT64 },
13929 13930                  { "conn_in_timestamp",          KSTAT_DATA_UINT64 },
13930 13931          };
13931 13932  
13932 13933          ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13933 13934              KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13934 13935              KSTAT_FLAG_VIRTUAL, stackid);
13935 13936  
13936 13937          if (ksp == NULL)
13937 13938                  return (NULL);
13938 13939  
13939 13940          bcopy(&template, ip_statisticsp, sizeof (template));
13940 13941          ksp->ks_data = (void *)ip_statisticsp;
13941 13942          ksp->ks_private = (void *)(uintptr_t)stackid;
13942 13943  
13943 13944          kstat_install(ksp);
13944 13945          return (ksp);
13945 13946  }
13946 13947  
13947 13948  static void
13948 13949  ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13949 13950  {
13950 13951          if (ksp != NULL) {
13951 13952                  ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13952 13953                  kstat_delete_netstack(ksp, stackid);
13953 13954          }
13954 13955  }
13955 13956  
13956 13957  static void *
13957 13958  ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
13958 13959  {
13959 13960          kstat_t *ksp;
13960 13961  
13961 13962          ip_named_kstat_t template = {
13962 13963                  { "forwarding",         KSTAT_DATA_UINT32, 0 },
13963 13964                  { "defaultTTL",         KSTAT_DATA_UINT32, 0 },
13964 13965                  { "inReceives",         KSTAT_DATA_UINT64, 0 },
13965 13966                  { "inHdrErrors",        KSTAT_DATA_UINT32, 0 },
13966 13967                  { "inAddrErrors",       KSTAT_DATA_UINT32, 0 },
13967 13968                  { "forwDatagrams",      KSTAT_DATA_UINT64, 0 },
13968 13969                  { "inUnknownProtos",    KSTAT_DATA_UINT32, 0 },
13969 13970                  { "inDiscards",         KSTAT_DATA_UINT32, 0 },
13970 13971                  { "inDelivers",         KSTAT_DATA_UINT64, 0 },
13971 13972                  { "outRequests",        KSTAT_DATA_UINT64, 0 },
13972 13973                  { "outDiscards",        KSTAT_DATA_UINT32, 0 },
13973 13974                  { "outNoRoutes",        KSTAT_DATA_UINT32, 0 },
13974 13975                  { "reasmTimeout",       KSTAT_DATA_UINT32, 0 },
13975 13976                  { "reasmReqds",         KSTAT_DATA_UINT32, 0 },
13976 13977                  { "reasmOKs",           KSTAT_DATA_UINT32, 0 },
13977 13978                  { "reasmFails",         KSTAT_DATA_UINT32, 0 },
13978 13979                  { "fragOKs",            KSTAT_DATA_UINT32, 0 },
13979 13980                  { "fragFails",          KSTAT_DATA_UINT32, 0 },
13980 13981                  { "fragCreates",        KSTAT_DATA_UINT32, 0 },
13981 13982                  { "addrEntrySize",      KSTAT_DATA_INT32, 0 },
13982 13983                  { "routeEntrySize",     KSTAT_DATA_INT32, 0 },
13983 13984                  { "netToMediaEntrySize",        KSTAT_DATA_INT32, 0 },
13984 13985                  { "routingDiscards",    KSTAT_DATA_UINT32, 0 },
13985 13986                  { "inErrs",             KSTAT_DATA_UINT32, 0 },
13986 13987                  { "noPorts",            KSTAT_DATA_UINT32, 0 },
13987 13988                  { "inCksumErrs",        KSTAT_DATA_UINT32, 0 },
13988 13989                  { "reasmDuplicates",    KSTAT_DATA_UINT32, 0 },
13989 13990                  { "reasmPartDups",      KSTAT_DATA_UINT32, 0 },
13990 13991                  { "forwProhibits",      KSTAT_DATA_UINT32, 0 },
13991 13992                  { "udpInCksumErrs",     KSTAT_DATA_UINT32, 0 },
13992 13993                  { "udpInOverflows",     KSTAT_DATA_UINT32, 0 },
13993 13994                  { "rawipInOverflows",   KSTAT_DATA_UINT32, 0 },
13994 13995                  { "ipsecInSucceeded",   KSTAT_DATA_UINT32, 0 },
13995 13996                  { "ipsecInFailed",      KSTAT_DATA_INT32, 0 },
13996 13997                  { "memberEntrySize",    KSTAT_DATA_INT32, 0 },
13997 13998                  { "inIPv6",             KSTAT_DATA_UINT32, 0 },
13998 13999                  { "outIPv6",            KSTAT_DATA_UINT32, 0 },
13999 14000                  { "outSwitchIPv6",      KSTAT_DATA_UINT32, 0 },
14000 14001          };
14001 14002  
14002 14003          ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
14003 14004              NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
14004 14005          if (ksp == NULL || ksp->ks_data == NULL)
14005 14006                  return (NULL);
14006 14007  
14007 14008          template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
14008 14009          template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
14009 14010          template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14010 14011          template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
14011 14012          template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
14012 14013  
14013 14014          template.netToMediaEntrySize.value.i32 =
14014 14015              sizeof (mib2_ipNetToMediaEntry_t);
14015 14016  
14016 14017          template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
14017 14018  
14018 14019          bcopy(&template, ksp->ks_data, sizeof (template));
14019 14020          ksp->ks_update = ip_kstat_update;
14020 14021          ksp->ks_private = (void *)(uintptr_t)stackid;
14021 14022  
14022 14023          kstat_install(ksp);
14023 14024          return (ksp);
14024 14025  }
14025 14026  
14026 14027  static void
14027 14028  ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14028 14029  {
14029 14030          if (ksp != NULL) {
14030 14031                  ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14031 14032                  kstat_delete_netstack(ksp, stackid);
14032 14033          }
14033 14034  }
14034 14035  
14035 14036  static int
14036 14037  ip_kstat_update(kstat_t *kp, int rw)
14037 14038  {
14038 14039          ip_named_kstat_t *ipkp;
14039 14040          mib2_ipIfStatsEntry_t ipmib;
14040 14041          ill_walk_context_t ctx;
14041 14042          ill_t *ill;
14042 14043          netstackid_t    stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14043 14044          netstack_t      *ns;
14044 14045          ip_stack_t      *ipst;
14045 14046  
14046 14047          if (kp == NULL || kp->ks_data == NULL)
14047 14048                  return (EIO);
14048 14049  
14049 14050          if (rw == KSTAT_WRITE)
14050 14051                  return (EACCES);
14051 14052  
14052 14053          ns = netstack_find_by_stackid(stackid);
14053 14054          if (ns == NULL)
14054 14055                  return (-1);
14055 14056          ipst = ns->netstack_ip;
14056 14057          if (ipst == NULL) {
14057 14058                  netstack_rele(ns);
14058 14059                  return (-1);
14059 14060          }
14060 14061          ipkp = (ip_named_kstat_t *)kp->ks_data;
14061 14062  
14062 14063          bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
14063 14064          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14064 14065          ill = ILL_START_WALK_V4(&ctx, ipst);
14065 14066          for (; ill != NULL; ill = ill_next(&ctx, ill))
14066 14067                  ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
14067 14068          rw_exit(&ipst->ips_ill_g_lock);
14068 14069  
14069 14070          ipkp->forwarding.value.ui32 =           ipmib.ipIfStatsForwarding;
14070 14071          ipkp->defaultTTL.value.ui32 =           ipmib.ipIfStatsDefaultTTL;
14071 14072          ipkp->inReceives.value.ui64 =           ipmib.ipIfStatsHCInReceives;
14072 14073          ipkp->inHdrErrors.value.ui32 =          ipmib.ipIfStatsInHdrErrors;
14073 14074          ipkp->inAddrErrors.value.ui32 =         ipmib.ipIfStatsInAddrErrors;
14074 14075          ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
14075 14076          ipkp->inUnknownProtos.value.ui32 =      ipmib.ipIfStatsInUnknownProtos;
14076 14077          ipkp->inDiscards.value.ui32 =           ipmib.ipIfStatsInDiscards;
14077 14078          ipkp->inDelivers.value.ui64 =           ipmib.ipIfStatsHCInDelivers;
14078 14079          ipkp->outRequests.value.ui64 =          ipmib.ipIfStatsHCOutRequests;
14079 14080          ipkp->outDiscards.value.ui32 =          ipmib.ipIfStatsOutDiscards;
14080 14081          ipkp->outNoRoutes.value.ui32 =          ipmib.ipIfStatsOutNoRoutes;
14081 14082          ipkp->reasmTimeout.value.ui32 =         ipst->ips_ip_reassembly_timeout;
14082 14083          ipkp->reasmReqds.value.ui32 =           ipmib.ipIfStatsReasmReqds;
14083 14084          ipkp->reasmOKs.value.ui32 =             ipmib.ipIfStatsReasmOKs;
14084 14085          ipkp->reasmFails.value.ui32 =           ipmib.ipIfStatsReasmFails;
14085 14086          ipkp->fragOKs.value.ui32 =              ipmib.ipIfStatsOutFragOKs;
14086 14087          ipkp->fragFails.value.ui32 =            ipmib.ipIfStatsOutFragFails;
14087 14088          ipkp->fragCreates.value.ui32 =          ipmib.ipIfStatsOutFragCreates;
14088 14089  
14089 14090          ipkp->routingDiscards.value.ui32 =      0;
14090 14091          ipkp->inErrs.value.ui32 =               ipmib.tcpIfStatsInErrs;
14091 14092          ipkp->noPorts.value.ui32 =              ipmib.udpIfStatsNoPorts;
14092 14093          ipkp->inCksumErrs.value.ui32 =          ipmib.ipIfStatsInCksumErrs;
14093 14094          ipkp->reasmDuplicates.value.ui32 =      ipmib.ipIfStatsReasmDuplicates;
14094 14095          ipkp->reasmPartDups.value.ui32 =        ipmib.ipIfStatsReasmPartDups;
14095 14096          ipkp->forwProhibits.value.ui32 =        ipmib.ipIfStatsForwProhibits;
14096 14097          ipkp->udpInCksumErrs.value.ui32 =       ipmib.udpIfStatsInCksumErrs;
14097 14098          ipkp->udpInOverflows.value.ui32 =       ipmib.udpIfStatsInOverflows;
14098 14099          ipkp->rawipInOverflows.value.ui32 =     ipmib.rawipIfStatsInOverflows;
14099 14100          ipkp->ipsecInSucceeded.value.ui32 =     ipmib.ipsecIfStatsInSucceeded;
14100 14101          ipkp->ipsecInFailed.value.i32 =         ipmib.ipsecIfStatsInFailed;
14101 14102  
14102 14103          ipkp->inIPv6.value.ui32 =       ipmib.ipIfStatsInWrongIPVersion;
14103 14104          ipkp->outIPv6.value.ui32 =      ipmib.ipIfStatsOutWrongIPVersion;
14104 14105          ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
14105 14106  
14106 14107          netstack_rele(ns);
14107 14108  
14108 14109          return (0);
14109 14110  }
14110 14111  
14111 14112  static void *
14112 14113  icmp_kstat_init(netstackid_t stackid)
14113 14114  {
14114 14115          kstat_t *ksp;
14115 14116  
14116 14117          icmp_named_kstat_t template = {
14117 14118                  { "inMsgs",             KSTAT_DATA_UINT32 },
14118 14119                  { "inErrors",           KSTAT_DATA_UINT32 },
14119 14120                  { "inDestUnreachs",     KSTAT_DATA_UINT32 },
14120 14121                  { "inTimeExcds",        KSTAT_DATA_UINT32 },
14121 14122                  { "inParmProbs",        KSTAT_DATA_UINT32 },
14122 14123                  { "inSrcQuenchs",       KSTAT_DATA_UINT32 },
14123 14124                  { "inRedirects",        KSTAT_DATA_UINT32 },
14124 14125                  { "inEchos",            KSTAT_DATA_UINT32 },
14125 14126                  { "inEchoReps",         KSTAT_DATA_UINT32 },
14126 14127                  { "inTimestamps",       KSTAT_DATA_UINT32 },
14127 14128                  { "inTimestampReps",    KSTAT_DATA_UINT32 },
14128 14129                  { "inAddrMasks",        KSTAT_DATA_UINT32 },
14129 14130                  { "inAddrMaskReps",     KSTAT_DATA_UINT32 },
14130 14131                  { "outMsgs",            KSTAT_DATA_UINT32 },
14131 14132                  { "outErrors",          KSTAT_DATA_UINT32 },
14132 14133                  { "outDestUnreachs",    KSTAT_DATA_UINT32 },
14133 14134                  { "outTimeExcds",       KSTAT_DATA_UINT32 },
14134 14135                  { "outParmProbs",       KSTAT_DATA_UINT32 },
14135 14136                  { "outSrcQuenchs",      KSTAT_DATA_UINT32 },
14136 14137                  { "outRedirects",       KSTAT_DATA_UINT32 },
14137 14138                  { "outEchos",           KSTAT_DATA_UINT32 },
14138 14139                  { "outEchoReps",        KSTAT_DATA_UINT32 },
14139 14140                  { "outTimestamps",      KSTAT_DATA_UINT32 },
14140 14141                  { "outTimestampReps",   KSTAT_DATA_UINT32 },
14141 14142                  { "outAddrMasks",       KSTAT_DATA_UINT32 },
14142 14143                  { "outAddrMaskReps",    KSTAT_DATA_UINT32 },
14143 14144                  { "inChksumErrs",       KSTAT_DATA_UINT32 },
14144 14145                  { "inUnknowns",         KSTAT_DATA_UINT32 },
14145 14146                  { "inFragNeeded",       KSTAT_DATA_UINT32 },
14146 14147                  { "outFragNeeded",      KSTAT_DATA_UINT32 },
14147 14148                  { "outDrops",           KSTAT_DATA_UINT32 },
14148 14149                  { "inOverFlows",        KSTAT_DATA_UINT32 },
14149 14150                  { "inBadRedirects",     KSTAT_DATA_UINT32 },
14150 14151          };
14151 14152  
14152 14153          ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
14153 14154              NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
14154 14155          if (ksp == NULL || ksp->ks_data == NULL)
14155 14156                  return (NULL);
14156 14157  
14157 14158          bcopy(&template, ksp->ks_data, sizeof (template));
14158 14159  
14159 14160          ksp->ks_update = icmp_kstat_update;
14160 14161          ksp->ks_private = (void *)(uintptr_t)stackid;
14161 14162  
14162 14163          kstat_install(ksp);
14163 14164          return (ksp);
14164 14165  }
14165 14166  
14166 14167  static void
14167 14168  icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14168 14169  {
14169 14170          if (ksp != NULL) {
14170 14171                  ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14171 14172                  kstat_delete_netstack(ksp, stackid);
14172 14173          }
14173 14174  }
14174 14175  
14175 14176  static int
14176 14177  icmp_kstat_update(kstat_t *kp, int rw)
14177 14178  {
14178 14179          icmp_named_kstat_t *icmpkp;
14179 14180          netstackid_t    stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14180 14181          netstack_t      *ns;
14181 14182          ip_stack_t      *ipst;
14182 14183  
14183 14184          if ((kp == NULL) || (kp->ks_data == NULL))
14184 14185                  return (EIO);
14185 14186  
14186 14187          if (rw == KSTAT_WRITE)
14187 14188                  return (EACCES);
14188 14189  
14189 14190          ns = netstack_find_by_stackid(stackid);
14190 14191          if (ns == NULL)
14191 14192                  return (-1);
14192 14193          ipst = ns->netstack_ip;
14193 14194          if (ipst == NULL) {
14194 14195                  netstack_rele(ns);
14195 14196                  return (-1);
14196 14197          }
14197 14198          icmpkp = (icmp_named_kstat_t *)kp->ks_data;
14198 14199  
14199 14200          icmpkp->inMsgs.value.ui32 =         ipst->ips_icmp_mib.icmpInMsgs;
14200 14201          icmpkp->inErrors.value.ui32 =       ipst->ips_icmp_mib.icmpInErrors;
14201 14202          icmpkp->inDestUnreachs.value.ui32 =
14202 14203              ipst->ips_icmp_mib.icmpInDestUnreachs;
14203 14204          icmpkp->inTimeExcds.value.ui32 =    ipst->ips_icmp_mib.icmpInTimeExcds;
14204 14205          icmpkp->inParmProbs.value.ui32 =    ipst->ips_icmp_mib.icmpInParmProbs;
14205 14206          icmpkp->inSrcQuenchs.value.ui32 =   ipst->ips_icmp_mib.icmpInSrcQuenchs;
14206 14207          icmpkp->inRedirects.value.ui32 =    ipst->ips_icmp_mib.icmpInRedirects;
14207 14208          icmpkp->inEchos.value.ui32 =        ipst->ips_icmp_mib.icmpInEchos;
14208 14209          icmpkp->inEchoReps.value.ui32 =     ipst->ips_icmp_mib.icmpInEchoReps;
14209 14210          icmpkp->inTimestamps.value.ui32 =   ipst->ips_icmp_mib.icmpInTimestamps;
14210 14211          icmpkp->inTimestampReps.value.ui32 =
14211 14212              ipst->ips_icmp_mib.icmpInTimestampReps;
14212 14213          icmpkp->inAddrMasks.value.ui32 =    ipst->ips_icmp_mib.icmpInAddrMasks;
14213 14214          icmpkp->inAddrMaskReps.value.ui32 =
14214 14215              ipst->ips_icmp_mib.icmpInAddrMaskReps;
14215 14216          icmpkp->outMsgs.value.ui32 =        ipst->ips_icmp_mib.icmpOutMsgs;
14216 14217          icmpkp->outErrors.value.ui32 =      ipst->ips_icmp_mib.icmpOutErrors;
14217 14218          icmpkp->outDestUnreachs.value.ui32 =
14218 14219              ipst->ips_icmp_mib.icmpOutDestUnreachs;
14219 14220          icmpkp->outTimeExcds.value.ui32 =   ipst->ips_icmp_mib.icmpOutTimeExcds;
14220 14221          icmpkp->outParmProbs.value.ui32 =   ipst->ips_icmp_mib.icmpOutParmProbs;
14221 14222          icmpkp->outSrcQuenchs.value.ui32 =
14222 14223              ipst->ips_icmp_mib.icmpOutSrcQuenchs;
14223 14224          icmpkp->outRedirects.value.ui32 =   ipst->ips_icmp_mib.icmpOutRedirects;
14224 14225          icmpkp->outEchos.value.ui32 =       ipst->ips_icmp_mib.icmpOutEchos;
14225 14226          icmpkp->outEchoReps.value.ui32 =    ipst->ips_icmp_mib.icmpOutEchoReps;
14226 14227          icmpkp->outTimestamps.value.ui32 =
14227 14228              ipst->ips_icmp_mib.icmpOutTimestamps;
14228 14229          icmpkp->outTimestampReps.value.ui32 =
14229 14230              ipst->ips_icmp_mib.icmpOutTimestampReps;
14230 14231          icmpkp->outAddrMasks.value.ui32 =
14231 14232              ipst->ips_icmp_mib.icmpOutAddrMasks;
14232 14233          icmpkp->outAddrMaskReps.value.ui32 =
14233 14234              ipst->ips_icmp_mib.icmpOutAddrMaskReps;
14234 14235          icmpkp->inCksumErrs.value.ui32 =    ipst->ips_icmp_mib.icmpInCksumErrs;
14235 14236          icmpkp->inUnknowns.value.ui32 =     ipst->ips_icmp_mib.icmpInUnknowns;
14236 14237          icmpkp->inFragNeeded.value.ui32 =   ipst->ips_icmp_mib.icmpInFragNeeded;
14237 14238          icmpkp->outFragNeeded.value.ui32 =
14238 14239              ipst->ips_icmp_mib.icmpOutFragNeeded;
14239 14240          icmpkp->outDrops.value.ui32 =       ipst->ips_icmp_mib.icmpOutDrops;
14240 14241          icmpkp->inOverflows.value.ui32 =    ipst->ips_icmp_mib.icmpInOverflows;
14241 14242          icmpkp->inBadRedirects.value.ui32 =
14242 14243              ipst->ips_icmp_mib.icmpInBadRedirects;
14243 14244  
14244 14245          netstack_rele(ns);
14245 14246          return (0);
14246 14247  }
14247 14248  
14248 14249  /*
14249 14250   * This is the fanout function for raw socket opened for SCTP.  Note
14250 14251   * that it is called after SCTP checks that there is no socket which
14251 14252   * wants a packet.  Then before SCTP handles this out of the blue packet,
14252 14253   * this function is called to see if there is any raw socket for SCTP.
14253 14254   * If there is and it is bound to the correct address, the packet will
14254 14255   * be sent to that socket.  Note that only one raw socket can be bound to
14255 14256   * a port.  This is assured in ipcl_sctp_hash_insert();
14256 14257   */
14257 14258  void
14258 14259  ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
14259 14260      ip_recv_attr_t *ira)
14260 14261  {
14261 14262          conn_t          *connp;
14262 14263          queue_t         *rq;
14263 14264          boolean_t       secure;
14264 14265          ill_t           *ill = ira->ira_ill;
14265 14266          ip_stack_t      *ipst = ill->ill_ipst;
14266 14267          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
14267 14268          sctp_stack_t    *sctps = ipst->ips_netstack->netstack_sctp;
14268 14269          iaflags_t       iraflags = ira->ira_flags;
14269 14270          ill_t           *rill = ira->ira_rill;
14270 14271  
14271 14272          secure = iraflags & IRAF_IPSEC_SECURE;
14272 14273  
14273 14274          connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
14274 14275              ira, ipst);
14275 14276          if (connp == NULL) {
14276 14277                  /*
14277 14278                   * Although raw sctp is not summed, OOB chunks must be.
14278 14279                   * Drop the packet here if the sctp checksum failed.
14279 14280                   */
14280 14281                  if (iraflags & IRAF_SCTP_CSUM_ERR) {
14281 14282                          SCTPS_BUMP_MIB(sctps, sctpChecksumError);
14282 14283                          freemsg(mp);
14283 14284                          return;
14284 14285                  }
14285 14286                  ira->ira_ill = ira->ira_rill = NULL;
14286 14287                  sctp_ootb_input(mp, ira, ipst);
14287 14288                  ira->ira_ill = ill;
14288 14289                  ira->ira_rill = rill;
14289 14290                  return;
14290 14291          }
14291 14292          rq = connp->conn_rq;
14292 14293          if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
14293 14294                  CONN_DEC_REF(connp);
14294 14295                  BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
14295 14296                  freemsg(mp);
14296 14297                  return;
14297 14298          }
14298 14299          if (((iraflags & IRAF_IS_IPV4) ?
14299 14300              CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
14300 14301              CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
14301 14302              secure) {
14302 14303                  mp = ipsec_check_inbound_policy(mp, connp, ipha,
14303 14304                      ip6h, ira);
14304 14305                  if (mp == NULL) {
14305 14306                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
14306 14307                          /* Note that mp is NULL */
14307 14308                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
14308 14309                          CONN_DEC_REF(connp);
14309 14310                          return;
14310 14311                  }
14311 14312          }
14312 14313  
14313 14314          if (iraflags & IRAF_ICMP_ERROR) {
14314 14315                  (connp->conn_recvicmp)(connp, mp, NULL, ira);
14315 14316          } else {
14316 14317                  ill_t *rill = ira->ira_rill;
14317 14318  
14318 14319                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
14319 14320                  /* This is the SOCK_RAW, IPPROTO_SCTP case. */
14320 14321                  ira->ira_ill = ira->ira_rill = NULL;
14321 14322                  (connp->conn_recv)(connp, mp, NULL, ira);
14322 14323                  ira->ira_ill = ill;
14323 14324                  ira->ira_rill = rill;
14324 14325          }
14325 14326          CONN_DEC_REF(connp);
14326 14327  }
14327 14328  
14328 14329  /*
14329 14330   * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
14330 14331   * header before the ip payload.
14331 14332   */
14332 14333  static void
14333 14334  ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
14334 14335  {
14335 14336          int len = (mp->b_wptr - mp->b_rptr);
14336 14337          mblk_t *ip_mp;
14337 14338  
14338 14339          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14339 14340          if (is_fp_mp || len != fp_mp_len) {
14340 14341                  if (len > fp_mp_len) {
14341 14342                          /*
14342 14343                           * fastpath header and ip header in the first mblk
14343 14344                           */
14344 14345                          mp->b_rptr += fp_mp_len;
14345 14346                  } else {
14346 14347                          /*
14347 14348                           * ip_xmit_attach_llhdr had to prepend an mblk to
14348 14349                           * attach the fastpath header before ip header.
14349 14350                           */
14350 14351                          ip_mp = mp->b_cont;
14351 14352                          freeb(mp);
14352 14353                          mp = ip_mp;
14353 14354                          mp->b_rptr += (fp_mp_len - len);
14354 14355                  }
14355 14356          } else {
14356 14357                  ip_mp = mp->b_cont;
14357 14358                  freeb(mp);
14358 14359                  mp = ip_mp;
14359 14360          }
14360 14361          ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
14361 14362          freemsg(mp);
14362 14363  }
14363 14364  
14364 14365  /*
14365 14366   * Normal post fragmentation function.
14366 14367   *
14367 14368   * Send a packet using the passed in nce. This handles both IPv4 and IPv6
14368 14369   * using the same state machine.
14369 14370   *
14370 14371   * We return an error on failure. In particular we return EWOULDBLOCK
14371 14372   * when the driver flow controls. In that case this ensures that ip_wsrv runs
14372 14373   * (currently by canputnext failure resulting in backenabling from GLD.)
14373 14374   * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
14374 14375   * indication that they can flow control until ip_wsrv() tells then to restart.
14375 14376   *
14376 14377   * If the nce passed by caller is incomplete, this function
14377 14378   * queues the packet and if necessary, sends ARP request and bails.
14378 14379   * If the Neighbor Cache passed is fully resolved, we simply prepend
14379 14380   * the link-layer header to the packet, do ipsec hw acceleration
14380 14381   * work if necessary, and send the packet out on the wire.
14381 14382   */
14382 14383  /* ARGSUSED6 */
14383 14384  int
14384 14385  ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
14385 14386      uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
14386 14387  {
14387 14388          queue_t         *wq;
14388 14389          ill_t           *ill = nce->nce_ill;
14389 14390          ip_stack_t      *ipst = ill->ill_ipst;
14390 14391          uint64_t        delta;
14391 14392          boolean_t       isv6 = ill->ill_isv6;
14392 14393          boolean_t       fp_mp;
14393 14394          ncec_t          *ncec = nce->nce_common;
14394 14395          int64_t         now = LBOLT_FASTPATH64;
14395 14396          boolean_t       is_probe;
14396 14397  
14397 14398          DTRACE_PROBE1(ip__xmit, nce_t *, nce);
14398 14399  
14399 14400          ASSERT(mp != NULL);
14400 14401          ASSERT(mp->b_datap->db_type == M_DATA);
14401 14402          ASSERT(pkt_len == msgdsize(mp));
14402 14403  
14403 14404          /*
14404 14405           * If we have already been here and are coming back after ARP/ND.
14405 14406           * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
14406 14407           * in that case since they have seen the packet when it came here
14407 14408           * the first time.
14408 14409           */
14409 14410          if (ixaflags & IXAF_NO_TRACE)
14410 14411                  goto sendit;
14411 14412  
14412 14413          if (ixaflags & IXAF_IS_IPV4) {
14413 14414                  ipha_t *ipha = (ipha_t *)mp->b_rptr;
14414 14415  
14415 14416                  ASSERT(!isv6);
14416 14417                  ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
14417 14418                  if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
14418 14419                      !(ixaflags & IXAF_NO_PFHOOK)) {
14419 14420                          int     error;
14420 14421  
14421 14422                          FW_HOOKS(ipst->ips_ip4_physical_out_event,
14422 14423                              ipst->ips_ipv4firewall_physical_out,
14423 14424                              NULL, ill, ipha, mp, mp, 0, ipst, error);
14424 14425                          DTRACE_PROBE1(ip4__physical__out__end,
14425 14426                              mblk_t *, mp);
14426 14427                          if (mp == NULL)
14427 14428                                  return (error);
14428 14429  
14429 14430                          /* The length could have changed */
14430 14431                          pkt_len = msgdsize(mp);
14431 14432                  }
14432 14433                  if (ipst->ips_ip4_observe.he_interested) {
14433 14434                          /*
14434 14435                           * Note that for TX the zoneid is the sending
14435 14436                           * zone, whether or not MLP is in play.
14436 14437                           * Since the szone argument is the IP zoneid (i.e.,
14437 14438                           * zero for exclusive-IP zones) and ipobs wants
14438 14439                           * the system zoneid, we map it here.
14439 14440                           */
14440 14441                          szone = IP_REAL_ZONEID(szone, ipst);
14441 14442  
14442 14443                          /*
14443 14444                           * On the outbound path the destination zone will be
14444 14445                           * unknown as we're sending this packet out on the
14445 14446                           * wire.
14446 14447                           */
14447 14448                          ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14448 14449                              ill, ipst);
14449 14450                  }
14450 14451                  DTRACE_IP7(send, mblk_t *, mp,  conn_t *, NULL,
14451 14452                      void_ip_t *, ipha,  __dtrace_ipsr_ill_t *, ill,
14452 14453                      ipha_t *, ipha, ip6_t *, NULL, int, 0);
14453 14454          } else {
14454 14455                  ip6_t *ip6h = (ip6_t *)mp->b_rptr;
14455 14456  
14456 14457                  ASSERT(isv6);
14457 14458                  ASSERT(pkt_len ==
14458 14459                      ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
14459 14460                  if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
14460 14461                      !(ixaflags & IXAF_NO_PFHOOK)) {
14461 14462                          int     error;
14462 14463  
14463 14464                          FW_HOOKS6(ipst->ips_ip6_physical_out_event,
14464 14465                              ipst->ips_ipv6firewall_physical_out,
14465 14466                              NULL, ill, ip6h, mp, mp, 0, ipst, error);
14466 14467                          DTRACE_PROBE1(ip6__physical__out__end,
14467 14468                              mblk_t *, mp);
14468 14469                          if (mp == NULL)
14469 14470                                  return (error);
14470 14471  
14471 14472                          /* The length could have changed */
14472 14473                          pkt_len = msgdsize(mp);
14473 14474                  }
14474 14475                  if (ipst->ips_ip6_observe.he_interested) {
14475 14476                          /* See above */
14476 14477                          szone = IP_REAL_ZONEID(szone, ipst);
14477 14478  
14478 14479                          ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14479 14480                              ill, ipst);
14480 14481                  }
14481 14482                  DTRACE_IP7(send, mblk_t *, mp,  conn_t *, NULL,
14482 14483                      void_ip_t *, ip6h,  __dtrace_ipsr_ill_t *, ill,
14483 14484                      ipha_t *, NULL, ip6_t *, ip6h, int, 0);
14484 14485          }
14485 14486  
14486 14487  sendit:
14487 14488          /*
14488 14489           * We check the state without a lock because the state can never
14489 14490           * move "backwards" to initial or incomplete.
14490 14491           */
14491 14492          switch (ncec->ncec_state) {
14492 14493          case ND_REACHABLE:
14493 14494          case ND_STALE:
14494 14495          case ND_DELAY:
14495 14496          case ND_PROBE:
14496 14497                  mp = ip_xmit_attach_llhdr(mp, nce);
14497 14498                  if (mp == NULL) {
14498 14499                          /*
14499 14500                           * ip_xmit_attach_llhdr has increased
14500 14501                           * ipIfStatsOutDiscards and called ip_drop_output()
14501 14502                           */
14502 14503                          return (ENOBUFS);
14503 14504                  }
14504 14505                  /*
14505 14506                   * check if nce_fastpath completed and we tagged on a
14506 14507                   * copy of nce_fp_mp in ip_xmit_attach_llhdr().
14507 14508                   */
14508 14509                  fp_mp = (mp->b_datap->db_type == M_DATA);
14509 14510  
14510 14511                  if (fp_mp &&
14511 14512                      (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
14512 14513                          ill_dld_direct_t *idd;
14513 14514  
14514 14515                          idd = &ill->ill_dld_capab->idc_direct;
14515 14516                          /*
14516 14517                           * Send the packet directly to DLD, where it
14517 14518                           * may be queued depending on the availability
14518 14519                           * of transmit resources at the media layer.
14519 14520                           * Return value should be taken into
14520 14521                           * account and flow control the TCP.
14521 14522                           */
14522 14523                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14523 14524                          UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14524 14525                              pkt_len);
14525 14526  
14526 14527                          if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
14527 14528                                  (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
14528 14529                                      (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
14529 14530                          } else {
14530 14531                                  uintptr_t cookie;
14531 14532  
14532 14533                                  if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
14533 14534                                      mp, (uintptr_t)xmit_hint, 0)) != 0) {
14534 14535                                          if (ixacookie != NULL)
14535 14536                                                  *ixacookie = cookie;
14536 14537                                          return (EWOULDBLOCK);
14537 14538                                  }
14538 14539                          }
14539 14540                  } else {
14540 14541                          wq = ill->ill_wq;
14541 14542  
14542 14543                          if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
14543 14544                              !canputnext(wq)) {
14544 14545                                  if (ixacookie != NULL)
14545 14546                                          *ixacookie = 0;
14546 14547                                  ip_xmit_flowctl_drop(ill, mp, fp_mp,
14547 14548                                      nce->nce_fp_mp != NULL ?
14548 14549                                      MBLKL(nce->nce_fp_mp) : 0);
14549 14550                                  return (EWOULDBLOCK);
14550 14551                          }
14551 14552                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14552 14553                          UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14553 14554                              pkt_len);
14554 14555                          putnext(wq, mp);
14555 14556                  }
14556 14557  
14557 14558                  /*
14558 14559                   * The rest of this function implements Neighbor Unreachability
14559 14560                   * detection. Determine if the ncec is eligible for NUD.
14560 14561                   */
14561 14562                  if (ncec->ncec_flags & NCE_F_NONUD)
14562 14563                          return (0);
14563 14564  
14564 14565                  ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14565 14566  
14566 14567                  /*
14567 14568                   * Check for upper layer advice
14568 14569                   */
14569 14570                  if (ixaflags & IXAF_REACH_CONF) {
14570 14571                          timeout_id_t tid;
14571 14572  
14572 14573                          /*
14573 14574                           * It should be o.k. to check the state without
14574 14575                           * a lock here, at most we lose an advice.
14575 14576                           */
14576 14577                          ncec->ncec_last = TICK_TO_MSEC(now);
14577 14578                          if (ncec->ncec_state != ND_REACHABLE) {
14578 14579                                  mutex_enter(&ncec->ncec_lock);
14579 14580                                  ncec->ncec_state = ND_REACHABLE;
14580 14581                                  tid = ncec->ncec_timeout_id;
14581 14582                                  ncec->ncec_timeout_id = 0;
14582 14583                                  mutex_exit(&ncec->ncec_lock);
14583 14584                                  (void) untimeout(tid);
14584 14585                                  if (ip_debug > 2) {
14585 14586                                          /* ip1dbg */
14586 14587                                          pr_addr_dbg("ip_xmit: state"
14587 14588                                              " for %s changed to"
14588 14589                                              " REACHABLE\n", AF_INET6,
14589 14590                                              &ncec->ncec_addr);
14590 14591                                  }
14591 14592                          }
14592 14593                          return (0);
14593 14594                  }
14594 14595  
14595 14596                  delta =  TICK_TO_MSEC(now) - ncec->ncec_last;
14596 14597                  ip1dbg(("ip_xmit: delta = %" PRId64
14597 14598                      " ill_reachable_time = %d \n", delta,
14598 14599                      ill->ill_reachable_time));
14599 14600                  if (delta > (uint64_t)ill->ill_reachable_time) {
14600 14601                          mutex_enter(&ncec->ncec_lock);
14601 14602                          switch (ncec->ncec_state) {
14602 14603                          case ND_REACHABLE:
14603 14604                                  ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14604 14605                                  /* FALLTHROUGH */
14605 14606                          case ND_STALE:
14606 14607                                  /*
14607 14608                                   * ND_REACHABLE is identical to
14608 14609                                   * ND_STALE in this specific case. If
14609 14610                                   * reachable time has expired for this
14610 14611                                   * neighbor (delta is greater than
14611 14612                                   * reachable time), conceptually, the
14612 14613                                   * neighbor cache is no longer in
14613 14614                                   * REACHABLE state, but already in
14614 14615                                   * STALE state.  So the correct
14615 14616                                   * transition here is to ND_DELAY.
14616 14617                                   */
14617 14618                                  ncec->ncec_state = ND_DELAY;
14618 14619                                  mutex_exit(&ncec->ncec_lock);
14619 14620                                  nce_restart_timer(ncec,
14620 14621                                      ipst->ips_delay_first_probe_time);
14621 14622                                  if (ip_debug > 3) {
14622 14623                                          /* ip2dbg */
14623 14624                                          pr_addr_dbg("ip_xmit: state"
14624 14625                                              " for %s changed to"
14625 14626                                              " DELAY\n", AF_INET6,
14626 14627                                              &ncec->ncec_addr);
14627 14628                                  }
14628 14629                                  break;
14629 14630                          case ND_DELAY:
14630 14631                          case ND_PROBE:
14631 14632                                  mutex_exit(&ncec->ncec_lock);
14632 14633                                  /* Timers have already started */
14633 14634                                  break;
14634 14635                          case ND_UNREACHABLE:
14635 14636                                  /*
14636 14637                                   * nce_timer has detected that this ncec
14637 14638                                   * is unreachable and initiated deleting
14638 14639                                   * this ncec.
14639 14640                                   * This is a harmless race where we found the
14640 14641                                   * ncec before it was deleted and have
14641 14642                                   * just sent out a packet using this
14642 14643                                   * unreachable ncec.
14643 14644                                   */
14644 14645                                  mutex_exit(&ncec->ncec_lock);
14645 14646                                  break;
14646 14647                          default:
14647 14648                                  ASSERT(0);
14648 14649                                  mutex_exit(&ncec->ncec_lock);
14649 14650                          }
14650 14651                  }
14651 14652                  return (0);
14652 14653  
14653 14654          case ND_INCOMPLETE:
14654 14655                  /*
14655 14656                   * the state could have changed since we didn't hold the lock.
14656 14657                   * Re-verify state under lock.
14657 14658                   */
14658 14659                  is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14659 14660                  mutex_enter(&ncec->ncec_lock);
14660 14661                  if (NCE_ISREACHABLE(ncec)) {
14661 14662                          mutex_exit(&ncec->ncec_lock);
14662 14663                          goto sendit;
14663 14664                  }
14664 14665                  /* queue the packet */
14665 14666                  nce_queue_mp(ncec, mp, is_probe);
14666 14667                  mutex_exit(&ncec->ncec_lock);
14667 14668                  DTRACE_PROBE2(ip__xmit__incomplete,
14668 14669                      (ncec_t *), ncec, (mblk_t *), mp);
14669 14670                  return (0);
14670 14671  
14671 14672          case ND_INITIAL:
14672 14673                  /*
14673 14674                   * State could have changed since we didn't hold the lock, so
14674 14675                   * re-verify state.
14675 14676                   */
14676 14677                  is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14677 14678                  mutex_enter(&ncec->ncec_lock);
14678 14679                  if (NCE_ISREACHABLE(ncec))  {
14679 14680                          mutex_exit(&ncec->ncec_lock);
14680 14681                          goto sendit;
14681 14682                  }
14682 14683                  nce_queue_mp(ncec, mp, is_probe);
14683 14684                  if (ncec->ncec_state == ND_INITIAL) {
14684 14685                          ncec->ncec_state = ND_INCOMPLETE;
14685 14686                          mutex_exit(&ncec->ncec_lock);
14686 14687                          /*
14687 14688                           * figure out the source we want to use
14688 14689                           * and resolve it.
14689 14690                           */
14690 14691                          ip_ndp_resolve(ncec);
14691 14692                  } else  {
14692 14693                          mutex_exit(&ncec->ncec_lock);
14693 14694                  }
14694 14695                  return (0);
14695 14696  
14696 14697          case ND_UNREACHABLE:
14697 14698                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14698 14699                  ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14699 14700                      mp, ill);
14700 14701                  freemsg(mp);
14701 14702                  return (0);
14702 14703  
14703 14704          default:
14704 14705                  ASSERT(0);
14705 14706                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14706 14707                  ip_drop_output("ipIfStatsOutDiscards - ND_other",
14707 14708                      mp, ill);
14708 14709                  freemsg(mp);
14709 14710                  return (ENETUNREACH);
14710 14711          }
14711 14712  }
14712 14713  
14713 14714  /*
14714 14715   * Return B_TRUE if the buffers differ in length or content.
14715 14716   * This is used for comparing extension header buffers.
14716 14717   * Note that an extension header would be declared different
14717 14718   * even if all that changed was the next header value in that header i.e.
14718 14719   * what really changed is the next extension header.
14719 14720   */
14720 14721  boolean_t
14721 14722  ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14722 14723      uint_t blen)
14723 14724  {
14724 14725          if (!b_valid)
14725 14726                  blen = 0;
14726 14727  
14727 14728          if (alen != blen)
14728 14729                  return (B_TRUE);
14729 14730          if (alen == 0)
14730 14731                  return (B_FALSE);       /* Both zero length */
14731 14732          return (bcmp(abuf, bbuf, alen));
14732 14733  }
14733 14734  
14734 14735  /*
14735 14736   * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14736 14737   * Return B_FALSE if memory allocation fails - don't change any state!
14737 14738   */
14738 14739  boolean_t
14739 14740  ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14740 14741      const void *src, uint_t srclen)
14741 14742  {
14742 14743          void *dst;
14743 14744  
14744 14745          if (!src_valid)
14745 14746                  srclen = 0;
14746 14747  
14747 14748          ASSERT(*dstlenp == 0);
14748 14749          if (src != NULL && srclen != 0) {
14749 14750                  dst = mi_alloc(srclen, BPRI_MED);
14750 14751                  if (dst == NULL)
14751 14752                          return (B_FALSE);
14752 14753          } else {
14753 14754                  dst = NULL;
14754 14755          }
14755 14756          if (*dstp != NULL)
14756 14757                  mi_free(*dstp);
14757 14758          *dstp = dst;
14758 14759          *dstlenp = dst == NULL ? 0 : srclen;
14759 14760          return (B_TRUE);
14760 14761  }
14761 14762  
14762 14763  /*
14763 14764   * Replace what is in *dst, *dstlen with the source.
14764 14765   * Assumes ip_allocbuf has already been called.
14765 14766   */
14766 14767  void
14767 14768  ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14768 14769      const void *src, uint_t srclen)
14769 14770  {
14770 14771          if (!src_valid)
14771 14772                  srclen = 0;
14772 14773  
14773 14774          ASSERT(*dstlenp == srclen);
14774 14775          if (src != NULL && srclen != 0)
14775 14776                  bcopy(src, *dstp, srclen);
14776 14777  }
14777 14778  
14778 14779  /*
14779 14780   * Free the storage pointed to by the members of an ip_pkt_t.
14780 14781   */
14781 14782  void
14782 14783  ip_pkt_free(ip_pkt_t *ipp)
14783 14784  {
14784 14785          uint_t  fields = ipp->ipp_fields;
14785 14786  
14786 14787          if (fields & IPPF_HOPOPTS) {
14787 14788                  kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14788 14789                  ipp->ipp_hopopts = NULL;
14789 14790                  ipp->ipp_hopoptslen = 0;
14790 14791          }
14791 14792          if (fields & IPPF_RTHDRDSTOPTS) {
14792 14793                  kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14793 14794                  ipp->ipp_rthdrdstopts = NULL;
14794 14795                  ipp->ipp_rthdrdstoptslen = 0;
14795 14796          }
14796 14797          if (fields & IPPF_DSTOPTS) {
14797 14798                  kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14798 14799                  ipp->ipp_dstopts = NULL;
14799 14800                  ipp->ipp_dstoptslen = 0;
14800 14801          }
14801 14802          if (fields & IPPF_RTHDR) {
14802 14803                  kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14803 14804                  ipp->ipp_rthdr = NULL;
14804 14805                  ipp->ipp_rthdrlen = 0;
14805 14806          }
14806 14807          if (fields & IPPF_IPV4_OPTIONS) {
14807 14808                  kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14808 14809                  ipp->ipp_ipv4_options = NULL;
14809 14810                  ipp->ipp_ipv4_options_len = 0;
14810 14811          }
14811 14812          if (fields & IPPF_LABEL_V4) {
14812 14813                  kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
14813 14814                  ipp->ipp_label_v4 = NULL;
14814 14815                  ipp->ipp_label_len_v4 = 0;
14815 14816          }
14816 14817          if (fields & IPPF_LABEL_V6) {
14817 14818                  kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6);
14818 14819                  ipp->ipp_label_v6 = NULL;
14819 14820                  ipp->ipp_label_len_v6 = 0;
14820 14821          }
14821 14822          ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14822 14823              IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14823 14824  }
14824 14825  
14825 14826  /*
14826 14827   * Copy from src to dst and allocate as needed.
14827 14828   * Returns zero or ENOMEM.
14828 14829   *
14829 14830   * The caller must initialize dst to zero.
14830 14831   */
14831 14832  int
14832 14833  ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14833 14834  {
14834 14835          uint_t  fields = src->ipp_fields;
14835 14836  
14836 14837          /* Start with fields that don't require memory allocation */
14837 14838          dst->ipp_fields = fields &
14838 14839              ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14839 14840              IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14840 14841  
14841 14842          dst->ipp_addr = src->ipp_addr;
14842 14843          dst->ipp_unicast_hops = src->ipp_unicast_hops;
14843 14844          dst->ipp_hoplimit = src->ipp_hoplimit;
14844 14845          dst->ipp_tclass = src->ipp_tclass;
14845 14846          dst->ipp_type_of_service = src->ipp_type_of_service;
14846 14847  
14847 14848          if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14848 14849              IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6)))
14849 14850                  return (0);
14850 14851  
14851 14852          if (fields & IPPF_HOPOPTS) {
14852 14853                  dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14853 14854                  if (dst->ipp_hopopts == NULL) {
14854 14855                          ip_pkt_free(dst);
14855 14856                          return (ENOMEM);
14856 14857                  }
14857 14858                  dst->ipp_fields |= IPPF_HOPOPTS;
14858 14859                  bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14859 14860                      src->ipp_hopoptslen);
14860 14861                  dst->ipp_hopoptslen = src->ipp_hopoptslen;
14861 14862          }
14862 14863          if (fields & IPPF_RTHDRDSTOPTS) {
14863 14864                  dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14864 14865                      kmflag);
14865 14866                  if (dst->ipp_rthdrdstopts == NULL) {
14866 14867                          ip_pkt_free(dst);
14867 14868                          return (ENOMEM);
14868 14869                  }
14869 14870                  dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14870 14871                  bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14871 14872                      src->ipp_rthdrdstoptslen);
14872 14873                  dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14873 14874          }
14874 14875          if (fields & IPPF_DSTOPTS) {
14875 14876                  dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14876 14877                  if (dst->ipp_dstopts == NULL) {
14877 14878                          ip_pkt_free(dst);
14878 14879                          return (ENOMEM);
14879 14880                  }
14880 14881                  dst->ipp_fields |= IPPF_DSTOPTS;
14881 14882                  bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14882 14883                      src->ipp_dstoptslen);
14883 14884                  dst->ipp_dstoptslen = src->ipp_dstoptslen;
14884 14885          }
14885 14886          if (fields & IPPF_RTHDR) {
14886 14887                  dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14887 14888                  if (dst->ipp_rthdr == NULL) {
14888 14889                          ip_pkt_free(dst);
14889 14890                          return (ENOMEM);
14890 14891                  }
14891 14892                  dst->ipp_fields |= IPPF_RTHDR;
14892 14893                  bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14893 14894                      src->ipp_rthdrlen);
14894 14895                  dst->ipp_rthdrlen = src->ipp_rthdrlen;
14895 14896          }
14896 14897          if (fields & IPPF_IPV4_OPTIONS) {
14897 14898                  dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14898 14899                      kmflag);
14899 14900                  if (dst->ipp_ipv4_options == NULL) {
14900 14901                          ip_pkt_free(dst);
14901 14902                          return (ENOMEM);
14902 14903                  }
14903 14904                  dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14904 14905                  bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14905 14906                      src->ipp_ipv4_options_len);
14906 14907                  dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14907 14908          }
14908 14909          if (fields & IPPF_LABEL_V4) {
14909 14910                  dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag);
14910 14911                  if (dst->ipp_label_v4 == NULL) {
14911 14912                          ip_pkt_free(dst);
14912 14913                          return (ENOMEM);
14913 14914                  }
14914 14915                  dst->ipp_fields |= IPPF_LABEL_V4;
14915 14916                  bcopy(src->ipp_label_v4, dst->ipp_label_v4,
14916 14917                      src->ipp_label_len_v4);
14917 14918                  dst->ipp_label_len_v4 = src->ipp_label_len_v4;
14918 14919          }
14919 14920          if (fields & IPPF_LABEL_V6) {
14920 14921                  dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag);
14921 14922                  if (dst->ipp_label_v6 == NULL) {
14922 14923                          ip_pkt_free(dst);
14923 14924                          return (ENOMEM);
14924 14925                  }
14925 14926                  dst->ipp_fields |= IPPF_LABEL_V6;
14926 14927                  bcopy(src->ipp_label_v6, dst->ipp_label_v6,
14927 14928                      src->ipp_label_len_v6);
14928 14929                  dst->ipp_label_len_v6 = src->ipp_label_len_v6;
14929 14930          }
14930 14931          if (fields & IPPF_FRAGHDR) {
14931 14932                  dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14932 14933                  if (dst->ipp_fraghdr == NULL) {
14933 14934                          ip_pkt_free(dst);
14934 14935                          return (ENOMEM);
14935 14936                  }
14936 14937                  dst->ipp_fields |= IPPF_FRAGHDR;
14937 14938                  bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14938 14939                      src->ipp_fraghdrlen);
14939 14940                  dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14940 14941          }
14941 14942          return (0);
14942 14943  }
14943 14944  
14944 14945  /*
14945 14946   * Returns INADDR_ANY if no source route
14946 14947   */
14947 14948  ipaddr_t
14948 14949  ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14949 14950  {
14950 14951          ipaddr_t        nexthop = INADDR_ANY;
14951 14952          ipoptp_t        opts;
14952 14953          uchar_t         *opt;
14953 14954          uint8_t         optval;
14954 14955          uint8_t         optlen;
14955 14956          uint32_t        totallen;
14956 14957  
14957 14958          if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14958 14959                  return (INADDR_ANY);
14959 14960  
14960 14961          totallen = ipp->ipp_ipv4_options_len;
14961 14962          if (totallen & 0x3)
14962 14963                  return (INADDR_ANY);
14963 14964  
14964 14965          for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14965 14966              optval != IPOPT_EOL;
14966 14967              optval = ipoptp_next(&opts)) {
14967 14968                  opt = opts.ipoptp_cur;
14968 14969                  switch (optval) {
14969 14970                          uint8_t off;
14970 14971                  case IPOPT_SSRR:
14971 14972                  case IPOPT_LSRR:
14972 14973                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
14973 14974                                  break;
14974 14975                          }
14975 14976                          optlen = opts.ipoptp_len;
14976 14977                          off = opt[IPOPT_OFFSET];
14977 14978                          off--;
14978 14979                          if (optlen < IP_ADDR_LEN ||
14979 14980                              off > optlen - IP_ADDR_LEN) {
14980 14981                                  /* End of source route */
14981 14982                                  break;
14982 14983                          }
14983 14984                          bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
14984 14985                          if (nexthop == htonl(INADDR_LOOPBACK)) {
14985 14986                                  /* Ignore */
14986 14987                                  nexthop = INADDR_ANY;
14987 14988                                  break;
14988 14989                          }
14989 14990                          break;
14990 14991                  }
14991 14992          }
14992 14993          return (nexthop);
14993 14994  }
14994 14995  
14995 14996  /*
14996 14997   * Reverse a source route.
14997 14998   */
14998 14999  void
14999 15000  ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
15000 15001  {
15001 15002          ipaddr_t        tmp;
15002 15003          ipoptp_t        opts;
15003 15004          uchar_t         *opt;
15004 15005          uint8_t         optval;
15005 15006          uint32_t        totallen;
15006 15007  
15007 15008          if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15008 15009                  return;
15009 15010  
15010 15011          totallen = ipp->ipp_ipv4_options_len;
15011 15012          if (totallen & 0x3)
15012 15013                  return;
15013 15014  
15014 15015          for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15015 15016              optval != IPOPT_EOL;
15016 15017              optval = ipoptp_next(&opts)) {
15017 15018                  uint8_t off1, off2;
15018 15019  
15019 15020                  opt = opts.ipoptp_cur;
15020 15021                  switch (optval) {
15021 15022                  case IPOPT_SSRR:
15022 15023                  case IPOPT_LSRR:
15023 15024                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15024 15025                                  break;
15025 15026                          }
15026 15027                          off1 = IPOPT_MINOFF_SR - 1;
15027 15028                          off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
15028 15029                          while (off2 > off1) {
15029 15030                                  bcopy(opt + off2, &tmp, IP_ADDR_LEN);
15030 15031                                  bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
15031 15032                                  bcopy(&tmp, opt + off2, IP_ADDR_LEN);
15032 15033                                  off2 -= IP_ADDR_LEN;
15033 15034                                  off1 += IP_ADDR_LEN;
15034 15035                          }
15035 15036                          opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
15036 15037                          break;
15037 15038                  }
15038 15039          }
15039 15040  }
15040 15041  
15041 15042  /*
15042 15043   * Returns NULL if no routing header
15043 15044   */
15044 15045  in6_addr_t *
15045 15046  ip_pkt_source_route_v6(const ip_pkt_t *ipp)
15046 15047  {
15047 15048          in6_addr_t      *nexthop = NULL;
15048 15049          ip6_rthdr0_t    *rthdr;
15049 15050  
15050 15051          if (!(ipp->ipp_fields & IPPF_RTHDR))
15051 15052                  return (NULL);
15052 15053  
15053 15054          rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
15054 15055          if (rthdr->ip6r0_segleft == 0)
15055 15056                  return (NULL);
15056 15057  
15057 15058          nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
15058 15059          return (nexthop);
15059 15060  }
15060 15061  
15061 15062  zoneid_t
15062 15063  ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
15063 15064      zoneid_t lookup_zoneid)
15064 15065  {
15065 15066          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
15066 15067          ire_t           *ire;
15067 15068          int             ire_flags = MATCH_IRE_TYPE;
15068 15069          zoneid_t        zoneid = ALL_ZONES;
15069 15070  
15070 15071          if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15071 15072                  return (ALL_ZONES);
15072 15073  
15073 15074          if (lookup_zoneid != ALL_ZONES)
15074 15075                  ire_flags |= MATCH_IRE_ZONEONLY;
15075 15076          ire = ire_ftable_lookup_v4(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15076 15077              NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15077 15078          if (ire != NULL) {
15078 15079                  zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15079 15080                  ire_refrele(ire);
15080 15081          }
15081 15082          return (zoneid);
15082 15083  }
15083 15084  
15084 15085  zoneid_t
15085 15086  ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
15086 15087      ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
15087 15088  {
15088 15089          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
15089 15090          ire_t           *ire;
15090 15091          int             ire_flags = MATCH_IRE_TYPE;
15091 15092          zoneid_t        zoneid = ALL_ZONES;
15092 15093  
15093 15094          if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15094 15095                  return (ALL_ZONES);
15095 15096  
15096 15097          if (IN6_IS_ADDR_LINKLOCAL(addr))
15097 15098                  ire_flags |= MATCH_IRE_ILL;
15098 15099  
15099 15100          if (lookup_zoneid != ALL_ZONES)
15100 15101                  ire_flags |= MATCH_IRE_ZONEONLY;
15101 15102          ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15102 15103              ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15103 15104          if (ire != NULL) {
15104 15105                  zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15105 15106                  ire_refrele(ire);
15106 15107          }
15107 15108          return (zoneid);
15108 15109  }
15109 15110  
15110 15111  /*
15111 15112   * IP obserability hook support functions.
15112 15113   */
15113 15114  static void
15114 15115  ipobs_init(ip_stack_t *ipst)
15115 15116  {
15116 15117          netid_t id;
15117 15118  
15118 15119          id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
15119 15120  
15120 15121          ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
15121 15122          VERIFY(ipst->ips_ip4_observe_pr != NULL);
15122 15123  
15123 15124          ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
15124 15125          VERIFY(ipst->ips_ip6_observe_pr != NULL);
15125 15126  }
15126 15127  
15127 15128  static void
15128 15129  ipobs_fini(ip_stack_t *ipst)
15129 15130  {
15130 15131  
15131 15132          VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
15132 15133          VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
15133 15134  }
15134 15135  
15135 15136  /*
15136 15137   * hook_pkt_observe_t is composed in network byte order so that the
15137 15138   * entire mblk_t chain handed into hook_run can be used as-is.
15138 15139   * The caveat is that use of the fields, such as the zone fields,
15139 15140   * requires conversion into host byte order first.
15140 15141   */
15141 15142  void
15142 15143  ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
15143 15144      const ill_t *ill, ip_stack_t *ipst)
15144 15145  {
15145 15146          hook_pkt_observe_t *hdr;
15146 15147          uint64_t grifindex;
15147 15148          mblk_t *imp;
15148 15149  
15149 15150          imp = allocb(sizeof (*hdr), BPRI_HI);
15150 15151          if (imp == NULL)
15151 15152                  return;
15152 15153  
15153 15154          hdr = (hook_pkt_observe_t *)imp->b_rptr;
15154 15155          /*
15155 15156           * b_wptr is set to make the apparent size of the data in the mblk_t
15156 15157           * to exclude the pointers at the end of hook_pkt_observer_t.
15157 15158           */
15158 15159          imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
15159 15160          imp->b_cont = mp;
15160 15161  
15161 15162          ASSERT(DB_TYPE(mp) == M_DATA);
15162 15163  
15163 15164          if (IS_UNDER_IPMP(ill))
15164 15165                  grifindex = ipmp_ill_get_ipmp_ifindex(ill);
15165 15166          else
15166 15167                  grifindex = 0;
15167 15168  
15168 15169          hdr->hpo_version = 1;
15169 15170          hdr->hpo_htype = htons(htype);
15170 15171          hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
15171 15172          hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
15172 15173          hdr->hpo_grifindex = htonl(grifindex);
15173 15174          hdr->hpo_zsrc = htonl(zsrc);
15174 15175          hdr->hpo_zdst = htonl(zdst);
15175 15176          hdr->hpo_pkt = imp;
15176 15177          hdr->hpo_ctx = ipst->ips_netstack;
15177 15178  
15178 15179          if (ill->ill_isv6) {
15179 15180                  hdr->hpo_family = AF_INET6;
15180 15181                  (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
15181 15182                      ipst->ips_ipv6observing, (hook_data_t)hdr);
15182 15183          } else {
15183 15184                  hdr->hpo_family = AF_INET;
15184 15185                  (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
15185 15186                      ipst->ips_ipv4observing, (hook_data_t)hdr);
15186 15187          }
15187 15188  
15188 15189          imp->b_cont = NULL;
15189 15190          freemsg(imp);
15190 15191  }
15191 15192  
15192 15193  /*
15193 15194   * Utility routine that checks if `v4srcp' is a valid address on underlying
15194 15195   * interface `ill'.  If `ipifp' is non-NULL, it's set to a held ipif
15195 15196   * associated with `v4srcp' on success.  NOTE: if this is not called from
15196 15197   * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
15197 15198   * group during or after this lookup.
15198 15199   */
15199 15200  boolean_t
15200 15201  ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
15201 15202  {
15202 15203          ipif_t *ipif;
15203 15204  
15204 15205          ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
15205 15206          if (ipif != NULL) {
15206 15207                  if (ipifp != NULL)
15207 15208                          *ipifp = ipif;
15208 15209                  else
15209 15210                          ipif_refrele(ipif);
15210 15211                  return (B_TRUE);
15211 15212          }
15212 15213  
15213 15214          ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
15214 15215              *v4srcp));
15215 15216          return (B_FALSE);
15216 15217  }
15217 15218  
15218 15219  /*
15219 15220   * Transport protocol call back function for CPU state change.
15220 15221   */
15221 15222  /* ARGSUSED */
15222 15223  static int
15223 15224  ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
15224 15225  {
15225 15226          processorid_t cpu_seqid;
15226 15227          netstack_handle_t nh;
15227 15228          netstack_t *ns;
15228 15229  
15229 15230          ASSERT(MUTEX_HELD(&cpu_lock));
15230 15231  
15231 15232          switch (what) {
15232 15233          case CPU_CONFIG:
15233 15234          case CPU_ON:
15234 15235          case CPU_INIT:
15235 15236          case CPU_CPUPART_IN:
15236 15237                  cpu_seqid = cpu[id]->cpu_seqid;
15237 15238                  netstack_next_init(&nh);
15238 15239                  while ((ns = netstack_next(&nh)) != NULL) {
15239 15240                          tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
15240 15241                          sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
15241 15242                          udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
15242 15243                          netstack_rele(ns);
15243 15244                  }
15244 15245                  netstack_next_fini(&nh);
15245 15246                  break;
15246 15247          case CPU_UNCONFIG:
15247 15248          case CPU_OFF:
15248 15249          case CPU_CPUPART_OUT:
15249 15250                  /*
15250 15251                   * Nothing to do.  We don't remove the per CPU stats from
15251 15252                   * the IP stack even when the CPU goes offline.
15252 15253                   */
15253 15254                  break;
15254 15255          default:
15255 15256                  break;
15256 15257          }
15257 15258          return (0);
15258 15259  }
  
    | 
      ↓ open down ↓ | 
    10596 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX