1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include <sys/types.h> 31 #include <sys/stream.h> 32 #include <sys/dlpi.h> 33 #include <sys/stropts.h> 34 #include <sys/sysmacros.h> 35 #include <sys/strsubr.h> 36 #include <sys/strlog.h> 37 #include <sys/strsun.h> 38 #include <sys/zone.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/xti_inet.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/cmn_err.h> 45 #include <sys/debug.h> 46 #include <sys/kobj.h> 47 #include <sys/modctl.h> 48 #include <sys/atomic.h> 49 #include <sys/policy.h> 50 #include <sys/priv.h> 51 52 #include <sys/systm.h> 53 #include <sys/param.h> 54 #include <sys/kmem.h> 55 #include <sys/sdt.h> 56 #include <sys/socket.h> 57 #include <sys/vtrace.h> 58 #include <sys/isa_defs.h> 59 #include <net/if.h> 60 #include <net/if_arp.h> 61 #include <net/route.h> 62 #include <sys/sockio.h> 63 #include <netinet/in.h> 64 #include <net/if_dl.h> 65 66 #include <inet/common.h> 67 #include <inet/mi.h> 68 #include <inet/mib2.h> 69 #include <inet/nd.h> 70 #include <inet/arp.h> 71 #include <inet/snmpcom.h> 72 #include <inet/kstatcom.h> 73 74 #include <netinet/igmp_var.h> 75 #include <netinet/ip6.h> 76 #include <netinet/icmp6.h> 77 #include <netinet/sctp.h> 78 79 #include <inet/ip.h> 80 #include <inet/ip_impl.h> 81 #include <inet/ip6.h> 82 #include <inet/ip6_asp.h> 83 #include <inet/tcp.h> 84 #include <inet/tcp_impl.h> 85 #include <inet/ip_multi.h> 86 #include <inet/ip_if.h> 87 #include <inet/ip_ire.h> 88 #include <inet/ip_ftable.h> 89 #include <inet/ip_rts.h> 90 #include <inet/optcom.h> 91 #include <inet/ip_ndp.h> 92 #include <inet/ip_listutils.h> 93 #include <netinet/igmp.h> 94 #include <netinet/ip_mroute.h> 95 #include <inet/ipp_common.h> 96 97 #include <net/pfkeyv2.h> 98 #include <inet/ipsec_info.h> 99 #include <inet/sadb.h> 100 #include <inet/ipsec_impl.h> 101 #include <sys/iphada.h> 102 #include <inet/tun.h> 103 #include <inet/ipdrop.h> 104 #include <inet/ip_netinfo.h> 105 106 #include <sys/ethernet.h> 107 #include <net/if_types.h> 108 #include <sys/cpuvar.h> 109 110 #include <ipp/ipp.h> 111 #include <ipp/ipp_impl.h> 112 #include <ipp/ipgpc/ipgpc.h> 113 114 #include <sys/multidata.h> 115 #include <sys/pattr.h> 116 117 #include <inet/ipclassifier.h> 118 #include <inet/sctp_ip.h> 119 #include <inet/sctp/sctp_impl.h> 120 #include <inet/udp_impl.h> 121 #include <sys/sunddi.h> 122 123 #include <sys/tsol/label.h> 124 #include <sys/tsol/tnet.h> 125 126 #include <rpc/pmap_prot.h> 127 128 /* 129 * Values for squeue switch: 130 * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain 131 * IP_SQUEUE_ENTER: squeue_enter 132 * IP_SQUEUE_FILL: squeue_fill 133 */ 134 int ip_squeue_enter = 2; /* Setable in /etc/system */ 135 136 squeue_func_t ip_input_proc; 137 #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x)) 138 139 #define TCP6 "tcp6" 140 #define TCP "tcp" 141 #define SCTP "sctp" 142 #define SCTP6 "sctp6" 143 144 major_t TCP6_MAJ; 145 major_t TCP_MAJ; 146 major_t SCTP_MAJ; 147 major_t SCTP6_MAJ; 148 149 /* 150 * Setable in /etc/system 151 */ 152 int ip_poll_normal_ms = 100; 153 int ip_poll_normal_ticks = 0; 154 int ip_modclose_ackwait_ms = 3000; 155 156 /* 157 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 158 */ 159 160 struct listptr_s { 161 mblk_t *lp_head; /* pointer to the head of the list */ 162 mblk_t *lp_tail; /* pointer to the tail of the list */ 163 }; 164 165 typedef struct listptr_s listptr_t; 166 167 /* 168 * This is used by ip_snmp_get_mib2_ip_route_media and 169 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 170 */ 171 typedef struct iproutedata_s { 172 uint_t ird_idx; 173 listptr_t ird_route; /* ipRouteEntryTable */ 174 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 175 listptr_t ird_attrs; /* ipRouteAttributeTable */ 176 } iproutedata_t; 177 178 /* 179 * Cluster specific hooks. These should be NULL when booted as a non-cluster 180 */ 181 182 /* 183 * Hook functions to enable cluster networking 184 * On non-clustered systems these vectors must always be NULL. 185 * 186 * Hook function to Check ip specified ip address is a shared ip address 187 * in the cluster 188 * 189 */ 190 int (*cl_inet_isclusterwide)(uint8_t protocol, 191 sa_family_t addr_family, uint8_t *laddrp) = NULL; 192 193 /* 194 * Hook function to generate cluster wide ip fragment identifier 195 */ 196 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 197 uint8_t *laddrp, uint8_t *faddrp) = NULL; 198 199 /* 200 * Synchronization notes: 201 * 202 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 203 * MT level protection given by STREAMS. IP uses a combination of its own 204 * internal serialization mechanism and standard Solaris locking techniques. 205 * The internal serialization is per phyint (no IPMP) or per IPMP group. 206 * This is used to serialize plumbing operations, IPMP operations, certain 207 * multicast operations, most set ioctls, igmp/mld timers etc. 208 * 209 * Plumbing is a long sequence of operations involving message 210 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 211 * involved in plumbing operations. A natural model is to serialize these 212 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 213 * parallel without any interference. But various set ioctls on hme0 are best 214 * serialized. However if the system uses IPMP, the operations are easier if 215 * they are serialized on a per IPMP group basis since IPMP operations 216 * happen across ill's of a group. Thus the lowest common denominator is to 217 * serialize most set ioctls, multicast join/leave operations, IPMP operations 218 * igmp/mld timer operations, and processing of DLPI control messages received 219 * from drivers on a per IPMP group basis. If the system does not employ 220 * IPMP the serialization is on a per phyint basis. This serialization is 221 * provided by the ipsq_t and primitives operating on this. Details can 222 * be found in ip_if.c above the core primitives operating on ipsq_t. 223 * 224 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 225 * Simiarly lookup of an ire by a thread also returns a refheld ire. 226 * In addition ipif's and ill's referenced by the ire are also indirectly 227 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 228 * the ipif's address or netmask change as long as an ipif is refheld 229 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the 230 * address of an ipif has to go through the ipsq_t. This ensures that only 231 * 1 such exclusive operation proceeds at any time on the ipif. It then 232 * deletes all ires associated with this ipif, and waits for all refcnts 233 * associated with this ipif to come down to zero. The address is changed 234 * only after the ipif has been quiesced. Then the ipif is brought up again. 235 * More details are described above the comment in ip_sioctl_flags. 236 * 237 * Packet processing is based mostly on IREs and are fully multi-threaded 238 * using standard Solaris MT techniques. 239 * 240 * There are explicit locks in IP to handle: 241 * - The ip_g_head list maintained by mi_open_link() and friends. 242 * 243 * - The reassembly data structures (one lock per hash bucket) 244 * 245 * - conn_lock is meant to protect conn_t fields. The fields actually 246 * protected by conn_lock are documented in the conn_t definition. 247 * 248 * - ire_lock to protect some of the fields of the ire, IRE tables 249 * (one lock per hash bucket). Refer to ip_ire.c for details. 250 * 251 * - ndp_g_lock and nce_lock for protecting NCEs. 252 * 253 * - ill_lock protects fields of the ill and ipif. Details in ip.h 254 * 255 * - ill_g_lock: This is a global reader/writer lock. Protects the following 256 * * The AVL tree based global multi list of all ills. 257 * * The linked list of all ipifs of an ill 258 * * The <ill-ipsq> mapping 259 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next 260 * * The illgroup list threaded by ill_group_next. 261 * * <ill-phyint> association 262 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 263 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion 264 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill 265 * will all have to hold the ill_g_lock as writer for the actual duration 266 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping 267 * may be found in the IPMP section. 268 * 269 * - ill_lock: This is a per ill mutex. 270 * It protects some members of the ill and is documented below. 271 * It also protects the <ill-ipsq> mapping 272 * It also protects the illgroup list threaded by ill_group_next. 273 * It also protects the <ill-phyint> assoc. 274 * It also protects the list of ipifs hanging off the ill. 275 * 276 * - ipsq_lock: This is a per ipsq_t mutex lock. 277 * This protects all the other members of the ipsq struct except 278 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock 279 * 280 * - illgrp_lock: This is a per ill_group mutex lock. 281 * The only thing it protects is the illgrp_ill_schednext member of ill_group 282 * which dictates which is the next ill in an ill_group that is to be chosen 283 * for sending outgoing packets, through creation of an IRE_CACHE that 284 * references this ill. 285 * 286 * - phyint_lock: This is a per phyint mutex lock. Protects just the 287 * phyint_flags 288 * 289 * - ip_g_nd_lock: This is a global reader/writer lock. 290 * Any call to nd_load to load a new parameter to the ND table must hold the 291 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 292 * as reader. 293 * 294 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 295 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 296 * uniqueness check also done atomically. 297 * 298 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 299 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 300 * as a writer when adding or deleting elements from these lists, and 301 * as a reader when walking these lists to send a SADB update to the 302 * IPsec capable ills. 303 * 304 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 305 * group list linked by ill_usesrc_grp_next. It also protects the 306 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 307 * group is being added or deleted. This lock is taken as a reader when 308 * walking the list/group(eg: to get the number of members in a usesrc group). 309 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 310 * field is changing state i.e from NULL to non-NULL or vice-versa. For 311 * example, it is not necessary to take this lock in the initial portion 312 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and 313 * ip_sioctl_flags since the these operations are executed exclusively and 314 * that ensures that the "usesrc group state" cannot change. The "usesrc 315 * group state" change can happen only in the latter part of 316 * ip_sioctl_slifusesrc and in ill_delete. 317 * 318 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications. 319 * 320 * To change the <ill-phyint> association, the ill_g_lock must be held 321 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 322 * must be held. 323 * 324 * To change the <ill-ipsq> association the ill_g_lock must be held as writer 325 * and the ill_lock of the ill in question must be held. 326 * 327 * To change the <ill-illgroup> association the ill_g_lock must be held as 328 * writer and the ill_lock of the ill in question must be held. 329 * 330 * To add or delete an ipif from the list of ipifs hanging off the ill, 331 * ill_g_lock (writer) and ill_lock must be held and the thread must be 332 * a writer on the associated ipsq,. 333 * 334 * To add or delete an ill to the system, the ill_g_lock must be held as 335 * writer and the thread must be a writer on the associated ipsq. 336 * 337 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 338 * must be a writer on the associated ipsq. 339 * 340 * Lock hierarchy 341 * 342 * Some lock hierarchy scenarios are listed below. 343 * 344 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 345 * ill_g_lock -> illgrp_lock -> ill_lock 346 * ill_g_lock -> ill_lock(s) -> phyint_lock 347 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 348 * ill_g_lock -> ip_addr_avail_lock 349 * conn_lock -> irb_lock -> ill_lock -> ire_lock 350 * ill_g_lock -> ip_g_nd_lock 351 * 352 * When more than 1 ill lock is needed to be held, all ill lock addresses 353 * are sorted on address and locked starting from highest addressed lock 354 * downward. 355 * 356 * Mobile-IP scenarios 357 * 358 * irb_lock -> ill_lock -> ire_mrtun_lock 359 * irb_lock -> ill_lock -> ire_srcif_table_lock 360 * 361 * IPsec scenarios 362 * 363 * ipsa_lock -> ill_g_lock -> ill_lock 364 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 365 * ipsec_capab_ills_lock -> ipsa_lock 366 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 367 * 368 * Trusted Solaris scenarios 369 * 370 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 371 * igsa_lock -> gcdb_lock 372 * gcgrp_rwlock -> ire_lock 373 * gcgrp_rwlock -> gcdb_lock 374 * 375 * 376 * Routing/forwarding table locking notes: 377 * 378 * Lock acquisition order: Radix tree lock, irb_lock. 379 * Requirements: 380 * i. Walker must not hold any locks during the walker callback. 381 * ii Walker must not see a truncated tree during the walk because of any node 382 * deletion. 383 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 384 * in many places in the code to walk the irb list. Thus even if all the 385 * ires in a bucket have been deleted, we still can't free the radix node 386 * until the ires have actually been inactive'd (freed). 387 * 388 * Tree traversal - Need to hold the global tree lock in read mode. 389 * Before dropping the global tree lock, need to either increment the ire_refcnt 390 * to ensure that the radix node can't be deleted. 391 * 392 * Tree add - Need to hold the global tree lock in write mode to add a 393 * radix node. To prevent the node from being deleted, increment the 394 * irb_refcnt, after the node is added to the tree. The ire itself is 395 * added later while holding the irb_lock, but not the tree lock. 396 * 397 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 398 * All associated ires must be inactive (i.e. freed), and irb_refcnt 399 * must be zero. 400 * 401 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 402 * global tree lock (read mode) for traversal. 403 * 404 * IPSEC notes : 405 * 406 * IP interacts with the IPSEC code (AH/ESP) by tagging a M_CTL message 407 * in front of the actual packet. For outbound datagrams, the M_CTL 408 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 409 * information used by the IPSEC code for applying the right level of 410 * protection. The information initialized by IP in the ipsec_out_t 411 * is determined by the per-socket policy or global policy in the system. 412 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 413 * ipsec_info.h) which starts out with nothing in it. It gets filled 414 * with the right information if it goes through the AH/ESP code, which 415 * happens if the incoming packet is secure. The information initialized 416 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 417 * the policy requirements needed by per-socket policy or global policy 418 * is met or not. 419 * 420 * If there is both per-socket policy (set using setsockopt) and there 421 * is also global policy match for the 5 tuples of the socket, 422 * ipsec_override_policy() makes the decision of which one to use. 423 * 424 * For fully connected sockets i.e dst, src [addr, port] is known, 425 * conn_policy_cached is set indicating that policy has been cached. 426 * conn_in_enforce_policy may or may not be set depending on whether 427 * there is a global policy match or per-socket policy match. 428 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 429 * Once the right policy is set on the conn_t, policy cannot change for 430 * this socket. This makes life simpler for TCP (UDP ?) where 431 * re-transmissions go out with the same policy. For symmetry, policy 432 * is cached for fully connected UDP sockets also. Thus if policy is cached, 433 * it also implies that policy is latched i.e policy cannot change 434 * on these sockets. As we have the right policy on the conn, we don't 435 * have to lookup global policy for every outbound and inbound datagram 436 * and thus serving as an optimization. Note that a global policy change 437 * does not affect fully connected sockets if they have policy. If fully 438 * connected sockets did not have any policy associated with it, global 439 * policy change may affect them. 440 * 441 * IP Flow control notes: 442 * 443 * Non-TCP streams are flow controlled by IP. On the send side, if the packet 444 * cannot be sent down to the driver by IP, because of a canput failure, IP 445 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. 446 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained 447 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the 448 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the 449 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains 450 * the queued messages, and removes the conn from the drain list, if all 451 * messages were drained. It also qenables the next conn in the drain list to 452 * continue the drain process. 453 * 454 * In reality the drain list is not a single list, but a configurable number 455 * of lists. The ip_wsrv on the IP module, qenables the first conn in each 456 * list. If the ip_wsrv of the next qenabled conn does not run, because the 457 * stream closes, ip_close takes responsibility to qenable the next conn in 458 * the drain list. The directly called ip_wput path always does a putq, if 459 * it cannot putnext. Thus synchronization problems are handled between 460 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 461 * functions that manipulate this drain list. Furthermore conn_drain_insert 462 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv 463 * running on a queue at any time. conn_drain_tail can be simultaneously called 464 * from both ip_wsrv and ip_close. 465 * 466 * IPQOS notes: 467 * 468 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 469 * and IPQoS modules. IPPF includes hooks in IP at different control points 470 * (callout positions) which direct packets to IPQoS modules for policy 471 * processing. Policies, if present, are global. 472 * 473 * The callout positions are located in the following paths: 474 * o local_in (packets destined for this host) 475 * o local_out (packets orginating from this host ) 476 * o fwd_in (packets forwarded by this m/c - inbound) 477 * o fwd_out (packets forwarded by this m/c - outbound) 478 * Hooks at these callout points can be enabled/disabled using the ndd variable 479 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 480 * By default all the callout positions are enabled. 481 * 482 * Outbound (local_out) 483 * Hooks are placed in ip_wput_ire and ipsec_out_process. 484 * 485 * Inbound (local_in) 486 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 487 * TCP and UDP fanout routines. 488 * 489 * Forwarding (in and out) 490 * Hooks are placed in ip_rput_forward and ip_mrtun_forward. 491 * 492 * IP Policy Framework processing (IPPF processing) 493 * Policy processing for a packet is initiated by ip_process, which ascertains 494 * that the classifier (ipgpc) is loaded and configured, failing which the 495 * packet resumes normal processing in IP. If the clasifier is present, the 496 * packet is acted upon by one or more IPQoS modules (action instances), per 497 * filters configured in ipgpc and resumes normal IP processing thereafter. 498 * An action instance can drop a packet in course of its processing. 499 * 500 * A boolean variable, ip_policy, is used in all the fanout routines that can 501 * invoke ip_process for a packet. This variable indicates if the packet should 502 * to be sent for policy processing. The variable is set to B_TRUE by default, 503 * i.e. when the routines are invoked in the normal ip procesing path for a 504 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 505 * ip_policy is set to B_FALSE for all the routines called in these two 506 * functions because, in the former case, we don't process loopback traffic 507 * currently while in the latter, the packets have already been processed in 508 * icmp_inbound. 509 * 510 * Zones notes: 511 * 512 * The partitioning rules for networking are as follows: 513 * 1) Packets coming from a zone must have a source address belonging to that 514 * zone. 515 * 2) Packets coming from a zone can only be sent on a physical interface on 516 * which the zone has an IP address. 517 * 3) Between two zones on the same machine, packet delivery is only allowed if 518 * there's a matching route for the destination and zone in the forwarding 519 * table. 520 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 521 * different zones can bind to the same port with the wildcard address 522 * (INADDR_ANY). 523 * 524 * The granularity of interface partitioning is at the logical interface level. 525 * Therefore, every zone has its own IP addresses, and incoming packets can be 526 * attributed to a zone unambiguously. A logical interface is placed into a zone 527 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 528 * structure. Rule (1) is implemented by modifying the source address selection 529 * algorithm so that the list of eligible addresses is filtered based on the 530 * sending process zone. 531 * 532 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 533 * across all zones, depending on their type. Here is the break-up: 534 * 535 * IRE type Shared/exclusive 536 * -------- ---------------- 537 * IRE_BROADCAST Exclusive 538 * IRE_DEFAULT (default routes) Shared (*) 539 * IRE_LOCAL Exclusive (x) 540 * IRE_LOOPBACK Exclusive 541 * IRE_PREFIX (net routes) Shared (*) 542 * IRE_CACHE Exclusive 543 * IRE_IF_NORESOLVER (interface routes) Exclusive 544 * IRE_IF_RESOLVER (interface routes) Exclusive 545 * IRE_HOST (host routes) Shared (*) 546 * 547 * (*) A zone can only use a default or off-subnet route if the gateway is 548 * directly reachable from the zone, that is, if the gateway's address matches 549 * one of the zone's logical interfaces. 550 * 551 * (x) IRE_LOCAL are handled a bit differently, since for all other entries 552 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source 553 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP 554 * address of the zone itself (the destination). Since IRE_LOCAL is used 555 * for communication between zones, ip_wput_ire has special logic to set 556 * the right source address when sending using an IRE_LOCAL. 557 * 558 * Furthermore, when ip_restrict_interzone_loopback is set (the default), 559 * ire_cache_lookup restricts loopback using an IRE_LOCAL 560 * between zone to the case when L2 would have conceptually looped the packet 561 * back, i.e. the loopback which is required since neither Ethernet drivers 562 * nor Ethernet hardware loops them back. This is the case when the normal 563 * routes (ignoring IREs with different zoneids) would send out the packet on 564 * the same ill (or ill group) as the ill with which is IRE_LOCAL is 565 * associated. 566 * 567 * Multiple zones can share a common broadcast address; typically all zones 568 * share the 255.255.255.255 address. Incoming as well as locally originated 569 * broadcast packets must be dispatched to all the zones on the broadcast 570 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 571 * since some zones may not be on the 10.16.72/24 network. To handle this, each 572 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 573 * sent to every zone that has an IRE_BROADCAST entry for the destination 574 * address on the input ill, see conn_wantpacket(). 575 * 576 * Applications in different zones can join the same multicast group address. 577 * For IPv4, group memberships are per-logical interface, so they're already 578 * inherently part of a zone. For IPv6, group memberships are per-physical 579 * interface, so we distinguish IPv6 group memberships based on group address, 580 * interface and zoneid. In both cases, received multicast packets are sent to 581 * every zone for which a group membership entry exists. On IPv6 we need to 582 * check that the target zone still has an address on the receiving physical 583 * interface; it could have been removed since the application issued the 584 * IPV6_JOIN_GROUP. 585 */ 586 587 /* 588 * Squeue Fanout flags: 589 * 0: No fanout. 590 * 1: Fanout across all squeues 591 */ 592 boolean_t ip_squeue_fanout = 0; 593 594 /* 595 * Maximum dups allowed per packet. 596 */ 597 uint_t ip_max_frag_dups = 10; 598 599 #define IS_SIMPLE_IPH(ipha) \ 600 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 601 602 /* RFC1122 Conformance */ 603 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 604 605 #define ILL_MAX_NAMELEN LIFNAMSIZ 606 607 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 608 609 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t); 610 static void ip_ipsec_out_prepend(mblk_t *, mblk_t *, ill_t *); 611 612 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t, 613 ip_stack_t *); 614 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 615 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 616 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); 617 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, 618 mblk_t *, int, ip_stack_t *); 619 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 620 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 621 ill_t *, zoneid_t); 622 static void icmp_options_update(ipha_t *); 623 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t, 624 ip_stack_t *); 625 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, 626 zoneid_t zoneid, ip_stack_t *); 627 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *); 628 static void icmp_redirect(ill_t *, mblk_t *); 629 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t, 630 ip_stack_t *); 631 632 static void ip_arp_news(queue_t *, mblk_t *); 633 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *, 634 ip_stack_t *); 635 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 636 char *ip_dot_addr(ipaddr_t, char *); 637 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 638 int ip_close(queue_t *, int); 639 static char *ip_dot_saddr(uchar_t *, char *); 640 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 641 boolean_t, boolean_t, ill_t *, zoneid_t); 642 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 643 boolean_t, boolean_t, zoneid_t); 644 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 645 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 646 static void ip_lrput(queue_t *, mblk_t *); 647 static void ip_mrtun_forward(ire_t *, ill_t *, mblk_t *); 648 ipaddr_t ip_net_mask(ipaddr_t); 649 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, ill_t *, conn_t *, 650 zoneid_t, ip_stack_t *); 651 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 652 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *); 653 char *ip_nv_lookup(nv_t *, int); 654 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 655 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 656 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 657 static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t, 658 ipndp_t *, size_t); 659 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 660 void ip_rput(queue_t *, mblk_t *); 661 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 662 void *dummy_arg); 663 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 664 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *, 665 ip_stack_t *); 666 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 667 ire_t *, ip_stack_t *); 668 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *, 669 mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *); 670 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *, 671 ip_stack_t *); 672 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, 673 uint16_t *); 674 int ip_snmp_get(queue_t *, mblk_t *); 675 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, 676 mib2_ipIfStatsEntry_t *, ip_stack_t *); 677 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *, 678 ip_stack_t *); 679 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *); 680 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst); 681 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst); 682 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst); 683 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst); 684 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *, 685 ip_stack_t *ipst); 686 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *, 687 ip_stack_t *ipst); 688 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *, 689 ip_stack_t *ipst); 690 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *, 691 ip_stack_t *ipst); 692 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *, 693 ip_stack_t *ipst); 694 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *, 695 ip_stack_t *ipst); 696 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *, 697 ip_stack_t *ipst); 698 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *, 699 ip_stack_t *ipst); 700 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, 701 ip_stack_t *ipst); 702 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, 703 ip_stack_t *ipst); 704 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 705 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 706 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); 707 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 708 static boolean_t ip_source_routed(ipha_t *, ip_stack_t *); 709 static boolean_t ip_source_route_included(ipha_t *); 710 static void ip_trash_ire_reclaim_stack(ip_stack_t *); 711 712 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, 713 zoneid_t, ip_stack_t *); 714 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *); 715 static void ip_wput_local_options(ipha_t *, ip_stack_t *); 716 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 717 zoneid_t, ip_stack_t *); 718 719 static void conn_drain_init(ip_stack_t *); 720 static void conn_drain_fini(ip_stack_t *); 721 static void conn_drain_tail(conn_t *connp, boolean_t closing); 722 723 static void conn_walk_drain(ip_stack_t *); 724 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, 725 zoneid_t); 726 727 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns); 728 static void ip_stack_shutdown(netstackid_t stackid, void *arg); 729 static void ip_stack_fini(netstackid_t stackid, void *arg); 730 731 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 732 zoneid_t); 733 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 734 void *dummy_arg); 735 736 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 737 738 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 739 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 740 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 741 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 742 743 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 744 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 745 caddr_t, cred_t *); 746 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, 747 caddr_t cp, cred_t *cr); 748 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, 749 cred_t *); 750 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 751 caddr_t cp, cred_t *cr); 752 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 753 cred_t *); 754 static squeue_func_t ip_squeue_switch(int); 755 756 static void *ip_kstat_init(netstackid_t, ip_stack_t *); 757 static void ip_kstat_fini(netstackid_t, kstat_t *); 758 static int ip_kstat_update(kstat_t *kp, int rw); 759 static void *icmp_kstat_init(netstackid_t); 760 static void icmp_kstat_fini(netstackid_t, kstat_t *); 761 static int icmp_kstat_update(kstat_t *kp, int rw); 762 static void *ip_kstat2_init(netstackid_t, ip_stat_t *); 763 static void ip_kstat2_fini(netstackid_t, kstat_t *); 764 765 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); 766 767 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 768 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 769 770 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 771 ipha_t *, ill_t *, boolean_t); 772 773 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 774 ipha_t *, ill_t *, boolean_t); 775 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 776 777 /* How long, in seconds, we allow frags to hang around. */ 778 #define IP_FRAG_TIMEOUT 60 779 780 /* 781 * Threshold which determines whether MDT should be used when 782 * generating IP fragments; payload size must be greater than 783 * this threshold for MDT to take place. 784 */ 785 #define IP_WPUT_FRAG_MDT_MIN 32768 786 787 /* Setable in /etc/system only */ 788 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 789 790 static long ip_rput_pullups; 791 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 792 793 vmem_t *ip_minor_arena; 794 795 int ip_debug; 796 797 #ifdef DEBUG 798 uint32_t ipsechw_debug = 0; 799 #endif 800 801 /* 802 * Multirouting/CGTP stuff 803 */ 804 cgtp_filter_ops_t *ip_cgtp_filter_ops; /* CGTP hooks */ 805 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 806 boolean_t ip_cgtp_filter; /* Enable/disable CGTP hooks */ 807 808 /* 809 * XXX following really should only be in a header. Would need more 810 * header and .c clean up first. 811 */ 812 extern optdb_obj_t ip_opt_obj; 813 814 ulong_t ip_squeue_enter_unbound = 0; 815 816 /* 817 * Named Dispatch Parameter Table. 818 * All of these are alterable, within the min/max values given, at run time. 819 */ 820 static ipparam_t lcl_param_arr[] = { 821 /* min max value name */ 822 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 823 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 824 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 825 { 0, 1, 0, "ip_respond_to_timestamp"}, 826 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 827 { 0, 1, 1, "ip_send_redirects"}, 828 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 829 { 0, 10, 0, "ip_debug"}, 830 { 0, 10, 0, "ip_mrtdebug"}, 831 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 832 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 833 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 834 { 1, 255, 255, "ip_def_ttl" }, 835 { 0, 1, 0, "ip_forward_src_routed"}, 836 { 0, 256, 32, "ip_wroff_extra" }, 837 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 838 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 839 { 0, 1, 1, "ip_path_mtu_discovery" }, 840 { 0, 240, 30, "ip_ignore_delete_time" }, 841 { 0, 1, 0, "ip_ignore_redirect" }, 842 { 0, 1, 1, "ip_output_queue" }, 843 { 1, 254, 1, "ip_broadcast_ttl" }, 844 { 0, 99999, 100, "ip_icmp_err_interval" }, 845 { 1, 99999, 10, "ip_icmp_err_burst" }, 846 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 847 { 0, 1, 0, "ip_strict_dst_multihoming" }, 848 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 849 { 0, 1, 0, "ipsec_override_persocket_policy" }, 850 { 0, 1, 1, "icmp_accept_clear_messages" }, 851 { 0, 1, 1, "igmp_accept_clear_messages" }, 852 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 853 "ip_ndp_delay_first_probe_time"}, 854 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 855 "ip_ndp_max_unicast_solicit"}, 856 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 857 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 858 { 0, 1, 0, "ip6_forward_src_routed"}, 859 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 860 { 0, 1, 1, "ip6_send_redirects"}, 861 { 0, 1, 0, "ip6_ignore_redirect" }, 862 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 863 864 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 865 866 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 867 868 { 0, 1, 1, "pim_accept_clear_messages" }, 869 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 870 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 871 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 872 { 0, 15, 0, "ip_policy_mask" }, 873 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 874 { 0, 255, 1, "ip_multirt_ttl" }, 875 { 0, 1, 1, "ip_multidata_outbound" }, 876 { 0, 3600000, 300000, "ip_ndp_defense_interval" }, 877 { 0, 999999, 60*60*24, "ip_max_temp_idle" }, 878 { 0, 1000, 1, "ip_max_temp_defend" }, 879 { 0, 1000, 3, "ip_max_defend" }, 880 { 0, 999999, 30, "ip_defend_interval" }, 881 { 0, 3600000, 300000, "ip_dup_recovery" }, 882 { 0, 1, 1, "ip_restrict_interzone_loopback" }, 883 { 0, 1, 1, "ip_lso_outbound" }, 884 #ifdef DEBUG 885 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 886 #else 887 { 0, 0, 0, "" }, 888 #endif 889 }; 890 891 /* 892 * Extended NDP table 893 * The addresses for the first two are filled in to be ips_ip_g_forward 894 * and ips_ipv6_forward at init time. 895 */ 896 static ipndp_t lcl_ndp_arr[] = { 897 /* getf setf data name */ 898 #define IPNDP_IP_FORWARDING_OFFSET 0 899 { ip_param_generic_get, ip_forward_set, NULL, 900 "ip_forwarding" }, 901 #define IPNDP_IP6_FORWARDING_OFFSET 1 902 { ip_param_generic_get, ip_forward_set, NULL, 903 "ip6_forwarding" }, 904 { ip_ill_report, NULL, NULL, 905 "ip_ill_status" }, 906 { ip_ipif_report, NULL, NULL, 907 "ip_ipif_status" }, 908 { ip_ire_report, NULL, NULL, 909 "ipv4_ire_status" }, 910 { ip_ire_report_mrtun, NULL, NULL, 911 "ipv4_mrtun_ire_status" }, 912 { ip_ire_report_srcif, NULL, NULL, 913 "ipv4_srcif_ire_status" }, 914 { ip_ire_report_v6, NULL, NULL, 915 "ipv6_ire_status" }, 916 { ip_conn_report, NULL, NULL, 917 "ip_conn_status" }, 918 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, 919 "ip_rput_pullups" }, 920 { ndp_report, NULL, NULL, 921 "ip_ndp_cache_report" }, 922 { ip_srcid_report, NULL, NULL, 923 "ip_srcid_status" }, 924 { ip_param_generic_get, ip_squeue_profile_set, 925 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, 926 { ip_param_generic_get, ip_squeue_bind_set, 927 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, 928 { ip_param_generic_get, ip_input_proc_set, 929 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 930 { ip_param_generic_get, ip_int_set, 931 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 932 #define IPNDP_CGTP_FILTER_OFFSET 16 933 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL, 934 "ip_cgtp_filter" }, 935 { ip_param_generic_get, ip_int_set, 936 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" }, 937 }; 938 939 /* 940 * Table of IP ioctls encoding the various properties of the ioctl and 941 * indexed based on the last byte of the ioctl command. Occasionally there 942 * is a clash, and there is more than 1 ioctl with the same last byte. 943 * In such a case 1 ioctl is encoded in the ndx table and the remaining 944 * ioctls are encoded in the misc table. An entry in the ndx table is 945 * retrieved by indexing on the last byte of the ioctl command and comparing 946 * the ioctl command with the value in the ndx table. In the event of a 947 * mismatch the misc table is then searched sequentially for the desired 948 * ioctl command. 949 * 950 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 951 */ 952 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 953 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 954 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 955 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 956 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 957 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 958 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 959 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 960 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 961 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 962 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 963 964 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 965 MISC_CMD, ip_siocaddrt, NULL }, 966 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 967 MISC_CMD, ip_siocdelrt, NULL }, 968 969 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 970 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 971 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 972 IF_CMD, ip_sioctl_get_addr, NULL }, 973 974 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 975 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 976 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 977 IPI_GET_CMD | IPI_REPL, 978 IF_CMD, ip_sioctl_get_dstaddr, NULL }, 979 980 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 981 IPI_PRIV | IPI_WR | IPI_REPL, 982 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 983 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 984 IPI_MODOK | IPI_GET_CMD | IPI_REPL, 985 IF_CMD, ip_sioctl_get_flags, NULL }, 986 987 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 988 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 989 990 /* copyin size cannot be coded for SIOCGIFCONF */ 991 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 992 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 993 994 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 995 IF_CMD, ip_sioctl_mtu, NULL }, 996 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 997 IF_CMD, ip_sioctl_get_mtu, NULL }, 998 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 999 IPI_GET_CMD | IPI_REPL, 1000 IF_CMD, ip_sioctl_get_brdaddr, NULL }, 1001 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1002 IF_CMD, ip_sioctl_brdaddr, NULL }, 1003 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 1004 IPI_GET_CMD | IPI_REPL, 1005 IF_CMD, ip_sioctl_get_netmask, NULL }, 1006 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1007 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1008 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 1009 IPI_GET_CMD | IPI_REPL, 1010 IF_CMD, ip_sioctl_get_metric, NULL }, 1011 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 1012 IF_CMD, ip_sioctl_metric, NULL }, 1013 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1014 1015 /* See 166-168 below for extended SIOC*XARP ioctls */ 1016 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV, 1017 MISC_CMD, ip_sioctl_arp, NULL }, 1018 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, 1019 MISC_CMD, ip_sioctl_arp, NULL }, 1020 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, 1021 MISC_CMD, ip_sioctl_arp, NULL }, 1022 1023 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1024 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1025 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1026 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1027 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1028 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1029 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1030 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1031 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1032 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1033 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1034 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1035 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1036 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1037 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1038 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1039 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1040 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1041 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1042 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1043 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1044 1045 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 1046 MISC_CMD, if_unitsel, if_unitsel_restart }, 1047 1048 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1049 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1050 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1051 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1052 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1053 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1054 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1055 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1056 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1057 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1058 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1059 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1060 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1061 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1062 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1063 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1064 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1065 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1066 1067 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 1068 IPI_PRIV | IPI_WR | IPI_MODOK, 1069 IF_CMD, ip_sioctl_sifname, NULL }, 1070 1071 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1072 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1073 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1074 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1075 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1076 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1077 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1078 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1079 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1080 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1081 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1082 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1083 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1084 1085 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL, 1086 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 1087 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1088 IF_CMD, ip_sioctl_get_muxid, NULL }, 1089 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 1090 IPI_PRIV | IPI_WR | IPI_REPL, 1091 IF_CMD, ip_sioctl_muxid, NULL }, 1092 1093 /* Both if and lif variants share same func */ 1094 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1095 IF_CMD, ip_sioctl_get_lifindex, NULL }, 1096 /* Both if and lif variants share same func */ 1097 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 1098 IPI_PRIV | IPI_WR | IPI_REPL, 1099 IF_CMD, ip_sioctl_slifindex, NULL }, 1100 1101 /* copyin size cannot be coded for SIOCGIFCONF */ 1102 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1103 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1104 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1105 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1106 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1107 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1108 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1109 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1110 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1111 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1112 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1113 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1114 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1115 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1116 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1117 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1118 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1119 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1120 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1121 1122 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 1123 IPI_PRIV | IPI_WR | IPI_REPL, 1124 LIF_CMD, ip_sioctl_removeif, 1125 ip_sioctl_removeif_restart }, 1126 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 1127 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL, 1128 LIF_CMD, ip_sioctl_addif, NULL }, 1129 #define SIOCLIFADDR_NDX 112 1130 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1131 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1132 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 1133 IPI_GET_CMD | IPI_REPL, 1134 LIF_CMD, ip_sioctl_get_addr, NULL }, 1135 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1136 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1137 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 1138 IPI_GET_CMD | IPI_REPL, 1139 LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 1140 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 1141 IPI_PRIV | IPI_WR | IPI_REPL, 1142 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1143 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 1144 IPI_GET_CMD | IPI_MODOK | IPI_REPL, 1145 LIF_CMD, ip_sioctl_get_flags, NULL }, 1146 1147 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1148 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1149 1150 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1151 ip_sioctl_get_lifconf, NULL }, 1152 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1153 LIF_CMD, ip_sioctl_mtu, NULL }, 1154 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, 1155 LIF_CMD, ip_sioctl_get_mtu, NULL }, 1156 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 1157 IPI_GET_CMD | IPI_REPL, 1158 LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 1159 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1160 LIF_CMD, ip_sioctl_brdaddr, NULL }, 1161 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 1162 IPI_GET_CMD | IPI_REPL, 1163 LIF_CMD, ip_sioctl_get_netmask, NULL }, 1164 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1165 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1166 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 1167 IPI_GET_CMD | IPI_REPL, 1168 LIF_CMD, ip_sioctl_get_metric, NULL }, 1169 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1170 LIF_CMD, ip_sioctl_metric, NULL }, 1171 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1172 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL, 1173 LIF_CMD, ip_sioctl_slifname, 1174 ip_sioctl_slifname_restart }, 1175 1176 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL, 1177 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1178 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1179 IPI_GET_CMD | IPI_REPL, 1180 LIF_CMD, ip_sioctl_get_muxid, NULL }, 1181 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1182 IPI_PRIV | IPI_WR | IPI_REPL, 1183 LIF_CMD, ip_sioctl_muxid, NULL }, 1184 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1185 IPI_GET_CMD | IPI_REPL, 1186 LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1187 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1188 IPI_PRIV | IPI_WR | IPI_REPL, 1189 LIF_CMD, ip_sioctl_slifindex, 0 }, 1190 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1191 LIF_CMD, ip_sioctl_token, NULL }, 1192 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1193 IPI_GET_CMD | IPI_REPL, 1194 LIF_CMD, ip_sioctl_get_token, NULL }, 1195 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1196 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1197 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1198 IPI_GET_CMD | IPI_REPL, 1199 LIF_CMD, ip_sioctl_get_subnet, NULL }, 1200 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1201 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1202 1203 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1204 IPI_GET_CMD | IPI_REPL, 1205 LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1206 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1207 LIF_CMD, ip_siocdelndp_v6, NULL }, 1208 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1209 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1210 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1211 LIF_CMD, ip_siocsetndp_v6, NULL }, 1212 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1213 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1214 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1215 MISC_CMD, ip_sioctl_tonlink, NULL }, 1216 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1217 MISC_CMD, ip_sioctl_tmysite, NULL }, 1218 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL, 1219 TUN_CMD, ip_sioctl_tunparam, NULL }, 1220 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), 1221 IPI_PRIV | IPI_WR, 1222 TUN_CMD, ip_sioctl_tunparam, NULL }, 1223 1224 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1225 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1226 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1227 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1228 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1229 1230 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq), 1231 IPI_PRIV | IPI_WR | IPI_REPL, 1232 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1233 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq), 1234 IPI_PRIV | IPI_WR | IPI_REPL, 1235 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1236 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1237 IPI_PRIV | IPI_WR, 1238 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1239 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1240 IPI_GET_CMD | IPI_REPL, 1241 LIF_CMD, ip_sioctl_get_groupname, NULL }, 1242 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq), 1243 IPI_GET_CMD | IPI_REPL, 1244 LIF_CMD, ip_sioctl_get_oindex, NULL }, 1245 1246 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1247 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1248 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1249 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1250 1251 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1252 LIF_CMD, ip_sioctl_slifoindex, NULL }, 1253 1254 /* These are handled in ip_sioctl_copyin_setup itself */ 1255 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1256 MISC_CMD, NULL, NULL }, 1257 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1258 MISC_CMD, NULL, NULL }, 1259 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1260 1261 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1262 ip_sioctl_get_lifconf, NULL }, 1263 1264 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, 1265 MISC_CMD, ip_sioctl_xarp, NULL }, 1266 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, 1267 MISC_CMD, ip_sioctl_xarp, NULL }, 1268 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, 1269 MISC_CMD, ip_sioctl_xarp, NULL }, 1270 1271 /* SIOCPOPSOCKFS is not handled by IP */ 1272 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1273 1274 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1275 IPI_GET_CMD | IPI_REPL, 1276 LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1277 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1278 IPI_PRIV | IPI_WR | IPI_REPL, 1279 LIF_CMD, ip_sioctl_slifzone, 1280 ip_sioctl_slifzone_restart }, 1281 /* 172-174 are SCTP ioctls and not handled by IP */ 1282 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1283 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1284 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1285 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1286 IPI_GET_CMD, LIF_CMD, 1287 ip_sioctl_get_lifusesrc, 0 }, 1288 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1289 IPI_PRIV | IPI_WR, 1290 LIF_CMD, ip_sioctl_slifusesrc, 1291 NULL }, 1292 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1293 ip_sioctl_get_lifsrcof, NULL }, 1294 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1295 MISC_CMD, ip_sioctl_msfilter, NULL }, 1296 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1297 MISC_CMD, ip_sioctl_msfilter, NULL }, 1298 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1299 MISC_CMD, ip_sioctl_msfilter, NULL }, 1300 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1301 MISC_CMD, ip_sioctl_msfilter, NULL }, 1302 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD, 1303 ip_sioctl_set_ipmpfailback, NULL } 1304 }; 1305 1306 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1307 1308 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1309 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), 1310 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, 1311 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, 1312 TUN_CMD, ip_sioctl_tunparam, NULL }, 1313 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1314 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1315 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1316 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1317 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, 1318 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1319 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1320 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD, 1321 MISC_CMD, mrt_ioctl}, 1322 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD, 1323 MISC_CMD, mrt_ioctl}, 1324 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD, 1325 MISC_CMD, mrt_ioctl} 1326 }; 1327 1328 int ip_misc_ioctl_count = 1329 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1330 1331 int conn_drain_nthreads; /* Number of drainers reqd. */ 1332 /* Settable in /etc/system */ 1333 /* Defined in ip_ire.c */ 1334 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1335 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1336 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1337 1338 static nv_t ire_nv_arr[] = { 1339 { IRE_BROADCAST, "BROADCAST" }, 1340 { IRE_LOCAL, "LOCAL" }, 1341 { IRE_LOOPBACK, "LOOPBACK" }, 1342 { IRE_CACHE, "CACHE" }, 1343 { IRE_DEFAULT, "DEFAULT" }, 1344 { IRE_PREFIX, "PREFIX" }, 1345 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1346 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1347 { IRE_HOST, "HOST" }, 1348 { 0 } 1349 }; 1350 1351 nv_t *ire_nv_tbl = ire_nv_arr; 1352 1353 /* Defined in ip_netinfo.c */ 1354 extern ddi_taskq_t *eventq_queue_nic; 1355 1356 /* Simple ICMP IP Header Template */ 1357 static ipha_t icmp_ipha = { 1358 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1359 }; 1360 1361 struct module_info ip_mod_info = { 1362 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024 1363 }; 1364 1365 /* 1366 * Duplicate static symbols within a module confuses mdb; so we avoid the 1367 * problem by making the symbols here distinct from those in udp.c. 1368 */ 1369 1370 static struct qinit iprinit = { 1371 (pfi_t)ip_rput, NULL, ip_open, ip_close, NULL, 1372 &ip_mod_info 1373 }; 1374 1375 static struct qinit ipwinit = { 1376 (pfi_t)ip_wput, (pfi_t)ip_wsrv, ip_open, ip_close, NULL, 1377 &ip_mod_info 1378 }; 1379 1380 static struct qinit iplrinit = { 1381 (pfi_t)ip_lrput, NULL, ip_open, ip_close, NULL, 1382 &ip_mod_info 1383 }; 1384 1385 static struct qinit iplwinit = { 1386 (pfi_t)ip_lwput, NULL, ip_open, ip_close, NULL, 1387 &ip_mod_info 1388 }; 1389 1390 struct streamtab ipinfo = { 1391 &iprinit, &ipwinit, &iplrinit, &iplwinit 1392 }; 1393 1394 #ifdef DEBUG 1395 static boolean_t skip_sctp_cksum = B_FALSE; 1396 #endif 1397 1398 /* 1399 * Prepend the zoneid using an ipsec_out_t for later use by functions like 1400 * ip_rput_v6(), ip_output(), etc. If the message 1401 * block already has a M_CTL at the front of it, then simply set the zoneid 1402 * appropriately. 1403 */ 1404 mblk_t * 1405 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst) 1406 { 1407 mblk_t *first_mp; 1408 ipsec_out_t *io; 1409 1410 ASSERT(zoneid != ALL_ZONES); 1411 if (mp->b_datap->db_type == M_CTL) { 1412 io = (ipsec_out_t *)mp->b_rptr; 1413 ASSERT(io->ipsec_out_type == IPSEC_OUT); 1414 io->ipsec_out_zoneid = zoneid; 1415 return (mp); 1416 } 1417 1418 first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack); 1419 if (first_mp == NULL) 1420 return (NULL); 1421 io = (ipsec_out_t *)first_mp->b_rptr; 1422 /* This is not a secure packet */ 1423 io->ipsec_out_secure = B_FALSE; 1424 io->ipsec_out_zoneid = zoneid; 1425 first_mp->b_cont = mp; 1426 return (first_mp); 1427 } 1428 1429 /* 1430 * Copy an M_CTL-tagged message, preserving reference counts appropriately. 1431 */ 1432 mblk_t * 1433 ip_copymsg(mblk_t *mp) 1434 { 1435 mblk_t *nmp; 1436 ipsec_info_t *in; 1437 1438 if (mp->b_datap->db_type != M_CTL) 1439 return (copymsg(mp)); 1440 1441 in = (ipsec_info_t *)mp->b_rptr; 1442 1443 /* 1444 * Note that M_CTL is also used for delivering ICMP error messages 1445 * upstream to transport layers. 1446 */ 1447 if (in->ipsec_info_type != IPSEC_OUT && 1448 in->ipsec_info_type != IPSEC_IN) 1449 return (copymsg(mp)); 1450 1451 nmp = copymsg(mp->b_cont); 1452 1453 if (in->ipsec_info_type == IPSEC_OUT) { 1454 return (ipsec_out_tag(mp, nmp, 1455 ((ipsec_out_t *)in)->ipsec_out_ns)); 1456 } else { 1457 return (ipsec_in_tag(mp, nmp, 1458 ((ipsec_in_t *)in)->ipsec_in_ns)); 1459 } 1460 } 1461 1462 /* Generate an ICMP fragmentation needed message. */ 1463 static void 1464 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid, 1465 ip_stack_t *ipst) 1466 { 1467 icmph_t icmph; 1468 mblk_t *first_mp; 1469 boolean_t mctl_present; 1470 1471 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1472 1473 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 1474 if (mctl_present) 1475 freeb(first_mp); 1476 return; 1477 } 1478 1479 bzero(&icmph, sizeof (icmph_t)); 1480 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1481 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1482 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1483 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded); 1484 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 1485 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 1486 ipst); 1487 } 1488 1489 /* 1490 * icmp_inbound deals with ICMP messages in the following ways. 1491 * 1492 * 1) It needs to send a reply back and possibly delivering it 1493 * to the "interested" upper clients. 1494 * 2) It needs to send it to the upper clients only. 1495 * 3) It needs to change some values in IP only. 1496 * 4) It needs to change some values in IP and upper layers e.g TCP. 1497 * 1498 * We need to accomodate icmp messages coming in clear until we get 1499 * everything secure from the wire. If icmp_accept_clear_messages 1500 * is zero we check with the global policy and act accordingly. If 1501 * it is non-zero, we accept the message without any checks. But 1502 * *this does not mean* that this will be delivered to the upper 1503 * clients. By accepting we might send replies back, change our MTU 1504 * value etc. but delivery to the ULP/clients depends on their policy 1505 * dispositions. 1506 * 1507 * We handle the above 4 cases in the context of IPSEC in the 1508 * following way : 1509 * 1510 * 1) Send the reply back in the same way as the request came in. 1511 * If it came in encrypted, it goes out encrypted. If it came in 1512 * clear, it goes out in clear. Thus, this will prevent chosen 1513 * plain text attack. 1514 * 2) The client may or may not expect things to come in secure. 1515 * If it comes in secure, the policy constraints are checked 1516 * before delivering it to the upper layers. If it comes in 1517 * clear, ipsec_inbound_accept_clear will decide whether to 1518 * accept this in clear or not. In both the cases, if the returned 1519 * message (IP header + 8 bytes) that caused the icmp message has 1520 * AH/ESP headers, it is sent up to AH/ESP for validation before 1521 * sending up. If there are only 8 bytes of returned message, then 1522 * upper client will not be notified. 1523 * 3) Check with global policy to see whether it matches the constaints. 1524 * But this will be done only if icmp_accept_messages_in_clear is 1525 * zero. 1526 * 4) If we need to change both in IP and ULP, then the decision taken 1527 * while affecting the values in IP and while delivering up to TCP 1528 * should be the same. 1529 * 1530 * There are two cases. 1531 * 1532 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1533 * failed), we will not deliver it to the ULP, even though they 1534 * are *willing* to accept in *clear*. This is fine as our global 1535 * disposition to icmp messages asks us reject the datagram. 1536 * 1537 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1538 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1539 * to deliver it to ULP (policy failed), it can lead to 1540 * consistency problems. The cases known at this time are 1541 * ICMP_DESTINATION_UNREACHABLE messages with following code 1542 * values : 1543 * 1544 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1545 * and Upper layer rejects. Then the communication will 1546 * come to a stop. This is solved by making similar decisions 1547 * at both levels. Currently, when we are unable to deliver 1548 * to the Upper Layer (due to policy failures) while IP has 1549 * adjusted ire_max_frag, the next outbound datagram would 1550 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1551 * will be with the right level of protection. Thus the right 1552 * value will be communicated even if we are not able to 1553 * communicate when we get from the wire initially. But this 1554 * assumes there would be at least one outbound datagram after 1555 * IP has adjusted its ire_max_frag value. To make things 1556 * simpler, we accept in clear after the validation of 1557 * AH/ESP headers. 1558 * 1559 * - Other ICMP ERRORS : We may not be able to deliver it to the 1560 * upper layer depending on the level of protection the upper 1561 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1562 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1563 * should be accepted in clear when the Upper layer expects secure. 1564 * Thus the communication may get aborted by some bad ICMP 1565 * packets. 1566 * 1567 * IPQoS Notes: 1568 * The only instance when a packet is sent for processing is when there 1569 * isn't an ICMP client and if we are interested in it. 1570 * If there is a client, IPPF processing will take place in the 1571 * ip_fanout_proto routine. 1572 * 1573 * Zones notes: 1574 * The packet is only processed in the context of the specified zone: typically 1575 * only this zone will reply to an echo request, and only interested clients in 1576 * this zone will receive a copy of the packet. This means that the caller must 1577 * call icmp_inbound() for each relevant zone. 1578 */ 1579 static void 1580 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, 1581 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, 1582 ill_t *recv_ill, zoneid_t zoneid) 1583 { 1584 icmph_t *icmph; 1585 ipha_t *ipha; 1586 int iph_hdr_length; 1587 int hdr_length; 1588 boolean_t interested; 1589 uint32_t ts; 1590 uchar_t *wptr; 1591 ipif_t *ipif; 1592 mblk_t *first_mp; 1593 ipsec_in_t *ii; 1594 ire_t *src_ire; 1595 boolean_t onlink; 1596 timestruc_t now; 1597 uint32_t ill_index; 1598 ip_stack_t *ipst; 1599 1600 ASSERT(ill != NULL); 1601 ipst = ill->ill_ipst; 1602 1603 first_mp = mp; 1604 if (mctl_present) { 1605 mp = first_mp->b_cont; 1606 ASSERT(mp != NULL); 1607 } 1608 1609 ipha = (ipha_t *)mp->b_rptr; 1610 if (ipst->ips_icmp_accept_clear_messages == 0) { 1611 first_mp = ipsec_check_global_policy(first_mp, NULL, 1612 ipha, NULL, mctl_present, ipst->ips_netstack); 1613 if (first_mp == NULL) 1614 return; 1615 } 1616 1617 /* 1618 * On a labeled system, we have to check whether the zone itself is 1619 * permitted to receive raw traffic. 1620 */ 1621 if (is_system_labeled()) { 1622 if (zoneid == ALL_ZONES) 1623 zoneid = tsol_packet_to_zoneid(mp); 1624 if (!tsol_can_accept_raw(mp, B_FALSE)) { 1625 ip1dbg(("icmp_inbound: zone %d can't receive raw", 1626 zoneid)); 1627 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1628 freemsg(first_mp); 1629 return; 1630 } 1631 } 1632 1633 /* 1634 * We have accepted the ICMP message. It means that we will 1635 * respond to the packet if needed. It may not be delivered 1636 * to the upper client depending on the policy constraints 1637 * and the disposition in ipsec_inbound_accept_clear. 1638 */ 1639 1640 ASSERT(ill != NULL); 1641 1642 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs); 1643 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1644 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { 1645 /* Last chance to get real. */ 1646 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { 1647 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1648 freemsg(first_mp); 1649 return; 1650 } 1651 /* Refresh iph following the pullup. */ 1652 ipha = (ipha_t *)mp->b_rptr; 1653 } 1654 /* ICMP header checksum, including checksum field, should be zero. */ 1655 if (sum_valid ? (sum != 0 && sum != 0xFFFF) : 1656 IP_CSUM(mp, iph_hdr_length, 0)) { 1657 BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs); 1658 freemsg(first_mp); 1659 return; 1660 } 1661 /* The IP header will always be a multiple of four bytes */ 1662 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1663 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, 1664 icmph->icmph_code)); 1665 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1666 /* We will set "interested" to "true" if we want a copy */ 1667 interested = B_FALSE; 1668 switch (icmph->icmph_type) { 1669 case ICMP_ECHO_REPLY: 1670 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps); 1671 break; 1672 case ICMP_DEST_UNREACHABLE: 1673 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1674 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded); 1675 interested = B_TRUE; /* Pass up to transport */ 1676 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs); 1677 break; 1678 case ICMP_SOURCE_QUENCH: 1679 interested = B_TRUE; /* Pass up to transport */ 1680 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs); 1681 break; 1682 case ICMP_REDIRECT: 1683 if (!ipst->ips_ip_ignore_redirect) 1684 interested = B_TRUE; 1685 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects); 1686 break; 1687 case ICMP_ECHO_REQUEST: 1688 /* 1689 * Whether to respond to echo requests that come in as IP 1690 * broadcasts or as IP multicast is subject to debate 1691 * (what isn't?). We aim to please, you pick it. 1692 * Default is do it. 1693 */ 1694 if (!broadcast && !CLASSD(ipha->ipha_dst)) { 1695 /* unicast: always respond */ 1696 interested = B_TRUE; 1697 } else if (CLASSD(ipha->ipha_dst)) { 1698 /* multicast: respond based on tunable */ 1699 interested = ipst->ips_ip_g_resp_to_echo_mcast; 1700 } else if (broadcast) { 1701 /* broadcast: respond based on tunable */ 1702 interested = ipst->ips_ip_g_resp_to_echo_bcast; 1703 } 1704 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos); 1705 break; 1706 case ICMP_ROUTER_ADVERTISEMENT: 1707 case ICMP_ROUTER_SOLICITATION: 1708 break; 1709 case ICMP_TIME_EXCEEDED: 1710 interested = B_TRUE; /* Pass up to transport */ 1711 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds); 1712 break; 1713 case ICMP_PARAM_PROBLEM: 1714 interested = B_TRUE; /* Pass up to transport */ 1715 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs); 1716 break; 1717 case ICMP_TIME_STAMP_REQUEST: 1718 /* Response to Time Stamp Requests is local policy. */ 1719 if (ipst->ips_ip_g_resp_to_timestamp && 1720 /* So is whether to respond if it was an IP broadcast. */ 1721 (!broadcast || ipst->ips_ip_g_resp_to_timestamp_bcast)) { 1722 int tstamp_len = 3 * sizeof (uint32_t); 1723 1724 if (wptr + tstamp_len > mp->b_wptr) { 1725 if (!pullupmsg(mp, wptr + tstamp_len - 1726 mp->b_rptr)) { 1727 BUMP_MIB(ill->ill_ip_mib, 1728 ipIfStatsInDiscards); 1729 freemsg(first_mp); 1730 return; 1731 } 1732 /* Refresh ipha following the pullup. */ 1733 ipha = (ipha_t *)mp->b_rptr; 1734 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1735 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1736 } 1737 interested = B_TRUE; 1738 } 1739 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps); 1740 break; 1741 case ICMP_TIME_STAMP_REPLY: 1742 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps); 1743 break; 1744 case ICMP_INFO_REQUEST: 1745 /* Per RFC 1122 3.2.2.7, ignore this. */ 1746 case ICMP_INFO_REPLY: 1747 break; 1748 case ICMP_ADDRESS_MASK_REQUEST: 1749 if ((ipst->ips_ip_respond_to_address_mask_broadcast || 1750 !broadcast) && 1751 /* TODO m_pullup of complete header? */ 1752 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) 1753 interested = B_TRUE; 1754 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks); 1755 break; 1756 case ICMP_ADDRESS_MASK_REPLY: 1757 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps); 1758 break; 1759 default: 1760 interested = B_TRUE; /* Pass up to transport */ 1761 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns); 1762 break; 1763 } 1764 /* See if there is an ICMP client. */ 1765 if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) { 1766 /* If there is an ICMP client and we want one too, copy it. */ 1767 mblk_t *first_mp1; 1768 1769 if (!interested) { 1770 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1771 ip_policy, recv_ill, zoneid); 1772 return; 1773 } 1774 first_mp1 = ip_copymsg(first_mp); 1775 if (first_mp1 != NULL) { 1776 ip_fanout_proto(q, first_mp1, ill, ipha, 1777 0, mctl_present, ip_policy, recv_ill, zoneid); 1778 } 1779 } else if (!interested) { 1780 freemsg(first_mp); 1781 return; 1782 } else { 1783 /* 1784 * Initiate policy processing for this packet if ip_policy 1785 * is true. 1786 */ 1787 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 1788 ill_index = ill->ill_phyint->phyint_ifindex; 1789 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1790 if (mp == NULL) { 1791 if (mctl_present) { 1792 freeb(first_mp); 1793 } 1794 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1795 return; 1796 } 1797 } 1798 } 1799 /* We want to do something with it. */ 1800 /* Check db_ref to make sure we can modify the packet. */ 1801 if (mp->b_datap->db_ref > 1) { 1802 mblk_t *first_mp1; 1803 1804 first_mp1 = ip_copymsg(first_mp); 1805 freemsg(first_mp); 1806 if (!first_mp1) { 1807 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1808 return; 1809 } 1810 first_mp = first_mp1; 1811 if (mctl_present) { 1812 mp = first_mp->b_cont; 1813 ASSERT(mp != NULL); 1814 } else { 1815 mp = first_mp; 1816 } 1817 ipha = (ipha_t *)mp->b_rptr; 1818 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1819 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1820 } 1821 switch (icmph->icmph_type) { 1822 case ICMP_ADDRESS_MASK_REQUEST: 1823 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1824 if (ipif == NULL) { 1825 freemsg(first_mp); 1826 return; 1827 } 1828 /* 1829 * outging interface must be IPv4 1830 */ 1831 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1832 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1833 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1834 ipif_refrele(ipif); 1835 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps); 1836 break; 1837 case ICMP_ECHO_REQUEST: 1838 icmph->icmph_type = ICMP_ECHO_REPLY; 1839 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps); 1840 break; 1841 case ICMP_TIME_STAMP_REQUEST: { 1842 uint32_t *tsp; 1843 1844 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1845 tsp = (uint32_t *)wptr; 1846 tsp++; /* Skip past 'originate time' */ 1847 /* Compute # of milliseconds since midnight */ 1848 gethrestime(&now); 1849 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1850 now.tv_nsec / (NANOSEC / MILLISEC); 1851 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1852 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1853 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps); 1854 break; 1855 } 1856 default: 1857 ipha = (ipha_t *)&icmph[1]; 1858 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1859 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1860 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1861 freemsg(first_mp); 1862 return; 1863 } 1864 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1865 ipha = (ipha_t *)&icmph[1]; 1866 } 1867 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1868 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1869 freemsg(first_mp); 1870 return; 1871 } 1872 hdr_length = IPH_HDR_LENGTH(ipha); 1873 if (hdr_length < sizeof (ipha_t)) { 1874 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1875 freemsg(first_mp); 1876 return; 1877 } 1878 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 1879 if (!pullupmsg(mp, 1880 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 1881 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1882 freemsg(first_mp); 1883 return; 1884 } 1885 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1886 ipha = (ipha_t *)&icmph[1]; 1887 } 1888 switch (icmph->icmph_type) { 1889 case ICMP_REDIRECT: 1890 /* 1891 * As there is no upper client to deliver, we don't 1892 * need the first_mp any more. 1893 */ 1894 if (mctl_present) { 1895 freeb(first_mp); 1896 } 1897 icmp_redirect(ill, mp); 1898 return; 1899 case ICMP_DEST_UNREACHABLE: 1900 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 1901 if (!icmp_inbound_too_big(icmph, ipha, ill, 1902 zoneid, mp, iph_hdr_length, ipst)) { 1903 freemsg(first_mp); 1904 return; 1905 } 1906 /* 1907 * icmp_inbound_too_big() may alter mp. 1908 * Resynch ipha and icmph accordingly. 1909 */ 1910 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1911 ipha = (ipha_t *)&icmph[1]; 1912 } 1913 /* FALLTHRU */ 1914 default : 1915 /* 1916 * IPQoS notes: Since we have already done IPQoS 1917 * processing we don't want to do it again in 1918 * the fanout routines called by 1919 * icmp_inbound_error_fanout, hence the last 1920 * argument, ip_policy, is B_FALSE. 1921 */ 1922 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 1923 ipha, iph_hdr_length, hdr_length, mctl_present, 1924 B_FALSE, recv_ill, zoneid); 1925 } 1926 return; 1927 } 1928 /* Send out an ICMP packet */ 1929 icmph->icmph_checksum = 0; 1930 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 1931 if (icmph->icmph_checksum == 0) 1932 icmph->icmph_checksum = 0xFFFF; 1933 if (broadcast || CLASSD(ipha->ipha_dst)) { 1934 ipif_t *ipif_chosen; 1935 /* 1936 * Make it look like it was directed to us, so we don't look 1937 * like a fool with a broadcast or multicast source address. 1938 */ 1939 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1940 /* 1941 * Make sure that we haven't grabbed an interface that's DOWN. 1942 */ 1943 if (ipif != NULL) { 1944 ipif_chosen = ipif_select_source(ipif->ipif_ill, 1945 ipha->ipha_src, zoneid); 1946 if (ipif_chosen != NULL) { 1947 ipif_refrele(ipif); 1948 ipif = ipif_chosen; 1949 } 1950 } 1951 if (ipif == NULL) { 1952 ip0dbg(("icmp_inbound: " 1953 "No source for broadcast/multicast:\n" 1954 "\tsrc 0x%x dst 0x%x ill %p " 1955 "ipif_lcl_addr 0x%x\n", 1956 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 1957 (void *)ill, 1958 ill->ill_ipif->ipif_lcl_addr)); 1959 freemsg(first_mp); 1960 return; 1961 } 1962 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1963 ipha->ipha_dst = ipif->ipif_src_addr; 1964 ipif_refrele(ipif); 1965 } 1966 /* Reset time to live. */ 1967 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 1968 { 1969 /* Swap source and destination addresses */ 1970 ipaddr_t tmp; 1971 1972 tmp = ipha->ipha_src; 1973 ipha->ipha_src = ipha->ipha_dst; 1974 ipha->ipha_dst = tmp; 1975 } 1976 ipha->ipha_ident = 0; 1977 if (!IS_SIMPLE_IPH(ipha)) 1978 icmp_options_update(ipha); 1979 1980 /* 1981 * ICMP echo replies should go out on the same interface 1982 * the request came on as probes used by in.mpathd for detecting 1983 * NIC failures are ECHO packets. We turn-off load spreading 1984 * by setting ipsec_in_attach_if to B_TRUE, which is copied 1985 * to ipsec_out_attach_if by ipsec_in_to_out called later in this 1986 * function. This is in turn handled by ip_wput and ip_newroute 1987 * to make sure that the packet goes out on the interface it came 1988 * in on. If we don't turnoff load spreading, the packets might get 1989 * dropped if there are no non-FAILED/INACTIVE interfaces for it 1990 * to go out and in.mpathd would wrongly detect a failure or 1991 * mis-detect a NIC failure for link failure. As load spreading 1992 * can happen only if ill_group is not NULL, we do only for 1993 * that case and this does not affect the normal case. 1994 * 1995 * We turn off load spreading only on echo packets that came from 1996 * on-link hosts. If the interface route has been deleted, this will 1997 * not be enforced as we can't do much. For off-link hosts, as the 1998 * default routes in IPv4 does not typically have an ire_ipif 1999 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. 2000 * Moreover, expecting a default route through this interface may 2001 * not be correct. We use ipha_dst because of the swap above. 2002 */ 2003 onlink = B_FALSE; 2004 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { 2005 /* 2006 * First, we need to make sure that it is not one of our 2007 * local addresses. If we set onlink when it is one of 2008 * our local addresses, we will end up creating IRE_CACHES 2009 * for one of our local addresses. Then, we will never 2010 * accept packets for them afterwards. 2011 */ 2012 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, 2013 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2014 if (src_ire == NULL) { 2015 ipif = ipif_get_next_ipif(NULL, ill); 2016 if (ipif == NULL) { 2017 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2018 freemsg(mp); 2019 return; 2020 } 2021 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 2022 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 2023 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE, ipst); 2024 ipif_refrele(ipif); 2025 if (src_ire != NULL) { 2026 onlink = B_TRUE; 2027 ire_refrele(src_ire); 2028 } 2029 } else { 2030 ire_refrele(src_ire); 2031 } 2032 } 2033 if (!mctl_present) { 2034 /* 2035 * This packet should go out the same way as it 2036 * came in i.e in clear. To make sure that global 2037 * policy will not be applied to this in ip_wput_ire, 2038 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2039 */ 2040 ASSERT(first_mp == mp); 2041 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2042 if (first_mp == NULL) { 2043 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2044 freemsg(mp); 2045 return; 2046 } 2047 ii = (ipsec_in_t *)first_mp->b_rptr; 2048 2049 /* This is not a secure packet */ 2050 ii->ipsec_in_secure = B_FALSE; 2051 if (onlink) { 2052 ii->ipsec_in_attach_if = B_TRUE; 2053 ii->ipsec_in_ill_index = 2054 ill->ill_phyint->phyint_ifindex; 2055 ii->ipsec_in_rill_index = 2056 recv_ill->ill_phyint->phyint_ifindex; 2057 } 2058 first_mp->b_cont = mp; 2059 } else if (onlink) { 2060 ii = (ipsec_in_t *)first_mp->b_rptr; 2061 ii->ipsec_in_attach_if = B_TRUE; 2062 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 2063 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; 2064 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2065 } else { 2066 ii = (ipsec_in_t *)first_mp->b_rptr; 2067 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2068 } 2069 ii->ipsec_in_zoneid = zoneid; 2070 ASSERT(zoneid != ALL_ZONES); 2071 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2072 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2073 return; 2074 } 2075 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 2076 put(WR(q), first_mp); 2077 } 2078 2079 static ipaddr_t 2080 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) 2081 { 2082 conn_t *connp; 2083 connf_t *connfp; 2084 ipaddr_t nexthop_addr = INADDR_ANY; 2085 int hdr_length = IPH_HDR_LENGTH(ipha); 2086 uint16_t *up; 2087 uint32_t ports; 2088 ip_stack_t *ipst = ill->ill_ipst; 2089 2090 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2091 switch (ipha->ipha_protocol) { 2092 case IPPROTO_TCP: 2093 { 2094 tcph_t *tcph; 2095 2096 /* do a reverse lookup */ 2097 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2098 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, 2099 TCPS_LISTEN, ipst); 2100 break; 2101 } 2102 case IPPROTO_UDP: 2103 { 2104 uint32_t dstport, srcport; 2105 2106 ((uint16_t *)&ports)[0] = up[1]; 2107 ((uint16_t *)&ports)[1] = up[0]; 2108 2109 /* Extract ports in net byte order */ 2110 dstport = htons(ntohl(ports) & 0xFFFF); 2111 srcport = htons(ntohl(ports) >> 16); 2112 2113 connfp = &ipst->ips_ipcl_udp_fanout[ 2114 IPCL_UDP_HASH(dstport, ipst)]; 2115 mutex_enter(&connfp->connf_lock); 2116 connp = connfp->connf_head; 2117 2118 /* do a reverse lookup */ 2119 while ((connp != NULL) && 2120 (!IPCL_UDP_MATCH(connp, dstport, 2121 ipha->ipha_src, srcport, ipha->ipha_dst) || 2122 !IPCL_ZONE_MATCH(connp, zoneid))) { 2123 connp = connp->conn_next; 2124 } 2125 if (connp != NULL) 2126 CONN_INC_REF(connp); 2127 mutex_exit(&connfp->connf_lock); 2128 break; 2129 } 2130 case IPPROTO_SCTP: 2131 { 2132 in6_addr_t map_src, map_dst; 2133 2134 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); 2135 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); 2136 ((uint16_t *)&ports)[0] = up[1]; 2137 ((uint16_t *)&ports)[1] = up[0]; 2138 2139 connp = sctp_find_conn(&map_src, &map_dst, ports, 2140 zoneid, ipst->ips_netstack->netstack_sctp); 2141 if (connp == NULL) { 2142 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, 2143 zoneid, ports, ipha, ipst); 2144 } else { 2145 CONN_INC_REF(connp); 2146 SCTP_REFRELE(CONN2SCTP(connp)); 2147 } 2148 break; 2149 } 2150 default: 2151 { 2152 ipha_t ripha; 2153 2154 ripha.ipha_src = ipha->ipha_dst; 2155 ripha.ipha_dst = ipha->ipha_src; 2156 ripha.ipha_protocol = ipha->ipha_protocol; 2157 2158 connfp = &ipst->ips_ipcl_proto_fanout[ 2159 ipha->ipha_protocol]; 2160 mutex_enter(&connfp->connf_lock); 2161 connp = connfp->connf_head; 2162 for (connp = connfp->connf_head; connp != NULL; 2163 connp = connp->conn_next) { 2164 if (IPCL_PROTO_MATCH(connp, 2165 ipha->ipha_protocol, &ripha, ill, 2166 0, zoneid)) { 2167 CONN_INC_REF(connp); 2168 break; 2169 } 2170 } 2171 mutex_exit(&connfp->connf_lock); 2172 } 2173 } 2174 if (connp != NULL) { 2175 if (connp->conn_nexthop_set) 2176 nexthop_addr = connp->conn_nexthop_v4; 2177 CONN_DEC_REF(connp); 2178 } 2179 return (nexthop_addr); 2180 } 2181 2182 /* Table from RFC 1191 */ 2183 static int icmp_frag_size_table[] = 2184 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2185 2186 /* 2187 * Process received ICMP Packet too big. 2188 * After updating any IRE it does the fanout to any matching transport streams. 2189 * Assumes the message has been pulled up till the IP header that caused 2190 * the error. 2191 * 2192 * Returns B_FALSE on failure and B_TRUE on success. 2193 */ 2194 static boolean_t 2195 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, 2196 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length, 2197 ip_stack_t *ipst) 2198 { 2199 ire_t *ire, *first_ire; 2200 int mtu; 2201 int hdr_length; 2202 ipaddr_t nexthop_addr; 2203 2204 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 2205 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 2206 ASSERT(ill != NULL); 2207 2208 hdr_length = IPH_HDR_LENGTH(ipha); 2209 2210 /* Drop if the original packet contained a source route */ 2211 if (ip_source_route_included(ipha)) { 2212 return (B_FALSE); 2213 } 2214 /* 2215 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport 2216 * header. 2217 */ 2218 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2219 mp->b_wptr) { 2220 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2221 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2222 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2223 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n")); 2224 return (B_FALSE); 2225 } 2226 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2227 ipha = (ipha_t *)&icmph[1]; 2228 } 2229 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp); 2230 if (nexthop_addr != INADDR_ANY) { 2231 /* nexthop set */ 2232 first_ire = ire_ctable_lookup(ipha->ipha_dst, 2233 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp), 2234 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, ipst); 2235 } else { 2236 /* nexthop not set */ 2237 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, 2238 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2239 } 2240 2241 if (!first_ire) { 2242 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", 2243 ntohl(ipha->ipha_dst))); 2244 return (B_FALSE); 2245 } 2246 /* Check for MTU discovery advice as described in RFC 1191 */ 2247 mtu = ntohs(icmph->icmph_du_mtu); 2248 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2249 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2250 ire = ire->ire_next) { 2251 /* 2252 * Look for the connection to which this ICMP message is 2253 * directed. If it has the IP_NEXTHOP option set, then the 2254 * search is limited to IREs with the MATCH_IRE_PRIVATE 2255 * option. Else the search is limited to regular IREs. 2256 */ 2257 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2258 (nexthop_addr != ire->ire_gateway_addr)) || 2259 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2260 (nexthop_addr != INADDR_ANY))) 2261 continue; 2262 2263 mutex_enter(&ire->ire_lock); 2264 if (icmph->icmph_du_zero == 0 && mtu > 68) { 2265 /* Reduce the IRE max frag value as advised. */ 2266 ip1dbg(("Received mtu from router: %d (was %d)\n", 2267 mtu, ire->ire_max_frag)); 2268 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2269 } else { 2270 uint32_t length; 2271 int i; 2272 2273 /* 2274 * Use the table from RFC 1191 to figure out 2275 * the next "plateau" based on the length in 2276 * the original IP packet. 2277 */ 2278 length = ntohs(ipha->ipha_length); 2279 if (ire->ire_max_frag <= length && 2280 ire->ire_max_frag >= length - hdr_length) { 2281 /* 2282 * Handle broken BSD 4.2 systems that 2283 * return the wrong iph_length in ICMP 2284 * errors. 2285 */ 2286 ip1dbg(("Wrong mtu: sent %d, ire %d\n", 2287 length, ire->ire_max_frag)); 2288 length -= hdr_length; 2289 } 2290 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2291 if (length > icmp_frag_size_table[i]) 2292 break; 2293 } 2294 if (i == A_CNT(icmp_frag_size_table)) { 2295 /* Smaller than 68! */ 2296 ip1dbg(("Too big for packet size %d\n", 2297 length)); 2298 ire->ire_max_frag = MIN(ire->ire_max_frag, 576); 2299 ire->ire_frag_flag = 0; 2300 } else { 2301 mtu = icmp_frag_size_table[i]; 2302 ip1dbg(("Calculated mtu %d, packet size %d, " 2303 "before %d", mtu, length, 2304 ire->ire_max_frag)); 2305 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2306 ip1dbg((", after %d\n", ire->ire_max_frag)); 2307 } 2308 /* Record the new max frag size for the ULP. */ 2309 icmph->icmph_du_zero = 0; 2310 icmph->icmph_du_mtu = 2311 htons((uint16_t)ire->ire_max_frag); 2312 } 2313 mutex_exit(&ire->ire_lock); 2314 } 2315 rw_exit(&first_ire->ire_bucket->irb_lock); 2316 ire_refrele(first_ire); 2317 return (B_TRUE); 2318 } 2319 2320 /* 2321 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2322 * calls this function. 2323 */ 2324 static mblk_t * 2325 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2326 { 2327 ipha_t *ipha; 2328 icmph_t *icmph; 2329 ipha_t *in_ipha; 2330 int length; 2331 2332 ASSERT(mp->b_datap->db_type == M_DATA); 2333 2334 /* 2335 * For Self-encapsulated packets, we added an extra IP header 2336 * without the options. Inner IP header is the one from which 2337 * the outer IP header was formed. Thus, we need to remove the 2338 * outer IP header. To do this, we pullup the whole message 2339 * and overlay whatever follows the outer IP header over the 2340 * outer IP header. 2341 */ 2342 2343 if (!pullupmsg(mp, -1)) 2344 return (NULL); 2345 2346 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2347 ipha = (ipha_t *)&icmph[1]; 2348 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2349 2350 /* 2351 * The length that we want to overlay is following the inner 2352 * IP header. Subtracting the IP header + icmp header + outer 2353 * IP header's length should give us the length that we want to 2354 * overlay. 2355 */ 2356 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2357 hdr_length; 2358 /* 2359 * Overlay whatever follows the inner header over the 2360 * outer header. 2361 */ 2362 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2363 2364 /* Set the wptr to account for the outer header */ 2365 mp->b_wptr -= hdr_length; 2366 return (mp); 2367 } 2368 2369 /* 2370 * Try to pass the ICMP message upstream in case the ULP cares. 2371 * 2372 * If the packet that caused the ICMP error is secure, we send 2373 * it to AH/ESP to make sure that the attached packet has a 2374 * valid association. ipha in the code below points to the 2375 * IP header of the packet that caused the error. 2376 * 2377 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2378 * in the context of IPSEC. Normally we tell the upper layer 2379 * whenever we send the ire (including ip_bind), the IPSEC header 2380 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2381 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2382 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2383 * same thing. As TCP has the IPSEC options size that needs to be 2384 * adjusted, we just pass the MTU unchanged. 2385 * 2386 * IFN could have been generated locally or by some router. 2387 * 2388 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2389 * This happens because IP adjusted its value of MTU on an 2390 * earlier IFN message and could not tell the upper layer, 2391 * the new adjusted value of MTU e.g. Packet was encrypted 2392 * or there was not enough information to fanout to upper 2393 * layers. Thus on the next outbound datagram, ip_wput_ire 2394 * generates the IFN, where IPSEC processing has *not* been 2395 * done. 2396 * 2397 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2398 * could have generated this. This happens because ire_max_frag 2399 * value in IP was set to a new value, while the IPSEC processing 2400 * was being done and after we made the fragmentation check in 2401 * ip_wput_ire. Thus on return from IPSEC processing, 2402 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2403 * and generates the IFN. As IPSEC processing is over, we fanout 2404 * to AH/ESP to remove the header. 2405 * 2406 * In both these cases, ipsec_in_loopback will be set indicating 2407 * that IFN was generated locally. 2408 * 2409 * ROUTER : IFN could be secure or non-secure. 2410 * 2411 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2412 * packet in error has AH/ESP headers to validate the AH/ESP 2413 * headers. AH/ESP will verify whether there is a valid SA or 2414 * not and send it back. We will fanout again if we have more 2415 * data in the packet. 2416 * 2417 * If the packet in error does not have AH/ESP, we handle it 2418 * like any other case. 2419 * 2420 * * NON_SECURE : If the packet in error has AH/ESP headers, 2421 * we attach a dummy ipsec_in and send it up to AH/ESP 2422 * for validation. AH/ESP will verify whether there is a 2423 * valid SA or not and send it back. We will fanout again if 2424 * we have more data in the packet. 2425 * 2426 * If the packet in error does not have AH/ESP, we handle it 2427 * like any other case. 2428 */ 2429 static void 2430 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2431 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2432 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2433 zoneid_t zoneid) 2434 { 2435 uint16_t *up; /* Pointer to ports in ULP header */ 2436 uint32_t ports; /* reversed ports for fanout */ 2437 ipha_t ripha; /* With reversed addresses */ 2438 mblk_t *first_mp; 2439 ipsec_in_t *ii; 2440 tcph_t *tcph; 2441 conn_t *connp; 2442 ip_stack_t *ipst; 2443 2444 ASSERT(ill != NULL); 2445 2446 ASSERT(recv_ill != NULL); 2447 ipst = recv_ill->ill_ipst; 2448 2449 first_mp = mp; 2450 if (mctl_present) { 2451 mp = first_mp->b_cont; 2452 ASSERT(mp != NULL); 2453 2454 ii = (ipsec_in_t *)first_mp->b_rptr; 2455 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2456 } else { 2457 ii = NULL; 2458 } 2459 2460 switch (ipha->ipha_protocol) { 2461 case IPPROTO_UDP: 2462 /* 2463 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2464 * transport header. 2465 */ 2466 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2467 mp->b_wptr) { 2468 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2469 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2470 goto discard_pkt; 2471 } 2472 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2473 ipha = (ipha_t *)&icmph[1]; 2474 } 2475 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2476 2477 /* 2478 * Attempt to find a client stream based on port. 2479 * Note that we do a reverse lookup since the header is 2480 * in the form we sent it out. 2481 * The ripha header is only used for the IP_UDP_MATCH and we 2482 * only set the src and dst addresses and protocol. 2483 */ 2484 ripha.ipha_src = ipha->ipha_dst; 2485 ripha.ipha_dst = ipha->ipha_src; 2486 ripha.ipha_protocol = ipha->ipha_protocol; 2487 ((uint16_t *)&ports)[0] = up[1]; 2488 ((uint16_t *)&ports)[1] = up[0]; 2489 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2490 ntohl(ipha->ipha_src), ntohs(up[0]), 2491 ntohl(ipha->ipha_dst), ntohs(up[1]), 2492 icmph->icmph_type, icmph->icmph_code)); 2493 2494 /* Have to change db_type after any pullupmsg */ 2495 DB_TYPE(mp) = M_CTL; 2496 2497 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2498 mctl_present, ip_policy, recv_ill, zoneid); 2499 return; 2500 2501 case IPPROTO_TCP: 2502 /* 2503 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2504 * transport header. 2505 */ 2506 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2507 mp->b_wptr) { 2508 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2509 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2510 goto discard_pkt; 2511 } 2512 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2513 ipha = (ipha_t *)&icmph[1]; 2514 } 2515 /* 2516 * Find a TCP client stream for this packet. 2517 * Note that we do a reverse lookup since the header is 2518 * in the form we sent it out. 2519 */ 2520 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2521 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN, 2522 ipst); 2523 if (connp == NULL) 2524 goto discard_pkt; 2525 2526 /* Have to change db_type after any pullupmsg */ 2527 DB_TYPE(mp) = M_CTL; 2528 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 2529 connp, SQTAG_TCP_INPUT_ICMP_ERR); 2530 return; 2531 2532 case IPPROTO_SCTP: 2533 /* 2534 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2535 * transport header. 2536 */ 2537 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2538 mp->b_wptr) { 2539 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2540 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2541 goto discard_pkt; 2542 } 2543 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2544 ipha = (ipha_t *)&icmph[1]; 2545 } 2546 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2547 /* 2548 * Find a SCTP client stream for this packet. 2549 * Note that we do a reverse lookup since the header is 2550 * in the form we sent it out. 2551 * The ripha header is only used for the matching and we 2552 * only set the src and dst addresses, protocol, and version. 2553 */ 2554 ripha.ipha_src = ipha->ipha_dst; 2555 ripha.ipha_dst = ipha->ipha_src; 2556 ripha.ipha_protocol = ipha->ipha_protocol; 2557 ripha.ipha_version_and_hdr_length = 2558 ipha->ipha_version_and_hdr_length; 2559 ((uint16_t *)&ports)[0] = up[1]; 2560 ((uint16_t *)&ports)[1] = up[0]; 2561 2562 /* Have to change db_type after any pullupmsg */ 2563 DB_TYPE(mp) = M_CTL; 2564 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2565 mctl_present, ip_policy, zoneid); 2566 return; 2567 2568 case IPPROTO_ESP: 2569 case IPPROTO_AH: { 2570 int ipsec_rc; 2571 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 2572 2573 /* 2574 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2575 * We will re-use the IPSEC_IN if it is already present as 2576 * AH/ESP will not affect any fields in the IPSEC_IN for 2577 * ICMP errors. If there is no IPSEC_IN, allocate a new 2578 * one and attach it in the front. 2579 */ 2580 if (ii != NULL) { 2581 /* 2582 * ip_fanout_proto_again converts the ICMP errors 2583 * that come back from AH/ESP to M_DATA so that 2584 * if it is non-AH/ESP and we do a pullupmsg in 2585 * this function, it would work. Convert it back 2586 * to M_CTL before we send up as this is a ICMP 2587 * error. This could have been generated locally or 2588 * by some router. Validate the inner IPSEC 2589 * headers. 2590 * 2591 * NOTE : ill_index is used by ip_fanout_proto_again 2592 * to locate the ill. 2593 */ 2594 ASSERT(ill != NULL); 2595 ii->ipsec_in_ill_index = 2596 ill->ill_phyint->phyint_ifindex; 2597 ii->ipsec_in_rill_index = 2598 recv_ill->ill_phyint->phyint_ifindex; 2599 DB_TYPE(first_mp->b_cont) = M_CTL; 2600 } else { 2601 /* 2602 * IPSEC_IN is not present. We attach a ipsec_in 2603 * message and send up to IPSEC for validating 2604 * and removing the IPSEC headers. Clear 2605 * ipsec_in_secure so that when we return 2606 * from IPSEC, we don't mistakenly think that this 2607 * is a secure packet came from the network. 2608 * 2609 * NOTE : ill_index is used by ip_fanout_proto_again 2610 * to locate the ill. 2611 */ 2612 ASSERT(first_mp == mp); 2613 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2614 if (first_mp == NULL) { 2615 freemsg(mp); 2616 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2617 return; 2618 } 2619 ii = (ipsec_in_t *)first_mp->b_rptr; 2620 2621 /* This is not a secure packet */ 2622 ii->ipsec_in_secure = B_FALSE; 2623 first_mp->b_cont = mp; 2624 DB_TYPE(mp) = M_CTL; 2625 ASSERT(ill != NULL); 2626 ii->ipsec_in_ill_index = 2627 ill->ill_phyint->phyint_ifindex; 2628 ii->ipsec_in_rill_index = 2629 recv_ill->ill_phyint->phyint_ifindex; 2630 } 2631 ip2dbg(("icmp_inbound_error: ipsec\n")); 2632 2633 if (!ipsec_loaded(ipss)) { 2634 ip_proto_not_sup(q, first_mp, 0, zoneid, ipst); 2635 return; 2636 } 2637 2638 if (ipha->ipha_protocol == IPPROTO_ESP) 2639 ipsec_rc = ipsecesp_icmp_error(first_mp); 2640 else 2641 ipsec_rc = ipsecah_icmp_error(first_mp); 2642 if (ipsec_rc == IPSEC_STATUS_FAILED) 2643 return; 2644 2645 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2646 return; 2647 } 2648 default: 2649 /* 2650 * The ripha header is only used for the lookup and we 2651 * only set the src and dst addresses and protocol. 2652 */ 2653 ripha.ipha_src = ipha->ipha_dst; 2654 ripha.ipha_dst = ipha->ipha_src; 2655 ripha.ipha_protocol = ipha->ipha_protocol; 2656 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2657 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2658 ntohl(ipha->ipha_dst), 2659 icmph->icmph_type, icmph->icmph_code)); 2660 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2661 ipha_t *in_ipha; 2662 2663 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2664 mp->b_wptr) { 2665 if (!pullupmsg(mp, (uchar_t *)ipha + 2666 hdr_length + sizeof (ipha_t) - 2667 mp->b_rptr)) { 2668 goto discard_pkt; 2669 } 2670 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2671 ipha = (ipha_t *)&icmph[1]; 2672 } 2673 /* 2674 * Caller has verified that length has to be 2675 * at least the size of IP header. 2676 */ 2677 ASSERT(hdr_length >= sizeof (ipha_t)); 2678 /* 2679 * Check the sanity of the inner IP header like 2680 * we did for the outer header. 2681 */ 2682 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2683 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2684 goto discard_pkt; 2685 } 2686 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2687 goto discard_pkt; 2688 } 2689 /* Check for Self-encapsulated tunnels */ 2690 if (in_ipha->ipha_src == ipha->ipha_src && 2691 in_ipha->ipha_dst == ipha->ipha_dst) { 2692 2693 mp = icmp_inbound_self_encap_error(mp, 2694 iph_hdr_length, hdr_length); 2695 if (mp == NULL) 2696 goto discard_pkt; 2697 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2698 ipha = (ipha_t *)&icmph[1]; 2699 hdr_length = IPH_HDR_LENGTH(ipha); 2700 /* 2701 * The packet in error is self-encapsualted. 2702 * And we are finding it further encapsulated 2703 * which we could not have possibly generated. 2704 */ 2705 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2706 goto discard_pkt; 2707 } 2708 icmp_inbound_error_fanout(q, ill, first_mp, 2709 icmph, ipha, iph_hdr_length, hdr_length, 2710 mctl_present, ip_policy, recv_ill, zoneid); 2711 return; 2712 } 2713 } 2714 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2715 ipha->ipha_protocol == IPPROTO_IPV6) && 2716 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2717 ii != NULL && 2718 ii->ipsec_in_loopback && 2719 ii->ipsec_in_secure) { 2720 /* 2721 * For IP tunnels that get a looped-back 2722 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2723 * reported new MTU to take into account the IPsec 2724 * headers protecting this configured tunnel. 2725 * 2726 * This allows the tunnel module (tun.c) to blindly 2727 * accept the MTU reported in an ICMP "too big" 2728 * message. 2729 * 2730 * Non-looped back ICMP messages will just be 2731 * handled by the security protocols (if needed), 2732 * and the first subsequent packet will hit this 2733 * path. 2734 */ 2735 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2736 ipsec_in_extra_length(first_mp)); 2737 } 2738 /* Have to change db_type after any pullupmsg */ 2739 DB_TYPE(mp) = M_CTL; 2740 2741 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2742 ip_policy, recv_ill, zoneid); 2743 return; 2744 } 2745 /* NOTREACHED */ 2746 discard_pkt: 2747 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2748 drop_pkt:; 2749 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2750 freemsg(first_mp); 2751 } 2752 2753 /* 2754 * Common IP options parser. 2755 * 2756 * Setup routine: fill in *optp with options-parsing state, then 2757 * tail-call ipoptp_next to return the first option. 2758 */ 2759 uint8_t 2760 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2761 { 2762 uint32_t totallen; /* total length of all options */ 2763 2764 totallen = ipha->ipha_version_and_hdr_length - 2765 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2766 totallen <<= 2; 2767 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2768 optp->ipoptp_end = optp->ipoptp_next + totallen; 2769 optp->ipoptp_flags = 0; 2770 return (ipoptp_next(optp)); 2771 } 2772 2773 /* 2774 * Common IP options parser: extract next option. 2775 */ 2776 uint8_t 2777 ipoptp_next(ipoptp_t *optp) 2778 { 2779 uint8_t *end = optp->ipoptp_end; 2780 uint8_t *cur = optp->ipoptp_next; 2781 uint8_t opt, len, pointer; 2782 2783 /* 2784 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2785 * has been corrupted. 2786 */ 2787 ASSERT(cur <= end); 2788 2789 if (cur == end) 2790 return (IPOPT_EOL); 2791 2792 opt = cur[IPOPT_OPTVAL]; 2793 2794 /* 2795 * Skip any NOP options. 2796 */ 2797 while (opt == IPOPT_NOP) { 2798 cur++; 2799 if (cur == end) 2800 return (IPOPT_EOL); 2801 opt = cur[IPOPT_OPTVAL]; 2802 } 2803 2804 if (opt == IPOPT_EOL) 2805 return (IPOPT_EOL); 2806 2807 /* 2808 * Option requiring a length. 2809 */ 2810 if ((cur + 1) >= end) { 2811 optp->ipoptp_flags |= IPOPTP_ERROR; 2812 return (IPOPT_EOL); 2813 } 2814 len = cur[IPOPT_OLEN]; 2815 if (len < 2) { 2816 optp->ipoptp_flags |= IPOPTP_ERROR; 2817 return (IPOPT_EOL); 2818 } 2819 optp->ipoptp_cur = cur; 2820 optp->ipoptp_len = len; 2821 optp->ipoptp_next = cur + len; 2822 if (cur + len > end) { 2823 optp->ipoptp_flags |= IPOPTP_ERROR; 2824 return (IPOPT_EOL); 2825 } 2826 2827 /* 2828 * For the options which require a pointer field, make sure 2829 * its there, and make sure it points to either something 2830 * inside this option, or the end of the option. 2831 */ 2832 switch (opt) { 2833 case IPOPT_RR: 2834 case IPOPT_TS: 2835 case IPOPT_LSRR: 2836 case IPOPT_SSRR: 2837 if (len <= IPOPT_OFFSET) { 2838 optp->ipoptp_flags |= IPOPTP_ERROR; 2839 return (opt); 2840 } 2841 pointer = cur[IPOPT_OFFSET]; 2842 if (pointer - 1 > len) { 2843 optp->ipoptp_flags |= IPOPTP_ERROR; 2844 return (opt); 2845 } 2846 break; 2847 } 2848 2849 /* 2850 * Sanity check the pointer field based on the type of the 2851 * option. 2852 */ 2853 switch (opt) { 2854 case IPOPT_RR: 2855 case IPOPT_SSRR: 2856 case IPOPT_LSRR: 2857 if (pointer < IPOPT_MINOFF_SR) 2858 optp->ipoptp_flags |= IPOPTP_ERROR; 2859 break; 2860 case IPOPT_TS: 2861 if (pointer < IPOPT_MINOFF_IT) 2862 optp->ipoptp_flags |= IPOPTP_ERROR; 2863 /* 2864 * Note that the Internet Timestamp option also 2865 * contains two four bit fields (the Overflow field, 2866 * and the Flag field), which follow the pointer 2867 * field. We don't need to check that these fields 2868 * fall within the length of the option because this 2869 * was implicitely done above. We've checked that the 2870 * pointer value is at least IPOPT_MINOFF_IT, and that 2871 * it falls within the option. Since IPOPT_MINOFF_IT > 2872 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2873 */ 2874 ASSERT(len > IPOPT_POS_OV_FLG); 2875 break; 2876 } 2877 2878 return (opt); 2879 } 2880 2881 /* 2882 * Use the outgoing IP header to create an IP_OPTIONS option the way 2883 * it was passed down from the application. 2884 */ 2885 int 2886 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) 2887 { 2888 ipoptp_t opts; 2889 const uchar_t *opt; 2890 uint8_t optval; 2891 uint8_t optlen; 2892 uint32_t len = 0; 2893 uchar_t *buf1 = buf; 2894 2895 buf += IP_ADDR_LEN; /* Leave room for final destination */ 2896 len += IP_ADDR_LEN; 2897 bzero(buf1, IP_ADDR_LEN); 2898 2899 /* 2900 * OK to cast away const here, as we don't store through the returned 2901 * opts.ipoptp_cur pointer. 2902 */ 2903 for (optval = ipoptp_first(&opts, (ipha_t *)ipha); 2904 optval != IPOPT_EOL; 2905 optval = ipoptp_next(&opts)) { 2906 int off; 2907 2908 opt = opts.ipoptp_cur; 2909 optlen = opts.ipoptp_len; 2910 switch (optval) { 2911 case IPOPT_SSRR: 2912 case IPOPT_LSRR: 2913 2914 /* 2915 * Insert ipha_dst as the first entry in the source 2916 * route and move down the entries on step. 2917 * The last entry gets placed at buf1. 2918 */ 2919 buf[IPOPT_OPTVAL] = optval; 2920 buf[IPOPT_OLEN] = optlen; 2921 buf[IPOPT_OFFSET] = optlen; 2922 2923 off = optlen - IP_ADDR_LEN; 2924 if (off < 0) { 2925 /* No entries in source route */ 2926 break; 2927 } 2928 /* Last entry in source route */ 2929 bcopy(opt + off, buf1, IP_ADDR_LEN); 2930 off -= IP_ADDR_LEN; 2931 2932 while (off > 0) { 2933 bcopy(opt + off, 2934 buf + off + IP_ADDR_LEN, 2935 IP_ADDR_LEN); 2936 off -= IP_ADDR_LEN; 2937 } 2938 /* ipha_dst into first slot */ 2939 bcopy(&ipha->ipha_dst, 2940 buf + off + IP_ADDR_LEN, 2941 IP_ADDR_LEN); 2942 buf += optlen; 2943 len += optlen; 2944 break; 2945 2946 case IPOPT_COMSEC: 2947 case IPOPT_SECURITY: 2948 /* if passing up a label is not ok, then remove */ 2949 if (is_system_labeled()) 2950 break; 2951 /* FALLTHROUGH */ 2952 default: 2953 bcopy(opt, buf, optlen); 2954 buf += optlen; 2955 len += optlen; 2956 break; 2957 } 2958 } 2959 done: 2960 /* Pad the resulting options */ 2961 while (len & 0x3) { 2962 *buf++ = IPOPT_EOL; 2963 len++; 2964 } 2965 return (len); 2966 } 2967 2968 /* 2969 * Update any record route or timestamp options to include this host. 2970 * Reverse any source route option. 2971 * This routine assumes that the options are well formed i.e. that they 2972 * have already been checked. 2973 */ 2974 static void 2975 icmp_options_update(ipha_t *ipha) 2976 { 2977 ipoptp_t opts; 2978 uchar_t *opt; 2979 uint8_t optval; 2980 ipaddr_t src; /* Our local address */ 2981 ipaddr_t dst; 2982 2983 ip2dbg(("icmp_options_update\n")); 2984 src = ipha->ipha_src; 2985 dst = ipha->ipha_dst; 2986 2987 for (optval = ipoptp_first(&opts, ipha); 2988 optval != IPOPT_EOL; 2989 optval = ipoptp_next(&opts)) { 2990 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 2991 opt = opts.ipoptp_cur; 2992 ip2dbg(("icmp_options_update: opt %d, len %d\n", 2993 optval, opts.ipoptp_len)); 2994 switch (optval) { 2995 int off1, off2; 2996 case IPOPT_SSRR: 2997 case IPOPT_LSRR: 2998 /* 2999 * Reverse the source route. The first entry 3000 * should be the next to last one in the current 3001 * source route (the last entry is our address). 3002 * The last entry should be the final destination. 3003 */ 3004 off1 = IPOPT_MINOFF_SR - 1; 3005 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 3006 if (off2 < 0) { 3007 /* No entries in source route */ 3008 ip1dbg(( 3009 "icmp_options_update: bad src route\n")); 3010 break; 3011 } 3012 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 3013 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 3014 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 3015 off2 -= IP_ADDR_LEN; 3016 3017 while (off1 < off2) { 3018 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 3019 bcopy((char *)opt + off2, (char *)opt + off1, 3020 IP_ADDR_LEN); 3021 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 3022 off1 += IP_ADDR_LEN; 3023 off2 -= IP_ADDR_LEN; 3024 } 3025 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 3026 break; 3027 } 3028 } 3029 } 3030 3031 /* 3032 * Process received ICMP Redirect messages. 3033 */ 3034 static void 3035 icmp_redirect(ill_t *ill, mblk_t *mp) 3036 { 3037 ipha_t *ipha; 3038 int iph_hdr_length; 3039 icmph_t *icmph; 3040 ipha_t *ipha_err; 3041 ire_t *ire; 3042 ire_t *prev_ire; 3043 ire_t *save_ire; 3044 ipaddr_t src, dst, gateway; 3045 iulp_t ulp_info = { 0 }; 3046 int error; 3047 ip_stack_t *ipst; 3048 3049 ASSERT(ill != NULL); 3050 ipst = ill->ill_ipst; 3051 3052 ipha = (ipha_t *)mp->b_rptr; 3053 iph_hdr_length = IPH_HDR_LENGTH(ipha); 3054 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 3055 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 3056 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3057 freemsg(mp); 3058 return; 3059 } 3060 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 3061 ipha_err = (ipha_t *)&icmph[1]; 3062 src = ipha->ipha_src; 3063 dst = ipha_err->ipha_dst; 3064 gateway = icmph->icmph_rd_gateway; 3065 /* Make sure the new gateway is reachable somehow. */ 3066 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 3067 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3068 /* 3069 * Make sure we had a route for the dest in question and that 3070 * that route was pointing to the old gateway (the source of the 3071 * redirect packet.) 3072 */ 3073 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 3074 NULL, MATCH_IRE_GW, ipst); 3075 /* 3076 * Check that 3077 * the redirect was not from ourselves 3078 * the new gateway and the old gateway are directly reachable 3079 */ 3080 if (!prev_ire || 3081 !ire || 3082 ire->ire_type == IRE_LOCAL) { 3083 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3084 freemsg(mp); 3085 if (ire != NULL) 3086 ire_refrele(ire); 3087 if (prev_ire != NULL) 3088 ire_refrele(prev_ire); 3089 return; 3090 } 3091 3092 /* 3093 * Should we use the old ULP info to create the new gateway? From 3094 * a user's perspective, we should inherit the info so that it 3095 * is a "smooth" transition. If we do not do that, then new 3096 * connections going thru the new gateway will have no route metrics, 3097 * which is counter-intuitive to user. From a network point of 3098 * view, this may or may not make sense even though the new gateway 3099 * is still directly connected to us so the route metrics should not 3100 * change much. 3101 * 3102 * But if the old ire_uinfo is not initialized, we do another 3103 * recursive lookup on the dest using the new gateway. There may 3104 * be a route to that. If so, use it to initialize the redirect 3105 * route. 3106 */ 3107 if (prev_ire->ire_uinfo.iulp_set) { 3108 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3109 } else { 3110 ire_t *tmp_ire; 3111 ire_t *sire; 3112 3113 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 3114 ALL_ZONES, 0, NULL, 3115 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT), 3116 ipst); 3117 if (sire != NULL) { 3118 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3119 /* 3120 * If sire != NULL, ire_ftable_lookup() should not 3121 * return a NULL value. 3122 */ 3123 ASSERT(tmp_ire != NULL); 3124 ire_refrele(tmp_ire); 3125 ire_refrele(sire); 3126 } else if (tmp_ire != NULL) { 3127 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 3128 sizeof (iulp_t)); 3129 ire_refrele(tmp_ire); 3130 } 3131 } 3132 if (prev_ire->ire_type == IRE_CACHE) 3133 ire_delete(prev_ire); 3134 ire_refrele(prev_ire); 3135 /* 3136 * TODO: more precise handling for cases 0, 2, 3, the latter two 3137 * require TOS routing 3138 */ 3139 switch (icmph->icmph_code) { 3140 case 0: 3141 case 1: 3142 /* TODO: TOS specificity for cases 2 and 3 */ 3143 case 2: 3144 case 3: 3145 break; 3146 default: 3147 freemsg(mp); 3148 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3149 ire_refrele(ire); 3150 return; 3151 } 3152 /* 3153 * Create a Route Association. This will allow us to remember that 3154 * someone we believe told us to use the particular gateway. 3155 */ 3156 save_ire = ire; 3157 ire = ire_create( 3158 (uchar_t *)&dst, /* dest addr */ 3159 (uchar_t *)&ip_g_all_ones, /* mask */ 3160 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 3161 (uchar_t *)&gateway, /* gateway addr */ 3162 NULL, /* no in_srcaddr */ 3163 &save_ire->ire_max_frag, /* max frag */ 3164 NULL, /* Fast Path header */ 3165 NULL, /* no rfq */ 3166 NULL, /* no stq */ 3167 IRE_HOST, 3168 NULL, 3169 NULL, 3170 NULL, 3171 0, 3172 0, 3173 0, 3174 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 3175 &ulp_info, 3176 NULL, 3177 NULL, 3178 ipst); 3179 3180 if (ire == NULL) { 3181 freemsg(mp); 3182 ire_refrele(save_ire); 3183 return; 3184 } 3185 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); 3186 ire_refrele(save_ire); 3187 atomic_inc_32(&ipst->ips_ip_redirect_cnt); 3188 3189 if (error == 0) { 3190 ire_refrele(ire); /* Held in ire_add_v4 */ 3191 /* tell routing sockets that we received a redirect */ 3192 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 3193 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 3194 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst); 3195 } 3196 3197 /* 3198 * Delete any existing IRE_HOST type redirect ires for this destination. 3199 * This together with the added IRE has the effect of 3200 * modifying an existing redirect. 3201 */ 3202 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL, 3203 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst); 3204 if (prev_ire != NULL) { 3205 if (prev_ire ->ire_flags & RTF_DYNAMIC) 3206 ire_delete(prev_ire); 3207 ire_refrele(prev_ire); 3208 } 3209 3210 freemsg(mp); 3211 } 3212 3213 /* 3214 * Generate an ICMP parameter problem message. 3215 */ 3216 static void 3217 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid, 3218 ip_stack_t *ipst) 3219 { 3220 icmph_t icmph; 3221 boolean_t mctl_present; 3222 mblk_t *first_mp; 3223 3224 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3225 3226 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3227 if (mctl_present) 3228 freeb(first_mp); 3229 return; 3230 } 3231 3232 bzero(&icmph, sizeof (icmph_t)); 3233 icmph.icmph_type = ICMP_PARAM_PROBLEM; 3234 icmph.icmph_pp_ptr = ptr; 3235 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs); 3236 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3237 ipst); 3238 } 3239 3240 /* 3241 * Build and ship an IPv4 ICMP message using the packet data in mp, and 3242 * the ICMP header pointed to by "stuff". (May be called as writer.) 3243 * Note: assumes that icmp_pkt_err_ok has been called to verify that 3244 * an icmp error packet can be sent. 3245 * Assigns an appropriate source address to the packet. If ipha_dst is 3246 * one of our addresses use it for source. Otherwise pick a source based 3247 * on a route lookup back to ipha_src. 3248 * Note that ipha_src must be set here since the 3249 * packet is likely to arrive on an ill queue in ip_wput() which will 3250 * not set a source address. 3251 */ 3252 static void 3253 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, 3254 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 3255 { 3256 ipaddr_t dst; 3257 icmph_t *icmph; 3258 ipha_t *ipha; 3259 uint_t len_needed; 3260 size_t msg_len; 3261 mblk_t *mp1; 3262 ipaddr_t src; 3263 ire_t *ire; 3264 mblk_t *ipsec_mp; 3265 ipsec_out_t *io = NULL; 3266 boolean_t xmit_if_on = B_FALSE; 3267 3268 if (mctl_present) { 3269 /* 3270 * If it is : 3271 * 3272 * 1) a IPSEC_OUT, then this is caused by outbound 3273 * datagram originating on this host. IPSEC processing 3274 * may or may not have been done. Refer to comments above 3275 * icmp_inbound_error_fanout for details. 3276 * 3277 * 2) a IPSEC_IN if we are generating a icmp_message 3278 * for an incoming datagram destined for us i.e called 3279 * from ip_fanout_send_icmp. 3280 */ 3281 ipsec_info_t *in; 3282 ipsec_mp = mp; 3283 mp = ipsec_mp->b_cont; 3284 3285 in = (ipsec_info_t *)ipsec_mp->b_rptr; 3286 ipha = (ipha_t *)mp->b_rptr; 3287 3288 ASSERT(in->ipsec_info_type == IPSEC_OUT || 3289 in->ipsec_info_type == IPSEC_IN); 3290 3291 if (in->ipsec_info_type == IPSEC_IN) { 3292 /* 3293 * Convert the IPSEC_IN to IPSEC_OUT. 3294 */ 3295 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3296 BUMP_MIB(&ipst->ips_ip_mib, 3297 ipIfStatsOutDiscards); 3298 return; 3299 } 3300 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3301 } else { 3302 ASSERT(in->ipsec_info_type == IPSEC_OUT); 3303 io = (ipsec_out_t *)in; 3304 if (io->ipsec_out_xmit_if) 3305 xmit_if_on = B_TRUE; 3306 /* 3307 * Clear out ipsec_out_proc_begin, so we do a fresh 3308 * ire lookup. 3309 */ 3310 io->ipsec_out_proc_begin = B_FALSE; 3311 } 3312 ASSERT(zoneid == io->ipsec_out_zoneid); 3313 ASSERT(zoneid != ALL_ZONES); 3314 } else { 3315 /* 3316 * This is in clear. The icmp message we are building 3317 * here should go out in clear. 3318 * 3319 * Pardon the convolution of it all, but it's easier to 3320 * allocate a "use cleartext" IPSEC_IN message and convert 3321 * it than it is to allocate a new one. 3322 */ 3323 ipsec_in_t *ii; 3324 ASSERT(DB_TYPE(mp) == M_DATA); 3325 ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 3326 if (ipsec_mp == NULL) { 3327 freemsg(mp); 3328 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3329 return; 3330 } 3331 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3332 3333 /* This is not a secure packet */ 3334 ii->ipsec_in_secure = B_FALSE; 3335 /* 3336 * For trusted extensions using a shared IP address we can 3337 * send using any zoneid. 3338 */ 3339 if (zoneid == ALL_ZONES) 3340 ii->ipsec_in_zoneid = GLOBAL_ZONEID; 3341 else 3342 ii->ipsec_in_zoneid = zoneid; 3343 ipsec_mp->b_cont = mp; 3344 ipha = (ipha_t *)mp->b_rptr; 3345 /* 3346 * Convert the IPSEC_IN to IPSEC_OUT. 3347 */ 3348 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3349 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3350 return; 3351 } 3352 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3353 } 3354 3355 /* Remember our eventual destination */ 3356 dst = ipha->ipha_src; 3357 3358 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 3359 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst); 3360 if (ire != NULL && 3361 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) { 3362 src = ipha->ipha_dst; 3363 } else if (!xmit_if_on) { 3364 if (ire != NULL) 3365 ire_refrele(ire); 3366 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL, 3367 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY), 3368 ipst); 3369 if (ire == NULL) { 3370 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3371 freemsg(ipsec_mp); 3372 return; 3373 } 3374 src = ire->ire_src_addr; 3375 } else { 3376 ipif_t *ipif = NULL; 3377 ill_t *ill; 3378 /* 3379 * This must be an ICMP error coming from 3380 * ip_mrtun_forward(). The src addr should 3381 * be equal to the IP-addr of the outgoing 3382 * interface. 3383 */ 3384 if (io == NULL) { 3385 /* This is not a IPSEC_OUT type control msg */ 3386 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3387 freemsg(ipsec_mp); 3388 return; 3389 } 3390 ill = ill_lookup_on_ifindex(io->ipsec_out_ill_index, B_FALSE, 3391 NULL, NULL, NULL, NULL, ipst); 3392 if (ill != NULL) { 3393 ipif = ipif_get_next_ipif(NULL, ill); 3394 ill_refrele(ill); 3395 } 3396 if (ipif == NULL) { 3397 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3398 freemsg(ipsec_mp); 3399 return; 3400 } 3401 src = ipif->ipif_src_addr; 3402 ipif_refrele(ipif); 3403 } 3404 3405 if (ire != NULL) 3406 ire_refrele(ire); 3407 3408 /* 3409 * Check if we can send back more then 8 bytes in addition 3410 * to the IP header. We will include as much as 64 bytes. 3411 */ 3412 len_needed = IPH_HDR_LENGTH(ipha); 3413 if (ipha->ipha_protocol == IPPROTO_ENCAP && 3414 (uchar_t *)ipha + len_needed + 1 <= mp->b_wptr) { 3415 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + len_needed)); 3416 } 3417 len_needed += ipst->ips_ip_icmp_return; 3418 msg_len = msgdsize(mp); 3419 if (msg_len > len_needed) { 3420 (void) adjmsg(mp, len_needed - msg_len); 3421 msg_len = len_needed; 3422 } 3423 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_HI); 3424 if (mp1 == NULL) { 3425 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors); 3426 freemsg(ipsec_mp); 3427 return; 3428 } 3429 /* 3430 * On an unlabeled system, dblks don't necessarily have creds. 3431 */ 3432 ASSERT(!is_system_labeled() || DB_CRED(mp) != NULL); 3433 if (DB_CRED(mp) != NULL) 3434 mblk_setcred(mp1, DB_CRED(mp)); 3435 mp1->b_cont = mp; 3436 mp = mp1; 3437 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 3438 ipsec_mp->b_rptr == (uint8_t *)io && 3439 io->ipsec_out_type == IPSEC_OUT); 3440 ipsec_mp->b_cont = mp; 3441 3442 /* 3443 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 3444 * node generates be accepted in peace by all on-host destinations. 3445 * If we do NOT assume that all on-host destinations trust 3446 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 3447 * (Look for ipsec_out_icmp_loopback). 3448 */ 3449 io->ipsec_out_icmp_loopback = B_TRUE; 3450 3451 ipha = (ipha_t *)mp->b_rptr; 3452 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 3453 *ipha = icmp_ipha; 3454 ipha->ipha_src = src; 3455 ipha->ipha_dst = dst; 3456 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 3457 msg_len += sizeof (icmp_ipha) + len; 3458 if (msg_len > IP_MAXPACKET) { 3459 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 3460 msg_len = IP_MAXPACKET; 3461 } 3462 ipha->ipha_length = htons((uint16_t)msg_len); 3463 icmph = (icmph_t *)&ipha[1]; 3464 bcopy(stuff, icmph, len); 3465 icmph->icmph_checksum = 0; 3466 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 3467 if (icmph->icmph_checksum == 0) 3468 icmph->icmph_checksum = 0xFFFF; 3469 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 3470 put(q, ipsec_mp); 3471 } 3472 3473 /* 3474 * Determine if an ICMP error packet can be sent given the rate limit. 3475 * The limit consists of an average frequency (icmp_pkt_err_interval measured 3476 * in milliseconds) and a burst size. Burst size number of packets can 3477 * be sent arbitrarely closely spaced. 3478 * The state is tracked using two variables to implement an approximate 3479 * token bucket filter: 3480 * icmp_pkt_err_last - lbolt value when the last burst started 3481 * icmp_pkt_err_sent - number of packets sent in current burst 3482 */ 3483 boolean_t 3484 icmp_err_rate_limit(ip_stack_t *ipst) 3485 { 3486 clock_t now = TICK_TO_MSEC(lbolt); 3487 uint_t refilled; /* Number of packets refilled in tbf since last */ 3488 /* Guard against changes by loading into local variable */ 3489 uint_t err_interval = ipst->ips_ip_icmp_err_interval; 3490 3491 if (err_interval == 0) 3492 return (B_FALSE); 3493 3494 if (ipst->ips_icmp_pkt_err_last > now) { 3495 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 3496 ipst->ips_icmp_pkt_err_last = 0; 3497 ipst->ips_icmp_pkt_err_sent = 0; 3498 } 3499 /* 3500 * If we are in a burst update the token bucket filter. 3501 * Update the "last" time to be close to "now" but make sure 3502 * we don't loose precision. 3503 */ 3504 if (ipst->ips_icmp_pkt_err_sent != 0) { 3505 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval; 3506 if (refilled > ipst->ips_icmp_pkt_err_sent) { 3507 ipst->ips_icmp_pkt_err_sent = 0; 3508 } else { 3509 ipst->ips_icmp_pkt_err_sent -= refilled; 3510 ipst->ips_icmp_pkt_err_last += refilled * err_interval; 3511 } 3512 } 3513 if (ipst->ips_icmp_pkt_err_sent == 0) { 3514 /* Start of new burst */ 3515 ipst->ips_icmp_pkt_err_last = now; 3516 } 3517 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) { 3518 ipst->ips_icmp_pkt_err_sent++; 3519 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 3520 ipst->ips_icmp_pkt_err_sent)); 3521 return (B_FALSE); 3522 } 3523 ip1dbg(("icmp_err_rate_limit: dropped\n")); 3524 return (B_TRUE); 3525 } 3526 3527 /* 3528 * Check if it is ok to send an IPv4 ICMP error packet in 3529 * response to the IPv4 packet in mp. 3530 * Free the message and return null if no 3531 * ICMP error packet should be sent. 3532 */ 3533 static mblk_t * 3534 icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst) 3535 { 3536 icmph_t *icmph; 3537 ipha_t *ipha; 3538 uint_t len_needed; 3539 ire_t *src_ire; 3540 ire_t *dst_ire; 3541 3542 if (!mp) 3543 return (NULL); 3544 ipha = (ipha_t *)mp->b_rptr; 3545 if (ip_csum_hdr(ipha)) { 3546 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); 3547 freemsg(mp); 3548 return (NULL); 3549 } 3550 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, 3551 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3552 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, 3553 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3554 if (src_ire != NULL || dst_ire != NULL || 3555 CLASSD(ipha->ipha_dst) || 3556 CLASSD(ipha->ipha_src) || 3557 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3558 /* Note: only errors to the fragment with offset 0 */ 3559 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3560 freemsg(mp); 3561 if (src_ire != NULL) 3562 ire_refrele(src_ire); 3563 if (dst_ire != NULL) 3564 ire_refrele(dst_ire); 3565 return (NULL); 3566 } 3567 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3568 /* 3569 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3570 * errors in response to any ICMP errors. 3571 */ 3572 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3573 if (mp->b_wptr - mp->b_rptr < len_needed) { 3574 if (!pullupmsg(mp, len_needed)) { 3575 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3576 freemsg(mp); 3577 return (NULL); 3578 } 3579 ipha = (ipha_t *)mp->b_rptr; 3580 } 3581 icmph = (icmph_t *) 3582 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3583 switch (icmph->icmph_type) { 3584 case ICMP_DEST_UNREACHABLE: 3585 case ICMP_SOURCE_QUENCH: 3586 case ICMP_TIME_EXCEEDED: 3587 case ICMP_PARAM_PROBLEM: 3588 case ICMP_REDIRECT: 3589 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3590 freemsg(mp); 3591 return (NULL); 3592 default: 3593 break; 3594 } 3595 } 3596 /* 3597 * If this is a labeled system, then check to see if we're allowed to 3598 * send a response to this particular sender. If not, then just drop. 3599 */ 3600 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 3601 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); 3602 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3603 freemsg(mp); 3604 return (NULL); 3605 } 3606 if (icmp_err_rate_limit(ipst)) { 3607 /* 3608 * Only send ICMP error packets every so often. 3609 * This should be done on a per port/source basis, 3610 * but for now this will suffice. 3611 */ 3612 freemsg(mp); 3613 return (NULL); 3614 } 3615 return (mp); 3616 } 3617 3618 /* 3619 * Generate an ICMP redirect message. 3620 */ 3621 static void 3622 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst) 3623 { 3624 icmph_t icmph; 3625 3626 /* 3627 * We are called from ip_rput where we could 3628 * not have attached an IPSEC_IN. 3629 */ 3630 ASSERT(mp->b_datap->db_type == M_DATA); 3631 3632 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3633 return; 3634 } 3635 3636 bzero(&icmph, sizeof (icmph_t)); 3637 icmph.icmph_type = ICMP_REDIRECT; 3638 icmph.icmph_code = 1; 3639 icmph.icmph_rd_gateway = gateway; 3640 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects); 3641 /* Redirects sent by router, and router is global zone */ 3642 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst); 3643 } 3644 3645 /* 3646 * Generate an ICMP time exceeded message. 3647 */ 3648 void 3649 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3650 ip_stack_t *ipst) 3651 { 3652 icmph_t icmph; 3653 boolean_t mctl_present; 3654 mblk_t *first_mp; 3655 3656 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3657 3658 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3659 if (mctl_present) 3660 freeb(first_mp); 3661 return; 3662 } 3663 3664 bzero(&icmph, sizeof (icmph_t)); 3665 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3666 icmph.icmph_code = code; 3667 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds); 3668 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3669 ipst); 3670 } 3671 3672 /* 3673 * Generate an ICMP unreachable message. 3674 */ 3675 void 3676 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3677 ip_stack_t *ipst) 3678 { 3679 icmph_t icmph; 3680 mblk_t *first_mp; 3681 boolean_t mctl_present; 3682 3683 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3684 3685 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3686 if (mctl_present) 3687 freeb(first_mp); 3688 return; 3689 } 3690 3691 bzero(&icmph, sizeof (icmph_t)); 3692 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3693 icmph.icmph_code = code; 3694 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 3695 ip2dbg(("send icmp destination unreachable code %d\n", code)); 3696 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present, 3697 zoneid, ipst); 3698 } 3699 3700 /* 3701 * Attempt to start recovery of an IPv4 interface that's been shut down as a 3702 * duplicate. As long as someone else holds the address, the interface will 3703 * stay down. When that conflict goes away, the interface is brought back up. 3704 * This is done so that accidental shutdowns of addresses aren't made 3705 * permanent. Your server will recover from a failure. 3706 * 3707 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a 3708 * user space process (dhcpagent). 3709 * 3710 * Recovery completes if ARP reports that the address is now ours (via 3711 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation. 3712 * 3713 * This function is entered on a timer expiry; the ID is in ipif_recovery_id. 3714 */ 3715 static void 3716 ipif_dup_recovery(void *arg) 3717 { 3718 ipif_t *ipif = arg; 3719 ill_t *ill = ipif->ipif_ill; 3720 mblk_t *arp_add_mp; 3721 mblk_t *arp_del_mp; 3722 area_t *area; 3723 ip_stack_t *ipst = ill->ill_ipst; 3724 3725 ipif->ipif_recovery_id = 0; 3726 3727 /* 3728 * No lock needed for moving or condemned check, as this is just an 3729 * optimization. 3730 */ 3731 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || 3732 (ipif->ipif_flags & IPIF_POINTOPOINT) || 3733 (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) { 3734 /* No reason to try to bring this address back. */ 3735 return; 3736 } 3737 3738 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL) 3739 goto alloc_fail; 3740 3741 if (ipif->ipif_arp_del_mp == NULL) { 3742 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) 3743 goto alloc_fail; 3744 ipif->ipif_arp_del_mp = arp_del_mp; 3745 } 3746 3747 /* Setting the 'unverified' flag restarts DAD */ 3748 area = (area_t *)arp_add_mp->b_rptr; 3749 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | 3750 ACE_F_UNVERIFIED; 3751 putnext(ill->ill_rq, arp_add_mp); 3752 return; 3753 3754 alloc_fail: 3755 /* 3756 * On allocation failure, just restart the timer. Note that the ipif 3757 * is down here, so no other thread could be trying to start a recovery 3758 * timer. The ill_lock protects the condemned flag and the recovery 3759 * timer ID. 3760 */ 3761 freemsg(arp_add_mp); 3762 mutex_enter(&ill->ill_lock); 3763 if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 && 3764 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 3765 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, 3766 MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3767 } 3768 mutex_exit(&ill->ill_lock); 3769 } 3770 3771 /* 3772 * This is for exclusive changes due to ARP. Either tear down an interface due 3773 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. 3774 */ 3775 /* ARGSUSED */ 3776 static void 3777 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3778 { 3779 ill_t *ill = rq->q_ptr; 3780 arh_t *arh; 3781 ipaddr_t src; 3782 ipif_t *ipif; 3783 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ 3784 char hbuf[MAC_STR_LEN]; 3785 char sbuf[INET_ADDRSTRLEN]; 3786 const char *failtype; 3787 boolean_t bring_up; 3788 ip_stack_t *ipst = ill->ill_ipst; 3789 3790 switch (((arcn_t *)mp->b_rptr)->arcn_code) { 3791 case AR_CN_READY: 3792 failtype = NULL; 3793 bring_up = B_TRUE; 3794 break; 3795 case AR_CN_FAILED: 3796 failtype = "in use"; 3797 bring_up = B_FALSE; 3798 break; 3799 default: 3800 failtype = "claimed"; 3801 bring_up = B_FALSE; 3802 break; 3803 } 3804 3805 arh = (arh_t *)mp->b_cont->b_rptr; 3806 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3807 3808 /* Handle failures due to probes */ 3809 if (src == 0) { 3810 bcopy((char *)&arh[1] + 2 * arh->arh_hlen + IP_ADDR_LEN, &src, 3811 IP_ADDR_LEN); 3812 } 3813 3814 (void) strlcpy(ibuf, ill->ill_name, sizeof (ibuf)); 3815 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, 3816 sizeof (hbuf)); 3817 (void) ip_dot_addr(src, sbuf); 3818 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3819 3820 if ((ipif->ipif_flags & IPIF_POINTOPOINT) || 3821 ipif->ipif_lcl_addr != src) { 3822 continue; 3823 } 3824 3825 /* 3826 * If we failed on a recovery probe, then restart the timer to 3827 * try again later. 3828 */ 3829 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) && 3830 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3831 ill->ill_net_type == IRE_IF_RESOLVER && 3832 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3833 ipst->ips_ip_dup_recovery > 0 && 3834 ipif->ipif_recovery_id == 0) { 3835 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3836 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3837 continue; 3838 } 3839 3840 /* 3841 * If what we're trying to do has already been done, then do 3842 * nothing. 3843 */ 3844 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0)) 3845 continue; 3846 3847 if (ipif->ipif_id != 0) { 3848 (void) snprintf(ibuf + ill->ill_name_length - 1, 3849 sizeof (ibuf) - ill->ill_name_length + 1, ":%d", 3850 ipif->ipif_id); 3851 } 3852 if (failtype == NULL) { 3853 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf, 3854 ibuf); 3855 } else { 3856 cmn_err(CE_WARN, "%s has duplicate address %s (%s " 3857 "by %s); disabled", ibuf, sbuf, failtype, hbuf); 3858 } 3859 3860 if (bring_up) { 3861 ASSERT(ill->ill_dl_up); 3862 /* 3863 * Free up the ARP delete message so we can allocate 3864 * a fresh one through the normal path. 3865 */ 3866 freemsg(ipif->ipif_arp_del_mp); 3867 ipif->ipif_arp_del_mp = NULL; 3868 if (ipif_resolver_up(ipif, Res_act_initial) != 3869 EINPROGRESS) { 3870 ipif->ipif_addr_ready = 1; 3871 (void) ipif_up_done(ipif); 3872 } 3873 continue; 3874 } 3875 3876 mutex_enter(&ill->ill_lock); 3877 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 3878 ipif->ipif_flags |= IPIF_DUPLICATE; 3879 ill->ill_ipif_dup_count++; 3880 mutex_exit(&ill->ill_lock); 3881 /* 3882 * Already exclusive on the ill; no need to handle deferred 3883 * processing here. 3884 */ 3885 (void) ipif_down(ipif, NULL, NULL); 3886 ipif_down_tail(ipif); 3887 mutex_enter(&ill->ill_lock); 3888 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3889 ill->ill_net_type == IRE_IF_RESOLVER && 3890 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3891 ipst->ips_ip_dup_recovery > 0) { 3892 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3893 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3894 } 3895 mutex_exit(&ill->ill_lock); 3896 } 3897 freemsg(mp); 3898 } 3899 3900 /* ARGSUSED */ 3901 static void 3902 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3903 { 3904 ill_t *ill = rq->q_ptr; 3905 arh_t *arh; 3906 ipaddr_t src; 3907 ipif_t *ipif; 3908 3909 arh = (arh_t *)mp->b_cont->b_rptr; 3910 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3911 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3912 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) 3913 (void) ipif_resolver_up(ipif, Res_act_defend); 3914 } 3915 freemsg(mp); 3916 } 3917 3918 /* 3919 * News from ARP. ARP sends notification of interesting events down 3920 * to its clients using M_CTL messages with the interesting ARP packet 3921 * attached via b_cont. 3922 * The interesting event from a device comes up the corresponding ARP-IP-DEV 3923 * queue as opposed to ARP sending the message to all the clients, i.e. all 3924 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 3925 * table if a cache IRE is found to delete all the entries for the address in 3926 * the packet. 3927 */ 3928 static void 3929 ip_arp_news(queue_t *q, mblk_t *mp) 3930 { 3931 arcn_t *arcn; 3932 arh_t *arh; 3933 ire_t *ire = NULL; 3934 char hbuf[MAC_STR_LEN]; 3935 char sbuf[INET_ADDRSTRLEN]; 3936 ipaddr_t src; 3937 in6_addr_t v6src; 3938 boolean_t isv6 = B_FALSE; 3939 ipif_t *ipif; 3940 ill_t *ill; 3941 ip_stack_t *ipst; 3942 3943 if (CONN_Q(q)) { 3944 conn_t *connp = Q_TO_CONN(q); 3945 3946 ipst = connp->conn_netstack->netstack_ip; 3947 } else { 3948 ill_t *ill = (ill_t *)q->q_ptr; 3949 3950 ipst = ill->ill_ipst; 3951 } 3952 3953 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 3954 if (q->q_next) { 3955 putnext(q, mp); 3956 } else 3957 freemsg(mp); 3958 return; 3959 } 3960 arh = (arh_t *)mp->b_cont->b_rptr; 3961 /* Is it one we are interested in? */ 3962 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 3963 isv6 = B_TRUE; 3964 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 3965 IPV6_ADDR_LEN); 3966 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 3967 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 3968 IP_ADDR_LEN); 3969 } else { 3970 freemsg(mp); 3971 return; 3972 } 3973 3974 ill = q->q_ptr; 3975 3976 arcn = (arcn_t *)mp->b_rptr; 3977 switch (arcn->arcn_code) { 3978 case AR_CN_BOGON: 3979 /* 3980 * Someone is sending ARP packets with a source protocol 3981 * address that we have published and for which we believe our 3982 * entry is authoritative and (when ill_arp_extend is set) 3983 * verified to be unique on the network. 3984 * 3985 * The ARP module internally handles the cases where the sender 3986 * is just probing (for DAD) and where the hardware address of 3987 * a non-authoritative entry has changed. Thus, these are the 3988 * real conflicts, and we have to do resolution. 3989 * 3990 * We back away quickly from the address if it's from DHCP or 3991 * otherwise temporary and hasn't been used recently (or at 3992 * all). We'd like to include "deprecated" addresses here as 3993 * well (as there's no real reason to defend something we're 3994 * discarding), but IPMP "reuses" this flag to mean something 3995 * other than the standard meaning. 3996 * 3997 * If the ARP module above is not extended (meaning that it 3998 * doesn't know how to defend the address), then we just log 3999 * the problem as we always did and continue on. It's not 4000 * right, but there's little else we can do, and those old ATM 4001 * users are going away anyway. 4002 */ 4003 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, 4004 hbuf, sizeof (hbuf)); 4005 (void) ip_dot_addr(src, sbuf); 4006 if (isv6) { 4007 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL, 4008 ipst); 4009 } else { 4010 ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst); 4011 } 4012 if (ire != NULL && IRE_IS_LOCAL(ire)) { 4013 uint32_t now; 4014 uint32_t maxage; 4015 clock_t lused; 4016 uint_t maxdefense; 4017 uint_t defs; 4018 4019 /* 4020 * First, figure out if this address hasn't been used 4021 * in a while. If it hasn't, then it's a better 4022 * candidate for abandoning. 4023 */ 4024 ipif = ire->ire_ipif; 4025 ASSERT(ipif != NULL); 4026 now = gethrestime_sec(); 4027 maxage = now - ire->ire_create_time; 4028 if (maxage > ipst->ips_ip_max_temp_idle) 4029 maxage = ipst->ips_ip_max_temp_idle; 4030 lused = drv_hztousec(ddi_get_lbolt() - 4031 ire->ire_last_used_time) / MICROSEC + 1; 4032 if (lused >= maxage && (ipif->ipif_flags & 4033 (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) 4034 maxdefense = ipst->ips_ip_max_temp_defend; 4035 else 4036 maxdefense = ipst->ips_ip_max_defend; 4037 4038 /* 4039 * Now figure out how many times we've defended 4040 * ourselves. Ignore defenses that happened long in 4041 * the past. 4042 */ 4043 mutex_enter(&ire->ire_lock); 4044 if ((defs = ire->ire_defense_count) > 0 && 4045 now - ire->ire_defense_time > 4046 ipst->ips_ip_defend_interval) { 4047 ire->ire_defense_count = defs = 0; 4048 } 4049 ire->ire_defense_count++; 4050 ire->ire_defense_time = now; 4051 mutex_exit(&ire->ire_lock); 4052 ill_refhold(ill); 4053 ire_refrele(ire); 4054 4055 /* 4056 * If we've defended ourselves too many times already, 4057 * then give up and tear down the interface(s) using 4058 * this address. Otherwise, defend by sending out a 4059 * gratuitous ARP. 4060 */ 4061 if (defs >= maxdefense && ill->ill_arp_extend) { 4062 (void) qwriter_ip(NULL, ill, q, mp, 4063 ip_arp_excl, CUR_OP, B_FALSE); 4064 } else { 4065 cmn_err(CE_WARN, 4066 "node %s is using our IP address %s on %s", 4067 hbuf, sbuf, ill->ill_name); 4068 /* 4069 * If this is an old (ATM) ARP module, then 4070 * don't try to defend the address. Remain 4071 * compatible with the old behavior. Defend 4072 * only with new ARP. 4073 */ 4074 if (ill->ill_arp_extend) { 4075 (void) qwriter_ip(NULL, ill, q, mp, 4076 ip_arp_defend, CUR_OP, B_FALSE); 4077 } else { 4078 ill_refrele(ill); 4079 } 4080 } 4081 return; 4082 } 4083 cmn_err(CE_WARN, 4084 "proxy ARP problem? Node '%s' is using %s on %s", 4085 hbuf, sbuf, ill->ill_name); 4086 if (ire != NULL) 4087 ire_refrele(ire); 4088 break; 4089 case AR_CN_ANNOUNCE: 4090 if (isv6) { 4091 /* 4092 * For XRESOLV interfaces. 4093 * Delete the IRE cache entry and NCE for this 4094 * v6 address 4095 */ 4096 ip_ire_clookup_and_delete_v6(&v6src, ipst); 4097 /* 4098 * If v6src is a non-zero, it's a router address 4099 * as below. Do the same sort of thing to clean 4100 * out off-net IRE_CACHE entries that go through 4101 * the router. 4102 */ 4103 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4104 ire_walk_v6(ire_delete_cache_gw_v6, 4105 (char *)&v6src, ALL_ZONES, ipst); 4106 } 4107 } else { 4108 nce_hw_map_t hwm; 4109 4110 /* 4111 * ARP gives us a copy of any packet where it thinks 4112 * the address has changed, so that we can update our 4113 * caches. We're responsible for caching known answers 4114 * in the current design. We check whether the 4115 * hardware address really has changed in all of our 4116 * entries that have cached this mapping, and if so, we 4117 * blow them away. This way we will immediately pick 4118 * up the rare case of a host changing hardware 4119 * address. 4120 */ 4121 if (src == 0) 4122 break; 4123 hwm.hwm_addr = src; 4124 hwm.hwm_hwlen = arh->arh_hlen; 4125 hwm.hwm_hwaddr = (uchar_t *)(arh + 1); 4126 ndp_walk_common(ipst->ips_ndp4, NULL, 4127 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); 4128 } 4129 break; 4130 case AR_CN_READY: 4131 /* No external v6 resolver has a contract to use this */ 4132 if (isv6) 4133 break; 4134 /* If the link is down, we'll retry this later */ 4135 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) 4136 break; 4137 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, 4138 NULL, NULL, ipst); 4139 if (ipif != NULL) { 4140 /* 4141 * If this is a duplicate recovery, then we now need to 4142 * go exclusive to bring this thing back up. 4143 */ 4144 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == 4145 IPIF_DUPLICATE) { 4146 ipif_refrele(ipif); 4147 ill_refhold(ill); 4148 (void) qwriter_ip(NULL, ill, q, mp, 4149 ip_arp_excl, CUR_OP, B_FALSE); 4150 return; 4151 } 4152 /* 4153 * If this is the first notice that this address is 4154 * ready, then let the user know now. 4155 */ 4156 if ((ipif->ipif_flags & IPIF_UP) && 4157 !ipif->ipif_addr_ready) { 4158 ipif_mask_reply(ipif); 4159 ip_rts_ifmsg(ipif); 4160 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 4161 sctp_update_ipif(ipif, SCTP_IPIF_UP); 4162 } 4163 ipif->ipif_addr_ready = 1; 4164 ipif_refrele(ipif); 4165 } 4166 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp), ipst); 4167 if (ire != NULL) { 4168 ire->ire_defense_count = 0; 4169 ire_refrele(ire); 4170 } 4171 break; 4172 case AR_CN_FAILED: 4173 /* No external v6 resolver has a contract to use this */ 4174 if (isv6) 4175 break; 4176 ill_refhold(ill); 4177 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_excl, CUR_OP, 4178 B_FALSE); 4179 return; 4180 } 4181 freemsg(mp); 4182 } 4183 4184 /* 4185 * Create a mblk suitable for carrying the interface index and/or source link 4186 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 4187 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 4188 * application. 4189 */ 4190 mblk_t * 4191 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid, 4192 ip_stack_t *ipst) 4193 { 4194 mblk_t *mp; 4195 ip_pktinfo_t *pinfo; 4196 ipha_t *ipha; 4197 struct ether_header *pether; 4198 4199 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED); 4200 if (mp == NULL) { 4201 ip1dbg(("ip_add_info: allocation failure.\n")); 4202 return (data_mp); 4203 } 4204 4205 ipha = (ipha_t *)data_mp->b_rptr; 4206 pinfo = (ip_pktinfo_t *)mp->b_rptr; 4207 bzero(pinfo, sizeof (ip_pktinfo_t)); 4208 pinfo->ip_pkt_flags = (uchar_t)flags; 4209 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 4210 4211 if (flags & (IPF_RECVIF | IPF_RECVADDR)) 4212 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 4213 if (flags & IPF_RECVADDR) { 4214 ipif_t *ipif; 4215 ire_t *ire; 4216 4217 /* 4218 * Only valid for V4 4219 */ 4220 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) == 4221 (IPV4_VERSION << 4)); 4222 4223 ipif = ipif_get_next_ipif(NULL, ill); 4224 if (ipif != NULL) { 4225 /* 4226 * Since a decision has already been made to deliver the 4227 * packet, there is no need to test for SECATTR and 4228 * ZONEONLY. 4229 */ 4230 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 0, ipif, 4231 zoneid, NULL, MATCH_IRE_ILL_GROUP, ipst); 4232 if (ire == NULL) { 4233 /* 4234 * packet must have come on a different 4235 * interface. 4236 * Since a decision has already been made to 4237 * deliver the packet, there is no need to test 4238 * for SECATTR and ZONEONLY. 4239 */ 4240 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 0, 4241 ipif, zoneid, NULL, NULL, ipst); 4242 } 4243 4244 if (ire == NULL) { 4245 /* 4246 * This is either a multicast packet or 4247 * the address has been removed since 4248 * the packet was received. 4249 * Return INADDR_ANY so that normal source 4250 * selection occurs for the response. 4251 */ 4252 4253 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4254 } else { 4255 ASSERT(ire->ire_type != IRE_CACHE); 4256 pinfo->ip_pkt_match_addr.s_addr = 4257 ire->ire_src_addr; 4258 ire_refrele(ire); 4259 } 4260 ipif_refrele(ipif); 4261 } else { 4262 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4263 } 4264 } 4265 4266 pether = (struct ether_header *)((char *)ipha 4267 - sizeof (struct ether_header)); 4268 /* 4269 * Make sure the interface is an ethernet type, since this option 4270 * is currently supported only on this type of interface. Also make 4271 * sure we are pointing correctly above db_base. 4272 */ 4273 4274 if ((flags & IPF_RECVSLLA) && 4275 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 4276 (ill->ill_type == IFT_ETHER) && 4277 (ill->ill_net_type == IRE_IF_RESOLVER)) { 4278 4279 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER; 4280 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, 4281 (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL); 4282 } else { 4283 /* 4284 * Clear the bit. Indicate to upper layer that IP is not 4285 * sending this ancillary info. 4286 */ 4287 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA; 4288 } 4289 4290 mp->b_datap->db_type = M_CTL; 4291 mp->b_wptr += sizeof (ip_pktinfo_t); 4292 mp->b_cont = data_mp; 4293 4294 return (mp); 4295 } 4296 4297 /* 4298 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 4299 * part of the bind request. 4300 */ 4301 4302 boolean_t 4303 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 4304 { 4305 ipsec_in_t *ii; 4306 4307 ASSERT(policy_mp != NULL); 4308 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 4309 4310 ii = (ipsec_in_t *)policy_mp->b_rptr; 4311 ASSERT(ii->ipsec_in_type == IPSEC_IN); 4312 4313 connp->conn_policy = ii->ipsec_in_policy; 4314 ii->ipsec_in_policy = NULL; 4315 4316 if (ii->ipsec_in_action != NULL) { 4317 if (connp->conn_latch == NULL) { 4318 connp->conn_latch = iplatch_create(); 4319 if (connp->conn_latch == NULL) 4320 return (B_FALSE); 4321 } 4322 ipsec_latch_inbound(connp->conn_latch, ii); 4323 } 4324 return (B_TRUE); 4325 } 4326 4327 /* 4328 * Upper level protocols (ULP) pass through bind requests to IP for inspection 4329 * and to arrange for power-fanout assist. The ULP is identified by 4330 * adding a single byte at the end of the original bind message. 4331 * A ULP other than UDP or TCP that wishes to be recognized passes 4332 * down a bind with a zero length address. 4333 * 4334 * The binding works as follows: 4335 * - A zero byte address means just bind to the protocol. 4336 * - A four byte address is treated as a request to validate 4337 * that the address is a valid local address, appropriate for 4338 * an application to bind to. This does not affect any fanout 4339 * information in IP. 4340 * - A sizeof sin_t byte address is used to bind to only the local address 4341 * and port. 4342 * - A sizeof ipa_conn_t byte address contains complete fanout information 4343 * consisting of local and remote addresses and ports. In 4344 * this case, the addresses are both validated as appropriate 4345 * for this operation, and, if so, the information is retained 4346 * for use in the inbound fanout. 4347 * 4348 * The ULP (except in the zero-length bind) can append an 4349 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 4350 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 4351 * a copy of the source or destination IRE (source for local bind; 4352 * destination for complete bind). IPSEC_POLICY_SET indicates that the 4353 * policy information contained should be copied on to the conn. 4354 * 4355 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 4356 */ 4357 mblk_t * 4358 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 4359 { 4360 ssize_t len; 4361 struct T_bind_req *tbr; 4362 sin_t *sin; 4363 ipa_conn_t *ac; 4364 uchar_t *ucp; 4365 mblk_t *mp1; 4366 boolean_t ire_requested; 4367 boolean_t ipsec_policy_set = B_FALSE; 4368 int error = 0; 4369 int protocol; 4370 ipa_conn_x_t *acx; 4371 4372 ASSERT(!connp->conn_af_isv6); 4373 connp->conn_pkt_isv6 = B_FALSE; 4374 4375 len = MBLKL(mp); 4376 if (len < (sizeof (*tbr) + 1)) { 4377 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 4378 "ip_bind: bogus msg, len %ld", len); 4379 /* XXX: Need to return something better */ 4380 goto bad_addr; 4381 } 4382 /* Back up and extract the protocol identifier. */ 4383 mp->b_wptr--; 4384 protocol = *mp->b_wptr & 0xFF; 4385 tbr = (struct T_bind_req *)mp->b_rptr; 4386 /* Reset the message type in preparation for shipping it back. */ 4387 DB_TYPE(mp) = M_PCPROTO; 4388 4389 connp->conn_ulp = (uint8_t)protocol; 4390 4391 /* 4392 * Check for a zero length address. This is from a protocol that 4393 * wants to register to receive all packets of its type. 4394 */ 4395 if (tbr->ADDR_length == 0) { 4396 /* 4397 * These protocols are now intercepted in ip_bind_v6(). 4398 * Reject protocol-level binds here for now. 4399 * 4400 * For SCTP raw socket, ICMP sends down a bind with sin_t 4401 * so that the protocol type cannot be SCTP. 4402 */ 4403 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 4404 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 4405 goto bad_addr; 4406 } 4407 4408 /* 4409 * 4410 * The udp module never sends down a zero-length address, 4411 * and allowing this on a labeled system will break MLP 4412 * functionality. 4413 */ 4414 if (is_system_labeled() && protocol == IPPROTO_UDP) 4415 goto bad_addr; 4416 4417 if (connp->conn_mac_exempt) 4418 goto bad_addr; 4419 4420 /* No hash here really. The table is big enough. */ 4421 connp->conn_srcv6 = ipv6_all_zeros; 4422 4423 ipcl_proto_insert(connp, protocol); 4424 4425 tbr->PRIM_type = T_BIND_ACK; 4426 return (mp); 4427 } 4428 4429 /* Extract the address pointer from the message. */ 4430 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 4431 tbr->ADDR_length); 4432 if (ucp == NULL) { 4433 ip1dbg(("ip_bind: no address\n")); 4434 goto bad_addr; 4435 } 4436 if (!OK_32PTR(ucp)) { 4437 ip1dbg(("ip_bind: unaligned address\n")); 4438 goto bad_addr; 4439 } 4440 /* 4441 * Check for trailing mps. 4442 */ 4443 4444 mp1 = mp->b_cont; 4445 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 4446 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); 4447 4448 switch (tbr->ADDR_length) { 4449 default: 4450 ip1dbg(("ip_bind: bad address length %d\n", 4451 (int)tbr->ADDR_length)); 4452 goto bad_addr; 4453 4454 case IP_ADDR_LEN: 4455 /* Verification of local address only */ 4456 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, 4457 ire_requested, ipsec_policy_set, B_FALSE); 4458 break; 4459 4460 case sizeof (sin_t): 4461 sin = (sin_t *)ucp; 4462 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, 4463 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); 4464 break; 4465 4466 case sizeof (ipa_conn_t): 4467 ac = (ipa_conn_t *)ucp; 4468 /* For raw socket, the local port is not set. */ 4469 if (ac->ac_lport == 0) 4470 ac->ac_lport = connp->conn_lport; 4471 /* Always verify destination reachability. */ 4472 error = ip_bind_connected(connp, mp, &ac->ac_laddr, 4473 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, 4474 ipsec_policy_set, B_TRUE, B_TRUE); 4475 break; 4476 4477 case sizeof (ipa_conn_x_t): 4478 acx = (ipa_conn_x_t *)ucp; 4479 /* 4480 * Whether or not to verify destination reachability depends 4481 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 4482 */ 4483 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, 4484 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, 4485 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, 4486 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); 4487 break; 4488 } 4489 if (error == EINPROGRESS) 4490 return (NULL); 4491 else if (error != 0) 4492 goto bad_addr; 4493 /* 4494 * Pass the IPSEC headers size in ire_ipsec_overhead. 4495 * We can't do this in ip_bind_insert_ire because the policy 4496 * may not have been inherited at that point in time and hence 4497 * conn_out_enforce_policy may not be set. 4498 */ 4499 mp1 = mp->b_cont; 4500 if (ire_requested && connp->conn_out_enforce_policy && 4501 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 4502 ire_t *ire = (ire_t *)mp1->b_rptr; 4503 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 4504 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 4505 } 4506 4507 /* Send it home. */ 4508 mp->b_datap->db_type = M_PCPROTO; 4509 tbr->PRIM_type = T_BIND_ACK; 4510 return (mp); 4511 4512 bad_addr: 4513 /* 4514 * If error = -1 then we generate a TBADADDR - otherwise error is 4515 * a unix errno. 4516 */ 4517 if (error > 0) 4518 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 4519 else 4520 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 4521 return (mp); 4522 } 4523 4524 /* 4525 * Here address is verified to be a valid local address. 4526 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 4527 * address is also considered a valid local address. 4528 * In the case of a broadcast/multicast address, however, the 4529 * upper protocol is expected to reset the src address 4530 * to 0 if it sees a IRE_BROADCAST type returned so that 4531 * no packets are emitted with broadcast/multicast address as 4532 * source address (that violates hosts requirements RFC1122) 4533 * The addresses valid for bind are: 4534 * (1) - INADDR_ANY (0) 4535 * (2) - IP address of an UP interface 4536 * (3) - IP address of a DOWN interface 4537 * (4) - valid local IP broadcast addresses. In this case 4538 * the conn will only receive packets destined to 4539 * the specified broadcast address. 4540 * (5) - a multicast address. In this case 4541 * the conn will only receive packets destined to 4542 * the specified multicast address. Note: the 4543 * application still has to issue an 4544 * IP_ADD_MEMBERSHIP socket option. 4545 * 4546 * On error, return -1 for TBADADDR otherwise pass the 4547 * errno with TSYSERR reply. 4548 * 4549 * In all the above cases, the bound address must be valid in the current zone. 4550 * When the address is loopback, multicast or broadcast, there might be many 4551 * matching IREs so bind has to look up based on the zone. 4552 * 4553 * Note: lport is in network byte order. 4554 */ 4555 int 4556 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, 4557 boolean_t ire_requested, boolean_t ipsec_policy_set, 4558 boolean_t fanout_insert) 4559 { 4560 int error = 0; 4561 ire_t *src_ire; 4562 mblk_t *policy_mp; 4563 ipif_t *ipif; 4564 zoneid_t zoneid; 4565 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4566 4567 if (ipsec_policy_set) { 4568 policy_mp = mp->b_cont; 4569 } 4570 4571 /* 4572 * If it was previously connected, conn_fully_bound would have 4573 * been set. 4574 */ 4575 connp->conn_fully_bound = B_FALSE; 4576 4577 src_ire = NULL; 4578 ipif = NULL; 4579 4580 zoneid = IPCL_ZONEID(connp); 4581 4582 if (src_addr) { 4583 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 4584 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 4585 /* 4586 * If an address other than 0.0.0.0 is requested, 4587 * we verify that it is a valid address for bind 4588 * Note: Following code is in if-else-if form for 4589 * readability compared to a condition check. 4590 */ 4591 /* LINTED - statement has no consequent */ 4592 if (IRE_IS_LOCAL(src_ire)) { 4593 /* 4594 * (2) Bind to address of local UP interface 4595 */ 4596 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 4597 /* 4598 * (4) Bind to broadcast address 4599 * Note: permitted only from transports that 4600 * request IRE 4601 */ 4602 if (!ire_requested) 4603 error = EADDRNOTAVAIL; 4604 } else { 4605 /* 4606 * (3) Bind to address of local DOWN interface 4607 * (ipif_lookup_addr() looks up all interfaces 4608 * but we do not get here for UP interfaces 4609 * - case (2) above) 4610 * We put the protocol byte back into the mblk 4611 * since we may come back via ip_wput_nondata() 4612 * later with this mblk if ipif_lookup_addr chooses 4613 * to defer processing. 4614 */ 4615 *mp->b_wptr++ = (char)connp->conn_ulp; 4616 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, 4617 CONNP_TO_WQ(connp), mp, ip_wput_nondata, 4618 &error, ipst)) != NULL) { 4619 ipif_refrele(ipif); 4620 } else if (error == EINPROGRESS) { 4621 if (src_ire != NULL) 4622 ire_refrele(src_ire); 4623 return (EINPROGRESS); 4624 } else if (CLASSD(src_addr)) { 4625 error = 0; 4626 if (src_ire != NULL) 4627 ire_refrele(src_ire); 4628 /* 4629 * (5) bind to multicast address. 4630 * Fake out the IRE returned to upper 4631 * layer to be a broadcast IRE. 4632 */ 4633 src_ire = ire_ctable_lookup( 4634 INADDR_BROADCAST, INADDR_ANY, 4635 IRE_BROADCAST, NULL, zoneid, NULL, 4636 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY), 4637 ipst); 4638 if (src_ire == NULL || !ire_requested) 4639 error = EADDRNOTAVAIL; 4640 } else { 4641 /* 4642 * Not a valid address for bind 4643 */ 4644 error = EADDRNOTAVAIL; 4645 } 4646 /* 4647 * Just to keep it consistent with the processing in 4648 * ip_bind_v4() 4649 */ 4650 mp->b_wptr--; 4651 } 4652 if (error) { 4653 /* Red Alert! Attempting to be a bogon! */ 4654 ip1dbg(("ip_bind: bad src address 0x%x\n", 4655 ntohl(src_addr))); 4656 goto bad_addr; 4657 } 4658 } 4659 4660 /* 4661 * Allow setting new policies. For example, disconnects come 4662 * down as ipa_t bind. As we would have set conn_policy_cached 4663 * to B_TRUE before, we should set it to B_FALSE, so that policy 4664 * can change after the disconnect. 4665 */ 4666 connp->conn_policy_cached = B_FALSE; 4667 4668 /* 4669 * If not fanout_insert this was just an address verification 4670 */ 4671 if (fanout_insert) { 4672 /* 4673 * The addresses have been verified. Time to insert in 4674 * the correct fanout list. 4675 */ 4676 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4677 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4678 connp->conn_lport = lport; 4679 connp->conn_fport = 0; 4680 /* 4681 * Do we need to add a check to reject Multicast packets 4682 * 4683 * We need to make sure that the conn_recv is set to a non-null 4684 * value before we insert the conn into the classifier table. 4685 * This is to avoid a race with an incoming packet which does an 4686 * ipcl_classify(). 4687 */ 4688 if (*mp->b_wptr == IPPROTO_TCP) 4689 connp->conn_recv = tcp_conn_request; 4690 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); 4691 } 4692 4693 if (error == 0) { 4694 if (ire_requested) { 4695 if (!ip_bind_insert_ire(mp, src_ire, NULL, ipst)) { 4696 error = -1; 4697 /* Falls through to bad_addr */ 4698 } 4699 } else if (ipsec_policy_set) { 4700 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4701 error = -1; 4702 /* Falls through to bad_addr */ 4703 } 4704 } 4705 } else if (connp->conn_ulp == IPPROTO_TCP) { 4706 connp->conn_recv = tcp_input; 4707 } 4708 bad_addr: 4709 if (error != 0) { 4710 if (connp->conn_anon_port) { 4711 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4712 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 4713 B_FALSE); 4714 } 4715 connp->conn_mlp_type = mlptSingle; 4716 } 4717 if (src_ire != NULL) 4718 IRE_REFRELE(src_ire); 4719 if (ipsec_policy_set) { 4720 ASSERT(policy_mp == mp->b_cont); 4721 ASSERT(policy_mp != NULL); 4722 freeb(policy_mp); 4723 /* 4724 * As of now assume that nothing else accompanies 4725 * IPSEC_POLICY_SET. 4726 */ 4727 mp->b_cont = NULL; 4728 } 4729 return (error); 4730 } 4731 4732 /* 4733 * Verify that both the source and destination addresses 4734 * are valid. If verify_dst is false, then the destination address may be 4735 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4736 * destination reachability, while tunnels do not. 4737 * Note that we allow connect to broadcast and multicast 4738 * addresses when ire_requested is set. Thus the ULP 4739 * has to check for IRE_BROADCAST and multicast. 4740 * 4741 * Returns zero if ok. 4742 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4743 * (for use with TSYSERR reply). 4744 * 4745 * Note: lport and fport are in network byte order. 4746 */ 4747 int 4748 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, 4749 uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4750 boolean_t ire_requested, boolean_t ipsec_policy_set, 4751 boolean_t fanout_insert, boolean_t verify_dst) 4752 { 4753 ire_t *src_ire; 4754 ire_t *dst_ire; 4755 int error = 0; 4756 int protocol; 4757 mblk_t *policy_mp; 4758 ire_t *sire = NULL; 4759 ire_t *md_dst_ire = NULL; 4760 ire_t *lso_dst_ire = NULL; 4761 ill_t *ill = NULL; 4762 zoneid_t zoneid; 4763 ipaddr_t src_addr = *src_addrp; 4764 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4765 4766 src_ire = dst_ire = NULL; 4767 protocol = *mp->b_wptr & 0xFF; 4768 4769 /* 4770 * If we never got a disconnect before, clear it now. 4771 */ 4772 connp->conn_fully_bound = B_FALSE; 4773 4774 if (ipsec_policy_set) { 4775 policy_mp = mp->b_cont; 4776 } 4777 4778 zoneid = IPCL_ZONEID(connp); 4779 4780 if (CLASSD(dst_addr)) { 4781 /* Pick up an IRE_BROADCAST */ 4782 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4783 NULL, zoneid, MBLK_GETLABEL(mp), 4784 (MATCH_IRE_RECURSIVE | 4785 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | 4786 MATCH_IRE_SECATTR), ipst); 4787 } else { 4788 /* 4789 * If conn_dontroute is set or if conn_nexthop_set is set, 4790 * and onlink ipif is not found set ENETUNREACH error. 4791 */ 4792 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4793 ipif_t *ipif; 4794 4795 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4796 dst_addr : connp->conn_nexthop_v4, zoneid, ipst); 4797 if (ipif == NULL) { 4798 error = ENETUNREACH; 4799 goto bad_addr; 4800 } 4801 ipif_refrele(ipif); 4802 } 4803 4804 if (connp->conn_nexthop_set) { 4805 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4806 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), 4807 MATCH_IRE_SECATTR, ipst); 4808 } else { 4809 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4810 &sire, zoneid, MBLK_GETLABEL(mp), 4811 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4812 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | 4813 MATCH_IRE_SECATTR), ipst); 4814 } 4815 } 4816 /* 4817 * dst_ire can't be a broadcast when not ire_requested. 4818 * We also prevent ire's with src address INADDR_ANY to 4819 * be used, which are created temporarily for 4820 * sending out packets from endpoints that have 4821 * conn_unspec_src set. If verify_dst is true, the destination must be 4822 * reachable. If verify_dst is false, the destination needn't be 4823 * reachable. 4824 * 4825 * If we match on a reject or black hole, then we've got a 4826 * local failure. May as well fail out the connect() attempt, 4827 * since it's never going to succeed. 4828 */ 4829 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4830 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4831 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4832 /* 4833 * If we're verifying destination reachability, we always want 4834 * to complain here. 4835 * 4836 * If we're not verifying destination reachability but the 4837 * destination has a route, we still want to fail on the 4838 * temporary address and broadcast address tests. 4839 */ 4840 if (verify_dst || (dst_ire != NULL)) { 4841 if (ip_debug > 2) { 4842 pr_addr_dbg("ip_bind_connected: bad connected " 4843 "dst %s\n", AF_INET, &dst_addr); 4844 } 4845 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4846 error = ENETUNREACH; 4847 else 4848 error = EHOSTUNREACH; 4849 goto bad_addr; 4850 } 4851 } 4852 4853 /* 4854 * We now know that routing will allow us to reach the destination. 4855 * Check whether Trusted Solaris policy allows communication with this 4856 * host, and pretend that the destination is unreachable if not. 4857 * 4858 * This is never a problem for TCP, since that transport is known to 4859 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 4860 * handling. If the remote is unreachable, it will be detected at that 4861 * point, so there's no reason to check it here. 4862 * 4863 * Note that for sendto (and other datagram-oriented friends), this 4864 * check is done as part of the data path label computation instead. 4865 * The check here is just to make non-TCP connect() report the right 4866 * error. 4867 */ 4868 if (dst_ire != NULL && is_system_labeled() && 4869 !IPCL_IS_TCP(connp) && 4870 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL, 4871 connp->conn_mac_exempt, ipst) != 0) { 4872 error = EHOSTUNREACH; 4873 if (ip_debug > 2) { 4874 pr_addr_dbg("ip_bind_connected: no label for dst %s\n", 4875 AF_INET, &dst_addr); 4876 } 4877 goto bad_addr; 4878 } 4879 4880 /* 4881 * If the app does a connect(), it means that it will most likely 4882 * send more than 1 packet to the destination. It makes sense 4883 * to clear the temporary flag. 4884 */ 4885 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4886 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4887 irb_t *irb = dst_ire->ire_bucket; 4888 4889 rw_enter(&irb->irb_lock, RW_WRITER); 4890 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4891 irb->irb_tmp_ire_cnt--; 4892 rw_exit(&irb->irb_lock); 4893 } 4894 4895 /* 4896 * See if we should notify ULP about LSO/MDT; we do this whether or not 4897 * ire_requested is TRUE, in order to handle active connects; LSO/MDT 4898 * eligibility tests for passive connects are handled separately 4899 * through tcp_adapt_ire(). We do this before the source address 4900 * selection, because dst_ire may change after a call to 4901 * ipif_select_source(). This is a best-effort check, as the 4902 * packet for this connection may not actually go through 4903 * dst_ire->ire_stq, and the exact IRE can only be known after 4904 * calling ip_newroute(). This is why we further check on the 4905 * IRE during LSO/Multidata packet transmission in 4906 * tcp_lsosend()/tcp_multisend(). 4907 */ 4908 if (!ipsec_policy_set && dst_ire != NULL && 4909 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4910 (ill = ire_to_ill(dst_ire), ill != NULL)) { 4911 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 4912 lso_dst_ire = dst_ire; 4913 IRE_REFHOLD(lso_dst_ire); 4914 } else if (ipst->ips_ip_multidata_outbound && 4915 ILL_MDT_CAPABLE(ill)) { 4916 md_dst_ire = dst_ire; 4917 IRE_REFHOLD(md_dst_ire); 4918 } 4919 } 4920 4921 if (dst_ire != NULL && 4922 dst_ire->ire_type == IRE_LOCAL && 4923 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { 4924 /* 4925 * If the IRE belongs to a different zone, look for a matching 4926 * route in the forwarding table and use the source address from 4927 * that route. 4928 */ 4929 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 4930 zoneid, 0, NULL, 4931 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4932 MATCH_IRE_RJ_BHOLE, ipst); 4933 if (src_ire == NULL) { 4934 error = EHOSTUNREACH; 4935 goto bad_addr; 4936 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 4937 if (!(src_ire->ire_type & IRE_HOST)) 4938 error = ENETUNREACH; 4939 else 4940 error = EHOSTUNREACH; 4941 goto bad_addr; 4942 } 4943 if (src_addr == INADDR_ANY) 4944 src_addr = src_ire->ire_src_addr; 4945 ire_refrele(src_ire); 4946 src_ire = NULL; 4947 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 4948 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 4949 src_addr = sire->ire_src_addr; 4950 ire_refrele(dst_ire); 4951 dst_ire = sire; 4952 sire = NULL; 4953 } else { 4954 /* 4955 * Pick a source address so that a proper inbound 4956 * load spreading would happen. 4957 */ 4958 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; 4959 ipif_t *src_ipif = NULL; 4960 ire_t *ipif_ire; 4961 4962 /* 4963 * Supply a local source address such that inbound 4964 * load spreading happens. 4965 * 4966 * Determine the best source address on this ill for 4967 * the destination. 4968 * 4969 * 1) For broadcast, we should return a broadcast ire 4970 * found above so that upper layers know that the 4971 * destination address is a broadcast address. 4972 * 4973 * 2) If this is part of a group, select a better 4974 * source address so that better inbound load 4975 * balancing happens. Do the same if the ipif 4976 * is DEPRECATED. 4977 * 4978 * 3) If the outgoing interface is part of a usesrc 4979 * group, then try selecting a source address from 4980 * the usesrc ILL. 4981 */ 4982 if ((dst_ire->ire_zoneid != zoneid && 4983 dst_ire->ire_zoneid != ALL_ZONES) || 4984 (!(dst_ire->ire_type & IRE_BROADCAST) && 4985 ((dst_ill->ill_group != NULL) || 4986 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 4987 (dst_ill->ill_usesrc_ifindex != 0)))) { 4988 /* 4989 * If the destination is reachable via a 4990 * given gateway, the selected source address 4991 * should be in the same subnet as the gateway. 4992 * Otherwise, the destination is not reachable. 4993 * 4994 * If there are no interfaces on the same subnet 4995 * as the destination, ipif_select_source gives 4996 * first non-deprecated interface which might be 4997 * on a different subnet than the gateway. 4998 * This is not desirable. Hence pass the dst_ire 4999 * source address to ipif_select_source. 5000 * It is sure that the destination is reachable 5001 * with the dst_ire source address subnet. 5002 * So passing dst_ire source address to 5003 * ipif_select_source will make sure that the 5004 * selected source will be on the same subnet 5005 * as dst_ire source address. 5006 */ 5007 ipaddr_t saddr = 5008 dst_ire->ire_ipif->ipif_src_addr; 5009 src_ipif = ipif_select_source(dst_ill, 5010 saddr, zoneid); 5011 if (src_ipif != NULL) { 5012 if (IS_VNI(src_ipif->ipif_ill)) { 5013 /* 5014 * For VNI there is no 5015 * interface route 5016 */ 5017 src_addr = 5018 src_ipif->ipif_src_addr; 5019 } else { 5020 ipif_ire = 5021 ipif_to_ire(src_ipif); 5022 if (ipif_ire != NULL) { 5023 IRE_REFRELE(dst_ire); 5024 dst_ire = ipif_ire; 5025 } 5026 src_addr = 5027 dst_ire->ire_src_addr; 5028 } 5029 ipif_refrele(src_ipif); 5030 } else { 5031 src_addr = dst_ire->ire_src_addr; 5032 } 5033 } else { 5034 src_addr = dst_ire->ire_src_addr; 5035 } 5036 } 5037 } 5038 5039 /* 5040 * We do ire_route_lookup() here (and not 5041 * interface lookup as we assert that 5042 * src_addr should only come from an 5043 * UP interface for hard binding. 5044 */ 5045 ASSERT(src_ire == NULL); 5046 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 5047 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 5048 /* src_ire must be a local|loopback */ 5049 if (!IRE_IS_LOCAL(src_ire)) { 5050 if (ip_debug > 2) { 5051 pr_addr_dbg("ip_bind_connected: bad connected " 5052 "src %s\n", AF_INET, &src_addr); 5053 } 5054 error = EADDRNOTAVAIL; 5055 goto bad_addr; 5056 } 5057 5058 /* 5059 * If the source address is a loopback address, the 5060 * destination had best be local or multicast. 5061 * The transports that can't handle multicast will reject 5062 * those addresses. 5063 */ 5064 if (src_ire->ire_type == IRE_LOOPBACK && 5065 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 5066 ip1dbg(("ip_bind_connected: bad connected loopback\n")); 5067 error = -1; 5068 goto bad_addr; 5069 } 5070 5071 /* 5072 * Allow setting new policies. For example, disconnects come 5073 * down as ipa_t bind. As we would have set conn_policy_cached 5074 * to B_TRUE before, we should set it to B_FALSE, so that policy 5075 * can change after the disconnect. 5076 */ 5077 connp->conn_policy_cached = B_FALSE; 5078 5079 /* 5080 * Set the conn addresses/ports immediately, so the IPsec policy calls 5081 * can handle their passed-in conn's. 5082 */ 5083 5084 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 5085 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 5086 connp->conn_lport = lport; 5087 connp->conn_fport = fport; 5088 *src_addrp = src_addr; 5089 5090 ASSERT(!(ipsec_policy_set && ire_requested)); 5091 if (ire_requested) { 5092 iulp_t *ulp_info = NULL; 5093 5094 /* 5095 * Note that sire will not be NULL if this is an off-link 5096 * connection and there is not cache for that dest yet. 5097 * 5098 * XXX Because of an existing bug, if there are multiple 5099 * default routes, the IRE returned now may not be the actual 5100 * default route used (default routes are chosen in a 5101 * round robin fashion). So if the metrics for different 5102 * default routes are different, we may return the wrong 5103 * metrics. This will not be a problem if the existing 5104 * bug is fixed. 5105 */ 5106 if (sire != NULL) { 5107 ulp_info = &(sire->ire_uinfo); 5108 } 5109 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info, ipst)) { 5110 error = -1; 5111 goto bad_addr; 5112 } 5113 } else if (ipsec_policy_set) { 5114 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 5115 error = -1; 5116 goto bad_addr; 5117 } 5118 } 5119 5120 /* 5121 * Cache IPsec policy in this conn. If we have per-socket policy, 5122 * we'll cache that. If we don't, we'll inherit global policy. 5123 * 5124 * We can't insert until the conn reflects the policy. Note that 5125 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 5126 * connections where we don't have a policy. This is to prevent 5127 * global policy lookups in the inbound path. 5128 * 5129 * If we insert before we set conn_policy_cached, 5130 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 5131 * because global policy cound be non-empty. We normally call 5132 * ipsec_check_policy() for conn_policy_cached connections only if 5133 * ipc_in_enforce_policy is set. But in this case, 5134 * conn_policy_cached can get set anytime since we made the 5135 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 5136 * called, which will make the above assumption false. Thus, we 5137 * need to insert after we set conn_policy_cached. 5138 */ 5139 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 5140 goto bad_addr; 5141 5142 if (fanout_insert) { 5143 /* 5144 * The addresses have been verified. Time to insert in 5145 * the correct fanout list. 5146 * We need to make sure that the conn_recv is set to a non-null 5147 * value before we insert into the classifier table to avoid a 5148 * race with an incoming packet which does an ipcl_classify(). 5149 */ 5150 if (protocol == IPPROTO_TCP) 5151 connp->conn_recv = tcp_input; 5152 error = ipcl_conn_insert(connp, protocol, src_addr, 5153 dst_addr, connp->conn_ports); 5154 } 5155 5156 if (error == 0) { 5157 connp->conn_fully_bound = B_TRUE; 5158 /* 5159 * Our initial checks for LSO/MDT have passed; the IRE is not 5160 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 5161 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into 5162 * ip_xxinfo_return(), which performs further checks 5163 * against them and upon success, returns the LSO/MDT info 5164 * mblk which we will attach to the bind acknowledgment. 5165 */ 5166 if (lso_dst_ire != NULL) { 5167 mblk_t *lsoinfo_mp; 5168 5169 ASSERT(ill->ill_lso_capab != NULL); 5170 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp, 5171 ill->ill_name, ill->ill_lso_capab)) != NULL) 5172 linkb(mp, lsoinfo_mp); 5173 } else if (md_dst_ire != NULL) { 5174 mblk_t *mdinfo_mp; 5175 5176 ASSERT(ill->ill_mdt_capab != NULL); 5177 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 5178 ill->ill_name, ill->ill_mdt_capab)) != NULL) 5179 linkb(mp, mdinfo_mp); 5180 } 5181 } 5182 bad_addr: 5183 if (ipsec_policy_set) { 5184 ASSERT(policy_mp == mp->b_cont); 5185 ASSERT(policy_mp != NULL); 5186 freeb(policy_mp); 5187 /* 5188 * As of now assume that nothing else accompanies 5189 * IPSEC_POLICY_SET. 5190 */ 5191 mp->b_cont = NULL; 5192 } 5193 if (src_ire != NULL) 5194 IRE_REFRELE(src_ire); 5195 if (dst_ire != NULL) 5196 IRE_REFRELE(dst_ire); 5197 if (sire != NULL) 5198 IRE_REFRELE(sire); 5199 if (md_dst_ire != NULL) 5200 IRE_REFRELE(md_dst_ire); 5201 if (lso_dst_ire != NULL) 5202 IRE_REFRELE(lso_dst_ire); 5203 return (error); 5204 } 5205 5206 /* 5207 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 5208 * Prefers dst_ire over src_ire. 5209 */ 5210 static boolean_t 5211 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst) 5212 { 5213 mblk_t *mp1; 5214 ire_t *ret_ire = NULL; 5215 5216 mp1 = mp->b_cont; 5217 ASSERT(mp1 != NULL); 5218 5219 if (ire != NULL) { 5220 /* 5221 * mp1 initialized above to IRE_DB_REQ_TYPE 5222 * appended mblk. Its <upper protocol>'s 5223 * job to make sure there is room. 5224 */ 5225 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 5226 return (0); 5227 5228 mp1->b_datap->db_type = IRE_DB_TYPE; 5229 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 5230 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 5231 ret_ire = (ire_t *)mp1->b_rptr; 5232 /* 5233 * Pass the latest setting of the ip_path_mtu_discovery and 5234 * copy the ulp info if any. 5235 */ 5236 ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? 5237 IPH_DF : 0; 5238 if (ulp_info != NULL) { 5239 bcopy(ulp_info, &(ret_ire->ire_uinfo), 5240 sizeof (iulp_t)); 5241 } 5242 ret_ire->ire_mp = mp1; 5243 } else { 5244 /* 5245 * No IRE was found. Remove IRE mblk. 5246 */ 5247 mp->b_cont = mp1->b_cont; 5248 freeb(mp1); 5249 } 5250 5251 return (1); 5252 } 5253 5254 /* 5255 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 5256 * the final piece where we don't. Return a pointer to the first mblk in the 5257 * result, and update the pointer to the next mblk to chew on. If anything 5258 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 5259 * NULL pointer. 5260 */ 5261 mblk_t * 5262 ip_carve_mp(mblk_t **mpp, ssize_t len) 5263 { 5264 mblk_t *mp0; 5265 mblk_t *mp1; 5266 mblk_t *mp2; 5267 5268 if (!len || !mpp || !(mp0 = *mpp)) 5269 return (NULL); 5270 /* If we aren't going to consume the first mblk, we need a dup. */ 5271 if (mp0->b_wptr - mp0->b_rptr > len) { 5272 mp1 = dupb(mp0); 5273 if (mp1) { 5274 /* Partition the data between the two mblks. */ 5275 mp1->b_wptr = mp1->b_rptr + len; 5276 mp0->b_rptr = mp1->b_wptr; 5277 /* 5278 * after adjustments if mblk not consumed is now 5279 * unaligned, try to align it. If this fails free 5280 * all messages and let upper layer recover. 5281 */ 5282 if (!OK_32PTR(mp0->b_rptr)) { 5283 if (!pullupmsg(mp0, -1)) { 5284 freemsg(mp0); 5285 freemsg(mp1); 5286 *mpp = NULL; 5287 return (NULL); 5288 } 5289 } 5290 } 5291 return (mp1); 5292 } 5293 /* Eat through as many mblks as we need to get len bytes. */ 5294 len -= mp0->b_wptr - mp0->b_rptr; 5295 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 5296 if (mp2->b_wptr - mp2->b_rptr > len) { 5297 /* 5298 * We won't consume the entire last mblk. Like 5299 * above, dup and partition it. 5300 */ 5301 mp1->b_cont = dupb(mp2); 5302 mp1 = mp1->b_cont; 5303 if (!mp1) { 5304 /* 5305 * Trouble. Rather than go to a lot of 5306 * trouble to clean up, we free the messages. 5307 * This won't be any worse than losing it on 5308 * the wire. 5309 */ 5310 freemsg(mp0); 5311 freemsg(mp2); 5312 *mpp = NULL; 5313 return (NULL); 5314 } 5315 mp1->b_wptr = mp1->b_rptr + len; 5316 mp2->b_rptr = mp1->b_wptr; 5317 /* 5318 * after adjustments if mblk not consumed is now 5319 * unaligned, try to align it. If this fails free 5320 * all messages and let upper layer recover. 5321 */ 5322 if (!OK_32PTR(mp2->b_rptr)) { 5323 if (!pullupmsg(mp2, -1)) { 5324 freemsg(mp0); 5325 freemsg(mp2); 5326 *mpp = NULL; 5327 return (NULL); 5328 } 5329 } 5330 *mpp = mp2; 5331 return (mp0); 5332 } 5333 /* Decrement len by the amount we just got. */ 5334 len -= mp2->b_wptr - mp2->b_rptr; 5335 } 5336 /* 5337 * len should be reduced to zero now. If not our caller has 5338 * screwed up. 5339 */ 5340 if (len) { 5341 /* Shouldn't happen! */ 5342 freemsg(mp0); 5343 *mpp = NULL; 5344 return (NULL); 5345 } 5346 /* 5347 * We consumed up to exactly the end of an mblk. Detach the part 5348 * we are returning from the rest of the chain. 5349 */ 5350 mp1->b_cont = NULL; 5351 *mpp = mp2; 5352 return (mp0); 5353 } 5354 5355 /* The ill stream is being unplumbed. Called from ip_close */ 5356 int 5357 ip_modclose(ill_t *ill) 5358 { 5359 5360 boolean_t success; 5361 ipsq_t *ipsq; 5362 ipif_t *ipif; 5363 queue_t *q = ill->ill_rq; 5364 hook_nic_event_t *info; 5365 ip_stack_t *ipst = ill->ill_ipst; 5366 clock_t timeout; 5367 5368 /* 5369 * Wait for the ACKs of all deferred control messages to be processed. 5370 * In particular, we wait for a potential capability reset initiated 5371 * in ip_sioctl_plink() to complete before proceeding. 5372 * 5373 * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms) 5374 * in case the driver never replies. 5375 */ 5376 timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms); 5377 mutex_enter(&ill->ill_lock); 5378 while (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 5379 if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) { 5380 /* Timeout */ 5381 break; 5382 } 5383 } 5384 mutex_exit(&ill->ill_lock); 5385 5386 /* 5387 * Forcibly enter the ipsq after some delay. This is to take 5388 * care of the case when some ioctl does not complete because 5389 * we sent a control message to the driver and it did not 5390 * send us a reply. We want to be able to at least unplumb 5391 * and replumb rather than force the user to reboot the system. 5392 */ 5393 success = ipsq_enter(ill, B_FALSE); 5394 5395 /* 5396 * Open/close/push/pop is guaranteed to be single threaded 5397 * per stream by STREAMS. FS guarantees that all references 5398 * from top are gone before close is called. So there can't 5399 * be another close thread that has set CONDEMNED on this ill. 5400 * and cause ipsq_enter to return failure. 5401 */ 5402 ASSERT(success); 5403 ipsq = ill->ill_phyint->phyint_ipsq; 5404 5405 /* 5406 * Mark it condemned. No new reference will be made to this ill. 5407 * Lookup functions will return an error. Threads that try to 5408 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 5409 * that the refcnt will drop down to zero. 5410 */ 5411 mutex_enter(&ill->ill_lock); 5412 ill->ill_state_flags |= ILL_CONDEMNED; 5413 for (ipif = ill->ill_ipif; ipif != NULL; 5414 ipif = ipif->ipif_next) { 5415 ipif->ipif_state_flags |= IPIF_CONDEMNED; 5416 } 5417 /* 5418 * Wake up anybody waiting to enter the ipsq. ipsq_enter 5419 * returns error if ILL_CONDEMNED is set 5420 */ 5421 cv_broadcast(&ill->ill_cv); 5422 mutex_exit(&ill->ill_lock); 5423 5424 /* 5425 * Send all the deferred control messages downstream which came in 5426 * during the small window right before ipsq_enter(). We do this 5427 * without waiting for the ACKs because all the ACKs for M_PROTO 5428 * messages are ignored in ip_rput() when ILL_CONDEMNED is set. 5429 */ 5430 ill_send_all_deferred_mp(ill); 5431 5432 /* 5433 * Shut down fragmentation reassembly. 5434 * ill_frag_timer won't start a timer again. 5435 * Now cancel any existing timer 5436 */ 5437 (void) untimeout(ill->ill_frag_timer_id); 5438 (void) ill_frag_timeout(ill, 0); 5439 5440 /* 5441 * If MOVE was in progress, clear the 5442 * move_in_progress fields also. 5443 */ 5444 if (ill->ill_move_in_progress) { 5445 ILL_CLEAR_MOVE(ill); 5446 } 5447 5448 /* 5449 * Call ill_delete to bring down the ipifs, ilms and ill on 5450 * this ill. Then wait for the refcnts to drop to zero. 5451 * ill_is_quiescent checks whether the ill is really quiescent. 5452 * Then make sure that threads that are waiting to enter the 5453 * ipsq have seen the error returned by ipsq_enter and have 5454 * gone away. Then we call ill_delete_tail which does the 5455 * DL_UNBIND and DL_DETACH with the driver and then qprocsoff. 5456 */ 5457 ill_delete(ill); 5458 mutex_enter(&ill->ill_lock); 5459 while (!ill_is_quiescent(ill)) 5460 cv_wait(&ill->ill_cv, &ill->ill_lock); 5461 while (ill->ill_waiters) 5462 cv_wait(&ill->ill_cv, &ill->ill_lock); 5463 5464 mutex_exit(&ill->ill_lock); 5465 5466 /* 5467 * ill_delete_tail drops reference on ill_ipst, but we need to keep 5468 * it held until the end of the function since the cleanup 5469 * below needs to be able to use the ip_stack_t. 5470 */ 5471 netstack_hold(ipst->ips_netstack); 5472 5473 /* qprocsoff is called in ill_delete_tail */ 5474 ill_delete_tail(ill); 5475 ASSERT(ill->ill_ipst == NULL); 5476 5477 /* 5478 * Walk through all upper (conn) streams and qenable 5479 * those that have queued data. 5480 * close synchronization needs this to 5481 * be done to ensure that all upper layers blocked 5482 * due to flow control to the closing device 5483 * get unblocked. 5484 */ 5485 ip1dbg(("ip_wsrv: walking\n")); 5486 conn_walk_drain(ipst); 5487 5488 mutex_enter(&ipst->ips_ip_mi_lock); 5489 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill); 5490 mutex_exit(&ipst->ips_ip_mi_lock); 5491 5492 /* 5493 * credp could be null if the open didn't succeed and ip_modopen 5494 * itself calls ip_close. 5495 */ 5496 if (ill->ill_credp != NULL) 5497 crfree(ill->ill_credp); 5498 5499 /* 5500 * Unhook the nic event message from the ill and enqueue it into the nic 5501 * event taskq. 5502 */ 5503 if ((info = ill->ill_nic_event_info) != NULL) { 5504 if (ddi_taskq_dispatch(eventq_queue_nic, 5505 ip_ne_queue_func, 5506 (void *)info, DDI_SLEEP) == DDI_FAILURE) { 5507 ip2dbg(("ip_ioctl_finish:ddi_taskq_dispatch failed\n")); 5508 if (info->hne_data != NULL) 5509 kmem_free(info->hne_data, info->hne_datalen); 5510 kmem_free(info, sizeof (hook_nic_event_t)); 5511 } 5512 ill->ill_nic_event_info = NULL; 5513 } 5514 5515 /* 5516 * Now we are done with the module close pieces that 5517 * need the netstack_t. 5518 */ 5519 netstack_rele(ipst->ips_netstack); 5520 5521 mi_close_free((IDP)ill); 5522 q->q_ptr = WR(q)->q_ptr = NULL; 5523 5524 ipsq_exit(ipsq, B_TRUE, B_TRUE); 5525 5526 return (0); 5527 } 5528 5529 /* 5530 * This is called as part of close() for both IP and UDP 5531 * in order to quiesce the conn. 5532 */ 5533 void 5534 ip_quiesce_conn(conn_t *connp) 5535 { 5536 boolean_t drain_cleanup_reqd = B_FALSE; 5537 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 5538 boolean_t ilg_cleanup_reqd = B_FALSE; 5539 ip_stack_t *ipst; 5540 5541 ASSERT(!IPCL_IS_TCP(connp)); 5542 ipst = connp->conn_netstack->netstack_ip; 5543 5544 /* 5545 * Mark the conn as closing, and this conn must not be 5546 * inserted in future into any list. Eg. conn_drain_insert(), 5547 * won't insert this conn into the conn_drain_list. 5548 * Similarly ill_pending_mp_add() will not add any mp to 5549 * the pending mp list, after this conn has started closing. 5550 * 5551 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 5552 * cannot get set henceforth. 5553 */ 5554 mutex_enter(&connp->conn_lock); 5555 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 5556 connp->conn_state_flags |= CONN_CLOSING; 5557 if (connp->conn_idl != NULL) 5558 drain_cleanup_reqd = B_TRUE; 5559 if (connp->conn_oper_pending_ill != NULL) 5560 conn_ioctl_cleanup_reqd = B_TRUE; 5561 if (connp->conn_ilg_inuse != 0) 5562 ilg_cleanup_reqd = B_TRUE; 5563 mutex_exit(&connp->conn_lock); 5564 5565 if (IPCL_IS_UDP(connp)) 5566 udp_quiesce_conn(connp); 5567 5568 if (conn_ioctl_cleanup_reqd) 5569 conn_ioctl_cleanup(connp); 5570 5571 if (is_system_labeled() && connp->conn_anon_port) { 5572 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 5573 connp->conn_mlp_type, connp->conn_ulp, 5574 ntohs(connp->conn_lport), B_FALSE); 5575 connp->conn_anon_port = 0; 5576 } 5577 connp->conn_mlp_type = mlptSingle; 5578 5579 /* 5580 * Remove this conn from any fanout list it is on. 5581 * and then wait for any threads currently operating 5582 * on this endpoint to finish 5583 */ 5584 ipcl_hash_remove(connp); 5585 5586 /* 5587 * Remove this conn from the drain list, and do 5588 * any other cleanup that may be required. 5589 * (Only non-tcp streams may have a non-null conn_idl. 5590 * TCP streams are never flow controlled, and 5591 * conn_idl will be null) 5592 */ 5593 if (drain_cleanup_reqd) 5594 conn_drain_tail(connp, B_TRUE); 5595 5596 if (connp->conn_rq == ipst->ips_ip_g_mrouter || 5597 connp->conn_wq == ipst->ips_ip_g_mrouter) 5598 (void) ip_mrouter_done(NULL, ipst); 5599 5600 if (ilg_cleanup_reqd) 5601 ilg_delete_all(connp); 5602 5603 conn_delete_ire(connp, NULL); 5604 5605 /* 5606 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 5607 * callers from write side can't be there now because close 5608 * is in progress. The only other caller is ipcl_walk 5609 * which checks for the condemned flag. 5610 */ 5611 mutex_enter(&connp->conn_lock); 5612 connp->conn_state_flags |= CONN_CONDEMNED; 5613 while (connp->conn_ref != 1) 5614 cv_wait(&connp->conn_cv, &connp->conn_lock); 5615 connp->conn_state_flags |= CONN_QUIESCED; 5616 mutex_exit(&connp->conn_lock); 5617 } 5618 5619 /* ARGSUSED */ 5620 int 5621 ip_close(queue_t *q, int flags) 5622 { 5623 conn_t *connp; 5624 5625 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 5626 5627 /* 5628 * Call the appropriate delete routine depending on whether this is 5629 * a module or device. 5630 */ 5631 if (WR(q)->q_next != NULL) { 5632 /* This is a module close */ 5633 return (ip_modclose((ill_t *)q->q_ptr)); 5634 } 5635 5636 connp = q->q_ptr; 5637 ip_quiesce_conn(connp); 5638 5639 qprocsoff(q); 5640 5641 /* 5642 * Now we are truly single threaded on this stream, and can 5643 * delete the things hanging off the connp, and finally the connp. 5644 * We removed this connp from the fanout list, it cannot be 5645 * accessed thru the fanouts, and we already waited for the 5646 * conn_ref to drop to 0. We are already in close, so 5647 * there cannot be any other thread from the top. qprocsoff 5648 * has completed, and service has completed or won't run in 5649 * future. 5650 */ 5651 ASSERT(connp->conn_ref == 1); 5652 5653 /* 5654 * A conn which was previously marked as IPCL_UDP cannot 5655 * retain the flag because it would have been cleared by 5656 * udp_close(). 5657 */ 5658 ASSERT(!IPCL_IS_UDP(connp)); 5659 5660 if (connp->conn_latch != NULL) { 5661 IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack); 5662 connp->conn_latch = NULL; 5663 } 5664 if (connp->conn_policy != NULL) { 5665 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 5666 connp->conn_policy = NULL; 5667 } 5668 if (connp->conn_ipsec_opt_mp != NULL) { 5669 freemsg(connp->conn_ipsec_opt_mp); 5670 connp->conn_ipsec_opt_mp = NULL; 5671 } 5672 5673 inet_minor_free(ip_minor_arena, connp->conn_dev); 5674 5675 connp->conn_ref--; 5676 ipcl_conn_destroy(connp); 5677 5678 q->q_ptr = WR(q)->q_ptr = NULL; 5679 return (0); 5680 } 5681 5682 int 5683 ip_snmpmod_close(queue_t *q) 5684 { 5685 conn_t *connp = Q_TO_CONN(q); 5686 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5687 5688 qprocsoff(q); 5689 5690 if (connp->conn_flags & IPCL_UDPMOD) 5691 udp_close_free(connp); 5692 5693 if (connp->conn_cred != NULL) { 5694 crfree(connp->conn_cred); 5695 connp->conn_cred = NULL; 5696 } 5697 CONN_DEC_REF(connp); 5698 q->q_ptr = WR(q)->q_ptr = NULL; 5699 return (0); 5700 } 5701 5702 /* 5703 * Write side put procedure for TCP module or UDP module instance. TCP/UDP 5704 * as a module is only used for MIB browsers that push TCP/UDP over IP or ARP. 5705 * The only supported primitives are T_SVR4_OPTMGMT_REQ and T_OPTMGMT_REQ. 5706 * M_FLUSH messages and ioctls are only passed downstream; we don't flush our 5707 * queues as we never enqueue messages there and we don't handle any ioctls. 5708 * Everything else is freed. 5709 */ 5710 void 5711 ip_snmpmod_wput(queue_t *q, mblk_t *mp) 5712 { 5713 conn_t *connp = q->q_ptr; 5714 pfi_t setfn; 5715 pfi_t getfn; 5716 5717 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5718 5719 switch (DB_TYPE(mp)) { 5720 case M_PROTO: 5721 case M_PCPROTO: 5722 if ((MBLKL(mp) >= sizeof (t_scalar_t)) && 5723 ((((union T_primitives *)mp->b_rptr)->type == 5724 T_SVR4_OPTMGMT_REQ) || 5725 (((union T_primitives *)mp->b_rptr)->type == 5726 T_OPTMGMT_REQ))) { 5727 /* 5728 * This is the only TPI primitive supported. Its 5729 * handling does not require tcp_t, but it does require 5730 * conn_t to check permissions. 5731 */ 5732 cred_t *cr = DB_CREDDEF(mp, connp->conn_cred); 5733 5734 if (connp->conn_flags & IPCL_TCPMOD) { 5735 setfn = tcp_snmp_set; 5736 getfn = tcp_snmp_get; 5737 } else { 5738 setfn = udp_snmp_set; 5739 getfn = udp_snmp_get; 5740 } 5741 if (!snmpcom_req(q, mp, setfn, getfn, cr)) { 5742 freemsg(mp); 5743 return; 5744 } 5745 } else if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, ENOTSUP)) 5746 != NULL) 5747 qreply(q, mp); 5748 break; 5749 case M_FLUSH: 5750 case M_IOCTL: 5751 putnext(q, mp); 5752 break; 5753 default: 5754 freemsg(mp); 5755 break; 5756 } 5757 } 5758 5759 /* Return the IP checksum for the IP header at "iph". */ 5760 uint16_t 5761 ip_csum_hdr(ipha_t *ipha) 5762 { 5763 uint16_t *uph; 5764 uint32_t sum; 5765 int opt_len; 5766 5767 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - 5768 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 5769 uph = (uint16_t *)ipha; 5770 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 5771 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 5772 if (opt_len > 0) { 5773 do { 5774 sum += uph[10]; 5775 sum += uph[11]; 5776 uph += 2; 5777 } while (--opt_len); 5778 } 5779 sum = (sum & 0xFFFF) + (sum >> 16); 5780 sum = ~(sum + (sum >> 16)) & 0xFFFF; 5781 if (sum == 0xffff) 5782 sum = 0; 5783 return ((uint16_t)sum); 5784 } 5785 5786 /* 5787 * Called when the module is about to be unloaded 5788 */ 5789 void 5790 ip_ddi_destroy(void) 5791 { 5792 tnet_fini(); 5793 5794 sctp_ddi_g_destroy(); 5795 tcp_ddi_g_destroy(); 5796 ipsec_policy_g_destroy(); 5797 ipcl_g_destroy(); 5798 ip_net_g_destroy(); 5799 ip_ire_g_fini(); 5800 inet_minor_destroy(ip_minor_arena); 5801 5802 netstack_unregister(NS_IP); 5803 } 5804 5805 /* 5806 * First step in cleanup. 5807 */ 5808 /* ARGSUSED */ 5809 static void 5810 ip_stack_shutdown(netstackid_t stackid, void *arg) 5811 { 5812 ip_stack_t *ipst = (ip_stack_t *)arg; 5813 5814 #ifdef NS_DEBUG 5815 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid); 5816 #endif 5817 5818 /* Get rid of loopback interfaces and their IREs */ 5819 ip_loopback_cleanup(ipst); 5820 } 5821 5822 /* 5823 * Free the IP stack instance. 5824 */ 5825 static void 5826 ip_stack_fini(netstackid_t stackid, void *arg) 5827 { 5828 ip_stack_t *ipst = (ip_stack_t *)arg; 5829 int ret; 5830 5831 #ifdef NS_DEBUG 5832 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid); 5833 #endif 5834 ipv4_hook_destroy(ipst); 5835 ipv6_hook_destroy(ipst); 5836 ip_net_destroy(ipst); 5837 5838 rw_destroy(&ipst->ips_srcid_lock); 5839 5840 ip_kstat_fini(stackid, ipst->ips_ip_mibkp); 5841 ipst->ips_ip_mibkp = NULL; 5842 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp); 5843 ipst->ips_icmp_mibkp = NULL; 5844 ip_kstat2_fini(stackid, ipst->ips_ip_kstat); 5845 ipst->ips_ip_kstat = NULL; 5846 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics)); 5847 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat); 5848 ipst->ips_ip6_kstat = NULL; 5849 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics)); 5850 5851 nd_free(&ipst->ips_ip_g_nd); 5852 kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr)); 5853 ipst->ips_param_arr = NULL; 5854 kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 5855 ipst->ips_ndp_arr = NULL; 5856 5857 ip_mrouter_stack_destroy(ipst); 5858 5859 mutex_destroy(&ipst->ips_ip_mi_lock); 5860 rw_destroy(&ipst->ips_ipsec_capab_ills_lock); 5861 rw_destroy(&ipst->ips_ill_g_usesrc_lock); 5862 rw_destroy(&ipst->ips_ip_g_nd_lock); 5863 5864 ret = untimeout(ipst->ips_igmp_timeout_id); 5865 if (ret == -1) { 5866 ASSERT(ipst->ips_igmp_timeout_id == 0); 5867 } else { 5868 ASSERT(ipst->ips_igmp_timeout_id != 0); 5869 ipst->ips_igmp_timeout_id = 0; 5870 } 5871 ret = untimeout(ipst->ips_igmp_slowtimeout_id); 5872 if (ret == -1) { 5873 ASSERT(ipst->ips_igmp_slowtimeout_id == 0); 5874 } else { 5875 ASSERT(ipst->ips_igmp_slowtimeout_id != 0); 5876 ipst->ips_igmp_slowtimeout_id = 0; 5877 } 5878 ret = untimeout(ipst->ips_mld_timeout_id); 5879 if (ret == -1) { 5880 ASSERT(ipst->ips_mld_timeout_id == 0); 5881 } else { 5882 ASSERT(ipst->ips_mld_timeout_id != 0); 5883 ipst->ips_mld_timeout_id = 0; 5884 } 5885 ret = untimeout(ipst->ips_mld_slowtimeout_id); 5886 if (ret == -1) { 5887 ASSERT(ipst->ips_mld_slowtimeout_id == 0); 5888 } else { 5889 ASSERT(ipst->ips_mld_slowtimeout_id != 0); 5890 ipst->ips_mld_slowtimeout_id = 0; 5891 } 5892 ret = untimeout(ipst->ips_ip_ire_expire_id); 5893 if (ret == -1) { 5894 ASSERT(ipst->ips_ip_ire_expire_id == 0); 5895 } else { 5896 ASSERT(ipst->ips_ip_ire_expire_id != 0); 5897 ipst->ips_ip_ire_expire_id = 0; 5898 } 5899 5900 mutex_destroy(&ipst->ips_igmp_timer_lock); 5901 mutex_destroy(&ipst->ips_mld_timer_lock); 5902 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock); 5903 mutex_destroy(&ipst->ips_mld_slowtimeout_lock); 5904 mutex_destroy(&ipst->ips_ip_addr_avail_lock); 5905 rw_destroy(&ipst->ips_ill_g_lock); 5906 5907 ip_ire_fini(ipst); 5908 ip6_asp_free(ipst); 5909 conn_drain_fini(ipst); 5910 ipcl_destroy(ipst); 5911 5912 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock); 5913 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock); 5914 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t)); 5915 ipst->ips_ndp4 = NULL; 5916 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t)); 5917 ipst->ips_ndp6 = NULL; 5918 5919 if (ipst->ips_loopback_ksp != NULL) { 5920 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid); 5921 ipst->ips_loopback_ksp = NULL; 5922 } 5923 5924 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t)); 5925 ipst->ips_phyint_g_list = NULL; 5926 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS); 5927 ipst->ips_ill_g_heads = NULL; 5928 5929 kmem_free(ipst, sizeof (*ipst)); 5930 } 5931 5932 /* 5933 * Called when the IP kernel module is loaded into the kernel 5934 */ 5935 void 5936 ip_ddi_init(void) 5937 { 5938 TCP6_MAJ = ddi_name_to_major(TCP6); 5939 TCP_MAJ = ddi_name_to_major(TCP); 5940 SCTP_MAJ = ddi_name_to_major(SCTP); 5941 SCTP6_MAJ = ddi_name_to_major(SCTP6); 5942 5943 ip_input_proc = ip_squeue_switch(ip_squeue_enter); 5944 5945 /* 5946 * For IP and TCP the minor numbers should start from 2 since we have 4 5947 * initial devices: ip, ip6, tcp, tcp6. 5948 */ 5949 if ((ip_minor_arena = inet_minor_create("ip_minor_arena", 5950 INET_MIN_DEV + 2, KM_SLEEP)) == NULL) { 5951 cmn_err(CE_PANIC, 5952 "ip_ddi_init: ip_minor_arena creation failed\n"); 5953 } 5954 5955 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5956 5957 ipcl_g_init(); 5958 ip_ire_g_init(); 5959 ip_net_g_init(); 5960 5961 /* 5962 * We want to be informed each time a stack is created or 5963 * destroyed in the kernel, so we can maintain the 5964 * set of udp_stack_t's. 5965 */ 5966 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown, 5967 ip_stack_fini); 5968 5969 ipsec_policy_g_init(); 5970 tcp_ddi_g_init(); 5971 sctp_ddi_g_init(); 5972 5973 tnet_init(); 5974 } 5975 5976 /* 5977 * Initialize the IP stack instance. 5978 */ 5979 static void * 5980 ip_stack_init(netstackid_t stackid, netstack_t *ns) 5981 { 5982 ip_stack_t *ipst; 5983 ipparam_t *pa; 5984 ipndp_t *na; 5985 5986 #ifdef NS_DEBUG 5987 printf("ip_stack_init(stack %d)\n", stackid); 5988 #endif 5989 5990 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP); 5991 ipst->ips_netstack = ns; 5992 5993 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS, 5994 KM_SLEEP); 5995 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t), 5996 KM_SLEEP); 5997 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5998 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5999 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 6000 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 6001 6002 rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 6003 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6004 ipst->ips_igmp_deferred_next = INFINITY; 6005 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6006 ipst->ips_mld_deferred_next = INFINITY; 6007 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6008 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6009 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 6010 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 6011 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL); 6012 rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 6013 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 6014 6015 ipcl_init(ipst); 6016 ip_ire_init(ipst); 6017 ip6_asp_init(ipst); 6018 ipif_init(ipst); 6019 conn_drain_init(ipst); 6020 ip_mrouter_stack_init(ipst); 6021 6022 ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT; 6023 ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 6024 6025 ipst->ips_ip_multirt_log_interval = 1000; 6026 6027 ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT; 6028 ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT; 6029 ipst->ips_ill_index = 1; 6030 6031 ipst->ips_saved_ip_g_forward = -1; 6032 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */ 6033 6034 pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 6035 ipst->ips_param_arr = pa; 6036 bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr)); 6037 6038 na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP); 6039 ipst->ips_ndp_arr = na; 6040 bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 6041 ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data = 6042 (caddr_t)&ipst->ips_ip_g_forward; 6043 ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data = 6044 (caddr_t)&ipst->ips_ipv6_forward; 6045 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name, 6046 "ip_cgtp_filter") == 0); 6047 ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data = 6048 (caddr_t)&ip_cgtp_filter; 6049 6050 (void) ip_param_register(&ipst->ips_ip_g_nd, 6051 ipst->ips_param_arr, A_CNT(lcl_param_arr), 6052 ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr)); 6053 6054 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst); 6055 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid); 6056 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics); 6057 ipst->ips_ip6_kstat = 6058 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics); 6059 6060 ipst->ips_ipmp_enable_failback = B_TRUE; 6061 6062 ipst->ips_ip_src_id = 1; 6063 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL); 6064 6065 ip_net_init(ipst, ns); 6066 ipv4_hook_init(ipst); 6067 ipv6_hook_init(ipst); 6068 6069 return (ipst); 6070 } 6071 6072 /* 6073 * Allocate and initialize a DLPI template of the specified length. (May be 6074 * called as writer.) 6075 */ 6076 mblk_t * 6077 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 6078 { 6079 mblk_t *mp; 6080 6081 mp = allocb(len, BPRI_MED); 6082 if (!mp) 6083 return (NULL); 6084 6085 /* 6086 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 6087 * of which we don't seem to use) are sent with M_PCPROTO, and 6088 * that other DLPI are M_PROTO. 6089 */ 6090 if (prim == DL_INFO_REQ) { 6091 mp->b_datap->db_type = M_PCPROTO; 6092 } else { 6093 mp->b_datap->db_type = M_PROTO; 6094 } 6095 6096 mp->b_wptr = mp->b_rptr + len; 6097 bzero(mp->b_rptr, len); 6098 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 6099 return (mp); 6100 } 6101 6102 const char * 6103 dlpi_prim_str(int prim) 6104 { 6105 switch (prim) { 6106 case DL_INFO_REQ: return ("DL_INFO_REQ"); 6107 case DL_INFO_ACK: return ("DL_INFO_ACK"); 6108 case DL_ATTACH_REQ: return ("DL_ATTACH_REQ"); 6109 case DL_DETACH_REQ: return ("DL_DETACH_REQ"); 6110 case DL_BIND_REQ: return ("DL_BIND_REQ"); 6111 case DL_BIND_ACK: return ("DL_BIND_ACK"); 6112 case DL_UNBIND_REQ: return ("DL_UNBIND_REQ"); 6113 case DL_OK_ACK: return ("DL_OK_ACK"); 6114 case DL_ERROR_ACK: return ("DL_ERROR_ACK"); 6115 case DL_ENABMULTI_REQ: return ("DL_ENABMULTI_REQ"); 6116 case DL_DISABMULTI_REQ: return ("DL_DISABMULTI_REQ"); 6117 case DL_PROMISCON_REQ: return ("DL_PROMISCON_REQ"); 6118 case DL_PROMISCOFF_REQ: return ("DL_PROMISCOFF_REQ"); 6119 case DL_UNITDATA_REQ: return ("DL_UNITDATA_REQ"); 6120 case DL_UNITDATA_IND: return ("DL_UNITDATA_IND"); 6121 case DL_UDERROR_IND: return ("DL_UDERROR_IND"); 6122 case DL_PHYS_ADDR_REQ: return ("DL_PHYS_ADDR_REQ"); 6123 case DL_PHYS_ADDR_ACK: return ("DL_PHYS_ADDR_ACK"); 6124 case DL_SET_PHYS_ADDR_REQ: return ("DL_SET_PHYS_ADDR_REQ"); 6125 case DL_NOTIFY_REQ: return ("DL_NOTIFY_REQ"); 6126 case DL_NOTIFY_ACK: return ("DL_NOTIFY_ACK"); 6127 case DL_NOTIFY_IND: return ("DL_NOTIFY_IND"); 6128 case DL_CAPABILITY_REQ: return ("DL_CAPABILITY_REQ"); 6129 case DL_CAPABILITY_ACK: return ("DL_CAPABILITY_ACK"); 6130 case DL_CONTROL_REQ: return ("DL_CONTROL_REQ"); 6131 case DL_CONTROL_ACK: return ("DL_CONTROL_ACK"); 6132 default: return ("<unknown primitive>"); 6133 } 6134 } 6135 6136 const char * 6137 dlpi_err_str(int err) 6138 { 6139 switch (err) { 6140 case DL_ACCESS: return ("DL_ACCESS"); 6141 case DL_BADADDR: return ("DL_BADADDR"); 6142 case DL_BADCORR: return ("DL_BADCORR"); 6143 case DL_BADDATA: return ("DL_BADDATA"); 6144 case DL_BADPPA: return ("DL_BADPPA"); 6145 case DL_BADPRIM: return ("DL_BADPRIM"); 6146 case DL_BADQOSPARAM: return ("DL_BADQOSPARAM"); 6147 case DL_BADQOSTYPE: return ("DL_BADQOSTYPE"); 6148 case DL_BADSAP: return ("DL_BADSAP"); 6149 case DL_BADTOKEN: return ("DL_BADTOKEN"); 6150 case DL_BOUND: return ("DL_BOUND"); 6151 case DL_INITFAILED: return ("DL_INITFAILED"); 6152 case DL_NOADDR: return ("DL_NOADDR"); 6153 case DL_NOTINIT: return ("DL_NOTINIT"); 6154 case DL_OUTSTATE: return ("DL_OUTSTATE"); 6155 case DL_SYSERR: return ("DL_SYSERR"); 6156 case DL_UNSUPPORTED: return ("DL_UNSUPPORTED"); 6157 case DL_UNDELIVERABLE: return ("DL_UNDELIVERABLE"); 6158 case DL_NOTSUPPORTED : return ("DL_NOTSUPPORTED "); 6159 case DL_TOOMANY: return ("DL_TOOMANY"); 6160 case DL_NOTENAB: return ("DL_NOTENAB"); 6161 case DL_BUSY: return ("DL_BUSY"); 6162 case DL_NOAUTO: return ("DL_NOAUTO"); 6163 case DL_NOXIDAUTO: return ("DL_NOXIDAUTO"); 6164 case DL_NOTESTAUTO: return ("DL_NOTESTAUTO"); 6165 case DL_XIDAUTO: return ("DL_XIDAUTO"); 6166 case DL_TESTAUTO: return ("DL_TESTAUTO"); 6167 case DL_PENDING: return ("DL_PENDING"); 6168 default: return ("<unknown error>"); 6169 } 6170 } 6171 6172 /* 6173 * Debug formatting routine. Returns a character string representation of the 6174 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 6175 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 6176 * 6177 * Once the ndd table-printing interfaces are removed, this can be changed to 6178 * standard dotted-decimal form. 6179 */ 6180 char * 6181 ip_dot_addr(ipaddr_t addr, char *buf) 6182 { 6183 uint8_t *ap = (uint8_t *)&addr; 6184 6185 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 6186 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 6187 return (buf); 6188 } 6189 6190 /* 6191 * Write the given MAC address as a printable string in the usual colon- 6192 * separated format. 6193 */ 6194 const char * 6195 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 6196 { 6197 char *bp; 6198 6199 if (alen == 0 || buflen < 4) 6200 return ("?"); 6201 bp = buf; 6202 for (;;) { 6203 /* 6204 * If there are more MAC address bytes available, but we won't 6205 * have any room to print them, then add "..." to the string 6206 * instead. See below for the 'magic number' explanation. 6207 */ 6208 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 6209 (void) strcpy(bp, "..."); 6210 break; 6211 } 6212 (void) sprintf(bp, "%02x", *addr++); 6213 bp += 2; 6214 if (--alen == 0) 6215 break; 6216 *bp++ = ':'; 6217 buflen -= 3; 6218 /* 6219 * At this point, based on the first 'if' statement above, 6220 * either alen == 1 and buflen >= 3, or alen > 1 and 6221 * buflen >= 4. The first case leaves room for the final "xx" 6222 * number and trailing NUL byte. The second leaves room for at 6223 * least "...". Thus the apparently 'magic' numbers chosen for 6224 * that statement. 6225 */ 6226 } 6227 return (buf); 6228 } 6229 6230 /* 6231 * Send an ICMP error after patching up the packet appropriately. Returns 6232 * non-zero if the appropriate MIB should be bumped; zero otherwise. 6233 */ 6234 static boolean_t 6235 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 6236 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, 6237 zoneid_t zoneid, ip_stack_t *ipst) 6238 { 6239 ipha_t *ipha; 6240 mblk_t *first_mp; 6241 boolean_t secure; 6242 unsigned char db_type; 6243 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6244 6245 first_mp = mp; 6246 if (mctl_present) { 6247 mp = mp->b_cont; 6248 secure = ipsec_in_is_secure(first_mp); 6249 ASSERT(mp != NULL); 6250 } else { 6251 /* 6252 * If this is an ICMP error being reported - which goes 6253 * up as M_CTLs, we need to convert them to M_DATA till 6254 * we finish checking with global policy because 6255 * ipsec_check_global_policy() assumes M_DATA as clear 6256 * and M_CTL as secure. 6257 */ 6258 db_type = DB_TYPE(mp); 6259 DB_TYPE(mp) = M_DATA; 6260 secure = B_FALSE; 6261 } 6262 /* 6263 * We are generating an icmp error for some inbound packet. 6264 * Called from all ip_fanout_(udp, tcp, proto) functions. 6265 * Before we generate an error, check with global policy 6266 * to see whether this is allowed to enter the system. As 6267 * there is no "conn", we are checking with global policy. 6268 */ 6269 ipha = (ipha_t *)mp->b_rptr; 6270 if (secure || ipss->ipsec_inbound_v4_policy_present) { 6271 first_mp = ipsec_check_global_policy(first_mp, NULL, 6272 ipha, NULL, mctl_present, ipst->ips_netstack); 6273 if (first_mp == NULL) 6274 return (B_FALSE); 6275 } 6276 6277 if (!mctl_present) 6278 DB_TYPE(mp) = db_type; 6279 6280 if (flags & IP_FF_SEND_ICMP) { 6281 if (flags & IP_FF_HDR_COMPLETE) { 6282 if (ip_hdr_complete(ipha, zoneid, ipst)) { 6283 freemsg(first_mp); 6284 return (B_TRUE); 6285 } 6286 } 6287 if (flags & IP_FF_CKSUM) { 6288 /* 6289 * Have to correct checksum since 6290 * the packet might have been 6291 * fragmented and the reassembly code in ip_rput 6292 * does not restore the IP checksum. 6293 */ 6294 ipha->ipha_hdr_checksum = 0; 6295 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6296 } 6297 switch (icmp_type) { 6298 case ICMP_DEST_UNREACHABLE: 6299 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid, 6300 ipst); 6301 break; 6302 default: 6303 freemsg(first_mp); 6304 break; 6305 } 6306 } else { 6307 freemsg(first_mp); 6308 return (B_FALSE); 6309 } 6310 6311 return (B_TRUE); 6312 } 6313 6314 /* 6315 * Used to send an ICMP error message when a packet is received for 6316 * a protocol that is not supported. The mblk passed as argument 6317 * is consumed by this function. 6318 */ 6319 void 6320 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid, 6321 ip_stack_t *ipst) 6322 { 6323 mblk_t *mp; 6324 ipha_t *ipha; 6325 ill_t *ill; 6326 ipsec_in_t *ii; 6327 6328 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6329 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6330 6331 mp = ipsec_mp->b_cont; 6332 ipsec_mp->b_cont = NULL; 6333 ipha = (ipha_t *)mp->b_rptr; 6334 /* Get ill from index in ipsec_in_t. */ 6335 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 6336 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL, 6337 ipst); 6338 if (ill != NULL) { 6339 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 6340 if (ip_fanout_send_icmp(q, mp, flags, 6341 ICMP_DEST_UNREACHABLE, 6342 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) { 6343 BUMP_MIB(ill->ill_ip_mib, 6344 ipIfStatsInUnknownProtos); 6345 } 6346 } else { 6347 if (ip_fanout_send_icmp_v6(q, mp, flags, 6348 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 6349 0, B_FALSE, zoneid, ipst)) { 6350 BUMP_MIB(ill->ill_ip_mib, 6351 ipIfStatsInUnknownProtos); 6352 } 6353 } 6354 ill_refrele(ill); 6355 } else { /* re-link for the freemsg() below. */ 6356 ipsec_mp->b_cont = mp; 6357 } 6358 6359 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 6360 freemsg(ipsec_mp); 6361 } 6362 6363 /* 6364 * See if the inbound datagram has had IPsec processing applied to it. 6365 */ 6366 boolean_t 6367 ipsec_in_is_secure(mblk_t *ipsec_mp) 6368 { 6369 ipsec_in_t *ii; 6370 6371 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6372 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6373 6374 if (ii->ipsec_in_loopback) { 6375 return (ii->ipsec_in_secure); 6376 } else { 6377 return (ii->ipsec_in_ah_sa != NULL || 6378 ii->ipsec_in_esp_sa != NULL || 6379 ii->ipsec_in_decaps); 6380 } 6381 } 6382 6383 /* 6384 * Handle protocols with which IP is less intimate. There 6385 * can be more than one stream bound to a particular 6386 * protocol. When this is the case, normally each one gets a copy 6387 * of any incoming packets. 6388 * 6389 * IPSEC NOTE : 6390 * 6391 * Don't allow a secure packet going up a non-secure connection. 6392 * We don't allow this because 6393 * 6394 * 1) Reply might go out in clear which will be dropped at 6395 * the sending side. 6396 * 2) If the reply goes out in clear it will give the 6397 * adversary enough information for getting the key in 6398 * most of the cases. 6399 * 6400 * Moreover getting a secure packet when we expect clear 6401 * implies that SA's were added without checking for 6402 * policy on both ends. This should not happen once ISAKMP 6403 * is used to negotiate SAs as SAs will be added only after 6404 * verifying the policy. 6405 * 6406 * NOTE : If the packet was tunneled and not multicast we only send 6407 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 6408 * back to delivering packets to AF_INET6 raw sockets. 6409 * 6410 * IPQoS Notes: 6411 * Once we have determined the client, invoke IPPF processing. 6412 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6413 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6414 * ip_policy will be false. 6415 * 6416 * Zones notes: 6417 * Currently only applications in the global zone can create raw sockets for 6418 * protocols other than ICMP. So unlike the broadcast / multicast case of 6419 * ip_fanout_udp(), we only send a copy of the packet to streams in the 6420 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 6421 */ 6422 static void 6423 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 6424 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 6425 zoneid_t zoneid) 6426 { 6427 queue_t *rq; 6428 mblk_t *mp1, *first_mp1; 6429 uint_t protocol = ipha->ipha_protocol; 6430 ipaddr_t dst; 6431 boolean_t one_only; 6432 mblk_t *first_mp = mp; 6433 boolean_t secure; 6434 uint32_t ill_index; 6435 conn_t *connp, *first_connp, *next_connp; 6436 connf_t *connfp; 6437 boolean_t shared_addr; 6438 mib2_ipIfStatsEntry_t *mibptr; 6439 ip_stack_t *ipst = recv_ill->ill_ipst; 6440 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6441 6442 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 6443 if (mctl_present) { 6444 mp = first_mp->b_cont; 6445 secure = ipsec_in_is_secure(first_mp); 6446 ASSERT(mp != NULL); 6447 } else { 6448 secure = B_FALSE; 6449 } 6450 dst = ipha->ipha_dst; 6451 /* 6452 * If the packet was tunneled and not multicast we only send to it 6453 * the first match. 6454 */ 6455 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 6456 !CLASSD(dst)); 6457 6458 shared_addr = (zoneid == ALL_ZONES); 6459 if (shared_addr) { 6460 /* 6461 * We don't allow multilevel ports for raw IP, so no need to 6462 * check for that here. 6463 */ 6464 zoneid = tsol_packet_to_zoneid(mp); 6465 } 6466 6467 connfp = &ipst->ips_ipcl_proto_fanout[protocol]; 6468 mutex_enter(&connfp->connf_lock); 6469 connp = connfp->connf_head; 6470 for (connp = connfp->connf_head; connp != NULL; 6471 connp = connp->conn_next) { 6472 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, 6473 zoneid) && 6474 (!is_system_labeled() || 6475 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6476 connp))) 6477 break; 6478 } 6479 6480 if (connp == NULL || connp->conn_upq == NULL) { 6481 /* 6482 * No one bound to these addresses. Is 6483 * there a client that wants all 6484 * unclaimed datagrams? 6485 */ 6486 mutex_exit(&connfp->connf_lock); 6487 /* 6488 * Check for IPPROTO_ENCAP... 6489 */ 6490 if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) { 6491 /* 6492 * If an IPsec mblk is here on a multicast 6493 * tunnel (using ip_mroute stuff), check policy here, 6494 * THEN ship off to ip_mroute_decap(). 6495 * 6496 * BTW, If I match a configured IP-in-IP 6497 * tunnel, this path will not be reached, and 6498 * ip_mroute_decap will never be called. 6499 */ 6500 first_mp = ipsec_check_global_policy(first_mp, connp, 6501 ipha, NULL, mctl_present, ipst->ips_netstack); 6502 if (first_mp != NULL) { 6503 if (mctl_present) 6504 freeb(first_mp); 6505 ip_mroute_decap(q, mp, ill); 6506 } /* Else we already freed everything! */ 6507 } else { 6508 /* 6509 * Otherwise send an ICMP protocol unreachable. 6510 */ 6511 if (ip_fanout_send_icmp(q, first_mp, flags, 6512 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 6513 mctl_present, zoneid, ipst)) { 6514 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos); 6515 } 6516 } 6517 return; 6518 } 6519 CONN_INC_REF(connp); 6520 first_connp = connp; 6521 6522 /* 6523 * Only send message to one tunnel driver by immediately 6524 * terminating the loop. 6525 */ 6526 connp = one_only ? NULL : connp->conn_next; 6527 6528 for (;;) { 6529 while (connp != NULL) { 6530 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 6531 flags, zoneid) && 6532 (!is_system_labeled() || 6533 tsol_receive_local(mp, &dst, IPV4_VERSION, 6534 shared_addr, connp))) 6535 break; 6536 connp = connp->conn_next; 6537 } 6538 6539 /* 6540 * Copy the packet. 6541 */ 6542 if (connp == NULL || connp->conn_upq == NULL || 6543 (((first_mp1 = dupmsg(first_mp)) == NULL) && 6544 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 6545 /* 6546 * No more interested clients or memory 6547 * allocation failed 6548 */ 6549 connp = first_connp; 6550 break; 6551 } 6552 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 6553 CONN_INC_REF(connp); 6554 mutex_exit(&connfp->connf_lock); 6555 rq = connp->conn_rq; 6556 if (!canputnext(rq)) { 6557 if (flags & IP_FF_RAWIP) { 6558 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6559 } else { 6560 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6561 } 6562 6563 freemsg(first_mp1); 6564 } else { 6565 /* 6566 * Don't enforce here if we're an actual tunnel - 6567 * let "tun" do it instead. 6568 */ 6569 if (!IPCL_IS_IPTUN(connp) && 6570 (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || 6571 secure)) { 6572 first_mp1 = ipsec_check_inbound_policy 6573 (first_mp1, connp, ipha, NULL, 6574 mctl_present); 6575 } 6576 if (first_mp1 != NULL) { 6577 int in_flags = 0; 6578 /* 6579 * ip_fanout_proto also gets called from 6580 * icmp_inbound_error_fanout, in which case 6581 * the msg type is M_CTL. Don't add info 6582 * in this case for the time being. In future 6583 * when there is a need for knowing the 6584 * inbound iface index for ICMP error msgs, 6585 * then this can be changed. 6586 */ 6587 if (connp->conn_recvif) 6588 in_flags = IPF_RECVIF; 6589 /* 6590 * The ULP may support IP_RECVPKTINFO for both 6591 * IP v4 and v6 so pass the appropriate argument 6592 * based on conn IP version. 6593 */ 6594 if (connp->conn_ip_recvpktinfo) { 6595 if (connp->conn_af_isv6) { 6596 /* 6597 * V6 only needs index 6598 */ 6599 in_flags |= IPF_RECVIF; 6600 } else { 6601 /* 6602 * V4 needs index + 6603 * matching address. 6604 */ 6605 in_flags |= IPF_RECVADDR; 6606 } 6607 } 6608 if ((in_flags != 0) && 6609 (mp->b_datap->db_type != M_CTL)) { 6610 /* 6611 * the actual data will be 6612 * contained in b_cont upon 6613 * successful return of the 6614 * following call else 6615 * original mblk is returned 6616 */ 6617 ASSERT(recv_ill != NULL); 6618 mp1 = ip_add_info(mp1, recv_ill, 6619 in_flags, IPCL_ZONEID(connp), ipst); 6620 } 6621 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6622 if (mctl_present) 6623 freeb(first_mp1); 6624 putnext(rq, mp1); 6625 } 6626 } 6627 mutex_enter(&connfp->connf_lock); 6628 /* Follow the next pointer before releasing the conn. */ 6629 next_connp = connp->conn_next; 6630 CONN_DEC_REF(connp); 6631 connp = next_connp; 6632 } 6633 6634 /* Last one. Send it upstream. */ 6635 mutex_exit(&connfp->connf_lock); 6636 6637 /* 6638 * If this packet is coming from icmp_inbound_error_fanout ip_policy 6639 * will be set to false. 6640 */ 6641 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6642 ill_index = ill->ill_phyint->phyint_ifindex; 6643 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6644 if (mp == NULL) { 6645 CONN_DEC_REF(connp); 6646 if (mctl_present) { 6647 freeb(first_mp); 6648 } 6649 return; 6650 } 6651 } 6652 6653 rq = connp->conn_rq; 6654 if (!canputnext(rq)) { 6655 if (flags & IP_FF_RAWIP) { 6656 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6657 } else { 6658 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6659 } 6660 6661 freemsg(first_mp); 6662 } else { 6663 if (IPCL_IS_IPTUN(connp)) { 6664 /* 6665 * Tunneled packet. We enforce policy in the tunnel 6666 * module itself. 6667 * 6668 * Send the WHOLE packet up (incl. IPSEC_IN) without 6669 * a policy check. 6670 */ 6671 putnext(rq, first_mp); 6672 CONN_DEC_REF(connp); 6673 return; 6674 } 6675 6676 if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) { 6677 first_mp = ipsec_check_inbound_policy(first_mp, connp, 6678 ipha, NULL, mctl_present); 6679 } 6680 6681 if (first_mp != NULL) { 6682 int in_flags = 0; 6683 6684 /* 6685 * ip_fanout_proto also gets called 6686 * from icmp_inbound_error_fanout, in 6687 * which case the msg type is M_CTL. 6688 * Don't add info in this case for time 6689 * being. In future when there is a 6690 * need for knowing the inbound iface 6691 * index for ICMP error msgs, then this 6692 * can be changed 6693 */ 6694 if (connp->conn_recvif) 6695 in_flags = IPF_RECVIF; 6696 if (connp->conn_ip_recvpktinfo) { 6697 if (connp->conn_af_isv6) { 6698 /* 6699 * V6 only needs index 6700 */ 6701 in_flags |= IPF_RECVIF; 6702 } else { 6703 /* 6704 * V4 needs index + 6705 * matching address. 6706 */ 6707 in_flags |= IPF_RECVADDR; 6708 } 6709 } 6710 if ((in_flags != 0) && 6711 (mp->b_datap->db_type != M_CTL)) { 6712 6713 /* 6714 * the actual data will be contained in 6715 * b_cont upon successful return 6716 * of the following call else original 6717 * mblk is returned 6718 */ 6719 ASSERT(recv_ill != NULL); 6720 mp = ip_add_info(mp, recv_ill, 6721 in_flags, IPCL_ZONEID(connp), ipst); 6722 } 6723 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6724 putnext(rq, mp); 6725 if (mctl_present) 6726 freeb(first_mp); 6727 } 6728 } 6729 CONN_DEC_REF(connp); 6730 } 6731 6732 /* 6733 * Fanout for TCP packets 6734 * The caller puts <fport, lport> in the ports parameter. 6735 * 6736 * IPQoS Notes 6737 * Before sending it to the client, invoke IPPF processing. 6738 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6739 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6740 * ip_policy is false. 6741 */ 6742 static void 6743 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 6744 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 6745 { 6746 mblk_t *first_mp; 6747 boolean_t secure; 6748 uint32_t ill_index; 6749 int ip_hdr_len; 6750 tcph_t *tcph; 6751 boolean_t syn_present = B_FALSE; 6752 conn_t *connp; 6753 ip_stack_t *ipst = recv_ill->ill_ipst; 6754 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6755 6756 ASSERT(recv_ill != NULL); 6757 6758 first_mp = mp; 6759 if (mctl_present) { 6760 ASSERT(first_mp->b_datap->db_type == M_CTL); 6761 mp = first_mp->b_cont; 6762 secure = ipsec_in_is_secure(first_mp); 6763 ASSERT(mp != NULL); 6764 } else { 6765 secure = B_FALSE; 6766 } 6767 6768 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 6769 6770 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 6771 zoneid, ipst)) == NULL) { 6772 /* 6773 * No connected connection or listener. Send a 6774 * TH_RST via tcp_xmit_listeners_reset. 6775 */ 6776 6777 /* Initiate IPPf processing, if needed. */ 6778 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 6779 uint32_t ill_index; 6780 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6781 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 6782 if (first_mp == NULL) 6783 return; 6784 } 6785 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6786 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", 6787 zoneid)); 6788 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6789 ipst->ips_netstack->netstack_tcp); 6790 return; 6791 } 6792 6793 /* 6794 * Allocate the SYN for the TCP connection here itself 6795 */ 6796 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 6797 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 6798 if (IPCL_IS_TCP(connp)) { 6799 squeue_t *sqp; 6800 6801 /* 6802 * For fused tcp loopback, assign the eager's 6803 * squeue to be that of the active connect's. 6804 * Note that we don't check for IP_FF_LOOPBACK 6805 * here since this routine gets called only 6806 * for loopback (unlike the IPv6 counterpart). 6807 */ 6808 ASSERT(Q_TO_CONN(q) != NULL); 6809 if (do_tcp_fusion && 6810 !CONN_INBOUND_POLICY_PRESENT(connp, ipss) && 6811 !secure && 6812 !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy && 6813 IPCL_IS_TCP(Q_TO_CONN(q))) { 6814 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 6815 sqp = Q_TO_CONN(q)->conn_sqp; 6816 } else { 6817 sqp = IP_SQUEUE_GET(lbolt); 6818 } 6819 6820 mp->b_datap->db_struioflag |= STRUIO_EAGER; 6821 DB_CKSUMSTART(mp) = (intptr_t)sqp; 6822 syn_present = B_TRUE; 6823 } 6824 } 6825 6826 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 6827 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 6828 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6829 if ((flags & TH_RST) || (flags & TH_URG)) { 6830 CONN_DEC_REF(connp); 6831 freemsg(first_mp); 6832 return; 6833 } 6834 if (flags & TH_ACK) { 6835 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6836 ipst->ips_netstack->netstack_tcp); 6837 CONN_DEC_REF(connp); 6838 return; 6839 } 6840 6841 CONN_DEC_REF(connp); 6842 freemsg(first_mp); 6843 return; 6844 } 6845 6846 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6847 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6848 NULL, mctl_present); 6849 if (first_mp == NULL) { 6850 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6851 CONN_DEC_REF(connp); 6852 return; 6853 } 6854 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 6855 ASSERT(syn_present); 6856 if (mctl_present) { 6857 ASSERT(first_mp != mp); 6858 first_mp->b_datap->db_struioflag |= 6859 STRUIO_POLICY; 6860 } else { 6861 ASSERT(first_mp == mp); 6862 mp->b_datap->db_struioflag &= 6863 ~STRUIO_EAGER; 6864 mp->b_datap->db_struioflag |= 6865 STRUIO_POLICY; 6866 } 6867 } else { 6868 /* 6869 * Discard first_mp early since we're dealing with a 6870 * fully-connected conn_t and tcp doesn't do policy in 6871 * this case. 6872 */ 6873 if (mctl_present) { 6874 freeb(first_mp); 6875 mctl_present = B_FALSE; 6876 } 6877 first_mp = mp; 6878 } 6879 } 6880 6881 /* 6882 * Initiate policy processing here if needed. If we get here from 6883 * icmp_inbound_error_fanout, ip_policy is false. 6884 */ 6885 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6886 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6887 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6888 if (mp == NULL) { 6889 CONN_DEC_REF(connp); 6890 if (mctl_present) 6891 freeb(first_mp); 6892 return; 6893 } else if (mctl_present) { 6894 ASSERT(first_mp != mp); 6895 first_mp->b_cont = mp; 6896 } else { 6897 first_mp = mp; 6898 } 6899 } 6900 6901 6902 6903 /* Handle socket options. */ 6904 if (!syn_present && 6905 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6906 /* Add header */ 6907 ASSERT(recv_ill != NULL); 6908 /* 6909 * Since tcp does not support IP_RECVPKTINFO for V4, only pass 6910 * IPF_RECVIF. 6911 */ 6912 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp), 6913 ipst); 6914 if (mp == NULL) { 6915 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6916 CONN_DEC_REF(connp); 6917 if (mctl_present) 6918 freeb(first_mp); 6919 return; 6920 } else if (mctl_present) { 6921 /* 6922 * ip_add_info might return a new mp. 6923 */ 6924 ASSERT(first_mp != mp); 6925 first_mp->b_cont = mp; 6926 } else { 6927 first_mp = mp; 6928 } 6929 } 6930 6931 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6932 if (IPCL_IS_TCP(connp)) { 6933 (*ip_input_proc)(connp->conn_sqp, first_mp, 6934 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); 6935 } else { 6936 putnext(connp->conn_rq, first_mp); 6937 CONN_DEC_REF(connp); 6938 } 6939 } 6940 6941 /* 6942 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 6943 * We are responsible for disposing of mp, such as by freemsg() or putnext() 6944 * Caller is responsible for dropping references to the conn, and freeing 6945 * first_mp. 6946 * 6947 * IPQoS Notes 6948 * Before sending it to the client, invoke IPPF processing. Policy processing 6949 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 6950 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 6951 * ip_wput_local, ip_policy is false. 6952 */ 6953 static void 6954 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 6955 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 6956 boolean_t ip_policy) 6957 { 6958 boolean_t mctl_present = (first_mp != NULL); 6959 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 6960 uint32_t ill_index; 6961 ip_stack_t *ipst = recv_ill->ill_ipst; 6962 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6963 6964 ASSERT(ill != NULL); 6965 6966 if (mctl_present) 6967 first_mp->b_cont = mp; 6968 else 6969 first_mp = mp; 6970 6971 if (CONN_UDP_FLOWCTLD(connp)) { 6972 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 6973 freemsg(first_mp); 6974 return; 6975 } 6976 6977 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6978 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6979 NULL, mctl_present); 6980 if (first_mp == NULL) { 6981 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 6982 return; /* Freed by ipsec_check_inbound_policy(). */ 6983 } 6984 } 6985 if (mctl_present) 6986 freeb(first_mp); 6987 6988 /* Handle options. */ 6989 if (connp->conn_recvif) 6990 in_flags = IPF_RECVIF; 6991 /* 6992 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag 6993 * passed to ip_add_info is based on IP version of connp. 6994 */ 6995 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6996 if (connp->conn_af_isv6) { 6997 /* 6998 * V6 only needs index 6999 */ 7000 in_flags |= IPF_RECVIF; 7001 } else { 7002 /* 7003 * V4 needs index + matching address. 7004 */ 7005 in_flags |= IPF_RECVADDR; 7006 } 7007 } 7008 7009 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 7010 in_flags |= IPF_RECVSLLA; 7011 7012 /* 7013 * Initiate IPPF processing here, if needed. Note first_mp won't be 7014 * freed if the packet is dropped. The caller will do so. 7015 */ 7016 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 7017 ill_index = recv_ill->ill_phyint->phyint_ifindex; 7018 ip_process(IPP_LOCAL_IN, &mp, ill_index); 7019 if (mp == NULL) { 7020 return; 7021 } 7022 } 7023 if ((in_flags != 0) && 7024 (mp->b_datap->db_type != M_CTL)) { 7025 /* 7026 * The actual data will be contained in b_cont 7027 * upon successful return of the following call 7028 * else original mblk is returned 7029 */ 7030 ASSERT(recv_ill != NULL); 7031 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp), 7032 ipst); 7033 } 7034 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 7035 /* Send it upstream */ 7036 CONN_UDP_RECV(connp, mp); 7037 } 7038 7039 /* 7040 * Fanout for UDP packets. 7041 * The caller puts <fport, lport> in the ports parameter. 7042 * 7043 * If SO_REUSEADDR is set all multicast and broadcast packets 7044 * will be delivered to all streams bound to the same port. 7045 * 7046 * Zones notes: 7047 * Multicast and broadcast packets will be distributed to streams in all zones. 7048 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 7049 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 7050 * packets. To maintain this behavior with multiple zones, the conns are grouped 7051 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 7052 * each zone. If unset, all the following conns in the same zone are skipped. 7053 */ 7054 static void 7055 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 7056 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 7057 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 7058 { 7059 uint32_t dstport, srcport; 7060 ipaddr_t dst; 7061 mblk_t *first_mp; 7062 boolean_t secure; 7063 in6_addr_t v6src; 7064 conn_t *connp; 7065 connf_t *connfp; 7066 conn_t *first_connp; 7067 conn_t *next_connp; 7068 mblk_t *mp1, *first_mp1; 7069 ipaddr_t src; 7070 zoneid_t last_zoneid; 7071 boolean_t reuseaddr; 7072 boolean_t shared_addr; 7073 ip_stack_t *ipst; 7074 7075 ASSERT(recv_ill != NULL); 7076 ipst = recv_ill->ill_ipst; 7077 7078 first_mp = mp; 7079 if (mctl_present) { 7080 mp = first_mp->b_cont; 7081 first_mp->b_cont = NULL; 7082 secure = ipsec_in_is_secure(first_mp); 7083 ASSERT(mp != NULL); 7084 } else { 7085 first_mp = NULL; 7086 secure = B_FALSE; 7087 } 7088 7089 /* Extract ports in net byte order */ 7090 dstport = htons(ntohl(ports) & 0xFFFF); 7091 srcport = htons(ntohl(ports) >> 16); 7092 dst = ipha->ipha_dst; 7093 src = ipha->ipha_src; 7094 7095 shared_addr = (zoneid == ALL_ZONES); 7096 if (shared_addr) { 7097 /* 7098 * No need to handle exclusive-stack zones since ALL_ZONES 7099 * only applies to the shared stack. 7100 */ 7101 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 7102 if (zoneid == ALL_ZONES) 7103 zoneid = tsol_packet_to_zoneid(mp); 7104 } 7105 7106 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7107 mutex_enter(&connfp->connf_lock); 7108 connp = connfp->connf_head; 7109 if (!broadcast && !CLASSD(dst)) { 7110 /* 7111 * Not broadcast or multicast. Send to the one (first) 7112 * client we find. No need to check conn_wantpacket() 7113 * since IP_BOUND_IF/conn_incoming_ill does not apply to 7114 * IPv4 unicast packets. 7115 */ 7116 while ((connp != NULL) && 7117 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || 7118 !IPCL_ZONE_MATCH(connp, zoneid))) { 7119 connp = connp->conn_next; 7120 } 7121 7122 if (connp == NULL || connp->conn_upq == NULL) 7123 goto notfound; 7124 7125 if (is_system_labeled() && 7126 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7127 connp)) 7128 goto notfound; 7129 7130 CONN_INC_REF(connp); 7131 mutex_exit(&connfp->connf_lock); 7132 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7133 flags, recv_ill, ip_policy); 7134 IP_STAT(ipst, ip_udp_fannorm); 7135 CONN_DEC_REF(connp); 7136 return; 7137 } 7138 7139 /* 7140 * Broadcast and multicast case 7141 * 7142 * Need to check conn_wantpacket(). 7143 * If SO_REUSEADDR has been set on the first we send the 7144 * packet to all clients that have joined the group and 7145 * match the port. 7146 */ 7147 7148 while (connp != NULL) { 7149 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 7150 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7151 (!is_system_labeled() || 7152 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7153 connp))) 7154 break; 7155 connp = connp->conn_next; 7156 } 7157 7158 if (connp == NULL || connp->conn_upq == NULL) 7159 goto notfound; 7160 7161 first_connp = connp; 7162 /* 7163 * When SO_REUSEADDR is not set, send the packet only to the first 7164 * matching connection in its zone by keeping track of the zoneid. 7165 */ 7166 reuseaddr = first_connp->conn_reuseaddr; 7167 last_zoneid = first_connp->conn_zoneid; 7168 7169 CONN_INC_REF(connp); 7170 connp = connp->conn_next; 7171 for (;;) { 7172 while (connp != NULL) { 7173 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 7174 (reuseaddr || connp->conn_zoneid != last_zoneid) && 7175 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7176 (!is_system_labeled() || 7177 tsol_receive_local(mp, &dst, IPV4_VERSION, 7178 shared_addr, connp))) 7179 break; 7180 connp = connp->conn_next; 7181 } 7182 /* 7183 * Just copy the data part alone. The mctl part is 7184 * needed just for verifying policy and it is never 7185 * sent up. 7186 */ 7187 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7188 ((mp1 = copymsg(mp)) == NULL))) { 7189 /* 7190 * No more interested clients or memory 7191 * allocation failed 7192 */ 7193 connp = first_connp; 7194 break; 7195 } 7196 if (connp->conn_zoneid != last_zoneid) { 7197 /* 7198 * Update the zoneid so that the packet isn't sent to 7199 * any more conns in the same zone unless SO_REUSEADDR 7200 * is set. 7201 */ 7202 reuseaddr = connp->conn_reuseaddr; 7203 last_zoneid = connp->conn_zoneid; 7204 } 7205 if (first_mp != NULL) { 7206 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7207 ipsec_info_type == IPSEC_IN); 7208 first_mp1 = ipsec_in_tag(first_mp, NULL, 7209 ipst->ips_netstack); 7210 if (first_mp1 == NULL) { 7211 freemsg(mp1); 7212 connp = first_connp; 7213 break; 7214 } 7215 } else { 7216 first_mp1 = NULL; 7217 } 7218 CONN_INC_REF(connp); 7219 mutex_exit(&connfp->connf_lock); 7220 /* 7221 * IPQoS notes: We don't send the packet for policy 7222 * processing here, will do it for the last one (below). 7223 * i.e. we do it per-packet now, but if we do policy 7224 * processing per-conn, then we would need to do it 7225 * here too. 7226 */ 7227 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7228 ipha, flags, recv_ill, B_FALSE); 7229 mutex_enter(&connfp->connf_lock); 7230 /* Follow the next pointer before releasing the conn. */ 7231 next_connp = connp->conn_next; 7232 IP_STAT(ipst, ip_udp_fanmb); 7233 CONN_DEC_REF(connp); 7234 connp = next_connp; 7235 } 7236 7237 /* Last one. Send it upstream. */ 7238 mutex_exit(&connfp->connf_lock); 7239 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7240 recv_ill, ip_policy); 7241 IP_STAT(ipst, ip_udp_fanmb); 7242 CONN_DEC_REF(connp); 7243 return; 7244 7245 notfound: 7246 7247 mutex_exit(&connfp->connf_lock); 7248 IP_STAT(ipst, ip_udp_fanothers); 7249 /* 7250 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 7251 * have already been matched above, since they live in the IPv4 7252 * fanout tables. This implies we only need to 7253 * check for IPv6 in6addr_any endpoints here. 7254 * Thus we compare using ipv6_all_zeros instead of the destination 7255 * address, except for the multicast group membership lookup which 7256 * uses the IPv4 destination. 7257 */ 7258 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 7259 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7260 mutex_enter(&connfp->connf_lock); 7261 connp = connfp->connf_head; 7262 if (!broadcast && !CLASSD(dst)) { 7263 while (connp != NULL) { 7264 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7265 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && 7266 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7267 !connp->conn_ipv6_v6only) 7268 break; 7269 connp = connp->conn_next; 7270 } 7271 7272 if (connp != NULL && is_system_labeled() && 7273 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7274 connp)) 7275 connp = NULL; 7276 7277 if (connp == NULL || connp->conn_upq == NULL) { 7278 /* 7279 * No one bound to this port. Is 7280 * there a client that wants all 7281 * unclaimed datagrams? 7282 */ 7283 mutex_exit(&connfp->connf_lock); 7284 7285 if (mctl_present) 7286 first_mp->b_cont = mp; 7287 else 7288 first_mp = mp; 7289 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP]. 7290 connf_head != NULL) { 7291 ip_fanout_proto(q, first_mp, ill, ipha, 7292 flags | IP_FF_RAWIP, mctl_present, 7293 ip_policy, recv_ill, zoneid); 7294 } else { 7295 if (ip_fanout_send_icmp(q, first_mp, flags, 7296 ICMP_DEST_UNREACHABLE, 7297 ICMP_PORT_UNREACHABLE, 7298 mctl_present, zoneid, ipst)) { 7299 BUMP_MIB(ill->ill_ip_mib, 7300 udpIfStatsNoPorts); 7301 } 7302 } 7303 return; 7304 } 7305 7306 CONN_INC_REF(connp); 7307 mutex_exit(&connfp->connf_lock); 7308 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7309 flags, recv_ill, ip_policy); 7310 CONN_DEC_REF(connp); 7311 return; 7312 } 7313 /* 7314 * IPv4 multicast packet being delivered to an AF_INET6 7315 * in6addr_any endpoint. 7316 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 7317 * and not conn_wantpacket_v6() since any multicast membership is 7318 * for an IPv4-mapped multicast address. 7319 * The packet is sent to all clients in all zones that have joined the 7320 * group and match the port. 7321 */ 7322 while (connp != NULL) { 7323 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7324 srcport, v6src) && 7325 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7326 (!is_system_labeled() || 7327 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7328 connp))) 7329 break; 7330 connp = connp->conn_next; 7331 } 7332 7333 if (connp == NULL || connp->conn_upq == NULL) { 7334 /* 7335 * No one bound to this port. Is 7336 * there a client that wants all 7337 * unclaimed datagrams? 7338 */ 7339 mutex_exit(&connfp->connf_lock); 7340 7341 if (mctl_present) 7342 first_mp->b_cont = mp; 7343 else 7344 first_mp = mp; 7345 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head != 7346 NULL) { 7347 ip_fanout_proto(q, first_mp, ill, ipha, 7348 flags | IP_FF_RAWIP, mctl_present, ip_policy, 7349 recv_ill, zoneid); 7350 } else { 7351 /* 7352 * We used to attempt to send an icmp error here, but 7353 * since this is known to be a multicast packet 7354 * and we don't send icmp errors in response to 7355 * multicast, just drop the packet and give up sooner. 7356 */ 7357 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 7358 freemsg(first_mp); 7359 } 7360 return; 7361 } 7362 7363 first_connp = connp; 7364 7365 CONN_INC_REF(connp); 7366 connp = connp->conn_next; 7367 for (;;) { 7368 while (connp != NULL) { 7369 if (IPCL_UDP_MATCH_V6(connp, dstport, 7370 ipv6_all_zeros, srcport, v6src) && 7371 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7372 (!is_system_labeled() || 7373 tsol_receive_local(mp, &dst, IPV4_VERSION, 7374 shared_addr, connp))) 7375 break; 7376 connp = connp->conn_next; 7377 } 7378 /* 7379 * Just copy the data part alone. The mctl part is 7380 * needed just for verifying policy and it is never 7381 * sent up. 7382 */ 7383 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7384 ((mp1 = copymsg(mp)) == NULL))) { 7385 /* 7386 * No more intested clients or memory 7387 * allocation failed 7388 */ 7389 connp = first_connp; 7390 break; 7391 } 7392 if (first_mp != NULL) { 7393 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7394 ipsec_info_type == IPSEC_IN); 7395 first_mp1 = ipsec_in_tag(first_mp, NULL, 7396 ipst->ips_netstack); 7397 if (first_mp1 == NULL) { 7398 freemsg(mp1); 7399 connp = first_connp; 7400 break; 7401 } 7402 } else { 7403 first_mp1 = NULL; 7404 } 7405 CONN_INC_REF(connp); 7406 mutex_exit(&connfp->connf_lock); 7407 /* 7408 * IPQoS notes: We don't send the packet for policy 7409 * processing here, will do it for the last one (below). 7410 * i.e. we do it per-packet now, but if we do policy 7411 * processing per-conn, then we would need to do it 7412 * here too. 7413 */ 7414 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7415 ipha, flags, recv_ill, B_FALSE); 7416 mutex_enter(&connfp->connf_lock); 7417 /* Follow the next pointer before releasing the conn. */ 7418 next_connp = connp->conn_next; 7419 CONN_DEC_REF(connp); 7420 connp = next_connp; 7421 } 7422 7423 /* Last one. Send it upstream. */ 7424 mutex_exit(&connfp->connf_lock); 7425 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7426 recv_ill, ip_policy); 7427 CONN_DEC_REF(connp); 7428 } 7429 7430 /* 7431 * Complete the ip_wput header so that it 7432 * is possible to generate ICMP 7433 * errors. 7434 */ 7435 int 7436 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst) 7437 { 7438 ire_t *ire; 7439 7440 if (ipha->ipha_src == INADDR_ANY) { 7441 ire = ire_lookup_local(zoneid, ipst); 7442 if (ire == NULL) { 7443 ip1dbg(("ip_hdr_complete: no source IRE\n")); 7444 return (1); 7445 } 7446 ipha->ipha_src = ire->ire_addr; 7447 ire_refrele(ire); 7448 } 7449 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 7450 ipha->ipha_hdr_checksum = 0; 7451 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 7452 return (0); 7453 } 7454 7455 /* 7456 * Nobody should be sending 7457 * packets up this stream 7458 */ 7459 static void 7460 ip_lrput(queue_t *q, mblk_t *mp) 7461 { 7462 mblk_t *mp1; 7463 7464 switch (mp->b_datap->db_type) { 7465 case M_FLUSH: 7466 /* Turn around */ 7467 if (*mp->b_rptr & FLUSHW) { 7468 *mp->b_rptr &= ~FLUSHR; 7469 qreply(q, mp); 7470 return; 7471 } 7472 break; 7473 } 7474 /* Could receive messages that passed through ar_rput */ 7475 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 7476 mp1->b_prev = mp1->b_next = NULL; 7477 freemsg(mp); 7478 } 7479 7480 /* Nobody should be sending packets down this stream */ 7481 /* ARGSUSED */ 7482 void 7483 ip_lwput(queue_t *q, mblk_t *mp) 7484 { 7485 freemsg(mp); 7486 } 7487 7488 /* 7489 * Move the first hop in any source route to ipha_dst and remove that part of 7490 * the source route. Called by other protocols. Errors in option formatting 7491 * are ignored - will be handled by ip_wput_options Return the final 7492 * destination (either ipha_dst or the last entry in a source route.) 7493 */ 7494 ipaddr_t 7495 ip_massage_options(ipha_t *ipha, netstack_t *ns) 7496 { 7497 ipoptp_t opts; 7498 uchar_t *opt; 7499 uint8_t optval; 7500 uint8_t optlen; 7501 ipaddr_t dst; 7502 int i; 7503 ire_t *ire; 7504 ip_stack_t *ipst = ns->netstack_ip; 7505 7506 ip2dbg(("ip_massage_options\n")); 7507 dst = ipha->ipha_dst; 7508 for (optval = ipoptp_first(&opts, ipha); 7509 optval != IPOPT_EOL; 7510 optval = ipoptp_next(&opts)) { 7511 opt = opts.ipoptp_cur; 7512 switch (optval) { 7513 uint8_t off; 7514 case IPOPT_SSRR: 7515 case IPOPT_LSRR: 7516 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 7517 ip1dbg(("ip_massage_options: bad src route\n")); 7518 break; 7519 } 7520 optlen = opts.ipoptp_len; 7521 off = opt[IPOPT_OFFSET]; 7522 off--; 7523 redo_srr: 7524 if (optlen < IP_ADDR_LEN || 7525 off > optlen - IP_ADDR_LEN) { 7526 /* End of source route */ 7527 ip1dbg(("ip_massage_options: end of SR\n")); 7528 break; 7529 } 7530 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 7531 ip1dbg(("ip_massage_options: next hop 0x%x\n", 7532 ntohl(dst))); 7533 /* 7534 * Check if our address is present more than 7535 * once as consecutive hops in source route. 7536 * XXX verify per-interface ip_forwarding 7537 * for source route? 7538 */ 7539 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 7540 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 7541 if (ire != NULL) { 7542 ire_refrele(ire); 7543 off += IP_ADDR_LEN; 7544 goto redo_srr; 7545 } 7546 if (dst == htonl(INADDR_LOOPBACK)) { 7547 ip1dbg(("ip_massage_options: loopback addr in " 7548 "source route!\n")); 7549 break; 7550 } 7551 /* 7552 * Update ipha_dst to be the first hop and remove the 7553 * first hop from the source route (by overwriting 7554 * part of the option with NOP options). 7555 */ 7556 ipha->ipha_dst = dst; 7557 /* Put the last entry in dst */ 7558 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 7559 3; 7560 bcopy(&opt[off], &dst, IP_ADDR_LEN); 7561 7562 ip1dbg(("ip_massage_options: last hop 0x%x\n", 7563 ntohl(dst))); 7564 /* Move down and overwrite */ 7565 opt[IP_ADDR_LEN] = opt[0]; 7566 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 7567 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 7568 for (i = 0; i < IP_ADDR_LEN; i++) 7569 opt[i] = IPOPT_NOP; 7570 break; 7571 } 7572 } 7573 return (dst); 7574 } 7575 7576 /* 7577 * This function's job is to forward data to the reverse tunnel (FA->HA) 7578 * after doing a few checks. It is assumed that the incoming interface 7579 * of the packet is always different than the outgoing interface and the 7580 * ire_type of the found ire has to be a non-resolver type. 7581 * 7582 * IPQoS notes 7583 * IP policy is invoked twice for a forwarded packet, once on the read side 7584 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 7585 * enabled. 7586 */ 7587 static void 7588 ip_mrtun_forward(ire_t *ire, ill_t *in_ill, mblk_t *mp) 7589 { 7590 ipha_t *ipha; 7591 queue_t *q; 7592 uint32_t pkt_len; 7593 #define rptr ((uchar_t *)ipha) 7594 uint32_t sum; 7595 uint32_t max_frag; 7596 mblk_t *first_mp; 7597 uint32_t ill_index; 7598 ipxmit_state_t pktxmit_state; 7599 ill_t *out_ill; 7600 ip_stack_t *ipst = in_ill->ill_ipst; 7601 7602 ASSERT(ire != NULL); 7603 ASSERT(ire->ire_ipif->ipif_net_type == IRE_IF_NORESOLVER); 7604 ASSERT(ire->ire_stq != NULL); 7605 7606 /* Initiate read side IPPF processing */ 7607 if (IPP_ENABLED(IPP_FWD_IN, ipst)) { 7608 ill_index = in_ill->ill_phyint->phyint_ifindex; 7609 ip_process(IPP_FWD_IN, &mp, ill_index); 7610 if (mp == NULL) { 7611 ip2dbg(("ip_mrtun_forward: inbound pkt " 7612 "dropped during IPPF processing\n")); 7613 return; 7614 } 7615 } 7616 7617 if (((in_ill->ill_flags & ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 7618 ILLF_ROUTER) == 0) || 7619 (in_ill == (ill_t *)ire->ire_stq->q_ptr)) { 7620 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsForwProhibits); 7621 ip0dbg(("ip_mrtun_forward: Can't forward :" 7622 "forwarding is not turned on\n")); 7623 goto drop_pkt; 7624 } 7625 7626 /* 7627 * Don't forward if the interface is down 7628 */ 7629 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 7630 goto discard_pkt; 7631 } 7632 7633 ipha = (ipha_t *)mp->b_rptr; 7634 pkt_len = ntohs(ipha->ipha_length); 7635 /* Adjust the checksum to reflect the ttl decrement. */ 7636 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 7637 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 7638 if (ipha->ipha_ttl-- <= 1) { 7639 if (ip_csum_hdr(ipha)) { 7640 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInCksumErrs); 7641 goto drop_pkt; 7642 } 7643 q = ire->ire_stq; 7644 if ((first_mp = allocb(sizeof (ipsec_info_t), 7645 BPRI_HI)) == NULL) { 7646 goto discard_pkt; 7647 } 7648 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsForwProhibits); 7649 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7650 /* Sent by forwarding path, and router is global zone */ 7651 icmp_time_exceeded(q, first_mp, ICMP_TTL_EXCEEDED, 7652 GLOBAL_ZONEID, ipst); 7653 return; 7654 } 7655 7656 /* Get the ill_index of the ILL */ 7657 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 7658 7659 /* 7660 * This location is chosen for the placement of the forwarding hook 7661 * because at this point we know that we have a path out for the 7662 * packet but haven't yet applied any logic (such as fragmenting) 7663 * that happen as part of transmitting the packet out. 7664 */ 7665 out_ill = ire->ire_ipif->ipif_ill; 7666 7667 DTRACE_PROBE4(ip4__forwarding__start, 7668 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 7669 7670 FW_HOOKS(ipst->ips_ip4_forwarding_event, 7671 ipst->ips_ipv4firewall_forwarding, 7672 in_ill, out_ill, ipha, mp, mp, ipst); 7673 7674 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 7675 7676 if (mp == NULL) 7677 return; 7678 pkt_len = ntohs(ipha->ipha_length); 7679 7680 /* 7681 * ip_mrtun_forward is only used by foreign agent to reverse 7682 * tunnel the incoming packet. So it does not do any option 7683 * processing for source routing. 7684 */ 7685 max_frag = ire->ire_max_frag; 7686 if (pkt_len > max_frag) { 7687 /* 7688 * It needs fragging on its way out. We haven't 7689 * verified the header checksum yet. Since we 7690 * are going to put a surely good checksum in the 7691 * outgoing header, we have to make sure that it 7692 * was good coming in. 7693 */ 7694 if (ip_csum_hdr(ipha)) { 7695 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInCksumErrs); 7696 goto drop_pkt; 7697 } 7698 7699 /* Initiate write side IPPF processing */ 7700 if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { 7701 ip_process(IPP_FWD_OUT, &mp, ill_index); 7702 if (mp == NULL) { 7703 ip2dbg(("ip_mrtun_forward: outbound pkt "\ 7704 "dropped/deferred during ip policy "\ 7705 "processing\n")); 7706 return; 7707 } 7708 } 7709 if ((first_mp = allocb(sizeof (ipsec_info_t), 7710 BPRI_HI)) == NULL) { 7711 goto discard_pkt; 7712 } 7713 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7714 mp = first_mp; 7715 7716 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID, ipst); 7717 return; 7718 } 7719 7720 ip2dbg(("ip_mrtun_forward: ire type (%d)\n", ire->ire_type)); 7721 7722 ASSERT(ire->ire_ipif != NULL); 7723 7724 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 7725 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 7726 FW_HOOKS(ipst->ips_ip4_physical_out_event, 7727 ipst->ips_ipv4firewall_physical_out, 7728 NULL, out_ill, ipha, mp, mp, ipst); 7729 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 7730 if (mp == NULL) 7731 return; 7732 7733 /* Now send the packet to the tunnel interface */ 7734 mp->b_prev = SET_BPREV_FLAG(IPP_FWD_OUT); 7735 q = ire->ire_stq; 7736 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_FALSE); 7737 if ((pktxmit_state == SEND_FAILED) || 7738 (pktxmit_state == LLHDR_RESLV_FAILED)) { 7739 ip2dbg(("ip_mrtun_forward: failed to send packet to ill %p\n", 7740 q->q_ptr)); 7741 } 7742 7743 return; 7744 discard_pkt: 7745 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInDiscards); 7746 drop_pkt:; 7747 ip2dbg(("ip_mrtun_forward: dropping pkt\n")); 7748 freemsg(mp); 7749 #undef rptr 7750 } 7751 7752 /* 7753 * Fills the ipsec_out_t data structure with appropriate fields and 7754 * prepends it to mp which contains the IP hdr + data that was meant 7755 * to be forwarded. Please note that ipsec_out_info data structure 7756 * is used here to communicate the outgoing ill path at ip_wput() 7757 * for the ICMP error packet. This has nothing to do with ipsec IP 7758 * security. ipsec_out_t is really used to pass the info to the module 7759 * IP where this information cannot be extracted from conn. 7760 * This functions is called by ip_mrtun_forward(). 7761 */ 7762 void 7763 ip_ipsec_out_prepend(mblk_t *first_mp, mblk_t *mp, ill_t *xmit_ill) 7764 { 7765 ipsec_out_t *io; 7766 7767 ASSERT(xmit_ill != NULL); 7768 first_mp->b_datap->db_type = M_CTL; 7769 first_mp->b_wptr += sizeof (ipsec_info_t); 7770 /* 7771 * This is to pass info to ip_wput in absence of conn. 7772 * ipsec_out_secure will be B_FALSE because of this. 7773 * Thus ipsec_out_secure being B_FALSE indicates that 7774 * this is not IPSEC security related information. 7775 */ 7776 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 7777 io = (ipsec_out_t *)first_mp->b_rptr; 7778 io->ipsec_out_type = IPSEC_OUT; 7779 io->ipsec_out_len = sizeof (ipsec_out_t); 7780 first_mp->b_cont = mp; 7781 io->ipsec_out_ill_index = 7782 xmit_ill->ill_phyint->phyint_ifindex; 7783 io->ipsec_out_xmit_if = B_TRUE; 7784 io->ipsec_out_ns = xmit_ill->ill_ipst->ips_netstack; 7785 } 7786 7787 /* 7788 * Return the network mask 7789 * associated with the specified address. 7790 */ 7791 ipaddr_t 7792 ip_net_mask(ipaddr_t addr) 7793 { 7794 uchar_t *up = (uchar_t *)&addr; 7795 ipaddr_t mask = 0; 7796 uchar_t *maskp = (uchar_t *)&mask; 7797 7798 #if defined(__i386) || defined(__amd64) 7799 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 7800 #endif 7801 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 7802 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 7803 #endif 7804 if (CLASSD(addr)) { 7805 maskp[0] = 0xF0; 7806 return (mask); 7807 } 7808 if (addr == 0) 7809 return (0); 7810 maskp[0] = 0xFF; 7811 if ((up[0] & 0x80) == 0) 7812 return (mask); 7813 7814 maskp[1] = 0xFF; 7815 if ((up[0] & 0xC0) == 0x80) 7816 return (mask); 7817 7818 maskp[2] = 0xFF; 7819 if ((up[0] & 0xE0) == 0xC0) 7820 return (mask); 7821 7822 /* Must be experimental or multicast, indicate as much */ 7823 return ((ipaddr_t)0); 7824 } 7825 7826 /* 7827 * Select an ill for the packet by considering load spreading across 7828 * a different ill in the group if dst_ill is part of some group. 7829 */ 7830 ill_t * 7831 ip_newroute_get_dst_ill(ill_t *dst_ill) 7832 { 7833 ill_t *ill; 7834 7835 /* 7836 * We schedule irrespective of whether the source address is 7837 * INADDR_ANY or not. illgrp_scheduler returns a held ill. 7838 */ 7839 ill = illgrp_scheduler(dst_ill); 7840 if (ill == NULL) 7841 return (NULL); 7842 7843 /* 7844 * For groups with names ip_sioctl_groupname ensures that all 7845 * ills are of same type. For groups without names, ifgrp_insert 7846 * ensures this. 7847 */ 7848 ASSERT(dst_ill->ill_type == ill->ill_type); 7849 7850 return (ill); 7851 } 7852 7853 /* 7854 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. 7855 */ 7856 ill_t * 7857 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6, 7858 ip_stack_t *ipst) 7859 { 7860 ill_t *ret_ill; 7861 7862 ASSERT(ifindex != 0); 7863 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL, 7864 ipst); 7865 if (ret_ill == NULL || 7866 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { 7867 if (isv6) { 7868 if (ill != NULL) { 7869 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7870 } else { 7871 BUMP_MIB(&ipst->ips_ip6_mib, 7872 ipIfStatsOutDiscards); 7873 } 7874 ip1dbg(("ip_grab_attach_ill (IPv6): " 7875 "bad ifindex %d.\n", ifindex)); 7876 } else { 7877 if (ill != NULL) { 7878 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7879 } else { 7880 BUMP_MIB(&ipst->ips_ip_mib, 7881 ipIfStatsOutDiscards); 7882 } 7883 ip1dbg(("ip_grab_attach_ill (IPv4): " 7884 "bad ifindex %d.\n", ifindex)); 7885 } 7886 if (ret_ill != NULL) 7887 ill_refrele(ret_ill); 7888 freemsg(first_mp); 7889 return (NULL); 7890 } 7891 7892 return (ret_ill); 7893 } 7894 7895 /* 7896 * IPv4 - 7897 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 7898 * out a packet to a destination address for which we do not have specific 7899 * (or sufficient) routing information. 7900 * 7901 * NOTE : These are the scopes of some of the variables that point at IRE, 7902 * which needs to be followed while making any future modifications 7903 * to avoid memory leaks. 7904 * 7905 * - ire and sire are the entries looked up initially by 7906 * ire_ftable_lookup. 7907 * - ipif_ire is used to hold the interface ire associated with 7908 * the new cache ire. But it's scope is limited, so we always REFRELE 7909 * it before branching out to error paths. 7910 * - save_ire is initialized before ire_create, so that ire returned 7911 * by ire_create will not over-write the ire. We REFRELE save_ire 7912 * before breaking out of the switch. 7913 * 7914 * Thus on failures, we have to REFRELE only ire and sire, if they 7915 * are not NULL. 7916 */ 7917 void 7918 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, ill_t *in_ill, conn_t *connp, 7919 zoneid_t zoneid, ip_stack_t *ipst) 7920 { 7921 areq_t *areq; 7922 ipaddr_t gw = 0; 7923 ire_t *ire = NULL; 7924 mblk_t *res_mp; 7925 ipaddr_t *addrp; 7926 ipaddr_t nexthop_addr; 7927 ipif_t *src_ipif = NULL; 7928 ill_t *dst_ill = NULL; 7929 ipha_t *ipha; 7930 ire_t *sire = NULL; 7931 mblk_t *first_mp; 7932 ire_t *save_ire; 7933 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ 7934 ushort_t ire_marks = 0; 7935 boolean_t mctl_present; 7936 ipsec_out_t *io; 7937 mblk_t *saved_mp; 7938 ire_t *first_sire = NULL; 7939 mblk_t *copy_mp = NULL; 7940 mblk_t *xmit_mp = NULL; 7941 ipaddr_t save_dst; 7942 uint32_t multirt_flags = 7943 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 7944 boolean_t multirt_is_resolvable; 7945 boolean_t multirt_resolve_next; 7946 boolean_t do_attach_ill = B_FALSE; 7947 boolean_t ip_nexthop = B_FALSE; 7948 tsol_ire_gw_secattr_t *attrp = NULL; 7949 tsol_gcgrp_t *gcgrp = NULL; 7950 tsol_gcgrp_addr_t ga; 7951 7952 if (ip_debug > 2) { 7953 /* ip1dbg */ 7954 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 7955 } 7956 7957 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7958 if (mctl_present) { 7959 io = (ipsec_out_t *)first_mp->b_rptr; 7960 ASSERT(io->ipsec_out_type == IPSEC_OUT); 7961 ASSERT(zoneid == io->ipsec_out_zoneid); 7962 ASSERT(zoneid != ALL_ZONES); 7963 } 7964 7965 ipha = (ipha_t *)mp->b_rptr; 7966 7967 /* All multicast lookups come through ip_newroute_ipif() */ 7968 if (CLASSD(dst)) { 7969 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 7970 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 7971 freemsg(first_mp); 7972 return; 7973 } 7974 7975 if (mctl_present && io->ipsec_out_attach_if) { 7976 /* ip_grab_attach_ill returns a held ill */ 7977 attach_ill = ip_grab_attach_ill(NULL, first_mp, 7978 io->ipsec_out_ill_index, B_FALSE, ipst); 7979 7980 /* Failure case frees things for us. */ 7981 if (attach_ill == NULL) 7982 return; 7983 7984 /* 7985 * Check if we need an ire that will not be 7986 * looked up by anybody else i.e. HIDDEN. 7987 */ 7988 if (ill_is_probeonly(attach_ill)) 7989 ire_marks = IRE_MARK_HIDDEN; 7990 } 7991 if (mctl_present && io->ipsec_out_ip_nexthop) { 7992 ip_nexthop = B_TRUE; 7993 nexthop_addr = io->ipsec_out_nexthop_addr; 7994 } 7995 /* 7996 * If this IRE is created for forwarding or it is not for 7997 * traffic for congestion controlled protocols, mark it as temporary. 7998 */ 7999 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 8000 ire_marks |= IRE_MARK_TEMPORARY; 8001 8002 /* 8003 * Get what we can from ire_ftable_lookup which will follow an IRE 8004 * chain until it gets the most specific information available. 8005 * For example, we know that there is no IRE_CACHE for this dest, 8006 * but there may be an IRE_OFFSUBNET which specifies a gateway. 8007 * ire_ftable_lookup will look up the gateway, etc. 8008 * Check if in_ill != NULL. If it is true, the packet must be 8009 * from an incoming interface where RTA_SRCIFP is set. 8010 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 8011 * to the destination, of equal netmask length in the forward table, 8012 * will be recursively explored. If no information is available 8013 * for the final gateway of that route, we force the returned ire 8014 * to be equal to sire using MATCH_IRE_PARENT. 8015 * At least, in this case we have a starting point (in the buckets) 8016 * to look for other routes to the destination in the forward table. 8017 * This is actually used only for multirouting, where a list 8018 * of routes has to be processed in sequence. 8019 * 8020 * In the process of coming up with the most specific information, 8021 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry 8022 * for the gateway (i.e., one for which the ire_nce->nce_state is 8023 * not yet ND_REACHABLE, and is in the middle of arp resolution). 8024 * Two caveats when handling incomplete ire's in ip_newroute: 8025 * - we should be careful when accessing its ire_nce (specifically 8026 * the nce_res_mp) ast it might change underneath our feet, and, 8027 * - not all legacy code path callers are prepared to handle 8028 * incomplete ire's, so we should not create/add incomplete 8029 * ire_cache entries here. (See discussion about temporary solution 8030 * further below). 8031 * 8032 * In order to minimize packet dropping, and to preserve existing 8033 * behavior, we treat this case as if there were no IRE_CACHE for the 8034 * gateway, and instead use the IF_RESOLVER ire to send out 8035 * another request to ARP (this is achieved by passing the 8036 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the 8037 * arp response comes back in ip_wput_nondata, we will create 8038 * a per-dst ire_cache that has an ND_COMPLETE ire. 8039 * 8040 * Note that this is a temporary solution; the correct solution is 8041 * to create an incomplete per-dst ire_cache entry, and send the 8042 * packet out when the gw's nce is resolved. In order to achieve this, 8043 * all packet processing must have been completed prior to calling 8044 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need 8045 * to be modified to accomodate this solution. 8046 */ 8047 if (in_ill != NULL) { 8048 ire = ire_srcif_table_lookup(dst, IRE_IF_RESOLVER, NULL, 8049 in_ill, MATCH_IRE_TYPE); 8050 } else if (ip_nexthop) { 8051 /* 8052 * The first time we come here, we look for an IRE_INTERFACE 8053 * entry for the specified nexthop, set the dst to be the 8054 * nexthop address and create an IRE_CACHE entry for the 8055 * nexthop. The next time around, we are able to find an 8056 * IRE_CACHE entry for the nexthop, set the gateway to be the 8057 * nexthop address and create an IRE_CACHE entry for the 8058 * destination address via the specified nexthop. 8059 */ 8060 ire = ire_cache_lookup(nexthop_addr, zoneid, 8061 MBLK_GETLABEL(mp), ipst); 8062 if (ire != NULL) { 8063 gw = nexthop_addr; 8064 ire_marks |= IRE_MARK_PRIVATE_ADDR; 8065 } else { 8066 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 8067 IRE_INTERFACE, NULL, NULL, zoneid, 0, 8068 MBLK_GETLABEL(mp), 8069 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 8070 ipst); 8071 if (ire != NULL) { 8072 dst = nexthop_addr; 8073 } 8074 } 8075 } else if (attach_ill == NULL) { 8076 ire = ire_ftable_lookup(dst, 0, 0, 0, 8077 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), 8078 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 8079 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | 8080 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE, 8081 ipst); 8082 } else { 8083 /* 8084 * attach_ill is set only for communicating with 8085 * on-link hosts. So, don't look for DEFAULT. 8086 */ 8087 ipif_t *attach_ipif; 8088 8089 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 8090 if (attach_ipif == NULL) { 8091 ill_refrele(attach_ill); 8092 goto icmp_err_ret; 8093 } 8094 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, 8095 &sire, zoneid, 0, MBLK_GETLABEL(mp), 8096 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL | 8097 MATCH_IRE_SECATTR, ipst); 8098 ipif_refrele(attach_ipif); 8099 } 8100 ip3dbg(("ip_newroute: ire_ftable_lookup() " 8101 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 8102 8103 /* 8104 * This loop is run only once in most cases. 8105 * We loop to resolve further routes only when the destination 8106 * can be reached through multiple RTF_MULTIRT-flagged ires. 8107 */ 8108 do { 8109 /* Clear the previous iteration's values */ 8110 if (src_ipif != NULL) { 8111 ipif_refrele(src_ipif); 8112 src_ipif = NULL; 8113 } 8114 if (dst_ill != NULL) { 8115 ill_refrele(dst_ill); 8116 dst_ill = NULL; 8117 } 8118 8119 multirt_resolve_next = B_FALSE; 8120 /* 8121 * We check if packets have to be multirouted. 8122 * In this case, given the current <ire, sire> couple, 8123 * we look for the next suitable <ire, sire>. 8124 * This check is done in ire_multirt_lookup(), 8125 * which applies various criteria to find the next route 8126 * to resolve. ire_multirt_lookup() leaves <ire, sire> 8127 * unchanged if it detects it has not been tried yet. 8128 */ 8129 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8130 ip3dbg(("ip_newroute: starting next_resolution " 8131 "with first_mp %p, tag %d\n", 8132 (void *)first_mp, 8133 MULTIRT_DEBUG_TAGGED(first_mp))); 8134 8135 ASSERT(sire != NULL); 8136 multirt_is_resolvable = 8137 ire_multirt_lookup(&ire, &sire, multirt_flags, 8138 MBLK_GETLABEL(mp), ipst); 8139 8140 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 8141 "ire %p, sire %p\n", 8142 multirt_is_resolvable, 8143 (void *)ire, (void *)sire)); 8144 8145 if (!multirt_is_resolvable) { 8146 /* 8147 * No more multirt route to resolve; give up 8148 * (all routes resolved or no more 8149 * resolvable routes). 8150 */ 8151 if (ire != NULL) { 8152 ire_refrele(ire); 8153 ire = NULL; 8154 } 8155 } else { 8156 ASSERT(sire != NULL); 8157 ASSERT(ire != NULL); 8158 /* 8159 * We simply use first_sire as a flag that 8160 * indicates if a resolvable multirt route 8161 * has already been found. 8162 * If it is not the case, we may have to send 8163 * an ICMP error to report that the 8164 * destination is unreachable. 8165 * We do not IRE_REFHOLD first_sire. 8166 */ 8167 if (first_sire == NULL) { 8168 first_sire = sire; 8169 } 8170 } 8171 } 8172 if (ire == NULL) { 8173 if (ip_debug > 3) { 8174 /* ip2dbg */ 8175 pr_addr_dbg("ip_newroute: " 8176 "can't resolve %s\n", AF_INET, &dst); 8177 } 8178 ip3dbg(("ip_newroute: " 8179 "ire %p, sire %p, first_sire %p\n", 8180 (void *)ire, (void *)sire, (void *)first_sire)); 8181 8182 if (sire != NULL) { 8183 ire_refrele(sire); 8184 sire = NULL; 8185 } 8186 8187 if (first_sire != NULL) { 8188 /* 8189 * At least one multirt route has been found 8190 * in the same call to ip_newroute(); 8191 * there is no need to report an ICMP error. 8192 * first_sire was not IRE_REFHOLDed. 8193 */ 8194 MULTIRT_DEBUG_UNTAG(first_mp); 8195 freemsg(first_mp); 8196 return; 8197 } 8198 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 8199 RTA_DST, ipst); 8200 if (attach_ill != NULL) 8201 ill_refrele(attach_ill); 8202 goto icmp_err_ret; 8203 } 8204 8205 /* 8206 * When RTA_SRCIFP is used to add a route, then an interface 8207 * route is added in the source interface's routing table. 8208 * If the outgoing interface of this route is of type 8209 * IRE_IF_RESOLVER, then upon creation of the ire, 8210 * ire_nce->nce_res_mp is set to NULL. 8211 * Later, when this route is first used for forwarding 8212 * a packet, ip_newroute() is called 8213 * to resolve the hardware address of the outgoing ipif. 8214 * We do not come here for IRE_IF_NORESOLVER entries in the 8215 * source interface based table. We only come here if the 8216 * outgoing interface is a resolver interface and we don't 8217 * have the ire_nce->nce_res_mp information yet. 8218 * If in_ill is not null that means it is called from 8219 * ip_rput. 8220 */ 8221 8222 ASSERT(ire->ire_in_ill == NULL || 8223 (ire->ire_type == IRE_IF_RESOLVER && 8224 ire->ire_nce != NULL && ire->ire_nce->nce_res_mp == NULL)); 8225 8226 /* 8227 * Verify that the returned IRE does not have either 8228 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 8229 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 8230 */ 8231 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 8232 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 8233 if (attach_ill != NULL) 8234 ill_refrele(attach_ill); 8235 goto icmp_err_ret; 8236 } 8237 /* 8238 * Increment the ire_ob_pkt_count field for ire if it is an 8239 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 8240 * increment the same for the parent IRE, sire, if it is some 8241 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST 8242 * and HOST_REDIRECT). 8243 */ 8244 if ((ire->ire_type & IRE_INTERFACE) != 0) { 8245 UPDATE_OB_PKT_COUNT(ire); 8246 ire->ire_last_used_time = lbolt; 8247 } 8248 8249 if (sire != NULL) { 8250 gw = sire->ire_gateway_addr; 8251 ASSERT((sire->ire_type & (IRE_CACHETABLE | 8252 IRE_INTERFACE)) == 0); 8253 UPDATE_OB_PKT_COUNT(sire); 8254 sire->ire_last_used_time = lbolt; 8255 } 8256 /* 8257 * We have a route to reach the destination. 8258 * 8259 * 1) If the interface is part of ill group, try to get a new 8260 * ill taking load spreading into account. 8261 * 8262 * 2) After selecting the ill, get a source address that 8263 * might create good inbound load spreading. 8264 * ipif_select_source does this for us. 8265 * 8266 * If the application specified the ill (ifindex), we still 8267 * load spread. Only if the packets needs to go out 8268 * specifically on a given ill e.g. binding to 8269 * IPIF_NOFAILOVER address, then we don't try to use a 8270 * different ill for load spreading. 8271 */ 8272 if (attach_ill == NULL) { 8273 /* 8274 * Don't perform outbound load spreading in the 8275 * case of an RTF_MULTIRT route, as we actually 8276 * typically want to replicate outgoing packets 8277 * through particular interfaces. 8278 */ 8279 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8280 dst_ill = ire->ire_ipif->ipif_ill; 8281 /* for uniformity */ 8282 ill_refhold(dst_ill); 8283 } else { 8284 /* 8285 * If we are here trying to create an IRE_CACHE 8286 * for an offlink destination and have the 8287 * IRE_CACHE for the next hop and the latter is 8288 * using virtual IP source address selection i.e 8289 * it's ire->ire_ipif is pointing to a virtual 8290 * network interface (vni) then 8291 * ip_newroute_get_dst_ll() will return the vni 8292 * interface as the dst_ill. Since the vni is 8293 * virtual i.e not associated with any physical 8294 * interface, it cannot be the dst_ill, hence 8295 * in such a case call ip_newroute_get_dst_ll() 8296 * with the stq_ill instead of the ire_ipif ILL. 8297 * The function returns a refheld ill. 8298 */ 8299 if ((ire->ire_type == IRE_CACHE) && 8300 IS_VNI(ire->ire_ipif->ipif_ill)) 8301 dst_ill = ip_newroute_get_dst_ill( 8302 ire->ire_stq->q_ptr); 8303 else 8304 dst_ill = ip_newroute_get_dst_ill( 8305 ire->ire_ipif->ipif_ill); 8306 } 8307 if (dst_ill == NULL) { 8308 if (ip_debug > 2) { 8309 pr_addr_dbg("ip_newroute: " 8310 "no dst ill for dst" 8311 " %s\n", AF_INET, &dst); 8312 } 8313 goto icmp_err_ret; 8314 } 8315 } else { 8316 dst_ill = ire->ire_ipif->ipif_ill; 8317 /* for uniformity */ 8318 ill_refhold(dst_ill); 8319 /* 8320 * We should have found a route matching ill as we 8321 * called ire_ftable_lookup with MATCH_IRE_ILL. 8322 * Rather than asserting, when there is a mismatch, 8323 * we just drop the packet. 8324 */ 8325 if (dst_ill != attach_ill) { 8326 ip0dbg(("ip_newroute: Packet dropped as " 8327 "IPIF_NOFAILOVER ill is %s, " 8328 "ire->ire_ipif->ipif_ill is %s\n", 8329 attach_ill->ill_name, 8330 dst_ill->ill_name)); 8331 ill_refrele(attach_ill); 8332 goto icmp_err_ret; 8333 } 8334 } 8335 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ 8336 if (attach_ill != NULL) { 8337 ill_refrele(attach_ill); 8338 attach_ill = NULL; 8339 do_attach_ill = B_TRUE; 8340 } 8341 ASSERT(dst_ill != NULL); 8342 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 8343 8344 /* 8345 * Pick the best source address from dst_ill. 8346 * 8347 * 1) If it is part of a multipathing group, we would 8348 * like to spread the inbound packets across different 8349 * interfaces. ipif_select_source picks a random source 8350 * across the different ills in the group. 8351 * 8352 * 2) If it is not part of a multipathing group, we try 8353 * to pick the source address from the destination 8354 * route. Clustering assumes that when we have multiple 8355 * prefixes hosted on an interface, the prefix of the 8356 * source address matches the prefix of the destination 8357 * route. We do this only if the address is not 8358 * DEPRECATED. 8359 * 8360 * 3) If the conn is in a different zone than the ire, we 8361 * need to pick a source address from the right zone. 8362 * 8363 * NOTE : If we hit case (1) above, the prefix of the source 8364 * address picked may not match the prefix of the 8365 * destination routes prefix as ipif_select_source 8366 * does not look at "dst" while picking a source 8367 * address. 8368 * If we want the same behavior as (2), we will need 8369 * to change the behavior of ipif_select_source. 8370 */ 8371 ASSERT(src_ipif == NULL); 8372 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 8373 /* 8374 * The RTF_SETSRC flag is set in the parent ire (sire). 8375 * Check that the ipif matching the requested source 8376 * address still exists. 8377 */ 8378 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 8379 zoneid, NULL, NULL, NULL, NULL, ipst); 8380 } 8381 if (src_ipif == NULL) { 8382 ire_marks |= IRE_MARK_USESRC_CHECK; 8383 if ((dst_ill->ill_group != NULL) || 8384 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 8385 (connp != NULL && ire->ire_zoneid != zoneid && 8386 ire->ire_zoneid != ALL_ZONES) || 8387 (dst_ill->ill_usesrc_ifindex != 0)) { 8388 /* 8389 * If the destination is reachable via a 8390 * given gateway, the selected source address 8391 * should be in the same subnet as the gateway. 8392 * Otherwise, the destination is not reachable. 8393 * 8394 * If there are no interfaces on the same subnet 8395 * as the destination, ipif_select_source gives 8396 * first non-deprecated interface which might be 8397 * on a different subnet than the gateway. 8398 * This is not desirable. Hence pass the dst_ire 8399 * source address to ipif_select_source. 8400 * It is sure that the destination is reachable 8401 * with the dst_ire source address subnet. 8402 * So passing dst_ire source address to 8403 * ipif_select_source will make sure that the 8404 * selected source will be on the same subnet 8405 * as dst_ire source address. 8406 */ 8407 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 8408 src_ipif = ipif_select_source(dst_ill, saddr, 8409 zoneid); 8410 if (src_ipif == NULL) { 8411 if (ip_debug > 2) { 8412 pr_addr_dbg("ip_newroute: " 8413 "no src for dst %s ", 8414 AF_INET, &dst); 8415 printf("through interface %s\n", 8416 dst_ill->ill_name); 8417 } 8418 goto icmp_err_ret; 8419 } 8420 } else { 8421 src_ipif = ire->ire_ipif; 8422 ASSERT(src_ipif != NULL); 8423 /* hold src_ipif for uniformity */ 8424 ipif_refhold(src_ipif); 8425 } 8426 } 8427 8428 /* 8429 * Assign a source address while we have the conn. 8430 * We can't have ip_wput_ire pick a source address when the 8431 * packet returns from arp since we need to look at 8432 * conn_unspec_src and conn_zoneid, and we lose the conn when 8433 * going through arp. 8434 * 8435 * NOTE : ip_newroute_v6 does not have this piece of code as 8436 * it uses ip6i to store this information. 8437 */ 8438 if (ipha->ipha_src == INADDR_ANY && 8439 (connp == NULL || !connp->conn_unspec_src)) { 8440 ipha->ipha_src = src_ipif->ipif_src_addr; 8441 } 8442 if (ip_debug > 3) { 8443 /* ip2dbg */ 8444 pr_addr_dbg("ip_newroute: first hop %s\n", 8445 AF_INET, &gw); 8446 } 8447 ip2dbg(("\tire type %s (%d)\n", 8448 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 8449 8450 /* 8451 * The TTL of multirouted packets is bounded by the 8452 * ip_multirt_ttl ndd variable. 8453 */ 8454 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8455 /* Force TTL of multirouted packets */ 8456 if ((ipst->ips_ip_multirt_ttl > 0) && 8457 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 8458 ip2dbg(("ip_newroute: forcing multirt TTL " 8459 "to %d (was %d), dst 0x%08x\n", 8460 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 8461 ntohl(sire->ire_addr))); 8462 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 8463 } 8464 } 8465 /* 8466 * At this point in ip_newroute(), ire is either the 8467 * IRE_CACHE of the next-hop gateway for an off-subnet 8468 * destination or an IRE_INTERFACE type that should be used 8469 * to resolve an on-subnet destination or an on-subnet 8470 * next-hop gateway. 8471 * 8472 * In the IRE_CACHE case, we have the following : 8473 * 8474 * 1) src_ipif - used for getting a source address. 8475 * 8476 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8477 * means packets using this IRE_CACHE will go out on 8478 * dst_ill. 8479 * 8480 * 3) The IRE sire will point to the prefix that is the 8481 * longest matching route for the destination. These 8482 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST. 8483 * 8484 * The newly created IRE_CACHE entry for the off-subnet 8485 * destination is tied to both the prefix route and the 8486 * interface route used to resolve the next-hop gateway 8487 * via the ire_phandle and ire_ihandle fields, 8488 * respectively. 8489 * 8490 * In the IRE_INTERFACE case, we have the following : 8491 * 8492 * 1) src_ipif - used for getting a source address. 8493 * 8494 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8495 * means packets using the IRE_CACHE that we will build 8496 * here will go out on dst_ill. 8497 * 8498 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 8499 * to be created will only be tied to the IRE_INTERFACE 8500 * that was derived from the ire_ihandle field. 8501 * 8502 * If sire is non-NULL, it means the destination is 8503 * off-link and we will first create the IRE_CACHE for the 8504 * gateway. Next time through ip_newroute, we will create 8505 * the IRE_CACHE for the final destination as described 8506 * above. 8507 * 8508 * In both cases, after the current resolution has been 8509 * completed (or possibly initialised, in the IRE_INTERFACE 8510 * case), the loop may be re-entered to attempt the resolution 8511 * of another RTF_MULTIRT route. 8512 * 8513 * When an IRE_CACHE entry for the off-subnet destination is 8514 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 8515 * for further processing in emission loops. 8516 */ 8517 save_ire = ire; 8518 switch (ire->ire_type) { 8519 case IRE_CACHE: { 8520 ire_t *ipif_ire; 8521 mblk_t *ire_fp_mp; 8522 8523 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); 8524 if (gw == 0) 8525 gw = ire->ire_gateway_addr; 8526 /* 8527 * We need 3 ire's to create a new cache ire for an 8528 * off-link destination from the cache ire of the 8529 * gateway. 8530 * 8531 * 1. The prefix ire 'sire' (Note that this does 8532 * not apply to the conn_nexthop_set case) 8533 * 2. The cache ire of the gateway 'ire' 8534 * 3. The interface ire 'ipif_ire' 8535 * 8536 * We have (1) and (2). We lookup (3) below. 8537 * 8538 * If there is no interface route to the gateway, 8539 * it is a race condition, where we found the cache 8540 * but the interface route has been deleted. 8541 */ 8542 if (ip_nexthop) { 8543 ipif_ire = ire_ihandle_lookup_onlink(ire); 8544 } else { 8545 ipif_ire = 8546 ire_ihandle_lookup_offlink(ire, sire); 8547 } 8548 if (ipif_ire == NULL) { 8549 ip1dbg(("ip_newroute: " 8550 "ire_ihandle_lookup_offlink failed\n")); 8551 goto icmp_err_ret; 8552 } 8553 /* 8554 * XXX We are using the same res_mp 8555 * (DL_UNITDATA_REQ) though the save_ire is not 8556 * pointing at the same ill. 8557 * This is incorrect. We need to send it up to the 8558 * resolver to get the right res_mp. For ethernets 8559 * this may be okay (ill_type == DL_ETHER). 8560 */ 8561 res_mp = save_ire->ire_nce->nce_res_mp; 8562 ire_fp_mp = NULL; 8563 /* 8564 * save_ire's nce_fp_mp can't change since it is 8565 * not an IRE_MIPRTUN or IRE_BROADCAST 8566 * LOCK_IRE_FP_MP does not do any useful work in 8567 * the case of IRE_CACHE. So we don't use it below. 8568 */ 8569 if (save_ire->ire_stq == dst_ill->ill_wq) 8570 ire_fp_mp = save_ire->ire_nce->nce_fp_mp; 8571 8572 /* 8573 * Check cached gateway IRE for any security 8574 * attributes; if found, associate the gateway 8575 * credentials group to the destination IRE. 8576 */ 8577 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 8578 mutex_enter(&attrp->igsa_lock); 8579 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 8580 GCGRP_REFHOLD(gcgrp); 8581 mutex_exit(&attrp->igsa_lock); 8582 } 8583 8584 ire = ire_create( 8585 (uchar_t *)&dst, /* dest address */ 8586 (uchar_t *)&ip_g_all_ones, /* mask */ 8587 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8588 (uchar_t *)&gw, /* gateway address */ 8589 NULL, 8590 &save_ire->ire_max_frag, 8591 ire_fp_mp, /* Fast Path header */ 8592 dst_ill->ill_rq, /* recv-from queue */ 8593 dst_ill->ill_wq, /* send-to queue */ 8594 IRE_CACHE, /* IRE type */ 8595 res_mp, 8596 src_ipif, 8597 in_ill, /* incoming ill */ 8598 (sire != NULL) ? 8599 sire->ire_mask : 0, /* Parent mask */ 8600 (sire != NULL) ? 8601 sire->ire_phandle : 0, /* Parent handle */ 8602 ipif_ire->ire_ihandle, /* Interface handle */ 8603 (sire != NULL) ? (sire->ire_flags & 8604 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 8605 (sire != NULL) ? 8606 &(sire->ire_uinfo) : &(save_ire->ire_uinfo), 8607 NULL, 8608 gcgrp, 8609 ipst); 8610 8611 if (ire == NULL) { 8612 if (gcgrp != NULL) { 8613 GCGRP_REFRELE(gcgrp); 8614 gcgrp = NULL; 8615 } 8616 ire_refrele(ipif_ire); 8617 ire_refrele(save_ire); 8618 break; 8619 } 8620 8621 /* reference now held by IRE */ 8622 gcgrp = NULL; 8623 8624 ire->ire_marks |= ire_marks; 8625 8626 /* 8627 * Prevent sire and ipif_ire from getting deleted. 8628 * The newly created ire is tied to both of them via 8629 * the phandle and ihandle respectively. 8630 */ 8631 if (sire != NULL) { 8632 IRB_REFHOLD(sire->ire_bucket); 8633 /* Has it been removed already ? */ 8634 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 8635 IRB_REFRELE(sire->ire_bucket); 8636 ire_refrele(ipif_ire); 8637 ire_refrele(save_ire); 8638 break; 8639 } 8640 } 8641 8642 IRB_REFHOLD(ipif_ire->ire_bucket); 8643 /* Has it been removed already ? */ 8644 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 8645 IRB_REFRELE(ipif_ire->ire_bucket); 8646 if (sire != NULL) 8647 IRB_REFRELE(sire->ire_bucket); 8648 ire_refrele(ipif_ire); 8649 ire_refrele(save_ire); 8650 break; 8651 } 8652 8653 xmit_mp = first_mp; 8654 /* 8655 * In the case of multirouting, a copy 8656 * of the packet is done before its sending. 8657 * The copy is used to attempt another 8658 * route resolution, in a next loop. 8659 */ 8660 if (ire->ire_flags & RTF_MULTIRT) { 8661 copy_mp = copymsg(first_mp); 8662 if (copy_mp != NULL) { 8663 xmit_mp = copy_mp; 8664 MULTIRT_DEBUG_TAG(first_mp); 8665 } 8666 } 8667 ire_add_then_send(q, ire, xmit_mp); 8668 ire_refrele(save_ire); 8669 8670 /* Assert that sire is not deleted yet. */ 8671 if (sire != NULL) { 8672 ASSERT(sire->ire_ptpn != NULL); 8673 IRB_REFRELE(sire->ire_bucket); 8674 } 8675 8676 /* Assert that ipif_ire is not deleted yet. */ 8677 ASSERT(ipif_ire->ire_ptpn != NULL); 8678 IRB_REFRELE(ipif_ire->ire_bucket); 8679 ire_refrele(ipif_ire); 8680 8681 /* 8682 * If copy_mp is not NULL, multirouting was 8683 * requested. We loop to initiate a next 8684 * route resolution attempt, starting from sire. 8685 */ 8686 if (copy_mp != NULL) { 8687 /* 8688 * Search for the next unresolved 8689 * multirt route. 8690 */ 8691 copy_mp = NULL; 8692 ipif_ire = NULL; 8693 ire = NULL; 8694 multirt_resolve_next = B_TRUE; 8695 continue; 8696 } 8697 if (sire != NULL) 8698 ire_refrele(sire); 8699 ipif_refrele(src_ipif); 8700 ill_refrele(dst_ill); 8701 return; 8702 } 8703 case IRE_IF_NORESOLVER: { 8704 /* 8705 * We have what we need to build an IRE_CACHE. 8706 * 8707 * Create a new res_mp with the IP gateway address 8708 * in destination address in the DLPI hdr if the 8709 * physical length is exactly 4 bytes. 8710 */ 8711 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 8712 uchar_t *addr; 8713 8714 if (gw) 8715 addr = (uchar_t *)&gw; 8716 else 8717 addr = (uchar_t *)&dst; 8718 8719 res_mp = ill_dlur_gen(addr, 8720 dst_ill->ill_phys_addr_length, 8721 dst_ill->ill_sap, 8722 dst_ill->ill_sap_length); 8723 8724 if (res_mp == NULL) { 8725 ip1dbg(("ip_newroute: res_mp NULL\n")); 8726 break; 8727 } 8728 } else if (dst_ill->ill_resolver_mp == NULL) { 8729 ip1dbg(("ip_newroute: dst_ill %p " 8730 "for IF_NORESOLV ire %p has " 8731 "no ill_resolver_mp\n", 8732 (void *)dst_ill, (void *)ire)); 8733 break; 8734 } else { 8735 res_mp = NULL; 8736 } 8737 8738 /* 8739 * TSol note: We are creating the ire cache for the 8740 * destination 'dst'. If 'dst' is offlink, going 8741 * through the first hop 'gw', the security attributes 8742 * of 'dst' must be set to point to the gateway 8743 * credentials of gateway 'gw'. If 'dst' is onlink, it 8744 * is possible that 'dst' is a potential gateway that is 8745 * referenced by some route that has some security 8746 * attributes. Thus in the former case, we need to do a 8747 * gcgrp_lookup of 'gw' while in the latter case we 8748 * need to do gcgrp_lookup of 'dst' itself. 8749 */ 8750 ga.ga_af = AF_INET; 8751 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, 8752 &ga.ga_addr); 8753 gcgrp = gcgrp_lookup(&ga, B_FALSE); 8754 8755 ire = ire_create( 8756 (uchar_t *)&dst, /* dest address */ 8757 (uchar_t *)&ip_g_all_ones, /* mask */ 8758 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8759 (uchar_t *)&gw, /* gateway address */ 8760 NULL, 8761 &save_ire->ire_max_frag, 8762 NULL, /* Fast Path header */ 8763 dst_ill->ill_rq, /* recv-from queue */ 8764 dst_ill->ill_wq, /* send-to queue */ 8765 IRE_CACHE, 8766 res_mp, 8767 src_ipif, 8768 in_ill, /* Incoming ill */ 8769 save_ire->ire_mask, /* Parent mask */ 8770 (sire != NULL) ? /* Parent handle */ 8771 sire->ire_phandle : 0, 8772 save_ire->ire_ihandle, /* Interface handle */ 8773 (sire != NULL) ? sire->ire_flags & 8774 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 8775 &(save_ire->ire_uinfo), 8776 NULL, 8777 gcgrp, 8778 ipst); 8779 8780 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) 8781 freeb(res_mp); 8782 8783 if (ire == NULL) { 8784 if (gcgrp != NULL) { 8785 GCGRP_REFRELE(gcgrp); 8786 gcgrp = NULL; 8787 } 8788 ire_refrele(save_ire); 8789 break; 8790 } 8791 8792 /* reference now held by IRE */ 8793 gcgrp = NULL; 8794 8795 ire->ire_marks |= ire_marks; 8796 8797 /* Prevent save_ire from getting deleted */ 8798 IRB_REFHOLD(save_ire->ire_bucket); 8799 /* Has it been removed already ? */ 8800 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8801 IRB_REFRELE(save_ire->ire_bucket); 8802 ire_refrele(save_ire); 8803 break; 8804 } 8805 8806 /* 8807 * In the case of multirouting, a copy 8808 * of the packet is made before it is sent. 8809 * The copy is used in the next 8810 * loop to attempt another resolution. 8811 */ 8812 xmit_mp = first_mp; 8813 if ((sire != NULL) && 8814 (sire->ire_flags & RTF_MULTIRT)) { 8815 copy_mp = copymsg(first_mp); 8816 if (copy_mp != NULL) { 8817 xmit_mp = copy_mp; 8818 MULTIRT_DEBUG_TAG(first_mp); 8819 } 8820 } 8821 ire_add_then_send(q, ire, xmit_mp); 8822 8823 /* Assert that it is not deleted yet. */ 8824 ASSERT(save_ire->ire_ptpn != NULL); 8825 IRB_REFRELE(save_ire->ire_bucket); 8826 ire_refrele(save_ire); 8827 8828 if (copy_mp != NULL) { 8829 /* 8830 * If we found a (no)resolver, we ignore any 8831 * trailing top priority IRE_CACHE in further 8832 * loops. This ensures that we do not omit any 8833 * (no)resolver. 8834 * This IRE_CACHE, if any, will be processed 8835 * by another thread entering ip_newroute(). 8836 * IRE_CACHE entries, if any, will be processed 8837 * by another thread entering ip_newroute(), 8838 * (upon resolver response, for instance). 8839 * This aims to force parallel multirt 8840 * resolutions as soon as a packet must be sent. 8841 * In the best case, after the tx of only one 8842 * packet, all reachable routes are resolved. 8843 * Otherwise, the resolution of all RTF_MULTIRT 8844 * routes would require several emissions. 8845 */ 8846 multirt_flags &= ~MULTIRT_CACHEGW; 8847 8848 /* 8849 * Search for the next unresolved multirt 8850 * route. 8851 */ 8852 copy_mp = NULL; 8853 save_ire = NULL; 8854 ire = NULL; 8855 multirt_resolve_next = B_TRUE; 8856 continue; 8857 } 8858 8859 /* 8860 * Don't need sire anymore 8861 */ 8862 if (sire != NULL) 8863 ire_refrele(sire); 8864 8865 ipif_refrele(src_ipif); 8866 ill_refrele(dst_ill); 8867 return; 8868 } 8869 case IRE_IF_RESOLVER: 8870 /* 8871 * We can't build an IRE_CACHE yet, but at least we 8872 * found a resolver that can help. 8873 */ 8874 res_mp = dst_ill->ill_resolver_mp; 8875 if (!OK_RESOLVER_MP(res_mp)) 8876 break; 8877 8878 /* 8879 * To be at this point in the code with a non-zero gw 8880 * means that dst is reachable through a gateway that 8881 * we have never resolved. By changing dst to the gw 8882 * addr we resolve the gateway first. 8883 * When ire_add_then_send() tries to put the IP dg 8884 * to dst, it will reenter ip_newroute() at which 8885 * time we will find the IRE_CACHE for the gw and 8886 * create another IRE_CACHE in case IRE_CACHE above. 8887 */ 8888 if (gw != INADDR_ANY) { 8889 /* 8890 * The source ipif that was determined above was 8891 * relative to the destination address, not the 8892 * gateway's. If src_ipif was not taken out of 8893 * the IRE_IF_RESOLVER entry, we'll need to call 8894 * ipif_select_source() again. 8895 */ 8896 if (src_ipif != ire->ire_ipif) { 8897 ipif_refrele(src_ipif); 8898 src_ipif = ipif_select_source(dst_ill, 8899 gw, zoneid); 8900 if (src_ipif == NULL) { 8901 if (ip_debug > 2) { 8902 pr_addr_dbg( 8903 "ip_newroute: no " 8904 "src for gw %s ", 8905 AF_INET, &gw); 8906 printf("through " 8907 "interface %s\n", 8908 dst_ill->ill_name); 8909 } 8910 goto icmp_err_ret; 8911 } 8912 } 8913 save_dst = dst; 8914 dst = gw; 8915 gw = INADDR_ANY; 8916 } 8917 8918 /* 8919 * We obtain a partial IRE_CACHE which we will pass 8920 * along with the resolver query. When the response 8921 * comes back it will be there ready for us to add. 8922 * The ire_max_frag is atomically set under the 8923 * irebucket lock in ire_add_v[46]. 8924 */ 8925 8926 ire = ire_create_mp( 8927 (uchar_t *)&dst, /* dest address */ 8928 (uchar_t *)&ip_g_all_ones, /* mask */ 8929 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8930 (uchar_t *)&gw, /* gateway address */ 8931 NULL, /* no in_src_addr */ 8932 NULL, /* ire_max_frag */ 8933 NULL, /* Fast Path header */ 8934 dst_ill->ill_rq, /* recv-from queue */ 8935 dst_ill->ill_wq, /* send-to queue */ 8936 IRE_CACHE, 8937 NULL, 8938 src_ipif, /* Interface ipif */ 8939 in_ill, /* Incoming ILL */ 8940 save_ire->ire_mask, /* Parent mask */ 8941 0, 8942 save_ire->ire_ihandle, /* Interface handle */ 8943 0, /* flags if any */ 8944 &(save_ire->ire_uinfo), 8945 NULL, 8946 NULL, 8947 ipst); 8948 8949 if (ire == NULL) { 8950 ire_refrele(save_ire); 8951 break; 8952 } 8953 8954 if ((sire != NULL) && 8955 (sire->ire_flags & RTF_MULTIRT)) { 8956 copy_mp = copymsg(first_mp); 8957 if (copy_mp != NULL) 8958 MULTIRT_DEBUG_TAG(copy_mp); 8959 } 8960 8961 ire->ire_marks |= ire_marks; 8962 8963 /* 8964 * Construct message chain for the resolver 8965 * of the form: 8966 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8967 * Packet could contain a IPSEC_OUT mp. 8968 * 8969 * NOTE : ire will be added later when the response 8970 * comes back from ARP. If the response does not 8971 * come back, ARP frees the packet. For this reason, 8972 * we can't REFHOLD the bucket of save_ire to prevent 8973 * deletions. We may not be able to REFRELE the bucket 8974 * if the response never comes back. Thus, before 8975 * adding the ire, ire_add_v4 will make sure that the 8976 * interface route does not get deleted. This is the 8977 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 8978 * where we can always prevent deletions because of 8979 * the synchronous nature of adding IRES i.e 8980 * ire_add_then_send is called after creating the IRE. 8981 */ 8982 ASSERT(ire->ire_mp != NULL); 8983 ire->ire_mp->b_cont = first_mp; 8984 /* Have saved_mp handy, for cleanup if canput fails */ 8985 saved_mp = mp; 8986 mp = copyb(res_mp); 8987 if (mp == NULL) { 8988 /* Prepare for cleanup */ 8989 mp = saved_mp; /* pkt */ 8990 ire_delete(ire); /* ire_mp */ 8991 ire = NULL; 8992 ire_refrele(save_ire); 8993 if (copy_mp != NULL) { 8994 MULTIRT_DEBUG_UNTAG(copy_mp); 8995 freemsg(copy_mp); 8996 copy_mp = NULL; 8997 } 8998 break; 8999 } 9000 linkb(mp, ire->ire_mp); 9001 9002 /* 9003 * Fill in the source and dest addrs for the resolver. 9004 * NOTE: this depends on memory layouts imposed by 9005 * ill_init(). 9006 */ 9007 areq = (areq_t *)mp->b_rptr; 9008 addrp = (ipaddr_t *)((char *)areq + 9009 areq->areq_sender_addr_offset); 9010 if (do_attach_ill) { 9011 /* 9012 * This is bind to no failover case. 9013 * arp packet also must go out on attach_ill. 9014 */ 9015 ASSERT(ipha->ipha_src != NULL); 9016 *addrp = ipha->ipha_src; 9017 } else { 9018 *addrp = save_ire->ire_src_addr; 9019 } 9020 9021 ire_refrele(save_ire); 9022 addrp = (ipaddr_t *)((char *)areq + 9023 areq->areq_target_addr_offset); 9024 *addrp = dst; 9025 /* Up to the resolver. */ 9026 if (canputnext(dst_ill->ill_rq) && 9027 !(dst_ill->ill_arp_closing)) { 9028 putnext(dst_ill->ill_rq, mp); 9029 ire = NULL; 9030 if (copy_mp != NULL) { 9031 /* 9032 * If we found a resolver, we ignore 9033 * any trailing top priority IRE_CACHE 9034 * in the further loops. This ensures 9035 * that we do not omit any resolver. 9036 * IRE_CACHE entries, if any, will be 9037 * processed next time we enter 9038 * ip_newroute(). 9039 */ 9040 multirt_flags &= ~MULTIRT_CACHEGW; 9041 /* 9042 * Search for the next unresolved 9043 * multirt route. 9044 */ 9045 first_mp = copy_mp; 9046 copy_mp = NULL; 9047 /* Prepare the next resolution loop. */ 9048 mp = first_mp; 9049 EXTRACT_PKT_MP(mp, first_mp, 9050 mctl_present); 9051 if (mctl_present) 9052 io = (ipsec_out_t *) 9053 first_mp->b_rptr; 9054 ipha = (ipha_t *)mp->b_rptr; 9055 9056 ASSERT(sire != NULL); 9057 9058 dst = save_dst; 9059 multirt_resolve_next = B_TRUE; 9060 continue; 9061 } 9062 9063 if (sire != NULL) 9064 ire_refrele(sire); 9065 9066 /* 9067 * The response will come back in ip_wput 9068 * with db_type IRE_DB_TYPE. 9069 */ 9070 ipif_refrele(src_ipif); 9071 ill_refrele(dst_ill); 9072 return; 9073 } else { 9074 /* Prepare for cleanup */ 9075 DTRACE_PROBE1(ip__newroute__drop, mblk_t *, 9076 mp); 9077 mp->b_cont = NULL; 9078 freeb(mp); /* areq */ 9079 /* 9080 * this is an ire that is not added to the 9081 * cache. ire_freemblk will handle the release 9082 * of any resources associated with the ire. 9083 */ 9084 ire_delete(ire); /* ire_mp */ 9085 mp = saved_mp; /* pkt */ 9086 ire = NULL; 9087 if (copy_mp != NULL) { 9088 MULTIRT_DEBUG_UNTAG(copy_mp); 9089 freemsg(copy_mp); 9090 copy_mp = NULL; 9091 } 9092 break; 9093 } 9094 default: 9095 break; 9096 } 9097 } while (multirt_resolve_next); 9098 9099 ip1dbg(("ip_newroute: dropped\n")); 9100 /* Did this packet originate externally? */ 9101 if (mp->b_prev) { 9102 mp->b_next = NULL; 9103 mp->b_prev = NULL; 9104 if (in_ill != NULL) { 9105 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInDiscards); 9106 } else { 9107 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 9108 } 9109 } else { 9110 if (dst_ill != NULL) { 9111 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards); 9112 } else { 9113 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 9114 } 9115 } 9116 ASSERT(copy_mp == NULL); 9117 MULTIRT_DEBUG_UNTAG(first_mp); 9118 freemsg(first_mp); 9119 if (ire != NULL) 9120 ire_refrele(ire); 9121 if (sire != NULL) 9122 ire_refrele(sire); 9123 if (src_ipif != NULL) 9124 ipif_refrele(src_ipif); 9125 if (dst_ill != NULL) 9126 ill_refrele(dst_ill); 9127 return; 9128 9129 icmp_err_ret: 9130 ip1dbg(("ip_newroute: no route\n")); 9131 if (src_ipif != NULL) 9132 ipif_refrele(src_ipif); 9133 if (dst_ill != NULL) 9134 ill_refrele(dst_ill); 9135 if (sire != NULL) 9136 ire_refrele(sire); 9137 /* Did this packet originate externally? */ 9138 if (mp->b_prev) { 9139 mp->b_next = NULL; 9140 mp->b_prev = NULL; 9141 if (in_ill != NULL) { 9142 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInNoRoutes); 9143 } else { 9144 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes); 9145 } 9146 q = WR(q); 9147 } else { 9148 /* 9149 * There is no outgoing ill, so just increment the 9150 * system MIB. 9151 */ 9152 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 9153 /* 9154 * Since ip_wput() isn't close to finished, we fill 9155 * in enough of the header for credible error reporting. 9156 */ 9157 if (ip_hdr_complete(ipha, zoneid, ipst)) { 9158 /* Failed */ 9159 MULTIRT_DEBUG_UNTAG(first_mp); 9160 freemsg(first_mp); 9161 if (ire != NULL) 9162 ire_refrele(ire); 9163 return; 9164 } 9165 } 9166 9167 /* 9168 * At this point we will have ire only if RTF_BLACKHOLE 9169 * or RTF_REJECT flags are set on the IRE. It will not 9170 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9171 */ 9172 if (ire != NULL) { 9173 if (ire->ire_flags & RTF_BLACKHOLE) { 9174 ire_refrele(ire); 9175 MULTIRT_DEBUG_UNTAG(first_mp); 9176 freemsg(first_mp); 9177 return; 9178 } 9179 ire_refrele(ire); 9180 } 9181 if (ip_source_routed(ipha, ipst)) { 9182 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, 9183 zoneid, ipst); 9184 return; 9185 } 9186 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9187 } 9188 9189 ip_opt_info_t zero_info; 9190 9191 /* 9192 * IPv4 - 9193 * ip_newroute_ipif is called by ip_wput_multicast and 9194 * ip_rput_forward_multicast whenever we need to send 9195 * out a packet to a destination address for which we do not have specific 9196 * routing information. It is used when the packet will be sent out 9197 * on a specific interface. It is also called by ip_wput() when IP_XMIT_IF 9198 * socket option is set or icmp error message wants to go out on a particular 9199 * interface for a unicast packet. 9200 * 9201 * In most cases, the destination address is resolved thanks to the ipif 9202 * intrinsic resolver. However, there are some cases where the call to 9203 * ip_newroute_ipif must take into account the potential presence of 9204 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 9205 * that uses the interface. This is specified through flags, 9206 * which can be a combination of: 9207 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 9208 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 9209 * and flags. Additionally, the packet source address has to be set to 9210 * the specified address. The caller is thus expected to set this flag 9211 * if the packet has no specific source address yet. 9212 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 9213 * flag, the resulting ire will inherit the flag. All unresolved routes 9214 * to the destination must be explored in the same call to 9215 * ip_newroute_ipif(). 9216 */ 9217 static void 9218 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 9219 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop) 9220 { 9221 areq_t *areq; 9222 ire_t *ire = NULL; 9223 mblk_t *res_mp; 9224 ipaddr_t *addrp; 9225 mblk_t *first_mp; 9226 ire_t *save_ire = NULL; 9227 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ 9228 ipif_t *src_ipif = NULL; 9229 ushort_t ire_marks = 0; 9230 ill_t *dst_ill = NULL; 9231 boolean_t mctl_present; 9232 ipsec_out_t *io; 9233 ipha_t *ipha; 9234 int ihandle = 0; 9235 mblk_t *saved_mp; 9236 ire_t *fire = NULL; 9237 mblk_t *copy_mp = NULL; 9238 boolean_t multirt_resolve_next; 9239 ipaddr_t ipha_dst; 9240 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 9241 9242 /* 9243 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 9244 * here for uniformity 9245 */ 9246 ipif_refhold(ipif); 9247 9248 /* 9249 * This loop is run only once in most cases. 9250 * We loop to resolve further routes only when the destination 9251 * can be reached through multiple RTF_MULTIRT-flagged ires. 9252 */ 9253 do { 9254 if (dst_ill != NULL) { 9255 ill_refrele(dst_ill); 9256 dst_ill = NULL; 9257 } 9258 if (src_ipif != NULL) { 9259 ipif_refrele(src_ipif); 9260 src_ipif = NULL; 9261 } 9262 multirt_resolve_next = B_FALSE; 9263 9264 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 9265 ipif->ipif_ill->ill_name)); 9266 9267 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 9268 if (mctl_present) 9269 io = (ipsec_out_t *)first_mp->b_rptr; 9270 9271 ipha = (ipha_t *)mp->b_rptr; 9272 9273 /* 9274 * Save the packet destination address, we may need it after 9275 * the packet has been consumed. 9276 */ 9277 ipha_dst = ipha->ipha_dst; 9278 9279 /* 9280 * If the interface is a pt-pt interface we look for an 9281 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 9282 * local_address and the pt-pt destination address. Otherwise 9283 * we just match the local address. 9284 * NOTE: dst could be different than ipha->ipha_dst in case 9285 * of sending igmp multicast packets over a point-to-point 9286 * connection. 9287 * Thus we must be careful enough to check ipha_dst to be a 9288 * multicast address, otherwise it will take xmit_if path for 9289 * multicast packets resulting into kernel stack overflow by 9290 * repeated calls to ip_newroute_ipif from ire_send(). 9291 */ 9292 if (CLASSD(ipha_dst) && 9293 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 9294 goto err_ret; 9295 } 9296 9297 /* 9298 * We check if an IRE_OFFSUBNET for the addr that goes through 9299 * ipif exists. We need it to determine if the RTF_SETSRC and/or 9300 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 9301 * propagate its flags to the new ire. 9302 */ 9303 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 9304 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 9305 ip2dbg(("ip_newroute_ipif: " 9306 "ipif_lookup_multi_ire(" 9307 "ipif %p, dst %08x) = fire %p\n", 9308 (void *)ipif, ntohl(dst), (void *)fire)); 9309 } 9310 9311 if (mctl_present && io->ipsec_out_attach_if) { 9312 attach_ill = ip_grab_attach_ill(NULL, first_mp, 9313 io->ipsec_out_ill_index, B_FALSE, ipst); 9314 9315 /* Failure case frees things for us. */ 9316 if (attach_ill == NULL) { 9317 ipif_refrele(ipif); 9318 if (fire != NULL) 9319 ire_refrele(fire); 9320 return; 9321 } 9322 9323 /* 9324 * Check if we need an ire that will not be 9325 * looked up by anybody else i.e. HIDDEN. 9326 */ 9327 if (ill_is_probeonly(attach_ill)) { 9328 ire_marks = IRE_MARK_HIDDEN; 9329 } 9330 /* 9331 * ip_wput passes the right ipif for IPIF_NOFAILOVER 9332 * case. 9333 */ 9334 dst_ill = ipif->ipif_ill; 9335 /* attach_ill has been refheld by ip_grab_attach_ill */ 9336 ASSERT(dst_ill == attach_ill); 9337 } else { 9338 /* 9339 * If this is set by IP_XMIT_IF, then make sure that 9340 * ipif is pointing to the same ill as the IP_XMIT_IF 9341 * specified ill. 9342 */ 9343 ASSERT((connp == NULL) || 9344 (connp->conn_xmit_if_ill == NULL) || 9345 (connp->conn_xmit_if_ill == ipif->ipif_ill)); 9346 /* 9347 * If the interface belongs to an interface group, 9348 * make sure the next possible interface in the group 9349 * is used. This encourages load spreading among 9350 * peers in an interface group. 9351 * Note: load spreading is disabled for RTF_MULTIRT 9352 * routes. 9353 */ 9354 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9355 (fire->ire_flags & RTF_MULTIRT)) { 9356 /* 9357 * Don't perform outbound load spreading 9358 * in the case of an RTF_MULTIRT issued route, 9359 * we actually typically want to replicate 9360 * outgoing packets through particular 9361 * interfaces. 9362 */ 9363 dst_ill = ipif->ipif_ill; 9364 ill_refhold(dst_ill); 9365 } else { 9366 dst_ill = ip_newroute_get_dst_ill( 9367 ipif->ipif_ill); 9368 } 9369 if (dst_ill == NULL) { 9370 if (ip_debug > 2) { 9371 pr_addr_dbg("ip_newroute_ipif: " 9372 "no dst ill for dst %s\n", 9373 AF_INET, &dst); 9374 } 9375 goto err_ret; 9376 } 9377 } 9378 9379 /* 9380 * Pick a source address preferring non-deprecated ones. 9381 * Unlike ip_newroute, we don't do any source address 9382 * selection here since for multicast it really does not help 9383 * in inbound load spreading as in the unicast case. 9384 */ 9385 if ((flags & RTF_SETSRC) && (fire != NULL) && 9386 (fire->ire_flags & RTF_SETSRC)) { 9387 /* 9388 * As requested by flags, an IRE_OFFSUBNET was looked up 9389 * on that interface. This ire has RTF_SETSRC flag, so 9390 * the source address of the packet must be changed. 9391 * Check that the ipif matching the requested source 9392 * address still exists. 9393 */ 9394 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 9395 zoneid, NULL, NULL, NULL, NULL, ipst); 9396 } 9397 if (((ipif->ipif_flags & IPIF_DEPRECATED) || 9398 (connp != NULL && ipif->ipif_zoneid != zoneid && 9399 ipif->ipif_zoneid != ALL_ZONES)) && 9400 (src_ipif == NULL)) { 9401 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 9402 if (src_ipif == NULL) { 9403 if (ip_debug > 2) { 9404 /* ip1dbg */ 9405 pr_addr_dbg("ip_newroute_ipif: " 9406 "no src for dst %s", 9407 AF_INET, &dst); 9408 } 9409 ip1dbg((" through interface %s\n", 9410 dst_ill->ill_name)); 9411 goto err_ret; 9412 } 9413 ipif_refrele(ipif); 9414 ipif = src_ipif; 9415 ipif_refhold(ipif); 9416 } 9417 if (src_ipif == NULL) { 9418 src_ipif = ipif; 9419 ipif_refhold(src_ipif); 9420 } 9421 9422 /* 9423 * Assign a source address while we have the conn. 9424 * We can't have ip_wput_ire pick a source address when the 9425 * packet returns from arp since conn_unspec_src might be set 9426 * and we loose the conn when going through arp. 9427 */ 9428 if (ipha->ipha_src == INADDR_ANY && 9429 (connp == NULL || !connp->conn_unspec_src)) { 9430 ipha->ipha_src = src_ipif->ipif_src_addr; 9431 } 9432 9433 /* 9434 * In case of IP_XMIT_IF, it is possible that the outgoing 9435 * interface does not have an interface ire. 9436 * Example: Thousands of mobileip PPP interfaces to mobile 9437 * nodes. We don't want to create interface ires because 9438 * packets from other mobile nodes must not take the route 9439 * via interface ires to the visiting mobile node without 9440 * going through the home agent, in absence of mobileip 9441 * route optimization. 9442 */ 9443 if (CLASSD(ipha_dst) && (connp == NULL || 9444 connp->conn_xmit_if_ill == NULL) && 9445 infop->ip_opt_ill_index == 0) { 9446 /* ipif_to_ire returns an held ire */ 9447 ire = ipif_to_ire(ipif); 9448 if (ire == NULL) 9449 goto err_ret; 9450 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 9451 goto err_ret; 9452 /* 9453 * ihandle is needed when the ire is added to 9454 * cache table. 9455 */ 9456 save_ire = ire; 9457 ihandle = save_ire->ire_ihandle; 9458 9459 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 9460 "flags %04x\n", 9461 (void *)ire, (void *)ipif, flags)); 9462 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9463 (fire->ire_flags & RTF_MULTIRT)) { 9464 /* 9465 * As requested by flags, an IRE_OFFSUBNET was 9466 * looked up on that interface. This ire has 9467 * RTF_MULTIRT flag, so the resolution loop will 9468 * be re-entered to resolve additional routes on 9469 * other interfaces. For that purpose, a copy of 9470 * the packet is performed at this point. 9471 */ 9472 fire->ire_last_used_time = lbolt; 9473 copy_mp = copymsg(first_mp); 9474 if (copy_mp) { 9475 MULTIRT_DEBUG_TAG(copy_mp); 9476 } 9477 } 9478 if ((flags & RTF_SETSRC) && (fire != NULL) && 9479 (fire->ire_flags & RTF_SETSRC)) { 9480 /* 9481 * As requested by flags, an IRE_OFFSUBET was 9482 * looked up on that interface. This ire has 9483 * RTF_SETSRC flag, so the source address of the 9484 * packet must be changed. 9485 */ 9486 ipha->ipha_src = fire->ire_src_addr; 9487 } 9488 } else { 9489 ASSERT((connp == NULL) || 9490 (connp->conn_xmit_if_ill != NULL) || 9491 (connp->conn_dontroute) || 9492 infop->ip_opt_ill_index != 0); 9493 /* 9494 * The only ways we can come here are: 9495 * 1) IP_XMIT_IF socket option is set 9496 * 2) ICMP error message generated from 9497 * ip_mrtun_forward() routine and it needs 9498 * to go through the specified ill. 9499 * 3) SO_DONTROUTE socket option is set 9500 * 4) IP_PKTINFO option is passed in as ancillary data. 9501 * In all cases, the new ire will not be added 9502 * into cache table. 9503 */ 9504 ire_marks |= IRE_MARK_NOADD; 9505 } 9506 9507 switch (ipif->ipif_net_type) { 9508 case IRE_IF_NORESOLVER: { 9509 /* We have what we need to build an IRE_CACHE. */ 9510 mblk_t *res_mp; 9511 9512 /* 9513 * Create a new res_mp with the 9514 * IP gateway address as destination address in the 9515 * DLPI hdr if the physical length is exactly 4 bytes. 9516 */ 9517 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 9518 res_mp = ill_dlur_gen((uchar_t *)&dst, 9519 dst_ill->ill_phys_addr_length, 9520 dst_ill->ill_sap, 9521 dst_ill->ill_sap_length); 9522 } else if (dst_ill->ill_resolver_mp == NULL) { 9523 ip1dbg(("ip_newroute: dst_ill %p " 9524 "for IF_NORESOLV ire %p has " 9525 "no ill_resolver_mp\n", 9526 (void *)dst_ill, (void *)ire)); 9527 break; 9528 } else { 9529 /* use the value set in ip_ll_subnet_defaults */ 9530 res_mp = ill_dlur_gen(NULL, 9531 dst_ill->ill_phys_addr_length, 9532 dst_ill->ill_sap, 9533 dst_ill->ill_sap_length); 9534 } 9535 9536 if (res_mp == NULL) 9537 break; 9538 /* 9539 * The new ire inherits the IRE_OFFSUBNET flags 9540 * and source address, if this was requested. 9541 */ 9542 ire = ire_create( 9543 (uchar_t *)&dst, /* dest address */ 9544 (uchar_t *)&ip_g_all_ones, /* mask */ 9545 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9546 NULL, /* gateway address */ 9547 NULL, 9548 &ipif->ipif_mtu, 9549 NULL, /* Fast Path header */ 9550 dst_ill->ill_rq, /* recv-from queue */ 9551 dst_ill->ill_wq, /* send-to queue */ 9552 IRE_CACHE, 9553 res_mp, 9554 src_ipif, 9555 NULL, 9556 (save_ire != NULL ? save_ire->ire_mask : 0), 9557 (fire != NULL) ? /* Parent handle */ 9558 fire->ire_phandle : 0, 9559 ihandle, /* Interface handle */ 9560 (fire != NULL) ? 9561 (fire->ire_flags & 9562 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9563 (save_ire == NULL ? &ire_uinfo_null : 9564 &save_ire->ire_uinfo), 9565 NULL, 9566 NULL, 9567 ipst); 9568 9569 freeb(res_mp); 9570 9571 if (ire == NULL) { 9572 if (save_ire != NULL) 9573 ire_refrele(save_ire); 9574 break; 9575 } 9576 9577 ire->ire_marks |= ire_marks; 9578 9579 /* 9580 * If IRE_MARK_NOADD is set then we need to convert 9581 * the max_fragp to a useable value now. This is 9582 * normally done in ire_add_v[46]. We also need to 9583 * associate the ire with an nce (normally would be 9584 * done in ip_wput_nondata()). 9585 * 9586 * Note that IRE_MARK_NOADD packets created here 9587 * do not have a non-null ire_mp pointer. The null 9588 * value of ire_bucket indicates that they were 9589 * never added. 9590 */ 9591 if (ire->ire_marks & IRE_MARK_NOADD) { 9592 uint_t max_frag; 9593 9594 max_frag = *ire->ire_max_fragp; 9595 ire->ire_max_fragp = NULL; 9596 ire->ire_max_frag = max_frag; 9597 9598 if ((ire->ire_nce = ndp_lookup_v4( 9599 ire_to_ill(ire), 9600 (ire->ire_gateway_addr != INADDR_ANY ? 9601 &ire->ire_gateway_addr : &ire->ire_addr), 9602 B_FALSE)) == NULL) { 9603 if (save_ire != NULL) 9604 ire_refrele(save_ire); 9605 break; 9606 } 9607 ASSERT(ire->ire_nce->nce_state == 9608 ND_REACHABLE); 9609 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 9610 } 9611 9612 /* Prevent save_ire from getting deleted */ 9613 if (save_ire != NULL) { 9614 IRB_REFHOLD(save_ire->ire_bucket); 9615 /* Has it been removed already ? */ 9616 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 9617 IRB_REFRELE(save_ire->ire_bucket); 9618 ire_refrele(save_ire); 9619 break; 9620 } 9621 } 9622 9623 ire_add_then_send(q, ire, first_mp); 9624 9625 /* Assert that save_ire is not deleted yet. */ 9626 if (save_ire != NULL) { 9627 ASSERT(save_ire->ire_ptpn != NULL); 9628 IRB_REFRELE(save_ire->ire_bucket); 9629 ire_refrele(save_ire); 9630 save_ire = NULL; 9631 } 9632 if (fire != NULL) { 9633 ire_refrele(fire); 9634 fire = NULL; 9635 } 9636 9637 /* 9638 * the resolution loop is re-entered if this 9639 * was requested through flags and if we 9640 * actually are in a multirouting case. 9641 */ 9642 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9643 boolean_t need_resolve = 9644 ire_multirt_need_resolve(ipha_dst, 9645 MBLK_GETLABEL(copy_mp), ipst); 9646 if (!need_resolve) { 9647 MULTIRT_DEBUG_UNTAG(copy_mp); 9648 freemsg(copy_mp); 9649 copy_mp = NULL; 9650 } else { 9651 /* 9652 * ipif_lookup_group() calls 9653 * ire_lookup_multi() that uses 9654 * ire_ftable_lookup() to find 9655 * an IRE_INTERFACE for the group. 9656 * In the multirt case, 9657 * ire_lookup_multi() then invokes 9658 * ire_multirt_lookup() to find 9659 * the next resolvable ire. 9660 * As a result, we obtain an new 9661 * interface, derived from the 9662 * next ire. 9663 */ 9664 ipif_refrele(ipif); 9665 ipif = ipif_lookup_group(ipha_dst, 9666 zoneid, ipst); 9667 ip2dbg(("ip_newroute_ipif: " 9668 "multirt dst %08x, ipif %p\n", 9669 htonl(dst), (void *)ipif)); 9670 if (ipif != NULL) { 9671 mp = copy_mp; 9672 copy_mp = NULL; 9673 multirt_resolve_next = B_TRUE; 9674 continue; 9675 } else { 9676 freemsg(copy_mp); 9677 } 9678 } 9679 } 9680 if (ipif != NULL) 9681 ipif_refrele(ipif); 9682 ill_refrele(dst_ill); 9683 ipif_refrele(src_ipif); 9684 return; 9685 } 9686 case IRE_IF_RESOLVER: 9687 /* 9688 * We can't build an IRE_CACHE yet, but at least 9689 * we found a resolver that can help. 9690 */ 9691 res_mp = dst_ill->ill_resolver_mp; 9692 if (!OK_RESOLVER_MP(res_mp)) 9693 break; 9694 9695 /* 9696 * We obtain a partial IRE_CACHE which we will pass 9697 * along with the resolver query. When the response 9698 * comes back it will be there ready for us to add. 9699 * The new ire inherits the IRE_OFFSUBNET flags 9700 * and source address, if this was requested. 9701 * The ire_max_frag is atomically set under the 9702 * irebucket lock in ire_add_v[46]. Only in the 9703 * case of IRE_MARK_NOADD, we set it here itself. 9704 */ 9705 ire = ire_create_mp( 9706 (uchar_t *)&dst, /* dest address */ 9707 (uchar_t *)&ip_g_all_ones, /* mask */ 9708 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9709 NULL, /* gateway address */ 9710 NULL, /* no in_src_addr */ 9711 (ire_marks & IRE_MARK_NOADD) ? 9712 ipif->ipif_mtu : 0, /* max_frag */ 9713 NULL, /* Fast path header */ 9714 dst_ill->ill_rq, /* recv-from queue */ 9715 dst_ill->ill_wq, /* send-to queue */ 9716 IRE_CACHE, 9717 NULL, /* let ire_nce_init figure res_mp out */ 9718 src_ipif, 9719 NULL, 9720 (save_ire != NULL ? save_ire->ire_mask : 0), 9721 (fire != NULL) ? /* Parent handle */ 9722 fire->ire_phandle : 0, 9723 ihandle, /* Interface handle */ 9724 (fire != NULL) ? /* flags if any */ 9725 (fire->ire_flags & 9726 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9727 (save_ire == NULL ? &ire_uinfo_null : 9728 &save_ire->ire_uinfo), 9729 NULL, 9730 NULL, 9731 ipst); 9732 9733 if (save_ire != NULL) { 9734 ire_refrele(save_ire); 9735 save_ire = NULL; 9736 } 9737 if (ire == NULL) 9738 break; 9739 9740 ire->ire_marks |= ire_marks; 9741 /* 9742 * Construct message chain for the resolver of the 9743 * form: 9744 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 9745 * 9746 * NOTE : ire will be added later when the response 9747 * comes back from ARP. If the response does not 9748 * come back, ARP frees the packet. For this reason, 9749 * we can't REFHOLD the bucket of save_ire to prevent 9750 * deletions. We may not be able to REFRELE the 9751 * bucket if the response never comes back. 9752 * Thus, before adding the ire, ire_add_v4 will make 9753 * sure that the interface route does not get deleted. 9754 * This is the only case unlike ip_newroute_v6, 9755 * ip_newroute_ipif_v6 where we can always prevent 9756 * deletions because ire_add_then_send is called after 9757 * creating the IRE. 9758 * If IRE_MARK_NOADD is set, then ire_add_then_send 9759 * does not add this IRE into the IRE CACHE. 9760 */ 9761 ASSERT(ire->ire_mp != NULL); 9762 ire->ire_mp->b_cont = first_mp; 9763 /* Have saved_mp handy, for cleanup if canput fails */ 9764 saved_mp = mp; 9765 mp = copyb(res_mp); 9766 if (mp == NULL) { 9767 /* Prepare for cleanup */ 9768 mp = saved_mp; /* pkt */ 9769 ire_delete(ire); /* ire_mp */ 9770 ire = NULL; 9771 if (copy_mp != NULL) { 9772 MULTIRT_DEBUG_UNTAG(copy_mp); 9773 freemsg(copy_mp); 9774 copy_mp = NULL; 9775 } 9776 break; 9777 } 9778 linkb(mp, ire->ire_mp); 9779 9780 /* 9781 * Fill in the source and dest addrs for the resolver. 9782 * NOTE: this depends on memory layouts imposed by 9783 * ill_init(). 9784 */ 9785 areq = (areq_t *)mp->b_rptr; 9786 addrp = (ipaddr_t *)((char *)areq + 9787 areq->areq_sender_addr_offset); 9788 *addrp = ire->ire_src_addr; 9789 addrp = (ipaddr_t *)((char *)areq + 9790 areq->areq_target_addr_offset); 9791 *addrp = dst; 9792 /* Up to the resolver. */ 9793 if (canputnext(dst_ill->ill_rq) && 9794 !(dst_ill->ill_arp_closing)) { 9795 putnext(dst_ill->ill_rq, mp); 9796 /* 9797 * The response will come back in ip_wput 9798 * with db_type IRE_DB_TYPE. 9799 */ 9800 } else { 9801 mp->b_cont = NULL; 9802 freeb(mp); /* areq */ 9803 ire_delete(ire); /* ire_mp */ 9804 saved_mp->b_next = NULL; 9805 saved_mp->b_prev = NULL; 9806 freemsg(first_mp); /* pkt */ 9807 ip2dbg(("ip_newroute_ipif: dropped\n")); 9808 } 9809 9810 if (fire != NULL) { 9811 ire_refrele(fire); 9812 fire = NULL; 9813 } 9814 9815 9816 /* 9817 * The resolution loop is re-entered if this was 9818 * requested through flags and we actually are 9819 * in a multirouting case. 9820 */ 9821 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9822 boolean_t need_resolve = 9823 ire_multirt_need_resolve(ipha_dst, 9824 MBLK_GETLABEL(copy_mp), ipst); 9825 if (!need_resolve) { 9826 MULTIRT_DEBUG_UNTAG(copy_mp); 9827 freemsg(copy_mp); 9828 copy_mp = NULL; 9829 } else { 9830 /* 9831 * ipif_lookup_group() calls 9832 * ire_lookup_multi() that uses 9833 * ire_ftable_lookup() to find 9834 * an IRE_INTERFACE for the group. 9835 * In the multirt case, 9836 * ire_lookup_multi() then invokes 9837 * ire_multirt_lookup() to find 9838 * the next resolvable ire. 9839 * As a result, we obtain an new 9840 * interface, derived from the 9841 * next ire. 9842 */ 9843 ipif_refrele(ipif); 9844 ipif = ipif_lookup_group(ipha_dst, 9845 zoneid, ipst); 9846 if (ipif != NULL) { 9847 mp = copy_mp; 9848 copy_mp = NULL; 9849 multirt_resolve_next = B_TRUE; 9850 continue; 9851 } else { 9852 freemsg(copy_mp); 9853 } 9854 } 9855 } 9856 if (ipif != NULL) 9857 ipif_refrele(ipif); 9858 ill_refrele(dst_ill); 9859 ipif_refrele(src_ipif); 9860 return; 9861 default: 9862 break; 9863 } 9864 } while (multirt_resolve_next); 9865 9866 err_ret: 9867 ip2dbg(("ip_newroute_ipif: dropped\n")); 9868 if (fire != NULL) 9869 ire_refrele(fire); 9870 ipif_refrele(ipif); 9871 /* Did this packet originate externally? */ 9872 if (dst_ill != NULL) 9873 ill_refrele(dst_ill); 9874 if (src_ipif != NULL) 9875 ipif_refrele(src_ipif); 9876 if (mp->b_prev || mp->b_next) { 9877 mp->b_next = NULL; 9878 mp->b_prev = NULL; 9879 } else { 9880 /* 9881 * Since ip_wput() isn't close to finished, we fill 9882 * in enough of the header for credible error reporting. 9883 */ 9884 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 9885 /* Failed */ 9886 freemsg(first_mp); 9887 if (ire != NULL) 9888 ire_refrele(ire); 9889 return; 9890 } 9891 } 9892 /* 9893 * At this point we will have ire only if RTF_BLACKHOLE 9894 * or RTF_REJECT flags are set on the IRE. It will not 9895 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9896 */ 9897 if (ire != NULL) { 9898 if (ire->ire_flags & RTF_BLACKHOLE) { 9899 ire_refrele(ire); 9900 freemsg(first_mp); 9901 return; 9902 } 9903 ire_refrele(ire); 9904 } 9905 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9906 } 9907 9908 /* Name/Value Table Lookup Routine */ 9909 char * 9910 ip_nv_lookup(nv_t *nv, int value) 9911 { 9912 if (!nv) 9913 return (NULL); 9914 for (; nv->nv_name; nv++) { 9915 if (nv->nv_value == value) 9916 return (nv->nv_name); 9917 } 9918 return ("unknown"); 9919 } 9920 9921 /* 9922 * This is a module open, i.e. this is a control stream for access 9923 * to a DLPI device. We allocate an ill_t as the instance data in 9924 * this case. 9925 */ 9926 int 9927 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9928 { 9929 ill_t *ill; 9930 int err; 9931 zoneid_t zoneid; 9932 netstack_t *ns; 9933 ip_stack_t *ipst; 9934 9935 /* 9936 * Prevent unprivileged processes from pushing IP so that 9937 * they can't send raw IP. 9938 */ 9939 if (secpolicy_net_rawaccess(credp) != 0) 9940 return (EPERM); 9941 9942 ns = netstack_find_by_cred(credp); 9943 ASSERT(ns != NULL); 9944 ipst = ns->netstack_ip; 9945 ASSERT(ipst != NULL); 9946 9947 /* 9948 * For exclusive stacks we set the zoneid to zero 9949 * to make IP operate as if in the global zone. 9950 */ 9951 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9952 zoneid = GLOBAL_ZONEID; 9953 else 9954 zoneid = crgetzoneid(credp); 9955 9956 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 9957 q->q_ptr = WR(q)->q_ptr = ill; 9958 ill->ill_ipst = ipst; 9959 ill->ill_zoneid = zoneid; 9960 9961 /* 9962 * ill_init initializes the ill fields and then sends down 9963 * down a DL_INFO_REQ after calling qprocson. 9964 */ 9965 err = ill_init(q, ill); 9966 if (err != 0) { 9967 mi_free(ill); 9968 netstack_rele(ipst->ips_netstack); 9969 q->q_ptr = NULL; 9970 WR(q)->q_ptr = NULL; 9971 return (err); 9972 } 9973 9974 /* ill_init initializes the ipsq marking this thread as writer */ 9975 ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE, B_TRUE); 9976 /* Wait for the DL_INFO_ACK */ 9977 mutex_enter(&ill->ill_lock); 9978 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 9979 /* 9980 * Return value of 0 indicates a pending signal. 9981 */ 9982 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 9983 if (err == 0) { 9984 mutex_exit(&ill->ill_lock); 9985 (void) ip_close(q, 0); 9986 return (EINTR); 9987 } 9988 } 9989 mutex_exit(&ill->ill_lock); 9990 9991 /* 9992 * ip_rput_other could have set an error in ill_error on 9993 * receipt of M_ERROR. 9994 */ 9995 9996 err = ill->ill_error; 9997 if (err != 0) { 9998 (void) ip_close(q, 0); 9999 return (err); 10000 } 10001 10002 ill->ill_credp = credp; 10003 crhold(credp); 10004 10005 mutex_enter(&ipst->ips_ip_mi_lock); 10006 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag, 10007 credp); 10008 mutex_exit(&ipst->ips_ip_mi_lock); 10009 if (err) { 10010 (void) ip_close(q, 0); 10011 return (err); 10012 } 10013 return (0); 10014 } 10015 10016 /* IP open routine. */ 10017 int 10018 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 10019 { 10020 conn_t *connp; 10021 major_t maj; 10022 zoneid_t zoneid; 10023 netstack_t *ns; 10024 ip_stack_t *ipst; 10025 10026 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 10027 10028 /* Allow reopen. */ 10029 if (q->q_ptr != NULL) 10030 return (0); 10031 10032 if (sflag & MODOPEN) { 10033 /* This is a module open */ 10034 return (ip_modopen(q, devp, flag, sflag, credp)); 10035 } 10036 10037 ns = netstack_find_by_cred(credp); 10038 ASSERT(ns != NULL); 10039 ipst = ns->netstack_ip; 10040 ASSERT(ipst != NULL); 10041 10042 /* 10043 * For exclusive stacks we set the zoneid to zero 10044 * to make IP operate as if in the global zone. 10045 */ 10046 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 10047 zoneid = GLOBAL_ZONEID; 10048 else 10049 zoneid = crgetzoneid(credp); 10050 10051 /* 10052 * We are opening as a device. This is an IP client stream, and we 10053 * allocate an conn_t as the instance data. 10054 */ 10055 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack); 10056 10057 /* 10058 * ipcl_conn_create did a netstack_hold. Undo the hold that was 10059 * done by netstack_find_by_cred() 10060 */ 10061 netstack_rele(ipst->ips_netstack); 10062 10063 connp->conn_zoneid = zoneid; 10064 10065 connp->conn_upq = q; 10066 q->q_ptr = WR(q)->q_ptr = connp; 10067 10068 if (flag & SO_SOCKSTR) 10069 connp->conn_flags |= IPCL_SOCKET; 10070 10071 /* Minor tells us which /dev entry was opened */ 10072 if (geteminor(*devp) == IPV6_MINOR) { 10073 connp->conn_flags |= IPCL_ISV6; 10074 connp->conn_af_isv6 = B_TRUE; 10075 ip_setqinfo(q, geteminor(*devp), B_FALSE, ipst); 10076 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 10077 } else { 10078 connp->conn_af_isv6 = B_FALSE; 10079 connp->conn_pkt_isv6 = B_FALSE; 10080 } 10081 10082 if ((connp->conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 10083 /* CONN_DEC_REF takes care of netstack_rele() */ 10084 q->q_ptr = WR(q)->q_ptr = NULL; 10085 CONN_DEC_REF(connp); 10086 return (EBUSY); 10087 } 10088 10089 maj = getemajor(*devp); 10090 *devp = makedevice(maj, (minor_t)connp->conn_dev); 10091 10092 /* 10093 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 10094 */ 10095 connp->conn_cred = credp; 10096 crhold(connp->conn_cred); 10097 10098 /* 10099 * If the caller has the process-wide flag set, then default to MAC 10100 * exempt mode. This allows read-down to unlabeled hosts. 10101 */ 10102 if (getpflags(NET_MAC_AWARE, credp) != 0) 10103 connp->conn_mac_exempt = B_TRUE; 10104 10105 /* 10106 * This should only happen for ndd, netstat, raw socket or other SCTP 10107 * administrative ops. In these cases, we just need a normal conn_t 10108 * with ulp set to IPPROTO_SCTP. All other ops are trapped and 10109 * an error will be returned. 10110 */ 10111 if (maj != SCTP_MAJ && maj != SCTP6_MAJ) { 10112 connp->conn_rq = q; 10113 connp->conn_wq = WR(q); 10114 } else { 10115 connp->conn_ulp = IPPROTO_SCTP; 10116 connp->conn_rq = connp->conn_wq = NULL; 10117 } 10118 /* Non-zero default values */ 10119 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 10120 10121 /* 10122 * Make the conn globally visible to walkers 10123 */ 10124 mutex_enter(&connp->conn_lock); 10125 connp->conn_state_flags &= ~CONN_INCIPIENT; 10126 mutex_exit(&connp->conn_lock); 10127 ASSERT(connp->conn_ref == 1); 10128 10129 qprocson(q); 10130 10131 return (0); 10132 } 10133 10134 /* 10135 * Change q_qinfo based on the value of isv6. 10136 * This can not called on an ill queue. 10137 * Note that there is no race since either q_qinfo works for conn queues - it 10138 * is just an optimization to enter the best wput routine directly. 10139 */ 10140 void 10141 ip_setqinfo(queue_t *q, minor_t minor, boolean_t bump_mib, ip_stack_t *ipst) 10142 { 10143 ASSERT(q->q_flag & QREADR); 10144 ASSERT(WR(q)->q_next == NULL); 10145 ASSERT(q->q_ptr != NULL); 10146 10147 if (minor == IPV6_MINOR) { 10148 if (bump_mib) { 10149 BUMP_MIB(&ipst->ips_ip6_mib, 10150 ipIfStatsOutSwitchIPVersion); 10151 } 10152 q->q_qinfo = &rinit_ipv6; 10153 WR(q)->q_qinfo = &winit_ipv6; 10154 (Q_TO_CONN(q))->conn_pkt_isv6 = B_TRUE; 10155 } else { 10156 if (bump_mib) { 10157 BUMP_MIB(&ipst->ips_ip_mib, 10158 ipIfStatsOutSwitchIPVersion); 10159 } 10160 q->q_qinfo = &iprinit; 10161 WR(q)->q_qinfo = &ipwinit; 10162 (Q_TO_CONN(q))->conn_pkt_isv6 = B_FALSE; 10163 } 10164 10165 } 10166 10167 /* 10168 * See if IPsec needs loading because of the options in mp. 10169 */ 10170 static boolean_t 10171 ipsec_opt_present(mblk_t *mp) 10172 { 10173 uint8_t *optcp, *next_optcp, *opt_endcp; 10174 struct opthdr *opt; 10175 struct T_opthdr *topt; 10176 int opthdr_len; 10177 t_uscalar_t optname, optlevel; 10178 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 10179 ipsec_req_t *ipsr; 10180 10181 /* 10182 * Walk through the mess, and find IP_SEC_OPT. If it's there, 10183 * return TRUE. 10184 */ 10185 10186 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 10187 opt_endcp = optcp + tor->OPT_length; 10188 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10189 opthdr_len = sizeof (struct T_opthdr); 10190 } else { /* O_OPTMGMT_REQ */ 10191 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 10192 opthdr_len = sizeof (struct opthdr); 10193 } 10194 for (; optcp < opt_endcp; optcp = next_optcp) { 10195 if (optcp + opthdr_len > opt_endcp) 10196 return (B_FALSE); /* Not enough option header. */ 10197 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10198 topt = (struct T_opthdr *)optcp; 10199 optlevel = topt->level; 10200 optname = topt->name; 10201 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 10202 } else { 10203 opt = (struct opthdr *)optcp; 10204 optlevel = opt->level; 10205 optname = opt->name; 10206 next_optcp = optcp + opthdr_len + 10207 _TPI_ALIGN_OPT(opt->len); 10208 } 10209 if ((next_optcp < optcp) || /* wraparound pointer space */ 10210 ((next_optcp >= opt_endcp) && /* last option bad len */ 10211 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 10212 return (B_FALSE); /* bad option buffer */ 10213 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 10214 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 10215 /* 10216 * Check to see if it's an all-bypass or all-zeroes 10217 * IPsec request. Don't bother loading IPsec if 10218 * the socket doesn't want to use it. (A good example 10219 * is a bypass request.) 10220 * 10221 * Basically, if any of the non-NEVER bits are set, 10222 * load IPsec. 10223 */ 10224 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 10225 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 10226 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 10227 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 10228 != 0) 10229 return (B_TRUE); 10230 } 10231 } 10232 return (B_FALSE); 10233 } 10234 10235 /* 10236 * If conn is is waiting for ipsec to finish loading, kick it. 10237 */ 10238 /* ARGSUSED */ 10239 static void 10240 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 10241 { 10242 t_scalar_t optreq_prim; 10243 mblk_t *mp; 10244 cred_t *cr; 10245 int err = 0; 10246 10247 /* 10248 * This function is called, after ipsec loading is complete. 10249 * Since IP checks exclusively and atomically (i.e it prevents 10250 * ipsec load from completing until ip_optcom_req completes) 10251 * whether ipsec load is complete, there cannot be a race with IP 10252 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 10253 */ 10254 mutex_enter(&connp->conn_lock); 10255 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 10256 ASSERT(connp->conn_ipsec_opt_mp != NULL); 10257 mp = connp->conn_ipsec_opt_mp; 10258 connp->conn_ipsec_opt_mp = NULL; 10259 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 10260 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); 10261 mutex_exit(&connp->conn_lock); 10262 10263 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 10264 10265 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 10266 if (optreq_prim == T_OPTMGMT_REQ) { 10267 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10268 &ip_opt_obj); 10269 } else { 10270 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 10271 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10272 &ip_opt_obj); 10273 } 10274 if (err != EINPROGRESS) 10275 CONN_OPER_PENDING_DONE(connp); 10276 return; 10277 } 10278 mutex_exit(&connp->conn_lock); 10279 } 10280 10281 /* 10282 * Called from the ipsec_loader thread, outside any perimeter, to tell 10283 * ip qenable any of the queues waiting for the ipsec loader to 10284 * complete. 10285 */ 10286 void 10287 ip_ipsec_load_complete(ipsec_stack_t *ipss) 10288 { 10289 netstack_t *ns = ipss->ipsec_netstack; 10290 10291 ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip); 10292 } 10293 10294 /* 10295 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 10296 * determines the grp on which it has to become exclusive, queues the mp 10297 * and sq draining restarts the optmgmt 10298 */ 10299 static boolean_t 10300 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 10301 { 10302 conn_t *connp = Q_TO_CONN(q); 10303 ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec; 10304 10305 /* 10306 * Take IPsec requests and treat them special. 10307 */ 10308 if (ipsec_opt_present(mp)) { 10309 /* First check if IPsec is loaded. */ 10310 mutex_enter(&ipss->ipsec_loader_lock); 10311 if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) { 10312 mutex_exit(&ipss->ipsec_loader_lock); 10313 return (B_FALSE); 10314 } 10315 mutex_enter(&connp->conn_lock); 10316 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 10317 10318 ASSERT(connp->conn_ipsec_opt_mp == NULL); 10319 connp->conn_ipsec_opt_mp = mp; 10320 mutex_exit(&connp->conn_lock); 10321 mutex_exit(&ipss->ipsec_loader_lock); 10322 10323 ipsec_loader_loadnow(ipss); 10324 return (B_TRUE); 10325 } 10326 return (B_FALSE); 10327 } 10328 10329 /* 10330 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 10331 * all of them are copied to the conn_t. If the req is "zero", the policy is 10332 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 10333 * fields. 10334 * We keep only the latest setting of the policy and thus policy setting 10335 * is not incremental/cumulative. 10336 * 10337 * Requests to set policies with multiple alternative actions will 10338 * go through a different API. 10339 */ 10340 int 10341 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 10342 { 10343 uint_t ah_req = 0; 10344 uint_t esp_req = 0; 10345 uint_t se_req = 0; 10346 ipsec_selkey_t sel; 10347 ipsec_act_t *actp = NULL; 10348 uint_t nact; 10349 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 10350 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 10351 ipsec_policy_root_t *pr; 10352 ipsec_policy_head_t *ph; 10353 int fam; 10354 boolean_t is_pol_reset; 10355 int error = 0; 10356 netstack_t *ns = connp->conn_netstack; 10357 ip_stack_t *ipst = ns->netstack_ip; 10358 ipsec_stack_t *ipss = ns->netstack_ipsec; 10359 10360 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 10361 10362 /* 10363 * The IP_SEC_OPT option does not allow variable length parameters, 10364 * hence a request cannot be NULL. 10365 */ 10366 if (req == NULL) 10367 return (EINVAL); 10368 10369 ah_req = req->ipsr_ah_req; 10370 esp_req = req->ipsr_esp_req; 10371 se_req = req->ipsr_self_encap_req; 10372 10373 /* 10374 * Are we dealing with a request to reset the policy (i.e. 10375 * zero requests). 10376 */ 10377 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 10378 (esp_req & REQ_MASK) == 0 && 10379 (se_req & REQ_MASK) == 0); 10380 10381 if (!is_pol_reset) { 10382 /* 10383 * If we couldn't load IPsec, fail with "protocol 10384 * not supported". 10385 * IPsec may not have been loaded for a request with zero 10386 * policies, so we don't fail in this case. 10387 */ 10388 mutex_enter(&ipss->ipsec_loader_lock); 10389 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 10390 mutex_exit(&ipss->ipsec_loader_lock); 10391 return (EPROTONOSUPPORT); 10392 } 10393 mutex_exit(&ipss->ipsec_loader_lock); 10394 10395 /* 10396 * Test for valid requests. Invalid algorithms 10397 * need to be tested by IPSEC code because new 10398 * algorithms can be added dynamically. 10399 */ 10400 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10401 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10402 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 10403 return (EINVAL); 10404 } 10405 10406 /* 10407 * Only privileged users can issue these 10408 * requests. 10409 */ 10410 if (((ah_req & IPSEC_PREF_NEVER) || 10411 (esp_req & IPSEC_PREF_NEVER) || 10412 (se_req & IPSEC_PREF_NEVER)) && 10413 secpolicy_ip_config(cr, B_FALSE) != 0) { 10414 return (EPERM); 10415 } 10416 10417 /* 10418 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 10419 * are mutually exclusive. 10420 */ 10421 if (((ah_req & REQ_MASK) == REQ_MASK) || 10422 ((esp_req & REQ_MASK) == REQ_MASK) || 10423 ((se_req & REQ_MASK) == REQ_MASK)) { 10424 /* Both of them are set */ 10425 return (EINVAL); 10426 } 10427 } 10428 10429 mutex_enter(&connp->conn_lock); 10430 10431 /* 10432 * If we have already cached policies in ip_bind_connected*(), don't 10433 * let them change now. We cache policies for connections 10434 * whose src,dst [addr, port] is known. 10435 */ 10436 if (connp->conn_policy_cached) { 10437 mutex_exit(&connp->conn_lock); 10438 return (EINVAL); 10439 } 10440 10441 /* 10442 * We have a zero policies, reset the connection policy if already 10443 * set. This will cause the connection to inherit the 10444 * global policy, if any. 10445 */ 10446 if (is_pol_reset) { 10447 if (connp->conn_policy != NULL) { 10448 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack); 10449 connp->conn_policy = NULL; 10450 } 10451 connp->conn_flags &= ~IPCL_CHECK_POLICY; 10452 connp->conn_in_enforce_policy = B_FALSE; 10453 connp->conn_out_enforce_policy = B_FALSE; 10454 mutex_exit(&connp->conn_lock); 10455 return (0); 10456 } 10457 10458 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy, 10459 ipst->ips_netstack); 10460 if (ph == NULL) 10461 goto enomem; 10462 10463 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack); 10464 if (actp == NULL) 10465 goto enomem; 10466 10467 /* 10468 * Always allocate IPv4 policy entries, since they can also 10469 * apply to ipv6 sockets being used in ipv4-compat mode. 10470 */ 10471 bzero(&sel, sizeof (sel)); 10472 sel.ipsl_valid = IPSL_IPV4; 10473 10474 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10475 ipst->ips_netstack); 10476 if (pin4 == NULL) 10477 goto enomem; 10478 10479 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10480 ipst->ips_netstack); 10481 if (pout4 == NULL) 10482 goto enomem; 10483 10484 if (connp->conn_pkt_isv6) { 10485 /* 10486 * We're looking at a v6 socket, also allocate the 10487 * v6-specific entries... 10488 */ 10489 sel.ipsl_valid = IPSL_IPV6; 10490 pin6 = ipsec_policy_create(&sel, actp, nact, 10491 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10492 if (pin6 == NULL) 10493 goto enomem; 10494 10495 pout6 = ipsec_policy_create(&sel, actp, nact, 10496 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10497 if (pout6 == NULL) 10498 goto enomem; 10499 10500 /* 10501 * .. and file them away in the right place. 10502 */ 10503 fam = IPSEC_AF_V6; 10504 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10505 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 10506 ipsec_insert_always(&ph->iph_rulebyid, pin6); 10507 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10508 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 10509 ipsec_insert_always(&ph->iph_rulebyid, pout6); 10510 } 10511 10512 ipsec_actvec_free(actp, nact); 10513 10514 /* 10515 * File the v4 policies. 10516 */ 10517 fam = IPSEC_AF_V4; 10518 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10519 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 10520 ipsec_insert_always(&ph->iph_rulebyid, pin4); 10521 10522 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10523 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 10524 ipsec_insert_always(&ph->iph_rulebyid, pout4); 10525 10526 /* 10527 * If the requests need security, set enforce_policy. 10528 * If the requests are IPSEC_PREF_NEVER, one should 10529 * still set conn_out_enforce_policy so that an ipsec_out 10530 * gets attached in ip_wput. This is needed so that 10531 * for connections that we don't cache policy in ip_bind, 10532 * if global policy matches in ip_wput_attach_policy, we 10533 * don't wrongly inherit global policy. Similarly, we need 10534 * to set conn_in_enforce_policy also so that we don't verify 10535 * policy wrongly. 10536 */ 10537 if ((ah_req & REQ_MASK) != 0 || 10538 (esp_req & REQ_MASK) != 0 || 10539 (se_req & REQ_MASK) != 0) { 10540 connp->conn_in_enforce_policy = B_TRUE; 10541 connp->conn_out_enforce_policy = B_TRUE; 10542 connp->conn_flags |= IPCL_CHECK_POLICY; 10543 } 10544 10545 mutex_exit(&connp->conn_lock); 10546 return (error); 10547 #undef REQ_MASK 10548 10549 /* 10550 * Common memory-allocation-failure exit path. 10551 */ 10552 enomem: 10553 mutex_exit(&connp->conn_lock); 10554 if (actp != NULL) 10555 ipsec_actvec_free(actp, nact); 10556 if (pin4 != NULL) 10557 IPPOL_REFRELE(pin4, ipst->ips_netstack); 10558 if (pout4 != NULL) 10559 IPPOL_REFRELE(pout4, ipst->ips_netstack); 10560 if (pin6 != NULL) 10561 IPPOL_REFRELE(pin6, ipst->ips_netstack); 10562 if (pout6 != NULL) 10563 IPPOL_REFRELE(pout6, ipst->ips_netstack); 10564 return (ENOMEM); 10565 } 10566 10567 /* 10568 * Only for options that pass in an IP addr. Currently only V4 options 10569 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 10570 * So this function assumes level is IPPROTO_IP 10571 */ 10572 int 10573 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 10574 mblk_t *first_mp) 10575 { 10576 ipif_t *ipif = NULL; 10577 int error; 10578 ill_t *ill; 10579 int zoneid; 10580 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10581 10582 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 10583 10584 if (addr != INADDR_ANY || checkonly) { 10585 ASSERT(connp != NULL); 10586 zoneid = IPCL_ZONEID(connp); 10587 if (option == IP_NEXTHOP) { 10588 ipif = ipif_lookup_onlink_addr(addr, 10589 connp->conn_zoneid, ipst); 10590 } else { 10591 ipif = ipif_lookup_addr(addr, NULL, zoneid, 10592 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 10593 &error, ipst); 10594 } 10595 if (ipif == NULL) { 10596 if (error == EINPROGRESS) 10597 return (error); 10598 else if ((option == IP_MULTICAST_IF) || 10599 (option == IP_NEXTHOP)) 10600 return (EHOSTUNREACH); 10601 else 10602 return (EINVAL); 10603 } else if (checkonly) { 10604 if (option == IP_MULTICAST_IF) { 10605 ill = ipif->ipif_ill; 10606 /* not supported by the virtual network iface */ 10607 if (IS_VNI(ill)) { 10608 ipif_refrele(ipif); 10609 return (EINVAL); 10610 } 10611 } 10612 ipif_refrele(ipif); 10613 return (0); 10614 } 10615 ill = ipif->ipif_ill; 10616 mutex_enter(&connp->conn_lock); 10617 mutex_enter(&ill->ill_lock); 10618 if ((ill->ill_state_flags & ILL_CONDEMNED) || 10619 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 10620 mutex_exit(&ill->ill_lock); 10621 mutex_exit(&connp->conn_lock); 10622 ipif_refrele(ipif); 10623 return (option == IP_MULTICAST_IF ? 10624 EHOSTUNREACH : EINVAL); 10625 } 10626 } else { 10627 mutex_enter(&connp->conn_lock); 10628 } 10629 10630 /* None of the options below are supported on the VNI */ 10631 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 10632 mutex_exit(&ill->ill_lock); 10633 mutex_exit(&connp->conn_lock); 10634 ipif_refrele(ipif); 10635 return (EINVAL); 10636 } 10637 10638 switch (option) { 10639 case IP_DONTFAILOVER_IF: 10640 /* 10641 * This option is used by in.mpathd to ensure 10642 * that IPMP probe packets only go out on the 10643 * test interfaces. in.mpathd sets this option 10644 * on the non-failover interfaces. 10645 * For backward compatibility, this option 10646 * implicitly sets IP_MULTICAST_IF, as used 10647 * be done in bind(), so that ip_wput gets 10648 * this ipif to send mcast packets. 10649 */ 10650 if (ipif != NULL) { 10651 ASSERT(addr != INADDR_ANY); 10652 connp->conn_nofailover_ill = ipif->ipif_ill; 10653 connp->conn_multicast_ipif = ipif; 10654 } else { 10655 ASSERT(addr == INADDR_ANY); 10656 connp->conn_nofailover_ill = NULL; 10657 connp->conn_multicast_ipif = NULL; 10658 } 10659 break; 10660 10661 case IP_MULTICAST_IF: 10662 connp->conn_multicast_ipif = ipif; 10663 break; 10664 case IP_NEXTHOP: 10665 connp->conn_nexthop_v4 = addr; 10666 connp->conn_nexthop_set = B_TRUE; 10667 break; 10668 } 10669 10670 if (ipif != NULL) { 10671 mutex_exit(&ill->ill_lock); 10672 mutex_exit(&connp->conn_lock); 10673 ipif_refrele(ipif); 10674 return (0); 10675 } 10676 mutex_exit(&connp->conn_lock); 10677 /* We succeded in cleared the option */ 10678 return (0); 10679 } 10680 10681 /* 10682 * For options that pass in an ifindex specifying the ill. V6 options always 10683 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 10684 */ 10685 int 10686 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 10687 int level, int option, mblk_t *first_mp) 10688 { 10689 ill_t *ill = NULL; 10690 int error = 0; 10691 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10692 10693 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 10694 if (ifindex != 0) { 10695 ASSERT(connp != NULL); 10696 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 10697 first_mp, ip_restart_optmgmt, &error, ipst); 10698 if (ill != NULL) { 10699 if (checkonly) { 10700 /* not supported by the virtual network iface */ 10701 if (IS_VNI(ill)) { 10702 ill_refrele(ill); 10703 return (EINVAL); 10704 } 10705 ill_refrele(ill); 10706 return (0); 10707 } 10708 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 10709 0, NULL)) { 10710 ill_refrele(ill); 10711 ill = NULL; 10712 mutex_enter(&connp->conn_lock); 10713 goto setit; 10714 } 10715 mutex_enter(&connp->conn_lock); 10716 mutex_enter(&ill->ill_lock); 10717 if (ill->ill_state_flags & ILL_CONDEMNED) { 10718 mutex_exit(&ill->ill_lock); 10719 mutex_exit(&connp->conn_lock); 10720 ill_refrele(ill); 10721 ill = NULL; 10722 mutex_enter(&connp->conn_lock); 10723 } 10724 goto setit; 10725 } else if (error == EINPROGRESS) { 10726 return (error); 10727 } else { 10728 error = 0; 10729 } 10730 } 10731 mutex_enter(&connp->conn_lock); 10732 setit: 10733 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 10734 10735 /* 10736 * The options below assume that the ILL (if any) transmits and/or 10737 * receives traffic. Neither of which is true for the virtual network 10738 * interface, so fail setting these on a VNI. 10739 */ 10740 if (IS_VNI(ill)) { 10741 ASSERT(ill != NULL); 10742 mutex_exit(&ill->ill_lock); 10743 mutex_exit(&connp->conn_lock); 10744 ill_refrele(ill); 10745 return (EINVAL); 10746 } 10747 10748 if (level == IPPROTO_IP) { 10749 switch (option) { 10750 case IP_BOUND_IF: 10751 connp->conn_incoming_ill = ill; 10752 connp->conn_outgoing_ill = ill; 10753 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10754 0 : ifindex; 10755 break; 10756 10757 case IP_XMIT_IF: 10758 /* 10759 * Similar to IP_BOUND_IF, but this only 10760 * determines the outgoing interface for 10761 * unicast packets. Also no IRE_CACHE entry 10762 * is added for the destination of the 10763 * outgoing packets. This feature is needed 10764 * for mobile IP. 10765 */ 10766 connp->conn_xmit_if_ill = ill; 10767 connp->conn_orig_xmit_ifindex = (ill == NULL) ? 10768 0 : ifindex; 10769 break; 10770 10771 case IP_MULTICAST_IF: 10772 /* 10773 * This option is an internal special. The socket 10774 * level IP_MULTICAST_IF specifies an 'ipaddr' and 10775 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 10776 * specifies an ifindex and we try first on V6 ill's. 10777 * If we don't find one, we they try using on v4 ill's 10778 * intenally and we come here. 10779 */ 10780 if (!checkonly && ill != NULL) { 10781 ipif_t *ipif; 10782 ipif = ill->ill_ipif; 10783 10784 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 10785 mutex_exit(&ill->ill_lock); 10786 mutex_exit(&connp->conn_lock); 10787 ill_refrele(ill); 10788 ill = NULL; 10789 mutex_enter(&connp->conn_lock); 10790 } else { 10791 connp->conn_multicast_ipif = ipif; 10792 } 10793 } 10794 break; 10795 } 10796 } else { 10797 switch (option) { 10798 case IPV6_BOUND_IF: 10799 connp->conn_incoming_ill = ill; 10800 connp->conn_outgoing_ill = ill; 10801 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10802 0 : ifindex; 10803 break; 10804 10805 case IPV6_BOUND_PIF: 10806 /* 10807 * Limit all transmit to this ill. 10808 * Unlike IPV6_BOUND_IF, using this option 10809 * prevents load spreading and failover from 10810 * happening when the interface is part of the 10811 * group. That's why we don't need to remember 10812 * the ifindex in orig_bound_ifindex as in 10813 * IPV6_BOUND_IF. 10814 */ 10815 connp->conn_outgoing_pill = ill; 10816 break; 10817 10818 case IPV6_DONTFAILOVER_IF: 10819 /* 10820 * This option is used by in.mpathd to ensure 10821 * that IPMP probe packets only go out on the 10822 * test interfaces. in.mpathd sets this option 10823 * on the non-failover interfaces. 10824 */ 10825 connp->conn_nofailover_ill = ill; 10826 /* 10827 * For backward compatibility, this option 10828 * implicitly sets ip_multicast_ill as used in 10829 * IP_MULTICAST_IF so that ip_wput gets 10830 * this ipif to send mcast packets. 10831 */ 10832 connp->conn_multicast_ill = ill; 10833 connp->conn_orig_multicast_ifindex = (ill == NULL) ? 10834 0 : ifindex; 10835 break; 10836 10837 case IPV6_MULTICAST_IF: 10838 /* 10839 * Set conn_multicast_ill to be the IPv6 ill. 10840 * Set conn_multicast_ipif to be an IPv4 ipif 10841 * for ifindex to make IPv4 mapped addresses 10842 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 10843 * Even if no IPv6 ill exists for the ifindex 10844 * we need to check for an IPv4 ifindex in order 10845 * for this to work with mapped addresses. In that 10846 * case only set conn_multicast_ipif. 10847 */ 10848 if (!checkonly) { 10849 if (ifindex == 0) { 10850 connp->conn_multicast_ill = NULL; 10851 connp->conn_orig_multicast_ifindex = 0; 10852 connp->conn_multicast_ipif = NULL; 10853 } else if (ill != NULL) { 10854 connp->conn_multicast_ill = ill; 10855 connp->conn_orig_multicast_ifindex = 10856 ifindex; 10857 } 10858 } 10859 break; 10860 } 10861 } 10862 10863 if (ill != NULL) { 10864 mutex_exit(&ill->ill_lock); 10865 mutex_exit(&connp->conn_lock); 10866 ill_refrele(ill); 10867 return (0); 10868 } 10869 mutex_exit(&connp->conn_lock); 10870 /* 10871 * We succeeded in clearing the option (ifindex == 0) or failed to 10872 * locate the ill and could not set the option (ifindex != 0) 10873 */ 10874 return (ifindex == 0 ? 0 : EINVAL); 10875 } 10876 10877 /* This routine sets socket options. */ 10878 /* ARGSUSED */ 10879 int 10880 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10881 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10882 void *dummy, cred_t *cr, mblk_t *first_mp) 10883 { 10884 int *i1 = (int *)invalp; 10885 conn_t *connp = Q_TO_CONN(q); 10886 int error = 0; 10887 boolean_t checkonly; 10888 ire_t *ire; 10889 boolean_t found; 10890 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10891 10892 switch (optset_context) { 10893 10894 case SETFN_OPTCOM_CHECKONLY: 10895 checkonly = B_TRUE; 10896 /* 10897 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10898 * inlen != 0 implies value supplied and 10899 * we have to "pretend" to set it. 10900 * inlen == 0 implies that there is no 10901 * value part in T_CHECK request and just validation 10902 * done elsewhere should be enough, we just return here. 10903 */ 10904 if (inlen == 0) { 10905 *outlenp = 0; 10906 return (0); 10907 } 10908 break; 10909 case SETFN_OPTCOM_NEGOTIATE: 10910 case SETFN_UD_NEGOTIATE: 10911 case SETFN_CONN_NEGOTIATE: 10912 checkonly = B_FALSE; 10913 break; 10914 default: 10915 /* 10916 * We should never get here 10917 */ 10918 *outlenp = 0; 10919 return (EINVAL); 10920 } 10921 10922 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10923 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10924 10925 /* 10926 * For fixed length options, no sanity check 10927 * of passed in length is done. It is assumed *_optcom_req() 10928 * routines do the right thing. 10929 */ 10930 10931 switch (level) { 10932 case SOL_SOCKET: 10933 /* 10934 * conn_lock protects the bitfields, and is used to 10935 * set the fields atomically. 10936 */ 10937 switch (name) { 10938 case SO_BROADCAST: 10939 if (!checkonly) { 10940 /* TODO: use value someplace? */ 10941 mutex_enter(&connp->conn_lock); 10942 connp->conn_broadcast = *i1 ? 1 : 0; 10943 mutex_exit(&connp->conn_lock); 10944 } 10945 break; /* goto sizeof (int) option return */ 10946 case SO_USELOOPBACK: 10947 if (!checkonly) { 10948 /* TODO: use value someplace? */ 10949 mutex_enter(&connp->conn_lock); 10950 connp->conn_loopback = *i1 ? 1 : 0; 10951 mutex_exit(&connp->conn_lock); 10952 } 10953 break; /* goto sizeof (int) option return */ 10954 case SO_DONTROUTE: 10955 if (!checkonly) { 10956 mutex_enter(&connp->conn_lock); 10957 connp->conn_dontroute = *i1 ? 1 : 0; 10958 mutex_exit(&connp->conn_lock); 10959 } 10960 break; /* goto sizeof (int) option return */ 10961 case SO_REUSEADDR: 10962 if (!checkonly) { 10963 mutex_enter(&connp->conn_lock); 10964 connp->conn_reuseaddr = *i1 ? 1 : 0; 10965 mutex_exit(&connp->conn_lock); 10966 } 10967 break; /* goto sizeof (int) option return */ 10968 case SO_PROTOTYPE: 10969 if (!checkonly) { 10970 mutex_enter(&connp->conn_lock); 10971 connp->conn_proto = *i1; 10972 mutex_exit(&connp->conn_lock); 10973 } 10974 break; /* goto sizeof (int) option return */ 10975 case SO_ALLZONES: 10976 if (!checkonly) { 10977 mutex_enter(&connp->conn_lock); 10978 if (IPCL_IS_BOUND(connp)) { 10979 mutex_exit(&connp->conn_lock); 10980 return (EINVAL); 10981 } 10982 connp->conn_allzones = *i1 != 0 ? 1 : 0; 10983 mutex_exit(&connp->conn_lock); 10984 } 10985 break; /* goto sizeof (int) option return */ 10986 case SO_ANON_MLP: 10987 if (!checkonly) { 10988 mutex_enter(&connp->conn_lock); 10989 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; 10990 mutex_exit(&connp->conn_lock); 10991 } 10992 break; /* goto sizeof (int) option return */ 10993 case SO_MAC_EXEMPT: 10994 if (secpolicy_net_mac_aware(cr) != 0 || 10995 IPCL_IS_BOUND(connp)) 10996 return (EACCES); 10997 if (!checkonly) { 10998 mutex_enter(&connp->conn_lock); 10999 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; 11000 mutex_exit(&connp->conn_lock); 11001 } 11002 break; /* goto sizeof (int) option return */ 11003 default: 11004 /* 11005 * "soft" error (negative) 11006 * option not handled at this level 11007 * Note: Do not modify *outlenp 11008 */ 11009 return (-EINVAL); 11010 } 11011 break; 11012 case IPPROTO_IP: 11013 switch (name) { 11014 case IP_NEXTHOP: 11015 if (secpolicy_ip_config(cr, B_FALSE) != 0) 11016 return (EPERM); 11017 /* FALLTHRU */ 11018 case IP_MULTICAST_IF: 11019 case IP_DONTFAILOVER_IF: { 11020 ipaddr_t addr = *i1; 11021 11022 error = ip_opt_set_ipif(connp, addr, checkonly, name, 11023 first_mp); 11024 if (error != 0) 11025 return (error); 11026 break; /* goto sizeof (int) option return */ 11027 } 11028 11029 case IP_MULTICAST_TTL: 11030 /* Recorded in transport above IP */ 11031 *outvalp = *invalp; 11032 *outlenp = sizeof (uchar_t); 11033 return (0); 11034 case IP_MULTICAST_LOOP: 11035 if (!checkonly) { 11036 mutex_enter(&connp->conn_lock); 11037 connp->conn_multicast_loop = *invalp ? 1 : 0; 11038 mutex_exit(&connp->conn_lock); 11039 } 11040 *outvalp = *invalp; 11041 *outlenp = sizeof (uchar_t); 11042 return (0); 11043 case IP_ADD_MEMBERSHIP: 11044 case MCAST_JOIN_GROUP: 11045 case IP_DROP_MEMBERSHIP: 11046 case MCAST_LEAVE_GROUP: { 11047 struct ip_mreq *mreqp; 11048 struct group_req *greqp; 11049 ire_t *ire; 11050 boolean_t done = B_FALSE; 11051 ipaddr_t group, ifaddr; 11052 struct sockaddr_in *sin; 11053 uint32_t *ifindexp; 11054 boolean_t mcast_opt = B_TRUE; 11055 mcast_record_t fmode; 11056 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 11057 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 11058 11059 switch (name) { 11060 case IP_ADD_MEMBERSHIP: 11061 mcast_opt = B_FALSE; 11062 /* FALLTHRU */ 11063 case MCAST_JOIN_GROUP: 11064 fmode = MODE_IS_EXCLUDE; 11065 optfn = ip_opt_add_group; 11066 break; 11067 11068 case IP_DROP_MEMBERSHIP: 11069 mcast_opt = B_FALSE; 11070 /* FALLTHRU */ 11071 case MCAST_LEAVE_GROUP: 11072 fmode = MODE_IS_INCLUDE; 11073 optfn = ip_opt_delete_group; 11074 break; 11075 } 11076 11077 if (mcast_opt) { 11078 greqp = (struct group_req *)i1; 11079 sin = (struct sockaddr_in *)&greqp->gr_group; 11080 if (sin->sin_family != AF_INET) { 11081 *outlenp = 0; 11082 return (ENOPROTOOPT); 11083 } 11084 group = (ipaddr_t)sin->sin_addr.s_addr; 11085 ifaddr = INADDR_ANY; 11086 ifindexp = &greqp->gr_interface; 11087 } else { 11088 mreqp = (struct ip_mreq *)i1; 11089 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 11090 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 11091 ifindexp = NULL; 11092 } 11093 11094 /* 11095 * In the multirouting case, we need to replicate 11096 * the request on all interfaces that will take part 11097 * in replication. We do so because multirouting is 11098 * reflective, thus we will probably receive multi- 11099 * casts on those interfaces. 11100 * The ip_multirt_apply_membership() succeeds if the 11101 * operation succeeds on at least one interface. 11102 */ 11103 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 11104 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11105 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11106 if (ire != NULL) { 11107 if (ire->ire_flags & RTF_MULTIRT) { 11108 error = ip_multirt_apply_membership( 11109 optfn, ire, connp, checkonly, group, 11110 fmode, INADDR_ANY, first_mp); 11111 done = B_TRUE; 11112 } 11113 ire_refrele(ire); 11114 } 11115 if (!done) { 11116 error = optfn(connp, checkonly, group, ifaddr, 11117 ifindexp, fmode, INADDR_ANY, first_mp); 11118 } 11119 if (error) { 11120 /* 11121 * EINPROGRESS is a soft error, needs retry 11122 * so don't make *outlenp zero. 11123 */ 11124 if (error != EINPROGRESS) 11125 *outlenp = 0; 11126 return (error); 11127 } 11128 /* OK return - copy input buffer into output buffer */ 11129 if (invalp != outvalp) { 11130 /* don't trust bcopy for identical src/dst */ 11131 bcopy(invalp, outvalp, inlen); 11132 } 11133 *outlenp = inlen; 11134 return (0); 11135 } 11136 case IP_BLOCK_SOURCE: 11137 case IP_UNBLOCK_SOURCE: 11138 case IP_ADD_SOURCE_MEMBERSHIP: 11139 case IP_DROP_SOURCE_MEMBERSHIP: 11140 case MCAST_BLOCK_SOURCE: 11141 case MCAST_UNBLOCK_SOURCE: 11142 case MCAST_JOIN_SOURCE_GROUP: 11143 case MCAST_LEAVE_SOURCE_GROUP: { 11144 struct ip_mreq_source *imreqp; 11145 struct group_source_req *gsreqp; 11146 in_addr_t grp, src, ifaddr = INADDR_ANY; 11147 uint32_t ifindex = 0; 11148 mcast_record_t fmode; 11149 struct sockaddr_in *sin; 11150 ire_t *ire; 11151 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 11152 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 11153 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 11154 11155 switch (name) { 11156 case IP_BLOCK_SOURCE: 11157 mcast_opt = B_FALSE; 11158 /* FALLTHRU */ 11159 case MCAST_BLOCK_SOURCE: 11160 fmode = MODE_IS_EXCLUDE; 11161 optfn = ip_opt_add_group; 11162 break; 11163 11164 case IP_UNBLOCK_SOURCE: 11165 mcast_opt = B_FALSE; 11166 /* FALLTHRU */ 11167 case MCAST_UNBLOCK_SOURCE: 11168 fmode = MODE_IS_EXCLUDE; 11169 optfn = ip_opt_delete_group; 11170 break; 11171 11172 case IP_ADD_SOURCE_MEMBERSHIP: 11173 mcast_opt = B_FALSE; 11174 /* FALLTHRU */ 11175 case MCAST_JOIN_SOURCE_GROUP: 11176 fmode = MODE_IS_INCLUDE; 11177 optfn = ip_opt_add_group; 11178 break; 11179 11180 case IP_DROP_SOURCE_MEMBERSHIP: 11181 mcast_opt = B_FALSE; 11182 /* FALLTHRU */ 11183 case MCAST_LEAVE_SOURCE_GROUP: 11184 fmode = MODE_IS_INCLUDE; 11185 optfn = ip_opt_delete_group; 11186 break; 11187 } 11188 11189 if (mcast_opt) { 11190 gsreqp = (struct group_source_req *)i1; 11191 if (gsreqp->gsr_group.ss_family != AF_INET) { 11192 *outlenp = 0; 11193 return (ENOPROTOOPT); 11194 } 11195 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 11196 grp = (ipaddr_t)sin->sin_addr.s_addr; 11197 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 11198 src = (ipaddr_t)sin->sin_addr.s_addr; 11199 ifindex = gsreqp->gsr_interface; 11200 } else { 11201 imreqp = (struct ip_mreq_source *)i1; 11202 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 11203 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 11204 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 11205 } 11206 11207 /* 11208 * In the multirouting case, we need to replicate 11209 * the request as noted in the mcast cases above. 11210 */ 11211 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 11212 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11213 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11214 if (ire != NULL) { 11215 if (ire->ire_flags & RTF_MULTIRT) { 11216 error = ip_multirt_apply_membership( 11217 optfn, ire, connp, checkonly, grp, 11218 fmode, src, first_mp); 11219 done = B_TRUE; 11220 } 11221 ire_refrele(ire); 11222 } 11223 if (!done) { 11224 error = optfn(connp, checkonly, grp, ifaddr, 11225 &ifindex, fmode, src, first_mp); 11226 } 11227 if (error != 0) { 11228 /* 11229 * EINPROGRESS is a soft error, needs retry 11230 * so don't make *outlenp zero. 11231 */ 11232 if (error != EINPROGRESS) 11233 *outlenp = 0; 11234 return (error); 11235 } 11236 /* OK return - copy input buffer into output buffer */ 11237 if (invalp != outvalp) { 11238 bcopy(invalp, outvalp, inlen); 11239 } 11240 *outlenp = inlen; 11241 return (0); 11242 } 11243 case IP_SEC_OPT: 11244 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11245 if (error != 0) { 11246 *outlenp = 0; 11247 return (error); 11248 } 11249 break; 11250 case IP_HDRINCL: 11251 case IP_OPTIONS: 11252 case T_IP_OPTIONS: 11253 case IP_TOS: 11254 case T_IP_TOS: 11255 case IP_TTL: 11256 case IP_RECVDSTADDR: 11257 case IP_RECVOPTS: 11258 /* OK return - copy input buffer into output buffer */ 11259 if (invalp != outvalp) { 11260 /* don't trust bcopy for identical src/dst */ 11261 bcopy(invalp, outvalp, inlen); 11262 } 11263 *outlenp = inlen; 11264 return (0); 11265 case IP_RECVIF: 11266 /* Retrieve the inbound interface index */ 11267 if (!checkonly) { 11268 mutex_enter(&connp->conn_lock); 11269 connp->conn_recvif = *i1 ? 1 : 0; 11270 mutex_exit(&connp->conn_lock); 11271 } 11272 break; /* goto sizeof (int) option return */ 11273 case IP_RECVPKTINFO: 11274 if (!checkonly) { 11275 mutex_enter(&connp->conn_lock); 11276 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11277 mutex_exit(&connp->conn_lock); 11278 } 11279 break; /* goto sizeof (int) option return */ 11280 case IP_RECVSLLA: 11281 /* Retrieve the source link layer address */ 11282 if (!checkonly) { 11283 mutex_enter(&connp->conn_lock); 11284 connp->conn_recvslla = *i1 ? 1 : 0; 11285 mutex_exit(&connp->conn_lock); 11286 } 11287 break; /* goto sizeof (int) option return */ 11288 case MRT_INIT: 11289 case MRT_DONE: 11290 case MRT_ADD_VIF: 11291 case MRT_DEL_VIF: 11292 case MRT_ADD_MFC: 11293 case MRT_DEL_MFC: 11294 case MRT_ASSERT: 11295 if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) { 11296 *outlenp = 0; 11297 return (error); 11298 } 11299 error = ip_mrouter_set((int)name, q, checkonly, 11300 (uchar_t *)invalp, inlen, first_mp); 11301 if (error) { 11302 *outlenp = 0; 11303 return (error); 11304 } 11305 /* OK return - copy input buffer into output buffer */ 11306 if (invalp != outvalp) { 11307 /* don't trust bcopy for identical src/dst */ 11308 bcopy(invalp, outvalp, inlen); 11309 } 11310 *outlenp = inlen; 11311 return (0); 11312 case IP_BOUND_IF: 11313 case IP_XMIT_IF: 11314 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11315 level, name, first_mp); 11316 if (error != 0) 11317 return (error); 11318 break; /* goto sizeof (int) option return */ 11319 11320 case IP_UNSPEC_SRC: 11321 /* Allow sending with a zero source address */ 11322 if (!checkonly) { 11323 mutex_enter(&connp->conn_lock); 11324 connp->conn_unspec_src = *i1 ? 1 : 0; 11325 mutex_exit(&connp->conn_lock); 11326 } 11327 break; /* goto sizeof (int) option return */ 11328 default: 11329 /* 11330 * "soft" error (negative) 11331 * option not handled at this level 11332 * Note: Do not modify *outlenp 11333 */ 11334 return (-EINVAL); 11335 } 11336 break; 11337 case IPPROTO_IPV6: 11338 switch (name) { 11339 case IPV6_BOUND_IF: 11340 case IPV6_BOUND_PIF: 11341 case IPV6_DONTFAILOVER_IF: 11342 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11343 level, name, first_mp); 11344 if (error != 0) 11345 return (error); 11346 break; /* goto sizeof (int) option return */ 11347 11348 case IPV6_MULTICAST_IF: 11349 /* 11350 * The only possible errors are EINPROGRESS and 11351 * EINVAL. EINPROGRESS will be restarted and is not 11352 * a hard error. We call this option on both V4 and V6 11353 * If both return EINVAL, then this call returns 11354 * EINVAL. If at least one of them succeeds we 11355 * return success. 11356 */ 11357 found = B_FALSE; 11358 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11359 level, name, first_mp); 11360 if (error == EINPROGRESS) 11361 return (error); 11362 if (error == 0) 11363 found = B_TRUE; 11364 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11365 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 11366 if (error == 0) 11367 found = B_TRUE; 11368 if (!found) 11369 return (error); 11370 break; /* goto sizeof (int) option return */ 11371 11372 case IPV6_MULTICAST_HOPS: 11373 /* Recorded in transport above IP */ 11374 break; /* goto sizeof (int) option return */ 11375 case IPV6_MULTICAST_LOOP: 11376 if (!checkonly) { 11377 mutex_enter(&connp->conn_lock); 11378 connp->conn_multicast_loop = *i1; 11379 mutex_exit(&connp->conn_lock); 11380 } 11381 break; /* goto sizeof (int) option return */ 11382 case IPV6_JOIN_GROUP: 11383 case MCAST_JOIN_GROUP: 11384 case IPV6_LEAVE_GROUP: 11385 case MCAST_LEAVE_GROUP: { 11386 struct ipv6_mreq *ip_mreqp; 11387 struct group_req *greqp; 11388 ire_t *ire; 11389 boolean_t done = B_FALSE; 11390 in6_addr_t groupv6; 11391 uint32_t ifindex; 11392 boolean_t mcast_opt = B_TRUE; 11393 mcast_record_t fmode; 11394 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11395 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11396 11397 switch (name) { 11398 case IPV6_JOIN_GROUP: 11399 mcast_opt = B_FALSE; 11400 /* FALLTHRU */ 11401 case MCAST_JOIN_GROUP: 11402 fmode = MODE_IS_EXCLUDE; 11403 optfn = ip_opt_add_group_v6; 11404 break; 11405 11406 case IPV6_LEAVE_GROUP: 11407 mcast_opt = B_FALSE; 11408 /* FALLTHRU */ 11409 case MCAST_LEAVE_GROUP: 11410 fmode = MODE_IS_INCLUDE; 11411 optfn = ip_opt_delete_group_v6; 11412 break; 11413 } 11414 11415 if (mcast_opt) { 11416 struct sockaddr_in *sin; 11417 struct sockaddr_in6 *sin6; 11418 greqp = (struct group_req *)i1; 11419 if (greqp->gr_group.ss_family == AF_INET) { 11420 sin = (struct sockaddr_in *) 11421 &(greqp->gr_group); 11422 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 11423 &groupv6); 11424 } else { 11425 sin6 = (struct sockaddr_in6 *) 11426 &(greqp->gr_group); 11427 groupv6 = sin6->sin6_addr; 11428 } 11429 ifindex = greqp->gr_interface; 11430 } else { 11431 ip_mreqp = (struct ipv6_mreq *)i1; 11432 groupv6 = ip_mreqp->ipv6mr_multiaddr; 11433 ifindex = ip_mreqp->ipv6mr_interface; 11434 } 11435 /* 11436 * In the multirouting case, we need to replicate 11437 * the request on all interfaces that will take part 11438 * in replication. We do so because multirouting is 11439 * reflective, thus we will probably receive multi- 11440 * casts on those interfaces. 11441 * The ip_multirt_apply_membership_v6() succeeds if 11442 * the operation succeeds on at least one interface. 11443 */ 11444 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 11445 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11446 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11447 if (ire != NULL) { 11448 if (ire->ire_flags & RTF_MULTIRT) { 11449 error = ip_multirt_apply_membership_v6( 11450 optfn, ire, connp, checkonly, 11451 &groupv6, fmode, &ipv6_all_zeros, 11452 first_mp); 11453 done = B_TRUE; 11454 } 11455 ire_refrele(ire); 11456 } 11457 if (!done) { 11458 error = optfn(connp, checkonly, &groupv6, 11459 ifindex, fmode, &ipv6_all_zeros, first_mp); 11460 } 11461 if (error) { 11462 /* 11463 * EINPROGRESS is a soft error, needs retry 11464 * so don't make *outlenp zero. 11465 */ 11466 if (error != EINPROGRESS) 11467 *outlenp = 0; 11468 return (error); 11469 } 11470 /* OK return - copy input buffer into output buffer */ 11471 if (invalp != outvalp) { 11472 /* don't trust bcopy for identical src/dst */ 11473 bcopy(invalp, outvalp, inlen); 11474 } 11475 *outlenp = inlen; 11476 return (0); 11477 } 11478 case MCAST_BLOCK_SOURCE: 11479 case MCAST_UNBLOCK_SOURCE: 11480 case MCAST_JOIN_SOURCE_GROUP: 11481 case MCAST_LEAVE_SOURCE_GROUP: { 11482 struct group_source_req *gsreqp; 11483 in6_addr_t v6grp, v6src; 11484 uint32_t ifindex; 11485 mcast_record_t fmode; 11486 ire_t *ire; 11487 boolean_t done = B_FALSE; 11488 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11489 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11490 11491 switch (name) { 11492 case MCAST_BLOCK_SOURCE: 11493 fmode = MODE_IS_EXCLUDE; 11494 optfn = ip_opt_add_group_v6; 11495 break; 11496 case MCAST_UNBLOCK_SOURCE: 11497 fmode = MODE_IS_EXCLUDE; 11498 optfn = ip_opt_delete_group_v6; 11499 break; 11500 case MCAST_JOIN_SOURCE_GROUP: 11501 fmode = MODE_IS_INCLUDE; 11502 optfn = ip_opt_add_group_v6; 11503 break; 11504 case MCAST_LEAVE_SOURCE_GROUP: 11505 fmode = MODE_IS_INCLUDE; 11506 optfn = ip_opt_delete_group_v6; 11507 break; 11508 } 11509 11510 gsreqp = (struct group_source_req *)i1; 11511 ifindex = gsreqp->gsr_interface; 11512 if (gsreqp->gsr_group.ss_family == AF_INET) { 11513 struct sockaddr_in *s; 11514 s = (struct sockaddr_in *)&gsreqp->gsr_group; 11515 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 11516 s = (struct sockaddr_in *)&gsreqp->gsr_source; 11517 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 11518 } else { 11519 struct sockaddr_in6 *s6; 11520 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 11521 v6grp = s6->sin6_addr; 11522 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 11523 v6src = s6->sin6_addr; 11524 } 11525 11526 /* 11527 * In the multirouting case, we need to replicate 11528 * the request as noted in the mcast cases above. 11529 */ 11530 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 11531 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11532 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11533 if (ire != NULL) { 11534 if (ire->ire_flags & RTF_MULTIRT) { 11535 error = ip_multirt_apply_membership_v6( 11536 optfn, ire, connp, checkonly, 11537 &v6grp, fmode, &v6src, first_mp); 11538 done = B_TRUE; 11539 } 11540 ire_refrele(ire); 11541 } 11542 if (!done) { 11543 error = optfn(connp, checkonly, &v6grp, 11544 ifindex, fmode, &v6src, first_mp); 11545 } 11546 if (error != 0) { 11547 /* 11548 * EINPROGRESS is a soft error, needs retry 11549 * so don't make *outlenp zero. 11550 */ 11551 if (error != EINPROGRESS) 11552 *outlenp = 0; 11553 return (error); 11554 } 11555 /* OK return - copy input buffer into output buffer */ 11556 if (invalp != outvalp) { 11557 bcopy(invalp, outvalp, inlen); 11558 } 11559 *outlenp = inlen; 11560 return (0); 11561 } 11562 case IPV6_UNICAST_HOPS: 11563 /* Recorded in transport above IP */ 11564 break; /* goto sizeof (int) option return */ 11565 case IPV6_UNSPEC_SRC: 11566 /* Allow sending with a zero source address */ 11567 if (!checkonly) { 11568 mutex_enter(&connp->conn_lock); 11569 connp->conn_unspec_src = *i1 ? 1 : 0; 11570 mutex_exit(&connp->conn_lock); 11571 } 11572 break; /* goto sizeof (int) option return */ 11573 case IPV6_RECVPKTINFO: 11574 if (!checkonly) { 11575 mutex_enter(&connp->conn_lock); 11576 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11577 mutex_exit(&connp->conn_lock); 11578 } 11579 break; /* goto sizeof (int) option return */ 11580 case IPV6_RECVTCLASS: 11581 if (!checkonly) { 11582 if (*i1 < 0 || *i1 > 1) { 11583 return (EINVAL); 11584 } 11585 mutex_enter(&connp->conn_lock); 11586 connp->conn_ipv6_recvtclass = *i1; 11587 mutex_exit(&connp->conn_lock); 11588 } 11589 break; 11590 case IPV6_RECVPATHMTU: 11591 if (!checkonly) { 11592 if (*i1 < 0 || *i1 > 1) { 11593 return (EINVAL); 11594 } 11595 mutex_enter(&connp->conn_lock); 11596 connp->conn_ipv6_recvpathmtu = *i1; 11597 mutex_exit(&connp->conn_lock); 11598 } 11599 break; 11600 case IPV6_RECVHOPLIMIT: 11601 if (!checkonly) { 11602 mutex_enter(&connp->conn_lock); 11603 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 11604 mutex_exit(&connp->conn_lock); 11605 } 11606 break; /* goto sizeof (int) option return */ 11607 case IPV6_RECVHOPOPTS: 11608 if (!checkonly) { 11609 mutex_enter(&connp->conn_lock); 11610 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 11611 mutex_exit(&connp->conn_lock); 11612 } 11613 break; /* goto sizeof (int) option return */ 11614 case IPV6_RECVDSTOPTS: 11615 if (!checkonly) { 11616 mutex_enter(&connp->conn_lock); 11617 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 11618 mutex_exit(&connp->conn_lock); 11619 } 11620 break; /* goto sizeof (int) option return */ 11621 case IPV6_RECVRTHDR: 11622 if (!checkonly) { 11623 mutex_enter(&connp->conn_lock); 11624 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 11625 mutex_exit(&connp->conn_lock); 11626 } 11627 break; /* goto sizeof (int) option return */ 11628 case IPV6_RECVRTHDRDSTOPTS: 11629 if (!checkonly) { 11630 mutex_enter(&connp->conn_lock); 11631 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 11632 mutex_exit(&connp->conn_lock); 11633 } 11634 break; /* goto sizeof (int) option return */ 11635 case IPV6_PKTINFO: 11636 if (inlen == 0) 11637 return (-EINVAL); /* clearing option */ 11638 error = ip6_set_pktinfo(cr, connp, 11639 (struct in6_pktinfo *)invalp, first_mp); 11640 if (error != 0) 11641 *outlenp = 0; 11642 else 11643 *outlenp = inlen; 11644 return (error); 11645 case IPV6_NEXTHOP: { 11646 struct sockaddr_in6 *sin6; 11647 11648 /* Verify that the nexthop is reachable */ 11649 if (inlen == 0) 11650 return (-EINVAL); /* clearing option */ 11651 11652 sin6 = (struct sockaddr_in6 *)invalp; 11653 ire = ire_route_lookup_v6(&sin6->sin6_addr, 11654 0, 0, 0, NULL, NULL, connp->conn_zoneid, 11655 NULL, MATCH_IRE_DEFAULT, ipst); 11656 11657 if (ire == NULL) { 11658 *outlenp = 0; 11659 return (EHOSTUNREACH); 11660 } 11661 ire_refrele(ire); 11662 return (-EINVAL); 11663 } 11664 case IPV6_SEC_OPT: 11665 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11666 if (error != 0) { 11667 *outlenp = 0; 11668 return (error); 11669 } 11670 break; 11671 case IPV6_SRC_PREFERENCES: { 11672 /* 11673 * This is implemented strictly in the ip module 11674 * (here and in tcp_opt_*() to accomodate tcp 11675 * sockets). Modules above ip pass this option 11676 * down here since ip is the only one that needs to 11677 * be aware of source address preferences. 11678 * 11679 * This socket option only affects connected 11680 * sockets that haven't already bound to a specific 11681 * IPv6 address. In other words, sockets that 11682 * don't call bind() with an address other than the 11683 * unspecified address and that call connect(). 11684 * ip_bind_connected_v6() passes these preferences 11685 * to the ipif_select_source_v6() function. 11686 */ 11687 if (inlen != sizeof (uint32_t)) 11688 return (EINVAL); 11689 error = ip6_set_src_preferences(connp, 11690 *(uint32_t *)invalp); 11691 if (error != 0) { 11692 *outlenp = 0; 11693 return (error); 11694 } else { 11695 *outlenp = sizeof (uint32_t); 11696 } 11697 break; 11698 } 11699 case IPV6_V6ONLY: 11700 if (*i1 < 0 || *i1 > 1) { 11701 return (EINVAL); 11702 } 11703 mutex_enter(&connp->conn_lock); 11704 connp->conn_ipv6_v6only = *i1; 11705 mutex_exit(&connp->conn_lock); 11706 break; 11707 default: 11708 return (-EINVAL); 11709 } 11710 break; 11711 default: 11712 /* 11713 * "soft" error (negative) 11714 * option not handled at this level 11715 * Note: Do not modify *outlenp 11716 */ 11717 return (-EINVAL); 11718 } 11719 /* 11720 * Common case of return from an option that is sizeof (int) 11721 */ 11722 *(int *)outvalp = *i1; 11723 *outlenp = sizeof (int); 11724 return (0); 11725 } 11726 11727 /* 11728 * This routine gets default values of certain options whose default 11729 * values are maintained by protocol specific code 11730 */ 11731 /* ARGSUSED */ 11732 int 11733 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 11734 { 11735 int *i1 = (int *)ptr; 11736 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11737 11738 switch (level) { 11739 case IPPROTO_IP: 11740 switch (name) { 11741 case IP_MULTICAST_TTL: 11742 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 11743 return (sizeof (uchar_t)); 11744 case IP_MULTICAST_LOOP: 11745 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 11746 return (sizeof (uchar_t)); 11747 default: 11748 return (-1); 11749 } 11750 case IPPROTO_IPV6: 11751 switch (name) { 11752 case IPV6_UNICAST_HOPS: 11753 *i1 = ipst->ips_ipv6_def_hops; 11754 return (sizeof (int)); 11755 case IPV6_MULTICAST_HOPS: 11756 *i1 = IP_DEFAULT_MULTICAST_TTL; 11757 return (sizeof (int)); 11758 case IPV6_MULTICAST_LOOP: 11759 *i1 = IP_DEFAULT_MULTICAST_LOOP; 11760 return (sizeof (int)); 11761 case IPV6_V6ONLY: 11762 *i1 = 1; 11763 return (sizeof (int)); 11764 default: 11765 return (-1); 11766 } 11767 default: 11768 return (-1); 11769 } 11770 /* NOTREACHED */ 11771 } 11772 11773 /* 11774 * Given a destination address and a pointer to where to put the information 11775 * this routine fills in the mtuinfo. 11776 */ 11777 int 11778 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 11779 struct ip6_mtuinfo *mtuinfo, netstack_t *ns) 11780 { 11781 ire_t *ire; 11782 ip_stack_t *ipst = ns->netstack_ip; 11783 11784 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 11785 return (-1); 11786 11787 bzero(mtuinfo, sizeof (*mtuinfo)); 11788 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11789 mtuinfo->ip6m_addr.sin6_port = port; 11790 mtuinfo->ip6m_addr.sin6_addr = *in6; 11791 11792 ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL, ipst); 11793 if (ire != NULL) { 11794 mtuinfo->ip6m_mtu = ire->ire_max_frag; 11795 ire_refrele(ire); 11796 } else { 11797 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 11798 } 11799 return (sizeof (struct ip6_mtuinfo)); 11800 } 11801 11802 /* 11803 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 11804 * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and 11805 * isn't. This doesn't matter as the error checking is done properly for the 11806 * other MRT options coming in through ip_opt_set. 11807 */ 11808 int 11809 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 11810 { 11811 conn_t *connp = Q_TO_CONN(q); 11812 ipsec_req_t *req = (ipsec_req_t *)ptr; 11813 11814 switch (level) { 11815 case IPPROTO_IP: 11816 switch (name) { 11817 case MRT_VERSION: 11818 case MRT_ASSERT: 11819 (void) ip_mrouter_get(name, q, ptr); 11820 return (sizeof (int)); 11821 case IP_SEC_OPT: 11822 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 11823 case IP_NEXTHOP: 11824 if (connp->conn_nexthop_set) { 11825 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 11826 return (sizeof (ipaddr_t)); 11827 } else 11828 return (0); 11829 case IP_RECVPKTINFO: 11830 *(int *)ptr = connp->conn_ip_recvpktinfo ? 1: 0; 11831 return (sizeof (int)); 11832 default: 11833 break; 11834 } 11835 break; 11836 case IPPROTO_IPV6: 11837 switch (name) { 11838 case IPV6_SEC_OPT: 11839 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 11840 case IPV6_SRC_PREFERENCES: { 11841 return (ip6_get_src_preferences(connp, 11842 (uint32_t *)ptr)); 11843 } 11844 case IPV6_V6ONLY: 11845 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 11846 return (sizeof (int)); 11847 case IPV6_PATHMTU: 11848 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 11849 (struct ip6_mtuinfo *)ptr, 11850 connp->conn_netstack)); 11851 default: 11852 break; 11853 } 11854 break; 11855 default: 11856 break; 11857 } 11858 return (-1); 11859 } 11860 11861 /* Named Dispatch routine to get a current value out of our parameter table. */ 11862 /* ARGSUSED */ 11863 static int 11864 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11865 { 11866 ipparam_t *ippa = (ipparam_t *)cp; 11867 11868 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 11869 return (0); 11870 } 11871 11872 /* ARGSUSED */ 11873 static int 11874 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11875 { 11876 11877 (void) mi_mpprintf(mp, "%d", *(int *)cp); 11878 return (0); 11879 } 11880 11881 /* 11882 * Set ip{,6}_forwarding values. This means walking through all of the 11883 * ill's and toggling their forwarding values. 11884 */ 11885 /* ARGSUSED */ 11886 static int 11887 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11888 { 11889 long new_value; 11890 int *forwarding_value = (int *)cp; 11891 ill_t *walker; 11892 boolean_t isv6; 11893 ill_walk_context_t ctx; 11894 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11895 11896 isv6 = (forwarding_value == &ipst->ips_ipv6_forward); 11897 11898 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11899 new_value < 0 || new_value > 1) { 11900 return (EINVAL); 11901 } 11902 11903 *forwarding_value = new_value; 11904 11905 /* 11906 * Regardless of the current value of ip_forwarding, set all per-ill 11907 * values of ip_forwarding to the value being set. 11908 * 11909 * Bring all the ill's up to date with the new global value. 11910 */ 11911 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 11912 11913 if (isv6) 11914 walker = ILL_START_WALK_V6(&ctx, ipst); 11915 else 11916 walker = ILL_START_WALK_V4(&ctx, ipst); 11917 for (; walker != NULL; walker = ill_next(&ctx, walker)) { 11918 (void) ill_forward_set(q, mp, (new_value != 0), 11919 (caddr_t)walker); 11920 } 11921 rw_exit(&ipst->ips_ill_g_lock); 11922 11923 return (0); 11924 } 11925 11926 /* 11927 * Walk through the param array specified registering each element with the 11928 * Named Dispatch handler. This is called only during init. So it is ok 11929 * not to acquire any locks 11930 */ 11931 static boolean_t 11932 ip_param_register(IDP *ndp, ipparam_t *ippa, size_t ippa_cnt, 11933 ipndp_t *ipnd, size_t ipnd_cnt) 11934 { 11935 for (; ippa_cnt-- > 0; ippa++) { 11936 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 11937 if (!nd_load(ndp, ippa->ip_param_name, 11938 ip_param_get, ip_param_set, (caddr_t)ippa)) { 11939 nd_free(ndp); 11940 return (B_FALSE); 11941 } 11942 } 11943 } 11944 11945 for (; ipnd_cnt-- > 0; ipnd++) { 11946 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 11947 if (!nd_load(ndp, ipnd->ip_ndp_name, 11948 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 11949 ipnd->ip_ndp_data)) { 11950 nd_free(ndp); 11951 return (B_FALSE); 11952 } 11953 } 11954 } 11955 11956 return (B_TRUE); 11957 } 11958 11959 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 11960 /* ARGSUSED */ 11961 static int 11962 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11963 { 11964 long new_value; 11965 ipparam_t *ippa = (ipparam_t *)cp; 11966 11967 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11968 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 11969 return (EINVAL); 11970 } 11971 ippa->ip_param_value = new_value; 11972 return (0); 11973 } 11974 11975 /* 11976 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 11977 * When an ipf is passed here for the first time, if 11978 * we already have in-order fragments on the queue, we convert from the fast- 11979 * path reassembly scheme to the hard-case scheme. From then on, additional 11980 * fragments are reassembled here. We keep track of the start and end offsets 11981 * of each piece, and the number of holes in the chain. When the hole count 11982 * goes to zero, we are done! 11983 * 11984 * The ipf_count will be updated to account for any mblk(s) added (pointed to 11985 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 11986 * ipfb_count and ill_frag_count by the difference of ipf_count before and 11987 * after the call to ip_reassemble(). 11988 */ 11989 int 11990 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 11991 size_t msg_len) 11992 { 11993 uint_t end; 11994 mblk_t *next_mp; 11995 mblk_t *mp1; 11996 uint_t offset; 11997 boolean_t incr_dups = B_TRUE; 11998 boolean_t offset_zero_seen = B_FALSE; 11999 boolean_t pkt_boundary_checked = B_FALSE; 12000 12001 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 12002 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 12003 12004 /* Add in byte count */ 12005 ipf->ipf_count += msg_len; 12006 if (ipf->ipf_end) { 12007 /* 12008 * We were part way through in-order reassembly, but now there 12009 * is a hole. We walk through messages already queued, and 12010 * mark them for hard case reassembly. We know that up till 12011 * now they were in order starting from offset zero. 12012 */ 12013 offset = 0; 12014 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12015 IP_REASS_SET_START(mp1, offset); 12016 if (offset == 0) { 12017 ASSERT(ipf->ipf_nf_hdr_len != 0); 12018 offset = -ipf->ipf_nf_hdr_len; 12019 } 12020 offset += mp1->b_wptr - mp1->b_rptr; 12021 IP_REASS_SET_END(mp1, offset); 12022 } 12023 /* One hole at the end. */ 12024 ipf->ipf_hole_cnt = 1; 12025 /* Brand it as a hard case, forever. */ 12026 ipf->ipf_end = 0; 12027 } 12028 /* Walk through all the new pieces. */ 12029 do { 12030 end = start + (mp->b_wptr - mp->b_rptr); 12031 /* 12032 * If start is 0, decrease 'end' only for the first mblk of 12033 * the fragment. Otherwise 'end' can get wrong value in the 12034 * second pass of the loop if first mblk is exactly the 12035 * size of ipf_nf_hdr_len. 12036 */ 12037 if (start == 0 && !offset_zero_seen) { 12038 /* First segment */ 12039 ASSERT(ipf->ipf_nf_hdr_len != 0); 12040 end -= ipf->ipf_nf_hdr_len; 12041 offset_zero_seen = B_TRUE; 12042 } 12043 next_mp = mp->b_cont; 12044 /* 12045 * We are checking to see if there is any interesing data 12046 * to process. If there isn't and the mblk isn't the 12047 * one which carries the unfragmentable header then we 12048 * drop it. It's possible to have just the unfragmentable 12049 * header come through without any data. That needs to be 12050 * saved. 12051 * 12052 * If the assert at the top of this function holds then the 12053 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 12054 * is infrequently traveled enough that the test is left in 12055 * to protect against future code changes which break that 12056 * invariant. 12057 */ 12058 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 12059 /* Empty. Blast it. */ 12060 IP_REASS_SET_START(mp, 0); 12061 IP_REASS_SET_END(mp, 0); 12062 /* 12063 * If the ipf points to the mblk we are about to free, 12064 * update ipf to point to the next mblk (or NULL 12065 * if none). 12066 */ 12067 if (ipf->ipf_mp->b_cont == mp) 12068 ipf->ipf_mp->b_cont = next_mp; 12069 freeb(mp); 12070 continue; 12071 } 12072 mp->b_cont = NULL; 12073 IP_REASS_SET_START(mp, start); 12074 IP_REASS_SET_END(mp, end); 12075 if (!ipf->ipf_tail_mp) { 12076 ipf->ipf_tail_mp = mp; 12077 ipf->ipf_mp->b_cont = mp; 12078 if (start == 0 || !more) { 12079 ipf->ipf_hole_cnt = 1; 12080 /* 12081 * if the first fragment comes in more than one 12082 * mblk, this loop will be executed for each 12083 * mblk. Need to adjust hole count so exiting 12084 * this routine will leave hole count at 1. 12085 */ 12086 if (next_mp) 12087 ipf->ipf_hole_cnt++; 12088 } else 12089 ipf->ipf_hole_cnt = 2; 12090 continue; 12091 } else if (ipf->ipf_last_frag_seen && !more && 12092 !pkt_boundary_checked) { 12093 /* 12094 * We check datagram boundary only if this fragment 12095 * claims to be the last fragment and we have seen a 12096 * last fragment in the past too. We do this only 12097 * once for a given fragment. 12098 * 12099 * start cannot be 0 here as fragments with start=0 12100 * and MF=0 gets handled as a complete packet. These 12101 * fragments should not reach here. 12102 */ 12103 12104 if (start + msgdsize(mp) != 12105 IP_REASS_END(ipf->ipf_tail_mp)) { 12106 /* 12107 * We have two fragments both of which claim 12108 * to be the last fragment but gives conflicting 12109 * information about the whole datagram size. 12110 * Something fishy is going on. Drop the 12111 * fragment and free up the reassembly list. 12112 */ 12113 return (IP_REASS_FAILED); 12114 } 12115 12116 /* 12117 * We shouldn't come to this code block again for this 12118 * particular fragment. 12119 */ 12120 pkt_boundary_checked = B_TRUE; 12121 } 12122 12123 /* New stuff at or beyond tail? */ 12124 offset = IP_REASS_END(ipf->ipf_tail_mp); 12125 if (start >= offset) { 12126 if (ipf->ipf_last_frag_seen) { 12127 /* current fragment is beyond last fragment */ 12128 return (IP_REASS_FAILED); 12129 } 12130 /* Link it on end. */ 12131 ipf->ipf_tail_mp->b_cont = mp; 12132 ipf->ipf_tail_mp = mp; 12133 if (more) { 12134 if (start != offset) 12135 ipf->ipf_hole_cnt++; 12136 } else if (start == offset && next_mp == NULL) 12137 ipf->ipf_hole_cnt--; 12138 continue; 12139 } 12140 mp1 = ipf->ipf_mp->b_cont; 12141 offset = IP_REASS_START(mp1); 12142 /* New stuff at the front? */ 12143 if (start < offset) { 12144 if (start == 0) { 12145 if (end >= offset) { 12146 /* Nailed the hole at the begining. */ 12147 ipf->ipf_hole_cnt--; 12148 } 12149 } else if (end < offset) { 12150 /* 12151 * A hole, stuff, and a hole where there used 12152 * to be just a hole. 12153 */ 12154 ipf->ipf_hole_cnt++; 12155 } 12156 mp->b_cont = mp1; 12157 /* Check for overlap. */ 12158 while (end > offset) { 12159 if (end < IP_REASS_END(mp1)) { 12160 mp->b_wptr -= end - offset; 12161 IP_REASS_SET_END(mp, offset); 12162 BUMP_MIB(ill->ill_ip_mib, 12163 ipIfStatsReasmPartDups); 12164 break; 12165 } 12166 /* Did we cover another hole? */ 12167 if ((mp1->b_cont && 12168 IP_REASS_END(mp1) != 12169 IP_REASS_START(mp1->b_cont) && 12170 end >= IP_REASS_START(mp1->b_cont)) || 12171 (!ipf->ipf_last_frag_seen && !more)) { 12172 ipf->ipf_hole_cnt--; 12173 } 12174 /* Clip out mp1. */ 12175 if ((mp->b_cont = mp1->b_cont) == NULL) { 12176 /* 12177 * After clipping out mp1, this guy 12178 * is now hanging off the end. 12179 */ 12180 ipf->ipf_tail_mp = mp; 12181 } 12182 IP_REASS_SET_START(mp1, 0); 12183 IP_REASS_SET_END(mp1, 0); 12184 /* Subtract byte count */ 12185 ipf->ipf_count -= mp1->b_datap->db_lim - 12186 mp1->b_datap->db_base; 12187 freeb(mp1); 12188 BUMP_MIB(ill->ill_ip_mib, 12189 ipIfStatsReasmPartDups); 12190 mp1 = mp->b_cont; 12191 if (!mp1) 12192 break; 12193 offset = IP_REASS_START(mp1); 12194 } 12195 ipf->ipf_mp->b_cont = mp; 12196 continue; 12197 } 12198 /* 12199 * The new piece starts somewhere between the start of the head 12200 * and before the end of the tail. 12201 */ 12202 for (; mp1; mp1 = mp1->b_cont) { 12203 offset = IP_REASS_END(mp1); 12204 if (start < offset) { 12205 if (end <= offset) { 12206 /* Nothing new. */ 12207 IP_REASS_SET_START(mp, 0); 12208 IP_REASS_SET_END(mp, 0); 12209 /* Subtract byte count */ 12210 ipf->ipf_count -= mp->b_datap->db_lim - 12211 mp->b_datap->db_base; 12212 if (incr_dups) { 12213 ipf->ipf_num_dups++; 12214 incr_dups = B_FALSE; 12215 } 12216 freeb(mp); 12217 BUMP_MIB(ill->ill_ip_mib, 12218 ipIfStatsReasmDuplicates); 12219 break; 12220 } 12221 /* 12222 * Trim redundant stuff off beginning of new 12223 * piece. 12224 */ 12225 IP_REASS_SET_START(mp, offset); 12226 mp->b_rptr += offset - start; 12227 BUMP_MIB(ill->ill_ip_mib, 12228 ipIfStatsReasmPartDups); 12229 start = offset; 12230 if (!mp1->b_cont) { 12231 /* 12232 * After trimming, this guy is now 12233 * hanging off the end. 12234 */ 12235 mp1->b_cont = mp; 12236 ipf->ipf_tail_mp = mp; 12237 if (!more) { 12238 ipf->ipf_hole_cnt--; 12239 } 12240 break; 12241 } 12242 } 12243 if (start >= IP_REASS_START(mp1->b_cont)) 12244 continue; 12245 /* Fill a hole */ 12246 if (start > offset) 12247 ipf->ipf_hole_cnt++; 12248 mp->b_cont = mp1->b_cont; 12249 mp1->b_cont = mp; 12250 mp1 = mp->b_cont; 12251 offset = IP_REASS_START(mp1); 12252 if (end >= offset) { 12253 ipf->ipf_hole_cnt--; 12254 /* Check for overlap. */ 12255 while (end > offset) { 12256 if (end < IP_REASS_END(mp1)) { 12257 mp->b_wptr -= end - offset; 12258 IP_REASS_SET_END(mp, offset); 12259 /* 12260 * TODO we might bump 12261 * this up twice if there is 12262 * overlap at both ends. 12263 */ 12264 BUMP_MIB(ill->ill_ip_mib, 12265 ipIfStatsReasmPartDups); 12266 break; 12267 } 12268 /* Did we cover another hole? */ 12269 if ((mp1->b_cont && 12270 IP_REASS_END(mp1) 12271 != IP_REASS_START(mp1->b_cont) && 12272 end >= 12273 IP_REASS_START(mp1->b_cont)) || 12274 (!ipf->ipf_last_frag_seen && 12275 !more)) { 12276 ipf->ipf_hole_cnt--; 12277 } 12278 /* Clip out mp1. */ 12279 if ((mp->b_cont = mp1->b_cont) == 12280 NULL) { 12281 /* 12282 * After clipping out mp1, 12283 * this guy is now hanging 12284 * off the end. 12285 */ 12286 ipf->ipf_tail_mp = mp; 12287 } 12288 IP_REASS_SET_START(mp1, 0); 12289 IP_REASS_SET_END(mp1, 0); 12290 /* Subtract byte count */ 12291 ipf->ipf_count -= 12292 mp1->b_datap->db_lim - 12293 mp1->b_datap->db_base; 12294 freeb(mp1); 12295 BUMP_MIB(ill->ill_ip_mib, 12296 ipIfStatsReasmPartDups); 12297 mp1 = mp->b_cont; 12298 if (!mp1) 12299 break; 12300 offset = IP_REASS_START(mp1); 12301 } 12302 } 12303 break; 12304 } 12305 } while (start = end, mp = next_mp); 12306 12307 /* Fragment just processed could be the last one. Remember this fact */ 12308 if (!more) 12309 ipf->ipf_last_frag_seen = B_TRUE; 12310 12311 /* Still got holes? */ 12312 if (ipf->ipf_hole_cnt) 12313 return (IP_REASS_PARTIAL); 12314 /* Clean up overloaded fields to avoid upstream disasters. */ 12315 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12316 IP_REASS_SET_START(mp1, 0); 12317 IP_REASS_SET_END(mp1, 0); 12318 } 12319 return (IP_REASS_COMPLETE); 12320 } 12321 12322 /* 12323 * ipsec processing for the fast path, used for input UDP Packets 12324 */ 12325 static boolean_t 12326 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 12327 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present) 12328 { 12329 uint32_t ill_index; 12330 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 12331 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 12332 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 12333 12334 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12335 /* The ill_index of the incoming ILL */ 12336 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 12337 12338 /* pass packet up to the transport */ 12339 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 12340 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 12341 NULL, mctl_present); 12342 if (*first_mpp == NULL) { 12343 return (B_FALSE); 12344 } 12345 } 12346 12347 /* Initiate IPPF processing for fastpath UDP */ 12348 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 12349 ip_process(IPP_LOCAL_IN, mpp, ill_index); 12350 if (*mpp == NULL) { 12351 ip2dbg(("ip_input_ipsec_process: UDP pkt " 12352 "deferred/dropped during IPPF processing\n")); 12353 return (B_FALSE); 12354 } 12355 } 12356 /* 12357 * We make the checks as below since we are in the fast path 12358 * and want to minimize the number of checks if the IP_RECVIF and/or 12359 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 12360 */ 12361 if (connp->conn_recvif || connp->conn_recvslla || 12362 connp->conn_ip_recvpktinfo) { 12363 if (connp->conn_recvif) { 12364 in_flags = IPF_RECVIF; 12365 } 12366 /* 12367 * UDP supports IP_RECVPKTINFO option for both v4 and v6 12368 * so the flag passed to ip_add_info is based on IP version 12369 * of connp. 12370 */ 12371 if (connp->conn_ip_recvpktinfo) { 12372 if (connp->conn_af_isv6) { 12373 /* 12374 * V6 only needs index 12375 */ 12376 in_flags |= IPF_RECVIF; 12377 } else { 12378 /* 12379 * V4 needs index + matching address. 12380 */ 12381 in_flags |= IPF_RECVADDR; 12382 } 12383 } 12384 if (connp->conn_recvslla) { 12385 in_flags |= IPF_RECVSLLA; 12386 } 12387 /* 12388 * since in_flags are being set ill will be 12389 * referenced in ip_add_info, so it better not 12390 * be NULL. 12391 */ 12392 /* 12393 * the actual data will be contained in b_cont 12394 * upon successful return of the following call. 12395 * If the call fails then the original mblk is 12396 * returned. 12397 */ 12398 *mpp = ip_add_info(*mpp, ill, in_flags, IPCL_ZONEID(connp), 12399 ipst); 12400 } 12401 12402 return (B_TRUE); 12403 } 12404 12405 /* 12406 * Fragmentation reassembly. Each ILL has a hash table for 12407 * queuing packets undergoing reassembly for all IPIFs 12408 * associated with the ILL. The hash is based on the packet 12409 * IP ident field. The ILL frag hash table was allocated 12410 * as a timer block at the time the ILL was created. Whenever 12411 * there is anything on the reassembly queue, the timer will 12412 * be running. Returns B_TRUE if successful else B_FALSE; 12413 * frees mp on failure. 12414 */ 12415 static boolean_t 12416 ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, 12417 uint32_t *cksum_val, uint16_t *cksum_flags) 12418 { 12419 uint32_t frag_offset_flags; 12420 ill_t *ill = (ill_t *)q->q_ptr; 12421 mblk_t *mp = *mpp; 12422 mblk_t *t_mp; 12423 ipaddr_t dst; 12424 uint8_t proto = ipha->ipha_protocol; 12425 uint32_t sum_val; 12426 uint16_t sum_flags; 12427 ipf_t *ipf; 12428 ipf_t **ipfp; 12429 ipfb_t *ipfb; 12430 uint16_t ident; 12431 uint32_t offset; 12432 ipaddr_t src; 12433 uint_t hdr_length; 12434 uint32_t end; 12435 mblk_t *mp1; 12436 mblk_t *tail_mp; 12437 size_t count; 12438 size_t msg_len; 12439 uint8_t ecn_info = 0; 12440 uint32_t packet_size; 12441 boolean_t pruned = B_FALSE; 12442 ip_stack_t *ipst = ill->ill_ipst; 12443 12444 if (cksum_val != NULL) 12445 *cksum_val = 0; 12446 if (cksum_flags != NULL) 12447 *cksum_flags = 0; 12448 12449 /* 12450 * Drop the fragmented as early as possible, if 12451 * we don't have resource(s) to re-assemble. 12452 */ 12453 if (ipst->ips_ip_reass_queue_bytes == 0) { 12454 freemsg(mp); 12455 return (B_FALSE); 12456 } 12457 12458 /* Check for fragmentation offset; return if there's none */ 12459 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 12460 (IPH_MF | IPH_OFFSET)) == 0) 12461 return (B_TRUE); 12462 12463 /* 12464 * We utilize hardware computed checksum info only for UDP since 12465 * IP fragmentation is a normal occurence for the protocol. In 12466 * addition, checksum offload support for IP fragments carrying 12467 * UDP payload is commonly implemented across network adapters. 12468 */ 12469 ASSERT(ill != NULL); 12470 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 12471 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 12472 mblk_t *mp1 = mp->b_cont; 12473 int32_t len; 12474 12475 /* Record checksum information from the packet */ 12476 sum_val = (uint32_t)DB_CKSUM16(mp); 12477 sum_flags = DB_CKSUMFLAGS(mp); 12478 12479 /* IP payload offset from beginning of mblk */ 12480 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 12481 12482 if ((sum_flags & HCK_PARTIALCKSUM) && 12483 (mp1 == NULL || mp1->b_cont == NULL) && 12484 offset >= DB_CKSUMSTART(mp) && 12485 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 12486 uint32_t adj; 12487 /* 12488 * Partial checksum has been calculated by hardware 12489 * and attached to the packet; in addition, any 12490 * prepended extraneous data is even byte aligned. 12491 * If any such data exists, we adjust the checksum; 12492 * this would also handle any postpended data. 12493 */ 12494 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 12495 mp, mp1, len, adj); 12496 12497 /* One's complement subtract extraneous checksum */ 12498 if (adj >= sum_val) 12499 sum_val = ~(adj - sum_val) & 0xFFFF; 12500 else 12501 sum_val -= adj; 12502 } 12503 } else { 12504 sum_val = 0; 12505 sum_flags = 0; 12506 } 12507 12508 /* Clear hardware checksumming flag */ 12509 DB_CKSUMFLAGS(mp) = 0; 12510 12511 ident = ipha->ipha_ident; 12512 offset = (frag_offset_flags << 3) & 0xFFFF; 12513 src = ipha->ipha_src; 12514 dst = ipha->ipha_dst; 12515 hdr_length = IPH_HDR_LENGTH(ipha); 12516 end = ntohs(ipha->ipha_length) - hdr_length; 12517 12518 /* If end == 0 then we have a packet with no data, so just free it */ 12519 if (end == 0) { 12520 freemsg(mp); 12521 return (B_FALSE); 12522 } 12523 12524 /* Record the ECN field info. */ 12525 ecn_info = (ipha->ipha_type_of_service & 0x3); 12526 if (offset != 0) { 12527 /* 12528 * If this isn't the first piece, strip the header, and 12529 * add the offset to the end value. 12530 */ 12531 mp->b_rptr += hdr_length; 12532 end += offset; 12533 } 12534 12535 msg_len = MBLKSIZE(mp); 12536 tail_mp = mp; 12537 while (tail_mp->b_cont != NULL) { 12538 tail_mp = tail_mp->b_cont; 12539 msg_len += MBLKSIZE(tail_mp); 12540 } 12541 12542 /* If the reassembly list for this ILL will get too big, prune it */ 12543 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 12544 ipst->ips_ip_reass_queue_bytes) { 12545 ill_frag_prune(ill, 12546 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 : 12547 (ipst->ips_ip_reass_queue_bytes - msg_len)); 12548 pruned = B_TRUE; 12549 } 12550 12551 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 12552 mutex_enter(&ipfb->ipfb_lock); 12553 12554 ipfp = &ipfb->ipfb_ipf; 12555 /* Try to find an existing fragment queue for this packet. */ 12556 for (;;) { 12557 ipf = ipfp[0]; 12558 if (ipf != NULL) { 12559 /* 12560 * It has to match on ident and src/dst address. 12561 */ 12562 if (ipf->ipf_ident == ident && 12563 ipf->ipf_src == src && 12564 ipf->ipf_dst == dst && 12565 ipf->ipf_protocol == proto) { 12566 /* 12567 * If we have received too many 12568 * duplicate fragments for this packet 12569 * free it. 12570 */ 12571 if (ipf->ipf_num_dups > ip_max_frag_dups) { 12572 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12573 freemsg(mp); 12574 mutex_exit(&ipfb->ipfb_lock); 12575 return (B_FALSE); 12576 } 12577 /* Found it. */ 12578 break; 12579 } 12580 ipfp = &ipf->ipf_hash_next; 12581 continue; 12582 } 12583 12584 /* 12585 * If we pruned the list, do we want to store this new 12586 * fragment?. We apply an optimization here based on the 12587 * fact that most fragments will be received in order. 12588 * So if the offset of this incoming fragment is zero, 12589 * it is the first fragment of a new packet. We will 12590 * keep it. Otherwise drop the fragment, as we have 12591 * probably pruned the packet already (since the 12592 * packet cannot be found). 12593 */ 12594 if (pruned && offset != 0) { 12595 mutex_exit(&ipfb->ipfb_lock); 12596 freemsg(mp); 12597 return (B_FALSE); 12598 } 12599 12600 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) { 12601 /* 12602 * Too many fragmented packets in this hash 12603 * bucket. Free the oldest. 12604 */ 12605 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 12606 } 12607 12608 /* New guy. Allocate a frag message. */ 12609 mp1 = allocb(sizeof (*ipf), BPRI_MED); 12610 if (mp1 == NULL) { 12611 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12612 freemsg(mp); 12613 reass_done: 12614 mutex_exit(&ipfb->ipfb_lock); 12615 return (B_FALSE); 12616 } 12617 12618 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); 12619 mp1->b_cont = mp; 12620 12621 /* Initialize the fragment header. */ 12622 ipf = (ipf_t *)mp1->b_rptr; 12623 ipf->ipf_mp = mp1; 12624 ipf->ipf_ptphn = ipfp; 12625 ipfp[0] = ipf; 12626 ipf->ipf_hash_next = NULL; 12627 ipf->ipf_ident = ident; 12628 ipf->ipf_protocol = proto; 12629 ipf->ipf_src = src; 12630 ipf->ipf_dst = dst; 12631 ipf->ipf_nf_hdr_len = 0; 12632 /* Record reassembly start time. */ 12633 ipf->ipf_timestamp = gethrestime_sec(); 12634 /* Record ipf generation and account for frag header */ 12635 ipf->ipf_gen = ill->ill_ipf_gen++; 12636 ipf->ipf_count = MBLKSIZE(mp1); 12637 ipf->ipf_last_frag_seen = B_FALSE; 12638 ipf->ipf_ecn = ecn_info; 12639 ipf->ipf_num_dups = 0; 12640 ipfb->ipfb_frag_pkts++; 12641 ipf->ipf_checksum = 0; 12642 ipf->ipf_checksum_flags = 0; 12643 12644 /* Store checksum value in fragment header */ 12645 if (sum_flags != 0) { 12646 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12647 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12648 ipf->ipf_checksum = sum_val; 12649 ipf->ipf_checksum_flags = sum_flags; 12650 } 12651 12652 /* 12653 * We handle reassembly two ways. In the easy case, 12654 * where all the fragments show up in order, we do 12655 * minimal bookkeeping, and just clip new pieces on 12656 * the end. If we ever see a hole, then we go off 12657 * to ip_reassemble which has to mark the pieces and 12658 * keep track of the number of holes, etc. Obviously, 12659 * the point of having both mechanisms is so we can 12660 * handle the easy case as efficiently as possible. 12661 */ 12662 if (offset == 0) { 12663 /* Easy case, in-order reassembly so far. */ 12664 ipf->ipf_count += msg_len; 12665 ipf->ipf_tail_mp = tail_mp; 12666 /* 12667 * Keep track of next expected offset in 12668 * ipf_end. 12669 */ 12670 ipf->ipf_end = end; 12671 ipf->ipf_nf_hdr_len = hdr_length; 12672 } else { 12673 /* Hard case, hole at the beginning. */ 12674 ipf->ipf_tail_mp = NULL; 12675 /* 12676 * ipf_end == 0 means that we have given up 12677 * on easy reassembly. 12678 */ 12679 ipf->ipf_end = 0; 12680 12681 /* Forget checksum offload from now on */ 12682 ipf->ipf_checksum_flags = 0; 12683 12684 /* 12685 * ipf_hole_cnt is set by ip_reassemble. 12686 * ipf_count is updated by ip_reassemble. 12687 * No need to check for return value here 12688 * as we don't expect reassembly to complete 12689 * or fail for the first fragment itself. 12690 */ 12691 (void) ip_reassemble(mp, ipf, 12692 (frag_offset_flags & IPH_OFFSET) << 3, 12693 (frag_offset_flags & IPH_MF), ill, msg_len); 12694 } 12695 /* Update per ipfb and ill byte counts */ 12696 ipfb->ipfb_count += ipf->ipf_count; 12697 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12698 ill->ill_frag_count += ipf->ipf_count; 12699 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12700 /* If the frag timer wasn't already going, start it. */ 12701 mutex_enter(&ill->ill_lock); 12702 ill_frag_timer_start(ill); 12703 mutex_exit(&ill->ill_lock); 12704 goto reass_done; 12705 } 12706 12707 /* 12708 * If the packet's flag has changed (it could be coming up 12709 * from an interface different than the previous, therefore 12710 * possibly different checksum capability), then forget about 12711 * any stored checksum states. Otherwise add the value to 12712 * the existing one stored in the fragment header. 12713 */ 12714 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 12715 sum_val += ipf->ipf_checksum; 12716 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12717 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12718 ipf->ipf_checksum = sum_val; 12719 } else if (ipf->ipf_checksum_flags != 0) { 12720 /* Forget checksum offload from now on */ 12721 ipf->ipf_checksum_flags = 0; 12722 } 12723 12724 /* 12725 * We have a new piece of a datagram which is already being 12726 * reassembled. Update the ECN info if all IP fragments 12727 * are ECN capable. If there is one which is not, clear 12728 * all the info. If there is at least one which has CE 12729 * code point, IP needs to report that up to transport. 12730 */ 12731 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 12732 if (ecn_info == IPH_ECN_CE) 12733 ipf->ipf_ecn = IPH_ECN_CE; 12734 } else { 12735 ipf->ipf_ecn = IPH_ECN_NECT; 12736 } 12737 if (offset && ipf->ipf_end == offset) { 12738 /* The new fragment fits at the end */ 12739 ipf->ipf_tail_mp->b_cont = mp; 12740 /* Update the byte count */ 12741 ipf->ipf_count += msg_len; 12742 /* Update per ipfb and ill byte counts */ 12743 ipfb->ipfb_count += msg_len; 12744 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12745 ill->ill_frag_count += msg_len; 12746 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12747 if (frag_offset_flags & IPH_MF) { 12748 /* More to come. */ 12749 ipf->ipf_end = end; 12750 ipf->ipf_tail_mp = tail_mp; 12751 goto reass_done; 12752 } 12753 } else { 12754 /* Go do the hard cases. */ 12755 int ret; 12756 12757 if (offset == 0) 12758 ipf->ipf_nf_hdr_len = hdr_length; 12759 12760 /* Save current byte count */ 12761 count = ipf->ipf_count; 12762 ret = ip_reassemble(mp, ipf, 12763 (frag_offset_flags & IPH_OFFSET) << 3, 12764 (frag_offset_flags & IPH_MF), ill, msg_len); 12765 /* Count of bytes added and subtracted (freeb()ed) */ 12766 count = ipf->ipf_count - count; 12767 if (count) { 12768 /* Update per ipfb and ill byte counts */ 12769 ipfb->ipfb_count += count; 12770 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12771 ill->ill_frag_count += count; 12772 ASSERT(ill->ill_frag_count > 0); 12773 } 12774 if (ret == IP_REASS_PARTIAL) { 12775 goto reass_done; 12776 } else if (ret == IP_REASS_FAILED) { 12777 /* Reassembly failed. Free up all resources */ 12778 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12779 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 12780 IP_REASS_SET_START(t_mp, 0); 12781 IP_REASS_SET_END(t_mp, 0); 12782 } 12783 freemsg(mp); 12784 goto reass_done; 12785 } 12786 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 12787 } 12788 /* 12789 * We have completed reassembly. Unhook the frag header from 12790 * the reassembly list. 12791 * 12792 * Before we free the frag header, record the ECN info 12793 * to report back to the transport. 12794 */ 12795 ecn_info = ipf->ipf_ecn; 12796 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); 12797 ipfp = ipf->ipf_ptphn; 12798 12799 /* We need to supply these to caller */ 12800 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 12801 sum_val = ipf->ipf_checksum; 12802 else 12803 sum_val = 0; 12804 12805 mp1 = ipf->ipf_mp; 12806 count = ipf->ipf_count; 12807 ipf = ipf->ipf_hash_next; 12808 if (ipf != NULL) 12809 ipf->ipf_ptphn = ipfp; 12810 ipfp[0] = ipf; 12811 ill->ill_frag_count -= count; 12812 ASSERT(ipfb->ipfb_count >= count); 12813 ipfb->ipfb_count -= count; 12814 ipfb->ipfb_frag_pkts--; 12815 mutex_exit(&ipfb->ipfb_lock); 12816 /* Ditch the frag header. */ 12817 mp = mp1->b_cont; 12818 12819 freeb(mp1); 12820 12821 /* Restore original IP length in header. */ 12822 packet_size = (uint32_t)msgdsize(mp); 12823 if (packet_size > IP_MAXPACKET) { 12824 freemsg(mp); 12825 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 12826 return (B_FALSE); 12827 } 12828 12829 if (DB_REF(mp) > 1) { 12830 mblk_t *mp2 = copymsg(mp); 12831 12832 freemsg(mp); 12833 if (mp2 == NULL) { 12834 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12835 return (B_FALSE); 12836 } 12837 mp = mp2; 12838 } 12839 ipha = (ipha_t *)mp->b_rptr; 12840 12841 ipha->ipha_length = htons((uint16_t)packet_size); 12842 /* We're now complete, zip the frag state */ 12843 ipha->ipha_fragment_offset_and_flags = 0; 12844 /* Record the ECN info. */ 12845 ipha->ipha_type_of_service &= 0xFC; 12846 ipha->ipha_type_of_service |= ecn_info; 12847 *mpp = mp; 12848 12849 /* Reassembly is successful; return checksum information if needed */ 12850 if (cksum_val != NULL) 12851 *cksum_val = sum_val; 12852 if (cksum_flags != NULL) 12853 *cksum_flags = sum_flags; 12854 12855 return (B_TRUE); 12856 } 12857 12858 /* 12859 * Perform ip header check sum update local options. 12860 * return B_TRUE if all is well, else return B_FALSE and release 12861 * the mp. caller is responsible for decrementing ire ref cnt. 12862 */ 12863 static boolean_t 12864 ip_options_cksum(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12865 ip_stack_t *ipst) 12866 { 12867 mblk_t *first_mp; 12868 boolean_t mctl_present; 12869 uint16_t sum; 12870 12871 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12872 /* 12873 * Don't do the checksum if it has gone through AH/ESP 12874 * processing. 12875 */ 12876 if (!mctl_present) { 12877 sum = ip_csum_hdr(ipha); 12878 if (sum != 0) { 12879 if (ill != NULL) { 12880 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12881 } else { 12882 BUMP_MIB(&ipst->ips_ip_mib, 12883 ipIfStatsInCksumErrs); 12884 } 12885 freemsg(first_mp); 12886 return (B_FALSE); 12887 } 12888 } 12889 12890 if (!ip_rput_local_options(q, mp, ipha, ire, ipst)) { 12891 if (mctl_present) 12892 freeb(first_mp); 12893 return (B_FALSE); 12894 } 12895 12896 return (B_TRUE); 12897 } 12898 12899 /* 12900 * All udp packet are delivered to the local host via this routine. 12901 */ 12902 void 12903 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12904 ill_t *recv_ill) 12905 { 12906 uint32_t sum; 12907 uint32_t u1; 12908 boolean_t mctl_present; 12909 conn_t *connp; 12910 mblk_t *first_mp; 12911 uint16_t *up; 12912 ill_t *ill = (ill_t *)q->q_ptr; 12913 uint16_t reass_hck_flags = 0; 12914 ip_stack_t *ipst; 12915 12916 ASSERT(recv_ill != NULL); 12917 ipst = recv_ill->ill_ipst; 12918 12919 #define rptr ((uchar_t *)ipha) 12920 12921 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12922 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 12923 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12924 ASSERT(ill != NULL); 12925 12926 /* 12927 * FAST PATH for udp packets 12928 */ 12929 12930 /* u1 is # words of IP options */ 12931 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 12932 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12933 12934 /* IP options present */ 12935 if (u1 != 0) 12936 goto ipoptions; 12937 12938 /* Check the IP header checksum. */ 12939 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12940 /* Clear the IP header h/w cksum flag */ 12941 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12942 } else { 12943 #define uph ((uint16_t *)ipha) 12944 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 12945 uph[6] + uph[7] + uph[8] + uph[9]; 12946 #undef uph 12947 /* finish doing IP checksum */ 12948 sum = (sum & 0xFFFF) + (sum >> 16); 12949 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12950 /* 12951 * Don't verify header checksum if this packet is coming 12952 * back from AH/ESP as we already did it. 12953 */ 12954 if (!mctl_present && sum != 0 && sum != 0xFFFF) { 12955 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12956 freemsg(first_mp); 12957 return; 12958 } 12959 } 12960 12961 /* 12962 * Count for SNMP of inbound packets for ire. 12963 * if mctl is present this might be a secure packet and 12964 * has already been counted for in ip_proto_input(). 12965 */ 12966 if (!mctl_present) { 12967 UPDATE_IB_PKT_COUNT(ire); 12968 ire->ire_last_used_time = lbolt; 12969 } 12970 12971 /* packet part of fragmented IP packet? */ 12972 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12973 if (u1 & (IPH_MF | IPH_OFFSET)) { 12974 goto fragmented; 12975 } 12976 12977 /* u1 = IP header length (20 bytes) */ 12978 u1 = IP_SIMPLE_HDR_LENGTH; 12979 12980 /* packet does not contain complete IP & UDP headers */ 12981 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 12982 goto udppullup; 12983 12984 /* up points to UDP header */ 12985 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 12986 #define iphs ((uint16_t *)ipha) 12987 12988 /* if udp hdr cksum != 0, then need to checksum udp packet */ 12989 if (up[3] != 0) { 12990 mblk_t *mp1 = mp->b_cont; 12991 boolean_t cksum_err; 12992 uint16_t hck_flags = 0; 12993 12994 /* Pseudo-header checksum */ 12995 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12996 iphs[9] + up[2]; 12997 12998 /* 12999 * Revert to software checksum calculation if the interface 13000 * isn't capable of checksum offload or if IPsec is present. 13001 */ 13002 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 13003 hck_flags = DB_CKSUMFLAGS(mp); 13004 13005 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13006 IP_STAT(ipst, ip_in_sw_cksum); 13007 13008 IP_CKSUM_RECV(hck_flags, u1, 13009 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13010 (int32_t)((uchar_t *)up - rptr), 13011 mp, mp1, cksum_err); 13012 13013 if (cksum_err) { 13014 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 13015 if (hck_flags & HCK_FULLCKSUM) 13016 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 13017 else if (hck_flags & HCK_PARTIALCKSUM) 13018 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 13019 else 13020 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 13021 13022 freemsg(first_mp); 13023 return; 13024 } 13025 } 13026 13027 /* Non-fragmented broadcast or multicast packet? */ 13028 if (ire->ire_type == IRE_BROADCAST) 13029 goto udpslowpath; 13030 13031 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 13032 ire->ire_zoneid, ipst)) != NULL) { 13033 ASSERT(connp->conn_upq != NULL); 13034 IP_STAT(ipst, ip_udp_fast_path); 13035 13036 if (CONN_UDP_FLOWCTLD(connp)) { 13037 freemsg(mp); 13038 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 13039 } else { 13040 if (!mctl_present) { 13041 BUMP_MIB(ill->ill_ip_mib, 13042 ipIfStatsHCInDelivers); 13043 } 13044 /* 13045 * mp and first_mp can change. 13046 */ 13047 if (ip_udp_check(q, connp, recv_ill, 13048 ipha, &mp, &first_mp, mctl_present)) { 13049 /* Send it upstream */ 13050 CONN_UDP_RECV(connp, mp); 13051 } 13052 } 13053 /* 13054 * freeb() cannot deal with null mblk being passed 13055 * in and first_mp can be set to null in the call 13056 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 13057 */ 13058 if (mctl_present && first_mp != NULL) { 13059 freeb(first_mp); 13060 } 13061 CONN_DEC_REF(connp); 13062 return; 13063 } 13064 13065 /* 13066 * if we got here we know the packet is not fragmented and 13067 * has no options. The classifier could not find a conn_t and 13068 * most likely its an icmp packet so send it through slow path. 13069 */ 13070 13071 goto udpslowpath; 13072 13073 ipoptions: 13074 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 13075 goto slow_done; 13076 } 13077 13078 UPDATE_IB_PKT_COUNT(ire); 13079 ire->ire_last_used_time = lbolt; 13080 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13081 if (u1 & (IPH_MF | IPH_OFFSET)) { 13082 fragmented: 13083 /* 13084 * "sum" and "reass_hck_flags" are non-zero if the 13085 * reassembled packet has a valid hardware computed 13086 * checksum information associated with it. 13087 */ 13088 if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) 13089 goto slow_done; 13090 /* 13091 * Make sure that first_mp points back to mp as 13092 * the mp we came in with could have changed in 13093 * ip_rput_fragment(). 13094 */ 13095 ASSERT(!mctl_present); 13096 ipha = (ipha_t *)mp->b_rptr; 13097 first_mp = mp; 13098 } 13099 13100 /* Now we have a complete datagram, destined for this machine. */ 13101 u1 = IPH_HDR_LENGTH(ipha); 13102 /* Pull up the UDP header, if necessary. */ 13103 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 13104 udppullup: 13105 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 13106 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13107 freemsg(first_mp); 13108 goto slow_done; 13109 } 13110 ipha = (ipha_t *)mp->b_rptr; 13111 } 13112 13113 /* 13114 * Validate the checksum for the reassembled packet; for the 13115 * pullup case we calculate the payload checksum in software. 13116 */ 13117 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 13118 if (up[3] != 0) { 13119 boolean_t cksum_err; 13120 13121 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13122 IP_STAT(ipst, ip_in_sw_cksum); 13123 13124 IP_CKSUM_RECV_REASS(reass_hck_flags, 13125 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 13126 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 13127 iphs[9] + up[2], sum, cksum_err); 13128 13129 if (cksum_err) { 13130 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 13131 13132 if (reass_hck_flags & HCK_FULLCKSUM) 13133 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 13134 else if (reass_hck_flags & HCK_PARTIALCKSUM) 13135 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 13136 else 13137 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 13138 13139 freemsg(first_mp); 13140 goto slow_done; 13141 } 13142 } 13143 udpslowpath: 13144 13145 /* Clear hardware checksum flag to be safe */ 13146 DB_CKSUMFLAGS(mp) = 0; 13147 13148 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 13149 (ire->ire_type == IRE_BROADCAST), 13150 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IPINFO, 13151 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 13152 13153 slow_done: 13154 IP_STAT(ipst, ip_udp_slow_path); 13155 return; 13156 13157 #undef iphs 13158 #undef rptr 13159 } 13160 13161 /* ARGSUSED */ 13162 static mblk_t * 13163 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13164 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 13165 ill_rx_ring_t *ill_ring) 13166 { 13167 conn_t *connp; 13168 uint32_t sum; 13169 uint32_t u1; 13170 uint16_t *up; 13171 int offset; 13172 ssize_t len; 13173 mblk_t *mp1; 13174 boolean_t syn_present = B_FALSE; 13175 tcph_t *tcph; 13176 uint_t ip_hdr_len; 13177 ill_t *ill = (ill_t *)q->q_ptr; 13178 zoneid_t zoneid = ire->ire_zoneid; 13179 boolean_t cksum_err; 13180 uint16_t hck_flags = 0; 13181 ip_stack_t *ipst = recv_ill->ill_ipst; 13182 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 13183 13184 #define rptr ((uchar_t *)ipha) 13185 13186 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 13187 ASSERT(ill != NULL); 13188 13189 /* 13190 * FAST PATH for tcp packets 13191 */ 13192 13193 /* u1 is # words of IP options */ 13194 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13195 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13196 13197 /* IP options present */ 13198 if (u1) { 13199 goto ipoptions; 13200 } else { 13201 /* Check the IP header checksum. */ 13202 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 13203 /* Clear the IP header h/w cksum flag */ 13204 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 13205 } else { 13206 #define uph ((uint16_t *)ipha) 13207 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13208 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13209 #undef uph 13210 /* finish doing IP checksum */ 13211 sum = (sum & 0xFFFF) + (sum >> 16); 13212 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13213 /* 13214 * Don't verify header checksum if this packet 13215 * is coming back from AH/ESP as we already did it. 13216 */ 13217 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 13218 BUMP_MIB(ill->ill_ip_mib, 13219 ipIfStatsInCksumErrs); 13220 goto error; 13221 } 13222 } 13223 } 13224 13225 if (!mctl_present) { 13226 UPDATE_IB_PKT_COUNT(ire); 13227 ire->ire_last_used_time = lbolt; 13228 } 13229 13230 /* packet part of fragmented IP packet? */ 13231 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13232 if (u1 & (IPH_MF | IPH_OFFSET)) { 13233 goto fragmented; 13234 } 13235 13236 /* u1 = IP header length (20 bytes) */ 13237 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 13238 13239 /* does packet contain IP+TCP headers? */ 13240 len = mp->b_wptr - rptr; 13241 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 13242 IP_STAT(ipst, ip_tcppullup); 13243 goto tcppullup; 13244 } 13245 13246 /* TCP options present? */ 13247 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 13248 13249 /* 13250 * If options need to be pulled up, then goto tcpoptions. 13251 * otherwise we are still in the fast path 13252 */ 13253 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 13254 IP_STAT(ipst, ip_tcpoptions); 13255 goto tcpoptions; 13256 } 13257 13258 /* multiple mblks of tcp data? */ 13259 if ((mp1 = mp->b_cont) != NULL) { 13260 /* more then two? */ 13261 if (mp1->b_cont != NULL) { 13262 IP_STAT(ipst, ip_multipkttcp); 13263 goto multipkttcp; 13264 } 13265 len += mp1->b_wptr - mp1->b_rptr; 13266 } 13267 13268 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 13269 13270 /* part of pseudo checksum */ 13271 13272 /* TCP datagram length */ 13273 u1 = len - IP_SIMPLE_HDR_LENGTH; 13274 13275 #define iphs ((uint16_t *)ipha) 13276 13277 #ifdef _BIG_ENDIAN 13278 u1 += IPPROTO_TCP; 13279 #else 13280 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13281 #endif 13282 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13283 13284 /* 13285 * Revert to software checksum calculation if the interface 13286 * isn't capable of checksum offload or if IPsec is present. 13287 */ 13288 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 13289 hck_flags = DB_CKSUMFLAGS(mp); 13290 13291 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13292 IP_STAT(ipst, ip_in_sw_cksum); 13293 13294 IP_CKSUM_RECV(hck_flags, u1, 13295 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13296 (int32_t)((uchar_t *)up - rptr), 13297 mp, mp1, cksum_err); 13298 13299 if (cksum_err) { 13300 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13301 13302 if (hck_flags & HCK_FULLCKSUM) 13303 IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err); 13304 else if (hck_flags & HCK_PARTIALCKSUM) 13305 IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err); 13306 else 13307 IP_STAT(ipst, ip_tcp_in_sw_cksum_err); 13308 13309 goto error; 13310 } 13311 13312 try_again: 13313 13314 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 13315 zoneid, ipst)) == NULL) { 13316 /* Send the TH_RST */ 13317 goto no_conn; 13318 } 13319 13320 /* 13321 * TCP FAST PATH for AF_INET socket. 13322 * 13323 * TCP fast path to avoid extra work. An AF_INET socket type 13324 * does not have facility to receive extra information via 13325 * ip_process or ip_add_info. Also, when the connection was 13326 * established, we made a check if this connection is impacted 13327 * by any global IPSec policy or per connection policy (a 13328 * policy that comes in effect later will not apply to this 13329 * connection). Since all this can be determined at the 13330 * connection establishment time, a quick check of flags 13331 * can avoid extra work. 13332 */ 13333 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 13334 !IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13335 ASSERT(first_mp == mp); 13336 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13337 SET_SQUEUE(mp, tcp_rput_data, connp); 13338 return (mp); 13339 } 13340 13341 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 13342 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 13343 if (IPCL_IS_TCP(connp)) { 13344 mp->b_datap->db_struioflag |= STRUIO_EAGER; 13345 DB_CKSUMSTART(mp) = 13346 (intptr_t)ip_squeue_get(ill_ring); 13347 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 13348 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13349 BUMP_MIB(ill->ill_ip_mib, 13350 ipIfStatsHCInDelivers); 13351 SET_SQUEUE(mp, connp->conn_recv, connp); 13352 return (mp); 13353 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 13354 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13355 BUMP_MIB(ill->ill_ip_mib, 13356 ipIfStatsHCInDelivers); 13357 ip_squeue_enter_unbound++; 13358 SET_SQUEUE(mp, tcp_conn_request_unbound, 13359 connp); 13360 return (mp); 13361 } 13362 syn_present = B_TRUE; 13363 } 13364 13365 } 13366 13367 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 13368 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13369 13370 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13371 /* No need to send this packet to TCP */ 13372 if ((flags & TH_RST) || (flags & TH_URG)) { 13373 CONN_DEC_REF(connp); 13374 freemsg(first_mp); 13375 return (NULL); 13376 } 13377 if (flags & TH_ACK) { 13378 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 13379 ipst->ips_netstack->netstack_tcp); 13380 CONN_DEC_REF(connp); 13381 return (NULL); 13382 } 13383 13384 CONN_DEC_REF(connp); 13385 freemsg(first_mp); 13386 return (NULL); 13387 } 13388 13389 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 13390 first_mp = ipsec_check_inbound_policy(first_mp, connp, 13391 ipha, NULL, mctl_present); 13392 if (first_mp == NULL) { 13393 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13394 CONN_DEC_REF(connp); 13395 return (NULL); 13396 } 13397 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 13398 ASSERT(syn_present); 13399 if (mctl_present) { 13400 ASSERT(first_mp != mp); 13401 first_mp->b_datap->db_struioflag |= 13402 STRUIO_POLICY; 13403 } else { 13404 ASSERT(first_mp == mp); 13405 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 13406 mp->b_datap->db_struioflag |= STRUIO_POLICY; 13407 } 13408 } else { 13409 /* 13410 * Discard first_mp early since we're dealing with a 13411 * fully-connected conn_t and tcp doesn't do policy in 13412 * this case. 13413 */ 13414 if (mctl_present) { 13415 freeb(first_mp); 13416 mctl_present = B_FALSE; 13417 } 13418 first_mp = mp; 13419 } 13420 } 13421 13422 /* Initiate IPPF processing for fastpath */ 13423 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13424 uint32_t ill_index; 13425 13426 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13427 ip_process(IPP_LOCAL_IN, &mp, ill_index); 13428 if (mp == NULL) { 13429 ip2dbg(("ip_input_ipsec_process: TCP pkt " 13430 "deferred/dropped during IPPF processing\n")); 13431 CONN_DEC_REF(connp); 13432 if (mctl_present) 13433 freeb(first_mp); 13434 return (NULL); 13435 } else if (mctl_present) { 13436 /* 13437 * ip_process might return a new mp. 13438 */ 13439 ASSERT(first_mp != mp); 13440 first_mp->b_cont = mp; 13441 } else { 13442 first_mp = mp; 13443 } 13444 13445 } 13446 13447 if (!syn_present && connp->conn_ip_recvpktinfo) { 13448 /* 13449 * TCP does not support IP_RECVPKTINFO for v4 so lets 13450 * make sure IPF_RECVIF is passed to ip_add_info. 13451 */ 13452 mp = ip_add_info(mp, recv_ill, flags|IPF_RECVIF, 13453 IPCL_ZONEID(connp), ipst); 13454 if (mp == NULL) { 13455 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13456 CONN_DEC_REF(connp); 13457 if (mctl_present) 13458 freeb(first_mp); 13459 return (NULL); 13460 } else if (mctl_present) { 13461 /* 13462 * ip_add_info might return a new mp. 13463 */ 13464 ASSERT(first_mp != mp); 13465 first_mp->b_cont = mp; 13466 } else { 13467 first_mp = mp; 13468 } 13469 } 13470 13471 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13472 if (IPCL_IS_TCP(connp)) { 13473 SET_SQUEUE(first_mp, connp->conn_recv, connp); 13474 return (first_mp); 13475 } else { 13476 putnext(connp->conn_rq, first_mp); 13477 CONN_DEC_REF(connp); 13478 return (NULL); 13479 } 13480 13481 no_conn: 13482 /* Initiate IPPf processing, if needed. */ 13483 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13484 uint32_t ill_index; 13485 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13486 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 13487 if (first_mp == NULL) { 13488 return (NULL); 13489 } 13490 } 13491 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13492 13493 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid, 13494 ipst->ips_netstack->netstack_tcp); 13495 return (NULL); 13496 ipoptions: 13497 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) { 13498 goto slow_done; 13499 } 13500 13501 UPDATE_IB_PKT_COUNT(ire); 13502 ire->ire_last_used_time = lbolt; 13503 13504 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13505 if (u1 & (IPH_MF | IPH_OFFSET)) { 13506 fragmented: 13507 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 13508 if (mctl_present) 13509 freeb(first_mp); 13510 goto slow_done; 13511 } 13512 /* 13513 * Make sure that first_mp points back to mp as 13514 * the mp we came in with could have changed in 13515 * ip_rput_fragment(). 13516 */ 13517 ASSERT(!mctl_present); 13518 ipha = (ipha_t *)mp->b_rptr; 13519 first_mp = mp; 13520 } 13521 13522 /* Now we have a complete datagram, destined for this machine. */ 13523 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 13524 13525 len = mp->b_wptr - mp->b_rptr; 13526 /* Pull up a minimal TCP header, if necessary. */ 13527 if (len < (u1 + 20)) { 13528 tcppullup: 13529 if (!pullupmsg(mp, u1 + 20)) { 13530 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13531 goto error; 13532 } 13533 ipha = (ipha_t *)mp->b_rptr; 13534 len = mp->b_wptr - mp->b_rptr; 13535 } 13536 13537 /* 13538 * Extract the offset field from the TCP header. As usual, we 13539 * try to help the compiler more than the reader. 13540 */ 13541 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 13542 if (offset != 5) { 13543 tcpoptions: 13544 if (offset < 5) { 13545 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13546 goto error; 13547 } 13548 /* 13549 * There must be TCP options. 13550 * Make sure we can grab them. 13551 */ 13552 offset <<= 2; 13553 offset += u1; 13554 if (len < offset) { 13555 if (!pullupmsg(mp, offset)) { 13556 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13557 goto error; 13558 } 13559 ipha = (ipha_t *)mp->b_rptr; 13560 len = mp->b_wptr - rptr; 13561 } 13562 } 13563 13564 /* Get the total packet length in len, including headers. */ 13565 if (mp->b_cont) { 13566 multipkttcp: 13567 len = msgdsize(mp); 13568 } 13569 13570 /* 13571 * Check the TCP checksum by pulling together the pseudo- 13572 * header checksum, and passing it to ip_csum to be added in 13573 * with the TCP datagram. 13574 * 13575 * Since we are not using the hwcksum if available we must 13576 * clear the flag. We may come here via tcppullup or tcpoptions. 13577 * If either of these fails along the way the mblk is freed. 13578 * If this logic ever changes and mblk is reused to say send 13579 * ICMP's back, then this flag may need to be cleared in 13580 * other places as well. 13581 */ 13582 DB_CKSUMFLAGS(mp) = 0; 13583 13584 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 13585 13586 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 13587 #ifdef _BIG_ENDIAN 13588 u1 += IPPROTO_TCP; 13589 #else 13590 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13591 #endif 13592 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13593 /* 13594 * Not M_DATA mblk or its a dup, so do the checksum now. 13595 */ 13596 IP_STAT(ipst, ip_in_sw_cksum); 13597 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 13598 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13599 goto error; 13600 } 13601 13602 IP_STAT(ipst, ip_tcp_slow_path); 13603 goto try_again; 13604 #undef iphs 13605 #undef rptr 13606 13607 error: 13608 freemsg(first_mp); 13609 slow_done: 13610 return (NULL); 13611 } 13612 13613 /* ARGSUSED */ 13614 static void 13615 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13616 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 13617 { 13618 conn_t *connp; 13619 uint32_t sum; 13620 uint32_t u1; 13621 ssize_t len; 13622 sctp_hdr_t *sctph; 13623 zoneid_t zoneid = ire->ire_zoneid; 13624 uint32_t pktsum; 13625 uint32_t calcsum; 13626 uint32_t ports; 13627 in6_addr_t map_src, map_dst; 13628 ill_t *ill = (ill_t *)q->q_ptr; 13629 ip_stack_t *ipst; 13630 sctp_stack_t *sctps; 13631 13632 ASSERT(recv_ill != NULL); 13633 ipst = recv_ill->ill_ipst; 13634 sctps = ipst->ips_netstack->netstack_sctp; 13635 13636 #define rptr ((uchar_t *)ipha) 13637 13638 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 13639 ASSERT(ill != NULL); 13640 13641 /* u1 is # words of IP options */ 13642 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13643 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13644 13645 /* IP options present */ 13646 if (u1 > 0) { 13647 goto ipoptions; 13648 } else { 13649 /* Check the IP header checksum. */ 13650 if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 13651 #define uph ((uint16_t *)ipha) 13652 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13653 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13654 #undef uph 13655 /* finish doing IP checksum */ 13656 sum = (sum & 0xFFFF) + (sum >> 16); 13657 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13658 /* 13659 * Don't verify header checksum if this packet 13660 * is coming back from AH/ESP as we already did it. 13661 */ 13662 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 13663 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 13664 goto error; 13665 } 13666 } 13667 /* 13668 * Since there is no SCTP h/w cksum support yet, just 13669 * clear the flag. 13670 */ 13671 DB_CKSUMFLAGS(mp) = 0; 13672 } 13673 13674 /* 13675 * Don't verify header checksum if this packet is coming 13676 * back from AH/ESP as we already did it. 13677 */ 13678 if (!mctl_present) { 13679 UPDATE_IB_PKT_COUNT(ire); 13680 ire->ire_last_used_time = lbolt; 13681 } 13682 13683 /* packet part of fragmented IP packet? */ 13684 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13685 if (u1 & (IPH_MF | IPH_OFFSET)) 13686 goto fragmented; 13687 13688 /* u1 = IP header length (20 bytes) */ 13689 u1 = IP_SIMPLE_HDR_LENGTH; 13690 13691 find_sctp_client: 13692 /* Pullup if we don't have the sctp common header. */ 13693 len = MBLKL(mp); 13694 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 13695 if (mp->b_cont == NULL || 13696 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 13697 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13698 goto error; 13699 } 13700 ipha = (ipha_t *)mp->b_rptr; 13701 len = MBLKL(mp); 13702 } 13703 13704 sctph = (sctp_hdr_t *)(rptr + u1); 13705 #ifdef DEBUG 13706 if (!skip_sctp_cksum) { 13707 #endif 13708 pktsum = sctph->sh_chksum; 13709 sctph->sh_chksum = 0; 13710 calcsum = sctp_cksum(mp, u1); 13711 if (calcsum != pktsum) { 13712 BUMP_MIB(&sctps->sctps_mib, sctpChecksumError); 13713 goto error; 13714 } 13715 sctph->sh_chksum = pktsum; 13716 #ifdef DEBUG /* skip_sctp_cksum */ 13717 } 13718 #endif 13719 /* get the ports */ 13720 ports = *(uint32_t *)&sctph->sh_sport; 13721 13722 IRE_REFRELE(ire); 13723 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 13724 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 13725 if ((connp = sctp_fanout(&map_src, &map_dst, ports, zoneid, mp, 13726 sctps)) == NULL) { 13727 /* Check for raw socket or OOTB handling */ 13728 goto no_conn; 13729 } 13730 13731 /* Found a client; up it goes */ 13732 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13733 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 13734 return; 13735 13736 no_conn: 13737 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 13738 ports, mctl_present, flags, B_TRUE, zoneid); 13739 return; 13740 13741 ipoptions: 13742 DB_CKSUMFLAGS(mp) = 0; 13743 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) 13744 goto slow_done; 13745 13746 UPDATE_IB_PKT_COUNT(ire); 13747 ire->ire_last_used_time = lbolt; 13748 13749 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13750 if (u1 & (IPH_MF | IPH_OFFSET)) { 13751 fragmented: 13752 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) 13753 goto slow_done; 13754 /* 13755 * Make sure that first_mp points back to mp as 13756 * the mp we came in with could have changed in 13757 * ip_rput_fragment(). 13758 */ 13759 ASSERT(!mctl_present); 13760 ipha = (ipha_t *)mp->b_rptr; 13761 first_mp = mp; 13762 } 13763 13764 /* Now we have a complete datagram, destined for this machine. */ 13765 u1 = IPH_HDR_LENGTH(ipha); 13766 goto find_sctp_client; 13767 #undef iphs 13768 #undef rptr 13769 13770 error: 13771 freemsg(first_mp); 13772 slow_done: 13773 IRE_REFRELE(ire); 13774 } 13775 13776 #define VER_BITS 0xF0 13777 #define VERSION_6 0x60 13778 13779 static boolean_t 13780 ip_rput_multimblk_ipoptions(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t **iphapp, 13781 ipaddr_t *dstp, ip_stack_t *ipst) 13782 { 13783 uint_t opt_len; 13784 ipha_t *ipha; 13785 ssize_t len; 13786 uint_t pkt_len; 13787 13788 ASSERT(ill != NULL); 13789 IP_STAT(ipst, ip_ipoptions); 13790 ipha = *iphapp; 13791 13792 #define rptr ((uchar_t *)ipha) 13793 /* Assume no IPv6 packets arrive over the IPv4 queue */ 13794 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 13795 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); 13796 freemsg(mp); 13797 return (B_FALSE); 13798 } 13799 13800 /* multiple mblk or too short */ 13801 pkt_len = ntohs(ipha->ipha_length); 13802 13803 /* Get the number of words of IP options in the IP header. */ 13804 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 13805 if (opt_len) { 13806 /* IP Options present! Validate and process. */ 13807 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 13808 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13809 goto done; 13810 } 13811 /* 13812 * Recompute complete header length and make sure we 13813 * have access to all of it. 13814 */ 13815 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 13816 if (len > (mp->b_wptr - rptr)) { 13817 if (len > pkt_len) { 13818 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13819 goto done; 13820 } 13821 if (!pullupmsg(mp, len)) { 13822 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13823 goto done; 13824 } 13825 ipha = (ipha_t *)mp->b_rptr; 13826 } 13827 /* 13828 * Go off to ip_rput_options which returns the next hop 13829 * destination address, which may have been affected 13830 * by source routing. 13831 */ 13832 IP_STAT(ipst, ip_opt); 13833 if (ip_rput_options(q, mp, ipha, dstp, ipst) == -1) { 13834 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13835 return (B_FALSE); 13836 } 13837 } 13838 *iphapp = ipha; 13839 return (B_TRUE); 13840 done: 13841 /* clear b_prev - used by ip_mroute_decap */ 13842 mp->b_prev = NULL; 13843 freemsg(mp); 13844 return (B_FALSE); 13845 #undef rptr 13846 } 13847 13848 /* 13849 * Deal with the fact that there is no ire for the destination. 13850 * The incoming ill (in_ill) is passed in to ip_newroute only 13851 * in the case of packets coming from mobile ip forward tunnel. 13852 * It must be null otherwise. 13853 */ 13854 static ire_t * 13855 ip_rput_noire(queue_t *q, ill_t *in_ill, mblk_t *mp, int ll_multicast, 13856 ipaddr_t dst) 13857 { 13858 ipha_t *ipha; 13859 ill_t *ill; 13860 ire_t *ire; 13861 boolean_t check_multirt = B_FALSE; 13862 ip_stack_t *ipst; 13863 13864 ipha = (ipha_t *)mp->b_rptr; 13865 ill = (ill_t *)q->q_ptr; 13866 13867 ASSERT(ill != NULL); 13868 ipst = ill->ill_ipst; 13869 13870 /* 13871 * No IRE for this destination, so it can't be for us. 13872 * Unless we are forwarding, drop the packet. 13873 * We have to let source routed packets through 13874 * since we don't yet know if they are 'ping -l' 13875 * packets i.e. if they will go out over the 13876 * same interface as they came in on. 13877 */ 13878 if (ll_multicast) { 13879 freemsg(mp); 13880 return (NULL); 13881 } 13882 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) { 13883 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13884 freemsg(mp); 13885 return (NULL); 13886 } 13887 13888 /* 13889 * Mark this packet as having originated externally. 13890 * 13891 * For non-forwarding code path, ire_send later double 13892 * checks this interface to see if it is still exists 13893 * post-ARP resolution. 13894 * 13895 * Also, IPQOS uses this to differentiate between 13896 * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP 13897 * QOS packet processing in ip_wput_attach_llhdr(). 13898 * The QoS module can mark the b_band for a fastpath message 13899 * or the dl_priority field in a unitdata_req header for 13900 * CoS marking. This info can only be found in 13901 * ip_wput_attach_llhdr(). 13902 */ 13903 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 13904 /* 13905 * Clear the indication that this may have a hardware checksum 13906 * as we are not using it 13907 */ 13908 DB_CKSUMFLAGS(mp) = 0; 13909 13910 if (in_ill != NULL) { 13911 /* 13912 * Now hand the packet to ip_newroute. 13913 */ 13914 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID, ipst); 13915 return (NULL); 13916 } 13917 ire = ire_forward(dst, &check_multirt, NULL, NULL, 13918 MBLK_GETLABEL(mp), ipst); 13919 13920 if (ire == NULL && check_multirt) { 13921 /* Let ip_newroute handle CGTP */ 13922 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID, ipst); 13923 return (NULL); 13924 } 13925 13926 if (ire != NULL) 13927 return (ire); 13928 13929 mp->b_prev = mp->b_next = 0; 13930 /* send icmp unreachable */ 13931 q = WR(q); 13932 /* Sent by forwarding path, and router is global zone */ 13933 if (ip_source_routed(ipha, ipst)) { 13934 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, 13935 GLOBAL_ZONEID, ipst); 13936 } else { 13937 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 13938 ipst); 13939 } 13940 13941 return (NULL); 13942 13943 } 13944 13945 /* 13946 * check ip header length and align it. 13947 */ 13948 static boolean_t 13949 ip_check_and_align_header(queue_t *q, mblk_t *mp, ip_stack_t *ipst) 13950 { 13951 ssize_t len; 13952 ill_t *ill; 13953 ipha_t *ipha; 13954 13955 len = MBLKL(mp); 13956 13957 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13958 ill = (ill_t *)q->q_ptr; 13959 13960 if (!OK_32PTR(mp->b_rptr)) 13961 IP_STAT(ipst, ip_notaligned1); 13962 else 13963 IP_STAT(ipst, ip_notaligned2); 13964 /* Guard against bogus device drivers */ 13965 if (len < 0) { 13966 /* clear b_prev - used by ip_mroute_decap */ 13967 mp->b_prev = NULL; 13968 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13969 freemsg(mp); 13970 return (B_FALSE); 13971 } 13972 13973 if (ip_rput_pullups++ == 0) { 13974 ipha = (ipha_t *)mp->b_rptr; 13975 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 13976 "ip_check_and_align_header: %s forced us to " 13977 " pullup pkt, hdr len %ld, hdr addr %p", 13978 ill->ill_name, len, ipha); 13979 } 13980 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 13981 /* clear b_prev - used by ip_mroute_decap */ 13982 mp->b_prev = NULL; 13983 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13984 freemsg(mp); 13985 return (B_FALSE); 13986 } 13987 } 13988 return (B_TRUE); 13989 } 13990 13991 static boolean_t 13992 ip_rput_notforus(queue_t **qp, mblk_t *mp, ire_t *ire, ill_t *ill) 13993 { 13994 ill_group_t *ill_group; 13995 ill_group_t *ire_group; 13996 queue_t *q; 13997 ill_t *ire_ill; 13998 uint_t ill_ifindex; 13999 ip_stack_t *ipst = ill->ill_ipst; 14000 14001 q = *qp; 14002 /* 14003 * We need to check to make sure the packet came in 14004 * on the queue associated with the destination IRE. 14005 * Note that for multicast packets and broadcast packets sent to 14006 * a broadcast address which is shared between multiple interfaces 14007 * we should not do this since we just got a random broadcast ire. 14008 */ 14009 if (ire->ire_rfq && ire->ire_type != IRE_BROADCAST) { 14010 boolean_t check_multi = B_TRUE; 14011 14012 /* 14013 * This packet came in on an interface other than the 14014 * one associated with the destination address. 14015 * "Gateway" it to the appropriate interface here. 14016 * As long as the ills belong to the same group, 14017 * we don't consider them to arriving on the wrong 14018 * interface. Thus, when the switch is doing inbound 14019 * load spreading, we won't drop packets when we 14020 * are doing strict multihoming checks. Note, the 14021 * same holds true for 'usesrc groups' where the 14022 * destination address may belong to another interface 14023 * to allow multipathing to happen 14024 */ 14025 ill_group = ill->ill_group; 14026 ire_ill = (ill_t *)(ire->ire_rfq)->q_ptr; 14027 ill_ifindex = ill->ill_usesrc_ifindex; 14028 ire_group = ire_ill->ill_group; 14029 14030 /* 14031 * If it's part of the same IPMP group, or if it's a legal 14032 * address on the 'usesrc' interface, then bypass strict 14033 * checks. 14034 */ 14035 if (ill_group != NULL && ill_group == ire_group) { 14036 check_multi = B_FALSE; 14037 } else if (ill_ifindex != 0 && 14038 ill_ifindex == ire_ill->ill_phyint->phyint_ifindex) { 14039 check_multi = B_FALSE; 14040 } 14041 14042 if (check_multi && 14043 ipst->ips_ip_strict_dst_multihoming && 14044 ((ill->ill_flags & 14045 ire->ire_ipif->ipif_ill->ill_flags & 14046 ILLF_ROUTER) == 0)) { 14047 /* Drop packet */ 14048 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14049 freemsg(mp); 14050 return (B_TRUE); 14051 } 14052 14053 /* 14054 * Change the queue (for non-virtual destination network 14055 * interfaces) and ip_rput_local will be called with the right 14056 * queue 14057 */ 14058 q = ire->ire_rfq; 14059 } 14060 /* Must be broadcast. We'll take it. */ 14061 *qp = q; 14062 return (B_FALSE); 14063 } 14064 14065 ire_t * 14066 ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) 14067 { 14068 ipha_t *ipha; 14069 ipaddr_t ip_dst, ip_src; 14070 ire_t *src_ire = NULL; 14071 ill_t *stq_ill; 14072 uint_t hlen; 14073 uint_t pkt_len; 14074 uint32_t sum; 14075 queue_t *dev_q; 14076 boolean_t check_multirt = B_FALSE; 14077 ip_stack_t *ipst = ill->ill_ipst; 14078 14079 ipha = (ipha_t *)mp->b_rptr; 14080 14081 /* 14082 * Martian Address Filtering [RFC 1812, Section 5.3.7] 14083 * The loopback address check for both src and dst has already 14084 * been checked in ip_input 14085 */ 14086 ip_dst = ntohl(dst); 14087 ip_src = ntohl(ipha->ipha_src); 14088 14089 if (ip_dst == INADDR_ANY || IN_BADCLASS(ip_dst) || 14090 IN_CLASSD(ip_src)) { 14091 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14092 goto drop; 14093 } 14094 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14095 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 14096 14097 if (src_ire != NULL) { 14098 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14099 goto drop; 14100 } 14101 14102 14103 /* No ire cache of nexthop. So first create one */ 14104 if (ire == NULL) { 14105 ire = ire_forward(dst, &check_multirt, NULL, NULL, NULL, ipst); 14106 /* 14107 * We only come to ip_fast_forward if ip_cgtp_filter is 14108 * is not set. So upon return from ire_forward 14109 * check_multirt should remain as false. 14110 */ 14111 ASSERT(!check_multirt); 14112 if (ire == NULL) { 14113 /* An attempt was made to forward the packet */ 14114 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14115 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14116 mp->b_prev = mp->b_next = 0; 14117 /* send icmp unreachable */ 14118 /* Sent by forwarding path, and router is global zone */ 14119 if (ip_source_routed(ipha, ipst)) { 14120 icmp_unreachable(ill->ill_wq, mp, 14121 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, 14122 ipst); 14123 } else { 14124 icmp_unreachable(ill->ill_wq, mp, 14125 ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 14126 ipst); 14127 } 14128 return (ire); 14129 } 14130 } 14131 14132 /* 14133 * Forwarding fastpath exception case: 14134 * If either of the follwoing case is true, we take 14135 * the slowpath 14136 * o forwarding is not enabled 14137 * o incoming and outgoing interface are the same, or the same 14138 * IPMP group 14139 * o corresponding ire is in incomplete state 14140 * o packet needs fragmentation 14141 * 14142 * The codeflow from here on is thus: 14143 * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 14144 */ 14145 pkt_len = ntohs(ipha->ipha_length); 14146 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 14147 if (!(stq_ill->ill_flags & ILLF_ROUTER) || 14148 !(ill->ill_flags & ILLF_ROUTER) || 14149 (ill == stq_ill) || 14150 (ill->ill_group != NULL && ill->ill_group == stq_ill->ill_group) || 14151 (ire->ire_nce == NULL) || 14152 (ire->ire_nce->nce_state != ND_REACHABLE) || 14153 (pkt_len > ire->ire_max_frag) || 14154 ipha->ipha_ttl <= 1) { 14155 ip_rput_process_forward(ill->ill_rq, mp, ire, 14156 ipha, ill, B_FALSE); 14157 return (ire); 14158 } 14159 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14160 14161 DTRACE_PROBE4(ip4__forwarding__start, 14162 ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 14163 14164 FW_HOOKS(ipst->ips_ip4_forwarding_event, 14165 ipst->ips_ipv4firewall_forwarding, 14166 ill, stq_ill, ipha, mp, mp, ipst); 14167 14168 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 14169 14170 if (mp == NULL) 14171 goto drop; 14172 14173 mp->b_datap->db_struioun.cksum.flags = 0; 14174 /* Adjust the checksum to reflect the ttl decrement. */ 14175 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 14176 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 14177 ipha->ipha_ttl--; 14178 14179 dev_q = ire->ire_stq->q_next; 14180 if ((dev_q->q_next != NULL || 14181 dev_q->q_first != NULL) && !canput(dev_q)) { 14182 goto indiscard; 14183 } 14184 14185 hlen = ire->ire_nce->nce_fp_mp != NULL ? 14186 MBLKL(ire->ire_nce->nce_fp_mp) : 0; 14187 14188 if (hlen != 0 || ire->ire_nce->nce_res_mp != NULL) { 14189 mblk_t *mpip = mp; 14190 14191 mp = ip_wput_attach_llhdr(mpip, ire, 0, 0); 14192 if (mp != NULL) { 14193 DTRACE_PROBE4(ip4__physical__out__start, 14194 ill_t *, NULL, ill_t *, stq_ill, 14195 ipha_t *, ipha, mblk_t *, mp); 14196 FW_HOOKS(ipst->ips_ip4_physical_out_event, 14197 ipst->ips_ipv4firewall_physical_out, 14198 NULL, stq_ill, ipha, mp, mpip, ipst); 14199 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, 14200 mp); 14201 if (mp == NULL) 14202 goto drop; 14203 14204 UPDATE_IB_PKT_COUNT(ire); 14205 ire->ire_last_used_time = lbolt; 14206 BUMP_MIB(stq_ill->ill_ip_mib, 14207 ipIfStatsHCOutForwDatagrams); 14208 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 14209 UPDATE_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutOctets, 14210 pkt_len); 14211 putnext(ire->ire_stq, mp); 14212 return (ire); 14213 } 14214 } 14215 14216 indiscard: 14217 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14218 drop: 14219 if (mp != NULL) 14220 freemsg(mp); 14221 if (src_ire != NULL) 14222 ire_refrele(src_ire); 14223 return (ire); 14224 14225 } 14226 14227 /* 14228 * This function is called in the forwarding slowpath, when 14229 * either the ire lacks the link-layer address, or the packet needs 14230 * further processing(eg. fragmentation), before transmission. 14231 */ 14232 14233 static void 14234 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14235 ill_t *ill, boolean_t ll_multicast) 14236 { 14237 ill_group_t *ill_group; 14238 ill_group_t *ire_group; 14239 queue_t *dev_q; 14240 ire_t *src_ire; 14241 ip_stack_t *ipst = ill->ill_ipst; 14242 14243 ASSERT(ire->ire_stq != NULL); 14244 14245 mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ 14246 mp->b_next = NULL; /* ip_rput_noire sets dst here */ 14247 14248 if (ll_multicast != 0) { 14249 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14250 goto drop_pkt; 14251 } 14252 14253 /* 14254 * check if ipha_src is a broadcast address. Note that this 14255 * check is redundant when we get here from ip_fast_forward() 14256 * which has already done this check. However, since we can 14257 * also get here from ip_rput_process_broadcast() or, for 14258 * for the slow path through ip_fast_forward(), we perform 14259 * the check again for code-reusability 14260 */ 14261 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14262 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 14263 if (src_ire != NULL || ntohl(ipha->ipha_dst) == INADDR_ANY || 14264 IN_BADCLASS(ntohl(ipha->ipha_dst))) { 14265 if (src_ire != NULL) 14266 ire_refrele(src_ire); 14267 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14268 ip2dbg(("ip_rput_process_forward: Received packet with" 14269 " bad src/dst address on %s\n", ill->ill_name)); 14270 goto drop_pkt; 14271 } 14272 14273 ill_group = ill->ill_group; 14274 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 14275 /* 14276 * Check if we want to forward this one at this time. 14277 * We allow source routed packets on a host provided that 14278 * they go out the same interface or same interface group 14279 * as they came in on. 14280 * 14281 * XXX To be quicker, we may wish to not chase pointers to 14282 * get the ILLF_ROUTER flag and instead store the 14283 * forwarding policy in the ire. An unfortunate 14284 * side-effect of that would be requiring an ire flush 14285 * whenever the ILLF_ROUTER flag changes. 14286 */ 14287 if (((ill->ill_flags & 14288 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 14289 ILLF_ROUTER) == 0) && 14290 !(ip_source_routed(ipha, ipst) && (ire->ire_rfq == q || 14291 (ill_group != NULL && ill_group == ire_group)))) { 14292 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14293 if (ip_source_routed(ipha, ipst)) { 14294 q = WR(q); 14295 /* 14296 * Clear the indication that this may have 14297 * hardware checksum as we are not using it. 14298 */ 14299 DB_CKSUMFLAGS(mp) = 0; 14300 /* Sent by forwarding path, and router is global zone */ 14301 icmp_unreachable(q, mp, 14302 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); 14303 return; 14304 } 14305 goto drop_pkt; 14306 } 14307 14308 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14309 14310 /* Packet is being forwarded. Turning off hwcksum flag. */ 14311 DB_CKSUMFLAGS(mp) = 0; 14312 if (ipst->ips_ip_g_send_redirects) { 14313 /* 14314 * Check whether the incoming interface and outgoing 14315 * interface is part of the same group. If so, 14316 * send redirects. 14317 * 14318 * Check the source address to see if it originated 14319 * on the same logical subnet it is going back out on. 14320 * If so, we should be able to send it a redirect. 14321 * Avoid sending a redirect if the destination 14322 * is directly connected (i.e., ipha_dst is the same 14323 * as ire_gateway_addr or the ire_addr of the 14324 * nexthop IRE_CACHE ), or if the packet was source 14325 * routed out this interface. 14326 */ 14327 ipaddr_t src, nhop; 14328 mblk_t *mp1; 14329 ire_t *nhop_ire = NULL; 14330 14331 /* 14332 * Check whether ire_rfq and q are from the same ill 14333 * or if they are not same, they at least belong 14334 * to the same group. If so, send redirects. 14335 */ 14336 if ((ire->ire_rfq == q || 14337 (ill_group != NULL && ill_group == ire_group)) && 14338 !ip_source_routed(ipha, ipst)) { 14339 14340 nhop = (ire->ire_gateway_addr != 0 ? 14341 ire->ire_gateway_addr : ire->ire_addr); 14342 14343 if (ipha->ipha_dst == nhop) { 14344 /* 14345 * We avoid sending a redirect if the 14346 * destination is directly connected 14347 * because it is possible that multiple 14348 * IP subnets may have been configured on 14349 * the link, and the source may not 14350 * be on the same subnet as ip destination, 14351 * even though they are on the same 14352 * physical link. 14353 */ 14354 goto sendit; 14355 } 14356 14357 src = ipha->ipha_src; 14358 14359 /* 14360 * We look up the interface ire for the nexthop, 14361 * to see if ipha_src is in the same subnet 14362 * as the nexthop. 14363 * 14364 * Note that, if, in the future, IRE_CACHE entries 14365 * are obsoleted, this lookup will not be needed, 14366 * as the ire passed to this function will be the 14367 * same as the nhop_ire computed below. 14368 */ 14369 nhop_ire = ire_ftable_lookup(nhop, 0, 0, 14370 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 14371 0, NULL, MATCH_IRE_TYPE, ipst); 14372 14373 if (nhop_ire != NULL) { 14374 if ((src & nhop_ire->ire_mask) == 14375 (nhop & nhop_ire->ire_mask)) { 14376 /* 14377 * The source is directly connected. 14378 * Just copy the ip header (which is 14379 * in the first mblk) 14380 */ 14381 mp1 = copyb(mp); 14382 if (mp1 != NULL) { 14383 icmp_send_redirect(WR(q), mp1, 14384 nhop, ipst); 14385 } 14386 } 14387 ire_refrele(nhop_ire); 14388 } 14389 } 14390 } 14391 sendit: 14392 dev_q = ire->ire_stq->q_next; 14393 if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { 14394 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14395 freemsg(mp); 14396 return; 14397 } 14398 14399 ip_rput_forward(ire, ipha, mp, ill); 14400 return; 14401 14402 drop_pkt: 14403 ip2dbg(("ip_rput_process_forward: drop pkt\n")); 14404 freemsg(mp); 14405 } 14406 14407 ire_t * 14408 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14409 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 14410 { 14411 queue_t *q; 14412 uint16_t hcksumflags; 14413 ip_stack_t *ipst = ill->ill_ipst; 14414 14415 q = *qp; 14416 14417 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts); 14418 14419 /* 14420 * Clear the indication that this may have hardware 14421 * checksum as we are not using it for forwarding. 14422 */ 14423 hcksumflags = DB_CKSUMFLAGS(mp); 14424 DB_CKSUMFLAGS(mp) = 0; 14425 14426 /* 14427 * Directed broadcast forwarding: if the packet came in over a 14428 * different interface then it is routed out over we can forward it. 14429 */ 14430 if (ipha->ipha_protocol == IPPROTO_TCP) { 14431 ire_refrele(ire); 14432 freemsg(mp); 14433 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14434 return (NULL); 14435 } 14436 /* 14437 * For multicast we have set dst to be INADDR_BROADCAST 14438 * for delivering to all STREAMS. IRE_MARK_NORECV is really 14439 * only for broadcast packets. 14440 */ 14441 if (!CLASSD(ipha->ipha_dst)) { 14442 ire_t *new_ire; 14443 ipif_t *ipif; 14444 /* 14445 * For ill groups, as the switch duplicates broadcasts 14446 * across all the ports, we need to filter out and 14447 * send up only one copy. There is one copy for every 14448 * broadcast address on each ill. Thus, we look for a 14449 * specific IRE on this ill and look at IRE_MARK_NORECV 14450 * later to see whether this ill is eligible to receive 14451 * them or not. ill_nominate_bcast_rcv() nominates only 14452 * one set of IREs for receiving. 14453 */ 14454 14455 ipif = ipif_get_next_ipif(NULL, ill); 14456 if (ipif == NULL) { 14457 ire_refrele(ire); 14458 freemsg(mp); 14459 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14460 return (NULL); 14461 } 14462 new_ire = ire_ctable_lookup(dst, 0, 0, 14463 ipif, ALL_ZONES, NULL, MATCH_IRE_ILL, ipst); 14464 ipif_refrele(ipif); 14465 14466 if (new_ire != NULL) { 14467 if (new_ire->ire_marks & IRE_MARK_NORECV) { 14468 ire_refrele(ire); 14469 ire_refrele(new_ire); 14470 freemsg(mp); 14471 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14472 return (NULL); 14473 } 14474 /* 14475 * In the special case of multirouted broadcast 14476 * packets, we unconditionally need to "gateway" 14477 * them to the appropriate interface here. 14478 * In the normal case, this cannot happen, because 14479 * there is no broadcast IRE tagged with the 14480 * RTF_MULTIRT flag. 14481 */ 14482 if (new_ire->ire_flags & RTF_MULTIRT) { 14483 ire_refrele(new_ire); 14484 if (ire->ire_rfq != NULL) { 14485 q = ire->ire_rfq; 14486 *qp = q; 14487 } 14488 } else { 14489 ire_refrele(ire); 14490 ire = new_ire; 14491 } 14492 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 14493 if (!ipst->ips_ip_g_forward_directed_bcast) { 14494 /* 14495 * Free the message if 14496 * ip_g_forward_directed_bcast is turned 14497 * off for non-local broadcast. 14498 */ 14499 ire_refrele(ire); 14500 freemsg(mp); 14501 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14502 return (NULL); 14503 } 14504 } else { 14505 /* 14506 * This CGTP packet successfully passed the 14507 * CGTP filter, but the related CGTP 14508 * broadcast IRE has not been found, 14509 * meaning that the redundant ipif is 14510 * probably down. However, if we discarded 14511 * this packet, its duplicate would be 14512 * filtered out by the CGTP filter so none 14513 * of them would get through. So we keep 14514 * going with this one. 14515 */ 14516 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 14517 if (ire->ire_rfq != NULL) { 14518 q = ire->ire_rfq; 14519 *qp = q; 14520 } 14521 } 14522 } 14523 if (ipst->ips_ip_g_forward_directed_bcast && ll_multicast == 0) { 14524 /* 14525 * Verify that there are not more then one 14526 * IRE_BROADCAST with this broadcast address which 14527 * has ire_stq set. 14528 * TODO: simplify, loop over all IRE's 14529 */ 14530 ire_t *ire1; 14531 int num_stq = 0; 14532 mblk_t *mp1; 14533 14534 /* Find the first one with ire_stq set */ 14535 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 14536 for (ire1 = ire; ire1 && 14537 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 14538 ire1 = ire1->ire_next) 14539 ; 14540 if (ire1) { 14541 ire_refrele(ire); 14542 ire = ire1; 14543 IRE_REFHOLD(ire); 14544 } 14545 14546 /* Check if there are additional ones with stq set */ 14547 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 14548 if (ire->ire_addr != ire1->ire_addr) 14549 break; 14550 if (ire1->ire_stq) { 14551 num_stq++; 14552 break; 14553 } 14554 } 14555 rw_exit(&ire->ire_bucket->irb_lock); 14556 if (num_stq == 1 && ire->ire_stq != NULL) { 14557 ip1dbg(("ip_rput_process_broadcast: directed " 14558 "broadcast to 0x%x\n", 14559 ntohl(ire->ire_addr))); 14560 mp1 = copymsg(mp); 14561 if (mp1) { 14562 switch (ipha->ipha_protocol) { 14563 case IPPROTO_UDP: 14564 ip_udp_input(q, mp1, ipha, ire, ill); 14565 break; 14566 default: 14567 ip_proto_input(q, mp1, ipha, ire, ill); 14568 break; 14569 } 14570 } 14571 /* 14572 * Adjust ttl to 2 (1+1 - the forward engine 14573 * will decrement it by one. 14574 */ 14575 if (ip_csum_hdr(ipha)) { 14576 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 14577 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 14578 freemsg(mp); 14579 ire_refrele(ire); 14580 return (NULL); 14581 } 14582 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1; 14583 ipha->ipha_hdr_checksum = 0; 14584 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14585 ip_rput_process_forward(q, mp, ire, ipha, 14586 ill, ll_multicast); 14587 ire_refrele(ire); 14588 return (NULL); 14589 } 14590 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 14591 ntohl(ire->ire_addr))); 14592 } 14593 14594 14595 /* Restore any hardware checksum flags */ 14596 DB_CKSUMFLAGS(mp) = hcksumflags; 14597 return (ire); 14598 } 14599 14600 /* ARGSUSED */ 14601 static boolean_t 14602 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 14603 int *ll_multicast, ipaddr_t *dstp) 14604 { 14605 ip_stack_t *ipst = ill->ill_ipst; 14606 14607 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts); 14608 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, 14609 ntohs(ipha->ipha_length)); 14610 14611 /* 14612 * Forward packets only if we have joined the allmulti 14613 * group on this interface. 14614 */ 14615 if (ipst->ips_ip_g_mrouter && ill->ill_join_allmulti) { 14616 int retval; 14617 14618 /* 14619 * Clear the indication that this may have hardware 14620 * checksum as we are not using it. 14621 */ 14622 DB_CKSUMFLAGS(mp) = 0; 14623 retval = ip_mforward(ill, ipha, mp); 14624 /* ip_mforward updates mib variables if needed */ 14625 /* clear b_prev - used by ip_mroute_decap */ 14626 mp->b_prev = NULL; 14627 14628 switch (retval) { 14629 case 0: 14630 /* 14631 * pkt is okay and arrived on phyint. 14632 * 14633 * If we are running as a multicast router 14634 * we need to see all IGMP and/or PIM packets. 14635 */ 14636 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 14637 (ipha->ipha_protocol == IPPROTO_PIM)) { 14638 goto done; 14639 } 14640 break; 14641 case -1: 14642 /* pkt is mal-formed, toss it */ 14643 goto drop_pkt; 14644 case 1: 14645 /* pkt is okay and arrived on a tunnel */ 14646 /* 14647 * If we are running a multicast router 14648 * we need to see all igmp packets. 14649 */ 14650 if (ipha->ipha_protocol == IPPROTO_IGMP) { 14651 *dstp = INADDR_BROADCAST; 14652 *ll_multicast = 1; 14653 return (B_FALSE); 14654 } 14655 14656 goto drop_pkt; 14657 } 14658 } 14659 14660 ILM_WALKER_HOLD(ill); 14661 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 14662 /* 14663 * This might just be caused by the fact that 14664 * multiple IP Multicast addresses map to the same 14665 * link layer multicast - no need to increment counter! 14666 */ 14667 ILM_WALKER_RELE(ill); 14668 freemsg(mp); 14669 return (B_TRUE); 14670 } 14671 ILM_WALKER_RELE(ill); 14672 done: 14673 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 14674 /* 14675 * This assumes the we deliver to all streams for multicast 14676 * and broadcast packets. 14677 */ 14678 *dstp = INADDR_BROADCAST; 14679 *ll_multicast = 1; 14680 return (B_FALSE); 14681 drop_pkt: 14682 ip2dbg(("ip_rput: drop pkt\n")); 14683 freemsg(mp); 14684 return (B_TRUE); 14685 } 14686 14687 static boolean_t 14688 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 14689 int *ll_multicast, mblk_t **mpp) 14690 { 14691 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 14692 boolean_t must_copy = B_FALSE; 14693 struct iocblk *iocp; 14694 ipha_t *ipha; 14695 ip_stack_t *ipst = ill->ill_ipst; 14696 14697 #define rptr ((uchar_t *)ipha) 14698 14699 first_mp = *first_mpp; 14700 mp = *mpp; 14701 14702 ASSERT(first_mp == mp); 14703 14704 /* 14705 * if db_ref > 1 then copymsg and free original. Packet may be 14706 * changed and do not want other entity who has a reference to this 14707 * message to trip over the changes. This is a blind change because 14708 * trying to catch all places that might change packet is too 14709 * difficult (since it may be a module above this one) 14710 * 14711 * This corresponds to the non-fast path case. We walk down the full 14712 * chain in this case, and check the db_ref count of all the dblks, 14713 * and do a copymsg if required. It is possible that the db_ref counts 14714 * of the data blocks in the mblk chain can be different. 14715 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 14716 * count of 1, followed by a M_DATA block with a ref count of 2, if 14717 * 'snoop' is running. 14718 */ 14719 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 14720 if (mp1->b_datap->db_ref > 1) { 14721 must_copy = B_TRUE; 14722 break; 14723 } 14724 } 14725 14726 if (must_copy) { 14727 mp1 = copymsg(mp); 14728 if (mp1 == NULL) { 14729 for (mp1 = mp; mp1 != NULL; 14730 mp1 = mp1->b_cont) { 14731 mp1->b_next = NULL; 14732 mp1->b_prev = NULL; 14733 } 14734 freemsg(mp); 14735 if (ill != NULL) { 14736 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14737 } else { 14738 BUMP_MIB(&ipst->ips_ip_mib, 14739 ipIfStatsInDiscards); 14740 } 14741 return (B_TRUE); 14742 } 14743 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 14744 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 14745 /* Copy b_prev - used by ip_mroute_decap */ 14746 to_mp->b_prev = from_mp->b_prev; 14747 from_mp->b_prev = NULL; 14748 } 14749 *first_mpp = first_mp = mp1; 14750 freemsg(mp); 14751 mp = mp1; 14752 *mpp = mp1; 14753 } 14754 14755 ipha = (ipha_t *)mp->b_rptr; 14756 14757 /* 14758 * previous code has a case for M_DATA. 14759 * We want to check how that happens. 14760 */ 14761 ASSERT(first_mp->b_datap->db_type != M_DATA); 14762 switch (first_mp->b_datap->db_type) { 14763 case M_PROTO: 14764 case M_PCPROTO: 14765 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 14766 DL_UNITDATA_IND) { 14767 /* Go handle anything other than data elsewhere. */ 14768 ip_rput_dlpi(q, mp); 14769 return (B_TRUE); 14770 } 14771 *ll_multicast = ((dl_unitdata_ind_t *)rptr)->dl_group_address; 14772 /* Ditch the DLPI header. */ 14773 mp1 = mp->b_cont; 14774 ASSERT(first_mp == mp); 14775 *first_mpp = mp1; 14776 freeb(mp); 14777 *mpp = mp1; 14778 return (B_FALSE); 14779 case M_IOCACK: 14780 ip1dbg(("got iocack ")); 14781 iocp = (struct iocblk *)mp->b_rptr; 14782 switch (iocp->ioc_cmd) { 14783 case DL_IOC_HDR_INFO: 14784 ill = (ill_t *)q->q_ptr; 14785 ill_fastpath_ack(ill, mp); 14786 return (B_TRUE); 14787 case SIOCSTUNPARAM: 14788 case OSIOCSTUNPARAM: 14789 /* Go through qwriter_ip */ 14790 break; 14791 case SIOCGTUNPARAM: 14792 case OSIOCGTUNPARAM: 14793 ip_rput_other(NULL, q, mp, NULL); 14794 return (B_TRUE); 14795 default: 14796 putnext(q, mp); 14797 return (B_TRUE); 14798 } 14799 /* FALLTHRU */ 14800 case M_ERROR: 14801 case M_HANGUP: 14802 /* 14803 * Since this is on the ill stream we unconditionally 14804 * bump up the refcount 14805 */ 14806 ill_refhold(ill); 14807 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, 14808 B_FALSE); 14809 return (B_TRUE); 14810 case M_CTL: 14811 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 14812 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 14813 IPHADA_M_CTL)) { 14814 /* 14815 * It's an IPsec accelerated packet. 14816 * Make sure that the ill from which we received the 14817 * packet has enabled IPsec hardware acceleration. 14818 */ 14819 if (!(ill->ill_capabilities & 14820 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 14821 /* IPsec kstats: bean counter */ 14822 freemsg(mp); 14823 return (B_TRUE); 14824 } 14825 14826 /* 14827 * Make mp point to the mblk following the M_CTL, 14828 * then process according to type of mp. 14829 * After this processing, first_mp will point to 14830 * the data-attributes and mp to the pkt following 14831 * the M_CTL. 14832 */ 14833 mp = first_mp->b_cont; 14834 if (mp == NULL) { 14835 freemsg(first_mp); 14836 return (B_TRUE); 14837 } 14838 /* 14839 * A Hardware Accelerated packet can only be M_DATA 14840 * ESP or AH packet. 14841 */ 14842 if (mp->b_datap->db_type != M_DATA) { 14843 /* non-M_DATA IPsec accelerated packet */ 14844 IPSECHW_DEBUG(IPSECHW_PKT, 14845 ("non-M_DATA IPsec accelerated pkt\n")); 14846 freemsg(first_mp); 14847 return (B_TRUE); 14848 } 14849 ipha = (ipha_t *)mp->b_rptr; 14850 if (ipha->ipha_protocol != IPPROTO_AH && 14851 ipha->ipha_protocol != IPPROTO_ESP) { 14852 IPSECHW_DEBUG(IPSECHW_PKT, 14853 ("non-M_DATA IPsec accelerated pkt\n")); 14854 freemsg(first_mp); 14855 return (B_TRUE); 14856 } 14857 *mpp = mp; 14858 return (B_FALSE); 14859 } 14860 putnext(q, mp); 14861 return (B_TRUE); 14862 case M_FLUSH: 14863 if (*mp->b_rptr & FLUSHW) { 14864 *mp->b_rptr &= ~FLUSHR; 14865 qreply(q, mp); 14866 return (B_TRUE); 14867 } 14868 freemsg(mp); 14869 return (B_TRUE); 14870 case M_IOCNAK: 14871 ip1dbg(("got iocnak ")); 14872 iocp = (struct iocblk *)mp->b_rptr; 14873 switch (iocp->ioc_cmd) { 14874 case DL_IOC_HDR_INFO: 14875 case SIOCSTUNPARAM: 14876 case OSIOCSTUNPARAM: 14877 /* 14878 * Since this is on the ill stream we unconditionally 14879 * bump up the refcount 14880 */ 14881 ill_refhold(ill); 14882 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, 14883 CUR_OP, B_FALSE); 14884 return (B_TRUE); 14885 case SIOCGTUNPARAM: 14886 case OSIOCGTUNPARAM: 14887 ip_rput_other(NULL, q, mp, NULL); 14888 return (B_TRUE); 14889 default: 14890 break; 14891 } 14892 /* FALLTHRU */ 14893 default: 14894 putnext(q, mp); 14895 return (B_TRUE); 14896 } 14897 } 14898 14899 /* Read side put procedure. Packets coming from the wire arrive here. */ 14900 void 14901 ip_rput(queue_t *q, mblk_t *mp) 14902 { 14903 ill_t *ill; 14904 ip_stack_t *ipst; 14905 14906 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 14907 14908 ill = (ill_t *)q->q_ptr; 14909 ipst = ill->ill_ipst; 14910 14911 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 14912 union DL_primitives *dl; 14913 14914 /* 14915 * Things are opening or closing. Only accept DLPI control 14916 * messages. In the open case, the ill->ill_ipif has not yet 14917 * been created. In the close case, things hanging off the 14918 * ill could have been freed already. In either case it 14919 * may not be safe to proceed further. 14920 */ 14921 14922 dl = (union DL_primitives *)mp->b_rptr; 14923 if ((mp->b_datap->db_type != M_PCPROTO) || 14924 (dl->dl_primitive == DL_UNITDATA_IND)) { 14925 /* 14926 * Also SIOC[GS]TUN* ioctls can come here. 14927 */ 14928 inet_freemsg(mp); 14929 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14930 "ip_input_end: q %p (%S)", q, "uninit"); 14931 return; 14932 } 14933 } 14934 14935 /* 14936 * if db_ref > 1 then copymsg and free original. Packet may be 14937 * changed and we do not want the other entity who has a reference to 14938 * this message to trip over the changes. This is a blind change because 14939 * trying to catch all places that might change the packet is too 14940 * difficult. 14941 * 14942 * This corresponds to the fast path case, where we have a chain of 14943 * M_DATA mblks. We check the db_ref count of only the 1st data block 14944 * in the mblk chain. There doesn't seem to be a reason why a device 14945 * driver would send up data with varying db_ref counts in the mblk 14946 * chain. In any case the Fast path is a private interface, and our 14947 * drivers don't do such a thing. Given the above assumption, there is 14948 * no need to walk down the entire mblk chain (which could have a 14949 * potential performance problem) 14950 */ 14951 if (mp->b_datap->db_ref > 1) { 14952 mblk_t *mp1; 14953 boolean_t adjusted = B_FALSE; 14954 IP_STAT(ipst, ip_db_ref); 14955 14956 /* 14957 * The IP_RECVSLLA option depends on having the link layer 14958 * header. First check that: 14959 * a> the underlying device is of type ether, since this 14960 * option is currently supported only over ethernet. 14961 * b> there is enough room to copy over the link layer header. 14962 * 14963 * Once the checks are done, adjust rptr so that the link layer 14964 * header will be copied via copymsg. Note that, IFT_ETHER may 14965 * be returned by some non-ethernet drivers but in this case the 14966 * second check will fail. 14967 */ 14968 if (ill->ill_type == IFT_ETHER && 14969 (mp->b_rptr - mp->b_datap->db_base) >= 14970 sizeof (struct ether_header)) { 14971 mp->b_rptr -= sizeof (struct ether_header); 14972 adjusted = B_TRUE; 14973 } 14974 mp1 = copymsg(mp); 14975 if (mp1 == NULL) { 14976 mp->b_next = NULL; 14977 /* clear b_prev - used by ip_mroute_decap */ 14978 mp->b_prev = NULL; 14979 freemsg(mp); 14980 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14981 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14982 "ip_rput_end: q %p (%S)", q, "copymsg"); 14983 return; 14984 } 14985 if (adjusted) { 14986 /* 14987 * Copy is done. Restore the pointer in the _new_ mblk 14988 */ 14989 mp1->b_rptr += sizeof (struct ether_header); 14990 } 14991 /* Copy b_prev - used by ip_mroute_decap */ 14992 mp1->b_prev = mp->b_prev; 14993 mp->b_prev = NULL; 14994 freemsg(mp); 14995 mp = mp1; 14996 } 14997 14998 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14999 "ip_rput_end: q %p (%S)", q, "end"); 15000 15001 ip_input(ill, NULL, mp, NULL); 15002 } 15003 15004 /* 15005 * Direct read side procedure capable of dealing with chains. GLDv3 based 15006 * drivers call this function directly with mblk chains while STREAMS 15007 * read side procedure ip_rput() calls this for single packet with ip_ring 15008 * set to NULL to process one packet at a time. 15009 * 15010 * The ill will always be valid if this function is called directly from 15011 * the driver. 15012 * 15013 * If ip_input() is called from GLDv3: 15014 * 15015 * - This must be a non-VLAN IP stream. 15016 * - 'mp' is either an untagged or a special priority-tagged packet. 15017 * - Any VLAN tag that was in the MAC header has been stripped. 15018 * 15019 * If the IP header in packet is not 32-bit aligned, every message in the 15020 * chain will be aligned before further operations. This is required on SPARC 15021 * platform. 15022 */ 15023 /* ARGSUSED */ 15024 void 15025 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, 15026 struct mac_header_info_s *mhip) 15027 { 15028 ipaddr_t dst = NULL; 15029 ipaddr_t prev_dst; 15030 ire_t *ire = NULL; 15031 ipha_t *ipha; 15032 uint_t pkt_len; 15033 ssize_t len; 15034 uint_t opt_len; 15035 int ll_multicast; 15036 int cgtp_flt_pkt; 15037 queue_t *q = ill->ill_rq; 15038 squeue_t *curr_sqp = NULL; 15039 mblk_t *head = NULL; 15040 mblk_t *tail = NULL; 15041 mblk_t *first_mp; 15042 mblk_t *mp; 15043 mblk_t *dmp; 15044 int cnt = 0; 15045 ip_stack_t *ipst = ill->ill_ipst; 15046 15047 ASSERT(mp_chain != NULL); 15048 ASSERT(ill != NULL); 15049 15050 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 15051 15052 #define rptr ((uchar_t *)ipha) 15053 15054 while (mp_chain != NULL) { 15055 first_mp = mp = mp_chain; 15056 mp_chain = mp_chain->b_next; 15057 mp->b_next = NULL; 15058 ll_multicast = 0; 15059 15060 /* 15061 * We do ire caching from one iteration to 15062 * another. In the event the packet chain contains 15063 * all packets from the same dst, this caching saves 15064 * an ire_cache_lookup for each of the succeeding 15065 * packets in a packet chain. 15066 */ 15067 prev_dst = dst; 15068 15069 /* 15070 * Check and align the IP header. 15071 */ 15072 if (DB_TYPE(mp) == M_DATA) { 15073 dmp = mp; 15074 } else if (DB_TYPE(mp) == M_PROTO && 15075 *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { 15076 dmp = mp->b_cont; 15077 } else { 15078 dmp = NULL; 15079 } 15080 if (dmp != NULL) { 15081 /* 15082 * IP header ptr not aligned? 15083 * OR IP header not complete in first mblk 15084 */ 15085 if (!OK_32PTR(dmp->b_rptr) || 15086 MBLKL(dmp) < IP_SIMPLE_HDR_LENGTH) { 15087 if (!ip_check_and_align_header(q, dmp, ipst)) 15088 continue; 15089 } 15090 } 15091 15092 /* 15093 * ip_input fast path 15094 */ 15095 15096 /* mblk type is not M_DATA */ 15097 if (DB_TYPE(mp) != M_DATA) { 15098 if (ip_rput_process_notdata(q, &first_mp, ill, 15099 &ll_multicast, &mp)) 15100 continue; 15101 } 15102 15103 /* Make sure its an M_DATA and that its aligned */ 15104 ASSERT(DB_TYPE(mp) == M_DATA); 15105 ASSERT(DB_REF(mp) == 1 && OK_32PTR(mp->b_rptr)); 15106 15107 ipha = (ipha_t *)mp->b_rptr; 15108 len = mp->b_wptr - rptr; 15109 pkt_len = ntohs(ipha->ipha_length); 15110 15111 /* 15112 * We must count all incoming packets, even if they end 15113 * up being dropped later on. 15114 */ 15115 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 15116 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); 15117 15118 /* multiple mblk or too short */ 15119 len -= pkt_len; 15120 if (len != 0) { 15121 /* 15122 * Make sure we have data length consistent 15123 * with the IP header. 15124 */ 15125 if (mp->b_cont == NULL) { 15126 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15127 BUMP_MIB(ill->ill_ip_mib, 15128 ipIfStatsInHdrErrors); 15129 ip2dbg(("ip_input: drop pkt\n")); 15130 freemsg(mp); 15131 continue; 15132 } 15133 mp->b_wptr = rptr + pkt_len; 15134 } else if ((len += msgdsize(mp->b_cont)) != 0) { 15135 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15136 BUMP_MIB(ill->ill_ip_mib, 15137 ipIfStatsInHdrErrors); 15138 ip2dbg(("ip_input: drop pkt\n")); 15139 freemsg(mp); 15140 continue; 15141 } 15142 (void) adjmsg(mp, -len); 15143 IP_STAT(ipst, ip_multimblk3); 15144 } 15145 } 15146 15147 /* Obtain the dst of the current packet */ 15148 dst = ipha->ipha_dst; 15149 15150 if (IP_LOOPBACK_ADDR(dst) || 15151 IP_LOOPBACK_ADDR(ipha->ipha_src)) { 15152 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors); 15153 cmn_err(CE_CONT, "dst %X src %X\n", 15154 dst, ipha->ipha_src); 15155 freemsg(mp); 15156 continue; 15157 } 15158 15159 /* 15160 * The event for packets being received from a 'physical' 15161 * interface is placed after validation of the source and/or 15162 * destination address as being local so that packets can be 15163 * redirected to loopback addresses using ipnat. 15164 */ 15165 DTRACE_PROBE4(ip4__physical__in__start, 15166 ill_t *, ill, ill_t *, NULL, 15167 ipha_t *, ipha, mblk_t *, first_mp); 15168 15169 FW_HOOKS(ipst->ips_ip4_physical_in_event, 15170 ipst->ips_ipv4firewall_physical_in, 15171 ill, NULL, ipha, first_mp, mp, ipst); 15172 15173 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 15174 15175 if (first_mp == NULL) { 15176 continue; 15177 } 15178 dst = ipha->ipha_dst; 15179 15180 /* 15181 * Attach any necessary label information to 15182 * this packet 15183 */ 15184 if (is_system_labeled() && 15185 !tsol_get_pkt_label(mp, IPV4_VERSION)) { 15186 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 15187 freemsg(mp); 15188 continue; 15189 } 15190 15191 /* 15192 * Reuse the cached ire only if the ipha_dst of the previous 15193 * packet is the same as the current packet AND it is not 15194 * INADDR_ANY. 15195 */ 15196 if (!(dst == prev_dst && dst != INADDR_ANY) && 15197 (ire != NULL)) { 15198 ire_refrele(ire); 15199 ire = NULL; 15200 } 15201 opt_len = ipha->ipha_version_and_hdr_length - 15202 IP_SIMPLE_HDR_VERSION; 15203 15204 /* 15205 * Check to see if we can take the fastpath. 15206 * That is possible if the following conditions are met 15207 * o Tsol disabled 15208 * o CGTP disabled 15209 * o ipp_action_count is 0 15210 * o Mobile IP not running 15211 * o no options in the packet 15212 * o not a RSVP packet 15213 * o not a multicast packet 15214 */ 15215 if (!is_system_labeled() && 15216 !ip_cgtp_filter && ipp_action_count == 0 && 15217 ill->ill_mrtun_refcnt == 0 && ill->ill_srcif_refcnt == 0 && 15218 opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && 15219 !ll_multicast && !CLASSD(dst)) { 15220 if (ire == NULL) 15221 ire = ire_cache_lookup(dst, ALL_ZONES, NULL, 15222 ipst); 15223 15224 /* incoming packet is for forwarding */ 15225 if (ire == NULL || (ire->ire_type & IRE_CACHE)) { 15226 ire = ip_fast_forward(ire, dst, ill, mp); 15227 continue; 15228 } 15229 /* incoming packet is for local consumption */ 15230 if (ire->ire_type & IRE_LOCAL) 15231 goto local; 15232 } 15233 15234 /* 15235 * Disable ire caching for anything more complex 15236 * than the simple fast path case we checked for above. 15237 */ 15238 if (ire != NULL) { 15239 ire_refrele(ire); 15240 ire = NULL; 15241 } 15242 15243 /* Full-blown slow path */ 15244 if (opt_len != 0) { 15245 if (len != 0) 15246 IP_STAT(ipst, ip_multimblk4); 15247 else 15248 IP_STAT(ipst, ip_ipoptions); 15249 if (!ip_rput_multimblk_ipoptions(q, ill, mp, &ipha, 15250 &dst, ipst)) 15251 continue; 15252 } 15253 15254 /* 15255 * Invoke the CGTP (multirouting) filtering module to process 15256 * the incoming packet. Packets identified as duplicates 15257 * must be discarded. Filtering is active only if the 15258 * the ip_cgtp_filter ndd variable is non-zero. 15259 * 15260 * Only applies to the shared stack since the filter_ops 15261 * do not carry an ip_stack_t or zoneid. 15262 */ 15263 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 15264 if (ip_cgtp_filter && (ip_cgtp_filter_ops != NULL) && 15265 ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) { 15266 cgtp_flt_pkt = 15267 ip_cgtp_filter_ops->cfo_filter(q, mp); 15268 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 15269 freemsg(first_mp); 15270 continue; 15271 } 15272 } 15273 15274 /* 15275 * If rsvpd is running, let RSVP daemon handle its processing 15276 * and forwarding of RSVP multicast/unicast packets. 15277 * If rsvpd is not running but mrouted is running, RSVP 15278 * multicast packets are forwarded as multicast traffic 15279 * and RSVP unicast packets are forwarded by unicast router. 15280 * If neither rsvpd nor mrouted is running, RSVP multicast 15281 * packets are not forwarded, but the unicast packets are 15282 * forwarded like unicast traffic. 15283 */ 15284 if (ipha->ipha_protocol == IPPROTO_RSVP && 15285 ipst->ips_ipcl_proto_fanout[IPPROTO_RSVP].connf_head != 15286 NULL) { 15287 /* RSVP packet and rsvpd running. Treat as ours */ 15288 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 15289 /* 15290 * This assumes that we deliver to all streams for 15291 * multicast and broadcast packets. 15292 * We have to force ll_multicast to 1 to handle the 15293 * M_DATA messages passed in from ip_mroute_decap. 15294 */ 15295 dst = INADDR_BROADCAST; 15296 ll_multicast = 1; 15297 } else if (CLASSD(dst)) { 15298 /* packet is multicast */ 15299 mp->b_next = NULL; 15300 if (ip_rput_process_multicast(q, mp, ill, ipha, 15301 &ll_multicast, &dst)) 15302 continue; 15303 } 15304 15305 15306 /* 15307 * Check if the packet is coming from the Mobile IP 15308 * forward tunnel interface 15309 */ 15310 if (ill->ill_srcif_refcnt > 0) { 15311 ire = ire_srcif_table_lookup(dst, IRE_INTERFACE, 15312 NULL, ill, MATCH_IRE_TYPE); 15313 if (ire != NULL && ire->ire_nce->nce_res_mp == NULL && 15314 ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER) { 15315 15316 /* We need to resolve the link layer info */ 15317 ire_refrele(ire); 15318 ire = NULL; 15319 (void) ip_rput_noire(q, (ill_t *)q->q_ptr, mp, 15320 ll_multicast, dst); 15321 continue; 15322 } 15323 } 15324 15325 if (ire == NULL) { 15326 ire = ire_cache_lookup(dst, ALL_ZONES, 15327 MBLK_GETLABEL(mp), ipst); 15328 } 15329 15330 /* 15331 * If mipagent is running and reverse tunnel is created as per 15332 * mobile node request, then any packet coming through the 15333 * incoming interface from the mobile-node, should be reverse 15334 * tunneled to it's home agent except those that are destined 15335 * to foreign agent only. 15336 * This needs source address based ire lookup. The routing 15337 * entries for source address based lookup are only created by 15338 * mipagent program only when a reverse tunnel is created. 15339 * Reference : RFC2002, RFC2344 15340 */ 15341 if (ill->ill_mrtun_refcnt > 0) { 15342 ipaddr_t srcaddr; 15343 ire_t *tmp_ire; 15344 15345 tmp_ire = ire; /* Save, we might need it later */ 15346 if (ire == NULL || (ire->ire_type != IRE_LOCAL && 15347 ire->ire_type != IRE_BROADCAST)) { 15348 srcaddr = ipha->ipha_src; 15349 ire = ire_mrtun_lookup(srcaddr, ill); 15350 if (ire != NULL) { 15351 /* 15352 * Should not be getting iphada packet 15353 * here. we should only get those for 15354 * IRE_LOCAL traffic, excluded above. 15355 * Fail-safe (drop packet) in the event 15356 * hardware is misbehaving. 15357 */ 15358 if (first_mp != mp) { 15359 /* IPsec KSTATS: beancount me */ 15360 freemsg(first_mp); 15361 } else { 15362 /* 15363 * This packet must be forwarded 15364 * to Reverse Tunnel 15365 */ 15366 ip_mrtun_forward(ire, ill, mp); 15367 } 15368 ire_refrele(ire); 15369 ire = NULL; 15370 if (tmp_ire != NULL) { 15371 ire_refrele(tmp_ire); 15372 tmp_ire = NULL; 15373 } 15374 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15375 "ip_input_end: q %p (%S)", 15376 q, "uninit"); 15377 continue; 15378 } 15379 } 15380 /* 15381 * If this packet is from a non-mobilenode or a 15382 * mobile-node which does not request reverse 15383 * tunnel service 15384 */ 15385 ire = tmp_ire; 15386 } 15387 15388 15389 /* 15390 * If we reach here that means the incoming packet satisfies 15391 * one of the following conditions: 15392 * - packet is from a mobile node which does not request 15393 * reverse tunnel 15394 * - packet is from a non-mobile node, which is the most 15395 * common case 15396 * - packet is from a reverse tunnel enabled mobile node 15397 * and destined to foreign agent only 15398 */ 15399 15400 if (ire == NULL) { 15401 /* 15402 * No IRE for this destination, so it can't be for us. 15403 * Unless we are forwarding, drop the packet. 15404 * We have to let source routed packets through 15405 * since we don't yet know if they are 'ping -l' 15406 * packets i.e. if they will go out over the 15407 * same interface as they came in on. 15408 */ 15409 ire = ip_rput_noire(q, NULL, mp, ll_multicast, dst); 15410 if (ire == NULL) 15411 continue; 15412 } 15413 15414 /* 15415 * Broadcast IRE may indicate either broadcast or 15416 * multicast packet 15417 */ 15418 if (ire->ire_type == IRE_BROADCAST) { 15419 /* 15420 * Skip broadcast checks if packet is UDP multicast; 15421 * we'd rather not enter ip_rput_process_broadcast() 15422 * unless the packet is broadcast for real, since 15423 * that routine is a no-op for multicast. 15424 */ 15425 if (ipha->ipha_protocol != IPPROTO_UDP || 15426 !CLASSD(ipha->ipha_dst)) { 15427 ire = ip_rput_process_broadcast(&q, mp, 15428 ire, ipha, ill, dst, cgtp_flt_pkt, 15429 ll_multicast); 15430 if (ire == NULL) 15431 continue; 15432 } 15433 } else if (ire->ire_stq != NULL) { 15434 /* fowarding? */ 15435 ip_rput_process_forward(q, mp, ire, ipha, ill, 15436 ll_multicast); 15437 /* ip_rput_process_forward consumed the packet */ 15438 continue; 15439 } 15440 15441 local: 15442 /* packet not for us */ 15443 if (ire->ire_rfq != q) { 15444 if (ip_rput_notforus(&q, mp, ire, ill)) 15445 continue; 15446 } 15447 15448 switch (ipha->ipha_protocol) { 15449 case IPPROTO_TCP: 15450 ASSERT(first_mp == mp); 15451 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 15452 mp, 0, q, ip_ring)) != NULL) { 15453 if (curr_sqp == NULL) { 15454 curr_sqp = GET_SQUEUE(mp); 15455 ASSERT(cnt == 0); 15456 cnt++; 15457 head = tail = mp; 15458 } else if (curr_sqp == GET_SQUEUE(mp)) { 15459 ASSERT(tail != NULL); 15460 cnt++; 15461 tail->b_next = mp; 15462 tail = mp; 15463 } else { 15464 /* 15465 * A different squeue. Send the 15466 * chain for the previous squeue on 15467 * its way. This shouldn't happen 15468 * often unless interrupt binding 15469 * changes. 15470 */ 15471 IP_STAT(ipst, ip_input_multi_squeue); 15472 squeue_enter_chain(curr_sqp, head, 15473 tail, cnt, SQTAG_IP_INPUT); 15474 curr_sqp = GET_SQUEUE(mp); 15475 head = mp; 15476 tail = mp; 15477 cnt = 1; 15478 } 15479 } 15480 continue; 15481 case IPPROTO_UDP: 15482 ASSERT(first_mp == mp); 15483 ip_udp_input(q, mp, ipha, ire, ill); 15484 continue; 15485 case IPPROTO_SCTP: 15486 ASSERT(first_mp == mp); 15487 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 15488 q, dst); 15489 /* ire has been released by ip_sctp_input */ 15490 ire = NULL; 15491 continue; 15492 default: 15493 ip_proto_input(q, first_mp, ipha, ire, ill); 15494 continue; 15495 } 15496 } 15497 15498 if (ire != NULL) 15499 ire_refrele(ire); 15500 15501 if (head != NULL) 15502 squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); 15503 15504 /* 15505 * This code is there just to make netperf/ttcp look good. 15506 * 15507 * Its possible that after being in polling mode (and having cleared 15508 * the backlog), squeues have turned the interrupt frequency higher 15509 * to improve latency at the expense of more CPU utilization (less 15510 * packets per interrupts or more number of interrupts). Workloads 15511 * like ttcp/netperf do manage to tickle polling once in a while 15512 * but for the remaining time, stay in higher interrupt mode since 15513 * their packet arrival rate is pretty uniform and this shows up 15514 * as higher CPU utilization. Since people care about CPU utilization 15515 * while running netperf/ttcp, turn the interrupt frequency back to 15516 * normal/default if polling has not been used in ip_poll_normal_ticks. 15517 */ 15518 if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { 15519 if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { 15520 ip_ring->rr_poll_state &= ~ILL_POLLING; 15521 ip_ring->rr_blank(ip_ring->rr_handle, 15522 ip_ring->rr_normal_blank_time, 15523 ip_ring->rr_normal_pkt_cnt); 15524 } 15525 } 15526 15527 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15528 "ip_input_end: q %p (%S)", q, "end"); 15529 #undef rptr 15530 } 15531 15532 static void 15533 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 15534 t_uscalar_t err) 15535 { 15536 if (dl_err == DL_SYSERR) { 15537 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15538 "%s: %s failed: DL_SYSERR (errno %u)\n", 15539 ill->ill_name, dlpi_prim_str(prim), err); 15540 return; 15541 } 15542 15543 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15544 "%s: %s failed: %s\n", ill->ill_name, dlpi_prim_str(prim), 15545 dlpi_err_str(dl_err)); 15546 } 15547 15548 /* 15549 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 15550 * than DL_UNITDATA_IND messages. If we need to process this message 15551 * exclusively, we call qwriter_ip, in which case we also need to call 15552 * ill_refhold before that, since qwriter_ip does an ill_refrele. 15553 */ 15554 void 15555 ip_rput_dlpi(queue_t *q, mblk_t *mp) 15556 { 15557 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15558 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15559 ill_t *ill; 15560 15561 ip1dbg(("ip_rput_dlpi")); 15562 ill = (ill_t *)q->q_ptr; 15563 switch (dloa->dl_primitive) { 15564 case DL_ERROR_ACK: 15565 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK %s (0x%x): " 15566 "%s (0x%x), unix %u\n", ill->ill_name, 15567 dlpi_prim_str(dlea->dl_error_primitive), 15568 dlea->dl_error_primitive, 15569 dlpi_err_str(dlea->dl_errno), 15570 dlea->dl_errno, 15571 dlea->dl_unix_errno)); 15572 switch (dlea->dl_error_primitive) { 15573 case DL_UNBIND_REQ: 15574 mutex_enter(&ill->ill_lock); 15575 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15576 cv_signal(&ill->ill_cv); 15577 mutex_exit(&ill->ill_lock); 15578 /* FALLTHRU */ 15579 case DL_NOTIFY_REQ: 15580 case DL_ATTACH_REQ: 15581 case DL_DETACH_REQ: 15582 case DL_INFO_REQ: 15583 case DL_BIND_REQ: 15584 case DL_ENABMULTI_REQ: 15585 case DL_PHYS_ADDR_REQ: 15586 case DL_CAPABILITY_REQ: 15587 case DL_CONTROL_REQ: 15588 /* 15589 * Refhold the ill to match qwriter_ip which does a 15590 * refrele. Since this is on the ill stream we 15591 * unconditionally bump up the refcount without 15592 * checking for ILL_CAN_LOOKUP 15593 */ 15594 ill_refhold(ill); 15595 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15596 CUR_OP, B_FALSE); 15597 return; 15598 case DL_DISABMULTI_REQ: 15599 freemsg(mp); /* Don't want to pass this up */ 15600 return; 15601 default: 15602 break; 15603 } 15604 ip_dlpi_error(ill, dlea->dl_error_primitive, 15605 dlea->dl_errno, dlea->dl_unix_errno); 15606 freemsg(mp); 15607 return; 15608 case DL_INFO_ACK: 15609 case DL_BIND_ACK: 15610 case DL_PHYS_ADDR_ACK: 15611 case DL_NOTIFY_ACK: 15612 case DL_CAPABILITY_ACK: 15613 case DL_CONTROL_ACK: 15614 /* 15615 * Refhold the ill to match qwriter_ip which does a refrele 15616 * Since this is on the ill stream we unconditionally 15617 * bump up the refcount without doing ILL_CAN_LOOKUP. 15618 */ 15619 ill_refhold(ill); 15620 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15621 CUR_OP, B_FALSE); 15622 return; 15623 case DL_NOTIFY_IND: 15624 ill_refhold(ill); 15625 /* 15626 * The DL_NOTIFY_IND is an asynchronous message that has no 15627 * relation to the current ioctl in progress (if any). Hence we 15628 * pass in NEW_OP in this case. 15629 */ 15630 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15631 NEW_OP, B_FALSE); 15632 return; 15633 case DL_OK_ACK: 15634 ip1dbg(("ip_rput: DL_OK_ACK for %s\n", 15635 dlpi_prim_str((int)dloa->dl_correct_primitive))); 15636 switch (dloa->dl_correct_primitive) { 15637 case DL_UNBIND_REQ: 15638 mutex_enter(&ill->ill_lock); 15639 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15640 cv_signal(&ill->ill_cv); 15641 mutex_exit(&ill->ill_lock); 15642 /* FALLTHRU */ 15643 case DL_ATTACH_REQ: 15644 case DL_DETACH_REQ: 15645 /* 15646 * Refhold the ill to match qwriter_ip which does a 15647 * refrele. Since this is on the ill stream we 15648 * unconditionally bump up the refcount 15649 */ 15650 ill_refhold(ill); 15651 qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15652 CUR_OP, B_FALSE); 15653 return; 15654 case DL_ENABMULTI_REQ: 15655 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15656 ill->ill_dlpi_multicast_state = IDS_OK; 15657 break; 15658 15659 } 15660 break; 15661 default: 15662 break; 15663 } 15664 freemsg(mp); 15665 } 15666 15667 /* 15668 * Handling of DLPI messages that require exclusive access to the ipsq. 15669 * 15670 * Need to do ill_pending_mp_release on ioctl completion, which could 15671 * happen here. (along with mi_copy_done) 15672 */ 15673 /* ARGSUSED */ 15674 static void 15675 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15676 { 15677 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15678 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15679 int err = 0; 15680 ill_t *ill; 15681 ipif_t *ipif = NULL; 15682 mblk_t *mp1 = NULL; 15683 conn_t *connp = NULL; 15684 t_uscalar_t paddrreq; 15685 mblk_t *mp_hw; 15686 boolean_t success; 15687 boolean_t ioctl_aborted = B_FALSE; 15688 boolean_t log = B_TRUE; 15689 hook_nic_event_t *info; 15690 ip_stack_t *ipst; 15691 15692 ip1dbg(("ip_rput_dlpi_writer ..")); 15693 ill = (ill_t *)q->q_ptr; 15694 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 15695 15696 ASSERT(IAM_WRITER_ILL(ill)); 15697 15698 ipst = ill->ill_ipst; 15699 15700 /* 15701 * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. 15702 * both are null or non-null. However we can assert that only 15703 * after grabbing the ipsq_lock. So we don't make any assertion 15704 * here and in other places in the code. 15705 */ 15706 ipif = ipsq->ipsq_pending_ipif; 15707 /* 15708 * The current ioctl could have been aborted by the user and a new 15709 * ioctl to bring up another ill could have started. We could still 15710 * get a response from the driver later. 15711 */ 15712 if (ipif != NULL && ipif->ipif_ill != ill) 15713 ioctl_aborted = B_TRUE; 15714 15715 switch (dloa->dl_primitive) { 15716 case DL_ERROR_ACK: 15717 switch (dlea->dl_error_primitive) { 15718 case DL_UNBIND_REQ: 15719 case DL_ATTACH_REQ: 15720 case DL_DETACH_REQ: 15721 case DL_INFO_REQ: 15722 ill_dlpi_done(ill, dlea->dl_error_primitive); 15723 break; 15724 case DL_NOTIFY_REQ: 15725 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15726 log = B_FALSE; 15727 break; 15728 case DL_PHYS_ADDR_REQ: 15729 /* 15730 * For IPv6 only, there are two additional 15731 * phys_addr_req's sent to the driver to get the 15732 * IPv6 token and lla. This allows IP to acquire 15733 * the hardware address format for a given interface 15734 * without having built in knowledge of the hardware 15735 * address. ill_phys_addr_pend keeps track of the last 15736 * DL_PAR sent so we know which response we are 15737 * dealing with. ill_dlpi_done will update 15738 * ill_phys_addr_pend when it sends the next req. 15739 * We don't complete the IOCTL until all three DL_PARs 15740 * have been attempted, so set *_len to 0 and break. 15741 */ 15742 paddrreq = ill->ill_phys_addr_pend; 15743 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15744 if (paddrreq == DL_IPV6_TOKEN) { 15745 ill->ill_token_length = 0; 15746 log = B_FALSE; 15747 break; 15748 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 15749 ill->ill_nd_lla_len = 0; 15750 log = B_FALSE; 15751 break; 15752 } 15753 /* 15754 * Something went wrong with the DL_PHYS_ADDR_REQ. 15755 * We presumably have an IOCTL hanging out waiting 15756 * for completion. Find it and complete the IOCTL 15757 * with the error noted. 15758 * However, ill_dl_phys was called on an ill queue 15759 * (from SIOCSLIFNAME), thus conn_pending_ill is not 15760 * set. But the ioctl is known to be pending on ill_wq. 15761 */ 15762 if (!ill->ill_ifname_pending) 15763 break; 15764 ill->ill_ifname_pending = 0; 15765 if (!ioctl_aborted) 15766 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15767 if (mp1 != NULL) { 15768 /* 15769 * This operation (SIOCSLIFNAME) must have 15770 * happened on the ill. Assert there is no conn 15771 */ 15772 ASSERT(connp == NULL); 15773 q = ill->ill_wq; 15774 } 15775 break; 15776 case DL_BIND_REQ: 15777 ill_dlpi_done(ill, DL_BIND_REQ); 15778 if (ill->ill_ifname_pending) 15779 break; 15780 /* 15781 * Something went wrong with the bind. We presumably 15782 * have an IOCTL hanging out waiting for completion. 15783 * Find it, take down the interface that was coming 15784 * up, and complete the IOCTL with the error noted. 15785 */ 15786 if (!ioctl_aborted) 15787 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15788 if (mp1 != NULL) { 15789 /* 15790 * This operation (SIOCSLIFFLAGS) must have 15791 * happened from a conn. 15792 */ 15793 ASSERT(connp != NULL); 15794 q = CONNP_TO_WQ(connp); 15795 if (ill->ill_move_in_progress) { 15796 ILL_CLEAR_MOVE(ill); 15797 } 15798 (void) ipif_down(ipif, NULL, NULL); 15799 /* error is set below the switch */ 15800 } 15801 break; 15802 case DL_ENABMULTI_REQ: 15803 ip1dbg(("DL_ERROR_ACK to enabmulti\n")); 15804 15805 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15806 ill->ill_dlpi_multicast_state = IDS_FAILED; 15807 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 15808 ipif_t *ipif; 15809 15810 log = B_FALSE; 15811 printf("ip: joining multicasts failed (%d)" 15812 " on %s - will use link layer " 15813 "broadcasts for multicast\n", 15814 dlea->dl_errno, ill->ill_name); 15815 15816 /* 15817 * Set up the multicast mapping alone. 15818 * writer, so ok to access ill->ill_ipif 15819 * without any lock. 15820 */ 15821 ipif = ill->ill_ipif; 15822 mutex_enter(&ill->ill_phyint->phyint_lock); 15823 ill->ill_phyint->phyint_flags |= 15824 PHYI_MULTI_BCAST; 15825 mutex_exit(&ill->ill_phyint->phyint_lock); 15826 15827 if (!ill->ill_isv6) { 15828 (void) ipif_arp_setup_multicast(ipif, 15829 NULL); 15830 } else { 15831 (void) ipif_ndp_setup_multicast(ipif, 15832 NULL); 15833 } 15834 } 15835 freemsg(mp); /* Don't want to pass this up */ 15836 return; 15837 case DL_CAPABILITY_REQ: 15838 case DL_CONTROL_REQ: 15839 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 15840 "DL_CAPABILITY/CONTROL REQ\n")); 15841 ill_dlpi_done(ill, dlea->dl_error_primitive); 15842 ill->ill_dlpi_capab_state = IDS_FAILED; 15843 freemsg(mp); 15844 return; 15845 } 15846 /* 15847 * Note the error for IOCTL completion (mp1 is set when 15848 * ready to complete ioctl). If ill_ifname_pending_err is 15849 * set, an error occured during plumbing (ill_ifname_pending), 15850 * so we want to report that error. 15851 * 15852 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 15853 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 15854 * expected to get errack'd if the driver doesn't support 15855 * these flags (e.g. ethernet). log will be set to B_FALSE 15856 * if these error conditions are encountered. 15857 */ 15858 if (mp1 != NULL) { 15859 if (ill->ill_ifname_pending_err != 0) { 15860 err = ill->ill_ifname_pending_err; 15861 ill->ill_ifname_pending_err = 0; 15862 } else { 15863 err = dlea->dl_unix_errno ? 15864 dlea->dl_unix_errno : ENXIO; 15865 } 15866 /* 15867 * If we're plumbing an interface and an error hasn't already 15868 * been saved, set ill_ifname_pending_err to the error passed 15869 * up. Ignore the error if log is B_FALSE (see comment above). 15870 */ 15871 } else if (log && ill->ill_ifname_pending && 15872 ill->ill_ifname_pending_err == 0) { 15873 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 15874 dlea->dl_unix_errno : ENXIO; 15875 } 15876 15877 if (log) 15878 ip_dlpi_error(ill, dlea->dl_error_primitive, 15879 dlea->dl_errno, dlea->dl_unix_errno); 15880 break; 15881 case DL_CAPABILITY_ACK: { 15882 boolean_t reneg_flag = B_FALSE; 15883 /* Call a routine to handle this one. */ 15884 ill_dlpi_done(ill, DL_CAPABILITY_REQ); 15885 /* 15886 * Check if the ACK is due to renegotiation case since we 15887 * will need to send a new CAPABILITY_REQ later. 15888 */ 15889 if (ill->ill_dlpi_capab_state == IDS_RENEG) { 15890 /* This is the ack for a renogiation case */ 15891 reneg_flag = B_TRUE; 15892 ill->ill_dlpi_capab_state = IDS_UNKNOWN; 15893 } 15894 ill_capability_ack(ill, mp); 15895 if (reneg_flag) 15896 ill_capability_probe(ill); 15897 break; 15898 } 15899 case DL_CONTROL_ACK: 15900 /* We treat all of these as "fire and forget" */ 15901 ill_dlpi_done(ill, DL_CONTROL_REQ); 15902 break; 15903 case DL_INFO_ACK: 15904 /* Call a routine to handle this one. */ 15905 ill_dlpi_done(ill, DL_INFO_REQ); 15906 ip_ll_subnet_defaults(ill, mp); 15907 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 15908 return; 15909 case DL_BIND_ACK: 15910 /* 15911 * We should have an IOCTL waiting on this unless 15912 * sent by ill_dl_phys, in which case just return 15913 */ 15914 ill_dlpi_done(ill, DL_BIND_REQ); 15915 if (ill->ill_ifname_pending) 15916 break; 15917 15918 if (!ioctl_aborted) 15919 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15920 if (mp1 == NULL) 15921 break; 15922 /* 15923 * Because mp1 was added by ill_dl_up(), and it always 15924 * passes a valid connp, connp must be valid here. 15925 */ 15926 ASSERT(connp != NULL); 15927 q = CONNP_TO_WQ(connp); 15928 15929 /* 15930 * We are exclusive. So nothing can change even after 15931 * we get the pending mp. If need be we can put it back 15932 * and restart, as in calling ipif_arp_up() below. 15933 */ 15934 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 15935 15936 mutex_enter(&ill->ill_lock); 15937 15938 ill->ill_dl_up = 1; 15939 15940 if ((info = ill->ill_nic_event_info) != NULL) { 15941 ip2dbg(("ip_rput_dlpi_writer: unexpected nic event %d " 15942 "attached for %s\n", info->hne_event, 15943 ill->ill_name)); 15944 if (info->hne_data != NULL) 15945 kmem_free(info->hne_data, info->hne_datalen); 15946 kmem_free(info, sizeof (hook_nic_event_t)); 15947 } 15948 15949 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 15950 if (info != NULL) { 15951 info->hne_nic = ill->ill_phyint->phyint_ifindex; 15952 info->hne_lif = 0; 15953 info->hne_event = NE_UP; 15954 info->hne_data = NULL; 15955 info->hne_datalen = 0; 15956 info->hne_family = ill->ill_isv6 ? 15957 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data; 15958 } else 15959 ip2dbg(("ip_rput_dlpi_writer: could not attach UP nic " 15960 "event information for %s (ENOMEM)\n", 15961 ill->ill_name)); 15962 15963 ill->ill_nic_event_info = info; 15964 15965 mutex_exit(&ill->ill_lock); 15966 15967 /* 15968 * Now bring up the resolver; when that is complete, we'll 15969 * create IREs. Note that we intentionally mirror what 15970 * ipif_up() would have done, because we got here by way of 15971 * ill_dl_up(), which stopped ipif_up()'s processing. 15972 */ 15973 if (ill->ill_isv6) { 15974 /* 15975 * v6 interfaces. 15976 * Unlike ARP which has to do another bind 15977 * and attach, once we get here we are 15978 * done with NDP. Except in the case of 15979 * ILLF_XRESOLV, in which case we send an 15980 * AR_INTERFACE_UP to the external resolver. 15981 * If all goes well, the ioctl will complete 15982 * in ip_rput(). If there's an error, we 15983 * complete it here. 15984 */ 15985 err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr); 15986 if (err == 0) { 15987 if (ill->ill_flags & ILLF_XRESOLV) { 15988 mutex_enter(&connp->conn_lock); 15989 mutex_enter(&ill->ill_lock); 15990 success = ipsq_pending_mp_add( 15991 connp, ipif, q, mp1, 0); 15992 mutex_exit(&ill->ill_lock); 15993 mutex_exit(&connp->conn_lock); 15994 if (success) { 15995 err = ipif_resolver_up(ipif, 15996 Res_act_initial); 15997 if (err == EINPROGRESS) { 15998 freemsg(mp); 15999 return; 16000 } 16001 ASSERT(err != 0); 16002 mp1 = ipsq_pending_mp_get(ipsq, 16003 &connp); 16004 ASSERT(mp1 != NULL); 16005 } else { 16006 /* conn has started closing */ 16007 err = EINTR; 16008 } 16009 } else { /* Non XRESOLV interface */ 16010 (void) ipif_resolver_up(ipif, 16011 Res_act_initial); 16012 err = ipif_up_done_v6(ipif); 16013 } 16014 } 16015 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 16016 /* 16017 * ARP and other v4 external resolvers. 16018 * Leave the pending mblk intact so that 16019 * the ioctl completes in ip_rput(). 16020 */ 16021 mutex_enter(&connp->conn_lock); 16022 mutex_enter(&ill->ill_lock); 16023 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 16024 mutex_exit(&ill->ill_lock); 16025 mutex_exit(&connp->conn_lock); 16026 if (success) { 16027 err = ipif_resolver_up(ipif, Res_act_initial); 16028 if (err == EINPROGRESS) { 16029 freemsg(mp); 16030 return; 16031 } 16032 ASSERT(err != 0); 16033 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16034 } else { 16035 /* The conn has started closing */ 16036 err = EINTR; 16037 } 16038 } else { 16039 /* 16040 * This one is complete. Reply to pending ioctl. 16041 */ 16042 (void) ipif_resolver_up(ipif, Res_act_initial); 16043 err = ipif_up_done(ipif); 16044 } 16045 16046 if ((err == 0) && (ill->ill_up_ipifs)) { 16047 err = ill_up_ipifs(ill, q, mp1); 16048 if (err == EINPROGRESS) { 16049 freemsg(mp); 16050 return; 16051 } 16052 } 16053 16054 if (ill->ill_up_ipifs) { 16055 ill_group_cleanup(ill); 16056 } 16057 16058 break; 16059 case DL_NOTIFY_IND: { 16060 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 16061 ire_t *ire; 16062 boolean_t need_ire_walk_v4 = B_FALSE; 16063 boolean_t need_ire_walk_v6 = B_FALSE; 16064 16065 switch (notify->dl_notification) { 16066 case DL_NOTE_PHYS_ADDR: 16067 err = ill_set_phys_addr(ill, mp); 16068 break; 16069 16070 case DL_NOTE_FASTPATH_FLUSH: 16071 ill_fastpath_flush(ill); 16072 break; 16073 16074 case DL_NOTE_SDU_SIZE: 16075 /* 16076 * Change the MTU size of the interface, of all 16077 * attached ipif's, and of all relevant ire's. The 16078 * new value's a uint32_t at notify->dl_data. 16079 * Mtu change Vs. new ire creation - protocol below. 16080 * 16081 * a Mark the ipif as IPIF_CHANGING. 16082 * b Set the new mtu in the ipif. 16083 * c Change the ire_max_frag on all affected ires 16084 * d Unmark the IPIF_CHANGING 16085 * 16086 * To see how the protocol works, assume an interface 16087 * route is also being added simultaneously by 16088 * ip_rt_add and let 'ipif' be the ipif referenced by 16089 * the ire. If the ire is created before step a, 16090 * it will be cleaned up by step c. If the ire is 16091 * created after step d, it will see the new value of 16092 * ipif_mtu. Any attempt to create the ire between 16093 * steps a to d will fail because of the IPIF_CHANGING 16094 * flag. Note that ire_create() is passed a pointer to 16095 * the ipif_mtu, and not the value. During ire_add 16096 * under the bucket lock, the ire_max_frag of the 16097 * new ire being created is set from the ipif/ire from 16098 * which it is being derived. 16099 */ 16100 mutex_enter(&ill->ill_lock); 16101 ill->ill_max_frag = (uint_t)notify->dl_data; 16102 16103 /* 16104 * If an SIOCSLIFLNKINFO has changed the ill_max_mtu 16105 * leave it alone 16106 */ 16107 if (ill->ill_mtu_userspecified) { 16108 mutex_exit(&ill->ill_lock); 16109 break; 16110 } 16111 ill->ill_max_mtu = ill->ill_max_frag; 16112 if (ill->ill_isv6) { 16113 if (ill->ill_max_mtu < IPV6_MIN_MTU) 16114 ill->ill_max_mtu = IPV6_MIN_MTU; 16115 } else { 16116 if (ill->ill_max_mtu < IP_MIN_MTU) 16117 ill->ill_max_mtu = IP_MIN_MTU; 16118 } 16119 for (ipif = ill->ill_ipif; ipif != NULL; 16120 ipif = ipif->ipif_next) { 16121 /* 16122 * Don't override the mtu if the user 16123 * has explicitly set it. 16124 */ 16125 if (ipif->ipif_flags & IPIF_FIXEDMTU) 16126 continue; 16127 ipif->ipif_mtu = (uint_t)notify->dl_data; 16128 if (ipif->ipif_isv6) 16129 ire = ipif_to_ire_v6(ipif); 16130 else 16131 ire = ipif_to_ire(ipif); 16132 if (ire != NULL) { 16133 ire->ire_max_frag = ipif->ipif_mtu; 16134 ire_refrele(ire); 16135 } 16136 if (ipif->ipif_flags & IPIF_UP) { 16137 if (ill->ill_isv6) 16138 need_ire_walk_v6 = B_TRUE; 16139 else 16140 need_ire_walk_v4 = B_TRUE; 16141 } 16142 } 16143 mutex_exit(&ill->ill_lock); 16144 if (need_ire_walk_v4) 16145 ire_walk_v4(ill_mtu_change, (char *)ill, 16146 ALL_ZONES, ipst); 16147 if (need_ire_walk_v6) 16148 ire_walk_v6(ill_mtu_change, (char *)ill, 16149 ALL_ZONES, ipst); 16150 break; 16151 case DL_NOTE_LINK_UP: 16152 case DL_NOTE_LINK_DOWN: { 16153 /* 16154 * We are writer. ill / phyint / ipsq assocs stable. 16155 * The RUNNING flag reflects the state of the link. 16156 */ 16157 phyint_t *phyint = ill->ill_phyint; 16158 uint64_t new_phyint_flags; 16159 boolean_t changed = B_FALSE; 16160 boolean_t went_up; 16161 16162 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 16163 mutex_enter(&phyint->phyint_lock); 16164 new_phyint_flags = went_up ? 16165 phyint->phyint_flags | PHYI_RUNNING : 16166 phyint->phyint_flags & ~PHYI_RUNNING; 16167 if (new_phyint_flags != phyint->phyint_flags) { 16168 phyint->phyint_flags = new_phyint_flags; 16169 changed = B_TRUE; 16170 } 16171 mutex_exit(&phyint->phyint_lock); 16172 /* 16173 * ill_restart_dad handles the DAD restart and routing 16174 * socket notification logic. 16175 */ 16176 if (changed) { 16177 ill_restart_dad(phyint->phyint_illv4, went_up); 16178 ill_restart_dad(phyint->phyint_illv6, went_up); 16179 } 16180 break; 16181 } 16182 case DL_NOTE_PROMISC_ON_PHYS: 16183 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 16184 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 16185 mutex_enter(&ill->ill_lock); 16186 ill->ill_promisc_on_phys = B_TRUE; 16187 mutex_exit(&ill->ill_lock); 16188 break; 16189 case DL_NOTE_PROMISC_OFF_PHYS: 16190 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 16191 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 16192 mutex_enter(&ill->ill_lock); 16193 ill->ill_promisc_on_phys = B_FALSE; 16194 mutex_exit(&ill->ill_lock); 16195 break; 16196 case DL_NOTE_CAPAB_RENEG: 16197 /* 16198 * Something changed on the driver side. 16199 * It wants us to renegotiate the capabilities 16200 * on this ill. The most likely cause is the 16201 * aggregation interface under us where a 16202 * port got added or went away. 16203 * 16204 * We reset the capabilities and set the 16205 * state to IDS_RENG so that when the ack 16206 * comes back, we can start the 16207 * renegotiation process. 16208 */ 16209 ill_capability_reset(ill); 16210 ill->ill_dlpi_capab_state = IDS_RENEG; 16211 break; 16212 default: 16213 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 16214 "type 0x%x for DL_NOTIFY_IND\n", 16215 notify->dl_notification)); 16216 break; 16217 } 16218 16219 /* 16220 * As this is an asynchronous operation, we 16221 * should not call ill_dlpi_done 16222 */ 16223 break; 16224 } 16225 case DL_NOTIFY_ACK: { 16226 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 16227 16228 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 16229 ill->ill_note_link = 1; 16230 ill_dlpi_done(ill, DL_NOTIFY_REQ); 16231 break; 16232 } 16233 case DL_PHYS_ADDR_ACK: { 16234 /* 16235 * As part of plumbing the interface via SIOCSLIFNAME, 16236 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, 16237 * whose answers we receive here. As each answer is received, 16238 * we call ill_dlpi_done() to dispatch the next request as 16239 * we're processing the current one. Once all answers have 16240 * been received, we use ipsq_pending_mp_get() to dequeue the 16241 * outstanding IOCTL and reply to it. (Because ill_dl_phys() 16242 * is invoked from an ill queue, conn_oper_pending_ill is not 16243 * available, but we know the ioctl is pending on ill_wq.) 16244 */ 16245 uint_t paddrlen, paddroff; 16246 16247 paddrreq = ill->ill_phys_addr_pend; 16248 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; 16249 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; 16250 16251 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 16252 if (paddrreq == DL_IPV6_TOKEN) { 16253 /* 16254 * bcopy to low-order bits of ill_token 16255 * 16256 * XXX Temporary hack - currently, all known tokens 16257 * are 64 bits, so I'll cheat for the moment. 16258 */ 16259 bcopy(mp->b_rptr + paddroff, 16260 &ill->ill_token.s6_addr32[2], paddrlen); 16261 ill->ill_token_length = paddrlen; 16262 break; 16263 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 16264 ASSERT(ill->ill_nd_lla_mp == NULL); 16265 ill_set_ndmp(ill, mp, paddroff, paddrlen); 16266 mp = NULL; 16267 break; 16268 } 16269 16270 ASSERT(paddrreq == DL_CURR_PHYS_ADDR); 16271 ASSERT(ill->ill_phys_addr_mp == NULL); 16272 if (!ill->ill_ifname_pending) 16273 break; 16274 ill->ill_ifname_pending = 0; 16275 if (!ioctl_aborted) 16276 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16277 if (mp1 != NULL) { 16278 ASSERT(connp == NULL); 16279 q = ill->ill_wq; 16280 } 16281 /* 16282 * If any error acks received during the plumbing sequence, 16283 * ill_ifname_pending_err will be set. Break out and send up 16284 * the error to the pending ioctl. 16285 */ 16286 if (ill->ill_ifname_pending_err != 0) { 16287 err = ill->ill_ifname_pending_err; 16288 ill->ill_ifname_pending_err = 0; 16289 break; 16290 } 16291 16292 ill->ill_phys_addr_mp = mp; 16293 ill->ill_phys_addr = mp->b_rptr + paddroff; 16294 mp = NULL; 16295 16296 /* 16297 * If paddrlen is zero, the DLPI provider doesn't support 16298 * physical addresses. The other two tests were historical 16299 * workarounds for bugs in our former PPP implementation, but 16300 * now other things have grown dependencies on them -- e.g., 16301 * the tun module specifies a dl_addr_length of zero in its 16302 * DL_BIND_ACK, but then specifies an incorrect value in its 16303 * DL_PHYS_ADDR_ACK. These bogus checks need to be removed, 16304 * but only after careful testing ensures that all dependent 16305 * broken DLPI providers have been fixed. 16306 */ 16307 if (paddrlen == 0 || ill->ill_phys_addr_length == 0 || 16308 ill->ill_phys_addr_length == IP_ADDR_LEN) { 16309 ill->ill_phys_addr = NULL; 16310 } else if (paddrlen != ill->ill_phys_addr_length) { 16311 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", 16312 paddrlen, ill->ill_phys_addr_length)); 16313 err = EINVAL; 16314 break; 16315 } 16316 16317 if (ill->ill_nd_lla_mp == NULL) { 16318 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { 16319 err = ENOMEM; 16320 break; 16321 } 16322 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); 16323 } 16324 16325 /* 16326 * Set the interface token. If the zeroth interface address 16327 * is unspecified, then set it to the link local address. 16328 */ 16329 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 16330 (void) ill_setdefaulttoken(ill); 16331 16332 ASSERT(ill->ill_ipif->ipif_id == 0); 16333 if (ipif != NULL && 16334 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { 16335 (void) ipif_setlinklocal(ipif); 16336 } 16337 break; 16338 } 16339 case DL_OK_ACK: 16340 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 16341 dlpi_prim_str((int)dloa->dl_correct_primitive), 16342 dloa->dl_correct_primitive)); 16343 switch (dloa->dl_correct_primitive) { 16344 case DL_UNBIND_REQ: 16345 case DL_ATTACH_REQ: 16346 case DL_DETACH_REQ: 16347 ill_dlpi_done(ill, dloa->dl_correct_primitive); 16348 break; 16349 } 16350 break; 16351 default: 16352 break; 16353 } 16354 16355 freemsg(mp); 16356 if (mp1 != NULL) { 16357 /* 16358 * The operation must complete without EINPROGRESS 16359 * since ipsq_pending_mp_get() has removed the mblk 16360 * from ipsq_pending_mp. Otherwise, the operation 16361 * will be stuck forever in the ipsq. 16362 */ 16363 ASSERT(err != EINPROGRESS); 16364 16365 switch (ipsq->ipsq_current_ioctl) { 16366 case 0: 16367 ipsq_current_finish(ipsq); 16368 break; 16369 16370 case SIOCLIFADDIF: 16371 case SIOCSLIFNAME: 16372 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 16373 break; 16374 16375 default: 16376 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 16377 break; 16378 } 16379 } 16380 } 16381 16382 /* 16383 * ip_rput_other is called by ip_rput to handle messages modifying the global 16384 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 16385 */ 16386 /* ARGSUSED */ 16387 void 16388 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 16389 { 16390 ill_t *ill; 16391 struct iocblk *iocp; 16392 mblk_t *mp1; 16393 conn_t *connp = NULL; 16394 16395 ip1dbg(("ip_rput_other ")); 16396 ill = (ill_t *)q->q_ptr; 16397 /* 16398 * This routine is not a writer in the case of SIOCGTUNPARAM 16399 * in which case ipsq is NULL. 16400 */ 16401 if (ipsq != NULL) { 16402 ASSERT(IAM_WRITER_IPSQ(ipsq)); 16403 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 16404 } 16405 16406 switch (mp->b_datap->db_type) { 16407 case M_ERROR: 16408 case M_HANGUP: 16409 /* 16410 * The device has a problem. We force the ILL down. It can 16411 * be brought up again manually using SIOCSIFFLAGS (via 16412 * ifconfig or equivalent). 16413 */ 16414 ASSERT(ipsq != NULL); 16415 if (mp->b_rptr < mp->b_wptr) 16416 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 16417 if (ill->ill_error == 0) 16418 ill->ill_error = ENXIO; 16419 if (!ill_down_start(q, mp)) 16420 return; 16421 ipif_all_down_tail(ipsq, q, mp, NULL); 16422 break; 16423 case M_IOCACK: 16424 iocp = (struct iocblk *)mp->b_rptr; 16425 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 16426 switch (iocp->ioc_cmd) { 16427 case SIOCSTUNPARAM: 16428 case OSIOCSTUNPARAM: 16429 ASSERT(ipsq != NULL); 16430 /* 16431 * Finish socket ioctl passed through to tun. 16432 * We should have an IOCTL waiting on this. 16433 */ 16434 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16435 if (ill->ill_isv6) { 16436 struct iftun_req *ta; 16437 16438 /* 16439 * if a source or destination is 16440 * being set, try and set the link 16441 * local address for the tunnel 16442 */ 16443 ta = (struct iftun_req *)mp->b_cont-> 16444 b_cont->b_rptr; 16445 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 16446 ipif_set_tun_llink(ill, ta); 16447 } 16448 16449 } 16450 if (mp1 != NULL) { 16451 /* 16452 * Now copy back the b_next/b_prev used by 16453 * mi code for the mi_copy* functions. 16454 * See ip_sioctl_tunparam() for the reason. 16455 * Also protect against missing b_cont. 16456 */ 16457 if (mp->b_cont != NULL) { 16458 mp->b_cont->b_next = 16459 mp1->b_cont->b_next; 16460 mp->b_cont->b_prev = 16461 mp1->b_cont->b_prev; 16462 } 16463 inet_freemsg(mp1); 16464 ASSERT(connp != NULL); 16465 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16466 iocp->ioc_error, NO_COPYOUT, ipsq); 16467 } else { 16468 ASSERT(connp == NULL); 16469 putnext(q, mp); 16470 } 16471 break; 16472 case SIOCGTUNPARAM: 16473 case OSIOCGTUNPARAM: 16474 /* 16475 * This is really M_IOCDATA from the tunnel driver. 16476 * convert back and complete the ioctl. 16477 * We should have an IOCTL waiting on this. 16478 */ 16479 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 16480 if (mp1) { 16481 /* 16482 * Now copy back the b_next/b_prev used by 16483 * mi code for the mi_copy* functions. 16484 * See ip_sioctl_tunparam() for the reason. 16485 * Also protect against missing b_cont. 16486 */ 16487 if (mp->b_cont != NULL) { 16488 mp->b_cont->b_next = 16489 mp1->b_cont->b_next; 16490 mp->b_cont->b_prev = 16491 mp1->b_cont->b_prev; 16492 } 16493 inet_freemsg(mp1); 16494 if (iocp->ioc_error == 0) 16495 mp->b_datap->db_type = M_IOCDATA; 16496 ASSERT(connp != NULL); 16497 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16498 iocp->ioc_error, COPYOUT, NULL); 16499 } else { 16500 ASSERT(connp == NULL); 16501 putnext(q, mp); 16502 } 16503 break; 16504 default: 16505 break; 16506 } 16507 break; 16508 case M_IOCNAK: 16509 iocp = (struct iocblk *)mp->b_rptr; 16510 16511 switch (iocp->ioc_cmd) { 16512 int mode; 16513 16514 case DL_IOC_HDR_INFO: 16515 /* 16516 * If this was the first attempt turn of the 16517 * fastpath probing. 16518 */ 16519 mutex_enter(&ill->ill_lock); 16520 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 16521 ill->ill_dlpi_fastpath_state = IDS_FAILED; 16522 mutex_exit(&ill->ill_lock); 16523 ill_fastpath_nack(ill); 16524 ip1dbg(("ip_rput: DLPI fastpath off on " 16525 "interface %s\n", 16526 ill->ill_name)); 16527 } else { 16528 mutex_exit(&ill->ill_lock); 16529 } 16530 freemsg(mp); 16531 break; 16532 case SIOCSTUNPARAM: 16533 case OSIOCSTUNPARAM: 16534 ASSERT(ipsq != NULL); 16535 /* 16536 * Finish socket ioctl passed through to tun 16537 * We should have an IOCTL waiting on this. 16538 */ 16539 /* FALLTHRU */ 16540 case SIOCGTUNPARAM: 16541 case OSIOCGTUNPARAM: 16542 /* 16543 * This is really M_IOCDATA from the tunnel driver. 16544 * convert back and complete the ioctl. 16545 * We should have an IOCTL waiting on this. 16546 */ 16547 if (iocp->ioc_cmd == SIOCGTUNPARAM || 16548 iocp->ioc_cmd == OSIOCGTUNPARAM) { 16549 mp1 = ill_pending_mp_get(ill, &connp, 16550 iocp->ioc_id); 16551 mode = COPYOUT; 16552 ipsq = NULL; 16553 } else { 16554 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16555 mode = NO_COPYOUT; 16556 } 16557 if (mp1 != NULL) { 16558 /* 16559 * Now copy back the b_next/b_prev used by 16560 * mi code for the mi_copy* functions. 16561 * See ip_sioctl_tunparam() for the reason. 16562 * Also protect against missing b_cont. 16563 */ 16564 if (mp->b_cont != NULL) { 16565 mp->b_cont->b_next = 16566 mp1->b_cont->b_next; 16567 mp->b_cont->b_prev = 16568 mp1->b_cont->b_prev; 16569 } 16570 inet_freemsg(mp1); 16571 if (iocp->ioc_error == 0) 16572 iocp->ioc_error = EINVAL; 16573 ASSERT(connp != NULL); 16574 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16575 iocp->ioc_error, mode, ipsq); 16576 } else { 16577 ASSERT(connp == NULL); 16578 putnext(q, mp); 16579 } 16580 break; 16581 default: 16582 break; 16583 } 16584 default: 16585 break; 16586 } 16587 } 16588 16589 /* 16590 * NOTE : This function does not ire_refrele the ire argument passed in. 16591 * 16592 * IPQoS notes 16593 * IP policy is invoked twice for a forwarded packet, once on the read side 16594 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 16595 * enabled. An additional parameter, in_ill, has been added for this purpose. 16596 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 16597 * because ip_mroute drops this information. 16598 * 16599 */ 16600 void 16601 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 16602 { 16603 uint32_t pkt_len; 16604 queue_t *q; 16605 uint32_t sum; 16606 #define rptr ((uchar_t *)ipha) 16607 uint32_t max_frag; 16608 uint32_t ill_index; 16609 ill_t *out_ill; 16610 mib2_ipIfStatsEntry_t *mibptr; 16611 ip_stack_t *ipst = in_ill->ill_ipst; 16612 16613 /* Get the ill_index of the incoming ILL */ 16614 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 16615 mibptr = (in_ill != NULL) ? in_ill->ill_ip_mib : &ipst->ips_ip_mib; 16616 16617 /* Initiate Read side IPPF processing */ 16618 if (IPP_ENABLED(IPP_FWD_IN, ipst)) { 16619 ip_process(IPP_FWD_IN, &mp, ill_index); 16620 if (mp == NULL) { 16621 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 16622 "during IPPF processing\n")); 16623 return; 16624 } 16625 } 16626 16627 pkt_len = ntohs(ipha->ipha_length); 16628 16629 /* Adjust the checksum to reflect the ttl decrement. */ 16630 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 16631 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 16632 16633 if (ipha->ipha_ttl-- <= 1) { 16634 if (ip_csum_hdr(ipha)) { 16635 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16636 goto drop_pkt; 16637 } 16638 /* 16639 * Note: ire_stq this will be NULL for multicast 16640 * datagrams using the long path through arp (the IRE 16641 * is not an IRE_CACHE). This should not cause 16642 * problems since we don't generate ICMP errors for 16643 * multicast packets. 16644 */ 16645 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16646 q = ire->ire_stq; 16647 if (q != NULL) { 16648 /* Sent by forwarding path, and router is global zone */ 16649 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, 16650 GLOBAL_ZONEID, ipst); 16651 } else 16652 freemsg(mp); 16653 return; 16654 } 16655 16656 /* 16657 * Don't forward if the interface is down 16658 */ 16659 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 16660 BUMP_MIB(mibptr, ipIfStatsInDiscards); 16661 ip2dbg(("ip_rput_forward:interface is down\n")); 16662 goto drop_pkt; 16663 } 16664 16665 /* Get the ill_index of the outgoing ILL */ 16666 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 16667 16668 out_ill = ire->ire_ipif->ipif_ill; 16669 16670 DTRACE_PROBE4(ip4__forwarding__start, 16671 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16672 16673 FW_HOOKS(ipst->ips_ip4_forwarding_event, 16674 ipst->ips_ipv4firewall_forwarding, 16675 in_ill, out_ill, ipha, mp, mp, ipst); 16676 16677 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 16678 16679 if (mp == NULL) 16680 return; 16681 pkt_len = ntohs(ipha->ipha_length); 16682 16683 if (is_system_labeled()) { 16684 mblk_t *mp1; 16685 16686 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 16687 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16688 goto drop_pkt; 16689 } 16690 /* Size may have changed */ 16691 mp = mp1; 16692 ipha = (ipha_t *)mp->b_rptr; 16693 pkt_len = ntohs(ipha->ipha_length); 16694 } 16695 16696 /* Check if there are options to update */ 16697 if (!IS_SIMPLE_IPH(ipha)) { 16698 if (ip_csum_hdr(ipha)) { 16699 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16700 goto drop_pkt; 16701 } 16702 if (ip_rput_forward_options(mp, ipha, ire, ipst)) { 16703 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16704 return; 16705 } 16706 16707 ipha->ipha_hdr_checksum = 0; 16708 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 16709 } 16710 max_frag = ire->ire_max_frag; 16711 if (pkt_len > max_frag) { 16712 /* 16713 * It needs fragging on its way out. We haven't 16714 * verified the header checksum yet. Since we 16715 * are going to put a surely good checksum in the 16716 * outgoing header, we have to make sure that it 16717 * was good coming in. 16718 */ 16719 if (ip_csum_hdr(ipha)) { 16720 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16721 goto drop_pkt; 16722 } 16723 /* Initiate Write side IPPF processing */ 16724 if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { 16725 ip_process(IPP_FWD_OUT, &mp, ill_index); 16726 if (mp == NULL) { 16727 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 16728 " during IPPF processing\n")); 16729 return; 16730 } 16731 } 16732 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID, ipst); 16733 ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); 16734 return; 16735 } 16736 16737 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 16738 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16739 FW_HOOKS(ipst->ips_ip4_physical_out_event, 16740 ipst->ips_ipv4firewall_physical_out, 16741 NULL, out_ill, ipha, mp, mp, ipst); 16742 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 16743 if (mp == NULL) 16744 return; 16745 16746 mp->b_prev = (mblk_t *)IPP_FWD_OUT; 16747 ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); 16748 (void) ip_xmit_v4(mp, ire, NULL, B_FALSE); 16749 /* ip_xmit_v4 always consumes the packet */ 16750 return; 16751 16752 drop_pkt:; 16753 ip1dbg(("ip_rput_forward: drop pkt\n")); 16754 freemsg(mp); 16755 #undef rptr 16756 } 16757 16758 void 16759 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 16760 { 16761 ire_t *ire; 16762 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 16763 16764 ASSERT(!ipif->ipif_isv6); 16765 /* 16766 * Find an IRE which matches the destination and the outgoing 16767 * queue in the cache table. All we need is an IRE_CACHE which 16768 * is pointing at ipif->ipif_ill. If it is part of some ill group, 16769 * then it is enough to have some IRE_CACHE in the group. 16770 */ 16771 if (ipif->ipif_flags & IPIF_POINTOPOINT) 16772 dst = ipif->ipif_pp_dst_addr; 16773 16774 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MBLK_GETLABEL(mp), 16775 MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR, ipst); 16776 if (ire == NULL) { 16777 /* 16778 * Mark this packet to make it be delivered to 16779 * ip_rput_forward after the new ire has been 16780 * created. 16781 */ 16782 mp->b_prev = NULL; 16783 mp->b_next = mp; 16784 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 16785 NULL, 0, GLOBAL_ZONEID, &zero_info); 16786 } else { 16787 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 16788 IRE_REFRELE(ire); 16789 } 16790 } 16791 16792 /* Update any source route, record route or timestamp options */ 16793 static int 16794 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) 16795 { 16796 ipoptp_t opts; 16797 uchar_t *opt; 16798 uint8_t optval; 16799 uint8_t optlen; 16800 ipaddr_t dst; 16801 uint32_t ts; 16802 ire_t *dst_ire = NULL; 16803 ire_t *tmp_ire = NULL; 16804 timestruc_t now; 16805 16806 ip2dbg(("ip_rput_forward_options\n")); 16807 dst = ipha->ipha_dst; 16808 for (optval = ipoptp_first(&opts, ipha); 16809 optval != IPOPT_EOL; 16810 optval = ipoptp_next(&opts)) { 16811 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 16812 opt = opts.ipoptp_cur; 16813 optlen = opts.ipoptp_len; 16814 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 16815 optval, opts.ipoptp_len)); 16816 switch (optval) { 16817 uint32_t off; 16818 case IPOPT_SSRR: 16819 case IPOPT_LSRR: 16820 /* Check if adminstratively disabled */ 16821 if (!ipst->ips_ip_forward_src_routed) { 16822 if (ire->ire_stq != NULL) { 16823 /* 16824 * Sent by forwarding path, and router 16825 * is global zone 16826 */ 16827 icmp_unreachable(ire->ire_stq, mp, 16828 ICMP_SOURCE_ROUTE_FAILED, 16829 GLOBAL_ZONEID, ipst); 16830 } else { 16831 ip0dbg(("ip_rput_forward_options: " 16832 "unable to send unreach\n")); 16833 freemsg(mp); 16834 } 16835 return (-1); 16836 } 16837 16838 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16839 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16840 if (dst_ire == NULL) { 16841 /* 16842 * Must be partial since ip_rput_options 16843 * checked for strict. 16844 */ 16845 break; 16846 } 16847 off = opt[IPOPT_OFFSET]; 16848 off--; 16849 redo_srr: 16850 if (optlen < IP_ADDR_LEN || 16851 off > optlen - IP_ADDR_LEN) { 16852 /* End of source route */ 16853 ip1dbg(( 16854 "ip_rput_forward_options: end of SR\n")); 16855 ire_refrele(dst_ire); 16856 break; 16857 } 16858 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16859 bcopy(&ire->ire_src_addr, (char *)opt + off, 16860 IP_ADDR_LEN); 16861 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 16862 ntohl(dst))); 16863 16864 /* 16865 * Check if our address is present more than 16866 * once as consecutive hops in source route. 16867 */ 16868 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16869 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16870 if (tmp_ire != NULL) { 16871 ire_refrele(tmp_ire); 16872 off += IP_ADDR_LEN; 16873 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16874 goto redo_srr; 16875 } 16876 ipha->ipha_dst = dst; 16877 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16878 ire_refrele(dst_ire); 16879 break; 16880 case IPOPT_RR: 16881 off = opt[IPOPT_OFFSET]; 16882 off--; 16883 if (optlen < IP_ADDR_LEN || 16884 off > optlen - IP_ADDR_LEN) { 16885 /* No more room - ignore */ 16886 ip1dbg(( 16887 "ip_rput_forward_options: end of RR\n")); 16888 break; 16889 } 16890 bcopy(&ire->ire_src_addr, (char *)opt + off, 16891 IP_ADDR_LEN); 16892 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16893 break; 16894 case IPOPT_TS: 16895 /* Insert timestamp if there is room */ 16896 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16897 case IPOPT_TS_TSONLY: 16898 off = IPOPT_TS_TIMELEN; 16899 break; 16900 case IPOPT_TS_PRESPEC: 16901 case IPOPT_TS_PRESPEC_RFC791: 16902 /* Verify that the address matched */ 16903 off = opt[IPOPT_OFFSET] - 1; 16904 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16905 dst_ire = ire_ctable_lookup(dst, 0, 16906 IRE_LOCAL, NULL, ALL_ZONES, NULL, 16907 MATCH_IRE_TYPE, ipst); 16908 if (dst_ire == NULL) { 16909 /* Not for us */ 16910 break; 16911 } 16912 ire_refrele(dst_ire); 16913 /* FALLTHRU */ 16914 case IPOPT_TS_TSANDADDR: 16915 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16916 break; 16917 default: 16918 /* 16919 * ip_*put_options should have already 16920 * dropped this packet. 16921 */ 16922 cmn_err(CE_PANIC, "ip_rput_forward_options: " 16923 "unknown IT - bug in ip_rput_options?\n"); 16924 return (0); /* Keep "lint" happy */ 16925 } 16926 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 16927 /* Increase overflow counter */ 16928 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 16929 opt[IPOPT_POS_OV_FLG] = 16930 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 16931 (off << 4)); 16932 break; 16933 } 16934 off = opt[IPOPT_OFFSET] - 1; 16935 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16936 case IPOPT_TS_PRESPEC: 16937 case IPOPT_TS_PRESPEC_RFC791: 16938 case IPOPT_TS_TSANDADDR: 16939 bcopy(&ire->ire_src_addr, 16940 (char *)opt + off, IP_ADDR_LEN); 16941 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16942 /* FALLTHRU */ 16943 case IPOPT_TS_TSONLY: 16944 off = opt[IPOPT_OFFSET] - 1; 16945 /* Compute # of milliseconds since midnight */ 16946 gethrestime(&now); 16947 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 16948 now.tv_nsec / (NANOSEC / MILLISEC); 16949 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 16950 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 16951 break; 16952 } 16953 break; 16954 } 16955 } 16956 return (0); 16957 } 16958 16959 /* 16960 * This is called after processing at least one of AH/ESP headers. 16961 * 16962 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 16963 * the actual, physical interface on which the packet was received, 16964 * but, when ip_strict_dst_multihoming is set to 1, could be the 16965 * interface which had the ipha_dst configured when the packet went 16966 * through ip_rput. The ill_index corresponding to the recv_ill 16967 * is saved in ipsec_in_rill_index 16968 */ 16969 void 16970 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 16971 { 16972 mblk_t *mp; 16973 ipaddr_t dst; 16974 in6_addr_t *v6dstp; 16975 ipha_t *ipha; 16976 ip6_t *ip6h; 16977 ipsec_in_t *ii; 16978 boolean_t ill_need_rele = B_FALSE; 16979 boolean_t rill_need_rele = B_FALSE; 16980 boolean_t ire_need_rele = B_FALSE; 16981 netstack_t *ns; 16982 ip_stack_t *ipst; 16983 16984 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 16985 ASSERT(ii->ipsec_in_ill_index != 0); 16986 ns = ii->ipsec_in_ns; 16987 ASSERT(ii->ipsec_in_ns != NULL); 16988 ipst = ns->netstack_ip; 16989 16990 mp = ipsec_mp->b_cont; 16991 ASSERT(mp != NULL); 16992 16993 16994 if (ill == NULL) { 16995 ASSERT(recv_ill == NULL); 16996 /* 16997 * We need to get the original queue on which ip_rput_local 16998 * or ip_rput_data_v6 was called. 16999 */ 17000 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 17001 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL, ipst); 17002 ill_need_rele = B_TRUE; 17003 17004 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 17005 recv_ill = ill_lookup_on_ifindex( 17006 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 17007 NULL, NULL, NULL, NULL, ipst); 17008 rill_need_rele = B_TRUE; 17009 } else { 17010 recv_ill = ill; 17011 } 17012 17013 if ((ill == NULL) || (recv_ill == NULL)) { 17014 ip0dbg(("ip_fanout_proto_again: interface " 17015 "disappeared\n")); 17016 if (ill != NULL) 17017 ill_refrele(ill); 17018 if (recv_ill != NULL) 17019 ill_refrele(recv_ill); 17020 freemsg(ipsec_mp); 17021 return; 17022 } 17023 } 17024 17025 ASSERT(ill != NULL && recv_ill != NULL); 17026 17027 if (mp->b_datap->db_type == M_CTL) { 17028 /* 17029 * AH/ESP is returning the ICMP message after 17030 * removing their headers. Fanout again till 17031 * it gets to the right protocol. 17032 */ 17033 if (ii->ipsec_in_v4) { 17034 icmph_t *icmph; 17035 int iph_hdr_length; 17036 int hdr_length; 17037 17038 ipha = (ipha_t *)mp->b_rptr; 17039 iph_hdr_length = IPH_HDR_LENGTH(ipha); 17040 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 17041 ipha = (ipha_t *)&icmph[1]; 17042 hdr_length = IPH_HDR_LENGTH(ipha); 17043 /* 17044 * icmp_inbound_error_fanout may need to do pullupmsg. 17045 * Reset the type to M_DATA. 17046 */ 17047 mp->b_datap->db_type = M_DATA; 17048 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 17049 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 17050 B_FALSE, ill, ii->ipsec_in_zoneid); 17051 } else { 17052 icmp6_t *icmp6; 17053 int hdr_length; 17054 17055 ip6h = (ip6_t *)mp->b_rptr; 17056 /* Don't call hdr_length_v6() unless you have to. */ 17057 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 17058 hdr_length = ip_hdr_length_v6(mp, ip6h); 17059 else 17060 hdr_length = IPV6_HDR_LEN; 17061 17062 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 17063 /* 17064 * icmp_inbound_error_fanout_v6 may need to do 17065 * pullupmsg. Reset the type to M_DATA. 17066 */ 17067 mp->b_datap->db_type = M_DATA; 17068 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 17069 ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); 17070 } 17071 if (ill_need_rele) 17072 ill_refrele(ill); 17073 if (rill_need_rele) 17074 ill_refrele(recv_ill); 17075 return; 17076 } 17077 17078 if (ii->ipsec_in_v4) { 17079 ipha = (ipha_t *)mp->b_rptr; 17080 dst = ipha->ipha_dst; 17081 if (CLASSD(dst)) { 17082 /* 17083 * Multicast has to be delivered to all streams. 17084 */ 17085 dst = INADDR_BROADCAST; 17086 } 17087 17088 if (ire == NULL) { 17089 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, 17090 MBLK_GETLABEL(mp), ipst); 17091 if (ire == NULL) { 17092 if (ill_need_rele) 17093 ill_refrele(ill); 17094 if (rill_need_rele) 17095 ill_refrele(recv_ill); 17096 ip1dbg(("ip_fanout_proto_again: " 17097 "IRE not found")); 17098 freemsg(ipsec_mp); 17099 return; 17100 } 17101 ire_need_rele = B_TRUE; 17102 } 17103 17104 switch (ipha->ipha_protocol) { 17105 case IPPROTO_UDP: 17106 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 17107 recv_ill); 17108 if (ire_need_rele) 17109 ire_refrele(ire); 17110 break; 17111 case IPPROTO_TCP: 17112 if (!ire_need_rele) 17113 IRE_REFHOLD(ire); 17114 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 17115 ire, ipsec_mp, 0, ill->ill_rq, NULL); 17116 IRE_REFRELE(ire); 17117 if (mp != NULL) 17118 squeue_enter_chain(GET_SQUEUE(mp), mp, 17119 mp, 1, SQTAG_IP_PROTO_AGAIN); 17120 break; 17121 case IPPROTO_SCTP: 17122 if (!ire_need_rele) 17123 IRE_REFHOLD(ire); 17124 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 17125 ipsec_mp, 0, ill->ill_rq, dst); 17126 break; 17127 default: 17128 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 17129 recv_ill); 17130 if (ire_need_rele) 17131 ire_refrele(ire); 17132 break; 17133 } 17134 } else { 17135 uint32_t rput_flags = 0; 17136 17137 ip6h = (ip6_t *)mp->b_rptr; 17138 v6dstp = &ip6h->ip6_dst; 17139 /* 17140 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 17141 * address. 17142 * 17143 * Currently, we don't store that state in the IPSEC_IN 17144 * message, and we may need to. 17145 */ 17146 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 17147 IP6_IN_LLMCAST : 0); 17148 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 17149 NULL, NULL); 17150 } 17151 if (ill_need_rele) 17152 ill_refrele(ill); 17153 if (rill_need_rele) 17154 ill_refrele(recv_ill); 17155 } 17156 17157 /* 17158 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 17159 * returns 'true' if there are still fragments left on the queue, in 17160 * which case we restart the timer. 17161 */ 17162 void 17163 ill_frag_timer(void *arg) 17164 { 17165 ill_t *ill = (ill_t *)arg; 17166 boolean_t frag_pending; 17167 ip_stack_t *ipst = ill->ill_ipst; 17168 17169 mutex_enter(&ill->ill_lock); 17170 ASSERT(!ill->ill_fragtimer_executing); 17171 if (ill->ill_state_flags & ILL_CONDEMNED) { 17172 ill->ill_frag_timer_id = 0; 17173 mutex_exit(&ill->ill_lock); 17174 return; 17175 } 17176 ill->ill_fragtimer_executing = 1; 17177 mutex_exit(&ill->ill_lock); 17178 17179 frag_pending = ill_frag_timeout(ill, ipst->ips_ip_g_frag_timeout); 17180 17181 /* 17182 * Restart the timer, if we have fragments pending or if someone 17183 * wanted us to be scheduled again. 17184 */ 17185 mutex_enter(&ill->ill_lock); 17186 ill->ill_fragtimer_executing = 0; 17187 ill->ill_frag_timer_id = 0; 17188 if (frag_pending || ill->ill_fragtimer_needrestart) 17189 ill_frag_timer_start(ill); 17190 mutex_exit(&ill->ill_lock); 17191 } 17192 17193 void 17194 ill_frag_timer_start(ill_t *ill) 17195 { 17196 ip_stack_t *ipst = ill->ill_ipst; 17197 17198 ASSERT(MUTEX_HELD(&ill->ill_lock)); 17199 17200 /* If the ill is closing or opening don't proceed */ 17201 if (ill->ill_state_flags & ILL_CONDEMNED) 17202 return; 17203 17204 if (ill->ill_fragtimer_executing) { 17205 /* 17206 * ill_frag_timer is currently executing. Just record the 17207 * the fact that we want the timer to be restarted. 17208 * ill_frag_timer will post a timeout before it returns, 17209 * ensuring it will be called again. 17210 */ 17211 ill->ill_fragtimer_needrestart = 1; 17212 return; 17213 } 17214 17215 if (ill->ill_frag_timer_id == 0) { 17216 /* 17217 * The timer is neither running nor is the timeout handler 17218 * executing. Post a timeout so that ill_frag_timer will be 17219 * called 17220 */ 17221 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 17222 MSEC_TO_TICK(ipst->ips_ip_g_frag_timo_ms >> 1)); 17223 ill->ill_fragtimer_needrestart = 0; 17224 } 17225 } 17226 17227 /* 17228 * This routine is needed for loopback when forwarding multicasts. 17229 * 17230 * IPQoS Notes: 17231 * IPPF processing is done in fanout routines. 17232 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 17233 * processing for IPSec packets is done when it comes back in clear. 17234 * NOTE : The callers of this function need to do the ire_refrele for the 17235 * ire that is being passed in. 17236 */ 17237 void 17238 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17239 ill_t *recv_ill) 17240 { 17241 ill_t *ill = (ill_t *)q->q_ptr; 17242 uint32_t sum; 17243 uint32_t u1; 17244 uint32_t u2; 17245 int hdr_length; 17246 boolean_t mctl_present; 17247 mblk_t *first_mp = mp; 17248 mblk_t *hada_mp = NULL; 17249 ipha_t *inner_ipha; 17250 ip_stack_t *ipst; 17251 17252 ASSERT(recv_ill != NULL); 17253 ipst = recv_ill->ill_ipst; 17254 17255 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 17256 "ip_rput_locl_start: q %p", q); 17257 17258 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17259 ASSERT(ill != NULL); 17260 17261 17262 #define rptr ((uchar_t *)ipha) 17263 #define iphs ((uint16_t *)ipha) 17264 17265 /* 17266 * no UDP or TCP packet should come here anymore. 17267 */ 17268 ASSERT((ipha->ipha_protocol != IPPROTO_TCP) && 17269 (ipha->ipha_protocol != IPPROTO_UDP)); 17270 17271 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 17272 if (mctl_present && 17273 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 17274 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 17275 17276 /* 17277 * It's an IPsec accelerated packet. 17278 * Keep a pointer to the data attributes around until 17279 * we allocate the ipsec_info_t. 17280 */ 17281 IPSECHW_DEBUG(IPSECHW_PKT, 17282 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 17283 hada_mp = first_mp; 17284 hada_mp->b_cont = NULL; 17285 /* 17286 * Since it is accelerated, it comes directly from 17287 * the ill and the data attributes is followed by 17288 * the packet data. 17289 */ 17290 ASSERT(mp->b_datap->db_type != M_CTL); 17291 first_mp = mp; 17292 mctl_present = B_FALSE; 17293 } 17294 17295 /* 17296 * IF M_CTL is not present, then ipsec_in_is_secure 17297 * should return B_TRUE. There is a case where loopback 17298 * packets has an M_CTL in the front with all the 17299 * IPSEC options set to IPSEC_PREF_NEVER - which means 17300 * ipsec_in_is_secure will return B_FALSE. As loopback 17301 * packets never comes here, it is safe to ASSERT the 17302 * following. 17303 */ 17304 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 17305 17306 17307 /* u1 is # words of IP options */ 17308 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 17309 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 17310 17311 if (u1) { 17312 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 17313 if (hada_mp != NULL) 17314 freemsg(hada_mp); 17315 return; 17316 } 17317 } else { 17318 /* Check the IP header checksum. */ 17319 #define uph ((uint16_t *)ipha) 17320 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 17321 uph[6] + uph[7] + uph[8] + uph[9]; 17322 #undef uph 17323 /* finish doing IP checksum */ 17324 sum = (sum & 0xFFFF) + (sum >> 16); 17325 sum = ~(sum + (sum >> 16)) & 0xFFFF; 17326 /* 17327 * Don't verify header checksum if this packet is coming 17328 * back from AH/ESP as we already did it. 17329 */ 17330 if (!mctl_present && (sum && sum != 0xFFFF)) { 17331 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 17332 goto drop_pkt; 17333 } 17334 } 17335 17336 /* 17337 * Count for SNMP of inbound packets for ire. As ip_proto_input 17338 * might be called more than once for secure packets, count only 17339 * the first time. 17340 */ 17341 if (!mctl_present) { 17342 UPDATE_IB_PKT_COUNT(ire); 17343 ire->ire_last_used_time = lbolt; 17344 } 17345 17346 /* Check for fragmentation offset. */ 17347 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 17348 u1 = u2 & (IPH_MF | IPH_OFFSET); 17349 if (u1) { 17350 /* 17351 * We re-assemble fragments before we do the AH/ESP 17352 * processing. Thus, M_CTL should not be present 17353 * while we are re-assembling. 17354 */ 17355 ASSERT(!mctl_present); 17356 ASSERT(first_mp == mp); 17357 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 17358 return; 17359 } 17360 /* 17361 * Make sure that first_mp points back to mp as 17362 * the mp we came in with could have changed in 17363 * ip_rput_fragment(). 17364 */ 17365 ipha = (ipha_t *)mp->b_rptr; 17366 first_mp = mp; 17367 } 17368 17369 /* 17370 * Clear hardware checksumming flag as it is currently only 17371 * used by TCP and UDP. 17372 */ 17373 DB_CKSUMFLAGS(mp) = 0; 17374 17375 /* Now we have a complete datagram, destined for this machine. */ 17376 u1 = IPH_HDR_LENGTH(ipha); 17377 switch (ipha->ipha_protocol) { 17378 case IPPROTO_ICMP: { 17379 ire_t *ire_zone; 17380 ilm_t *ilm; 17381 mblk_t *mp1; 17382 zoneid_t last_zoneid; 17383 17384 if (CLASSD(ipha->ipha_dst) && 17385 !(recv_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 17386 ASSERT(ire->ire_type == IRE_BROADCAST); 17387 /* 17388 * In the multicast case, applications may have joined 17389 * the group from different zones, so we need to deliver 17390 * the packet to each of them. Loop through the 17391 * multicast memberships structures (ilm) on the receive 17392 * ill and send a copy of the packet up each matching 17393 * one. However, we don't do this for multicasts sent on 17394 * the loopback interface (PHYI_LOOPBACK flag set) as 17395 * they must stay in the sender's zone. 17396 * 17397 * ilm_add_v6() ensures that ilms in the same zone are 17398 * contiguous in the ill_ilm list. We use this property 17399 * to avoid sending duplicates needed when two 17400 * applications in the same zone join the same group on 17401 * different logical interfaces: we ignore the ilm if 17402 * its zoneid is the same as the last matching one. 17403 * In addition, the sending of the packet for 17404 * ire_zoneid is delayed until all of the other ilms 17405 * have been exhausted. 17406 */ 17407 last_zoneid = -1; 17408 ILM_WALKER_HOLD(recv_ill); 17409 for (ilm = recv_ill->ill_ilm; ilm != NULL; 17410 ilm = ilm->ilm_next) { 17411 if ((ilm->ilm_flags & ILM_DELETED) || 17412 ipha->ipha_dst != ilm->ilm_addr || 17413 ilm->ilm_zoneid == last_zoneid || 17414 ilm->ilm_zoneid == ire->ire_zoneid || 17415 ilm->ilm_zoneid == ALL_ZONES || 17416 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 17417 continue; 17418 mp1 = ip_copymsg(first_mp); 17419 if (mp1 == NULL) 17420 continue; 17421 icmp_inbound(q, mp1, B_TRUE, ill, 17422 0, sum, mctl_present, B_TRUE, 17423 recv_ill, ilm->ilm_zoneid); 17424 last_zoneid = ilm->ilm_zoneid; 17425 } 17426 ILM_WALKER_RELE(recv_ill); 17427 } else if (ire->ire_type == IRE_BROADCAST) { 17428 /* 17429 * In the broadcast case, there may be many zones 17430 * which need a copy of the packet delivered to them. 17431 * There is one IRE_BROADCAST per broadcast address 17432 * and per zone; we walk those using a helper function. 17433 * In addition, the sending of the packet for ire is 17434 * delayed until all of the other ires have been 17435 * processed. 17436 */ 17437 IRB_REFHOLD(ire->ire_bucket); 17438 ire_zone = NULL; 17439 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 17440 ire)) != NULL) { 17441 mp1 = ip_copymsg(first_mp); 17442 if (mp1 == NULL) 17443 continue; 17444 17445 UPDATE_IB_PKT_COUNT(ire_zone); 17446 ire_zone->ire_last_used_time = lbolt; 17447 icmp_inbound(q, mp1, B_TRUE, ill, 17448 0, sum, mctl_present, B_TRUE, 17449 recv_ill, ire_zone->ire_zoneid); 17450 } 17451 IRB_REFRELE(ire->ire_bucket); 17452 } 17453 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 17454 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 17455 ire->ire_zoneid); 17456 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17457 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 17458 return; 17459 } 17460 case IPPROTO_IGMP: 17461 /* 17462 * If we are not willing to accept IGMP packets in clear, 17463 * then check with global policy. 17464 */ 17465 if (ipst->ips_igmp_accept_clear_messages == 0) { 17466 first_mp = ipsec_check_global_policy(first_mp, NULL, 17467 ipha, NULL, mctl_present, ipst->ips_netstack); 17468 if (first_mp == NULL) 17469 return; 17470 } 17471 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17472 freemsg(first_mp); 17473 ip1dbg(("ip_proto_input: zone all cannot accept raw")); 17474 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17475 return; 17476 } 17477 if ((mp = igmp_input(q, mp, ill)) == NULL) { 17478 /* Bad packet - discarded by igmp_input */ 17479 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17480 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 17481 if (mctl_present) 17482 freeb(first_mp); 17483 return; 17484 } 17485 /* 17486 * igmp_input() may have returned the pulled up message. 17487 * So first_mp and ipha need to be reinitialized. 17488 */ 17489 ipha = (ipha_t *)mp->b_rptr; 17490 if (mctl_present) 17491 first_mp->b_cont = mp; 17492 else 17493 first_mp = mp; 17494 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17495 connf_head != NULL) { 17496 /* No user-level listener for IGMP packets */ 17497 goto drop_pkt; 17498 } 17499 /* deliver to local raw users */ 17500 break; 17501 case IPPROTO_PIM: 17502 /* 17503 * If we are not willing to accept PIM packets in clear, 17504 * then check with global policy. 17505 */ 17506 if (ipst->ips_pim_accept_clear_messages == 0) { 17507 first_mp = ipsec_check_global_policy(first_mp, NULL, 17508 ipha, NULL, mctl_present, ipst->ips_netstack); 17509 if (first_mp == NULL) 17510 return; 17511 } 17512 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17513 freemsg(first_mp); 17514 ip1dbg(("ip_proto_input: zone all cannot accept PIM")); 17515 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17516 return; 17517 } 17518 if (pim_input(q, mp, ill) != 0) { 17519 /* Bad packet - discarded by pim_input */ 17520 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17521 "ip_rput_locl_end: q %p (%S)", q, "pim"); 17522 if (mctl_present) 17523 freeb(first_mp); 17524 return; 17525 } 17526 17527 /* 17528 * pim_input() may have pulled up the message so ipha needs to 17529 * be reinitialized. 17530 */ 17531 ipha = (ipha_t *)mp->b_rptr; 17532 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17533 connf_head != NULL) { 17534 /* No user-level listener for PIM packets */ 17535 goto drop_pkt; 17536 } 17537 /* deliver to local raw users */ 17538 break; 17539 case IPPROTO_ENCAP: 17540 /* 17541 * Handle self-encapsulated packets (IP-in-IP where 17542 * the inner addresses == the outer addresses). 17543 */ 17544 hdr_length = IPH_HDR_LENGTH(ipha); 17545 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 17546 mp->b_wptr) { 17547 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 17548 sizeof (ipha_t) - mp->b_rptr)) { 17549 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17550 freemsg(first_mp); 17551 return; 17552 } 17553 ipha = (ipha_t *)mp->b_rptr; 17554 } 17555 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 17556 /* 17557 * Check the sanity of the inner IP header. 17558 */ 17559 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 17560 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17561 freemsg(first_mp); 17562 return; 17563 } 17564 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 17565 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17566 freemsg(first_mp); 17567 return; 17568 } 17569 if (inner_ipha->ipha_src == ipha->ipha_src && 17570 inner_ipha->ipha_dst == ipha->ipha_dst) { 17571 ipsec_in_t *ii; 17572 17573 /* 17574 * Self-encapsulated tunnel packet. Remove 17575 * the outer IP header and fanout again. 17576 * We also need to make sure that the inner 17577 * header is pulled up until options. 17578 */ 17579 mp->b_rptr = (uchar_t *)inner_ipha; 17580 ipha = inner_ipha; 17581 hdr_length = IPH_HDR_LENGTH(ipha); 17582 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 17583 if (!pullupmsg(mp, (uchar_t *)ipha + 17584 + hdr_length - mp->b_rptr)) { 17585 freemsg(first_mp); 17586 return; 17587 } 17588 ipha = (ipha_t *)mp->b_rptr; 17589 } 17590 if (!mctl_present) { 17591 ASSERT(first_mp == mp); 17592 /* 17593 * This means that somebody is sending 17594 * Self-encapsualted packets without AH/ESP. 17595 * If AH/ESP was present, we would have already 17596 * allocated the first_mp. 17597 */ 17598 first_mp = ipsec_in_alloc(B_TRUE, 17599 ipst->ips_netstack); 17600 if (first_mp == NULL) { 17601 ip1dbg(("ip_proto_input: IPSEC_IN " 17602 "allocation failure.\n")); 17603 BUMP_MIB(ill->ill_ip_mib, 17604 ipIfStatsInDiscards); 17605 freemsg(mp); 17606 return; 17607 } 17608 first_mp->b_cont = mp; 17609 } 17610 /* 17611 * We generally store the ill_index if we need to 17612 * do IPSEC processing as we lose the ill queue when 17613 * we come back. But in this case, we never should 17614 * have to store the ill_index here as it should have 17615 * been stored previously when we processed the 17616 * AH/ESP header in this routine or for non-ipsec 17617 * cases, we still have the queue. But for some bad 17618 * packets from the wire, we can get to IPSEC after 17619 * this and we better store the index for that case. 17620 */ 17621 ill = (ill_t *)q->q_ptr; 17622 ii = (ipsec_in_t *)first_mp->b_rptr; 17623 ii->ipsec_in_ill_index = 17624 ill->ill_phyint->phyint_ifindex; 17625 ii->ipsec_in_rill_index = 17626 recv_ill->ill_phyint->phyint_ifindex; 17627 if (ii->ipsec_in_decaps) { 17628 /* 17629 * This packet is self-encapsulated multiple 17630 * times. We don't want to recurse infinitely. 17631 * To keep it simple, drop the packet. 17632 */ 17633 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17634 freemsg(first_mp); 17635 return; 17636 } 17637 ii->ipsec_in_decaps = B_TRUE; 17638 ip_fanout_proto_again(first_mp, recv_ill, recv_ill, 17639 ire); 17640 return; 17641 } 17642 break; 17643 case IPPROTO_AH: 17644 case IPPROTO_ESP: { 17645 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 17646 17647 /* 17648 * Fast path for AH/ESP. If this is the first time 17649 * we are sending a datagram to AH/ESP, allocate 17650 * a IPSEC_IN message and prepend it. Otherwise, 17651 * just fanout. 17652 */ 17653 17654 int ipsec_rc; 17655 ipsec_in_t *ii; 17656 netstack_t *ns = ipst->ips_netstack; 17657 17658 IP_STAT(ipst, ipsec_proto_ahesp); 17659 if (!mctl_present) { 17660 ASSERT(first_mp == mp); 17661 first_mp = ipsec_in_alloc(B_TRUE, ns); 17662 if (first_mp == NULL) { 17663 ip1dbg(("ip_proto_input: IPSEC_IN " 17664 "allocation failure.\n")); 17665 freemsg(hada_mp); /* okay ifnull */ 17666 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17667 freemsg(mp); 17668 return; 17669 } 17670 /* 17671 * Store the ill_index so that when we come back 17672 * from IPSEC we ride on the same queue. 17673 */ 17674 ill = (ill_t *)q->q_ptr; 17675 ii = (ipsec_in_t *)first_mp->b_rptr; 17676 ii->ipsec_in_ill_index = 17677 ill->ill_phyint->phyint_ifindex; 17678 ii->ipsec_in_rill_index = 17679 recv_ill->ill_phyint->phyint_ifindex; 17680 first_mp->b_cont = mp; 17681 /* 17682 * Cache hardware acceleration info. 17683 */ 17684 if (hada_mp != NULL) { 17685 IPSECHW_DEBUG(IPSECHW_PKT, 17686 ("ip_rput_local: caching data attr.\n")); 17687 ii->ipsec_in_accelerated = B_TRUE; 17688 ii->ipsec_in_da = hada_mp; 17689 hada_mp = NULL; 17690 } 17691 } else { 17692 ii = (ipsec_in_t *)first_mp->b_rptr; 17693 } 17694 17695 if (!ipsec_loaded(ipss)) { 17696 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 17697 ire->ire_zoneid, ipst); 17698 return; 17699 } 17700 17701 ns = ipst->ips_netstack; 17702 /* select inbound SA and have IPsec process the pkt */ 17703 if (ipha->ipha_protocol == IPPROTO_ESP) { 17704 esph_t *esph = ipsec_inbound_esp_sa(first_mp, ns); 17705 if (esph == NULL) 17706 return; 17707 ASSERT(ii->ipsec_in_esp_sa != NULL); 17708 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 17709 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 17710 first_mp, esph); 17711 } else { 17712 ah_t *ah = ipsec_inbound_ah_sa(first_mp, ns); 17713 if (ah == NULL) 17714 return; 17715 ASSERT(ii->ipsec_in_ah_sa != NULL); 17716 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 17717 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 17718 first_mp, ah); 17719 } 17720 17721 switch (ipsec_rc) { 17722 case IPSEC_STATUS_SUCCESS: 17723 break; 17724 case IPSEC_STATUS_FAILED: 17725 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17726 /* FALLTHRU */ 17727 case IPSEC_STATUS_PENDING: 17728 return; 17729 } 17730 /* we're done with IPsec processing, send it up */ 17731 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 17732 return; 17733 } 17734 default: 17735 break; 17736 } 17737 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { 17738 ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", 17739 ire->ire_zoneid)); 17740 goto drop_pkt; 17741 } 17742 /* 17743 * Handle protocols with which IP is less intimate. There 17744 * can be more than one stream bound to a particular 17745 * protocol. When this is the case, each one gets a copy 17746 * of any incoming packets. 17747 */ 17748 ip_fanout_proto(q, first_mp, ill, ipha, 17749 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 17750 B_TRUE, recv_ill, ire->ire_zoneid); 17751 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17752 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 17753 return; 17754 17755 drop_pkt: 17756 freemsg(first_mp); 17757 if (hada_mp != NULL) 17758 freeb(hada_mp); 17759 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17760 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 17761 #undef rptr 17762 #undef iphs 17763 17764 } 17765 17766 /* 17767 * Update any source route, record route or timestamp options. 17768 * Check that we are at end of strict source route. 17769 * The options have already been checked for sanity in ip_rput_options(). 17770 */ 17771 static boolean_t 17772 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17773 ip_stack_t *ipst) 17774 { 17775 ipoptp_t opts; 17776 uchar_t *opt; 17777 uint8_t optval; 17778 uint8_t optlen; 17779 ipaddr_t dst; 17780 uint32_t ts; 17781 ire_t *dst_ire; 17782 timestruc_t now; 17783 zoneid_t zoneid; 17784 ill_t *ill; 17785 17786 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17787 17788 ip2dbg(("ip_rput_local_options\n")); 17789 17790 for (optval = ipoptp_first(&opts, ipha); 17791 optval != IPOPT_EOL; 17792 optval = ipoptp_next(&opts)) { 17793 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 17794 opt = opts.ipoptp_cur; 17795 optlen = opts.ipoptp_len; 17796 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 17797 optval, optlen)); 17798 switch (optval) { 17799 uint32_t off; 17800 case IPOPT_SSRR: 17801 case IPOPT_LSRR: 17802 off = opt[IPOPT_OFFSET]; 17803 off--; 17804 if (optlen < IP_ADDR_LEN || 17805 off > optlen - IP_ADDR_LEN) { 17806 /* End of source route */ 17807 ip1dbg(("ip_rput_local_options: end of SR\n")); 17808 break; 17809 } 17810 /* 17811 * This will only happen if two consecutive entries 17812 * in the source route contains our address or if 17813 * it is a packet with a loose source route which 17814 * reaches us before consuming the whole source route 17815 */ 17816 ip1dbg(("ip_rput_local_options: not end of SR\n")); 17817 if (optval == IPOPT_SSRR) { 17818 goto bad_src_route; 17819 } 17820 /* 17821 * Hack: instead of dropping the packet truncate the 17822 * source route to what has been used by filling the 17823 * rest with IPOPT_NOP. 17824 */ 17825 opt[IPOPT_OLEN] = (uint8_t)off; 17826 while (off < optlen) { 17827 opt[off++] = IPOPT_NOP; 17828 } 17829 break; 17830 case IPOPT_RR: 17831 off = opt[IPOPT_OFFSET]; 17832 off--; 17833 if (optlen < IP_ADDR_LEN || 17834 off > optlen - IP_ADDR_LEN) { 17835 /* No more room - ignore */ 17836 ip1dbg(( 17837 "ip_rput_local_options: end of RR\n")); 17838 break; 17839 } 17840 bcopy(&ire->ire_src_addr, (char *)opt + off, 17841 IP_ADDR_LEN); 17842 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17843 break; 17844 case IPOPT_TS: 17845 /* Insert timestamp if there is romm */ 17846 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17847 case IPOPT_TS_TSONLY: 17848 off = IPOPT_TS_TIMELEN; 17849 break; 17850 case IPOPT_TS_PRESPEC: 17851 case IPOPT_TS_PRESPEC_RFC791: 17852 /* Verify that the address matched */ 17853 off = opt[IPOPT_OFFSET] - 1; 17854 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17855 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17856 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 17857 ipst); 17858 if (dst_ire == NULL) { 17859 /* Not for us */ 17860 break; 17861 } 17862 ire_refrele(dst_ire); 17863 /* FALLTHRU */ 17864 case IPOPT_TS_TSANDADDR: 17865 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17866 break; 17867 default: 17868 /* 17869 * ip_*put_options should have already 17870 * dropped this packet. 17871 */ 17872 cmn_err(CE_PANIC, "ip_rput_local_options: " 17873 "unknown IT - bug in ip_rput_options?\n"); 17874 return (B_TRUE); /* Keep "lint" happy */ 17875 } 17876 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 17877 /* Increase overflow counter */ 17878 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 17879 opt[IPOPT_POS_OV_FLG] = 17880 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 17881 (off << 4)); 17882 break; 17883 } 17884 off = opt[IPOPT_OFFSET] - 1; 17885 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17886 case IPOPT_TS_PRESPEC: 17887 case IPOPT_TS_PRESPEC_RFC791: 17888 case IPOPT_TS_TSANDADDR: 17889 bcopy(&ire->ire_src_addr, (char *)opt + off, 17890 IP_ADDR_LEN); 17891 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17892 /* FALLTHRU */ 17893 case IPOPT_TS_TSONLY: 17894 off = opt[IPOPT_OFFSET] - 1; 17895 /* Compute # of milliseconds since midnight */ 17896 gethrestime(&now); 17897 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 17898 now.tv_nsec / (NANOSEC / MILLISEC); 17899 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 17900 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 17901 break; 17902 } 17903 break; 17904 } 17905 } 17906 return (B_TRUE); 17907 17908 bad_src_route: 17909 q = WR(q); 17910 if (q->q_next != NULL) 17911 ill = q->q_ptr; 17912 else 17913 ill = NULL; 17914 17915 /* make sure we clear any indication of a hardware checksum */ 17916 DB_CKSUMFLAGS(mp) = 0; 17917 zoneid = ipif_lookup_addr_zoneid(ipha->ipha_dst, ill, ipst); 17918 if (zoneid == ALL_ZONES) 17919 freemsg(mp); 17920 else 17921 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 17922 return (B_FALSE); 17923 17924 } 17925 17926 /* 17927 * Process IP options in an inbound packet. If an option affects the 17928 * effective destination address, return the next hop address via dstp. 17929 * Returns -1 if something fails in which case an ICMP error has been sent 17930 * and mp freed. 17931 */ 17932 static int 17933 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp, 17934 ip_stack_t *ipst) 17935 { 17936 ipoptp_t opts; 17937 uchar_t *opt; 17938 uint8_t optval; 17939 uint8_t optlen; 17940 ipaddr_t dst; 17941 intptr_t code = 0; 17942 ire_t *ire = NULL; 17943 zoneid_t zoneid; 17944 ill_t *ill; 17945 17946 ip2dbg(("ip_rput_options\n")); 17947 dst = ipha->ipha_dst; 17948 for (optval = ipoptp_first(&opts, ipha); 17949 optval != IPOPT_EOL; 17950 optval = ipoptp_next(&opts)) { 17951 opt = opts.ipoptp_cur; 17952 optlen = opts.ipoptp_len; 17953 ip2dbg(("ip_rput_options: opt %d, len %d\n", 17954 optval, optlen)); 17955 /* 17956 * Note: we need to verify the checksum before we 17957 * modify anything thus this routine only extracts the next 17958 * hop dst from any source route. 17959 */ 17960 switch (optval) { 17961 uint32_t off; 17962 case IPOPT_SSRR: 17963 case IPOPT_LSRR: 17964 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17965 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17966 if (ire == NULL) { 17967 if (optval == IPOPT_SSRR) { 17968 ip1dbg(("ip_rput_options: not next" 17969 " strict source route 0x%x\n", 17970 ntohl(dst))); 17971 code = (char *)&ipha->ipha_dst - 17972 (char *)ipha; 17973 goto param_prob; /* RouterReq's */ 17974 } 17975 ip2dbg(("ip_rput_options: " 17976 "not next source route 0x%x\n", 17977 ntohl(dst))); 17978 break; 17979 } 17980 ire_refrele(ire); 17981 17982 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17983 ip1dbg(( 17984 "ip_rput_options: bad option offset\n")); 17985 code = (char *)&opt[IPOPT_OLEN] - 17986 (char *)ipha; 17987 goto param_prob; 17988 } 17989 off = opt[IPOPT_OFFSET]; 17990 off--; 17991 redo_srr: 17992 if (optlen < IP_ADDR_LEN || 17993 off > optlen - IP_ADDR_LEN) { 17994 /* End of source route */ 17995 ip1dbg(("ip_rput_options: end of SR\n")); 17996 break; 17997 } 17998 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17999 ip1dbg(("ip_rput_options: next hop 0x%x\n", 18000 ntohl(dst))); 18001 18002 /* 18003 * Check if our address is present more than 18004 * once as consecutive hops in source route. 18005 * XXX verify per-interface ip_forwarding 18006 * for source route? 18007 */ 18008 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 18009 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 18010 18011 if (ire != NULL) { 18012 ire_refrele(ire); 18013 off += IP_ADDR_LEN; 18014 goto redo_srr; 18015 } 18016 18017 if (dst == htonl(INADDR_LOOPBACK)) { 18018 ip1dbg(("ip_rput_options: loopback addr in " 18019 "source route!\n")); 18020 goto bad_src_route; 18021 } 18022 /* 18023 * For strict: verify that dst is directly 18024 * reachable. 18025 */ 18026 if (optval == IPOPT_SSRR) { 18027 ire = ire_ftable_lookup(dst, 0, 0, 18028 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 18029 MBLK_GETLABEL(mp), 18030 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 18031 if (ire == NULL) { 18032 ip1dbg(("ip_rput_options: SSRR not " 18033 "directly reachable: 0x%x\n", 18034 ntohl(dst))); 18035 goto bad_src_route; 18036 } 18037 ire_refrele(ire); 18038 } 18039 /* 18040 * Defer update of the offset and the record route 18041 * until the packet is forwarded. 18042 */ 18043 break; 18044 case IPOPT_RR: 18045 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 18046 ip1dbg(( 18047 "ip_rput_options: bad option offset\n")); 18048 code = (char *)&opt[IPOPT_OLEN] - 18049 (char *)ipha; 18050 goto param_prob; 18051 } 18052 break; 18053 case IPOPT_TS: 18054 /* 18055 * Verify that length >= 5 and that there is either 18056 * room for another timestamp or that the overflow 18057 * counter is not maxed out. 18058 */ 18059 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 18060 if (optlen < IPOPT_MINLEN_IT) { 18061 goto param_prob; 18062 } 18063 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 18064 ip1dbg(( 18065 "ip_rput_options: bad option offset\n")); 18066 code = (char *)&opt[IPOPT_OFFSET] - 18067 (char *)ipha; 18068 goto param_prob; 18069 } 18070 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 18071 case IPOPT_TS_TSONLY: 18072 off = IPOPT_TS_TIMELEN; 18073 break; 18074 case IPOPT_TS_TSANDADDR: 18075 case IPOPT_TS_PRESPEC: 18076 case IPOPT_TS_PRESPEC_RFC791: 18077 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 18078 break; 18079 default: 18080 code = (char *)&opt[IPOPT_POS_OV_FLG] - 18081 (char *)ipha; 18082 goto param_prob; 18083 } 18084 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 18085 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 18086 /* 18087 * No room and the overflow counter is 15 18088 * already. 18089 */ 18090 goto param_prob; 18091 } 18092 break; 18093 } 18094 } 18095 18096 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 18097 *dstp = dst; 18098 return (0); 18099 } 18100 18101 ip1dbg(("ip_rput_options: error processing IP options.")); 18102 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 18103 18104 param_prob: 18105 q = WR(q); 18106 if (q->q_next != NULL) 18107 ill = q->q_ptr; 18108 else 18109 ill = NULL; 18110 18111 /* make sure we clear any indication of a hardware checksum */ 18112 DB_CKSUMFLAGS(mp) = 0; 18113 /* Don't know whether this is for non-global or global/forwarding */ 18114 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 18115 if (zoneid == ALL_ZONES) 18116 freemsg(mp); 18117 else 18118 icmp_param_problem(q, mp, (uint8_t)code, zoneid, ipst); 18119 return (-1); 18120 18121 bad_src_route: 18122 q = WR(q); 18123 if (q->q_next != NULL) 18124 ill = q->q_ptr; 18125 else 18126 ill = NULL; 18127 18128 /* make sure we clear any indication of a hardware checksum */ 18129 DB_CKSUMFLAGS(mp) = 0; 18130 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 18131 if (zoneid == ALL_ZONES) 18132 freemsg(mp); 18133 else 18134 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 18135 return (-1); 18136 } 18137 18138 /* 18139 * IP & ICMP info in >=14 msg's ... 18140 * - ip fixed part (mib2_ip_t) 18141 * - icmp fixed part (mib2_icmp_t) 18142 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 18143 * - ipRouteEntryTable (ip 21) all IPv4 IREs 18144 * - ipNetToMediaEntryTable (ip 22) [filled in by the arp module] 18145 * - ipRouteAttributeTable (ip 102) labeled routes 18146 * - ip multicast membership (ip_member_t) 18147 * - ip multicast source filtering (ip_grpsrc_t) 18148 * - igmp fixed part (struct igmpstat) 18149 * - multicast routing stats (struct mrtstat) 18150 * - multicast routing vifs (array of struct vifctl) 18151 * - multicast routing routes (array of struct mfcctl) 18152 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 18153 * One per ill plus one generic 18154 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 18155 * One per ill plus one generic 18156 * - ipv6RouteEntry all IPv6 IREs 18157 * - ipv6RouteAttributeTable (ip6 102) labeled routes 18158 * - ipv6NetToMediaEntry all Neighbor Cache entries 18159 * - ipv6AddrEntry all IPv6 ipifs 18160 * - ipv6 multicast membership (ipv6_member_t) 18161 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 18162 * 18163 * MIB2_IP_MEDIA is filled in by the arp module with ARP cache entries. 18164 * 18165 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 18166 * already filled in by the caller. 18167 * Return value of 0 indicates that no messages were sent and caller 18168 * should free mpctl. 18169 */ 18170 int 18171 ip_snmp_get(queue_t *q, mblk_t *mpctl) 18172 { 18173 ip_stack_t *ipst; 18174 sctp_stack_t *sctps; 18175 18176 18177 if (q->q_next != NULL) { 18178 ipst = ILLQ_TO_IPST(q); 18179 } else { 18180 ipst = CONNQ_TO_IPST(q); 18181 } 18182 ASSERT(ipst != NULL); 18183 sctps = ipst->ips_netstack->netstack_sctp; 18184 18185 if (mpctl == NULL || mpctl->b_cont == NULL) { 18186 return (0); 18187 } 18188 18189 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl, 18190 ipst)) == NULL) { 18191 return (1); 18192 } 18193 18194 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst)) == NULL) { 18195 return (1); 18196 } 18197 18198 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) { 18199 return (1); 18200 } 18201 18202 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) { 18203 return (1); 18204 } 18205 18206 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) { 18207 return (1); 18208 } 18209 18210 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) { 18211 return (1); 18212 } 18213 18214 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst)) == NULL) { 18215 return (1); 18216 } 18217 18218 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst)) == NULL) { 18219 return (1); 18220 } 18221 18222 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) { 18223 return (1); 18224 } 18225 18226 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) { 18227 return (1); 18228 } 18229 18230 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) { 18231 return (1); 18232 } 18233 18234 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) { 18235 return (1); 18236 } 18237 18238 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) { 18239 return (1); 18240 } 18241 18242 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) { 18243 return (1); 18244 } 18245 18246 if ((mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, ipst)) == NULL) { 18247 return (1); 18248 } 18249 18250 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, ipst); 18251 if (mpctl == NULL) { 18252 return (1); 18253 } 18254 18255 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) { 18256 return (1); 18257 } 18258 freemsg(mpctl); 18259 return (1); 18260 } 18261 18262 18263 /* Get global (legacy) IPv4 statistics */ 18264 static mblk_t * 18265 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib, 18266 ip_stack_t *ipst) 18267 { 18268 mib2_ip_t old_ip_mib; 18269 struct opthdr *optp; 18270 mblk_t *mp2ctl; 18271 18272 /* 18273 * make a copy of the original message 18274 */ 18275 mp2ctl = copymsg(mpctl); 18276 18277 /* fixed length IP structure... */ 18278 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18279 optp->level = MIB2_IP; 18280 optp->name = 0; 18281 SET_MIB(old_ip_mib.ipForwarding, 18282 (WE_ARE_FORWARDING(ipst) ? 1 : 2)); 18283 SET_MIB(old_ip_mib.ipDefaultTTL, 18284 (uint32_t)ipst->ips_ip_def_ttl); 18285 SET_MIB(old_ip_mib.ipReasmTimeout, 18286 ipst->ips_ip_g_frag_timeout); 18287 SET_MIB(old_ip_mib.ipAddrEntrySize, 18288 sizeof (mib2_ipAddrEntry_t)); 18289 SET_MIB(old_ip_mib.ipRouteEntrySize, 18290 sizeof (mib2_ipRouteEntry_t)); 18291 SET_MIB(old_ip_mib.ipNetToMediaEntrySize, 18292 sizeof (mib2_ipNetToMediaEntry_t)); 18293 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 18294 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 18295 SET_MIB(old_ip_mib.ipRouteAttributeSize, 18296 sizeof (mib2_ipAttributeEntry_t)); 18297 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 18298 18299 /* 18300 * Grab the statistics from the new IP MIB 18301 */ 18302 SET_MIB(old_ip_mib.ipInReceives, 18303 (uint32_t)ipmib->ipIfStatsHCInReceives); 18304 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors); 18305 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors); 18306 SET_MIB(old_ip_mib.ipForwDatagrams, 18307 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams); 18308 SET_MIB(old_ip_mib.ipInUnknownProtos, 18309 ipmib->ipIfStatsInUnknownProtos); 18310 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards); 18311 SET_MIB(old_ip_mib.ipInDelivers, 18312 (uint32_t)ipmib->ipIfStatsHCInDelivers); 18313 SET_MIB(old_ip_mib.ipOutRequests, 18314 (uint32_t)ipmib->ipIfStatsHCOutRequests); 18315 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards); 18316 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes); 18317 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds); 18318 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs); 18319 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails); 18320 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs); 18321 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails); 18322 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates); 18323 18324 /* ipRoutingDiscards is not being used */ 18325 SET_MIB(old_ip_mib.ipRoutingDiscards, 0); 18326 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs); 18327 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts); 18328 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs); 18329 SET_MIB(old_ip_mib.ipReasmDuplicates, 18330 ipmib->ipIfStatsReasmDuplicates); 18331 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups); 18332 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits); 18333 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs); 18334 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows); 18335 SET_MIB(old_ip_mib.rawipInOverflows, 18336 ipmib->rawipIfStatsInOverflows); 18337 18338 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded); 18339 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed); 18340 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion); 18341 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion); 18342 SET_MIB(old_ip_mib.ipOutSwitchIPv6, 18343 ipmib->ipIfStatsOutSwitchIPVersion); 18344 18345 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib, 18346 (int)sizeof (old_ip_mib))) { 18347 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 18348 (uint_t)sizeof (old_ip_mib))); 18349 } 18350 18351 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18352 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 18353 (int)optp->level, (int)optp->name, (int)optp->len)); 18354 qreply(q, mpctl); 18355 return (mp2ctl); 18356 } 18357 18358 /* Per interface IPv4 statistics */ 18359 static mblk_t * 18360 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18361 { 18362 struct opthdr *optp; 18363 mblk_t *mp2ctl; 18364 ill_t *ill; 18365 ill_walk_context_t ctx; 18366 mblk_t *mp_tail = NULL; 18367 mib2_ipIfStatsEntry_t global_ip_mib; 18368 18369 /* 18370 * Make a copy of the original message 18371 */ 18372 mp2ctl = copymsg(mpctl); 18373 18374 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18375 optp->level = MIB2_IP; 18376 optp->name = MIB2_IP_TRAFFIC_STATS; 18377 /* Include "unknown interface" ip_mib */ 18378 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 18379 ipst->ips_ip_mib.ipIfStatsIfIndex = 18380 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 18381 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding, 18382 (ipst->ips_ip_g_forward ? 1 : 2)); 18383 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL, 18384 (uint32_t)ipst->ips_ip_def_ttl); 18385 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize, 18386 sizeof (mib2_ipIfStatsEntry_t)); 18387 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize, 18388 sizeof (mib2_ipAddrEntry_t)); 18389 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize, 18390 sizeof (mib2_ipRouteEntry_t)); 18391 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize, 18392 sizeof (mib2_ipNetToMediaEntry_t)); 18393 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize, 18394 sizeof (ip_member_t)); 18395 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize, 18396 sizeof (ip_grpsrc_t)); 18397 18398 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18399 (char *)&ipst->ips_ip_mib, (int)sizeof (ipst->ips_ip_mib))) { 18400 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18401 "failed to allocate %u bytes\n", 18402 (uint_t)sizeof (ipst->ips_ip_mib))); 18403 } 18404 18405 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib)); 18406 18407 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18408 ill = ILL_START_WALK_V4(&ctx, ipst); 18409 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18410 ill->ill_ip_mib->ipIfStatsIfIndex = 18411 ill->ill_phyint->phyint_ifindex; 18412 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 18413 (ipst->ips_ip_g_forward ? 1 : 2)); 18414 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL, 18415 (uint32_t)ipst->ips_ip_def_ttl); 18416 18417 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib); 18418 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18419 (char *)ill->ill_ip_mib, 18420 (int)sizeof (*ill->ill_ip_mib))) { 18421 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18422 "failed to allocate %u bytes\n", 18423 (uint_t)sizeof (*ill->ill_ip_mib))); 18424 } 18425 } 18426 rw_exit(&ipst->ips_ill_g_lock); 18427 18428 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18429 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18430 "level %d, name %d, len %d\n", 18431 (int)optp->level, (int)optp->name, (int)optp->len)); 18432 qreply(q, mpctl); 18433 18434 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst)); 18435 } 18436 18437 /* Global IPv4 ICMP statistics */ 18438 static mblk_t * 18439 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18440 { 18441 struct opthdr *optp; 18442 mblk_t *mp2ctl; 18443 18444 /* 18445 * Make a copy of the original message 18446 */ 18447 mp2ctl = copymsg(mpctl); 18448 18449 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18450 optp->level = MIB2_ICMP; 18451 optp->name = 0; 18452 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib, 18453 (int)sizeof (ipst->ips_icmp_mib))) { 18454 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 18455 (uint_t)sizeof (ipst->ips_icmp_mib))); 18456 } 18457 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18458 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 18459 (int)optp->level, (int)optp->name, (int)optp->len)); 18460 qreply(q, mpctl); 18461 return (mp2ctl); 18462 } 18463 18464 /* Global IPv4 IGMP statistics */ 18465 static mblk_t * 18466 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18467 { 18468 struct opthdr *optp; 18469 mblk_t *mp2ctl; 18470 18471 /* 18472 * make a copy of the original message 18473 */ 18474 mp2ctl = copymsg(mpctl); 18475 18476 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18477 optp->level = EXPER_IGMP; 18478 optp->name = 0; 18479 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat, 18480 (int)sizeof (ipst->ips_igmpstat))) { 18481 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 18482 (uint_t)sizeof (ipst->ips_igmpstat))); 18483 } 18484 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18485 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 18486 (int)optp->level, (int)optp->name, (int)optp->len)); 18487 qreply(q, mpctl); 18488 return (mp2ctl); 18489 } 18490 18491 /* Global IPv4 Multicast Routing statistics */ 18492 static mblk_t * 18493 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18494 { 18495 struct opthdr *optp; 18496 mblk_t *mp2ctl; 18497 18498 /* 18499 * make a copy of the original message 18500 */ 18501 mp2ctl = copymsg(mpctl); 18502 18503 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18504 optp->level = EXPER_DVMRP; 18505 optp->name = 0; 18506 if (!ip_mroute_stats(mpctl->b_cont, ipst)) { 18507 ip0dbg(("ip_mroute_stats: failed\n")); 18508 } 18509 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18510 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 18511 (int)optp->level, (int)optp->name, (int)optp->len)); 18512 qreply(q, mpctl); 18513 return (mp2ctl); 18514 } 18515 18516 /* IPv4 address information */ 18517 static mblk_t * 18518 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18519 { 18520 struct opthdr *optp; 18521 mblk_t *mp2ctl; 18522 mblk_t *mp_tail = NULL; 18523 ill_t *ill; 18524 ipif_t *ipif; 18525 uint_t bitval; 18526 mib2_ipAddrEntry_t mae; 18527 zoneid_t zoneid; 18528 ill_walk_context_t ctx; 18529 18530 /* 18531 * make a copy of the original message 18532 */ 18533 mp2ctl = copymsg(mpctl); 18534 18535 /* ipAddrEntryTable */ 18536 18537 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18538 optp->level = MIB2_IP; 18539 optp->name = MIB2_IP_ADDR; 18540 zoneid = Q_TO_CONN(q)->conn_zoneid; 18541 18542 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18543 ill = ILL_START_WALK_V4(&ctx, ipst); 18544 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18545 for (ipif = ill->ill_ipif; ipif != NULL; 18546 ipif = ipif->ipif_next) { 18547 if (ipif->ipif_zoneid != zoneid && 18548 ipif->ipif_zoneid != ALL_ZONES) 18549 continue; 18550 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18551 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18552 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18553 18554 (void) ipif_get_name(ipif, 18555 mae.ipAdEntIfIndex.o_bytes, 18556 OCTET_LENGTH); 18557 mae.ipAdEntIfIndex.o_length = 18558 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 18559 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 18560 mae.ipAdEntNetMask = ipif->ipif_net_mask; 18561 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 18562 mae.ipAdEntInfo.ae_subnet_len = 18563 ip_mask_to_plen(ipif->ipif_net_mask); 18564 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 18565 for (bitval = 1; 18566 bitval && 18567 !(bitval & ipif->ipif_brd_addr); 18568 bitval <<= 1) 18569 noop; 18570 mae.ipAdEntBcastAddr = bitval; 18571 mae.ipAdEntReasmMaxSize = IP_MAXPACKET; 18572 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 18573 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 18574 mae.ipAdEntInfo.ae_broadcast_addr = 18575 ipif->ipif_brd_addr; 18576 mae.ipAdEntInfo.ae_pp_dst_addr = 18577 ipif->ipif_pp_dst_addr; 18578 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 18579 ill->ill_flags | ill->ill_phyint->phyint_flags; 18580 mae.ipAdEntRetransmitTime = AR_EQ_DEFAULT_XMIT_INTERVAL; 18581 18582 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18583 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 18584 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 18585 "allocate %u bytes\n", 18586 (uint_t)sizeof (mib2_ipAddrEntry_t))); 18587 } 18588 } 18589 } 18590 rw_exit(&ipst->ips_ill_g_lock); 18591 18592 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18593 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 18594 (int)optp->level, (int)optp->name, (int)optp->len)); 18595 qreply(q, mpctl); 18596 return (mp2ctl); 18597 } 18598 18599 /* IPv6 address information */ 18600 static mblk_t * 18601 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18602 { 18603 struct opthdr *optp; 18604 mblk_t *mp2ctl; 18605 mblk_t *mp_tail = NULL; 18606 ill_t *ill; 18607 ipif_t *ipif; 18608 mib2_ipv6AddrEntry_t mae6; 18609 zoneid_t zoneid; 18610 ill_walk_context_t ctx; 18611 18612 /* 18613 * make a copy of the original message 18614 */ 18615 mp2ctl = copymsg(mpctl); 18616 18617 /* ipv6AddrEntryTable */ 18618 18619 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18620 optp->level = MIB2_IP6; 18621 optp->name = MIB2_IP6_ADDR; 18622 zoneid = Q_TO_CONN(q)->conn_zoneid; 18623 18624 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18625 ill = ILL_START_WALK_V6(&ctx, ipst); 18626 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18627 for (ipif = ill->ill_ipif; ipif != NULL; 18628 ipif = ipif->ipif_next) { 18629 if (ipif->ipif_zoneid != zoneid && 18630 ipif->ipif_zoneid != ALL_ZONES) 18631 continue; 18632 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18633 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18634 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18635 18636 (void) ipif_get_name(ipif, 18637 mae6.ipv6AddrIfIndex.o_bytes, 18638 OCTET_LENGTH); 18639 mae6.ipv6AddrIfIndex.o_length = 18640 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 18641 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 18642 mae6.ipv6AddrPfxLength = 18643 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 18644 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 18645 mae6.ipv6AddrInfo.ae_subnet_len = 18646 mae6.ipv6AddrPfxLength; 18647 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 18648 18649 /* Type: stateless(1), stateful(2), unknown(3) */ 18650 if (ipif->ipif_flags & IPIF_ADDRCONF) 18651 mae6.ipv6AddrType = 1; 18652 else 18653 mae6.ipv6AddrType = 2; 18654 /* Anycast: true(1), false(2) */ 18655 if (ipif->ipif_flags & IPIF_ANYCAST) 18656 mae6.ipv6AddrAnycastFlag = 1; 18657 else 18658 mae6.ipv6AddrAnycastFlag = 2; 18659 18660 /* 18661 * Address status: preferred(1), deprecated(2), 18662 * invalid(3), inaccessible(4), unknown(5) 18663 */ 18664 if (ipif->ipif_flags & IPIF_NOLOCAL) 18665 mae6.ipv6AddrStatus = 3; 18666 else if (ipif->ipif_flags & IPIF_DEPRECATED) 18667 mae6.ipv6AddrStatus = 2; 18668 else 18669 mae6.ipv6AddrStatus = 1; 18670 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 18671 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 18672 mae6.ipv6AddrInfo.ae_pp_dst_addr = 18673 ipif->ipif_v6pp_dst_addr; 18674 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 18675 ill->ill_flags | ill->ill_phyint->phyint_flags; 18676 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET; 18677 mae6.ipv6AddrIdentifier = ill->ill_token; 18678 mae6.ipv6AddrIdentifierLen = ill->ill_token_length; 18679 mae6.ipv6AddrReachableTime = ill->ill_reachable_time; 18680 mae6.ipv6AddrRetransmitTime = 18681 ill->ill_reachable_retrans_time; 18682 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18683 (char *)&mae6, 18684 (int)sizeof (mib2_ipv6AddrEntry_t))) { 18685 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 18686 "allocate %u bytes\n", 18687 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 18688 } 18689 } 18690 } 18691 rw_exit(&ipst->ips_ill_g_lock); 18692 18693 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18694 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 18695 (int)optp->level, (int)optp->name, (int)optp->len)); 18696 qreply(q, mpctl); 18697 return (mp2ctl); 18698 } 18699 18700 /* IPv4 multicast group membership. */ 18701 static mblk_t * 18702 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18703 { 18704 struct opthdr *optp; 18705 mblk_t *mp2ctl; 18706 ill_t *ill; 18707 ipif_t *ipif; 18708 ilm_t *ilm; 18709 ip_member_t ipm; 18710 mblk_t *mp_tail = NULL; 18711 ill_walk_context_t ctx; 18712 zoneid_t zoneid; 18713 18714 /* 18715 * make a copy of the original message 18716 */ 18717 mp2ctl = copymsg(mpctl); 18718 zoneid = Q_TO_CONN(q)->conn_zoneid; 18719 18720 /* ipGroupMember table */ 18721 optp = (struct opthdr *)&mpctl->b_rptr[ 18722 sizeof (struct T_optmgmt_ack)]; 18723 optp->level = MIB2_IP; 18724 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 18725 18726 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18727 ill = ILL_START_WALK_V4(&ctx, ipst); 18728 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18729 ILM_WALKER_HOLD(ill); 18730 for (ipif = ill->ill_ipif; ipif != NULL; 18731 ipif = ipif->ipif_next) { 18732 if (ipif->ipif_zoneid != zoneid && 18733 ipif->ipif_zoneid != ALL_ZONES) 18734 continue; /* not this zone */ 18735 (void) ipif_get_name(ipif, 18736 ipm.ipGroupMemberIfIndex.o_bytes, 18737 OCTET_LENGTH); 18738 ipm.ipGroupMemberIfIndex.o_length = 18739 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 18740 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18741 ASSERT(ilm->ilm_ipif != NULL); 18742 ASSERT(ilm->ilm_ill == NULL); 18743 if (ilm->ilm_ipif != ipif) 18744 continue; 18745 ipm.ipGroupMemberAddress = ilm->ilm_addr; 18746 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 18747 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 18748 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18749 (char *)&ipm, (int)sizeof (ipm))) { 18750 ip1dbg(("ip_snmp_get_mib2_ip_group: " 18751 "failed to allocate %u bytes\n", 18752 (uint_t)sizeof (ipm))); 18753 } 18754 } 18755 } 18756 ILM_WALKER_RELE(ill); 18757 } 18758 rw_exit(&ipst->ips_ill_g_lock); 18759 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18760 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18761 (int)optp->level, (int)optp->name, (int)optp->len)); 18762 qreply(q, mpctl); 18763 return (mp2ctl); 18764 } 18765 18766 /* IPv6 multicast group membership. */ 18767 static mblk_t * 18768 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18769 { 18770 struct opthdr *optp; 18771 mblk_t *mp2ctl; 18772 ill_t *ill; 18773 ilm_t *ilm; 18774 ipv6_member_t ipm6; 18775 mblk_t *mp_tail = NULL; 18776 ill_walk_context_t ctx; 18777 zoneid_t zoneid; 18778 18779 /* 18780 * make a copy of the original message 18781 */ 18782 mp2ctl = copymsg(mpctl); 18783 zoneid = Q_TO_CONN(q)->conn_zoneid; 18784 18785 /* ip6GroupMember table */ 18786 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18787 optp->level = MIB2_IP6; 18788 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 18789 18790 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18791 ill = ILL_START_WALK_V6(&ctx, ipst); 18792 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18793 ILM_WALKER_HOLD(ill); 18794 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 18795 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18796 ASSERT(ilm->ilm_ipif == NULL); 18797 ASSERT(ilm->ilm_ill != NULL); 18798 if (ilm->ilm_zoneid != zoneid) 18799 continue; /* not this zone */ 18800 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 18801 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 18802 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 18803 if (!snmp_append_data2(mpctl->b_cont, 18804 &mp_tail, 18805 (char *)&ipm6, (int)sizeof (ipm6))) { 18806 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 18807 "failed to allocate %u bytes\n", 18808 (uint_t)sizeof (ipm6))); 18809 } 18810 } 18811 ILM_WALKER_RELE(ill); 18812 } 18813 rw_exit(&ipst->ips_ill_g_lock); 18814 18815 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18816 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18817 (int)optp->level, (int)optp->name, (int)optp->len)); 18818 qreply(q, mpctl); 18819 return (mp2ctl); 18820 } 18821 18822 /* IP multicast filtered sources */ 18823 static mblk_t * 18824 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18825 { 18826 struct opthdr *optp; 18827 mblk_t *mp2ctl; 18828 ill_t *ill; 18829 ipif_t *ipif; 18830 ilm_t *ilm; 18831 ip_grpsrc_t ips; 18832 mblk_t *mp_tail = NULL; 18833 ill_walk_context_t ctx; 18834 zoneid_t zoneid; 18835 int i; 18836 slist_t *sl; 18837 18838 /* 18839 * make a copy of the original message 18840 */ 18841 mp2ctl = copymsg(mpctl); 18842 zoneid = Q_TO_CONN(q)->conn_zoneid; 18843 18844 /* ipGroupSource table */ 18845 optp = (struct opthdr *)&mpctl->b_rptr[ 18846 sizeof (struct T_optmgmt_ack)]; 18847 optp->level = MIB2_IP; 18848 optp->name = EXPER_IP_GROUP_SOURCES; 18849 18850 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18851 ill = ILL_START_WALK_V4(&ctx, ipst); 18852 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18853 ILM_WALKER_HOLD(ill); 18854 for (ipif = ill->ill_ipif; ipif != NULL; 18855 ipif = ipif->ipif_next) { 18856 if (ipif->ipif_zoneid != zoneid) 18857 continue; /* not this zone */ 18858 (void) ipif_get_name(ipif, 18859 ips.ipGroupSourceIfIndex.o_bytes, 18860 OCTET_LENGTH); 18861 ips.ipGroupSourceIfIndex.o_length = 18862 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 18863 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18864 ASSERT(ilm->ilm_ipif != NULL); 18865 ASSERT(ilm->ilm_ill == NULL); 18866 sl = ilm->ilm_filter; 18867 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 18868 continue; 18869 ips.ipGroupSourceGroup = ilm->ilm_addr; 18870 for (i = 0; i < sl->sl_numsrc; i++) { 18871 if (!IN6_IS_ADDR_V4MAPPED( 18872 &sl->sl_addr[i])) 18873 continue; 18874 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 18875 ips.ipGroupSourceAddress); 18876 if (snmp_append_data2(mpctl->b_cont, 18877 &mp_tail, (char *)&ips, 18878 (int)sizeof (ips)) == 0) { 18879 ip1dbg(("ip_snmp_get_mib2_" 18880 "ip_group_src: failed to " 18881 "allocate %u bytes\n", 18882 (uint_t)sizeof (ips))); 18883 } 18884 } 18885 } 18886 } 18887 ILM_WALKER_RELE(ill); 18888 } 18889 rw_exit(&ipst->ips_ill_g_lock); 18890 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18891 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18892 (int)optp->level, (int)optp->name, (int)optp->len)); 18893 qreply(q, mpctl); 18894 return (mp2ctl); 18895 } 18896 18897 /* IPv6 multicast filtered sources. */ 18898 static mblk_t * 18899 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18900 { 18901 struct opthdr *optp; 18902 mblk_t *mp2ctl; 18903 ill_t *ill; 18904 ilm_t *ilm; 18905 ipv6_grpsrc_t ips6; 18906 mblk_t *mp_tail = NULL; 18907 ill_walk_context_t ctx; 18908 zoneid_t zoneid; 18909 int i; 18910 slist_t *sl; 18911 18912 /* 18913 * make a copy of the original message 18914 */ 18915 mp2ctl = copymsg(mpctl); 18916 zoneid = Q_TO_CONN(q)->conn_zoneid; 18917 18918 /* ip6GroupMember table */ 18919 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18920 optp->level = MIB2_IP6; 18921 optp->name = EXPER_IP6_GROUP_SOURCES; 18922 18923 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18924 ill = ILL_START_WALK_V6(&ctx, ipst); 18925 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18926 ILM_WALKER_HOLD(ill); 18927 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 18928 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18929 ASSERT(ilm->ilm_ipif == NULL); 18930 ASSERT(ilm->ilm_ill != NULL); 18931 sl = ilm->ilm_filter; 18932 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 18933 continue; 18934 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 18935 for (i = 0; i < sl->sl_numsrc; i++) { 18936 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 18937 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18938 (char *)&ips6, (int)sizeof (ips6))) { 18939 ip1dbg(("ip_snmp_get_mib2_ip6_" 18940 "group_src: failed to allocate " 18941 "%u bytes\n", 18942 (uint_t)sizeof (ips6))); 18943 } 18944 } 18945 } 18946 ILM_WALKER_RELE(ill); 18947 } 18948 rw_exit(&ipst->ips_ill_g_lock); 18949 18950 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18951 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18952 (int)optp->level, (int)optp->name, (int)optp->len)); 18953 qreply(q, mpctl); 18954 return (mp2ctl); 18955 } 18956 18957 /* Multicast routing virtual interface table. */ 18958 static mblk_t * 18959 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18960 { 18961 struct opthdr *optp; 18962 mblk_t *mp2ctl; 18963 18964 /* 18965 * make a copy of the original message 18966 */ 18967 mp2ctl = copymsg(mpctl); 18968 18969 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18970 optp->level = EXPER_DVMRP; 18971 optp->name = EXPER_DVMRP_VIF; 18972 if (!ip_mroute_vif(mpctl->b_cont, ipst)) { 18973 ip0dbg(("ip_mroute_vif: failed\n")); 18974 } 18975 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18976 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 18977 (int)optp->level, (int)optp->name, (int)optp->len)); 18978 qreply(q, mpctl); 18979 return (mp2ctl); 18980 } 18981 18982 /* Multicast routing table. */ 18983 static mblk_t * 18984 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18985 { 18986 struct opthdr *optp; 18987 mblk_t *mp2ctl; 18988 18989 /* 18990 * make a copy of the original message 18991 */ 18992 mp2ctl = copymsg(mpctl); 18993 18994 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18995 optp->level = EXPER_DVMRP; 18996 optp->name = EXPER_DVMRP_MRT; 18997 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) { 18998 ip0dbg(("ip_mroute_mrt: failed\n")); 18999 } 19000 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19001 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 19002 (int)optp->level, (int)optp->name, (int)optp->len)); 19003 qreply(q, mpctl); 19004 return (mp2ctl); 19005 } 19006 19007 /* 19008 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 19009 * in one IRE walk. 19010 */ 19011 static mblk_t * 19012 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19013 { 19014 struct opthdr *optp; 19015 mblk_t *mp2ctl; /* Returned */ 19016 mblk_t *mp3ctl; /* nettomedia */ 19017 mblk_t *mp4ctl; /* routeattrs */ 19018 iproutedata_t ird; 19019 zoneid_t zoneid; 19020 19021 /* 19022 * make copies of the original message 19023 * - mp2ctl is returned unchanged to the caller for his use 19024 * - mpctl is sent upstream as ipRouteEntryTable 19025 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 19026 * - mp4ctl is sent upstream as ipRouteAttributeTable 19027 */ 19028 mp2ctl = copymsg(mpctl); 19029 mp3ctl = copymsg(mpctl); 19030 mp4ctl = copymsg(mpctl); 19031 if (mp3ctl == NULL || mp4ctl == NULL) { 19032 freemsg(mp4ctl); 19033 freemsg(mp3ctl); 19034 freemsg(mp2ctl); 19035 freemsg(mpctl); 19036 return (NULL); 19037 } 19038 19039 bzero(&ird, sizeof (ird)); 19040 19041 ird.ird_route.lp_head = mpctl->b_cont; 19042 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 19043 ird.ird_attrs.lp_head = mp4ctl->b_cont; 19044 19045 zoneid = Q_TO_CONN(q)->conn_zoneid; 19046 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst); 19047 if (zoneid == GLOBAL_ZONEID) { 19048 /* 19049 * Those IREs are used by Mobile-IP; since mipagent(1M) 19050 * requires the sys_net_config or sys_ip_config privilege, 19051 * it can only run in the global zone or an exclusive-IP zone, 19052 * and both those have a conn_zoneid == GLOBAL_ZONEID. 19053 */ 19054 ire_walk_srcif_table_v4(ip_snmp_get2_v4, &ird, ipst); 19055 ire_walk_ill_mrtun(0, 0, ip_snmp_get2_v4, &ird, NULL, ipst); 19056 } 19057 19058 /* ipRouteEntryTable in mpctl */ 19059 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19060 optp->level = MIB2_IP; 19061 optp->name = MIB2_IP_ROUTE; 19062 optp->len = msgdsize(ird.ird_route.lp_head); 19063 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19064 (int)optp->level, (int)optp->name, (int)optp->len)); 19065 qreply(q, mpctl); 19066 19067 /* ipNetToMediaEntryTable in mp3ctl */ 19068 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19069 optp->level = MIB2_IP; 19070 optp->name = MIB2_IP_MEDIA; 19071 optp->len = msgdsize(ird.ird_netmedia.lp_head); 19072 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19073 (int)optp->level, (int)optp->name, (int)optp->len)); 19074 qreply(q, mp3ctl); 19075 19076 /* ipRouteAttributeTable in mp4ctl */ 19077 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19078 optp->level = MIB2_IP; 19079 optp->name = EXPER_IP_RTATTR; 19080 optp->len = msgdsize(ird.ird_attrs.lp_head); 19081 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19082 (int)optp->level, (int)optp->name, (int)optp->len)); 19083 if (optp->len == 0) 19084 freemsg(mp4ctl); 19085 else 19086 qreply(q, mp4ctl); 19087 19088 return (mp2ctl); 19089 } 19090 19091 /* 19092 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 19093 * ipv6NetToMediaEntryTable in an NDP walk. 19094 */ 19095 static mblk_t * 19096 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19097 { 19098 struct opthdr *optp; 19099 mblk_t *mp2ctl; /* Returned */ 19100 mblk_t *mp3ctl; /* nettomedia */ 19101 mblk_t *mp4ctl; /* routeattrs */ 19102 iproutedata_t ird; 19103 zoneid_t zoneid; 19104 19105 /* 19106 * make copies of the original message 19107 * - mp2ctl is returned unchanged to the caller for his use 19108 * - mpctl is sent upstream as ipv6RouteEntryTable 19109 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 19110 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 19111 */ 19112 mp2ctl = copymsg(mpctl); 19113 mp3ctl = copymsg(mpctl); 19114 mp4ctl = copymsg(mpctl); 19115 if (mp3ctl == NULL || mp4ctl == NULL) { 19116 freemsg(mp4ctl); 19117 freemsg(mp3ctl); 19118 freemsg(mp2ctl); 19119 freemsg(mpctl); 19120 return (NULL); 19121 } 19122 19123 bzero(&ird, sizeof (ird)); 19124 19125 ird.ird_route.lp_head = mpctl->b_cont; 19126 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 19127 ird.ird_attrs.lp_head = mp4ctl->b_cont; 19128 19129 zoneid = Q_TO_CONN(q)->conn_zoneid; 19130 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst); 19131 19132 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19133 optp->level = MIB2_IP6; 19134 optp->name = MIB2_IP6_ROUTE; 19135 optp->len = msgdsize(ird.ird_route.lp_head); 19136 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19137 (int)optp->level, (int)optp->name, (int)optp->len)); 19138 qreply(q, mpctl); 19139 19140 /* ipv6NetToMediaEntryTable in mp3ctl */ 19141 ndp_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst); 19142 19143 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19144 optp->level = MIB2_IP6; 19145 optp->name = MIB2_IP6_MEDIA; 19146 optp->len = msgdsize(ird.ird_netmedia.lp_head); 19147 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19148 (int)optp->level, (int)optp->name, (int)optp->len)); 19149 qreply(q, mp3ctl); 19150 19151 /* ipv6RouteAttributeTable in mp4ctl */ 19152 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19153 optp->level = MIB2_IP6; 19154 optp->name = EXPER_IP_RTATTR; 19155 optp->len = msgdsize(ird.ird_attrs.lp_head); 19156 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19157 (int)optp->level, (int)optp->name, (int)optp->len)); 19158 if (optp->len == 0) 19159 freemsg(mp4ctl); 19160 else 19161 qreply(q, mp4ctl); 19162 19163 return (mp2ctl); 19164 } 19165 19166 /* 19167 * IPv6 mib: One per ill 19168 */ 19169 static mblk_t * 19170 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19171 { 19172 struct opthdr *optp; 19173 mblk_t *mp2ctl; 19174 ill_t *ill; 19175 ill_walk_context_t ctx; 19176 mblk_t *mp_tail = NULL; 19177 19178 /* 19179 * Make a copy of the original message 19180 */ 19181 mp2ctl = copymsg(mpctl); 19182 19183 /* fixed length IPv6 structure ... */ 19184 19185 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19186 optp->level = MIB2_IP6; 19187 optp->name = 0; 19188 /* Include "unknown interface" ip6_mib */ 19189 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 19190 ipst->ips_ip6_mib.ipIfStatsIfIndex = 19191 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 19192 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding, 19193 ipst->ips_ipv6_forward ? 1 : 2); 19194 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit, 19195 ipst->ips_ipv6_def_hops); 19196 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize, 19197 sizeof (mib2_ipIfStatsEntry_t)); 19198 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize, 19199 sizeof (mib2_ipv6AddrEntry_t)); 19200 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize, 19201 sizeof (mib2_ipv6RouteEntry_t)); 19202 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize, 19203 sizeof (mib2_ipv6NetToMediaEntry_t)); 19204 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize, 19205 sizeof (ipv6_member_t)); 19206 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize, 19207 sizeof (ipv6_grpsrc_t)); 19208 19209 /* 19210 * Synchronize 64- and 32-bit counters 19211 */ 19212 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives, 19213 ipIfStatsHCInReceives); 19214 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers, 19215 ipIfStatsHCInDelivers); 19216 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests, 19217 ipIfStatsHCOutRequests); 19218 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams, 19219 ipIfStatsHCOutForwDatagrams); 19220 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts, 19221 ipIfStatsHCOutMcastPkts); 19222 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts, 19223 ipIfStatsHCInMcastPkts); 19224 19225 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19226 (char *)&ipst->ips_ip6_mib, (int)sizeof (ipst->ips_ip6_mib))) { 19227 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 19228 (uint_t)sizeof (ipst->ips_ip6_mib))); 19229 } 19230 19231 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19232 ill = ILL_START_WALK_V6(&ctx, ipst); 19233 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19234 ill->ill_ip_mib->ipIfStatsIfIndex = 19235 ill->ill_phyint->phyint_ifindex; 19236 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 19237 ipst->ips_ipv6_forward ? 1 : 2); 19238 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit, 19239 ill->ill_max_hops); 19240 19241 /* 19242 * Synchronize 64- and 32-bit counters 19243 */ 19244 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives, 19245 ipIfStatsHCInReceives); 19246 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers, 19247 ipIfStatsHCInDelivers); 19248 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests, 19249 ipIfStatsHCOutRequests); 19250 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams, 19251 ipIfStatsHCOutForwDatagrams); 19252 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts, 19253 ipIfStatsHCOutMcastPkts); 19254 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts, 19255 ipIfStatsHCInMcastPkts); 19256 19257 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19258 (char *)ill->ill_ip_mib, 19259 (int)sizeof (*ill->ill_ip_mib))) { 19260 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 19261 "%u bytes\n", 19262 (uint_t)sizeof (*ill->ill_ip_mib))); 19263 } 19264 } 19265 rw_exit(&ipst->ips_ill_g_lock); 19266 19267 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19268 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 19269 (int)optp->level, (int)optp->name, (int)optp->len)); 19270 qreply(q, mpctl); 19271 return (mp2ctl); 19272 } 19273 19274 /* 19275 * ICMPv6 mib: One per ill 19276 */ 19277 static mblk_t * 19278 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19279 { 19280 struct opthdr *optp; 19281 mblk_t *mp2ctl; 19282 ill_t *ill; 19283 ill_walk_context_t ctx; 19284 mblk_t *mp_tail = NULL; 19285 /* 19286 * Make a copy of the original message 19287 */ 19288 mp2ctl = copymsg(mpctl); 19289 19290 /* fixed length ICMPv6 structure ... */ 19291 19292 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19293 optp->level = MIB2_ICMP6; 19294 optp->name = 0; 19295 /* Include "unknown interface" icmp6_mib */ 19296 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex = 19297 MIB2_UNKNOWN_INTERFACE; /* netstat flag */ 19298 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize = 19299 sizeof (mib2_ipv6IfIcmpEntry_t); 19300 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19301 (char *)&ipst->ips_icmp6_mib, 19302 (int)sizeof (ipst->ips_icmp6_mib))) { 19303 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 19304 (uint_t)sizeof (ipst->ips_icmp6_mib))); 19305 } 19306 19307 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19308 ill = ILL_START_WALK_V6(&ctx, ipst); 19309 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19310 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 19311 ill->ill_phyint->phyint_ifindex; 19312 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19313 (char *)ill->ill_icmp6_mib, 19314 (int)sizeof (*ill->ill_icmp6_mib))) { 19315 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 19316 "%u bytes\n", 19317 (uint_t)sizeof (*ill->ill_icmp6_mib))); 19318 } 19319 } 19320 rw_exit(&ipst->ips_ill_g_lock); 19321 19322 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19323 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 19324 (int)optp->level, (int)optp->name, (int)optp->len)); 19325 qreply(q, mpctl); 19326 return (mp2ctl); 19327 } 19328 19329 /* 19330 * ire_walk routine to create both ipRouteEntryTable and 19331 * ipRouteAttributeTable in one IRE walk 19332 */ 19333 static void 19334 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 19335 { 19336 ill_t *ill; 19337 ipif_t *ipif; 19338 mib2_ipRouteEntry_t *re; 19339 mib2_ipAttributeEntry_t *iae, *iaeptr; 19340 ipaddr_t gw_addr; 19341 tsol_ire_gw_secattr_t *attrp; 19342 tsol_gc_t *gc = NULL; 19343 tsol_gcgrp_t *gcgrp = NULL; 19344 uint_t sacnt = 0; 19345 int i; 19346 19347 ASSERT(ire->ire_ipversion == IPV4_VERSION); 19348 19349 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19350 return; 19351 19352 if ((attrp = ire->ire_gw_secattr) != NULL) { 19353 mutex_enter(&attrp->igsa_lock); 19354 if ((gc = attrp->igsa_gc) != NULL) { 19355 gcgrp = gc->gc_grp; 19356 ASSERT(gcgrp != NULL); 19357 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19358 sacnt = 1; 19359 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19360 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19361 gc = gcgrp->gcgrp_head; 19362 sacnt = gcgrp->gcgrp_count; 19363 } 19364 mutex_exit(&attrp->igsa_lock); 19365 19366 /* do nothing if there's no gc to report */ 19367 if (gc == NULL) { 19368 ASSERT(sacnt == 0); 19369 if (gcgrp != NULL) { 19370 /* we might as well drop the lock now */ 19371 rw_exit(&gcgrp->gcgrp_rwlock); 19372 gcgrp = NULL; 19373 } 19374 attrp = NULL; 19375 } 19376 19377 ASSERT(gc == NULL || (gcgrp != NULL && 19378 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19379 } 19380 ASSERT(sacnt == 0 || gc != NULL); 19381 19382 if (sacnt != 0 && 19383 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19384 kmem_free(re, sizeof (*re)); 19385 rw_exit(&gcgrp->gcgrp_rwlock); 19386 return; 19387 } 19388 19389 /* 19390 * Return all IRE types for route table... let caller pick and choose 19391 */ 19392 re->ipRouteDest = ire->ire_addr; 19393 ipif = ire->ire_ipif; 19394 re->ipRouteIfIndex.o_length = 0; 19395 if (ire->ire_type == IRE_CACHE) { 19396 ill = (ill_t *)ire->ire_stq->q_ptr; 19397 re->ipRouteIfIndex.o_length = 19398 ill->ill_name_length == 0 ? 0 : 19399 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19400 bcopy(ill->ill_name, re->ipRouteIfIndex.o_bytes, 19401 re->ipRouteIfIndex.o_length); 19402 } else if (ipif != NULL) { 19403 (void) ipif_get_name(ipif, re->ipRouteIfIndex.o_bytes, 19404 OCTET_LENGTH); 19405 re->ipRouteIfIndex.o_length = 19406 mi_strlen(re->ipRouteIfIndex.o_bytes); 19407 } 19408 re->ipRouteMetric1 = -1; 19409 re->ipRouteMetric2 = -1; 19410 re->ipRouteMetric3 = -1; 19411 re->ipRouteMetric4 = -1; 19412 19413 gw_addr = ire->ire_gateway_addr; 19414 19415 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 19416 re->ipRouteNextHop = ire->ire_src_addr; 19417 else 19418 re->ipRouteNextHop = gw_addr; 19419 /* indirect(4), direct(3), or invalid(2) */ 19420 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19421 re->ipRouteType = 2; 19422 else 19423 re->ipRouteType = (gw_addr != 0) ? 4 : 3; 19424 re->ipRouteProto = -1; 19425 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 19426 re->ipRouteMask = ire->ire_mask; 19427 re->ipRouteMetric5 = -1; 19428 re->ipRouteInfo.re_max_frag = ire->ire_max_frag; 19429 re->ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 19430 re->ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19431 re->ipRouteInfo.re_ref = ire->ire_refcnt; 19432 re->ipRouteInfo.re_src_addr = ire->ire_src_addr; 19433 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19434 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19435 re->ipRouteInfo.re_flags = ire->ire_flags; 19436 re->ipRouteInfo.re_in_ill.o_length = 0; 19437 19438 if (ire->ire_flags & RTF_DYNAMIC) { 19439 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19440 } else { 19441 re->ipRouteInfo.re_ire_type = ire->ire_type; 19442 } 19443 19444 if (ire->ire_in_ill != NULL) { 19445 re->ipRouteInfo.re_in_ill.o_length = 19446 ire->ire_in_ill->ill_name_length == 0 ? 0 : 19447 MIN(OCTET_LENGTH, ire->ire_in_ill->ill_name_length - 1); 19448 bcopy(ire->ire_in_ill->ill_name, 19449 re->ipRouteInfo.re_in_ill.o_bytes, 19450 re->ipRouteInfo.re_in_ill.o_length); 19451 } 19452 re->ipRouteInfo.re_in_src_addr = ire->ire_in_src_addr; 19453 19454 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19455 (char *)re, (int)sizeof (*re))) { 19456 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19457 (uint_t)sizeof (*re))); 19458 } 19459 19460 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19461 iaeptr->iae_routeidx = ird->ird_idx; 19462 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19463 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19464 } 19465 19466 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19467 (char *)iae, sacnt * sizeof (*iae))) { 19468 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19469 (unsigned)(sacnt * sizeof (*iae)))); 19470 } 19471 19472 /* bump route index for next pass */ 19473 ird->ird_idx++; 19474 19475 kmem_free(re, sizeof (*re)); 19476 if (sacnt != 0) 19477 kmem_free(iae, sacnt * sizeof (*iae)); 19478 19479 if (gcgrp != NULL) 19480 rw_exit(&gcgrp->gcgrp_rwlock); 19481 } 19482 19483 /* 19484 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 19485 */ 19486 static void 19487 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 19488 { 19489 ill_t *ill; 19490 ipif_t *ipif; 19491 mib2_ipv6RouteEntry_t *re; 19492 mib2_ipAttributeEntry_t *iae, *iaeptr; 19493 in6_addr_t gw_addr_v6; 19494 tsol_ire_gw_secattr_t *attrp; 19495 tsol_gc_t *gc = NULL; 19496 tsol_gcgrp_t *gcgrp = NULL; 19497 uint_t sacnt = 0; 19498 int i; 19499 19500 ASSERT(ire->ire_ipversion == IPV6_VERSION); 19501 19502 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19503 return; 19504 19505 if ((attrp = ire->ire_gw_secattr) != NULL) { 19506 mutex_enter(&attrp->igsa_lock); 19507 if ((gc = attrp->igsa_gc) != NULL) { 19508 gcgrp = gc->gc_grp; 19509 ASSERT(gcgrp != NULL); 19510 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19511 sacnt = 1; 19512 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19513 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19514 gc = gcgrp->gcgrp_head; 19515 sacnt = gcgrp->gcgrp_count; 19516 } 19517 mutex_exit(&attrp->igsa_lock); 19518 19519 /* do nothing if there's no gc to report */ 19520 if (gc == NULL) { 19521 ASSERT(sacnt == 0); 19522 if (gcgrp != NULL) { 19523 /* we might as well drop the lock now */ 19524 rw_exit(&gcgrp->gcgrp_rwlock); 19525 gcgrp = NULL; 19526 } 19527 attrp = NULL; 19528 } 19529 19530 ASSERT(gc == NULL || (gcgrp != NULL && 19531 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19532 } 19533 ASSERT(sacnt == 0 || gc != NULL); 19534 19535 if (sacnt != 0 && 19536 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19537 kmem_free(re, sizeof (*re)); 19538 rw_exit(&gcgrp->gcgrp_rwlock); 19539 return; 19540 } 19541 19542 /* 19543 * Return all IRE types for route table... let caller pick and choose 19544 */ 19545 re->ipv6RouteDest = ire->ire_addr_v6; 19546 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 19547 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 19548 re->ipv6RouteIfIndex.o_length = 0; 19549 ipif = ire->ire_ipif; 19550 if (ire->ire_type == IRE_CACHE) { 19551 ill = (ill_t *)ire->ire_stq->q_ptr; 19552 re->ipv6RouteIfIndex.o_length = 19553 ill->ill_name_length == 0 ? 0 : 19554 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19555 bcopy(ill->ill_name, re->ipv6RouteIfIndex.o_bytes, 19556 re->ipv6RouteIfIndex.o_length); 19557 } else if (ipif != NULL) { 19558 (void) ipif_get_name(ipif, re->ipv6RouteIfIndex.o_bytes, 19559 OCTET_LENGTH); 19560 re->ipv6RouteIfIndex.o_length = 19561 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 19562 } 19563 19564 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19565 19566 mutex_enter(&ire->ire_lock); 19567 gw_addr_v6 = ire->ire_gateway_addr_v6; 19568 mutex_exit(&ire->ire_lock); 19569 19570 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 19571 re->ipv6RouteNextHop = ire->ire_src_addr_v6; 19572 else 19573 re->ipv6RouteNextHop = gw_addr_v6; 19574 19575 /* remote(4), local(3), or discard(2) */ 19576 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19577 re->ipv6RouteType = 2; 19578 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 19579 re->ipv6RouteType = 3; 19580 else 19581 re->ipv6RouteType = 4; 19582 19583 re->ipv6RouteProtocol = -1; 19584 re->ipv6RoutePolicy = 0; 19585 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 19586 re->ipv6RouteNextHopRDI = 0; 19587 re->ipv6RouteWeight = 0; 19588 re->ipv6RouteMetric = 0; 19589 re->ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 19590 re->ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 19591 re->ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19592 re->ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 19593 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19594 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19595 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 19596 re->ipv6RouteInfo.re_flags = ire->ire_flags; 19597 19598 if (ire->ire_flags & RTF_DYNAMIC) { 19599 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19600 } else { 19601 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 19602 } 19603 19604 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19605 (char *)re, (int)sizeof (*re))) { 19606 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19607 (uint_t)sizeof (*re))); 19608 } 19609 19610 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19611 iaeptr->iae_routeidx = ird->ird_idx; 19612 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19613 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19614 } 19615 19616 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19617 (char *)iae, sacnt * sizeof (*iae))) { 19618 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19619 (unsigned)(sacnt * sizeof (*iae)))); 19620 } 19621 19622 /* bump route index for next pass */ 19623 ird->ird_idx++; 19624 19625 kmem_free(re, sizeof (*re)); 19626 if (sacnt != 0) 19627 kmem_free(iae, sacnt * sizeof (*iae)); 19628 19629 if (gcgrp != NULL) 19630 rw_exit(&gcgrp->gcgrp_rwlock); 19631 } 19632 19633 /* 19634 * ndp_walk routine to create ipv6NetToMediaEntryTable 19635 */ 19636 static int 19637 ip_snmp_get2_v6_media(nce_t *nce, iproutedata_t *ird) 19638 { 19639 ill_t *ill; 19640 mib2_ipv6NetToMediaEntry_t ntme; 19641 dl_unitdata_req_t *dl; 19642 19643 ill = nce->nce_ill; 19644 if (ill->ill_isv6 == B_FALSE) /* skip arpce entry */ 19645 return (0); 19646 19647 /* 19648 * Neighbor cache entry attached to IRE with on-link 19649 * destination. 19650 */ 19651 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 19652 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 19653 if ((ill->ill_flags & ILLF_XRESOLV) && 19654 (nce->nce_res_mp != NULL)) { 19655 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 19656 ntme.ipv6NetToMediaPhysAddress.o_length = 19657 dl->dl_dest_addr_length; 19658 } else { 19659 ntme.ipv6NetToMediaPhysAddress.o_length = 19660 ill->ill_phys_addr_length; 19661 } 19662 if (nce->nce_res_mp != NULL) { 19663 bcopy((char *)nce->nce_res_mp->b_rptr + 19664 NCE_LL_ADDR_OFFSET(ill), 19665 ntme.ipv6NetToMediaPhysAddress.o_bytes, 19666 ntme.ipv6NetToMediaPhysAddress.o_length); 19667 } else { 19668 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 19669 ill->ill_phys_addr_length); 19670 } 19671 /* 19672 * Note: Returns ND_* states. Should be: 19673 * reachable(1), stale(2), delay(3), probe(4), 19674 * invalid(5), unknown(6) 19675 */ 19676 ntme.ipv6NetToMediaState = nce->nce_state; 19677 ntme.ipv6NetToMediaLastUpdated = 0; 19678 19679 /* other(1), dynamic(2), static(3), local(4) */ 19680 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 19681 ntme.ipv6NetToMediaType = 4; 19682 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 19683 ntme.ipv6NetToMediaType = 1; 19684 } else { 19685 ntme.ipv6NetToMediaType = 2; 19686 } 19687 19688 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 19689 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 19690 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 19691 (uint_t)sizeof (ntme))); 19692 } 19693 return (0); 19694 } 19695 19696 /* 19697 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 19698 */ 19699 /* ARGSUSED */ 19700 int 19701 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 19702 { 19703 switch (level) { 19704 case MIB2_IP: 19705 case MIB2_ICMP: 19706 switch (name) { 19707 default: 19708 break; 19709 } 19710 return (1); 19711 default: 19712 return (1); 19713 } 19714 } 19715 19716 /* 19717 * When there exists both a 64- and 32-bit counter of a particular type 19718 * (i.e., InReceives), only the 64-bit counters are added. 19719 */ 19720 void 19721 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2) 19722 { 19723 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors); 19724 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors); 19725 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes); 19726 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors); 19727 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos); 19728 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts); 19729 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards); 19730 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards); 19731 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs); 19732 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails); 19733 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates); 19734 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds); 19735 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs); 19736 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails); 19737 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes); 19738 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates); 19739 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups); 19740 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits); 19741 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs); 19742 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows); 19743 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows); 19744 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion, 19745 o2->ipIfStatsInWrongIPVersion); 19746 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion, 19747 o2->ipIfStatsInWrongIPVersion); 19748 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion, 19749 o2->ipIfStatsOutSwitchIPVersion); 19750 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives); 19751 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets); 19752 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams, 19753 o2->ipIfStatsHCInForwDatagrams); 19754 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers); 19755 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests); 19756 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams, 19757 o2->ipIfStatsHCOutForwDatagrams); 19758 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds); 19759 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits); 19760 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets); 19761 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts); 19762 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets); 19763 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts); 19764 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets, 19765 o2->ipIfStatsHCOutMcastOctets); 19766 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts); 19767 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts); 19768 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded); 19769 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed); 19770 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs); 19771 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs); 19772 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts); 19773 } 19774 19775 void 19776 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2) 19777 { 19778 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs); 19779 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors); 19780 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs); 19781 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs); 19782 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds); 19783 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems); 19784 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs); 19785 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos); 19786 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies); 19787 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits, 19788 o2->ipv6IfIcmpInRouterSolicits); 19789 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements, 19790 o2->ipv6IfIcmpInRouterAdvertisements); 19791 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits, 19792 o2->ipv6IfIcmpInNeighborSolicits); 19793 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements, 19794 o2->ipv6IfIcmpInNeighborAdvertisements); 19795 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects); 19796 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries, 19797 o2->ipv6IfIcmpInGroupMembQueries); 19798 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses, 19799 o2->ipv6IfIcmpInGroupMembResponses); 19800 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions, 19801 o2->ipv6IfIcmpInGroupMembReductions); 19802 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs); 19803 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors); 19804 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs, 19805 o2->ipv6IfIcmpOutDestUnreachs); 19806 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs, 19807 o2->ipv6IfIcmpOutAdminProhibs); 19808 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds); 19809 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems, 19810 o2->ipv6IfIcmpOutParmProblems); 19811 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs); 19812 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos); 19813 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies); 19814 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits, 19815 o2->ipv6IfIcmpOutRouterSolicits); 19816 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements, 19817 o2->ipv6IfIcmpOutRouterAdvertisements); 19818 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits, 19819 o2->ipv6IfIcmpOutNeighborSolicits); 19820 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements, 19821 o2->ipv6IfIcmpOutNeighborAdvertisements); 19822 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects); 19823 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries, 19824 o2->ipv6IfIcmpOutGroupMembQueries); 19825 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses, 19826 o2->ipv6IfIcmpOutGroupMembResponses); 19827 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions, 19828 o2->ipv6IfIcmpOutGroupMembReductions); 19829 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows); 19830 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit); 19831 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements, 19832 o2->ipv6IfIcmpInBadNeighborAdvertisements); 19833 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations, 19834 o2->ipv6IfIcmpInBadNeighborSolicitations); 19835 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects); 19836 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal, 19837 o2->ipv6IfIcmpInGroupMembTotal); 19838 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries, 19839 o2->ipv6IfIcmpInGroupMembBadQueries); 19840 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports, 19841 o2->ipv6IfIcmpInGroupMembBadReports); 19842 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports, 19843 o2->ipv6IfIcmpInGroupMembOurReports); 19844 } 19845 19846 /* 19847 * Called before the options are updated to check if this packet will 19848 * be source routed from here. 19849 * This routine assumes that the options are well formed i.e. that they 19850 * have already been checked. 19851 */ 19852 static boolean_t 19853 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst) 19854 { 19855 ipoptp_t opts; 19856 uchar_t *opt; 19857 uint8_t optval; 19858 uint8_t optlen; 19859 ipaddr_t dst; 19860 ire_t *ire; 19861 19862 if (IS_SIMPLE_IPH(ipha)) { 19863 ip2dbg(("not source routed\n")); 19864 return (B_FALSE); 19865 } 19866 dst = ipha->ipha_dst; 19867 for (optval = ipoptp_first(&opts, ipha); 19868 optval != IPOPT_EOL; 19869 optval = ipoptp_next(&opts)) { 19870 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 19871 opt = opts.ipoptp_cur; 19872 optlen = opts.ipoptp_len; 19873 ip2dbg(("ip_source_routed: opt %d, len %d\n", 19874 optval, optlen)); 19875 switch (optval) { 19876 uint32_t off; 19877 case IPOPT_SSRR: 19878 case IPOPT_LSRR: 19879 /* 19880 * If dst is one of our addresses and there are some 19881 * entries left in the source route return (true). 19882 */ 19883 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 19884 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 19885 if (ire == NULL) { 19886 ip2dbg(("ip_source_routed: not next" 19887 " source route 0x%x\n", 19888 ntohl(dst))); 19889 return (B_FALSE); 19890 } 19891 ire_refrele(ire); 19892 off = opt[IPOPT_OFFSET]; 19893 off--; 19894 if (optlen < IP_ADDR_LEN || 19895 off > optlen - IP_ADDR_LEN) { 19896 /* End of source route */ 19897 ip1dbg(("ip_source_routed: end of SR\n")); 19898 return (B_FALSE); 19899 } 19900 return (B_TRUE); 19901 } 19902 } 19903 ip2dbg(("not source routed\n")); 19904 return (B_FALSE); 19905 } 19906 19907 /* 19908 * Check if the packet contains any source route. 19909 */ 19910 static boolean_t 19911 ip_source_route_included(ipha_t *ipha) 19912 { 19913 ipoptp_t opts; 19914 uint8_t optval; 19915 19916 if (IS_SIMPLE_IPH(ipha)) 19917 return (B_FALSE); 19918 for (optval = ipoptp_first(&opts, ipha); 19919 optval != IPOPT_EOL; 19920 optval = ipoptp_next(&opts)) { 19921 switch (optval) { 19922 case IPOPT_SSRR: 19923 case IPOPT_LSRR: 19924 return (B_TRUE); 19925 } 19926 } 19927 return (B_FALSE); 19928 } 19929 19930 /* 19931 * Called when the IRE expiration timer fires. 19932 */ 19933 void 19934 ip_trash_timer_expire(void *args) 19935 { 19936 int flush_flag = 0; 19937 ire_expire_arg_t iea; 19938 ip_stack_t *ipst = (ip_stack_t *)args; 19939 19940 iea.iea_ipst = ipst; /* No netstack_hold */ 19941 19942 /* 19943 * ip_ire_expire_id is protected by ip_trash_timer_lock. 19944 * This lock makes sure that a new invocation of this function 19945 * that occurs due to an almost immediate timer firing will not 19946 * progress beyond this point until the current invocation is done 19947 */ 19948 mutex_enter(&ipst->ips_ip_trash_timer_lock); 19949 ipst->ips_ip_ire_expire_id = 0; 19950 mutex_exit(&ipst->ips_ip_trash_timer_lock); 19951 19952 /* Periodic timer */ 19953 if (ipst->ips_ip_ire_arp_time_elapsed >= 19954 ipst->ips_ip_ire_arp_interval) { 19955 /* 19956 * Remove all IRE_CACHE entries since they might 19957 * contain arp information. 19958 */ 19959 flush_flag |= FLUSH_ARP_TIME; 19960 ipst->ips_ip_ire_arp_time_elapsed = 0; 19961 IP_STAT(ipst, ip_ire_arp_timer_expired); 19962 } 19963 if (ipst->ips_ip_ire_rd_time_elapsed >= 19964 ipst->ips_ip_ire_redir_interval) { 19965 /* Remove all redirects */ 19966 flush_flag |= FLUSH_REDIRECT_TIME; 19967 ipst->ips_ip_ire_rd_time_elapsed = 0; 19968 IP_STAT(ipst, ip_ire_redirect_timer_expired); 19969 } 19970 if (ipst->ips_ip_ire_pmtu_time_elapsed >= 19971 ipst->ips_ip_ire_pathmtu_interval) { 19972 /* Increase path mtu */ 19973 flush_flag |= FLUSH_MTU_TIME; 19974 ipst->ips_ip_ire_pmtu_time_elapsed = 0; 19975 IP_STAT(ipst, ip_ire_pmtu_timer_expired); 19976 } 19977 19978 /* 19979 * Optimize for the case when there are no redirects in the 19980 * ftable, that is, no need to walk the ftable in that case. 19981 */ 19982 if (flush_flag & (FLUSH_MTU_TIME|FLUSH_ARP_TIME)) { 19983 iea.iea_flush_flag = flush_flag; 19984 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_CACHETABLE, ire_expire, 19985 (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 0, NULL, 19986 ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table, 19987 NULL, ALL_ZONES, ipst); 19988 } 19989 if ((flush_flag & FLUSH_REDIRECT_TIME) && 19990 ipst->ips_ip_redirect_cnt > 0) { 19991 iea.iea_flush_flag = flush_flag; 19992 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_FORWARDTABLE, 19993 ire_expire, (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 19994 0, NULL, 0, NULL, NULL, ALL_ZONES, ipst); 19995 } 19996 if (flush_flag & FLUSH_MTU_TIME) { 19997 /* 19998 * Walk all IPv6 IRE's and update them 19999 * Note that ARP and redirect timers are not 20000 * needed since NUD handles stale entries. 20001 */ 20002 flush_flag = FLUSH_MTU_TIME; 20003 iea.iea_flush_flag = flush_flag; 20004 ire_walk_v6(ire_expire, (char *)(uintptr_t)&iea, 20005 ALL_ZONES, ipst); 20006 } 20007 20008 ipst->ips_ip_ire_arp_time_elapsed += ipst->ips_ip_timer_interval; 20009 ipst->ips_ip_ire_rd_time_elapsed += ipst->ips_ip_timer_interval; 20010 ipst->ips_ip_ire_pmtu_time_elapsed += ipst->ips_ip_timer_interval; 20011 20012 /* 20013 * Hold the lock to serialize timeout calls and prevent 20014 * stale values in ip_ire_expire_id. Otherwise it is possible 20015 * for the timer to fire and a new invocation of this function 20016 * to start before the return value of timeout has been stored 20017 * in ip_ire_expire_id by the current invocation. 20018 */ 20019 mutex_enter(&ipst->ips_ip_trash_timer_lock); 20020 ipst->ips_ip_ire_expire_id = timeout(ip_trash_timer_expire, 20021 (void *)ipst, MSEC_TO_TICK(ipst->ips_ip_timer_interval)); 20022 mutex_exit(&ipst->ips_ip_trash_timer_lock); 20023 } 20024 20025 /* 20026 * Called by the memory allocator subsystem directly, when the system 20027 * is running low on memory. 20028 */ 20029 /* ARGSUSED */ 20030 void 20031 ip_trash_ire_reclaim(void *args) 20032 { 20033 netstack_handle_t nh; 20034 netstack_t *ns; 20035 20036 netstack_next_init(&nh); 20037 while ((ns = netstack_next(&nh)) != NULL) { 20038 ip_trash_ire_reclaim_stack(ns->netstack_ip); 20039 netstack_rele(ns); 20040 } 20041 netstack_next_fini(&nh); 20042 } 20043 20044 static void 20045 ip_trash_ire_reclaim_stack(ip_stack_t *ipst) 20046 { 20047 ire_cache_count_t icc; 20048 ire_cache_reclaim_t icr; 20049 ncc_cache_count_t ncc; 20050 nce_cache_reclaim_t ncr; 20051 uint_t delete_cnt; 20052 /* 20053 * Memory reclaim call back. 20054 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 20055 * Then, with a target of freeing 1/Nth of IRE_CACHE 20056 * entries, determine what fraction to free for 20057 * each category of IRE_CACHE entries giving absolute priority 20058 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 20059 * entry will be freed unless all offlink entries are freed). 20060 */ 20061 icc.icc_total = 0; 20062 icc.icc_unused = 0; 20063 icc.icc_offlink = 0; 20064 icc.icc_pmtu = 0; 20065 icc.icc_onlink = 0; 20066 ire_walk(ire_cache_count, (char *)&icc, ipst); 20067 20068 /* 20069 * Free NCEs for IPv6 like the onlink ires. 20070 */ 20071 ncc.ncc_total = 0; 20072 ncc.ncc_host = 0; 20073 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc, ipst); 20074 20075 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 20076 icc.icc_pmtu + icc.icc_onlink); 20077 delete_cnt = icc.icc_total/ipst->ips_ip_ire_reclaim_fraction; 20078 IP_STAT(ipst, ip_trash_ire_reclaim_calls); 20079 if (delete_cnt == 0) 20080 return; 20081 IP_STAT(ipst, ip_trash_ire_reclaim_success); 20082 /* Always delete all unused offlink entries */ 20083 icr.icr_ipst = ipst; 20084 icr.icr_unused = 1; 20085 if (delete_cnt <= icc.icc_unused) { 20086 /* 20087 * Only need to free unused entries. In other words, 20088 * there are enough unused entries to free to meet our 20089 * target number of freed ire cache entries. 20090 */ 20091 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 20092 ncr.ncr_host = 0; 20093 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 20094 /* 20095 * Only need to free unused entries, plus a fraction of offlink 20096 * entries. It follows from the first if statement that 20097 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 20098 */ 20099 delete_cnt -= icc.icc_unused; 20100 /* Round up # deleted by truncating fraction */ 20101 icr.icr_offlink = icc.icc_offlink / delete_cnt; 20102 icr.icr_pmtu = icr.icr_onlink = 0; 20103 ncr.ncr_host = 0; 20104 } else if (delete_cnt <= 20105 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 20106 /* 20107 * Free all unused and offlink entries, plus a fraction of 20108 * pmtu entries. It follows from the previous if statement 20109 * that icc_pmtu is non-zero, and that 20110 * delete_cnt != icc_unused + icc_offlink. 20111 */ 20112 icr.icr_offlink = 1; 20113 delete_cnt -= icc.icc_unused + icc.icc_offlink; 20114 /* Round up # deleted by truncating fraction */ 20115 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 20116 icr.icr_onlink = 0; 20117 ncr.ncr_host = 0; 20118 } else { 20119 /* 20120 * Free all unused, offlink, and pmtu entries, plus a fraction 20121 * of onlink entries. If we're here, then we know that 20122 * icc_onlink is non-zero, and that 20123 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 20124 */ 20125 icr.icr_offlink = icr.icr_pmtu = 1; 20126 delete_cnt -= icc.icc_unused + icc.icc_offlink + 20127 icc.icc_pmtu; 20128 /* Round up # deleted by truncating fraction */ 20129 icr.icr_onlink = icc.icc_onlink / delete_cnt; 20130 /* Using the same delete fraction as for onlink IREs */ 20131 ncr.ncr_host = ncc.ncc_host / delete_cnt; 20132 } 20133 #ifdef DEBUG 20134 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 20135 "fractions %d/%d/%d/%d\n", 20136 icc.icc_total/ipst->ips_ip_ire_reclaim_fraction, icc.icc_total, 20137 icc.icc_unused, icc.icc_offlink, 20138 icc.icc_pmtu, icc.icc_onlink, 20139 icr.icr_unused, icr.icr_offlink, 20140 icr.icr_pmtu, icr.icr_onlink)); 20141 #endif 20142 ire_walk(ire_cache_reclaim, (char *)&icr, ipst); 20143 if (ncr.ncr_host != 0) 20144 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 20145 (uchar_t *)&ncr, ipst); 20146 #ifdef DEBUG 20147 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 20148 icc.icc_pmtu = 0; icc.icc_onlink = 0; 20149 ire_walk(ire_cache_count, (char *)&icc, ipst); 20150 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 20151 icc.icc_total, icc.icc_unused, icc.icc_offlink, 20152 icc.icc_pmtu, icc.icc_onlink)); 20153 #endif 20154 } 20155 20156 /* 20157 * ip_unbind is called when a copy of an unbind request is received from the 20158 * upper level protocol. We remove this conn from any fanout hash list it is 20159 * on, and zero out the bind information. No reply is expected up above. 20160 */ 20161 mblk_t * 20162 ip_unbind(queue_t *q, mblk_t *mp) 20163 { 20164 conn_t *connp = Q_TO_CONN(q); 20165 20166 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 20167 20168 if (is_system_labeled() && connp->conn_anon_port) { 20169 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 20170 connp->conn_mlp_type, connp->conn_ulp, 20171 ntohs(connp->conn_lport), B_FALSE); 20172 connp->conn_anon_port = 0; 20173 } 20174 connp->conn_mlp_type = mlptSingle; 20175 20176 ipcl_hash_remove(connp); 20177 20178 ASSERT(mp->b_cont == NULL); 20179 /* 20180 * Convert mp into a T_OK_ACK 20181 */ 20182 mp = mi_tpi_ok_ack_alloc(mp); 20183 20184 /* 20185 * should not happen in practice... T_OK_ACK is smaller than the 20186 * original message. 20187 */ 20188 if (mp == NULL) 20189 return (NULL); 20190 20191 /* 20192 * Don't bzero the ports if its TCP since TCP still needs the 20193 * lport to remove it from its own bind hash. TCP will do the 20194 * cleanup. 20195 */ 20196 if (!IPCL_IS_TCP(connp)) 20197 bzero(&connp->u_port, sizeof (connp->u_port)); 20198 20199 return (mp); 20200 } 20201 20202 /* 20203 * Write side put procedure. Outbound data, IOCTLs, responses from 20204 * resolvers, etc, come down through here. 20205 * 20206 * arg2 is always a queue_t *. 20207 * When that queue is an ill_t (i.e. q_next != NULL), then arg must be 20208 * the zoneid. 20209 * When that queue is not an ill_t, then arg must be a conn_t pointer. 20210 */ 20211 void 20212 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 20213 { 20214 ip_output_options(arg, mp, arg2, caller, &zero_info); 20215 } 20216 20217 void 20218 ip_output_options(void *arg, mblk_t *mp, void *arg2, int caller, 20219 ip_opt_info_t *infop) 20220 { 20221 conn_t *connp = NULL; 20222 queue_t *q = (queue_t *)arg2; 20223 ipha_t *ipha; 20224 #define rptr ((uchar_t *)ipha) 20225 ire_t *ire = NULL; 20226 ire_t *sctp_ire = NULL; 20227 uint32_t v_hlen_tos_len; 20228 ipaddr_t dst; 20229 mblk_t *first_mp = NULL; 20230 boolean_t mctl_present; 20231 ipsec_out_t *io; 20232 int match_flags; 20233 ill_t *attach_ill = NULL; 20234 /* Bind to IPIF_NOFAILOVER ill etc. */ 20235 ill_t *xmit_ill = NULL; /* IP_XMIT_IF etc. */ 20236 ipif_t *dst_ipif; 20237 boolean_t multirt_need_resolve = B_FALSE; 20238 mblk_t *copy_mp = NULL; 20239 int err; 20240 zoneid_t zoneid; 20241 int adjust; 20242 uint16_t iplen; 20243 boolean_t need_decref = B_FALSE; 20244 boolean_t ignore_dontroute = B_FALSE; 20245 boolean_t ignore_nexthop = B_FALSE; 20246 boolean_t ip_nexthop = B_FALSE; 20247 ipaddr_t nexthop_addr; 20248 ip_stack_t *ipst; 20249 20250 #ifdef _BIG_ENDIAN 20251 #define V_HLEN (v_hlen_tos_len >> 24) 20252 #else 20253 #define V_HLEN (v_hlen_tos_len & 0xFF) 20254 #endif 20255 20256 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 20257 "ip_wput_start: q %p", q); 20258 20259 /* 20260 * ip_wput fast path 20261 */ 20262 20263 /* is packet from ARP ? */ 20264 if (q->q_next != NULL) { 20265 zoneid = (zoneid_t)(uintptr_t)arg; 20266 goto qnext; 20267 } 20268 20269 connp = (conn_t *)arg; 20270 ASSERT(connp != NULL); 20271 zoneid = connp->conn_zoneid; 20272 ipst = connp->conn_netstack->netstack_ip; 20273 20274 /* is queue flow controlled? */ 20275 if ((q->q_first != NULL || connp->conn_draining) && 20276 (caller == IP_WPUT)) { 20277 ASSERT(!need_decref); 20278 (void) putq(q, mp); 20279 return; 20280 } 20281 20282 /* Multidata transmit? */ 20283 if (DB_TYPE(mp) == M_MULTIDATA) { 20284 /* 20285 * We should never get here, since all Multidata messages 20286 * originating from tcp should have been directed over to 20287 * tcp_multisend() in the first place. 20288 */ 20289 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20290 freemsg(mp); 20291 return; 20292 } else if (DB_TYPE(mp) != M_DATA) 20293 goto notdata; 20294 20295 if (mp->b_flag & MSGHASREF) { 20296 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20297 mp->b_flag &= ~MSGHASREF; 20298 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 20299 need_decref = B_TRUE; 20300 } 20301 ipha = (ipha_t *)mp->b_rptr; 20302 20303 /* is IP header non-aligned or mblk smaller than basic IP header */ 20304 #ifndef SAFETY_BEFORE_SPEED 20305 if (!OK_32PTR(rptr) || 20306 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 20307 goto hdrtoosmall; 20308 #endif 20309 20310 ASSERT(OK_32PTR(ipha)); 20311 20312 /* 20313 * This function assumes that mp points to an IPv4 packet. If it's the 20314 * wrong version, we'll catch it again in ip_output_v6. 20315 * 20316 * Note that this is *only* locally-generated output here, and never 20317 * forwarded data, and that we need to deal only with transports that 20318 * don't know how to label. (TCP, UDP, and ICMP/raw-IP all know how to 20319 * label.) 20320 */ 20321 if (is_system_labeled() && 20322 (ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4) && 20323 !connp->conn_ulp_labeled) { 20324 err = tsol_check_label(BEST_CRED(mp, connp), &mp, &adjust, 20325 connp->conn_mac_exempt, ipst); 20326 ipha = (ipha_t *)mp->b_rptr; 20327 if (err != 0) { 20328 first_mp = mp; 20329 if (err == EINVAL) 20330 goto icmp_parameter_problem; 20331 ip2dbg(("ip_wput: label check failed (%d)\n", err)); 20332 goto discard_pkt; 20333 } 20334 iplen = ntohs(ipha->ipha_length) + adjust; 20335 ipha->ipha_length = htons(iplen); 20336 } 20337 20338 ASSERT(infop != NULL); 20339 20340 if (infop->ip_opt_flags & IP_VERIFY_SRC) { 20341 /* 20342 * IP_PKTINFO ancillary option is present. 20343 * IPCL_ZONEID is used to honor IP_ALLZONES option which 20344 * allows using address of any zone as the source address. 20345 */ 20346 ire = ire_ctable_lookup(ipha->ipha_src, 0, 20347 (IRE_LOCAL|IRE_LOOPBACK), NULL, IPCL_ZONEID(connp), 20348 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 20349 if (ire == NULL) 20350 goto drop_pkt; 20351 ire_refrele(ire); 20352 ire = NULL; 20353 } 20354 20355 /* 20356 * IP_DONTFAILOVER_IF and IP_XMIT_IF have precedence over 20357 * ill index passed in IP_PKTINFO. 20358 */ 20359 if (infop->ip_opt_ill_index != 0 && 20360 connp->conn_xmit_if_ill == NULL && 20361 connp->conn_nofailover_ill == NULL) { 20362 20363 xmit_ill = ill_lookup_on_ifindex( 20364 infop->ip_opt_ill_index, B_FALSE, NULL, NULL, NULL, NULL, 20365 ipst); 20366 20367 if (xmit_ill == NULL || IS_VNI(xmit_ill)) 20368 goto drop_pkt; 20369 /* 20370 * check that there is an ipif belonging 20371 * to our zone. IPCL_ZONEID is not used because 20372 * IP_ALLZONES option is valid only when the ill is 20373 * accessible from all zones i.e has a valid ipif in 20374 * all zones. 20375 */ 20376 if (!ipif_lookup_zoneid_group(xmit_ill, zoneid, 0, NULL)) { 20377 goto drop_pkt; 20378 } 20379 } 20380 20381 /* 20382 * If there is a policy, try to attach an ipsec_out in 20383 * the front. At the end, first_mp either points to a 20384 * M_DATA message or IPSEC_OUT message linked to a 20385 * M_DATA message. We have to do it now as we might 20386 * lose the "conn" if we go through ip_newroute. 20387 */ 20388 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 20389 if (((mp = ipsec_attach_ipsec_out(mp, connp, NULL, 20390 ipha->ipha_protocol, ipst->ips_netstack)) == NULL)) { 20391 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20392 if (need_decref) 20393 CONN_DEC_REF(connp); 20394 return; 20395 } else { 20396 ASSERT(mp->b_datap->db_type == M_CTL); 20397 first_mp = mp; 20398 mp = mp->b_cont; 20399 mctl_present = B_TRUE; 20400 } 20401 } else { 20402 first_mp = mp; 20403 mctl_present = B_FALSE; 20404 } 20405 20406 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20407 20408 /* is wrong version or IP options present */ 20409 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 20410 goto version_hdrlen_check; 20411 dst = ipha->ipha_dst; 20412 20413 if (connp->conn_nofailover_ill != NULL) { 20414 attach_ill = conn_get_held_ill(connp, 20415 &connp->conn_nofailover_ill, &err); 20416 if (err == ILL_LOOKUP_FAILED) { 20417 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20418 if (need_decref) 20419 CONN_DEC_REF(connp); 20420 freemsg(first_mp); 20421 return; 20422 } 20423 } 20424 20425 20426 /* is packet multicast? */ 20427 if (CLASSD(dst)) 20428 goto multicast; 20429 20430 /* 20431 * If xmit_ill is set above due to index passed in ip_pkt_info. It 20432 * takes precedence over conn_dontroute and conn_nexthop_set 20433 */ 20434 if (xmit_ill != NULL) { 20435 goto send_from_ill; 20436 } 20437 20438 if ((connp->conn_dontroute) || (connp->conn_xmit_if_ill != NULL) || 20439 (connp->conn_nexthop_set)) { 20440 /* 20441 * If the destination is a broadcast or a loopback 20442 * address, SO_DONTROUTE, IP_XMIT_IF and IP_NEXTHOP go 20443 * through the standard path. But in the case of local 20444 * destination only SO_DONTROUTE and IP_NEXTHOP go through 20445 * the standard path not IP_XMIT_IF. 20446 */ 20447 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20448 if ((ire == NULL) || ((ire->ire_type != IRE_BROADCAST) && 20449 (ire->ire_type != IRE_LOOPBACK))) { 20450 if ((connp->conn_dontroute || 20451 connp->conn_nexthop_set) && (ire != NULL) && 20452 (ire->ire_type == IRE_LOCAL)) 20453 goto standard_path; 20454 20455 if (ire != NULL) { 20456 ire_refrele(ire); 20457 /* No more access to ire */ 20458 ire = NULL; 20459 } 20460 /* 20461 * bypass routing checks and go directly to 20462 * interface. 20463 */ 20464 if (connp->conn_dontroute) { 20465 goto dontroute; 20466 } else if (connp->conn_nexthop_set) { 20467 ip_nexthop = B_TRUE; 20468 nexthop_addr = connp->conn_nexthop_v4; 20469 goto send_from_ill; 20470 } 20471 20472 /* 20473 * If IP_XMIT_IF socket option is set, 20474 * then we allow unicast and multicast 20475 * packets to go through the ill. It is 20476 * quite possible that the destination 20477 * is not in the ire cache table and we 20478 * do not want to go to ip_newroute() 20479 * instead we call ip_newroute_ipif. 20480 */ 20481 xmit_ill = conn_get_held_ill(connp, 20482 &connp->conn_xmit_if_ill, &err); 20483 if (err == ILL_LOOKUP_FAILED) { 20484 BUMP_MIB(&ipst->ips_ip_mib, 20485 ipIfStatsOutDiscards); 20486 if (attach_ill != NULL) 20487 ill_refrele(attach_ill); 20488 if (need_decref) 20489 CONN_DEC_REF(connp); 20490 freemsg(first_mp); 20491 return; 20492 } 20493 goto send_from_ill; 20494 } 20495 standard_path: 20496 /* Must be a broadcast, a loopback or a local ire */ 20497 if (ire != NULL) { 20498 ire_refrele(ire); 20499 /* No more access to ire */ 20500 ire = NULL; 20501 } 20502 } 20503 20504 if (attach_ill != NULL) 20505 goto send_from_ill; 20506 20507 /* 20508 * We cache IRE_CACHEs to avoid lookups. We don't do 20509 * this for the tcp global queue and listen end point 20510 * as it does not really have a real destination to 20511 * talk to. This is also true for SCTP. 20512 */ 20513 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 20514 !connp->conn_fully_bound) { 20515 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20516 if (ire == NULL) 20517 goto noirefound; 20518 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20519 "ip_wput_end: q %p (%S)", q, "end"); 20520 20521 /* 20522 * Check if the ire has the RTF_MULTIRT flag, inherited 20523 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20524 */ 20525 if (ire->ire_flags & RTF_MULTIRT) { 20526 20527 /* 20528 * Force the TTL of multirouted packets if required. 20529 * The TTL of such packets is bounded by the 20530 * ip_multirt_ttl ndd variable. 20531 */ 20532 if ((ipst->ips_ip_multirt_ttl > 0) && 20533 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20534 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20535 "(was %d), dst 0x%08x\n", 20536 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20537 ntohl(ire->ire_addr))); 20538 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20539 } 20540 /* 20541 * We look at this point if there are pending 20542 * unresolved routes. ire_multirt_resolvable() 20543 * checks in O(n) that all IRE_OFFSUBNET ire 20544 * entries for the packet's destination and 20545 * flagged RTF_MULTIRT are currently resolved. 20546 * If some remain unresolved, we make a copy 20547 * of the current message. It will be used 20548 * to initiate additional route resolutions. 20549 */ 20550 multirt_need_resolve = 20551 ire_multirt_need_resolve(ire->ire_addr, 20552 MBLK_GETLABEL(first_mp), ipst); 20553 ip2dbg(("ip_wput[TCP]: ire %p, " 20554 "multirt_need_resolve %d, first_mp %p\n", 20555 (void *)ire, multirt_need_resolve, 20556 (void *)first_mp)); 20557 if (multirt_need_resolve) { 20558 copy_mp = copymsg(first_mp); 20559 if (copy_mp != NULL) { 20560 MULTIRT_DEBUG_TAG(copy_mp); 20561 } 20562 } 20563 } 20564 20565 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20566 20567 /* 20568 * Try to resolve another multiroute if 20569 * ire_multirt_need_resolve() deemed it necessary. 20570 */ 20571 if (copy_mp != NULL) { 20572 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid, ipst); 20573 } 20574 if (need_decref) 20575 CONN_DEC_REF(connp); 20576 return; 20577 } 20578 20579 /* 20580 * Access to conn_ire_cache. (protected by conn_lock) 20581 * 20582 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 20583 * the ire bucket lock here to check for CONDEMNED as it is okay to 20584 * send a packet or two with the IRE_CACHE that is going away. 20585 * Access to the ire requires an ire refhold on the ire prior to 20586 * its use since an interface unplumb thread may delete the cached 20587 * ire and release the refhold at any time. 20588 * 20589 * Caching an ire in the conn_ire_cache 20590 * 20591 * o Caching an ire pointer in the conn requires a strict check for 20592 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 20593 * ires before cleaning up the conns. So the caching of an ire pointer 20594 * in the conn is done after making sure under the bucket lock that the 20595 * ire has not yet been marked CONDEMNED. Otherwise we will end up 20596 * caching an ire after the unplumb thread has cleaned up the conn. 20597 * If the conn does not send a packet subsequently the unplumb thread 20598 * will be hanging waiting for the ire count to drop to zero. 20599 * 20600 * o We also need to atomically test for a null conn_ire_cache and 20601 * set the conn_ire_cache under the the protection of the conn_lock 20602 * to avoid races among concurrent threads trying to simultaneously 20603 * cache an ire in the conn_ire_cache. 20604 */ 20605 mutex_enter(&connp->conn_lock); 20606 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 20607 20608 if (ire != NULL && ire->ire_addr == dst && 20609 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20610 20611 IRE_REFHOLD(ire); 20612 mutex_exit(&connp->conn_lock); 20613 20614 } else { 20615 boolean_t cached = B_FALSE; 20616 connp->conn_ire_cache = NULL; 20617 mutex_exit(&connp->conn_lock); 20618 /* Release the old ire */ 20619 if (ire != NULL && sctp_ire == NULL) 20620 IRE_REFRELE_NOTR(ire); 20621 20622 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20623 if (ire == NULL) 20624 goto noirefound; 20625 IRE_REFHOLD_NOTR(ire); 20626 20627 mutex_enter(&connp->conn_lock); 20628 if (CONN_CACHE_IRE(connp) && connp->conn_ire_cache == NULL) { 20629 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 20630 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20631 connp->conn_ire_cache = ire; 20632 cached = B_TRUE; 20633 } 20634 rw_exit(&ire->ire_bucket->irb_lock); 20635 } 20636 mutex_exit(&connp->conn_lock); 20637 20638 /* 20639 * We can continue to use the ire but since it was 20640 * not cached, we should drop the extra reference. 20641 */ 20642 if (!cached) 20643 IRE_REFRELE_NOTR(ire); 20644 } 20645 20646 20647 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20648 "ip_wput_end: q %p (%S)", q, "end"); 20649 20650 /* 20651 * Check if the ire has the RTF_MULTIRT flag, inherited 20652 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20653 */ 20654 if (ire->ire_flags & RTF_MULTIRT) { 20655 20656 /* 20657 * Force the TTL of multirouted packets if required. 20658 * The TTL of such packets is bounded by the 20659 * ip_multirt_ttl ndd variable. 20660 */ 20661 if ((ipst->ips_ip_multirt_ttl > 0) && 20662 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20663 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20664 "(was %d), dst 0x%08x\n", 20665 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20666 ntohl(ire->ire_addr))); 20667 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20668 } 20669 20670 /* 20671 * At this point, we check to see if there are any pending 20672 * unresolved routes. ire_multirt_resolvable() 20673 * checks in O(n) that all IRE_OFFSUBNET ire 20674 * entries for the packet's destination and 20675 * flagged RTF_MULTIRT are currently resolved. 20676 * If some remain unresolved, we make a copy 20677 * of the current message. It will be used 20678 * to initiate additional route resolutions. 20679 */ 20680 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 20681 MBLK_GETLABEL(first_mp), ipst); 20682 ip2dbg(("ip_wput[not TCP]: ire %p, " 20683 "multirt_need_resolve %d, first_mp %p\n", 20684 (void *)ire, multirt_need_resolve, (void *)first_mp)); 20685 if (multirt_need_resolve) { 20686 copy_mp = copymsg(first_mp); 20687 if (copy_mp != NULL) { 20688 MULTIRT_DEBUG_TAG(copy_mp); 20689 } 20690 } 20691 } 20692 20693 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20694 20695 /* 20696 * Try to resolve another multiroute if 20697 * ire_multirt_resolvable() deemed it necessary 20698 */ 20699 if (copy_mp != NULL) { 20700 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid, ipst); 20701 } 20702 if (need_decref) 20703 CONN_DEC_REF(connp); 20704 return; 20705 20706 qnext: 20707 /* 20708 * Upper Level Protocols pass down complete IP datagrams 20709 * as M_DATA messages. Everything else is a sideshow. 20710 * 20711 * 1) We could be re-entering ip_wput because of ip_neworute 20712 * in which case we could have a IPSEC_OUT message. We 20713 * need to pass through ip_wput like other datagrams and 20714 * hence cannot branch to ip_wput_nondata. 20715 * 20716 * 2) ARP, AH, ESP, and other clients who are on the module 20717 * instance of IP stream, give us something to deal with. 20718 * We will handle AH and ESP here and rest in ip_wput_nondata. 20719 * 20720 * 3) ICMP replies also could come here. 20721 */ 20722 ipst = ILLQ_TO_IPST(q); 20723 20724 if (DB_TYPE(mp) != M_DATA) { 20725 notdata: 20726 if (DB_TYPE(mp) == M_CTL) { 20727 /* 20728 * M_CTL messages are used by ARP, AH and ESP to 20729 * communicate with IP. We deal with IPSEC_IN and 20730 * IPSEC_OUT here. ip_wput_nondata handles other 20731 * cases. 20732 */ 20733 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 20734 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 20735 first_mp = mp->b_cont; 20736 first_mp->b_flag &= ~MSGHASREF; 20737 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20738 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 20739 CONN_DEC_REF(connp); 20740 connp = NULL; 20741 } 20742 if (ii->ipsec_info_type == IPSEC_IN) { 20743 /* 20744 * Either this message goes back to 20745 * IPSEC for further processing or to 20746 * ULP after policy checks. 20747 */ 20748 ip_fanout_proto_again(mp, NULL, NULL, NULL); 20749 return; 20750 } else if (ii->ipsec_info_type == IPSEC_OUT) { 20751 io = (ipsec_out_t *)ii; 20752 if (io->ipsec_out_proc_begin) { 20753 /* 20754 * IPSEC processing has already started. 20755 * Complete it. 20756 * IPQoS notes: We don't care what is 20757 * in ipsec_out_ill_index since this 20758 * won't be processed for IPQoS policies 20759 * in ipsec_out_process. 20760 */ 20761 ipsec_out_process(q, mp, NULL, 20762 io->ipsec_out_ill_index); 20763 return; 20764 } else { 20765 connp = (q->q_next != NULL) ? 20766 NULL : Q_TO_CONN(q); 20767 first_mp = mp; 20768 mp = mp->b_cont; 20769 mctl_present = B_TRUE; 20770 } 20771 zoneid = io->ipsec_out_zoneid; 20772 ASSERT(zoneid != ALL_ZONES); 20773 } else if (ii->ipsec_info_type == IPSEC_CTL) { 20774 /* 20775 * It's an IPsec control message requesting 20776 * an SADB update to be sent to the IPsec 20777 * hardware acceleration capable ills. 20778 */ 20779 ipsec_ctl_t *ipsec_ctl = 20780 (ipsec_ctl_t *)mp->b_rptr; 20781 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 20782 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 20783 mblk_t *cmp = mp->b_cont; 20784 20785 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 20786 ASSERT(cmp != NULL); 20787 20788 freeb(mp); 20789 ill_ipsec_capab_send_all(satype, cmp, sa, 20790 ipst->ips_netstack); 20791 return; 20792 } else { 20793 /* 20794 * This must be ARP or special TSOL signaling. 20795 */ 20796 ip_wput_nondata(NULL, q, mp, NULL); 20797 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20798 "ip_wput_end: q %p (%S)", q, "nondata"); 20799 return; 20800 } 20801 } else { 20802 /* 20803 * This must be non-(ARP/AH/ESP) messages. 20804 */ 20805 ASSERT(!need_decref); 20806 ip_wput_nondata(NULL, q, mp, NULL); 20807 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20808 "ip_wput_end: q %p (%S)", q, "nondata"); 20809 return; 20810 } 20811 } else { 20812 first_mp = mp; 20813 mctl_present = B_FALSE; 20814 } 20815 20816 ASSERT(first_mp != NULL); 20817 /* 20818 * ICMP echo replies attach an ipsec_out and set ipsec_out_attach_if 20819 * to make sure that this packet goes out on the same interface it 20820 * came in. We handle that here. 20821 */ 20822 if (mctl_present) { 20823 uint_t ifindex; 20824 20825 io = (ipsec_out_t *)first_mp->b_rptr; 20826 if (io->ipsec_out_attach_if || 20827 io->ipsec_out_xmit_if || 20828 io->ipsec_out_ip_nexthop) { 20829 ill_t *ill; 20830 20831 /* 20832 * We may have lost the conn context if we are 20833 * coming here from ip_newroute(). Copy the 20834 * nexthop information. 20835 */ 20836 if (io->ipsec_out_ip_nexthop) { 20837 ip_nexthop = B_TRUE; 20838 nexthop_addr = io->ipsec_out_nexthop_addr; 20839 20840 ipha = (ipha_t *)mp->b_rptr; 20841 dst = ipha->ipha_dst; 20842 goto send_from_ill; 20843 } else { 20844 ASSERT(io->ipsec_out_ill_index != 0); 20845 ifindex = io->ipsec_out_ill_index; 20846 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 20847 NULL, NULL, NULL, NULL, ipst); 20848 /* 20849 * ipsec_out_xmit_if bit is used to tell 20850 * ip_wput to use the ill to send outgoing data 20851 * as we have no conn when data comes from ICMP 20852 * error msg routines. Currently this feature is 20853 * only used by ip_mrtun_forward routine. 20854 */ 20855 if (io->ipsec_out_xmit_if) { 20856 xmit_ill = ill; 20857 if (xmit_ill == NULL) { 20858 ip1dbg(("ip_output:bad ifindex " 20859 "for xmit_ill %d\n", 20860 ifindex)); 20861 freemsg(first_mp); 20862 BUMP_MIB(&ipst->ips_ip_mib, 20863 ipIfStatsOutDiscards); 20864 ASSERT(!need_decref); 20865 return; 20866 } 20867 /* Free up the ipsec_out_t mblk */ 20868 ASSERT(first_mp->b_cont == mp); 20869 first_mp->b_cont = NULL; 20870 freeb(first_mp); 20871 /* Just send the IP header+ICMP+data */ 20872 first_mp = mp; 20873 ipha = (ipha_t *)mp->b_rptr; 20874 dst = ipha->ipha_dst; 20875 goto send_from_ill; 20876 } else { 20877 attach_ill = ill; 20878 } 20879 20880 if (attach_ill == NULL) { 20881 ASSERT(xmit_ill == NULL); 20882 ip1dbg(("ip_output: bad ifindex for " 20883 "(BIND TO IPIF_NOFAILOVER) %d\n", 20884 ifindex)); 20885 freemsg(first_mp); 20886 BUMP_MIB(&ipst->ips_ip_mib, 20887 ipIfStatsOutDiscards); 20888 ASSERT(!need_decref); 20889 return; 20890 } 20891 } 20892 } 20893 } 20894 20895 ASSERT(xmit_ill == NULL); 20896 20897 /* We have a complete IP datagram heading outbound. */ 20898 ipha = (ipha_t *)mp->b_rptr; 20899 20900 #ifndef SPEED_BEFORE_SAFETY 20901 /* 20902 * Make sure we have a full-word aligned message and that at least 20903 * a simple IP header is accessible in the first message. If not, 20904 * try a pullup. 20905 */ 20906 if (!OK_32PTR(rptr) || 20907 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) { 20908 hdrtoosmall: 20909 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 20910 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20911 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 20912 if (first_mp == NULL) 20913 first_mp = mp; 20914 goto discard_pkt; 20915 } 20916 20917 /* This function assumes that mp points to an IPv4 packet. */ 20918 if (is_system_labeled() && q->q_next == NULL && 20919 (*mp->b_rptr & 0xf0) == (IPV4_VERSION << 4) && 20920 !connp->conn_ulp_labeled) { 20921 err = tsol_check_label(BEST_CRED(mp, connp), &mp, 20922 &adjust, connp->conn_mac_exempt, ipst); 20923 ipha = (ipha_t *)mp->b_rptr; 20924 if (first_mp != NULL) 20925 first_mp->b_cont = mp; 20926 if (err != 0) { 20927 if (first_mp == NULL) 20928 first_mp = mp; 20929 if (err == EINVAL) 20930 goto icmp_parameter_problem; 20931 ip2dbg(("ip_wput: label check failed (%d)\n", 20932 err)); 20933 goto discard_pkt; 20934 } 20935 iplen = ntohs(ipha->ipha_length) + adjust; 20936 ipha->ipha_length = htons(iplen); 20937 } 20938 20939 ipha = (ipha_t *)mp->b_rptr; 20940 if (first_mp == NULL) { 20941 ASSERT(attach_ill == NULL && xmit_ill == NULL); 20942 /* 20943 * If we got here because of "goto hdrtoosmall" 20944 * We need to attach a IPSEC_OUT. 20945 */ 20946 if (connp->conn_out_enforce_policy) { 20947 if (((mp = ipsec_attach_ipsec_out(mp, connp, 20948 NULL, ipha->ipha_protocol, 20949 ipst->ips_netstack)) == NULL)) { 20950 BUMP_MIB(&ipst->ips_ip_mib, 20951 ipIfStatsOutDiscards); 20952 if (need_decref) 20953 CONN_DEC_REF(connp); 20954 return; 20955 } else { 20956 ASSERT(mp->b_datap->db_type == M_CTL); 20957 first_mp = mp; 20958 mp = mp->b_cont; 20959 mctl_present = B_TRUE; 20960 } 20961 } else { 20962 first_mp = mp; 20963 mctl_present = B_FALSE; 20964 } 20965 } 20966 } 20967 #endif 20968 20969 /* Most of the code below is written for speed, not readability */ 20970 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20971 20972 /* 20973 * If ip_newroute() fails, we're going to need a full 20974 * header for the icmp wraparound. 20975 */ 20976 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 20977 uint_t v_hlen; 20978 version_hdrlen_check: 20979 ASSERT(first_mp != NULL); 20980 v_hlen = V_HLEN; 20981 /* 20982 * siphon off IPv6 packets coming down from transport 20983 * layer modules here. 20984 * Note: high-order bit carries NUD reachability confirmation 20985 */ 20986 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 20987 /* 20988 * XXX implement a IPv4 and IPv6 packet counter per 20989 * conn and switch when ratio exceeds e.g. 10:1 20990 */ 20991 #ifdef notyet 20992 if (q->q_next == NULL) /* Avoid ill queue */ 20993 ip_setqinfo(RD(q), B_TRUE, B_TRUE, ipst); 20994 #endif 20995 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutWrongIPVersion); 20996 ASSERT(xmit_ill == NULL); 20997 if (attach_ill != NULL) 20998 ill_refrele(attach_ill); 20999 if (need_decref) 21000 mp->b_flag |= MSGHASREF; 21001 (void) ip_output_v6(arg, first_mp, arg2, caller); 21002 return; 21003 } 21004 21005 if ((v_hlen >> 4) != IP_VERSION) { 21006 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21007 "ip_wput_end: q %p (%S)", q, "badvers"); 21008 goto discard_pkt; 21009 } 21010 /* 21011 * Is the header length at least 20 bytes? 21012 * 21013 * Are there enough bytes accessible in the header? If 21014 * not, try a pullup. 21015 */ 21016 v_hlen &= 0xF; 21017 v_hlen <<= 2; 21018 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 21019 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21020 "ip_wput_end: q %p (%S)", q, "badlen"); 21021 goto discard_pkt; 21022 } 21023 if (v_hlen > (mp->b_wptr - rptr)) { 21024 if (!pullupmsg(mp, v_hlen)) { 21025 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21026 "ip_wput_end: q %p (%S)", q, "badpullup2"); 21027 goto discard_pkt; 21028 } 21029 ipha = (ipha_t *)mp->b_rptr; 21030 } 21031 /* 21032 * Move first entry from any source route into ipha_dst and 21033 * verify the options 21034 */ 21035 if (ip_wput_options(q, first_mp, ipha, mctl_present, 21036 zoneid, ipst)) { 21037 ASSERT(xmit_ill == NULL); 21038 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21039 if (attach_ill != NULL) 21040 ill_refrele(attach_ill); 21041 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21042 "ip_wput_end: q %p (%S)", q, "badopts"); 21043 if (need_decref) 21044 CONN_DEC_REF(connp); 21045 return; 21046 } 21047 } 21048 dst = ipha->ipha_dst; 21049 21050 /* 21051 * Try to get an IRE_CACHE for the destination address. If we can't, 21052 * we have to run the packet through ip_newroute which will take 21053 * the appropriate action to arrange for an IRE_CACHE, such as querying 21054 * a resolver, or assigning a default gateway, etc. 21055 */ 21056 if (CLASSD(dst)) { 21057 ipif_t *ipif; 21058 uint32_t setsrc = 0; 21059 21060 multicast: 21061 ASSERT(first_mp != NULL); 21062 ip2dbg(("ip_wput: CLASSD\n")); 21063 if (connp == NULL) { 21064 /* 21065 * Use the first good ipif on the ill. 21066 * XXX Should this ever happen? (Appears 21067 * to show up with just ppp and no ethernet due 21068 * to in.rdisc.) 21069 * However, ire_send should be able to 21070 * call ip_wput_ire directly. 21071 * 21072 * XXX Also, this can happen for ICMP and other packets 21073 * with multicast source addresses. Perhaps we should 21074 * fix things so that we drop the packet in question, 21075 * but for now, just run with it. 21076 */ 21077 ill_t *ill = (ill_t *)q->q_ptr; 21078 21079 /* 21080 * Don't honor attach_if for this case. If ill 21081 * is part of the group, ipif could belong to 21082 * any ill and we cannot maintain attach_ill 21083 * and ipif_ill same anymore and the assert 21084 * below would fail. 21085 */ 21086 if (mctl_present && io->ipsec_out_attach_if) { 21087 io->ipsec_out_ill_index = 0; 21088 io->ipsec_out_attach_if = B_FALSE; 21089 ASSERT(attach_ill != NULL); 21090 ill_refrele(attach_ill); 21091 attach_ill = NULL; 21092 } 21093 21094 ASSERT(attach_ill == NULL); 21095 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 21096 if (ipif == NULL) { 21097 if (need_decref) 21098 CONN_DEC_REF(connp); 21099 freemsg(first_mp); 21100 return; 21101 } 21102 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 21103 ntohl(dst), ill->ill_name)); 21104 } else { 21105 /* 21106 * The order of precedence is IP_XMIT_IF, IP_PKTINFO 21107 * and IP_MULTICAST_IF. 21108 * Block comment above this function explains the 21109 * locking mechanism used here 21110 */ 21111 if (xmit_ill == NULL) { 21112 xmit_ill = conn_get_held_ill(connp, 21113 &connp->conn_xmit_if_ill, &err); 21114 if (err == ILL_LOOKUP_FAILED) { 21115 ip1dbg(("ip_wput: No ill for " 21116 "IP_XMIT_IF\n")); 21117 BUMP_MIB(&ipst->ips_ip_mib, 21118 ipIfStatsOutNoRoutes); 21119 goto drop_pkt; 21120 } 21121 } 21122 21123 if (xmit_ill == NULL) { 21124 ipif = conn_get_held_ipif(connp, 21125 &connp->conn_multicast_ipif, &err); 21126 if (err == IPIF_LOOKUP_FAILED) { 21127 ip1dbg(("ip_wput: No ipif for " 21128 "multicast\n")); 21129 BUMP_MIB(&ipst->ips_ip_mib, 21130 ipIfStatsOutNoRoutes); 21131 goto drop_pkt; 21132 } 21133 } 21134 if (xmit_ill != NULL) { 21135 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21136 if (ipif == NULL) { 21137 ip1dbg(("ip_wput: No ipif for " 21138 "IP_XMIT_IF\n")); 21139 BUMP_MIB(&ipst->ips_ip_mib, 21140 ipIfStatsOutNoRoutes); 21141 goto drop_pkt; 21142 } 21143 } else if (ipif == NULL || ipif->ipif_isv6) { 21144 /* 21145 * We must do this ipif determination here 21146 * else we could pass through ip_newroute 21147 * and come back here without the conn context. 21148 * 21149 * Note: we do late binding i.e. we bind to 21150 * the interface when the first packet is sent. 21151 * For performance reasons we do not rebind on 21152 * each packet but keep the binding until the 21153 * next IP_MULTICAST_IF option. 21154 * 21155 * conn_multicast_{ipif,ill} are shared between 21156 * IPv4 and IPv6 and AF_INET6 sockets can 21157 * send both IPv4 and IPv6 packets. Hence 21158 * we have to check that "isv6" matches above. 21159 */ 21160 if (ipif != NULL) 21161 ipif_refrele(ipif); 21162 ipif = ipif_lookup_group(dst, zoneid, ipst); 21163 if (ipif == NULL) { 21164 ip1dbg(("ip_wput: No ipif for " 21165 "multicast\n")); 21166 BUMP_MIB(&ipst->ips_ip_mib, 21167 ipIfStatsOutNoRoutes); 21168 goto drop_pkt; 21169 } 21170 err = conn_set_held_ipif(connp, 21171 &connp->conn_multicast_ipif, ipif); 21172 if (err == IPIF_LOOKUP_FAILED) { 21173 ipif_refrele(ipif); 21174 ip1dbg(("ip_wput: No ipif for " 21175 "multicast\n")); 21176 BUMP_MIB(&ipst->ips_ip_mib, 21177 ipIfStatsOutNoRoutes); 21178 goto drop_pkt; 21179 } 21180 } 21181 } 21182 ASSERT(!ipif->ipif_isv6); 21183 /* 21184 * As we may lose the conn by the time we reach ip_wput_ire, 21185 * we copy conn_multicast_loop and conn_dontroute on to an 21186 * ipsec_out. In case if this datagram goes out secure, 21187 * we need the ill_index also. Copy that also into the 21188 * ipsec_out. 21189 */ 21190 if (mctl_present) { 21191 io = (ipsec_out_t *)first_mp->b_rptr; 21192 ASSERT(first_mp->b_datap->db_type == M_CTL); 21193 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21194 } else { 21195 ASSERT(mp == first_mp); 21196 if ((first_mp = allocb(sizeof (ipsec_info_t), 21197 BPRI_HI)) == NULL) { 21198 ipif_refrele(ipif); 21199 first_mp = mp; 21200 goto discard_pkt; 21201 } 21202 first_mp->b_datap->db_type = M_CTL; 21203 first_mp->b_wptr += sizeof (ipsec_info_t); 21204 /* ipsec_out_secure is B_FALSE now */ 21205 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 21206 io = (ipsec_out_t *)first_mp->b_rptr; 21207 io->ipsec_out_type = IPSEC_OUT; 21208 io->ipsec_out_len = sizeof (ipsec_out_t); 21209 io->ipsec_out_use_global_policy = B_TRUE; 21210 io->ipsec_out_ns = ipst->ips_netstack; 21211 first_mp->b_cont = mp; 21212 mctl_present = B_TRUE; 21213 } 21214 if (attach_ill != NULL) { 21215 ASSERT(attach_ill == ipif->ipif_ill); 21216 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21217 21218 /* 21219 * Check if we need an ire that will not be 21220 * looked up by anybody else i.e. HIDDEN. 21221 */ 21222 if (ill_is_probeonly(attach_ill)) { 21223 match_flags |= MATCH_IRE_MARK_HIDDEN; 21224 } 21225 io->ipsec_out_ill_index = 21226 attach_ill->ill_phyint->phyint_ifindex; 21227 io->ipsec_out_attach_if = B_TRUE; 21228 } else { 21229 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 21230 io->ipsec_out_ill_index = 21231 ipif->ipif_ill->ill_phyint->phyint_ifindex; 21232 } 21233 if (connp != NULL) { 21234 io->ipsec_out_multicast_loop = 21235 connp->conn_multicast_loop; 21236 io->ipsec_out_dontroute = connp->conn_dontroute; 21237 io->ipsec_out_zoneid = connp->conn_zoneid; 21238 } 21239 /* 21240 * If the application uses IP_MULTICAST_IF with 21241 * different logical addresses of the same ILL, we 21242 * need to make sure that the soruce address of 21243 * the packet matches the logical IP address used 21244 * in the option. We do it by initializing ipha_src 21245 * here. This should keep IPSEC also happy as 21246 * when we return from IPSEC processing, we don't 21247 * have to worry about getting the right address on 21248 * the packet. Thus it is sufficient to look for 21249 * IRE_CACHE using MATCH_IRE_ILL rathen than 21250 * MATCH_IRE_IPIF. 21251 * 21252 * NOTE : We need to do it for non-secure case also as 21253 * this might go out secure if there is a global policy 21254 * match in ip_wput_ire. For bind to IPIF_NOFAILOVER 21255 * address, the source should be initialized already and 21256 * hence we won't be initializing here. 21257 * 21258 * As we do not have the ire yet, it is possible that 21259 * we set the source address here and then later discover 21260 * that the ire implies the source address to be assigned 21261 * through the RTF_SETSRC flag. 21262 * In that case, the setsrc variable will remind us 21263 * that overwritting the source address by the one 21264 * of the RTF_SETSRC-flagged ire is allowed. 21265 */ 21266 if (ipha->ipha_src == INADDR_ANY && 21267 (connp == NULL || !connp->conn_unspec_src)) { 21268 ipha->ipha_src = ipif->ipif_src_addr; 21269 setsrc = RTF_SETSRC; 21270 } 21271 /* 21272 * Find an IRE which matches the destination and the outgoing 21273 * queue (i.e. the outgoing interface.) 21274 * For loopback use a unicast IP address for 21275 * the ire lookup. 21276 */ 21277 if (ipif->ipif_ill->ill_phyint->phyint_flags & 21278 PHYI_LOOPBACK) { 21279 dst = ipif->ipif_lcl_addr; 21280 } 21281 /* 21282 * If IP_XMIT_IF is set, we branch out to ip_newroute_ipif. 21283 * We don't need to lookup ire in ctable as the packet 21284 * needs to be sent to the destination through the specified 21285 * ill irrespective of ires in the cache table. 21286 */ 21287 ire = NULL; 21288 if (xmit_ill == NULL) { 21289 ire = ire_ctable_lookup(dst, 0, 0, ipif, 21290 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21291 } 21292 21293 /* 21294 * refrele attach_ill as its not needed anymore. 21295 */ 21296 if (attach_ill != NULL) { 21297 ill_refrele(attach_ill); 21298 attach_ill = NULL; 21299 } 21300 21301 if (ire == NULL) { 21302 /* 21303 * Multicast loopback and multicast forwarding is 21304 * done in ip_wput_ire. 21305 * 21306 * Mark this packet to make it be delivered to 21307 * ip_wput_ire after the new ire has been 21308 * created. 21309 * 21310 * The call to ip_newroute_ipif takes into account 21311 * the setsrc reminder. In any case, we take care 21312 * of the RTF_MULTIRT flag. 21313 */ 21314 mp->b_prev = mp->b_next = NULL; 21315 if (xmit_ill == NULL || 21316 xmit_ill->ill_ipif_up_count > 0) { 21317 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 21318 setsrc | RTF_MULTIRT, zoneid, infop); 21319 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21320 "ip_wput_end: q %p (%S)", q, "noire"); 21321 } else { 21322 freemsg(first_mp); 21323 } 21324 ipif_refrele(ipif); 21325 if (xmit_ill != NULL) 21326 ill_refrele(xmit_ill); 21327 if (need_decref) 21328 CONN_DEC_REF(connp); 21329 return; 21330 } 21331 21332 ipif_refrele(ipif); 21333 ipif = NULL; 21334 ASSERT(xmit_ill == NULL); 21335 21336 /* 21337 * Honor the RTF_SETSRC flag for multicast packets, 21338 * if allowed by the setsrc reminder. 21339 */ 21340 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 21341 ipha->ipha_src = ire->ire_src_addr; 21342 } 21343 21344 /* 21345 * Unconditionally force the TTL to 1 for 21346 * multirouted multicast packets: 21347 * multirouted multicast should not cross 21348 * multicast routers. 21349 */ 21350 if (ire->ire_flags & RTF_MULTIRT) { 21351 if (ipha->ipha_ttl > 1) { 21352 ip2dbg(("ip_wput: forcing multicast " 21353 "multirt TTL to 1 (was %d), dst 0x%08x\n", 21354 ipha->ipha_ttl, ntohl(ire->ire_addr))); 21355 ipha->ipha_ttl = 1; 21356 } 21357 } 21358 } else { 21359 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 21360 if ((ire != NULL) && (ire->ire_type & 21361 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 21362 ignore_dontroute = B_TRUE; 21363 ignore_nexthop = B_TRUE; 21364 } 21365 if (ire != NULL) { 21366 ire_refrele(ire); 21367 ire = NULL; 21368 } 21369 /* 21370 * Guard against coming in from arp in which case conn is NULL. 21371 * Also guard against non M_DATA with dontroute set but 21372 * destined to local, loopback or broadcast addresses. 21373 */ 21374 if (connp != NULL && connp->conn_dontroute && 21375 !ignore_dontroute) { 21376 dontroute: 21377 /* 21378 * Set TTL to 1 if SO_DONTROUTE is set to prevent 21379 * routing protocols from seeing false direct 21380 * connectivity. 21381 */ 21382 ipha->ipha_ttl = 1; 21383 /* 21384 * If IP_XMIT_IF is also set (conn_xmit_if_ill != NULL) 21385 * along with SO_DONTROUTE, higher precedence is 21386 * given to IP_XMIT_IF and the IP_XMIT_IF ipif is used. 21387 */ 21388 if (connp->conn_xmit_if_ill == NULL) { 21389 /* If suitable ipif not found, drop packet */ 21390 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid, 21391 ipst); 21392 if (dst_ipif == NULL) { 21393 ip1dbg(("ip_wput: no route for " 21394 "dst using SO_DONTROUTE\n")); 21395 BUMP_MIB(&ipst->ips_ip_mib, 21396 ipIfStatsOutNoRoutes); 21397 mp->b_prev = mp->b_next = NULL; 21398 if (first_mp == NULL) 21399 first_mp = mp; 21400 goto drop_pkt; 21401 } else { 21402 /* 21403 * If suitable ipif has been found, set 21404 * xmit_ill to the corresponding 21405 * ipif_ill because we'll be following 21406 * the IP_XMIT_IF logic. 21407 */ 21408 ASSERT(xmit_ill == NULL); 21409 xmit_ill = dst_ipif->ipif_ill; 21410 mutex_enter(&xmit_ill->ill_lock); 21411 if (!ILL_CAN_LOOKUP(xmit_ill)) { 21412 mutex_exit(&xmit_ill->ill_lock); 21413 xmit_ill = NULL; 21414 ipif_refrele(dst_ipif); 21415 ip1dbg(("ip_wput: no route for" 21416 " dst using" 21417 " SO_DONTROUTE\n")); 21418 BUMP_MIB(&ipst->ips_ip_mib, 21419 ipIfStatsOutNoRoutes); 21420 mp->b_prev = mp->b_next = NULL; 21421 if (first_mp == NULL) 21422 first_mp = mp; 21423 goto drop_pkt; 21424 } 21425 ill_refhold_locked(xmit_ill); 21426 mutex_exit(&xmit_ill->ill_lock); 21427 ipif_refrele(dst_ipif); 21428 } 21429 } 21430 21431 } 21432 /* 21433 * If we are bound to IPIF_NOFAILOVER address, look for 21434 * an IRE_CACHE matching the ill. 21435 */ 21436 send_from_ill: 21437 if (attach_ill != NULL) { 21438 ipif_t *attach_ipif; 21439 21440 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21441 21442 /* 21443 * Check if we need an ire that will not be 21444 * looked up by anybody else i.e. HIDDEN. 21445 */ 21446 if (ill_is_probeonly(attach_ill)) { 21447 match_flags |= MATCH_IRE_MARK_HIDDEN; 21448 } 21449 21450 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 21451 if (attach_ipif == NULL) { 21452 ip1dbg(("ip_wput: No ipif for attach_ill\n")); 21453 goto discard_pkt; 21454 } 21455 ire = ire_ctable_lookup(dst, 0, 0, attach_ipif, 21456 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21457 ipif_refrele(attach_ipif); 21458 } else if (xmit_ill != NULL || (connp != NULL && 21459 connp->conn_xmit_if_ill != NULL)) { 21460 /* 21461 * Mark this packet as originated locally 21462 */ 21463 mp->b_prev = mp->b_next = NULL; 21464 /* 21465 * xmit_ill could be NULL if SO_DONTROUTE 21466 * is also set. 21467 */ 21468 if (xmit_ill == NULL) { 21469 xmit_ill = conn_get_held_ill(connp, 21470 &connp->conn_xmit_if_ill, &err); 21471 if (err == ILL_LOOKUP_FAILED) { 21472 BUMP_MIB(&ipst->ips_ip_mib, 21473 ipIfStatsOutDiscards); 21474 if (need_decref) 21475 CONN_DEC_REF(connp); 21476 freemsg(first_mp); 21477 return; 21478 } 21479 if (xmit_ill == NULL) { 21480 if (connp->conn_dontroute) 21481 goto dontroute; 21482 goto send_from_ill; 21483 } 21484 } 21485 /* 21486 * Could be SO_DONTROUTE case also. 21487 * check at least one interface is UP as 21488 * specified by this ILL 21489 */ 21490 if (xmit_ill->ill_ipif_up_count > 0) { 21491 ipif_t *ipif; 21492 21493 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21494 if (ipif == NULL) { 21495 ip1dbg(("ip_output: " 21496 "xmit_ill NULL ipif\n")); 21497 goto drop_pkt; 21498 } 21499 /* 21500 * Look for a ire that is part of the group, 21501 * if found use it else call ip_newroute_ipif. 21502 * IPCL_ZONEID is not used for matching because 21503 * IP_ALLZONES option is valid only when the 21504 * ill is accessible from all zones i.e has a 21505 * valid ipif in all zones. 21506 */ 21507 match_flags = MATCH_IRE_ILL_GROUP | 21508 MATCH_IRE_SECATTR; 21509 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 21510 MBLK_GETLABEL(mp), match_flags, ipst); 21511 /* 21512 * If an ire exists use it or else create 21513 * an ire but don't add it to the cache. 21514 * Adding an ire may cause issues with 21515 * asymmetric routing. 21516 * In case of multiroute always act as if 21517 * ire does not exist. 21518 */ 21519 if (ire == NULL || 21520 ire->ire_flags & RTF_MULTIRT) { 21521 if (ire != NULL) 21522 ire_refrele(ire); 21523 ip_newroute_ipif(q, first_mp, ipif, 21524 dst, connp, 0, zoneid, infop); 21525 ipif_refrele(ipif); 21526 ip1dbg(("ip_wput: ip_unicast_if\n")); 21527 ill_refrele(xmit_ill); 21528 if (need_decref) 21529 CONN_DEC_REF(connp); 21530 return; 21531 } 21532 ipif_refrele(ipif); 21533 } else { 21534 goto drop_pkt; 21535 } 21536 } else if (ip_nexthop || (connp != NULL && 21537 (connp->conn_nexthop_set)) && !ignore_nexthop) { 21538 if (!ip_nexthop) { 21539 ip_nexthop = B_TRUE; 21540 nexthop_addr = connp->conn_nexthop_v4; 21541 } 21542 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 21543 MATCH_IRE_GW; 21544 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 21545 NULL, zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21546 } else { 21547 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), 21548 ipst); 21549 } 21550 if (!ire) { 21551 /* 21552 * Make sure we don't load spread if this 21553 * is IPIF_NOFAILOVER case. 21554 */ 21555 if ((attach_ill != NULL) || 21556 (ip_nexthop && !ignore_nexthop)) { 21557 if (mctl_present) { 21558 io = (ipsec_out_t *)first_mp->b_rptr; 21559 ASSERT(first_mp->b_datap->db_type == 21560 M_CTL); 21561 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21562 } else { 21563 ASSERT(mp == first_mp); 21564 first_mp = allocb( 21565 sizeof (ipsec_info_t), BPRI_HI); 21566 if (first_mp == NULL) { 21567 first_mp = mp; 21568 goto discard_pkt; 21569 } 21570 first_mp->b_datap->db_type = M_CTL; 21571 first_mp->b_wptr += 21572 sizeof (ipsec_info_t); 21573 /* ipsec_out_secure is B_FALSE now */ 21574 bzero(first_mp->b_rptr, 21575 sizeof (ipsec_info_t)); 21576 io = (ipsec_out_t *)first_mp->b_rptr; 21577 io->ipsec_out_type = IPSEC_OUT; 21578 io->ipsec_out_len = 21579 sizeof (ipsec_out_t); 21580 io->ipsec_out_use_global_policy = 21581 B_TRUE; 21582 io->ipsec_out_ns = ipst->ips_netstack; 21583 first_mp->b_cont = mp; 21584 mctl_present = B_TRUE; 21585 } 21586 if (attach_ill != NULL) { 21587 io->ipsec_out_ill_index = attach_ill-> 21588 ill_phyint->phyint_ifindex; 21589 io->ipsec_out_attach_if = B_TRUE; 21590 } else { 21591 io->ipsec_out_ip_nexthop = ip_nexthop; 21592 io->ipsec_out_nexthop_addr = 21593 nexthop_addr; 21594 } 21595 } 21596 noirefound: 21597 /* 21598 * Mark this packet as having originated on 21599 * this machine. This will be noted in 21600 * ire_add_then_send, which needs to know 21601 * whether to run it back through ip_wput or 21602 * ip_rput following successful resolution. 21603 */ 21604 mp->b_prev = NULL; 21605 mp->b_next = NULL; 21606 ip_newroute(q, first_mp, dst, NULL, connp, zoneid, 21607 ipst); 21608 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21609 "ip_wput_end: q %p (%S)", q, "newroute"); 21610 if (attach_ill != NULL) 21611 ill_refrele(attach_ill); 21612 if (xmit_ill != NULL) 21613 ill_refrele(xmit_ill); 21614 if (need_decref) 21615 CONN_DEC_REF(connp); 21616 return; 21617 } 21618 } 21619 21620 /* We now know where we are going with it. */ 21621 21622 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21623 "ip_wput_end: q %p (%S)", q, "end"); 21624 21625 /* 21626 * Check if the ire has the RTF_MULTIRT flag, inherited 21627 * from an IRE_OFFSUBNET ire entry in ip_newroute. 21628 */ 21629 if (ire->ire_flags & RTF_MULTIRT) { 21630 /* 21631 * Force the TTL of multirouted packets if required. 21632 * The TTL of such packets is bounded by the 21633 * ip_multirt_ttl ndd variable. 21634 */ 21635 if ((ipst->ips_ip_multirt_ttl > 0) && 21636 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 21637 ip2dbg(("ip_wput: forcing multirt TTL to %d " 21638 "(was %d), dst 0x%08x\n", 21639 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 21640 ntohl(ire->ire_addr))); 21641 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 21642 } 21643 /* 21644 * At this point, we check to see if there are any pending 21645 * unresolved routes. ire_multirt_resolvable() 21646 * checks in O(n) that all IRE_OFFSUBNET ire 21647 * entries for the packet's destination and 21648 * flagged RTF_MULTIRT are currently resolved. 21649 * If some remain unresolved, we make a copy 21650 * of the current message. It will be used 21651 * to initiate additional route resolutions. 21652 */ 21653 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 21654 MBLK_GETLABEL(first_mp), ipst); 21655 ip2dbg(("ip_wput[noirefound]: ire %p, " 21656 "multirt_need_resolve %d, first_mp %p\n", 21657 (void *)ire, multirt_need_resolve, (void *)first_mp)); 21658 if (multirt_need_resolve) { 21659 copy_mp = copymsg(first_mp); 21660 if (copy_mp != NULL) { 21661 MULTIRT_DEBUG_TAG(copy_mp); 21662 } 21663 } 21664 } 21665 21666 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 21667 /* 21668 * Try to resolve another multiroute if 21669 * ire_multirt_resolvable() deemed it necessary. 21670 * At this point, we need to distinguish 21671 * multicasts from other packets. For multicasts, 21672 * we call ip_newroute_ipif() and request that both 21673 * multirouting and setsrc flags are checked. 21674 */ 21675 if (copy_mp != NULL) { 21676 if (CLASSD(dst)) { 21677 ipif_t *ipif = ipif_lookup_group(dst, zoneid, ipst); 21678 if (ipif) { 21679 ASSERT(infop->ip_opt_ill_index == 0); 21680 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 21681 RTF_SETSRC | RTF_MULTIRT, zoneid, infop); 21682 ipif_refrele(ipif); 21683 } else { 21684 MULTIRT_DEBUG_UNTAG(copy_mp); 21685 freemsg(copy_mp); 21686 copy_mp = NULL; 21687 } 21688 } else { 21689 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid, ipst); 21690 } 21691 } 21692 if (attach_ill != NULL) 21693 ill_refrele(attach_ill); 21694 if (xmit_ill != NULL) 21695 ill_refrele(xmit_ill); 21696 if (need_decref) 21697 CONN_DEC_REF(connp); 21698 return; 21699 21700 icmp_parameter_problem: 21701 /* could not have originated externally */ 21702 ASSERT(mp->b_prev == NULL); 21703 if (ip_hdr_complete(ipha, zoneid, ipst) == 0) { 21704 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 21705 /* it's the IP header length that's in trouble */ 21706 icmp_param_problem(q, first_mp, 0, zoneid, ipst); 21707 first_mp = NULL; 21708 } 21709 21710 discard_pkt: 21711 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21712 drop_pkt: 21713 ip1dbg(("ip_wput: dropped packet\n")); 21714 if (ire != NULL) 21715 ire_refrele(ire); 21716 if (need_decref) 21717 CONN_DEC_REF(connp); 21718 freemsg(first_mp); 21719 if (attach_ill != NULL) 21720 ill_refrele(attach_ill); 21721 if (xmit_ill != NULL) 21722 ill_refrele(xmit_ill); 21723 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21724 "ip_wput_end: q %p (%S)", q, "droppkt"); 21725 } 21726 21727 /* 21728 * If this is a conn_t queue, then we pass in the conn. This includes the 21729 * zoneid. 21730 * Otherwise, this is a message coming back from ARP or for an ill_t queue, 21731 * in which case we use the global zoneid since those are all part of 21732 * the global zone. 21733 */ 21734 void 21735 ip_wput(queue_t *q, mblk_t *mp) 21736 { 21737 if (CONN_Q(q)) 21738 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 21739 else 21740 ip_output(GLOBAL_ZONEID, mp, q, IP_WPUT); 21741 } 21742 21743 /* 21744 * 21745 * The following rules must be observed when accessing any ipif or ill 21746 * that has been cached in the conn. Typically conn_nofailover_ill, 21747 * conn_xmit_if_ill, conn_multicast_ipif and conn_multicast_ill. 21748 * 21749 * Access: The ipif or ill pointed to from the conn can be accessed under 21750 * the protection of the conn_lock or after it has been refheld under the 21751 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 21752 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 21753 * The reason for this is that a concurrent unplumb could actually be 21754 * cleaning up these cached pointers by walking the conns and might have 21755 * finished cleaning up the conn in question. The macros check that an 21756 * unplumb has not yet started on the ipif or ill. 21757 * 21758 * Caching: An ipif or ill pointer may be cached in the conn only after 21759 * making sure that an unplumb has not started. So the caching is done 21760 * while holding both the conn_lock and the ill_lock and after using the 21761 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 21762 * flag before starting the cleanup of conns. 21763 * 21764 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 21765 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 21766 * or a reference to the ipif or a reference to an ire that references the 21767 * ipif. An ipif does not change its ill except for failover/failback. Since 21768 * failover/failback happens only after bringing down the ipif and making sure 21769 * the ipif refcnt has gone to zero and holding the ill_g_lock and ill_lock 21770 * the above holds. 21771 */ 21772 ipif_t * 21773 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 21774 { 21775 ipif_t *ipif; 21776 ill_t *ill; 21777 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 21778 21779 *err = 0; 21780 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 21781 mutex_enter(&connp->conn_lock); 21782 ipif = *ipifp; 21783 if (ipif != NULL) { 21784 ill = ipif->ipif_ill; 21785 mutex_enter(&ill->ill_lock); 21786 if (IPIF_CAN_LOOKUP(ipif)) { 21787 ipif_refhold_locked(ipif); 21788 mutex_exit(&ill->ill_lock); 21789 mutex_exit(&connp->conn_lock); 21790 rw_exit(&ipst->ips_ill_g_lock); 21791 return (ipif); 21792 } else { 21793 *err = IPIF_LOOKUP_FAILED; 21794 } 21795 mutex_exit(&ill->ill_lock); 21796 } 21797 mutex_exit(&connp->conn_lock); 21798 rw_exit(&ipst->ips_ill_g_lock); 21799 return (NULL); 21800 } 21801 21802 ill_t * 21803 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 21804 { 21805 ill_t *ill; 21806 21807 *err = 0; 21808 mutex_enter(&connp->conn_lock); 21809 ill = *illp; 21810 if (ill != NULL) { 21811 mutex_enter(&ill->ill_lock); 21812 if (ILL_CAN_LOOKUP(ill)) { 21813 ill_refhold_locked(ill); 21814 mutex_exit(&ill->ill_lock); 21815 mutex_exit(&connp->conn_lock); 21816 return (ill); 21817 } else { 21818 *err = ILL_LOOKUP_FAILED; 21819 } 21820 mutex_exit(&ill->ill_lock); 21821 } 21822 mutex_exit(&connp->conn_lock); 21823 return (NULL); 21824 } 21825 21826 static int 21827 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 21828 { 21829 ill_t *ill; 21830 21831 ill = ipif->ipif_ill; 21832 mutex_enter(&connp->conn_lock); 21833 mutex_enter(&ill->ill_lock); 21834 if (IPIF_CAN_LOOKUP(ipif)) { 21835 *ipifp = ipif; 21836 mutex_exit(&ill->ill_lock); 21837 mutex_exit(&connp->conn_lock); 21838 return (0); 21839 } 21840 mutex_exit(&ill->ill_lock); 21841 mutex_exit(&connp->conn_lock); 21842 return (IPIF_LOOKUP_FAILED); 21843 } 21844 21845 /* 21846 * This is called if the outbound datagram needs fragmentation. 21847 * 21848 * NOTE : This function does not ire_refrele the ire argument passed in. 21849 */ 21850 static void 21851 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire, zoneid_t zoneid, 21852 ip_stack_t *ipst) 21853 { 21854 ipha_t *ipha; 21855 mblk_t *mp; 21856 uint32_t v_hlen_tos_len; 21857 uint32_t max_frag; 21858 uint32_t frag_flag; 21859 boolean_t dont_use; 21860 21861 if (ipsec_mp->b_datap->db_type == M_CTL) { 21862 mp = ipsec_mp->b_cont; 21863 } else { 21864 mp = ipsec_mp; 21865 } 21866 21867 ipha = (ipha_t *)mp->b_rptr; 21868 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21869 21870 #ifdef _BIG_ENDIAN 21871 #define V_HLEN (v_hlen_tos_len >> 24) 21872 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21873 #else 21874 #define V_HLEN (v_hlen_tos_len & 0xFF) 21875 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21876 #endif 21877 21878 #ifndef SPEED_BEFORE_SAFETY 21879 /* 21880 * Check that ipha_length is consistent with 21881 * the mblk length 21882 */ 21883 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 21884 ip0dbg(("Packet length mismatch: %d, %ld\n", 21885 LENGTH, msgdsize(mp))); 21886 freemsg(ipsec_mp); 21887 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21888 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 21889 "packet length mismatch"); 21890 return; 21891 } 21892 #endif 21893 /* 21894 * Don't use frag_flag if pre-built packet or source 21895 * routed or if multicast (since multicast packets do not solicit 21896 * ICMP "packet too big" messages). Get the values of 21897 * max_frag and frag_flag atomically by acquiring the 21898 * ire_lock. 21899 */ 21900 mutex_enter(&ire->ire_lock); 21901 max_frag = ire->ire_max_frag; 21902 frag_flag = ire->ire_frag_flag; 21903 mutex_exit(&ire->ire_lock); 21904 21905 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 21906 (V_HLEN != IP_SIMPLE_HDR_VERSION && 21907 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 21908 21909 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 21910 (dont_use ? 0 : frag_flag), zoneid, ipst); 21911 } 21912 21913 /* 21914 * Used for deciding the MSS size for the upper layer. Thus 21915 * we need to check the outbound policy values in the conn. 21916 */ 21917 int 21918 conn_ipsec_length(conn_t *connp) 21919 { 21920 ipsec_latch_t *ipl; 21921 21922 ipl = connp->conn_latch; 21923 if (ipl == NULL) 21924 return (0); 21925 21926 if (ipl->ipl_out_policy == NULL) 21927 return (0); 21928 21929 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 21930 } 21931 21932 /* 21933 * Returns an estimate of the IPSEC headers size. This is used if 21934 * we don't want to call into IPSEC to get the exact size. 21935 */ 21936 int 21937 ipsec_out_extra_length(mblk_t *ipsec_mp) 21938 { 21939 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21940 ipsec_action_t *a; 21941 21942 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21943 if (!io->ipsec_out_secure) 21944 return (0); 21945 21946 a = io->ipsec_out_act; 21947 21948 if (a == NULL) { 21949 ASSERT(io->ipsec_out_policy != NULL); 21950 a = io->ipsec_out_policy->ipsp_act; 21951 } 21952 ASSERT(a != NULL); 21953 21954 return (a->ipa_ovhd); 21955 } 21956 21957 /* 21958 * Returns an estimate of the IPSEC headers size. This is used if 21959 * we don't want to call into IPSEC to get the exact size. 21960 */ 21961 int 21962 ipsec_in_extra_length(mblk_t *ipsec_mp) 21963 { 21964 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21965 ipsec_action_t *a; 21966 21967 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21968 21969 a = ii->ipsec_in_action; 21970 return (a == NULL ? 0 : a->ipa_ovhd); 21971 } 21972 21973 /* 21974 * If there are any source route options, return the true final 21975 * destination. Otherwise, return the destination. 21976 */ 21977 ipaddr_t 21978 ip_get_dst(ipha_t *ipha) 21979 { 21980 ipoptp_t opts; 21981 uchar_t *opt; 21982 uint8_t optval; 21983 uint8_t optlen; 21984 ipaddr_t dst; 21985 uint32_t off; 21986 21987 dst = ipha->ipha_dst; 21988 21989 if (IS_SIMPLE_IPH(ipha)) 21990 return (dst); 21991 21992 for (optval = ipoptp_first(&opts, ipha); 21993 optval != IPOPT_EOL; 21994 optval = ipoptp_next(&opts)) { 21995 opt = opts.ipoptp_cur; 21996 optlen = opts.ipoptp_len; 21997 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 21998 switch (optval) { 21999 case IPOPT_SSRR: 22000 case IPOPT_LSRR: 22001 off = opt[IPOPT_OFFSET]; 22002 /* 22003 * If one of the conditions is true, it means 22004 * end of options and dst already has the right 22005 * value. 22006 */ 22007 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 22008 off = optlen - IP_ADDR_LEN; 22009 bcopy(&opt[off], &dst, IP_ADDR_LEN); 22010 } 22011 return (dst); 22012 default: 22013 break; 22014 } 22015 } 22016 22017 return (dst); 22018 } 22019 22020 mblk_t * 22021 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 22022 conn_t *connp, boolean_t unspec_src, zoneid_t zoneid) 22023 { 22024 ipsec_out_t *io; 22025 mblk_t *first_mp; 22026 boolean_t policy_present; 22027 ip_stack_t *ipst; 22028 ipsec_stack_t *ipss; 22029 22030 ASSERT(ire != NULL); 22031 ipst = ire->ire_ipst; 22032 ipss = ipst->ips_netstack->netstack_ipsec; 22033 22034 first_mp = mp; 22035 if (mp->b_datap->db_type == M_CTL) { 22036 io = (ipsec_out_t *)first_mp->b_rptr; 22037 /* 22038 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 22039 * 22040 * 1) There is per-socket policy (including cached global 22041 * policy) or a policy on the IP-in-IP tunnel. 22042 * 2) There is no per-socket policy, but it is 22043 * a multicast packet that needs to go out 22044 * on a specific interface. This is the case 22045 * where (ip_wput and ip_wput_multicast) attaches 22046 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 22047 * 22048 * In case (2) we check with global policy to 22049 * see if there is a match and set the ill_index 22050 * appropriately so that we can lookup the ire 22051 * properly in ip_wput_ipsec_out. 22052 */ 22053 22054 /* 22055 * ipsec_out_use_global_policy is set to B_FALSE 22056 * in ipsec_in_to_out(). Refer to that function for 22057 * details. 22058 */ 22059 if ((io->ipsec_out_latch == NULL) && 22060 (io->ipsec_out_use_global_policy)) { 22061 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 22062 ire, connp, unspec_src, zoneid)); 22063 } 22064 if (!io->ipsec_out_secure) { 22065 /* 22066 * If this is not a secure packet, drop 22067 * the IPSEC_OUT mp and treat it as a clear 22068 * packet. This happens when we are sending 22069 * a ICMP reply back to a clear packet. See 22070 * ipsec_in_to_out() for details. 22071 */ 22072 mp = first_mp->b_cont; 22073 freeb(first_mp); 22074 } 22075 return (mp); 22076 } 22077 /* 22078 * See whether we need to attach a global policy here. We 22079 * don't depend on the conn (as it could be null) for deciding 22080 * what policy this datagram should go through because it 22081 * should have happened in ip_wput if there was some 22082 * policy. This normally happens for connections which are not 22083 * fully bound preventing us from caching policies in 22084 * ip_bind. Packets coming from the TCP listener/global queue 22085 * - which are non-hard_bound - could also be affected by 22086 * applying policy here. 22087 * 22088 * If this packet is coming from tcp global queue or listener, 22089 * we will be applying policy here. This may not be *right* 22090 * if these packets are coming from the detached connection as 22091 * it could have gone in clear before. This happens only if a 22092 * TCP connection started when there is no policy and somebody 22093 * added policy before it became detached. Thus packets of the 22094 * detached connection could go out secure and the other end 22095 * would drop it because it will be expecting in clear. The 22096 * converse is not true i.e if somebody starts a TCP 22097 * connection and deletes the policy, all the packets will 22098 * still go out with the policy that existed before deleting 22099 * because ip_unbind sends up policy information which is used 22100 * by TCP on subsequent ip_wputs. The right solution is to fix 22101 * TCP to attach a dummy IPSEC_OUT and set 22102 * ipsec_out_use_global_policy to B_FALSE. As this might 22103 * affect performance for normal cases, we are not doing it. 22104 * Thus, set policy before starting any TCP connections. 22105 * 22106 * NOTE - We might apply policy even for a hard bound connection 22107 * - for which we cached policy in ip_bind - if somebody added 22108 * global policy after we inherited the policy in ip_bind. 22109 * This means that the packets that were going out in clear 22110 * previously would start going secure and hence get dropped 22111 * on the other side. To fix this, TCP attaches a dummy 22112 * ipsec_out and make sure that we don't apply global policy. 22113 */ 22114 if (ipha != NULL) 22115 policy_present = ipss->ipsec_outbound_v4_policy_present; 22116 else 22117 policy_present = ipss->ipsec_outbound_v6_policy_present; 22118 if (!policy_present) 22119 return (mp); 22120 22121 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src, 22122 zoneid)); 22123 } 22124 22125 ire_t * 22126 conn_set_outgoing_ill(conn_t *connp, ire_t *ire, ill_t **conn_outgoing_ill) 22127 { 22128 ipaddr_t addr; 22129 ire_t *save_ire; 22130 irb_t *irb; 22131 ill_group_t *illgrp; 22132 int err; 22133 22134 save_ire = ire; 22135 addr = ire->ire_addr; 22136 22137 ASSERT(ire->ire_type == IRE_BROADCAST); 22138 22139 illgrp = connp->conn_outgoing_ill->ill_group; 22140 if (illgrp == NULL) { 22141 *conn_outgoing_ill = conn_get_held_ill(connp, 22142 &connp->conn_outgoing_ill, &err); 22143 if (err == ILL_LOOKUP_FAILED) { 22144 ire_refrele(save_ire); 22145 return (NULL); 22146 } 22147 return (save_ire); 22148 } 22149 /* 22150 * If IP_BOUND_IF has been done, conn_outgoing_ill will be set. 22151 * If it is part of the group, we need to send on the ire 22152 * that has been cleared of IRE_MARK_NORECV and that belongs 22153 * to this group. This is okay as IP_BOUND_IF really means 22154 * any ill in the group. We depend on the fact that the 22155 * first ire in the group is always cleared of IRE_MARK_NORECV 22156 * if such an ire exists. This is possible only if you have 22157 * at least one ill in the group that has not failed. 22158 * 22159 * First get to the ire that matches the address and group. 22160 * 22161 * We don't look for an ire with a matching zoneid because a given zone 22162 * won't always have broadcast ires on all ills in the group. 22163 */ 22164 irb = ire->ire_bucket; 22165 rw_enter(&irb->irb_lock, RW_READER); 22166 if (ire->ire_marks & IRE_MARK_NORECV) { 22167 /* 22168 * If the current zone only has an ire broadcast for this 22169 * address marked NORECV, the ire we want is ahead in the 22170 * bucket, so we look it up deliberately ignoring the zoneid. 22171 */ 22172 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 22173 if (ire->ire_addr != addr) 22174 continue; 22175 /* skip over deleted ires */ 22176 if (ire->ire_marks & IRE_MARK_CONDEMNED) 22177 continue; 22178 } 22179 } 22180 while (ire != NULL) { 22181 /* 22182 * If a new interface is coming up, we could end up 22183 * seeing the loopback ire and the non-loopback ire 22184 * may not have been added yet. So check for ire_stq 22185 */ 22186 if (ire->ire_stq != NULL && (ire->ire_addr != addr || 22187 ire->ire_ipif->ipif_ill->ill_group == illgrp)) { 22188 break; 22189 } 22190 ire = ire->ire_next; 22191 } 22192 if (ire != NULL && ire->ire_addr == addr && 22193 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 22194 IRE_REFHOLD(ire); 22195 rw_exit(&irb->irb_lock); 22196 ire_refrele(save_ire); 22197 *conn_outgoing_ill = ire_to_ill(ire); 22198 /* 22199 * Refhold the ill to make the conn_outgoing_ill 22200 * independent of the ire. ip_wput_ire goes in a loop 22201 * and may refrele the ire. Since we have an ire at this 22202 * point we don't need to use ILL_CAN_LOOKUP on the ill. 22203 */ 22204 ill_refhold(*conn_outgoing_ill); 22205 return (ire); 22206 } 22207 rw_exit(&irb->irb_lock); 22208 ip1dbg(("conn_set_outgoing_ill: No matching ire\n")); 22209 /* 22210 * If we can't find a suitable ire, return the original ire. 22211 */ 22212 return (save_ire); 22213 } 22214 22215 /* 22216 * This function does the ire_refrele of the ire passed in as the 22217 * argument. As this function looks up more ires i.e broadcast ires, 22218 * it needs to REFRELE them. Currently, for simplicity we don't 22219 * differentiate the one passed in and looked up here. We always 22220 * REFRELE. 22221 * IPQoS Notes: 22222 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 22223 * IPSec packets are done in ipsec_out_process. 22224 * 22225 */ 22226 void 22227 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller, 22228 zoneid_t zoneid) 22229 { 22230 ipha_t *ipha; 22231 #define rptr ((uchar_t *)ipha) 22232 queue_t *stq; 22233 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 22234 uint32_t v_hlen_tos_len; 22235 uint32_t ttl_protocol; 22236 ipaddr_t src; 22237 ipaddr_t dst; 22238 uint32_t cksum; 22239 ipaddr_t orig_src; 22240 ire_t *ire1; 22241 mblk_t *next_mp; 22242 uint_t hlen; 22243 uint16_t *up; 22244 uint32_t max_frag = ire->ire_max_frag; 22245 ill_t *ill = ire_to_ill(ire); 22246 int clusterwide; 22247 uint16_t ip_hdr_included; /* IP header included by ULP? */ 22248 int ipsec_len; 22249 mblk_t *first_mp; 22250 ipsec_out_t *io; 22251 boolean_t conn_dontroute; /* conn value for multicast */ 22252 boolean_t conn_multicast_loop; /* conn value for multicast */ 22253 boolean_t multicast_forward; /* Should we forward ? */ 22254 boolean_t unspec_src; 22255 ill_t *conn_outgoing_ill = NULL; 22256 ill_t *ire_ill; 22257 ill_t *ire1_ill; 22258 ill_t *out_ill; 22259 uint32_t ill_index = 0; 22260 boolean_t multirt_send = B_FALSE; 22261 int err; 22262 ipxmit_state_t pktxmit_state; 22263 ip_stack_t *ipst = ire->ire_ipst; 22264 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 22265 22266 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 22267 "ip_wput_ire_start: q %p", q); 22268 22269 multicast_forward = B_FALSE; 22270 unspec_src = (connp != NULL && connp->conn_unspec_src); 22271 22272 if (ire->ire_flags & RTF_MULTIRT) { 22273 /* 22274 * Multirouting case. The bucket where ire is stored 22275 * probably holds other RTF_MULTIRT flagged ire 22276 * to the destination. In this call to ip_wput_ire, 22277 * we attempt to send the packet through all 22278 * those ires. Thus, we first ensure that ire is the 22279 * first RTF_MULTIRT ire in the bucket, 22280 * before walking the ire list. 22281 */ 22282 ire_t *first_ire; 22283 irb_t *irb = ire->ire_bucket; 22284 ASSERT(irb != NULL); 22285 22286 /* Make sure we do not omit any multiroute ire. */ 22287 IRB_REFHOLD(irb); 22288 for (first_ire = irb->irb_ire; 22289 first_ire != NULL; 22290 first_ire = first_ire->ire_next) { 22291 if ((first_ire->ire_flags & RTF_MULTIRT) && 22292 (first_ire->ire_addr == ire->ire_addr) && 22293 !(first_ire->ire_marks & 22294 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 22295 break; 22296 } 22297 22298 if ((first_ire != NULL) && (first_ire != ire)) { 22299 IRE_REFHOLD(first_ire); 22300 ire_refrele(ire); 22301 ire = first_ire; 22302 ill = ire_to_ill(ire); 22303 } 22304 IRB_REFRELE(irb); 22305 } 22306 22307 /* 22308 * conn_outgoing_ill is used only in the broadcast loop. 22309 * for performance we don't grab the mutexs in the fastpath 22310 */ 22311 if ((connp != NULL) && 22312 (connp->conn_xmit_if_ill == NULL) && 22313 (ire->ire_type == IRE_BROADCAST) && 22314 ((connp->conn_nofailover_ill != NULL) || 22315 (connp->conn_outgoing_ill != NULL))) { 22316 /* 22317 * Bind to IPIF_NOFAILOVER address overrides IP_BOUND_IF 22318 * option. So, see if this endpoint is bound to a 22319 * IPIF_NOFAILOVER address. If so, honor it. This implies 22320 * that if the interface is failed, we will still send 22321 * the packet on the same ill which is what we want. 22322 */ 22323 conn_outgoing_ill = conn_get_held_ill(connp, 22324 &connp->conn_nofailover_ill, &err); 22325 if (err == ILL_LOOKUP_FAILED) { 22326 ire_refrele(ire); 22327 freemsg(mp); 22328 return; 22329 } 22330 if (conn_outgoing_ill == NULL) { 22331 /* 22332 * Choose a good ill in the group to send the 22333 * packets on. 22334 */ 22335 ire = conn_set_outgoing_ill(connp, ire, 22336 &conn_outgoing_ill); 22337 if (ire == NULL) { 22338 freemsg(mp); 22339 return; 22340 } 22341 } 22342 } 22343 22344 if (mp->b_datap->db_type != M_CTL) { 22345 ipha = (ipha_t *)mp->b_rptr; 22346 } else { 22347 io = (ipsec_out_t *)mp->b_rptr; 22348 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22349 ASSERT(zoneid == io->ipsec_out_zoneid); 22350 ASSERT(zoneid != ALL_ZONES); 22351 ipha = (ipha_t *)mp->b_cont->b_rptr; 22352 dst = ipha->ipha_dst; 22353 /* 22354 * For the multicast case, ipsec_out carries conn_dontroute and 22355 * conn_multicast_loop as conn may not be available here. We 22356 * need this for multicast loopback and forwarding which is done 22357 * later in the code. 22358 */ 22359 if (CLASSD(dst)) { 22360 conn_dontroute = io->ipsec_out_dontroute; 22361 conn_multicast_loop = io->ipsec_out_multicast_loop; 22362 /* 22363 * If conn_dontroute is not set or conn_multicast_loop 22364 * is set, we need to do forwarding/loopback. For 22365 * datagrams from ip_wput_multicast, conn_dontroute is 22366 * set to B_TRUE and conn_multicast_loop is set to 22367 * B_FALSE so that we neither do forwarding nor 22368 * loopback. 22369 */ 22370 if (!conn_dontroute || conn_multicast_loop) 22371 multicast_forward = B_TRUE; 22372 } 22373 } 22374 22375 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 22376 ire->ire_zoneid != ALL_ZONES) { 22377 /* 22378 * When a zone sends a packet to another zone, we try to deliver 22379 * the packet under the same conditions as if the destination 22380 * was a real node on the network. To do so, we look for a 22381 * matching route in the forwarding table. 22382 * RTF_REJECT and RTF_BLACKHOLE are handled just like 22383 * ip_newroute() does. 22384 * Note that IRE_LOCAL are special, since they are used 22385 * when the zoneid doesn't match in some cases. This means that 22386 * we need to handle ipha_src differently since ire_src_addr 22387 * belongs to the receiving zone instead of the sending zone. 22388 * When ip_restrict_interzone_loopback is set, then 22389 * ire_cache_lookup() ensures that IRE_LOCAL are only used 22390 * for loopback between zones when the logical "Ethernet" would 22391 * have looped them back. 22392 */ 22393 ire_t *src_ire; 22394 22395 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 22396 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 22397 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE), ipst); 22398 if (src_ire != NULL && 22399 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) && 22400 (!ipst->ips_ip_restrict_interzone_loopback || 22401 ire_local_same_ill_group(ire, src_ire))) { 22402 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 22403 ipha->ipha_src = src_ire->ire_src_addr; 22404 ire_refrele(src_ire); 22405 } else { 22406 ire_refrele(ire); 22407 if (conn_outgoing_ill != NULL) 22408 ill_refrele(conn_outgoing_ill); 22409 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 22410 if (src_ire != NULL) { 22411 if (src_ire->ire_flags & RTF_BLACKHOLE) { 22412 ire_refrele(src_ire); 22413 freemsg(mp); 22414 return; 22415 } 22416 ire_refrele(src_ire); 22417 } 22418 if (ip_hdr_complete(ipha, zoneid, ipst)) { 22419 /* Failed */ 22420 freemsg(mp); 22421 return; 22422 } 22423 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, zoneid, 22424 ipst); 22425 return; 22426 } 22427 } 22428 22429 if (mp->b_datap->db_type == M_CTL || 22430 ipss->ipsec_outbound_v4_policy_present) { 22431 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 22432 unspec_src, zoneid); 22433 if (mp == NULL) { 22434 ire_refrele(ire); 22435 if (conn_outgoing_ill != NULL) 22436 ill_refrele(conn_outgoing_ill); 22437 return; 22438 } 22439 } 22440 22441 first_mp = mp; 22442 ipsec_len = 0; 22443 22444 if (first_mp->b_datap->db_type == M_CTL) { 22445 io = (ipsec_out_t *)first_mp->b_rptr; 22446 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22447 mp = first_mp->b_cont; 22448 ipsec_len = ipsec_out_extra_length(first_mp); 22449 ASSERT(ipsec_len >= 0); 22450 /* We already picked up the zoneid from the M_CTL above */ 22451 ASSERT(zoneid == io->ipsec_out_zoneid); 22452 ASSERT(zoneid != ALL_ZONES); 22453 22454 /* 22455 * Drop M_CTL here if IPsec processing is not needed. 22456 * (Non-IPsec use of M_CTL extracted any information it 22457 * needed above). 22458 */ 22459 if (ipsec_len == 0) { 22460 freeb(first_mp); 22461 first_mp = mp; 22462 } 22463 } 22464 22465 /* 22466 * Fast path for ip_wput_ire 22467 */ 22468 22469 ipha = (ipha_t *)mp->b_rptr; 22470 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 22471 dst = ipha->ipha_dst; 22472 22473 /* 22474 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 22475 * if the socket is a SOCK_RAW type. The transport checksum should 22476 * be provided in the pre-built packet, so we don't need to compute it. 22477 * Also, other application set flags, like DF, should not be altered. 22478 * Other transport MUST pass down zero. 22479 */ 22480 ip_hdr_included = ipha->ipha_ident; 22481 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 22482 22483 if (CLASSD(dst)) { 22484 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 22485 ntohl(dst), 22486 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 22487 ntohl(ire->ire_addr))); 22488 } 22489 22490 /* Macros to extract header fields from data already in registers */ 22491 #ifdef _BIG_ENDIAN 22492 #define V_HLEN (v_hlen_tos_len >> 24) 22493 #define LENGTH (v_hlen_tos_len & 0xFFFF) 22494 #define PROTO (ttl_protocol & 0xFF) 22495 #else 22496 #define V_HLEN (v_hlen_tos_len & 0xFF) 22497 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 22498 #define PROTO (ttl_protocol >> 8) 22499 #endif 22500 22501 22502 orig_src = src = ipha->ipha_src; 22503 /* (The loop back to "another" is explained down below.) */ 22504 another:; 22505 /* 22506 * Assign an ident value for this packet. We assign idents on 22507 * a per destination basis out of the IRE. There could be 22508 * other threads targeting the same destination, so we have to 22509 * arrange for a atomic increment. Note that we use a 32-bit 22510 * atomic add because it has better performance than its 22511 * 16-bit sibling. 22512 * 22513 * If running in cluster mode and if the source address 22514 * belongs to a replicated service then vector through 22515 * cl_inet_ipident vector to allocate ip identifier 22516 * NOTE: This is a contract private interface with the 22517 * clustering group. 22518 */ 22519 clusterwide = 0; 22520 if (cl_inet_ipident) { 22521 ASSERT(cl_inet_isclusterwide); 22522 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 22523 AF_INET, (uint8_t *)(uintptr_t)src)) { 22524 ipha->ipha_ident = (*cl_inet_ipident)(IPPROTO_IP, 22525 AF_INET, (uint8_t *)(uintptr_t)src, 22526 (uint8_t *)(uintptr_t)dst); 22527 clusterwide = 1; 22528 } 22529 } 22530 if (!clusterwide) { 22531 ipha->ipha_ident = 22532 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 22533 } 22534 22535 #ifndef _BIG_ENDIAN 22536 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 22537 #endif 22538 22539 /* 22540 * Set source address unless sent on an ill or conn_unspec_src is set. 22541 * This is needed to obey conn_unspec_src when packets go through 22542 * ip_newroute + arp. 22543 * Assumes ip_newroute{,_multi} sets the source address as well. 22544 */ 22545 if (src == INADDR_ANY && !unspec_src) { 22546 /* 22547 * Assign the appropriate source address from the IRE if none 22548 * was specified. 22549 */ 22550 ASSERT(ire->ire_ipversion == IPV4_VERSION); 22551 22552 /* 22553 * With IP multipathing, broadcast packets are sent on the ire 22554 * that has been cleared of IRE_MARK_NORECV and that belongs to 22555 * the group. However, this ire might not be in the same zone so 22556 * we can't always use its source address. We look for a 22557 * broadcast ire in the same group and in the right zone. 22558 */ 22559 if (ire->ire_type == IRE_BROADCAST && 22560 ire->ire_zoneid != zoneid) { 22561 ire_t *src_ire = ire_ctable_lookup(dst, 0, 22562 IRE_BROADCAST, ire->ire_ipif, zoneid, NULL, 22563 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 22564 if (src_ire != NULL) { 22565 src = src_ire->ire_src_addr; 22566 ire_refrele(src_ire); 22567 } else { 22568 ire_refrele(ire); 22569 if (conn_outgoing_ill != NULL) 22570 ill_refrele(conn_outgoing_ill); 22571 freemsg(first_mp); 22572 if (ill != NULL) { 22573 BUMP_MIB(ill->ill_ip_mib, 22574 ipIfStatsOutDiscards); 22575 } else { 22576 BUMP_MIB(&ipst->ips_ip_mib, 22577 ipIfStatsOutDiscards); 22578 } 22579 return; 22580 } 22581 } else { 22582 src = ire->ire_src_addr; 22583 } 22584 22585 if (connp == NULL) { 22586 ip1dbg(("ip_wput_ire: no connp and no src " 22587 "address for dst 0x%x, using src 0x%x\n", 22588 ntohl(dst), 22589 ntohl(src))); 22590 } 22591 ipha->ipha_src = src; 22592 } 22593 stq = ire->ire_stq; 22594 22595 /* 22596 * We only allow ire chains for broadcasts since there will 22597 * be multiple IRE_CACHE entries for the same multicast 22598 * address (one per ipif). 22599 */ 22600 next_mp = NULL; 22601 22602 /* broadcast packet */ 22603 if (ire->ire_type == IRE_BROADCAST) 22604 goto broadcast; 22605 22606 /* loopback ? */ 22607 if (stq == NULL) 22608 goto nullstq; 22609 22610 /* The ill_index for outbound ILL */ 22611 ill_index = Q_TO_INDEX(stq); 22612 22613 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 22614 ttl_protocol = ((uint16_t *)ipha)[4]; 22615 22616 /* pseudo checksum (do it in parts for IP header checksum) */ 22617 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 22618 22619 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 22620 queue_t *dev_q = stq->q_next; 22621 22622 /* flow controlled */ 22623 if ((dev_q->q_next || dev_q->q_first) && 22624 !canput(dev_q)) 22625 goto blocked; 22626 if ((PROTO == IPPROTO_UDP) && 22627 (ip_hdr_included != IP_HDR_INCLUDED)) { 22628 hlen = (V_HLEN & 0xF) << 2; 22629 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22630 if (*up != 0) { 22631 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 22632 hlen, LENGTH, max_frag, ipsec_len, cksum); 22633 /* Software checksum? */ 22634 if (DB_CKSUMFLAGS(mp) == 0) { 22635 IP_STAT(ipst, ip_out_sw_cksum); 22636 IP_STAT_UPDATE(ipst, 22637 ip_udp_out_sw_cksum_bytes, 22638 LENGTH - hlen); 22639 } 22640 } 22641 } 22642 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 22643 hlen = (V_HLEN & 0xF) << 2; 22644 if (PROTO == IPPROTO_TCP) { 22645 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22646 /* 22647 * The packet header is processed once and for all, even 22648 * in the multirouting case. We disable hardware 22649 * checksum if the packet is multirouted, as it will be 22650 * replicated via several interfaces, and not all of 22651 * them may have this capability. 22652 */ 22653 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 22654 LENGTH, max_frag, ipsec_len, cksum); 22655 /* Software checksum? */ 22656 if (DB_CKSUMFLAGS(mp) == 0) { 22657 IP_STAT(ipst, ip_out_sw_cksum); 22658 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22659 LENGTH - hlen); 22660 } 22661 } else { 22662 sctp_hdr_t *sctph; 22663 22664 ASSERT(PROTO == IPPROTO_SCTP); 22665 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22666 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22667 /* 22668 * Zero out the checksum field to ensure proper 22669 * checksum calculation. 22670 */ 22671 sctph->sh_chksum = 0; 22672 #ifdef DEBUG 22673 if (!skip_sctp_cksum) 22674 #endif 22675 sctph->sh_chksum = sctp_cksum(mp, hlen); 22676 } 22677 } 22678 22679 /* 22680 * If this is a multicast packet and originated from ip_wput 22681 * we need to do loopback and forwarding checks. If it comes 22682 * from ip_wput_multicast, we SHOULD not do this. 22683 */ 22684 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 22685 22686 /* checksum */ 22687 cksum += ttl_protocol; 22688 22689 /* fragment the packet */ 22690 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 22691 goto fragmentit; 22692 /* 22693 * Don't use frag_flag if packet is pre-built or source 22694 * routed or if multicast (since multicast packets do 22695 * not solicit ICMP "packet too big" messages). 22696 */ 22697 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22698 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22699 !ip_source_route_included(ipha)) && 22700 !CLASSD(ipha->ipha_dst)) 22701 ipha->ipha_fragment_offset_and_flags |= 22702 htons(ire->ire_frag_flag); 22703 22704 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22705 /* calculate IP header checksum */ 22706 cksum += ipha->ipha_ident; 22707 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 22708 cksum += ipha->ipha_fragment_offset_and_flags; 22709 22710 /* IP options present */ 22711 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22712 if (hlen) 22713 goto checksumoptions; 22714 22715 /* calculate hdr checksum */ 22716 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22717 cksum = ~(cksum + (cksum >> 16)); 22718 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22719 } 22720 if (ipsec_len != 0) { 22721 /* 22722 * We will do the rest of the processing after 22723 * we come back from IPSEC in ip_wput_ipsec_out(). 22724 */ 22725 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 22726 22727 io = (ipsec_out_t *)first_mp->b_rptr; 22728 io->ipsec_out_ill_index = ((ill_t *)stq->q_ptr)-> 22729 ill_phyint->phyint_ifindex; 22730 22731 ipsec_out_process(q, first_mp, ire, ill_index); 22732 ire_refrele(ire); 22733 if (conn_outgoing_ill != NULL) 22734 ill_refrele(conn_outgoing_ill); 22735 return; 22736 } 22737 22738 /* 22739 * In most cases, the emission loop below is entered only 22740 * once. Only in the case where the ire holds the 22741 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 22742 * flagged ires in the bucket, and send the packet 22743 * through all crossed RTF_MULTIRT routes. 22744 */ 22745 if (ire->ire_flags & RTF_MULTIRT) { 22746 multirt_send = B_TRUE; 22747 } 22748 do { 22749 if (multirt_send) { 22750 irb_t *irb; 22751 /* 22752 * We are in a multiple send case, need to get 22753 * the next ire and make a duplicate of the packet. 22754 * ire1 holds here the next ire to process in the 22755 * bucket. If multirouting is expected, 22756 * any non-RTF_MULTIRT ire that has the 22757 * right destination address is ignored. 22758 */ 22759 irb = ire->ire_bucket; 22760 ASSERT(irb != NULL); 22761 22762 IRB_REFHOLD(irb); 22763 for (ire1 = ire->ire_next; 22764 ire1 != NULL; 22765 ire1 = ire1->ire_next) { 22766 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 22767 continue; 22768 if (ire1->ire_addr != ire->ire_addr) 22769 continue; 22770 if (ire1->ire_marks & 22771 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 22772 continue; 22773 22774 /* Got one */ 22775 IRE_REFHOLD(ire1); 22776 break; 22777 } 22778 IRB_REFRELE(irb); 22779 22780 if (ire1 != NULL) { 22781 next_mp = copyb(mp); 22782 if ((next_mp == NULL) || 22783 ((mp->b_cont != NULL) && 22784 ((next_mp->b_cont = 22785 dupmsg(mp->b_cont)) == NULL))) { 22786 freemsg(next_mp); 22787 next_mp = NULL; 22788 ire_refrele(ire1); 22789 ire1 = NULL; 22790 } 22791 } 22792 22793 /* Last multiroute ire; don't loop anymore. */ 22794 if (ire1 == NULL) { 22795 multirt_send = B_FALSE; 22796 } 22797 } 22798 22799 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 22800 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 22801 mblk_t *, mp); 22802 FW_HOOKS(ipst->ips_ip4_physical_out_event, 22803 ipst->ips_ipv4firewall_physical_out, 22804 NULL, ire->ire_ipif->ipif_ill, ipha, mp, mp, ipst); 22805 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 22806 if (mp == NULL) 22807 goto release_ire_and_ill; 22808 22809 mp->b_prev = SET_BPREV_FLAG(IPP_LOCAL_OUT); 22810 DTRACE_PROBE2(ip__xmit__1, mblk_t *, mp, ire_t *, ire); 22811 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_TRUE); 22812 if ((pktxmit_state == SEND_FAILED) || 22813 (pktxmit_state == LLHDR_RESLV_FAILED)) { 22814 ip2dbg(("ip_wput_ire: ip_xmit_v4 failed" 22815 "- packet dropped\n")); 22816 release_ire_and_ill: 22817 ire_refrele(ire); 22818 if (next_mp != NULL) { 22819 freemsg(next_mp); 22820 ire_refrele(ire1); 22821 } 22822 if (conn_outgoing_ill != NULL) 22823 ill_refrele(conn_outgoing_ill); 22824 return; 22825 } 22826 22827 if (CLASSD(dst)) { 22828 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastPkts); 22829 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastOctets, 22830 LENGTH); 22831 } 22832 22833 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22834 "ip_wput_ire_end: q %p (%S)", 22835 q, "last copy out"); 22836 IRE_REFRELE(ire); 22837 22838 if (multirt_send) { 22839 ASSERT(ire1); 22840 /* 22841 * Proceed with the next RTF_MULTIRT ire, 22842 * Also set up the send-to queue accordingly. 22843 */ 22844 ire = ire1; 22845 ire1 = NULL; 22846 stq = ire->ire_stq; 22847 mp = next_mp; 22848 next_mp = NULL; 22849 ipha = (ipha_t *)mp->b_rptr; 22850 ill_index = Q_TO_INDEX(stq); 22851 ill = (ill_t *)stq->q_ptr; 22852 } 22853 } while (multirt_send); 22854 if (conn_outgoing_ill != NULL) 22855 ill_refrele(conn_outgoing_ill); 22856 return; 22857 22858 /* 22859 * ire->ire_type == IRE_BROADCAST (minimize diffs) 22860 */ 22861 broadcast: 22862 { 22863 /* 22864 * Avoid broadcast storms by setting the ttl to 1 22865 * for broadcasts. This parameter can be set 22866 * via ndd, so make sure that for the SO_DONTROUTE 22867 * case that ipha_ttl is always set to 1. 22868 * In the event that we are replying to incoming 22869 * ICMP packets, conn could be NULL. 22870 */ 22871 if ((connp != NULL) && connp->conn_dontroute) 22872 ipha->ipha_ttl = 1; 22873 else 22874 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl; 22875 22876 /* 22877 * Note that we are not doing a IRB_REFHOLD here. 22878 * Actually we don't care if the list changes i.e 22879 * if somebody deletes an IRE from the list while 22880 * we drop the lock, the next time we come around 22881 * ire_next will be NULL and hence we won't send 22882 * out multiple copies which is fine. 22883 */ 22884 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 22885 ire1 = ire->ire_next; 22886 if (conn_outgoing_ill != NULL) { 22887 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 22888 ASSERT(ire1 == ire->ire_next); 22889 if (ire1 != NULL && ire1->ire_addr == dst) { 22890 ire_refrele(ire); 22891 ire = ire1; 22892 IRE_REFHOLD(ire); 22893 ire1 = ire->ire_next; 22894 continue; 22895 } 22896 rw_exit(&ire->ire_bucket->irb_lock); 22897 /* Did not find a matching ill */ 22898 ip1dbg(("ip_wput_ire: broadcast with no " 22899 "matching IP_BOUND_IF ill %s\n", 22900 conn_outgoing_ill->ill_name)); 22901 freemsg(first_mp); 22902 if (ire != NULL) 22903 ire_refrele(ire); 22904 ill_refrele(conn_outgoing_ill); 22905 return; 22906 } 22907 } else if (ire1 != NULL && ire1->ire_addr == dst) { 22908 /* 22909 * If the next IRE has the same address and is not one 22910 * of the two copies that we need to send, try to see 22911 * whether this copy should be sent at all. This 22912 * assumes that we insert loopbacks first and then 22913 * non-loopbacks. This is acheived by inserting the 22914 * loopback always before non-loopback. 22915 * This is used to send a single copy of a broadcast 22916 * packet out all physical interfaces that have an 22917 * matching IRE_BROADCAST while also looping 22918 * back one copy (to ip_wput_local) for each 22919 * matching physical interface. However, we avoid 22920 * sending packets out different logical that match by 22921 * having ipif_up/ipif_down supress duplicate 22922 * IRE_BROADCASTS. 22923 * 22924 * This feature is currently used to get broadcasts 22925 * sent to multiple interfaces, when the broadcast 22926 * address being used applies to multiple interfaces. 22927 * For example, a whole net broadcast will be 22928 * replicated on every connected subnet of 22929 * the target net. 22930 * 22931 * Each zone has its own set of IRE_BROADCASTs, so that 22932 * we're able to distribute inbound packets to multiple 22933 * zones who share a broadcast address. We avoid looping 22934 * back outbound packets in different zones but on the 22935 * same ill, as the application would see duplicates. 22936 * 22937 * If the interfaces are part of the same group, 22938 * we would want to send only one copy out for 22939 * whole group. 22940 * 22941 * This logic assumes that ire_add_v4() groups the 22942 * IRE_BROADCAST entries so that those with the same 22943 * ire_addr and ill_group are kept together. 22944 */ 22945 ire_ill = ire->ire_ipif->ipif_ill; 22946 if (ire->ire_stq == NULL && ire1->ire_stq != NULL) { 22947 if (ire_ill->ill_group != NULL && 22948 (ire->ire_marks & IRE_MARK_NORECV)) { 22949 /* 22950 * If the current zone only has an ire 22951 * broadcast for this address marked 22952 * NORECV, the ire we want is ahead in 22953 * the bucket, so we look it up 22954 * deliberately ignoring the zoneid. 22955 */ 22956 for (ire1 = ire->ire_bucket->irb_ire; 22957 ire1 != NULL; 22958 ire1 = ire1->ire_next) { 22959 ire1_ill = 22960 ire1->ire_ipif->ipif_ill; 22961 if (ire1->ire_addr != dst) 22962 continue; 22963 /* skip over the current ire */ 22964 if (ire1 == ire) 22965 continue; 22966 /* skip over deleted ires */ 22967 if (ire1->ire_marks & 22968 IRE_MARK_CONDEMNED) 22969 continue; 22970 /* 22971 * non-loopback ire in our 22972 * group: use it for the next 22973 * pass in the loop 22974 */ 22975 if (ire1->ire_stq != NULL && 22976 ire1_ill->ill_group == 22977 ire_ill->ill_group) 22978 break; 22979 } 22980 } 22981 } else { 22982 while (ire1 != NULL && ire1->ire_addr == dst) { 22983 ire1_ill = ire1->ire_ipif->ipif_ill; 22984 /* 22985 * We can have two broadcast ires on the 22986 * same ill in different zones; here 22987 * we'll send a copy of the packet on 22988 * each ill and the fanout code will 22989 * call conn_wantpacket() to check that 22990 * the zone has the broadcast address 22991 * configured on the ill. If the two 22992 * ires are in the same group we only 22993 * send one copy up. 22994 */ 22995 if (ire1_ill != ire_ill && 22996 (ire1_ill->ill_group == NULL || 22997 ire_ill->ill_group == NULL || 22998 ire1_ill->ill_group != 22999 ire_ill->ill_group)) { 23000 break; 23001 } 23002 ire1 = ire1->ire_next; 23003 } 23004 } 23005 } 23006 ASSERT(multirt_send == B_FALSE); 23007 if (ire1 != NULL && ire1->ire_addr == dst) { 23008 if ((ire->ire_flags & RTF_MULTIRT) && 23009 (ire1->ire_flags & RTF_MULTIRT)) { 23010 /* 23011 * We are in the multirouting case. 23012 * The message must be sent at least 23013 * on both ires. These ires have been 23014 * inserted AFTER the standard ones 23015 * in ip_rt_add(). There are thus no 23016 * other ire entries for the destination 23017 * address in the rest of the bucket 23018 * that do not have the RTF_MULTIRT 23019 * flag. We don't process a copy 23020 * of the message here. This will be 23021 * done in the final sending loop. 23022 */ 23023 multirt_send = B_TRUE; 23024 } else { 23025 next_mp = ip_copymsg(first_mp); 23026 if (next_mp != NULL) 23027 IRE_REFHOLD(ire1); 23028 } 23029 } 23030 rw_exit(&ire->ire_bucket->irb_lock); 23031 } 23032 23033 if (stq) { 23034 /* 23035 * A non-NULL send-to queue means this packet is going 23036 * out of this machine. 23037 */ 23038 out_ill = (ill_t *)stq->q_ptr; 23039 23040 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutRequests); 23041 ttl_protocol = ((uint16_t *)ipha)[4]; 23042 /* 23043 * We accumulate the pseudo header checksum in cksum. 23044 * This is pretty hairy code, so watch close. One 23045 * thing to keep in mind is that UDP and TCP have 23046 * stored their respective datagram lengths in their 23047 * checksum fields. This lines things up real nice. 23048 */ 23049 cksum = (dst >> 16) + (dst & 0xFFFF) + 23050 (src >> 16) + (src & 0xFFFF); 23051 /* 23052 * We assume the udp checksum field contains the 23053 * length, so to compute the pseudo header checksum, 23054 * all we need is the protocol number and src/dst. 23055 */ 23056 /* Provide the checksums for UDP and TCP. */ 23057 if ((PROTO == IPPROTO_TCP) && 23058 (ip_hdr_included != IP_HDR_INCLUDED)) { 23059 /* hlen gets the number of uchar_ts in the IP header */ 23060 hlen = (V_HLEN & 0xF) << 2; 23061 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 23062 IP_STAT(ipst, ip_out_sw_cksum); 23063 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 23064 LENGTH - hlen); 23065 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 23066 if (*up == 0) 23067 *up = 0xFFFF; 23068 } else if (PROTO == IPPROTO_SCTP && 23069 (ip_hdr_included != IP_HDR_INCLUDED)) { 23070 sctp_hdr_t *sctph; 23071 23072 hlen = (V_HLEN & 0xF) << 2; 23073 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 23074 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 23075 sctph->sh_chksum = 0; 23076 #ifdef DEBUG 23077 if (!skip_sctp_cksum) 23078 #endif 23079 sctph->sh_chksum = sctp_cksum(mp, hlen); 23080 } else { 23081 queue_t *dev_q = stq->q_next; 23082 23083 if ((dev_q->q_next || dev_q->q_first) && 23084 !canput(dev_q)) { 23085 blocked: 23086 ipha->ipha_ident = ip_hdr_included; 23087 /* 23088 * If we don't have a conn to apply 23089 * backpressure, free the message. 23090 * In the ire_send path, we don't know 23091 * the position to requeue the packet. Rather 23092 * than reorder packets, we just drop this 23093 * packet. 23094 */ 23095 if (ipst->ips_ip_output_queue && 23096 connp != NULL && 23097 caller != IRE_SEND) { 23098 if (caller == IP_WSRV) { 23099 connp->conn_did_putbq = 1; 23100 (void) putbq(connp->conn_wq, 23101 first_mp); 23102 conn_drain_insert(connp); 23103 /* 23104 * This is the service thread, 23105 * and the queue is already 23106 * noenabled. The check for 23107 * canput and the putbq is not 23108 * atomic. So we need to check 23109 * again. 23110 */ 23111 if (canput(stq->q_next)) 23112 connp->conn_did_putbq 23113 = 0; 23114 IP_STAT(ipst, ip_conn_flputbq); 23115 } else { 23116 /* 23117 * We are not the service proc. 23118 * ip_wsrv will be scheduled or 23119 * is already running. 23120 */ 23121 (void) putq(connp->conn_wq, 23122 first_mp); 23123 } 23124 } else { 23125 out_ill = (ill_t *)stq->q_ptr; 23126 BUMP_MIB(out_ill->ill_ip_mib, 23127 ipIfStatsOutDiscards); 23128 freemsg(first_mp); 23129 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23130 "ip_wput_ire_end: q %p (%S)", 23131 q, "discard"); 23132 } 23133 ire_refrele(ire); 23134 if (next_mp) { 23135 ire_refrele(ire1); 23136 freemsg(next_mp); 23137 } 23138 if (conn_outgoing_ill != NULL) 23139 ill_refrele(conn_outgoing_ill); 23140 return; 23141 } 23142 if ((PROTO == IPPROTO_UDP) && 23143 (ip_hdr_included != IP_HDR_INCLUDED)) { 23144 /* 23145 * hlen gets the number of uchar_ts in the 23146 * IP header 23147 */ 23148 hlen = (V_HLEN & 0xF) << 2; 23149 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 23150 max_frag = ire->ire_max_frag; 23151 if (*up != 0) { 23152 IP_CKSUM_XMIT(ire_ill, ire, mp, ipha, 23153 up, PROTO, hlen, LENGTH, max_frag, 23154 ipsec_len, cksum); 23155 /* Software checksum? */ 23156 if (DB_CKSUMFLAGS(mp) == 0) { 23157 IP_STAT(ipst, ip_out_sw_cksum); 23158 IP_STAT_UPDATE(ipst, 23159 ip_udp_out_sw_cksum_bytes, 23160 LENGTH - hlen); 23161 } 23162 } 23163 } 23164 } 23165 /* 23166 * Need to do this even when fragmenting. The local 23167 * loopback can be done without computing checksums 23168 * but forwarding out other interface must be done 23169 * after the IP checksum (and ULP checksums) have been 23170 * computed. 23171 * 23172 * NOTE : multicast_forward is set only if this packet 23173 * originated from ip_wput. For packets originating from 23174 * ip_wput_multicast, it is not set. 23175 */ 23176 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 23177 multi_loopback: 23178 ip2dbg(("ip_wput: multicast, loop %d\n", 23179 conn_multicast_loop)); 23180 23181 /* Forget header checksum offload */ 23182 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 23183 23184 /* 23185 * Local loopback of multicasts? Check the 23186 * ill. 23187 * 23188 * Note that the loopback function will not come 23189 * in through ip_rput - it will only do the 23190 * client fanout thus we need to do an mforward 23191 * as well. The is different from the BSD 23192 * logic. 23193 */ 23194 if (ill != NULL) { 23195 ilm_t *ilm; 23196 23197 ILM_WALKER_HOLD(ill); 23198 ilm = ilm_lookup_ill(ill, ipha->ipha_dst, 23199 ALL_ZONES); 23200 ILM_WALKER_RELE(ill); 23201 if (ilm != NULL) { 23202 /* 23203 * Pass along the virtual output q. 23204 * ip_wput_local() will distribute the 23205 * packet to all the matching zones, 23206 * except the sending zone when 23207 * IP_MULTICAST_LOOP is false. 23208 */ 23209 ip_multicast_loopback(q, ill, first_mp, 23210 conn_multicast_loop ? 0 : 23211 IP_FF_NO_MCAST_LOOP, zoneid); 23212 } 23213 } 23214 if (ipha->ipha_ttl == 0) { 23215 /* 23216 * 0 => only to this host i.e. we are 23217 * done. We are also done if this was the 23218 * loopback interface since it is sufficient 23219 * to loopback one copy of a multicast packet. 23220 */ 23221 freemsg(first_mp); 23222 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23223 "ip_wput_ire_end: q %p (%S)", 23224 q, "loopback"); 23225 ire_refrele(ire); 23226 if (conn_outgoing_ill != NULL) 23227 ill_refrele(conn_outgoing_ill); 23228 return; 23229 } 23230 /* 23231 * ILLF_MULTICAST is checked in ip_newroute 23232 * i.e. we don't need to check it here since 23233 * all IRE_CACHEs come from ip_newroute. 23234 * For multicast traffic, SO_DONTROUTE is interpreted 23235 * to mean only send the packet out the interface 23236 * (optionally specified with IP_MULTICAST_IF) 23237 * and do not forward it out additional interfaces. 23238 * RSVP and the rsvp daemon is an example of a 23239 * protocol and user level process that 23240 * handles it's own routing. Hence, it uses the 23241 * SO_DONTROUTE option to accomplish this. 23242 */ 23243 23244 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 23245 ill != NULL) { 23246 /* Unconditionally redo the checksum */ 23247 ipha->ipha_hdr_checksum = 0; 23248 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23249 23250 /* 23251 * If this needs to go out secure, we need 23252 * to wait till we finish the IPSEC 23253 * processing. 23254 */ 23255 if (ipsec_len == 0 && 23256 ip_mforward(ill, ipha, mp)) { 23257 freemsg(first_mp); 23258 ip1dbg(("ip_wput: mforward failed\n")); 23259 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23260 "ip_wput_ire_end: q %p (%S)", 23261 q, "mforward failed"); 23262 ire_refrele(ire); 23263 if (conn_outgoing_ill != NULL) 23264 ill_refrele(conn_outgoing_ill); 23265 return; 23266 } 23267 } 23268 } 23269 max_frag = ire->ire_max_frag; 23270 cksum += ttl_protocol; 23271 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 23272 /* No fragmentation required for this one. */ 23273 /* 23274 * Don't use frag_flag if packet is pre-built or source 23275 * routed or if multicast (since multicast packets do 23276 * not solicit ICMP "packet too big" messages). 23277 */ 23278 if ((ip_hdr_included != IP_HDR_INCLUDED) && 23279 (V_HLEN == IP_SIMPLE_HDR_VERSION || 23280 !ip_source_route_included(ipha)) && 23281 !CLASSD(ipha->ipha_dst)) 23282 ipha->ipha_fragment_offset_and_flags |= 23283 htons(ire->ire_frag_flag); 23284 23285 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 23286 /* Complete the IP header checksum. */ 23287 cksum += ipha->ipha_ident; 23288 cksum += (v_hlen_tos_len >> 16)+ 23289 (v_hlen_tos_len & 0xFFFF); 23290 cksum += ipha->ipha_fragment_offset_and_flags; 23291 hlen = (V_HLEN & 0xF) - 23292 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 23293 if (hlen) { 23294 checksumoptions: 23295 /* 23296 * Account for the IP Options in the IP 23297 * header checksum. 23298 */ 23299 up = (uint16_t *)(rptr+ 23300 IP_SIMPLE_HDR_LENGTH); 23301 do { 23302 cksum += up[0]; 23303 cksum += up[1]; 23304 up += 2; 23305 } while (--hlen); 23306 } 23307 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 23308 cksum = ~(cksum + (cksum >> 16)); 23309 ipha->ipha_hdr_checksum = (uint16_t)cksum; 23310 } 23311 if (ipsec_len != 0) { 23312 ipsec_out_process(q, first_mp, ire, ill_index); 23313 if (!next_mp) { 23314 ire_refrele(ire); 23315 if (conn_outgoing_ill != NULL) 23316 ill_refrele(conn_outgoing_ill); 23317 return; 23318 } 23319 goto next; 23320 } 23321 23322 /* 23323 * multirt_send has already been handled 23324 * for broadcast, but not yet for multicast 23325 * or IP options. 23326 */ 23327 if (next_mp == NULL) { 23328 if (ire->ire_flags & RTF_MULTIRT) { 23329 multirt_send = B_TRUE; 23330 } 23331 } 23332 23333 /* 23334 * In most cases, the emission loop below is 23335 * entered only once. Only in the case where 23336 * the ire holds the RTF_MULTIRT flag, do we loop 23337 * to process all RTF_MULTIRT ires in the bucket, 23338 * and send the packet through all crossed 23339 * RTF_MULTIRT routes. 23340 */ 23341 do { 23342 if (multirt_send) { 23343 irb_t *irb; 23344 23345 irb = ire->ire_bucket; 23346 ASSERT(irb != NULL); 23347 /* 23348 * We are in a multiple send case, 23349 * need to get the next IRE and make 23350 * a duplicate of the packet. 23351 */ 23352 IRB_REFHOLD(irb); 23353 for (ire1 = ire->ire_next; 23354 ire1 != NULL; 23355 ire1 = ire1->ire_next) { 23356 if (!(ire1->ire_flags & 23357 RTF_MULTIRT)) 23358 continue; 23359 if (ire1->ire_addr != 23360 ire->ire_addr) 23361 continue; 23362 if (ire1->ire_marks & 23363 (IRE_MARK_CONDEMNED| 23364 IRE_MARK_HIDDEN)) 23365 continue; 23366 23367 /* Got one */ 23368 IRE_REFHOLD(ire1); 23369 break; 23370 } 23371 IRB_REFRELE(irb); 23372 23373 if (ire1 != NULL) { 23374 next_mp = copyb(mp); 23375 if ((next_mp == NULL) || 23376 ((mp->b_cont != NULL) && 23377 ((next_mp->b_cont = 23378 dupmsg(mp->b_cont)) 23379 == NULL))) { 23380 freemsg(next_mp); 23381 next_mp = NULL; 23382 ire_refrele(ire1); 23383 ire1 = NULL; 23384 } 23385 } 23386 23387 /* 23388 * Last multiroute ire; don't loop 23389 * anymore. The emission is over 23390 * and next_mp is NULL. 23391 */ 23392 if (ire1 == NULL) { 23393 multirt_send = B_FALSE; 23394 } 23395 } 23396 23397 out_ill = ire->ire_ipif->ipif_ill; 23398 DTRACE_PROBE4(ip4__physical__out__start, 23399 ill_t *, NULL, 23400 ill_t *, out_ill, 23401 ipha_t *, ipha, mblk_t *, mp); 23402 FW_HOOKS(ipst->ips_ip4_physical_out_event, 23403 ipst->ips_ipv4firewall_physical_out, 23404 NULL, out_ill, ipha, mp, mp, ipst); 23405 DTRACE_PROBE1(ip4__physical__out__end, 23406 mblk_t *, mp); 23407 if (mp == NULL) 23408 goto release_ire_and_ill_2; 23409 23410 ASSERT(ipsec_len == 0); 23411 mp->b_prev = 23412 SET_BPREV_FLAG(IPP_LOCAL_OUT); 23413 DTRACE_PROBE2(ip__xmit__2, 23414 mblk_t *, mp, ire_t *, ire); 23415 pktxmit_state = ip_xmit_v4(mp, ire, 23416 NULL, B_TRUE); 23417 if ((pktxmit_state == SEND_FAILED) || 23418 (pktxmit_state == LLHDR_RESLV_FAILED)) { 23419 release_ire_and_ill_2: 23420 if (next_mp) { 23421 freemsg(next_mp); 23422 ire_refrele(ire1); 23423 } 23424 ire_refrele(ire); 23425 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23426 "ip_wput_ire_end: q %p (%S)", 23427 q, "discard MDATA"); 23428 if (conn_outgoing_ill != NULL) 23429 ill_refrele(conn_outgoing_ill); 23430 return; 23431 } 23432 23433 if (CLASSD(dst)) { 23434 BUMP_MIB(out_ill->ill_ip_mib, 23435 ipIfStatsHCOutMcastPkts); 23436 UPDATE_MIB(out_ill->ill_ip_mib, 23437 ipIfStatsHCOutMcastOctets, 23438 LENGTH); 23439 } else if (ire->ire_type == IRE_BROADCAST) { 23440 BUMP_MIB(out_ill->ill_ip_mib, 23441 ipIfStatsHCOutBcastPkts); 23442 } 23443 23444 if (multirt_send) { 23445 /* 23446 * We are in a multiple send case, 23447 * need to re-enter the sending loop 23448 * using the next ire. 23449 */ 23450 ire_refrele(ire); 23451 ire = ire1; 23452 stq = ire->ire_stq; 23453 mp = next_mp; 23454 next_mp = NULL; 23455 ipha = (ipha_t *)mp->b_rptr; 23456 ill_index = Q_TO_INDEX(stq); 23457 } 23458 } while (multirt_send); 23459 23460 if (!next_mp) { 23461 /* 23462 * Last copy going out (the ultra-common 23463 * case). Note that we intentionally replicate 23464 * the putnext rather than calling it before 23465 * the next_mp check in hopes of a little 23466 * tail-call action out of the compiler. 23467 */ 23468 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23469 "ip_wput_ire_end: q %p (%S)", 23470 q, "last copy out(1)"); 23471 ire_refrele(ire); 23472 if (conn_outgoing_ill != NULL) 23473 ill_refrele(conn_outgoing_ill); 23474 return; 23475 } 23476 /* More copies going out below. */ 23477 } else { 23478 int offset; 23479 fragmentit: 23480 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23481 /* 23482 * If this would generate a icmp_frag_needed message, 23483 * we need to handle it before we do the IPSEC 23484 * processing. Otherwise, we need to strip the IPSEC 23485 * headers before we send up the message to the ULPs 23486 * which becomes messy and difficult. 23487 */ 23488 if (ipsec_len != 0) { 23489 if ((max_frag < (unsigned int)(LENGTH + 23490 ipsec_len)) && (offset & IPH_DF)) { 23491 out_ill = (ill_t *)stq->q_ptr; 23492 BUMP_MIB(out_ill->ill_ip_mib, 23493 ipIfStatsOutFragFails); 23494 BUMP_MIB(out_ill->ill_ip_mib, 23495 ipIfStatsOutFragReqds); 23496 ipha->ipha_hdr_checksum = 0; 23497 ipha->ipha_hdr_checksum = 23498 (uint16_t)ip_csum_hdr(ipha); 23499 icmp_frag_needed(ire->ire_stq, first_mp, 23500 max_frag, zoneid, ipst); 23501 if (!next_mp) { 23502 ire_refrele(ire); 23503 if (conn_outgoing_ill != NULL) { 23504 ill_refrele( 23505 conn_outgoing_ill); 23506 } 23507 return; 23508 } 23509 } else { 23510 /* 23511 * This won't cause a icmp_frag_needed 23512 * message. to be generated. Send it on 23513 * the wire. Note that this could still 23514 * cause fragmentation and all we 23515 * do is the generation of the message 23516 * to the ULP if needed before IPSEC. 23517 */ 23518 if (!next_mp) { 23519 ipsec_out_process(q, first_mp, 23520 ire, ill_index); 23521 TRACE_2(TR_FAC_IP, 23522 TR_IP_WPUT_IRE_END, 23523 "ip_wput_ire_end: q %p " 23524 "(%S)", q, 23525 "last ipsec_out_process"); 23526 ire_refrele(ire); 23527 if (conn_outgoing_ill != NULL) { 23528 ill_refrele( 23529 conn_outgoing_ill); 23530 } 23531 return; 23532 } 23533 ipsec_out_process(q, first_mp, 23534 ire, ill_index); 23535 } 23536 } else { 23537 /* 23538 * Initiate IPPF processing. For 23539 * fragmentable packets we finish 23540 * all QOS packet processing before 23541 * calling: 23542 * ip_wput_ire_fragmentit->ip_wput_frag 23543 */ 23544 23545 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23546 ip_process(IPP_LOCAL_OUT, &mp, 23547 ill_index); 23548 if (mp == NULL) { 23549 out_ill = (ill_t *)stq->q_ptr; 23550 BUMP_MIB(out_ill->ill_ip_mib, 23551 ipIfStatsOutDiscards); 23552 if (next_mp != NULL) { 23553 freemsg(next_mp); 23554 ire_refrele(ire1); 23555 } 23556 ire_refrele(ire); 23557 TRACE_2(TR_FAC_IP, 23558 TR_IP_WPUT_IRE_END, 23559 "ip_wput_ire: q %p (%S)", 23560 q, "discard MDATA"); 23561 if (conn_outgoing_ill != NULL) { 23562 ill_refrele( 23563 conn_outgoing_ill); 23564 } 23565 return; 23566 } 23567 } 23568 if (!next_mp) { 23569 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23570 "ip_wput_ire_end: q %p (%S)", 23571 q, "last fragmentation"); 23572 ip_wput_ire_fragmentit(mp, ire, 23573 zoneid, ipst); 23574 ire_refrele(ire); 23575 if (conn_outgoing_ill != NULL) 23576 ill_refrele(conn_outgoing_ill); 23577 return; 23578 } 23579 ip_wput_ire_fragmentit(mp, ire, zoneid, ipst); 23580 } 23581 } 23582 } else { 23583 nullstq: 23584 /* A NULL stq means the destination address is local. */ 23585 UPDATE_OB_PKT_COUNT(ire); 23586 ire->ire_last_used_time = lbolt; 23587 ASSERT(ire->ire_ipif != NULL); 23588 if (!next_mp) { 23589 /* 23590 * Is there an "in" and "out" for traffic local 23591 * to a host (loopback)? The code in Solaris doesn't 23592 * explicitly draw a line in its code for in vs out, 23593 * so we've had to draw a line in the sand: ip_wput_ire 23594 * is considered to be the "output" side and 23595 * ip_wput_local to be the "input" side. 23596 */ 23597 out_ill = ire->ire_ipif->ipif_ill; 23598 23599 DTRACE_PROBE4(ip4__loopback__out__start, 23600 ill_t *, NULL, ill_t *, out_ill, 23601 ipha_t *, ipha, mblk_t *, first_mp); 23602 23603 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23604 ipst->ips_ipv4firewall_loopback_out, 23605 NULL, out_ill, ipha, first_mp, mp, ipst); 23606 23607 DTRACE_PROBE1(ip4__loopback__out_end, 23608 mblk_t *, first_mp); 23609 23610 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23611 "ip_wput_ire_end: q %p (%S)", 23612 q, "local address"); 23613 23614 if (first_mp != NULL) 23615 ip_wput_local(q, out_ill, ipha, 23616 first_mp, ire, 0, ire->ire_zoneid); 23617 ire_refrele(ire); 23618 if (conn_outgoing_ill != NULL) 23619 ill_refrele(conn_outgoing_ill); 23620 return; 23621 } 23622 23623 out_ill = ire->ire_ipif->ipif_ill; 23624 23625 DTRACE_PROBE4(ip4__loopback__out__start, 23626 ill_t *, NULL, ill_t *, out_ill, 23627 ipha_t *, ipha, mblk_t *, first_mp); 23628 23629 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23630 ipst->ips_ipv4firewall_loopback_out, 23631 NULL, out_ill, ipha, first_mp, mp, ipst); 23632 23633 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, first_mp); 23634 23635 if (first_mp != NULL) 23636 ip_wput_local(q, out_ill, ipha, 23637 first_mp, ire, 0, ire->ire_zoneid); 23638 } 23639 next: 23640 /* 23641 * More copies going out to additional interfaces. 23642 * ire1 has already been held. We don't need the 23643 * "ire" anymore. 23644 */ 23645 ire_refrele(ire); 23646 ire = ire1; 23647 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 23648 mp = next_mp; 23649 ASSERT(ire->ire_ipversion == IPV4_VERSION); 23650 ill = ire_to_ill(ire); 23651 first_mp = mp; 23652 if (ipsec_len != 0) { 23653 ASSERT(first_mp->b_datap->db_type == M_CTL); 23654 mp = mp->b_cont; 23655 } 23656 dst = ire->ire_addr; 23657 ipha = (ipha_t *)mp->b_rptr; 23658 /* 23659 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 23660 * Restore ipha_ident "no checksum" flag. 23661 */ 23662 src = orig_src; 23663 ipha->ipha_ident = ip_hdr_included; 23664 goto another; 23665 23666 #undef rptr 23667 #undef Q_TO_INDEX 23668 } 23669 23670 /* 23671 * Routine to allocate a message that is used to notify the ULP about MDT. 23672 * The caller may provide a pointer to the link-layer MDT capabilities, 23673 * or NULL if MDT is to be disabled on the stream. 23674 */ 23675 mblk_t * 23676 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 23677 { 23678 mblk_t *mp; 23679 ip_mdt_info_t *mdti; 23680 ill_mdt_capab_t *idst; 23681 23682 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 23683 DB_TYPE(mp) = M_CTL; 23684 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 23685 mdti = (ip_mdt_info_t *)mp->b_rptr; 23686 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 23687 idst = &(mdti->mdt_capab); 23688 23689 /* 23690 * If the caller provides us with the capability, copy 23691 * it over into our notification message; otherwise 23692 * we zero out the capability portion. 23693 */ 23694 if (isrc != NULL) 23695 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23696 else 23697 bzero((caddr_t)idst, sizeof (*idst)); 23698 } 23699 return (mp); 23700 } 23701 23702 /* 23703 * Routine which determines whether MDT can be enabled on the destination 23704 * IRE and IPC combination, and if so, allocates and returns the MDT 23705 * notification mblk that may be used by ULP. We also check if we need to 23706 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 23707 * MDT usage in the past have been lifted. This gets called during IP 23708 * and ULP binding. 23709 */ 23710 mblk_t * 23711 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23712 ill_mdt_capab_t *mdt_cap) 23713 { 23714 mblk_t *mp; 23715 boolean_t rc = B_FALSE; 23716 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23717 23718 ASSERT(dst_ire != NULL); 23719 ASSERT(connp != NULL); 23720 ASSERT(mdt_cap != NULL); 23721 23722 /* 23723 * Currently, we only support simple TCP/{IPv4,IPv6} with 23724 * Multidata, which is handled in tcp_multisend(). This 23725 * is the reason why we do all these checks here, to ensure 23726 * that we don't enable Multidata for the cases which we 23727 * can't handle at the moment. 23728 */ 23729 do { 23730 /* Only do TCP at the moment */ 23731 if (connp->conn_ulp != IPPROTO_TCP) 23732 break; 23733 23734 /* 23735 * IPSEC outbound policy present? Note that we get here 23736 * after calling ipsec_conn_cache_policy() where the global 23737 * policy checking is performed. conn_latch will be 23738 * non-NULL as long as there's a policy defined, 23739 * i.e. conn_out_enforce_policy may be NULL in such case 23740 * when the connection is non-secure, and hence we check 23741 * further if the latch refers to an outbound policy. 23742 */ 23743 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 23744 break; 23745 23746 /* CGTP (multiroute) is enabled? */ 23747 if (dst_ire->ire_flags & RTF_MULTIRT) 23748 break; 23749 23750 /* Outbound IPQoS enabled? */ 23751 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23752 /* 23753 * In this case, we disable MDT for this and all 23754 * future connections going over the interface. 23755 */ 23756 mdt_cap->ill_mdt_on = 0; 23757 break; 23758 } 23759 23760 /* socket option(s) present? */ 23761 if (!CONN_IS_LSO_MD_FASTPATH(connp)) 23762 break; 23763 23764 rc = B_TRUE; 23765 /* CONSTCOND */ 23766 } while (0); 23767 23768 /* Remember the result */ 23769 connp->conn_mdt_ok = rc; 23770 23771 if (!rc) 23772 return (NULL); 23773 else if (!mdt_cap->ill_mdt_on) { 23774 /* 23775 * If MDT has been previously turned off in the past, and we 23776 * currently can do MDT (due to IPQoS policy removal, etc.) 23777 * then enable it for this interface. 23778 */ 23779 mdt_cap->ill_mdt_on = 1; 23780 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 23781 "interface %s\n", ill_name)); 23782 } 23783 23784 /* Allocate the MDT info mblk */ 23785 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 23786 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 23787 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23788 return (NULL); 23789 } 23790 return (mp); 23791 } 23792 23793 /* 23794 * Routine to allocate a message that is used to notify the ULP about LSO. 23795 * The caller may provide a pointer to the link-layer LSO capabilities, 23796 * or NULL if LSO is to be disabled on the stream. 23797 */ 23798 mblk_t * 23799 ip_lsoinfo_alloc(ill_lso_capab_t *isrc) 23800 { 23801 mblk_t *mp; 23802 ip_lso_info_t *lsoi; 23803 ill_lso_capab_t *idst; 23804 23805 if ((mp = allocb(sizeof (*lsoi), BPRI_HI)) != NULL) { 23806 DB_TYPE(mp) = M_CTL; 23807 mp->b_wptr = mp->b_rptr + sizeof (*lsoi); 23808 lsoi = (ip_lso_info_t *)mp->b_rptr; 23809 lsoi->lso_info_id = LSO_IOC_INFO_UPDATE; 23810 idst = &(lsoi->lso_capab); 23811 23812 /* 23813 * If the caller provides us with the capability, copy 23814 * it over into our notification message; otherwise 23815 * we zero out the capability portion. 23816 */ 23817 if (isrc != NULL) 23818 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23819 else 23820 bzero((caddr_t)idst, sizeof (*idst)); 23821 } 23822 return (mp); 23823 } 23824 23825 /* 23826 * Routine which determines whether LSO can be enabled on the destination 23827 * IRE and IPC combination, and if so, allocates and returns the LSO 23828 * notification mblk that may be used by ULP. We also check if we need to 23829 * turn LSO back to 'on' when certain restrictions prohibiting us to allow 23830 * LSO usage in the past have been lifted. This gets called during IP 23831 * and ULP binding. 23832 */ 23833 mblk_t * 23834 ip_lsoinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23835 ill_lso_capab_t *lso_cap) 23836 { 23837 mblk_t *mp; 23838 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23839 23840 ASSERT(dst_ire != NULL); 23841 ASSERT(connp != NULL); 23842 ASSERT(lso_cap != NULL); 23843 23844 connp->conn_lso_ok = B_TRUE; 23845 23846 if ((connp->conn_ulp != IPPROTO_TCP) || 23847 CONN_IPSEC_OUT_ENCAPSULATED(connp) || 23848 (dst_ire->ire_flags & RTF_MULTIRT) || 23849 !CONN_IS_LSO_MD_FASTPATH(connp) || 23850 (IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 23851 connp->conn_lso_ok = B_FALSE; 23852 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23853 /* 23854 * Disable LSO for this and all future connections going 23855 * over the interface. 23856 */ 23857 lso_cap->ill_lso_on = 0; 23858 } 23859 } 23860 23861 if (!connp->conn_lso_ok) 23862 return (NULL); 23863 else if (!lso_cap->ill_lso_on) { 23864 /* 23865 * If LSO has been previously turned off in the past, and we 23866 * currently can do LSO (due to IPQoS policy removal, etc.) 23867 * then enable it for this interface. 23868 */ 23869 lso_cap->ill_lso_on = 1; 23870 ip1dbg(("ip_mdinfo_return: reenabling LSO for interface %s\n", 23871 ill_name)); 23872 } 23873 23874 /* Allocate the LSO info mblk */ 23875 if ((mp = ip_lsoinfo_alloc(lso_cap)) == NULL) 23876 ip0dbg(("ip_lsoinfo_return: can't enable LSO for " 23877 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23878 23879 return (mp); 23880 } 23881 23882 /* 23883 * Create destination address attribute, and fill it with the physical 23884 * destination address and SAP taken from the template DL_UNITDATA_REQ 23885 * message block. 23886 */ 23887 boolean_t 23888 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 23889 { 23890 dl_unitdata_req_t *dlurp; 23891 pattr_t *pa; 23892 pattrinfo_t pa_info; 23893 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 23894 uint_t das_len, das_off; 23895 23896 ASSERT(dlmp != NULL); 23897 23898 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 23899 das_len = dlurp->dl_dest_addr_length; 23900 das_off = dlurp->dl_dest_addr_offset; 23901 23902 pa_info.type = PATTR_DSTADDRSAP; 23903 pa_info.len = sizeof (**das) + das_len - 1; 23904 23905 /* create and associate the attribute */ 23906 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23907 if (pa != NULL) { 23908 ASSERT(*das != NULL); 23909 (*das)->addr_is_group = 0; 23910 (*das)->addr_len = (uint8_t)das_len; 23911 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 23912 } 23913 23914 return (pa != NULL); 23915 } 23916 23917 /* 23918 * Create hardware checksum attribute and fill it with the values passed. 23919 */ 23920 boolean_t 23921 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 23922 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 23923 { 23924 pattr_t *pa; 23925 pattrinfo_t pa_info; 23926 23927 ASSERT(mmd != NULL); 23928 23929 pa_info.type = PATTR_HCKSUM; 23930 pa_info.len = sizeof (pattr_hcksum_t); 23931 23932 /* create and associate the attribute */ 23933 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23934 if (pa != NULL) { 23935 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 23936 23937 hck->hcksum_start_offset = start_offset; 23938 hck->hcksum_stuff_offset = stuff_offset; 23939 hck->hcksum_end_offset = end_offset; 23940 hck->hcksum_flags = flags; 23941 } 23942 return (pa != NULL); 23943 } 23944 23945 /* 23946 * Create zerocopy attribute and fill it with the specified flags 23947 */ 23948 boolean_t 23949 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 23950 { 23951 pattr_t *pa; 23952 pattrinfo_t pa_info; 23953 23954 ASSERT(mmd != NULL); 23955 pa_info.type = PATTR_ZCOPY; 23956 pa_info.len = sizeof (pattr_zcopy_t); 23957 23958 /* create and associate the attribute */ 23959 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23960 if (pa != NULL) { 23961 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 23962 23963 zcopy->zcopy_flags = flags; 23964 } 23965 return (pa != NULL); 23966 } 23967 23968 /* 23969 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 23970 * block chain. We could rewrite to handle arbitrary message block chains but 23971 * that would make the code complicated and slow. Right now there three 23972 * restrictions: 23973 * 23974 * 1. The first message block must contain the complete IP header and 23975 * at least 1 byte of payload data. 23976 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 23977 * so that we can use a single Multidata message. 23978 * 3. No frag must be distributed over two or more message blocks so 23979 * that we don't need more than two packet descriptors per frag. 23980 * 23981 * The above restrictions allow us to support userland applications (which 23982 * will send down a single message block) and NFS over UDP (which will 23983 * send down a chain of at most three message blocks). 23984 * 23985 * We also don't use MDT for payloads with less than or equal to 23986 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 23987 */ 23988 boolean_t 23989 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 23990 { 23991 int blocks; 23992 ssize_t total, missing, size; 23993 23994 ASSERT(mp != NULL); 23995 ASSERT(hdr_len > 0); 23996 23997 size = MBLKL(mp) - hdr_len; 23998 if (size <= 0) 23999 return (B_FALSE); 24000 24001 /* The first mblk contains the header and some payload. */ 24002 blocks = 1; 24003 total = size; 24004 size %= len; 24005 missing = (size == 0) ? 0 : (len - size); 24006 mp = mp->b_cont; 24007 24008 while (mp != NULL) { 24009 /* 24010 * Give up if we encounter a zero length message block. 24011 * In practice, this should rarely happen and therefore 24012 * not worth the trouble of freeing and re-linking the 24013 * mblk from the chain to handle such case. 24014 */ 24015 if ((size = MBLKL(mp)) == 0) 24016 return (B_FALSE); 24017 24018 /* Too many payload buffers for a single Multidata message? */ 24019 if (++blocks > MULTIDATA_MAX_PBUFS) 24020 return (B_FALSE); 24021 24022 total += size; 24023 /* Is a frag distributed over two or more message blocks? */ 24024 if (missing > size) 24025 return (B_FALSE); 24026 size -= missing; 24027 24028 size %= len; 24029 missing = (size == 0) ? 0 : (len - size); 24030 24031 mp = mp->b_cont; 24032 } 24033 24034 return (total > ip_wput_frag_mdt_min); 24035 } 24036 24037 /* 24038 * Outbound IPv4 fragmentation routine using MDT. 24039 */ 24040 static void 24041 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 24042 uint32_t frag_flag, int offset) 24043 { 24044 ipha_t *ipha_orig; 24045 int i1, ip_data_end; 24046 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 24047 mblk_t *hdr_mp, *md_mp = NULL; 24048 unsigned char *hdr_ptr, *pld_ptr; 24049 multidata_t *mmd; 24050 ip_pdescinfo_t pdi; 24051 ill_t *ill; 24052 ip_stack_t *ipst = ire->ire_ipst; 24053 24054 ASSERT(DB_TYPE(mp) == M_DATA); 24055 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 24056 24057 ill = ire_to_ill(ire); 24058 ASSERT(ill != NULL); 24059 24060 ipha_orig = (ipha_t *)mp->b_rptr; 24061 mp->b_rptr += sizeof (ipha_t); 24062 24063 /* Calculate how many packets we will send out */ 24064 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 24065 pkts = (i1 + len - 1) / len; 24066 ASSERT(pkts > 1); 24067 24068 /* Allocate a message block which will hold all the IP Headers. */ 24069 wroff = ipst->ips_ip_wroff_extra; 24070 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 24071 24072 i1 = pkts * hdr_chunk_len; 24073 /* 24074 * Create the header buffer, Multidata and destination address 24075 * and SAP attribute that should be associated with it. 24076 */ 24077 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 24078 ((hdr_mp->b_wptr += i1), 24079 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 24080 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 24081 freemsg(mp); 24082 if (md_mp == NULL) { 24083 freemsg(hdr_mp); 24084 } else { 24085 free_mmd: IP_STAT(ipst, ip_frag_mdt_discarded); 24086 freemsg(md_mp); 24087 } 24088 IP_STAT(ipst, ip_frag_mdt_allocfail); 24089 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 24090 return; 24091 } 24092 IP_STAT(ipst, ip_frag_mdt_allocd); 24093 24094 /* 24095 * Add a payload buffer to the Multidata; this operation must not 24096 * fail, or otherwise our logic in this routine is broken. There 24097 * is no memory allocation done by the routine, so any returned 24098 * failure simply tells us that we've done something wrong. 24099 * 24100 * A failure tells us that either we're adding the same payload 24101 * buffer more than once, or we're trying to add more buffers than 24102 * allowed. None of the above cases should happen, and we panic 24103 * because either there's horrible heap corruption, and/or 24104 * programming mistake. 24105 */ 24106 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24107 goto pbuf_panic; 24108 24109 hdr_ptr = hdr_mp->b_rptr; 24110 pld_ptr = mp->b_rptr; 24111 24112 /* Establish the ending byte offset, based on the starting offset. */ 24113 offset <<= 3; 24114 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 24115 IP_SIMPLE_HDR_LENGTH; 24116 24117 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 24118 24119 while (pld_ptr < mp->b_wptr) { 24120 ipha_t *ipha; 24121 uint16_t offset_and_flags; 24122 uint16_t ip_len; 24123 int error; 24124 24125 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 24126 ipha = (ipha_t *)(hdr_ptr + wroff); 24127 ASSERT(OK_32PTR(ipha)); 24128 *ipha = *ipha_orig; 24129 24130 if (ip_data_end - offset > len) { 24131 offset_and_flags = IPH_MF; 24132 } else { 24133 /* 24134 * Last frag. Set len to the length of this last piece. 24135 */ 24136 len = ip_data_end - offset; 24137 /* A frag of a frag might have IPH_MF non-zero */ 24138 offset_and_flags = 24139 ntohs(ipha->ipha_fragment_offset_and_flags) & 24140 IPH_MF; 24141 } 24142 offset_and_flags |= (uint16_t)(offset >> 3); 24143 offset_and_flags |= (uint16_t)frag_flag; 24144 /* Store the offset and flags in the IP header. */ 24145 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24146 24147 /* Store the length in the IP header. */ 24148 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 24149 ipha->ipha_length = htons(ip_len); 24150 24151 /* 24152 * Set the IP header checksum. Note that mp is just 24153 * the header, so this is easy to pass to ip_csum. 24154 */ 24155 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24156 24157 /* 24158 * Record offset and size of header and data of the next packet 24159 * in the multidata message. 24160 */ 24161 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 24162 PDESC_PLD_INIT(&pdi); 24163 i1 = MIN(mp->b_wptr - pld_ptr, len); 24164 ASSERT(i1 > 0); 24165 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 24166 if (i1 == len) { 24167 pld_ptr += len; 24168 } else { 24169 i1 = len - i1; 24170 mp = mp->b_cont; 24171 ASSERT(mp != NULL); 24172 ASSERT(MBLKL(mp) >= i1); 24173 /* 24174 * Attach the next payload message block to the 24175 * multidata message. 24176 */ 24177 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24178 goto pbuf_panic; 24179 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 24180 pld_ptr = mp->b_rptr + i1; 24181 } 24182 24183 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 24184 KM_NOSLEEP)) == NULL) { 24185 /* 24186 * Any failure other than ENOMEM indicates that we 24187 * have passed in invalid pdesc info or parameters 24188 * to mmd_addpdesc, which must not happen. 24189 * 24190 * EINVAL is a result of failure on boundary checks 24191 * against the pdesc info contents. It should not 24192 * happen, and we panic because either there's 24193 * horrible heap corruption, and/or programming 24194 * mistake. 24195 */ 24196 if (error != ENOMEM) { 24197 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 24198 "pdesc logic error detected for " 24199 "mmd %p pinfo %p (%d)\n", 24200 (void *)mmd, (void *)&pdi, error); 24201 /* NOTREACHED */ 24202 } 24203 IP_STAT(ipst, ip_frag_mdt_addpdescfail); 24204 /* Free unattached payload message blocks as well */ 24205 md_mp->b_cont = mp->b_cont; 24206 goto free_mmd; 24207 } 24208 24209 /* Advance fragment offset. */ 24210 offset += len; 24211 24212 /* Advance to location for next header in the buffer. */ 24213 hdr_ptr += hdr_chunk_len; 24214 24215 /* Did we reach the next payload message block? */ 24216 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 24217 mp = mp->b_cont; 24218 /* 24219 * Attach the next message block with payload 24220 * data to the multidata message. 24221 */ 24222 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24223 goto pbuf_panic; 24224 pld_ptr = mp->b_rptr; 24225 } 24226 } 24227 24228 ASSERT(hdr_mp->b_wptr == hdr_ptr); 24229 ASSERT(mp->b_wptr == pld_ptr); 24230 24231 /* Update IP statistics */ 24232 IP_STAT_UPDATE(ipst, ip_frag_mdt_pkt_out, pkts); 24233 24234 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates, pkts); 24235 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs); 24236 24237 len = ntohs(ipha_orig->ipha_length) + (pkts - 1) * IP_SIMPLE_HDR_LENGTH; 24238 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, pkts); 24239 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, len); 24240 24241 if (pkt_type == OB_PKT) { 24242 ire->ire_ob_pkt_count += pkts; 24243 if (ire->ire_ipif != NULL) 24244 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 24245 } else { 24246 /* 24247 * The type is IB_PKT in the forwarding path and in 24248 * the mobile IP case when the packet is being reverse- 24249 * tunneled to the home agent. 24250 */ 24251 ire->ire_ib_pkt_count += pkts; 24252 ASSERT(!IRE_IS_LOCAL(ire)); 24253 if (ire->ire_type & IRE_BROADCAST) { 24254 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 24255 } else { 24256 UPDATE_MIB(ill->ill_ip_mib, 24257 ipIfStatsHCOutForwDatagrams, pkts); 24258 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 24259 } 24260 } 24261 ire->ire_last_used_time = lbolt; 24262 /* Send it down */ 24263 putnext(ire->ire_stq, md_mp); 24264 return; 24265 24266 pbuf_panic: 24267 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 24268 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 24269 pbuf_idx); 24270 /* NOTREACHED */ 24271 } 24272 24273 /* 24274 * Outbound IP fragmentation routine. 24275 * 24276 * NOTE : This routine does not ire_refrele the ire that is passed in 24277 * as the argument. 24278 */ 24279 static void 24280 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 24281 uint32_t frag_flag, zoneid_t zoneid, ip_stack_t *ipst) 24282 { 24283 int i1; 24284 mblk_t *ll_hdr_mp; 24285 int ll_hdr_len; 24286 int hdr_len; 24287 mblk_t *hdr_mp; 24288 ipha_t *ipha; 24289 int ip_data_end; 24290 int len; 24291 mblk_t *mp = mp_orig, *mp1; 24292 int offset; 24293 queue_t *q; 24294 uint32_t v_hlen_tos_len; 24295 mblk_t *first_mp; 24296 boolean_t mctl_present; 24297 ill_t *ill; 24298 ill_t *out_ill; 24299 mblk_t *xmit_mp; 24300 mblk_t *carve_mp; 24301 ire_t *ire1 = NULL; 24302 ire_t *save_ire = NULL; 24303 mblk_t *next_mp = NULL; 24304 boolean_t last_frag = B_FALSE; 24305 boolean_t multirt_send = B_FALSE; 24306 ire_t *first_ire = NULL; 24307 irb_t *irb = NULL; 24308 mib2_ipIfStatsEntry_t *mibptr = NULL; 24309 24310 ill = ire_to_ill(ire); 24311 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 24312 24313 BUMP_MIB(mibptr, ipIfStatsOutFragReqds); 24314 24315 /* 24316 * IPSEC does not allow hw accelerated packets to be fragmented 24317 * This check is made in ip_wput_ipsec_out prior to coming here 24318 * via ip_wput_ire_fragmentit. 24319 * 24320 * If at this point we have an ire whose ARP request has not 24321 * been sent out, we call ip_xmit_v4->ire_arpresolve to trigger 24322 * sending of ARP query and change ire's state to ND_INCOMPLETE. 24323 * This packet and all fragmentable packets for this ire will 24324 * continue to get dropped while ire_nce->nce_state remains in 24325 * ND_INCOMPLETE. Post-ARP resolution, after ire's nce_state changes to 24326 * ND_REACHABLE, all subsquent large packets for this ire will 24327 * get fragemented and sent out by this function. 24328 */ 24329 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 24330 /* If nce_state is ND_INITIAL, trigger ARP query */ 24331 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 24332 ip1dbg(("ip_wput_frag: mac address for ire is unresolved" 24333 " - dropping packet\n")); 24334 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24335 freemsg(mp); 24336 return; 24337 } 24338 24339 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 24340 "ip_wput_frag_start:"); 24341 24342 if (mp->b_datap->db_type == M_CTL) { 24343 first_mp = mp; 24344 mp_orig = mp = mp->b_cont; 24345 mctl_present = B_TRUE; 24346 } else { 24347 first_mp = mp; 24348 mctl_present = B_FALSE; 24349 } 24350 24351 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 24352 ipha = (ipha_t *)mp->b_rptr; 24353 24354 /* 24355 * If the Don't Fragment flag is on, generate an ICMP destination 24356 * unreachable, fragmentation needed. 24357 */ 24358 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 24359 if (offset & IPH_DF) { 24360 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24361 /* 24362 * Need to compute hdr checksum if called from ip_wput_ire. 24363 * Note that ip_rput_forward verifies the checksum before 24364 * calling this routine so in that case this is a noop. 24365 */ 24366 ipha->ipha_hdr_checksum = 0; 24367 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24368 icmp_frag_needed(ire->ire_stq, first_mp, max_frag, zoneid, 24369 ipst); 24370 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24371 "ip_wput_frag_end:(%S)", 24372 "don't fragment"); 24373 return; 24374 } 24375 if (mctl_present) 24376 freeb(first_mp); 24377 /* 24378 * Establish the starting offset. May not be zero if we are fragging 24379 * a fragment that is being forwarded. 24380 */ 24381 offset = offset & IPH_OFFSET; 24382 24383 /* TODO why is this test needed? */ 24384 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 24385 if (((max_frag - LENGTH) & ~7) < 8) { 24386 /* TODO: notify ulp somehow */ 24387 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24388 freemsg(mp); 24389 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24390 "ip_wput_frag_end:(%S)", 24391 "len < 8"); 24392 return; 24393 } 24394 24395 hdr_len = (V_HLEN & 0xF) << 2; 24396 24397 ipha->ipha_hdr_checksum = 0; 24398 24399 /* 24400 * Establish the number of bytes maximum per frag, after putting 24401 * in the header. 24402 */ 24403 len = (max_frag - hdr_len) & ~7; 24404 24405 /* Check if we can use MDT to send out the frags. */ 24406 ASSERT(!IRE_IS_LOCAL(ire)); 24407 if (hdr_len == IP_SIMPLE_HDR_LENGTH && 24408 ipst->ips_ip_multidata_outbound && 24409 !(ire->ire_flags & RTF_MULTIRT) && 24410 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 24411 ill != NULL && ILL_MDT_CAPABLE(ill) && 24412 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 24413 ASSERT(ill->ill_mdt_capab != NULL); 24414 if (!ill->ill_mdt_capab->ill_mdt_on) { 24415 /* 24416 * If MDT has been previously turned off in the past, 24417 * and we currently can do MDT (due to IPQoS policy 24418 * removal, etc.) then enable it for this interface. 24419 */ 24420 ill->ill_mdt_capab->ill_mdt_on = 1; 24421 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 24422 ill->ill_name)); 24423 } 24424 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 24425 offset); 24426 return; 24427 } 24428 24429 /* Get a copy of the header for the trailing frags */ 24430 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst); 24431 if (!hdr_mp) { 24432 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24433 freemsg(mp); 24434 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24435 "ip_wput_frag_end:(%S)", 24436 "couldn't copy hdr"); 24437 return; 24438 } 24439 if (DB_CRED(mp) != NULL) 24440 mblk_setcred(hdr_mp, DB_CRED(mp)); 24441 24442 /* Store the starting offset, with the MoreFrags flag. */ 24443 i1 = offset | IPH_MF | frag_flag; 24444 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 24445 24446 /* Establish the ending byte offset, based on the starting offset. */ 24447 offset <<= 3; 24448 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 24449 24450 /* Store the length of the first fragment in the IP header. */ 24451 i1 = len + hdr_len; 24452 ASSERT(i1 <= IP_MAXPACKET); 24453 ipha->ipha_length = htons((uint16_t)i1); 24454 24455 /* 24456 * Compute the IP header checksum for the first frag. We have to 24457 * watch out that we stop at the end of the header. 24458 */ 24459 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24460 24461 /* 24462 * Now carve off the first frag. Note that this will include the 24463 * original IP header. 24464 */ 24465 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 24466 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24467 freeb(hdr_mp); 24468 freemsg(mp_orig); 24469 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24470 "ip_wput_frag_end:(%S)", 24471 "couldn't carve first"); 24472 return; 24473 } 24474 24475 /* 24476 * Multirouting case. Each fragment is replicated 24477 * via all non-condemned RTF_MULTIRT routes 24478 * currently resolved. 24479 * We ensure that first_ire is the first RTF_MULTIRT 24480 * ire in the bucket. 24481 */ 24482 if (ire->ire_flags & RTF_MULTIRT) { 24483 irb = ire->ire_bucket; 24484 ASSERT(irb != NULL); 24485 24486 multirt_send = B_TRUE; 24487 24488 /* Make sure we do not omit any multiroute ire. */ 24489 IRB_REFHOLD(irb); 24490 for (first_ire = irb->irb_ire; 24491 first_ire != NULL; 24492 first_ire = first_ire->ire_next) { 24493 if ((first_ire->ire_flags & RTF_MULTIRT) && 24494 (first_ire->ire_addr == ire->ire_addr) && 24495 !(first_ire->ire_marks & 24496 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 24497 break; 24498 } 24499 24500 if (first_ire != NULL) { 24501 if (first_ire != ire) { 24502 IRE_REFHOLD(first_ire); 24503 /* 24504 * Do not release the ire passed in 24505 * as the argument. 24506 */ 24507 ire = first_ire; 24508 } else { 24509 first_ire = NULL; 24510 } 24511 } 24512 IRB_REFRELE(irb); 24513 24514 /* 24515 * Save the first ire; we will need to restore it 24516 * for the trailing frags. 24517 * We REFHOLD save_ire, as each iterated ire will be 24518 * REFRELEd. 24519 */ 24520 save_ire = ire; 24521 IRE_REFHOLD(save_ire); 24522 } 24523 24524 /* 24525 * First fragment emission loop. 24526 * In most cases, the emission loop below is entered only 24527 * once. Only in the case where the ire holds the RTF_MULTIRT 24528 * flag, do we loop to process all RTF_MULTIRT ires in the 24529 * bucket, and send the fragment through all crossed 24530 * RTF_MULTIRT routes. 24531 */ 24532 do { 24533 if (ire->ire_flags & RTF_MULTIRT) { 24534 /* 24535 * We are in a multiple send case, need to get 24536 * the next ire and make a copy of the packet. 24537 * ire1 holds here the next ire to process in the 24538 * bucket. If multirouting is expected, 24539 * any non-RTF_MULTIRT ire that has the 24540 * right destination address is ignored. 24541 * 24542 * We have to take into account the MTU of 24543 * each walked ire. max_frag is set by the 24544 * the caller and generally refers to 24545 * the primary ire entry. Here we ensure that 24546 * no route with a lower MTU will be used, as 24547 * fragments are carved once for all ires, 24548 * then replicated. 24549 */ 24550 ASSERT(irb != NULL); 24551 IRB_REFHOLD(irb); 24552 for (ire1 = ire->ire_next; 24553 ire1 != NULL; 24554 ire1 = ire1->ire_next) { 24555 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 24556 continue; 24557 if (ire1->ire_addr != ire->ire_addr) 24558 continue; 24559 if (ire1->ire_marks & 24560 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 24561 continue; 24562 /* 24563 * Ensure we do not exceed the MTU 24564 * of the next route. 24565 */ 24566 if (ire1->ire_max_frag < max_frag) { 24567 ip_multirt_bad_mtu(ire1, max_frag); 24568 continue; 24569 } 24570 24571 /* Got one. */ 24572 IRE_REFHOLD(ire1); 24573 break; 24574 } 24575 IRB_REFRELE(irb); 24576 24577 if (ire1 != NULL) { 24578 next_mp = copyb(mp); 24579 if ((next_mp == NULL) || 24580 ((mp->b_cont != NULL) && 24581 ((next_mp->b_cont = 24582 dupmsg(mp->b_cont)) == NULL))) { 24583 freemsg(next_mp); 24584 next_mp = NULL; 24585 ire_refrele(ire1); 24586 ire1 = NULL; 24587 } 24588 } 24589 24590 /* Last multiroute ire; don't loop anymore. */ 24591 if (ire1 == NULL) { 24592 multirt_send = B_FALSE; 24593 } 24594 } 24595 24596 ll_hdr_len = 0; 24597 LOCK_IRE_FP_MP(ire); 24598 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24599 if (ll_hdr_mp != NULL) { 24600 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24601 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 24602 } else { 24603 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24604 } 24605 24606 /* If there is a transmit header, get a copy for this frag. */ 24607 /* 24608 * TODO: should check db_ref before calling ip_carve_mp since 24609 * it might give us a dup. 24610 */ 24611 if (!ll_hdr_mp) { 24612 /* No xmit header. */ 24613 xmit_mp = mp; 24614 24615 /* We have a link-layer header that can fit in our mblk. */ 24616 } else if (mp->b_datap->db_ref == 1 && 24617 ll_hdr_len != 0 && 24618 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24619 /* M_DATA fastpath */ 24620 mp->b_rptr -= ll_hdr_len; 24621 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 24622 xmit_mp = mp; 24623 24624 /* Corner case if copyb has failed */ 24625 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 24626 UNLOCK_IRE_FP_MP(ire); 24627 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24628 freeb(hdr_mp); 24629 freemsg(mp); 24630 freemsg(mp_orig); 24631 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24632 "ip_wput_frag_end:(%S)", 24633 "discard"); 24634 24635 if (multirt_send) { 24636 ASSERT(ire1); 24637 ASSERT(next_mp); 24638 24639 freemsg(next_mp); 24640 ire_refrele(ire1); 24641 } 24642 if (save_ire != NULL) 24643 IRE_REFRELE(save_ire); 24644 24645 if (first_ire != NULL) 24646 ire_refrele(first_ire); 24647 return; 24648 24649 /* 24650 * Case of res_mp OR the fastpath mp can't fit 24651 * in the mblk 24652 */ 24653 } else { 24654 xmit_mp->b_cont = mp; 24655 if (DB_CRED(mp) != NULL) 24656 mblk_setcred(xmit_mp, DB_CRED(mp)); 24657 /* 24658 * Get priority marking, if any. 24659 * We propagate the CoS marking from the 24660 * original packet that went to QoS processing 24661 * in ip_wput_ire to the newly carved mp. 24662 */ 24663 if (DB_TYPE(xmit_mp) == M_DATA) 24664 xmit_mp->b_band = mp->b_band; 24665 } 24666 UNLOCK_IRE_FP_MP(ire); 24667 24668 q = ire->ire_stq; 24669 out_ill = (ill_t *)q->q_ptr; 24670 24671 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24672 24673 DTRACE_PROBE4(ip4__physical__out__start, 24674 ill_t *, NULL, ill_t *, out_ill, 24675 ipha_t *, ipha, mblk_t *, xmit_mp); 24676 24677 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24678 ipst->ips_ipv4firewall_physical_out, 24679 NULL, out_ill, ipha, xmit_mp, mp, ipst); 24680 24681 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, xmit_mp); 24682 24683 if (xmit_mp != NULL) { 24684 putnext(q, xmit_mp); 24685 24686 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 24687 UPDATE_MIB(out_ill->ill_ip_mib, 24688 ipIfStatsHCOutOctets, i1); 24689 24690 if (pkt_type != OB_PKT) { 24691 /* 24692 * Update the packet count and MIB stats 24693 * of trailing RTF_MULTIRT ires. 24694 */ 24695 UPDATE_OB_PKT_COUNT(ire); 24696 BUMP_MIB(out_ill->ill_ip_mib, 24697 ipIfStatsOutFragReqds); 24698 } 24699 } 24700 24701 if (multirt_send) { 24702 /* 24703 * We are in a multiple send case; look for 24704 * the next ire and re-enter the loop. 24705 */ 24706 ASSERT(ire1); 24707 ASSERT(next_mp); 24708 /* REFRELE the current ire before looping */ 24709 ire_refrele(ire); 24710 ire = ire1; 24711 ire1 = NULL; 24712 mp = next_mp; 24713 next_mp = NULL; 24714 } 24715 } while (multirt_send); 24716 24717 ASSERT(ire1 == NULL); 24718 24719 /* Restore the original ire; we need it for the trailing frags */ 24720 if (save_ire != NULL) { 24721 /* REFRELE the last iterated ire */ 24722 ire_refrele(ire); 24723 /* save_ire has been REFHOLDed */ 24724 ire = save_ire; 24725 save_ire = NULL; 24726 q = ire->ire_stq; 24727 } 24728 24729 if (pkt_type == OB_PKT) { 24730 UPDATE_OB_PKT_COUNT(ire); 24731 } else { 24732 out_ill = (ill_t *)q->q_ptr; 24733 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 24734 UPDATE_IB_PKT_COUNT(ire); 24735 } 24736 24737 /* Advance the offset to the second frag starting point. */ 24738 offset += len; 24739 /* 24740 * Update hdr_len from the copied header - there might be less options 24741 * in the later fragments. 24742 */ 24743 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 24744 /* Loop until done. */ 24745 for (;;) { 24746 uint16_t offset_and_flags; 24747 uint16_t ip_len; 24748 24749 if (ip_data_end - offset > len) { 24750 /* 24751 * Carve off the appropriate amount from the original 24752 * datagram. 24753 */ 24754 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24755 mp = NULL; 24756 break; 24757 } 24758 /* 24759 * More frags after this one. Get another copy 24760 * of the header. 24761 */ 24762 if (carve_mp->b_datap->db_ref == 1 && 24763 hdr_mp->b_wptr - hdr_mp->b_rptr < 24764 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24765 /* Inline IP header */ 24766 carve_mp->b_rptr -= hdr_mp->b_wptr - 24767 hdr_mp->b_rptr; 24768 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24769 hdr_mp->b_wptr - hdr_mp->b_rptr); 24770 mp = carve_mp; 24771 } else { 24772 if (!(mp = copyb(hdr_mp))) { 24773 freemsg(carve_mp); 24774 break; 24775 } 24776 /* Get priority marking, if any. */ 24777 mp->b_band = carve_mp->b_band; 24778 mp->b_cont = carve_mp; 24779 } 24780 ipha = (ipha_t *)mp->b_rptr; 24781 offset_and_flags = IPH_MF; 24782 } else { 24783 /* 24784 * Last frag. Consume the header. Set len to 24785 * the length of this last piece. 24786 */ 24787 len = ip_data_end - offset; 24788 24789 /* 24790 * Carve off the appropriate amount from the original 24791 * datagram. 24792 */ 24793 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24794 mp = NULL; 24795 break; 24796 } 24797 if (carve_mp->b_datap->db_ref == 1 && 24798 hdr_mp->b_wptr - hdr_mp->b_rptr < 24799 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24800 /* Inline IP header */ 24801 carve_mp->b_rptr -= hdr_mp->b_wptr - 24802 hdr_mp->b_rptr; 24803 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24804 hdr_mp->b_wptr - hdr_mp->b_rptr); 24805 mp = carve_mp; 24806 freeb(hdr_mp); 24807 hdr_mp = mp; 24808 } else { 24809 mp = hdr_mp; 24810 /* Get priority marking, if any. */ 24811 mp->b_band = carve_mp->b_band; 24812 mp->b_cont = carve_mp; 24813 } 24814 ipha = (ipha_t *)mp->b_rptr; 24815 /* A frag of a frag might have IPH_MF non-zero */ 24816 offset_and_flags = 24817 ntohs(ipha->ipha_fragment_offset_and_flags) & 24818 IPH_MF; 24819 } 24820 offset_and_flags |= (uint16_t)(offset >> 3); 24821 offset_and_flags |= (uint16_t)frag_flag; 24822 /* Store the offset and flags in the IP header. */ 24823 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24824 24825 /* Store the length in the IP header. */ 24826 ip_len = (uint16_t)(len + hdr_len); 24827 ipha->ipha_length = htons(ip_len); 24828 24829 /* 24830 * Set the IP header checksum. Note that mp is just 24831 * the header, so this is easy to pass to ip_csum. 24832 */ 24833 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24834 24835 /* Attach a transmit header, if any, and ship it. */ 24836 if (pkt_type == OB_PKT) { 24837 UPDATE_OB_PKT_COUNT(ire); 24838 } else { 24839 out_ill = (ill_t *)q->q_ptr; 24840 BUMP_MIB(out_ill->ill_ip_mib, 24841 ipIfStatsHCOutForwDatagrams); 24842 UPDATE_IB_PKT_COUNT(ire); 24843 } 24844 24845 if (ire->ire_flags & RTF_MULTIRT) { 24846 irb = ire->ire_bucket; 24847 ASSERT(irb != NULL); 24848 24849 multirt_send = B_TRUE; 24850 24851 /* 24852 * Save the original ire; we will need to restore it 24853 * for the tailing frags. 24854 */ 24855 save_ire = ire; 24856 IRE_REFHOLD(save_ire); 24857 } 24858 /* 24859 * Emission loop for this fragment, similar 24860 * to what is done for the first fragment. 24861 */ 24862 do { 24863 if (multirt_send) { 24864 /* 24865 * We are in a multiple send case, need to get 24866 * the next ire and make a copy of the packet. 24867 */ 24868 ASSERT(irb != NULL); 24869 IRB_REFHOLD(irb); 24870 for (ire1 = ire->ire_next; 24871 ire1 != NULL; 24872 ire1 = ire1->ire_next) { 24873 if (!(ire1->ire_flags & RTF_MULTIRT)) 24874 continue; 24875 if (ire1->ire_addr != ire->ire_addr) 24876 continue; 24877 if (ire1->ire_marks & 24878 (IRE_MARK_CONDEMNED| 24879 IRE_MARK_HIDDEN)) 24880 continue; 24881 /* 24882 * Ensure we do not exceed the MTU 24883 * of the next route. 24884 */ 24885 if (ire1->ire_max_frag < max_frag) { 24886 ip_multirt_bad_mtu(ire1, 24887 max_frag); 24888 continue; 24889 } 24890 24891 /* Got one. */ 24892 IRE_REFHOLD(ire1); 24893 break; 24894 } 24895 IRB_REFRELE(irb); 24896 24897 if (ire1 != NULL) { 24898 next_mp = copyb(mp); 24899 if ((next_mp == NULL) || 24900 ((mp->b_cont != NULL) && 24901 ((next_mp->b_cont = 24902 dupmsg(mp->b_cont)) == NULL))) { 24903 freemsg(next_mp); 24904 next_mp = NULL; 24905 ire_refrele(ire1); 24906 ire1 = NULL; 24907 } 24908 } 24909 24910 /* Last multiroute ire; don't loop anymore. */ 24911 if (ire1 == NULL) { 24912 multirt_send = B_FALSE; 24913 } 24914 } 24915 24916 /* Update transmit header */ 24917 ll_hdr_len = 0; 24918 LOCK_IRE_FP_MP(ire); 24919 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24920 if (ll_hdr_mp != NULL) { 24921 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24922 ll_hdr_len = MBLKL(ll_hdr_mp); 24923 } else { 24924 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24925 } 24926 24927 if (!ll_hdr_mp) { 24928 xmit_mp = mp; 24929 24930 /* 24931 * We have link-layer header that can fit in 24932 * our mblk. 24933 */ 24934 } else if (mp->b_datap->db_ref == 1 && 24935 ll_hdr_len != 0 && 24936 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24937 /* M_DATA fastpath */ 24938 mp->b_rptr -= ll_hdr_len; 24939 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 24940 ll_hdr_len); 24941 xmit_mp = mp; 24942 24943 /* 24944 * Case of res_mp OR the fastpath mp can't fit 24945 * in the mblk 24946 */ 24947 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 24948 xmit_mp->b_cont = mp; 24949 if (DB_CRED(mp) != NULL) 24950 mblk_setcred(xmit_mp, DB_CRED(mp)); 24951 /* Get priority marking, if any. */ 24952 if (DB_TYPE(xmit_mp) == M_DATA) 24953 xmit_mp->b_band = mp->b_band; 24954 24955 /* Corner case if copyb failed */ 24956 } else { 24957 /* 24958 * Exit both the replication and 24959 * fragmentation loops. 24960 */ 24961 UNLOCK_IRE_FP_MP(ire); 24962 goto drop_pkt; 24963 } 24964 UNLOCK_IRE_FP_MP(ire); 24965 24966 mp1 = mp; 24967 out_ill = (ill_t *)q->q_ptr; 24968 24969 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24970 24971 DTRACE_PROBE4(ip4__physical__out__start, 24972 ill_t *, NULL, ill_t *, out_ill, 24973 ipha_t *, ipha, mblk_t *, xmit_mp); 24974 24975 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24976 ipst->ips_ipv4firewall_physical_out, 24977 NULL, out_ill, ipha, xmit_mp, mp, ipst); 24978 24979 DTRACE_PROBE1(ip4__physical__out__end, 24980 mblk_t *, xmit_mp); 24981 24982 if (mp != mp1 && hdr_mp == mp1) 24983 hdr_mp = mp; 24984 if (mp != mp1 && mp_orig == mp1) 24985 mp_orig = mp; 24986 24987 if (xmit_mp != NULL) { 24988 putnext(q, xmit_mp); 24989 24990 BUMP_MIB(out_ill->ill_ip_mib, 24991 ipIfStatsHCOutTransmits); 24992 UPDATE_MIB(out_ill->ill_ip_mib, 24993 ipIfStatsHCOutOctets, ip_len); 24994 24995 if (pkt_type != OB_PKT) { 24996 /* 24997 * Update the packet count of trailing 24998 * RTF_MULTIRT ires. 24999 */ 25000 UPDATE_OB_PKT_COUNT(ire); 25001 } 25002 } 25003 25004 /* All done if we just consumed the hdr_mp. */ 25005 if (mp == hdr_mp) { 25006 last_frag = B_TRUE; 25007 BUMP_MIB(out_ill->ill_ip_mib, 25008 ipIfStatsOutFragOKs); 25009 } 25010 25011 if (multirt_send) { 25012 /* 25013 * We are in a multiple send case; look for 25014 * the next ire and re-enter the loop. 25015 */ 25016 ASSERT(ire1); 25017 ASSERT(next_mp); 25018 /* REFRELE the current ire before looping */ 25019 ire_refrele(ire); 25020 ire = ire1; 25021 ire1 = NULL; 25022 q = ire->ire_stq; 25023 mp = next_mp; 25024 next_mp = NULL; 25025 } 25026 } while (multirt_send); 25027 /* 25028 * Restore the original ire; we need it for the 25029 * trailing frags 25030 */ 25031 if (save_ire != NULL) { 25032 ASSERT(ire1 == NULL); 25033 /* REFRELE the last iterated ire */ 25034 ire_refrele(ire); 25035 /* save_ire has been REFHOLDed */ 25036 ire = save_ire; 25037 q = ire->ire_stq; 25038 save_ire = NULL; 25039 } 25040 25041 if (last_frag) { 25042 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 25043 "ip_wput_frag_end:(%S)", 25044 "consumed hdr_mp"); 25045 25046 if (first_ire != NULL) 25047 ire_refrele(first_ire); 25048 return; 25049 } 25050 /* Otherwise, advance and loop. */ 25051 offset += len; 25052 } 25053 25054 drop_pkt: 25055 /* Clean up following allocation failure. */ 25056 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 25057 freemsg(mp); 25058 if (mp != hdr_mp) 25059 freeb(hdr_mp); 25060 if (mp != mp_orig) 25061 freemsg(mp_orig); 25062 25063 if (save_ire != NULL) 25064 IRE_REFRELE(save_ire); 25065 if (first_ire != NULL) 25066 ire_refrele(first_ire); 25067 25068 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 25069 "ip_wput_frag_end:(%S)", 25070 "end--alloc failure"); 25071 } 25072 25073 /* 25074 * Copy the header plus those options which have the copy bit set 25075 */ 25076 static mblk_t * 25077 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst) 25078 { 25079 mblk_t *mp; 25080 uchar_t *up; 25081 25082 /* 25083 * Quick check if we need to look for options without the copy bit 25084 * set 25085 */ 25086 mp = allocb(ipst->ips_ip_wroff_extra + hdr_len, BPRI_HI); 25087 if (!mp) 25088 return (mp); 25089 mp->b_rptr += ipst->ips_ip_wroff_extra; 25090 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 25091 bcopy(rptr, mp->b_rptr, hdr_len); 25092 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra; 25093 return (mp); 25094 } 25095 up = mp->b_rptr; 25096 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 25097 up += IP_SIMPLE_HDR_LENGTH; 25098 rptr += IP_SIMPLE_HDR_LENGTH; 25099 hdr_len -= IP_SIMPLE_HDR_LENGTH; 25100 while (hdr_len > 0) { 25101 uint32_t optval; 25102 uint32_t optlen; 25103 25104 optval = *rptr; 25105 if (optval == IPOPT_EOL) 25106 break; 25107 if (optval == IPOPT_NOP) 25108 optlen = 1; 25109 else 25110 optlen = rptr[1]; 25111 if (optval & IPOPT_COPY) { 25112 bcopy(rptr, up, optlen); 25113 up += optlen; 25114 } 25115 rptr += optlen; 25116 hdr_len -= optlen; 25117 } 25118 /* 25119 * Make sure that we drop an even number of words by filling 25120 * with EOL to the next word boundary. 25121 */ 25122 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 25123 hdr_len & 0x3; hdr_len++) 25124 *up++ = IPOPT_EOL; 25125 mp->b_wptr = up; 25126 /* Update header length */ 25127 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 25128 return (mp); 25129 } 25130 25131 /* 25132 * Delivery to local recipients including fanout to multiple recipients. 25133 * Does not do checksumming of UDP/TCP. 25134 * Note: q should be the read side queue for either the ill or conn. 25135 * Note: rq should be the read side q for the lower (ill) stream. 25136 * We don't send packets to IPPF processing, thus the last argument 25137 * to all the fanout calls are B_FALSE. 25138 */ 25139 void 25140 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 25141 int fanout_flags, zoneid_t zoneid) 25142 { 25143 uint32_t protocol; 25144 mblk_t *first_mp; 25145 boolean_t mctl_present; 25146 int ire_type; 25147 #define rptr ((uchar_t *)ipha) 25148 ip_stack_t *ipst = ill->ill_ipst; 25149 25150 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 25151 "ip_wput_local_start: q %p", q); 25152 25153 if (ire != NULL) { 25154 ire_type = ire->ire_type; 25155 } else { 25156 /* 25157 * Only ip_multicast_loopback() calls us with a NULL ire. If the 25158 * packet is not multicast, we can't tell the ire type. 25159 */ 25160 ASSERT(CLASSD(ipha->ipha_dst)); 25161 ire_type = IRE_BROADCAST; 25162 } 25163 25164 first_mp = mp; 25165 if (first_mp->b_datap->db_type == M_CTL) { 25166 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 25167 if (!io->ipsec_out_secure) { 25168 /* 25169 * This ipsec_out_t was allocated in ip_wput 25170 * for multicast packets to store the ill_index. 25171 * As this is being delivered locally, we don't 25172 * need this anymore. 25173 */ 25174 mp = first_mp->b_cont; 25175 freeb(first_mp); 25176 first_mp = mp; 25177 mctl_present = B_FALSE; 25178 } else { 25179 /* 25180 * Convert IPSEC_OUT to IPSEC_IN, preserving all 25181 * security properties for the looped-back packet. 25182 */ 25183 mctl_present = B_TRUE; 25184 mp = first_mp->b_cont; 25185 ASSERT(mp != NULL); 25186 ipsec_out_to_in(first_mp); 25187 } 25188 } else { 25189 mctl_present = B_FALSE; 25190 } 25191 25192 DTRACE_PROBE4(ip4__loopback__in__start, 25193 ill_t *, ill, ill_t *, NULL, 25194 ipha_t *, ipha, mblk_t *, first_mp); 25195 25196 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 25197 ipst->ips_ipv4firewall_loopback_in, 25198 ill, NULL, ipha, first_mp, mp, ipst); 25199 25200 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, first_mp); 25201 25202 if (first_mp == NULL) 25203 return; 25204 25205 ipst->ips_loopback_packets++; 25206 25207 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 25208 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 25209 if (!IS_SIMPLE_IPH(ipha)) { 25210 ip_wput_local_options(ipha, ipst); 25211 } 25212 25213 protocol = ipha->ipha_protocol; 25214 switch (protocol) { 25215 case IPPROTO_ICMP: { 25216 ire_t *ire_zone; 25217 ilm_t *ilm; 25218 mblk_t *mp1; 25219 zoneid_t last_zoneid; 25220 25221 if (CLASSD(ipha->ipha_dst) && 25222 !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 25223 ASSERT(ire_type == IRE_BROADCAST); 25224 /* 25225 * In the multicast case, applications may have joined 25226 * the group from different zones, so we need to deliver 25227 * the packet to each of them. Loop through the 25228 * multicast memberships structures (ilm) on the receive 25229 * ill and send a copy of the packet up each matching 25230 * one. However, we don't do this for multicasts sent on 25231 * the loopback interface (PHYI_LOOPBACK flag set) as 25232 * they must stay in the sender's zone. 25233 * 25234 * ilm_add_v6() ensures that ilms in the same zone are 25235 * contiguous in the ill_ilm list. We use this property 25236 * to avoid sending duplicates needed when two 25237 * applications in the same zone join the same group on 25238 * different logical interfaces: we ignore the ilm if 25239 * it's zoneid is the same as the last matching one. 25240 * In addition, the sending of the packet for 25241 * ire_zoneid is delayed until all of the other ilms 25242 * have been exhausted. 25243 */ 25244 last_zoneid = -1; 25245 ILM_WALKER_HOLD(ill); 25246 for (ilm = ill->ill_ilm; ilm != NULL; 25247 ilm = ilm->ilm_next) { 25248 if ((ilm->ilm_flags & ILM_DELETED) || 25249 ipha->ipha_dst != ilm->ilm_addr || 25250 ilm->ilm_zoneid == last_zoneid || 25251 ilm->ilm_zoneid == zoneid || 25252 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 25253 continue; 25254 mp1 = ip_copymsg(first_mp); 25255 if (mp1 == NULL) 25256 continue; 25257 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25258 mctl_present, B_FALSE, ill, 25259 ilm->ilm_zoneid); 25260 last_zoneid = ilm->ilm_zoneid; 25261 } 25262 ILM_WALKER_RELE(ill); 25263 /* 25264 * Loopback case: the sending endpoint has 25265 * IP_MULTICAST_LOOP disabled, therefore we don't 25266 * dispatch the multicast packet to the sending zone. 25267 */ 25268 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 25269 freemsg(first_mp); 25270 return; 25271 } 25272 } else if (ire_type == IRE_BROADCAST) { 25273 /* 25274 * In the broadcast case, there may be many zones 25275 * which need a copy of the packet delivered to them. 25276 * There is one IRE_BROADCAST per broadcast address 25277 * and per zone; we walk those using a helper function. 25278 * In addition, the sending of the packet for zoneid is 25279 * delayed until all of the other ires have been 25280 * processed. 25281 */ 25282 IRB_REFHOLD(ire->ire_bucket); 25283 ire_zone = NULL; 25284 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 25285 ire)) != NULL) { 25286 mp1 = ip_copymsg(first_mp); 25287 if (mp1 == NULL) 25288 continue; 25289 25290 UPDATE_IB_PKT_COUNT(ire_zone); 25291 ire_zone->ire_last_used_time = lbolt; 25292 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25293 mctl_present, B_FALSE, ill, 25294 ire_zone->ire_zoneid); 25295 } 25296 IRB_REFRELE(ire->ire_bucket); 25297 } 25298 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 25299 0, mctl_present, B_FALSE, ill, zoneid); 25300 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25301 "ip_wput_local_end: q %p (%S)", 25302 q, "icmp"); 25303 return; 25304 } 25305 case IPPROTO_IGMP: 25306 if ((mp = igmp_input(q, mp, ill)) == NULL) { 25307 /* Bad packet - discarded by igmp_input */ 25308 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25309 "ip_wput_local_end: q %p (%S)", 25310 q, "igmp_input--bad packet"); 25311 if (mctl_present) 25312 freeb(first_mp); 25313 return; 25314 } 25315 /* 25316 * igmp_input() may have returned the pulled up message. 25317 * So first_mp and ipha need to be reinitialized. 25318 */ 25319 ipha = (ipha_t *)mp->b_rptr; 25320 if (mctl_present) 25321 first_mp->b_cont = mp; 25322 else 25323 first_mp = mp; 25324 /* deliver to local raw users */ 25325 break; 25326 case IPPROTO_ENCAP: 25327 /* 25328 * This case is covered by either ip_fanout_proto, or by 25329 * the above security processing for self-tunneled packets. 25330 */ 25331 break; 25332 case IPPROTO_UDP: { 25333 uint16_t *up; 25334 uint32_t ports; 25335 25336 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 25337 UDP_PORTS_OFFSET); 25338 /* Force a 'valid' checksum. */ 25339 up[3] = 0; 25340 25341 ports = *(uint32_t *)up; 25342 ip_fanout_udp(q, first_mp, ill, ipha, ports, 25343 (ire_type == IRE_BROADCAST), 25344 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25345 IP_FF_SEND_SLLA | IP_FF_IPINFO, mctl_present, B_FALSE, 25346 ill, zoneid); 25347 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25348 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 25349 return; 25350 } 25351 case IPPROTO_TCP: { 25352 25353 /* 25354 * For TCP, discard broadcast packets. 25355 */ 25356 if ((ushort_t)ire_type == IRE_BROADCAST) { 25357 freemsg(first_mp); 25358 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 25359 ip2dbg(("ip_wput_local: discard broadcast\n")); 25360 return; 25361 } 25362 25363 if (mp->b_datap->db_type == M_DATA) { 25364 /* 25365 * M_DATA mblk, so init mblk (chain) for no struio(). 25366 */ 25367 mblk_t *mp1 = mp; 25368 25369 do 25370 mp1->b_datap->db_struioflag = 0; 25371 while ((mp1 = mp1->b_cont) != NULL); 25372 } 25373 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 25374 <= mp->b_wptr); 25375 ip_fanout_tcp(q, first_mp, ill, ipha, 25376 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25377 IP_FF_SYN_ADDIRE | IP_FF_IPINFO, 25378 mctl_present, B_FALSE, zoneid); 25379 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25380 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 25381 return; 25382 } 25383 case IPPROTO_SCTP: 25384 { 25385 uint32_t ports; 25386 25387 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 25388 ip_fanout_sctp(first_mp, ill, ipha, ports, 25389 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25390 IP_FF_IPINFO, mctl_present, B_FALSE, zoneid); 25391 return; 25392 } 25393 25394 default: 25395 break; 25396 } 25397 /* 25398 * Find a client for some other protocol. We give 25399 * copies to multiple clients, if more than one is 25400 * bound. 25401 */ 25402 ip_fanout_proto(q, first_mp, ill, ipha, 25403 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 25404 mctl_present, B_FALSE, ill, zoneid); 25405 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25406 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 25407 #undef rptr 25408 } 25409 25410 /* 25411 * Update any source route, record route, or timestamp options. 25412 * Check that we are at end of strict source route. 25413 * The options have been sanity checked by ip_wput_options(). 25414 */ 25415 static void 25416 ip_wput_local_options(ipha_t *ipha, ip_stack_t *ipst) 25417 { 25418 ipoptp_t opts; 25419 uchar_t *opt; 25420 uint8_t optval; 25421 uint8_t optlen; 25422 ipaddr_t dst; 25423 uint32_t ts; 25424 ire_t *ire; 25425 timestruc_t now; 25426 25427 ip2dbg(("ip_wput_local_options\n")); 25428 for (optval = ipoptp_first(&opts, ipha); 25429 optval != IPOPT_EOL; 25430 optval = ipoptp_next(&opts)) { 25431 opt = opts.ipoptp_cur; 25432 optlen = opts.ipoptp_len; 25433 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 25434 switch (optval) { 25435 uint32_t off; 25436 case IPOPT_SSRR: 25437 case IPOPT_LSRR: 25438 off = opt[IPOPT_OFFSET]; 25439 off--; 25440 if (optlen < IP_ADDR_LEN || 25441 off > optlen - IP_ADDR_LEN) { 25442 /* End of source route */ 25443 break; 25444 } 25445 /* 25446 * This will only happen if two consecutive entries 25447 * in the source route contains our address or if 25448 * it is a packet with a loose source route which 25449 * reaches us before consuming the whole source route 25450 */ 25451 ip1dbg(("ip_wput_local_options: not end of SR\n")); 25452 if (optval == IPOPT_SSRR) { 25453 return; 25454 } 25455 /* 25456 * Hack: instead of dropping the packet truncate the 25457 * source route to what has been used by filling the 25458 * rest with IPOPT_NOP. 25459 */ 25460 opt[IPOPT_OLEN] = (uint8_t)off; 25461 while (off < optlen) { 25462 opt[off++] = IPOPT_NOP; 25463 } 25464 break; 25465 case IPOPT_RR: 25466 off = opt[IPOPT_OFFSET]; 25467 off--; 25468 if (optlen < IP_ADDR_LEN || 25469 off > optlen - IP_ADDR_LEN) { 25470 /* No more room - ignore */ 25471 ip1dbg(( 25472 "ip_wput_forward_options: end of RR\n")); 25473 break; 25474 } 25475 dst = htonl(INADDR_LOOPBACK); 25476 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25477 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25478 break; 25479 case IPOPT_TS: 25480 /* Insert timestamp if there is romm */ 25481 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25482 case IPOPT_TS_TSONLY: 25483 off = IPOPT_TS_TIMELEN; 25484 break; 25485 case IPOPT_TS_PRESPEC: 25486 case IPOPT_TS_PRESPEC_RFC791: 25487 /* Verify that the address matched */ 25488 off = opt[IPOPT_OFFSET] - 1; 25489 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 25490 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 25491 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 25492 ipst); 25493 if (ire == NULL) { 25494 /* Not for us */ 25495 break; 25496 } 25497 ire_refrele(ire); 25498 /* FALLTHRU */ 25499 case IPOPT_TS_TSANDADDR: 25500 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 25501 break; 25502 default: 25503 /* 25504 * ip_*put_options should have already 25505 * dropped this packet. 25506 */ 25507 cmn_err(CE_PANIC, "ip_wput_local_options: " 25508 "unknown IT - bug in ip_wput_options?\n"); 25509 return; /* Keep "lint" happy */ 25510 } 25511 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 25512 /* Increase overflow counter */ 25513 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 25514 opt[IPOPT_POS_OV_FLG] = (uint8_t) 25515 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 25516 (off << 4); 25517 break; 25518 } 25519 off = opt[IPOPT_OFFSET] - 1; 25520 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25521 case IPOPT_TS_PRESPEC: 25522 case IPOPT_TS_PRESPEC_RFC791: 25523 case IPOPT_TS_TSANDADDR: 25524 dst = htonl(INADDR_LOOPBACK); 25525 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25526 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25527 /* FALLTHRU */ 25528 case IPOPT_TS_TSONLY: 25529 off = opt[IPOPT_OFFSET] - 1; 25530 /* Compute # of milliseconds since midnight */ 25531 gethrestime(&now); 25532 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 25533 now.tv_nsec / (NANOSEC / MILLISEC); 25534 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 25535 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 25536 break; 25537 } 25538 break; 25539 } 25540 } 25541 } 25542 25543 /* 25544 * Send out a multicast packet on interface ipif. 25545 * The sender does not have an conn. 25546 * Caller verifies that this isn't a PHYI_LOOPBACK. 25547 */ 25548 void 25549 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif, zoneid_t zoneid) 25550 { 25551 ipha_t *ipha; 25552 ire_t *ire; 25553 ipaddr_t dst; 25554 mblk_t *first_mp; 25555 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 25556 25557 /* igmp_sendpkt always allocates a ipsec_out_t */ 25558 ASSERT(mp->b_datap->db_type == M_CTL); 25559 ASSERT(!ipif->ipif_isv6); 25560 ASSERT(!(ipif->ipif_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)); 25561 25562 first_mp = mp; 25563 mp = first_mp->b_cont; 25564 ASSERT(mp->b_datap->db_type == M_DATA); 25565 ipha = (ipha_t *)mp->b_rptr; 25566 25567 /* 25568 * Find an IRE which matches the destination and the outgoing 25569 * queue (i.e. the outgoing interface.) 25570 */ 25571 if (ipif->ipif_flags & IPIF_POINTOPOINT) 25572 dst = ipif->ipif_pp_dst_addr; 25573 else 25574 dst = ipha->ipha_dst; 25575 /* 25576 * The source address has already been initialized by the 25577 * caller and hence matching on ILL (MATCH_IRE_ILL) would 25578 * be sufficient rather than MATCH_IRE_IPIF. 25579 * 25580 * This function is used for sending IGMP packets. We need 25581 * to make sure that we send the packet out of the interface 25582 * (ipif->ipif_ill) where we joined the group. This is to 25583 * prevent from switches doing IGMP snooping to send us multicast 25584 * packets for a given group on the interface we have joined. 25585 * If we can't find an ire, igmp_sendpkt has already initialized 25586 * ipsec_out_attach_if so that this will not be load spread in 25587 * ip_newroute_ipif. 25588 */ 25589 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, NULL, 25590 MATCH_IRE_ILL, ipst); 25591 if (!ire) { 25592 /* 25593 * Mark this packet to make it be delivered to 25594 * ip_wput_ire after the new ire has been 25595 * created. 25596 */ 25597 mp->b_prev = NULL; 25598 mp->b_next = NULL; 25599 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC, 25600 zoneid, &zero_info); 25601 return; 25602 } 25603 25604 /* 25605 * Honor the RTF_SETSRC flag; this is the only case 25606 * where we force this addr whatever the current src addr is, 25607 * because this address is set by igmp_sendpkt(), and 25608 * cannot be specified by any user. 25609 */ 25610 if (ire->ire_flags & RTF_SETSRC) { 25611 ipha->ipha_src = ire->ire_src_addr; 25612 } 25613 25614 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE, zoneid); 25615 } 25616 25617 /* 25618 * NOTE : This function does not ire_refrele the ire argument passed in. 25619 * 25620 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 25621 * failure. The nce_fp_mp can vanish any time in the case of IRE_MIPRTUN 25622 * and IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 25623 * the ire_lock to access the nce_fp_mp in this case. 25624 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 25625 * prepending a fastpath message IPQoS processing must precede it, we also set 25626 * the b_band of the fastpath message to that of the mblk returned by IPQoS 25627 * (IPQoS might have set the b_band for CoS marking). 25628 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 25629 * must follow it so that IPQoS can mark the dl_priority field for CoS 25630 * marking, if needed. 25631 */ 25632 static mblk_t * 25633 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, uint32_t ill_index) 25634 { 25635 uint_t hlen; 25636 ipha_t *ipha; 25637 mblk_t *mp1; 25638 boolean_t qos_done = B_FALSE; 25639 uchar_t *ll_hdr; 25640 ip_stack_t *ipst = ire->ire_ipst; 25641 25642 #define rptr ((uchar_t *)ipha) 25643 25644 ipha = (ipha_t *)mp->b_rptr; 25645 hlen = 0; 25646 LOCK_IRE_FP_MP(ire); 25647 if ((mp1 = ire->ire_nce->nce_fp_mp) != NULL) { 25648 ASSERT(DB_TYPE(mp1) == M_DATA); 25649 /* Initiate IPPF processing */ 25650 if ((proc != 0) && IPP_ENABLED(proc, ipst)) { 25651 UNLOCK_IRE_FP_MP(ire); 25652 ip_process(proc, &mp, ill_index); 25653 if (mp == NULL) 25654 return (NULL); 25655 25656 ipha = (ipha_t *)mp->b_rptr; 25657 LOCK_IRE_FP_MP(ire); 25658 if ((mp1 = ire->ire_nce->nce_fp_mp) == NULL) { 25659 qos_done = B_TRUE; 25660 goto no_fp_mp; 25661 } 25662 ASSERT(DB_TYPE(mp1) == M_DATA); 25663 } 25664 hlen = MBLKL(mp1); 25665 /* 25666 * Check if we have enough room to prepend fastpath 25667 * header 25668 */ 25669 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 25670 ll_hdr = rptr - hlen; 25671 bcopy(mp1->b_rptr, ll_hdr, hlen); 25672 /* 25673 * Set the b_rptr to the start of the link layer 25674 * header 25675 */ 25676 mp->b_rptr = ll_hdr; 25677 mp1 = mp; 25678 } else { 25679 mp1 = copyb(mp1); 25680 if (mp1 == NULL) 25681 goto unlock_err; 25682 mp1->b_band = mp->b_band; 25683 mp1->b_cont = mp; 25684 /* 25685 * certain system generated traffic may not 25686 * have cred/label in ip header block. This 25687 * is true even for a labeled system. But for 25688 * labeled traffic, inherit the label in the 25689 * new header. 25690 */ 25691 if (DB_CRED(mp) != NULL) 25692 mblk_setcred(mp1, DB_CRED(mp)); 25693 /* 25694 * XXX disable ICK_VALID and compute checksum 25695 * here; can happen if nce_fp_mp changes and 25696 * it can't be copied now due to insufficient 25697 * space. (unlikely, fp mp can change, but it 25698 * does not increase in length) 25699 */ 25700 } 25701 UNLOCK_IRE_FP_MP(ire); 25702 } else { 25703 no_fp_mp: 25704 mp1 = copyb(ire->ire_nce->nce_res_mp); 25705 if (mp1 == NULL) { 25706 unlock_err: 25707 UNLOCK_IRE_FP_MP(ire); 25708 freemsg(mp); 25709 return (NULL); 25710 } 25711 UNLOCK_IRE_FP_MP(ire); 25712 mp1->b_cont = mp; 25713 /* 25714 * certain system generated traffic may not 25715 * have cred/label in ip header block. This 25716 * is true even for a labeled system. But for 25717 * labeled traffic, inherit the label in the 25718 * new header. 25719 */ 25720 if (DB_CRED(mp) != NULL) 25721 mblk_setcred(mp1, DB_CRED(mp)); 25722 if (!qos_done && (proc != 0) && IPP_ENABLED(proc, ipst)) { 25723 ip_process(proc, &mp1, ill_index); 25724 if (mp1 == NULL) 25725 return (NULL); 25726 } 25727 } 25728 return (mp1); 25729 #undef rptr 25730 } 25731 25732 /* 25733 * Finish the outbound IPsec processing for an IPv6 packet. This function 25734 * is called from ipsec_out_process() if the IPsec packet was processed 25735 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25736 * asynchronously. 25737 */ 25738 void 25739 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 25740 ire_t *ire_arg) 25741 { 25742 in6_addr_t *v6dstp; 25743 ire_t *ire; 25744 mblk_t *mp; 25745 ip6_t *ip6h1; 25746 uint_t ill_index; 25747 ipsec_out_t *io; 25748 boolean_t attach_if, hwaccel; 25749 uint32_t flags = IP6_NO_IPPOLICY; 25750 int match_flags; 25751 zoneid_t zoneid; 25752 boolean_t ill_need_rele = B_FALSE; 25753 boolean_t ire_need_rele = B_FALSE; 25754 ip_stack_t *ipst; 25755 25756 mp = ipsec_mp->b_cont; 25757 ip6h1 = (ip6_t *)mp->b_rptr; 25758 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25759 ASSERT(io->ipsec_out_ns != NULL); 25760 ipst = io->ipsec_out_ns->netstack_ip; 25761 ill_index = io->ipsec_out_ill_index; 25762 if (io->ipsec_out_reachable) { 25763 flags |= IPV6_REACHABILITY_CONFIRMATION; 25764 } 25765 attach_if = io->ipsec_out_attach_if; 25766 hwaccel = io->ipsec_out_accelerated; 25767 zoneid = io->ipsec_out_zoneid; 25768 ASSERT(zoneid != ALL_ZONES); 25769 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25770 /* Multicast addresses should have non-zero ill_index. */ 25771 v6dstp = &ip6h->ip6_dst; 25772 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 25773 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 25774 ASSERT(!attach_if || ill_index != 0); 25775 if (ill_index != 0) { 25776 if (ill == NULL) { 25777 ill = ip_grab_attach_ill(NULL, ipsec_mp, ill_index, 25778 B_TRUE, ipst); 25779 25780 /* Failure case frees things for us. */ 25781 if (ill == NULL) 25782 return; 25783 25784 ill_need_rele = B_TRUE; 25785 } 25786 /* 25787 * If this packet needs to go out on a particular interface 25788 * honor it. 25789 */ 25790 if (attach_if) { 25791 match_flags = MATCH_IRE_ILL; 25792 25793 /* 25794 * Check if we need an ire that will not be 25795 * looked up by anybody else i.e. HIDDEN. 25796 */ 25797 if (ill_is_probeonly(ill)) { 25798 match_flags |= MATCH_IRE_MARK_HIDDEN; 25799 } 25800 } 25801 } 25802 ASSERT(mp != NULL); 25803 25804 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 25805 boolean_t unspec_src; 25806 ipif_t *ipif; 25807 25808 /* 25809 * Use the ill_index to get the right ill. 25810 */ 25811 unspec_src = io->ipsec_out_unspec_src; 25812 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25813 if (ipif == NULL) { 25814 if (ill_need_rele) 25815 ill_refrele(ill); 25816 freemsg(ipsec_mp); 25817 return; 25818 } 25819 25820 if (ire_arg != NULL) { 25821 ire = ire_arg; 25822 } else { 25823 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25824 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25825 ire_need_rele = B_TRUE; 25826 } 25827 if (ire != NULL) { 25828 ipif_refrele(ipif); 25829 /* 25830 * XXX Do the multicast forwarding now, as the IPSEC 25831 * processing has been done. 25832 */ 25833 goto send; 25834 } 25835 25836 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 25837 mp->b_prev = NULL; 25838 mp->b_next = NULL; 25839 25840 /* 25841 * If the IPsec packet was processed asynchronously, 25842 * drop it now. 25843 */ 25844 if (q == NULL) { 25845 if (ill_need_rele) 25846 ill_refrele(ill); 25847 freemsg(ipsec_mp); 25848 return; 25849 } 25850 25851 ip_newroute_ipif_v6(q, ipsec_mp, ipif, *v6dstp, 25852 unspec_src, zoneid); 25853 ipif_refrele(ipif); 25854 } else { 25855 if (attach_if) { 25856 ipif_t *ipif; 25857 25858 ipif = ipif_get_next_ipif(NULL, ill); 25859 if (ipif == NULL) { 25860 if (ill_need_rele) 25861 ill_refrele(ill); 25862 freemsg(ipsec_mp); 25863 return; 25864 } 25865 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25866 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25867 ire_need_rele = B_TRUE; 25868 ipif_refrele(ipif); 25869 } else { 25870 if (ire_arg != NULL) { 25871 ire = ire_arg; 25872 } else { 25873 ire = ire_cache_lookup_v6(v6dstp, zoneid, NULL, 25874 ipst); 25875 ire_need_rele = B_TRUE; 25876 } 25877 } 25878 if (ire != NULL) 25879 goto send; 25880 /* 25881 * ire disappeared underneath. 25882 * 25883 * What we need to do here is the ip_newroute 25884 * logic to get the ire without doing the IPSEC 25885 * processing. Follow the same old path. But this 25886 * time, ip_wput or ire_add_then_send will call us 25887 * directly as all the IPSEC operations are done. 25888 */ 25889 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 25890 mp->b_prev = NULL; 25891 mp->b_next = NULL; 25892 25893 /* 25894 * If the IPsec packet was processed asynchronously, 25895 * drop it now. 25896 */ 25897 if (q == NULL) { 25898 if (ill_need_rele) 25899 ill_refrele(ill); 25900 freemsg(ipsec_mp); 25901 return; 25902 } 25903 25904 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 25905 zoneid, ipst); 25906 } 25907 if (ill != NULL && ill_need_rele) 25908 ill_refrele(ill); 25909 return; 25910 send: 25911 if (ill != NULL && ill_need_rele) 25912 ill_refrele(ill); 25913 25914 /* Local delivery */ 25915 if (ire->ire_stq == NULL) { 25916 ill_t *out_ill; 25917 ASSERT(q != NULL); 25918 25919 /* PFHooks: LOOPBACK_OUT */ 25920 out_ill = ire->ire_ipif->ipif_ill; 25921 25922 DTRACE_PROBE4(ip6__loopback__out__start, 25923 ill_t *, NULL, ill_t *, out_ill, 25924 ip6_t *, ip6h1, mblk_t *, ipsec_mp); 25925 25926 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 25927 ipst->ips_ipv6firewall_loopback_out, 25928 NULL, out_ill, ip6h1, ipsec_mp, mp, ipst); 25929 25930 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, ipsec_mp); 25931 25932 if (ipsec_mp != NULL) 25933 ip_wput_local_v6(RD(q), out_ill, 25934 ip6h, ipsec_mp, ire, 0); 25935 if (ire_need_rele) 25936 ire_refrele(ire); 25937 return; 25938 } 25939 /* 25940 * Everything is done. Send it out on the wire. 25941 * We force the insertion of a fragment header using the 25942 * IPH_FRAG_HDR flag in two cases: 25943 * - after reception of an ICMPv6 "packet too big" message 25944 * with a MTU < 1280 (cf. RFC 2460 section 5) 25945 * - for multirouted IPv6 packets, so that the receiver can 25946 * discard duplicates according to their fragment identifier 25947 */ 25948 /* XXX fix flow control problems. */ 25949 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 25950 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 25951 if (hwaccel) { 25952 /* 25953 * hardware acceleration does not handle these 25954 * "slow path" cases. 25955 */ 25956 /* IPsec KSTATS: should bump bean counter here. */ 25957 if (ire_need_rele) 25958 ire_refrele(ire); 25959 freemsg(ipsec_mp); 25960 return; 25961 } 25962 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 25963 (mp->b_cont ? msgdsize(mp) : 25964 mp->b_wptr - (uchar_t *)ip6h)) { 25965 /* IPsec KSTATS: should bump bean counter here. */ 25966 ip0dbg(("Packet length mismatch: %d, %ld\n", 25967 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 25968 msgdsize(mp))); 25969 if (ire_need_rele) 25970 ire_refrele(ire); 25971 freemsg(ipsec_mp); 25972 return; 25973 } 25974 ASSERT(mp->b_prev == NULL); 25975 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 25976 ntohs(ip6h->ip6_plen) + 25977 IPV6_HDR_LEN, ire->ire_max_frag)); 25978 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 25979 ire->ire_max_frag); 25980 } else { 25981 UPDATE_OB_PKT_COUNT(ire); 25982 ire->ire_last_used_time = lbolt; 25983 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 25984 } 25985 if (ire_need_rele) 25986 ire_refrele(ire); 25987 freeb(ipsec_mp); 25988 } 25989 25990 void 25991 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 25992 { 25993 mblk_t *hada_mp; /* attributes M_CTL mblk */ 25994 da_ipsec_t *hada; /* data attributes */ 25995 ill_t *ill = (ill_t *)q->q_ptr; 25996 25997 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 25998 25999 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 26000 /* IPsec KSTATS: Bump lose counter here! */ 26001 freemsg(mp); 26002 return; 26003 } 26004 26005 /* 26006 * It's an IPsec packet that must be 26007 * accelerated by the Provider, and the 26008 * outbound ill is IPsec acceleration capable. 26009 * Prepends the mblk with an IPHADA_M_CTL, and ship it 26010 * to the ill. 26011 * IPsec KSTATS: should bump packet counter here. 26012 */ 26013 26014 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 26015 if (hada_mp == NULL) { 26016 /* IPsec KSTATS: should bump packet counter here. */ 26017 freemsg(mp); 26018 return; 26019 } 26020 26021 hada_mp->b_datap->db_type = M_CTL; 26022 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 26023 hada_mp->b_cont = mp; 26024 26025 hada = (da_ipsec_t *)hada_mp->b_rptr; 26026 bzero(hada, sizeof (da_ipsec_t)); 26027 hada->da_type = IPHADA_M_CTL; 26028 26029 putnext(q, hada_mp); 26030 } 26031 26032 /* 26033 * Finish the outbound IPsec processing. This function is called from 26034 * ipsec_out_process() if the IPsec packet was processed 26035 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 26036 * asynchronously. 26037 */ 26038 void 26039 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 26040 ire_t *ire_arg) 26041 { 26042 uint32_t v_hlen_tos_len; 26043 ipaddr_t dst; 26044 ipif_t *ipif = NULL; 26045 ire_t *ire; 26046 ire_t *ire1 = NULL; 26047 mblk_t *next_mp = NULL; 26048 uint32_t max_frag; 26049 boolean_t multirt_send = B_FALSE; 26050 mblk_t *mp; 26051 mblk_t *mp1; 26052 ipha_t *ipha1; 26053 uint_t ill_index; 26054 ipsec_out_t *io; 26055 boolean_t attach_if; 26056 int match_flags, offset; 26057 irb_t *irb = NULL; 26058 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 26059 zoneid_t zoneid; 26060 uint32_t cksum; 26061 uint16_t *up; 26062 ipxmit_state_t pktxmit_state; 26063 ip_stack_t *ipst; 26064 26065 #ifdef _BIG_ENDIAN 26066 #define LENGTH (v_hlen_tos_len & 0xFFFF) 26067 #else 26068 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 26069 #endif 26070 26071 mp = ipsec_mp->b_cont; 26072 ipha1 = (ipha_t *)mp->b_rptr; 26073 ASSERT(mp != NULL); 26074 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 26075 dst = ipha->ipha_dst; 26076 26077 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26078 ill_index = io->ipsec_out_ill_index; 26079 attach_if = io->ipsec_out_attach_if; 26080 zoneid = io->ipsec_out_zoneid; 26081 ASSERT(zoneid != ALL_ZONES); 26082 ipst = io->ipsec_out_ns->netstack_ip; 26083 ASSERT(io->ipsec_out_ns != NULL); 26084 26085 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 26086 if (ill_index != 0) { 26087 if (ill == NULL) { 26088 ill = ip_grab_attach_ill(NULL, ipsec_mp, 26089 ill_index, B_FALSE, ipst); 26090 26091 /* Failure case frees things for us. */ 26092 if (ill == NULL) 26093 return; 26094 26095 ill_need_rele = B_TRUE; 26096 } 26097 /* 26098 * If this packet needs to go out on a particular interface 26099 * honor it. 26100 */ 26101 if (attach_if) { 26102 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 26103 26104 /* 26105 * Check if we need an ire that will not be 26106 * looked up by anybody else i.e. HIDDEN. 26107 */ 26108 if (ill_is_probeonly(ill)) { 26109 match_flags |= MATCH_IRE_MARK_HIDDEN; 26110 } 26111 } 26112 } 26113 26114 if (CLASSD(dst)) { 26115 boolean_t conn_dontroute; 26116 /* 26117 * Use the ill_index to get the right ipif. 26118 */ 26119 conn_dontroute = io->ipsec_out_dontroute; 26120 if (ill_index == 0) 26121 ipif = ipif_lookup_group(dst, zoneid, ipst); 26122 else 26123 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 26124 if (ipif == NULL) { 26125 ip1dbg(("ip_wput_ipsec_out: No ipif for" 26126 " multicast\n")); 26127 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 26128 freemsg(ipsec_mp); 26129 goto done; 26130 } 26131 /* 26132 * ipha_src has already been intialized with the 26133 * value of the ipif in ip_wput. All we need now is 26134 * an ire to send this downstream. 26135 */ 26136 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 26137 MBLK_GETLABEL(mp), match_flags, ipst); 26138 if (ire != NULL) { 26139 ill_t *ill1; 26140 /* 26141 * Do the multicast forwarding now, as the IPSEC 26142 * processing has been done. 26143 */ 26144 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 26145 (ill1 = ire_to_ill(ire))) { 26146 if (ip_mforward(ill1, ipha, mp)) { 26147 freemsg(ipsec_mp); 26148 ip1dbg(("ip_wput_ipsec_out: mforward " 26149 "failed\n")); 26150 ire_refrele(ire); 26151 goto done; 26152 } 26153 } 26154 goto send; 26155 } 26156 26157 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 26158 mp->b_prev = NULL; 26159 mp->b_next = NULL; 26160 26161 /* 26162 * If the IPsec packet was processed asynchronously, 26163 * drop it now. 26164 */ 26165 if (q == NULL) { 26166 freemsg(ipsec_mp); 26167 goto done; 26168 } 26169 26170 /* 26171 * We may be using a wrong ipif to create the ire. 26172 * But it is okay as the source address is assigned 26173 * for the packet already. Next outbound packet would 26174 * create the IRE with the right IPIF in ip_wput. 26175 * 26176 * Also handle RTF_MULTIRT routes. 26177 */ 26178 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT, 26179 zoneid, &zero_info); 26180 } else { 26181 if (attach_if) { 26182 ire = ire_ctable_lookup(dst, 0, 0, ill->ill_ipif, 26183 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 26184 } else { 26185 if (ire_arg != NULL) { 26186 ire = ire_arg; 26187 ire_need_rele = B_FALSE; 26188 } else { 26189 ire = ire_cache_lookup(dst, zoneid, 26190 MBLK_GETLABEL(mp), ipst); 26191 } 26192 } 26193 if (ire != NULL) { 26194 goto send; 26195 } 26196 26197 /* 26198 * ire disappeared underneath. 26199 * 26200 * What we need to do here is the ip_newroute 26201 * logic to get the ire without doing the IPSEC 26202 * processing. Follow the same old path. But this 26203 * time, ip_wput or ire_add_then_put will call us 26204 * directly as all the IPSEC operations are done. 26205 */ 26206 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 26207 mp->b_prev = NULL; 26208 mp->b_next = NULL; 26209 26210 /* 26211 * If the IPsec packet was processed asynchronously, 26212 * drop it now. 26213 */ 26214 if (q == NULL) { 26215 freemsg(ipsec_mp); 26216 goto done; 26217 } 26218 26219 /* 26220 * Since we're going through ip_newroute() again, we 26221 * need to make sure we don't: 26222 * 26223 * 1.) Trigger the ASSERT() with the ipha_ident 26224 * overloading. 26225 * 2.) Redo transport-layer checksumming, since we've 26226 * already done all that to get this far. 26227 * 26228 * The easiest way not do either of the above is to set 26229 * the ipha_ident field to IP_HDR_INCLUDED. 26230 */ 26231 ipha->ipha_ident = IP_HDR_INCLUDED; 26232 ip_newroute(q, ipsec_mp, dst, NULL, 26233 (CONN_Q(q) ? Q_TO_CONN(q) : NULL), zoneid, ipst); 26234 } 26235 goto done; 26236 send: 26237 if (ipha->ipha_protocol == IPPROTO_UDP && 26238 udp_compute_checksum(ipst->ips_netstack)) { 26239 /* 26240 * ESP NAT-Traversal packet. 26241 * 26242 * Just do software checksum for now. 26243 */ 26244 26245 offset = IP_SIMPLE_HDR_LENGTH + UDP_CHECKSUM_OFFSET; 26246 IP_STAT(ipst, ip_out_sw_cksum); 26247 IP_STAT_UPDATE(ipst, ip_udp_out_sw_cksum_bytes, 26248 ntohs(htons(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH)); 26249 #define iphs ((uint16_t *)ipha) 26250 cksum = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 26251 iphs[9] + ntohs(htons(ipha->ipha_length) - 26252 IP_SIMPLE_HDR_LENGTH); 26253 #undef iphs 26254 if ((cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH, cksum)) == 0) 26255 cksum = 0xFFFF; 26256 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) 26257 if (mp1->b_wptr - mp1->b_rptr >= 26258 offset + sizeof (uint16_t)) { 26259 up = (uint16_t *)(mp1->b_rptr + offset); 26260 *up = cksum; 26261 break; /* out of for loop */ 26262 } else { 26263 offset -= (mp->b_wptr - mp->b_rptr); 26264 } 26265 } /* Otherwise, just keep the all-zero checksum. */ 26266 26267 if (ire->ire_stq == NULL) { 26268 ill_t *out_ill; 26269 /* 26270 * Loopbacks go through ip_wput_local except for one case. 26271 * We come here if we generate a icmp_frag_needed message 26272 * after IPSEC processing is over. When this function calls 26273 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 26274 * icmp_frag_needed. The message generated comes back here 26275 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 26276 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 26277 * source address as it is usually set in ip_wput_ire. As 26278 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 26279 * and we end up here. We can't enter ip_wput_ire once the 26280 * IPSEC processing is over and hence we need to do it here. 26281 */ 26282 ASSERT(q != NULL); 26283 UPDATE_OB_PKT_COUNT(ire); 26284 ire->ire_last_used_time = lbolt; 26285 if (ipha->ipha_src == 0) 26286 ipha->ipha_src = ire->ire_src_addr; 26287 26288 /* PFHooks: LOOPBACK_OUT */ 26289 out_ill = ire->ire_ipif->ipif_ill; 26290 26291 DTRACE_PROBE4(ip4__loopback__out__start, 26292 ill_t *, NULL, ill_t *, out_ill, 26293 ipha_t *, ipha1, mblk_t *, ipsec_mp); 26294 26295 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 26296 ipst->ips_ipv4firewall_loopback_out, 26297 NULL, out_ill, ipha1, ipsec_mp, mp, ipst); 26298 26299 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp); 26300 26301 if (ipsec_mp != NULL) 26302 ip_wput_local(RD(q), out_ill, 26303 ipha, ipsec_mp, ire, 0, zoneid); 26304 if (ire_need_rele) 26305 ire_refrele(ire); 26306 goto done; 26307 } 26308 26309 if (ire->ire_max_frag < (unsigned int)LENGTH) { 26310 /* 26311 * We are through with IPSEC processing. 26312 * Fragment this and send it on the wire. 26313 */ 26314 if (io->ipsec_out_accelerated) { 26315 /* 26316 * The packet has been accelerated but must 26317 * be fragmented. This should not happen 26318 * since AH and ESP must not accelerate 26319 * packets that need fragmentation, however 26320 * the configuration could have changed 26321 * since the AH or ESP processing. 26322 * Drop packet. 26323 * IPsec KSTATS: bump bean counter here. 26324 */ 26325 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 26326 "fragmented accelerated packet!\n")); 26327 freemsg(ipsec_mp); 26328 } else { 26329 ip_wput_ire_fragmentit(ipsec_mp, ire, zoneid, ipst); 26330 } 26331 if (ire_need_rele) 26332 ire_refrele(ire); 26333 goto done; 26334 } 26335 26336 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 26337 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 26338 (void *)ire->ire_ipif, (void *)ipif)); 26339 26340 /* 26341 * Multiroute the secured packet, unless IPsec really 26342 * requires the packet to go out only through a particular 26343 * interface. 26344 */ 26345 if ((ire->ire_flags & RTF_MULTIRT) && !attach_if) { 26346 ire_t *first_ire; 26347 irb = ire->ire_bucket; 26348 ASSERT(irb != NULL); 26349 /* 26350 * This ire has been looked up as the one that 26351 * goes through the given ipif; 26352 * make sure we do not omit any other multiroute ire 26353 * that may be present in the bucket before this one. 26354 */ 26355 IRB_REFHOLD(irb); 26356 for (first_ire = irb->irb_ire; 26357 first_ire != NULL; 26358 first_ire = first_ire->ire_next) { 26359 if ((first_ire->ire_flags & RTF_MULTIRT) && 26360 (first_ire->ire_addr == ire->ire_addr) && 26361 !(first_ire->ire_marks & 26362 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 26363 break; 26364 } 26365 26366 if ((first_ire != NULL) && (first_ire != ire)) { 26367 /* 26368 * Don't change the ire if the packet must 26369 * be fragmented if sent via this new one. 26370 */ 26371 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 26372 IRE_REFHOLD(first_ire); 26373 if (ire_need_rele) 26374 ire_refrele(ire); 26375 else 26376 ire_need_rele = B_TRUE; 26377 ire = first_ire; 26378 } 26379 } 26380 IRB_REFRELE(irb); 26381 26382 multirt_send = B_TRUE; 26383 max_frag = ire->ire_max_frag; 26384 } else { 26385 if ((ire->ire_flags & RTF_MULTIRT) && attach_if) { 26386 ip1dbg(("ip_wput_ipsec_out: ignoring multirouting " 26387 "flag, attach_if %d\n", attach_if)); 26388 } 26389 } 26390 26391 /* 26392 * In most cases, the emission loop below is entered only once. 26393 * Only in the case where the ire holds the RTF_MULTIRT 26394 * flag, we loop to process all RTF_MULTIRT ires in the 26395 * bucket, and send the packet through all crossed 26396 * RTF_MULTIRT routes. 26397 */ 26398 do { 26399 if (multirt_send) { 26400 /* 26401 * ire1 holds here the next ire to process in the 26402 * bucket. If multirouting is expected, 26403 * any non-RTF_MULTIRT ire that has the 26404 * right destination address is ignored. 26405 */ 26406 ASSERT(irb != NULL); 26407 IRB_REFHOLD(irb); 26408 for (ire1 = ire->ire_next; 26409 ire1 != NULL; 26410 ire1 = ire1->ire_next) { 26411 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 26412 continue; 26413 if (ire1->ire_addr != ire->ire_addr) 26414 continue; 26415 if (ire1->ire_marks & 26416 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 26417 continue; 26418 /* No loopback here */ 26419 if (ire1->ire_stq == NULL) 26420 continue; 26421 /* 26422 * Ensure we do not exceed the MTU 26423 * of the next route. 26424 */ 26425 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 26426 ip_multirt_bad_mtu(ire1, max_frag); 26427 continue; 26428 } 26429 26430 IRE_REFHOLD(ire1); 26431 break; 26432 } 26433 IRB_REFRELE(irb); 26434 if (ire1 != NULL) { 26435 /* 26436 * We are in a multiple send case, need to 26437 * make a copy of the packet. 26438 */ 26439 next_mp = copymsg(ipsec_mp); 26440 if (next_mp == NULL) { 26441 ire_refrele(ire1); 26442 ire1 = NULL; 26443 } 26444 } 26445 } 26446 /* 26447 * Everything is done. Send it out on the wire 26448 * 26449 * ip_xmit_v4 will call ip_wput_attach_llhdr and then 26450 * either send it on the wire or, in the case of 26451 * HW acceleration, call ipsec_hw_putnext. 26452 */ 26453 if (ire->ire_nce && 26454 ire->ire_nce->nce_state != ND_REACHABLE) { 26455 DTRACE_PROBE2(ip__wput__ipsec__bail, 26456 (ire_t *), ire, (mblk_t *), ipsec_mp); 26457 /* 26458 * If ire's link-layer is unresolved (this 26459 * would only happen if the incomplete ire 26460 * was added to cachetable via forwarding path) 26461 * don't bother going to ip_xmit_v4. Just drop the 26462 * packet. 26463 * There is a slight risk here, in that, if we 26464 * have the forwarding path create an incomplete 26465 * IRE, then until the IRE is completed, any 26466 * transmitted IPSEC packets will be dropped 26467 * instead of being queued waiting for resolution. 26468 * 26469 * But the likelihood of a forwarding packet and a wput 26470 * packet sending to the same dst at the same time 26471 * and there not yet be an ARP entry for it is small. 26472 * Furthermore, if this actually happens, it might 26473 * be likely that wput would generate multiple 26474 * packets (and forwarding would also have a train 26475 * of packets) for that destination. If this is 26476 * the case, some of them would have been dropped 26477 * anyway, since ARP only queues a few packets while 26478 * waiting for resolution 26479 * 26480 * NOTE: We should really call ip_xmit_v4, 26481 * and let it queue the packet and send the 26482 * ARP query and have ARP come back thus: 26483 * <ARP> ip_wput->ip_output->ip-wput_nondata-> 26484 * ip_xmit_v4->ip_wput_attach_llhdr + ipsec 26485 * hw accel work. But it's too complex to get 26486 * the IPsec hw acceleration approach to fit 26487 * well with ip_xmit_v4 doing ARP without 26488 * doing IPSEC simplification. For now, we just 26489 * poke ip_xmit_v4 to trigger the arp resolve, so 26490 * that we can continue with the send on the next 26491 * attempt. 26492 * 26493 * XXX THis should be revisited, when 26494 * the IPsec/IP interaction is cleaned up 26495 */ 26496 ip1dbg(("ip_wput_ipsec_out: ire is incomplete" 26497 " - dropping packet\n")); 26498 freemsg(ipsec_mp); 26499 /* 26500 * Call ip_xmit_v4() to trigger ARP query 26501 * in case the nce_state is ND_INITIAL 26502 */ 26503 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 26504 goto drop_pkt; 26505 } 26506 26507 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 26508 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha1, 26509 mblk_t *, mp); 26510 FW_HOOKS(ipst->ips_ip4_physical_out_event, 26511 ipst->ips_ipv4firewall_physical_out, 26512 NULL, ire->ire_ipif->ipif_ill, ipha1, mp, mp, ipst); 26513 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 26514 if (mp == NULL) 26515 goto drop_pkt; 26516 26517 ip1dbg(("ip_wput_ipsec_out: calling ip_xmit_v4\n")); 26518 pktxmit_state = ip_xmit_v4(mp, ire, 26519 (io->ipsec_out_accelerated ? io : NULL), B_FALSE); 26520 26521 if ((pktxmit_state == SEND_FAILED) || 26522 (pktxmit_state == LLHDR_RESLV_FAILED)) { 26523 26524 freeb(ipsec_mp); /* ip_xmit_v4 frees the mp */ 26525 drop_pkt: 26526 BUMP_MIB(((ill_t *)ire->ire_stq->q_ptr)->ill_ip_mib, 26527 ipIfStatsOutDiscards); 26528 if (ire_need_rele) 26529 ire_refrele(ire); 26530 if (ire1 != NULL) { 26531 ire_refrele(ire1); 26532 freemsg(next_mp); 26533 } 26534 goto done; 26535 } 26536 26537 freeb(ipsec_mp); 26538 if (ire_need_rele) 26539 ire_refrele(ire); 26540 26541 if (ire1 != NULL) { 26542 ire = ire1; 26543 ire_need_rele = B_TRUE; 26544 ASSERT(next_mp); 26545 ipsec_mp = next_mp; 26546 mp = ipsec_mp->b_cont; 26547 ire1 = NULL; 26548 next_mp = NULL; 26549 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26550 } else { 26551 multirt_send = B_FALSE; 26552 } 26553 } while (multirt_send); 26554 done: 26555 if (ill != NULL && ill_need_rele) 26556 ill_refrele(ill); 26557 if (ipif != NULL) 26558 ipif_refrele(ipif); 26559 } 26560 26561 /* 26562 * Get the ill corresponding to the specified ire, and compare its 26563 * capabilities with the protocol and algorithms specified by the 26564 * the SA obtained from ipsec_out. If they match, annotate the 26565 * ipsec_out structure to indicate that the packet needs acceleration. 26566 * 26567 * 26568 * A packet is eligible for outbound hardware acceleration if the 26569 * following conditions are satisfied: 26570 * 26571 * 1. the packet will not be fragmented 26572 * 2. the provider supports the algorithm 26573 * 3. there is no pending control message being exchanged 26574 * 4. snoop is not attached 26575 * 5. the destination address is not a broadcast or multicast address. 26576 * 26577 * Rationale: 26578 * - Hardware drivers do not support fragmentation with 26579 * the current interface. 26580 * - snoop, multicast, and broadcast may result in exposure of 26581 * a cleartext datagram. 26582 * We check all five of these conditions here. 26583 * 26584 * XXX would like to nuke "ire_t *" parameter here; problem is that 26585 * IRE is only way to figure out if a v4 address is a broadcast and 26586 * thus ineligible for acceleration... 26587 */ 26588 static void 26589 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 26590 { 26591 ipsec_out_t *io; 26592 mblk_t *data_mp; 26593 uint_t plen, overhead; 26594 ip_stack_t *ipst; 26595 26596 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 26597 return; 26598 26599 if (ill == NULL) 26600 return; 26601 ipst = ill->ill_ipst; 26602 /* 26603 * Destination address is a broadcast or multicast. Punt. 26604 */ 26605 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 26606 IRE_LOCAL))) 26607 return; 26608 26609 data_mp = ipsec_mp->b_cont; 26610 26611 if (ill->ill_isv6) { 26612 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 26613 26614 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 26615 return; 26616 26617 plen = ip6h->ip6_plen; 26618 } else { 26619 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 26620 26621 if (CLASSD(ipha->ipha_dst)) 26622 return; 26623 26624 plen = ipha->ipha_length; 26625 } 26626 /* 26627 * Is there a pending DLPI control message being exchanged 26628 * between IP/IPsec and the DLS Provider? If there is, it 26629 * could be a SADB update, and the state of the DLS Provider 26630 * SADB might not be in sync with the SADB maintained by 26631 * IPsec. To avoid dropping packets or using the wrong keying 26632 * material, we do not accelerate this packet. 26633 */ 26634 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 26635 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26636 "ill_dlpi_pending! don't accelerate packet\n")); 26637 return; 26638 } 26639 26640 /* 26641 * Is the Provider in promiscous mode? If it does, we don't 26642 * accelerate the packet since it will bounce back up to the 26643 * listeners in the clear. 26644 */ 26645 if (ill->ill_promisc_on_phys) { 26646 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26647 "ill in promiscous mode, don't accelerate packet\n")); 26648 return; 26649 } 26650 26651 /* 26652 * Will the packet require fragmentation? 26653 */ 26654 26655 /* 26656 * IPsec ESP note: this is a pessimistic estimate, but the same 26657 * as is used elsewhere. 26658 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 26659 * + 2-byte trailer 26660 */ 26661 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 26662 IPSEC_BASE_ESP_HDR_SIZE(sa); 26663 26664 if ((plen + overhead) > ill->ill_max_mtu) 26665 return; 26666 26667 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26668 26669 /* 26670 * Can the ill accelerate this IPsec protocol and algorithm 26671 * specified by the SA? 26672 */ 26673 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 26674 ill->ill_isv6, sa, ipst->ips_netstack)) { 26675 return; 26676 } 26677 26678 /* 26679 * Tell AH or ESP that the outbound ill is capable of 26680 * accelerating this packet. 26681 */ 26682 io->ipsec_out_is_capab_ill = B_TRUE; 26683 } 26684 26685 /* 26686 * Select which AH & ESP SA's to use (if any) for the outbound packet. 26687 * 26688 * If this function returns B_TRUE, the requested SA's have been filled 26689 * into the ipsec_out_*_sa pointers. 26690 * 26691 * If the function returns B_FALSE, the packet has been "consumed", most 26692 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 26693 * 26694 * The SA references created by the protocol-specific "select" 26695 * function will be released when the ipsec_mp is freed, thanks to the 26696 * ipsec_out_free destructor -- see spd.c. 26697 */ 26698 static boolean_t 26699 ipsec_out_select_sa(mblk_t *ipsec_mp) 26700 { 26701 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 26702 ipsec_out_t *io; 26703 ipsec_policy_t *pp; 26704 ipsec_action_t *ap; 26705 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26706 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26707 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26708 26709 if (!io->ipsec_out_secure) { 26710 /* 26711 * We came here by mistake. 26712 * Don't bother with ipsec processing 26713 * We should "discourage" this path in the future. 26714 */ 26715 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26716 return (B_FALSE); 26717 } 26718 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26719 ASSERT((io->ipsec_out_policy != NULL) || 26720 (io->ipsec_out_act != NULL)); 26721 26722 ASSERT(io->ipsec_out_failed == B_FALSE); 26723 26724 /* 26725 * IPSEC processing has started. 26726 */ 26727 io->ipsec_out_proc_begin = B_TRUE; 26728 ap = io->ipsec_out_act; 26729 if (ap == NULL) { 26730 pp = io->ipsec_out_policy; 26731 ASSERT(pp != NULL); 26732 ap = pp->ipsp_act; 26733 ASSERT(ap != NULL); 26734 } 26735 26736 /* 26737 * We have an action. now, let's select SA's. 26738 * (In the future, we can cache this in the conn_t..) 26739 */ 26740 if (ap->ipa_want_esp) { 26741 if (io->ipsec_out_esp_sa == NULL) { 26742 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 26743 IPPROTO_ESP); 26744 } 26745 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 26746 } 26747 26748 if (ap->ipa_want_ah) { 26749 if (io->ipsec_out_ah_sa == NULL) { 26750 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 26751 IPPROTO_AH); 26752 } 26753 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 26754 /* 26755 * The ESP and AH processing order needs to be preserved 26756 * when both protocols are required (ESP should be applied 26757 * before AH for an outbound packet). Force an ESP ACQUIRE 26758 * when both ESP and AH are required, and an AH ACQUIRE 26759 * is needed. 26760 */ 26761 if (ap->ipa_want_esp && need_ah_acquire) 26762 need_esp_acquire = B_TRUE; 26763 } 26764 26765 /* 26766 * Send an ACQUIRE (extended, regular, or both) if we need one. 26767 * Release SAs that got referenced, but will not be used until we 26768 * acquire _all_ of the SAs we need. 26769 */ 26770 if (need_ah_acquire || need_esp_acquire) { 26771 if (io->ipsec_out_ah_sa != NULL) { 26772 IPSA_REFRELE(io->ipsec_out_ah_sa); 26773 io->ipsec_out_ah_sa = NULL; 26774 } 26775 if (io->ipsec_out_esp_sa != NULL) { 26776 IPSA_REFRELE(io->ipsec_out_esp_sa); 26777 io->ipsec_out_esp_sa = NULL; 26778 } 26779 26780 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 26781 return (B_FALSE); 26782 } 26783 26784 return (B_TRUE); 26785 } 26786 26787 /* 26788 * Process an IPSEC_OUT message and see what you can 26789 * do with it. 26790 * IPQoS Notes: 26791 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 26792 * IPSec. 26793 * XXX would like to nuke ire_t. 26794 * XXX ill_index better be "real" 26795 */ 26796 void 26797 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 26798 { 26799 ipsec_out_t *io; 26800 ipsec_policy_t *pp; 26801 ipsec_action_t *ap; 26802 ipha_t *ipha; 26803 ip6_t *ip6h; 26804 mblk_t *mp; 26805 ill_t *ill; 26806 zoneid_t zoneid; 26807 ipsec_status_t ipsec_rc; 26808 boolean_t ill_need_rele = B_FALSE; 26809 ip_stack_t *ipst; 26810 ipsec_stack_t *ipss; 26811 26812 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26813 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26814 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26815 ipst = io->ipsec_out_ns->netstack_ip; 26816 mp = ipsec_mp->b_cont; 26817 26818 /* 26819 * Initiate IPPF processing. We do it here to account for packets 26820 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 26821 * We can check for ipsec_out_proc_begin even for such packets, as 26822 * they will always be false (asserted below). 26823 */ 26824 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst) && !io->ipsec_out_proc_begin) { 26825 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 26826 io->ipsec_out_ill_index : ill_index); 26827 if (mp == NULL) { 26828 ip2dbg(("ipsec_out_process: packet dropped "\ 26829 "during IPPF processing\n")); 26830 freeb(ipsec_mp); 26831 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26832 return; 26833 } 26834 } 26835 26836 if (!io->ipsec_out_secure) { 26837 /* 26838 * We came here by mistake. 26839 * Don't bother with ipsec processing 26840 * Should "discourage" this path in the future. 26841 */ 26842 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26843 goto done; 26844 } 26845 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26846 ASSERT((io->ipsec_out_policy != NULL) || 26847 (io->ipsec_out_act != NULL)); 26848 ASSERT(io->ipsec_out_failed == B_FALSE); 26849 26850 ipss = ipst->ips_netstack->netstack_ipsec; 26851 if (!ipsec_loaded(ipss)) { 26852 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 26853 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26854 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26855 } else { 26856 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); 26857 } 26858 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 26859 DROPPER(ipss, ipds_ip_ipsec_not_loaded), 26860 &ipss->ipsec_dropper); 26861 return; 26862 } 26863 26864 /* 26865 * IPSEC processing has started. 26866 */ 26867 io->ipsec_out_proc_begin = B_TRUE; 26868 ap = io->ipsec_out_act; 26869 if (ap == NULL) { 26870 pp = io->ipsec_out_policy; 26871 ASSERT(pp != NULL); 26872 ap = pp->ipsp_act; 26873 ASSERT(ap != NULL); 26874 } 26875 26876 /* 26877 * Save the outbound ill index. When the packet comes back 26878 * from IPsec, we make sure the ill hasn't changed or disappeared 26879 * before sending it the accelerated packet. 26880 */ 26881 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 26882 int ifindex; 26883 ill = ire_to_ill(ire); 26884 ifindex = ill->ill_phyint->phyint_ifindex; 26885 io->ipsec_out_capab_ill_index = ifindex; 26886 } 26887 26888 /* 26889 * The order of processing is first insert a IP header if needed. 26890 * Then insert the ESP header and then the AH header. 26891 */ 26892 if ((io->ipsec_out_se_done == B_FALSE) && 26893 (ap->ipa_want_se)) { 26894 /* 26895 * First get the outer IP header before sending 26896 * it to ESP. 26897 */ 26898 ipha_t *oipha, *iipha; 26899 mblk_t *outer_mp, *inner_mp; 26900 26901 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 26902 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 26903 "ipsec_out_process: " 26904 "Self-Encapsulation failed: Out of memory\n"); 26905 freemsg(ipsec_mp); 26906 if (ill != NULL) { 26907 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26908 } else { 26909 BUMP_MIB(&ipst->ips_ip_mib, 26910 ipIfStatsOutDiscards); 26911 } 26912 return; 26913 } 26914 inner_mp = ipsec_mp->b_cont; 26915 ASSERT(inner_mp->b_datap->db_type == M_DATA); 26916 oipha = (ipha_t *)outer_mp->b_rptr; 26917 iipha = (ipha_t *)inner_mp->b_rptr; 26918 *oipha = *iipha; 26919 outer_mp->b_wptr += sizeof (ipha_t); 26920 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 26921 sizeof (ipha_t)); 26922 oipha->ipha_protocol = IPPROTO_ENCAP; 26923 oipha->ipha_version_and_hdr_length = 26924 IP_SIMPLE_HDR_VERSION; 26925 oipha->ipha_hdr_checksum = 0; 26926 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 26927 outer_mp->b_cont = inner_mp; 26928 ipsec_mp->b_cont = outer_mp; 26929 26930 io->ipsec_out_se_done = B_TRUE; 26931 io->ipsec_out_tunnel = B_TRUE; 26932 } 26933 26934 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 26935 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 26936 !ipsec_out_select_sa(ipsec_mp)) 26937 return; 26938 26939 /* 26940 * By now, we know what SA's to use. Toss over to ESP & AH 26941 * to do the heavy lifting. 26942 */ 26943 zoneid = io->ipsec_out_zoneid; 26944 ASSERT(zoneid != ALL_ZONES); 26945 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 26946 ASSERT(io->ipsec_out_esp_sa != NULL); 26947 io->ipsec_out_esp_done = B_TRUE; 26948 /* 26949 * Note that since hw accel can only apply one transform, 26950 * not two, we skip hw accel for ESP if we also have AH 26951 * This is an design limitation of the interface 26952 * which should be revisited. 26953 */ 26954 ASSERT(ire != NULL); 26955 if (io->ipsec_out_ah_sa == NULL) { 26956 ill = (ill_t *)ire->ire_stq->q_ptr; 26957 ipsec_out_is_accelerated(ipsec_mp, 26958 io->ipsec_out_esp_sa, ill, ire); 26959 } 26960 26961 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 26962 switch (ipsec_rc) { 26963 case IPSEC_STATUS_SUCCESS: 26964 break; 26965 case IPSEC_STATUS_FAILED: 26966 if (ill != NULL) { 26967 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26968 } else { 26969 BUMP_MIB(&ipst->ips_ip_mib, 26970 ipIfStatsOutDiscards); 26971 } 26972 /* FALLTHRU */ 26973 case IPSEC_STATUS_PENDING: 26974 return; 26975 } 26976 } 26977 26978 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 26979 ASSERT(io->ipsec_out_ah_sa != NULL); 26980 io->ipsec_out_ah_done = B_TRUE; 26981 if (ire == NULL) { 26982 int idx = io->ipsec_out_capab_ill_index; 26983 ill = ill_lookup_on_ifindex(idx, B_FALSE, 26984 NULL, NULL, NULL, NULL, ipst); 26985 ill_need_rele = B_TRUE; 26986 } else { 26987 ill = (ill_t *)ire->ire_stq->q_ptr; 26988 } 26989 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 26990 ire); 26991 26992 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 26993 switch (ipsec_rc) { 26994 case IPSEC_STATUS_SUCCESS: 26995 break; 26996 case IPSEC_STATUS_FAILED: 26997 if (ill != NULL) { 26998 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26999 } else { 27000 BUMP_MIB(&ipst->ips_ip_mib, 27001 ipIfStatsOutDiscards); 27002 } 27003 /* FALLTHRU */ 27004 case IPSEC_STATUS_PENDING: 27005 if (ill != NULL && ill_need_rele) 27006 ill_refrele(ill); 27007 return; 27008 } 27009 } 27010 /* 27011 * We are done with IPSEC processing. Send it over 27012 * the wire. 27013 */ 27014 done: 27015 mp = ipsec_mp->b_cont; 27016 ipha = (ipha_t *)mp->b_rptr; 27017 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 27018 ip_wput_ipsec_out(q, ipsec_mp, ipha, ill, ire); 27019 } else { 27020 ip6h = (ip6_t *)ipha; 27021 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ill, ire); 27022 } 27023 if (ill != NULL && ill_need_rele) 27024 ill_refrele(ill); 27025 } 27026 27027 /* ARGSUSED */ 27028 void 27029 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 27030 { 27031 opt_restart_t *or; 27032 int err; 27033 conn_t *connp; 27034 27035 ASSERT(CONN_Q(q)); 27036 connp = Q_TO_CONN(q); 27037 27038 ASSERT(first_mp->b_datap->db_type == M_CTL); 27039 or = (opt_restart_t *)first_mp->b_rptr; 27040 /* 27041 * We don't need to pass any credentials here since this is just 27042 * a restart. The credentials are passed in when svr4_optcom_req 27043 * is called the first time (from ip_wput_nondata). 27044 */ 27045 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 27046 err = svr4_optcom_req(q, first_mp, NULL, 27047 &ip_opt_obj); 27048 } else { 27049 ASSERT(or->or_type == T_OPTMGMT_REQ); 27050 err = tpi_optcom_req(q, first_mp, NULL, 27051 &ip_opt_obj); 27052 } 27053 if (err != EINPROGRESS) { 27054 /* operation is done */ 27055 CONN_OPER_PENDING_DONE(connp); 27056 } 27057 } 27058 27059 /* 27060 * ioctls that go through a down/up sequence may need to wait for the down 27061 * to complete. This involves waiting for the ire and ipif refcnts to go down 27062 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 27063 */ 27064 /* ARGSUSED */ 27065 void 27066 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 27067 { 27068 struct iocblk *iocp; 27069 mblk_t *mp1; 27070 ip_ioctl_cmd_t *ipip; 27071 int err; 27072 sin_t *sin; 27073 struct lifreq *lifr; 27074 struct ifreq *ifr; 27075 27076 iocp = (struct iocblk *)mp->b_rptr; 27077 ASSERT(ipsq != NULL); 27078 /* Existence of mp1 verified in ip_wput_nondata */ 27079 mp1 = mp->b_cont->b_cont; 27080 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27081 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 27082 /* 27083 * Special case where ipsq_current_ipif is not set: 27084 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 27085 * ill could also have become part of a ipmp group in the 27086 * process, we are here as were not able to complete the 27087 * operation in ipif_set_values because we could not become 27088 * exclusive on the new ipsq, In such a case ipsq_current_ipif 27089 * will not be set so we need to set it. 27090 */ 27091 ill_t *ill = q->q_ptr; 27092 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd); 27093 } 27094 ASSERT(ipsq->ipsq_current_ipif != NULL); 27095 27096 if (ipip->ipi_cmd_type == IF_CMD) { 27097 /* This a old style SIOC[GS]IF* command */ 27098 ifr = (struct ifreq *)mp1->b_rptr; 27099 sin = (sin_t *)&ifr->ifr_addr; 27100 } else if (ipip->ipi_cmd_type == LIF_CMD) { 27101 /* This a new style SIOC[GS]LIF* command */ 27102 lifr = (struct lifreq *)mp1->b_rptr; 27103 sin = (sin_t *)&lifr->lifr_addr; 27104 } else { 27105 sin = NULL; 27106 } 27107 27108 err = (*ipip->ipi_func_restart)(ipsq->ipsq_current_ipif, sin, q, mp, 27109 ipip, mp1->b_rptr); 27110 27111 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 27112 } 27113 27114 /* 27115 * ioctl processing 27116 * 27117 * ioctl processing starts with ip_sioctl_copyin_setup which looks up 27118 * the ioctl command in the ioctl tables and determines the copyin data size 27119 * from the ioctl property ipi_copyin_size, and does an mi_copyin() of that 27120 * size. 27121 * 27122 * ioctl processing then continues when the M_IOCDATA makes its way down. 27123 * Now the ioctl is looked up again in the ioctl table, and its properties are 27124 * extracted. The associated 'conn' is then refheld till the end of the ioctl 27125 * and the general ioctl processing function ip_process_ioctl is called. 27126 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 27127 * so goes thru the serialization primitive ipsq_try_enter. Then the 27128 * appropriate function to handle the ioctl is called based on the entry in 27129 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 27130 * which also refreleases the 'conn' that was refheld at the start of the 27131 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 27132 * ip_extract_lifreq_cmn extracts the interface name from the lifreq/ifreq 27133 * struct and looks up the ipif. ip_extract_tunreq handles the case of tunnel. 27134 * 27135 * Many exclusive ioctls go thru an internal down up sequence as part of 27136 * the operation. For example an attempt to change the IP address of an 27137 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 27138 * does all the cleanup such as deleting all ires that use this address. 27139 * Then we need to wait till all references to the interface go away. 27140 */ 27141 void 27142 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 27143 { 27144 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 27145 ip_ioctl_cmd_t *ipip = (ip_ioctl_cmd_t *)arg; 27146 cmd_info_t ci; 27147 int err; 27148 boolean_t entered_ipsq = B_FALSE; 27149 27150 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 27151 27152 if (ipip == NULL) 27153 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27154 27155 /* 27156 * SIOCLIFADDIF needs to go thru a special path since the 27157 * ill may not exist yet. This happens in the case of lo0 27158 * which is created using this ioctl. 27159 */ 27160 if (ipip->ipi_cmd == SIOCLIFADDIF) { 27161 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 27162 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27163 return; 27164 } 27165 27166 ci.ci_ipif = NULL; 27167 switch (ipip->ipi_cmd_type) { 27168 case IF_CMD: 27169 case LIF_CMD: 27170 /* 27171 * ioctls that pass in a [l]ifreq appear here. 27172 * ip_extract_lifreq_cmn returns a refheld ipif in 27173 * ci.ci_ipif 27174 */ 27175 err = ip_extract_lifreq_cmn(q, mp, ipip->ipi_cmd_type, 27176 ipip->ipi_flags, &ci, ip_process_ioctl); 27177 if (err != 0) { 27178 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27179 return; 27180 } 27181 ASSERT(ci.ci_ipif != NULL); 27182 break; 27183 27184 case TUN_CMD: 27185 /* 27186 * SIOC[GS]TUNPARAM appear here. ip_extract_tunreq returns 27187 * a refheld ipif in ci.ci_ipif 27188 */ 27189 err = ip_extract_tunreq(q, mp, &ci.ci_ipif, ip_process_ioctl); 27190 if (err != 0) { 27191 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27192 return; 27193 } 27194 ASSERT(ci.ci_ipif != NULL); 27195 break; 27196 27197 case MISC_CMD: 27198 /* 27199 * ioctls that neither pass in [l]ifreq or iftun_req come here 27200 * For eg. SIOCGLIFCONF will appear here. 27201 */ 27202 switch (ipip->ipi_cmd) { 27203 case IF_UNITSEL: 27204 /* ioctl comes down the ill */ 27205 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 27206 ipif_refhold(ci.ci_ipif); 27207 break; 27208 case SIOCGMSFILTER: 27209 case SIOCSMSFILTER: 27210 case SIOCGIPMSFILTER: 27211 case SIOCSIPMSFILTER: 27212 err = ip_extract_msfilter(q, mp, &ci.ci_ipif, 27213 ip_process_ioctl); 27214 if (err != 0) { 27215 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), 27216 NULL); 27217 } 27218 break; 27219 } 27220 err = 0; 27221 ci.ci_sin = NULL; 27222 ci.ci_sin6 = NULL; 27223 ci.ci_lifr = NULL; 27224 break; 27225 } 27226 27227 /* 27228 * If ipsq is non-null, we are already being called exclusively 27229 */ 27230 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 27231 if (!(ipip->ipi_flags & IPI_WR)) { 27232 /* 27233 * A return value of EINPROGRESS means the ioctl is 27234 * either queued and waiting for some reason or has 27235 * already completed. 27236 */ 27237 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 27238 ci.ci_lifr); 27239 if (ci.ci_ipif != NULL) 27240 ipif_refrele(ci.ci_ipif); 27241 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27242 return; 27243 } 27244 27245 ASSERT(ci.ci_ipif != NULL); 27246 27247 if (ipsq == NULL) { 27248 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, 27249 ip_process_ioctl, NEW_OP, B_TRUE); 27250 entered_ipsq = B_TRUE; 27251 } 27252 /* 27253 * Release the ipif so that ipif_down and friends that wait for 27254 * references to go away are not misled about the current ipif_refcnt 27255 * values. We are writer so we can access the ipif even after releasing 27256 * the ipif. 27257 */ 27258 ipif_refrele(ci.ci_ipif); 27259 if (ipsq == NULL) 27260 return; 27261 27262 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd); 27263 27264 /* 27265 * For most set ioctls that come here, this serves as a single point 27266 * where we set the IPIF_CHANGING flag. This ensures that there won't 27267 * be any new references to the ipif. This helps functions that go 27268 * through this path and end up trying to wait for the refcnts 27269 * associated with the ipif to go down to zero. Some exceptions are 27270 * Failover, Failback, and Groupname commands that operate on more than 27271 * just the ci.ci_ipif. These commands internally determine the 27272 * set of ipif's they operate on and set and clear the IPIF_CHANGING 27273 * flags on that set. Another exception is the Removeif command that 27274 * sets the IPIF_CONDEMNED flag internally after identifying the right 27275 * ipif to operate on. 27276 */ 27277 mutex_enter(&(ci.ci_ipif)->ipif_ill->ill_lock); 27278 if (ipip->ipi_cmd != SIOCLIFREMOVEIF && 27279 ipip->ipi_cmd != SIOCLIFFAILOVER && 27280 ipip->ipi_cmd != SIOCLIFFAILBACK && 27281 ipip->ipi_cmd != SIOCSLIFGROUPNAME) 27282 (ci.ci_ipif)->ipif_state_flags |= IPIF_CHANGING; 27283 mutex_exit(&(ci.ci_ipif)->ipif_ill->ill_lock); 27284 27285 /* 27286 * A return value of EINPROGRESS means the ioctl is 27287 * either queued and waiting for some reason or has 27288 * already completed. 27289 */ 27290 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr); 27291 27292 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 27293 27294 if (entered_ipsq) 27295 ipsq_exit(ipsq, B_TRUE, B_TRUE); 27296 } 27297 27298 /* 27299 * Complete the ioctl. Typically ioctls use the mi package and need to 27300 * do mi_copyout/mi_copy_done. 27301 */ 27302 void 27303 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq) 27304 { 27305 conn_t *connp = NULL; 27306 27307 if (err == EINPROGRESS) 27308 return; 27309 27310 if (CONN_Q(q)) { 27311 connp = Q_TO_CONN(q); 27312 ASSERT(connp->conn_ref >= 2); 27313 } 27314 27315 switch (mode) { 27316 case COPYOUT: 27317 if (err == 0) 27318 mi_copyout(q, mp); 27319 else 27320 mi_copy_done(q, mp, err); 27321 break; 27322 27323 case NO_COPYOUT: 27324 mi_copy_done(q, mp, err); 27325 break; 27326 27327 default: 27328 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */ 27329 break; 27330 } 27331 27332 /* 27333 * The refhold placed at the start of the ioctl is released here. 27334 */ 27335 if (connp != NULL) 27336 CONN_OPER_PENDING_DONE(connp); 27337 27338 if (ipsq != NULL) 27339 ipsq_current_finish(ipsq); 27340 } 27341 27342 /* 27343 * This is called from ip_wput_nondata to resume a deferred TCP bind. 27344 */ 27345 /* ARGSUSED */ 27346 void 27347 ip_resume_tcp_bind(void *arg, mblk_t *mp, void *arg2) 27348 { 27349 conn_t *connp = arg; 27350 tcp_t *tcp; 27351 27352 ASSERT(connp != NULL && IPCL_IS_TCP(connp) && connp->conn_tcp != NULL); 27353 tcp = connp->conn_tcp; 27354 27355 if (connp->conn_tcp->tcp_state == TCPS_CLOSED) 27356 freemsg(mp); 27357 else 27358 tcp_rput_other(tcp, mp); 27359 CONN_OPER_PENDING_DONE(connp); 27360 } 27361 27362 /* Called from ip_wput for all non data messages */ 27363 /* ARGSUSED */ 27364 void 27365 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 27366 { 27367 mblk_t *mp1; 27368 ire_t *ire, *fake_ire; 27369 ill_t *ill; 27370 struct iocblk *iocp; 27371 ip_ioctl_cmd_t *ipip; 27372 cred_t *cr; 27373 conn_t *connp; 27374 int cmd, err; 27375 nce_t *nce; 27376 ipif_t *ipif; 27377 ip_stack_t *ipst; 27378 char *proto_str; 27379 27380 if (CONN_Q(q)) { 27381 connp = Q_TO_CONN(q); 27382 ipst = connp->conn_netstack->netstack_ip; 27383 } else { 27384 connp = NULL; 27385 ipst = ILLQ_TO_IPST(q); 27386 } 27387 27388 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 27389 27390 /* Check if it is a queue to /dev/sctp. */ 27391 if (connp != NULL && connp->conn_ulp == IPPROTO_SCTP && 27392 connp->conn_rq == NULL) { 27393 sctp_wput(q, mp); 27394 return; 27395 } 27396 27397 switch (DB_TYPE(mp)) { 27398 case M_IOCTL: 27399 /* 27400 * IOCTL processing begins in ip_sioctl_copyin_setup which 27401 * will arrange to copy in associated control structures. 27402 */ 27403 ip_sioctl_copyin_setup(q, mp); 27404 return; 27405 case M_IOCDATA: 27406 /* 27407 * Ensure that this is associated with one of our trans- 27408 * parent ioctls. If it's not ours, discard it if we're 27409 * running as a driver, or pass it on if we're a module. 27410 */ 27411 iocp = (struct iocblk *)mp->b_rptr; 27412 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27413 if (ipip == NULL) { 27414 if (q->q_next == NULL) { 27415 goto nak; 27416 } else { 27417 putnext(q, mp); 27418 } 27419 return; 27420 } else if ((q->q_next != NULL) && 27421 !(ipip->ipi_flags & IPI_MODOK)) { 27422 /* 27423 * the ioctl is one we recognise, but is not 27424 * consumed by IP as a module, pass M_IOCDATA 27425 * for processing downstream, but only for 27426 * common Streams ioctls. 27427 */ 27428 if (ipip->ipi_flags & IPI_PASS_DOWN) { 27429 putnext(q, mp); 27430 return; 27431 } else { 27432 goto nak; 27433 } 27434 } 27435 27436 /* IOCTL continuation following copyin or copyout. */ 27437 if (mi_copy_state(q, mp, NULL) == -1) { 27438 /* 27439 * The copy operation failed. mi_copy_state already 27440 * cleaned up, so we're out of here. 27441 */ 27442 return; 27443 } 27444 /* 27445 * If we just completed a copy in, we become writer and 27446 * continue processing in ip_sioctl_copyin_done. If it 27447 * was a copy out, we call mi_copyout again. If there is 27448 * nothing more to copy out, it will complete the IOCTL. 27449 */ 27450 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 27451 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 27452 mi_copy_done(q, mp, EPROTO); 27453 return; 27454 } 27455 /* 27456 * Check for cases that need more copying. A return 27457 * value of 0 means a second copyin has been started, 27458 * so we return; a return value of 1 means no more 27459 * copying is needed, so we continue. 27460 */ 27461 cmd = iocp->ioc_cmd; 27462 if ((cmd == SIOCGMSFILTER || cmd == SIOCSMSFILTER || 27463 cmd == SIOCGIPMSFILTER || cmd == SIOCSIPMSFILTER) && 27464 MI_COPY_COUNT(mp) == 1) { 27465 if (ip_copyin_msfilter(q, mp) == 0) 27466 return; 27467 } 27468 /* 27469 * Refhold the conn, till the ioctl completes. This is 27470 * needed in case the ioctl ends up in the pending mp 27471 * list. Every mp in the ill_pending_mp list and 27472 * the ipsq_pending_mp must have a refhold on the conn 27473 * to resume processing. The refhold is released when 27474 * the ioctl completes. (normally or abnormally) 27475 * In all cases ip_ioctl_finish is called to finish 27476 * the ioctl. 27477 */ 27478 if (connp != NULL) { 27479 /* This is not a reentry */ 27480 ASSERT(ipsq == NULL); 27481 CONN_INC_REF(connp); 27482 } else { 27483 if (!(ipip->ipi_flags & IPI_MODOK)) { 27484 mi_copy_done(q, mp, EINVAL); 27485 return; 27486 } 27487 } 27488 27489 ip_process_ioctl(ipsq, q, mp, ipip); 27490 27491 } else { 27492 mi_copyout(q, mp); 27493 } 27494 return; 27495 nak: 27496 iocp->ioc_error = EINVAL; 27497 mp->b_datap->db_type = M_IOCNAK; 27498 iocp->ioc_count = 0; 27499 qreply(q, mp); 27500 return; 27501 27502 case M_IOCNAK: 27503 /* 27504 * The only way we could get here is if a resolver didn't like 27505 * an IOCTL we sent it. This shouldn't happen. 27506 */ 27507 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 27508 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 27509 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 27510 freemsg(mp); 27511 return; 27512 case M_IOCACK: 27513 /* /dev/ip shouldn't see this */ 27514 if (CONN_Q(q)) 27515 goto nak; 27516 27517 /* Finish socket ioctls passed through to ARP. */ 27518 ip_sioctl_iocack(q, mp); 27519 return; 27520 case M_FLUSH: 27521 if (*mp->b_rptr & FLUSHW) 27522 flushq(q, FLUSHALL); 27523 if (q->q_next) { 27524 /* 27525 * M_FLUSH is sent up to IP by some drivers during 27526 * unbind. ip_rput has already replied to it. We are 27527 * here for the M_FLUSH that we originated in IP 27528 * before sending the unbind request to the driver. 27529 * Just free it as we don't queue packets in IP 27530 * on the write side of the device instance. 27531 */ 27532 freemsg(mp); 27533 return; 27534 } 27535 if (*mp->b_rptr & FLUSHR) { 27536 *mp->b_rptr &= ~FLUSHW; 27537 qreply(q, mp); 27538 return; 27539 } 27540 freemsg(mp); 27541 return; 27542 case IRE_DB_REQ_TYPE: 27543 if (connp == NULL) { 27544 proto_str = "IRE_DB_REQ_TYPE"; 27545 goto protonak; 27546 } 27547 /* An Upper Level Protocol wants a copy of an IRE. */ 27548 ip_ire_req(q, mp); 27549 return; 27550 case M_CTL: 27551 if (mp->b_wptr - mp->b_rptr < sizeof (uint32_t)) 27552 break; 27553 27554 if (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == 27555 TUN_HELLO) { 27556 ASSERT(connp != NULL); 27557 connp->conn_flags |= IPCL_IPTUN; 27558 freeb(mp); 27559 return; 27560 } 27561 27562 if (connp != NULL && *(uint32_t *)mp->b_rptr == 27563 IP_ULP_OUT_LABELED) { 27564 out_labeled_t *olp; 27565 27566 if (mp->b_wptr - mp->b_rptr != sizeof (*olp)) 27567 break; 27568 olp = (out_labeled_t *)mp->b_rptr; 27569 connp->conn_ulp_labeled = olp->out_qnext == q; 27570 freemsg(mp); 27571 return; 27572 } 27573 27574 /* M_CTL messages are used by ARP to tell us things. */ 27575 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 27576 break; 27577 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 27578 case AR_ENTRY_SQUERY: 27579 ip_wput_ctl(q, mp); 27580 return; 27581 case AR_CLIENT_NOTIFY: 27582 ip_arp_news(q, mp); 27583 return; 27584 case AR_DLPIOP_DONE: 27585 ASSERT(q->q_next != NULL); 27586 ill = (ill_t *)q->q_ptr; 27587 /* qwriter_ip releases the refhold */ 27588 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 27589 ill_refhold(ill); 27590 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_done, 27591 CUR_OP, B_FALSE); 27592 return; 27593 case AR_ARP_CLOSING: 27594 /* 27595 * ARP (above us) is closing. If no ARP bringup is 27596 * currently pending, ack the message so that ARP 27597 * can complete its close. Also mark ill_arp_closing 27598 * so that new ARP bringups will fail. If any 27599 * ARP bringup is currently in progress, we will 27600 * ack this when the current ARP bringup completes. 27601 */ 27602 ASSERT(q->q_next != NULL); 27603 ill = (ill_t *)q->q_ptr; 27604 mutex_enter(&ill->ill_lock); 27605 ill->ill_arp_closing = 1; 27606 if (!ill->ill_arp_bringup_pending) { 27607 mutex_exit(&ill->ill_lock); 27608 qreply(q, mp); 27609 } else { 27610 mutex_exit(&ill->ill_lock); 27611 freemsg(mp); 27612 } 27613 return; 27614 case AR_ARP_EXTEND: 27615 /* 27616 * The ARP module above us is capable of duplicate 27617 * address detection. Old ATM drivers will not send 27618 * this message. 27619 */ 27620 ASSERT(q->q_next != NULL); 27621 ill = (ill_t *)q->q_ptr; 27622 ill->ill_arp_extend = B_TRUE; 27623 freemsg(mp); 27624 return; 27625 default: 27626 break; 27627 } 27628 break; 27629 case M_PROTO: 27630 case M_PCPROTO: 27631 /* 27632 * The only PROTO messages we expect are ULP binds and 27633 * copies of option negotiation acknowledgements. 27634 */ 27635 switch (((union T_primitives *)mp->b_rptr)->type) { 27636 case O_T_BIND_REQ: 27637 case T_BIND_REQ: { 27638 /* Request can get queued in bind */ 27639 if (connp == NULL) { 27640 proto_str = "O_T_BIND_REQ/T_BIND_REQ"; 27641 goto protonak; 27642 } 27643 /* 27644 * Both TCP and UDP call ip_bind_{v4,v6}() directly 27645 * instead of going through this path. We only get 27646 * here in the following cases: 27647 * 27648 * a. Bind retries, where ipsq is non-NULL. 27649 * b. T_BIND_REQ is issued from non TCP/UDP 27650 * transport, e.g. icmp for raw socket, 27651 * in which case ipsq will be NULL. 27652 */ 27653 ASSERT(ipsq != NULL || 27654 (!IPCL_IS_TCP(connp) && !IPCL_IS_UDP(connp))); 27655 27656 /* Don't increment refcnt if this is a re-entry */ 27657 if (ipsq == NULL) 27658 CONN_INC_REF(connp); 27659 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 27660 connp, NULL) : ip_bind_v4(q, mp, connp); 27661 if (mp == NULL) 27662 return; 27663 if (IPCL_IS_TCP(connp)) { 27664 /* 27665 * In the case of TCP endpoint we 27666 * come here only for bind retries 27667 */ 27668 ASSERT(ipsq != NULL); 27669 CONN_INC_REF(connp); 27670 squeue_fill(connp->conn_sqp, mp, 27671 ip_resume_tcp_bind, connp, 27672 SQTAG_BIND_RETRY); 27673 return; 27674 } else if (IPCL_IS_UDP(connp)) { 27675 /* 27676 * In the case of UDP endpoint we 27677 * come here only for bind retries 27678 */ 27679 ASSERT(ipsq != NULL); 27680 udp_resume_bind(connp, mp); 27681 return; 27682 } 27683 qreply(q, mp); 27684 CONN_OPER_PENDING_DONE(connp); 27685 return; 27686 } 27687 case T_SVR4_OPTMGMT_REQ: 27688 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 27689 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 27690 27691 if (connp == NULL) { 27692 proto_str = "T_SVR4_OPTMGMT_REQ"; 27693 goto protonak; 27694 } 27695 27696 if (!snmpcom_req(q, mp, ip_snmp_set, 27697 ip_snmp_get, cr)) { 27698 /* 27699 * Call svr4_optcom_req so that it can 27700 * generate the ack. We don't come here 27701 * if this operation is being restarted. 27702 * ip_restart_optmgmt will drop the conn ref. 27703 * In the case of ipsec option after the ipsec 27704 * load is complete conn_restart_ipsec_waiter 27705 * drops the conn ref. 27706 */ 27707 ASSERT(ipsq == NULL); 27708 CONN_INC_REF(connp); 27709 if (ip_check_for_ipsec_opt(q, mp)) 27710 return; 27711 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj); 27712 if (err != EINPROGRESS) { 27713 /* Operation is done */ 27714 CONN_OPER_PENDING_DONE(connp); 27715 } 27716 } 27717 return; 27718 case T_OPTMGMT_REQ: 27719 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 27720 /* 27721 * Note: No snmpcom_req support through new 27722 * T_OPTMGMT_REQ. 27723 * Call tpi_optcom_req so that it can 27724 * generate the ack. 27725 */ 27726 if (connp == NULL) { 27727 proto_str = "T_OPTMGMT_REQ"; 27728 goto protonak; 27729 } 27730 27731 ASSERT(ipsq == NULL); 27732 /* 27733 * We don't come here for restart. ip_restart_optmgmt 27734 * will drop the conn ref. In the case of ipsec option 27735 * after the ipsec load is complete 27736 * conn_restart_ipsec_waiter drops the conn ref. 27737 */ 27738 CONN_INC_REF(connp); 27739 if (ip_check_for_ipsec_opt(q, mp)) 27740 return; 27741 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj); 27742 if (err != EINPROGRESS) { 27743 /* Operation is done */ 27744 CONN_OPER_PENDING_DONE(connp); 27745 } 27746 return; 27747 case T_UNBIND_REQ: 27748 if (connp == NULL) { 27749 proto_str = "T_UNBIND_REQ"; 27750 goto protonak; 27751 } 27752 mp = ip_unbind(q, mp); 27753 qreply(q, mp); 27754 return; 27755 default: 27756 /* 27757 * Have to drop any DLPI messages coming down from 27758 * arp (such as an info_req which would cause ip 27759 * to receive an extra info_ack if it was passed 27760 * through. 27761 */ 27762 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 27763 (int)*(uint_t *)mp->b_rptr)); 27764 freemsg(mp); 27765 return; 27766 } 27767 /* NOTREACHED */ 27768 case IRE_DB_TYPE: { 27769 nce_t *nce; 27770 ill_t *ill; 27771 in6_addr_t gw_addr_v6; 27772 27773 27774 /* 27775 * This is a response back from a resolver. It 27776 * consists of a message chain containing: 27777 * IRE_MBLK-->LL_HDR_MBLK->pkt 27778 * The IRE_MBLK is the one we allocated in ip_newroute. 27779 * The LL_HDR_MBLK is the DLPI header to use to get 27780 * the attached packet, and subsequent ones for the 27781 * same destination, transmitted. 27782 */ 27783 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 27784 break; 27785 /* 27786 * First, check to make sure the resolution succeeded. 27787 * If it failed, the second mblk will be empty. 27788 * If it is, free the chain, dropping the packet. 27789 * (We must ire_delete the ire; that frees the ire mblk) 27790 * We're doing this now to support PVCs for ATM; it's 27791 * a partial xresolv implementation. When we fully implement 27792 * xresolv interfaces, instead of freeing everything here 27793 * we'll initiate neighbor discovery. 27794 * 27795 * For v4 (ARP and other external resolvers) the resolver 27796 * frees the message, so no check is needed. This check 27797 * is required, though, for a full xresolve implementation. 27798 * Including this code here now both shows how external 27799 * resolvers can NACK a resolution request using an 27800 * existing design that has no specific provisions for NACKs, 27801 * and also takes into account that the current non-ARP 27802 * external resolver has been coded to use this method of 27803 * NACKing for all IPv6 (xresolv) cases, 27804 * whether our xresolv implementation is complete or not. 27805 * 27806 */ 27807 ire = (ire_t *)mp->b_rptr; 27808 ill = ire_to_ill(ire); 27809 mp1 = mp->b_cont; /* dl_unitdata_req */ 27810 if (mp1->b_rptr == mp1->b_wptr) { 27811 if (ire->ire_ipversion == IPV6_VERSION) { 27812 /* 27813 * XRESOLV interface. 27814 */ 27815 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27816 mutex_enter(&ire->ire_lock); 27817 gw_addr_v6 = ire->ire_gateway_addr_v6; 27818 mutex_exit(&ire->ire_lock); 27819 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27820 nce = ndp_lookup_v6(ill, 27821 &ire->ire_addr_v6, B_FALSE); 27822 } else { 27823 nce = ndp_lookup_v6(ill, &gw_addr_v6, 27824 B_FALSE); 27825 } 27826 if (nce != NULL) { 27827 nce_resolv_failed(nce); 27828 ndp_delete(nce); 27829 NCE_REFRELE(nce); 27830 } 27831 } 27832 mp->b_cont = NULL; 27833 freemsg(mp1); /* frees the pkt as well */ 27834 ASSERT(ire->ire_nce == NULL); 27835 ire_delete((ire_t *)mp->b_rptr); 27836 return; 27837 } 27838 27839 /* 27840 * Split them into IRE_MBLK and pkt and feed it into 27841 * ire_add_then_send. Then in ire_add_then_send 27842 * the IRE will be added, and then the packet will be 27843 * run back through ip_wput. This time it will make 27844 * it to the wire. 27845 */ 27846 mp->b_cont = NULL; 27847 mp = mp1->b_cont; /* now, mp points to pkt */ 27848 mp1->b_cont = NULL; 27849 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 27850 if (ire->ire_ipversion == IPV6_VERSION) { 27851 /* 27852 * XRESOLV interface. Find the nce and put a copy 27853 * of the dl_unitdata_req in nce_res_mp 27854 */ 27855 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27856 mutex_enter(&ire->ire_lock); 27857 gw_addr_v6 = ire->ire_gateway_addr_v6; 27858 mutex_exit(&ire->ire_lock); 27859 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27860 nce = ndp_lookup_v6(ill, &ire->ire_addr_v6, 27861 B_FALSE); 27862 } else { 27863 nce = ndp_lookup_v6(ill, &gw_addr_v6, B_FALSE); 27864 } 27865 if (nce != NULL) { 27866 /* 27867 * We have to protect nce_res_mp here 27868 * from being accessed by other threads 27869 * while we change the mblk pointer. 27870 * Other functions will also lock the nce when 27871 * accessing nce_res_mp. 27872 * 27873 * The reason we change the mblk pointer 27874 * here rather than copying the resolved address 27875 * into the template is that, unlike with 27876 * ethernet, we have no guarantee that the 27877 * resolved address length will be 27878 * smaller than or equal to the lla length 27879 * with which the template was allocated, 27880 * (for ethernet, they're equal) 27881 * so we have to use the actual resolved 27882 * address mblk - which holds the real 27883 * dl_unitdata_req with the resolved address. 27884 * 27885 * Doing this is the same behavior as was 27886 * previously used in the v4 ARP case. 27887 */ 27888 mutex_enter(&nce->nce_lock); 27889 if (nce->nce_res_mp != NULL) 27890 freemsg(nce->nce_res_mp); 27891 nce->nce_res_mp = mp1; 27892 mutex_exit(&nce->nce_lock); 27893 /* 27894 * We do a fastpath probe here because 27895 * we have resolved the address without 27896 * using Neighbor Discovery. 27897 * In the non-XRESOLV v6 case, the fastpath 27898 * probe is done right after neighbor 27899 * discovery completes. 27900 */ 27901 if (nce->nce_res_mp != NULL) { 27902 int res; 27903 nce_fastpath_list_add(nce); 27904 res = ill_fastpath_probe(ill, 27905 nce->nce_res_mp); 27906 if (res != 0 && res != EAGAIN) 27907 nce_fastpath_list_delete(nce); 27908 } 27909 27910 ire_add_then_send(q, ire, mp); 27911 /* 27912 * Now we have to clean out any packets 27913 * that may have been queued on the nce 27914 * while it was waiting for address resolution 27915 * to complete. 27916 */ 27917 mutex_enter(&nce->nce_lock); 27918 mp1 = nce->nce_qd_mp; 27919 nce->nce_qd_mp = NULL; 27920 mutex_exit(&nce->nce_lock); 27921 while (mp1 != NULL) { 27922 mblk_t *nxt_mp; 27923 queue_t *fwdq = NULL; 27924 ill_t *inbound_ill; 27925 uint_t ifindex; 27926 27927 nxt_mp = mp1->b_next; 27928 mp1->b_next = NULL; 27929 /* 27930 * Retrieve ifindex stored in 27931 * ip_rput_data_v6() 27932 */ 27933 ifindex = 27934 (uint_t)(uintptr_t)mp1->b_prev; 27935 inbound_ill = 27936 ill_lookup_on_ifindex(ifindex, 27937 B_TRUE, NULL, NULL, NULL, 27938 NULL, ipst); 27939 mp1->b_prev = NULL; 27940 if (inbound_ill != NULL) 27941 fwdq = inbound_ill->ill_rq; 27942 27943 if (fwdq != NULL) { 27944 put(fwdq, mp1); 27945 ill_refrele(inbound_ill); 27946 } else 27947 put(WR(ill->ill_rq), mp1); 27948 mp1 = nxt_mp; 27949 } 27950 NCE_REFRELE(nce); 27951 } else { /* nce is NULL; clean up */ 27952 ire_delete(ire); 27953 freemsg(mp); 27954 freemsg(mp1); 27955 return; 27956 } 27957 } else { 27958 nce_t *arpce; 27959 /* 27960 * Link layer resolution succeeded. Recompute the 27961 * ire_nce. 27962 */ 27963 ASSERT(ire->ire_type & (IRE_CACHE|IRE_BROADCAST)); 27964 if ((arpce = ndp_lookup_v4(ill, 27965 (ire->ire_gateway_addr != INADDR_ANY ? 27966 &ire->ire_gateway_addr : &ire->ire_addr), 27967 B_FALSE)) == NULL) { 27968 freeb(ire->ire_mp); 27969 freeb(mp1); 27970 freemsg(mp); 27971 return; 27972 } 27973 mutex_enter(&arpce->nce_lock); 27974 arpce->nce_last = TICK_TO_MSEC(lbolt64); 27975 if (arpce->nce_state == ND_REACHABLE) { 27976 /* 27977 * Someone resolved this before us; 27978 * cleanup the res_mp. Since ire has 27979 * not been added yet, the call to ire_add_v4 27980 * from ire_add_then_send (when a dup is 27981 * detected) will clean up the ire. 27982 */ 27983 freeb(mp1); 27984 } else { 27985 if (arpce->nce_res_mp != NULL) 27986 freemsg(arpce->nce_res_mp); 27987 arpce->nce_res_mp = mp1; 27988 arpce->nce_state = ND_REACHABLE; 27989 } 27990 mutex_exit(&arpce->nce_lock); 27991 if (ire->ire_marks & IRE_MARK_NOADD) { 27992 /* 27993 * this ire will not be added to the ire 27994 * cache table, so we can set the ire_nce 27995 * here, as there are no atomicity constraints. 27996 */ 27997 ire->ire_nce = arpce; 27998 /* 27999 * We are associating this nce with the ire 28000 * so change the nce ref taken in 28001 * ndp_lookup_v4() from 28002 * NCE_REFHOLD to NCE_REFHOLD_NOTR 28003 */ 28004 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 28005 } else { 28006 NCE_REFRELE(arpce); 28007 } 28008 ire_add_then_send(q, ire, mp); 28009 } 28010 return; /* All is well, the packet has been sent. */ 28011 } 28012 case IRE_ARPRESOLVE_TYPE: { 28013 28014 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* fake_ire */ 28015 break; 28016 mp1 = mp->b_cont; /* dl_unitdata_req */ 28017 mp->b_cont = NULL; 28018 /* 28019 * First, check to make sure the resolution succeeded. 28020 * If it failed, the second mblk will be empty. 28021 */ 28022 if (mp1->b_rptr == mp1->b_wptr) { 28023 /* cleanup the incomplete ire, free queued packets */ 28024 freemsg(mp); /* fake ire */ 28025 freeb(mp1); /* dl_unitdata response */ 28026 return; 28027 } 28028 28029 /* 28030 * update any incomplete nce_t found. we lookup the ctable 28031 * and find the nce from the ire->ire_nce because we need 28032 * to pass the ire to ip_xmit_v4 later, and can find both 28033 * ire and nce in one lookup from the ctable. 28034 */ 28035 fake_ire = (ire_t *)mp->b_rptr; 28036 /* 28037 * By the time we come back here from ARP 28038 * the logical outgoing interface of the incomplete ire 28039 * we added in ire_forward could have disappeared, 28040 * causing the incomplete ire to also have 28041 * dissapeared. So we need to retreive the 28042 * proper ipif for the ire before looking 28043 * in ctable; do the ctablelookup based on ire_ipif_seqid 28044 */ 28045 ill = q->q_ptr; 28046 28047 /* Get the outgoing ipif */ 28048 mutex_enter(&ill->ill_lock); 28049 if (ill->ill_state_flags & ILL_CONDEMNED) { 28050 mutex_exit(&ill->ill_lock); 28051 freemsg(mp); /* fake ire */ 28052 freeb(mp1); /* dl_unitdata response */ 28053 return; 28054 } 28055 ipif = ipif_lookup_seqid(ill, fake_ire->ire_ipif_seqid); 28056 28057 if (ipif == NULL) { 28058 mutex_exit(&ill->ill_lock); 28059 ip1dbg(("logical intrf to incomplete ire vanished\n")); 28060 freemsg(mp); 28061 freeb(mp1); 28062 return; 28063 } 28064 ipif_refhold_locked(ipif); 28065 mutex_exit(&ill->ill_lock); 28066 ire = ire_ctable_lookup(fake_ire->ire_addr, 28067 fake_ire->ire_gateway_addr, IRE_CACHE, 28068 ipif, fake_ire->ire_zoneid, NULL, 28069 (MATCH_IRE_GW|MATCH_IRE_IPIF|MATCH_IRE_ZONEONLY), ipst); 28070 ipif_refrele(ipif); 28071 if (ire == NULL) { 28072 /* 28073 * no ire was found; check if there is an nce 28074 * for this lookup; if it has no ire's pointing at it 28075 * cleanup. 28076 */ 28077 if ((nce = ndp_lookup_v4(ill, 28078 (fake_ire->ire_gateway_addr != INADDR_ANY ? 28079 &fake_ire->ire_gateway_addr : &fake_ire->ire_addr), 28080 B_FALSE)) != NULL) { 28081 /* 28082 * cleanup: just reset nce. 28083 * We check for refcnt 2 (one for the nce 28084 * hash list + 1 for the ref taken by 28085 * ndp_lookup_v4) to ensure that there are 28086 * no ire's pointing at the nce. 28087 */ 28088 if (nce->nce_refcnt == 2) { 28089 nce = nce_reinit(nce); 28090 } 28091 if (nce != NULL) 28092 NCE_REFRELE(nce); 28093 } 28094 freeb(mp1); /* dl_unitdata response */ 28095 freemsg(mp); /* fake ire */ 28096 return; 28097 } 28098 nce = ire->ire_nce; 28099 DTRACE_PROBE2(ire__arpresolve__type, 28100 ire_t *, ire, nce_t *, nce); 28101 ASSERT(nce->nce_state != ND_INITIAL); 28102 mutex_enter(&nce->nce_lock); 28103 nce->nce_last = TICK_TO_MSEC(lbolt64); 28104 if (nce->nce_state == ND_REACHABLE) { 28105 /* 28106 * Someone resolved this before us; 28107 * our response is not needed any more. 28108 */ 28109 mutex_exit(&nce->nce_lock); 28110 freeb(mp1); /* dl_unitdata response */ 28111 } else { 28112 if (nce->nce_res_mp != NULL) { 28113 freemsg(nce->nce_res_mp); 28114 /* existing dl_unitdata template */ 28115 } 28116 nce->nce_res_mp = mp1; 28117 nce->nce_state = ND_REACHABLE; 28118 mutex_exit(&nce->nce_lock); 28119 nce_fastpath(nce); 28120 } 28121 /* 28122 * The cached nce_t has been updated to be reachable; 28123 * Set the IRE_MARK_UNCACHED flag and free the fake_ire. 28124 */ 28125 fake_ire->ire_marks &= ~IRE_MARK_UNCACHED; 28126 freemsg(mp); 28127 /* 28128 * send out queued packets. 28129 */ 28130 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 28131 28132 IRE_REFRELE(ire); 28133 return; 28134 } 28135 default: 28136 break; 28137 } 28138 if (q->q_next) { 28139 putnext(q, mp); 28140 } else 28141 freemsg(mp); 28142 return; 28143 28144 protonak: 28145 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str); 28146 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL) 28147 qreply(q, mp); 28148 } 28149 28150 /* 28151 * Process IP options in an outbound packet. Modify the destination if there 28152 * is a source route option. 28153 * Returns non-zero if something fails in which case an ICMP error has been 28154 * sent and mp freed. 28155 */ 28156 static int 28157 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 28158 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 28159 { 28160 ipoptp_t opts; 28161 uchar_t *opt; 28162 uint8_t optval; 28163 uint8_t optlen; 28164 ipaddr_t dst; 28165 intptr_t code = 0; 28166 mblk_t *mp; 28167 ire_t *ire = NULL; 28168 28169 ip2dbg(("ip_wput_options\n")); 28170 mp = ipsec_mp; 28171 if (mctl_present) { 28172 mp = ipsec_mp->b_cont; 28173 } 28174 28175 dst = ipha->ipha_dst; 28176 for (optval = ipoptp_first(&opts, ipha); 28177 optval != IPOPT_EOL; 28178 optval = ipoptp_next(&opts)) { 28179 opt = opts.ipoptp_cur; 28180 optlen = opts.ipoptp_len; 28181 ip2dbg(("ip_wput_options: opt %d, len %d\n", 28182 optval, optlen)); 28183 switch (optval) { 28184 uint32_t off; 28185 case IPOPT_SSRR: 28186 case IPOPT_LSRR: 28187 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28188 ip1dbg(( 28189 "ip_wput_options: bad option offset\n")); 28190 code = (char *)&opt[IPOPT_OLEN] - 28191 (char *)ipha; 28192 goto param_prob; 28193 } 28194 off = opt[IPOPT_OFFSET]; 28195 ip1dbg(("ip_wput_options: next hop 0x%x\n", 28196 ntohl(dst))); 28197 /* 28198 * For strict: verify that dst is directly 28199 * reachable. 28200 */ 28201 if (optval == IPOPT_SSRR) { 28202 ire = ire_ftable_lookup(dst, 0, 0, 28203 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 28204 MBLK_GETLABEL(mp), 28205 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 28206 if (ire == NULL) { 28207 ip1dbg(("ip_wput_options: SSRR not" 28208 " directly reachable: 0x%x\n", 28209 ntohl(dst))); 28210 goto bad_src_route; 28211 } 28212 ire_refrele(ire); 28213 } 28214 break; 28215 case IPOPT_RR: 28216 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28217 ip1dbg(( 28218 "ip_wput_options: bad option offset\n")); 28219 code = (char *)&opt[IPOPT_OLEN] - 28220 (char *)ipha; 28221 goto param_prob; 28222 } 28223 break; 28224 case IPOPT_TS: 28225 /* 28226 * Verify that length >=5 and that there is either 28227 * room for another timestamp or that the overflow 28228 * counter is not maxed out. 28229 */ 28230 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 28231 if (optlen < IPOPT_MINLEN_IT) { 28232 goto param_prob; 28233 } 28234 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28235 ip1dbg(( 28236 "ip_wput_options: bad option offset\n")); 28237 code = (char *)&opt[IPOPT_OFFSET] - 28238 (char *)ipha; 28239 goto param_prob; 28240 } 28241 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 28242 case IPOPT_TS_TSONLY: 28243 off = IPOPT_TS_TIMELEN; 28244 break; 28245 case IPOPT_TS_TSANDADDR: 28246 case IPOPT_TS_PRESPEC: 28247 case IPOPT_TS_PRESPEC_RFC791: 28248 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 28249 break; 28250 default: 28251 code = (char *)&opt[IPOPT_POS_OV_FLG] - 28252 (char *)ipha; 28253 goto param_prob; 28254 } 28255 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 28256 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 28257 /* 28258 * No room and the overflow counter is 15 28259 * already. 28260 */ 28261 goto param_prob; 28262 } 28263 break; 28264 } 28265 } 28266 28267 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 28268 return (0); 28269 28270 ip1dbg(("ip_wput_options: error processing IP options.")); 28271 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 28272 28273 param_prob: 28274 /* 28275 * Since ip_wput() isn't close to finished, we fill 28276 * in enough of the header for credible error reporting. 28277 */ 28278 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28279 /* Failed */ 28280 freemsg(ipsec_mp); 28281 return (-1); 28282 } 28283 icmp_param_problem(q, ipsec_mp, (uint8_t)code, zoneid, ipst); 28284 return (-1); 28285 28286 bad_src_route: 28287 /* 28288 * Since ip_wput() isn't close to finished, we fill 28289 * in enough of the header for credible error reporting. 28290 */ 28291 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28292 /* Failed */ 28293 freemsg(ipsec_mp); 28294 return (-1); 28295 } 28296 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 28297 return (-1); 28298 } 28299 28300 /* 28301 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 28302 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 28303 * thru /etc/system. 28304 */ 28305 #define CONN_MAXDRAINCNT 64 28306 28307 static void 28308 conn_drain_init(ip_stack_t *ipst) 28309 { 28310 int i; 28311 28312 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads; 28313 28314 if ((ipst->ips_conn_drain_list_cnt == 0) || 28315 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 28316 /* 28317 * Default value of the number of drainers is the 28318 * number of cpus, subject to maximum of 8 drainers. 28319 */ 28320 if (boot_max_ncpus != -1) 28321 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 28322 else 28323 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8); 28324 } 28325 28326 ipst->ips_conn_drain_list = kmem_zalloc(ipst->ips_conn_drain_list_cnt * 28327 sizeof (idl_t), KM_SLEEP); 28328 28329 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28330 mutex_init(&ipst->ips_conn_drain_list[i].idl_lock, NULL, 28331 MUTEX_DEFAULT, NULL); 28332 } 28333 } 28334 28335 static void 28336 conn_drain_fini(ip_stack_t *ipst) 28337 { 28338 int i; 28339 28340 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) 28341 mutex_destroy(&ipst->ips_conn_drain_list[i].idl_lock); 28342 kmem_free(ipst->ips_conn_drain_list, 28343 ipst->ips_conn_drain_list_cnt * sizeof (idl_t)); 28344 ipst->ips_conn_drain_list = NULL; 28345 } 28346 28347 /* 28348 * Note: For an overview of how flowcontrol is handled in IP please see the 28349 * IP Flowcontrol notes at the top of this file. 28350 * 28351 * Flow control has blocked us from proceeding. Insert the given conn in one 28352 * of the conn drain lists. These conn wq's will be qenabled later on when 28353 * STREAMS flow control does a backenable. conn_walk_drain will enable 28354 * the first conn in each of these drain lists. Each of these qenabled conns 28355 * in turn enables the next in the list, after it runs, or when it closes, 28356 * thus sustaining the drain process. 28357 * 28358 * The only possible calling sequence is ip_wsrv (on conn) -> ip_wput -> 28359 * conn_drain_insert. Thus there can be only 1 instance of conn_drain_insert 28360 * running at any time, on a given conn, since there can be only 1 service proc 28361 * running on a queue at any time. 28362 */ 28363 void 28364 conn_drain_insert(conn_t *connp) 28365 { 28366 idl_t *idl; 28367 uint_t index; 28368 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28369 28370 mutex_enter(&connp->conn_lock); 28371 if (connp->conn_state_flags & CONN_CLOSING) { 28372 /* 28373 * The conn is closing as a result of which CONN_CLOSING 28374 * is set. Return. 28375 */ 28376 mutex_exit(&connp->conn_lock); 28377 return; 28378 } else if (connp->conn_idl == NULL) { 28379 /* 28380 * Assign the next drain list round robin. We dont' use 28381 * a lock, and thus it may not be strictly round robin. 28382 * Atomicity of load/stores is enough to make sure that 28383 * conn_drain_list_index is always within bounds. 28384 */ 28385 index = ipst->ips_conn_drain_list_index; 28386 ASSERT(index < ipst->ips_conn_drain_list_cnt); 28387 connp->conn_idl = &ipst->ips_conn_drain_list[index]; 28388 index++; 28389 if (index == ipst->ips_conn_drain_list_cnt) 28390 index = 0; 28391 ipst->ips_conn_drain_list_index = index; 28392 } 28393 mutex_exit(&connp->conn_lock); 28394 28395 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28396 if ((connp->conn_drain_prev != NULL) || 28397 (connp->conn_state_flags & CONN_CLOSING)) { 28398 /* 28399 * The conn is already in the drain list, OR 28400 * the conn is closing. We need to check again for 28401 * the closing case again since close can happen 28402 * after we drop the conn_lock, and before we 28403 * acquire the CONN_DRAIN_LIST_LOCK. 28404 */ 28405 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28406 return; 28407 } else { 28408 idl = connp->conn_idl; 28409 } 28410 28411 /* 28412 * The conn is not in the drain list. Insert it at the 28413 * tail of the drain list. The drain list is circular 28414 * and doubly linked. idl_conn points to the 1st element 28415 * in the list. 28416 */ 28417 if (idl->idl_conn == NULL) { 28418 idl->idl_conn = connp; 28419 connp->conn_drain_next = connp; 28420 connp->conn_drain_prev = connp; 28421 } else { 28422 conn_t *head = idl->idl_conn; 28423 28424 connp->conn_drain_next = head; 28425 connp->conn_drain_prev = head->conn_drain_prev; 28426 head->conn_drain_prev->conn_drain_next = connp; 28427 head->conn_drain_prev = connp; 28428 } 28429 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28430 } 28431 28432 /* 28433 * This conn is closing, and we are called from ip_close. OR 28434 * This conn has been serviced by ip_wsrv, and we need to do the tail 28435 * processing. 28436 * If this conn is part of the drain list, we may need to sustain the drain 28437 * process by qenabling the next conn in the drain list. We may also need to 28438 * remove this conn from the list, if it is done. 28439 */ 28440 static void 28441 conn_drain_tail(conn_t *connp, boolean_t closing) 28442 { 28443 idl_t *idl; 28444 28445 /* 28446 * connp->conn_idl is stable at this point, and no lock is needed 28447 * to check it. If we are called from ip_close, close has already 28448 * set CONN_CLOSING, thus freezing the value of conn_idl, and 28449 * called us only because conn_idl is non-null. If we are called thru 28450 * service, conn_idl could be null, but it cannot change because 28451 * service is single-threaded per queue, and there cannot be another 28452 * instance of service trying to call conn_drain_insert on this conn 28453 * now. 28454 */ 28455 ASSERT(!closing || (connp->conn_idl != NULL)); 28456 28457 /* 28458 * If connp->conn_idl is null, the conn has not been inserted into any 28459 * drain list even once since creation of the conn. Just return. 28460 */ 28461 if (connp->conn_idl == NULL) 28462 return; 28463 28464 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28465 28466 if (connp->conn_drain_prev == NULL) { 28467 /* This conn is currently not in the drain list. */ 28468 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28469 return; 28470 } 28471 idl = connp->conn_idl; 28472 if (idl->idl_conn_draining == connp) { 28473 /* 28474 * This conn is the current drainer. If this is the last conn 28475 * in the drain list, we need to do more checks, in the 'if' 28476 * below. Otherwwise we need to just qenable the next conn, 28477 * to sustain the draining, and is handled in the 'else' 28478 * below. 28479 */ 28480 if (connp->conn_drain_next == idl->idl_conn) { 28481 /* 28482 * This conn is the last in this list. This round 28483 * of draining is complete. If idl_repeat is set, 28484 * it means another flow enabling has happened from 28485 * the driver/streams and we need to another round 28486 * of draining. 28487 * If there are more than 2 conns in the drain list, 28488 * do a left rotate by 1, so that all conns except the 28489 * conn at the head move towards the head by 1, and the 28490 * the conn at the head goes to the tail. This attempts 28491 * a more even share for all queues that are being 28492 * drained. 28493 */ 28494 if ((connp->conn_drain_next != connp) && 28495 (idl->idl_conn->conn_drain_next != connp)) { 28496 idl->idl_conn = idl->idl_conn->conn_drain_next; 28497 } 28498 if (idl->idl_repeat) { 28499 qenable(idl->idl_conn->conn_wq); 28500 idl->idl_conn_draining = idl->idl_conn; 28501 idl->idl_repeat = 0; 28502 } else { 28503 idl->idl_conn_draining = NULL; 28504 } 28505 } else { 28506 /* 28507 * If the next queue that we are now qenable'ing, 28508 * is closing, it will remove itself from this list 28509 * and qenable the subsequent queue in ip_close(). 28510 * Serialization is acheived thru idl_lock. 28511 */ 28512 qenable(connp->conn_drain_next->conn_wq); 28513 idl->idl_conn_draining = connp->conn_drain_next; 28514 } 28515 } 28516 if (!connp->conn_did_putbq || closing) { 28517 /* 28518 * Remove ourself from the drain list, if we did not do 28519 * a putbq, or if the conn is closing. 28520 * Note: It is possible that q->q_first is non-null. It means 28521 * that these messages landed after we did a enableok() in 28522 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 28523 * service them. 28524 */ 28525 if (connp->conn_drain_next == connp) { 28526 /* Singleton in the list */ 28527 ASSERT(connp->conn_drain_prev == connp); 28528 idl->idl_conn = NULL; 28529 idl->idl_conn_draining = NULL; 28530 } else { 28531 connp->conn_drain_prev->conn_drain_next = 28532 connp->conn_drain_next; 28533 connp->conn_drain_next->conn_drain_prev = 28534 connp->conn_drain_prev; 28535 if (idl->idl_conn == connp) 28536 idl->idl_conn = connp->conn_drain_next; 28537 ASSERT(idl->idl_conn_draining != connp); 28538 28539 } 28540 connp->conn_drain_next = NULL; 28541 connp->conn_drain_prev = NULL; 28542 } 28543 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28544 } 28545 28546 /* 28547 * Write service routine. Shared perimeter entry point. 28548 * ip_wsrv can be called in any of the following ways. 28549 * 1. The device queue's messages has fallen below the low water mark 28550 * and STREAMS has backenabled the ill_wq. We walk thru all the 28551 * the drain lists and backenable the first conn in each list. 28552 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 28553 * qenabled non-tcp upper layers. We start dequeing messages and call 28554 * ip_wput for each message. 28555 */ 28556 28557 void 28558 ip_wsrv(queue_t *q) 28559 { 28560 conn_t *connp; 28561 ill_t *ill; 28562 mblk_t *mp; 28563 28564 if (q->q_next) { 28565 ill = (ill_t *)q->q_ptr; 28566 if (ill->ill_state_flags == 0) { 28567 /* 28568 * The device flow control has opened up. 28569 * Walk through conn drain lists and qenable the 28570 * first conn in each list. This makes sense only 28571 * if the stream is fully plumbed and setup. 28572 * Hence the if check above. 28573 */ 28574 ip1dbg(("ip_wsrv: walking\n")); 28575 conn_walk_drain(ill->ill_ipst); 28576 } 28577 return; 28578 } 28579 28580 connp = Q_TO_CONN(q); 28581 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 28582 28583 /* 28584 * 1. Set conn_draining flag to signal that service is active. 28585 * 28586 * 2. ip_output determines whether it has been called from service, 28587 * based on the last parameter. If it is IP_WSRV it concludes it 28588 * has been called from service. 28589 * 28590 * 3. Message ordering is preserved by the following logic. 28591 * i. A directly called ip_output (i.e. not thru service) will queue 28592 * the message at the tail, if conn_draining is set (i.e. service 28593 * is running) or if q->q_first is non-null. 28594 * 28595 * ii. If ip_output is called from service, and if ip_output cannot 28596 * putnext due to flow control, it does a putbq. 28597 * 28598 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 28599 * (causing an infinite loop). 28600 */ 28601 ASSERT(!connp->conn_did_putbq); 28602 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 28603 connp->conn_draining = 1; 28604 noenable(q); 28605 while ((mp = getq(q)) != NULL) { 28606 ASSERT(CONN_Q(q)); 28607 28608 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 28609 if (connp->conn_did_putbq) { 28610 /* ip_wput did a putbq */ 28611 break; 28612 } 28613 } 28614 /* 28615 * At this point, a thread coming down from top, calling 28616 * ip_wput, may end up queueing the message. We have not yet 28617 * enabled the queue, so ip_wsrv won't be called again. 28618 * To avoid this race, check q->q_first again (in the loop) 28619 * If the other thread queued the message before we call 28620 * enableok(), we will catch it in the q->q_first check. 28621 * If the other thread queues the message after we call 28622 * enableok(), ip_wsrv will be called again by STREAMS. 28623 */ 28624 connp->conn_draining = 0; 28625 enableok(q); 28626 } 28627 28628 /* Enable the next conn for draining */ 28629 conn_drain_tail(connp, B_FALSE); 28630 28631 connp->conn_did_putbq = 0; 28632 } 28633 28634 /* 28635 * Walk the list of all conn's calling the function provided with the 28636 * specified argument for each. Note that this only walks conn's that 28637 * have been bound. 28638 * Applies to both IPv4 and IPv6. 28639 */ 28640 static void 28641 conn_walk_fanout(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 28642 { 28643 conn_walk_fanout_table(ipst->ips_ipcl_udp_fanout, 28644 ipst->ips_ipcl_udp_fanout_size, 28645 func, arg, zoneid); 28646 conn_walk_fanout_table(ipst->ips_ipcl_conn_fanout, 28647 ipst->ips_ipcl_conn_fanout_size, 28648 func, arg, zoneid); 28649 conn_walk_fanout_table(ipst->ips_ipcl_bind_fanout, 28650 ipst->ips_ipcl_bind_fanout_size, 28651 func, arg, zoneid); 28652 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout, 28653 IPPROTO_MAX, func, arg, zoneid); 28654 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout_v6, 28655 IPPROTO_MAX, func, arg, zoneid); 28656 } 28657 28658 /* 28659 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 28660 * of conns that need to be drained, check if drain is already in progress. 28661 * If so set the idl_repeat bit, indicating that the last conn in the list 28662 * needs to reinitiate the drain once again, for the list. If drain is not 28663 * in progress for the list, initiate the draining, by qenabling the 1st 28664 * conn in the list. The drain is self-sustaining, each qenabled conn will 28665 * in turn qenable the next conn, when it is done/blocked/closing. 28666 */ 28667 static void 28668 conn_walk_drain(ip_stack_t *ipst) 28669 { 28670 int i; 28671 idl_t *idl; 28672 28673 IP_STAT(ipst, ip_conn_walk_drain); 28674 28675 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28676 idl = &ipst->ips_conn_drain_list[i]; 28677 mutex_enter(&idl->idl_lock); 28678 if (idl->idl_conn == NULL) { 28679 mutex_exit(&idl->idl_lock); 28680 continue; 28681 } 28682 /* 28683 * If this list is not being drained currently by 28684 * an ip_wsrv thread, start the process. 28685 */ 28686 if (idl->idl_conn_draining == NULL) { 28687 ASSERT(idl->idl_repeat == 0); 28688 qenable(idl->idl_conn->conn_wq); 28689 idl->idl_conn_draining = idl->idl_conn; 28690 } else { 28691 idl->idl_repeat = 1; 28692 } 28693 mutex_exit(&idl->idl_lock); 28694 } 28695 } 28696 28697 /* 28698 * Walk an conn hash table of `count' buckets, calling func for each entry. 28699 */ 28700 static void 28701 conn_walk_fanout_table(connf_t *connfp, uint_t count, pfv_t func, void *arg, 28702 zoneid_t zoneid) 28703 { 28704 conn_t *connp; 28705 28706 while (count-- > 0) { 28707 mutex_enter(&connfp->connf_lock); 28708 for (connp = connfp->connf_head; connp != NULL; 28709 connp = connp->conn_next) { 28710 if (zoneid == GLOBAL_ZONEID || 28711 zoneid == connp->conn_zoneid) { 28712 CONN_INC_REF(connp); 28713 mutex_exit(&connfp->connf_lock); 28714 (*func)(connp, arg); 28715 mutex_enter(&connfp->connf_lock); 28716 CONN_DEC_REF(connp); 28717 } 28718 } 28719 mutex_exit(&connfp->connf_lock); 28720 connfp++; 28721 } 28722 } 28723 28724 /* ipcl_walk routine invoked for ip_conn_report for each conn. */ 28725 static void 28726 conn_report1(conn_t *connp, void *mp) 28727 { 28728 char buf1[INET6_ADDRSTRLEN]; 28729 char buf2[INET6_ADDRSTRLEN]; 28730 uint_t print_len, buf_len; 28731 28732 ASSERT(connp != NULL); 28733 28734 buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr; 28735 if (buf_len <= 0) 28736 return; 28737 (void) inet_ntop(AF_INET6, &connp->conn_srcv6, buf1, sizeof (buf1)), 28738 (void) inet_ntop(AF_INET6, &connp->conn_remv6, buf2, sizeof (buf2)), 28739 print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len, 28740 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 28741 "%5d %s/%05d %s/%05d\n", 28742 (void *)connp, (void *)CONNP_TO_RQ(connp), 28743 (void *)CONNP_TO_WQ(connp), connp->conn_zoneid, 28744 buf1, connp->conn_lport, 28745 buf2, connp->conn_fport); 28746 if (print_len < buf_len) { 28747 ((mblk_t *)mp)->b_wptr += print_len; 28748 } else { 28749 ((mblk_t *)mp)->b_wptr += buf_len; 28750 } 28751 } 28752 28753 /* 28754 * Named Dispatch routine to produce a formatted report on all conns 28755 * that are listed in one of the fanout tables. 28756 * This report is accessed by using the ndd utility to "get" ND variable 28757 * "ip_conn_status". 28758 */ 28759 /* ARGSUSED */ 28760 static int 28761 ip_conn_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 28762 { 28763 conn_t *connp = Q_TO_CONN(q); 28764 28765 (void) mi_mpprintf(mp, 28766 "CONN " MI_COL_HDRPAD_STR 28767 "rfq " MI_COL_HDRPAD_STR 28768 "stq " MI_COL_HDRPAD_STR 28769 " zone local remote"); 28770 28771 /* 28772 * Because of the ndd constraint, at most we can have 64K buffer 28773 * to put in all conn info. So to be more efficient, just 28774 * allocate a 64K buffer here, assuming we need that large buffer. 28775 * This should be OK as only privileged processes can do ndd /dev/ip. 28776 */ 28777 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 28778 /* The following may work even if we cannot get a large buf. */ 28779 (void) mi_mpprintf(mp, "<< Out of buffer >>\n"); 28780 return (0); 28781 } 28782 28783 conn_walk_fanout(conn_report1, mp->b_cont, connp->conn_zoneid, 28784 connp->conn_netstack->netstack_ip); 28785 return (0); 28786 } 28787 28788 /* 28789 * Determine if the ill and multicast aspects of that packets 28790 * "matches" the conn. 28791 */ 28792 boolean_t 28793 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 28794 zoneid_t zoneid) 28795 { 28796 ill_t *in_ill; 28797 boolean_t found; 28798 ipif_t *ipif; 28799 ire_t *ire; 28800 ipaddr_t dst, src; 28801 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28802 28803 dst = ipha->ipha_dst; 28804 src = ipha->ipha_src; 28805 28806 /* 28807 * conn_incoming_ill is set by IP_BOUND_IF which limits 28808 * unicast, broadcast and multicast reception to 28809 * conn_incoming_ill. conn_wantpacket itself is called 28810 * only for BROADCAST and multicast. 28811 * 28812 * 1) ip_rput supresses duplicate broadcasts if the ill 28813 * is part of a group. Hence, we should be receiving 28814 * just one copy of broadcast for the whole group. 28815 * Thus, if it is part of the group the packet could 28816 * come on any ill of the group and hence we need a 28817 * match on the group. Otherwise, match on ill should 28818 * be sufficient. 28819 * 28820 * 2) ip_rput does not suppress duplicate multicast packets. 28821 * If there are two interfaces in a ill group and we have 28822 * 2 applications (conns) joined a multicast group G on 28823 * both the interfaces, ilm_lookup_ill filter in ip_rput 28824 * will give us two packets because we join G on both the 28825 * interfaces rather than nominating just one interface 28826 * for receiving multicast like broadcast above. So, 28827 * we have to call ilg_lookup_ill to filter out duplicate 28828 * copies, if ill is part of a group. 28829 */ 28830 in_ill = connp->conn_incoming_ill; 28831 if (in_ill != NULL) { 28832 if (in_ill->ill_group == NULL) { 28833 if (in_ill != ill) 28834 return (B_FALSE); 28835 } else if (in_ill->ill_group != ill->ill_group) { 28836 return (B_FALSE); 28837 } 28838 } 28839 28840 if (!CLASSD(dst)) { 28841 if (IPCL_ZONE_MATCH(connp, zoneid)) 28842 return (B_TRUE); 28843 /* 28844 * The conn is in a different zone; we need to check that this 28845 * broadcast address is configured in the application's zone and 28846 * on one ill in the group. 28847 */ 28848 ipif = ipif_get_next_ipif(NULL, ill); 28849 if (ipif == NULL) 28850 return (B_FALSE); 28851 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 28852 connp->conn_zoneid, NULL, 28853 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 28854 ipif_refrele(ipif); 28855 if (ire != NULL) { 28856 ire_refrele(ire); 28857 return (B_TRUE); 28858 } else { 28859 return (B_FALSE); 28860 } 28861 } 28862 28863 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 28864 connp->conn_zoneid == zoneid) { 28865 /* 28866 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 28867 * disabled, therefore we don't dispatch the multicast packet to 28868 * the sending zone. 28869 */ 28870 return (B_FALSE); 28871 } 28872 28873 if ((ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) && 28874 connp->conn_zoneid != zoneid) { 28875 /* 28876 * Multicast packet on the loopback interface: we only match 28877 * conns who joined the group in the specified zone. 28878 */ 28879 return (B_FALSE); 28880 } 28881 28882 if (connp->conn_multi_router) { 28883 /* multicast packet and multicast router socket: send up */ 28884 return (B_TRUE); 28885 } 28886 28887 mutex_enter(&connp->conn_lock); 28888 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 28889 mutex_exit(&connp->conn_lock); 28890 return (found); 28891 } 28892 28893 /* 28894 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 28895 */ 28896 /* ARGSUSED */ 28897 static void 28898 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 28899 { 28900 ill_t *ill = (ill_t *)q->q_ptr; 28901 mblk_t *mp1, *mp2; 28902 ipif_t *ipif; 28903 int err = 0; 28904 conn_t *connp = NULL; 28905 ipsq_t *ipsq; 28906 arc_t *arc; 28907 28908 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 28909 28910 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 28911 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 28912 28913 ASSERT(IAM_WRITER_ILL(ill)); 28914 mp2 = mp->b_cont; 28915 mp->b_cont = NULL; 28916 28917 /* 28918 * We have now received the arp bringup completion message 28919 * from ARP. Mark the arp bringup as done. Also if the arp 28920 * stream has already started closing, send up the AR_ARP_CLOSING 28921 * ack now since ARP is waiting in close for this ack. 28922 */ 28923 mutex_enter(&ill->ill_lock); 28924 ill->ill_arp_bringup_pending = 0; 28925 if (ill->ill_arp_closing) { 28926 mutex_exit(&ill->ill_lock); 28927 /* Let's reuse the mp for sending the ack */ 28928 arc = (arc_t *)mp->b_rptr; 28929 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 28930 arc->arc_cmd = AR_ARP_CLOSING; 28931 qreply(q, mp); 28932 } else { 28933 mutex_exit(&ill->ill_lock); 28934 freeb(mp); 28935 } 28936 28937 ipsq = ill->ill_phyint->phyint_ipsq; 28938 ipif = ipsq->ipsq_pending_ipif; 28939 mp1 = ipsq_pending_mp_get(ipsq, &connp); 28940 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 28941 if (mp1 == NULL) { 28942 /* bringup was aborted by the user */ 28943 freemsg(mp2); 28944 return; 28945 } 28946 28947 /* 28948 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we 28949 * must have an associated conn_t. Otherwise, we're bringing this 28950 * interface back up as part of handling an asynchronous event (e.g., 28951 * physical address change). 28952 */ 28953 if (ipsq->ipsq_current_ioctl != 0) { 28954 ASSERT(connp != NULL); 28955 q = CONNP_TO_WQ(connp); 28956 } else { 28957 ASSERT(connp == NULL); 28958 q = ill->ill_rq; 28959 } 28960 28961 /* 28962 * If the DL_BIND_REQ fails, it is noted 28963 * in arc_name_offset. 28964 */ 28965 err = *((int *)mp2->b_rptr); 28966 if (err == 0) { 28967 if (ipif->ipif_isv6) { 28968 if ((err = ipif_up_done_v6(ipif)) != 0) 28969 ip0dbg(("ip_arp_done: init failed\n")); 28970 } else { 28971 if ((err = ipif_up_done(ipif)) != 0) 28972 ip0dbg(("ip_arp_done: init failed\n")); 28973 } 28974 } else { 28975 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 28976 } 28977 28978 freemsg(mp2); 28979 28980 if ((err == 0) && (ill->ill_up_ipifs)) { 28981 err = ill_up_ipifs(ill, q, mp1); 28982 if (err == EINPROGRESS) 28983 return; 28984 } 28985 28986 if (ill->ill_up_ipifs) 28987 ill_group_cleanup(ill); 28988 28989 /* 28990 * The operation must complete without EINPROGRESS since 28991 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp. 28992 * Otherwise, the operation will be stuck forever in the ipsq. 28993 */ 28994 ASSERT(err != EINPROGRESS); 28995 if (ipsq->ipsq_current_ioctl != 0) 28996 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 28997 else 28998 ipsq_current_finish(ipsq); 28999 } 29000 29001 /* Allocate the private structure */ 29002 static int 29003 ip_priv_alloc(void **bufp) 29004 { 29005 void *buf; 29006 29007 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 29008 return (ENOMEM); 29009 29010 *bufp = buf; 29011 return (0); 29012 } 29013 29014 /* Function to delete the private structure */ 29015 void 29016 ip_priv_free(void *buf) 29017 { 29018 ASSERT(buf != NULL); 29019 kmem_free(buf, sizeof (ip_priv_t)); 29020 } 29021 29022 /* 29023 * The entry point for IPPF processing. 29024 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 29025 * routine just returns. 29026 * 29027 * When called, ip_process generates an ipp_packet_t structure 29028 * which holds the state information for this packet and invokes the 29029 * the classifier (via ipp_packet_process). The classification, depending on 29030 * configured filters, results in a list of actions for this packet. Invoking 29031 * an action may cause the packet to be dropped, in which case the resulting 29032 * mblk (*mpp) is NULL. proc indicates the callout position for 29033 * this packet and ill_index is the interface this packet on or will leave 29034 * on (inbound and outbound resp.). 29035 */ 29036 void 29037 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 29038 { 29039 mblk_t *mp; 29040 ip_priv_t *priv; 29041 ipp_action_id_t aid; 29042 int rc = 0; 29043 ipp_packet_t *pp; 29044 #define IP_CLASS "ip" 29045 29046 /* If the classifier is not loaded, return */ 29047 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 29048 return; 29049 } 29050 29051 mp = *mpp; 29052 ASSERT(mp != NULL); 29053 29054 /* Allocate the packet structure */ 29055 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 29056 if (rc != 0) { 29057 *mpp = NULL; 29058 freemsg(mp); 29059 return; 29060 } 29061 29062 /* Allocate the private structure */ 29063 rc = ip_priv_alloc((void **)&priv); 29064 if (rc != 0) { 29065 *mpp = NULL; 29066 freemsg(mp); 29067 ipp_packet_free(pp); 29068 return; 29069 } 29070 priv->proc = proc; 29071 priv->ill_index = ill_index; 29072 ipp_packet_set_private(pp, priv, ip_priv_free); 29073 ipp_packet_set_data(pp, mp); 29074 29075 /* Invoke the classifier */ 29076 rc = ipp_packet_process(&pp); 29077 if (pp != NULL) { 29078 mp = ipp_packet_get_data(pp); 29079 ipp_packet_free(pp); 29080 if (rc != 0) { 29081 freemsg(mp); 29082 *mpp = NULL; 29083 } 29084 } else { 29085 *mpp = NULL; 29086 } 29087 #undef IP_CLASS 29088 } 29089 29090 /* 29091 * Propagate a multicast group membership operation (add/drop) on 29092 * all the interfaces crossed by the related multirt routes. 29093 * The call is considered successful if the operation succeeds 29094 * on at least one interface. 29095 */ 29096 static int 29097 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 29098 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 29099 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 29100 mblk_t *first_mp) 29101 { 29102 ire_t *ire_gw; 29103 irb_t *irb; 29104 int error = 0; 29105 opt_restart_t *or; 29106 ip_stack_t *ipst = ire->ire_ipst; 29107 29108 irb = ire->ire_bucket; 29109 ASSERT(irb != NULL); 29110 29111 ASSERT(DB_TYPE(first_mp) == M_CTL); 29112 29113 or = (opt_restart_t *)first_mp->b_rptr; 29114 IRB_REFHOLD(irb); 29115 for (; ire != NULL; ire = ire->ire_next) { 29116 if ((ire->ire_flags & RTF_MULTIRT) == 0) 29117 continue; 29118 if (ire->ire_addr != group) 29119 continue; 29120 29121 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 29122 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 29123 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE, ipst); 29124 /* No resolver exists for the gateway; skip this ire. */ 29125 if (ire_gw == NULL) 29126 continue; 29127 29128 /* 29129 * This function can return EINPROGRESS. If so the operation 29130 * will be restarted from ip_restart_optmgmt which will 29131 * call ip_opt_set and option processing will restart for 29132 * this option. So we may end up calling 'fn' more than once. 29133 * This requires that 'fn' is idempotent except for the 29134 * return value. The operation is considered a success if 29135 * it succeeds at least once on any one interface. 29136 */ 29137 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 29138 NULL, fmode, src, first_mp); 29139 if (error == 0) 29140 or->or_private = CGTP_MCAST_SUCCESS; 29141 29142 if (ip_debug > 0) { 29143 ulong_t off; 29144 char *ksym; 29145 ksym = kobj_getsymname((uintptr_t)fn, &off); 29146 ip2dbg(("ip_multirt_apply_membership: " 29147 "called %s, multirt group 0x%08x via itf 0x%08x, " 29148 "error %d [success %u]\n", 29149 ksym ? ksym : "?", 29150 ntohl(group), ntohl(ire_gw->ire_src_addr), 29151 error, or->or_private)); 29152 } 29153 29154 ire_refrele(ire_gw); 29155 if (error == EINPROGRESS) { 29156 IRB_REFRELE(irb); 29157 return (error); 29158 } 29159 } 29160 IRB_REFRELE(irb); 29161 /* 29162 * Consider the call as successful if we succeeded on at least 29163 * one interface. Otherwise, return the last encountered error. 29164 */ 29165 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 29166 } 29167 29168 29169 /* 29170 * Issue a warning regarding a route crossing an interface with an 29171 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 29172 * amount of time is logged. 29173 */ 29174 static void 29175 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 29176 { 29177 hrtime_t current = gethrtime(); 29178 char buf[INET_ADDRSTRLEN]; 29179 ip_stack_t *ipst = ire->ire_ipst; 29180 29181 /* Convert interval in ms to hrtime in ns */ 29182 if (ipst->ips_multirt_bad_mtu_last_time + 29183 ((hrtime_t)ipst->ips_ip_multirt_log_interval * (hrtime_t)1000000) <= 29184 current) { 29185 cmn_err(CE_WARN, "ip: ignoring multiroute " 29186 "to %s, incorrect MTU %u (expected %u)\n", 29187 ip_dot_addr(ire->ire_addr, buf), 29188 ire->ire_max_frag, max_frag); 29189 29190 ipst->ips_multirt_bad_mtu_last_time = current; 29191 } 29192 } 29193 29194 29195 /* 29196 * Get the CGTP (multirouting) filtering status. 29197 * If 0, the CGTP hooks are transparent. 29198 */ 29199 /* ARGSUSED */ 29200 static int 29201 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 29202 { 29203 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 29204 ip_stack_t *ipst = CONNQ_TO_IPST(q); 29205 29206 /* 29207 * Only applies to the shared stack since the filter_ops 29208 * do not carry an ip_stack_t or zoneid. 29209 */ 29210 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 29211 return (ENOTSUP); 29212 29213 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 29214 return (0); 29215 } 29216 29217 29218 /* 29219 * Set the CGTP (multirouting) filtering status. 29220 * If the status is changed from active to transparent 29221 * or from transparent to active, forward the new status 29222 * to the filtering module (if loaded). 29223 */ 29224 /* ARGSUSED */ 29225 static int 29226 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 29227 cred_t *ioc_cr) 29228 { 29229 long new_value; 29230 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 29231 ip_stack_t *ipst = CONNQ_TO_IPST(q); 29232 29233 if (secpolicy_net_config(ioc_cr, B_FALSE) != 0) 29234 return (EPERM); 29235 29236 /* 29237 * Only applies to the shared stack since the filter_ops 29238 * do not carry an ip_stack_t or zoneid. 29239 */ 29240 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 29241 return (ENOTSUP); 29242 29243 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 29244 new_value < 0 || new_value > 1) { 29245 return (EINVAL); 29246 } 29247 29248 /* 29249 * Do not enable CGTP filtering - thus preventing the hooks 29250 * from being invoked - if the version number of the 29251 * filtering module hooks does not match. 29252 */ 29253 if ((ip_cgtp_filter_ops != NULL) && 29254 (ip_cgtp_filter_ops->cfo_filter_rev != CGTP_FILTER_REV)) { 29255 cmn_err(CE_WARN, "IP: CGTP filtering version mismatch " 29256 "(module hooks version %d, expecting %d)\n", 29257 ip_cgtp_filter_ops->cfo_filter_rev, 29258 CGTP_FILTER_REV); 29259 return (ENOTSUP); 29260 } 29261 29262 if ((!*ip_cgtp_filter_value) && new_value) { 29263 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 29264 ip_cgtp_filter_ops == NULL ? 29265 " (module not loaded)" : ""); 29266 } 29267 if (*ip_cgtp_filter_value && (!new_value)) { 29268 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 29269 ip_cgtp_filter_ops == NULL ? 29270 " (module not loaded)" : ""); 29271 } 29272 29273 if (ip_cgtp_filter_ops != NULL) { 29274 int res; 29275 29276 res = ip_cgtp_filter_ops->cfo_change_state(new_value); 29277 if (res) 29278 return (res); 29279 } 29280 29281 *ip_cgtp_filter_value = (boolean_t)new_value; 29282 29283 return (0); 29284 } 29285 29286 29287 /* 29288 * Return the expected CGTP hooks version number. 29289 */ 29290 int 29291 ip_cgtp_filter_supported(void) 29292 { 29293 ip_stack_t *ipst; 29294 int ret; 29295 29296 ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip; 29297 if (ipst == NULL) 29298 return (-1); 29299 ret = ip_cgtp_filter_rev; 29300 netstack_rele(ipst->ips_netstack); 29301 return (ret); 29302 } 29303 29304 29305 /* 29306 * CGTP hooks can be registered by directly touching ip_cgtp_filter_ops 29307 * or by invoking this function. In the first case, the version number 29308 * of the registered structure is checked at hooks activation time 29309 * in ip_cgtp_filter_set(). 29310 * 29311 * Only applies to the shared stack since the filter_ops 29312 * do not carry an ip_stack_t or zoneid. 29313 */ 29314 int 29315 ip_cgtp_filter_register(cgtp_filter_ops_t *ops) 29316 { 29317 ip_stack_t *ipst; 29318 29319 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 29320 return (ENOTSUP); 29321 29322 ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip; 29323 if (ipst == NULL) 29324 return (EINVAL); 29325 29326 ip_cgtp_filter_ops = ops; 29327 netstack_rele(ipst->ips_netstack); 29328 return (0); 29329 } 29330 29331 static squeue_func_t 29332 ip_squeue_switch(int val) 29333 { 29334 squeue_func_t rval = squeue_fill; 29335 29336 switch (val) { 29337 case IP_SQUEUE_ENTER_NODRAIN: 29338 rval = squeue_enter_nodrain; 29339 break; 29340 case IP_SQUEUE_ENTER: 29341 rval = squeue_enter; 29342 break; 29343 default: 29344 break; 29345 } 29346 return (rval); 29347 } 29348 29349 /* ARGSUSED */ 29350 static int 29351 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 29352 caddr_t addr, cred_t *cr) 29353 { 29354 int *v = (int *)addr; 29355 long new_value; 29356 29357 if (secpolicy_net_config(cr, B_FALSE) != 0) 29358 return (EPERM); 29359 29360 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29361 return (EINVAL); 29362 29363 ip_input_proc = ip_squeue_switch(new_value); 29364 *v = new_value; 29365 return (0); 29366 } 29367 29368 /* ARGSUSED */ 29369 static int 29370 ip_int_set(queue_t *q, mblk_t *mp, char *value, 29371 caddr_t addr, cred_t *cr) 29372 { 29373 int *v = (int *)addr; 29374 long new_value; 29375 29376 if (secpolicy_net_config(cr, B_FALSE) != 0) 29377 return (EPERM); 29378 29379 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29380 return (EINVAL); 29381 29382 *v = new_value; 29383 return (0); 29384 } 29385 29386 static void * 29387 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp) 29388 { 29389 kstat_t *ksp; 29390 29391 ip_stat_t template = { 29392 { "ipsec_fanout_proto", KSTAT_DATA_UINT64 }, 29393 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 29394 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 29395 { "ip_udp_fanothers", KSTAT_DATA_UINT64 }, 29396 { "ip_udp_fast_path", KSTAT_DATA_UINT64 }, 29397 { "ip_udp_slow_path", KSTAT_DATA_UINT64 }, 29398 { "ip_udp_input_err", KSTAT_DATA_UINT64 }, 29399 { "ip_tcppullup", KSTAT_DATA_UINT64 }, 29400 { "ip_tcpoptions", KSTAT_DATA_UINT64 }, 29401 { "ip_multipkttcp", KSTAT_DATA_UINT64 }, 29402 { "ip_tcp_fast_path", KSTAT_DATA_UINT64 }, 29403 { "ip_tcp_slow_path", KSTAT_DATA_UINT64 }, 29404 { "ip_tcp_input_error", KSTAT_DATA_UINT64 }, 29405 { "ip_db_ref", KSTAT_DATA_UINT64 }, 29406 { "ip_notaligned1", KSTAT_DATA_UINT64 }, 29407 { "ip_notaligned2", KSTAT_DATA_UINT64 }, 29408 { "ip_multimblk3", KSTAT_DATA_UINT64 }, 29409 { "ip_multimblk4", KSTAT_DATA_UINT64 }, 29410 { "ip_ipoptions", KSTAT_DATA_UINT64 }, 29411 { "ip_classify_fail", KSTAT_DATA_UINT64 }, 29412 { "ip_opt", KSTAT_DATA_UINT64 }, 29413 { "ip_udp_rput_local", KSTAT_DATA_UINT64 }, 29414 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 29415 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 29416 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 29417 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 29418 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 29419 { "ip_trash_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 29420 { "ip_trash_ire_reclaim_success", KSTAT_DATA_UINT64 }, 29421 { "ip_ire_arp_timer_expired", KSTAT_DATA_UINT64 }, 29422 { "ip_ire_redirect_timer_expired", KSTAT_DATA_UINT64 }, 29423 { "ip_ire_pmtu_timer_expired", KSTAT_DATA_UINT64 }, 29424 { "ip_input_multi_squeue", KSTAT_DATA_UINT64 }, 29425 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29426 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29427 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29428 { "ip_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29429 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29430 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29431 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29432 { "ip_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29433 { "ip_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 29434 { "ip_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 29435 { "ip_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 29436 { "ip_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 29437 { "ip_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 29438 }; 29439 29440 ksp = kstat_create_netstack("ip", 0, "ipstat", "net", 29441 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 29442 KSTAT_FLAG_VIRTUAL, stackid); 29443 29444 if (ksp == NULL) 29445 return (NULL); 29446 29447 bcopy(&template, ip_statisticsp, sizeof (template)); 29448 ksp->ks_data = (void *)ip_statisticsp; 29449 ksp->ks_private = (void *)(uintptr_t)stackid; 29450 29451 kstat_install(ksp); 29452 return (ksp); 29453 } 29454 29455 static void 29456 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 29457 { 29458 if (ksp != NULL) { 29459 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29460 kstat_delete_netstack(ksp, stackid); 29461 } 29462 } 29463 29464 static void * 29465 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst) 29466 { 29467 kstat_t *ksp; 29468 29469 ip_named_kstat_t template = { 29470 { "forwarding", KSTAT_DATA_UINT32, 0 }, 29471 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 29472 { "inReceives", KSTAT_DATA_UINT64, 0 }, 29473 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 29474 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 29475 { "forwDatagrams", KSTAT_DATA_UINT64, 0 }, 29476 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 29477 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 29478 { "inDelivers", KSTAT_DATA_UINT64, 0 }, 29479 { "outRequests", KSTAT_DATA_UINT64, 0 }, 29480 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 29481 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 29482 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 29483 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 29484 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 29485 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 29486 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 29487 { "fragFails", KSTAT_DATA_UINT32, 0 }, 29488 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 29489 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 29490 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 29491 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 29492 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 29493 { "inErrs", KSTAT_DATA_UINT32, 0 }, 29494 { "noPorts", KSTAT_DATA_UINT32, 0 }, 29495 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 29496 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 29497 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 29498 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 29499 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 29500 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 29501 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 29502 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 29503 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 29504 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 29505 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 29506 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 29507 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 29508 }; 29509 29510 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 29511 NUM_OF_FIELDS(ip_named_kstat_t), 29512 0, stackid); 29513 if (ksp == NULL || ksp->ks_data == NULL) 29514 return (NULL); 29515 29516 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2; 29517 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl; 29518 template.reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29519 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 29520 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 29521 29522 template.netToMediaEntrySize.value.i32 = 29523 sizeof (mib2_ipNetToMediaEntry_t); 29524 29525 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 29526 29527 bcopy(&template, ksp->ks_data, sizeof (template)); 29528 ksp->ks_update = ip_kstat_update; 29529 ksp->ks_private = (void *)(uintptr_t)stackid; 29530 29531 kstat_install(ksp); 29532 return (ksp); 29533 } 29534 29535 static void 29536 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29537 { 29538 if (ksp != NULL) { 29539 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29540 kstat_delete_netstack(ksp, stackid); 29541 } 29542 } 29543 29544 static int 29545 ip_kstat_update(kstat_t *kp, int rw) 29546 { 29547 ip_named_kstat_t *ipkp; 29548 mib2_ipIfStatsEntry_t ipmib; 29549 ill_walk_context_t ctx; 29550 ill_t *ill; 29551 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29552 netstack_t *ns; 29553 ip_stack_t *ipst; 29554 29555 if (kp == NULL || kp->ks_data == NULL) 29556 return (EIO); 29557 29558 if (rw == KSTAT_WRITE) 29559 return (EACCES); 29560 29561 ns = netstack_find_by_stackid(stackid); 29562 if (ns == NULL) 29563 return (-1); 29564 ipst = ns->netstack_ip; 29565 if (ipst == NULL) { 29566 netstack_rele(ns); 29567 return (-1); 29568 } 29569 ipkp = (ip_named_kstat_t *)kp->ks_data; 29570 29571 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib)); 29572 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 29573 ill = ILL_START_WALK_V4(&ctx, ipst); 29574 for (; ill != NULL; ill = ill_next(&ctx, ill)) 29575 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib); 29576 rw_exit(&ipst->ips_ill_g_lock); 29577 29578 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding; 29579 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL; 29580 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives; 29581 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors; 29582 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors; 29583 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams; 29584 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos; 29585 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards; 29586 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers; 29587 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests; 29588 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards; 29589 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes; 29590 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29591 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds; 29592 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs; 29593 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails; 29594 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs; 29595 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails; 29596 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates; 29597 29598 ipkp->routingDiscards.value.ui32 = 0; 29599 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs; 29600 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts; 29601 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs; 29602 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates; 29603 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups; 29604 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits; 29605 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs; 29606 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows; 29607 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows; 29608 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded; 29609 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed; 29610 29611 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion; 29612 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion; 29613 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion; 29614 29615 netstack_rele(ns); 29616 29617 return (0); 29618 } 29619 29620 static void * 29621 icmp_kstat_init(netstackid_t stackid) 29622 { 29623 kstat_t *ksp; 29624 29625 icmp_named_kstat_t template = { 29626 { "inMsgs", KSTAT_DATA_UINT32 }, 29627 { "inErrors", KSTAT_DATA_UINT32 }, 29628 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 29629 { "inTimeExcds", KSTAT_DATA_UINT32 }, 29630 { "inParmProbs", KSTAT_DATA_UINT32 }, 29631 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 29632 { "inRedirects", KSTAT_DATA_UINT32 }, 29633 { "inEchos", KSTAT_DATA_UINT32 }, 29634 { "inEchoReps", KSTAT_DATA_UINT32 }, 29635 { "inTimestamps", KSTAT_DATA_UINT32 }, 29636 { "inTimestampReps", KSTAT_DATA_UINT32 }, 29637 { "inAddrMasks", KSTAT_DATA_UINT32 }, 29638 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 29639 { "outMsgs", KSTAT_DATA_UINT32 }, 29640 { "outErrors", KSTAT_DATA_UINT32 }, 29641 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 29642 { "outTimeExcds", KSTAT_DATA_UINT32 }, 29643 { "outParmProbs", KSTAT_DATA_UINT32 }, 29644 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 29645 { "outRedirects", KSTAT_DATA_UINT32 }, 29646 { "outEchos", KSTAT_DATA_UINT32 }, 29647 { "outEchoReps", KSTAT_DATA_UINT32 }, 29648 { "outTimestamps", KSTAT_DATA_UINT32 }, 29649 { "outTimestampReps", KSTAT_DATA_UINT32 }, 29650 { "outAddrMasks", KSTAT_DATA_UINT32 }, 29651 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 29652 { "inChksumErrs", KSTAT_DATA_UINT32 }, 29653 { "inUnknowns", KSTAT_DATA_UINT32 }, 29654 { "inFragNeeded", KSTAT_DATA_UINT32 }, 29655 { "outFragNeeded", KSTAT_DATA_UINT32 }, 29656 { "outDrops", KSTAT_DATA_UINT32 }, 29657 { "inOverFlows", KSTAT_DATA_UINT32 }, 29658 { "inBadRedirects", KSTAT_DATA_UINT32 }, 29659 }; 29660 29661 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 29662 NUM_OF_FIELDS(icmp_named_kstat_t), 29663 0, stackid); 29664 if (ksp == NULL || ksp->ks_data == NULL) 29665 return (NULL); 29666 29667 bcopy(&template, ksp->ks_data, sizeof (template)); 29668 29669 ksp->ks_update = icmp_kstat_update; 29670 ksp->ks_private = (void *)(uintptr_t)stackid; 29671 29672 kstat_install(ksp); 29673 return (ksp); 29674 } 29675 29676 static void 29677 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29678 { 29679 if (ksp != NULL) { 29680 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29681 kstat_delete_netstack(ksp, stackid); 29682 } 29683 } 29684 29685 static int 29686 icmp_kstat_update(kstat_t *kp, int rw) 29687 { 29688 icmp_named_kstat_t *icmpkp; 29689 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29690 netstack_t *ns; 29691 ip_stack_t *ipst; 29692 29693 if ((kp == NULL) || (kp->ks_data == NULL)) 29694 return (EIO); 29695 29696 if (rw == KSTAT_WRITE) 29697 return (EACCES); 29698 29699 ns = netstack_find_by_stackid(stackid); 29700 if (ns == NULL) 29701 return (-1); 29702 ipst = ns->netstack_ip; 29703 if (ipst == NULL) { 29704 netstack_rele(ns); 29705 return (-1); 29706 } 29707 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 29708 29709 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs; 29710 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors; 29711 icmpkp->inDestUnreachs.value.ui32 = 29712 ipst->ips_icmp_mib.icmpInDestUnreachs; 29713 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds; 29714 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs; 29715 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs; 29716 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects; 29717 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos; 29718 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps; 29719 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps; 29720 icmpkp->inTimestampReps.value.ui32 = 29721 ipst->ips_icmp_mib.icmpInTimestampReps; 29722 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks; 29723 icmpkp->inAddrMaskReps.value.ui32 = 29724 ipst->ips_icmp_mib.icmpInAddrMaskReps; 29725 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs; 29726 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors; 29727 icmpkp->outDestUnreachs.value.ui32 = 29728 ipst->ips_icmp_mib.icmpOutDestUnreachs; 29729 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds; 29730 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs; 29731 icmpkp->outSrcQuenchs.value.ui32 = 29732 ipst->ips_icmp_mib.icmpOutSrcQuenchs; 29733 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects; 29734 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos; 29735 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps; 29736 icmpkp->outTimestamps.value.ui32 = 29737 ipst->ips_icmp_mib.icmpOutTimestamps; 29738 icmpkp->outTimestampReps.value.ui32 = 29739 ipst->ips_icmp_mib.icmpOutTimestampReps; 29740 icmpkp->outAddrMasks.value.ui32 = 29741 ipst->ips_icmp_mib.icmpOutAddrMasks; 29742 icmpkp->outAddrMaskReps.value.ui32 = 29743 ipst->ips_icmp_mib.icmpOutAddrMaskReps; 29744 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs; 29745 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns; 29746 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded; 29747 icmpkp->outFragNeeded.value.ui32 = 29748 ipst->ips_icmp_mib.icmpOutFragNeeded; 29749 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops; 29750 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows; 29751 icmpkp->inBadRedirects.value.ui32 = 29752 ipst->ips_icmp_mib.icmpInBadRedirects; 29753 29754 netstack_rele(ns); 29755 return (0); 29756 } 29757 29758 /* 29759 * This is the fanout function for raw socket opened for SCTP. Note 29760 * that it is called after SCTP checks that there is no socket which 29761 * wants a packet. Then before SCTP handles this out of the blue packet, 29762 * this function is called to see if there is any raw socket for SCTP. 29763 * If there is and it is bound to the correct address, the packet will 29764 * be sent to that socket. Note that only one raw socket can be bound to 29765 * a port. This is assured in ipcl_sctp_hash_insert(); 29766 */ 29767 void 29768 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 29769 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 29770 zoneid_t zoneid) 29771 { 29772 conn_t *connp; 29773 queue_t *rq; 29774 mblk_t *first_mp; 29775 boolean_t secure; 29776 ip6_t *ip6h; 29777 ip_stack_t *ipst = recv_ill->ill_ipst; 29778 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 29779 29780 first_mp = mp; 29781 if (mctl_present) { 29782 mp = first_mp->b_cont; 29783 secure = ipsec_in_is_secure(first_mp); 29784 ASSERT(mp != NULL); 29785 } else { 29786 secure = B_FALSE; 29787 } 29788 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 29789 29790 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha, ipst); 29791 if (connp == NULL) { 29792 sctp_ootb_input(first_mp, recv_ill, zoneid, mctl_present); 29793 return; 29794 } 29795 rq = connp->conn_rq; 29796 if (!canputnext(rq)) { 29797 CONN_DEC_REF(connp); 29798 BUMP_MIB(recv_ill->ill_ip_mib, rawipIfStatsInOverflows); 29799 freemsg(first_mp); 29800 return; 29801 } 29802 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 29803 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || secure) { 29804 first_mp = ipsec_check_inbound_policy(first_mp, connp, 29805 (isv4 ? ipha : NULL), ip6h, mctl_present); 29806 if (first_mp == NULL) { 29807 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 29808 CONN_DEC_REF(connp); 29809 return; 29810 } 29811 } 29812 /* 29813 * We probably should not send M_CTL message up to 29814 * raw socket. 29815 */ 29816 if (mctl_present) 29817 freeb(first_mp); 29818 29819 /* Initiate IPPF processing here if needed. */ 29820 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) || 29821 (!isv4 && IP6_IN_IPP(flags, ipst))) { 29822 ip_process(IPP_LOCAL_IN, &mp, 29823 recv_ill->ill_phyint->phyint_ifindex); 29824 if (mp == NULL) { 29825 CONN_DEC_REF(connp); 29826 return; 29827 } 29828 } 29829 29830 if (connp->conn_recvif || connp->conn_recvslla || 29831 ((connp->conn_ip_recvpktinfo || 29832 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 29833 (flags & IP_FF_IPINFO))) { 29834 int in_flags = 0; 29835 29836 /* 29837 * Since sctp does not support IP_RECVPKTINFO for v4, only pass 29838 * IPF_RECVIF. 29839 */ 29840 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 29841 in_flags = IPF_RECVIF; 29842 } 29843 if (connp->conn_recvslla) { 29844 in_flags |= IPF_RECVSLLA; 29845 } 29846 if (isv4) { 29847 mp = ip_add_info(mp, recv_ill, in_flags, 29848 IPCL_ZONEID(connp), ipst); 29849 } else { 29850 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 29851 if (mp == NULL) { 29852 BUMP_MIB(recv_ill->ill_ip_mib, 29853 ipIfStatsInDiscards); 29854 CONN_DEC_REF(connp); 29855 return; 29856 } 29857 } 29858 } 29859 29860 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 29861 /* 29862 * We are sending the IPSEC_IN message also up. Refer 29863 * to comments above this function. 29864 */ 29865 putnext(rq, mp); 29866 CONN_DEC_REF(connp); 29867 } 29868 29869 #define UPDATE_IP_MIB_OB_COUNTERS(ill, len) \ 29870 { \ 29871 BUMP_MIB((ill)->ill_ip_mib, ipIfStatsHCOutTransmits); \ 29872 UPDATE_MIB((ill)->ill_ip_mib, ipIfStatsHCOutOctets, (len)); \ 29873 } 29874 /* 29875 * This function should be called only if all packet processing 29876 * including fragmentation is complete. Callers of this function 29877 * must set mp->b_prev to one of these values: 29878 * {0, IPP_FWD_OUT, IPP_LOCAL_OUT} 29879 * prior to handing over the mp as first argument to this function. 29880 * 29881 * If the ire passed by caller is incomplete, this function 29882 * queues the packet and if necessary, sends ARP request and bails. 29883 * If the ire passed is fully resolved, we simply prepend 29884 * the link-layer header to the packet, do ipsec hw acceleration 29885 * work if necessary, and send the packet out on the wire. 29886 * 29887 * NOTE: IPSEC will only call this function with fully resolved 29888 * ires if hw acceleration is involved. 29889 * TODO list : 29890 * a Handle M_MULTIDATA so that 29891 * tcp_multisend->tcp_multisend_data can 29892 * call ip_xmit_v4 directly 29893 * b Handle post-ARP work for fragments so that 29894 * ip_wput_frag can call this function. 29895 */ 29896 ipxmit_state_t 29897 ip_xmit_v4(mblk_t *mp, ire_t *ire, ipsec_out_t *io, boolean_t flow_ctl_enabled) 29898 { 29899 nce_t *arpce; 29900 queue_t *q; 29901 int ill_index; 29902 mblk_t *nxt_mp, *first_mp; 29903 boolean_t xmit_drop = B_FALSE; 29904 ip_proc_t proc; 29905 ill_t *out_ill; 29906 int pkt_len; 29907 29908 arpce = ire->ire_nce; 29909 ASSERT(arpce != NULL); 29910 29911 DTRACE_PROBE2(ip__xmit__v4, ire_t *, ire, nce_t *, arpce); 29912 29913 mutex_enter(&arpce->nce_lock); 29914 switch (arpce->nce_state) { 29915 case ND_REACHABLE: 29916 /* If there are other queued packets, queue this packet */ 29917 if (arpce->nce_qd_mp != NULL) { 29918 if (mp != NULL) 29919 nce_queue_mp_common(arpce, mp, B_FALSE); 29920 mp = arpce->nce_qd_mp; 29921 } 29922 arpce->nce_qd_mp = NULL; 29923 mutex_exit(&arpce->nce_lock); 29924 29925 /* 29926 * Flush the queue. In the common case, where the 29927 * ARP is already resolved, it will go through the 29928 * while loop only once. 29929 */ 29930 while (mp != NULL) { 29931 29932 nxt_mp = mp->b_next; 29933 mp->b_next = NULL; 29934 ASSERT(mp->b_datap->db_type != M_CTL); 29935 pkt_len = ntohs(((ipha_t *)mp->b_rptr)->ipha_length); 29936 /* 29937 * This info is needed for IPQOS to do COS marking 29938 * in ip_wput_attach_llhdr->ip_process. 29939 */ 29940 proc = (ip_proc_t)(uintptr_t)mp->b_prev; 29941 mp->b_prev = NULL; 29942 29943 /* set up ill index for outbound qos processing */ 29944 out_ill = ire->ire_ipif->ipif_ill; 29945 ill_index = out_ill->ill_phyint->phyint_ifindex; 29946 first_mp = ip_wput_attach_llhdr(mp, ire, proc, 29947 ill_index); 29948 if (first_mp == NULL) { 29949 xmit_drop = B_TRUE; 29950 BUMP_MIB(out_ill->ill_ip_mib, 29951 ipIfStatsOutDiscards); 29952 goto next_mp; 29953 } 29954 /* non-ipsec hw accel case */ 29955 if (io == NULL || !io->ipsec_out_accelerated) { 29956 /* send it */ 29957 q = ire->ire_stq; 29958 if (proc == IPP_FWD_OUT) { 29959 UPDATE_IB_PKT_COUNT(ire); 29960 } else { 29961 UPDATE_OB_PKT_COUNT(ire); 29962 } 29963 ire->ire_last_used_time = lbolt; 29964 29965 if (flow_ctl_enabled || canputnext(q)) { 29966 if (proc == IPP_FWD_OUT) { 29967 BUMP_MIB(out_ill->ill_ip_mib, 29968 ipIfStatsHCOutForwDatagrams); 29969 } 29970 UPDATE_IP_MIB_OB_COUNTERS(out_ill, 29971 pkt_len); 29972 29973 putnext(q, first_mp); 29974 } else { 29975 BUMP_MIB(out_ill->ill_ip_mib, 29976 ipIfStatsOutDiscards); 29977 xmit_drop = B_TRUE; 29978 freemsg(first_mp); 29979 } 29980 } else { 29981 /* 29982 * Safety Pup says: make sure this 29983 * is going to the right interface! 29984 */ 29985 ill_t *ill1 = 29986 (ill_t *)ire->ire_stq->q_ptr; 29987 int ifindex = 29988 ill1->ill_phyint->phyint_ifindex; 29989 if (ifindex != 29990 io->ipsec_out_capab_ill_index) { 29991 xmit_drop = B_TRUE; 29992 freemsg(mp); 29993 } else { 29994 UPDATE_IP_MIB_OB_COUNTERS(ill1, 29995 pkt_len); 29996 ipsec_hw_putnext(ire->ire_stq, mp); 29997 } 29998 } 29999 next_mp: 30000 mp = nxt_mp; 30001 } /* while (mp != NULL) */ 30002 if (xmit_drop) 30003 return (SEND_FAILED); 30004 else 30005 return (SEND_PASSED); 30006 30007 case ND_INITIAL: 30008 case ND_INCOMPLETE: 30009 30010 /* 30011 * While we do send off packets to dests that 30012 * use fully-resolved CGTP routes, we do not 30013 * handle unresolved CGTP routes. 30014 */ 30015 ASSERT(!(ire->ire_flags & RTF_MULTIRT)); 30016 ASSERT(io == NULL || !io->ipsec_out_accelerated); 30017 30018 if (mp != NULL) { 30019 /* queue the packet */ 30020 nce_queue_mp_common(arpce, mp, B_FALSE); 30021 } 30022 30023 if (arpce->nce_state == ND_INCOMPLETE) { 30024 mutex_exit(&arpce->nce_lock); 30025 DTRACE_PROBE3(ip__xmit__incomplete, 30026 (ire_t *), ire, (mblk_t *), mp, 30027 (ipsec_out_t *), io); 30028 return (LOOKUP_IN_PROGRESS); 30029 } 30030 30031 arpce->nce_state = ND_INCOMPLETE; 30032 mutex_exit(&arpce->nce_lock); 30033 /* 30034 * Note that ire_add() (called from ire_forward()) 30035 * holds a ref on the ire until ARP is completed. 30036 */ 30037 30038 ire_arpresolve(ire, ire_to_ill(ire)); 30039 return (LOOKUP_IN_PROGRESS); 30040 default: 30041 ASSERT(0); 30042 mutex_exit(&arpce->nce_lock); 30043 return (LLHDR_RESLV_FAILED); 30044 } 30045 } 30046 30047 #undef UPDATE_IP_MIB_OB_COUNTERS 30048 30049 /* 30050 * Return B_TRUE if the buffers differ in length or content. 30051 * This is used for comparing extension header buffers. 30052 * Note that an extension header would be declared different 30053 * even if all that changed was the next header value in that header i.e. 30054 * what really changed is the next extension header. 30055 */ 30056 boolean_t 30057 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 30058 uint_t blen) 30059 { 30060 if (!b_valid) 30061 blen = 0; 30062 30063 if (alen != blen) 30064 return (B_TRUE); 30065 if (alen == 0) 30066 return (B_FALSE); /* Both zero length */ 30067 return (bcmp(abuf, bbuf, alen)); 30068 } 30069 30070 /* 30071 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 30072 * Return B_FALSE if memory allocation fails - don't change any state! 30073 */ 30074 boolean_t 30075 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 30076 const void *src, uint_t srclen) 30077 { 30078 void *dst; 30079 30080 if (!src_valid) 30081 srclen = 0; 30082 30083 ASSERT(*dstlenp == 0); 30084 if (src != NULL && srclen != 0) { 30085 dst = mi_alloc(srclen, BPRI_MED); 30086 if (dst == NULL) 30087 return (B_FALSE); 30088 } else { 30089 dst = NULL; 30090 } 30091 if (*dstp != NULL) 30092 mi_free(*dstp); 30093 *dstp = dst; 30094 *dstlenp = dst == NULL ? 0 : srclen; 30095 return (B_TRUE); 30096 } 30097 30098 /* 30099 * Replace what is in *dst, *dstlen with the source. 30100 * Assumes ip_allocbuf has already been called. 30101 */ 30102 void 30103 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 30104 const void *src, uint_t srclen) 30105 { 30106 if (!src_valid) 30107 srclen = 0; 30108 30109 ASSERT(*dstlenp == srclen); 30110 if (src != NULL && srclen != 0) 30111 bcopy(src, *dstp, srclen); 30112 } 30113 30114 /* 30115 * Free the storage pointed to by the members of an ip6_pkt_t. 30116 */ 30117 void 30118 ip6_pkt_free(ip6_pkt_t *ipp) 30119 { 30120 ASSERT(ipp->ipp_pathmtu == NULL && !(ipp->ipp_fields & IPPF_PATHMTU)); 30121 30122 if (ipp->ipp_fields & IPPF_HOPOPTS) { 30123 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 30124 ipp->ipp_hopopts = NULL; 30125 ipp->ipp_hopoptslen = 0; 30126 } 30127 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 30128 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 30129 ipp->ipp_rtdstopts = NULL; 30130 ipp->ipp_rtdstoptslen = 0; 30131 } 30132 if (ipp->ipp_fields & IPPF_DSTOPTS) { 30133 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 30134 ipp->ipp_dstopts = NULL; 30135 ipp->ipp_dstoptslen = 0; 30136 } 30137 if (ipp->ipp_fields & IPPF_RTHDR) { 30138 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 30139 ipp->ipp_rthdr = NULL; 30140 ipp->ipp_rthdrlen = 0; 30141 } 30142 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 30143 IPPF_RTHDR); 30144 } 30145