Print this page
    
10472 Limit number of multicast NCEs
Reviewed by: Cody Peter Mello <melloc@writev.io>
Reviewed by: Jason King <jason.king@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
    
      
        | 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.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  
    | 
      ↓ open down ↓ | 
    16 lines elided | 
    
      ↑ open up ↑ | 
  
  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   25   * Copyright (c) 2017 OmniTI Computer Consulting, Inc. All rights reserved.
  26   26   * Copyright (c) 2016 by Delphix. All rights reserved.
  27      - * Copyright (c) 2018 Joyent, Inc. All rights reserved.
       27 + * Copyright (c) 2019 Joyent, Inc. All rights reserved.
  28   28   */
  29   29  
  30   30  #include <sys/types.h>
  31   31  #include <sys/stream.h>
  32   32  #include <sys/dlpi.h>
  33   33  #include <sys/stropts.h>
  34   34  #include <sys/sysmacros.h>
  35   35  #include <sys/strsubr.h>
  36   36  #include <sys/strlog.h>
  37   37  #include <sys/strsun.h>
  38   38  #include <sys/zone.h>
  39   39  #define _SUN_TPI_VERSION 2
  40   40  #include <sys/tihdr.h>
  41   41  #include <sys/xti_inet.h>
  42   42  #include <sys/ddi.h>
  43   43  #include <sys/suntpi.h>
  44   44  #include <sys/cmn_err.h>
  45   45  #include <sys/debug.h>
  46   46  #include <sys/kobj.h>
  47   47  #include <sys/modctl.h>
  48   48  #include <sys/atomic.h>
  49   49  #include <sys/policy.h>
  50   50  #include <sys/priv.h>
  51   51  #include <sys/taskq.h>
  52   52  
  53   53  #include <sys/systm.h>
  54   54  #include <sys/param.h>
  55   55  #include <sys/kmem.h>
  56   56  #include <sys/sdt.h>
  57   57  #include <sys/socket.h>
  58   58  #include <sys/vtrace.h>
  59   59  #include <sys/isa_defs.h>
  60   60  #include <sys/mac.h>
  61   61  #include <net/if.h>
  62   62  #include <net/if_arp.h>
  63   63  #include <net/route.h>
  64   64  #include <sys/sockio.h>
  65   65  #include <netinet/in.h>
  66   66  #include <net/if_dl.h>
  67   67  
  68   68  #include <inet/common.h>
  69   69  #include <inet/mi.h>
  70   70  #include <inet/mib2.h>
  71   71  #include <inet/nd.h>
  72   72  #include <inet/arp.h>
  73   73  #include <inet/snmpcom.h>
  74   74  #include <inet/optcom.h>
  75   75  #include <inet/kstatcom.h>
  76   76  
  77   77  #include <netinet/igmp_var.h>
  78   78  #include <netinet/ip6.h>
  79   79  #include <netinet/icmp6.h>
  80   80  #include <netinet/sctp.h>
  81   81  
  82   82  #include <inet/ip.h>
  83   83  #include <inet/ip_impl.h>
  84   84  #include <inet/ip6.h>
  85   85  #include <inet/ip6_asp.h>
  86   86  #include <inet/tcp.h>
  87   87  #include <inet/tcp_impl.h>
  88   88  #include <inet/ip_multi.h>
  89   89  #include <inet/ip_if.h>
  90   90  #include <inet/ip_ire.h>
  91   91  #include <inet/ip_ftable.h>
  92   92  #include <inet/ip_rts.h>
  93   93  #include <inet/ip_ndp.h>
  94   94  #include <inet/ip_listutils.h>
  95   95  #include <netinet/igmp.h>
  96   96  #include <netinet/ip_mroute.h>
  97   97  #include <inet/ipp_common.h>
  98   98  
  99   99  #include <net/pfkeyv2.h>
 100  100  #include <inet/sadb.h>
 101  101  #include <inet/ipsec_impl.h>
 102  102  #include <inet/iptun/iptun_impl.h>
 103  103  #include <inet/ipdrop.h>
 104  104  #include <inet/ip_netinfo.h>
 105  105  #include <inet/ilb_ip.h>
 106  106  
 107  107  #include <sys/ethernet.h>
 108  108  #include <net/if_types.h>
 109  109  #include <sys/cpuvar.h>
 110  110  
 111  111  #include <ipp/ipp.h>
 112  112  #include <ipp/ipp_impl.h>
 113  113  #include <ipp/ipgpc/ipgpc.h>
 114  114  
 115  115  #include <sys/pattr.h>
 116  116  #include <inet/ipclassifier.h>
 117  117  #include <inet/sctp_ip.h>
 118  118  #include <inet/sctp/sctp_impl.h>
 119  119  #include <inet/udp_impl.h>
 120  120  #include <inet/rawip_impl.h>
 121  121  #include <inet/rts_impl.h>
 122  122  
 123  123  #include <sys/tsol/label.h>
 124  124  #include <sys/tsol/tnet.h>
 125  125  
 126  126  #include <sys/squeue_impl.h>
 127  127  #include <inet/ip_arp.h>
 128  128  
 129  129  #include <sys/clock_impl.h>     /* For LBOLT_FASTPATH{,64} */
 130  130  
 131  131  /*
 132  132   * Values for squeue switch:
 133  133   * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
 134  134   * IP_SQUEUE_ENTER: SQ_PROCESS
 135  135   * IP_SQUEUE_FILL: SQ_FILL
 136  136   */
 137  137  int ip_squeue_enter = IP_SQUEUE_ENTER;  /* Setable in /etc/system */
 138  138  
 139  139  int ip_squeue_flag;
 140  140  
 141  141  /*
 142  142   * Setable in /etc/system
 143  143   */
 144  144  int ip_poll_normal_ms = 100;
 145  145  int ip_poll_normal_ticks = 0;
 146  146  int ip_modclose_ackwait_ms = 3000;
 147  147  
 148  148  /*
 149  149   * It would be nice to have these present only in DEBUG systems, but the
 150  150   * current design of the global symbol checking logic requires them to be
 151  151   * unconditionally present.
 152  152   */
 153  153  uint_t ip_thread_data;                  /* TSD key for debug support */
 154  154  krwlock_t ip_thread_rwlock;
 155  155  list_t  ip_thread_list;
 156  156  
 157  157  /*
 158  158   * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
 159  159   */
 160  160  
 161  161  struct listptr_s {
 162  162          mblk_t  *lp_head;       /* pointer to the head of the list */
 163  163          mblk_t  *lp_tail;       /* pointer to the tail of the list */
 164  164  };
 165  165  
 166  166  typedef struct listptr_s listptr_t;
 167  167  
 168  168  /*
 169  169   * This is used by ip_snmp_get_mib2_ip_route_media and
 170  170   * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
 171  171   */
 172  172  typedef struct iproutedata_s {
 173  173          uint_t          ird_idx;
 174  174          uint_t          ird_flags;      /* see below */
 175  175          listptr_t       ird_route;      /* ipRouteEntryTable */
 176  176          listptr_t       ird_netmedia;   /* ipNetToMediaEntryTable */
 177  177          listptr_t       ird_attrs;      /* ipRouteAttributeTable */
 178  178  } iproutedata_t;
 179  179  
 180  180  /* Include ire_testhidden and IRE_IF_CLONE routes */
 181  181  #define IRD_REPORT_ALL  0x01
 182  182  
 183  183  /*
 184  184   * Cluster specific hooks. These should be NULL when booted as a non-cluster
 185  185   */
 186  186  
 187  187  /*
 188  188   * Hook functions to enable cluster networking
 189  189   * On non-clustered systems these vectors must always be NULL.
 190  190   *
 191  191   * Hook function to Check ip specified ip address is a shared ip address
 192  192   * in the cluster
 193  193   *
 194  194   */
 195  195  int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol,
 196  196      sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL;
 197  197  
 198  198  /*
 199  199   * Hook function to generate cluster wide ip fragment identifier
 200  200   */
 201  201  uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol,
 202  202      sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp,
 203  203      void *args) = NULL;
 204  204  
 205  205  /*
 206  206   * Hook function to generate cluster wide SPI.
 207  207   */
 208  208  void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t,
 209  209      void *) = NULL;
 210  210  
 211  211  /*
 212  212   * Hook function to verify if the SPI is already utlized.
 213  213   */
 214  214  
 215  215  int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
 216  216  
 217  217  /*
 218  218   * Hook function to delete the SPI from the cluster wide repository.
 219  219   */
 220  220  
 221  221  void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
 222  222  
 223  223  /*
 224  224   * Hook function to inform the cluster when packet received on an IDLE SA
 225  225   */
 226  226  
 227  227  void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t,
 228  228      in6_addr_t, in6_addr_t, void *) = NULL;
 229  229  
 230  230  /*
 231  231   * Synchronization notes:
 232  232   *
 233  233   * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
 234  234   * MT level protection given by STREAMS. IP uses a combination of its own
 235  235   * internal serialization mechanism and standard Solaris locking techniques.
 236  236   * The internal serialization is per phyint.  This is used to serialize
 237  237   * plumbing operations, IPMP operations, most set ioctls, etc.
 238  238   *
 239  239   * Plumbing is a long sequence of operations involving message
 240  240   * exchanges between IP, ARP and device drivers. Many set ioctls are typically
 241  241   * involved in plumbing operations. A natural model is to serialize these
 242  242   * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
 243  243   * parallel without any interference. But various set ioctls on hme0 are best
 244  244   * serialized, along with IPMP operations and processing of DLPI control
 245  245   * messages received from drivers on a per phyint basis. This serialization is
 246  246   * provided by the ipsq_t and primitives operating on this. Details can
 247  247   * be found in ip_if.c above the core primitives operating on ipsq_t.
 248  248   *
 249  249   * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
 250  250   * Simiarly lookup of an ire by a thread also returns a refheld ire.
 251  251   * In addition ipif's and ill's referenced by the ire are also indirectly
 252  252   * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
 253  253   * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
 254  254   * address of an ipif has to go through the ipsq_t. This ensures that only
 255  255   * one such exclusive operation proceeds at any time on the ipif. It then
 256  256   * waits for all refcnts
 257  257   * associated with this ipif to come down to zero. The address is changed
 258  258   * only after the ipif has been quiesced. Then the ipif is brought up again.
 259  259   * More details are described above the comment in ip_sioctl_flags.
 260  260   *
 261  261   * Packet processing is based mostly on IREs and are fully multi-threaded
 262  262   * using standard Solaris MT techniques.
 263  263   *
 264  264   * There are explicit locks in IP to handle:
 265  265   * - The ip_g_head list maintained by mi_open_link() and friends.
 266  266   *
 267  267   * - The reassembly data structures (one lock per hash bucket)
 268  268   *
 269  269   * - conn_lock is meant to protect conn_t fields. The fields actually
 270  270   *   protected by conn_lock are documented in the conn_t definition.
 271  271   *
 272  272   * - ire_lock to protect some of the fields of the ire, IRE tables
 273  273   *   (one lock per hash bucket). Refer to ip_ire.c for details.
 274  274   *
 275  275   * - ndp_g_lock and ncec_lock for protecting NCEs.
 276  276   *
 277  277   * - ill_lock protects fields of the ill and ipif. Details in ip.h
 278  278   *
 279  279   * - ill_g_lock: This is a global reader/writer lock. Protects the following
 280  280   *      * The AVL tree based global multi list of all ills.
 281  281   *      * The linked list of all ipifs of an ill
 282  282   *      * The <ipsq-xop> mapping
 283  283   *      * <ill-phyint> association
 284  284   *   Insertion/deletion of an ill in the system, insertion/deletion of an ipif
 285  285   *   into an ill, changing the <ipsq-xop> mapping of an ill, changing the
 286  286   *   <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
 287  287   *   writer for the actual duration of the insertion/deletion/change.
 288  288   *
 289  289   * - ill_lock:  This is a per ill mutex.
 290  290   *   It protects some members of the ill_t struct; see ip.h for details.
 291  291   *   It also protects the <ill-phyint> assoc.
 292  292   *   It also protects the list of ipifs hanging off the ill.
 293  293   *
 294  294   * - ipsq_lock: This is a per ipsq_t mutex lock.
 295  295   *   This protects some members of the ipsq_t struct; see ip.h for details.
 296  296   *   It also protects the <ipsq-ipxop> mapping
 297  297   *
 298  298   * - ipx_lock: This is a per ipxop_t mutex lock.
 299  299   *   This protects some members of the ipxop_t struct; see ip.h for details.
 300  300   *
 301  301   * - phyint_lock: This is a per phyint mutex lock. Protects just the
 302  302   *   phyint_flags
 303  303   *
 304  304   * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
 305  305   *   This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
 306  306   *   uniqueness check also done atomically.
 307  307   *
 308  308   * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
 309  309   *   group list linked by ill_usesrc_grp_next. It also protects the
 310  310   *   ill_usesrc_ifindex field. It is taken as a writer when a member of the
 311  311   *   group is being added or deleted.  This lock is taken as a reader when
 312  312   *   walking the list/group(eg: to get the number of members in a usesrc group).
 313  313   *   Note, it is only necessary to take this lock if the ill_usesrc_grp_next
 314  314   *   field is changing state i.e from NULL to non-NULL or vice-versa. For
 315  315   *   example, it is not necessary to take this lock in the initial portion
 316  316   *   of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
 317  317   *   operations are executed exclusively and that ensures that the "usesrc
 318  318   *   group state" cannot change. The "usesrc group state" change can happen
 319  319   *   only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
 320  320   *
 321  321   * Changing <ill-phyint>, <ipsq-xop> assocications:
 322  322   *
 323  323   * To change the <ill-phyint> association, the ill_g_lock must be held
 324  324   * as writer, and the ill_locks of both the v4 and v6 instance of the ill
 325  325   * must be held.
 326  326   *
 327  327   * To change the <ipsq-xop> association, the ill_g_lock must be held as
 328  328   * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
 329  329   * This is only done when ills are added or removed from IPMP groups.
 330  330   *
 331  331   * To add or delete an ipif from the list of ipifs hanging off the ill,
 332  332   * ill_g_lock (writer) and ill_lock must be held and the thread must be
 333  333   * a writer on the associated ipsq.
 334  334   *
 335  335   * To add or delete an ill to the system, the ill_g_lock must be held as
 336  336   * writer and the thread must be a writer on the associated ipsq.
 337  337   *
 338  338   * To add or delete an ilm to an ill, the ill_lock must be held and the thread
 339  339   * must be a writer on the associated ipsq.
 340  340   *
 341  341   * Lock hierarchy
 342  342   *
 343  343   * Some lock hierarchy scenarios are listed below.
 344  344   *
 345  345   * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
 346  346   * ill_g_lock -> ill_lock(s) -> phyint_lock
 347  347   * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
 348  348   * ill_g_lock -> ip_addr_avail_lock
 349  349   * conn_lock -> irb_lock -> ill_lock -> ire_lock
 350  350   * ill_g_lock -> ip_g_nd_lock
 351  351   * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
 352  352   * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
 353  353   * arl_lock -> ill_lock
 354  354   * ips_ire_dep_lock -> irb_lock
 355  355   *
 356  356   * When more than 1 ill lock is needed to be held, all ill lock addresses
 357  357   * are sorted on address and locked starting from highest addressed lock
 358  358   * downward.
 359  359   *
 360  360   * Multicast scenarios
 361  361   * ips_ill_g_lock -> ill_mcast_lock
 362  362   * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
 363  363   * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
 364  364   * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
 365  365   * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
 366  366   * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
 367  367   *
 368  368   * IPsec scenarios
 369  369   *
 370  370   * ipsa_lock -> ill_g_lock -> ill_lock
 371  371   * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
 372  372   *
 373  373   * Trusted Solaris scenarios
 374  374   *
 375  375   * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
 376  376   * igsa_lock -> gcdb_lock
 377  377   * gcgrp_rwlock -> ire_lock
 378  378   * gcgrp_rwlock -> gcdb_lock
 379  379   *
 380  380   * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
 381  381   *
 382  382   * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
 383  383   * sq_lock -> conn_lock -> QLOCK(q)
 384  384   * ill_lock -> ft_lock -> fe_lock
 385  385   *
 386  386   * Routing/forwarding table locking notes:
 387  387   *
 388  388   * Lock acquisition order: Radix tree lock, irb_lock.
 389  389   * Requirements:
 390  390   * i.  Walker must not hold any locks during the walker callback.
 391  391   * ii  Walker must not see a truncated tree during the walk because of any node
 392  392   *     deletion.
 393  393   * iii Existing code assumes ire_bucket is valid if it is non-null and is used
 394  394   *     in many places in the code to walk the irb list. Thus even if all the
 395  395   *     ires in a bucket have been deleted, we still can't free the radix node
 396  396   *     until the ires have actually been inactive'd (freed).
 397  397   *
 398  398   * Tree traversal - Need to hold the global tree lock in read mode.
 399  399   * Before dropping the global tree lock, need to either increment the ire_refcnt
 400  400   * to ensure that the radix node can't be deleted.
 401  401   *
 402  402   * Tree add - Need to hold the global tree lock in write mode to add a
 403  403   * radix node. To prevent the node from being deleted, increment the
 404  404   * irb_refcnt, after the node is added to the tree. The ire itself is
 405  405   * added later while holding the irb_lock, but not the tree lock.
 406  406   *
 407  407   * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
 408  408   * All associated ires must be inactive (i.e. freed), and irb_refcnt
 409  409   * must be zero.
 410  410   *
 411  411   * Walker - Increment irb_refcnt before calling the walker callback. Hold the
 412  412   * global tree lock (read mode) for traversal.
 413  413   *
 414  414   * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
 415  415   * hence we will acquire irb_lock while holding ips_ire_dep_lock.
 416  416   *
 417  417   * IPsec notes :
 418  418   *
 419  419   * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
 420  420   * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
 421  421   * ip_xmit_attr_t has the
 422  422   * information used by the IPsec code for applying the right level of
 423  423   * protection. The information initialized by IP in the ip_xmit_attr_t
 424  424   * is determined by the per-socket policy or global policy in the system.
 425  425   * For inbound datagrams, the ip_recv_attr_t
 426  426   * starts out with nothing in it. It gets filled
 427  427   * with the right information if it goes through the AH/ESP code, which
 428  428   * happens if the incoming packet is secure. The information initialized
 429  429   * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
 430  430   * the policy requirements needed by per-socket policy or global policy
 431  431   * is met or not.
 432  432   *
 433  433   * For fully connected sockets i.e dst, src [addr, port] is known,
 434  434   * conn_policy_cached is set indicating that policy has been cached.
 435  435   * conn_in_enforce_policy may or may not be set depending on whether
 436  436   * there is a global policy match or per-socket policy match.
 437  437   * Policy inheriting happpens in ip_policy_set once the destination is known.
 438  438   * Once the right policy is set on the conn_t, policy cannot change for
 439  439   * this socket. This makes life simpler for TCP (UDP ?) where
 440  440   * re-transmissions go out with the same policy. For symmetry, policy
 441  441   * is cached for fully connected UDP sockets also. Thus if policy is cached,
 442  442   * it also implies that policy is latched i.e policy cannot change
 443  443   * on these sockets. As we have the right policy on the conn, we don't
 444  444   * have to lookup global policy for every outbound and inbound datagram
 445  445   * and thus serving as an optimization. Note that a global policy change
 446  446   * does not affect fully connected sockets if they have policy. If fully
 447  447   * connected sockets did not have any policy associated with it, global
 448  448   * policy change may affect them.
 449  449   *
 450  450   * IP Flow control notes:
 451  451   * ---------------------
 452  452   * Non-TCP streams are flow controlled by IP. The way this is accomplished
 453  453   * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
 454  454   * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
 455  455   * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
 456  456   * functions.
 457  457   *
 458  458   * Per Tx ring udp flow control:
 459  459   * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
 460  460   * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
 461  461   *
 462  462   * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
 463  463   * To achieve best performance, outgoing traffic need to be fanned out among
 464  464   * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
 465  465   * traffic out of the NIC and it takes a fanout hint. UDP connections pass
 466  466   * the address of connp as fanout hint to mac_tx(). Under flow controlled
 467  467   * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
 468  468   * cookie points to a specific Tx ring that is blocked. The cookie is used to
 469  469   * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
 470  470   * point to drain_lists (idl_t's). These drain list will store the blocked UDP
 471  471   * connp's. The drain list is not a single list but a configurable number of
 472  472   * lists.
 473  473   *
 474  474   * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
 475  475   * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
 476  476   * which is equal to 128. This array in turn contains a pointer to idl_t[],
 477  477   * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
 478  478   * list will point to the list of connp's that are flow controlled.
 479  479   *
 480  480   *                      ---------------   -------   -------   -------
 481  481   *                   |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
 482  482   *                   |  ---------------   -------   -------   -------
 483  483   *                   |  ---------------   -------   -------   -------
 484  484   *                   |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
 485  485   * ----------------  |  ---------------   -------   -------   -------
 486  486   * |idl_tx_list[0]|->|  ---------------   -------   -------   -------
 487  487   * ----------------  |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
 488  488   *                   |  ---------------   -------   -------   -------
 489  489   *                   .        .              .         .         .
 490  490   *                   |  ---------------   -------   -------   -------
 491  491   *                   |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
 492  492   *                      ---------------   -------   -------   -------
 493  493   *                      ---------------   -------   -------   -------
 494  494   *                   |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
 495  495   *                   |  ---------------   -------   -------   -------
 496  496   *                   |  ---------------   -------   -------   -------
 497  497   * ----------------  |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
 498  498   * |idl_tx_list[1]|->|  ---------------   -------   -------   -------
 499  499   * ----------------  |        .              .         .         .
 500  500   *                   |  ---------------   -------   -------   -------
 501  501   *                   |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
 502  502   *                      ---------------   -------   -------   -------
 503  503   *     .....
 504  504   * ----------------
 505  505   * |idl_tx_list[n]|-> ...
 506  506   * ----------------
 507  507   *
 508  508   * When mac_tx() returns a cookie, the cookie is hashed into an index into
 509  509   * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
 510  510   * to insert the conn onto.  conn_drain_insert() asserts flow control for the
 511  511   * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
 512  512   * Further, conn_blocked is set to indicate that the conn is blocked.
 513  513   *
 514  514   * GLDv3 calls ill_flow_enable() when flow control is relieved.  The cookie
 515  515   * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
 516  516   * is again hashed to locate the appropriate idl_tx_list, which is then
 517  517   * drained via conn_walk_drain().  conn_walk_drain() goes through each conn in
 518  518   * the drain list and calls conn_drain_remove() to clear flow control (via
 519  519   * calling su_txq_full() or clearing QFULL), and remove the conn from the
 520  520   * drain list.
 521  521   *
 522  522   * Note that the drain list is not a single list but a (configurable) array of
 523  523   * lists (8 elements by default).  Synchronization between drain insertion and
 524  524   * flow control wakeup is handled by using idl_txl->txl_lock, and only
 525  525   * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
 526  526   *
 527  527   * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
 528  528   * On the send side, if the packet cannot be sent down to the driver by IP
 529  529   * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
 530  530   * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
 531  531   * the 0'th drain list.  When ip_wsrv() runs on the ill_wq because flow
 532  532   * control has been relieved, the blocked conns in the 0'th drain list are
 533  533   * drained as in the non-STREAMS case.
 534  534   *
 535  535   * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
 536  536   * is done when the conn is inserted into the drain list (conn_drain_insert())
 537  537   * and cleared when the conn is removed from the it (conn_drain_remove()).
 538  538   *
 539  539   * IPQOS notes:
 540  540   *
 541  541   * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
 542  542   * and IPQoS modules. IPPF includes hooks in IP at different control points
 543  543   * (callout positions) which direct packets to IPQoS modules for policy
 544  544   * processing. Policies, if present, are global.
 545  545   *
 546  546   * The callout positions are located in the following paths:
 547  547   *              o local_in (packets destined for this host)
 548  548   *              o local_out (packets orginating from this host )
 549  549   *              o fwd_in  (packets forwarded by this m/c - inbound)
 550  550   *              o fwd_out (packets forwarded by this m/c - outbound)
 551  551   * Hooks at these callout points can be enabled/disabled using the ndd variable
 552  552   * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
 553  553   * By default all the callout positions are enabled.
 554  554   *
 555  555   * Outbound (local_out)
 556  556   * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
 557  557   *
 558  558   * Inbound (local_in)
 559  559   * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
 560  560   *
 561  561   * Forwarding (in and out)
 562  562   * Hooks are placed in ire_recv_forward_v4/v6.
 563  563   *
 564  564   * IP Policy Framework processing (IPPF processing)
 565  565   * Policy processing for a packet is initiated by ip_process, which ascertains
 566  566   * that the classifier (ipgpc) is loaded and configured, failing which the
 567  567   * packet resumes normal processing in IP. If the clasifier is present, the
 568  568   * packet is acted upon by one or more IPQoS modules (action instances), per
 569  569   * filters configured in ipgpc and resumes normal IP processing thereafter.
 570  570   * An action instance can drop a packet in course of its processing.
 571  571   *
 572  572   * Zones notes:
 573  573   *
 574  574   * The partitioning rules for networking are as follows:
 575  575   * 1) Packets coming from a zone must have a source address belonging to that
 576  576   * zone.
 577  577   * 2) Packets coming from a zone can only be sent on a physical interface on
 578  578   * which the zone has an IP address.
 579  579   * 3) Between two zones on the same machine, packet delivery is only allowed if
 580  580   * there's a matching route for the destination and zone in the forwarding
 581  581   * table.
 582  582   * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
 583  583   * different zones can bind to the same port with the wildcard address
 584  584   * (INADDR_ANY).
 585  585   *
 586  586   * The granularity of interface partitioning is at the logical interface level.
 587  587   * Therefore, every zone has its own IP addresses, and incoming packets can be
 588  588   * attributed to a zone unambiguously. A logical interface is placed into a zone
 589  589   * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
 590  590   * structure. Rule (1) is implemented by modifying the source address selection
 591  591   * algorithm so that the list of eligible addresses is filtered based on the
 592  592   * sending process zone.
 593  593   *
 594  594   * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
 595  595   * across all zones, depending on their type. Here is the break-up:
 596  596   *
 597  597   * IRE type                             Shared/exclusive
 598  598   * --------                             ----------------
 599  599   * IRE_BROADCAST                        Exclusive
 600  600   * IRE_DEFAULT (default routes)         Shared (*)
 601  601   * IRE_LOCAL                            Exclusive (x)
 602  602   * IRE_LOOPBACK                         Exclusive
 603  603   * IRE_PREFIX (net routes)              Shared (*)
 604  604   * IRE_IF_NORESOLVER (interface routes) Exclusive
 605  605   * IRE_IF_RESOLVER (interface routes)   Exclusive
 606  606   * IRE_IF_CLONE (interface routes)      Exclusive
 607  607   * IRE_HOST (host routes)               Shared (*)
 608  608   *
 609  609   * (*) A zone can only use a default or off-subnet route if the gateway is
 610  610   * directly reachable from the zone, that is, if the gateway's address matches
 611  611   * one of the zone's logical interfaces.
 612  612   *
 613  613   * (x) IRE_LOCAL are handled a bit differently.
 614  614   * When ip_restrict_interzone_loopback is set (the default),
 615  615   * ire_route_recursive restricts loopback using an IRE_LOCAL
 616  616   * between zone to the case when L2 would have conceptually looped the packet
 617  617   * back, i.e. the loopback which is required since neither Ethernet drivers
 618  618   * nor Ethernet hardware loops them back. This is the case when the normal
 619  619   * routes (ignoring IREs with different zoneids) would send out the packet on
 620  620   * the same ill as the ill with which is IRE_LOCAL is associated.
 621  621   *
 622  622   * Multiple zones can share a common broadcast address; typically all zones
 623  623   * share the 255.255.255.255 address. Incoming as well as locally originated
 624  624   * broadcast packets must be dispatched to all the zones on the broadcast
 625  625   * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
 626  626   * since some zones may not be on the 10.16.72/24 network. To handle this, each
 627  627   * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
 628  628   * sent to every zone that has an IRE_BROADCAST entry for the destination
 629  629   * address on the input ill, see ip_input_broadcast().
 630  630   *
 631  631   * Applications in different zones can join the same multicast group address.
 632  632   * The same logic applies for multicast as for broadcast. ip_input_multicast
 633  633   * dispatches packets to all zones that have members on the physical interface.
 634  634   */
 635  635  
 636  636  /*
 637  637   * Squeue Fanout flags:
 638  638   *      0: No fanout.
 639  639   *      1: Fanout across all squeues
 640  640   */
 641  641  boolean_t       ip_squeue_fanout = 0;
 642  642  
 643  643  /*
 644  644   * Maximum dups allowed per packet.
 645  645   */
 646  646  uint_t ip_max_frag_dups = 10;
 647  647  
 648  648  static int      ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
 649  649                      cred_t *credp, boolean_t isv6);
 650  650  static mblk_t   *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
 651  651  
 652  652  static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
 653  653  static void     icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
 654  654  static void     icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
 655  655      ip_recv_attr_t *);
 656  656  static void     icmp_options_update(ipha_t *);
 657  657  static void     icmp_param_problem(mblk_t *, uint8_t,  ip_recv_attr_t *);
 658  658  static void     icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
 659  659  static mblk_t   *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
 660  660  static void     icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
 661  661      ip_recv_attr_t *);
 662  662  static void     icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
 663  663  static void     icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
 664  664      ip_recv_attr_t *);
 665  665  
 666  666  mblk_t          *ip_dlpi_alloc(size_t, t_uscalar_t);
 667  667  char            *ip_dot_addr(ipaddr_t, char *);
 668  668  mblk_t          *ip_carve_mp(mblk_t **, ssize_t);
 669  669  static char     *ip_dot_saddr(uchar_t *, char *);
 670  670  static int      ip_lrput(queue_t *, mblk_t *);
 671  671  ipaddr_t        ip_net_mask(ipaddr_t);
 672  672  char            *ip_nv_lookup(nv_t *, int);
 673  673  int             ip_rput(queue_t *, mblk_t *);
 674  674  static void     ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
 675  675                      void *dummy_arg);
 676  676  int             ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
 677  677  static mblk_t   *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
 678  678                      mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
 679  679  static mblk_t   *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
 680  680                      ip_stack_t *, boolean_t);
 681  681  static mblk_t   *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
 682  682                      boolean_t);
 683  683  static mblk_t   *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
 684  684  static mblk_t   *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
 685  685  static mblk_t   *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
 686  686  static mblk_t   *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
 687  687  static mblk_t   *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
 688  688                      ip_stack_t *ipst, boolean_t);
 689  689  static mblk_t   *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
 690  690                      ip_stack_t *ipst, boolean_t);
 691  691  static mblk_t   *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
 692  692                      ip_stack_t *ipst);
 693  693  static mblk_t   *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
 694  694                      ip_stack_t *ipst);
 695  695  static mblk_t   *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
 696  696                      ip_stack_t *ipst);
 697  697  static mblk_t   *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
 698  698                      ip_stack_t *ipst);
 699  699  static mblk_t   *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
 700  700                      ip_stack_t *ipst);
 701  701  static mblk_t   *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
 702  702                      ip_stack_t *ipst);
 703  703  static mblk_t   *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
 704  704                      ip_stack_t *ipst);
 705  705  static mblk_t   *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
 706  706                      ip_stack_t *ipst);
 707  707  static void     ip_snmp_get2_v4(ire_t *, iproutedata_t *);
 708  708  static void     ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
 709  709  static void     ip_snmp_get2_v4_media(ncec_t *, void *);
 710  710  static void     ip_snmp_get2_v6_media(ncec_t *, void *);
 711  711  int             ip_snmp_set(queue_t *, int, int, uchar_t *, int);
 712  712  
 713  713  static mblk_t   *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
 714  714                      mblk_t *);
 715  715  
 716  716  static void     conn_drain_init(ip_stack_t *);
 717  717  static void     conn_drain_fini(ip_stack_t *);
 718  718  static void     conn_drain(conn_t *connp, boolean_t closing);
 719  719  
 720  720  static void     conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
 721  721  static void     conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
 722  722  
 723  723  static void     *ip_stack_init(netstackid_t stackid, netstack_t *ns);
 724  724  static void     ip_stack_shutdown(netstackid_t stackid, void *arg);
 725  725  static void     ip_stack_fini(netstackid_t stackid, void *arg);
 726  726  
 727  727  static int      ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
 728  728      const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
 729  729      ire_t *, conn_t *, boolean_t, const in6_addr_t *,  mcast_record_t,
 730  730      const in6_addr_t *);
 731  731  
 732  732  static int      ip_squeue_switch(int);
 733  733  
 734  734  static void     *ip_kstat_init(netstackid_t, ip_stack_t *);
 735  735  static void     ip_kstat_fini(netstackid_t, kstat_t *);
 736  736  static int      ip_kstat_update(kstat_t *kp, int rw);
 737  737  static void     *icmp_kstat_init(netstackid_t);
 738  738  static void     icmp_kstat_fini(netstackid_t, kstat_t *);
 739  739  static int      icmp_kstat_update(kstat_t *kp, int rw);
 740  740  static void     *ip_kstat2_init(netstackid_t, ip_stat_t *);
 741  741  static void     ip_kstat2_fini(netstackid_t, kstat_t *);
 742  742  
 743  743  static void     ipobs_init(ip_stack_t *);
 744  744  static void     ipobs_fini(ip_stack_t *);
 745  745  
 746  746  static int      ip_tp_cpu_update(cpu_setup_t, int, void *);
 747  747  
 748  748  ipaddr_t        ip_g_all_ones = IP_HOST_MASK;
 749  749  
 750  750  static long ip_rput_pullups;
 751  751  int     dohwcksum = 1;  /* use h/w cksum if supported by the hardware */
 752  752  
 753  753  vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
 754  754  vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
 755  755  
 756  756  int     ip_debug;
 757  757  
 758  758  /*
 759  759   * Multirouting/CGTP stuff
 760  760   */
 761  761  int     ip_cgtp_filter_rev = CGTP_FILTER_REV;   /* CGTP hooks version */
 762  762  
 763  763  /*
 764  764   * IP tunables related declarations. Definitions are in ip_tunables.c
 765  765   */
 766  766  extern mod_prop_info_t ip_propinfo_tbl[];
 767  767  extern int ip_propinfo_count;
 768  768  
 769  769  /*
 770  770   * Table of IP ioctls encoding the various properties of the ioctl and
 771  771   * indexed based on the last byte of the ioctl command. Occasionally there
 772  772   * is a clash, and there is more than 1 ioctl with the same last byte.
 773  773   * In such a case 1 ioctl is encoded in the ndx table and the remaining
 774  774   * ioctls are encoded in the misc table. An entry in the ndx table is
 775  775   * retrieved by indexing on the last byte of the ioctl command and comparing
 776  776   * the ioctl command with the value in the ndx table. In the event of a
 777  777   * mismatch the misc table is then searched sequentially for the desired
 778  778   * ioctl command.
 779  779   *
 780  780   * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
 781  781   */
 782  782  ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
 783  783          /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 784  784          /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 785  785          /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 786  786          /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 787  787          /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 788  788          /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 789  789          /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 790  790          /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 791  791          /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 792  792          /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 793  793  
 794  794          /* 010 */ { SIOCADDRT,  sizeof (struct rtentry), IPI_PRIV,
 795  795                          MISC_CMD, ip_siocaddrt, NULL },
 796  796          /* 011 */ { SIOCDELRT,  sizeof (struct rtentry), IPI_PRIV,
 797  797                          MISC_CMD, ip_siocdelrt, NULL },
 798  798  
 799  799          /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 800  800                          IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
 801  801          /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
 802  802                          IF_CMD, ip_sioctl_get_addr, NULL },
 803  803  
 804  804          /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 805  805                          IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
 806  806          /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
 807  807                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
 808  808  
 809  809          /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
 810  810                          IPI_PRIV | IPI_WR,
 811  811                          IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
 812  812          /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
 813  813                          IPI_MODOK | IPI_GET_CMD,
 814  814                          IF_CMD, ip_sioctl_get_flags, NULL },
 815  815  
 816  816          /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 817  817          /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 818  818  
 819  819          /* copyin size cannot be coded for SIOCGIFCONF */
 820  820          /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
 821  821                          MISC_CMD, ip_sioctl_get_ifconf, NULL },
 822  822  
 823  823          /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 824  824                          IF_CMD, ip_sioctl_mtu, NULL },
 825  825          /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
 826  826                          IF_CMD, ip_sioctl_get_mtu, NULL },
 827  827          /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
 828  828                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
 829  829          /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 830  830                          IF_CMD, ip_sioctl_brdaddr, NULL },
 831  831          /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
 832  832                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
 833  833          /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
 834  834                          IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
 835  835          /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
 836  836                          IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
 837  837          /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
 838  838                          IF_CMD, ip_sioctl_metric, NULL },
 839  839          /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 840  840  
 841  841          /* See 166-168 below for extended SIOC*XARP ioctls */
 842  842          /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
 843  843                          ARP_CMD, ip_sioctl_arp, NULL },
 844  844          /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
 845  845                          ARP_CMD, ip_sioctl_arp, NULL },
 846  846          /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
 847  847                          ARP_CMD, ip_sioctl_arp, NULL },
 848  848  
 849  849          /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 850  850          /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 851  851          /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 852  852          /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 853  853          /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 854  854          /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 855  855          /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 856  856          /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 857  857          /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 858  858          /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 859  859          /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 860  860          /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 861  861          /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 862  862          /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 863  863          /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 864  864          /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 865  865          /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 866  866          /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 867  867          /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 868  868          /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 869  869          /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 870  870  
 871  871          /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
 872  872                          MISC_CMD, if_unitsel, if_unitsel_restart },
 873  873  
 874  874          /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 875  875          /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 876  876          /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 877  877          /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 878  878          /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 879  879          /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 880  880          /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 881  881          /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 882  882          /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 883  883          /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 884  884          /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 885  885          /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 886  886          /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 887  887          /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 888  888          /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 889  889          /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 890  890          /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 891  891          /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 892  892  
 893  893          /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
 894  894                          IPI_PRIV | IPI_WR | IPI_MODOK,
 895  895                          IF_CMD, ip_sioctl_sifname, NULL },
 896  896  
 897  897          /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 898  898          /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 899  899          /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 900  900          /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 901  901          /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 902  902          /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 903  903          /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 904  904          /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 905  905          /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 906  906          /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 907  907          /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 908  908          /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 909  909          /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 910  910  
 911  911          /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
 912  912                          MISC_CMD, ip_sioctl_get_ifnum, NULL },
 913  913          /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
 914  914                          IF_CMD, ip_sioctl_get_muxid, NULL },
 915  915          /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
 916  916                          IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
 917  917  
 918  918          /* Both if and lif variants share same func */
 919  919          /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
 920  920                          IF_CMD, ip_sioctl_get_lifindex, NULL },
 921  921          /* Both if and lif variants share same func */
 922  922          /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
 923  923                          IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
 924  924  
 925  925          /* copyin size cannot be coded for SIOCGIFCONF */
 926  926          /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
 927  927                          MISC_CMD, ip_sioctl_get_ifconf, NULL },
 928  928          /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 929  929          /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 930  930          /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 931  931          /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 932  932          /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 933  933          /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 934  934          /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 935  935          /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 936  936          /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 937  937          /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 938  938          /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 939  939          /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 940  940          /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 941  941          /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 942  942          /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 943  943          /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 944  944          /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 945  945  
 946  946          /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
 947  947                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
 948  948                          ip_sioctl_removeif_restart },
 949  949          /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
 950  950                          IPI_GET_CMD | IPI_PRIV | IPI_WR,
 951  951                          LIF_CMD, ip_sioctl_addif, NULL },
 952  952  #define SIOCLIFADDR_NDX 112
 953  953          /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 954  954                          LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
 955  955          /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
 956  956                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
 957  957          /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 958  958                          LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
 959  959          /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
 960  960                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
 961  961          /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
 962  962                          IPI_PRIV | IPI_WR,
 963  963                          LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
 964  964          /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
 965  965                          IPI_GET_CMD | IPI_MODOK,
 966  966                          LIF_CMD, ip_sioctl_get_flags, NULL },
 967  967  
 968  968          /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 969  969          /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
 970  970  
 971  971          /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
 972  972                          ip_sioctl_get_lifconf, NULL },
 973  973          /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 974  974                          LIF_CMD, ip_sioctl_mtu, NULL },
 975  975          /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
 976  976                          LIF_CMD, ip_sioctl_get_mtu, NULL },
 977  977          /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
 978  978                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
 979  979          /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 980  980                          LIF_CMD, ip_sioctl_brdaddr, NULL },
 981  981          /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
 982  982                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
 983  983          /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 984  984                          LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
 985  985          /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
 986  986                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
 987  987          /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
 988  988                          LIF_CMD, ip_sioctl_metric, NULL },
 989  989          /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
 990  990                          IPI_PRIV | IPI_WR | IPI_MODOK,
 991  991                          LIF_CMD, ip_sioctl_slifname,
 992  992                          ip_sioctl_slifname_restart },
 993  993  
 994  994          /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
 995  995                          MISC_CMD, ip_sioctl_get_lifnum, NULL },
 996  996          /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
 997  997                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
 998  998          /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
 999  999                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
1000 1000          /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1001 1001                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
1002 1002          /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1003 1003                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
1004 1004          /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1005 1005                          LIF_CMD, ip_sioctl_token, NULL },
1006 1006          /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1007 1007                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
1008 1008          /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1009 1009                          LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1010 1010          /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1011 1011                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
1012 1012          /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1013 1013                          LIF_CMD, ip_sioctl_lnkinfo, NULL },
1014 1014  
1015 1015          /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1016 1016                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1017 1017          /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1018 1018                          LIF_CMD, ip_siocdelndp_v6, NULL },
1019 1019          /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1020 1020                          LIF_CMD, ip_siocqueryndp_v6, NULL },
1021 1021          /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1022 1022                          LIF_CMD, ip_siocsetndp_v6, NULL },
1023 1023          /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1024 1024                          MISC_CMD, ip_sioctl_tmyaddr, NULL },
1025 1025          /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1026 1026                          MISC_CMD, ip_sioctl_tonlink, NULL },
1027 1027          /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1028 1028                          MISC_CMD, ip_sioctl_tmysite, NULL },
1029 1029          /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1030 1030          /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1031 1031  
1032 1032          /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
1033 1033          /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1034 1034          /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1035 1035          /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1036 1036          /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 1037  
1038 1038          /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1039 1039  
1040 1040          /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
1041 1041                          LIF_CMD, ip_sioctl_get_binding, NULL },
1042 1042          /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1043 1043                          IPI_PRIV | IPI_WR,
1044 1044                          LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1045 1045          /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1046 1046                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
1047 1047          /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
1048 1048                          IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
1049 1049  
1050 1050          /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1051 1051          /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1052 1052          /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1053 1053          /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1054 1054  
1055 1055          /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1056 1056  
1057 1057          /* These are handled in ip_sioctl_copyin_setup itself */
1058 1058          /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1059 1059                          MISC_CMD, NULL, NULL },
1060 1060          /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1061 1061                          MISC_CMD, NULL, NULL },
1062 1062          /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1063 1063  
1064 1064          /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1065 1065                          ip_sioctl_get_lifconf, NULL },
1066 1066  
1067 1067          /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1068 1068                          XARP_CMD, ip_sioctl_arp, NULL },
1069 1069          /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1070 1070                          XARP_CMD, ip_sioctl_arp, NULL },
1071 1071          /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1072 1072                          XARP_CMD, ip_sioctl_arp, NULL },
1073 1073  
1074 1074          /* SIOCPOPSOCKFS is not handled by IP */
1075 1075          /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1076 1076  
1077 1077          /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1078 1078                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1079 1079          /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1080 1080                          IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1081 1081                          ip_sioctl_slifzone_restart },
1082 1082          /* 172-174 are SCTP ioctls and not handled by IP */
1083 1083          /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1084 1084          /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1085 1085          /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1086 1086          /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1087 1087                          IPI_GET_CMD, LIF_CMD,
1088 1088                          ip_sioctl_get_lifusesrc, 0 },
1089 1089          /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1090 1090                          IPI_PRIV | IPI_WR,
1091 1091                          LIF_CMD, ip_sioctl_slifusesrc,
1092 1092                          NULL },
1093 1093          /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1094 1094                          ip_sioctl_get_lifsrcof, NULL },
1095 1095          /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1096 1096                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1097 1097          /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1098 1098                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1099 1099          /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1100 1100                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1101 1101          /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1102 1102                          MSFILT_CMD, ip_sioctl_msfilter, NULL },
1103 1103          /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1104 1104          /* SIOCSENABLESDP is handled by SDP */
1105 1105          /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1106 1106          /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1107 1107          /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1108 1108                          IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1109 1109          /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1110 1110          /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1111 1111                          ip_sioctl_ilb_cmd, NULL },
1112 1112          /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL },
1113 1113          /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL},
1114 1114          /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1115 1115                          IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1116 1116          /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1117 1117                          LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1118 1118          /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1119 1119                          LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1120 1120  };
1121 1121  
1122 1122  int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1123 1123  
1124 1124  ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1125 1125          { I_LINK,       0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1126 1126          { I_UNLINK,     0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1127 1127          { I_PLINK,      0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1128 1128          { I_PUNLINK,    0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1129 1129          { ND_GET,       0, 0, 0, NULL, NULL },
1130 1130          { ND_SET,       0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1131 1131          { IP_IOCTL,     0, 0, 0, NULL, NULL },
1132 1132          { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1133 1133                  MISC_CMD, mrt_ioctl},
1134 1134          { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1135 1135                  MISC_CMD, mrt_ioctl},
1136 1136          { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1137 1137                  MISC_CMD, mrt_ioctl}
1138 1138  };
1139 1139  
1140 1140  int ip_misc_ioctl_count =
1141 1141      sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1142 1142  
1143 1143  int     conn_drain_nthreads;            /* Number of drainers reqd. */
1144 1144                                          /* Settable in /etc/system */
1145 1145  /* Defined in ip_ire.c */
1146 1146  extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1147 1147  extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1148 1148  extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1149 1149  
1150 1150  static nv_t     ire_nv_arr[] = {
1151 1151          { IRE_BROADCAST, "BROADCAST" },
1152 1152          { IRE_LOCAL, "LOCAL" },
1153 1153          { IRE_LOOPBACK, "LOOPBACK" },
1154 1154          { IRE_DEFAULT, "DEFAULT" },
1155 1155          { IRE_PREFIX, "PREFIX" },
1156 1156          { IRE_IF_NORESOLVER, "IF_NORESOL" },
1157 1157          { IRE_IF_RESOLVER, "IF_RESOLV" },
1158 1158          { IRE_IF_CLONE, "IF_CLONE" },
1159 1159          { IRE_HOST, "HOST" },
1160 1160          { IRE_MULTICAST, "MULTICAST" },
1161 1161          { IRE_NOROUTE, "NOROUTE" },
1162 1162          { 0 }
1163 1163  };
1164 1164  
1165 1165  nv_t    *ire_nv_tbl = ire_nv_arr;
1166 1166  
1167 1167  /* Simple ICMP IP Header Template */
1168 1168  static ipha_t icmp_ipha = {
1169 1169          IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1170 1170  };
1171 1171  
1172 1172  struct module_info ip_mod_info = {
1173 1173          IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1174 1174          IP_MOD_LOWAT
1175 1175  };
1176 1176  
1177 1177  /*
1178 1178   * Duplicate static symbols within a module confuses mdb; so we avoid the
1179 1179   * problem by making the symbols here distinct from those in udp.c.
1180 1180   */
1181 1181  
1182 1182  /*
1183 1183   * Entry points for IP as a device and as a module.
1184 1184   * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1185 1185   */
1186 1186  static struct qinit iprinitv4 = {
1187 1187          ip_rput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1188 1188  };
1189 1189  
1190 1190  struct qinit iprinitv6 = {
1191 1191          ip_rput_v6, NULL, ip_openv6, ip_close, NULL, &ip_mod_info
1192 1192  };
1193 1193  
1194 1194  static struct qinit ipwinit = {
1195 1195          ip_wput_nondata, ip_wsrv, NULL, NULL, NULL, &ip_mod_info
1196 1196  };
1197 1197  
1198 1198  static struct qinit iplrinit = {
1199 1199          ip_lrput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1200 1200  };
1201 1201  
1202 1202  static struct qinit iplwinit = {
1203 1203          ip_lwput, NULL, NULL, NULL, NULL, &ip_mod_info
1204 1204  };
1205 1205  
1206 1206  /* For AF_INET aka /dev/ip */
1207 1207  struct streamtab ipinfov4 = {
1208 1208          &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1209 1209  };
1210 1210  
1211 1211  /* For AF_INET6 aka /dev/ip6 */
1212 1212  struct streamtab ipinfov6 = {
1213 1213          &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1214 1214  };
1215 1215  
1216 1216  #ifdef  DEBUG
1217 1217  boolean_t skip_sctp_cksum = B_FALSE;
1218 1218  #endif
1219 1219  
1220 1220  /*
1221 1221   * Generate an ICMP fragmentation needed message.
1222 1222   * When called from ip_output side a minimal ip_recv_attr_t needs to be
1223 1223   * constructed by the caller.
1224 1224   */
1225 1225  void
1226 1226  icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1227 1227  {
1228 1228          icmph_t icmph;
1229 1229          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
1230 1230  
1231 1231          mp = icmp_pkt_err_ok(mp, ira);
1232 1232          if (mp == NULL)
1233 1233                  return;
1234 1234  
1235 1235          bzero(&icmph, sizeof (icmph_t));
1236 1236          icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1237 1237          icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1238 1238          icmph.icmph_du_mtu = htons((uint16_t)mtu);
1239 1239          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1240 1240          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1241 1241  
1242 1242          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1243 1243  }
1244 1244  
1245 1245  /*
1246 1246   * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1247 1247   * If the ICMP message is consumed by IP, i.e., it should not be delivered
1248 1248   * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1249 1249   * Likewise, if the ICMP error is misformed (too short, etc), then it
1250 1250   * returns NULL. The caller uses this to determine whether or not to send
1251 1251   * to raw sockets.
1252 1252   *
1253 1253   * All error messages are passed to the matching transport stream.
1254 1254   *
1255 1255   * The following cases are handled by icmp_inbound:
1256 1256   * 1) It needs to send a reply back and possibly delivering it
1257 1257   *    to the "interested" upper clients.
1258 1258   * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1259 1259   * 3) It needs to change some values in IP only.
1260 1260   * 4) It needs to change some values in IP and upper layers e.g TCP
1261 1261   *    by delivering an error to the upper layers.
1262 1262   *
1263 1263   * We handle the above three cases in the context of IPsec in the
1264 1264   * following way :
1265 1265   *
1266 1266   * 1) Send the reply back in the same way as the request came in.
1267 1267   *    If it came in encrypted, it goes out encrypted. If it came in
1268 1268   *    clear, it goes out in clear. Thus, this will prevent chosen
1269 1269   *    plain text attack.
1270 1270   * 2) The client may or may not expect things to come in secure.
1271 1271   *    If it comes in secure, the policy constraints are checked
1272 1272   *    before delivering it to the upper layers. If it comes in
1273 1273   *    clear, ipsec_inbound_accept_clear will decide whether to
1274 1274   *    accept this in clear or not. In both the cases, if the returned
1275 1275   *    message (IP header + 8 bytes) that caused the icmp message has
1276 1276   *    AH/ESP headers, it is sent up to AH/ESP for validation before
1277 1277   *    sending up. If there are only 8 bytes of returned message, then
1278 1278   *    upper client will not be notified.
1279 1279   * 3) Check with global policy to see whether it matches the constaints.
1280 1280   *    But this will be done only if icmp_accept_messages_in_clear is
1281 1281   *    zero.
1282 1282   * 4) If we need to change both in IP and ULP, then the decision taken
1283 1283   *    while affecting the values in IP and while delivering up to TCP
1284 1284   *    should be the same.
1285 1285   *
1286 1286   *      There are two cases.
1287 1287   *
1288 1288   *      a) If we reject data at the IP layer (ipsec_check_global_policy()
1289 1289   *         failed), we will not deliver it to the ULP, even though they
1290 1290   *         are *willing* to accept in *clear*. This is fine as our global
1291 1291   *         disposition to icmp messages asks us reject the datagram.
1292 1292   *
1293 1293   *      b) If we accept data at the IP layer (ipsec_check_global_policy()
1294 1294   *         succeeded or icmp_accept_messages_in_clear is 1), and not able
1295 1295   *         to deliver it to ULP (policy failed), it can lead to
1296 1296   *         consistency problems. The cases known at this time are
1297 1297   *         ICMP_DESTINATION_UNREACHABLE  messages with following code
1298 1298   *         values :
1299 1299   *
1300 1300   *         - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1301 1301   *           and Upper layer rejects. Then the communication will
1302 1302   *           come to a stop. This is solved by making similar decisions
1303 1303   *           at both levels. Currently, when we are unable to deliver
1304 1304   *           to the Upper Layer (due to policy failures) while IP has
1305 1305   *           adjusted dce_pmtu, the next outbound datagram would
1306 1306   *           generate a local ICMP_FRAGMENTATION_NEEDED message - which
1307 1307   *           will be with the right level of protection. Thus the right
1308 1308   *           value will be communicated even if we are not able to
1309 1309   *           communicate when we get from the wire initially. But this
1310 1310   *           assumes there would be at least one outbound datagram after
1311 1311   *           IP has adjusted its dce_pmtu value. To make things
1312 1312   *           simpler, we accept in clear after the validation of
1313 1313   *           AH/ESP headers.
1314 1314   *
1315 1315   *         - Other ICMP ERRORS : We may not be able to deliver it to the
1316 1316   *           upper layer depending on the level of protection the upper
1317 1317   *           layer expects and the disposition in ipsec_inbound_accept_clear().
1318 1318   *           ipsec_inbound_accept_clear() decides whether a given ICMP error
1319 1319   *           should be accepted in clear when the Upper layer expects secure.
1320 1320   *           Thus the communication may get aborted by some bad ICMP
1321 1321   *           packets.
1322 1322   */
1323 1323  mblk_t *
1324 1324  icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1325 1325  {
1326 1326          icmph_t         *icmph;
1327 1327          ipha_t          *ipha;          /* Outer header */
1328 1328          int             ip_hdr_length;  /* Outer header length */
1329 1329          boolean_t       interested;
1330 1330          ipif_t          *ipif;
1331 1331          uint32_t        ts;
1332 1332          uint32_t        *tsp;
1333 1333          timestruc_t     now;
1334 1334          ill_t           *ill = ira->ira_ill;
1335 1335          ip_stack_t      *ipst = ill->ill_ipst;
1336 1336          zoneid_t        zoneid = ira->ira_zoneid;
1337 1337          int             len_needed;
1338 1338          mblk_t          *mp_ret = NULL;
1339 1339  
1340 1340          ipha = (ipha_t *)mp->b_rptr;
1341 1341  
1342 1342          BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1343 1343  
1344 1344          ip_hdr_length = ira->ira_ip_hdr_length;
1345 1345          if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1346 1346                  if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1347 1347                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1348 1348                          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1349 1349                          freemsg(mp);
1350 1350                          return (NULL);
1351 1351                  }
1352 1352                  /* Last chance to get real. */
1353 1353                  ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1354 1354                  if (ipha == NULL) {
1355 1355                          BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1356 1356                          freemsg(mp);
1357 1357                          return (NULL);
1358 1358                  }
1359 1359          }
1360 1360  
1361 1361          /* The IP header will always be a multiple of four bytes */
1362 1362          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1363 1363          ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1364 1364              icmph->icmph_code));
1365 1365  
1366 1366          /*
1367 1367           * We will set "interested" to "true" if we should pass a copy to
1368 1368           * the transport or if we handle the packet locally.
1369 1369           */
1370 1370          interested = B_FALSE;
1371 1371          switch (icmph->icmph_type) {
1372 1372          case ICMP_ECHO_REPLY:
1373 1373                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1374 1374                  break;
1375 1375          case ICMP_DEST_UNREACHABLE:
1376 1376                  if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1377 1377                          BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1378 1378                  interested = B_TRUE;    /* Pass up to transport */
1379 1379                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1380 1380                  break;
1381 1381          case ICMP_SOURCE_QUENCH:
1382 1382                  interested = B_TRUE;    /* Pass up to transport */
1383 1383                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1384 1384                  break;
1385 1385          case ICMP_REDIRECT:
1386 1386                  if (!ipst->ips_ip_ignore_redirect)
1387 1387                          interested = B_TRUE;
1388 1388                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1389 1389                  break;
1390 1390          case ICMP_ECHO_REQUEST:
1391 1391                  /*
1392 1392                   * Whether to respond to echo requests that come in as IP
1393 1393                   * broadcasts or as IP multicast is subject to debate
1394 1394                   * (what isn't?).  We aim to please, you pick it.
1395 1395                   * Default is do it.
1396 1396                   */
1397 1397                  if (ira->ira_flags & IRAF_MULTICAST) {
1398 1398                          /* multicast: respond based on tunable */
1399 1399                          interested = ipst->ips_ip_g_resp_to_echo_mcast;
1400 1400                  } else if (ira->ira_flags & IRAF_BROADCAST) {
1401 1401                          /* broadcast: respond based on tunable */
1402 1402                          interested = ipst->ips_ip_g_resp_to_echo_bcast;
1403 1403                  } else {
1404 1404                          /* unicast: always respond */
1405 1405                          interested = B_TRUE;
1406 1406                  }
1407 1407                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1408 1408                  if (!interested) {
1409 1409                          /* We never pass these to RAW sockets */
1410 1410                          freemsg(mp);
1411 1411                          return (NULL);
1412 1412                  }
1413 1413  
1414 1414                  /* Check db_ref to make sure we can modify the packet. */
1415 1415                  if (mp->b_datap->db_ref > 1) {
1416 1416                          mblk_t  *mp1;
1417 1417  
1418 1418                          mp1 = copymsg(mp);
1419 1419                          freemsg(mp);
1420 1420                          if (!mp1) {
1421 1421                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1422 1422                                  return (NULL);
1423 1423                          }
1424 1424                          mp = mp1;
1425 1425                          ipha = (ipha_t *)mp->b_rptr;
1426 1426                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1427 1427                  }
1428 1428                  icmph->icmph_type = ICMP_ECHO_REPLY;
1429 1429                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1430 1430                  icmp_send_reply_v4(mp, ipha, icmph, ira);
1431 1431                  return (NULL);
1432 1432  
1433 1433          case ICMP_ROUTER_ADVERTISEMENT:
1434 1434          case ICMP_ROUTER_SOLICITATION:
1435 1435                  break;
1436 1436          case ICMP_TIME_EXCEEDED:
1437 1437                  interested = B_TRUE;    /* Pass up to transport */
1438 1438                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1439 1439                  break;
1440 1440          case ICMP_PARAM_PROBLEM:
1441 1441                  interested = B_TRUE;    /* Pass up to transport */
1442 1442                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1443 1443                  break;
1444 1444          case ICMP_TIME_STAMP_REQUEST:
1445 1445                  /* Response to Time Stamp Requests is local policy. */
1446 1446                  if (ipst->ips_ip_g_resp_to_timestamp) {
1447 1447                          if (ira->ira_flags & IRAF_MULTIBROADCAST)
1448 1448                                  interested =
1449 1449                                      ipst->ips_ip_g_resp_to_timestamp_bcast;
1450 1450                          else
1451 1451                                  interested = B_TRUE;
1452 1452                  }
1453 1453                  if (!interested) {
1454 1454                          /* We never pass these to RAW sockets */
1455 1455                          freemsg(mp);
1456 1456                          return (NULL);
1457 1457                  }
1458 1458  
1459 1459                  /* Make sure we have enough of the packet */
1460 1460                  len_needed = ip_hdr_length + ICMPH_SIZE +
1461 1461                      3 * sizeof (uint32_t);
1462 1462  
1463 1463                  if (mp->b_wptr - mp->b_rptr < len_needed) {
1464 1464                          ipha = ip_pullup(mp, len_needed, ira);
1465 1465                          if (ipha == NULL) {
1466 1466                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1467 1467                                  ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1468 1468                                      mp, ill);
1469 1469                                  freemsg(mp);
1470 1470                                  return (NULL);
1471 1471                          }
1472 1472                          /* Refresh following the pullup. */
1473 1473                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1474 1474                  }
1475 1475                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1476 1476                  /* Check db_ref to make sure we can modify the packet. */
1477 1477                  if (mp->b_datap->db_ref > 1) {
1478 1478                          mblk_t  *mp1;
1479 1479  
1480 1480                          mp1 = copymsg(mp);
1481 1481                          freemsg(mp);
1482 1482                          if (!mp1) {
1483 1483                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1484 1484                                  return (NULL);
1485 1485                          }
1486 1486                          mp = mp1;
1487 1487                          ipha = (ipha_t *)mp->b_rptr;
1488 1488                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1489 1489                  }
1490 1490                  icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1491 1491                  tsp = (uint32_t *)&icmph[1];
1492 1492                  tsp++;          /* Skip past 'originate time' */
1493 1493                  /* Compute # of milliseconds since midnight */
1494 1494                  gethrestime(&now);
1495 1495                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1496 1496                      NSEC2MSEC(now.tv_nsec);
1497 1497                  *tsp++ = htonl(ts);     /* Lay in 'receive time' */
1498 1498                  *tsp++ = htonl(ts);     /* Lay in 'send time' */
1499 1499                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1500 1500                  icmp_send_reply_v4(mp, ipha, icmph, ira);
1501 1501                  return (NULL);
1502 1502  
1503 1503          case ICMP_TIME_STAMP_REPLY:
1504 1504                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1505 1505                  break;
1506 1506          case ICMP_INFO_REQUEST:
1507 1507                  /* Per RFC 1122 3.2.2.7, ignore this. */
1508 1508          case ICMP_INFO_REPLY:
1509 1509                  break;
1510 1510          case ICMP_ADDRESS_MASK_REQUEST:
1511 1511                  if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1512 1512                          interested =
1513 1513                              ipst->ips_ip_respond_to_address_mask_broadcast;
1514 1514                  } else {
1515 1515                          interested = B_TRUE;
1516 1516                  }
1517 1517                  if (!interested) {
1518 1518                          /* We never pass these to RAW sockets */
1519 1519                          freemsg(mp);
1520 1520                          return (NULL);
1521 1521                  }
1522 1522                  len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1523 1523                  if (mp->b_wptr - mp->b_rptr < len_needed) {
1524 1524                          ipha = ip_pullup(mp, len_needed, ira);
1525 1525                          if (ipha == NULL) {
1526 1526                                  BUMP_MIB(ill->ill_ip_mib,
1527 1527                                      ipIfStatsInTruncatedPkts);
1528 1528                                  ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1529 1529                                      ill);
1530 1530                                  freemsg(mp);
1531 1531                                  return (NULL);
1532 1532                          }
1533 1533                          /* Refresh following the pullup. */
1534 1534                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1535 1535                  }
1536 1536                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1537 1537                  /* Check db_ref to make sure we can modify the packet. */
1538 1538                  if (mp->b_datap->db_ref > 1) {
1539 1539                          mblk_t  *mp1;
1540 1540  
1541 1541                          mp1 = copymsg(mp);
1542 1542                          freemsg(mp);
1543 1543                          if (!mp1) {
1544 1544                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1545 1545                                  return (NULL);
1546 1546                          }
1547 1547                          mp = mp1;
1548 1548                          ipha = (ipha_t *)mp->b_rptr;
1549 1549                          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1550 1550                  }
1551 1551                  /*
1552 1552                   * Need the ipif with the mask be the same as the source
1553 1553                   * address of the mask reply. For unicast we have a specific
1554 1554                   * ipif. For multicast/broadcast we only handle onlink
1555 1555                   * senders, and use the source address to pick an ipif.
1556 1556                   */
1557 1557                  ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1558 1558                  if (ipif == NULL) {
1559 1559                          /* Broadcast or multicast */
1560 1560                          ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1561 1561                          if (ipif == NULL) {
1562 1562                                  freemsg(mp);
1563 1563                                  return (NULL);
1564 1564                          }
1565 1565                  }
1566 1566                  icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1567 1567                  bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1568 1568                  ipif_refrele(ipif);
1569 1569                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1570 1570                  icmp_send_reply_v4(mp, ipha, icmph, ira);
1571 1571                  return (NULL);
1572 1572  
1573 1573          case ICMP_ADDRESS_MASK_REPLY:
1574 1574                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1575 1575                  break;
1576 1576          default:
1577 1577                  interested = B_TRUE;    /* Pass up to transport */
1578 1578                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1579 1579                  break;
1580 1580          }
1581 1581          /*
1582 1582           * See if there is an ICMP client to avoid an extra copymsg/freemsg
1583 1583           * if there isn't one.
1584 1584           */
1585 1585          if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1586 1586                  /* If there is an ICMP client and we want one too, copy it. */
1587 1587  
1588 1588                  if (!interested) {
1589 1589                          /* Caller will deliver to RAW sockets */
1590 1590                          return (mp);
1591 1591                  }
1592 1592                  mp_ret = copymsg(mp);
1593 1593                  if (mp_ret == NULL) {
1594 1594                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1595 1595                          ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1596 1596                  }
1597 1597          } else if (!interested) {
1598 1598                  /* Neither we nor raw sockets are interested. Drop packet now */
1599 1599                  freemsg(mp);
1600 1600                  return (NULL);
1601 1601          }
1602 1602  
1603 1603          /*
1604 1604           * ICMP error or redirect packet. Make sure we have enough of
1605 1605           * the header and that db_ref == 1 since we might end up modifying
1606 1606           * the packet.
1607 1607           */
1608 1608          if (mp->b_cont != NULL) {
1609 1609                  if (ip_pullup(mp, -1, ira) == NULL) {
1610 1610                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1611 1611                          ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1612 1612                              mp, ill);
1613 1613                          freemsg(mp);
1614 1614                          return (mp_ret);
1615 1615                  }
1616 1616          }
1617 1617  
1618 1618          if (mp->b_datap->db_ref > 1) {
1619 1619                  mblk_t  *mp1;
1620 1620  
1621 1621                  mp1 = copymsg(mp);
1622 1622                  if (mp1 == NULL) {
1623 1623                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1624 1624                          ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1625 1625                          freemsg(mp);
1626 1626                          return (mp_ret);
1627 1627                  }
1628 1628                  freemsg(mp);
1629 1629                  mp = mp1;
1630 1630          }
1631 1631  
1632 1632          /*
1633 1633           * In case mp has changed, verify the message before any further
1634 1634           * processes.
1635 1635           */
1636 1636          ipha = (ipha_t *)mp->b_rptr;
1637 1637          icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1638 1638          if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1639 1639                  freemsg(mp);
1640 1640                  return (mp_ret);
1641 1641          }
1642 1642  
1643 1643          switch (icmph->icmph_type) {
1644 1644          case ICMP_REDIRECT:
1645 1645                  icmp_redirect_v4(mp, ipha, icmph, ira);
1646 1646                  break;
1647 1647          case ICMP_DEST_UNREACHABLE:
1648 1648                  if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1649 1649                          /* Update DCE and adjust MTU is icmp header if needed */
1650 1650                          icmp_inbound_too_big_v4(icmph, ira);
1651 1651                  }
1652 1652                  /* FALLTHROUGH */
1653 1653          default:
1654 1654                  icmp_inbound_error_fanout_v4(mp, icmph, ira);
1655 1655                  break;
1656 1656          }
1657 1657          return (mp_ret);
1658 1658  }
1659 1659  
1660 1660  /*
1661 1661   * Send an ICMP echo, timestamp or address mask reply.
1662 1662   * The caller has already updated the payload part of the packet.
1663 1663   * We handle the ICMP checksum, IP source address selection and feed
1664 1664   * the packet into ip_output_simple.
1665 1665   */
1666 1666  static void
1667 1667  icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1668 1668      ip_recv_attr_t *ira)
1669 1669  {
1670 1670          uint_t          ip_hdr_length = ira->ira_ip_hdr_length;
1671 1671          ill_t           *ill = ira->ira_ill;
1672 1672          ip_stack_t      *ipst = ill->ill_ipst;
1673 1673          ip_xmit_attr_t  ixas;
1674 1674  
1675 1675          /* Send out an ICMP packet */
1676 1676          icmph->icmph_checksum = 0;
1677 1677          icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1678 1678          /* Reset time to live. */
1679 1679          ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1680 1680          {
1681 1681                  /* Swap source and destination addresses */
1682 1682                  ipaddr_t tmp;
1683 1683  
1684 1684                  tmp = ipha->ipha_src;
1685 1685                  ipha->ipha_src = ipha->ipha_dst;
1686 1686                  ipha->ipha_dst = tmp;
1687 1687          }
1688 1688          ipha->ipha_ident = 0;
1689 1689          if (!IS_SIMPLE_IPH(ipha))
1690 1690                  icmp_options_update(ipha);
1691 1691  
1692 1692          bzero(&ixas, sizeof (ixas));
1693 1693          ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1694 1694          ixas.ixa_zoneid = ira->ira_zoneid;
1695 1695          ixas.ixa_cred = kcred;
1696 1696          ixas.ixa_cpid = NOPID;
1697 1697          ixas.ixa_tsl = ira->ira_tsl;    /* Behave as a multi-level responder */
1698 1698          ixas.ixa_ifindex = 0;
1699 1699          ixas.ixa_ipst = ipst;
1700 1700          ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1701 1701  
1702 1702          if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1703 1703                  /*
1704 1704                   * This packet should go out the same way as it
1705 1705                   * came in i.e in clear, independent of the IPsec policy
1706 1706                   * for transmitting packets.
1707 1707                   */
1708 1708                  ixas.ixa_flags |= IXAF_NO_IPSEC;
1709 1709          } else {
1710 1710                  if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1711 1711                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1712 1712                          /* Note: mp already consumed and ip_drop_packet done */
1713 1713                          return;
1714 1714                  }
1715 1715          }
1716 1716          if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1717 1717                  /*
1718 1718                   * Not one or our addresses (IRE_LOCALs), thus we let
1719 1719                   * ip_output_simple pick the source.
1720 1720                   */
1721 1721                  ipha->ipha_src = INADDR_ANY;
1722 1722                  ixas.ixa_flags |= IXAF_SET_SOURCE;
1723 1723          }
1724 1724          /* Should we send with DF and use dce_pmtu? */
1725 1725          if (ipst->ips_ipv4_icmp_return_pmtu) {
1726 1726                  ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1727 1727                  ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1728 1728          }
1729 1729  
1730 1730          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1731 1731  
1732 1732          (void) ip_output_simple(mp, &ixas);
1733 1733          ixa_cleanup(&ixas);
1734 1734  }
1735 1735  
1736 1736  /*
1737 1737   * Verify the ICMP messages for either for ICMP error or redirect packet.
1738 1738   * The caller should have fully pulled up the message. If it's a redirect
1739 1739   * packet, only basic checks on IP header will be done; otherwise, verify
1740 1740   * the packet by looking at the included ULP header.
1741 1741   *
1742 1742   * Called before icmp_inbound_error_fanout_v4 is called.
1743 1743   */
1744 1744  static boolean_t
1745 1745  icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1746 1746  {
1747 1747          ill_t           *ill = ira->ira_ill;
1748 1748          int             hdr_length;
1749 1749          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
1750 1750          conn_t          *connp;
1751 1751          ipha_t          *ipha;  /* Inner IP header */
1752 1752  
1753 1753          ipha = (ipha_t *)&icmph[1];
1754 1754          if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1755 1755                  goto truncated;
1756 1756  
1757 1757          hdr_length = IPH_HDR_LENGTH(ipha);
1758 1758  
1759 1759          if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1760 1760                  goto discard_pkt;
1761 1761  
1762 1762          if (hdr_length < sizeof (ipha_t))
1763 1763                  goto truncated;
1764 1764  
1765 1765          if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1766 1766                  goto truncated;
1767 1767  
1768 1768          /*
1769 1769           * Stop here for ICMP_REDIRECT.
1770 1770           */
1771 1771          if (icmph->icmph_type == ICMP_REDIRECT)
1772 1772                  return (B_TRUE);
1773 1773  
1774 1774          /*
1775 1775           * ICMP errors only.
1776 1776           */
1777 1777          switch (ipha->ipha_protocol) {
1778 1778          case IPPROTO_UDP:
1779 1779                  /*
1780 1780                   * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1781 1781                   * transport header.
1782 1782                   */
1783 1783                  if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1784 1784                      mp->b_wptr)
1785 1785                          goto truncated;
1786 1786                  break;
1787 1787          case IPPROTO_TCP: {
1788 1788                  tcpha_t         *tcpha;
1789 1789  
1790 1790                  /*
1791 1791                   * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1792 1792                   * transport header.
1793 1793                   */
1794 1794                  if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1795 1795                      mp->b_wptr)
1796 1796                          goto truncated;
1797 1797  
1798 1798                  tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1799 1799                  connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1800 1800                      ipst);
1801 1801                  if (connp == NULL)
1802 1802                          goto discard_pkt;
1803 1803  
1804 1804                  if ((connp->conn_verifyicmp != NULL) &&
1805 1805                      !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1806 1806                          CONN_DEC_REF(connp);
1807 1807                          goto discard_pkt;
1808 1808                  }
1809 1809                  CONN_DEC_REF(connp);
1810 1810                  break;
1811 1811          }
1812 1812          case IPPROTO_SCTP:
1813 1813                  /*
1814 1814                   * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1815 1815                   * transport header.
1816 1816                   */
1817 1817                  if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1818 1818                      mp->b_wptr)
1819 1819                          goto truncated;
1820 1820                  break;
1821 1821          case IPPROTO_ESP:
1822 1822          case IPPROTO_AH:
1823 1823                  break;
1824 1824          case IPPROTO_ENCAP:
1825 1825                  if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1826 1826                      mp->b_wptr)
1827 1827                          goto truncated;
1828 1828                  break;
1829 1829          default:
1830 1830                  break;
1831 1831          }
1832 1832  
1833 1833          return (B_TRUE);
1834 1834  
1835 1835  discard_pkt:
1836 1836          /* Bogus ICMP error. */
1837 1837          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1838 1838          return (B_FALSE);
1839 1839  
1840 1840  truncated:
1841 1841          /* We pulled up everthing already. Must be truncated */
1842 1842          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1843 1843          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1844 1844          return (B_FALSE);
1845 1845  }
1846 1846  
1847 1847  /* Table from RFC 1191 */
1848 1848  static int icmp_frag_size_table[] =
1849 1849  { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1850 1850  
1851 1851  /*
1852 1852   * Process received ICMP Packet too big.
1853 1853   * Just handles the DCE create/update, including using the above table of
1854 1854   * PMTU guesses. The caller is responsible for validating the packet before
1855 1855   * passing it in and also to fanout the ICMP error to any matching transport
1856 1856   * conns. Assumes the message has been fully pulled up and verified.
1857 1857   *
1858 1858   * Before getting here, the caller has called icmp_inbound_verify_v4()
1859 1859   * that should have verified with ULP to prevent undoing the changes we're
1860 1860   * going to make to DCE. For example, TCP might have verified that the packet
1861 1861   * which generated error is in the send window.
1862 1862   *
1863 1863   * In some cases modified this MTU in the ICMP header packet; the caller
1864 1864   * should pass to the matching ULP after this returns.
1865 1865   */
1866 1866  static void
1867 1867  icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1868 1868  {
1869 1869          dce_t           *dce;
1870 1870          int             old_mtu;
1871 1871          int             mtu, orig_mtu;
1872 1872          ipaddr_t        dst;
1873 1873          boolean_t       disable_pmtud;
1874 1874          ill_t           *ill = ira->ira_ill;
1875 1875          ip_stack_t      *ipst = ill->ill_ipst;
1876 1876          uint_t          hdr_length;
1877 1877          ipha_t          *ipha;
1878 1878  
1879 1879          /* Caller already pulled up everything. */
1880 1880          ipha = (ipha_t *)&icmph[1];
1881 1881          ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1882 1882              icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1883 1883          ASSERT(ill != NULL);
1884 1884  
1885 1885          hdr_length = IPH_HDR_LENGTH(ipha);
1886 1886  
1887 1887          /*
1888 1888           * We handle path MTU for source routed packets since the DCE
1889 1889           * is looked up using the final destination.
1890 1890           */
1891 1891          dst = ip_get_dst(ipha);
1892 1892  
1893 1893          dce = dce_lookup_and_add_v4(dst, ipst);
1894 1894          if (dce == NULL) {
1895 1895                  /* Couldn't add a unique one - ENOMEM */
1896 1896                  ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1897 1897                      ntohl(dst)));
1898 1898                  return;
1899 1899          }
1900 1900  
1901 1901          /* Check for MTU discovery advice as described in RFC 1191 */
1902 1902          mtu = ntohs(icmph->icmph_du_mtu);
1903 1903          orig_mtu = mtu;
1904 1904          disable_pmtud = B_FALSE;
1905 1905  
1906 1906          mutex_enter(&dce->dce_lock);
1907 1907          if (dce->dce_flags & DCEF_PMTU)
1908 1908                  old_mtu = dce->dce_pmtu;
1909 1909          else
1910 1910                  old_mtu = ill->ill_mtu;
1911 1911  
1912 1912          if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1913 1913                  uint32_t length;
1914 1914                  int     i;
1915 1915  
1916 1916                  /*
1917 1917                   * Use the table from RFC 1191 to figure out
1918 1918                   * the next "plateau" based on the length in
1919 1919                   * the original IP packet.
1920 1920                   */
1921 1921                  length = ntohs(ipha->ipha_length);
1922 1922                  DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1923 1923                      uint32_t, length);
1924 1924                  if (old_mtu <= length &&
1925 1925                      old_mtu >= length - hdr_length) {
1926 1926                          /*
1927 1927                           * Handle broken BSD 4.2 systems that
1928 1928                           * return the wrong ipha_length in ICMP
1929 1929                           * errors.
1930 1930                           */
1931 1931                          ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1932 1932                              length, old_mtu));
1933 1933                          length -= hdr_length;
1934 1934                  }
1935 1935                  for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1936 1936                          if (length > icmp_frag_size_table[i])
1937 1937                                  break;
1938 1938                  }
1939 1939                  if (i == A_CNT(icmp_frag_size_table)) {
1940 1940                          /* Smaller than IP_MIN_MTU! */
1941 1941                          ip1dbg(("Too big for packet size %d\n",
1942 1942                              length));
1943 1943                          disable_pmtud = B_TRUE;
1944 1944                          mtu = ipst->ips_ip_pmtu_min;
1945 1945                  } else {
1946 1946                          mtu = icmp_frag_size_table[i];
1947 1947                          ip1dbg(("Calculated mtu %d, packet size %d, "
1948 1948                              "before %d\n", mtu, length, old_mtu));
1949 1949                          if (mtu < ipst->ips_ip_pmtu_min) {
1950 1950                                  mtu = ipst->ips_ip_pmtu_min;
1951 1951                                  disable_pmtud = B_TRUE;
1952 1952                          }
1953 1953                  }
1954 1954          }
1955 1955          if (disable_pmtud)
1956 1956                  dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1957 1957          else
1958 1958                  dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1959 1959  
1960 1960          dce->dce_pmtu = MIN(old_mtu, mtu);
1961 1961          /* Prepare to send the new max frag size for the ULP. */
1962 1962          icmph->icmph_du_zero = 0;
1963 1963          icmph->icmph_du_mtu =  htons((uint16_t)dce->dce_pmtu);
1964 1964          DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1965 1965              dce, int, orig_mtu, int, mtu);
1966 1966  
1967 1967          /* We now have a PMTU for sure */
1968 1968          dce->dce_flags |= DCEF_PMTU;
1969 1969          dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1970 1970          mutex_exit(&dce->dce_lock);
1971 1971          /*
1972 1972           * After dropping the lock the new value is visible to everyone.
1973 1973           * Then we bump the generation number so any cached values reinspect
1974 1974           * the dce_t.
1975 1975           */
1976 1976          dce_increment_generation(dce);
1977 1977          dce_refrele(dce);
1978 1978  }
1979 1979  
1980 1980  /*
1981 1981   * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1982 1982   * calls this function.
1983 1983   */
1984 1984  static mblk_t *
1985 1985  icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1986 1986  {
1987 1987          int length;
1988 1988  
1989 1989          ASSERT(mp->b_datap->db_type == M_DATA);
1990 1990  
1991 1991          /* icmp_inbound_v4 has already pulled up the whole error packet */
1992 1992          ASSERT(mp->b_cont == NULL);
1993 1993  
1994 1994          /*
1995 1995           * The length that we want to overlay is the inner header
1996 1996           * and what follows it.
1997 1997           */
1998 1998          length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
1999 1999  
2000 2000          /*
2001 2001           * Overlay the inner header and whatever follows it over the
2002 2002           * outer header.
2003 2003           */
2004 2004          bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2005 2005  
2006 2006          /* Adjust for what we removed */
2007 2007          mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
2008 2008          return (mp);
2009 2009  }
2010 2010  
2011 2011  /*
2012 2012   * Try to pass the ICMP message upstream in case the ULP cares.
2013 2013   *
2014 2014   * If the packet that caused the ICMP error is secure, we send
2015 2015   * it to AH/ESP to make sure that the attached packet has a
2016 2016   * valid association. ipha in the code below points to the
2017 2017   * IP header of the packet that caused the error.
2018 2018   *
2019 2019   * For IPsec cases, we let the next-layer-up (which has access to
2020 2020   * cached policy on the conn_t, or can query the SPD directly)
2021 2021   * subtract out any IPsec overhead if they must.  We therefore make no
2022 2022   * adjustments here for IPsec overhead.
2023 2023   *
2024 2024   * IFN could have been generated locally or by some router.
2025 2025   *
2026 2026   * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
2027 2027   * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
2028 2028   *          This happens because IP adjusted its value of MTU on an
2029 2029   *          earlier IFN message and could not tell the upper layer,
2030 2030   *          the new adjusted value of MTU e.g. Packet was encrypted
2031 2031   *          or there was not enough information to fanout to upper
2032 2032   *          layers. Thus on the next outbound datagram, ire_send_wire
2033 2033   *          generates the IFN, where IPsec processing has *not* been
2034 2034   *          done.
2035 2035   *
2036 2036   *          Note that we retain ixa_fragsize across IPsec thus once
2037 2037   *          we have picking ixa_fragsize and entered ipsec_out_process we do
2038 2038   *          no change the fragsize even if the path MTU changes before
2039 2039   *          we reach ip_output_post_ipsec.
2040 2040   *
2041 2041   *          In the local case, IRAF_LOOPBACK will be set indicating
2042 2042   *          that IFN was generated locally.
2043 2043   *
2044 2044   * ROUTER : IFN could be secure or non-secure.
2045 2045   *
2046 2046   *          * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2047 2047   *            packet in error has AH/ESP headers to validate the AH/ESP
2048 2048   *            headers. AH/ESP will verify whether there is a valid SA or
2049 2049   *            not and send it back. We will fanout again if we have more
2050 2050   *            data in the packet.
2051 2051   *
2052 2052   *            If the packet in error does not have AH/ESP, we handle it
2053 2053   *            like any other case.
2054 2054   *
2055 2055   *          * NON_SECURE : If the packet in error has AH/ESP headers, we send it
2056 2056   *            up to AH/ESP for validation. AH/ESP will verify whether there is a
2057 2057   *            valid SA or not and send it back. We will fanout again if
2058 2058   *            we have more data in the packet.
2059 2059   *
2060 2060   *            If the packet in error does not have AH/ESP, we handle it
2061 2061   *            like any other case.
2062 2062   *
2063 2063   * The caller must have called icmp_inbound_verify_v4.
2064 2064   */
2065 2065  static void
2066 2066  icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2067 2067  {
2068 2068          uint16_t        *up;    /* Pointer to ports in ULP header */
2069 2069          uint32_t        ports;  /* reversed ports for fanout */
2070 2070          ipha_t          ripha;  /* With reversed addresses */
2071 2071          ipha_t          *ipha;  /* Inner IP header */
2072 2072          uint_t          hdr_length; /* Inner IP header length */
2073 2073          tcpha_t         *tcpha;
2074 2074          conn_t          *connp;
2075 2075          ill_t           *ill = ira->ira_ill;
2076 2076          ip_stack_t      *ipst = ill->ill_ipst;
2077 2077          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
2078 2078          ill_t           *rill = ira->ira_rill;
2079 2079  
2080 2080          /* Caller already pulled up everything. */
2081 2081          ipha = (ipha_t *)&icmph[1];
2082 2082          ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2083 2083          ASSERT(mp->b_cont == NULL);
2084 2084  
2085 2085          hdr_length = IPH_HDR_LENGTH(ipha);
2086 2086          ira->ira_protocol = ipha->ipha_protocol;
2087 2087  
2088 2088          /*
2089 2089           * We need a separate IP header with the source and destination
2090 2090           * addresses reversed to do fanout/classification because the ipha in
2091 2091           * the ICMP error is in the form we sent it out.
2092 2092           */
2093 2093          ripha.ipha_src = ipha->ipha_dst;
2094 2094          ripha.ipha_dst = ipha->ipha_src;
2095 2095          ripha.ipha_protocol = ipha->ipha_protocol;
2096 2096          ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2097 2097  
2098 2098          ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2099 2099              ripha.ipha_protocol, ntohl(ipha->ipha_src),
2100 2100              ntohl(ipha->ipha_dst),
2101 2101              icmph->icmph_type, icmph->icmph_code));
2102 2102  
2103 2103          switch (ipha->ipha_protocol) {
2104 2104          case IPPROTO_UDP:
2105 2105                  up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2106 2106  
2107 2107                  /* Attempt to find a client stream based on port. */
2108 2108                  ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2109 2109                      ntohs(up[0]), ntohs(up[1])));
2110 2110  
2111 2111                  /* Note that we send error to all matches. */
2112 2112                  ira->ira_flags |= IRAF_ICMP_ERROR;
2113 2113                  ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2114 2114                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2115 2115                  return;
2116 2116  
2117 2117          case IPPROTO_TCP:
2118 2118                  /*
2119 2119                   * Find a TCP client stream for this packet.
2120 2120                   * Note that we do a reverse lookup since the header is
2121 2121                   * in the form we sent it out.
2122 2122                   */
2123 2123                  tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2124 2124                  connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2125 2125                      ipst);
2126 2126                  if (connp == NULL)
2127 2127                          goto discard_pkt;
2128 2128  
2129 2129                  if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2130 2130                      (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2131 2131                          mp = ipsec_check_inbound_policy(mp, connp,
2132 2132                              ipha, NULL, ira);
2133 2133                          if (mp == NULL) {
2134 2134                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2135 2135                                  /* Note that mp is NULL */
2136 2136                                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
2137 2137                                  CONN_DEC_REF(connp);
2138 2138                                  return;
2139 2139                          }
2140 2140                  }
2141 2141  
2142 2142                  ira->ira_flags |= IRAF_ICMP_ERROR;
2143 2143                  ira->ira_ill = ira->ira_rill = NULL;
2144 2144                  if (IPCL_IS_TCP(connp)) {
2145 2145                          SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2146 2146                              connp->conn_recvicmp, connp, ira, SQ_FILL,
2147 2147                              SQTAG_TCP_INPUT_ICMP_ERR);
2148 2148                  } else {
2149 2149                          /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2150 2150                          (connp->conn_recv)(connp, mp, NULL, ira);
2151 2151                          CONN_DEC_REF(connp);
2152 2152                  }
2153 2153                  ira->ira_ill = ill;
2154 2154                  ira->ira_rill = rill;
2155 2155                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2156 2156                  return;
2157 2157  
2158 2158          case IPPROTO_SCTP:
2159 2159                  up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2160 2160                  /* Find a SCTP client stream for this packet. */
2161 2161                  ((uint16_t *)&ports)[0] = up[1];
2162 2162                  ((uint16_t *)&ports)[1] = up[0];
2163 2163  
2164 2164                  ira->ira_flags |= IRAF_ICMP_ERROR;
2165 2165                  ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2166 2166                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2167 2167                  return;
2168 2168  
2169 2169          case IPPROTO_ESP:
2170 2170          case IPPROTO_AH:
2171 2171                  if (!ipsec_loaded(ipss)) {
2172 2172                          ip_proto_not_sup(mp, ira);
2173 2173                          return;
2174 2174                  }
2175 2175  
2176 2176                  if (ipha->ipha_protocol == IPPROTO_ESP)
2177 2177                          mp = ipsecesp_icmp_error(mp, ira);
2178 2178                  else
2179 2179                          mp = ipsecah_icmp_error(mp, ira);
2180 2180                  if (mp == NULL)
2181 2181                          return;
2182 2182  
2183 2183                  /* Just in case ipsec didn't preserve the NULL b_cont */
2184 2184                  if (mp->b_cont != NULL) {
2185 2185                          if (!pullupmsg(mp, -1))
2186 2186                                  goto discard_pkt;
2187 2187                  }
2188 2188  
2189 2189                  /*
2190 2190                   * Note that ira_pktlen and ira_ip_hdr_length are no longer
2191 2191                   * correct, but we don't use them any more here.
2192 2192                   *
2193 2193                   * If succesful, the mp has been modified to not include
2194 2194                   * the ESP/AH header so we can fanout to the ULP's icmp
2195 2195                   * error handler.
2196 2196                   */
2197 2197                  if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2198 2198                          goto truncated;
2199 2199  
2200 2200                  /* Verify the modified message before any further processes. */
2201 2201                  ipha = (ipha_t *)mp->b_rptr;
2202 2202                  hdr_length = IPH_HDR_LENGTH(ipha);
2203 2203                  icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2204 2204                  if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2205 2205                          freemsg(mp);
2206 2206                          return;
2207 2207                  }
2208 2208  
2209 2209                  icmp_inbound_error_fanout_v4(mp, icmph, ira);
2210 2210                  return;
2211 2211  
2212 2212          case IPPROTO_ENCAP: {
2213 2213                  /* Look for self-encapsulated packets that caused an error */
2214 2214                  ipha_t *in_ipha;
2215 2215  
2216 2216                  /*
2217 2217                   * Caller has verified that length has to be
2218 2218                   * at least the size of IP header.
2219 2219                   */
2220 2220                  ASSERT(hdr_length >= sizeof (ipha_t));
2221 2221                  /*
2222 2222                   * Check the sanity of the inner IP header like
2223 2223                   * we did for the outer header.
2224 2224                   */
2225 2225                  in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2226 2226                  if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2227 2227                          goto discard_pkt;
2228 2228                  }
2229 2229                  if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2230 2230                          goto discard_pkt;
2231 2231                  }
2232 2232                  /* Check for Self-encapsulated tunnels */
2233 2233                  if (in_ipha->ipha_src == ipha->ipha_src &&
2234 2234                      in_ipha->ipha_dst == ipha->ipha_dst) {
2235 2235  
2236 2236                          mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2237 2237                              in_ipha);
2238 2238                          if (mp == NULL)
2239 2239                                  goto discard_pkt;
2240 2240  
2241 2241                          /*
2242 2242                           * Just in case self_encap didn't preserve the NULL
2243 2243                           * b_cont
2244 2244                           */
2245 2245                          if (mp->b_cont != NULL) {
2246 2246                                  if (!pullupmsg(mp, -1))
2247 2247                                          goto discard_pkt;
2248 2248                          }
2249 2249                          /*
2250 2250                           * Note that ira_pktlen and ira_ip_hdr_length are no
2251 2251                           * longer correct, but we don't use them any more here.
2252 2252                           */
2253 2253                          if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2254 2254                                  goto truncated;
2255 2255  
2256 2256                          /*
2257 2257                           * Verify the modified message before any further
2258 2258                           * processes.
2259 2259                           */
2260 2260                          ipha = (ipha_t *)mp->b_rptr;
2261 2261                          hdr_length = IPH_HDR_LENGTH(ipha);
2262 2262                          icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2263 2263                          if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2264 2264                                  freemsg(mp);
2265 2265                                  return;
2266 2266                          }
2267 2267  
2268 2268                          /*
2269 2269                           * The packet in error is self-encapsualted.
2270 2270                           * And we are finding it further encapsulated
2271 2271                           * which we could not have possibly generated.
2272 2272                           */
2273 2273                          if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2274 2274                                  goto discard_pkt;
2275 2275                          }
2276 2276                          icmp_inbound_error_fanout_v4(mp, icmph, ira);
2277 2277                          return;
2278 2278                  }
2279 2279                  /* No self-encapsulated */
2280 2280          }
2281 2281          /* FALLTHROUGH */
2282 2282          case IPPROTO_IPV6:
2283 2283                  if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2284 2284                      &ripha.ipha_dst, ipst)) != NULL) {
2285 2285                          ira->ira_flags |= IRAF_ICMP_ERROR;
2286 2286                          connp->conn_recvicmp(connp, mp, NULL, ira);
2287 2287                          CONN_DEC_REF(connp);
2288 2288                          ira->ira_flags &= ~IRAF_ICMP_ERROR;
2289 2289                          return;
2290 2290                  }
2291 2291                  /*
2292 2292                   * No IP tunnel is interested, fallthrough and see
2293 2293                   * if a raw socket will want it.
2294 2294                   */
2295 2295                  /* FALLTHROUGH */
2296 2296          default:
2297 2297                  ira->ira_flags |= IRAF_ICMP_ERROR;
2298 2298                  ip_fanout_proto_v4(mp, &ripha, ira);
2299 2299                  ira->ira_flags &= ~IRAF_ICMP_ERROR;
2300 2300                  return;
2301 2301          }
2302 2302          /* NOTREACHED */
2303 2303  discard_pkt:
2304 2304          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2305 2305          ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2306 2306          ip_drop_input("ipIfStatsInDiscards", mp, ill);
2307 2307          freemsg(mp);
2308 2308          return;
2309 2309  
2310 2310  truncated:
2311 2311          /* We pulled up everthing already. Must be truncated */
2312 2312          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2313 2313          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2314 2314          freemsg(mp);
2315 2315  }
2316 2316  
2317 2317  /*
2318 2318   * Common IP options parser.
2319 2319   *
2320 2320   * Setup routine: fill in *optp with options-parsing state, then
2321 2321   * tail-call ipoptp_next to return the first option.
2322 2322   */
2323 2323  uint8_t
2324 2324  ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2325 2325  {
2326 2326          uint32_t totallen; /* total length of all options */
2327 2327  
2328 2328          totallen = ipha->ipha_version_and_hdr_length -
2329 2329              (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2330 2330          totallen <<= 2;
2331 2331          optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2332 2332          optp->ipoptp_end = optp->ipoptp_next + totallen;
2333 2333          optp->ipoptp_flags = 0;
2334 2334          return (ipoptp_next(optp));
2335 2335  }
2336 2336  
2337 2337  /* Like above but without an ipha_t */
2338 2338  uint8_t
2339 2339  ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2340 2340  {
2341 2341          optp->ipoptp_next = opt;
2342 2342          optp->ipoptp_end = optp->ipoptp_next + totallen;
2343 2343          optp->ipoptp_flags = 0;
2344 2344          return (ipoptp_next(optp));
2345 2345  }
2346 2346  
2347 2347  /*
2348 2348   * Common IP options parser: extract next option.
2349 2349   */
2350 2350  uint8_t
2351 2351  ipoptp_next(ipoptp_t *optp)
2352 2352  {
2353 2353          uint8_t *end = optp->ipoptp_end;
2354 2354          uint8_t *cur = optp->ipoptp_next;
2355 2355          uint8_t opt, len, pointer;
2356 2356  
2357 2357          /*
2358 2358           * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2359 2359           * has been corrupted.
2360 2360           */
2361 2361          ASSERT(cur <= end);
2362 2362  
2363 2363          if (cur == end)
2364 2364                  return (IPOPT_EOL);
2365 2365  
2366 2366          opt = cur[IPOPT_OPTVAL];
2367 2367  
2368 2368          /*
2369 2369           * Skip any NOP options.
2370 2370           */
2371 2371          while (opt == IPOPT_NOP) {
2372 2372                  cur++;
2373 2373                  if (cur == end)
2374 2374                          return (IPOPT_EOL);
2375 2375                  opt = cur[IPOPT_OPTVAL];
2376 2376          }
2377 2377  
2378 2378          if (opt == IPOPT_EOL)
2379 2379                  return (IPOPT_EOL);
2380 2380  
2381 2381          /*
2382 2382           * Option requiring a length.
2383 2383           */
2384 2384          if ((cur + 1) >= end) {
2385 2385                  optp->ipoptp_flags |= IPOPTP_ERROR;
2386 2386                  return (IPOPT_EOL);
2387 2387          }
2388 2388          len = cur[IPOPT_OLEN];
2389 2389          if (len < 2) {
2390 2390                  optp->ipoptp_flags |= IPOPTP_ERROR;
2391 2391                  return (IPOPT_EOL);
2392 2392          }
2393 2393          optp->ipoptp_cur = cur;
2394 2394          optp->ipoptp_len = len;
2395 2395          optp->ipoptp_next = cur + len;
2396 2396          if (cur + len > end) {
2397 2397                  optp->ipoptp_flags |= IPOPTP_ERROR;
2398 2398                  return (IPOPT_EOL);
2399 2399          }
2400 2400  
2401 2401          /*
2402 2402           * For the options which require a pointer field, make sure
2403 2403           * its there, and make sure it points to either something
2404 2404           * inside this option, or the end of the option.
2405 2405           */
2406 2406          switch (opt) {
2407 2407          case IPOPT_RR:
2408 2408          case IPOPT_TS:
2409 2409          case IPOPT_LSRR:
2410 2410          case IPOPT_SSRR:
2411 2411                  if (len <= IPOPT_OFFSET) {
2412 2412                          optp->ipoptp_flags |= IPOPTP_ERROR;
2413 2413                          return (opt);
2414 2414                  }
2415 2415                  pointer = cur[IPOPT_OFFSET];
2416 2416                  if (pointer - 1 > len) {
2417 2417                          optp->ipoptp_flags |= IPOPTP_ERROR;
2418 2418                          return (opt);
2419 2419                  }
2420 2420                  break;
2421 2421          }
2422 2422  
2423 2423          /*
2424 2424           * Sanity check the pointer field based on the type of the
2425 2425           * option.
2426 2426           */
2427 2427          switch (opt) {
2428 2428          case IPOPT_RR:
2429 2429          case IPOPT_SSRR:
2430 2430          case IPOPT_LSRR:
2431 2431                  if (pointer < IPOPT_MINOFF_SR)
2432 2432                          optp->ipoptp_flags |= IPOPTP_ERROR;
2433 2433                  break;
2434 2434          case IPOPT_TS:
2435 2435                  if (pointer < IPOPT_MINOFF_IT)
2436 2436                          optp->ipoptp_flags |= IPOPTP_ERROR;
2437 2437                  /*
2438 2438                   * Note that the Internet Timestamp option also
2439 2439                   * contains two four bit fields (the Overflow field,
2440 2440                   * and the Flag field), which follow the pointer
2441 2441                   * field.  We don't need to check that these fields
2442 2442                   * fall within the length of the option because this
2443 2443                   * was implicitely done above.  We've checked that the
2444 2444                   * pointer value is at least IPOPT_MINOFF_IT, and that
2445 2445                   * it falls within the option.  Since IPOPT_MINOFF_IT >
2446 2446                   * IPOPT_POS_OV_FLG, we don't need the explicit check.
2447 2447                   */
2448 2448                  ASSERT(len > IPOPT_POS_OV_FLG);
2449 2449                  break;
2450 2450          }
2451 2451  
2452 2452          return (opt);
2453 2453  }
2454 2454  
2455 2455  /*
2456 2456   * Use the outgoing IP header to create an IP_OPTIONS option the way
2457 2457   * it was passed down from the application.
2458 2458   *
2459 2459   * This is compatible with BSD in that it returns
2460 2460   * the reverse source route with the final destination
2461 2461   * as the last entry. The first 4 bytes of the option
2462 2462   * will contain the final destination.
2463 2463   */
2464 2464  int
2465 2465  ip_opt_get_user(conn_t *connp, uchar_t *buf)
2466 2466  {
2467 2467          ipoptp_t        opts;
2468 2468          uchar_t         *opt;
2469 2469          uint8_t         optval;
2470 2470          uint8_t         optlen;
2471 2471          uint32_t        len = 0;
2472 2472          uchar_t         *buf1 = buf;
2473 2473          uint32_t        totallen;
2474 2474          ipaddr_t        dst;
2475 2475          ip_pkt_t        *ipp = &connp->conn_xmit_ipp;
2476 2476  
2477 2477          if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2478 2478                  return (0);
2479 2479  
2480 2480          totallen = ipp->ipp_ipv4_options_len;
2481 2481          if (totallen & 0x3)
2482 2482                  return (0);
2483 2483  
2484 2484          buf += IP_ADDR_LEN;     /* Leave room for final destination */
2485 2485          len += IP_ADDR_LEN;
2486 2486          bzero(buf1, IP_ADDR_LEN);
2487 2487  
2488 2488          dst = connp->conn_faddr_v4;
2489 2489  
2490 2490          for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2491 2491              optval != IPOPT_EOL;
2492 2492              optval = ipoptp_next(&opts)) {
2493 2493                  int     off;
2494 2494  
2495 2495                  opt = opts.ipoptp_cur;
2496 2496                  if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2497 2497                          break;
2498 2498                  }
2499 2499                  optlen = opts.ipoptp_len;
2500 2500  
2501 2501                  switch (optval) {
2502 2502                  case IPOPT_SSRR:
2503 2503                  case IPOPT_LSRR:
2504 2504  
2505 2505                          /*
2506 2506                           * Insert destination as the first entry in the source
2507 2507                           * route and move down the entries on step.
2508 2508                           * The last entry gets placed at buf1.
2509 2509                           */
2510 2510                          buf[IPOPT_OPTVAL] = optval;
2511 2511                          buf[IPOPT_OLEN] = optlen;
2512 2512                          buf[IPOPT_OFFSET] = optlen;
2513 2513  
2514 2514                          off = optlen - IP_ADDR_LEN;
2515 2515                          if (off < 0) {
2516 2516                                  /* No entries in source route */
2517 2517                                  break;
2518 2518                          }
2519 2519                          /* Last entry in source route if not already set */
2520 2520                          if (dst == INADDR_ANY)
2521 2521                                  bcopy(opt + off, buf1, IP_ADDR_LEN);
2522 2522                          off -= IP_ADDR_LEN;
2523 2523  
2524 2524                          while (off > 0) {
2525 2525                                  bcopy(opt + off,
2526 2526                                      buf + off + IP_ADDR_LEN,
2527 2527                                      IP_ADDR_LEN);
2528 2528                                  off -= IP_ADDR_LEN;
2529 2529                          }
2530 2530                          /* ipha_dst into first slot */
2531 2531                          bcopy(&dst, buf + off + IP_ADDR_LEN,
2532 2532                              IP_ADDR_LEN);
2533 2533                          buf += optlen;
2534 2534                          len += optlen;
2535 2535                          break;
2536 2536  
2537 2537                  default:
2538 2538                          bcopy(opt, buf, optlen);
2539 2539                          buf += optlen;
2540 2540                          len += optlen;
2541 2541                          break;
2542 2542                  }
2543 2543          }
2544 2544  done:
2545 2545          /* Pad the resulting options */
2546 2546          while (len & 0x3) {
2547 2547                  *buf++ = IPOPT_EOL;
2548 2548                  len++;
2549 2549          }
2550 2550          return (len);
2551 2551  }
2552 2552  
2553 2553  /*
2554 2554   * Update any record route or timestamp options to include this host.
2555 2555   * Reverse any source route option.
2556 2556   * This routine assumes that the options are well formed i.e. that they
2557 2557   * have already been checked.
2558 2558   */
2559 2559  static void
2560 2560  icmp_options_update(ipha_t *ipha)
2561 2561  {
2562 2562          ipoptp_t        opts;
2563 2563          uchar_t         *opt;
2564 2564          uint8_t         optval;
2565 2565          ipaddr_t        src;            /* Our local address */
2566 2566          ipaddr_t        dst;
2567 2567  
2568 2568          ip2dbg(("icmp_options_update\n"));
2569 2569          src = ipha->ipha_src;
2570 2570          dst = ipha->ipha_dst;
2571 2571  
2572 2572          for (optval = ipoptp_first(&opts, ipha);
2573 2573              optval != IPOPT_EOL;
2574 2574              optval = ipoptp_next(&opts)) {
2575 2575                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2576 2576                  opt = opts.ipoptp_cur;
2577 2577                  ip2dbg(("icmp_options_update: opt %d, len %d\n",
2578 2578                      optval, opts.ipoptp_len));
2579 2579                  switch (optval) {
2580 2580                          int off1, off2;
2581 2581                  case IPOPT_SSRR:
2582 2582                  case IPOPT_LSRR:
2583 2583                          /*
2584 2584                           * Reverse the source route.  The first entry
2585 2585                           * should be the next to last one in the current
2586 2586                           * source route (the last entry is our address).
2587 2587                           * The last entry should be the final destination.
2588 2588                           */
2589 2589                          off1 = IPOPT_MINOFF_SR - 1;
2590 2590                          off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2591 2591                          if (off2 < 0) {
2592 2592                                  /* No entries in source route */
2593 2593                                  ip1dbg((
2594 2594                                      "icmp_options_update: bad src route\n"));
2595 2595                                  break;
2596 2596                          }
2597 2597                          bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2598 2598                          bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2599 2599                          bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2600 2600                          off2 -= IP_ADDR_LEN;
2601 2601  
2602 2602                          while (off1 < off2) {
2603 2603                                  bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2604 2604                                  bcopy((char *)opt + off2, (char *)opt + off1,
2605 2605                                      IP_ADDR_LEN);
2606 2606                                  bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2607 2607                                  off1 += IP_ADDR_LEN;
2608 2608                                  off2 -= IP_ADDR_LEN;
2609 2609                          }
2610 2610                          opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2611 2611                          break;
2612 2612                  }
2613 2613          }
2614 2614  }
2615 2615  
2616 2616  /*
2617 2617   * Process received ICMP Redirect messages.
2618 2618   * Assumes the caller has verified that the headers are in the pulled up mblk.
2619 2619   * Consumes mp.
2620 2620   */
2621 2621  static void
2622 2622  icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2623 2623  {
2624 2624          ire_t           *ire, *nire;
2625 2625          ire_t           *prev_ire;
2626 2626          ipaddr_t        src, dst, gateway;
2627 2627          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
2628 2628          ipha_t          *inner_ipha;    /* Inner IP header */
2629 2629  
2630 2630          /* Caller already pulled up everything. */
2631 2631          inner_ipha = (ipha_t *)&icmph[1];
2632 2632          src = ipha->ipha_src;
2633 2633          dst = inner_ipha->ipha_dst;
2634 2634          gateway = icmph->icmph_rd_gateway;
2635 2635          /* Make sure the new gateway is reachable somehow. */
2636 2636          ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2637 2637              ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
2638 2638          /*
2639 2639           * Make sure we had a route for the dest in question and that
2640 2640           * that route was pointing to the old gateway (the source of the
2641 2641           * redirect packet.)
2642 2642           * We do longest match and then compare ire_gateway_addr below.
2643 2643           */
2644 2644          prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2645 2645              NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
2646 2646          /*
2647 2647           * Check that
2648 2648           *      the redirect was not from ourselves
2649 2649           *      the new gateway and the old gateway are directly reachable
2650 2650           */
2651 2651          if (prev_ire == NULL || ire == NULL ||
2652 2652              (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2653 2653              (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2654 2654              !(ire->ire_type & IRE_IF_ALL) ||
2655 2655              prev_ire->ire_gateway_addr != src) {
2656 2656                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2657 2657                  ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2658 2658                  freemsg(mp);
2659 2659                  if (ire != NULL)
2660 2660                          ire_refrele(ire);
2661 2661                  if (prev_ire != NULL)
2662 2662                          ire_refrele(prev_ire);
2663 2663                  return;
2664 2664          }
2665 2665  
2666 2666          ire_refrele(prev_ire);
2667 2667          ire_refrele(ire);
2668 2668  
2669 2669          /*
2670 2670           * TODO: more precise handling for cases 0, 2, 3, the latter two
2671 2671           * require TOS routing
2672 2672           */
2673 2673          switch (icmph->icmph_code) {
2674 2674          case 0:
2675 2675          case 1:
2676 2676                  /* TODO: TOS specificity for cases 2 and 3 */
2677 2677          case 2:
2678 2678          case 3:
2679 2679                  break;
2680 2680          default:
2681 2681                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2682 2682                  ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2683 2683                  freemsg(mp);
2684 2684                  return;
2685 2685          }
2686 2686          /*
2687 2687           * Create a Route Association.  This will allow us to remember that
2688 2688           * someone we believe told us to use the particular gateway.
2689 2689           */
2690 2690          ire = ire_create(
2691 2691              (uchar_t *)&dst,                    /* dest addr */
2692 2692              (uchar_t *)&ip_g_all_ones,          /* mask */
2693 2693              (uchar_t *)&gateway,                /* gateway addr */
2694 2694              IRE_HOST,
2695 2695              NULL,                               /* ill */
2696 2696              ALL_ZONES,
2697 2697              (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2698 2698              NULL,                               /* tsol_gc_t */
2699 2699              ipst);
2700 2700  
2701 2701          if (ire == NULL) {
2702 2702                  freemsg(mp);
2703 2703                  return;
2704 2704          }
2705 2705          nire = ire_add(ire);
2706 2706          /* Check if it was a duplicate entry */
2707 2707          if (nire != NULL && nire != ire) {
2708 2708                  ASSERT(nire->ire_identical_ref > 1);
2709 2709                  ire_delete(nire);
2710 2710                  ire_refrele(nire);
2711 2711                  nire = NULL;
2712 2712          }
2713 2713          ire = nire;
2714 2714          if (ire != NULL) {
2715 2715                  ire_refrele(ire);               /* Held in ire_add */
2716 2716  
2717 2717                  /* tell routing sockets that we received a redirect */
2718 2718                  ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2719 2719                      (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2720 2720                      (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2721 2721          }
2722 2722  
2723 2723          /*
2724 2724           * Delete any existing IRE_HOST type redirect ires for this destination.
2725 2725           * This together with the added IRE has the effect of
2726 2726           * modifying an existing redirect.
2727 2727           */
2728 2728          prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2729 2729              ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2730 2730          if (prev_ire != NULL) {
2731 2731                  if (prev_ire ->ire_flags & RTF_DYNAMIC)
2732 2732                          ire_delete(prev_ire);
2733 2733                  ire_refrele(prev_ire);
2734 2734          }
2735 2735  
2736 2736          freemsg(mp);
2737 2737  }
2738 2738  
2739 2739  /*
2740 2740   * Generate an ICMP parameter problem message.
2741 2741   * When called from ip_output side a minimal ip_recv_attr_t needs to be
2742 2742   * constructed by the caller.
2743 2743   */
2744 2744  static void
2745 2745  icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2746 2746  {
2747 2747          icmph_t icmph;
2748 2748          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
2749 2749  
2750 2750          mp = icmp_pkt_err_ok(mp, ira);
2751 2751          if (mp == NULL)
2752 2752                  return;
2753 2753  
2754 2754          bzero(&icmph, sizeof (icmph_t));
2755 2755          icmph.icmph_type = ICMP_PARAM_PROBLEM;
2756 2756          icmph.icmph_pp_ptr = ptr;
2757 2757          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2758 2758          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2759 2759  }
2760 2760  
2761 2761  /*
2762 2762   * Build and ship an IPv4 ICMP message using the packet data in mp, and
2763 2763   * the ICMP header pointed to by "stuff".  (May be called as writer.)
2764 2764   * Note: assumes that icmp_pkt_err_ok has been called to verify that
2765 2765   * an icmp error packet can be sent.
2766 2766   * Assigns an appropriate source address to the packet. If ipha_dst is
2767 2767   * one of our addresses use it for source. Otherwise let ip_output_simple
2768 2768   * pick the source address.
2769 2769   */
2770 2770  static void
2771 2771  icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2772 2772  {
2773 2773          ipaddr_t dst;
2774 2774          icmph_t *icmph;
2775 2775          ipha_t  *ipha;
2776 2776          uint_t  len_needed;
2777 2777          size_t  msg_len;
2778 2778          mblk_t  *mp1;
2779 2779          ipaddr_t src;
2780 2780          ire_t   *ire;
2781 2781          ip_xmit_attr_t ixas;
2782 2782          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2783 2783  
2784 2784          ipha = (ipha_t *)mp->b_rptr;
2785 2785  
2786 2786          bzero(&ixas, sizeof (ixas));
2787 2787          ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2788 2788          ixas.ixa_zoneid = ira->ira_zoneid;
2789 2789          ixas.ixa_ifindex = 0;
2790 2790          ixas.ixa_ipst = ipst;
2791 2791          ixas.ixa_cred = kcred;
2792 2792          ixas.ixa_cpid = NOPID;
2793 2793          ixas.ixa_tsl = ira->ira_tsl;    /* Behave as a multi-level responder */
2794 2794          ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2795 2795  
2796 2796          if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2797 2797                  /*
2798 2798                   * Apply IPsec based on how IPsec was applied to
2799 2799                   * the packet that had the error.
2800 2800                   *
2801 2801                   * If it was an outbound packet that caused the ICMP
2802 2802                   * error, then the caller will have setup the IRA
2803 2803                   * appropriately.
2804 2804                   */
2805 2805                  if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2806 2806                          BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2807 2807                          /* Note: mp already consumed and ip_drop_packet done */
2808 2808                          return;
2809 2809                  }
2810 2810          } else {
2811 2811                  /*
2812 2812                   * This is in clear. The icmp message we are building
2813 2813                   * here should go out in clear, independent of our policy.
2814 2814                   */
2815 2815                  ixas.ixa_flags |= IXAF_NO_IPSEC;
2816 2816          }
2817 2817  
2818 2818          /* Remember our eventual destination */
2819 2819          dst = ipha->ipha_src;
2820 2820  
2821 2821          /*
2822 2822           * If the packet was for one of our unicast addresses, make
2823 2823           * sure we respond with that as the source. Otherwise
2824 2824           * have ip_output_simple pick the source address.
2825 2825           */
2826 2826          ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2827 2827              (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL,
2828 2828              MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2829 2829          if (ire != NULL) {
2830 2830                  ire_refrele(ire);
2831 2831                  src = ipha->ipha_dst;
2832 2832          } else {
2833 2833                  src = INADDR_ANY;
2834 2834                  ixas.ixa_flags |= IXAF_SET_SOURCE;
2835 2835          }
2836 2836  
2837 2837          /*
2838 2838           * Check if we can send back more then 8 bytes in addition to
2839 2839           * the IP header.  We try to send 64 bytes of data and the internal
2840 2840           * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2841 2841           */
2842 2842          len_needed = IPH_HDR_LENGTH(ipha);
2843 2843          if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2844 2844              ipha->ipha_protocol == IPPROTO_IPV6) {
2845 2845                  if (!pullupmsg(mp, -1)) {
2846 2846                          BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2847 2847                          ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2848 2848                          freemsg(mp);
2849 2849                          return;
2850 2850                  }
2851 2851                  ipha = (ipha_t *)mp->b_rptr;
2852 2852  
2853 2853                  if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2854 2854                          len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2855 2855                              len_needed));
2856 2856                  } else {
2857 2857                          ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2858 2858  
2859 2859                          ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2860 2860                          len_needed += ip_hdr_length_v6(mp, ip6h);
2861 2861                  }
2862 2862          }
2863 2863          len_needed += ipst->ips_ip_icmp_return;
2864 2864          msg_len = msgdsize(mp);
2865 2865          if (msg_len > len_needed) {
2866 2866                  (void) adjmsg(mp, len_needed - msg_len);
2867 2867                  msg_len = len_needed;
2868 2868          }
2869 2869          mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2870 2870          if (mp1 == NULL) {
2871 2871                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2872 2872                  freemsg(mp);
2873 2873                  return;
2874 2874          }
2875 2875          mp1->b_cont = mp;
2876 2876          mp = mp1;
2877 2877  
2878 2878          /*
2879 2879           * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2880 2880           * node generates be accepted in peace by all on-host destinations.
2881 2881           * If we do NOT assume that all on-host destinations trust
2882 2882           * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2883 2883           * (Look for IXAF_TRUSTED_ICMP).
2884 2884           */
2885 2885          ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2886 2886  
2887 2887          ipha = (ipha_t *)mp->b_rptr;
2888 2888          mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2889 2889          *ipha = icmp_ipha;
2890 2890          ipha->ipha_src = src;
2891 2891          ipha->ipha_dst = dst;
2892 2892          ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2893 2893          msg_len += sizeof (icmp_ipha) + len;
2894 2894          if (msg_len > IP_MAXPACKET) {
2895 2895                  (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2896 2896                  msg_len = IP_MAXPACKET;
2897 2897          }
2898 2898          ipha->ipha_length = htons((uint16_t)msg_len);
2899 2899          icmph = (icmph_t *)&ipha[1];
2900 2900          bcopy(stuff, icmph, len);
2901 2901          icmph->icmph_checksum = 0;
2902 2902          icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2903 2903          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2904 2904  
2905 2905          (void) ip_output_simple(mp, &ixas);
2906 2906          ixa_cleanup(&ixas);
2907 2907  }
2908 2908  
2909 2909  /*
2910 2910   * Determine if an ICMP error packet can be sent given the rate limit.
2911 2911   * The limit consists of an average frequency (icmp_pkt_err_interval measured
2912 2912   * in milliseconds) and a burst size. Burst size number of packets can
2913 2913   * be sent arbitrarely closely spaced.
2914 2914   * The state is tracked using two variables to implement an approximate
2915 2915   * token bucket filter:
2916 2916   *      icmp_pkt_err_last - lbolt value when the last burst started
2917 2917   *      icmp_pkt_err_sent - number of packets sent in current burst
2918 2918   */
2919 2919  boolean_t
2920 2920  icmp_err_rate_limit(ip_stack_t *ipst)
2921 2921  {
2922 2922          clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2923 2923          uint_t refilled; /* Number of packets refilled in tbf since last */
2924 2924          /* Guard against changes by loading into local variable */
2925 2925          uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2926 2926  
2927 2927          if (err_interval == 0)
2928 2928                  return (B_FALSE);
2929 2929  
2930 2930          if (ipst->ips_icmp_pkt_err_last > now) {
2931 2931                  /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2932 2932                  ipst->ips_icmp_pkt_err_last = 0;
2933 2933                  ipst->ips_icmp_pkt_err_sent = 0;
2934 2934          }
2935 2935          /*
2936 2936           * If we are in a burst update the token bucket filter.
2937 2937           * Update the "last" time to be close to "now" but make sure
2938 2938           * we don't loose precision.
2939 2939           */
2940 2940          if (ipst->ips_icmp_pkt_err_sent != 0) {
2941 2941                  refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2942 2942                  if (refilled > ipst->ips_icmp_pkt_err_sent) {
2943 2943                          ipst->ips_icmp_pkt_err_sent = 0;
2944 2944                  } else {
2945 2945                          ipst->ips_icmp_pkt_err_sent -= refilled;
2946 2946                          ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2947 2947                  }
2948 2948          }
2949 2949          if (ipst->ips_icmp_pkt_err_sent == 0) {
2950 2950                  /* Start of new burst */
2951 2951                  ipst->ips_icmp_pkt_err_last = now;
2952 2952          }
2953 2953          if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2954 2954                  ipst->ips_icmp_pkt_err_sent++;
2955 2955                  ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2956 2956                      ipst->ips_icmp_pkt_err_sent));
2957 2957                  return (B_FALSE);
2958 2958          }
2959 2959          ip1dbg(("icmp_err_rate_limit: dropped\n"));
2960 2960          return (B_TRUE);
2961 2961  }
2962 2962  
2963 2963  /*
2964 2964   * Check if it is ok to send an IPv4 ICMP error packet in
2965 2965   * response to the IPv4 packet in mp.
2966 2966   * Free the message and return null if no
2967 2967   * ICMP error packet should be sent.
2968 2968   */
2969 2969  static mblk_t *
2970 2970  icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2971 2971  {
2972 2972          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
2973 2973          icmph_t *icmph;
2974 2974          ipha_t  *ipha;
2975 2975          uint_t  len_needed;
2976 2976  
2977 2977          if (!mp)
2978 2978                  return (NULL);
2979 2979          ipha = (ipha_t *)mp->b_rptr;
2980 2980          if (ip_csum_hdr(ipha)) {
2981 2981                  BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2982 2982                  ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2983 2983                  freemsg(mp);
2984 2984                  return (NULL);
2985 2985          }
2986 2986          if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2987 2987              ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2988 2988              CLASSD(ipha->ipha_dst) ||
2989 2989              CLASSD(ipha->ipha_src) ||
2990 2990              (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2991 2991                  /* Note: only errors to the fragment with offset 0 */
2992 2992                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2993 2993                  freemsg(mp);
2994 2994                  return (NULL);
2995 2995          }
2996 2996          if (ipha->ipha_protocol == IPPROTO_ICMP) {
2997 2997                  /*
2998 2998                   * Check the ICMP type.  RFC 1122 sez:  don't send ICMP
2999 2999                   * errors in response to any ICMP errors.
3000 3000                   */
3001 3001                  len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3002 3002                  if (mp->b_wptr - mp->b_rptr < len_needed) {
3003 3003                          if (!pullupmsg(mp, len_needed)) {
3004 3004                                  BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3005 3005                                  freemsg(mp);
3006 3006                                  return (NULL);
3007 3007                          }
3008 3008                          ipha = (ipha_t *)mp->b_rptr;
3009 3009                  }
3010 3010                  icmph = (icmph_t *)
3011 3011                      (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3012 3012                  switch (icmph->icmph_type) {
3013 3013                  case ICMP_DEST_UNREACHABLE:
3014 3014                  case ICMP_SOURCE_QUENCH:
3015 3015                  case ICMP_TIME_EXCEEDED:
3016 3016                  case ICMP_PARAM_PROBLEM:
3017 3017                  case ICMP_REDIRECT:
3018 3018                          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3019 3019                          freemsg(mp);
3020 3020                          return (NULL);
3021 3021                  default:
3022 3022                          break;
3023 3023                  }
3024 3024          }
3025 3025          /*
3026 3026           * If this is a labeled system, then check to see if we're allowed to
3027 3027           * send a response to this particular sender.  If not, then just drop.
3028 3028           */
3029 3029          if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
3030 3030                  ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3031 3031                  BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3032 3032                  freemsg(mp);
3033 3033                  return (NULL);
3034 3034          }
3035 3035          if (icmp_err_rate_limit(ipst)) {
3036 3036                  /*
3037 3037                   * Only send ICMP error packets every so often.
3038 3038                   * This should be done on a per port/source basis,
3039 3039                   * but for now this will suffice.
3040 3040                   */
3041 3041                  freemsg(mp);
3042 3042                  return (NULL);
3043 3043          }
3044 3044          return (mp);
3045 3045  }
3046 3046  
3047 3047  /*
3048 3048   * Called when a packet was sent out the same link that it arrived on.
3049 3049   * Check if it is ok to send a redirect and then send it.
3050 3050   */
3051 3051  void
3052 3052  ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
3053 3053      ip_recv_attr_t *ira)
3054 3054  {
3055 3055          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
3056 3056          ipaddr_t        src, nhop;
3057 3057          mblk_t          *mp1;
3058 3058          ire_t           *nhop_ire;
3059 3059  
3060 3060          /*
3061 3061           * Check the source address to see if it originated
3062 3062           * on the same logical subnet it is going back out on.
3063 3063           * If so, we should be able to send it a redirect.
3064 3064           * Avoid sending a redirect if the destination
3065 3065           * is directly connected (i.e., we matched an IRE_ONLINK),
3066 3066           * or if the packet was source routed out this interface.
3067 3067           *
3068 3068           * We avoid sending a redirect if the
3069 3069           * destination is directly connected
3070 3070           * because it is possible that multiple
3071 3071           * IP subnets may have been configured on
3072 3072           * the link, and the source may not
3073 3073           * be on the same subnet as ip destination,
3074 3074           * even though they are on the same
3075 3075           * physical link.
3076 3076           */
3077 3077          if ((ire->ire_type & IRE_ONLINK) ||
3078 3078              ip_source_routed(ipha, ipst))
3079 3079                  return;
3080 3080  
3081 3081          nhop_ire = ire_nexthop(ire);
3082 3082          if (nhop_ire == NULL)
3083 3083                  return;
3084 3084  
3085 3085          nhop = nhop_ire->ire_addr;
3086 3086  
3087 3087          if (nhop_ire->ire_type & IRE_IF_CLONE) {
3088 3088                  ire_t   *ire2;
3089 3089  
3090 3090                  /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3091 3091                  mutex_enter(&nhop_ire->ire_lock);
3092 3092                  ire2 = nhop_ire->ire_dep_parent;
3093 3093                  if (ire2 != NULL)
3094 3094                          ire_refhold(ire2);
3095 3095                  mutex_exit(&nhop_ire->ire_lock);
3096 3096                  ire_refrele(nhop_ire);
3097 3097                  nhop_ire = ire2;
3098 3098          }
3099 3099          if (nhop_ire == NULL)
3100 3100                  return;
3101 3101  
3102 3102          ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3103 3103  
3104 3104          src = ipha->ipha_src;
3105 3105  
3106 3106          /*
3107 3107           * We look at the interface ire for the nexthop,
3108 3108           * to see if ipha_src is in the same subnet
3109 3109           * as the nexthop.
3110 3110           */
3111 3111          if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3112 3112                  /*
3113 3113                   * The source is directly connected.
3114 3114                   */
3115 3115                  mp1 = copymsg(mp);
3116 3116                  if (mp1 != NULL) {
3117 3117                          icmp_send_redirect(mp1, nhop, ira);
3118 3118                  }
3119 3119          }
3120 3120          ire_refrele(nhop_ire);
3121 3121  }
3122 3122  
3123 3123  /*
3124 3124   * Generate an ICMP redirect message.
3125 3125   */
3126 3126  static void
3127 3127  icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3128 3128  {
3129 3129          icmph_t icmph;
3130 3130          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3131 3131  
3132 3132          mp = icmp_pkt_err_ok(mp, ira);
3133 3133          if (mp == NULL)
3134 3134                  return;
3135 3135  
3136 3136          bzero(&icmph, sizeof (icmph_t));
3137 3137          icmph.icmph_type = ICMP_REDIRECT;
3138 3138          icmph.icmph_code = 1;
3139 3139          icmph.icmph_rd_gateway = gateway;
3140 3140          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3141 3141          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3142 3142  }
3143 3143  
3144 3144  /*
3145 3145   * Generate an ICMP time exceeded message.
3146 3146   */
3147 3147  void
3148 3148  icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3149 3149  {
3150 3150          icmph_t icmph;
3151 3151          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3152 3152  
3153 3153          mp = icmp_pkt_err_ok(mp, ira);
3154 3154          if (mp == NULL)
3155 3155                  return;
3156 3156  
3157 3157          bzero(&icmph, sizeof (icmph_t));
3158 3158          icmph.icmph_type = ICMP_TIME_EXCEEDED;
3159 3159          icmph.icmph_code = code;
3160 3160          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3161 3161          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3162 3162  }
3163 3163  
3164 3164  /*
3165 3165   * Generate an ICMP unreachable message.
3166 3166   * When called from ip_output side a minimal ip_recv_attr_t needs to be
3167 3167   * constructed by the caller.
3168 3168   */
3169 3169  void
3170 3170  icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3171 3171  {
3172 3172          icmph_t icmph;
3173 3173          ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3174 3174  
3175 3175          mp = icmp_pkt_err_ok(mp, ira);
3176 3176          if (mp == NULL)
3177 3177                  return;
3178 3178  
3179 3179          bzero(&icmph, sizeof (icmph_t));
3180 3180          icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3181 3181          icmph.icmph_code = code;
3182 3182          BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3183 3183          icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3184 3184  }
3185 3185  
3186 3186  /*
3187 3187   * Latch in the IPsec state for a stream based the policy in the listener
3188 3188   * and the actions in the ip_recv_attr_t.
3189 3189   * Called directly from TCP and SCTP.
3190 3190   */
3191 3191  boolean_t
3192 3192  ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3193 3193  {
3194 3194          ASSERT(lconnp->conn_policy != NULL);
3195 3195          ASSERT(connp->conn_policy == NULL);
3196 3196  
3197 3197          IPPH_REFHOLD(lconnp->conn_policy);
3198 3198          connp->conn_policy = lconnp->conn_policy;
3199 3199  
3200 3200          if (ira->ira_ipsec_action != NULL) {
3201 3201                  if (connp->conn_latch == NULL) {
3202 3202                          connp->conn_latch = iplatch_create();
3203 3203                          if (connp->conn_latch == NULL)
3204 3204                                  return (B_FALSE);
3205 3205                  }
3206 3206                  ipsec_latch_inbound(connp, ira);
3207 3207          }
3208 3208          return (B_TRUE);
3209 3209  }
3210 3210  
3211 3211  /*
3212 3212   * Verify whether or not the IP address is a valid local address.
3213 3213   * Could be a unicast, including one for a down interface.
3214 3214   * If allow_mcbc then a multicast or broadcast address is also
3215 3215   * acceptable.
3216 3216   *
3217 3217   * In the case of a broadcast/multicast address, however, the
3218 3218   * upper protocol is expected to reset the src address
3219 3219   * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3220 3220   * no packets are emitted with broadcast/multicast address as
3221 3221   * source address (that violates hosts requirements RFC 1122)
3222 3222   * The addresses valid for bind are:
3223 3223   *      (1) - INADDR_ANY (0)
3224 3224   *      (2) - IP address of an UP interface
3225 3225   *      (3) - IP address of a DOWN interface
3226 3226   *      (4) - valid local IP broadcast addresses. In this case
3227 3227   *      the conn will only receive packets destined to
3228 3228   *      the specified broadcast address.
3229 3229   *      (5) - a multicast address. In this case
3230 3230   *      the conn will only receive packets destined to
3231 3231   *      the specified multicast address. Note: the
3232 3232   *      application still has to issue an
3233 3233   *      IP_ADD_MEMBERSHIP socket option.
3234 3234   *
3235 3235   * In all the above cases, the bound address must be valid in the current zone.
3236 3236   * When the address is loopback, multicast or broadcast, there might be many
3237 3237   * matching IREs so bind has to look up based on the zone.
3238 3238   */
3239 3239  ip_laddr_t
3240 3240  ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3241 3241      ip_stack_t *ipst, boolean_t allow_mcbc)
3242 3242  {
3243 3243          ire_t *src_ire;
3244 3244  
3245 3245          ASSERT(src_addr != INADDR_ANY);
3246 3246  
3247 3247          src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3248 3248              NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3249 3249  
3250 3250          /*
3251 3251           * If an address other than in6addr_any is requested,
3252 3252           * we verify that it is a valid address for bind
3253 3253           * Note: Following code is in if-else-if form for
3254 3254           * readability compared to a condition check.
3255 3255           */
3256 3256          if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3257 3257                  /*
3258 3258                   * (2) Bind to address of local UP interface
3259 3259                   */
3260 3260                  ire_refrele(src_ire);
3261 3261                  return (IPVL_UNICAST_UP);
3262 3262          } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3263 3263                  /*
3264 3264                   * (4) Bind to broadcast address
3265 3265                   */
3266 3266                  ire_refrele(src_ire);
3267 3267                  if (allow_mcbc)
3268 3268                          return (IPVL_BCAST);
3269 3269                  else
3270 3270                          return (IPVL_BAD);
3271 3271          } else if (CLASSD(src_addr)) {
3272 3272                  /* (5) bind to multicast address. */
3273 3273                  if (src_ire != NULL)
3274 3274                          ire_refrele(src_ire);
3275 3275  
3276 3276                  if (allow_mcbc)
3277 3277                          return (IPVL_MCAST);
3278 3278                  else
3279 3279                          return (IPVL_BAD);
3280 3280          } else {
3281 3281                  ipif_t *ipif;
3282 3282  
3283 3283                  /*
3284 3284                   * (3) Bind to address of local DOWN interface?
3285 3285                   * (ipif_lookup_addr() looks up all interfaces
3286 3286                   * but we do not get here for UP interfaces
3287 3287                   * - case (2) above)
3288 3288                   */
3289 3289                  if (src_ire != NULL)
3290 3290                          ire_refrele(src_ire);
3291 3291  
3292 3292                  ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3293 3293                  if (ipif == NULL)
3294 3294                          return (IPVL_BAD);
3295 3295  
3296 3296                  /* Not a useful source? */
3297 3297                  if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3298 3298                          ipif_refrele(ipif);
3299 3299                          return (IPVL_BAD);
3300 3300                  }
3301 3301                  ipif_refrele(ipif);
3302 3302                  return (IPVL_UNICAST_DOWN);
3303 3303          }
3304 3304  }
3305 3305  
3306 3306  /*
3307 3307   * Insert in the bind fanout for IPv4 and IPv6.
3308 3308   * The caller should already have used ip_laddr_verify_v*() before calling
3309 3309   * this.
3310 3310   */
3311 3311  int
3312 3312  ip_laddr_fanout_insert(conn_t *connp)
3313 3313  {
3314 3314          int             error;
3315 3315  
3316 3316          /*
3317 3317           * Allow setting new policies. For example, disconnects result
3318 3318           * in us being called. As we would have set conn_policy_cached
3319 3319           * to B_TRUE before, we should set it to B_FALSE, so that policy
3320 3320           * can change after the disconnect.
3321 3321           */
3322 3322          connp->conn_policy_cached = B_FALSE;
3323 3323  
3324 3324          error = ipcl_bind_insert(connp);
3325 3325          if (error != 0) {
3326 3326                  if (connp->conn_anon_port) {
3327 3327                          (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
3328 3328                              connp->conn_mlp_type, connp->conn_proto,
3329 3329                              ntohs(connp->conn_lport), B_FALSE);
3330 3330                  }
3331 3331                  connp->conn_mlp_type = mlptSingle;
3332 3332          }
3333 3333          return (error);
3334 3334  }
3335 3335  
3336 3336  /*
3337 3337   * Verify that both the source and destination addresses are valid. If
3338 3338   * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3339 3339   * i.e. have no route to it.  Protocols like TCP want to verify destination
3340 3340   * reachability, while tunnels do not.
3341 3341   *
3342 3342   * Determine the route, the interface, and (optionally) the source address
3343 3343   * to use to reach a given destination.
3344 3344   * Note that we allow connect to broadcast and multicast addresses when
3345 3345   * IPDF_ALLOW_MCBC is set.
3346 3346   * first_hop and dst_addr are normally the same, but if source routing
3347 3347   * they will differ; in that case the first_hop is what we'll use for the
3348 3348   * routing lookup but the dce and label checks will be done on dst_addr,
3349 3349   *
3350 3350   * If uinfo is set, then we fill in the best available information
3351 3351   * we have for the destination. This is based on (in priority order) any
3352 3352   * metrics and path MTU stored in a dce_t, route metrics, and finally the
3353 3353   * ill_mtu/ill_mc_mtu.
3354 3354   *
3355 3355   * Tsol note: If we have a source route then dst_addr != firsthop. But we
3356 3356   * always do the label check on dst_addr.
3357 3357   */
3358 3358  int
3359 3359  ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3360 3360      ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode)
3361 3361  {
3362 3362          ire_t           *ire = NULL;
3363 3363          int             error = 0;
3364 3364          ipaddr_t        setsrc;                         /* RTF_SETSRC */
3365 3365          zoneid_t        zoneid = ixa->ixa_zoneid;       /* Honors SO_ALLZONES */
3366 3366          ip_stack_t      *ipst = ixa->ixa_ipst;
3367 3367          dce_t           *dce;
3368 3368          uint_t          pmtu;
3369 3369          uint_t          generation;
3370 3370          nce_t           *nce;
3371 3371          ill_t           *ill = NULL;
3372 3372          boolean_t       multirt = B_FALSE;
3373 3373  
3374 3374          ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3375 3375  
3376 3376          /*
3377 3377           * We never send to zero; the ULPs map it to the loopback address.
3378 3378           * We can't allow it since we use zero to mean unitialized in some
3379 3379           * places.
3380 3380           */
3381 3381          ASSERT(dst_addr != INADDR_ANY);
3382 3382  
3383 3383          if (is_system_labeled()) {
3384 3384                  ts_label_t *tsl = NULL;
3385 3385  
3386 3386                  error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION,
3387 3387                      mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl);
3388 3388                  if (error != 0)
3389 3389                          return (error);
3390 3390                  if (tsl != NULL) {
3391 3391                          /* Update the label */
3392 3392                          ip_xmit_attr_replace_tsl(ixa, tsl);
3393 3393                  }
3394 3394          }
3395 3395  
3396 3396          setsrc = INADDR_ANY;
3397 3397          /*
3398 3398           * Select a route; For IPMP interfaces, we would only select
3399 3399           * a "hidden" route (i.e., going through a specific under_ill)
3400 3400           * if ixa_ifindex has been specified.
3401 3401           */
3402 3402          ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3403 3403              &generation, &setsrc, &error, &multirt);
3404 3404          ASSERT(ire != NULL);    /* IRE_NOROUTE if none found */
3405 3405          if (error != 0)
3406 3406                  goto bad_addr;
3407 3407  
3408 3408          /*
3409 3409           * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3410 3410           * If IPDF_VERIFY_DST is set, the destination must be reachable;
3411 3411           * Otherwise the destination needn't be reachable.
3412 3412           *
3413 3413           * If we match on a reject or black hole, then we've got a
3414 3414           * local failure.  May as well fail out the connect() attempt,
3415 3415           * since it's never going to succeed.
3416 3416           */
3417 3417          if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3418 3418                  /*
3419 3419                   * If we're verifying destination reachability, we always want
3420 3420                   * to complain here.
3421 3421                   *
3422 3422                   * If we're not verifying destination reachability but the
3423 3423                   * destination has a route, we still want to fail on the
3424 3424                   * temporary address and broadcast address tests.
3425 3425                   *
3426 3426                   * In both cases do we let the code continue so some reasonable
3427 3427                   * information is returned to the caller. That enables the
3428 3428                   * caller to use (and even cache) the IRE. conn_ip_ouput will
3429 3429                   * use the generation mismatch path to check for the unreachable
3430 3430                   * case thereby avoiding any specific check in the main path.
3431 3431                   */
3432 3432                  ASSERT(generation == IRE_GENERATION_VERIFY);
3433 3433                  if (flags & IPDF_VERIFY_DST) {
3434 3434                          /*
3435 3435                           * Set errno but continue to set up ixa_ire to be
3436 3436                           * the RTF_REJECT|RTF_BLACKHOLE IRE.
3437 3437                           * That allows callers to use ip_output to get an
3438 3438                           * ICMP error back.
3439 3439                           */
3440 3440                          if (!(ire->ire_type & IRE_HOST))
3441 3441                                  error = ENETUNREACH;
3442 3442                          else
3443 3443                                  error = EHOSTUNREACH;
3444 3444                  }
3445 3445          }
3446 3446  
3447 3447          if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3448 3448              !(flags & IPDF_ALLOW_MCBC)) {
3449 3449                  ire_refrele(ire);
3450 3450                  ire = ire_reject(ipst, B_FALSE);
3451 3451                  generation = IRE_GENERATION_VERIFY;
3452 3452                  error = ENETUNREACH;
3453 3453          }
3454 3454  
3455 3455          /* Cache things */
3456 3456          if (ixa->ixa_ire != NULL)
3457 3457                  ire_refrele_notr(ixa->ixa_ire);
3458 3458  #ifdef DEBUG
3459 3459          ire_refhold_notr(ire);
3460 3460          ire_refrele(ire);
3461 3461  #endif
3462 3462          ixa->ixa_ire = ire;
3463 3463          ixa->ixa_ire_generation = generation;
3464 3464  
3465 3465          /*
3466 3466           * Ensure that ixa_dce is always set any time that ixa_ire is set,
3467 3467           * since some callers will send a packet to conn_ip_output() even if
3468 3468           * there's an error.
3469 3469           */
3470 3470          if (flags & IPDF_UNIQUE_DCE) {
3471 3471                  /* Fallback to the default dce if allocation fails */
3472 3472                  dce = dce_lookup_and_add_v4(dst_addr, ipst);
3473 3473                  if (dce != NULL)
3474 3474                          generation = dce->dce_generation;
3475 3475                  else
3476 3476                          dce = dce_lookup_v4(dst_addr, ipst, &generation);
3477 3477          } else {
3478 3478                  dce = dce_lookup_v4(dst_addr, ipst, &generation);
3479 3479          }
3480 3480          ASSERT(dce != NULL);
3481 3481          if (ixa->ixa_dce != NULL)
3482 3482                  dce_refrele_notr(ixa->ixa_dce);
3483 3483  #ifdef DEBUG
3484 3484          dce_refhold_notr(dce);
3485 3485          dce_refrele(dce);
3486 3486  #endif
3487 3487          ixa->ixa_dce = dce;
3488 3488          ixa->ixa_dce_generation = generation;
3489 3489  
3490 3490          /*
3491 3491           * For multicast with multirt we have a flag passed back from
3492 3492           * ire_lookup_multi_ill_v4 since we don't have an IRE for each
3493 3493           * possible multicast address.
3494 3494           * We also need a flag for multicast since we can't check
3495 3495           * whether RTF_MULTIRT is set in ixa_ire for multicast.
3496 3496           */
3497 3497          if (multirt) {
3498 3498                  ixa->ixa_postfragfn = ip_postfrag_multirt_v4;
3499 3499                  ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST;
3500 3500          } else {
3501 3501                  ixa->ixa_postfragfn = ire->ire_postfragfn;
3502 3502                  ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST;
3503 3503          }
3504 3504          if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3505 3505                  /* Get an nce to cache. */
3506 3506                  nce = ire_to_nce(ire, firsthop, NULL);
3507 3507                  if (nce == NULL) {
3508 3508                          /* Allocation failure? */
3509 3509                          ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3510 3510                  } else {
3511 3511                          if (ixa->ixa_nce != NULL)
3512 3512                                  nce_refrele(ixa->ixa_nce);
3513 3513                          ixa->ixa_nce = nce;
3514 3514                  }
3515 3515          }
3516 3516  
3517 3517          /*
3518 3518           * If the source address is a loopback address, the
3519 3519           * destination had best be local or multicast.
3520 3520           * If we are sending to an IRE_LOCAL using a loopback source then
3521 3521           * it had better be the same zoneid.
3522 3522           */
3523 3523          if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3524 3524                  if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3525 3525                          ire = NULL;     /* Stored in ixa_ire */
3526 3526                          error = EADDRNOTAVAIL;
3527 3527                          goto bad_addr;
3528 3528                  }
3529 3529                  if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3530 3530                          ire = NULL;     /* Stored in ixa_ire */
3531 3531                          error = EADDRNOTAVAIL;
3532 3532                          goto bad_addr;
3533 3533                  }
3534 3534          }
3535 3535          if (ire->ire_type & IRE_BROADCAST) {
3536 3536                  /*
3537 3537                   * If the ULP didn't have a specified source, then we
3538 3538                   * make sure we reselect the source when sending
3539 3539                   * broadcasts out different interfaces.
3540 3540                   */
3541 3541                  if (flags & IPDF_SELECT_SRC)
3542 3542                          ixa->ixa_flags |= IXAF_SET_SOURCE;
3543 3543                  else
3544 3544                          ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3545 3545          }
3546 3546  
3547 3547          /*
3548 3548           * Does the caller want us to pick a source address?
3549 3549           */
3550 3550          if (flags & IPDF_SELECT_SRC) {
3551 3551                  ipaddr_t        src_addr;
3552 3552  
3553 3553                  /*
3554 3554                   * We use use ire_nexthop_ill to avoid the under ipmp
3555 3555                   * interface for source address selection. Note that for ipmp
3556 3556                   * probe packets, ixa_ifindex would have been specified, and
3557 3557                   * the ip_select_route() invocation would have picked an ire
3558 3558                   * will ire_ill pointing at an under interface.
3559 3559                   */
3560 3560                  ill = ire_nexthop_ill(ire);
3561 3561  
3562 3562                  /* If unreachable we have no ill but need some source */
3563 3563                  if (ill == NULL) {
3564 3564                          src_addr = htonl(INADDR_LOOPBACK);
3565 3565                          /* Make sure we look for a better source address */
3566 3566                          generation = SRC_GENERATION_VERIFY;
3567 3567                  } else {
3568 3568                          error = ip_select_source_v4(ill, setsrc, dst_addr,
3569 3569                              ixa->ixa_multicast_ifaddr, zoneid,
3570 3570                              ipst, &src_addr, &generation, NULL);
3571 3571                          if (error != 0) {
3572 3572                                  ire = NULL;     /* Stored in ixa_ire */
3573 3573                                  goto bad_addr;
3574 3574                          }
3575 3575                  }
3576 3576  
3577 3577                  /*
3578 3578                   * We allow the source address to to down.
3579 3579                   * However, we check that we don't use the loopback address
3580 3580                   * as a source when sending out on the wire.
3581 3581                   */
3582 3582                  if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3583 3583                      !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3584 3584                      !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3585 3585                          ire = NULL;     /* Stored in ixa_ire */
3586 3586                          error = EADDRNOTAVAIL;
3587 3587                          goto bad_addr;
3588 3588                  }
3589 3589  
3590 3590                  *src_addrp = src_addr;
3591 3591                  ixa->ixa_src_generation = generation;
3592 3592          }
3593 3593  
3594 3594          /*
3595 3595           * Make sure we don't leave an unreachable ixa_nce in place
3596 3596           * since ip_select_route is used when we unplumb i.e., remove
3597 3597           * references on ixa_ire, ixa_nce, and ixa_dce.
3598 3598           */
3599 3599          nce = ixa->ixa_nce;
3600 3600          if (nce != NULL && nce->nce_is_condemned) {
3601 3601                  nce_refrele(nce);
3602 3602                  ixa->ixa_nce = NULL;
3603 3603                  ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3604 3604          }
3605 3605  
3606 3606          /*
3607 3607           * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3608 3608           * However, we can't do it for IPv4 multicast or broadcast.
3609 3609           */
3610 3610          if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3611 3611                  ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3612 3612  
3613 3613          /*
3614 3614           * Set initial value for fragmentation limit. Either conn_ip_output
3615 3615           * or ULP might updates it when there are routing changes.
3616 3616           * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3617 3617           */
3618 3618          pmtu = ip_get_pmtu(ixa);
3619 3619          ixa->ixa_fragsize = pmtu;
3620 3620          /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3621 3621          if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3622 3622                  ixa->ixa_pmtu = pmtu;
3623 3623  
3624 3624          /*
3625 3625           * Extract information useful for some transports.
3626 3626           * First we look for DCE metrics. Then we take what we have in
3627 3627           * the metrics in the route, where the offlink is used if we have
3628 3628           * one.
3629 3629           */
3630 3630          if (uinfo != NULL) {
3631 3631                  bzero(uinfo, sizeof (*uinfo));
3632 3632  
3633 3633                  if (dce->dce_flags & DCEF_UINFO)
3634 3634                          *uinfo = dce->dce_uinfo;
3635 3635  
3636 3636                  rts_merge_metrics(uinfo, &ire->ire_metrics);
3637 3637  
3638 3638                  /* Allow ire_metrics to decrease the path MTU from above */
3639 3639                  if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3640 3640                          uinfo->iulp_mtu = pmtu;
3641 3641  
3642 3642                  uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3643 3643                  uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3644 3644                  uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3645 3645          }
3646 3646  
3647 3647          if (ill != NULL)
3648 3648                  ill_refrele(ill);
3649 3649  
3650 3650          return (error);
3651 3651  
3652 3652  bad_addr:
3653 3653          if (ire != NULL)
3654 3654                  ire_refrele(ire);
3655 3655  
3656 3656          if (ill != NULL)
3657 3657                  ill_refrele(ill);
3658 3658  
3659 3659          /*
3660 3660           * Make sure we don't leave an unreachable ixa_nce in place
3661 3661           * since ip_select_route is used when we unplumb i.e., remove
3662 3662           * references on ixa_ire, ixa_nce, and ixa_dce.
3663 3663           */
3664 3664          nce = ixa->ixa_nce;
3665 3665          if (nce != NULL && nce->nce_is_condemned) {
3666 3666                  nce_refrele(nce);
3667 3667                  ixa->ixa_nce = NULL;
3668 3668                  ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3669 3669          }
3670 3670  
3671 3671          return (error);
3672 3672  }
3673 3673  
3674 3674  
3675 3675  /*
3676 3676   * Get the base MTU for the case when path MTU discovery is not used.
3677 3677   * Takes the MTU of the IRE into account.
3678 3678   */
3679 3679  uint_t
3680 3680  ip_get_base_mtu(ill_t *ill, ire_t *ire)
3681 3681  {
3682 3682          uint_t mtu;
3683 3683          uint_t iremtu = ire->ire_metrics.iulp_mtu;
3684 3684  
3685 3685          if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3686 3686                  mtu = ill->ill_mc_mtu;
3687 3687          else
3688 3688                  mtu = ill->ill_mtu;
3689 3689  
3690 3690          if (iremtu != 0 && iremtu < mtu)
3691 3691                  mtu = iremtu;
3692 3692  
3693 3693          return (mtu);
3694 3694  }
3695 3695  
3696 3696  /*
3697 3697   * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3698 3698   * Assumes that ixa_ire, dce, and nce have already been set up.
3699 3699   *
3700 3700   * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3701 3701   * We avoid path MTU discovery if it is disabled with ndd.
3702 3702   * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3703 3703   *
3704 3704   * NOTE: We also used to turn it off for source routed packets. That
3705 3705   * is no longer required since the dce is per final destination.
3706 3706   */
3707 3707  uint_t
3708 3708  ip_get_pmtu(ip_xmit_attr_t *ixa)
3709 3709  {
3710 3710          ip_stack_t      *ipst = ixa->ixa_ipst;
3711 3711          dce_t           *dce;
3712 3712          nce_t           *nce;
3713 3713          ire_t           *ire;
3714 3714          uint_t          pmtu;
3715 3715  
3716 3716          ire = ixa->ixa_ire;
3717 3717          dce = ixa->ixa_dce;
3718 3718          nce = ixa->ixa_nce;
3719 3719  
3720 3720          /*
3721 3721           * If path MTU discovery has been turned off by ndd, then we ignore
3722 3722           * any dce_pmtu and for IPv4 we will not set DF.
3723 3723           */
3724 3724          if (!ipst->ips_ip_path_mtu_discovery)
3725 3725                  ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3726 3726  
3727 3727          pmtu = IP_MAXPACKET;
3728 3728          /*
3729 3729           * Decide whether whether IPv4 sets DF
3730 3730           * For IPv6 "no DF" means to use the 1280 mtu
3731 3731           */
3732 3732          if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3733 3733                  ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3734 3734          } else {
3735 3735                  ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3736 3736                  if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3737 3737                          pmtu = IPV6_MIN_MTU;
3738 3738          }
3739 3739  
3740 3740          /* Check if the PMTU is to old before we use it */
3741 3741          if ((dce->dce_flags & DCEF_PMTU) &&
3742 3742              TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3743 3743              ipst->ips_ip_pathmtu_interval) {
3744 3744                  /*
3745 3745                   * Older than 20 minutes. Drop the path MTU information.
3746 3746                   */
3747 3747                  mutex_enter(&dce->dce_lock);
3748 3748                  dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3749 3749                  dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3750 3750                  mutex_exit(&dce->dce_lock);
3751 3751                  dce_increment_generation(dce);
3752 3752          }
3753 3753  
3754 3754          /* The metrics on the route can lower the path MTU */
3755 3755          if (ire->ire_metrics.iulp_mtu != 0 &&
3756 3756              ire->ire_metrics.iulp_mtu < pmtu)
3757 3757                  pmtu = ire->ire_metrics.iulp_mtu;
3758 3758  
3759 3759          /*
3760 3760           * If the path MTU is smaller than some minimum, we still use dce_pmtu
3761 3761           * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3762 3762           * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3763 3763           */
3764 3764          if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3765 3765                  if (dce->dce_flags & DCEF_PMTU) {
3766 3766                          if (dce->dce_pmtu < pmtu)
3767 3767                                  pmtu = dce->dce_pmtu;
3768 3768  
3769 3769                          if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3770 3770                                  ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3771 3771                                  ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3772 3772                          } else {
3773 3773                                  ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3774 3774                                  ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3775 3775                          }
3776 3776                  } else {
3777 3777                          ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3778 3778                          ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3779 3779                  }
3780 3780          }
3781 3781  
3782 3782          /*
3783 3783           * If we have an IRE_LOCAL we use the loopback mtu instead of
3784 3784           * the ill for going out the wire i.e., IRE_LOCAL gets the same
3785 3785           * mtu as IRE_LOOPBACK.
3786 3786           */
3787 3787          if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3788 3788                  uint_t loopback_mtu;
3789 3789  
3790 3790                  loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3791 3791                      ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3792 3792  
3793 3793                  if (loopback_mtu < pmtu)
3794 3794                          pmtu = loopback_mtu;
3795 3795          } else if (nce != NULL) {
3796 3796                  /*
3797 3797                   * Make sure we don't exceed the interface MTU.
3798 3798                   * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3799 3799                   * an ill. We'd use the above IP_MAXPACKET in that case just
3800 3800                   * to tell the transport something larger than zero.
3801 3801                   */
3802 3802                  if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3803 3803                          if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3804 3804                                  pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3805 3805                          if (nce->nce_common->ncec_ill != nce->nce_ill &&
3806 3806                              nce->nce_ill->ill_mc_mtu < pmtu) {
3807 3807                                  /*
3808 3808                                   * for interfaces in an IPMP group, the mtu of
3809 3809                                   * the nce_ill (under_ill) could be different
3810 3810                                   * from the mtu of the ncec_ill, so we take the
3811 3811                                   * min of the two.
3812 3812                                   */
3813 3813                                  pmtu = nce->nce_ill->ill_mc_mtu;
3814 3814                          }
3815 3815                  } else {
3816 3816                          if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3817 3817                                  pmtu = nce->nce_common->ncec_ill->ill_mtu;
3818 3818                          if (nce->nce_common->ncec_ill != nce->nce_ill &&
3819 3819                              nce->nce_ill->ill_mtu < pmtu) {
3820 3820                                  /*
3821 3821                                   * for interfaces in an IPMP group, the mtu of
3822 3822                                   * the nce_ill (under_ill) could be different
3823 3823                                   * from the mtu of the ncec_ill, so we take the
3824 3824                                   * min of the two.
3825 3825                                   */
3826 3826                                  pmtu = nce->nce_ill->ill_mtu;
3827 3827                          }
3828 3828                  }
3829 3829          }
3830 3830  
3831 3831          /*
3832 3832           * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3833 3833           * Only applies to IPv6.
3834 3834           */
3835 3835          if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3836 3836                  if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3837 3837                          switch (ixa->ixa_use_min_mtu) {
3838 3838                          case IPV6_USE_MIN_MTU_MULTICAST:
3839 3839                                  if (ire->ire_type & IRE_MULTICAST)
3840 3840                                          pmtu = IPV6_MIN_MTU;
3841 3841                                  break;
3842 3842                          case IPV6_USE_MIN_MTU_ALWAYS:
3843 3843                                  pmtu = IPV6_MIN_MTU;
3844 3844                                  break;
3845 3845                          case IPV6_USE_MIN_MTU_NEVER:
3846 3846                                  break;
3847 3847                          }
3848 3848                  } else {
3849 3849                          /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3850 3850                          if (ire->ire_type & IRE_MULTICAST)
3851 3851                                  pmtu = IPV6_MIN_MTU;
3852 3852                  }
3853 3853          }
3854 3854  
3855 3855          /*
3856 3856           * For multirouted IPv6 packets, the IP layer will insert a 8-byte
3857 3857           * fragment header in every packet. We compensate for those cases by
3858 3858           * returning a smaller path MTU to the ULP.
3859 3859           *
3860 3860           * In the case of CGTP then ip_output will add a fragment header.
3861 3861           * Make sure there is room for it by telling a smaller number
3862 3862           * to the transport.
3863 3863           *
3864 3864           * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here
3865 3865           * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu()
3866 3866           * which is the size of the packets it can send.
3867 3867           */
3868 3868          if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3869 3869                  if ((ire->ire_flags & RTF_MULTIRT) ||
3870 3870                      (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) {
3871 3871                          pmtu -= sizeof (ip6_frag_t);
3872 3872                          ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR;
3873 3873                  }
3874 3874          }
3875 3875  
3876 3876          return (pmtu);
3877 3877  }
3878 3878  
3879 3879  /*
3880 3880   * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3881 3881   * the final piece where we don't.  Return a pointer to the first mblk in the
3882 3882   * result, and update the pointer to the next mblk to chew on.  If anything
3883 3883   * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3884 3884   * NULL pointer.
3885 3885   */
3886 3886  mblk_t *
3887 3887  ip_carve_mp(mblk_t **mpp, ssize_t len)
3888 3888  {
3889 3889          mblk_t  *mp0;
3890 3890          mblk_t  *mp1;
3891 3891          mblk_t  *mp2;
3892 3892  
3893 3893          if (!len || !mpp || !(mp0 = *mpp))
3894 3894                  return (NULL);
3895 3895          /* If we aren't going to consume the first mblk, we need a dup. */
3896 3896          if (mp0->b_wptr - mp0->b_rptr > len) {
3897 3897                  mp1 = dupb(mp0);
3898 3898                  if (mp1) {
3899 3899                          /* Partition the data between the two mblks. */
3900 3900                          mp1->b_wptr = mp1->b_rptr + len;
3901 3901                          mp0->b_rptr = mp1->b_wptr;
3902 3902                          /*
3903 3903                           * after adjustments if mblk not consumed is now
3904 3904                           * unaligned, try to align it. If this fails free
3905 3905                           * all messages and let upper layer recover.
3906 3906                           */
3907 3907                          if (!OK_32PTR(mp0->b_rptr)) {
3908 3908                                  if (!pullupmsg(mp0, -1)) {
3909 3909                                          freemsg(mp0);
3910 3910                                          freemsg(mp1);
3911 3911                                          *mpp = NULL;
3912 3912                                          return (NULL);
3913 3913                                  }
3914 3914                          }
3915 3915                  }
3916 3916                  return (mp1);
3917 3917          }
3918 3918          /* Eat through as many mblks as we need to get len bytes. */
3919 3919          len -= mp0->b_wptr - mp0->b_rptr;
3920 3920          for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3921 3921                  if (mp2->b_wptr - mp2->b_rptr > len) {
3922 3922                          /*
3923 3923                           * We won't consume the entire last mblk.  Like
3924 3924                           * above, dup and partition it.
3925 3925                           */
3926 3926                          mp1->b_cont = dupb(mp2);
3927 3927                          mp1 = mp1->b_cont;
3928 3928                          if (!mp1) {
3929 3929                                  /*
3930 3930                                   * Trouble.  Rather than go to a lot of
3931 3931                                   * trouble to clean up, we free the messages.
3932 3932                                   * This won't be any worse than losing it on
3933 3933                                   * the wire.
3934 3934                                   */
3935 3935                                  freemsg(mp0);
3936 3936                                  freemsg(mp2);
3937 3937                                  *mpp = NULL;
3938 3938                                  return (NULL);
3939 3939                          }
3940 3940                          mp1->b_wptr = mp1->b_rptr + len;
3941 3941                          mp2->b_rptr = mp1->b_wptr;
3942 3942                          /*
3943 3943                           * after adjustments if mblk not consumed is now
3944 3944                           * unaligned, try to align it. If this fails free
3945 3945                           * all messages and let upper layer recover.
3946 3946                           */
3947 3947                          if (!OK_32PTR(mp2->b_rptr)) {
3948 3948                                  if (!pullupmsg(mp2, -1)) {
3949 3949                                          freemsg(mp0);
3950 3950                                          freemsg(mp2);
3951 3951                                          *mpp = NULL;
3952 3952                                          return (NULL);
3953 3953                                  }
3954 3954                          }
3955 3955                          *mpp = mp2;
3956 3956                          return (mp0);
3957 3957                  }
3958 3958                  /* Decrement len by the amount we just got. */
3959 3959                  len -= mp2->b_wptr - mp2->b_rptr;
3960 3960          }
3961 3961          /*
3962 3962           * len should be reduced to zero now.  If not our caller has
3963 3963           * screwed up.
3964 3964           */
3965 3965          if (len) {
3966 3966                  /* Shouldn't happen! */
3967 3967                  freemsg(mp0);
3968 3968                  *mpp = NULL;
3969 3969                  return (NULL);
3970 3970          }
3971 3971          /*
3972 3972           * We consumed up to exactly the end of an mblk.  Detach the part
3973 3973           * we are returning from the rest of the chain.
3974 3974           */
3975 3975          mp1->b_cont = NULL;
3976 3976          *mpp = mp2;
3977 3977          return (mp0);
3978 3978  }
3979 3979  
3980 3980  /* The ill stream is being unplumbed. Called from ip_close */
3981 3981  int
3982 3982  ip_modclose(ill_t *ill)
3983 3983  {
3984 3984          boolean_t success;
3985 3985          ipsq_t  *ipsq;
3986 3986          ipif_t  *ipif;
3987 3987          queue_t *q = ill->ill_rq;
3988 3988          ip_stack_t      *ipst = ill->ill_ipst;
3989 3989          int     i;
3990 3990          arl_ill_common_t *ai = ill->ill_common;
3991 3991  
3992 3992          /*
3993 3993           * The punlink prior to this may have initiated a capability
3994 3994           * negotiation. But ipsq_enter will block until that finishes or
3995 3995           * times out.
3996 3996           */
3997 3997          success = ipsq_enter(ill, B_FALSE, NEW_OP);
3998 3998  
3999 3999          /*
4000 4000           * Open/close/push/pop is guaranteed to be single threaded
4001 4001           * per stream by STREAMS. FS guarantees that all references
4002 4002           * from top are gone before close is called. So there can't
4003 4003           * be another close thread that has set CONDEMNED on this ill.
4004 4004           * and cause ipsq_enter to return failure.
4005 4005           */
4006 4006          ASSERT(success);
4007 4007          ipsq = ill->ill_phyint->phyint_ipsq;
4008 4008  
4009 4009          /*
4010 4010           * Mark it condemned. No new reference will be made to this ill.
4011 4011           * Lookup functions will return an error. Threads that try to
4012 4012           * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
4013 4013           * that the refcnt will drop down to zero.
4014 4014           */
4015 4015          mutex_enter(&ill->ill_lock);
4016 4016          ill->ill_state_flags |= ILL_CONDEMNED;
4017 4017          for (ipif = ill->ill_ipif; ipif != NULL;
4018 4018              ipif = ipif->ipif_next) {
4019 4019                  ipif->ipif_state_flags |= IPIF_CONDEMNED;
4020 4020          }
4021 4021          /*
4022 4022           * Wake up anybody waiting to enter the ipsq. ipsq_enter
4023 4023           * returns  error if ILL_CONDEMNED is set
4024 4024           */
4025 4025          cv_broadcast(&ill->ill_cv);
4026 4026          mutex_exit(&ill->ill_lock);
4027 4027  
4028 4028          /*
4029 4029           * Send all the deferred DLPI messages downstream which came in
4030 4030           * during the small window right before ipsq_enter(). We do this
4031 4031           * without waiting for the ACKs because all the ACKs for M_PROTO
4032 4032           * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
4033 4033           */
4034 4034          ill_dlpi_send_deferred(ill);
4035 4035  
4036 4036          /*
4037 4037           * Shut down fragmentation reassembly.
4038 4038           * ill_frag_timer won't start a timer again.
4039 4039           * Now cancel any existing timer
4040 4040           */
4041 4041          (void) untimeout(ill->ill_frag_timer_id);
4042 4042          (void) ill_frag_timeout(ill, 0);
4043 4043  
4044 4044          /*
4045 4045           * Call ill_delete to bring down the ipifs, ilms and ill on
4046 4046           * this ill. Then wait for the refcnts to drop to zero.
4047 4047           * ill_is_freeable checks whether the ill is really quiescent.
4048 4048           * Then make sure that threads that are waiting to enter the
4049 4049           * ipsq have seen the error returned by ipsq_enter and have
4050 4050           * gone away. Then we call ill_delete_tail which does the
4051 4051           * DL_UNBIND_REQ with the driver and then qprocsoff.
4052 4052           */
4053 4053          ill_delete(ill);
4054 4054          mutex_enter(&ill->ill_lock);
4055 4055          while (!ill_is_freeable(ill))
4056 4056                  cv_wait(&ill->ill_cv, &ill->ill_lock);
4057 4057  
4058 4058          while (ill->ill_waiters)
4059 4059                  cv_wait(&ill->ill_cv, &ill->ill_lock);
4060 4060  
4061 4061          mutex_exit(&ill->ill_lock);
4062 4062  
4063 4063          /*
4064 4064           * ill_delete_tail drops reference on ill_ipst, but we need to keep
4065 4065           * it held until the end of the function since the cleanup
4066 4066           * below needs to be able to use the ip_stack_t.
4067 4067           */
4068 4068          netstack_hold(ipst->ips_netstack);
4069 4069  
4070 4070          /* qprocsoff is done via ill_delete_tail */
4071 4071          ill_delete_tail(ill);
4072 4072          /*
4073 4073           * synchronously wait for arp stream to unbind. After this, we
4074 4074           * cannot get any data packets up from the driver.
4075 4075           */
4076 4076          arp_unbind_complete(ill);
4077 4077          ASSERT(ill->ill_ipst == NULL);
4078 4078  
4079 4079          /*
4080 4080           * Walk through all conns and qenable those that have queued data.
4081 4081           * Close synchronization needs this to
4082 4082           * be done to ensure that all upper layers blocked
4083 4083           * due to flow control to the closing device
4084 4084           * get unblocked.
4085 4085           */
4086 4086          ip1dbg(("ip_wsrv: walking\n"));
4087 4087          for (i = 0; i < TX_FANOUT_SIZE; i++) {
4088 4088                  conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
4089 4089          }
4090 4090  
4091 4091          /*
4092 4092           * ai can be null if this is an IPv6 ill, or if the IPv4
4093 4093           * stream is being torn down before ARP was plumbed (e.g.,
4094 4094           * /sbin/ifconfig plumbing a stream twice, and encountering
4095 4095           * an error
4096 4096           */
4097 4097          if (ai != NULL) {
4098 4098                  ASSERT(!ill->ill_isv6);
4099 4099                  mutex_enter(&ai->ai_lock);
4100 4100                  ai->ai_ill = NULL;
4101 4101                  if (ai->ai_arl == NULL) {
4102 4102                          mutex_destroy(&ai->ai_lock);
4103 4103                          kmem_free(ai, sizeof (*ai));
4104 4104                  } else {
4105 4105                          cv_signal(&ai->ai_ill_unplumb_done);
4106 4106                          mutex_exit(&ai->ai_lock);
4107 4107                  }
4108 4108          }
4109 4109  
4110 4110          mutex_enter(&ipst->ips_ip_mi_lock);
4111 4111          mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
4112 4112          mutex_exit(&ipst->ips_ip_mi_lock);
4113 4113  
4114 4114          /*
4115 4115           * credp could be null if the open didn't succeed and ip_modopen
4116 4116           * itself calls ip_close.
4117 4117           */
4118 4118          if (ill->ill_credp != NULL)
4119 4119                  crfree(ill->ill_credp);
4120 4120  
4121 4121          mutex_destroy(&ill->ill_saved_ire_lock);
4122 4122          mutex_destroy(&ill->ill_lock);
4123 4123          rw_destroy(&ill->ill_mcast_lock);
4124 4124          mutex_destroy(&ill->ill_mcast_serializer);
4125 4125          list_destroy(&ill->ill_nce);
4126 4126  
4127 4127          /*
4128 4128           * Now we are done with the module close pieces that
4129 4129           * need the netstack_t.
4130 4130           */
4131 4131          netstack_rele(ipst->ips_netstack);
4132 4132  
4133 4133          mi_close_free((IDP)ill);
4134 4134          q->q_ptr = WR(q)->q_ptr = NULL;
4135 4135  
4136 4136          ipsq_exit(ipsq);
4137 4137  
4138 4138          return (0);
4139 4139  }
4140 4140  
4141 4141  /*
4142 4142   * This is called as part of close() for IP, UDP, ICMP, and RTS
4143 4143   * in order to quiesce the conn.
4144 4144   */
4145 4145  void
4146 4146  ip_quiesce_conn(conn_t *connp)
4147 4147  {
4148 4148          boolean_t       drain_cleanup_reqd = B_FALSE;
4149 4149          boolean_t       conn_ioctl_cleanup_reqd = B_FALSE;
4150 4150          boolean_t       ilg_cleanup_reqd = B_FALSE;
4151 4151          ip_stack_t      *ipst;
4152 4152  
4153 4153          ASSERT(!IPCL_IS_TCP(connp));
4154 4154          ipst = connp->conn_netstack->netstack_ip;
4155 4155  
4156 4156          /*
4157 4157           * Mark the conn as closing, and this conn must not be
4158 4158           * inserted in future into any list. Eg. conn_drain_insert(),
4159 4159           * won't insert this conn into the conn_drain_list.
4160 4160           *
4161 4161           * conn_idl, and conn_ilg cannot get set henceforth.
4162 4162           */
4163 4163          mutex_enter(&connp->conn_lock);
4164 4164          ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4165 4165          connp->conn_state_flags |= CONN_CLOSING;
4166 4166          if (connp->conn_idl != NULL)
4167 4167                  drain_cleanup_reqd = B_TRUE;
4168 4168          if (connp->conn_oper_pending_ill != NULL)
4169 4169                  conn_ioctl_cleanup_reqd = B_TRUE;
4170 4170          if (connp->conn_dhcpinit_ill != NULL) {
4171 4171                  ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4172 4172                  atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4173 4173                  ill_set_inputfn(connp->conn_dhcpinit_ill);
4174 4174                  connp->conn_dhcpinit_ill = NULL;
4175 4175          }
4176 4176          if (connp->conn_ilg != NULL)
4177 4177                  ilg_cleanup_reqd = B_TRUE;
4178 4178          mutex_exit(&connp->conn_lock);
4179 4179  
4180 4180          if (conn_ioctl_cleanup_reqd)
4181 4181                  conn_ioctl_cleanup(connp);
4182 4182  
4183 4183          if (is_system_labeled() && connp->conn_anon_port) {
4184 4184                  (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4185 4185                      connp->conn_mlp_type, connp->conn_proto,
4186 4186                      ntohs(connp->conn_lport), B_FALSE);
4187 4187                  connp->conn_anon_port = 0;
4188 4188          }
4189 4189          connp->conn_mlp_type = mlptSingle;
4190 4190  
4191 4191          /*
4192 4192           * Remove this conn from any fanout list it is on.
4193 4193           * and then wait for any threads currently operating
4194 4194           * on this endpoint to finish
4195 4195           */
4196 4196          ipcl_hash_remove(connp);
4197 4197  
4198 4198          /*
4199 4199           * Remove this conn from the drain list, and do any other cleanup that
4200 4200           * may be required.  (TCP conns are never flow controlled, and
4201 4201           * conn_idl will be NULL.)
4202 4202           */
4203 4203          if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4204 4204                  idl_t *idl = connp->conn_idl;
4205 4205  
4206 4206                  mutex_enter(&idl->idl_lock);
4207 4207                  conn_drain(connp, B_TRUE);
4208 4208                  mutex_exit(&idl->idl_lock);
4209 4209          }
4210 4210  
4211 4211          if (connp == ipst->ips_ip_g_mrouter)
4212 4212                  (void) ip_mrouter_done(ipst);
4213 4213  
4214 4214          if (ilg_cleanup_reqd)
4215 4215                  ilg_delete_all(connp);
4216 4216  
4217 4217          /*
4218 4218           * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4219 4219           * callers from write side can't be there now because close
4220 4220           * is in progress. The only other caller is ipcl_walk
4221 4221           * which checks for the condemned flag.
4222 4222           */
4223 4223          mutex_enter(&connp->conn_lock);
4224 4224          connp->conn_state_flags |= CONN_CONDEMNED;
4225 4225          while (connp->conn_ref != 1)
4226 4226                  cv_wait(&connp->conn_cv, &connp->conn_lock);
4227 4227          connp->conn_state_flags |= CONN_QUIESCED;
4228 4228          mutex_exit(&connp->conn_lock);
4229 4229  }
4230 4230  
4231 4231  /* ARGSUSED */
4232 4232  int
4233 4233  ip_close(queue_t *q, int flags, cred_t *credp __unused)
4234 4234  {
4235 4235          conn_t          *connp;
4236 4236  
4237 4237          /*
4238 4238           * Call the appropriate delete routine depending on whether this is
4239 4239           * a module or device.
4240 4240           */
4241 4241          if (WR(q)->q_next != NULL) {
4242 4242                  /* This is a module close */
4243 4243                  return (ip_modclose((ill_t *)q->q_ptr));
4244 4244          }
4245 4245  
4246 4246          connp = q->q_ptr;
4247 4247          ip_quiesce_conn(connp);
4248 4248  
4249 4249          qprocsoff(q);
4250 4250  
4251 4251          /*
4252 4252           * Now we are truly single threaded on this stream, and can
4253 4253           * delete the things hanging off the connp, and finally the connp.
4254 4254           * We removed this connp from the fanout list, it cannot be
4255 4255           * accessed thru the fanouts, and we already waited for the
4256 4256           * conn_ref to drop to 0. We are already in close, so
4257 4257           * there cannot be any other thread from the top. qprocsoff
4258 4258           * has completed, and service has completed or won't run in
4259 4259           * future.
4260 4260           */
4261 4261          ASSERT(connp->conn_ref == 1);
4262 4262  
4263 4263          inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4264 4264  
4265 4265          connp->conn_ref--;
4266 4266          ipcl_conn_destroy(connp);
4267 4267  
4268 4268          q->q_ptr = WR(q)->q_ptr = NULL;
4269 4269          return (0);
4270 4270  }
4271 4271  
4272 4272  /*
4273 4273   * Wapper around putnext() so that ip_rts_request can merely use
4274 4274   * conn_recv.
4275 4275   */
4276 4276  /*ARGSUSED2*/
4277 4277  static void
4278 4278  ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4279 4279  {
4280 4280          conn_t *connp = (conn_t *)arg1;
4281 4281  
4282 4282          putnext(connp->conn_rq, mp);
4283 4283  }
4284 4284  
4285 4285  /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4286 4286  /* ARGSUSED */
4287 4287  static void
4288 4288  ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4289 4289  {
4290 4290          freemsg(mp);
4291 4291  }
4292 4292  
4293 4293  /*
4294 4294   * Called when the module is about to be unloaded
4295 4295   */
4296 4296  void
4297 4297  ip_ddi_destroy(void)
4298 4298  {
4299 4299          /* This needs to be called before destroying any transports. */
4300 4300          mutex_enter(&cpu_lock);
4301 4301          unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4302 4302          mutex_exit(&cpu_lock);
4303 4303  
4304 4304          tnet_fini();
4305 4305  
4306 4306          icmp_ddi_g_destroy();
4307 4307          rts_ddi_g_destroy();
4308 4308          udp_ddi_g_destroy();
4309 4309          sctp_ddi_g_destroy();
4310 4310          tcp_ddi_g_destroy();
4311 4311          ilb_ddi_g_destroy();
4312 4312          dce_g_destroy();
4313 4313          ipsec_policy_g_destroy();
4314 4314          ipcl_g_destroy();
4315 4315          ip_net_g_destroy();
4316 4316          ip_ire_g_fini();
4317 4317          inet_minor_destroy(ip_minor_arena_sa);
4318 4318  #if defined(_LP64)
4319 4319          inet_minor_destroy(ip_minor_arena_la);
4320 4320  #endif
4321 4321  
4322 4322  #ifdef DEBUG
4323 4323          list_destroy(&ip_thread_list);
4324 4324          rw_destroy(&ip_thread_rwlock);
4325 4325          tsd_destroy(&ip_thread_data);
4326 4326  #endif
4327 4327  
4328 4328          netstack_unregister(NS_IP);
4329 4329  }
4330 4330  
4331 4331  /*
4332 4332   * First step in cleanup.
4333 4333   */
4334 4334  /* ARGSUSED */
4335 4335  static void
4336 4336  ip_stack_shutdown(netstackid_t stackid, void *arg)
4337 4337  {
4338 4338          ip_stack_t *ipst = (ip_stack_t *)arg;
4339 4339          kt_did_t ktid;
4340 4340  
4341 4341  #ifdef NS_DEBUG
4342 4342          printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4343 4343  #endif
4344 4344  
4345 4345          /*
4346 4346           * Perform cleanup for special interfaces (loopback and IPMP).
4347 4347           */
4348 4348          ip_interface_cleanup(ipst);
4349 4349  
4350 4350          /*
4351 4351           * The *_hook_shutdown()s start the process of notifying any
4352 4352           * consumers that things are going away.... nothing is destroyed.
4353 4353           */
4354 4354          ipv4_hook_shutdown(ipst);
4355 4355          ipv6_hook_shutdown(ipst);
4356 4356          arp_hook_shutdown(ipst);
4357 4357  
4358 4358          mutex_enter(&ipst->ips_capab_taskq_lock);
4359 4359          ktid = ipst->ips_capab_taskq_thread->t_did;
4360 4360          ipst->ips_capab_taskq_quit = B_TRUE;
4361 4361          cv_signal(&ipst->ips_capab_taskq_cv);
4362 4362          mutex_exit(&ipst->ips_capab_taskq_lock);
4363 4363  
4364 4364          /*
4365 4365           * In rare occurrences, particularly on virtual hardware where CPUs can
4366 4366           * be de-scheduled, the thread that we just signaled will not run until
4367 4367           * after we have gotten through parts of ip_stack_fini. If that happens
4368 4368           * then we'll try to grab the ips_capab_taskq_lock as part of returning
4369 4369           * from cv_wait which no longer exists.
4370 4370           */
4371 4371          thread_join(ktid);
4372 4372  }
4373 4373  
4374 4374  /*
4375 4375   * Free the IP stack instance.
4376 4376   */
4377 4377  static void
4378 4378  ip_stack_fini(netstackid_t stackid, void *arg)
4379 4379  {
4380 4380          ip_stack_t *ipst = (ip_stack_t *)arg;
4381 4381          int ret;
4382 4382  
4383 4383  #ifdef NS_DEBUG
4384 4384          printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4385 4385  #endif
4386 4386          /*
4387 4387           * At this point, all of the notifications that the events and
4388 4388           * protocols are going away have been run, meaning that we can
4389 4389           * now set about starting to clean things up.
4390 4390           */
4391 4391          ipobs_fini(ipst);
4392 4392          ipv4_hook_destroy(ipst);
4393 4393          ipv6_hook_destroy(ipst);
4394 4394          arp_hook_destroy(ipst);
4395 4395          ip_net_destroy(ipst);
4396 4396  
4397 4397          ipmp_destroy(ipst);
4398 4398  
4399 4399          ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4400 4400          ipst->ips_ip_mibkp = NULL;
4401 4401          icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4402 4402          ipst->ips_icmp_mibkp = NULL;
4403 4403          ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4404 4404          ipst->ips_ip_kstat = NULL;
4405 4405          bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4406 4406          ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4407 4407          ipst->ips_ip6_kstat = NULL;
4408 4408          bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4409 4409  
4410 4410          kmem_free(ipst->ips_propinfo_tbl,
4411 4411              ip_propinfo_count * sizeof (mod_prop_info_t));
4412 4412          ipst->ips_propinfo_tbl = NULL;
4413 4413  
4414 4414          dce_stack_destroy(ipst);
4415 4415          ip_mrouter_stack_destroy(ipst);
4416 4416  
4417 4417          /*
4418 4418           * Quiesce all of our timers. Note we set the quiesce flags before we
4419 4419           * call untimeout. The slowtimers may actually kick off another instance
4420 4420           * of the non-slow timers.
4421 4421           */
4422 4422          mutex_enter(&ipst->ips_igmp_timer_lock);
4423 4423          ipst->ips_igmp_timer_quiesce = B_TRUE;
4424 4424          mutex_exit(&ipst->ips_igmp_timer_lock);
4425 4425  
4426 4426          mutex_enter(&ipst->ips_mld_timer_lock);
4427 4427          ipst->ips_mld_timer_quiesce = B_TRUE;
4428 4428          mutex_exit(&ipst->ips_mld_timer_lock);
4429 4429  
4430 4430          mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
4431 4431          ipst->ips_igmp_slowtimeout_quiesce = B_TRUE;
4432 4432          mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
4433 4433  
4434 4434          mutex_enter(&ipst->ips_mld_slowtimeout_lock);
4435 4435          ipst->ips_mld_slowtimeout_quiesce = B_TRUE;
4436 4436          mutex_exit(&ipst->ips_mld_slowtimeout_lock);
4437 4437  
4438 4438          ret = untimeout(ipst->ips_igmp_timeout_id);
4439 4439          if (ret == -1) {
4440 4440                  ASSERT(ipst->ips_igmp_timeout_id == 0);
4441 4441          } else {
4442 4442                  ASSERT(ipst->ips_igmp_timeout_id != 0);
4443 4443                  ipst->ips_igmp_timeout_id = 0;
4444 4444          }
4445 4445          ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4446 4446          if (ret == -1) {
4447 4447                  ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4448 4448          } else {
4449 4449                  ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4450 4450                  ipst->ips_igmp_slowtimeout_id = 0;
4451 4451          }
4452 4452          ret = untimeout(ipst->ips_mld_timeout_id);
4453 4453          if (ret == -1) {
4454 4454                  ASSERT(ipst->ips_mld_timeout_id == 0);
4455 4455          } else {
4456 4456                  ASSERT(ipst->ips_mld_timeout_id != 0);
4457 4457                  ipst->ips_mld_timeout_id = 0;
4458 4458          }
4459 4459          ret = untimeout(ipst->ips_mld_slowtimeout_id);
4460 4460          if (ret == -1) {
4461 4461                  ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4462 4462          } else {
4463 4463                  ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4464 4464                  ipst->ips_mld_slowtimeout_id = 0;
4465 4465          }
4466 4466  
4467 4467          ip_ire_fini(ipst);
4468 4468          ip6_asp_free(ipst);
4469 4469          conn_drain_fini(ipst);
4470 4470          ipcl_destroy(ipst);
4471 4471  
4472 4472          mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4473 4473          mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4474 4474          kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4475 4475          ipst->ips_ndp4 = NULL;
4476 4476          kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4477 4477          ipst->ips_ndp6 = NULL;
4478 4478  
4479 4479          if (ipst->ips_loopback_ksp != NULL) {
4480 4480                  kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4481 4481                  ipst->ips_loopback_ksp = NULL;
4482 4482          }
4483 4483  
4484 4484          mutex_destroy(&ipst->ips_capab_taskq_lock);
4485 4485          cv_destroy(&ipst->ips_capab_taskq_cv);
4486 4486  
4487 4487          rw_destroy(&ipst->ips_srcid_lock);
4488 4488  
4489 4489          mutex_destroy(&ipst->ips_ip_mi_lock);
4490 4490          rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4491 4491  
4492 4492          mutex_destroy(&ipst->ips_igmp_timer_lock);
4493 4493          mutex_destroy(&ipst->ips_mld_timer_lock);
4494 4494          mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4495 4495          mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4496 4496          mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4497 4497          rw_destroy(&ipst->ips_ill_g_lock);
4498 4498  
4499 4499          kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4500 4500          ipst->ips_phyint_g_list = NULL;
4501 4501          kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4502 4502          ipst->ips_ill_g_heads = NULL;
4503 4503  
4504 4504          ldi_ident_release(ipst->ips_ldi_ident);
4505 4505          kmem_free(ipst, sizeof (*ipst));
4506 4506  }
4507 4507  
4508 4508  /*
4509 4509   * This function is called from the TSD destructor, and is used to debug
4510 4510   * reference count issues in IP. See block comment in <inet/ip_if.h> for
4511 4511   * details.
4512 4512   */
4513 4513  static void
4514 4514  ip_thread_exit(void *phash)
4515 4515  {
4516 4516          th_hash_t *thh = phash;
4517 4517  
4518 4518          rw_enter(&ip_thread_rwlock, RW_WRITER);
4519 4519          list_remove(&ip_thread_list, thh);
4520 4520          rw_exit(&ip_thread_rwlock);
4521 4521          mod_hash_destroy_hash(thh->thh_hash);
4522 4522          kmem_free(thh, sizeof (*thh));
4523 4523  }
4524 4524  
4525 4525  /*
4526 4526   * Called when the IP kernel module is loaded into the kernel
4527 4527   */
4528 4528  void
4529 4529  ip_ddi_init(void)
4530 4530  {
4531 4531          ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4532 4532  
4533 4533          /*
4534 4534           * For IP and TCP the minor numbers should start from 2 since we have 4
4535 4535           * initial devices: ip, ip6, tcp, tcp6.
4536 4536           */
4537 4537          /*
4538 4538           * If this is a 64-bit kernel, then create two separate arenas -
4539 4539           * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4540 4540           * other for socket apps in the range 2^^18 through 2^^32-1.
4541 4541           */
4542 4542          ip_minor_arena_la = NULL;
4543 4543          ip_minor_arena_sa = NULL;
4544 4544  #if defined(_LP64)
4545 4545          if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4546 4546              INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4547 4547                  cmn_err(CE_PANIC,
4548 4548                      "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4549 4549          }
4550 4550          if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4551 4551              MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4552 4552                  cmn_err(CE_PANIC,
4553 4553                      "ip_ddi_init: ip_minor_arena_la creation failed\n");
4554 4554          }
4555 4555  #else
4556 4556          if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4557 4557              INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4558 4558                  cmn_err(CE_PANIC,
4559 4559                      "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4560 4560          }
4561 4561  #endif
4562 4562          ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4563 4563  
4564 4564          ipcl_g_init();
4565 4565          ip_ire_g_init();
4566 4566          ip_net_g_init();
4567 4567  
4568 4568  #ifdef DEBUG
4569 4569          tsd_create(&ip_thread_data, ip_thread_exit);
4570 4570          rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4571 4571          list_create(&ip_thread_list, sizeof (th_hash_t),
4572 4572              offsetof(th_hash_t, thh_link));
4573 4573  #endif
4574 4574          ipsec_policy_g_init();
4575 4575          tcp_ddi_g_init();
4576 4576          sctp_ddi_g_init();
4577 4577          dce_g_init();
4578 4578  
4579 4579          /*
4580 4580           * We want to be informed each time a stack is created or
4581 4581           * destroyed in the kernel, so we can maintain the
4582 4582           * set of udp_stack_t's.
4583 4583           */
4584 4584          netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4585 4585              ip_stack_fini);
4586 4586  
4587 4587          tnet_init();
4588 4588  
4589 4589          udp_ddi_g_init();
4590 4590          rts_ddi_g_init();
4591 4591          icmp_ddi_g_init();
4592 4592          ilb_ddi_g_init();
4593 4593  
4594 4594          /* This needs to be called after all transports are initialized. */
4595 4595          mutex_enter(&cpu_lock);
4596 4596          register_cpu_setup_func(ip_tp_cpu_update, NULL);
4597 4597          mutex_exit(&cpu_lock);
4598 4598  }
4599 4599  
4600 4600  /*
4601 4601   * Initialize the IP stack instance.
4602 4602   */
4603 4603  static void *
4604 4604  ip_stack_init(netstackid_t stackid, netstack_t *ns)
4605 4605  {
4606 4606          ip_stack_t      *ipst;
4607 4607          size_t          arrsz;
4608 4608          major_t         major;
4609 4609  
4610 4610  #ifdef NS_DEBUG
4611 4611          printf("ip_stack_init(stack %d)\n", stackid);
4612 4612  #endif
4613 4613  
4614 4614          ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4615 4615          ipst->ips_netstack = ns;
4616 4616  
4617 4617          ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4618 4618              KM_SLEEP);
4619 4619          ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4620 4620              KM_SLEEP);
4621 4621          ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4622 4622          ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4623 4623          mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4624 4624          mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4625 4625  
4626 4626          mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4627 4627          ipst->ips_igmp_deferred_next = INFINITY;
4628 4628          mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4629 4629          ipst->ips_mld_deferred_next = INFINITY;
4630 4630          mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4631 4631          mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4632 4632          mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4633 4633          mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4634 4634          rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4635 4635          rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4636 4636  
4637 4637          ipcl_init(ipst);
4638 4638          ip_ire_init(ipst);
4639 4639          ip6_asp_init(ipst);
4640 4640          ipif_init(ipst);
4641 4641          conn_drain_init(ipst);
4642 4642          ip_mrouter_stack_init(ipst);
4643 4643          dce_stack_init(ipst);
4644 4644  
4645 4645          ipst->ips_ip_multirt_log_interval = 1000;
4646 4646  
4647 4647          ipst->ips_ill_index = 1;
4648 4648  
4649 4649          ipst->ips_saved_ip_forwarding = -1;
4650 4650          ipst->ips_reg_vif_num = ALL_VIFS;       /* Index to Register vif */
4651 4651  
4652 4652          arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4653 4653          ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4654 4654          bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4655 4655  
4656 4656          ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4657 4657          ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4658 4658          ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4659 4659          ipst->ips_ip6_kstat =
4660 4660              ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4661 4661  
4662 4662          ipst->ips_ip_src_id = 1;
4663 4663          rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4664 4664  
4665 4665          ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4666 4666  
4667 4667          ip_net_init(ipst, ns);
4668 4668          ipv4_hook_init(ipst);
4669 4669          ipv6_hook_init(ipst);
4670 4670          arp_hook_init(ipst);
4671 4671          ipmp_init(ipst);
4672 4672          ipobs_init(ipst);
4673 4673  
4674 4674          /*
4675 4675           * Create the taskq dispatcher thread and initialize related stuff.
4676 4676           */
4677 4677          mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4678 4678          cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4679 4679          ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4680 4680              ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4681 4681  
4682 4682          major = mod_name_to_major(INET_NAME);
4683 4683          (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4684 4684          return (ipst);
4685 4685  }
4686 4686  
4687 4687  /*
4688 4688   * Allocate and initialize a DLPI template of the specified length.  (May be
4689 4689   * called as writer.)
4690 4690   */
4691 4691  mblk_t *
4692 4692  ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4693 4693  {
4694 4694          mblk_t  *mp;
4695 4695  
4696 4696          mp = allocb(len, BPRI_MED);
4697 4697          if (!mp)
4698 4698                  return (NULL);
4699 4699  
4700 4700          /*
4701 4701           * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4702 4702           * of which we don't seem to use) are sent with M_PCPROTO, and
4703 4703           * that other DLPI are M_PROTO.
4704 4704           */
4705 4705          if (prim == DL_INFO_REQ) {
4706 4706                  mp->b_datap->db_type = M_PCPROTO;
4707 4707          } else {
4708 4708                  mp->b_datap->db_type = M_PROTO;
4709 4709          }
4710 4710  
4711 4711          mp->b_wptr = mp->b_rptr + len;
4712 4712          bzero(mp->b_rptr, len);
4713 4713          ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4714 4714          return (mp);
4715 4715  }
4716 4716  
4717 4717  /*
4718 4718   * Allocate and initialize a DLPI notification.  (May be called as writer.)
4719 4719   */
4720 4720  mblk_t *
4721 4721  ip_dlnotify_alloc(uint_t notification, uint_t data)
4722 4722  {
4723 4723          dl_notify_ind_t *notifyp;
4724 4724          mblk_t          *mp;
4725 4725  
4726 4726          if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4727 4727                  return (NULL);
4728 4728  
4729 4729          notifyp = (dl_notify_ind_t *)mp->b_rptr;
4730 4730          notifyp->dl_notification = notification;
4731 4731          notifyp->dl_data = data;
4732 4732          return (mp);
4733 4733  }
4734 4734  
4735 4735  mblk_t *
4736 4736  ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4737 4737  {
4738 4738          dl_notify_ind_t *notifyp;
4739 4739          mblk_t          *mp;
4740 4740  
4741 4741          if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4742 4742                  return (NULL);
4743 4743  
4744 4744          notifyp = (dl_notify_ind_t *)mp->b_rptr;
4745 4745          notifyp->dl_notification = notification;
4746 4746          notifyp->dl_data1 = data1;
4747 4747          notifyp->dl_data2 = data2;
4748 4748          return (mp);
4749 4749  }
4750 4750  
4751 4751  /*
4752 4752   * Debug formatting routine.  Returns a character string representation of the
4753 4753   * addr in buf, of the form xxx.xxx.xxx.xxx.  This routine takes the address
4754 4754   * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4755 4755   *
4756 4756   * Once the ndd table-printing interfaces are removed, this can be changed to
4757 4757   * standard dotted-decimal form.
4758 4758   */
4759 4759  char *
4760 4760  ip_dot_addr(ipaddr_t addr, char *buf)
4761 4761  {
4762 4762          uint8_t *ap = (uint8_t *)&addr;
4763 4763  
4764 4764          (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4765 4765              ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4766 4766          return (buf);
4767 4767  }
4768 4768  
4769 4769  /*
4770 4770   * Write the given MAC address as a printable string in the usual colon-
4771 4771   * separated format.
4772 4772   */
4773 4773  const char *
4774 4774  mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4775 4775  {
4776 4776          char *bp;
4777 4777  
4778 4778          if (alen == 0 || buflen < 4)
4779 4779                  return ("?");
4780 4780          bp = buf;
4781 4781          for (;;) {
4782 4782                  /*
4783 4783                   * If there are more MAC address bytes available, but we won't
4784 4784                   * have any room to print them, then add "..." to the string
4785 4785                   * instead.  See below for the 'magic number' explanation.
4786 4786                   */
4787 4787                  if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4788 4788                          (void) strcpy(bp, "...");
4789 4789                          break;
4790 4790                  }
4791 4791                  (void) sprintf(bp, "%02x", *addr++);
4792 4792                  bp += 2;
4793 4793                  if (--alen == 0)
4794 4794                          break;
4795 4795                  *bp++ = ':';
4796 4796                  buflen -= 3;
4797 4797                  /*
4798 4798                   * At this point, based on the first 'if' statement above,
4799 4799                   * either alen == 1 and buflen >= 3, or alen > 1 and
4800 4800                   * buflen >= 4.  The first case leaves room for the final "xx"
4801 4801                   * number and trailing NUL byte.  The second leaves room for at
4802 4802                   * least "...".  Thus the apparently 'magic' numbers chosen for
4803 4803                   * that statement.
4804 4804                   */
4805 4805          }
4806 4806          return (buf);
4807 4807  }
4808 4808  
4809 4809  /*
4810 4810   * Called when it is conceptually a ULP that would sent the packet
4811 4811   * e.g., port unreachable and protocol unreachable. Check that the packet
4812 4812   * would have passed the IPsec global policy before sending the error.
4813 4813   *
4814 4814   * Send an ICMP error after patching up the packet appropriately.
4815 4815   * Uses ip_drop_input and bumps the appropriate MIB.
4816 4816   */
4817 4817  void
4818 4818  ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4819 4819      ip_recv_attr_t *ira)
4820 4820  {
4821 4821          ipha_t          *ipha;
4822 4822          boolean_t       secure;
4823 4823          ill_t           *ill = ira->ira_ill;
4824 4824          ip_stack_t      *ipst = ill->ill_ipst;
4825 4825          netstack_t      *ns = ipst->ips_netstack;
4826 4826          ipsec_stack_t   *ipss = ns->netstack_ipsec;
4827 4827  
4828 4828          secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4829 4829  
4830 4830          /*
4831 4831           * We are generating an icmp error for some inbound packet.
4832 4832           * Called from all ip_fanout_(udp, tcp, proto) functions.
4833 4833           * Before we generate an error, check with global policy
4834 4834           * to see whether this is allowed to enter the system. As
4835 4835           * there is no "conn", we are checking with global policy.
4836 4836           */
4837 4837          ipha = (ipha_t *)mp->b_rptr;
4838 4838          if (secure || ipss->ipsec_inbound_v4_policy_present) {
4839 4839                  mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4840 4840                  if (mp == NULL)
4841 4841                          return;
4842 4842          }
4843 4843  
4844 4844          /* We never send errors for protocols that we do implement */
4845 4845          if (ira->ira_protocol == IPPROTO_ICMP ||
4846 4846              ira->ira_protocol == IPPROTO_IGMP) {
4847 4847                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4848 4848                  ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4849 4849                  freemsg(mp);
4850 4850                  return;
4851 4851          }
4852 4852          /*
4853 4853           * Have to correct checksum since
4854 4854           * the packet might have been
4855 4855           * fragmented and the reassembly code in ip_rput
4856 4856           * does not restore the IP checksum.
4857 4857           */
4858 4858          ipha->ipha_hdr_checksum = 0;
4859 4859          ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4860 4860  
4861 4861          switch (icmp_type) {
4862 4862          case ICMP_DEST_UNREACHABLE:
4863 4863                  switch (icmp_code) {
4864 4864                  case ICMP_PROTOCOL_UNREACHABLE:
4865 4865                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4866 4866                          ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4867 4867                          break;
4868 4868                  case ICMP_PORT_UNREACHABLE:
4869 4869                          BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4870 4870                          ip_drop_input("ipIfStatsNoPorts", mp, ill);
4871 4871                          break;
4872 4872                  }
4873 4873  
4874 4874                  icmp_unreachable(mp, icmp_code, ira);
4875 4875                  break;
4876 4876          default:
4877 4877  #ifdef DEBUG
4878 4878                  panic("ip_fanout_send_icmp_v4: wrong type");
4879 4879                  /*NOTREACHED*/
4880 4880  #else
4881 4881                  freemsg(mp);
4882 4882                  break;
4883 4883  #endif
4884 4884          }
4885 4885  }
4886 4886  
4887 4887  /*
4888 4888   * Used to send an ICMP error message when a packet is received for
4889 4889   * a protocol that is not supported. The mblk passed as argument
4890 4890   * is consumed by this function.
4891 4891   */
4892 4892  void
4893 4893  ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4894 4894  {
4895 4895          ipha_t          *ipha;
4896 4896  
4897 4897          ipha = (ipha_t *)mp->b_rptr;
4898 4898          if (ira->ira_flags & IRAF_IS_IPV4) {
4899 4899                  ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4900 4900                  ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4901 4901                      ICMP_PROTOCOL_UNREACHABLE, ira);
4902 4902          } else {
4903 4903                  ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4904 4904                  ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4905 4905                      ICMP6_PARAMPROB_NEXTHEADER, ira);
4906 4906          }
4907 4907  }
4908 4908  
4909 4909  /*
4910 4910   * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4911 4911   * Handles IPv4 and IPv6.
4912 4912   * We are responsible for disposing of mp, such as by freemsg() or putnext()
4913 4913   * Caller is responsible for dropping references to the conn.
4914 4914   */
4915 4915  void
4916 4916  ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4917 4917      ip_recv_attr_t *ira)
4918 4918  {
4919 4919          ill_t           *ill = ira->ira_ill;
4920 4920          ip_stack_t      *ipst = ill->ill_ipst;
4921 4921          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
4922 4922          boolean_t       secure;
4923 4923          uint_t          protocol = ira->ira_protocol;
4924 4924          iaflags_t       iraflags = ira->ira_flags;
4925 4925          queue_t         *rq;
4926 4926  
4927 4927          secure = iraflags & IRAF_IPSEC_SECURE;
4928 4928  
4929 4929          rq = connp->conn_rq;
4930 4930          if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4931 4931                  switch (protocol) {
4932 4932                  case IPPROTO_ICMPV6:
4933 4933                          BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4934 4934                          break;
4935 4935                  case IPPROTO_ICMP:
4936 4936                          BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4937 4937                          break;
4938 4938                  default:
4939 4939                          BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4940 4940                          break;
4941 4941                  }
4942 4942                  freemsg(mp);
4943 4943                  return;
4944 4944          }
4945 4945  
4946 4946          ASSERT(!(IPCL_IS_IPTUN(connp)));
4947 4947  
4948 4948          if (((iraflags & IRAF_IS_IPV4) ?
4949 4949              CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4950 4950              CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4951 4951              secure) {
4952 4952                  mp = ipsec_check_inbound_policy(mp, connp, ipha,
4953 4953                      ip6h, ira);
4954 4954                  if (mp == NULL) {
4955 4955                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4956 4956                          /* Note that mp is NULL */
4957 4957                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
4958 4958                          return;
4959 4959                  }
4960 4960          }
4961 4961  
4962 4962          if (iraflags & IRAF_ICMP_ERROR) {
4963 4963                  (connp->conn_recvicmp)(connp, mp, NULL, ira);
4964 4964          } else {
4965 4965                  ill_t *rill = ira->ira_rill;
4966 4966  
4967 4967                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4968 4968                  ira->ira_ill = ira->ira_rill = NULL;
4969 4969                  /* Send it upstream */
4970 4970                  (connp->conn_recv)(connp, mp, NULL, ira);
4971 4971                  ira->ira_ill = ill;
4972 4972                  ira->ira_rill = rill;
4973 4973          }
4974 4974  }
4975 4975  
4976 4976  /*
4977 4977   * Handle protocols with which IP is less intimate.  There
4978 4978   * can be more than one stream bound to a particular
4979 4979   * protocol.  When this is the case, normally each one gets a copy
4980 4980   * of any incoming packets.
4981 4981   *
4982 4982   * IPsec NOTE :
4983 4983   *
4984 4984   * Don't allow a secure packet going up a non-secure connection.
4985 4985   * We don't allow this because
4986 4986   *
4987 4987   * 1) Reply might go out in clear which will be dropped at
4988 4988   *    the sending side.
4989 4989   * 2) If the reply goes out in clear it will give the
4990 4990   *    adversary enough information for getting the key in
4991 4991   *    most of the cases.
4992 4992   *
4993 4993   * Moreover getting a secure packet when we expect clear
4994 4994   * implies that SA's were added without checking for
4995 4995   * policy on both ends. This should not happen once ISAKMP
4996 4996   * is used to negotiate SAs as SAs will be added only after
4997 4997   * verifying the policy.
4998 4998   *
4999 4999   * Zones notes:
5000 5000   * Earlier in ip_input on a system with multiple shared-IP zones we
5001 5001   * duplicate the multicast and broadcast packets and send them up
5002 5002   * with each explicit zoneid that exists on that ill.
5003 5003   * This means that here we can match the zoneid with SO_ALLZONES being special.
5004 5004   */
5005 5005  void
5006 5006  ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
5007 5007  {
5008 5008          mblk_t          *mp1;
5009 5009          ipaddr_t        laddr;
5010 5010          conn_t          *connp, *first_connp, *next_connp;
5011 5011          connf_t         *connfp;
5012 5012          ill_t           *ill = ira->ira_ill;
5013 5013          ip_stack_t      *ipst = ill->ill_ipst;
5014 5014  
5015 5015          laddr = ipha->ipha_dst;
5016 5016  
5017 5017          connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
5018 5018          mutex_enter(&connfp->connf_lock);
5019 5019          connp = connfp->connf_head;
5020 5020          for (connp = connfp->connf_head; connp != NULL;
5021 5021              connp = connp->conn_next) {
5022 5022                  /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5023 5023                  if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5024 5024                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5025 5025                      tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) {
5026 5026                          break;
5027 5027                  }
5028 5028          }
5029 5029  
5030 5030          if (connp == NULL) {
5031 5031                  /*
5032 5032                   * No one bound to these addresses.  Is
5033 5033                   * there a client that wants all
5034 5034                   * unclaimed datagrams?
5035 5035                   */
5036 5036                  mutex_exit(&connfp->connf_lock);
5037 5037                  ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
5038 5038                      ICMP_PROTOCOL_UNREACHABLE, ira);
5039 5039                  return;
5040 5040          }
5041 5041  
5042 5042          ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5043 5043  
5044 5044          CONN_INC_REF(connp);
5045 5045          first_connp = connp;
5046 5046          connp = connp->conn_next;
5047 5047  
5048 5048          for (;;) {
5049 5049                  while (connp != NULL) {
5050 5050                          /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5051 5051                          if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5052 5052                              (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5053 5053                              tsol_receive_local(mp, &laddr, IPV4_VERSION,
5054 5054                              ira, connp)))
5055 5055                                  break;
5056 5056                          connp = connp->conn_next;
5057 5057                  }
5058 5058  
5059 5059                  if (connp == NULL) {
5060 5060                          /* No more interested clients */
5061 5061                          connp = first_connp;
5062 5062                          break;
5063 5063                  }
5064 5064                  if (((mp1 = dupmsg(mp)) == NULL) &&
5065 5065                      ((mp1 = copymsg(mp)) == NULL)) {
5066 5066                          /* Memory allocation failed */
5067 5067                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5068 5068                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
5069 5069                          connp = first_connp;
5070 5070                          break;
5071 5071                  }
5072 5072  
5073 5073                  CONN_INC_REF(connp);
5074 5074                  mutex_exit(&connfp->connf_lock);
5075 5075  
5076 5076                  ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
5077 5077                      ira);
5078 5078  
5079 5079                  mutex_enter(&connfp->connf_lock);
5080 5080                  /* Follow the next pointer before releasing the conn. */
5081 5081                  next_connp = connp->conn_next;
5082 5082                  CONN_DEC_REF(connp);
5083 5083                  connp = next_connp;
5084 5084          }
5085 5085  
5086 5086          /* Last one.  Send it upstream. */
5087 5087          mutex_exit(&connfp->connf_lock);
5088 5088  
5089 5089          ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
5090 5090  
5091 5091          CONN_DEC_REF(connp);
5092 5092  }
5093 5093  
5094 5094  /*
5095 5095   * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
5096 5096   * pass it along to ESP if the SPI is non-zero.  Returns the mblk if the mblk
5097 5097   * is not consumed.
5098 5098   *
5099 5099   * One of three things can happen, all of which affect the passed-in mblk:
5100 5100   *
5101 5101   * 1.) The packet is stock UDP and gets its zero-SPI stripped.  Return mblk..
5102 5102   *
5103 5103   * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
5104 5104   *     ESP packet, and is passed along to ESP for consumption.  Return NULL.
5105 5105   *
5106 5106   * 3.) The packet is an ESP-in-UDP Keepalive.  Drop it and return NULL.
5107 5107   */
5108 5108  mblk_t *
5109 5109  zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
5110 5110  {
5111 5111          int shift, plen, iph_len;
5112 5112          ipha_t *ipha;
5113 5113          udpha_t *udpha;
5114 5114          uint32_t *spi;
5115 5115          uint32_t esp_ports;
5116 5116          uint8_t *orptr;
5117 5117          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
5118 5118          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
5119 5119  
5120 5120          ipha = (ipha_t *)mp->b_rptr;
5121 5121          iph_len = ira->ira_ip_hdr_length;
5122 5122          plen = ira->ira_pktlen;
5123 5123  
5124 5124          if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
5125 5125                  /*
5126 5126                   * Most likely a keepalive for the benefit of an intervening
5127 5127                   * NAT.  These aren't for us, per se, so drop it.
5128 5128                   *
5129 5129                   * RFC 3947/8 doesn't say for sure what to do for 2-3
5130 5130                   * byte packets (keepalives are 1-byte), but we'll drop them
5131 5131                   * also.
5132 5132                   */
5133 5133                  ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5134 5134                      DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
5135 5135                  return (NULL);
5136 5136          }
5137 5137  
5138 5138          if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
5139 5139                  /* might as well pull it all up - it might be ESP. */
5140 5140                  if (!pullupmsg(mp, -1)) {
5141 5141                          ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5142 5142                              DROPPER(ipss, ipds_esp_nomem),
5143 5143                              &ipss->ipsec_dropper);
5144 5144                          return (NULL);
5145 5145                  }
5146 5146  
5147 5147                  ipha = (ipha_t *)mp->b_rptr;
5148 5148          }
5149 5149          spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
5150 5150          if (*spi == 0) {
5151 5151                  /* UDP packet - remove 0-spi. */
5152 5152                  shift = sizeof (uint32_t);
5153 5153          } else {
5154 5154                  /* ESP-in-UDP packet - reduce to ESP. */
5155 5155                  ipha->ipha_protocol = IPPROTO_ESP;
5156 5156                  shift = sizeof (udpha_t);
5157 5157          }
5158 5158  
5159 5159          /* Fix IP header */
5160 5160          ira->ira_pktlen = (plen - shift);
5161 5161          ipha->ipha_length = htons(ira->ira_pktlen);
5162 5162          ipha->ipha_hdr_checksum = 0;
5163 5163  
5164 5164          orptr = mp->b_rptr;
5165 5165          mp->b_rptr += shift;
5166 5166  
5167 5167          udpha = (udpha_t *)(orptr + iph_len);
5168 5168          if (*spi == 0) {
5169 5169                  ASSERT((uint8_t *)ipha == orptr);
5170 5170                  udpha->uha_length = htons(plen - shift - iph_len);
5171 5171                  iph_len += sizeof (udpha_t);    /* For the call to ovbcopy(). */
5172 5172                  esp_ports = 0;
5173 5173          } else {
5174 5174                  esp_ports = *((uint32_t *)udpha);
5175 5175                  ASSERT(esp_ports != 0);
5176 5176          }
5177 5177          ovbcopy(orptr, orptr + shift, iph_len);
5178 5178          if (esp_ports != 0) /* Punt up for ESP processing. */ {
5179 5179                  ipha = (ipha_t *)(orptr + shift);
5180 5180  
5181 5181                  ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5182 5182                  ira->ira_esp_udp_ports = esp_ports;
5183 5183                  ip_fanout_v4(mp, ipha, ira);
5184 5184                  return (NULL);
5185 5185          }
5186 5186          return (mp);
5187 5187  }
5188 5188  
5189 5189  /*
5190 5190   * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5191 5191   * Handles IPv4 and IPv6.
5192 5192   * We are responsible for disposing of mp, such as by freemsg() or putnext()
5193 5193   * Caller is responsible for dropping references to the conn.
5194 5194   */
5195 5195  void
5196 5196  ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5197 5197      ip_recv_attr_t *ira)
5198 5198  {
5199 5199          ill_t           *ill = ira->ira_ill;
5200 5200          ip_stack_t      *ipst = ill->ill_ipst;
5201 5201          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
5202 5202          boolean_t       secure;
5203 5203          iaflags_t       iraflags = ira->ira_flags;
5204 5204  
5205 5205          secure = iraflags & IRAF_IPSEC_SECURE;
5206 5206  
5207 5207          if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5208 5208              !canputnext(connp->conn_rq)) {
5209 5209                  BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5210 5210                  freemsg(mp);
5211 5211                  return;
5212 5212          }
5213 5213  
5214 5214          if (((iraflags & IRAF_IS_IPV4) ?
5215 5215              CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5216 5216              CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5217 5217              secure) {
5218 5218                  mp = ipsec_check_inbound_policy(mp, connp, ipha,
5219 5219                      ip6h, ira);
5220 5220                  if (mp == NULL) {
5221 5221                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5222 5222                          /* Note that mp is NULL */
5223 5223                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
5224 5224                          return;
5225 5225                  }
5226 5226          }
5227 5227  
5228 5228          /*
5229 5229           * Since this code is not used for UDP unicast we don't need a NAT_T
5230 5230           * check. Only ip_fanout_v4 has that check.
5231 5231           */
5232 5232          if (ira->ira_flags & IRAF_ICMP_ERROR) {
5233 5233                  (connp->conn_recvicmp)(connp, mp, NULL, ira);
5234 5234          } else {
5235 5235                  ill_t *rill = ira->ira_rill;
5236 5236  
5237 5237                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5238 5238                  ira->ira_ill = ira->ira_rill = NULL;
5239 5239                  /* Send it upstream */
5240 5240                  (connp->conn_recv)(connp, mp, NULL, ira);
5241 5241                  ira->ira_ill = ill;
5242 5242                  ira->ira_rill = rill;
5243 5243          }
5244 5244  }
5245 5245  
5246 5246  /*
5247 5247   * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5248 5248   * (Unicast fanout is handled in ip_input_v4.)
5249 5249   *
5250 5250   * If SO_REUSEADDR is set all multicast and broadcast packets
5251 5251   * will be delivered to all conns bound to the same port.
5252 5252   *
5253 5253   * If there is at least one matching AF_INET receiver, then we will
5254 5254   * ignore any AF_INET6 receivers.
5255 5255   * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5256 5256   * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5257 5257   * packets.
5258 5258   *
5259 5259   * Zones notes:
5260 5260   * Earlier in ip_input on a system with multiple shared-IP zones we
5261 5261   * duplicate the multicast and broadcast packets and send them up
5262 5262   * with each explicit zoneid that exists on that ill.
5263 5263   * This means that here we can match the zoneid with SO_ALLZONES being special.
5264 5264   */
5265 5265  void
5266 5266  ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5267 5267      ip_recv_attr_t *ira)
5268 5268  {
5269 5269          ipaddr_t        laddr;
5270 5270          in6_addr_t      v6faddr;
5271 5271          conn_t          *connp;
5272 5272          connf_t         *connfp;
5273 5273          ipaddr_t        faddr;
5274 5274          ill_t           *ill = ira->ira_ill;
5275 5275          ip_stack_t      *ipst = ill->ill_ipst;
5276 5276  
5277 5277          ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5278 5278  
5279 5279          laddr = ipha->ipha_dst;
5280 5280          faddr = ipha->ipha_src;
5281 5281  
5282 5282          connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5283 5283          mutex_enter(&connfp->connf_lock);
5284 5284          connp = connfp->connf_head;
5285 5285  
5286 5286          /*
5287 5287           * If SO_REUSEADDR has been set on the first we send the
5288 5288           * packet to all clients that have joined the group and
5289 5289           * match the port.
5290 5290           */
5291 5291          while (connp != NULL) {
5292 5292                  if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5293 5293                      conn_wantpacket(connp, ira, ipha) &&
5294 5294                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5295 5295                      tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5296 5296                          break;
5297 5297                  connp = connp->conn_next;
5298 5298          }
5299 5299  
5300 5300          if (connp == NULL)
5301 5301                  goto notfound;
5302 5302  
5303 5303          CONN_INC_REF(connp);
5304 5304  
5305 5305          if (connp->conn_reuseaddr) {
5306 5306                  conn_t          *first_connp = connp;
5307 5307                  conn_t          *next_connp;
5308 5308                  mblk_t          *mp1;
5309 5309  
5310 5310                  connp = connp->conn_next;
5311 5311                  for (;;) {
5312 5312                          while (connp != NULL) {
5313 5313                                  if (IPCL_UDP_MATCH(connp, lport, laddr,
5314 5314                                      fport, faddr) &&
5315 5315                                      conn_wantpacket(connp, ira, ipha) &&
5316 5316                                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5317 5317                                      tsol_receive_local(mp, &laddr, IPV4_VERSION,
5318 5318                                      ira, connp)))
5319 5319                                          break;
5320 5320                                  connp = connp->conn_next;
5321 5321                          }
5322 5322                          if (connp == NULL) {
5323 5323                                  /* No more interested clients */
5324 5324                                  connp = first_connp;
5325 5325                                  break;
5326 5326                          }
5327 5327                          if (((mp1 = dupmsg(mp)) == NULL) &&
5328 5328                              ((mp1 = copymsg(mp)) == NULL)) {
5329 5329                                  /* Memory allocation failed */
5330 5330                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5331 5331                                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
5332 5332                                  connp = first_connp;
5333 5333                                  break;
5334 5334                          }
5335 5335                          CONN_INC_REF(connp);
5336 5336                          mutex_exit(&connfp->connf_lock);
5337 5337  
5338 5338                          IP_STAT(ipst, ip_udp_fanmb);
5339 5339                          ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5340 5340                              NULL, ira);
5341 5341                          mutex_enter(&connfp->connf_lock);
5342 5342                          /* Follow the next pointer before releasing the conn */
5343 5343                          next_connp = connp->conn_next;
5344 5344                          CONN_DEC_REF(connp);
5345 5345                          connp = next_connp;
5346 5346                  }
5347 5347          }
5348 5348  
5349 5349          /* Last one.  Send it upstream. */
5350 5350          mutex_exit(&connfp->connf_lock);
5351 5351          IP_STAT(ipst, ip_udp_fanmb);
5352 5352          ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5353 5353          CONN_DEC_REF(connp);
5354 5354          return;
5355 5355  
5356 5356  notfound:
5357 5357          mutex_exit(&connfp->connf_lock);
5358 5358          /*
5359 5359           * IPv6 endpoints bound to multicast IPv4-mapped addresses
5360 5360           * have already been matched above, since they live in the IPv4
5361 5361           * fanout tables. This implies we only need to
5362 5362           * check for IPv6 in6addr_any endpoints here.
5363 5363           * Thus we compare using ipv6_all_zeros instead of the destination
5364 5364           * address, except for the multicast group membership lookup which
5365 5365           * uses the IPv4 destination.
5366 5366           */
5367 5367          IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5368 5368          connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5369 5369          mutex_enter(&connfp->connf_lock);
5370 5370          connp = connfp->connf_head;
5371 5371          /*
5372 5372           * IPv4 multicast packet being delivered to an AF_INET6
5373 5373           * in6addr_any endpoint.
5374 5374           * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5375 5375           * and not conn_wantpacket_v6() since any multicast membership is
5376 5376           * for an IPv4-mapped multicast address.
5377 5377           */
5378 5378          while (connp != NULL) {
5379 5379                  if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5380 5380                      fport, v6faddr) &&
5381 5381                      conn_wantpacket(connp, ira, ipha) &&
5382 5382                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5383 5383                      tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5384 5384                          break;
5385 5385                  connp = connp->conn_next;
5386 5386          }
5387 5387  
5388 5388          if (connp == NULL) {
5389 5389                  /*
5390 5390                   * No one bound to this port.  Is
5391 5391                   * there a client that wants all
5392 5392                   * unclaimed datagrams?
5393 5393                   */
5394 5394                  mutex_exit(&connfp->connf_lock);
5395 5395  
5396 5396                  if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5397 5397                      NULL) {
5398 5398                          ASSERT(ira->ira_protocol == IPPROTO_UDP);
5399 5399                          ip_fanout_proto_v4(mp, ipha, ira);
5400 5400                  } else {
5401 5401                          /*
5402 5402                           * We used to attempt to send an icmp error here, but
5403 5403                           * since this is known to be a multicast packet
5404 5404                           * and we don't send icmp errors in response to
5405 5405                           * multicast, just drop the packet and give up sooner.
5406 5406                           */
5407 5407                          BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5408 5408                          freemsg(mp);
5409 5409                  }
5410 5410                  return;
5411 5411          }
5412 5412          CONN_INC_REF(connp);
5413 5413          ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5414 5414  
5415 5415          /*
5416 5416           * If SO_REUSEADDR has been set on the first we send the
5417 5417           * packet to all clients that have joined the group and
5418 5418           * match the port.
5419 5419           */
5420 5420          if (connp->conn_reuseaddr) {
5421 5421                  conn_t          *first_connp = connp;
5422 5422                  conn_t          *next_connp;
5423 5423                  mblk_t          *mp1;
5424 5424  
5425 5425                  connp = connp->conn_next;
5426 5426                  for (;;) {
5427 5427                          while (connp != NULL) {
5428 5428                                  if (IPCL_UDP_MATCH_V6(connp, lport,
5429 5429                                      ipv6_all_zeros, fport, v6faddr) &&
5430 5430                                      conn_wantpacket(connp, ira, ipha) &&
5431 5431                                      (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5432 5432                                      tsol_receive_local(mp, &laddr, IPV4_VERSION,
5433 5433                                      ira, connp)))
5434 5434                                          break;
5435 5435                                  connp = connp->conn_next;
5436 5436                          }
5437 5437                          if (connp == NULL) {
5438 5438                                  /* No more interested clients */
5439 5439                                  connp = first_connp;
5440 5440                                  break;
5441 5441                          }
5442 5442                          if (((mp1 = dupmsg(mp)) == NULL) &&
5443 5443                              ((mp1 = copymsg(mp)) == NULL)) {
5444 5444                                  /* Memory allocation failed */
5445 5445                                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5446 5446                                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
5447 5447                                  connp = first_connp;
5448 5448                                  break;
5449 5449                          }
5450 5450                          CONN_INC_REF(connp);
5451 5451                          mutex_exit(&connfp->connf_lock);
5452 5452  
5453 5453                          IP_STAT(ipst, ip_udp_fanmb);
5454 5454                          ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5455 5455                              NULL, ira);
5456 5456                          mutex_enter(&connfp->connf_lock);
5457 5457                          /* Follow the next pointer before releasing the conn */
5458 5458                          next_connp = connp->conn_next;
5459 5459                          CONN_DEC_REF(connp);
5460 5460                          connp = next_connp;
5461 5461                  }
5462 5462          }
5463 5463  
5464 5464          /* Last one.  Send it upstream. */
5465 5465          mutex_exit(&connfp->connf_lock);
5466 5466          IP_STAT(ipst, ip_udp_fanmb);
5467 5467          ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5468 5468          CONN_DEC_REF(connp);
5469 5469  }
5470 5470  
5471 5471  /*
5472 5472   * Split an incoming packet's IPv4 options into the label and the other options.
5473 5473   * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5474 5474   * clearing out any leftover label or options.
5475 5475   * Otherwise it just makes ipp point into the packet.
5476 5476   *
5477 5477   * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5478 5478   */
5479 5479  int
5480 5480  ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5481 5481  {
5482 5482          uchar_t         *opt;
5483 5483          uint32_t        totallen;
5484 5484          uint32_t        optval;
5485 5485          uint32_t        optlen;
5486 5486  
5487 5487          ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5488 5488          ipp->ipp_hoplimit = ipha->ipha_ttl;
5489 5489          ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5490 5490          IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5491 5491  
5492 5492          /*
5493 5493           * Get length (in 4 byte octets) of IP header options.
5494 5494           */
5495 5495          totallen = ipha->ipha_version_and_hdr_length -
5496 5496              (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5497 5497  
5498 5498          if (totallen == 0) {
5499 5499                  if (!allocate)
5500 5500                          return (0);
5501 5501  
5502 5502                  /* Clear out anything from a previous packet */
5503 5503                  if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5504 5504                          kmem_free(ipp->ipp_ipv4_options,
5505 5505                              ipp->ipp_ipv4_options_len);
5506 5506                          ipp->ipp_ipv4_options = NULL;
5507 5507                          ipp->ipp_ipv4_options_len = 0;
5508 5508                          ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5509 5509                  }
5510 5510                  if (ipp->ipp_fields & IPPF_LABEL_V4) {
5511 5511                          kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5512 5512                          ipp->ipp_label_v4 = NULL;
5513 5513                          ipp->ipp_label_len_v4 = 0;
5514 5514                          ipp->ipp_fields &= ~IPPF_LABEL_V4;
5515 5515                  }
5516 5516                  return (0);
5517 5517          }
5518 5518  
5519 5519          totallen <<= 2;
5520 5520          opt = (uchar_t *)&ipha[1];
5521 5521          if (!is_system_labeled()) {
5522 5522  
5523 5523          copyall:
5524 5524                  if (!allocate) {
5525 5525                          if (totallen != 0) {
5526 5526                                  ipp->ipp_ipv4_options = opt;
5527 5527                                  ipp->ipp_ipv4_options_len = totallen;
5528 5528                                  ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5529 5529                          }
5530 5530                          return (0);
5531 5531                  }
5532 5532                  /* Just copy all of options */
5533 5533                  if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5534 5534                          if (totallen == ipp->ipp_ipv4_options_len) {
5535 5535                                  bcopy(opt, ipp->ipp_ipv4_options, totallen);
5536 5536                                  return (0);
5537 5537                          }
5538 5538                          kmem_free(ipp->ipp_ipv4_options,
5539 5539                              ipp->ipp_ipv4_options_len);
5540 5540                          ipp->ipp_ipv4_options = NULL;
5541 5541                          ipp->ipp_ipv4_options_len = 0;
5542 5542                          ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5543 5543                  }
5544 5544                  if (totallen == 0)
5545 5545                          return (0);
5546 5546  
5547 5547                  ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5548 5548                  if (ipp->ipp_ipv4_options == NULL)
5549 5549                          return (ENOMEM);
5550 5550                  ipp->ipp_ipv4_options_len = totallen;
5551 5551                  ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5552 5552                  bcopy(opt, ipp->ipp_ipv4_options, totallen);
5553 5553                  return (0);
5554 5554          }
5555 5555  
5556 5556          if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) {
5557 5557                  kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5558 5558                  ipp->ipp_label_v4 = NULL;
5559 5559                  ipp->ipp_label_len_v4 = 0;
5560 5560                  ipp->ipp_fields &= ~IPPF_LABEL_V4;
5561 5561          }
5562 5562  
5563 5563          /*
5564 5564           * Search for CIPSO option.
5565 5565           * We assume CIPSO is first in options if it is present.
5566 5566           * If it isn't, then ipp_opt_ipv4_options will not include the options
5567 5567           * prior to the CIPSO option.
5568 5568           */
5569 5569          while (totallen != 0) {
5570 5570                  switch (optval = opt[IPOPT_OPTVAL]) {
5571 5571                  case IPOPT_EOL:
5572 5572                          return (0);
5573 5573                  case IPOPT_NOP:
5574 5574                          optlen = 1;
5575 5575                          break;
5576 5576                  default:
5577 5577                          if (totallen <= IPOPT_OLEN)
5578 5578                                  return (EINVAL);
5579 5579                          optlen = opt[IPOPT_OLEN];
5580 5580                          if (optlen < 2)
5581 5581                                  return (EINVAL);
5582 5582                  }
5583 5583                  if (optlen > totallen)
5584 5584                          return (EINVAL);
5585 5585  
5586 5586                  switch (optval) {
5587 5587                  case IPOPT_COMSEC:
5588 5588                          if (!allocate) {
5589 5589                                  ipp->ipp_label_v4 = opt;
5590 5590                                  ipp->ipp_label_len_v4 = optlen;
5591 5591                                  ipp->ipp_fields |= IPPF_LABEL_V4;
5592 5592                          } else {
5593 5593                                  ipp->ipp_label_v4 = kmem_alloc(optlen,
5594 5594                                      KM_NOSLEEP);
5595 5595                                  if (ipp->ipp_label_v4 == NULL)
5596 5596                                          return (ENOMEM);
5597 5597                                  ipp->ipp_label_len_v4 = optlen;
5598 5598                                  ipp->ipp_fields |= IPPF_LABEL_V4;
5599 5599                                  bcopy(opt, ipp->ipp_label_v4, optlen);
5600 5600                          }
5601 5601                          totallen -= optlen;
5602 5602                          opt += optlen;
5603 5603  
5604 5604                          /* Skip padding bytes until we get to a multiple of 4 */
5605 5605                          while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) {
5606 5606                                  totallen--;
5607 5607                                  opt++;
5608 5608                          }
5609 5609                          /* Remaining as ipp_ipv4_options */
5610 5610                          goto copyall;
5611 5611                  }
5612 5612                  totallen -= optlen;
5613 5613                  opt += optlen;
5614 5614          }
5615 5615          /* No CIPSO found; return everything as ipp_ipv4_options */
5616 5616          totallen = ipha->ipha_version_and_hdr_length -
5617 5617              (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5618 5618          totallen <<= 2;
5619 5619          opt = (uchar_t *)&ipha[1];
5620 5620          goto copyall;
5621 5621  }
5622 5622  
5623 5623  /*
5624 5624   * Efficient versions of lookup for an IRE when we only
5625 5625   * match the address.
5626 5626   * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5627 5627   * Does not handle multicast addresses.
5628 5628   */
5629 5629  uint_t
5630 5630  ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5631 5631  {
5632 5632          ire_t *ire;
5633 5633          uint_t result;
5634 5634  
5635 5635          ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5636 5636          ASSERT(ire != NULL);
5637 5637          if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5638 5638                  result = IRE_NOROUTE;
5639 5639          else
5640 5640                  result = ire->ire_type;
5641 5641          ire_refrele(ire);
5642 5642          return (result);
5643 5643  }
5644 5644  
5645 5645  /*
5646 5646   * Efficient versions of lookup for an IRE when we only
5647 5647   * match the address.
5648 5648   * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5649 5649   * Does not handle multicast addresses.
5650 5650   */
5651 5651  uint_t
5652 5652  ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5653 5653  {
5654 5654          ire_t *ire;
5655 5655          uint_t result;
5656 5656  
5657 5657          ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5658 5658          ASSERT(ire != NULL);
5659 5659          if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5660 5660                  result = IRE_NOROUTE;
5661 5661          else
5662 5662                  result = ire->ire_type;
5663 5663          ire_refrele(ire);
5664 5664          return (result);
5665 5665  }
5666 5666  
5667 5667  /*
5668 5668   * Nobody should be sending
5669 5669   * packets up this stream
5670 5670   */
5671 5671  static int
5672 5672  ip_lrput(queue_t *q, mblk_t *mp)
5673 5673  {
5674 5674          switch (mp->b_datap->db_type) {
5675 5675          case M_FLUSH:
5676 5676                  /* Turn around */
5677 5677                  if (*mp->b_rptr & FLUSHW) {
5678 5678                          *mp->b_rptr &= ~FLUSHR;
5679 5679                          qreply(q, mp);
5680 5680                          return (0);
5681 5681                  }
5682 5682                  break;
5683 5683          }
5684 5684          freemsg(mp);
5685 5685          return (0);
5686 5686  }
5687 5687  
5688 5688  /* Nobody should be sending packets down this stream */
5689 5689  /* ARGSUSED */
5690 5690  int
5691 5691  ip_lwput(queue_t *q, mblk_t *mp)
5692 5692  {
5693 5693          freemsg(mp);
5694 5694          return (0);
5695 5695  }
5696 5696  
5697 5697  /*
5698 5698   * Move the first hop in any source route to ipha_dst and remove that part of
5699 5699   * the source route.  Called by other protocols.  Errors in option formatting
5700 5700   * are ignored - will be handled by ip_output_options. Return the final
5701 5701   * destination (either ipha_dst or the last entry in a source route.)
5702 5702   */
5703 5703  ipaddr_t
5704 5704  ip_massage_options(ipha_t *ipha, netstack_t *ns)
5705 5705  {
5706 5706          ipoptp_t        opts;
5707 5707          uchar_t         *opt;
5708 5708          uint8_t         optval;
5709 5709          uint8_t         optlen;
5710 5710          ipaddr_t        dst;
5711 5711          int             i;
5712 5712          ip_stack_t      *ipst = ns->netstack_ip;
5713 5713  
5714 5714          ip2dbg(("ip_massage_options\n"));
5715 5715          dst = ipha->ipha_dst;
5716 5716          for (optval = ipoptp_first(&opts, ipha);
5717 5717              optval != IPOPT_EOL;
5718 5718              optval = ipoptp_next(&opts)) {
5719 5719                  opt = opts.ipoptp_cur;
5720 5720                  switch (optval) {
5721 5721                          uint8_t off;
5722 5722                  case IPOPT_SSRR:
5723 5723                  case IPOPT_LSRR:
5724 5724                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5725 5725                                  ip1dbg(("ip_massage_options: bad src route\n"));
5726 5726                                  break;
5727 5727                          }
5728 5728                          optlen = opts.ipoptp_len;
5729 5729                          off = opt[IPOPT_OFFSET];
5730 5730                          off--;
5731 5731                  redo_srr:
5732 5732                          if (optlen < IP_ADDR_LEN ||
5733 5733                              off > optlen - IP_ADDR_LEN) {
5734 5734                                  /* End of source route */
5735 5735                                  ip1dbg(("ip_massage_options: end of SR\n"));
5736 5736                                  break;
5737 5737                          }
5738 5738                          bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5739 5739                          ip1dbg(("ip_massage_options: next hop 0x%x\n",
5740 5740                              ntohl(dst)));
5741 5741                          /*
5742 5742                           * Check if our address is present more than
5743 5743                           * once as consecutive hops in source route.
5744 5744                           * XXX verify per-interface ip_forwarding
5745 5745                           * for source route?
5746 5746                           */
5747 5747                          if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5748 5748                                  off += IP_ADDR_LEN;
5749 5749                                  goto redo_srr;
5750 5750                          }
5751 5751                          if (dst == htonl(INADDR_LOOPBACK)) {
5752 5752                                  ip1dbg(("ip_massage_options: loopback addr in "
5753 5753                                      "source route!\n"));
5754 5754                                  break;
5755 5755                          }
5756 5756                          /*
5757 5757                           * Update ipha_dst to be the first hop and remove the
5758 5758                           * first hop from the source route (by overwriting
5759 5759                           * part of the option with NOP options).
5760 5760                           */
5761 5761                          ipha->ipha_dst = dst;
5762 5762                          /* Put the last entry in dst */
5763 5763                          off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5764 5764                              3;
5765 5765                          bcopy(&opt[off], &dst, IP_ADDR_LEN);
5766 5766  
5767 5767                          ip1dbg(("ip_massage_options: last hop 0x%x\n",
5768 5768                              ntohl(dst)));
5769 5769                          /* Move down and overwrite */
5770 5770                          opt[IP_ADDR_LEN] = opt[0];
5771 5771                          opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5772 5772                          opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5773 5773                          for (i = 0; i < IP_ADDR_LEN; i++)
5774 5774                                  opt[i] = IPOPT_NOP;
5775 5775                          break;
5776 5776                  }
5777 5777          }
5778 5778          return (dst);
5779 5779  }
5780 5780  
5781 5781  /*
5782 5782   * Return the network mask
5783 5783   * associated with the specified address.
5784 5784   */
5785 5785  ipaddr_t
5786 5786  ip_net_mask(ipaddr_t addr)
5787 5787  {
5788 5788          uchar_t *up = (uchar_t *)&addr;
5789 5789          ipaddr_t mask = 0;
5790 5790          uchar_t *maskp = (uchar_t *)&mask;
5791 5791  
5792 5792  #if defined(__i386) || defined(__amd64)
5793 5793  #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5794 5794  #endif
5795 5795  #ifdef  TOTALLY_BRAIN_DAMAGED_C_COMPILER
5796 5796          maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5797 5797  #endif
5798 5798          if (CLASSD(addr)) {
5799 5799                  maskp[0] = 0xF0;
5800 5800                  return (mask);
5801 5801          }
5802 5802  
5803 5803          /* We assume Class E default netmask to be 32 */
5804 5804          if (CLASSE(addr))
5805 5805                  return (0xffffffffU);
5806 5806  
5807 5807          if (addr == 0)
5808 5808                  return (0);
5809 5809          maskp[0] = 0xFF;
5810 5810          if ((up[0] & 0x80) == 0)
5811 5811                  return (mask);
5812 5812  
5813 5813          maskp[1] = 0xFF;
5814 5814          if ((up[0] & 0xC0) == 0x80)
5815 5815                  return (mask);
5816 5816  
5817 5817          maskp[2] = 0xFF;
5818 5818          if ((up[0] & 0xE0) == 0xC0)
5819 5819                  return (mask);
5820 5820  
5821 5821          /* Otherwise return no mask */
5822 5822          return ((ipaddr_t)0);
5823 5823  }
5824 5824  
5825 5825  /* Name/Value Table Lookup Routine */
5826 5826  char *
5827 5827  ip_nv_lookup(nv_t *nv, int value)
5828 5828  {
5829 5829          if (!nv)
5830 5830                  return (NULL);
5831 5831          for (; nv->nv_name; nv++) {
5832 5832                  if (nv->nv_value == value)
5833 5833                          return (nv->nv_name);
5834 5834          }
5835 5835          return ("unknown");
5836 5836  }
5837 5837  
5838 5838  static int
5839 5839  ip_wait_for_info_ack(ill_t *ill)
5840 5840  {
5841 5841          int err;
5842 5842  
5843 5843          mutex_enter(&ill->ill_lock);
5844 5844          while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5845 5845                  /*
5846 5846                   * Return value of 0 indicates a pending signal.
5847 5847                   */
5848 5848                  err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5849 5849                  if (err == 0) {
5850 5850                          mutex_exit(&ill->ill_lock);
5851 5851                          return (EINTR);
5852 5852                  }
5853 5853          }
5854 5854          mutex_exit(&ill->ill_lock);
5855 5855          /*
5856 5856           * ip_rput_other could have set an error  in ill_error on
5857 5857           * receipt of M_ERROR.
5858 5858           */
5859 5859          return (ill->ill_error);
5860 5860  }
5861 5861  
5862 5862  /*
5863 5863   * This is a module open, i.e. this is a control stream for access
5864 5864   * to a DLPI device.  We allocate an ill_t as the instance data in
5865 5865   * this case.
5866 5866   */
5867 5867  static int
5868 5868  ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5869 5869  {
5870 5870          ill_t   *ill;
5871 5871          int     err;
5872 5872          zoneid_t zoneid;
5873 5873          netstack_t *ns;
5874 5874          ip_stack_t *ipst;
5875 5875  
5876 5876          /*
5877 5877           * Prevent unprivileged processes from pushing IP so that
5878 5878           * they can't send raw IP.
5879 5879           */
5880 5880          if (secpolicy_net_rawaccess(credp) != 0)
5881 5881                  return (EPERM);
5882 5882  
5883 5883          ns = netstack_find_by_cred(credp);
5884 5884          ASSERT(ns != NULL);
5885 5885          ipst = ns->netstack_ip;
5886 5886          ASSERT(ipst != NULL);
5887 5887  
5888 5888          /*
5889 5889           * For exclusive stacks we set the zoneid to zero
5890 5890           * to make IP operate as if in the global zone.
5891 5891           */
5892 5892          if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5893 5893                  zoneid = GLOBAL_ZONEID;
5894 5894          else
5895 5895                  zoneid = crgetzoneid(credp);
5896 5896  
5897 5897          ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5898 5898          q->q_ptr = WR(q)->q_ptr = ill;
5899 5899          ill->ill_ipst = ipst;
5900 5900          ill->ill_zoneid = zoneid;
5901 5901  
5902 5902          /*
5903 5903           * ill_init initializes the ill fields and then sends down
5904 5904           * down a DL_INFO_REQ after calling qprocson.
5905 5905           */
5906 5906          err = ill_init(q, ill);
5907 5907  
5908 5908          if (err != 0) {
5909 5909                  mi_free(ill);
5910 5910                  netstack_rele(ipst->ips_netstack);
5911 5911                  q->q_ptr = NULL;
5912 5912                  WR(q)->q_ptr = NULL;
5913 5913                  return (err);
5914 5914          }
5915 5915  
5916 5916          /*
5917 5917           * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5918 5918           *
5919 5919           * ill_init initializes the ipsq marking this thread as
5920 5920           * writer
5921 5921           */
5922 5922          ipsq_exit(ill->ill_phyint->phyint_ipsq);
5923 5923          err = ip_wait_for_info_ack(ill);
5924 5924          if (err == 0)
5925 5925                  ill->ill_credp = credp;
5926 5926          else
5927 5927                  goto fail;
5928 5928  
5929 5929          crhold(credp);
5930 5930  
5931 5931          mutex_enter(&ipst->ips_ip_mi_lock);
5932 5932          err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5933 5933              sflag, credp);
5934 5934          mutex_exit(&ipst->ips_ip_mi_lock);
5935 5935  fail:
5936 5936          if (err) {
5937 5937                  (void) ip_close(q, 0, credp);
5938 5938                  return (err);
5939 5939          }
5940 5940          return (0);
5941 5941  }
5942 5942  
5943 5943  /* For /dev/ip aka AF_INET open */
5944 5944  int
5945 5945  ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5946 5946  {
5947 5947          return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5948 5948  }
5949 5949  
5950 5950  /* For /dev/ip6 aka AF_INET6 open */
5951 5951  int
5952 5952  ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5953 5953  {
5954 5954          return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5955 5955  }
5956 5956  
5957 5957  /* IP open routine. */
5958 5958  int
5959 5959  ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5960 5960      boolean_t isv6)
5961 5961  {
5962 5962          conn_t          *connp;
5963 5963          major_t         maj;
5964 5964          zoneid_t        zoneid;
5965 5965          netstack_t      *ns;
5966 5966          ip_stack_t      *ipst;
5967 5967  
5968 5968          /* Allow reopen. */
5969 5969          if (q->q_ptr != NULL)
5970 5970                  return (0);
5971 5971  
5972 5972          if (sflag & MODOPEN) {
5973 5973                  /* This is a module open */
5974 5974                  return (ip_modopen(q, devp, flag, sflag, credp));
5975 5975          }
5976 5976  
5977 5977          if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5978 5978                  /*
5979 5979                   * Non streams based socket looking for a stream
5980 5980                   * to access IP
5981 5981                   */
5982 5982                  return (ip_helper_stream_setup(q, devp, flag, sflag,
5983 5983                      credp, isv6));
5984 5984          }
5985 5985  
5986 5986          ns = netstack_find_by_cred(credp);
5987 5987          ASSERT(ns != NULL);
5988 5988          ipst = ns->netstack_ip;
5989 5989          ASSERT(ipst != NULL);
5990 5990  
5991 5991          /*
5992 5992           * For exclusive stacks we set the zoneid to zero
5993 5993           * to make IP operate as if in the global zone.
5994 5994           */
5995 5995          if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5996 5996                  zoneid = GLOBAL_ZONEID;
5997 5997          else
5998 5998                  zoneid = crgetzoneid(credp);
5999 5999  
6000 6000          /*
6001 6001           * We are opening as a device. This is an IP client stream, and we
6002 6002           * allocate an conn_t as the instance data.
6003 6003           */
6004 6004          connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
6005 6005  
6006 6006          /*
6007 6007           * ipcl_conn_create did a netstack_hold. Undo the hold that was
6008 6008           * done by netstack_find_by_cred()
6009 6009           */
6010 6010          netstack_rele(ipst->ips_netstack);
6011 6011  
6012 6012          connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
6013 6013          /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
6014 6014          connp->conn_ixa->ixa_zoneid = zoneid;
6015 6015          connp->conn_zoneid = zoneid;
6016 6016  
6017 6017          connp->conn_rq = q;
6018 6018          q->q_ptr = WR(q)->q_ptr = connp;
6019 6019  
6020 6020          /* Minor tells us which /dev entry was opened */
6021 6021          if (isv6) {
6022 6022                  connp->conn_family = AF_INET6;
6023 6023                  connp->conn_ipversion = IPV6_VERSION;
6024 6024                  connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
6025 6025                  connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
6026 6026          } else {
6027 6027                  connp->conn_family = AF_INET;
6028 6028                  connp->conn_ipversion = IPV4_VERSION;
6029 6029                  connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
6030 6030          }
6031 6031  
6032 6032          if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
6033 6033              ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
6034 6034                  connp->conn_minor_arena = ip_minor_arena_la;
6035 6035          } else {
6036 6036                  /*
6037 6037                   * Either minor numbers in the large arena were exhausted
6038 6038                   * or a non socket application is doing the open.
6039 6039                   * Try to allocate from the small arena.
6040 6040                   */
6041 6041                  if ((connp->conn_dev =
6042 6042                      inet_minor_alloc(ip_minor_arena_sa)) == 0) {
6043 6043                          /* CONN_DEC_REF takes care of netstack_rele() */
6044 6044                          q->q_ptr = WR(q)->q_ptr = NULL;
6045 6045                          CONN_DEC_REF(connp);
6046 6046                          return (EBUSY);
6047 6047                  }
6048 6048                  connp->conn_minor_arena = ip_minor_arena_sa;
6049 6049          }
6050 6050  
6051 6051          maj = getemajor(*devp);
6052 6052          *devp = makedevice(maj, (minor_t)connp->conn_dev);
6053 6053  
6054 6054          /*
6055 6055           * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
6056 6056           */
6057 6057          connp->conn_cred = credp;
6058 6058          connp->conn_cpid = curproc->p_pid;
6059 6059          /* Cache things in ixa without an extra refhold */
6060 6060          ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
6061 6061          connp->conn_ixa->ixa_cred = connp->conn_cred;
6062 6062          connp->conn_ixa->ixa_cpid = connp->conn_cpid;
6063 6063          if (is_system_labeled())
6064 6064                  connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6065 6065  
6066 6066          /*
6067 6067           * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
6068 6068           */
6069 6069          connp->conn_recv = ip_conn_input;
6070 6070          connp->conn_recvicmp = ip_conn_input_icmp;
6071 6071  
6072 6072          crhold(connp->conn_cred);
6073 6073  
6074 6074          /*
6075 6075           * If the caller has the process-wide flag set, then default to MAC
6076 6076           * exempt mode.  This allows read-down to unlabeled hosts.
6077 6077           */
6078 6078          if (getpflags(NET_MAC_AWARE, credp) != 0)
6079 6079                  connp->conn_mac_mode = CONN_MAC_AWARE;
6080 6080  
6081 6081          connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
6082 6082  
6083 6083          connp->conn_rq = q;
6084 6084          connp->conn_wq = WR(q);
6085 6085  
6086 6086          /* Non-zero default values */
6087 6087          connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
6088 6088  
6089 6089          /*
6090 6090           * Make the conn globally visible to walkers
6091 6091           */
6092 6092          ASSERT(connp->conn_ref == 1);
6093 6093          mutex_enter(&connp->conn_lock);
6094 6094          connp->conn_state_flags &= ~CONN_INCIPIENT;
6095 6095          mutex_exit(&connp->conn_lock);
6096 6096  
6097 6097          qprocson(q);
6098 6098  
6099 6099          return (0);
6100 6100  }
6101 6101  
6102 6102  /*
6103 6103   * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
6104 6104   * all of them are copied to the conn_t. If the req is "zero", the policy is
6105 6105   * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
6106 6106   * fields.
6107 6107   * We keep only the latest setting of the policy and thus policy setting
6108 6108   * is not incremental/cumulative.
6109 6109   *
6110 6110   * Requests to set policies with multiple alternative actions will
6111 6111   * go through a different API.
6112 6112   */
6113 6113  int
6114 6114  ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
6115 6115  {
6116 6116          uint_t ah_req = 0;
6117 6117          uint_t esp_req = 0;
6118 6118          uint_t se_req = 0;
6119 6119          ipsec_act_t *actp = NULL;
6120 6120          uint_t nact;
6121 6121          ipsec_policy_head_t *ph;
6122 6122          boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
6123 6123          int error = 0;
6124 6124          netstack_t      *ns = connp->conn_netstack;
6125 6125          ip_stack_t      *ipst = ns->netstack_ip;
6126 6126          ipsec_stack_t   *ipss = ns->netstack_ipsec;
6127 6127  
6128 6128  #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
6129 6129  
6130 6130          /*
6131 6131           * The IP_SEC_OPT option does not allow variable length parameters,
6132 6132           * hence a request cannot be NULL.
6133 6133           */
6134 6134          if (req == NULL)
6135 6135                  return (EINVAL);
6136 6136  
6137 6137          ah_req = req->ipsr_ah_req;
6138 6138          esp_req = req->ipsr_esp_req;
6139 6139          se_req = req->ipsr_self_encap_req;
6140 6140  
6141 6141          /* Don't allow setting self-encap without one or more of AH/ESP. */
6142 6142          if (se_req != 0 && esp_req == 0 && ah_req == 0)
6143 6143                  return (EINVAL);
6144 6144  
6145 6145          /*
6146 6146           * Are we dealing with a request to reset the policy (i.e.
6147 6147           * zero requests).
6148 6148           */
6149 6149          is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
6150 6150              (esp_req & REQ_MASK) == 0 &&
6151 6151              (se_req & REQ_MASK) == 0);
6152 6152  
6153 6153          if (!is_pol_reset) {
6154 6154                  /*
6155 6155                   * If we couldn't load IPsec, fail with "protocol
6156 6156                   * not supported".
6157 6157                   * IPsec may not have been loaded for a request with zero
6158 6158                   * policies, so we don't fail in this case.
6159 6159                   */
6160 6160                  mutex_enter(&ipss->ipsec_loader_lock);
6161 6161                  if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
6162 6162                          mutex_exit(&ipss->ipsec_loader_lock);
6163 6163                          return (EPROTONOSUPPORT);
6164 6164                  }
6165 6165                  mutex_exit(&ipss->ipsec_loader_lock);
6166 6166  
6167 6167                  /*
6168 6168                   * Test for valid requests. Invalid algorithms
6169 6169                   * need to be tested by IPsec code because new
6170 6170                   * algorithms can be added dynamically.
6171 6171                   */
6172 6172                  if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6173 6173                      (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6174 6174                      (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
6175 6175                          return (EINVAL);
6176 6176                  }
6177 6177  
6178 6178                  /*
6179 6179                   * Only privileged users can issue these
6180 6180                   * requests.
6181 6181                   */
6182 6182                  if (((ah_req & IPSEC_PREF_NEVER) ||
6183 6183                      (esp_req & IPSEC_PREF_NEVER) ||
6184 6184                      (se_req & IPSEC_PREF_NEVER)) &&
6185 6185                      secpolicy_ip_config(cr, B_FALSE) != 0) {
6186 6186                          return (EPERM);
6187 6187                  }
6188 6188  
6189 6189                  /*
6190 6190                   * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
6191 6191                   * are mutually exclusive.
6192 6192                   */
6193 6193                  if (((ah_req & REQ_MASK) == REQ_MASK) ||
6194 6194                      ((esp_req & REQ_MASK) == REQ_MASK) ||
6195 6195                      ((se_req & REQ_MASK) == REQ_MASK)) {
6196 6196                          /* Both of them are set */
6197 6197                          return (EINVAL);
6198 6198                  }
6199 6199          }
6200 6200  
6201 6201          ASSERT(MUTEX_HELD(&connp->conn_lock));
6202 6202  
6203 6203          /*
6204 6204           * If we have already cached policies in conn_connect(), don't
6205 6205           * let them change now. We cache policies for connections
6206 6206           * whose src,dst [addr, port] is known.
6207 6207           */
6208 6208          if (connp->conn_policy_cached) {
6209 6209                  return (EINVAL);
6210 6210          }
6211 6211  
6212 6212          /*
6213 6213           * We have a zero policies, reset the connection policy if already
6214 6214           * set. This will cause the connection to inherit the
6215 6215           * global policy, if any.
6216 6216           */
6217 6217          if (is_pol_reset) {
6218 6218                  if (connp->conn_policy != NULL) {
6219 6219                          IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
6220 6220                          connp->conn_policy = NULL;
6221 6221                  }
6222 6222                  connp->conn_in_enforce_policy = B_FALSE;
6223 6223                  connp->conn_out_enforce_policy = B_FALSE;
6224 6224                  return (0);
6225 6225          }
6226 6226  
6227 6227          ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
6228 6228              ipst->ips_netstack);
6229 6229          if (ph == NULL)
6230 6230                  goto enomem;
6231 6231  
6232 6232          ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
6233 6233          if (actp == NULL)
6234 6234                  goto enomem;
6235 6235  
6236 6236          /*
6237 6237           * Always insert IPv4 policy entries, since they can also apply to
6238 6238           * ipv6 sockets being used in ipv4-compat mode.
6239 6239           */
6240 6240          if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6241 6241              IPSEC_TYPE_INBOUND, ns))
6242 6242                  goto enomem;
6243 6243          is_pol_inserted = B_TRUE;
6244 6244          if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6245 6245              IPSEC_TYPE_OUTBOUND, ns))
6246 6246                  goto enomem;
6247 6247  
6248 6248          /*
6249 6249           * We're looking at a v6 socket, also insert the v6-specific
6250 6250           * entries.
6251 6251           */
6252 6252          if (connp->conn_family == AF_INET6) {
6253 6253                  if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6254 6254                      IPSEC_TYPE_INBOUND, ns))
6255 6255                          goto enomem;
6256 6256                  if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6257 6257                      IPSEC_TYPE_OUTBOUND, ns))
6258 6258                          goto enomem;
6259 6259          }
6260 6260  
6261 6261          ipsec_actvec_free(actp, nact);
6262 6262  
6263 6263          /*
6264 6264           * If the requests need security, set enforce_policy.
6265 6265           * If the requests are IPSEC_PREF_NEVER, one should
6266 6266           * still set conn_out_enforce_policy so that ip_set_destination
6267 6267           * marks the ip_xmit_attr_t appropriatly. This is needed so that
6268 6268           * for connections that we don't cache policy in at connect time,
6269 6269           * if global policy matches in ip_output_attach_policy, we
6270 6270           * don't wrongly inherit global policy. Similarly, we need
6271 6271           * to set conn_in_enforce_policy also so that we don't verify
6272 6272           * policy wrongly.
6273 6273           */
6274 6274          if ((ah_req & REQ_MASK) != 0 ||
6275 6275              (esp_req & REQ_MASK) != 0 ||
6276 6276              (se_req & REQ_MASK) != 0) {
6277 6277                  connp->conn_in_enforce_policy = B_TRUE;
6278 6278                  connp->conn_out_enforce_policy = B_TRUE;
6279 6279          }
6280 6280  
6281 6281          return (error);
6282 6282  #undef REQ_MASK
6283 6283  
6284 6284          /*
6285 6285           * Common memory-allocation-failure exit path.
6286 6286           */
6287 6287  enomem:
6288 6288          if (actp != NULL)
6289 6289                  ipsec_actvec_free(actp, nact);
6290 6290          if (is_pol_inserted)
6291 6291                  ipsec_polhead_flush(ph, ns);
6292 6292          return (ENOMEM);
6293 6293  }
6294 6294  
6295 6295  /*
6296 6296   * Set socket options for joining and leaving multicast groups.
6297 6297   * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6298 6298   * The caller has already check that the option name is consistent with
6299 6299   * the address family of the socket.
6300 6300   */
6301 6301  int
6302 6302  ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6303 6303      uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6304 6304  {
6305 6305          int             *i1 = (int *)invalp;
6306 6306          int             error = 0;
6307 6307          ip_stack_t      *ipst = connp->conn_netstack->netstack_ip;
6308 6308          struct ip_mreq  *v4_mreqp;
6309 6309          struct ipv6_mreq *v6_mreqp;
6310 6310          struct group_req *greqp;
6311 6311          ire_t *ire;
6312 6312          boolean_t done = B_FALSE;
6313 6313          ipaddr_t ifaddr;
6314 6314          in6_addr_t v6group;
6315 6315          uint_t ifindex;
6316 6316          boolean_t mcast_opt = B_TRUE;
6317 6317          mcast_record_t fmode;
6318 6318          int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6319 6319              ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6320 6320  
6321 6321          switch (name) {
6322 6322          case IP_ADD_MEMBERSHIP:
6323 6323          case IPV6_JOIN_GROUP:
6324 6324                  mcast_opt = B_FALSE;
6325 6325                  /* FALLTHROUGH */
6326 6326          case MCAST_JOIN_GROUP:
6327 6327                  fmode = MODE_IS_EXCLUDE;
6328 6328                  optfn = ip_opt_add_group;
6329 6329                  break;
6330 6330  
6331 6331          case IP_DROP_MEMBERSHIP:
6332 6332          case IPV6_LEAVE_GROUP:
6333 6333                  mcast_opt = B_FALSE;
6334 6334                  /* FALLTHROUGH */
6335 6335          case MCAST_LEAVE_GROUP:
6336 6336                  fmode = MODE_IS_INCLUDE;
6337 6337                  optfn = ip_opt_delete_group;
6338 6338                  break;
6339 6339          default:
6340 6340                  ASSERT(0);
6341 6341          }
6342 6342  
6343 6343          if (mcast_opt) {
6344 6344                  struct sockaddr_in *sin;
6345 6345                  struct sockaddr_in6 *sin6;
6346 6346  
6347 6347                  greqp = (struct group_req *)i1;
6348 6348                  if (greqp->gr_group.ss_family == AF_INET) {
6349 6349                          sin = (struct sockaddr_in *)&(greqp->gr_group);
6350 6350                          IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6351 6351                  } else {
6352 6352                          if (!inet6)
6353 6353                                  return (EINVAL);        /* Not on INET socket */
6354 6354  
6355 6355                          sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6356 6356                          v6group = sin6->sin6_addr;
6357 6357                  }
6358 6358                  ifaddr = INADDR_ANY;
6359 6359                  ifindex = greqp->gr_interface;
6360 6360          } else if (inet6) {
6361 6361                  v6_mreqp = (struct ipv6_mreq *)i1;
6362 6362                  v6group = v6_mreqp->ipv6mr_multiaddr;
6363 6363                  ifaddr = INADDR_ANY;
6364 6364                  ifindex = v6_mreqp->ipv6mr_interface;
6365 6365          } else {
6366 6366                  v4_mreqp = (struct ip_mreq *)i1;
6367 6367                  IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6368 6368                  ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6369 6369                  ifindex = 0;
6370 6370          }
6371 6371  
6372 6372          /*
6373 6373           * In the multirouting case, we need to replicate
6374 6374           * the request on all interfaces that will take part
6375 6375           * in replication.  We do so because multirouting is
6376 6376           * reflective, thus we will probably receive multi-
6377 6377           * casts on those interfaces.
6378 6378           * The ip_multirt_apply_membership() succeeds if
6379 6379           * the operation succeeds on at least one interface.
6380 6380           */
6381 6381          if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6382 6382                  ipaddr_t group;
6383 6383  
6384 6384                  IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6385 6385  
6386 6386                  ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6387 6387                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6388 6388                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6389 6389          } else {
6390 6390                  ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6391 6391                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6392 6392                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6393 6393          }
6394 6394          if (ire != NULL) {
6395 6395                  if (ire->ire_flags & RTF_MULTIRT) {
6396 6396                          error = ip_multirt_apply_membership(optfn, ire, connp,
6397 6397                              checkonly, &v6group, fmode, &ipv6_all_zeros);
6398 6398                          done = B_TRUE;
6399 6399                  }
6400 6400                  ire_refrele(ire);
6401 6401          }
6402 6402  
6403 6403          if (!done) {
6404 6404                  error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6405 6405                      fmode, &ipv6_all_zeros);
6406 6406          }
6407 6407          return (error);
6408 6408  }
6409 6409  
6410 6410  /*
6411 6411   * Set socket options for joining and leaving multicast groups
6412 6412   * for specific sources.
6413 6413   * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6414 6414   * The caller has already check that the option name is consistent with
6415 6415   * the address family of the socket.
6416 6416   */
6417 6417  int
6418 6418  ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6419 6419      uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6420 6420  {
6421 6421          int             *i1 = (int *)invalp;
6422 6422          int             error = 0;
6423 6423          ip_stack_t      *ipst = connp->conn_netstack->netstack_ip;
6424 6424          struct ip_mreq_source *imreqp;
6425 6425          struct group_source_req *gsreqp;
6426 6426          in6_addr_t v6group, v6src;
6427 6427          uint32_t ifindex;
6428 6428          ipaddr_t ifaddr;
6429 6429          boolean_t mcast_opt = B_TRUE;
6430 6430          mcast_record_t fmode;
6431 6431          ire_t *ire;
6432 6432          boolean_t done = B_FALSE;
6433 6433          int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6434 6434              ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6435 6435  
6436 6436          switch (name) {
6437 6437          case IP_BLOCK_SOURCE:
6438 6438                  mcast_opt = B_FALSE;
6439 6439                  /* FALLTHROUGH */
6440 6440          case MCAST_BLOCK_SOURCE:
6441 6441                  fmode = MODE_IS_EXCLUDE;
6442 6442                  optfn = ip_opt_add_group;
6443 6443                  break;
6444 6444  
6445 6445          case IP_UNBLOCK_SOURCE:
6446 6446                  mcast_opt = B_FALSE;
6447 6447                  /* FALLTHROUGH */
6448 6448          case MCAST_UNBLOCK_SOURCE:
6449 6449                  fmode = MODE_IS_EXCLUDE;
6450 6450                  optfn = ip_opt_delete_group;
6451 6451                  break;
6452 6452  
6453 6453          case IP_ADD_SOURCE_MEMBERSHIP:
6454 6454                  mcast_opt = B_FALSE;
6455 6455                  /* FALLTHROUGH */
6456 6456          case MCAST_JOIN_SOURCE_GROUP:
6457 6457                  fmode = MODE_IS_INCLUDE;
6458 6458                  optfn = ip_opt_add_group;
6459 6459                  break;
6460 6460  
6461 6461          case IP_DROP_SOURCE_MEMBERSHIP:
6462 6462                  mcast_opt = B_FALSE;
6463 6463                  /* FALLTHROUGH */
6464 6464          case MCAST_LEAVE_SOURCE_GROUP:
6465 6465                  fmode = MODE_IS_INCLUDE;
6466 6466                  optfn = ip_opt_delete_group;
6467 6467                  break;
6468 6468          default:
6469 6469                  ASSERT(0);
6470 6470          }
6471 6471  
6472 6472          if (mcast_opt) {
6473 6473                  gsreqp = (struct group_source_req *)i1;
6474 6474                  ifindex = gsreqp->gsr_interface;
6475 6475                  if (gsreqp->gsr_group.ss_family == AF_INET) {
6476 6476                          struct sockaddr_in *s;
6477 6477                          s = (struct sockaddr_in *)&gsreqp->gsr_group;
6478 6478                          IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6479 6479                          s = (struct sockaddr_in *)&gsreqp->gsr_source;
6480 6480                          IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6481 6481                  } else {
6482 6482                          struct sockaddr_in6 *s6;
6483 6483  
6484 6484                          if (!inet6)
6485 6485                                  return (EINVAL);        /* Not on INET socket */
6486 6486  
6487 6487                          s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6488 6488                          v6group = s6->sin6_addr;
6489 6489                          s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6490 6490                          v6src = s6->sin6_addr;
6491 6491                  }
6492 6492                  ifaddr = INADDR_ANY;
6493 6493          } else {
6494 6494                  imreqp = (struct ip_mreq_source *)i1;
6495 6495                  IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6496 6496                  IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6497 6497                  ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6498 6498                  ifindex = 0;
6499 6499          }
6500 6500  
6501 6501          /*
6502 6502           * Handle src being mapped INADDR_ANY by changing it to unspecified.
6503 6503           */
6504 6504          if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6505 6505                  v6src = ipv6_all_zeros;
6506 6506  
6507 6507          /*
6508 6508           * In the multirouting case, we need to replicate
6509 6509           * the request as noted in the mcast cases above.
6510 6510           */
6511 6511          if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6512 6512                  ipaddr_t group;
6513 6513  
6514 6514                  IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6515 6515  
6516 6516                  ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6517 6517                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6518 6518                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6519 6519          } else {
6520 6520                  ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6521 6521                      IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6522 6522                      MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6523 6523          }
6524 6524          if (ire != NULL) {
6525 6525                  if (ire->ire_flags & RTF_MULTIRT) {
6526 6526                          error = ip_multirt_apply_membership(optfn, ire, connp,
6527 6527                              checkonly, &v6group, fmode, &v6src);
6528 6528                          done = B_TRUE;
6529 6529                  }
6530 6530                  ire_refrele(ire);
6531 6531          }
6532 6532          if (!done) {
6533 6533                  error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6534 6534                      fmode, &v6src);
6535 6535          }
6536 6536          return (error);
6537 6537  }
6538 6538  
6539 6539  /*
6540 6540   * Given a destination address and a pointer to where to put the information
6541 6541   * this routine fills in the mtuinfo.
6542 6542   * The socket must be connected.
6543 6543   * For sctp conn_faddr is the primary address.
6544 6544   */
6545 6545  int
6546 6546  ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6547 6547  {
6548 6548          uint32_t        pmtu = IP_MAXPACKET;
6549 6549          uint_t          scopeid;
6550 6550  
6551 6551          if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6552 6552                  return (-1);
6553 6553  
6554 6554          /* In case we never sent or called ip_set_destination_v4/v6 */
6555 6555          if (ixa->ixa_ire != NULL)
6556 6556                  pmtu = ip_get_pmtu(ixa);
6557 6557  
6558 6558          if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6559 6559                  scopeid = ixa->ixa_scopeid;
6560 6560          else
6561 6561                  scopeid = 0;
6562 6562  
6563 6563          bzero(mtuinfo, sizeof (*mtuinfo));
6564 6564          mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6565 6565          mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6566 6566          mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6567 6567          mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6568 6568          mtuinfo->ip6m_mtu = pmtu;
6569 6569  
6570 6570          return (sizeof (struct ip6_mtuinfo));
6571 6571  }
6572 6572  
6573 6573  /*
6574 6574   * When the src multihoming is changed from weak to [strong, preferred]
6575 6575   * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6576 6576   * and identify routes that were created by user-applications in the
6577 6577   * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6578 6578   * currently defined. These routes are then 'rebound', i.e., their ire_ill
6579 6579   * is selected by finding an interface route for the gateway.
6580 6580   */
6581 6581  /* ARGSUSED */
6582 6582  void
6583 6583  ip_ire_rebind_walker(ire_t *ire, void *notused)
6584 6584  {
6585 6585          if (!ire->ire_unbound || ire->ire_ill != NULL)
6586 6586                  return;
6587 6587          ire_rebind(ire);
6588 6588          ire_delete(ire);
6589 6589  }
6590 6590  
6591 6591  /*
6592 6592   * When the src multihoming is changed from  [strong, preferred] to weak,
6593 6593   * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6594 6594   * set any entries that were created by user-applications in the unbound state
6595 6595   * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6596 6596   */
6597 6597  /* ARGSUSED */
6598 6598  void
6599 6599  ip_ire_unbind_walker(ire_t *ire, void *notused)
6600 6600  {
6601 6601          ire_t *new_ire;
6602 6602  
6603 6603          if (!ire->ire_unbound || ire->ire_ill == NULL)
6604 6604                  return;
6605 6605          if (ire->ire_ipversion == IPV6_VERSION) {
6606 6606                  new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6607 6607                      &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6608 6608                      ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6609 6609          } else {
6610 6610                  new_ire = ire_create((uchar_t *)&ire->ire_addr,
6611 6611                      (uchar_t *)&ire->ire_mask,
6612 6612                      (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6613 6613                      ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6614 6614          }
6615 6615          if (new_ire == NULL)
6616 6616                  return;
6617 6617          new_ire->ire_unbound = B_TRUE;
6618 6618          /*
6619 6619           * The bound ire must first be deleted so that we don't return
6620 6620           * the existing one on the attempt to add the unbound new_ire.
6621 6621           */
6622 6622          ire_delete(ire);
6623 6623          new_ire = ire_add(new_ire);
6624 6624          if (new_ire != NULL)
6625 6625                  ire_refrele(new_ire);
6626 6626  }
6627 6627  
6628 6628  /*
6629 6629   * When the settings of ip*_strict_src_multihoming tunables are changed,
6630 6630   * all cached routes need to be recomputed. This recomputation needs to be
6631 6631   * done when going from weaker to stronger modes so that the cached ire
6632 6632   * for the connection does not violate the current ip*_strict_src_multihoming
6633 6633   * setting. It also needs to be done when going from stronger to weaker modes,
6634 6634   * so that we fall back to matching on the longest-matching-route (as opposed
6635 6635   * to a shorter match that may have been selected in the strong mode
6636 6636   * to satisfy src_multihoming settings).
6637 6637   *
6638 6638   * The cached ixa_ire entires for all conn_t entries are marked as
6639 6639   * "verify" so that they will be recomputed for the next packet.
6640 6640   */
6641 6641  void
6642 6642  conn_ire_revalidate(conn_t *connp, void *arg)
6643 6643  {
6644 6644          boolean_t isv6 = (boolean_t)arg;
6645 6645  
6646 6646          if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6647 6647              (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6648 6648                  return;
6649 6649          connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6650 6650  }
6651 6651  
6652 6652  /*
6653 6653   * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6654 6654   * When an ipf is passed here for the first time, if
6655 6655   * we already have in-order fragments on the queue, we convert from the fast-
6656 6656   * path reassembly scheme to the hard-case scheme.  From then on, additional
6657 6657   * fragments are reassembled here.  We keep track of the start and end offsets
6658 6658   * of each piece, and the number of holes in the chain.  When the hole count
6659 6659   * goes to zero, we are done!
6660 6660   *
6661 6661   * The ipf_count will be updated to account for any mblk(s) added (pointed to
6662 6662   * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6663 6663   * ipfb_count and ill_frag_count by the difference of ipf_count before and
6664 6664   * after the call to ip_reassemble().
6665 6665   */
6666 6666  int
6667 6667  ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6668 6668      size_t msg_len)
6669 6669  {
6670 6670          uint_t  end;
6671 6671          mblk_t  *next_mp;
6672 6672          mblk_t  *mp1;
6673 6673          uint_t  offset;
6674 6674          boolean_t incr_dups = B_TRUE;
6675 6675          boolean_t offset_zero_seen = B_FALSE;
6676 6676          boolean_t pkt_boundary_checked = B_FALSE;
6677 6677  
6678 6678          /* If start == 0 then ipf_nf_hdr_len has to be set. */
6679 6679          ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6680 6680  
6681 6681          /* Add in byte count */
6682 6682          ipf->ipf_count += msg_len;
6683 6683          if (ipf->ipf_end) {
6684 6684                  /*
6685 6685                   * We were part way through in-order reassembly, but now there
6686 6686                   * is a hole.  We walk through messages already queued, and
6687 6687                   * mark them for hard case reassembly.  We know that up till
6688 6688                   * now they were in order starting from offset zero.
6689 6689                   */
6690 6690                  offset = 0;
6691 6691                  for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6692 6692                          IP_REASS_SET_START(mp1, offset);
6693 6693                          if (offset == 0) {
6694 6694                                  ASSERT(ipf->ipf_nf_hdr_len != 0);
6695 6695                                  offset = -ipf->ipf_nf_hdr_len;
6696 6696                          }
6697 6697                          offset += mp1->b_wptr - mp1->b_rptr;
6698 6698                          IP_REASS_SET_END(mp1, offset);
6699 6699                  }
6700 6700                  /* One hole at the end. */
6701 6701                  ipf->ipf_hole_cnt = 1;
6702 6702                  /* Brand it as a hard case, forever. */
6703 6703                  ipf->ipf_end = 0;
6704 6704          }
6705 6705          /* Walk through all the new pieces. */
6706 6706          do {
6707 6707                  end = start + (mp->b_wptr - mp->b_rptr);
6708 6708                  /*
6709 6709                   * If start is 0, decrease 'end' only for the first mblk of
6710 6710                   * the fragment. Otherwise 'end' can get wrong value in the
6711 6711                   * second pass of the loop if first mblk is exactly the
6712 6712                   * size of ipf_nf_hdr_len.
6713 6713                   */
6714 6714                  if (start == 0 && !offset_zero_seen) {
6715 6715                          /* First segment */
6716 6716                          ASSERT(ipf->ipf_nf_hdr_len != 0);
6717 6717                          end -= ipf->ipf_nf_hdr_len;
6718 6718                          offset_zero_seen = B_TRUE;
6719 6719                  }
6720 6720                  next_mp = mp->b_cont;
6721 6721                  /*
6722 6722                   * We are checking to see if there is any interesing data
6723 6723                   * to process.  If there isn't and the mblk isn't the
6724 6724                   * one which carries the unfragmentable header then we
6725 6725                   * drop it.  It's possible to have just the unfragmentable
6726 6726                   * header come through without any data.  That needs to be
6727 6727                   * saved.
6728 6728                   *
6729 6729                   * If the assert at the top of this function holds then the
6730 6730                   * term "ipf->ipf_nf_hdr_len != 0" isn't needed.  This code
6731 6731                   * is infrequently traveled enough that the test is left in
6732 6732                   * to protect against future code changes which break that
6733 6733                   * invariant.
6734 6734                   */
6735 6735                  if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6736 6736                          /* Empty.  Blast it. */
6737 6737                          IP_REASS_SET_START(mp, 0);
6738 6738                          IP_REASS_SET_END(mp, 0);
6739 6739                          /*
6740 6740                           * If the ipf points to the mblk we are about to free,
6741 6741                           * update ipf to point to the next mblk (or NULL
6742 6742                           * if none).
6743 6743                           */
6744 6744                          if (ipf->ipf_mp->b_cont == mp)
6745 6745                                  ipf->ipf_mp->b_cont = next_mp;
6746 6746                          freeb(mp);
6747 6747                          continue;
6748 6748                  }
6749 6749                  mp->b_cont = NULL;
6750 6750                  IP_REASS_SET_START(mp, start);
6751 6751                  IP_REASS_SET_END(mp, end);
6752 6752                  if (!ipf->ipf_tail_mp) {
6753 6753                          ipf->ipf_tail_mp = mp;
6754 6754                          ipf->ipf_mp->b_cont = mp;
6755 6755                          if (start == 0 || !more) {
6756 6756                                  ipf->ipf_hole_cnt = 1;
6757 6757                                  /*
6758 6758                                   * if the first fragment comes in more than one
6759 6759                                   * mblk, this loop will be executed for each
6760 6760                                   * mblk. Need to adjust hole count so exiting
6761 6761                                   * this routine will leave hole count at 1.
6762 6762                                   */
6763 6763                                  if (next_mp)
6764 6764                                          ipf->ipf_hole_cnt++;
6765 6765                          } else
6766 6766                                  ipf->ipf_hole_cnt = 2;
6767 6767                          continue;
6768 6768                  } else if (ipf->ipf_last_frag_seen && !more &&
6769 6769                      !pkt_boundary_checked) {
6770 6770                          /*
6771 6771                           * We check datagram boundary only if this fragment
6772 6772                           * claims to be the last fragment and we have seen a
6773 6773                           * last fragment in the past too. We do this only
6774 6774                           * once for a given fragment.
6775 6775                           *
6776 6776                           * start cannot be 0 here as fragments with start=0
6777 6777                           * and MF=0 gets handled as a complete packet. These
6778 6778                           * fragments should not reach here.
6779 6779                           */
6780 6780  
6781 6781                          if (start + msgdsize(mp) !=
6782 6782                              IP_REASS_END(ipf->ipf_tail_mp)) {
6783 6783                                  /*
6784 6784                                   * We have two fragments both of which claim
6785 6785                                   * to be the last fragment but gives conflicting
6786 6786                                   * information about the whole datagram size.
6787 6787                                   * Something fishy is going on. Drop the
6788 6788                                   * fragment and free up the reassembly list.
6789 6789                                   */
6790 6790                                  return (IP_REASS_FAILED);
6791 6791                          }
6792 6792  
6793 6793                          /*
6794 6794                           * We shouldn't come to this code block again for this
6795 6795                           * particular fragment.
6796 6796                           */
6797 6797                          pkt_boundary_checked = B_TRUE;
6798 6798                  }
6799 6799  
6800 6800                  /* New stuff at or beyond tail? */
6801 6801                  offset = IP_REASS_END(ipf->ipf_tail_mp);
6802 6802                  if (start >= offset) {
6803 6803                          if (ipf->ipf_last_frag_seen) {
6804 6804                                  /* current fragment is beyond last fragment */
6805 6805                                  return (IP_REASS_FAILED);
6806 6806                          }
6807 6807                          /* Link it on end. */
6808 6808                          ipf->ipf_tail_mp->b_cont = mp;
6809 6809                          ipf->ipf_tail_mp = mp;
6810 6810                          if (more) {
6811 6811                                  if (start != offset)
6812 6812                                          ipf->ipf_hole_cnt++;
6813 6813                          } else if (start == offset && next_mp == NULL)
6814 6814                                          ipf->ipf_hole_cnt--;
6815 6815                          continue;
6816 6816                  }
6817 6817                  mp1 = ipf->ipf_mp->b_cont;
6818 6818                  offset = IP_REASS_START(mp1);
6819 6819                  /* New stuff at the front? */
6820 6820                  if (start < offset) {
6821 6821                          if (start == 0) {
6822 6822                                  if (end >= offset) {
6823 6823                                          /* Nailed the hole at the begining. */
6824 6824                                          ipf->ipf_hole_cnt--;
6825 6825                                  }
6826 6826                          } else if (end < offset) {
6827 6827                                  /*
6828 6828                                   * A hole, stuff, and a hole where there used
6829 6829                                   * to be just a hole.
6830 6830                                   */
6831 6831                                  ipf->ipf_hole_cnt++;
6832 6832                          }
6833 6833                          mp->b_cont = mp1;
6834 6834                          /* Check for overlap. */
6835 6835                          while (end > offset) {
6836 6836                                  if (end < IP_REASS_END(mp1)) {
6837 6837                                          mp->b_wptr -= end - offset;
6838 6838                                          IP_REASS_SET_END(mp, offset);
6839 6839                                          BUMP_MIB(ill->ill_ip_mib,
6840 6840                                              ipIfStatsReasmPartDups);
6841 6841                                          break;
6842 6842                                  }
6843 6843                                  /* Did we cover another hole? */
6844 6844                                  if ((mp1->b_cont &&
6845 6845                                      IP_REASS_END(mp1) !=
6846 6846                                      IP_REASS_START(mp1->b_cont) &&
6847 6847                                      end >= IP_REASS_START(mp1->b_cont)) ||
6848 6848                                      (!ipf->ipf_last_frag_seen && !more)) {
6849 6849                                          ipf->ipf_hole_cnt--;
6850 6850                                  }
6851 6851                                  /* Clip out mp1. */
6852 6852                                  if ((mp->b_cont = mp1->b_cont) == NULL) {
6853 6853                                          /*
6854 6854                                           * After clipping out mp1, this guy
6855 6855                                           * is now hanging off the end.
6856 6856                                           */
6857 6857                                          ipf->ipf_tail_mp = mp;
6858 6858                                  }
6859 6859                                  IP_REASS_SET_START(mp1, 0);
6860 6860                                  IP_REASS_SET_END(mp1, 0);
6861 6861                                  /* Subtract byte count */
6862 6862                                  ipf->ipf_count -= mp1->b_datap->db_lim -
6863 6863                                      mp1->b_datap->db_base;
6864 6864                                  freeb(mp1);
6865 6865                                  BUMP_MIB(ill->ill_ip_mib,
6866 6866                                      ipIfStatsReasmPartDups);
6867 6867                                  mp1 = mp->b_cont;
6868 6868                                  if (!mp1)
6869 6869                                          break;
6870 6870                                  offset = IP_REASS_START(mp1);
6871 6871                          }
6872 6872                          ipf->ipf_mp->b_cont = mp;
6873 6873                          continue;
6874 6874                  }
6875 6875                  /*
6876 6876                   * The new piece starts somewhere between the start of the head
6877 6877                   * and before the end of the tail.
6878 6878                   */
6879 6879                  for (; mp1; mp1 = mp1->b_cont) {
6880 6880                          offset = IP_REASS_END(mp1);
6881 6881                          if (start < offset) {
6882 6882                                  if (end <= offset) {
6883 6883                                          /* Nothing new. */
6884 6884                                          IP_REASS_SET_START(mp, 0);
6885 6885                                          IP_REASS_SET_END(mp, 0);
6886 6886                                          /* Subtract byte count */
6887 6887                                          ipf->ipf_count -= mp->b_datap->db_lim -
6888 6888                                              mp->b_datap->db_base;
6889 6889                                          if (incr_dups) {
6890 6890                                                  ipf->ipf_num_dups++;
6891 6891                                                  incr_dups = B_FALSE;
6892 6892                                          }
6893 6893                                          freeb(mp);
6894 6894                                          BUMP_MIB(ill->ill_ip_mib,
6895 6895                                              ipIfStatsReasmDuplicates);
6896 6896                                          break;
6897 6897                                  }
6898 6898                                  /*
6899 6899                                   * Trim redundant stuff off beginning of new
6900 6900                                   * piece.
6901 6901                                   */
6902 6902                                  IP_REASS_SET_START(mp, offset);
6903 6903                                  mp->b_rptr += offset - start;
6904 6904                                  BUMP_MIB(ill->ill_ip_mib,
6905 6905                                      ipIfStatsReasmPartDups);
6906 6906                                  start = offset;
6907 6907                                  if (!mp1->b_cont) {
6908 6908                                          /*
6909 6909                                           * After trimming, this guy is now
6910 6910                                           * hanging off the end.
6911 6911                                           */
6912 6912                                          mp1->b_cont = mp;
6913 6913                                          ipf->ipf_tail_mp = mp;
6914 6914                                          if (!more) {
6915 6915                                                  ipf->ipf_hole_cnt--;
6916 6916                                          }
6917 6917                                          break;
6918 6918                                  }
6919 6919                          }
6920 6920                          if (start >= IP_REASS_START(mp1->b_cont))
6921 6921                                  continue;
6922 6922                          /* Fill a hole */
6923 6923                          if (start > offset)
6924 6924                                  ipf->ipf_hole_cnt++;
6925 6925                          mp->b_cont = mp1->b_cont;
6926 6926                          mp1->b_cont = mp;
6927 6927                          mp1 = mp->b_cont;
6928 6928                          offset = IP_REASS_START(mp1);
6929 6929                          if (end >= offset) {
6930 6930                                  ipf->ipf_hole_cnt--;
6931 6931                                  /* Check for overlap. */
6932 6932                                  while (end > offset) {
6933 6933                                          if (end < IP_REASS_END(mp1)) {
6934 6934                                                  mp->b_wptr -= end - offset;
6935 6935                                                  IP_REASS_SET_END(mp, offset);
6936 6936                                                  /*
6937 6937                                                   * TODO we might bump
6938 6938                                                   * this up twice if there is
6939 6939                                                   * overlap at both ends.
6940 6940                                                   */
6941 6941                                                  BUMP_MIB(ill->ill_ip_mib,
6942 6942                                                      ipIfStatsReasmPartDups);
6943 6943                                                  break;
6944 6944                                          }
6945 6945                                          /* Did we cover another hole? */
6946 6946                                          if ((mp1->b_cont &&
6947 6947                                              IP_REASS_END(mp1)
6948 6948                                              != IP_REASS_START(mp1->b_cont) &&
6949 6949                                              end >=
6950 6950                                              IP_REASS_START(mp1->b_cont)) ||
6951 6951                                              (!ipf->ipf_last_frag_seen &&
6952 6952                                              !more)) {
6953 6953                                                  ipf->ipf_hole_cnt--;
6954 6954                                          }
6955 6955                                          /* Clip out mp1. */
6956 6956                                          if ((mp->b_cont = mp1->b_cont) ==
6957 6957                                              NULL) {
6958 6958                                                  /*
6959 6959                                                   * After clipping out mp1,
6960 6960                                                   * this guy is now hanging
6961 6961                                                   * off the end.
6962 6962                                                   */
6963 6963                                                  ipf->ipf_tail_mp = mp;
6964 6964                                          }
6965 6965                                          IP_REASS_SET_START(mp1, 0);
6966 6966                                          IP_REASS_SET_END(mp1, 0);
6967 6967                                          /* Subtract byte count */
6968 6968                                          ipf->ipf_count -=
6969 6969                                              mp1->b_datap->db_lim -
6970 6970                                              mp1->b_datap->db_base;
6971 6971                                          freeb(mp1);
6972 6972                                          BUMP_MIB(ill->ill_ip_mib,
6973 6973                                              ipIfStatsReasmPartDups);
6974 6974                                          mp1 = mp->b_cont;
6975 6975                                          if (!mp1)
6976 6976                                                  break;
6977 6977                                          offset = IP_REASS_START(mp1);
6978 6978                                  }
6979 6979                          }
6980 6980                          break;
6981 6981                  }
6982 6982          } while (start = end, mp = next_mp);
6983 6983  
6984 6984          /* Fragment just processed could be the last one. Remember this fact */
6985 6985          if (!more)
6986 6986                  ipf->ipf_last_frag_seen = B_TRUE;
6987 6987  
6988 6988          /* Still got holes? */
6989 6989          if (ipf->ipf_hole_cnt)
6990 6990                  return (IP_REASS_PARTIAL);
6991 6991          /* Clean up overloaded fields to avoid upstream disasters. */
6992 6992          for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6993 6993                  IP_REASS_SET_START(mp1, 0);
6994 6994                  IP_REASS_SET_END(mp1, 0);
6995 6995          }
6996 6996          return (IP_REASS_COMPLETE);
6997 6997  }
6998 6998  
6999 6999  /*
7000 7000   * Fragmentation reassembly.  Each ILL has a hash table for
7001 7001   * queuing packets undergoing reassembly for all IPIFs
7002 7002   * associated with the ILL.  The hash is based on the packet
7003 7003   * IP ident field.  The ILL frag hash table was allocated
7004 7004   * as a timer block at the time the ILL was created.  Whenever
7005 7005   * there is anything on the reassembly queue, the timer will
7006 7006   * be running.  Returns the reassembled packet if reassembly completes.
7007 7007   */
7008 7008  mblk_t *
7009 7009  ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
7010 7010  {
7011 7011          uint32_t        frag_offset_flags;
7012 7012          mblk_t          *t_mp;
7013 7013          ipaddr_t        dst;
7014 7014          uint8_t         proto = ipha->ipha_protocol;
7015 7015          uint32_t        sum_val;
7016 7016          uint16_t        sum_flags;
7017 7017          ipf_t           *ipf;
7018 7018          ipf_t           **ipfp;
7019 7019          ipfb_t          *ipfb;
7020 7020          uint16_t        ident;
7021 7021          uint32_t        offset;
7022 7022          ipaddr_t        src;
7023 7023          uint_t          hdr_length;
7024 7024          uint32_t        end;
7025 7025          mblk_t          *mp1;
7026 7026          mblk_t          *tail_mp;
7027 7027          size_t          count;
7028 7028          size_t          msg_len;
7029 7029          uint8_t         ecn_info = 0;
7030 7030          uint32_t        packet_size;
7031 7031          boolean_t       pruned = B_FALSE;
7032 7032          ill_t           *ill = ira->ira_ill;
7033 7033          ip_stack_t      *ipst = ill->ill_ipst;
7034 7034  
7035 7035          /*
7036 7036           * Drop the fragmented as early as possible, if
7037 7037           * we don't have resource(s) to re-assemble.
7038 7038           */
7039 7039          if (ipst->ips_ip_reass_queue_bytes == 0) {
7040 7040                  freemsg(mp);
7041 7041                  return (NULL);
7042 7042          }
7043 7043  
7044 7044          /* Check for fragmentation offset; return if there's none */
7045 7045          if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
7046 7046              (IPH_MF | IPH_OFFSET)) == 0)
7047 7047                  return (mp);
7048 7048  
7049 7049          /*
7050 7050           * We utilize hardware computed checksum info only for UDP since
7051 7051           * IP fragmentation is a normal occurrence for the protocol.  In
7052 7052           * addition, checksum offload support for IP fragments carrying
7053 7053           * UDP payload is commonly implemented across network adapters.
7054 7054           */
7055 7055          ASSERT(ira->ira_rill != NULL);
7056 7056          if (proto == IPPROTO_UDP && dohwcksum &&
7057 7057              ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
7058 7058              (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
7059 7059                  mblk_t *mp1 = mp->b_cont;
7060 7060                  int32_t len;
7061 7061  
7062 7062                  /* Record checksum information from the packet */
7063 7063                  sum_val = (uint32_t)DB_CKSUM16(mp);
7064 7064                  sum_flags = DB_CKSUMFLAGS(mp);
7065 7065  
7066 7066                  /* IP payload offset from beginning of mblk */
7067 7067                  offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
7068 7068  
7069 7069                  if ((sum_flags & HCK_PARTIALCKSUM) &&
7070 7070                      (mp1 == NULL || mp1->b_cont == NULL) &&
7071 7071                      offset >= DB_CKSUMSTART(mp) &&
7072 7072                      ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
7073 7073                          uint32_t adj;
7074 7074                          /*
7075 7075                           * Partial checksum has been calculated by hardware
7076 7076                           * and attached to the packet; in addition, any
7077 7077                           * prepended extraneous data is even byte aligned.
7078 7078                           * If any such data exists, we adjust the checksum;
7079 7079                           * this would also handle any postpended data.
7080 7080                           */
7081 7081                          IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
7082 7082                              mp, mp1, len, adj);
7083 7083  
7084 7084                          /* One's complement subtract extraneous checksum */
7085 7085                          if (adj >= sum_val)
7086 7086                                  sum_val = ~(adj - sum_val) & 0xFFFF;
7087 7087                          else
7088 7088                                  sum_val -= adj;
7089 7089                  }
7090 7090          } else {
7091 7091                  sum_val = 0;
7092 7092                  sum_flags = 0;
7093 7093          }
7094 7094  
7095 7095          /* Clear hardware checksumming flag */
7096 7096          DB_CKSUMFLAGS(mp) = 0;
7097 7097  
7098 7098          ident = ipha->ipha_ident;
7099 7099          offset = (frag_offset_flags << 3) & 0xFFFF;
7100 7100          src = ipha->ipha_src;
7101 7101          dst = ipha->ipha_dst;
7102 7102          hdr_length = IPH_HDR_LENGTH(ipha);
7103 7103          end = ntohs(ipha->ipha_length) - hdr_length;
7104 7104  
7105 7105          /* If end == 0 then we have a packet with no data, so just free it */
7106 7106          if (end == 0) {
7107 7107                  freemsg(mp);
7108 7108                  return (NULL);
7109 7109          }
7110 7110  
7111 7111          /* Record the ECN field info. */
7112 7112          ecn_info = (ipha->ipha_type_of_service & 0x3);
7113 7113          if (offset != 0) {
7114 7114                  /*
7115 7115                   * If this isn't the first piece, strip the header, and
7116 7116                   * add the offset to the end value.
7117 7117                   */
7118 7118                  mp->b_rptr += hdr_length;
7119 7119                  end += offset;
7120 7120          }
7121 7121  
7122 7122          /* Handle vnic loopback of fragments */
7123 7123          if (mp->b_datap->db_ref > 2)
7124 7124                  msg_len = 0;
7125 7125          else
7126 7126                  msg_len = MBLKSIZE(mp);
7127 7127  
7128 7128          tail_mp = mp;
7129 7129          while (tail_mp->b_cont != NULL) {
7130 7130                  tail_mp = tail_mp->b_cont;
7131 7131                  if (tail_mp->b_datap->db_ref <= 2)
7132 7132                          msg_len += MBLKSIZE(tail_mp);
7133 7133          }
7134 7134  
7135 7135          /* If the reassembly list for this ILL will get too big, prune it */
7136 7136          if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
7137 7137              ipst->ips_ip_reass_queue_bytes) {
7138 7138                  DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
7139 7139                      uint_t, ill->ill_frag_count,
7140 7140                      uint_t, ipst->ips_ip_reass_queue_bytes);
7141 7141                  ill_frag_prune(ill,
7142 7142                      (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
7143 7143                      (ipst->ips_ip_reass_queue_bytes - msg_len));
7144 7144                  pruned = B_TRUE;
7145 7145          }
7146 7146  
7147 7147          ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
7148 7148          mutex_enter(&ipfb->ipfb_lock);
7149 7149  
7150 7150          ipfp = &ipfb->ipfb_ipf;
7151 7151          /* Try to find an existing fragment queue for this packet. */
7152 7152          for (;;) {
7153 7153                  ipf = ipfp[0];
7154 7154                  if (ipf != NULL) {
7155 7155                          /*
7156 7156                           * It has to match on ident and src/dst address.
7157 7157                           */
7158 7158                          if (ipf->ipf_ident == ident &&
7159 7159                              ipf->ipf_src == src &&
7160 7160                              ipf->ipf_dst == dst &&
7161 7161                              ipf->ipf_protocol == proto) {
7162 7162                                  /*
7163 7163                                   * If we have received too many
7164 7164                                   * duplicate fragments for this packet
7165 7165                                   * free it.
7166 7166                                   */
7167 7167                                  if (ipf->ipf_num_dups > ip_max_frag_dups) {
7168 7168                                          ill_frag_free_pkts(ill, ipfb, ipf, 1);
7169 7169                                          freemsg(mp);
7170 7170                                          mutex_exit(&ipfb->ipfb_lock);
7171 7171                                          return (NULL);
7172 7172                                  }
7173 7173                                  /* Found it. */
7174 7174                                  break;
7175 7175                          }
7176 7176                          ipfp = &ipf->ipf_hash_next;
7177 7177                          continue;
7178 7178                  }
7179 7179  
7180 7180                  /*
7181 7181                   * If we pruned the list, do we want to store this new
7182 7182                   * fragment?. We apply an optimization here based on the
7183 7183                   * fact that most fragments will be received in order.
7184 7184                   * So if the offset of this incoming fragment is zero,
7185 7185                   * it is the first fragment of a new packet. We will
7186 7186                   * keep it.  Otherwise drop the fragment, as we have
7187 7187                   * probably pruned the packet already (since the
7188 7188                   * packet cannot be found).
7189 7189                   */
7190 7190                  if (pruned && offset != 0) {
7191 7191                          mutex_exit(&ipfb->ipfb_lock);
7192 7192                          freemsg(mp);
7193 7193                          return (NULL);
7194 7194                  }
7195 7195  
7196 7196                  if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst))  {
7197 7197                          /*
7198 7198                           * Too many fragmented packets in this hash
7199 7199                           * bucket. Free the oldest.
7200 7200                           */
7201 7201                          ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
7202 7202                  }
7203 7203  
7204 7204                  /* New guy.  Allocate a frag message. */
7205 7205                  mp1 = allocb(sizeof (*ipf), BPRI_MED);
7206 7206                  if (mp1 == NULL) {
7207 7207                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7208 7208                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7209 7209                          freemsg(mp);
7210 7210  reass_done:
7211 7211                          mutex_exit(&ipfb->ipfb_lock);
7212 7212                          return (NULL);
7213 7213                  }
7214 7214  
7215 7215                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
7216 7216                  mp1->b_cont = mp;
7217 7217  
7218 7218                  /* Initialize the fragment header. */
7219 7219                  ipf = (ipf_t *)mp1->b_rptr;
7220 7220                  ipf->ipf_mp = mp1;
7221 7221                  ipf->ipf_ptphn = ipfp;
7222 7222                  ipfp[0] = ipf;
7223 7223                  ipf->ipf_hash_next = NULL;
7224 7224                  ipf->ipf_ident = ident;
7225 7225                  ipf->ipf_protocol = proto;
7226 7226                  ipf->ipf_src = src;
7227 7227                  ipf->ipf_dst = dst;
7228 7228                  ipf->ipf_nf_hdr_len = 0;
7229 7229                  /* Record reassembly start time. */
7230 7230                  ipf->ipf_timestamp = gethrestime_sec();
7231 7231                  /* Record ipf generation and account for frag header */
7232 7232                  ipf->ipf_gen = ill->ill_ipf_gen++;
7233 7233                  ipf->ipf_count = MBLKSIZE(mp1);
7234 7234                  ipf->ipf_last_frag_seen = B_FALSE;
7235 7235                  ipf->ipf_ecn = ecn_info;
7236 7236                  ipf->ipf_num_dups = 0;
7237 7237                  ipfb->ipfb_frag_pkts++;
7238 7238                  ipf->ipf_checksum = 0;
7239 7239                  ipf->ipf_checksum_flags = 0;
7240 7240  
7241 7241                  /* Store checksum value in fragment header */
7242 7242                  if (sum_flags != 0) {
7243 7243                          sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7244 7244                          sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7245 7245                          ipf->ipf_checksum = sum_val;
7246 7246                          ipf->ipf_checksum_flags = sum_flags;
7247 7247                  }
7248 7248  
7249 7249                  /*
7250 7250                   * We handle reassembly two ways.  In the easy case,
7251 7251                   * where all the fragments show up in order, we do
7252 7252                   * minimal bookkeeping, and just clip new pieces on
7253 7253                   * the end.  If we ever see a hole, then we go off
7254 7254                   * to ip_reassemble which has to mark the pieces and
7255 7255                   * keep track of the number of holes, etc.  Obviously,
7256 7256                   * the point of having both mechanisms is so we can
7257 7257                   * handle the easy case as efficiently as possible.
7258 7258                   */
7259 7259                  if (offset == 0) {
7260 7260                          /* Easy case, in-order reassembly so far. */
7261 7261                          ipf->ipf_count += msg_len;
7262 7262                          ipf->ipf_tail_mp = tail_mp;
7263 7263                          /*
7264 7264                           * Keep track of next expected offset in
7265 7265                           * ipf_end.
7266 7266                           */
7267 7267                          ipf->ipf_end = end;
7268 7268                          ipf->ipf_nf_hdr_len = hdr_length;
7269 7269                  } else {
7270 7270                          /* Hard case, hole at the beginning. */
7271 7271                          ipf->ipf_tail_mp = NULL;
7272 7272                          /*
7273 7273                           * ipf_end == 0 means that we have given up
7274 7274                           * on easy reassembly.
7275 7275                           */
7276 7276                          ipf->ipf_end = 0;
7277 7277  
7278 7278                          /* Forget checksum offload from now on */
7279 7279                          ipf->ipf_checksum_flags = 0;
7280 7280  
7281 7281                          /*
7282 7282                           * ipf_hole_cnt is set by ip_reassemble.
7283 7283                           * ipf_count is updated by ip_reassemble.
7284 7284                           * No need to check for return value here
7285 7285                           * as we don't expect reassembly to complete
7286 7286                           * or fail for the first fragment itself.
7287 7287                           */
7288 7288                          (void) ip_reassemble(mp, ipf,
7289 7289                              (frag_offset_flags & IPH_OFFSET) << 3,
7290 7290                              (frag_offset_flags & IPH_MF), ill, msg_len);
7291 7291                  }
7292 7292                  /* Update per ipfb and ill byte counts */
7293 7293                  ipfb->ipfb_count += ipf->ipf_count;
7294 7294                  ASSERT(ipfb->ipfb_count > 0);   /* Wraparound */
7295 7295                  atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7296 7296                  /* If the frag timer wasn't already going, start it. */
7297 7297                  mutex_enter(&ill->ill_lock);
7298 7298                  ill_frag_timer_start(ill);
7299 7299                  mutex_exit(&ill->ill_lock);
7300 7300                  goto reass_done;
7301 7301          }
7302 7302  
7303 7303          /*
7304 7304           * If the packet's flag has changed (it could be coming up
7305 7305           * from an interface different than the previous, therefore
7306 7306           * possibly different checksum capability), then forget about
7307 7307           * any stored checksum states.  Otherwise add the value to
7308 7308           * the existing one stored in the fragment header.
7309 7309           */
7310 7310          if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7311 7311                  sum_val += ipf->ipf_checksum;
7312 7312                  sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7313 7313                  sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7314 7314                  ipf->ipf_checksum = sum_val;
7315 7315          } else if (ipf->ipf_checksum_flags != 0) {
7316 7316                  /* Forget checksum offload from now on */
7317 7317                  ipf->ipf_checksum_flags = 0;
7318 7318          }
7319 7319  
7320 7320          /*
7321 7321           * We have a new piece of a datagram which is already being
7322 7322           * reassembled.  Update the ECN info if all IP fragments
7323 7323           * are ECN capable.  If there is one which is not, clear
7324 7324           * all the info.  If there is at least one which has CE
7325 7325           * code point, IP needs to report that up to transport.
7326 7326           */
7327 7327          if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7328 7328                  if (ecn_info == IPH_ECN_CE)
7329 7329                          ipf->ipf_ecn = IPH_ECN_CE;
7330 7330          } else {
7331 7331                  ipf->ipf_ecn = IPH_ECN_NECT;
7332 7332          }
7333 7333          if (offset && ipf->ipf_end == offset) {
7334 7334                  /* The new fragment fits at the end */
7335 7335                  ipf->ipf_tail_mp->b_cont = mp;
7336 7336                  /* Update the byte count */
7337 7337                  ipf->ipf_count += msg_len;
7338 7338                  /* Update per ipfb and ill byte counts */
7339 7339                  ipfb->ipfb_count += msg_len;
7340 7340                  ASSERT(ipfb->ipfb_count > 0);   /* Wraparound */
7341 7341                  atomic_add_32(&ill->ill_frag_count, msg_len);
7342 7342                  if (frag_offset_flags & IPH_MF) {
7343 7343                          /* More to come. */
7344 7344                          ipf->ipf_end = end;
7345 7345                          ipf->ipf_tail_mp = tail_mp;
7346 7346                          goto reass_done;
7347 7347                  }
7348 7348          } else {
7349 7349                  /* Go do the hard cases. */
7350 7350                  int ret;
7351 7351  
7352 7352                  if (offset == 0)
7353 7353                          ipf->ipf_nf_hdr_len = hdr_length;
7354 7354  
7355 7355                  /* Save current byte count */
7356 7356                  count = ipf->ipf_count;
7357 7357                  ret = ip_reassemble(mp, ipf,
7358 7358                      (frag_offset_flags & IPH_OFFSET) << 3,
7359 7359                      (frag_offset_flags & IPH_MF), ill, msg_len);
7360 7360                  /* Count of bytes added and subtracted (freeb()ed) */
7361 7361                  count = ipf->ipf_count - count;
7362 7362                  if (count) {
7363 7363                          /* Update per ipfb and ill byte counts */
7364 7364                          ipfb->ipfb_count += count;
7365 7365                          ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7366 7366                          atomic_add_32(&ill->ill_frag_count, count);
7367 7367                  }
7368 7368                  if (ret == IP_REASS_PARTIAL) {
7369 7369                          goto reass_done;
7370 7370                  } else if (ret == IP_REASS_FAILED) {
7371 7371                          /* Reassembly failed. Free up all resources */
7372 7372                          ill_frag_free_pkts(ill, ipfb, ipf, 1);
7373 7373                          for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7374 7374                                  IP_REASS_SET_START(t_mp, 0);
7375 7375                                  IP_REASS_SET_END(t_mp, 0);
7376 7376                          }
7377 7377                          freemsg(mp);
7378 7378                          goto reass_done;
7379 7379                  }
7380 7380                  /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7381 7381          }
7382 7382          /*
7383 7383           * We have completed reassembly.  Unhook the frag header from
7384 7384           * the reassembly list.
7385 7385           *
7386 7386           * Before we free the frag header, record the ECN info
7387 7387           * to report back to the transport.
7388 7388           */
7389 7389          ecn_info = ipf->ipf_ecn;
7390 7390          BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7391 7391          ipfp = ipf->ipf_ptphn;
7392 7392  
7393 7393          /* We need to supply these to caller */
7394 7394          if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7395 7395                  sum_val = ipf->ipf_checksum;
7396 7396          else
7397 7397                  sum_val = 0;
7398 7398  
7399 7399          mp1 = ipf->ipf_mp;
7400 7400          count = ipf->ipf_count;
7401 7401          ipf = ipf->ipf_hash_next;
7402 7402          if (ipf != NULL)
7403 7403                  ipf->ipf_ptphn = ipfp;
7404 7404          ipfp[0] = ipf;
7405 7405          atomic_add_32(&ill->ill_frag_count, -count);
7406 7406          ASSERT(ipfb->ipfb_count >= count);
7407 7407          ipfb->ipfb_count -= count;
7408 7408          ipfb->ipfb_frag_pkts--;
7409 7409          mutex_exit(&ipfb->ipfb_lock);
7410 7410          /* Ditch the frag header. */
7411 7411          mp = mp1->b_cont;
7412 7412  
7413 7413          freeb(mp1);
7414 7414  
7415 7415          /* Restore original IP length in header. */
7416 7416          packet_size = (uint32_t)msgdsize(mp);
7417 7417          if (packet_size > IP_MAXPACKET) {
7418 7418                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7419 7419                  ip_drop_input("Reassembled packet too large", mp, ill);
7420 7420                  freemsg(mp);
7421 7421                  return (NULL);
7422 7422          }
7423 7423  
7424 7424          if (DB_REF(mp) > 1) {
7425 7425                  mblk_t *mp2 = copymsg(mp);
7426 7426  
7427 7427                  if (mp2 == NULL) {
7428 7428                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7429 7429                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7430 7430                          freemsg(mp);
7431 7431                          return (NULL);
7432 7432                  }
7433 7433                  freemsg(mp);
7434 7434                  mp = mp2;
7435 7435          }
7436 7436          ipha = (ipha_t *)mp->b_rptr;
7437 7437  
7438 7438          ipha->ipha_length = htons((uint16_t)packet_size);
7439 7439          /* We're now complete, zip the frag state */
7440 7440          ipha->ipha_fragment_offset_and_flags = 0;
7441 7441          /* Record the ECN info. */
7442 7442          ipha->ipha_type_of_service &= 0xFC;
7443 7443          ipha->ipha_type_of_service |= ecn_info;
7444 7444  
7445 7445          /* Update the receive attributes */
7446 7446          ira->ira_pktlen = packet_size;
7447 7447          ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7448 7448  
7449 7449          /* Reassembly is successful; set checksum information in packet */
7450 7450          DB_CKSUM16(mp) = (uint16_t)sum_val;
7451 7451          DB_CKSUMFLAGS(mp) = sum_flags;
7452 7452          DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7453 7453  
7454 7454          return (mp);
7455 7455  }
7456 7456  
7457 7457  /*
7458 7458   * Pullup function that should be used for IP input in order to
7459 7459   * ensure we do not loose the L2 source address; we need the l2 source
7460 7460   * address for IP_RECVSLLA and for ndp_input.
7461 7461   *
7462 7462   * We return either NULL or b_rptr.
7463 7463   */
7464 7464  void *
7465 7465  ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7466 7466  {
7467 7467          ill_t           *ill = ira->ira_ill;
7468 7468  
7469 7469          if (ip_rput_pullups++ == 0) {
7470 7470                  (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7471 7471                      "ip_pullup: %s forced us to "
7472 7472                      " pullup pkt, hdr len %ld, hdr addr %p",
7473 7473                      ill->ill_name, len, (void *)mp->b_rptr);
7474 7474          }
7475 7475          if (!(ira->ira_flags & IRAF_L2SRC_SET))
7476 7476                  ip_setl2src(mp, ira, ira->ira_rill);
7477 7477          ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7478 7478          if (!pullupmsg(mp, len))
7479 7479                  return (NULL);
7480 7480          else
7481 7481                  return (mp->b_rptr);
7482 7482  }
7483 7483  
7484 7484  /*
7485 7485   * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7486 7486   * When called from the ULP ira_rill will be NULL hence the caller has to
7487 7487   * pass in the ill.
7488 7488   */
7489 7489  /* ARGSUSED */
7490 7490  void
7491 7491  ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7492 7492  {
7493 7493          const uchar_t *addr;
7494 7494          int alen;
7495 7495  
7496 7496          if (ira->ira_flags & IRAF_L2SRC_SET)
7497 7497                  return;
7498 7498  
7499 7499          ASSERT(ill != NULL);
7500 7500          alen = ill->ill_phys_addr_length;
7501 7501          ASSERT(alen <= sizeof (ira->ira_l2src));
7502 7502          if (ira->ira_mhip != NULL &&
7503 7503              (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7504 7504                  bcopy(addr, ira->ira_l2src, alen);
7505 7505          } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7506 7506              (addr = ill->ill_phys_addr) != NULL) {
7507 7507                  bcopy(addr, ira->ira_l2src, alen);
7508 7508          } else {
7509 7509                  bzero(ira->ira_l2src, alen);
7510 7510          }
7511 7511          ira->ira_flags |= IRAF_L2SRC_SET;
7512 7512  }
7513 7513  
7514 7514  /*
7515 7515   * check ip header length and align it.
7516 7516   */
7517 7517  mblk_t *
7518 7518  ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7519 7519  {
7520 7520          ill_t   *ill = ira->ira_ill;
7521 7521          ssize_t len;
7522 7522  
7523 7523          len = MBLKL(mp);
7524 7524  
7525 7525          if (!OK_32PTR(mp->b_rptr))
7526 7526                  IP_STAT(ill->ill_ipst, ip_notaligned);
7527 7527          else
7528 7528                  IP_STAT(ill->ill_ipst, ip_recv_pullup);
7529 7529  
7530 7530          /* Guard against bogus device drivers */
7531 7531          if (len < 0) {
7532 7532                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7533 7533                  ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7534 7534                  freemsg(mp);
7535 7535                  return (NULL);
7536 7536          }
7537 7537  
7538 7538          if (len == 0) {
7539 7539                  /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7540 7540                  mblk_t *mp1 = mp->b_cont;
7541 7541  
7542 7542                  if (!(ira->ira_flags & IRAF_L2SRC_SET))
7543 7543                          ip_setl2src(mp, ira, ira->ira_rill);
7544 7544                  ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7545 7545  
7546 7546                  freeb(mp);
7547 7547                  mp = mp1;
7548 7548                  if (mp == NULL)
7549 7549                          return (NULL);
7550 7550  
7551 7551                  if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7552 7552                          return (mp);
7553 7553          }
7554 7554          if (ip_pullup(mp, min_size, ira) == NULL) {
7555 7555                  if (msgdsize(mp) < min_size) {
7556 7556                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7557 7557                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7558 7558                  } else {
7559 7559                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7560 7560                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7561 7561                  }
7562 7562                  freemsg(mp);
7563 7563                  return (NULL);
7564 7564          }
7565 7565          return (mp);
7566 7566  }
7567 7567  
7568 7568  /*
7569 7569   * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7570 7570   */
7571 7571  mblk_t *
7572 7572  ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7573 7573      uint_t min_size, ip_recv_attr_t *ira)
7574 7574  {
7575 7575          ill_t   *ill = ira->ira_ill;
7576 7576  
7577 7577          /*
7578 7578           * Make sure we have data length consistent
7579 7579           * with the IP header.
7580 7580           */
7581 7581          if (mp->b_cont == NULL) {
7582 7582                  /* pkt_len is based on ipha_len, not the mblk length */
7583 7583                  if (pkt_len < min_size) {
7584 7584                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7585 7585                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7586 7586                          freemsg(mp);
7587 7587                          return (NULL);
7588 7588                  }
7589 7589                  if (len < 0) {
7590 7590                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7591 7591                          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7592 7592                          freemsg(mp);
7593 7593                          return (NULL);
7594 7594                  }
7595 7595                  /* Drop any pad */
7596 7596                  mp->b_wptr = rptr + pkt_len;
7597 7597          } else if ((len += msgdsize(mp->b_cont)) != 0) {
7598 7598                  ASSERT(pkt_len >= min_size);
7599 7599                  if (pkt_len < min_size) {
7600 7600                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7601 7601                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7602 7602                          freemsg(mp);
7603 7603                          return (NULL);
7604 7604                  }
7605 7605                  if (len < 0) {
7606 7606                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7607 7607                          ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7608 7608                          freemsg(mp);
7609 7609                          return (NULL);
7610 7610                  }
7611 7611                  /* Drop any pad */
7612 7612                  (void) adjmsg(mp, -len);
7613 7613                  /*
7614 7614                   * adjmsg may have freed an mblk from the chain, hence
7615 7615                   * invalidate any hw checksum here. This will force IP to
7616 7616                   * calculate the checksum in sw, but only for this packet.
7617 7617                   */
7618 7618                  DB_CKSUMFLAGS(mp) = 0;
7619 7619                  IP_STAT(ill->ill_ipst, ip_multimblk);
7620 7620          }
7621 7621          return (mp);
7622 7622  }
7623 7623  
7624 7624  /*
7625 7625   * Check that the IPv4 opt_len is consistent with the packet and pullup
7626 7626   * the options.
7627 7627   */
7628 7628  mblk_t *
7629 7629  ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7630 7630      ip_recv_attr_t *ira)
7631 7631  {
7632 7632          ill_t   *ill = ira->ira_ill;
7633 7633          ssize_t len;
7634 7634  
7635 7635          /* Assume no IPv6 packets arrive over the IPv4 queue */
7636 7636          if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7637 7637                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7638 7638                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7639 7639                  ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7640 7640                  freemsg(mp);
7641 7641                  return (NULL);
7642 7642          }
7643 7643  
7644 7644          if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7645 7645                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7646 7646                  ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7647 7647                  freemsg(mp);
7648 7648                  return (NULL);
7649 7649          }
7650 7650          /*
7651 7651           * Recompute complete header length and make sure we
7652 7652           * have access to all of it.
7653 7653           */
7654 7654          len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7655 7655          if (len > (mp->b_wptr - mp->b_rptr)) {
7656 7656                  if (len > pkt_len) {
7657 7657                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7658 7658                          ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7659 7659                          freemsg(mp);
7660 7660                          return (NULL);
7661 7661                  }
7662 7662                  if (ip_pullup(mp, len, ira) == NULL) {
7663 7663                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7664 7664                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
7665 7665                          freemsg(mp);
7666 7666                          return (NULL);
7667 7667                  }
7668 7668          }
7669 7669          return (mp);
7670 7670  }
7671 7671  
7672 7672  /*
7673 7673   * Returns a new ire, or the same ire, or NULL.
7674 7674   * If a different IRE is returned, then it is held; the caller
7675 7675   * needs to release it.
7676 7676   * In no case is there any hold/release on the ire argument.
7677 7677   */
7678 7678  ire_t *
7679 7679  ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7680 7680  {
7681 7681          ire_t           *new_ire;
7682 7682          ill_t           *ire_ill;
7683 7683          uint_t          ifindex;
7684 7684          ip_stack_t      *ipst = ill->ill_ipst;
7685 7685          boolean_t       strict_check = B_FALSE;
7686 7686  
7687 7687          /*
7688 7688           * IPMP common case: if IRE and ILL are in the same group, there's no
7689 7689           * issue (e.g. packet received on an underlying interface matched an
7690 7690           * IRE_LOCAL on its associated group interface).
7691 7691           */
7692 7692          ASSERT(ire->ire_ill != NULL);
7693 7693          if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7694 7694                  return (ire);
7695 7695  
7696 7696          /*
7697 7697           * Do another ire lookup here, using the ingress ill, to see if the
7698 7698           * interface is in a usesrc group.
7699 7699           * As long as the ills belong to the same group, we don't consider
7700 7700           * them to be arriving on the wrong interface. Thus, if the switch
7701 7701           * is doing inbound load spreading, we won't drop packets when the
7702 7702           * ip*_strict_dst_multihoming switch is on.
7703 7703           * We also need to check for IPIF_UNNUMBERED point2point interfaces
7704 7704           * where the local address may not be unique. In this case we were
7705 7705           * at the mercy of the initial ire lookup and the IRE_LOCAL it
7706 7706           * actually returned. The new lookup, which is more specific, should
7707 7707           * only find the IRE_LOCAL associated with the ingress ill if one
7708 7708           * exists.
7709 7709           */
7710 7710          if (ire->ire_ipversion == IPV4_VERSION) {
7711 7711                  if (ipst->ips_ip_strict_dst_multihoming)
7712 7712                          strict_check = B_TRUE;
7713 7713                  new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7714 7714                      IRE_LOCAL, ill, ALL_ZONES, NULL,
7715 7715                      (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7716 7716          } else {
7717 7717                  ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7718 7718                  if (ipst->ips_ipv6_strict_dst_multihoming)
7719 7719                          strict_check = B_TRUE;
7720 7720                  new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7721 7721                      IRE_LOCAL, ill, ALL_ZONES, NULL,
7722 7722                      (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7723 7723          }
7724 7724          /*
7725 7725           * If the same ire that was returned in ip_input() is found then this
7726 7726           * is an indication that usesrc groups are in use. The packet
7727 7727           * arrived on a different ill in the group than the one associated with
7728 7728           * the destination address.  If a different ire was found then the same
7729 7729           * IP address must be hosted on multiple ills. This is possible with
7730 7730           * unnumbered point2point interfaces. We switch to use this new ire in
7731 7731           * order to have accurate interface statistics.
7732 7732           */
7733 7733          if (new_ire != NULL) {
7734 7734                  /* Note: held in one case but not the other? Caller handles */
7735 7735                  if (new_ire != ire)
7736 7736                          return (new_ire);
7737 7737                  /* Unchanged */
7738 7738                  ire_refrele(new_ire);
7739 7739                  return (ire);
7740 7740          }
7741 7741  
7742 7742          /*
7743 7743           * Chase pointers once and store locally.
7744 7744           */
7745 7745          ASSERT(ire->ire_ill != NULL);
7746 7746          ire_ill = ire->ire_ill;
7747 7747          ifindex = ill->ill_usesrc_ifindex;
7748 7748  
7749 7749          /*
7750 7750           * Check if it's a legal address on the 'usesrc' interface.
7751 7751           * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7752 7752           * can just check phyint_ifindex.
7753 7753           */
7754 7754          if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7755 7755                  return (ire);
7756 7756          }
7757 7757  
7758 7758          /*
7759 7759           * If the ip*_strict_dst_multihoming switch is on then we can
7760 7760           * only accept this packet if the interface is marked as routing.
7761 7761           */
7762 7762          if (!(strict_check))
7763 7763                  return (ire);
7764 7764  
7765 7765          if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7766 7766                  return (ire);
7767 7767          }
7768 7768          return (NULL);
7769 7769  }
7770 7770  
7771 7771  /*
7772 7772   * This function is used to construct a mac_header_info_s from a
7773 7773   * DL_UNITDATA_IND message.
7774 7774   * The address fields in the mhi structure points into the message,
7775 7775   * thus the caller can't use those fields after freeing the message.
7776 7776   *
7777 7777   * We determine whether the packet received is a non-unicast packet
7778 7778   * and in doing so, determine whether or not it is broadcast vs multicast.
7779 7779   * For it to be a broadcast packet, we must have the appropriate mblk_t
7780 7780   * hanging off the ill_t.  If this is either not present or doesn't match
7781 7781   * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7782 7782   * to be multicast.  Thus NICs that have no broadcast address (or no
7783 7783   * capability for one, such as point to point links) cannot return as
7784 7784   * the packet being broadcast.
7785 7785   */
7786 7786  void
7787 7787  ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7788 7788  {
7789 7789          dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7790 7790          mblk_t *bmp;
7791 7791          uint_t extra_offset;
7792 7792  
7793 7793          bzero(mhip, sizeof (struct mac_header_info_s));
7794 7794  
7795 7795          mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7796 7796  
7797 7797          if (ill->ill_sap_length < 0)
7798 7798                  extra_offset = 0;
7799 7799          else
7800 7800                  extra_offset = ill->ill_sap_length;
7801 7801  
7802 7802          mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7803 7803              extra_offset;
7804 7804          mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7805 7805              extra_offset;
7806 7806  
7807 7807          if (!ind->dl_group_address)
7808 7808                  return;
7809 7809  
7810 7810          /* Multicast or broadcast */
7811 7811          mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7812 7812  
7813 7813          if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7814 7814              ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7815 7815              (bmp = ill->ill_bcast_mp) != NULL) {
7816 7816                  dl_unitdata_req_t *dlur;
7817 7817                  uint8_t *bphys_addr;
7818 7818  
7819 7819                  dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7820 7820                  bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7821 7821                      extra_offset;
7822 7822  
7823 7823                  if (bcmp(mhip->mhi_daddr, bphys_addr,
7824 7824                      ind->dl_dest_addr_length) == 0)
7825 7825                          mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7826 7826          }
7827 7827  }
7828 7828  
7829 7829  /*
7830 7830   * This function is used to construct a mac_header_info_s from a
7831 7831   * M_DATA fastpath message from a DLPI driver.
7832 7832   * The address fields in the mhi structure points into the message,
7833 7833   * thus the caller can't use those fields after freeing the message.
7834 7834   *
7835 7835   * We determine whether the packet received is a non-unicast packet
7836 7836   * and in doing so, determine whether or not it is broadcast vs multicast.
7837 7837   * For it to be a broadcast packet, we must have the appropriate mblk_t
7838 7838   * hanging off the ill_t.  If this is either not present or doesn't match
7839 7839   * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7840 7840   * to be multicast.  Thus NICs that have no broadcast address (or no
7841 7841   * capability for one, such as point to point links) cannot return as
7842 7842   * the packet being broadcast.
7843 7843   */
7844 7844  void
7845 7845  ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7846 7846  {
7847 7847          mblk_t *bmp;
7848 7848          struct ether_header *pether;
7849 7849  
7850 7850          bzero(mhip, sizeof (struct mac_header_info_s));
7851 7851  
7852 7852          mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7853 7853  
7854 7854          pether = (struct ether_header *)((char *)mp->b_rptr
7855 7855              - sizeof (struct ether_header));
7856 7856  
7857 7857          /*
7858 7858           * Make sure the interface is an ethernet type, since we don't
7859 7859           * know the header format for anything but Ethernet. Also make
7860 7860           * sure we are pointing correctly above db_base.
7861 7861           */
7862 7862          if (ill->ill_type != IFT_ETHER)
7863 7863                  return;
7864 7864  
7865 7865  retry:
7866 7866          if ((uchar_t *)pether < mp->b_datap->db_base)
7867 7867                  return;
7868 7868  
7869 7869          /* Is there a VLAN tag? */
7870 7870          if (ill->ill_isv6) {
7871 7871                  if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7872 7872                          pether = (struct ether_header *)((char *)pether - 4);
7873 7873                          goto retry;
7874 7874                  }
7875 7875          } else {
7876 7876                  if (pether->ether_type != htons(ETHERTYPE_IP)) {
7877 7877                          pether = (struct ether_header *)((char *)pether - 4);
7878 7878                          goto retry;
7879 7879                  }
7880 7880          }
7881 7881          mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7882 7882          mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7883 7883  
7884 7884          if (!(mhip->mhi_daddr[0] & 0x01))
7885 7885                  return;
7886 7886  
7887 7887          /* Multicast or broadcast */
7888 7888          mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7889 7889  
7890 7890          if ((bmp = ill->ill_bcast_mp) != NULL) {
7891 7891                  dl_unitdata_req_t *dlur;
7892 7892                  uint8_t *bphys_addr;
7893 7893                  uint_t  addrlen;
7894 7894  
7895 7895                  dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7896 7896                  addrlen = dlur->dl_dest_addr_length;
7897 7897                  if (ill->ill_sap_length < 0) {
7898 7898                          bphys_addr = (uchar_t *)dlur +
7899 7899                              dlur->dl_dest_addr_offset;
7900 7900                          addrlen += ill->ill_sap_length;
7901 7901                  } else {
7902 7902                          bphys_addr = (uchar_t *)dlur +
7903 7903                              dlur->dl_dest_addr_offset +
7904 7904                              ill->ill_sap_length;
7905 7905                          addrlen -= ill->ill_sap_length;
7906 7906                  }
7907 7907                  if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7908 7908                          mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7909 7909          }
7910 7910  }
7911 7911  
7912 7912  /*
7913 7913   * Handle anything but M_DATA messages
7914 7914   * We see the DL_UNITDATA_IND which are part
7915 7915   * of the data path, and also the other messages from the driver.
7916 7916   */
7917 7917  void
7918 7918  ip_rput_notdata(ill_t *ill, mblk_t *mp)
7919 7919  {
7920 7920          mblk_t          *first_mp;
7921 7921          struct iocblk   *iocp;
7922 7922          struct mac_header_info_s mhi;
7923 7923  
7924 7924          switch (DB_TYPE(mp)) {
7925 7925          case M_PROTO:
7926 7926          case M_PCPROTO: {
7927 7927                  if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7928 7928                      DL_UNITDATA_IND) {
7929 7929                          /* Go handle anything other than data elsewhere. */
7930 7930                          ip_rput_dlpi(ill, mp);
7931 7931                          return;
7932 7932                  }
7933 7933  
7934 7934                  first_mp = mp;
7935 7935                  mp = first_mp->b_cont;
7936 7936                  first_mp->b_cont = NULL;
7937 7937  
7938 7938                  if (mp == NULL) {
7939 7939                          freeb(first_mp);
7940 7940                          return;
7941 7941                  }
7942 7942                  ip_dlur_to_mhi(ill, first_mp, &mhi);
7943 7943                  if (ill->ill_isv6)
7944 7944                          ip_input_v6(ill, NULL, mp, &mhi);
7945 7945                  else
7946 7946                          ip_input(ill, NULL, mp, &mhi);
7947 7947  
7948 7948                  /* Ditch the DLPI header. */
7949 7949                  freeb(first_mp);
7950 7950                  return;
7951 7951          }
7952 7952          case M_IOCACK:
7953 7953                  iocp = (struct iocblk *)mp->b_rptr;
7954 7954                  switch (iocp->ioc_cmd) {
7955 7955                  case DL_IOC_HDR_INFO:
7956 7956                          ill_fastpath_ack(ill, mp);
7957 7957                          return;
7958 7958                  default:
7959 7959                          putnext(ill->ill_rq, mp);
7960 7960                          return;
7961 7961                  }
7962 7962                  /* FALLTHROUGH */
7963 7963          case M_ERROR:
7964 7964          case M_HANGUP:
7965 7965                  mutex_enter(&ill->ill_lock);
7966 7966                  if (ill->ill_state_flags & ILL_CONDEMNED) {
7967 7967                          mutex_exit(&ill->ill_lock);
7968 7968                          freemsg(mp);
7969 7969                          return;
7970 7970                  }
7971 7971                  ill_refhold_locked(ill);
7972 7972                  mutex_exit(&ill->ill_lock);
7973 7973                  qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7974 7974                      B_FALSE);
7975 7975                  return;
7976 7976          case M_CTL:
7977 7977                  putnext(ill->ill_rq, mp);
7978 7978                  return;
7979 7979          case M_IOCNAK:
7980 7980                  ip1dbg(("got iocnak "));
7981 7981                  iocp = (struct iocblk *)mp->b_rptr;
7982 7982                  switch (iocp->ioc_cmd) {
7983 7983                  case DL_IOC_HDR_INFO:
7984 7984                          ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7985 7985                          return;
7986 7986                  default:
7987 7987                          break;
7988 7988                  }
7989 7989                  /* FALLTHROUGH */
7990 7990          default:
7991 7991                  putnext(ill->ill_rq, mp);
7992 7992                  return;
7993 7993          }
7994 7994  }
7995 7995  
7996 7996  /* Read side put procedure.  Packets coming from the wire arrive here. */
7997 7997  int
7998 7998  ip_rput(queue_t *q, mblk_t *mp)
7999 7999  {
8000 8000          ill_t   *ill;
8001 8001          union DL_primitives *dl;
8002 8002  
8003 8003          ill = (ill_t *)q->q_ptr;
8004 8004  
8005 8005          if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
8006 8006                  /*
8007 8007                   * If things are opening or closing, only accept high-priority
8008 8008                   * DLPI messages.  (On open ill->ill_ipif has not yet been
8009 8009                   * created; on close, things hanging off the ill may have been
8010 8010                   * freed already.)
8011 8011                   */
8012 8012                  dl = (union DL_primitives *)mp->b_rptr;
8013 8013                  if (DB_TYPE(mp) != M_PCPROTO ||
8014 8014                      dl->dl_primitive == DL_UNITDATA_IND) {
8015 8015                          inet_freemsg(mp);
8016 8016                          return (0);
8017 8017                  }
8018 8018          }
8019 8019          if (DB_TYPE(mp) == M_DATA) {
8020 8020                  struct mac_header_info_s mhi;
8021 8021  
8022 8022                  ip_mdata_to_mhi(ill, mp, &mhi);
8023 8023                  ip_input(ill, NULL, mp, &mhi);
8024 8024          } else {
8025 8025                  ip_rput_notdata(ill, mp);
8026 8026          }
8027 8027          return (0);
8028 8028  }
8029 8029  
8030 8030  /*
8031 8031   * Move the information to a copy.
8032 8032   */
8033 8033  mblk_t *
8034 8034  ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
8035 8035  {
8036 8036          mblk_t          *mp1;
8037 8037          ill_t           *ill = ira->ira_ill;
8038 8038          ip_stack_t      *ipst = ill->ill_ipst;
8039 8039  
8040 8040          IP_STAT(ipst, ip_db_ref);
8041 8041  
8042 8042          /* Make sure we have ira_l2src before we loose the original mblk */
8043 8043          if (!(ira->ira_flags & IRAF_L2SRC_SET))
8044 8044                  ip_setl2src(mp, ira, ira->ira_rill);
8045 8045  
8046 8046          mp1 = copymsg(mp);
8047 8047          if (mp1 == NULL) {
8048 8048                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8049 8049                  ip_drop_input("ipIfStatsInDiscards", mp, ill);
8050 8050                  freemsg(mp);
8051 8051                  return (NULL);
8052 8052          }
8053 8053          /* preserve the hardware checksum flags and data, if present */
8054 8054          if (DB_CKSUMFLAGS(mp) != 0) {
8055 8055                  DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
8056 8056                  DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
8057 8057                  DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
8058 8058                  DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
8059 8059                  DB_CKSUM16(mp1) = DB_CKSUM16(mp);
8060 8060          }
8061 8061          freemsg(mp);
8062 8062          return (mp1);
8063 8063  }
8064 8064  
8065 8065  static void
8066 8066  ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
8067 8067      t_uscalar_t err)
8068 8068  {
8069 8069          if (dl_err == DL_SYSERR) {
8070 8070                  (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8071 8071                      "%s: %s failed: DL_SYSERR (errno %u)\n",
8072 8072                      ill->ill_name, dl_primstr(prim), err);
8073 8073                  return;
8074 8074          }
8075 8075  
8076 8076          (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8077 8077              "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
8078 8078              dl_errstr(dl_err));
8079 8079  }
8080 8080  
8081 8081  /*
8082 8082   * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
8083 8083   * than DL_UNITDATA_IND messages. If we need to process this message
8084 8084   * exclusively, we call qwriter_ip, in which case we also need to call
8085 8085   * ill_refhold before that, since qwriter_ip does an ill_refrele.
8086 8086   */
8087 8087  void
8088 8088  ip_rput_dlpi(ill_t *ill, mblk_t *mp)
8089 8089  {
8090 8090          dl_ok_ack_t     *dloa = (dl_ok_ack_t *)mp->b_rptr;
8091 8091          dl_error_ack_t  *dlea = (dl_error_ack_t *)dloa;
8092 8092          queue_t         *q = ill->ill_rq;
8093 8093          t_uscalar_t     prim = dloa->dl_primitive;
8094 8094          t_uscalar_t     reqprim = DL_PRIM_INVAL;
8095 8095  
8096 8096          DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
8097 8097              char *, dl_primstr(prim), ill_t *, ill);
8098 8098          ip1dbg(("ip_rput_dlpi"));
8099 8099  
8100 8100          /*
8101 8101           * If we received an ACK but didn't send a request for it, then it
8102 8102           * can't be part of any pending operation; discard up-front.
8103 8103           */
8104 8104          switch (prim) {
8105 8105          case DL_ERROR_ACK:
8106 8106                  reqprim = dlea->dl_error_primitive;
8107 8107                  ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
8108 8108                      "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
8109 8109                      reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
8110 8110                      dlea->dl_unix_errno));
8111 8111                  break;
8112 8112          case DL_OK_ACK:
8113 8113                  reqprim = dloa->dl_correct_primitive;
8114 8114                  break;
8115 8115          case DL_INFO_ACK:
8116 8116                  reqprim = DL_INFO_REQ;
8117 8117                  break;
8118 8118          case DL_BIND_ACK:
8119 8119                  reqprim = DL_BIND_REQ;
8120 8120                  break;
8121 8121          case DL_PHYS_ADDR_ACK:
8122 8122                  reqprim = DL_PHYS_ADDR_REQ;
8123 8123                  break;
8124 8124          case DL_NOTIFY_ACK:
8125 8125                  reqprim = DL_NOTIFY_REQ;
8126 8126                  break;
8127 8127          case DL_CAPABILITY_ACK:
8128 8128                  reqprim = DL_CAPABILITY_REQ;
8129 8129                  break;
8130 8130          }
8131 8131  
8132 8132          if (prim != DL_NOTIFY_IND) {
8133 8133                  if (reqprim == DL_PRIM_INVAL ||
8134 8134                      !ill_dlpi_pending(ill, reqprim)) {
8135 8135                          /* Not a DLPI message we support or expected */
8136 8136                          freemsg(mp);
8137 8137                          return;
8138 8138                  }
8139 8139                  ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
8140 8140                      dl_primstr(reqprim)));
8141 8141          }
8142 8142  
8143 8143          switch (reqprim) {
8144 8144          case DL_UNBIND_REQ:
8145 8145                  /*
8146 8146                   * NOTE: we mark the unbind as complete even if we got a
8147 8147                   * DL_ERROR_ACK, since there's not much else we can do.
8148 8148                   */
8149 8149                  mutex_enter(&ill->ill_lock);
8150 8150                  ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
8151 8151                  cv_signal(&ill->ill_cv);
8152 8152                  mutex_exit(&ill->ill_lock);
8153 8153                  break;
8154 8154  
8155 8155          case DL_ENABMULTI_REQ:
8156 8156                  if (prim == DL_OK_ACK) {
8157 8157                          if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8158 8158                                  ill->ill_dlpi_multicast_state = IDS_OK;
8159 8159                  }
8160 8160                  break;
8161 8161          }
8162 8162  
8163 8163          /*
8164 8164           * The message is one we're waiting for (or DL_NOTIFY_IND), but we
8165 8165           * need to become writer to continue to process it.  Because an
8166 8166           * exclusive operation doesn't complete until replies to all queued
8167 8167           * DLPI messages have been received, we know we're in the middle of an
8168 8168           * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
8169 8169           *
8170 8170           * As required by qwriter_ip(), we refhold the ill; it will refrele.
8171 8171           * Since this is on the ill stream we unconditionally bump up the
8172 8172           * refcount without doing ILL_CAN_LOOKUP().
8173 8173           */
8174 8174          ill_refhold(ill);
8175 8175          if (prim == DL_NOTIFY_IND)
8176 8176                  qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
8177 8177          else
8178 8178                  qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
8179 8179  }
8180 8180  
8181 8181  /*
8182 8182   * Handling of DLPI messages that require exclusive access to the ipsq.
8183 8183   *
8184 8184   * Need to do ipsq_pending_mp_get on ioctl completion, which could
8185 8185   * happen here. (along with mi_copy_done)
8186 8186   */
8187 8187  /* ARGSUSED */
8188 8188  static void
8189 8189  ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8190 8190  {
8191 8191          dl_ok_ack_t     *dloa = (dl_ok_ack_t *)mp->b_rptr;
8192 8192          dl_error_ack_t  *dlea = (dl_error_ack_t *)dloa;
8193 8193          int             err = 0;
8194 8194          ill_t           *ill = (ill_t *)q->q_ptr;
8195 8195          ipif_t          *ipif = NULL;
8196 8196          mblk_t          *mp1 = NULL;
8197 8197          conn_t          *connp = NULL;
8198 8198          t_uscalar_t     paddrreq;
8199 8199          mblk_t          *mp_hw;
8200 8200          boolean_t       success;
8201 8201          boolean_t       ioctl_aborted = B_FALSE;
8202 8202          boolean_t       log = B_TRUE;
8203 8203  
8204 8204          DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
8205 8205              char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
8206 8206  
8207 8207          ip1dbg(("ip_rput_dlpi_writer .."));
8208 8208          ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
8209 8209          ASSERT(IAM_WRITER_ILL(ill));
8210 8210  
8211 8211          ipif = ipsq->ipsq_xop->ipx_pending_ipif;
8212 8212          /*
8213 8213           * The current ioctl could have been aborted by the user and a new
8214 8214           * ioctl to bring up another ill could have started. We could still
8215 8215           * get a response from the driver later.
8216 8216           */
8217 8217          if (ipif != NULL && ipif->ipif_ill != ill)
8218 8218                  ioctl_aborted = B_TRUE;
8219 8219  
8220 8220          switch (dloa->dl_primitive) {
8221 8221          case DL_ERROR_ACK:
8222 8222                  ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
8223 8223                      dl_primstr(dlea->dl_error_primitive)));
8224 8224  
8225 8225                  DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
8226 8226                      char *, dl_primstr(dlea->dl_error_primitive),
8227 8227                      ill_t *, ill);
8228 8228  
8229 8229                  switch (dlea->dl_error_primitive) {
8230 8230                  case DL_DISABMULTI_REQ:
8231 8231                          ill_dlpi_done(ill, dlea->dl_error_primitive);
8232 8232                          break;
8233 8233                  case DL_PROMISCON_REQ:
8234 8234                  case DL_PROMISCOFF_REQ:
8235 8235                  case DL_UNBIND_REQ:
8236 8236                  case DL_ATTACH_REQ:
8237 8237                  case DL_INFO_REQ:
8238 8238                          ill_dlpi_done(ill, dlea->dl_error_primitive);
8239 8239                          break;
8240 8240                  case DL_NOTIFY_REQ:
8241 8241                          ill_dlpi_done(ill, DL_NOTIFY_REQ);
8242 8242                          log = B_FALSE;
8243 8243                          break;
8244 8244                  case DL_PHYS_ADDR_REQ:
8245 8245                          /*
8246 8246                           * For IPv6 only, there are two additional
8247 8247                           * phys_addr_req's sent to the driver to get the
8248 8248                           * IPv6 token and lla. This allows IP to acquire
8249 8249                           * the hardware address format for a given interface
8250 8250                           * without having built in knowledge of the hardware
8251 8251                           * address. ill_phys_addr_pend keeps track of the last
8252 8252                           * DL_PAR sent so we know which response we are
8253 8253                           * dealing with. ill_dlpi_done will update
8254 8254                           * ill_phys_addr_pend when it sends the next req.
8255 8255                           * We don't complete the IOCTL until all three DL_PARs
8256 8256                           * have been attempted, so set *_len to 0 and break.
8257 8257                           */
8258 8258                          paddrreq = ill->ill_phys_addr_pend;
8259 8259                          ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8260 8260                          if (paddrreq == DL_IPV6_TOKEN) {
8261 8261                                  ill->ill_token_length = 0;
8262 8262                                  log = B_FALSE;
8263 8263                                  break;
8264 8264                          } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8265 8265                                  ill->ill_nd_lla_len = 0;
8266 8266                                  log = B_FALSE;
8267 8267                                  break;
8268 8268                          }
8269 8269                          /*
8270 8270                           * Something went wrong with the DL_PHYS_ADDR_REQ.
8271 8271                           * We presumably have an IOCTL hanging out waiting
8272 8272                           * for completion. Find it and complete the IOCTL
8273 8273                           * with the error noted.
8274 8274                           * However, ill_dl_phys was called on an ill queue
8275 8275                           * (from SIOCSLIFNAME), thus conn_pending_ill is not
8276 8276                           * set. But the ioctl is known to be pending on ill_wq.
8277 8277                           */
8278 8278                          if (!ill->ill_ifname_pending)
8279 8279                                  break;
8280 8280                          ill->ill_ifname_pending = 0;
8281 8281                          if (!ioctl_aborted)
8282 8282                                  mp1 = ipsq_pending_mp_get(ipsq, &connp);
8283 8283                          if (mp1 != NULL) {
8284 8284                                  /*
8285 8285                                   * This operation (SIOCSLIFNAME) must have
8286 8286                                   * happened on the ill. Assert there is no conn
8287 8287                                   */
8288 8288                                  ASSERT(connp == NULL);
8289 8289                                  q = ill->ill_wq;
8290 8290                          }
8291 8291                          break;
8292 8292                  case DL_BIND_REQ:
8293 8293                          ill_dlpi_done(ill, DL_BIND_REQ);
8294 8294                          if (ill->ill_ifname_pending)
8295 8295                                  break;
8296 8296                          mutex_enter(&ill->ill_lock);
8297 8297                          ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8298 8298                          mutex_exit(&ill->ill_lock);
8299 8299                          /*
8300 8300                           * Something went wrong with the bind.  We presumably
8301 8301                           * have an IOCTL hanging out waiting for completion.
8302 8302                           * Find it, take down the interface that was coming
8303 8303                           * up, and complete the IOCTL with the error noted.
8304 8304                           */
8305 8305                          if (!ioctl_aborted)
8306 8306                                  mp1 = ipsq_pending_mp_get(ipsq, &connp);
8307 8307                          if (mp1 != NULL) {
8308 8308                                  /*
8309 8309                                   * This might be a result of a DL_NOTE_REPLUMB
8310 8310                                   * notification. In that case, connp is NULL.
8311 8311                                   */
8312 8312                                  if (connp != NULL)
8313 8313                                          q = CONNP_TO_WQ(connp);
8314 8314  
8315 8315                                  (void) ipif_down(ipif, NULL, NULL);
8316 8316                                  /* error is set below the switch */
8317 8317                          }
8318 8318                          break;
8319 8319                  case DL_ENABMULTI_REQ:
8320 8320                          ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8321 8321  
8322 8322                          if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8323 8323                                  ill->ill_dlpi_multicast_state = IDS_FAILED;
8324 8324                          if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8325 8325  
8326 8326                                  printf("ip: joining multicasts failed (%d)"
8327 8327                                      " on %s - will use link layer "
8328 8328                                      "broadcasts for multicast\n",
8329 8329                                      dlea->dl_errno, ill->ill_name);
8330 8330  
8331 8331                                  /*
8332 8332                                   * Set up for multi_bcast; We are the
8333 8333                                   * writer, so ok to access ill->ill_ipif
8334 8334                                   * without any lock.
8335 8335                                   */
8336 8336                                  mutex_enter(&ill->ill_phyint->phyint_lock);
8337 8337                                  ill->ill_phyint->phyint_flags |=
8338 8338                                      PHYI_MULTI_BCAST;
8339 8339                                  mutex_exit(&ill->ill_phyint->phyint_lock);
8340 8340  
8341 8341                          }
8342 8342                          freemsg(mp);    /* Don't want to pass this up */
8343 8343                          return;
8344 8344                  case DL_CAPABILITY_REQ:
8345 8345                          ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8346 8346                              "DL_CAPABILITY REQ\n"));
8347 8347                          if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8348 8348                                  ill->ill_dlpi_capab_state = IDCS_FAILED;
8349 8349                          ill_capability_done(ill);
8350 8350                          freemsg(mp);
8351 8351                          return;
8352 8352                  }
8353 8353                  /*
8354 8354                   * Note the error for IOCTL completion (mp1 is set when
8355 8355                   * ready to complete ioctl). If ill_ifname_pending_err is
8356 8356                   * set, an error occured during plumbing (ill_ifname_pending),
8357 8357                   * so we want to report that error.
8358 8358                   *
8359 8359                   * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8360 8360                   * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8361 8361                   * expected to get errack'd if the driver doesn't support
8362 8362                   * these flags (e.g. ethernet). log will be set to B_FALSE
8363 8363                   * if these error conditions are encountered.
8364 8364                   */
8365 8365                  if (mp1 != NULL) {
8366 8366                          if (ill->ill_ifname_pending_err != 0)  {
8367 8367                                  err = ill->ill_ifname_pending_err;
8368 8368                                  ill->ill_ifname_pending_err = 0;
8369 8369                          } else {
8370 8370                                  err = dlea->dl_unix_errno ?
8371 8371                                      dlea->dl_unix_errno : ENXIO;
8372 8372                          }
8373 8373                  /*
8374 8374                   * If we're plumbing an interface and an error hasn't already
8375 8375                   * been saved, set ill_ifname_pending_err to the error passed
8376 8376                   * up. Ignore the error if log is B_FALSE (see comment above).
8377 8377                   */
8378 8378                  } else if (log && ill->ill_ifname_pending &&
8379 8379                      ill->ill_ifname_pending_err == 0) {
8380 8380                          ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8381 8381                              dlea->dl_unix_errno : ENXIO;
8382 8382                  }
8383 8383  
8384 8384                  if (log)
8385 8385                          ip_dlpi_error(ill, dlea->dl_error_primitive,
8386 8386                              dlea->dl_errno, dlea->dl_unix_errno);
8387 8387                  break;
8388 8388          case DL_CAPABILITY_ACK:
8389 8389                  ill_capability_ack(ill, mp);
8390 8390                  /*
8391 8391                   * The message has been handed off to ill_capability_ack
8392 8392                   * and must not be freed below
8393 8393                   */
8394 8394                  mp = NULL;
8395 8395                  break;
8396 8396  
8397 8397          case DL_INFO_ACK:
8398 8398                  /* Call a routine to handle this one. */
8399 8399                  ill_dlpi_done(ill, DL_INFO_REQ);
8400 8400                  ip_ll_subnet_defaults(ill, mp);
8401 8401                  ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8402 8402                  return;
8403 8403          case DL_BIND_ACK:
8404 8404                  /*
8405 8405                   * We should have an IOCTL waiting on this unless
8406 8406                   * sent by ill_dl_phys, in which case just return
8407 8407                   */
8408 8408                  ill_dlpi_done(ill, DL_BIND_REQ);
8409 8409  
8410 8410                  if (ill->ill_ifname_pending) {
8411 8411                          DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8412 8412                              ill_t *, ill, mblk_t *, mp);
8413 8413                          break;
8414 8414                  }
8415 8415                  mutex_enter(&ill->ill_lock);
8416 8416                  ill->ill_dl_up = 1;
8417 8417                  ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8418 8418                  mutex_exit(&ill->ill_lock);
8419 8419  
8420 8420                  if (!ioctl_aborted)
8421 8421                          mp1 = ipsq_pending_mp_get(ipsq, &connp);
8422 8422                  if (mp1 == NULL) {
8423 8423                          DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8424 8424                          break;
8425 8425                  }
8426 8426                  /*
8427 8427                   * mp1 was added by ill_dl_up(). if that is a result of
8428 8428                   * a DL_NOTE_REPLUMB notification, connp could be NULL.
8429 8429                   */
8430 8430                  if (connp != NULL)
8431 8431                          q = CONNP_TO_WQ(connp);
8432 8432                  /*
8433 8433                   * We are exclusive. So nothing can change even after
8434 8434                   * we get the pending mp.
8435 8435                   */
8436 8436                  ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8437 8437                  DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8438 8438                  ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8439 8439  
8440 8440                  /*
8441 8441                   * Now bring up the resolver; when that is complete, we'll
8442 8442                   * create IREs.  Note that we intentionally mirror what
8443 8443                   * ipif_up() would have done, because we got here by way of
8444 8444                   * ill_dl_up(), which stopped ipif_up()'s processing.
8445 8445                   */
8446 8446                  if (ill->ill_isv6) {
8447 8447                          /*
8448 8448                           * v6 interfaces.
8449 8449                           * Unlike ARP which has to do another bind
8450 8450                           * and attach, once we get here we are
8451 8451                           * done with NDP
8452 8452                           */
8453 8453                          (void) ipif_resolver_up(ipif, Res_act_initial);
8454 8454                          if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8455 8455                                  err = ipif_up_done_v6(ipif);
8456 8456                  } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8457 8457                          /*
8458 8458                           * ARP and other v4 external resolvers.
8459 8459                           * Leave the pending mblk intact so that
8460 8460                           * the ioctl completes in ip_rput().
8461 8461                           */
8462 8462                          if (connp != NULL)
8463 8463                                  mutex_enter(&connp->conn_lock);
8464 8464                          mutex_enter(&ill->ill_lock);
8465 8465                          success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8466 8466                          mutex_exit(&ill->ill_lock);
8467 8467                          if (connp != NULL)
8468 8468                                  mutex_exit(&connp->conn_lock);
8469 8469                          if (success) {
8470 8470                                  err = ipif_resolver_up(ipif, Res_act_initial);
8471 8471                                  if (err == EINPROGRESS) {
8472 8472                                          freemsg(mp);
8473 8473                                          return;
8474 8474                                  }
8475 8475                                  mp1 = ipsq_pending_mp_get(ipsq, &connp);
8476 8476                          } else {
8477 8477                                  /* The conn has started closing */
8478 8478                                  err = EINTR;
8479 8479                          }
8480 8480                  } else {
8481 8481                          /*
8482 8482                           * This one is complete. Reply to pending ioctl.
8483 8483                           */
8484 8484                          (void) ipif_resolver_up(ipif, Res_act_initial);
8485 8485                          err = ipif_up_done(ipif);
8486 8486                  }
8487 8487  
8488 8488                  if ((err == 0) && (ill->ill_up_ipifs)) {
8489 8489                          err = ill_up_ipifs(ill, q, mp1);
8490 8490                          if (err == EINPROGRESS) {
8491 8491                                  freemsg(mp);
8492 8492                                  return;
8493 8493                          }
8494 8494                  }
8495 8495  
8496 8496                  /*
8497 8497                   * If we have a moved ipif to bring up, and everything has
8498 8498                   * succeeded to this point, bring it up on the IPMP ill.
8499 8499                   * Otherwise, leave it down -- the admin can try to bring it
8500 8500                   * up by hand if need be.
8501 8501                   */
8502 8502                  if (ill->ill_move_ipif != NULL) {
8503 8503                          if (err != 0) {
8504 8504                                  ill->ill_move_ipif = NULL;
8505 8505                          } else {
8506 8506                                  ipif = ill->ill_move_ipif;
8507 8507                                  ill->ill_move_ipif = NULL;
8508 8508                                  err = ipif_up(ipif, q, mp1);
8509 8509                                  if (err == EINPROGRESS) {
8510 8510                                          freemsg(mp);
8511 8511                                          return;
8512 8512                                  }
8513 8513                          }
8514 8514                  }
8515 8515                  break;
8516 8516  
8517 8517          case DL_NOTIFY_IND: {
8518 8518                  dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8519 8519                  uint_t orig_mtu, orig_mc_mtu;
8520 8520  
8521 8521                  switch (notify->dl_notification) {
8522 8522                  case DL_NOTE_PHYS_ADDR:
8523 8523                          err = ill_set_phys_addr(ill, mp);
8524 8524                          break;
8525 8525  
8526 8526                  case DL_NOTE_REPLUMB:
8527 8527                          /*
8528 8528                           * Directly return after calling ill_replumb().
8529 8529                           * Note that we should not free mp as it is reused
8530 8530                           * in the ill_replumb() function.
8531 8531                           */
8532 8532                          err = ill_replumb(ill, mp);
8533 8533                          return;
8534 8534  
8535 8535                  case DL_NOTE_FASTPATH_FLUSH:
8536 8536                          nce_flush(ill, B_FALSE);
8537 8537                          break;
8538 8538  
8539 8539                  case DL_NOTE_SDU_SIZE:
8540 8540                  case DL_NOTE_SDU_SIZE2:
8541 8541                          /*
8542 8542                           * The dce and fragmentation code can cope with
8543 8543                           * this changing while packets are being sent.
8544 8544                           * When packets are sent ip_output will discover
8545 8545                           * a change.
8546 8546                           *
8547 8547                           * Change the MTU size of the interface.
8548 8548                           */
8549 8549                          mutex_enter(&ill->ill_lock);
8550 8550                          orig_mtu = ill->ill_mtu;
8551 8551                          orig_mc_mtu = ill->ill_mc_mtu;
8552 8552                          switch (notify->dl_notification) {
8553 8553                          case DL_NOTE_SDU_SIZE:
8554 8554                                  ill->ill_current_frag =
8555 8555                                      (uint_t)notify->dl_data;
8556 8556                                  ill->ill_mc_mtu = (uint_t)notify->dl_data;
8557 8557                                  break;
8558 8558                          case DL_NOTE_SDU_SIZE2:
8559 8559                                  ill->ill_current_frag =
8560 8560                                      (uint_t)notify->dl_data1;
8561 8561                                  ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8562 8562                                  break;
8563 8563                          }
8564 8564                          if (ill->ill_current_frag > ill->ill_max_frag)
8565 8565                                  ill->ill_max_frag = ill->ill_current_frag;
8566 8566  
8567 8567                          if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8568 8568                                  ill->ill_mtu = ill->ill_current_frag;
8569 8569  
8570 8570                                  /*
8571 8571                                   * If ill_user_mtu was set (via
8572 8572                                   * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8573 8573                                   */
8574 8574                                  if (ill->ill_user_mtu != 0 &&
8575 8575                                      ill->ill_user_mtu < ill->ill_mtu)
8576 8576                                          ill->ill_mtu = ill->ill_user_mtu;
8577 8577  
8578 8578                                  if (ill->ill_user_mtu != 0 &&
8579 8579                                      ill->ill_user_mtu < ill->ill_mc_mtu)
8580 8580                                          ill->ill_mc_mtu = ill->ill_user_mtu;
8581 8581  
8582 8582                                  if (ill->ill_isv6) {
8583 8583                                          if (ill->ill_mtu < IPV6_MIN_MTU)
8584 8584                                                  ill->ill_mtu = IPV6_MIN_MTU;
8585 8585                                          if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8586 8586                                                  ill->ill_mc_mtu = IPV6_MIN_MTU;
8587 8587                                  } else {
8588 8588                                          if (ill->ill_mtu < IP_MIN_MTU)
8589 8589                                                  ill->ill_mtu = IP_MIN_MTU;
8590 8590                                          if (ill->ill_mc_mtu < IP_MIN_MTU)
8591 8591                                                  ill->ill_mc_mtu = IP_MIN_MTU;
8592 8592                                  }
8593 8593                          } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8594 8594                                  ill->ill_mc_mtu = ill->ill_mtu;
8595 8595                          }
8596 8596  
8597 8597                          mutex_exit(&ill->ill_lock);
8598 8598                          /*
8599 8599                           * Make sure all dce_generation checks find out
8600 8600                           * that ill_mtu/ill_mc_mtu has changed.
8601 8601                           */
8602 8602                          if (orig_mtu != ill->ill_mtu ||
8603 8603                              orig_mc_mtu != ill->ill_mc_mtu) {
8604 8604                                  dce_increment_all_generations(ill->ill_isv6,
8605 8605                                      ill->ill_ipst);
8606 8606                          }
8607 8607  
8608 8608                          /*
8609 8609                           * Refresh IPMP meta-interface MTU if necessary.
8610 8610                           */
8611 8611                          if (IS_UNDER_IPMP(ill))
8612 8612                                  ipmp_illgrp_refresh_mtu(ill->ill_grp);
8613 8613                          break;
8614 8614  
8615 8615                  case DL_NOTE_LINK_UP:
8616 8616                  case DL_NOTE_LINK_DOWN: {
8617 8617                          /*
8618 8618                           * We are writer. ill / phyint / ipsq assocs stable.
8619 8619                           * The RUNNING flag reflects the state of the link.
8620 8620                           */
8621 8621                          phyint_t *phyint = ill->ill_phyint;
8622 8622                          uint64_t new_phyint_flags;
8623 8623                          boolean_t changed = B_FALSE;
8624 8624                          boolean_t went_up;
8625 8625  
8626 8626                          went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8627 8627                          mutex_enter(&phyint->phyint_lock);
8628 8628  
8629 8629                          new_phyint_flags = went_up ?
8630 8630                              phyint->phyint_flags | PHYI_RUNNING :
8631 8631                              phyint->phyint_flags & ~PHYI_RUNNING;
8632 8632  
8633 8633                          if (IS_IPMP(ill)) {
8634 8634                                  new_phyint_flags = went_up ?
8635 8635                                      new_phyint_flags & ~PHYI_FAILED :
8636 8636                                      new_phyint_flags | PHYI_FAILED;
8637 8637                          }
8638 8638  
8639 8639                          if (new_phyint_flags != phyint->phyint_flags) {
8640 8640                                  phyint->phyint_flags = new_phyint_flags;
8641 8641                                  changed = B_TRUE;
8642 8642                          }
8643 8643                          mutex_exit(&phyint->phyint_lock);
8644 8644                          /*
8645 8645                           * ill_restart_dad handles the DAD restart and routing
8646 8646                           * socket notification logic.
8647 8647                           */
8648 8648                          if (changed) {
8649 8649                                  ill_restart_dad(phyint->phyint_illv4, went_up);
8650 8650                                  ill_restart_dad(phyint->phyint_illv6, went_up);
8651 8651                          }
8652 8652                          break;
8653 8653                  }
8654 8654                  case DL_NOTE_PROMISC_ON_PHYS: {
8655 8655                          phyint_t *phyint = ill->ill_phyint;
8656 8656  
8657 8657                          mutex_enter(&phyint->phyint_lock);
8658 8658                          phyint->phyint_flags |= PHYI_PROMISC;
8659 8659                          mutex_exit(&phyint->phyint_lock);
8660 8660                          break;
8661 8661                  }
8662 8662                  case DL_NOTE_PROMISC_OFF_PHYS: {
8663 8663                          phyint_t *phyint = ill->ill_phyint;
8664 8664  
8665 8665                          mutex_enter(&phyint->phyint_lock);
8666 8666                          phyint->phyint_flags &= ~PHYI_PROMISC;
8667 8667                          mutex_exit(&phyint->phyint_lock);
8668 8668                          break;
8669 8669                  }
8670 8670                  case DL_NOTE_CAPAB_RENEG:
8671 8671                          /*
8672 8672                           * Something changed on the driver side.
8673 8673                           * It wants us to renegotiate the capabilities
8674 8674                           * on this ill. One possible cause is the aggregation
8675 8675                           * interface under us where a port got added or
8676 8676                           * went away.
8677 8677                           *
8678 8678                           * If the capability negotiation is already done
8679 8679                           * or is in progress, reset the capabilities and
8680 8680                           * mark the ill's ill_capab_reneg to be B_TRUE,
8681 8681                           * so that when the ack comes back, we can start
8682 8682                           * the renegotiation process.
8683 8683                           *
8684 8684                           * Note that if ill_capab_reneg is already B_TRUE
8685 8685                           * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8686 8686                           * the capability resetting request has been sent
8687 8687                           * and the renegotiation has not been started yet;
8688 8688                           * nothing needs to be done in this case.
8689 8689                           */
8690 8690                          ipsq_current_start(ipsq, ill->ill_ipif, 0);
8691 8691                          ill_capability_reset(ill, B_TRUE);
8692 8692                          ipsq_current_finish(ipsq);
8693 8693                          break;
8694 8694  
8695 8695                  case DL_NOTE_ALLOWED_IPS:
8696 8696                          ill_set_allowed_ips(ill, mp);
8697 8697                          break;
8698 8698                  default:
8699 8699                          ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8700 8700                              "type 0x%x for DL_NOTIFY_IND\n",
8701 8701                              notify->dl_notification));
8702 8702                          break;
8703 8703                  }
8704 8704  
8705 8705                  /*
8706 8706                   * As this is an asynchronous operation, we
8707 8707                   * should not call ill_dlpi_done
8708 8708                   */
8709 8709                  break;
8710 8710          }
8711 8711          case DL_NOTIFY_ACK: {
8712 8712                  dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8713 8713  
8714 8714                  if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8715 8715                          ill->ill_note_link = 1;
8716 8716                  ill_dlpi_done(ill, DL_NOTIFY_REQ);
8717 8717                  break;
8718 8718          }
8719 8719          case DL_PHYS_ADDR_ACK: {
8720 8720                  /*
8721 8721                   * As part of plumbing the interface via SIOCSLIFNAME,
8722 8722                   * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8723 8723                   * whose answers we receive here.  As each answer is received,
8724 8724                   * we call ill_dlpi_done() to dispatch the next request as
8725 8725                   * we're processing the current one.  Once all answers have
8726 8726                   * been received, we use ipsq_pending_mp_get() to dequeue the
8727 8727                   * outstanding IOCTL and reply to it.  (Because ill_dl_phys()
8728 8728                   * is invoked from an ill queue, conn_oper_pending_ill is not
8729 8729                   * available, but we know the ioctl is pending on ill_wq.)
8730 8730                   */
8731 8731                  uint_t  paddrlen, paddroff;
8732 8732                  uint8_t *addr;
8733 8733  
8734 8734                  paddrreq = ill->ill_phys_addr_pend;
8735 8735                  paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8736 8736                  paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8737 8737                  addr = mp->b_rptr + paddroff;
8738 8738  
8739 8739                  ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8740 8740                  if (paddrreq == DL_IPV6_TOKEN) {
8741 8741                          /*
8742 8742                           * bcopy to low-order bits of ill_token
8743 8743                           *
8744 8744                           * XXX Temporary hack - currently, all known tokens
8745 8745                           * are 64 bits, so I'll cheat for the moment.
8746 8746                           */
8747 8747                          bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8748 8748                          ill->ill_token_length = paddrlen;
8749 8749                          break;
8750 8750                  } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8751 8751                          ASSERT(ill->ill_nd_lla_mp == NULL);
8752 8752                          ill_set_ndmp(ill, mp, paddroff, paddrlen);
8753 8753                          mp = NULL;
8754 8754                          break;
8755 8755                  } else if (paddrreq == DL_CURR_DEST_ADDR) {
8756 8756                          ASSERT(ill->ill_dest_addr_mp == NULL);
8757 8757                          ill->ill_dest_addr_mp = mp;
8758 8758                          ill->ill_dest_addr = addr;
8759 8759                          mp = NULL;
8760 8760                          if (ill->ill_isv6) {
8761 8761                                  ill_setdesttoken(ill);
8762 8762                                  ipif_setdestlinklocal(ill->ill_ipif);
8763 8763                          }
8764 8764                          break;
8765 8765                  }
8766 8766  
8767 8767                  ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8768 8768                  ASSERT(ill->ill_phys_addr_mp == NULL);
8769 8769                  if (!ill->ill_ifname_pending)
8770 8770                          break;
8771 8771                  ill->ill_ifname_pending = 0;
8772 8772                  if (!ioctl_aborted)
8773 8773                          mp1 = ipsq_pending_mp_get(ipsq, &connp);
8774 8774                  if (mp1 != NULL) {
8775 8775                          ASSERT(connp == NULL);
8776 8776                          q = ill->ill_wq;
8777 8777                  }
8778 8778                  /*
8779 8779                   * If any error acks received during the plumbing sequence,
8780 8780                   * ill_ifname_pending_err will be set. Break out and send up
8781 8781                   * the error to the pending ioctl.
8782 8782                   */
8783 8783                  if (ill->ill_ifname_pending_err != 0) {
8784 8784                          err = ill->ill_ifname_pending_err;
8785 8785                          ill->ill_ifname_pending_err = 0;
8786 8786                          break;
8787 8787                  }
8788 8788  
8789 8789                  ill->ill_phys_addr_mp = mp;
8790 8790                  ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8791 8791                  mp = NULL;
8792 8792  
8793 8793                  /*
8794 8794                   * If paddrlen or ill_phys_addr_length is zero, the DLPI
8795 8795                   * provider doesn't support physical addresses.  We check both
8796 8796                   * paddrlen and ill_phys_addr_length because sppp (PPP) does
8797 8797                   * not have physical addresses, but historically adversises a
8798 8798                   * physical address length of 0 in its DL_INFO_ACK, but 6 in
8799 8799                   * its DL_PHYS_ADDR_ACK.
8800 8800                   */
8801 8801                  if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8802 8802                          ill->ill_phys_addr = NULL;
8803 8803                  } else if (paddrlen != ill->ill_phys_addr_length) {
8804 8804                          ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8805 8805                              paddrlen, ill->ill_phys_addr_length));
8806 8806                          err = EINVAL;
8807 8807                          break;
8808 8808                  }
8809 8809  
8810 8810                  if (ill->ill_nd_lla_mp == NULL) {
8811 8811                          if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8812 8812                                  err = ENOMEM;
8813 8813                                  break;
8814 8814                          }
8815 8815                          ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8816 8816                  }
8817 8817  
8818 8818                  if (ill->ill_isv6) {
8819 8819                          ill_setdefaulttoken(ill);
8820 8820                          ipif_setlinklocal(ill->ill_ipif);
8821 8821                  }
8822 8822                  break;
8823 8823          }
8824 8824          case DL_OK_ACK:
8825 8825                  ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8826 8826                      dl_primstr((int)dloa->dl_correct_primitive),
8827 8827                      dloa->dl_correct_primitive));
8828 8828                  DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8829 8829                      char *, dl_primstr(dloa->dl_correct_primitive),
8830 8830                      ill_t *, ill);
8831 8831  
8832 8832                  switch (dloa->dl_correct_primitive) {
8833 8833                  case DL_ENABMULTI_REQ:
8834 8834                  case DL_DISABMULTI_REQ:
8835 8835                          ill_dlpi_done(ill, dloa->dl_correct_primitive);
8836 8836                          break;
8837 8837                  case DL_PROMISCON_REQ:
8838 8838                  case DL_PROMISCOFF_REQ:
8839 8839                  case DL_UNBIND_REQ:
8840 8840                  case DL_ATTACH_REQ:
8841 8841                          ill_dlpi_done(ill, dloa->dl_correct_primitive);
8842 8842                          break;
8843 8843                  }
8844 8844                  break;
8845 8845          default:
8846 8846                  break;
8847 8847          }
8848 8848  
8849 8849          freemsg(mp);
8850 8850          if (mp1 == NULL)
8851 8851                  return;
8852 8852  
8853 8853          /*
8854 8854           * The operation must complete without EINPROGRESS since
8855 8855           * ipsq_pending_mp_get() has removed the mblk (mp1).  Otherwise,
8856 8856           * the operation will be stuck forever inside the IPSQ.
8857 8857           */
8858 8858          ASSERT(err != EINPROGRESS);
8859 8859  
8860 8860          DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8861 8861              int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8862 8862              ipif_t *, NULL);
8863 8863  
8864 8864          switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8865 8865          case 0:
8866 8866                  ipsq_current_finish(ipsq);
8867 8867                  break;
8868 8868  
8869 8869          case SIOCSLIFNAME:
8870 8870          case IF_UNITSEL: {
8871 8871                  ill_t *ill_other = ILL_OTHER(ill);
8872 8872  
8873 8873                  /*
8874 8874                   * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8875 8875                   * ill has a peer which is in an IPMP group, then place ill
8876 8876                   * into the same group.  One catch: although ifconfig plumbs
8877 8877                   * the appropriate IPMP meta-interface prior to plumbing this
8878 8878                   * ill, it is possible for multiple ifconfig applications to
8879 8879                   * race (or for another application to adjust plumbing), in
8880 8880                   * which case the IPMP meta-interface we need will be missing.
8881 8881                   * If so, kick the phyint out of the group.
8882 8882                   */
8883 8883                  if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8884 8884                          ipmp_grp_t      *grp = ill->ill_phyint->phyint_grp;
8885 8885                          ipmp_illgrp_t   *illg;
8886 8886  
8887 8887                          illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8888 8888                          if (illg == NULL)
8889 8889                                  ipmp_phyint_leave_grp(ill->ill_phyint);
8890 8890                          else
8891 8891                                  ipmp_ill_join_illgrp(ill, illg);
8892 8892                  }
8893 8893  
8894 8894                  if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8895 8895                          ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8896 8896                  else
8897 8897                          ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8898 8898                  break;
8899 8899          }
8900 8900          case SIOCLIFADDIF:
8901 8901                  ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8902 8902                  break;
8903 8903  
8904 8904          default:
8905 8905                  ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8906 8906                  break;
8907 8907          }
8908 8908  }
8909 8909  
8910 8910  /*
8911 8911   * ip_rput_other is called by ip_rput to handle messages modifying the global
8912 8912   * state in IP.  If 'ipsq' is non-NULL, caller is writer on it.
8913 8913   */
8914 8914  /* ARGSUSED */
8915 8915  void
8916 8916  ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8917 8917  {
8918 8918          ill_t           *ill = q->q_ptr;
8919 8919          struct iocblk   *iocp;
8920 8920  
8921 8921          ip1dbg(("ip_rput_other "));
8922 8922          if (ipsq != NULL) {
8923 8923                  ASSERT(IAM_WRITER_IPSQ(ipsq));
8924 8924                  ASSERT(ipsq->ipsq_xop ==
8925 8925                      ill->ill_phyint->phyint_ipsq->ipsq_xop);
8926 8926          }
8927 8927  
8928 8928          switch (mp->b_datap->db_type) {
8929 8929          case M_ERROR:
8930 8930          case M_HANGUP:
8931 8931                  /*
8932 8932                   * The device has a problem.  We force the ILL down.  It can
8933 8933                   * be brought up again manually using SIOCSIFFLAGS (via
8934 8934                   * ifconfig or equivalent).
8935 8935                   */
8936 8936                  ASSERT(ipsq != NULL);
8937 8937                  if (mp->b_rptr < mp->b_wptr)
8938 8938                          ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8939 8939                  if (ill->ill_error == 0)
8940 8940                          ill->ill_error = ENXIO;
8941 8941                  if (!ill_down_start(q, mp))
8942 8942                          return;
8943 8943                  ipif_all_down_tail(ipsq, q, mp, NULL);
8944 8944                  break;
8945 8945          case M_IOCNAK: {
8946 8946                  iocp = (struct iocblk *)mp->b_rptr;
8947 8947  
8948 8948                  ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8949 8949                  /*
8950 8950                   * If this was the first attempt, turn off the fastpath
8951 8951                   * probing.
8952 8952                   */
8953 8953                  mutex_enter(&ill->ill_lock);
8954 8954                  if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8955 8955                          ill->ill_dlpi_fastpath_state = IDS_FAILED;
8956 8956                          mutex_exit(&ill->ill_lock);
8957 8957                          /*
8958 8958                           * don't flush the nce_t entries: we use them
8959 8959                           * as an index to the ncec itself.
8960 8960                           */
8961 8961                          ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8962 8962                              ill->ill_name));
8963 8963                  } else {
8964 8964                          mutex_exit(&ill->ill_lock);
8965 8965                  }
8966 8966                  freemsg(mp);
8967 8967                  break;
8968 8968          }
8969 8969          default:
8970 8970                  ASSERT(0);
8971 8971                  break;
8972 8972          }
8973 8973  }
8974 8974  
8975 8975  /*
8976 8976   * Update any source route, record route or timestamp options
8977 8977   * When it fails it has consumed the message and BUMPed the MIB.
8978 8978   */
8979 8979  boolean_t
8980 8980  ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8981 8981      ip_recv_attr_t *ira)
8982 8982  {
8983 8983          ipoptp_t        opts;
8984 8984          uchar_t         *opt;
8985 8985          uint8_t         optval;
8986 8986          uint8_t         optlen;
8987 8987          ipaddr_t        dst;
8988 8988          ipaddr_t        ifaddr;
8989 8989          uint32_t        ts;
8990 8990          timestruc_t     now;
8991 8991          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
8992 8992  
8993 8993          ip2dbg(("ip_forward_options\n"));
8994 8994          dst = ipha->ipha_dst;
8995 8995          for (optval = ipoptp_first(&opts, ipha);
8996 8996              optval != IPOPT_EOL;
8997 8997              optval = ipoptp_next(&opts)) {
8998 8998                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8999 8999                  opt = opts.ipoptp_cur;
9000 9000                  optlen = opts.ipoptp_len;
9001 9001                  ip2dbg(("ip_forward_options: opt %d, len %d\n",
9002 9002                      optval, opts.ipoptp_len));
9003 9003                  switch (optval) {
9004 9004                          uint32_t off;
9005 9005                  case IPOPT_SSRR:
9006 9006                  case IPOPT_LSRR:
9007 9007                          /* Check if adminstratively disabled */
9008 9008                          if (!ipst->ips_ip_forward_src_routed) {
9009 9009                                  BUMP_MIB(dst_ill->ill_ip_mib,
9010 9010                                      ipIfStatsForwProhibits);
9011 9011                                  ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
9012 9012                                      mp, dst_ill);
9013 9013                                  icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
9014 9014                                      ira);
9015 9015                                  return (B_FALSE);
9016 9016                          }
9017 9017                          if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9018 9018                                  /*
9019 9019                                   * Must be partial since ip_input_options
9020 9020                                   * checked for strict.
9021 9021                                   */
9022 9022                                  break;
9023 9023                          }
9024 9024                          off = opt[IPOPT_OFFSET];
9025 9025                          off--;
9026 9026                  redo_srr:
9027 9027                          if (optlen < IP_ADDR_LEN ||
9028 9028                              off > optlen - IP_ADDR_LEN) {
9029 9029                                  /* End of source route */
9030 9030                                  ip1dbg((
9031 9031                                      "ip_forward_options: end of SR\n"));
9032 9032                                  break;
9033 9033                          }
9034 9034                          /* Pick a reasonable address on the outbound if */
9035 9035                          ASSERT(dst_ill != NULL);
9036 9036                          if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9037 9037                              INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9038 9038                              NULL) != 0) {
9039 9039                                  /* No source! Shouldn't happen */
9040 9040                                  ifaddr = INADDR_ANY;
9041 9041                          }
9042 9042                          bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9043 9043                          bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9044 9044                          ip1dbg(("ip_forward_options: next hop 0x%x\n",
9045 9045                              ntohl(dst)));
9046 9046  
9047 9047                          /*
9048 9048                           * Check if our address is present more than
9049 9049                           * once as consecutive hops in source route.
9050 9050                           */
9051 9051                          if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9052 9052                                  off += IP_ADDR_LEN;
9053 9053                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9054 9054                                  goto redo_srr;
9055 9055                          }
9056 9056                          ipha->ipha_dst = dst;
9057 9057                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9058 9058                          break;
9059 9059                  case IPOPT_RR:
9060 9060                          off = opt[IPOPT_OFFSET];
9061 9061                          off--;
9062 9062                          if (optlen < IP_ADDR_LEN ||
9063 9063                              off > optlen - IP_ADDR_LEN) {
9064 9064                                  /* No more room - ignore */
9065 9065                                  ip1dbg((
9066 9066                                      "ip_forward_options: end of RR\n"));
9067 9067                                  break;
9068 9068                          }
9069 9069                          /* Pick a reasonable address on the outbound if */
9070 9070                          ASSERT(dst_ill != NULL);
9071 9071                          if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9072 9072                              INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9073 9073                              NULL) != 0) {
9074 9074                                  /* No source! Shouldn't happen */
9075 9075                                  ifaddr = INADDR_ANY;
9076 9076                          }
9077 9077                          bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9078 9078                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9079 9079                          break;
9080 9080                  case IPOPT_TS:
9081 9081                          /* Insert timestamp if there is room */
9082 9082                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9083 9083                          case IPOPT_TS_TSONLY:
9084 9084                                  off = IPOPT_TS_TIMELEN;
9085 9085                                  break;
9086 9086                          case IPOPT_TS_PRESPEC:
9087 9087                          case IPOPT_TS_PRESPEC_RFC791:
9088 9088                                  /* Verify that the address matched */
9089 9089                                  off = opt[IPOPT_OFFSET] - 1;
9090 9090                                  bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9091 9091                                  if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9092 9092                                          /* Not for us */
9093 9093                                          break;
9094 9094                                  }
9095 9095                                  /* FALLTHROUGH */
9096 9096                          case IPOPT_TS_TSANDADDR:
9097 9097                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9098 9098                                  break;
9099 9099                          default:
9100 9100                                  /*
9101 9101                                   * ip_*put_options should have already
9102 9102                                   * dropped this packet.
9103 9103                                   */
9104 9104                                  cmn_err(CE_PANIC, "ip_forward_options: "
9105 9105                                      "unknown IT - bug in ip_input_options?\n");
9106 9106                                  return (B_TRUE);        /* Keep "lint" happy */
9107 9107                          }
9108 9108                          if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9109 9109                                  /* Increase overflow counter */
9110 9110                                  off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9111 9111                                  opt[IPOPT_POS_OV_FLG] =
9112 9112                                      (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9113 9113                                      (off << 4));
9114 9114                                  break;
9115 9115                          }
9116 9116                          off = opt[IPOPT_OFFSET] - 1;
9117 9117                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9118 9118                          case IPOPT_TS_PRESPEC:
9119 9119                          case IPOPT_TS_PRESPEC_RFC791:
9120 9120                          case IPOPT_TS_TSANDADDR:
9121 9121                                  /* Pick a reasonable addr on the outbound if */
9122 9122                                  ASSERT(dst_ill != NULL);
9123 9123                                  if (ip_select_source_v4(dst_ill, INADDR_ANY,
9124 9124                                      dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
9125 9125                                      NULL, NULL) != 0) {
9126 9126                                          /* No source! Shouldn't happen */
9127 9127                                          ifaddr = INADDR_ANY;
9128 9128                                  }
9129 9129                                  bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9130 9130                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9131 9131                                  /* FALLTHROUGH */
9132 9132                          case IPOPT_TS_TSONLY:
9133 9133                                  off = opt[IPOPT_OFFSET] - 1;
9134 9134                                  /* Compute # of milliseconds since midnight */
9135 9135                                  gethrestime(&now);
9136 9136                                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9137 9137                                      NSEC2MSEC(now.tv_nsec);
9138 9138                                  bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9139 9139                                  opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9140 9140                                  break;
9141 9141                          }
9142 9142                          break;
9143 9143                  }
9144 9144          }
9145 9145          return (B_TRUE);
9146 9146  }
9147 9147  
9148 9148  /*
9149 9149   * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
9150 9150   * returns 'true' if there are still fragments left on the queue, in
9151 9151   * which case we restart the timer.
9152 9152   */
9153 9153  void
9154 9154  ill_frag_timer(void *arg)
9155 9155  {
9156 9156          ill_t   *ill = (ill_t *)arg;
9157 9157          boolean_t frag_pending;
9158 9158          ip_stack_t *ipst = ill->ill_ipst;
9159 9159          time_t  timeout;
9160 9160  
9161 9161          mutex_enter(&ill->ill_lock);
9162 9162          ASSERT(!ill->ill_fragtimer_executing);
9163 9163          if (ill->ill_state_flags & ILL_CONDEMNED) {
9164 9164                  ill->ill_frag_timer_id = 0;
9165 9165                  mutex_exit(&ill->ill_lock);
9166 9166                  return;
9167 9167          }
9168 9168          ill->ill_fragtimer_executing = 1;
9169 9169          mutex_exit(&ill->ill_lock);
9170 9170  
9171 9171          timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9172 9172              ipst->ips_ip_reassembly_timeout);
9173 9173  
9174 9174          frag_pending = ill_frag_timeout(ill, timeout);
9175 9175  
9176 9176          /*
9177 9177           * Restart the timer, if we have fragments pending or if someone
9178 9178           * wanted us to be scheduled again.
9179 9179           */
9180 9180          mutex_enter(&ill->ill_lock);
9181 9181          ill->ill_fragtimer_executing = 0;
9182 9182          ill->ill_frag_timer_id = 0;
9183 9183          if (frag_pending || ill->ill_fragtimer_needrestart)
9184 9184                  ill_frag_timer_start(ill);
9185 9185          mutex_exit(&ill->ill_lock);
9186 9186  }
9187 9187  
9188 9188  void
9189 9189  ill_frag_timer_start(ill_t *ill)
9190 9190  {
9191 9191          ip_stack_t *ipst = ill->ill_ipst;
9192 9192          clock_t timeo_ms;
9193 9193  
9194 9194          ASSERT(MUTEX_HELD(&ill->ill_lock));
9195 9195  
9196 9196          /* If the ill is closing or opening don't proceed */
9197 9197          if (ill->ill_state_flags & ILL_CONDEMNED)
9198 9198                  return;
9199 9199  
9200 9200          if (ill->ill_fragtimer_executing) {
9201 9201                  /*
9202 9202                   * ill_frag_timer is currently executing. Just record the
9203 9203                   * the fact that we want the timer to be restarted.
9204 9204                   * ill_frag_timer will post a timeout before it returns,
9205 9205                   * ensuring it will be called again.
9206 9206                   */
9207 9207                  ill->ill_fragtimer_needrestart = 1;
9208 9208                  return;
9209 9209          }
9210 9210  
9211 9211          if (ill->ill_frag_timer_id == 0) {
9212 9212                  timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9213 9213                      ipst->ips_ip_reassembly_timeout) * SECONDS;
9214 9214  
9215 9215                  /*
9216 9216                   * The timer is neither running nor is the timeout handler
9217 9217                   * executing. Post a timeout so that ill_frag_timer will be
9218 9218                   * called
9219 9219                   */
9220 9220                  ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
9221 9221                      MSEC_TO_TICK(timeo_ms >> 1));
9222 9222                  ill->ill_fragtimer_needrestart = 0;
9223 9223          }
9224 9224  }
9225 9225  
9226 9226  /*
9227 9227   * Update any source route, record route or timestamp options.
9228 9228   * Check that we are at end of strict source route.
9229 9229   * The options have already been checked for sanity in ip_input_options().
9230 9230   */
9231 9231  boolean_t
9232 9232  ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
9233 9233  {
9234 9234          ipoptp_t        opts;
9235 9235          uchar_t         *opt;
9236 9236          uint8_t         optval;
9237 9237          uint8_t         optlen;
9238 9238          ipaddr_t        dst;
9239 9239          ipaddr_t        ifaddr;
9240 9240          uint32_t        ts;
9241 9241          timestruc_t     now;
9242 9242          ill_t           *ill = ira->ira_ill;
9243 9243          ip_stack_t      *ipst = ill->ill_ipst;
9244 9244  
9245 9245          ip2dbg(("ip_input_local_options\n"));
9246 9246  
9247 9247          for (optval = ipoptp_first(&opts, ipha);
9248 9248              optval != IPOPT_EOL;
9249 9249              optval = ipoptp_next(&opts)) {
9250 9250                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9251 9251                  opt = opts.ipoptp_cur;
9252 9252                  optlen = opts.ipoptp_len;
9253 9253                  ip2dbg(("ip_input_local_options: opt %d, len %d\n",
9254 9254                      optval, optlen));
9255 9255                  switch (optval) {
9256 9256                          uint32_t off;
9257 9257                  case IPOPT_SSRR:
9258 9258                  case IPOPT_LSRR:
9259 9259                          off = opt[IPOPT_OFFSET];
9260 9260                          off--;
9261 9261                          if (optlen < IP_ADDR_LEN ||
9262 9262                              off > optlen - IP_ADDR_LEN) {
9263 9263                                  /* End of source route */
9264 9264                                  ip1dbg(("ip_input_local_options: end of SR\n"));
9265 9265                                  break;
9266 9266                          }
9267 9267                          /*
9268 9268                           * This will only happen if two consecutive entries
9269 9269                           * in the source route contains our address or if
9270 9270                           * it is a packet with a loose source route which
9271 9271                           * reaches us before consuming the whole source route
9272 9272                           */
9273 9273                          ip1dbg(("ip_input_local_options: not end of SR\n"));
9274 9274                          if (optval == IPOPT_SSRR) {
9275 9275                                  goto bad_src_route;
9276 9276                          }
9277 9277                          /*
9278 9278                           * Hack: instead of dropping the packet truncate the
9279 9279                           * source route to what has been used by filling the
9280 9280                           * rest with IPOPT_NOP.
9281 9281                           */
9282 9282                          opt[IPOPT_OLEN] = (uint8_t)off;
9283 9283                          while (off < optlen) {
9284 9284                                  opt[off++] = IPOPT_NOP;
9285 9285                          }
9286 9286                          break;
9287 9287                  case IPOPT_RR:
9288 9288                          off = opt[IPOPT_OFFSET];
9289 9289                          off--;
9290 9290                          if (optlen < IP_ADDR_LEN ||
9291 9291                              off > optlen - IP_ADDR_LEN) {
9292 9292                                  /* No more room - ignore */
9293 9293                                  ip1dbg((
9294 9294                                      "ip_input_local_options: end of RR\n"));
9295 9295                                  break;
9296 9296                          }
9297 9297                          /* Pick a reasonable address on the outbound if */
9298 9298                          if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9299 9299                              INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9300 9300                              NULL) != 0) {
9301 9301                                  /* No source! Shouldn't happen */
9302 9302                                  ifaddr = INADDR_ANY;
9303 9303                          }
9304 9304                          bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9305 9305                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9306 9306                          break;
9307 9307                  case IPOPT_TS:
9308 9308                          /* Insert timestamp if there is romm */
9309 9309                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9310 9310                          case IPOPT_TS_TSONLY:
9311 9311                                  off = IPOPT_TS_TIMELEN;
9312 9312                                  break;
9313 9313                          case IPOPT_TS_PRESPEC:
9314 9314                          case IPOPT_TS_PRESPEC_RFC791:
9315 9315                                  /* Verify that the address matched */
9316 9316                                  off = opt[IPOPT_OFFSET] - 1;
9317 9317                                  bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9318 9318                                  if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9319 9319                                          /* Not for us */
9320 9320                                          break;
9321 9321                                  }
9322 9322                                  /* FALLTHROUGH */
9323 9323                          case IPOPT_TS_TSANDADDR:
9324 9324                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9325 9325                                  break;
9326 9326                          default:
9327 9327                                  /*
9328 9328                                   * ip_*put_options should have already
9329 9329                                   * dropped this packet.
9330 9330                                   */
9331 9331                                  cmn_err(CE_PANIC, "ip_input_local_options: "
9332 9332                                      "unknown IT - bug in ip_input_options?\n");
9333 9333                                  return (B_TRUE);        /* Keep "lint" happy */
9334 9334                          }
9335 9335                          if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9336 9336                                  /* Increase overflow counter */
9337 9337                                  off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9338 9338                                  opt[IPOPT_POS_OV_FLG] =
9339 9339                                      (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9340 9340                                      (off << 4));
9341 9341                                  break;
9342 9342                          }
9343 9343                          off = opt[IPOPT_OFFSET] - 1;
9344 9344                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9345 9345                          case IPOPT_TS_PRESPEC:
9346 9346                          case IPOPT_TS_PRESPEC_RFC791:
9347 9347                          case IPOPT_TS_TSANDADDR:
9348 9348                                  /* Pick a reasonable addr on the outbound if */
9349 9349                                  if (ip_select_source_v4(ill, INADDR_ANY,
9350 9350                                      ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9351 9351                                      &ifaddr, NULL, NULL) != 0) {
9352 9352                                          /* No source! Shouldn't happen */
9353 9353                                          ifaddr = INADDR_ANY;
9354 9354                                  }
9355 9355                                  bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9356 9356                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9357 9357                                  /* FALLTHROUGH */
9358 9358                          case IPOPT_TS_TSONLY:
9359 9359                                  off = opt[IPOPT_OFFSET] - 1;
9360 9360                                  /* Compute # of milliseconds since midnight */
9361 9361                                  gethrestime(&now);
9362 9362                                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9363 9363                                      NSEC2MSEC(now.tv_nsec);
9364 9364                                  bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9365 9365                                  opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9366 9366                                  break;
9367 9367                          }
9368 9368                          break;
9369 9369                  }
9370 9370          }
9371 9371          return (B_TRUE);
9372 9372  
9373 9373  bad_src_route:
9374 9374          /* make sure we clear any indication of a hardware checksum */
9375 9375          DB_CKSUMFLAGS(mp) = 0;
9376 9376          ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9377 9377          icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9378 9378          return (B_FALSE);
9379 9379  
9380 9380  }
9381 9381  
9382 9382  /*
9383 9383   * Process IP options in an inbound packet.  Always returns the nexthop.
9384 9384   * Normally this is the passed in nexthop, but if there is an option
9385 9385   * that effects the nexthop (such as a source route) that will be returned.
9386 9386   * Sets *errorp if there is an error, in which case an ICMP error has been sent
9387 9387   * and mp freed.
9388 9388   */
9389 9389  ipaddr_t
9390 9390  ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9391 9391      ip_recv_attr_t *ira, int *errorp)
9392 9392  {
9393 9393          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
9394 9394          ipoptp_t        opts;
9395 9395          uchar_t         *opt;
9396 9396          uint8_t         optval;
9397 9397          uint8_t         optlen;
9398 9398          intptr_t        code = 0;
9399 9399          ire_t           *ire;
9400 9400  
9401 9401          ip2dbg(("ip_input_options\n"));
9402 9402          *errorp = 0;
9403 9403          for (optval = ipoptp_first(&opts, ipha);
9404 9404              optval != IPOPT_EOL;
9405 9405              optval = ipoptp_next(&opts)) {
9406 9406                  opt = opts.ipoptp_cur;
9407 9407                  optlen = opts.ipoptp_len;
9408 9408                  ip2dbg(("ip_input_options: opt %d, len %d\n",
9409 9409                      optval, optlen));
9410 9410                  /*
9411 9411                   * Note: we need to verify the checksum before we
9412 9412                   * modify anything thus this routine only extracts the next
9413 9413                   * hop dst from any source route.
9414 9414                   */
9415 9415                  switch (optval) {
9416 9416                          uint32_t off;
9417 9417                  case IPOPT_SSRR:
9418 9418                  case IPOPT_LSRR:
9419 9419                          if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9420 9420                                  if (optval == IPOPT_SSRR) {
9421 9421                                          ip1dbg(("ip_input_options: not next"
9422 9422                                              " strict source route 0x%x\n",
9423 9423                                              ntohl(dst)));
9424 9424                                          code = (char *)&ipha->ipha_dst -
9425 9425                                              (char *)ipha;
9426 9426                                          goto param_prob; /* RouterReq's */
9427 9427                                  }
9428 9428                                  ip2dbg(("ip_input_options: "
9429 9429                                      "not next source route 0x%x\n",
9430 9430                                      ntohl(dst)));
9431 9431                                  break;
9432 9432                          }
9433 9433  
9434 9434                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9435 9435                                  ip1dbg((
9436 9436                                      "ip_input_options: bad option offset\n"));
9437 9437                                  code = (char *)&opt[IPOPT_OLEN] -
9438 9438                                      (char *)ipha;
9439 9439                                  goto param_prob;
9440 9440                          }
9441 9441                          off = opt[IPOPT_OFFSET];
9442 9442                          off--;
9443 9443                  redo_srr:
9444 9444                          if (optlen < IP_ADDR_LEN ||
9445 9445                              off > optlen - IP_ADDR_LEN) {
9446 9446                                  /* End of source route */
9447 9447                                  ip1dbg(("ip_input_options: end of SR\n"));
9448 9448                                  break;
9449 9449                          }
9450 9450                          bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9451 9451                          ip1dbg(("ip_input_options: next hop 0x%x\n",
9452 9452                              ntohl(dst)));
9453 9453  
9454 9454                          /*
9455 9455                           * Check if our address is present more than
9456 9456                           * once as consecutive hops in source route.
9457 9457                           * XXX verify per-interface ip_forwarding
9458 9458                           * for source route?
9459 9459                           */
9460 9460                          if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9461 9461                                  off += IP_ADDR_LEN;
9462 9462                                  goto redo_srr;
9463 9463                          }
9464 9464  
9465 9465                          if (dst == htonl(INADDR_LOOPBACK)) {
9466 9466                                  ip1dbg(("ip_input_options: loopback addr in "
9467 9467                                      "source route!\n"));
9468 9468                                  goto bad_src_route;
9469 9469                          }
9470 9470                          /*
9471 9471                           * For strict: verify that dst is directly
9472 9472                           * reachable.
9473 9473                           */
9474 9474                          if (optval == IPOPT_SSRR) {
9475 9475                                  ire = ire_ftable_lookup_v4(dst, 0, 0,
9476 9476                                      IRE_INTERFACE, NULL, ALL_ZONES,
9477 9477                                      ira->ira_tsl,
9478 9478                                      MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
9479 9479                                      NULL);
9480 9480                                  if (ire == NULL) {
9481 9481                                          ip1dbg(("ip_input_options: SSRR not "
9482 9482                                              "directly reachable: 0x%x\n",
9483 9483                                              ntohl(dst)));
9484 9484                                          goto bad_src_route;
9485 9485                                  }
9486 9486                                  ire_refrele(ire);
9487 9487                          }
9488 9488                          /*
9489 9489                           * Defer update of the offset and the record route
9490 9490                           * until the packet is forwarded.
9491 9491                           */
9492 9492                          break;
9493 9493                  case IPOPT_RR:
9494 9494                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9495 9495                                  ip1dbg((
9496 9496                                      "ip_input_options: bad option offset\n"));
9497 9497                                  code = (char *)&opt[IPOPT_OLEN] -
9498 9498                                      (char *)ipha;
9499 9499                                  goto param_prob;
9500 9500                          }
9501 9501                          break;
9502 9502                  case IPOPT_TS:
9503 9503                          /*
9504 9504                           * Verify that length >= 5 and that there is either
9505 9505                           * room for another timestamp or that the overflow
9506 9506                           * counter is not maxed out.
9507 9507                           */
9508 9508                          code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9509 9509                          if (optlen < IPOPT_MINLEN_IT) {
9510 9510                                  goto param_prob;
9511 9511                          }
9512 9512                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9513 9513                                  ip1dbg((
9514 9514                                      "ip_input_options: bad option offset\n"));
9515 9515                                  code = (char *)&opt[IPOPT_OFFSET] -
9516 9516                                      (char *)ipha;
9517 9517                                  goto param_prob;
9518 9518                          }
9519 9519                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9520 9520                          case IPOPT_TS_TSONLY:
9521 9521                                  off = IPOPT_TS_TIMELEN;
9522 9522                                  break;
9523 9523                          case IPOPT_TS_TSANDADDR:
9524 9524                          case IPOPT_TS_PRESPEC:
9525 9525                          case IPOPT_TS_PRESPEC_RFC791:
9526 9526                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9527 9527                                  break;
9528 9528                          default:
9529 9529                                  code = (char *)&opt[IPOPT_POS_OV_FLG] -
9530 9530                                      (char *)ipha;
9531 9531                                  goto param_prob;
9532 9532                          }
9533 9533                          if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9534 9534                              (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9535 9535                                  /*
9536 9536                                   * No room and the overflow counter is 15
9537 9537                                   * already.
9538 9538                                   */
9539 9539                                  goto param_prob;
9540 9540                          }
9541 9541                          break;
9542 9542                  }
9543 9543          }
9544 9544  
9545 9545          if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9546 9546                  return (dst);
9547 9547          }
9548 9548  
9549 9549          ip1dbg(("ip_input_options: error processing IP options."));
9550 9550          code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9551 9551  
9552 9552  param_prob:
9553 9553          /* make sure we clear any indication of a hardware checksum */
9554 9554          DB_CKSUMFLAGS(mp) = 0;
9555 9555          ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9556 9556          icmp_param_problem(mp, (uint8_t)code, ira);
9557 9557          *errorp = -1;
9558 9558          return (dst);
9559 9559  
9560 9560  bad_src_route:
9561 9561          /* make sure we clear any indication of a hardware checksum */
9562 9562          DB_CKSUMFLAGS(mp) = 0;
9563 9563          ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9564 9564          icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9565 9565          *errorp = -1;
9566 9566          return (dst);
9567 9567  }
9568 9568  
9569 9569  /*
9570 9570   * IP & ICMP info in >=14 msg's ...
9571 9571   *  - ip fixed part (mib2_ip_t)
9572 9572   *  - icmp fixed part (mib2_icmp_t)
9573 9573   *  - ipAddrEntryTable (ip 20)          all IPv4 ipifs
9574 9574   *  - ipRouteEntryTable (ip 21)         all IPv4 IREs
9575 9575   *  - ipNetToMediaEntryTable (ip 22)    all IPv4 Neighbor Cache entries
9576 9576   *  - ipRouteAttributeTable (ip 102)    labeled routes
9577 9577   *  - ip multicast membership (ip_member_t)
9578 9578   *  - ip multicast source filtering (ip_grpsrc_t)
9579 9579   *  - igmp fixed part (struct igmpstat)
9580 9580   *  - multicast routing stats (struct mrtstat)
9581 9581   *  - multicast routing vifs (array of struct vifctl)
9582 9582   *  - multicast routing routes (array of struct mfcctl)
9583 9583   *  - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9584 9584   *                                      One per ill plus one generic
9585 9585   *  - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9586 9586   *                                      One per ill plus one generic
9587 9587   *  - ipv6RouteEntry                    all IPv6 IREs
9588 9588   *  - ipv6RouteAttributeTable (ip6 102) labeled routes
9589 9589   *  - ipv6NetToMediaEntry               all IPv6 Neighbor Cache entries
9590 9590   *  - ipv6AddrEntry                     all IPv6 ipifs
9591 9591   *  - ipv6 multicast membership (ipv6_member_t)
9592 9592   *  - ipv6 multicast source filtering (ipv6_grpsrc_t)
9593 9593   *
9594 9594   * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9595 9595   * already filled in by the caller.
9596 9596   * If legacy_req is true then MIB structures needs to be truncated to their
9597 9597   * legacy sizes before being returned.
9598 9598   * Return value of 0 indicates that no messages were sent and caller
9599 9599   * should free mpctl.
9600 9600   */
9601 9601  int
9602 9602  ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9603 9603  {
9604 9604          ip_stack_t *ipst;
9605 9605          sctp_stack_t *sctps;
9606 9606  
9607 9607          if (q->q_next != NULL) {
9608 9608                  ipst = ILLQ_TO_IPST(q);
9609 9609          } else {
9610 9610                  ipst = CONNQ_TO_IPST(q);
9611 9611          }
9612 9612          ASSERT(ipst != NULL);
9613 9613          sctps = ipst->ips_netstack->netstack_sctp;
9614 9614  
9615 9615          if (mpctl == NULL || mpctl->b_cont == NULL) {
9616 9616                  return (0);
9617 9617          }
9618 9618  
9619 9619          /*
9620 9620           * For the purposes of the (broken) packet shell use
9621 9621           * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9622 9622           * to make TCP and UDP appear first in the list of mib items.
9623 9623           * TBD: We could expand this and use it in netstat so that
9624 9624           * the kernel doesn't have to produce large tables (connections,
9625 9625           * routes, etc) when netstat only wants the statistics or a particular
9626 9626           * table.
9627 9627           */
9628 9628          if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9629 9629                  if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9630 9630                          return (1);
9631 9631                  }
9632 9632          }
9633 9633  
9634 9634          if (level != MIB2_TCP) {
9635 9635                  if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9636 9636                          return (1);
9637 9637                  }
9638 9638          }
9639 9639  
9640 9640          if (level != MIB2_UDP) {
9641 9641                  if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9642 9642                          return (1);
9643 9643                  }
9644 9644          }
9645 9645  
9646 9646          if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9647 9647              ipst, legacy_req)) == NULL) {
9648 9648                  return (1);
9649 9649          }
9650 9650  
9651 9651          if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9652 9652              legacy_req)) == NULL) {
9653 9653                  return (1);
9654 9654          }
9655 9655  
9656 9656          if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9657 9657                  return (1);
9658 9658          }
9659 9659  
9660 9660          if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9661 9661                  return (1);
9662 9662          }
9663 9663  
9664 9664          if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9665 9665                  return (1);
9666 9666          }
9667 9667  
9668 9668          if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9669 9669                  return (1);
9670 9670          }
9671 9671  
9672 9672          if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9673 9673              legacy_req)) == NULL) {
9674 9674                  return (1);
9675 9675          }
9676 9676  
9677 9677          if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9678 9678              legacy_req)) == NULL) {
9679 9679                  return (1);
9680 9680          }
9681 9681  
9682 9682          if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9683 9683                  return (1);
9684 9684          }
9685 9685  
9686 9686          if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9687 9687                  return (1);
9688 9688          }
9689 9689  
9690 9690          if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9691 9691                  return (1);
9692 9692          }
9693 9693  
9694 9694          if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9695 9695                  return (1);
9696 9696          }
9697 9697  
9698 9698          if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9699 9699                  return (1);
9700 9700          }
9701 9701  
9702 9702          if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9703 9703                  return (1);
9704 9704          }
9705 9705  
9706 9706          mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9707 9707          if (mpctl == NULL)
9708 9708                  return (1);
9709 9709  
9710 9710          mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9711 9711          if (mpctl == NULL)
9712 9712                  return (1);
9713 9713  
9714 9714          if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9715 9715                  return (1);
9716 9716          }
9717 9717          if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9718 9718                  return (1);
9719 9719          }
9720 9720          freemsg(mpctl);
9721 9721          return (1);
9722 9722  }
9723 9723  
9724 9724  /* Get global (legacy) IPv4 statistics */
9725 9725  static mblk_t *
9726 9726  ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9727 9727      ip_stack_t *ipst, boolean_t legacy_req)
9728 9728  {
9729 9729          mib2_ip_t               old_ip_mib;
9730 9730          struct opthdr           *optp;
9731 9731          mblk_t                  *mp2ctl;
9732 9732          mib2_ipAddrEntry_t      mae;
9733 9733  
9734 9734          /*
9735 9735           * make a copy of the original message
9736 9736           */
9737 9737          mp2ctl = copymsg(mpctl);
9738 9738  
9739 9739          /* fixed length IP structure... */
9740 9740          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9741 9741          optp->level = MIB2_IP;
9742 9742          optp->name = 0;
9743 9743          SET_MIB(old_ip_mib.ipForwarding,
9744 9744              (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9745 9745          SET_MIB(old_ip_mib.ipDefaultTTL,
9746 9746              (uint32_t)ipst->ips_ip_def_ttl);
9747 9747          SET_MIB(old_ip_mib.ipReasmTimeout,
9748 9748              ipst->ips_ip_reassembly_timeout);
9749 9749          SET_MIB(old_ip_mib.ipAddrEntrySize,
9750 9750              (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9751 9751              sizeof (mib2_ipAddrEntry_t));
9752 9752          SET_MIB(old_ip_mib.ipRouteEntrySize,
9753 9753              sizeof (mib2_ipRouteEntry_t));
9754 9754          SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9755 9755              sizeof (mib2_ipNetToMediaEntry_t));
9756 9756          SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9757 9757          SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9758 9758          SET_MIB(old_ip_mib.ipRouteAttributeSize,
9759 9759              sizeof (mib2_ipAttributeEntry_t));
9760 9760          SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t));
9761 9761          SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9762 9762  
9763 9763          /*
9764 9764           * Grab the statistics from the new IP MIB
9765 9765           */
9766 9766          SET_MIB(old_ip_mib.ipInReceives,
9767 9767              (uint32_t)ipmib->ipIfStatsHCInReceives);
9768 9768          SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9769 9769          SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9770 9770          SET_MIB(old_ip_mib.ipForwDatagrams,
9771 9771              (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9772 9772          SET_MIB(old_ip_mib.ipInUnknownProtos,
9773 9773              ipmib->ipIfStatsInUnknownProtos);
9774 9774          SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9775 9775          SET_MIB(old_ip_mib.ipInDelivers,
9776 9776              (uint32_t)ipmib->ipIfStatsHCInDelivers);
9777 9777          SET_MIB(old_ip_mib.ipOutRequests,
9778 9778              (uint32_t)ipmib->ipIfStatsHCOutRequests);
9779 9779          SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9780 9780          SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9781 9781          SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9782 9782          SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9783 9783          SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9784 9784          SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9785 9785          SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9786 9786          SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9787 9787  
9788 9788          /* ipRoutingDiscards is not being used */
9789 9789          SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9790 9790          SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9791 9791          SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9792 9792          SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9793 9793          SET_MIB(old_ip_mib.ipReasmDuplicates,
9794 9794              ipmib->ipIfStatsReasmDuplicates);
9795 9795          SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9796 9796          SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9797 9797          SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9798 9798          SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9799 9799          SET_MIB(old_ip_mib.rawipInOverflows,
9800 9800              ipmib->rawipIfStatsInOverflows);
9801 9801  
9802 9802          SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9803 9803          SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9804 9804          SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9805 9805          SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9806 9806          SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9807 9807              ipmib->ipIfStatsOutSwitchIPVersion);
9808 9808  
9809 9809          if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9810 9810              (int)sizeof (old_ip_mib))) {
9811 9811                  ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9812 9812                      (uint_t)sizeof (old_ip_mib)));
9813 9813          }
9814 9814  
9815 9815          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9816 9816          ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9817 9817              (int)optp->level, (int)optp->name, (int)optp->len));
9818 9818          qreply(q, mpctl);
9819 9819          return (mp2ctl);
9820 9820  }
9821 9821  
9822 9822  /* Per interface IPv4 statistics */
9823 9823  static mblk_t *
9824 9824  ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9825 9825      boolean_t legacy_req)
9826 9826  {
9827 9827          struct opthdr           *optp;
9828 9828          mblk_t                  *mp2ctl;
9829 9829          ill_t                   *ill;
9830 9830          ill_walk_context_t      ctx;
9831 9831          mblk_t                  *mp_tail = NULL;
9832 9832          mib2_ipIfStatsEntry_t   global_ip_mib;
9833 9833          mib2_ipAddrEntry_t      mae;
9834 9834  
9835 9835          /*
9836 9836           * Make a copy of the original message
9837 9837           */
9838 9838          mp2ctl = copymsg(mpctl);
9839 9839  
9840 9840          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9841 9841          optp->level = MIB2_IP;
9842 9842          optp->name = MIB2_IP_TRAFFIC_STATS;
9843 9843          /* Include "unknown interface" ip_mib */
9844 9844          ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9845 9845          ipst->ips_ip_mib.ipIfStatsIfIndex =
9846 9846              MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9847 9847          SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9848 9848              (ipst->ips_ip_forwarding ? 1 : 2));
9849 9849          SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9850 9850              (uint32_t)ipst->ips_ip_def_ttl);
9851 9851          SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9852 9852              sizeof (mib2_ipIfStatsEntry_t));
9853 9853          SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9854 9854              sizeof (mib2_ipAddrEntry_t));
9855 9855          SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9856 9856              sizeof (mib2_ipRouteEntry_t));
9857 9857          SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9858 9858              sizeof (mib2_ipNetToMediaEntry_t));
9859 9859          SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9860 9860              sizeof (ip_member_t));
9861 9861          SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9862 9862              sizeof (ip_grpsrc_t));
9863 9863  
9864 9864          bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9865 9865  
9866 9866          if (legacy_req) {
9867 9867                  SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9868 9868                      LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9869 9869          }
9870 9870  
9871 9871          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9872 9872              (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9873 9873                  ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9874 9874                      "failed to allocate %u bytes\n",
9875 9875                      (uint_t)sizeof (global_ip_mib)));
9876 9876          }
9877 9877  
9878 9878          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9879 9879          ill = ILL_START_WALK_V4(&ctx, ipst);
9880 9880          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9881 9881                  ill->ill_ip_mib->ipIfStatsIfIndex =
9882 9882                      ill->ill_phyint->phyint_ifindex;
9883 9883                  SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9884 9884                      (ipst->ips_ip_forwarding ? 1 : 2));
9885 9885                  SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9886 9886                      (uint32_t)ipst->ips_ip_def_ttl);
9887 9887  
9888 9888                  ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9889 9889                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9890 9890                      (char *)ill->ill_ip_mib,
9891 9891                      (int)sizeof (*ill->ill_ip_mib))) {
9892 9892                          ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9893 9893                              "failed to allocate %u bytes\n",
9894 9894                              (uint_t)sizeof (*ill->ill_ip_mib)));
9895 9895                  }
9896 9896          }
9897 9897          rw_exit(&ipst->ips_ill_g_lock);
9898 9898  
9899 9899          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9900 9900          ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9901 9901              "level %d, name %d, len %d\n",
9902 9902              (int)optp->level, (int)optp->name, (int)optp->len));
9903 9903          qreply(q, mpctl);
9904 9904  
9905 9905          if (mp2ctl == NULL)
9906 9906                  return (NULL);
9907 9907  
9908 9908          return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9909 9909              legacy_req));
9910 9910  }
9911 9911  
9912 9912  /* Global IPv4 ICMP statistics */
9913 9913  static mblk_t *
9914 9914  ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9915 9915  {
9916 9916          struct opthdr           *optp;
9917 9917          mblk_t                  *mp2ctl;
9918 9918  
9919 9919          /*
9920 9920           * Make a copy of the original message
9921 9921           */
9922 9922          mp2ctl = copymsg(mpctl);
9923 9923  
9924 9924          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9925 9925          optp->level = MIB2_ICMP;
9926 9926          optp->name = 0;
9927 9927          if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9928 9928              (int)sizeof (ipst->ips_icmp_mib))) {
9929 9929                  ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9930 9930                      (uint_t)sizeof (ipst->ips_icmp_mib)));
9931 9931          }
9932 9932          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9933 9933          ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9934 9934              (int)optp->level, (int)optp->name, (int)optp->len));
9935 9935          qreply(q, mpctl);
9936 9936          return (mp2ctl);
9937 9937  }
9938 9938  
9939 9939  /* Global IPv4 IGMP statistics */
9940 9940  static mblk_t *
9941 9941  ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9942 9942  {
9943 9943          struct opthdr           *optp;
9944 9944          mblk_t                  *mp2ctl;
9945 9945  
9946 9946          /*
9947 9947           * make a copy of the original message
9948 9948           */
9949 9949          mp2ctl = copymsg(mpctl);
9950 9950  
9951 9951          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9952 9952          optp->level = EXPER_IGMP;
9953 9953          optp->name = 0;
9954 9954          if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9955 9955              (int)sizeof (ipst->ips_igmpstat))) {
9956 9956                  ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9957 9957                      (uint_t)sizeof (ipst->ips_igmpstat)));
9958 9958          }
9959 9959          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9960 9960          ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9961 9961              (int)optp->level, (int)optp->name, (int)optp->len));
9962 9962          qreply(q, mpctl);
9963 9963          return (mp2ctl);
9964 9964  }
9965 9965  
9966 9966  /* Global IPv4 Multicast Routing statistics */
9967 9967  static mblk_t *
9968 9968  ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9969 9969  {
9970 9970          struct opthdr           *optp;
9971 9971          mblk_t                  *mp2ctl;
9972 9972  
9973 9973          /*
9974 9974           * make a copy of the original message
9975 9975           */
9976 9976          mp2ctl = copymsg(mpctl);
9977 9977  
9978 9978          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9979 9979          optp->level = EXPER_DVMRP;
9980 9980          optp->name = 0;
9981 9981          if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9982 9982                  ip0dbg(("ip_mroute_stats: failed\n"));
9983 9983          }
9984 9984          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9985 9985          ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9986 9986              (int)optp->level, (int)optp->name, (int)optp->len));
9987 9987          qreply(q, mpctl);
9988 9988          return (mp2ctl);
9989 9989  }
9990 9990  
9991 9991  /* IPv4 address information */
9992 9992  static mblk_t *
9993 9993  ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9994 9994      boolean_t legacy_req)
9995 9995  {
9996 9996          struct opthdr           *optp;
9997 9997          mblk_t                  *mp2ctl;
9998 9998          mblk_t                  *mp_tail = NULL;
9999 9999          ill_t                   *ill;
10000 10000          ipif_t                  *ipif;
10001 10001          uint_t                  bitval;
10002 10002          mib2_ipAddrEntry_t      mae;
10003 10003          size_t                  mae_size;
10004 10004          zoneid_t                zoneid;
10005 10005          ill_walk_context_t      ctx;
10006 10006  
10007 10007          /*
10008 10008           * make a copy of the original message
10009 10009           */
10010 10010          mp2ctl = copymsg(mpctl);
10011 10011  
10012 10012          mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
10013 10013              sizeof (mib2_ipAddrEntry_t);
10014 10014  
10015 10015          /* ipAddrEntryTable */
10016 10016  
10017 10017          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10018 10018          optp->level = MIB2_IP;
10019 10019          optp->name = MIB2_IP_ADDR;
10020 10020          zoneid = Q_TO_CONN(q)->conn_zoneid;
10021 10021  
10022 10022          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10023 10023          ill = ILL_START_WALK_V4(&ctx, ipst);
10024 10024          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10025 10025                  for (ipif = ill->ill_ipif; ipif != NULL;
10026 10026                      ipif = ipif->ipif_next) {
10027 10027                          if (ipif->ipif_zoneid != zoneid &&
10028 10028                              ipif->ipif_zoneid != ALL_ZONES)
10029 10029                                  continue;
10030 10030                          /* Sum of count from dead IRE_LO* and our current */
10031 10031                          mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10032 10032                          if (ipif->ipif_ire_local != NULL) {
10033 10033                                  mae.ipAdEntInfo.ae_ibcnt +=
10034 10034                                      ipif->ipif_ire_local->ire_ib_pkt_count;
10035 10035                          }
10036 10036                          mae.ipAdEntInfo.ae_obcnt = 0;
10037 10037                          mae.ipAdEntInfo.ae_focnt = 0;
10038 10038  
10039 10039                          ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
10040 10040                              OCTET_LENGTH);
10041 10041                          mae.ipAdEntIfIndex.o_length =
10042 10042                              mi_strlen(mae.ipAdEntIfIndex.o_bytes);
10043 10043                          mae.ipAdEntAddr = ipif->ipif_lcl_addr;
10044 10044                          mae.ipAdEntNetMask = ipif->ipif_net_mask;
10045 10045                          mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
10046 10046                          mae.ipAdEntInfo.ae_subnet_len =
10047 10047                              ip_mask_to_plen(ipif->ipif_net_mask);
10048 10048                          mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
10049 10049                          for (bitval = 1;
10050 10050                              bitval &&
10051 10051                              !(bitval & ipif->ipif_brd_addr);
10052 10052                              bitval <<= 1)
10053 10053                                  noop;
10054 10054                          mae.ipAdEntBcastAddr = bitval;
10055 10055                          mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
10056 10056                          mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10057 10057                          mae.ipAdEntInfo.ae_metric  = ipif->ipif_ill->ill_metric;
10058 10058                          mae.ipAdEntInfo.ae_broadcast_addr =
10059 10059                              ipif->ipif_brd_addr;
10060 10060                          mae.ipAdEntInfo.ae_pp_dst_addr =
10061 10061                              ipif->ipif_pp_dst_addr;
10062 10062                          mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
10063 10063                              ill->ill_flags | ill->ill_phyint->phyint_flags;
10064 10064                          mae.ipAdEntRetransmitTime =
10065 10065                              ill->ill_reachable_retrans_time;
10066 10066  
10067 10067                          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10068 10068                              (char *)&mae, (int)mae_size)) {
10069 10069                                  ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
10070 10070                                      "allocate %u bytes\n", (uint_t)mae_size));
10071 10071                          }
10072 10072                  }
10073 10073          }
10074 10074          rw_exit(&ipst->ips_ill_g_lock);
10075 10075  
10076 10076          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10077 10077          ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
10078 10078              (int)optp->level, (int)optp->name, (int)optp->len));
10079 10079          qreply(q, mpctl);
10080 10080          return (mp2ctl);
10081 10081  }
10082 10082  
10083 10083  /* IPv6 address information */
10084 10084  static mblk_t *
10085 10085  ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10086 10086      boolean_t legacy_req)
10087 10087  {
10088 10088          struct opthdr           *optp;
10089 10089          mblk_t                  *mp2ctl;
10090 10090          mblk_t                  *mp_tail = NULL;
10091 10091          ill_t                   *ill;
10092 10092          ipif_t                  *ipif;
10093 10093          mib2_ipv6AddrEntry_t    mae6;
10094 10094          size_t                  mae6_size;
10095 10095          zoneid_t                zoneid;
10096 10096          ill_walk_context_t      ctx;
10097 10097  
10098 10098          /*
10099 10099           * make a copy of the original message
10100 10100           */
10101 10101          mp2ctl = copymsg(mpctl);
10102 10102  
10103 10103          mae6_size = (legacy_req) ?
10104 10104              LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
10105 10105              sizeof (mib2_ipv6AddrEntry_t);
10106 10106  
10107 10107          /* ipv6AddrEntryTable */
10108 10108  
10109 10109          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10110 10110          optp->level = MIB2_IP6;
10111 10111          optp->name = MIB2_IP6_ADDR;
10112 10112          zoneid = Q_TO_CONN(q)->conn_zoneid;
10113 10113  
10114 10114          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10115 10115          ill = ILL_START_WALK_V6(&ctx, ipst);
10116 10116          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10117 10117                  for (ipif = ill->ill_ipif; ipif != NULL;
10118 10118                      ipif = ipif->ipif_next) {
10119 10119                          if (ipif->ipif_zoneid != zoneid &&
10120 10120                              ipif->ipif_zoneid != ALL_ZONES)
10121 10121                                  continue;
10122 10122                          /* Sum of count from dead IRE_LO* and our current */
10123 10123                          mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10124 10124                          if (ipif->ipif_ire_local != NULL) {
10125 10125                                  mae6.ipv6AddrInfo.ae_ibcnt +=
10126 10126                                      ipif->ipif_ire_local->ire_ib_pkt_count;
10127 10127                          }
10128 10128                          mae6.ipv6AddrInfo.ae_obcnt = 0;
10129 10129                          mae6.ipv6AddrInfo.ae_focnt = 0;
10130 10130  
10131 10131                          ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
10132 10132                              OCTET_LENGTH);
10133 10133                          mae6.ipv6AddrIfIndex.o_length =
10134 10134                              mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
10135 10135                          mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
10136 10136                          mae6.ipv6AddrPfxLength =
10137 10137                              ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10138 10138                          mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
10139 10139                          mae6.ipv6AddrInfo.ae_subnet_len =
10140 10140                              mae6.ipv6AddrPfxLength;
10141 10141                          mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
10142 10142  
10143 10143                          /* Type: stateless(1), stateful(2), unknown(3) */
10144 10144                          if (ipif->ipif_flags & IPIF_ADDRCONF)
10145 10145                                  mae6.ipv6AddrType = 1;
10146 10146                          else
10147 10147                                  mae6.ipv6AddrType = 2;
10148 10148                          /* Anycast: true(1), false(2) */
10149 10149                          if (ipif->ipif_flags & IPIF_ANYCAST)
10150 10150                                  mae6.ipv6AddrAnycastFlag = 1;
10151 10151                          else
10152 10152                                  mae6.ipv6AddrAnycastFlag = 2;
10153 10153  
10154 10154                          /*
10155 10155                           * Address status: preferred(1), deprecated(2),
10156 10156                           * invalid(3), inaccessible(4), unknown(5)
10157 10157                           */
10158 10158                          if (ipif->ipif_flags & IPIF_NOLOCAL)
10159 10159                                  mae6.ipv6AddrStatus = 3;
10160 10160                          else if (ipif->ipif_flags & IPIF_DEPRECATED)
10161 10161                                  mae6.ipv6AddrStatus = 2;
10162 10162                          else
10163 10163                                  mae6.ipv6AddrStatus = 1;
10164 10164                          mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10165 10165                          mae6.ipv6AddrInfo.ae_metric  =
10166 10166                              ipif->ipif_ill->ill_metric;
10167 10167                          mae6.ipv6AddrInfo.ae_pp_dst_addr =
10168 10168                              ipif->ipif_v6pp_dst_addr;
10169 10169                          mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
10170 10170                              ill->ill_flags | ill->ill_phyint->phyint_flags;
10171 10171                          mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
10172 10172                          mae6.ipv6AddrIdentifier = ill->ill_token;
10173 10173                          mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
10174 10174                          mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
10175 10175                          mae6.ipv6AddrRetransmitTime =
10176 10176                              ill->ill_reachable_retrans_time;
10177 10177                          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10178 10178                              (char *)&mae6, (int)mae6_size)) {
10179 10179                                  ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
10180 10180                                      "allocate %u bytes\n",
10181 10181                                      (uint_t)mae6_size));
10182 10182                          }
10183 10183                  }
10184 10184          }
10185 10185          rw_exit(&ipst->ips_ill_g_lock);
10186 10186  
10187 10187          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10188 10188          ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
10189 10189              (int)optp->level, (int)optp->name, (int)optp->len));
10190 10190          qreply(q, mpctl);
10191 10191          return (mp2ctl);
10192 10192  }
10193 10193  
10194 10194  /* IPv4 multicast group membership. */
10195 10195  static mblk_t *
10196 10196  ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10197 10197  {
10198 10198          struct opthdr           *optp;
10199 10199          mblk_t                  *mp2ctl;
10200 10200          ill_t                   *ill;
10201 10201          ipif_t                  *ipif;
10202 10202          ilm_t                   *ilm;
10203 10203          ip_member_t             ipm;
10204 10204          mblk_t                  *mp_tail = NULL;
10205 10205          ill_walk_context_t      ctx;
10206 10206          zoneid_t                zoneid;
10207 10207  
10208 10208          /*
10209 10209           * make a copy of the original message
10210 10210           */
10211 10211          mp2ctl = copymsg(mpctl);
10212 10212          zoneid = Q_TO_CONN(q)->conn_zoneid;
10213 10213  
10214 10214          /* ipGroupMember table */
10215 10215          optp = (struct opthdr *)&mpctl->b_rptr[
10216 10216              sizeof (struct T_optmgmt_ack)];
10217 10217          optp->level = MIB2_IP;
10218 10218          optp->name = EXPER_IP_GROUP_MEMBERSHIP;
10219 10219  
10220 10220          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10221 10221          ill = ILL_START_WALK_V4(&ctx, ipst);
10222 10222          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10223 10223                  /* Make sure the ill isn't going away. */
10224 10224                  if (!ill_check_and_refhold(ill))
10225 10225                          continue;
10226 10226                  rw_exit(&ipst->ips_ill_g_lock);
10227 10227                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10228 10228                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10229 10229                          if (ilm->ilm_zoneid != zoneid &&
10230 10230                              ilm->ilm_zoneid != ALL_ZONES)
10231 10231                                  continue;
10232 10232  
10233 10233                          /* Is there an ipif for ilm_ifaddr? */
10234 10234                          for (ipif = ill->ill_ipif; ipif != NULL;
10235 10235                              ipif = ipif->ipif_next) {
10236 10236                                  if (!IPIF_IS_CONDEMNED(ipif) &&
10237 10237                                      ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10238 10238                                      ilm->ilm_ifaddr != INADDR_ANY)
10239 10239                                          break;
10240 10240                          }
10241 10241                          if (ipif != NULL) {
10242 10242                                  ipif_get_name(ipif,
10243 10243                                      ipm.ipGroupMemberIfIndex.o_bytes,
10244 10244                                      OCTET_LENGTH);
10245 10245                          } else {
10246 10246                                  ill_get_name(ill,
10247 10247                                      ipm.ipGroupMemberIfIndex.o_bytes,
10248 10248                                      OCTET_LENGTH);
10249 10249                          }
10250 10250                          ipm.ipGroupMemberIfIndex.o_length =
10251 10251                              mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
10252 10252  
10253 10253                          ipm.ipGroupMemberAddress = ilm->ilm_addr;
10254 10254                          ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
10255 10255                          ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
10256 10256                          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10257 10257                              (char *)&ipm, (int)sizeof (ipm))) {
10258 10258                                  ip1dbg(("ip_snmp_get_mib2_ip_group: "
10259 10259                                      "failed to allocate %u bytes\n",
10260 10260                                      (uint_t)sizeof (ipm)));
10261 10261                          }
10262 10262                  }
10263 10263                  rw_exit(&ill->ill_mcast_lock);
10264 10264                  ill_refrele(ill);
10265 10265                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10266 10266          }
10267 10267          rw_exit(&ipst->ips_ill_g_lock);
10268 10268          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10269 10269          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10270 10270              (int)optp->level, (int)optp->name, (int)optp->len));
10271 10271          qreply(q, mpctl);
10272 10272          return (mp2ctl);
10273 10273  }
10274 10274  
10275 10275  /* IPv6 multicast group membership. */
10276 10276  static mblk_t *
10277 10277  ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10278 10278  {
10279 10279          struct opthdr           *optp;
10280 10280          mblk_t                  *mp2ctl;
10281 10281          ill_t                   *ill;
10282 10282          ilm_t                   *ilm;
10283 10283          ipv6_member_t           ipm6;
10284 10284          mblk_t                  *mp_tail = NULL;
10285 10285          ill_walk_context_t      ctx;
10286 10286          zoneid_t                zoneid;
10287 10287  
10288 10288          /*
10289 10289           * make a copy of the original message
10290 10290           */
10291 10291          mp2ctl = copymsg(mpctl);
10292 10292          zoneid = Q_TO_CONN(q)->conn_zoneid;
10293 10293  
10294 10294          /* ip6GroupMember table */
10295 10295          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10296 10296          optp->level = MIB2_IP6;
10297 10297          optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10298 10298  
10299 10299          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10300 10300          ill = ILL_START_WALK_V6(&ctx, ipst);
10301 10301          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10302 10302                  /* Make sure the ill isn't going away. */
10303 10303                  if (!ill_check_and_refhold(ill))
10304 10304                          continue;
10305 10305                  rw_exit(&ipst->ips_ill_g_lock);
10306 10306                  /*
10307 10307                   * Normally we don't have any members on under IPMP interfaces.
10308 10308                   * We report them as a debugging aid.
10309 10309                   */
10310 10310                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10311 10311                  ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10312 10312                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10313 10313                          if (ilm->ilm_zoneid != zoneid &&
10314 10314                              ilm->ilm_zoneid != ALL_ZONES)
10315 10315                                  continue;       /* not this zone */
10316 10316                          ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10317 10317                          ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10318 10318                          ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10319 10319                          if (!snmp_append_data2(mpctl->b_cont,
10320 10320                              &mp_tail,
10321 10321                              (char *)&ipm6, (int)sizeof (ipm6))) {
10322 10322                                  ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10323 10323                                      "failed to allocate %u bytes\n",
10324 10324                                      (uint_t)sizeof (ipm6)));
10325 10325                          }
10326 10326                  }
10327 10327                  rw_exit(&ill->ill_mcast_lock);
10328 10328                  ill_refrele(ill);
10329 10329                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10330 10330          }
10331 10331          rw_exit(&ipst->ips_ill_g_lock);
10332 10332  
10333 10333          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10334 10334          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10335 10335              (int)optp->level, (int)optp->name, (int)optp->len));
10336 10336          qreply(q, mpctl);
10337 10337          return (mp2ctl);
10338 10338  }
10339 10339  
10340 10340  /* IP multicast filtered sources */
10341 10341  static mblk_t *
10342 10342  ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10343 10343  {
10344 10344          struct opthdr           *optp;
10345 10345          mblk_t                  *mp2ctl;
10346 10346          ill_t                   *ill;
10347 10347          ipif_t                  *ipif;
10348 10348          ilm_t                   *ilm;
10349 10349          ip_grpsrc_t             ips;
10350 10350          mblk_t                  *mp_tail = NULL;
10351 10351          ill_walk_context_t      ctx;
10352 10352          zoneid_t                zoneid;
10353 10353          int                     i;
10354 10354          slist_t                 *sl;
10355 10355  
10356 10356          /*
10357 10357           * make a copy of the original message
10358 10358           */
10359 10359          mp2ctl = copymsg(mpctl);
10360 10360          zoneid = Q_TO_CONN(q)->conn_zoneid;
10361 10361  
10362 10362          /* ipGroupSource table */
10363 10363          optp = (struct opthdr *)&mpctl->b_rptr[
10364 10364              sizeof (struct T_optmgmt_ack)];
10365 10365          optp->level = MIB2_IP;
10366 10366          optp->name = EXPER_IP_GROUP_SOURCES;
10367 10367  
10368 10368          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10369 10369          ill = ILL_START_WALK_V4(&ctx, ipst);
10370 10370          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10371 10371                  /* Make sure the ill isn't going away. */
10372 10372                  if (!ill_check_and_refhold(ill))
10373 10373                          continue;
10374 10374                  rw_exit(&ipst->ips_ill_g_lock);
10375 10375                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10376 10376                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10377 10377                          sl = ilm->ilm_filter;
10378 10378                          if (ilm->ilm_zoneid != zoneid &&
10379 10379                              ilm->ilm_zoneid != ALL_ZONES)
10380 10380                                  continue;
10381 10381                          if (SLIST_IS_EMPTY(sl))
10382 10382                                  continue;
10383 10383  
10384 10384                          /* Is there an ipif for ilm_ifaddr? */
10385 10385                          for (ipif = ill->ill_ipif; ipif != NULL;
10386 10386                              ipif = ipif->ipif_next) {
10387 10387                                  if (!IPIF_IS_CONDEMNED(ipif) &&
10388 10388                                      ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10389 10389                                      ilm->ilm_ifaddr != INADDR_ANY)
10390 10390                                          break;
10391 10391                          }
10392 10392                          if (ipif != NULL) {
10393 10393                                  ipif_get_name(ipif,
10394 10394                                      ips.ipGroupSourceIfIndex.o_bytes,
10395 10395                                      OCTET_LENGTH);
10396 10396                          } else {
10397 10397                                  ill_get_name(ill,
10398 10398                                      ips.ipGroupSourceIfIndex.o_bytes,
10399 10399                                      OCTET_LENGTH);
10400 10400                          }
10401 10401                          ips.ipGroupSourceIfIndex.o_length =
10402 10402                              mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10403 10403  
10404 10404                          ips.ipGroupSourceGroup = ilm->ilm_addr;
10405 10405                          for (i = 0; i < sl->sl_numsrc; i++) {
10406 10406                                  if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10407 10407                                          continue;
10408 10408                                  IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10409 10409                                      ips.ipGroupSourceAddress);
10410 10410                                  if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10411 10411                                      (char *)&ips, (int)sizeof (ips)) == 0) {
10412 10412                                          ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10413 10413                                              " failed to allocate %u bytes\n",
10414 10414                                              (uint_t)sizeof (ips)));
10415 10415                                  }
10416 10416                          }
10417 10417                  }
10418 10418                  rw_exit(&ill->ill_mcast_lock);
10419 10419                  ill_refrele(ill);
10420 10420                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10421 10421          }
10422 10422          rw_exit(&ipst->ips_ill_g_lock);
10423 10423          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10424 10424          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10425 10425              (int)optp->level, (int)optp->name, (int)optp->len));
10426 10426          qreply(q, mpctl);
10427 10427          return (mp2ctl);
10428 10428  }
10429 10429  
10430 10430  /* IPv6 multicast filtered sources. */
10431 10431  static mblk_t *
10432 10432  ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10433 10433  {
10434 10434          struct opthdr           *optp;
10435 10435          mblk_t                  *mp2ctl;
10436 10436          ill_t                   *ill;
10437 10437          ilm_t                   *ilm;
10438 10438          ipv6_grpsrc_t           ips6;
10439 10439          mblk_t                  *mp_tail = NULL;
10440 10440          ill_walk_context_t      ctx;
10441 10441          zoneid_t                zoneid;
10442 10442          int                     i;
10443 10443          slist_t                 *sl;
10444 10444  
10445 10445          /*
10446 10446           * make a copy of the original message
10447 10447           */
10448 10448          mp2ctl = copymsg(mpctl);
10449 10449          zoneid = Q_TO_CONN(q)->conn_zoneid;
10450 10450  
10451 10451          /* ip6GroupMember table */
10452 10452          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10453 10453          optp->level = MIB2_IP6;
10454 10454          optp->name = EXPER_IP6_GROUP_SOURCES;
10455 10455  
10456 10456          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10457 10457          ill = ILL_START_WALK_V6(&ctx, ipst);
10458 10458          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10459 10459                  /* Make sure the ill isn't going away. */
10460 10460                  if (!ill_check_and_refhold(ill))
10461 10461                          continue;
10462 10462                  rw_exit(&ipst->ips_ill_g_lock);
10463 10463                  /*
10464 10464                   * Normally we don't have any members on under IPMP interfaces.
10465 10465                   * We report them as a debugging aid.
10466 10466                   */
10467 10467                  rw_enter(&ill->ill_mcast_lock, RW_READER);
10468 10468                  ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10469 10469                  for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10470 10470                          sl = ilm->ilm_filter;
10471 10471                          if (ilm->ilm_zoneid != zoneid &&
10472 10472                              ilm->ilm_zoneid != ALL_ZONES)
10473 10473                                  continue;
10474 10474                          if (SLIST_IS_EMPTY(sl))
10475 10475                                  continue;
10476 10476                          ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10477 10477                          for (i = 0; i < sl->sl_numsrc; i++) {
10478 10478                                  ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10479 10479                                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10480 10480                                      (char *)&ips6, (int)sizeof (ips6))) {
10481 10481                                          ip1dbg(("ip_snmp_get_mib2_ip6_"
10482 10482                                              "group_src: failed to allocate "
10483 10483                                              "%u bytes\n",
10484 10484                                              (uint_t)sizeof (ips6)));
10485 10485                                  }
10486 10486                          }
10487 10487                  }
10488 10488                  rw_exit(&ill->ill_mcast_lock);
10489 10489                  ill_refrele(ill);
10490 10490                  rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10491 10491          }
10492 10492          rw_exit(&ipst->ips_ill_g_lock);
10493 10493  
10494 10494          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10495 10495          ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10496 10496              (int)optp->level, (int)optp->name, (int)optp->len));
10497 10497          qreply(q, mpctl);
10498 10498          return (mp2ctl);
10499 10499  }
10500 10500  
10501 10501  /* Multicast routing virtual interface table. */
10502 10502  static mblk_t *
10503 10503  ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10504 10504  {
10505 10505          struct opthdr           *optp;
10506 10506          mblk_t                  *mp2ctl;
10507 10507  
10508 10508          /*
10509 10509           * make a copy of the original message
10510 10510           */
10511 10511          mp2ctl = copymsg(mpctl);
10512 10512  
10513 10513          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10514 10514          optp->level = EXPER_DVMRP;
10515 10515          optp->name = EXPER_DVMRP_VIF;
10516 10516          if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10517 10517                  ip0dbg(("ip_mroute_vif: failed\n"));
10518 10518          }
10519 10519          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10520 10520          ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10521 10521              (int)optp->level, (int)optp->name, (int)optp->len));
10522 10522          qreply(q, mpctl);
10523 10523          return (mp2ctl);
10524 10524  }
10525 10525  
10526 10526  /* Multicast routing table. */
10527 10527  static mblk_t *
10528 10528  ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10529 10529  {
10530 10530          struct opthdr           *optp;
10531 10531          mblk_t                  *mp2ctl;
10532 10532  
10533 10533          /*
10534 10534           * make a copy of the original message
10535 10535           */
10536 10536          mp2ctl = copymsg(mpctl);
10537 10537  
10538 10538          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10539 10539          optp->level = EXPER_DVMRP;
10540 10540          optp->name = EXPER_DVMRP_MRT;
10541 10541          if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10542 10542                  ip0dbg(("ip_mroute_mrt: failed\n"));
10543 10543          }
10544 10544          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10545 10545          ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10546 10546              (int)optp->level, (int)optp->name, (int)optp->len));
10547 10547          qreply(q, mpctl);
10548 10548          return (mp2ctl);
10549 10549  }
10550 10550  
10551 10551  /*
10552 10552   * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable
10553 10553   * in one IRE walk.
10554 10554   */
10555 10555  static mblk_t *
10556 10556  ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10557 10557      ip_stack_t *ipst)
10558 10558  {
10559 10559          struct opthdr   *optp;
10560 10560          mblk_t          *mp2ctl;        /* Returned */
10561 10561          mblk_t          *mp3ctl;        /* nettomedia */
10562 10562          mblk_t          *mp4ctl;        /* routeattrs */
10563 10563          iproutedata_t   ird;
10564 10564          zoneid_t        zoneid;
10565 10565  
10566 10566          /*
10567 10567           * make copies of the original message
10568 10568           *      - mp2ctl is returned unchanged to the caller for its use
10569 10569           *      - mpctl is sent upstream as ipRouteEntryTable
10570 10570           *      - mp3ctl is sent upstream as ipNetToMediaEntryTable
10571 10571           *      - mp4ctl is sent upstream as ipRouteAttributeTable
10572 10572           */
10573 10573          mp2ctl = copymsg(mpctl);
10574 10574          mp3ctl = copymsg(mpctl);
10575 10575          mp4ctl = copymsg(mpctl);
10576 10576          if (mp3ctl == NULL || mp4ctl == NULL) {
10577 10577                  freemsg(mp4ctl);
10578 10578                  freemsg(mp3ctl);
10579 10579                  freemsg(mp2ctl);
10580 10580                  freemsg(mpctl);
10581 10581                  return (NULL);
10582 10582          }
10583 10583  
10584 10584          bzero(&ird, sizeof (ird));
10585 10585  
10586 10586          ird.ird_route.lp_head = mpctl->b_cont;
10587 10587          ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10588 10588          ird.ird_attrs.lp_head = mp4ctl->b_cont;
10589 10589          /*
10590 10590           * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10591 10591           * then also include ire_testhidden IREs and IRE_IF_CLONE.  This is
10592 10592           * intended a temporary solution until a proper MIB API is provided
10593 10593           * that provides complete filtering/caller-opt-in.
10594 10594           */
10595 10595          if (level == EXPER_IP_AND_ALL_IRES)
10596 10596                  ird.ird_flags |= IRD_REPORT_ALL;
10597 10597  
10598 10598          zoneid = Q_TO_CONN(q)->conn_zoneid;
10599 10599          ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10600 10600  
10601 10601          /* ipRouteEntryTable in mpctl */
10602 10602          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10603 10603          optp->level = MIB2_IP;
10604 10604          optp->name = MIB2_IP_ROUTE;
10605 10605          optp->len = msgdsize(ird.ird_route.lp_head);
10606 10606          ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10607 10607              (int)optp->level, (int)optp->name, (int)optp->len));
10608 10608          qreply(q, mpctl);
10609 10609  
10610 10610          /* ipNetToMediaEntryTable in mp3ctl */
10611 10611          ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10612 10612  
10613 10613          optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10614 10614          optp->level = MIB2_IP;
10615 10615          optp->name = MIB2_IP_MEDIA;
10616 10616          optp->len = msgdsize(ird.ird_netmedia.lp_head);
10617 10617          ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10618 10618              (int)optp->level, (int)optp->name, (int)optp->len));
10619 10619          qreply(q, mp3ctl);
10620 10620  
10621 10621          /* ipRouteAttributeTable in mp4ctl */
10622 10622          optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10623 10623          optp->level = MIB2_IP;
10624 10624          optp->name = EXPER_IP_RTATTR;
10625 10625          optp->len = msgdsize(ird.ird_attrs.lp_head);
10626 10626          ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10627 10627              (int)optp->level, (int)optp->name, (int)optp->len));
10628 10628          if (optp->len == 0)
10629 10629                  freemsg(mp4ctl);
10630 10630          else
10631 10631                  qreply(q, mp4ctl);
10632 10632  
10633 10633          return (mp2ctl);
10634 10634  }
10635 10635  
10636 10636  /*
10637 10637   * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and
10638 10638   * ipv6NetToMediaEntryTable in an NDP walk.
10639 10639   */
10640 10640  static mblk_t *
10641 10641  ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10642 10642      ip_stack_t *ipst)
10643 10643  {
10644 10644          struct opthdr   *optp;
10645 10645          mblk_t          *mp2ctl;        /* Returned */
10646 10646          mblk_t          *mp3ctl;        /* nettomedia */
10647 10647          mblk_t          *mp4ctl;        /* routeattrs */
10648 10648          iproutedata_t   ird;
10649 10649          zoneid_t        zoneid;
10650 10650  
10651 10651          /*
10652 10652           * make copies of the original message
10653 10653           *      - mp2ctl is returned unchanged to the caller for its use
10654 10654           *      - mpctl is sent upstream as ipv6RouteEntryTable
10655 10655           *      - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10656 10656           *      - mp4ctl is sent upstream as ipv6RouteAttributeTable
10657 10657           */
10658 10658          mp2ctl = copymsg(mpctl);
10659 10659          mp3ctl = copymsg(mpctl);
10660 10660          mp4ctl = copymsg(mpctl);
10661 10661          if (mp3ctl == NULL || mp4ctl == NULL) {
10662 10662                  freemsg(mp4ctl);
10663 10663                  freemsg(mp3ctl);
10664 10664                  freemsg(mp2ctl);
10665 10665                  freemsg(mpctl);
10666 10666                  return (NULL);
10667 10667          }
10668 10668  
10669 10669          bzero(&ird, sizeof (ird));
10670 10670  
10671 10671          ird.ird_route.lp_head = mpctl->b_cont;
10672 10672          ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10673 10673          ird.ird_attrs.lp_head = mp4ctl->b_cont;
10674 10674          /*
10675 10675           * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10676 10676           * then also include ire_testhidden IREs and IRE_IF_CLONE.  This is
10677 10677           * intended a temporary solution until a proper MIB API is provided
10678 10678           * that provides complete filtering/caller-opt-in.
10679 10679           */
10680 10680          if (level == EXPER_IP_AND_ALL_IRES)
10681 10681                  ird.ird_flags |= IRD_REPORT_ALL;
10682 10682  
10683 10683          zoneid = Q_TO_CONN(q)->conn_zoneid;
10684 10684          ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10685 10685  
10686 10686          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10687 10687          optp->level = MIB2_IP6;
10688 10688          optp->name = MIB2_IP6_ROUTE;
10689 10689          optp->len = msgdsize(ird.ird_route.lp_head);
10690 10690          ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10691 10691              (int)optp->level, (int)optp->name, (int)optp->len));
10692 10692          qreply(q, mpctl);
10693 10693  
10694 10694          /* ipv6NetToMediaEntryTable in mp3ctl */
10695 10695          ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10696 10696  
10697 10697          optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10698 10698          optp->level = MIB2_IP6;
10699 10699          optp->name = MIB2_IP6_MEDIA;
10700 10700          optp->len = msgdsize(ird.ird_netmedia.lp_head);
10701 10701          ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10702 10702              (int)optp->level, (int)optp->name, (int)optp->len));
10703 10703          qreply(q, mp3ctl);
10704 10704  
10705 10705          /* ipv6RouteAttributeTable in mp4ctl */
10706 10706          optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10707 10707          optp->level = MIB2_IP6;
10708 10708          optp->name = EXPER_IP_RTATTR;
10709 10709          optp->len = msgdsize(ird.ird_attrs.lp_head);
10710 10710          ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10711 10711              (int)optp->level, (int)optp->name, (int)optp->len));
10712 10712          if (optp->len == 0)
10713 10713                  freemsg(mp4ctl);
10714 10714          else
10715 10715                  qreply(q, mp4ctl);
10716 10716  
10717 10717          return (mp2ctl);
10718 10718  }
10719 10719  
10720 10720  /*
10721 10721   * IPv6 mib: One per ill
10722 10722   */
10723 10723  static mblk_t *
10724 10724  ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10725 10725      boolean_t legacy_req)
10726 10726  {
10727 10727          struct opthdr           *optp;
10728 10728          mblk_t                  *mp2ctl;
10729 10729          ill_t                   *ill;
10730 10730          ill_walk_context_t      ctx;
10731 10731          mblk_t                  *mp_tail = NULL;
10732 10732          mib2_ipv6AddrEntry_t    mae6;
10733 10733          mib2_ipIfStatsEntry_t   *ise;
10734 10734          size_t                  ise_size, iae_size;
10735 10735  
10736 10736          /*
10737 10737           * Make a copy of the original message
10738 10738           */
10739 10739          mp2ctl = copymsg(mpctl);
10740 10740  
10741 10741          /* fixed length IPv6 structure ... */
10742 10742  
10743 10743          if (legacy_req) {
10744 10744                  ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10745 10745                      mib2_ipIfStatsEntry_t);
10746 10746                  iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10747 10747          } else {
10748 10748                  ise_size = sizeof (mib2_ipIfStatsEntry_t);
10749 10749                  iae_size = sizeof (mib2_ipv6AddrEntry_t);
10750 10750          }
10751 10751  
10752 10752          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10753 10753          optp->level = MIB2_IP6;
10754 10754          optp->name = 0;
10755 10755          /* Include "unknown interface" ip6_mib */
10756 10756          ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10757 10757          ipst->ips_ip6_mib.ipIfStatsIfIndex =
10758 10758              MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10759 10759          SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10760 10760              ipst->ips_ipv6_forwarding ? 1 : 2);
10761 10761          SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10762 10762              ipst->ips_ipv6_def_hops);
10763 10763          SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10764 10764              sizeof (mib2_ipIfStatsEntry_t));
10765 10765          SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10766 10766              sizeof (mib2_ipv6AddrEntry_t));
10767 10767          SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10768 10768              sizeof (mib2_ipv6RouteEntry_t));
10769 10769          SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10770 10770              sizeof (mib2_ipv6NetToMediaEntry_t));
10771 10771          SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10772 10772              sizeof (ipv6_member_t));
10773 10773          SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10774 10774              sizeof (ipv6_grpsrc_t));
10775 10775  
10776 10776          /*
10777 10777           * Synchronize 64- and 32-bit counters
10778 10778           */
10779 10779          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10780 10780              ipIfStatsHCInReceives);
10781 10781          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10782 10782              ipIfStatsHCInDelivers);
10783 10783          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10784 10784              ipIfStatsHCOutRequests);
10785 10785          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10786 10786              ipIfStatsHCOutForwDatagrams);
10787 10787          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10788 10788              ipIfStatsHCOutMcastPkts);
10789 10789          SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10790 10790              ipIfStatsHCInMcastPkts);
10791 10791  
10792 10792          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10793 10793              (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10794 10794                  ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10795 10795                      (uint_t)ise_size));
10796 10796          } else if (legacy_req) {
10797 10797                  /* Adjust the EntrySize fields for legacy requests. */
10798 10798                  ise =
10799 10799                      (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10800 10800                  SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10801 10801                  SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10802 10802          }
10803 10803  
10804 10804          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10805 10805          ill = ILL_START_WALK_V6(&ctx, ipst);
10806 10806          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10807 10807                  ill->ill_ip_mib->ipIfStatsIfIndex =
10808 10808                      ill->ill_phyint->phyint_ifindex;
10809 10809                  SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10810 10810                      ipst->ips_ipv6_forwarding ? 1 : 2);
10811 10811                  SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10812 10812                      ill->ill_max_hops);
10813 10813  
10814 10814                  /*
10815 10815                   * Synchronize 64- and 32-bit counters
10816 10816                   */
10817 10817                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10818 10818                      ipIfStatsHCInReceives);
10819 10819                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10820 10820                      ipIfStatsHCInDelivers);
10821 10821                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10822 10822                      ipIfStatsHCOutRequests);
10823 10823                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10824 10824                      ipIfStatsHCOutForwDatagrams);
10825 10825                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10826 10826                      ipIfStatsHCOutMcastPkts);
10827 10827                  SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10828 10828                      ipIfStatsHCInMcastPkts);
10829 10829  
10830 10830                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10831 10831                      (char *)ill->ill_ip_mib, (int)ise_size)) {
10832 10832                          ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10833 10833                          "%u bytes\n", (uint_t)ise_size));
10834 10834                  } else if (legacy_req) {
10835 10835                          /* Adjust the EntrySize fields for legacy requests. */
10836 10836                          ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10837 10837                              (int)ise_size);
10838 10838                          SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10839 10839                          SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10840 10840                  }
10841 10841          }
10842 10842          rw_exit(&ipst->ips_ill_g_lock);
10843 10843  
10844 10844          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10845 10845          ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10846 10846              (int)optp->level, (int)optp->name, (int)optp->len));
10847 10847          qreply(q, mpctl);
10848 10848          return (mp2ctl);
10849 10849  }
10850 10850  
10851 10851  /*
10852 10852   * ICMPv6 mib: One per ill
10853 10853   */
10854 10854  static mblk_t *
10855 10855  ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10856 10856  {
10857 10857          struct opthdr           *optp;
10858 10858          mblk_t                  *mp2ctl;
10859 10859          ill_t                   *ill;
10860 10860          ill_walk_context_t      ctx;
10861 10861          mblk_t                  *mp_tail = NULL;
10862 10862          /*
10863 10863           * Make a copy of the original message
10864 10864           */
10865 10865          mp2ctl = copymsg(mpctl);
10866 10866  
10867 10867          /* fixed length ICMPv6 structure ... */
10868 10868  
10869 10869          optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10870 10870          optp->level = MIB2_ICMP6;
10871 10871          optp->name = 0;
10872 10872          /* Include "unknown interface" icmp6_mib */
10873 10873          ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10874 10874              MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10875 10875          ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10876 10876              sizeof (mib2_ipv6IfIcmpEntry_t);
10877 10877          if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10878 10878              (char *)&ipst->ips_icmp6_mib,
10879 10879              (int)sizeof (ipst->ips_icmp6_mib))) {
10880 10880                  ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10881 10881                      (uint_t)sizeof (ipst->ips_icmp6_mib)));
10882 10882          }
10883 10883  
10884 10884          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10885 10885          ill = ILL_START_WALK_V6(&ctx, ipst);
10886 10886          for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10887 10887                  ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10888 10888                      ill->ill_phyint->phyint_ifindex;
10889 10889                  if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10890 10890                      (char *)ill->ill_icmp6_mib,
10891 10891                      (int)sizeof (*ill->ill_icmp6_mib))) {
10892 10892                          ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10893 10893                              "%u bytes\n",
10894 10894                              (uint_t)sizeof (*ill->ill_icmp6_mib)));
10895 10895                  }
10896 10896          }
10897 10897          rw_exit(&ipst->ips_ill_g_lock);
10898 10898  
10899 10899          optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10900 10900          ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10901 10901              (int)optp->level, (int)optp->name, (int)optp->len));
10902 10902          qreply(q, mpctl);
10903 10903          return (mp2ctl);
10904 10904  }
10905 10905  
10906 10906  /*
10907 10907   * ire_walk routine to create both ipRouteEntryTable and
10908 10908   * ipRouteAttributeTable in one IRE walk
10909 10909   */
10910 10910  static void
10911 10911  ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10912 10912  {
10913 10913          ill_t                           *ill;
10914 10914          mib2_ipRouteEntry_t             *re;
10915 10915          mib2_ipAttributeEntry_t         iaes;
10916 10916          tsol_ire_gw_secattr_t           *attrp;
10917 10917          tsol_gc_t                       *gc = NULL;
10918 10918          tsol_gcgrp_t                    *gcgrp = NULL;
10919 10919          ip_stack_t                      *ipst = ire->ire_ipst;
10920 10920  
10921 10921          ASSERT(ire->ire_ipversion == IPV4_VERSION);
10922 10922  
10923 10923          if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10924 10924                  if (ire->ire_testhidden)
10925 10925                          return;
10926 10926                  if (ire->ire_type & IRE_IF_CLONE)
10927 10927                          return;
10928 10928          }
10929 10929  
10930 10930          if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10931 10931                  return;
10932 10932  
10933 10933          if ((attrp = ire->ire_gw_secattr) != NULL) {
10934 10934                  mutex_enter(&attrp->igsa_lock);
10935 10935                  if ((gc = attrp->igsa_gc) != NULL) {
10936 10936                          gcgrp = gc->gc_grp;
10937 10937                          ASSERT(gcgrp != NULL);
10938 10938                          rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
10939 10939                  }
10940 10940                  mutex_exit(&attrp->igsa_lock);
10941 10941          }
10942 10942          /*
10943 10943           * Return all IRE types for route table... let caller pick and choose
10944 10944           */
10945 10945          re->ipRouteDest = ire->ire_addr;
10946 10946          ill = ire->ire_ill;
10947 10947          re->ipRouteIfIndex.o_length = 0;
10948 10948          if (ill != NULL) {
10949 10949                  ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10950 10950                  re->ipRouteIfIndex.o_length =
10951 10951                      mi_strlen(re->ipRouteIfIndex.o_bytes);
10952 10952          }
10953 10953          re->ipRouteMetric1 = -1;
10954 10954          re->ipRouteMetric2 = -1;
10955 10955          re->ipRouteMetric3 = -1;
10956 10956          re->ipRouteMetric4 = -1;
10957 10957  
10958 10958          re->ipRouteNextHop = ire->ire_gateway_addr;
10959 10959          /* indirect(4), direct(3), or invalid(2) */
10960 10960          if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10961 10961                  re->ipRouteType = 2;
10962 10962          else if (ire->ire_type & IRE_ONLINK)
10963 10963                  re->ipRouteType = 3;
10964 10964          else
10965 10965                  re->ipRouteType = 4;
10966 10966  
10967 10967          re->ipRouteProto = -1;
10968 10968          re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10969 10969          re->ipRouteMask = ire->ire_mask;
10970 10970          re->ipRouteMetric5 = -1;
10971 10971          re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10972 10972          if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10973 10973                  re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10974 10974  
10975 10975          re->ipRouteInfo.re_frag_flag    = 0;
10976 10976          re->ipRouteInfo.re_rtt          = 0;
10977 10977          re->ipRouteInfo.re_src_addr     = 0;
10978 10978          re->ipRouteInfo.re_ref          = ire->ire_refcnt;
10979 10979          re->ipRouteInfo.re_obpkt        = ire->ire_ob_pkt_count;
10980 10980          re->ipRouteInfo.re_ibpkt        = ire->ire_ib_pkt_count;
10981 10981          re->ipRouteInfo.re_flags        = ire->ire_flags;
10982 10982  
10983 10983          /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10984 10984          if (ire->ire_type & IRE_INTERFACE) {
10985 10985                  ire_t *child;
10986 10986  
10987 10987                  rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10988 10988                  child = ire->ire_dep_children;
10989 10989                  while (child != NULL) {
10990 10990                          re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10991 10991                          re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10992 10992                          child = child->ire_dep_sib_next;
10993 10993                  }
10994 10994                  rw_exit(&ipst->ips_ire_dep_lock);
10995 10995          }
10996 10996  
10997 10997          if (ire->ire_flags & RTF_DYNAMIC) {
10998 10998                  re->ipRouteInfo.re_ire_type     = IRE_HOST_REDIRECT;
10999 10999          } else {
11000 11000                  re->ipRouteInfo.re_ire_type     = ire->ire_type;
11001 11001          }
11002 11002  
11003 11003          if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11004 11004              (char *)re, (int)sizeof (*re))) {
11005 11005                  ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
11006 11006                      (uint_t)sizeof (*re)));
11007 11007          }
11008 11008  
11009 11009          if (gc != NULL) {
11010 11010                  iaes.iae_routeidx = ird->ird_idx;
11011 11011                  iaes.iae_doi = gc->gc_db->gcdb_doi;
11012 11012                  iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11013 11013  
11014 11014                  if (!snmp_append_data2(ird->ird_attrs.lp_head,
11015 11015                      &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11016 11016                          ip1dbg(("ip_snmp_get2_v4: failed to allocate %u "
11017 11017                              "bytes\n", (uint_t)sizeof (iaes)));
11018 11018                  }
11019 11019          }
11020 11020  
11021 11021          /* bump route index for next pass */
11022 11022          ird->ird_idx++;
11023 11023  
11024 11024          kmem_free(re, sizeof (*re));
11025 11025          if (gcgrp != NULL)
11026 11026                  rw_exit(&gcgrp->gcgrp_rwlock);
11027 11027  }
11028 11028  
11029 11029  /*
11030 11030   * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
11031 11031   */
11032 11032  static void
11033 11033  ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
11034 11034  {
11035 11035          ill_t                           *ill;
11036 11036          mib2_ipv6RouteEntry_t           *re;
11037 11037          mib2_ipAttributeEntry_t         iaes;
11038 11038          tsol_ire_gw_secattr_t           *attrp;
11039 11039          tsol_gc_t                       *gc = NULL;
11040 11040          tsol_gcgrp_t                    *gcgrp = NULL;
11041 11041          ip_stack_t                      *ipst = ire->ire_ipst;
11042 11042  
11043 11043          ASSERT(ire->ire_ipversion == IPV6_VERSION);
11044 11044  
11045 11045          if (!(ird->ird_flags & IRD_REPORT_ALL)) {
11046 11046                  if (ire->ire_testhidden)
11047 11047                          return;
11048 11048                  if (ire->ire_type & IRE_IF_CLONE)
11049 11049                          return;
11050 11050          }
11051 11051  
11052 11052          if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
11053 11053                  return;
11054 11054  
11055 11055          if ((attrp = ire->ire_gw_secattr) != NULL) {
11056 11056                  mutex_enter(&attrp->igsa_lock);
11057 11057                  if ((gc = attrp->igsa_gc) != NULL) {
11058 11058                          gcgrp = gc->gc_grp;
11059 11059                          ASSERT(gcgrp != NULL);
11060 11060                          rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
11061 11061                  }
11062 11062                  mutex_exit(&attrp->igsa_lock);
11063 11063          }
11064 11064          /*
11065 11065           * Return all IRE types for route table... let caller pick and choose
11066 11066           */
11067 11067          re->ipv6RouteDest = ire->ire_addr_v6;
11068 11068          re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
11069 11069          re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
11070 11070          re->ipv6RouteIfIndex.o_length = 0;
11071 11071          ill = ire->ire_ill;
11072 11072          if (ill != NULL) {
11073 11073                  ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
11074 11074                  re->ipv6RouteIfIndex.o_length =
11075 11075                      mi_strlen(re->ipv6RouteIfIndex.o_bytes);
11076 11076          }
11077 11077  
11078 11078          ASSERT(!(ire->ire_type & IRE_BROADCAST));
11079 11079  
11080 11080          mutex_enter(&ire->ire_lock);
11081 11081          re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
11082 11082          mutex_exit(&ire->ire_lock);
11083 11083  
11084 11084          /* remote(4), local(3), or discard(2) */
11085 11085          if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11086 11086                  re->ipv6RouteType = 2;
11087 11087          else if (ire->ire_type & IRE_ONLINK)
11088 11088                  re->ipv6RouteType = 3;
11089 11089          else
11090 11090                  re->ipv6RouteType = 4;
11091 11091  
11092 11092          re->ipv6RouteProtocol   = -1;
11093 11093          re->ipv6RoutePolicy     = 0;
11094 11094          re->ipv6RouteAge        = gethrestime_sec() - ire->ire_create_time;
11095 11095          re->ipv6RouteNextHopRDI = 0;
11096 11096          re->ipv6RouteWeight     = 0;
11097 11097          re->ipv6RouteMetric     = 0;
11098 11098          re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11099 11099          if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
11100 11100                  re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11101 11101  
11102 11102          re->ipv6RouteInfo.re_frag_flag  = 0;
11103 11103          re->ipv6RouteInfo.re_rtt        = 0;
11104 11104          re->ipv6RouteInfo.re_src_addr   = ipv6_all_zeros;
11105 11105          re->ipv6RouteInfo.re_obpkt      = ire->ire_ob_pkt_count;
11106 11106          re->ipv6RouteInfo.re_ibpkt      = ire->ire_ib_pkt_count;
11107 11107          re->ipv6RouteInfo.re_ref        = ire->ire_refcnt;
11108 11108          re->ipv6RouteInfo.re_flags      = ire->ire_flags;
11109 11109  
11110 11110          /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11111 11111          if (ire->ire_type & IRE_INTERFACE) {
11112 11112                  ire_t *child;
11113 11113  
11114 11114                  rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11115 11115                  child = ire->ire_dep_children;
11116 11116                  while (child != NULL) {
11117 11117                          re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
11118 11118                          re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11119 11119                          child = child->ire_dep_sib_next;
11120 11120                  }
11121 11121                  rw_exit(&ipst->ips_ire_dep_lock);
11122 11122          }
11123 11123          if (ire->ire_flags & RTF_DYNAMIC) {
11124 11124                  re->ipv6RouteInfo.re_ire_type   = IRE_HOST_REDIRECT;
11125 11125          } else {
11126 11126                  re->ipv6RouteInfo.re_ire_type   = ire->ire_type;
11127 11127          }
11128 11128  
11129 11129          if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11130 11130              (char *)re, (int)sizeof (*re))) {
11131 11131                  ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
11132 11132                      (uint_t)sizeof (*re)));
11133 11133          }
11134 11134  
11135 11135          if (gc != NULL) {
11136 11136                  iaes.iae_routeidx = ird->ird_idx;
11137 11137                  iaes.iae_doi = gc->gc_db->gcdb_doi;
11138 11138                  iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11139 11139  
11140 11140                  if (!snmp_append_data2(ird->ird_attrs.lp_head,
11141 11141                      &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11142 11142                          ip1dbg(("ip_snmp_get2_v6: failed to allocate %u "
11143 11143                              "bytes\n", (uint_t)sizeof (iaes)));
11144 11144                  }
11145 11145          }
11146 11146  
11147 11147          /* bump route index for next pass */
11148 11148          ird->ird_idx++;
11149 11149  
11150 11150          kmem_free(re, sizeof (*re));
11151 11151          if (gcgrp != NULL)
11152 11152                  rw_exit(&gcgrp->gcgrp_rwlock);
11153 11153  }
11154 11154  
11155 11155  /*
11156 11156   * ncec_walk routine to create ipv6NetToMediaEntryTable
11157 11157   */
11158 11158  static void
11159 11159  ip_snmp_get2_v6_media(ncec_t *ncec, void *ptr)
11160 11160  {
11161 11161          iproutedata_t *ird              = ptr;
11162 11162          ill_t                           *ill;
11163 11163          mib2_ipv6NetToMediaEntry_t      ntme;
11164 11164  
11165 11165          ill = ncec->ncec_ill;
11166 11166          /* skip arpce entries, and loopback ncec entries */
11167 11167          if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
11168 11168                  return;
11169 11169          /*
11170 11170           * Neighbor cache entry attached to IRE with on-link
11171 11171           * destination.
11172 11172           * We report all IPMP groups on ncec_ill which is normally the upper.
11173 11173           */
11174 11174          ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
11175 11175          ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
11176 11176          ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
11177 11177          if (ncec->ncec_lladdr != NULL) {
11178 11178                  bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
11179 11179                      ntme.ipv6NetToMediaPhysAddress.o_length);
11180 11180          }
11181 11181          /*
11182 11182           * Note: Returns ND_* states. Should be:
11183 11183           * reachable(1), stale(2), delay(3), probe(4),
11184 11184           * invalid(5), unknown(6)
11185 11185           */
11186 11186          ntme.ipv6NetToMediaState = ncec->ncec_state;
11187 11187          ntme.ipv6NetToMediaLastUpdated = 0;
11188 11188  
11189 11189          /* other(1), dynamic(2), static(3), local(4) */
11190 11190          if (NCE_MYADDR(ncec)) {
11191 11191                  ntme.ipv6NetToMediaType = 4;
11192 11192          } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
11193 11193                  ntme.ipv6NetToMediaType = 1; /* proxy */
11194 11194          } else if (ncec->ncec_flags & NCE_F_STATIC) {
11195 11195                  ntme.ipv6NetToMediaType = 3;
11196 11196          } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
11197 11197                  ntme.ipv6NetToMediaType = 1;
11198 11198          } else {
11199 11199                  ntme.ipv6NetToMediaType = 2;
11200 11200          }
11201 11201  
11202 11202          if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11203 11203              &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11204 11204                  ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
11205 11205                      (uint_t)sizeof (ntme)));
11206 11206          }
11207 11207  }
11208 11208  
11209 11209  int
11210 11210  nce2ace(ncec_t *ncec)
11211 11211  {
11212 11212          int flags = 0;
11213 11213  
11214 11214          if (NCE_ISREACHABLE(ncec))
11215 11215                  flags |= ACE_F_RESOLVED;
11216 11216          if (ncec->ncec_flags & NCE_F_AUTHORITY)
11217 11217                  flags |= ACE_F_AUTHORITY;
11218 11218          if (ncec->ncec_flags & NCE_F_PUBLISH)
11219 11219                  flags |= ACE_F_PUBLISH;
11220 11220          if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
11221 11221                  flags |= ACE_F_PERMANENT;
11222 11222          if (NCE_MYADDR(ncec))
11223 11223                  flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
11224 11224          if (ncec->ncec_flags & NCE_F_UNVERIFIED)
11225 11225                  flags |= ACE_F_UNVERIFIED;
11226 11226          if (ncec->ncec_flags & NCE_F_AUTHORITY)
11227 11227                  flags |= ACE_F_AUTHORITY;
11228 11228          if (ncec->ncec_flags & NCE_F_DELAYED)
11229 11229                  flags |= ACE_F_DELAYED;
11230 11230          return (flags);
11231 11231  }
11232 11232  
11233 11233  /*
11234 11234   * ncec_walk routine to create ipNetToMediaEntryTable
11235 11235   */
11236 11236  static void
11237 11237  ip_snmp_get2_v4_media(ncec_t *ncec, void *ptr)
11238 11238  {
11239 11239          iproutedata_t *ird              = ptr;
11240 11240          ill_t                           *ill;
11241 11241          mib2_ipNetToMediaEntry_t        ntme;
11242 11242          const char                      *name = "unknown";
11243 11243          ipaddr_t                        ncec_addr;
11244 11244  
11245 11245          ill = ncec->ncec_ill;
11246 11246          if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
11247 11247              ill->ill_net_type == IRE_LOOPBACK)
11248 11248                  return;
11249 11249  
11250 11250          /* We report all IPMP groups on ncec_ill which is normally the upper. */
11251 11251          name = ill->ill_name;
11252 11252          /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
11253 11253          if (NCE_MYADDR(ncec)) {
11254 11254                  ntme.ipNetToMediaType = 4;
11255 11255          } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
11256 11256                  ntme.ipNetToMediaType = 1;
11257 11257          } else {
11258 11258                  ntme.ipNetToMediaType = 3;
11259 11259          }
11260 11260          ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
11261 11261          bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
11262 11262              ntme.ipNetToMediaIfIndex.o_length);
11263 11263  
11264 11264          IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
11265 11265          bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
11266 11266  
11267 11267          ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
11268 11268          ncec_addr = INADDR_BROADCAST;
11269 11269          bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
11270 11270              sizeof (ncec_addr));
11271 11271          /*
11272 11272           * map all the flags to the ACE counterpart.
11273 11273           */
11274 11274          ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
11275 11275  
11276 11276          ntme.ipNetToMediaPhysAddress.o_length =
11277 11277              MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
11278 11278  
11279 11279          if (!NCE_ISREACHABLE(ncec))
11280 11280                  ntme.ipNetToMediaPhysAddress.o_length = 0;
11281 11281          else {
11282 11282                  if (ncec->ncec_lladdr != NULL) {
11283 11283                          bcopy(ncec->ncec_lladdr,
11284 11284                              ntme.ipNetToMediaPhysAddress.o_bytes,
11285 11285                              ntme.ipNetToMediaPhysAddress.o_length);
11286 11286                  }
11287 11287          }
11288 11288  
11289 11289          if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11290 11290              &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11291 11291                  ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
11292 11292                      (uint_t)sizeof (ntme)));
11293 11293          }
11294 11294  }
11295 11295  
11296 11296  /*
11297 11297   * return (0) if invalid set request, 1 otherwise, including non-tcp requests
11298 11298   */
11299 11299  /* ARGSUSED */
11300 11300  int
11301 11301  ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
11302 11302  {
11303 11303          switch (level) {
11304 11304          case MIB2_IP:
11305 11305          case MIB2_ICMP:
11306 11306                  switch (name) {
11307 11307                  default:
11308 11308                          break;
11309 11309                  }
11310 11310                  return (1);
11311 11311          default:
11312 11312                  return (1);
11313 11313          }
11314 11314  }
11315 11315  
11316 11316  /*
11317 11317   * When there exists both a 64- and 32-bit counter of a particular type
11318 11318   * (i.e., InReceives), only the 64-bit counters are added.
11319 11319   */
11320 11320  void
11321 11321  ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
11322 11322  {
11323 11323          UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
11324 11324          UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
11325 11325          UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
11326 11326          UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
11327 11327          UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
11328 11328          UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
11329 11329          UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
11330 11330          UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
11331 11331          UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
11332 11332          UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
11333 11333          UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
11334 11334          UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
11335 11335          UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
11336 11336          UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
11337 11337          UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
11338 11338          UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
11339 11339          UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
11340 11340          UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
11341 11341          UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
11342 11342          UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
11343 11343          UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
11344 11344          UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
11345 11345              o2->ipIfStatsInWrongIPVersion);
11346 11346          UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
11347 11347              o2->ipIfStatsInWrongIPVersion);
11348 11348          UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
11349 11349              o2->ipIfStatsOutSwitchIPVersion);
11350 11350          UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
11351 11351          UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
11352 11352          UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
11353 11353              o2->ipIfStatsHCInForwDatagrams);
11354 11354          UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
11355 11355          UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
11356 11356          UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
11357 11357              o2->ipIfStatsHCOutForwDatagrams);
11358 11358          UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
11359 11359          UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
11360 11360          UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
11361 11361          UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
11362 11362          UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
11363 11363          UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
11364 11364          UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11365 11365              o2->ipIfStatsHCOutMcastOctets);
11366 11366          UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11367 11367          UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11368 11368          UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11369 11369          UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11370 11370          UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11371 11371          UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11372 11372          UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11373 11373  }
11374 11374  
11375 11375  void
11376 11376  ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11377 11377  {
11378 11378          UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11379 11379          UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11380 11380          UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11381 11381          UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11382 11382          UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11383 11383          UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11384 11384          UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11385 11385          UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11386 11386          UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11387 11387          UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11388 11388              o2->ipv6IfIcmpInRouterSolicits);
11389 11389          UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11390 11390              o2->ipv6IfIcmpInRouterAdvertisements);
11391 11391          UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11392 11392              o2->ipv6IfIcmpInNeighborSolicits);
11393 11393          UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11394 11394              o2->ipv6IfIcmpInNeighborAdvertisements);
11395 11395          UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11396 11396          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11397 11397              o2->ipv6IfIcmpInGroupMembQueries);
11398 11398          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11399 11399              o2->ipv6IfIcmpInGroupMembResponses);
11400 11400          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11401 11401              o2->ipv6IfIcmpInGroupMembReductions);
11402 11402          UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11403 11403          UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11404 11404          UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11405 11405              o2->ipv6IfIcmpOutDestUnreachs);
11406 11406          UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11407 11407              o2->ipv6IfIcmpOutAdminProhibs);
11408 11408          UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11409 11409          UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11410 11410              o2->ipv6IfIcmpOutParmProblems);
11411 11411          UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11412 11412          UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11413 11413          UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11414 11414          UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11415 11415              o2->ipv6IfIcmpOutRouterSolicits);
11416 11416          UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11417 11417              o2->ipv6IfIcmpOutRouterAdvertisements);
11418 11418          UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11419 11419              o2->ipv6IfIcmpOutNeighborSolicits);
11420 11420          UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11421 11421              o2->ipv6IfIcmpOutNeighborAdvertisements);
11422 11422          UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11423 11423          UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11424 11424              o2->ipv6IfIcmpOutGroupMembQueries);
11425 11425          UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11426 11426              o2->ipv6IfIcmpOutGroupMembResponses);
11427 11427          UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11428 11428              o2->ipv6IfIcmpOutGroupMembReductions);
11429 11429          UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11430 11430          UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11431 11431          UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11432 11432              o2->ipv6IfIcmpInBadNeighborAdvertisements);
11433 11433          UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11434 11434              o2->ipv6IfIcmpInBadNeighborSolicitations);
11435 11435          UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11436 11436          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11437 11437              o2->ipv6IfIcmpInGroupMembTotal);
11438 11438          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11439 11439              o2->ipv6IfIcmpInGroupMembBadQueries);
11440 11440          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11441 11441              o2->ipv6IfIcmpInGroupMembBadReports);
11442 11442          UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11443 11443              o2->ipv6IfIcmpInGroupMembOurReports);
11444 11444  }
11445 11445  
11446 11446  /*
11447 11447   * Called before the options are updated to check if this packet will
11448 11448   * be source routed from here.
11449 11449   * This routine assumes that the options are well formed i.e. that they
11450 11450   * have already been checked.
11451 11451   */
11452 11452  boolean_t
11453 11453  ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11454 11454  {
11455 11455          ipoptp_t        opts;
11456 11456          uchar_t         *opt;
11457 11457          uint8_t         optval;
11458 11458          uint8_t         optlen;
11459 11459          ipaddr_t        dst;
11460 11460  
11461 11461          if (IS_SIMPLE_IPH(ipha)) {
11462 11462                  ip2dbg(("not source routed\n"));
11463 11463                  return (B_FALSE);
11464 11464          }
11465 11465          dst = ipha->ipha_dst;
11466 11466          for (optval = ipoptp_first(&opts, ipha);
11467 11467              optval != IPOPT_EOL;
11468 11468              optval = ipoptp_next(&opts)) {
11469 11469                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11470 11470                  opt = opts.ipoptp_cur;
11471 11471                  optlen = opts.ipoptp_len;
11472 11472                  ip2dbg(("ip_source_routed: opt %d, len %d\n",
11473 11473                      optval, optlen));
11474 11474                  switch (optval) {
11475 11475                          uint32_t off;
11476 11476                  case IPOPT_SSRR:
11477 11477                  case IPOPT_LSRR:
11478 11478                          /*
11479 11479                           * If dst is one of our addresses and there are some
11480 11480                           * entries left in the source route return (true).
11481 11481                           */
11482 11482                          if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11483 11483                                  ip2dbg(("ip_source_routed: not next"
11484 11484                                      " source route 0x%x\n",
11485 11485                                      ntohl(dst)));
11486 11486                                  return (B_FALSE);
11487 11487                          }
11488 11488                          off = opt[IPOPT_OFFSET];
11489 11489                          off--;
11490 11490                          if (optlen < IP_ADDR_LEN ||
11491 11491                              off > optlen - IP_ADDR_LEN) {
11492 11492                                  /* End of source route */
11493 11493                                  ip1dbg(("ip_source_routed: end of SR\n"));
11494 11494                                  return (B_FALSE);
11495 11495                          }
11496 11496                          return (B_TRUE);
11497 11497                  }
11498 11498          }
11499 11499          ip2dbg(("not source routed\n"));
11500 11500          return (B_FALSE);
11501 11501  }
11502 11502  
11503 11503  /*
11504 11504   * ip_unbind is called by the transports to remove a conn from
11505 11505   * the fanout table.
11506 11506   */
11507 11507  void
11508 11508  ip_unbind(conn_t *connp)
11509 11509  {
11510 11510  
11511 11511          ASSERT(!MUTEX_HELD(&connp->conn_lock));
11512 11512  
11513 11513          if (is_system_labeled() && connp->conn_anon_port) {
11514 11514                  (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
11515 11515                      connp->conn_mlp_type, connp->conn_proto,
11516 11516                      ntohs(connp->conn_lport), B_FALSE);
11517 11517                  connp->conn_anon_port = 0;
11518 11518          }
11519 11519          connp->conn_mlp_type = mlptSingle;
11520 11520  
11521 11521          ipcl_hash_remove(connp);
11522 11522  }
11523 11523  
11524 11524  /*
11525 11525   * Used for deciding the MSS size for the upper layer. Thus
11526 11526   * we need to check the outbound policy values in the conn.
11527 11527   */
11528 11528  int
11529 11529  conn_ipsec_length(conn_t *connp)
11530 11530  {
11531 11531          ipsec_latch_t *ipl;
11532 11532  
11533 11533          ipl = connp->conn_latch;
11534 11534          if (ipl == NULL)
11535 11535                  return (0);
11536 11536  
11537 11537          if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11538 11538                  return (0);
11539 11539  
11540 11540          return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11541 11541  }
11542 11542  
11543 11543  /*
11544 11544   * Returns an estimate of the IPsec headers size. This is used if
11545 11545   * we don't want to call into IPsec to get the exact size.
11546 11546   */
11547 11547  int
11548 11548  ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11549 11549  {
11550 11550          ipsec_action_t *a;
11551 11551  
11552 11552          if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11553 11553                  return (0);
11554 11554  
11555 11555          a = ixa->ixa_ipsec_action;
11556 11556          if (a == NULL) {
11557 11557                  ASSERT(ixa->ixa_ipsec_policy != NULL);
11558 11558                  a = ixa->ixa_ipsec_policy->ipsp_act;
11559 11559          }
11560 11560          ASSERT(a != NULL);
11561 11561  
11562 11562          return (a->ipa_ovhd);
11563 11563  }
11564 11564  
11565 11565  /*
11566 11566   * If there are any source route options, return the true final
11567 11567   * destination. Otherwise, return the destination.
11568 11568   */
11569 11569  ipaddr_t
11570 11570  ip_get_dst(ipha_t *ipha)
11571 11571  {
11572 11572          ipoptp_t        opts;
11573 11573          uchar_t         *opt;
11574 11574          uint8_t         optval;
11575 11575          uint8_t         optlen;
11576 11576          ipaddr_t        dst;
11577 11577          uint32_t off;
11578 11578  
11579 11579          dst = ipha->ipha_dst;
11580 11580  
11581 11581          if (IS_SIMPLE_IPH(ipha))
11582 11582                  return (dst);
11583 11583  
11584 11584          for (optval = ipoptp_first(&opts, ipha);
11585 11585              optval != IPOPT_EOL;
11586 11586              optval = ipoptp_next(&opts)) {
11587 11587                  opt = opts.ipoptp_cur;
11588 11588                  optlen = opts.ipoptp_len;
11589 11589                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11590 11590                  switch (optval) {
11591 11591                  case IPOPT_SSRR:
11592 11592                  case IPOPT_LSRR:
11593 11593                          off = opt[IPOPT_OFFSET];
11594 11594                          /*
11595 11595                           * If one of the conditions is true, it means
11596 11596                           * end of options and dst already has the right
11597 11597                           * value.
11598 11598                           */
11599 11599                          if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11600 11600                                  off = optlen - IP_ADDR_LEN;
11601 11601                                  bcopy(&opt[off], &dst, IP_ADDR_LEN);
11602 11602                          }
11603 11603                          return (dst);
11604 11604                  default:
11605 11605                          break;
11606 11606                  }
11607 11607          }
11608 11608  
11609 11609          return (dst);
11610 11610  }
11611 11611  
11612 11612  /*
11613 11613   * Outbound IP fragmentation routine.
11614 11614   * Assumes the caller has checked whether or not fragmentation should
11615 11615   * be allowed. Here we copy the DF bit from the header to all the generated
11616 11616   * fragments.
11617 11617   */
11618 11618  int
11619 11619  ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11620 11620      uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11621 11621      zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11622 11622  {
11623 11623          int             i1;
11624 11624          int             hdr_len;
11625 11625          mblk_t          *hdr_mp;
11626 11626          ipha_t          *ipha;
11627 11627          int             ip_data_end;
11628 11628          int             len;
11629 11629          mblk_t          *mp = mp_orig;
11630 11630          int             offset;
11631 11631          ill_t           *ill = nce->nce_ill;
11632 11632          ip_stack_t      *ipst = ill->ill_ipst;
11633 11633          mblk_t          *carve_mp;
11634 11634          uint32_t        frag_flag;
11635 11635          uint_t          priority = mp->b_band;
11636 11636          int             error = 0;
11637 11637  
11638 11638          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11639 11639  
11640 11640          if (pkt_len != msgdsize(mp)) {
11641 11641                  ip0dbg(("Packet length mismatch: %d, %ld\n",
11642 11642                      pkt_len, msgdsize(mp)));
11643 11643                  freemsg(mp);
11644 11644                  return (EINVAL);
11645 11645          }
11646 11646  
11647 11647          if (max_frag == 0) {
11648 11648                  ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11649 11649                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11650 11650                  ip_drop_output("FragFails: zero max_frag", mp, ill);
11651 11651                  freemsg(mp);
11652 11652                  return (EINVAL);
11653 11653          }
11654 11654  
11655 11655          ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11656 11656          ipha = (ipha_t *)mp->b_rptr;
11657 11657          ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11658 11658          frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11659 11659  
11660 11660          /*
11661 11661           * Establish the starting offset.  May not be zero if we are fragging
11662 11662           * a fragment that is being forwarded.
11663 11663           */
11664 11664          offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11665 11665  
11666 11666          /* TODO why is this test needed? */
11667 11667          if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11668 11668                  /* TODO: notify ulp somehow */
11669 11669                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11670 11670                  ip_drop_output("FragFails: bad starting offset", mp, ill);
11671 11671                  freemsg(mp);
11672 11672                  return (EINVAL);
11673 11673          }
11674 11674  
11675 11675          hdr_len = IPH_HDR_LENGTH(ipha);
11676 11676          ipha->ipha_hdr_checksum = 0;
11677 11677  
11678 11678          /*
11679 11679           * Establish the number of bytes maximum per frag, after putting
11680 11680           * in the header.
11681 11681           */
11682 11682          len = (max_frag - hdr_len) & ~7;
11683 11683  
11684 11684          /* Get a copy of the header for the trailing frags */
11685 11685          hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11686 11686              mp);
11687 11687          if (hdr_mp == NULL) {
11688 11688                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11689 11689                  ip_drop_output("FragFails: no hdr_mp", mp, ill);
11690 11690                  freemsg(mp);
11691 11691                  return (ENOBUFS);
11692 11692          }
11693 11693  
11694 11694          /* Store the starting offset, with the MoreFrags flag. */
11695 11695          i1 = offset | IPH_MF | frag_flag;
11696 11696          ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11697 11697  
11698 11698          /* Establish the ending byte offset, based on the starting offset. */
11699 11699          offset <<= 3;
11700 11700          ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11701 11701  
11702 11702          /* Store the length of the first fragment in the IP header. */
11703 11703          i1 = len + hdr_len;
11704 11704          ASSERT(i1 <= IP_MAXPACKET);
11705 11705          ipha->ipha_length = htons((uint16_t)i1);
11706 11706  
11707 11707          /*
11708 11708           * Compute the IP header checksum for the first frag.  We have to
11709 11709           * watch out that we stop at the end of the header.
11710 11710           */
11711 11711          ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11712 11712  
11713 11713          /*
11714 11714           * Now carve off the first frag.  Note that this will include the
11715 11715           * original IP header.
11716 11716           */
11717 11717          if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11718 11718                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11719 11719                  ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11720 11720                  freeb(hdr_mp);
11721 11721                  freemsg(mp_orig);
11722 11722                  return (ENOBUFS);
11723 11723          }
11724 11724  
11725 11725          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11726 11726  
11727 11727          error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11728 11728              ixa_cookie);
11729 11729          if (error != 0 && error != EWOULDBLOCK) {
11730 11730                  /* No point in sending the other fragments */
11731 11731                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11732 11732                  ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11733 11733                  freeb(hdr_mp);
11734 11734                  freemsg(mp_orig);
11735 11735                  return (error);
11736 11736          }
11737 11737  
11738 11738          /* No need to redo state machine in loop */
11739 11739          ixaflags &= ~IXAF_REACH_CONF;
11740 11740  
11741 11741          /* Advance the offset to the second frag starting point. */
11742 11742          offset += len;
11743 11743          /*
11744 11744           * Update hdr_len from the copied header - there might be less options
11745 11745           * in the later fragments.
11746 11746           */
11747 11747          hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11748 11748          /* Loop until done. */
11749 11749          for (;;) {
11750 11750                  uint16_t        offset_and_flags;
11751 11751                  uint16_t        ip_len;
11752 11752  
11753 11753                  if (ip_data_end - offset > len) {
11754 11754                          /*
11755 11755                           * Carve off the appropriate amount from the original
11756 11756                           * datagram.
11757 11757                           */
11758 11758                          if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11759 11759                                  mp = NULL;
11760 11760                                  break;
11761 11761                          }
11762 11762                          /*
11763 11763                           * More frags after this one.  Get another copy
11764 11764                           * of the header.
11765 11765                           */
11766 11766                          if (carve_mp->b_datap->db_ref == 1 &&
11767 11767                              hdr_mp->b_wptr - hdr_mp->b_rptr <
11768 11768                              carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11769 11769                                  /* Inline IP header */
11770 11770                                  carve_mp->b_rptr -= hdr_mp->b_wptr -
11771 11771                                      hdr_mp->b_rptr;
11772 11772                                  bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11773 11773                                      hdr_mp->b_wptr - hdr_mp->b_rptr);
11774 11774                                  mp = carve_mp;
11775 11775                          } else {
11776 11776                                  if (!(mp = copyb(hdr_mp))) {
11777 11777                                          freemsg(carve_mp);
11778 11778                                          break;
11779 11779                                  }
11780 11780                                  /* Get priority marking, if any. */
11781 11781                                  mp->b_band = priority;
11782 11782                                  mp->b_cont = carve_mp;
11783 11783                          }
11784 11784                          ipha = (ipha_t *)mp->b_rptr;
11785 11785                          offset_and_flags = IPH_MF;
11786 11786                  } else {
11787 11787                          /*
11788 11788                           * Last frag.  Consume the header. Set len to
11789 11789                           * the length of this last piece.
11790 11790                           */
11791 11791                          len = ip_data_end - offset;
11792 11792  
11793 11793                          /*
11794 11794                           * Carve off the appropriate amount from the original
11795 11795                           * datagram.
11796 11796                           */
11797 11797                          if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11798 11798                                  mp = NULL;
11799 11799                                  break;
11800 11800                          }
11801 11801                          if (carve_mp->b_datap->db_ref == 1 &&
11802 11802                              hdr_mp->b_wptr - hdr_mp->b_rptr <
11803 11803                              carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11804 11804                                  /* Inline IP header */
11805 11805                                  carve_mp->b_rptr -= hdr_mp->b_wptr -
11806 11806                                      hdr_mp->b_rptr;
11807 11807                                  bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11808 11808                                      hdr_mp->b_wptr - hdr_mp->b_rptr);
11809 11809                                  mp = carve_mp;
11810 11810                                  freeb(hdr_mp);
11811 11811                                  hdr_mp = mp;
11812 11812                          } else {
11813 11813                                  mp = hdr_mp;
11814 11814                                  /* Get priority marking, if any. */
11815 11815                                  mp->b_band = priority;
11816 11816                                  mp->b_cont = carve_mp;
11817 11817                          }
11818 11818                          ipha = (ipha_t *)mp->b_rptr;
11819 11819                          /* A frag of a frag might have IPH_MF non-zero */
11820 11820                          offset_and_flags =
11821 11821                              ntohs(ipha->ipha_fragment_offset_and_flags) &
11822 11822                              IPH_MF;
11823 11823                  }
11824 11824                  offset_and_flags |= (uint16_t)(offset >> 3);
11825 11825                  offset_and_flags |= (uint16_t)frag_flag;
11826 11826                  /* Store the offset and flags in the IP header. */
11827 11827                  ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11828 11828  
11829 11829                  /* Store the length in the IP header. */
11830 11830                  ip_len = (uint16_t)(len + hdr_len);
11831 11831                  ipha->ipha_length = htons(ip_len);
11832 11832  
11833 11833                  /*
11834 11834                   * Set the IP header checksum.  Note that mp is just
11835 11835                   * the header, so this is easy to pass to ip_csum.
11836 11836                   */
11837 11837                  ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11838 11838  
11839 11839                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11840 11840  
11841 11841                  error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11842 11842                      nolzid, ixa_cookie);
11843 11843                  /* All done if we just consumed the hdr_mp. */
11844 11844                  if (mp == hdr_mp) {
11845 11845                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11846 11846                          return (error);
11847 11847                  }
11848 11848                  if (error != 0 && error != EWOULDBLOCK) {
11849 11849                          DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11850 11850                              mblk_t *, hdr_mp);
11851 11851                          /* No point in sending the other fragments */
11852 11852                          break;
11853 11853                  }
11854 11854  
11855 11855                  /* Otherwise, advance and loop. */
11856 11856                  offset += len;
11857 11857          }
11858 11858          /* Clean up following allocation failure. */
11859 11859          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11860 11860          ip_drop_output("FragFails: loop ended", NULL, ill);
11861 11861          if (mp != hdr_mp)
11862 11862                  freeb(hdr_mp);
11863 11863          if (mp != mp_orig)
11864 11864                  freemsg(mp_orig);
11865 11865          return (error);
11866 11866  }
11867 11867  
11868 11868  /*
11869 11869   * Copy the header plus those options which have the copy bit set
11870 11870   */
11871 11871  static mblk_t *
11872 11872  ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11873 11873      mblk_t *src)
11874 11874  {
11875 11875          mblk_t  *mp;
11876 11876          uchar_t *up;
11877 11877  
11878 11878          /*
11879 11879           * Quick check if we need to look for options without the copy bit
11880 11880           * set
11881 11881           */
11882 11882          mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11883 11883          if (!mp)
11884 11884                  return (mp);
11885 11885          mp->b_rptr += ipst->ips_ip_wroff_extra;
11886 11886          if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11887 11887                  bcopy(rptr, mp->b_rptr, hdr_len);
11888 11888                  mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11889 11889                  return (mp);
11890 11890          }
11891 11891          up  = mp->b_rptr;
11892 11892          bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11893 11893          up += IP_SIMPLE_HDR_LENGTH;
11894 11894          rptr += IP_SIMPLE_HDR_LENGTH;
11895 11895          hdr_len -= IP_SIMPLE_HDR_LENGTH;
11896 11896          while (hdr_len > 0) {
11897 11897                  uint32_t optval;
11898 11898                  uint32_t optlen;
11899 11899  
11900 11900                  optval = *rptr;
11901 11901                  if (optval == IPOPT_EOL)
11902 11902                          break;
11903 11903                  if (optval == IPOPT_NOP)
11904 11904                          optlen = 1;
11905 11905                  else
11906 11906                          optlen = rptr[1];
11907 11907                  if (optval & IPOPT_COPY) {
11908 11908                          bcopy(rptr, up, optlen);
11909 11909                          up += optlen;
11910 11910                  }
11911 11911                  rptr += optlen;
11912 11912                  hdr_len -= optlen;
11913 11913          }
11914 11914          /*
11915 11915           * Make sure that we drop an even number of words by filling
11916 11916           * with EOL to the next word boundary.
11917 11917           */
11918 11918          for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11919 11919              hdr_len & 0x3; hdr_len++)
11920 11920                  *up++ = IPOPT_EOL;
11921 11921          mp->b_wptr = up;
11922 11922          /* Update header length */
11923 11923          mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11924 11924          return (mp);
11925 11925  }
11926 11926  
11927 11927  /*
11928 11928   * Update any source route, record route, or timestamp options when
11929 11929   * sending a packet back to ourselves.
11930 11930   * Check that we are at end of strict source route.
11931 11931   * The options have been sanity checked by ip_output_options().
11932 11932   */
11933 11933  void
11934 11934  ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11935 11935  {
11936 11936          ipoptp_t        opts;
11937 11937          uchar_t         *opt;
11938 11938          uint8_t         optval;
11939 11939          uint8_t         optlen;
11940 11940          ipaddr_t        dst;
11941 11941          uint32_t        ts;
11942 11942          timestruc_t     now;
11943 11943  
11944 11944          for (optval = ipoptp_first(&opts, ipha);
11945 11945              optval != IPOPT_EOL;
11946 11946              optval = ipoptp_next(&opts)) {
11947 11947                  opt = opts.ipoptp_cur;
11948 11948                  optlen = opts.ipoptp_len;
11949 11949                  ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11950 11950                  switch (optval) {
11951 11951                          uint32_t off;
11952 11952                  case IPOPT_SSRR:
11953 11953                  case IPOPT_LSRR:
11954 11954                          off = opt[IPOPT_OFFSET];
11955 11955                          off--;
11956 11956                          if (optlen < IP_ADDR_LEN ||
11957 11957                              off > optlen - IP_ADDR_LEN) {
11958 11958                                  /* End of source route */
11959 11959                                  break;
11960 11960                          }
11961 11961                          /*
11962 11962                           * This will only happen if two consecutive entries
11963 11963                           * in the source route contains our address or if
11964 11964                           * it is a packet with a loose source route which
11965 11965                           * reaches us before consuming the whole source route
11966 11966                           */
11967 11967  
11968 11968                          if (optval == IPOPT_SSRR) {
11969 11969                                  return;
11970 11970                          }
11971 11971                          /*
11972 11972                           * Hack: instead of dropping the packet truncate the
11973 11973                           * source route to what has been used by filling the
11974 11974                           * rest with IPOPT_NOP.
11975 11975                           */
11976 11976                          opt[IPOPT_OLEN] = (uint8_t)off;
11977 11977                          while (off < optlen) {
11978 11978                                  opt[off++] = IPOPT_NOP;
11979 11979                          }
11980 11980                          break;
11981 11981                  case IPOPT_RR:
11982 11982                          off = opt[IPOPT_OFFSET];
11983 11983                          off--;
11984 11984                          if (optlen < IP_ADDR_LEN ||
11985 11985                              off > optlen - IP_ADDR_LEN) {
11986 11986                                  /* No more room - ignore */
11987 11987                                  ip1dbg((
11988 11988                                      "ip_output_local_options: end of RR\n"));
11989 11989                                  break;
11990 11990                          }
11991 11991                          dst = htonl(INADDR_LOOPBACK);
11992 11992                          bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11993 11993                          opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11994 11994                          break;
11995 11995                  case IPOPT_TS:
11996 11996                          /* Insert timestamp if there is romm */
11997 11997                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11998 11998                          case IPOPT_TS_TSONLY:
11999 11999                                  off = IPOPT_TS_TIMELEN;
12000 12000                                  break;
12001 12001                          case IPOPT_TS_PRESPEC:
12002 12002                          case IPOPT_TS_PRESPEC_RFC791:
12003 12003                                  /* Verify that the address matched */
12004 12004                                  off = opt[IPOPT_OFFSET] - 1;
12005 12005                                  bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
12006 12006                                  if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
12007 12007                                          /* Not for us */
12008 12008                                          break;
12009 12009                                  }
12010 12010                                  /* FALLTHROUGH */
12011 12011                          case IPOPT_TS_TSANDADDR:
12012 12012                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
12013 12013                                  break;
12014 12014                          default:
12015 12015                                  /*
12016 12016                                   * ip_*put_options should have already
12017 12017                                   * dropped this packet.
12018 12018                                   */
12019 12019                                  cmn_err(CE_PANIC, "ip_output_local_options: "
12020 12020                                      "unknown IT - bug in ip_output_options?\n");
12021 12021                                  return; /* Keep "lint" happy */
12022 12022                          }
12023 12023                          if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
12024 12024                                  /* Increase overflow counter */
12025 12025                                  off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
12026 12026                                  opt[IPOPT_POS_OV_FLG] = (uint8_t)
12027 12027                                      (opt[IPOPT_POS_OV_FLG] & 0x0F) |
12028 12028                                      (off << 4);
12029 12029                                  break;
12030 12030                          }
12031 12031                          off = opt[IPOPT_OFFSET] - 1;
12032 12032                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12033 12033                          case IPOPT_TS_PRESPEC:
12034 12034                          case IPOPT_TS_PRESPEC_RFC791:
12035 12035                          case IPOPT_TS_TSANDADDR:
12036 12036                                  dst = htonl(INADDR_LOOPBACK);
12037 12037                                  bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12038 12038                                  opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12039 12039                                  /* FALLTHROUGH */
12040 12040                          case IPOPT_TS_TSONLY:
12041 12041                                  off = opt[IPOPT_OFFSET] - 1;
12042 12042                                  /* Compute # of milliseconds since midnight */
12043 12043                                  gethrestime(&now);
12044 12044                                  ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
12045 12045                                      NSEC2MSEC(now.tv_nsec);
12046 12046                                  bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
12047 12047                                  opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
12048 12048                                  break;
12049 12049                          }
12050 12050                          break;
12051 12051                  }
12052 12052          }
12053 12053  }
12054 12054  
12055 12055  /*
12056 12056   * Prepend an M_DATA fastpath header, and if none present prepend a
12057 12057   * DL_UNITDATA_REQ. Frees the mblk on failure.
12058 12058   *
12059 12059   * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
12060 12060   * If there is a change to them, the nce will be deleted (condemned) and
12061 12061   * a new nce_t will be created when packets are sent. Thus we need no locks
12062 12062   * to access those fields.
12063 12063   *
12064 12064   * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
12065 12065   * we place b_band in dl_priority.dl_max.
12066 12066   */
12067 12067  static mblk_t *
12068 12068  ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
12069 12069  {
12070 12070          uint_t  hlen;
12071 12071          mblk_t *mp1;
12072 12072          uint_t  priority;
12073 12073          uchar_t *rptr;
12074 12074  
12075 12075          rptr = mp->b_rptr;
12076 12076  
12077 12077          ASSERT(DB_TYPE(mp) == M_DATA);
12078 12078          priority = mp->b_band;
12079 12079  
12080 12080          ASSERT(nce != NULL);
12081 12081          if ((mp1 = nce->nce_fp_mp) != NULL) {
12082 12082                  hlen = MBLKL(mp1);
12083 12083                  /*
12084 12084                   * Check if we have enough room to prepend fastpath
12085 12085                   * header
12086 12086                   */
12087 12087                  if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
12088 12088                          rptr -= hlen;
12089 12089                          bcopy(mp1->b_rptr, rptr, hlen);
12090 12090                          /*
12091 12091                           * Set the b_rptr to the start of the link layer
12092 12092                           * header
12093 12093                           */
12094 12094                          mp->b_rptr = rptr;
12095 12095                          return (mp);
12096 12096                  }
12097 12097                  mp1 = copyb(mp1);
12098 12098                  if (mp1 == NULL) {
12099 12099                          ill_t *ill = nce->nce_ill;
12100 12100  
12101 12101                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12102 12102                          ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12103 12103                          freemsg(mp);
12104 12104                          return (NULL);
12105 12105                  }
12106 12106                  mp1->b_band = priority;
12107 12107                  mp1->b_cont = mp;
12108 12108                  DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
12109 12109                  DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
12110 12110                  DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
12111 12111                  DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
12112 12112                  DB_LSOMSS(mp1) = DB_LSOMSS(mp);
12113 12113                  DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
12114 12114                  /*
12115 12115                   * XXX disable ICK_VALID and compute checksum
12116 12116                   * here; can happen if nce_fp_mp changes and
12117 12117                   * it can't be copied now due to insufficient
12118 12118                   * space. (unlikely, fp mp can change, but it
12119 12119                   * does not increase in length)
12120 12120                   */
12121 12121                  return (mp1);
12122 12122          }
12123 12123          mp1 = copyb(nce->nce_dlur_mp);
12124 12124  
12125 12125          if (mp1 == NULL) {
12126 12126                  ill_t *ill = nce->nce_ill;
12127 12127  
12128 12128                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12129 12129                  ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12130 12130                  freemsg(mp);
12131 12131                  return (NULL);
12132 12132          }
12133 12133          mp1->b_cont = mp;
12134 12134          if (priority != 0) {
12135 12135                  mp1->b_band = priority;
12136 12136                  ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
12137 12137                      priority;
12138 12138          }
12139 12139          return (mp1);
12140 12140  }
12141 12141  
12142 12142  /*
12143 12143   * Finish the outbound IPsec processing. This function is called from
12144 12144   * ipsec_out_process() if the IPsec packet was processed
12145 12145   * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12146 12146   * asynchronously.
12147 12147   *
12148 12148   * This is common to IPv4 and IPv6.
12149 12149   */
12150 12150  int
12151 12151  ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
12152 12152  {
12153 12153          iaflags_t       ixaflags = ixa->ixa_flags;
12154 12154          uint_t          pktlen;
12155 12155  
12156 12156  
12157 12157          /* AH/ESP don't update ixa_pktlen when they modify the packet */
12158 12158          if (ixaflags & IXAF_IS_IPV4) {
12159 12159                  ipha_t          *ipha = (ipha_t *)mp->b_rptr;
12160 12160  
12161 12161                  ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12162 12162                  pktlen = ntohs(ipha->ipha_length);
12163 12163          } else {
12164 12164                  ip6_t           *ip6h = (ip6_t *)mp->b_rptr;
12165 12165  
12166 12166                  ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12167 12167                  pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12168 12168          }
12169 12169  
12170 12170          /*
12171 12171           * We release any hard reference on the SAs here to make
12172 12172           * sure the SAs can be garbage collected. ipsr_sa has a soft reference
12173 12173           * on the SAs.
12174 12174           * If in the future we want the hard latching of the SAs in the
12175 12175           * ip_xmit_attr_t then we should remove this.
12176 12176           */
12177 12177          if (ixa->ixa_ipsec_esp_sa != NULL) {
12178 12178                  IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12179 12179                  ixa->ixa_ipsec_esp_sa = NULL;
12180 12180          }
12181 12181          if (ixa->ixa_ipsec_ah_sa != NULL) {
12182 12182                  IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12183 12183                  ixa->ixa_ipsec_ah_sa = NULL;
12184 12184          }
12185 12185  
12186 12186          /* Do we need to fragment? */
12187 12187          if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
12188 12188              pktlen > ixa->ixa_fragsize) {
12189 12189                  if (ixaflags & IXAF_IS_IPV4) {
12190 12190                          ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
12191 12191                          /*
12192 12192                           * We check for the DF case in ipsec_out_process
12193 12193                           * hence this only handles the non-DF case.
12194 12194                           */
12195 12195                          return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
12196 12196                              pktlen, ixa->ixa_fragsize,
12197 12197                              ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12198 12198                              ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
12199 12199                              &ixa->ixa_cookie));
12200 12200                  } else {
12201 12201                          mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
12202 12202                          if (mp == NULL) {
12203 12203                                  /* MIB and ip_drop_output already done */
12204 12204                                  return (ENOMEM);
12205 12205                          }
12206 12206                          pktlen += sizeof (ip6_frag_t);
12207 12207                          if (pktlen > ixa->ixa_fragsize) {
12208 12208                                  return (ip_fragment_v6(mp, ixa->ixa_nce,
12209 12209                                      ixa->ixa_flags, pktlen,
12210 12210                                      ixa->ixa_fragsize, ixa->ixa_xmit_hint,
12211 12211                                      ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
12212 12212                                      ixa->ixa_postfragfn, &ixa->ixa_cookie));
12213 12213                          }
12214 12214                  }
12215 12215          }
12216 12216          return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
12217 12217              pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12218 12218              ixa->ixa_no_loop_zoneid, NULL));
12219 12219  }
12220 12220  
12221 12221  /*
12222 12222   * Finish the inbound IPsec processing. This function is called from
12223 12223   * ipsec_out_process() if the IPsec packet was processed
12224 12224   * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12225 12225   * asynchronously.
12226 12226   *
12227 12227   * This is common to IPv4 and IPv6.
12228 12228   */
12229 12229  void
12230 12230  ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
12231 12231  {
12232 12232          iaflags_t       iraflags = ira->ira_flags;
12233 12233  
12234 12234          /* Length might have changed */
12235 12235          if (iraflags & IRAF_IS_IPV4) {
12236 12236                  ipha_t          *ipha = (ipha_t *)mp->b_rptr;
12237 12237  
12238 12238                  ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12239 12239                  ira->ira_pktlen = ntohs(ipha->ipha_length);
12240 12240                  ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
12241 12241                  ira->ira_protocol = ipha->ipha_protocol;
12242 12242  
12243 12243                  ip_fanout_v4(mp, ipha, ira);
12244 12244          } else {
12245 12245                  ip6_t           *ip6h = (ip6_t *)mp->b_rptr;
12246 12246                  uint8_t         *nexthdrp;
12247 12247  
12248 12248                  ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12249 12249                  ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12250 12250                  if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
12251 12251                      &nexthdrp)) {
12252 12252                          /* Malformed packet */
12253 12253                          BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
12254 12254                          ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
12255 12255                          freemsg(mp);
12256 12256                          return;
12257 12257                  }
12258 12258                  ira->ira_protocol = *nexthdrp;
12259 12259                  ip_fanout_v6(mp, ip6h, ira);
12260 12260          }
12261 12261  }
12262 12262  
12263 12263  /*
12264 12264   * Select which AH & ESP SA's to use (if any) for the outbound packet.
12265 12265   *
12266 12266   * If this function returns B_TRUE, the requested SA's have been filled
12267 12267   * into the ixa_ipsec_*_sa pointers.
12268 12268   *
12269 12269   * If the function returns B_FALSE, the packet has been "consumed", most
12270 12270   * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
12271 12271   *
12272 12272   * The SA references created by the protocol-specific "select"
12273 12273   * function will be released in ip_output_post_ipsec.
12274 12274   */
12275 12275  static boolean_t
12276 12276  ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
12277 12277  {
12278 12278          boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
12279 12279          ipsec_policy_t *pp;
12280 12280          ipsec_action_t *ap;
12281 12281  
12282 12282          ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12283 12283          ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12284 12284              (ixa->ixa_ipsec_action != NULL));
12285 12285  
12286 12286          ap = ixa->ixa_ipsec_action;
12287 12287          if (ap == NULL) {
12288 12288                  pp = ixa->ixa_ipsec_policy;
12289 12289                  ASSERT(pp != NULL);
12290 12290                  ap = pp->ipsp_act;
12291 12291                  ASSERT(ap != NULL);
12292 12292          }
12293 12293  
12294 12294          /*
12295 12295           * We have an action.  now, let's select SA's.
12296 12296           * A side effect of setting ixa_ipsec_*_sa is that it will
12297 12297           * be cached in the conn_t.
12298 12298           */
12299 12299          if (ap->ipa_want_esp) {
12300 12300                  if (ixa->ixa_ipsec_esp_sa == NULL) {
12301 12301                          need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
12302 12302                              IPPROTO_ESP);
12303 12303                  }
12304 12304                  ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
12305 12305          }
12306 12306  
12307 12307          if (ap->ipa_want_ah) {
12308 12308                  if (ixa->ixa_ipsec_ah_sa == NULL) {
12309 12309                          need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
12310 12310                              IPPROTO_AH);
12311 12311                  }
12312 12312                  ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
12313 12313                  /*
12314 12314                   * The ESP and AH processing order needs to be preserved
12315 12315                   * when both protocols are required (ESP should be applied
12316 12316                   * before AH for an outbound packet). Force an ESP ACQUIRE
12317 12317                   * when both ESP and AH are required, and an AH ACQUIRE
12318 12318                   * is needed.
12319 12319                   */
12320 12320                  if (ap->ipa_want_esp && need_ah_acquire)
12321 12321                          need_esp_acquire = B_TRUE;
12322 12322          }
12323 12323  
12324 12324          /*
12325 12325           * Send an ACQUIRE (extended, regular, or both) if we need one.
12326 12326           * Release SAs that got referenced, but will not be used until we
12327 12327           * acquire _all_ of the SAs we need.
12328 12328           */
12329 12329          if (need_ah_acquire || need_esp_acquire) {
12330 12330                  if (ixa->ixa_ipsec_ah_sa != NULL) {
12331 12331                          IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12332 12332                          ixa->ixa_ipsec_ah_sa = NULL;
12333 12333                  }
12334 12334                  if (ixa->ixa_ipsec_esp_sa != NULL) {
12335 12335                          IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12336 12336                          ixa->ixa_ipsec_esp_sa = NULL;
12337 12337                  }
12338 12338  
12339 12339                  sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
12340 12340                  return (B_FALSE);
12341 12341          }
12342 12342  
12343 12343          return (B_TRUE);
12344 12344  }
12345 12345  
12346 12346  /*
12347 12347   * Handle IPsec output processing.
12348 12348   * This function is only entered once for a given packet.
12349 12349   * We try to do things synchronously, but if we need to have user-level
12350 12350   * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
12351 12351   * will be completed
12352 12352   *  - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
12353 12353   *  - when asynchronous ESP is done it will do AH
12354 12354   *
12355 12355   * In all cases we come back in ip_output_post_ipsec() to fragment and
12356 12356   * send out the packet.
12357 12357   */
12358 12358  int
12359 12359  ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
12360 12360  {
12361 12361          ill_t           *ill = ixa->ixa_nce->nce_ill;
12362 12362          ip_stack_t      *ipst = ixa->ixa_ipst;
12363 12363          ipsec_stack_t   *ipss;
12364 12364          ipsec_policy_t  *pp;
12365 12365          ipsec_action_t  *ap;
12366 12366  
12367 12367          ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12368 12368  
12369 12369          ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12370 12370              (ixa->ixa_ipsec_action != NULL));
12371 12371  
12372 12372          ipss = ipst->ips_netstack->netstack_ipsec;
12373 12373          if (!ipsec_loaded(ipss)) {
12374 12374                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12375 12375                  ip_drop_packet(mp, B_TRUE, ill,
12376 12376                      DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12377 12377                      &ipss->ipsec_dropper);
12378 12378                  return (ENOTSUP);
12379 12379          }
12380 12380  
12381 12381          ap = ixa->ixa_ipsec_action;
12382 12382          if (ap == NULL) {
12383 12383                  pp = ixa->ixa_ipsec_policy;
12384 12384                  ASSERT(pp != NULL);
12385 12385                  ap = pp->ipsp_act;
12386 12386                  ASSERT(ap != NULL);
12387 12387          }
12388 12388  
12389 12389          /* Handle explicit drop action and bypass. */
12390 12390          switch (ap->ipa_act.ipa_type) {
12391 12391          case IPSEC_ACT_DISCARD:
12392 12392          case IPSEC_ACT_REJECT:
12393 12393                  ip_drop_packet(mp, B_FALSE, ill,
12394 12394                      DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12395 12395                  return (EHOSTUNREACH);  /* IPsec policy failure */
12396 12396          case IPSEC_ACT_BYPASS:
12397 12397                  return (ip_output_post_ipsec(mp, ixa));
12398 12398          }
12399 12399  
12400 12400          /*
12401 12401           * The order of processing is first insert a IP header if needed.
12402 12402           * Then insert the ESP header and then the AH header.
12403 12403           */
12404 12404          if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12405 12405                  /*
12406 12406                   * First get the outer IP header before sending
12407 12407                   * it to ESP.
12408 12408                   */
12409 12409                  ipha_t *oipha, *iipha;
12410 12410                  mblk_t *outer_mp, *inner_mp;
12411 12411  
12412 12412                  if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12413 12413                          (void) mi_strlog(ill->ill_rq, 0,
12414 12414                              SL_ERROR|SL_TRACE|SL_CONSOLE,
12415 12415                              "ipsec_out_process: "
12416 12416                              "Self-Encapsulation failed: Out of memory\n");
12417 12417                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12418 12418                          ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12419 12419                          freemsg(mp);
12420 12420                          return (ENOBUFS);
12421 12421                  }
12422 12422                  inner_mp = mp;
12423 12423                  ASSERT(inner_mp->b_datap->db_type == M_DATA);
12424 12424                  oipha = (ipha_t *)outer_mp->b_rptr;
12425 12425                  iipha = (ipha_t *)inner_mp->b_rptr;
12426 12426                  *oipha = *iipha;
12427 12427                  outer_mp->b_wptr += sizeof (ipha_t);
12428 12428                  oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12429 12429                      sizeof (ipha_t));
12430 12430                  oipha->ipha_protocol = IPPROTO_ENCAP;
12431 12431                  oipha->ipha_version_and_hdr_length =
12432 12432                      IP_SIMPLE_HDR_VERSION;
12433 12433                  oipha->ipha_hdr_checksum = 0;
12434 12434                  oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12435 12435                  outer_mp->b_cont = inner_mp;
12436 12436                  mp = outer_mp;
12437 12437  
12438 12438                  ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12439 12439          }
12440 12440  
12441 12441          /* If we need to wait for a SA then we can't return any errno */
12442 12442          if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12443 12443              (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12444 12444              !ipsec_out_select_sa(mp, ixa))
12445 12445                  return (0);
12446 12446  
12447 12447          /*
12448 12448           * By now, we know what SA's to use.  Toss over to ESP & AH
12449 12449           * to do the heavy lifting.
12450 12450           */
12451 12451          if (ap->ipa_want_esp) {
12452 12452                  ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12453 12453  
12454 12454                  mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12455 12455                  if (mp == NULL) {
12456 12456                          /*
12457 12457                           * Either it failed or is pending. In the former case
12458 12458                           * ipIfStatsInDiscards was increased.
12459 12459                           */
12460 12460                          return (0);
12461 12461                  }
12462 12462          }
12463 12463  
12464 12464          if (ap->ipa_want_ah) {
12465 12465                  ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12466 12466  
12467 12467                  mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12468 12468                  if (mp == NULL) {
12469 12469                          /*
12470 12470                           * Either it failed or is pending. In the former case
12471 12471                           * ipIfStatsInDiscards was increased.
12472 12472                           */
12473 12473                          return (0);
12474 12474                  }
12475 12475          }
12476 12476          /*
12477 12477           * We are done with IPsec processing. Send it over
12478 12478           * the wire.
12479 12479           */
12480 12480          return (ip_output_post_ipsec(mp, ixa));
12481 12481  }
12482 12482  
12483 12483  /*
12484 12484   * ioctls that go through a down/up sequence may need to wait for the down
12485 12485   * to complete. This involves waiting for the ire and ipif refcnts to go down
12486 12486   * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12487 12487   */
12488 12488  /* ARGSUSED */
12489 12489  void
12490 12490  ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12491 12491  {
12492 12492          struct iocblk *iocp;
12493 12493          mblk_t *mp1;
12494 12494          ip_ioctl_cmd_t *ipip;
12495 12495          int err;
12496 12496          sin_t   *sin;
12497 12497          struct lifreq *lifr;
12498 12498          struct ifreq *ifr;
12499 12499  
12500 12500          iocp = (struct iocblk *)mp->b_rptr;
12501 12501          ASSERT(ipsq != NULL);
12502 12502          /* Existence of mp1 verified in ip_wput_nondata */
12503 12503          mp1 = mp->b_cont->b_cont;
12504 12504          ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12505 12505          if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12506 12506                  /*
12507 12507                   * Special case where ipx_current_ipif is not set:
12508 12508                   * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12509 12509                   * We are here as were not able to complete the operation in
12510 12510                   * ipif_set_values because we could not become exclusive on
12511 12511                   * the new ipsq.
12512 12512                   */
12513 12513                  ill_t *ill = q->q_ptr;
12514 12514                  ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12515 12515          }
12516 12516          ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12517 12517  
12518 12518          if (ipip->ipi_cmd_type == IF_CMD) {
12519 12519                  /* This a old style SIOC[GS]IF* command */
12520 12520                  ifr = (struct ifreq *)mp1->b_rptr;
12521 12521                  sin = (sin_t *)&ifr->ifr_addr;
12522 12522          } else if (ipip->ipi_cmd_type == LIF_CMD) {
12523 12523                  /* This a new style SIOC[GS]LIF* command */
12524 12524                  lifr = (struct lifreq *)mp1->b_rptr;
12525 12525                  sin = (sin_t *)&lifr->lifr_addr;
12526 12526          } else {
12527 12527                  sin = NULL;
12528 12528          }
12529 12529  
12530 12530          err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12531 12531              q, mp, ipip, mp1->b_rptr);
12532 12532  
12533 12533          DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12534 12534              int, ipip->ipi_cmd,
12535 12535              ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12536 12536              ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12537 12537  
12538 12538          ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12539 12539  }
12540 12540  
12541 12541  /*
12542 12542   * ioctl processing
12543 12543   *
12544 12544   * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12545 12545   * the ioctl command in the ioctl tables, determines the copyin data size
12546 12546   * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12547 12547   *
12548 12548   * ioctl processing then continues when the M_IOCDATA makes its way down to
12549 12549   * ip_wput_nondata().  The ioctl is looked up again in the ioctl table, its
12550 12550   * associated 'conn' is refheld till the end of the ioctl and the general
12551 12551   * ioctl processing function ip_process_ioctl() is called to extract the
12552 12552   * arguments and process the ioctl.  To simplify extraction, ioctl commands
12553 12553   * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12554 12554   * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12555 12555   * is used to extract the ioctl's arguments.
12556 12556   *
12557 12557   * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12558 12558   * so goes thru the serialization primitive ipsq_try_enter. Then the
12559 12559   * appropriate function to handle the ioctl is called based on the entry in
12560 12560   * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12561 12561   * which also refreleases the 'conn' that was refheld at the start of the
12562 12562   * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12563 12563   *
12564 12564   * Many exclusive ioctls go thru an internal down up sequence as part of
12565 12565   * the operation. For example an attempt to change the IP address of an
12566 12566   * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12567 12567   * does all the cleanup such as deleting all ires that use this address.
12568 12568   * Then we need to wait till all references to the interface go away.
12569 12569   */
12570 12570  void
12571 12571  ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12572 12572  {
12573 12573          struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12574 12574          ip_ioctl_cmd_t *ipip = arg;
12575 12575          ip_extract_func_t *extract_funcp;
12576 12576          cmd_info_t ci;
12577 12577          int err;
12578 12578          boolean_t entered_ipsq = B_FALSE;
12579 12579  
12580 12580          ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12581 12581  
12582 12582          if (ipip == NULL)
12583 12583                  ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12584 12584  
12585 12585          /*
12586 12586           * SIOCLIFADDIF needs to go thru a special path since the
12587 12587           * ill may not exist yet. This happens in the case of lo0
12588 12588           * which is created using this ioctl.
12589 12589           */
12590 12590          if (ipip->ipi_cmd == SIOCLIFADDIF) {
12591 12591                  err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12592 12592                  DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12593 12593                      int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12594 12594                  ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12595 12595                  return;
12596 12596          }
12597 12597  
12598 12598          ci.ci_ipif = NULL;
12599 12599          switch (ipip->ipi_cmd_type) {
12600 12600          case MISC_CMD:
12601 12601          case MSFILT_CMD:
12602 12602                  /*
12603 12603                   * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12604 12604                   */
12605 12605                  if (ipip->ipi_cmd == IF_UNITSEL) {
12606 12606                          /* ioctl comes down the ill */
12607 12607                          ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12608 12608                          ipif_refhold(ci.ci_ipif);
12609 12609                  }
12610 12610                  err = 0;
12611 12611                  ci.ci_sin = NULL;
12612 12612                  ci.ci_sin6 = NULL;
12613 12613                  ci.ci_lifr = NULL;
12614 12614                  extract_funcp = NULL;
12615 12615                  break;
12616 12616  
12617 12617          case IF_CMD:
12618 12618          case LIF_CMD:
12619 12619                  extract_funcp = ip_extract_lifreq;
12620 12620                  break;
12621 12621  
12622 12622          case ARP_CMD:
12623 12623          case XARP_CMD:
12624 12624                  extract_funcp = ip_extract_arpreq;
12625 12625                  break;
12626 12626  
12627 12627          default:
12628 12628                  ASSERT(0);
12629 12629          }
12630 12630  
12631 12631          if (extract_funcp != NULL) {
12632 12632                  err = (*extract_funcp)(q, mp, ipip, &ci);
12633 12633                  if (err != 0) {
12634 12634                          DTRACE_PROBE4(ipif__ioctl,
12635 12635                              char *, "ip_process_ioctl finish err",
12636 12636                              int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12637 12637                          ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12638 12638                          return;
12639 12639                  }
12640 12640  
12641 12641                  /*
12642 12642                   * All of the extraction functions return a refheld ipif.
12643 12643                   */
12644 12644                  ASSERT(ci.ci_ipif != NULL);
12645 12645          }
12646 12646  
12647 12647          if (!(ipip->ipi_flags & IPI_WR)) {
12648 12648                  /*
12649 12649                   * A return value of EINPROGRESS means the ioctl is
12650 12650                   * either queued and waiting for some reason or has
12651 12651                   * already completed.
12652 12652                   */
12653 12653                  err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12654 12654                      ci.ci_lifr);
12655 12655                  if (ci.ci_ipif != NULL) {
12656 12656                          DTRACE_PROBE4(ipif__ioctl,
12657 12657                              char *, "ip_process_ioctl finish RD",
12658 12658                              int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12659 12659                              ipif_t *, ci.ci_ipif);
12660 12660                          ipif_refrele(ci.ci_ipif);
12661 12661                  } else {
12662 12662                          DTRACE_PROBE4(ipif__ioctl,
12663 12663                              char *, "ip_process_ioctl finish RD",
12664 12664                              int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12665 12665                  }
12666 12666                  ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12667 12667                  return;
12668 12668          }
12669 12669  
12670 12670          ASSERT(ci.ci_ipif != NULL);
12671 12671  
12672 12672          /*
12673 12673           * If ipsq is non-NULL, we are already being called exclusively
12674 12674           */
12675 12675          ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12676 12676          if (ipsq == NULL) {
12677 12677                  ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12678 12678                      NEW_OP, B_TRUE);
12679 12679                  if (ipsq == NULL) {
12680 12680                          ipif_refrele(ci.ci_ipif);
12681 12681                          return;
12682 12682                  }
12683 12683                  entered_ipsq = B_TRUE;
12684 12684          }
12685 12685          /*
12686 12686           * Release the ipif so that ipif_down and friends that wait for
12687 12687           * references to go away are not misled about the current ipif_refcnt
12688 12688           * values. We are writer so we can access the ipif even after releasing
12689 12689           * the ipif.
12690 12690           */
12691 12691          ipif_refrele(ci.ci_ipif);
12692 12692  
12693 12693          ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12694 12694  
12695 12695          /*
12696 12696           * A return value of EINPROGRESS means the ioctl is
12697 12697           * either queued and waiting for some reason or has
12698 12698           * already completed.
12699 12699           */
12700 12700          err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12701 12701  
12702 12702          DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12703 12703              int, ipip->ipi_cmd,
12704 12704              ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12705 12705              ipif_t *, ci.ci_ipif);
12706 12706          ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12707 12707  
12708 12708          if (entered_ipsq)
12709 12709                  ipsq_exit(ipsq);
12710 12710  }
12711 12711  
12712 12712  /*
12713 12713   * Complete the ioctl. Typically ioctls use the mi package and need to
12714 12714   * do mi_copyout/mi_copy_done.
12715 12715   */
12716 12716  void
12717 12717  ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12718 12718  {
12719 12719          conn_t  *connp = NULL;
12720 12720  
12721 12721          if (err == EINPROGRESS)
12722 12722                  return;
12723 12723  
12724 12724          if (CONN_Q(q)) {
12725 12725                  connp = Q_TO_CONN(q);
12726 12726                  ASSERT(connp->conn_ref >= 2);
12727 12727          }
12728 12728  
12729 12729          switch (mode) {
12730 12730          case COPYOUT:
12731 12731                  if (err == 0)
12732 12732                          mi_copyout(q, mp);
12733 12733                  else
12734 12734                          mi_copy_done(q, mp, err);
12735 12735                  break;
12736 12736  
12737 12737          case NO_COPYOUT:
12738 12738                  mi_copy_done(q, mp, err);
12739 12739                  break;
12740 12740  
12741 12741          default:
12742 12742                  ASSERT(mode == CONN_CLOSE);     /* aborted through CONN_CLOSE */
12743 12743                  break;
12744 12744          }
12745 12745  
12746 12746          /*
12747 12747           * The conn refhold and ioctlref placed on the conn at the start of the
12748 12748           * ioctl are released here.
12749 12749           */
12750 12750          if (connp != NULL) {
12751 12751                  CONN_DEC_IOCTLREF(connp);
12752 12752                  CONN_OPER_PENDING_DONE(connp);
12753 12753          }
12754 12754  
12755 12755          if (ipsq != NULL)
12756 12756                  ipsq_current_finish(ipsq);
12757 12757  }
12758 12758  
12759 12759  /* Handles all non data messages */
12760 12760  int
12761 12761  ip_wput_nondata(queue_t *q, mblk_t *mp)
12762 12762  {
12763 12763          mblk_t          *mp1;
12764 12764          struct iocblk   *iocp;
12765 12765          ip_ioctl_cmd_t  *ipip;
12766 12766          conn_t          *connp;
12767 12767          cred_t          *cr;
12768 12768          char            *proto_str;
12769 12769  
12770 12770          if (CONN_Q(q))
12771 12771                  connp = Q_TO_CONN(q);
12772 12772          else
12773 12773                  connp = NULL;
12774 12774  
12775 12775          switch (DB_TYPE(mp)) {
12776 12776          case M_IOCTL:
12777 12777                  /*
12778 12778                   * IOCTL processing begins in ip_sioctl_copyin_setup which
12779 12779                   * will arrange to copy in associated control structures.
12780 12780                   */
12781 12781                  ip_sioctl_copyin_setup(q, mp);
12782 12782                  return (0);
12783 12783          case M_IOCDATA:
12784 12784                  /*
12785 12785                   * Ensure that this is associated with one of our trans-
12786 12786                   * parent ioctls.  If it's not ours, discard it if we're
12787 12787                   * running as a driver, or pass it on if we're a module.
12788 12788                   */
12789 12789                  iocp = (struct iocblk *)mp->b_rptr;
12790 12790                  ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12791 12791                  if (ipip == NULL) {
12792 12792                          if (q->q_next == NULL) {
12793 12793                                  goto nak;
12794 12794                          } else {
12795 12795                                  putnext(q, mp);
12796 12796                          }
12797 12797                          return (0);
12798 12798                  }
12799 12799                  if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12800 12800                          /*
12801 12801                           * The ioctl is one we recognise, but is not consumed
12802 12802                           * by IP as a module and we are a module, so we drop
12803 12803                           */
12804 12804                          goto nak;
12805 12805                  }
12806 12806  
12807 12807                  /* IOCTL continuation following copyin or copyout. */
12808 12808                  if (mi_copy_state(q, mp, NULL) == -1) {
12809 12809                          /*
12810 12810                           * The copy operation failed.  mi_copy_state already
12811 12811                           * cleaned up, so we're out of here.
12812 12812                           */
12813 12813                          return (0);
12814 12814                  }
12815 12815                  /*
12816 12816                   * If we just completed a copy in, we become writer and
12817 12817                   * continue processing in ip_sioctl_copyin_done.  If it
12818 12818                   * was a copy out, we call mi_copyout again.  If there is
12819 12819                   * nothing more to copy out, it will complete the IOCTL.
12820 12820                   */
12821 12821                  if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12822 12822                          if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12823 12823                                  mi_copy_done(q, mp, EPROTO);
12824 12824                                  return (0);
12825 12825                          }
12826 12826                          /*
12827 12827                           * Check for cases that need more copying.  A return
12828 12828                           * value of 0 means a second copyin has been started,
12829 12829                           * so we return; a return value of 1 means no more
12830 12830                           * copying is needed, so we continue.
12831 12831                           */
12832 12832                          if (ipip->ipi_cmd_type == MSFILT_CMD &&
12833 12833                              MI_COPY_COUNT(mp) == 1) {
12834 12834                                  if (ip_copyin_msfilter(q, mp) == 0)
12835 12835                                          return (0);
12836 12836                          }
12837 12837                          /*
12838 12838                           * Refhold the conn, till the ioctl completes. This is
12839 12839                           * needed in case the ioctl ends up in the pending mp
12840 12840                           * list. Every mp in the ipx_pending_mp list must have
12841 12841                           * a refhold on the conn to resume processing. The
12842 12842                           * refhold is released when the ioctl completes
12843 12843                           * (whether normally or abnormally). An ioctlref is also
12844 12844                           * placed on the conn to prevent TCP from removing the
12845 12845                           * queue needed to send the ioctl reply back.
12846 12846                           * In all cases ip_ioctl_finish is called to finish
12847 12847                           * the ioctl and release the refholds.
12848 12848                           */
12849 12849                          if (connp != NULL) {
12850 12850                                  /* This is not a reentry */
12851 12851                                  CONN_INC_REF(connp);
12852 12852                                  CONN_INC_IOCTLREF(connp);
12853 12853                          } else {
12854 12854                                  if (!(ipip->ipi_flags & IPI_MODOK)) {
12855 12855                                          mi_copy_done(q, mp, EINVAL);
12856 12856                                          return (0);
12857 12857                                  }
12858 12858                          }
12859 12859  
12860 12860                          ip_process_ioctl(NULL, q, mp, ipip);
12861 12861  
12862 12862                  } else {
12863 12863                          mi_copyout(q, mp);
12864 12864                  }
12865 12865                  return (0);
12866 12866  
12867 12867          case M_IOCNAK:
12868 12868                  /*
12869 12869                   * The only way we could get here is if a resolver didn't like
12870 12870                   * an IOCTL we sent it.  This shouldn't happen.
12871 12871                   */
12872 12872                  (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12873 12873                      "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12874 12874                      ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12875 12875                  freemsg(mp);
12876 12876                  return (0);
12877 12877          case M_IOCACK:
12878 12878                  /* /dev/ip shouldn't see this */
12879 12879                  goto nak;
12880 12880          case M_FLUSH:
12881 12881                  if (*mp->b_rptr & FLUSHW)
12882 12882                          flushq(q, FLUSHALL);
12883 12883                  if (q->q_next) {
12884 12884                          putnext(q, mp);
12885 12885                          return (0);
12886 12886                  }
12887 12887                  if (*mp->b_rptr & FLUSHR) {
12888 12888                          *mp->b_rptr &= ~FLUSHW;
12889 12889                          qreply(q, mp);
12890 12890                          return (0);
12891 12891                  }
12892 12892                  freemsg(mp);
12893 12893                  return (0);
12894 12894          case M_CTL:
12895 12895                  break;
12896 12896          case M_PROTO:
12897 12897          case M_PCPROTO:
12898 12898                  /*
12899 12899                   * The only PROTO messages we expect are SNMP-related.
12900 12900                   */
12901 12901                  switch (((union T_primitives *)mp->b_rptr)->type) {
12902 12902                  case T_SVR4_OPTMGMT_REQ:
12903 12903                          ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12904 12904                              "flags %x\n",
12905 12905                              ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12906 12906  
12907 12907                          if (connp == NULL) {
12908 12908                                  proto_str = "T_SVR4_OPTMGMT_REQ";
12909 12909                                  goto protonak;
12910 12910                          }
12911 12911  
12912 12912                          /*
12913 12913                           * All Solaris components should pass a db_credp
12914 12914                           * for this TPI message, hence we ASSERT.
12915 12915                           * But in case there is some other M_PROTO that looks
12916 12916                           * like a TPI message sent by some other kernel
12917 12917                           * component, we check and return an error.
12918 12918                           */
12919 12919                          cr = msg_getcred(mp, NULL);
12920 12920                          ASSERT(cr != NULL);
12921 12921                          if (cr == NULL) {
12922 12922                                  mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12923 12923                                  if (mp != NULL)
12924 12924                                          qreply(q, mp);
12925 12925                                  return (0);
12926 12926                          }
12927 12927  
12928 12928                          if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12929 12929                                  proto_str = "Bad SNMPCOM request?";
12930 12930                                  goto protonak;
12931 12931                          }
12932 12932                          return (0);
12933 12933                  default:
12934 12934                          ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12935 12935                              (int)*(uint_t *)mp->b_rptr));
12936 12936                          freemsg(mp);
12937 12937                          return (0);
12938 12938                  }
12939 12939          default:
12940 12940                  break;
12941 12941          }
12942 12942          if (q->q_next) {
12943 12943                  putnext(q, mp);
12944 12944          } else
12945 12945                  freemsg(mp);
12946 12946          return (0);
12947 12947  
12948 12948  nak:
12949 12949          iocp->ioc_error = EINVAL;
12950 12950          mp->b_datap->db_type = M_IOCNAK;
12951 12951          iocp->ioc_count = 0;
12952 12952          qreply(q, mp);
12953 12953          return (0);
12954 12954  
12955 12955  protonak:
12956 12956          cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12957 12957          if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12958 12958                  qreply(q, mp);
12959 12959          return (0);
12960 12960  }
12961 12961  
12962 12962  /*
12963 12963   * Process IP options in an outbound packet.  Verify that the nexthop in a
12964 12964   * strict source route is onlink.
12965 12965   * Returns non-zero if something fails in which case an ICMP error has been
12966 12966   * sent and mp freed.
12967 12967   *
12968 12968   * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12969 12969   */
12970 12970  int
12971 12971  ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12972 12972  {
12973 12973          ipoptp_t        opts;
12974 12974          uchar_t         *opt;
12975 12975          uint8_t         optval;
12976 12976          uint8_t         optlen;
12977 12977          ipaddr_t        dst;
12978 12978          intptr_t        code = 0;
12979 12979          ire_t           *ire;
12980 12980          ip_stack_t      *ipst = ixa->ixa_ipst;
12981 12981          ip_recv_attr_t  iras;
12982 12982  
12983 12983          ip2dbg(("ip_output_options\n"));
12984 12984  
12985 12985          dst = ipha->ipha_dst;
12986 12986          for (optval = ipoptp_first(&opts, ipha);
12987 12987              optval != IPOPT_EOL;
12988 12988              optval = ipoptp_next(&opts)) {
12989 12989                  opt = opts.ipoptp_cur;
12990 12990                  optlen = opts.ipoptp_len;
12991 12991                  ip2dbg(("ip_output_options: opt %d, len %d\n",
12992 12992                      optval, optlen));
12993 12993                  switch (optval) {
12994 12994                          uint32_t off;
12995 12995                  case IPOPT_SSRR:
12996 12996                  case IPOPT_LSRR:
12997 12997                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12998 12998                                  ip1dbg((
12999 12999                                      "ip_output_options: bad option offset\n"));
13000 13000                                  code = (char *)&opt[IPOPT_OLEN] -
13001 13001                                      (char *)ipha;
13002 13002                                  goto param_prob;
13003 13003                          }
13004 13004                          off = opt[IPOPT_OFFSET];
13005 13005                          ip1dbg(("ip_output_options: next hop 0x%x\n",
13006 13006                              ntohl(dst)));
13007 13007                          /*
13008 13008                           * For strict: verify that dst is directly
13009 13009                           * reachable.
13010 13010                           */
13011 13011                          if (optval == IPOPT_SSRR) {
13012 13012                                  ire = ire_ftable_lookup_v4(dst, 0, 0,
13013 13013                                      IRE_INTERFACE, NULL, ALL_ZONES,
13014 13014                                      ixa->ixa_tsl,
13015 13015                                      MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
13016 13016                                      NULL);
13017 13017                                  if (ire == NULL) {
13018 13018                                          ip1dbg(("ip_output_options: SSRR not"
13019 13019                                              " directly reachable: 0x%x\n",
13020 13020                                              ntohl(dst)));
13021 13021                                          goto bad_src_route;
13022 13022                                  }
13023 13023                                  ire_refrele(ire);
13024 13024                          }
13025 13025                          break;
13026 13026                  case IPOPT_RR:
13027 13027                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13028 13028                                  ip1dbg((
13029 13029                                      "ip_output_options: bad option offset\n"));
13030 13030                                  code = (char *)&opt[IPOPT_OLEN] -
13031 13031                                      (char *)ipha;
13032 13032                                  goto param_prob;
13033 13033                          }
13034 13034                          break;
13035 13035                  case IPOPT_TS:
13036 13036                          /*
13037 13037                           * Verify that length >=5 and that there is either
13038 13038                           * room for another timestamp or that the overflow
13039 13039                           * counter is not maxed out.
13040 13040                           */
13041 13041                          code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
13042 13042                          if (optlen < IPOPT_MINLEN_IT) {
13043 13043                                  goto param_prob;
13044 13044                          }
13045 13045                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13046 13046                                  ip1dbg((
13047 13047                                      "ip_output_options: bad option offset\n"));
13048 13048                                  code = (char *)&opt[IPOPT_OFFSET] -
13049 13049                                      (char *)ipha;
13050 13050                                  goto param_prob;
13051 13051                          }
13052 13052                          switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
13053 13053                          case IPOPT_TS_TSONLY:
13054 13054                                  off = IPOPT_TS_TIMELEN;
13055 13055                                  break;
13056 13056                          case IPOPT_TS_TSANDADDR:
13057 13057                          case IPOPT_TS_PRESPEC:
13058 13058                          case IPOPT_TS_PRESPEC_RFC791:
13059 13059                                  off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
13060 13060                                  break;
13061 13061                          default:
13062 13062                                  code = (char *)&opt[IPOPT_POS_OV_FLG] -
13063 13063                                      (char *)ipha;
13064 13064                                  goto param_prob;
13065 13065                          }
13066 13066                          if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
13067 13067                              (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
13068 13068                                  /*
13069 13069                                   * No room and the overflow counter is 15
13070 13070                                   * already.
13071 13071                                   */
13072 13072                                  goto param_prob;
13073 13073                          }
13074 13074                          break;
13075 13075                  }
13076 13076          }
13077 13077  
13078 13078          if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
13079 13079                  return (0);
13080 13080  
13081 13081          ip1dbg(("ip_output_options: error processing IP options."));
13082 13082          code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
13083 13083  
13084 13084  param_prob:
13085 13085          bzero(&iras, sizeof (iras));
13086 13086          iras.ira_ill = iras.ira_rill = ill;
13087 13087          iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13088 13088          iras.ira_rifindex = iras.ira_ruifindex;
13089 13089          iras.ira_flags = IRAF_IS_IPV4;
13090 13090  
13091 13091          ip_drop_output("ip_output_options", mp, ill);
13092 13092          icmp_param_problem(mp, (uint8_t)code, &iras);
13093 13093          ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13094 13094          return (-1);
13095 13095  
13096 13096  bad_src_route:
13097 13097          bzero(&iras, sizeof (iras));
13098 13098          iras.ira_ill = iras.ira_rill = ill;
13099 13099          iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13100 13100          iras.ira_rifindex = iras.ira_ruifindex;
13101 13101          iras.ira_flags = IRAF_IS_IPV4;
13102 13102  
13103 13103          ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
13104 13104          icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
13105 13105          ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13106 13106          return (-1);
13107 13107  }
13108 13108  
13109 13109  /*
13110 13110   * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
13111 13111   * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
13112 13112   * thru /etc/system.
13113 13113   */
13114 13114  #define CONN_MAXDRAINCNT        64
13115 13115  
13116 13116  static void
13117 13117  conn_drain_init(ip_stack_t *ipst)
13118 13118  {
13119 13119          int i, j;
13120 13120          idl_tx_list_t *itl_tx;
13121 13121  
13122 13122          ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
13123 13123  
13124 13124          if ((ipst->ips_conn_drain_list_cnt == 0) ||
13125 13125              (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
13126 13126                  /*
13127 13127                   * Default value of the number of drainers is the
13128 13128                   * number of cpus, subject to maximum of 8 drainers.
13129 13129                   */
13130 13130                  if (boot_max_ncpus != -1)
13131 13131                          ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
13132 13132                  else
13133 13133                          ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
13134 13134          }
13135 13135  
13136 13136          ipst->ips_idl_tx_list =
13137 13137              kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
13138 13138          for (i = 0; i < TX_FANOUT_SIZE; i++) {
13139 13139                  itl_tx =  &ipst->ips_idl_tx_list[i];
13140 13140                  itl_tx->txl_drain_list =
13141 13141                      kmem_zalloc(ipst->ips_conn_drain_list_cnt *
13142 13142                      sizeof (idl_t), KM_SLEEP);
13143 13143                  mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
13144 13144                  for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
13145 13145                          mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
13146 13146                              MUTEX_DEFAULT, NULL);
13147 13147                          itl_tx->txl_drain_list[j].idl_itl = itl_tx;
13148 13148                  }
13149 13149          }
13150 13150  }
13151 13151  
13152 13152  static void
13153 13153  conn_drain_fini(ip_stack_t *ipst)
13154 13154  {
13155 13155          int i;
13156 13156          idl_tx_list_t *itl_tx;
13157 13157  
13158 13158          for (i = 0; i < TX_FANOUT_SIZE; i++) {
13159 13159                  itl_tx =  &ipst->ips_idl_tx_list[i];
13160 13160                  kmem_free(itl_tx->txl_drain_list,
13161 13161                      ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
13162 13162          }
13163 13163          kmem_free(ipst->ips_idl_tx_list,
13164 13164              TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
13165 13165          ipst->ips_idl_tx_list = NULL;
13166 13166  }
13167 13167  
13168 13168  /*
13169 13169   * Flow control has blocked us from proceeding.  Insert the given conn in one
13170 13170   * of the conn drain lists.  When flow control is unblocked, either ip_wsrv()
13171 13171   * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
13172 13172   * will call conn_walk_drain().  See the flow control notes at the top of this
13173 13173   * file for more details.
13174 13174   */
13175 13175  void
13176 13176  conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
13177 13177  {
13178 13178          idl_t   *idl = tx_list->txl_drain_list;
13179 13179          uint_t  index;
13180 13180          ip_stack_t      *ipst = connp->conn_netstack->netstack_ip;
13181 13181  
13182 13182          mutex_enter(&connp->conn_lock);
13183 13183          if (connp->conn_state_flags & CONN_CLOSING) {
13184 13184                  /*
13185 13185                   * The conn is closing as a result of which CONN_CLOSING
13186 13186                   * is set. Return.
13187 13187                   */
13188 13188                  mutex_exit(&connp->conn_lock);
13189 13189                  return;
13190 13190          } else if (connp->conn_idl == NULL) {
13191 13191                  /*
13192 13192                   * Assign the next drain list round robin. We dont' use
13193 13193                   * a lock, and thus it may not be strictly round robin.
13194 13194                   * Atomicity of load/stores is enough to make sure that
13195 13195                   * conn_drain_list_index is always within bounds.
13196 13196                   */
13197 13197                  index = tx_list->txl_drain_index;
13198 13198                  ASSERT(index < ipst->ips_conn_drain_list_cnt);
13199 13199                  connp->conn_idl = &tx_list->txl_drain_list[index];
13200 13200                  index++;
13201 13201                  if (index == ipst->ips_conn_drain_list_cnt)
13202 13202                          index = 0;
13203 13203                  tx_list->txl_drain_index = index;
13204 13204          } else {
13205 13205                  ASSERT(connp->conn_idl->idl_itl == tx_list);
13206 13206          }
13207 13207          mutex_exit(&connp->conn_lock);
13208 13208  
13209 13209          idl = connp->conn_idl;
13210 13210          mutex_enter(&idl->idl_lock);
13211 13211          if ((connp->conn_drain_prev != NULL) ||
13212 13212              (connp->conn_state_flags & CONN_CLOSING)) {
13213 13213                  /*
13214 13214                   * The conn is either already in the drain list or closing.
13215 13215                   * (We needed to check for CONN_CLOSING again since close can
13216 13216                   * sneak in between dropping conn_lock and acquiring idl_lock.)
13217 13217                   */
13218 13218                  mutex_exit(&idl->idl_lock);
13219 13219                  return;
13220 13220          }
13221 13221  
13222 13222          /*
13223 13223           * The conn is not in the drain list. Insert it at the
13224 13224           * tail of the drain list. The drain list is circular
13225 13225           * and doubly linked. idl_conn points to the 1st element
13226 13226           * in the list.
13227 13227           */
13228 13228          if (idl->idl_conn == NULL) {
13229 13229                  idl->idl_conn = connp;
13230 13230                  connp->conn_drain_next = connp;
13231 13231                  connp->conn_drain_prev = connp;
13232 13232          } else {
13233 13233                  conn_t *head = idl->idl_conn;
13234 13234  
13235 13235                  connp->conn_drain_next = head;
13236 13236                  connp->conn_drain_prev = head->conn_drain_prev;
13237 13237                  head->conn_drain_prev->conn_drain_next = connp;
13238 13238                  head->conn_drain_prev = connp;
13239 13239          }
13240 13240          /*
13241 13241           * For non streams based sockets assert flow control.
13242 13242           */
13243 13243          conn_setqfull(connp, NULL);
13244 13244          mutex_exit(&idl->idl_lock);
13245 13245  }
13246 13246  
13247 13247  static void
13248 13248  conn_drain_remove(conn_t *connp)
13249 13249  {
13250 13250          idl_t *idl = connp->conn_idl;
13251 13251  
13252 13252          if (idl != NULL) {
13253 13253                  /*
13254 13254                   * Remove ourself from the drain list.
13255 13255                   */
13256 13256                  if (connp->conn_drain_next == connp) {
13257 13257                          /* Singleton in the list */
13258 13258                          ASSERT(connp->conn_drain_prev == connp);
13259 13259                          idl->idl_conn = NULL;
13260 13260                  } else {
13261 13261                          connp->conn_drain_prev->conn_drain_next =
13262 13262                              connp->conn_drain_next;
13263 13263                          connp->conn_drain_next->conn_drain_prev =
13264 13264                              connp->conn_drain_prev;
13265 13265                          if (idl->idl_conn == connp)
13266 13266                                  idl->idl_conn = connp->conn_drain_next;
13267 13267                  }
13268 13268  
13269 13269                  /*
13270 13270                   * NOTE: because conn_idl is associated with a specific drain
13271 13271                   * list which in turn is tied to the index the TX ring
13272 13272                   * (txl_cookie) hashes to, and because the TX ring can change
13273 13273                   * over the lifetime of the conn_t, we must clear conn_idl so
13274 13274                   * a subsequent conn_drain_insert() will set conn_idl again
13275 13275                   * based on the latest txl_cookie.
13276 13276                   */
13277 13277                  connp->conn_idl = NULL;
13278 13278          }
13279 13279          connp->conn_drain_next = NULL;
13280 13280          connp->conn_drain_prev = NULL;
13281 13281  
13282 13282          conn_clrqfull(connp, NULL);
13283 13283          /*
13284 13284           * For streams based sockets open up flow control.
13285 13285           */
13286 13286          if (!IPCL_IS_NONSTR(connp))
13287 13287                  enableok(connp->conn_wq);
13288 13288  }
13289 13289  
13290 13290  /*
13291 13291   * This conn is closing, and we are called from ip_close. OR
13292 13292   * this conn is draining because flow-control on the ill has been relieved.
13293 13293   *
13294 13294   * We must also need to remove conn's on this idl from the list, and also
13295 13295   * inform the sockfs upcalls about the change in flow-control.
13296 13296   */
13297 13297  static void
13298 13298  conn_drain(conn_t *connp, boolean_t closing)
13299 13299  {
13300 13300          idl_t *idl;
13301 13301          conn_t *next_connp;
13302 13302  
13303 13303          /*
13304 13304           * connp->conn_idl is stable at this point, and no lock is needed
13305 13305           * to check it. If we are called from ip_close, close has already
13306 13306           * set CONN_CLOSING, thus freezing the value of conn_idl, and
13307 13307           * called us only because conn_idl is non-null. If we are called thru
13308 13308           * service, conn_idl could be null, but it cannot change because
13309 13309           * service is single-threaded per queue, and there cannot be another
13310 13310           * instance of service trying to call conn_drain_insert on this conn
13311 13311           * now.
13312 13312           */
13313 13313          ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
13314 13314  
13315 13315          /*
13316 13316           * If the conn doesn't exist or is not on a drain list, bail.
13317 13317           */
13318 13318          if (connp == NULL || connp->conn_idl == NULL ||
13319 13319              connp->conn_drain_prev == NULL) {
13320 13320                  return;
13321 13321          }
13322 13322  
13323 13323          idl = connp->conn_idl;
13324 13324          ASSERT(MUTEX_HELD(&idl->idl_lock));
13325 13325  
13326 13326          if (!closing) {
13327 13327                  next_connp = connp->conn_drain_next;
13328 13328                  while (next_connp != connp) {
13329 13329                          conn_t *delconnp = next_connp;
13330 13330  
13331 13331                          next_connp = next_connp->conn_drain_next;
13332 13332                          conn_drain_remove(delconnp);
13333 13333                  }
13334 13334                  ASSERT(connp->conn_drain_next == idl->idl_conn);
13335 13335          }
13336 13336          conn_drain_remove(connp);
13337 13337  }
13338 13338  
13339 13339  /*
13340 13340   * Write service routine. Shared perimeter entry point.
13341 13341   * The device queue's messages has fallen below the low water mark and STREAMS
13342 13342   * has backenabled the ill_wq. Send sockfs notification about flow-control on
13343 13343   * each waiting conn.
13344 13344   */
13345 13345  int
13346 13346  ip_wsrv(queue_t *q)
13347 13347  {
13348 13348          ill_t   *ill;
13349 13349  
13350 13350          ill = (ill_t *)q->q_ptr;
13351 13351          if (ill->ill_state_flags == 0) {
13352 13352                  ip_stack_t *ipst = ill->ill_ipst;
13353 13353  
13354 13354                  /*
13355 13355                   * The device flow control has opened up.
13356 13356                   * Walk through conn drain lists and qenable the
13357 13357                   * first conn in each list. This makes sense only
13358 13358                   * if the stream is fully plumbed and setup.
13359 13359                   * Hence the ill_state_flags check above.
13360 13360                   */
13361 13361                  ip1dbg(("ip_wsrv: walking\n"));
13362 13362                  conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
13363 13363                  enableok(ill->ill_wq);
13364 13364          }
13365 13365          return (0);
13366 13366  }
13367 13367  
13368 13368  /*
13369 13369   * Callback to disable flow control in IP.
13370 13370   *
13371 13371   * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
13372 13372   * is enabled.
13373 13373   *
13374 13374   * When MAC_TX() is not able to send any more packets, dld sets its queue
13375 13375   * to QFULL and enable the STREAMS flow control. Later, when the underlying
13376 13376   * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13377 13377   * function and wakes up corresponding mac worker threads, which in turn
13378 13378   * calls this callback function, and disables flow control.
13379 13379   */
13380 13380  void
13381 13381  ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13382 13382  {
13383 13383          ill_t *ill = (ill_t *)arg;
13384 13384          ip_stack_t *ipst = ill->ill_ipst;
13385 13385          idl_tx_list_t *idl_txl;
13386 13386  
13387 13387          idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13388 13388          mutex_enter(&idl_txl->txl_lock);
13389 13389          /* add code to to set a flag to indicate idl_txl is enabled */
13390 13390          conn_walk_drain(ipst, idl_txl);
13391 13391          mutex_exit(&idl_txl->txl_lock);
13392 13392  }
13393 13393  
13394 13394  /*
13395 13395   * Flow control has been relieved and STREAMS has backenabled us; drain
13396 13396   * all the conn lists on `tx_list'.
13397 13397   */
13398 13398  static void
13399 13399  conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13400 13400  {
13401 13401          int i;
13402 13402          idl_t *idl;
13403 13403  
13404 13404          IP_STAT(ipst, ip_conn_walk_drain);
13405 13405  
13406 13406          for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13407 13407                  idl = &tx_list->txl_drain_list[i];
13408 13408                  mutex_enter(&idl->idl_lock);
13409 13409                  conn_drain(idl->idl_conn, B_FALSE);
13410 13410                  mutex_exit(&idl->idl_lock);
13411 13411          }
13412 13412  }
13413 13413  
13414 13414  /*
13415 13415   * Determine if the ill and multicast aspects of that packets
13416 13416   * "matches" the conn.
13417 13417   */
13418 13418  boolean_t
13419 13419  conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13420 13420  {
13421 13421          ill_t           *ill = ira->ira_rill;
13422 13422          zoneid_t        zoneid = ira->ira_zoneid;
13423 13423          uint_t          in_ifindex;
13424 13424          ipaddr_t        dst, src;
13425 13425  
13426 13426          dst = ipha->ipha_dst;
13427 13427          src = ipha->ipha_src;
13428 13428  
13429 13429          /*
13430 13430           * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13431 13431           * unicast, broadcast and multicast reception to
13432 13432           * conn_incoming_ifindex.
13433 13433           * conn_wantpacket is called for unicast, broadcast and
13434 13434           * multicast packets.
13435 13435           */
13436 13436          in_ifindex = connp->conn_incoming_ifindex;
13437 13437  
13438 13438          /* mpathd can bind to the under IPMP interface, which we allow */
13439 13439          if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13440 13440                  if (!IS_UNDER_IPMP(ill))
13441 13441                          return (B_FALSE);
13442 13442  
13443 13443                  if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13444 13444                          return (B_FALSE);
13445 13445          }
13446 13446  
13447 13447          if (!IPCL_ZONE_MATCH(connp, zoneid))
13448 13448                  return (B_FALSE);
13449 13449  
13450 13450          if (!(ira->ira_flags & IRAF_MULTICAST))
13451 13451                  return (B_TRUE);
13452 13452  
13453 13453          if (connp->conn_multi_router) {
13454 13454                  /* multicast packet and multicast router socket: send up */
13455 13455                  return (B_TRUE);
13456 13456          }
13457 13457  
13458 13458          if (ipha->ipha_protocol == IPPROTO_PIM ||
13459 13459              ipha->ipha_protocol == IPPROTO_RSVP)
13460 13460                  return (B_TRUE);
13461 13461  
13462 13462          return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13463 13463  }
13464 13464  
13465 13465  void
13466 13466  conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13467 13467  {
13468 13468          if (IPCL_IS_NONSTR(connp)) {
13469 13469                  (*connp->conn_upcalls->su_txq_full)
13470 13470                      (connp->conn_upper_handle, B_TRUE);
13471 13471                  if (flow_stopped != NULL)
13472 13472                          *flow_stopped = B_TRUE;
13473 13473          } else {
13474 13474                  queue_t *q = connp->conn_wq;
13475 13475  
13476 13476                  ASSERT(q != NULL);
13477 13477                  if (!(q->q_flag & QFULL)) {
13478 13478                          mutex_enter(QLOCK(q));
13479 13479                          if (!(q->q_flag & QFULL)) {
13480 13480                                  /* still need to set QFULL */
13481 13481                                  q->q_flag |= QFULL;
13482 13482                                  /* set flow_stopped to true under QLOCK */
13483 13483                                  if (flow_stopped != NULL)
13484 13484                                          *flow_stopped = B_TRUE;
13485 13485                                  mutex_exit(QLOCK(q));
13486 13486                          } else {
13487 13487                                  /* flow_stopped is left unchanged */
13488 13488                                  mutex_exit(QLOCK(q));
13489 13489                          }
13490 13490                  }
13491 13491          }
13492 13492  }
13493 13493  
13494 13494  void
13495 13495  conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13496 13496  {
13497 13497          if (IPCL_IS_NONSTR(connp)) {
13498 13498                  (*connp->conn_upcalls->su_txq_full)
13499 13499                      (connp->conn_upper_handle, B_FALSE);
13500 13500                  if (flow_stopped != NULL)
13501 13501                          *flow_stopped = B_FALSE;
13502 13502          } else {
13503 13503                  queue_t *q = connp->conn_wq;
13504 13504  
13505 13505                  ASSERT(q != NULL);
13506 13506                  if (q->q_flag & QFULL) {
13507 13507                          mutex_enter(QLOCK(q));
13508 13508                          if (q->q_flag & QFULL) {
13509 13509                                  q->q_flag &= ~QFULL;
13510 13510                                  /* set flow_stopped to false under QLOCK */
13511 13511                                  if (flow_stopped != NULL)
13512 13512                                          *flow_stopped = B_FALSE;
13513 13513                                  mutex_exit(QLOCK(q));
13514 13514                                  if (q->q_flag & QWANTW)
13515 13515                                          qbackenable(q, 0);
13516 13516                          } else {
13517 13517                                  /* flow_stopped is left unchanged */
13518 13518                                  mutex_exit(QLOCK(q));
13519 13519                          }
13520 13520                  }
13521 13521          }
13522 13522  
13523 13523          mutex_enter(&connp->conn_lock);
13524 13524          connp->conn_blocked = B_FALSE;
13525 13525          mutex_exit(&connp->conn_lock);
13526 13526  }
13527 13527  
13528 13528  /*
13529 13529   * Return the length in bytes of the IPv4 headers (base header, label, and
13530 13530   * other IP options) that will be needed based on the
13531 13531   * ip_pkt_t structure passed by the caller.
13532 13532   *
13533 13533   * The returned length does not include the length of the upper level
13534 13534   * protocol (ULP) header.
13535 13535   * The caller needs to check that the length doesn't exceed the max for IPv4.
13536 13536   */
13537 13537  int
13538 13538  ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13539 13539  {
13540 13540          int len;
13541 13541  
13542 13542          len = IP_SIMPLE_HDR_LENGTH;
13543 13543          if (ipp->ipp_fields & IPPF_LABEL_V4) {
13544 13544                  ASSERT(ipp->ipp_label_len_v4 != 0);
13545 13545                  /* We need to round up here */
13546 13546                  len += (ipp->ipp_label_len_v4 + 3) & ~3;
13547 13547          }
13548 13548  
13549 13549          if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13550 13550                  ASSERT(ipp->ipp_ipv4_options_len != 0);
13551 13551                  ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13552 13552                  len += ipp->ipp_ipv4_options_len;
13553 13553          }
13554 13554          return (len);
13555 13555  }
13556 13556  
13557 13557  /*
13558 13558   * All-purpose routine to build an IPv4 header with options based
13559 13559   * on the abstract ip_pkt_t.
13560 13560   *
13561 13561   * The caller has to set the source and destination address as well as
13562 13562   * ipha_length. The caller has to massage any source route and compensate
13563 13563   * for the ULP pseudo-header checksum due to the source route.
13564 13564   */
13565 13565  void
13566 13566  ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13567 13567      uint8_t protocol)
13568 13568  {
13569 13569          ipha_t  *ipha = (ipha_t *)buf;
13570 13570          uint8_t *cp;
13571 13571  
13572 13572          /* Initialize IPv4 header */
13573 13573          ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13574 13574          ipha->ipha_length = 0;  /* Caller will set later */
13575 13575          ipha->ipha_ident = 0;
13576 13576          ipha->ipha_fragment_offset_and_flags = 0;
13577 13577          ipha->ipha_ttl = ipp->ipp_unicast_hops;
13578 13578          ipha->ipha_protocol = protocol;
13579 13579          ipha->ipha_hdr_checksum = 0;
13580 13580  
13581 13581          if ((ipp->ipp_fields & IPPF_ADDR) &&
13582 13582              IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13583 13583                  ipha->ipha_src = ipp->ipp_addr_v4;
13584 13584  
13585 13585          cp = (uint8_t *)&ipha[1];
13586 13586          if (ipp->ipp_fields & IPPF_LABEL_V4) {
13587 13587                  ASSERT(ipp->ipp_label_len_v4 != 0);
13588 13588                  bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4);
13589 13589                  cp += ipp->ipp_label_len_v4;
13590 13590                  /* We need to round up here */
13591 13591                  while ((uintptr_t)cp & 0x3) {
13592 13592                          *cp++ = IPOPT_NOP;
13593 13593                  }
13594 13594          }
13595 13595  
13596 13596          if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13597 13597                  ASSERT(ipp->ipp_ipv4_options_len != 0);
13598 13598                  ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13599 13599                  bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13600 13600                  cp += ipp->ipp_ipv4_options_len;
13601 13601          }
13602 13602          ipha->ipha_version_and_hdr_length =
13603 13603              (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13604 13604  
13605 13605          ASSERT((int)(cp - buf) == buf_len);
13606 13606  }
13607 13607  
13608 13608  /* Allocate the private structure */
13609 13609  static int
13610 13610  ip_priv_alloc(void **bufp)
13611 13611  {
13612 13612          void    *buf;
13613 13613  
13614 13614          if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13615 13615                  return (ENOMEM);
13616 13616  
13617 13617          *bufp = buf;
13618 13618          return (0);
13619 13619  }
13620 13620  
13621 13621  /* Function to delete the private structure */
13622 13622  void
13623 13623  ip_priv_free(void *buf)
13624 13624  {
13625 13625          ASSERT(buf != NULL);
13626 13626          kmem_free(buf, sizeof (ip_priv_t));
13627 13627  }
13628 13628  
13629 13629  /*
13630 13630   * The entry point for IPPF processing.
13631 13631   * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13632 13632   * routine just returns.
13633 13633   *
13634 13634   * When called, ip_process generates an ipp_packet_t structure
13635 13635   * which holds the state information for this packet and invokes the
13636 13636   * the classifier (via ipp_packet_process). The classification, depending on
13637 13637   * configured filters, results in a list of actions for this packet. Invoking
13638 13638   * an action may cause the packet to be dropped, in which case we return NULL.
13639 13639   * proc indicates the callout position for
13640 13640   * this packet and ill is the interface this packet arrived on or will leave
13641 13641   * on (inbound and outbound resp.).
13642 13642   *
13643 13643   * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13644 13644   * on the ill corrsponding to the destination IP address.
13645 13645   */
13646 13646  mblk_t *
13647 13647  ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13648 13648  {
13649 13649          ip_priv_t       *priv;
13650 13650          ipp_action_id_t aid;
13651 13651          int             rc = 0;
13652 13652          ipp_packet_t    *pp;
13653 13653  
13654 13654          /* If the classifier is not loaded, return  */
13655 13655          if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13656 13656                  return (mp);
13657 13657          }
13658 13658  
13659 13659          ASSERT(mp != NULL);
13660 13660  
13661 13661          /* Allocate the packet structure */
13662 13662          rc = ipp_packet_alloc(&pp, "ip", aid);
13663 13663          if (rc != 0)
13664 13664                  goto drop;
13665 13665  
13666 13666          /* Allocate the private structure */
13667 13667          rc = ip_priv_alloc((void **)&priv);
13668 13668          if (rc != 0) {
13669 13669                  ipp_packet_free(pp);
13670 13670                  goto drop;
13671 13671          }
13672 13672          priv->proc = proc;
13673 13673          priv->ill_index = ill_get_upper_ifindex(rill);
13674 13674  
13675 13675          ipp_packet_set_private(pp, priv, ip_priv_free);
13676 13676          ipp_packet_set_data(pp, mp);
13677 13677  
13678 13678          /* Invoke the classifier */
13679 13679          rc = ipp_packet_process(&pp);
13680 13680          if (pp != NULL) {
13681 13681                  mp = ipp_packet_get_data(pp);
13682 13682                  ipp_packet_free(pp);
13683 13683                  if (rc != 0)
13684 13684                          goto drop;
13685 13685                  return (mp);
13686 13686          } else {
13687 13687                  /* No mp to trace in ip_drop_input/ip_drop_output  */
13688 13688                  mp = NULL;
13689 13689          }
13690 13690  drop:
13691 13691          if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13692 13692                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13693 13693                  ip_drop_input("ip_process", mp, ill);
13694 13694          } else {
13695 13695                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13696 13696                  ip_drop_output("ip_process", mp, ill);
13697 13697          }
13698 13698          freemsg(mp);
13699 13699          return (NULL);
13700 13700  }
13701 13701  
13702 13702  /*
13703 13703   * Propagate a multicast group membership operation (add/drop) on
13704 13704   * all the interfaces crossed by the related multirt routes.
13705 13705   * The call is considered successful if the operation succeeds
13706 13706   * on at least one interface.
13707 13707   *
13708 13708   * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the
13709 13709   * multicast addresses with the ire argument being the first one.
13710 13710   * We walk the bucket to find all the of those.
13711 13711   *
13712 13712   * Common to IPv4 and IPv6.
13713 13713   */
13714 13714  static int
13715 13715  ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
13716 13716      const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
13717 13717      ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group,
13718 13718      mcast_record_t fmode, const in6_addr_t *v6src)
13719 13719  {
13720 13720          ire_t           *ire_gw;
13721 13721          irb_t           *irb;
13722 13722          int             ifindex;
13723 13723          int             error = 0;
13724 13724          int             result;
13725 13725          ip_stack_t      *ipst = ire->ire_ipst;
13726 13726          ipaddr_t        group;
13727 13727          boolean_t       isv6;
13728 13728          int             match_flags;
13729 13729  
13730 13730          if (IN6_IS_ADDR_V4MAPPED(v6group)) {
13731 13731                  IN6_V4MAPPED_TO_IPADDR(v6group, group);
13732 13732                  isv6 = B_FALSE;
13733 13733          } else {
13734 13734                  isv6 = B_TRUE;
13735 13735          }
13736 13736  
13737 13737          irb = ire->ire_bucket;
13738 13738          ASSERT(irb != NULL);
13739 13739  
13740 13740          result = 0;
13741 13741          irb_refhold(irb);
13742 13742          for (; ire != NULL; ire = ire->ire_next) {
13743 13743                  if ((ire->ire_flags & RTF_MULTIRT) == 0)
13744 13744                          continue;
13745 13745  
13746 13746                  /* We handle -ifp routes by matching on the ill if set */
13747 13747                  match_flags = MATCH_IRE_TYPE;
13748 13748                  if (ire->ire_ill != NULL)
13749 13749                          match_flags |= MATCH_IRE_ILL;
13750 13750  
13751 13751                  if (isv6) {
13752 13752                          if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group))
13753 13753                                  continue;
13754 13754  
13755 13755                          ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6,
13756 13756                              0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13757 13757                              match_flags, 0, ipst, NULL);
13758 13758                  } else {
13759 13759                          if (ire->ire_addr != group)
13760 13760                                  continue;
13761 13761  
13762 13762                          ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr,
13763 13763                              0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13764 13764                              match_flags, 0, ipst, NULL);
13765 13765                  }
13766 13766                  /* No interface route exists for the gateway; skip this ire. */
13767 13767                  if (ire_gw == NULL)
13768 13768                          continue;
13769 13769                  if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
13770 13770                          ire_refrele(ire_gw);
13771 13771                          continue;
13772 13772                  }
13773 13773                  ASSERT(ire_gw->ire_ill != NULL);        /* IRE_INTERFACE */
13774 13774                  ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex;
13775 13775  
13776 13776                  /*
13777 13777                   * The operation is considered a success if
13778 13778                   * it succeeds at least once on any one interface.
13779 13779                   */
13780 13780                  error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex,
13781 13781                      fmode, v6src);
13782 13782                  if (error == 0)
13783 13783                          result = CGTP_MCAST_SUCCESS;
13784 13784  
13785 13785                  ire_refrele(ire_gw);
13786 13786          }
13787 13787          irb_refrele(irb);
13788 13788          /*
13789 13789           * Consider the call as successful if we succeeded on at least
13790 13790           * one interface. Otherwise, return the last encountered error.
13791 13791           */
13792 13792          return (result == CGTP_MCAST_SUCCESS ? 0 : error);
13793 13793  }
13794 13794  
13795 13795  /*
13796 13796   * Return the expected CGTP hooks version number.
13797 13797   */
13798 13798  int
13799 13799  ip_cgtp_filter_supported(void)
13800 13800  {
13801 13801          return (ip_cgtp_filter_rev);
13802 13802  }
13803 13803  
13804 13804  /*
13805 13805   * CGTP hooks can be registered by invoking this function.
13806 13806   * Checks that the version number matches.
13807 13807   */
13808 13808  int
13809 13809  ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops)
13810 13810  {
13811 13811          netstack_t *ns;
13812 13812          ip_stack_t *ipst;
13813 13813  
13814 13814          if (ops->cfo_filter_rev != CGTP_FILTER_REV)
13815 13815                  return (ENOTSUP);
13816 13816  
13817 13817          ns = netstack_find_by_stackid(stackid);
13818 13818          if (ns == NULL)
13819 13819                  return (EINVAL);
13820 13820          ipst = ns->netstack_ip;
13821 13821          ASSERT(ipst != NULL);
13822 13822  
13823 13823          if (ipst->ips_ip_cgtp_filter_ops != NULL) {
13824 13824                  netstack_rele(ns);
13825 13825                  return (EALREADY);
13826 13826          }
13827 13827  
13828 13828          ipst->ips_ip_cgtp_filter_ops = ops;
13829 13829  
13830 13830          ill_set_inputfn_all(ipst);
13831 13831  
13832 13832          netstack_rele(ns);
13833 13833          return (0);
13834 13834  }
13835 13835  
13836 13836  /*
13837 13837   * CGTP hooks can be unregistered by invoking this function.
13838 13838   * Returns ENXIO if there was no registration.
13839 13839   * Returns EBUSY if the ndd variable has not been turned off.
13840 13840   */
13841 13841  int
13842 13842  ip_cgtp_filter_unregister(netstackid_t stackid)
13843 13843  {
13844 13844          netstack_t *ns;
13845 13845          ip_stack_t *ipst;
13846 13846  
13847 13847          ns = netstack_find_by_stackid(stackid);
13848 13848          if (ns == NULL)
13849 13849                  return (EINVAL);
13850 13850          ipst = ns->netstack_ip;
13851 13851          ASSERT(ipst != NULL);
13852 13852  
13853 13853          if (ipst->ips_ip_cgtp_filter) {
13854 13854                  netstack_rele(ns);
13855 13855                  return (EBUSY);
13856 13856          }
13857 13857  
13858 13858          if (ipst->ips_ip_cgtp_filter_ops == NULL) {
13859 13859                  netstack_rele(ns);
13860 13860                  return (ENXIO);
13861 13861          }
13862 13862          ipst->ips_ip_cgtp_filter_ops = NULL;
13863 13863  
13864 13864          ill_set_inputfn_all(ipst);
13865 13865  
13866 13866          netstack_rele(ns);
13867 13867          return (0);
13868 13868  }
13869 13869  
13870 13870  /*
13871 13871   * Check whether there is a CGTP filter registration.
13872 13872   * Returns non-zero if there is a registration, otherwise returns zero.
13873 13873   * Note: returns zero if bad stackid.
13874 13874   */
13875 13875  int
13876 13876  ip_cgtp_filter_is_registered(netstackid_t stackid)
13877 13877  {
13878 13878          netstack_t *ns;
13879 13879          ip_stack_t *ipst;
13880 13880          int ret;
13881 13881  
13882 13882          ns = netstack_find_by_stackid(stackid);
13883 13883          if (ns == NULL)
13884 13884                  return (0);
13885 13885          ipst = ns->netstack_ip;
13886 13886          ASSERT(ipst != NULL);
13887 13887  
13888 13888          if (ipst->ips_ip_cgtp_filter_ops != NULL)
13889 13889                  ret = 1;
13890 13890          else
13891 13891                  ret = 0;
13892 13892  
13893 13893          netstack_rele(ns);
13894 13894          return (ret);
13895 13895  }
13896 13896  
13897 13897  static int
13898 13898  ip_squeue_switch(int val)
13899 13899  {
13900 13900          int rval;
13901 13901  
13902 13902          switch (val) {
13903 13903          case IP_SQUEUE_ENTER_NODRAIN:
13904 13904                  rval = SQ_NODRAIN;
13905 13905                  break;
13906 13906          case IP_SQUEUE_ENTER:
13907 13907                  rval = SQ_PROCESS;
13908 13908                  break;
13909 13909          case IP_SQUEUE_FILL:
13910 13910          default:
13911 13911                  rval = SQ_FILL;
13912 13912                  break;
13913 13913          }
13914 13914          return (rval);
13915 13915  }
13916 13916  
13917 13917  static void *
13918 13918  ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13919 13919  {
13920 13920          kstat_t *ksp;
13921 13921  
13922 13922          ip_stat_t template = {
13923 13923                  { "ip_udp_fannorm",             KSTAT_DATA_UINT64 },
13924 13924                  { "ip_udp_fanmb",               KSTAT_DATA_UINT64 },
13925 13925                  { "ip_recv_pullup",             KSTAT_DATA_UINT64 },
13926 13926                  { "ip_db_ref",                  KSTAT_DATA_UINT64 },
13927 13927                  { "ip_notaligned",              KSTAT_DATA_UINT64 },
13928 13928                  { "ip_multimblk",               KSTAT_DATA_UINT64 },
13929 13929                  { "ip_opt",                     KSTAT_DATA_UINT64 },
  
    | 
      ↓ open down ↓ | 
    13892 lines elided | 
    
      ↑ open up ↑ | 
  
13930 13930                  { "ipsec_proto_ahesp",          KSTAT_DATA_UINT64 },
13931 13931                  { "ip_conn_flputbq",            KSTAT_DATA_UINT64 },
13932 13932                  { "ip_conn_walk_drain",         KSTAT_DATA_UINT64 },
13933 13933                  { "ip_out_sw_cksum",            KSTAT_DATA_UINT64 },
13934 13934                  { "ip_out_sw_cksum_bytes",      KSTAT_DATA_UINT64 },
13935 13935                  { "ip_in_sw_cksum",             KSTAT_DATA_UINT64 },
13936 13936                  { "ip_ire_reclaim_calls",       KSTAT_DATA_UINT64 },
13937 13937                  { "ip_ire_reclaim_deleted",     KSTAT_DATA_UINT64 },
13938 13938                  { "ip_nce_reclaim_calls",       KSTAT_DATA_UINT64 },
13939 13939                  { "ip_nce_reclaim_deleted",     KSTAT_DATA_UINT64 },
     13940 +                { "ip_nce_mcast_reclaim_calls", KSTAT_DATA_UINT64 },
     13941 +                { "ip_nce_mcast_reclaim_deleted",       KSTAT_DATA_UINT64 },
     13942 +                { "ip_nce_mcast_reclaim_tqfail",        KSTAT_DATA_UINT64 },
13940 13943                  { "ip_dce_reclaim_calls",       KSTAT_DATA_UINT64 },
13941 13944                  { "ip_dce_reclaim_deleted",     KSTAT_DATA_UINT64 },
13942 13945                  { "ip_tcp_in_full_hw_cksum_err",        KSTAT_DATA_UINT64 },
13943 13946                  { "ip_tcp_in_part_hw_cksum_err",        KSTAT_DATA_UINT64 },
13944 13947                  { "ip_tcp_in_sw_cksum_err",             KSTAT_DATA_UINT64 },
13945 13948                  { "ip_udp_in_full_hw_cksum_err",        KSTAT_DATA_UINT64 },
13946 13949                  { "ip_udp_in_part_hw_cksum_err",        KSTAT_DATA_UINT64 },
13947 13950                  { "ip_udp_in_sw_cksum_err",     KSTAT_DATA_UINT64 },
13948 13951                  { "conn_in_recvdstaddr",        KSTAT_DATA_UINT64 },
13949 13952                  { "conn_in_recvopts",           KSTAT_DATA_UINT64 },
13950 13953                  { "conn_in_recvif",             KSTAT_DATA_UINT64 },
13951 13954                  { "conn_in_recvslla",           KSTAT_DATA_UINT64 },
13952 13955                  { "conn_in_recvucred",          KSTAT_DATA_UINT64 },
13953 13956                  { "conn_in_recvttl",            KSTAT_DATA_UINT64 },
13954 13957                  { "conn_in_recvhopopts",        KSTAT_DATA_UINT64 },
13955 13958                  { "conn_in_recvhoplimit",       KSTAT_DATA_UINT64 },
13956 13959                  { "conn_in_recvdstopts",        KSTAT_DATA_UINT64 },
13957 13960                  { "conn_in_recvrthdrdstopts",   KSTAT_DATA_UINT64 },
13958 13961                  { "conn_in_recvrthdr",          KSTAT_DATA_UINT64 },
13959 13962                  { "conn_in_recvpktinfo",        KSTAT_DATA_UINT64 },
13960 13963                  { "conn_in_recvtclass",         KSTAT_DATA_UINT64 },
13961 13964                  { "conn_in_timestamp",          KSTAT_DATA_UINT64 },
13962 13965          };
13963 13966  
13964 13967          ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13965 13968              KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13966 13969              KSTAT_FLAG_VIRTUAL, stackid);
13967 13970  
13968 13971          if (ksp == NULL)
13969 13972                  return (NULL);
13970 13973  
13971 13974          bcopy(&template, ip_statisticsp, sizeof (template));
13972 13975          ksp->ks_data = (void *)ip_statisticsp;
13973 13976          ksp->ks_private = (void *)(uintptr_t)stackid;
13974 13977  
13975 13978          kstat_install(ksp);
13976 13979          return (ksp);
13977 13980  }
13978 13981  
13979 13982  static void
13980 13983  ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13981 13984  {
13982 13985          if (ksp != NULL) {
13983 13986                  ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13984 13987                  kstat_delete_netstack(ksp, stackid);
13985 13988          }
13986 13989  }
13987 13990  
13988 13991  static void *
13989 13992  ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
13990 13993  {
13991 13994          kstat_t *ksp;
13992 13995  
13993 13996          ip_named_kstat_t template = {
13994 13997                  { "forwarding",         KSTAT_DATA_UINT32, 0 },
13995 13998                  { "defaultTTL",         KSTAT_DATA_UINT32, 0 },
13996 13999                  { "inReceives",         KSTAT_DATA_UINT64, 0 },
13997 14000                  { "inHdrErrors",        KSTAT_DATA_UINT32, 0 },
13998 14001                  { "inAddrErrors",       KSTAT_DATA_UINT32, 0 },
13999 14002                  { "forwDatagrams",      KSTAT_DATA_UINT64, 0 },
14000 14003                  { "inUnknownProtos",    KSTAT_DATA_UINT32, 0 },
14001 14004                  { "inDiscards",         KSTAT_DATA_UINT32, 0 },
14002 14005                  { "inDelivers",         KSTAT_DATA_UINT64, 0 },
14003 14006                  { "outRequests",        KSTAT_DATA_UINT64, 0 },
14004 14007                  { "outDiscards",        KSTAT_DATA_UINT32, 0 },
14005 14008                  { "outNoRoutes",        KSTAT_DATA_UINT32, 0 },
14006 14009                  { "reasmTimeout",       KSTAT_DATA_UINT32, 0 },
14007 14010                  { "reasmReqds",         KSTAT_DATA_UINT32, 0 },
14008 14011                  { "reasmOKs",           KSTAT_DATA_UINT32, 0 },
14009 14012                  { "reasmFails",         KSTAT_DATA_UINT32, 0 },
14010 14013                  { "fragOKs",            KSTAT_DATA_UINT32, 0 },
14011 14014                  { "fragFails",          KSTAT_DATA_UINT32, 0 },
14012 14015                  { "fragCreates",        KSTAT_DATA_UINT32, 0 },
14013 14016                  { "addrEntrySize",      KSTAT_DATA_INT32, 0 },
14014 14017                  { "routeEntrySize",     KSTAT_DATA_INT32, 0 },
14015 14018                  { "netToMediaEntrySize",        KSTAT_DATA_INT32, 0 },
14016 14019                  { "routingDiscards",    KSTAT_DATA_UINT32, 0 },
14017 14020                  { "inErrs",             KSTAT_DATA_UINT32, 0 },
14018 14021                  { "noPorts",            KSTAT_DATA_UINT32, 0 },
14019 14022                  { "inCksumErrs",        KSTAT_DATA_UINT32, 0 },
14020 14023                  { "reasmDuplicates",    KSTAT_DATA_UINT32, 0 },
14021 14024                  { "reasmPartDups",      KSTAT_DATA_UINT32, 0 },
14022 14025                  { "forwProhibits",      KSTAT_DATA_UINT32, 0 },
14023 14026                  { "udpInCksumErrs",     KSTAT_DATA_UINT32, 0 },
14024 14027                  { "udpInOverflows",     KSTAT_DATA_UINT32, 0 },
14025 14028                  { "rawipInOverflows",   KSTAT_DATA_UINT32, 0 },
14026 14029                  { "ipsecInSucceeded",   KSTAT_DATA_UINT32, 0 },
14027 14030                  { "ipsecInFailed",      KSTAT_DATA_INT32, 0 },
14028 14031                  { "memberEntrySize",    KSTAT_DATA_INT32, 0 },
14029 14032                  { "inIPv6",             KSTAT_DATA_UINT32, 0 },
14030 14033                  { "outIPv6",            KSTAT_DATA_UINT32, 0 },
14031 14034                  { "outSwitchIPv6",      KSTAT_DATA_UINT32, 0 },
14032 14035          };
14033 14036  
14034 14037          ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
14035 14038              NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
14036 14039          if (ksp == NULL || ksp->ks_data == NULL)
14037 14040                  return (NULL);
14038 14041  
14039 14042          template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
14040 14043          template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
14041 14044          template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14042 14045          template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
14043 14046          template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
14044 14047  
14045 14048          template.netToMediaEntrySize.value.i32 =
14046 14049              sizeof (mib2_ipNetToMediaEntry_t);
14047 14050  
14048 14051          template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
14049 14052  
14050 14053          bcopy(&template, ksp->ks_data, sizeof (template));
14051 14054          ksp->ks_update = ip_kstat_update;
14052 14055          ksp->ks_private = (void *)(uintptr_t)stackid;
14053 14056  
14054 14057          kstat_install(ksp);
14055 14058          return (ksp);
14056 14059  }
14057 14060  
14058 14061  static void
14059 14062  ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14060 14063  {
14061 14064          if (ksp != NULL) {
14062 14065                  ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14063 14066                  kstat_delete_netstack(ksp, stackid);
14064 14067          }
14065 14068  }
14066 14069  
14067 14070  static int
14068 14071  ip_kstat_update(kstat_t *kp, int rw)
14069 14072  {
14070 14073          ip_named_kstat_t *ipkp;
14071 14074          mib2_ipIfStatsEntry_t ipmib;
14072 14075          ill_walk_context_t ctx;
14073 14076          ill_t *ill;
14074 14077          netstackid_t    stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14075 14078          netstack_t      *ns;
14076 14079          ip_stack_t      *ipst;
14077 14080  
14078 14081          if (kp->ks_data == NULL)
14079 14082                  return (EIO);
14080 14083  
14081 14084          if (rw == KSTAT_WRITE)
14082 14085                  return (EACCES);
14083 14086  
14084 14087          ns = netstack_find_by_stackid(stackid);
14085 14088          if (ns == NULL)
14086 14089                  return (-1);
14087 14090          ipst = ns->netstack_ip;
14088 14091          if (ipst == NULL) {
14089 14092                  netstack_rele(ns);
14090 14093                  return (-1);
14091 14094          }
14092 14095          ipkp = (ip_named_kstat_t *)kp->ks_data;
14093 14096  
14094 14097          bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
14095 14098          rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14096 14099          ill = ILL_START_WALK_V4(&ctx, ipst);
14097 14100          for (; ill != NULL; ill = ill_next(&ctx, ill))
14098 14101                  ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
14099 14102          rw_exit(&ipst->ips_ill_g_lock);
14100 14103  
14101 14104          ipkp->forwarding.value.ui32 =           ipmib.ipIfStatsForwarding;
14102 14105          ipkp->defaultTTL.value.ui32 =           ipmib.ipIfStatsDefaultTTL;
14103 14106          ipkp->inReceives.value.ui64 =           ipmib.ipIfStatsHCInReceives;
14104 14107          ipkp->inHdrErrors.value.ui32 =          ipmib.ipIfStatsInHdrErrors;
14105 14108          ipkp->inAddrErrors.value.ui32 =         ipmib.ipIfStatsInAddrErrors;
14106 14109          ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
14107 14110          ipkp->inUnknownProtos.value.ui32 =      ipmib.ipIfStatsInUnknownProtos;
14108 14111          ipkp->inDiscards.value.ui32 =           ipmib.ipIfStatsInDiscards;
14109 14112          ipkp->inDelivers.value.ui64 =           ipmib.ipIfStatsHCInDelivers;
14110 14113          ipkp->outRequests.value.ui64 =          ipmib.ipIfStatsHCOutRequests;
14111 14114          ipkp->outDiscards.value.ui32 =          ipmib.ipIfStatsOutDiscards;
14112 14115          ipkp->outNoRoutes.value.ui32 =          ipmib.ipIfStatsOutNoRoutes;
14113 14116          ipkp->reasmTimeout.value.ui32 =         ipst->ips_ip_reassembly_timeout;
14114 14117          ipkp->reasmReqds.value.ui32 =           ipmib.ipIfStatsReasmReqds;
14115 14118          ipkp->reasmOKs.value.ui32 =             ipmib.ipIfStatsReasmOKs;
14116 14119          ipkp->reasmFails.value.ui32 =           ipmib.ipIfStatsReasmFails;
14117 14120          ipkp->fragOKs.value.ui32 =              ipmib.ipIfStatsOutFragOKs;
14118 14121          ipkp->fragFails.value.ui32 =            ipmib.ipIfStatsOutFragFails;
14119 14122          ipkp->fragCreates.value.ui32 =          ipmib.ipIfStatsOutFragCreates;
14120 14123  
14121 14124          ipkp->routingDiscards.value.ui32 =      0;
14122 14125          ipkp->inErrs.value.ui32 =               ipmib.tcpIfStatsInErrs;
14123 14126          ipkp->noPorts.value.ui32 =              ipmib.udpIfStatsNoPorts;
14124 14127          ipkp->inCksumErrs.value.ui32 =          ipmib.ipIfStatsInCksumErrs;
14125 14128          ipkp->reasmDuplicates.value.ui32 =      ipmib.ipIfStatsReasmDuplicates;
14126 14129          ipkp->reasmPartDups.value.ui32 =        ipmib.ipIfStatsReasmPartDups;
14127 14130          ipkp->forwProhibits.value.ui32 =        ipmib.ipIfStatsForwProhibits;
14128 14131          ipkp->udpInCksumErrs.value.ui32 =       ipmib.udpIfStatsInCksumErrs;
14129 14132          ipkp->udpInOverflows.value.ui32 =       ipmib.udpIfStatsInOverflows;
14130 14133          ipkp->rawipInOverflows.value.ui32 =     ipmib.rawipIfStatsInOverflows;
14131 14134          ipkp->ipsecInSucceeded.value.ui32 =     ipmib.ipsecIfStatsInSucceeded;
14132 14135          ipkp->ipsecInFailed.value.i32 =         ipmib.ipsecIfStatsInFailed;
14133 14136  
14134 14137          ipkp->inIPv6.value.ui32 =       ipmib.ipIfStatsInWrongIPVersion;
14135 14138          ipkp->outIPv6.value.ui32 =      ipmib.ipIfStatsOutWrongIPVersion;
14136 14139          ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
14137 14140  
14138 14141          netstack_rele(ns);
14139 14142  
14140 14143          return (0);
14141 14144  }
14142 14145  
14143 14146  static void *
14144 14147  icmp_kstat_init(netstackid_t stackid)
14145 14148  {
14146 14149          kstat_t *ksp;
14147 14150  
14148 14151          icmp_named_kstat_t template = {
14149 14152                  { "inMsgs",             KSTAT_DATA_UINT32 },
14150 14153                  { "inErrors",           KSTAT_DATA_UINT32 },
14151 14154                  { "inDestUnreachs",     KSTAT_DATA_UINT32 },
14152 14155                  { "inTimeExcds",        KSTAT_DATA_UINT32 },
14153 14156                  { "inParmProbs",        KSTAT_DATA_UINT32 },
14154 14157                  { "inSrcQuenchs",       KSTAT_DATA_UINT32 },
14155 14158                  { "inRedirects",        KSTAT_DATA_UINT32 },
14156 14159                  { "inEchos",            KSTAT_DATA_UINT32 },
14157 14160                  { "inEchoReps",         KSTAT_DATA_UINT32 },
14158 14161                  { "inTimestamps",       KSTAT_DATA_UINT32 },
14159 14162                  { "inTimestampReps",    KSTAT_DATA_UINT32 },
14160 14163                  { "inAddrMasks",        KSTAT_DATA_UINT32 },
14161 14164                  { "inAddrMaskReps",     KSTAT_DATA_UINT32 },
14162 14165                  { "outMsgs",            KSTAT_DATA_UINT32 },
14163 14166                  { "outErrors",          KSTAT_DATA_UINT32 },
14164 14167                  { "outDestUnreachs",    KSTAT_DATA_UINT32 },
14165 14168                  { "outTimeExcds",       KSTAT_DATA_UINT32 },
14166 14169                  { "outParmProbs",       KSTAT_DATA_UINT32 },
14167 14170                  { "outSrcQuenchs",      KSTAT_DATA_UINT32 },
14168 14171                  { "outRedirects",       KSTAT_DATA_UINT32 },
14169 14172                  { "outEchos",           KSTAT_DATA_UINT32 },
14170 14173                  { "outEchoReps",        KSTAT_DATA_UINT32 },
14171 14174                  { "outTimestamps",      KSTAT_DATA_UINT32 },
14172 14175                  { "outTimestampReps",   KSTAT_DATA_UINT32 },
14173 14176                  { "outAddrMasks",       KSTAT_DATA_UINT32 },
14174 14177                  { "outAddrMaskReps",    KSTAT_DATA_UINT32 },
14175 14178                  { "inChksumErrs",       KSTAT_DATA_UINT32 },
14176 14179                  { "inUnknowns",         KSTAT_DATA_UINT32 },
14177 14180                  { "inFragNeeded",       KSTAT_DATA_UINT32 },
14178 14181                  { "outFragNeeded",      KSTAT_DATA_UINT32 },
14179 14182                  { "outDrops",           KSTAT_DATA_UINT32 },
14180 14183                  { "inOverFlows",        KSTAT_DATA_UINT32 },
14181 14184                  { "inBadRedirects",     KSTAT_DATA_UINT32 },
14182 14185          };
14183 14186  
14184 14187          ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
14185 14188              NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
14186 14189          if (ksp == NULL || ksp->ks_data == NULL)
14187 14190                  return (NULL);
14188 14191  
14189 14192          bcopy(&template, ksp->ks_data, sizeof (template));
14190 14193  
14191 14194          ksp->ks_update = icmp_kstat_update;
14192 14195          ksp->ks_private = (void *)(uintptr_t)stackid;
14193 14196  
14194 14197          kstat_install(ksp);
14195 14198          return (ksp);
14196 14199  }
14197 14200  
14198 14201  static void
14199 14202  icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14200 14203  {
14201 14204          if (ksp != NULL) {
14202 14205                  ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14203 14206                  kstat_delete_netstack(ksp, stackid);
14204 14207          }
14205 14208  }
14206 14209  
14207 14210  static int
14208 14211  icmp_kstat_update(kstat_t *kp, int rw)
14209 14212  {
14210 14213          icmp_named_kstat_t *icmpkp;
14211 14214          netstackid_t    stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14212 14215          netstack_t      *ns;
14213 14216          ip_stack_t      *ipst;
14214 14217  
14215 14218          if (kp->ks_data == NULL)
14216 14219                  return (EIO);
14217 14220  
14218 14221          if (rw == KSTAT_WRITE)
14219 14222                  return (EACCES);
14220 14223  
14221 14224          ns = netstack_find_by_stackid(stackid);
14222 14225          if (ns == NULL)
14223 14226                  return (-1);
14224 14227          ipst = ns->netstack_ip;
14225 14228          if (ipst == NULL) {
14226 14229                  netstack_rele(ns);
14227 14230                  return (-1);
14228 14231          }
14229 14232          icmpkp = (icmp_named_kstat_t *)kp->ks_data;
14230 14233  
14231 14234          icmpkp->inMsgs.value.ui32 =         ipst->ips_icmp_mib.icmpInMsgs;
14232 14235          icmpkp->inErrors.value.ui32 =       ipst->ips_icmp_mib.icmpInErrors;
14233 14236          icmpkp->inDestUnreachs.value.ui32 =
14234 14237              ipst->ips_icmp_mib.icmpInDestUnreachs;
14235 14238          icmpkp->inTimeExcds.value.ui32 =    ipst->ips_icmp_mib.icmpInTimeExcds;
14236 14239          icmpkp->inParmProbs.value.ui32 =    ipst->ips_icmp_mib.icmpInParmProbs;
14237 14240          icmpkp->inSrcQuenchs.value.ui32 =   ipst->ips_icmp_mib.icmpInSrcQuenchs;
14238 14241          icmpkp->inRedirects.value.ui32 =    ipst->ips_icmp_mib.icmpInRedirects;
14239 14242          icmpkp->inEchos.value.ui32 =        ipst->ips_icmp_mib.icmpInEchos;
14240 14243          icmpkp->inEchoReps.value.ui32 =     ipst->ips_icmp_mib.icmpInEchoReps;
14241 14244          icmpkp->inTimestamps.value.ui32 =   ipst->ips_icmp_mib.icmpInTimestamps;
14242 14245          icmpkp->inTimestampReps.value.ui32 =
14243 14246              ipst->ips_icmp_mib.icmpInTimestampReps;
14244 14247          icmpkp->inAddrMasks.value.ui32 =    ipst->ips_icmp_mib.icmpInAddrMasks;
14245 14248          icmpkp->inAddrMaskReps.value.ui32 =
14246 14249              ipst->ips_icmp_mib.icmpInAddrMaskReps;
14247 14250          icmpkp->outMsgs.value.ui32 =        ipst->ips_icmp_mib.icmpOutMsgs;
14248 14251          icmpkp->outErrors.value.ui32 =      ipst->ips_icmp_mib.icmpOutErrors;
14249 14252          icmpkp->outDestUnreachs.value.ui32 =
14250 14253              ipst->ips_icmp_mib.icmpOutDestUnreachs;
14251 14254          icmpkp->outTimeExcds.value.ui32 =   ipst->ips_icmp_mib.icmpOutTimeExcds;
14252 14255          icmpkp->outParmProbs.value.ui32 =   ipst->ips_icmp_mib.icmpOutParmProbs;
14253 14256          icmpkp->outSrcQuenchs.value.ui32 =
14254 14257              ipst->ips_icmp_mib.icmpOutSrcQuenchs;
14255 14258          icmpkp->outRedirects.value.ui32 =   ipst->ips_icmp_mib.icmpOutRedirects;
14256 14259          icmpkp->outEchos.value.ui32 =       ipst->ips_icmp_mib.icmpOutEchos;
14257 14260          icmpkp->outEchoReps.value.ui32 =    ipst->ips_icmp_mib.icmpOutEchoReps;
14258 14261          icmpkp->outTimestamps.value.ui32 =
14259 14262              ipst->ips_icmp_mib.icmpOutTimestamps;
14260 14263          icmpkp->outTimestampReps.value.ui32 =
14261 14264              ipst->ips_icmp_mib.icmpOutTimestampReps;
14262 14265          icmpkp->outAddrMasks.value.ui32 =
14263 14266              ipst->ips_icmp_mib.icmpOutAddrMasks;
14264 14267          icmpkp->outAddrMaskReps.value.ui32 =
14265 14268              ipst->ips_icmp_mib.icmpOutAddrMaskReps;
14266 14269          icmpkp->inCksumErrs.value.ui32 =    ipst->ips_icmp_mib.icmpInCksumErrs;
14267 14270          icmpkp->inUnknowns.value.ui32 =     ipst->ips_icmp_mib.icmpInUnknowns;
14268 14271          icmpkp->inFragNeeded.value.ui32 =   ipst->ips_icmp_mib.icmpInFragNeeded;
14269 14272          icmpkp->outFragNeeded.value.ui32 =
14270 14273              ipst->ips_icmp_mib.icmpOutFragNeeded;
14271 14274          icmpkp->outDrops.value.ui32 =       ipst->ips_icmp_mib.icmpOutDrops;
14272 14275          icmpkp->inOverflows.value.ui32 =    ipst->ips_icmp_mib.icmpInOverflows;
14273 14276          icmpkp->inBadRedirects.value.ui32 =
14274 14277              ipst->ips_icmp_mib.icmpInBadRedirects;
14275 14278  
14276 14279          netstack_rele(ns);
14277 14280          return (0);
14278 14281  }
14279 14282  
14280 14283  /*
14281 14284   * This is the fanout function for raw socket opened for SCTP.  Note
14282 14285   * that it is called after SCTP checks that there is no socket which
14283 14286   * wants a packet.  Then before SCTP handles this out of the blue packet,
14284 14287   * this function is called to see if there is any raw socket for SCTP.
14285 14288   * If there is and it is bound to the correct address, the packet will
14286 14289   * be sent to that socket.  Note that only one raw socket can be bound to
14287 14290   * a port.  This is assured in ipcl_sctp_hash_insert();
14288 14291   */
14289 14292  void
14290 14293  ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
14291 14294      ip_recv_attr_t *ira)
14292 14295  {
14293 14296          conn_t          *connp;
14294 14297          queue_t         *rq;
14295 14298          boolean_t       secure;
14296 14299          ill_t           *ill = ira->ira_ill;
14297 14300          ip_stack_t      *ipst = ill->ill_ipst;
14298 14301          ipsec_stack_t   *ipss = ipst->ips_netstack->netstack_ipsec;
14299 14302          sctp_stack_t    *sctps = ipst->ips_netstack->netstack_sctp;
14300 14303          iaflags_t       iraflags = ira->ira_flags;
14301 14304          ill_t           *rill = ira->ira_rill;
14302 14305  
14303 14306          secure = iraflags & IRAF_IPSEC_SECURE;
14304 14307  
14305 14308          connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
14306 14309              ira, ipst);
14307 14310          if (connp == NULL) {
14308 14311                  /*
14309 14312                   * Although raw sctp is not summed, OOB chunks must be.
14310 14313                   * Drop the packet here if the sctp checksum failed.
14311 14314                   */
14312 14315                  if (iraflags & IRAF_SCTP_CSUM_ERR) {
14313 14316                          SCTPS_BUMP_MIB(sctps, sctpChecksumError);
14314 14317                          freemsg(mp);
14315 14318                          return;
14316 14319                  }
14317 14320                  ira->ira_ill = ira->ira_rill = NULL;
14318 14321                  sctp_ootb_input(mp, ira, ipst);
14319 14322                  ira->ira_ill = ill;
14320 14323                  ira->ira_rill = rill;
14321 14324                  return;
14322 14325          }
14323 14326          rq = connp->conn_rq;
14324 14327          if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
14325 14328                  CONN_DEC_REF(connp);
14326 14329                  BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
14327 14330                  freemsg(mp);
14328 14331                  return;
14329 14332          }
14330 14333          if (((iraflags & IRAF_IS_IPV4) ?
14331 14334              CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
14332 14335              CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
14333 14336              secure) {
14334 14337                  mp = ipsec_check_inbound_policy(mp, connp, ipha,
14335 14338                      ip6h, ira);
14336 14339                  if (mp == NULL) {
14337 14340                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
14338 14341                          /* Note that mp is NULL */
14339 14342                          ip_drop_input("ipIfStatsInDiscards", mp, ill);
14340 14343                          CONN_DEC_REF(connp);
14341 14344                          return;
14342 14345                  }
14343 14346          }
14344 14347  
14345 14348          if (iraflags & IRAF_ICMP_ERROR) {
14346 14349                  (connp->conn_recvicmp)(connp, mp, NULL, ira);
14347 14350          } else {
14348 14351                  ill_t *rill = ira->ira_rill;
14349 14352  
14350 14353                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
14351 14354                  /* This is the SOCK_RAW, IPPROTO_SCTP case. */
14352 14355                  ira->ira_ill = ira->ira_rill = NULL;
14353 14356                  (connp->conn_recv)(connp, mp, NULL, ira);
14354 14357                  ira->ira_ill = ill;
14355 14358                  ira->ira_rill = rill;
14356 14359          }
14357 14360          CONN_DEC_REF(connp);
14358 14361  }
14359 14362  
14360 14363  /*
14361 14364   * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
14362 14365   * header before the ip payload.
14363 14366   */
14364 14367  static void
14365 14368  ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
14366 14369  {
14367 14370          int len = (mp->b_wptr - mp->b_rptr);
14368 14371          mblk_t *ip_mp;
14369 14372  
14370 14373          BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14371 14374          if (is_fp_mp || len != fp_mp_len) {
14372 14375                  if (len > fp_mp_len) {
14373 14376                          /*
14374 14377                           * fastpath header and ip header in the first mblk
14375 14378                           */
14376 14379                          mp->b_rptr += fp_mp_len;
14377 14380                  } else {
14378 14381                          /*
14379 14382                           * ip_xmit_attach_llhdr had to prepend an mblk to
14380 14383                           * attach the fastpath header before ip header.
14381 14384                           */
14382 14385                          ip_mp = mp->b_cont;
14383 14386                          freeb(mp);
14384 14387                          mp = ip_mp;
14385 14388                          mp->b_rptr += (fp_mp_len - len);
14386 14389                  }
14387 14390          } else {
14388 14391                  ip_mp = mp->b_cont;
14389 14392                  freeb(mp);
14390 14393                  mp = ip_mp;
14391 14394          }
14392 14395          ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
14393 14396          freemsg(mp);
14394 14397  }
14395 14398  
14396 14399  /*
14397 14400   * Normal post fragmentation function.
14398 14401   *
14399 14402   * Send a packet using the passed in nce. This handles both IPv4 and IPv6
14400 14403   * using the same state machine.
14401 14404   *
14402 14405   * We return an error on failure. In particular we return EWOULDBLOCK
14403 14406   * when the driver flow controls. In that case this ensures that ip_wsrv runs
14404 14407   * (currently by canputnext failure resulting in backenabling from GLD.)
14405 14408   * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
14406 14409   * indication that they can flow control until ip_wsrv() tells then to restart.
14407 14410   *
14408 14411   * If the nce passed by caller is incomplete, this function
14409 14412   * queues the packet and if necessary, sends ARP request and bails.
14410 14413   * If the Neighbor Cache passed is fully resolved, we simply prepend
14411 14414   * the link-layer header to the packet, do ipsec hw acceleration
14412 14415   * work if necessary, and send the packet out on the wire.
14413 14416   */
14414 14417  /* ARGSUSED6 */
14415 14418  int
14416 14419  ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
14417 14420      uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
14418 14421  {
14419 14422          queue_t         *wq;
14420 14423          ill_t           *ill = nce->nce_ill;
14421 14424          ip_stack_t      *ipst = ill->ill_ipst;
14422 14425          uint64_t        delta;
14423 14426          boolean_t       isv6 = ill->ill_isv6;
14424 14427          boolean_t       fp_mp;
14425 14428          ncec_t          *ncec = nce->nce_common;
14426 14429          int64_t         now = LBOLT_FASTPATH64;
14427 14430          boolean_t       is_probe;
14428 14431  
14429 14432          DTRACE_PROBE1(ip__xmit, nce_t *, nce);
14430 14433  
14431 14434          ASSERT(mp != NULL);
14432 14435          ASSERT(mp->b_datap->db_type == M_DATA);
14433 14436          ASSERT(pkt_len == msgdsize(mp));
14434 14437  
14435 14438          /*
14436 14439           * If we have already been here and are coming back after ARP/ND.
14437 14440           * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
14438 14441           * in that case since they have seen the packet when it came here
14439 14442           * the first time.
14440 14443           */
14441 14444          if (ixaflags & IXAF_NO_TRACE)
14442 14445                  goto sendit;
14443 14446  
14444 14447          if (ixaflags & IXAF_IS_IPV4) {
14445 14448                  ipha_t *ipha = (ipha_t *)mp->b_rptr;
14446 14449  
14447 14450                  ASSERT(!isv6);
14448 14451                  ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
14449 14452                  if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
14450 14453                      !(ixaflags & IXAF_NO_PFHOOK)) {
14451 14454                          int     error;
14452 14455  
14453 14456                          FW_HOOKS(ipst->ips_ip4_physical_out_event,
14454 14457                              ipst->ips_ipv4firewall_physical_out,
14455 14458                              NULL, ill, ipha, mp, mp, 0, ipst, error);
14456 14459                          DTRACE_PROBE1(ip4__physical__out__end,
14457 14460                              mblk_t *, mp);
14458 14461                          if (mp == NULL)
14459 14462                                  return (error);
14460 14463  
14461 14464                          /* The length could have changed */
14462 14465                          pkt_len = msgdsize(mp);
14463 14466                  }
14464 14467                  if (ipst->ips_ip4_observe.he_interested) {
14465 14468                          /*
14466 14469                           * Note that for TX the zoneid is the sending
14467 14470                           * zone, whether or not MLP is in play.
14468 14471                           * Since the szone argument is the IP zoneid (i.e.,
14469 14472                           * zero for exclusive-IP zones) and ipobs wants
14470 14473                           * the system zoneid, we map it here.
14471 14474                           */
14472 14475                          szone = IP_REAL_ZONEID(szone, ipst);
14473 14476  
14474 14477                          /*
14475 14478                           * On the outbound path the destination zone will be
14476 14479                           * unknown as we're sending this packet out on the
14477 14480                           * wire.
14478 14481                           */
14479 14482                          ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14480 14483                              ill, ipst);
14481 14484                  }
14482 14485                  DTRACE_IP7(send, mblk_t *, mp,  conn_t *, NULL,
14483 14486                      void_ip_t *, ipha,  __dtrace_ipsr_ill_t *, ill,
14484 14487                      ipha_t *, ipha, ip6_t *, NULL, int, 0);
14485 14488          } else {
14486 14489                  ip6_t *ip6h = (ip6_t *)mp->b_rptr;
14487 14490  
14488 14491                  ASSERT(isv6);
14489 14492                  ASSERT(pkt_len ==
14490 14493                      ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
14491 14494                  if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
14492 14495                      !(ixaflags & IXAF_NO_PFHOOK)) {
14493 14496                          int     error;
14494 14497  
14495 14498                          FW_HOOKS6(ipst->ips_ip6_physical_out_event,
14496 14499                              ipst->ips_ipv6firewall_physical_out,
14497 14500                              NULL, ill, ip6h, mp, mp, 0, ipst, error);
14498 14501                          DTRACE_PROBE1(ip6__physical__out__end,
14499 14502                              mblk_t *, mp);
14500 14503                          if (mp == NULL)
14501 14504                                  return (error);
14502 14505  
14503 14506                          /* The length could have changed */
14504 14507                          pkt_len = msgdsize(mp);
14505 14508                  }
14506 14509                  if (ipst->ips_ip6_observe.he_interested) {
14507 14510                          /* See above */
14508 14511                          szone = IP_REAL_ZONEID(szone, ipst);
14509 14512  
14510 14513                          ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14511 14514                              ill, ipst);
14512 14515                  }
14513 14516                  DTRACE_IP7(send, mblk_t *, mp,  conn_t *, NULL,
14514 14517                      void_ip_t *, ip6h,  __dtrace_ipsr_ill_t *, ill,
14515 14518                      ipha_t *, NULL, ip6_t *, ip6h, int, 0);
14516 14519          }
14517 14520  
14518 14521  sendit:
14519 14522          /*
14520 14523           * We check the state without a lock because the state can never
14521 14524           * move "backwards" to initial or incomplete.
14522 14525           */
14523 14526          switch (ncec->ncec_state) {
14524 14527          case ND_REACHABLE:
14525 14528          case ND_STALE:
14526 14529          case ND_DELAY:
14527 14530          case ND_PROBE:
14528 14531                  mp = ip_xmit_attach_llhdr(mp, nce);
14529 14532                  if (mp == NULL) {
14530 14533                          /*
14531 14534                           * ip_xmit_attach_llhdr has increased
14532 14535                           * ipIfStatsOutDiscards and called ip_drop_output()
14533 14536                           */
14534 14537                          return (ENOBUFS);
14535 14538                  }
14536 14539                  /*
14537 14540                   * check if nce_fastpath completed and we tagged on a
14538 14541                   * copy of nce_fp_mp in ip_xmit_attach_llhdr().
14539 14542                   */
14540 14543                  fp_mp = (mp->b_datap->db_type == M_DATA);
14541 14544  
14542 14545                  if (fp_mp &&
14543 14546                      (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
14544 14547                          ill_dld_direct_t *idd;
14545 14548  
14546 14549                          idd = &ill->ill_dld_capab->idc_direct;
14547 14550                          /*
14548 14551                           * Send the packet directly to DLD, where it
14549 14552                           * may be queued depending on the availability
14550 14553                           * of transmit resources at the media layer.
14551 14554                           * Return value should be taken into
14552 14555                           * account and flow control the TCP.
14553 14556                           */
14554 14557                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14555 14558                          UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14556 14559                              pkt_len);
14557 14560  
14558 14561                          if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
14559 14562                                  (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
14560 14563                                      (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
14561 14564                          } else {
14562 14565                                  uintptr_t cookie;
14563 14566  
14564 14567                                  if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
14565 14568                                      mp, (uintptr_t)xmit_hint, 0)) != 0) {
14566 14569                                          if (ixacookie != NULL)
14567 14570                                                  *ixacookie = cookie;
14568 14571                                          return (EWOULDBLOCK);
14569 14572                                  }
14570 14573                          }
14571 14574                  } else {
14572 14575                          wq = ill->ill_wq;
14573 14576  
14574 14577                          if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
14575 14578                              !canputnext(wq)) {
14576 14579                                  if (ixacookie != NULL)
14577 14580                                          *ixacookie = 0;
14578 14581                                  ip_xmit_flowctl_drop(ill, mp, fp_mp,
14579 14582                                      nce->nce_fp_mp != NULL ?
14580 14583                                      MBLKL(nce->nce_fp_mp) : 0);
14581 14584                                  return (EWOULDBLOCK);
14582 14585                          }
14583 14586                          BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14584 14587                          UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14585 14588                              pkt_len);
14586 14589                          putnext(wq, mp);
14587 14590                  }
14588 14591  
14589 14592                  /*
14590 14593                   * The rest of this function implements Neighbor Unreachability
14591 14594                   * detection. Determine if the ncec is eligible for NUD.
14592 14595                   */
14593 14596                  if (ncec->ncec_flags & NCE_F_NONUD)
14594 14597                          return (0);
14595 14598  
14596 14599                  ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14597 14600  
14598 14601                  /*
14599 14602                   * Check for upper layer advice
14600 14603                   */
14601 14604                  if (ixaflags & IXAF_REACH_CONF) {
14602 14605                          timeout_id_t tid;
14603 14606  
14604 14607                          /*
14605 14608                           * It should be o.k. to check the state without
14606 14609                           * a lock here, at most we lose an advice.
14607 14610                           */
14608 14611                          ncec->ncec_last = TICK_TO_MSEC(now);
14609 14612                          if (ncec->ncec_state != ND_REACHABLE) {
14610 14613                                  mutex_enter(&ncec->ncec_lock);
14611 14614                                  ncec->ncec_state = ND_REACHABLE;
14612 14615                                  tid = ncec->ncec_timeout_id;
14613 14616                                  ncec->ncec_timeout_id = 0;
14614 14617                                  mutex_exit(&ncec->ncec_lock);
14615 14618                                  (void) untimeout(tid);
14616 14619                                  if (ip_debug > 2) {
14617 14620                                          /* ip1dbg */
14618 14621                                          pr_addr_dbg("ip_xmit: state"
14619 14622                                              " for %s changed to"
14620 14623                                              " REACHABLE\n", AF_INET6,
14621 14624                                              &ncec->ncec_addr);
14622 14625                                  }
14623 14626                          }
14624 14627                          return (0);
14625 14628                  }
14626 14629  
14627 14630                  delta =  TICK_TO_MSEC(now) - ncec->ncec_last;
14628 14631                  ip1dbg(("ip_xmit: delta = %" PRId64
14629 14632                      " ill_reachable_time = %d \n", delta,
14630 14633                      ill->ill_reachable_time));
14631 14634                  if (delta > (uint64_t)ill->ill_reachable_time) {
14632 14635                          mutex_enter(&ncec->ncec_lock);
14633 14636                          switch (ncec->ncec_state) {
14634 14637                          case ND_REACHABLE:
14635 14638                                  ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14636 14639                                  /* FALLTHROUGH */
14637 14640                          case ND_STALE:
14638 14641                                  /*
14639 14642                                   * ND_REACHABLE is identical to
14640 14643                                   * ND_STALE in this specific case. If
14641 14644                                   * reachable time has expired for this
14642 14645                                   * neighbor (delta is greater than
14643 14646                                   * reachable time), conceptually, the
14644 14647                                   * neighbor cache is no longer in
14645 14648                                   * REACHABLE state, but already in
14646 14649                                   * STALE state.  So the correct
14647 14650                                   * transition here is to ND_DELAY.
14648 14651                                   */
14649 14652                                  ncec->ncec_state = ND_DELAY;
14650 14653                                  mutex_exit(&ncec->ncec_lock);
14651 14654                                  nce_restart_timer(ncec,
14652 14655                                      ipst->ips_delay_first_probe_time);
14653 14656                                  if (ip_debug > 3) {
14654 14657                                          /* ip2dbg */
14655 14658                                          pr_addr_dbg("ip_xmit: state"
14656 14659                                              " for %s changed to"
14657 14660                                              " DELAY\n", AF_INET6,
14658 14661                                              &ncec->ncec_addr);
14659 14662                                  }
14660 14663                                  break;
14661 14664                          case ND_DELAY:
14662 14665                          case ND_PROBE:
14663 14666                                  mutex_exit(&ncec->ncec_lock);
14664 14667                                  /* Timers have already started */
14665 14668                                  break;
14666 14669                          case ND_UNREACHABLE:
14667 14670                                  /*
14668 14671                                   * nce_timer has detected that this ncec
14669 14672                                   * is unreachable and initiated deleting
14670 14673                                   * this ncec.
14671 14674                                   * This is a harmless race where we found the
14672 14675                                   * ncec before it was deleted and have
14673 14676                                   * just sent out a packet using this
14674 14677                                   * unreachable ncec.
14675 14678                                   */
14676 14679                                  mutex_exit(&ncec->ncec_lock);
14677 14680                                  break;
14678 14681                          default:
14679 14682                                  ASSERT(0);
14680 14683                                  mutex_exit(&ncec->ncec_lock);
14681 14684                          }
14682 14685                  }
14683 14686                  return (0);
14684 14687  
14685 14688          case ND_INCOMPLETE:
14686 14689                  /*
14687 14690                   * the state could have changed since we didn't hold the lock.
14688 14691                   * Re-verify state under lock.
14689 14692                   */
14690 14693                  is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14691 14694                  mutex_enter(&ncec->ncec_lock);
14692 14695                  if (NCE_ISREACHABLE(ncec)) {
14693 14696                          mutex_exit(&ncec->ncec_lock);
14694 14697                          goto sendit;
14695 14698                  }
14696 14699                  /* queue the packet */
14697 14700                  nce_queue_mp(ncec, mp, is_probe);
14698 14701                  mutex_exit(&ncec->ncec_lock);
14699 14702                  DTRACE_PROBE2(ip__xmit__incomplete,
14700 14703                      (ncec_t *), ncec, (mblk_t *), mp);
14701 14704                  return (0);
14702 14705  
14703 14706          case ND_INITIAL:
14704 14707                  /*
14705 14708                   * State could have changed since we didn't hold the lock, so
14706 14709                   * re-verify state.
14707 14710                   */
14708 14711                  is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14709 14712                  mutex_enter(&ncec->ncec_lock);
14710 14713                  if (NCE_ISREACHABLE(ncec))  {
14711 14714                          mutex_exit(&ncec->ncec_lock);
14712 14715                          goto sendit;
14713 14716                  }
14714 14717                  nce_queue_mp(ncec, mp, is_probe);
14715 14718                  if (ncec->ncec_state == ND_INITIAL) {
14716 14719                          ncec->ncec_state = ND_INCOMPLETE;
14717 14720                          mutex_exit(&ncec->ncec_lock);
14718 14721                          /*
14719 14722                           * figure out the source we want to use
14720 14723                           * and resolve it.
14721 14724                           */
14722 14725                          ip_ndp_resolve(ncec);
14723 14726                  } else  {
14724 14727                          mutex_exit(&ncec->ncec_lock);
14725 14728                  }
14726 14729                  return (0);
14727 14730  
14728 14731          case ND_UNREACHABLE:
14729 14732                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14730 14733                  ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14731 14734                      mp, ill);
14732 14735                  freemsg(mp);
14733 14736                  return (0);
14734 14737  
14735 14738          default:
14736 14739                  ASSERT(0);
14737 14740                  BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14738 14741                  ip_drop_output("ipIfStatsOutDiscards - ND_other",
14739 14742                      mp, ill);
14740 14743                  freemsg(mp);
14741 14744                  return (ENETUNREACH);
14742 14745          }
14743 14746  }
14744 14747  
14745 14748  /*
14746 14749   * Return B_TRUE if the buffers differ in length or content.
14747 14750   * This is used for comparing extension header buffers.
14748 14751   * Note that an extension header would be declared different
14749 14752   * even if all that changed was the next header value in that header i.e.
14750 14753   * what really changed is the next extension header.
14751 14754   */
14752 14755  boolean_t
14753 14756  ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14754 14757      uint_t blen)
14755 14758  {
14756 14759          if (!b_valid)
14757 14760                  blen = 0;
14758 14761  
14759 14762          if (alen != blen)
14760 14763                  return (B_TRUE);
14761 14764          if (alen == 0)
14762 14765                  return (B_FALSE);       /* Both zero length */
14763 14766          return (bcmp(abuf, bbuf, alen));
14764 14767  }
14765 14768  
14766 14769  /*
14767 14770   * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14768 14771   * Return B_FALSE if memory allocation fails - don't change any state!
14769 14772   */
14770 14773  boolean_t
14771 14774  ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14772 14775      const void *src, uint_t srclen)
14773 14776  {
14774 14777          void *dst;
14775 14778  
14776 14779          if (!src_valid)
14777 14780                  srclen = 0;
14778 14781  
14779 14782          ASSERT(*dstlenp == 0);
14780 14783          if (src != NULL && srclen != 0) {
14781 14784                  dst = mi_alloc(srclen, BPRI_MED);
14782 14785                  if (dst == NULL)
14783 14786                          return (B_FALSE);
14784 14787          } else {
14785 14788                  dst = NULL;
14786 14789          }
14787 14790          if (*dstp != NULL)
14788 14791                  mi_free(*dstp);
14789 14792          *dstp = dst;
14790 14793          *dstlenp = dst == NULL ? 0 : srclen;
14791 14794          return (B_TRUE);
14792 14795  }
14793 14796  
14794 14797  /*
14795 14798   * Replace what is in *dst, *dstlen with the source.
14796 14799   * Assumes ip_allocbuf has already been called.
14797 14800   */
14798 14801  void
14799 14802  ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14800 14803      const void *src, uint_t srclen)
14801 14804  {
14802 14805          if (!src_valid)
14803 14806                  srclen = 0;
14804 14807  
14805 14808          ASSERT(*dstlenp == srclen);
14806 14809          if (src != NULL && srclen != 0)
14807 14810                  bcopy(src, *dstp, srclen);
14808 14811  }
14809 14812  
14810 14813  /*
14811 14814   * Free the storage pointed to by the members of an ip_pkt_t.
14812 14815   */
14813 14816  void
14814 14817  ip_pkt_free(ip_pkt_t *ipp)
14815 14818  {
14816 14819          uint_t  fields = ipp->ipp_fields;
14817 14820  
14818 14821          if (fields & IPPF_HOPOPTS) {
14819 14822                  kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14820 14823                  ipp->ipp_hopopts = NULL;
14821 14824                  ipp->ipp_hopoptslen = 0;
14822 14825          }
14823 14826          if (fields & IPPF_RTHDRDSTOPTS) {
14824 14827                  kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14825 14828                  ipp->ipp_rthdrdstopts = NULL;
14826 14829                  ipp->ipp_rthdrdstoptslen = 0;
14827 14830          }
14828 14831          if (fields & IPPF_DSTOPTS) {
14829 14832                  kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14830 14833                  ipp->ipp_dstopts = NULL;
14831 14834                  ipp->ipp_dstoptslen = 0;
14832 14835          }
14833 14836          if (fields & IPPF_RTHDR) {
14834 14837                  kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14835 14838                  ipp->ipp_rthdr = NULL;
14836 14839                  ipp->ipp_rthdrlen = 0;
14837 14840          }
14838 14841          if (fields & IPPF_IPV4_OPTIONS) {
14839 14842                  kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14840 14843                  ipp->ipp_ipv4_options = NULL;
14841 14844                  ipp->ipp_ipv4_options_len = 0;
14842 14845          }
14843 14846          if (fields & IPPF_LABEL_V4) {
14844 14847                  kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
14845 14848                  ipp->ipp_label_v4 = NULL;
14846 14849                  ipp->ipp_label_len_v4 = 0;
14847 14850          }
14848 14851          if (fields & IPPF_LABEL_V6) {
14849 14852                  kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6);
14850 14853                  ipp->ipp_label_v6 = NULL;
14851 14854                  ipp->ipp_label_len_v6 = 0;
14852 14855          }
14853 14856          ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14854 14857              IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14855 14858  }
14856 14859  
14857 14860  /*
14858 14861   * Copy from src to dst and allocate as needed.
14859 14862   * Returns zero or ENOMEM.
14860 14863   *
14861 14864   * The caller must initialize dst to zero.
14862 14865   */
14863 14866  int
14864 14867  ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14865 14868  {
14866 14869          uint_t  fields = src->ipp_fields;
14867 14870  
14868 14871          /* Start with fields that don't require memory allocation */
14869 14872          dst->ipp_fields = fields &
14870 14873              ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14871 14874              IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14872 14875  
14873 14876          dst->ipp_addr = src->ipp_addr;
14874 14877          dst->ipp_unicast_hops = src->ipp_unicast_hops;
14875 14878          dst->ipp_hoplimit = src->ipp_hoplimit;
14876 14879          dst->ipp_tclass = src->ipp_tclass;
14877 14880          dst->ipp_type_of_service = src->ipp_type_of_service;
14878 14881  
14879 14882          if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14880 14883              IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6)))
14881 14884                  return (0);
14882 14885  
14883 14886          if (fields & IPPF_HOPOPTS) {
14884 14887                  dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14885 14888                  if (dst->ipp_hopopts == NULL) {
14886 14889                          ip_pkt_free(dst);
14887 14890                          return (ENOMEM);
14888 14891                  }
14889 14892                  dst->ipp_fields |= IPPF_HOPOPTS;
14890 14893                  bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14891 14894                      src->ipp_hopoptslen);
14892 14895                  dst->ipp_hopoptslen = src->ipp_hopoptslen;
14893 14896          }
14894 14897          if (fields & IPPF_RTHDRDSTOPTS) {
14895 14898                  dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14896 14899                      kmflag);
14897 14900                  if (dst->ipp_rthdrdstopts == NULL) {
14898 14901                          ip_pkt_free(dst);
14899 14902                          return (ENOMEM);
14900 14903                  }
14901 14904                  dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14902 14905                  bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14903 14906                      src->ipp_rthdrdstoptslen);
14904 14907                  dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14905 14908          }
14906 14909          if (fields & IPPF_DSTOPTS) {
14907 14910                  dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14908 14911                  if (dst->ipp_dstopts == NULL) {
14909 14912                          ip_pkt_free(dst);
14910 14913                          return (ENOMEM);
14911 14914                  }
14912 14915                  dst->ipp_fields |= IPPF_DSTOPTS;
14913 14916                  bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14914 14917                      src->ipp_dstoptslen);
14915 14918                  dst->ipp_dstoptslen = src->ipp_dstoptslen;
14916 14919          }
14917 14920          if (fields & IPPF_RTHDR) {
14918 14921                  dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14919 14922                  if (dst->ipp_rthdr == NULL) {
14920 14923                          ip_pkt_free(dst);
14921 14924                          return (ENOMEM);
14922 14925                  }
14923 14926                  dst->ipp_fields |= IPPF_RTHDR;
14924 14927                  bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14925 14928                      src->ipp_rthdrlen);
14926 14929                  dst->ipp_rthdrlen = src->ipp_rthdrlen;
14927 14930          }
14928 14931          if (fields & IPPF_IPV4_OPTIONS) {
14929 14932                  dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14930 14933                      kmflag);
14931 14934                  if (dst->ipp_ipv4_options == NULL) {
14932 14935                          ip_pkt_free(dst);
14933 14936                          return (ENOMEM);
14934 14937                  }
14935 14938                  dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14936 14939                  bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14937 14940                      src->ipp_ipv4_options_len);
14938 14941                  dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14939 14942          }
14940 14943          if (fields & IPPF_LABEL_V4) {
14941 14944                  dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag);
14942 14945                  if (dst->ipp_label_v4 == NULL) {
14943 14946                          ip_pkt_free(dst);
14944 14947                          return (ENOMEM);
14945 14948                  }
14946 14949                  dst->ipp_fields |= IPPF_LABEL_V4;
14947 14950                  bcopy(src->ipp_label_v4, dst->ipp_label_v4,
14948 14951                      src->ipp_label_len_v4);
14949 14952                  dst->ipp_label_len_v4 = src->ipp_label_len_v4;
14950 14953          }
14951 14954          if (fields & IPPF_LABEL_V6) {
14952 14955                  dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag);
14953 14956                  if (dst->ipp_label_v6 == NULL) {
14954 14957                          ip_pkt_free(dst);
14955 14958                          return (ENOMEM);
14956 14959                  }
14957 14960                  dst->ipp_fields |= IPPF_LABEL_V6;
14958 14961                  bcopy(src->ipp_label_v6, dst->ipp_label_v6,
14959 14962                      src->ipp_label_len_v6);
14960 14963                  dst->ipp_label_len_v6 = src->ipp_label_len_v6;
14961 14964          }
14962 14965          if (fields & IPPF_FRAGHDR) {
14963 14966                  dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14964 14967                  if (dst->ipp_fraghdr == NULL) {
14965 14968                          ip_pkt_free(dst);
14966 14969                          return (ENOMEM);
14967 14970                  }
14968 14971                  dst->ipp_fields |= IPPF_FRAGHDR;
14969 14972                  bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14970 14973                      src->ipp_fraghdrlen);
14971 14974                  dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14972 14975          }
14973 14976          return (0);
14974 14977  }
14975 14978  
14976 14979  /*
14977 14980   * Returns INADDR_ANY if no source route
14978 14981   */
14979 14982  ipaddr_t
14980 14983  ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14981 14984  {
14982 14985          ipaddr_t        nexthop = INADDR_ANY;
14983 14986          ipoptp_t        opts;
14984 14987          uchar_t         *opt;
14985 14988          uint8_t         optval;
14986 14989          uint8_t         optlen;
14987 14990          uint32_t        totallen;
14988 14991  
14989 14992          if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14990 14993                  return (INADDR_ANY);
14991 14994  
14992 14995          totallen = ipp->ipp_ipv4_options_len;
14993 14996          if (totallen & 0x3)
14994 14997                  return (INADDR_ANY);
14995 14998  
14996 14999          for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14997 15000              optval != IPOPT_EOL;
14998 15001              optval = ipoptp_next(&opts)) {
14999 15002                  opt = opts.ipoptp_cur;
15000 15003                  switch (optval) {
15001 15004                          uint8_t off;
15002 15005                  case IPOPT_SSRR:
15003 15006                  case IPOPT_LSRR:
15004 15007                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15005 15008                                  break;
15006 15009                          }
15007 15010                          optlen = opts.ipoptp_len;
15008 15011                          off = opt[IPOPT_OFFSET];
15009 15012                          off--;
15010 15013                          if (optlen < IP_ADDR_LEN ||
15011 15014                              off > optlen - IP_ADDR_LEN) {
15012 15015                                  /* End of source route */
15013 15016                                  break;
15014 15017                          }
15015 15018                          bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
15016 15019                          if (nexthop == htonl(INADDR_LOOPBACK)) {
15017 15020                                  /* Ignore */
15018 15021                                  nexthop = INADDR_ANY;
15019 15022                                  break;
15020 15023                          }
15021 15024                          break;
15022 15025                  }
15023 15026          }
15024 15027          return (nexthop);
15025 15028  }
15026 15029  
15027 15030  /*
15028 15031   * Reverse a source route.
15029 15032   */
15030 15033  void
15031 15034  ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
15032 15035  {
15033 15036          ipaddr_t        tmp;
15034 15037          ipoptp_t        opts;
15035 15038          uchar_t         *opt;
15036 15039          uint8_t         optval;
15037 15040          uint32_t        totallen;
15038 15041  
15039 15042          if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15040 15043                  return;
15041 15044  
15042 15045          totallen = ipp->ipp_ipv4_options_len;
15043 15046          if (totallen & 0x3)
15044 15047                  return;
15045 15048  
15046 15049          for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15047 15050              optval != IPOPT_EOL;
15048 15051              optval = ipoptp_next(&opts)) {
15049 15052                  uint8_t off1, off2;
15050 15053  
15051 15054                  opt = opts.ipoptp_cur;
15052 15055                  switch (optval) {
15053 15056                  case IPOPT_SSRR:
15054 15057                  case IPOPT_LSRR:
15055 15058                          if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15056 15059                                  break;
15057 15060                          }
15058 15061                          off1 = IPOPT_MINOFF_SR - 1;
15059 15062                          off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
15060 15063                          while (off2 > off1) {
15061 15064                                  bcopy(opt + off2, &tmp, IP_ADDR_LEN);
15062 15065                                  bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
15063 15066                                  bcopy(&tmp, opt + off2, IP_ADDR_LEN);
15064 15067                                  off2 -= IP_ADDR_LEN;
15065 15068                                  off1 += IP_ADDR_LEN;
15066 15069                          }
15067 15070                          opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
15068 15071                          break;
15069 15072                  }
15070 15073          }
15071 15074  }
15072 15075  
15073 15076  /*
15074 15077   * Returns NULL if no routing header
15075 15078   */
15076 15079  in6_addr_t *
15077 15080  ip_pkt_source_route_v6(const ip_pkt_t *ipp)
15078 15081  {
15079 15082          in6_addr_t      *nexthop = NULL;
15080 15083          ip6_rthdr0_t    *rthdr;
15081 15084  
15082 15085          if (!(ipp->ipp_fields & IPPF_RTHDR))
15083 15086                  return (NULL);
15084 15087  
15085 15088          rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
15086 15089          if (rthdr->ip6r0_segleft == 0)
15087 15090                  return (NULL);
15088 15091  
15089 15092          nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
15090 15093          return (nexthop);
15091 15094  }
15092 15095  
15093 15096  zoneid_t
15094 15097  ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
15095 15098      zoneid_t lookup_zoneid)
15096 15099  {
15097 15100          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
15098 15101          ire_t           *ire;
15099 15102          int             ire_flags = MATCH_IRE_TYPE;
15100 15103          zoneid_t        zoneid = ALL_ZONES;
15101 15104  
15102 15105          if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15103 15106                  return (ALL_ZONES);
15104 15107  
15105 15108          if (lookup_zoneid != ALL_ZONES)
15106 15109                  ire_flags |= MATCH_IRE_ZONEONLY;
15107 15110          ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_LOCAL | IRE_LOOPBACK,
15108 15111              NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15109 15112          if (ire != NULL) {
15110 15113                  zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15111 15114                  ire_refrele(ire);
15112 15115          }
15113 15116          return (zoneid);
15114 15117  }
15115 15118  
15116 15119  zoneid_t
15117 15120  ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
15118 15121      ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
15119 15122  {
15120 15123          ip_stack_t      *ipst = ira->ira_ill->ill_ipst;
15121 15124          ire_t           *ire;
15122 15125          int             ire_flags = MATCH_IRE_TYPE;
15123 15126          zoneid_t        zoneid = ALL_ZONES;
15124 15127  
15125 15128          if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15126 15129                  return (ALL_ZONES);
15127 15130  
15128 15131          if (IN6_IS_ADDR_LINKLOCAL(addr))
15129 15132                  ire_flags |= MATCH_IRE_ILL;
15130 15133  
15131 15134          if (lookup_zoneid != ALL_ZONES)
15132 15135                  ire_flags |= MATCH_IRE_ZONEONLY;
15133 15136          ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15134 15137              ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15135 15138          if (ire != NULL) {
15136 15139                  zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15137 15140                  ire_refrele(ire);
15138 15141          }
15139 15142          return (zoneid);
15140 15143  }
15141 15144  
15142 15145  /*
15143 15146   * IP obserability hook support functions.
15144 15147   */
15145 15148  static void
15146 15149  ipobs_init(ip_stack_t *ipst)
15147 15150  {
15148 15151          netid_t id;
15149 15152  
15150 15153          id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
15151 15154  
15152 15155          ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
15153 15156          VERIFY(ipst->ips_ip4_observe_pr != NULL);
15154 15157  
15155 15158          ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
15156 15159          VERIFY(ipst->ips_ip6_observe_pr != NULL);
15157 15160  }
15158 15161  
15159 15162  static void
15160 15163  ipobs_fini(ip_stack_t *ipst)
15161 15164  {
15162 15165  
15163 15166          VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
15164 15167          VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
15165 15168  }
15166 15169  
15167 15170  /*
15168 15171   * hook_pkt_observe_t is composed in network byte order so that the
15169 15172   * entire mblk_t chain handed into hook_run can be used as-is.
15170 15173   * The caveat is that use of the fields, such as the zone fields,
15171 15174   * requires conversion into host byte order first.
15172 15175   */
15173 15176  void
15174 15177  ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
15175 15178      const ill_t *ill, ip_stack_t *ipst)
15176 15179  {
15177 15180          hook_pkt_observe_t *hdr;
15178 15181          uint64_t grifindex;
15179 15182          mblk_t *imp;
15180 15183  
15181 15184          imp = allocb(sizeof (*hdr), BPRI_HI);
15182 15185          if (imp == NULL)
15183 15186                  return;
15184 15187  
15185 15188          hdr = (hook_pkt_observe_t *)imp->b_rptr;
15186 15189          /*
15187 15190           * b_wptr is set to make the apparent size of the data in the mblk_t
15188 15191           * to exclude the pointers at the end of hook_pkt_observer_t.
15189 15192           */
15190 15193          imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
15191 15194          imp->b_cont = mp;
15192 15195  
15193 15196          ASSERT(DB_TYPE(mp) == M_DATA);
15194 15197  
15195 15198          if (IS_UNDER_IPMP(ill))
15196 15199                  grifindex = ipmp_ill_get_ipmp_ifindex(ill);
15197 15200          else
15198 15201                  grifindex = 0;
15199 15202  
15200 15203          hdr->hpo_version = 1;
15201 15204          hdr->hpo_htype = htons(htype);
15202 15205          hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
15203 15206          hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
15204 15207          hdr->hpo_grifindex = htonl(grifindex);
15205 15208          hdr->hpo_zsrc = htonl(zsrc);
15206 15209          hdr->hpo_zdst = htonl(zdst);
15207 15210          hdr->hpo_pkt = imp;
15208 15211          hdr->hpo_ctx = ipst->ips_netstack;
15209 15212  
15210 15213          if (ill->ill_isv6) {
15211 15214                  hdr->hpo_family = AF_INET6;
15212 15215                  (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
15213 15216                      ipst->ips_ipv6observing, (hook_data_t)hdr);
15214 15217          } else {
15215 15218                  hdr->hpo_family = AF_INET;
15216 15219                  (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
15217 15220                      ipst->ips_ipv4observing, (hook_data_t)hdr);
15218 15221          }
15219 15222  
15220 15223          imp->b_cont = NULL;
15221 15224          freemsg(imp);
15222 15225  }
15223 15226  
15224 15227  /*
15225 15228   * Utility routine that checks if `v4srcp' is a valid address on underlying
15226 15229   * interface `ill'.  If `ipifp' is non-NULL, it's set to a held ipif
15227 15230   * associated with `v4srcp' on success.  NOTE: if this is not called from
15228 15231   * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
15229 15232   * group during or after this lookup.
15230 15233   */
15231 15234  boolean_t
15232 15235  ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
15233 15236  {
15234 15237          ipif_t *ipif;
15235 15238  
15236 15239          ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
15237 15240          if (ipif != NULL) {
15238 15241                  if (ipifp != NULL)
15239 15242                          *ipifp = ipif;
15240 15243                  else
15241 15244                          ipif_refrele(ipif);
15242 15245                  return (B_TRUE);
15243 15246          }
15244 15247  
15245 15248          ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
15246 15249              *v4srcp));
15247 15250          return (B_FALSE);
15248 15251  }
15249 15252  
15250 15253  /*
15251 15254   * Transport protocol call back function for CPU state change.
15252 15255   */
15253 15256  /* ARGSUSED */
15254 15257  static int
15255 15258  ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
15256 15259  {
15257 15260          processorid_t cpu_seqid;
15258 15261          netstack_handle_t nh;
15259 15262          netstack_t *ns;
15260 15263  
15261 15264          ASSERT(MUTEX_HELD(&cpu_lock));
15262 15265  
15263 15266          switch (what) {
15264 15267          case CPU_CONFIG:
15265 15268          case CPU_ON:
15266 15269          case CPU_INIT:
15267 15270          case CPU_CPUPART_IN:
15268 15271                  cpu_seqid = cpu[id]->cpu_seqid;
15269 15272                  netstack_next_init(&nh);
15270 15273                  while ((ns = netstack_next(&nh)) != NULL) {
15271 15274                          tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
15272 15275                          sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
15273 15276                          udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
15274 15277                          netstack_rele(ns);
15275 15278                  }
15276 15279                  netstack_next_fini(&nh);
15277 15280                  break;
15278 15281          case CPU_UNCONFIG:
15279 15282          case CPU_OFF:
15280 15283          case CPU_CPUPART_OUT:
15281 15284                  /*
15282 15285                   * Nothing to do.  We don't remove the per CPU stats from
15283 15286                   * the IP stack even when the CPU goes offline.
15284 15287                   */
15285 15288                  break;
15286 15289          default:
15287 15290                  break;
15288 15291          }
15289 15292          return (0);
15290 15293  }
  
    | 
      ↓ open down ↓ | 
    1341 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX