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 2008 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 <sys/mac.h> 60 #include <net/if.h> 61 #include <net/if_arp.h> 62 #include <net/route.h> 63 #include <sys/sockio.h> 64 #include <netinet/in.h> 65 #include <net/if_dl.h> 66 67 #include <inet/common.h> 68 #include <inet/mi.h> 69 #include <inet/mib2.h> 70 #include <inet/nd.h> 71 #include <inet/arp.h> 72 #include <inet/snmpcom.h> 73 #include <inet/optcom.h> 74 #include <inet/kstatcom.h> 75 76 #include <netinet/igmp_var.h> 77 #include <netinet/ip6.h> 78 #include <netinet/icmp6.h> 79 #include <netinet/sctp.h> 80 81 #include <inet/ip.h> 82 #include <inet/ip_impl.h> 83 #include <inet/ip6.h> 84 #include <inet/ip6_asp.h> 85 #include <inet/tcp.h> 86 #include <inet/tcp_impl.h> 87 #include <inet/ip_multi.h> 88 #include <inet/ip_if.h> 89 #include <inet/ip_ire.h> 90 #include <inet/ip_ftable.h> 91 #include <inet/ip_rts.h> 92 #include <inet/ip_ndp.h> 93 #include <inet/ip_listutils.h> 94 #include <netinet/igmp.h> 95 #include <netinet/ip_mroute.h> 96 #include <inet/ipp_common.h> 97 98 #include <net/pfkeyv2.h> 99 #include <inet/ipsec_info.h> 100 #include <inet/sadb.h> 101 #include <inet/ipsec_impl.h> 102 #include <sys/iphada.h> 103 #include <inet/tun.h> 104 #include <inet/ipdrop.h> 105 #include <inet/ip_netinfo.h> 106 107 #include <sys/ethernet.h> 108 #include <net/if_types.h> 109 #include <sys/cpuvar.h> 110 111 #include <ipp/ipp.h> 112 #include <ipp/ipp_impl.h> 113 #include <ipp/ipgpc/ipgpc.h> 114 115 #include <sys/multidata.h> 116 #include <sys/pattr.h> 117 118 #include <inet/ipclassifier.h> 119 #include <inet/sctp_ip.h> 120 #include <inet/sctp/sctp_impl.h> 121 #include <inet/udp_impl.h> 122 #include <inet/rawip_impl.h> 123 #include <inet/rts_impl.h> 124 #include <sys/sunddi.h> 125 126 #include <sys/tsol/label.h> 127 #include <sys/tsol/tnet.h> 128 129 #include <rpc/pmap_prot.h> 130 131 /* 132 * Values for squeue switch: 133 * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain 134 * IP_SQUEUE_ENTER: squeue_enter 135 * IP_SQUEUE_FILL: squeue_fill 136 */ 137 int ip_squeue_enter = 2; /* Setable in /etc/system */ 138 139 squeue_func_t ip_input_proc; 140 #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x)) 141 142 /* 143 * Setable in /etc/system 144 */ 145 int ip_poll_normal_ms = 100; 146 int ip_poll_normal_ticks = 0; 147 int ip_modclose_ackwait_ms = 3000; 148 149 /* 150 * It would be nice to have these present only in DEBUG systems, but the 151 * current design of the global symbol checking logic requires them to be 152 * unconditionally present. 153 */ 154 uint_t ip_thread_data; /* TSD key for debug support */ 155 krwlock_t ip_thread_rwlock; 156 list_t ip_thread_list; 157 158 /* 159 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 160 */ 161 162 struct listptr_s { 163 mblk_t *lp_head; /* pointer to the head of the list */ 164 mblk_t *lp_tail; /* pointer to the tail of the list */ 165 }; 166 167 typedef struct listptr_s listptr_t; 168 169 /* 170 * This is used by ip_snmp_get_mib2_ip_route_media and 171 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 172 */ 173 typedef struct iproutedata_s { 174 uint_t ird_idx; 175 listptr_t ird_route; /* ipRouteEntryTable */ 176 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 177 listptr_t ird_attrs; /* ipRouteAttributeTable */ 178 } iproutedata_t; 179 180 /* 181 * Cluster specific hooks. These should be NULL when booted as a non-cluster 182 */ 183 184 /* 185 * Hook functions to enable cluster networking 186 * On non-clustered systems these vectors must always be NULL. 187 * 188 * Hook function to Check ip specified ip address is a shared ip address 189 * in the cluster 190 * 191 */ 192 int (*cl_inet_isclusterwide)(uint8_t protocol, 193 sa_family_t addr_family, uint8_t *laddrp) = NULL; 194 195 /* 196 * Hook function to generate cluster wide ip fragment identifier 197 */ 198 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 199 uint8_t *laddrp, uint8_t *faddrp) = NULL; 200 201 /* 202 * Synchronization notes: 203 * 204 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 205 * MT level protection given by STREAMS. IP uses a combination of its own 206 * internal serialization mechanism and standard Solaris locking techniques. 207 * The internal serialization is per phyint (no IPMP) or per IPMP group. 208 * This is used to serialize plumbing operations, IPMP operations, certain 209 * multicast operations, most set ioctls, igmp/mld timers etc. 210 * 211 * Plumbing is a long sequence of operations involving message 212 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 213 * involved in plumbing operations. A natural model is to serialize these 214 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 215 * parallel without any interference. But various set ioctls on hme0 are best 216 * serialized. However if the system uses IPMP, the operations are easier if 217 * they are serialized on a per IPMP group basis since IPMP operations 218 * happen across ill's of a group. Thus the lowest common denominator is to 219 * serialize most set ioctls, multicast join/leave operations, IPMP operations 220 * igmp/mld timer operations, and processing of DLPI control messages received 221 * from drivers on a per IPMP group basis. If the system does not employ 222 * IPMP the serialization is on a per phyint basis. This serialization is 223 * provided by the ipsq_t and primitives operating on this. Details can 224 * be found in ip_if.c above the core primitives operating on ipsq_t. 225 * 226 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 227 * Simiarly lookup of an ire by a thread also returns a refheld ire. 228 * In addition ipif's and ill's referenced by the ire are also indirectly 229 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 230 * the ipif's address or netmask change as long as an ipif is refheld 231 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the 232 * address of an ipif has to go through the ipsq_t. This ensures that only 233 * 1 such exclusive operation proceeds at any time on the ipif. It then 234 * deletes all ires associated with this ipif, and waits for all refcnts 235 * associated with this ipif to come down to zero. The address is changed 236 * only after the ipif has been quiesced. Then the ipif is brought up again. 237 * More details are described above the comment in ip_sioctl_flags. 238 * 239 * Packet processing is based mostly on IREs and are fully multi-threaded 240 * using standard Solaris MT techniques. 241 * 242 * There are explicit locks in IP to handle: 243 * - The ip_g_head list maintained by mi_open_link() and friends. 244 * 245 * - The reassembly data structures (one lock per hash bucket) 246 * 247 * - conn_lock is meant to protect conn_t fields. The fields actually 248 * protected by conn_lock are documented in the conn_t definition. 249 * 250 * - ire_lock to protect some of the fields of the ire, IRE tables 251 * (one lock per hash bucket). Refer to ip_ire.c for details. 252 * 253 * - ndp_g_lock and nce_lock for protecting NCEs. 254 * 255 * - ill_lock protects fields of the ill and ipif. Details in ip.h 256 * 257 * - ill_g_lock: This is a global reader/writer lock. Protects the following 258 * * The AVL tree based global multi list of all ills. 259 * * The linked list of all ipifs of an ill 260 * * The <ill-ipsq> mapping 261 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next 262 * * The illgroup list threaded by ill_group_next. 263 * * <ill-phyint> association 264 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 265 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion 266 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill 267 * will all have to hold the ill_g_lock as writer for the actual duration 268 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping 269 * may be found in the IPMP section. 270 * 271 * - ill_lock: This is a per ill mutex. 272 * It protects some members of the ill and is documented below. 273 * It also protects the <ill-ipsq> mapping 274 * It also protects the illgroup list threaded by ill_group_next. 275 * It also protects the <ill-phyint> assoc. 276 * It also protects the list of ipifs hanging off the ill. 277 * 278 * - ipsq_lock: This is a per ipsq_t mutex lock. 279 * This protects all the other members of the ipsq struct except 280 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock 281 * 282 * - illgrp_lock: This is a per ill_group mutex lock. 283 * The only thing it protects is the illgrp_ill_schednext member of ill_group 284 * which dictates which is the next ill in an ill_group that is to be chosen 285 * for sending outgoing packets, through creation of an IRE_CACHE that 286 * references this ill. 287 * 288 * - phyint_lock: This is a per phyint mutex lock. Protects just the 289 * phyint_flags 290 * 291 * - ip_g_nd_lock: This is a global reader/writer lock. 292 * Any call to nd_load to load a new parameter to the ND table must hold the 293 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 294 * as reader. 295 * 296 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 297 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 298 * uniqueness check also done atomically. 299 * 300 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 301 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 302 * as a writer when adding or deleting elements from these lists, and 303 * as a reader when walking these lists to send a SADB update to the 304 * IPsec capable ills. 305 * 306 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 307 * group list linked by ill_usesrc_grp_next. It also protects the 308 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 309 * group is being added or deleted. This lock is taken as a reader when 310 * walking the list/group(eg: to get the number of members in a usesrc group). 311 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 312 * field is changing state i.e from NULL to non-NULL or vice-versa. For 313 * example, it is not necessary to take this lock in the initial portion 314 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and 315 * ip_sioctl_flags since the these operations are executed exclusively and 316 * that ensures that the "usesrc group state" cannot change. The "usesrc 317 * group state" change can happen only in the latter part of 318 * ip_sioctl_slifusesrc and in ill_delete. 319 * 320 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications. 321 * 322 * To change the <ill-phyint> association, the ill_g_lock must be held 323 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 324 * must be held. 325 * 326 * To change the <ill-ipsq> association the ill_g_lock must be held as writer 327 * and the ill_lock of the ill in question must be held. 328 * 329 * To change the <ill-illgroup> association the ill_g_lock must be held as 330 * writer and the ill_lock of the ill in question must be held. 331 * 332 * To add or delete an ipif from the list of ipifs hanging off the ill, 333 * ill_g_lock (writer) and ill_lock must be held and the thread must be 334 * a writer on the associated ipsq,. 335 * 336 * To add or delete an ill to the system, the ill_g_lock must be held as 337 * writer and the thread must be a writer on the associated ipsq. 338 * 339 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 340 * must be a writer on the associated ipsq. 341 * 342 * Lock hierarchy 343 * 344 * Some lock hierarchy scenarios are listed below. 345 * 346 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 347 * ill_g_lock -> illgrp_lock -> ill_lock 348 * ill_g_lock -> ill_lock(s) -> phyint_lock 349 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 350 * ill_g_lock -> ip_addr_avail_lock 351 * conn_lock -> irb_lock -> ill_lock -> ire_lock 352 * ill_g_lock -> ip_g_nd_lock 353 * 354 * When more than 1 ill lock is needed to be held, all ill lock addresses 355 * are sorted on address and locked starting from highest addressed lock 356 * downward. 357 * 358 * IPsec scenarios 359 * 360 * ipsa_lock -> ill_g_lock -> ill_lock 361 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 362 * ipsec_capab_ills_lock -> ipsa_lock 363 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 364 * 365 * Trusted Solaris scenarios 366 * 367 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 368 * igsa_lock -> gcdb_lock 369 * gcgrp_rwlock -> ire_lock 370 * gcgrp_rwlock -> gcdb_lock 371 * 372 * 373 * Routing/forwarding table locking notes: 374 * 375 * Lock acquisition order: Radix tree lock, irb_lock. 376 * Requirements: 377 * i. Walker must not hold any locks during the walker callback. 378 * ii Walker must not see a truncated tree during the walk because of any node 379 * deletion. 380 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 381 * in many places in the code to walk the irb list. Thus even if all the 382 * ires in a bucket have been deleted, we still can't free the radix node 383 * until the ires have actually been inactive'd (freed). 384 * 385 * Tree traversal - Need to hold the global tree lock in read mode. 386 * Before dropping the global tree lock, need to either increment the ire_refcnt 387 * to ensure that the radix node can't be deleted. 388 * 389 * Tree add - Need to hold the global tree lock in write mode to add a 390 * radix node. To prevent the node from being deleted, increment the 391 * irb_refcnt, after the node is added to the tree. The ire itself is 392 * added later while holding the irb_lock, but not the tree lock. 393 * 394 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 395 * All associated ires must be inactive (i.e. freed), and irb_refcnt 396 * must be zero. 397 * 398 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 399 * global tree lock (read mode) for traversal. 400 * 401 * IPsec notes : 402 * 403 * IP interacts with the IPsec code (AH/ESP) by tagging a M_CTL message 404 * in front of the actual packet. For outbound datagrams, the M_CTL 405 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 406 * information used by the IPsec code for applying the right level of 407 * protection. The information initialized by IP in the ipsec_out_t 408 * is determined by the per-socket policy or global policy in the system. 409 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 410 * ipsec_info.h) which starts out with nothing in it. It gets filled 411 * with the right information if it goes through the AH/ESP code, which 412 * happens if the incoming packet is secure. The information initialized 413 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 414 * the policy requirements needed by per-socket policy or global policy 415 * is met or not. 416 * 417 * If there is both per-socket policy (set using setsockopt) and there 418 * is also global policy match for the 5 tuples of the socket, 419 * ipsec_override_policy() makes the decision of which one to use. 420 * 421 * For fully connected sockets i.e dst, src [addr, port] is known, 422 * conn_policy_cached is set indicating that policy has been cached. 423 * conn_in_enforce_policy may or may not be set depending on whether 424 * there is a global policy match or per-socket policy match. 425 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 426 * Once the right policy is set on the conn_t, policy cannot change for 427 * this socket. This makes life simpler for TCP (UDP ?) where 428 * re-transmissions go out with the same policy. For symmetry, policy 429 * is cached for fully connected UDP sockets also. Thus if policy is cached, 430 * it also implies that policy is latched i.e policy cannot change 431 * on these sockets. As we have the right policy on the conn, we don't 432 * have to lookup global policy for every outbound and inbound datagram 433 * and thus serving as an optimization. Note that a global policy change 434 * does not affect fully connected sockets if they have policy. If fully 435 * connected sockets did not have any policy associated with it, global 436 * policy change may affect them. 437 * 438 * IP Flow control notes: 439 * 440 * Non-TCP streams are flow controlled by IP. On the send side, if the packet 441 * cannot be sent down to the driver by IP, because of a canput failure, IP 442 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. 443 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained 444 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the 445 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the 446 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains 447 * the queued messages, and removes the conn from the drain list, if all 448 * messages were drained. It also qenables the next conn in the drain list to 449 * continue the drain process. 450 * 451 * In reality the drain list is not a single list, but a configurable number 452 * of lists. The ip_wsrv on the IP module, qenables the first conn in each 453 * list. If the ip_wsrv of the next qenabled conn does not run, because the 454 * stream closes, ip_close takes responsibility to qenable the next conn in 455 * the drain list. The directly called ip_wput path always does a putq, if 456 * it cannot putnext. Thus synchronization problems are handled between 457 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 458 * functions that manipulate this drain list. Furthermore conn_drain_insert 459 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv 460 * running on a queue at any time. conn_drain_tail can be simultaneously called 461 * from both ip_wsrv and ip_close. 462 * 463 * IPQOS notes: 464 * 465 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 466 * and IPQoS modules. IPPF includes hooks in IP at different control points 467 * (callout positions) which direct packets to IPQoS modules for policy 468 * processing. Policies, if present, are global. 469 * 470 * The callout positions are located in the following paths: 471 * o local_in (packets destined for this host) 472 * o local_out (packets orginating from this host ) 473 * o fwd_in (packets forwarded by this m/c - inbound) 474 * o fwd_out (packets forwarded by this m/c - outbound) 475 * Hooks at these callout points can be enabled/disabled using the ndd variable 476 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 477 * By default all the callout positions are enabled. 478 * 479 * Outbound (local_out) 480 * Hooks are placed in ip_wput_ire and ipsec_out_process. 481 * 482 * Inbound (local_in) 483 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 484 * TCP and UDP fanout routines. 485 * 486 * Forwarding (in and out) 487 * Hooks are placed in ip_rput_forward. 488 * 489 * IP Policy Framework processing (IPPF processing) 490 * Policy processing for a packet is initiated by ip_process, which ascertains 491 * that the classifier (ipgpc) is loaded and configured, failing which the 492 * packet resumes normal processing in IP. If the clasifier is present, the 493 * packet is acted upon by one or more IPQoS modules (action instances), per 494 * filters configured in ipgpc and resumes normal IP processing thereafter. 495 * An action instance can drop a packet in course of its processing. 496 * 497 * A boolean variable, ip_policy, is used in all the fanout routines that can 498 * invoke ip_process for a packet. This variable indicates if the packet should 499 * to be sent for policy processing. The variable is set to B_TRUE by default, 500 * i.e. when the routines are invoked in the normal ip procesing path for a 501 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 502 * ip_policy is set to B_FALSE for all the routines called in these two 503 * functions because, in the former case, we don't process loopback traffic 504 * currently while in the latter, the packets have already been processed in 505 * icmp_inbound. 506 * 507 * Zones notes: 508 * 509 * The partitioning rules for networking are as follows: 510 * 1) Packets coming from a zone must have a source address belonging to that 511 * zone. 512 * 2) Packets coming from a zone can only be sent on a physical interface on 513 * which the zone has an IP address. 514 * 3) Between two zones on the same machine, packet delivery is only allowed if 515 * there's a matching route for the destination and zone in the forwarding 516 * table. 517 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 518 * different zones can bind to the same port with the wildcard address 519 * (INADDR_ANY). 520 * 521 * The granularity of interface partitioning is at the logical interface level. 522 * Therefore, every zone has its own IP addresses, and incoming packets can be 523 * attributed to a zone unambiguously. A logical interface is placed into a zone 524 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 525 * structure. Rule (1) is implemented by modifying the source address selection 526 * algorithm so that the list of eligible addresses is filtered based on the 527 * sending process zone. 528 * 529 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 530 * across all zones, depending on their type. Here is the break-up: 531 * 532 * IRE type Shared/exclusive 533 * -------- ---------------- 534 * IRE_BROADCAST Exclusive 535 * IRE_DEFAULT (default routes) Shared (*) 536 * IRE_LOCAL Exclusive (x) 537 * IRE_LOOPBACK Exclusive 538 * IRE_PREFIX (net routes) Shared (*) 539 * IRE_CACHE Exclusive 540 * IRE_IF_NORESOLVER (interface routes) Exclusive 541 * IRE_IF_RESOLVER (interface routes) Exclusive 542 * IRE_HOST (host routes) Shared (*) 543 * 544 * (*) A zone can only use a default or off-subnet route if the gateway is 545 * directly reachable from the zone, that is, if the gateway's address matches 546 * one of the zone's logical interfaces. 547 * 548 * (x) IRE_LOCAL are handled a bit differently, since for all other entries 549 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source 550 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP 551 * address of the zone itself (the destination). Since IRE_LOCAL is used 552 * for communication between zones, ip_wput_ire has special logic to set 553 * the right source address when sending using an IRE_LOCAL. 554 * 555 * Furthermore, when ip_restrict_interzone_loopback is set (the default), 556 * ire_cache_lookup restricts loopback using an IRE_LOCAL 557 * between zone to the case when L2 would have conceptually looped the packet 558 * back, i.e. the loopback which is required since neither Ethernet drivers 559 * nor Ethernet hardware loops them back. This is the case when the normal 560 * routes (ignoring IREs with different zoneids) would send out the packet on 561 * the same ill (or ill group) as the ill with which is IRE_LOCAL is 562 * associated. 563 * 564 * Multiple zones can share a common broadcast address; typically all zones 565 * share the 255.255.255.255 address. Incoming as well as locally originated 566 * broadcast packets must be dispatched to all the zones on the broadcast 567 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 568 * since some zones may not be on the 10.16.72/24 network. To handle this, each 569 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 570 * sent to every zone that has an IRE_BROADCAST entry for the destination 571 * address on the input ill, see conn_wantpacket(). 572 * 573 * Applications in different zones can join the same multicast group address. 574 * For IPv4, group memberships are per-logical interface, so they're already 575 * inherently part of a zone. For IPv6, group memberships are per-physical 576 * interface, so we distinguish IPv6 group memberships based on group address, 577 * interface and zoneid. In both cases, received multicast packets are sent to 578 * every zone for which a group membership entry exists. On IPv6 we need to 579 * check that the target zone still has an address on the receiving physical 580 * interface; it could have been removed since the application issued the 581 * IPV6_JOIN_GROUP. 582 */ 583 584 /* 585 * Squeue Fanout flags: 586 * 0: No fanout. 587 * 1: Fanout across all squeues 588 */ 589 boolean_t ip_squeue_fanout = 0; 590 591 /* 592 * Maximum dups allowed per packet. 593 */ 594 uint_t ip_max_frag_dups = 10; 595 596 #define IS_SIMPLE_IPH(ipha) \ 597 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 598 599 /* RFC1122 Conformance */ 600 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 601 602 #define ILL_MAX_NAMELEN LIFNAMSIZ 603 604 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 605 606 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag, 607 cred_t *credp, boolean_t isv6); 608 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t); 609 610 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t, 611 ip_stack_t *); 612 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 613 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 614 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); 615 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, 616 mblk_t *, int, ip_stack_t *); 617 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 618 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 619 ill_t *, zoneid_t); 620 static void icmp_options_update(ipha_t *); 621 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t, 622 ip_stack_t *); 623 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, 624 zoneid_t zoneid, ip_stack_t *); 625 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *); 626 static void icmp_redirect(ill_t *, mblk_t *); 627 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t, 628 ip_stack_t *); 629 630 static void ip_arp_news(queue_t *, mblk_t *); 631 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *, 632 ip_stack_t *); 633 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 634 char *ip_dot_addr(ipaddr_t, char *); 635 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 636 int ip_close(queue_t *, int); 637 static char *ip_dot_saddr(uchar_t *, char *); 638 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 639 boolean_t, boolean_t, ill_t *, zoneid_t); 640 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 641 boolean_t, boolean_t, zoneid_t); 642 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 643 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 644 static void ip_lrput(queue_t *, mblk_t *); 645 ipaddr_t ip_net_mask(ipaddr_t); 646 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, conn_t *, zoneid_t, 647 ip_stack_t *); 648 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 649 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *); 650 char *ip_nv_lookup(nv_t *, int); 651 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 652 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 653 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 654 static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t, 655 ipndp_t *, size_t); 656 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 657 void ip_rput(queue_t *, mblk_t *); 658 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 659 void *dummy_arg); 660 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 661 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *, 662 ip_stack_t *); 663 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 664 ire_t *, ip_stack_t *); 665 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *, 666 mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *); 667 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *, 668 ip_stack_t *); 669 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, 670 uint16_t *); 671 int ip_snmp_get(queue_t *, mblk_t *, int); 672 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, 673 mib2_ipIfStatsEntry_t *, ip_stack_t *); 674 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *, 675 ip_stack_t *); 676 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *); 677 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst); 678 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst); 679 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst); 680 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst); 681 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *, 682 ip_stack_t *ipst); 683 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *, 684 ip_stack_t *ipst); 685 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *, 686 ip_stack_t *ipst); 687 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *, 688 ip_stack_t *ipst); 689 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *, 690 ip_stack_t *ipst); 691 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *, 692 ip_stack_t *ipst); 693 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *, 694 ip_stack_t *ipst); 695 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *, 696 ip_stack_t *ipst); 697 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, 698 ip_stack_t *ipst); 699 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, 700 ip_stack_t *ipst); 701 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 702 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 703 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); 704 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 705 static boolean_t ip_source_routed(ipha_t *, ip_stack_t *); 706 static boolean_t ip_source_route_included(ipha_t *); 707 static void ip_trash_ire_reclaim_stack(ip_stack_t *); 708 709 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, 710 zoneid_t, ip_stack_t *); 711 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *); 712 static void ip_wput_local_options(ipha_t *, ip_stack_t *); 713 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 714 zoneid_t, ip_stack_t *); 715 716 static void conn_drain_init(ip_stack_t *); 717 static void conn_drain_fini(ip_stack_t *); 718 static void conn_drain_tail(conn_t *connp, boolean_t closing); 719 720 static void conn_walk_drain(ip_stack_t *); 721 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, 722 zoneid_t); 723 724 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns); 725 static void ip_stack_shutdown(netstackid_t stackid, void *arg); 726 static void ip_stack_fini(netstackid_t stackid, void *arg); 727 728 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 729 zoneid_t); 730 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 731 void *dummy_arg); 732 733 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 734 735 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 736 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 737 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 738 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 739 740 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 741 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 742 caddr_t, cred_t *); 743 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, 744 caddr_t cp, cred_t *cr); 745 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, 746 cred_t *); 747 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 748 caddr_t cp, cred_t *cr); 749 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 750 cred_t *); 751 static int ipmp_hook_emulation_set(queue_t *, mblk_t *, char *, caddr_t, 752 cred_t *); 753 static squeue_func_t ip_squeue_switch(int); 754 755 static void *ip_kstat_init(netstackid_t, ip_stack_t *); 756 static void ip_kstat_fini(netstackid_t, kstat_t *); 757 static int ip_kstat_update(kstat_t *kp, int rw); 758 static void *icmp_kstat_init(netstackid_t); 759 static void icmp_kstat_fini(netstackid_t, kstat_t *); 760 static int icmp_kstat_update(kstat_t *kp, int rw); 761 static void *ip_kstat2_init(netstackid_t, ip_stat_t *); 762 static void ip_kstat2_fini(netstackid_t, kstat_t *); 763 764 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); 765 766 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 767 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 768 769 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 770 ipha_t *, ill_t *, boolean_t); 771 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 772 773 /* How long, in seconds, we allow frags to hang around. */ 774 #define IP_FRAG_TIMEOUT 60 775 776 /* 777 * Threshold which determines whether MDT should be used when 778 * generating IP fragments; payload size must be greater than 779 * this threshold for MDT to take place. 780 */ 781 #define IP_WPUT_FRAG_MDT_MIN 32768 782 783 /* Setable in /etc/system only */ 784 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 785 786 static long ip_rput_pullups; 787 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 788 789 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */ 790 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */ 791 792 int ip_debug; 793 794 #ifdef DEBUG 795 uint32_t ipsechw_debug = 0; 796 #endif 797 798 /* 799 * Multirouting/CGTP stuff 800 */ 801 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 802 803 /* 804 * XXX following really should only be in a header. Would need more 805 * header and .c clean up first. 806 */ 807 extern optdb_obj_t ip_opt_obj; 808 809 ulong_t ip_squeue_enter_unbound = 0; 810 811 /* 812 * Named Dispatch Parameter Table. 813 * All of these are alterable, within the min/max values given, at run time. 814 */ 815 static ipparam_t lcl_param_arr[] = { 816 /* min max value name */ 817 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 818 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 819 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 820 { 0, 1, 0, "ip_respond_to_timestamp"}, 821 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 822 { 0, 1, 1, "ip_send_redirects"}, 823 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 824 { 0, 10, 0, "ip_mrtdebug"}, 825 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 826 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 827 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 828 { 1, 255, 255, "ip_def_ttl" }, 829 { 0, 1, 0, "ip_forward_src_routed"}, 830 { 0, 256, 32, "ip_wroff_extra" }, 831 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 832 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 833 { 0, 1, 1, "ip_path_mtu_discovery" }, 834 { 0, 240, 30, "ip_ignore_delete_time" }, 835 { 0, 1, 0, "ip_ignore_redirect" }, 836 { 0, 1, 1, "ip_output_queue" }, 837 { 1, 254, 1, "ip_broadcast_ttl" }, 838 { 0, 99999, 100, "ip_icmp_err_interval" }, 839 { 1, 99999, 10, "ip_icmp_err_burst" }, 840 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 841 { 0, 1, 0, "ip_strict_dst_multihoming" }, 842 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 843 { 0, 1, 0, "ipsec_override_persocket_policy" }, 844 { 0, 1, 1, "icmp_accept_clear_messages" }, 845 { 0, 1, 1, "igmp_accept_clear_messages" }, 846 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 847 "ip_ndp_delay_first_probe_time"}, 848 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 849 "ip_ndp_max_unicast_solicit"}, 850 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 851 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 852 { 0, 1, 0, "ip6_forward_src_routed"}, 853 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 854 { 0, 1, 1, "ip6_send_redirects"}, 855 { 0, 1, 0, "ip6_ignore_redirect" }, 856 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 857 858 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 859 860 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 861 862 { 0, 1, 1, "pim_accept_clear_messages" }, 863 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 864 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 865 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 866 { 0, 15, 0, "ip_policy_mask" }, 867 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 868 { 0, 255, 1, "ip_multirt_ttl" }, 869 { 0, 1, 1, "ip_multidata_outbound" }, 870 { 0, 3600000, 300000, "ip_ndp_defense_interval" }, 871 { 0, 999999, 60*60*24, "ip_max_temp_idle" }, 872 { 0, 1000, 1, "ip_max_temp_defend" }, 873 { 0, 1000, 3, "ip_max_defend" }, 874 { 0, 999999, 30, "ip_defend_interval" }, 875 { 0, 3600000, 300000, "ip_dup_recovery" }, 876 { 0, 1, 1, "ip_restrict_interzone_loopback" }, 877 { 0, 1, 1, "ip_lso_outbound" }, 878 { IGMP_V1_ROUTER, IGMP_V3_ROUTER, IGMP_V3_ROUTER, "igmp_max_version" }, 879 { MLD_V1_ROUTER, MLD_V2_ROUTER, MLD_V2_ROUTER, "mld_max_version" }, 880 #ifdef DEBUG 881 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 882 #else 883 { 0, 0, 0, "" }, 884 #endif 885 }; 886 887 /* 888 * Extended NDP table 889 * The addresses for the first two are filled in to be ips_ip_g_forward 890 * and ips_ipv6_forward at init time. 891 */ 892 static ipndp_t lcl_ndp_arr[] = { 893 /* getf setf data name */ 894 #define IPNDP_IP_FORWARDING_OFFSET 0 895 { ip_param_generic_get, ip_forward_set, NULL, 896 "ip_forwarding" }, 897 #define IPNDP_IP6_FORWARDING_OFFSET 1 898 { ip_param_generic_get, ip_forward_set, NULL, 899 "ip6_forwarding" }, 900 { ip_ill_report, NULL, NULL, 901 "ip_ill_status" }, 902 { ip_ipif_report, NULL, NULL, 903 "ip_ipif_status" }, 904 { ip_conn_report, NULL, NULL, 905 "ip_conn_status" }, 906 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, 907 "ip_rput_pullups" }, 908 { ip_srcid_report, NULL, NULL, 909 "ip_srcid_status" }, 910 { ip_param_generic_get, ip_squeue_profile_set, 911 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, 912 { ip_param_generic_get, ip_squeue_bind_set, 913 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, 914 { ip_param_generic_get, ip_input_proc_set, 915 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 916 { ip_param_generic_get, ip_int_set, 917 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 918 #define IPNDP_CGTP_FILTER_OFFSET 11 919 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL, 920 "ip_cgtp_filter" }, 921 { ip_param_generic_get, ip_int_set, 922 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" }, 923 #define IPNDP_IPMP_HOOK_OFFSET 13 924 { ip_param_generic_get, ipmp_hook_emulation_set, NULL, 925 "ipmp_hook_emulation" }, 926 { ip_param_generic_get, ip_int_set, (caddr_t)&ip_debug, 927 "ip_debug" }, 928 }; 929 930 /* 931 * Table of IP ioctls encoding the various properties of the ioctl and 932 * indexed based on the last byte of the ioctl command. Occasionally there 933 * is a clash, and there is more than 1 ioctl with the same last byte. 934 * In such a case 1 ioctl is encoded in the ndx table and the remaining 935 * ioctls are encoded in the misc table. An entry in the ndx table is 936 * retrieved by indexing on the last byte of the ioctl command and comparing 937 * the ioctl command with the value in the ndx table. In the event of a 938 * mismatch the misc table is then searched sequentially for the desired 939 * ioctl command. 940 * 941 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 942 */ 943 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 944 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 945 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 946 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 947 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 948 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 949 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 950 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 951 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 952 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 953 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 954 955 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 956 MISC_CMD, ip_siocaddrt, NULL }, 957 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 958 MISC_CMD, ip_siocdelrt, NULL }, 959 960 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 961 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 962 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 963 IF_CMD, ip_sioctl_get_addr, NULL }, 964 965 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 966 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 967 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 968 IPI_GET_CMD | IPI_REPL, 969 IF_CMD, ip_sioctl_get_dstaddr, NULL }, 970 971 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 972 IPI_PRIV | IPI_WR | IPI_REPL, 973 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 974 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 975 IPI_MODOK | IPI_GET_CMD | IPI_REPL, 976 IF_CMD, ip_sioctl_get_flags, NULL }, 977 978 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 979 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 980 981 /* copyin size cannot be coded for SIOCGIFCONF */ 982 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD, 983 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 984 985 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 986 IF_CMD, ip_sioctl_mtu, NULL }, 987 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 988 IF_CMD, ip_sioctl_get_mtu, NULL }, 989 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 990 IPI_GET_CMD | IPI_REPL, 991 IF_CMD, ip_sioctl_get_brdaddr, NULL }, 992 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 993 IF_CMD, ip_sioctl_brdaddr, NULL }, 994 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 995 IPI_GET_CMD | IPI_REPL, 996 IF_CMD, ip_sioctl_get_netmask, NULL }, 997 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 998 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 999 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 1000 IPI_GET_CMD | IPI_REPL, 1001 IF_CMD, ip_sioctl_get_metric, NULL }, 1002 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 1003 IF_CMD, ip_sioctl_metric, NULL }, 1004 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1005 1006 /* See 166-168 below for extended SIOC*XARP ioctls */ 1007 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV, 1008 ARP_CMD, ip_sioctl_arp, NULL }, 1009 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, 1010 ARP_CMD, ip_sioctl_arp, NULL }, 1011 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, 1012 ARP_CMD, ip_sioctl_arp, NULL }, 1013 1014 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1015 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1016 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1017 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1018 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1019 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1020 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1021 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1022 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1023 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1024 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1025 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1026 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1027 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1028 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1029 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1030 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1031 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1032 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1033 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1034 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1035 1036 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 1037 MISC_CMD, if_unitsel, if_unitsel_restart }, 1038 1039 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1040 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1041 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1042 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1043 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1044 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1045 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1046 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1047 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1048 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1049 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1050 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1051 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1052 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1053 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1054 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1055 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1056 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1057 1058 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 1059 IPI_PRIV | IPI_WR | IPI_MODOK, 1060 IF_CMD, ip_sioctl_sifname, NULL }, 1061 1062 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1063 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1064 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1065 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1066 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1067 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1068 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1069 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1070 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1071 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1072 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1073 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1074 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1075 1076 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL, 1077 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 1078 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1079 IF_CMD, ip_sioctl_get_muxid, NULL }, 1080 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 1081 IPI_PRIV | IPI_WR | IPI_REPL, 1082 IF_CMD, ip_sioctl_muxid, NULL }, 1083 1084 /* Both if and lif variants share same func */ 1085 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1086 IF_CMD, ip_sioctl_get_lifindex, NULL }, 1087 /* Both if and lif variants share same func */ 1088 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 1089 IPI_PRIV | IPI_WR | IPI_REPL, 1090 IF_CMD, ip_sioctl_slifindex, NULL }, 1091 1092 /* copyin size cannot be coded for SIOCGIFCONF */ 1093 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD, 1094 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1095 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1096 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1097 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1098 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1099 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1100 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1101 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1102 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1103 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1104 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1105 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1106 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1107 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1108 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1109 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1110 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1111 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1112 1113 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 1114 IPI_PRIV | IPI_WR | IPI_REPL, 1115 LIF_CMD, ip_sioctl_removeif, 1116 ip_sioctl_removeif_restart }, 1117 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 1118 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL, 1119 LIF_CMD, ip_sioctl_addif, NULL }, 1120 #define SIOCLIFADDR_NDX 112 1121 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1122 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1123 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 1124 IPI_GET_CMD | IPI_REPL, 1125 LIF_CMD, ip_sioctl_get_addr, NULL }, 1126 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1127 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1128 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 1129 IPI_GET_CMD | IPI_REPL, 1130 LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 1131 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 1132 IPI_PRIV | IPI_WR | IPI_REPL, 1133 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1134 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 1135 IPI_GET_CMD | IPI_MODOK | IPI_REPL, 1136 LIF_CMD, ip_sioctl_get_flags, NULL }, 1137 1138 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1139 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1140 1141 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, 1142 ip_sioctl_get_lifconf, NULL }, 1143 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1144 LIF_CMD, ip_sioctl_mtu, NULL }, 1145 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, 1146 LIF_CMD, ip_sioctl_get_mtu, NULL }, 1147 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 1148 IPI_GET_CMD | IPI_REPL, 1149 LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 1150 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1151 LIF_CMD, ip_sioctl_brdaddr, NULL }, 1152 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 1153 IPI_GET_CMD | IPI_REPL, 1154 LIF_CMD, ip_sioctl_get_netmask, NULL }, 1155 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1156 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1157 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 1158 IPI_GET_CMD | IPI_REPL, 1159 LIF_CMD, ip_sioctl_get_metric, NULL }, 1160 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1161 LIF_CMD, ip_sioctl_metric, NULL }, 1162 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1163 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL, 1164 LIF_CMD, ip_sioctl_slifname, 1165 ip_sioctl_slifname_restart }, 1166 1167 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL, 1168 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1169 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1170 IPI_GET_CMD | IPI_REPL, 1171 LIF_CMD, ip_sioctl_get_muxid, NULL }, 1172 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1173 IPI_PRIV | IPI_WR | IPI_REPL, 1174 LIF_CMD, ip_sioctl_muxid, NULL }, 1175 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1176 IPI_GET_CMD | IPI_REPL, 1177 LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1178 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1179 IPI_PRIV | IPI_WR | IPI_REPL, 1180 LIF_CMD, ip_sioctl_slifindex, 0 }, 1181 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1182 LIF_CMD, ip_sioctl_token, NULL }, 1183 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1184 IPI_GET_CMD | IPI_REPL, 1185 LIF_CMD, ip_sioctl_get_token, NULL }, 1186 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1187 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1188 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1189 IPI_GET_CMD | IPI_REPL, 1190 LIF_CMD, ip_sioctl_get_subnet, NULL }, 1191 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1192 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1193 1194 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1195 IPI_GET_CMD | IPI_REPL, 1196 LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1197 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1198 LIF_CMD, ip_siocdelndp_v6, NULL }, 1199 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1200 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1201 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1202 LIF_CMD, ip_siocsetndp_v6, NULL }, 1203 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1204 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1205 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1206 MISC_CMD, ip_sioctl_tonlink, NULL }, 1207 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1208 MISC_CMD, ip_sioctl_tmysite, NULL }, 1209 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL, 1210 TUN_CMD, ip_sioctl_tunparam, NULL }, 1211 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), 1212 IPI_PRIV | IPI_WR, 1213 TUN_CMD, ip_sioctl_tunparam, NULL }, 1214 1215 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1216 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1217 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1218 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1219 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1220 1221 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq), 1222 IPI_PRIV | IPI_WR | IPI_REPL, 1223 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1224 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq), 1225 IPI_PRIV | IPI_WR | IPI_REPL, 1226 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1227 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1228 IPI_PRIV | IPI_WR, 1229 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1230 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1231 IPI_GET_CMD | IPI_REPL, 1232 LIF_CMD, ip_sioctl_get_groupname, NULL }, 1233 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq), 1234 IPI_GET_CMD | IPI_REPL, 1235 LIF_CMD, ip_sioctl_get_oindex, NULL }, 1236 1237 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1238 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1239 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1240 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1241 1242 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1243 LIF_CMD, ip_sioctl_slifoindex, NULL }, 1244 1245 /* These are handled in ip_sioctl_copyin_setup itself */ 1246 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1247 MISC_CMD, NULL, NULL }, 1248 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1249 MISC_CMD, NULL, NULL }, 1250 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1251 1252 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, 1253 ip_sioctl_get_lifconf, NULL }, 1254 1255 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, 1256 XARP_CMD, ip_sioctl_arp, NULL }, 1257 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, 1258 XARP_CMD, ip_sioctl_arp, NULL }, 1259 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, 1260 XARP_CMD, ip_sioctl_arp, NULL }, 1261 1262 /* SIOCPOPSOCKFS is not handled by IP */ 1263 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1264 1265 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1266 IPI_GET_CMD | IPI_REPL, 1267 LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1268 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1269 IPI_PRIV | IPI_WR | IPI_REPL, 1270 LIF_CMD, ip_sioctl_slifzone, 1271 ip_sioctl_slifzone_restart }, 1272 /* 172-174 are SCTP ioctls and not handled by IP */ 1273 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1274 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1275 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1276 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1277 IPI_GET_CMD, LIF_CMD, 1278 ip_sioctl_get_lifusesrc, 0 }, 1279 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1280 IPI_PRIV | IPI_WR, 1281 LIF_CMD, ip_sioctl_slifusesrc, 1282 NULL }, 1283 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1284 ip_sioctl_get_lifsrcof, NULL }, 1285 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1286 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1287 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1288 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1289 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1290 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1291 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1292 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1293 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD, 1294 ip_sioctl_set_ipmpfailback, NULL }, 1295 /* SIOCSENABLESDP is handled by SDP */ 1296 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL }, 1297 }; 1298 1299 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1300 1301 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1302 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), 1303 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, 1304 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, 1305 TUN_CMD, ip_sioctl_tunparam, NULL }, 1306 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1307 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1308 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1309 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1310 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, 1311 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1312 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1313 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD, 1314 MISC_CMD, mrt_ioctl}, 1315 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD, 1316 MISC_CMD, mrt_ioctl}, 1317 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD, 1318 MISC_CMD, mrt_ioctl} 1319 }; 1320 1321 int ip_misc_ioctl_count = 1322 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1323 1324 int conn_drain_nthreads; /* Number of drainers reqd. */ 1325 /* Settable in /etc/system */ 1326 /* Defined in ip_ire.c */ 1327 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1328 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1329 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1330 1331 static nv_t ire_nv_arr[] = { 1332 { IRE_BROADCAST, "BROADCAST" }, 1333 { IRE_LOCAL, "LOCAL" }, 1334 { IRE_LOOPBACK, "LOOPBACK" }, 1335 { IRE_CACHE, "CACHE" }, 1336 { IRE_DEFAULT, "DEFAULT" }, 1337 { IRE_PREFIX, "PREFIX" }, 1338 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1339 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1340 { IRE_HOST, "HOST" }, 1341 { 0 } 1342 }; 1343 1344 nv_t *ire_nv_tbl = ire_nv_arr; 1345 1346 /* Simple ICMP IP Header Template */ 1347 static ipha_t icmp_ipha = { 1348 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1349 }; 1350 1351 struct module_info ip_mod_info = { 1352 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024 1353 }; 1354 1355 /* 1356 * Duplicate static symbols within a module confuses mdb; so we avoid the 1357 * problem by making the symbols here distinct from those in udp.c. 1358 */ 1359 1360 /* 1361 * Entry points for IP as a device and as a module. 1362 * FIXME: down the road we might want a separate module and driver qinit. 1363 * We have separate open functions for the /dev/ip and /dev/ip6 devices. 1364 */ 1365 static struct qinit iprinitv4 = { 1366 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL, 1367 &ip_mod_info 1368 }; 1369 1370 struct qinit iprinitv6 = { 1371 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL, 1372 &ip_mod_info 1373 }; 1374 1375 static struct qinit ipwinitv4 = { 1376 (pfi_t)ip_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL, 1377 &ip_mod_info 1378 }; 1379 1380 struct qinit ipwinitv6 = { 1381 (pfi_t)ip_wput_v6, (pfi_t)ip_wsrv, NULL, NULL, NULL, 1382 &ip_mod_info 1383 }; 1384 1385 static struct qinit iplrinit = { 1386 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL, 1387 &ip_mod_info 1388 }; 1389 1390 static struct qinit iplwinit = { 1391 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL, 1392 &ip_mod_info 1393 }; 1394 1395 /* For AF_INET aka /dev/ip */ 1396 struct streamtab ipinfov4 = { 1397 &iprinitv4, &ipwinitv4, &iplrinit, &iplwinit 1398 }; 1399 1400 /* For AF_INET6 aka /dev/ip6 */ 1401 struct streamtab ipinfov6 = { 1402 &iprinitv6, &ipwinitv6, &iplrinit, &iplwinit 1403 }; 1404 1405 #ifdef DEBUG 1406 static boolean_t skip_sctp_cksum = B_FALSE; 1407 #endif 1408 1409 /* 1410 * Prepend the zoneid using an ipsec_out_t for later use by functions like 1411 * ip_rput_v6(), ip_output(), etc. If the message 1412 * block already has a M_CTL at the front of it, then simply set the zoneid 1413 * appropriately. 1414 */ 1415 mblk_t * 1416 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst) 1417 { 1418 mblk_t *first_mp; 1419 ipsec_out_t *io; 1420 1421 ASSERT(zoneid != ALL_ZONES); 1422 if (mp->b_datap->db_type == M_CTL) { 1423 io = (ipsec_out_t *)mp->b_rptr; 1424 ASSERT(io->ipsec_out_type == IPSEC_OUT); 1425 io->ipsec_out_zoneid = zoneid; 1426 return (mp); 1427 } 1428 1429 first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack); 1430 if (first_mp == NULL) 1431 return (NULL); 1432 io = (ipsec_out_t *)first_mp->b_rptr; 1433 /* This is not a secure packet */ 1434 io->ipsec_out_secure = B_FALSE; 1435 io->ipsec_out_zoneid = zoneid; 1436 first_mp->b_cont = mp; 1437 return (first_mp); 1438 } 1439 1440 /* 1441 * Copy an M_CTL-tagged message, preserving reference counts appropriately. 1442 */ 1443 mblk_t * 1444 ip_copymsg(mblk_t *mp) 1445 { 1446 mblk_t *nmp; 1447 ipsec_info_t *in; 1448 1449 if (mp->b_datap->db_type != M_CTL) 1450 return (copymsg(mp)); 1451 1452 in = (ipsec_info_t *)mp->b_rptr; 1453 1454 /* 1455 * Note that M_CTL is also used for delivering ICMP error messages 1456 * upstream to transport layers. 1457 */ 1458 if (in->ipsec_info_type != IPSEC_OUT && 1459 in->ipsec_info_type != IPSEC_IN) 1460 return (copymsg(mp)); 1461 1462 nmp = copymsg(mp->b_cont); 1463 1464 if (in->ipsec_info_type == IPSEC_OUT) { 1465 return (ipsec_out_tag(mp, nmp, 1466 ((ipsec_out_t *)in)->ipsec_out_ns)); 1467 } else { 1468 return (ipsec_in_tag(mp, nmp, 1469 ((ipsec_in_t *)in)->ipsec_in_ns)); 1470 } 1471 } 1472 1473 /* Generate an ICMP fragmentation needed message. */ 1474 static void 1475 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid, 1476 ip_stack_t *ipst) 1477 { 1478 icmph_t icmph; 1479 mblk_t *first_mp; 1480 boolean_t mctl_present; 1481 1482 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1483 1484 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 1485 if (mctl_present) 1486 freeb(first_mp); 1487 return; 1488 } 1489 1490 bzero(&icmph, sizeof (icmph_t)); 1491 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1492 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1493 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1494 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded); 1495 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 1496 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 1497 ipst); 1498 } 1499 1500 /* 1501 * icmp_inbound deals with ICMP messages in the following ways. 1502 * 1503 * 1) It needs to send a reply back and possibly delivering it 1504 * to the "interested" upper clients. 1505 * 2) It needs to send it to the upper clients only. 1506 * 3) It needs to change some values in IP only. 1507 * 4) It needs to change some values in IP and upper layers e.g TCP. 1508 * 1509 * We need to accomodate icmp messages coming in clear until we get 1510 * everything secure from the wire. If icmp_accept_clear_messages 1511 * is zero we check with the global policy and act accordingly. If 1512 * it is non-zero, we accept the message without any checks. But 1513 * *this does not mean* that this will be delivered to the upper 1514 * clients. By accepting we might send replies back, change our MTU 1515 * value etc. but delivery to the ULP/clients depends on their policy 1516 * dispositions. 1517 * 1518 * We handle the above 4 cases in the context of IPsec in the 1519 * following way : 1520 * 1521 * 1) Send the reply back in the same way as the request came in. 1522 * If it came in encrypted, it goes out encrypted. If it came in 1523 * clear, it goes out in clear. Thus, this will prevent chosen 1524 * plain text attack. 1525 * 2) The client may or may not expect things to come in secure. 1526 * If it comes in secure, the policy constraints are checked 1527 * before delivering it to the upper layers. If it comes in 1528 * clear, ipsec_inbound_accept_clear will decide whether to 1529 * accept this in clear or not. In both the cases, if the returned 1530 * message (IP header + 8 bytes) that caused the icmp message has 1531 * AH/ESP headers, it is sent up to AH/ESP for validation before 1532 * sending up. If there are only 8 bytes of returned message, then 1533 * upper client will not be notified. 1534 * 3) Check with global policy to see whether it matches the constaints. 1535 * But this will be done only if icmp_accept_messages_in_clear is 1536 * zero. 1537 * 4) If we need to change both in IP and ULP, then the decision taken 1538 * while affecting the values in IP and while delivering up to TCP 1539 * should be the same. 1540 * 1541 * There are two cases. 1542 * 1543 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1544 * failed), we will not deliver it to the ULP, even though they 1545 * are *willing* to accept in *clear*. This is fine as our global 1546 * disposition to icmp messages asks us reject the datagram. 1547 * 1548 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1549 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1550 * to deliver it to ULP (policy failed), it can lead to 1551 * consistency problems. The cases known at this time are 1552 * ICMP_DESTINATION_UNREACHABLE messages with following code 1553 * values : 1554 * 1555 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1556 * and Upper layer rejects. Then the communication will 1557 * come to a stop. This is solved by making similar decisions 1558 * at both levels. Currently, when we are unable to deliver 1559 * to the Upper Layer (due to policy failures) while IP has 1560 * adjusted ire_max_frag, the next outbound datagram would 1561 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1562 * will be with the right level of protection. Thus the right 1563 * value will be communicated even if we are not able to 1564 * communicate when we get from the wire initially. But this 1565 * assumes there would be at least one outbound datagram after 1566 * IP has adjusted its ire_max_frag value. To make things 1567 * simpler, we accept in clear after the validation of 1568 * AH/ESP headers. 1569 * 1570 * - Other ICMP ERRORS : We may not be able to deliver it to the 1571 * upper layer depending on the level of protection the upper 1572 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1573 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1574 * should be accepted in clear when the Upper layer expects secure. 1575 * Thus the communication may get aborted by some bad ICMP 1576 * packets. 1577 * 1578 * IPQoS Notes: 1579 * The only instance when a packet is sent for processing is when there 1580 * isn't an ICMP client and if we are interested in it. 1581 * If there is a client, IPPF processing will take place in the 1582 * ip_fanout_proto routine. 1583 * 1584 * Zones notes: 1585 * The packet is only processed in the context of the specified zone: typically 1586 * only this zone will reply to an echo request, and only interested clients in 1587 * this zone will receive a copy of the packet. This means that the caller must 1588 * call icmp_inbound() for each relevant zone. 1589 */ 1590 static void 1591 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, 1592 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, 1593 ill_t *recv_ill, zoneid_t zoneid) 1594 { 1595 icmph_t *icmph; 1596 ipha_t *ipha; 1597 int iph_hdr_length; 1598 int hdr_length; 1599 boolean_t interested; 1600 uint32_t ts; 1601 uchar_t *wptr; 1602 ipif_t *ipif; 1603 mblk_t *first_mp; 1604 ipsec_in_t *ii; 1605 ire_t *src_ire; 1606 boolean_t onlink; 1607 timestruc_t now; 1608 uint32_t ill_index; 1609 ip_stack_t *ipst; 1610 1611 ASSERT(ill != NULL); 1612 ipst = ill->ill_ipst; 1613 1614 first_mp = mp; 1615 if (mctl_present) { 1616 mp = first_mp->b_cont; 1617 ASSERT(mp != NULL); 1618 } 1619 1620 ipha = (ipha_t *)mp->b_rptr; 1621 if (ipst->ips_icmp_accept_clear_messages == 0) { 1622 first_mp = ipsec_check_global_policy(first_mp, NULL, 1623 ipha, NULL, mctl_present, ipst->ips_netstack); 1624 if (first_mp == NULL) 1625 return; 1626 } 1627 1628 /* 1629 * On a labeled system, we have to check whether the zone itself is 1630 * permitted to receive raw traffic. 1631 */ 1632 if (is_system_labeled()) { 1633 if (zoneid == ALL_ZONES) 1634 zoneid = tsol_packet_to_zoneid(mp); 1635 if (!tsol_can_accept_raw(mp, B_FALSE)) { 1636 ip1dbg(("icmp_inbound: zone %d can't receive raw", 1637 zoneid)); 1638 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1639 freemsg(first_mp); 1640 return; 1641 } 1642 } 1643 1644 /* 1645 * We have accepted the ICMP message. It means that we will 1646 * respond to the packet if needed. It may not be delivered 1647 * to the upper client depending on the policy constraints 1648 * and the disposition in ipsec_inbound_accept_clear. 1649 */ 1650 1651 ASSERT(ill != NULL); 1652 1653 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs); 1654 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1655 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { 1656 /* Last chance to get real. */ 1657 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { 1658 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1659 freemsg(first_mp); 1660 return; 1661 } 1662 /* Refresh iph following the pullup. */ 1663 ipha = (ipha_t *)mp->b_rptr; 1664 } 1665 /* ICMP header checksum, including checksum field, should be zero. */ 1666 if (sum_valid ? (sum != 0 && sum != 0xFFFF) : 1667 IP_CSUM(mp, iph_hdr_length, 0)) { 1668 BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs); 1669 freemsg(first_mp); 1670 return; 1671 } 1672 /* The IP header will always be a multiple of four bytes */ 1673 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1674 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, 1675 icmph->icmph_code)); 1676 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1677 /* We will set "interested" to "true" if we want a copy */ 1678 interested = B_FALSE; 1679 switch (icmph->icmph_type) { 1680 case ICMP_ECHO_REPLY: 1681 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps); 1682 break; 1683 case ICMP_DEST_UNREACHABLE: 1684 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1685 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded); 1686 interested = B_TRUE; /* Pass up to transport */ 1687 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs); 1688 break; 1689 case ICMP_SOURCE_QUENCH: 1690 interested = B_TRUE; /* Pass up to transport */ 1691 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs); 1692 break; 1693 case ICMP_REDIRECT: 1694 if (!ipst->ips_ip_ignore_redirect) 1695 interested = B_TRUE; 1696 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects); 1697 break; 1698 case ICMP_ECHO_REQUEST: 1699 /* 1700 * Whether to respond to echo requests that come in as IP 1701 * broadcasts or as IP multicast is subject to debate 1702 * (what isn't?). We aim to please, you pick it. 1703 * Default is do it. 1704 */ 1705 if (!broadcast && !CLASSD(ipha->ipha_dst)) { 1706 /* unicast: always respond */ 1707 interested = B_TRUE; 1708 } else if (CLASSD(ipha->ipha_dst)) { 1709 /* multicast: respond based on tunable */ 1710 interested = ipst->ips_ip_g_resp_to_echo_mcast; 1711 } else if (broadcast) { 1712 /* broadcast: respond based on tunable */ 1713 interested = ipst->ips_ip_g_resp_to_echo_bcast; 1714 } 1715 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos); 1716 break; 1717 case ICMP_ROUTER_ADVERTISEMENT: 1718 case ICMP_ROUTER_SOLICITATION: 1719 break; 1720 case ICMP_TIME_EXCEEDED: 1721 interested = B_TRUE; /* Pass up to transport */ 1722 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds); 1723 break; 1724 case ICMP_PARAM_PROBLEM: 1725 interested = B_TRUE; /* Pass up to transport */ 1726 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs); 1727 break; 1728 case ICMP_TIME_STAMP_REQUEST: 1729 /* Response to Time Stamp Requests is local policy. */ 1730 if (ipst->ips_ip_g_resp_to_timestamp && 1731 /* So is whether to respond if it was an IP broadcast. */ 1732 (!broadcast || ipst->ips_ip_g_resp_to_timestamp_bcast)) { 1733 int tstamp_len = 3 * sizeof (uint32_t); 1734 1735 if (wptr + tstamp_len > mp->b_wptr) { 1736 if (!pullupmsg(mp, wptr + tstamp_len - 1737 mp->b_rptr)) { 1738 BUMP_MIB(ill->ill_ip_mib, 1739 ipIfStatsInDiscards); 1740 freemsg(first_mp); 1741 return; 1742 } 1743 /* Refresh ipha following the pullup. */ 1744 ipha = (ipha_t *)mp->b_rptr; 1745 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1746 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1747 } 1748 interested = B_TRUE; 1749 } 1750 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps); 1751 break; 1752 case ICMP_TIME_STAMP_REPLY: 1753 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps); 1754 break; 1755 case ICMP_INFO_REQUEST: 1756 /* Per RFC 1122 3.2.2.7, ignore this. */ 1757 case ICMP_INFO_REPLY: 1758 break; 1759 case ICMP_ADDRESS_MASK_REQUEST: 1760 if ((ipst->ips_ip_respond_to_address_mask_broadcast || 1761 !broadcast) && 1762 /* TODO m_pullup of complete header? */ 1763 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) { 1764 interested = B_TRUE; 1765 } 1766 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks); 1767 break; 1768 case ICMP_ADDRESS_MASK_REPLY: 1769 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps); 1770 break; 1771 default: 1772 interested = B_TRUE; /* Pass up to transport */ 1773 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns); 1774 break; 1775 } 1776 /* See if there is an ICMP client. */ 1777 if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) { 1778 /* If there is an ICMP client and we want one too, copy it. */ 1779 mblk_t *first_mp1; 1780 1781 if (!interested) { 1782 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1783 ip_policy, recv_ill, zoneid); 1784 return; 1785 } 1786 first_mp1 = ip_copymsg(first_mp); 1787 if (first_mp1 != NULL) { 1788 ip_fanout_proto(q, first_mp1, ill, ipha, 1789 0, mctl_present, ip_policy, recv_ill, zoneid); 1790 } 1791 } else if (!interested) { 1792 freemsg(first_mp); 1793 return; 1794 } else { 1795 /* 1796 * Initiate policy processing for this packet if ip_policy 1797 * is true. 1798 */ 1799 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 1800 ill_index = ill->ill_phyint->phyint_ifindex; 1801 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1802 if (mp == NULL) { 1803 if (mctl_present) { 1804 freeb(first_mp); 1805 } 1806 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1807 return; 1808 } 1809 } 1810 } 1811 /* We want to do something with it. */ 1812 /* Check db_ref to make sure we can modify the packet. */ 1813 if (mp->b_datap->db_ref > 1) { 1814 mblk_t *first_mp1; 1815 1816 first_mp1 = ip_copymsg(first_mp); 1817 freemsg(first_mp); 1818 if (!first_mp1) { 1819 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1820 return; 1821 } 1822 first_mp = first_mp1; 1823 if (mctl_present) { 1824 mp = first_mp->b_cont; 1825 ASSERT(mp != NULL); 1826 } else { 1827 mp = first_mp; 1828 } 1829 ipha = (ipha_t *)mp->b_rptr; 1830 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1831 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1832 } 1833 switch (icmph->icmph_type) { 1834 case ICMP_ADDRESS_MASK_REQUEST: 1835 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1836 if (ipif == NULL) { 1837 freemsg(first_mp); 1838 return; 1839 } 1840 /* 1841 * outging interface must be IPv4 1842 */ 1843 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1844 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1845 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1846 ipif_refrele(ipif); 1847 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps); 1848 break; 1849 case ICMP_ECHO_REQUEST: 1850 icmph->icmph_type = ICMP_ECHO_REPLY; 1851 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps); 1852 break; 1853 case ICMP_TIME_STAMP_REQUEST: { 1854 uint32_t *tsp; 1855 1856 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1857 tsp = (uint32_t *)wptr; 1858 tsp++; /* Skip past 'originate time' */ 1859 /* Compute # of milliseconds since midnight */ 1860 gethrestime(&now); 1861 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1862 now.tv_nsec / (NANOSEC / MILLISEC); 1863 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1864 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1865 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps); 1866 break; 1867 } 1868 default: 1869 ipha = (ipha_t *)&icmph[1]; 1870 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1871 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1872 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1873 freemsg(first_mp); 1874 return; 1875 } 1876 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1877 ipha = (ipha_t *)&icmph[1]; 1878 } 1879 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1880 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1881 freemsg(first_mp); 1882 return; 1883 } 1884 hdr_length = IPH_HDR_LENGTH(ipha); 1885 if (hdr_length < sizeof (ipha_t)) { 1886 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1887 freemsg(first_mp); 1888 return; 1889 } 1890 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 1891 if (!pullupmsg(mp, 1892 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 1893 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1894 freemsg(first_mp); 1895 return; 1896 } 1897 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1898 ipha = (ipha_t *)&icmph[1]; 1899 } 1900 switch (icmph->icmph_type) { 1901 case ICMP_REDIRECT: 1902 /* 1903 * As there is no upper client to deliver, we don't 1904 * need the first_mp any more. 1905 */ 1906 if (mctl_present) { 1907 freeb(first_mp); 1908 } 1909 icmp_redirect(ill, mp); 1910 return; 1911 case ICMP_DEST_UNREACHABLE: 1912 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 1913 if (!icmp_inbound_too_big(icmph, ipha, ill, 1914 zoneid, mp, iph_hdr_length, ipst)) { 1915 freemsg(first_mp); 1916 return; 1917 } 1918 /* 1919 * icmp_inbound_too_big() may alter mp. 1920 * Resynch ipha and icmph accordingly. 1921 */ 1922 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1923 ipha = (ipha_t *)&icmph[1]; 1924 } 1925 /* FALLTHRU */ 1926 default : 1927 /* 1928 * IPQoS notes: Since we have already done IPQoS 1929 * processing we don't want to do it again in 1930 * the fanout routines called by 1931 * icmp_inbound_error_fanout, hence the last 1932 * argument, ip_policy, is B_FALSE. 1933 */ 1934 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 1935 ipha, iph_hdr_length, hdr_length, mctl_present, 1936 B_FALSE, recv_ill, zoneid); 1937 } 1938 return; 1939 } 1940 /* Send out an ICMP packet */ 1941 icmph->icmph_checksum = 0; 1942 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 1943 if (broadcast || CLASSD(ipha->ipha_dst)) { 1944 ipif_t *ipif_chosen; 1945 /* 1946 * Make it look like it was directed to us, so we don't look 1947 * like a fool with a broadcast or multicast source address. 1948 */ 1949 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1950 /* 1951 * Make sure that we haven't grabbed an interface that's DOWN. 1952 */ 1953 if (ipif != NULL) { 1954 ipif_chosen = ipif_select_source(ipif->ipif_ill, 1955 ipha->ipha_src, zoneid); 1956 if (ipif_chosen != NULL) { 1957 ipif_refrele(ipif); 1958 ipif = ipif_chosen; 1959 } 1960 } 1961 if (ipif == NULL) { 1962 ip0dbg(("icmp_inbound: " 1963 "No source for broadcast/multicast:\n" 1964 "\tsrc 0x%x dst 0x%x ill %p " 1965 "ipif_lcl_addr 0x%x\n", 1966 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 1967 (void *)ill, 1968 ill->ill_ipif->ipif_lcl_addr)); 1969 freemsg(first_mp); 1970 return; 1971 } 1972 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1973 ipha->ipha_dst = ipif->ipif_src_addr; 1974 ipif_refrele(ipif); 1975 } 1976 /* Reset time to live. */ 1977 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 1978 { 1979 /* Swap source and destination addresses */ 1980 ipaddr_t tmp; 1981 1982 tmp = ipha->ipha_src; 1983 ipha->ipha_src = ipha->ipha_dst; 1984 ipha->ipha_dst = tmp; 1985 } 1986 ipha->ipha_ident = 0; 1987 if (!IS_SIMPLE_IPH(ipha)) 1988 icmp_options_update(ipha); 1989 1990 /* 1991 * ICMP echo replies should go out on the same interface 1992 * the request came on as probes used by in.mpathd for detecting 1993 * NIC failures are ECHO packets. We turn-off load spreading 1994 * by setting ipsec_in_attach_if to B_TRUE, which is copied 1995 * to ipsec_out_attach_if by ipsec_in_to_out called later in this 1996 * function. This is in turn handled by ip_wput and ip_newroute 1997 * to make sure that the packet goes out on the interface it came 1998 * in on. If we don't turnoff load spreading, the packets might get 1999 * dropped if there are no non-FAILED/INACTIVE interfaces for it 2000 * to go out and in.mpathd would wrongly detect a failure or 2001 * mis-detect a NIC failure for link failure. As load spreading 2002 * can happen only if ill_group is not NULL, we do only for 2003 * that case and this does not affect the normal case. 2004 * 2005 * We turn off load spreading only on echo packets that came from 2006 * on-link hosts. If the interface route has been deleted, this will 2007 * not be enforced as we can't do much. For off-link hosts, as the 2008 * default routes in IPv4 does not typically have an ire_ipif 2009 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. 2010 * Moreover, expecting a default route through this interface may 2011 * not be correct. We use ipha_dst because of the swap above. 2012 */ 2013 onlink = B_FALSE; 2014 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { 2015 /* 2016 * First, we need to make sure that it is not one of our 2017 * local addresses. If we set onlink when it is one of 2018 * our local addresses, we will end up creating IRE_CACHES 2019 * for one of our local addresses. Then, we will never 2020 * accept packets for them afterwards. 2021 */ 2022 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, 2023 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2024 if (src_ire == NULL) { 2025 ipif = ipif_get_next_ipif(NULL, ill); 2026 if (ipif == NULL) { 2027 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2028 freemsg(mp); 2029 return; 2030 } 2031 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 2032 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 2033 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE, ipst); 2034 ipif_refrele(ipif); 2035 if (src_ire != NULL) { 2036 onlink = B_TRUE; 2037 ire_refrele(src_ire); 2038 } 2039 } else { 2040 ire_refrele(src_ire); 2041 } 2042 } 2043 if (!mctl_present) { 2044 /* 2045 * This packet should go out the same way as it 2046 * came in i.e in clear. To make sure that global 2047 * policy will not be applied to this in ip_wput_ire, 2048 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2049 */ 2050 ASSERT(first_mp == mp); 2051 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2052 if (first_mp == NULL) { 2053 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2054 freemsg(mp); 2055 return; 2056 } 2057 ii = (ipsec_in_t *)first_mp->b_rptr; 2058 2059 /* This is not a secure packet */ 2060 ii->ipsec_in_secure = B_FALSE; 2061 if (onlink) { 2062 ii->ipsec_in_attach_if = B_TRUE; 2063 ii->ipsec_in_ill_index = 2064 ill->ill_phyint->phyint_ifindex; 2065 ii->ipsec_in_rill_index = 2066 recv_ill->ill_phyint->phyint_ifindex; 2067 } 2068 first_mp->b_cont = mp; 2069 } else if (onlink) { 2070 ii = (ipsec_in_t *)first_mp->b_rptr; 2071 ii->ipsec_in_attach_if = B_TRUE; 2072 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 2073 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; 2074 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2075 } else { 2076 ii = (ipsec_in_t *)first_mp->b_rptr; 2077 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2078 } 2079 ii->ipsec_in_zoneid = zoneid; 2080 ASSERT(zoneid != ALL_ZONES); 2081 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2082 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2083 return; 2084 } 2085 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 2086 put(WR(q), first_mp); 2087 } 2088 2089 static ipaddr_t 2090 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) 2091 { 2092 conn_t *connp; 2093 connf_t *connfp; 2094 ipaddr_t nexthop_addr = INADDR_ANY; 2095 int hdr_length = IPH_HDR_LENGTH(ipha); 2096 uint16_t *up; 2097 uint32_t ports; 2098 ip_stack_t *ipst = ill->ill_ipst; 2099 2100 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2101 switch (ipha->ipha_protocol) { 2102 case IPPROTO_TCP: 2103 { 2104 tcph_t *tcph; 2105 2106 /* do a reverse lookup */ 2107 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2108 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, 2109 TCPS_LISTEN, ipst); 2110 break; 2111 } 2112 case IPPROTO_UDP: 2113 { 2114 uint32_t dstport, srcport; 2115 2116 ((uint16_t *)&ports)[0] = up[1]; 2117 ((uint16_t *)&ports)[1] = up[0]; 2118 2119 /* Extract ports in net byte order */ 2120 dstport = htons(ntohl(ports) & 0xFFFF); 2121 srcport = htons(ntohl(ports) >> 16); 2122 2123 connfp = &ipst->ips_ipcl_udp_fanout[ 2124 IPCL_UDP_HASH(dstport, ipst)]; 2125 mutex_enter(&connfp->connf_lock); 2126 connp = connfp->connf_head; 2127 2128 /* do a reverse lookup */ 2129 while ((connp != NULL) && 2130 (!IPCL_UDP_MATCH(connp, dstport, 2131 ipha->ipha_src, srcport, ipha->ipha_dst) || 2132 !IPCL_ZONE_MATCH(connp, zoneid))) { 2133 connp = connp->conn_next; 2134 } 2135 if (connp != NULL) 2136 CONN_INC_REF(connp); 2137 mutex_exit(&connfp->connf_lock); 2138 break; 2139 } 2140 case IPPROTO_SCTP: 2141 { 2142 in6_addr_t map_src, map_dst; 2143 2144 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); 2145 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); 2146 ((uint16_t *)&ports)[0] = up[1]; 2147 ((uint16_t *)&ports)[1] = up[0]; 2148 2149 connp = sctp_find_conn(&map_src, &map_dst, ports, 2150 zoneid, ipst->ips_netstack->netstack_sctp); 2151 if (connp == NULL) { 2152 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, 2153 zoneid, ports, ipha, ipst); 2154 } else { 2155 CONN_INC_REF(connp); 2156 SCTP_REFRELE(CONN2SCTP(connp)); 2157 } 2158 break; 2159 } 2160 default: 2161 { 2162 ipha_t ripha; 2163 2164 ripha.ipha_src = ipha->ipha_dst; 2165 ripha.ipha_dst = ipha->ipha_src; 2166 ripha.ipha_protocol = ipha->ipha_protocol; 2167 2168 connfp = &ipst->ips_ipcl_proto_fanout[ 2169 ipha->ipha_protocol]; 2170 mutex_enter(&connfp->connf_lock); 2171 connp = connfp->connf_head; 2172 for (connp = connfp->connf_head; connp != NULL; 2173 connp = connp->conn_next) { 2174 if (IPCL_PROTO_MATCH(connp, 2175 ipha->ipha_protocol, &ripha, ill, 2176 0, zoneid)) { 2177 CONN_INC_REF(connp); 2178 break; 2179 } 2180 } 2181 mutex_exit(&connfp->connf_lock); 2182 } 2183 } 2184 if (connp != NULL) { 2185 if (connp->conn_nexthop_set) 2186 nexthop_addr = connp->conn_nexthop_v4; 2187 CONN_DEC_REF(connp); 2188 } 2189 return (nexthop_addr); 2190 } 2191 2192 /* Table from RFC 1191 */ 2193 static int icmp_frag_size_table[] = 2194 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2195 2196 /* 2197 * Process received ICMP Packet too big. 2198 * After updating any IRE it does the fanout to any matching transport streams. 2199 * Assumes the message has been pulled up till the IP header that caused 2200 * the error. 2201 * 2202 * Returns B_FALSE on failure and B_TRUE on success. 2203 */ 2204 static boolean_t 2205 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, 2206 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length, 2207 ip_stack_t *ipst) 2208 { 2209 ire_t *ire, *first_ire; 2210 int mtu; 2211 int hdr_length; 2212 ipaddr_t nexthop_addr; 2213 2214 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 2215 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 2216 ASSERT(ill != NULL); 2217 2218 hdr_length = IPH_HDR_LENGTH(ipha); 2219 2220 /* Drop if the original packet contained a source route */ 2221 if (ip_source_route_included(ipha)) { 2222 return (B_FALSE); 2223 } 2224 /* 2225 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport 2226 * header. 2227 */ 2228 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2229 mp->b_wptr) { 2230 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2231 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2232 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2233 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n")); 2234 return (B_FALSE); 2235 } 2236 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2237 ipha = (ipha_t *)&icmph[1]; 2238 } 2239 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp); 2240 if (nexthop_addr != INADDR_ANY) { 2241 /* nexthop set */ 2242 first_ire = ire_ctable_lookup(ipha->ipha_dst, 2243 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp), 2244 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, ipst); 2245 } else { 2246 /* nexthop not set */ 2247 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, 2248 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2249 } 2250 2251 if (!first_ire) { 2252 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", 2253 ntohl(ipha->ipha_dst))); 2254 return (B_FALSE); 2255 } 2256 /* Check for MTU discovery advice as described in RFC 1191 */ 2257 mtu = ntohs(icmph->icmph_du_mtu); 2258 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2259 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2260 ire = ire->ire_next) { 2261 /* 2262 * Look for the connection to which this ICMP message is 2263 * directed. If it has the IP_NEXTHOP option set, then the 2264 * search is limited to IREs with the MATCH_IRE_PRIVATE 2265 * option. Else the search is limited to regular IREs. 2266 */ 2267 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2268 (nexthop_addr != ire->ire_gateway_addr)) || 2269 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2270 (nexthop_addr != INADDR_ANY))) 2271 continue; 2272 2273 mutex_enter(&ire->ire_lock); 2274 if (icmph->icmph_du_zero == 0 && mtu > 68) { 2275 /* Reduce the IRE max frag value as advised. */ 2276 ip1dbg(("Received mtu from router: %d (was %d)\n", 2277 mtu, ire->ire_max_frag)); 2278 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2279 } else { 2280 uint32_t length; 2281 int i; 2282 2283 /* 2284 * Use the table from RFC 1191 to figure out 2285 * the next "plateau" based on the length in 2286 * the original IP packet. 2287 */ 2288 length = ntohs(ipha->ipha_length); 2289 if (ire->ire_max_frag <= length && 2290 ire->ire_max_frag >= length - hdr_length) { 2291 /* 2292 * Handle broken BSD 4.2 systems that 2293 * return the wrong iph_length in ICMP 2294 * errors. 2295 */ 2296 ip1dbg(("Wrong mtu: sent %d, ire %d\n", 2297 length, ire->ire_max_frag)); 2298 length -= hdr_length; 2299 } 2300 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2301 if (length > icmp_frag_size_table[i]) 2302 break; 2303 } 2304 if (i == A_CNT(icmp_frag_size_table)) { 2305 /* Smaller than 68! */ 2306 ip1dbg(("Too big for packet size %d\n", 2307 length)); 2308 ire->ire_max_frag = MIN(ire->ire_max_frag, 576); 2309 ire->ire_frag_flag = 0; 2310 } else { 2311 mtu = icmp_frag_size_table[i]; 2312 ip1dbg(("Calculated mtu %d, packet size %d, " 2313 "before %d", mtu, length, 2314 ire->ire_max_frag)); 2315 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2316 ip1dbg((", after %d\n", ire->ire_max_frag)); 2317 } 2318 /* Record the new max frag size for the ULP. */ 2319 icmph->icmph_du_zero = 0; 2320 icmph->icmph_du_mtu = 2321 htons((uint16_t)ire->ire_max_frag); 2322 } 2323 mutex_exit(&ire->ire_lock); 2324 } 2325 rw_exit(&first_ire->ire_bucket->irb_lock); 2326 ire_refrele(first_ire); 2327 return (B_TRUE); 2328 } 2329 2330 /* 2331 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2332 * calls this function. 2333 */ 2334 static mblk_t * 2335 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2336 { 2337 ipha_t *ipha; 2338 icmph_t *icmph; 2339 ipha_t *in_ipha; 2340 int length; 2341 2342 ASSERT(mp->b_datap->db_type == M_DATA); 2343 2344 /* 2345 * For Self-encapsulated packets, we added an extra IP header 2346 * without the options. Inner IP header is the one from which 2347 * the outer IP header was formed. Thus, we need to remove the 2348 * outer IP header. To do this, we pullup the whole message 2349 * and overlay whatever follows the outer IP header over the 2350 * outer IP header. 2351 */ 2352 2353 if (!pullupmsg(mp, -1)) 2354 return (NULL); 2355 2356 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2357 ipha = (ipha_t *)&icmph[1]; 2358 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2359 2360 /* 2361 * The length that we want to overlay is following the inner 2362 * IP header. Subtracting the IP header + icmp header + outer 2363 * IP header's length should give us the length that we want to 2364 * overlay. 2365 */ 2366 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2367 hdr_length; 2368 /* 2369 * Overlay whatever follows the inner header over the 2370 * outer header. 2371 */ 2372 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2373 2374 /* Set the wptr to account for the outer header */ 2375 mp->b_wptr -= hdr_length; 2376 return (mp); 2377 } 2378 2379 /* 2380 * Try to pass the ICMP message upstream in case the ULP cares. 2381 * 2382 * If the packet that caused the ICMP error is secure, we send 2383 * it to AH/ESP to make sure that the attached packet has a 2384 * valid association. ipha in the code below points to the 2385 * IP header of the packet that caused the error. 2386 * 2387 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2388 * in the context of IPsec. Normally we tell the upper layer 2389 * whenever we send the ire (including ip_bind), the IPsec header 2390 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2391 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2392 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2393 * same thing. As TCP has the IPsec options size that needs to be 2394 * adjusted, we just pass the MTU unchanged. 2395 * 2396 * IFN could have been generated locally or by some router. 2397 * 2398 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2399 * This happens because IP adjusted its value of MTU on an 2400 * earlier IFN message and could not tell the upper layer, 2401 * the new adjusted value of MTU e.g. Packet was encrypted 2402 * or there was not enough information to fanout to upper 2403 * layers. Thus on the next outbound datagram, ip_wput_ire 2404 * generates the IFN, where IPsec processing has *not* been 2405 * done. 2406 * 2407 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2408 * could have generated this. This happens because ire_max_frag 2409 * value in IP was set to a new value, while the IPsec processing 2410 * was being done and after we made the fragmentation check in 2411 * ip_wput_ire. Thus on return from IPsec processing, 2412 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2413 * and generates the IFN. As IPsec processing is over, we fanout 2414 * to AH/ESP to remove the header. 2415 * 2416 * In both these cases, ipsec_in_loopback will be set indicating 2417 * that IFN was generated locally. 2418 * 2419 * ROUTER : IFN could be secure or non-secure. 2420 * 2421 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2422 * packet in error has AH/ESP headers to validate the AH/ESP 2423 * headers. AH/ESP will verify whether there is a valid SA or 2424 * not and send it back. We will fanout again if we have more 2425 * data in the packet. 2426 * 2427 * If the packet in error does not have AH/ESP, we handle it 2428 * like any other case. 2429 * 2430 * * NON_SECURE : If the packet in error has AH/ESP headers, 2431 * we attach a dummy ipsec_in and send it up to AH/ESP 2432 * for validation. AH/ESP will verify whether there is a 2433 * valid SA or not and send it back. We will fanout again if 2434 * we have more data in the packet. 2435 * 2436 * If the packet in error does not have AH/ESP, we handle it 2437 * like any other case. 2438 */ 2439 static void 2440 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2441 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2442 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2443 zoneid_t zoneid) 2444 { 2445 uint16_t *up; /* Pointer to ports in ULP header */ 2446 uint32_t ports; /* reversed ports for fanout */ 2447 ipha_t ripha; /* With reversed addresses */ 2448 mblk_t *first_mp; 2449 ipsec_in_t *ii; 2450 tcph_t *tcph; 2451 conn_t *connp; 2452 ip_stack_t *ipst; 2453 2454 ASSERT(ill != NULL); 2455 2456 ASSERT(recv_ill != NULL); 2457 ipst = recv_ill->ill_ipst; 2458 2459 first_mp = mp; 2460 if (mctl_present) { 2461 mp = first_mp->b_cont; 2462 ASSERT(mp != NULL); 2463 2464 ii = (ipsec_in_t *)first_mp->b_rptr; 2465 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2466 } else { 2467 ii = NULL; 2468 } 2469 2470 switch (ipha->ipha_protocol) { 2471 case IPPROTO_UDP: 2472 /* 2473 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2474 * transport header. 2475 */ 2476 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2477 mp->b_wptr) { 2478 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2479 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2480 goto discard_pkt; 2481 } 2482 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2483 ipha = (ipha_t *)&icmph[1]; 2484 } 2485 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2486 2487 /* 2488 * Attempt to find a client stream based on port. 2489 * Note that we do a reverse lookup since the header is 2490 * in the form we sent it out. 2491 * The ripha header is only used for the IP_UDP_MATCH and we 2492 * only set the src and dst addresses and protocol. 2493 */ 2494 ripha.ipha_src = ipha->ipha_dst; 2495 ripha.ipha_dst = ipha->ipha_src; 2496 ripha.ipha_protocol = ipha->ipha_protocol; 2497 ((uint16_t *)&ports)[0] = up[1]; 2498 ((uint16_t *)&ports)[1] = up[0]; 2499 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2500 ntohl(ipha->ipha_src), ntohs(up[0]), 2501 ntohl(ipha->ipha_dst), ntohs(up[1]), 2502 icmph->icmph_type, icmph->icmph_code)); 2503 2504 /* Have to change db_type after any pullupmsg */ 2505 DB_TYPE(mp) = M_CTL; 2506 2507 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2508 mctl_present, ip_policy, recv_ill, zoneid); 2509 return; 2510 2511 case IPPROTO_TCP: 2512 /* 2513 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2514 * transport header. 2515 */ 2516 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2517 mp->b_wptr) { 2518 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2519 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2520 goto discard_pkt; 2521 } 2522 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2523 ipha = (ipha_t *)&icmph[1]; 2524 } 2525 /* 2526 * Find a TCP client stream for this packet. 2527 * Note that we do a reverse lookup since the header is 2528 * in the form we sent it out. 2529 */ 2530 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2531 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN, 2532 ipst); 2533 if (connp == NULL) 2534 goto discard_pkt; 2535 2536 /* Have to change db_type after any pullupmsg */ 2537 DB_TYPE(mp) = M_CTL; 2538 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 2539 connp, SQTAG_TCP_INPUT_ICMP_ERR); 2540 return; 2541 2542 case IPPROTO_SCTP: 2543 /* 2544 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2545 * transport header. 2546 */ 2547 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2548 mp->b_wptr) { 2549 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2550 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2551 goto discard_pkt; 2552 } 2553 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2554 ipha = (ipha_t *)&icmph[1]; 2555 } 2556 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2557 /* 2558 * Find a SCTP client stream for this packet. 2559 * Note that we do a reverse lookup since the header is 2560 * in the form we sent it out. 2561 * The ripha header is only used for the matching and we 2562 * only set the src and dst addresses, protocol, and version. 2563 */ 2564 ripha.ipha_src = ipha->ipha_dst; 2565 ripha.ipha_dst = ipha->ipha_src; 2566 ripha.ipha_protocol = ipha->ipha_protocol; 2567 ripha.ipha_version_and_hdr_length = 2568 ipha->ipha_version_and_hdr_length; 2569 ((uint16_t *)&ports)[0] = up[1]; 2570 ((uint16_t *)&ports)[1] = up[0]; 2571 2572 /* Have to change db_type after any pullupmsg */ 2573 DB_TYPE(mp) = M_CTL; 2574 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2575 mctl_present, ip_policy, zoneid); 2576 return; 2577 2578 case IPPROTO_ESP: 2579 case IPPROTO_AH: { 2580 int ipsec_rc; 2581 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 2582 2583 /* 2584 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2585 * We will re-use the IPSEC_IN if it is already present as 2586 * AH/ESP will not affect any fields in the IPSEC_IN for 2587 * ICMP errors. If there is no IPSEC_IN, allocate a new 2588 * one and attach it in the front. 2589 */ 2590 if (ii != NULL) { 2591 /* 2592 * ip_fanout_proto_again converts the ICMP errors 2593 * that come back from AH/ESP to M_DATA so that 2594 * if it is non-AH/ESP and we do a pullupmsg in 2595 * this function, it would work. Convert it back 2596 * to M_CTL before we send up as this is a ICMP 2597 * error. This could have been generated locally or 2598 * by some router. Validate the inner IPsec 2599 * headers. 2600 * 2601 * NOTE : ill_index is used by ip_fanout_proto_again 2602 * to locate the ill. 2603 */ 2604 ASSERT(ill != NULL); 2605 ii->ipsec_in_ill_index = 2606 ill->ill_phyint->phyint_ifindex; 2607 ii->ipsec_in_rill_index = 2608 recv_ill->ill_phyint->phyint_ifindex; 2609 DB_TYPE(first_mp->b_cont) = M_CTL; 2610 } else { 2611 /* 2612 * IPSEC_IN is not present. We attach a ipsec_in 2613 * message and send up to IPsec for validating 2614 * and removing the IPsec headers. Clear 2615 * ipsec_in_secure so that when we return 2616 * from IPsec, we don't mistakenly think that this 2617 * is a secure packet came from the network. 2618 * 2619 * NOTE : ill_index is used by ip_fanout_proto_again 2620 * to locate the ill. 2621 */ 2622 ASSERT(first_mp == mp); 2623 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2624 if (first_mp == NULL) { 2625 freemsg(mp); 2626 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2627 return; 2628 } 2629 ii = (ipsec_in_t *)first_mp->b_rptr; 2630 2631 /* This is not a secure packet */ 2632 ii->ipsec_in_secure = B_FALSE; 2633 first_mp->b_cont = mp; 2634 DB_TYPE(mp) = M_CTL; 2635 ASSERT(ill != NULL); 2636 ii->ipsec_in_ill_index = 2637 ill->ill_phyint->phyint_ifindex; 2638 ii->ipsec_in_rill_index = 2639 recv_ill->ill_phyint->phyint_ifindex; 2640 } 2641 ip2dbg(("icmp_inbound_error: ipsec\n")); 2642 2643 if (!ipsec_loaded(ipss)) { 2644 ip_proto_not_sup(q, first_mp, 0, zoneid, ipst); 2645 return; 2646 } 2647 2648 if (ipha->ipha_protocol == IPPROTO_ESP) 2649 ipsec_rc = ipsecesp_icmp_error(first_mp); 2650 else 2651 ipsec_rc = ipsecah_icmp_error(first_mp); 2652 if (ipsec_rc == IPSEC_STATUS_FAILED) 2653 return; 2654 2655 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2656 return; 2657 } 2658 default: 2659 /* 2660 * The ripha header is only used for the lookup and we 2661 * only set the src and dst addresses and protocol. 2662 */ 2663 ripha.ipha_src = ipha->ipha_dst; 2664 ripha.ipha_dst = ipha->ipha_src; 2665 ripha.ipha_protocol = ipha->ipha_protocol; 2666 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2667 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2668 ntohl(ipha->ipha_dst), 2669 icmph->icmph_type, icmph->icmph_code)); 2670 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2671 ipha_t *in_ipha; 2672 2673 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2674 mp->b_wptr) { 2675 if (!pullupmsg(mp, (uchar_t *)ipha + 2676 hdr_length + sizeof (ipha_t) - 2677 mp->b_rptr)) { 2678 goto discard_pkt; 2679 } 2680 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2681 ipha = (ipha_t *)&icmph[1]; 2682 } 2683 /* 2684 * Caller has verified that length has to be 2685 * at least the size of IP header. 2686 */ 2687 ASSERT(hdr_length >= sizeof (ipha_t)); 2688 /* 2689 * Check the sanity of the inner IP header like 2690 * we did for the outer header. 2691 */ 2692 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2693 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2694 goto discard_pkt; 2695 } 2696 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2697 goto discard_pkt; 2698 } 2699 /* Check for Self-encapsulated tunnels */ 2700 if (in_ipha->ipha_src == ipha->ipha_src && 2701 in_ipha->ipha_dst == ipha->ipha_dst) { 2702 2703 mp = icmp_inbound_self_encap_error(mp, 2704 iph_hdr_length, hdr_length); 2705 if (mp == NULL) 2706 goto discard_pkt; 2707 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2708 ipha = (ipha_t *)&icmph[1]; 2709 hdr_length = IPH_HDR_LENGTH(ipha); 2710 /* 2711 * The packet in error is self-encapsualted. 2712 * And we are finding it further encapsulated 2713 * which we could not have possibly generated. 2714 */ 2715 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2716 goto discard_pkt; 2717 } 2718 icmp_inbound_error_fanout(q, ill, first_mp, 2719 icmph, ipha, iph_hdr_length, hdr_length, 2720 mctl_present, ip_policy, recv_ill, zoneid); 2721 return; 2722 } 2723 } 2724 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2725 ipha->ipha_protocol == IPPROTO_IPV6) && 2726 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2727 ii != NULL && 2728 ii->ipsec_in_loopback && 2729 ii->ipsec_in_secure) { 2730 /* 2731 * For IP tunnels that get a looped-back 2732 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2733 * reported new MTU to take into account the IPsec 2734 * headers protecting this configured tunnel. 2735 * 2736 * This allows the tunnel module (tun.c) to blindly 2737 * accept the MTU reported in an ICMP "too big" 2738 * message. 2739 * 2740 * Non-looped back ICMP messages will just be 2741 * handled by the security protocols (if needed), 2742 * and the first subsequent packet will hit this 2743 * path. 2744 */ 2745 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2746 ipsec_in_extra_length(first_mp)); 2747 } 2748 /* Have to change db_type after any pullupmsg */ 2749 DB_TYPE(mp) = M_CTL; 2750 2751 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2752 ip_policy, recv_ill, zoneid); 2753 return; 2754 } 2755 /* NOTREACHED */ 2756 discard_pkt: 2757 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2758 drop_pkt:; 2759 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2760 freemsg(first_mp); 2761 } 2762 2763 /* 2764 * Common IP options parser. 2765 * 2766 * Setup routine: fill in *optp with options-parsing state, then 2767 * tail-call ipoptp_next to return the first option. 2768 */ 2769 uint8_t 2770 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2771 { 2772 uint32_t totallen; /* total length of all options */ 2773 2774 totallen = ipha->ipha_version_and_hdr_length - 2775 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2776 totallen <<= 2; 2777 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2778 optp->ipoptp_end = optp->ipoptp_next + totallen; 2779 optp->ipoptp_flags = 0; 2780 return (ipoptp_next(optp)); 2781 } 2782 2783 /* 2784 * Common IP options parser: extract next option. 2785 */ 2786 uint8_t 2787 ipoptp_next(ipoptp_t *optp) 2788 { 2789 uint8_t *end = optp->ipoptp_end; 2790 uint8_t *cur = optp->ipoptp_next; 2791 uint8_t opt, len, pointer; 2792 2793 /* 2794 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2795 * has been corrupted. 2796 */ 2797 ASSERT(cur <= end); 2798 2799 if (cur == end) 2800 return (IPOPT_EOL); 2801 2802 opt = cur[IPOPT_OPTVAL]; 2803 2804 /* 2805 * Skip any NOP options. 2806 */ 2807 while (opt == IPOPT_NOP) { 2808 cur++; 2809 if (cur == end) 2810 return (IPOPT_EOL); 2811 opt = cur[IPOPT_OPTVAL]; 2812 } 2813 2814 if (opt == IPOPT_EOL) 2815 return (IPOPT_EOL); 2816 2817 /* 2818 * Option requiring a length. 2819 */ 2820 if ((cur + 1) >= end) { 2821 optp->ipoptp_flags |= IPOPTP_ERROR; 2822 return (IPOPT_EOL); 2823 } 2824 len = cur[IPOPT_OLEN]; 2825 if (len < 2) { 2826 optp->ipoptp_flags |= IPOPTP_ERROR; 2827 return (IPOPT_EOL); 2828 } 2829 optp->ipoptp_cur = cur; 2830 optp->ipoptp_len = len; 2831 optp->ipoptp_next = cur + len; 2832 if (cur + len > end) { 2833 optp->ipoptp_flags |= IPOPTP_ERROR; 2834 return (IPOPT_EOL); 2835 } 2836 2837 /* 2838 * For the options which require a pointer field, make sure 2839 * its there, and make sure it points to either something 2840 * inside this option, or the end of the option. 2841 */ 2842 switch (opt) { 2843 case IPOPT_RR: 2844 case IPOPT_TS: 2845 case IPOPT_LSRR: 2846 case IPOPT_SSRR: 2847 if (len <= IPOPT_OFFSET) { 2848 optp->ipoptp_flags |= IPOPTP_ERROR; 2849 return (opt); 2850 } 2851 pointer = cur[IPOPT_OFFSET]; 2852 if (pointer - 1 > len) { 2853 optp->ipoptp_flags |= IPOPTP_ERROR; 2854 return (opt); 2855 } 2856 break; 2857 } 2858 2859 /* 2860 * Sanity check the pointer field based on the type of the 2861 * option. 2862 */ 2863 switch (opt) { 2864 case IPOPT_RR: 2865 case IPOPT_SSRR: 2866 case IPOPT_LSRR: 2867 if (pointer < IPOPT_MINOFF_SR) 2868 optp->ipoptp_flags |= IPOPTP_ERROR; 2869 break; 2870 case IPOPT_TS: 2871 if (pointer < IPOPT_MINOFF_IT) 2872 optp->ipoptp_flags |= IPOPTP_ERROR; 2873 /* 2874 * Note that the Internet Timestamp option also 2875 * contains two four bit fields (the Overflow field, 2876 * and the Flag field), which follow the pointer 2877 * field. We don't need to check that these fields 2878 * fall within the length of the option because this 2879 * was implicitely done above. We've checked that the 2880 * pointer value is at least IPOPT_MINOFF_IT, and that 2881 * it falls within the option. Since IPOPT_MINOFF_IT > 2882 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2883 */ 2884 ASSERT(len > IPOPT_POS_OV_FLG); 2885 break; 2886 } 2887 2888 return (opt); 2889 } 2890 2891 /* 2892 * Use the outgoing IP header to create an IP_OPTIONS option the way 2893 * it was passed down from the application. 2894 */ 2895 int 2896 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) 2897 { 2898 ipoptp_t opts; 2899 const uchar_t *opt; 2900 uint8_t optval; 2901 uint8_t optlen; 2902 uint32_t len = 0; 2903 uchar_t *buf1 = buf; 2904 2905 buf += IP_ADDR_LEN; /* Leave room for final destination */ 2906 len += IP_ADDR_LEN; 2907 bzero(buf1, IP_ADDR_LEN); 2908 2909 /* 2910 * OK to cast away const here, as we don't store through the returned 2911 * opts.ipoptp_cur pointer. 2912 */ 2913 for (optval = ipoptp_first(&opts, (ipha_t *)ipha); 2914 optval != IPOPT_EOL; 2915 optval = ipoptp_next(&opts)) { 2916 int off; 2917 2918 opt = opts.ipoptp_cur; 2919 optlen = opts.ipoptp_len; 2920 switch (optval) { 2921 case IPOPT_SSRR: 2922 case IPOPT_LSRR: 2923 2924 /* 2925 * Insert ipha_dst as the first entry in the source 2926 * route and move down the entries on step. 2927 * The last entry gets placed at buf1. 2928 */ 2929 buf[IPOPT_OPTVAL] = optval; 2930 buf[IPOPT_OLEN] = optlen; 2931 buf[IPOPT_OFFSET] = optlen; 2932 2933 off = optlen - IP_ADDR_LEN; 2934 if (off < 0) { 2935 /* No entries in source route */ 2936 break; 2937 } 2938 /* Last entry in source route */ 2939 bcopy(opt + off, buf1, IP_ADDR_LEN); 2940 off -= IP_ADDR_LEN; 2941 2942 while (off > 0) { 2943 bcopy(opt + off, 2944 buf + off + IP_ADDR_LEN, 2945 IP_ADDR_LEN); 2946 off -= IP_ADDR_LEN; 2947 } 2948 /* ipha_dst into first slot */ 2949 bcopy(&ipha->ipha_dst, 2950 buf + off + IP_ADDR_LEN, 2951 IP_ADDR_LEN); 2952 buf += optlen; 2953 len += optlen; 2954 break; 2955 2956 case IPOPT_COMSEC: 2957 case IPOPT_SECURITY: 2958 /* if passing up a label is not ok, then remove */ 2959 if (is_system_labeled()) 2960 break; 2961 /* FALLTHROUGH */ 2962 default: 2963 bcopy(opt, buf, optlen); 2964 buf += optlen; 2965 len += optlen; 2966 break; 2967 } 2968 } 2969 done: 2970 /* Pad the resulting options */ 2971 while (len & 0x3) { 2972 *buf++ = IPOPT_EOL; 2973 len++; 2974 } 2975 return (len); 2976 } 2977 2978 /* 2979 * Update any record route or timestamp options to include this host. 2980 * Reverse any source route option. 2981 * This routine assumes that the options are well formed i.e. that they 2982 * have already been checked. 2983 */ 2984 static void 2985 icmp_options_update(ipha_t *ipha) 2986 { 2987 ipoptp_t opts; 2988 uchar_t *opt; 2989 uint8_t optval; 2990 ipaddr_t src; /* Our local address */ 2991 ipaddr_t dst; 2992 2993 ip2dbg(("icmp_options_update\n")); 2994 src = ipha->ipha_src; 2995 dst = ipha->ipha_dst; 2996 2997 for (optval = ipoptp_first(&opts, ipha); 2998 optval != IPOPT_EOL; 2999 optval = ipoptp_next(&opts)) { 3000 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 3001 opt = opts.ipoptp_cur; 3002 ip2dbg(("icmp_options_update: opt %d, len %d\n", 3003 optval, opts.ipoptp_len)); 3004 switch (optval) { 3005 int off1, off2; 3006 case IPOPT_SSRR: 3007 case IPOPT_LSRR: 3008 /* 3009 * Reverse the source route. The first entry 3010 * should be the next to last one in the current 3011 * source route (the last entry is our address). 3012 * The last entry should be the final destination. 3013 */ 3014 off1 = IPOPT_MINOFF_SR - 1; 3015 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 3016 if (off2 < 0) { 3017 /* No entries in source route */ 3018 ip1dbg(( 3019 "icmp_options_update: bad src route\n")); 3020 break; 3021 } 3022 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 3023 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 3024 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 3025 off2 -= IP_ADDR_LEN; 3026 3027 while (off1 < off2) { 3028 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 3029 bcopy((char *)opt + off2, (char *)opt + off1, 3030 IP_ADDR_LEN); 3031 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 3032 off1 += IP_ADDR_LEN; 3033 off2 -= IP_ADDR_LEN; 3034 } 3035 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 3036 break; 3037 } 3038 } 3039 } 3040 3041 /* 3042 * Process received ICMP Redirect messages. 3043 */ 3044 static void 3045 icmp_redirect(ill_t *ill, mblk_t *mp) 3046 { 3047 ipha_t *ipha; 3048 int iph_hdr_length; 3049 icmph_t *icmph; 3050 ipha_t *ipha_err; 3051 ire_t *ire; 3052 ire_t *prev_ire; 3053 ire_t *save_ire; 3054 ipaddr_t src, dst, gateway; 3055 iulp_t ulp_info = { 0 }; 3056 int error; 3057 ip_stack_t *ipst; 3058 3059 ASSERT(ill != NULL); 3060 ipst = ill->ill_ipst; 3061 3062 ipha = (ipha_t *)mp->b_rptr; 3063 iph_hdr_length = IPH_HDR_LENGTH(ipha); 3064 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 3065 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 3066 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3067 freemsg(mp); 3068 return; 3069 } 3070 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 3071 ipha_err = (ipha_t *)&icmph[1]; 3072 src = ipha->ipha_src; 3073 dst = ipha_err->ipha_dst; 3074 gateway = icmph->icmph_rd_gateway; 3075 /* Make sure the new gateway is reachable somehow. */ 3076 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 3077 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3078 /* 3079 * Make sure we had a route for the dest in question and that 3080 * that route was pointing to the old gateway (the source of the 3081 * redirect packet.) 3082 */ 3083 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 3084 NULL, MATCH_IRE_GW, ipst); 3085 /* 3086 * Check that 3087 * the redirect was not from ourselves 3088 * the new gateway and the old gateway are directly reachable 3089 */ 3090 if (!prev_ire || 3091 !ire || 3092 ire->ire_type == IRE_LOCAL) { 3093 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3094 freemsg(mp); 3095 if (ire != NULL) 3096 ire_refrele(ire); 3097 if (prev_ire != NULL) 3098 ire_refrele(prev_ire); 3099 return; 3100 } 3101 3102 /* 3103 * Should we use the old ULP info to create the new gateway? From 3104 * a user's perspective, we should inherit the info so that it 3105 * is a "smooth" transition. If we do not do that, then new 3106 * connections going thru the new gateway will have no route metrics, 3107 * which is counter-intuitive to user. From a network point of 3108 * view, this may or may not make sense even though the new gateway 3109 * is still directly connected to us so the route metrics should not 3110 * change much. 3111 * 3112 * But if the old ire_uinfo is not initialized, we do another 3113 * recursive lookup on the dest using the new gateway. There may 3114 * be a route to that. If so, use it to initialize the redirect 3115 * route. 3116 */ 3117 if (prev_ire->ire_uinfo.iulp_set) { 3118 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3119 } else { 3120 ire_t *tmp_ire; 3121 ire_t *sire; 3122 3123 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 3124 ALL_ZONES, 0, NULL, 3125 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT), 3126 ipst); 3127 if (sire != NULL) { 3128 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3129 /* 3130 * If sire != NULL, ire_ftable_lookup() should not 3131 * return a NULL value. 3132 */ 3133 ASSERT(tmp_ire != NULL); 3134 ire_refrele(tmp_ire); 3135 ire_refrele(sire); 3136 } else if (tmp_ire != NULL) { 3137 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 3138 sizeof (iulp_t)); 3139 ire_refrele(tmp_ire); 3140 } 3141 } 3142 if (prev_ire->ire_type == IRE_CACHE) 3143 ire_delete(prev_ire); 3144 ire_refrele(prev_ire); 3145 /* 3146 * TODO: more precise handling for cases 0, 2, 3, the latter two 3147 * require TOS routing 3148 */ 3149 switch (icmph->icmph_code) { 3150 case 0: 3151 case 1: 3152 /* TODO: TOS specificity for cases 2 and 3 */ 3153 case 2: 3154 case 3: 3155 break; 3156 default: 3157 freemsg(mp); 3158 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3159 ire_refrele(ire); 3160 return; 3161 } 3162 /* 3163 * Create a Route Association. This will allow us to remember that 3164 * someone we believe told us to use the particular gateway. 3165 */ 3166 save_ire = ire; 3167 ire = ire_create( 3168 (uchar_t *)&dst, /* dest addr */ 3169 (uchar_t *)&ip_g_all_ones, /* mask */ 3170 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 3171 (uchar_t *)&gateway, /* gateway addr */ 3172 &save_ire->ire_max_frag, /* max frag */ 3173 NULL, /* no src nce */ 3174 NULL, /* no rfq */ 3175 NULL, /* no stq */ 3176 IRE_HOST, 3177 NULL, /* ipif */ 3178 0, /* cmask */ 3179 0, /* phandle */ 3180 0, /* ihandle */ 3181 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 3182 &ulp_info, 3183 NULL, /* tsol_gc_t */ 3184 NULL, /* gcgrp */ 3185 ipst); 3186 3187 if (ire == NULL) { 3188 freemsg(mp); 3189 ire_refrele(save_ire); 3190 return; 3191 } 3192 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); 3193 ire_refrele(save_ire); 3194 atomic_inc_32(&ipst->ips_ip_redirect_cnt); 3195 3196 if (error == 0) { 3197 ire_refrele(ire); /* Held in ire_add_v4 */ 3198 /* tell routing sockets that we received a redirect */ 3199 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 3200 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 3201 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst); 3202 } 3203 3204 /* 3205 * Delete any existing IRE_HOST type redirect ires for this destination. 3206 * This together with the added IRE has the effect of 3207 * modifying an existing redirect. 3208 */ 3209 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL, 3210 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst); 3211 if (prev_ire != NULL) { 3212 if (prev_ire ->ire_flags & RTF_DYNAMIC) 3213 ire_delete(prev_ire); 3214 ire_refrele(prev_ire); 3215 } 3216 3217 freemsg(mp); 3218 } 3219 3220 /* 3221 * Generate an ICMP parameter problem message. 3222 */ 3223 static void 3224 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid, 3225 ip_stack_t *ipst) 3226 { 3227 icmph_t icmph; 3228 boolean_t mctl_present; 3229 mblk_t *first_mp; 3230 3231 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3232 3233 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3234 if (mctl_present) 3235 freeb(first_mp); 3236 return; 3237 } 3238 3239 bzero(&icmph, sizeof (icmph_t)); 3240 icmph.icmph_type = ICMP_PARAM_PROBLEM; 3241 icmph.icmph_pp_ptr = ptr; 3242 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs); 3243 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3244 ipst); 3245 } 3246 3247 /* 3248 * Build and ship an IPv4 ICMP message using the packet data in mp, and 3249 * the ICMP header pointed to by "stuff". (May be called as writer.) 3250 * Note: assumes that icmp_pkt_err_ok has been called to verify that 3251 * an icmp error packet can be sent. 3252 * Assigns an appropriate source address to the packet. If ipha_dst is 3253 * one of our addresses use it for source. Otherwise pick a source based 3254 * on a route lookup back to ipha_src. 3255 * Note that ipha_src must be set here since the 3256 * packet is likely to arrive on an ill queue in ip_wput() which will 3257 * not set a source address. 3258 */ 3259 static void 3260 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, 3261 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 3262 { 3263 ipaddr_t dst; 3264 icmph_t *icmph; 3265 ipha_t *ipha; 3266 uint_t len_needed; 3267 size_t msg_len; 3268 mblk_t *mp1; 3269 ipaddr_t src; 3270 ire_t *ire; 3271 mblk_t *ipsec_mp; 3272 ipsec_out_t *io = NULL; 3273 3274 if (mctl_present) { 3275 /* 3276 * If it is : 3277 * 3278 * 1) a IPSEC_OUT, then this is caused by outbound 3279 * datagram originating on this host. IPsec processing 3280 * may or may not have been done. Refer to comments above 3281 * icmp_inbound_error_fanout for details. 3282 * 3283 * 2) a IPSEC_IN if we are generating a icmp_message 3284 * for an incoming datagram destined for us i.e called 3285 * from ip_fanout_send_icmp. 3286 */ 3287 ipsec_info_t *in; 3288 ipsec_mp = mp; 3289 mp = ipsec_mp->b_cont; 3290 3291 in = (ipsec_info_t *)ipsec_mp->b_rptr; 3292 ipha = (ipha_t *)mp->b_rptr; 3293 3294 ASSERT(in->ipsec_info_type == IPSEC_OUT || 3295 in->ipsec_info_type == IPSEC_IN); 3296 3297 if (in->ipsec_info_type == IPSEC_IN) { 3298 /* 3299 * Convert the IPSEC_IN to IPSEC_OUT. 3300 */ 3301 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3302 BUMP_MIB(&ipst->ips_ip_mib, 3303 ipIfStatsOutDiscards); 3304 return; 3305 } 3306 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3307 } else { 3308 ASSERT(in->ipsec_info_type == IPSEC_OUT); 3309 io = (ipsec_out_t *)in; 3310 /* 3311 * Clear out ipsec_out_proc_begin, so we do a fresh 3312 * ire lookup. 3313 */ 3314 io->ipsec_out_proc_begin = B_FALSE; 3315 } 3316 ASSERT(zoneid == io->ipsec_out_zoneid); 3317 ASSERT(zoneid != ALL_ZONES); 3318 } else { 3319 /* 3320 * This is in clear. The icmp message we are building 3321 * here should go out in clear. 3322 * 3323 * Pardon the convolution of it all, but it's easier to 3324 * allocate a "use cleartext" IPSEC_IN message and convert 3325 * it than it is to allocate a new one. 3326 */ 3327 ipsec_in_t *ii; 3328 ASSERT(DB_TYPE(mp) == M_DATA); 3329 ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 3330 if (ipsec_mp == NULL) { 3331 freemsg(mp); 3332 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3333 return; 3334 } 3335 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3336 3337 /* This is not a secure packet */ 3338 ii->ipsec_in_secure = B_FALSE; 3339 /* 3340 * For trusted extensions using a shared IP address we can 3341 * send using any zoneid. 3342 */ 3343 if (zoneid == ALL_ZONES) 3344 ii->ipsec_in_zoneid = GLOBAL_ZONEID; 3345 else 3346 ii->ipsec_in_zoneid = zoneid; 3347 ipsec_mp->b_cont = mp; 3348 ipha = (ipha_t *)mp->b_rptr; 3349 /* 3350 * Convert the IPSEC_IN to IPSEC_OUT. 3351 */ 3352 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3353 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3354 return; 3355 } 3356 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3357 } 3358 3359 /* Remember our eventual destination */ 3360 dst = ipha->ipha_src; 3361 3362 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 3363 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst); 3364 if (ire != NULL && 3365 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) { 3366 src = ipha->ipha_dst; 3367 } else { 3368 if (ire != NULL) 3369 ire_refrele(ire); 3370 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL, 3371 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY), 3372 ipst); 3373 if (ire == NULL) { 3374 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3375 freemsg(ipsec_mp); 3376 return; 3377 } 3378 src = ire->ire_src_addr; 3379 } 3380 3381 if (ire != NULL) 3382 ire_refrele(ire); 3383 3384 /* 3385 * Check if we can send back more then 8 bytes in addition to 3386 * the IP header. We try to send 64 bytes of data and the internal 3387 * header in the special cases of ipv4 encapsulated ipv4 or ipv6. 3388 */ 3389 len_needed = IPH_HDR_LENGTH(ipha); 3390 if (ipha->ipha_protocol == IPPROTO_ENCAP || 3391 ipha->ipha_protocol == IPPROTO_IPV6) { 3392 3393 if (!pullupmsg(mp, -1)) { 3394 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3395 freemsg(ipsec_mp); 3396 return; 3397 } 3398 ipha = (ipha_t *)mp->b_rptr; 3399 3400 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 3401 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + 3402 len_needed)); 3403 } else { 3404 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed); 3405 3406 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6); 3407 len_needed += ip_hdr_length_v6(mp, ip6h); 3408 } 3409 } 3410 len_needed += ipst->ips_ip_icmp_return; 3411 msg_len = msgdsize(mp); 3412 if (msg_len > len_needed) { 3413 (void) adjmsg(mp, len_needed - msg_len); 3414 msg_len = len_needed; 3415 } 3416 mp1 = allocb_tmpl(sizeof (icmp_ipha) + len, mp); 3417 if (mp1 == NULL) { 3418 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors); 3419 freemsg(ipsec_mp); 3420 return; 3421 } 3422 mp1->b_cont = mp; 3423 mp = mp1; 3424 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 3425 ipsec_mp->b_rptr == (uint8_t *)io && 3426 io->ipsec_out_type == IPSEC_OUT); 3427 ipsec_mp->b_cont = mp; 3428 3429 /* 3430 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 3431 * node generates be accepted in peace by all on-host destinations. 3432 * If we do NOT assume that all on-host destinations trust 3433 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 3434 * (Look for ipsec_out_icmp_loopback). 3435 */ 3436 io->ipsec_out_icmp_loopback = B_TRUE; 3437 3438 ipha = (ipha_t *)mp->b_rptr; 3439 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 3440 *ipha = icmp_ipha; 3441 ipha->ipha_src = src; 3442 ipha->ipha_dst = dst; 3443 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 3444 msg_len += sizeof (icmp_ipha) + len; 3445 if (msg_len > IP_MAXPACKET) { 3446 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 3447 msg_len = IP_MAXPACKET; 3448 } 3449 ipha->ipha_length = htons((uint16_t)msg_len); 3450 icmph = (icmph_t *)&ipha[1]; 3451 bcopy(stuff, icmph, len); 3452 icmph->icmph_checksum = 0; 3453 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 3454 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 3455 put(q, ipsec_mp); 3456 } 3457 3458 /* 3459 * Determine if an ICMP error packet can be sent given the rate limit. 3460 * The limit consists of an average frequency (icmp_pkt_err_interval measured 3461 * in milliseconds) and a burst size. Burst size number of packets can 3462 * be sent arbitrarely closely spaced. 3463 * The state is tracked using two variables to implement an approximate 3464 * token bucket filter: 3465 * icmp_pkt_err_last - lbolt value when the last burst started 3466 * icmp_pkt_err_sent - number of packets sent in current burst 3467 */ 3468 boolean_t 3469 icmp_err_rate_limit(ip_stack_t *ipst) 3470 { 3471 clock_t now = TICK_TO_MSEC(lbolt); 3472 uint_t refilled; /* Number of packets refilled in tbf since last */ 3473 /* Guard against changes by loading into local variable */ 3474 uint_t err_interval = ipst->ips_ip_icmp_err_interval; 3475 3476 if (err_interval == 0) 3477 return (B_FALSE); 3478 3479 if (ipst->ips_icmp_pkt_err_last > now) { 3480 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 3481 ipst->ips_icmp_pkt_err_last = 0; 3482 ipst->ips_icmp_pkt_err_sent = 0; 3483 } 3484 /* 3485 * If we are in a burst update the token bucket filter. 3486 * Update the "last" time to be close to "now" but make sure 3487 * we don't loose precision. 3488 */ 3489 if (ipst->ips_icmp_pkt_err_sent != 0) { 3490 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval; 3491 if (refilled > ipst->ips_icmp_pkt_err_sent) { 3492 ipst->ips_icmp_pkt_err_sent = 0; 3493 } else { 3494 ipst->ips_icmp_pkt_err_sent -= refilled; 3495 ipst->ips_icmp_pkt_err_last += refilled * err_interval; 3496 } 3497 } 3498 if (ipst->ips_icmp_pkt_err_sent == 0) { 3499 /* Start of new burst */ 3500 ipst->ips_icmp_pkt_err_last = now; 3501 } 3502 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) { 3503 ipst->ips_icmp_pkt_err_sent++; 3504 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 3505 ipst->ips_icmp_pkt_err_sent)); 3506 return (B_FALSE); 3507 } 3508 ip1dbg(("icmp_err_rate_limit: dropped\n")); 3509 return (B_TRUE); 3510 } 3511 3512 /* 3513 * Check if it is ok to send an IPv4 ICMP error packet in 3514 * response to the IPv4 packet in mp. 3515 * Free the message and return null if no 3516 * ICMP error packet should be sent. 3517 */ 3518 static mblk_t * 3519 icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst) 3520 { 3521 icmph_t *icmph; 3522 ipha_t *ipha; 3523 uint_t len_needed; 3524 ire_t *src_ire; 3525 ire_t *dst_ire; 3526 3527 if (!mp) 3528 return (NULL); 3529 ipha = (ipha_t *)mp->b_rptr; 3530 if (ip_csum_hdr(ipha)) { 3531 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); 3532 freemsg(mp); 3533 return (NULL); 3534 } 3535 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, 3536 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3537 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, 3538 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3539 if (src_ire != NULL || dst_ire != NULL || 3540 CLASSD(ipha->ipha_dst) || 3541 CLASSD(ipha->ipha_src) || 3542 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3543 /* Note: only errors to the fragment with offset 0 */ 3544 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3545 freemsg(mp); 3546 if (src_ire != NULL) 3547 ire_refrele(src_ire); 3548 if (dst_ire != NULL) 3549 ire_refrele(dst_ire); 3550 return (NULL); 3551 } 3552 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3553 /* 3554 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3555 * errors in response to any ICMP errors. 3556 */ 3557 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3558 if (mp->b_wptr - mp->b_rptr < len_needed) { 3559 if (!pullupmsg(mp, len_needed)) { 3560 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3561 freemsg(mp); 3562 return (NULL); 3563 } 3564 ipha = (ipha_t *)mp->b_rptr; 3565 } 3566 icmph = (icmph_t *) 3567 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3568 switch (icmph->icmph_type) { 3569 case ICMP_DEST_UNREACHABLE: 3570 case ICMP_SOURCE_QUENCH: 3571 case ICMP_TIME_EXCEEDED: 3572 case ICMP_PARAM_PROBLEM: 3573 case ICMP_REDIRECT: 3574 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3575 freemsg(mp); 3576 return (NULL); 3577 default: 3578 break; 3579 } 3580 } 3581 /* 3582 * If this is a labeled system, then check to see if we're allowed to 3583 * send a response to this particular sender. If not, then just drop. 3584 */ 3585 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 3586 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); 3587 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3588 freemsg(mp); 3589 return (NULL); 3590 } 3591 if (icmp_err_rate_limit(ipst)) { 3592 /* 3593 * Only send ICMP error packets every so often. 3594 * This should be done on a per port/source basis, 3595 * but for now this will suffice. 3596 */ 3597 freemsg(mp); 3598 return (NULL); 3599 } 3600 return (mp); 3601 } 3602 3603 /* 3604 * Generate an ICMP redirect message. 3605 */ 3606 static void 3607 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst) 3608 { 3609 icmph_t icmph; 3610 3611 /* 3612 * We are called from ip_rput where we could 3613 * not have attached an IPSEC_IN. 3614 */ 3615 ASSERT(mp->b_datap->db_type == M_DATA); 3616 3617 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3618 return; 3619 } 3620 3621 bzero(&icmph, sizeof (icmph_t)); 3622 icmph.icmph_type = ICMP_REDIRECT; 3623 icmph.icmph_code = 1; 3624 icmph.icmph_rd_gateway = gateway; 3625 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects); 3626 /* Redirects sent by router, and router is global zone */ 3627 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst); 3628 } 3629 3630 /* 3631 * Generate an ICMP time exceeded message. 3632 */ 3633 void 3634 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3635 ip_stack_t *ipst) 3636 { 3637 icmph_t icmph; 3638 boolean_t mctl_present; 3639 mblk_t *first_mp; 3640 3641 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3642 3643 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3644 if (mctl_present) 3645 freeb(first_mp); 3646 return; 3647 } 3648 3649 bzero(&icmph, sizeof (icmph_t)); 3650 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3651 icmph.icmph_code = code; 3652 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds); 3653 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3654 ipst); 3655 } 3656 3657 /* 3658 * Generate an ICMP unreachable message. 3659 */ 3660 void 3661 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3662 ip_stack_t *ipst) 3663 { 3664 icmph_t icmph; 3665 mblk_t *first_mp; 3666 boolean_t mctl_present; 3667 3668 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3669 3670 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3671 if (mctl_present) 3672 freeb(first_mp); 3673 return; 3674 } 3675 3676 bzero(&icmph, sizeof (icmph_t)); 3677 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3678 icmph.icmph_code = code; 3679 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 3680 ip2dbg(("send icmp destination unreachable code %d\n", code)); 3681 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present, 3682 zoneid, ipst); 3683 } 3684 3685 /* 3686 * Attempt to start recovery of an IPv4 interface that's been shut down as a 3687 * duplicate. As long as someone else holds the address, the interface will 3688 * stay down. When that conflict goes away, the interface is brought back up. 3689 * This is done so that accidental shutdowns of addresses aren't made 3690 * permanent. Your server will recover from a failure. 3691 * 3692 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a 3693 * user space process (dhcpagent). 3694 * 3695 * Recovery completes if ARP reports that the address is now ours (via 3696 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation. 3697 * 3698 * This function is entered on a timer expiry; the ID is in ipif_recovery_id. 3699 */ 3700 static void 3701 ipif_dup_recovery(void *arg) 3702 { 3703 ipif_t *ipif = arg; 3704 ill_t *ill = ipif->ipif_ill; 3705 mblk_t *arp_add_mp; 3706 mblk_t *arp_del_mp; 3707 area_t *area; 3708 ip_stack_t *ipst = ill->ill_ipst; 3709 3710 ipif->ipif_recovery_id = 0; 3711 3712 /* 3713 * No lock needed for moving or condemned check, as this is just an 3714 * optimization. 3715 */ 3716 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || 3717 (ipif->ipif_flags & IPIF_POINTOPOINT) || 3718 (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) { 3719 /* No reason to try to bring this address back. */ 3720 return; 3721 } 3722 3723 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL) 3724 goto alloc_fail; 3725 3726 if (ipif->ipif_arp_del_mp == NULL) { 3727 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) 3728 goto alloc_fail; 3729 ipif->ipif_arp_del_mp = arp_del_mp; 3730 } 3731 3732 /* Setting the 'unverified' flag restarts DAD */ 3733 area = (area_t *)arp_add_mp->b_rptr; 3734 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | 3735 ACE_F_UNVERIFIED; 3736 putnext(ill->ill_rq, arp_add_mp); 3737 return; 3738 3739 alloc_fail: 3740 /* 3741 * On allocation failure, just restart the timer. Note that the ipif 3742 * is down here, so no other thread could be trying to start a recovery 3743 * timer. The ill_lock protects the condemned flag and the recovery 3744 * timer ID. 3745 */ 3746 freemsg(arp_add_mp); 3747 mutex_enter(&ill->ill_lock); 3748 if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 && 3749 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 3750 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, 3751 MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3752 } 3753 mutex_exit(&ill->ill_lock); 3754 } 3755 3756 /* 3757 * This is for exclusive changes due to ARP. Either tear down an interface due 3758 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. 3759 */ 3760 /* ARGSUSED */ 3761 static void 3762 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3763 { 3764 ill_t *ill = rq->q_ptr; 3765 arh_t *arh; 3766 ipaddr_t src; 3767 ipif_t *ipif; 3768 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ 3769 char hbuf[MAC_STR_LEN]; 3770 char sbuf[INET_ADDRSTRLEN]; 3771 const char *failtype; 3772 boolean_t bring_up; 3773 ip_stack_t *ipst = ill->ill_ipst; 3774 3775 switch (((arcn_t *)mp->b_rptr)->arcn_code) { 3776 case AR_CN_READY: 3777 failtype = NULL; 3778 bring_up = B_TRUE; 3779 break; 3780 case AR_CN_FAILED: 3781 failtype = "in use"; 3782 bring_up = B_FALSE; 3783 break; 3784 default: 3785 failtype = "claimed"; 3786 bring_up = B_FALSE; 3787 break; 3788 } 3789 3790 arh = (arh_t *)mp->b_cont->b_rptr; 3791 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3792 3793 /* Handle failures due to probes */ 3794 if (src == 0) { 3795 bcopy((char *)&arh[1] + 2 * arh->arh_hlen + IP_ADDR_LEN, &src, 3796 IP_ADDR_LEN); 3797 } 3798 3799 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, 3800 sizeof (hbuf)); 3801 (void) ip_dot_addr(src, sbuf); 3802 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3803 3804 if ((ipif->ipif_flags & IPIF_POINTOPOINT) || 3805 ipif->ipif_lcl_addr != src) { 3806 continue; 3807 } 3808 3809 /* 3810 * If we failed on a recovery probe, then restart the timer to 3811 * try again later. 3812 */ 3813 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) && 3814 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3815 ill->ill_net_type == IRE_IF_RESOLVER && 3816 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3817 ipst->ips_ip_dup_recovery > 0 && 3818 ipif->ipif_recovery_id == 0) { 3819 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3820 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3821 continue; 3822 } 3823 3824 /* 3825 * If what we're trying to do has already been done, then do 3826 * nothing. 3827 */ 3828 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0)) 3829 continue; 3830 3831 ipif_get_name(ipif, ibuf, sizeof (ibuf)); 3832 3833 if (failtype == NULL) { 3834 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf, 3835 ibuf); 3836 } else { 3837 cmn_err(CE_WARN, "%s has duplicate address %s (%s " 3838 "by %s); disabled", ibuf, sbuf, failtype, hbuf); 3839 } 3840 3841 if (bring_up) { 3842 ASSERT(ill->ill_dl_up); 3843 /* 3844 * Free up the ARP delete message so we can allocate 3845 * a fresh one through the normal path. 3846 */ 3847 freemsg(ipif->ipif_arp_del_mp); 3848 ipif->ipif_arp_del_mp = NULL; 3849 if (ipif_resolver_up(ipif, Res_act_initial) != 3850 EINPROGRESS) { 3851 ipif->ipif_addr_ready = 1; 3852 (void) ipif_up_done(ipif); 3853 } 3854 continue; 3855 } 3856 3857 mutex_enter(&ill->ill_lock); 3858 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 3859 ipif->ipif_flags |= IPIF_DUPLICATE; 3860 ill->ill_ipif_dup_count++; 3861 mutex_exit(&ill->ill_lock); 3862 /* 3863 * Already exclusive on the ill; no need to handle deferred 3864 * processing here. 3865 */ 3866 (void) ipif_down(ipif, NULL, NULL); 3867 ipif_down_tail(ipif); 3868 mutex_enter(&ill->ill_lock); 3869 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3870 ill->ill_net_type == IRE_IF_RESOLVER && 3871 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3872 ipst->ips_ip_dup_recovery > 0) { 3873 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3874 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3875 } 3876 mutex_exit(&ill->ill_lock); 3877 } 3878 freemsg(mp); 3879 } 3880 3881 /* ARGSUSED */ 3882 static void 3883 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3884 { 3885 ill_t *ill = rq->q_ptr; 3886 arh_t *arh; 3887 ipaddr_t src; 3888 ipif_t *ipif; 3889 3890 arh = (arh_t *)mp->b_cont->b_rptr; 3891 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3892 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3893 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) 3894 (void) ipif_resolver_up(ipif, Res_act_defend); 3895 } 3896 freemsg(mp); 3897 } 3898 3899 /* 3900 * News from ARP. ARP sends notification of interesting events down 3901 * to its clients using M_CTL messages with the interesting ARP packet 3902 * attached via b_cont. 3903 * The interesting event from a device comes up the corresponding ARP-IP-DEV 3904 * queue as opposed to ARP sending the message to all the clients, i.e. all 3905 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 3906 * table if a cache IRE is found to delete all the entries for the address in 3907 * the packet. 3908 */ 3909 static void 3910 ip_arp_news(queue_t *q, mblk_t *mp) 3911 { 3912 arcn_t *arcn; 3913 arh_t *arh; 3914 ire_t *ire = NULL; 3915 char hbuf[MAC_STR_LEN]; 3916 char sbuf[INET_ADDRSTRLEN]; 3917 ipaddr_t src; 3918 in6_addr_t v6src; 3919 boolean_t isv6 = B_FALSE; 3920 ipif_t *ipif; 3921 ill_t *ill; 3922 ip_stack_t *ipst; 3923 3924 if (CONN_Q(q)) { 3925 conn_t *connp = Q_TO_CONN(q); 3926 3927 ipst = connp->conn_netstack->netstack_ip; 3928 } else { 3929 ill_t *ill = (ill_t *)q->q_ptr; 3930 3931 ipst = ill->ill_ipst; 3932 } 3933 3934 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 3935 if (q->q_next) { 3936 putnext(q, mp); 3937 } else 3938 freemsg(mp); 3939 return; 3940 } 3941 arh = (arh_t *)mp->b_cont->b_rptr; 3942 /* Is it one we are interested in? */ 3943 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 3944 isv6 = B_TRUE; 3945 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 3946 IPV6_ADDR_LEN); 3947 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 3948 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 3949 IP_ADDR_LEN); 3950 } else { 3951 freemsg(mp); 3952 return; 3953 } 3954 3955 ill = q->q_ptr; 3956 3957 arcn = (arcn_t *)mp->b_rptr; 3958 switch (arcn->arcn_code) { 3959 case AR_CN_BOGON: 3960 /* 3961 * Someone is sending ARP packets with a source protocol 3962 * address that we have published and for which we believe our 3963 * entry is authoritative and (when ill_arp_extend is set) 3964 * verified to be unique on the network. 3965 * 3966 * The ARP module internally handles the cases where the sender 3967 * is just probing (for DAD) and where the hardware address of 3968 * a non-authoritative entry has changed. Thus, these are the 3969 * real conflicts, and we have to do resolution. 3970 * 3971 * We back away quickly from the address if it's from DHCP or 3972 * otherwise temporary and hasn't been used recently (or at 3973 * all). We'd like to include "deprecated" addresses here as 3974 * well (as there's no real reason to defend something we're 3975 * discarding), but IPMP "reuses" this flag to mean something 3976 * other than the standard meaning. 3977 * 3978 * If the ARP module above is not extended (meaning that it 3979 * doesn't know how to defend the address), then we just log 3980 * the problem as we always did and continue on. It's not 3981 * right, but there's little else we can do, and those old ATM 3982 * users are going away anyway. 3983 */ 3984 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, 3985 hbuf, sizeof (hbuf)); 3986 (void) ip_dot_addr(src, sbuf); 3987 if (isv6) { 3988 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL, 3989 ipst); 3990 } else { 3991 ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst); 3992 } 3993 if (ire != NULL && IRE_IS_LOCAL(ire)) { 3994 uint32_t now; 3995 uint32_t maxage; 3996 clock_t lused; 3997 uint_t maxdefense; 3998 uint_t defs; 3999 4000 /* 4001 * First, figure out if this address hasn't been used 4002 * in a while. If it hasn't, then it's a better 4003 * candidate for abandoning. 4004 */ 4005 ipif = ire->ire_ipif; 4006 ASSERT(ipif != NULL); 4007 now = gethrestime_sec(); 4008 maxage = now - ire->ire_create_time; 4009 if (maxage > ipst->ips_ip_max_temp_idle) 4010 maxage = ipst->ips_ip_max_temp_idle; 4011 lused = drv_hztousec(ddi_get_lbolt() - 4012 ire->ire_last_used_time) / MICROSEC + 1; 4013 if (lused >= maxage && (ipif->ipif_flags & 4014 (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) 4015 maxdefense = ipst->ips_ip_max_temp_defend; 4016 else 4017 maxdefense = ipst->ips_ip_max_defend; 4018 4019 /* 4020 * Now figure out how many times we've defended 4021 * ourselves. Ignore defenses that happened long in 4022 * the past. 4023 */ 4024 mutex_enter(&ire->ire_lock); 4025 if ((defs = ire->ire_defense_count) > 0 && 4026 now - ire->ire_defense_time > 4027 ipst->ips_ip_defend_interval) { 4028 ire->ire_defense_count = defs = 0; 4029 } 4030 ire->ire_defense_count++; 4031 ire->ire_defense_time = now; 4032 mutex_exit(&ire->ire_lock); 4033 ill_refhold(ill); 4034 ire_refrele(ire); 4035 4036 /* 4037 * If we've defended ourselves too many times already, 4038 * then give up and tear down the interface(s) using 4039 * this address. Otherwise, defend by sending out a 4040 * gratuitous ARP. 4041 */ 4042 if (defs >= maxdefense && ill->ill_arp_extend) { 4043 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, 4044 B_FALSE); 4045 } else { 4046 cmn_err(CE_WARN, 4047 "node %s is using our IP address %s on %s", 4048 hbuf, sbuf, ill->ill_name); 4049 /* 4050 * If this is an old (ATM) ARP module, then 4051 * don't try to defend the address. Remain 4052 * compatible with the old behavior. Defend 4053 * only with new ARP. 4054 */ 4055 if (ill->ill_arp_extend) { 4056 qwriter_ip(ill, q, mp, ip_arp_defend, 4057 NEW_OP, B_FALSE); 4058 } else { 4059 ill_refrele(ill); 4060 } 4061 } 4062 return; 4063 } 4064 cmn_err(CE_WARN, 4065 "proxy ARP problem? Node '%s' is using %s on %s", 4066 hbuf, sbuf, ill->ill_name); 4067 if (ire != NULL) 4068 ire_refrele(ire); 4069 break; 4070 case AR_CN_ANNOUNCE: 4071 if (isv6) { 4072 /* 4073 * For XRESOLV interfaces. 4074 * Delete the IRE cache entry and NCE for this 4075 * v6 address 4076 */ 4077 ip_ire_clookup_and_delete_v6(&v6src, ipst); 4078 /* 4079 * If v6src is a non-zero, it's a router address 4080 * as below. Do the same sort of thing to clean 4081 * out off-net IRE_CACHE entries that go through 4082 * the router. 4083 */ 4084 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4085 ire_walk_v6(ire_delete_cache_gw_v6, 4086 (char *)&v6src, ALL_ZONES, ipst); 4087 } 4088 } else { 4089 nce_hw_map_t hwm; 4090 4091 /* 4092 * ARP gives us a copy of any packet where it thinks 4093 * the address has changed, so that we can update our 4094 * caches. We're responsible for caching known answers 4095 * in the current design. We check whether the 4096 * hardware address really has changed in all of our 4097 * entries that have cached this mapping, and if so, we 4098 * blow them away. This way we will immediately pick 4099 * up the rare case of a host changing hardware 4100 * address. 4101 */ 4102 if (src == 0) 4103 break; 4104 hwm.hwm_addr = src; 4105 hwm.hwm_hwlen = arh->arh_hlen; 4106 hwm.hwm_hwaddr = (uchar_t *)(arh + 1); 4107 NDP_HW_CHANGE_INCR(ipst->ips_ndp4); 4108 ndp_walk_common(ipst->ips_ndp4, NULL, 4109 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); 4110 NDP_HW_CHANGE_DECR(ipst->ips_ndp4); 4111 } 4112 break; 4113 case AR_CN_READY: 4114 /* No external v6 resolver has a contract to use this */ 4115 if (isv6) 4116 break; 4117 /* If the link is down, we'll retry this later */ 4118 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) 4119 break; 4120 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, 4121 NULL, NULL, ipst); 4122 if (ipif != NULL) { 4123 /* 4124 * If this is a duplicate recovery, then we now need to 4125 * go exclusive to bring this thing back up. 4126 */ 4127 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == 4128 IPIF_DUPLICATE) { 4129 ipif_refrele(ipif); 4130 ill_refhold(ill); 4131 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, 4132 B_FALSE); 4133 return; 4134 } 4135 /* 4136 * If this is the first notice that this address is 4137 * ready, then let the user know now. 4138 */ 4139 if ((ipif->ipif_flags & IPIF_UP) && 4140 !ipif->ipif_addr_ready) { 4141 ipif_mask_reply(ipif); 4142 ip_rts_ifmsg(ipif); 4143 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 4144 sctp_update_ipif(ipif, SCTP_IPIF_UP); 4145 } 4146 ipif->ipif_addr_ready = 1; 4147 ipif_refrele(ipif); 4148 } 4149 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp), ipst); 4150 if (ire != NULL) { 4151 ire->ire_defense_count = 0; 4152 ire_refrele(ire); 4153 } 4154 break; 4155 case AR_CN_FAILED: 4156 /* No external v6 resolver has a contract to use this */ 4157 if (isv6) 4158 break; 4159 ill_refhold(ill); 4160 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE); 4161 return; 4162 } 4163 freemsg(mp); 4164 } 4165 4166 /* 4167 * Create a mblk suitable for carrying the interface index and/or source link 4168 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 4169 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 4170 * application. 4171 */ 4172 mblk_t * 4173 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid, 4174 ip_stack_t *ipst) 4175 { 4176 mblk_t *mp; 4177 ip_pktinfo_t *pinfo; 4178 ipha_t *ipha; 4179 struct ether_header *pether; 4180 4181 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED); 4182 if (mp == NULL) { 4183 ip1dbg(("ip_add_info: allocation failure.\n")); 4184 return (data_mp); 4185 } 4186 4187 ipha = (ipha_t *)data_mp->b_rptr; 4188 pinfo = (ip_pktinfo_t *)mp->b_rptr; 4189 bzero(pinfo, sizeof (ip_pktinfo_t)); 4190 pinfo->ip_pkt_flags = (uchar_t)flags; 4191 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 4192 4193 if (flags & (IPF_RECVIF | IPF_RECVADDR)) 4194 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 4195 if (flags & IPF_RECVADDR) { 4196 ipif_t *ipif; 4197 ire_t *ire; 4198 4199 /* 4200 * Only valid for V4 4201 */ 4202 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) == 4203 (IPV4_VERSION << 4)); 4204 4205 ipif = ipif_get_next_ipif(NULL, ill); 4206 if (ipif != NULL) { 4207 /* 4208 * Since a decision has already been made to deliver the 4209 * packet, there is no need to test for SECATTR and 4210 * ZONEONLY. 4211 * When a multicast packet is transmitted 4212 * a cache entry is created for the multicast address. 4213 * When delivering a copy of the packet or when new 4214 * packets are received we do not want to match on the 4215 * cached entry so explicitly match on 4216 * IRE_LOCAL and IRE_LOOPBACK 4217 */ 4218 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 4219 IRE_LOCAL | IRE_LOOPBACK, 4220 ipif, zoneid, NULL, 4221 MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP, ipst); 4222 if (ire == NULL) { 4223 /* 4224 * packet must have come on a different 4225 * interface. 4226 * Since a decision has already been made to 4227 * deliver the packet, there is no need to test 4228 * for SECATTR and ZONEONLY. 4229 * Only match on local and broadcast ire's. 4230 * See detailed comment above. 4231 */ 4232 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 4233 IRE_LOCAL | IRE_LOOPBACK, ipif, zoneid, 4234 NULL, MATCH_IRE_TYPE, ipst); 4235 } 4236 4237 if (ire == NULL) { 4238 /* 4239 * This is either a multicast packet or 4240 * the address has been removed since 4241 * the packet was received. 4242 * Return INADDR_ANY so that normal source 4243 * selection occurs for the response. 4244 */ 4245 4246 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4247 } else { 4248 pinfo->ip_pkt_match_addr.s_addr = 4249 ire->ire_src_addr; 4250 ire_refrele(ire); 4251 } 4252 ipif_refrele(ipif); 4253 } else { 4254 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4255 } 4256 } 4257 4258 pether = (struct ether_header *)((char *)ipha 4259 - sizeof (struct ether_header)); 4260 /* 4261 * Make sure the interface is an ethernet type, since this option 4262 * is currently supported only on this type of interface. Also make 4263 * sure we are pointing correctly above db_base. 4264 */ 4265 4266 if ((flags & IPF_RECVSLLA) && 4267 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 4268 (ill->ill_type == IFT_ETHER) && 4269 (ill->ill_net_type == IRE_IF_RESOLVER)) { 4270 4271 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER; 4272 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, 4273 (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL); 4274 } else { 4275 /* 4276 * Clear the bit. Indicate to upper layer that IP is not 4277 * sending this ancillary info. 4278 */ 4279 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA; 4280 } 4281 4282 mp->b_datap->db_type = M_CTL; 4283 mp->b_wptr += sizeof (ip_pktinfo_t); 4284 mp->b_cont = data_mp; 4285 4286 return (mp); 4287 } 4288 4289 /* 4290 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 4291 * part of the bind request. 4292 */ 4293 4294 boolean_t 4295 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 4296 { 4297 ipsec_in_t *ii; 4298 4299 ASSERT(policy_mp != NULL); 4300 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 4301 4302 ii = (ipsec_in_t *)policy_mp->b_rptr; 4303 ASSERT(ii->ipsec_in_type == IPSEC_IN); 4304 4305 connp->conn_policy = ii->ipsec_in_policy; 4306 ii->ipsec_in_policy = NULL; 4307 4308 if (ii->ipsec_in_action != NULL) { 4309 if (connp->conn_latch == NULL) { 4310 connp->conn_latch = iplatch_create(); 4311 if (connp->conn_latch == NULL) 4312 return (B_FALSE); 4313 } 4314 ipsec_latch_inbound(connp->conn_latch, ii); 4315 } 4316 return (B_TRUE); 4317 } 4318 4319 /* 4320 * Upper level protocols (ULP) pass through bind requests to IP for inspection 4321 * and to arrange for power-fanout assist. The ULP is identified by 4322 * adding a single byte at the end of the original bind message. 4323 * A ULP other than UDP or TCP that wishes to be recognized passes 4324 * down a bind with a zero length address. 4325 * 4326 * The binding works as follows: 4327 * - A zero byte address means just bind to the protocol. 4328 * - A four byte address is treated as a request to validate 4329 * that the address is a valid local address, appropriate for 4330 * an application to bind to. This does not affect any fanout 4331 * information in IP. 4332 * - A sizeof sin_t byte address is used to bind to only the local address 4333 * and port. 4334 * - A sizeof ipa_conn_t byte address contains complete fanout information 4335 * consisting of local and remote addresses and ports. In 4336 * this case, the addresses are both validated as appropriate 4337 * for this operation, and, if so, the information is retained 4338 * for use in the inbound fanout. 4339 * 4340 * The ULP (except in the zero-length bind) can append an 4341 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 4342 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 4343 * a copy of the source or destination IRE (source for local bind; 4344 * destination for complete bind). IPSEC_POLICY_SET indicates that the 4345 * policy information contained should be copied on to the conn. 4346 * 4347 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 4348 */ 4349 mblk_t * 4350 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 4351 { 4352 ssize_t len; 4353 struct T_bind_req *tbr; 4354 sin_t *sin; 4355 ipa_conn_t *ac; 4356 uchar_t *ucp; 4357 mblk_t *mp1; 4358 boolean_t ire_requested; 4359 boolean_t ipsec_policy_set = B_FALSE; 4360 int error = 0; 4361 int protocol; 4362 ipa_conn_x_t *acx; 4363 4364 ASSERT(!connp->conn_af_isv6); 4365 connp->conn_pkt_isv6 = B_FALSE; 4366 4367 len = MBLKL(mp); 4368 if (len < (sizeof (*tbr) + 1)) { 4369 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 4370 "ip_bind: bogus msg, len %ld", len); 4371 /* XXX: Need to return something better */ 4372 goto bad_addr; 4373 } 4374 /* Back up and extract the protocol identifier. */ 4375 mp->b_wptr--; 4376 protocol = *mp->b_wptr & 0xFF; 4377 tbr = (struct T_bind_req *)mp->b_rptr; 4378 /* Reset the message type in preparation for shipping it back. */ 4379 DB_TYPE(mp) = M_PCPROTO; 4380 4381 connp->conn_ulp = (uint8_t)protocol; 4382 4383 /* 4384 * Check for a zero length address. This is from a protocol that 4385 * wants to register to receive all packets of its type. 4386 */ 4387 if (tbr->ADDR_length == 0) { 4388 /* 4389 * These protocols are now intercepted in ip_bind_v6(). 4390 * Reject protocol-level binds here for now. 4391 * 4392 * For SCTP raw socket, ICMP sends down a bind with sin_t 4393 * so that the protocol type cannot be SCTP. 4394 */ 4395 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 4396 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 4397 goto bad_addr; 4398 } 4399 4400 /* 4401 * 4402 * The udp module never sends down a zero-length address, 4403 * and allowing this on a labeled system will break MLP 4404 * functionality. 4405 */ 4406 if (is_system_labeled() && protocol == IPPROTO_UDP) 4407 goto bad_addr; 4408 4409 if (connp->conn_mac_exempt) 4410 goto bad_addr; 4411 4412 /* No hash here really. The table is big enough. */ 4413 connp->conn_srcv6 = ipv6_all_zeros; 4414 4415 ipcl_proto_insert(connp, protocol); 4416 4417 tbr->PRIM_type = T_BIND_ACK; 4418 return (mp); 4419 } 4420 4421 /* Extract the address pointer from the message. */ 4422 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 4423 tbr->ADDR_length); 4424 if (ucp == NULL) { 4425 ip1dbg(("ip_bind: no address\n")); 4426 goto bad_addr; 4427 } 4428 if (!OK_32PTR(ucp)) { 4429 ip1dbg(("ip_bind: unaligned address\n")); 4430 goto bad_addr; 4431 } 4432 /* 4433 * Check for trailing mps. 4434 */ 4435 4436 mp1 = mp->b_cont; 4437 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 4438 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); 4439 4440 switch (tbr->ADDR_length) { 4441 default: 4442 ip1dbg(("ip_bind: bad address length %d\n", 4443 (int)tbr->ADDR_length)); 4444 goto bad_addr; 4445 4446 case IP_ADDR_LEN: 4447 /* Verification of local address only */ 4448 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, 4449 ire_requested, ipsec_policy_set, B_FALSE); 4450 break; 4451 4452 case sizeof (sin_t): 4453 sin = (sin_t *)ucp; 4454 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, 4455 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); 4456 break; 4457 4458 case sizeof (ipa_conn_t): 4459 ac = (ipa_conn_t *)ucp; 4460 /* For raw socket, the local port is not set. */ 4461 if (ac->ac_lport == 0) 4462 ac->ac_lport = connp->conn_lport; 4463 /* Always verify destination reachability. */ 4464 error = ip_bind_connected(connp, mp, &ac->ac_laddr, 4465 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, 4466 ipsec_policy_set, B_TRUE, B_TRUE); 4467 break; 4468 4469 case sizeof (ipa_conn_x_t): 4470 acx = (ipa_conn_x_t *)ucp; 4471 /* 4472 * Whether or not to verify destination reachability depends 4473 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 4474 */ 4475 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, 4476 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, 4477 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, 4478 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); 4479 break; 4480 } 4481 if (error == EINPROGRESS) 4482 return (NULL); 4483 else if (error != 0) 4484 goto bad_addr; 4485 /* 4486 * Pass the IPsec headers size in ire_ipsec_overhead. 4487 * We can't do this in ip_bind_insert_ire because the policy 4488 * may not have been inherited at that point in time and hence 4489 * conn_out_enforce_policy may not be set. 4490 */ 4491 mp1 = mp->b_cont; 4492 if (ire_requested && connp->conn_out_enforce_policy && 4493 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 4494 ire_t *ire = (ire_t *)mp1->b_rptr; 4495 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 4496 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 4497 } 4498 4499 /* Send it home. */ 4500 mp->b_datap->db_type = M_PCPROTO; 4501 tbr->PRIM_type = T_BIND_ACK; 4502 return (mp); 4503 4504 bad_addr: 4505 /* 4506 * If error = -1 then we generate a TBADADDR - otherwise error is 4507 * a unix errno. 4508 */ 4509 if (error > 0) 4510 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 4511 else 4512 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 4513 return (mp); 4514 } 4515 4516 /* 4517 * Here address is verified to be a valid local address. 4518 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 4519 * address is also considered a valid local address. 4520 * In the case of a broadcast/multicast address, however, the 4521 * upper protocol is expected to reset the src address 4522 * to 0 if it sees a IRE_BROADCAST type returned so that 4523 * no packets are emitted with broadcast/multicast address as 4524 * source address (that violates hosts requirements RFC1122) 4525 * The addresses valid for bind are: 4526 * (1) - INADDR_ANY (0) 4527 * (2) - IP address of an UP interface 4528 * (3) - IP address of a DOWN interface 4529 * (4) - valid local IP broadcast addresses. In this case 4530 * the conn will only receive packets destined to 4531 * the specified broadcast address. 4532 * (5) - a multicast address. In this case 4533 * the conn will only receive packets destined to 4534 * the specified multicast address. Note: the 4535 * application still has to issue an 4536 * IP_ADD_MEMBERSHIP socket option. 4537 * 4538 * On error, return -1 for TBADADDR otherwise pass the 4539 * errno with TSYSERR reply. 4540 * 4541 * In all the above cases, the bound address must be valid in the current zone. 4542 * When the address is loopback, multicast or broadcast, there might be many 4543 * matching IREs so bind has to look up based on the zone. 4544 * 4545 * Note: lport is in network byte order. 4546 */ 4547 int 4548 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, 4549 boolean_t ire_requested, boolean_t ipsec_policy_set, 4550 boolean_t fanout_insert) 4551 { 4552 int error = 0; 4553 ire_t *src_ire; 4554 mblk_t *policy_mp; 4555 ipif_t *ipif; 4556 zoneid_t zoneid; 4557 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4558 4559 if (ipsec_policy_set) { 4560 policy_mp = mp->b_cont; 4561 } 4562 4563 /* 4564 * If it was previously connected, conn_fully_bound would have 4565 * been set. 4566 */ 4567 connp->conn_fully_bound = B_FALSE; 4568 4569 src_ire = NULL; 4570 ipif = NULL; 4571 4572 zoneid = IPCL_ZONEID(connp); 4573 4574 if (src_addr) { 4575 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 4576 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 4577 /* 4578 * If an address other than 0.0.0.0 is requested, 4579 * we verify that it is a valid address for bind 4580 * Note: Following code is in if-else-if form for 4581 * readability compared to a condition check. 4582 */ 4583 /* LINTED - statement has no consequent */ 4584 if (IRE_IS_LOCAL(src_ire)) { 4585 /* 4586 * (2) Bind to address of local UP interface 4587 */ 4588 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 4589 /* 4590 * (4) Bind to broadcast address 4591 * Note: permitted only from transports that 4592 * request IRE 4593 */ 4594 if (!ire_requested) 4595 error = EADDRNOTAVAIL; 4596 } else { 4597 /* 4598 * (3) Bind to address of local DOWN interface 4599 * (ipif_lookup_addr() looks up all interfaces 4600 * but we do not get here for UP interfaces 4601 * - case (2) above) 4602 * We put the protocol byte back into the mblk 4603 * since we may come back via ip_wput_nondata() 4604 * later with this mblk if ipif_lookup_addr chooses 4605 * to defer processing. 4606 */ 4607 *mp->b_wptr++ = (char)connp->conn_ulp; 4608 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, 4609 CONNP_TO_WQ(connp), mp, ip_wput_nondata, 4610 &error, ipst)) != NULL) { 4611 ipif_refrele(ipif); 4612 } else if (error == EINPROGRESS) { 4613 if (src_ire != NULL) 4614 ire_refrele(src_ire); 4615 return (EINPROGRESS); 4616 } else if (CLASSD(src_addr)) { 4617 error = 0; 4618 if (src_ire != NULL) 4619 ire_refrele(src_ire); 4620 /* 4621 * (5) bind to multicast address. 4622 * Fake out the IRE returned to upper 4623 * layer to be a broadcast IRE. 4624 */ 4625 src_ire = ire_ctable_lookup( 4626 INADDR_BROADCAST, INADDR_ANY, 4627 IRE_BROADCAST, NULL, zoneid, NULL, 4628 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY), 4629 ipst); 4630 if (src_ire == NULL || !ire_requested) 4631 error = EADDRNOTAVAIL; 4632 } else { 4633 /* 4634 * Not a valid address for bind 4635 */ 4636 error = EADDRNOTAVAIL; 4637 } 4638 /* 4639 * Just to keep it consistent with the processing in 4640 * ip_bind_v4() 4641 */ 4642 mp->b_wptr--; 4643 } 4644 if (error) { 4645 /* Red Alert! Attempting to be a bogon! */ 4646 ip1dbg(("ip_bind: bad src address 0x%x\n", 4647 ntohl(src_addr))); 4648 goto bad_addr; 4649 } 4650 } 4651 4652 /* 4653 * Allow setting new policies. For example, disconnects come 4654 * down as ipa_t bind. As we would have set conn_policy_cached 4655 * to B_TRUE before, we should set it to B_FALSE, so that policy 4656 * can change after the disconnect. 4657 */ 4658 connp->conn_policy_cached = B_FALSE; 4659 4660 /* 4661 * If not fanout_insert this was just an address verification 4662 */ 4663 if (fanout_insert) { 4664 /* 4665 * The addresses have been verified. Time to insert in 4666 * the correct fanout list. 4667 */ 4668 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4669 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4670 connp->conn_lport = lport; 4671 connp->conn_fport = 0; 4672 /* 4673 * Do we need to add a check to reject Multicast packets 4674 */ 4675 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); 4676 } 4677 4678 if (error == 0) { 4679 if (ire_requested) { 4680 if (!ip_bind_insert_ire(mp, src_ire, NULL, ipst)) { 4681 error = -1; 4682 /* Falls through to bad_addr */ 4683 } 4684 } else if (ipsec_policy_set) { 4685 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4686 error = -1; 4687 /* Falls through to bad_addr */ 4688 } 4689 } 4690 } 4691 bad_addr: 4692 if (error != 0) { 4693 if (connp->conn_anon_port) { 4694 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4695 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 4696 B_FALSE); 4697 } 4698 connp->conn_mlp_type = mlptSingle; 4699 } 4700 if (src_ire != NULL) 4701 IRE_REFRELE(src_ire); 4702 if (ipsec_policy_set) { 4703 ASSERT(policy_mp == mp->b_cont); 4704 ASSERT(policy_mp != NULL); 4705 freeb(policy_mp); 4706 /* 4707 * As of now assume that nothing else accompanies 4708 * IPSEC_POLICY_SET. 4709 */ 4710 mp->b_cont = NULL; 4711 } 4712 return (error); 4713 } 4714 4715 /* 4716 * Verify that both the source and destination addresses 4717 * are valid. If verify_dst is false, then the destination address may be 4718 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4719 * destination reachability, while tunnels do not. 4720 * Note that we allow connect to broadcast and multicast 4721 * addresses when ire_requested is set. Thus the ULP 4722 * has to check for IRE_BROADCAST and multicast. 4723 * 4724 * Returns zero if ok. 4725 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4726 * (for use with TSYSERR reply). 4727 * 4728 * Note: lport and fport are in network byte order. 4729 */ 4730 int 4731 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, 4732 uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4733 boolean_t ire_requested, boolean_t ipsec_policy_set, 4734 boolean_t fanout_insert, boolean_t verify_dst) 4735 { 4736 ire_t *src_ire; 4737 ire_t *dst_ire; 4738 int error = 0; 4739 int protocol; 4740 mblk_t *policy_mp; 4741 ire_t *sire = NULL; 4742 ire_t *md_dst_ire = NULL; 4743 ire_t *lso_dst_ire = NULL; 4744 ill_t *ill = NULL; 4745 zoneid_t zoneid; 4746 ipaddr_t src_addr = *src_addrp; 4747 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4748 4749 src_ire = dst_ire = NULL; 4750 protocol = *mp->b_wptr & 0xFF; 4751 4752 /* 4753 * If we never got a disconnect before, clear it now. 4754 */ 4755 connp->conn_fully_bound = B_FALSE; 4756 4757 if (ipsec_policy_set) { 4758 policy_mp = mp->b_cont; 4759 } 4760 4761 zoneid = IPCL_ZONEID(connp); 4762 4763 if (CLASSD(dst_addr)) { 4764 /* Pick up an IRE_BROADCAST */ 4765 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4766 NULL, zoneid, MBLK_GETLABEL(mp), 4767 (MATCH_IRE_RECURSIVE | 4768 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | 4769 MATCH_IRE_SECATTR), ipst); 4770 } else { 4771 /* 4772 * If conn_dontroute is set or if conn_nexthop_set is set, 4773 * and onlink ipif is not found set ENETUNREACH error. 4774 */ 4775 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4776 ipif_t *ipif; 4777 4778 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4779 dst_addr : connp->conn_nexthop_v4, zoneid, ipst); 4780 if (ipif == NULL) { 4781 error = ENETUNREACH; 4782 goto bad_addr; 4783 } 4784 ipif_refrele(ipif); 4785 } 4786 4787 if (connp->conn_nexthop_set) { 4788 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4789 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), 4790 MATCH_IRE_SECATTR, ipst); 4791 } else { 4792 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4793 &sire, zoneid, MBLK_GETLABEL(mp), 4794 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4795 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | 4796 MATCH_IRE_SECATTR), ipst); 4797 } 4798 } 4799 /* 4800 * dst_ire can't be a broadcast when not ire_requested. 4801 * We also prevent ire's with src address INADDR_ANY to 4802 * be used, which are created temporarily for 4803 * sending out packets from endpoints that have 4804 * conn_unspec_src set. If verify_dst is true, the destination must be 4805 * reachable. If verify_dst is false, the destination needn't be 4806 * reachable. 4807 * 4808 * If we match on a reject or black hole, then we've got a 4809 * local failure. May as well fail out the connect() attempt, 4810 * since it's never going to succeed. 4811 */ 4812 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4813 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4814 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4815 /* 4816 * If we're verifying destination reachability, we always want 4817 * to complain here. 4818 * 4819 * If we're not verifying destination reachability but the 4820 * destination has a route, we still want to fail on the 4821 * temporary address and broadcast address tests. 4822 */ 4823 if (verify_dst || (dst_ire != NULL)) { 4824 if (ip_debug > 2) { 4825 pr_addr_dbg("ip_bind_connected: bad connected " 4826 "dst %s\n", AF_INET, &dst_addr); 4827 } 4828 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4829 error = ENETUNREACH; 4830 else 4831 error = EHOSTUNREACH; 4832 goto bad_addr; 4833 } 4834 } 4835 4836 /* 4837 * We now know that routing will allow us to reach the destination. 4838 * Check whether Trusted Solaris policy allows communication with this 4839 * host, and pretend that the destination is unreachable if not. 4840 * 4841 * This is never a problem for TCP, since that transport is known to 4842 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 4843 * handling. If the remote is unreachable, it will be detected at that 4844 * point, so there's no reason to check it here. 4845 * 4846 * Note that for sendto (and other datagram-oriented friends), this 4847 * check is done as part of the data path label computation instead. 4848 * The check here is just to make non-TCP connect() report the right 4849 * error. 4850 */ 4851 if (dst_ire != NULL && is_system_labeled() && 4852 !IPCL_IS_TCP(connp) && 4853 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL, 4854 connp->conn_mac_exempt, ipst) != 0) { 4855 error = EHOSTUNREACH; 4856 if (ip_debug > 2) { 4857 pr_addr_dbg("ip_bind_connected: no label for dst %s\n", 4858 AF_INET, &dst_addr); 4859 } 4860 goto bad_addr; 4861 } 4862 4863 /* 4864 * If the app does a connect(), it means that it will most likely 4865 * send more than 1 packet to the destination. It makes sense 4866 * to clear the temporary flag. 4867 */ 4868 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4869 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4870 irb_t *irb = dst_ire->ire_bucket; 4871 4872 rw_enter(&irb->irb_lock, RW_WRITER); 4873 /* 4874 * We need to recheck for IRE_MARK_TEMPORARY after acquiring 4875 * the lock to guarantee irb_tmp_ire_cnt. 4876 */ 4877 if (dst_ire->ire_marks & IRE_MARK_TEMPORARY) { 4878 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4879 irb->irb_tmp_ire_cnt--; 4880 } 4881 rw_exit(&irb->irb_lock); 4882 } 4883 4884 /* 4885 * See if we should notify ULP about LSO/MDT; we do this whether or not 4886 * ire_requested is TRUE, in order to handle active connects; LSO/MDT 4887 * eligibility tests for passive connects are handled separately 4888 * through tcp_adapt_ire(). We do this before the source address 4889 * selection, because dst_ire may change after a call to 4890 * ipif_select_source(). This is a best-effort check, as the 4891 * packet for this connection may not actually go through 4892 * dst_ire->ire_stq, and the exact IRE can only be known after 4893 * calling ip_newroute(). This is why we further check on the 4894 * IRE during LSO/Multidata packet transmission in 4895 * tcp_lsosend()/tcp_multisend(). 4896 */ 4897 if (!ipsec_policy_set && dst_ire != NULL && 4898 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4899 (ill = ire_to_ill(dst_ire), ill != NULL)) { 4900 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 4901 lso_dst_ire = dst_ire; 4902 IRE_REFHOLD(lso_dst_ire); 4903 } else if (ipst->ips_ip_multidata_outbound && 4904 ILL_MDT_CAPABLE(ill)) { 4905 md_dst_ire = dst_ire; 4906 IRE_REFHOLD(md_dst_ire); 4907 } 4908 } 4909 4910 if (dst_ire != NULL && 4911 dst_ire->ire_type == IRE_LOCAL && 4912 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { 4913 /* 4914 * If the IRE belongs to a different zone, look for a matching 4915 * route in the forwarding table and use the source address from 4916 * that route. 4917 */ 4918 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 4919 zoneid, 0, NULL, 4920 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4921 MATCH_IRE_RJ_BHOLE, ipst); 4922 if (src_ire == NULL) { 4923 error = EHOSTUNREACH; 4924 goto bad_addr; 4925 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 4926 if (!(src_ire->ire_type & IRE_HOST)) 4927 error = ENETUNREACH; 4928 else 4929 error = EHOSTUNREACH; 4930 goto bad_addr; 4931 } 4932 if (src_addr == INADDR_ANY) 4933 src_addr = src_ire->ire_src_addr; 4934 ire_refrele(src_ire); 4935 src_ire = NULL; 4936 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 4937 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 4938 src_addr = sire->ire_src_addr; 4939 ire_refrele(dst_ire); 4940 dst_ire = sire; 4941 sire = NULL; 4942 } else { 4943 /* 4944 * Pick a source address so that a proper inbound 4945 * load spreading would happen. 4946 */ 4947 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; 4948 ipif_t *src_ipif = NULL; 4949 ire_t *ipif_ire; 4950 4951 /* 4952 * Supply a local source address such that inbound 4953 * load spreading happens. 4954 * 4955 * Determine the best source address on this ill for 4956 * the destination. 4957 * 4958 * 1) For broadcast, we should return a broadcast ire 4959 * found above so that upper layers know that the 4960 * destination address is a broadcast address. 4961 * 4962 * 2) If this is part of a group, select a better 4963 * source address so that better inbound load 4964 * balancing happens. Do the same if the ipif 4965 * is DEPRECATED. 4966 * 4967 * 3) If the outgoing interface is part of a usesrc 4968 * group, then try selecting a source address from 4969 * the usesrc ILL. 4970 */ 4971 if ((dst_ire->ire_zoneid != zoneid && 4972 dst_ire->ire_zoneid != ALL_ZONES) || 4973 (!(dst_ire->ire_flags & RTF_SETSRC)) && 4974 (!(dst_ire->ire_type & IRE_BROADCAST) && 4975 ((dst_ill->ill_group != NULL) || 4976 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 4977 (dst_ill->ill_usesrc_ifindex != 0)))) { 4978 /* 4979 * If the destination is reachable via a 4980 * given gateway, the selected source address 4981 * should be in the same subnet as the gateway. 4982 * Otherwise, the destination is not reachable. 4983 * 4984 * If there are no interfaces on the same subnet 4985 * as the destination, ipif_select_source gives 4986 * first non-deprecated interface which might be 4987 * on a different subnet than the gateway. 4988 * This is not desirable. Hence pass the dst_ire 4989 * source address to ipif_select_source. 4990 * It is sure that the destination is reachable 4991 * with the dst_ire source address subnet. 4992 * So passing dst_ire source address to 4993 * ipif_select_source will make sure that the 4994 * selected source will be on the same subnet 4995 * as dst_ire source address. 4996 */ 4997 ipaddr_t saddr = 4998 dst_ire->ire_ipif->ipif_src_addr; 4999 src_ipif = ipif_select_source(dst_ill, 5000 saddr, zoneid); 5001 if (src_ipif != NULL) { 5002 if (IS_VNI(src_ipif->ipif_ill)) { 5003 /* 5004 * For VNI there is no 5005 * interface route 5006 */ 5007 src_addr = 5008 src_ipif->ipif_src_addr; 5009 } else { 5010 ipif_ire = 5011 ipif_to_ire(src_ipif); 5012 if (ipif_ire != NULL) { 5013 IRE_REFRELE(dst_ire); 5014 dst_ire = ipif_ire; 5015 } 5016 src_addr = 5017 dst_ire->ire_src_addr; 5018 } 5019 ipif_refrele(src_ipif); 5020 } else { 5021 src_addr = dst_ire->ire_src_addr; 5022 } 5023 } else { 5024 src_addr = dst_ire->ire_src_addr; 5025 } 5026 } 5027 } 5028 5029 /* 5030 * We do ire_route_lookup() here (and not 5031 * interface lookup as we assert that 5032 * src_addr should only come from an 5033 * UP interface for hard binding. 5034 */ 5035 ASSERT(src_ire == NULL); 5036 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 5037 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 5038 /* src_ire must be a local|loopback */ 5039 if (!IRE_IS_LOCAL(src_ire)) { 5040 if (ip_debug > 2) { 5041 pr_addr_dbg("ip_bind_connected: bad connected " 5042 "src %s\n", AF_INET, &src_addr); 5043 } 5044 error = EADDRNOTAVAIL; 5045 goto bad_addr; 5046 } 5047 5048 /* 5049 * If the source address is a loopback address, the 5050 * destination had best be local or multicast. 5051 * The transports that can't handle multicast will reject 5052 * those addresses. 5053 */ 5054 if (src_ire->ire_type == IRE_LOOPBACK && 5055 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 5056 ip1dbg(("ip_bind_connected: bad connected loopback\n")); 5057 error = -1; 5058 goto bad_addr; 5059 } 5060 5061 /* 5062 * Allow setting new policies. For example, disconnects come 5063 * down as ipa_t bind. As we would have set conn_policy_cached 5064 * to B_TRUE before, we should set it to B_FALSE, so that policy 5065 * can change after the disconnect. 5066 */ 5067 connp->conn_policy_cached = B_FALSE; 5068 5069 /* 5070 * Set the conn addresses/ports immediately, so the IPsec policy calls 5071 * can handle their passed-in conn's. 5072 */ 5073 5074 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 5075 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 5076 connp->conn_lport = lport; 5077 connp->conn_fport = fport; 5078 *src_addrp = src_addr; 5079 5080 ASSERT(!(ipsec_policy_set && ire_requested)); 5081 if (ire_requested) { 5082 iulp_t *ulp_info = NULL; 5083 5084 /* 5085 * Note that sire will not be NULL if this is an off-link 5086 * connection and there is not cache for that dest yet. 5087 * 5088 * XXX Because of an existing bug, if there are multiple 5089 * default routes, the IRE returned now may not be the actual 5090 * default route used (default routes are chosen in a 5091 * round robin fashion). So if the metrics for different 5092 * default routes are different, we may return the wrong 5093 * metrics. This will not be a problem if the existing 5094 * bug is fixed. 5095 */ 5096 if (sire != NULL) { 5097 ulp_info = &(sire->ire_uinfo); 5098 } 5099 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info, ipst)) { 5100 error = -1; 5101 goto bad_addr; 5102 } 5103 } else if (ipsec_policy_set) { 5104 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 5105 error = -1; 5106 goto bad_addr; 5107 } 5108 } 5109 5110 /* 5111 * Cache IPsec policy in this conn. If we have per-socket policy, 5112 * we'll cache that. If we don't, we'll inherit global policy. 5113 * 5114 * We can't insert until the conn reflects the policy. Note that 5115 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 5116 * connections where we don't have a policy. This is to prevent 5117 * global policy lookups in the inbound path. 5118 * 5119 * If we insert before we set conn_policy_cached, 5120 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 5121 * because global policy cound be non-empty. We normally call 5122 * ipsec_check_policy() for conn_policy_cached connections only if 5123 * ipc_in_enforce_policy is set. But in this case, 5124 * conn_policy_cached can get set anytime since we made the 5125 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 5126 * called, which will make the above assumption false. Thus, we 5127 * need to insert after we set conn_policy_cached. 5128 */ 5129 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 5130 goto bad_addr; 5131 5132 if (fanout_insert) { 5133 /* 5134 * The addresses have been verified. Time to insert in 5135 * the correct fanout list. 5136 */ 5137 error = ipcl_conn_insert(connp, protocol, src_addr, 5138 dst_addr, connp->conn_ports); 5139 } 5140 5141 if (error == 0) { 5142 connp->conn_fully_bound = B_TRUE; 5143 /* 5144 * Our initial checks for LSO/MDT have passed; the IRE is not 5145 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 5146 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into 5147 * ip_xxinfo_return(), which performs further checks 5148 * against them and upon success, returns the LSO/MDT info 5149 * mblk which we will attach to the bind acknowledgment. 5150 */ 5151 if (lso_dst_ire != NULL) { 5152 mblk_t *lsoinfo_mp; 5153 5154 ASSERT(ill->ill_lso_capab != NULL); 5155 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp, 5156 ill->ill_name, ill->ill_lso_capab)) != NULL) 5157 linkb(mp, lsoinfo_mp); 5158 } else if (md_dst_ire != NULL) { 5159 mblk_t *mdinfo_mp; 5160 5161 ASSERT(ill->ill_mdt_capab != NULL); 5162 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 5163 ill->ill_name, ill->ill_mdt_capab)) != NULL) 5164 linkb(mp, mdinfo_mp); 5165 } 5166 } 5167 bad_addr: 5168 if (ipsec_policy_set) { 5169 ASSERT(policy_mp == mp->b_cont); 5170 ASSERT(policy_mp != NULL); 5171 freeb(policy_mp); 5172 /* 5173 * As of now assume that nothing else accompanies 5174 * IPSEC_POLICY_SET. 5175 */ 5176 mp->b_cont = NULL; 5177 } 5178 if (src_ire != NULL) 5179 IRE_REFRELE(src_ire); 5180 if (dst_ire != NULL) 5181 IRE_REFRELE(dst_ire); 5182 if (sire != NULL) 5183 IRE_REFRELE(sire); 5184 if (md_dst_ire != NULL) 5185 IRE_REFRELE(md_dst_ire); 5186 if (lso_dst_ire != NULL) 5187 IRE_REFRELE(lso_dst_ire); 5188 return (error); 5189 } 5190 5191 /* 5192 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 5193 * Prefers dst_ire over src_ire. 5194 */ 5195 static boolean_t 5196 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst) 5197 { 5198 mblk_t *mp1; 5199 ire_t *ret_ire = NULL; 5200 5201 mp1 = mp->b_cont; 5202 ASSERT(mp1 != NULL); 5203 5204 if (ire != NULL) { 5205 /* 5206 * mp1 initialized above to IRE_DB_REQ_TYPE 5207 * appended mblk. Its <upper protocol>'s 5208 * job to make sure there is room. 5209 */ 5210 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 5211 return (0); 5212 5213 mp1->b_datap->db_type = IRE_DB_TYPE; 5214 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 5215 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 5216 ret_ire = (ire_t *)mp1->b_rptr; 5217 /* 5218 * Pass the latest setting of the ip_path_mtu_discovery and 5219 * copy the ulp info if any. 5220 */ 5221 ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? 5222 IPH_DF : 0; 5223 if (ulp_info != NULL) { 5224 bcopy(ulp_info, &(ret_ire->ire_uinfo), 5225 sizeof (iulp_t)); 5226 } 5227 ret_ire->ire_mp = mp1; 5228 } else { 5229 /* 5230 * No IRE was found. Remove IRE mblk. 5231 */ 5232 mp->b_cont = mp1->b_cont; 5233 freeb(mp1); 5234 } 5235 5236 return (1); 5237 } 5238 5239 /* 5240 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 5241 * the final piece where we don't. Return a pointer to the first mblk in the 5242 * result, and update the pointer to the next mblk to chew on. If anything 5243 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 5244 * NULL pointer. 5245 */ 5246 mblk_t * 5247 ip_carve_mp(mblk_t **mpp, ssize_t len) 5248 { 5249 mblk_t *mp0; 5250 mblk_t *mp1; 5251 mblk_t *mp2; 5252 5253 if (!len || !mpp || !(mp0 = *mpp)) 5254 return (NULL); 5255 /* If we aren't going to consume the first mblk, we need a dup. */ 5256 if (mp0->b_wptr - mp0->b_rptr > len) { 5257 mp1 = dupb(mp0); 5258 if (mp1) { 5259 /* Partition the data between the two mblks. */ 5260 mp1->b_wptr = mp1->b_rptr + len; 5261 mp0->b_rptr = mp1->b_wptr; 5262 /* 5263 * after adjustments if mblk not consumed is now 5264 * unaligned, try to align it. If this fails free 5265 * all messages and let upper layer recover. 5266 */ 5267 if (!OK_32PTR(mp0->b_rptr)) { 5268 if (!pullupmsg(mp0, -1)) { 5269 freemsg(mp0); 5270 freemsg(mp1); 5271 *mpp = NULL; 5272 return (NULL); 5273 } 5274 } 5275 } 5276 return (mp1); 5277 } 5278 /* Eat through as many mblks as we need to get len bytes. */ 5279 len -= mp0->b_wptr - mp0->b_rptr; 5280 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 5281 if (mp2->b_wptr - mp2->b_rptr > len) { 5282 /* 5283 * We won't consume the entire last mblk. Like 5284 * above, dup and partition it. 5285 */ 5286 mp1->b_cont = dupb(mp2); 5287 mp1 = mp1->b_cont; 5288 if (!mp1) { 5289 /* 5290 * Trouble. Rather than go to a lot of 5291 * trouble to clean up, we free the messages. 5292 * This won't be any worse than losing it on 5293 * the wire. 5294 */ 5295 freemsg(mp0); 5296 freemsg(mp2); 5297 *mpp = NULL; 5298 return (NULL); 5299 } 5300 mp1->b_wptr = mp1->b_rptr + len; 5301 mp2->b_rptr = mp1->b_wptr; 5302 /* 5303 * after adjustments if mblk not consumed is now 5304 * unaligned, try to align it. If this fails free 5305 * all messages and let upper layer recover. 5306 */ 5307 if (!OK_32PTR(mp2->b_rptr)) { 5308 if (!pullupmsg(mp2, -1)) { 5309 freemsg(mp0); 5310 freemsg(mp2); 5311 *mpp = NULL; 5312 return (NULL); 5313 } 5314 } 5315 *mpp = mp2; 5316 return (mp0); 5317 } 5318 /* Decrement len by the amount we just got. */ 5319 len -= mp2->b_wptr - mp2->b_rptr; 5320 } 5321 /* 5322 * len should be reduced to zero now. If not our caller has 5323 * screwed up. 5324 */ 5325 if (len) { 5326 /* Shouldn't happen! */ 5327 freemsg(mp0); 5328 *mpp = NULL; 5329 return (NULL); 5330 } 5331 /* 5332 * We consumed up to exactly the end of an mblk. Detach the part 5333 * we are returning from the rest of the chain. 5334 */ 5335 mp1->b_cont = NULL; 5336 *mpp = mp2; 5337 return (mp0); 5338 } 5339 5340 /* The ill stream is being unplumbed. Called from ip_close */ 5341 int 5342 ip_modclose(ill_t *ill) 5343 { 5344 boolean_t success; 5345 ipsq_t *ipsq; 5346 ipif_t *ipif; 5347 queue_t *q = ill->ill_rq; 5348 ip_stack_t *ipst = ill->ill_ipst; 5349 clock_t timeout; 5350 5351 /* 5352 * Wait for the ACKs of all deferred control messages to be processed. 5353 * In particular, we wait for a potential capability reset initiated 5354 * in ip_sioctl_plink() to complete before proceeding. 5355 * 5356 * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms) 5357 * in case the driver never replies. 5358 */ 5359 timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms); 5360 mutex_enter(&ill->ill_lock); 5361 while (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 5362 if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) { 5363 /* Timeout */ 5364 break; 5365 } 5366 } 5367 mutex_exit(&ill->ill_lock); 5368 5369 /* 5370 * Forcibly enter the ipsq after some delay. This is to take 5371 * care of the case when some ioctl does not complete because 5372 * we sent a control message to the driver and it did not 5373 * send us a reply. We want to be able to at least unplumb 5374 * and replumb rather than force the user to reboot the system. 5375 */ 5376 success = ipsq_enter(ill, B_FALSE); 5377 5378 /* 5379 * Open/close/push/pop is guaranteed to be single threaded 5380 * per stream by STREAMS. FS guarantees that all references 5381 * from top are gone before close is called. So there can't 5382 * be another close thread that has set CONDEMNED on this ill. 5383 * and cause ipsq_enter to return failure. 5384 */ 5385 ASSERT(success); 5386 ipsq = ill->ill_phyint->phyint_ipsq; 5387 5388 /* 5389 * Mark it condemned. No new reference will be made to this ill. 5390 * Lookup functions will return an error. Threads that try to 5391 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 5392 * that the refcnt will drop down to zero. 5393 */ 5394 mutex_enter(&ill->ill_lock); 5395 ill->ill_state_flags |= ILL_CONDEMNED; 5396 for (ipif = ill->ill_ipif; ipif != NULL; 5397 ipif = ipif->ipif_next) { 5398 ipif->ipif_state_flags |= IPIF_CONDEMNED; 5399 } 5400 /* 5401 * Wake up anybody waiting to enter the ipsq. ipsq_enter 5402 * returns error if ILL_CONDEMNED is set 5403 */ 5404 cv_broadcast(&ill->ill_cv); 5405 mutex_exit(&ill->ill_lock); 5406 5407 /* 5408 * Send all the deferred DLPI messages downstream which came in 5409 * during the small window right before ipsq_enter(). We do this 5410 * without waiting for the ACKs because all the ACKs for M_PROTO 5411 * messages are ignored in ip_rput() when ILL_CONDEMNED is set. 5412 */ 5413 ill_dlpi_send_deferred(ill); 5414 5415 /* 5416 * Shut down fragmentation reassembly. 5417 * ill_frag_timer won't start a timer again. 5418 * Now cancel any existing timer 5419 */ 5420 (void) untimeout(ill->ill_frag_timer_id); 5421 (void) ill_frag_timeout(ill, 0); 5422 5423 /* 5424 * If MOVE was in progress, clear the 5425 * move_in_progress fields also. 5426 */ 5427 if (ill->ill_move_in_progress) { 5428 ILL_CLEAR_MOVE(ill); 5429 } 5430 5431 /* 5432 * Call ill_delete to bring down the ipifs, ilms and ill on 5433 * this ill. Then wait for the refcnts to drop to zero. 5434 * ill_is_quiescent checks whether the ill is really quiescent. 5435 * Then make sure that threads that are waiting to enter the 5436 * ipsq have seen the error returned by ipsq_enter and have 5437 * gone away. Then we call ill_delete_tail which does the 5438 * DL_UNBIND_REQ with the driver and then qprocsoff. 5439 */ 5440 ill_delete(ill); 5441 mutex_enter(&ill->ill_lock); 5442 while (!ill_is_quiescent(ill)) 5443 cv_wait(&ill->ill_cv, &ill->ill_lock); 5444 while (ill->ill_waiters) 5445 cv_wait(&ill->ill_cv, &ill->ill_lock); 5446 5447 mutex_exit(&ill->ill_lock); 5448 5449 /* 5450 * ill_delete_tail drops reference on ill_ipst, but we need to keep 5451 * it held until the end of the function since the cleanup 5452 * below needs to be able to use the ip_stack_t. 5453 */ 5454 netstack_hold(ipst->ips_netstack); 5455 5456 /* qprocsoff is called in ill_delete_tail */ 5457 ill_delete_tail(ill); 5458 ASSERT(ill->ill_ipst == NULL); 5459 5460 /* 5461 * Walk through all upper (conn) streams and qenable 5462 * those that have queued data. 5463 * close synchronization needs this to 5464 * be done to ensure that all upper layers blocked 5465 * due to flow control to the closing device 5466 * get unblocked. 5467 */ 5468 ip1dbg(("ip_wsrv: walking\n")); 5469 conn_walk_drain(ipst); 5470 5471 mutex_enter(&ipst->ips_ip_mi_lock); 5472 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill); 5473 mutex_exit(&ipst->ips_ip_mi_lock); 5474 5475 /* 5476 * credp could be null if the open didn't succeed and ip_modopen 5477 * itself calls ip_close. 5478 */ 5479 if (ill->ill_credp != NULL) 5480 crfree(ill->ill_credp); 5481 5482 mutex_enter(&ill->ill_lock); 5483 ill_nic_info_dispatch(ill); 5484 mutex_exit(&ill->ill_lock); 5485 5486 /* 5487 * Now we are done with the module close pieces that 5488 * need the netstack_t. 5489 */ 5490 netstack_rele(ipst->ips_netstack); 5491 5492 mi_close_free((IDP)ill); 5493 q->q_ptr = WR(q)->q_ptr = NULL; 5494 5495 ipsq_exit(ipsq, B_TRUE, B_TRUE); 5496 5497 return (0); 5498 } 5499 5500 /* 5501 * This is called as part of close() for IP, UDP, ICMP, and RTS 5502 * in order to quiesce the conn. 5503 */ 5504 void 5505 ip_quiesce_conn(conn_t *connp) 5506 { 5507 boolean_t drain_cleanup_reqd = B_FALSE; 5508 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 5509 boolean_t ilg_cleanup_reqd = B_FALSE; 5510 ip_stack_t *ipst; 5511 5512 ASSERT(!IPCL_IS_TCP(connp)); 5513 ipst = connp->conn_netstack->netstack_ip; 5514 5515 /* 5516 * Mark the conn as closing, and this conn must not be 5517 * inserted in future into any list. Eg. conn_drain_insert(), 5518 * won't insert this conn into the conn_drain_list. 5519 * Similarly ill_pending_mp_add() will not add any mp to 5520 * the pending mp list, after this conn has started closing. 5521 * 5522 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 5523 * cannot get set henceforth. 5524 */ 5525 mutex_enter(&connp->conn_lock); 5526 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 5527 connp->conn_state_flags |= CONN_CLOSING; 5528 if (connp->conn_idl != NULL) 5529 drain_cleanup_reqd = B_TRUE; 5530 if (connp->conn_oper_pending_ill != NULL) 5531 conn_ioctl_cleanup_reqd = B_TRUE; 5532 if (connp->conn_dhcpinit_ill != NULL) { 5533 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0); 5534 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit); 5535 connp->conn_dhcpinit_ill = NULL; 5536 } 5537 if (connp->conn_ilg_inuse != 0) 5538 ilg_cleanup_reqd = B_TRUE; 5539 mutex_exit(&connp->conn_lock); 5540 5541 if (conn_ioctl_cleanup_reqd) 5542 conn_ioctl_cleanup(connp); 5543 5544 if (is_system_labeled() && connp->conn_anon_port) { 5545 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 5546 connp->conn_mlp_type, connp->conn_ulp, 5547 ntohs(connp->conn_lport), B_FALSE); 5548 connp->conn_anon_port = 0; 5549 } 5550 connp->conn_mlp_type = mlptSingle; 5551 5552 /* 5553 * Remove this conn from any fanout list it is on. 5554 * and then wait for any threads currently operating 5555 * on this endpoint to finish 5556 */ 5557 ipcl_hash_remove(connp); 5558 5559 /* 5560 * Remove this conn from the drain list, and do 5561 * any other cleanup that may be required. 5562 * (Only non-tcp streams may have a non-null conn_idl. 5563 * TCP streams are never flow controlled, and 5564 * conn_idl will be null) 5565 */ 5566 if (drain_cleanup_reqd) 5567 conn_drain_tail(connp, B_TRUE); 5568 5569 if (connp == ipst->ips_ip_g_mrouter) 5570 (void) ip_mrouter_done(NULL, ipst); 5571 5572 if (ilg_cleanup_reqd) 5573 ilg_delete_all(connp); 5574 5575 conn_delete_ire(connp, NULL); 5576 5577 /* 5578 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 5579 * callers from write side can't be there now because close 5580 * is in progress. The only other caller is ipcl_walk 5581 * which checks for the condemned flag. 5582 */ 5583 mutex_enter(&connp->conn_lock); 5584 connp->conn_state_flags |= CONN_CONDEMNED; 5585 while (connp->conn_ref != 1) 5586 cv_wait(&connp->conn_cv, &connp->conn_lock); 5587 connp->conn_state_flags |= CONN_QUIESCED; 5588 mutex_exit(&connp->conn_lock); 5589 } 5590 5591 /* ARGSUSED */ 5592 int 5593 ip_close(queue_t *q, int flags) 5594 { 5595 conn_t *connp; 5596 5597 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 5598 5599 /* 5600 * Call the appropriate delete routine depending on whether this is 5601 * a module or device. 5602 */ 5603 if (WR(q)->q_next != NULL) { 5604 /* This is a module close */ 5605 return (ip_modclose((ill_t *)q->q_ptr)); 5606 } 5607 5608 connp = q->q_ptr; 5609 ip_quiesce_conn(connp); 5610 5611 qprocsoff(q); 5612 5613 /* 5614 * Now we are truly single threaded on this stream, and can 5615 * delete the things hanging off the connp, and finally the connp. 5616 * We removed this connp from the fanout list, it cannot be 5617 * accessed thru the fanouts, and we already waited for the 5618 * conn_ref to drop to 0. We are already in close, so 5619 * there cannot be any other thread from the top. qprocsoff 5620 * has completed, and service has completed or won't run in 5621 * future. 5622 */ 5623 ASSERT(connp->conn_ref == 1); 5624 5625 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 5626 5627 connp->conn_ref--; 5628 ipcl_conn_destroy(connp); 5629 5630 q->q_ptr = WR(q)->q_ptr = NULL; 5631 return (0); 5632 } 5633 5634 /* 5635 * Wapper around putnext() so that ip_rts_request can merely use 5636 * conn_recv. 5637 */ 5638 /*ARGSUSED2*/ 5639 static void 5640 ip_conn_input(void *arg1, mblk_t *mp, void *arg2) 5641 { 5642 conn_t *connp = (conn_t *)arg1; 5643 5644 putnext(connp->conn_rq, mp); 5645 } 5646 5647 /* Return the IP checksum for the IP header at "iph". */ 5648 uint16_t 5649 ip_csum_hdr(ipha_t *ipha) 5650 { 5651 uint16_t *uph; 5652 uint32_t sum; 5653 int opt_len; 5654 5655 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - 5656 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 5657 uph = (uint16_t *)ipha; 5658 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 5659 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 5660 if (opt_len > 0) { 5661 do { 5662 sum += uph[10]; 5663 sum += uph[11]; 5664 uph += 2; 5665 } while (--opt_len); 5666 } 5667 sum = (sum & 0xFFFF) + (sum >> 16); 5668 sum = ~(sum + (sum >> 16)) & 0xFFFF; 5669 if (sum == 0xffff) 5670 sum = 0; 5671 return ((uint16_t)sum); 5672 } 5673 5674 /* 5675 * Called when the module is about to be unloaded 5676 */ 5677 void 5678 ip_ddi_destroy(void) 5679 { 5680 tnet_fini(); 5681 5682 icmp_ddi_destroy(); 5683 rts_ddi_destroy(); 5684 udp_ddi_destroy(); 5685 sctp_ddi_g_destroy(); 5686 tcp_ddi_g_destroy(); 5687 ipsec_policy_g_destroy(); 5688 ipcl_g_destroy(); 5689 ip_net_g_destroy(); 5690 ip_ire_g_fini(); 5691 inet_minor_destroy(ip_minor_arena_sa); 5692 #if defined(_LP64) 5693 inet_minor_destroy(ip_minor_arena_la); 5694 #endif 5695 5696 #ifdef DEBUG 5697 list_destroy(&ip_thread_list); 5698 rw_destroy(&ip_thread_rwlock); 5699 tsd_destroy(&ip_thread_data); 5700 #endif 5701 5702 netstack_unregister(NS_IP); 5703 } 5704 5705 /* 5706 * First step in cleanup. 5707 */ 5708 /* ARGSUSED */ 5709 static void 5710 ip_stack_shutdown(netstackid_t stackid, void *arg) 5711 { 5712 ip_stack_t *ipst = (ip_stack_t *)arg; 5713 5714 #ifdef NS_DEBUG 5715 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid); 5716 #endif 5717 5718 /* Get rid of loopback interfaces and their IREs */ 5719 ip_loopback_cleanup(ipst); 5720 } 5721 5722 /* 5723 * Free the IP stack instance. 5724 */ 5725 static void 5726 ip_stack_fini(netstackid_t stackid, void *arg) 5727 { 5728 ip_stack_t *ipst = (ip_stack_t *)arg; 5729 int ret; 5730 5731 #ifdef NS_DEBUG 5732 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid); 5733 #endif 5734 ipv4_hook_destroy(ipst); 5735 ipv6_hook_destroy(ipst); 5736 ip_net_destroy(ipst); 5737 5738 rw_destroy(&ipst->ips_srcid_lock); 5739 5740 ip_kstat_fini(stackid, ipst->ips_ip_mibkp); 5741 ipst->ips_ip_mibkp = NULL; 5742 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp); 5743 ipst->ips_icmp_mibkp = NULL; 5744 ip_kstat2_fini(stackid, ipst->ips_ip_kstat); 5745 ipst->ips_ip_kstat = NULL; 5746 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics)); 5747 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat); 5748 ipst->ips_ip6_kstat = NULL; 5749 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics)); 5750 5751 nd_free(&ipst->ips_ip_g_nd); 5752 kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr)); 5753 ipst->ips_param_arr = NULL; 5754 kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 5755 ipst->ips_ndp_arr = NULL; 5756 5757 ip_mrouter_stack_destroy(ipst); 5758 5759 mutex_destroy(&ipst->ips_ip_mi_lock); 5760 rw_destroy(&ipst->ips_ipsec_capab_ills_lock); 5761 rw_destroy(&ipst->ips_ill_g_usesrc_lock); 5762 rw_destroy(&ipst->ips_ip_g_nd_lock); 5763 5764 ret = untimeout(ipst->ips_igmp_timeout_id); 5765 if (ret == -1) { 5766 ASSERT(ipst->ips_igmp_timeout_id == 0); 5767 } else { 5768 ASSERT(ipst->ips_igmp_timeout_id != 0); 5769 ipst->ips_igmp_timeout_id = 0; 5770 } 5771 ret = untimeout(ipst->ips_igmp_slowtimeout_id); 5772 if (ret == -1) { 5773 ASSERT(ipst->ips_igmp_slowtimeout_id == 0); 5774 } else { 5775 ASSERT(ipst->ips_igmp_slowtimeout_id != 0); 5776 ipst->ips_igmp_slowtimeout_id = 0; 5777 } 5778 ret = untimeout(ipst->ips_mld_timeout_id); 5779 if (ret == -1) { 5780 ASSERT(ipst->ips_mld_timeout_id == 0); 5781 } else { 5782 ASSERT(ipst->ips_mld_timeout_id != 0); 5783 ipst->ips_mld_timeout_id = 0; 5784 } 5785 ret = untimeout(ipst->ips_mld_slowtimeout_id); 5786 if (ret == -1) { 5787 ASSERT(ipst->ips_mld_slowtimeout_id == 0); 5788 } else { 5789 ASSERT(ipst->ips_mld_slowtimeout_id != 0); 5790 ipst->ips_mld_slowtimeout_id = 0; 5791 } 5792 ret = untimeout(ipst->ips_ip_ire_expire_id); 5793 if (ret == -1) { 5794 ASSERT(ipst->ips_ip_ire_expire_id == 0); 5795 } else { 5796 ASSERT(ipst->ips_ip_ire_expire_id != 0); 5797 ipst->ips_ip_ire_expire_id = 0; 5798 } 5799 5800 mutex_destroy(&ipst->ips_igmp_timer_lock); 5801 mutex_destroy(&ipst->ips_mld_timer_lock); 5802 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock); 5803 mutex_destroy(&ipst->ips_mld_slowtimeout_lock); 5804 mutex_destroy(&ipst->ips_ip_addr_avail_lock); 5805 rw_destroy(&ipst->ips_ill_g_lock); 5806 5807 ip_ire_fini(ipst); 5808 ip6_asp_free(ipst); 5809 conn_drain_fini(ipst); 5810 ipcl_destroy(ipst); 5811 5812 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock); 5813 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock); 5814 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t)); 5815 ipst->ips_ndp4 = NULL; 5816 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t)); 5817 ipst->ips_ndp6 = NULL; 5818 5819 if (ipst->ips_loopback_ksp != NULL) { 5820 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid); 5821 ipst->ips_loopback_ksp = NULL; 5822 } 5823 5824 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t)); 5825 ipst->ips_phyint_g_list = NULL; 5826 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS); 5827 ipst->ips_ill_g_heads = NULL; 5828 5829 kmem_free(ipst, sizeof (*ipst)); 5830 } 5831 5832 /* 5833 * This function is called from the TSD destructor, and is used to debug 5834 * reference count issues in IP. See block comment in <inet/ip_if.h> for 5835 * details. 5836 */ 5837 static void 5838 ip_thread_exit(void *phash) 5839 { 5840 th_hash_t *thh = phash; 5841 5842 rw_enter(&ip_thread_rwlock, RW_WRITER); 5843 list_remove(&ip_thread_list, thh); 5844 rw_exit(&ip_thread_rwlock); 5845 mod_hash_destroy_hash(thh->thh_hash); 5846 kmem_free(thh, sizeof (*thh)); 5847 } 5848 5849 /* 5850 * Called when the IP kernel module is loaded into the kernel 5851 */ 5852 void 5853 ip_ddi_init(void) 5854 { 5855 ip_input_proc = ip_squeue_switch(ip_squeue_enter); 5856 5857 /* 5858 * For IP and TCP the minor numbers should start from 2 since we have 4 5859 * initial devices: ip, ip6, tcp, tcp6. 5860 */ 5861 /* 5862 * If this is a 64-bit kernel, then create two separate arenas - 5863 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the 5864 * other for socket apps in the range 2^^18 through 2^^32-1. 5865 */ 5866 ip_minor_arena_la = NULL; 5867 ip_minor_arena_sa = NULL; 5868 #if defined(_LP64) 5869 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", 5870 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) { 5871 cmn_err(CE_PANIC, 5872 "ip_ddi_init: ip_minor_arena_sa creation failed\n"); 5873 } 5874 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la", 5875 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) { 5876 cmn_err(CE_PANIC, 5877 "ip_ddi_init: ip_minor_arena_la creation failed\n"); 5878 } 5879 #else 5880 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", 5881 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) { 5882 cmn_err(CE_PANIC, 5883 "ip_ddi_init: ip_minor_arena_sa creation failed\n"); 5884 } 5885 #endif 5886 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5887 5888 ipcl_g_init(); 5889 ip_ire_g_init(); 5890 ip_net_g_init(); 5891 5892 #ifdef DEBUG 5893 tsd_create(&ip_thread_data, ip_thread_exit); 5894 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL); 5895 list_create(&ip_thread_list, sizeof (th_hash_t), 5896 offsetof(th_hash_t, thh_link)); 5897 #endif 5898 5899 /* 5900 * We want to be informed each time a stack is created or 5901 * destroyed in the kernel, so we can maintain the 5902 * set of udp_stack_t's. 5903 */ 5904 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown, 5905 ip_stack_fini); 5906 5907 ipsec_policy_g_init(); 5908 tcp_ddi_g_init(); 5909 sctp_ddi_g_init(); 5910 5911 tnet_init(); 5912 5913 udp_ddi_init(); 5914 rts_ddi_init(); 5915 icmp_ddi_init(); 5916 } 5917 5918 /* 5919 * Initialize the IP stack instance. 5920 */ 5921 static void * 5922 ip_stack_init(netstackid_t stackid, netstack_t *ns) 5923 { 5924 ip_stack_t *ipst; 5925 ipparam_t *pa; 5926 ipndp_t *na; 5927 5928 #ifdef NS_DEBUG 5929 printf("ip_stack_init(stack %d)\n", stackid); 5930 #endif 5931 5932 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP); 5933 ipst->ips_netstack = ns; 5934 5935 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS, 5936 KM_SLEEP); 5937 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t), 5938 KM_SLEEP); 5939 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5940 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5941 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 5942 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 5943 5944 rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 5945 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5946 ipst->ips_igmp_deferred_next = INFINITY; 5947 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5948 ipst->ips_mld_deferred_next = INFINITY; 5949 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5950 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5951 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 5952 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 5953 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL); 5954 rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 5955 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 5956 5957 ipcl_init(ipst); 5958 ip_ire_init(ipst); 5959 ip6_asp_init(ipst); 5960 ipif_init(ipst); 5961 conn_drain_init(ipst); 5962 ip_mrouter_stack_init(ipst); 5963 5964 ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT; 5965 ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 5966 5967 ipst->ips_ip_multirt_log_interval = 1000; 5968 5969 ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT; 5970 ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT; 5971 ipst->ips_ill_index = 1; 5972 5973 ipst->ips_saved_ip_g_forward = -1; 5974 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */ 5975 5976 pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 5977 ipst->ips_param_arr = pa; 5978 bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr)); 5979 5980 na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP); 5981 ipst->ips_ndp_arr = na; 5982 bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 5983 ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data = 5984 (caddr_t)&ipst->ips_ip_g_forward; 5985 ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data = 5986 (caddr_t)&ipst->ips_ipv6_forward; 5987 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name, 5988 "ip_cgtp_filter") == 0); 5989 ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data = 5990 (caddr_t)&ipst->ips_ip_cgtp_filter; 5991 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_name, 5992 "ipmp_hook_emulation") == 0); 5993 ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_data = 5994 (caddr_t)&ipst->ips_ipmp_hook_emulation; 5995 5996 (void) ip_param_register(&ipst->ips_ip_g_nd, 5997 ipst->ips_param_arr, A_CNT(lcl_param_arr), 5998 ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr)); 5999 6000 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst); 6001 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid); 6002 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics); 6003 ipst->ips_ip6_kstat = 6004 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics); 6005 6006 ipst->ips_ipmp_enable_failback = B_TRUE; 6007 6008 ipst->ips_ip_src_id = 1; 6009 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL); 6010 6011 ip_net_init(ipst, ns); 6012 ipv4_hook_init(ipst); 6013 ipv6_hook_init(ipst); 6014 6015 return (ipst); 6016 } 6017 6018 /* 6019 * Allocate and initialize a DLPI template of the specified length. (May be 6020 * called as writer.) 6021 */ 6022 mblk_t * 6023 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 6024 { 6025 mblk_t *mp; 6026 6027 mp = allocb(len, BPRI_MED); 6028 if (!mp) 6029 return (NULL); 6030 6031 /* 6032 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 6033 * of which we don't seem to use) are sent with M_PCPROTO, and 6034 * that other DLPI are M_PROTO. 6035 */ 6036 if (prim == DL_INFO_REQ) { 6037 mp->b_datap->db_type = M_PCPROTO; 6038 } else { 6039 mp->b_datap->db_type = M_PROTO; 6040 } 6041 6042 mp->b_wptr = mp->b_rptr + len; 6043 bzero(mp->b_rptr, len); 6044 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 6045 return (mp); 6046 } 6047 6048 /* 6049 * Debug formatting routine. Returns a character string representation of the 6050 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 6051 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 6052 * 6053 * Once the ndd table-printing interfaces are removed, this can be changed to 6054 * standard dotted-decimal form. 6055 */ 6056 char * 6057 ip_dot_addr(ipaddr_t addr, char *buf) 6058 { 6059 uint8_t *ap = (uint8_t *)&addr; 6060 6061 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 6062 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 6063 return (buf); 6064 } 6065 6066 /* 6067 * Write the given MAC address as a printable string in the usual colon- 6068 * separated format. 6069 */ 6070 const char * 6071 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 6072 { 6073 char *bp; 6074 6075 if (alen == 0 || buflen < 4) 6076 return ("?"); 6077 bp = buf; 6078 for (;;) { 6079 /* 6080 * If there are more MAC address bytes available, but we won't 6081 * have any room to print them, then add "..." to the string 6082 * instead. See below for the 'magic number' explanation. 6083 */ 6084 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 6085 (void) strcpy(bp, "..."); 6086 break; 6087 } 6088 (void) sprintf(bp, "%02x", *addr++); 6089 bp += 2; 6090 if (--alen == 0) 6091 break; 6092 *bp++ = ':'; 6093 buflen -= 3; 6094 /* 6095 * At this point, based on the first 'if' statement above, 6096 * either alen == 1 and buflen >= 3, or alen > 1 and 6097 * buflen >= 4. The first case leaves room for the final "xx" 6098 * number and trailing NUL byte. The second leaves room for at 6099 * least "...". Thus the apparently 'magic' numbers chosen for 6100 * that statement. 6101 */ 6102 } 6103 return (buf); 6104 } 6105 6106 /* 6107 * Send an ICMP error after patching up the packet appropriately. Returns 6108 * non-zero if the appropriate MIB should be bumped; zero otherwise. 6109 */ 6110 static boolean_t 6111 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 6112 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, 6113 zoneid_t zoneid, ip_stack_t *ipst) 6114 { 6115 ipha_t *ipha; 6116 mblk_t *first_mp; 6117 boolean_t secure; 6118 unsigned char db_type; 6119 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6120 6121 first_mp = mp; 6122 if (mctl_present) { 6123 mp = mp->b_cont; 6124 secure = ipsec_in_is_secure(first_mp); 6125 ASSERT(mp != NULL); 6126 } else { 6127 /* 6128 * If this is an ICMP error being reported - which goes 6129 * up as M_CTLs, we need to convert them to M_DATA till 6130 * we finish checking with global policy because 6131 * ipsec_check_global_policy() assumes M_DATA as clear 6132 * and M_CTL as secure. 6133 */ 6134 db_type = DB_TYPE(mp); 6135 DB_TYPE(mp) = M_DATA; 6136 secure = B_FALSE; 6137 } 6138 /* 6139 * We are generating an icmp error for some inbound packet. 6140 * Called from all ip_fanout_(udp, tcp, proto) functions. 6141 * Before we generate an error, check with global policy 6142 * to see whether this is allowed to enter the system. As 6143 * there is no "conn", we are checking with global policy. 6144 */ 6145 ipha = (ipha_t *)mp->b_rptr; 6146 if (secure || ipss->ipsec_inbound_v4_policy_present) { 6147 first_mp = ipsec_check_global_policy(first_mp, NULL, 6148 ipha, NULL, mctl_present, ipst->ips_netstack); 6149 if (first_mp == NULL) 6150 return (B_FALSE); 6151 } 6152 6153 if (!mctl_present) 6154 DB_TYPE(mp) = db_type; 6155 6156 if (flags & IP_FF_SEND_ICMP) { 6157 if (flags & IP_FF_HDR_COMPLETE) { 6158 if (ip_hdr_complete(ipha, zoneid, ipst)) { 6159 freemsg(first_mp); 6160 return (B_TRUE); 6161 } 6162 } 6163 if (flags & IP_FF_CKSUM) { 6164 /* 6165 * Have to correct checksum since 6166 * the packet might have been 6167 * fragmented and the reassembly code in ip_rput 6168 * does not restore the IP checksum. 6169 */ 6170 ipha->ipha_hdr_checksum = 0; 6171 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6172 } 6173 switch (icmp_type) { 6174 case ICMP_DEST_UNREACHABLE: 6175 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid, 6176 ipst); 6177 break; 6178 default: 6179 freemsg(first_mp); 6180 break; 6181 } 6182 } else { 6183 freemsg(first_mp); 6184 return (B_FALSE); 6185 } 6186 6187 return (B_TRUE); 6188 } 6189 6190 /* 6191 * Used to send an ICMP error message when a packet is received for 6192 * a protocol that is not supported. The mblk passed as argument 6193 * is consumed by this function. 6194 */ 6195 void 6196 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid, 6197 ip_stack_t *ipst) 6198 { 6199 mblk_t *mp; 6200 ipha_t *ipha; 6201 ill_t *ill; 6202 ipsec_in_t *ii; 6203 6204 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6205 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6206 6207 mp = ipsec_mp->b_cont; 6208 ipsec_mp->b_cont = NULL; 6209 ipha = (ipha_t *)mp->b_rptr; 6210 /* Get ill from index in ipsec_in_t. */ 6211 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 6212 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL, 6213 ipst); 6214 if (ill != NULL) { 6215 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 6216 if (ip_fanout_send_icmp(q, mp, flags, 6217 ICMP_DEST_UNREACHABLE, 6218 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) { 6219 BUMP_MIB(ill->ill_ip_mib, 6220 ipIfStatsInUnknownProtos); 6221 } 6222 } else { 6223 if (ip_fanout_send_icmp_v6(q, mp, flags, 6224 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 6225 0, B_FALSE, zoneid, ipst)) { 6226 BUMP_MIB(ill->ill_ip_mib, 6227 ipIfStatsInUnknownProtos); 6228 } 6229 } 6230 ill_refrele(ill); 6231 } else { /* re-link for the freemsg() below. */ 6232 ipsec_mp->b_cont = mp; 6233 } 6234 6235 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 6236 freemsg(ipsec_mp); 6237 } 6238 6239 /* 6240 * See if the inbound datagram has had IPsec processing applied to it. 6241 */ 6242 boolean_t 6243 ipsec_in_is_secure(mblk_t *ipsec_mp) 6244 { 6245 ipsec_in_t *ii; 6246 6247 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6248 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6249 6250 if (ii->ipsec_in_loopback) { 6251 return (ii->ipsec_in_secure); 6252 } else { 6253 return (ii->ipsec_in_ah_sa != NULL || 6254 ii->ipsec_in_esp_sa != NULL || 6255 ii->ipsec_in_decaps); 6256 } 6257 } 6258 6259 /* 6260 * Handle protocols with which IP is less intimate. There 6261 * can be more than one stream bound to a particular 6262 * protocol. When this is the case, normally each one gets a copy 6263 * of any incoming packets. 6264 * 6265 * IPsec NOTE : 6266 * 6267 * Don't allow a secure packet going up a non-secure connection. 6268 * We don't allow this because 6269 * 6270 * 1) Reply might go out in clear which will be dropped at 6271 * the sending side. 6272 * 2) If the reply goes out in clear it will give the 6273 * adversary enough information for getting the key in 6274 * most of the cases. 6275 * 6276 * Moreover getting a secure packet when we expect clear 6277 * implies that SA's were added without checking for 6278 * policy on both ends. This should not happen once ISAKMP 6279 * is used to negotiate SAs as SAs will be added only after 6280 * verifying the policy. 6281 * 6282 * NOTE : If the packet was tunneled and not multicast we only send 6283 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 6284 * back to delivering packets to AF_INET6 raw sockets. 6285 * 6286 * IPQoS Notes: 6287 * Once we have determined the client, invoke IPPF processing. 6288 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6289 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6290 * ip_policy will be false. 6291 * 6292 * Zones notes: 6293 * Currently only applications in the global zone can create raw sockets for 6294 * protocols other than ICMP. So unlike the broadcast / multicast case of 6295 * ip_fanout_udp(), we only send a copy of the packet to streams in the 6296 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 6297 */ 6298 static void 6299 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 6300 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 6301 zoneid_t zoneid) 6302 { 6303 queue_t *rq; 6304 mblk_t *mp1, *first_mp1; 6305 uint_t protocol = ipha->ipha_protocol; 6306 ipaddr_t dst; 6307 boolean_t one_only; 6308 mblk_t *first_mp = mp; 6309 boolean_t secure; 6310 uint32_t ill_index; 6311 conn_t *connp, *first_connp, *next_connp; 6312 connf_t *connfp; 6313 boolean_t shared_addr; 6314 mib2_ipIfStatsEntry_t *mibptr; 6315 ip_stack_t *ipst = recv_ill->ill_ipst; 6316 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6317 6318 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 6319 if (mctl_present) { 6320 mp = first_mp->b_cont; 6321 secure = ipsec_in_is_secure(first_mp); 6322 ASSERT(mp != NULL); 6323 } else { 6324 secure = B_FALSE; 6325 } 6326 dst = ipha->ipha_dst; 6327 /* 6328 * If the packet was tunneled and not multicast we only send to it 6329 * the first match. 6330 */ 6331 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 6332 !CLASSD(dst)); 6333 6334 shared_addr = (zoneid == ALL_ZONES); 6335 if (shared_addr) { 6336 /* 6337 * We don't allow multilevel ports for raw IP, so no need to 6338 * check for that here. 6339 */ 6340 zoneid = tsol_packet_to_zoneid(mp); 6341 } 6342 6343 connfp = &ipst->ips_ipcl_proto_fanout[protocol]; 6344 mutex_enter(&connfp->connf_lock); 6345 connp = connfp->connf_head; 6346 for (connp = connfp->connf_head; connp != NULL; 6347 connp = connp->conn_next) { 6348 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, 6349 zoneid) && 6350 (!is_system_labeled() || 6351 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6352 connp))) { 6353 break; 6354 } 6355 } 6356 6357 if (connp == NULL || connp->conn_upq == NULL) { 6358 /* 6359 * No one bound to these addresses. Is 6360 * there a client that wants all 6361 * unclaimed datagrams? 6362 */ 6363 mutex_exit(&connfp->connf_lock); 6364 /* 6365 * Check for IPPROTO_ENCAP... 6366 */ 6367 if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) { 6368 /* 6369 * If an IPsec mblk is here on a multicast 6370 * tunnel (using ip_mroute stuff), check policy here, 6371 * THEN ship off to ip_mroute_decap(). 6372 * 6373 * BTW, If I match a configured IP-in-IP 6374 * tunnel, this path will not be reached, and 6375 * ip_mroute_decap will never be called. 6376 */ 6377 first_mp = ipsec_check_global_policy(first_mp, connp, 6378 ipha, NULL, mctl_present, ipst->ips_netstack); 6379 if (first_mp != NULL) { 6380 if (mctl_present) 6381 freeb(first_mp); 6382 ip_mroute_decap(q, mp, ill); 6383 } /* Else we already freed everything! */ 6384 } else { 6385 /* 6386 * Otherwise send an ICMP protocol unreachable. 6387 */ 6388 if (ip_fanout_send_icmp(q, first_mp, flags, 6389 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 6390 mctl_present, zoneid, ipst)) { 6391 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos); 6392 } 6393 } 6394 return; 6395 } 6396 CONN_INC_REF(connp); 6397 first_connp = connp; 6398 6399 /* 6400 * Only send message to one tunnel driver by immediately 6401 * terminating the loop. 6402 */ 6403 connp = one_only ? NULL : connp->conn_next; 6404 6405 for (;;) { 6406 while (connp != NULL) { 6407 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 6408 flags, zoneid) && 6409 (!is_system_labeled() || 6410 tsol_receive_local(mp, &dst, IPV4_VERSION, 6411 shared_addr, connp))) 6412 break; 6413 connp = connp->conn_next; 6414 } 6415 6416 /* 6417 * Copy the packet. 6418 */ 6419 if (connp == NULL || connp->conn_upq == NULL || 6420 (((first_mp1 = dupmsg(first_mp)) == NULL) && 6421 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 6422 /* 6423 * No more interested clients or memory 6424 * allocation failed 6425 */ 6426 connp = first_connp; 6427 break; 6428 } 6429 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 6430 CONN_INC_REF(connp); 6431 mutex_exit(&connfp->connf_lock); 6432 rq = connp->conn_rq; 6433 if (!canputnext(rq)) { 6434 if (flags & IP_FF_RAWIP) { 6435 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6436 } else { 6437 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6438 } 6439 6440 freemsg(first_mp1); 6441 } else { 6442 /* 6443 * Don't enforce here if we're an actual tunnel - 6444 * let "tun" do it instead. 6445 */ 6446 if (!IPCL_IS_IPTUN(connp) && 6447 (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || 6448 secure)) { 6449 first_mp1 = ipsec_check_inbound_policy 6450 (first_mp1, connp, ipha, NULL, 6451 mctl_present); 6452 } 6453 if (first_mp1 != NULL) { 6454 int in_flags = 0; 6455 /* 6456 * ip_fanout_proto also gets called from 6457 * icmp_inbound_error_fanout, in which case 6458 * the msg type is M_CTL. Don't add info 6459 * in this case for the time being. In future 6460 * when there is a need for knowing the 6461 * inbound iface index for ICMP error msgs, 6462 * then this can be changed. 6463 */ 6464 if (connp->conn_recvif) 6465 in_flags = IPF_RECVIF; 6466 /* 6467 * The ULP may support IP_RECVPKTINFO for both 6468 * IP v4 and v6 so pass the appropriate argument 6469 * based on conn IP version. 6470 */ 6471 if (connp->conn_ip_recvpktinfo) { 6472 if (connp->conn_af_isv6) { 6473 /* 6474 * V6 only needs index 6475 */ 6476 in_flags |= IPF_RECVIF; 6477 } else { 6478 /* 6479 * V4 needs index + 6480 * matching address. 6481 */ 6482 in_flags |= IPF_RECVADDR; 6483 } 6484 } 6485 if ((in_flags != 0) && 6486 (mp->b_datap->db_type != M_CTL)) { 6487 /* 6488 * the actual data will be 6489 * contained in b_cont upon 6490 * successful return of the 6491 * following call else 6492 * original mblk is returned 6493 */ 6494 ASSERT(recv_ill != NULL); 6495 mp1 = ip_add_info(mp1, recv_ill, 6496 in_flags, IPCL_ZONEID(connp), ipst); 6497 } 6498 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6499 if (mctl_present) 6500 freeb(first_mp1); 6501 (connp->conn_recv)(connp, mp1, NULL); 6502 } 6503 } 6504 mutex_enter(&connfp->connf_lock); 6505 /* Follow the next pointer before releasing the conn. */ 6506 next_connp = connp->conn_next; 6507 CONN_DEC_REF(connp); 6508 connp = next_connp; 6509 } 6510 6511 /* Last one. Send it upstream. */ 6512 mutex_exit(&connfp->connf_lock); 6513 6514 /* 6515 * If this packet is coming from icmp_inbound_error_fanout ip_policy 6516 * will be set to false. 6517 */ 6518 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6519 ill_index = ill->ill_phyint->phyint_ifindex; 6520 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6521 if (mp == NULL) { 6522 CONN_DEC_REF(connp); 6523 if (mctl_present) { 6524 freeb(first_mp); 6525 } 6526 return; 6527 } 6528 } 6529 6530 rq = connp->conn_rq; 6531 if (!canputnext(rq)) { 6532 if (flags & IP_FF_RAWIP) { 6533 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6534 } else { 6535 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6536 } 6537 6538 freemsg(first_mp); 6539 } else { 6540 if (IPCL_IS_IPTUN(connp)) { 6541 /* 6542 * Tunneled packet. We enforce policy in the tunnel 6543 * module itself. 6544 * 6545 * Send the WHOLE packet up (incl. IPSEC_IN) without 6546 * a policy check. 6547 * FIXME to use conn_recv for tun later. 6548 */ 6549 putnext(rq, first_mp); 6550 CONN_DEC_REF(connp); 6551 return; 6552 } 6553 6554 if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) { 6555 first_mp = ipsec_check_inbound_policy(first_mp, connp, 6556 ipha, NULL, mctl_present); 6557 } 6558 6559 if (first_mp != NULL) { 6560 int in_flags = 0; 6561 6562 /* 6563 * ip_fanout_proto also gets called 6564 * from icmp_inbound_error_fanout, in 6565 * which case the msg type is M_CTL. 6566 * Don't add info in this case for time 6567 * being. In future when there is a 6568 * need for knowing the inbound iface 6569 * index for ICMP error msgs, then this 6570 * can be changed 6571 */ 6572 if (connp->conn_recvif) 6573 in_flags = IPF_RECVIF; 6574 if (connp->conn_ip_recvpktinfo) { 6575 if (connp->conn_af_isv6) { 6576 /* 6577 * V6 only needs index 6578 */ 6579 in_flags |= IPF_RECVIF; 6580 } else { 6581 /* 6582 * V4 needs index + 6583 * matching address. 6584 */ 6585 in_flags |= IPF_RECVADDR; 6586 } 6587 } 6588 if ((in_flags != 0) && 6589 (mp->b_datap->db_type != M_CTL)) { 6590 6591 /* 6592 * the actual data will be contained in 6593 * b_cont upon successful return 6594 * of the following call else original 6595 * mblk is returned 6596 */ 6597 ASSERT(recv_ill != NULL); 6598 mp = ip_add_info(mp, recv_ill, 6599 in_flags, IPCL_ZONEID(connp), ipst); 6600 } 6601 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6602 (connp->conn_recv)(connp, mp, NULL); 6603 if (mctl_present) 6604 freeb(first_mp); 6605 } 6606 } 6607 CONN_DEC_REF(connp); 6608 } 6609 6610 /* 6611 * Fanout for TCP packets 6612 * The caller puts <fport, lport> in the ports parameter. 6613 * 6614 * IPQoS Notes 6615 * Before sending it to the client, invoke IPPF processing. 6616 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6617 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6618 * ip_policy is false. 6619 */ 6620 static void 6621 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 6622 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 6623 { 6624 mblk_t *first_mp; 6625 boolean_t secure; 6626 uint32_t ill_index; 6627 int ip_hdr_len; 6628 tcph_t *tcph; 6629 boolean_t syn_present = B_FALSE; 6630 conn_t *connp; 6631 ip_stack_t *ipst = recv_ill->ill_ipst; 6632 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6633 6634 ASSERT(recv_ill != NULL); 6635 6636 first_mp = mp; 6637 if (mctl_present) { 6638 ASSERT(first_mp->b_datap->db_type == M_CTL); 6639 mp = first_mp->b_cont; 6640 secure = ipsec_in_is_secure(first_mp); 6641 ASSERT(mp != NULL); 6642 } else { 6643 secure = B_FALSE; 6644 } 6645 6646 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 6647 6648 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 6649 zoneid, ipst)) == NULL) { 6650 /* 6651 * No connected connection or listener. Send a 6652 * TH_RST via tcp_xmit_listeners_reset. 6653 */ 6654 6655 /* Initiate IPPf processing, if needed. */ 6656 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 6657 uint32_t ill_index; 6658 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6659 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 6660 if (first_mp == NULL) 6661 return; 6662 } 6663 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6664 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", 6665 zoneid)); 6666 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6667 ipst->ips_netstack->netstack_tcp, NULL); 6668 return; 6669 } 6670 6671 /* 6672 * Allocate the SYN for the TCP connection here itself 6673 */ 6674 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 6675 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 6676 if (IPCL_IS_TCP(connp)) { 6677 squeue_t *sqp; 6678 6679 /* 6680 * For fused tcp loopback, assign the eager's 6681 * squeue to be that of the active connect's. 6682 * Note that we don't check for IP_FF_LOOPBACK 6683 * here since this routine gets called only 6684 * for loopback (unlike the IPv6 counterpart). 6685 */ 6686 ASSERT(Q_TO_CONN(q) != NULL); 6687 if (do_tcp_fusion && 6688 !CONN_INBOUND_POLICY_PRESENT(connp, ipss) && 6689 !secure && 6690 !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy && 6691 IPCL_IS_TCP(Q_TO_CONN(q))) { 6692 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 6693 sqp = Q_TO_CONN(q)->conn_sqp; 6694 } else { 6695 sqp = IP_SQUEUE_GET(lbolt); 6696 } 6697 6698 mp->b_datap->db_struioflag |= STRUIO_EAGER; 6699 DB_CKSUMSTART(mp) = (intptr_t)sqp; 6700 syn_present = B_TRUE; 6701 } 6702 } 6703 6704 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 6705 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 6706 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6707 if ((flags & TH_RST) || (flags & TH_URG)) { 6708 CONN_DEC_REF(connp); 6709 freemsg(first_mp); 6710 return; 6711 } 6712 if (flags & TH_ACK) { 6713 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6714 ipst->ips_netstack->netstack_tcp, connp); 6715 CONN_DEC_REF(connp); 6716 return; 6717 } 6718 6719 CONN_DEC_REF(connp); 6720 freemsg(first_mp); 6721 return; 6722 } 6723 6724 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6725 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6726 NULL, mctl_present); 6727 if (first_mp == NULL) { 6728 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6729 CONN_DEC_REF(connp); 6730 return; 6731 } 6732 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 6733 ASSERT(syn_present); 6734 if (mctl_present) { 6735 ASSERT(first_mp != mp); 6736 first_mp->b_datap->db_struioflag |= 6737 STRUIO_POLICY; 6738 } else { 6739 ASSERT(first_mp == mp); 6740 mp->b_datap->db_struioflag &= 6741 ~STRUIO_EAGER; 6742 mp->b_datap->db_struioflag |= 6743 STRUIO_POLICY; 6744 } 6745 } else { 6746 /* 6747 * Discard first_mp early since we're dealing with a 6748 * fully-connected conn_t and tcp doesn't do policy in 6749 * this case. 6750 */ 6751 if (mctl_present) { 6752 freeb(first_mp); 6753 mctl_present = B_FALSE; 6754 } 6755 first_mp = mp; 6756 } 6757 } 6758 6759 /* 6760 * Initiate policy processing here if needed. If we get here from 6761 * icmp_inbound_error_fanout, ip_policy is false. 6762 */ 6763 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6764 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6765 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6766 if (mp == NULL) { 6767 CONN_DEC_REF(connp); 6768 if (mctl_present) 6769 freeb(first_mp); 6770 return; 6771 } else if (mctl_present) { 6772 ASSERT(first_mp != mp); 6773 first_mp->b_cont = mp; 6774 } else { 6775 first_mp = mp; 6776 } 6777 } 6778 6779 6780 6781 /* Handle socket options. */ 6782 if (!syn_present && 6783 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6784 /* Add header */ 6785 ASSERT(recv_ill != NULL); 6786 /* 6787 * Since tcp does not support IP_RECVPKTINFO for V4, only pass 6788 * IPF_RECVIF. 6789 */ 6790 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp), 6791 ipst); 6792 if (mp == NULL) { 6793 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6794 CONN_DEC_REF(connp); 6795 if (mctl_present) 6796 freeb(first_mp); 6797 return; 6798 } else if (mctl_present) { 6799 /* 6800 * ip_add_info might return a new mp. 6801 */ 6802 ASSERT(first_mp != mp); 6803 first_mp->b_cont = mp; 6804 } else { 6805 first_mp = mp; 6806 } 6807 } 6808 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6809 if (IPCL_IS_TCP(connp)) { 6810 /* do not drain, certain use cases can blow the stack */ 6811 squeue_enter_nodrain(connp->conn_sqp, first_mp, 6812 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); 6813 } else { 6814 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */ 6815 (connp->conn_recv)(connp, first_mp, NULL); 6816 CONN_DEC_REF(connp); 6817 } 6818 } 6819 6820 /* 6821 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or 6822 * pass it along to ESP if the SPI is non-zero. Returns TRUE if the mblk 6823 * is not consumed. 6824 * 6825 * One of four things can happen, all of which affect the passed-in mblk: 6826 * 6827 * 1.) ICMP messages that go through here just get returned TRUE. 6828 * 6829 * 2.) The packet is stock UDP and gets its zero-SPI stripped. Return TRUE. 6830 * 6831 * 3.) The packet is ESP-in-UDP, gets transformed into an equivalent 6832 * ESP packet, and is passed along to ESP for consumption. Return FALSE. 6833 * 6834 * 4.) The packet is an ESP-in-UDP Keepalive. Drop it and return FALSE. 6835 */ 6836 static boolean_t 6837 zero_spi_check(queue_t *q, mblk_t *mp, ire_t *ire, ill_t *recv_ill, 6838 ipsec_stack_t *ipss) 6839 { 6840 int shift, plen, iph_len; 6841 ipha_t *ipha; 6842 udpha_t *udpha; 6843 uint32_t *spi; 6844 uint8_t *orptr; 6845 boolean_t udp_pkt, free_ire; 6846 6847 if (DB_TYPE(mp) == M_CTL) { 6848 /* 6849 * ICMP message with UDP inside. Don't bother stripping, just 6850 * send it up. 6851 * 6852 * NOTE: Any app with UDP_NAT_T_ENDPOINT set is probably going 6853 * to ignore errors set by ICMP anyway ('cause they might be 6854 * forged), but that's the app's decision, not ours. 6855 */ 6856 6857 /* Bunch of reality checks for DEBUG kernels... */ 6858 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); 6859 ASSERT(((ipha_t *)mp->b_rptr)->ipha_protocol == IPPROTO_ICMP); 6860 6861 return (B_TRUE); 6862 } 6863 6864 ipha = (ipha_t *)mp->b_rptr; 6865 iph_len = IPH_HDR_LENGTH(ipha); 6866 plen = ntohs(ipha->ipha_length); 6867 6868 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) { 6869 /* 6870 * Most likely a keepalive for the benefit of an intervening 6871 * NAT. These aren't for us, per se, so drop it. 6872 * 6873 * RFC 3947/8 doesn't say for sure what to do for 2-3 6874 * byte packets (keepalives are 1-byte), but we'll drop them 6875 * also. 6876 */ 6877 ip_drop_packet(mp, B_TRUE, recv_ill, NULL, 6878 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper); 6879 return (B_FALSE); 6880 } 6881 6882 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) { 6883 /* might as well pull it all up - it might be ESP. */ 6884 if (!pullupmsg(mp, -1)) { 6885 ip_drop_packet(mp, B_TRUE, recv_ill, NULL, 6886 DROPPER(ipss, ipds_esp_nomem), 6887 &ipss->ipsec_dropper); 6888 return (B_FALSE); 6889 } 6890 6891 ipha = (ipha_t *)mp->b_rptr; 6892 } 6893 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t)); 6894 if (*spi == 0) { 6895 /* UDP packet - remove 0-spi. */ 6896 shift = sizeof (uint32_t); 6897 } else { 6898 /* ESP-in-UDP packet - reduce to ESP. */ 6899 ipha->ipha_protocol = IPPROTO_ESP; 6900 shift = sizeof (udpha_t); 6901 } 6902 6903 /* Fix IP header */ 6904 ipha->ipha_length = htons(plen - shift); 6905 ipha->ipha_hdr_checksum = 0; 6906 6907 orptr = mp->b_rptr; 6908 mp->b_rptr += shift; 6909 6910 if (*spi == 0) { 6911 ASSERT((uint8_t *)ipha == orptr); 6912 udpha = (udpha_t *)(orptr + iph_len); 6913 udpha->uha_length = htons(plen - shift - iph_len); 6914 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */ 6915 udp_pkt = B_TRUE; 6916 } else { 6917 udp_pkt = B_FALSE; 6918 } 6919 ovbcopy(orptr, orptr + shift, iph_len); 6920 if (!udp_pkt) /* Punt up for ESP processing. */ { 6921 ipha = (ipha_t *)(orptr + shift); 6922 6923 free_ire = (ire == NULL); 6924 if (free_ire) { 6925 /* Re-acquire ire. */ 6926 ire = ire_cache_lookup(ipha->ipha_dst, ALL_ZONES, NULL, 6927 ipss->ipsec_netstack->netstack_ip); 6928 if (ire == NULL || !(ire->ire_type & IRE_LOCAL)) { 6929 if (ire != NULL) 6930 ire_refrele(ire); 6931 /* 6932 * Do a regular freemsg(), as this is an IP 6933 * error (no local route) not an IPsec one. 6934 */ 6935 freemsg(mp); 6936 } 6937 } 6938 6939 ip_proto_input(q, mp, ipha, ire, recv_ill, B_TRUE); 6940 if (free_ire) 6941 ire_refrele(ire); 6942 } 6943 6944 return (udp_pkt); 6945 } 6946 6947 /* 6948 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 6949 * We are responsible for disposing of mp, such as by freemsg() or putnext() 6950 * Caller is responsible for dropping references to the conn, and freeing 6951 * first_mp. 6952 * 6953 * IPQoS Notes 6954 * Before sending it to the client, invoke IPPF processing. Policy processing 6955 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 6956 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 6957 * ip_wput_local, ip_policy is false. 6958 */ 6959 static void 6960 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 6961 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 6962 boolean_t ip_policy) 6963 { 6964 boolean_t mctl_present = (first_mp != NULL); 6965 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 6966 uint32_t ill_index; 6967 ip_stack_t *ipst = recv_ill->ill_ipst; 6968 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6969 6970 ASSERT(ill != NULL); 6971 6972 if (mctl_present) 6973 first_mp->b_cont = mp; 6974 else 6975 first_mp = mp; 6976 6977 if (CONN_UDP_FLOWCTLD(connp)) { 6978 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 6979 freemsg(first_mp); 6980 return; 6981 } 6982 6983 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6984 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6985 NULL, mctl_present); 6986 if (first_mp == NULL) { 6987 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 6988 return; /* Freed by ipsec_check_inbound_policy(). */ 6989 } 6990 } 6991 if (mctl_present) 6992 freeb(first_mp); 6993 6994 /* Let's hope the compilers utter "branch, predict-not-taken..." ;) */ 6995 if (connp->conn_udp->udp_nat_t_endpoint) { 6996 if (mctl_present) { 6997 /* mctl_present *shouldn't* happen. */ 6998 ip_drop_packet(mp, B_TRUE, NULL, NULL, 6999 DROPPER(ipss, ipds_esp_nat_t_ipsec), 7000 &ipss->ipsec_dropper); 7001 return; 7002 } 7003 7004 if (!zero_spi_check(ill->ill_rq, mp, NULL, recv_ill, ipss)) 7005 return; 7006 } 7007 7008 /* Handle options. */ 7009 if (connp->conn_recvif) 7010 in_flags = IPF_RECVIF; 7011 /* 7012 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag 7013 * passed to ip_add_info is based on IP version of connp. 7014 */ 7015 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 7016 if (connp->conn_af_isv6) { 7017 /* 7018 * V6 only needs index 7019 */ 7020 in_flags |= IPF_RECVIF; 7021 } else { 7022 /* 7023 * V4 needs index + matching address. 7024 */ 7025 in_flags |= IPF_RECVADDR; 7026 } 7027 } 7028 7029 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 7030 in_flags |= IPF_RECVSLLA; 7031 7032 /* 7033 * Initiate IPPF processing here, if needed. Note first_mp won't be 7034 * freed if the packet is dropped. The caller will do so. 7035 */ 7036 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 7037 ill_index = recv_ill->ill_phyint->phyint_ifindex; 7038 ip_process(IPP_LOCAL_IN, &mp, ill_index); 7039 if (mp == NULL) { 7040 return; 7041 } 7042 } 7043 if ((in_flags != 0) && 7044 (mp->b_datap->db_type != M_CTL)) { 7045 /* 7046 * The actual data will be contained in b_cont 7047 * upon successful return of the following call 7048 * else original mblk is returned 7049 */ 7050 ASSERT(recv_ill != NULL); 7051 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp), 7052 ipst); 7053 } 7054 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 7055 /* Send it upstream */ 7056 (connp->conn_recv)(connp, mp, NULL); 7057 } 7058 7059 /* 7060 * Fanout for UDP packets. 7061 * The caller puts <fport, lport> in the ports parameter. 7062 * 7063 * If SO_REUSEADDR is set all multicast and broadcast packets 7064 * will be delivered to all streams bound to the same port. 7065 * 7066 * Zones notes: 7067 * Multicast and broadcast packets will be distributed to streams in all zones. 7068 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 7069 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 7070 * packets. To maintain this behavior with multiple zones, the conns are grouped 7071 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 7072 * each zone. If unset, all the following conns in the same zone are skipped. 7073 */ 7074 static void 7075 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 7076 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 7077 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 7078 { 7079 uint32_t dstport, srcport; 7080 ipaddr_t dst; 7081 mblk_t *first_mp; 7082 boolean_t secure; 7083 in6_addr_t v6src; 7084 conn_t *connp; 7085 connf_t *connfp; 7086 conn_t *first_connp; 7087 conn_t *next_connp; 7088 mblk_t *mp1, *first_mp1; 7089 ipaddr_t src; 7090 zoneid_t last_zoneid; 7091 boolean_t reuseaddr; 7092 boolean_t shared_addr; 7093 ip_stack_t *ipst; 7094 7095 ASSERT(recv_ill != NULL); 7096 ipst = recv_ill->ill_ipst; 7097 7098 first_mp = mp; 7099 if (mctl_present) { 7100 mp = first_mp->b_cont; 7101 first_mp->b_cont = NULL; 7102 secure = ipsec_in_is_secure(first_mp); 7103 ASSERT(mp != NULL); 7104 } else { 7105 first_mp = NULL; 7106 secure = B_FALSE; 7107 } 7108 7109 /* Extract ports in net byte order */ 7110 dstport = htons(ntohl(ports) & 0xFFFF); 7111 srcport = htons(ntohl(ports) >> 16); 7112 dst = ipha->ipha_dst; 7113 src = ipha->ipha_src; 7114 7115 shared_addr = (zoneid == ALL_ZONES); 7116 if (shared_addr) { 7117 /* 7118 * No need to handle exclusive-stack zones since ALL_ZONES 7119 * only applies to the shared stack. 7120 */ 7121 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 7122 if (zoneid == ALL_ZONES) 7123 zoneid = tsol_packet_to_zoneid(mp); 7124 } 7125 7126 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7127 mutex_enter(&connfp->connf_lock); 7128 connp = connfp->connf_head; 7129 if (!broadcast && !CLASSD(dst)) { 7130 /* 7131 * Not broadcast or multicast. Send to the one (first) 7132 * client we find. No need to check conn_wantpacket() 7133 * since IP_BOUND_IF/conn_incoming_ill does not apply to 7134 * IPv4 unicast packets. 7135 */ 7136 while ((connp != NULL) && 7137 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || 7138 !IPCL_ZONE_MATCH(connp, zoneid))) { 7139 connp = connp->conn_next; 7140 } 7141 7142 if (connp == NULL || connp->conn_upq == NULL) 7143 goto notfound; 7144 7145 if (is_system_labeled() && 7146 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7147 connp)) 7148 goto notfound; 7149 7150 CONN_INC_REF(connp); 7151 mutex_exit(&connfp->connf_lock); 7152 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7153 flags, recv_ill, ip_policy); 7154 IP_STAT(ipst, ip_udp_fannorm); 7155 CONN_DEC_REF(connp); 7156 return; 7157 } 7158 7159 /* 7160 * Broadcast and multicast case 7161 * 7162 * Need to check conn_wantpacket(). 7163 * If SO_REUSEADDR has been set on the first we send the 7164 * packet to all clients that have joined the group and 7165 * match the port. 7166 */ 7167 7168 while (connp != NULL) { 7169 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 7170 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7171 (!is_system_labeled() || 7172 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7173 connp))) 7174 break; 7175 connp = connp->conn_next; 7176 } 7177 7178 if (connp == NULL || connp->conn_upq == NULL) 7179 goto notfound; 7180 7181 first_connp = connp; 7182 /* 7183 * When SO_REUSEADDR is not set, send the packet only to the first 7184 * matching connection in its zone by keeping track of the zoneid. 7185 */ 7186 reuseaddr = first_connp->conn_reuseaddr; 7187 last_zoneid = first_connp->conn_zoneid; 7188 7189 CONN_INC_REF(connp); 7190 connp = connp->conn_next; 7191 for (;;) { 7192 while (connp != NULL) { 7193 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 7194 (reuseaddr || connp->conn_zoneid != last_zoneid) && 7195 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7196 (!is_system_labeled() || 7197 tsol_receive_local(mp, &dst, IPV4_VERSION, 7198 shared_addr, connp))) 7199 break; 7200 connp = connp->conn_next; 7201 } 7202 /* 7203 * Just copy the data part alone. The mctl part is 7204 * needed just for verifying policy and it is never 7205 * sent up. 7206 */ 7207 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7208 ((mp1 = copymsg(mp)) == NULL))) { 7209 /* 7210 * No more interested clients or memory 7211 * allocation failed 7212 */ 7213 connp = first_connp; 7214 break; 7215 } 7216 if (connp->conn_zoneid != last_zoneid) { 7217 /* 7218 * Update the zoneid so that the packet isn't sent to 7219 * any more conns in the same zone unless SO_REUSEADDR 7220 * is set. 7221 */ 7222 reuseaddr = connp->conn_reuseaddr; 7223 last_zoneid = connp->conn_zoneid; 7224 } 7225 if (first_mp != NULL) { 7226 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7227 ipsec_info_type == IPSEC_IN); 7228 first_mp1 = ipsec_in_tag(first_mp, NULL, 7229 ipst->ips_netstack); 7230 if (first_mp1 == NULL) { 7231 freemsg(mp1); 7232 connp = first_connp; 7233 break; 7234 } 7235 } else { 7236 first_mp1 = NULL; 7237 } 7238 CONN_INC_REF(connp); 7239 mutex_exit(&connfp->connf_lock); 7240 /* 7241 * IPQoS notes: We don't send the packet for policy 7242 * processing here, will do it for the last one (below). 7243 * i.e. we do it per-packet now, but if we do policy 7244 * processing per-conn, then we would need to do it 7245 * here too. 7246 */ 7247 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7248 ipha, flags, recv_ill, B_FALSE); 7249 mutex_enter(&connfp->connf_lock); 7250 /* Follow the next pointer before releasing the conn. */ 7251 next_connp = connp->conn_next; 7252 IP_STAT(ipst, ip_udp_fanmb); 7253 CONN_DEC_REF(connp); 7254 connp = next_connp; 7255 } 7256 7257 /* Last one. Send it upstream. */ 7258 mutex_exit(&connfp->connf_lock); 7259 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7260 recv_ill, ip_policy); 7261 IP_STAT(ipst, ip_udp_fanmb); 7262 CONN_DEC_REF(connp); 7263 return; 7264 7265 notfound: 7266 7267 mutex_exit(&connfp->connf_lock); 7268 IP_STAT(ipst, ip_udp_fanothers); 7269 /* 7270 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 7271 * have already been matched above, since they live in the IPv4 7272 * fanout tables. This implies we only need to 7273 * check for IPv6 in6addr_any endpoints here. 7274 * Thus we compare using ipv6_all_zeros instead of the destination 7275 * address, except for the multicast group membership lookup which 7276 * uses the IPv4 destination. 7277 */ 7278 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 7279 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7280 mutex_enter(&connfp->connf_lock); 7281 connp = connfp->connf_head; 7282 if (!broadcast && !CLASSD(dst)) { 7283 while (connp != NULL) { 7284 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7285 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && 7286 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7287 !connp->conn_ipv6_v6only) 7288 break; 7289 connp = connp->conn_next; 7290 } 7291 7292 if (connp != NULL && is_system_labeled() && 7293 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7294 connp)) 7295 connp = NULL; 7296 7297 if (connp == NULL || connp->conn_upq == NULL) { 7298 /* 7299 * No one bound to this port. Is 7300 * there a client that wants all 7301 * unclaimed datagrams? 7302 */ 7303 mutex_exit(&connfp->connf_lock); 7304 7305 if (mctl_present) 7306 first_mp->b_cont = mp; 7307 else 7308 first_mp = mp; 7309 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP]. 7310 connf_head != NULL) { 7311 ip_fanout_proto(q, first_mp, ill, ipha, 7312 flags | IP_FF_RAWIP, mctl_present, 7313 ip_policy, recv_ill, zoneid); 7314 } else { 7315 if (ip_fanout_send_icmp(q, first_mp, flags, 7316 ICMP_DEST_UNREACHABLE, 7317 ICMP_PORT_UNREACHABLE, 7318 mctl_present, zoneid, ipst)) { 7319 BUMP_MIB(ill->ill_ip_mib, 7320 udpIfStatsNoPorts); 7321 } 7322 } 7323 return; 7324 } 7325 7326 CONN_INC_REF(connp); 7327 mutex_exit(&connfp->connf_lock); 7328 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7329 flags, recv_ill, ip_policy); 7330 CONN_DEC_REF(connp); 7331 return; 7332 } 7333 /* 7334 * IPv4 multicast packet being delivered to an AF_INET6 7335 * in6addr_any endpoint. 7336 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 7337 * and not conn_wantpacket_v6() since any multicast membership is 7338 * for an IPv4-mapped multicast address. 7339 * The packet is sent to all clients in all zones that have joined the 7340 * group and match the port. 7341 */ 7342 while (connp != NULL) { 7343 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7344 srcport, v6src) && 7345 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7346 (!is_system_labeled() || 7347 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7348 connp))) 7349 break; 7350 connp = connp->conn_next; 7351 } 7352 7353 if (connp == NULL || connp->conn_upq == NULL) { 7354 /* 7355 * No one bound to this port. Is 7356 * there a client that wants all 7357 * unclaimed datagrams? 7358 */ 7359 mutex_exit(&connfp->connf_lock); 7360 7361 if (mctl_present) 7362 first_mp->b_cont = mp; 7363 else 7364 first_mp = mp; 7365 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head != 7366 NULL) { 7367 ip_fanout_proto(q, first_mp, ill, ipha, 7368 flags | IP_FF_RAWIP, mctl_present, ip_policy, 7369 recv_ill, zoneid); 7370 } else { 7371 /* 7372 * We used to attempt to send an icmp error here, but 7373 * since this is known to be a multicast packet 7374 * and we don't send icmp errors in response to 7375 * multicast, just drop the packet and give up sooner. 7376 */ 7377 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 7378 freemsg(first_mp); 7379 } 7380 return; 7381 } 7382 7383 first_connp = connp; 7384 7385 CONN_INC_REF(connp); 7386 connp = connp->conn_next; 7387 for (;;) { 7388 while (connp != NULL) { 7389 if (IPCL_UDP_MATCH_V6(connp, dstport, 7390 ipv6_all_zeros, srcport, v6src) && 7391 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7392 (!is_system_labeled() || 7393 tsol_receive_local(mp, &dst, IPV4_VERSION, 7394 shared_addr, connp))) 7395 break; 7396 connp = connp->conn_next; 7397 } 7398 /* 7399 * Just copy the data part alone. The mctl part is 7400 * needed just for verifying policy and it is never 7401 * sent up. 7402 */ 7403 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7404 ((mp1 = copymsg(mp)) == NULL))) { 7405 /* 7406 * No more intested clients or memory 7407 * allocation failed 7408 */ 7409 connp = first_connp; 7410 break; 7411 } 7412 if (first_mp != NULL) { 7413 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7414 ipsec_info_type == IPSEC_IN); 7415 first_mp1 = ipsec_in_tag(first_mp, NULL, 7416 ipst->ips_netstack); 7417 if (first_mp1 == NULL) { 7418 freemsg(mp1); 7419 connp = first_connp; 7420 break; 7421 } 7422 } else { 7423 first_mp1 = NULL; 7424 } 7425 CONN_INC_REF(connp); 7426 mutex_exit(&connfp->connf_lock); 7427 /* 7428 * IPQoS notes: We don't send the packet for policy 7429 * processing here, will do it for the last one (below). 7430 * i.e. we do it per-packet now, but if we do policy 7431 * processing per-conn, then we would need to do it 7432 * here too. 7433 */ 7434 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7435 ipha, flags, recv_ill, B_FALSE); 7436 mutex_enter(&connfp->connf_lock); 7437 /* Follow the next pointer before releasing the conn. */ 7438 next_connp = connp->conn_next; 7439 CONN_DEC_REF(connp); 7440 connp = next_connp; 7441 } 7442 7443 /* Last one. Send it upstream. */ 7444 mutex_exit(&connfp->connf_lock); 7445 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7446 recv_ill, ip_policy); 7447 CONN_DEC_REF(connp); 7448 } 7449 7450 /* 7451 * Complete the ip_wput header so that it 7452 * is possible to generate ICMP 7453 * errors. 7454 */ 7455 int 7456 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst) 7457 { 7458 ire_t *ire; 7459 7460 if (ipha->ipha_src == INADDR_ANY) { 7461 ire = ire_lookup_local(zoneid, ipst); 7462 if (ire == NULL) { 7463 ip1dbg(("ip_hdr_complete: no source IRE\n")); 7464 return (1); 7465 } 7466 ipha->ipha_src = ire->ire_addr; 7467 ire_refrele(ire); 7468 } 7469 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 7470 ipha->ipha_hdr_checksum = 0; 7471 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 7472 return (0); 7473 } 7474 7475 /* 7476 * Nobody should be sending 7477 * packets up this stream 7478 */ 7479 static void 7480 ip_lrput(queue_t *q, mblk_t *mp) 7481 { 7482 mblk_t *mp1; 7483 7484 switch (mp->b_datap->db_type) { 7485 case M_FLUSH: 7486 /* Turn around */ 7487 if (*mp->b_rptr & FLUSHW) { 7488 *mp->b_rptr &= ~FLUSHR; 7489 qreply(q, mp); 7490 return; 7491 } 7492 break; 7493 } 7494 /* Could receive messages that passed through ar_rput */ 7495 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 7496 mp1->b_prev = mp1->b_next = NULL; 7497 freemsg(mp); 7498 } 7499 7500 /* Nobody should be sending packets down this stream */ 7501 /* ARGSUSED */ 7502 void 7503 ip_lwput(queue_t *q, mblk_t *mp) 7504 { 7505 freemsg(mp); 7506 } 7507 7508 /* 7509 * Move the first hop in any source route to ipha_dst and remove that part of 7510 * the source route. Called by other protocols. Errors in option formatting 7511 * are ignored - will be handled by ip_wput_options Return the final 7512 * destination (either ipha_dst or the last entry in a source route.) 7513 */ 7514 ipaddr_t 7515 ip_massage_options(ipha_t *ipha, netstack_t *ns) 7516 { 7517 ipoptp_t opts; 7518 uchar_t *opt; 7519 uint8_t optval; 7520 uint8_t optlen; 7521 ipaddr_t dst; 7522 int i; 7523 ire_t *ire; 7524 ip_stack_t *ipst = ns->netstack_ip; 7525 7526 ip2dbg(("ip_massage_options\n")); 7527 dst = ipha->ipha_dst; 7528 for (optval = ipoptp_first(&opts, ipha); 7529 optval != IPOPT_EOL; 7530 optval = ipoptp_next(&opts)) { 7531 opt = opts.ipoptp_cur; 7532 switch (optval) { 7533 uint8_t off; 7534 case IPOPT_SSRR: 7535 case IPOPT_LSRR: 7536 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 7537 ip1dbg(("ip_massage_options: bad src route\n")); 7538 break; 7539 } 7540 optlen = opts.ipoptp_len; 7541 off = opt[IPOPT_OFFSET]; 7542 off--; 7543 redo_srr: 7544 if (optlen < IP_ADDR_LEN || 7545 off > optlen - IP_ADDR_LEN) { 7546 /* End of source route */ 7547 ip1dbg(("ip_massage_options: end of SR\n")); 7548 break; 7549 } 7550 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 7551 ip1dbg(("ip_massage_options: next hop 0x%x\n", 7552 ntohl(dst))); 7553 /* 7554 * Check if our address is present more than 7555 * once as consecutive hops in source route. 7556 * XXX verify per-interface ip_forwarding 7557 * for source route? 7558 */ 7559 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 7560 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 7561 if (ire != NULL) { 7562 ire_refrele(ire); 7563 off += IP_ADDR_LEN; 7564 goto redo_srr; 7565 } 7566 if (dst == htonl(INADDR_LOOPBACK)) { 7567 ip1dbg(("ip_massage_options: loopback addr in " 7568 "source route!\n")); 7569 break; 7570 } 7571 /* 7572 * Update ipha_dst to be the first hop and remove the 7573 * first hop from the source route (by overwriting 7574 * part of the option with NOP options). 7575 */ 7576 ipha->ipha_dst = dst; 7577 /* Put the last entry in dst */ 7578 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 7579 3; 7580 bcopy(&opt[off], &dst, IP_ADDR_LEN); 7581 7582 ip1dbg(("ip_massage_options: last hop 0x%x\n", 7583 ntohl(dst))); 7584 /* Move down and overwrite */ 7585 opt[IP_ADDR_LEN] = opt[0]; 7586 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 7587 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 7588 for (i = 0; i < IP_ADDR_LEN; i++) 7589 opt[i] = IPOPT_NOP; 7590 break; 7591 } 7592 } 7593 return (dst); 7594 } 7595 7596 /* 7597 * Return the network mask 7598 * associated with the specified address. 7599 */ 7600 ipaddr_t 7601 ip_net_mask(ipaddr_t addr) 7602 { 7603 uchar_t *up = (uchar_t *)&addr; 7604 ipaddr_t mask = 0; 7605 uchar_t *maskp = (uchar_t *)&mask; 7606 7607 #if defined(__i386) || defined(__amd64) 7608 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 7609 #endif 7610 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 7611 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 7612 #endif 7613 if (CLASSD(addr)) { 7614 maskp[0] = 0xF0; 7615 return (mask); 7616 } 7617 7618 /* We assume Class E default netmask to be 32 */ 7619 if (CLASSE(addr)) 7620 return (0xffffffffU); 7621 7622 if (addr == 0) 7623 return (0); 7624 maskp[0] = 0xFF; 7625 if ((up[0] & 0x80) == 0) 7626 return (mask); 7627 7628 maskp[1] = 0xFF; 7629 if ((up[0] & 0xC0) == 0x80) 7630 return (mask); 7631 7632 maskp[2] = 0xFF; 7633 if ((up[0] & 0xE0) == 0xC0) 7634 return (mask); 7635 7636 /* Otherwise return no mask */ 7637 return ((ipaddr_t)0); 7638 } 7639 7640 /* 7641 * Select an ill for the packet by considering load spreading across 7642 * a different ill in the group if dst_ill is part of some group. 7643 */ 7644 ill_t * 7645 ip_newroute_get_dst_ill(ill_t *dst_ill) 7646 { 7647 ill_t *ill; 7648 7649 /* 7650 * We schedule irrespective of whether the source address is 7651 * INADDR_ANY or not. illgrp_scheduler returns a held ill. 7652 */ 7653 ill = illgrp_scheduler(dst_ill); 7654 if (ill == NULL) 7655 return (NULL); 7656 7657 /* 7658 * For groups with names ip_sioctl_groupname ensures that all 7659 * ills are of same type. For groups without names, ifgrp_insert 7660 * ensures this. 7661 */ 7662 ASSERT(dst_ill->ill_type == ill->ill_type); 7663 7664 return (ill); 7665 } 7666 7667 /* 7668 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. 7669 */ 7670 ill_t * 7671 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6, 7672 ip_stack_t *ipst) 7673 { 7674 ill_t *ret_ill; 7675 7676 ASSERT(ifindex != 0); 7677 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL, 7678 ipst); 7679 if (ret_ill == NULL || 7680 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { 7681 if (isv6) { 7682 if (ill != NULL) { 7683 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7684 } else { 7685 BUMP_MIB(&ipst->ips_ip6_mib, 7686 ipIfStatsOutDiscards); 7687 } 7688 ip1dbg(("ip_grab_attach_ill (IPv6): " 7689 "bad ifindex %d.\n", ifindex)); 7690 } else { 7691 if (ill != NULL) { 7692 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7693 } else { 7694 BUMP_MIB(&ipst->ips_ip_mib, 7695 ipIfStatsOutDiscards); 7696 } 7697 ip1dbg(("ip_grab_attach_ill (IPv4): " 7698 "bad ifindex %d.\n", ifindex)); 7699 } 7700 if (ret_ill != NULL) 7701 ill_refrele(ret_ill); 7702 freemsg(first_mp); 7703 return (NULL); 7704 } 7705 7706 return (ret_ill); 7707 } 7708 7709 /* 7710 * IPv4 - 7711 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 7712 * out a packet to a destination address for which we do not have specific 7713 * (or sufficient) routing information. 7714 * 7715 * NOTE : These are the scopes of some of the variables that point at IRE, 7716 * which needs to be followed while making any future modifications 7717 * to avoid memory leaks. 7718 * 7719 * - ire and sire are the entries looked up initially by 7720 * ire_ftable_lookup. 7721 * - ipif_ire is used to hold the interface ire associated with 7722 * the new cache ire. But it's scope is limited, so we always REFRELE 7723 * it before branching out to error paths. 7724 * - save_ire is initialized before ire_create, so that ire returned 7725 * by ire_create will not over-write the ire. We REFRELE save_ire 7726 * before breaking out of the switch. 7727 * 7728 * Thus on failures, we have to REFRELE only ire and sire, if they 7729 * are not NULL. 7730 */ 7731 void 7732 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, conn_t *connp, 7733 zoneid_t zoneid, ip_stack_t *ipst) 7734 { 7735 areq_t *areq; 7736 ipaddr_t gw = 0; 7737 ire_t *ire = NULL; 7738 mblk_t *res_mp; 7739 ipaddr_t *addrp; 7740 ipaddr_t nexthop_addr; 7741 ipif_t *src_ipif = NULL; 7742 ill_t *dst_ill = NULL; 7743 ipha_t *ipha; 7744 ire_t *sire = NULL; 7745 mblk_t *first_mp; 7746 ire_t *save_ire; 7747 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ 7748 ushort_t ire_marks = 0; 7749 boolean_t mctl_present; 7750 ipsec_out_t *io; 7751 mblk_t *saved_mp; 7752 ire_t *first_sire = NULL; 7753 mblk_t *copy_mp = NULL; 7754 mblk_t *xmit_mp = NULL; 7755 ipaddr_t save_dst; 7756 uint32_t multirt_flags = 7757 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 7758 boolean_t multirt_is_resolvable; 7759 boolean_t multirt_resolve_next; 7760 boolean_t unspec_src; 7761 boolean_t do_attach_ill = B_FALSE; 7762 boolean_t ip_nexthop = B_FALSE; 7763 tsol_ire_gw_secattr_t *attrp = NULL; 7764 tsol_gcgrp_t *gcgrp = NULL; 7765 tsol_gcgrp_addr_t ga; 7766 7767 if (ip_debug > 2) { 7768 /* ip1dbg */ 7769 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 7770 } 7771 7772 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7773 if (mctl_present) { 7774 io = (ipsec_out_t *)first_mp->b_rptr; 7775 ASSERT(io->ipsec_out_type == IPSEC_OUT); 7776 ASSERT(zoneid == io->ipsec_out_zoneid); 7777 ASSERT(zoneid != ALL_ZONES); 7778 } 7779 7780 ipha = (ipha_t *)mp->b_rptr; 7781 7782 /* All multicast lookups come through ip_newroute_ipif() */ 7783 if (CLASSD(dst)) { 7784 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 7785 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 7786 freemsg(first_mp); 7787 return; 7788 } 7789 7790 if (mctl_present && io->ipsec_out_attach_if) { 7791 /* ip_grab_attach_ill returns a held ill */ 7792 attach_ill = ip_grab_attach_ill(NULL, first_mp, 7793 io->ipsec_out_ill_index, B_FALSE, ipst); 7794 7795 /* Failure case frees things for us. */ 7796 if (attach_ill == NULL) 7797 return; 7798 7799 /* 7800 * Check if we need an ire that will not be 7801 * looked up by anybody else i.e. HIDDEN. 7802 */ 7803 if (ill_is_probeonly(attach_ill)) 7804 ire_marks = IRE_MARK_HIDDEN; 7805 } 7806 if (mctl_present && io->ipsec_out_ip_nexthop) { 7807 ip_nexthop = B_TRUE; 7808 nexthop_addr = io->ipsec_out_nexthop_addr; 7809 } 7810 /* 7811 * If this IRE is created for forwarding or it is not for 7812 * traffic for congestion controlled protocols, mark it as temporary. 7813 */ 7814 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 7815 ire_marks |= IRE_MARK_TEMPORARY; 7816 7817 /* 7818 * Get what we can from ire_ftable_lookup which will follow an IRE 7819 * chain until it gets the most specific information available. 7820 * For example, we know that there is no IRE_CACHE for this dest, 7821 * but there may be an IRE_OFFSUBNET which specifies a gateway. 7822 * ire_ftable_lookup will look up the gateway, etc. 7823 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 7824 * to the destination, of equal netmask length in the forward table, 7825 * will be recursively explored. If no information is available 7826 * for the final gateway of that route, we force the returned ire 7827 * to be equal to sire using MATCH_IRE_PARENT. 7828 * At least, in this case we have a starting point (in the buckets) 7829 * to look for other routes to the destination in the forward table. 7830 * This is actually used only for multirouting, where a list 7831 * of routes has to be processed in sequence. 7832 * 7833 * In the process of coming up with the most specific information, 7834 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry 7835 * for the gateway (i.e., one for which the ire_nce->nce_state is 7836 * not yet ND_REACHABLE, and is in the middle of arp resolution). 7837 * Two caveats when handling incomplete ire's in ip_newroute: 7838 * - we should be careful when accessing its ire_nce (specifically 7839 * the nce_res_mp) ast it might change underneath our feet, and, 7840 * - not all legacy code path callers are prepared to handle 7841 * incomplete ire's, so we should not create/add incomplete 7842 * ire_cache entries here. (See discussion about temporary solution 7843 * further below). 7844 * 7845 * In order to minimize packet dropping, and to preserve existing 7846 * behavior, we treat this case as if there were no IRE_CACHE for the 7847 * gateway, and instead use the IF_RESOLVER ire to send out 7848 * another request to ARP (this is achieved by passing the 7849 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the 7850 * arp response comes back in ip_wput_nondata, we will create 7851 * a per-dst ire_cache that has an ND_COMPLETE ire. 7852 * 7853 * Note that this is a temporary solution; the correct solution is 7854 * to create an incomplete per-dst ire_cache entry, and send the 7855 * packet out when the gw's nce is resolved. In order to achieve this, 7856 * all packet processing must have been completed prior to calling 7857 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need 7858 * to be modified to accomodate this solution. 7859 */ 7860 if (ip_nexthop) { 7861 /* 7862 * The first time we come here, we look for an IRE_INTERFACE 7863 * entry for the specified nexthop, set the dst to be the 7864 * nexthop address and create an IRE_CACHE entry for the 7865 * nexthop. The next time around, we are able to find an 7866 * IRE_CACHE entry for the nexthop, set the gateway to be the 7867 * nexthop address and create an IRE_CACHE entry for the 7868 * destination address via the specified nexthop. 7869 */ 7870 ire = ire_cache_lookup(nexthop_addr, zoneid, 7871 MBLK_GETLABEL(mp), ipst); 7872 if (ire != NULL) { 7873 gw = nexthop_addr; 7874 ire_marks |= IRE_MARK_PRIVATE_ADDR; 7875 } else { 7876 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 7877 IRE_INTERFACE, NULL, NULL, zoneid, 0, 7878 MBLK_GETLABEL(mp), 7879 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 7880 ipst); 7881 if (ire != NULL) { 7882 dst = nexthop_addr; 7883 } 7884 } 7885 } else if (attach_ill == NULL) { 7886 ire = ire_ftable_lookup(dst, 0, 0, 0, 7887 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), 7888 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 7889 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | 7890 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE, 7891 ipst); 7892 } else { 7893 /* 7894 * attach_ill is set only for communicating with 7895 * on-link hosts. So, don't look for DEFAULT. 7896 */ 7897 ipif_t *attach_ipif; 7898 7899 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 7900 if (attach_ipif == NULL) { 7901 ill_refrele(attach_ill); 7902 goto icmp_err_ret; 7903 } 7904 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, 7905 &sire, zoneid, 0, MBLK_GETLABEL(mp), 7906 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL | 7907 MATCH_IRE_SECATTR, ipst); 7908 ipif_refrele(attach_ipif); 7909 } 7910 ip3dbg(("ip_newroute: ire_ftable_lookup() " 7911 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 7912 7913 /* 7914 * This loop is run only once in most cases. 7915 * We loop to resolve further routes only when the destination 7916 * can be reached through multiple RTF_MULTIRT-flagged ires. 7917 */ 7918 do { 7919 /* Clear the previous iteration's values */ 7920 if (src_ipif != NULL) { 7921 ipif_refrele(src_ipif); 7922 src_ipif = NULL; 7923 } 7924 if (dst_ill != NULL) { 7925 ill_refrele(dst_ill); 7926 dst_ill = NULL; 7927 } 7928 7929 multirt_resolve_next = B_FALSE; 7930 /* 7931 * We check if packets have to be multirouted. 7932 * In this case, given the current <ire, sire> couple, 7933 * we look for the next suitable <ire, sire>. 7934 * This check is done in ire_multirt_lookup(), 7935 * which applies various criteria to find the next route 7936 * to resolve. ire_multirt_lookup() leaves <ire, sire> 7937 * unchanged if it detects it has not been tried yet. 7938 */ 7939 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 7940 ip3dbg(("ip_newroute: starting next_resolution " 7941 "with first_mp %p, tag %d\n", 7942 (void *)first_mp, 7943 MULTIRT_DEBUG_TAGGED(first_mp))); 7944 7945 ASSERT(sire != NULL); 7946 multirt_is_resolvable = 7947 ire_multirt_lookup(&ire, &sire, multirt_flags, 7948 MBLK_GETLABEL(mp), ipst); 7949 7950 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 7951 "ire %p, sire %p\n", 7952 multirt_is_resolvable, 7953 (void *)ire, (void *)sire)); 7954 7955 if (!multirt_is_resolvable) { 7956 /* 7957 * No more multirt route to resolve; give up 7958 * (all routes resolved or no more 7959 * resolvable routes). 7960 */ 7961 if (ire != NULL) { 7962 ire_refrele(ire); 7963 ire = NULL; 7964 } 7965 } else { 7966 ASSERT(sire != NULL); 7967 ASSERT(ire != NULL); 7968 /* 7969 * We simply use first_sire as a flag that 7970 * indicates if a resolvable multirt route 7971 * has already been found. 7972 * If it is not the case, we may have to send 7973 * an ICMP error to report that the 7974 * destination is unreachable. 7975 * We do not IRE_REFHOLD first_sire. 7976 */ 7977 if (first_sire == NULL) { 7978 first_sire = sire; 7979 } 7980 } 7981 } 7982 if (ire == NULL) { 7983 if (ip_debug > 3) { 7984 /* ip2dbg */ 7985 pr_addr_dbg("ip_newroute: " 7986 "can't resolve %s\n", AF_INET, &dst); 7987 } 7988 ip3dbg(("ip_newroute: " 7989 "ire %p, sire %p, first_sire %p\n", 7990 (void *)ire, (void *)sire, (void *)first_sire)); 7991 7992 if (sire != NULL) { 7993 ire_refrele(sire); 7994 sire = NULL; 7995 } 7996 7997 if (first_sire != NULL) { 7998 /* 7999 * At least one multirt route has been found 8000 * in the same call to ip_newroute(); 8001 * there is no need to report an ICMP error. 8002 * first_sire was not IRE_REFHOLDed. 8003 */ 8004 MULTIRT_DEBUG_UNTAG(first_mp); 8005 freemsg(first_mp); 8006 return; 8007 } 8008 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 8009 RTA_DST, ipst); 8010 if (attach_ill != NULL) 8011 ill_refrele(attach_ill); 8012 goto icmp_err_ret; 8013 } 8014 8015 /* 8016 * Verify that the returned IRE does not have either 8017 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 8018 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 8019 */ 8020 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 8021 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 8022 if (attach_ill != NULL) 8023 ill_refrele(attach_ill); 8024 goto icmp_err_ret; 8025 } 8026 /* 8027 * Increment the ire_ob_pkt_count field for ire if it is an 8028 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 8029 * increment the same for the parent IRE, sire, if it is some 8030 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST) 8031 */ 8032 if ((ire->ire_type & IRE_INTERFACE) != 0) { 8033 UPDATE_OB_PKT_COUNT(ire); 8034 ire->ire_last_used_time = lbolt; 8035 } 8036 8037 if (sire != NULL) { 8038 gw = sire->ire_gateway_addr; 8039 ASSERT((sire->ire_type & (IRE_CACHETABLE | 8040 IRE_INTERFACE)) == 0); 8041 UPDATE_OB_PKT_COUNT(sire); 8042 sire->ire_last_used_time = lbolt; 8043 } 8044 /* 8045 * We have a route to reach the destination. 8046 * 8047 * 1) If the interface is part of ill group, try to get a new 8048 * ill taking load spreading into account. 8049 * 8050 * 2) After selecting the ill, get a source address that 8051 * might create good inbound load spreading. 8052 * ipif_select_source does this for us. 8053 * 8054 * If the application specified the ill (ifindex), we still 8055 * load spread. Only if the packets needs to go out 8056 * specifically on a given ill e.g. binding to 8057 * IPIF_NOFAILOVER address, then we don't try to use a 8058 * different ill for load spreading. 8059 */ 8060 if (attach_ill == NULL) { 8061 /* 8062 * Don't perform outbound load spreading in the 8063 * case of an RTF_MULTIRT route, as we actually 8064 * typically want to replicate outgoing packets 8065 * through particular interfaces. 8066 */ 8067 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8068 dst_ill = ire->ire_ipif->ipif_ill; 8069 /* for uniformity */ 8070 ill_refhold(dst_ill); 8071 } else { 8072 /* 8073 * If we are here trying to create an IRE_CACHE 8074 * for an offlink destination and have the 8075 * IRE_CACHE for the next hop and the latter is 8076 * using virtual IP source address selection i.e 8077 * it's ire->ire_ipif is pointing to a virtual 8078 * network interface (vni) then 8079 * ip_newroute_get_dst_ll() will return the vni 8080 * interface as the dst_ill. Since the vni is 8081 * virtual i.e not associated with any physical 8082 * interface, it cannot be the dst_ill, hence 8083 * in such a case call ip_newroute_get_dst_ll() 8084 * with the stq_ill instead of the ire_ipif ILL. 8085 * The function returns a refheld ill. 8086 */ 8087 if ((ire->ire_type == IRE_CACHE) && 8088 IS_VNI(ire->ire_ipif->ipif_ill)) 8089 dst_ill = ip_newroute_get_dst_ill( 8090 ire->ire_stq->q_ptr); 8091 else 8092 dst_ill = ip_newroute_get_dst_ill( 8093 ire->ire_ipif->ipif_ill); 8094 } 8095 if (dst_ill == NULL) { 8096 if (ip_debug > 2) { 8097 pr_addr_dbg("ip_newroute: " 8098 "no dst ill for dst" 8099 " %s\n", AF_INET, &dst); 8100 } 8101 goto icmp_err_ret; 8102 } 8103 } else { 8104 dst_ill = ire->ire_ipif->ipif_ill; 8105 /* for uniformity */ 8106 ill_refhold(dst_ill); 8107 /* 8108 * We should have found a route matching ill as we 8109 * called ire_ftable_lookup with MATCH_IRE_ILL. 8110 * Rather than asserting, when there is a mismatch, 8111 * we just drop the packet. 8112 */ 8113 if (dst_ill != attach_ill) { 8114 ip0dbg(("ip_newroute: Packet dropped as " 8115 "IPIF_NOFAILOVER ill is %s, " 8116 "ire->ire_ipif->ipif_ill is %s\n", 8117 attach_ill->ill_name, 8118 dst_ill->ill_name)); 8119 ill_refrele(attach_ill); 8120 goto icmp_err_ret; 8121 } 8122 } 8123 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ 8124 if (attach_ill != NULL) { 8125 ill_refrele(attach_ill); 8126 attach_ill = NULL; 8127 do_attach_ill = B_TRUE; 8128 } 8129 ASSERT(dst_ill != NULL); 8130 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 8131 8132 /* 8133 * Pick the best source address from dst_ill. 8134 * 8135 * 1) If it is part of a multipathing group, we would 8136 * like to spread the inbound packets across different 8137 * interfaces. ipif_select_source picks a random source 8138 * across the different ills in the group. 8139 * 8140 * 2) If it is not part of a multipathing group, we try 8141 * to pick the source address from the destination 8142 * route. Clustering assumes that when we have multiple 8143 * prefixes hosted on an interface, the prefix of the 8144 * source address matches the prefix of the destination 8145 * route. We do this only if the address is not 8146 * DEPRECATED. 8147 * 8148 * 3) If the conn is in a different zone than the ire, we 8149 * need to pick a source address from the right zone. 8150 * 8151 * NOTE : If we hit case (1) above, the prefix of the source 8152 * address picked may not match the prefix of the 8153 * destination routes prefix as ipif_select_source 8154 * does not look at "dst" while picking a source 8155 * address. 8156 * If we want the same behavior as (2), we will need 8157 * to change the behavior of ipif_select_source. 8158 */ 8159 ASSERT(src_ipif == NULL); 8160 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 8161 /* 8162 * The RTF_SETSRC flag is set in the parent ire (sire). 8163 * Check that the ipif matching the requested source 8164 * address still exists. 8165 */ 8166 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 8167 zoneid, NULL, NULL, NULL, NULL, ipst); 8168 } 8169 8170 unspec_src = (connp != NULL && connp->conn_unspec_src); 8171 8172 if (src_ipif == NULL && 8173 (!unspec_src || ipha->ipha_src != INADDR_ANY)) { 8174 ire_marks |= IRE_MARK_USESRC_CHECK; 8175 if ((dst_ill->ill_group != NULL) || 8176 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 8177 (connp != NULL && ire->ire_zoneid != zoneid && 8178 ire->ire_zoneid != ALL_ZONES) || 8179 (dst_ill->ill_usesrc_ifindex != 0)) { 8180 /* 8181 * If the destination is reachable via a 8182 * given gateway, the selected source address 8183 * should be in the same subnet as the gateway. 8184 * Otherwise, the destination is not reachable. 8185 * 8186 * If there are no interfaces on the same subnet 8187 * as the destination, ipif_select_source gives 8188 * first non-deprecated interface which might be 8189 * on a different subnet than the gateway. 8190 * This is not desirable. Hence pass the dst_ire 8191 * source address to ipif_select_source. 8192 * It is sure that the destination is reachable 8193 * with the dst_ire source address subnet. 8194 * So passing dst_ire source address to 8195 * ipif_select_source will make sure that the 8196 * selected source will be on the same subnet 8197 * as dst_ire source address. 8198 */ 8199 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 8200 src_ipif = ipif_select_source(dst_ill, saddr, 8201 zoneid); 8202 if (src_ipif == NULL) { 8203 if (ip_debug > 2) { 8204 pr_addr_dbg("ip_newroute: " 8205 "no src for dst %s ", 8206 AF_INET, &dst); 8207 printf("through interface %s\n", 8208 dst_ill->ill_name); 8209 } 8210 goto icmp_err_ret; 8211 } 8212 } else { 8213 src_ipif = ire->ire_ipif; 8214 ASSERT(src_ipif != NULL); 8215 /* hold src_ipif for uniformity */ 8216 ipif_refhold(src_ipif); 8217 } 8218 } 8219 8220 /* 8221 * Assign a source address while we have the conn. 8222 * We can't have ip_wput_ire pick a source address when the 8223 * packet returns from arp since we need to look at 8224 * conn_unspec_src and conn_zoneid, and we lose the conn when 8225 * going through arp. 8226 * 8227 * NOTE : ip_newroute_v6 does not have this piece of code as 8228 * it uses ip6i to store this information. 8229 */ 8230 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 8231 ipha->ipha_src = src_ipif->ipif_src_addr; 8232 8233 if (ip_debug > 3) { 8234 /* ip2dbg */ 8235 pr_addr_dbg("ip_newroute: first hop %s\n", 8236 AF_INET, &gw); 8237 } 8238 ip2dbg(("\tire type %s (%d)\n", 8239 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 8240 8241 /* 8242 * The TTL of multirouted packets is bounded by the 8243 * ip_multirt_ttl ndd variable. 8244 */ 8245 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8246 /* Force TTL of multirouted packets */ 8247 if ((ipst->ips_ip_multirt_ttl > 0) && 8248 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 8249 ip2dbg(("ip_newroute: forcing multirt TTL " 8250 "to %d (was %d), dst 0x%08x\n", 8251 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 8252 ntohl(sire->ire_addr))); 8253 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 8254 } 8255 } 8256 /* 8257 * At this point in ip_newroute(), ire is either the 8258 * IRE_CACHE of the next-hop gateway for an off-subnet 8259 * destination or an IRE_INTERFACE type that should be used 8260 * to resolve an on-subnet destination or an on-subnet 8261 * next-hop gateway. 8262 * 8263 * In the IRE_CACHE case, we have the following : 8264 * 8265 * 1) src_ipif - used for getting a source address. 8266 * 8267 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8268 * means packets using this IRE_CACHE will go out on 8269 * dst_ill. 8270 * 8271 * 3) The IRE sire will point to the prefix that is the 8272 * longest matching route for the destination. These 8273 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST. 8274 * 8275 * The newly created IRE_CACHE entry for the off-subnet 8276 * destination is tied to both the prefix route and the 8277 * interface route used to resolve the next-hop gateway 8278 * via the ire_phandle and ire_ihandle fields, 8279 * respectively. 8280 * 8281 * In the IRE_INTERFACE case, we have the following : 8282 * 8283 * 1) src_ipif - used for getting a source address. 8284 * 8285 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8286 * means packets using the IRE_CACHE that we will build 8287 * here will go out on dst_ill. 8288 * 8289 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 8290 * to be created will only be tied to the IRE_INTERFACE 8291 * that was derived from the ire_ihandle field. 8292 * 8293 * If sire is non-NULL, it means the destination is 8294 * off-link and we will first create the IRE_CACHE for the 8295 * gateway. Next time through ip_newroute, we will create 8296 * the IRE_CACHE for the final destination as described 8297 * above. 8298 * 8299 * In both cases, after the current resolution has been 8300 * completed (or possibly initialised, in the IRE_INTERFACE 8301 * case), the loop may be re-entered to attempt the resolution 8302 * of another RTF_MULTIRT route. 8303 * 8304 * When an IRE_CACHE entry for the off-subnet destination is 8305 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 8306 * for further processing in emission loops. 8307 */ 8308 save_ire = ire; 8309 switch (ire->ire_type) { 8310 case IRE_CACHE: { 8311 ire_t *ipif_ire; 8312 8313 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); 8314 if (gw == 0) 8315 gw = ire->ire_gateway_addr; 8316 /* 8317 * We need 3 ire's to create a new cache ire for an 8318 * off-link destination from the cache ire of the 8319 * gateway. 8320 * 8321 * 1. The prefix ire 'sire' (Note that this does 8322 * not apply to the conn_nexthop_set case) 8323 * 2. The cache ire of the gateway 'ire' 8324 * 3. The interface ire 'ipif_ire' 8325 * 8326 * We have (1) and (2). We lookup (3) below. 8327 * 8328 * If there is no interface route to the gateway, 8329 * it is a race condition, where we found the cache 8330 * but the interface route has been deleted. 8331 */ 8332 if (ip_nexthop) { 8333 ipif_ire = ire_ihandle_lookup_onlink(ire); 8334 } else { 8335 ipif_ire = 8336 ire_ihandle_lookup_offlink(ire, sire); 8337 } 8338 if (ipif_ire == NULL) { 8339 ip1dbg(("ip_newroute: " 8340 "ire_ihandle_lookup_offlink failed\n")); 8341 goto icmp_err_ret; 8342 } 8343 8344 /* 8345 * Check cached gateway IRE for any security 8346 * attributes; if found, associate the gateway 8347 * credentials group to the destination IRE. 8348 */ 8349 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 8350 mutex_enter(&attrp->igsa_lock); 8351 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 8352 GCGRP_REFHOLD(gcgrp); 8353 mutex_exit(&attrp->igsa_lock); 8354 } 8355 8356 /* 8357 * XXX For the source of the resolver mp, 8358 * we are using the same DL_UNITDATA_REQ 8359 * (from save_ire->ire_nce->nce_res_mp) 8360 * though the save_ire is not pointing at the same ill. 8361 * This is incorrect. We need to send it up to the 8362 * resolver to get the right res_mp. For ethernets 8363 * this may be okay (ill_type == DL_ETHER). 8364 */ 8365 8366 ire = ire_create( 8367 (uchar_t *)&dst, /* dest address */ 8368 (uchar_t *)&ip_g_all_ones, /* mask */ 8369 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8370 (uchar_t *)&gw, /* gateway address */ 8371 &save_ire->ire_max_frag, 8372 save_ire->ire_nce, /* src nce */ 8373 dst_ill->ill_rq, /* recv-from queue */ 8374 dst_ill->ill_wq, /* send-to queue */ 8375 IRE_CACHE, /* IRE type */ 8376 src_ipif, 8377 (sire != NULL) ? 8378 sire->ire_mask : 0, /* Parent mask */ 8379 (sire != NULL) ? 8380 sire->ire_phandle : 0, /* Parent handle */ 8381 ipif_ire->ire_ihandle, /* Interface handle */ 8382 (sire != NULL) ? (sire->ire_flags & 8383 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 8384 (sire != NULL) ? 8385 &(sire->ire_uinfo) : &(save_ire->ire_uinfo), 8386 NULL, 8387 gcgrp, 8388 ipst); 8389 8390 if (ire == NULL) { 8391 if (gcgrp != NULL) { 8392 GCGRP_REFRELE(gcgrp); 8393 gcgrp = NULL; 8394 } 8395 ire_refrele(ipif_ire); 8396 ire_refrele(save_ire); 8397 break; 8398 } 8399 8400 /* reference now held by IRE */ 8401 gcgrp = NULL; 8402 8403 ire->ire_marks |= ire_marks; 8404 8405 /* 8406 * Prevent sire and ipif_ire from getting deleted. 8407 * The newly created ire is tied to both of them via 8408 * the phandle and ihandle respectively. 8409 */ 8410 if (sire != NULL) { 8411 IRB_REFHOLD(sire->ire_bucket); 8412 /* Has it been removed already ? */ 8413 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 8414 IRB_REFRELE(sire->ire_bucket); 8415 ire_refrele(ipif_ire); 8416 ire_refrele(save_ire); 8417 break; 8418 } 8419 } 8420 8421 IRB_REFHOLD(ipif_ire->ire_bucket); 8422 /* Has it been removed already ? */ 8423 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 8424 IRB_REFRELE(ipif_ire->ire_bucket); 8425 if (sire != NULL) 8426 IRB_REFRELE(sire->ire_bucket); 8427 ire_refrele(ipif_ire); 8428 ire_refrele(save_ire); 8429 break; 8430 } 8431 8432 xmit_mp = first_mp; 8433 /* 8434 * In the case of multirouting, a copy 8435 * of the packet is done before its sending. 8436 * The copy is used to attempt another 8437 * route resolution, in a next loop. 8438 */ 8439 if (ire->ire_flags & RTF_MULTIRT) { 8440 copy_mp = copymsg(first_mp); 8441 if (copy_mp != NULL) { 8442 xmit_mp = copy_mp; 8443 MULTIRT_DEBUG_TAG(first_mp); 8444 } 8445 } 8446 ire_add_then_send(q, ire, xmit_mp); 8447 ire_refrele(save_ire); 8448 8449 /* Assert that sire is not deleted yet. */ 8450 if (sire != NULL) { 8451 ASSERT(sire->ire_ptpn != NULL); 8452 IRB_REFRELE(sire->ire_bucket); 8453 } 8454 8455 /* Assert that ipif_ire is not deleted yet. */ 8456 ASSERT(ipif_ire->ire_ptpn != NULL); 8457 IRB_REFRELE(ipif_ire->ire_bucket); 8458 ire_refrele(ipif_ire); 8459 8460 /* 8461 * If copy_mp is not NULL, multirouting was 8462 * requested. We loop to initiate a next 8463 * route resolution attempt, starting from sire. 8464 */ 8465 if (copy_mp != NULL) { 8466 /* 8467 * Search for the next unresolved 8468 * multirt route. 8469 */ 8470 copy_mp = NULL; 8471 ipif_ire = NULL; 8472 ire = NULL; 8473 multirt_resolve_next = B_TRUE; 8474 continue; 8475 } 8476 if (sire != NULL) 8477 ire_refrele(sire); 8478 ipif_refrele(src_ipif); 8479 ill_refrele(dst_ill); 8480 return; 8481 } 8482 case IRE_IF_NORESOLVER: { 8483 8484 if (dst_ill->ill_phys_addr_length != IP_ADDR_LEN && 8485 dst_ill->ill_resolver_mp == NULL) { 8486 ip1dbg(("ip_newroute: dst_ill %p " 8487 "for IRE_IF_NORESOLVER ire %p has " 8488 "no ill_resolver_mp\n", 8489 (void *)dst_ill, (void *)ire)); 8490 break; 8491 } 8492 8493 /* 8494 * TSol note: We are creating the ire cache for the 8495 * destination 'dst'. If 'dst' is offlink, going 8496 * through the first hop 'gw', the security attributes 8497 * of 'dst' must be set to point to the gateway 8498 * credentials of gateway 'gw'. If 'dst' is onlink, it 8499 * is possible that 'dst' is a potential gateway that is 8500 * referenced by some route that has some security 8501 * attributes. Thus in the former case, we need to do a 8502 * gcgrp_lookup of 'gw' while in the latter case we 8503 * need to do gcgrp_lookup of 'dst' itself. 8504 */ 8505 ga.ga_af = AF_INET; 8506 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, 8507 &ga.ga_addr); 8508 gcgrp = gcgrp_lookup(&ga, B_FALSE); 8509 8510 ire = ire_create( 8511 (uchar_t *)&dst, /* dest address */ 8512 (uchar_t *)&ip_g_all_ones, /* mask */ 8513 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8514 (uchar_t *)&gw, /* gateway address */ 8515 &save_ire->ire_max_frag, 8516 NULL, /* no src nce */ 8517 dst_ill->ill_rq, /* recv-from queue */ 8518 dst_ill->ill_wq, /* send-to queue */ 8519 IRE_CACHE, 8520 src_ipif, 8521 save_ire->ire_mask, /* Parent mask */ 8522 (sire != NULL) ? /* Parent handle */ 8523 sire->ire_phandle : 0, 8524 save_ire->ire_ihandle, /* Interface handle */ 8525 (sire != NULL) ? sire->ire_flags & 8526 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 8527 &(save_ire->ire_uinfo), 8528 NULL, 8529 gcgrp, 8530 ipst); 8531 8532 if (ire == NULL) { 8533 if (gcgrp != NULL) { 8534 GCGRP_REFRELE(gcgrp); 8535 gcgrp = NULL; 8536 } 8537 ire_refrele(save_ire); 8538 break; 8539 } 8540 8541 /* reference now held by IRE */ 8542 gcgrp = NULL; 8543 8544 ire->ire_marks |= ire_marks; 8545 8546 /* Prevent save_ire from getting deleted */ 8547 IRB_REFHOLD(save_ire->ire_bucket); 8548 /* Has it been removed already ? */ 8549 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8550 IRB_REFRELE(save_ire->ire_bucket); 8551 ire_refrele(save_ire); 8552 break; 8553 } 8554 8555 /* 8556 * In the case of multirouting, a copy 8557 * of the packet is made before it is sent. 8558 * The copy is used in the next 8559 * loop to attempt another resolution. 8560 */ 8561 xmit_mp = first_mp; 8562 if ((sire != NULL) && 8563 (sire->ire_flags & RTF_MULTIRT)) { 8564 copy_mp = copymsg(first_mp); 8565 if (copy_mp != NULL) { 8566 xmit_mp = copy_mp; 8567 MULTIRT_DEBUG_TAG(first_mp); 8568 } 8569 } 8570 ire_add_then_send(q, ire, xmit_mp); 8571 8572 /* Assert that it is not deleted yet. */ 8573 ASSERT(save_ire->ire_ptpn != NULL); 8574 IRB_REFRELE(save_ire->ire_bucket); 8575 ire_refrele(save_ire); 8576 8577 if (copy_mp != NULL) { 8578 /* 8579 * If we found a (no)resolver, we ignore any 8580 * trailing top priority IRE_CACHE in further 8581 * loops. This ensures that we do not omit any 8582 * (no)resolver. 8583 * This IRE_CACHE, if any, will be processed 8584 * by another thread entering ip_newroute(). 8585 * IRE_CACHE entries, if any, will be processed 8586 * by another thread entering ip_newroute(), 8587 * (upon resolver response, for instance). 8588 * This aims to force parallel multirt 8589 * resolutions as soon as a packet must be sent. 8590 * In the best case, after the tx of only one 8591 * packet, all reachable routes are resolved. 8592 * Otherwise, the resolution of all RTF_MULTIRT 8593 * routes would require several emissions. 8594 */ 8595 multirt_flags &= ~MULTIRT_CACHEGW; 8596 8597 /* 8598 * Search for the next unresolved multirt 8599 * route. 8600 */ 8601 copy_mp = NULL; 8602 save_ire = NULL; 8603 ire = NULL; 8604 multirt_resolve_next = B_TRUE; 8605 continue; 8606 } 8607 8608 /* 8609 * Don't need sire anymore 8610 */ 8611 if (sire != NULL) 8612 ire_refrele(sire); 8613 8614 ipif_refrele(src_ipif); 8615 ill_refrele(dst_ill); 8616 return; 8617 } 8618 case IRE_IF_RESOLVER: 8619 /* 8620 * We can't build an IRE_CACHE yet, but at least we 8621 * found a resolver that can help. 8622 */ 8623 res_mp = dst_ill->ill_resolver_mp; 8624 if (!OK_RESOLVER_MP(res_mp)) 8625 break; 8626 8627 /* 8628 * To be at this point in the code with a non-zero gw 8629 * means that dst is reachable through a gateway that 8630 * we have never resolved. By changing dst to the gw 8631 * addr we resolve the gateway first. 8632 * When ire_add_then_send() tries to put the IP dg 8633 * to dst, it will reenter ip_newroute() at which 8634 * time we will find the IRE_CACHE for the gw and 8635 * create another IRE_CACHE in case IRE_CACHE above. 8636 */ 8637 if (gw != INADDR_ANY) { 8638 /* 8639 * The source ipif that was determined above was 8640 * relative to the destination address, not the 8641 * gateway's. If src_ipif was not taken out of 8642 * the IRE_IF_RESOLVER entry, we'll need to call 8643 * ipif_select_source() again. 8644 */ 8645 if (src_ipif != ire->ire_ipif) { 8646 ipif_refrele(src_ipif); 8647 src_ipif = ipif_select_source(dst_ill, 8648 gw, zoneid); 8649 if (src_ipif == NULL) { 8650 if (ip_debug > 2) { 8651 pr_addr_dbg( 8652 "ip_newroute: no " 8653 "src for gw %s ", 8654 AF_INET, &gw); 8655 printf("through " 8656 "interface %s\n", 8657 dst_ill->ill_name); 8658 } 8659 goto icmp_err_ret; 8660 } 8661 } 8662 save_dst = dst; 8663 dst = gw; 8664 gw = INADDR_ANY; 8665 } 8666 8667 /* 8668 * We obtain a partial IRE_CACHE which we will pass 8669 * along with the resolver query. When the response 8670 * comes back it will be there ready for us to add. 8671 * The ire_max_frag is atomically set under the 8672 * irebucket lock in ire_add_v[46]. 8673 */ 8674 8675 ire = ire_create_mp( 8676 (uchar_t *)&dst, /* dest address */ 8677 (uchar_t *)&ip_g_all_ones, /* mask */ 8678 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8679 (uchar_t *)&gw, /* gateway address */ 8680 NULL, /* ire_max_frag */ 8681 NULL, /* no src nce */ 8682 dst_ill->ill_rq, /* recv-from queue */ 8683 dst_ill->ill_wq, /* send-to queue */ 8684 IRE_CACHE, 8685 src_ipif, /* Interface ipif */ 8686 save_ire->ire_mask, /* Parent mask */ 8687 0, 8688 save_ire->ire_ihandle, /* Interface handle */ 8689 0, /* flags if any */ 8690 &(save_ire->ire_uinfo), 8691 NULL, 8692 NULL, 8693 ipst); 8694 8695 if (ire == NULL) { 8696 ire_refrele(save_ire); 8697 break; 8698 } 8699 8700 if ((sire != NULL) && 8701 (sire->ire_flags & RTF_MULTIRT)) { 8702 copy_mp = copymsg(first_mp); 8703 if (copy_mp != NULL) 8704 MULTIRT_DEBUG_TAG(copy_mp); 8705 } 8706 8707 ire->ire_marks |= ire_marks; 8708 8709 /* 8710 * Construct message chain for the resolver 8711 * of the form: 8712 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8713 * Packet could contain a IPSEC_OUT mp. 8714 * 8715 * NOTE : ire will be added later when the response 8716 * comes back from ARP. If the response does not 8717 * come back, ARP frees the packet. For this reason, 8718 * we can't REFHOLD the bucket of save_ire to prevent 8719 * deletions. We may not be able to REFRELE the bucket 8720 * if the response never comes back. Thus, before 8721 * adding the ire, ire_add_v4 will make sure that the 8722 * interface route does not get deleted. This is the 8723 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 8724 * where we can always prevent deletions because of 8725 * the synchronous nature of adding IRES i.e 8726 * ire_add_then_send is called after creating the IRE. 8727 */ 8728 ASSERT(ire->ire_mp != NULL); 8729 ire->ire_mp->b_cont = first_mp; 8730 /* Have saved_mp handy, for cleanup if canput fails */ 8731 saved_mp = mp; 8732 mp = copyb(res_mp); 8733 if (mp == NULL) { 8734 /* Prepare for cleanup */ 8735 mp = saved_mp; /* pkt */ 8736 ire_delete(ire); /* ire_mp */ 8737 ire = NULL; 8738 ire_refrele(save_ire); 8739 if (copy_mp != NULL) { 8740 MULTIRT_DEBUG_UNTAG(copy_mp); 8741 freemsg(copy_mp); 8742 copy_mp = NULL; 8743 } 8744 break; 8745 } 8746 linkb(mp, ire->ire_mp); 8747 8748 /* 8749 * Fill in the source and dest addrs for the resolver. 8750 * NOTE: this depends on memory layouts imposed by 8751 * ill_init(). 8752 */ 8753 areq = (areq_t *)mp->b_rptr; 8754 addrp = (ipaddr_t *)((char *)areq + 8755 areq->areq_sender_addr_offset); 8756 if (do_attach_ill) { 8757 /* 8758 * This is bind to no failover case. 8759 * arp packet also must go out on attach_ill. 8760 */ 8761 ASSERT(ipha->ipha_src != NULL); 8762 *addrp = ipha->ipha_src; 8763 } else { 8764 *addrp = save_ire->ire_src_addr; 8765 } 8766 8767 ire_refrele(save_ire); 8768 addrp = (ipaddr_t *)((char *)areq + 8769 areq->areq_target_addr_offset); 8770 *addrp = dst; 8771 /* Up to the resolver. */ 8772 if (canputnext(dst_ill->ill_rq) && 8773 !(dst_ill->ill_arp_closing)) { 8774 putnext(dst_ill->ill_rq, mp); 8775 ire = NULL; 8776 if (copy_mp != NULL) { 8777 /* 8778 * If we found a resolver, we ignore 8779 * any trailing top priority IRE_CACHE 8780 * in the further loops. This ensures 8781 * that we do not omit any resolver. 8782 * IRE_CACHE entries, if any, will be 8783 * processed next time we enter 8784 * ip_newroute(). 8785 */ 8786 multirt_flags &= ~MULTIRT_CACHEGW; 8787 /* 8788 * Search for the next unresolved 8789 * multirt route. 8790 */ 8791 first_mp = copy_mp; 8792 copy_mp = NULL; 8793 /* Prepare the next resolution loop. */ 8794 mp = first_mp; 8795 EXTRACT_PKT_MP(mp, first_mp, 8796 mctl_present); 8797 if (mctl_present) 8798 io = (ipsec_out_t *) 8799 first_mp->b_rptr; 8800 ipha = (ipha_t *)mp->b_rptr; 8801 8802 ASSERT(sire != NULL); 8803 8804 dst = save_dst; 8805 multirt_resolve_next = B_TRUE; 8806 continue; 8807 } 8808 8809 if (sire != NULL) 8810 ire_refrele(sire); 8811 8812 /* 8813 * The response will come back in ip_wput 8814 * with db_type IRE_DB_TYPE. 8815 */ 8816 ipif_refrele(src_ipif); 8817 ill_refrele(dst_ill); 8818 return; 8819 } else { 8820 /* Prepare for cleanup */ 8821 DTRACE_PROBE1(ip__newroute__drop, mblk_t *, 8822 mp); 8823 mp->b_cont = NULL; 8824 freeb(mp); /* areq */ 8825 /* 8826 * this is an ire that is not added to the 8827 * cache. ire_freemblk will handle the release 8828 * of any resources associated with the ire. 8829 */ 8830 ire_delete(ire); /* ire_mp */ 8831 mp = saved_mp; /* pkt */ 8832 ire = NULL; 8833 if (copy_mp != NULL) { 8834 MULTIRT_DEBUG_UNTAG(copy_mp); 8835 freemsg(copy_mp); 8836 copy_mp = NULL; 8837 } 8838 break; 8839 } 8840 default: 8841 break; 8842 } 8843 } while (multirt_resolve_next); 8844 8845 ip1dbg(("ip_newroute: dropped\n")); 8846 /* Did this packet originate externally? */ 8847 if (mp->b_prev) { 8848 mp->b_next = NULL; 8849 mp->b_prev = NULL; 8850 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 8851 } else { 8852 if (dst_ill != NULL) { 8853 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards); 8854 } else { 8855 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 8856 } 8857 } 8858 ASSERT(copy_mp == NULL); 8859 MULTIRT_DEBUG_UNTAG(first_mp); 8860 freemsg(first_mp); 8861 if (ire != NULL) 8862 ire_refrele(ire); 8863 if (sire != NULL) 8864 ire_refrele(sire); 8865 if (src_ipif != NULL) 8866 ipif_refrele(src_ipif); 8867 if (dst_ill != NULL) 8868 ill_refrele(dst_ill); 8869 return; 8870 8871 icmp_err_ret: 8872 ip1dbg(("ip_newroute: no route\n")); 8873 if (src_ipif != NULL) 8874 ipif_refrele(src_ipif); 8875 if (dst_ill != NULL) 8876 ill_refrele(dst_ill); 8877 if (sire != NULL) 8878 ire_refrele(sire); 8879 /* Did this packet originate externally? */ 8880 if (mp->b_prev) { 8881 mp->b_next = NULL; 8882 mp->b_prev = NULL; 8883 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes); 8884 q = WR(q); 8885 } else { 8886 /* 8887 * There is no outgoing ill, so just increment the 8888 * system MIB. 8889 */ 8890 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 8891 /* 8892 * Since ip_wput() isn't close to finished, we fill 8893 * in enough of the header for credible error reporting. 8894 */ 8895 if (ip_hdr_complete(ipha, zoneid, ipst)) { 8896 /* Failed */ 8897 MULTIRT_DEBUG_UNTAG(first_mp); 8898 freemsg(first_mp); 8899 if (ire != NULL) 8900 ire_refrele(ire); 8901 return; 8902 } 8903 } 8904 8905 /* 8906 * At this point we will have ire only if RTF_BLACKHOLE 8907 * or RTF_REJECT flags are set on the IRE. It will not 8908 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 8909 */ 8910 if (ire != NULL) { 8911 if (ire->ire_flags & RTF_BLACKHOLE) { 8912 ire_refrele(ire); 8913 MULTIRT_DEBUG_UNTAG(first_mp); 8914 freemsg(first_mp); 8915 return; 8916 } 8917 ire_refrele(ire); 8918 } 8919 if (ip_source_routed(ipha, ipst)) { 8920 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, 8921 zoneid, ipst); 8922 return; 8923 } 8924 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 8925 } 8926 8927 ip_opt_info_t zero_info; 8928 8929 /* 8930 * IPv4 - 8931 * ip_newroute_ipif is called by ip_wput_multicast and 8932 * ip_rput_forward_multicast whenever we need to send 8933 * out a packet to a destination address for which we do not have specific 8934 * routing information. It is used when the packet will be sent out 8935 * on a specific interface. It is also called by ip_wput() when IP_BOUND_IF 8936 * socket option is set or icmp error message wants to go out on a particular 8937 * interface for a unicast packet. 8938 * 8939 * In most cases, the destination address is resolved thanks to the ipif 8940 * intrinsic resolver. However, there are some cases where the call to 8941 * ip_newroute_ipif must take into account the potential presence of 8942 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 8943 * that uses the interface. This is specified through flags, 8944 * which can be a combination of: 8945 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 8946 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 8947 * and flags. Additionally, the packet source address has to be set to 8948 * the specified address. The caller is thus expected to set this flag 8949 * if the packet has no specific source address yet. 8950 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 8951 * flag, the resulting ire will inherit the flag. All unresolved routes 8952 * to the destination must be explored in the same call to 8953 * ip_newroute_ipif(). 8954 */ 8955 static void 8956 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 8957 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop) 8958 { 8959 areq_t *areq; 8960 ire_t *ire = NULL; 8961 mblk_t *res_mp; 8962 ipaddr_t *addrp; 8963 mblk_t *first_mp; 8964 ire_t *save_ire = NULL; 8965 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ 8966 ipif_t *src_ipif = NULL; 8967 ushort_t ire_marks = 0; 8968 ill_t *dst_ill = NULL; 8969 boolean_t mctl_present; 8970 ipsec_out_t *io; 8971 ipha_t *ipha; 8972 int ihandle = 0; 8973 mblk_t *saved_mp; 8974 ire_t *fire = NULL; 8975 mblk_t *copy_mp = NULL; 8976 boolean_t multirt_resolve_next; 8977 boolean_t unspec_src; 8978 ipaddr_t ipha_dst; 8979 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 8980 8981 /* 8982 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 8983 * here for uniformity 8984 */ 8985 ipif_refhold(ipif); 8986 8987 /* 8988 * This loop is run only once in most cases. 8989 * We loop to resolve further routes only when the destination 8990 * can be reached through multiple RTF_MULTIRT-flagged ires. 8991 */ 8992 do { 8993 if (dst_ill != NULL) { 8994 ill_refrele(dst_ill); 8995 dst_ill = NULL; 8996 } 8997 if (src_ipif != NULL) { 8998 ipif_refrele(src_ipif); 8999 src_ipif = NULL; 9000 } 9001 multirt_resolve_next = B_FALSE; 9002 9003 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 9004 ipif->ipif_ill->ill_name)); 9005 9006 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 9007 if (mctl_present) 9008 io = (ipsec_out_t *)first_mp->b_rptr; 9009 9010 ipha = (ipha_t *)mp->b_rptr; 9011 9012 /* 9013 * Save the packet destination address, we may need it after 9014 * the packet has been consumed. 9015 */ 9016 ipha_dst = ipha->ipha_dst; 9017 9018 /* 9019 * If the interface is a pt-pt interface we look for an 9020 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 9021 * local_address and the pt-pt destination address. Otherwise 9022 * we just match the local address. 9023 * NOTE: dst could be different than ipha->ipha_dst in case 9024 * of sending igmp multicast packets over a point-to-point 9025 * connection. 9026 * Thus we must be careful enough to check ipha_dst to be a 9027 * multicast address, otherwise it will take xmit_if path for 9028 * multicast packets resulting into kernel stack overflow by 9029 * repeated calls to ip_newroute_ipif from ire_send(). 9030 */ 9031 if (CLASSD(ipha_dst) && 9032 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 9033 goto err_ret; 9034 } 9035 9036 /* 9037 * We check if an IRE_OFFSUBNET for the addr that goes through 9038 * ipif exists. We need it to determine if the RTF_SETSRC and/or 9039 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 9040 * propagate its flags to the new ire. 9041 */ 9042 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 9043 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 9044 ip2dbg(("ip_newroute_ipif: " 9045 "ipif_lookup_multi_ire(" 9046 "ipif %p, dst %08x) = fire %p\n", 9047 (void *)ipif, ntohl(dst), (void *)fire)); 9048 } 9049 9050 if (mctl_present && io->ipsec_out_attach_if) { 9051 attach_ill = ip_grab_attach_ill(NULL, first_mp, 9052 io->ipsec_out_ill_index, B_FALSE, ipst); 9053 9054 /* Failure case frees things for us. */ 9055 if (attach_ill == NULL) { 9056 ipif_refrele(ipif); 9057 if (fire != NULL) 9058 ire_refrele(fire); 9059 return; 9060 } 9061 9062 /* 9063 * Check if we need an ire that will not be 9064 * looked up by anybody else i.e. HIDDEN. 9065 */ 9066 if (ill_is_probeonly(attach_ill)) { 9067 ire_marks = IRE_MARK_HIDDEN; 9068 } 9069 /* 9070 * ip_wput passes the right ipif for IPIF_NOFAILOVER 9071 * case. 9072 */ 9073 dst_ill = ipif->ipif_ill; 9074 /* attach_ill has been refheld by ip_grab_attach_ill */ 9075 ASSERT(dst_ill == attach_ill); 9076 } else { 9077 /* 9078 * If the interface belongs to an interface group, 9079 * make sure the next possible interface in the group 9080 * is used. This encourages load spreading among 9081 * peers in an interface group. 9082 * Note: load spreading is disabled for RTF_MULTIRT 9083 * routes. 9084 */ 9085 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9086 (fire->ire_flags & RTF_MULTIRT)) { 9087 /* 9088 * Don't perform outbound load spreading 9089 * in the case of an RTF_MULTIRT issued route, 9090 * we actually typically want to replicate 9091 * outgoing packets through particular 9092 * interfaces. 9093 */ 9094 dst_ill = ipif->ipif_ill; 9095 ill_refhold(dst_ill); 9096 } else { 9097 dst_ill = ip_newroute_get_dst_ill( 9098 ipif->ipif_ill); 9099 } 9100 if (dst_ill == NULL) { 9101 if (ip_debug > 2) { 9102 pr_addr_dbg("ip_newroute_ipif: " 9103 "no dst ill for dst %s\n", 9104 AF_INET, &dst); 9105 } 9106 goto err_ret; 9107 } 9108 } 9109 9110 /* 9111 * Pick a source address preferring non-deprecated ones. 9112 * Unlike ip_newroute, we don't do any source address 9113 * selection here since for multicast it really does not help 9114 * in inbound load spreading as in the unicast case. 9115 */ 9116 if ((flags & RTF_SETSRC) && (fire != NULL) && 9117 (fire->ire_flags & RTF_SETSRC)) { 9118 /* 9119 * As requested by flags, an IRE_OFFSUBNET was looked up 9120 * on that interface. This ire has RTF_SETSRC flag, so 9121 * the source address of the packet must be changed. 9122 * Check that the ipif matching the requested source 9123 * address still exists. 9124 */ 9125 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 9126 zoneid, NULL, NULL, NULL, NULL, ipst); 9127 } 9128 9129 unspec_src = (connp != NULL && connp->conn_unspec_src); 9130 9131 if (((!ipif->ipif_isv6 && ipif->ipif_lcl_addr == INADDR_ANY) || 9132 (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_UP)) != IPIF_UP || 9133 (connp != NULL && ipif->ipif_zoneid != zoneid && 9134 ipif->ipif_zoneid != ALL_ZONES)) && 9135 (src_ipif == NULL) && 9136 (!unspec_src || ipha->ipha_src != INADDR_ANY)) { 9137 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 9138 if (src_ipif == NULL) { 9139 if (ip_debug > 2) { 9140 /* ip1dbg */ 9141 pr_addr_dbg("ip_newroute_ipif: " 9142 "no src for dst %s", 9143 AF_INET, &dst); 9144 } 9145 ip1dbg((" through interface %s\n", 9146 dst_ill->ill_name)); 9147 goto err_ret; 9148 } 9149 ipif_refrele(ipif); 9150 ipif = src_ipif; 9151 ipif_refhold(ipif); 9152 } 9153 if (src_ipif == NULL) { 9154 src_ipif = ipif; 9155 ipif_refhold(src_ipif); 9156 } 9157 9158 /* 9159 * Assign a source address while we have the conn. 9160 * We can't have ip_wput_ire pick a source address when the 9161 * packet returns from arp since conn_unspec_src might be set 9162 * and we lose the conn when going through arp. 9163 */ 9164 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 9165 ipha->ipha_src = src_ipif->ipif_src_addr; 9166 9167 /* 9168 * In the case of IP_BOUND_IF and IP_PKTINFO, it is possible 9169 * that the outgoing interface does not have an interface ire. 9170 */ 9171 if (CLASSD(ipha_dst) && (connp == NULL || 9172 connp->conn_outgoing_ill == NULL) && 9173 infop->ip_opt_ill_index == 0) { 9174 /* ipif_to_ire returns an held ire */ 9175 ire = ipif_to_ire(ipif); 9176 if (ire == NULL) 9177 goto err_ret; 9178 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 9179 goto err_ret; 9180 /* 9181 * ihandle is needed when the ire is added to 9182 * cache table. 9183 */ 9184 save_ire = ire; 9185 ihandle = save_ire->ire_ihandle; 9186 9187 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 9188 "flags %04x\n", 9189 (void *)ire, (void *)ipif, flags)); 9190 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9191 (fire->ire_flags & RTF_MULTIRT)) { 9192 /* 9193 * As requested by flags, an IRE_OFFSUBNET was 9194 * looked up on that interface. This ire has 9195 * RTF_MULTIRT flag, so the resolution loop will 9196 * be re-entered to resolve additional routes on 9197 * other interfaces. For that purpose, a copy of 9198 * the packet is performed at this point. 9199 */ 9200 fire->ire_last_used_time = lbolt; 9201 copy_mp = copymsg(first_mp); 9202 if (copy_mp) { 9203 MULTIRT_DEBUG_TAG(copy_mp); 9204 } 9205 } 9206 if ((flags & RTF_SETSRC) && (fire != NULL) && 9207 (fire->ire_flags & RTF_SETSRC)) { 9208 /* 9209 * As requested by flags, an IRE_OFFSUBET was 9210 * looked up on that interface. This ire has 9211 * RTF_SETSRC flag, so the source address of the 9212 * packet must be changed. 9213 */ 9214 ipha->ipha_src = fire->ire_src_addr; 9215 } 9216 } else { 9217 ASSERT((connp == NULL) || 9218 (connp->conn_outgoing_ill != NULL) || 9219 (connp->conn_dontroute) || 9220 infop->ip_opt_ill_index != 0); 9221 /* 9222 * The only ways we can come here are: 9223 * 1) IP_BOUND_IF socket option is set 9224 * 2) SO_DONTROUTE socket option is set 9225 * 3) IP_PKTINFO option is passed in as ancillary data. 9226 * In all cases, the new ire will not be added 9227 * into cache table. 9228 */ 9229 ire_marks |= IRE_MARK_NOADD; 9230 } 9231 9232 switch (ipif->ipif_net_type) { 9233 case IRE_IF_NORESOLVER: { 9234 /* We have what we need to build an IRE_CACHE. */ 9235 9236 if ((dst_ill->ill_phys_addr_length != IP_ADDR_LEN) && 9237 (dst_ill->ill_resolver_mp == NULL)) { 9238 ip1dbg(("ip_newroute_ipif: dst_ill %p " 9239 "for IRE_IF_NORESOLVER ire %p has " 9240 "no ill_resolver_mp\n", 9241 (void *)dst_ill, (void *)ire)); 9242 break; 9243 } 9244 9245 /* 9246 * The new ire inherits the IRE_OFFSUBNET flags 9247 * and source address, if this was requested. 9248 */ 9249 ire = ire_create( 9250 (uchar_t *)&dst, /* dest address */ 9251 (uchar_t *)&ip_g_all_ones, /* mask */ 9252 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9253 NULL, /* gateway address */ 9254 &ipif->ipif_mtu, 9255 NULL, /* no src nce */ 9256 dst_ill->ill_rq, /* recv-from queue */ 9257 dst_ill->ill_wq, /* send-to queue */ 9258 IRE_CACHE, 9259 src_ipif, 9260 (save_ire != NULL ? save_ire->ire_mask : 0), 9261 (fire != NULL) ? /* Parent handle */ 9262 fire->ire_phandle : 0, 9263 ihandle, /* Interface handle */ 9264 (fire != NULL) ? 9265 (fire->ire_flags & 9266 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9267 (save_ire == NULL ? &ire_uinfo_null : 9268 &save_ire->ire_uinfo), 9269 NULL, 9270 NULL, 9271 ipst); 9272 9273 if (ire == NULL) { 9274 if (save_ire != NULL) 9275 ire_refrele(save_ire); 9276 break; 9277 } 9278 9279 ire->ire_marks |= ire_marks; 9280 9281 /* 9282 * If IRE_MARK_NOADD is set then we need to convert 9283 * the max_fragp to a useable value now. This is 9284 * normally done in ire_add_v[46]. We also need to 9285 * associate the ire with an nce (normally would be 9286 * done in ip_wput_nondata()). 9287 * 9288 * Note that IRE_MARK_NOADD packets created here 9289 * do not have a non-null ire_mp pointer. The null 9290 * value of ire_bucket indicates that they were 9291 * never added. 9292 */ 9293 if (ire->ire_marks & IRE_MARK_NOADD) { 9294 uint_t max_frag; 9295 9296 max_frag = *ire->ire_max_fragp; 9297 ire->ire_max_fragp = NULL; 9298 ire->ire_max_frag = max_frag; 9299 9300 if ((ire->ire_nce = ndp_lookup_v4( 9301 ire_to_ill(ire), 9302 (ire->ire_gateway_addr != INADDR_ANY ? 9303 &ire->ire_gateway_addr : &ire->ire_addr), 9304 B_FALSE)) == NULL) { 9305 if (save_ire != NULL) 9306 ire_refrele(save_ire); 9307 break; 9308 } 9309 ASSERT(ire->ire_nce->nce_state == 9310 ND_REACHABLE); 9311 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 9312 } 9313 9314 /* Prevent save_ire from getting deleted */ 9315 if (save_ire != NULL) { 9316 IRB_REFHOLD(save_ire->ire_bucket); 9317 /* Has it been removed already ? */ 9318 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 9319 IRB_REFRELE(save_ire->ire_bucket); 9320 ire_refrele(save_ire); 9321 break; 9322 } 9323 } 9324 9325 ire_add_then_send(q, ire, first_mp); 9326 9327 /* Assert that save_ire is not deleted yet. */ 9328 if (save_ire != NULL) { 9329 ASSERT(save_ire->ire_ptpn != NULL); 9330 IRB_REFRELE(save_ire->ire_bucket); 9331 ire_refrele(save_ire); 9332 save_ire = NULL; 9333 } 9334 if (fire != NULL) { 9335 ire_refrele(fire); 9336 fire = NULL; 9337 } 9338 9339 /* 9340 * the resolution loop is re-entered if this 9341 * was requested through flags and if we 9342 * actually are in a multirouting case. 9343 */ 9344 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9345 boolean_t need_resolve = 9346 ire_multirt_need_resolve(ipha_dst, 9347 MBLK_GETLABEL(copy_mp), ipst); 9348 if (!need_resolve) { 9349 MULTIRT_DEBUG_UNTAG(copy_mp); 9350 freemsg(copy_mp); 9351 copy_mp = NULL; 9352 } else { 9353 /* 9354 * ipif_lookup_group() calls 9355 * ire_lookup_multi() that uses 9356 * ire_ftable_lookup() to find 9357 * an IRE_INTERFACE for the group. 9358 * In the multirt case, 9359 * ire_lookup_multi() then invokes 9360 * ire_multirt_lookup() to find 9361 * the next resolvable ire. 9362 * As a result, we obtain an new 9363 * interface, derived from the 9364 * next ire. 9365 */ 9366 ipif_refrele(ipif); 9367 ipif = ipif_lookup_group(ipha_dst, 9368 zoneid, ipst); 9369 ip2dbg(("ip_newroute_ipif: " 9370 "multirt dst %08x, ipif %p\n", 9371 htonl(dst), (void *)ipif)); 9372 if (ipif != NULL) { 9373 mp = copy_mp; 9374 copy_mp = NULL; 9375 multirt_resolve_next = B_TRUE; 9376 continue; 9377 } else { 9378 freemsg(copy_mp); 9379 } 9380 } 9381 } 9382 if (ipif != NULL) 9383 ipif_refrele(ipif); 9384 ill_refrele(dst_ill); 9385 ipif_refrele(src_ipif); 9386 return; 9387 } 9388 case IRE_IF_RESOLVER: 9389 /* 9390 * We can't build an IRE_CACHE yet, but at least 9391 * we found a resolver that can help. 9392 */ 9393 res_mp = dst_ill->ill_resolver_mp; 9394 if (!OK_RESOLVER_MP(res_mp)) 9395 break; 9396 9397 /* 9398 * We obtain a partial IRE_CACHE which we will pass 9399 * along with the resolver query. When the response 9400 * comes back it will be there ready for us to add. 9401 * The new ire inherits the IRE_OFFSUBNET flags 9402 * and source address, if this was requested. 9403 * The ire_max_frag is atomically set under the 9404 * irebucket lock in ire_add_v[46]. Only in the 9405 * case of IRE_MARK_NOADD, we set it here itself. 9406 */ 9407 ire = ire_create_mp( 9408 (uchar_t *)&dst, /* dest address */ 9409 (uchar_t *)&ip_g_all_ones, /* mask */ 9410 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9411 NULL, /* gateway address */ 9412 (ire_marks & IRE_MARK_NOADD) ? 9413 ipif->ipif_mtu : 0, /* max_frag */ 9414 NULL, /* no src nce */ 9415 dst_ill->ill_rq, /* recv-from queue */ 9416 dst_ill->ill_wq, /* send-to queue */ 9417 IRE_CACHE, 9418 src_ipif, 9419 (save_ire != NULL ? save_ire->ire_mask : 0), 9420 (fire != NULL) ? /* Parent handle */ 9421 fire->ire_phandle : 0, 9422 ihandle, /* Interface handle */ 9423 (fire != NULL) ? /* flags if any */ 9424 (fire->ire_flags & 9425 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9426 (save_ire == NULL ? &ire_uinfo_null : 9427 &save_ire->ire_uinfo), 9428 NULL, 9429 NULL, 9430 ipst); 9431 9432 if (save_ire != NULL) { 9433 ire_refrele(save_ire); 9434 save_ire = NULL; 9435 } 9436 if (ire == NULL) 9437 break; 9438 9439 ire->ire_marks |= ire_marks; 9440 /* 9441 * Construct message chain for the resolver of the 9442 * form: 9443 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 9444 * 9445 * NOTE : ire will be added later when the response 9446 * comes back from ARP. If the response does not 9447 * come back, ARP frees the packet. For this reason, 9448 * we can't REFHOLD the bucket of save_ire to prevent 9449 * deletions. We may not be able to REFRELE the 9450 * bucket if the response never comes back. 9451 * Thus, before adding the ire, ire_add_v4 will make 9452 * sure that the interface route does not get deleted. 9453 * This is the only case unlike ip_newroute_v6, 9454 * ip_newroute_ipif_v6 where we can always prevent 9455 * deletions because ire_add_then_send is called after 9456 * creating the IRE. 9457 * If IRE_MARK_NOADD is set, then ire_add_then_send 9458 * does not add this IRE into the IRE CACHE. 9459 */ 9460 ASSERT(ire->ire_mp != NULL); 9461 ire->ire_mp->b_cont = first_mp; 9462 /* Have saved_mp handy, for cleanup if canput fails */ 9463 saved_mp = mp; 9464 mp = copyb(res_mp); 9465 if (mp == NULL) { 9466 /* Prepare for cleanup */ 9467 mp = saved_mp; /* pkt */ 9468 ire_delete(ire); /* ire_mp */ 9469 ire = NULL; 9470 if (copy_mp != NULL) { 9471 MULTIRT_DEBUG_UNTAG(copy_mp); 9472 freemsg(copy_mp); 9473 copy_mp = NULL; 9474 } 9475 break; 9476 } 9477 linkb(mp, ire->ire_mp); 9478 9479 /* 9480 * Fill in the source and dest addrs for the resolver. 9481 * NOTE: this depends on memory layouts imposed by 9482 * ill_init(). 9483 */ 9484 areq = (areq_t *)mp->b_rptr; 9485 addrp = (ipaddr_t *)((char *)areq + 9486 areq->areq_sender_addr_offset); 9487 *addrp = ire->ire_src_addr; 9488 addrp = (ipaddr_t *)((char *)areq + 9489 areq->areq_target_addr_offset); 9490 *addrp = dst; 9491 /* Up to the resolver. */ 9492 if (canputnext(dst_ill->ill_rq) && 9493 !(dst_ill->ill_arp_closing)) { 9494 putnext(dst_ill->ill_rq, mp); 9495 /* 9496 * The response will come back in ip_wput 9497 * with db_type IRE_DB_TYPE. 9498 */ 9499 } else { 9500 mp->b_cont = NULL; 9501 freeb(mp); /* areq */ 9502 ire_delete(ire); /* ire_mp */ 9503 saved_mp->b_next = NULL; 9504 saved_mp->b_prev = NULL; 9505 freemsg(first_mp); /* pkt */ 9506 ip2dbg(("ip_newroute_ipif: dropped\n")); 9507 } 9508 9509 if (fire != NULL) { 9510 ire_refrele(fire); 9511 fire = NULL; 9512 } 9513 9514 9515 /* 9516 * The resolution loop is re-entered if this was 9517 * requested through flags and we actually are 9518 * in a multirouting case. 9519 */ 9520 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9521 boolean_t need_resolve = 9522 ire_multirt_need_resolve(ipha_dst, 9523 MBLK_GETLABEL(copy_mp), ipst); 9524 if (!need_resolve) { 9525 MULTIRT_DEBUG_UNTAG(copy_mp); 9526 freemsg(copy_mp); 9527 copy_mp = NULL; 9528 } else { 9529 /* 9530 * ipif_lookup_group() calls 9531 * ire_lookup_multi() that uses 9532 * ire_ftable_lookup() to find 9533 * an IRE_INTERFACE for the group. 9534 * In the multirt case, 9535 * ire_lookup_multi() then invokes 9536 * ire_multirt_lookup() to find 9537 * the next resolvable ire. 9538 * As a result, we obtain an new 9539 * interface, derived from the 9540 * next ire. 9541 */ 9542 ipif_refrele(ipif); 9543 ipif = ipif_lookup_group(ipha_dst, 9544 zoneid, ipst); 9545 if (ipif != NULL) { 9546 mp = copy_mp; 9547 copy_mp = NULL; 9548 multirt_resolve_next = B_TRUE; 9549 continue; 9550 } else { 9551 freemsg(copy_mp); 9552 } 9553 } 9554 } 9555 if (ipif != NULL) 9556 ipif_refrele(ipif); 9557 ill_refrele(dst_ill); 9558 ipif_refrele(src_ipif); 9559 return; 9560 default: 9561 break; 9562 } 9563 } while (multirt_resolve_next); 9564 9565 err_ret: 9566 ip2dbg(("ip_newroute_ipif: dropped\n")); 9567 if (fire != NULL) 9568 ire_refrele(fire); 9569 ipif_refrele(ipif); 9570 /* Did this packet originate externally? */ 9571 if (dst_ill != NULL) 9572 ill_refrele(dst_ill); 9573 if (src_ipif != NULL) 9574 ipif_refrele(src_ipif); 9575 if (mp->b_prev || mp->b_next) { 9576 mp->b_next = NULL; 9577 mp->b_prev = NULL; 9578 } else { 9579 /* 9580 * Since ip_wput() isn't close to finished, we fill 9581 * in enough of the header for credible error reporting. 9582 */ 9583 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 9584 /* Failed */ 9585 freemsg(first_mp); 9586 if (ire != NULL) 9587 ire_refrele(ire); 9588 return; 9589 } 9590 } 9591 /* 9592 * At this point we will have ire only if RTF_BLACKHOLE 9593 * or RTF_REJECT flags are set on the IRE. It will not 9594 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9595 */ 9596 if (ire != NULL) { 9597 if (ire->ire_flags & RTF_BLACKHOLE) { 9598 ire_refrele(ire); 9599 freemsg(first_mp); 9600 return; 9601 } 9602 ire_refrele(ire); 9603 } 9604 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9605 } 9606 9607 /* Name/Value Table Lookup Routine */ 9608 char * 9609 ip_nv_lookup(nv_t *nv, int value) 9610 { 9611 if (!nv) 9612 return (NULL); 9613 for (; nv->nv_name; nv++) { 9614 if (nv->nv_value == value) 9615 return (nv->nv_name); 9616 } 9617 return ("unknown"); 9618 } 9619 9620 /* 9621 * This is a module open, i.e. this is a control stream for access 9622 * to a DLPI device. We allocate an ill_t as the instance data in 9623 * this case. 9624 */ 9625 int 9626 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9627 { 9628 ill_t *ill; 9629 int err; 9630 zoneid_t zoneid; 9631 netstack_t *ns; 9632 ip_stack_t *ipst; 9633 9634 /* 9635 * Prevent unprivileged processes from pushing IP so that 9636 * they can't send raw IP. 9637 */ 9638 if (secpolicy_net_rawaccess(credp) != 0) 9639 return (EPERM); 9640 9641 ns = netstack_find_by_cred(credp); 9642 ASSERT(ns != NULL); 9643 ipst = ns->netstack_ip; 9644 ASSERT(ipst != NULL); 9645 9646 /* 9647 * For exclusive stacks we set the zoneid to zero 9648 * to make IP operate as if in the global zone. 9649 */ 9650 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9651 zoneid = GLOBAL_ZONEID; 9652 else 9653 zoneid = crgetzoneid(credp); 9654 9655 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 9656 q->q_ptr = WR(q)->q_ptr = ill; 9657 ill->ill_ipst = ipst; 9658 ill->ill_zoneid = zoneid; 9659 9660 /* 9661 * ill_init initializes the ill fields and then sends down 9662 * down a DL_INFO_REQ after calling qprocson. 9663 */ 9664 err = ill_init(q, ill); 9665 if (err != 0) { 9666 mi_free(ill); 9667 netstack_rele(ipst->ips_netstack); 9668 q->q_ptr = NULL; 9669 WR(q)->q_ptr = NULL; 9670 return (err); 9671 } 9672 9673 /* ill_init initializes the ipsq marking this thread as writer */ 9674 ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE, B_TRUE); 9675 /* Wait for the DL_INFO_ACK */ 9676 mutex_enter(&ill->ill_lock); 9677 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 9678 /* 9679 * Return value of 0 indicates a pending signal. 9680 */ 9681 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 9682 if (err == 0) { 9683 mutex_exit(&ill->ill_lock); 9684 (void) ip_close(q, 0); 9685 return (EINTR); 9686 } 9687 } 9688 mutex_exit(&ill->ill_lock); 9689 9690 /* 9691 * ip_rput_other could have set an error in ill_error on 9692 * receipt of M_ERROR. 9693 */ 9694 9695 err = ill->ill_error; 9696 if (err != 0) { 9697 (void) ip_close(q, 0); 9698 return (err); 9699 } 9700 9701 ill->ill_credp = credp; 9702 crhold(credp); 9703 9704 mutex_enter(&ipst->ips_ip_mi_lock); 9705 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag, 9706 credp); 9707 mutex_exit(&ipst->ips_ip_mi_lock); 9708 if (err) { 9709 (void) ip_close(q, 0); 9710 return (err); 9711 } 9712 return (0); 9713 } 9714 9715 /* For /dev/ip aka AF_INET open */ 9716 int 9717 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9718 { 9719 return (ip_open(q, devp, flag, sflag, credp, B_FALSE)); 9720 } 9721 9722 /* For /dev/ip6 aka AF_INET6 open */ 9723 int 9724 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9725 { 9726 return (ip_open(q, devp, flag, sflag, credp, B_TRUE)); 9727 } 9728 9729 /* IP open routine. */ 9730 int 9731 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9732 boolean_t isv6) 9733 { 9734 conn_t *connp; 9735 major_t maj; 9736 zoneid_t zoneid; 9737 netstack_t *ns; 9738 ip_stack_t *ipst; 9739 9740 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 9741 9742 /* Allow reopen. */ 9743 if (q->q_ptr != NULL) 9744 return (0); 9745 9746 if (sflag & MODOPEN) { 9747 /* This is a module open */ 9748 return (ip_modopen(q, devp, flag, sflag, credp)); 9749 } 9750 9751 ns = netstack_find_by_cred(credp); 9752 ASSERT(ns != NULL); 9753 ipst = ns->netstack_ip; 9754 ASSERT(ipst != NULL); 9755 9756 /* 9757 * For exclusive stacks we set the zoneid to zero 9758 * to make IP operate as if in the global zone. 9759 */ 9760 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9761 zoneid = GLOBAL_ZONEID; 9762 else 9763 zoneid = crgetzoneid(credp); 9764 9765 /* 9766 * We are opening as a device. This is an IP client stream, and we 9767 * allocate an conn_t as the instance data. 9768 */ 9769 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack); 9770 9771 /* 9772 * ipcl_conn_create did a netstack_hold. Undo the hold that was 9773 * done by netstack_find_by_cred() 9774 */ 9775 netstack_rele(ipst->ips_netstack); 9776 9777 connp->conn_zoneid = zoneid; 9778 9779 connp->conn_upq = q; 9780 q->q_ptr = WR(q)->q_ptr = connp; 9781 9782 if (flag & SO_SOCKSTR) 9783 connp->conn_flags |= IPCL_SOCKET; 9784 9785 /* Minor tells us which /dev entry was opened */ 9786 if (isv6) { 9787 connp->conn_flags |= IPCL_ISV6; 9788 connp->conn_af_isv6 = B_TRUE; 9789 ip_setpktversion(connp, isv6, B_FALSE, ipst); 9790 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9791 } else { 9792 connp->conn_af_isv6 = B_FALSE; 9793 connp->conn_pkt_isv6 = B_FALSE; 9794 } 9795 9796 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9797 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9798 connp->conn_minor_arena = ip_minor_arena_la; 9799 } else { 9800 /* 9801 * Either minor numbers in the large arena were exhausted 9802 * or a non socket application is doing the open. 9803 * Try to allocate from the small arena. 9804 */ 9805 if ((connp->conn_dev = 9806 inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9807 /* CONN_DEC_REF takes care of netstack_rele() */ 9808 q->q_ptr = WR(q)->q_ptr = NULL; 9809 CONN_DEC_REF(connp); 9810 return (EBUSY); 9811 } 9812 connp->conn_minor_arena = ip_minor_arena_sa; 9813 } 9814 9815 maj = getemajor(*devp); 9816 *devp = makedevice(maj, (minor_t)connp->conn_dev); 9817 9818 /* 9819 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 9820 */ 9821 connp->conn_cred = credp; 9822 9823 /* 9824 * Handle IP_RTS_REQUEST and other ioctls which use conn_recv 9825 */ 9826 connp->conn_recv = ip_conn_input; 9827 9828 crhold(connp->conn_cred); 9829 9830 /* 9831 * If the caller has the process-wide flag set, then default to MAC 9832 * exempt mode. This allows read-down to unlabeled hosts. 9833 */ 9834 if (getpflags(NET_MAC_AWARE, credp) != 0) 9835 connp->conn_mac_exempt = B_TRUE; 9836 9837 connp->conn_rq = q; 9838 connp->conn_wq = WR(q); 9839 9840 /* Non-zero default values */ 9841 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9842 9843 /* 9844 * Make the conn globally visible to walkers 9845 */ 9846 ASSERT(connp->conn_ref == 1); 9847 mutex_enter(&connp->conn_lock); 9848 connp->conn_state_flags &= ~CONN_INCIPIENT; 9849 mutex_exit(&connp->conn_lock); 9850 9851 qprocson(q); 9852 9853 return (0); 9854 } 9855 9856 /* 9857 * Change the output format (IPv4 vs. IPv6) for a conn_t. 9858 * Note that there is no race since either ip_output function works - it 9859 * is just an optimization to enter the best ip_output routine directly. 9860 */ 9861 void 9862 ip_setpktversion(conn_t *connp, boolean_t isv6, boolean_t bump_mib, 9863 ip_stack_t *ipst) 9864 { 9865 if (isv6) { 9866 if (bump_mib) { 9867 BUMP_MIB(&ipst->ips_ip6_mib, 9868 ipIfStatsOutSwitchIPVersion); 9869 } 9870 connp->conn_send = ip_output_v6; 9871 connp->conn_pkt_isv6 = B_TRUE; 9872 } else { 9873 if (bump_mib) { 9874 BUMP_MIB(&ipst->ips_ip_mib, 9875 ipIfStatsOutSwitchIPVersion); 9876 } 9877 connp->conn_send = ip_output; 9878 connp->conn_pkt_isv6 = B_FALSE; 9879 } 9880 9881 } 9882 9883 /* 9884 * See if IPsec needs loading because of the options in mp. 9885 */ 9886 static boolean_t 9887 ipsec_opt_present(mblk_t *mp) 9888 { 9889 uint8_t *optcp, *next_optcp, *opt_endcp; 9890 struct opthdr *opt; 9891 struct T_opthdr *topt; 9892 int opthdr_len; 9893 t_uscalar_t optname, optlevel; 9894 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 9895 ipsec_req_t *ipsr; 9896 9897 /* 9898 * Walk through the mess, and find IP_SEC_OPT. If it's there, 9899 * return TRUE. 9900 */ 9901 9902 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 9903 opt_endcp = optcp + tor->OPT_length; 9904 if (tor->PRIM_type == T_OPTMGMT_REQ) { 9905 opthdr_len = sizeof (struct T_opthdr); 9906 } else { /* O_OPTMGMT_REQ */ 9907 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 9908 opthdr_len = sizeof (struct opthdr); 9909 } 9910 for (; optcp < opt_endcp; optcp = next_optcp) { 9911 if (optcp + opthdr_len > opt_endcp) 9912 return (B_FALSE); /* Not enough option header. */ 9913 if (tor->PRIM_type == T_OPTMGMT_REQ) { 9914 topt = (struct T_opthdr *)optcp; 9915 optlevel = topt->level; 9916 optname = topt->name; 9917 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 9918 } else { 9919 opt = (struct opthdr *)optcp; 9920 optlevel = opt->level; 9921 optname = opt->name; 9922 next_optcp = optcp + opthdr_len + 9923 _TPI_ALIGN_OPT(opt->len); 9924 } 9925 if ((next_optcp < optcp) || /* wraparound pointer space */ 9926 ((next_optcp >= opt_endcp) && /* last option bad len */ 9927 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 9928 return (B_FALSE); /* bad option buffer */ 9929 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 9930 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 9931 /* 9932 * Check to see if it's an all-bypass or all-zeroes 9933 * IPsec request. Don't bother loading IPsec if 9934 * the socket doesn't want to use it. (A good example 9935 * is a bypass request.) 9936 * 9937 * Basically, if any of the non-NEVER bits are set, 9938 * load IPsec. 9939 */ 9940 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 9941 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 9942 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 9943 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 9944 != 0) 9945 return (B_TRUE); 9946 } 9947 } 9948 return (B_FALSE); 9949 } 9950 9951 /* 9952 * If conn is is waiting for ipsec to finish loading, kick it. 9953 */ 9954 /* ARGSUSED */ 9955 static void 9956 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 9957 { 9958 t_scalar_t optreq_prim; 9959 mblk_t *mp; 9960 cred_t *cr; 9961 int err = 0; 9962 9963 /* 9964 * This function is called, after ipsec loading is complete. 9965 * Since IP checks exclusively and atomically (i.e it prevents 9966 * ipsec load from completing until ip_optcom_req completes) 9967 * whether ipsec load is complete, there cannot be a race with IP 9968 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 9969 */ 9970 mutex_enter(&connp->conn_lock); 9971 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 9972 ASSERT(connp->conn_ipsec_opt_mp != NULL); 9973 mp = connp->conn_ipsec_opt_mp; 9974 connp->conn_ipsec_opt_mp = NULL; 9975 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 9976 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); 9977 mutex_exit(&connp->conn_lock); 9978 9979 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 9980 9981 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 9982 if (optreq_prim == T_OPTMGMT_REQ) { 9983 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 9984 &ip_opt_obj, B_FALSE); 9985 } else { 9986 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 9987 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 9988 &ip_opt_obj, B_FALSE); 9989 } 9990 if (err != EINPROGRESS) 9991 CONN_OPER_PENDING_DONE(connp); 9992 return; 9993 } 9994 mutex_exit(&connp->conn_lock); 9995 } 9996 9997 /* 9998 * Called from the ipsec_loader thread, outside any perimeter, to tell 9999 * ip qenable any of the queues waiting for the ipsec loader to 10000 * complete. 10001 */ 10002 void 10003 ip_ipsec_load_complete(ipsec_stack_t *ipss) 10004 { 10005 netstack_t *ns = ipss->ipsec_netstack; 10006 10007 ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip); 10008 } 10009 10010 /* 10011 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 10012 * determines the grp on which it has to become exclusive, queues the mp 10013 * and sq draining restarts the optmgmt 10014 */ 10015 static boolean_t 10016 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 10017 { 10018 conn_t *connp = Q_TO_CONN(q); 10019 ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec; 10020 10021 /* 10022 * Take IPsec requests and treat them special. 10023 */ 10024 if (ipsec_opt_present(mp)) { 10025 /* First check if IPsec is loaded. */ 10026 mutex_enter(&ipss->ipsec_loader_lock); 10027 if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) { 10028 mutex_exit(&ipss->ipsec_loader_lock); 10029 return (B_FALSE); 10030 } 10031 mutex_enter(&connp->conn_lock); 10032 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 10033 10034 ASSERT(connp->conn_ipsec_opt_mp == NULL); 10035 connp->conn_ipsec_opt_mp = mp; 10036 mutex_exit(&connp->conn_lock); 10037 mutex_exit(&ipss->ipsec_loader_lock); 10038 10039 ipsec_loader_loadnow(ipss); 10040 return (B_TRUE); 10041 } 10042 return (B_FALSE); 10043 } 10044 10045 /* 10046 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 10047 * all of them are copied to the conn_t. If the req is "zero", the policy is 10048 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 10049 * fields. 10050 * We keep only the latest setting of the policy and thus policy setting 10051 * is not incremental/cumulative. 10052 * 10053 * Requests to set policies with multiple alternative actions will 10054 * go through a different API. 10055 */ 10056 int 10057 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 10058 { 10059 uint_t ah_req = 0; 10060 uint_t esp_req = 0; 10061 uint_t se_req = 0; 10062 ipsec_selkey_t sel; 10063 ipsec_act_t *actp = NULL; 10064 uint_t nact; 10065 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 10066 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 10067 ipsec_policy_root_t *pr; 10068 ipsec_policy_head_t *ph; 10069 int fam; 10070 boolean_t is_pol_reset; 10071 int error = 0; 10072 netstack_t *ns = connp->conn_netstack; 10073 ip_stack_t *ipst = ns->netstack_ip; 10074 ipsec_stack_t *ipss = ns->netstack_ipsec; 10075 10076 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 10077 10078 /* 10079 * The IP_SEC_OPT option does not allow variable length parameters, 10080 * hence a request cannot be NULL. 10081 */ 10082 if (req == NULL) 10083 return (EINVAL); 10084 10085 ah_req = req->ipsr_ah_req; 10086 esp_req = req->ipsr_esp_req; 10087 se_req = req->ipsr_self_encap_req; 10088 10089 /* 10090 * Are we dealing with a request to reset the policy (i.e. 10091 * zero requests). 10092 */ 10093 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 10094 (esp_req & REQ_MASK) == 0 && 10095 (se_req & REQ_MASK) == 0); 10096 10097 if (!is_pol_reset) { 10098 /* 10099 * If we couldn't load IPsec, fail with "protocol 10100 * not supported". 10101 * IPsec may not have been loaded for a request with zero 10102 * policies, so we don't fail in this case. 10103 */ 10104 mutex_enter(&ipss->ipsec_loader_lock); 10105 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 10106 mutex_exit(&ipss->ipsec_loader_lock); 10107 return (EPROTONOSUPPORT); 10108 } 10109 mutex_exit(&ipss->ipsec_loader_lock); 10110 10111 /* 10112 * Test for valid requests. Invalid algorithms 10113 * need to be tested by IPsec code because new 10114 * algorithms can be added dynamically. 10115 */ 10116 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10117 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10118 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 10119 return (EINVAL); 10120 } 10121 10122 /* 10123 * Only privileged users can issue these 10124 * requests. 10125 */ 10126 if (((ah_req & IPSEC_PREF_NEVER) || 10127 (esp_req & IPSEC_PREF_NEVER) || 10128 (se_req & IPSEC_PREF_NEVER)) && 10129 secpolicy_ip_config(cr, B_FALSE) != 0) { 10130 return (EPERM); 10131 } 10132 10133 /* 10134 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 10135 * are mutually exclusive. 10136 */ 10137 if (((ah_req & REQ_MASK) == REQ_MASK) || 10138 ((esp_req & REQ_MASK) == REQ_MASK) || 10139 ((se_req & REQ_MASK) == REQ_MASK)) { 10140 /* Both of them are set */ 10141 return (EINVAL); 10142 } 10143 } 10144 10145 mutex_enter(&connp->conn_lock); 10146 10147 /* 10148 * If we have already cached policies in ip_bind_connected*(), don't 10149 * let them change now. We cache policies for connections 10150 * whose src,dst [addr, port] is known. 10151 */ 10152 if (connp->conn_policy_cached) { 10153 mutex_exit(&connp->conn_lock); 10154 return (EINVAL); 10155 } 10156 10157 /* 10158 * We have a zero policies, reset the connection policy if already 10159 * set. This will cause the connection to inherit the 10160 * global policy, if any. 10161 */ 10162 if (is_pol_reset) { 10163 if (connp->conn_policy != NULL) { 10164 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack); 10165 connp->conn_policy = NULL; 10166 } 10167 connp->conn_flags &= ~IPCL_CHECK_POLICY; 10168 connp->conn_in_enforce_policy = B_FALSE; 10169 connp->conn_out_enforce_policy = B_FALSE; 10170 mutex_exit(&connp->conn_lock); 10171 return (0); 10172 } 10173 10174 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy, 10175 ipst->ips_netstack); 10176 if (ph == NULL) 10177 goto enomem; 10178 10179 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack); 10180 if (actp == NULL) 10181 goto enomem; 10182 10183 /* 10184 * Always allocate IPv4 policy entries, since they can also 10185 * apply to ipv6 sockets being used in ipv4-compat mode. 10186 */ 10187 bzero(&sel, sizeof (sel)); 10188 sel.ipsl_valid = IPSL_IPV4; 10189 10190 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10191 ipst->ips_netstack); 10192 if (pin4 == NULL) 10193 goto enomem; 10194 10195 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10196 ipst->ips_netstack); 10197 if (pout4 == NULL) 10198 goto enomem; 10199 10200 if (connp->conn_af_isv6) { 10201 /* 10202 * We're looking at a v6 socket, also allocate the 10203 * v6-specific entries... 10204 */ 10205 sel.ipsl_valid = IPSL_IPV6; 10206 pin6 = ipsec_policy_create(&sel, actp, nact, 10207 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10208 if (pin6 == NULL) 10209 goto enomem; 10210 10211 pout6 = ipsec_policy_create(&sel, actp, nact, 10212 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10213 if (pout6 == NULL) 10214 goto enomem; 10215 10216 /* 10217 * .. and file them away in the right place. 10218 */ 10219 fam = IPSEC_AF_V6; 10220 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10221 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 10222 ipsec_insert_always(&ph->iph_rulebyid, pin6); 10223 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10224 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 10225 ipsec_insert_always(&ph->iph_rulebyid, pout6); 10226 } 10227 10228 ipsec_actvec_free(actp, nact); 10229 10230 /* 10231 * File the v4 policies. 10232 */ 10233 fam = IPSEC_AF_V4; 10234 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10235 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 10236 ipsec_insert_always(&ph->iph_rulebyid, pin4); 10237 10238 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10239 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 10240 ipsec_insert_always(&ph->iph_rulebyid, pout4); 10241 10242 /* 10243 * If the requests need security, set enforce_policy. 10244 * If the requests are IPSEC_PREF_NEVER, one should 10245 * still set conn_out_enforce_policy so that an ipsec_out 10246 * gets attached in ip_wput. This is needed so that 10247 * for connections that we don't cache policy in ip_bind, 10248 * if global policy matches in ip_wput_attach_policy, we 10249 * don't wrongly inherit global policy. Similarly, we need 10250 * to set conn_in_enforce_policy also so that we don't verify 10251 * policy wrongly. 10252 */ 10253 if ((ah_req & REQ_MASK) != 0 || 10254 (esp_req & REQ_MASK) != 0 || 10255 (se_req & REQ_MASK) != 0) { 10256 connp->conn_in_enforce_policy = B_TRUE; 10257 connp->conn_out_enforce_policy = B_TRUE; 10258 connp->conn_flags |= IPCL_CHECK_POLICY; 10259 } 10260 10261 mutex_exit(&connp->conn_lock); 10262 return (error); 10263 #undef REQ_MASK 10264 10265 /* 10266 * Common memory-allocation-failure exit path. 10267 */ 10268 enomem: 10269 mutex_exit(&connp->conn_lock); 10270 if (actp != NULL) 10271 ipsec_actvec_free(actp, nact); 10272 if (pin4 != NULL) 10273 IPPOL_REFRELE(pin4, ipst->ips_netstack); 10274 if (pout4 != NULL) 10275 IPPOL_REFRELE(pout4, ipst->ips_netstack); 10276 if (pin6 != NULL) 10277 IPPOL_REFRELE(pin6, ipst->ips_netstack); 10278 if (pout6 != NULL) 10279 IPPOL_REFRELE(pout6, ipst->ips_netstack); 10280 return (ENOMEM); 10281 } 10282 10283 /* 10284 * Only for options that pass in an IP addr. Currently only V4 options 10285 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 10286 * So this function assumes level is IPPROTO_IP 10287 */ 10288 int 10289 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 10290 mblk_t *first_mp) 10291 { 10292 ipif_t *ipif = NULL; 10293 int error; 10294 ill_t *ill; 10295 int zoneid; 10296 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10297 10298 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 10299 10300 if (addr != INADDR_ANY || checkonly) { 10301 ASSERT(connp != NULL); 10302 zoneid = IPCL_ZONEID(connp); 10303 if (option == IP_NEXTHOP) { 10304 ipif = ipif_lookup_onlink_addr(addr, 10305 connp->conn_zoneid, ipst); 10306 } else { 10307 ipif = ipif_lookup_addr(addr, NULL, zoneid, 10308 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 10309 &error, ipst); 10310 } 10311 if (ipif == NULL) { 10312 if (error == EINPROGRESS) 10313 return (error); 10314 else if ((option == IP_MULTICAST_IF) || 10315 (option == IP_NEXTHOP)) 10316 return (EHOSTUNREACH); 10317 else 10318 return (EINVAL); 10319 } else if (checkonly) { 10320 if (option == IP_MULTICAST_IF) { 10321 ill = ipif->ipif_ill; 10322 /* not supported by the virtual network iface */ 10323 if (IS_VNI(ill)) { 10324 ipif_refrele(ipif); 10325 return (EINVAL); 10326 } 10327 } 10328 ipif_refrele(ipif); 10329 return (0); 10330 } 10331 ill = ipif->ipif_ill; 10332 mutex_enter(&connp->conn_lock); 10333 mutex_enter(&ill->ill_lock); 10334 if ((ill->ill_state_flags & ILL_CONDEMNED) || 10335 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 10336 mutex_exit(&ill->ill_lock); 10337 mutex_exit(&connp->conn_lock); 10338 ipif_refrele(ipif); 10339 return (option == IP_MULTICAST_IF ? 10340 EHOSTUNREACH : EINVAL); 10341 } 10342 } else { 10343 mutex_enter(&connp->conn_lock); 10344 } 10345 10346 /* None of the options below are supported on the VNI */ 10347 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 10348 mutex_exit(&ill->ill_lock); 10349 mutex_exit(&connp->conn_lock); 10350 ipif_refrele(ipif); 10351 return (EINVAL); 10352 } 10353 10354 switch (option) { 10355 case IP_DONTFAILOVER_IF: 10356 /* 10357 * This option is used by in.mpathd to ensure 10358 * that IPMP probe packets only go out on the 10359 * test interfaces. in.mpathd sets this option 10360 * on the non-failover interfaces. 10361 * For backward compatibility, this option 10362 * implicitly sets IP_MULTICAST_IF, as used 10363 * be done in bind(), so that ip_wput gets 10364 * this ipif to send mcast packets. 10365 */ 10366 if (ipif != NULL) { 10367 ASSERT(addr != INADDR_ANY); 10368 connp->conn_nofailover_ill = ipif->ipif_ill; 10369 connp->conn_multicast_ipif = ipif; 10370 } else { 10371 ASSERT(addr == INADDR_ANY); 10372 connp->conn_nofailover_ill = NULL; 10373 connp->conn_multicast_ipif = NULL; 10374 } 10375 break; 10376 10377 case IP_MULTICAST_IF: 10378 connp->conn_multicast_ipif = ipif; 10379 break; 10380 case IP_NEXTHOP: 10381 connp->conn_nexthop_v4 = addr; 10382 connp->conn_nexthop_set = B_TRUE; 10383 break; 10384 } 10385 10386 if (ipif != NULL) { 10387 mutex_exit(&ill->ill_lock); 10388 mutex_exit(&connp->conn_lock); 10389 ipif_refrele(ipif); 10390 return (0); 10391 } 10392 mutex_exit(&connp->conn_lock); 10393 /* We succeded in cleared the option */ 10394 return (0); 10395 } 10396 10397 /* 10398 * For options that pass in an ifindex specifying the ill. V6 options always 10399 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 10400 */ 10401 int 10402 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 10403 int level, int option, mblk_t *first_mp) 10404 { 10405 ill_t *ill = NULL; 10406 int error = 0; 10407 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10408 10409 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 10410 if (ifindex != 0) { 10411 ASSERT(connp != NULL); 10412 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 10413 first_mp, ip_restart_optmgmt, &error, ipst); 10414 if (ill != NULL) { 10415 if (checkonly) { 10416 /* not supported by the virtual network iface */ 10417 if (IS_VNI(ill)) { 10418 ill_refrele(ill); 10419 return (EINVAL); 10420 } 10421 ill_refrele(ill); 10422 return (0); 10423 } 10424 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 10425 0, NULL)) { 10426 ill_refrele(ill); 10427 ill = NULL; 10428 mutex_enter(&connp->conn_lock); 10429 goto setit; 10430 } 10431 mutex_enter(&connp->conn_lock); 10432 mutex_enter(&ill->ill_lock); 10433 if (ill->ill_state_flags & ILL_CONDEMNED) { 10434 mutex_exit(&ill->ill_lock); 10435 mutex_exit(&connp->conn_lock); 10436 ill_refrele(ill); 10437 ill = NULL; 10438 mutex_enter(&connp->conn_lock); 10439 } 10440 goto setit; 10441 } else if (error == EINPROGRESS) { 10442 return (error); 10443 } else { 10444 error = 0; 10445 } 10446 } 10447 mutex_enter(&connp->conn_lock); 10448 setit: 10449 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 10450 10451 /* 10452 * The options below assume that the ILL (if any) transmits and/or 10453 * receives traffic. Neither of which is true for the virtual network 10454 * interface, so fail setting these on a VNI. 10455 */ 10456 if (IS_VNI(ill)) { 10457 ASSERT(ill != NULL); 10458 mutex_exit(&ill->ill_lock); 10459 mutex_exit(&connp->conn_lock); 10460 ill_refrele(ill); 10461 return (EINVAL); 10462 } 10463 10464 if (level == IPPROTO_IP) { 10465 switch (option) { 10466 case IP_BOUND_IF: 10467 connp->conn_incoming_ill = ill; 10468 connp->conn_outgoing_ill = ill; 10469 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10470 0 : ifindex; 10471 break; 10472 10473 case IP_MULTICAST_IF: 10474 /* 10475 * This option is an internal special. The socket 10476 * level IP_MULTICAST_IF specifies an 'ipaddr' and 10477 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 10478 * specifies an ifindex and we try first on V6 ill's. 10479 * If we don't find one, we they try using on v4 ill's 10480 * intenally and we come here. 10481 */ 10482 if (!checkonly && ill != NULL) { 10483 ipif_t *ipif; 10484 ipif = ill->ill_ipif; 10485 10486 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 10487 mutex_exit(&ill->ill_lock); 10488 mutex_exit(&connp->conn_lock); 10489 ill_refrele(ill); 10490 ill = NULL; 10491 mutex_enter(&connp->conn_lock); 10492 } else { 10493 connp->conn_multicast_ipif = ipif; 10494 } 10495 } 10496 break; 10497 10498 case IP_DHCPINIT_IF: 10499 if (connp->conn_dhcpinit_ill != NULL) { 10500 /* 10501 * We've locked the conn so conn_cleanup_ill() 10502 * cannot clear conn_dhcpinit_ill -- so it's 10503 * safe to access the ill. 10504 */ 10505 ill_t *oill = connp->conn_dhcpinit_ill; 10506 10507 ASSERT(oill->ill_dhcpinit != 0); 10508 atomic_dec_32(&oill->ill_dhcpinit); 10509 connp->conn_dhcpinit_ill = NULL; 10510 } 10511 10512 if (ill != NULL) { 10513 connp->conn_dhcpinit_ill = ill; 10514 atomic_inc_32(&ill->ill_dhcpinit); 10515 } 10516 break; 10517 } 10518 } else { 10519 switch (option) { 10520 case IPV6_BOUND_IF: 10521 connp->conn_incoming_ill = ill; 10522 connp->conn_outgoing_ill = ill; 10523 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10524 0 : ifindex; 10525 break; 10526 10527 case IPV6_BOUND_PIF: 10528 /* 10529 * Limit all transmit to this ill. 10530 * Unlike IPV6_BOUND_IF, using this option 10531 * prevents load spreading and failover from 10532 * happening when the interface is part of the 10533 * group. That's why we don't need to remember 10534 * the ifindex in orig_bound_ifindex as in 10535 * IPV6_BOUND_IF. 10536 */ 10537 connp->conn_outgoing_pill = ill; 10538 break; 10539 10540 case IPV6_DONTFAILOVER_IF: 10541 /* 10542 * This option is used by in.mpathd to ensure 10543 * that IPMP probe packets only go out on the 10544 * test interfaces. in.mpathd sets this option 10545 * on the non-failover interfaces. 10546 */ 10547 connp->conn_nofailover_ill = ill; 10548 /* 10549 * For backward compatibility, this option 10550 * implicitly sets ip_multicast_ill as used in 10551 * IPV6_MULTICAST_IF so that ip_wput gets 10552 * this ill to send mcast packets. 10553 */ 10554 connp->conn_multicast_ill = ill; 10555 connp->conn_orig_multicast_ifindex = (ill == NULL) ? 10556 0 : ifindex; 10557 break; 10558 10559 case IPV6_MULTICAST_IF: 10560 /* 10561 * Set conn_multicast_ill to be the IPv6 ill. 10562 * Set conn_multicast_ipif to be an IPv4 ipif 10563 * for ifindex to make IPv4 mapped addresses 10564 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 10565 * Even if no IPv6 ill exists for the ifindex 10566 * we need to check for an IPv4 ifindex in order 10567 * for this to work with mapped addresses. In that 10568 * case only set conn_multicast_ipif. 10569 */ 10570 if (!checkonly) { 10571 if (ifindex == 0) { 10572 connp->conn_multicast_ill = NULL; 10573 connp->conn_orig_multicast_ifindex = 0; 10574 connp->conn_multicast_ipif = NULL; 10575 } else if (ill != NULL) { 10576 connp->conn_multicast_ill = ill; 10577 connp->conn_orig_multicast_ifindex = 10578 ifindex; 10579 } 10580 } 10581 break; 10582 } 10583 } 10584 10585 if (ill != NULL) { 10586 mutex_exit(&ill->ill_lock); 10587 mutex_exit(&connp->conn_lock); 10588 ill_refrele(ill); 10589 return (0); 10590 } 10591 mutex_exit(&connp->conn_lock); 10592 /* 10593 * We succeeded in clearing the option (ifindex == 0) or failed to 10594 * locate the ill and could not set the option (ifindex != 0) 10595 */ 10596 return (ifindex == 0 ? 0 : EINVAL); 10597 } 10598 10599 /* This routine sets socket options. */ 10600 /* ARGSUSED */ 10601 int 10602 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10603 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10604 void *dummy, cred_t *cr, mblk_t *first_mp) 10605 { 10606 int *i1 = (int *)invalp; 10607 conn_t *connp = Q_TO_CONN(q); 10608 int error = 0; 10609 boolean_t checkonly; 10610 ire_t *ire; 10611 boolean_t found; 10612 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10613 10614 switch (optset_context) { 10615 10616 case SETFN_OPTCOM_CHECKONLY: 10617 checkonly = B_TRUE; 10618 /* 10619 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10620 * inlen != 0 implies value supplied and 10621 * we have to "pretend" to set it. 10622 * inlen == 0 implies that there is no 10623 * value part in T_CHECK request and just validation 10624 * done elsewhere should be enough, we just return here. 10625 */ 10626 if (inlen == 0) { 10627 *outlenp = 0; 10628 return (0); 10629 } 10630 break; 10631 case SETFN_OPTCOM_NEGOTIATE: 10632 case SETFN_UD_NEGOTIATE: 10633 case SETFN_CONN_NEGOTIATE: 10634 checkonly = B_FALSE; 10635 break; 10636 default: 10637 /* 10638 * We should never get here 10639 */ 10640 *outlenp = 0; 10641 return (EINVAL); 10642 } 10643 10644 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10645 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10646 10647 /* 10648 * For fixed length options, no sanity check 10649 * of passed in length is done. It is assumed *_optcom_req() 10650 * routines do the right thing. 10651 */ 10652 10653 switch (level) { 10654 case SOL_SOCKET: 10655 /* 10656 * conn_lock protects the bitfields, and is used to 10657 * set the fields atomically. 10658 */ 10659 switch (name) { 10660 case SO_BROADCAST: 10661 if (!checkonly) { 10662 /* TODO: use value someplace? */ 10663 mutex_enter(&connp->conn_lock); 10664 connp->conn_broadcast = *i1 ? 1 : 0; 10665 mutex_exit(&connp->conn_lock); 10666 } 10667 break; /* goto sizeof (int) option return */ 10668 case SO_USELOOPBACK: 10669 if (!checkonly) { 10670 /* TODO: use value someplace? */ 10671 mutex_enter(&connp->conn_lock); 10672 connp->conn_loopback = *i1 ? 1 : 0; 10673 mutex_exit(&connp->conn_lock); 10674 } 10675 break; /* goto sizeof (int) option return */ 10676 case SO_DONTROUTE: 10677 if (!checkonly) { 10678 mutex_enter(&connp->conn_lock); 10679 connp->conn_dontroute = *i1 ? 1 : 0; 10680 mutex_exit(&connp->conn_lock); 10681 } 10682 break; /* goto sizeof (int) option return */ 10683 case SO_REUSEADDR: 10684 if (!checkonly) { 10685 mutex_enter(&connp->conn_lock); 10686 connp->conn_reuseaddr = *i1 ? 1 : 0; 10687 mutex_exit(&connp->conn_lock); 10688 } 10689 break; /* goto sizeof (int) option return */ 10690 case SO_PROTOTYPE: 10691 if (!checkonly) { 10692 mutex_enter(&connp->conn_lock); 10693 connp->conn_proto = *i1; 10694 mutex_exit(&connp->conn_lock); 10695 } 10696 break; /* goto sizeof (int) option return */ 10697 case SO_ALLZONES: 10698 if (!checkonly) { 10699 mutex_enter(&connp->conn_lock); 10700 if (IPCL_IS_BOUND(connp)) { 10701 mutex_exit(&connp->conn_lock); 10702 return (EINVAL); 10703 } 10704 connp->conn_allzones = *i1 != 0 ? 1 : 0; 10705 mutex_exit(&connp->conn_lock); 10706 } 10707 break; /* goto sizeof (int) option return */ 10708 case SO_ANON_MLP: 10709 if (!checkonly) { 10710 mutex_enter(&connp->conn_lock); 10711 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; 10712 mutex_exit(&connp->conn_lock); 10713 } 10714 break; /* goto sizeof (int) option return */ 10715 case SO_MAC_EXEMPT: 10716 if (secpolicy_net_mac_aware(cr) != 0 || 10717 IPCL_IS_BOUND(connp)) 10718 return (EACCES); 10719 if (!checkonly) { 10720 mutex_enter(&connp->conn_lock); 10721 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; 10722 mutex_exit(&connp->conn_lock); 10723 } 10724 break; /* goto sizeof (int) option return */ 10725 default: 10726 /* 10727 * "soft" error (negative) 10728 * option not handled at this level 10729 * Note: Do not modify *outlenp 10730 */ 10731 return (-EINVAL); 10732 } 10733 break; 10734 case IPPROTO_IP: 10735 switch (name) { 10736 case IP_NEXTHOP: 10737 if (secpolicy_ip_config(cr, B_FALSE) != 0) 10738 return (EPERM); 10739 /* FALLTHRU */ 10740 case IP_MULTICAST_IF: 10741 case IP_DONTFAILOVER_IF: { 10742 ipaddr_t addr = *i1; 10743 10744 error = ip_opt_set_ipif(connp, addr, checkonly, name, 10745 first_mp); 10746 if (error != 0) 10747 return (error); 10748 break; /* goto sizeof (int) option return */ 10749 } 10750 10751 case IP_MULTICAST_TTL: 10752 /* Recorded in transport above IP */ 10753 *outvalp = *invalp; 10754 *outlenp = sizeof (uchar_t); 10755 return (0); 10756 case IP_MULTICAST_LOOP: 10757 if (!checkonly) { 10758 mutex_enter(&connp->conn_lock); 10759 connp->conn_multicast_loop = *invalp ? 1 : 0; 10760 mutex_exit(&connp->conn_lock); 10761 } 10762 *outvalp = *invalp; 10763 *outlenp = sizeof (uchar_t); 10764 return (0); 10765 case IP_ADD_MEMBERSHIP: 10766 case MCAST_JOIN_GROUP: 10767 case IP_DROP_MEMBERSHIP: 10768 case MCAST_LEAVE_GROUP: { 10769 struct ip_mreq *mreqp; 10770 struct group_req *greqp; 10771 ire_t *ire; 10772 boolean_t done = B_FALSE; 10773 ipaddr_t group, ifaddr; 10774 struct sockaddr_in *sin; 10775 uint32_t *ifindexp; 10776 boolean_t mcast_opt = B_TRUE; 10777 mcast_record_t fmode; 10778 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10779 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10780 10781 switch (name) { 10782 case IP_ADD_MEMBERSHIP: 10783 mcast_opt = B_FALSE; 10784 /* FALLTHRU */ 10785 case MCAST_JOIN_GROUP: 10786 fmode = MODE_IS_EXCLUDE; 10787 optfn = ip_opt_add_group; 10788 break; 10789 10790 case IP_DROP_MEMBERSHIP: 10791 mcast_opt = B_FALSE; 10792 /* FALLTHRU */ 10793 case MCAST_LEAVE_GROUP: 10794 fmode = MODE_IS_INCLUDE; 10795 optfn = ip_opt_delete_group; 10796 break; 10797 } 10798 10799 if (mcast_opt) { 10800 greqp = (struct group_req *)i1; 10801 sin = (struct sockaddr_in *)&greqp->gr_group; 10802 if (sin->sin_family != AF_INET) { 10803 *outlenp = 0; 10804 return (ENOPROTOOPT); 10805 } 10806 group = (ipaddr_t)sin->sin_addr.s_addr; 10807 ifaddr = INADDR_ANY; 10808 ifindexp = &greqp->gr_interface; 10809 } else { 10810 mreqp = (struct ip_mreq *)i1; 10811 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 10812 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 10813 ifindexp = NULL; 10814 } 10815 10816 /* 10817 * In the multirouting case, we need to replicate 10818 * the request on all interfaces that will take part 10819 * in replication. We do so because multirouting is 10820 * reflective, thus we will probably receive multi- 10821 * casts on those interfaces. 10822 * The ip_multirt_apply_membership() succeeds if the 10823 * operation succeeds on at least one interface. 10824 */ 10825 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 10826 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10827 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 10828 if (ire != NULL) { 10829 if (ire->ire_flags & RTF_MULTIRT) { 10830 error = ip_multirt_apply_membership( 10831 optfn, ire, connp, checkonly, group, 10832 fmode, INADDR_ANY, first_mp); 10833 done = B_TRUE; 10834 } 10835 ire_refrele(ire); 10836 } 10837 if (!done) { 10838 error = optfn(connp, checkonly, group, ifaddr, 10839 ifindexp, fmode, INADDR_ANY, first_mp); 10840 } 10841 if (error) { 10842 /* 10843 * EINPROGRESS is a soft error, needs retry 10844 * so don't make *outlenp zero. 10845 */ 10846 if (error != EINPROGRESS) 10847 *outlenp = 0; 10848 return (error); 10849 } 10850 /* OK return - copy input buffer into output buffer */ 10851 if (invalp != outvalp) { 10852 /* don't trust bcopy for identical src/dst */ 10853 bcopy(invalp, outvalp, inlen); 10854 } 10855 *outlenp = inlen; 10856 return (0); 10857 } 10858 case IP_BLOCK_SOURCE: 10859 case IP_UNBLOCK_SOURCE: 10860 case IP_ADD_SOURCE_MEMBERSHIP: 10861 case IP_DROP_SOURCE_MEMBERSHIP: 10862 case MCAST_BLOCK_SOURCE: 10863 case MCAST_UNBLOCK_SOURCE: 10864 case MCAST_JOIN_SOURCE_GROUP: 10865 case MCAST_LEAVE_SOURCE_GROUP: { 10866 struct ip_mreq_source *imreqp; 10867 struct group_source_req *gsreqp; 10868 in_addr_t grp, src, ifaddr = INADDR_ANY; 10869 uint32_t ifindex = 0; 10870 mcast_record_t fmode; 10871 struct sockaddr_in *sin; 10872 ire_t *ire; 10873 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 10874 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10875 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10876 10877 switch (name) { 10878 case IP_BLOCK_SOURCE: 10879 mcast_opt = B_FALSE; 10880 /* FALLTHRU */ 10881 case MCAST_BLOCK_SOURCE: 10882 fmode = MODE_IS_EXCLUDE; 10883 optfn = ip_opt_add_group; 10884 break; 10885 10886 case IP_UNBLOCK_SOURCE: 10887 mcast_opt = B_FALSE; 10888 /* FALLTHRU */ 10889 case MCAST_UNBLOCK_SOURCE: 10890 fmode = MODE_IS_EXCLUDE; 10891 optfn = ip_opt_delete_group; 10892 break; 10893 10894 case IP_ADD_SOURCE_MEMBERSHIP: 10895 mcast_opt = B_FALSE; 10896 /* FALLTHRU */ 10897 case MCAST_JOIN_SOURCE_GROUP: 10898 fmode = MODE_IS_INCLUDE; 10899 optfn = ip_opt_add_group; 10900 break; 10901 10902 case IP_DROP_SOURCE_MEMBERSHIP: 10903 mcast_opt = B_FALSE; 10904 /* FALLTHRU */ 10905 case MCAST_LEAVE_SOURCE_GROUP: 10906 fmode = MODE_IS_INCLUDE; 10907 optfn = ip_opt_delete_group; 10908 break; 10909 } 10910 10911 if (mcast_opt) { 10912 gsreqp = (struct group_source_req *)i1; 10913 if (gsreqp->gsr_group.ss_family != AF_INET) { 10914 *outlenp = 0; 10915 return (ENOPROTOOPT); 10916 } 10917 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 10918 grp = (ipaddr_t)sin->sin_addr.s_addr; 10919 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 10920 src = (ipaddr_t)sin->sin_addr.s_addr; 10921 ifindex = gsreqp->gsr_interface; 10922 } else { 10923 imreqp = (struct ip_mreq_source *)i1; 10924 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 10925 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 10926 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 10927 } 10928 10929 /* 10930 * In the multirouting case, we need to replicate 10931 * the request as noted in the mcast cases above. 10932 */ 10933 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 10934 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10935 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 10936 if (ire != NULL) { 10937 if (ire->ire_flags & RTF_MULTIRT) { 10938 error = ip_multirt_apply_membership( 10939 optfn, ire, connp, checkonly, grp, 10940 fmode, src, first_mp); 10941 done = B_TRUE; 10942 } 10943 ire_refrele(ire); 10944 } 10945 if (!done) { 10946 error = optfn(connp, checkonly, grp, ifaddr, 10947 &ifindex, fmode, src, first_mp); 10948 } 10949 if (error != 0) { 10950 /* 10951 * EINPROGRESS is a soft error, needs retry 10952 * so don't make *outlenp zero. 10953 */ 10954 if (error != EINPROGRESS) 10955 *outlenp = 0; 10956 return (error); 10957 } 10958 /* OK return - copy input buffer into output buffer */ 10959 if (invalp != outvalp) { 10960 bcopy(invalp, outvalp, inlen); 10961 } 10962 *outlenp = inlen; 10963 return (0); 10964 } 10965 case IP_SEC_OPT: 10966 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 10967 if (error != 0) { 10968 *outlenp = 0; 10969 return (error); 10970 } 10971 break; 10972 case IP_HDRINCL: 10973 case IP_OPTIONS: 10974 case T_IP_OPTIONS: 10975 case IP_TOS: 10976 case T_IP_TOS: 10977 case IP_TTL: 10978 case IP_RECVDSTADDR: 10979 case IP_RECVOPTS: 10980 /* OK return - copy input buffer into output buffer */ 10981 if (invalp != outvalp) { 10982 /* don't trust bcopy for identical src/dst */ 10983 bcopy(invalp, outvalp, inlen); 10984 } 10985 *outlenp = inlen; 10986 return (0); 10987 case IP_RECVIF: 10988 /* Retrieve the inbound interface index */ 10989 if (!checkonly) { 10990 mutex_enter(&connp->conn_lock); 10991 connp->conn_recvif = *i1 ? 1 : 0; 10992 mutex_exit(&connp->conn_lock); 10993 } 10994 break; /* goto sizeof (int) option return */ 10995 case IP_RECVPKTINFO: 10996 if (!checkonly) { 10997 mutex_enter(&connp->conn_lock); 10998 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 10999 mutex_exit(&connp->conn_lock); 11000 } 11001 break; /* goto sizeof (int) option return */ 11002 case IP_RECVSLLA: 11003 /* Retrieve the source link layer address */ 11004 if (!checkonly) { 11005 mutex_enter(&connp->conn_lock); 11006 connp->conn_recvslla = *i1 ? 1 : 0; 11007 mutex_exit(&connp->conn_lock); 11008 } 11009 break; /* goto sizeof (int) option return */ 11010 case MRT_INIT: 11011 case MRT_DONE: 11012 case MRT_ADD_VIF: 11013 case MRT_DEL_VIF: 11014 case MRT_ADD_MFC: 11015 case MRT_DEL_MFC: 11016 case MRT_ASSERT: 11017 if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) { 11018 *outlenp = 0; 11019 return (error); 11020 } 11021 error = ip_mrouter_set((int)name, q, checkonly, 11022 (uchar_t *)invalp, inlen, first_mp); 11023 if (error) { 11024 *outlenp = 0; 11025 return (error); 11026 } 11027 /* OK return - copy input buffer into output buffer */ 11028 if (invalp != outvalp) { 11029 /* don't trust bcopy for identical src/dst */ 11030 bcopy(invalp, outvalp, inlen); 11031 } 11032 *outlenp = inlen; 11033 return (0); 11034 case IP_BOUND_IF: 11035 case IP_DHCPINIT_IF: 11036 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11037 level, name, first_mp); 11038 if (error != 0) 11039 return (error); 11040 break; /* goto sizeof (int) option return */ 11041 11042 case IP_UNSPEC_SRC: 11043 /* Allow sending with a zero source address */ 11044 if (!checkonly) { 11045 mutex_enter(&connp->conn_lock); 11046 connp->conn_unspec_src = *i1 ? 1 : 0; 11047 mutex_exit(&connp->conn_lock); 11048 } 11049 break; /* goto sizeof (int) option return */ 11050 default: 11051 /* 11052 * "soft" error (negative) 11053 * option not handled at this level 11054 * Note: Do not modify *outlenp 11055 */ 11056 return (-EINVAL); 11057 } 11058 break; 11059 case IPPROTO_IPV6: 11060 switch (name) { 11061 case IPV6_BOUND_IF: 11062 case IPV6_BOUND_PIF: 11063 case IPV6_DONTFAILOVER_IF: 11064 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11065 level, name, first_mp); 11066 if (error != 0) 11067 return (error); 11068 break; /* goto sizeof (int) option return */ 11069 11070 case IPV6_MULTICAST_IF: 11071 /* 11072 * The only possible errors are EINPROGRESS and 11073 * EINVAL. EINPROGRESS will be restarted and is not 11074 * a hard error. We call this option on both V4 and V6 11075 * If both return EINVAL, then this call returns 11076 * EINVAL. If at least one of them succeeds we 11077 * return success. 11078 */ 11079 found = B_FALSE; 11080 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11081 level, name, first_mp); 11082 if (error == EINPROGRESS) 11083 return (error); 11084 if (error == 0) 11085 found = B_TRUE; 11086 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11087 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 11088 if (error == 0) 11089 found = B_TRUE; 11090 if (!found) 11091 return (error); 11092 break; /* goto sizeof (int) option return */ 11093 11094 case IPV6_MULTICAST_HOPS: 11095 /* Recorded in transport above IP */ 11096 break; /* goto sizeof (int) option return */ 11097 case IPV6_MULTICAST_LOOP: 11098 if (!checkonly) { 11099 mutex_enter(&connp->conn_lock); 11100 connp->conn_multicast_loop = *i1; 11101 mutex_exit(&connp->conn_lock); 11102 } 11103 break; /* goto sizeof (int) option return */ 11104 case IPV6_JOIN_GROUP: 11105 case MCAST_JOIN_GROUP: 11106 case IPV6_LEAVE_GROUP: 11107 case MCAST_LEAVE_GROUP: { 11108 struct ipv6_mreq *ip_mreqp; 11109 struct group_req *greqp; 11110 ire_t *ire; 11111 boolean_t done = B_FALSE; 11112 in6_addr_t groupv6; 11113 uint32_t ifindex; 11114 boolean_t mcast_opt = B_TRUE; 11115 mcast_record_t fmode; 11116 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11117 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11118 11119 switch (name) { 11120 case IPV6_JOIN_GROUP: 11121 mcast_opt = B_FALSE; 11122 /* FALLTHRU */ 11123 case MCAST_JOIN_GROUP: 11124 fmode = MODE_IS_EXCLUDE; 11125 optfn = ip_opt_add_group_v6; 11126 break; 11127 11128 case IPV6_LEAVE_GROUP: 11129 mcast_opt = B_FALSE; 11130 /* FALLTHRU */ 11131 case MCAST_LEAVE_GROUP: 11132 fmode = MODE_IS_INCLUDE; 11133 optfn = ip_opt_delete_group_v6; 11134 break; 11135 } 11136 11137 if (mcast_opt) { 11138 struct sockaddr_in *sin; 11139 struct sockaddr_in6 *sin6; 11140 greqp = (struct group_req *)i1; 11141 if (greqp->gr_group.ss_family == AF_INET) { 11142 sin = (struct sockaddr_in *) 11143 &(greqp->gr_group); 11144 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 11145 &groupv6); 11146 } else { 11147 sin6 = (struct sockaddr_in6 *) 11148 &(greqp->gr_group); 11149 groupv6 = sin6->sin6_addr; 11150 } 11151 ifindex = greqp->gr_interface; 11152 } else { 11153 ip_mreqp = (struct ipv6_mreq *)i1; 11154 groupv6 = ip_mreqp->ipv6mr_multiaddr; 11155 ifindex = ip_mreqp->ipv6mr_interface; 11156 } 11157 /* 11158 * In the multirouting case, we need to replicate 11159 * the request on all interfaces that will take part 11160 * in replication. We do so because multirouting is 11161 * reflective, thus we will probably receive multi- 11162 * casts on those interfaces. 11163 * The ip_multirt_apply_membership_v6() succeeds if 11164 * the operation succeeds on at least one interface. 11165 */ 11166 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 11167 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11168 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11169 if (ire != NULL) { 11170 if (ire->ire_flags & RTF_MULTIRT) { 11171 error = ip_multirt_apply_membership_v6( 11172 optfn, ire, connp, checkonly, 11173 &groupv6, fmode, &ipv6_all_zeros, 11174 first_mp); 11175 done = B_TRUE; 11176 } 11177 ire_refrele(ire); 11178 } 11179 if (!done) { 11180 error = optfn(connp, checkonly, &groupv6, 11181 ifindex, fmode, &ipv6_all_zeros, first_mp); 11182 } 11183 if (error) { 11184 /* 11185 * EINPROGRESS is a soft error, needs retry 11186 * so don't make *outlenp zero. 11187 */ 11188 if (error != EINPROGRESS) 11189 *outlenp = 0; 11190 return (error); 11191 } 11192 /* OK return - copy input buffer into output buffer */ 11193 if (invalp != outvalp) { 11194 /* don't trust bcopy for identical src/dst */ 11195 bcopy(invalp, outvalp, inlen); 11196 } 11197 *outlenp = inlen; 11198 return (0); 11199 } 11200 case MCAST_BLOCK_SOURCE: 11201 case MCAST_UNBLOCK_SOURCE: 11202 case MCAST_JOIN_SOURCE_GROUP: 11203 case MCAST_LEAVE_SOURCE_GROUP: { 11204 struct group_source_req *gsreqp; 11205 in6_addr_t v6grp, v6src; 11206 uint32_t ifindex; 11207 mcast_record_t fmode; 11208 ire_t *ire; 11209 boolean_t done = B_FALSE; 11210 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11211 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11212 11213 switch (name) { 11214 case MCAST_BLOCK_SOURCE: 11215 fmode = MODE_IS_EXCLUDE; 11216 optfn = ip_opt_add_group_v6; 11217 break; 11218 case MCAST_UNBLOCK_SOURCE: 11219 fmode = MODE_IS_EXCLUDE; 11220 optfn = ip_opt_delete_group_v6; 11221 break; 11222 case MCAST_JOIN_SOURCE_GROUP: 11223 fmode = MODE_IS_INCLUDE; 11224 optfn = ip_opt_add_group_v6; 11225 break; 11226 case MCAST_LEAVE_SOURCE_GROUP: 11227 fmode = MODE_IS_INCLUDE; 11228 optfn = ip_opt_delete_group_v6; 11229 break; 11230 } 11231 11232 gsreqp = (struct group_source_req *)i1; 11233 ifindex = gsreqp->gsr_interface; 11234 if (gsreqp->gsr_group.ss_family == AF_INET) { 11235 struct sockaddr_in *s; 11236 s = (struct sockaddr_in *)&gsreqp->gsr_group; 11237 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 11238 s = (struct sockaddr_in *)&gsreqp->gsr_source; 11239 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 11240 } else { 11241 struct sockaddr_in6 *s6; 11242 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 11243 v6grp = s6->sin6_addr; 11244 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 11245 v6src = s6->sin6_addr; 11246 } 11247 11248 /* 11249 * In the multirouting case, we need to replicate 11250 * the request as noted in the mcast cases above. 11251 */ 11252 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 11253 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11254 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11255 if (ire != NULL) { 11256 if (ire->ire_flags & RTF_MULTIRT) { 11257 error = ip_multirt_apply_membership_v6( 11258 optfn, ire, connp, checkonly, 11259 &v6grp, fmode, &v6src, first_mp); 11260 done = B_TRUE; 11261 } 11262 ire_refrele(ire); 11263 } 11264 if (!done) { 11265 error = optfn(connp, checkonly, &v6grp, 11266 ifindex, fmode, &v6src, first_mp); 11267 } 11268 if (error != 0) { 11269 /* 11270 * EINPROGRESS is a soft error, needs retry 11271 * so don't make *outlenp zero. 11272 */ 11273 if (error != EINPROGRESS) 11274 *outlenp = 0; 11275 return (error); 11276 } 11277 /* OK return - copy input buffer into output buffer */ 11278 if (invalp != outvalp) { 11279 bcopy(invalp, outvalp, inlen); 11280 } 11281 *outlenp = inlen; 11282 return (0); 11283 } 11284 case IPV6_UNICAST_HOPS: 11285 /* Recorded in transport above IP */ 11286 break; /* goto sizeof (int) option return */ 11287 case IPV6_UNSPEC_SRC: 11288 /* Allow sending with a zero source address */ 11289 if (!checkonly) { 11290 mutex_enter(&connp->conn_lock); 11291 connp->conn_unspec_src = *i1 ? 1 : 0; 11292 mutex_exit(&connp->conn_lock); 11293 } 11294 break; /* goto sizeof (int) option return */ 11295 case IPV6_RECVPKTINFO: 11296 if (!checkonly) { 11297 mutex_enter(&connp->conn_lock); 11298 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11299 mutex_exit(&connp->conn_lock); 11300 } 11301 break; /* goto sizeof (int) option return */ 11302 case IPV6_RECVTCLASS: 11303 if (!checkonly) { 11304 if (*i1 < 0 || *i1 > 1) { 11305 return (EINVAL); 11306 } 11307 mutex_enter(&connp->conn_lock); 11308 connp->conn_ipv6_recvtclass = *i1; 11309 mutex_exit(&connp->conn_lock); 11310 } 11311 break; 11312 case IPV6_RECVPATHMTU: 11313 if (!checkonly) { 11314 if (*i1 < 0 || *i1 > 1) { 11315 return (EINVAL); 11316 } 11317 mutex_enter(&connp->conn_lock); 11318 connp->conn_ipv6_recvpathmtu = *i1; 11319 mutex_exit(&connp->conn_lock); 11320 } 11321 break; 11322 case IPV6_RECVHOPLIMIT: 11323 if (!checkonly) { 11324 mutex_enter(&connp->conn_lock); 11325 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 11326 mutex_exit(&connp->conn_lock); 11327 } 11328 break; /* goto sizeof (int) option return */ 11329 case IPV6_RECVHOPOPTS: 11330 if (!checkonly) { 11331 mutex_enter(&connp->conn_lock); 11332 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 11333 mutex_exit(&connp->conn_lock); 11334 } 11335 break; /* goto sizeof (int) option return */ 11336 case IPV6_RECVDSTOPTS: 11337 if (!checkonly) { 11338 mutex_enter(&connp->conn_lock); 11339 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 11340 mutex_exit(&connp->conn_lock); 11341 } 11342 break; /* goto sizeof (int) option return */ 11343 case IPV6_RECVRTHDR: 11344 if (!checkonly) { 11345 mutex_enter(&connp->conn_lock); 11346 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 11347 mutex_exit(&connp->conn_lock); 11348 } 11349 break; /* goto sizeof (int) option return */ 11350 case IPV6_RECVRTHDRDSTOPTS: 11351 if (!checkonly) { 11352 mutex_enter(&connp->conn_lock); 11353 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 11354 mutex_exit(&connp->conn_lock); 11355 } 11356 break; /* goto sizeof (int) option return */ 11357 case IPV6_PKTINFO: 11358 if (inlen == 0) 11359 return (-EINVAL); /* clearing option */ 11360 error = ip6_set_pktinfo(cr, connp, 11361 (struct in6_pktinfo *)invalp, first_mp); 11362 if (error != 0) 11363 *outlenp = 0; 11364 else 11365 *outlenp = inlen; 11366 return (error); 11367 case IPV6_NEXTHOP: { 11368 struct sockaddr_in6 *sin6; 11369 11370 /* Verify that the nexthop is reachable */ 11371 if (inlen == 0) 11372 return (-EINVAL); /* clearing option */ 11373 11374 sin6 = (struct sockaddr_in6 *)invalp; 11375 ire = ire_route_lookup_v6(&sin6->sin6_addr, 11376 0, 0, 0, NULL, NULL, connp->conn_zoneid, 11377 NULL, MATCH_IRE_DEFAULT, ipst); 11378 11379 if (ire == NULL) { 11380 *outlenp = 0; 11381 return (EHOSTUNREACH); 11382 } 11383 ire_refrele(ire); 11384 return (-EINVAL); 11385 } 11386 case IPV6_SEC_OPT: 11387 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11388 if (error != 0) { 11389 *outlenp = 0; 11390 return (error); 11391 } 11392 break; 11393 case IPV6_SRC_PREFERENCES: { 11394 /* 11395 * This is implemented strictly in the ip module 11396 * (here and in tcp_opt_*() to accomodate tcp 11397 * sockets). Modules above ip pass this option 11398 * down here since ip is the only one that needs to 11399 * be aware of source address preferences. 11400 * 11401 * This socket option only affects connected 11402 * sockets that haven't already bound to a specific 11403 * IPv6 address. In other words, sockets that 11404 * don't call bind() with an address other than the 11405 * unspecified address and that call connect(). 11406 * ip_bind_connected_v6() passes these preferences 11407 * to the ipif_select_source_v6() function. 11408 */ 11409 if (inlen != sizeof (uint32_t)) 11410 return (EINVAL); 11411 error = ip6_set_src_preferences(connp, 11412 *(uint32_t *)invalp); 11413 if (error != 0) { 11414 *outlenp = 0; 11415 return (error); 11416 } else { 11417 *outlenp = sizeof (uint32_t); 11418 } 11419 break; 11420 } 11421 case IPV6_V6ONLY: 11422 if (*i1 < 0 || *i1 > 1) { 11423 return (EINVAL); 11424 } 11425 mutex_enter(&connp->conn_lock); 11426 connp->conn_ipv6_v6only = *i1; 11427 mutex_exit(&connp->conn_lock); 11428 break; 11429 default: 11430 return (-EINVAL); 11431 } 11432 break; 11433 default: 11434 /* 11435 * "soft" error (negative) 11436 * option not handled at this level 11437 * Note: Do not modify *outlenp 11438 */ 11439 return (-EINVAL); 11440 } 11441 /* 11442 * Common case of return from an option that is sizeof (int) 11443 */ 11444 *(int *)outvalp = *i1; 11445 *outlenp = sizeof (int); 11446 return (0); 11447 } 11448 11449 /* 11450 * This routine gets default values of certain options whose default 11451 * values are maintained by protocol specific code 11452 */ 11453 /* ARGSUSED */ 11454 int 11455 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 11456 { 11457 int *i1 = (int *)ptr; 11458 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11459 11460 switch (level) { 11461 case IPPROTO_IP: 11462 switch (name) { 11463 case IP_MULTICAST_TTL: 11464 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 11465 return (sizeof (uchar_t)); 11466 case IP_MULTICAST_LOOP: 11467 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 11468 return (sizeof (uchar_t)); 11469 default: 11470 return (-1); 11471 } 11472 case IPPROTO_IPV6: 11473 switch (name) { 11474 case IPV6_UNICAST_HOPS: 11475 *i1 = ipst->ips_ipv6_def_hops; 11476 return (sizeof (int)); 11477 case IPV6_MULTICAST_HOPS: 11478 *i1 = IP_DEFAULT_MULTICAST_TTL; 11479 return (sizeof (int)); 11480 case IPV6_MULTICAST_LOOP: 11481 *i1 = IP_DEFAULT_MULTICAST_LOOP; 11482 return (sizeof (int)); 11483 case IPV6_V6ONLY: 11484 *i1 = 1; 11485 return (sizeof (int)); 11486 default: 11487 return (-1); 11488 } 11489 default: 11490 return (-1); 11491 } 11492 /* NOTREACHED */ 11493 } 11494 11495 /* 11496 * Given a destination address and a pointer to where to put the information 11497 * this routine fills in the mtuinfo. 11498 */ 11499 int 11500 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 11501 struct ip6_mtuinfo *mtuinfo, netstack_t *ns) 11502 { 11503 ire_t *ire; 11504 ip_stack_t *ipst = ns->netstack_ip; 11505 11506 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 11507 return (-1); 11508 11509 bzero(mtuinfo, sizeof (*mtuinfo)); 11510 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11511 mtuinfo->ip6m_addr.sin6_port = port; 11512 mtuinfo->ip6m_addr.sin6_addr = *in6; 11513 11514 ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL, ipst); 11515 if (ire != NULL) { 11516 mtuinfo->ip6m_mtu = ire->ire_max_frag; 11517 ire_refrele(ire); 11518 } else { 11519 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 11520 } 11521 return (sizeof (struct ip6_mtuinfo)); 11522 } 11523 11524 /* 11525 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 11526 * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and 11527 * isn't. This doesn't matter as the error checking is done properly for the 11528 * other MRT options coming in through ip_opt_set. 11529 */ 11530 int 11531 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 11532 { 11533 conn_t *connp = Q_TO_CONN(q); 11534 ipsec_req_t *req = (ipsec_req_t *)ptr; 11535 11536 switch (level) { 11537 case IPPROTO_IP: 11538 switch (name) { 11539 case MRT_VERSION: 11540 case MRT_ASSERT: 11541 (void) ip_mrouter_get(name, q, ptr); 11542 return (sizeof (int)); 11543 case IP_SEC_OPT: 11544 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 11545 case IP_NEXTHOP: 11546 if (connp->conn_nexthop_set) { 11547 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 11548 return (sizeof (ipaddr_t)); 11549 } else 11550 return (0); 11551 case IP_RECVPKTINFO: 11552 *(int *)ptr = connp->conn_ip_recvpktinfo ? 1: 0; 11553 return (sizeof (int)); 11554 default: 11555 break; 11556 } 11557 break; 11558 case IPPROTO_IPV6: 11559 switch (name) { 11560 case IPV6_SEC_OPT: 11561 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 11562 case IPV6_SRC_PREFERENCES: { 11563 return (ip6_get_src_preferences(connp, 11564 (uint32_t *)ptr)); 11565 } 11566 case IPV6_V6ONLY: 11567 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 11568 return (sizeof (int)); 11569 case IPV6_PATHMTU: 11570 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 11571 (struct ip6_mtuinfo *)ptr, connp->conn_netstack)); 11572 default: 11573 break; 11574 } 11575 break; 11576 default: 11577 break; 11578 } 11579 return (-1); 11580 } 11581 11582 /* Named Dispatch routine to get a current value out of our parameter table. */ 11583 /* ARGSUSED */ 11584 static int 11585 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11586 { 11587 ipparam_t *ippa = (ipparam_t *)cp; 11588 11589 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 11590 return (0); 11591 } 11592 11593 /* ARGSUSED */ 11594 static int 11595 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11596 { 11597 11598 (void) mi_mpprintf(mp, "%d", *(int *)cp); 11599 return (0); 11600 } 11601 11602 /* 11603 * Set ip{,6}_forwarding values. This means walking through all of the 11604 * ill's and toggling their forwarding values. 11605 */ 11606 /* ARGSUSED */ 11607 static int 11608 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11609 { 11610 long new_value; 11611 int *forwarding_value = (int *)cp; 11612 ill_t *ill; 11613 boolean_t isv6; 11614 ill_walk_context_t ctx; 11615 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11616 11617 isv6 = (forwarding_value == &ipst->ips_ipv6_forward); 11618 11619 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11620 new_value < 0 || new_value > 1) { 11621 return (EINVAL); 11622 } 11623 11624 *forwarding_value = new_value; 11625 11626 /* 11627 * Regardless of the current value of ip_forwarding, set all per-ill 11628 * values of ip_forwarding to the value being set. 11629 * 11630 * Bring all the ill's up to date with the new global value. 11631 */ 11632 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 11633 11634 if (isv6) 11635 ill = ILL_START_WALK_V6(&ctx, ipst); 11636 else 11637 ill = ILL_START_WALK_V4(&ctx, ipst); 11638 11639 for (; ill != NULL; ill = ill_next(&ctx, ill)) 11640 (void) ill_forward_set(ill, new_value != 0); 11641 11642 rw_exit(&ipst->ips_ill_g_lock); 11643 return (0); 11644 } 11645 11646 /* 11647 * Walk through the param array specified registering each element with the 11648 * Named Dispatch handler. This is called only during init. So it is ok 11649 * not to acquire any locks 11650 */ 11651 static boolean_t 11652 ip_param_register(IDP *ndp, ipparam_t *ippa, size_t ippa_cnt, 11653 ipndp_t *ipnd, size_t ipnd_cnt) 11654 { 11655 for (; ippa_cnt-- > 0; ippa++) { 11656 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 11657 if (!nd_load(ndp, ippa->ip_param_name, 11658 ip_param_get, ip_param_set, (caddr_t)ippa)) { 11659 nd_free(ndp); 11660 return (B_FALSE); 11661 } 11662 } 11663 } 11664 11665 for (; ipnd_cnt-- > 0; ipnd++) { 11666 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 11667 if (!nd_load(ndp, ipnd->ip_ndp_name, 11668 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 11669 ipnd->ip_ndp_data)) { 11670 nd_free(ndp); 11671 return (B_FALSE); 11672 } 11673 } 11674 } 11675 11676 return (B_TRUE); 11677 } 11678 11679 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 11680 /* ARGSUSED */ 11681 static int 11682 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11683 { 11684 long new_value; 11685 ipparam_t *ippa = (ipparam_t *)cp; 11686 11687 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11688 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 11689 return (EINVAL); 11690 } 11691 ippa->ip_param_value = new_value; 11692 return (0); 11693 } 11694 11695 /* 11696 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 11697 * When an ipf is passed here for the first time, if 11698 * we already have in-order fragments on the queue, we convert from the fast- 11699 * path reassembly scheme to the hard-case scheme. From then on, additional 11700 * fragments are reassembled here. We keep track of the start and end offsets 11701 * of each piece, and the number of holes in the chain. When the hole count 11702 * goes to zero, we are done! 11703 * 11704 * The ipf_count will be updated to account for any mblk(s) added (pointed to 11705 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 11706 * ipfb_count and ill_frag_count by the difference of ipf_count before and 11707 * after the call to ip_reassemble(). 11708 */ 11709 int 11710 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 11711 size_t msg_len) 11712 { 11713 uint_t end; 11714 mblk_t *next_mp; 11715 mblk_t *mp1; 11716 uint_t offset; 11717 boolean_t incr_dups = B_TRUE; 11718 boolean_t offset_zero_seen = B_FALSE; 11719 boolean_t pkt_boundary_checked = B_FALSE; 11720 11721 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 11722 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 11723 11724 /* Add in byte count */ 11725 ipf->ipf_count += msg_len; 11726 if (ipf->ipf_end) { 11727 /* 11728 * We were part way through in-order reassembly, but now there 11729 * is a hole. We walk through messages already queued, and 11730 * mark them for hard case reassembly. We know that up till 11731 * now they were in order starting from offset zero. 11732 */ 11733 offset = 0; 11734 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 11735 IP_REASS_SET_START(mp1, offset); 11736 if (offset == 0) { 11737 ASSERT(ipf->ipf_nf_hdr_len != 0); 11738 offset = -ipf->ipf_nf_hdr_len; 11739 } 11740 offset += mp1->b_wptr - mp1->b_rptr; 11741 IP_REASS_SET_END(mp1, offset); 11742 } 11743 /* One hole at the end. */ 11744 ipf->ipf_hole_cnt = 1; 11745 /* Brand it as a hard case, forever. */ 11746 ipf->ipf_end = 0; 11747 } 11748 /* Walk through all the new pieces. */ 11749 do { 11750 end = start + (mp->b_wptr - mp->b_rptr); 11751 /* 11752 * If start is 0, decrease 'end' only for the first mblk of 11753 * the fragment. Otherwise 'end' can get wrong value in the 11754 * second pass of the loop if first mblk is exactly the 11755 * size of ipf_nf_hdr_len. 11756 */ 11757 if (start == 0 && !offset_zero_seen) { 11758 /* First segment */ 11759 ASSERT(ipf->ipf_nf_hdr_len != 0); 11760 end -= ipf->ipf_nf_hdr_len; 11761 offset_zero_seen = B_TRUE; 11762 } 11763 next_mp = mp->b_cont; 11764 /* 11765 * We are checking to see if there is any interesing data 11766 * to process. If there isn't and the mblk isn't the 11767 * one which carries the unfragmentable header then we 11768 * drop it. It's possible to have just the unfragmentable 11769 * header come through without any data. That needs to be 11770 * saved. 11771 * 11772 * If the assert at the top of this function holds then the 11773 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 11774 * is infrequently traveled enough that the test is left in 11775 * to protect against future code changes which break that 11776 * invariant. 11777 */ 11778 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 11779 /* Empty. Blast it. */ 11780 IP_REASS_SET_START(mp, 0); 11781 IP_REASS_SET_END(mp, 0); 11782 /* 11783 * If the ipf points to the mblk we are about to free, 11784 * update ipf to point to the next mblk (or NULL 11785 * if none). 11786 */ 11787 if (ipf->ipf_mp->b_cont == mp) 11788 ipf->ipf_mp->b_cont = next_mp; 11789 freeb(mp); 11790 continue; 11791 } 11792 mp->b_cont = NULL; 11793 IP_REASS_SET_START(mp, start); 11794 IP_REASS_SET_END(mp, end); 11795 if (!ipf->ipf_tail_mp) { 11796 ipf->ipf_tail_mp = mp; 11797 ipf->ipf_mp->b_cont = mp; 11798 if (start == 0 || !more) { 11799 ipf->ipf_hole_cnt = 1; 11800 /* 11801 * if the first fragment comes in more than one 11802 * mblk, this loop will be executed for each 11803 * mblk. Need to adjust hole count so exiting 11804 * this routine will leave hole count at 1. 11805 */ 11806 if (next_mp) 11807 ipf->ipf_hole_cnt++; 11808 } else 11809 ipf->ipf_hole_cnt = 2; 11810 continue; 11811 } else if (ipf->ipf_last_frag_seen && !more && 11812 !pkt_boundary_checked) { 11813 /* 11814 * We check datagram boundary only if this fragment 11815 * claims to be the last fragment and we have seen a 11816 * last fragment in the past too. We do this only 11817 * once for a given fragment. 11818 * 11819 * start cannot be 0 here as fragments with start=0 11820 * and MF=0 gets handled as a complete packet. These 11821 * fragments should not reach here. 11822 */ 11823 11824 if (start + msgdsize(mp) != 11825 IP_REASS_END(ipf->ipf_tail_mp)) { 11826 /* 11827 * We have two fragments both of which claim 11828 * to be the last fragment but gives conflicting 11829 * information about the whole datagram size. 11830 * Something fishy is going on. Drop the 11831 * fragment and free up the reassembly list. 11832 */ 11833 return (IP_REASS_FAILED); 11834 } 11835 11836 /* 11837 * We shouldn't come to this code block again for this 11838 * particular fragment. 11839 */ 11840 pkt_boundary_checked = B_TRUE; 11841 } 11842 11843 /* New stuff at or beyond tail? */ 11844 offset = IP_REASS_END(ipf->ipf_tail_mp); 11845 if (start >= offset) { 11846 if (ipf->ipf_last_frag_seen) { 11847 /* current fragment is beyond last fragment */ 11848 return (IP_REASS_FAILED); 11849 } 11850 /* Link it on end. */ 11851 ipf->ipf_tail_mp->b_cont = mp; 11852 ipf->ipf_tail_mp = mp; 11853 if (more) { 11854 if (start != offset) 11855 ipf->ipf_hole_cnt++; 11856 } else if (start == offset && next_mp == NULL) 11857 ipf->ipf_hole_cnt--; 11858 continue; 11859 } 11860 mp1 = ipf->ipf_mp->b_cont; 11861 offset = IP_REASS_START(mp1); 11862 /* New stuff at the front? */ 11863 if (start < offset) { 11864 if (start == 0) { 11865 if (end >= offset) { 11866 /* Nailed the hole at the begining. */ 11867 ipf->ipf_hole_cnt--; 11868 } 11869 } else if (end < offset) { 11870 /* 11871 * A hole, stuff, and a hole where there used 11872 * to be just a hole. 11873 */ 11874 ipf->ipf_hole_cnt++; 11875 } 11876 mp->b_cont = mp1; 11877 /* Check for overlap. */ 11878 while (end > offset) { 11879 if (end < IP_REASS_END(mp1)) { 11880 mp->b_wptr -= end - offset; 11881 IP_REASS_SET_END(mp, offset); 11882 BUMP_MIB(ill->ill_ip_mib, 11883 ipIfStatsReasmPartDups); 11884 break; 11885 } 11886 /* Did we cover another hole? */ 11887 if ((mp1->b_cont && 11888 IP_REASS_END(mp1) != 11889 IP_REASS_START(mp1->b_cont) && 11890 end >= IP_REASS_START(mp1->b_cont)) || 11891 (!ipf->ipf_last_frag_seen && !more)) { 11892 ipf->ipf_hole_cnt--; 11893 } 11894 /* Clip out mp1. */ 11895 if ((mp->b_cont = mp1->b_cont) == NULL) { 11896 /* 11897 * After clipping out mp1, this guy 11898 * is now hanging off the end. 11899 */ 11900 ipf->ipf_tail_mp = mp; 11901 } 11902 IP_REASS_SET_START(mp1, 0); 11903 IP_REASS_SET_END(mp1, 0); 11904 /* Subtract byte count */ 11905 ipf->ipf_count -= mp1->b_datap->db_lim - 11906 mp1->b_datap->db_base; 11907 freeb(mp1); 11908 BUMP_MIB(ill->ill_ip_mib, 11909 ipIfStatsReasmPartDups); 11910 mp1 = mp->b_cont; 11911 if (!mp1) 11912 break; 11913 offset = IP_REASS_START(mp1); 11914 } 11915 ipf->ipf_mp->b_cont = mp; 11916 continue; 11917 } 11918 /* 11919 * The new piece starts somewhere between the start of the head 11920 * and before the end of the tail. 11921 */ 11922 for (; mp1; mp1 = mp1->b_cont) { 11923 offset = IP_REASS_END(mp1); 11924 if (start < offset) { 11925 if (end <= offset) { 11926 /* Nothing new. */ 11927 IP_REASS_SET_START(mp, 0); 11928 IP_REASS_SET_END(mp, 0); 11929 /* Subtract byte count */ 11930 ipf->ipf_count -= mp->b_datap->db_lim - 11931 mp->b_datap->db_base; 11932 if (incr_dups) { 11933 ipf->ipf_num_dups++; 11934 incr_dups = B_FALSE; 11935 } 11936 freeb(mp); 11937 BUMP_MIB(ill->ill_ip_mib, 11938 ipIfStatsReasmDuplicates); 11939 break; 11940 } 11941 /* 11942 * Trim redundant stuff off beginning of new 11943 * piece. 11944 */ 11945 IP_REASS_SET_START(mp, offset); 11946 mp->b_rptr += offset - start; 11947 BUMP_MIB(ill->ill_ip_mib, 11948 ipIfStatsReasmPartDups); 11949 start = offset; 11950 if (!mp1->b_cont) { 11951 /* 11952 * After trimming, this guy is now 11953 * hanging off the end. 11954 */ 11955 mp1->b_cont = mp; 11956 ipf->ipf_tail_mp = mp; 11957 if (!more) { 11958 ipf->ipf_hole_cnt--; 11959 } 11960 break; 11961 } 11962 } 11963 if (start >= IP_REASS_START(mp1->b_cont)) 11964 continue; 11965 /* Fill a hole */ 11966 if (start > offset) 11967 ipf->ipf_hole_cnt++; 11968 mp->b_cont = mp1->b_cont; 11969 mp1->b_cont = mp; 11970 mp1 = mp->b_cont; 11971 offset = IP_REASS_START(mp1); 11972 if (end >= offset) { 11973 ipf->ipf_hole_cnt--; 11974 /* Check for overlap. */ 11975 while (end > offset) { 11976 if (end < IP_REASS_END(mp1)) { 11977 mp->b_wptr -= end - offset; 11978 IP_REASS_SET_END(mp, offset); 11979 /* 11980 * TODO we might bump 11981 * this up twice if there is 11982 * overlap at both ends. 11983 */ 11984 BUMP_MIB(ill->ill_ip_mib, 11985 ipIfStatsReasmPartDups); 11986 break; 11987 } 11988 /* Did we cover another hole? */ 11989 if ((mp1->b_cont && 11990 IP_REASS_END(mp1) 11991 != IP_REASS_START(mp1->b_cont) && 11992 end >= 11993 IP_REASS_START(mp1->b_cont)) || 11994 (!ipf->ipf_last_frag_seen && 11995 !more)) { 11996 ipf->ipf_hole_cnt--; 11997 } 11998 /* Clip out mp1. */ 11999 if ((mp->b_cont = mp1->b_cont) == 12000 NULL) { 12001 /* 12002 * After clipping out mp1, 12003 * this guy is now hanging 12004 * off the end. 12005 */ 12006 ipf->ipf_tail_mp = mp; 12007 } 12008 IP_REASS_SET_START(mp1, 0); 12009 IP_REASS_SET_END(mp1, 0); 12010 /* Subtract byte count */ 12011 ipf->ipf_count -= 12012 mp1->b_datap->db_lim - 12013 mp1->b_datap->db_base; 12014 freeb(mp1); 12015 BUMP_MIB(ill->ill_ip_mib, 12016 ipIfStatsReasmPartDups); 12017 mp1 = mp->b_cont; 12018 if (!mp1) 12019 break; 12020 offset = IP_REASS_START(mp1); 12021 } 12022 } 12023 break; 12024 } 12025 } while (start = end, mp = next_mp); 12026 12027 /* Fragment just processed could be the last one. Remember this fact */ 12028 if (!more) 12029 ipf->ipf_last_frag_seen = B_TRUE; 12030 12031 /* Still got holes? */ 12032 if (ipf->ipf_hole_cnt) 12033 return (IP_REASS_PARTIAL); 12034 /* Clean up overloaded fields to avoid upstream disasters. */ 12035 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12036 IP_REASS_SET_START(mp1, 0); 12037 IP_REASS_SET_END(mp1, 0); 12038 } 12039 return (IP_REASS_COMPLETE); 12040 } 12041 12042 /* 12043 * ipsec processing for the fast path, used for input UDP Packets 12044 * Returns true if ready for passup to UDP. 12045 * Return false if packet is not passable to UDP (e.g. it failed IPsec policy, 12046 * was an ESP-in-UDP packet, etc.). 12047 */ 12048 static boolean_t 12049 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 12050 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present, ire_t *ire) 12051 { 12052 uint32_t ill_index; 12053 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 12054 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 12055 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 12056 udp_t *udp = connp->conn_udp; 12057 12058 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12059 /* The ill_index of the incoming ILL */ 12060 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 12061 12062 /* pass packet up to the transport */ 12063 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 12064 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 12065 NULL, mctl_present); 12066 if (*first_mpp == NULL) { 12067 return (B_FALSE); 12068 } 12069 } 12070 12071 /* Initiate IPPF processing for fastpath UDP */ 12072 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 12073 ip_process(IPP_LOCAL_IN, mpp, ill_index); 12074 if (*mpp == NULL) { 12075 ip2dbg(("ip_input_ipsec_process: UDP pkt " 12076 "deferred/dropped during IPPF processing\n")); 12077 return (B_FALSE); 12078 } 12079 } 12080 /* 12081 * Remove 0-spi if it's 0, or move everything behind 12082 * the UDP header over it and forward to ESP via 12083 * ip_proto_input(). 12084 */ 12085 if (udp->udp_nat_t_endpoint) { 12086 if (mctl_present) { 12087 /* mctl_present *shouldn't* happen. */ 12088 ip_drop_packet(*first_mpp, B_TRUE, NULL, 12089 NULL, DROPPER(ipss, ipds_esp_nat_t_ipsec), 12090 &ipss->ipsec_dropper); 12091 *first_mpp = NULL; 12092 return (B_FALSE); 12093 } 12094 12095 /* "ill" is "recv_ill" in actuality. */ 12096 if (!zero_spi_check(q, *mpp, ire, ill, ipss)) 12097 return (B_FALSE); 12098 12099 /* Else continue like a normal UDP packet. */ 12100 } 12101 12102 /* 12103 * We make the checks as below since we are in the fast path 12104 * and want to minimize the number of checks if the IP_RECVIF and/or 12105 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 12106 */ 12107 if (connp->conn_recvif || connp->conn_recvslla || 12108 connp->conn_ip_recvpktinfo) { 12109 if (connp->conn_recvif) { 12110 in_flags = IPF_RECVIF; 12111 } 12112 /* 12113 * UDP supports IP_RECVPKTINFO option for both v4 and v6 12114 * so the flag passed to ip_add_info is based on IP version 12115 * of connp. 12116 */ 12117 if (connp->conn_ip_recvpktinfo) { 12118 if (connp->conn_af_isv6) { 12119 /* 12120 * V6 only needs index 12121 */ 12122 in_flags |= IPF_RECVIF; 12123 } else { 12124 /* 12125 * V4 needs index + matching address. 12126 */ 12127 in_flags |= IPF_RECVADDR; 12128 } 12129 } 12130 if (connp->conn_recvslla) { 12131 in_flags |= IPF_RECVSLLA; 12132 } 12133 /* 12134 * since in_flags are being set ill will be 12135 * referenced in ip_add_info, so it better not 12136 * be NULL. 12137 */ 12138 /* 12139 * the actual data will be contained in b_cont 12140 * upon successful return of the following call. 12141 * If the call fails then the original mblk is 12142 * returned. 12143 */ 12144 *mpp = ip_add_info(*mpp, ill, in_flags, IPCL_ZONEID(connp), 12145 ipst); 12146 } 12147 12148 return (B_TRUE); 12149 } 12150 12151 /* 12152 * Fragmentation reassembly. Each ILL has a hash table for 12153 * queuing packets undergoing reassembly for all IPIFs 12154 * associated with the ILL. The hash is based on the packet 12155 * IP ident field. The ILL frag hash table was allocated 12156 * as a timer block at the time the ILL was created. Whenever 12157 * there is anything on the reassembly queue, the timer will 12158 * be running. Returns B_TRUE if successful else B_FALSE; 12159 * frees mp on failure. 12160 */ 12161 static boolean_t 12162 ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, 12163 uint32_t *cksum_val, uint16_t *cksum_flags) 12164 { 12165 uint32_t frag_offset_flags; 12166 ill_t *ill = (ill_t *)q->q_ptr; 12167 mblk_t *mp = *mpp; 12168 mblk_t *t_mp; 12169 ipaddr_t dst; 12170 uint8_t proto = ipha->ipha_protocol; 12171 uint32_t sum_val; 12172 uint16_t sum_flags; 12173 ipf_t *ipf; 12174 ipf_t **ipfp; 12175 ipfb_t *ipfb; 12176 uint16_t ident; 12177 uint32_t offset; 12178 ipaddr_t src; 12179 uint_t hdr_length; 12180 uint32_t end; 12181 mblk_t *mp1; 12182 mblk_t *tail_mp; 12183 size_t count; 12184 size_t msg_len; 12185 uint8_t ecn_info = 0; 12186 uint32_t packet_size; 12187 boolean_t pruned = B_FALSE; 12188 ip_stack_t *ipst = ill->ill_ipst; 12189 12190 if (cksum_val != NULL) 12191 *cksum_val = 0; 12192 if (cksum_flags != NULL) 12193 *cksum_flags = 0; 12194 12195 /* 12196 * Drop the fragmented as early as possible, if 12197 * we don't have resource(s) to re-assemble. 12198 */ 12199 if (ipst->ips_ip_reass_queue_bytes == 0) { 12200 freemsg(mp); 12201 return (B_FALSE); 12202 } 12203 12204 /* Check for fragmentation offset; return if there's none */ 12205 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 12206 (IPH_MF | IPH_OFFSET)) == 0) 12207 return (B_TRUE); 12208 12209 /* 12210 * We utilize hardware computed checksum info only for UDP since 12211 * IP fragmentation is a normal occurence for the protocol. In 12212 * addition, checksum offload support for IP fragments carrying 12213 * UDP payload is commonly implemented across network adapters. 12214 */ 12215 ASSERT(ill != NULL); 12216 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 12217 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 12218 mblk_t *mp1 = mp->b_cont; 12219 int32_t len; 12220 12221 /* Record checksum information from the packet */ 12222 sum_val = (uint32_t)DB_CKSUM16(mp); 12223 sum_flags = DB_CKSUMFLAGS(mp); 12224 12225 /* IP payload offset from beginning of mblk */ 12226 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 12227 12228 if ((sum_flags & HCK_PARTIALCKSUM) && 12229 (mp1 == NULL || mp1->b_cont == NULL) && 12230 offset >= DB_CKSUMSTART(mp) && 12231 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 12232 uint32_t adj; 12233 /* 12234 * Partial checksum has been calculated by hardware 12235 * and attached to the packet; in addition, any 12236 * prepended extraneous data is even byte aligned. 12237 * If any such data exists, we adjust the checksum; 12238 * this would also handle any postpended data. 12239 */ 12240 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 12241 mp, mp1, len, adj); 12242 12243 /* One's complement subtract extraneous checksum */ 12244 if (adj >= sum_val) 12245 sum_val = ~(adj - sum_val) & 0xFFFF; 12246 else 12247 sum_val -= adj; 12248 } 12249 } else { 12250 sum_val = 0; 12251 sum_flags = 0; 12252 } 12253 12254 /* Clear hardware checksumming flag */ 12255 DB_CKSUMFLAGS(mp) = 0; 12256 12257 ident = ipha->ipha_ident; 12258 offset = (frag_offset_flags << 3) & 0xFFFF; 12259 src = ipha->ipha_src; 12260 dst = ipha->ipha_dst; 12261 hdr_length = IPH_HDR_LENGTH(ipha); 12262 end = ntohs(ipha->ipha_length) - hdr_length; 12263 12264 /* If end == 0 then we have a packet with no data, so just free it */ 12265 if (end == 0) { 12266 freemsg(mp); 12267 return (B_FALSE); 12268 } 12269 12270 /* Record the ECN field info. */ 12271 ecn_info = (ipha->ipha_type_of_service & 0x3); 12272 if (offset != 0) { 12273 /* 12274 * If this isn't the first piece, strip the header, and 12275 * add the offset to the end value. 12276 */ 12277 mp->b_rptr += hdr_length; 12278 end += offset; 12279 } 12280 12281 msg_len = MBLKSIZE(mp); 12282 tail_mp = mp; 12283 while (tail_mp->b_cont != NULL) { 12284 tail_mp = tail_mp->b_cont; 12285 msg_len += MBLKSIZE(tail_mp); 12286 } 12287 12288 /* If the reassembly list for this ILL will get too big, prune it */ 12289 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 12290 ipst->ips_ip_reass_queue_bytes) { 12291 ill_frag_prune(ill, 12292 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 : 12293 (ipst->ips_ip_reass_queue_bytes - msg_len)); 12294 pruned = B_TRUE; 12295 } 12296 12297 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 12298 mutex_enter(&ipfb->ipfb_lock); 12299 12300 ipfp = &ipfb->ipfb_ipf; 12301 /* Try to find an existing fragment queue for this packet. */ 12302 for (;;) { 12303 ipf = ipfp[0]; 12304 if (ipf != NULL) { 12305 /* 12306 * It has to match on ident and src/dst address. 12307 */ 12308 if (ipf->ipf_ident == ident && 12309 ipf->ipf_src == src && 12310 ipf->ipf_dst == dst && 12311 ipf->ipf_protocol == proto) { 12312 /* 12313 * If we have received too many 12314 * duplicate fragments for this packet 12315 * free it. 12316 */ 12317 if (ipf->ipf_num_dups > ip_max_frag_dups) { 12318 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12319 freemsg(mp); 12320 mutex_exit(&ipfb->ipfb_lock); 12321 return (B_FALSE); 12322 } 12323 /* Found it. */ 12324 break; 12325 } 12326 ipfp = &ipf->ipf_hash_next; 12327 continue; 12328 } 12329 12330 /* 12331 * If we pruned the list, do we want to store this new 12332 * fragment?. We apply an optimization here based on the 12333 * fact that most fragments will be received in order. 12334 * So if the offset of this incoming fragment is zero, 12335 * it is the first fragment of a new packet. We will 12336 * keep it. Otherwise drop the fragment, as we have 12337 * probably pruned the packet already (since the 12338 * packet cannot be found). 12339 */ 12340 if (pruned && offset != 0) { 12341 mutex_exit(&ipfb->ipfb_lock); 12342 freemsg(mp); 12343 return (B_FALSE); 12344 } 12345 12346 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) { 12347 /* 12348 * Too many fragmented packets in this hash 12349 * bucket. Free the oldest. 12350 */ 12351 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 12352 } 12353 12354 /* New guy. Allocate a frag message. */ 12355 mp1 = allocb(sizeof (*ipf), BPRI_MED); 12356 if (mp1 == NULL) { 12357 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12358 freemsg(mp); 12359 reass_done: 12360 mutex_exit(&ipfb->ipfb_lock); 12361 return (B_FALSE); 12362 } 12363 12364 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); 12365 mp1->b_cont = mp; 12366 12367 /* Initialize the fragment header. */ 12368 ipf = (ipf_t *)mp1->b_rptr; 12369 ipf->ipf_mp = mp1; 12370 ipf->ipf_ptphn = ipfp; 12371 ipfp[0] = ipf; 12372 ipf->ipf_hash_next = NULL; 12373 ipf->ipf_ident = ident; 12374 ipf->ipf_protocol = proto; 12375 ipf->ipf_src = src; 12376 ipf->ipf_dst = dst; 12377 ipf->ipf_nf_hdr_len = 0; 12378 /* Record reassembly start time. */ 12379 ipf->ipf_timestamp = gethrestime_sec(); 12380 /* Record ipf generation and account for frag header */ 12381 ipf->ipf_gen = ill->ill_ipf_gen++; 12382 ipf->ipf_count = MBLKSIZE(mp1); 12383 ipf->ipf_last_frag_seen = B_FALSE; 12384 ipf->ipf_ecn = ecn_info; 12385 ipf->ipf_num_dups = 0; 12386 ipfb->ipfb_frag_pkts++; 12387 ipf->ipf_checksum = 0; 12388 ipf->ipf_checksum_flags = 0; 12389 12390 /* Store checksum value in fragment header */ 12391 if (sum_flags != 0) { 12392 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12393 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12394 ipf->ipf_checksum = sum_val; 12395 ipf->ipf_checksum_flags = sum_flags; 12396 } 12397 12398 /* 12399 * We handle reassembly two ways. In the easy case, 12400 * where all the fragments show up in order, we do 12401 * minimal bookkeeping, and just clip new pieces on 12402 * the end. If we ever see a hole, then we go off 12403 * to ip_reassemble which has to mark the pieces and 12404 * keep track of the number of holes, etc. Obviously, 12405 * the point of having both mechanisms is so we can 12406 * handle the easy case as efficiently as possible. 12407 */ 12408 if (offset == 0) { 12409 /* Easy case, in-order reassembly so far. */ 12410 ipf->ipf_count += msg_len; 12411 ipf->ipf_tail_mp = tail_mp; 12412 /* 12413 * Keep track of next expected offset in 12414 * ipf_end. 12415 */ 12416 ipf->ipf_end = end; 12417 ipf->ipf_nf_hdr_len = hdr_length; 12418 } else { 12419 /* Hard case, hole at the beginning. */ 12420 ipf->ipf_tail_mp = NULL; 12421 /* 12422 * ipf_end == 0 means that we have given up 12423 * on easy reassembly. 12424 */ 12425 ipf->ipf_end = 0; 12426 12427 /* Forget checksum offload from now on */ 12428 ipf->ipf_checksum_flags = 0; 12429 12430 /* 12431 * ipf_hole_cnt is set by ip_reassemble. 12432 * ipf_count is updated by ip_reassemble. 12433 * No need to check for return value here 12434 * as we don't expect reassembly to complete 12435 * or fail for the first fragment itself. 12436 */ 12437 (void) ip_reassemble(mp, ipf, 12438 (frag_offset_flags & IPH_OFFSET) << 3, 12439 (frag_offset_flags & IPH_MF), ill, msg_len); 12440 } 12441 /* Update per ipfb and ill byte counts */ 12442 ipfb->ipfb_count += ipf->ipf_count; 12443 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12444 ill->ill_frag_count += ipf->ipf_count; 12445 /* If the frag timer wasn't already going, start it. */ 12446 mutex_enter(&ill->ill_lock); 12447 ill_frag_timer_start(ill); 12448 mutex_exit(&ill->ill_lock); 12449 goto reass_done; 12450 } 12451 12452 /* 12453 * If the packet's flag has changed (it could be coming up 12454 * from an interface different than the previous, therefore 12455 * possibly different checksum capability), then forget about 12456 * any stored checksum states. Otherwise add the value to 12457 * the existing one stored in the fragment header. 12458 */ 12459 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 12460 sum_val += ipf->ipf_checksum; 12461 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12462 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12463 ipf->ipf_checksum = sum_val; 12464 } else if (ipf->ipf_checksum_flags != 0) { 12465 /* Forget checksum offload from now on */ 12466 ipf->ipf_checksum_flags = 0; 12467 } 12468 12469 /* 12470 * We have a new piece of a datagram which is already being 12471 * reassembled. Update the ECN info if all IP fragments 12472 * are ECN capable. If there is one which is not, clear 12473 * all the info. If there is at least one which has CE 12474 * code point, IP needs to report that up to transport. 12475 */ 12476 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 12477 if (ecn_info == IPH_ECN_CE) 12478 ipf->ipf_ecn = IPH_ECN_CE; 12479 } else { 12480 ipf->ipf_ecn = IPH_ECN_NECT; 12481 } 12482 if (offset && ipf->ipf_end == offset) { 12483 /* The new fragment fits at the end */ 12484 ipf->ipf_tail_mp->b_cont = mp; 12485 /* Update the byte count */ 12486 ipf->ipf_count += msg_len; 12487 /* Update per ipfb and ill byte counts */ 12488 ipfb->ipfb_count += msg_len; 12489 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12490 ill->ill_frag_count += msg_len; 12491 if (frag_offset_flags & IPH_MF) { 12492 /* More to come. */ 12493 ipf->ipf_end = end; 12494 ipf->ipf_tail_mp = tail_mp; 12495 goto reass_done; 12496 } 12497 } else { 12498 /* Go do the hard cases. */ 12499 int ret; 12500 12501 if (offset == 0) 12502 ipf->ipf_nf_hdr_len = hdr_length; 12503 12504 /* Save current byte count */ 12505 count = ipf->ipf_count; 12506 ret = ip_reassemble(mp, ipf, 12507 (frag_offset_flags & IPH_OFFSET) << 3, 12508 (frag_offset_flags & IPH_MF), ill, msg_len); 12509 /* Count of bytes added and subtracted (freeb()ed) */ 12510 count = ipf->ipf_count - count; 12511 if (count) { 12512 /* Update per ipfb and ill byte counts */ 12513 ipfb->ipfb_count += count; 12514 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12515 ill->ill_frag_count += count; 12516 } 12517 if (ret == IP_REASS_PARTIAL) { 12518 goto reass_done; 12519 } else if (ret == IP_REASS_FAILED) { 12520 /* Reassembly failed. Free up all resources */ 12521 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12522 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 12523 IP_REASS_SET_START(t_mp, 0); 12524 IP_REASS_SET_END(t_mp, 0); 12525 } 12526 freemsg(mp); 12527 goto reass_done; 12528 } 12529 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 12530 } 12531 /* 12532 * We have completed reassembly. Unhook the frag header from 12533 * the reassembly list. 12534 * 12535 * Before we free the frag header, record the ECN info 12536 * to report back to the transport. 12537 */ 12538 ecn_info = ipf->ipf_ecn; 12539 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); 12540 ipfp = ipf->ipf_ptphn; 12541 12542 /* We need to supply these to caller */ 12543 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 12544 sum_val = ipf->ipf_checksum; 12545 else 12546 sum_val = 0; 12547 12548 mp1 = ipf->ipf_mp; 12549 count = ipf->ipf_count; 12550 ipf = ipf->ipf_hash_next; 12551 if (ipf != NULL) 12552 ipf->ipf_ptphn = ipfp; 12553 ipfp[0] = ipf; 12554 ill->ill_frag_count -= count; 12555 ASSERT(ipfb->ipfb_count >= count); 12556 ipfb->ipfb_count -= count; 12557 ipfb->ipfb_frag_pkts--; 12558 mutex_exit(&ipfb->ipfb_lock); 12559 /* Ditch the frag header. */ 12560 mp = mp1->b_cont; 12561 12562 freeb(mp1); 12563 12564 /* Restore original IP length in header. */ 12565 packet_size = (uint32_t)msgdsize(mp); 12566 if (packet_size > IP_MAXPACKET) { 12567 freemsg(mp); 12568 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 12569 return (B_FALSE); 12570 } 12571 12572 if (DB_REF(mp) > 1) { 12573 mblk_t *mp2 = copymsg(mp); 12574 12575 freemsg(mp); 12576 if (mp2 == NULL) { 12577 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12578 return (B_FALSE); 12579 } 12580 mp = mp2; 12581 } 12582 ipha = (ipha_t *)mp->b_rptr; 12583 12584 ipha->ipha_length = htons((uint16_t)packet_size); 12585 /* We're now complete, zip the frag state */ 12586 ipha->ipha_fragment_offset_and_flags = 0; 12587 /* Record the ECN info. */ 12588 ipha->ipha_type_of_service &= 0xFC; 12589 ipha->ipha_type_of_service |= ecn_info; 12590 *mpp = mp; 12591 12592 /* Reassembly is successful; return checksum information if needed */ 12593 if (cksum_val != NULL) 12594 *cksum_val = sum_val; 12595 if (cksum_flags != NULL) 12596 *cksum_flags = sum_flags; 12597 12598 return (B_TRUE); 12599 } 12600 12601 /* 12602 * Perform ip header check sum update local options. 12603 * return B_TRUE if all is well, else return B_FALSE and release 12604 * the mp. caller is responsible for decrementing ire ref cnt. 12605 */ 12606 static boolean_t 12607 ip_options_cksum(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12608 ip_stack_t *ipst) 12609 { 12610 mblk_t *first_mp; 12611 boolean_t mctl_present; 12612 uint16_t sum; 12613 12614 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12615 /* 12616 * Don't do the checksum if it has gone through AH/ESP 12617 * processing. 12618 */ 12619 if (!mctl_present) { 12620 sum = ip_csum_hdr(ipha); 12621 if (sum != 0) { 12622 if (ill != NULL) { 12623 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12624 } else { 12625 BUMP_MIB(&ipst->ips_ip_mib, 12626 ipIfStatsInCksumErrs); 12627 } 12628 freemsg(first_mp); 12629 return (B_FALSE); 12630 } 12631 } 12632 12633 if (!ip_rput_local_options(q, mp, ipha, ire, ipst)) { 12634 if (mctl_present) 12635 freeb(first_mp); 12636 return (B_FALSE); 12637 } 12638 12639 return (B_TRUE); 12640 } 12641 12642 /* 12643 * All udp packet are delivered to the local host via this routine. 12644 */ 12645 void 12646 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12647 ill_t *recv_ill) 12648 { 12649 uint32_t sum; 12650 uint32_t u1; 12651 boolean_t mctl_present; 12652 conn_t *connp; 12653 mblk_t *first_mp; 12654 uint16_t *up; 12655 ill_t *ill = (ill_t *)q->q_ptr; 12656 uint16_t reass_hck_flags = 0; 12657 ip_stack_t *ipst; 12658 12659 ASSERT(recv_ill != NULL); 12660 ipst = recv_ill->ill_ipst; 12661 12662 #define rptr ((uchar_t *)ipha) 12663 12664 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12665 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 12666 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12667 ASSERT(ill != NULL); 12668 12669 /* 12670 * FAST PATH for udp packets 12671 */ 12672 12673 /* u1 is # words of IP options */ 12674 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 12675 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12676 12677 /* IP options present */ 12678 if (u1 != 0) 12679 goto ipoptions; 12680 12681 /* Check the IP header checksum. */ 12682 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12683 /* Clear the IP header h/w cksum flag */ 12684 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12685 } else if (!mctl_present) { 12686 /* 12687 * Don't verify header checksum if this packet is coming 12688 * back from AH/ESP as we already did it. 12689 */ 12690 #define uph ((uint16_t *)ipha) 12691 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 12692 uph[6] + uph[7] + uph[8] + uph[9]; 12693 #undef uph 12694 /* finish doing IP checksum */ 12695 sum = (sum & 0xFFFF) + (sum >> 16); 12696 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12697 if (sum != 0 && sum != 0xFFFF) { 12698 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12699 freemsg(first_mp); 12700 return; 12701 } 12702 } 12703 12704 /* 12705 * Count for SNMP of inbound packets for ire. 12706 * if mctl is present this might be a secure packet and 12707 * has already been counted for in ip_proto_input(). 12708 */ 12709 if (!mctl_present) { 12710 UPDATE_IB_PKT_COUNT(ire); 12711 ire->ire_last_used_time = lbolt; 12712 } 12713 12714 /* packet part of fragmented IP packet? */ 12715 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12716 if (u1 & (IPH_MF | IPH_OFFSET)) { 12717 goto fragmented; 12718 } 12719 12720 /* u1 = IP header length (20 bytes) */ 12721 u1 = IP_SIMPLE_HDR_LENGTH; 12722 12723 /* packet does not contain complete IP & UDP headers */ 12724 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 12725 goto udppullup; 12726 12727 /* up points to UDP header */ 12728 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 12729 #define iphs ((uint16_t *)ipha) 12730 12731 /* if udp hdr cksum != 0, then need to checksum udp packet */ 12732 if (up[3] != 0) { 12733 mblk_t *mp1 = mp->b_cont; 12734 boolean_t cksum_err; 12735 uint16_t hck_flags = 0; 12736 12737 /* Pseudo-header checksum */ 12738 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12739 iphs[9] + up[2]; 12740 12741 /* 12742 * Revert to software checksum calculation if the interface 12743 * isn't capable of checksum offload or if IPsec is present. 12744 */ 12745 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 12746 hck_flags = DB_CKSUMFLAGS(mp); 12747 12748 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12749 IP_STAT(ipst, ip_in_sw_cksum); 12750 12751 IP_CKSUM_RECV(hck_flags, u1, 12752 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 12753 (int32_t)((uchar_t *)up - rptr), 12754 mp, mp1, cksum_err); 12755 12756 if (cksum_err) { 12757 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12758 if (hck_flags & HCK_FULLCKSUM) 12759 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 12760 else if (hck_flags & HCK_PARTIALCKSUM) 12761 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 12762 else 12763 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 12764 12765 freemsg(first_mp); 12766 return; 12767 } 12768 } 12769 12770 /* Non-fragmented broadcast or multicast packet? */ 12771 if (ire->ire_type == IRE_BROADCAST) 12772 goto udpslowpath; 12773 12774 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 12775 ire->ire_zoneid, ipst)) != NULL) { 12776 ASSERT(connp->conn_upq != NULL); 12777 IP_STAT(ipst, ip_udp_fast_path); 12778 12779 if (CONN_UDP_FLOWCTLD(connp)) { 12780 freemsg(mp); 12781 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 12782 } else { 12783 if (!mctl_present) { 12784 BUMP_MIB(ill->ill_ip_mib, 12785 ipIfStatsHCInDelivers); 12786 } 12787 /* 12788 * mp and first_mp can change. 12789 */ 12790 if (ip_udp_check(q, connp, recv_ill, 12791 ipha, &mp, &first_mp, mctl_present, ire)) { 12792 /* Send it upstream */ 12793 (connp->conn_recv)(connp, mp, NULL); 12794 } 12795 } 12796 /* 12797 * freeb() cannot deal with null mblk being passed 12798 * in and first_mp can be set to null in the call 12799 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 12800 */ 12801 if (mctl_present && first_mp != NULL) { 12802 freeb(first_mp); 12803 } 12804 CONN_DEC_REF(connp); 12805 return; 12806 } 12807 12808 /* 12809 * if we got here we know the packet is not fragmented and 12810 * has no options. The classifier could not find a conn_t and 12811 * most likely its an icmp packet so send it through slow path. 12812 */ 12813 12814 goto udpslowpath; 12815 12816 ipoptions: 12817 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 12818 goto slow_done; 12819 } 12820 12821 UPDATE_IB_PKT_COUNT(ire); 12822 ire->ire_last_used_time = lbolt; 12823 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12824 if (u1 & (IPH_MF | IPH_OFFSET)) { 12825 fragmented: 12826 /* 12827 * "sum" and "reass_hck_flags" are non-zero if the 12828 * reassembled packet has a valid hardware computed 12829 * checksum information associated with it. 12830 */ 12831 if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) 12832 goto slow_done; 12833 /* 12834 * Make sure that first_mp points back to mp as 12835 * the mp we came in with could have changed in 12836 * ip_rput_fragment(). 12837 */ 12838 ASSERT(!mctl_present); 12839 ipha = (ipha_t *)mp->b_rptr; 12840 first_mp = mp; 12841 } 12842 12843 /* Now we have a complete datagram, destined for this machine. */ 12844 u1 = IPH_HDR_LENGTH(ipha); 12845 /* Pull up the UDP header, if necessary. */ 12846 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 12847 udppullup: 12848 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 12849 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12850 freemsg(first_mp); 12851 goto slow_done; 12852 } 12853 ipha = (ipha_t *)mp->b_rptr; 12854 } 12855 12856 /* 12857 * Validate the checksum for the reassembled packet; for the 12858 * pullup case we calculate the payload checksum in software. 12859 */ 12860 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 12861 if (up[3] != 0) { 12862 boolean_t cksum_err; 12863 12864 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12865 IP_STAT(ipst, ip_in_sw_cksum); 12866 12867 IP_CKSUM_RECV_REASS(reass_hck_flags, 12868 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 12869 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12870 iphs[9] + up[2], sum, cksum_err); 12871 12872 if (cksum_err) { 12873 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12874 12875 if (reass_hck_flags & HCK_FULLCKSUM) 12876 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 12877 else if (reass_hck_flags & HCK_PARTIALCKSUM) 12878 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 12879 else 12880 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 12881 12882 freemsg(first_mp); 12883 goto slow_done; 12884 } 12885 } 12886 udpslowpath: 12887 12888 /* Clear hardware checksum flag to be safe */ 12889 DB_CKSUMFLAGS(mp) = 0; 12890 12891 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 12892 (ire->ire_type == IRE_BROADCAST), 12893 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IPINFO, 12894 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 12895 12896 slow_done: 12897 IP_STAT(ipst, ip_udp_slow_path); 12898 return; 12899 12900 #undef iphs 12901 #undef rptr 12902 } 12903 12904 /* ARGSUSED */ 12905 static mblk_t * 12906 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 12907 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 12908 ill_rx_ring_t *ill_ring) 12909 { 12910 conn_t *connp; 12911 uint32_t sum; 12912 uint32_t u1; 12913 uint16_t *up; 12914 int offset; 12915 ssize_t len; 12916 mblk_t *mp1; 12917 boolean_t syn_present = B_FALSE; 12918 tcph_t *tcph; 12919 uint_t ip_hdr_len; 12920 ill_t *ill = (ill_t *)q->q_ptr; 12921 zoneid_t zoneid = ire->ire_zoneid; 12922 boolean_t cksum_err; 12923 uint16_t hck_flags = 0; 12924 ip_stack_t *ipst = recv_ill->ill_ipst; 12925 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 12926 12927 #define rptr ((uchar_t *)ipha) 12928 12929 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 12930 ASSERT(ill != NULL); 12931 12932 /* 12933 * FAST PATH for tcp packets 12934 */ 12935 12936 /* u1 is # words of IP options */ 12937 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 12938 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12939 12940 /* IP options present */ 12941 if (u1) { 12942 goto ipoptions; 12943 } else if (!mctl_present) { 12944 /* Check the IP header checksum. */ 12945 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12946 /* Clear the IP header h/w cksum flag */ 12947 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12948 } else if (!mctl_present) { 12949 /* 12950 * Don't verify header checksum if this packet 12951 * is coming back from AH/ESP as we already did it. 12952 */ 12953 #define uph ((uint16_t *)ipha) 12954 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 12955 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 12956 #undef uph 12957 /* finish doing IP checksum */ 12958 sum = (sum & 0xFFFF) + (sum >> 16); 12959 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12960 if (sum != 0 && sum != 0xFFFF) { 12961 BUMP_MIB(ill->ill_ip_mib, 12962 ipIfStatsInCksumErrs); 12963 goto error; 12964 } 12965 } 12966 } 12967 12968 if (!mctl_present) { 12969 UPDATE_IB_PKT_COUNT(ire); 12970 ire->ire_last_used_time = lbolt; 12971 } 12972 12973 /* packet part of fragmented IP packet? */ 12974 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12975 if (u1 & (IPH_MF | IPH_OFFSET)) { 12976 goto fragmented; 12977 } 12978 12979 /* u1 = IP header length (20 bytes) */ 12980 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 12981 12982 /* does packet contain IP+TCP headers? */ 12983 len = mp->b_wptr - rptr; 12984 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 12985 IP_STAT(ipst, ip_tcppullup); 12986 goto tcppullup; 12987 } 12988 12989 /* TCP options present? */ 12990 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 12991 12992 /* 12993 * If options need to be pulled up, then goto tcpoptions. 12994 * otherwise we are still in the fast path 12995 */ 12996 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 12997 IP_STAT(ipst, ip_tcpoptions); 12998 goto tcpoptions; 12999 } 13000 13001 /* multiple mblks of tcp data? */ 13002 if ((mp1 = mp->b_cont) != NULL) { 13003 /* more then two? */ 13004 if (mp1->b_cont != NULL) { 13005 IP_STAT(ipst, ip_multipkttcp); 13006 goto multipkttcp; 13007 } 13008 len += mp1->b_wptr - mp1->b_rptr; 13009 } 13010 13011 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 13012 13013 /* part of pseudo checksum */ 13014 13015 /* TCP datagram length */ 13016 u1 = len - IP_SIMPLE_HDR_LENGTH; 13017 13018 #define iphs ((uint16_t *)ipha) 13019 13020 #ifdef _BIG_ENDIAN 13021 u1 += IPPROTO_TCP; 13022 #else 13023 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13024 #endif 13025 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13026 13027 /* 13028 * Revert to software checksum calculation if the interface 13029 * isn't capable of checksum offload or if IPsec is present. 13030 */ 13031 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 13032 hck_flags = DB_CKSUMFLAGS(mp); 13033 13034 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13035 IP_STAT(ipst, ip_in_sw_cksum); 13036 13037 IP_CKSUM_RECV(hck_flags, u1, 13038 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13039 (int32_t)((uchar_t *)up - rptr), 13040 mp, mp1, cksum_err); 13041 13042 if (cksum_err) { 13043 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13044 13045 if (hck_flags & HCK_FULLCKSUM) 13046 IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err); 13047 else if (hck_flags & HCK_PARTIALCKSUM) 13048 IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err); 13049 else 13050 IP_STAT(ipst, ip_tcp_in_sw_cksum_err); 13051 13052 goto error; 13053 } 13054 13055 try_again: 13056 13057 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 13058 zoneid, ipst)) == NULL) { 13059 /* Send the TH_RST */ 13060 goto no_conn; 13061 } 13062 13063 /* 13064 * TCP FAST PATH for AF_INET socket. 13065 * 13066 * TCP fast path to avoid extra work. An AF_INET socket type 13067 * does not have facility to receive extra information via 13068 * ip_process or ip_add_info. Also, when the connection was 13069 * established, we made a check if this connection is impacted 13070 * by any global IPsec policy or per connection policy (a 13071 * policy that comes in effect later will not apply to this 13072 * connection). Since all this can be determined at the 13073 * connection establishment time, a quick check of flags 13074 * can avoid extra work. 13075 */ 13076 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 13077 !IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13078 ASSERT(first_mp == mp); 13079 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13080 SET_SQUEUE(mp, tcp_rput_data, connp); 13081 return (mp); 13082 } 13083 13084 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 13085 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 13086 if (IPCL_IS_TCP(connp)) { 13087 mp->b_datap->db_struioflag |= STRUIO_EAGER; 13088 DB_CKSUMSTART(mp) = 13089 (intptr_t)ip_squeue_get(ill_ring); 13090 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 13091 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13092 BUMP_MIB(ill->ill_ip_mib, 13093 ipIfStatsHCInDelivers); 13094 SET_SQUEUE(mp, connp->conn_recv, connp); 13095 return (mp); 13096 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 13097 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13098 BUMP_MIB(ill->ill_ip_mib, 13099 ipIfStatsHCInDelivers); 13100 ip_squeue_enter_unbound++; 13101 SET_SQUEUE(mp, tcp_conn_request_unbound, 13102 connp); 13103 return (mp); 13104 } 13105 syn_present = B_TRUE; 13106 } 13107 13108 } 13109 13110 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 13111 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13112 13113 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13114 /* No need to send this packet to TCP */ 13115 if ((flags & TH_RST) || (flags & TH_URG)) { 13116 CONN_DEC_REF(connp); 13117 freemsg(first_mp); 13118 return (NULL); 13119 } 13120 if (flags & TH_ACK) { 13121 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 13122 ipst->ips_netstack->netstack_tcp, connp); 13123 CONN_DEC_REF(connp); 13124 return (NULL); 13125 } 13126 13127 CONN_DEC_REF(connp); 13128 freemsg(first_mp); 13129 return (NULL); 13130 } 13131 13132 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 13133 first_mp = ipsec_check_inbound_policy(first_mp, connp, 13134 ipha, NULL, mctl_present); 13135 if (first_mp == NULL) { 13136 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13137 CONN_DEC_REF(connp); 13138 return (NULL); 13139 } 13140 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 13141 ASSERT(syn_present); 13142 if (mctl_present) { 13143 ASSERT(first_mp != mp); 13144 first_mp->b_datap->db_struioflag |= 13145 STRUIO_POLICY; 13146 } else { 13147 ASSERT(first_mp == mp); 13148 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 13149 mp->b_datap->db_struioflag |= STRUIO_POLICY; 13150 } 13151 } else { 13152 /* 13153 * Discard first_mp early since we're dealing with a 13154 * fully-connected conn_t and tcp doesn't do policy in 13155 * this case. 13156 */ 13157 if (mctl_present) { 13158 freeb(first_mp); 13159 mctl_present = B_FALSE; 13160 } 13161 first_mp = mp; 13162 } 13163 } 13164 13165 /* Initiate IPPF processing for fastpath */ 13166 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13167 uint32_t ill_index; 13168 13169 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13170 ip_process(IPP_LOCAL_IN, &mp, ill_index); 13171 if (mp == NULL) { 13172 ip2dbg(("ip_input_ipsec_process: TCP pkt " 13173 "deferred/dropped during IPPF processing\n")); 13174 CONN_DEC_REF(connp); 13175 if (mctl_present) 13176 freeb(first_mp); 13177 return (NULL); 13178 } else if (mctl_present) { 13179 /* 13180 * ip_process might return a new mp. 13181 */ 13182 ASSERT(first_mp != mp); 13183 first_mp->b_cont = mp; 13184 } else { 13185 first_mp = mp; 13186 } 13187 13188 } 13189 13190 if (!syn_present && connp->conn_ip_recvpktinfo) { 13191 /* 13192 * TCP does not support IP_RECVPKTINFO for v4 so lets 13193 * make sure IPF_RECVIF is passed to ip_add_info. 13194 */ 13195 mp = ip_add_info(mp, recv_ill, flags|IPF_RECVIF, 13196 IPCL_ZONEID(connp), ipst); 13197 if (mp == NULL) { 13198 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13199 CONN_DEC_REF(connp); 13200 if (mctl_present) 13201 freeb(first_mp); 13202 return (NULL); 13203 } else if (mctl_present) { 13204 /* 13205 * ip_add_info might return a new mp. 13206 */ 13207 ASSERT(first_mp != mp); 13208 first_mp->b_cont = mp; 13209 } else { 13210 first_mp = mp; 13211 } 13212 } 13213 13214 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13215 if (IPCL_IS_TCP(connp)) { 13216 SET_SQUEUE(first_mp, connp->conn_recv, connp); 13217 return (first_mp); 13218 } else { 13219 /* SOCK_RAW, IPPROTO_TCP case */ 13220 (connp->conn_recv)(connp, first_mp, NULL); 13221 CONN_DEC_REF(connp); 13222 return (NULL); 13223 } 13224 13225 no_conn: 13226 /* Initiate IPPf processing, if needed. */ 13227 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13228 uint32_t ill_index; 13229 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13230 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 13231 if (first_mp == NULL) { 13232 return (NULL); 13233 } 13234 } 13235 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13236 13237 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid, 13238 ipst->ips_netstack->netstack_tcp, NULL); 13239 return (NULL); 13240 ipoptions: 13241 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) { 13242 goto slow_done; 13243 } 13244 13245 UPDATE_IB_PKT_COUNT(ire); 13246 ire->ire_last_used_time = lbolt; 13247 13248 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13249 if (u1 & (IPH_MF | IPH_OFFSET)) { 13250 fragmented: 13251 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 13252 if (mctl_present) 13253 freeb(first_mp); 13254 goto slow_done; 13255 } 13256 /* 13257 * Make sure that first_mp points back to mp as 13258 * the mp we came in with could have changed in 13259 * ip_rput_fragment(). 13260 */ 13261 ASSERT(!mctl_present); 13262 ipha = (ipha_t *)mp->b_rptr; 13263 first_mp = mp; 13264 } 13265 13266 /* Now we have a complete datagram, destined for this machine. */ 13267 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 13268 13269 len = mp->b_wptr - mp->b_rptr; 13270 /* Pull up a minimal TCP header, if necessary. */ 13271 if (len < (u1 + 20)) { 13272 tcppullup: 13273 if (!pullupmsg(mp, u1 + 20)) { 13274 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13275 goto error; 13276 } 13277 ipha = (ipha_t *)mp->b_rptr; 13278 len = mp->b_wptr - mp->b_rptr; 13279 } 13280 13281 /* 13282 * Extract the offset field from the TCP header. As usual, we 13283 * try to help the compiler more than the reader. 13284 */ 13285 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 13286 if (offset != 5) { 13287 tcpoptions: 13288 if (offset < 5) { 13289 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13290 goto error; 13291 } 13292 /* 13293 * There must be TCP options. 13294 * Make sure we can grab them. 13295 */ 13296 offset <<= 2; 13297 offset += u1; 13298 if (len < offset) { 13299 if (!pullupmsg(mp, offset)) { 13300 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13301 goto error; 13302 } 13303 ipha = (ipha_t *)mp->b_rptr; 13304 len = mp->b_wptr - rptr; 13305 } 13306 } 13307 13308 /* Get the total packet length in len, including headers. */ 13309 if (mp->b_cont) { 13310 multipkttcp: 13311 len = msgdsize(mp); 13312 } 13313 13314 /* 13315 * Check the TCP checksum by pulling together the pseudo- 13316 * header checksum, and passing it to ip_csum to be added in 13317 * with the TCP datagram. 13318 * 13319 * Since we are not using the hwcksum if available we must 13320 * clear the flag. We may come here via tcppullup or tcpoptions. 13321 * If either of these fails along the way the mblk is freed. 13322 * If this logic ever changes and mblk is reused to say send 13323 * ICMP's back, then this flag may need to be cleared in 13324 * other places as well. 13325 */ 13326 DB_CKSUMFLAGS(mp) = 0; 13327 13328 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 13329 13330 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 13331 #ifdef _BIG_ENDIAN 13332 u1 += IPPROTO_TCP; 13333 #else 13334 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13335 #endif 13336 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13337 /* 13338 * Not M_DATA mblk or its a dup, so do the checksum now. 13339 */ 13340 IP_STAT(ipst, ip_in_sw_cksum); 13341 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 13342 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13343 goto error; 13344 } 13345 13346 IP_STAT(ipst, ip_tcp_slow_path); 13347 goto try_again; 13348 #undef iphs 13349 #undef rptr 13350 13351 error: 13352 freemsg(first_mp); 13353 slow_done: 13354 return (NULL); 13355 } 13356 13357 /* ARGSUSED */ 13358 static void 13359 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13360 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 13361 { 13362 conn_t *connp; 13363 uint32_t sum; 13364 uint32_t u1; 13365 ssize_t len; 13366 sctp_hdr_t *sctph; 13367 zoneid_t zoneid = ire->ire_zoneid; 13368 uint32_t pktsum; 13369 uint32_t calcsum; 13370 uint32_t ports; 13371 in6_addr_t map_src, map_dst; 13372 ill_t *ill = (ill_t *)q->q_ptr; 13373 ip_stack_t *ipst; 13374 sctp_stack_t *sctps; 13375 boolean_t sctp_csum_err = B_FALSE; 13376 13377 ASSERT(recv_ill != NULL); 13378 ipst = recv_ill->ill_ipst; 13379 sctps = ipst->ips_netstack->netstack_sctp; 13380 13381 #define rptr ((uchar_t *)ipha) 13382 13383 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 13384 ASSERT(ill != NULL); 13385 13386 /* u1 is # words of IP options */ 13387 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13388 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13389 13390 /* IP options present */ 13391 if (u1 > 0) { 13392 goto ipoptions; 13393 } else { 13394 /* Check the IP header checksum. */ 13395 if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, ill) && 13396 !mctl_present) { 13397 #define uph ((uint16_t *)ipha) 13398 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13399 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13400 #undef uph 13401 /* finish doing IP checksum */ 13402 sum = (sum & 0xFFFF) + (sum >> 16); 13403 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13404 /* 13405 * Don't verify header checksum if this packet 13406 * is coming back from AH/ESP as we already did it. 13407 */ 13408 if (sum != 0 && sum != 0xFFFF) { 13409 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 13410 goto error; 13411 } 13412 } 13413 /* 13414 * Since there is no SCTP h/w cksum support yet, just 13415 * clear the flag. 13416 */ 13417 DB_CKSUMFLAGS(mp) = 0; 13418 } 13419 13420 /* 13421 * Don't verify header checksum if this packet is coming 13422 * back from AH/ESP as we already did it. 13423 */ 13424 if (!mctl_present) { 13425 UPDATE_IB_PKT_COUNT(ire); 13426 ire->ire_last_used_time = lbolt; 13427 } 13428 13429 /* packet part of fragmented IP packet? */ 13430 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13431 if (u1 & (IPH_MF | IPH_OFFSET)) 13432 goto fragmented; 13433 13434 /* u1 = IP header length (20 bytes) */ 13435 u1 = IP_SIMPLE_HDR_LENGTH; 13436 13437 find_sctp_client: 13438 /* Pullup if we don't have the sctp common header. */ 13439 len = MBLKL(mp); 13440 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 13441 if (mp->b_cont == NULL || 13442 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 13443 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13444 goto error; 13445 } 13446 ipha = (ipha_t *)mp->b_rptr; 13447 len = MBLKL(mp); 13448 } 13449 13450 sctph = (sctp_hdr_t *)(rptr + u1); 13451 #ifdef DEBUG 13452 if (!skip_sctp_cksum) { 13453 #endif 13454 pktsum = sctph->sh_chksum; 13455 sctph->sh_chksum = 0; 13456 calcsum = sctp_cksum(mp, u1); 13457 sctph->sh_chksum = pktsum; 13458 if (calcsum != pktsum) 13459 sctp_csum_err = B_TRUE; 13460 #ifdef DEBUG /* skip_sctp_cksum */ 13461 } 13462 #endif 13463 /* get the ports */ 13464 ports = *(uint32_t *)&sctph->sh_sport; 13465 13466 IRE_REFRELE(ire); 13467 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 13468 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 13469 if (sctp_csum_err) { 13470 /* 13471 * No potential sctp checksum errors go to the Sun 13472 * sctp stack however they might be Adler-32 summed 13473 * packets a userland stack bound to a raw IP socket 13474 * could reasonably use. Note though that Adler-32 is 13475 * a long deprecated algorithm and customer sctp 13476 * networks should eventually migrate to CRC-32 at 13477 * which time this facility should be removed. 13478 */ 13479 flags |= IP_FF_SCTP_CSUM_ERR; 13480 goto no_conn; 13481 } 13482 if ((connp = sctp_fanout(&map_src, &map_dst, ports, zoneid, mp, 13483 sctps)) == NULL) { 13484 /* Check for raw socket or OOTB handling */ 13485 goto no_conn; 13486 } 13487 13488 /* Found a client; up it goes */ 13489 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13490 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 13491 return; 13492 13493 no_conn: 13494 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 13495 ports, mctl_present, flags, B_TRUE, zoneid); 13496 return; 13497 13498 ipoptions: 13499 DB_CKSUMFLAGS(mp) = 0; 13500 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) 13501 goto slow_done; 13502 13503 UPDATE_IB_PKT_COUNT(ire); 13504 ire->ire_last_used_time = lbolt; 13505 13506 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13507 if (u1 & (IPH_MF | IPH_OFFSET)) { 13508 fragmented: 13509 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) 13510 goto slow_done; 13511 /* 13512 * Make sure that first_mp points back to mp as 13513 * the mp we came in with could have changed in 13514 * ip_rput_fragment(). 13515 */ 13516 ASSERT(!mctl_present); 13517 ipha = (ipha_t *)mp->b_rptr; 13518 first_mp = mp; 13519 } 13520 13521 /* Now we have a complete datagram, destined for this machine. */ 13522 u1 = IPH_HDR_LENGTH(ipha); 13523 goto find_sctp_client; 13524 #undef iphs 13525 #undef rptr 13526 13527 error: 13528 freemsg(first_mp); 13529 slow_done: 13530 IRE_REFRELE(ire); 13531 } 13532 13533 #define VER_BITS 0xF0 13534 #define VERSION_6 0x60 13535 13536 static boolean_t 13537 ip_rput_multimblk_ipoptions(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t **iphapp, 13538 ipaddr_t *dstp, ip_stack_t *ipst) 13539 { 13540 uint_t opt_len; 13541 ipha_t *ipha; 13542 ssize_t len; 13543 uint_t pkt_len; 13544 13545 ASSERT(ill != NULL); 13546 IP_STAT(ipst, ip_ipoptions); 13547 ipha = *iphapp; 13548 13549 #define rptr ((uchar_t *)ipha) 13550 /* Assume no IPv6 packets arrive over the IPv4 queue */ 13551 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 13552 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); 13553 freemsg(mp); 13554 return (B_FALSE); 13555 } 13556 13557 /* multiple mblk or too short */ 13558 pkt_len = ntohs(ipha->ipha_length); 13559 13560 /* Get the number of words of IP options in the IP header. */ 13561 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 13562 if (opt_len) { 13563 /* IP Options present! Validate and process. */ 13564 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 13565 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13566 goto done; 13567 } 13568 /* 13569 * Recompute complete header length and make sure we 13570 * have access to all of it. 13571 */ 13572 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 13573 if (len > (mp->b_wptr - rptr)) { 13574 if (len > pkt_len) { 13575 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13576 goto done; 13577 } 13578 if (!pullupmsg(mp, len)) { 13579 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13580 goto done; 13581 } 13582 ipha = (ipha_t *)mp->b_rptr; 13583 } 13584 /* 13585 * Go off to ip_rput_options which returns the next hop 13586 * destination address, which may have been affected 13587 * by source routing. 13588 */ 13589 IP_STAT(ipst, ip_opt); 13590 if (ip_rput_options(q, mp, ipha, dstp, ipst) == -1) { 13591 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13592 return (B_FALSE); 13593 } 13594 } 13595 *iphapp = ipha; 13596 return (B_TRUE); 13597 done: 13598 /* clear b_prev - used by ip_mroute_decap */ 13599 mp->b_prev = NULL; 13600 freemsg(mp); 13601 return (B_FALSE); 13602 #undef rptr 13603 } 13604 13605 /* 13606 * Deal with the fact that there is no ire for the destination. 13607 */ 13608 static ire_t * 13609 ip_rput_noire(queue_t *q, mblk_t *mp, int ll_multicast, ipaddr_t dst) 13610 { 13611 ipha_t *ipha; 13612 ill_t *ill; 13613 ire_t *ire; 13614 ip_stack_t *ipst; 13615 enum ire_forward_action ret_action; 13616 13617 ipha = (ipha_t *)mp->b_rptr; 13618 ill = (ill_t *)q->q_ptr; 13619 13620 ASSERT(ill != NULL); 13621 ipst = ill->ill_ipst; 13622 13623 /* 13624 * No IRE for this destination, so it can't be for us. 13625 * Unless we are forwarding, drop the packet. 13626 * We have to let source routed packets through 13627 * since we don't yet know if they are 'ping -l' 13628 * packets i.e. if they will go out over the 13629 * same interface as they came in on. 13630 */ 13631 if (ll_multicast) { 13632 freemsg(mp); 13633 return (NULL); 13634 } 13635 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) { 13636 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13637 freemsg(mp); 13638 return (NULL); 13639 } 13640 13641 /* 13642 * Mark this packet as having originated externally. 13643 * 13644 * For non-forwarding code path, ire_send later double 13645 * checks this interface to see if it is still exists 13646 * post-ARP resolution. 13647 * 13648 * Also, IPQOS uses this to differentiate between 13649 * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP 13650 * QOS packet processing in ip_wput_attach_llhdr(). 13651 * The QoS module can mark the b_band for a fastpath message 13652 * or the dl_priority field in a unitdata_req header for 13653 * CoS marking. This info can only be found in 13654 * ip_wput_attach_llhdr(). 13655 */ 13656 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 13657 /* 13658 * Clear the indication that this may have a hardware checksum 13659 * as we are not using it 13660 */ 13661 DB_CKSUMFLAGS(mp) = 0; 13662 13663 ire = ire_forward(dst, &ret_action, NULL, NULL, 13664 MBLK_GETLABEL(mp), ipst); 13665 13666 if (ire == NULL && ret_action == Forward_check_multirt) { 13667 /* Let ip_newroute handle CGTP */ 13668 ip_newroute(q, mp, dst, NULL, GLOBAL_ZONEID, ipst); 13669 return (NULL); 13670 } 13671 13672 if (ire != NULL) 13673 return (ire); 13674 13675 mp->b_prev = mp->b_next = 0; 13676 13677 if (ret_action == Forward_blackhole) { 13678 freemsg(mp); 13679 return (NULL); 13680 } 13681 /* send icmp unreachable */ 13682 q = WR(q); 13683 /* Sent by forwarding path, and router is global zone */ 13684 if (ip_source_routed(ipha, ipst)) { 13685 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, 13686 GLOBAL_ZONEID, ipst); 13687 } else { 13688 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 13689 ipst); 13690 } 13691 13692 return (NULL); 13693 13694 } 13695 13696 /* 13697 * check ip header length and align it. 13698 */ 13699 static boolean_t 13700 ip_check_and_align_header(queue_t *q, mblk_t *mp, ip_stack_t *ipst) 13701 { 13702 ssize_t len; 13703 ill_t *ill; 13704 ipha_t *ipha; 13705 13706 len = MBLKL(mp); 13707 13708 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13709 ill = (ill_t *)q->q_ptr; 13710 13711 if (!OK_32PTR(mp->b_rptr)) 13712 IP_STAT(ipst, ip_notaligned1); 13713 else 13714 IP_STAT(ipst, ip_notaligned2); 13715 /* Guard against bogus device drivers */ 13716 if (len < 0) { 13717 /* clear b_prev - used by ip_mroute_decap */ 13718 mp->b_prev = NULL; 13719 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13720 freemsg(mp); 13721 return (B_FALSE); 13722 } 13723 13724 if (ip_rput_pullups++ == 0) { 13725 ipha = (ipha_t *)mp->b_rptr; 13726 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 13727 "ip_check_and_align_header: %s forced us to " 13728 " pullup pkt, hdr len %ld, hdr addr %p", 13729 ill->ill_name, len, ipha); 13730 } 13731 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 13732 /* clear b_prev - used by ip_mroute_decap */ 13733 mp->b_prev = NULL; 13734 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13735 freemsg(mp); 13736 return (B_FALSE); 13737 } 13738 } 13739 return (B_TRUE); 13740 } 13741 13742 ire_t * 13743 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill) 13744 { 13745 ire_t *new_ire; 13746 ill_t *ire_ill; 13747 uint_t ifindex; 13748 ip_stack_t *ipst = ill->ill_ipst; 13749 boolean_t strict_check = B_FALSE; 13750 13751 /* 13752 * This packet came in on an interface other than the one associated 13753 * with the first ire we found for the destination address. We do 13754 * another ire lookup here, using the ingress ill, to see if the 13755 * interface is in an interface group. 13756 * As long as the ills belong to the same group, we don't consider 13757 * them to be arriving on the wrong interface. Thus, if the switch 13758 * is doing inbound load spreading, we won't drop packets when the 13759 * ip*_strict_dst_multihoming switch is on. Note, the same holds true 13760 * for 'usesrc groups' where the destination address may belong to 13761 * another interface to allow multipathing to happen. 13762 * We also need to check for IPIF_UNNUMBERED point2point interfaces 13763 * where the local address may not be unique. In this case we were 13764 * at the mercy of the initial ire cache lookup and the IRE_LOCAL it 13765 * actually returned. The new lookup, which is more specific, should 13766 * only find the IRE_LOCAL associated with the ingress ill if one 13767 * exists. 13768 */ 13769 13770 if (ire->ire_ipversion == IPV4_VERSION) { 13771 if (ipst->ips_ip_strict_dst_multihoming) 13772 strict_check = B_TRUE; 13773 new_ire = ire_ctable_lookup(*((ipaddr_t *)addr), 0, IRE_LOCAL, 13774 ill->ill_ipif, ALL_ZONES, NULL, 13775 (MATCH_IRE_TYPE|MATCH_IRE_ILL_GROUP), ipst); 13776 } else { 13777 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr)); 13778 if (ipst->ips_ipv6_strict_dst_multihoming) 13779 strict_check = B_TRUE; 13780 new_ire = ire_ctable_lookup_v6((in6_addr_t *)addr, NULL, 13781 IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL, 13782 (MATCH_IRE_TYPE|MATCH_IRE_ILL_GROUP), ipst); 13783 } 13784 /* 13785 * If the same ire that was returned in ip_input() is found then this 13786 * is an indication that interface groups are in use. The packet 13787 * arrived on a different ill in the group than the one associated with 13788 * the destination address. If a different ire was found then the same 13789 * IP address must be hosted on multiple ills. This is possible with 13790 * unnumbered point2point interfaces. We switch to use this new ire in 13791 * order to have accurate interface statistics. 13792 */ 13793 if (new_ire != NULL) { 13794 if ((new_ire != ire) && (new_ire->ire_rfq != NULL)) { 13795 ire_refrele(ire); 13796 ire = new_ire; 13797 } else { 13798 ire_refrele(new_ire); 13799 } 13800 return (ire); 13801 } else if ((ire->ire_rfq == NULL) && 13802 (ire->ire_ipversion == IPV4_VERSION)) { 13803 /* 13804 * The best match could have been the original ire which 13805 * was created against an IRE_LOCAL on lo0. In the IPv4 case 13806 * the strict multihoming checks are irrelevant as we consider 13807 * local addresses hosted on lo0 to be interface agnostic. We 13808 * only expect a null ire_rfq on IREs which are associated with 13809 * lo0 hence we can return now. 13810 */ 13811 return (ire); 13812 } 13813 13814 /* 13815 * Chase pointers once and store locally. 13816 */ 13817 ire_ill = (ire->ire_rfq == NULL) ? NULL : 13818 (ill_t *)(ire->ire_rfq->q_ptr); 13819 ifindex = ill->ill_usesrc_ifindex; 13820 13821 /* 13822 * Check if it's a legal address on the 'usesrc' interface. 13823 */ 13824 if ((ifindex != 0) && (ire_ill != NULL) && 13825 (ifindex == ire_ill->ill_phyint->phyint_ifindex)) { 13826 return (ire); 13827 } 13828 13829 /* 13830 * If the ip*_strict_dst_multihoming switch is on then we can 13831 * only accept this packet if the interface is marked as routing. 13832 */ 13833 if (!(strict_check)) 13834 return (ire); 13835 13836 if ((ill->ill_flags & ire->ire_ipif->ipif_ill->ill_flags & 13837 ILLF_ROUTER) != 0) { 13838 return (ire); 13839 } 13840 13841 ire_refrele(ire); 13842 return (NULL); 13843 } 13844 13845 ire_t * 13846 ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) 13847 { 13848 ipha_t *ipha; 13849 ire_t *src_ire; 13850 ill_t *stq_ill; 13851 uint_t hlen; 13852 uint_t pkt_len; 13853 uint32_t sum; 13854 queue_t *dev_q; 13855 ip_stack_t *ipst = ill->ill_ipst; 13856 mblk_t *fpmp; 13857 enum ire_forward_action ret_action; 13858 13859 ipha = (ipha_t *)mp->b_rptr; 13860 13861 /* 13862 * Martian Address Filtering [RFC 1812, Section 5.3.7] 13863 * The loopback address check for both src and dst has already 13864 * been checked in ip_input 13865 */ 13866 13867 if (dst == INADDR_ANY || CLASSD(ipha->ipha_src)) { 13868 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13869 goto drop; 13870 } 13871 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 13872 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 13873 13874 if (src_ire != NULL) { 13875 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13876 ire_refrele(src_ire); 13877 goto drop; 13878 } 13879 13880 13881 /* No ire cache of nexthop. So first create one */ 13882 if (ire == NULL) { 13883 13884 ire = ire_forward(dst, &ret_action, NULL, NULL, 13885 NULL, ipst); 13886 /* 13887 * We only come to ip_fast_forward if ip_cgtp_filter 13888 * is not set. So ire_forward() should not return with 13889 * Forward_check_multirt as the next action. 13890 */ 13891 ASSERT(ret_action != Forward_check_multirt); 13892 if (ire == NULL) { 13893 /* An attempt was made to forward the packet */ 13894 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 13895 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13896 mp->b_prev = mp->b_next = 0; 13897 /* send icmp unreachable */ 13898 /* Sent by forwarding path, and router is global zone */ 13899 if (ret_action == Forward_ret_icmp_err) { 13900 if (ip_source_routed(ipha, ipst)) { 13901 icmp_unreachable(ill->ill_wq, mp, 13902 ICMP_SOURCE_ROUTE_FAILED, 13903 GLOBAL_ZONEID, ipst); 13904 } else { 13905 icmp_unreachable(ill->ill_wq, mp, 13906 ICMP_HOST_UNREACHABLE, 13907 GLOBAL_ZONEID, ipst); 13908 } 13909 } else { 13910 freemsg(mp); 13911 } 13912 return (NULL); 13913 } 13914 } 13915 13916 /* 13917 * Forwarding fastpath exception case: 13918 * If either of the follwoing case is true, we take 13919 * the slowpath 13920 * o forwarding is not enabled 13921 * o incoming and outgoing interface are the same, or the same 13922 * IPMP group 13923 * o corresponding ire is in incomplete state 13924 * o packet needs fragmentation 13925 * o ARP cache is not resolved 13926 * 13927 * The codeflow from here on is thus: 13928 * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 13929 */ 13930 pkt_len = ntohs(ipha->ipha_length); 13931 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 13932 if (!(stq_ill->ill_flags & ILLF_ROUTER) || 13933 !(ill->ill_flags & ILLF_ROUTER) || 13934 (ill == stq_ill) || 13935 (ill->ill_group != NULL && ill->ill_group == stq_ill->ill_group) || 13936 (ire->ire_nce == NULL) || 13937 (pkt_len > ire->ire_max_frag) || 13938 ((fpmp = ire->ire_nce->nce_fp_mp) == NULL) || 13939 ((hlen = MBLKL(fpmp)) > MBLKHEAD(mp)) || 13940 ipha->ipha_ttl <= 1) { 13941 ip_rput_process_forward(ill->ill_rq, mp, ire, 13942 ipha, ill, B_FALSE); 13943 return (ire); 13944 } 13945 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 13946 13947 DTRACE_PROBE4(ip4__forwarding__start, 13948 ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 13949 13950 FW_HOOKS(ipst->ips_ip4_forwarding_event, 13951 ipst->ips_ipv4firewall_forwarding, 13952 ill, stq_ill, ipha, mp, mp, 0, ipst); 13953 13954 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 13955 13956 if (mp == NULL) 13957 goto drop; 13958 13959 mp->b_datap->db_struioun.cksum.flags = 0; 13960 /* Adjust the checksum to reflect the ttl decrement. */ 13961 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 13962 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 13963 ipha->ipha_ttl--; 13964 13965 /* 13966 * Write the link layer header. We can do this safely here, 13967 * because we have already tested to make sure that the IP 13968 * policy is not set, and that we have a fast path destination 13969 * header. 13970 */ 13971 mp->b_rptr -= hlen; 13972 bcopy(fpmp->b_rptr, mp->b_rptr, hlen); 13973 13974 UPDATE_IB_PKT_COUNT(ire); 13975 ire->ire_last_used_time = lbolt; 13976 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 13977 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 13978 UPDATE_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutOctets, pkt_len); 13979 13980 dev_q = ire->ire_stq->q_next; 13981 if ((dev_q->q_next != NULL || dev_q->q_first != NULL) && 13982 !canputnext(ire->ire_stq)) { 13983 goto indiscard; 13984 } 13985 if (ILL_DLS_CAPABLE(stq_ill)) { 13986 /* 13987 * Send the packet directly to DLD, where it 13988 * may be queued depending on the availability 13989 * of transmit resources at the media layer. 13990 */ 13991 IP_DLS_ILL_TX(stq_ill, ipha, mp, ipst); 13992 } else { 13993 DTRACE_PROBE4(ip4__physical__out__start, 13994 ill_t *, NULL, ill_t *, stq_ill, 13995 ipha_t *, ipha, mblk_t *, mp); 13996 FW_HOOKS(ipst->ips_ip4_physical_out_event, 13997 ipst->ips_ipv4firewall_physical_out, 13998 NULL, stq_ill, ipha, mp, mp, 0, ipst); 13999 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 14000 if (mp == NULL) 14001 goto drop; 14002 putnext(ire->ire_stq, mp); 14003 } 14004 return (ire); 14005 14006 indiscard: 14007 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14008 drop: 14009 if (mp != NULL) 14010 freemsg(mp); 14011 return (ire); 14012 14013 } 14014 14015 /* 14016 * This function is called in the forwarding slowpath, when 14017 * either the ire lacks the link-layer address, or the packet needs 14018 * further processing(eg. fragmentation), before transmission. 14019 */ 14020 14021 static void 14022 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14023 ill_t *ill, boolean_t ll_multicast) 14024 { 14025 ill_group_t *ill_group; 14026 ill_group_t *ire_group; 14027 queue_t *dev_q; 14028 ire_t *src_ire; 14029 ip_stack_t *ipst = ill->ill_ipst; 14030 14031 ASSERT(ire->ire_stq != NULL); 14032 14033 mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ 14034 mp->b_next = NULL; /* ip_rput_noire sets dst here */ 14035 14036 if (ll_multicast != 0) { 14037 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14038 goto drop_pkt; 14039 } 14040 14041 /* 14042 * check if ipha_src is a broadcast address. Note that this 14043 * check is redundant when we get here from ip_fast_forward() 14044 * which has already done this check. However, since we can 14045 * also get here from ip_rput_process_broadcast() or, for 14046 * for the slow path through ip_fast_forward(), we perform 14047 * the check again for code-reusability 14048 */ 14049 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14050 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 14051 if (src_ire != NULL || ipha->ipha_dst == INADDR_ANY) { 14052 if (src_ire != NULL) 14053 ire_refrele(src_ire); 14054 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14055 ip2dbg(("ip_rput_process_forward: Received packet with" 14056 " bad src/dst address on %s\n", ill->ill_name)); 14057 goto drop_pkt; 14058 } 14059 14060 ill_group = ill->ill_group; 14061 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 14062 /* 14063 * Check if we want to forward this one at this time. 14064 * We allow source routed packets on a host provided that 14065 * they go out the same interface or same interface group 14066 * as they came in on. 14067 * 14068 * XXX To be quicker, we may wish to not chase pointers to 14069 * get the ILLF_ROUTER flag and instead store the 14070 * forwarding policy in the ire. An unfortunate 14071 * side-effect of that would be requiring an ire flush 14072 * whenever the ILLF_ROUTER flag changes. 14073 */ 14074 if (((ill->ill_flags & 14075 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 14076 ILLF_ROUTER) == 0) && 14077 !(ip_source_routed(ipha, ipst) && (ire->ire_rfq == q || 14078 (ill_group != NULL && ill_group == ire_group)))) { 14079 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14080 if (ip_source_routed(ipha, ipst)) { 14081 q = WR(q); 14082 /* 14083 * Clear the indication that this may have 14084 * hardware checksum as we are not using it. 14085 */ 14086 DB_CKSUMFLAGS(mp) = 0; 14087 /* Sent by forwarding path, and router is global zone */ 14088 icmp_unreachable(q, mp, 14089 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); 14090 return; 14091 } 14092 goto drop_pkt; 14093 } 14094 14095 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14096 14097 /* Packet is being forwarded. Turning off hwcksum flag. */ 14098 DB_CKSUMFLAGS(mp) = 0; 14099 if (ipst->ips_ip_g_send_redirects) { 14100 /* 14101 * Check whether the incoming interface and outgoing 14102 * interface is part of the same group. If so, 14103 * send redirects. 14104 * 14105 * Check the source address to see if it originated 14106 * on the same logical subnet it is going back out on. 14107 * If so, we should be able to send it a redirect. 14108 * Avoid sending a redirect if the destination 14109 * is directly connected (i.e., ipha_dst is the same 14110 * as ire_gateway_addr or the ire_addr of the 14111 * nexthop IRE_CACHE ), or if the packet was source 14112 * routed out this interface. 14113 */ 14114 ipaddr_t src, nhop; 14115 mblk_t *mp1; 14116 ire_t *nhop_ire = NULL; 14117 14118 /* 14119 * Check whether ire_rfq and q are from the same ill 14120 * or if they are not same, they at least belong 14121 * to the same group. If so, send redirects. 14122 */ 14123 if ((ire->ire_rfq == q || 14124 (ill_group != NULL && ill_group == ire_group)) && 14125 !ip_source_routed(ipha, ipst)) { 14126 14127 nhop = (ire->ire_gateway_addr != 0 ? 14128 ire->ire_gateway_addr : ire->ire_addr); 14129 14130 if (ipha->ipha_dst == nhop) { 14131 /* 14132 * We avoid sending a redirect if the 14133 * destination is directly connected 14134 * because it is possible that multiple 14135 * IP subnets may have been configured on 14136 * the link, and the source may not 14137 * be on the same subnet as ip destination, 14138 * even though they are on the same 14139 * physical link. 14140 */ 14141 goto sendit; 14142 } 14143 14144 src = ipha->ipha_src; 14145 14146 /* 14147 * We look up the interface ire for the nexthop, 14148 * to see if ipha_src is in the same subnet 14149 * as the nexthop. 14150 * 14151 * Note that, if, in the future, IRE_CACHE entries 14152 * are obsoleted, this lookup will not be needed, 14153 * as the ire passed to this function will be the 14154 * same as the nhop_ire computed below. 14155 */ 14156 nhop_ire = ire_ftable_lookup(nhop, 0, 0, 14157 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 14158 0, NULL, MATCH_IRE_TYPE, ipst); 14159 14160 if (nhop_ire != NULL) { 14161 if ((src & nhop_ire->ire_mask) == 14162 (nhop & nhop_ire->ire_mask)) { 14163 /* 14164 * The source is directly connected. 14165 * Just copy the ip header (which is 14166 * in the first mblk) 14167 */ 14168 mp1 = copyb(mp); 14169 if (mp1 != NULL) { 14170 icmp_send_redirect(WR(q), mp1, 14171 nhop, ipst); 14172 } 14173 } 14174 ire_refrele(nhop_ire); 14175 } 14176 } 14177 } 14178 sendit: 14179 dev_q = ire->ire_stq->q_next; 14180 if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { 14181 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14182 freemsg(mp); 14183 return; 14184 } 14185 14186 ip_rput_forward(ire, ipha, mp, ill); 14187 return; 14188 14189 drop_pkt: 14190 ip2dbg(("ip_rput_process_forward: drop pkt\n")); 14191 freemsg(mp); 14192 } 14193 14194 ire_t * 14195 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14196 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 14197 { 14198 queue_t *q; 14199 uint16_t hcksumflags; 14200 ip_stack_t *ipst = ill->ill_ipst; 14201 14202 q = *qp; 14203 14204 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts); 14205 14206 /* 14207 * Clear the indication that this may have hardware 14208 * checksum as we are not using it for forwarding. 14209 */ 14210 hcksumflags = DB_CKSUMFLAGS(mp); 14211 DB_CKSUMFLAGS(mp) = 0; 14212 14213 /* 14214 * Directed broadcast forwarding: if the packet came in over a 14215 * different interface then it is routed out over we can forward it. 14216 */ 14217 if (ipha->ipha_protocol == IPPROTO_TCP) { 14218 ire_refrele(ire); 14219 freemsg(mp); 14220 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14221 return (NULL); 14222 } 14223 /* 14224 * For multicast we have set dst to be INADDR_BROADCAST 14225 * for delivering to all STREAMS. IRE_MARK_NORECV is really 14226 * only for broadcast packets. 14227 */ 14228 if (!CLASSD(ipha->ipha_dst)) { 14229 ire_t *new_ire; 14230 ipif_t *ipif; 14231 /* 14232 * For ill groups, as the switch duplicates broadcasts 14233 * across all the ports, we need to filter out and 14234 * send up only one copy. There is one copy for every 14235 * broadcast address on each ill. Thus, we look for a 14236 * specific IRE on this ill and look at IRE_MARK_NORECV 14237 * later to see whether this ill is eligible to receive 14238 * them or not. ill_nominate_bcast_rcv() nominates only 14239 * one set of IREs for receiving. 14240 */ 14241 14242 ipif = ipif_get_next_ipif(NULL, ill); 14243 if (ipif == NULL) { 14244 ire_refrele(ire); 14245 freemsg(mp); 14246 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14247 return (NULL); 14248 } 14249 new_ire = ire_ctable_lookup(dst, 0, 0, 14250 ipif, ALL_ZONES, NULL, MATCH_IRE_ILL, ipst); 14251 ipif_refrele(ipif); 14252 14253 if (new_ire != NULL) { 14254 if (new_ire->ire_marks & IRE_MARK_NORECV) { 14255 ire_refrele(ire); 14256 ire_refrele(new_ire); 14257 freemsg(mp); 14258 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14259 return (NULL); 14260 } 14261 /* 14262 * In the special case of multirouted broadcast 14263 * packets, we unconditionally need to "gateway" 14264 * them to the appropriate interface here. 14265 * In the normal case, this cannot happen, because 14266 * there is no broadcast IRE tagged with the 14267 * RTF_MULTIRT flag. 14268 */ 14269 if (new_ire->ire_flags & RTF_MULTIRT) { 14270 ire_refrele(new_ire); 14271 if (ire->ire_rfq != NULL) { 14272 q = ire->ire_rfq; 14273 *qp = q; 14274 } 14275 } else { 14276 ire_refrele(ire); 14277 ire = new_ire; 14278 } 14279 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 14280 if (!ipst->ips_ip_g_forward_directed_bcast) { 14281 /* 14282 * Free the message if 14283 * ip_g_forward_directed_bcast is turned 14284 * off for non-local broadcast. 14285 */ 14286 ire_refrele(ire); 14287 freemsg(mp); 14288 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14289 return (NULL); 14290 } 14291 } else { 14292 /* 14293 * This CGTP packet successfully passed the 14294 * CGTP filter, but the related CGTP 14295 * broadcast IRE has not been found, 14296 * meaning that the redundant ipif is 14297 * probably down. However, if we discarded 14298 * this packet, its duplicate would be 14299 * filtered out by the CGTP filter so none 14300 * of them would get through. So we keep 14301 * going with this one. 14302 */ 14303 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 14304 if (ire->ire_rfq != NULL) { 14305 q = ire->ire_rfq; 14306 *qp = q; 14307 } 14308 } 14309 } 14310 if (ipst->ips_ip_g_forward_directed_bcast && ll_multicast == 0) { 14311 /* 14312 * Verify that there are not more then one 14313 * IRE_BROADCAST with this broadcast address which 14314 * has ire_stq set. 14315 * TODO: simplify, loop over all IRE's 14316 */ 14317 ire_t *ire1; 14318 int num_stq = 0; 14319 mblk_t *mp1; 14320 14321 /* Find the first one with ire_stq set */ 14322 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 14323 for (ire1 = ire; ire1 && 14324 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 14325 ire1 = ire1->ire_next) 14326 ; 14327 if (ire1) { 14328 ire_refrele(ire); 14329 ire = ire1; 14330 IRE_REFHOLD(ire); 14331 } 14332 14333 /* Check if there are additional ones with stq set */ 14334 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 14335 if (ire->ire_addr != ire1->ire_addr) 14336 break; 14337 if (ire1->ire_stq) { 14338 num_stq++; 14339 break; 14340 } 14341 } 14342 rw_exit(&ire->ire_bucket->irb_lock); 14343 if (num_stq == 1 && ire->ire_stq != NULL) { 14344 ip1dbg(("ip_rput_process_broadcast: directed " 14345 "broadcast to 0x%x\n", 14346 ntohl(ire->ire_addr))); 14347 mp1 = copymsg(mp); 14348 if (mp1) { 14349 switch (ipha->ipha_protocol) { 14350 case IPPROTO_UDP: 14351 ip_udp_input(q, mp1, ipha, ire, ill); 14352 break; 14353 default: 14354 ip_proto_input(q, mp1, ipha, ire, ill, 14355 B_FALSE); 14356 break; 14357 } 14358 } 14359 /* 14360 * Adjust ttl to 2 (1+1 - the forward engine 14361 * will decrement it by one. 14362 */ 14363 if (ip_csum_hdr(ipha)) { 14364 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 14365 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 14366 freemsg(mp); 14367 ire_refrele(ire); 14368 return (NULL); 14369 } 14370 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1; 14371 ipha->ipha_hdr_checksum = 0; 14372 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14373 ip_rput_process_forward(q, mp, ire, ipha, 14374 ill, ll_multicast); 14375 ire_refrele(ire); 14376 return (NULL); 14377 } 14378 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 14379 ntohl(ire->ire_addr))); 14380 } 14381 14382 14383 /* Restore any hardware checksum flags */ 14384 DB_CKSUMFLAGS(mp) = hcksumflags; 14385 return (ire); 14386 } 14387 14388 /* ARGSUSED */ 14389 static boolean_t 14390 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 14391 int *ll_multicast, ipaddr_t *dstp) 14392 { 14393 ip_stack_t *ipst = ill->ill_ipst; 14394 14395 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts); 14396 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, 14397 ntohs(ipha->ipha_length)); 14398 14399 /* 14400 * Forward packets only if we have joined the allmulti 14401 * group on this interface. 14402 */ 14403 if (ipst->ips_ip_g_mrouter && ill->ill_join_allmulti) { 14404 int retval; 14405 14406 /* 14407 * Clear the indication that this may have hardware 14408 * checksum as we are not using it. 14409 */ 14410 DB_CKSUMFLAGS(mp) = 0; 14411 retval = ip_mforward(ill, ipha, mp); 14412 /* ip_mforward updates mib variables if needed */ 14413 /* clear b_prev - used by ip_mroute_decap */ 14414 mp->b_prev = NULL; 14415 14416 switch (retval) { 14417 case 0: 14418 /* 14419 * pkt is okay and arrived on phyint. 14420 * 14421 * If we are running as a multicast router 14422 * we need to see all IGMP and/or PIM packets. 14423 */ 14424 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 14425 (ipha->ipha_protocol == IPPROTO_PIM)) { 14426 goto done; 14427 } 14428 break; 14429 case -1: 14430 /* pkt is mal-formed, toss it */ 14431 goto drop_pkt; 14432 case 1: 14433 /* pkt is okay and arrived on a tunnel */ 14434 /* 14435 * If we are running a multicast router 14436 * we need to see all igmp packets. 14437 */ 14438 if (ipha->ipha_protocol == IPPROTO_IGMP) { 14439 *dstp = INADDR_BROADCAST; 14440 *ll_multicast = 1; 14441 return (B_FALSE); 14442 } 14443 14444 goto drop_pkt; 14445 } 14446 } 14447 14448 ILM_WALKER_HOLD(ill); 14449 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 14450 /* 14451 * This might just be caused by the fact that 14452 * multiple IP Multicast addresses map to the same 14453 * link layer multicast - no need to increment counter! 14454 */ 14455 ILM_WALKER_RELE(ill); 14456 freemsg(mp); 14457 return (B_TRUE); 14458 } 14459 ILM_WALKER_RELE(ill); 14460 done: 14461 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 14462 /* 14463 * This assumes the we deliver to all streams for multicast 14464 * and broadcast packets. 14465 */ 14466 *dstp = INADDR_BROADCAST; 14467 *ll_multicast = 1; 14468 return (B_FALSE); 14469 drop_pkt: 14470 ip2dbg(("ip_rput: drop pkt\n")); 14471 freemsg(mp); 14472 return (B_TRUE); 14473 } 14474 14475 /* 14476 * This function is used to both return an indication of whether or not 14477 * the packet received is a non-unicast packet (by way of the DL_UNITDATA_IND) 14478 * and in doing so, determine whether or not it is broadcast vs multicast. 14479 * For it to be a broadcast packet, we must have the appropriate mblk_t 14480 * hanging off the ill_t. If this is either not present or doesn't match 14481 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed 14482 * to be multicast. Thus NICs that have no broadcast address (or no 14483 * capability for one, such as point to point links) cannot return as 14484 * the packet being broadcast. The use of HPE_BROADCAST/HPE_MULTICAST as 14485 * the return values simplifies the current use of the return value of this 14486 * function, which is to pass through the multicast/broadcast characteristic 14487 * to consumers of the netinfo/pfhooks API. While this is not cast in stone, 14488 * changing the return value to some other symbol demands the appropriate 14489 * "translation" when hpe_flags is set prior to calling hook_run() for 14490 * packet events. 14491 */ 14492 int 14493 ip_get_dlpi_mbcast(ill_t *ill, mblk_t *mb) 14494 { 14495 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr; 14496 mblk_t *bmp; 14497 14498 if (ind->dl_group_address) { 14499 if (ind->dl_dest_addr_offset > sizeof (*ind) && 14500 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < 14501 MBLKL(mb) && 14502 (bmp = ill->ill_bcast_mp) != NULL) { 14503 dl_unitdata_req_t *dlur; 14504 uint8_t *bphys_addr; 14505 14506 dlur = (dl_unitdata_req_t *)bmp->b_rptr; 14507 if (ill->ill_sap_length < 0) 14508 bphys_addr = (uchar_t *)dlur + 14509 dlur->dl_dest_addr_offset; 14510 else 14511 bphys_addr = (uchar_t *)dlur + 14512 dlur->dl_dest_addr_offset + 14513 ill->ill_sap_length; 14514 14515 if (bcmp(mb->b_rptr + ind->dl_dest_addr_offset, 14516 bphys_addr, ind->dl_dest_addr_length) == 0) { 14517 return (HPE_BROADCAST); 14518 } 14519 return (HPE_MULTICAST); 14520 } 14521 return (HPE_MULTICAST); 14522 } 14523 return (0); 14524 } 14525 14526 static boolean_t 14527 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 14528 int *ll_multicast, mblk_t **mpp) 14529 { 14530 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 14531 boolean_t must_copy = B_FALSE; 14532 struct iocblk *iocp; 14533 ipha_t *ipha; 14534 ip_stack_t *ipst = ill->ill_ipst; 14535 14536 #define rptr ((uchar_t *)ipha) 14537 14538 first_mp = *first_mpp; 14539 mp = *mpp; 14540 14541 ASSERT(first_mp == mp); 14542 14543 /* 14544 * if db_ref > 1 then copymsg and free original. Packet may be 14545 * changed and do not want other entity who has a reference to this 14546 * message to trip over the changes. This is a blind change because 14547 * trying to catch all places that might change packet is too 14548 * difficult (since it may be a module above this one) 14549 * 14550 * This corresponds to the non-fast path case. We walk down the full 14551 * chain in this case, and check the db_ref count of all the dblks, 14552 * and do a copymsg if required. It is possible that the db_ref counts 14553 * of the data blocks in the mblk chain can be different. 14554 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 14555 * count of 1, followed by a M_DATA block with a ref count of 2, if 14556 * 'snoop' is running. 14557 */ 14558 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 14559 if (mp1->b_datap->db_ref > 1) { 14560 must_copy = B_TRUE; 14561 break; 14562 } 14563 } 14564 14565 if (must_copy) { 14566 mp1 = copymsg(mp); 14567 if (mp1 == NULL) { 14568 for (mp1 = mp; mp1 != NULL; 14569 mp1 = mp1->b_cont) { 14570 mp1->b_next = NULL; 14571 mp1->b_prev = NULL; 14572 } 14573 freemsg(mp); 14574 if (ill != NULL) { 14575 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14576 } else { 14577 BUMP_MIB(&ipst->ips_ip_mib, 14578 ipIfStatsInDiscards); 14579 } 14580 return (B_TRUE); 14581 } 14582 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 14583 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 14584 /* Copy b_prev - used by ip_mroute_decap */ 14585 to_mp->b_prev = from_mp->b_prev; 14586 from_mp->b_prev = NULL; 14587 } 14588 *first_mpp = first_mp = mp1; 14589 freemsg(mp); 14590 mp = mp1; 14591 *mpp = mp1; 14592 } 14593 14594 ipha = (ipha_t *)mp->b_rptr; 14595 14596 /* 14597 * previous code has a case for M_DATA. 14598 * We want to check how that happens. 14599 */ 14600 ASSERT(first_mp->b_datap->db_type != M_DATA); 14601 switch (first_mp->b_datap->db_type) { 14602 case M_PROTO: 14603 case M_PCPROTO: 14604 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 14605 DL_UNITDATA_IND) { 14606 /* Go handle anything other than data elsewhere. */ 14607 ip_rput_dlpi(q, mp); 14608 return (B_TRUE); 14609 } 14610 14611 *ll_multicast = ip_get_dlpi_mbcast(ill, mp); 14612 /* Ditch the DLPI header. */ 14613 mp1 = mp->b_cont; 14614 ASSERT(first_mp == mp); 14615 *first_mpp = mp1; 14616 freeb(mp); 14617 *mpp = mp1; 14618 return (B_FALSE); 14619 case M_IOCACK: 14620 ip1dbg(("got iocack ")); 14621 iocp = (struct iocblk *)mp->b_rptr; 14622 switch (iocp->ioc_cmd) { 14623 case DL_IOC_HDR_INFO: 14624 ill = (ill_t *)q->q_ptr; 14625 ill_fastpath_ack(ill, mp); 14626 return (B_TRUE); 14627 case SIOCSTUNPARAM: 14628 case OSIOCSTUNPARAM: 14629 /* Go through qwriter_ip */ 14630 break; 14631 case SIOCGTUNPARAM: 14632 case OSIOCGTUNPARAM: 14633 ip_rput_other(NULL, q, mp, NULL); 14634 return (B_TRUE); 14635 default: 14636 putnext(q, mp); 14637 return (B_TRUE); 14638 } 14639 /* FALLTHRU */ 14640 case M_ERROR: 14641 case M_HANGUP: 14642 /* 14643 * Since this is on the ill stream we unconditionally 14644 * bump up the refcount 14645 */ 14646 ill_refhold(ill); 14647 qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); 14648 return (B_TRUE); 14649 case M_CTL: 14650 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 14651 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 14652 IPHADA_M_CTL)) { 14653 /* 14654 * It's an IPsec accelerated packet. 14655 * Make sure that the ill from which we received the 14656 * packet has enabled IPsec hardware acceleration. 14657 */ 14658 if (!(ill->ill_capabilities & 14659 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 14660 /* IPsec kstats: bean counter */ 14661 freemsg(mp); 14662 return (B_TRUE); 14663 } 14664 14665 /* 14666 * Make mp point to the mblk following the M_CTL, 14667 * then process according to type of mp. 14668 * After this processing, first_mp will point to 14669 * the data-attributes and mp to the pkt following 14670 * the M_CTL. 14671 */ 14672 mp = first_mp->b_cont; 14673 if (mp == NULL) { 14674 freemsg(first_mp); 14675 return (B_TRUE); 14676 } 14677 /* 14678 * A Hardware Accelerated packet can only be M_DATA 14679 * ESP or AH packet. 14680 */ 14681 if (mp->b_datap->db_type != M_DATA) { 14682 /* non-M_DATA IPsec accelerated packet */ 14683 IPSECHW_DEBUG(IPSECHW_PKT, 14684 ("non-M_DATA IPsec accelerated pkt\n")); 14685 freemsg(first_mp); 14686 return (B_TRUE); 14687 } 14688 ipha = (ipha_t *)mp->b_rptr; 14689 if (ipha->ipha_protocol != IPPROTO_AH && 14690 ipha->ipha_protocol != IPPROTO_ESP) { 14691 IPSECHW_DEBUG(IPSECHW_PKT, 14692 ("non-M_DATA IPsec accelerated pkt\n")); 14693 freemsg(first_mp); 14694 return (B_TRUE); 14695 } 14696 *mpp = mp; 14697 return (B_FALSE); 14698 } 14699 putnext(q, mp); 14700 return (B_TRUE); 14701 case M_IOCNAK: 14702 ip1dbg(("got iocnak ")); 14703 iocp = (struct iocblk *)mp->b_rptr; 14704 switch (iocp->ioc_cmd) { 14705 case SIOCSTUNPARAM: 14706 case OSIOCSTUNPARAM: 14707 /* 14708 * Since this is on the ill stream we unconditionally 14709 * bump up the refcount 14710 */ 14711 ill_refhold(ill); 14712 qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); 14713 return (B_TRUE); 14714 case DL_IOC_HDR_INFO: 14715 case SIOCGTUNPARAM: 14716 case OSIOCGTUNPARAM: 14717 ip_rput_other(NULL, q, mp, NULL); 14718 return (B_TRUE); 14719 default: 14720 break; 14721 } 14722 /* FALLTHRU */ 14723 default: 14724 putnext(q, mp); 14725 return (B_TRUE); 14726 } 14727 } 14728 14729 /* Read side put procedure. Packets coming from the wire arrive here. */ 14730 void 14731 ip_rput(queue_t *q, mblk_t *mp) 14732 { 14733 ill_t *ill; 14734 union DL_primitives *dl; 14735 14736 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 14737 14738 ill = (ill_t *)q->q_ptr; 14739 14740 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 14741 /* 14742 * If things are opening or closing, only accept high-priority 14743 * DLPI messages. (On open ill->ill_ipif has not yet been 14744 * created; on close, things hanging off the ill may have been 14745 * freed already.) 14746 */ 14747 dl = (union DL_primitives *)mp->b_rptr; 14748 if (DB_TYPE(mp) != M_PCPROTO || 14749 dl->dl_primitive == DL_UNITDATA_IND) { 14750 /* 14751 * SIOC[GS]TUNPARAM ioctls can come here. 14752 */ 14753 inet_freemsg(mp); 14754 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14755 "ip_rput_end: q %p (%S)", q, "uninit"); 14756 return; 14757 } 14758 } 14759 14760 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14761 "ip_rput_end: q %p (%S)", q, "end"); 14762 14763 ip_input(ill, NULL, mp, NULL); 14764 } 14765 14766 static mblk_t * 14767 ip_fix_dbref(ill_t *ill, mblk_t *mp) 14768 { 14769 mblk_t *mp1; 14770 boolean_t adjusted = B_FALSE; 14771 ip_stack_t *ipst = ill->ill_ipst; 14772 14773 IP_STAT(ipst, ip_db_ref); 14774 /* 14775 * The IP_RECVSLLA option depends on having the 14776 * link layer header. First check that: 14777 * a> the underlying device is of type ether, 14778 * since this option is currently supported only 14779 * over ethernet. 14780 * b> there is enough room to copy over the link 14781 * layer header. 14782 * 14783 * Once the checks are done, adjust rptr so that 14784 * the link layer header will be copied via 14785 * copymsg. Note that, IFT_ETHER may be returned 14786 * by some non-ethernet drivers but in this case 14787 * the second check will fail. 14788 */ 14789 if (ill->ill_type == IFT_ETHER && 14790 (mp->b_rptr - mp->b_datap->db_base) >= 14791 sizeof (struct ether_header)) { 14792 mp->b_rptr -= sizeof (struct ether_header); 14793 adjusted = B_TRUE; 14794 } 14795 mp1 = copymsg(mp); 14796 14797 if (mp1 == NULL) { 14798 mp->b_next = NULL; 14799 /* clear b_prev - used by ip_mroute_decap */ 14800 mp->b_prev = NULL; 14801 freemsg(mp); 14802 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14803 return (NULL); 14804 } 14805 14806 if (adjusted) { 14807 /* 14808 * Copy is done. Restore the pointer in 14809 * the _new_ mblk 14810 */ 14811 mp1->b_rptr += sizeof (struct ether_header); 14812 } 14813 14814 /* Copy b_prev - used by ip_mroute_decap */ 14815 mp1->b_prev = mp->b_prev; 14816 mp->b_prev = NULL; 14817 14818 /* preserve the hardware checksum flags and data, if present */ 14819 if (DB_CKSUMFLAGS(mp) != 0) { 14820 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp); 14821 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp); 14822 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp); 14823 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp); 14824 DB_CKSUM16(mp1) = DB_CKSUM16(mp); 14825 } 14826 14827 freemsg(mp); 14828 return (mp1); 14829 } 14830 14831 /* 14832 * Direct read side procedure capable of dealing with chains. GLDv3 based 14833 * drivers call this function directly with mblk chains while STREAMS 14834 * read side procedure ip_rput() calls this for single packet with ip_ring 14835 * set to NULL to process one packet at a time. 14836 * 14837 * The ill will always be valid if this function is called directly from 14838 * the driver. 14839 * 14840 * If ip_input() is called from GLDv3: 14841 * 14842 * - This must be a non-VLAN IP stream. 14843 * - 'mp' is either an untagged or a special priority-tagged packet. 14844 * - Any VLAN tag that was in the MAC header has been stripped. 14845 * 14846 * If the IP header in packet is not 32-bit aligned, every message in the 14847 * chain will be aligned before further operations. This is required on SPARC 14848 * platform. 14849 */ 14850 /* ARGSUSED */ 14851 void 14852 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, 14853 struct mac_header_info_s *mhip) 14854 { 14855 ipaddr_t dst = NULL; 14856 ipaddr_t prev_dst; 14857 ire_t *ire = NULL; 14858 ipha_t *ipha; 14859 uint_t pkt_len; 14860 ssize_t len; 14861 uint_t opt_len; 14862 int ll_multicast; 14863 int cgtp_flt_pkt; 14864 queue_t *q = ill->ill_rq; 14865 squeue_t *curr_sqp = NULL; 14866 mblk_t *head = NULL; 14867 mblk_t *tail = NULL; 14868 mblk_t *first_mp; 14869 mblk_t *mp; 14870 mblk_t *dmp; 14871 int cnt = 0; 14872 ip_stack_t *ipst = ill->ill_ipst; 14873 14874 ASSERT(mp_chain != NULL); 14875 ASSERT(ill != NULL); 14876 14877 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 14878 14879 #define rptr ((uchar_t *)ipha) 14880 14881 while (mp_chain != NULL) { 14882 first_mp = mp = mp_chain; 14883 mp_chain = mp_chain->b_next; 14884 mp->b_next = NULL; 14885 ll_multicast = 0; 14886 14887 /* 14888 * We do ire caching from one iteration to 14889 * another. In the event the packet chain contains 14890 * all packets from the same dst, this caching saves 14891 * an ire_cache_lookup for each of the succeeding 14892 * packets in a packet chain. 14893 */ 14894 prev_dst = dst; 14895 14896 /* 14897 * if db_ref > 1 then copymsg and free original. Packet 14898 * may be changed and we do not want the other entity 14899 * who has a reference to this message to trip over the 14900 * changes. This is a blind change because trying to 14901 * catch all places that might change the packet is too 14902 * difficult. 14903 * 14904 * This corresponds to the fast path case, where we have 14905 * a chain of M_DATA mblks. We check the db_ref count 14906 * of only the 1st data block in the mblk chain. There 14907 * doesn't seem to be a reason why a device driver would 14908 * send up data with varying db_ref counts in the mblk 14909 * chain. In any case the Fast path is a private 14910 * interface, and our drivers don't do such a thing. 14911 * Given the above assumption, there is no need to walk 14912 * down the entire mblk chain (which could have a 14913 * potential performance problem) 14914 */ 14915 14916 if (DB_REF(mp) > 1) { 14917 if ((mp = ip_fix_dbref(ill, mp)) == NULL) 14918 continue; 14919 } 14920 14921 /* 14922 * Check and align the IP header. 14923 */ 14924 first_mp = mp; 14925 if (DB_TYPE(mp) == M_DATA) { 14926 dmp = mp; 14927 } else if (DB_TYPE(mp) == M_PROTO && 14928 *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { 14929 dmp = mp->b_cont; 14930 } else { 14931 dmp = NULL; 14932 } 14933 if (dmp != NULL) { 14934 /* 14935 * IP header ptr not aligned? 14936 * OR IP header not complete in first mblk 14937 */ 14938 if (!OK_32PTR(dmp->b_rptr) || 14939 MBLKL(dmp) < IP_SIMPLE_HDR_LENGTH) { 14940 if (!ip_check_and_align_header(q, dmp, ipst)) 14941 continue; 14942 } 14943 } 14944 14945 /* 14946 * ip_input fast path 14947 */ 14948 14949 /* mblk type is not M_DATA */ 14950 if (DB_TYPE(mp) != M_DATA) { 14951 if (ip_rput_process_notdata(q, &first_mp, ill, 14952 &ll_multicast, &mp)) 14953 continue; 14954 14955 /* 14956 * The only way we can get here is if we had a 14957 * packet that was either a DL_UNITDATA_IND or 14958 * an M_CTL for an IPsec accelerated packet. 14959 * 14960 * In either case, the first_mp will point to 14961 * the leading M_PROTO or M_CTL. 14962 */ 14963 ASSERT(first_mp != NULL); 14964 } else if (mhip != NULL) { 14965 /* 14966 * ll_multicast is set here so that it is ready 14967 * for easy use with FW_HOOKS(). ip_get_dlpi_mbcast 14968 * manipulates ll_multicast in the same fashion when 14969 * called from ip_rput_process_notdata. 14970 */ 14971 switch (mhip->mhi_dsttype) { 14972 case MAC_ADDRTYPE_MULTICAST : 14973 ll_multicast = HPE_MULTICAST; 14974 break; 14975 case MAC_ADDRTYPE_BROADCAST : 14976 ll_multicast = HPE_BROADCAST; 14977 break; 14978 default : 14979 break; 14980 } 14981 } 14982 14983 /* Make sure its an M_DATA and that its aligned */ 14984 ASSERT(DB_TYPE(mp) == M_DATA); 14985 ASSERT(DB_REF(mp) == 1 && OK_32PTR(mp->b_rptr)); 14986 14987 ipha = (ipha_t *)mp->b_rptr; 14988 len = mp->b_wptr - rptr; 14989 pkt_len = ntohs(ipha->ipha_length); 14990 14991 /* 14992 * We must count all incoming packets, even if they end 14993 * up being dropped later on. 14994 */ 14995 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 14996 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); 14997 14998 /* multiple mblk or too short */ 14999 len -= pkt_len; 15000 if (len != 0) { 15001 /* 15002 * Make sure we have data length consistent 15003 * with the IP header. 15004 */ 15005 if (mp->b_cont == NULL) { 15006 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15007 BUMP_MIB(ill->ill_ip_mib, 15008 ipIfStatsInHdrErrors); 15009 ip2dbg(("ip_input: drop pkt\n")); 15010 freemsg(mp); 15011 continue; 15012 } 15013 mp->b_wptr = rptr + pkt_len; 15014 } else if ((len += msgdsize(mp->b_cont)) != 0) { 15015 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15016 BUMP_MIB(ill->ill_ip_mib, 15017 ipIfStatsInHdrErrors); 15018 ip2dbg(("ip_input: drop pkt\n")); 15019 freemsg(mp); 15020 continue; 15021 } 15022 (void) adjmsg(mp, -len); 15023 IP_STAT(ipst, ip_multimblk3); 15024 } 15025 } 15026 15027 /* Obtain the dst of the current packet */ 15028 dst = ipha->ipha_dst; 15029 15030 /* 15031 * The following test for loopback is faster than 15032 * IP_LOOPBACK_ADDR(), because it avoids any bitwise 15033 * operations. 15034 * Note that these addresses are always in network byte order 15035 */ 15036 if (((*(uchar_t *)&ipha->ipha_dst) == 127) || 15037 ((*(uchar_t *)&ipha->ipha_src) == 127)) { 15038 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors); 15039 freemsg(mp); 15040 continue; 15041 } 15042 15043 /* 15044 * The event for packets being received from a 'physical' 15045 * interface is placed after validation of the source and/or 15046 * destination address as being local so that packets can be 15047 * redirected to loopback addresses using ipnat. 15048 */ 15049 DTRACE_PROBE4(ip4__physical__in__start, 15050 ill_t *, ill, ill_t *, NULL, 15051 ipha_t *, ipha, mblk_t *, first_mp); 15052 15053 FW_HOOKS(ipst->ips_ip4_physical_in_event, 15054 ipst->ips_ipv4firewall_physical_in, 15055 ill, NULL, ipha, first_mp, mp, ll_multicast, ipst); 15056 15057 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 15058 15059 if (first_mp == NULL) { 15060 continue; 15061 } 15062 dst = ipha->ipha_dst; 15063 15064 /* 15065 * Attach any necessary label information to 15066 * this packet 15067 */ 15068 if (is_system_labeled() && 15069 !tsol_get_pkt_label(mp, IPV4_VERSION)) { 15070 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 15071 freemsg(mp); 15072 continue; 15073 } 15074 15075 /* 15076 * Reuse the cached ire only if the ipha_dst of the previous 15077 * packet is the same as the current packet AND it is not 15078 * INADDR_ANY. 15079 */ 15080 if (!(dst == prev_dst && dst != INADDR_ANY) && 15081 (ire != NULL)) { 15082 ire_refrele(ire); 15083 ire = NULL; 15084 } 15085 opt_len = ipha->ipha_version_and_hdr_length - 15086 IP_SIMPLE_HDR_VERSION; 15087 15088 /* 15089 * Check to see if we can take the fastpath. 15090 * That is possible if the following conditions are met 15091 * o Tsol disabled 15092 * o CGTP disabled 15093 * o ipp_action_count is 0 15094 * o no options in the packet 15095 * o not a RSVP packet 15096 * o not a multicast packet 15097 * o ill not in IP_DHCPINIT_IF mode 15098 */ 15099 if (!is_system_labeled() && 15100 !ipst->ips_ip_cgtp_filter && ipp_action_count == 0 && 15101 opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && 15102 !ll_multicast && !CLASSD(dst) && ill->ill_dhcpinit == 0) { 15103 if (ire == NULL) 15104 ire = ire_cache_lookup(dst, ALL_ZONES, NULL, 15105 ipst); 15106 15107 /* incoming packet is for forwarding */ 15108 if (ire == NULL || (ire->ire_type & IRE_CACHE)) { 15109 ire = ip_fast_forward(ire, dst, ill, mp); 15110 continue; 15111 } 15112 /* incoming packet is for local consumption */ 15113 if (ire->ire_type & IRE_LOCAL) 15114 goto local; 15115 } 15116 15117 /* 15118 * Disable ire caching for anything more complex 15119 * than the simple fast path case we checked for above. 15120 */ 15121 if (ire != NULL) { 15122 ire_refrele(ire); 15123 ire = NULL; 15124 } 15125 15126 /* 15127 * Brutal hack for DHCPv4 unicast: RFC2131 allows a DHCP 15128 * server to unicast DHCP packets to a DHCP client using the 15129 * IP address it is offering to the client. This can be 15130 * disabled through the "broadcast bit", but not all DHCP 15131 * servers honor that bit. Therefore, to interoperate with as 15132 * many DHCP servers as possible, the DHCP client allows the 15133 * server to unicast, but we treat those packets as broadcast 15134 * here. Note that we don't rewrite the packet itself since 15135 * (a) that would mess up the checksums and (b) the DHCP 15136 * client conn is bound to INADDR_ANY so ip_fanout_udp() will 15137 * hand it the packet regardless. 15138 */ 15139 if (ill->ill_dhcpinit != 0 && 15140 IS_SIMPLE_IPH(ipha) && ipha->ipha_protocol == IPPROTO_UDP && 15141 pullupmsg(mp, sizeof (ipha_t) + sizeof (udpha_t)) == 1) { 15142 udpha_t *udpha; 15143 15144 /* 15145 * Reload ipha since pullupmsg() can change b_rptr. 15146 */ 15147 ipha = (ipha_t *)mp->b_rptr; 15148 udpha = (udpha_t *)&ipha[1]; 15149 15150 if (ntohs(udpha->uha_dst_port) == IPPORT_BOOTPC) { 15151 DTRACE_PROBE2(ip4__dhcpinit__pkt, ill_t *, ill, 15152 mblk_t *, mp); 15153 dst = INADDR_BROADCAST; 15154 } 15155 } 15156 15157 /* Full-blown slow path */ 15158 if (opt_len != 0) { 15159 if (len != 0) 15160 IP_STAT(ipst, ip_multimblk4); 15161 else 15162 IP_STAT(ipst, ip_ipoptions); 15163 if (!ip_rput_multimblk_ipoptions(q, ill, mp, &ipha, 15164 &dst, ipst)) 15165 continue; 15166 } 15167 15168 /* 15169 * Invoke the CGTP (multirouting) filtering module to process 15170 * the incoming packet. Packets identified as duplicates 15171 * must be discarded. Filtering is active only if the 15172 * the ip_cgtp_filter ndd variable is non-zero. 15173 */ 15174 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 15175 if (ipst->ips_ip_cgtp_filter && 15176 ipst->ips_ip_cgtp_filter_ops != NULL) { 15177 netstackid_t stackid; 15178 15179 stackid = ipst->ips_netstack->netstack_stackid; 15180 cgtp_flt_pkt = 15181 ipst->ips_ip_cgtp_filter_ops->cfo_filter(stackid, 15182 ill->ill_phyint->phyint_ifindex, mp); 15183 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 15184 freemsg(first_mp); 15185 continue; 15186 } 15187 } 15188 15189 /* 15190 * If rsvpd is running, let RSVP daemon handle its processing 15191 * and forwarding of RSVP multicast/unicast packets. 15192 * If rsvpd is not running but mrouted is running, RSVP 15193 * multicast packets are forwarded as multicast traffic 15194 * and RSVP unicast packets are forwarded by unicast router. 15195 * If neither rsvpd nor mrouted is running, RSVP multicast 15196 * packets are not forwarded, but the unicast packets are 15197 * forwarded like unicast traffic. 15198 */ 15199 if (ipha->ipha_protocol == IPPROTO_RSVP && 15200 ipst->ips_ipcl_proto_fanout[IPPROTO_RSVP].connf_head != 15201 NULL) { 15202 /* RSVP packet and rsvpd running. Treat as ours */ 15203 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 15204 /* 15205 * This assumes that we deliver to all streams for 15206 * multicast and broadcast packets. 15207 * We have to force ll_multicast to 1 to handle the 15208 * M_DATA messages passed in from ip_mroute_decap. 15209 */ 15210 dst = INADDR_BROADCAST; 15211 ll_multicast = 1; 15212 } else if (CLASSD(dst)) { 15213 /* packet is multicast */ 15214 mp->b_next = NULL; 15215 if (ip_rput_process_multicast(q, mp, ill, ipha, 15216 &ll_multicast, &dst)) 15217 continue; 15218 } 15219 15220 if (ire == NULL) { 15221 ire = ire_cache_lookup(dst, ALL_ZONES, 15222 MBLK_GETLABEL(mp), ipst); 15223 } 15224 15225 if (ire == NULL) { 15226 /* 15227 * No IRE for this destination, so it can't be for us. 15228 * Unless we are forwarding, drop the packet. 15229 * We have to let source routed packets through 15230 * since we don't yet know if they are 'ping -l' 15231 * packets i.e. if they will go out over the 15232 * same interface as they came in on. 15233 */ 15234 ire = ip_rput_noire(q, mp, ll_multicast, dst); 15235 if (ire == NULL) 15236 continue; 15237 } 15238 15239 /* 15240 * Broadcast IRE may indicate either broadcast or 15241 * multicast packet 15242 */ 15243 if (ire->ire_type == IRE_BROADCAST) { 15244 /* 15245 * Skip broadcast checks if packet is UDP multicast; 15246 * we'd rather not enter ip_rput_process_broadcast() 15247 * unless the packet is broadcast for real, since 15248 * that routine is a no-op for multicast. 15249 */ 15250 if (ipha->ipha_protocol != IPPROTO_UDP || 15251 !CLASSD(ipha->ipha_dst)) { 15252 ire = ip_rput_process_broadcast(&q, mp, 15253 ire, ipha, ill, dst, cgtp_flt_pkt, 15254 ll_multicast); 15255 if (ire == NULL) 15256 continue; 15257 } 15258 } else if (ire->ire_stq != NULL) { 15259 /* fowarding? */ 15260 ip_rput_process_forward(q, mp, ire, ipha, ill, 15261 ll_multicast); 15262 /* ip_rput_process_forward consumed the packet */ 15263 continue; 15264 } 15265 15266 local: 15267 /* 15268 * If the queue in the ire is different to the ingress queue 15269 * then we need to check to see if we can accept the packet. 15270 * Note that for multicast packets and broadcast packets sent 15271 * to a broadcast address which is shared between multiple 15272 * interfaces we should not do this since we just got a random 15273 * broadcast ire. 15274 */ 15275 if ((ire->ire_rfq != q) && (ire->ire_type != IRE_BROADCAST)) { 15276 if ((ire = ip_check_multihome(&ipha->ipha_dst, ire, 15277 ill)) == NULL) { 15278 /* Drop packet */ 15279 BUMP_MIB(ill->ill_ip_mib, 15280 ipIfStatsForwProhibits); 15281 freemsg(mp); 15282 continue; 15283 } 15284 if (ire->ire_rfq != NULL) 15285 q = ire->ire_rfq; 15286 } 15287 15288 switch (ipha->ipha_protocol) { 15289 case IPPROTO_TCP: 15290 ASSERT(first_mp == mp); 15291 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 15292 mp, 0, q, ip_ring)) != NULL) { 15293 if (curr_sqp == NULL) { 15294 curr_sqp = GET_SQUEUE(mp); 15295 ASSERT(cnt == 0); 15296 cnt++; 15297 head = tail = mp; 15298 } else if (curr_sqp == GET_SQUEUE(mp)) { 15299 ASSERT(tail != NULL); 15300 cnt++; 15301 tail->b_next = mp; 15302 tail = mp; 15303 } else { 15304 /* 15305 * A different squeue. Send the 15306 * chain for the previous squeue on 15307 * its way. This shouldn't happen 15308 * often unless interrupt binding 15309 * changes. 15310 */ 15311 IP_STAT(ipst, ip_input_multi_squeue); 15312 squeue_enter_chain(curr_sqp, head, 15313 tail, cnt, SQTAG_IP_INPUT); 15314 curr_sqp = GET_SQUEUE(mp); 15315 head = mp; 15316 tail = mp; 15317 cnt = 1; 15318 } 15319 } 15320 continue; 15321 case IPPROTO_UDP: 15322 ASSERT(first_mp == mp); 15323 ip_udp_input(q, mp, ipha, ire, ill); 15324 continue; 15325 case IPPROTO_SCTP: 15326 ASSERT(first_mp == mp); 15327 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 15328 q, dst); 15329 /* ire has been released by ip_sctp_input */ 15330 ire = NULL; 15331 continue; 15332 default: 15333 ip_proto_input(q, first_mp, ipha, ire, ill, B_FALSE); 15334 continue; 15335 } 15336 } 15337 15338 if (ire != NULL) 15339 ire_refrele(ire); 15340 15341 if (head != NULL) 15342 squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); 15343 15344 /* 15345 * This code is there just to make netperf/ttcp look good. 15346 * 15347 * Its possible that after being in polling mode (and having cleared 15348 * the backlog), squeues have turned the interrupt frequency higher 15349 * to improve latency at the expense of more CPU utilization (less 15350 * packets per interrupts or more number of interrupts). Workloads 15351 * like ttcp/netperf do manage to tickle polling once in a while 15352 * but for the remaining time, stay in higher interrupt mode since 15353 * their packet arrival rate is pretty uniform and this shows up 15354 * as higher CPU utilization. Since people care about CPU utilization 15355 * while running netperf/ttcp, turn the interrupt frequency back to 15356 * normal/default if polling has not been used in ip_poll_normal_ticks. 15357 */ 15358 if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { 15359 if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { 15360 ip_ring->rr_poll_state &= ~ILL_POLLING; 15361 ip_ring->rr_blank(ip_ring->rr_handle, 15362 ip_ring->rr_normal_blank_time, 15363 ip_ring->rr_normal_pkt_cnt); 15364 } 15365 } 15366 15367 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15368 "ip_input_end: q %p (%S)", q, "end"); 15369 #undef rptr 15370 } 15371 15372 static void 15373 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 15374 t_uscalar_t err) 15375 { 15376 if (dl_err == DL_SYSERR) { 15377 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15378 "%s: %s failed: DL_SYSERR (errno %u)\n", 15379 ill->ill_name, dl_primstr(prim), err); 15380 return; 15381 } 15382 15383 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15384 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim), 15385 dl_errstr(dl_err)); 15386 } 15387 15388 /* 15389 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 15390 * than DL_UNITDATA_IND messages. If we need to process this message 15391 * exclusively, we call qwriter_ip, in which case we also need to call 15392 * ill_refhold before that, since qwriter_ip does an ill_refrele. 15393 */ 15394 void 15395 ip_rput_dlpi(queue_t *q, mblk_t *mp) 15396 { 15397 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15398 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15399 ill_t *ill = (ill_t *)q->q_ptr; 15400 boolean_t pending; 15401 15402 ip1dbg(("ip_rput_dlpi")); 15403 if (dloa->dl_primitive == DL_ERROR_ACK) { 15404 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK %s (0x%x): " 15405 "%s (0x%x), unix %u\n", ill->ill_name, 15406 dl_primstr(dlea->dl_error_primitive), 15407 dlea->dl_error_primitive, 15408 dl_errstr(dlea->dl_errno), 15409 dlea->dl_errno, 15410 dlea->dl_unix_errno)); 15411 } 15412 15413 /* 15414 * If we received an ACK but didn't send a request for it, then it 15415 * can't be part of any pending operation; discard up-front. 15416 */ 15417 switch (dloa->dl_primitive) { 15418 case DL_NOTIFY_IND: 15419 pending = B_TRUE; 15420 break; 15421 case DL_ERROR_ACK: 15422 pending = ill_dlpi_pending(ill, dlea->dl_error_primitive); 15423 break; 15424 case DL_OK_ACK: 15425 pending = ill_dlpi_pending(ill, dloa->dl_correct_primitive); 15426 break; 15427 case DL_INFO_ACK: 15428 pending = ill_dlpi_pending(ill, DL_INFO_REQ); 15429 break; 15430 case DL_BIND_ACK: 15431 pending = ill_dlpi_pending(ill, DL_BIND_REQ); 15432 break; 15433 case DL_PHYS_ADDR_ACK: 15434 pending = ill_dlpi_pending(ill, DL_PHYS_ADDR_REQ); 15435 break; 15436 case DL_NOTIFY_ACK: 15437 pending = ill_dlpi_pending(ill, DL_NOTIFY_REQ); 15438 break; 15439 case DL_CONTROL_ACK: 15440 pending = ill_dlpi_pending(ill, DL_CONTROL_REQ); 15441 break; 15442 case DL_CAPABILITY_ACK: 15443 pending = ill_dlpi_pending(ill, DL_CAPABILITY_REQ); 15444 break; 15445 default: 15446 /* Not a DLPI message we support or were expecting */ 15447 freemsg(mp); 15448 return; 15449 } 15450 15451 if (!pending) { 15452 freemsg(mp); 15453 return; 15454 } 15455 15456 switch (dloa->dl_primitive) { 15457 case DL_ERROR_ACK: 15458 if (dlea->dl_error_primitive == DL_UNBIND_REQ) { 15459 mutex_enter(&ill->ill_lock); 15460 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15461 cv_signal(&ill->ill_cv); 15462 mutex_exit(&ill->ill_lock); 15463 } 15464 break; 15465 15466 case DL_OK_ACK: 15467 ip1dbg(("ip_rput: DL_OK_ACK for %s\n", 15468 dl_primstr((int)dloa->dl_correct_primitive))); 15469 switch (dloa->dl_correct_primitive) { 15470 case DL_UNBIND_REQ: 15471 mutex_enter(&ill->ill_lock); 15472 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15473 cv_signal(&ill->ill_cv); 15474 mutex_exit(&ill->ill_lock); 15475 break; 15476 15477 case DL_ENABMULTI_REQ: 15478 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15479 ill->ill_dlpi_multicast_state = IDS_OK; 15480 break; 15481 } 15482 break; 15483 default: 15484 break; 15485 } 15486 15487 /* 15488 * We know the message is one we're waiting for (or DL_NOTIFY_IND), 15489 * and we need to become writer to continue to process it. If it's not 15490 * a DL_NOTIFY_IND, we assume we're in the middle of an exclusive 15491 * operation and pass CUR_OP. If this isn't true, we'll end up doing 15492 * some work as part of the current exclusive operation that actually 15493 * is not part of it -- which is wrong, but better than the 15494 * alternative of deadlock (if NEW_OP is always used). Someday, we 15495 * should track which DLPI requests have ACKs that we wait on 15496 * synchronously so we can know whether to use CUR_OP or NEW_OP. 15497 * 15498 * As required by qwriter_ip(), we refhold the ill; it will refrele. 15499 * Since this is on the ill stream we unconditionally bump up the 15500 * refcount without doing ILL_CAN_LOOKUP(). 15501 */ 15502 ill_refhold(ill); 15503 if (dloa->dl_primitive == DL_NOTIFY_IND) 15504 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE); 15505 else 15506 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE); 15507 } 15508 15509 /* 15510 * Handling of DLPI messages that require exclusive access to the ipsq. 15511 * 15512 * Need to do ill_pending_mp_release on ioctl completion, which could 15513 * happen here. (along with mi_copy_done) 15514 */ 15515 /* ARGSUSED */ 15516 static void 15517 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15518 { 15519 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15520 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15521 int err = 0; 15522 ill_t *ill; 15523 ipif_t *ipif = NULL; 15524 mblk_t *mp1 = NULL; 15525 conn_t *connp = NULL; 15526 t_uscalar_t paddrreq; 15527 mblk_t *mp_hw; 15528 boolean_t success; 15529 boolean_t ioctl_aborted = B_FALSE; 15530 boolean_t log = B_TRUE; 15531 ip_stack_t *ipst; 15532 15533 ip1dbg(("ip_rput_dlpi_writer ..")); 15534 ill = (ill_t *)q->q_ptr; 15535 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 15536 15537 ASSERT(IAM_WRITER_ILL(ill)); 15538 15539 ipst = ill->ill_ipst; 15540 15541 /* 15542 * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. 15543 * both are null or non-null. However we can assert that only 15544 * after grabbing the ipsq_lock. So we don't make any assertion 15545 * here and in other places in the code. 15546 */ 15547 ipif = ipsq->ipsq_pending_ipif; 15548 /* 15549 * The current ioctl could have been aborted by the user and a new 15550 * ioctl to bring up another ill could have started. We could still 15551 * get a response from the driver later. 15552 */ 15553 if (ipif != NULL && ipif->ipif_ill != ill) 15554 ioctl_aborted = B_TRUE; 15555 15556 switch (dloa->dl_primitive) { 15557 case DL_ERROR_ACK: 15558 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n", 15559 dl_primstr(dlea->dl_error_primitive))); 15560 15561 switch (dlea->dl_error_primitive) { 15562 case DL_PROMISCON_REQ: 15563 case DL_PROMISCOFF_REQ: 15564 case DL_DISABMULTI_REQ: 15565 case DL_UNBIND_REQ: 15566 case DL_ATTACH_REQ: 15567 case DL_INFO_REQ: 15568 ill_dlpi_done(ill, dlea->dl_error_primitive); 15569 break; 15570 case DL_NOTIFY_REQ: 15571 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15572 log = B_FALSE; 15573 break; 15574 case DL_PHYS_ADDR_REQ: 15575 /* 15576 * For IPv6 only, there are two additional 15577 * phys_addr_req's sent to the driver to get the 15578 * IPv6 token and lla. This allows IP to acquire 15579 * the hardware address format for a given interface 15580 * without having built in knowledge of the hardware 15581 * address. ill_phys_addr_pend keeps track of the last 15582 * DL_PAR sent so we know which response we are 15583 * dealing with. ill_dlpi_done will update 15584 * ill_phys_addr_pend when it sends the next req. 15585 * We don't complete the IOCTL until all three DL_PARs 15586 * have been attempted, so set *_len to 0 and break. 15587 */ 15588 paddrreq = ill->ill_phys_addr_pend; 15589 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15590 if (paddrreq == DL_IPV6_TOKEN) { 15591 ill->ill_token_length = 0; 15592 log = B_FALSE; 15593 break; 15594 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 15595 ill->ill_nd_lla_len = 0; 15596 log = B_FALSE; 15597 break; 15598 } 15599 /* 15600 * Something went wrong with the DL_PHYS_ADDR_REQ. 15601 * We presumably have an IOCTL hanging out waiting 15602 * for completion. Find it and complete the IOCTL 15603 * with the error noted. 15604 * However, ill_dl_phys was called on an ill queue 15605 * (from SIOCSLIFNAME), thus conn_pending_ill is not 15606 * set. But the ioctl is known to be pending on ill_wq. 15607 */ 15608 if (!ill->ill_ifname_pending) 15609 break; 15610 ill->ill_ifname_pending = 0; 15611 if (!ioctl_aborted) 15612 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15613 if (mp1 != NULL) { 15614 /* 15615 * This operation (SIOCSLIFNAME) must have 15616 * happened on the ill. Assert there is no conn 15617 */ 15618 ASSERT(connp == NULL); 15619 q = ill->ill_wq; 15620 } 15621 break; 15622 case DL_BIND_REQ: 15623 ill_dlpi_done(ill, DL_BIND_REQ); 15624 if (ill->ill_ifname_pending) 15625 break; 15626 /* 15627 * Something went wrong with the bind. We presumably 15628 * have an IOCTL hanging out waiting for completion. 15629 * Find it, take down the interface that was coming 15630 * up, and complete the IOCTL with the error noted. 15631 */ 15632 if (!ioctl_aborted) 15633 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15634 if (mp1 != NULL) { 15635 /* 15636 * This operation (SIOCSLIFFLAGS) must have 15637 * happened from a conn. 15638 */ 15639 ASSERT(connp != NULL); 15640 q = CONNP_TO_WQ(connp); 15641 if (ill->ill_move_in_progress) { 15642 ILL_CLEAR_MOVE(ill); 15643 } 15644 (void) ipif_down(ipif, NULL, NULL); 15645 /* error is set below the switch */ 15646 } 15647 break; 15648 case DL_ENABMULTI_REQ: 15649 ill_dlpi_done(ill, DL_ENABMULTI_REQ); 15650 15651 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15652 ill->ill_dlpi_multicast_state = IDS_FAILED; 15653 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 15654 ipif_t *ipif; 15655 15656 printf("ip: joining multicasts failed (%d)" 15657 " on %s - will use link layer " 15658 "broadcasts for multicast\n", 15659 dlea->dl_errno, ill->ill_name); 15660 15661 /* 15662 * Set up the multicast mapping alone. 15663 * writer, so ok to access ill->ill_ipif 15664 * without any lock. 15665 */ 15666 ipif = ill->ill_ipif; 15667 mutex_enter(&ill->ill_phyint->phyint_lock); 15668 ill->ill_phyint->phyint_flags |= 15669 PHYI_MULTI_BCAST; 15670 mutex_exit(&ill->ill_phyint->phyint_lock); 15671 15672 if (!ill->ill_isv6) { 15673 (void) ipif_arp_setup_multicast(ipif, 15674 NULL); 15675 } else { 15676 (void) ipif_ndp_setup_multicast(ipif, 15677 NULL); 15678 } 15679 } 15680 freemsg(mp); /* Don't want to pass this up */ 15681 return; 15682 15683 case DL_CAPABILITY_REQ: 15684 case DL_CONTROL_REQ: 15685 ill_dlpi_done(ill, dlea->dl_error_primitive); 15686 ill->ill_dlpi_capab_state = IDS_FAILED; 15687 freemsg(mp); 15688 return; 15689 } 15690 /* 15691 * Note the error for IOCTL completion (mp1 is set when 15692 * ready to complete ioctl). If ill_ifname_pending_err is 15693 * set, an error occured during plumbing (ill_ifname_pending), 15694 * so we want to report that error. 15695 * 15696 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 15697 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 15698 * expected to get errack'd if the driver doesn't support 15699 * these flags (e.g. ethernet). log will be set to B_FALSE 15700 * if these error conditions are encountered. 15701 */ 15702 if (mp1 != NULL) { 15703 if (ill->ill_ifname_pending_err != 0) { 15704 err = ill->ill_ifname_pending_err; 15705 ill->ill_ifname_pending_err = 0; 15706 } else { 15707 err = dlea->dl_unix_errno ? 15708 dlea->dl_unix_errno : ENXIO; 15709 } 15710 /* 15711 * If we're plumbing an interface and an error hasn't already 15712 * been saved, set ill_ifname_pending_err to the error passed 15713 * up. Ignore the error if log is B_FALSE (see comment above). 15714 */ 15715 } else if (log && ill->ill_ifname_pending && 15716 ill->ill_ifname_pending_err == 0) { 15717 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 15718 dlea->dl_unix_errno : ENXIO; 15719 } 15720 15721 if (log) 15722 ip_dlpi_error(ill, dlea->dl_error_primitive, 15723 dlea->dl_errno, dlea->dl_unix_errno); 15724 break; 15725 case DL_CAPABILITY_ACK: 15726 /* Call a routine to handle this one. */ 15727 ill_dlpi_done(ill, DL_CAPABILITY_REQ); 15728 ill_capability_ack(ill, mp); 15729 15730 /* 15731 * If the ack is due to renegotiation, we will need to send 15732 * a new CAPABILITY_REQ to start the renegotiation. 15733 */ 15734 if (ill->ill_capab_reneg) { 15735 ill->ill_capab_reneg = B_FALSE; 15736 ill_capability_probe(ill); 15737 } 15738 break; 15739 case DL_CONTROL_ACK: 15740 /* We treat all of these as "fire and forget" */ 15741 ill_dlpi_done(ill, DL_CONTROL_REQ); 15742 break; 15743 case DL_INFO_ACK: 15744 /* Call a routine to handle this one. */ 15745 ill_dlpi_done(ill, DL_INFO_REQ); 15746 ip_ll_subnet_defaults(ill, mp); 15747 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 15748 return; 15749 case DL_BIND_ACK: 15750 /* 15751 * We should have an IOCTL waiting on this unless 15752 * sent by ill_dl_phys, in which case just return 15753 */ 15754 ill_dlpi_done(ill, DL_BIND_REQ); 15755 if (ill->ill_ifname_pending) 15756 break; 15757 15758 if (!ioctl_aborted) 15759 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15760 if (mp1 == NULL) 15761 break; 15762 /* 15763 * Because mp1 was added by ill_dl_up(), and it always 15764 * passes a valid connp, connp must be valid here. 15765 */ 15766 ASSERT(connp != NULL); 15767 q = CONNP_TO_WQ(connp); 15768 15769 /* 15770 * We are exclusive. So nothing can change even after 15771 * we get the pending mp. If need be we can put it back 15772 * and restart, as in calling ipif_arp_up() below. 15773 */ 15774 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 15775 15776 mutex_enter(&ill->ill_lock); 15777 ill->ill_dl_up = 1; 15778 (void) ill_hook_event_create(ill, 0, NE_UP, NULL, 0); 15779 mutex_exit(&ill->ill_lock); 15780 15781 /* 15782 * Now bring up the resolver; when that is complete, we'll 15783 * create IREs. Note that we intentionally mirror what 15784 * ipif_up() would have done, because we got here by way of 15785 * ill_dl_up(), which stopped ipif_up()'s processing. 15786 */ 15787 if (ill->ill_isv6) { 15788 /* 15789 * v6 interfaces. 15790 * Unlike ARP which has to do another bind 15791 * and attach, once we get here we are 15792 * done with NDP. Except in the case of 15793 * ILLF_XRESOLV, in which case we send an 15794 * AR_INTERFACE_UP to the external resolver. 15795 * If all goes well, the ioctl will complete 15796 * in ip_rput(). If there's an error, we 15797 * complete it here. 15798 */ 15799 if ((err = ipif_ndp_up(ipif)) == 0) { 15800 if (ill->ill_flags & ILLF_XRESOLV) { 15801 mutex_enter(&connp->conn_lock); 15802 mutex_enter(&ill->ill_lock); 15803 success = ipsq_pending_mp_add( 15804 connp, ipif, q, mp1, 0); 15805 mutex_exit(&ill->ill_lock); 15806 mutex_exit(&connp->conn_lock); 15807 if (success) { 15808 err = ipif_resolver_up(ipif, 15809 Res_act_initial); 15810 if (err == EINPROGRESS) { 15811 freemsg(mp); 15812 return; 15813 } 15814 ASSERT(err != 0); 15815 mp1 = ipsq_pending_mp_get(ipsq, 15816 &connp); 15817 ASSERT(mp1 != NULL); 15818 } else { 15819 /* conn has started closing */ 15820 err = EINTR; 15821 } 15822 } else { /* Non XRESOLV interface */ 15823 (void) ipif_resolver_up(ipif, 15824 Res_act_initial); 15825 err = ipif_up_done_v6(ipif); 15826 } 15827 } 15828 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 15829 /* 15830 * ARP and other v4 external resolvers. 15831 * Leave the pending mblk intact so that 15832 * the ioctl completes in ip_rput(). 15833 */ 15834 mutex_enter(&connp->conn_lock); 15835 mutex_enter(&ill->ill_lock); 15836 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 15837 mutex_exit(&ill->ill_lock); 15838 mutex_exit(&connp->conn_lock); 15839 if (success) { 15840 err = ipif_resolver_up(ipif, Res_act_initial); 15841 if (err == EINPROGRESS) { 15842 freemsg(mp); 15843 return; 15844 } 15845 ASSERT(err != 0); 15846 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15847 } else { 15848 /* The conn has started closing */ 15849 err = EINTR; 15850 } 15851 } else { 15852 /* 15853 * This one is complete. Reply to pending ioctl. 15854 */ 15855 (void) ipif_resolver_up(ipif, Res_act_initial); 15856 err = ipif_up_done(ipif); 15857 } 15858 15859 if ((err == 0) && (ill->ill_up_ipifs)) { 15860 err = ill_up_ipifs(ill, q, mp1); 15861 if (err == EINPROGRESS) { 15862 freemsg(mp); 15863 return; 15864 } 15865 } 15866 15867 if (ill->ill_up_ipifs) { 15868 ill_group_cleanup(ill); 15869 } 15870 15871 break; 15872 case DL_NOTIFY_IND: { 15873 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 15874 ire_t *ire; 15875 boolean_t need_ire_walk_v4 = B_FALSE; 15876 boolean_t need_ire_walk_v6 = B_FALSE; 15877 15878 switch (notify->dl_notification) { 15879 case DL_NOTE_PHYS_ADDR: 15880 err = ill_set_phys_addr(ill, mp); 15881 break; 15882 15883 case DL_NOTE_FASTPATH_FLUSH: 15884 ill_fastpath_flush(ill); 15885 break; 15886 15887 case DL_NOTE_SDU_SIZE: 15888 /* 15889 * Change the MTU size of the interface, of all 15890 * attached ipif's, and of all relevant ire's. The 15891 * new value's a uint32_t at notify->dl_data. 15892 * Mtu change Vs. new ire creation - protocol below. 15893 * 15894 * a Mark the ipif as IPIF_CHANGING. 15895 * b Set the new mtu in the ipif. 15896 * c Change the ire_max_frag on all affected ires 15897 * d Unmark the IPIF_CHANGING 15898 * 15899 * To see how the protocol works, assume an interface 15900 * route is also being added simultaneously by 15901 * ip_rt_add and let 'ipif' be the ipif referenced by 15902 * the ire. If the ire is created before step a, 15903 * it will be cleaned up by step c. If the ire is 15904 * created after step d, it will see the new value of 15905 * ipif_mtu. Any attempt to create the ire between 15906 * steps a to d will fail because of the IPIF_CHANGING 15907 * flag. Note that ire_create() is passed a pointer to 15908 * the ipif_mtu, and not the value. During ire_add 15909 * under the bucket lock, the ire_max_frag of the 15910 * new ire being created is set from the ipif/ire from 15911 * which it is being derived. 15912 */ 15913 mutex_enter(&ill->ill_lock); 15914 ill->ill_max_frag = (uint_t)notify->dl_data; 15915 15916 /* 15917 * If an SIOCSLIFLNKINFO has changed the ill_max_mtu 15918 * leave it alone 15919 */ 15920 if (ill->ill_mtu_userspecified) { 15921 mutex_exit(&ill->ill_lock); 15922 break; 15923 } 15924 ill->ill_max_mtu = ill->ill_max_frag; 15925 if (ill->ill_isv6) { 15926 if (ill->ill_max_mtu < IPV6_MIN_MTU) 15927 ill->ill_max_mtu = IPV6_MIN_MTU; 15928 } else { 15929 if (ill->ill_max_mtu < IP_MIN_MTU) 15930 ill->ill_max_mtu = IP_MIN_MTU; 15931 } 15932 for (ipif = ill->ill_ipif; ipif != NULL; 15933 ipif = ipif->ipif_next) { 15934 /* 15935 * Don't override the mtu if the user 15936 * has explicitly set it. 15937 */ 15938 if (ipif->ipif_flags & IPIF_FIXEDMTU) 15939 continue; 15940 ipif->ipif_mtu = (uint_t)notify->dl_data; 15941 if (ipif->ipif_isv6) 15942 ire = ipif_to_ire_v6(ipif); 15943 else 15944 ire = ipif_to_ire(ipif); 15945 if (ire != NULL) { 15946 ire->ire_max_frag = ipif->ipif_mtu; 15947 ire_refrele(ire); 15948 } 15949 if (ipif->ipif_flags & IPIF_UP) { 15950 if (ill->ill_isv6) 15951 need_ire_walk_v6 = B_TRUE; 15952 else 15953 need_ire_walk_v4 = B_TRUE; 15954 } 15955 } 15956 mutex_exit(&ill->ill_lock); 15957 if (need_ire_walk_v4) 15958 ire_walk_v4(ill_mtu_change, (char *)ill, 15959 ALL_ZONES, ipst); 15960 if (need_ire_walk_v6) 15961 ire_walk_v6(ill_mtu_change, (char *)ill, 15962 ALL_ZONES, ipst); 15963 break; 15964 case DL_NOTE_LINK_UP: 15965 case DL_NOTE_LINK_DOWN: { 15966 /* 15967 * We are writer. ill / phyint / ipsq assocs stable. 15968 * The RUNNING flag reflects the state of the link. 15969 */ 15970 phyint_t *phyint = ill->ill_phyint; 15971 uint64_t new_phyint_flags; 15972 boolean_t changed = B_FALSE; 15973 boolean_t went_up; 15974 15975 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 15976 mutex_enter(&phyint->phyint_lock); 15977 new_phyint_flags = went_up ? 15978 phyint->phyint_flags | PHYI_RUNNING : 15979 phyint->phyint_flags & ~PHYI_RUNNING; 15980 if (new_phyint_flags != phyint->phyint_flags) { 15981 phyint->phyint_flags = new_phyint_flags; 15982 changed = B_TRUE; 15983 } 15984 mutex_exit(&phyint->phyint_lock); 15985 /* 15986 * ill_restart_dad handles the DAD restart and routing 15987 * socket notification logic. 15988 */ 15989 if (changed) { 15990 ill_restart_dad(phyint->phyint_illv4, went_up); 15991 ill_restart_dad(phyint->phyint_illv6, went_up); 15992 } 15993 break; 15994 } 15995 case DL_NOTE_PROMISC_ON_PHYS: 15996 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 15997 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 15998 mutex_enter(&ill->ill_lock); 15999 ill->ill_promisc_on_phys = B_TRUE; 16000 mutex_exit(&ill->ill_lock); 16001 break; 16002 case DL_NOTE_PROMISC_OFF_PHYS: 16003 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 16004 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 16005 mutex_enter(&ill->ill_lock); 16006 ill->ill_promisc_on_phys = B_FALSE; 16007 mutex_exit(&ill->ill_lock); 16008 break; 16009 case DL_NOTE_CAPAB_RENEG: 16010 /* 16011 * Something changed on the driver side. 16012 * It wants us to renegotiate the capabilities 16013 * on this ill. One possible cause is the aggregation 16014 * interface under us where a port got added or 16015 * went away. 16016 * 16017 * If the capability negotiation is already done 16018 * or is in progress, reset the capabilities and 16019 * mark the ill's ill_capab_reneg to be B_TRUE, 16020 * so that when the ack comes back, we can start 16021 * the renegotiation process. 16022 * 16023 * Note that if ill_capab_reneg is already B_TRUE 16024 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case), 16025 * the capability resetting request has been sent 16026 * and the renegotiation has not been started yet; 16027 * nothing needs to be done in this case. 16028 */ 16029 if (ill->ill_dlpi_capab_state != IDS_UNKNOWN) { 16030 ill_capability_reset(ill); 16031 ill->ill_capab_reneg = B_TRUE; 16032 } 16033 break; 16034 default: 16035 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 16036 "type 0x%x for DL_NOTIFY_IND\n", 16037 notify->dl_notification)); 16038 break; 16039 } 16040 16041 /* 16042 * As this is an asynchronous operation, we 16043 * should not call ill_dlpi_done 16044 */ 16045 break; 16046 } 16047 case DL_NOTIFY_ACK: { 16048 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 16049 16050 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 16051 ill->ill_note_link = 1; 16052 ill_dlpi_done(ill, DL_NOTIFY_REQ); 16053 break; 16054 } 16055 case DL_PHYS_ADDR_ACK: { 16056 /* 16057 * As part of plumbing the interface via SIOCSLIFNAME, 16058 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, 16059 * whose answers we receive here. As each answer is received, 16060 * we call ill_dlpi_done() to dispatch the next request as 16061 * we're processing the current one. Once all answers have 16062 * been received, we use ipsq_pending_mp_get() to dequeue the 16063 * outstanding IOCTL and reply to it. (Because ill_dl_phys() 16064 * is invoked from an ill queue, conn_oper_pending_ill is not 16065 * available, but we know the ioctl is pending on ill_wq.) 16066 */ 16067 uint_t paddrlen, paddroff; 16068 16069 paddrreq = ill->ill_phys_addr_pend; 16070 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; 16071 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; 16072 16073 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 16074 if (paddrreq == DL_IPV6_TOKEN) { 16075 /* 16076 * bcopy to low-order bits of ill_token 16077 * 16078 * XXX Temporary hack - currently, all known tokens 16079 * are 64 bits, so I'll cheat for the moment. 16080 */ 16081 bcopy(mp->b_rptr + paddroff, 16082 &ill->ill_token.s6_addr32[2], paddrlen); 16083 ill->ill_token_length = paddrlen; 16084 break; 16085 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 16086 ASSERT(ill->ill_nd_lla_mp == NULL); 16087 ill_set_ndmp(ill, mp, paddroff, paddrlen); 16088 mp = NULL; 16089 break; 16090 } 16091 16092 ASSERT(paddrreq == DL_CURR_PHYS_ADDR); 16093 ASSERT(ill->ill_phys_addr_mp == NULL); 16094 if (!ill->ill_ifname_pending) 16095 break; 16096 ill->ill_ifname_pending = 0; 16097 if (!ioctl_aborted) 16098 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16099 if (mp1 != NULL) { 16100 ASSERT(connp == NULL); 16101 q = ill->ill_wq; 16102 } 16103 /* 16104 * If any error acks received during the plumbing sequence, 16105 * ill_ifname_pending_err will be set. Break out and send up 16106 * the error to the pending ioctl. 16107 */ 16108 if (ill->ill_ifname_pending_err != 0) { 16109 err = ill->ill_ifname_pending_err; 16110 ill->ill_ifname_pending_err = 0; 16111 break; 16112 } 16113 16114 ill->ill_phys_addr_mp = mp; 16115 ill->ill_phys_addr = mp->b_rptr + paddroff; 16116 mp = NULL; 16117 16118 /* 16119 * If paddrlen is zero, the DLPI provider doesn't support 16120 * physical addresses. The other two tests were historical 16121 * workarounds for bugs in our former PPP implementation, but 16122 * now other things have grown dependencies on them -- e.g., 16123 * the tun module specifies a dl_addr_length of zero in its 16124 * DL_BIND_ACK, but then specifies an incorrect value in its 16125 * DL_PHYS_ADDR_ACK. These bogus checks need to be removed, 16126 * but only after careful testing ensures that all dependent 16127 * broken DLPI providers have been fixed. 16128 */ 16129 if (paddrlen == 0 || ill->ill_phys_addr_length == 0 || 16130 ill->ill_phys_addr_length == IP_ADDR_LEN) { 16131 ill->ill_phys_addr = NULL; 16132 } else if (paddrlen != ill->ill_phys_addr_length) { 16133 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", 16134 paddrlen, ill->ill_phys_addr_length)); 16135 err = EINVAL; 16136 break; 16137 } 16138 16139 if (ill->ill_nd_lla_mp == NULL) { 16140 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { 16141 err = ENOMEM; 16142 break; 16143 } 16144 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); 16145 } 16146 16147 /* 16148 * Set the interface token. If the zeroth interface address 16149 * is unspecified, then set it to the link local address. 16150 */ 16151 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 16152 (void) ill_setdefaulttoken(ill); 16153 16154 ASSERT(ill->ill_ipif->ipif_id == 0); 16155 if (ipif != NULL && 16156 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { 16157 (void) ipif_setlinklocal(ipif); 16158 } 16159 break; 16160 } 16161 case DL_OK_ACK: 16162 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 16163 dl_primstr((int)dloa->dl_correct_primitive), 16164 dloa->dl_correct_primitive)); 16165 switch (dloa->dl_correct_primitive) { 16166 case DL_PROMISCON_REQ: 16167 case DL_PROMISCOFF_REQ: 16168 case DL_ENABMULTI_REQ: 16169 case DL_DISABMULTI_REQ: 16170 case DL_UNBIND_REQ: 16171 case DL_ATTACH_REQ: 16172 ill_dlpi_done(ill, dloa->dl_correct_primitive); 16173 break; 16174 } 16175 break; 16176 default: 16177 break; 16178 } 16179 16180 freemsg(mp); 16181 if (mp1 != NULL) { 16182 /* 16183 * The operation must complete without EINPROGRESS 16184 * since ipsq_pending_mp_get() has removed the mblk 16185 * from ipsq_pending_mp. Otherwise, the operation 16186 * will be stuck forever in the ipsq. 16187 */ 16188 ASSERT(err != EINPROGRESS); 16189 16190 switch (ipsq->ipsq_current_ioctl) { 16191 case 0: 16192 ipsq_current_finish(ipsq); 16193 break; 16194 16195 case SIOCLIFADDIF: 16196 case SIOCSLIFNAME: 16197 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 16198 break; 16199 16200 default: 16201 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 16202 break; 16203 } 16204 } 16205 } 16206 16207 /* 16208 * ip_rput_other is called by ip_rput to handle messages modifying the global 16209 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 16210 */ 16211 /* ARGSUSED */ 16212 void 16213 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 16214 { 16215 ill_t *ill; 16216 struct iocblk *iocp; 16217 mblk_t *mp1; 16218 conn_t *connp = NULL; 16219 16220 ip1dbg(("ip_rput_other ")); 16221 ill = (ill_t *)q->q_ptr; 16222 /* 16223 * This routine is not a writer in the case of SIOCGTUNPARAM 16224 * in which case ipsq is NULL. 16225 */ 16226 if (ipsq != NULL) { 16227 ASSERT(IAM_WRITER_IPSQ(ipsq)); 16228 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 16229 } 16230 16231 switch (mp->b_datap->db_type) { 16232 case M_ERROR: 16233 case M_HANGUP: 16234 /* 16235 * The device has a problem. We force the ILL down. It can 16236 * be brought up again manually using SIOCSIFFLAGS (via 16237 * ifconfig or equivalent). 16238 */ 16239 ASSERT(ipsq != NULL); 16240 if (mp->b_rptr < mp->b_wptr) 16241 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 16242 if (ill->ill_error == 0) 16243 ill->ill_error = ENXIO; 16244 if (!ill_down_start(q, mp)) 16245 return; 16246 ipif_all_down_tail(ipsq, q, mp, NULL); 16247 break; 16248 case M_IOCACK: 16249 iocp = (struct iocblk *)mp->b_rptr; 16250 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 16251 switch (iocp->ioc_cmd) { 16252 case SIOCSTUNPARAM: 16253 case OSIOCSTUNPARAM: 16254 ASSERT(ipsq != NULL); 16255 /* 16256 * Finish socket ioctl passed through to tun. 16257 * We should have an IOCTL waiting on this. 16258 */ 16259 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16260 if (ill->ill_isv6) { 16261 struct iftun_req *ta; 16262 16263 /* 16264 * if a source or destination is 16265 * being set, try and set the link 16266 * local address for the tunnel 16267 */ 16268 ta = (struct iftun_req *)mp->b_cont-> 16269 b_cont->b_rptr; 16270 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 16271 ipif_set_tun_llink(ill, ta); 16272 } 16273 16274 } 16275 if (mp1 != NULL) { 16276 /* 16277 * Now copy back the b_next/b_prev used by 16278 * mi code for the mi_copy* functions. 16279 * See ip_sioctl_tunparam() for the reason. 16280 * Also protect against missing b_cont. 16281 */ 16282 if (mp->b_cont != NULL) { 16283 mp->b_cont->b_next = 16284 mp1->b_cont->b_next; 16285 mp->b_cont->b_prev = 16286 mp1->b_cont->b_prev; 16287 } 16288 inet_freemsg(mp1); 16289 ASSERT(connp != NULL); 16290 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16291 iocp->ioc_error, NO_COPYOUT, ipsq); 16292 } else { 16293 ASSERT(connp == NULL); 16294 putnext(q, mp); 16295 } 16296 break; 16297 case SIOCGTUNPARAM: 16298 case OSIOCGTUNPARAM: 16299 /* 16300 * This is really M_IOCDATA from the tunnel driver. 16301 * convert back and complete the ioctl. 16302 * We should have an IOCTL waiting on this. 16303 */ 16304 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 16305 if (mp1) { 16306 /* 16307 * Now copy back the b_next/b_prev used by 16308 * mi code for the mi_copy* functions. 16309 * See ip_sioctl_tunparam() for the reason. 16310 * Also protect against missing b_cont. 16311 */ 16312 if (mp->b_cont != NULL) { 16313 mp->b_cont->b_next = 16314 mp1->b_cont->b_next; 16315 mp->b_cont->b_prev = 16316 mp1->b_cont->b_prev; 16317 } 16318 inet_freemsg(mp1); 16319 if (iocp->ioc_error == 0) 16320 mp->b_datap->db_type = M_IOCDATA; 16321 ASSERT(connp != NULL); 16322 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16323 iocp->ioc_error, COPYOUT, NULL); 16324 } else { 16325 ASSERT(connp == NULL); 16326 putnext(q, mp); 16327 } 16328 break; 16329 default: 16330 break; 16331 } 16332 break; 16333 case M_IOCNAK: 16334 iocp = (struct iocblk *)mp->b_rptr; 16335 16336 switch (iocp->ioc_cmd) { 16337 int mode; 16338 16339 case DL_IOC_HDR_INFO: 16340 /* 16341 * If this was the first attempt turn of the 16342 * fastpath probing. 16343 */ 16344 mutex_enter(&ill->ill_lock); 16345 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 16346 ill->ill_dlpi_fastpath_state = IDS_FAILED; 16347 mutex_exit(&ill->ill_lock); 16348 ill_fastpath_nack(ill); 16349 ip1dbg(("ip_rput: DLPI fastpath off on " 16350 "interface %s\n", 16351 ill->ill_name)); 16352 } else { 16353 mutex_exit(&ill->ill_lock); 16354 } 16355 freemsg(mp); 16356 break; 16357 case SIOCSTUNPARAM: 16358 case OSIOCSTUNPARAM: 16359 ASSERT(ipsq != NULL); 16360 /* 16361 * Finish socket ioctl passed through to tun 16362 * We should have an IOCTL waiting on this. 16363 */ 16364 /* FALLTHRU */ 16365 case SIOCGTUNPARAM: 16366 case OSIOCGTUNPARAM: 16367 /* 16368 * This is really M_IOCDATA from the tunnel driver. 16369 * convert back and complete the ioctl. 16370 * We should have an IOCTL waiting on this. 16371 */ 16372 if (iocp->ioc_cmd == SIOCGTUNPARAM || 16373 iocp->ioc_cmd == OSIOCGTUNPARAM) { 16374 mp1 = ill_pending_mp_get(ill, &connp, 16375 iocp->ioc_id); 16376 mode = COPYOUT; 16377 ipsq = NULL; 16378 } else { 16379 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16380 mode = NO_COPYOUT; 16381 } 16382 if (mp1 != NULL) { 16383 /* 16384 * Now copy back the b_next/b_prev used by 16385 * mi code for the mi_copy* functions. 16386 * See ip_sioctl_tunparam() for the reason. 16387 * Also protect against missing b_cont. 16388 */ 16389 if (mp->b_cont != NULL) { 16390 mp->b_cont->b_next = 16391 mp1->b_cont->b_next; 16392 mp->b_cont->b_prev = 16393 mp1->b_cont->b_prev; 16394 } 16395 inet_freemsg(mp1); 16396 if (iocp->ioc_error == 0) 16397 iocp->ioc_error = EINVAL; 16398 ASSERT(connp != NULL); 16399 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16400 iocp->ioc_error, mode, ipsq); 16401 } else { 16402 ASSERT(connp == NULL); 16403 putnext(q, mp); 16404 } 16405 break; 16406 default: 16407 break; 16408 } 16409 default: 16410 break; 16411 } 16412 } 16413 16414 /* 16415 * NOTE : This function does not ire_refrele the ire argument passed in. 16416 * 16417 * IPQoS notes 16418 * IP policy is invoked twice for a forwarded packet, once on the read side 16419 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 16420 * enabled. An additional parameter, in_ill, has been added for this purpose. 16421 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 16422 * because ip_mroute drops this information. 16423 * 16424 */ 16425 void 16426 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 16427 { 16428 uint32_t old_pkt_len; 16429 uint32_t pkt_len; 16430 queue_t *q; 16431 uint32_t sum; 16432 #define rptr ((uchar_t *)ipha) 16433 uint32_t max_frag; 16434 uint32_t ill_index; 16435 ill_t *out_ill; 16436 mib2_ipIfStatsEntry_t *mibptr; 16437 ip_stack_t *ipst = ((ill_t *)(ire->ire_stq->q_ptr))->ill_ipst; 16438 16439 /* Get the ill_index of the incoming ILL */ 16440 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 16441 mibptr = (in_ill != NULL) ? in_ill->ill_ip_mib : &ipst->ips_ip_mib; 16442 16443 /* Initiate Read side IPPF processing */ 16444 if (IPP_ENABLED(IPP_FWD_IN, ipst)) { 16445 ip_process(IPP_FWD_IN, &mp, ill_index); 16446 if (mp == NULL) { 16447 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 16448 "during IPPF processing\n")); 16449 return; 16450 } 16451 } 16452 16453 /* Adjust the checksum to reflect the ttl decrement. */ 16454 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 16455 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 16456 16457 if (ipha->ipha_ttl-- <= 1) { 16458 if (ip_csum_hdr(ipha)) { 16459 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16460 goto drop_pkt; 16461 } 16462 /* 16463 * Note: ire_stq this will be NULL for multicast 16464 * datagrams using the long path through arp (the IRE 16465 * is not an IRE_CACHE). This should not cause 16466 * problems since we don't generate ICMP errors for 16467 * multicast packets. 16468 */ 16469 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16470 q = ire->ire_stq; 16471 if (q != NULL) { 16472 /* Sent by forwarding path, and router is global zone */ 16473 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, 16474 GLOBAL_ZONEID, ipst); 16475 } else 16476 freemsg(mp); 16477 return; 16478 } 16479 16480 /* 16481 * Don't forward if the interface is down 16482 */ 16483 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 16484 BUMP_MIB(mibptr, ipIfStatsInDiscards); 16485 ip2dbg(("ip_rput_forward:interface is down\n")); 16486 goto drop_pkt; 16487 } 16488 16489 /* Get the ill_index of the outgoing ILL */ 16490 out_ill = ire_to_ill(ire); 16491 ill_index = out_ill->ill_phyint->phyint_ifindex; 16492 16493 DTRACE_PROBE4(ip4__forwarding__start, 16494 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16495 16496 FW_HOOKS(ipst->ips_ip4_forwarding_event, 16497 ipst->ips_ipv4firewall_forwarding, 16498 in_ill, out_ill, ipha, mp, mp, 0, ipst); 16499 16500 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 16501 16502 if (mp == NULL) 16503 return; 16504 old_pkt_len = pkt_len = ntohs(ipha->ipha_length); 16505 16506 if (is_system_labeled()) { 16507 mblk_t *mp1; 16508 16509 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 16510 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16511 goto drop_pkt; 16512 } 16513 /* Size may have changed */ 16514 mp = mp1; 16515 ipha = (ipha_t *)mp->b_rptr; 16516 pkt_len = ntohs(ipha->ipha_length); 16517 } 16518 16519 /* Check if there are options to update */ 16520 if (!IS_SIMPLE_IPH(ipha)) { 16521 if (ip_csum_hdr(ipha)) { 16522 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16523 goto drop_pkt; 16524 } 16525 if (ip_rput_forward_options(mp, ipha, ire, ipst)) { 16526 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16527 return; 16528 } 16529 16530 ipha->ipha_hdr_checksum = 0; 16531 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 16532 } 16533 max_frag = ire->ire_max_frag; 16534 if (pkt_len > max_frag) { 16535 /* 16536 * It needs fragging on its way out. We haven't 16537 * verified the header checksum yet. Since we 16538 * are going to put a surely good checksum in the 16539 * outgoing header, we have to make sure that it 16540 * was good coming in. 16541 */ 16542 if (ip_csum_hdr(ipha)) { 16543 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16544 goto drop_pkt; 16545 } 16546 /* Initiate Write side IPPF processing */ 16547 if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { 16548 ip_process(IPP_FWD_OUT, &mp, ill_index); 16549 if (mp == NULL) { 16550 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 16551 " during IPPF processing\n")); 16552 return; 16553 } 16554 } 16555 /* 16556 * Handle labeled packet resizing. 16557 * 16558 * If we have added a label, inform ip_wput_frag() of its 16559 * effect on the MTU for ICMP messages. 16560 */ 16561 if (pkt_len > old_pkt_len) { 16562 uint32_t secopt_size; 16563 16564 secopt_size = pkt_len - old_pkt_len; 16565 if (secopt_size < max_frag) 16566 max_frag -= secopt_size; 16567 } 16568 16569 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID, ipst); 16570 ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); 16571 return; 16572 } 16573 16574 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 16575 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16576 FW_HOOKS(ipst->ips_ip4_physical_out_event, 16577 ipst->ips_ipv4firewall_physical_out, 16578 NULL, out_ill, ipha, mp, mp, 0, ipst); 16579 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 16580 if (mp == NULL) 16581 return; 16582 16583 mp->b_prev = (mblk_t *)IPP_FWD_OUT; 16584 ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); 16585 (void) ip_xmit_v4(mp, ire, NULL, B_FALSE); 16586 /* ip_xmit_v4 always consumes the packet */ 16587 return; 16588 16589 drop_pkt:; 16590 ip1dbg(("ip_rput_forward: drop pkt\n")); 16591 freemsg(mp); 16592 #undef rptr 16593 } 16594 16595 void 16596 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 16597 { 16598 ire_t *ire; 16599 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 16600 16601 ASSERT(!ipif->ipif_isv6); 16602 /* 16603 * Find an IRE which matches the destination and the outgoing 16604 * queue in the cache table. All we need is an IRE_CACHE which 16605 * is pointing at ipif->ipif_ill. If it is part of some ill group, 16606 * then it is enough to have some IRE_CACHE in the group. 16607 */ 16608 if (ipif->ipif_flags & IPIF_POINTOPOINT) 16609 dst = ipif->ipif_pp_dst_addr; 16610 16611 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MBLK_GETLABEL(mp), 16612 MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR, ipst); 16613 if (ire == NULL) { 16614 /* 16615 * Mark this packet to make it be delivered to 16616 * ip_rput_forward after the new ire has been 16617 * created. 16618 */ 16619 mp->b_prev = NULL; 16620 mp->b_next = mp; 16621 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 16622 NULL, 0, GLOBAL_ZONEID, &zero_info); 16623 } else { 16624 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 16625 IRE_REFRELE(ire); 16626 } 16627 } 16628 16629 /* Update any source route, record route or timestamp options */ 16630 static int 16631 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) 16632 { 16633 ipoptp_t opts; 16634 uchar_t *opt; 16635 uint8_t optval; 16636 uint8_t optlen; 16637 ipaddr_t dst; 16638 uint32_t ts; 16639 ire_t *dst_ire = NULL; 16640 ire_t *tmp_ire = NULL; 16641 timestruc_t now; 16642 16643 ip2dbg(("ip_rput_forward_options\n")); 16644 dst = ipha->ipha_dst; 16645 for (optval = ipoptp_first(&opts, ipha); 16646 optval != IPOPT_EOL; 16647 optval = ipoptp_next(&opts)) { 16648 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 16649 opt = opts.ipoptp_cur; 16650 optlen = opts.ipoptp_len; 16651 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 16652 optval, opts.ipoptp_len)); 16653 switch (optval) { 16654 uint32_t off; 16655 case IPOPT_SSRR: 16656 case IPOPT_LSRR: 16657 /* Check if adminstratively disabled */ 16658 if (!ipst->ips_ip_forward_src_routed) { 16659 if (ire->ire_stq != NULL) { 16660 /* 16661 * Sent by forwarding path, and router 16662 * is global zone 16663 */ 16664 icmp_unreachable(ire->ire_stq, mp, 16665 ICMP_SOURCE_ROUTE_FAILED, 16666 GLOBAL_ZONEID, ipst); 16667 } else { 16668 ip0dbg(("ip_rput_forward_options: " 16669 "unable to send unreach\n")); 16670 freemsg(mp); 16671 } 16672 return (-1); 16673 } 16674 16675 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16676 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16677 if (dst_ire == NULL) { 16678 /* 16679 * Must be partial since ip_rput_options 16680 * checked for strict. 16681 */ 16682 break; 16683 } 16684 off = opt[IPOPT_OFFSET]; 16685 off--; 16686 redo_srr: 16687 if (optlen < IP_ADDR_LEN || 16688 off > optlen - IP_ADDR_LEN) { 16689 /* End of source route */ 16690 ip1dbg(( 16691 "ip_rput_forward_options: end of SR\n")); 16692 ire_refrele(dst_ire); 16693 break; 16694 } 16695 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16696 bcopy(&ire->ire_src_addr, (char *)opt + off, 16697 IP_ADDR_LEN); 16698 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 16699 ntohl(dst))); 16700 16701 /* 16702 * Check if our address is present more than 16703 * once as consecutive hops in source route. 16704 */ 16705 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16706 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16707 if (tmp_ire != NULL) { 16708 ire_refrele(tmp_ire); 16709 off += IP_ADDR_LEN; 16710 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16711 goto redo_srr; 16712 } 16713 ipha->ipha_dst = dst; 16714 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16715 ire_refrele(dst_ire); 16716 break; 16717 case IPOPT_RR: 16718 off = opt[IPOPT_OFFSET]; 16719 off--; 16720 if (optlen < IP_ADDR_LEN || 16721 off > optlen - IP_ADDR_LEN) { 16722 /* No more room - ignore */ 16723 ip1dbg(( 16724 "ip_rput_forward_options: end of RR\n")); 16725 break; 16726 } 16727 bcopy(&ire->ire_src_addr, (char *)opt + off, 16728 IP_ADDR_LEN); 16729 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16730 break; 16731 case IPOPT_TS: 16732 /* Insert timestamp if there is room */ 16733 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16734 case IPOPT_TS_TSONLY: 16735 off = IPOPT_TS_TIMELEN; 16736 break; 16737 case IPOPT_TS_PRESPEC: 16738 case IPOPT_TS_PRESPEC_RFC791: 16739 /* Verify that the address matched */ 16740 off = opt[IPOPT_OFFSET] - 1; 16741 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16742 dst_ire = ire_ctable_lookup(dst, 0, 16743 IRE_LOCAL, NULL, ALL_ZONES, NULL, 16744 MATCH_IRE_TYPE, ipst); 16745 if (dst_ire == NULL) { 16746 /* Not for us */ 16747 break; 16748 } 16749 ire_refrele(dst_ire); 16750 /* FALLTHRU */ 16751 case IPOPT_TS_TSANDADDR: 16752 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16753 break; 16754 default: 16755 /* 16756 * ip_*put_options should have already 16757 * dropped this packet. 16758 */ 16759 cmn_err(CE_PANIC, "ip_rput_forward_options: " 16760 "unknown IT - bug in ip_rput_options?\n"); 16761 return (0); /* Keep "lint" happy */ 16762 } 16763 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 16764 /* Increase overflow counter */ 16765 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 16766 opt[IPOPT_POS_OV_FLG] = 16767 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 16768 (off << 4)); 16769 break; 16770 } 16771 off = opt[IPOPT_OFFSET] - 1; 16772 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16773 case IPOPT_TS_PRESPEC: 16774 case IPOPT_TS_PRESPEC_RFC791: 16775 case IPOPT_TS_TSANDADDR: 16776 bcopy(&ire->ire_src_addr, 16777 (char *)opt + off, IP_ADDR_LEN); 16778 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16779 /* FALLTHRU */ 16780 case IPOPT_TS_TSONLY: 16781 off = opt[IPOPT_OFFSET] - 1; 16782 /* Compute # of milliseconds since midnight */ 16783 gethrestime(&now); 16784 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 16785 now.tv_nsec / (NANOSEC / MILLISEC); 16786 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 16787 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 16788 break; 16789 } 16790 break; 16791 } 16792 } 16793 return (0); 16794 } 16795 16796 /* 16797 * This is called after processing at least one of AH/ESP headers. 16798 * 16799 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 16800 * the actual, physical interface on which the packet was received, 16801 * but, when ip_strict_dst_multihoming is set to 1, could be the 16802 * interface which had the ipha_dst configured when the packet went 16803 * through ip_rput. The ill_index corresponding to the recv_ill 16804 * is saved in ipsec_in_rill_index 16805 * 16806 * NOTE2: The "ire" argument is only used in IPv4 cases. This function 16807 * cannot assume "ire" points to valid data for any IPv6 cases. 16808 */ 16809 void 16810 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 16811 { 16812 mblk_t *mp; 16813 ipaddr_t dst; 16814 in6_addr_t *v6dstp; 16815 ipha_t *ipha; 16816 ip6_t *ip6h; 16817 ipsec_in_t *ii; 16818 boolean_t ill_need_rele = B_FALSE; 16819 boolean_t rill_need_rele = B_FALSE; 16820 boolean_t ire_need_rele = B_FALSE; 16821 netstack_t *ns; 16822 ip_stack_t *ipst; 16823 16824 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 16825 ASSERT(ii->ipsec_in_ill_index != 0); 16826 ns = ii->ipsec_in_ns; 16827 ASSERT(ii->ipsec_in_ns != NULL); 16828 ipst = ns->netstack_ip; 16829 16830 mp = ipsec_mp->b_cont; 16831 ASSERT(mp != NULL); 16832 16833 16834 if (ill == NULL) { 16835 ASSERT(recv_ill == NULL); 16836 /* 16837 * We need to get the original queue on which ip_rput_local 16838 * or ip_rput_data_v6 was called. 16839 */ 16840 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 16841 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL, ipst); 16842 ill_need_rele = B_TRUE; 16843 16844 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 16845 recv_ill = ill_lookup_on_ifindex( 16846 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 16847 NULL, NULL, NULL, NULL, ipst); 16848 rill_need_rele = B_TRUE; 16849 } else { 16850 recv_ill = ill; 16851 } 16852 16853 if ((ill == NULL) || (recv_ill == NULL)) { 16854 ip0dbg(("ip_fanout_proto_again: interface " 16855 "disappeared\n")); 16856 if (ill != NULL) 16857 ill_refrele(ill); 16858 if (recv_ill != NULL) 16859 ill_refrele(recv_ill); 16860 freemsg(ipsec_mp); 16861 return; 16862 } 16863 } 16864 16865 ASSERT(ill != NULL && recv_ill != NULL); 16866 16867 if (mp->b_datap->db_type == M_CTL) { 16868 /* 16869 * AH/ESP is returning the ICMP message after 16870 * removing their headers. Fanout again till 16871 * it gets to the right protocol. 16872 */ 16873 if (ii->ipsec_in_v4) { 16874 icmph_t *icmph; 16875 int iph_hdr_length; 16876 int hdr_length; 16877 16878 ipha = (ipha_t *)mp->b_rptr; 16879 iph_hdr_length = IPH_HDR_LENGTH(ipha); 16880 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 16881 ipha = (ipha_t *)&icmph[1]; 16882 hdr_length = IPH_HDR_LENGTH(ipha); 16883 /* 16884 * icmp_inbound_error_fanout may need to do pullupmsg. 16885 * Reset the type to M_DATA. 16886 */ 16887 mp->b_datap->db_type = M_DATA; 16888 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 16889 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 16890 B_FALSE, ill, ii->ipsec_in_zoneid); 16891 } else { 16892 icmp6_t *icmp6; 16893 int hdr_length; 16894 16895 ip6h = (ip6_t *)mp->b_rptr; 16896 /* Don't call hdr_length_v6() unless you have to. */ 16897 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 16898 hdr_length = ip_hdr_length_v6(mp, ip6h); 16899 else 16900 hdr_length = IPV6_HDR_LEN; 16901 16902 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 16903 /* 16904 * icmp_inbound_error_fanout_v6 may need to do 16905 * pullupmsg. Reset the type to M_DATA. 16906 */ 16907 mp->b_datap->db_type = M_DATA; 16908 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 16909 ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); 16910 } 16911 if (ill_need_rele) 16912 ill_refrele(ill); 16913 if (rill_need_rele) 16914 ill_refrele(recv_ill); 16915 return; 16916 } 16917 16918 if (ii->ipsec_in_v4) { 16919 ipha = (ipha_t *)mp->b_rptr; 16920 dst = ipha->ipha_dst; 16921 if (CLASSD(dst)) { 16922 /* 16923 * Multicast has to be delivered to all streams. 16924 */ 16925 dst = INADDR_BROADCAST; 16926 } 16927 16928 if (ire == NULL) { 16929 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, 16930 MBLK_GETLABEL(mp), ipst); 16931 if (ire == NULL) { 16932 if (ill_need_rele) 16933 ill_refrele(ill); 16934 if (rill_need_rele) 16935 ill_refrele(recv_ill); 16936 ip1dbg(("ip_fanout_proto_again: " 16937 "IRE not found")); 16938 freemsg(ipsec_mp); 16939 return; 16940 } 16941 ire_need_rele = B_TRUE; 16942 } 16943 16944 switch (ipha->ipha_protocol) { 16945 case IPPROTO_UDP: 16946 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 16947 recv_ill); 16948 if (ire_need_rele) 16949 ire_refrele(ire); 16950 break; 16951 case IPPROTO_TCP: 16952 if (!ire_need_rele) 16953 IRE_REFHOLD(ire); 16954 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 16955 ire, ipsec_mp, 0, ill->ill_rq, NULL); 16956 IRE_REFRELE(ire); 16957 if (mp != NULL) 16958 squeue_enter_chain(GET_SQUEUE(mp), mp, 16959 mp, 1, SQTAG_IP_PROTO_AGAIN); 16960 break; 16961 case IPPROTO_SCTP: 16962 if (!ire_need_rele) 16963 IRE_REFHOLD(ire); 16964 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 16965 ipsec_mp, 0, ill->ill_rq, dst); 16966 break; 16967 default: 16968 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 16969 recv_ill, B_FALSE); 16970 if (ire_need_rele) 16971 ire_refrele(ire); 16972 break; 16973 } 16974 } else { 16975 uint32_t rput_flags = 0; 16976 16977 ip6h = (ip6_t *)mp->b_rptr; 16978 v6dstp = &ip6h->ip6_dst; 16979 /* 16980 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 16981 * address. 16982 * 16983 * Currently, we don't store that state in the IPSEC_IN 16984 * message, and we may need to. 16985 */ 16986 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 16987 IP6_IN_LLMCAST : 0); 16988 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 16989 NULL, NULL); 16990 } 16991 if (ill_need_rele) 16992 ill_refrele(ill); 16993 if (rill_need_rele) 16994 ill_refrele(recv_ill); 16995 } 16996 16997 /* 16998 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 16999 * returns 'true' if there are still fragments left on the queue, in 17000 * which case we restart the timer. 17001 */ 17002 void 17003 ill_frag_timer(void *arg) 17004 { 17005 ill_t *ill = (ill_t *)arg; 17006 boolean_t frag_pending; 17007 ip_stack_t *ipst = ill->ill_ipst; 17008 17009 mutex_enter(&ill->ill_lock); 17010 ASSERT(!ill->ill_fragtimer_executing); 17011 if (ill->ill_state_flags & ILL_CONDEMNED) { 17012 ill->ill_frag_timer_id = 0; 17013 mutex_exit(&ill->ill_lock); 17014 return; 17015 } 17016 ill->ill_fragtimer_executing = 1; 17017 mutex_exit(&ill->ill_lock); 17018 17019 frag_pending = ill_frag_timeout(ill, ipst->ips_ip_g_frag_timeout); 17020 17021 /* 17022 * Restart the timer, if we have fragments pending or if someone 17023 * wanted us to be scheduled again. 17024 */ 17025 mutex_enter(&ill->ill_lock); 17026 ill->ill_fragtimer_executing = 0; 17027 ill->ill_frag_timer_id = 0; 17028 if (frag_pending || ill->ill_fragtimer_needrestart) 17029 ill_frag_timer_start(ill); 17030 mutex_exit(&ill->ill_lock); 17031 } 17032 17033 void 17034 ill_frag_timer_start(ill_t *ill) 17035 { 17036 ip_stack_t *ipst = ill->ill_ipst; 17037 17038 ASSERT(MUTEX_HELD(&ill->ill_lock)); 17039 17040 /* If the ill is closing or opening don't proceed */ 17041 if (ill->ill_state_flags & ILL_CONDEMNED) 17042 return; 17043 17044 if (ill->ill_fragtimer_executing) { 17045 /* 17046 * ill_frag_timer is currently executing. Just record the 17047 * the fact that we want the timer to be restarted. 17048 * ill_frag_timer will post a timeout before it returns, 17049 * ensuring it will be called again. 17050 */ 17051 ill->ill_fragtimer_needrestart = 1; 17052 return; 17053 } 17054 17055 if (ill->ill_frag_timer_id == 0) { 17056 /* 17057 * The timer is neither running nor is the timeout handler 17058 * executing. Post a timeout so that ill_frag_timer will be 17059 * called 17060 */ 17061 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 17062 MSEC_TO_TICK(ipst->ips_ip_g_frag_timo_ms >> 1)); 17063 ill->ill_fragtimer_needrestart = 0; 17064 } 17065 } 17066 17067 /* 17068 * This routine is needed for loopback when forwarding multicasts. 17069 * 17070 * IPQoS Notes: 17071 * IPPF processing is done in fanout routines. 17072 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 17073 * processing for IPsec packets is done when it comes back in clear. 17074 * NOTE : The callers of this function need to do the ire_refrele for the 17075 * ire that is being passed in. 17076 */ 17077 void 17078 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17079 ill_t *recv_ill, boolean_t esp_in_udp_packet) 17080 { 17081 ill_t *ill = (ill_t *)q->q_ptr; 17082 uint32_t sum; 17083 uint32_t u1; 17084 uint32_t u2; 17085 int hdr_length; 17086 boolean_t mctl_present; 17087 mblk_t *first_mp = mp; 17088 mblk_t *hada_mp = NULL; 17089 ipha_t *inner_ipha; 17090 ip_stack_t *ipst; 17091 17092 ASSERT(recv_ill != NULL); 17093 ipst = recv_ill->ill_ipst; 17094 17095 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 17096 "ip_rput_locl_start: q %p", q); 17097 17098 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17099 ASSERT(ill != NULL); 17100 17101 17102 #define rptr ((uchar_t *)ipha) 17103 #define iphs ((uint16_t *)ipha) 17104 17105 /* 17106 * no UDP or TCP packet should come here anymore. 17107 */ 17108 ASSERT(ipha->ipha_protocol != IPPROTO_TCP && 17109 ipha->ipha_protocol != IPPROTO_UDP); 17110 17111 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 17112 if (mctl_present && 17113 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 17114 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 17115 17116 /* 17117 * It's an IPsec accelerated packet. 17118 * Keep a pointer to the data attributes around until 17119 * we allocate the ipsec_info_t. 17120 */ 17121 IPSECHW_DEBUG(IPSECHW_PKT, 17122 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 17123 hada_mp = first_mp; 17124 hada_mp->b_cont = NULL; 17125 /* 17126 * Since it is accelerated, it comes directly from 17127 * the ill and the data attributes is followed by 17128 * the packet data. 17129 */ 17130 ASSERT(mp->b_datap->db_type != M_CTL); 17131 first_mp = mp; 17132 mctl_present = B_FALSE; 17133 } 17134 17135 /* 17136 * IF M_CTL is not present, then ipsec_in_is_secure 17137 * should return B_TRUE. There is a case where loopback 17138 * packets has an M_CTL in the front with all the 17139 * IPsec options set to IPSEC_PREF_NEVER - which means 17140 * ipsec_in_is_secure will return B_FALSE. As loopback 17141 * packets never comes here, it is safe to ASSERT the 17142 * following. 17143 */ 17144 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 17145 17146 /* 17147 * Also, we should never have an mctl_present if this is an 17148 * ESP-in-UDP packet. 17149 */ 17150 ASSERT(!mctl_present || !esp_in_udp_packet); 17151 17152 17153 /* u1 is # words of IP options */ 17154 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 17155 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 17156 17157 if (u1 || (!esp_in_udp_packet && !mctl_present)) { 17158 if (u1) { 17159 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 17160 if (hada_mp != NULL) 17161 freemsg(hada_mp); 17162 return; 17163 } 17164 } else { 17165 /* Check the IP header checksum. */ 17166 #define uph ((uint16_t *)ipha) 17167 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 17168 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 17169 #undef uph 17170 /* finish doing IP checksum */ 17171 sum = (sum & 0xFFFF) + (sum >> 16); 17172 sum = ~(sum + (sum >> 16)) & 0xFFFF; 17173 if (sum && sum != 0xFFFF) { 17174 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 17175 goto drop_pkt; 17176 } 17177 } 17178 } 17179 17180 /* 17181 * Count for SNMP of inbound packets for ire. As ip_proto_input 17182 * might be called more than once for secure packets, count only 17183 * the first time. 17184 */ 17185 if (!mctl_present) { 17186 UPDATE_IB_PKT_COUNT(ire); 17187 ire->ire_last_used_time = lbolt; 17188 } 17189 17190 /* Check for fragmentation offset. */ 17191 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 17192 u1 = u2 & (IPH_MF | IPH_OFFSET); 17193 if (u1) { 17194 /* 17195 * We re-assemble fragments before we do the AH/ESP 17196 * processing. Thus, M_CTL should not be present 17197 * while we are re-assembling. 17198 */ 17199 ASSERT(!mctl_present); 17200 ASSERT(first_mp == mp); 17201 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 17202 return; 17203 } 17204 /* 17205 * Make sure that first_mp points back to mp as 17206 * the mp we came in with could have changed in 17207 * ip_rput_fragment(). 17208 */ 17209 ipha = (ipha_t *)mp->b_rptr; 17210 first_mp = mp; 17211 } 17212 17213 /* 17214 * Clear hardware checksumming flag as it is currently only 17215 * used by TCP and UDP. 17216 */ 17217 DB_CKSUMFLAGS(mp) = 0; 17218 17219 /* Now we have a complete datagram, destined for this machine. */ 17220 u1 = IPH_HDR_LENGTH(ipha); 17221 switch (ipha->ipha_protocol) { 17222 case IPPROTO_ICMP: { 17223 ire_t *ire_zone; 17224 ilm_t *ilm; 17225 mblk_t *mp1; 17226 zoneid_t last_zoneid; 17227 17228 if (CLASSD(ipha->ipha_dst) && !IS_LOOPBACK(recv_ill)) { 17229 ASSERT(ire->ire_type == IRE_BROADCAST); 17230 /* 17231 * In the multicast case, applications may have joined 17232 * the group from different zones, so we need to deliver 17233 * the packet to each of them. Loop through the 17234 * multicast memberships structures (ilm) on the receive 17235 * ill and send a copy of the packet up each matching 17236 * one. However, we don't do this for multicasts sent on 17237 * the loopback interface (PHYI_LOOPBACK flag set) as 17238 * they must stay in the sender's zone. 17239 * 17240 * ilm_add_v6() ensures that ilms in the same zone are 17241 * contiguous in the ill_ilm list. We use this property 17242 * to avoid sending duplicates needed when two 17243 * applications in the same zone join the same group on 17244 * different logical interfaces: we ignore the ilm if 17245 * its zoneid is the same as the last matching one. 17246 * In addition, the sending of the packet for 17247 * ire_zoneid is delayed until all of the other ilms 17248 * have been exhausted. 17249 */ 17250 last_zoneid = -1; 17251 ILM_WALKER_HOLD(recv_ill); 17252 for (ilm = recv_ill->ill_ilm; ilm != NULL; 17253 ilm = ilm->ilm_next) { 17254 if ((ilm->ilm_flags & ILM_DELETED) || 17255 ipha->ipha_dst != ilm->ilm_addr || 17256 ilm->ilm_zoneid == last_zoneid || 17257 ilm->ilm_zoneid == ire->ire_zoneid || 17258 ilm->ilm_zoneid == ALL_ZONES || 17259 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 17260 continue; 17261 mp1 = ip_copymsg(first_mp); 17262 if (mp1 == NULL) 17263 continue; 17264 icmp_inbound(q, mp1, B_TRUE, ill, 17265 0, sum, mctl_present, B_TRUE, 17266 recv_ill, ilm->ilm_zoneid); 17267 last_zoneid = ilm->ilm_zoneid; 17268 } 17269 ILM_WALKER_RELE(recv_ill); 17270 } else if (ire->ire_type == IRE_BROADCAST) { 17271 /* 17272 * In the broadcast case, there may be many zones 17273 * which need a copy of the packet delivered to them. 17274 * There is one IRE_BROADCAST per broadcast address 17275 * and per zone; we walk those using a helper function. 17276 * In addition, the sending of the packet for ire is 17277 * delayed until all of the other ires have been 17278 * processed. 17279 */ 17280 IRB_REFHOLD(ire->ire_bucket); 17281 ire_zone = NULL; 17282 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 17283 ire)) != NULL) { 17284 mp1 = ip_copymsg(first_mp); 17285 if (mp1 == NULL) 17286 continue; 17287 17288 UPDATE_IB_PKT_COUNT(ire_zone); 17289 ire_zone->ire_last_used_time = lbolt; 17290 icmp_inbound(q, mp1, B_TRUE, ill, 17291 0, sum, mctl_present, B_TRUE, 17292 recv_ill, ire_zone->ire_zoneid); 17293 } 17294 IRB_REFRELE(ire->ire_bucket); 17295 } 17296 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 17297 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 17298 ire->ire_zoneid); 17299 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17300 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 17301 return; 17302 } 17303 case IPPROTO_IGMP: 17304 /* 17305 * If we are not willing to accept IGMP packets in clear, 17306 * then check with global policy. 17307 */ 17308 if (ipst->ips_igmp_accept_clear_messages == 0) { 17309 first_mp = ipsec_check_global_policy(first_mp, NULL, 17310 ipha, NULL, mctl_present, ipst->ips_netstack); 17311 if (first_mp == NULL) 17312 return; 17313 } 17314 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17315 freemsg(first_mp); 17316 ip1dbg(("ip_proto_input: zone all cannot accept raw")); 17317 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17318 return; 17319 } 17320 if ((mp = igmp_input(q, mp, ill)) == NULL) { 17321 /* Bad packet - discarded by igmp_input */ 17322 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17323 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 17324 if (mctl_present) 17325 freeb(first_mp); 17326 return; 17327 } 17328 /* 17329 * igmp_input() may have returned the pulled up message. 17330 * So first_mp and ipha need to be reinitialized. 17331 */ 17332 ipha = (ipha_t *)mp->b_rptr; 17333 if (mctl_present) 17334 first_mp->b_cont = mp; 17335 else 17336 first_mp = mp; 17337 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17338 connf_head != NULL) { 17339 /* No user-level listener for IGMP packets */ 17340 goto drop_pkt; 17341 } 17342 /* deliver to local raw users */ 17343 break; 17344 case IPPROTO_PIM: 17345 /* 17346 * If we are not willing to accept PIM packets in clear, 17347 * then check with global policy. 17348 */ 17349 if (ipst->ips_pim_accept_clear_messages == 0) { 17350 first_mp = ipsec_check_global_policy(first_mp, NULL, 17351 ipha, NULL, mctl_present, ipst->ips_netstack); 17352 if (first_mp == NULL) 17353 return; 17354 } 17355 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17356 freemsg(first_mp); 17357 ip1dbg(("ip_proto_input: zone all cannot accept PIM")); 17358 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17359 return; 17360 } 17361 if (pim_input(q, mp, ill) != 0) { 17362 /* Bad packet - discarded by pim_input */ 17363 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17364 "ip_rput_locl_end: q %p (%S)", q, "pim"); 17365 if (mctl_present) 17366 freeb(first_mp); 17367 return; 17368 } 17369 17370 /* 17371 * pim_input() may have pulled up the message so ipha needs to 17372 * be reinitialized. 17373 */ 17374 ipha = (ipha_t *)mp->b_rptr; 17375 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17376 connf_head != NULL) { 17377 /* No user-level listener for PIM packets */ 17378 goto drop_pkt; 17379 } 17380 /* deliver to local raw users */ 17381 break; 17382 case IPPROTO_ENCAP: 17383 /* 17384 * Handle self-encapsulated packets (IP-in-IP where 17385 * the inner addresses == the outer addresses). 17386 */ 17387 hdr_length = IPH_HDR_LENGTH(ipha); 17388 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 17389 mp->b_wptr) { 17390 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 17391 sizeof (ipha_t) - mp->b_rptr)) { 17392 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17393 freemsg(first_mp); 17394 return; 17395 } 17396 ipha = (ipha_t *)mp->b_rptr; 17397 } 17398 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 17399 /* 17400 * Check the sanity of the inner IP header. 17401 */ 17402 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 17403 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17404 freemsg(first_mp); 17405 return; 17406 } 17407 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 17408 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17409 freemsg(first_mp); 17410 return; 17411 } 17412 if (inner_ipha->ipha_src == ipha->ipha_src && 17413 inner_ipha->ipha_dst == ipha->ipha_dst) { 17414 ipsec_in_t *ii; 17415 17416 /* 17417 * Self-encapsulated tunnel packet. Remove 17418 * the outer IP header and fanout again. 17419 * We also need to make sure that the inner 17420 * header is pulled up until options. 17421 */ 17422 mp->b_rptr = (uchar_t *)inner_ipha; 17423 ipha = inner_ipha; 17424 hdr_length = IPH_HDR_LENGTH(ipha); 17425 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 17426 if (!pullupmsg(mp, (uchar_t *)ipha + 17427 + hdr_length - mp->b_rptr)) { 17428 freemsg(first_mp); 17429 return; 17430 } 17431 ipha = (ipha_t *)mp->b_rptr; 17432 } 17433 if (!mctl_present) { 17434 ASSERT(first_mp == mp); 17435 /* 17436 * This means that somebody is sending 17437 * Self-encapsualted packets without AH/ESP. 17438 * If AH/ESP was present, we would have already 17439 * allocated the first_mp. 17440 */ 17441 first_mp = ipsec_in_alloc(B_TRUE, 17442 ipst->ips_netstack); 17443 if (first_mp == NULL) { 17444 ip1dbg(("ip_proto_input: IPSEC_IN " 17445 "allocation failure.\n")); 17446 BUMP_MIB(ill->ill_ip_mib, 17447 ipIfStatsInDiscards); 17448 freemsg(mp); 17449 return; 17450 } 17451 first_mp->b_cont = mp; 17452 } 17453 /* 17454 * We generally store the ill_index if we need to 17455 * do IPsec processing as we lose the ill queue when 17456 * we come back. But in this case, we never should 17457 * have to store the ill_index here as it should have 17458 * been stored previously when we processed the 17459 * AH/ESP header in this routine or for non-ipsec 17460 * cases, we still have the queue. But for some bad 17461 * packets from the wire, we can get to IPsec after 17462 * this and we better store the index for that case. 17463 */ 17464 ill = (ill_t *)q->q_ptr; 17465 ii = (ipsec_in_t *)first_mp->b_rptr; 17466 ii->ipsec_in_ill_index = 17467 ill->ill_phyint->phyint_ifindex; 17468 ii->ipsec_in_rill_index = 17469 recv_ill->ill_phyint->phyint_ifindex; 17470 if (ii->ipsec_in_decaps) { 17471 /* 17472 * This packet is self-encapsulated multiple 17473 * times. We don't want to recurse infinitely. 17474 * To keep it simple, drop the packet. 17475 */ 17476 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17477 freemsg(first_mp); 17478 return; 17479 } 17480 ii->ipsec_in_decaps = B_TRUE; 17481 ip_fanout_proto_again(first_mp, recv_ill, recv_ill, 17482 ire); 17483 return; 17484 } 17485 break; 17486 case IPPROTO_AH: 17487 case IPPROTO_ESP: { 17488 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 17489 17490 /* 17491 * Fast path for AH/ESP. If this is the first time 17492 * we are sending a datagram to AH/ESP, allocate 17493 * a IPSEC_IN message and prepend it. Otherwise, 17494 * just fanout. 17495 */ 17496 17497 int ipsec_rc; 17498 ipsec_in_t *ii; 17499 netstack_t *ns = ipst->ips_netstack; 17500 17501 IP_STAT(ipst, ipsec_proto_ahesp); 17502 if (!mctl_present) { 17503 ASSERT(first_mp == mp); 17504 first_mp = ipsec_in_alloc(B_TRUE, ns); 17505 if (first_mp == NULL) { 17506 ip1dbg(("ip_proto_input: IPSEC_IN " 17507 "allocation failure.\n")); 17508 freemsg(hada_mp); /* okay ifnull */ 17509 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17510 freemsg(mp); 17511 return; 17512 } 17513 /* 17514 * Store the ill_index so that when we come back 17515 * from IPsec we ride on the same queue. 17516 */ 17517 ill = (ill_t *)q->q_ptr; 17518 ii = (ipsec_in_t *)first_mp->b_rptr; 17519 ii->ipsec_in_ill_index = 17520 ill->ill_phyint->phyint_ifindex; 17521 ii->ipsec_in_rill_index = 17522 recv_ill->ill_phyint->phyint_ifindex; 17523 first_mp->b_cont = mp; 17524 /* 17525 * Cache hardware acceleration info. 17526 */ 17527 if (hada_mp != NULL) { 17528 IPSECHW_DEBUG(IPSECHW_PKT, 17529 ("ip_rput_local: caching data attr.\n")); 17530 ii->ipsec_in_accelerated = B_TRUE; 17531 ii->ipsec_in_da = hada_mp; 17532 hada_mp = NULL; 17533 } 17534 } else { 17535 ii = (ipsec_in_t *)first_mp->b_rptr; 17536 } 17537 17538 if (!ipsec_loaded(ipss)) { 17539 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 17540 ire->ire_zoneid, ipst); 17541 return; 17542 } 17543 17544 ns = ipst->ips_netstack; 17545 /* select inbound SA and have IPsec process the pkt */ 17546 if (ipha->ipha_protocol == IPPROTO_ESP) { 17547 esph_t *esph = ipsec_inbound_esp_sa(first_mp, ns); 17548 boolean_t esp_in_udp_sa; 17549 if (esph == NULL) 17550 return; 17551 ASSERT(ii->ipsec_in_esp_sa != NULL); 17552 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 17553 esp_in_udp_sa = ((ii->ipsec_in_esp_sa->ipsa_flags & 17554 IPSA_F_NATT) != 0); 17555 /* 17556 * The following is a fancy, but quick, way of saying: 17557 * ESP-in-UDP SA and Raw ESP packet --> drop 17558 * OR 17559 * ESP SA and ESP-in-UDP packet --> drop 17560 */ 17561 if (esp_in_udp_sa != esp_in_udp_packet) { 17562 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17563 ip_drop_packet(first_mp, B_TRUE, ill, NULL, 17564 DROPPER(ns->netstack_ipsec, ipds_esp_no_sa), 17565 &ns->netstack_ipsec->ipsec_dropper); 17566 return; 17567 } 17568 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 17569 first_mp, esph); 17570 } else { 17571 ah_t *ah = ipsec_inbound_ah_sa(first_mp, ns); 17572 if (ah == NULL) 17573 return; 17574 ASSERT(ii->ipsec_in_ah_sa != NULL); 17575 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 17576 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 17577 first_mp, ah); 17578 } 17579 17580 switch (ipsec_rc) { 17581 case IPSEC_STATUS_SUCCESS: 17582 break; 17583 case IPSEC_STATUS_FAILED: 17584 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17585 /* FALLTHRU */ 17586 case IPSEC_STATUS_PENDING: 17587 return; 17588 } 17589 /* we're done with IPsec processing, send it up */ 17590 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 17591 return; 17592 } 17593 default: 17594 break; 17595 } 17596 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { 17597 ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", 17598 ire->ire_zoneid)); 17599 goto drop_pkt; 17600 } 17601 /* 17602 * Handle protocols with which IP is less intimate. There 17603 * can be more than one stream bound to a particular 17604 * protocol. When this is the case, each one gets a copy 17605 * of any incoming packets. 17606 */ 17607 ip_fanout_proto(q, first_mp, ill, ipha, 17608 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 17609 B_TRUE, recv_ill, ire->ire_zoneid); 17610 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17611 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 17612 return; 17613 17614 drop_pkt: 17615 freemsg(first_mp); 17616 if (hada_mp != NULL) 17617 freeb(hada_mp); 17618 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17619 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 17620 #undef rptr 17621 #undef iphs 17622 17623 } 17624 17625 /* 17626 * Update any source route, record route or timestamp options. 17627 * Check that we are at end of strict source route. 17628 * The options have already been checked for sanity in ip_rput_options(). 17629 */ 17630 static boolean_t 17631 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17632 ip_stack_t *ipst) 17633 { 17634 ipoptp_t opts; 17635 uchar_t *opt; 17636 uint8_t optval; 17637 uint8_t optlen; 17638 ipaddr_t dst; 17639 uint32_t ts; 17640 ire_t *dst_ire; 17641 timestruc_t now; 17642 zoneid_t zoneid; 17643 ill_t *ill; 17644 17645 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17646 17647 ip2dbg(("ip_rput_local_options\n")); 17648 17649 for (optval = ipoptp_first(&opts, ipha); 17650 optval != IPOPT_EOL; 17651 optval = ipoptp_next(&opts)) { 17652 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 17653 opt = opts.ipoptp_cur; 17654 optlen = opts.ipoptp_len; 17655 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 17656 optval, optlen)); 17657 switch (optval) { 17658 uint32_t off; 17659 case IPOPT_SSRR: 17660 case IPOPT_LSRR: 17661 off = opt[IPOPT_OFFSET]; 17662 off--; 17663 if (optlen < IP_ADDR_LEN || 17664 off > optlen - IP_ADDR_LEN) { 17665 /* End of source route */ 17666 ip1dbg(("ip_rput_local_options: end of SR\n")); 17667 break; 17668 } 17669 /* 17670 * This will only happen if two consecutive entries 17671 * in the source route contains our address or if 17672 * it is a packet with a loose source route which 17673 * reaches us before consuming the whole source route 17674 */ 17675 ip1dbg(("ip_rput_local_options: not end of SR\n")); 17676 if (optval == IPOPT_SSRR) { 17677 goto bad_src_route; 17678 } 17679 /* 17680 * Hack: instead of dropping the packet truncate the 17681 * source route to what has been used by filling the 17682 * rest with IPOPT_NOP. 17683 */ 17684 opt[IPOPT_OLEN] = (uint8_t)off; 17685 while (off < optlen) { 17686 opt[off++] = IPOPT_NOP; 17687 } 17688 break; 17689 case IPOPT_RR: 17690 off = opt[IPOPT_OFFSET]; 17691 off--; 17692 if (optlen < IP_ADDR_LEN || 17693 off > optlen - IP_ADDR_LEN) { 17694 /* No more room - ignore */ 17695 ip1dbg(( 17696 "ip_rput_local_options: end of RR\n")); 17697 break; 17698 } 17699 bcopy(&ire->ire_src_addr, (char *)opt + off, 17700 IP_ADDR_LEN); 17701 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17702 break; 17703 case IPOPT_TS: 17704 /* Insert timestamp if there is romm */ 17705 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17706 case IPOPT_TS_TSONLY: 17707 off = IPOPT_TS_TIMELEN; 17708 break; 17709 case IPOPT_TS_PRESPEC: 17710 case IPOPT_TS_PRESPEC_RFC791: 17711 /* Verify that the address matched */ 17712 off = opt[IPOPT_OFFSET] - 1; 17713 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17714 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17715 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 17716 ipst); 17717 if (dst_ire == NULL) { 17718 /* Not for us */ 17719 break; 17720 } 17721 ire_refrele(dst_ire); 17722 /* FALLTHRU */ 17723 case IPOPT_TS_TSANDADDR: 17724 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17725 break; 17726 default: 17727 /* 17728 * ip_*put_options should have already 17729 * dropped this packet. 17730 */ 17731 cmn_err(CE_PANIC, "ip_rput_local_options: " 17732 "unknown IT - bug in ip_rput_options?\n"); 17733 return (B_TRUE); /* Keep "lint" happy */ 17734 } 17735 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 17736 /* Increase overflow counter */ 17737 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 17738 opt[IPOPT_POS_OV_FLG] = 17739 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 17740 (off << 4)); 17741 break; 17742 } 17743 off = opt[IPOPT_OFFSET] - 1; 17744 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17745 case IPOPT_TS_PRESPEC: 17746 case IPOPT_TS_PRESPEC_RFC791: 17747 case IPOPT_TS_TSANDADDR: 17748 bcopy(&ire->ire_src_addr, (char *)opt + off, 17749 IP_ADDR_LEN); 17750 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17751 /* FALLTHRU */ 17752 case IPOPT_TS_TSONLY: 17753 off = opt[IPOPT_OFFSET] - 1; 17754 /* Compute # of milliseconds since midnight */ 17755 gethrestime(&now); 17756 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 17757 now.tv_nsec / (NANOSEC / MILLISEC); 17758 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 17759 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 17760 break; 17761 } 17762 break; 17763 } 17764 } 17765 return (B_TRUE); 17766 17767 bad_src_route: 17768 q = WR(q); 17769 if (q->q_next != NULL) 17770 ill = q->q_ptr; 17771 else 17772 ill = NULL; 17773 17774 /* make sure we clear any indication of a hardware checksum */ 17775 DB_CKSUMFLAGS(mp) = 0; 17776 zoneid = ipif_lookup_addr_zoneid(ipha->ipha_dst, ill, ipst); 17777 if (zoneid == ALL_ZONES) 17778 freemsg(mp); 17779 else 17780 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 17781 return (B_FALSE); 17782 17783 } 17784 17785 /* 17786 * Process IP options in an inbound packet. If an option affects the 17787 * effective destination address, return the next hop address via dstp. 17788 * Returns -1 if something fails in which case an ICMP error has been sent 17789 * and mp freed. 17790 */ 17791 static int 17792 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp, 17793 ip_stack_t *ipst) 17794 { 17795 ipoptp_t opts; 17796 uchar_t *opt; 17797 uint8_t optval; 17798 uint8_t optlen; 17799 ipaddr_t dst; 17800 intptr_t code = 0; 17801 ire_t *ire = NULL; 17802 zoneid_t zoneid; 17803 ill_t *ill; 17804 17805 ip2dbg(("ip_rput_options\n")); 17806 dst = ipha->ipha_dst; 17807 for (optval = ipoptp_first(&opts, ipha); 17808 optval != IPOPT_EOL; 17809 optval = ipoptp_next(&opts)) { 17810 opt = opts.ipoptp_cur; 17811 optlen = opts.ipoptp_len; 17812 ip2dbg(("ip_rput_options: opt %d, len %d\n", 17813 optval, optlen)); 17814 /* 17815 * Note: we need to verify the checksum before we 17816 * modify anything thus this routine only extracts the next 17817 * hop dst from any source route. 17818 */ 17819 switch (optval) { 17820 uint32_t off; 17821 case IPOPT_SSRR: 17822 case IPOPT_LSRR: 17823 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17824 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17825 if (ire == NULL) { 17826 if (optval == IPOPT_SSRR) { 17827 ip1dbg(("ip_rput_options: not next" 17828 " strict source route 0x%x\n", 17829 ntohl(dst))); 17830 code = (char *)&ipha->ipha_dst - 17831 (char *)ipha; 17832 goto param_prob; /* RouterReq's */ 17833 } 17834 ip2dbg(("ip_rput_options: " 17835 "not next source route 0x%x\n", 17836 ntohl(dst))); 17837 break; 17838 } 17839 ire_refrele(ire); 17840 17841 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17842 ip1dbg(( 17843 "ip_rput_options: bad option offset\n")); 17844 code = (char *)&opt[IPOPT_OLEN] - 17845 (char *)ipha; 17846 goto param_prob; 17847 } 17848 off = opt[IPOPT_OFFSET]; 17849 off--; 17850 redo_srr: 17851 if (optlen < IP_ADDR_LEN || 17852 off > optlen - IP_ADDR_LEN) { 17853 /* End of source route */ 17854 ip1dbg(("ip_rput_options: end of SR\n")); 17855 break; 17856 } 17857 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17858 ip1dbg(("ip_rput_options: next hop 0x%x\n", 17859 ntohl(dst))); 17860 17861 /* 17862 * Check if our address is present more than 17863 * once as consecutive hops in source route. 17864 * XXX verify per-interface ip_forwarding 17865 * for source route? 17866 */ 17867 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17868 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17869 17870 if (ire != NULL) { 17871 ire_refrele(ire); 17872 off += IP_ADDR_LEN; 17873 goto redo_srr; 17874 } 17875 17876 if (dst == htonl(INADDR_LOOPBACK)) { 17877 ip1dbg(("ip_rput_options: loopback addr in " 17878 "source route!\n")); 17879 goto bad_src_route; 17880 } 17881 /* 17882 * For strict: verify that dst is directly 17883 * reachable. 17884 */ 17885 if (optval == IPOPT_SSRR) { 17886 ire = ire_ftable_lookup(dst, 0, 0, 17887 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 17888 MBLK_GETLABEL(mp), 17889 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 17890 if (ire == NULL) { 17891 ip1dbg(("ip_rput_options: SSRR not " 17892 "directly reachable: 0x%x\n", 17893 ntohl(dst))); 17894 goto bad_src_route; 17895 } 17896 ire_refrele(ire); 17897 } 17898 /* 17899 * Defer update of the offset and the record route 17900 * until the packet is forwarded. 17901 */ 17902 break; 17903 case IPOPT_RR: 17904 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17905 ip1dbg(( 17906 "ip_rput_options: bad option offset\n")); 17907 code = (char *)&opt[IPOPT_OLEN] - 17908 (char *)ipha; 17909 goto param_prob; 17910 } 17911 break; 17912 case IPOPT_TS: 17913 /* 17914 * Verify that length >= 5 and that there is either 17915 * room for another timestamp or that the overflow 17916 * counter is not maxed out. 17917 */ 17918 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 17919 if (optlen < IPOPT_MINLEN_IT) { 17920 goto param_prob; 17921 } 17922 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17923 ip1dbg(( 17924 "ip_rput_options: bad option offset\n")); 17925 code = (char *)&opt[IPOPT_OFFSET] - 17926 (char *)ipha; 17927 goto param_prob; 17928 } 17929 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17930 case IPOPT_TS_TSONLY: 17931 off = IPOPT_TS_TIMELEN; 17932 break; 17933 case IPOPT_TS_TSANDADDR: 17934 case IPOPT_TS_PRESPEC: 17935 case IPOPT_TS_PRESPEC_RFC791: 17936 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17937 break; 17938 default: 17939 code = (char *)&opt[IPOPT_POS_OV_FLG] - 17940 (char *)ipha; 17941 goto param_prob; 17942 } 17943 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 17944 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 17945 /* 17946 * No room and the overflow counter is 15 17947 * already. 17948 */ 17949 goto param_prob; 17950 } 17951 break; 17952 } 17953 } 17954 17955 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 17956 *dstp = dst; 17957 return (0); 17958 } 17959 17960 ip1dbg(("ip_rput_options: error processing IP options.")); 17961 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 17962 17963 param_prob: 17964 q = WR(q); 17965 if (q->q_next != NULL) 17966 ill = q->q_ptr; 17967 else 17968 ill = NULL; 17969 17970 /* make sure we clear any indication of a hardware checksum */ 17971 DB_CKSUMFLAGS(mp) = 0; 17972 /* Don't know whether this is for non-global or global/forwarding */ 17973 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 17974 if (zoneid == ALL_ZONES) 17975 freemsg(mp); 17976 else 17977 icmp_param_problem(q, mp, (uint8_t)code, zoneid, ipst); 17978 return (-1); 17979 17980 bad_src_route: 17981 q = WR(q); 17982 if (q->q_next != NULL) 17983 ill = q->q_ptr; 17984 else 17985 ill = NULL; 17986 17987 /* make sure we clear any indication of a hardware checksum */ 17988 DB_CKSUMFLAGS(mp) = 0; 17989 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 17990 if (zoneid == ALL_ZONES) 17991 freemsg(mp); 17992 else 17993 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 17994 return (-1); 17995 } 17996 17997 /* 17998 * IP & ICMP info in >=14 msg's ... 17999 * - ip fixed part (mib2_ip_t) 18000 * - icmp fixed part (mib2_icmp_t) 18001 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 18002 * - ipRouteEntryTable (ip 21) all IPv4 IREs 18003 * - ipNetToMediaEntryTable (ip 22) [filled in by the arp module] 18004 * - ipRouteAttributeTable (ip 102) labeled routes 18005 * - ip multicast membership (ip_member_t) 18006 * - ip multicast source filtering (ip_grpsrc_t) 18007 * - igmp fixed part (struct igmpstat) 18008 * - multicast routing stats (struct mrtstat) 18009 * - multicast routing vifs (array of struct vifctl) 18010 * - multicast routing routes (array of struct mfcctl) 18011 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 18012 * One per ill plus one generic 18013 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 18014 * One per ill plus one generic 18015 * - ipv6RouteEntry all IPv6 IREs 18016 * - ipv6RouteAttributeTable (ip6 102) labeled routes 18017 * - ipv6NetToMediaEntry all Neighbor Cache entries 18018 * - ipv6AddrEntry all IPv6 ipifs 18019 * - ipv6 multicast membership (ipv6_member_t) 18020 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 18021 * 18022 * MIB2_IP_MEDIA is filled in by the arp module with ARP cache entries. 18023 * 18024 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 18025 * already filled in by the caller. 18026 * Return value of 0 indicates that no messages were sent and caller 18027 * should free mpctl. 18028 */ 18029 int 18030 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level) 18031 { 18032 ip_stack_t *ipst; 18033 sctp_stack_t *sctps; 18034 18035 if (q->q_next != NULL) { 18036 ipst = ILLQ_TO_IPST(q); 18037 } else { 18038 ipst = CONNQ_TO_IPST(q); 18039 } 18040 ASSERT(ipst != NULL); 18041 sctps = ipst->ips_netstack->netstack_sctp; 18042 18043 if (mpctl == NULL || mpctl->b_cont == NULL) { 18044 return (0); 18045 } 18046 18047 /* 18048 * For the purposes of the (broken) packet shell use 18049 * of the level we make sure MIB2_TCP/MIB2_UDP can be used 18050 * to make TCP and UDP appear first in the list of mib items. 18051 * TBD: We could expand this and use it in netstat so that 18052 * the kernel doesn't have to produce large tables (connections, 18053 * routes, etc) when netstat only wants the statistics or a particular 18054 * table. 18055 */ 18056 if (!(level == MIB2_TCP || level == MIB2_UDP)) { 18057 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) { 18058 return (1); 18059 } 18060 } 18061 18062 if (level != MIB2_TCP) { 18063 if ((mpctl = udp_snmp_get(q, mpctl)) == NULL) { 18064 return (1); 18065 } 18066 } 18067 18068 if (level != MIB2_UDP) { 18069 if ((mpctl = tcp_snmp_get(q, mpctl)) == NULL) { 18070 return (1); 18071 } 18072 } 18073 18074 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl, 18075 ipst)) == NULL) { 18076 return (1); 18077 } 18078 18079 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst)) == NULL) { 18080 return (1); 18081 } 18082 18083 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) { 18084 return (1); 18085 } 18086 18087 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) { 18088 return (1); 18089 } 18090 18091 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) { 18092 return (1); 18093 } 18094 18095 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) { 18096 return (1); 18097 } 18098 18099 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst)) == NULL) { 18100 return (1); 18101 } 18102 18103 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst)) == NULL) { 18104 return (1); 18105 } 18106 18107 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) { 18108 return (1); 18109 } 18110 18111 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) { 18112 return (1); 18113 } 18114 18115 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) { 18116 return (1); 18117 } 18118 18119 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) { 18120 return (1); 18121 } 18122 18123 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) { 18124 return (1); 18125 } 18126 18127 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) { 18128 return (1); 18129 } 18130 18131 if ((mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, ipst)) == NULL) { 18132 return (1); 18133 } 18134 18135 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, ipst); 18136 if (mpctl == NULL) { 18137 return (1); 18138 } 18139 18140 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) { 18141 return (1); 18142 } 18143 freemsg(mpctl); 18144 return (1); 18145 } 18146 18147 18148 /* Get global (legacy) IPv4 statistics */ 18149 static mblk_t * 18150 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib, 18151 ip_stack_t *ipst) 18152 { 18153 mib2_ip_t old_ip_mib; 18154 struct opthdr *optp; 18155 mblk_t *mp2ctl; 18156 18157 /* 18158 * make a copy of the original message 18159 */ 18160 mp2ctl = copymsg(mpctl); 18161 18162 /* fixed length IP structure... */ 18163 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18164 optp->level = MIB2_IP; 18165 optp->name = 0; 18166 SET_MIB(old_ip_mib.ipForwarding, 18167 (WE_ARE_FORWARDING(ipst) ? 1 : 2)); 18168 SET_MIB(old_ip_mib.ipDefaultTTL, 18169 (uint32_t)ipst->ips_ip_def_ttl); 18170 SET_MIB(old_ip_mib.ipReasmTimeout, 18171 ipst->ips_ip_g_frag_timeout); 18172 SET_MIB(old_ip_mib.ipAddrEntrySize, 18173 sizeof (mib2_ipAddrEntry_t)); 18174 SET_MIB(old_ip_mib.ipRouteEntrySize, 18175 sizeof (mib2_ipRouteEntry_t)); 18176 SET_MIB(old_ip_mib.ipNetToMediaEntrySize, 18177 sizeof (mib2_ipNetToMediaEntry_t)); 18178 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 18179 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 18180 SET_MIB(old_ip_mib.ipRouteAttributeSize, 18181 sizeof (mib2_ipAttributeEntry_t)); 18182 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 18183 18184 /* 18185 * Grab the statistics from the new IP MIB 18186 */ 18187 SET_MIB(old_ip_mib.ipInReceives, 18188 (uint32_t)ipmib->ipIfStatsHCInReceives); 18189 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors); 18190 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors); 18191 SET_MIB(old_ip_mib.ipForwDatagrams, 18192 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams); 18193 SET_MIB(old_ip_mib.ipInUnknownProtos, 18194 ipmib->ipIfStatsInUnknownProtos); 18195 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards); 18196 SET_MIB(old_ip_mib.ipInDelivers, 18197 (uint32_t)ipmib->ipIfStatsHCInDelivers); 18198 SET_MIB(old_ip_mib.ipOutRequests, 18199 (uint32_t)ipmib->ipIfStatsHCOutRequests); 18200 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards); 18201 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes); 18202 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds); 18203 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs); 18204 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails); 18205 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs); 18206 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails); 18207 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates); 18208 18209 /* ipRoutingDiscards is not being used */ 18210 SET_MIB(old_ip_mib.ipRoutingDiscards, 0); 18211 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs); 18212 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts); 18213 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs); 18214 SET_MIB(old_ip_mib.ipReasmDuplicates, 18215 ipmib->ipIfStatsReasmDuplicates); 18216 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups); 18217 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits); 18218 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs); 18219 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows); 18220 SET_MIB(old_ip_mib.rawipInOverflows, 18221 ipmib->rawipIfStatsInOverflows); 18222 18223 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded); 18224 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed); 18225 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion); 18226 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion); 18227 SET_MIB(old_ip_mib.ipOutSwitchIPv6, 18228 ipmib->ipIfStatsOutSwitchIPVersion); 18229 18230 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib, 18231 (int)sizeof (old_ip_mib))) { 18232 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 18233 (uint_t)sizeof (old_ip_mib))); 18234 } 18235 18236 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18237 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 18238 (int)optp->level, (int)optp->name, (int)optp->len)); 18239 qreply(q, mpctl); 18240 return (mp2ctl); 18241 } 18242 18243 /* Per interface IPv4 statistics */ 18244 static mblk_t * 18245 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18246 { 18247 struct opthdr *optp; 18248 mblk_t *mp2ctl; 18249 ill_t *ill; 18250 ill_walk_context_t ctx; 18251 mblk_t *mp_tail = NULL; 18252 mib2_ipIfStatsEntry_t global_ip_mib; 18253 18254 /* 18255 * Make a copy of the original message 18256 */ 18257 mp2ctl = copymsg(mpctl); 18258 18259 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18260 optp->level = MIB2_IP; 18261 optp->name = MIB2_IP_TRAFFIC_STATS; 18262 /* Include "unknown interface" ip_mib */ 18263 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 18264 ipst->ips_ip_mib.ipIfStatsIfIndex = 18265 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 18266 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding, 18267 (ipst->ips_ip_g_forward ? 1 : 2)); 18268 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL, 18269 (uint32_t)ipst->ips_ip_def_ttl); 18270 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize, 18271 sizeof (mib2_ipIfStatsEntry_t)); 18272 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize, 18273 sizeof (mib2_ipAddrEntry_t)); 18274 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize, 18275 sizeof (mib2_ipRouteEntry_t)); 18276 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize, 18277 sizeof (mib2_ipNetToMediaEntry_t)); 18278 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize, 18279 sizeof (ip_member_t)); 18280 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize, 18281 sizeof (ip_grpsrc_t)); 18282 18283 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18284 (char *)&ipst->ips_ip_mib, (int)sizeof (ipst->ips_ip_mib))) { 18285 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18286 "failed to allocate %u bytes\n", 18287 (uint_t)sizeof (ipst->ips_ip_mib))); 18288 } 18289 18290 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib)); 18291 18292 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18293 ill = ILL_START_WALK_V4(&ctx, ipst); 18294 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18295 ill->ill_ip_mib->ipIfStatsIfIndex = 18296 ill->ill_phyint->phyint_ifindex; 18297 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 18298 (ipst->ips_ip_g_forward ? 1 : 2)); 18299 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL, 18300 (uint32_t)ipst->ips_ip_def_ttl); 18301 18302 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib); 18303 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18304 (char *)ill->ill_ip_mib, 18305 (int)sizeof (*ill->ill_ip_mib))) { 18306 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18307 "failed to allocate %u bytes\n", 18308 (uint_t)sizeof (*ill->ill_ip_mib))); 18309 } 18310 } 18311 rw_exit(&ipst->ips_ill_g_lock); 18312 18313 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18314 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18315 "level %d, name %d, len %d\n", 18316 (int)optp->level, (int)optp->name, (int)optp->len)); 18317 qreply(q, mpctl); 18318 18319 if (mp2ctl == NULL) 18320 return (NULL); 18321 18322 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst)); 18323 } 18324 18325 /* Global IPv4 ICMP statistics */ 18326 static mblk_t * 18327 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18328 { 18329 struct opthdr *optp; 18330 mblk_t *mp2ctl; 18331 18332 /* 18333 * Make a copy of the original message 18334 */ 18335 mp2ctl = copymsg(mpctl); 18336 18337 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18338 optp->level = MIB2_ICMP; 18339 optp->name = 0; 18340 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib, 18341 (int)sizeof (ipst->ips_icmp_mib))) { 18342 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 18343 (uint_t)sizeof (ipst->ips_icmp_mib))); 18344 } 18345 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18346 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 18347 (int)optp->level, (int)optp->name, (int)optp->len)); 18348 qreply(q, mpctl); 18349 return (mp2ctl); 18350 } 18351 18352 /* Global IPv4 IGMP statistics */ 18353 static mblk_t * 18354 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18355 { 18356 struct opthdr *optp; 18357 mblk_t *mp2ctl; 18358 18359 /* 18360 * make a copy of the original message 18361 */ 18362 mp2ctl = copymsg(mpctl); 18363 18364 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18365 optp->level = EXPER_IGMP; 18366 optp->name = 0; 18367 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat, 18368 (int)sizeof (ipst->ips_igmpstat))) { 18369 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 18370 (uint_t)sizeof (ipst->ips_igmpstat))); 18371 } 18372 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18373 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 18374 (int)optp->level, (int)optp->name, (int)optp->len)); 18375 qreply(q, mpctl); 18376 return (mp2ctl); 18377 } 18378 18379 /* Global IPv4 Multicast Routing statistics */ 18380 static mblk_t * 18381 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18382 { 18383 struct opthdr *optp; 18384 mblk_t *mp2ctl; 18385 18386 /* 18387 * make a copy of the original message 18388 */ 18389 mp2ctl = copymsg(mpctl); 18390 18391 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18392 optp->level = EXPER_DVMRP; 18393 optp->name = 0; 18394 if (!ip_mroute_stats(mpctl->b_cont, ipst)) { 18395 ip0dbg(("ip_mroute_stats: failed\n")); 18396 } 18397 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18398 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 18399 (int)optp->level, (int)optp->name, (int)optp->len)); 18400 qreply(q, mpctl); 18401 return (mp2ctl); 18402 } 18403 18404 /* IPv4 address information */ 18405 static mblk_t * 18406 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18407 { 18408 struct opthdr *optp; 18409 mblk_t *mp2ctl; 18410 mblk_t *mp_tail = NULL; 18411 ill_t *ill; 18412 ipif_t *ipif; 18413 uint_t bitval; 18414 mib2_ipAddrEntry_t mae; 18415 zoneid_t zoneid; 18416 ill_walk_context_t ctx; 18417 18418 /* 18419 * make a copy of the original message 18420 */ 18421 mp2ctl = copymsg(mpctl); 18422 18423 /* ipAddrEntryTable */ 18424 18425 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18426 optp->level = MIB2_IP; 18427 optp->name = MIB2_IP_ADDR; 18428 zoneid = Q_TO_CONN(q)->conn_zoneid; 18429 18430 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18431 ill = ILL_START_WALK_V4(&ctx, ipst); 18432 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18433 for (ipif = ill->ill_ipif; ipif != NULL; 18434 ipif = ipif->ipif_next) { 18435 if (ipif->ipif_zoneid != zoneid && 18436 ipif->ipif_zoneid != ALL_ZONES) 18437 continue; 18438 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18439 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18440 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18441 18442 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes, 18443 OCTET_LENGTH); 18444 mae.ipAdEntIfIndex.o_length = 18445 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 18446 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 18447 mae.ipAdEntNetMask = ipif->ipif_net_mask; 18448 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 18449 mae.ipAdEntInfo.ae_subnet_len = 18450 ip_mask_to_plen(ipif->ipif_net_mask); 18451 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 18452 for (bitval = 1; 18453 bitval && 18454 !(bitval & ipif->ipif_brd_addr); 18455 bitval <<= 1) 18456 noop; 18457 mae.ipAdEntBcastAddr = bitval; 18458 mae.ipAdEntReasmMaxSize = IP_MAXPACKET; 18459 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 18460 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 18461 mae.ipAdEntInfo.ae_broadcast_addr = 18462 ipif->ipif_brd_addr; 18463 mae.ipAdEntInfo.ae_pp_dst_addr = 18464 ipif->ipif_pp_dst_addr; 18465 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 18466 ill->ill_flags | ill->ill_phyint->phyint_flags; 18467 mae.ipAdEntRetransmitTime = AR_EQ_DEFAULT_XMIT_INTERVAL; 18468 18469 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18470 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 18471 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 18472 "allocate %u bytes\n", 18473 (uint_t)sizeof (mib2_ipAddrEntry_t))); 18474 } 18475 } 18476 } 18477 rw_exit(&ipst->ips_ill_g_lock); 18478 18479 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18480 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 18481 (int)optp->level, (int)optp->name, (int)optp->len)); 18482 qreply(q, mpctl); 18483 return (mp2ctl); 18484 } 18485 18486 /* IPv6 address information */ 18487 static mblk_t * 18488 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18489 { 18490 struct opthdr *optp; 18491 mblk_t *mp2ctl; 18492 mblk_t *mp_tail = NULL; 18493 ill_t *ill; 18494 ipif_t *ipif; 18495 mib2_ipv6AddrEntry_t mae6; 18496 zoneid_t zoneid; 18497 ill_walk_context_t ctx; 18498 18499 /* 18500 * make a copy of the original message 18501 */ 18502 mp2ctl = copymsg(mpctl); 18503 18504 /* ipv6AddrEntryTable */ 18505 18506 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18507 optp->level = MIB2_IP6; 18508 optp->name = MIB2_IP6_ADDR; 18509 zoneid = Q_TO_CONN(q)->conn_zoneid; 18510 18511 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18512 ill = ILL_START_WALK_V6(&ctx, ipst); 18513 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18514 for (ipif = ill->ill_ipif; ipif != NULL; 18515 ipif = ipif->ipif_next) { 18516 if (ipif->ipif_zoneid != zoneid && 18517 ipif->ipif_zoneid != ALL_ZONES) 18518 continue; 18519 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18520 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18521 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18522 18523 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes, 18524 OCTET_LENGTH); 18525 mae6.ipv6AddrIfIndex.o_length = 18526 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 18527 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 18528 mae6.ipv6AddrPfxLength = 18529 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 18530 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 18531 mae6.ipv6AddrInfo.ae_subnet_len = 18532 mae6.ipv6AddrPfxLength; 18533 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 18534 18535 /* Type: stateless(1), stateful(2), unknown(3) */ 18536 if (ipif->ipif_flags & IPIF_ADDRCONF) 18537 mae6.ipv6AddrType = 1; 18538 else 18539 mae6.ipv6AddrType = 2; 18540 /* Anycast: true(1), false(2) */ 18541 if (ipif->ipif_flags & IPIF_ANYCAST) 18542 mae6.ipv6AddrAnycastFlag = 1; 18543 else 18544 mae6.ipv6AddrAnycastFlag = 2; 18545 18546 /* 18547 * Address status: preferred(1), deprecated(2), 18548 * invalid(3), inaccessible(4), unknown(5) 18549 */ 18550 if (ipif->ipif_flags & IPIF_NOLOCAL) 18551 mae6.ipv6AddrStatus = 3; 18552 else if (ipif->ipif_flags & IPIF_DEPRECATED) 18553 mae6.ipv6AddrStatus = 2; 18554 else 18555 mae6.ipv6AddrStatus = 1; 18556 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 18557 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 18558 mae6.ipv6AddrInfo.ae_pp_dst_addr = 18559 ipif->ipif_v6pp_dst_addr; 18560 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 18561 ill->ill_flags | ill->ill_phyint->phyint_flags; 18562 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET; 18563 mae6.ipv6AddrIdentifier = ill->ill_token; 18564 mae6.ipv6AddrIdentifierLen = ill->ill_token_length; 18565 mae6.ipv6AddrReachableTime = ill->ill_reachable_time; 18566 mae6.ipv6AddrRetransmitTime = 18567 ill->ill_reachable_retrans_time; 18568 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18569 (char *)&mae6, 18570 (int)sizeof (mib2_ipv6AddrEntry_t))) { 18571 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 18572 "allocate %u bytes\n", 18573 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 18574 } 18575 } 18576 } 18577 rw_exit(&ipst->ips_ill_g_lock); 18578 18579 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18580 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 18581 (int)optp->level, (int)optp->name, (int)optp->len)); 18582 qreply(q, mpctl); 18583 return (mp2ctl); 18584 } 18585 18586 /* IPv4 multicast group membership. */ 18587 static mblk_t * 18588 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18589 { 18590 struct opthdr *optp; 18591 mblk_t *mp2ctl; 18592 ill_t *ill; 18593 ipif_t *ipif; 18594 ilm_t *ilm; 18595 ip_member_t ipm; 18596 mblk_t *mp_tail = NULL; 18597 ill_walk_context_t ctx; 18598 zoneid_t zoneid; 18599 18600 /* 18601 * make a copy of the original message 18602 */ 18603 mp2ctl = copymsg(mpctl); 18604 zoneid = Q_TO_CONN(q)->conn_zoneid; 18605 18606 /* ipGroupMember table */ 18607 optp = (struct opthdr *)&mpctl->b_rptr[ 18608 sizeof (struct T_optmgmt_ack)]; 18609 optp->level = MIB2_IP; 18610 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 18611 18612 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18613 ill = ILL_START_WALK_V4(&ctx, ipst); 18614 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18615 ILM_WALKER_HOLD(ill); 18616 for (ipif = ill->ill_ipif; ipif != NULL; 18617 ipif = ipif->ipif_next) { 18618 if (ipif->ipif_zoneid != zoneid && 18619 ipif->ipif_zoneid != ALL_ZONES) 18620 continue; /* not this zone */ 18621 ipif_get_name(ipif, ipm.ipGroupMemberIfIndex.o_bytes, 18622 OCTET_LENGTH); 18623 ipm.ipGroupMemberIfIndex.o_length = 18624 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 18625 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18626 ASSERT(ilm->ilm_ipif != NULL); 18627 ASSERT(ilm->ilm_ill == NULL); 18628 if (ilm->ilm_ipif != ipif) 18629 continue; 18630 ipm.ipGroupMemberAddress = ilm->ilm_addr; 18631 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 18632 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 18633 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18634 (char *)&ipm, (int)sizeof (ipm))) { 18635 ip1dbg(("ip_snmp_get_mib2_ip_group: " 18636 "failed to allocate %u bytes\n", 18637 (uint_t)sizeof (ipm))); 18638 } 18639 } 18640 } 18641 ILM_WALKER_RELE(ill); 18642 } 18643 rw_exit(&ipst->ips_ill_g_lock); 18644 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18645 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18646 (int)optp->level, (int)optp->name, (int)optp->len)); 18647 qreply(q, mpctl); 18648 return (mp2ctl); 18649 } 18650 18651 /* IPv6 multicast group membership. */ 18652 static mblk_t * 18653 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18654 { 18655 struct opthdr *optp; 18656 mblk_t *mp2ctl; 18657 ill_t *ill; 18658 ilm_t *ilm; 18659 ipv6_member_t ipm6; 18660 mblk_t *mp_tail = NULL; 18661 ill_walk_context_t ctx; 18662 zoneid_t zoneid; 18663 18664 /* 18665 * make a copy of the original message 18666 */ 18667 mp2ctl = copymsg(mpctl); 18668 zoneid = Q_TO_CONN(q)->conn_zoneid; 18669 18670 /* ip6GroupMember table */ 18671 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18672 optp->level = MIB2_IP6; 18673 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 18674 18675 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18676 ill = ILL_START_WALK_V6(&ctx, ipst); 18677 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18678 ILM_WALKER_HOLD(ill); 18679 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 18680 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18681 ASSERT(ilm->ilm_ipif == NULL); 18682 ASSERT(ilm->ilm_ill != NULL); 18683 if (ilm->ilm_zoneid != zoneid) 18684 continue; /* not this zone */ 18685 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 18686 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 18687 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 18688 if (!snmp_append_data2(mpctl->b_cont, 18689 &mp_tail, 18690 (char *)&ipm6, (int)sizeof (ipm6))) { 18691 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 18692 "failed to allocate %u bytes\n", 18693 (uint_t)sizeof (ipm6))); 18694 } 18695 } 18696 ILM_WALKER_RELE(ill); 18697 } 18698 rw_exit(&ipst->ips_ill_g_lock); 18699 18700 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18701 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18702 (int)optp->level, (int)optp->name, (int)optp->len)); 18703 qreply(q, mpctl); 18704 return (mp2ctl); 18705 } 18706 18707 /* IP multicast filtered sources */ 18708 static mblk_t * 18709 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18710 { 18711 struct opthdr *optp; 18712 mblk_t *mp2ctl; 18713 ill_t *ill; 18714 ipif_t *ipif; 18715 ilm_t *ilm; 18716 ip_grpsrc_t ips; 18717 mblk_t *mp_tail = NULL; 18718 ill_walk_context_t ctx; 18719 zoneid_t zoneid; 18720 int i; 18721 slist_t *sl; 18722 18723 /* 18724 * make a copy of the original message 18725 */ 18726 mp2ctl = copymsg(mpctl); 18727 zoneid = Q_TO_CONN(q)->conn_zoneid; 18728 18729 /* ipGroupSource table */ 18730 optp = (struct opthdr *)&mpctl->b_rptr[ 18731 sizeof (struct T_optmgmt_ack)]; 18732 optp->level = MIB2_IP; 18733 optp->name = EXPER_IP_GROUP_SOURCES; 18734 18735 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18736 ill = ILL_START_WALK_V4(&ctx, ipst); 18737 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18738 ILM_WALKER_HOLD(ill); 18739 for (ipif = ill->ill_ipif; ipif != NULL; 18740 ipif = ipif->ipif_next) { 18741 if (ipif->ipif_zoneid != zoneid) 18742 continue; /* not this zone */ 18743 ipif_get_name(ipif, ips.ipGroupSourceIfIndex.o_bytes, 18744 OCTET_LENGTH); 18745 ips.ipGroupSourceIfIndex.o_length = 18746 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 18747 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18748 ASSERT(ilm->ilm_ipif != NULL); 18749 ASSERT(ilm->ilm_ill == NULL); 18750 sl = ilm->ilm_filter; 18751 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 18752 continue; 18753 ips.ipGroupSourceGroup = ilm->ilm_addr; 18754 for (i = 0; i < sl->sl_numsrc; i++) { 18755 if (!IN6_IS_ADDR_V4MAPPED( 18756 &sl->sl_addr[i])) 18757 continue; 18758 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 18759 ips.ipGroupSourceAddress); 18760 if (snmp_append_data2(mpctl->b_cont, 18761 &mp_tail, (char *)&ips, 18762 (int)sizeof (ips)) == 0) { 18763 ip1dbg(("ip_snmp_get_mib2_" 18764 "ip_group_src: failed to " 18765 "allocate %u bytes\n", 18766 (uint_t)sizeof (ips))); 18767 } 18768 } 18769 } 18770 } 18771 ILM_WALKER_RELE(ill); 18772 } 18773 rw_exit(&ipst->ips_ill_g_lock); 18774 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18775 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18776 (int)optp->level, (int)optp->name, (int)optp->len)); 18777 qreply(q, mpctl); 18778 return (mp2ctl); 18779 } 18780 18781 /* IPv6 multicast filtered sources. */ 18782 static mblk_t * 18783 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18784 { 18785 struct opthdr *optp; 18786 mblk_t *mp2ctl; 18787 ill_t *ill; 18788 ilm_t *ilm; 18789 ipv6_grpsrc_t ips6; 18790 mblk_t *mp_tail = NULL; 18791 ill_walk_context_t ctx; 18792 zoneid_t zoneid; 18793 int i; 18794 slist_t *sl; 18795 18796 /* 18797 * make a copy of the original message 18798 */ 18799 mp2ctl = copymsg(mpctl); 18800 zoneid = Q_TO_CONN(q)->conn_zoneid; 18801 18802 /* ip6GroupMember table */ 18803 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18804 optp->level = MIB2_IP6; 18805 optp->name = EXPER_IP6_GROUP_SOURCES; 18806 18807 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18808 ill = ILL_START_WALK_V6(&ctx, ipst); 18809 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18810 ILM_WALKER_HOLD(ill); 18811 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 18812 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18813 ASSERT(ilm->ilm_ipif == NULL); 18814 ASSERT(ilm->ilm_ill != NULL); 18815 sl = ilm->ilm_filter; 18816 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 18817 continue; 18818 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 18819 for (i = 0; i < sl->sl_numsrc; i++) { 18820 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 18821 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18822 (char *)&ips6, (int)sizeof (ips6))) { 18823 ip1dbg(("ip_snmp_get_mib2_ip6_" 18824 "group_src: failed to allocate " 18825 "%u bytes\n", 18826 (uint_t)sizeof (ips6))); 18827 } 18828 } 18829 } 18830 ILM_WALKER_RELE(ill); 18831 } 18832 rw_exit(&ipst->ips_ill_g_lock); 18833 18834 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18835 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18836 (int)optp->level, (int)optp->name, (int)optp->len)); 18837 qreply(q, mpctl); 18838 return (mp2ctl); 18839 } 18840 18841 /* Multicast routing virtual interface table. */ 18842 static mblk_t * 18843 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18844 { 18845 struct opthdr *optp; 18846 mblk_t *mp2ctl; 18847 18848 /* 18849 * make a copy of the original message 18850 */ 18851 mp2ctl = copymsg(mpctl); 18852 18853 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18854 optp->level = EXPER_DVMRP; 18855 optp->name = EXPER_DVMRP_VIF; 18856 if (!ip_mroute_vif(mpctl->b_cont, ipst)) { 18857 ip0dbg(("ip_mroute_vif: failed\n")); 18858 } 18859 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18860 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 18861 (int)optp->level, (int)optp->name, (int)optp->len)); 18862 qreply(q, mpctl); 18863 return (mp2ctl); 18864 } 18865 18866 /* Multicast routing table. */ 18867 static mblk_t * 18868 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18869 { 18870 struct opthdr *optp; 18871 mblk_t *mp2ctl; 18872 18873 /* 18874 * make a copy of the original message 18875 */ 18876 mp2ctl = copymsg(mpctl); 18877 18878 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18879 optp->level = EXPER_DVMRP; 18880 optp->name = EXPER_DVMRP_MRT; 18881 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) { 18882 ip0dbg(("ip_mroute_mrt: failed\n")); 18883 } 18884 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18885 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 18886 (int)optp->level, (int)optp->name, (int)optp->len)); 18887 qreply(q, mpctl); 18888 return (mp2ctl); 18889 } 18890 18891 /* 18892 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 18893 * in one IRE walk. 18894 */ 18895 static mblk_t * 18896 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18897 { 18898 struct opthdr *optp; 18899 mblk_t *mp2ctl; /* Returned */ 18900 mblk_t *mp3ctl; /* nettomedia */ 18901 mblk_t *mp4ctl; /* routeattrs */ 18902 iproutedata_t ird; 18903 zoneid_t zoneid; 18904 18905 /* 18906 * make copies of the original message 18907 * - mp2ctl is returned unchanged to the caller for his use 18908 * - mpctl is sent upstream as ipRouteEntryTable 18909 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 18910 * - mp4ctl is sent upstream as ipRouteAttributeTable 18911 */ 18912 mp2ctl = copymsg(mpctl); 18913 mp3ctl = copymsg(mpctl); 18914 mp4ctl = copymsg(mpctl); 18915 if (mp3ctl == NULL || mp4ctl == NULL) { 18916 freemsg(mp4ctl); 18917 freemsg(mp3ctl); 18918 freemsg(mp2ctl); 18919 freemsg(mpctl); 18920 return (NULL); 18921 } 18922 18923 bzero(&ird, sizeof (ird)); 18924 18925 ird.ird_route.lp_head = mpctl->b_cont; 18926 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 18927 ird.ird_attrs.lp_head = mp4ctl->b_cont; 18928 18929 zoneid = Q_TO_CONN(q)->conn_zoneid; 18930 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst); 18931 18932 /* ipRouteEntryTable in mpctl */ 18933 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18934 optp->level = MIB2_IP; 18935 optp->name = MIB2_IP_ROUTE; 18936 optp->len = msgdsize(ird.ird_route.lp_head); 18937 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18938 (int)optp->level, (int)optp->name, (int)optp->len)); 18939 qreply(q, mpctl); 18940 18941 /* ipNetToMediaEntryTable in mp3ctl */ 18942 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18943 optp->level = MIB2_IP; 18944 optp->name = MIB2_IP_MEDIA; 18945 optp->len = msgdsize(ird.ird_netmedia.lp_head); 18946 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18947 (int)optp->level, (int)optp->name, (int)optp->len)); 18948 qreply(q, mp3ctl); 18949 18950 /* ipRouteAttributeTable in mp4ctl */ 18951 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18952 optp->level = MIB2_IP; 18953 optp->name = EXPER_IP_RTATTR; 18954 optp->len = msgdsize(ird.ird_attrs.lp_head); 18955 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18956 (int)optp->level, (int)optp->name, (int)optp->len)); 18957 if (optp->len == 0) 18958 freemsg(mp4ctl); 18959 else 18960 qreply(q, mp4ctl); 18961 18962 return (mp2ctl); 18963 } 18964 18965 /* 18966 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 18967 * ipv6NetToMediaEntryTable in an NDP walk. 18968 */ 18969 static mblk_t * 18970 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18971 { 18972 struct opthdr *optp; 18973 mblk_t *mp2ctl; /* Returned */ 18974 mblk_t *mp3ctl; /* nettomedia */ 18975 mblk_t *mp4ctl; /* routeattrs */ 18976 iproutedata_t ird; 18977 zoneid_t zoneid; 18978 18979 /* 18980 * make copies of the original message 18981 * - mp2ctl is returned unchanged to the caller for his use 18982 * - mpctl is sent upstream as ipv6RouteEntryTable 18983 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 18984 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 18985 */ 18986 mp2ctl = copymsg(mpctl); 18987 mp3ctl = copymsg(mpctl); 18988 mp4ctl = copymsg(mpctl); 18989 if (mp3ctl == NULL || mp4ctl == NULL) { 18990 freemsg(mp4ctl); 18991 freemsg(mp3ctl); 18992 freemsg(mp2ctl); 18993 freemsg(mpctl); 18994 return (NULL); 18995 } 18996 18997 bzero(&ird, sizeof (ird)); 18998 18999 ird.ird_route.lp_head = mpctl->b_cont; 19000 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 19001 ird.ird_attrs.lp_head = mp4ctl->b_cont; 19002 19003 zoneid = Q_TO_CONN(q)->conn_zoneid; 19004 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst); 19005 19006 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19007 optp->level = MIB2_IP6; 19008 optp->name = MIB2_IP6_ROUTE; 19009 optp->len = msgdsize(ird.ird_route.lp_head); 19010 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19011 (int)optp->level, (int)optp->name, (int)optp->len)); 19012 qreply(q, mpctl); 19013 19014 /* ipv6NetToMediaEntryTable in mp3ctl */ 19015 ndp_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst); 19016 19017 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19018 optp->level = MIB2_IP6; 19019 optp->name = MIB2_IP6_MEDIA; 19020 optp->len = msgdsize(ird.ird_netmedia.lp_head); 19021 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19022 (int)optp->level, (int)optp->name, (int)optp->len)); 19023 qreply(q, mp3ctl); 19024 19025 /* ipv6RouteAttributeTable in mp4ctl */ 19026 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19027 optp->level = MIB2_IP6; 19028 optp->name = EXPER_IP_RTATTR; 19029 optp->len = msgdsize(ird.ird_attrs.lp_head); 19030 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19031 (int)optp->level, (int)optp->name, (int)optp->len)); 19032 if (optp->len == 0) 19033 freemsg(mp4ctl); 19034 else 19035 qreply(q, mp4ctl); 19036 19037 return (mp2ctl); 19038 } 19039 19040 /* 19041 * IPv6 mib: One per ill 19042 */ 19043 static mblk_t * 19044 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19045 { 19046 struct opthdr *optp; 19047 mblk_t *mp2ctl; 19048 ill_t *ill; 19049 ill_walk_context_t ctx; 19050 mblk_t *mp_tail = NULL; 19051 19052 /* 19053 * Make a copy of the original message 19054 */ 19055 mp2ctl = copymsg(mpctl); 19056 19057 /* fixed length IPv6 structure ... */ 19058 19059 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19060 optp->level = MIB2_IP6; 19061 optp->name = 0; 19062 /* Include "unknown interface" ip6_mib */ 19063 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 19064 ipst->ips_ip6_mib.ipIfStatsIfIndex = 19065 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 19066 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding, 19067 ipst->ips_ipv6_forward ? 1 : 2); 19068 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit, 19069 ipst->ips_ipv6_def_hops); 19070 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize, 19071 sizeof (mib2_ipIfStatsEntry_t)); 19072 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize, 19073 sizeof (mib2_ipv6AddrEntry_t)); 19074 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize, 19075 sizeof (mib2_ipv6RouteEntry_t)); 19076 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize, 19077 sizeof (mib2_ipv6NetToMediaEntry_t)); 19078 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize, 19079 sizeof (ipv6_member_t)); 19080 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize, 19081 sizeof (ipv6_grpsrc_t)); 19082 19083 /* 19084 * Synchronize 64- and 32-bit counters 19085 */ 19086 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives, 19087 ipIfStatsHCInReceives); 19088 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers, 19089 ipIfStatsHCInDelivers); 19090 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests, 19091 ipIfStatsHCOutRequests); 19092 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams, 19093 ipIfStatsHCOutForwDatagrams); 19094 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts, 19095 ipIfStatsHCOutMcastPkts); 19096 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts, 19097 ipIfStatsHCInMcastPkts); 19098 19099 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19100 (char *)&ipst->ips_ip6_mib, (int)sizeof (ipst->ips_ip6_mib))) { 19101 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 19102 (uint_t)sizeof (ipst->ips_ip6_mib))); 19103 } 19104 19105 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19106 ill = ILL_START_WALK_V6(&ctx, ipst); 19107 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19108 ill->ill_ip_mib->ipIfStatsIfIndex = 19109 ill->ill_phyint->phyint_ifindex; 19110 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 19111 ipst->ips_ipv6_forward ? 1 : 2); 19112 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit, 19113 ill->ill_max_hops); 19114 19115 /* 19116 * Synchronize 64- and 32-bit counters 19117 */ 19118 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives, 19119 ipIfStatsHCInReceives); 19120 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers, 19121 ipIfStatsHCInDelivers); 19122 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests, 19123 ipIfStatsHCOutRequests); 19124 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams, 19125 ipIfStatsHCOutForwDatagrams); 19126 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts, 19127 ipIfStatsHCOutMcastPkts); 19128 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts, 19129 ipIfStatsHCInMcastPkts); 19130 19131 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19132 (char *)ill->ill_ip_mib, 19133 (int)sizeof (*ill->ill_ip_mib))) { 19134 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 19135 "%u bytes\n", (uint_t)sizeof (*ill->ill_ip_mib))); 19136 } 19137 } 19138 rw_exit(&ipst->ips_ill_g_lock); 19139 19140 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19141 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 19142 (int)optp->level, (int)optp->name, (int)optp->len)); 19143 qreply(q, mpctl); 19144 return (mp2ctl); 19145 } 19146 19147 /* 19148 * ICMPv6 mib: One per ill 19149 */ 19150 static mblk_t * 19151 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19152 { 19153 struct opthdr *optp; 19154 mblk_t *mp2ctl; 19155 ill_t *ill; 19156 ill_walk_context_t ctx; 19157 mblk_t *mp_tail = NULL; 19158 /* 19159 * Make a copy of the original message 19160 */ 19161 mp2ctl = copymsg(mpctl); 19162 19163 /* fixed length ICMPv6 structure ... */ 19164 19165 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19166 optp->level = MIB2_ICMP6; 19167 optp->name = 0; 19168 /* Include "unknown interface" icmp6_mib */ 19169 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex = 19170 MIB2_UNKNOWN_INTERFACE; /* netstat flag */ 19171 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize = 19172 sizeof (mib2_ipv6IfIcmpEntry_t); 19173 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19174 (char *)&ipst->ips_icmp6_mib, 19175 (int)sizeof (ipst->ips_icmp6_mib))) { 19176 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 19177 (uint_t)sizeof (ipst->ips_icmp6_mib))); 19178 } 19179 19180 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19181 ill = ILL_START_WALK_V6(&ctx, ipst); 19182 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19183 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 19184 ill->ill_phyint->phyint_ifindex; 19185 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19186 (char *)ill->ill_icmp6_mib, 19187 (int)sizeof (*ill->ill_icmp6_mib))) { 19188 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 19189 "%u bytes\n", 19190 (uint_t)sizeof (*ill->ill_icmp6_mib))); 19191 } 19192 } 19193 rw_exit(&ipst->ips_ill_g_lock); 19194 19195 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19196 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 19197 (int)optp->level, (int)optp->name, (int)optp->len)); 19198 qreply(q, mpctl); 19199 return (mp2ctl); 19200 } 19201 19202 /* 19203 * ire_walk routine to create both ipRouteEntryTable and 19204 * ipRouteAttributeTable in one IRE walk 19205 */ 19206 static void 19207 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 19208 { 19209 ill_t *ill; 19210 ipif_t *ipif; 19211 mib2_ipRouteEntry_t *re; 19212 mib2_ipAttributeEntry_t *iae, *iaeptr; 19213 ipaddr_t gw_addr; 19214 tsol_ire_gw_secattr_t *attrp; 19215 tsol_gc_t *gc = NULL; 19216 tsol_gcgrp_t *gcgrp = NULL; 19217 uint_t sacnt = 0; 19218 int i; 19219 19220 ASSERT(ire->ire_ipversion == IPV4_VERSION); 19221 19222 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19223 return; 19224 19225 if ((attrp = ire->ire_gw_secattr) != NULL) { 19226 mutex_enter(&attrp->igsa_lock); 19227 if ((gc = attrp->igsa_gc) != NULL) { 19228 gcgrp = gc->gc_grp; 19229 ASSERT(gcgrp != NULL); 19230 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19231 sacnt = 1; 19232 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19233 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19234 gc = gcgrp->gcgrp_head; 19235 sacnt = gcgrp->gcgrp_count; 19236 } 19237 mutex_exit(&attrp->igsa_lock); 19238 19239 /* do nothing if there's no gc to report */ 19240 if (gc == NULL) { 19241 ASSERT(sacnt == 0); 19242 if (gcgrp != NULL) { 19243 /* we might as well drop the lock now */ 19244 rw_exit(&gcgrp->gcgrp_rwlock); 19245 gcgrp = NULL; 19246 } 19247 attrp = NULL; 19248 } 19249 19250 ASSERT(gc == NULL || (gcgrp != NULL && 19251 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19252 } 19253 ASSERT(sacnt == 0 || gc != NULL); 19254 19255 if (sacnt != 0 && 19256 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19257 kmem_free(re, sizeof (*re)); 19258 rw_exit(&gcgrp->gcgrp_rwlock); 19259 return; 19260 } 19261 19262 /* 19263 * Return all IRE types for route table... let caller pick and choose 19264 */ 19265 re->ipRouteDest = ire->ire_addr; 19266 ipif = ire->ire_ipif; 19267 re->ipRouteIfIndex.o_length = 0; 19268 if (ire->ire_type == IRE_CACHE) { 19269 ill = (ill_t *)ire->ire_stq->q_ptr; 19270 re->ipRouteIfIndex.o_length = 19271 ill->ill_name_length == 0 ? 0 : 19272 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19273 bcopy(ill->ill_name, re->ipRouteIfIndex.o_bytes, 19274 re->ipRouteIfIndex.o_length); 19275 } else if (ipif != NULL) { 19276 ipif_get_name(ipif, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH); 19277 re->ipRouteIfIndex.o_length = 19278 mi_strlen(re->ipRouteIfIndex.o_bytes); 19279 } 19280 re->ipRouteMetric1 = -1; 19281 re->ipRouteMetric2 = -1; 19282 re->ipRouteMetric3 = -1; 19283 re->ipRouteMetric4 = -1; 19284 19285 gw_addr = ire->ire_gateway_addr; 19286 19287 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 19288 re->ipRouteNextHop = ire->ire_src_addr; 19289 else 19290 re->ipRouteNextHop = gw_addr; 19291 /* indirect(4), direct(3), or invalid(2) */ 19292 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19293 re->ipRouteType = 2; 19294 else 19295 re->ipRouteType = (gw_addr != 0) ? 4 : 3; 19296 re->ipRouteProto = -1; 19297 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 19298 re->ipRouteMask = ire->ire_mask; 19299 re->ipRouteMetric5 = -1; 19300 re->ipRouteInfo.re_max_frag = ire->ire_max_frag; 19301 re->ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 19302 re->ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19303 re->ipRouteInfo.re_ref = ire->ire_refcnt; 19304 re->ipRouteInfo.re_src_addr = ire->ire_src_addr; 19305 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19306 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19307 re->ipRouteInfo.re_flags = ire->ire_flags; 19308 19309 if (ire->ire_flags & RTF_DYNAMIC) { 19310 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19311 } else { 19312 re->ipRouteInfo.re_ire_type = ire->ire_type; 19313 } 19314 19315 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19316 (char *)re, (int)sizeof (*re))) { 19317 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19318 (uint_t)sizeof (*re))); 19319 } 19320 19321 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19322 iaeptr->iae_routeidx = ird->ird_idx; 19323 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19324 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19325 } 19326 19327 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19328 (char *)iae, sacnt * sizeof (*iae))) { 19329 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19330 (unsigned)(sacnt * sizeof (*iae)))); 19331 } 19332 19333 /* bump route index for next pass */ 19334 ird->ird_idx++; 19335 19336 kmem_free(re, sizeof (*re)); 19337 if (sacnt != 0) 19338 kmem_free(iae, sacnt * sizeof (*iae)); 19339 19340 if (gcgrp != NULL) 19341 rw_exit(&gcgrp->gcgrp_rwlock); 19342 } 19343 19344 /* 19345 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 19346 */ 19347 static void 19348 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 19349 { 19350 ill_t *ill; 19351 ipif_t *ipif; 19352 mib2_ipv6RouteEntry_t *re; 19353 mib2_ipAttributeEntry_t *iae, *iaeptr; 19354 in6_addr_t gw_addr_v6; 19355 tsol_ire_gw_secattr_t *attrp; 19356 tsol_gc_t *gc = NULL; 19357 tsol_gcgrp_t *gcgrp = NULL; 19358 uint_t sacnt = 0; 19359 int i; 19360 19361 ASSERT(ire->ire_ipversion == IPV6_VERSION); 19362 19363 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19364 return; 19365 19366 if ((attrp = ire->ire_gw_secattr) != NULL) { 19367 mutex_enter(&attrp->igsa_lock); 19368 if ((gc = attrp->igsa_gc) != NULL) { 19369 gcgrp = gc->gc_grp; 19370 ASSERT(gcgrp != NULL); 19371 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19372 sacnt = 1; 19373 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19374 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19375 gc = gcgrp->gcgrp_head; 19376 sacnt = gcgrp->gcgrp_count; 19377 } 19378 mutex_exit(&attrp->igsa_lock); 19379 19380 /* do nothing if there's no gc to report */ 19381 if (gc == NULL) { 19382 ASSERT(sacnt == 0); 19383 if (gcgrp != NULL) { 19384 /* we might as well drop the lock now */ 19385 rw_exit(&gcgrp->gcgrp_rwlock); 19386 gcgrp = NULL; 19387 } 19388 attrp = NULL; 19389 } 19390 19391 ASSERT(gc == NULL || (gcgrp != NULL && 19392 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19393 } 19394 ASSERT(sacnt == 0 || gc != NULL); 19395 19396 if (sacnt != 0 && 19397 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19398 kmem_free(re, sizeof (*re)); 19399 rw_exit(&gcgrp->gcgrp_rwlock); 19400 return; 19401 } 19402 19403 /* 19404 * Return all IRE types for route table... let caller pick and choose 19405 */ 19406 re->ipv6RouteDest = ire->ire_addr_v6; 19407 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 19408 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 19409 re->ipv6RouteIfIndex.o_length = 0; 19410 ipif = ire->ire_ipif; 19411 if (ire->ire_type == IRE_CACHE) { 19412 ill = (ill_t *)ire->ire_stq->q_ptr; 19413 re->ipv6RouteIfIndex.o_length = 19414 ill->ill_name_length == 0 ? 0 : 19415 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19416 bcopy(ill->ill_name, re->ipv6RouteIfIndex.o_bytes, 19417 re->ipv6RouteIfIndex.o_length); 19418 } else if (ipif != NULL) { 19419 ipif_get_name(ipif, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH); 19420 re->ipv6RouteIfIndex.o_length = 19421 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 19422 } 19423 19424 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19425 19426 mutex_enter(&ire->ire_lock); 19427 gw_addr_v6 = ire->ire_gateway_addr_v6; 19428 mutex_exit(&ire->ire_lock); 19429 19430 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 19431 re->ipv6RouteNextHop = ire->ire_src_addr_v6; 19432 else 19433 re->ipv6RouteNextHop = gw_addr_v6; 19434 19435 /* remote(4), local(3), or discard(2) */ 19436 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19437 re->ipv6RouteType = 2; 19438 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 19439 re->ipv6RouteType = 3; 19440 else 19441 re->ipv6RouteType = 4; 19442 19443 re->ipv6RouteProtocol = -1; 19444 re->ipv6RoutePolicy = 0; 19445 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 19446 re->ipv6RouteNextHopRDI = 0; 19447 re->ipv6RouteWeight = 0; 19448 re->ipv6RouteMetric = 0; 19449 re->ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 19450 re->ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 19451 re->ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19452 re->ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 19453 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19454 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19455 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 19456 re->ipv6RouteInfo.re_flags = ire->ire_flags; 19457 19458 if (ire->ire_flags & RTF_DYNAMIC) { 19459 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19460 } else { 19461 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 19462 } 19463 19464 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19465 (char *)re, (int)sizeof (*re))) { 19466 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19467 (uint_t)sizeof (*re))); 19468 } 19469 19470 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19471 iaeptr->iae_routeidx = ird->ird_idx; 19472 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19473 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19474 } 19475 19476 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19477 (char *)iae, sacnt * sizeof (*iae))) { 19478 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19479 (unsigned)(sacnt * sizeof (*iae)))); 19480 } 19481 19482 /* bump route index for next pass */ 19483 ird->ird_idx++; 19484 19485 kmem_free(re, sizeof (*re)); 19486 if (sacnt != 0) 19487 kmem_free(iae, sacnt * sizeof (*iae)); 19488 19489 if (gcgrp != NULL) 19490 rw_exit(&gcgrp->gcgrp_rwlock); 19491 } 19492 19493 /* 19494 * ndp_walk routine to create ipv6NetToMediaEntryTable 19495 */ 19496 static int 19497 ip_snmp_get2_v6_media(nce_t *nce, iproutedata_t *ird) 19498 { 19499 ill_t *ill; 19500 mib2_ipv6NetToMediaEntry_t ntme; 19501 dl_unitdata_req_t *dl; 19502 19503 ill = nce->nce_ill; 19504 if (ill->ill_isv6 == B_FALSE) /* skip arpce entry */ 19505 return (0); 19506 19507 /* 19508 * Neighbor cache entry attached to IRE with on-link 19509 * destination. 19510 */ 19511 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 19512 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 19513 if ((ill->ill_flags & ILLF_XRESOLV) && 19514 (nce->nce_res_mp != NULL)) { 19515 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 19516 ntme.ipv6NetToMediaPhysAddress.o_length = 19517 dl->dl_dest_addr_length; 19518 } else { 19519 ntme.ipv6NetToMediaPhysAddress.o_length = 19520 ill->ill_phys_addr_length; 19521 } 19522 if (nce->nce_res_mp != NULL) { 19523 bcopy((char *)nce->nce_res_mp->b_rptr + 19524 NCE_LL_ADDR_OFFSET(ill), 19525 ntme.ipv6NetToMediaPhysAddress.o_bytes, 19526 ntme.ipv6NetToMediaPhysAddress.o_length); 19527 } else { 19528 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 19529 ill->ill_phys_addr_length); 19530 } 19531 /* 19532 * Note: Returns ND_* states. Should be: 19533 * reachable(1), stale(2), delay(3), probe(4), 19534 * invalid(5), unknown(6) 19535 */ 19536 ntme.ipv6NetToMediaState = nce->nce_state; 19537 ntme.ipv6NetToMediaLastUpdated = 0; 19538 19539 /* other(1), dynamic(2), static(3), local(4) */ 19540 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 19541 ntme.ipv6NetToMediaType = 4; 19542 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 19543 ntme.ipv6NetToMediaType = 1; 19544 } else { 19545 ntme.ipv6NetToMediaType = 2; 19546 } 19547 19548 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 19549 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 19550 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 19551 (uint_t)sizeof (ntme))); 19552 } 19553 return (0); 19554 } 19555 19556 /* 19557 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 19558 */ 19559 /* ARGSUSED */ 19560 int 19561 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 19562 { 19563 switch (level) { 19564 case MIB2_IP: 19565 case MIB2_ICMP: 19566 switch (name) { 19567 default: 19568 break; 19569 } 19570 return (1); 19571 default: 19572 return (1); 19573 } 19574 } 19575 19576 /* 19577 * When there exists both a 64- and 32-bit counter of a particular type 19578 * (i.e., InReceives), only the 64-bit counters are added. 19579 */ 19580 void 19581 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2) 19582 { 19583 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors); 19584 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors); 19585 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes); 19586 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors); 19587 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos); 19588 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts); 19589 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards); 19590 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards); 19591 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs); 19592 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails); 19593 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates); 19594 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds); 19595 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs); 19596 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails); 19597 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes); 19598 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates); 19599 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups); 19600 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits); 19601 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs); 19602 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows); 19603 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows); 19604 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion, 19605 o2->ipIfStatsInWrongIPVersion); 19606 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion, 19607 o2->ipIfStatsInWrongIPVersion); 19608 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion, 19609 o2->ipIfStatsOutSwitchIPVersion); 19610 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives); 19611 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets); 19612 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams, 19613 o2->ipIfStatsHCInForwDatagrams); 19614 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers); 19615 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests); 19616 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams, 19617 o2->ipIfStatsHCOutForwDatagrams); 19618 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds); 19619 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits); 19620 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets); 19621 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts); 19622 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets); 19623 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts); 19624 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets, 19625 o2->ipIfStatsHCOutMcastOctets); 19626 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts); 19627 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts); 19628 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded); 19629 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed); 19630 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs); 19631 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs); 19632 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts); 19633 } 19634 19635 void 19636 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2) 19637 { 19638 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs); 19639 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors); 19640 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs); 19641 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs); 19642 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds); 19643 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems); 19644 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs); 19645 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos); 19646 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies); 19647 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits, 19648 o2->ipv6IfIcmpInRouterSolicits); 19649 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements, 19650 o2->ipv6IfIcmpInRouterAdvertisements); 19651 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits, 19652 o2->ipv6IfIcmpInNeighborSolicits); 19653 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements, 19654 o2->ipv6IfIcmpInNeighborAdvertisements); 19655 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects); 19656 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries, 19657 o2->ipv6IfIcmpInGroupMembQueries); 19658 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses, 19659 o2->ipv6IfIcmpInGroupMembResponses); 19660 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions, 19661 o2->ipv6IfIcmpInGroupMembReductions); 19662 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs); 19663 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors); 19664 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs, 19665 o2->ipv6IfIcmpOutDestUnreachs); 19666 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs, 19667 o2->ipv6IfIcmpOutAdminProhibs); 19668 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds); 19669 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems, 19670 o2->ipv6IfIcmpOutParmProblems); 19671 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs); 19672 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos); 19673 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies); 19674 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits, 19675 o2->ipv6IfIcmpOutRouterSolicits); 19676 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements, 19677 o2->ipv6IfIcmpOutRouterAdvertisements); 19678 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits, 19679 o2->ipv6IfIcmpOutNeighborSolicits); 19680 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements, 19681 o2->ipv6IfIcmpOutNeighborAdvertisements); 19682 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects); 19683 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries, 19684 o2->ipv6IfIcmpOutGroupMembQueries); 19685 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses, 19686 o2->ipv6IfIcmpOutGroupMembResponses); 19687 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions, 19688 o2->ipv6IfIcmpOutGroupMembReductions); 19689 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows); 19690 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit); 19691 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements, 19692 o2->ipv6IfIcmpInBadNeighborAdvertisements); 19693 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations, 19694 o2->ipv6IfIcmpInBadNeighborSolicitations); 19695 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects); 19696 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal, 19697 o2->ipv6IfIcmpInGroupMembTotal); 19698 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries, 19699 o2->ipv6IfIcmpInGroupMembBadQueries); 19700 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports, 19701 o2->ipv6IfIcmpInGroupMembBadReports); 19702 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports, 19703 o2->ipv6IfIcmpInGroupMembOurReports); 19704 } 19705 19706 /* 19707 * Called before the options are updated to check if this packet will 19708 * be source routed from here. 19709 * This routine assumes that the options are well formed i.e. that they 19710 * have already been checked. 19711 */ 19712 static boolean_t 19713 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst) 19714 { 19715 ipoptp_t opts; 19716 uchar_t *opt; 19717 uint8_t optval; 19718 uint8_t optlen; 19719 ipaddr_t dst; 19720 ire_t *ire; 19721 19722 if (IS_SIMPLE_IPH(ipha)) { 19723 ip2dbg(("not source routed\n")); 19724 return (B_FALSE); 19725 } 19726 dst = ipha->ipha_dst; 19727 for (optval = ipoptp_first(&opts, ipha); 19728 optval != IPOPT_EOL; 19729 optval = ipoptp_next(&opts)) { 19730 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 19731 opt = opts.ipoptp_cur; 19732 optlen = opts.ipoptp_len; 19733 ip2dbg(("ip_source_routed: opt %d, len %d\n", 19734 optval, optlen)); 19735 switch (optval) { 19736 uint32_t off; 19737 case IPOPT_SSRR: 19738 case IPOPT_LSRR: 19739 /* 19740 * If dst is one of our addresses and there are some 19741 * entries left in the source route return (true). 19742 */ 19743 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 19744 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 19745 if (ire == NULL) { 19746 ip2dbg(("ip_source_routed: not next" 19747 " source route 0x%x\n", 19748 ntohl(dst))); 19749 return (B_FALSE); 19750 } 19751 ire_refrele(ire); 19752 off = opt[IPOPT_OFFSET]; 19753 off--; 19754 if (optlen < IP_ADDR_LEN || 19755 off > optlen - IP_ADDR_LEN) { 19756 /* End of source route */ 19757 ip1dbg(("ip_source_routed: end of SR\n")); 19758 return (B_FALSE); 19759 } 19760 return (B_TRUE); 19761 } 19762 } 19763 ip2dbg(("not source routed\n")); 19764 return (B_FALSE); 19765 } 19766 19767 /* 19768 * Check if the packet contains any source route. 19769 */ 19770 static boolean_t 19771 ip_source_route_included(ipha_t *ipha) 19772 { 19773 ipoptp_t opts; 19774 uint8_t optval; 19775 19776 if (IS_SIMPLE_IPH(ipha)) 19777 return (B_FALSE); 19778 for (optval = ipoptp_first(&opts, ipha); 19779 optval != IPOPT_EOL; 19780 optval = ipoptp_next(&opts)) { 19781 switch (optval) { 19782 case IPOPT_SSRR: 19783 case IPOPT_LSRR: 19784 return (B_TRUE); 19785 } 19786 } 19787 return (B_FALSE); 19788 } 19789 19790 /* 19791 * Called when the IRE expiration timer fires. 19792 */ 19793 void 19794 ip_trash_timer_expire(void *args) 19795 { 19796 int flush_flag = 0; 19797 ire_expire_arg_t iea; 19798 ip_stack_t *ipst = (ip_stack_t *)args; 19799 19800 iea.iea_ipst = ipst; /* No netstack_hold */ 19801 19802 /* 19803 * ip_ire_expire_id is protected by ip_trash_timer_lock. 19804 * This lock makes sure that a new invocation of this function 19805 * that occurs due to an almost immediate timer firing will not 19806 * progress beyond this point until the current invocation is done 19807 */ 19808 mutex_enter(&ipst->ips_ip_trash_timer_lock); 19809 ipst->ips_ip_ire_expire_id = 0; 19810 mutex_exit(&ipst->ips_ip_trash_timer_lock); 19811 19812 /* Periodic timer */ 19813 if (ipst->ips_ip_ire_arp_time_elapsed >= 19814 ipst->ips_ip_ire_arp_interval) { 19815 /* 19816 * Remove all IRE_CACHE entries since they might 19817 * contain arp information. 19818 */ 19819 flush_flag |= FLUSH_ARP_TIME; 19820 ipst->ips_ip_ire_arp_time_elapsed = 0; 19821 IP_STAT(ipst, ip_ire_arp_timer_expired); 19822 } 19823 if (ipst->ips_ip_ire_rd_time_elapsed >= 19824 ipst->ips_ip_ire_redir_interval) { 19825 /* Remove all redirects */ 19826 flush_flag |= FLUSH_REDIRECT_TIME; 19827 ipst->ips_ip_ire_rd_time_elapsed = 0; 19828 IP_STAT(ipst, ip_ire_redirect_timer_expired); 19829 } 19830 if (ipst->ips_ip_ire_pmtu_time_elapsed >= 19831 ipst->ips_ip_ire_pathmtu_interval) { 19832 /* Increase path mtu */ 19833 flush_flag |= FLUSH_MTU_TIME; 19834 ipst->ips_ip_ire_pmtu_time_elapsed = 0; 19835 IP_STAT(ipst, ip_ire_pmtu_timer_expired); 19836 } 19837 19838 /* 19839 * Optimize for the case when there are no redirects in the 19840 * ftable, that is, no need to walk the ftable in that case. 19841 */ 19842 if (flush_flag & (FLUSH_MTU_TIME|FLUSH_ARP_TIME)) { 19843 iea.iea_flush_flag = flush_flag; 19844 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_CACHETABLE, ire_expire, 19845 (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 0, NULL, 19846 ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table, 19847 NULL, ALL_ZONES, ipst); 19848 } 19849 if ((flush_flag & FLUSH_REDIRECT_TIME) && 19850 ipst->ips_ip_redirect_cnt > 0) { 19851 iea.iea_flush_flag = flush_flag; 19852 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_FORWARDTABLE, 19853 ire_expire, (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 19854 0, NULL, 0, NULL, NULL, ALL_ZONES, ipst); 19855 } 19856 if (flush_flag & FLUSH_MTU_TIME) { 19857 /* 19858 * Walk all IPv6 IRE's and update them 19859 * Note that ARP and redirect timers are not 19860 * needed since NUD handles stale entries. 19861 */ 19862 flush_flag = FLUSH_MTU_TIME; 19863 iea.iea_flush_flag = flush_flag; 19864 ire_walk_v6(ire_expire, (char *)(uintptr_t)&iea, 19865 ALL_ZONES, ipst); 19866 } 19867 19868 ipst->ips_ip_ire_arp_time_elapsed += ipst->ips_ip_timer_interval; 19869 ipst->ips_ip_ire_rd_time_elapsed += ipst->ips_ip_timer_interval; 19870 ipst->ips_ip_ire_pmtu_time_elapsed += ipst->ips_ip_timer_interval; 19871 19872 /* 19873 * Hold the lock to serialize timeout calls and prevent 19874 * stale values in ip_ire_expire_id. Otherwise it is possible 19875 * for the timer to fire and a new invocation of this function 19876 * to start before the return value of timeout has been stored 19877 * in ip_ire_expire_id by the current invocation. 19878 */ 19879 mutex_enter(&ipst->ips_ip_trash_timer_lock); 19880 ipst->ips_ip_ire_expire_id = timeout(ip_trash_timer_expire, 19881 (void *)ipst, MSEC_TO_TICK(ipst->ips_ip_timer_interval)); 19882 mutex_exit(&ipst->ips_ip_trash_timer_lock); 19883 } 19884 19885 /* 19886 * Called by the memory allocator subsystem directly, when the system 19887 * is running low on memory. 19888 */ 19889 /* ARGSUSED */ 19890 void 19891 ip_trash_ire_reclaim(void *args) 19892 { 19893 netstack_handle_t nh; 19894 netstack_t *ns; 19895 19896 netstack_next_init(&nh); 19897 while ((ns = netstack_next(&nh)) != NULL) { 19898 ip_trash_ire_reclaim_stack(ns->netstack_ip); 19899 netstack_rele(ns); 19900 } 19901 netstack_next_fini(&nh); 19902 } 19903 19904 static void 19905 ip_trash_ire_reclaim_stack(ip_stack_t *ipst) 19906 { 19907 ire_cache_count_t icc; 19908 ire_cache_reclaim_t icr; 19909 ncc_cache_count_t ncc; 19910 nce_cache_reclaim_t ncr; 19911 uint_t delete_cnt; 19912 /* 19913 * Memory reclaim call back. 19914 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 19915 * Then, with a target of freeing 1/Nth of IRE_CACHE 19916 * entries, determine what fraction to free for 19917 * each category of IRE_CACHE entries giving absolute priority 19918 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 19919 * entry will be freed unless all offlink entries are freed). 19920 */ 19921 icc.icc_total = 0; 19922 icc.icc_unused = 0; 19923 icc.icc_offlink = 0; 19924 icc.icc_pmtu = 0; 19925 icc.icc_onlink = 0; 19926 ire_walk(ire_cache_count, (char *)&icc, ipst); 19927 19928 /* 19929 * Free NCEs for IPv6 like the onlink ires. 19930 */ 19931 ncc.ncc_total = 0; 19932 ncc.ncc_host = 0; 19933 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc, ipst); 19934 19935 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 19936 icc.icc_pmtu + icc.icc_onlink); 19937 delete_cnt = icc.icc_total/ipst->ips_ip_ire_reclaim_fraction; 19938 IP_STAT(ipst, ip_trash_ire_reclaim_calls); 19939 if (delete_cnt == 0) 19940 return; 19941 IP_STAT(ipst, ip_trash_ire_reclaim_success); 19942 /* Always delete all unused offlink entries */ 19943 icr.icr_ipst = ipst; 19944 icr.icr_unused = 1; 19945 if (delete_cnt <= icc.icc_unused) { 19946 /* 19947 * Only need to free unused entries. In other words, 19948 * there are enough unused entries to free to meet our 19949 * target number of freed ire cache entries. 19950 */ 19951 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 19952 ncr.ncr_host = 0; 19953 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 19954 /* 19955 * Only need to free unused entries, plus a fraction of offlink 19956 * entries. It follows from the first if statement that 19957 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 19958 */ 19959 delete_cnt -= icc.icc_unused; 19960 /* Round up # deleted by truncating fraction */ 19961 icr.icr_offlink = icc.icc_offlink / delete_cnt; 19962 icr.icr_pmtu = icr.icr_onlink = 0; 19963 ncr.ncr_host = 0; 19964 } else if (delete_cnt <= 19965 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 19966 /* 19967 * Free all unused and offlink entries, plus a fraction of 19968 * pmtu entries. It follows from the previous if statement 19969 * that icc_pmtu is non-zero, and that 19970 * delete_cnt != icc_unused + icc_offlink. 19971 */ 19972 icr.icr_offlink = 1; 19973 delete_cnt -= icc.icc_unused + icc.icc_offlink; 19974 /* Round up # deleted by truncating fraction */ 19975 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 19976 icr.icr_onlink = 0; 19977 ncr.ncr_host = 0; 19978 } else { 19979 /* 19980 * Free all unused, offlink, and pmtu entries, plus a fraction 19981 * of onlink entries. If we're here, then we know that 19982 * icc_onlink is non-zero, and that 19983 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 19984 */ 19985 icr.icr_offlink = icr.icr_pmtu = 1; 19986 delete_cnt -= icc.icc_unused + icc.icc_offlink + 19987 icc.icc_pmtu; 19988 /* Round up # deleted by truncating fraction */ 19989 icr.icr_onlink = icc.icc_onlink / delete_cnt; 19990 /* Using the same delete fraction as for onlink IREs */ 19991 ncr.ncr_host = ncc.ncc_host / delete_cnt; 19992 } 19993 #ifdef DEBUG 19994 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 19995 "fractions %d/%d/%d/%d\n", 19996 icc.icc_total/ipst->ips_ip_ire_reclaim_fraction, icc.icc_total, 19997 icc.icc_unused, icc.icc_offlink, 19998 icc.icc_pmtu, icc.icc_onlink, 19999 icr.icr_unused, icr.icr_offlink, 20000 icr.icr_pmtu, icr.icr_onlink)); 20001 #endif 20002 ire_walk(ire_cache_reclaim, (char *)&icr, ipst); 20003 if (ncr.ncr_host != 0) 20004 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 20005 (uchar_t *)&ncr, ipst); 20006 #ifdef DEBUG 20007 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 20008 icc.icc_pmtu = 0; icc.icc_onlink = 0; 20009 ire_walk(ire_cache_count, (char *)&icc, ipst); 20010 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 20011 icc.icc_total, icc.icc_unused, icc.icc_offlink, 20012 icc.icc_pmtu, icc.icc_onlink)); 20013 #endif 20014 } 20015 20016 /* 20017 * ip_unbind is called when a copy of an unbind request is received from the 20018 * upper level protocol. We remove this conn from any fanout hash list it is 20019 * on, and zero out the bind information. No reply is expected up above. 20020 */ 20021 mblk_t * 20022 ip_unbind(queue_t *q, mblk_t *mp) 20023 { 20024 conn_t *connp = Q_TO_CONN(q); 20025 20026 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 20027 20028 if (is_system_labeled() && connp->conn_anon_port) { 20029 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 20030 connp->conn_mlp_type, connp->conn_ulp, 20031 ntohs(connp->conn_lport), B_FALSE); 20032 connp->conn_anon_port = 0; 20033 } 20034 connp->conn_mlp_type = mlptSingle; 20035 20036 ipcl_hash_remove(connp); 20037 20038 ASSERT(mp->b_cont == NULL); 20039 /* 20040 * Convert mp into a T_OK_ACK 20041 */ 20042 mp = mi_tpi_ok_ack_alloc(mp); 20043 20044 /* 20045 * should not happen in practice... T_OK_ACK is smaller than the 20046 * original message. 20047 */ 20048 if (mp == NULL) 20049 return (NULL); 20050 20051 return (mp); 20052 } 20053 20054 /* 20055 * Write side put procedure. Outbound data, IOCTLs, responses from 20056 * resolvers, etc, come down through here. 20057 * 20058 * arg2 is always a queue_t *. 20059 * When that queue is an ill_t (i.e. q_next != NULL), then arg must be 20060 * the zoneid. 20061 * When that queue is not an ill_t, then arg must be a conn_t pointer. 20062 */ 20063 void 20064 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 20065 { 20066 ip_output_options(arg, mp, arg2, caller, &zero_info); 20067 } 20068 20069 void 20070 ip_output_options(void *arg, mblk_t *mp, void *arg2, int caller, 20071 ip_opt_info_t *infop) 20072 { 20073 conn_t *connp = NULL; 20074 queue_t *q = (queue_t *)arg2; 20075 ipha_t *ipha; 20076 #define rptr ((uchar_t *)ipha) 20077 ire_t *ire = NULL; 20078 ire_t *sctp_ire = NULL; 20079 uint32_t v_hlen_tos_len; 20080 ipaddr_t dst; 20081 mblk_t *first_mp = NULL; 20082 boolean_t mctl_present; 20083 ipsec_out_t *io; 20084 int match_flags; 20085 ill_t *attach_ill = NULL; 20086 /* Bind to IPIF_NOFAILOVER ill etc. */ 20087 ill_t *xmit_ill = NULL; /* IP_PKTINFO etc. */ 20088 ipif_t *dst_ipif; 20089 boolean_t multirt_need_resolve = B_FALSE; 20090 mblk_t *copy_mp = NULL; 20091 int err; 20092 zoneid_t zoneid; 20093 int adjust; 20094 uint16_t iplen; 20095 boolean_t need_decref = B_FALSE; 20096 boolean_t ignore_dontroute = B_FALSE; 20097 boolean_t ignore_nexthop = B_FALSE; 20098 boolean_t ip_nexthop = B_FALSE; 20099 ipaddr_t nexthop_addr; 20100 ip_stack_t *ipst; 20101 20102 #ifdef _BIG_ENDIAN 20103 #define V_HLEN (v_hlen_tos_len >> 24) 20104 #else 20105 #define V_HLEN (v_hlen_tos_len & 0xFF) 20106 #endif 20107 20108 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 20109 "ip_wput_start: q %p", q); 20110 20111 /* 20112 * ip_wput fast path 20113 */ 20114 20115 /* is packet from ARP ? */ 20116 if (q->q_next != NULL) { 20117 zoneid = (zoneid_t)(uintptr_t)arg; 20118 goto qnext; 20119 } 20120 20121 connp = (conn_t *)arg; 20122 ASSERT(connp != NULL); 20123 zoneid = connp->conn_zoneid; 20124 ipst = connp->conn_netstack->netstack_ip; 20125 20126 /* is queue flow controlled? */ 20127 if ((q->q_first != NULL || connp->conn_draining) && 20128 (caller == IP_WPUT)) { 20129 ASSERT(!need_decref); 20130 (void) putq(q, mp); 20131 return; 20132 } 20133 20134 /* Multidata transmit? */ 20135 if (DB_TYPE(mp) == M_MULTIDATA) { 20136 /* 20137 * We should never get here, since all Multidata messages 20138 * originating from tcp should have been directed over to 20139 * tcp_multisend() in the first place. 20140 */ 20141 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20142 freemsg(mp); 20143 return; 20144 } else if (DB_TYPE(mp) != M_DATA) 20145 goto notdata; 20146 20147 if (mp->b_flag & MSGHASREF) { 20148 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20149 mp->b_flag &= ~MSGHASREF; 20150 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 20151 need_decref = B_TRUE; 20152 } 20153 ipha = (ipha_t *)mp->b_rptr; 20154 20155 /* is IP header non-aligned or mblk smaller than basic IP header */ 20156 #ifndef SAFETY_BEFORE_SPEED 20157 if (!OK_32PTR(rptr) || 20158 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 20159 goto hdrtoosmall; 20160 #endif 20161 20162 ASSERT(OK_32PTR(ipha)); 20163 20164 /* 20165 * This function assumes that mp points to an IPv4 packet. If it's the 20166 * wrong version, we'll catch it again in ip_output_v6. 20167 * 20168 * Note that this is *only* locally-generated output here, and never 20169 * forwarded data, and that we need to deal only with transports that 20170 * don't know how to label. (TCP, UDP, and ICMP/raw-IP all know how to 20171 * label.) 20172 */ 20173 if (is_system_labeled() && 20174 (ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4) && 20175 !connp->conn_ulp_labeled) { 20176 err = tsol_check_label(BEST_CRED(mp, connp), &mp, &adjust, 20177 connp->conn_mac_exempt, ipst); 20178 ipha = (ipha_t *)mp->b_rptr; 20179 if (err != 0) { 20180 first_mp = mp; 20181 if (err == EINVAL) 20182 goto icmp_parameter_problem; 20183 ip2dbg(("ip_wput: label check failed (%d)\n", err)); 20184 goto discard_pkt; 20185 } 20186 iplen = ntohs(ipha->ipha_length) + adjust; 20187 ipha->ipha_length = htons(iplen); 20188 } 20189 20190 ASSERT(infop != NULL); 20191 20192 if (infop->ip_opt_flags & IP_VERIFY_SRC) { 20193 /* 20194 * IP_PKTINFO ancillary option is present. 20195 * IPCL_ZONEID is used to honor IP_ALLZONES option which 20196 * allows using address of any zone as the source address. 20197 */ 20198 ire = ire_ctable_lookup(ipha->ipha_src, 0, 20199 (IRE_LOCAL|IRE_LOOPBACK), NULL, IPCL_ZONEID(connp), 20200 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 20201 if (ire == NULL) 20202 goto drop_pkt; 20203 ire_refrele(ire); 20204 ire = NULL; 20205 } 20206 20207 /* 20208 * IP_DONTFAILOVER_IF and IP_BOUND_IF have precedence over ill index 20209 * passed in IP_PKTINFO. 20210 */ 20211 if (infop->ip_opt_ill_index != 0 && 20212 connp->conn_outgoing_ill == NULL && 20213 connp->conn_nofailover_ill == NULL) { 20214 20215 xmit_ill = ill_lookup_on_ifindex( 20216 infop->ip_opt_ill_index, B_FALSE, NULL, NULL, NULL, NULL, 20217 ipst); 20218 20219 if (xmit_ill == NULL || IS_VNI(xmit_ill)) 20220 goto drop_pkt; 20221 /* 20222 * check that there is an ipif belonging 20223 * to our zone. IPCL_ZONEID is not used because 20224 * IP_ALLZONES option is valid only when the ill is 20225 * accessible from all zones i.e has a valid ipif in 20226 * all zones. 20227 */ 20228 if (!ipif_lookup_zoneid_group(xmit_ill, zoneid, 0, NULL)) { 20229 goto drop_pkt; 20230 } 20231 } 20232 20233 /* 20234 * If there is a policy, try to attach an ipsec_out in 20235 * the front. At the end, first_mp either points to a 20236 * M_DATA message or IPSEC_OUT message linked to a 20237 * M_DATA message. We have to do it now as we might 20238 * lose the "conn" if we go through ip_newroute. 20239 */ 20240 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 20241 if (((mp = ipsec_attach_ipsec_out(&mp, connp, NULL, 20242 ipha->ipha_protocol, ipst->ips_netstack)) == NULL)) { 20243 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20244 if (need_decref) 20245 CONN_DEC_REF(connp); 20246 return; 20247 } else { 20248 ASSERT(mp->b_datap->db_type == M_CTL); 20249 first_mp = mp; 20250 mp = mp->b_cont; 20251 mctl_present = B_TRUE; 20252 } 20253 } else { 20254 first_mp = mp; 20255 mctl_present = B_FALSE; 20256 } 20257 20258 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20259 20260 /* is wrong version or IP options present */ 20261 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 20262 goto version_hdrlen_check; 20263 dst = ipha->ipha_dst; 20264 20265 if (connp->conn_nofailover_ill != NULL) { 20266 attach_ill = conn_get_held_ill(connp, 20267 &connp->conn_nofailover_ill, &err); 20268 if (err == ILL_LOOKUP_FAILED) { 20269 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20270 if (need_decref) 20271 CONN_DEC_REF(connp); 20272 freemsg(first_mp); 20273 return; 20274 } 20275 } 20276 20277 /* If IP_BOUND_IF has been set, use that ill. */ 20278 if (connp->conn_outgoing_ill != NULL) { 20279 xmit_ill = conn_get_held_ill(connp, 20280 &connp->conn_outgoing_ill, &err); 20281 if (err == ILL_LOOKUP_FAILED) 20282 goto drop_pkt; 20283 20284 goto send_from_ill; 20285 } 20286 20287 /* is packet multicast? */ 20288 if (CLASSD(dst)) 20289 goto multicast; 20290 20291 /* 20292 * If xmit_ill is set above due to index passed in ip_pkt_info. It 20293 * takes precedence over conn_dontroute and conn_nexthop_set 20294 */ 20295 if (xmit_ill != NULL) 20296 goto send_from_ill; 20297 20298 if (connp->conn_dontroute || connp->conn_nexthop_set) { 20299 /* 20300 * If the destination is a broadcast, local, or loopback 20301 * address, SO_DONTROUTE and IP_NEXTHOP go through the 20302 * standard path. 20303 */ 20304 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20305 if ((ire == NULL) || (ire->ire_type & 20306 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)) == 0) { 20307 if (ire != NULL) { 20308 ire_refrele(ire); 20309 /* No more access to ire */ 20310 ire = NULL; 20311 } 20312 /* 20313 * bypass routing checks and go directly to interface. 20314 */ 20315 if (connp->conn_dontroute) 20316 goto dontroute; 20317 20318 ASSERT(connp->conn_nexthop_set); 20319 ip_nexthop = B_TRUE; 20320 nexthop_addr = connp->conn_nexthop_v4; 20321 goto send_from_ill; 20322 } 20323 20324 /* Must be a broadcast, a loopback or a local ire */ 20325 ire_refrele(ire); 20326 /* No more access to ire */ 20327 ire = NULL; 20328 } 20329 20330 if (attach_ill != NULL) 20331 goto send_from_ill; 20332 20333 /* 20334 * We cache IRE_CACHEs to avoid lookups. We don't do 20335 * this for the tcp global queue and listen end point 20336 * as it does not really have a real destination to 20337 * talk to. This is also true for SCTP. 20338 */ 20339 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 20340 !connp->conn_fully_bound) { 20341 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20342 if (ire == NULL) 20343 goto noirefound; 20344 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20345 "ip_wput_end: q %p (%S)", q, "end"); 20346 20347 /* 20348 * Check if the ire has the RTF_MULTIRT flag, inherited 20349 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20350 */ 20351 if (ire->ire_flags & RTF_MULTIRT) { 20352 20353 /* 20354 * Force the TTL of multirouted packets if required. 20355 * The TTL of such packets is bounded by the 20356 * ip_multirt_ttl ndd variable. 20357 */ 20358 if ((ipst->ips_ip_multirt_ttl > 0) && 20359 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20360 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20361 "(was %d), dst 0x%08x\n", 20362 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20363 ntohl(ire->ire_addr))); 20364 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20365 } 20366 /* 20367 * We look at this point if there are pending 20368 * unresolved routes. ire_multirt_resolvable() 20369 * checks in O(n) that all IRE_OFFSUBNET ire 20370 * entries for the packet's destination and 20371 * flagged RTF_MULTIRT are currently resolved. 20372 * If some remain unresolved, we make a copy 20373 * of the current message. It will be used 20374 * to initiate additional route resolutions. 20375 */ 20376 multirt_need_resolve = 20377 ire_multirt_need_resolve(ire->ire_addr, 20378 MBLK_GETLABEL(first_mp), ipst); 20379 ip2dbg(("ip_wput[TCP]: ire %p, " 20380 "multirt_need_resolve %d, first_mp %p\n", 20381 (void *)ire, multirt_need_resolve, 20382 (void *)first_mp)); 20383 if (multirt_need_resolve) { 20384 copy_mp = copymsg(first_mp); 20385 if (copy_mp != NULL) { 20386 MULTIRT_DEBUG_TAG(copy_mp); 20387 } 20388 } 20389 } 20390 20391 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20392 20393 /* 20394 * Try to resolve another multiroute if 20395 * ire_multirt_need_resolve() deemed it necessary. 20396 */ 20397 if (copy_mp != NULL) 20398 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 20399 if (need_decref) 20400 CONN_DEC_REF(connp); 20401 return; 20402 } 20403 20404 /* 20405 * Access to conn_ire_cache. (protected by conn_lock) 20406 * 20407 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 20408 * the ire bucket lock here to check for CONDEMNED as it is okay to 20409 * send a packet or two with the IRE_CACHE that is going away. 20410 * Access to the ire requires an ire refhold on the ire prior to 20411 * its use since an interface unplumb thread may delete the cached 20412 * ire and release the refhold at any time. 20413 * 20414 * Caching an ire in the conn_ire_cache 20415 * 20416 * o Caching an ire pointer in the conn requires a strict check for 20417 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 20418 * ires before cleaning up the conns. So the caching of an ire pointer 20419 * in the conn is done after making sure under the bucket lock that the 20420 * ire has not yet been marked CONDEMNED. Otherwise we will end up 20421 * caching an ire after the unplumb thread has cleaned up the conn. 20422 * If the conn does not send a packet subsequently the unplumb thread 20423 * will be hanging waiting for the ire count to drop to zero. 20424 * 20425 * o We also need to atomically test for a null conn_ire_cache and 20426 * set the conn_ire_cache under the the protection of the conn_lock 20427 * to avoid races among concurrent threads trying to simultaneously 20428 * cache an ire in the conn_ire_cache. 20429 */ 20430 mutex_enter(&connp->conn_lock); 20431 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 20432 20433 if (ire != NULL && ire->ire_addr == dst && 20434 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20435 20436 IRE_REFHOLD(ire); 20437 mutex_exit(&connp->conn_lock); 20438 20439 } else { 20440 boolean_t cached = B_FALSE; 20441 connp->conn_ire_cache = NULL; 20442 mutex_exit(&connp->conn_lock); 20443 /* Release the old ire */ 20444 if (ire != NULL && sctp_ire == NULL) 20445 IRE_REFRELE_NOTR(ire); 20446 20447 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20448 if (ire == NULL) 20449 goto noirefound; 20450 IRE_REFHOLD_NOTR(ire); 20451 20452 mutex_enter(&connp->conn_lock); 20453 if (CONN_CACHE_IRE(connp) && connp->conn_ire_cache == NULL) { 20454 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 20455 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20456 if (connp->conn_ulp == IPPROTO_TCP) 20457 TCP_CHECK_IREINFO(connp->conn_tcp, ire); 20458 connp->conn_ire_cache = ire; 20459 cached = B_TRUE; 20460 } 20461 rw_exit(&ire->ire_bucket->irb_lock); 20462 } 20463 mutex_exit(&connp->conn_lock); 20464 20465 /* 20466 * We can continue to use the ire but since it was 20467 * not cached, we should drop the extra reference. 20468 */ 20469 if (!cached) 20470 IRE_REFRELE_NOTR(ire); 20471 } 20472 20473 20474 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20475 "ip_wput_end: q %p (%S)", q, "end"); 20476 20477 /* 20478 * Check if the ire has the RTF_MULTIRT flag, inherited 20479 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20480 */ 20481 if (ire->ire_flags & RTF_MULTIRT) { 20482 20483 /* 20484 * Force the TTL of multirouted packets if required. 20485 * The TTL of such packets is bounded by the 20486 * ip_multirt_ttl ndd variable. 20487 */ 20488 if ((ipst->ips_ip_multirt_ttl > 0) && 20489 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20490 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20491 "(was %d), dst 0x%08x\n", 20492 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20493 ntohl(ire->ire_addr))); 20494 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20495 } 20496 20497 /* 20498 * At this point, we check to see if there are any pending 20499 * unresolved routes. ire_multirt_resolvable() 20500 * checks in O(n) that all IRE_OFFSUBNET ire 20501 * entries for the packet's destination and 20502 * flagged RTF_MULTIRT are currently resolved. 20503 * If some remain unresolved, we make a copy 20504 * of the current message. It will be used 20505 * to initiate additional route resolutions. 20506 */ 20507 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 20508 MBLK_GETLABEL(first_mp), ipst); 20509 ip2dbg(("ip_wput[not TCP]: ire %p, " 20510 "multirt_need_resolve %d, first_mp %p\n", 20511 (void *)ire, multirt_need_resolve, (void *)first_mp)); 20512 if (multirt_need_resolve) { 20513 copy_mp = copymsg(first_mp); 20514 if (copy_mp != NULL) { 20515 MULTIRT_DEBUG_TAG(copy_mp); 20516 } 20517 } 20518 } 20519 20520 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20521 20522 /* 20523 * Try to resolve another multiroute if 20524 * ire_multirt_resolvable() deemed it necessary 20525 */ 20526 if (copy_mp != NULL) 20527 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 20528 if (need_decref) 20529 CONN_DEC_REF(connp); 20530 return; 20531 20532 qnext: 20533 /* 20534 * Upper Level Protocols pass down complete IP datagrams 20535 * as M_DATA messages. Everything else is a sideshow. 20536 * 20537 * 1) We could be re-entering ip_wput because of ip_neworute 20538 * in which case we could have a IPSEC_OUT message. We 20539 * need to pass through ip_wput like other datagrams and 20540 * hence cannot branch to ip_wput_nondata. 20541 * 20542 * 2) ARP, AH, ESP, and other clients who are on the module 20543 * instance of IP stream, give us something to deal with. 20544 * We will handle AH and ESP here and rest in ip_wput_nondata. 20545 * 20546 * 3) ICMP replies also could come here. 20547 */ 20548 ipst = ILLQ_TO_IPST(q); 20549 20550 if (DB_TYPE(mp) != M_DATA) { 20551 notdata: 20552 if (DB_TYPE(mp) == M_CTL) { 20553 /* 20554 * M_CTL messages are used by ARP, AH and ESP to 20555 * communicate with IP. We deal with IPSEC_IN and 20556 * IPSEC_OUT here. ip_wput_nondata handles other 20557 * cases. 20558 */ 20559 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 20560 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 20561 first_mp = mp->b_cont; 20562 first_mp->b_flag &= ~MSGHASREF; 20563 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20564 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 20565 CONN_DEC_REF(connp); 20566 connp = NULL; 20567 } 20568 if (ii->ipsec_info_type == IPSEC_IN) { 20569 /* 20570 * Either this message goes back to 20571 * IPsec for further processing or to 20572 * ULP after policy checks. 20573 */ 20574 ip_fanout_proto_again(mp, NULL, NULL, NULL); 20575 return; 20576 } else if (ii->ipsec_info_type == IPSEC_OUT) { 20577 io = (ipsec_out_t *)ii; 20578 if (io->ipsec_out_proc_begin) { 20579 /* 20580 * IPsec processing has already started. 20581 * Complete it. 20582 * IPQoS notes: We don't care what is 20583 * in ipsec_out_ill_index since this 20584 * won't be processed for IPQoS policies 20585 * in ipsec_out_process. 20586 */ 20587 ipsec_out_process(q, mp, NULL, 20588 io->ipsec_out_ill_index); 20589 return; 20590 } else { 20591 connp = (q->q_next != NULL) ? 20592 NULL : Q_TO_CONN(q); 20593 first_mp = mp; 20594 mp = mp->b_cont; 20595 mctl_present = B_TRUE; 20596 } 20597 zoneid = io->ipsec_out_zoneid; 20598 ASSERT(zoneid != ALL_ZONES); 20599 } else if (ii->ipsec_info_type == IPSEC_CTL) { 20600 /* 20601 * It's an IPsec control message requesting 20602 * an SADB update to be sent to the IPsec 20603 * hardware acceleration capable ills. 20604 */ 20605 ipsec_ctl_t *ipsec_ctl = 20606 (ipsec_ctl_t *)mp->b_rptr; 20607 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 20608 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 20609 mblk_t *cmp = mp->b_cont; 20610 20611 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 20612 ASSERT(cmp != NULL); 20613 20614 freeb(mp); 20615 ill_ipsec_capab_send_all(satype, cmp, sa, 20616 ipst->ips_netstack); 20617 return; 20618 } else { 20619 /* 20620 * This must be ARP or special TSOL signaling. 20621 */ 20622 ip_wput_nondata(NULL, q, mp, NULL); 20623 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20624 "ip_wput_end: q %p (%S)", q, "nondata"); 20625 return; 20626 } 20627 } else { 20628 /* 20629 * This must be non-(ARP/AH/ESP) messages. 20630 */ 20631 ASSERT(!need_decref); 20632 ip_wput_nondata(NULL, q, mp, NULL); 20633 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20634 "ip_wput_end: q %p (%S)", q, "nondata"); 20635 return; 20636 } 20637 } else { 20638 first_mp = mp; 20639 mctl_present = B_FALSE; 20640 } 20641 20642 ASSERT(first_mp != NULL); 20643 /* 20644 * ICMP echo replies attach an ipsec_out and set ipsec_out_attach_if 20645 * to make sure that this packet goes out on the same interface it 20646 * came in. We handle that here. 20647 */ 20648 if (mctl_present) { 20649 uint_t ifindex; 20650 20651 io = (ipsec_out_t *)first_mp->b_rptr; 20652 if (io->ipsec_out_attach_if || io->ipsec_out_ip_nexthop) { 20653 /* 20654 * We may have lost the conn context if we are 20655 * coming here from ip_newroute(). Copy the 20656 * nexthop information. 20657 */ 20658 if (io->ipsec_out_ip_nexthop) { 20659 ip_nexthop = B_TRUE; 20660 nexthop_addr = io->ipsec_out_nexthop_addr; 20661 20662 ipha = (ipha_t *)mp->b_rptr; 20663 dst = ipha->ipha_dst; 20664 goto send_from_ill; 20665 } else { 20666 ASSERT(io->ipsec_out_ill_index != 0); 20667 ifindex = io->ipsec_out_ill_index; 20668 attach_ill = ill_lookup_on_ifindex(ifindex, 20669 B_FALSE, NULL, NULL, NULL, NULL, ipst); 20670 if (attach_ill == NULL) { 20671 ASSERT(xmit_ill == NULL); 20672 ip1dbg(("ip_output: bad ifindex for " 20673 "(BIND TO IPIF_NOFAILOVER) %d\n", 20674 ifindex)); 20675 freemsg(first_mp); 20676 BUMP_MIB(&ipst->ips_ip_mib, 20677 ipIfStatsOutDiscards); 20678 ASSERT(!need_decref); 20679 return; 20680 } 20681 } 20682 } 20683 } 20684 20685 ASSERT(xmit_ill == NULL); 20686 20687 /* We have a complete IP datagram heading outbound. */ 20688 ipha = (ipha_t *)mp->b_rptr; 20689 20690 #ifndef SPEED_BEFORE_SAFETY 20691 /* 20692 * Make sure we have a full-word aligned message and that at least 20693 * a simple IP header is accessible in the first message. If not, 20694 * try a pullup. 20695 */ 20696 if (!OK_32PTR(rptr) || 20697 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) { 20698 hdrtoosmall: 20699 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 20700 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20701 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 20702 if (first_mp == NULL) 20703 first_mp = mp; 20704 goto discard_pkt; 20705 } 20706 20707 /* This function assumes that mp points to an IPv4 packet. */ 20708 if (is_system_labeled() && q->q_next == NULL && 20709 (*mp->b_rptr & 0xf0) == (IPV4_VERSION << 4) && 20710 !connp->conn_ulp_labeled) { 20711 err = tsol_check_label(BEST_CRED(mp, connp), &mp, 20712 &adjust, connp->conn_mac_exempt, ipst); 20713 ipha = (ipha_t *)mp->b_rptr; 20714 if (first_mp != NULL) 20715 first_mp->b_cont = mp; 20716 if (err != 0) { 20717 if (first_mp == NULL) 20718 first_mp = mp; 20719 if (err == EINVAL) 20720 goto icmp_parameter_problem; 20721 ip2dbg(("ip_wput: label check failed (%d)\n", 20722 err)); 20723 goto discard_pkt; 20724 } 20725 iplen = ntohs(ipha->ipha_length) + adjust; 20726 ipha->ipha_length = htons(iplen); 20727 } 20728 20729 ipha = (ipha_t *)mp->b_rptr; 20730 if (first_mp == NULL) { 20731 ASSERT(attach_ill == NULL && xmit_ill == NULL); 20732 /* 20733 * If we got here because of "goto hdrtoosmall" 20734 * We need to attach a IPSEC_OUT. 20735 */ 20736 if (connp->conn_out_enforce_policy) { 20737 if (((mp = ipsec_attach_ipsec_out(&mp, connp, 20738 NULL, ipha->ipha_protocol, 20739 ipst->ips_netstack)) == NULL)) { 20740 BUMP_MIB(&ipst->ips_ip_mib, 20741 ipIfStatsOutDiscards); 20742 if (need_decref) 20743 CONN_DEC_REF(connp); 20744 return; 20745 } else { 20746 ASSERT(mp->b_datap->db_type == M_CTL); 20747 first_mp = mp; 20748 mp = mp->b_cont; 20749 mctl_present = B_TRUE; 20750 } 20751 } else { 20752 first_mp = mp; 20753 mctl_present = B_FALSE; 20754 } 20755 } 20756 } 20757 #endif 20758 20759 /* Most of the code below is written for speed, not readability */ 20760 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20761 20762 /* 20763 * If ip_newroute() fails, we're going to need a full 20764 * header for the icmp wraparound. 20765 */ 20766 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 20767 uint_t v_hlen; 20768 version_hdrlen_check: 20769 ASSERT(first_mp != NULL); 20770 v_hlen = V_HLEN; 20771 /* 20772 * siphon off IPv6 packets coming down from transport 20773 * layer modules here. 20774 * Note: high-order bit carries NUD reachability confirmation 20775 */ 20776 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 20777 /* 20778 * FIXME: assume that callers of ip_output* call 20779 * the right version? 20780 */ 20781 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutWrongIPVersion); 20782 ASSERT(xmit_ill == NULL); 20783 if (attach_ill != NULL) 20784 ill_refrele(attach_ill); 20785 if (need_decref) 20786 mp->b_flag |= MSGHASREF; 20787 (void) ip_output_v6(arg, first_mp, arg2, caller); 20788 return; 20789 } 20790 20791 if ((v_hlen >> 4) != IP_VERSION) { 20792 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20793 "ip_wput_end: q %p (%S)", q, "badvers"); 20794 goto discard_pkt; 20795 } 20796 /* 20797 * Is the header length at least 20 bytes? 20798 * 20799 * Are there enough bytes accessible in the header? If 20800 * not, try a pullup. 20801 */ 20802 v_hlen &= 0xF; 20803 v_hlen <<= 2; 20804 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 20805 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20806 "ip_wput_end: q %p (%S)", q, "badlen"); 20807 goto discard_pkt; 20808 } 20809 if (v_hlen > (mp->b_wptr - rptr)) { 20810 if (!pullupmsg(mp, v_hlen)) { 20811 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20812 "ip_wput_end: q %p (%S)", q, "badpullup2"); 20813 goto discard_pkt; 20814 } 20815 ipha = (ipha_t *)mp->b_rptr; 20816 } 20817 /* 20818 * Move first entry from any source route into ipha_dst and 20819 * verify the options 20820 */ 20821 if (ip_wput_options(q, first_mp, ipha, mctl_present, 20822 zoneid, ipst)) { 20823 ASSERT(xmit_ill == NULL); 20824 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20825 if (attach_ill != NULL) 20826 ill_refrele(attach_ill); 20827 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20828 "ip_wput_end: q %p (%S)", q, "badopts"); 20829 if (need_decref) 20830 CONN_DEC_REF(connp); 20831 return; 20832 } 20833 } 20834 dst = ipha->ipha_dst; 20835 20836 /* 20837 * Try to get an IRE_CACHE for the destination address. If we can't, 20838 * we have to run the packet through ip_newroute which will take 20839 * the appropriate action to arrange for an IRE_CACHE, such as querying 20840 * a resolver, or assigning a default gateway, etc. 20841 */ 20842 if (CLASSD(dst)) { 20843 ipif_t *ipif; 20844 uint32_t setsrc = 0; 20845 20846 multicast: 20847 ASSERT(first_mp != NULL); 20848 ip2dbg(("ip_wput: CLASSD\n")); 20849 if (connp == NULL) { 20850 /* 20851 * Use the first good ipif on the ill. 20852 * XXX Should this ever happen? (Appears 20853 * to show up with just ppp and no ethernet due 20854 * to in.rdisc.) 20855 * However, ire_send should be able to 20856 * call ip_wput_ire directly. 20857 * 20858 * XXX Also, this can happen for ICMP and other packets 20859 * with multicast source addresses. Perhaps we should 20860 * fix things so that we drop the packet in question, 20861 * but for now, just run with it. 20862 */ 20863 ill_t *ill = (ill_t *)q->q_ptr; 20864 20865 /* 20866 * Don't honor attach_if for this case. If ill 20867 * is part of the group, ipif could belong to 20868 * any ill and we cannot maintain attach_ill 20869 * and ipif_ill same anymore and the assert 20870 * below would fail. 20871 */ 20872 if (mctl_present && io->ipsec_out_attach_if) { 20873 io->ipsec_out_ill_index = 0; 20874 io->ipsec_out_attach_if = B_FALSE; 20875 ASSERT(attach_ill != NULL); 20876 ill_refrele(attach_ill); 20877 attach_ill = NULL; 20878 } 20879 20880 ASSERT(attach_ill == NULL); 20881 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 20882 if (ipif == NULL) { 20883 if (need_decref) 20884 CONN_DEC_REF(connp); 20885 freemsg(first_mp); 20886 return; 20887 } 20888 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 20889 ntohl(dst), ill->ill_name)); 20890 } else { 20891 /* 20892 * The order of precedence is IP_BOUND_IF, IP_PKTINFO 20893 * and IP_MULTICAST_IF. The block comment above this 20894 * function explains the locking mechanism used here. 20895 */ 20896 if (xmit_ill == NULL) { 20897 xmit_ill = conn_get_held_ill(connp, 20898 &connp->conn_outgoing_ill, &err); 20899 if (err == ILL_LOOKUP_FAILED) { 20900 ip1dbg(("ip_wput: No ill for " 20901 "IP_BOUND_IF\n")); 20902 BUMP_MIB(&ipst->ips_ip_mib, 20903 ipIfStatsOutNoRoutes); 20904 goto drop_pkt; 20905 } 20906 } 20907 20908 if (xmit_ill == NULL) { 20909 ipif = conn_get_held_ipif(connp, 20910 &connp->conn_multicast_ipif, &err); 20911 if (err == IPIF_LOOKUP_FAILED) { 20912 ip1dbg(("ip_wput: No ipif for " 20913 "multicast\n")); 20914 BUMP_MIB(&ipst->ips_ip_mib, 20915 ipIfStatsOutNoRoutes); 20916 goto drop_pkt; 20917 } 20918 } 20919 if (xmit_ill != NULL) { 20920 ipif = ipif_get_next_ipif(NULL, xmit_ill); 20921 if (ipif == NULL) { 20922 ip1dbg(("ip_wput: No ipif for " 20923 "xmit_ill\n")); 20924 BUMP_MIB(&ipst->ips_ip_mib, 20925 ipIfStatsOutNoRoutes); 20926 goto drop_pkt; 20927 } 20928 } else if (ipif == NULL || ipif->ipif_isv6) { 20929 /* 20930 * We must do this ipif determination here 20931 * else we could pass through ip_newroute 20932 * and come back here without the conn context. 20933 * 20934 * Note: we do late binding i.e. we bind to 20935 * the interface when the first packet is sent. 20936 * For performance reasons we do not rebind on 20937 * each packet but keep the binding until the 20938 * next IP_MULTICAST_IF option. 20939 * 20940 * conn_multicast_{ipif,ill} are shared between 20941 * IPv4 and IPv6 and AF_INET6 sockets can 20942 * send both IPv4 and IPv6 packets. Hence 20943 * we have to check that "isv6" matches above. 20944 */ 20945 if (ipif != NULL) 20946 ipif_refrele(ipif); 20947 ipif = ipif_lookup_group(dst, zoneid, ipst); 20948 if (ipif == NULL) { 20949 ip1dbg(("ip_wput: No ipif for " 20950 "multicast\n")); 20951 BUMP_MIB(&ipst->ips_ip_mib, 20952 ipIfStatsOutNoRoutes); 20953 goto drop_pkt; 20954 } 20955 err = conn_set_held_ipif(connp, 20956 &connp->conn_multicast_ipif, ipif); 20957 if (err == IPIF_LOOKUP_FAILED) { 20958 ipif_refrele(ipif); 20959 ip1dbg(("ip_wput: No ipif for " 20960 "multicast\n")); 20961 BUMP_MIB(&ipst->ips_ip_mib, 20962 ipIfStatsOutNoRoutes); 20963 goto drop_pkt; 20964 } 20965 } 20966 } 20967 ASSERT(!ipif->ipif_isv6); 20968 /* 20969 * As we may lose the conn by the time we reach ip_wput_ire, 20970 * we copy conn_multicast_loop and conn_dontroute on to an 20971 * ipsec_out. In case if this datagram goes out secure, 20972 * we need the ill_index also. Copy that also into the 20973 * ipsec_out. 20974 */ 20975 if (mctl_present) { 20976 io = (ipsec_out_t *)first_mp->b_rptr; 20977 ASSERT(first_mp->b_datap->db_type == M_CTL); 20978 ASSERT(io->ipsec_out_type == IPSEC_OUT); 20979 } else { 20980 ASSERT(mp == first_mp); 20981 if ((first_mp = allocb(sizeof (ipsec_info_t), 20982 BPRI_HI)) == NULL) { 20983 ipif_refrele(ipif); 20984 first_mp = mp; 20985 goto discard_pkt; 20986 } 20987 first_mp->b_datap->db_type = M_CTL; 20988 first_mp->b_wptr += sizeof (ipsec_info_t); 20989 /* ipsec_out_secure is B_FALSE now */ 20990 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 20991 io = (ipsec_out_t *)first_mp->b_rptr; 20992 io->ipsec_out_type = IPSEC_OUT; 20993 io->ipsec_out_len = sizeof (ipsec_out_t); 20994 io->ipsec_out_use_global_policy = B_TRUE; 20995 io->ipsec_out_ns = ipst->ips_netstack; 20996 first_mp->b_cont = mp; 20997 mctl_present = B_TRUE; 20998 } 20999 if (attach_ill != NULL) { 21000 ASSERT(attach_ill == ipif->ipif_ill); 21001 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21002 21003 /* 21004 * Check if we need an ire that will not be 21005 * looked up by anybody else i.e. HIDDEN. 21006 */ 21007 if (ill_is_probeonly(attach_ill)) { 21008 match_flags |= MATCH_IRE_MARK_HIDDEN; 21009 } 21010 io->ipsec_out_ill_index = 21011 attach_ill->ill_phyint->phyint_ifindex; 21012 io->ipsec_out_attach_if = B_TRUE; 21013 } else { 21014 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 21015 io->ipsec_out_ill_index = 21016 ipif->ipif_ill->ill_phyint->phyint_ifindex; 21017 } 21018 if (connp != NULL) { 21019 io->ipsec_out_multicast_loop = 21020 connp->conn_multicast_loop; 21021 io->ipsec_out_dontroute = connp->conn_dontroute; 21022 io->ipsec_out_zoneid = connp->conn_zoneid; 21023 } 21024 /* 21025 * If the application uses IP_MULTICAST_IF with 21026 * different logical addresses of the same ILL, we 21027 * need to make sure that the soruce address of 21028 * the packet matches the logical IP address used 21029 * in the option. We do it by initializing ipha_src 21030 * here. This should keep IPsec also happy as 21031 * when we return from IPsec processing, we don't 21032 * have to worry about getting the right address on 21033 * the packet. Thus it is sufficient to look for 21034 * IRE_CACHE using MATCH_IRE_ILL rathen than 21035 * MATCH_IRE_IPIF. 21036 * 21037 * NOTE : We need to do it for non-secure case also as 21038 * this might go out secure if there is a global policy 21039 * match in ip_wput_ire. For bind to IPIF_NOFAILOVER 21040 * address, the source should be initialized already and 21041 * hence we won't be initializing here. 21042 * 21043 * As we do not have the ire yet, it is possible that 21044 * we set the source address here and then later discover 21045 * that the ire implies the source address to be assigned 21046 * through the RTF_SETSRC flag. 21047 * In that case, the setsrc variable will remind us 21048 * that overwritting the source address by the one 21049 * of the RTF_SETSRC-flagged ire is allowed. 21050 */ 21051 if (ipha->ipha_src == INADDR_ANY && 21052 (connp == NULL || !connp->conn_unspec_src)) { 21053 ipha->ipha_src = ipif->ipif_src_addr; 21054 setsrc = RTF_SETSRC; 21055 } 21056 /* 21057 * Find an IRE which matches the destination and the outgoing 21058 * queue (i.e. the outgoing interface.) 21059 * For loopback use a unicast IP address for 21060 * the ire lookup. 21061 */ 21062 if (IS_LOOPBACK(ipif->ipif_ill)) 21063 dst = ipif->ipif_lcl_addr; 21064 21065 /* 21066 * If xmit_ill is set, we branch out to ip_newroute_ipif. 21067 * We don't need to lookup ire in ctable as the packet 21068 * needs to be sent to the destination through the specified 21069 * ill irrespective of ires in the cache table. 21070 */ 21071 ire = NULL; 21072 if (xmit_ill == NULL) { 21073 ire = ire_ctable_lookup(dst, 0, 0, ipif, 21074 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21075 } 21076 21077 /* 21078 * refrele attach_ill as its not needed anymore. 21079 */ 21080 if (attach_ill != NULL) { 21081 ill_refrele(attach_ill); 21082 attach_ill = NULL; 21083 } 21084 21085 if (ire == NULL) { 21086 /* 21087 * Multicast loopback and multicast forwarding is 21088 * done in ip_wput_ire. 21089 * 21090 * Mark this packet to make it be delivered to 21091 * ip_wput_ire after the new ire has been 21092 * created. 21093 * 21094 * The call to ip_newroute_ipif takes into account 21095 * the setsrc reminder. In any case, we take care 21096 * of the RTF_MULTIRT flag. 21097 */ 21098 mp->b_prev = mp->b_next = NULL; 21099 if (xmit_ill == NULL || 21100 xmit_ill->ill_ipif_up_count > 0) { 21101 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 21102 setsrc | RTF_MULTIRT, zoneid, infop); 21103 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21104 "ip_wput_end: q %p (%S)", q, "noire"); 21105 } else { 21106 freemsg(first_mp); 21107 } 21108 ipif_refrele(ipif); 21109 if (xmit_ill != NULL) 21110 ill_refrele(xmit_ill); 21111 if (need_decref) 21112 CONN_DEC_REF(connp); 21113 return; 21114 } 21115 21116 ipif_refrele(ipif); 21117 ipif = NULL; 21118 ASSERT(xmit_ill == NULL); 21119 21120 /* 21121 * Honor the RTF_SETSRC flag for multicast packets, 21122 * if allowed by the setsrc reminder. 21123 */ 21124 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 21125 ipha->ipha_src = ire->ire_src_addr; 21126 } 21127 21128 /* 21129 * Unconditionally force the TTL to 1 for 21130 * multirouted multicast packets: 21131 * multirouted multicast should not cross 21132 * multicast routers. 21133 */ 21134 if (ire->ire_flags & RTF_MULTIRT) { 21135 if (ipha->ipha_ttl > 1) { 21136 ip2dbg(("ip_wput: forcing multicast " 21137 "multirt TTL to 1 (was %d), dst 0x%08x\n", 21138 ipha->ipha_ttl, ntohl(ire->ire_addr))); 21139 ipha->ipha_ttl = 1; 21140 } 21141 } 21142 } else { 21143 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 21144 if ((ire != NULL) && (ire->ire_type & 21145 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 21146 ignore_dontroute = B_TRUE; 21147 ignore_nexthop = B_TRUE; 21148 } 21149 if (ire != NULL) { 21150 ire_refrele(ire); 21151 ire = NULL; 21152 } 21153 /* 21154 * Guard against coming in from arp in which case conn is NULL. 21155 * Also guard against non M_DATA with dontroute set but 21156 * destined to local, loopback or broadcast addresses. 21157 */ 21158 if (connp != NULL && connp->conn_dontroute && 21159 !ignore_dontroute) { 21160 dontroute: 21161 /* 21162 * Set TTL to 1 if SO_DONTROUTE is set to prevent 21163 * routing protocols from seeing false direct 21164 * connectivity. 21165 */ 21166 ipha->ipha_ttl = 1; 21167 21168 /* If suitable ipif not found, drop packet */ 21169 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid, ipst); 21170 if (dst_ipif == NULL) { 21171 noroute: 21172 ip1dbg(("ip_wput: no route for dst using" 21173 " SO_DONTROUTE\n")); 21174 BUMP_MIB(&ipst->ips_ip_mib, 21175 ipIfStatsOutNoRoutes); 21176 mp->b_prev = mp->b_next = NULL; 21177 if (first_mp == NULL) 21178 first_mp = mp; 21179 goto drop_pkt; 21180 } else { 21181 /* 21182 * If suitable ipif has been found, set 21183 * xmit_ill to the corresponding 21184 * ipif_ill because we'll be using the 21185 * send_from_ill logic below. 21186 */ 21187 ASSERT(xmit_ill == NULL); 21188 xmit_ill = dst_ipif->ipif_ill; 21189 mutex_enter(&xmit_ill->ill_lock); 21190 if (!ILL_CAN_LOOKUP(xmit_ill)) { 21191 mutex_exit(&xmit_ill->ill_lock); 21192 xmit_ill = NULL; 21193 ipif_refrele(dst_ipif); 21194 goto noroute; 21195 } 21196 ill_refhold_locked(xmit_ill); 21197 mutex_exit(&xmit_ill->ill_lock); 21198 ipif_refrele(dst_ipif); 21199 } 21200 } 21201 /* 21202 * If we are bound to IPIF_NOFAILOVER address, look for 21203 * an IRE_CACHE matching the ill. 21204 */ 21205 send_from_ill: 21206 if (attach_ill != NULL) { 21207 ipif_t *attach_ipif; 21208 21209 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21210 21211 /* 21212 * Check if we need an ire that will not be 21213 * looked up by anybody else i.e. HIDDEN. 21214 */ 21215 if (ill_is_probeonly(attach_ill)) { 21216 match_flags |= MATCH_IRE_MARK_HIDDEN; 21217 } 21218 21219 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 21220 if (attach_ipif == NULL) { 21221 ip1dbg(("ip_wput: No ipif for attach_ill\n")); 21222 goto discard_pkt; 21223 } 21224 ire = ire_ctable_lookup(dst, 0, 0, attach_ipif, 21225 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21226 ipif_refrele(attach_ipif); 21227 } else if (xmit_ill != NULL) { 21228 ipif_t *ipif; 21229 21230 /* 21231 * Mark this packet as originated locally 21232 */ 21233 mp->b_prev = mp->b_next = NULL; 21234 21235 /* 21236 * Could be SO_DONTROUTE case also. 21237 * Verify that at least one ipif is up on the ill. 21238 */ 21239 if (xmit_ill->ill_ipif_up_count == 0) { 21240 ip1dbg(("ip_output: xmit_ill %s is down\n", 21241 xmit_ill->ill_name)); 21242 goto drop_pkt; 21243 } 21244 21245 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21246 if (ipif == NULL) { 21247 ip1dbg(("ip_output: xmit_ill %s NULL ipif\n", 21248 xmit_ill->ill_name)); 21249 goto drop_pkt; 21250 } 21251 21252 /* 21253 * Look for a ire that is part of the group, 21254 * if found use it else call ip_newroute_ipif. 21255 * IPCL_ZONEID is not used for matching because 21256 * IP_ALLZONES option is valid only when the 21257 * ill is accessible from all zones i.e has a 21258 * valid ipif in all zones. 21259 */ 21260 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 21261 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 21262 MBLK_GETLABEL(mp), match_flags, ipst); 21263 /* 21264 * If an ire exists use it or else create 21265 * an ire but don't add it to the cache. 21266 * Adding an ire may cause issues with 21267 * asymmetric routing. 21268 * In case of multiroute always act as if 21269 * ire does not exist. 21270 */ 21271 if (ire == NULL || ire->ire_flags & RTF_MULTIRT) { 21272 if (ire != NULL) 21273 ire_refrele(ire); 21274 ip_newroute_ipif(q, first_mp, ipif, 21275 dst, connp, 0, zoneid, infop); 21276 ipif_refrele(ipif); 21277 ip1dbg(("ip_output: xmit_ill via %s\n", 21278 xmit_ill->ill_name)); 21279 ill_refrele(xmit_ill); 21280 if (need_decref) 21281 CONN_DEC_REF(connp); 21282 return; 21283 } 21284 ipif_refrele(ipif); 21285 } else if (ip_nexthop || (connp != NULL && 21286 (connp->conn_nexthop_set)) && !ignore_nexthop) { 21287 if (!ip_nexthop) { 21288 ip_nexthop = B_TRUE; 21289 nexthop_addr = connp->conn_nexthop_v4; 21290 } 21291 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 21292 MATCH_IRE_GW; 21293 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 21294 NULL, zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21295 } else { 21296 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), 21297 ipst); 21298 } 21299 if (!ire) { 21300 /* 21301 * Make sure we don't load spread if this 21302 * is IPIF_NOFAILOVER case. 21303 */ 21304 if ((attach_ill != NULL) || 21305 (ip_nexthop && !ignore_nexthop)) { 21306 if (mctl_present) { 21307 io = (ipsec_out_t *)first_mp->b_rptr; 21308 ASSERT(first_mp->b_datap->db_type == 21309 M_CTL); 21310 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21311 } else { 21312 ASSERT(mp == first_mp); 21313 first_mp = allocb( 21314 sizeof (ipsec_info_t), BPRI_HI); 21315 if (first_mp == NULL) { 21316 first_mp = mp; 21317 goto discard_pkt; 21318 } 21319 first_mp->b_datap->db_type = M_CTL; 21320 first_mp->b_wptr += 21321 sizeof (ipsec_info_t); 21322 /* ipsec_out_secure is B_FALSE now */ 21323 bzero(first_mp->b_rptr, 21324 sizeof (ipsec_info_t)); 21325 io = (ipsec_out_t *)first_mp->b_rptr; 21326 io->ipsec_out_type = IPSEC_OUT; 21327 io->ipsec_out_len = 21328 sizeof (ipsec_out_t); 21329 io->ipsec_out_use_global_policy = 21330 B_TRUE; 21331 io->ipsec_out_ns = ipst->ips_netstack; 21332 first_mp->b_cont = mp; 21333 mctl_present = B_TRUE; 21334 } 21335 if (attach_ill != NULL) { 21336 io->ipsec_out_ill_index = attach_ill-> 21337 ill_phyint->phyint_ifindex; 21338 io->ipsec_out_attach_if = B_TRUE; 21339 } else { 21340 io->ipsec_out_ip_nexthop = ip_nexthop; 21341 io->ipsec_out_nexthop_addr = 21342 nexthop_addr; 21343 } 21344 } 21345 noirefound: 21346 /* 21347 * Mark this packet as having originated on 21348 * this machine. This will be noted in 21349 * ire_add_then_send, which needs to know 21350 * whether to run it back through ip_wput or 21351 * ip_rput following successful resolution. 21352 */ 21353 mp->b_prev = NULL; 21354 mp->b_next = NULL; 21355 ip_newroute(q, first_mp, dst, connp, zoneid, ipst); 21356 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21357 "ip_wput_end: q %p (%S)", q, "newroute"); 21358 if (attach_ill != NULL) 21359 ill_refrele(attach_ill); 21360 if (xmit_ill != NULL) 21361 ill_refrele(xmit_ill); 21362 if (need_decref) 21363 CONN_DEC_REF(connp); 21364 return; 21365 } 21366 } 21367 21368 /* We now know where we are going with it. */ 21369 21370 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21371 "ip_wput_end: q %p (%S)", q, "end"); 21372 21373 /* 21374 * Check if the ire has the RTF_MULTIRT flag, inherited 21375 * from an IRE_OFFSUBNET ire entry in ip_newroute. 21376 */ 21377 if (ire->ire_flags & RTF_MULTIRT) { 21378 /* 21379 * Force the TTL of multirouted packets if required. 21380 * The TTL of such packets is bounded by the 21381 * ip_multirt_ttl ndd variable. 21382 */ 21383 if ((ipst->ips_ip_multirt_ttl > 0) && 21384 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 21385 ip2dbg(("ip_wput: forcing multirt TTL to %d " 21386 "(was %d), dst 0x%08x\n", 21387 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 21388 ntohl(ire->ire_addr))); 21389 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 21390 } 21391 /* 21392 * At this point, we check to see if there are any pending 21393 * unresolved routes. ire_multirt_resolvable() 21394 * checks in O(n) that all IRE_OFFSUBNET ire 21395 * entries for the packet's destination and 21396 * flagged RTF_MULTIRT are currently resolved. 21397 * If some remain unresolved, we make a copy 21398 * of the current message. It will be used 21399 * to initiate additional route resolutions. 21400 */ 21401 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 21402 MBLK_GETLABEL(first_mp), ipst); 21403 ip2dbg(("ip_wput[noirefound]: ire %p, " 21404 "multirt_need_resolve %d, first_mp %p\n", 21405 (void *)ire, multirt_need_resolve, (void *)first_mp)); 21406 if (multirt_need_resolve) { 21407 copy_mp = copymsg(first_mp); 21408 if (copy_mp != NULL) { 21409 MULTIRT_DEBUG_TAG(copy_mp); 21410 } 21411 } 21412 } 21413 21414 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 21415 /* 21416 * Try to resolve another multiroute if 21417 * ire_multirt_resolvable() deemed it necessary. 21418 * At this point, we need to distinguish 21419 * multicasts from other packets. For multicasts, 21420 * we call ip_newroute_ipif() and request that both 21421 * multirouting and setsrc flags are checked. 21422 */ 21423 if (copy_mp != NULL) { 21424 if (CLASSD(dst)) { 21425 ipif_t *ipif = ipif_lookup_group(dst, zoneid, ipst); 21426 if (ipif) { 21427 ASSERT(infop->ip_opt_ill_index == 0); 21428 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 21429 RTF_SETSRC | RTF_MULTIRT, zoneid, infop); 21430 ipif_refrele(ipif); 21431 } else { 21432 MULTIRT_DEBUG_UNTAG(copy_mp); 21433 freemsg(copy_mp); 21434 copy_mp = NULL; 21435 } 21436 } else { 21437 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 21438 } 21439 } 21440 if (attach_ill != NULL) 21441 ill_refrele(attach_ill); 21442 if (xmit_ill != NULL) 21443 ill_refrele(xmit_ill); 21444 if (need_decref) 21445 CONN_DEC_REF(connp); 21446 return; 21447 21448 icmp_parameter_problem: 21449 /* could not have originated externally */ 21450 ASSERT(mp->b_prev == NULL); 21451 if (ip_hdr_complete(ipha, zoneid, ipst) == 0) { 21452 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 21453 /* it's the IP header length that's in trouble */ 21454 icmp_param_problem(q, first_mp, 0, zoneid, ipst); 21455 first_mp = NULL; 21456 } 21457 21458 discard_pkt: 21459 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21460 drop_pkt: 21461 ip1dbg(("ip_wput: dropped packet\n")); 21462 if (ire != NULL) 21463 ire_refrele(ire); 21464 if (need_decref) 21465 CONN_DEC_REF(connp); 21466 freemsg(first_mp); 21467 if (attach_ill != NULL) 21468 ill_refrele(attach_ill); 21469 if (xmit_ill != NULL) 21470 ill_refrele(xmit_ill); 21471 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21472 "ip_wput_end: q %p (%S)", q, "droppkt"); 21473 } 21474 21475 /* 21476 * If this is a conn_t queue, then we pass in the conn. This includes the 21477 * zoneid. 21478 * Otherwise, this is a message coming back from ARP or for an ill_t queue, 21479 * in which case we use the global zoneid since those are all part of 21480 * the global zone. 21481 */ 21482 void 21483 ip_wput(queue_t *q, mblk_t *mp) 21484 { 21485 if (CONN_Q(q)) 21486 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 21487 else 21488 ip_output(GLOBAL_ZONEID, mp, q, IP_WPUT); 21489 } 21490 21491 /* 21492 * 21493 * The following rules must be observed when accessing any ipif or ill 21494 * that has been cached in the conn. Typically conn_nofailover_ill, 21495 * conn_outgoing_ill, conn_multicast_ipif and conn_multicast_ill. 21496 * 21497 * Access: The ipif or ill pointed to from the conn can be accessed under 21498 * the protection of the conn_lock or after it has been refheld under the 21499 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 21500 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 21501 * The reason for this is that a concurrent unplumb could actually be 21502 * cleaning up these cached pointers by walking the conns and might have 21503 * finished cleaning up the conn in question. The macros check that an 21504 * unplumb has not yet started on the ipif or ill. 21505 * 21506 * Caching: An ipif or ill pointer may be cached in the conn only after 21507 * making sure that an unplumb has not started. So the caching is done 21508 * while holding both the conn_lock and the ill_lock and after using the 21509 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 21510 * flag before starting the cleanup of conns. 21511 * 21512 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 21513 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 21514 * or a reference to the ipif or a reference to an ire that references the 21515 * ipif. An ipif does not change its ill except for failover/failback. Since 21516 * failover/failback happens only after bringing down the ipif and making sure 21517 * the ipif refcnt has gone to zero and holding the ill_g_lock and ill_lock 21518 * the above holds. 21519 */ 21520 ipif_t * 21521 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 21522 { 21523 ipif_t *ipif; 21524 ill_t *ill; 21525 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 21526 21527 *err = 0; 21528 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 21529 mutex_enter(&connp->conn_lock); 21530 ipif = *ipifp; 21531 if (ipif != NULL) { 21532 ill = ipif->ipif_ill; 21533 mutex_enter(&ill->ill_lock); 21534 if (IPIF_CAN_LOOKUP(ipif)) { 21535 ipif_refhold_locked(ipif); 21536 mutex_exit(&ill->ill_lock); 21537 mutex_exit(&connp->conn_lock); 21538 rw_exit(&ipst->ips_ill_g_lock); 21539 return (ipif); 21540 } else { 21541 *err = IPIF_LOOKUP_FAILED; 21542 } 21543 mutex_exit(&ill->ill_lock); 21544 } 21545 mutex_exit(&connp->conn_lock); 21546 rw_exit(&ipst->ips_ill_g_lock); 21547 return (NULL); 21548 } 21549 21550 ill_t * 21551 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 21552 { 21553 ill_t *ill; 21554 21555 *err = 0; 21556 mutex_enter(&connp->conn_lock); 21557 ill = *illp; 21558 if (ill != NULL) { 21559 mutex_enter(&ill->ill_lock); 21560 if (ILL_CAN_LOOKUP(ill)) { 21561 ill_refhold_locked(ill); 21562 mutex_exit(&ill->ill_lock); 21563 mutex_exit(&connp->conn_lock); 21564 return (ill); 21565 } else { 21566 *err = ILL_LOOKUP_FAILED; 21567 } 21568 mutex_exit(&ill->ill_lock); 21569 } 21570 mutex_exit(&connp->conn_lock); 21571 return (NULL); 21572 } 21573 21574 static int 21575 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 21576 { 21577 ill_t *ill; 21578 21579 ill = ipif->ipif_ill; 21580 mutex_enter(&connp->conn_lock); 21581 mutex_enter(&ill->ill_lock); 21582 if (IPIF_CAN_LOOKUP(ipif)) { 21583 *ipifp = ipif; 21584 mutex_exit(&ill->ill_lock); 21585 mutex_exit(&connp->conn_lock); 21586 return (0); 21587 } 21588 mutex_exit(&ill->ill_lock); 21589 mutex_exit(&connp->conn_lock); 21590 return (IPIF_LOOKUP_FAILED); 21591 } 21592 21593 /* 21594 * This is called if the outbound datagram needs fragmentation. 21595 * 21596 * NOTE : This function does not ire_refrele the ire argument passed in. 21597 */ 21598 static void 21599 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire, zoneid_t zoneid, 21600 ip_stack_t *ipst) 21601 { 21602 ipha_t *ipha; 21603 mblk_t *mp; 21604 uint32_t v_hlen_tos_len; 21605 uint32_t max_frag; 21606 uint32_t frag_flag; 21607 boolean_t dont_use; 21608 21609 if (ipsec_mp->b_datap->db_type == M_CTL) { 21610 mp = ipsec_mp->b_cont; 21611 } else { 21612 mp = ipsec_mp; 21613 } 21614 21615 ipha = (ipha_t *)mp->b_rptr; 21616 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21617 21618 #ifdef _BIG_ENDIAN 21619 #define V_HLEN (v_hlen_tos_len >> 24) 21620 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21621 #else 21622 #define V_HLEN (v_hlen_tos_len & 0xFF) 21623 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21624 #endif 21625 21626 #ifndef SPEED_BEFORE_SAFETY 21627 /* 21628 * Check that ipha_length is consistent with 21629 * the mblk length 21630 */ 21631 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 21632 ip0dbg(("Packet length mismatch: %d, %ld\n", 21633 LENGTH, msgdsize(mp))); 21634 freemsg(ipsec_mp); 21635 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21636 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 21637 "packet length mismatch"); 21638 return; 21639 } 21640 #endif 21641 /* 21642 * Don't use frag_flag if pre-built packet or source 21643 * routed or if multicast (since multicast packets do not solicit 21644 * ICMP "packet too big" messages). Get the values of 21645 * max_frag and frag_flag atomically by acquiring the 21646 * ire_lock. 21647 */ 21648 mutex_enter(&ire->ire_lock); 21649 max_frag = ire->ire_max_frag; 21650 frag_flag = ire->ire_frag_flag; 21651 mutex_exit(&ire->ire_lock); 21652 21653 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 21654 (V_HLEN != IP_SIMPLE_HDR_VERSION && 21655 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 21656 21657 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 21658 (dont_use ? 0 : frag_flag), zoneid, ipst); 21659 } 21660 21661 /* 21662 * Used for deciding the MSS size for the upper layer. Thus 21663 * we need to check the outbound policy values in the conn. 21664 */ 21665 int 21666 conn_ipsec_length(conn_t *connp) 21667 { 21668 ipsec_latch_t *ipl; 21669 21670 ipl = connp->conn_latch; 21671 if (ipl == NULL) 21672 return (0); 21673 21674 if (ipl->ipl_out_policy == NULL) 21675 return (0); 21676 21677 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 21678 } 21679 21680 /* 21681 * Returns an estimate of the IPsec headers size. This is used if 21682 * we don't want to call into IPsec to get the exact size. 21683 */ 21684 int 21685 ipsec_out_extra_length(mblk_t *ipsec_mp) 21686 { 21687 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21688 ipsec_action_t *a; 21689 21690 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21691 if (!io->ipsec_out_secure) 21692 return (0); 21693 21694 a = io->ipsec_out_act; 21695 21696 if (a == NULL) { 21697 ASSERT(io->ipsec_out_policy != NULL); 21698 a = io->ipsec_out_policy->ipsp_act; 21699 } 21700 ASSERT(a != NULL); 21701 21702 return (a->ipa_ovhd); 21703 } 21704 21705 /* 21706 * Returns an estimate of the IPsec headers size. This is used if 21707 * we don't want to call into IPsec to get the exact size. 21708 */ 21709 int 21710 ipsec_in_extra_length(mblk_t *ipsec_mp) 21711 { 21712 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21713 ipsec_action_t *a; 21714 21715 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21716 21717 a = ii->ipsec_in_action; 21718 return (a == NULL ? 0 : a->ipa_ovhd); 21719 } 21720 21721 /* 21722 * If there are any source route options, return the true final 21723 * destination. Otherwise, return the destination. 21724 */ 21725 ipaddr_t 21726 ip_get_dst(ipha_t *ipha) 21727 { 21728 ipoptp_t opts; 21729 uchar_t *opt; 21730 uint8_t optval; 21731 uint8_t optlen; 21732 ipaddr_t dst; 21733 uint32_t off; 21734 21735 dst = ipha->ipha_dst; 21736 21737 if (IS_SIMPLE_IPH(ipha)) 21738 return (dst); 21739 21740 for (optval = ipoptp_first(&opts, ipha); 21741 optval != IPOPT_EOL; 21742 optval = ipoptp_next(&opts)) { 21743 opt = opts.ipoptp_cur; 21744 optlen = opts.ipoptp_len; 21745 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 21746 switch (optval) { 21747 case IPOPT_SSRR: 21748 case IPOPT_LSRR: 21749 off = opt[IPOPT_OFFSET]; 21750 /* 21751 * If one of the conditions is true, it means 21752 * end of options and dst already has the right 21753 * value. 21754 */ 21755 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 21756 off = optlen - IP_ADDR_LEN; 21757 bcopy(&opt[off], &dst, IP_ADDR_LEN); 21758 } 21759 return (dst); 21760 default: 21761 break; 21762 } 21763 } 21764 21765 return (dst); 21766 } 21767 21768 mblk_t * 21769 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 21770 conn_t *connp, boolean_t unspec_src, zoneid_t zoneid) 21771 { 21772 ipsec_out_t *io; 21773 mblk_t *first_mp; 21774 boolean_t policy_present; 21775 ip_stack_t *ipst; 21776 ipsec_stack_t *ipss; 21777 21778 ASSERT(ire != NULL); 21779 ipst = ire->ire_ipst; 21780 ipss = ipst->ips_netstack->netstack_ipsec; 21781 21782 first_mp = mp; 21783 if (mp->b_datap->db_type == M_CTL) { 21784 io = (ipsec_out_t *)first_mp->b_rptr; 21785 /* 21786 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 21787 * 21788 * 1) There is per-socket policy (including cached global 21789 * policy) or a policy on the IP-in-IP tunnel. 21790 * 2) There is no per-socket policy, but it is 21791 * a multicast packet that needs to go out 21792 * on a specific interface. This is the case 21793 * where (ip_wput and ip_wput_multicast) attaches 21794 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 21795 * 21796 * In case (2) we check with global policy to 21797 * see if there is a match and set the ill_index 21798 * appropriately so that we can lookup the ire 21799 * properly in ip_wput_ipsec_out. 21800 */ 21801 21802 /* 21803 * ipsec_out_use_global_policy is set to B_FALSE 21804 * in ipsec_in_to_out(). Refer to that function for 21805 * details. 21806 */ 21807 if ((io->ipsec_out_latch == NULL) && 21808 (io->ipsec_out_use_global_policy)) { 21809 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 21810 ire, connp, unspec_src, zoneid)); 21811 } 21812 if (!io->ipsec_out_secure) { 21813 /* 21814 * If this is not a secure packet, drop 21815 * the IPSEC_OUT mp and treat it as a clear 21816 * packet. This happens when we are sending 21817 * a ICMP reply back to a clear packet. See 21818 * ipsec_in_to_out() for details. 21819 */ 21820 mp = first_mp->b_cont; 21821 freeb(first_mp); 21822 } 21823 return (mp); 21824 } 21825 /* 21826 * See whether we need to attach a global policy here. We 21827 * don't depend on the conn (as it could be null) for deciding 21828 * what policy this datagram should go through because it 21829 * should have happened in ip_wput if there was some 21830 * policy. This normally happens for connections which are not 21831 * fully bound preventing us from caching policies in 21832 * ip_bind. Packets coming from the TCP listener/global queue 21833 * - which are non-hard_bound - could also be affected by 21834 * applying policy here. 21835 * 21836 * If this packet is coming from tcp global queue or listener, 21837 * we will be applying policy here. This may not be *right* 21838 * if these packets are coming from the detached connection as 21839 * it could have gone in clear before. This happens only if a 21840 * TCP connection started when there is no policy and somebody 21841 * added policy before it became detached. Thus packets of the 21842 * detached connection could go out secure and the other end 21843 * would drop it because it will be expecting in clear. The 21844 * converse is not true i.e if somebody starts a TCP 21845 * connection and deletes the policy, all the packets will 21846 * still go out with the policy that existed before deleting 21847 * because ip_unbind sends up policy information which is used 21848 * by TCP on subsequent ip_wputs. The right solution is to fix 21849 * TCP to attach a dummy IPSEC_OUT and set 21850 * ipsec_out_use_global_policy to B_FALSE. As this might 21851 * affect performance for normal cases, we are not doing it. 21852 * Thus, set policy before starting any TCP connections. 21853 * 21854 * NOTE - We might apply policy even for a hard bound connection 21855 * - for which we cached policy in ip_bind - if somebody added 21856 * global policy after we inherited the policy in ip_bind. 21857 * This means that the packets that were going out in clear 21858 * previously would start going secure and hence get dropped 21859 * on the other side. To fix this, TCP attaches a dummy 21860 * ipsec_out and make sure that we don't apply global policy. 21861 */ 21862 if (ipha != NULL) 21863 policy_present = ipss->ipsec_outbound_v4_policy_present; 21864 else 21865 policy_present = ipss->ipsec_outbound_v6_policy_present; 21866 if (!policy_present) 21867 return (mp); 21868 21869 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src, 21870 zoneid)); 21871 } 21872 21873 ire_t * 21874 conn_set_outgoing_ill(conn_t *connp, ire_t *ire, ill_t **conn_outgoing_ill) 21875 { 21876 ipaddr_t addr; 21877 ire_t *save_ire; 21878 irb_t *irb; 21879 ill_group_t *illgrp; 21880 int err; 21881 21882 save_ire = ire; 21883 addr = ire->ire_addr; 21884 21885 ASSERT(ire->ire_type == IRE_BROADCAST); 21886 21887 illgrp = connp->conn_outgoing_ill->ill_group; 21888 if (illgrp == NULL) { 21889 *conn_outgoing_ill = conn_get_held_ill(connp, 21890 &connp->conn_outgoing_ill, &err); 21891 if (err == ILL_LOOKUP_FAILED) { 21892 ire_refrele(save_ire); 21893 return (NULL); 21894 } 21895 return (save_ire); 21896 } 21897 /* 21898 * If IP_BOUND_IF has been done, conn_outgoing_ill will be set. 21899 * If it is part of the group, we need to send on the ire 21900 * that has been cleared of IRE_MARK_NORECV and that belongs 21901 * to this group. This is okay as IP_BOUND_IF really means 21902 * any ill in the group. We depend on the fact that the 21903 * first ire in the group is always cleared of IRE_MARK_NORECV 21904 * if such an ire exists. This is possible only if you have 21905 * at least one ill in the group that has not failed. 21906 * 21907 * First get to the ire that matches the address and group. 21908 * 21909 * We don't look for an ire with a matching zoneid because a given zone 21910 * won't always have broadcast ires on all ills in the group. 21911 */ 21912 irb = ire->ire_bucket; 21913 rw_enter(&irb->irb_lock, RW_READER); 21914 if (ire->ire_marks & IRE_MARK_NORECV) { 21915 /* 21916 * If the current zone only has an ire broadcast for this 21917 * address marked NORECV, the ire we want is ahead in the 21918 * bucket, so we look it up deliberately ignoring the zoneid. 21919 */ 21920 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 21921 if (ire->ire_addr != addr) 21922 continue; 21923 /* skip over deleted ires */ 21924 if (ire->ire_marks & IRE_MARK_CONDEMNED) 21925 continue; 21926 } 21927 } 21928 while (ire != NULL) { 21929 /* 21930 * If a new interface is coming up, we could end up 21931 * seeing the loopback ire and the non-loopback ire 21932 * may not have been added yet. So check for ire_stq 21933 */ 21934 if (ire->ire_stq != NULL && (ire->ire_addr != addr || 21935 ire->ire_ipif->ipif_ill->ill_group == illgrp)) { 21936 break; 21937 } 21938 ire = ire->ire_next; 21939 } 21940 if (ire != NULL && ire->ire_addr == addr && 21941 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 21942 IRE_REFHOLD(ire); 21943 rw_exit(&irb->irb_lock); 21944 ire_refrele(save_ire); 21945 *conn_outgoing_ill = ire_to_ill(ire); 21946 /* 21947 * Refhold the ill to make the conn_outgoing_ill 21948 * independent of the ire. ip_wput_ire goes in a loop 21949 * and may refrele the ire. Since we have an ire at this 21950 * point we don't need to use ILL_CAN_LOOKUP on the ill. 21951 */ 21952 ill_refhold(*conn_outgoing_ill); 21953 return (ire); 21954 } 21955 rw_exit(&irb->irb_lock); 21956 ip1dbg(("conn_set_outgoing_ill: No matching ire\n")); 21957 /* 21958 * If we can't find a suitable ire, return the original ire. 21959 */ 21960 return (save_ire); 21961 } 21962 21963 /* 21964 * This function does the ire_refrele of the ire passed in as the 21965 * argument. As this function looks up more ires i.e broadcast ires, 21966 * it needs to REFRELE them. Currently, for simplicity we don't 21967 * differentiate the one passed in and looked up here. We always 21968 * REFRELE. 21969 * IPQoS Notes: 21970 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 21971 * IPsec packets are done in ipsec_out_process. 21972 * 21973 */ 21974 void 21975 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller, 21976 zoneid_t zoneid) 21977 { 21978 ipha_t *ipha; 21979 #define rptr ((uchar_t *)ipha) 21980 queue_t *stq; 21981 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 21982 uint32_t v_hlen_tos_len; 21983 uint32_t ttl_protocol; 21984 ipaddr_t src; 21985 ipaddr_t dst; 21986 uint32_t cksum; 21987 ipaddr_t orig_src; 21988 ire_t *ire1; 21989 mblk_t *next_mp; 21990 uint_t hlen; 21991 uint16_t *up; 21992 uint32_t max_frag = ire->ire_max_frag; 21993 ill_t *ill = ire_to_ill(ire); 21994 int clusterwide; 21995 uint16_t ip_hdr_included; /* IP header included by ULP? */ 21996 int ipsec_len; 21997 mblk_t *first_mp; 21998 ipsec_out_t *io; 21999 boolean_t conn_dontroute; /* conn value for multicast */ 22000 boolean_t conn_multicast_loop; /* conn value for multicast */ 22001 boolean_t multicast_forward; /* Should we forward ? */ 22002 boolean_t unspec_src; 22003 ill_t *conn_outgoing_ill = NULL; 22004 ill_t *ire_ill; 22005 ill_t *ire1_ill; 22006 ill_t *out_ill; 22007 uint32_t ill_index = 0; 22008 boolean_t multirt_send = B_FALSE; 22009 int err; 22010 ipxmit_state_t pktxmit_state; 22011 ip_stack_t *ipst = ire->ire_ipst; 22012 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 22013 22014 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 22015 "ip_wput_ire_start: q %p", q); 22016 22017 multicast_forward = B_FALSE; 22018 unspec_src = (connp != NULL && connp->conn_unspec_src); 22019 22020 if (ire->ire_flags & RTF_MULTIRT) { 22021 /* 22022 * Multirouting case. The bucket where ire is stored 22023 * probably holds other RTF_MULTIRT flagged ire 22024 * to the destination. In this call to ip_wput_ire, 22025 * we attempt to send the packet through all 22026 * those ires. Thus, we first ensure that ire is the 22027 * first RTF_MULTIRT ire in the bucket, 22028 * before walking the ire list. 22029 */ 22030 ire_t *first_ire; 22031 irb_t *irb = ire->ire_bucket; 22032 ASSERT(irb != NULL); 22033 22034 /* Make sure we do not omit any multiroute ire. */ 22035 IRB_REFHOLD(irb); 22036 for (first_ire = irb->irb_ire; 22037 first_ire != NULL; 22038 first_ire = first_ire->ire_next) { 22039 if ((first_ire->ire_flags & RTF_MULTIRT) && 22040 (first_ire->ire_addr == ire->ire_addr) && 22041 !(first_ire->ire_marks & 22042 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) { 22043 break; 22044 } 22045 } 22046 22047 if ((first_ire != NULL) && (first_ire != ire)) { 22048 IRE_REFHOLD(first_ire); 22049 ire_refrele(ire); 22050 ire = first_ire; 22051 ill = ire_to_ill(ire); 22052 } 22053 IRB_REFRELE(irb); 22054 } 22055 22056 /* 22057 * conn_outgoing_ill variable is used only in the broadcast loop. 22058 * for performance we don't grab the mutexs in the fastpath 22059 */ 22060 if ((connp != NULL) && 22061 (ire->ire_type == IRE_BROADCAST) && 22062 ((connp->conn_nofailover_ill != NULL) || 22063 (connp->conn_outgoing_ill != NULL))) { 22064 /* 22065 * Bind to IPIF_NOFAILOVER address overrides IP_BOUND_IF 22066 * option. So, see if this endpoint is bound to a 22067 * IPIF_NOFAILOVER address. If so, honor it. This implies 22068 * that if the interface is failed, we will still send 22069 * the packet on the same ill which is what we want. 22070 */ 22071 conn_outgoing_ill = conn_get_held_ill(connp, 22072 &connp->conn_nofailover_ill, &err); 22073 if (err == ILL_LOOKUP_FAILED) { 22074 ire_refrele(ire); 22075 freemsg(mp); 22076 return; 22077 } 22078 if (conn_outgoing_ill == NULL) { 22079 /* 22080 * Choose a good ill in the group to send the 22081 * packets on. 22082 */ 22083 ire = conn_set_outgoing_ill(connp, ire, 22084 &conn_outgoing_ill); 22085 if (ire == NULL) { 22086 freemsg(mp); 22087 return; 22088 } 22089 } 22090 } 22091 22092 if (mp->b_datap->db_type != M_CTL) { 22093 ipha = (ipha_t *)mp->b_rptr; 22094 } else { 22095 io = (ipsec_out_t *)mp->b_rptr; 22096 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22097 ASSERT(zoneid == io->ipsec_out_zoneid); 22098 ASSERT(zoneid != ALL_ZONES); 22099 ipha = (ipha_t *)mp->b_cont->b_rptr; 22100 dst = ipha->ipha_dst; 22101 /* 22102 * For the multicast case, ipsec_out carries conn_dontroute and 22103 * conn_multicast_loop as conn may not be available here. We 22104 * need this for multicast loopback and forwarding which is done 22105 * later in the code. 22106 */ 22107 if (CLASSD(dst)) { 22108 conn_dontroute = io->ipsec_out_dontroute; 22109 conn_multicast_loop = io->ipsec_out_multicast_loop; 22110 /* 22111 * If conn_dontroute is not set or conn_multicast_loop 22112 * is set, we need to do forwarding/loopback. For 22113 * datagrams from ip_wput_multicast, conn_dontroute is 22114 * set to B_TRUE and conn_multicast_loop is set to 22115 * B_FALSE so that we neither do forwarding nor 22116 * loopback. 22117 */ 22118 if (!conn_dontroute || conn_multicast_loop) 22119 multicast_forward = B_TRUE; 22120 } 22121 } 22122 22123 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 22124 ire->ire_zoneid != ALL_ZONES) { 22125 /* 22126 * When a zone sends a packet to another zone, we try to deliver 22127 * the packet under the same conditions as if the destination 22128 * was a real node on the network. To do so, we look for a 22129 * matching route in the forwarding table. 22130 * RTF_REJECT and RTF_BLACKHOLE are handled just like 22131 * ip_newroute() does. 22132 * Note that IRE_LOCAL are special, since they are used 22133 * when the zoneid doesn't match in some cases. This means that 22134 * we need to handle ipha_src differently since ire_src_addr 22135 * belongs to the receiving zone instead of the sending zone. 22136 * When ip_restrict_interzone_loopback is set, then 22137 * ire_cache_lookup() ensures that IRE_LOCAL are only used 22138 * for loopback between zones when the logical "Ethernet" would 22139 * have looped them back. 22140 */ 22141 ire_t *src_ire; 22142 22143 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 22144 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 22145 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE), ipst); 22146 if (src_ire != NULL && 22147 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) && 22148 (!ipst->ips_ip_restrict_interzone_loopback || 22149 ire_local_same_ill_group(ire, src_ire))) { 22150 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 22151 ipha->ipha_src = src_ire->ire_src_addr; 22152 ire_refrele(src_ire); 22153 } else { 22154 ire_refrele(ire); 22155 if (conn_outgoing_ill != NULL) 22156 ill_refrele(conn_outgoing_ill); 22157 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 22158 if (src_ire != NULL) { 22159 if (src_ire->ire_flags & RTF_BLACKHOLE) { 22160 ire_refrele(src_ire); 22161 freemsg(mp); 22162 return; 22163 } 22164 ire_refrele(src_ire); 22165 } 22166 if (ip_hdr_complete(ipha, zoneid, ipst)) { 22167 /* Failed */ 22168 freemsg(mp); 22169 return; 22170 } 22171 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, zoneid, 22172 ipst); 22173 return; 22174 } 22175 } 22176 22177 if (mp->b_datap->db_type == M_CTL || 22178 ipss->ipsec_outbound_v4_policy_present) { 22179 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 22180 unspec_src, zoneid); 22181 if (mp == NULL) { 22182 ire_refrele(ire); 22183 if (conn_outgoing_ill != NULL) 22184 ill_refrele(conn_outgoing_ill); 22185 return; 22186 } 22187 } 22188 22189 first_mp = mp; 22190 ipsec_len = 0; 22191 22192 if (first_mp->b_datap->db_type == M_CTL) { 22193 io = (ipsec_out_t *)first_mp->b_rptr; 22194 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22195 mp = first_mp->b_cont; 22196 ipsec_len = ipsec_out_extra_length(first_mp); 22197 ASSERT(ipsec_len >= 0); 22198 /* We already picked up the zoneid from the M_CTL above */ 22199 ASSERT(zoneid == io->ipsec_out_zoneid); 22200 ASSERT(zoneid != ALL_ZONES); 22201 22202 /* 22203 * Drop M_CTL here if IPsec processing is not needed. 22204 * (Non-IPsec use of M_CTL extracted any information it 22205 * needed above). 22206 */ 22207 if (ipsec_len == 0) { 22208 freeb(first_mp); 22209 first_mp = mp; 22210 } 22211 } 22212 22213 /* 22214 * Fast path for ip_wput_ire 22215 */ 22216 22217 ipha = (ipha_t *)mp->b_rptr; 22218 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 22219 dst = ipha->ipha_dst; 22220 22221 /* 22222 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 22223 * if the socket is a SOCK_RAW type. The transport checksum should 22224 * be provided in the pre-built packet, so we don't need to compute it. 22225 * Also, other application set flags, like DF, should not be altered. 22226 * Other transport MUST pass down zero. 22227 */ 22228 ip_hdr_included = ipha->ipha_ident; 22229 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 22230 22231 if (CLASSD(dst)) { 22232 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 22233 ntohl(dst), 22234 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 22235 ntohl(ire->ire_addr))); 22236 } 22237 22238 /* Macros to extract header fields from data already in registers */ 22239 #ifdef _BIG_ENDIAN 22240 #define V_HLEN (v_hlen_tos_len >> 24) 22241 #define LENGTH (v_hlen_tos_len & 0xFFFF) 22242 #define PROTO (ttl_protocol & 0xFF) 22243 #else 22244 #define V_HLEN (v_hlen_tos_len & 0xFF) 22245 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 22246 #define PROTO (ttl_protocol >> 8) 22247 #endif 22248 22249 22250 orig_src = src = ipha->ipha_src; 22251 /* (The loop back to "another" is explained down below.) */ 22252 another:; 22253 /* 22254 * Assign an ident value for this packet. We assign idents on 22255 * a per destination basis out of the IRE. There could be 22256 * other threads targeting the same destination, so we have to 22257 * arrange for a atomic increment. Note that we use a 32-bit 22258 * atomic add because it has better performance than its 22259 * 16-bit sibling. 22260 * 22261 * If running in cluster mode and if the source address 22262 * belongs to a replicated service then vector through 22263 * cl_inet_ipident vector to allocate ip identifier 22264 * NOTE: This is a contract private interface with the 22265 * clustering group. 22266 */ 22267 clusterwide = 0; 22268 if (cl_inet_ipident) { 22269 ASSERT(cl_inet_isclusterwide); 22270 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 22271 AF_INET, (uint8_t *)(uintptr_t)src)) { 22272 ipha->ipha_ident = (*cl_inet_ipident)(IPPROTO_IP, 22273 AF_INET, (uint8_t *)(uintptr_t)src, 22274 (uint8_t *)(uintptr_t)dst); 22275 clusterwide = 1; 22276 } 22277 } 22278 if (!clusterwide) { 22279 ipha->ipha_ident = 22280 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 22281 } 22282 22283 #ifndef _BIG_ENDIAN 22284 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 22285 #endif 22286 22287 /* 22288 * Set source address unless sent on an ill or conn_unspec_src is set. 22289 * This is needed to obey conn_unspec_src when packets go through 22290 * ip_newroute + arp. 22291 * Assumes ip_newroute{,_multi} sets the source address as well. 22292 */ 22293 if (src == INADDR_ANY && !unspec_src) { 22294 /* 22295 * Assign the appropriate source address from the IRE if none 22296 * was specified. 22297 */ 22298 ASSERT(ire->ire_ipversion == IPV4_VERSION); 22299 22300 /* 22301 * With IP multipathing, broadcast packets are sent on the ire 22302 * that has been cleared of IRE_MARK_NORECV and that belongs to 22303 * the group. However, this ire might not be in the same zone so 22304 * we can't always use its source address. We look for a 22305 * broadcast ire in the same group and in the right zone. 22306 */ 22307 if (ire->ire_type == IRE_BROADCAST && 22308 ire->ire_zoneid != zoneid) { 22309 ire_t *src_ire = ire_ctable_lookup(dst, 0, 22310 IRE_BROADCAST, ire->ire_ipif, zoneid, NULL, 22311 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 22312 if (src_ire != NULL) { 22313 src = src_ire->ire_src_addr; 22314 ire_refrele(src_ire); 22315 } else { 22316 ire_refrele(ire); 22317 if (conn_outgoing_ill != NULL) 22318 ill_refrele(conn_outgoing_ill); 22319 freemsg(first_mp); 22320 if (ill != NULL) { 22321 BUMP_MIB(ill->ill_ip_mib, 22322 ipIfStatsOutDiscards); 22323 } else { 22324 BUMP_MIB(&ipst->ips_ip_mib, 22325 ipIfStatsOutDiscards); 22326 } 22327 return; 22328 } 22329 } else { 22330 src = ire->ire_src_addr; 22331 } 22332 22333 if (connp == NULL) { 22334 ip1dbg(("ip_wput_ire: no connp and no src " 22335 "address for dst 0x%x, using src 0x%x\n", 22336 ntohl(dst), 22337 ntohl(src))); 22338 } 22339 ipha->ipha_src = src; 22340 } 22341 stq = ire->ire_stq; 22342 22343 /* 22344 * We only allow ire chains for broadcasts since there will 22345 * be multiple IRE_CACHE entries for the same multicast 22346 * address (one per ipif). 22347 */ 22348 next_mp = NULL; 22349 22350 /* broadcast packet */ 22351 if (ire->ire_type == IRE_BROADCAST) 22352 goto broadcast; 22353 22354 /* loopback ? */ 22355 if (stq == NULL) 22356 goto nullstq; 22357 22358 /* The ill_index for outbound ILL */ 22359 ill_index = Q_TO_INDEX(stq); 22360 22361 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 22362 ttl_protocol = ((uint16_t *)ipha)[4]; 22363 22364 /* pseudo checksum (do it in parts for IP header checksum) */ 22365 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 22366 22367 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 22368 queue_t *dev_q = stq->q_next; 22369 22370 /* flow controlled */ 22371 if ((dev_q->q_next || dev_q->q_first) && 22372 !canput(dev_q)) 22373 goto blocked; 22374 if ((PROTO == IPPROTO_UDP) && 22375 (ip_hdr_included != IP_HDR_INCLUDED)) { 22376 hlen = (V_HLEN & 0xF) << 2; 22377 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22378 if (*up != 0) { 22379 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 22380 hlen, LENGTH, max_frag, ipsec_len, cksum); 22381 /* Software checksum? */ 22382 if (DB_CKSUMFLAGS(mp) == 0) { 22383 IP_STAT(ipst, ip_out_sw_cksum); 22384 IP_STAT_UPDATE(ipst, 22385 ip_udp_out_sw_cksum_bytes, 22386 LENGTH - hlen); 22387 } 22388 } 22389 } 22390 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 22391 hlen = (V_HLEN & 0xF) << 2; 22392 if (PROTO == IPPROTO_TCP) { 22393 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22394 /* 22395 * The packet header is processed once and for all, even 22396 * in the multirouting case. We disable hardware 22397 * checksum if the packet is multirouted, as it will be 22398 * replicated via several interfaces, and not all of 22399 * them may have this capability. 22400 */ 22401 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 22402 LENGTH, max_frag, ipsec_len, cksum); 22403 /* Software checksum? */ 22404 if (DB_CKSUMFLAGS(mp) == 0) { 22405 IP_STAT(ipst, ip_out_sw_cksum); 22406 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22407 LENGTH - hlen); 22408 } 22409 } else { 22410 sctp_hdr_t *sctph; 22411 22412 ASSERT(PROTO == IPPROTO_SCTP); 22413 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22414 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22415 /* 22416 * Zero out the checksum field to ensure proper 22417 * checksum calculation. 22418 */ 22419 sctph->sh_chksum = 0; 22420 #ifdef DEBUG 22421 if (!skip_sctp_cksum) 22422 #endif 22423 sctph->sh_chksum = sctp_cksum(mp, hlen); 22424 } 22425 } 22426 22427 /* 22428 * If this is a multicast packet and originated from ip_wput 22429 * we need to do loopback and forwarding checks. If it comes 22430 * from ip_wput_multicast, we SHOULD not do this. 22431 */ 22432 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 22433 22434 /* checksum */ 22435 cksum += ttl_protocol; 22436 22437 /* fragment the packet */ 22438 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 22439 goto fragmentit; 22440 /* 22441 * Don't use frag_flag if packet is pre-built or source 22442 * routed or if multicast (since multicast packets do 22443 * not solicit ICMP "packet too big" messages). 22444 */ 22445 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22446 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22447 !ip_source_route_included(ipha)) && 22448 !CLASSD(ipha->ipha_dst)) 22449 ipha->ipha_fragment_offset_and_flags |= 22450 htons(ire->ire_frag_flag); 22451 22452 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22453 /* calculate IP header checksum */ 22454 cksum += ipha->ipha_ident; 22455 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 22456 cksum += ipha->ipha_fragment_offset_and_flags; 22457 22458 /* IP options present */ 22459 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22460 if (hlen) 22461 goto checksumoptions; 22462 22463 /* calculate hdr checksum */ 22464 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22465 cksum = ~(cksum + (cksum >> 16)); 22466 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22467 } 22468 if (ipsec_len != 0) { 22469 /* 22470 * We will do the rest of the processing after 22471 * we come back from IPsec in ip_wput_ipsec_out(). 22472 */ 22473 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 22474 22475 io = (ipsec_out_t *)first_mp->b_rptr; 22476 io->ipsec_out_ill_index = ((ill_t *)stq->q_ptr)-> 22477 ill_phyint->phyint_ifindex; 22478 22479 ipsec_out_process(q, first_mp, ire, ill_index); 22480 ire_refrele(ire); 22481 if (conn_outgoing_ill != NULL) 22482 ill_refrele(conn_outgoing_ill); 22483 return; 22484 } 22485 22486 /* 22487 * In most cases, the emission loop below is entered only 22488 * once. Only in the case where the ire holds the 22489 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 22490 * flagged ires in the bucket, and send the packet 22491 * through all crossed RTF_MULTIRT routes. 22492 */ 22493 if (ire->ire_flags & RTF_MULTIRT) { 22494 multirt_send = B_TRUE; 22495 } 22496 do { 22497 if (multirt_send) { 22498 irb_t *irb; 22499 /* 22500 * We are in a multiple send case, need to get 22501 * the next ire and make a duplicate of the packet. 22502 * ire1 holds here the next ire to process in the 22503 * bucket. If multirouting is expected, 22504 * any non-RTF_MULTIRT ire that has the 22505 * right destination address is ignored. 22506 */ 22507 irb = ire->ire_bucket; 22508 ASSERT(irb != NULL); 22509 22510 IRB_REFHOLD(irb); 22511 for (ire1 = ire->ire_next; 22512 ire1 != NULL; 22513 ire1 = ire1->ire_next) { 22514 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 22515 continue; 22516 if (ire1->ire_addr != ire->ire_addr) 22517 continue; 22518 if (ire1->ire_marks & 22519 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 22520 continue; 22521 22522 /* Got one */ 22523 IRE_REFHOLD(ire1); 22524 break; 22525 } 22526 IRB_REFRELE(irb); 22527 22528 if (ire1 != NULL) { 22529 next_mp = copyb(mp); 22530 if ((next_mp == NULL) || 22531 ((mp->b_cont != NULL) && 22532 ((next_mp->b_cont = 22533 dupmsg(mp->b_cont)) == NULL))) { 22534 freemsg(next_mp); 22535 next_mp = NULL; 22536 ire_refrele(ire1); 22537 ire1 = NULL; 22538 } 22539 } 22540 22541 /* Last multiroute ire; don't loop anymore. */ 22542 if (ire1 == NULL) { 22543 multirt_send = B_FALSE; 22544 } 22545 } 22546 22547 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 22548 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 22549 mblk_t *, mp); 22550 FW_HOOKS(ipst->ips_ip4_physical_out_event, 22551 ipst->ips_ipv4firewall_physical_out, 22552 NULL, ire->ire_ipif->ipif_ill, ipha, mp, mp, 0, ipst); 22553 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 22554 if (mp == NULL) 22555 goto release_ire_and_ill; 22556 22557 mp->b_prev = SET_BPREV_FLAG(IPP_LOCAL_OUT); 22558 DTRACE_PROBE2(ip__xmit__1, mblk_t *, mp, ire_t *, ire); 22559 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_TRUE); 22560 if ((pktxmit_state == SEND_FAILED) || 22561 (pktxmit_state == LLHDR_RESLV_FAILED)) { 22562 ip2dbg(("ip_wput_ire: ip_xmit_v4 failed" 22563 "- packet dropped\n")); 22564 release_ire_and_ill: 22565 ire_refrele(ire); 22566 if (next_mp != NULL) { 22567 freemsg(next_mp); 22568 ire_refrele(ire1); 22569 } 22570 if (conn_outgoing_ill != NULL) 22571 ill_refrele(conn_outgoing_ill); 22572 return; 22573 } 22574 22575 if (CLASSD(dst)) { 22576 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastPkts); 22577 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastOctets, 22578 LENGTH); 22579 } 22580 22581 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22582 "ip_wput_ire_end: q %p (%S)", 22583 q, "last copy out"); 22584 IRE_REFRELE(ire); 22585 22586 if (multirt_send) { 22587 ASSERT(ire1); 22588 /* 22589 * Proceed with the next RTF_MULTIRT ire, 22590 * Also set up the send-to queue accordingly. 22591 */ 22592 ire = ire1; 22593 ire1 = NULL; 22594 stq = ire->ire_stq; 22595 mp = next_mp; 22596 next_mp = NULL; 22597 ipha = (ipha_t *)mp->b_rptr; 22598 ill_index = Q_TO_INDEX(stq); 22599 ill = (ill_t *)stq->q_ptr; 22600 } 22601 } while (multirt_send); 22602 if (conn_outgoing_ill != NULL) 22603 ill_refrele(conn_outgoing_ill); 22604 return; 22605 22606 /* 22607 * ire->ire_type == IRE_BROADCAST (minimize diffs) 22608 */ 22609 broadcast: 22610 { 22611 /* 22612 * To avoid broadcast storms, we usually set the TTL to 1 for 22613 * broadcasts. However, if SO_DONTROUTE isn't set, this value 22614 * can be overridden stack-wide through the ip_broadcast_ttl 22615 * ndd tunable, or on a per-connection basis through the 22616 * IP_BROADCAST_TTL socket option. 22617 * 22618 * In the event that we are replying to incoming ICMP packets, 22619 * connp could be NULL. 22620 */ 22621 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl; 22622 if (connp != NULL) { 22623 if (connp->conn_dontroute) 22624 ipha->ipha_ttl = 1; 22625 else if (connp->conn_broadcast_ttl != 0) 22626 ipha->ipha_ttl = connp->conn_broadcast_ttl; 22627 } 22628 22629 /* 22630 * Note that we are not doing a IRB_REFHOLD here. 22631 * Actually we don't care if the list changes i.e 22632 * if somebody deletes an IRE from the list while 22633 * we drop the lock, the next time we come around 22634 * ire_next will be NULL and hence we won't send 22635 * out multiple copies which is fine. 22636 */ 22637 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 22638 ire1 = ire->ire_next; 22639 if (conn_outgoing_ill != NULL) { 22640 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 22641 ASSERT(ire1 == ire->ire_next); 22642 if (ire1 != NULL && ire1->ire_addr == dst) { 22643 ire_refrele(ire); 22644 ire = ire1; 22645 IRE_REFHOLD(ire); 22646 ire1 = ire->ire_next; 22647 continue; 22648 } 22649 rw_exit(&ire->ire_bucket->irb_lock); 22650 /* Did not find a matching ill */ 22651 ip1dbg(("ip_wput_ire: broadcast with no " 22652 "matching IP_BOUND_IF ill %s dst %x\n", 22653 conn_outgoing_ill->ill_name, dst)); 22654 freemsg(first_mp); 22655 if (ire != NULL) 22656 ire_refrele(ire); 22657 ill_refrele(conn_outgoing_ill); 22658 return; 22659 } 22660 } else if (ire1 != NULL && ire1->ire_addr == dst) { 22661 /* 22662 * If the next IRE has the same address and is not one 22663 * of the two copies that we need to send, try to see 22664 * whether this copy should be sent at all. This 22665 * assumes that we insert loopbacks first and then 22666 * non-loopbacks. This is acheived by inserting the 22667 * loopback always before non-loopback. 22668 * This is used to send a single copy of a broadcast 22669 * packet out all physical interfaces that have an 22670 * matching IRE_BROADCAST while also looping 22671 * back one copy (to ip_wput_local) for each 22672 * matching physical interface. However, we avoid 22673 * sending packets out different logical that match by 22674 * having ipif_up/ipif_down supress duplicate 22675 * IRE_BROADCASTS. 22676 * 22677 * This feature is currently used to get broadcasts 22678 * sent to multiple interfaces, when the broadcast 22679 * address being used applies to multiple interfaces. 22680 * For example, a whole net broadcast will be 22681 * replicated on every connected subnet of 22682 * the target net. 22683 * 22684 * Each zone has its own set of IRE_BROADCASTs, so that 22685 * we're able to distribute inbound packets to multiple 22686 * zones who share a broadcast address. We avoid looping 22687 * back outbound packets in different zones but on the 22688 * same ill, as the application would see duplicates. 22689 * 22690 * If the interfaces are part of the same group, 22691 * we would want to send only one copy out for 22692 * whole group. 22693 * 22694 * This logic assumes that ire_add_v4() groups the 22695 * IRE_BROADCAST entries so that those with the same 22696 * ire_addr and ill_group are kept together. 22697 */ 22698 ire_ill = ire->ire_ipif->ipif_ill; 22699 if (ire->ire_stq == NULL && ire1->ire_stq != NULL) { 22700 if (ire_ill->ill_group != NULL && 22701 (ire->ire_marks & IRE_MARK_NORECV)) { 22702 /* 22703 * If the current zone only has an ire 22704 * broadcast for this address marked 22705 * NORECV, the ire we want is ahead in 22706 * the bucket, so we look it up 22707 * deliberately ignoring the zoneid. 22708 */ 22709 for (ire1 = ire->ire_bucket->irb_ire; 22710 ire1 != NULL; 22711 ire1 = ire1->ire_next) { 22712 ire1_ill = 22713 ire1->ire_ipif->ipif_ill; 22714 if (ire1->ire_addr != dst) 22715 continue; 22716 /* skip over the current ire */ 22717 if (ire1 == ire) 22718 continue; 22719 /* skip over deleted ires */ 22720 if (ire1->ire_marks & 22721 IRE_MARK_CONDEMNED) 22722 continue; 22723 /* 22724 * non-loopback ire in our 22725 * group: use it for the next 22726 * pass in the loop 22727 */ 22728 if (ire1->ire_stq != NULL && 22729 ire1_ill->ill_group == 22730 ire_ill->ill_group) 22731 break; 22732 } 22733 } 22734 } else { 22735 while (ire1 != NULL && ire1->ire_addr == dst) { 22736 ire1_ill = ire1->ire_ipif->ipif_ill; 22737 /* 22738 * We can have two broadcast ires on the 22739 * same ill in different zones; here 22740 * we'll send a copy of the packet on 22741 * each ill and the fanout code will 22742 * call conn_wantpacket() to check that 22743 * the zone has the broadcast address 22744 * configured on the ill. If the two 22745 * ires are in the same group we only 22746 * send one copy up. 22747 */ 22748 if (ire1_ill != ire_ill && 22749 (ire1_ill->ill_group == NULL || 22750 ire_ill->ill_group == NULL || 22751 ire1_ill->ill_group != 22752 ire_ill->ill_group)) { 22753 break; 22754 } 22755 ire1 = ire1->ire_next; 22756 } 22757 } 22758 } 22759 ASSERT(multirt_send == B_FALSE); 22760 if (ire1 != NULL && ire1->ire_addr == dst) { 22761 if ((ire->ire_flags & RTF_MULTIRT) && 22762 (ire1->ire_flags & RTF_MULTIRT)) { 22763 /* 22764 * We are in the multirouting case. 22765 * The message must be sent at least 22766 * on both ires. These ires have been 22767 * inserted AFTER the standard ones 22768 * in ip_rt_add(). There are thus no 22769 * other ire entries for the destination 22770 * address in the rest of the bucket 22771 * that do not have the RTF_MULTIRT 22772 * flag. We don't process a copy 22773 * of the message here. This will be 22774 * done in the final sending loop. 22775 */ 22776 multirt_send = B_TRUE; 22777 } else { 22778 next_mp = ip_copymsg(first_mp); 22779 if (next_mp != NULL) 22780 IRE_REFHOLD(ire1); 22781 } 22782 } 22783 rw_exit(&ire->ire_bucket->irb_lock); 22784 } 22785 22786 if (stq) { 22787 /* 22788 * A non-NULL send-to queue means this packet is going 22789 * out of this machine. 22790 */ 22791 out_ill = (ill_t *)stq->q_ptr; 22792 22793 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutRequests); 22794 ttl_protocol = ((uint16_t *)ipha)[4]; 22795 /* 22796 * We accumulate the pseudo header checksum in cksum. 22797 * This is pretty hairy code, so watch close. One 22798 * thing to keep in mind is that UDP and TCP have 22799 * stored their respective datagram lengths in their 22800 * checksum fields. This lines things up real nice. 22801 */ 22802 cksum = (dst >> 16) + (dst & 0xFFFF) + 22803 (src >> 16) + (src & 0xFFFF); 22804 /* 22805 * We assume the udp checksum field contains the 22806 * length, so to compute the pseudo header checksum, 22807 * all we need is the protocol number and src/dst. 22808 */ 22809 /* Provide the checksums for UDP and TCP. */ 22810 if ((PROTO == IPPROTO_TCP) && 22811 (ip_hdr_included != IP_HDR_INCLUDED)) { 22812 /* hlen gets the number of uchar_ts in the IP header */ 22813 hlen = (V_HLEN & 0xF) << 2; 22814 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22815 IP_STAT(ipst, ip_out_sw_cksum); 22816 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22817 LENGTH - hlen); 22818 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 22819 } else if (PROTO == IPPROTO_SCTP && 22820 (ip_hdr_included != IP_HDR_INCLUDED)) { 22821 sctp_hdr_t *sctph; 22822 22823 hlen = (V_HLEN & 0xF) << 2; 22824 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22825 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22826 sctph->sh_chksum = 0; 22827 #ifdef DEBUG 22828 if (!skip_sctp_cksum) 22829 #endif 22830 sctph->sh_chksum = sctp_cksum(mp, hlen); 22831 } else { 22832 queue_t *dev_q = stq->q_next; 22833 22834 if ((dev_q->q_next || dev_q->q_first) && 22835 !canput(dev_q)) { 22836 blocked: 22837 ipha->ipha_ident = ip_hdr_included; 22838 /* 22839 * If we don't have a conn to apply 22840 * backpressure, free the message. 22841 * In the ire_send path, we don't know 22842 * the position to requeue the packet. Rather 22843 * than reorder packets, we just drop this 22844 * packet. 22845 */ 22846 if (ipst->ips_ip_output_queue && 22847 connp != NULL && 22848 caller != IRE_SEND) { 22849 if (caller == IP_WSRV) { 22850 connp->conn_did_putbq = 1; 22851 (void) putbq(connp->conn_wq, 22852 first_mp); 22853 conn_drain_insert(connp); 22854 /* 22855 * This is the service thread, 22856 * and the queue is already 22857 * noenabled. The check for 22858 * canput and the putbq is not 22859 * atomic. So we need to check 22860 * again. 22861 */ 22862 if (canput(stq->q_next)) 22863 connp->conn_did_putbq 22864 = 0; 22865 IP_STAT(ipst, ip_conn_flputbq); 22866 } else { 22867 /* 22868 * We are not the service proc. 22869 * ip_wsrv will be scheduled or 22870 * is already running. 22871 */ 22872 (void) putq(connp->conn_wq, 22873 first_mp); 22874 } 22875 } else { 22876 out_ill = (ill_t *)stq->q_ptr; 22877 BUMP_MIB(out_ill->ill_ip_mib, 22878 ipIfStatsOutDiscards); 22879 freemsg(first_mp); 22880 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22881 "ip_wput_ire_end: q %p (%S)", 22882 q, "discard"); 22883 } 22884 ire_refrele(ire); 22885 if (next_mp) { 22886 ire_refrele(ire1); 22887 freemsg(next_mp); 22888 } 22889 if (conn_outgoing_ill != NULL) 22890 ill_refrele(conn_outgoing_ill); 22891 return; 22892 } 22893 if ((PROTO == IPPROTO_UDP) && 22894 (ip_hdr_included != IP_HDR_INCLUDED)) { 22895 /* 22896 * hlen gets the number of uchar_ts in the 22897 * IP header 22898 */ 22899 hlen = (V_HLEN & 0xF) << 2; 22900 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22901 max_frag = ire->ire_max_frag; 22902 if (*up != 0) { 22903 IP_CKSUM_XMIT(out_ill, ire, mp, ipha, 22904 up, PROTO, hlen, LENGTH, max_frag, 22905 ipsec_len, cksum); 22906 /* Software checksum? */ 22907 if (DB_CKSUMFLAGS(mp) == 0) { 22908 IP_STAT(ipst, ip_out_sw_cksum); 22909 IP_STAT_UPDATE(ipst, 22910 ip_udp_out_sw_cksum_bytes, 22911 LENGTH - hlen); 22912 } 22913 } 22914 } 22915 } 22916 /* 22917 * Need to do this even when fragmenting. The local 22918 * loopback can be done without computing checksums 22919 * but forwarding out other interface must be done 22920 * after the IP checksum (and ULP checksums) have been 22921 * computed. 22922 * 22923 * NOTE : multicast_forward is set only if this packet 22924 * originated from ip_wput. For packets originating from 22925 * ip_wput_multicast, it is not set. 22926 */ 22927 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 22928 multi_loopback: 22929 ip2dbg(("ip_wput: multicast, loop %d\n", 22930 conn_multicast_loop)); 22931 22932 /* Forget header checksum offload */ 22933 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 22934 22935 /* 22936 * Local loopback of multicasts? Check the 22937 * ill. 22938 * 22939 * Note that the loopback function will not come 22940 * in through ip_rput - it will only do the 22941 * client fanout thus we need to do an mforward 22942 * as well. The is different from the BSD 22943 * logic. 22944 */ 22945 if (ill != NULL) { 22946 ilm_t *ilm; 22947 22948 ILM_WALKER_HOLD(ill); 22949 ilm = ilm_lookup_ill(ill, ipha->ipha_dst, 22950 ALL_ZONES); 22951 ILM_WALKER_RELE(ill); 22952 if (ilm != NULL) { 22953 /* 22954 * Pass along the virtual output q. 22955 * ip_wput_local() will distribute the 22956 * packet to all the matching zones, 22957 * except the sending zone when 22958 * IP_MULTICAST_LOOP is false. 22959 */ 22960 ip_multicast_loopback(q, ill, first_mp, 22961 conn_multicast_loop ? 0 : 22962 IP_FF_NO_MCAST_LOOP, zoneid); 22963 } 22964 } 22965 if (ipha->ipha_ttl == 0) { 22966 /* 22967 * 0 => only to this host i.e. we are 22968 * done. We are also done if this was the 22969 * loopback interface since it is sufficient 22970 * to loopback one copy of a multicast packet. 22971 */ 22972 freemsg(first_mp); 22973 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22974 "ip_wput_ire_end: q %p (%S)", 22975 q, "loopback"); 22976 ire_refrele(ire); 22977 if (conn_outgoing_ill != NULL) 22978 ill_refrele(conn_outgoing_ill); 22979 return; 22980 } 22981 /* 22982 * ILLF_MULTICAST is checked in ip_newroute 22983 * i.e. we don't need to check it here since 22984 * all IRE_CACHEs come from ip_newroute. 22985 * For multicast traffic, SO_DONTROUTE is interpreted 22986 * to mean only send the packet out the interface 22987 * (optionally specified with IP_MULTICAST_IF) 22988 * and do not forward it out additional interfaces. 22989 * RSVP and the rsvp daemon is an example of a 22990 * protocol and user level process that 22991 * handles it's own routing. Hence, it uses the 22992 * SO_DONTROUTE option to accomplish this. 22993 */ 22994 22995 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 22996 ill != NULL) { 22997 /* Unconditionally redo the checksum */ 22998 ipha->ipha_hdr_checksum = 0; 22999 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23000 23001 /* 23002 * If this needs to go out secure, we need 23003 * to wait till we finish the IPsec 23004 * processing. 23005 */ 23006 if (ipsec_len == 0 && 23007 ip_mforward(ill, ipha, mp)) { 23008 freemsg(first_mp); 23009 ip1dbg(("ip_wput: mforward failed\n")); 23010 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23011 "ip_wput_ire_end: q %p (%S)", 23012 q, "mforward failed"); 23013 ire_refrele(ire); 23014 if (conn_outgoing_ill != NULL) 23015 ill_refrele(conn_outgoing_ill); 23016 return; 23017 } 23018 } 23019 } 23020 max_frag = ire->ire_max_frag; 23021 cksum += ttl_protocol; 23022 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 23023 /* No fragmentation required for this one. */ 23024 /* 23025 * Don't use frag_flag if packet is pre-built or source 23026 * routed or if multicast (since multicast packets do 23027 * not solicit ICMP "packet too big" messages). 23028 */ 23029 if ((ip_hdr_included != IP_HDR_INCLUDED) && 23030 (V_HLEN == IP_SIMPLE_HDR_VERSION || 23031 !ip_source_route_included(ipha)) && 23032 !CLASSD(ipha->ipha_dst)) 23033 ipha->ipha_fragment_offset_and_flags |= 23034 htons(ire->ire_frag_flag); 23035 23036 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 23037 /* Complete the IP header checksum. */ 23038 cksum += ipha->ipha_ident; 23039 cksum += (v_hlen_tos_len >> 16)+ 23040 (v_hlen_tos_len & 0xFFFF); 23041 cksum += ipha->ipha_fragment_offset_and_flags; 23042 hlen = (V_HLEN & 0xF) - 23043 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 23044 if (hlen) { 23045 checksumoptions: 23046 /* 23047 * Account for the IP Options in the IP 23048 * header checksum. 23049 */ 23050 up = (uint16_t *)(rptr+ 23051 IP_SIMPLE_HDR_LENGTH); 23052 do { 23053 cksum += up[0]; 23054 cksum += up[1]; 23055 up += 2; 23056 } while (--hlen); 23057 } 23058 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 23059 cksum = ~(cksum + (cksum >> 16)); 23060 ipha->ipha_hdr_checksum = (uint16_t)cksum; 23061 } 23062 if (ipsec_len != 0) { 23063 ipsec_out_process(q, first_mp, ire, ill_index); 23064 if (!next_mp) { 23065 ire_refrele(ire); 23066 if (conn_outgoing_ill != NULL) 23067 ill_refrele(conn_outgoing_ill); 23068 return; 23069 } 23070 goto next; 23071 } 23072 23073 /* 23074 * multirt_send has already been handled 23075 * for broadcast, but not yet for multicast 23076 * or IP options. 23077 */ 23078 if (next_mp == NULL) { 23079 if (ire->ire_flags & RTF_MULTIRT) { 23080 multirt_send = B_TRUE; 23081 } 23082 } 23083 23084 /* 23085 * In most cases, the emission loop below is 23086 * entered only once. Only in the case where 23087 * the ire holds the RTF_MULTIRT flag, do we loop 23088 * to process all RTF_MULTIRT ires in the bucket, 23089 * and send the packet through all crossed 23090 * RTF_MULTIRT routes. 23091 */ 23092 do { 23093 if (multirt_send) { 23094 irb_t *irb; 23095 23096 irb = ire->ire_bucket; 23097 ASSERT(irb != NULL); 23098 /* 23099 * We are in a multiple send case, 23100 * need to get the next IRE and make 23101 * a duplicate of the packet. 23102 */ 23103 IRB_REFHOLD(irb); 23104 for (ire1 = ire->ire_next; 23105 ire1 != NULL; 23106 ire1 = ire1->ire_next) { 23107 if (!(ire1->ire_flags & 23108 RTF_MULTIRT)) { 23109 continue; 23110 } 23111 if (ire1->ire_addr != 23112 ire->ire_addr) { 23113 continue; 23114 } 23115 if (ire1->ire_marks & 23116 (IRE_MARK_CONDEMNED| 23117 IRE_MARK_HIDDEN)) { 23118 continue; 23119 } 23120 23121 /* Got one */ 23122 IRE_REFHOLD(ire1); 23123 break; 23124 } 23125 IRB_REFRELE(irb); 23126 23127 if (ire1 != NULL) { 23128 next_mp = copyb(mp); 23129 if ((next_mp == NULL) || 23130 ((mp->b_cont != NULL) && 23131 ((next_mp->b_cont = 23132 dupmsg(mp->b_cont)) 23133 == NULL))) { 23134 freemsg(next_mp); 23135 next_mp = NULL; 23136 ire_refrele(ire1); 23137 ire1 = NULL; 23138 } 23139 } 23140 23141 /* 23142 * Last multiroute ire; don't loop 23143 * anymore. The emission is over 23144 * and next_mp is NULL. 23145 */ 23146 if (ire1 == NULL) { 23147 multirt_send = B_FALSE; 23148 } 23149 } 23150 23151 out_ill = ire_to_ill(ire); 23152 DTRACE_PROBE4(ip4__physical__out__start, 23153 ill_t *, NULL, 23154 ill_t *, out_ill, 23155 ipha_t *, ipha, mblk_t *, mp); 23156 FW_HOOKS(ipst->ips_ip4_physical_out_event, 23157 ipst->ips_ipv4firewall_physical_out, 23158 NULL, out_ill, ipha, mp, mp, 0, ipst); 23159 DTRACE_PROBE1(ip4__physical__out__end, 23160 mblk_t *, mp); 23161 if (mp == NULL) 23162 goto release_ire_and_ill_2; 23163 23164 ASSERT(ipsec_len == 0); 23165 mp->b_prev = 23166 SET_BPREV_FLAG(IPP_LOCAL_OUT); 23167 DTRACE_PROBE2(ip__xmit__2, 23168 mblk_t *, mp, ire_t *, ire); 23169 pktxmit_state = ip_xmit_v4(mp, ire, 23170 NULL, B_TRUE); 23171 if ((pktxmit_state == SEND_FAILED) || 23172 (pktxmit_state == LLHDR_RESLV_FAILED)) { 23173 release_ire_and_ill_2: 23174 if (next_mp) { 23175 freemsg(next_mp); 23176 ire_refrele(ire1); 23177 } 23178 ire_refrele(ire); 23179 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23180 "ip_wput_ire_end: q %p (%S)", 23181 q, "discard MDATA"); 23182 if (conn_outgoing_ill != NULL) 23183 ill_refrele(conn_outgoing_ill); 23184 return; 23185 } 23186 23187 if (CLASSD(dst)) { 23188 BUMP_MIB(out_ill->ill_ip_mib, 23189 ipIfStatsHCOutMcastPkts); 23190 UPDATE_MIB(out_ill->ill_ip_mib, 23191 ipIfStatsHCOutMcastOctets, 23192 LENGTH); 23193 } else if (ire->ire_type == IRE_BROADCAST) { 23194 BUMP_MIB(out_ill->ill_ip_mib, 23195 ipIfStatsHCOutBcastPkts); 23196 } 23197 23198 if (multirt_send) { 23199 /* 23200 * We are in a multiple send case, 23201 * need to re-enter the sending loop 23202 * using the next ire. 23203 */ 23204 ire_refrele(ire); 23205 ire = ire1; 23206 stq = ire->ire_stq; 23207 mp = next_mp; 23208 next_mp = NULL; 23209 ipha = (ipha_t *)mp->b_rptr; 23210 ill_index = Q_TO_INDEX(stq); 23211 } 23212 } while (multirt_send); 23213 23214 if (!next_mp) { 23215 /* 23216 * Last copy going out (the ultra-common 23217 * case). Note that we intentionally replicate 23218 * the putnext rather than calling it before 23219 * the next_mp check in hopes of a little 23220 * tail-call action out of the compiler. 23221 */ 23222 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23223 "ip_wput_ire_end: q %p (%S)", 23224 q, "last copy out(1)"); 23225 ire_refrele(ire); 23226 if (conn_outgoing_ill != NULL) 23227 ill_refrele(conn_outgoing_ill); 23228 return; 23229 } 23230 /* More copies going out below. */ 23231 } else { 23232 int offset; 23233 fragmentit: 23234 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23235 /* 23236 * If this would generate a icmp_frag_needed message, 23237 * we need to handle it before we do the IPsec 23238 * processing. Otherwise, we need to strip the IPsec 23239 * headers before we send up the message to the ULPs 23240 * which becomes messy and difficult. 23241 */ 23242 if (ipsec_len != 0) { 23243 if ((max_frag < (unsigned int)(LENGTH + 23244 ipsec_len)) && (offset & IPH_DF)) { 23245 out_ill = (ill_t *)stq->q_ptr; 23246 BUMP_MIB(out_ill->ill_ip_mib, 23247 ipIfStatsOutFragFails); 23248 BUMP_MIB(out_ill->ill_ip_mib, 23249 ipIfStatsOutFragReqds); 23250 ipha->ipha_hdr_checksum = 0; 23251 ipha->ipha_hdr_checksum = 23252 (uint16_t)ip_csum_hdr(ipha); 23253 icmp_frag_needed(ire->ire_stq, first_mp, 23254 max_frag, zoneid, ipst); 23255 if (!next_mp) { 23256 ire_refrele(ire); 23257 if (conn_outgoing_ill != NULL) { 23258 ill_refrele( 23259 conn_outgoing_ill); 23260 } 23261 return; 23262 } 23263 } else { 23264 /* 23265 * This won't cause a icmp_frag_needed 23266 * message. to be generated. Send it on 23267 * the wire. Note that this could still 23268 * cause fragmentation and all we 23269 * do is the generation of the message 23270 * to the ULP if needed before IPsec. 23271 */ 23272 if (!next_mp) { 23273 ipsec_out_process(q, first_mp, 23274 ire, ill_index); 23275 TRACE_2(TR_FAC_IP, 23276 TR_IP_WPUT_IRE_END, 23277 "ip_wput_ire_end: q %p " 23278 "(%S)", q, 23279 "last ipsec_out_process"); 23280 ire_refrele(ire); 23281 if (conn_outgoing_ill != NULL) { 23282 ill_refrele( 23283 conn_outgoing_ill); 23284 } 23285 return; 23286 } 23287 ipsec_out_process(q, first_mp, 23288 ire, ill_index); 23289 } 23290 } else { 23291 /* 23292 * Initiate IPPF processing. For 23293 * fragmentable packets we finish 23294 * all QOS packet processing before 23295 * calling: 23296 * ip_wput_ire_fragmentit->ip_wput_frag 23297 */ 23298 23299 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23300 ip_process(IPP_LOCAL_OUT, &mp, 23301 ill_index); 23302 if (mp == NULL) { 23303 out_ill = (ill_t *)stq->q_ptr; 23304 BUMP_MIB(out_ill->ill_ip_mib, 23305 ipIfStatsOutDiscards); 23306 if (next_mp != NULL) { 23307 freemsg(next_mp); 23308 ire_refrele(ire1); 23309 } 23310 ire_refrele(ire); 23311 TRACE_2(TR_FAC_IP, 23312 TR_IP_WPUT_IRE_END, 23313 "ip_wput_ire: q %p (%S)", 23314 q, "discard MDATA"); 23315 if (conn_outgoing_ill != NULL) { 23316 ill_refrele( 23317 conn_outgoing_ill); 23318 } 23319 return; 23320 } 23321 } 23322 if (!next_mp) { 23323 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23324 "ip_wput_ire_end: q %p (%S)", 23325 q, "last fragmentation"); 23326 ip_wput_ire_fragmentit(mp, ire, 23327 zoneid, ipst); 23328 ire_refrele(ire); 23329 if (conn_outgoing_ill != NULL) 23330 ill_refrele(conn_outgoing_ill); 23331 return; 23332 } 23333 ip_wput_ire_fragmentit(mp, ire, zoneid, ipst); 23334 } 23335 } 23336 } else { 23337 nullstq: 23338 /* A NULL stq means the destination address is local. */ 23339 UPDATE_OB_PKT_COUNT(ire); 23340 ire->ire_last_used_time = lbolt; 23341 ASSERT(ire->ire_ipif != NULL); 23342 if (!next_mp) { 23343 /* 23344 * Is there an "in" and "out" for traffic local 23345 * to a host (loopback)? The code in Solaris doesn't 23346 * explicitly draw a line in its code for in vs out, 23347 * so we've had to draw a line in the sand: ip_wput_ire 23348 * is considered to be the "output" side and 23349 * ip_wput_local to be the "input" side. 23350 */ 23351 out_ill = ire_to_ill(ire); 23352 23353 DTRACE_PROBE4(ip4__loopback__out__start, 23354 ill_t *, NULL, ill_t *, out_ill, 23355 ipha_t *, ipha, mblk_t *, first_mp); 23356 23357 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23358 ipst->ips_ipv4firewall_loopback_out, 23359 NULL, out_ill, ipha, first_mp, mp, 0, ipst); 23360 23361 DTRACE_PROBE1(ip4__loopback__out_end, 23362 mblk_t *, first_mp); 23363 23364 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23365 "ip_wput_ire_end: q %p (%S)", 23366 q, "local address"); 23367 23368 if (first_mp != NULL) 23369 ip_wput_local(q, out_ill, ipha, 23370 first_mp, ire, 0, ire->ire_zoneid); 23371 ire_refrele(ire); 23372 if (conn_outgoing_ill != NULL) 23373 ill_refrele(conn_outgoing_ill); 23374 return; 23375 } 23376 23377 out_ill = ire_to_ill(ire); 23378 23379 DTRACE_PROBE4(ip4__loopback__out__start, 23380 ill_t *, NULL, ill_t *, out_ill, 23381 ipha_t *, ipha, mblk_t *, first_mp); 23382 23383 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23384 ipst->ips_ipv4firewall_loopback_out, 23385 NULL, out_ill, ipha, first_mp, mp, 0, ipst); 23386 23387 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, first_mp); 23388 23389 if (first_mp != NULL) 23390 ip_wput_local(q, out_ill, ipha, 23391 first_mp, ire, 0, ire->ire_zoneid); 23392 } 23393 next: 23394 /* 23395 * More copies going out to additional interfaces. 23396 * ire1 has already been held. We don't need the 23397 * "ire" anymore. 23398 */ 23399 ire_refrele(ire); 23400 ire = ire1; 23401 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 23402 mp = next_mp; 23403 ASSERT(ire->ire_ipversion == IPV4_VERSION); 23404 ill = ire_to_ill(ire); 23405 first_mp = mp; 23406 if (ipsec_len != 0) { 23407 ASSERT(first_mp->b_datap->db_type == M_CTL); 23408 mp = mp->b_cont; 23409 } 23410 dst = ire->ire_addr; 23411 ipha = (ipha_t *)mp->b_rptr; 23412 /* 23413 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 23414 * Restore ipha_ident "no checksum" flag. 23415 */ 23416 src = orig_src; 23417 ipha->ipha_ident = ip_hdr_included; 23418 goto another; 23419 23420 #undef rptr 23421 #undef Q_TO_INDEX 23422 } 23423 23424 /* 23425 * Routine to allocate a message that is used to notify the ULP about MDT. 23426 * The caller may provide a pointer to the link-layer MDT capabilities, 23427 * or NULL if MDT is to be disabled on the stream. 23428 */ 23429 mblk_t * 23430 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 23431 { 23432 mblk_t *mp; 23433 ip_mdt_info_t *mdti; 23434 ill_mdt_capab_t *idst; 23435 23436 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 23437 DB_TYPE(mp) = M_CTL; 23438 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 23439 mdti = (ip_mdt_info_t *)mp->b_rptr; 23440 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 23441 idst = &(mdti->mdt_capab); 23442 23443 /* 23444 * If the caller provides us with the capability, copy 23445 * it over into our notification message; otherwise 23446 * we zero out the capability portion. 23447 */ 23448 if (isrc != NULL) 23449 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23450 else 23451 bzero((caddr_t)idst, sizeof (*idst)); 23452 } 23453 return (mp); 23454 } 23455 23456 /* 23457 * Routine which determines whether MDT can be enabled on the destination 23458 * IRE and IPC combination, and if so, allocates and returns the MDT 23459 * notification mblk that may be used by ULP. We also check if we need to 23460 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 23461 * MDT usage in the past have been lifted. This gets called during IP 23462 * and ULP binding. 23463 */ 23464 mblk_t * 23465 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23466 ill_mdt_capab_t *mdt_cap) 23467 { 23468 mblk_t *mp; 23469 boolean_t rc = B_FALSE; 23470 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23471 23472 ASSERT(dst_ire != NULL); 23473 ASSERT(connp != NULL); 23474 ASSERT(mdt_cap != NULL); 23475 23476 /* 23477 * Currently, we only support simple TCP/{IPv4,IPv6} with 23478 * Multidata, which is handled in tcp_multisend(). This 23479 * is the reason why we do all these checks here, to ensure 23480 * that we don't enable Multidata for the cases which we 23481 * can't handle at the moment. 23482 */ 23483 do { 23484 /* Only do TCP at the moment */ 23485 if (connp->conn_ulp != IPPROTO_TCP) 23486 break; 23487 23488 /* 23489 * IPsec outbound policy present? Note that we get here 23490 * after calling ipsec_conn_cache_policy() where the global 23491 * policy checking is performed. conn_latch will be 23492 * non-NULL as long as there's a policy defined, 23493 * i.e. conn_out_enforce_policy may be NULL in such case 23494 * when the connection is non-secure, and hence we check 23495 * further if the latch refers to an outbound policy. 23496 */ 23497 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 23498 break; 23499 23500 /* CGTP (multiroute) is enabled? */ 23501 if (dst_ire->ire_flags & RTF_MULTIRT) 23502 break; 23503 23504 /* Outbound IPQoS enabled? */ 23505 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23506 /* 23507 * In this case, we disable MDT for this and all 23508 * future connections going over the interface. 23509 */ 23510 mdt_cap->ill_mdt_on = 0; 23511 break; 23512 } 23513 23514 /* socket option(s) present? */ 23515 if (!CONN_IS_LSO_MD_FASTPATH(connp)) 23516 break; 23517 23518 rc = B_TRUE; 23519 /* CONSTCOND */ 23520 } while (0); 23521 23522 /* Remember the result */ 23523 connp->conn_mdt_ok = rc; 23524 23525 if (!rc) 23526 return (NULL); 23527 else if (!mdt_cap->ill_mdt_on) { 23528 /* 23529 * If MDT has been previously turned off in the past, and we 23530 * currently can do MDT (due to IPQoS policy removal, etc.) 23531 * then enable it for this interface. 23532 */ 23533 mdt_cap->ill_mdt_on = 1; 23534 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 23535 "interface %s\n", ill_name)); 23536 } 23537 23538 /* Allocate the MDT info mblk */ 23539 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 23540 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 23541 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23542 return (NULL); 23543 } 23544 return (mp); 23545 } 23546 23547 /* 23548 * Routine to allocate a message that is used to notify the ULP about LSO. 23549 * The caller may provide a pointer to the link-layer LSO capabilities, 23550 * or NULL if LSO is to be disabled on the stream. 23551 */ 23552 mblk_t * 23553 ip_lsoinfo_alloc(ill_lso_capab_t *isrc) 23554 { 23555 mblk_t *mp; 23556 ip_lso_info_t *lsoi; 23557 ill_lso_capab_t *idst; 23558 23559 if ((mp = allocb(sizeof (*lsoi), BPRI_HI)) != NULL) { 23560 DB_TYPE(mp) = M_CTL; 23561 mp->b_wptr = mp->b_rptr + sizeof (*lsoi); 23562 lsoi = (ip_lso_info_t *)mp->b_rptr; 23563 lsoi->lso_info_id = LSO_IOC_INFO_UPDATE; 23564 idst = &(lsoi->lso_capab); 23565 23566 /* 23567 * If the caller provides us with the capability, copy 23568 * it over into our notification message; otherwise 23569 * we zero out the capability portion. 23570 */ 23571 if (isrc != NULL) 23572 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23573 else 23574 bzero((caddr_t)idst, sizeof (*idst)); 23575 } 23576 return (mp); 23577 } 23578 23579 /* 23580 * Routine which determines whether LSO can be enabled on the destination 23581 * IRE and IPC combination, and if so, allocates and returns the LSO 23582 * notification mblk that may be used by ULP. We also check if we need to 23583 * turn LSO back to 'on' when certain restrictions prohibiting us to allow 23584 * LSO usage in the past have been lifted. This gets called during IP 23585 * and ULP binding. 23586 */ 23587 mblk_t * 23588 ip_lsoinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23589 ill_lso_capab_t *lso_cap) 23590 { 23591 mblk_t *mp; 23592 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23593 23594 ASSERT(dst_ire != NULL); 23595 ASSERT(connp != NULL); 23596 ASSERT(lso_cap != NULL); 23597 23598 connp->conn_lso_ok = B_TRUE; 23599 23600 if ((connp->conn_ulp != IPPROTO_TCP) || 23601 CONN_IPSEC_OUT_ENCAPSULATED(connp) || 23602 (dst_ire->ire_flags & RTF_MULTIRT) || 23603 !CONN_IS_LSO_MD_FASTPATH(connp) || 23604 (IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 23605 connp->conn_lso_ok = B_FALSE; 23606 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23607 /* 23608 * Disable LSO for this and all future connections going 23609 * over the interface. 23610 */ 23611 lso_cap->ill_lso_on = 0; 23612 } 23613 } 23614 23615 if (!connp->conn_lso_ok) 23616 return (NULL); 23617 else if (!lso_cap->ill_lso_on) { 23618 /* 23619 * If LSO has been previously turned off in the past, and we 23620 * currently can do LSO (due to IPQoS policy removal, etc.) 23621 * then enable it for this interface. 23622 */ 23623 lso_cap->ill_lso_on = 1; 23624 ip1dbg(("ip_mdinfo_return: reenabling LSO for interface %s\n", 23625 ill_name)); 23626 } 23627 23628 /* Allocate the LSO info mblk */ 23629 if ((mp = ip_lsoinfo_alloc(lso_cap)) == NULL) 23630 ip0dbg(("ip_lsoinfo_return: can't enable LSO for " 23631 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23632 23633 return (mp); 23634 } 23635 23636 /* 23637 * Create destination address attribute, and fill it with the physical 23638 * destination address and SAP taken from the template DL_UNITDATA_REQ 23639 * message block. 23640 */ 23641 boolean_t 23642 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 23643 { 23644 dl_unitdata_req_t *dlurp; 23645 pattr_t *pa; 23646 pattrinfo_t pa_info; 23647 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 23648 uint_t das_len, das_off; 23649 23650 ASSERT(dlmp != NULL); 23651 23652 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 23653 das_len = dlurp->dl_dest_addr_length; 23654 das_off = dlurp->dl_dest_addr_offset; 23655 23656 pa_info.type = PATTR_DSTADDRSAP; 23657 pa_info.len = sizeof (**das) + das_len - 1; 23658 23659 /* create and associate the attribute */ 23660 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23661 if (pa != NULL) { 23662 ASSERT(*das != NULL); 23663 (*das)->addr_is_group = 0; 23664 (*das)->addr_len = (uint8_t)das_len; 23665 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 23666 } 23667 23668 return (pa != NULL); 23669 } 23670 23671 /* 23672 * Create hardware checksum attribute and fill it with the values passed. 23673 */ 23674 boolean_t 23675 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 23676 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 23677 { 23678 pattr_t *pa; 23679 pattrinfo_t pa_info; 23680 23681 ASSERT(mmd != NULL); 23682 23683 pa_info.type = PATTR_HCKSUM; 23684 pa_info.len = sizeof (pattr_hcksum_t); 23685 23686 /* create and associate the attribute */ 23687 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23688 if (pa != NULL) { 23689 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 23690 23691 hck->hcksum_start_offset = start_offset; 23692 hck->hcksum_stuff_offset = stuff_offset; 23693 hck->hcksum_end_offset = end_offset; 23694 hck->hcksum_flags = flags; 23695 } 23696 return (pa != NULL); 23697 } 23698 23699 /* 23700 * Create zerocopy attribute and fill it with the specified flags 23701 */ 23702 boolean_t 23703 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 23704 { 23705 pattr_t *pa; 23706 pattrinfo_t pa_info; 23707 23708 ASSERT(mmd != NULL); 23709 pa_info.type = PATTR_ZCOPY; 23710 pa_info.len = sizeof (pattr_zcopy_t); 23711 23712 /* create and associate the attribute */ 23713 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23714 if (pa != NULL) { 23715 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 23716 23717 zcopy->zcopy_flags = flags; 23718 } 23719 return (pa != NULL); 23720 } 23721 23722 /* 23723 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 23724 * block chain. We could rewrite to handle arbitrary message block chains but 23725 * that would make the code complicated and slow. Right now there three 23726 * restrictions: 23727 * 23728 * 1. The first message block must contain the complete IP header and 23729 * at least 1 byte of payload data. 23730 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 23731 * so that we can use a single Multidata message. 23732 * 3. No frag must be distributed over two or more message blocks so 23733 * that we don't need more than two packet descriptors per frag. 23734 * 23735 * The above restrictions allow us to support userland applications (which 23736 * will send down a single message block) and NFS over UDP (which will 23737 * send down a chain of at most three message blocks). 23738 * 23739 * We also don't use MDT for payloads with less than or equal to 23740 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 23741 */ 23742 boolean_t 23743 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 23744 { 23745 int blocks; 23746 ssize_t total, missing, size; 23747 23748 ASSERT(mp != NULL); 23749 ASSERT(hdr_len > 0); 23750 23751 size = MBLKL(mp) - hdr_len; 23752 if (size <= 0) 23753 return (B_FALSE); 23754 23755 /* The first mblk contains the header and some payload. */ 23756 blocks = 1; 23757 total = size; 23758 size %= len; 23759 missing = (size == 0) ? 0 : (len - size); 23760 mp = mp->b_cont; 23761 23762 while (mp != NULL) { 23763 /* 23764 * Give up if we encounter a zero length message block. 23765 * In practice, this should rarely happen and therefore 23766 * not worth the trouble of freeing and re-linking the 23767 * mblk from the chain to handle such case. 23768 */ 23769 if ((size = MBLKL(mp)) == 0) 23770 return (B_FALSE); 23771 23772 /* Too many payload buffers for a single Multidata message? */ 23773 if (++blocks > MULTIDATA_MAX_PBUFS) 23774 return (B_FALSE); 23775 23776 total += size; 23777 /* Is a frag distributed over two or more message blocks? */ 23778 if (missing > size) 23779 return (B_FALSE); 23780 size -= missing; 23781 23782 size %= len; 23783 missing = (size == 0) ? 0 : (len - size); 23784 23785 mp = mp->b_cont; 23786 } 23787 23788 return (total > ip_wput_frag_mdt_min); 23789 } 23790 23791 /* 23792 * Outbound IPv4 fragmentation routine using MDT. 23793 */ 23794 static void 23795 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 23796 uint32_t frag_flag, int offset) 23797 { 23798 ipha_t *ipha_orig; 23799 int i1, ip_data_end; 23800 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 23801 mblk_t *hdr_mp, *md_mp = NULL; 23802 unsigned char *hdr_ptr, *pld_ptr; 23803 multidata_t *mmd; 23804 ip_pdescinfo_t pdi; 23805 ill_t *ill; 23806 ip_stack_t *ipst = ire->ire_ipst; 23807 23808 ASSERT(DB_TYPE(mp) == M_DATA); 23809 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 23810 23811 ill = ire_to_ill(ire); 23812 ASSERT(ill != NULL); 23813 23814 ipha_orig = (ipha_t *)mp->b_rptr; 23815 mp->b_rptr += sizeof (ipha_t); 23816 23817 /* Calculate how many packets we will send out */ 23818 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 23819 pkts = (i1 + len - 1) / len; 23820 ASSERT(pkts > 1); 23821 23822 /* Allocate a message block which will hold all the IP Headers. */ 23823 wroff = ipst->ips_ip_wroff_extra; 23824 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 23825 23826 i1 = pkts * hdr_chunk_len; 23827 /* 23828 * Create the header buffer, Multidata and destination address 23829 * and SAP attribute that should be associated with it. 23830 */ 23831 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 23832 ((hdr_mp->b_wptr += i1), 23833 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 23834 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 23835 freemsg(mp); 23836 if (md_mp == NULL) { 23837 freemsg(hdr_mp); 23838 } else { 23839 free_mmd: IP_STAT(ipst, ip_frag_mdt_discarded); 23840 freemsg(md_mp); 23841 } 23842 IP_STAT(ipst, ip_frag_mdt_allocfail); 23843 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 23844 return; 23845 } 23846 IP_STAT(ipst, ip_frag_mdt_allocd); 23847 23848 /* 23849 * Add a payload buffer to the Multidata; this operation must not 23850 * fail, or otherwise our logic in this routine is broken. There 23851 * is no memory allocation done by the routine, so any returned 23852 * failure simply tells us that we've done something wrong. 23853 * 23854 * A failure tells us that either we're adding the same payload 23855 * buffer more than once, or we're trying to add more buffers than 23856 * allowed. None of the above cases should happen, and we panic 23857 * because either there's horrible heap corruption, and/or 23858 * programming mistake. 23859 */ 23860 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23861 goto pbuf_panic; 23862 23863 hdr_ptr = hdr_mp->b_rptr; 23864 pld_ptr = mp->b_rptr; 23865 23866 /* Establish the ending byte offset, based on the starting offset. */ 23867 offset <<= 3; 23868 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 23869 IP_SIMPLE_HDR_LENGTH; 23870 23871 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 23872 23873 while (pld_ptr < mp->b_wptr) { 23874 ipha_t *ipha; 23875 uint16_t offset_and_flags; 23876 uint16_t ip_len; 23877 int error; 23878 23879 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 23880 ipha = (ipha_t *)(hdr_ptr + wroff); 23881 ASSERT(OK_32PTR(ipha)); 23882 *ipha = *ipha_orig; 23883 23884 if (ip_data_end - offset > len) { 23885 offset_and_flags = IPH_MF; 23886 } else { 23887 /* 23888 * Last frag. Set len to the length of this last piece. 23889 */ 23890 len = ip_data_end - offset; 23891 /* A frag of a frag might have IPH_MF non-zero */ 23892 offset_and_flags = 23893 ntohs(ipha->ipha_fragment_offset_and_flags) & 23894 IPH_MF; 23895 } 23896 offset_and_flags |= (uint16_t)(offset >> 3); 23897 offset_and_flags |= (uint16_t)frag_flag; 23898 /* Store the offset and flags in the IP header. */ 23899 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 23900 23901 /* Store the length in the IP header. */ 23902 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 23903 ipha->ipha_length = htons(ip_len); 23904 23905 /* 23906 * Set the IP header checksum. Note that mp is just 23907 * the header, so this is easy to pass to ip_csum. 23908 */ 23909 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23910 23911 /* 23912 * Record offset and size of header and data of the next packet 23913 * in the multidata message. 23914 */ 23915 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 23916 PDESC_PLD_INIT(&pdi); 23917 i1 = MIN(mp->b_wptr - pld_ptr, len); 23918 ASSERT(i1 > 0); 23919 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 23920 if (i1 == len) { 23921 pld_ptr += len; 23922 } else { 23923 i1 = len - i1; 23924 mp = mp->b_cont; 23925 ASSERT(mp != NULL); 23926 ASSERT(MBLKL(mp) >= i1); 23927 /* 23928 * Attach the next payload message block to the 23929 * multidata message. 23930 */ 23931 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23932 goto pbuf_panic; 23933 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 23934 pld_ptr = mp->b_rptr + i1; 23935 } 23936 23937 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 23938 KM_NOSLEEP)) == NULL) { 23939 /* 23940 * Any failure other than ENOMEM indicates that we 23941 * have passed in invalid pdesc info or parameters 23942 * to mmd_addpdesc, which must not happen. 23943 * 23944 * EINVAL is a result of failure on boundary checks 23945 * against the pdesc info contents. It should not 23946 * happen, and we panic because either there's 23947 * horrible heap corruption, and/or programming 23948 * mistake. 23949 */ 23950 if (error != ENOMEM) { 23951 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 23952 "pdesc logic error detected for " 23953 "mmd %p pinfo %p (%d)\n", 23954 (void *)mmd, (void *)&pdi, error); 23955 /* NOTREACHED */ 23956 } 23957 IP_STAT(ipst, ip_frag_mdt_addpdescfail); 23958 /* Free unattached payload message blocks as well */ 23959 md_mp->b_cont = mp->b_cont; 23960 goto free_mmd; 23961 } 23962 23963 /* Advance fragment offset. */ 23964 offset += len; 23965 23966 /* Advance to location for next header in the buffer. */ 23967 hdr_ptr += hdr_chunk_len; 23968 23969 /* Did we reach the next payload message block? */ 23970 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 23971 mp = mp->b_cont; 23972 /* 23973 * Attach the next message block with payload 23974 * data to the multidata message. 23975 */ 23976 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23977 goto pbuf_panic; 23978 pld_ptr = mp->b_rptr; 23979 } 23980 } 23981 23982 ASSERT(hdr_mp->b_wptr == hdr_ptr); 23983 ASSERT(mp->b_wptr == pld_ptr); 23984 23985 /* Update IP statistics */ 23986 IP_STAT_UPDATE(ipst, ip_frag_mdt_pkt_out, pkts); 23987 23988 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates, pkts); 23989 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs); 23990 23991 len = ntohs(ipha_orig->ipha_length) + (pkts - 1) * IP_SIMPLE_HDR_LENGTH; 23992 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, pkts); 23993 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, len); 23994 23995 if (pkt_type == OB_PKT) { 23996 ire->ire_ob_pkt_count += pkts; 23997 if (ire->ire_ipif != NULL) 23998 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 23999 } else { 24000 /* The type is IB_PKT in the forwarding path. */ 24001 ire->ire_ib_pkt_count += pkts; 24002 ASSERT(!IRE_IS_LOCAL(ire)); 24003 if (ire->ire_type & IRE_BROADCAST) { 24004 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 24005 } else { 24006 UPDATE_MIB(ill->ill_ip_mib, 24007 ipIfStatsHCOutForwDatagrams, pkts); 24008 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 24009 } 24010 } 24011 ire->ire_last_used_time = lbolt; 24012 /* Send it down */ 24013 putnext(ire->ire_stq, md_mp); 24014 return; 24015 24016 pbuf_panic: 24017 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 24018 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 24019 pbuf_idx); 24020 /* NOTREACHED */ 24021 } 24022 24023 /* 24024 * Outbound IP fragmentation routine. 24025 * 24026 * NOTE : This routine does not ire_refrele the ire that is passed in 24027 * as the argument. 24028 */ 24029 static void 24030 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 24031 uint32_t frag_flag, zoneid_t zoneid, ip_stack_t *ipst) 24032 { 24033 int i1; 24034 mblk_t *ll_hdr_mp; 24035 int ll_hdr_len; 24036 int hdr_len; 24037 mblk_t *hdr_mp; 24038 ipha_t *ipha; 24039 int ip_data_end; 24040 int len; 24041 mblk_t *mp = mp_orig, *mp1; 24042 int offset; 24043 queue_t *q; 24044 uint32_t v_hlen_tos_len; 24045 mblk_t *first_mp; 24046 boolean_t mctl_present; 24047 ill_t *ill; 24048 ill_t *out_ill; 24049 mblk_t *xmit_mp; 24050 mblk_t *carve_mp; 24051 ire_t *ire1 = NULL; 24052 ire_t *save_ire = NULL; 24053 mblk_t *next_mp = NULL; 24054 boolean_t last_frag = B_FALSE; 24055 boolean_t multirt_send = B_FALSE; 24056 ire_t *first_ire = NULL; 24057 irb_t *irb = NULL; 24058 mib2_ipIfStatsEntry_t *mibptr = NULL; 24059 24060 ill = ire_to_ill(ire); 24061 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 24062 24063 BUMP_MIB(mibptr, ipIfStatsOutFragReqds); 24064 24065 if (max_frag == 0) { 24066 ip1dbg(("ip_wput_frag: ire frag size is 0" 24067 " - dropping packet\n")); 24068 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24069 freemsg(mp); 24070 return; 24071 } 24072 24073 /* 24074 * IPsec does not allow hw accelerated packets to be fragmented 24075 * This check is made in ip_wput_ipsec_out prior to coming here 24076 * via ip_wput_ire_fragmentit. 24077 * 24078 * If at this point we have an ire whose ARP request has not 24079 * been sent out, we call ip_xmit_v4->ire_arpresolve to trigger 24080 * sending of ARP query and change ire's state to ND_INCOMPLETE. 24081 * This packet and all fragmentable packets for this ire will 24082 * continue to get dropped while ire_nce->nce_state remains in 24083 * ND_INCOMPLETE. Post-ARP resolution, after ire's nce_state changes to 24084 * ND_REACHABLE, all subsquent large packets for this ire will 24085 * get fragemented and sent out by this function. 24086 */ 24087 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 24088 /* If nce_state is ND_INITIAL, trigger ARP query */ 24089 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 24090 ip1dbg(("ip_wput_frag: mac address for ire is unresolved" 24091 " - dropping packet\n")); 24092 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24093 freemsg(mp); 24094 return; 24095 } 24096 24097 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 24098 "ip_wput_frag_start:"); 24099 24100 if (mp->b_datap->db_type == M_CTL) { 24101 first_mp = mp; 24102 mp_orig = mp = mp->b_cont; 24103 mctl_present = B_TRUE; 24104 } else { 24105 first_mp = mp; 24106 mctl_present = B_FALSE; 24107 } 24108 24109 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 24110 ipha = (ipha_t *)mp->b_rptr; 24111 24112 /* 24113 * If the Don't Fragment flag is on, generate an ICMP destination 24114 * unreachable, fragmentation needed. 24115 */ 24116 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 24117 if (offset & IPH_DF) { 24118 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24119 if (is_system_labeled()) { 24120 max_frag = tsol_pmtu_adjust(mp, ire->ire_max_frag, 24121 ire->ire_max_frag - max_frag, AF_INET); 24122 } 24123 /* 24124 * Need to compute hdr checksum if called from ip_wput_ire. 24125 * Note that ip_rput_forward verifies the checksum before 24126 * calling this routine so in that case this is a noop. 24127 */ 24128 ipha->ipha_hdr_checksum = 0; 24129 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24130 icmp_frag_needed(ire->ire_stq, first_mp, max_frag, zoneid, 24131 ipst); 24132 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24133 "ip_wput_frag_end:(%S)", 24134 "don't fragment"); 24135 return; 24136 } 24137 /* 24138 * Labeled systems adjust max_frag if they add a label 24139 * to send the correct path mtu. We need the real mtu since we 24140 * are fragmenting the packet after label adjustment. 24141 */ 24142 if (is_system_labeled()) 24143 max_frag = ire->ire_max_frag; 24144 if (mctl_present) 24145 freeb(first_mp); 24146 /* 24147 * Establish the starting offset. May not be zero if we are fragging 24148 * a fragment that is being forwarded. 24149 */ 24150 offset = offset & IPH_OFFSET; 24151 24152 /* TODO why is this test needed? */ 24153 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 24154 if (((max_frag - LENGTH) & ~7) < 8) { 24155 /* TODO: notify ulp somehow */ 24156 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24157 freemsg(mp); 24158 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24159 "ip_wput_frag_end:(%S)", 24160 "len < 8"); 24161 return; 24162 } 24163 24164 hdr_len = (V_HLEN & 0xF) << 2; 24165 24166 ipha->ipha_hdr_checksum = 0; 24167 24168 /* 24169 * Establish the number of bytes maximum per frag, after putting 24170 * in the header. 24171 */ 24172 len = (max_frag - hdr_len) & ~7; 24173 24174 /* Check if we can use MDT to send out the frags. */ 24175 ASSERT(!IRE_IS_LOCAL(ire)); 24176 if (hdr_len == IP_SIMPLE_HDR_LENGTH && 24177 ipst->ips_ip_multidata_outbound && 24178 !(ire->ire_flags & RTF_MULTIRT) && 24179 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 24180 ill != NULL && ILL_MDT_CAPABLE(ill) && 24181 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 24182 ASSERT(ill->ill_mdt_capab != NULL); 24183 if (!ill->ill_mdt_capab->ill_mdt_on) { 24184 /* 24185 * If MDT has been previously turned off in the past, 24186 * and we currently can do MDT (due to IPQoS policy 24187 * removal, etc.) then enable it for this interface. 24188 */ 24189 ill->ill_mdt_capab->ill_mdt_on = 1; 24190 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 24191 ill->ill_name)); 24192 } 24193 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 24194 offset); 24195 return; 24196 } 24197 24198 /* Get a copy of the header for the trailing frags */ 24199 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst); 24200 if (!hdr_mp) { 24201 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24202 freemsg(mp); 24203 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24204 "ip_wput_frag_end:(%S)", 24205 "couldn't copy hdr"); 24206 return; 24207 } 24208 if (DB_CRED(mp) != NULL) 24209 mblk_setcred(hdr_mp, DB_CRED(mp)); 24210 24211 /* Store the starting offset, with the MoreFrags flag. */ 24212 i1 = offset | IPH_MF | frag_flag; 24213 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 24214 24215 /* Establish the ending byte offset, based on the starting offset. */ 24216 offset <<= 3; 24217 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 24218 24219 /* Store the length of the first fragment in the IP header. */ 24220 i1 = len + hdr_len; 24221 ASSERT(i1 <= IP_MAXPACKET); 24222 ipha->ipha_length = htons((uint16_t)i1); 24223 24224 /* 24225 * Compute the IP header checksum for the first frag. We have to 24226 * watch out that we stop at the end of the header. 24227 */ 24228 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24229 24230 /* 24231 * Now carve off the first frag. Note that this will include the 24232 * original IP header. 24233 */ 24234 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 24235 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24236 freeb(hdr_mp); 24237 freemsg(mp_orig); 24238 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24239 "ip_wput_frag_end:(%S)", 24240 "couldn't carve first"); 24241 return; 24242 } 24243 24244 /* 24245 * Multirouting case. Each fragment is replicated 24246 * via all non-condemned RTF_MULTIRT routes 24247 * currently resolved. 24248 * We ensure that first_ire is the first RTF_MULTIRT 24249 * ire in the bucket. 24250 */ 24251 if (ire->ire_flags & RTF_MULTIRT) { 24252 irb = ire->ire_bucket; 24253 ASSERT(irb != NULL); 24254 24255 multirt_send = B_TRUE; 24256 24257 /* Make sure we do not omit any multiroute ire. */ 24258 IRB_REFHOLD(irb); 24259 for (first_ire = irb->irb_ire; 24260 first_ire != NULL; 24261 first_ire = first_ire->ire_next) { 24262 if ((first_ire->ire_flags & RTF_MULTIRT) && 24263 (first_ire->ire_addr == ire->ire_addr) && 24264 !(first_ire->ire_marks & 24265 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) { 24266 break; 24267 } 24268 } 24269 24270 if (first_ire != NULL) { 24271 if (first_ire != ire) { 24272 IRE_REFHOLD(first_ire); 24273 /* 24274 * Do not release the ire passed in 24275 * as the argument. 24276 */ 24277 ire = first_ire; 24278 } else { 24279 first_ire = NULL; 24280 } 24281 } 24282 IRB_REFRELE(irb); 24283 24284 /* 24285 * Save the first ire; we will need to restore it 24286 * for the trailing frags. 24287 * We REFHOLD save_ire, as each iterated ire will be 24288 * REFRELEd. 24289 */ 24290 save_ire = ire; 24291 IRE_REFHOLD(save_ire); 24292 } 24293 24294 /* 24295 * First fragment emission loop. 24296 * In most cases, the emission loop below is entered only 24297 * once. Only in the case where the ire holds the RTF_MULTIRT 24298 * flag, do we loop to process all RTF_MULTIRT ires in the 24299 * bucket, and send the fragment through all crossed 24300 * RTF_MULTIRT routes. 24301 */ 24302 do { 24303 if (ire->ire_flags & RTF_MULTIRT) { 24304 /* 24305 * We are in a multiple send case, need to get 24306 * the next ire and make a copy of the packet. 24307 * ire1 holds here the next ire to process in the 24308 * bucket. If multirouting is expected, 24309 * any non-RTF_MULTIRT ire that has the 24310 * right destination address is ignored. 24311 * 24312 * We have to take into account the MTU of 24313 * each walked ire. max_frag is set by the 24314 * the caller and generally refers to 24315 * the primary ire entry. Here we ensure that 24316 * no route with a lower MTU will be used, as 24317 * fragments are carved once for all ires, 24318 * then replicated. 24319 */ 24320 ASSERT(irb != NULL); 24321 IRB_REFHOLD(irb); 24322 for (ire1 = ire->ire_next; 24323 ire1 != NULL; 24324 ire1 = ire1->ire_next) { 24325 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 24326 continue; 24327 if (ire1->ire_addr != ire->ire_addr) 24328 continue; 24329 if (ire1->ire_marks & 24330 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 24331 continue; 24332 /* 24333 * Ensure we do not exceed the MTU 24334 * of the next route. 24335 */ 24336 if (ire1->ire_max_frag < max_frag) { 24337 ip_multirt_bad_mtu(ire1, max_frag); 24338 continue; 24339 } 24340 24341 /* Got one. */ 24342 IRE_REFHOLD(ire1); 24343 break; 24344 } 24345 IRB_REFRELE(irb); 24346 24347 if (ire1 != NULL) { 24348 next_mp = copyb(mp); 24349 if ((next_mp == NULL) || 24350 ((mp->b_cont != NULL) && 24351 ((next_mp->b_cont = 24352 dupmsg(mp->b_cont)) == NULL))) { 24353 freemsg(next_mp); 24354 next_mp = NULL; 24355 ire_refrele(ire1); 24356 ire1 = NULL; 24357 } 24358 } 24359 24360 /* Last multiroute ire; don't loop anymore. */ 24361 if (ire1 == NULL) { 24362 multirt_send = B_FALSE; 24363 } 24364 } 24365 24366 ll_hdr_len = 0; 24367 LOCK_IRE_FP_MP(ire); 24368 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24369 if (ll_hdr_mp != NULL) { 24370 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24371 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 24372 } else { 24373 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24374 } 24375 24376 /* If there is a transmit header, get a copy for this frag. */ 24377 /* 24378 * TODO: should check db_ref before calling ip_carve_mp since 24379 * it might give us a dup. 24380 */ 24381 if (!ll_hdr_mp) { 24382 /* No xmit header. */ 24383 xmit_mp = mp; 24384 24385 /* We have a link-layer header that can fit in our mblk. */ 24386 } else if (mp->b_datap->db_ref == 1 && 24387 ll_hdr_len != 0 && 24388 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24389 /* M_DATA fastpath */ 24390 mp->b_rptr -= ll_hdr_len; 24391 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 24392 xmit_mp = mp; 24393 24394 /* Corner case if copyb has failed */ 24395 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 24396 UNLOCK_IRE_FP_MP(ire); 24397 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24398 freeb(hdr_mp); 24399 freemsg(mp); 24400 freemsg(mp_orig); 24401 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24402 "ip_wput_frag_end:(%S)", 24403 "discard"); 24404 24405 if (multirt_send) { 24406 ASSERT(ire1); 24407 ASSERT(next_mp); 24408 24409 freemsg(next_mp); 24410 ire_refrele(ire1); 24411 } 24412 if (save_ire != NULL) 24413 IRE_REFRELE(save_ire); 24414 24415 if (first_ire != NULL) 24416 ire_refrele(first_ire); 24417 return; 24418 24419 /* 24420 * Case of res_mp OR the fastpath mp can't fit 24421 * in the mblk 24422 */ 24423 } else { 24424 xmit_mp->b_cont = mp; 24425 if (DB_CRED(mp) != NULL) 24426 mblk_setcred(xmit_mp, DB_CRED(mp)); 24427 /* 24428 * Get priority marking, if any. 24429 * We propagate the CoS marking from the 24430 * original packet that went to QoS processing 24431 * in ip_wput_ire to the newly carved mp. 24432 */ 24433 if (DB_TYPE(xmit_mp) == M_DATA) 24434 xmit_mp->b_band = mp->b_band; 24435 } 24436 UNLOCK_IRE_FP_MP(ire); 24437 24438 q = ire->ire_stq; 24439 out_ill = (ill_t *)q->q_ptr; 24440 24441 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24442 24443 DTRACE_PROBE4(ip4__physical__out__start, 24444 ill_t *, NULL, ill_t *, out_ill, 24445 ipha_t *, ipha, mblk_t *, xmit_mp); 24446 24447 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24448 ipst->ips_ipv4firewall_physical_out, 24449 NULL, out_ill, ipha, xmit_mp, mp, 0, ipst); 24450 24451 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, xmit_mp); 24452 24453 if (xmit_mp != NULL) { 24454 putnext(q, xmit_mp); 24455 24456 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 24457 UPDATE_MIB(out_ill->ill_ip_mib, 24458 ipIfStatsHCOutOctets, i1); 24459 24460 if (pkt_type != OB_PKT) { 24461 /* 24462 * Update the packet count and MIB stats 24463 * of trailing RTF_MULTIRT ires. 24464 */ 24465 UPDATE_OB_PKT_COUNT(ire); 24466 BUMP_MIB(out_ill->ill_ip_mib, 24467 ipIfStatsOutFragReqds); 24468 } 24469 } 24470 24471 if (multirt_send) { 24472 /* 24473 * We are in a multiple send case; look for 24474 * the next ire and re-enter the loop. 24475 */ 24476 ASSERT(ire1); 24477 ASSERT(next_mp); 24478 /* REFRELE the current ire before looping */ 24479 ire_refrele(ire); 24480 ire = ire1; 24481 ire1 = NULL; 24482 mp = next_mp; 24483 next_mp = NULL; 24484 } 24485 } while (multirt_send); 24486 24487 ASSERT(ire1 == NULL); 24488 24489 /* Restore the original ire; we need it for the trailing frags */ 24490 if (save_ire != NULL) { 24491 /* REFRELE the last iterated ire */ 24492 ire_refrele(ire); 24493 /* save_ire has been REFHOLDed */ 24494 ire = save_ire; 24495 save_ire = NULL; 24496 q = ire->ire_stq; 24497 } 24498 24499 if (pkt_type == OB_PKT) { 24500 UPDATE_OB_PKT_COUNT(ire); 24501 } else { 24502 out_ill = (ill_t *)q->q_ptr; 24503 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 24504 UPDATE_IB_PKT_COUNT(ire); 24505 } 24506 24507 /* Advance the offset to the second frag starting point. */ 24508 offset += len; 24509 /* 24510 * Update hdr_len from the copied header - there might be less options 24511 * in the later fragments. 24512 */ 24513 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 24514 /* Loop until done. */ 24515 for (;;) { 24516 uint16_t offset_and_flags; 24517 uint16_t ip_len; 24518 24519 if (ip_data_end - offset > len) { 24520 /* 24521 * Carve off the appropriate amount from the original 24522 * datagram. 24523 */ 24524 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24525 mp = NULL; 24526 break; 24527 } 24528 /* 24529 * More frags after this one. Get another copy 24530 * of the header. 24531 */ 24532 if (carve_mp->b_datap->db_ref == 1 && 24533 hdr_mp->b_wptr - hdr_mp->b_rptr < 24534 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24535 /* Inline IP header */ 24536 carve_mp->b_rptr -= hdr_mp->b_wptr - 24537 hdr_mp->b_rptr; 24538 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24539 hdr_mp->b_wptr - hdr_mp->b_rptr); 24540 mp = carve_mp; 24541 } else { 24542 if (!(mp = copyb(hdr_mp))) { 24543 freemsg(carve_mp); 24544 break; 24545 } 24546 /* Get priority marking, if any. */ 24547 mp->b_band = carve_mp->b_band; 24548 mp->b_cont = carve_mp; 24549 } 24550 ipha = (ipha_t *)mp->b_rptr; 24551 offset_and_flags = IPH_MF; 24552 } else { 24553 /* 24554 * Last frag. Consume the header. Set len to 24555 * the length of this last piece. 24556 */ 24557 len = ip_data_end - offset; 24558 24559 /* 24560 * Carve off the appropriate amount from the original 24561 * datagram. 24562 */ 24563 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24564 mp = NULL; 24565 break; 24566 } 24567 if (carve_mp->b_datap->db_ref == 1 && 24568 hdr_mp->b_wptr - hdr_mp->b_rptr < 24569 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24570 /* Inline IP header */ 24571 carve_mp->b_rptr -= hdr_mp->b_wptr - 24572 hdr_mp->b_rptr; 24573 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24574 hdr_mp->b_wptr - hdr_mp->b_rptr); 24575 mp = carve_mp; 24576 freeb(hdr_mp); 24577 hdr_mp = mp; 24578 } else { 24579 mp = hdr_mp; 24580 /* Get priority marking, if any. */ 24581 mp->b_band = carve_mp->b_band; 24582 mp->b_cont = carve_mp; 24583 } 24584 ipha = (ipha_t *)mp->b_rptr; 24585 /* A frag of a frag might have IPH_MF non-zero */ 24586 offset_and_flags = 24587 ntohs(ipha->ipha_fragment_offset_and_flags) & 24588 IPH_MF; 24589 } 24590 offset_and_flags |= (uint16_t)(offset >> 3); 24591 offset_and_flags |= (uint16_t)frag_flag; 24592 /* Store the offset and flags in the IP header. */ 24593 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24594 24595 /* Store the length in the IP header. */ 24596 ip_len = (uint16_t)(len + hdr_len); 24597 ipha->ipha_length = htons(ip_len); 24598 24599 /* 24600 * Set the IP header checksum. Note that mp is just 24601 * the header, so this is easy to pass to ip_csum. 24602 */ 24603 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24604 24605 /* Attach a transmit header, if any, and ship it. */ 24606 if (pkt_type == OB_PKT) { 24607 UPDATE_OB_PKT_COUNT(ire); 24608 } else { 24609 out_ill = (ill_t *)q->q_ptr; 24610 BUMP_MIB(out_ill->ill_ip_mib, 24611 ipIfStatsHCOutForwDatagrams); 24612 UPDATE_IB_PKT_COUNT(ire); 24613 } 24614 24615 if (ire->ire_flags & RTF_MULTIRT) { 24616 irb = ire->ire_bucket; 24617 ASSERT(irb != NULL); 24618 24619 multirt_send = B_TRUE; 24620 24621 /* 24622 * Save the original ire; we will need to restore it 24623 * for the tailing frags. 24624 */ 24625 save_ire = ire; 24626 IRE_REFHOLD(save_ire); 24627 } 24628 /* 24629 * Emission loop for this fragment, similar 24630 * to what is done for the first fragment. 24631 */ 24632 do { 24633 if (multirt_send) { 24634 /* 24635 * We are in a multiple send case, need to get 24636 * the next ire and make a copy of the packet. 24637 */ 24638 ASSERT(irb != NULL); 24639 IRB_REFHOLD(irb); 24640 for (ire1 = ire->ire_next; 24641 ire1 != NULL; 24642 ire1 = ire1->ire_next) { 24643 if (!(ire1->ire_flags & RTF_MULTIRT)) 24644 continue; 24645 if (ire1->ire_addr != ire->ire_addr) 24646 continue; 24647 if (ire1->ire_marks & 24648 (IRE_MARK_CONDEMNED| 24649 IRE_MARK_HIDDEN)) { 24650 continue; 24651 } 24652 /* 24653 * Ensure we do not exceed the MTU 24654 * of the next route. 24655 */ 24656 if (ire1->ire_max_frag < max_frag) { 24657 ip_multirt_bad_mtu(ire1, 24658 max_frag); 24659 continue; 24660 } 24661 24662 /* Got one. */ 24663 IRE_REFHOLD(ire1); 24664 break; 24665 } 24666 IRB_REFRELE(irb); 24667 24668 if (ire1 != NULL) { 24669 next_mp = copyb(mp); 24670 if ((next_mp == NULL) || 24671 ((mp->b_cont != NULL) && 24672 ((next_mp->b_cont = 24673 dupmsg(mp->b_cont)) == NULL))) { 24674 freemsg(next_mp); 24675 next_mp = NULL; 24676 ire_refrele(ire1); 24677 ire1 = NULL; 24678 } 24679 } 24680 24681 /* Last multiroute ire; don't loop anymore. */ 24682 if (ire1 == NULL) { 24683 multirt_send = B_FALSE; 24684 } 24685 } 24686 24687 /* Update transmit header */ 24688 ll_hdr_len = 0; 24689 LOCK_IRE_FP_MP(ire); 24690 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24691 if (ll_hdr_mp != NULL) { 24692 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24693 ll_hdr_len = MBLKL(ll_hdr_mp); 24694 } else { 24695 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24696 } 24697 24698 if (!ll_hdr_mp) { 24699 xmit_mp = mp; 24700 24701 /* 24702 * We have link-layer header that can fit in 24703 * our mblk. 24704 */ 24705 } else if (mp->b_datap->db_ref == 1 && 24706 ll_hdr_len != 0 && 24707 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24708 /* M_DATA fastpath */ 24709 mp->b_rptr -= ll_hdr_len; 24710 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 24711 ll_hdr_len); 24712 xmit_mp = mp; 24713 24714 /* 24715 * Case of res_mp OR the fastpath mp can't fit 24716 * in the mblk 24717 */ 24718 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 24719 xmit_mp->b_cont = mp; 24720 if (DB_CRED(mp) != NULL) 24721 mblk_setcred(xmit_mp, DB_CRED(mp)); 24722 /* Get priority marking, if any. */ 24723 if (DB_TYPE(xmit_mp) == M_DATA) 24724 xmit_mp->b_band = mp->b_band; 24725 24726 /* Corner case if copyb failed */ 24727 } else { 24728 /* 24729 * Exit both the replication and 24730 * fragmentation loops. 24731 */ 24732 UNLOCK_IRE_FP_MP(ire); 24733 goto drop_pkt; 24734 } 24735 UNLOCK_IRE_FP_MP(ire); 24736 24737 mp1 = mp; 24738 out_ill = (ill_t *)q->q_ptr; 24739 24740 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24741 24742 DTRACE_PROBE4(ip4__physical__out__start, 24743 ill_t *, NULL, ill_t *, out_ill, 24744 ipha_t *, ipha, mblk_t *, xmit_mp); 24745 24746 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24747 ipst->ips_ipv4firewall_physical_out, 24748 NULL, out_ill, ipha, xmit_mp, mp, 0, ipst); 24749 24750 DTRACE_PROBE1(ip4__physical__out__end, 24751 mblk_t *, xmit_mp); 24752 24753 if (mp != mp1 && hdr_mp == mp1) 24754 hdr_mp = mp; 24755 if (mp != mp1 && mp_orig == mp1) 24756 mp_orig = mp; 24757 24758 if (xmit_mp != NULL) { 24759 putnext(q, xmit_mp); 24760 24761 BUMP_MIB(out_ill->ill_ip_mib, 24762 ipIfStatsHCOutTransmits); 24763 UPDATE_MIB(out_ill->ill_ip_mib, 24764 ipIfStatsHCOutOctets, ip_len); 24765 24766 if (pkt_type != OB_PKT) { 24767 /* 24768 * Update the packet count of trailing 24769 * RTF_MULTIRT ires. 24770 */ 24771 UPDATE_OB_PKT_COUNT(ire); 24772 } 24773 } 24774 24775 /* All done if we just consumed the hdr_mp. */ 24776 if (mp == hdr_mp) { 24777 last_frag = B_TRUE; 24778 BUMP_MIB(out_ill->ill_ip_mib, 24779 ipIfStatsOutFragOKs); 24780 } 24781 24782 if (multirt_send) { 24783 /* 24784 * We are in a multiple send case; look for 24785 * the next ire and re-enter the loop. 24786 */ 24787 ASSERT(ire1); 24788 ASSERT(next_mp); 24789 /* REFRELE the current ire before looping */ 24790 ire_refrele(ire); 24791 ire = ire1; 24792 ire1 = NULL; 24793 q = ire->ire_stq; 24794 mp = next_mp; 24795 next_mp = NULL; 24796 } 24797 } while (multirt_send); 24798 /* 24799 * Restore the original ire; we need it for the 24800 * trailing frags 24801 */ 24802 if (save_ire != NULL) { 24803 ASSERT(ire1 == NULL); 24804 /* REFRELE the last iterated ire */ 24805 ire_refrele(ire); 24806 /* save_ire has been REFHOLDed */ 24807 ire = save_ire; 24808 q = ire->ire_stq; 24809 save_ire = NULL; 24810 } 24811 24812 if (last_frag) { 24813 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24814 "ip_wput_frag_end:(%S)", 24815 "consumed hdr_mp"); 24816 24817 if (first_ire != NULL) 24818 ire_refrele(first_ire); 24819 return; 24820 } 24821 /* Otherwise, advance and loop. */ 24822 offset += len; 24823 } 24824 24825 drop_pkt: 24826 /* Clean up following allocation failure. */ 24827 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24828 freemsg(mp); 24829 if (mp != hdr_mp) 24830 freeb(hdr_mp); 24831 if (mp != mp_orig) 24832 freemsg(mp_orig); 24833 24834 if (save_ire != NULL) 24835 IRE_REFRELE(save_ire); 24836 if (first_ire != NULL) 24837 ire_refrele(first_ire); 24838 24839 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24840 "ip_wput_frag_end:(%S)", 24841 "end--alloc failure"); 24842 } 24843 24844 /* 24845 * Copy the header plus those options which have the copy bit set 24846 */ 24847 static mblk_t * 24848 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst) 24849 { 24850 mblk_t *mp; 24851 uchar_t *up; 24852 24853 /* 24854 * Quick check if we need to look for options without the copy bit 24855 * set 24856 */ 24857 mp = allocb(ipst->ips_ip_wroff_extra + hdr_len, BPRI_HI); 24858 if (!mp) 24859 return (mp); 24860 mp->b_rptr += ipst->ips_ip_wroff_extra; 24861 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 24862 bcopy(rptr, mp->b_rptr, hdr_len); 24863 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra; 24864 return (mp); 24865 } 24866 up = mp->b_rptr; 24867 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 24868 up += IP_SIMPLE_HDR_LENGTH; 24869 rptr += IP_SIMPLE_HDR_LENGTH; 24870 hdr_len -= IP_SIMPLE_HDR_LENGTH; 24871 while (hdr_len > 0) { 24872 uint32_t optval; 24873 uint32_t optlen; 24874 24875 optval = *rptr; 24876 if (optval == IPOPT_EOL) 24877 break; 24878 if (optval == IPOPT_NOP) 24879 optlen = 1; 24880 else 24881 optlen = rptr[1]; 24882 if (optval & IPOPT_COPY) { 24883 bcopy(rptr, up, optlen); 24884 up += optlen; 24885 } 24886 rptr += optlen; 24887 hdr_len -= optlen; 24888 } 24889 /* 24890 * Make sure that we drop an even number of words by filling 24891 * with EOL to the next word boundary. 24892 */ 24893 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 24894 hdr_len & 0x3; hdr_len++) 24895 *up++ = IPOPT_EOL; 24896 mp->b_wptr = up; 24897 /* Update header length */ 24898 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 24899 return (mp); 24900 } 24901 24902 /* 24903 * Delivery to local recipients including fanout to multiple recipients. 24904 * Does not do checksumming of UDP/TCP. 24905 * Note: q should be the read side queue for either the ill or conn. 24906 * Note: rq should be the read side q for the lower (ill) stream. 24907 * We don't send packets to IPPF processing, thus the last argument 24908 * to all the fanout calls are B_FALSE. 24909 */ 24910 void 24911 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 24912 int fanout_flags, zoneid_t zoneid) 24913 { 24914 uint32_t protocol; 24915 mblk_t *first_mp; 24916 boolean_t mctl_present; 24917 int ire_type; 24918 #define rptr ((uchar_t *)ipha) 24919 ip_stack_t *ipst = ill->ill_ipst; 24920 24921 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 24922 "ip_wput_local_start: q %p", q); 24923 24924 if (ire != NULL) { 24925 ire_type = ire->ire_type; 24926 } else { 24927 /* 24928 * Only ip_multicast_loopback() calls us with a NULL ire. If the 24929 * packet is not multicast, we can't tell the ire type. 24930 */ 24931 ASSERT(CLASSD(ipha->ipha_dst)); 24932 ire_type = IRE_BROADCAST; 24933 } 24934 24935 first_mp = mp; 24936 if (first_mp->b_datap->db_type == M_CTL) { 24937 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 24938 if (!io->ipsec_out_secure) { 24939 /* 24940 * This ipsec_out_t was allocated in ip_wput 24941 * for multicast packets to store the ill_index. 24942 * As this is being delivered locally, we don't 24943 * need this anymore. 24944 */ 24945 mp = first_mp->b_cont; 24946 freeb(first_mp); 24947 first_mp = mp; 24948 mctl_present = B_FALSE; 24949 } else { 24950 /* 24951 * Convert IPSEC_OUT to IPSEC_IN, preserving all 24952 * security properties for the looped-back packet. 24953 */ 24954 mctl_present = B_TRUE; 24955 mp = first_mp->b_cont; 24956 ASSERT(mp != NULL); 24957 ipsec_out_to_in(first_mp); 24958 } 24959 } else { 24960 mctl_present = B_FALSE; 24961 } 24962 24963 DTRACE_PROBE4(ip4__loopback__in__start, 24964 ill_t *, ill, ill_t *, NULL, 24965 ipha_t *, ipha, mblk_t *, first_mp); 24966 24967 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 24968 ipst->ips_ipv4firewall_loopback_in, 24969 ill, NULL, ipha, first_mp, mp, 0, ipst); 24970 24971 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, first_mp); 24972 24973 if (first_mp == NULL) 24974 return; 24975 24976 ipst->ips_loopback_packets++; 24977 24978 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 24979 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 24980 if (!IS_SIMPLE_IPH(ipha)) { 24981 ip_wput_local_options(ipha, ipst); 24982 } 24983 24984 protocol = ipha->ipha_protocol; 24985 switch (protocol) { 24986 case IPPROTO_ICMP: { 24987 ire_t *ire_zone; 24988 ilm_t *ilm; 24989 mblk_t *mp1; 24990 zoneid_t last_zoneid; 24991 24992 if (CLASSD(ipha->ipha_dst) && !IS_LOOPBACK(ill)) { 24993 ASSERT(ire_type == IRE_BROADCAST); 24994 /* 24995 * In the multicast case, applications may have joined 24996 * the group from different zones, so we need to deliver 24997 * the packet to each of them. Loop through the 24998 * multicast memberships structures (ilm) on the receive 24999 * ill and send a copy of the packet up each matching 25000 * one. However, we don't do this for multicasts sent on 25001 * the loopback interface (PHYI_LOOPBACK flag set) as 25002 * they must stay in the sender's zone. 25003 * 25004 * ilm_add_v6() ensures that ilms in the same zone are 25005 * contiguous in the ill_ilm list. We use this property 25006 * to avoid sending duplicates needed when two 25007 * applications in the same zone join the same group on 25008 * different logical interfaces: we ignore the ilm if 25009 * it's zoneid is the same as the last matching one. 25010 * In addition, the sending of the packet for 25011 * ire_zoneid is delayed until all of the other ilms 25012 * have been exhausted. 25013 */ 25014 last_zoneid = -1; 25015 ILM_WALKER_HOLD(ill); 25016 for (ilm = ill->ill_ilm; ilm != NULL; 25017 ilm = ilm->ilm_next) { 25018 if ((ilm->ilm_flags & ILM_DELETED) || 25019 ipha->ipha_dst != ilm->ilm_addr || 25020 ilm->ilm_zoneid == last_zoneid || 25021 ilm->ilm_zoneid == zoneid || 25022 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 25023 continue; 25024 mp1 = ip_copymsg(first_mp); 25025 if (mp1 == NULL) 25026 continue; 25027 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25028 mctl_present, B_FALSE, ill, 25029 ilm->ilm_zoneid); 25030 last_zoneid = ilm->ilm_zoneid; 25031 } 25032 ILM_WALKER_RELE(ill); 25033 /* 25034 * Loopback case: the sending endpoint has 25035 * IP_MULTICAST_LOOP disabled, therefore we don't 25036 * dispatch the multicast packet to the sending zone. 25037 */ 25038 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 25039 freemsg(first_mp); 25040 return; 25041 } 25042 } else if (ire_type == IRE_BROADCAST) { 25043 /* 25044 * In the broadcast case, there may be many zones 25045 * which need a copy of the packet delivered to them. 25046 * There is one IRE_BROADCAST per broadcast address 25047 * and per zone; we walk those using a helper function. 25048 * In addition, the sending of the packet for zoneid is 25049 * delayed until all of the other ires have been 25050 * processed. 25051 */ 25052 IRB_REFHOLD(ire->ire_bucket); 25053 ire_zone = NULL; 25054 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 25055 ire)) != NULL) { 25056 mp1 = ip_copymsg(first_mp); 25057 if (mp1 == NULL) 25058 continue; 25059 25060 UPDATE_IB_PKT_COUNT(ire_zone); 25061 ire_zone->ire_last_used_time = lbolt; 25062 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25063 mctl_present, B_FALSE, ill, 25064 ire_zone->ire_zoneid); 25065 } 25066 IRB_REFRELE(ire->ire_bucket); 25067 } 25068 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 25069 0, mctl_present, B_FALSE, ill, zoneid); 25070 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25071 "ip_wput_local_end: q %p (%S)", 25072 q, "icmp"); 25073 return; 25074 } 25075 case IPPROTO_IGMP: 25076 if ((mp = igmp_input(q, mp, ill)) == NULL) { 25077 /* Bad packet - discarded by igmp_input */ 25078 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25079 "ip_wput_local_end: q %p (%S)", 25080 q, "igmp_input--bad packet"); 25081 if (mctl_present) 25082 freeb(first_mp); 25083 return; 25084 } 25085 /* 25086 * igmp_input() may have returned the pulled up message. 25087 * So first_mp and ipha need to be reinitialized. 25088 */ 25089 ipha = (ipha_t *)mp->b_rptr; 25090 if (mctl_present) 25091 first_mp->b_cont = mp; 25092 else 25093 first_mp = mp; 25094 /* deliver to local raw users */ 25095 break; 25096 case IPPROTO_ENCAP: 25097 /* 25098 * This case is covered by either ip_fanout_proto, or by 25099 * the above security processing for self-tunneled packets. 25100 */ 25101 break; 25102 case IPPROTO_UDP: { 25103 uint16_t *up; 25104 uint32_t ports; 25105 25106 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 25107 UDP_PORTS_OFFSET); 25108 /* Force a 'valid' checksum. */ 25109 up[3] = 0; 25110 25111 ports = *(uint32_t *)up; 25112 ip_fanout_udp(q, first_mp, ill, ipha, ports, 25113 (ire_type == IRE_BROADCAST), 25114 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25115 IP_FF_SEND_SLLA | IP_FF_IPINFO, mctl_present, B_FALSE, 25116 ill, zoneid); 25117 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25118 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 25119 return; 25120 } 25121 case IPPROTO_TCP: { 25122 25123 /* 25124 * For TCP, discard broadcast packets. 25125 */ 25126 if ((ushort_t)ire_type == IRE_BROADCAST) { 25127 freemsg(first_mp); 25128 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 25129 ip2dbg(("ip_wput_local: discard broadcast\n")); 25130 return; 25131 } 25132 25133 if (mp->b_datap->db_type == M_DATA) { 25134 /* 25135 * M_DATA mblk, so init mblk (chain) for no struio(). 25136 */ 25137 mblk_t *mp1 = mp; 25138 25139 do { 25140 mp1->b_datap->db_struioflag = 0; 25141 } while ((mp1 = mp1->b_cont) != NULL); 25142 } 25143 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 25144 <= mp->b_wptr); 25145 ip_fanout_tcp(q, first_mp, ill, ipha, 25146 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25147 IP_FF_SYN_ADDIRE | IP_FF_IPINFO, 25148 mctl_present, B_FALSE, zoneid); 25149 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25150 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 25151 return; 25152 } 25153 case IPPROTO_SCTP: 25154 { 25155 uint32_t ports; 25156 25157 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 25158 ip_fanout_sctp(first_mp, ill, ipha, ports, 25159 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25160 IP_FF_IPINFO, mctl_present, B_FALSE, zoneid); 25161 return; 25162 } 25163 25164 default: 25165 break; 25166 } 25167 /* 25168 * Find a client for some other protocol. We give 25169 * copies to multiple clients, if more than one is 25170 * bound. 25171 */ 25172 ip_fanout_proto(q, first_mp, ill, ipha, 25173 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 25174 mctl_present, B_FALSE, ill, zoneid); 25175 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25176 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 25177 #undef rptr 25178 } 25179 25180 /* 25181 * Update any source route, record route, or timestamp options. 25182 * Check that we are at end of strict source route. 25183 * The options have been sanity checked by ip_wput_options(). 25184 */ 25185 static void 25186 ip_wput_local_options(ipha_t *ipha, ip_stack_t *ipst) 25187 { 25188 ipoptp_t opts; 25189 uchar_t *opt; 25190 uint8_t optval; 25191 uint8_t optlen; 25192 ipaddr_t dst; 25193 uint32_t ts; 25194 ire_t *ire; 25195 timestruc_t now; 25196 25197 ip2dbg(("ip_wput_local_options\n")); 25198 for (optval = ipoptp_first(&opts, ipha); 25199 optval != IPOPT_EOL; 25200 optval = ipoptp_next(&opts)) { 25201 opt = opts.ipoptp_cur; 25202 optlen = opts.ipoptp_len; 25203 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 25204 switch (optval) { 25205 uint32_t off; 25206 case IPOPT_SSRR: 25207 case IPOPT_LSRR: 25208 off = opt[IPOPT_OFFSET]; 25209 off--; 25210 if (optlen < IP_ADDR_LEN || 25211 off > optlen - IP_ADDR_LEN) { 25212 /* End of source route */ 25213 break; 25214 } 25215 /* 25216 * This will only happen if two consecutive entries 25217 * in the source route contains our address or if 25218 * it is a packet with a loose source route which 25219 * reaches us before consuming the whole source route 25220 */ 25221 ip1dbg(("ip_wput_local_options: not end of SR\n")); 25222 if (optval == IPOPT_SSRR) { 25223 return; 25224 } 25225 /* 25226 * Hack: instead of dropping the packet truncate the 25227 * source route to what has been used by filling the 25228 * rest with IPOPT_NOP. 25229 */ 25230 opt[IPOPT_OLEN] = (uint8_t)off; 25231 while (off < optlen) { 25232 opt[off++] = IPOPT_NOP; 25233 } 25234 break; 25235 case IPOPT_RR: 25236 off = opt[IPOPT_OFFSET]; 25237 off--; 25238 if (optlen < IP_ADDR_LEN || 25239 off > optlen - IP_ADDR_LEN) { 25240 /* No more room - ignore */ 25241 ip1dbg(( 25242 "ip_wput_forward_options: end of RR\n")); 25243 break; 25244 } 25245 dst = htonl(INADDR_LOOPBACK); 25246 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25247 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25248 break; 25249 case IPOPT_TS: 25250 /* Insert timestamp if there is romm */ 25251 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25252 case IPOPT_TS_TSONLY: 25253 off = IPOPT_TS_TIMELEN; 25254 break; 25255 case IPOPT_TS_PRESPEC: 25256 case IPOPT_TS_PRESPEC_RFC791: 25257 /* Verify that the address matched */ 25258 off = opt[IPOPT_OFFSET] - 1; 25259 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 25260 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 25261 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 25262 ipst); 25263 if (ire == NULL) { 25264 /* Not for us */ 25265 break; 25266 } 25267 ire_refrele(ire); 25268 /* FALLTHRU */ 25269 case IPOPT_TS_TSANDADDR: 25270 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 25271 break; 25272 default: 25273 /* 25274 * ip_*put_options should have already 25275 * dropped this packet. 25276 */ 25277 cmn_err(CE_PANIC, "ip_wput_local_options: " 25278 "unknown IT - bug in ip_wput_options?\n"); 25279 return; /* Keep "lint" happy */ 25280 } 25281 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 25282 /* Increase overflow counter */ 25283 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 25284 opt[IPOPT_POS_OV_FLG] = (uint8_t) 25285 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 25286 (off << 4); 25287 break; 25288 } 25289 off = opt[IPOPT_OFFSET] - 1; 25290 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25291 case IPOPT_TS_PRESPEC: 25292 case IPOPT_TS_PRESPEC_RFC791: 25293 case IPOPT_TS_TSANDADDR: 25294 dst = htonl(INADDR_LOOPBACK); 25295 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25296 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25297 /* FALLTHRU */ 25298 case IPOPT_TS_TSONLY: 25299 off = opt[IPOPT_OFFSET] - 1; 25300 /* Compute # of milliseconds since midnight */ 25301 gethrestime(&now); 25302 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 25303 now.tv_nsec / (NANOSEC / MILLISEC); 25304 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 25305 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 25306 break; 25307 } 25308 break; 25309 } 25310 } 25311 } 25312 25313 /* 25314 * Send out a multicast packet on interface ipif. 25315 * The sender does not have an conn. 25316 * Caller verifies that this isn't a PHYI_LOOPBACK. 25317 */ 25318 void 25319 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif, zoneid_t zoneid) 25320 { 25321 ipha_t *ipha; 25322 ire_t *ire; 25323 ipaddr_t dst; 25324 mblk_t *first_mp; 25325 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 25326 25327 /* igmp_sendpkt always allocates a ipsec_out_t */ 25328 ASSERT(mp->b_datap->db_type == M_CTL); 25329 ASSERT(!ipif->ipif_isv6); 25330 ASSERT(!IS_LOOPBACK(ipif->ipif_ill)); 25331 25332 first_mp = mp; 25333 mp = first_mp->b_cont; 25334 ASSERT(mp->b_datap->db_type == M_DATA); 25335 ipha = (ipha_t *)mp->b_rptr; 25336 25337 /* 25338 * Find an IRE which matches the destination and the outgoing 25339 * queue (i.e. the outgoing interface.) 25340 */ 25341 if (ipif->ipif_flags & IPIF_POINTOPOINT) 25342 dst = ipif->ipif_pp_dst_addr; 25343 else 25344 dst = ipha->ipha_dst; 25345 /* 25346 * The source address has already been initialized by the 25347 * caller and hence matching on ILL (MATCH_IRE_ILL) would 25348 * be sufficient rather than MATCH_IRE_IPIF. 25349 * 25350 * This function is used for sending IGMP packets. We need 25351 * to make sure that we send the packet out of the interface 25352 * (ipif->ipif_ill) where we joined the group. This is to 25353 * prevent from switches doing IGMP snooping to send us multicast 25354 * packets for a given group on the interface we have joined. 25355 * If we can't find an ire, igmp_sendpkt has already initialized 25356 * ipsec_out_attach_if so that this will not be load spread in 25357 * ip_newroute_ipif. 25358 */ 25359 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, NULL, 25360 MATCH_IRE_ILL, ipst); 25361 if (!ire) { 25362 /* 25363 * Mark this packet to make it be delivered to 25364 * ip_wput_ire after the new ire has been 25365 * created. 25366 */ 25367 mp->b_prev = NULL; 25368 mp->b_next = NULL; 25369 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC, 25370 zoneid, &zero_info); 25371 return; 25372 } 25373 25374 /* 25375 * Honor the RTF_SETSRC flag; this is the only case 25376 * where we force this addr whatever the current src addr is, 25377 * because this address is set by igmp_sendpkt(), and 25378 * cannot be specified by any user. 25379 */ 25380 if (ire->ire_flags & RTF_SETSRC) { 25381 ipha->ipha_src = ire->ire_src_addr; 25382 } 25383 25384 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE, zoneid); 25385 } 25386 25387 /* 25388 * NOTE : This function does not ire_refrele the ire argument passed in. 25389 * 25390 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 25391 * failure. The nce_fp_mp can vanish any time in the case of 25392 * IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 25393 * the ire_lock to access the nce_fp_mp in this case. 25394 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 25395 * prepending a fastpath message IPQoS processing must precede it, we also set 25396 * the b_band of the fastpath message to that of the mblk returned by IPQoS 25397 * (IPQoS might have set the b_band for CoS marking). 25398 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 25399 * must follow it so that IPQoS can mark the dl_priority field for CoS 25400 * marking, if needed. 25401 */ 25402 static mblk_t * 25403 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, uint32_t ill_index) 25404 { 25405 uint_t hlen; 25406 ipha_t *ipha; 25407 mblk_t *mp1; 25408 boolean_t qos_done = B_FALSE; 25409 uchar_t *ll_hdr; 25410 ip_stack_t *ipst = ire->ire_ipst; 25411 25412 #define rptr ((uchar_t *)ipha) 25413 25414 ipha = (ipha_t *)mp->b_rptr; 25415 hlen = 0; 25416 LOCK_IRE_FP_MP(ire); 25417 if ((mp1 = ire->ire_nce->nce_fp_mp) != NULL) { 25418 ASSERT(DB_TYPE(mp1) == M_DATA); 25419 /* Initiate IPPF processing */ 25420 if ((proc != 0) && IPP_ENABLED(proc, ipst)) { 25421 UNLOCK_IRE_FP_MP(ire); 25422 ip_process(proc, &mp, ill_index); 25423 if (mp == NULL) 25424 return (NULL); 25425 25426 ipha = (ipha_t *)mp->b_rptr; 25427 LOCK_IRE_FP_MP(ire); 25428 if ((mp1 = ire->ire_nce->nce_fp_mp) == NULL) { 25429 qos_done = B_TRUE; 25430 goto no_fp_mp; 25431 } 25432 ASSERT(DB_TYPE(mp1) == M_DATA); 25433 } 25434 hlen = MBLKL(mp1); 25435 /* 25436 * Check if we have enough room to prepend fastpath 25437 * header 25438 */ 25439 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 25440 ll_hdr = rptr - hlen; 25441 bcopy(mp1->b_rptr, ll_hdr, hlen); 25442 /* 25443 * Set the b_rptr to the start of the link layer 25444 * header 25445 */ 25446 mp->b_rptr = ll_hdr; 25447 mp1 = mp; 25448 } else { 25449 mp1 = copyb(mp1); 25450 if (mp1 == NULL) 25451 goto unlock_err; 25452 mp1->b_band = mp->b_band; 25453 mp1->b_cont = mp; 25454 /* 25455 * certain system generated traffic may not 25456 * have cred/label in ip header block. This 25457 * is true even for a labeled system. But for 25458 * labeled traffic, inherit the label in the 25459 * new header. 25460 */ 25461 if (DB_CRED(mp) != NULL) 25462 mblk_setcred(mp1, DB_CRED(mp)); 25463 /* 25464 * XXX disable ICK_VALID and compute checksum 25465 * here; can happen if nce_fp_mp changes and 25466 * it can't be copied now due to insufficient 25467 * space. (unlikely, fp mp can change, but it 25468 * does not increase in length) 25469 */ 25470 } 25471 UNLOCK_IRE_FP_MP(ire); 25472 } else { 25473 no_fp_mp: 25474 mp1 = copyb(ire->ire_nce->nce_res_mp); 25475 if (mp1 == NULL) { 25476 unlock_err: 25477 UNLOCK_IRE_FP_MP(ire); 25478 freemsg(mp); 25479 return (NULL); 25480 } 25481 UNLOCK_IRE_FP_MP(ire); 25482 mp1->b_cont = mp; 25483 /* 25484 * certain system generated traffic may not 25485 * have cred/label in ip header block. This 25486 * is true even for a labeled system. But for 25487 * labeled traffic, inherit the label in the 25488 * new header. 25489 */ 25490 if (DB_CRED(mp) != NULL) 25491 mblk_setcred(mp1, DB_CRED(mp)); 25492 if (!qos_done && (proc != 0) && IPP_ENABLED(proc, ipst)) { 25493 ip_process(proc, &mp1, ill_index); 25494 if (mp1 == NULL) 25495 return (NULL); 25496 } 25497 } 25498 return (mp1); 25499 #undef rptr 25500 } 25501 25502 /* 25503 * Finish the outbound IPsec processing for an IPv6 packet. This function 25504 * is called from ipsec_out_process() if the IPsec packet was processed 25505 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25506 * asynchronously. 25507 */ 25508 void 25509 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 25510 ire_t *ire_arg) 25511 { 25512 in6_addr_t *v6dstp; 25513 ire_t *ire; 25514 mblk_t *mp; 25515 ip6_t *ip6h1; 25516 uint_t ill_index; 25517 ipsec_out_t *io; 25518 boolean_t attach_if, hwaccel; 25519 uint32_t flags = IP6_NO_IPPOLICY; 25520 int match_flags; 25521 zoneid_t zoneid; 25522 boolean_t ill_need_rele = B_FALSE; 25523 boolean_t ire_need_rele = B_FALSE; 25524 ip_stack_t *ipst; 25525 25526 mp = ipsec_mp->b_cont; 25527 ip6h1 = (ip6_t *)mp->b_rptr; 25528 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25529 ASSERT(io->ipsec_out_ns != NULL); 25530 ipst = io->ipsec_out_ns->netstack_ip; 25531 ill_index = io->ipsec_out_ill_index; 25532 if (io->ipsec_out_reachable) { 25533 flags |= IPV6_REACHABILITY_CONFIRMATION; 25534 } 25535 attach_if = io->ipsec_out_attach_if; 25536 hwaccel = io->ipsec_out_accelerated; 25537 zoneid = io->ipsec_out_zoneid; 25538 ASSERT(zoneid != ALL_ZONES); 25539 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25540 /* Multicast addresses should have non-zero ill_index. */ 25541 v6dstp = &ip6h->ip6_dst; 25542 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 25543 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 25544 ASSERT(!attach_if || ill_index != 0); 25545 if (ill_index != 0) { 25546 if (ill == NULL) { 25547 ill = ip_grab_attach_ill(NULL, ipsec_mp, ill_index, 25548 B_TRUE, ipst); 25549 25550 /* Failure case frees things for us. */ 25551 if (ill == NULL) 25552 return; 25553 25554 ill_need_rele = B_TRUE; 25555 } 25556 /* 25557 * If this packet needs to go out on a particular interface 25558 * honor it. 25559 */ 25560 if (attach_if) { 25561 match_flags = MATCH_IRE_ILL; 25562 25563 /* 25564 * Check if we need an ire that will not be 25565 * looked up by anybody else i.e. HIDDEN. 25566 */ 25567 if (ill_is_probeonly(ill)) { 25568 match_flags |= MATCH_IRE_MARK_HIDDEN; 25569 } 25570 } 25571 } 25572 ASSERT(mp != NULL); 25573 25574 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 25575 boolean_t unspec_src; 25576 ipif_t *ipif; 25577 25578 /* 25579 * Use the ill_index to get the right ill. 25580 */ 25581 unspec_src = io->ipsec_out_unspec_src; 25582 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25583 if (ipif == NULL) { 25584 if (ill_need_rele) 25585 ill_refrele(ill); 25586 freemsg(ipsec_mp); 25587 return; 25588 } 25589 25590 if (ire_arg != NULL) { 25591 ire = ire_arg; 25592 } else { 25593 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25594 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25595 ire_need_rele = B_TRUE; 25596 } 25597 if (ire != NULL) { 25598 ipif_refrele(ipif); 25599 /* 25600 * XXX Do the multicast forwarding now, as the IPsec 25601 * processing has been done. 25602 */ 25603 goto send; 25604 } 25605 25606 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 25607 mp->b_prev = NULL; 25608 mp->b_next = NULL; 25609 25610 /* 25611 * If the IPsec packet was processed asynchronously, 25612 * drop it now. 25613 */ 25614 if (q == NULL) { 25615 if (ill_need_rele) 25616 ill_refrele(ill); 25617 freemsg(ipsec_mp); 25618 return; 25619 } 25620 25621 ip_newroute_ipif_v6(q, ipsec_mp, ipif, *v6dstp, 25622 unspec_src, zoneid); 25623 ipif_refrele(ipif); 25624 } else { 25625 if (attach_if) { 25626 ipif_t *ipif; 25627 25628 ipif = ipif_get_next_ipif(NULL, ill); 25629 if (ipif == NULL) { 25630 if (ill_need_rele) 25631 ill_refrele(ill); 25632 freemsg(ipsec_mp); 25633 return; 25634 } 25635 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25636 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25637 ire_need_rele = B_TRUE; 25638 ipif_refrele(ipif); 25639 } else { 25640 if (ire_arg != NULL) { 25641 ire = ire_arg; 25642 } else { 25643 ire = ire_cache_lookup_v6(v6dstp, zoneid, NULL, 25644 ipst); 25645 ire_need_rele = B_TRUE; 25646 } 25647 } 25648 if (ire != NULL) 25649 goto send; 25650 /* 25651 * ire disappeared underneath. 25652 * 25653 * What we need to do here is the ip_newroute 25654 * logic to get the ire without doing the IPsec 25655 * processing. Follow the same old path. But this 25656 * time, ip_wput or ire_add_then_send will call us 25657 * directly as all the IPsec operations are done. 25658 */ 25659 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 25660 mp->b_prev = NULL; 25661 mp->b_next = NULL; 25662 25663 /* 25664 * If the IPsec packet was processed asynchronously, 25665 * drop it now. 25666 */ 25667 if (q == NULL) { 25668 if (ill_need_rele) 25669 ill_refrele(ill); 25670 freemsg(ipsec_mp); 25671 return; 25672 } 25673 25674 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 25675 zoneid, ipst); 25676 } 25677 if (ill != NULL && ill_need_rele) 25678 ill_refrele(ill); 25679 return; 25680 send: 25681 if (ill != NULL && ill_need_rele) 25682 ill_refrele(ill); 25683 25684 /* Local delivery */ 25685 if (ire->ire_stq == NULL) { 25686 ill_t *out_ill; 25687 ASSERT(q != NULL); 25688 25689 /* PFHooks: LOOPBACK_OUT */ 25690 out_ill = ire_to_ill(ire); 25691 25692 DTRACE_PROBE4(ip6__loopback__out__start, 25693 ill_t *, NULL, ill_t *, out_ill, 25694 ip6_t *, ip6h1, mblk_t *, ipsec_mp); 25695 25696 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 25697 ipst->ips_ipv6firewall_loopback_out, 25698 NULL, out_ill, ip6h1, ipsec_mp, mp, 0, ipst); 25699 25700 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, ipsec_mp); 25701 25702 if (ipsec_mp != NULL) 25703 ip_wput_local_v6(RD(q), out_ill, 25704 ip6h, ipsec_mp, ire, 0); 25705 if (ire_need_rele) 25706 ire_refrele(ire); 25707 return; 25708 } 25709 /* 25710 * Everything is done. Send it out on the wire. 25711 * We force the insertion of a fragment header using the 25712 * IPH_FRAG_HDR flag in two cases: 25713 * - after reception of an ICMPv6 "packet too big" message 25714 * with a MTU < 1280 (cf. RFC 2460 section 5) 25715 * - for multirouted IPv6 packets, so that the receiver can 25716 * discard duplicates according to their fragment identifier 25717 */ 25718 /* XXX fix flow control problems. */ 25719 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 25720 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 25721 if (hwaccel) { 25722 /* 25723 * hardware acceleration does not handle these 25724 * "slow path" cases. 25725 */ 25726 /* IPsec KSTATS: should bump bean counter here. */ 25727 if (ire_need_rele) 25728 ire_refrele(ire); 25729 freemsg(ipsec_mp); 25730 return; 25731 } 25732 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 25733 (mp->b_cont ? msgdsize(mp) : 25734 mp->b_wptr - (uchar_t *)ip6h)) { 25735 /* IPsec KSTATS: should bump bean counter here. */ 25736 ip0dbg(("Packet length mismatch: %d, %ld\n", 25737 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 25738 msgdsize(mp))); 25739 if (ire_need_rele) 25740 ire_refrele(ire); 25741 freemsg(ipsec_mp); 25742 return; 25743 } 25744 ASSERT(mp->b_prev == NULL); 25745 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 25746 ntohs(ip6h->ip6_plen) + 25747 IPV6_HDR_LEN, ire->ire_max_frag)); 25748 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 25749 ire->ire_max_frag); 25750 } else { 25751 UPDATE_OB_PKT_COUNT(ire); 25752 ire->ire_last_used_time = lbolt; 25753 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 25754 } 25755 if (ire_need_rele) 25756 ire_refrele(ire); 25757 freeb(ipsec_mp); 25758 } 25759 25760 void 25761 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 25762 { 25763 mblk_t *hada_mp; /* attributes M_CTL mblk */ 25764 da_ipsec_t *hada; /* data attributes */ 25765 ill_t *ill = (ill_t *)q->q_ptr; 25766 25767 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 25768 25769 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 25770 /* IPsec KSTATS: Bump lose counter here! */ 25771 freemsg(mp); 25772 return; 25773 } 25774 25775 /* 25776 * It's an IPsec packet that must be 25777 * accelerated by the Provider, and the 25778 * outbound ill is IPsec acceleration capable. 25779 * Prepends the mblk with an IPHADA_M_CTL, and ship it 25780 * to the ill. 25781 * IPsec KSTATS: should bump packet counter here. 25782 */ 25783 25784 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 25785 if (hada_mp == NULL) { 25786 /* IPsec KSTATS: should bump packet counter here. */ 25787 freemsg(mp); 25788 return; 25789 } 25790 25791 hada_mp->b_datap->db_type = M_CTL; 25792 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 25793 hada_mp->b_cont = mp; 25794 25795 hada = (da_ipsec_t *)hada_mp->b_rptr; 25796 bzero(hada, sizeof (da_ipsec_t)); 25797 hada->da_type = IPHADA_M_CTL; 25798 25799 putnext(q, hada_mp); 25800 } 25801 25802 /* 25803 * Finish the outbound IPsec processing. This function is called from 25804 * ipsec_out_process() if the IPsec packet was processed 25805 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25806 * asynchronously. 25807 */ 25808 void 25809 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 25810 ire_t *ire_arg) 25811 { 25812 uint32_t v_hlen_tos_len; 25813 ipaddr_t dst; 25814 ipif_t *ipif = NULL; 25815 ire_t *ire; 25816 ire_t *ire1 = NULL; 25817 mblk_t *next_mp = NULL; 25818 uint32_t max_frag; 25819 boolean_t multirt_send = B_FALSE; 25820 mblk_t *mp; 25821 ipha_t *ipha1; 25822 uint_t ill_index; 25823 ipsec_out_t *io; 25824 boolean_t attach_if; 25825 int match_flags; 25826 irb_t *irb = NULL; 25827 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 25828 zoneid_t zoneid; 25829 ipxmit_state_t pktxmit_state; 25830 ip_stack_t *ipst; 25831 25832 #ifdef _BIG_ENDIAN 25833 #define LENGTH (v_hlen_tos_len & 0xFFFF) 25834 #else 25835 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 25836 #endif 25837 25838 mp = ipsec_mp->b_cont; 25839 ipha1 = (ipha_t *)mp->b_rptr; 25840 ASSERT(mp != NULL); 25841 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 25842 dst = ipha->ipha_dst; 25843 25844 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25845 ill_index = io->ipsec_out_ill_index; 25846 attach_if = io->ipsec_out_attach_if; 25847 zoneid = io->ipsec_out_zoneid; 25848 ASSERT(zoneid != ALL_ZONES); 25849 ipst = io->ipsec_out_ns->netstack_ip; 25850 ASSERT(io->ipsec_out_ns != NULL); 25851 25852 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25853 if (ill_index != 0) { 25854 if (ill == NULL) { 25855 ill = ip_grab_attach_ill(NULL, ipsec_mp, 25856 ill_index, B_FALSE, ipst); 25857 25858 /* Failure case frees things for us. */ 25859 if (ill == NULL) 25860 return; 25861 25862 ill_need_rele = B_TRUE; 25863 } 25864 /* 25865 * If this packet needs to go out on a particular interface 25866 * honor it. 25867 */ 25868 if (attach_if) { 25869 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 25870 25871 /* 25872 * Check if we need an ire that will not be 25873 * looked up by anybody else i.e. HIDDEN. 25874 */ 25875 if (ill_is_probeonly(ill)) { 25876 match_flags |= MATCH_IRE_MARK_HIDDEN; 25877 } 25878 } 25879 } 25880 25881 if (CLASSD(dst)) { 25882 boolean_t conn_dontroute; 25883 /* 25884 * Use the ill_index to get the right ipif. 25885 */ 25886 conn_dontroute = io->ipsec_out_dontroute; 25887 if (ill_index == 0) 25888 ipif = ipif_lookup_group(dst, zoneid, ipst); 25889 else 25890 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25891 if (ipif == NULL) { 25892 ip1dbg(("ip_wput_ipsec_out: No ipif for" 25893 " multicast\n")); 25894 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 25895 freemsg(ipsec_mp); 25896 goto done; 25897 } 25898 /* 25899 * ipha_src has already been intialized with the 25900 * value of the ipif in ip_wput. All we need now is 25901 * an ire to send this downstream. 25902 */ 25903 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 25904 MBLK_GETLABEL(mp), match_flags, ipst); 25905 if (ire != NULL) { 25906 ill_t *ill1; 25907 /* 25908 * Do the multicast forwarding now, as the IPsec 25909 * processing has been done. 25910 */ 25911 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 25912 (ill1 = ire_to_ill(ire))) { 25913 if (ip_mforward(ill1, ipha, mp)) { 25914 freemsg(ipsec_mp); 25915 ip1dbg(("ip_wput_ipsec_out: mforward " 25916 "failed\n")); 25917 ire_refrele(ire); 25918 goto done; 25919 } 25920 } 25921 goto send; 25922 } 25923 25924 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 25925 mp->b_prev = NULL; 25926 mp->b_next = NULL; 25927 25928 /* 25929 * If the IPsec packet was processed asynchronously, 25930 * drop it now. 25931 */ 25932 if (q == NULL) { 25933 freemsg(ipsec_mp); 25934 goto done; 25935 } 25936 25937 /* 25938 * We may be using a wrong ipif to create the ire. 25939 * But it is okay as the source address is assigned 25940 * for the packet already. Next outbound packet would 25941 * create the IRE with the right IPIF in ip_wput. 25942 * 25943 * Also handle RTF_MULTIRT routes. 25944 */ 25945 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT, 25946 zoneid, &zero_info); 25947 } else { 25948 if (attach_if) { 25949 ire = ire_ctable_lookup(dst, 0, 0, ill->ill_ipif, 25950 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25951 } else { 25952 if (ire_arg != NULL) { 25953 ire = ire_arg; 25954 ire_need_rele = B_FALSE; 25955 } else { 25956 ire = ire_cache_lookup(dst, zoneid, 25957 MBLK_GETLABEL(mp), ipst); 25958 } 25959 } 25960 if (ire != NULL) { 25961 goto send; 25962 } 25963 25964 /* 25965 * ire disappeared underneath. 25966 * 25967 * What we need to do here is the ip_newroute 25968 * logic to get the ire without doing the IPsec 25969 * processing. Follow the same old path. But this 25970 * time, ip_wput or ire_add_then_put will call us 25971 * directly as all the IPsec operations are done. 25972 */ 25973 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 25974 mp->b_prev = NULL; 25975 mp->b_next = NULL; 25976 25977 /* 25978 * If the IPsec packet was processed asynchronously, 25979 * drop it now. 25980 */ 25981 if (q == NULL) { 25982 freemsg(ipsec_mp); 25983 goto done; 25984 } 25985 25986 /* 25987 * Since we're going through ip_newroute() again, we 25988 * need to make sure we don't: 25989 * 25990 * 1.) Trigger the ASSERT() with the ipha_ident 25991 * overloading. 25992 * 2.) Redo transport-layer checksumming, since we've 25993 * already done all that to get this far. 25994 * 25995 * The easiest way not do either of the above is to set 25996 * the ipha_ident field to IP_HDR_INCLUDED. 25997 */ 25998 ipha->ipha_ident = IP_HDR_INCLUDED; 25999 ip_newroute(q, ipsec_mp, dst, (CONN_Q(q) ? Q_TO_CONN(q) : NULL), 26000 zoneid, ipst); 26001 } 26002 goto done; 26003 send: 26004 if (ire->ire_stq == NULL) { 26005 ill_t *out_ill; 26006 /* 26007 * Loopbacks go through ip_wput_local except for one case. 26008 * We come here if we generate a icmp_frag_needed message 26009 * after IPsec processing is over. When this function calls 26010 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 26011 * icmp_frag_needed. The message generated comes back here 26012 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 26013 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 26014 * source address as it is usually set in ip_wput_ire. As 26015 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 26016 * and we end up here. We can't enter ip_wput_ire once the 26017 * IPsec processing is over and hence we need to do it here. 26018 */ 26019 ASSERT(q != NULL); 26020 UPDATE_OB_PKT_COUNT(ire); 26021 ire->ire_last_used_time = lbolt; 26022 if (ipha->ipha_src == 0) 26023 ipha->ipha_src = ire->ire_src_addr; 26024 26025 /* PFHooks: LOOPBACK_OUT */ 26026 out_ill = ire_to_ill(ire); 26027 26028 DTRACE_PROBE4(ip4__loopback__out__start, 26029 ill_t *, NULL, ill_t *, out_ill, 26030 ipha_t *, ipha1, mblk_t *, ipsec_mp); 26031 26032 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 26033 ipst->ips_ipv4firewall_loopback_out, 26034 NULL, out_ill, ipha1, ipsec_mp, mp, 0, ipst); 26035 26036 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp); 26037 26038 if (ipsec_mp != NULL) 26039 ip_wput_local(RD(q), out_ill, 26040 ipha, ipsec_mp, ire, 0, zoneid); 26041 if (ire_need_rele) 26042 ire_refrele(ire); 26043 goto done; 26044 } 26045 26046 if (ire->ire_max_frag < (unsigned int)LENGTH) { 26047 /* 26048 * We are through with IPsec processing. 26049 * Fragment this and send it on the wire. 26050 */ 26051 if (io->ipsec_out_accelerated) { 26052 /* 26053 * The packet has been accelerated but must 26054 * be fragmented. This should not happen 26055 * since AH and ESP must not accelerate 26056 * packets that need fragmentation, however 26057 * the configuration could have changed 26058 * since the AH or ESP processing. 26059 * Drop packet. 26060 * IPsec KSTATS: bump bean counter here. 26061 */ 26062 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 26063 "fragmented accelerated packet!\n")); 26064 freemsg(ipsec_mp); 26065 } else { 26066 ip_wput_ire_fragmentit(ipsec_mp, ire, zoneid, ipst); 26067 } 26068 if (ire_need_rele) 26069 ire_refrele(ire); 26070 goto done; 26071 } 26072 26073 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 26074 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 26075 (void *)ire->ire_ipif, (void *)ipif)); 26076 26077 /* 26078 * Multiroute the secured packet, unless IPsec really 26079 * requires the packet to go out only through a particular 26080 * interface. 26081 */ 26082 if ((ire->ire_flags & RTF_MULTIRT) && !attach_if) { 26083 ire_t *first_ire; 26084 irb = ire->ire_bucket; 26085 ASSERT(irb != NULL); 26086 /* 26087 * This ire has been looked up as the one that 26088 * goes through the given ipif; 26089 * make sure we do not omit any other multiroute ire 26090 * that may be present in the bucket before this one. 26091 */ 26092 IRB_REFHOLD(irb); 26093 for (first_ire = irb->irb_ire; 26094 first_ire != NULL; 26095 first_ire = first_ire->ire_next) { 26096 if ((first_ire->ire_flags & RTF_MULTIRT) && 26097 (first_ire->ire_addr == ire->ire_addr) && 26098 !(first_ire->ire_marks & 26099 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) { 26100 break; 26101 } 26102 } 26103 26104 if ((first_ire != NULL) && (first_ire != ire)) { 26105 /* 26106 * Don't change the ire if the packet must 26107 * be fragmented if sent via this new one. 26108 */ 26109 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 26110 IRE_REFHOLD(first_ire); 26111 if (ire_need_rele) 26112 ire_refrele(ire); 26113 else 26114 ire_need_rele = B_TRUE; 26115 ire = first_ire; 26116 } 26117 } 26118 IRB_REFRELE(irb); 26119 26120 multirt_send = B_TRUE; 26121 max_frag = ire->ire_max_frag; 26122 } else { 26123 if ((ire->ire_flags & RTF_MULTIRT) && attach_if) { 26124 ip1dbg(("ip_wput_ipsec_out: ignoring multirouting " 26125 "flag, attach_if %d\n", attach_if)); 26126 } 26127 } 26128 26129 /* 26130 * In most cases, the emission loop below is entered only once. 26131 * Only in the case where the ire holds the RTF_MULTIRT 26132 * flag, we loop to process all RTF_MULTIRT ires in the 26133 * bucket, and send the packet through all crossed 26134 * RTF_MULTIRT routes. 26135 */ 26136 do { 26137 if (multirt_send) { 26138 /* 26139 * ire1 holds here the next ire to process in the 26140 * bucket. If multirouting is expected, 26141 * any non-RTF_MULTIRT ire that has the 26142 * right destination address is ignored. 26143 */ 26144 ASSERT(irb != NULL); 26145 IRB_REFHOLD(irb); 26146 for (ire1 = ire->ire_next; 26147 ire1 != NULL; 26148 ire1 = ire1->ire_next) { 26149 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 26150 continue; 26151 if (ire1->ire_addr != ire->ire_addr) 26152 continue; 26153 if (ire1->ire_marks & 26154 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 26155 continue; 26156 /* No loopback here */ 26157 if (ire1->ire_stq == NULL) 26158 continue; 26159 /* 26160 * Ensure we do not exceed the MTU 26161 * of the next route. 26162 */ 26163 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 26164 ip_multirt_bad_mtu(ire1, max_frag); 26165 continue; 26166 } 26167 26168 IRE_REFHOLD(ire1); 26169 break; 26170 } 26171 IRB_REFRELE(irb); 26172 if (ire1 != NULL) { 26173 /* 26174 * We are in a multiple send case, need to 26175 * make a copy of the packet. 26176 */ 26177 next_mp = copymsg(ipsec_mp); 26178 if (next_mp == NULL) { 26179 ire_refrele(ire1); 26180 ire1 = NULL; 26181 } 26182 } 26183 } 26184 /* 26185 * Everything is done. Send it out on the wire 26186 * 26187 * ip_xmit_v4 will call ip_wput_attach_llhdr and then 26188 * either send it on the wire or, in the case of 26189 * HW acceleration, call ipsec_hw_putnext. 26190 */ 26191 if (ire->ire_nce && 26192 ire->ire_nce->nce_state != ND_REACHABLE) { 26193 DTRACE_PROBE2(ip__wput__ipsec__bail, 26194 (ire_t *), ire, (mblk_t *), ipsec_mp); 26195 /* 26196 * If ire's link-layer is unresolved (this 26197 * would only happen if the incomplete ire 26198 * was added to cachetable via forwarding path) 26199 * don't bother going to ip_xmit_v4. Just drop the 26200 * packet. 26201 * There is a slight risk here, in that, if we 26202 * have the forwarding path create an incomplete 26203 * IRE, then until the IRE is completed, any 26204 * transmitted IPsec packets will be dropped 26205 * instead of being queued waiting for resolution. 26206 * 26207 * But the likelihood of a forwarding packet and a wput 26208 * packet sending to the same dst at the same time 26209 * and there not yet be an ARP entry for it is small. 26210 * Furthermore, if this actually happens, it might 26211 * be likely that wput would generate multiple 26212 * packets (and forwarding would also have a train 26213 * of packets) for that destination. If this is 26214 * the case, some of them would have been dropped 26215 * anyway, since ARP only queues a few packets while 26216 * waiting for resolution 26217 * 26218 * NOTE: We should really call ip_xmit_v4, 26219 * and let it queue the packet and send the 26220 * ARP query and have ARP come back thus: 26221 * <ARP> ip_wput->ip_output->ip-wput_nondata-> 26222 * ip_xmit_v4->ip_wput_attach_llhdr + ipsec 26223 * hw accel work. But it's too complex to get 26224 * the IPsec hw acceleration approach to fit 26225 * well with ip_xmit_v4 doing ARP without 26226 * doing IPsec simplification. For now, we just 26227 * poke ip_xmit_v4 to trigger the arp resolve, so 26228 * that we can continue with the send on the next 26229 * attempt. 26230 * 26231 * XXX THis should be revisited, when 26232 * the IPsec/IP interaction is cleaned up 26233 */ 26234 ip1dbg(("ip_wput_ipsec_out: ire is incomplete" 26235 " - dropping packet\n")); 26236 freemsg(ipsec_mp); 26237 /* 26238 * Call ip_xmit_v4() to trigger ARP query 26239 * in case the nce_state is ND_INITIAL 26240 */ 26241 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 26242 goto drop_pkt; 26243 } 26244 26245 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 26246 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha1, 26247 mblk_t *, ipsec_mp); 26248 FW_HOOKS(ipst->ips_ip4_physical_out_event, 26249 ipst->ips_ipv4firewall_physical_out, NULL, 26250 ire->ire_ipif->ipif_ill, ipha1, ipsec_mp, mp, 0, ipst); 26251 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, ipsec_mp); 26252 if (ipsec_mp == NULL) 26253 goto drop_pkt; 26254 26255 ip1dbg(("ip_wput_ipsec_out: calling ip_xmit_v4\n")); 26256 pktxmit_state = ip_xmit_v4(mp, ire, 26257 (io->ipsec_out_accelerated ? io : NULL), B_FALSE); 26258 26259 if ((pktxmit_state == SEND_FAILED) || 26260 (pktxmit_state == LLHDR_RESLV_FAILED)) { 26261 26262 freeb(ipsec_mp); /* ip_xmit_v4 frees the mp */ 26263 drop_pkt: 26264 BUMP_MIB(((ill_t *)ire->ire_stq->q_ptr)->ill_ip_mib, 26265 ipIfStatsOutDiscards); 26266 if (ire_need_rele) 26267 ire_refrele(ire); 26268 if (ire1 != NULL) { 26269 ire_refrele(ire1); 26270 freemsg(next_mp); 26271 } 26272 goto done; 26273 } 26274 26275 freeb(ipsec_mp); 26276 if (ire_need_rele) 26277 ire_refrele(ire); 26278 26279 if (ire1 != NULL) { 26280 ire = ire1; 26281 ire_need_rele = B_TRUE; 26282 ASSERT(next_mp); 26283 ipsec_mp = next_mp; 26284 mp = ipsec_mp->b_cont; 26285 ire1 = NULL; 26286 next_mp = NULL; 26287 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26288 } else { 26289 multirt_send = B_FALSE; 26290 } 26291 } while (multirt_send); 26292 done: 26293 if (ill != NULL && ill_need_rele) 26294 ill_refrele(ill); 26295 if (ipif != NULL) 26296 ipif_refrele(ipif); 26297 } 26298 26299 /* 26300 * Get the ill corresponding to the specified ire, and compare its 26301 * capabilities with the protocol and algorithms specified by the 26302 * the SA obtained from ipsec_out. If they match, annotate the 26303 * ipsec_out structure to indicate that the packet needs acceleration. 26304 * 26305 * 26306 * A packet is eligible for outbound hardware acceleration if the 26307 * following conditions are satisfied: 26308 * 26309 * 1. the packet will not be fragmented 26310 * 2. the provider supports the algorithm 26311 * 3. there is no pending control message being exchanged 26312 * 4. snoop is not attached 26313 * 5. the destination address is not a broadcast or multicast address. 26314 * 26315 * Rationale: 26316 * - Hardware drivers do not support fragmentation with 26317 * the current interface. 26318 * - snoop, multicast, and broadcast may result in exposure of 26319 * a cleartext datagram. 26320 * We check all five of these conditions here. 26321 * 26322 * XXX would like to nuke "ire_t *" parameter here; problem is that 26323 * IRE is only way to figure out if a v4 address is a broadcast and 26324 * thus ineligible for acceleration... 26325 */ 26326 static void 26327 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 26328 { 26329 ipsec_out_t *io; 26330 mblk_t *data_mp; 26331 uint_t plen, overhead; 26332 ip_stack_t *ipst; 26333 26334 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 26335 return; 26336 26337 if (ill == NULL) 26338 return; 26339 ipst = ill->ill_ipst; 26340 /* 26341 * Destination address is a broadcast or multicast. Punt. 26342 */ 26343 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 26344 IRE_LOCAL))) 26345 return; 26346 26347 data_mp = ipsec_mp->b_cont; 26348 26349 if (ill->ill_isv6) { 26350 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 26351 26352 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 26353 return; 26354 26355 plen = ip6h->ip6_plen; 26356 } else { 26357 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 26358 26359 if (CLASSD(ipha->ipha_dst)) 26360 return; 26361 26362 plen = ipha->ipha_length; 26363 } 26364 /* 26365 * Is there a pending DLPI control message being exchanged 26366 * between IP/IPsec and the DLS Provider? If there is, it 26367 * could be a SADB update, and the state of the DLS Provider 26368 * SADB might not be in sync with the SADB maintained by 26369 * IPsec. To avoid dropping packets or using the wrong keying 26370 * material, we do not accelerate this packet. 26371 */ 26372 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 26373 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26374 "ill_dlpi_pending! don't accelerate packet\n")); 26375 return; 26376 } 26377 26378 /* 26379 * Is the Provider in promiscous mode? If it does, we don't 26380 * accelerate the packet since it will bounce back up to the 26381 * listeners in the clear. 26382 */ 26383 if (ill->ill_promisc_on_phys) { 26384 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26385 "ill in promiscous mode, don't accelerate packet\n")); 26386 return; 26387 } 26388 26389 /* 26390 * Will the packet require fragmentation? 26391 */ 26392 26393 /* 26394 * IPsec ESP note: this is a pessimistic estimate, but the same 26395 * as is used elsewhere. 26396 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 26397 * + 2-byte trailer 26398 */ 26399 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 26400 IPSEC_BASE_ESP_HDR_SIZE(sa); 26401 26402 if ((plen + overhead) > ill->ill_max_mtu) 26403 return; 26404 26405 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26406 26407 /* 26408 * Can the ill accelerate this IPsec protocol and algorithm 26409 * specified by the SA? 26410 */ 26411 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 26412 ill->ill_isv6, sa, ipst->ips_netstack)) { 26413 return; 26414 } 26415 26416 /* 26417 * Tell AH or ESP that the outbound ill is capable of 26418 * accelerating this packet. 26419 */ 26420 io->ipsec_out_is_capab_ill = B_TRUE; 26421 } 26422 26423 /* 26424 * Select which AH & ESP SA's to use (if any) for the outbound packet. 26425 * 26426 * If this function returns B_TRUE, the requested SA's have been filled 26427 * into the ipsec_out_*_sa pointers. 26428 * 26429 * If the function returns B_FALSE, the packet has been "consumed", most 26430 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 26431 * 26432 * The SA references created by the protocol-specific "select" 26433 * function will be released when the ipsec_mp is freed, thanks to the 26434 * ipsec_out_free destructor -- see spd.c. 26435 */ 26436 static boolean_t 26437 ipsec_out_select_sa(mblk_t *ipsec_mp) 26438 { 26439 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 26440 ipsec_out_t *io; 26441 ipsec_policy_t *pp; 26442 ipsec_action_t *ap; 26443 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26444 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26445 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26446 26447 if (!io->ipsec_out_secure) { 26448 /* 26449 * We came here by mistake. 26450 * Don't bother with ipsec processing 26451 * We should "discourage" this path in the future. 26452 */ 26453 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26454 return (B_FALSE); 26455 } 26456 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26457 ASSERT((io->ipsec_out_policy != NULL) || 26458 (io->ipsec_out_act != NULL)); 26459 26460 ASSERT(io->ipsec_out_failed == B_FALSE); 26461 26462 /* 26463 * IPsec processing has started. 26464 */ 26465 io->ipsec_out_proc_begin = B_TRUE; 26466 ap = io->ipsec_out_act; 26467 if (ap == NULL) { 26468 pp = io->ipsec_out_policy; 26469 ASSERT(pp != NULL); 26470 ap = pp->ipsp_act; 26471 ASSERT(ap != NULL); 26472 } 26473 26474 /* 26475 * We have an action. now, let's select SA's. 26476 * (In the future, we can cache this in the conn_t..) 26477 */ 26478 if (ap->ipa_want_esp) { 26479 if (io->ipsec_out_esp_sa == NULL) { 26480 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 26481 IPPROTO_ESP); 26482 } 26483 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 26484 } 26485 26486 if (ap->ipa_want_ah) { 26487 if (io->ipsec_out_ah_sa == NULL) { 26488 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 26489 IPPROTO_AH); 26490 } 26491 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 26492 /* 26493 * The ESP and AH processing order needs to be preserved 26494 * when both protocols are required (ESP should be applied 26495 * before AH for an outbound packet). Force an ESP ACQUIRE 26496 * when both ESP and AH are required, and an AH ACQUIRE 26497 * is needed. 26498 */ 26499 if (ap->ipa_want_esp && need_ah_acquire) 26500 need_esp_acquire = B_TRUE; 26501 } 26502 26503 /* 26504 * Send an ACQUIRE (extended, regular, or both) if we need one. 26505 * Release SAs that got referenced, but will not be used until we 26506 * acquire _all_ of the SAs we need. 26507 */ 26508 if (need_ah_acquire || need_esp_acquire) { 26509 if (io->ipsec_out_ah_sa != NULL) { 26510 IPSA_REFRELE(io->ipsec_out_ah_sa); 26511 io->ipsec_out_ah_sa = NULL; 26512 } 26513 if (io->ipsec_out_esp_sa != NULL) { 26514 IPSA_REFRELE(io->ipsec_out_esp_sa); 26515 io->ipsec_out_esp_sa = NULL; 26516 } 26517 26518 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 26519 return (B_FALSE); 26520 } 26521 26522 return (B_TRUE); 26523 } 26524 26525 /* 26526 * Process an IPSEC_OUT message and see what you can 26527 * do with it. 26528 * IPQoS Notes: 26529 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 26530 * IPsec. 26531 * XXX would like to nuke ire_t. 26532 * XXX ill_index better be "real" 26533 */ 26534 void 26535 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 26536 { 26537 ipsec_out_t *io; 26538 ipsec_policy_t *pp; 26539 ipsec_action_t *ap; 26540 ipha_t *ipha; 26541 ip6_t *ip6h; 26542 mblk_t *mp; 26543 ill_t *ill; 26544 zoneid_t zoneid; 26545 ipsec_status_t ipsec_rc; 26546 boolean_t ill_need_rele = B_FALSE; 26547 ip_stack_t *ipst; 26548 ipsec_stack_t *ipss; 26549 26550 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26551 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26552 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26553 ipst = io->ipsec_out_ns->netstack_ip; 26554 mp = ipsec_mp->b_cont; 26555 26556 /* 26557 * Initiate IPPF processing. We do it here to account for packets 26558 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 26559 * We can check for ipsec_out_proc_begin even for such packets, as 26560 * they will always be false (asserted below). 26561 */ 26562 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst) && !io->ipsec_out_proc_begin) { 26563 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 26564 io->ipsec_out_ill_index : ill_index); 26565 if (mp == NULL) { 26566 ip2dbg(("ipsec_out_process: packet dropped "\ 26567 "during IPPF processing\n")); 26568 freeb(ipsec_mp); 26569 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26570 return; 26571 } 26572 } 26573 26574 if (!io->ipsec_out_secure) { 26575 /* 26576 * We came here by mistake. 26577 * Don't bother with ipsec processing 26578 * Should "discourage" this path in the future. 26579 */ 26580 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26581 goto done; 26582 } 26583 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26584 ASSERT((io->ipsec_out_policy != NULL) || 26585 (io->ipsec_out_act != NULL)); 26586 ASSERT(io->ipsec_out_failed == B_FALSE); 26587 26588 ipss = ipst->ips_netstack->netstack_ipsec; 26589 if (!ipsec_loaded(ipss)) { 26590 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 26591 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26592 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26593 } else { 26594 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); 26595 } 26596 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 26597 DROPPER(ipss, ipds_ip_ipsec_not_loaded), 26598 &ipss->ipsec_dropper); 26599 return; 26600 } 26601 26602 /* 26603 * IPsec processing has started. 26604 */ 26605 io->ipsec_out_proc_begin = B_TRUE; 26606 ap = io->ipsec_out_act; 26607 if (ap == NULL) { 26608 pp = io->ipsec_out_policy; 26609 ASSERT(pp != NULL); 26610 ap = pp->ipsp_act; 26611 ASSERT(ap != NULL); 26612 } 26613 26614 /* 26615 * Save the outbound ill index. When the packet comes back 26616 * from IPsec, we make sure the ill hasn't changed or disappeared 26617 * before sending it the accelerated packet. 26618 */ 26619 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 26620 int ifindex; 26621 ill = ire_to_ill(ire); 26622 ifindex = ill->ill_phyint->phyint_ifindex; 26623 io->ipsec_out_capab_ill_index = ifindex; 26624 } 26625 26626 /* 26627 * The order of processing is first insert a IP header if needed. 26628 * Then insert the ESP header and then the AH header. 26629 */ 26630 if ((io->ipsec_out_se_done == B_FALSE) && 26631 (ap->ipa_want_se)) { 26632 /* 26633 * First get the outer IP header before sending 26634 * it to ESP. 26635 */ 26636 ipha_t *oipha, *iipha; 26637 mblk_t *outer_mp, *inner_mp; 26638 26639 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 26640 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 26641 "ipsec_out_process: " 26642 "Self-Encapsulation failed: Out of memory\n"); 26643 freemsg(ipsec_mp); 26644 if (ill != NULL) { 26645 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26646 } else { 26647 BUMP_MIB(&ipst->ips_ip_mib, 26648 ipIfStatsOutDiscards); 26649 } 26650 return; 26651 } 26652 inner_mp = ipsec_mp->b_cont; 26653 ASSERT(inner_mp->b_datap->db_type == M_DATA); 26654 oipha = (ipha_t *)outer_mp->b_rptr; 26655 iipha = (ipha_t *)inner_mp->b_rptr; 26656 *oipha = *iipha; 26657 outer_mp->b_wptr += sizeof (ipha_t); 26658 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 26659 sizeof (ipha_t)); 26660 oipha->ipha_protocol = IPPROTO_ENCAP; 26661 oipha->ipha_version_and_hdr_length = 26662 IP_SIMPLE_HDR_VERSION; 26663 oipha->ipha_hdr_checksum = 0; 26664 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 26665 outer_mp->b_cont = inner_mp; 26666 ipsec_mp->b_cont = outer_mp; 26667 26668 io->ipsec_out_se_done = B_TRUE; 26669 io->ipsec_out_tunnel = B_TRUE; 26670 } 26671 26672 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 26673 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 26674 !ipsec_out_select_sa(ipsec_mp)) 26675 return; 26676 26677 /* 26678 * By now, we know what SA's to use. Toss over to ESP & AH 26679 * to do the heavy lifting. 26680 */ 26681 zoneid = io->ipsec_out_zoneid; 26682 ASSERT(zoneid != ALL_ZONES); 26683 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 26684 ASSERT(io->ipsec_out_esp_sa != NULL); 26685 io->ipsec_out_esp_done = B_TRUE; 26686 /* 26687 * Note that since hw accel can only apply one transform, 26688 * not two, we skip hw accel for ESP if we also have AH 26689 * This is an design limitation of the interface 26690 * which should be revisited. 26691 */ 26692 ASSERT(ire != NULL); 26693 if (io->ipsec_out_ah_sa == NULL) { 26694 ill = (ill_t *)ire->ire_stq->q_ptr; 26695 ipsec_out_is_accelerated(ipsec_mp, 26696 io->ipsec_out_esp_sa, ill, ire); 26697 } 26698 26699 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 26700 switch (ipsec_rc) { 26701 case IPSEC_STATUS_SUCCESS: 26702 break; 26703 case IPSEC_STATUS_FAILED: 26704 if (ill != NULL) { 26705 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26706 } else { 26707 BUMP_MIB(&ipst->ips_ip_mib, 26708 ipIfStatsOutDiscards); 26709 } 26710 /* FALLTHRU */ 26711 case IPSEC_STATUS_PENDING: 26712 return; 26713 } 26714 } 26715 26716 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 26717 ASSERT(io->ipsec_out_ah_sa != NULL); 26718 io->ipsec_out_ah_done = B_TRUE; 26719 if (ire == NULL) { 26720 int idx = io->ipsec_out_capab_ill_index; 26721 ill = ill_lookup_on_ifindex(idx, B_FALSE, 26722 NULL, NULL, NULL, NULL, ipst); 26723 ill_need_rele = B_TRUE; 26724 } else { 26725 ill = (ill_t *)ire->ire_stq->q_ptr; 26726 } 26727 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 26728 ire); 26729 26730 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 26731 switch (ipsec_rc) { 26732 case IPSEC_STATUS_SUCCESS: 26733 break; 26734 case IPSEC_STATUS_FAILED: 26735 if (ill != NULL) { 26736 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26737 } else { 26738 BUMP_MIB(&ipst->ips_ip_mib, 26739 ipIfStatsOutDiscards); 26740 } 26741 /* FALLTHRU */ 26742 case IPSEC_STATUS_PENDING: 26743 if (ill != NULL && ill_need_rele) 26744 ill_refrele(ill); 26745 return; 26746 } 26747 } 26748 /* 26749 * We are done with IPsec processing. Send it over 26750 * the wire. 26751 */ 26752 done: 26753 mp = ipsec_mp->b_cont; 26754 ipha = (ipha_t *)mp->b_rptr; 26755 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26756 ip_wput_ipsec_out(q, ipsec_mp, ipha, ill, ire); 26757 } else { 26758 ip6h = (ip6_t *)ipha; 26759 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ill, ire); 26760 } 26761 if (ill != NULL && ill_need_rele) 26762 ill_refrele(ill); 26763 } 26764 26765 /* ARGSUSED */ 26766 void 26767 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 26768 { 26769 opt_restart_t *or; 26770 int err; 26771 conn_t *connp; 26772 26773 ASSERT(CONN_Q(q)); 26774 connp = Q_TO_CONN(q); 26775 26776 ASSERT(first_mp->b_datap->db_type == M_CTL); 26777 or = (opt_restart_t *)first_mp->b_rptr; 26778 /* 26779 * We don't need to pass any credentials here since this is just 26780 * a restart. The credentials are passed in when svr4_optcom_req 26781 * is called the first time (from ip_wput_nondata). 26782 */ 26783 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 26784 err = svr4_optcom_req(q, first_mp, NULL, 26785 &ip_opt_obj, B_FALSE); 26786 } else { 26787 ASSERT(or->or_type == T_OPTMGMT_REQ); 26788 err = tpi_optcom_req(q, first_mp, NULL, 26789 &ip_opt_obj, B_FALSE); 26790 } 26791 if (err != EINPROGRESS) { 26792 /* operation is done */ 26793 CONN_OPER_PENDING_DONE(connp); 26794 } 26795 } 26796 26797 /* 26798 * ioctls that go through a down/up sequence may need to wait for the down 26799 * to complete. This involves waiting for the ire and ipif refcnts to go down 26800 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 26801 */ 26802 /* ARGSUSED */ 26803 void 26804 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 26805 { 26806 struct iocblk *iocp; 26807 mblk_t *mp1; 26808 ip_ioctl_cmd_t *ipip; 26809 int err; 26810 sin_t *sin; 26811 struct lifreq *lifr; 26812 struct ifreq *ifr; 26813 26814 iocp = (struct iocblk *)mp->b_rptr; 26815 ASSERT(ipsq != NULL); 26816 /* Existence of mp1 verified in ip_wput_nondata */ 26817 mp1 = mp->b_cont->b_cont; 26818 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26819 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 26820 /* 26821 * Special case where ipsq_current_ipif is not set: 26822 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 26823 * ill could also have become part of a ipmp group in the 26824 * process, we are here as were not able to complete the 26825 * operation in ipif_set_values because we could not become 26826 * exclusive on the new ipsq, In such a case ipsq_current_ipif 26827 * will not be set so we need to set it. 26828 */ 26829 ill_t *ill = q->q_ptr; 26830 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd); 26831 } 26832 ASSERT(ipsq->ipsq_current_ipif != NULL); 26833 26834 if (ipip->ipi_cmd_type == IF_CMD) { 26835 /* This a old style SIOC[GS]IF* command */ 26836 ifr = (struct ifreq *)mp1->b_rptr; 26837 sin = (sin_t *)&ifr->ifr_addr; 26838 } else if (ipip->ipi_cmd_type == LIF_CMD) { 26839 /* This a new style SIOC[GS]LIF* command */ 26840 lifr = (struct lifreq *)mp1->b_rptr; 26841 sin = (sin_t *)&lifr->lifr_addr; 26842 } else { 26843 sin = NULL; 26844 } 26845 26846 err = (*ipip->ipi_func_restart)(ipsq->ipsq_current_ipif, sin, q, mp, 26847 ipip, mp1->b_rptr); 26848 26849 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 26850 } 26851 26852 /* 26853 * ioctl processing 26854 * 26855 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up 26856 * the ioctl command in the ioctl tables, determines the copyin data size 26857 * from the ipi_copyin_size field, and does an mi_copyin() of that size. 26858 * 26859 * ioctl processing then continues when the M_IOCDATA makes its way down to 26860 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its 26861 * associated 'conn' is refheld till the end of the ioctl and the general 26862 * ioctl processing function ip_process_ioctl() is called to extract the 26863 * arguments and process the ioctl. To simplify extraction, ioctl commands 26864 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a 26865 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq()) 26866 * is used to extract the ioctl's arguments. 26867 * 26868 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 26869 * so goes thru the serialization primitive ipsq_try_enter. Then the 26870 * appropriate function to handle the ioctl is called based on the entry in 26871 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 26872 * which also refreleases the 'conn' that was refheld at the start of the 26873 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 26874 * 26875 * Many exclusive ioctls go thru an internal down up sequence as part of 26876 * the operation. For example an attempt to change the IP address of an 26877 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 26878 * does all the cleanup such as deleting all ires that use this address. 26879 * Then we need to wait till all references to the interface go away. 26880 */ 26881 void 26882 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 26883 { 26884 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 26885 ip_ioctl_cmd_t *ipip = arg; 26886 ip_extract_func_t *extract_funcp; 26887 cmd_info_t ci; 26888 int err; 26889 boolean_t entered_ipsq = B_FALSE; 26890 26891 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 26892 26893 if (ipip == NULL) 26894 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26895 26896 /* 26897 * SIOCLIFADDIF needs to go thru a special path since the 26898 * ill may not exist yet. This happens in the case of lo0 26899 * which is created using this ioctl. 26900 */ 26901 if (ipip->ipi_cmd == SIOCLIFADDIF) { 26902 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 26903 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26904 return; 26905 } 26906 26907 ci.ci_ipif = NULL; 26908 if (ipip->ipi_cmd_type == MISC_CMD) { 26909 /* 26910 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF. 26911 */ 26912 if (ipip->ipi_cmd == IF_UNITSEL) { 26913 /* ioctl comes down the ill */ 26914 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 26915 ipif_refhold(ci.ci_ipif); 26916 } 26917 err = 0; 26918 ci.ci_sin = NULL; 26919 ci.ci_sin6 = NULL; 26920 ci.ci_lifr = NULL; 26921 } else { 26922 switch (ipip->ipi_cmd_type) { 26923 case IF_CMD: 26924 case LIF_CMD: 26925 extract_funcp = ip_extract_lifreq; 26926 break; 26927 26928 case ARP_CMD: 26929 case XARP_CMD: 26930 extract_funcp = ip_extract_arpreq; 26931 break; 26932 26933 case TUN_CMD: 26934 extract_funcp = ip_extract_tunreq; 26935 break; 26936 26937 case MSFILT_CMD: 26938 extract_funcp = ip_extract_msfilter; 26939 break; 26940 26941 default: 26942 ASSERT(0); 26943 } 26944 26945 err = (*extract_funcp)(q, mp, ipip, &ci, ip_process_ioctl); 26946 if (err != 0) { 26947 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26948 return; 26949 } 26950 26951 /* 26952 * All of the extraction functions return a refheld ipif. 26953 */ 26954 ASSERT(ci.ci_ipif != NULL); 26955 } 26956 26957 /* 26958 * If ipsq is non-null, we are already being called exclusively 26959 */ 26960 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 26961 if (!(ipip->ipi_flags & IPI_WR)) { 26962 /* 26963 * A return value of EINPROGRESS means the ioctl is 26964 * either queued and waiting for some reason or has 26965 * already completed. 26966 */ 26967 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 26968 ci.ci_lifr); 26969 if (ci.ci_ipif != NULL) 26970 ipif_refrele(ci.ci_ipif); 26971 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26972 return; 26973 } 26974 26975 ASSERT(ci.ci_ipif != NULL); 26976 26977 if (ipsq == NULL) { 26978 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, 26979 ip_process_ioctl, NEW_OP, B_TRUE); 26980 entered_ipsq = B_TRUE; 26981 } 26982 /* 26983 * Release the ipif so that ipif_down and friends that wait for 26984 * references to go away are not misled about the current ipif_refcnt 26985 * values. We are writer so we can access the ipif even after releasing 26986 * the ipif. 26987 */ 26988 ipif_refrele(ci.ci_ipif); 26989 if (ipsq == NULL) 26990 return; 26991 26992 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd); 26993 26994 /* 26995 * For most set ioctls that come here, this serves as a single point 26996 * where we set the IPIF_CHANGING flag. This ensures that there won't 26997 * be any new references to the ipif. This helps functions that go 26998 * through this path and end up trying to wait for the refcnts 26999 * associated with the ipif to go down to zero. Some exceptions are 27000 * Failover, Failback, and Groupname commands that operate on more than 27001 * just the ci.ci_ipif. These commands internally determine the 27002 * set of ipif's they operate on and set and clear the IPIF_CHANGING 27003 * flags on that set. Another exception is the Removeif command that 27004 * sets the IPIF_CONDEMNED flag internally after identifying the right 27005 * ipif to operate on. 27006 */ 27007 mutex_enter(&(ci.ci_ipif)->ipif_ill->ill_lock); 27008 if (ipip->ipi_cmd != SIOCLIFREMOVEIF && 27009 ipip->ipi_cmd != SIOCLIFFAILOVER && 27010 ipip->ipi_cmd != SIOCLIFFAILBACK && 27011 ipip->ipi_cmd != SIOCSLIFGROUPNAME) 27012 (ci.ci_ipif)->ipif_state_flags |= IPIF_CHANGING; 27013 mutex_exit(&(ci.ci_ipif)->ipif_ill->ill_lock); 27014 27015 /* 27016 * A return value of EINPROGRESS means the ioctl is 27017 * either queued and waiting for some reason or has 27018 * already completed. 27019 */ 27020 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr); 27021 27022 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 27023 27024 if (entered_ipsq) 27025 ipsq_exit(ipsq, B_TRUE, B_TRUE); 27026 } 27027 27028 /* 27029 * Complete the ioctl. Typically ioctls use the mi package and need to 27030 * do mi_copyout/mi_copy_done. 27031 */ 27032 void 27033 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq) 27034 { 27035 conn_t *connp = NULL; 27036 27037 if (err == EINPROGRESS) 27038 return; 27039 27040 if (CONN_Q(q)) { 27041 connp = Q_TO_CONN(q); 27042 ASSERT(connp->conn_ref >= 2); 27043 } 27044 27045 switch (mode) { 27046 case COPYOUT: 27047 if (err == 0) 27048 mi_copyout(q, mp); 27049 else 27050 mi_copy_done(q, mp, err); 27051 break; 27052 27053 case NO_COPYOUT: 27054 mi_copy_done(q, mp, err); 27055 break; 27056 27057 default: 27058 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */ 27059 break; 27060 } 27061 27062 /* 27063 * The refhold placed at the start of the ioctl is released here. 27064 */ 27065 if (connp != NULL) 27066 CONN_OPER_PENDING_DONE(connp); 27067 27068 if (ipsq != NULL) 27069 ipsq_current_finish(ipsq); 27070 } 27071 27072 /* 27073 * This is called from ip_wput_nondata to resume a deferred TCP bind. 27074 */ 27075 /* ARGSUSED */ 27076 void 27077 ip_resume_tcp_bind(void *arg, mblk_t *mp, void *arg2) 27078 { 27079 conn_t *connp = arg; 27080 tcp_t *tcp; 27081 27082 ASSERT(connp != NULL && IPCL_IS_TCP(connp) && connp->conn_tcp != NULL); 27083 tcp = connp->conn_tcp; 27084 27085 if (connp->conn_tcp->tcp_state == TCPS_CLOSED) 27086 freemsg(mp); 27087 else 27088 tcp_rput_other(tcp, mp); 27089 CONN_OPER_PENDING_DONE(connp); 27090 } 27091 27092 /* Called from ip_wput for all non data messages */ 27093 /* ARGSUSED */ 27094 void 27095 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 27096 { 27097 mblk_t *mp1; 27098 ire_t *ire, *fake_ire; 27099 ill_t *ill; 27100 struct iocblk *iocp; 27101 ip_ioctl_cmd_t *ipip; 27102 cred_t *cr; 27103 conn_t *connp; 27104 int err; 27105 nce_t *nce; 27106 ipif_t *ipif; 27107 ip_stack_t *ipst; 27108 char *proto_str; 27109 27110 if (CONN_Q(q)) { 27111 connp = Q_TO_CONN(q); 27112 ipst = connp->conn_netstack->netstack_ip; 27113 } else { 27114 connp = NULL; 27115 ipst = ILLQ_TO_IPST(q); 27116 } 27117 27118 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 27119 27120 switch (DB_TYPE(mp)) { 27121 case M_IOCTL: 27122 /* 27123 * IOCTL processing begins in ip_sioctl_copyin_setup which 27124 * will arrange to copy in associated control structures. 27125 */ 27126 ip_sioctl_copyin_setup(q, mp); 27127 return; 27128 case M_IOCDATA: 27129 /* 27130 * Ensure that this is associated with one of our trans- 27131 * parent ioctls. If it's not ours, discard it if we're 27132 * running as a driver, or pass it on if we're a module. 27133 */ 27134 iocp = (struct iocblk *)mp->b_rptr; 27135 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27136 if (ipip == NULL) { 27137 if (q->q_next == NULL) { 27138 goto nak; 27139 } else { 27140 putnext(q, mp); 27141 } 27142 return; 27143 } 27144 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) { 27145 /* 27146 * the ioctl is one we recognise, but is not 27147 * consumed by IP as a module, pass M_IOCDATA 27148 * for processing downstream, but only for 27149 * common Streams ioctls. 27150 */ 27151 if (ipip->ipi_flags & IPI_PASS_DOWN) { 27152 putnext(q, mp); 27153 return; 27154 } else { 27155 goto nak; 27156 } 27157 } 27158 27159 /* IOCTL continuation following copyin or copyout. */ 27160 if (mi_copy_state(q, mp, NULL) == -1) { 27161 /* 27162 * The copy operation failed. mi_copy_state already 27163 * cleaned up, so we're out of here. 27164 */ 27165 return; 27166 } 27167 /* 27168 * If we just completed a copy in, we become writer and 27169 * continue processing in ip_sioctl_copyin_done. If it 27170 * was a copy out, we call mi_copyout again. If there is 27171 * nothing more to copy out, it will complete the IOCTL. 27172 */ 27173 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 27174 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 27175 mi_copy_done(q, mp, EPROTO); 27176 return; 27177 } 27178 /* 27179 * Check for cases that need more copying. A return 27180 * value of 0 means a second copyin has been started, 27181 * so we return; a return value of 1 means no more 27182 * copying is needed, so we continue. 27183 */ 27184 if (ipip->ipi_cmd_type == MSFILT_CMD && 27185 MI_COPY_COUNT(mp) == 1) { 27186 if (ip_copyin_msfilter(q, mp) == 0) 27187 return; 27188 } 27189 /* 27190 * Refhold the conn, till the ioctl completes. This is 27191 * needed in case the ioctl ends up in the pending mp 27192 * list. Every mp in the ill_pending_mp list and 27193 * the ipsq_pending_mp must have a refhold on the conn 27194 * to resume processing. The refhold is released when 27195 * the ioctl completes. (normally or abnormally) 27196 * In all cases ip_ioctl_finish is called to finish 27197 * the ioctl. 27198 */ 27199 if (connp != NULL) { 27200 /* This is not a reentry */ 27201 ASSERT(ipsq == NULL); 27202 CONN_INC_REF(connp); 27203 } else { 27204 if (!(ipip->ipi_flags & IPI_MODOK)) { 27205 mi_copy_done(q, mp, EINVAL); 27206 return; 27207 } 27208 } 27209 27210 ip_process_ioctl(ipsq, q, mp, ipip); 27211 27212 } else { 27213 mi_copyout(q, mp); 27214 } 27215 return; 27216 nak: 27217 iocp->ioc_error = EINVAL; 27218 mp->b_datap->db_type = M_IOCNAK; 27219 iocp->ioc_count = 0; 27220 qreply(q, mp); 27221 return; 27222 27223 case M_IOCNAK: 27224 /* 27225 * The only way we could get here is if a resolver didn't like 27226 * an IOCTL we sent it. This shouldn't happen. 27227 */ 27228 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 27229 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 27230 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 27231 freemsg(mp); 27232 return; 27233 case M_IOCACK: 27234 /* /dev/ip shouldn't see this */ 27235 if (CONN_Q(q)) 27236 goto nak; 27237 27238 /* Finish socket ioctls passed through to ARP. */ 27239 ip_sioctl_iocack(q, mp); 27240 return; 27241 case M_FLUSH: 27242 if (*mp->b_rptr & FLUSHW) 27243 flushq(q, FLUSHALL); 27244 if (q->q_next) { 27245 putnext(q, mp); 27246 return; 27247 } 27248 if (*mp->b_rptr & FLUSHR) { 27249 *mp->b_rptr &= ~FLUSHW; 27250 qreply(q, mp); 27251 return; 27252 } 27253 freemsg(mp); 27254 return; 27255 case IRE_DB_REQ_TYPE: 27256 if (connp == NULL) { 27257 proto_str = "IRE_DB_REQ_TYPE"; 27258 goto protonak; 27259 } 27260 /* An Upper Level Protocol wants a copy of an IRE. */ 27261 ip_ire_req(q, mp); 27262 return; 27263 case M_CTL: 27264 if (mp->b_wptr - mp->b_rptr < sizeof (uint32_t)) 27265 break; 27266 27267 if (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == 27268 TUN_HELLO) { 27269 ASSERT(connp != NULL); 27270 connp->conn_flags |= IPCL_IPTUN; 27271 freeb(mp); 27272 return; 27273 } 27274 27275 /* M_CTL messages are used by ARP to tell us things. */ 27276 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 27277 break; 27278 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 27279 case AR_ENTRY_SQUERY: 27280 ip_wput_ctl(q, mp); 27281 return; 27282 case AR_CLIENT_NOTIFY: 27283 ip_arp_news(q, mp); 27284 return; 27285 case AR_DLPIOP_DONE: 27286 ASSERT(q->q_next != NULL); 27287 ill = (ill_t *)q->q_ptr; 27288 /* qwriter_ip releases the refhold */ 27289 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 27290 ill_refhold(ill); 27291 qwriter_ip(ill, q, mp, ip_arp_done, CUR_OP, B_FALSE); 27292 return; 27293 case AR_ARP_CLOSING: 27294 /* 27295 * ARP (above us) is closing. If no ARP bringup is 27296 * currently pending, ack the message so that ARP 27297 * can complete its close. Also mark ill_arp_closing 27298 * so that new ARP bringups will fail. If any 27299 * ARP bringup is currently in progress, we will 27300 * ack this when the current ARP bringup completes. 27301 */ 27302 ASSERT(q->q_next != NULL); 27303 ill = (ill_t *)q->q_ptr; 27304 mutex_enter(&ill->ill_lock); 27305 ill->ill_arp_closing = 1; 27306 if (!ill->ill_arp_bringup_pending) { 27307 mutex_exit(&ill->ill_lock); 27308 qreply(q, mp); 27309 } else { 27310 mutex_exit(&ill->ill_lock); 27311 freemsg(mp); 27312 } 27313 return; 27314 case AR_ARP_EXTEND: 27315 /* 27316 * The ARP module above us is capable of duplicate 27317 * address detection. Old ATM drivers will not send 27318 * this message. 27319 */ 27320 ASSERT(q->q_next != NULL); 27321 ill = (ill_t *)q->q_ptr; 27322 ill->ill_arp_extend = B_TRUE; 27323 freemsg(mp); 27324 return; 27325 default: 27326 break; 27327 } 27328 break; 27329 case M_PROTO: 27330 case M_PCPROTO: 27331 /* 27332 * The only PROTO messages we expect are ULP binds and 27333 * copies of option negotiation acknowledgements. 27334 */ 27335 switch (((union T_primitives *)mp->b_rptr)->type) { 27336 case O_T_BIND_REQ: 27337 case T_BIND_REQ: { 27338 /* Request can get queued in bind */ 27339 if (connp == NULL) { 27340 proto_str = "O_T_BIND_REQ/T_BIND_REQ"; 27341 goto protonak; 27342 } 27343 /* 27344 * The transports except SCTP call ip_bind_{v4,v6}() 27345 * directly instead of a a putnext. SCTP doesn't 27346 * generate any T_BIND_REQ since it has its own 27347 * fanout data structures. However, ESP and AH 27348 * come in for regular binds; all other cases are 27349 * bind retries. 27350 */ 27351 ASSERT(!IPCL_IS_SCTP(connp)); 27352 27353 /* Don't increment refcnt if this is a re-entry */ 27354 if (ipsq == NULL) 27355 CONN_INC_REF(connp); 27356 27357 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 27358 connp, NULL) : ip_bind_v4(q, mp, connp); 27359 if (mp == NULL) 27360 return; 27361 if (IPCL_IS_TCP(connp)) { 27362 /* 27363 * In the case of TCP endpoint we 27364 * come here only for bind retries 27365 */ 27366 ASSERT(ipsq != NULL); 27367 CONN_INC_REF(connp); 27368 squeue_fill(connp->conn_sqp, mp, 27369 ip_resume_tcp_bind, connp, 27370 SQTAG_BIND_RETRY); 27371 } else if (IPCL_IS_UDP(connp)) { 27372 /* 27373 * In the case of UDP endpoint we 27374 * come here only for bind retries 27375 */ 27376 ASSERT(ipsq != NULL); 27377 udp_resume_bind(connp, mp); 27378 } else if (IPCL_IS_RAWIP(connp)) { 27379 /* 27380 * In the case of RAWIP endpoint we 27381 * come here only for bind retries 27382 */ 27383 ASSERT(ipsq != NULL); 27384 rawip_resume_bind(connp, mp); 27385 } else { 27386 /* The case of AH and ESP */ 27387 qreply(q, mp); 27388 CONN_OPER_PENDING_DONE(connp); 27389 } 27390 return; 27391 } 27392 case T_SVR4_OPTMGMT_REQ: 27393 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 27394 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 27395 27396 if (connp == NULL) { 27397 proto_str = "T_SVR4_OPTMGMT_REQ"; 27398 goto protonak; 27399 } 27400 27401 if (!snmpcom_req(q, mp, ip_snmp_set, 27402 ip_snmp_get, cr)) { 27403 /* 27404 * Call svr4_optcom_req so that it can 27405 * generate the ack. We don't come here 27406 * if this operation is being restarted. 27407 * ip_restart_optmgmt will drop the conn ref. 27408 * In the case of ipsec option after the ipsec 27409 * load is complete conn_restart_ipsec_waiter 27410 * drops the conn ref. 27411 */ 27412 ASSERT(ipsq == NULL); 27413 CONN_INC_REF(connp); 27414 if (ip_check_for_ipsec_opt(q, mp)) 27415 return; 27416 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj, 27417 B_FALSE); 27418 if (err != EINPROGRESS) { 27419 /* Operation is done */ 27420 CONN_OPER_PENDING_DONE(connp); 27421 } 27422 } 27423 return; 27424 case T_OPTMGMT_REQ: 27425 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 27426 /* 27427 * Note: No snmpcom_req support through new 27428 * T_OPTMGMT_REQ. 27429 * Call tpi_optcom_req so that it can 27430 * generate the ack. 27431 */ 27432 if (connp == NULL) { 27433 proto_str = "T_OPTMGMT_REQ"; 27434 goto protonak; 27435 } 27436 27437 ASSERT(ipsq == NULL); 27438 /* 27439 * We don't come here for restart. ip_restart_optmgmt 27440 * will drop the conn ref. In the case of ipsec option 27441 * after the ipsec load is complete 27442 * conn_restart_ipsec_waiter drops the conn ref. 27443 */ 27444 CONN_INC_REF(connp); 27445 if (ip_check_for_ipsec_opt(q, mp)) 27446 return; 27447 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj, B_FALSE); 27448 if (err != EINPROGRESS) { 27449 /* Operation is done */ 27450 CONN_OPER_PENDING_DONE(connp); 27451 } 27452 return; 27453 case T_UNBIND_REQ: 27454 if (connp == NULL) { 27455 proto_str = "T_UNBIND_REQ"; 27456 goto protonak; 27457 } 27458 mp = ip_unbind(q, mp); 27459 qreply(q, mp); 27460 return; 27461 default: 27462 /* 27463 * Have to drop any DLPI messages coming down from 27464 * arp (such as an info_req which would cause ip 27465 * to receive an extra info_ack if it was passed 27466 * through. 27467 */ 27468 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 27469 (int)*(uint_t *)mp->b_rptr)); 27470 freemsg(mp); 27471 return; 27472 } 27473 /* NOTREACHED */ 27474 case IRE_DB_TYPE: { 27475 nce_t *nce; 27476 ill_t *ill; 27477 in6_addr_t gw_addr_v6; 27478 27479 27480 /* 27481 * This is a response back from a resolver. It 27482 * consists of a message chain containing: 27483 * IRE_MBLK-->LL_HDR_MBLK->pkt 27484 * The IRE_MBLK is the one we allocated in ip_newroute. 27485 * The LL_HDR_MBLK is the DLPI header to use to get 27486 * the attached packet, and subsequent ones for the 27487 * same destination, transmitted. 27488 */ 27489 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 27490 break; 27491 /* 27492 * First, check to make sure the resolution succeeded. 27493 * If it failed, the second mblk will be empty. 27494 * If it is, free the chain, dropping the packet. 27495 * (We must ire_delete the ire; that frees the ire mblk) 27496 * We're doing this now to support PVCs for ATM; it's 27497 * a partial xresolv implementation. When we fully implement 27498 * xresolv interfaces, instead of freeing everything here 27499 * we'll initiate neighbor discovery. 27500 * 27501 * For v4 (ARP and other external resolvers) the resolver 27502 * frees the message, so no check is needed. This check 27503 * is required, though, for a full xresolve implementation. 27504 * Including this code here now both shows how external 27505 * resolvers can NACK a resolution request using an 27506 * existing design that has no specific provisions for NACKs, 27507 * and also takes into account that the current non-ARP 27508 * external resolver has been coded to use this method of 27509 * NACKing for all IPv6 (xresolv) cases, 27510 * whether our xresolv implementation is complete or not. 27511 * 27512 */ 27513 ire = (ire_t *)mp->b_rptr; 27514 ill = ire_to_ill(ire); 27515 mp1 = mp->b_cont; /* dl_unitdata_req */ 27516 if (mp1->b_rptr == mp1->b_wptr) { 27517 if (ire->ire_ipversion == IPV6_VERSION) { 27518 /* 27519 * XRESOLV interface. 27520 */ 27521 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27522 mutex_enter(&ire->ire_lock); 27523 gw_addr_v6 = ire->ire_gateway_addr_v6; 27524 mutex_exit(&ire->ire_lock); 27525 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27526 nce = ndp_lookup_v6(ill, 27527 &ire->ire_addr_v6, B_FALSE); 27528 } else { 27529 nce = ndp_lookup_v6(ill, &gw_addr_v6, 27530 B_FALSE); 27531 } 27532 if (nce != NULL) { 27533 nce_resolv_failed(nce); 27534 ndp_delete(nce); 27535 NCE_REFRELE(nce); 27536 } 27537 } 27538 mp->b_cont = NULL; 27539 freemsg(mp1); /* frees the pkt as well */ 27540 ASSERT(ire->ire_nce == NULL); 27541 ire_delete((ire_t *)mp->b_rptr); 27542 return; 27543 } 27544 27545 /* 27546 * Split them into IRE_MBLK and pkt and feed it into 27547 * ire_add_then_send. Then in ire_add_then_send 27548 * the IRE will be added, and then the packet will be 27549 * run back through ip_wput. This time it will make 27550 * it to the wire. 27551 */ 27552 mp->b_cont = NULL; 27553 mp = mp1->b_cont; /* now, mp points to pkt */ 27554 mp1->b_cont = NULL; 27555 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 27556 if (ire->ire_ipversion == IPV6_VERSION) { 27557 /* 27558 * XRESOLV interface. Find the nce and put a copy 27559 * of the dl_unitdata_req in nce_res_mp 27560 */ 27561 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27562 mutex_enter(&ire->ire_lock); 27563 gw_addr_v6 = ire->ire_gateway_addr_v6; 27564 mutex_exit(&ire->ire_lock); 27565 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27566 nce = ndp_lookup_v6(ill, &ire->ire_addr_v6, 27567 B_FALSE); 27568 } else { 27569 nce = ndp_lookup_v6(ill, &gw_addr_v6, B_FALSE); 27570 } 27571 if (nce != NULL) { 27572 /* 27573 * We have to protect nce_res_mp here 27574 * from being accessed by other threads 27575 * while we change the mblk pointer. 27576 * Other functions will also lock the nce when 27577 * accessing nce_res_mp. 27578 * 27579 * The reason we change the mblk pointer 27580 * here rather than copying the resolved address 27581 * into the template is that, unlike with 27582 * ethernet, we have no guarantee that the 27583 * resolved address length will be 27584 * smaller than or equal to the lla length 27585 * with which the template was allocated, 27586 * (for ethernet, they're equal) 27587 * so we have to use the actual resolved 27588 * address mblk - which holds the real 27589 * dl_unitdata_req with the resolved address. 27590 * 27591 * Doing this is the same behavior as was 27592 * previously used in the v4 ARP case. 27593 */ 27594 mutex_enter(&nce->nce_lock); 27595 if (nce->nce_res_mp != NULL) 27596 freemsg(nce->nce_res_mp); 27597 nce->nce_res_mp = mp1; 27598 mutex_exit(&nce->nce_lock); 27599 /* 27600 * We do a fastpath probe here because 27601 * we have resolved the address without 27602 * using Neighbor Discovery. 27603 * In the non-XRESOLV v6 case, the fastpath 27604 * probe is done right after neighbor 27605 * discovery completes. 27606 */ 27607 if (nce->nce_res_mp != NULL) { 27608 int res; 27609 nce_fastpath_list_add(nce); 27610 res = ill_fastpath_probe(ill, 27611 nce->nce_res_mp); 27612 if (res != 0 && res != EAGAIN) 27613 nce_fastpath_list_delete(nce); 27614 } 27615 27616 ire_add_then_send(q, ire, mp); 27617 /* 27618 * Now we have to clean out any packets 27619 * that may have been queued on the nce 27620 * while it was waiting for address resolution 27621 * to complete. 27622 */ 27623 mutex_enter(&nce->nce_lock); 27624 mp1 = nce->nce_qd_mp; 27625 nce->nce_qd_mp = NULL; 27626 mutex_exit(&nce->nce_lock); 27627 while (mp1 != NULL) { 27628 mblk_t *nxt_mp; 27629 queue_t *fwdq = NULL; 27630 ill_t *inbound_ill; 27631 uint_t ifindex; 27632 27633 nxt_mp = mp1->b_next; 27634 mp1->b_next = NULL; 27635 /* 27636 * Retrieve ifindex stored in 27637 * ip_rput_data_v6() 27638 */ 27639 ifindex = 27640 (uint_t)(uintptr_t)mp1->b_prev; 27641 inbound_ill = 27642 ill_lookup_on_ifindex(ifindex, 27643 B_TRUE, NULL, NULL, NULL, 27644 NULL, ipst); 27645 mp1->b_prev = NULL; 27646 if (inbound_ill != NULL) 27647 fwdq = inbound_ill->ill_rq; 27648 27649 if (fwdq != NULL) { 27650 put(fwdq, mp1); 27651 ill_refrele(inbound_ill); 27652 } else 27653 put(WR(ill->ill_rq), mp1); 27654 mp1 = nxt_mp; 27655 } 27656 NCE_REFRELE(nce); 27657 } else { /* nce is NULL; clean up */ 27658 ire_delete(ire); 27659 freemsg(mp); 27660 freemsg(mp1); 27661 return; 27662 } 27663 } else { 27664 nce_t *arpce; 27665 /* 27666 * Link layer resolution succeeded. Recompute the 27667 * ire_nce. 27668 */ 27669 ASSERT(ire->ire_type & (IRE_CACHE|IRE_BROADCAST)); 27670 if ((arpce = ndp_lookup_v4(ill, 27671 (ire->ire_gateway_addr != INADDR_ANY ? 27672 &ire->ire_gateway_addr : &ire->ire_addr), 27673 B_FALSE)) == NULL) { 27674 freeb(ire->ire_mp); 27675 freeb(mp1); 27676 freemsg(mp); 27677 return; 27678 } 27679 mutex_enter(&arpce->nce_lock); 27680 arpce->nce_last = TICK_TO_MSEC(lbolt64); 27681 if (arpce->nce_state == ND_REACHABLE) { 27682 /* 27683 * Someone resolved this before us; 27684 * cleanup the res_mp. Since ire has 27685 * not been added yet, the call to ire_add_v4 27686 * from ire_add_then_send (when a dup is 27687 * detected) will clean up the ire. 27688 */ 27689 freeb(mp1); 27690 } else { 27691 ASSERT(arpce->nce_res_mp == NULL); 27692 arpce->nce_res_mp = mp1; 27693 arpce->nce_state = ND_REACHABLE; 27694 } 27695 mutex_exit(&arpce->nce_lock); 27696 if (ire->ire_marks & IRE_MARK_NOADD) { 27697 /* 27698 * this ire will not be added to the ire 27699 * cache table, so we can set the ire_nce 27700 * here, as there are no atomicity constraints. 27701 */ 27702 ire->ire_nce = arpce; 27703 /* 27704 * We are associating this nce with the ire 27705 * so change the nce ref taken in 27706 * ndp_lookup_v4() from 27707 * NCE_REFHOLD to NCE_REFHOLD_NOTR 27708 */ 27709 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 27710 } else { 27711 NCE_REFRELE(arpce); 27712 } 27713 ire_add_then_send(q, ire, mp); 27714 } 27715 return; /* All is well, the packet has been sent. */ 27716 } 27717 case IRE_ARPRESOLVE_TYPE: { 27718 27719 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* fake_ire */ 27720 break; 27721 mp1 = mp->b_cont; /* dl_unitdata_req */ 27722 mp->b_cont = NULL; 27723 /* 27724 * First, check to make sure the resolution succeeded. 27725 * If it failed, the second mblk will be empty. 27726 */ 27727 if (mp1->b_rptr == mp1->b_wptr) { 27728 /* cleanup the incomplete ire, free queued packets */ 27729 freemsg(mp); /* fake ire */ 27730 freeb(mp1); /* dl_unitdata response */ 27731 return; 27732 } 27733 27734 /* 27735 * update any incomplete nce_t found. we lookup the ctable 27736 * and find the nce from the ire->ire_nce because we need 27737 * to pass the ire to ip_xmit_v4 later, and can find both 27738 * ire and nce in one lookup from the ctable. 27739 */ 27740 fake_ire = (ire_t *)mp->b_rptr; 27741 /* 27742 * By the time we come back here from ARP 27743 * the logical outgoing interface of the incomplete ire 27744 * we added in ire_forward could have disappeared, 27745 * causing the incomplete ire to also have 27746 * dissapeared. So we need to retreive the 27747 * proper ipif for the ire before looking 27748 * in ctable; do the ctablelookup based on ire_ipif_seqid 27749 */ 27750 ill = q->q_ptr; 27751 27752 /* Get the outgoing ipif */ 27753 mutex_enter(&ill->ill_lock); 27754 if (ill->ill_state_flags & ILL_CONDEMNED) { 27755 mutex_exit(&ill->ill_lock); 27756 freemsg(mp); /* fake ire */ 27757 freeb(mp1); /* dl_unitdata response */ 27758 return; 27759 } 27760 ipif = ipif_lookup_seqid(ill, fake_ire->ire_ipif_seqid); 27761 27762 if (ipif == NULL) { 27763 mutex_exit(&ill->ill_lock); 27764 ip1dbg(("logical intrf to incomplete ire vanished\n")); 27765 freemsg(mp); 27766 freeb(mp1); 27767 return; 27768 } 27769 ipif_refhold_locked(ipif); 27770 mutex_exit(&ill->ill_lock); 27771 ire = ire_ctable_lookup(fake_ire->ire_addr, 27772 fake_ire->ire_gateway_addr, IRE_CACHE, 27773 ipif, fake_ire->ire_zoneid, NULL, 27774 (MATCH_IRE_GW|MATCH_IRE_IPIF|MATCH_IRE_ZONEONLY), ipst); 27775 ipif_refrele(ipif); 27776 if (ire == NULL) { 27777 /* 27778 * no ire was found; check if there is an nce 27779 * for this lookup; if it has no ire's pointing at it 27780 * cleanup. 27781 */ 27782 if ((nce = ndp_lookup_v4(ill, 27783 (fake_ire->ire_gateway_addr != INADDR_ANY ? 27784 &fake_ire->ire_gateway_addr : &fake_ire->ire_addr), 27785 B_FALSE)) != NULL) { 27786 /* 27787 * cleanup: 27788 * We check for refcnt 2 (one for the nce 27789 * hash list + 1 for the ref taken by 27790 * ndp_lookup_v4) to check that there are 27791 * no ire's pointing at the nce. 27792 */ 27793 if (nce->nce_refcnt == 2) 27794 ndp_delete(nce); 27795 NCE_REFRELE(nce); 27796 } 27797 freeb(mp1); /* dl_unitdata response */ 27798 freemsg(mp); /* fake ire */ 27799 return; 27800 } 27801 nce = ire->ire_nce; 27802 DTRACE_PROBE2(ire__arpresolve__type, 27803 ire_t *, ire, nce_t *, nce); 27804 ASSERT(nce->nce_state != ND_INITIAL); 27805 mutex_enter(&nce->nce_lock); 27806 nce->nce_last = TICK_TO_MSEC(lbolt64); 27807 if (nce->nce_state == ND_REACHABLE) { 27808 /* 27809 * Someone resolved this before us; 27810 * our response is not needed any more. 27811 */ 27812 mutex_exit(&nce->nce_lock); 27813 freeb(mp1); /* dl_unitdata response */ 27814 } else { 27815 ASSERT(nce->nce_res_mp == NULL); 27816 nce->nce_res_mp = mp1; 27817 nce->nce_state = ND_REACHABLE; 27818 mutex_exit(&nce->nce_lock); 27819 nce_fastpath(nce); 27820 } 27821 /* 27822 * The cached nce_t has been updated to be reachable; 27823 * Clear the IRE_MARK_UNCACHED flag and free the fake_ire. 27824 */ 27825 fake_ire->ire_marks &= ~IRE_MARK_UNCACHED; 27826 freemsg(mp); 27827 /* 27828 * send out queued packets. 27829 */ 27830 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 27831 27832 IRE_REFRELE(ire); 27833 return; 27834 } 27835 default: 27836 break; 27837 } 27838 if (q->q_next) { 27839 putnext(q, mp); 27840 } else 27841 freemsg(mp); 27842 return; 27843 27844 protonak: 27845 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str); 27846 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL) 27847 qreply(q, mp); 27848 } 27849 27850 /* 27851 * Process IP options in an outbound packet. Modify the destination if there 27852 * is a source route option. 27853 * Returns non-zero if something fails in which case an ICMP error has been 27854 * sent and mp freed. 27855 */ 27856 static int 27857 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 27858 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 27859 { 27860 ipoptp_t opts; 27861 uchar_t *opt; 27862 uint8_t optval; 27863 uint8_t optlen; 27864 ipaddr_t dst; 27865 intptr_t code = 0; 27866 mblk_t *mp; 27867 ire_t *ire = NULL; 27868 27869 ip2dbg(("ip_wput_options\n")); 27870 mp = ipsec_mp; 27871 if (mctl_present) { 27872 mp = ipsec_mp->b_cont; 27873 } 27874 27875 dst = ipha->ipha_dst; 27876 for (optval = ipoptp_first(&opts, ipha); 27877 optval != IPOPT_EOL; 27878 optval = ipoptp_next(&opts)) { 27879 opt = opts.ipoptp_cur; 27880 optlen = opts.ipoptp_len; 27881 ip2dbg(("ip_wput_options: opt %d, len %d\n", 27882 optval, optlen)); 27883 switch (optval) { 27884 uint32_t off; 27885 case IPOPT_SSRR: 27886 case IPOPT_LSRR: 27887 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27888 ip1dbg(( 27889 "ip_wput_options: bad option offset\n")); 27890 code = (char *)&opt[IPOPT_OLEN] - 27891 (char *)ipha; 27892 goto param_prob; 27893 } 27894 off = opt[IPOPT_OFFSET]; 27895 ip1dbg(("ip_wput_options: next hop 0x%x\n", 27896 ntohl(dst))); 27897 /* 27898 * For strict: verify that dst is directly 27899 * reachable. 27900 */ 27901 if (optval == IPOPT_SSRR) { 27902 ire = ire_ftable_lookup(dst, 0, 0, 27903 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 27904 MBLK_GETLABEL(mp), 27905 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 27906 if (ire == NULL) { 27907 ip1dbg(("ip_wput_options: SSRR not" 27908 " directly reachable: 0x%x\n", 27909 ntohl(dst))); 27910 goto bad_src_route; 27911 } 27912 ire_refrele(ire); 27913 } 27914 break; 27915 case IPOPT_RR: 27916 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27917 ip1dbg(( 27918 "ip_wput_options: bad option offset\n")); 27919 code = (char *)&opt[IPOPT_OLEN] - 27920 (char *)ipha; 27921 goto param_prob; 27922 } 27923 break; 27924 case IPOPT_TS: 27925 /* 27926 * Verify that length >=5 and that there is either 27927 * room for another timestamp or that the overflow 27928 * counter is not maxed out. 27929 */ 27930 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 27931 if (optlen < IPOPT_MINLEN_IT) { 27932 goto param_prob; 27933 } 27934 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27935 ip1dbg(( 27936 "ip_wput_options: bad option offset\n")); 27937 code = (char *)&opt[IPOPT_OFFSET] - 27938 (char *)ipha; 27939 goto param_prob; 27940 } 27941 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 27942 case IPOPT_TS_TSONLY: 27943 off = IPOPT_TS_TIMELEN; 27944 break; 27945 case IPOPT_TS_TSANDADDR: 27946 case IPOPT_TS_PRESPEC: 27947 case IPOPT_TS_PRESPEC_RFC791: 27948 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 27949 break; 27950 default: 27951 code = (char *)&opt[IPOPT_POS_OV_FLG] - 27952 (char *)ipha; 27953 goto param_prob; 27954 } 27955 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 27956 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 27957 /* 27958 * No room and the overflow counter is 15 27959 * already. 27960 */ 27961 goto param_prob; 27962 } 27963 break; 27964 } 27965 } 27966 27967 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 27968 return (0); 27969 27970 ip1dbg(("ip_wput_options: error processing IP options.")); 27971 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 27972 27973 param_prob: 27974 /* 27975 * Since ip_wput() isn't close to finished, we fill 27976 * in enough of the header for credible error reporting. 27977 */ 27978 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 27979 /* Failed */ 27980 freemsg(ipsec_mp); 27981 return (-1); 27982 } 27983 icmp_param_problem(q, ipsec_mp, (uint8_t)code, zoneid, ipst); 27984 return (-1); 27985 27986 bad_src_route: 27987 /* 27988 * Since ip_wput() isn't close to finished, we fill 27989 * in enough of the header for credible error reporting. 27990 */ 27991 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 27992 /* Failed */ 27993 freemsg(ipsec_mp); 27994 return (-1); 27995 } 27996 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 27997 return (-1); 27998 } 27999 28000 /* 28001 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 28002 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 28003 * thru /etc/system. 28004 */ 28005 #define CONN_MAXDRAINCNT 64 28006 28007 static void 28008 conn_drain_init(ip_stack_t *ipst) 28009 { 28010 int i; 28011 28012 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads; 28013 28014 if ((ipst->ips_conn_drain_list_cnt == 0) || 28015 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 28016 /* 28017 * Default value of the number of drainers is the 28018 * number of cpus, subject to maximum of 8 drainers. 28019 */ 28020 if (boot_max_ncpus != -1) 28021 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 28022 else 28023 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8); 28024 } 28025 28026 ipst->ips_conn_drain_list = kmem_zalloc(ipst->ips_conn_drain_list_cnt * 28027 sizeof (idl_t), KM_SLEEP); 28028 28029 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28030 mutex_init(&ipst->ips_conn_drain_list[i].idl_lock, NULL, 28031 MUTEX_DEFAULT, NULL); 28032 } 28033 } 28034 28035 static void 28036 conn_drain_fini(ip_stack_t *ipst) 28037 { 28038 int i; 28039 28040 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) 28041 mutex_destroy(&ipst->ips_conn_drain_list[i].idl_lock); 28042 kmem_free(ipst->ips_conn_drain_list, 28043 ipst->ips_conn_drain_list_cnt * sizeof (idl_t)); 28044 ipst->ips_conn_drain_list = NULL; 28045 } 28046 28047 /* 28048 * Note: For an overview of how flowcontrol is handled in IP please see the 28049 * IP Flowcontrol notes at the top of this file. 28050 * 28051 * Flow control has blocked us from proceeding. Insert the given conn in one 28052 * of the conn drain lists. These conn wq's will be qenabled later on when 28053 * STREAMS flow control does a backenable. conn_walk_drain will enable 28054 * the first conn in each of these drain lists. Each of these qenabled conns 28055 * in turn enables the next in the list, after it runs, or when it closes, 28056 * thus sustaining the drain process. 28057 * 28058 * The only possible calling sequence is ip_wsrv (on conn) -> ip_wput -> 28059 * conn_drain_insert. Thus there can be only 1 instance of conn_drain_insert 28060 * running at any time, on a given conn, since there can be only 1 service proc 28061 * running on a queue at any time. 28062 */ 28063 void 28064 conn_drain_insert(conn_t *connp) 28065 { 28066 idl_t *idl; 28067 uint_t index; 28068 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28069 28070 mutex_enter(&connp->conn_lock); 28071 if (connp->conn_state_flags & CONN_CLOSING) { 28072 /* 28073 * The conn is closing as a result of which CONN_CLOSING 28074 * is set. Return. 28075 */ 28076 mutex_exit(&connp->conn_lock); 28077 return; 28078 } else if (connp->conn_idl == NULL) { 28079 /* 28080 * Assign the next drain list round robin. We dont' use 28081 * a lock, and thus it may not be strictly round robin. 28082 * Atomicity of load/stores is enough to make sure that 28083 * conn_drain_list_index is always within bounds. 28084 */ 28085 index = ipst->ips_conn_drain_list_index; 28086 ASSERT(index < ipst->ips_conn_drain_list_cnt); 28087 connp->conn_idl = &ipst->ips_conn_drain_list[index]; 28088 index++; 28089 if (index == ipst->ips_conn_drain_list_cnt) 28090 index = 0; 28091 ipst->ips_conn_drain_list_index = index; 28092 } 28093 mutex_exit(&connp->conn_lock); 28094 28095 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28096 if ((connp->conn_drain_prev != NULL) || 28097 (connp->conn_state_flags & CONN_CLOSING)) { 28098 /* 28099 * The conn is already in the drain list, OR 28100 * the conn is closing. We need to check again for 28101 * the closing case again since close can happen 28102 * after we drop the conn_lock, and before we 28103 * acquire the CONN_DRAIN_LIST_LOCK. 28104 */ 28105 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28106 return; 28107 } else { 28108 idl = connp->conn_idl; 28109 } 28110 28111 /* 28112 * The conn is not in the drain list. Insert it at the 28113 * tail of the drain list. The drain list is circular 28114 * and doubly linked. idl_conn points to the 1st element 28115 * in the list. 28116 */ 28117 if (idl->idl_conn == NULL) { 28118 idl->idl_conn = connp; 28119 connp->conn_drain_next = connp; 28120 connp->conn_drain_prev = connp; 28121 } else { 28122 conn_t *head = idl->idl_conn; 28123 28124 connp->conn_drain_next = head; 28125 connp->conn_drain_prev = head->conn_drain_prev; 28126 head->conn_drain_prev->conn_drain_next = connp; 28127 head->conn_drain_prev = connp; 28128 } 28129 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28130 } 28131 28132 /* 28133 * This conn is closing, and we are called from ip_close. OR 28134 * This conn has been serviced by ip_wsrv, and we need to do the tail 28135 * processing. 28136 * If this conn is part of the drain list, we may need to sustain the drain 28137 * process by qenabling the next conn in the drain list. We may also need to 28138 * remove this conn from the list, if it is done. 28139 */ 28140 static void 28141 conn_drain_tail(conn_t *connp, boolean_t closing) 28142 { 28143 idl_t *idl; 28144 28145 /* 28146 * connp->conn_idl is stable at this point, and no lock is needed 28147 * to check it. If we are called from ip_close, close has already 28148 * set CONN_CLOSING, thus freezing the value of conn_idl, and 28149 * called us only because conn_idl is non-null. If we are called thru 28150 * service, conn_idl could be null, but it cannot change because 28151 * service is single-threaded per queue, and there cannot be another 28152 * instance of service trying to call conn_drain_insert on this conn 28153 * now. 28154 */ 28155 ASSERT(!closing || (connp->conn_idl != NULL)); 28156 28157 /* 28158 * If connp->conn_idl is null, the conn has not been inserted into any 28159 * drain list even once since creation of the conn. Just return. 28160 */ 28161 if (connp->conn_idl == NULL) 28162 return; 28163 28164 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28165 28166 if (connp->conn_drain_prev == NULL) { 28167 /* This conn is currently not in the drain list. */ 28168 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28169 return; 28170 } 28171 idl = connp->conn_idl; 28172 if (idl->idl_conn_draining == connp) { 28173 /* 28174 * This conn is the current drainer. If this is the last conn 28175 * in the drain list, we need to do more checks, in the 'if' 28176 * below. Otherwwise we need to just qenable the next conn, 28177 * to sustain the draining, and is handled in the 'else' 28178 * below. 28179 */ 28180 if (connp->conn_drain_next == idl->idl_conn) { 28181 /* 28182 * This conn is the last in this list. This round 28183 * of draining is complete. If idl_repeat is set, 28184 * it means another flow enabling has happened from 28185 * the driver/streams and we need to another round 28186 * of draining. 28187 * If there are more than 2 conns in the drain list, 28188 * do a left rotate by 1, so that all conns except the 28189 * conn at the head move towards the head by 1, and the 28190 * the conn at the head goes to the tail. This attempts 28191 * a more even share for all queues that are being 28192 * drained. 28193 */ 28194 if ((connp->conn_drain_next != connp) && 28195 (idl->idl_conn->conn_drain_next != connp)) { 28196 idl->idl_conn = idl->idl_conn->conn_drain_next; 28197 } 28198 if (idl->idl_repeat) { 28199 qenable(idl->idl_conn->conn_wq); 28200 idl->idl_conn_draining = idl->idl_conn; 28201 idl->idl_repeat = 0; 28202 } else { 28203 idl->idl_conn_draining = NULL; 28204 } 28205 } else { 28206 /* 28207 * If the next queue that we are now qenable'ing, 28208 * is closing, it will remove itself from this list 28209 * and qenable the subsequent queue in ip_close(). 28210 * Serialization is acheived thru idl_lock. 28211 */ 28212 qenable(connp->conn_drain_next->conn_wq); 28213 idl->idl_conn_draining = connp->conn_drain_next; 28214 } 28215 } 28216 if (!connp->conn_did_putbq || closing) { 28217 /* 28218 * Remove ourself from the drain list, if we did not do 28219 * a putbq, or if the conn is closing. 28220 * Note: It is possible that q->q_first is non-null. It means 28221 * that these messages landed after we did a enableok() in 28222 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 28223 * service them. 28224 */ 28225 if (connp->conn_drain_next == connp) { 28226 /* Singleton in the list */ 28227 ASSERT(connp->conn_drain_prev == connp); 28228 idl->idl_conn = NULL; 28229 idl->idl_conn_draining = NULL; 28230 } else { 28231 connp->conn_drain_prev->conn_drain_next = 28232 connp->conn_drain_next; 28233 connp->conn_drain_next->conn_drain_prev = 28234 connp->conn_drain_prev; 28235 if (idl->idl_conn == connp) 28236 idl->idl_conn = connp->conn_drain_next; 28237 ASSERT(idl->idl_conn_draining != connp); 28238 28239 } 28240 connp->conn_drain_next = NULL; 28241 connp->conn_drain_prev = NULL; 28242 } 28243 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28244 } 28245 28246 /* 28247 * Write service routine. Shared perimeter entry point. 28248 * ip_wsrv can be called in any of the following ways. 28249 * 1. The device queue's messages has fallen below the low water mark 28250 * and STREAMS has backenabled the ill_wq. We walk thru all the 28251 * the drain lists and backenable the first conn in each list. 28252 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 28253 * qenabled non-tcp upper layers. We start dequeing messages and call 28254 * ip_wput for each message. 28255 */ 28256 28257 void 28258 ip_wsrv(queue_t *q) 28259 { 28260 conn_t *connp; 28261 ill_t *ill; 28262 mblk_t *mp; 28263 28264 if (q->q_next) { 28265 ill = (ill_t *)q->q_ptr; 28266 if (ill->ill_state_flags == 0) { 28267 /* 28268 * The device flow control has opened up. 28269 * Walk through conn drain lists and qenable the 28270 * first conn in each list. This makes sense only 28271 * if the stream is fully plumbed and setup. 28272 * Hence the if check above. 28273 */ 28274 ip1dbg(("ip_wsrv: walking\n")); 28275 conn_walk_drain(ill->ill_ipst); 28276 } 28277 return; 28278 } 28279 28280 connp = Q_TO_CONN(q); 28281 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 28282 28283 /* 28284 * 1. Set conn_draining flag to signal that service is active. 28285 * 28286 * 2. ip_output determines whether it has been called from service, 28287 * based on the last parameter. If it is IP_WSRV it concludes it 28288 * has been called from service. 28289 * 28290 * 3. Message ordering is preserved by the following logic. 28291 * i. A directly called ip_output (i.e. not thru service) will queue 28292 * the message at the tail, if conn_draining is set (i.e. service 28293 * is running) or if q->q_first is non-null. 28294 * 28295 * ii. If ip_output is called from service, and if ip_output cannot 28296 * putnext due to flow control, it does a putbq. 28297 * 28298 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 28299 * (causing an infinite loop). 28300 */ 28301 ASSERT(!connp->conn_did_putbq); 28302 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 28303 connp->conn_draining = 1; 28304 noenable(q); 28305 while ((mp = getq(q)) != NULL) { 28306 ASSERT(CONN_Q(q)); 28307 28308 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 28309 if (connp->conn_did_putbq) { 28310 /* ip_wput did a putbq */ 28311 break; 28312 } 28313 } 28314 /* 28315 * At this point, a thread coming down from top, calling 28316 * ip_wput, may end up queueing the message. We have not yet 28317 * enabled the queue, so ip_wsrv won't be called again. 28318 * To avoid this race, check q->q_first again (in the loop) 28319 * If the other thread queued the message before we call 28320 * enableok(), we will catch it in the q->q_first check. 28321 * If the other thread queues the message after we call 28322 * enableok(), ip_wsrv will be called again by STREAMS. 28323 */ 28324 connp->conn_draining = 0; 28325 enableok(q); 28326 } 28327 28328 /* Enable the next conn for draining */ 28329 conn_drain_tail(connp, B_FALSE); 28330 28331 connp->conn_did_putbq = 0; 28332 } 28333 28334 /* 28335 * Walk the list of all conn's calling the function provided with the 28336 * specified argument for each. Note that this only walks conn's that 28337 * have been bound. 28338 * Applies to both IPv4 and IPv6. 28339 */ 28340 static void 28341 conn_walk_fanout(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 28342 { 28343 conn_walk_fanout_table(ipst->ips_ipcl_udp_fanout, 28344 ipst->ips_ipcl_udp_fanout_size, 28345 func, arg, zoneid); 28346 conn_walk_fanout_table(ipst->ips_ipcl_conn_fanout, 28347 ipst->ips_ipcl_conn_fanout_size, 28348 func, arg, zoneid); 28349 conn_walk_fanout_table(ipst->ips_ipcl_bind_fanout, 28350 ipst->ips_ipcl_bind_fanout_size, 28351 func, arg, zoneid); 28352 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout, 28353 IPPROTO_MAX, func, arg, zoneid); 28354 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout_v6, 28355 IPPROTO_MAX, func, arg, zoneid); 28356 } 28357 28358 /* 28359 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 28360 * of conns that need to be drained, check if drain is already in progress. 28361 * If so set the idl_repeat bit, indicating that the last conn in the list 28362 * needs to reinitiate the drain once again, for the list. If drain is not 28363 * in progress for the list, initiate the draining, by qenabling the 1st 28364 * conn in the list. The drain is self-sustaining, each qenabled conn will 28365 * in turn qenable the next conn, when it is done/blocked/closing. 28366 */ 28367 static void 28368 conn_walk_drain(ip_stack_t *ipst) 28369 { 28370 int i; 28371 idl_t *idl; 28372 28373 IP_STAT(ipst, ip_conn_walk_drain); 28374 28375 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28376 idl = &ipst->ips_conn_drain_list[i]; 28377 mutex_enter(&idl->idl_lock); 28378 if (idl->idl_conn == NULL) { 28379 mutex_exit(&idl->idl_lock); 28380 continue; 28381 } 28382 /* 28383 * If this list is not being drained currently by 28384 * an ip_wsrv thread, start the process. 28385 */ 28386 if (idl->idl_conn_draining == NULL) { 28387 ASSERT(idl->idl_repeat == 0); 28388 qenable(idl->idl_conn->conn_wq); 28389 idl->idl_conn_draining = idl->idl_conn; 28390 } else { 28391 idl->idl_repeat = 1; 28392 } 28393 mutex_exit(&idl->idl_lock); 28394 } 28395 } 28396 28397 /* 28398 * Walk an conn hash table of `count' buckets, calling func for each entry. 28399 */ 28400 static void 28401 conn_walk_fanout_table(connf_t *connfp, uint_t count, pfv_t func, void *arg, 28402 zoneid_t zoneid) 28403 { 28404 conn_t *connp; 28405 28406 while (count-- > 0) { 28407 mutex_enter(&connfp->connf_lock); 28408 for (connp = connfp->connf_head; connp != NULL; 28409 connp = connp->conn_next) { 28410 if (zoneid == GLOBAL_ZONEID || 28411 zoneid == connp->conn_zoneid) { 28412 CONN_INC_REF(connp); 28413 mutex_exit(&connfp->connf_lock); 28414 (*func)(connp, arg); 28415 mutex_enter(&connfp->connf_lock); 28416 CONN_DEC_REF(connp); 28417 } 28418 } 28419 mutex_exit(&connfp->connf_lock); 28420 connfp++; 28421 } 28422 } 28423 28424 /* conn_walk_fanout routine invoked for ip_conn_report for each conn. */ 28425 static void 28426 conn_report1(conn_t *connp, void *mp) 28427 { 28428 char buf1[INET6_ADDRSTRLEN]; 28429 char buf2[INET6_ADDRSTRLEN]; 28430 uint_t print_len, buf_len; 28431 28432 ASSERT(connp != NULL); 28433 28434 buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr; 28435 if (buf_len <= 0) 28436 return; 28437 (void) inet_ntop(AF_INET6, &connp->conn_srcv6, buf1, sizeof (buf1)); 28438 (void) inet_ntop(AF_INET6, &connp->conn_remv6, buf2, sizeof (buf2)); 28439 print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len, 28440 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 28441 "%5d %s/%05d %s/%05d\n", 28442 (void *)connp, (void *)CONNP_TO_RQ(connp), 28443 (void *)CONNP_TO_WQ(connp), connp->conn_zoneid, 28444 buf1, connp->conn_lport, 28445 buf2, connp->conn_fport); 28446 if (print_len < buf_len) { 28447 ((mblk_t *)mp)->b_wptr += print_len; 28448 } else { 28449 ((mblk_t *)mp)->b_wptr += buf_len; 28450 } 28451 } 28452 28453 /* 28454 * Named Dispatch routine to produce a formatted report on all conns 28455 * that are listed in one of the fanout tables. 28456 * This report is accessed by using the ndd utility to "get" ND variable 28457 * "ip_conn_status". 28458 */ 28459 /* ARGSUSED */ 28460 static int 28461 ip_conn_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 28462 { 28463 conn_t *connp = Q_TO_CONN(q); 28464 28465 (void) mi_mpprintf(mp, 28466 "CONN " MI_COL_HDRPAD_STR 28467 "rfq " MI_COL_HDRPAD_STR 28468 "stq " MI_COL_HDRPAD_STR 28469 " zone local remote"); 28470 28471 /* 28472 * Because of the ndd constraint, at most we can have 64K buffer 28473 * to put in all conn info. So to be more efficient, just 28474 * allocate a 64K buffer here, assuming we need that large buffer. 28475 * This should be OK as only privileged processes can do ndd /dev/ip. 28476 */ 28477 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 28478 /* The following may work even if we cannot get a large buf. */ 28479 (void) mi_mpprintf(mp, "<< Out of buffer >>\n"); 28480 return (0); 28481 } 28482 28483 conn_walk_fanout(conn_report1, mp->b_cont, connp->conn_zoneid, 28484 connp->conn_netstack->netstack_ip); 28485 return (0); 28486 } 28487 28488 /* 28489 * Determine if the ill and multicast aspects of that packets 28490 * "matches" the conn. 28491 */ 28492 boolean_t 28493 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 28494 zoneid_t zoneid) 28495 { 28496 ill_t *in_ill; 28497 boolean_t found; 28498 ipif_t *ipif; 28499 ire_t *ire; 28500 ipaddr_t dst, src; 28501 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28502 28503 dst = ipha->ipha_dst; 28504 src = ipha->ipha_src; 28505 28506 /* 28507 * conn_incoming_ill is set by IP_BOUND_IF which limits 28508 * unicast, broadcast and multicast reception to 28509 * conn_incoming_ill. conn_wantpacket itself is called 28510 * only for BROADCAST and multicast. 28511 * 28512 * 1) ip_rput supresses duplicate broadcasts if the ill 28513 * is part of a group. Hence, we should be receiving 28514 * just one copy of broadcast for the whole group. 28515 * Thus, if it is part of the group the packet could 28516 * come on any ill of the group and hence we need a 28517 * match on the group. Otherwise, match on ill should 28518 * be sufficient. 28519 * 28520 * 2) ip_rput does not suppress duplicate multicast packets. 28521 * If there are two interfaces in a ill group and we have 28522 * 2 applications (conns) joined a multicast group G on 28523 * both the interfaces, ilm_lookup_ill filter in ip_rput 28524 * will give us two packets because we join G on both the 28525 * interfaces rather than nominating just one interface 28526 * for receiving multicast like broadcast above. So, 28527 * we have to call ilg_lookup_ill to filter out duplicate 28528 * copies, if ill is part of a group. 28529 */ 28530 in_ill = connp->conn_incoming_ill; 28531 if (in_ill != NULL) { 28532 if (in_ill->ill_group == NULL) { 28533 if (in_ill != ill) 28534 return (B_FALSE); 28535 } else if (in_ill->ill_group != ill->ill_group) { 28536 return (B_FALSE); 28537 } 28538 } 28539 28540 if (!CLASSD(dst)) { 28541 if (IPCL_ZONE_MATCH(connp, zoneid)) 28542 return (B_TRUE); 28543 /* 28544 * The conn is in a different zone; we need to check that this 28545 * broadcast address is configured in the application's zone and 28546 * on one ill in the group. 28547 */ 28548 ipif = ipif_get_next_ipif(NULL, ill); 28549 if (ipif == NULL) 28550 return (B_FALSE); 28551 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 28552 connp->conn_zoneid, NULL, 28553 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 28554 ipif_refrele(ipif); 28555 if (ire != NULL) { 28556 ire_refrele(ire); 28557 return (B_TRUE); 28558 } else { 28559 return (B_FALSE); 28560 } 28561 } 28562 28563 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 28564 connp->conn_zoneid == zoneid) { 28565 /* 28566 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 28567 * disabled, therefore we don't dispatch the multicast packet to 28568 * the sending zone. 28569 */ 28570 return (B_FALSE); 28571 } 28572 28573 if (IS_LOOPBACK(ill) && connp->conn_zoneid != zoneid) { 28574 /* 28575 * Multicast packet on the loopback interface: we only match 28576 * conns who joined the group in the specified zone. 28577 */ 28578 return (B_FALSE); 28579 } 28580 28581 if (connp->conn_multi_router) { 28582 /* multicast packet and multicast router socket: send up */ 28583 return (B_TRUE); 28584 } 28585 28586 mutex_enter(&connp->conn_lock); 28587 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 28588 mutex_exit(&connp->conn_lock); 28589 return (found); 28590 } 28591 28592 /* 28593 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 28594 */ 28595 /* ARGSUSED */ 28596 static void 28597 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 28598 { 28599 ill_t *ill = (ill_t *)q->q_ptr; 28600 mblk_t *mp1, *mp2; 28601 ipif_t *ipif; 28602 int err = 0; 28603 conn_t *connp = NULL; 28604 ipsq_t *ipsq; 28605 arc_t *arc; 28606 28607 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 28608 28609 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 28610 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 28611 28612 ASSERT(IAM_WRITER_ILL(ill)); 28613 mp2 = mp->b_cont; 28614 mp->b_cont = NULL; 28615 28616 /* 28617 * We have now received the arp bringup completion message 28618 * from ARP. Mark the arp bringup as done. Also if the arp 28619 * stream has already started closing, send up the AR_ARP_CLOSING 28620 * ack now since ARP is waiting in close for this ack. 28621 */ 28622 mutex_enter(&ill->ill_lock); 28623 ill->ill_arp_bringup_pending = 0; 28624 if (ill->ill_arp_closing) { 28625 mutex_exit(&ill->ill_lock); 28626 /* Let's reuse the mp for sending the ack */ 28627 arc = (arc_t *)mp->b_rptr; 28628 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 28629 arc->arc_cmd = AR_ARP_CLOSING; 28630 qreply(q, mp); 28631 } else { 28632 mutex_exit(&ill->ill_lock); 28633 freeb(mp); 28634 } 28635 28636 ipsq = ill->ill_phyint->phyint_ipsq; 28637 ipif = ipsq->ipsq_pending_ipif; 28638 mp1 = ipsq_pending_mp_get(ipsq, &connp); 28639 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 28640 if (mp1 == NULL) { 28641 /* bringup was aborted by the user */ 28642 freemsg(mp2); 28643 return; 28644 } 28645 28646 /* 28647 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we 28648 * must have an associated conn_t. Otherwise, we're bringing this 28649 * interface back up as part of handling an asynchronous event (e.g., 28650 * physical address change). 28651 */ 28652 if (ipsq->ipsq_current_ioctl != 0) { 28653 ASSERT(connp != NULL); 28654 q = CONNP_TO_WQ(connp); 28655 } else { 28656 ASSERT(connp == NULL); 28657 q = ill->ill_rq; 28658 } 28659 28660 /* 28661 * If the DL_BIND_REQ fails, it is noted 28662 * in arc_name_offset. 28663 */ 28664 err = *((int *)mp2->b_rptr); 28665 if (err == 0) { 28666 if (ipif->ipif_isv6) { 28667 if ((err = ipif_up_done_v6(ipif)) != 0) 28668 ip0dbg(("ip_arp_done: init failed\n")); 28669 } else { 28670 if ((err = ipif_up_done(ipif)) != 0) 28671 ip0dbg(("ip_arp_done: init failed\n")); 28672 } 28673 } else { 28674 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 28675 } 28676 28677 freemsg(mp2); 28678 28679 if ((err == 0) && (ill->ill_up_ipifs)) { 28680 err = ill_up_ipifs(ill, q, mp1); 28681 if (err == EINPROGRESS) 28682 return; 28683 } 28684 28685 if (ill->ill_up_ipifs) 28686 ill_group_cleanup(ill); 28687 28688 /* 28689 * The operation must complete without EINPROGRESS since 28690 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp. 28691 * Otherwise, the operation will be stuck forever in the ipsq. 28692 */ 28693 ASSERT(err != EINPROGRESS); 28694 if (ipsq->ipsq_current_ioctl != 0) 28695 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 28696 else 28697 ipsq_current_finish(ipsq); 28698 } 28699 28700 /* Allocate the private structure */ 28701 static int 28702 ip_priv_alloc(void **bufp) 28703 { 28704 void *buf; 28705 28706 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 28707 return (ENOMEM); 28708 28709 *bufp = buf; 28710 return (0); 28711 } 28712 28713 /* Function to delete the private structure */ 28714 void 28715 ip_priv_free(void *buf) 28716 { 28717 ASSERT(buf != NULL); 28718 kmem_free(buf, sizeof (ip_priv_t)); 28719 } 28720 28721 /* 28722 * The entry point for IPPF processing. 28723 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 28724 * routine just returns. 28725 * 28726 * When called, ip_process generates an ipp_packet_t structure 28727 * which holds the state information for this packet and invokes the 28728 * the classifier (via ipp_packet_process). The classification, depending on 28729 * configured filters, results in a list of actions for this packet. Invoking 28730 * an action may cause the packet to be dropped, in which case the resulting 28731 * mblk (*mpp) is NULL. proc indicates the callout position for 28732 * this packet and ill_index is the interface this packet on or will leave 28733 * on (inbound and outbound resp.). 28734 */ 28735 void 28736 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 28737 { 28738 mblk_t *mp; 28739 ip_priv_t *priv; 28740 ipp_action_id_t aid; 28741 int rc = 0; 28742 ipp_packet_t *pp; 28743 #define IP_CLASS "ip" 28744 28745 /* If the classifier is not loaded, return */ 28746 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 28747 return; 28748 } 28749 28750 mp = *mpp; 28751 ASSERT(mp != NULL); 28752 28753 /* Allocate the packet structure */ 28754 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 28755 if (rc != 0) { 28756 *mpp = NULL; 28757 freemsg(mp); 28758 return; 28759 } 28760 28761 /* Allocate the private structure */ 28762 rc = ip_priv_alloc((void **)&priv); 28763 if (rc != 0) { 28764 *mpp = NULL; 28765 freemsg(mp); 28766 ipp_packet_free(pp); 28767 return; 28768 } 28769 priv->proc = proc; 28770 priv->ill_index = ill_index; 28771 ipp_packet_set_private(pp, priv, ip_priv_free); 28772 ipp_packet_set_data(pp, mp); 28773 28774 /* Invoke the classifier */ 28775 rc = ipp_packet_process(&pp); 28776 if (pp != NULL) { 28777 mp = ipp_packet_get_data(pp); 28778 ipp_packet_free(pp); 28779 if (rc != 0) { 28780 freemsg(mp); 28781 *mpp = NULL; 28782 } 28783 } else { 28784 *mpp = NULL; 28785 } 28786 #undef IP_CLASS 28787 } 28788 28789 /* 28790 * Propagate a multicast group membership operation (add/drop) on 28791 * all the interfaces crossed by the related multirt routes. 28792 * The call is considered successful if the operation succeeds 28793 * on at least one interface. 28794 */ 28795 static int 28796 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 28797 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 28798 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 28799 mblk_t *first_mp) 28800 { 28801 ire_t *ire_gw; 28802 irb_t *irb; 28803 int error = 0; 28804 opt_restart_t *or; 28805 ip_stack_t *ipst = ire->ire_ipst; 28806 28807 irb = ire->ire_bucket; 28808 ASSERT(irb != NULL); 28809 28810 ASSERT(DB_TYPE(first_mp) == M_CTL); 28811 28812 or = (opt_restart_t *)first_mp->b_rptr; 28813 IRB_REFHOLD(irb); 28814 for (; ire != NULL; ire = ire->ire_next) { 28815 if ((ire->ire_flags & RTF_MULTIRT) == 0) 28816 continue; 28817 if (ire->ire_addr != group) 28818 continue; 28819 28820 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 28821 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 28822 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE, ipst); 28823 /* No resolver exists for the gateway; skip this ire. */ 28824 if (ire_gw == NULL) 28825 continue; 28826 28827 /* 28828 * This function can return EINPROGRESS. If so the operation 28829 * will be restarted from ip_restart_optmgmt which will 28830 * call ip_opt_set and option processing will restart for 28831 * this option. So we may end up calling 'fn' more than once. 28832 * This requires that 'fn' is idempotent except for the 28833 * return value. The operation is considered a success if 28834 * it succeeds at least once on any one interface. 28835 */ 28836 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 28837 NULL, fmode, src, first_mp); 28838 if (error == 0) 28839 or->or_private = CGTP_MCAST_SUCCESS; 28840 28841 if (ip_debug > 0) { 28842 ulong_t off; 28843 char *ksym; 28844 ksym = kobj_getsymname((uintptr_t)fn, &off); 28845 ip2dbg(("ip_multirt_apply_membership: " 28846 "called %s, multirt group 0x%08x via itf 0x%08x, " 28847 "error %d [success %u]\n", 28848 ksym ? ksym : "?", 28849 ntohl(group), ntohl(ire_gw->ire_src_addr), 28850 error, or->or_private)); 28851 } 28852 28853 ire_refrele(ire_gw); 28854 if (error == EINPROGRESS) { 28855 IRB_REFRELE(irb); 28856 return (error); 28857 } 28858 } 28859 IRB_REFRELE(irb); 28860 /* 28861 * Consider the call as successful if we succeeded on at least 28862 * one interface. Otherwise, return the last encountered error. 28863 */ 28864 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 28865 } 28866 28867 28868 /* 28869 * Issue a warning regarding a route crossing an interface with an 28870 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 28871 * amount of time is logged. 28872 */ 28873 static void 28874 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 28875 { 28876 hrtime_t current = gethrtime(); 28877 char buf[INET_ADDRSTRLEN]; 28878 ip_stack_t *ipst = ire->ire_ipst; 28879 28880 /* Convert interval in ms to hrtime in ns */ 28881 if (ipst->ips_multirt_bad_mtu_last_time + 28882 ((hrtime_t)ipst->ips_ip_multirt_log_interval * (hrtime_t)1000000) <= 28883 current) { 28884 cmn_err(CE_WARN, "ip: ignoring multiroute " 28885 "to %s, incorrect MTU %u (expected %u)\n", 28886 ip_dot_addr(ire->ire_addr, buf), 28887 ire->ire_max_frag, max_frag); 28888 28889 ipst->ips_multirt_bad_mtu_last_time = current; 28890 } 28891 } 28892 28893 28894 /* 28895 * Get the CGTP (multirouting) filtering status. 28896 * If 0, the CGTP hooks are transparent. 28897 */ 28898 /* ARGSUSED */ 28899 static int 28900 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 28901 { 28902 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28903 28904 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 28905 return (0); 28906 } 28907 28908 28909 /* 28910 * Set the CGTP (multirouting) filtering status. 28911 * If the status is changed from active to transparent 28912 * or from transparent to active, forward the new status 28913 * to the filtering module (if loaded). 28914 */ 28915 /* ARGSUSED */ 28916 static int 28917 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 28918 cred_t *ioc_cr) 28919 { 28920 long new_value; 28921 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28922 ip_stack_t *ipst = CONNQ_TO_IPST(q); 28923 28924 if (secpolicy_ip_config(ioc_cr, B_FALSE) != 0) 28925 return (EPERM); 28926 28927 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 28928 new_value < 0 || new_value > 1) { 28929 return (EINVAL); 28930 } 28931 28932 if ((!*ip_cgtp_filter_value) && new_value) { 28933 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 28934 ipst->ips_ip_cgtp_filter_ops == NULL ? 28935 " (module not loaded)" : ""); 28936 } 28937 if (*ip_cgtp_filter_value && (!new_value)) { 28938 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 28939 ipst->ips_ip_cgtp_filter_ops == NULL ? 28940 " (module not loaded)" : ""); 28941 } 28942 28943 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 28944 int res; 28945 netstackid_t stackid; 28946 28947 stackid = ipst->ips_netstack->netstack_stackid; 28948 res = ipst->ips_ip_cgtp_filter_ops->cfo_change_state(stackid, 28949 new_value); 28950 if (res) 28951 return (res); 28952 } 28953 28954 *ip_cgtp_filter_value = (boolean_t)new_value; 28955 28956 return (0); 28957 } 28958 28959 28960 /* 28961 * Return the expected CGTP hooks version number. 28962 */ 28963 int 28964 ip_cgtp_filter_supported(void) 28965 { 28966 return (ip_cgtp_filter_rev); 28967 } 28968 28969 28970 /* 28971 * CGTP hooks can be registered by invoking this function. 28972 * Checks that the version number matches. 28973 */ 28974 int 28975 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops) 28976 { 28977 netstack_t *ns; 28978 ip_stack_t *ipst; 28979 28980 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 28981 return (ENOTSUP); 28982 28983 ns = netstack_find_by_stackid(stackid); 28984 if (ns == NULL) 28985 return (EINVAL); 28986 ipst = ns->netstack_ip; 28987 ASSERT(ipst != NULL); 28988 28989 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 28990 netstack_rele(ns); 28991 return (EALREADY); 28992 } 28993 28994 ipst->ips_ip_cgtp_filter_ops = ops; 28995 netstack_rele(ns); 28996 return (0); 28997 } 28998 28999 /* 29000 * CGTP hooks can be unregistered by invoking this function. 29001 * Returns ENXIO if there was no registration. 29002 * Returns EBUSY if the ndd variable has not been turned off. 29003 */ 29004 int 29005 ip_cgtp_filter_unregister(netstackid_t stackid) 29006 { 29007 netstack_t *ns; 29008 ip_stack_t *ipst; 29009 29010 ns = netstack_find_by_stackid(stackid); 29011 if (ns == NULL) 29012 return (EINVAL); 29013 ipst = ns->netstack_ip; 29014 ASSERT(ipst != NULL); 29015 29016 if (ipst->ips_ip_cgtp_filter) { 29017 netstack_rele(ns); 29018 return (EBUSY); 29019 } 29020 29021 if (ipst->ips_ip_cgtp_filter_ops == NULL) { 29022 netstack_rele(ns); 29023 return (ENXIO); 29024 } 29025 ipst->ips_ip_cgtp_filter_ops = NULL; 29026 netstack_rele(ns); 29027 return (0); 29028 } 29029 29030 /* 29031 * Check whether there is a CGTP filter registration. 29032 * Returns non-zero if there is a registration, otherwise returns zero. 29033 * Note: returns zero if bad stackid. 29034 */ 29035 int 29036 ip_cgtp_filter_is_registered(netstackid_t stackid) 29037 { 29038 netstack_t *ns; 29039 ip_stack_t *ipst; 29040 int ret; 29041 29042 ns = netstack_find_by_stackid(stackid); 29043 if (ns == NULL) 29044 return (0); 29045 ipst = ns->netstack_ip; 29046 ASSERT(ipst != NULL); 29047 29048 if (ipst->ips_ip_cgtp_filter_ops != NULL) 29049 ret = 1; 29050 else 29051 ret = 0; 29052 29053 netstack_rele(ns); 29054 return (ret); 29055 } 29056 29057 static squeue_func_t 29058 ip_squeue_switch(int val) 29059 { 29060 squeue_func_t rval = squeue_fill; 29061 29062 switch (val) { 29063 case IP_SQUEUE_ENTER_NODRAIN: 29064 rval = squeue_enter_nodrain; 29065 break; 29066 case IP_SQUEUE_ENTER: 29067 rval = squeue_enter; 29068 break; 29069 default: 29070 break; 29071 } 29072 return (rval); 29073 } 29074 29075 /* ARGSUSED */ 29076 static int 29077 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 29078 caddr_t addr, cred_t *cr) 29079 { 29080 int *v = (int *)addr; 29081 long new_value; 29082 29083 if (secpolicy_net_config(cr, B_FALSE) != 0) 29084 return (EPERM); 29085 29086 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29087 return (EINVAL); 29088 29089 ip_input_proc = ip_squeue_switch(new_value); 29090 *v = new_value; 29091 return (0); 29092 } 29093 29094 /* 29095 * Handle ndd set of variables which require PRIV_SYS_NET_CONFIG such as 29096 * ip_debug. 29097 */ 29098 /* ARGSUSED */ 29099 static int 29100 ip_int_set(queue_t *q, mblk_t *mp, char *value, 29101 caddr_t addr, cred_t *cr) 29102 { 29103 int *v = (int *)addr; 29104 long new_value; 29105 29106 if (secpolicy_net_config(cr, B_FALSE) != 0) 29107 return (EPERM); 29108 29109 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29110 return (EINVAL); 29111 29112 *v = new_value; 29113 return (0); 29114 } 29115 29116 /* 29117 * Handle changes to ipmp_hook_emulation ndd variable. 29118 * Need to update phyint_hook_ifindex. 29119 * Also generate a nic plumb event should a new ifidex be assigned to a group. 29120 */ 29121 static void 29122 ipmp_hook_emulation_changed(ip_stack_t *ipst) 29123 { 29124 phyint_t *phyi; 29125 phyint_t *phyi_tmp; 29126 char *groupname; 29127 int namelen; 29128 ill_t *ill; 29129 boolean_t new_group; 29130 29131 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 29132 /* 29133 * Group indicies are stored in the phyint - a common structure 29134 * to both IPv4 and IPv6. 29135 */ 29136 phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index); 29137 for (; phyi != NULL; 29138 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 29139 phyi, AVL_AFTER)) { 29140 /* Ignore the ones that do not have a group */ 29141 if (phyi->phyint_groupname_len == 0) 29142 continue; 29143 29144 /* 29145 * Look for other phyint in group. 29146 * Clear name/namelen so the lookup doesn't find ourselves. 29147 */ 29148 namelen = phyi->phyint_groupname_len; 29149 groupname = phyi->phyint_groupname; 29150 phyi->phyint_groupname_len = 0; 29151 phyi->phyint_groupname = NULL; 29152 29153 phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst); 29154 /* Restore */ 29155 phyi->phyint_groupname_len = namelen; 29156 phyi->phyint_groupname = groupname; 29157 29158 new_group = B_FALSE; 29159 if (ipst->ips_ipmp_hook_emulation) { 29160 /* 29161 * If the group already exists and has already 29162 * been assigned a group ifindex, we use the existing 29163 * group_ifindex, otherwise we pick a new group_ifindex 29164 * here. 29165 */ 29166 if (phyi_tmp != NULL && 29167 phyi_tmp->phyint_group_ifindex != 0) { 29168 phyi->phyint_group_ifindex = 29169 phyi_tmp->phyint_group_ifindex; 29170 } else { 29171 /* XXX We need a recovery strategy here. */ 29172 if (!ip_assign_ifindex( 29173 &phyi->phyint_group_ifindex, ipst)) 29174 cmn_err(CE_PANIC, 29175 "ip_assign_ifindex() failed"); 29176 new_group = B_TRUE; 29177 } 29178 } else { 29179 phyi->phyint_group_ifindex = 0; 29180 } 29181 if (ipst->ips_ipmp_hook_emulation) 29182 phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex; 29183 else 29184 phyi->phyint_hook_ifindex = phyi->phyint_ifindex; 29185 29186 /* 29187 * For IP Filter to find out the relationship between 29188 * names and interface indicies, we need to generate 29189 * a NE_PLUMB event when a new group can appear. 29190 * We always generate events when a new interface appears 29191 * (even when ipmp_hook_emulation is set) so there 29192 * is no need to generate NE_PLUMB events when 29193 * ipmp_hook_emulation is turned off. 29194 * And since it isn't critical for IP Filter to get 29195 * the NE_UNPLUMB events we skip those here. 29196 */ 29197 if (new_group) { 29198 /* 29199 * First phyint in group - generate group PLUMB event. 29200 * Since we are not running inside the ipsq we do 29201 * the dispatch immediately. 29202 */ 29203 if (phyi->phyint_illv4 != NULL) 29204 ill = phyi->phyint_illv4; 29205 else 29206 ill = phyi->phyint_illv6; 29207 29208 if (ill != NULL) { 29209 mutex_enter(&ill->ill_lock); 29210 ill_nic_info_plumb(ill, B_TRUE); 29211 ill_nic_info_dispatch(ill); 29212 mutex_exit(&ill->ill_lock); 29213 } 29214 } 29215 } 29216 rw_exit(&ipst->ips_ill_g_lock); 29217 } 29218 29219 /* ARGSUSED */ 29220 static int 29221 ipmp_hook_emulation_set(queue_t *q, mblk_t *mp, char *value, 29222 caddr_t addr, cred_t *cr) 29223 { 29224 int *v = (int *)addr; 29225 long new_value; 29226 ip_stack_t *ipst = CONNQ_TO_IPST(q); 29227 29228 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29229 return (EINVAL); 29230 29231 if (*v != new_value) { 29232 *v = new_value; 29233 ipmp_hook_emulation_changed(ipst); 29234 } 29235 return (0); 29236 } 29237 29238 static void * 29239 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp) 29240 { 29241 kstat_t *ksp; 29242 29243 ip_stat_t template = { 29244 { "ipsec_fanout_proto", KSTAT_DATA_UINT64 }, 29245 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 29246 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 29247 { "ip_udp_fanothers", KSTAT_DATA_UINT64 }, 29248 { "ip_udp_fast_path", KSTAT_DATA_UINT64 }, 29249 { "ip_udp_slow_path", KSTAT_DATA_UINT64 }, 29250 { "ip_udp_input_err", KSTAT_DATA_UINT64 }, 29251 { "ip_tcppullup", KSTAT_DATA_UINT64 }, 29252 { "ip_tcpoptions", KSTAT_DATA_UINT64 }, 29253 { "ip_multipkttcp", KSTAT_DATA_UINT64 }, 29254 { "ip_tcp_fast_path", KSTAT_DATA_UINT64 }, 29255 { "ip_tcp_slow_path", KSTAT_DATA_UINT64 }, 29256 { "ip_tcp_input_error", KSTAT_DATA_UINT64 }, 29257 { "ip_db_ref", KSTAT_DATA_UINT64 }, 29258 { "ip_notaligned1", KSTAT_DATA_UINT64 }, 29259 { "ip_notaligned2", KSTAT_DATA_UINT64 }, 29260 { "ip_multimblk3", KSTAT_DATA_UINT64 }, 29261 { "ip_multimblk4", KSTAT_DATA_UINT64 }, 29262 { "ip_ipoptions", KSTAT_DATA_UINT64 }, 29263 { "ip_classify_fail", KSTAT_DATA_UINT64 }, 29264 { "ip_opt", KSTAT_DATA_UINT64 }, 29265 { "ip_udp_rput_local", KSTAT_DATA_UINT64 }, 29266 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 29267 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 29268 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 29269 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 29270 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 29271 { "ip_trash_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 29272 { "ip_trash_ire_reclaim_success", KSTAT_DATA_UINT64 }, 29273 { "ip_ire_arp_timer_expired", KSTAT_DATA_UINT64 }, 29274 { "ip_ire_redirect_timer_expired", KSTAT_DATA_UINT64 }, 29275 { "ip_ire_pmtu_timer_expired", KSTAT_DATA_UINT64 }, 29276 { "ip_input_multi_squeue", KSTAT_DATA_UINT64 }, 29277 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29278 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29279 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29280 { "ip_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29281 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29282 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29283 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29284 { "ip_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29285 { "ip_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 29286 { "ip_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 29287 { "ip_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 29288 { "ip_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 29289 { "ip_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 29290 }; 29291 29292 ksp = kstat_create_netstack("ip", 0, "ipstat", "net", 29293 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 29294 KSTAT_FLAG_VIRTUAL, stackid); 29295 29296 if (ksp == NULL) 29297 return (NULL); 29298 29299 bcopy(&template, ip_statisticsp, sizeof (template)); 29300 ksp->ks_data = (void *)ip_statisticsp; 29301 ksp->ks_private = (void *)(uintptr_t)stackid; 29302 29303 kstat_install(ksp); 29304 return (ksp); 29305 } 29306 29307 static void 29308 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 29309 { 29310 if (ksp != NULL) { 29311 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29312 kstat_delete_netstack(ksp, stackid); 29313 } 29314 } 29315 29316 static void * 29317 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst) 29318 { 29319 kstat_t *ksp; 29320 29321 ip_named_kstat_t template = { 29322 { "forwarding", KSTAT_DATA_UINT32, 0 }, 29323 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 29324 { "inReceives", KSTAT_DATA_UINT64, 0 }, 29325 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 29326 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 29327 { "forwDatagrams", KSTAT_DATA_UINT64, 0 }, 29328 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 29329 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 29330 { "inDelivers", KSTAT_DATA_UINT64, 0 }, 29331 { "outRequests", KSTAT_DATA_UINT64, 0 }, 29332 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 29333 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 29334 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 29335 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 29336 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 29337 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 29338 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 29339 { "fragFails", KSTAT_DATA_UINT32, 0 }, 29340 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 29341 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 29342 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 29343 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 29344 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 29345 { "inErrs", KSTAT_DATA_UINT32, 0 }, 29346 { "noPorts", KSTAT_DATA_UINT32, 0 }, 29347 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 29348 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 29349 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 29350 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 29351 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 29352 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 29353 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 29354 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 29355 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 29356 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 29357 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 29358 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 29359 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 29360 }; 29361 29362 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 29363 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid); 29364 if (ksp == NULL || ksp->ks_data == NULL) 29365 return (NULL); 29366 29367 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2; 29368 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl; 29369 template.reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29370 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 29371 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 29372 29373 template.netToMediaEntrySize.value.i32 = 29374 sizeof (mib2_ipNetToMediaEntry_t); 29375 29376 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 29377 29378 bcopy(&template, ksp->ks_data, sizeof (template)); 29379 ksp->ks_update = ip_kstat_update; 29380 ksp->ks_private = (void *)(uintptr_t)stackid; 29381 29382 kstat_install(ksp); 29383 return (ksp); 29384 } 29385 29386 static void 29387 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29388 { 29389 if (ksp != NULL) { 29390 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29391 kstat_delete_netstack(ksp, stackid); 29392 } 29393 } 29394 29395 static int 29396 ip_kstat_update(kstat_t *kp, int rw) 29397 { 29398 ip_named_kstat_t *ipkp; 29399 mib2_ipIfStatsEntry_t ipmib; 29400 ill_walk_context_t ctx; 29401 ill_t *ill; 29402 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29403 netstack_t *ns; 29404 ip_stack_t *ipst; 29405 29406 if (kp == NULL || kp->ks_data == NULL) 29407 return (EIO); 29408 29409 if (rw == KSTAT_WRITE) 29410 return (EACCES); 29411 29412 ns = netstack_find_by_stackid(stackid); 29413 if (ns == NULL) 29414 return (-1); 29415 ipst = ns->netstack_ip; 29416 if (ipst == NULL) { 29417 netstack_rele(ns); 29418 return (-1); 29419 } 29420 ipkp = (ip_named_kstat_t *)kp->ks_data; 29421 29422 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib)); 29423 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 29424 ill = ILL_START_WALK_V4(&ctx, ipst); 29425 for (; ill != NULL; ill = ill_next(&ctx, ill)) 29426 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib); 29427 rw_exit(&ipst->ips_ill_g_lock); 29428 29429 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding; 29430 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL; 29431 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives; 29432 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors; 29433 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors; 29434 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams; 29435 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos; 29436 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards; 29437 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers; 29438 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests; 29439 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards; 29440 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes; 29441 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29442 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds; 29443 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs; 29444 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails; 29445 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs; 29446 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails; 29447 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates; 29448 29449 ipkp->routingDiscards.value.ui32 = 0; 29450 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs; 29451 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts; 29452 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs; 29453 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates; 29454 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups; 29455 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits; 29456 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs; 29457 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows; 29458 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows; 29459 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded; 29460 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed; 29461 29462 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion; 29463 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion; 29464 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion; 29465 29466 netstack_rele(ns); 29467 29468 return (0); 29469 } 29470 29471 static void * 29472 icmp_kstat_init(netstackid_t stackid) 29473 { 29474 kstat_t *ksp; 29475 29476 icmp_named_kstat_t template = { 29477 { "inMsgs", KSTAT_DATA_UINT32 }, 29478 { "inErrors", KSTAT_DATA_UINT32 }, 29479 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 29480 { "inTimeExcds", KSTAT_DATA_UINT32 }, 29481 { "inParmProbs", KSTAT_DATA_UINT32 }, 29482 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 29483 { "inRedirects", KSTAT_DATA_UINT32 }, 29484 { "inEchos", KSTAT_DATA_UINT32 }, 29485 { "inEchoReps", KSTAT_DATA_UINT32 }, 29486 { "inTimestamps", KSTAT_DATA_UINT32 }, 29487 { "inTimestampReps", KSTAT_DATA_UINT32 }, 29488 { "inAddrMasks", KSTAT_DATA_UINT32 }, 29489 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 29490 { "outMsgs", KSTAT_DATA_UINT32 }, 29491 { "outErrors", KSTAT_DATA_UINT32 }, 29492 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 29493 { "outTimeExcds", KSTAT_DATA_UINT32 }, 29494 { "outParmProbs", KSTAT_DATA_UINT32 }, 29495 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 29496 { "outRedirects", KSTAT_DATA_UINT32 }, 29497 { "outEchos", KSTAT_DATA_UINT32 }, 29498 { "outEchoReps", KSTAT_DATA_UINT32 }, 29499 { "outTimestamps", KSTAT_DATA_UINT32 }, 29500 { "outTimestampReps", KSTAT_DATA_UINT32 }, 29501 { "outAddrMasks", KSTAT_DATA_UINT32 }, 29502 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 29503 { "inChksumErrs", KSTAT_DATA_UINT32 }, 29504 { "inUnknowns", KSTAT_DATA_UINT32 }, 29505 { "inFragNeeded", KSTAT_DATA_UINT32 }, 29506 { "outFragNeeded", KSTAT_DATA_UINT32 }, 29507 { "outDrops", KSTAT_DATA_UINT32 }, 29508 { "inOverFlows", KSTAT_DATA_UINT32 }, 29509 { "inBadRedirects", KSTAT_DATA_UINT32 }, 29510 }; 29511 29512 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 29513 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid); 29514 if (ksp == NULL || ksp->ks_data == NULL) 29515 return (NULL); 29516 29517 bcopy(&template, ksp->ks_data, sizeof (template)); 29518 29519 ksp->ks_update = icmp_kstat_update; 29520 ksp->ks_private = (void *)(uintptr_t)stackid; 29521 29522 kstat_install(ksp); 29523 return (ksp); 29524 } 29525 29526 static void 29527 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29528 { 29529 if (ksp != NULL) { 29530 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29531 kstat_delete_netstack(ksp, stackid); 29532 } 29533 } 29534 29535 static int 29536 icmp_kstat_update(kstat_t *kp, int rw) 29537 { 29538 icmp_named_kstat_t *icmpkp; 29539 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29540 netstack_t *ns; 29541 ip_stack_t *ipst; 29542 29543 if ((kp == NULL) || (kp->ks_data == NULL)) 29544 return (EIO); 29545 29546 if (rw == KSTAT_WRITE) 29547 return (EACCES); 29548 29549 ns = netstack_find_by_stackid(stackid); 29550 if (ns == NULL) 29551 return (-1); 29552 ipst = ns->netstack_ip; 29553 if (ipst == NULL) { 29554 netstack_rele(ns); 29555 return (-1); 29556 } 29557 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 29558 29559 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs; 29560 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors; 29561 icmpkp->inDestUnreachs.value.ui32 = 29562 ipst->ips_icmp_mib.icmpInDestUnreachs; 29563 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds; 29564 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs; 29565 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs; 29566 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects; 29567 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos; 29568 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps; 29569 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps; 29570 icmpkp->inTimestampReps.value.ui32 = 29571 ipst->ips_icmp_mib.icmpInTimestampReps; 29572 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks; 29573 icmpkp->inAddrMaskReps.value.ui32 = 29574 ipst->ips_icmp_mib.icmpInAddrMaskReps; 29575 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs; 29576 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors; 29577 icmpkp->outDestUnreachs.value.ui32 = 29578 ipst->ips_icmp_mib.icmpOutDestUnreachs; 29579 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds; 29580 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs; 29581 icmpkp->outSrcQuenchs.value.ui32 = 29582 ipst->ips_icmp_mib.icmpOutSrcQuenchs; 29583 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects; 29584 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos; 29585 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps; 29586 icmpkp->outTimestamps.value.ui32 = 29587 ipst->ips_icmp_mib.icmpOutTimestamps; 29588 icmpkp->outTimestampReps.value.ui32 = 29589 ipst->ips_icmp_mib.icmpOutTimestampReps; 29590 icmpkp->outAddrMasks.value.ui32 = 29591 ipst->ips_icmp_mib.icmpOutAddrMasks; 29592 icmpkp->outAddrMaskReps.value.ui32 = 29593 ipst->ips_icmp_mib.icmpOutAddrMaskReps; 29594 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs; 29595 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns; 29596 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded; 29597 icmpkp->outFragNeeded.value.ui32 = 29598 ipst->ips_icmp_mib.icmpOutFragNeeded; 29599 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops; 29600 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows; 29601 icmpkp->inBadRedirects.value.ui32 = 29602 ipst->ips_icmp_mib.icmpInBadRedirects; 29603 29604 netstack_rele(ns); 29605 return (0); 29606 } 29607 29608 /* 29609 * This is the fanout function for raw socket opened for SCTP. Note 29610 * that it is called after SCTP checks that there is no socket which 29611 * wants a packet. Then before SCTP handles this out of the blue packet, 29612 * this function is called to see if there is any raw socket for SCTP. 29613 * If there is and it is bound to the correct address, the packet will 29614 * be sent to that socket. Note that only one raw socket can be bound to 29615 * a port. This is assured in ipcl_sctp_hash_insert(); 29616 */ 29617 void 29618 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 29619 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 29620 zoneid_t zoneid) 29621 { 29622 conn_t *connp; 29623 queue_t *rq; 29624 mblk_t *first_mp; 29625 boolean_t secure; 29626 ip6_t *ip6h; 29627 ip_stack_t *ipst = recv_ill->ill_ipst; 29628 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 29629 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp; 29630 boolean_t sctp_csum_err = B_FALSE; 29631 29632 if (flags & IP_FF_SCTP_CSUM_ERR) { 29633 sctp_csum_err = B_TRUE; 29634 flags &= ~IP_FF_SCTP_CSUM_ERR; 29635 } 29636 29637 first_mp = mp; 29638 if (mctl_present) { 29639 mp = first_mp->b_cont; 29640 secure = ipsec_in_is_secure(first_mp); 29641 ASSERT(mp != NULL); 29642 } else { 29643 secure = B_FALSE; 29644 } 29645 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 29646 29647 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha, ipst); 29648 if (connp == NULL) { 29649 /* 29650 * Although raw sctp is not summed, OOB chunks must be. 29651 * Drop the packet here if the sctp checksum failed. 29652 */ 29653 if (sctp_csum_err) { 29654 BUMP_MIB(&sctps->sctps_mib, sctpChecksumError); 29655 freemsg(first_mp); 29656 return; 29657 } 29658 sctp_ootb_input(first_mp, recv_ill, zoneid, mctl_present); 29659 return; 29660 } 29661 rq = connp->conn_rq; 29662 if (!canputnext(rq)) { 29663 CONN_DEC_REF(connp); 29664 BUMP_MIB(recv_ill->ill_ip_mib, rawipIfStatsInOverflows); 29665 freemsg(first_mp); 29666 return; 29667 } 29668 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 29669 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || secure) { 29670 first_mp = ipsec_check_inbound_policy(first_mp, connp, 29671 (isv4 ? ipha : NULL), ip6h, mctl_present); 29672 if (first_mp == NULL) { 29673 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 29674 CONN_DEC_REF(connp); 29675 return; 29676 } 29677 } 29678 /* 29679 * We probably should not send M_CTL message up to 29680 * raw socket. 29681 */ 29682 if (mctl_present) 29683 freeb(first_mp); 29684 29685 /* Initiate IPPF processing here if needed. */ 29686 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) || 29687 (!isv4 && IP6_IN_IPP(flags, ipst))) { 29688 ip_process(IPP_LOCAL_IN, &mp, 29689 recv_ill->ill_phyint->phyint_ifindex); 29690 if (mp == NULL) { 29691 CONN_DEC_REF(connp); 29692 return; 29693 } 29694 } 29695 29696 if (connp->conn_recvif || connp->conn_recvslla || 29697 ((connp->conn_ip_recvpktinfo || 29698 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 29699 (flags & IP_FF_IPINFO))) { 29700 int in_flags = 0; 29701 29702 /* 29703 * Since sctp does not support IP_RECVPKTINFO for v4, only pass 29704 * IPF_RECVIF. 29705 */ 29706 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 29707 in_flags = IPF_RECVIF; 29708 } 29709 if (connp->conn_recvslla) { 29710 in_flags |= IPF_RECVSLLA; 29711 } 29712 if (isv4) { 29713 mp = ip_add_info(mp, recv_ill, in_flags, 29714 IPCL_ZONEID(connp), ipst); 29715 } else { 29716 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 29717 if (mp == NULL) { 29718 BUMP_MIB(recv_ill->ill_ip_mib, 29719 ipIfStatsInDiscards); 29720 CONN_DEC_REF(connp); 29721 return; 29722 } 29723 } 29724 } 29725 29726 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 29727 /* 29728 * We are sending the IPSEC_IN message also up. Refer 29729 * to comments above this function. 29730 * This is the SOCK_RAW, IPPROTO_SCTP case. 29731 */ 29732 (connp->conn_recv)(connp, mp, NULL); 29733 CONN_DEC_REF(connp); 29734 } 29735 29736 #define UPDATE_IP_MIB_OB_COUNTERS(ill, len) \ 29737 { \ 29738 BUMP_MIB((ill)->ill_ip_mib, ipIfStatsHCOutTransmits); \ 29739 UPDATE_MIB((ill)->ill_ip_mib, ipIfStatsHCOutOctets, (len)); \ 29740 } 29741 /* 29742 * This function should be called only if all packet processing 29743 * including fragmentation is complete. Callers of this function 29744 * must set mp->b_prev to one of these values: 29745 * {0, IPP_FWD_OUT, IPP_LOCAL_OUT} 29746 * prior to handing over the mp as first argument to this function. 29747 * 29748 * If the ire passed by caller is incomplete, this function 29749 * queues the packet and if necessary, sends ARP request and bails. 29750 * If the ire passed is fully resolved, we simply prepend 29751 * the link-layer header to the packet, do ipsec hw acceleration 29752 * work if necessary, and send the packet out on the wire. 29753 * 29754 * NOTE: IPsec will only call this function with fully resolved 29755 * ires if hw acceleration is involved. 29756 * TODO list : 29757 * a Handle M_MULTIDATA so that 29758 * tcp_multisend->tcp_multisend_data can 29759 * call ip_xmit_v4 directly 29760 * b Handle post-ARP work for fragments so that 29761 * ip_wput_frag can call this function. 29762 */ 29763 ipxmit_state_t 29764 ip_xmit_v4(mblk_t *mp, ire_t *ire, ipsec_out_t *io, boolean_t flow_ctl_enabled) 29765 { 29766 nce_t *arpce; 29767 queue_t *q; 29768 int ill_index; 29769 mblk_t *nxt_mp, *first_mp; 29770 boolean_t xmit_drop = B_FALSE; 29771 ip_proc_t proc; 29772 ill_t *out_ill; 29773 int pkt_len; 29774 29775 arpce = ire->ire_nce; 29776 ASSERT(arpce != NULL); 29777 29778 DTRACE_PROBE2(ip__xmit__v4, ire_t *, ire, nce_t *, arpce); 29779 29780 mutex_enter(&arpce->nce_lock); 29781 switch (arpce->nce_state) { 29782 case ND_REACHABLE: 29783 /* If there are other queued packets, queue this packet */ 29784 if (arpce->nce_qd_mp != NULL) { 29785 if (mp != NULL) 29786 nce_queue_mp_common(arpce, mp, B_FALSE); 29787 mp = arpce->nce_qd_mp; 29788 } 29789 arpce->nce_qd_mp = NULL; 29790 mutex_exit(&arpce->nce_lock); 29791 29792 /* 29793 * Flush the queue. In the common case, where the 29794 * ARP is already resolved, it will go through the 29795 * while loop only once. 29796 */ 29797 while (mp != NULL) { 29798 29799 nxt_mp = mp->b_next; 29800 mp->b_next = NULL; 29801 ASSERT(mp->b_datap->db_type != M_CTL); 29802 pkt_len = ntohs(((ipha_t *)mp->b_rptr)->ipha_length); 29803 /* 29804 * This info is needed for IPQOS to do COS marking 29805 * in ip_wput_attach_llhdr->ip_process. 29806 */ 29807 proc = (ip_proc_t)(uintptr_t)mp->b_prev; 29808 mp->b_prev = NULL; 29809 29810 /* set up ill index for outbound qos processing */ 29811 out_ill = ire_to_ill(ire); 29812 ill_index = out_ill->ill_phyint->phyint_ifindex; 29813 first_mp = ip_wput_attach_llhdr(mp, ire, proc, 29814 ill_index); 29815 if (first_mp == NULL) { 29816 xmit_drop = B_TRUE; 29817 BUMP_MIB(out_ill->ill_ip_mib, 29818 ipIfStatsOutDiscards); 29819 goto next_mp; 29820 } 29821 /* non-ipsec hw accel case */ 29822 if (io == NULL || !io->ipsec_out_accelerated) { 29823 /* send it */ 29824 q = ire->ire_stq; 29825 if (proc == IPP_FWD_OUT) { 29826 UPDATE_IB_PKT_COUNT(ire); 29827 } else { 29828 UPDATE_OB_PKT_COUNT(ire); 29829 } 29830 ire->ire_last_used_time = lbolt; 29831 29832 if (flow_ctl_enabled || canputnext(q)) { 29833 if (proc == IPP_FWD_OUT) { 29834 29835 BUMP_MIB(out_ill->ill_ip_mib, 29836 ipIfStatsHCOutForwDatagrams); 29837 29838 } 29839 UPDATE_IP_MIB_OB_COUNTERS(out_ill, 29840 pkt_len); 29841 29842 putnext(q, first_mp); 29843 } else { 29844 BUMP_MIB(out_ill->ill_ip_mib, 29845 ipIfStatsOutDiscards); 29846 xmit_drop = B_TRUE; 29847 freemsg(first_mp); 29848 } 29849 } else { 29850 /* 29851 * Safety Pup says: make sure this 29852 * is going to the right interface! 29853 */ 29854 ill_t *ill1 = 29855 (ill_t *)ire->ire_stq->q_ptr; 29856 int ifindex = 29857 ill1->ill_phyint->phyint_ifindex; 29858 if (ifindex != 29859 io->ipsec_out_capab_ill_index) { 29860 xmit_drop = B_TRUE; 29861 freemsg(mp); 29862 } else { 29863 UPDATE_IP_MIB_OB_COUNTERS(ill1, 29864 pkt_len); 29865 ipsec_hw_putnext(ire->ire_stq, mp); 29866 } 29867 } 29868 next_mp: 29869 mp = nxt_mp; 29870 } /* while (mp != NULL) */ 29871 if (xmit_drop) 29872 return (SEND_FAILED); 29873 else 29874 return (SEND_PASSED); 29875 29876 case ND_INITIAL: 29877 case ND_INCOMPLETE: 29878 29879 /* 29880 * While we do send off packets to dests that 29881 * use fully-resolved CGTP routes, we do not 29882 * handle unresolved CGTP routes. 29883 */ 29884 ASSERT(!(ire->ire_flags & RTF_MULTIRT)); 29885 ASSERT(io == NULL || !io->ipsec_out_accelerated); 29886 29887 if (mp != NULL) { 29888 /* queue the packet */ 29889 nce_queue_mp_common(arpce, mp, B_FALSE); 29890 } 29891 29892 if (arpce->nce_state == ND_INCOMPLETE) { 29893 mutex_exit(&arpce->nce_lock); 29894 DTRACE_PROBE3(ip__xmit__incomplete, 29895 (ire_t *), ire, (mblk_t *), mp, 29896 (ipsec_out_t *), io); 29897 return (LOOKUP_IN_PROGRESS); 29898 } 29899 29900 arpce->nce_state = ND_INCOMPLETE; 29901 mutex_exit(&arpce->nce_lock); 29902 /* 29903 * Note that ire_add() (called from ire_forward()) 29904 * holds a ref on the ire until ARP is completed. 29905 */ 29906 29907 ire_arpresolve(ire, ire_to_ill(ire)); 29908 return (LOOKUP_IN_PROGRESS); 29909 default: 29910 ASSERT(0); 29911 mutex_exit(&arpce->nce_lock); 29912 return (LLHDR_RESLV_FAILED); 29913 } 29914 } 29915 29916 #undef UPDATE_IP_MIB_OB_COUNTERS 29917 29918 /* 29919 * Return B_TRUE if the buffers differ in length or content. 29920 * This is used for comparing extension header buffers. 29921 * Note that an extension header would be declared different 29922 * even if all that changed was the next header value in that header i.e. 29923 * what really changed is the next extension header. 29924 */ 29925 boolean_t 29926 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 29927 uint_t blen) 29928 { 29929 if (!b_valid) 29930 blen = 0; 29931 29932 if (alen != blen) 29933 return (B_TRUE); 29934 if (alen == 0) 29935 return (B_FALSE); /* Both zero length */ 29936 return (bcmp(abuf, bbuf, alen)); 29937 } 29938 29939 /* 29940 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 29941 * Return B_FALSE if memory allocation fails - don't change any state! 29942 */ 29943 boolean_t 29944 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29945 const void *src, uint_t srclen) 29946 { 29947 void *dst; 29948 29949 if (!src_valid) 29950 srclen = 0; 29951 29952 ASSERT(*dstlenp == 0); 29953 if (src != NULL && srclen != 0) { 29954 dst = mi_alloc(srclen, BPRI_MED); 29955 if (dst == NULL) 29956 return (B_FALSE); 29957 } else { 29958 dst = NULL; 29959 } 29960 if (*dstp != NULL) 29961 mi_free(*dstp); 29962 *dstp = dst; 29963 *dstlenp = dst == NULL ? 0 : srclen; 29964 return (B_TRUE); 29965 } 29966 29967 /* 29968 * Replace what is in *dst, *dstlen with the source. 29969 * Assumes ip_allocbuf has already been called. 29970 */ 29971 void 29972 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29973 const void *src, uint_t srclen) 29974 { 29975 if (!src_valid) 29976 srclen = 0; 29977 29978 ASSERT(*dstlenp == srclen); 29979 if (src != NULL && srclen != 0) 29980 bcopy(src, *dstp, srclen); 29981 } 29982 29983 /* 29984 * Free the storage pointed to by the members of an ip6_pkt_t. 29985 */ 29986 void 29987 ip6_pkt_free(ip6_pkt_t *ipp) 29988 { 29989 ASSERT(ipp->ipp_pathmtu == NULL && !(ipp->ipp_fields & IPPF_PATHMTU)); 29990 29991 if (ipp->ipp_fields & IPPF_HOPOPTS) { 29992 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 29993 ipp->ipp_hopopts = NULL; 29994 ipp->ipp_hopoptslen = 0; 29995 } 29996 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 29997 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 29998 ipp->ipp_rtdstopts = NULL; 29999 ipp->ipp_rtdstoptslen = 0; 30000 } 30001 if (ipp->ipp_fields & IPPF_DSTOPTS) { 30002 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 30003 ipp->ipp_dstopts = NULL; 30004 ipp->ipp_dstoptslen = 0; 30005 } 30006 if (ipp->ipp_fields & IPPF_RTHDR) { 30007 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 30008 ipp->ipp_rthdr = NULL; 30009 ipp->ipp_rthdrlen = 0; 30010 } 30011 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 30012 IPPF_RTHDR); 30013 } 30014