1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include <sys/types.h> 31 #include <sys/stream.h> 32 #include <sys/dlpi.h> 33 #include <sys/stropts.h> 34 #include <sys/sysmacros.h> 35 #include <sys/strsubr.h> 36 #include <sys/strlog.h> 37 #include <sys/strsun.h> 38 #include <sys/zone.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/xti_inet.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/cmn_err.h> 45 #include <sys/debug.h> 46 #include <sys/kobj.h> 47 #include <sys/modctl.h> 48 #include <sys/atomic.h> 49 #include <sys/policy.h> 50 #include <sys/priv.h> 51 52 #include <sys/systm.h> 53 #include <sys/param.h> 54 #include <sys/kmem.h> 55 #include <sys/sdt.h> 56 #include <sys/socket.h> 57 #include <sys/vtrace.h> 58 #include <sys/isa_defs.h> 59 #include <net/if.h> 60 #include <net/if_arp.h> 61 #include <net/route.h> 62 #include <sys/sockio.h> 63 #include <netinet/in.h> 64 #include <net/if_dl.h> 65 66 #include <inet/common.h> 67 #include <inet/mi.h> 68 #include <inet/mib2.h> 69 #include <inet/nd.h> 70 #include <inet/arp.h> 71 #include <inet/snmpcom.h> 72 #include <inet/kstatcom.h> 73 74 #include <netinet/igmp_var.h> 75 #include <netinet/ip6.h> 76 #include <netinet/icmp6.h> 77 #include <netinet/sctp.h> 78 79 #include <inet/ip.h> 80 #include <inet/ip_impl.h> 81 #include <inet/ip6.h> 82 #include <inet/ip6_asp.h> 83 #include <inet/tcp.h> 84 #include <inet/tcp_impl.h> 85 #include <inet/ip_multi.h> 86 #include <inet/ip_if.h> 87 #include <inet/ip_ire.h> 88 #include <inet/ip_ftable.h> 89 #include <inet/ip_rts.h> 90 #include <inet/optcom.h> 91 #include <inet/ip_ndp.h> 92 #include <inet/ip_listutils.h> 93 #include <netinet/igmp.h> 94 #include <netinet/ip_mroute.h> 95 #include <inet/ipp_common.h> 96 97 #include <net/pfkeyv2.h> 98 #include <inet/ipsec_info.h> 99 #include <inet/sadb.h> 100 #include <inet/ipsec_impl.h> 101 #include <sys/iphada.h> 102 #include <inet/tun.h> 103 #include <inet/ipdrop.h> 104 #include <inet/ip_netinfo.h> 105 106 #include <sys/ethernet.h> 107 #include <net/if_types.h> 108 #include <sys/cpuvar.h> 109 110 #include <ipp/ipp.h> 111 #include <ipp/ipp_impl.h> 112 #include <ipp/ipgpc/ipgpc.h> 113 114 #include <sys/multidata.h> 115 #include <sys/pattr.h> 116 117 #include <inet/ipclassifier.h> 118 #include <inet/sctp_ip.h> 119 #include <inet/sctp/sctp_impl.h> 120 #include <inet/udp_impl.h> 121 #include <sys/sunddi.h> 122 123 #include <sys/tsol/label.h> 124 #include <sys/tsol/tnet.h> 125 126 #include <rpc/pmap_prot.h> 127 128 /* 129 * Values for squeue switch: 130 * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain 131 * IP_SQUEUE_ENTER: squeue_enter 132 * IP_SQUEUE_FILL: squeue_fill 133 */ 134 int ip_squeue_enter = 2; /* Setable in /etc/system */ 135 136 squeue_func_t ip_input_proc; 137 #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x)) 138 139 #define TCP6 "tcp6" 140 #define TCP "tcp" 141 #define SCTP "sctp" 142 #define SCTP6 "sctp6" 143 144 major_t TCP6_MAJ; 145 major_t TCP_MAJ; 146 major_t SCTP_MAJ; 147 major_t SCTP6_MAJ; 148 149 /* 150 * Setable in /etc/system 151 */ 152 int ip_poll_normal_ms = 100; 153 int ip_poll_normal_ticks = 0; 154 int ip_modclose_ackwait_ms = 3000; 155 156 /* 157 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 158 */ 159 160 struct listptr_s { 161 mblk_t *lp_head; /* pointer to the head of the list */ 162 mblk_t *lp_tail; /* pointer to the tail of the list */ 163 }; 164 165 typedef struct listptr_s listptr_t; 166 167 /* 168 * This is used by ip_snmp_get_mib2_ip_route_media and 169 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 170 */ 171 typedef struct iproutedata_s { 172 uint_t ird_idx; 173 listptr_t ird_route; /* ipRouteEntryTable */ 174 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 175 listptr_t ird_attrs; /* ipRouteAttributeTable */ 176 } iproutedata_t; 177 178 /* 179 * Cluster specific hooks. These should be NULL when booted as a non-cluster 180 */ 181 182 /* 183 * Hook functions to enable cluster networking 184 * On non-clustered systems these vectors must always be NULL. 185 * 186 * Hook function to Check ip specified ip address is a shared ip address 187 * in the cluster 188 * 189 */ 190 int (*cl_inet_isclusterwide)(uint8_t protocol, 191 sa_family_t addr_family, uint8_t *laddrp) = NULL; 192 193 /* 194 * Hook function to generate cluster wide ip fragment identifier 195 */ 196 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 197 uint8_t *laddrp, uint8_t *faddrp) = NULL; 198 199 /* 200 * Synchronization notes: 201 * 202 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 203 * MT level protection given by STREAMS. IP uses a combination of its own 204 * internal serialization mechanism and standard Solaris locking techniques. 205 * The internal serialization is per phyint (no IPMP) or per IPMP group. 206 * This is used to serialize plumbing operations, IPMP operations, certain 207 * multicast operations, most set ioctls, igmp/mld timers etc. 208 * 209 * Plumbing is a long sequence of operations involving message 210 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 211 * involved in plumbing operations. A natural model is to serialize these 212 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 213 * parallel without any interference. But various set ioctls on hme0 are best 214 * serialized. However if the system uses IPMP, the operations are easier if 215 * they are serialized on a per IPMP group basis since IPMP operations 216 * happen across ill's of a group. Thus the lowest common denominator is to 217 * serialize most set ioctls, multicast join/leave operations, IPMP operations 218 * igmp/mld timer operations, and processing of DLPI control messages received 219 * from drivers on a per IPMP group basis. If the system does not employ 220 * IPMP the serialization is on a per phyint basis. This serialization is 221 * provided by the ipsq_t and primitives operating on this. Details can 222 * be found in ip_if.c above the core primitives operating on ipsq_t. 223 * 224 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 225 * Simiarly lookup of an ire by a thread also returns a refheld ire. 226 * In addition ipif's and ill's referenced by the ire are also indirectly 227 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 228 * the ipif's address or netmask change as long as an ipif is refheld 229 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the 230 * address of an ipif has to go through the ipsq_t. This ensures that only 231 * 1 such exclusive operation proceeds at any time on the ipif. It then 232 * deletes all ires associated with this ipif, and waits for all refcnts 233 * associated with this ipif to come down to zero. The address is changed 234 * only after the ipif has been quiesced. Then the ipif is brought up again. 235 * More details are described above the comment in ip_sioctl_flags. 236 * 237 * Packet processing is based mostly on IREs and are fully multi-threaded 238 * using standard Solaris MT techniques. 239 * 240 * There are explicit locks in IP to handle: 241 * - The ip_g_head list maintained by mi_open_link() and friends. 242 * 243 * - The reassembly data structures (one lock per hash bucket) 244 * 245 * - conn_lock is meant to protect conn_t fields. The fields actually 246 * protected by conn_lock are documented in the conn_t definition. 247 * 248 * - ire_lock to protect some of the fields of the ire, IRE tables 249 * (one lock per hash bucket). Refer to ip_ire.c for details. 250 * 251 * - ndp_g_lock and nce_lock for protecting NCEs. 252 * 253 * - ill_lock protects fields of the ill and ipif. Details in ip.h 254 * 255 * - ill_g_lock: This is a global reader/writer lock. Protects the following 256 * * The AVL tree based global multi list of all ills. 257 * * The linked list of all ipifs of an ill 258 * * The <ill-ipsq> mapping 259 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next 260 * * The illgroup list threaded by ill_group_next. 261 * * <ill-phyint> association 262 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 263 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion 264 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill 265 * will all have to hold the ill_g_lock as writer for the actual duration 266 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping 267 * may be found in the IPMP section. 268 * 269 * - ill_lock: This is a per ill mutex. 270 * It protects some members of the ill and is documented below. 271 * It also protects the <ill-ipsq> mapping 272 * It also protects the illgroup list threaded by ill_group_next. 273 * It also protects the <ill-phyint> assoc. 274 * It also protects the list of ipifs hanging off the ill. 275 * 276 * - ipsq_lock: This is a per ipsq_t mutex lock. 277 * This protects all the other members of the ipsq struct except 278 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock 279 * 280 * - illgrp_lock: This is a per ill_group mutex lock. 281 * The only thing it protects is the illgrp_ill_schednext member of ill_group 282 * which dictates which is the next ill in an ill_group that is to be chosen 283 * for sending outgoing packets, through creation of an IRE_CACHE that 284 * references this ill. 285 * 286 * - phyint_lock: This is a per phyint mutex lock. Protects just the 287 * phyint_flags 288 * 289 * - ip_g_nd_lock: This is a global reader/writer lock. 290 * Any call to nd_load to load a new parameter to the ND table must hold the 291 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 292 * as reader. 293 * 294 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 295 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 296 * uniqueness check also done atomically. 297 * 298 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 299 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 300 * as a writer when adding or deleting elements from these lists, and 301 * as a reader when walking these lists to send a SADB update to the 302 * IPsec capable ills. 303 * 304 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 305 * group list linked by ill_usesrc_grp_next. It also protects the 306 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 307 * group is being added or deleted. This lock is taken as a reader when 308 * walking the list/group(eg: to get the number of members in a usesrc group). 309 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 310 * field is changing state i.e from NULL to non-NULL or vice-versa. For 311 * example, it is not necessary to take this lock in the initial portion 312 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and 313 * ip_sioctl_flags since the these operations are executed exclusively and 314 * that ensures that the "usesrc group state" cannot change. The "usesrc 315 * group state" change can happen only in the latter part of 316 * ip_sioctl_slifusesrc and in ill_delete. 317 * 318 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications. 319 * 320 * To change the <ill-phyint> association, the ill_g_lock must be held 321 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 322 * must be held. 323 * 324 * To change the <ill-ipsq> association the ill_g_lock must be held as writer 325 * and the ill_lock of the ill in question must be held. 326 * 327 * To change the <ill-illgroup> association the ill_g_lock must be held as 328 * writer and the ill_lock of the ill in question must be held. 329 * 330 * To add or delete an ipif from the list of ipifs hanging off the ill, 331 * ill_g_lock (writer) and ill_lock must be held and the thread must be 332 * a writer on the associated ipsq,. 333 * 334 * To add or delete an ill to the system, the ill_g_lock must be held as 335 * writer and the thread must be a writer on the associated ipsq. 336 * 337 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 338 * must be a writer on the associated ipsq. 339 * 340 * Lock hierarchy 341 * 342 * Some lock hierarchy scenarios are listed below. 343 * 344 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 345 * ill_g_lock -> illgrp_lock -> ill_lock 346 * ill_g_lock -> ill_lock(s) -> phyint_lock 347 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 348 * ill_g_lock -> ip_addr_avail_lock 349 * conn_lock -> irb_lock -> ill_lock -> ire_lock 350 * ill_g_lock -> ip_g_nd_lock 351 * 352 * When more than 1 ill lock is needed to be held, all ill lock addresses 353 * are sorted on address and locked starting from highest addressed lock 354 * downward. 355 * 356 * Mobile-IP scenarios 357 * 358 * irb_lock -> ill_lock -> ire_mrtun_lock 359 * irb_lock -> ill_lock -> ire_srcif_table_lock 360 * 361 * IPsec scenarios 362 * 363 * ipsa_lock -> ill_g_lock -> ill_lock 364 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 365 * ipsec_capab_ills_lock -> ipsa_lock 366 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 367 * 368 * Trusted Solaris scenarios 369 * 370 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 371 * igsa_lock -> gcdb_lock 372 * gcgrp_rwlock -> ire_lock 373 * gcgrp_rwlock -> gcdb_lock 374 * 375 * 376 * Routing/forwarding table locking notes: 377 * 378 * Lock acquisition order: Radix tree lock, irb_lock. 379 * Requirements: 380 * i. Walker must not hold any locks during the walker callback. 381 * ii Walker must not see a truncated tree during the walk because of any node 382 * deletion. 383 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 384 * in many places in the code to walk the irb list. Thus even if all the 385 * ires in a bucket have been deleted, we still can't free the radix node 386 * until the ires have actually been inactive'd (freed). 387 * 388 * Tree traversal - Need to hold the global tree lock in read mode. 389 * Before dropping the global tree lock, need to either increment the ire_refcnt 390 * to ensure that the radix node can't be deleted. 391 * 392 * Tree add - Need to hold the global tree lock in write mode to add a 393 * radix node. To prevent the node from being deleted, increment the 394 * irb_refcnt, after the node is added to the tree. The ire itself is 395 * added later while holding the irb_lock, but not the tree lock. 396 * 397 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 398 * All associated ires must be inactive (i.e. freed), and irb_refcnt 399 * must be zero. 400 * 401 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 402 * global tree lock (read mode) for traversal. 403 * 404 * IPSEC notes : 405 * 406 * IP interacts with the IPSEC code (AH/ESP) by tagging a M_CTL message 407 * in front of the actual packet. For outbound datagrams, the M_CTL 408 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 409 * information used by the IPSEC code for applying the right level of 410 * protection. The information initialized by IP in the ipsec_out_t 411 * is determined by the per-socket policy or global policy in the system. 412 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 413 * ipsec_info.h) which starts out with nothing in it. It gets filled 414 * with the right information if it goes through the AH/ESP code, which 415 * happens if the incoming packet is secure. The information initialized 416 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 417 * the policy requirements needed by per-socket policy or global policy 418 * is met or not. 419 * 420 * If there is both per-socket policy (set using setsockopt) and there 421 * is also global policy match for the 5 tuples of the socket, 422 * ipsec_override_policy() makes the decision of which one to use. 423 * 424 * For fully connected sockets i.e dst, src [addr, port] is known, 425 * conn_policy_cached is set indicating that policy has been cached. 426 * conn_in_enforce_policy may or may not be set depending on whether 427 * there is a global policy match or per-socket policy match. 428 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 429 * Once the right policy is set on the conn_t, policy cannot change for 430 * this socket. This makes life simpler for TCP (UDP ?) where 431 * re-transmissions go out with the same policy. For symmetry, policy 432 * is cached for fully connected UDP sockets also. Thus if policy is cached, 433 * it also implies that policy is latched i.e policy cannot change 434 * on these sockets. As we have the right policy on the conn, we don't 435 * have to lookup global policy for every outbound and inbound datagram 436 * and thus serving as an optimization. Note that a global policy change 437 * does not affect fully connected sockets if they have policy. If fully 438 * connected sockets did not have any policy associated with it, global 439 * policy change may affect them. 440 * 441 * IP Flow control notes: 442 * 443 * Non-TCP streams are flow controlled by IP. On the send side, if the packet 444 * cannot be sent down to the driver by IP, because of a canput failure, IP 445 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. 446 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained 447 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the 448 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the 449 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains 450 * the queued messages, and removes the conn from the drain list, if all 451 * messages were drained. It also qenables the next conn in the drain list to 452 * continue the drain process. 453 * 454 * In reality the drain list is not a single list, but a configurable number 455 * of lists. The ip_wsrv on the IP module, qenables the first conn in each 456 * list. If the ip_wsrv of the next qenabled conn does not run, because the 457 * stream closes, ip_close takes responsibility to qenable the next conn in 458 * the drain list. The directly called ip_wput path always does a putq, if 459 * it cannot putnext. Thus synchronization problems are handled between 460 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 461 * functions that manipulate this drain list. Furthermore conn_drain_insert 462 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv 463 * running on a queue at any time. conn_drain_tail can be simultaneously called 464 * from both ip_wsrv and ip_close. 465 * 466 * IPQOS notes: 467 * 468 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 469 * and IPQoS modules. IPPF includes hooks in IP at different control points 470 * (callout positions) which direct packets to IPQoS modules for policy 471 * processing. Policies, if present, are global. 472 * 473 * The callout positions are located in the following paths: 474 * o local_in (packets destined for this host) 475 * o local_out (packets orginating from this host ) 476 * o fwd_in (packets forwarded by this m/c - inbound) 477 * o fwd_out (packets forwarded by this m/c - outbound) 478 * Hooks at these callout points can be enabled/disabled using the ndd variable 479 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 480 * By default all the callout positions are enabled. 481 * 482 * Outbound (local_out) 483 * Hooks are placed in ip_wput_ire and ipsec_out_process. 484 * 485 * Inbound (local_in) 486 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 487 * TCP and UDP fanout routines. 488 * 489 * Forwarding (in and out) 490 * Hooks are placed in ip_rput_forward and ip_mrtun_forward. 491 * 492 * IP Policy Framework processing (IPPF processing) 493 * Policy processing for a packet is initiated by ip_process, which ascertains 494 * that the classifier (ipgpc) is loaded and configured, failing which the 495 * packet resumes normal processing in IP. If the clasifier is present, the 496 * packet is acted upon by one or more IPQoS modules (action instances), per 497 * filters configured in ipgpc and resumes normal IP processing thereafter. 498 * An action instance can drop a packet in course of its processing. 499 * 500 * A boolean variable, ip_policy, is used in all the fanout routines that can 501 * invoke ip_process for a packet. This variable indicates if the packet should 502 * to be sent for policy processing. The variable is set to B_TRUE by default, 503 * i.e. when the routines are invoked in the normal ip procesing path for a 504 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 505 * ip_policy is set to B_FALSE for all the routines called in these two 506 * functions because, in the former case, we don't process loopback traffic 507 * currently while in the latter, the packets have already been processed in 508 * icmp_inbound. 509 * 510 * Zones notes: 511 * 512 * The partitioning rules for networking are as follows: 513 * 1) Packets coming from a zone must have a source address belonging to that 514 * zone. 515 * 2) Packets coming from a zone can only be sent on a physical interface on 516 * which the zone has an IP address. 517 * 3) Between two zones on the same machine, packet delivery is only allowed if 518 * there's a matching route for the destination and zone in the forwarding 519 * table. 520 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 521 * different zones can bind to the same port with the wildcard address 522 * (INADDR_ANY). 523 * 524 * The granularity of interface partitioning is at the logical interface level. 525 * Therefore, every zone has its own IP addresses, and incoming packets can be 526 * attributed to a zone unambiguously. A logical interface is placed into a zone 527 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 528 * structure. Rule (1) is implemented by modifying the source address selection 529 * algorithm so that the list of eligible addresses is filtered based on the 530 * sending process zone. 531 * 532 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 533 * across all zones, depending on their type. Here is the break-up: 534 * 535 * IRE type Shared/exclusive 536 * -------- ---------------- 537 * IRE_BROADCAST Exclusive 538 * IRE_DEFAULT (default routes) Shared (*) 539 * IRE_LOCAL Exclusive (x) 540 * IRE_LOOPBACK Exclusive 541 * IRE_PREFIX (net routes) Shared (*) 542 * IRE_CACHE Exclusive 543 * IRE_IF_NORESOLVER (interface routes) Exclusive 544 * IRE_IF_RESOLVER (interface routes) Exclusive 545 * IRE_HOST (host routes) Shared (*) 546 * 547 * (*) A zone can only use a default or off-subnet route if the gateway is 548 * directly reachable from the zone, that is, if the gateway's address matches 549 * one of the zone's logical interfaces. 550 * 551 * (x) IRE_LOCAL are handled a bit differently, since for all other entries 552 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source 553 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP 554 * address of the zone itself (the destination). Since IRE_LOCAL is used 555 * for communication between zones, ip_wput_ire has special logic to set 556 * the right source address when sending using an IRE_LOCAL. 557 * 558 * Furthermore, when ip_restrict_interzone_loopback is set (the default), 559 * ire_cache_lookup restricts loopback using an IRE_LOCAL 560 * between zone to the case when L2 would have conceptually looped the packet 561 * back, i.e. the loopback which is required since neither Ethernet drivers 562 * nor Ethernet hardware loops them back. This is the case when the normal 563 * routes (ignoring IREs with different zoneids) would send out the packet on 564 * the same ill (or ill group) as the ill with which is IRE_LOCAL is 565 * associated. 566 * 567 * Multiple zones can share a common broadcast address; typically all zones 568 * share the 255.255.255.255 address. Incoming as well as locally originated 569 * broadcast packets must be dispatched to all the zones on the broadcast 570 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 571 * since some zones may not be on the 10.16.72/24 network. To handle this, each 572 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 573 * sent to every zone that has an IRE_BROADCAST entry for the destination 574 * address on the input ill, see conn_wantpacket(). 575 * 576 * Applications in different zones can join the same multicast group address. 577 * For IPv4, group memberships are per-logical interface, so they're already 578 * inherently part of a zone. For IPv6, group memberships are per-physical 579 * interface, so we distinguish IPv6 group memberships based on group address, 580 * interface and zoneid. In both cases, received multicast packets are sent to 581 * every zone for which a group membership entry exists. On IPv6 we need to 582 * check that the target zone still has an address on the receiving physical 583 * interface; it could have been removed since the application issued the 584 * IPV6_JOIN_GROUP. 585 */ 586 587 /* 588 * Squeue Fanout flags: 589 * 0: No fanout. 590 * 1: Fanout across all squeues 591 */ 592 boolean_t ip_squeue_fanout = 0; 593 594 /* 595 * Maximum dups allowed per packet. 596 */ 597 uint_t ip_max_frag_dups = 10; 598 599 #define IS_SIMPLE_IPH(ipha) \ 600 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 601 602 /* RFC1122 Conformance */ 603 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 604 605 #define ILL_MAX_NAMELEN LIFNAMSIZ 606 607 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 608 609 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t); 610 static void ip_ipsec_out_prepend(mblk_t *, mblk_t *, ill_t *); 611 612 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t, 613 ip_stack_t *); 614 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 615 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 616 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); 617 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, 618 mblk_t *, int, ip_stack_t *); 619 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 620 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 621 ill_t *, zoneid_t); 622 static void icmp_options_update(ipha_t *); 623 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t, 624 ip_stack_t *); 625 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, 626 zoneid_t zoneid, ip_stack_t *); 627 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *); 628 static void icmp_redirect(ill_t *, mblk_t *); 629 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t, 630 ip_stack_t *); 631 632 static void ip_arp_news(queue_t *, mblk_t *); 633 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *, 634 ip_stack_t *); 635 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 636 char *ip_dot_addr(ipaddr_t, char *); 637 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 638 int ip_close(queue_t *, int); 639 static char *ip_dot_saddr(uchar_t *, char *); 640 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 641 boolean_t, boolean_t, ill_t *, zoneid_t); 642 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 643 boolean_t, boolean_t, zoneid_t); 644 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 645 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 646 static void ip_lrput(queue_t *, mblk_t *); 647 static void ip_mrtun_forward(ire_t *, ill_t *, mblk_t *); 648 ipaddr_t ip_net_mask(ipaddr_t); 649 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, ill_t *, conn_t *, 650 zoneid_t, ip_stack_t *); 651 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 652 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *); 653 char *ip_nv_lookup(nv_t *, int); 654 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 655 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 656 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 657 static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t, 658 ipndp_t *, size_t); 659 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 660 void ip_rput(queue_t *, mblk_t *); 661 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 662 void *dummy_arg); 663 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 664 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *, 665 ip_stack_t *); 666 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 667 ire_t *, ip_stack_t *); 668 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *, 669 mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *); 670 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *, 671 ip_stack_t *); 672 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, 673 uint16_t *); 674 int ip_snmp_get(queue_t *, mblk_t *); 675 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, 676 mib2_ipIfStatsEntry_t *, ip_stack_t *); 677 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *, 678 ip_stack_t *); 679 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *); 680 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst); 681 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst); 682 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst); 683 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst); 684 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *, 685 ip_stack_t *ipst); 686 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *, 687 ip_stack_t *ipst); 688 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *, 689 ip_stack_t *ipst); 690 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *, 691 ip_stack_t *ipst); 692 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *, 693 ip_stack_t *ipst); 694 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *, 695 ip_stack_t *ipst); 696 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *, 697 ip_stack_t *ipst); 698 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *, 699 ip_stack_t *ipst); 700 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, 701 ip_stack_t *ipst); 702 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, 703 ip_stack_t *ipst); 704 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 705 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 706 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); 707 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 708 static boolean_t ip_source_routed(ipha_t *, ip_stack_t *); 709 static boolean_t ip_source_route_included(ipha_t *); 710 static void ip_trash_ire_reclaim_stack(ip_stack_t *); 711 712 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, 713 zoneid_t, ip_stack_t *); 714 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *); 715 static void ip_wput_local_options(ipha_t *, ip_stack_t *); 716 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 717 zoneid_t, ip_stack_t *); 718 719 static void conn_drain_init(ip_stack_t *); 720 static void conn_drain_fini(ip_stack_t *); 721 static void conn_drain_tail(conn_t *connp, boolean_t closing); 722 723 static void conn_walk_drain(ip_stack_t *); 724 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, 725 zoneid_t); 726 727 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns); 728 static void ip_stack_shutdown(netstackid_t stackid, void *arg); 729 static void ip_stack_fini(netstackid_t stackid, void *arg); 730 731 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 732 zoneid_t); 733 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 734 void *dummy_arg); 735 736 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 737 738 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 739 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 740 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 741 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 742 743 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 744 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 745 caddr_t, cred_t *); 746 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, 747 caddr_t cp, cred_t *cr); 748 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, 749 cred_t *); 750 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 751 caddr_t cp, cred_t *cr); 752 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 753 cred_t *); 754 static squeue_func_t ip_squeue_switch(int); 755 756 static void *ip_kstat_init(netstackid_t, ip_stack_t *); 757 static void ip_kstat_fini(netstackid_t, kstat_t *); 758 static int ip_kstat_update(kstat_t *kp, int rw); 759 static void *icmp_kstat_init(netstackid_t); 760 static void icmp_kstat_fini(netstackid_t, kstat_t *); 761 static int icmp_kstat_update(kstat_t *kp, int rw); 762 static void *ip_kstat2_init(netstackid_t, ip_stat_t *); 763 static void ip_kstat2_fini(netstackid_t, kstat_t *); 764 765 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); 766 767 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 768 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 769 770 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 771 ipha_t *, ill_t *, boolean_t); 772 773 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 774 ipha_t *, ill_t *, boolean_t); 775 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 776 777 /* How long, in seconds, we allow frags to hang around. */ 778 #define IP_FRAG_TIMEOUT 60 779 780 /* 781 * Threshold which determines whether MDT should be used when 782 * generating IP fragments; payload size must be greater than 783 * this threshold for MDT to take place. 784 */ 785 #define IP_WPUT_FRAG_MDT_MIN 32768 786 787 /* Setable in /etc/system only */ 788 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 789 790 static long ip_rput_pullups; 791 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 792 793 vmem_t *ip_minor_arena; 794 795 int ip_debug; 796 797 #ifdef DEBUG 798 uint32_t ipsechw_debug = 0; 799 #endif 800 801 /* 802 * Multirouting/CGTP stuff 803 */ 804 cgtp_filter_ops_t *ip_cgtp_filter_ops; /* CGTP hooks */ 805 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 806 boolean_t ip_cgtp_filter; /* Enable/disable CGTP hooks */ 807 808 /* 809 * XXX following really should only be in a header. Would need more 810 * header and .c clean up first. 811 */ 812 extern optdb_obj_t ip_opt_obj; 813 814 ulong_t ip_squeue_enter_unbound = 0; 815 816 /* 817 * Named Dispatch Parameter Table. 818 * All of these are alterable, within the min/max values given, at run time. 819 */ 820 static ipparam_t lcl_param_arr[] = { 821 /* min max value name */ 822 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 823 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 824 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 825 { 0, 1, 0, "ip_respond_to_timestamp"}, 826 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 827 { 0, 1, 1, "ip_send_redirects"}, 828 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 829 { 0, 10, 0, "ip_debug"}, 830 { 0, 10, 0, "ip_mrtdebug"}, 831 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 832 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 833 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 834 { 1, 255, 255, "ip_def_ttl" }, 835 { 0, 1, 0, "ip_forward_src_routed"}, 836 { 0, 256, 32, "ip_wroff_extra" }, 837 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 838 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 839 { 0, 1, 1, "ip_path_mtu_discovery" }, 840 { 0, 240, 30, "ip_ignore_delete_time" }, 841 { 0, 1, 0, "ip_ignore_redirect" }, 842 { 0, 1, 1, "ip_output_queue" }, 843 { 1, 254, 1, "ip_broadcast_ttl" }, 844 { 0, 99999, 100, "ip_icmp_err_interval" }, 845 { 1, 99999, 10, "ip_icmp_err_burst" }, 846 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 847 { 0, 1, 0, "ip_strict_dst_multihoming" }, 848 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 849 { 0, 1, 0, "ipsec_override_persocket_policy" }, 850 { 0, 1, 1, "icmp_accept_clear_messages" }, 851 { 0, 1, 1, "igmp_accept_clear_messages" }, 852 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 853 "ip_ndp_delay_first_probe_time"}, 854 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 855 "ip_ndp_max_unicast_solicit"}, 856 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 857 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 858 { 0, 1, 0, "ip6_forward_src_routed"}, 859 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 860 { 0, 1, 1, "ip6_send_redirects"}, 861 { 0, 1, 0, "ip6_ignore_redirect" }, 862 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 863 864 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 865 866 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 867 868 { 0, 1, 1, "pim_accept_clear_messages" }, 869 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 870 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 871 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 872 { 0, 15, 0, "ip_policy_mask" }, 873 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 874 { 0, 255, 1, "ip_multirt_ttl" }, 875 { 0, 1, 1, "ip_multidata_outbound" }, 876 { 0, 3600000, 300000, "ip_ndp_defense_interval" }, 877 { 0, 999999, 60*60*24, "ip_max_temp_idle" }, 878 { 0, 1000, 1, "ip_max_temp_defend" }, 879 { 0, 1000, 3, "ip_max_defend" }, 880 { 0, 999999, 30, "ip_defend_interval" }, 881 { 0, 3600000, 300000, "ip_dup_recovery" }, 882 { 0, 1, 1, "ip_restrict_interzone_loopback" }, 883 { 0, 1, 1, "ip_lso_outbound" }, 884 #ifdef DEBUG 885 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 886 #else 887 { 0, 0, 0, "" }, 888 #endif 889 }; 890 891 /* 892 * Extended NDP table 893 * The addresses for the first two are filled in to be ips_ip_g_forward 894 * and ips_ipv6_forward at init time. 895 */ 896 static ipndp_t lcl_ndp_arr[] = { 897 /* getf setf data name */ 898 #define IPNDP_IP_FORWARDING_OFFSET 0 899 { ip_param_generic_get, ip_forward_set, NULL, 900 "ip_forwarding" }, 901 #define IPNDP_IP6_FORWARDING_OFFSET 1 902 { ip_param_generic_get, ip_forward_set, NULL, 903 "ip6_forwarding" }, 904 { ip_ill_report, NULL, NULL, 905 "ip_ill_status" }, 906 { ip_ipif_report, NULL, NULL, 907 "ip_ipif_status" }, 908 { ip_ire_report, NULL, NULL, 909 "ipv4_ire_status" }, 910 { ip_ire_report_mrtun, NULL, NULL, 911 "ipv4_mrtun_ire_status" }, 912 { ip_ire_report_srcif, NULL, NULL, 913 "ipv4_srcif_ire_status" }, 914 { ip_ire_report_v6, NULL, NULL, 915 "ipv6_ire_status" }, 916 { ip_conn_report, NULL, NULL, 917 "ip_conn_status" }, 918 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, 919 "ip_rput_pullups" }, 920 { ndp_report, NULL, NULL, 921 "ip_ndp_cache_report" }, 922 { ip_srcid_report, NULL, NULL, 923 "ip_srcid_status" }, 924 { ip_param_generic_get, ip_squeue_profile_set, 925 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, 926 { ip_param_generic_get, ip_squeue_bind_set, 927 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, 928 { ip_param_generic_get, ip_input_proc_set, 929 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 930 { ip_param_generic_get, ip_int_set, 931 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 932 #define IPNDP_CGTP_FILTER_OFFSET 16 933 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL, 934 "ip_cgtp_filter" }, 935 { ip_param_generic_get, ip_int_set, 936 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" }, 937 }; 938 939 /* 940 * Table of IP ioctls encoding the various properties of the ioctl and 941 * indexed based on the last byte of the ioctl command. Occasionally there 942 * is a clash, and there is more than 1 ioctl with the same last byte. 943 * In such a case 1 ioctl is encoded in the ndx table and the remaining 944 * ioctls are encoded in the misc table. An entry in the ndx table is 945 * retrieved by indexing on the last byte of the ioctl command and comparing 946 * the ioctl command with the value in the ndx table. In the event of a 947 * mismatch the misc table is then searched sequentially for the desired 948 * ioctl command. 949 * 950 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 951 */ 952 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 953 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 954 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 955 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 956 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 957 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 958 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 959 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 960 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 961 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 962 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 963 964 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 965 MISC_CMD, ip_siocaddrt, NULL }, 966 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 967 MISC_CMD, ip_siocdelrt, NULL }, 968 969 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 970 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 971 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 972 IF_CMD, ip_sioctl_get_addr, NULL }, 973 974 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 975 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 976 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 977 IPI_GET_CMD | IPI_REPL, 978 IF_CMD, ip_sioctl_get_dstaddr, NULL }, 979 980 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 981 IPI_PRIV | IPI_WR | IPI_REPL, 982 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 983 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 984 IPI_MODOK | IPI_GET_CMD | IPI_REPL, 985 IF_CMD, ip_sioctl_get_flags, NULL }, 986 987 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 988 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 989 990 /* copyin size cannot be coded for SIOCGIFCONF */ 991 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 992 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 993 994 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 995 IF_CMD, ip_sioctl_mtu, NULL }, 996 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 997 IF_CMD, ip_sioctl_get_mtu, NULL }, 998 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 999 IPI_GET_CMD | IPI_REPL, 1000 IF_CMD, ip_sioctl_get_brdaddr, NULL }, 1001 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1002 IF_CMD, ip_sioctl_brdaddr, NULL }, 1003 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 1004 IPI_GET_CMD | IPI_REPL, 1005 IF_CMD, ip_sioctl_get_netmask, NULL }, 1006 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1007 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1008 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 1009 IPI_GET_CMD | IPI_REPL, 1010 IF_CMD, ip_sioctl_get_metric, NULL }, 1011 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 1012 IF_CMD, ip_sioctl_metric, NULL }, 1013 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1014 1015 /* See 166-168 below for extended SIOC*XARP ioctls */ 1016 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV, 1017 MISC_CMD, ip_sioctl_arp, NULL }, 1018 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, 1019 MISC_CMD, ip_sioctl_arp, NULL }, 1020 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, 1021 MISC_CMD, ip_sioctl_arp, NULL }, 1022 1023 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1024 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1025 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1026 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1027 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1028 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1029 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1030 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1031 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1032 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1033 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1034 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1035 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1036 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1037 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1038 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1039 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1040 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1041 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1042 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1043 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1044 1045 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 1046 MISC_CMD, if_unitsel, if_unitsel_restart }, 1047 1048 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1049 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1050 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1051 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1052 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1053 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1054 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1055 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1056 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1057 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1058 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1059 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1060 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1061 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1062 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1063 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1064 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1065 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1066 1067 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 1068 IPI_PRIV | IPI_WR | IPI_MODOK, 1069 IF_CMD, ip_sioctl_sifname, NULL }, 1070 1071 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1072 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1073 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1074 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1075 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1076 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1077 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1078 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1079 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1080 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1081 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1082 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1083 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1084 1085 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL, 1086 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 1087 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1088 IF_CMD, ip_sioctl_get_muxid, NULL }, 1089 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 1090 IPI_PRIV | IPI_WR | IPI_REPL, 1091 IF_CMD, ip_sioctl_muxid, NULL }, 1092 1093 /* Both if and lif variants share same func */ 1094 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1095 IF_CMD, ip_sioctl_get_lifindex, NULL }, 1096 /* Both if and lif variants share same func */ 1097 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 1098 IPI_PRIV | IPI_WR | IPI_REPL, 1099 IF_CMD, ip_sioctl_slifindex, NULL }, 1100 1101 /* copyin size cannot be coded for SIOCGIFCONF */ 1102 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1103 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1104 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1105 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1106 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1107 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1108 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1109 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1110 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1111 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1112 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1113 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1114 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1115 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1116 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1117 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1118 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1119 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1120 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1121 1122 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 1123 IPI_PRIV | IPI_WR | IPI_REPL, 1124 LIF_CMD, ip_sioctl_removeif, 1125 ip_sioctl_removeif_restart }, 1126 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 1127 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL, 1128 LIF_CMD, ip_sioctl_addif, NULL }, 1129 #define SIOCLIFADDR_NDX 112 1130 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1131 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1132 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 1133 IPI_GET_CMD | IPI_REPL, 1134 LIF_CMD, ip_sioctl_get_addr, NULL }, 1135 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1136 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1137 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 1138 IPI_GET_CMD | IPI_REPL, 1139 LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 1140 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 1141 IPI_PRIV | IPI_WR | IPI_REPL, 1142 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1143 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 1144 IPI_GET_CMD | IPI_MODOK | IPI_REPL, 1145 LIF_CMD, ip_sioctl_get_flags, NULL }, 1146 1147 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1148 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1149 1150 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1151 ip_sioctl_get_lifconf, NULL }, 1152 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1153 LIF_CMD, ip_sioctl_mtu, NULL }, 1154 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, 1155 LIF_CMD, ip_sioctl_get_mtu, NULL }, 1156 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 1157 IPI_GET_CMD | IPI_REPL, 1158 LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 1159 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1160 LIF_CMD, ip_sioctl_brdaddr, NULL }, 1161 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 1162 IPI_GET_CMD | IPI_REPL, 1163 LIF_CMD, ip_sioctl_get_netmask, NULL }, 1164 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1165 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1166 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 1167 IPI_GET_CMD | IPI_REPL, 1168 LIF_CMD, ip_sioctl_get_metric, NULL }, 1169 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1170 LIF_CMD, ip_sioctl_metric, NULL }, 1171 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1172 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL, 1173 LIF_CMD, ip_sioctl_slifname, 1174 ip_sioctl_slifname_restart }, 1175 1176 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL, 1177 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1178 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1179 IPI_GET_CMD | IPI_REPL, 1180 LIF_CMD, ip_sioctl_get_muxid, NULL }, 1181 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1182 IPI_PRIV | IPI_WR | IPI_REPL, 1183 LIF_CMD, ip_sioctl_muxid, NULL }, 1184 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1185 IPI_GET_CMD | IPI_REPL, 1186 LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1187 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1188 IPI_PRIV | IPI_WR | IPI_REPL, 1189 LIF_CMD, ip_sioctl_slifindex, 0 }, 1190 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1191 LIF_CMD, ip_sioctl_token, NULL }, 1192 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1193 IPI_GET_CMD | IPI_REPL, 1194 LIF_CMD, ip_sioctl_get_token, NULL }, 1195 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1196 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1197 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1198 IPI_GET_CMD | IPI_REPL, 1199 LIF_CMD, ip_sioctl_get_subnet, NULL }, 1200 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1201 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1202 1203 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1204 IPI_GET_CMD | IPI_REPL, 1205 LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1206 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1207 LIF_CMD, ip_siocdelndp_v6, NULL }, 1208 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1209 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1210 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1211 LIF_CMD, ip_siocsetndp_v6, NULL }, 1212 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1213 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1214 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1215 MISC_CMD, ip_sioctl_tonlink, NULL }, 1216 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1217 MISC_CMD, ip_sioctl_tmysite, NULL }, 1218 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL, 1219 TUN_CMD, ip_sioctl_tunparam, NULL }, 1220 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), 1221 IPI_PRIV | IPI_WR, 1222 TUN_CMD, ip_sioctl_tunparam, NULL }, 1223 1224 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1225 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1226 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1227 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1228 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1229 1230 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq), 1231 IPI_PRIV | IPI_WR | IPI_REPL, 1232 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1233 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq), 1234 IPI_PRIV | IPI_WR | IPI_REPL, 1235 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1236 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1237 IPI_PRIV | IPI_WR, 1238 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1239 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1240 IPI_GET_CMD | IPI_REPL, 1241 LIF_CMD, ip_sioctl_get_groupname, NULL }, 1242 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq), 1243 IPI_GET_CMD | IPI_REPL, 1244 LIF_CMD, ip_sioctl_get_oindex, NULL }, 1245 1246 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1247 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1248 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1249 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1250 1251 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1252 LIF_CMD, ip_sioctl_slifoindex, NULL }, 1253 1254 /* These are handled in ip_sioctl_copyin_setup itself */ 1255 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1256 MISC_CMD, NULL, NULL }, 1257 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1258 MISC_CMD, NULL, NULL }, 1259 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1260 1261 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1262 ip_sioctl_get_lifconf, NULL }, 1263 1264 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, 1265 MISC_CMD, ip_sioctl_xarp, NULL }, 1266 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, 1267 MISC_CMD, ip_sioctl_xarp, NULL }, 1268 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, 1269 MISC_CMD, ip_sioctl_xarp, NULL }, 1270 1271 /* SIOCPOPSOCKFS is not handled by IP */ 1272 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1273 1274 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1275 IPI_GET_CMD | IPI_REPL, 1276 LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1277 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1278 IPI_PRIV | IPI_WR | IPI_REPL, 1279 LIF_CMD, ip_sioctl_slifzone, 1280 ip_sioctl_slifzone_restart }, 1281 /* 172-174 are SCTP ioctls and not handled by IP */ 1282 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1283 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1284 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1285 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1286 IPI_GET_CMD, LIF_CMD, 1287 ip_sioctl_get_lifusesrc, 0 }, 1288 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1289 IPI_PRIV | IPI_WR, 1290 LIF_CMD, ip_sioctl_slifusesrc, 1291 NULL }, 1292 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1293 ip_sioctl_get_lifsrcof, NULL }, 1294 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1295 MISC_CMD, ip_sioctl_msfilter, NULL }, 1296 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1297 MISC_CMD, ip_sioctl_msfilter, NULL }, 1298 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1299 MISC_CMD, ip_sioctl_msfilter, NULL }, 1300 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1301 MISC_CMD, ip_sioctl_msfilter, NULL }, 1302 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD, 1303 ip_sioctl_set_ipmpfailback, NULL } 1304 }; 1305 1306 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1307 1308 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1309 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), 1310 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, 1311 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, 1312 TUN_CMD, ip_sioctl_tunparam, NULL }, 1313 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1314 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1315 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1316 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1317 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, 1318 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1319 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1320 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD, 1321 MISC_CMD, mrt_ioctl}, 1322 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD, 1323 MISC_CMD, mrt_ioctl}, 1324 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD, 1325 MISC_CMD, mrt_ioctl} 1326 }; 1327 1328 int ip_misc_ioctl_count = 1329 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1330 1331 int conn_drain_nthreads; /* Number of drainers reqd. */ 1332 /* Settable in /etc/system */ 1333 /* Defined in ip_ire.c */ 1334 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1335 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1336 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1337 1338 static nv_t ire_nv_arr[] = { 1339 { IRE_BROADCAST, "BROADCAST" }, 1340 { IRE_LOCAL, "LOCAL" }, 1341 { IRE_LOOPBACK, "LOOPBACK" }, 1342 { IRE_CACHE, "CACHE" }, 1343 { IRE_DEFAULT, "DEFAULT" }, 1344 { IRE_PREFIX, "PREFIX" }, 1345 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1346 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1347 { IRE_HOST, "HOST" }, 1348 { 0 } 1349 }; 1350 1351 nv_t *ire_nv_tbl = ire_nv_arr; 1352 1353 /* Defined in ip_netinfo.c */ 1354 extern ddi_taskq_t *eventq_queue_nic; 1355 1356 /* Simple ICMP IP Header Template */ 1357 static ipha_t icmp_ipha = { 1358 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1359 }; 1360 1361 struct module_info ip_mod_info = { 1362 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024 1363 }; 1364 1365 /* 1366 * Duplicate static symbols within a module confuses mdb; so we avoid the 1367 * problem by making the symbols here distinct from those in udp.c. 1368 */ 1369 1370 static struct qinit iprinit = { 1371 (pfi_t)ip_rput, NULL, ip_open, ip_close, NULL, 1372 &ip_mod_info 1373 }; 1374 1375 static struct qinit ipwinit = { 1376 (pfi_t)ip_wput, (pfi_t)ip_wsrv, ip_open, ip_close, NULL, 1377 &ip_mod_info 1378 }; 1379 1380 static struct qinit iplrinit = { 1381 (pfi_t)ip_lrput, NULL, ip_open, ip_close, NULL, 1382 &ip_mod_info 1383 }; 1384 1385 static struct qinit iplwinit = { 1386 (pfi_t)ip_lwput, NULL, ip_open, ip_close, NULL, 1387 &ip_mod_info 1388 }; 1389 1390 struct streamtab ipinfo = { 1391 &iprinit, &ipwinit, &iplrinit, &iplwinit 1392 }; 1393 1394 #ifdef DEBUG 1395 static boolean_t skip_sctp_cksum = B_FALSE; 1396 #endif 1397 1398 /* 1399 * Prepend the zoneid using an ipsec_out_t for later use by functions like 1400 * ip_rput_v6(), ip_output(), etc. If the message 1401 * block already has a M_CTL at the front of it, then simply set the zoneid 1402 * appropriately. 1403 */ 1404 mblk_t * 1405 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst) 1406 { 1407 mblk_t *first_mp; 1408 ipsec_out_t *io; 1409 1410 ASSERT(zoneid != ALL_ZONES); 1411 if (mp->b_datap->db_type == M_CTL) { 1412 io = (ipsec_out_t *)mp->b_rptr; 1413 ASSERT(io->ipsec_out_type == IPSEC_OUT); 1414 io->ipsec_out_zoneid = zoneid; 1415 return (mp); 1416 } 1417 1418 first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack); 1419 if (first_mp == NULL) 1420 return (NULL); 1421 io = (ipsec_out_t *)first_mp->b_rptr; 1422 /* This is not a secure packet */ 1423 io->ipsec_out_secure = B_FALSE; 1424 io->ipsec_out_zoneid = zoneid; 1425 first_mp->b_cont = mp; 1426 return (first_mp); 1427 } 1428 1429 /* 1430 * Copy an M_CTL-tagged message, preserving reference counts appropriately. 1431 */ 1432 mblk_t * 1433 ip_copymsg(mblk_t *mp) 1434 { 1435 mblk_t *nmp; 1436 ipsec_info_t *in; 1437 1438 if (mp->b_datap->db_type != M_CTL) 1439 return (copymsg(mp)); 1440 1441 in = (ipsec_info_t *)mp->b_rptr; 1442 1443 /* 1444 * Note that M_CTL is also used for delivering ICMP error messages 1445 * upstream to transport layers. 1446 */ 1447 if (in->ipsec_info_type != IPSEC_OUT && 1448 in->ipsec_info_type != IPSEC_IN) 1449 return (copymsg(mp)); 1450 1451 nmp = copymsg(mp->b_cont); 1452 1453 if (in->ipsec_info_type == IPSEC_OUT) { 1454 return (ipsec_out_tag(mp, nmp, 1455 ((ipsec_out_t *)in)->ipsec_out_ns)); 1456 } else { 1457 return (ipsec_in_tag(mp, nmp, 1458 ((ipsec_in_t *)in)->ipsec_in_ns)); 1459 } 1460 } 1461 1462 /* Generate an ICMP fragmentation needed message. */ 1463 static void 1464 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid, 1465 ip_stack_t *ipst) 1466 { 1467 icmph_t icmph; 1468 mblk_t *first_mp; 1469 boolean_t mctl_present; 1470 1471 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1472 1473 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 1474 if (mctl_present) 1475 freeb(first_mp); 1476 return; 1477 } 1478 1479 bzero(&icmph, sizeof (icmph_t)); 1480 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1481 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1482 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1483 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded); 1484 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 1485 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 1486 ipst); 1487 } 1488 1489 /* 1490 * icmp_inbound deals with ICMP messages in the following ways. 1491 * 1492 * 1) It needs to send a reply back and possibly delivering it 1493 * to the "interested" upper clients. 1494 * 2) It needs to send it to the upper clients only. 1495 * 3) It needs to change some values in IP only. 1496 * 4) It needs to change some values in IP and upper layers e.g TCP. 1497 * 1498 * We need to accomodate icmp messages coming in clear until we get 1499 * everything secure from the wire. If icmp_accept_clear_messages 1500 * is zero we check with the global policy and act accordingly. If 1501 * it is non-zero, we accept the message without any checks. But 1502 * *this does not mean* that this will be delivered to the upper 1503 * clients. By accepting we might send replies back, change our MTU 1504 * value etc. but delivery to the ULP/clients depends on their policy 1505 * dispositions. 1506 * 1507 * We handle the above 4 cases in the context of IPSEC in the 1508 * following way : 1509 * 1510 * 1) Send the reply back in the same way as the request came in. 1511 * If it came in encrypted, it goes out encrypted. If it came in 1512 * clear, it goes out in clear. Thus, this will prevent chosen 1513 * plain text attack. 1514 * 2) The client may or may not expect things to come in secure. 1515 * If it comes in secure, the policy constraints are checked 1516 * before delivering it to the upper layers. If it comes in 1517 * clear, ipsec_inbound_accept_clear will decide whether to 1518 * accept this in clear or not. In both the cases, if the returned 1519 * message (IP header + 8 bytes) that caused the icmp message has 1520 * AH/ESP headers, it is sent up to AH/ESP for validation before 1521 * sending up. If there are only 8 bytes of returned message, then 1522 * upper client will not be notified. 1523 * 3) Check with global policy to see whether it matches the constaints. 1524 * But this will be done only if icmp_accept_messages_in_clear is 1525 * zero. 1526 * 4) If we need to change both in IP and ULP, then the decision taken 1527 * while affecting the values in IP and while delivering up to TCP 1528 * should be the same. 1529 * 1530 * There are two cases. 1531 * 1532 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1533 * failed), we will not deliver it to the ULP, even though they 1534 * are *willing* to accept in *clear*. This is fine as our global 1535 * disposition to icmp messages asks us reject the datagram. 1536 * 1537 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1538 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1539 * to deliver it to ULP (policy failed), it can lead to 1540 * consistency problems. The cases known at this time are 1541 * ICMP_DESTINATION_UNREACHABLE messages with following code 1542 * values : 1543 * 1544 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1545 * and Upper layer rejects. Then the communication will 1546 * come to a stop. This is solved by making similar decisions 1547 * at both levels. Currently, when we are unable to deliver 1548 * to the Upper Layer (due to policy failures) while IP has 1549 * adjusted ire_max_frag, the next outbound datagram would 1550 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1551 * will be with the right level of protection. Thus the right 1552 * value will be communicated even if we are not able to 1553 * communicate when we get from the wire initially. But this 1554 * assumes there would be at least one outbound datagram after 1555 * IP has adjusted its ire_max_frag value. To make things 1556 * simpler, we accept in clear after the validation of 1557 * AH/ESP headers. 1558 * 1559 * - Other ICMP ERRORS : We may not be able to deliver it to the 1560 * upper layer depending on the level of protection the upper 1561 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1562 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1563 * should be accepted in clear when the Upper layer expects secure. 1564 * Thus the communication may get aborted by some bad ICMP 1565 * packets. 1566 * 1567 * IPQoS Notes: 1568 * The only instance when a packet is sent for processing is when there 1569 * isn't an ICMP client and if we are interested in it. 1570 * If there is a client, IPPF processing will take place in the 1571 * ip_fanout_proto routine. 1572 * 1573 * Zones notes: 1574 * The packet is only processed in the context of the specified zone: typically 1575 * only this zone will reply to an echo request, and only interested clients in 1576 * this zone will receive a copy of the packet. This means that the caller must 1577 * call icmp_inbound() for each relevant zone. 1578 */ 1579 static void 1580 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, 1581 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, 1582 ill_t *recv_ill, zoneid_t zoneid) 1583 { 1584 icmph_t *icmph; 1585 ipha_t *ipha; 1586 int iph_hdr_length; 1587 int hdr_length; 1588 boolean_t interested; 1589 uint32_t ts; 1590 uchar_t *wptr; 1591 ipif_t *ipif; 1592 mblk_t *first_mp; 1593 ipsec_in_t *ii; 1594 ire_t *src_ire; 1595 boolean_t onlink; 1596 timestruc_t now; 1597 uint32_t ill_index; 1598 ip_stack_t *ipst; 1599 1600 ASSERT(ill != NULL); 1601 ipst = ill->ill_ipst; 1602 1603 first_mp = mp; 1604 if (mctl_present) { 1605 mp = first_mp->b_cont; 1606 ASSERT(mp != NULL); 1607 } 1608 1609 ipha = (ipha_t *)mp->b_rptr; 1610 if (ipst->ips_icmp_accept_clear_messages == 0) { 1611 first_mp = ipsec_check_global_policy(first_mp, NULL, 1612 ipha, NULL, mctl_present, ipst->ips_netstack); 1613 if (first_mp == NULL) 1614 return; 1615 } 1616 1617 /* 1618 * On a labeled system, we have to check whether the zone itself is 1619 * permitted to receive raw traffic. 1620 */ 1621 if (is_system_labeled()) { 1622 if (zoneid == ALL_ZONES) 1623 zoneid = tsol_packet_to_zoneid(mp); 1624 if (!tsol_can_accept_raw(mp, B_FALSE)) { 1625 ip1dbg(("icmp_inbound: zone %d can't receive raw", 1626 zoneid)); 1627 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1628 freemsg(first_mp); 1629 return; 1630 } 1631 } 1632 1633 /* 1634 * We have accepted the ICMP message. It means that we will 1635 * respond to the packet if needed. It may not be delivered 1636 * to the upper client depending on the policy constraints 1637 * and the disposition in ipsec_inbound_accept_clear. 1638 */ 1639 1640 ASSERT(ill != NULL); 1641 1642 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs); 1643 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1644 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { 1645 /* Last chance to get real. */ 1646 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { 1647 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1648 freemsg(first_mp); 1649 return; 1650 } 1651 /* Refresh iph following the pullup. */ 1652 ipha = (ipha_t *)mp->b_rptr; 1653 } 1654 /* ICMP header checksum, including checksum field, should be zero. */ 1655 if (sum_valid ? (sum != 0 && sum != 0xFFFF) : 1656 IP_CSUM(mp, iph_hdr_length, 0)) { 1657 BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs); 1658 freemsg(first_mp); 1659 return; 1660 } 1661 /* The IP header will always be a multiple of four bytes */ 1662 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1663 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, 1664 icmph->icmph_code)); 1665 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1666 /* We will set "interested" to "true" if we want a copy */ 1667 interested = B_FALSE; 1668 switch (icmph->icmph_type) { 1669 case ICMP_ECHO_REPLY: 1670 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps); 1671 break; 1672 case ICMP_DEST_UNREACHABLE: 1673 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1674 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded); 1675 interested = B_TRUE; /* Pass up to transport */ 1676 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs); 1677 break; 1678 case ICMP_SOURCE_QUENCH: 1679 interested = B_TRUE; /* Pass up to transport */ 1680 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs); 1681 break; 1682 case ICMP_REDIRECT: 1683 if (!ipst->ips_ip_ignore_redirect) 1684 interested = B_TRUE; 1685 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects); 1686 break; 1687 case ICMP_ECHO_REQUEST: 1688 /* 1689 * Whether to respond to echo requests that come in as IP 1690 * broadcasts or as IP multicast is subject to debate 1691 * (what isn't?). We aim to please, you pick it. 1692 * Default is do it. 1693 */ 1694 if (!broadcast && !CLASSD(ipha->ipha_dst)) { 1695 /* unicast: always respond */ 1696 interested = B_TRUE; 1697 } else if (CLASSD(ipha->ipha_dst)) { 1698 /* multicast: respond based on tunable */ 1699 interested = ipst->ips_ip_g_resp_to_echo_mcast; 1700 } else if (broadcast) { 1701 /* broadcast: respond based on tunable */ 1702 interested = ipst->ips_ip_g_resp_to_echo_bcast; 1703 } 1704 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos); 1705 break; 1706 case ICMP_ROUTER_ADVERTISEMENT: 1707 case ICMP_ROUTER_SOLICITATION: 1708 break; 1709 case ICMP_TIME_EXCEEDED: 1710 interested = B_TRUE; /* Pass up to transport */ 1711 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds); 1712 break; 1713 case ICMP_PARAM_PROBLEM: 1714 interested = B_TRUE; /* Pass up to transport */ 1715 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs); 1716 break; 1717 case ICMP_TIME_STAMP_REQUEST: 1718 /* Response to Time Stamp Requests is local policy. */ 1719 if (ipst->ips_ip_g_resp_to_timestamp && 1720 /* So is whether to respond if it was an IP broadcast. */ 1721 (!broadcast || ipst->ips_ip_g_resp_to_timestamp_bcast)) { 1722 int tstamp_len = 3 * sizeof (uint32_t); 1723 1724 if (wptr + tstamp_len > mp->b_wptr) { 1725 if (!pullupmsg(mp, wptr + tstamp_len - 1726 mp->b_rptr)) { 1727 BUMP_MIB(ill->ill_ip_mib, 1728 ipIfStatsInDiscards); 1729 freemsg(first_mp); 1730 return; 1731 } 1732 /* Refresh ipha following the pullup. */ 1733 ipha = (ipha_t *)mp->b_rptr; 1734 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1735 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1736 } 1737 interested = B_TRUE; 1738 } 1739 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps); 1740 break; 1741 case ICMP_TIME_STAMP_REPLY: 1742 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps); 1743 break; 1744 case ICMP_INFO_REQUEST: 1745 /* Per RFC 1122 3.2.2.7, ignore this. */ 1746 case ICMP_INFO_REPLY: 1747 break; 1748 case ICMP_ADDRESS_MASK_REQUEST: 1749 if ((ipst->ips_ip_respond_to_address_mask_broadcast || 1750 !broadcast) && 1751 /* TODO m_pullup of complete header? */ 1752 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) 1753 interested = B_TRUE; 1754 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks); 1755 break; 1756 case ICMP_ADDRESS_MASK_REPLY: 1757 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps); 1758 break; 1759 default: 1760 interested = B_TRUE; /* Pass up to transport */ 1761 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns); 1762 break; 1763 } 1764 /* See if there is an ICMP client. */ 1765 if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) { 1766 /* If there is an ICMP client and we want one too, copy it. */ 1767 mblk_t *first_mp1; 1768 1769 if (!interested) { 1770 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1771 ip_policy, recv_ill, zoneid); 1772 return; 1773 } 1774 first_mp1 = ip_copymsg(first_mp); 1775 if (first_mp1 != NULL) { 1776 ip_fanout_proto(q, first_mp1, ill, ipha, 1777 0, mctl_present, ip_policy, recv_ill, zoneid); 1778 } 1779 } else if (!interested) { 1780 freemsg(first_mp); 1781 return; 1782 } else { 1783 /* 1784 * Initiate policy processing for this packet if ip_policy 1785 * is true. 1786 */ 1787 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 1788 ill_index = ill->ill_phyint->phyint_ifindex; 1789 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1790 if (mp == NULL) { 1791 if (mctl_present) { 1792 freeb(first_mp); 1793 } 1794 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1795 return; 1796 } 1797 } 1798 } 1799 /* We want to do something with it. */ 1800 /* Check db_ref to make sure we can modify the packet. */ 1801 if (mp->b_datap->db_ref > 1) { 1802 mblk_t *first_mp1; 1803 1804 first_mp1 = ip_copymsg(first_mp); 1805 freemsg(first_mp); 1806 if (!first_mp1) { 1807 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1808 return; 1809 } 1810 first_mp = first_mp1; 1811 if (mctl_present) { 1812 mp = first_mp->b_cont; 1813 ASSERT(mp != NULL); 1814 } else { 1815 mp = first_mp; 1816 } 1817 ipha = (ipha_t *)mp->b_rptr; 1818 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1819 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1820 } 1821 switch (icmph->icmph_type) { 1822 case ICMP_ADDRESS_MASK_REQUEST: 1823 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1824 if (ipif == NULL) { 1825 freemsg(first_mp); 1826 return; 1827 } 1828 /* 1829 * outging interface must be IPv4 1830 */ 1831 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1832 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1833 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1834 ipif_refrele(ipif); 1835 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps); 1836 break; 1837 case ICMP_ECHO_REQUEST: 1838 icmph->icmph_type = ICMP_ECHO_REPLY; 1839 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps); 1840 break; 1841 case ICMP_TIME_STAMP_REQUEST: { 1842 uint32_t *tsp; 1843 1844 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1845 tsp = (uint32_t *)wptr; 1846 tsp++; /* Skip past 'originate time' */ 1847 /* Compute # of milliseconds since midnight */ 1848 gethrestime(&now); 1849 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1850 now.tv_nsec / (NANOSEC / MILLISEC); 1851 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1852 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1853 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps); 1854 break; 1855 } 1856 default: 1857 ipha = (ipha_t *)&icmph[1]; 1858 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1859 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1860 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1861 freemsg(first_mp); 1862 return; 1863 } 1864 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1865 ipha = (ipha_t *)&icmph[1]; 1866 } 1867 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1868 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1869 freemsg(first_mp); 1870 return; 1871 } 1872 hdr_length = IPH_HDR_LENGTH(ipha); 1873 if (hdr_length < sizeof (ipha_t)) { 1874 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1875 freemsg(first_mp); 1876 return; 1877 } 1878 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 1879 if (!pullupmsg(mp, 1880 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 1881 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1882 freemsg(first_mp); 1883 return; 1884 } 1885 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1886 ipha = (ipha_t *)&icmph[1]; 1887 } 1888 switch (icmph->icmph_type) { 1889 case ICMP_REDIRECT: 1890 /* 1891 * As there is no upper client to deliver, we don't 1892 * need the first_mp any more. 1893 */ 1894 if (mctl_present) { 1895 freeb(first_mp); 1896 } 1897 icmp_redirect(ill, mp); 1898 return; 1899 case ICMP_DEST_UNREACHABLE: 1900 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 1901 if (!icmp_inbound_too_big(icmph, ipha, ill, 1902 zoneid, mp, iph_hdr_length, ipst)) { 1903 freemsg(first_mp); 1904 return; 1905 } 1906 /* 1907 * icmp_inbound_too_big() may alter mp. 1908 * Resynch ipha and icmph accordingly. 1909 */ 1910 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1911 ipha = (ipha_t *)&icmph[1]; 1912 } 1913 /* FALLTHRU */ 1914 default : 1915 /* 1916 * IPQoS notes: Since we have already done IPQoS 1917 * processing we don't want to do it again in 1918 * the fanout routines called by 1919 * icmp_inbound_error_fanout, hence the last 1920 * argument, ip_policy, is B_FALSE. 1921 */ 1922 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 1923 ipha, iph_hdr_length, hdr_length, mctl_present, 1924 B_FALSE, recv_ill, zoneid); 1925 } 1926 return; 1927 } 1928 /* Send out an ICMP packet */ 1929 icmph->icmph_checksum = 0; 1930 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 1931 if (icmph->icmph_checksum == 0) 1932 icmph->icmph_checksum = 0xFFFF; 1933 if (broadcast || CLASSD(ipha->ipha_dst)) { 1934 ipif_t *ipif_chosen; 1935 /* 1936 * Make it look like it was directed to us, so we don't look 1937 * like a fool with a broadcast or multicast source address. 1938 */ 1939 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1940 /* 1941 * Make sure that we haven't grabbed an interface that's DOWN. 1942 */ 1943 if (ipif != NULL) { 1944 ipif_chosen = ipif_select_source(ipif->ipif_ill, 1945 ipha->ipha_src, zoneid); 1946 if (ipif_chosen != NULL) { 1947 ipif_refrele(ipif); 1948 ipif = ipif_chosen; 1949 } 1950 } 1951 if (ipif == NULL) { 1952 ip0dbg(("icmp_inbound: " 1953 "No source for broadcast/multicast:\n" 1954 "\tsrc 0x%x dst 0x%x ill %p " 1955 "ipif_lcl_addr 0x%x\n", 1956 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 1957 (void *)ill, 1958 ill->ill_ipif->ipif_lcl_addr)); 1959 freemsg(first_mp); 1960 return; 1961 } 1962 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1963 ipha->ipha_dst = ipif->ipif_src_addr; 1964 ipif_refrele(ipif); 1965 } 1966 /* Reset time to live. */ 1967 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 1968 { 1969 /* Swap source and destination addresses */ 1970 ipaddr_t tmp; 1971 1972 tmp = ipha->ipha_src; 1973 ipha->ipha_src = ipha->ipha_dst; 1974 ipha->ipha_dst = tmp; 1975 } 1976 ipha->ipha_ident = 0; 1977 if (!IS_SIMPLE_IPH(ipha)) 1978 icmp_options_update(ipha); 1979 1980 /* 1981 * ICMP echo replies should go out on the same interface 1982 * the request came on as probes used by in.mpathd for detecting 1983 * NIC failures are ECHO packets. We turn-off load spreading 1984 * by setting ipsec_in_attach_if to B_TRUE, which is copied 1985 * to ipsec_out_attach_if by ipsec_in_to_out called later in this 1986 * function. This is in turn handled by ip_wput and ip_newroute 1987 * to make sure that the packet goes out on the interface it came 1988 * in on. If we don't turnoff load spreading, the packets might get 1989 * dropped if there are no non-FAILED/INACTIVE interfaces for it 1990 * to go out and in.mpathd would wrongly detect a failure or 1991 * mis-detect a NIC failure for link failure. As load spreading 1992 * can happen only if ill_group is not NULL, we do only for 1993 * that case and this does not affect the normal case. 1994 * 1995 * We turn off load spreading only on echo packets that came from 1996 * on-link hosts. If the interface route has been deleted, this will 1997 * not be enforced as we can't do much. For off-link hosts, as the 1998 * default routes in IPv4 does not typically have an ire_ipif 1999 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. 2000 * Moreover, expecting a default route through this interface may 2001 * not be correct. We use ipha_dst because of the swap above. 2002 */ 2003 onlink = B_FALSE; 2004 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { 2005 /* 2006 * First, we need to make sure that it is not one of our 2007 * local addresses. If we set onlink when it is one of 2008 * our local addresses, we will end up creating IRE_CACHES 2009 * for one of our local addresses. Then, we will never 2010 * accept packets for them afterwards. 2011 */ 2012 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, 2013 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2014 if (src_ire == NULL) { 2015 ipif = ipif_get_next_ipif(NULL, ill); 2016 if (ipif == NULL) { 2017 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2018 freemsg(mp); 2019 return; 2020 } 2021 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 2022 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 2023 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE, ipst); 2024 ipif_refrele(ipif); 2025 if (src_ire != NULL) { 2026 onlink = B_TRUE; 2027 ire_refrele(src_ire); 2028 } 2029 } else { 2030 ire_refrele(src_ire); 2031 } 2032 } 2033 if (!mctl_present) { 2034 /* 2035 * This packet should go out the same way as it 2036 * came in i.e in clear. To make sure that global 2037 * policy will not be applied to this in ip_wput_ire, 2038 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2039 */ 2040 ASSERT(first_mp == mp); 2041 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2042 if (first_mp == NULL) { 2043 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2044 freemsg(mp); 2045 return; 2046 } 2047 ii = (ipsec_in_t *)first_mp->b_rptr; 2048 2049 /* This is not a secure packet */ 2050 ii->ipsec_in_secure = B_FALSE; 2051 if (onlink) { 2052 ii->ipsec_in_attach_if = B_TRUE; 2053 ii->ipsec_in_ill_index = 2054 ill->ill_phyint->phyint_ifindex; 2055 ii->ipsec_in_rill_index = 2056 recv_ill->ill_phyint->phyint_ifindex; 2057 } 2058 first_mp->b_cont = mp; 2059 } else if (onlink) { 2060 ii = (ipsec_in_t *)first_mp->b_rptr; 2061 ii->ipsec_in_attach_if = B_TRUE; 2062 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 2063 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; 2064 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2065 } else { 2066 ii = (ipsec_in_t *)first_mp->b_rptr; 2067 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2068 } 2069 ii->ipsec_in_zoneid = zoneid; 2070 ASSERT(zoneid != ALL_ZONES); 2071 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2072 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2073 return; 2074 } 2075 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 2076 put(WR(q), first_mp); 2077 } 2078 2079 static ipaddr_t 2080 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) 2081 { 2082 conn_t *connp; 2083 connf_t *connfp; 2084 ipaddr_t nexthop_addr = INADDR_ANY; 2085 int hdr_length = IPH_HDR_LENGTH(ipha); 2086 uint16_t *up; 2087 uint32_t ports; 2088 ip_stack_t *ipst = ill->ill_ipst; 2089 2090 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2091 switch (ipha->ipha_protocol) { 2092 case IPPROTO_TCP: 2093 { 2094 tcph_t *tcph; 2095 2096 /* do a reverse lookup */ 2097 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2098 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, 2099 TCPS_LISTEN, ipst); 2100 break; 2101 } 2102 case IPPROTO_UDP: 2103 { 2104 uint32_t dstport, srcport; 2105 2106 ((uint16_t *)&ports)[0] = up[1]; 2107 ((uint16_t *)&ports)[1] = up[0]; 2108 2109 /* Extract ports in net byte order */ 2110 dstport = htons(ntohl(ports) & 0xFFFF); 2111 srcport = htons(ntohl(ports) >> 16); 2112 2113 connfp = &ipst->ips_ipcl_udp_fanout[ 2114 IPCL_UDP_HASH(dstport, ipst)]; 2115 mutex_enter(&connfp->connf_lock); 2116 connp = connfp->connf_head; 2117 2118 /* do a reverse lookup */ 2119 while ((connp != NULL) && 2120 (!IPCL_UDP_MATCH(connp, dstport, 2121 ipha->ipha_src, srcport, ipha->ipha_dst) || 2122 !IPCL_ZONE_MATCH(connp, zoneid))) { 2123 connp = connp->conn_next; 2124 } 2125 if (connp != NULL) 2126 CONN_INC_REF(connp); 2127 mutex_exit(&connfp->connf_lock); 2128 break; 2129 } 2130 case IPPROTO_SCTP: 2131 { 2132 in6_addr_t map_src, map_dst; 2133 2134 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); 2135 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); 2136 ((uint16_t *)&ports)[0] = up[1]; 2137 ((uint16_t *)&ports)[1] = up[0]; 2138 2139 connp = sctp_find_conn(&map_src, &map_dst, ports, 2140 zoneid, ipst->ips_netstack->netstack_sctp); 2141 if (connp == NULL) { 2142 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, 2143 zoneid, ports, ipha, ipst); 2144 } else { 2145 CONN_INC_REF(connp); 2146 SCTP_REFRELE(CONN2SCTP(connp)); 2147 } 2148 break; 2149 } 2150 default: 2151 { 2152 ipha_t ripha; 2153 2154 ripha.ipha_src = ipha->ipha_dst; 2155 ripha.ipha_dst = ipha->ipha_src; 2156 ripha.ipha_protocol = ipha->ipha_protocol; 2157 2158 connfp = &ipst->ips_ipcl_proto_fanout[ 2159 ipha->ipha_protocol]; 2160 mutex_enter(&connfp->connf_lock); 2161 connp = connfp->connf_head; 2162 for (connp = connfp->connf_head; connp != NULL; 2163 connp = connp->conn_next) { 2164 if (IPCL_PROTO_MATCH(connp, 2165 ipha->ipha_protocol, &ripha, ill, 2166 0, zoneid)) { 2167 CONN_INC_REF(connp); 2168 break; 2169 } 2170 } 2171 mutex_exit(&connfp->connf_lock); 2172 } 2173 } 2174 if (connp != NULL) { 2175 if (connp->conn_nexthop_set) 2176 nexthop_addr = connp->conn_nexthop_v4; 2177 CONN_DEC_REF(connp); 2178 } 2179 return (nexthop_addr); 2180 } 2181 2182 /* Table from RFC 1191 */ 2183 static int icmp_frag_size_table[] = 2184 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2185 2186 /* 2187 * Process received ICMP Packet too big. 2188 * After updating any IRE it does the fanout to any matching transport streams. 2189 * Assumes the message has been pulled up till the IP header that caused 2190 * the error. 2191 * 2192 * Returns B_FALSE on failure and B_TRUE on success. 2193 */ 2194 static boolean_t 2195 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, 2196 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length, 2197 ip_stack_t *ipst) 2198 { 2199 ire_t *ire, *first_ire; 2200 int mtu; 2201 int hdr_length; 2202 ipaddr_t nexthop_addr; 2203 2204 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 2205 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 2206 ASSERT(ill != NULL); 2207 2208 hdr_length = IPH_HDR_LENGTH(ipha); 2209 2210 /* Drop if the original packet contained a source route */ 2211 if (ip_source_route_included(ipha)) { 2212 return (B_FALSE); 2213 } 2214 /* 2215 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport 2216 * header. 2217 */ 2218 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2219 mp->b_wptr) { 2220 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2221 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2222 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2223 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n")); 2224 return (B_FALSE); 2225 } 2226 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2227 ipha = (ipha_t *)&icmph[1]; 2228 } 2229 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp); 2230 if (nexthop_addr != INADDR_ANY) { 2231 /* nexthop set */ 2232 first_ire = ire_ctable_lookup(ipha->ipha_dst, 2233 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp), 2234 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, ipst); 2235 } else { 2236 /* nexthop not set */ 2237 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, 2238 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2239 } 2240 2241 if (!first_ire) { 2242 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", 2243 ntohl(ipha->ipha_dst))); 2244 return (B_FALSE); 2245 } 2246 /* Check for MTU discovery advice as described in RFC 1191 */ 2247 mtu = ntohs(icmph->icmph_du_mtu); 2248 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2249 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2250 ire = ire->ire_next) { 2251 /* 2252 * Look for the connection to which this ICMP message is 2253 * directed. If it has the IP_NEXTHOP option set, then the 2254 * search is limited to IREs with the MATCH_IRE_PRIVATE 2255 * option. Else the search is limited to regular IREs. 2256 */ 2257 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2258 (nexthop_addr != ire->ire_gateway_addr)) || 2259 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2260 (nexthop_addr != INADDR_ANY))) 2261 continue; 2262 2263 mutex_enter(&ire->ire_lock); 2264 if (icmph->icmph_du_zero == 0 && mtu > 68) { 2265 /* Reduce the IRE max frag value as advised. */ 2266 ip1dbg(("Received mtu from router: %d (was %d)\n", 2267 mtu, ire->ire_max_frag)); 2268 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2269 } else { 2270 uint32_t length; 2271 int i; 2272 2273 /* 2274 * Use the table from RFC 1191 to figure out 2275 * the next "plateau" based on the length in 2276 * the original IP packet. 2277 */ 2278 length = ntohs(ipha->ipha_length); 2279 if (ire->ire_max_frag <= length && 2280 ire->ire_max_frag >= length - hdr_length) { 2281 /* 2282 * Handle broken BSD 4.2 systems that 2283 * return the wrong iph_length in ICMP 2284 * errors. 2285 */ 2286 ip1dbg(("Wrong mtu: sent %d, ire %d\n", 2287 length, ire->ire_max_frag)); 2288 length -= hdr_length; 2289 } 2290 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2291 if (length > icmp_frag_size_table[i]) 2292 break; 2293 } 2294 if (i == A_CNT(icmp_frag_size_table)) { 2295 /* Smaller than 68! */ 2296 ip1dbg(("Too big for packet size %d\n", 2297 length)); 2298 ire->ire_max_frag = MIN(ire->ire_max_frag, 576); 2299 ire->ire_frag_flag = 0; 2300 } else { 2301 mtu = icmp_frag_size_table[i]; 2302 ip1dbg(("Calculated mtu %d, packet size %d, " 2303 "before %d", mtu, length, 2304 ire->ire_max_frag)); 2305 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2306 ip1dbg((", after %d\n", ire->ire_max_frag)); 2307 } 2308 /* Record the new max frag size for the ULP. */ 2309 icmph->icmph_du_zero = 0; 2310 icmph->icmph_du_mtu = 2311 htons((uint16_t)ire->ire_max_frag); 2312 } 2313 mutex_exit(&ire->ire_lock); 2314 } 2315 rw_exit(&first_ire->ire_bucket->irb_lock); 2316 ire_refrele(first_ire); 2317 return (B_TRUE); 2318 } 2319 2320 /* 2321 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2322 * calls this function. 2323 */ 2324 static mblk_t * 2325 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2326 { 2327 ipha_t *ipha; 2328 icmph_t *icmph; 2329 ipha_t *in_ipha; 2330 int length; 2331 2332 ASSERT(mp->b_datap->db_type == M_DATA); 2333 2334 /* 2335 * For Self-encapsulated packets, we added an extra IP header 2336 * without the options. Inner IP header is the one from which 2337 * the outer IP header was formed. Thus, we need to remove the 2338 * outer IP header. To do this, we pullup the whole message 2339 * and overlay whatever follows the outer IP header over the 2340 * outer IP header. 2341 */ 2342 2343 if (!pullupmsg(mp, -1)) 2344 return (NULL); 2345 2346 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2347 ipha = (ipha_t *)&icmph[1]; 2348 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2349 2350 /* 2351 * The length that we want to overlay is following the inner 2352 * IP header. Subtracting the IP header + icmp header + outer 2353 * IP header's length should give us the length that we want to 2354 * overlay. 2355 */ 2356 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2357 hdr_length; 2358 /* 2359 * Overlay whatever follows the inner header over the 2360 * outer header. 2361 */ 2362 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2363 2364 /* Set the wptr to account for the outer header */ 2365 mp->b_wptr -= hdr_length; 2366 return (mp); 2367 } 2368 2369 /* 2370 * Try to pass the ICMP message upstream in case the ULP cares. 2371 * 2372 * If the packet that caused the ICMP error is secure, we send 2373 * it to AH/ESP to make sure that the attached packet has a 2374 * valid association. ipha in the code below points to the 2375 * IP header of the packet that caused the error. 2376 * 2377 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2378 * in the context of IPSEC. Normally we tell the upper layer 2379 * whenever we send the ire (including ip_bind), the IPSEC header 2380 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2381 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2382 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2383 * same thing. As TCP has the IPSEC options size that needs to be 2384 * adjusted, we just pass the MTU unchanged. 2385 * 2386 * IFN could have been generated locally or by some router. 2387 * 2388 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2389 * This happens because IP adjusted its value of MTU on an 2390 * earlier IFN message and could not tell the upper layer, 2391 * the new adjusted value of MTU e.g. Packet was encrypted 2392 * or there was not enough information to fanout to upper 2393 * layers. Thus on the next outbound datagram, ip_wput_ire 2394 * generates the IFN, where IPSEC processing has *not* been 2395 * done. 2396 * 2397 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2398 * could have generated this. This happens because ire_max_frag 2399 * value in IP was set to a new value, while the IPSEC processing 2400 * was being done and after we made the fragmentation check in 2401 * ip_wput_ire. Thus on return from IPSEC processing, 2402 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2403 * and generates the IFN. As IPSEC processing is over, we fanout 2404 * to AH/ESP to remove the header. 2405 * 2406 * In both these cases, ipsec_in_loopback will be set indicating 2407 * that IFN was generated locally. 2408 * 2409 * ROUTER : IFN could be secure or non-secure. 2410 * 2411 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2412 * packet in error has AH/ESP headers to validate the AH/ESP 2413 * headers. AH/ESP will verify whether there is a valid SA or 2414 * not and send it back. We will fanout again if we have more 2415 * data in the packet. 2416 * 2417 * If the packet in error does not have AH/ESP, we handle it 2418 * like any other case. 2419 * 2420 * * NON_SECURE : If the packet in error has AH/ESP headers, 2421 * we attach a dummy ipsec_in and send it up to AH/ESP 2422 * for validation. AH/ESP will verify whether there is a 2423 * valid SA or not and send it back. We will fanout again if 2424 * we have more data in the packet. 2425 * 2426 * If the packet in error does not have AH/ESP, we handle it 2427 * like any other case. 2428 */ 2429 static void 2430 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2431 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2432 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2433 zoneid_t zoneid) 2434 { 2435 uint16_t *up; /* Pointer to ports in ULP header */ 2436 uint32_t ports; /* reversed ports for fanout */ 2437 ipha_t ripha; /* With reversed addresses */ 2438 mblk_t *first_mp; 2439 ipsec_in_t *ii; 2440 tcph_t *tcph; 2441 conn_t *connp; 2442 ip_stack_t *ipst; 2443 2444 ASSERT(ill != NULL); 2445 2446 ASSERT(recv_ill != NULL); 2447 ipst = recv_ill->ill_ipst; 2448 2449 first_mp = mp; 2450 if (mctl_present) { 2451 mp = first_mp->b_cont; 2452 ASSERT(mp != NULL); 2453 2454 ii = (ipsec_in_t *)first_mp->b_rptr; 2455 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2456 } else { 2457 ii = NULL; 2458 } 2459 2460 switch (ipha->ipha_protocol) { 2461 case IPPROTO_UDP: 2462 /* 2463 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2464 * transport header. 2465 */ 2466 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2467 mp->b_wptr) { 2468 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2469 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2470 goto discard_pkt; 2471 } 2472 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2473 ipha = (ipha_t *)&icmph[1]; 2474 } 2475 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2476 2477 /* 2478 * Attempt to find a client stream based on port. 2479 * Note that we do a reverse lookup since the header is 2480 * in the form we sent it out. 2481 * The ripha header is only used for the IP_UDP_MATCH and we 2482 * only set the src and dst addresses and protocol. 2483 */ 2484 ripha.ipha_src = ipha->ipha_dst; 2485 ripha.ipha_dst = ipha->ipha_src; 2486 ripha.ipha_protocol = ipha->ipha_protocol; 2487 ((uint16_t *)&ports)[0] = up[1]; 2488 ((uint16_t *)&ports)[1] = up[0]; 2489 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2490 ntohl(ipha->ipha_src), ntohs(up[0]), 2491 ntohl(ipha->ipha_dst), ntohs(up[1]), 2492 icmph->icmph_type, icmph->icmph_code)); 2493 2494 /* Have to change db_type after any pullupmsg */ 2495 DB_TYPE(mp) = M_CTL; 2496 2497 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2498 mctl_present, ip_policy, recv_ill, zoneid); 2499 return; 2500 2501 case IPPROTO_TCP: 2502 /* 2503 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2504 * transport header. 2505 */ 2506 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2507 mp->b_wptr) { 2508 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2509 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2510 goto discard_pkt; 2511 } 2512 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2513 ipha = (ipha_t *)&icmph[1]; 2514 } 2515 /* 2516 * Find a TCP client stream for this packet. 2517 * Note that we do a reverse lookup since the header is 2518 * in the form we sent it out. 2519 */ 2520 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2521 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN, 2522 ipst); 2523 if (connp == NULL) 2524 goto discard_pkt; 2525 2526 /* Have to change db_type after any pullupmsg */ 2527 DB_TYPE(mp) = M_CTL; 2528 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 2529 connp, SQTAG_TCP_INPUT_ICMP_ERR); 2530 return; 2531 2532 case IPPROTO_SCTP: 2533 /* 2534 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2535 * transport header. 2536 */ 2537 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2538 mp->b_wptr) { 2539 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2540 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2541 goto discard_pkt; 2542 } 2543 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2544 ipha = (ipha_t *)&icmph[1]; 2545 } 2546 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2547 /* 2548 * Find a SCTP client stream for this packet. 2549 * Note that we do a reverse lookup since the header is 2550 * in the form we sent it out. 2551 * The ripha header is only used for the matching and we 2552 * only set the src and dst addresses, protocol, and version. 2553 */ 2554 ripha.ipha_src = ipha->ipha_dst; 2555 ripha.ipha_dst = ipha->ipha_src; 2556 ripha.ipha_protocol = ipha->ipha_protocol; 2557 ripha.ipha_version_and_hdr_length = 2558 ipha->ipha_version_and_hdr_length; 2559 ((uint16_t *)&ports)[0] = up[1]; 2560 ((uint16_t *)&ports)[1] = up[0]; 2561 2562 /* Have to change db_type after any pullupmsg */ 2563 DB_TYPE(mp) = M_CTL; 2564 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2565 mctl_present, ip_policy, zoneid); 2566 return; 2567 2568 case IPPROTO_ESP: 2569 case IPPROTO_AH: { 2570 int ipsec_rc; 2571 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 2572 2573 /* 2574 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2575 * We will re-use the IPSEC_IN if it is already present as 2576 * AH/ESP will not affect any fields in the IPSEC_IN for 2577 * ICMP errors. If there is no IPSEC_IN, allocate a new 2578 * one and attach it in the front. 2579 */ 2580 if (ii != NULL) { 2581 /* 2582 * ip_fanout_proto_again converts the ICMP errors 2583 * that come back from AH/ESP to M_DATA so that 2584 * if it is non-AH/ESP and we do a pullupmsg in 2585 * this function, it would work. Convert it back 2586 * to M_CTL before we send up as this is a ICMP 2587 * error. This could have been generated locally or 2588 * by some router. Validate the inner IPSEC 2589 * headers. 2590 * 2591 * NOTE : ill_index is used by ip_fanout_proto_again 2592 * to locate the ill. 2593 */ 2594 ASSERT(ill != NULL); 2595 ii->ipsec_in_ill_index = 2596 ill->ill_phyint->phyint_ifindex; 2597 ii->ipsec_in_rill_index = 2598 recv_ill->ill_phyint->phyint_ifindex; 2599 DB_TYPE(first_mp->b_cont) = M_CTL; 2600 } else { 2601 /* 2602 * IPSEC_IN is not present. We attach a ipsec_in 2603 * message and send up to IPSEC for validating 2604 * and removing the IPSEC headers. Clear 2605 * ipsec_in_secure so that when we return 2606 * from IPSEC, we don't mistakenly think that this 2607 * is a secure packet came from the network. 2608 * 2609 * NOTE : ill_index is used by ip_fanout_proto_again 2610 * to locate the ill. 2611 */ 2612 ASSERT(first_mp == mp); 2613 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2614 if (first_mp == NULL) { 2615 freemsg(mp); 2616 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2617 return; 2618 } 2619 ii = (ipsec_in_t *)first_mp->b_rptr; 2620 2621 /* This is not a secure packet */ 2622 ii->ipsec_in_secure = B_FALSE; 2623 first_mp->b_cont = mp; 2624 DB_TYPE(mp) = M_CTL; 2625 ASSERT(ill != NULL); 2626 ii->ipsec_in_ill_index = 2627 ill->ill_phyint->phyint_ifindex; 2628 ii->ipsec_in_rill_index = 2629 recv_ill->ill_phyint->phyint_ifindex; 2630 } 2631 ip2dbg(("icmp_inbound_error: ipsec\n")); 2632 2633 if (!ipsec_loaded(ipss)) { 2634 ip_proto_not_sup(q, first_mp, 0, zoneid, ipst); 2635 return; 2636 } 2637 2638 if (ipha->ipha_protocol == IPPROTO_ESP) 2639 ipsec_rc = ipsecesp_icmp_error(first_mp); 2640 else 2641 ipsec_rc = ipsecah_icmp_error(first_mp); 2642 if (ipsec_rc == IPSEC_STATUS_FAILED) 2643 return; 2644 2645 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2646 return; 2647 } 2648 default: 2649 /* 2650 * The ripha header is only used for the lookup and we 2651 * only set the src and dst addresses and protocol. 2652 */ 2653 ripha.ipha_src = ipha->ipha_dst; 2654 ripha.ipha_dst = ipha->ipha_src; 2655 ripha.ipha_protocol = ipha->ipha_protocol; 2656 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2657 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2658 ntohl(ipha->ipha_dst), 2659 icmph->icmph_type, icmph->icmph_code)); 2660 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2661 ipha_t *in_ipha; 2662 2663 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2664 mp->b_wptr) { 2665 if (!pullupmsg(mp, (uchar_t *)ipha + 2666 hdr_length + sizeof (ipha_t) - 2667 mp->b_rptr)) { 2668 goto discard_pkt; 2669 } 2670 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2671 ipha = (ipha_t *)&icmph[1]; 2672 } 2673 /* 2674 * Caller has verified that length has to be 2675 * at least the size of IP header. 2676 */ 2677 ASSERT(hdr_length >= sizeof (ipha_t)); 2678 /* 2679 * Check the sanity of the inner IP header like 2680 * we did for the outer header. 2681 */ 2682 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2683 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2684 goto discard_pkt; 2685 } 2686 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2687 goto discard_pkt; 2688 } 2689 /* Check for Self-encapsulated tunnels */ 2690 if (in_ipha->ipha_src == ipha->ipha_src && 2691 in_ipha->ipha_dst == ipha->ipha_dst) { 2692 2693 mp = icmp_inbound_self_encap_error(mp, 2694 iph_hdr_length, hdr_length); 2695 if (mp == NULL) 2696 goto discard_pkt; 2697 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2698 ipha = (ipha_t *)&icmph[1]; 2699 hdr_length = IPH_HDR_LENGTH(ipha); 2700 /* 2701 * The packet in error is self-encapsualted. 2702 * And we are finding it further encapsulated 2703 * which we could not have possibly generated. 2704 */ 2705 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2706 goto discard_pkt; 2707 } 2708 icmp_inbound_error_fanout(q, ill, first_mp, 2709 icmph, ipha, iph_hdr_length, hdr_length, 2710 mctl_present, ip_policy, recv_ill, zoneid); 2711 return; 2712 } 2713 } 2714 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2715 ipha->ipha_protocol == IPPROTO_IPV6) && 2716 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2717 ii != NULL && 2718 ii->ipsec_in_loopback && 2719 ii->ipsec_in_secure) { 2720 /* 2721 * For IP tunnels that get a looped-back 2722 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2723 * reported new MTU to take into account the IPsec 2724 * headers protecting this configured tunnel. 2725 * 2726 * This allows the tunnel module (tun.c) to blindly 2727 * accept the MTU reported in an ICMP "too big" 2728 * message. 2729 * 2730 * Non-looped back ICMP messages will just be 2731 * handled by the security protocols (if needed), 2732 * and the first subsequent packet will hit this 2733 * path. 2734 */ 2735 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2736 ipsec_in_extra_length(first_mp)); 2737 } 2738 /* Have to change db_type after any pullupmsg */ 2739 DB_TYPE(mp) = M_CTL; 2740 2741 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2742 ip_policy, recv_ill, zoneid); 2743 return; 2744 } 2745 /* NOTREACHED */ 2746 discard_pkt: 2747 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2748 drop_pkt:; 2749 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2750 freemsg(first_mp); 2751 } 2752 2753 /* 2754 * Common IP options parser. 2755 * 2756 * Setup routine: fill in *optp with options-parsing state, then 2757 * tail-call ipoptp_next to return the first option. 2758 */ 2759 uint8_t 2760 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2761 { 2762 uint32_t totallen; /* total length of all options */ 2763 2764 totallen = ipha->ipha_version_and_hdr_length - 2765 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2766 totallen <<= 2; 2767 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2768 optp->ipoptp_end = optp->ipoptp_next + totallen; 2769 optp->ipoptp_flags = 0; 2770 return (ipoptp_next(optp)); 2771 } 2772 2773 /* 2774 * Common IP options parser: extract next option. 2775 */ 2776 uint8_t 2777 ipoptp_next(ipoptp_t *optp) 2778 { 2779 uint8_t *end = optp->ipoptp_end; 2780 uint8_t *cur = optp->ipoptp_next; 2781 uint8_t opt, len, pointer; 2782 2783 /* 2784 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2785 * has been corrupted. 2786 */ 2787 ASSERT(cur <= end); 2788 2789 if (cur == end) 2790 return (IPOPT_EOL); 2791 2792 opt = cur[IPOPT_OPTVAL]; 2793 2794 /* 2795 * Skip any NOP options. 2796 */ 2797 while (opt == IPOPT_NOP) { 2798 cur++; 2799 if (cur == end) 2800 return (IPOPT_EOL); 2801 opt = cur[IPOPT_OPTVAL]; 2802 } 2803 2804 if (opt == IPOPT_EOL) 2805 return (IPOPT_EOL); 2806 2807 /* 2808 * Option requiring a length. 2809 */ 2810 if ((cur + 1) >= end) { 2811 optp->ipoptp_flags |= IPOPTP_ERROR; 2812 return (IPOPT_EOL); 2813 } 2814 len = cur[IPOPT_OLEN]; 2815 if (len < 2) { 2816 optp->ipoptp_flags |= IPOPTP_ERROR; 2817 return (IPOPT_EOL); 2818 } 2819 optp->ipoptp_cur = cur; 2820 optp->ipoptp_len = len; 2821 optp->ipoptp_next = cur + len; 2822 if (cur + len > end) { 2823 optp->ipoptp_flags |= IPOPTP_ERROR; 2824 return (IPOPT_EOL); 2825 } 2826 2827 /* 2828 * For the options which require a pointer field, make sure 2829 * its there, and make sure it points to either something 2830 * inside this option, or the end of the option. 2831 */ 2832 switch (opt) { 2833 case IPOPT_RR: 2834 case IPOPT_TS: 2835 case IPOPT_LSRR: 2836 case IPOPT_SSRR: 2837 if (len <= IPOPT_OFFSET) { 2838 optp->ipoptp_flags |= IPOPTP_ERROR; 2839 return (opt); 2840 } 2841 pointer = cur[IPOPT_OFFSET]; 2842 if (pointer - 1 > len) { 2843 optp->ipoptp_flags |= IPOPTP_ERROR; 2844 return (opt); 2845 } 2846 break; 2847 } 2848 2849 /* 2850 * Sanity check the pointer field based on the type of the 2851 * option. 2852 */ 2853 switch (opt) { 2854 case IPOPT_RR: 2855 case IPOPT_SSRR: 2856 case IPOPT_LSRR: 2857 if (pointer < IPOPT_MINOFF_SR) 2858 optp->ipoptp_flags |= IPOPTP_ERROR; 2859 break; 2860 case IPOPT_TS: 2861 if (pointer < IPOPT_MINOFF_IT) 2862 optp->ipoptp_flags |= IPOPTP_ERROR; 2863 /* 2864 * Note that the Internet Timestamp option also 2865 * contains two four bit fields (the Overflow field, 2866 * and the Flag field), which follow the pointer 2867 * field. We don't need to check that these fields 2868 * fall within the length of the option because this 2869 * was implicitely done above. We've checked that the 2870 * pointer value is at least IPOPT_MINOFF_IT, and that 2871 * it falls within the option. Since IPOPT_MINOFF_IT > 2872 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2873 */ 2874 ASSERT(len > IPOPT_POS_OV_FLG); 2875 break; 2876 } 2877 2878 return (opt); 2879 } 2880 2881 /* 2882 * Use the outgoing IP header to create an IP_OPTIONS option the way 2883 * it was passed down from the application. 2884 */ 2885 int 2886 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) 2887 { 2888 ipoptp_t opts; 2889 const uchar_t *opt; 2890 uint8_t optval; 2891 uint8_t optlen; 2892 uint32_t len = 0; 2893 uchar_t *buf1 = buf; 2894 2895 buf += IP_ADDR_LEN; /* Leave room for final destination */ 2896 len += IP_ADDR_LEN; 2897 bzero(buf1, IP_ADDR_LEN); 2898 2899 /* 2900 * OK to cast away const here, as we don't store through the returned 2901 * opts.ipoptp_cur pointer. 2902 */ 2903 for (optval = ipoptp_first(&opts, (ipha_t *)ipha); 2904 optval != IPOPT_EOL; 2905 optval = ipoptp_next(&opts)) { 2906 int off; 2907 2908 opt = opts.ipoptp_cur; 2909 optlen = opts.ipoptp_len; 2910 switch (optval) { 2911 case IPOPT_SSRR: 2912 case IPOPT_LSRR: 2913 2914 /* 2915 * Insert ipha_dst as the first entry in the source 2916 * route and move down the entries on step. 2917 * The last entry gets placed at buf1. 2918 */ 2919 buf[IPOPT_OPTVAL] = optval; 2920 buf[IPOPT_OLEN] = optlen; 2921 buf[IPOPT_OFFSET] = optlen; 2922 2923 off = optlen - IP_ADDR_LEN; 2924 if (off < 0) { 2925 /* No entries in source route */ 2926 break; 2927 } 2928 /* Last entry in source route */ 2929 bcopy(opt + off, buf1, IP_ADDR_LEN); 2930 off -= IP_ADDR_LEN; 2931 2932 while (off > 0) { 2933 bcopy(opt + off, 2934 buf + off + IP_ADDR_LEN, 2935 IP_ADDR_LEN); 2936 off -= IP_ADDR_LEN; 2937 } 2938 /* ipha_dst into first slot */ 2939 bcopy(&ipha->ipha_dst, 2940 buf + off + IP_ADDR_LEN, 2941 IP_ADDR_LEN); 2942 buf += optlen; 2943 len += optlen; 2944 break; 2945 2946 case IPOPT_COMSEC: 2947 case IPOPT_SECURITY: 2948 /* if passing up a label is not ok, then remove */ 2949 if (is_system_labeled()) 2950 break; 2951 /* FALLTHROUGH */ 2952 default: 2953 bcopy(opt, buf, optlen); 2954 buf += optlen; 2955 len += optlen; 2956 break; 2957 } 2958 } 2959 done: 2960 /* Pad the resulting options */ 2961 while (len & 0x3) { 2962 *buf++ = IPOPT_EOL; 2963 len++; 2964 } 2965 return (len); 2966 } 2967 2968 /* 2969 * Update any record route or timestamp options to include this host. 2970 * Reverse any source route option. 2971 * This routine assumes that the options are well formed i.e. that they 2972 * have already been checked. 2973 */ 2974 static void 2975 icmp_options_update(ipha_t *ipha) 2976 { 2977 ipoptp_t opts; 2978 uchar_t *opt; 2979 uint8_t optval; 2980 ipaddr_t src; /* Our local address */ 2981 ipaddr_t dst; 2982 2983 ip2dbg(("icmp_options_update\n")); 2984 src = ipha->ipha_src; 2985 dst = ipha->ipha_dst; 2986 2987 for (optval = ipoptp_first(&opts, ipha); 2988 optval != IPOPT_EOL; 2989 optval = ipoptp_next(&opts)) { 2990 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 2991 opt = opts.ipoptp_cur; 2992 ip2dbg(("icmp_options_update: opt %d, len %d\n", 2993 optval, opts.ipoptp_len)); 2994 switch (optval) { 2995 int off1, off2; 2996 case IPOPT_SSRR: 2997 case IPOPT_LSRR: 2998 /* 2999 * Reverse the source route. The first entry 3000 * should be the next to last one in the current 3001 * source route (the last entry is our address). 3002 * The last entry should be the final destination. 3003 */ 3004 off1 = IPOPT_MINOFF_SR - 1; 3005 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 3006 if (off2 < 0) { 3007 /* No entries in source route */ 3008 ip1dbg(( 3009 "icmp_options_update: bad src route\n")); 3010 break; 3011 } 3012 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 3013 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 3014 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 3015 off2 -= IP_ADDR_LEN; 3016 3017 while (off1 < off2) { 3018 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 3019 bcopy((char *)opt + off2, (char *)opt + off1, 3020 IP_ADDR_LEN); 3021 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 3022 off1 += IP_ADDR_LEN; 3023 off2 -= IP_ADDR_LEN; 3024 } 3025 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 3026 break; 3027 } 3028 } 3029 } 3030 3031 /* 3032 * Process received ICMP Redirect messages. 3033 */ 3034 static void 3035 icmp_redirect(ill_t *ill, mblk_t *mp) 3036 { 3037 ipha_t *ipha; 3038 int iph_hdr_length; 3039 icmph_t *icmph; 3040 ipha_t *ipha_err; 3041 ire_t *ire; 3042 ire_t *prev_ire; 3043 ire_t *save_ire; 3044 ipaddr_t src, dst, gateway; 3045 iulp_t ulp_info = { 0 }; 3046 int error; 3047 ip_stack_t *ipst; 3048 3049 ASSERT(ill != NULL); 3050 ipst = ill->ill_ipst; 3051 3052 ipha = (ipha_t *)mp->b_rptr; 3053 iph_hdr_length = IPH_HDR_LENGTH(ipha); 3054 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 3055 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 3056 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3057 freemsg(mp); 3058 return; 3059 } 3060 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 3061 ipha_err = (ipha_t *)&icmph[1]; 3062 src = ipha->ipha_src; 3063 dst = ipha_err->ipha_dst; 3064 gateway = icmph->icmph_rd_gateway; 3065 /* Make sure the new gateway is reachable somehow. */ 3066 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 3067 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3068 /* 3069 * Make sure we had a route for the dest in question and that 3070 * that route was pointing to the old gateway (the source of the 3071 * redirect packet.) 3072 */ 3073 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 3074 NULL, MATCH_IRE_GW, ipst); 3075 /* 3076 * Check that 3077 * the redirect was not from ourselves 3078 * the new gateway and the old gateway are directly reachable 3079 */ 3080 if (!prev_ire || 3081 !ire || 3082 ire->ire_type == IRE_LOCAL) { 3083 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3084 freemsg(mp); 3085 if (ire != NULL) 3086 ire_refrele(ire); 3087 if (prev_ire != NULL) 3088 ire_refrele(prev_ire); 3089 return; 3090 } 3091 3092 /* 3093 * Should we use the old ULP info to create the new gateway? From 3094 * a user's perspective, we should inherit the info so that it 3095 * is a "smooth" transition. If we do not do that, then new 3096 * connections going thru the new gateway will have no route metrics, 3097 * which is counter-intuitive to user. From a network point of 3098 * view, this may or may not make sense even though the new gateway 3099 * is still directly connected to us so the route metrics should not 3100 * change much. 3101 * 3102 * But if the old ire_uinfo is not initialized, we do another 3103 * recursive lookup on the dest using the new gateway. There may 3104 * be a route to that. If so, use it to initialize the redirect 3105 * route. 3106 */ 3107 if (prev_ire->ire_uinfo.iulp_set) { 3108 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3109 } else { 3110 ire_t *tmp_ire; 3111 ire_t *sire; 3112 3113 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 3114 ALL_ZONES, 0, NULL, 3115 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT), 3116 ipst); 3117 if (sire != NULL) { 3118 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3119 /* 3120 * If sire != NULL, ire_ftable_lookup() should not 3121 * return a NULL value. 3122 */ 3123 ASSERT(tmp_ire != NULL); 3124 ire_refrele(tmp_ire); 3125 ire_refrele(sire); 3126 } else if (tmp_ire != NULL) { 3127 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 3128 sizeof (iulp_t)); 3129 ire_refrele(tmp_ire); 3130 } 3131 } 3132 if (prev_ire->ire_type == IRE_CACHE) 3133 ire_delete(prev_ire); 3134 ire_refrele(prev_ire); 3135 /* 3136 * TODO: more precise handling for cases 0, 2, 3, the latter two 3137 * require TOS routing 3138 */ 3139 switch (icmph->icmph_code) { 3140 case 0: 3141 case 1: 3142 /* TODO: TOS specificity for cases 2 and 3 */ 3143 case 2: 3144 case 3: 3145 break; 3146 default: 3147 freemsg(mp); 3148 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3149 ire_refrele(ire); 3150 return; 3151 } 3152 /* 3153 * Create a Route Association. This will allow us to remember that 3154 * someone we believe told us to use the particular gateway. 3155 */ 3156 save_ire = ire; 3157 ire = ire_create( 3158 (uchar_t *)&dst, /* dest addr */ 3159 (uchar_t *)&ip_g_all_ones, /* mask */ 3160 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 3161 (uchar_t *)&gateway, /* gateway addr */ 3162 NULL, /* no in_srcaddr */ 3163 &save_ire->ire_max_frag, /* max frag */ 3164 NULL, /* Fast Path header */ 3165 NULL, /* no rfq */ 3166 NULL, /* no stq */ 3167 IRE_HOST, 3168 NULL, 3169 NULL, 3170 NULL, 3171 0, 3172 0, 3173 0, 3174 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 3175 &ulp_info, 3176 NULL, 3177 NULL, 3178 ipst); 3179 3180 if (ire == NULL) { 3181 freemsg(mp); 3182 ire_refrele(save_ire); 3183 return; 3184 } 3185 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); 3186 ire_refrele(save_ire); 3187 atomic_inc_32(&ipst->ips_ip_redirect_cnt); 3188 3189 if (error == 0) { 3190 ire_refrele(ire); /* Held in ire_add_v4 */ 3191 /* tell routing sockets that we received a redirect */ 3192 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 3193 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 3194 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst); 3195 } 3196 3197 /* 3198 * Delete any existing IRE_HOST type redirect ires for this destination. 3199 * This together with the added IRE has the effect of 3200 * modifying an existing redirect. 3201 */ 3202 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL, 3203 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst); 3204 if (prev_ire != NULL) { 3205 if (prev_ire ->ire_flags & RTF_DYNAMIC) 3206 ire_delete(prev_ire); 3207 ire_refrele(prev_ire); 3208 } 3209 3210 freemsg(mp); 3211 } 3212 3213 /* 3214 * Generate an ICMP parameter problem message. 3215 */ 3216 static void 3217 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid, 3218 ip_stack_t *ipst) 3219 { 3220 icmph_t icmph; 3221 boolean_t mctl_present; 3222 mblk_t *first_mp; 3223 3224 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3225 3226 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3227 if (mctl_present) 3228 freeb(first_mp); 3229 return; 3230 } 3231 3232 bzero(&icmph, sizeof (icmph_t)); 3233 icmph.icmph_type = ICMP_PARAM_PROBLEM; 3234 icmph.icmph_pp_ptr = ptr; 3235 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs); 3236 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3237 ipst); 3238 } 3239 3240 /* 3241 * Build and ship an IPv4 ICMP message using the packet data in mp, and 3242 * the ICMP header pointed to by "stuff". (May be called as writer.) 3243 * Note: assumes that icmp_pkt_err_ok has been called to verify that 3244 * an icmp error packet can be sent. 3245 * Assigns an appropriate source address to the packet. If ipha_dst is 3246 * one of our addresses use it for source. Otherwise pick a source based 3247 * on a route lookup back to ipha_src. 3248 * Note that ipha_src must be set here since the 3249 * packet is likely to arrive on an ill queue in ip_wput() which will 3250 * not set a source address. 3251 */ 3252 static void 3253 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, 3254 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 3255 { 3256 ipaddr_t dst; 3257 icmph_t *icmph; 3258 ipha_t *ipha; 3259 uint_t len_needed; 3260 size_t msg_len; 3261 mblk_t *mp1; 3262 ipaddr_t src; 3263 ire_t *ire; 3264 mblk_t *ipsec_mp; 3265 ipsec_out_t *io = NULL; 3266 boolean_t xmit_if_on = B_FALSE; 3267 3268 if (mctl_present) { 3269 /* 3270 * If it is : 3271 * 3272 * 1) a IPSEC_OUT, then this is caused by outbound 3273 * datagram originating on this host. IPSEC processing 3274 * may or may not have been done. Refer to comments above 3275 * icmp_inbound_error_fanout for details. 3276 * 3277 * 2) a IPSEC_IN if we are generating a icmp_message 3278 * for an incoming datagram destined for us i.e called 3279 * from ip_fanout_send_icmp. 3280 */ 3281 ipsec_info_t *in; 3282 ipsec_mp = mp; 3283 mp = ipsec_mp->b_cont; 3284 3285 in = (ipsec_info_t *)ipsec_mp->b_rptr; 3286 ipha = (ipha_t *)mp->b_rptr; 3287 3288 ASSERT(in->ipsec_info_type == IPSEC_OUT || 3289 in->ipsec_info_type == IPSEC_IN); 3290 3291 if (in->ipsec_info_type == IPSEC_IN) { 3292 /* 3293 * Convert the IPSEC_IN to IPSEC_OUT. 3294 */ 3295 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3296 BUMP_MIB(&ipst->ips_ip_mib, 3297 ipIfStatsOutDiscards); 3298 return; 3299 } 3300 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3301 } else { 3302 ASSERT(in->ipsec_info_type == IPSEC_OUT); 3303 io = (ipsec_out_t *)in; 3304 if (io->ipsec_out_xmit_if) 3305 xmit_if_on = B_TRUE; 3306 /* 3307 * Clear out ipsec_out_proc_begin, so we do a fresh 3308 * ire lookup. 3309 */ 3310 io->ipsec_out_proc_begin = B_FALSE; 3311 } 3312 ASSERT(zoneid == io->ipsec_out_zoneid); 3313 ASSERT(zoneid != ALL_ZONES); 3314 } else { 3315 /* 3316 * This is in clear. The icmp message we are building 3317 * here should go out in clear. 3318 * 3319 * Pardon the convolution of it all, but it's easier to 3320 * allocate a "use cleartext" IPSEC_IN message and convert 3321 * it than it is to allocate a new one. 3322 */ 3323 ipsec_in_t *ii; 3324 ASSERT(DB_TYPE(mp) == M_DATA); 3325 ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 3326 if (ipsec_mp == NULL) { 3327 freemsg(mp); 3328 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3329 return; 3330 } 3331 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3332 3333 /* This is not a secure packet */ 3334 ii->ipsec_in_secure = B_FALSE; 3335 /* 3336 * For trusted extensions using a shared IP address we can 3337 * send using any zoneid. 3338 */ 3339 if (zoneid == ALL_ZONES) 3340 ii->ipsec_in_zoneid = GLOBAL_ZONEID; 3341 else 3342 ii->ipsec_in_zoneid = zoneid; 3343 ipsec_mp->b_cont = mp; 3344 ipha = (ipha_t *)mp->b_rptr; 3345 /* 3346 * Convert the IPSEC_IN to IPSEC_OUT. 3347 */ 3348 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3349 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3350 return; 3351 } 3352 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3353 } 3354 3355 /* Remember our eventual destination */ 3356 dst = ipha->ipha_src; 3357 3358 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 3359 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst); 3360 if (ire != NULL && 3361 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) { 3362 src = ipha->ipha_dst; 3363 } else if (!xmit_if_on) { 3364 if (ire != NULL) 3365 ire_refrele(ire); 3366 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL, 3367 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY), 3368 ipst); 3369 if (ire == NULL) { 3370 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3371 freemsg(ipsec_mp); 3372 return; 3373 } 3374 src = ire->ire_src_addr; 3375 } else { 3376 ipif_t *ipif = NULL; 3377 ill_t *ill; 3378 /* 3379 * This must be an ICMP error coming from 3380 * ip_mrtun_forward(). The src addr should 3381 * be equal to the IP-addr of the outgoing 3382 * interface. 3383 */ 3384 if (io == NULL) { 3385 /* This is not a IPSEC_OUT type control msg */ 3386 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3387 freemsg(ipsec_mp); 3388 return; 3389 } 3390 ill = ill_lookup_on_ifindex(io->ipsec_out_ill_index, B_FALSE, 3391 NULL, NULL, NULL, NULL, ipst); 3392 if (ill != NULL) { 3393 ipif = ipif_get_next_ipif(NULL, ill); 3394 ill_refrele(ill); 3395 } 3396 if (ipif == NULL) { 3397 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3398 freemsg(ipsec_mp); 3399 return; 3400 } 3401 src = ipif->ipif_src_addr; 3402 ipif_refrele(ipif); 3403 } 3404 3405 if (ire != NULL) 3406 ire_refrele(ire); 3407 3408 /* 3409 * Check if we can send back more then 8 bytes in addition 3410 * to the IP header. We will include as much as 64 bytes. 3411 */ 3412 len_needed = IPH_HDR_LENGTH(ipha); 3413 if (ipha->ipha_protocol == IPPROTO_ENCAP && 3414 (uchar_t *)ipha + len_needed + 1 <= mp->b_wptr) { 3415 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + len_needed)); 3416 } 3417 len_needed += ipst->ips_ip_icmp_return; 3418 msg_len = msgdsize(mp); 3419 if (msg_len > len_needed) { 3420 (void) adjmsg(mp, len_needed - msg_len); 3421 msg_len = len_needed; 3422 } 3423 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_HI); 3424 if (mp1 == NULL) { 3425 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors); 3426 freemsg(ipsec_mp); 3427 return; 3428 } 3429 /* 3430 * On an unlabeled system, dblks don't necessarily have creds. 3431 */ 3432 ASSERT(!is_system_labeled() || DB_CRED(mp) != NULL); 3433 if (DB_CRED(mp) != NULL) 3434 mblk_setcred(mp1, DB_CRED(mp)); 3435 mp1->b_cont = mp; 3436 mp = mp1; 3437 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 3438 ipsec_mp->b_rptr == (uint8_t *)io && 3439 io->ipsec_out_type == IPSEC_OUT); 3440 ipsec_mp->b_cont = mp; 3441 3442 /* 3443 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 3444 * node generates be accepted in peace by all on-host destinations. 3445 * If we do NOT assume that all on-host destinations trust 3446 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 3447 * (Look for ipsec_out_icmp_loopback). 3448 */ 3449 io->ipsec_out_icmp_loopback = B_TRUE; 3450 3451 ipha = (ipha_t *)mp->b_rptr; 3452 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 3453 *ipha = icmp_ipha; 3454 ipha->ipha_src = src; 3455 ipha->ipha_dst = dst; 3456 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 3457 msg_len += sizeof (icmp_ipha) + len; 3458 if (msg_len > IP_MAXPACKET) { 3459 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 3460 msg_len = IP_MAXPACKET; 3461 } 3462 ipha->ipha_length = htons((uint16_t)msg_len); 3463 icmph = (icmph_t *)&ipha[1]; 3464 bcopy(stuff, icmph, len); 3465 icmph->icmph_checksum = 0; 3466 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 3467 if (icmph->icmph_checksum == 0) 3468 icmph->icmph_checksum = 0xFFFF; 3469 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 3470 put(q, ipsec_mp); 3471 } 3472 3473 /* 3474 * Determine if an ICMP error packet can be sent given the rate limit. 3475 * The limit consists of an average frequency (icmp_pkt_err_interval measured 3476 * in milliseconds) and a burst size. Burst size number of packets can 3477 * be sent arbitrarely closely spaced. 3478 * The state is tracked using two variables to implement an approximate 3479 * token bucket filter: 3480 * icmp_pkt_err_last - lbolt value when the last burst started 3481 * icmp_pkt_err_sent - number of packets sent in current burst 3482 */ 3483 boolean_t 3484 icmp_err_rate_limit(ip_stack_t *ipst) 3485 { 3486 clock_t now = TICK_TO_MSEC(lbolt); 3487 uint_t refilled; /* Number of packets refilled in tbf since last */ 3488 /* Guard against changes by loading into local variable */ 3489 uint_t err_interval = ipst->ips_ip_icmp_err_interval; 3490 3491 if (err_interval == 0) 3492 return (B_FALSE); 3493 3494 if (ipst->ips_icmp_pkt_err_last > now) { 3495 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 3496 ipst->ips_icmp_pkt_err_last = 0; 3497 ipst->ips_icmp_pkt_err_sent = 0; 3498 } 3499 /* 3500 * If we are in a burst update the token bucket filter. 3501 * Update the "last" time to be close to "now" but make sure 3502 * we don't loose precision. 3503 */ 3504 if (ipst->ips_icmp_pkt_err_sent != 0) { 3505 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval; 3506 if (refilled > ipst->ips_icmp_pkt_err_sent) { 3507 ipst->ips_icmp_pkt_err_sent = 0; 3508 } else { 3509 ipst->ips_icmp_pkt_err_sent -= refilled; 3510 ipst->ips_icmp_pkt_err_last += refilled * err_interval; 3511 } 3512 } 3513 if (ipst->ips_icmp_pkt_err_sent == 0) { 3514 /* Start of new burst */ 3515 ipst->ips_icmp_pkt_err_last = now; 3516 } 3517 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) { 3518 ipst->ips_icmp_pkt_err_sent++; 3519 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 3520 ipst->ips_icmp_pkt_err_sent)); 3521 return (B_FALSE); 3522 } 3523 ip1dbg(("icmp_err_rate_limit: dropped\n")); 3524 return (B_TRUE); 3525 } 3526 3527 /* 3528 * Check if it is ok to send an IPv4 ICMP error packet in 3529 * response to the IPv4 packet in mp. 3530 * Free the message and return null if no 3531 * ICMP error packet should be sent. 3532 */ 3533 static mblk_t * 3534 icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst) 3535 { 3536 icmph_t *icmph; 3537 ipha_t *ipha; 3538 uint_t len_needed; 3539 ire_t *src_ire; 3540 ire_t *dst_ire; 3541 3542 if (!mp) 3543 return (NULL); 3544 ipha = (ipha_t *)mp->b_rptr; 3545 if (ip_csum_hdr(ipha)) { 3546 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); 3547 freemsg(mp); 3548 return (NULL); 3549 } 3550 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, 3551 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3552 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, 3553 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3554 if (src_ire != NULL || dst_ire != NULL || 3555 CLASSD(ipha->ipha_dst) || 3556 CLASSD(ipha->ipha_src) || 3557 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3558 /* Note: only errors to the fragment with offset 0 */ 3559 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3560 freemsg(mp); 3561 if (src_ire != NULL) 3562 ire_refrele(src_ire); 3563 if (dst_ire != NULL) 3564 ire_refrele(dst_ire); 3565 return (NULL); 3566 } 3567 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3568 /* 3569 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3570 * errors in response to any ICMP errors. 3571 */ 3572 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3573 if (mp->b_wptr - mp->b_rptr < len_needed) { 3574 if (!pullupmsg(mp, len_needed)) { 3575 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3576 freemsg(mp); 3577 return (NULL); 3578 } 3579 ipha = (ipha_t *)mp->b_rptr; 3580 } 3581 icmph = (icmph_t *) 3582 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3583 switch (icmph->icmph_type) { 3584 case ICMP_DEST_UNREACHABLE: 3585 case ICMP_SOURCE_QUENCH: 3586 case ICMP_TIME_EXCEEDED: 3587 case ICMP_PARAM_PROBLEM: 3588 case ICMP_REDIRECT: 3589 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3590 freemsg(mp); 3591 return (NULL); 3592 default: 3593 break; 3594 } 3595 } 3596 /* 3597 * If this is a labeled system, then check to see if we're allowed to 3598 * send a response to this particular sender. If not, then just drop. 3599 */ 3600 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 3601 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); 3602 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3603 freemsg(mp); 3604 return (NULL); 3605 } 3606 if (icmp_err_rate_limit(ipst)) { 3607 /* 3608 * Only send ICMP error packets every so often. 3609 * This should be done on a per port/source basis, 3610 * but for now this will suffice. 3611 */ 3612 freemsg(mp); 3613 return (NULL); 3614 } 3615 return (mp); 3616 } 3617 3618 /* 3619 * Generate an ICMP redirect message. 3620 */ 3621 static void 3622 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst) 3623 { 3624 icmph_t icmph; 3625 3626 /* 3627 * We are called from ip_rput where we could 3628 * not have attached an IPSEC_IN. 3629 */ 3630 ASSERT(mp->b_datap->db_type == M_DATA); 3631 3632 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3633 return; 3634 } 3635 3636 bzero(&icmph, sizeof (icmph_t)); 3637 icmph.icmph_type = ICMP_REDIRECT; 3638 icmph.icmph_code = 1; 3639 icmph.icmph_rd_gateway = gateway; 3640 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects); 3641 /* Redirects sent by router, and router is global zone */ 3642 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst); 3643 } 3644 3645 /* 3646 * Generate an ICMP time exceeded message. 3647 */ 3648 void 3649 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3650 ip_stack_t *ipst) 3651 { 3652 icmph_t icmph; 3653 boolean_t mctl_present; 3654 mblk_t *first_mp; 3655 3656 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3657 3658 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3659 if (mctl_present) 3660 freeb(first_mp); 3661 return; 3662 } 3663 3664 bzero(&icmph, sizeof (icmph_t)); 3665 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3666 icmph.icmph_code = code; 3667 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds); 3668 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3669 ipst); 3670 } 3671 3672 /* 3673 * Generate an ICMP unreachable message. 3674 */ 3675 void 3676 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3677 ip_stack_t *ipst) 3678 { 3679 icmph_t icmph; 3680 mblk_t *first_mp; 3681 boolean_t mctl_present; 3682 3683 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3684 3685 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3686 if (mctl_present) 3687 freeb(first_mp); 3688 return; 3689 } 3690 3691 bzero(&icmph, sizeof (icmph_t)); 3692 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3693 icmph.icmph_code = code; 3694 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 3695 ip2dbg(("send icmp destination unreachable code %d\n", code)); 3696 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present, 3697 zoneid, ipst); 3698 } 3699 3700 /* 3701 * Attempt to start recovery of an IPv4 interface that's been shut down as a 3702 * duplicate. As long as someone else holds the address, the interface will 3703 * stay down. When that conflict goes away, the interface is brought back up. 3704 * This is done so that accidental shutdowns of addresses aren't made 3705 * permanent. Your server will recover from a failure. 3706 * 3707 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a 3708 * user space process (dhcpagent). 3709 * 3710 * Recovery completes if ARP reports that the address is now ours (via 3711 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation. 3712 * 3713 * This function is entered on a timer expiry; the ID is in ipif_recovery_id. 3714 */ 3715 static void 3716 ipif_dup_recovery(void *arg) 3717 { 3718 ipif_t *ipif = arg; 3719 ill_t *ill = ipif->ipif_ill; 3720 mblk_t *arp_add_mp; 3721 mblk_t *arp_del_mp; 3722 area_t *area; 3723 ip_stack_t *ipst = ill->ill_ipst; 3724 3725 ipif->ipif_recovery_id = 0; 3726 3727 /* 3728 * No lock needed for moving or condemned check, as this is just an 3729 * optimization. 3730 */ 3731 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || 3732 (ipif->ipif_flags & IPIF_POINTOPOINT) || 3733 (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) { 3734 /* No reason to try to bring this address back. */ 3735 return; 3736 } 3737 3738 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL) 3739 goto alloc_fail; 3740 3741 if (ipif->ipif_arp_del_mp == NULL) { 3742 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) 3743 goto alloc_fail; 3744 ipif->ipif_arp_del_mp = arp_del_mp; 3745 } 3746 3747 /* Setting the 'unverified' flag restarts DAD */ 3748 area = (area_t *)arp_add_mp->b_rptr; 3749 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | 3750 ACE_F_UNVERIFIED; 3751 putnext(ill->ill_rq, arp_add_mp); 3752 return; 3753 3754 alloc_fail: 3755 /* 3756 * On allocation failure, just restart the timer. Note that the ipif 3757 * is down here, so no other thread could be trying to start a recovery 3758 * timer. The ill_lock protects the condemned flag and the recovery 3759 * timer ID. 3760 */ 3761 freemsg(arp_add_mp); 3762 mutex_enter(&ill->ill_lock); 3763 if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 && 3764 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 3765 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, 3766 MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3767 } 3768 mutex_exit(&ill->ill_lock); 3769 } 3770 3771 /* 3772 * This is for exclusive changes due to ARP. Either tear down an interface due 3773 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. 3774 */ 3775 /* ARGSUSED */ 3776 static void 3777 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3778 { 3779 ill_t *ill = rq->q_ptr; 3780 arh_t *arh; 3781 ipaddr_t src; 3782 ipif_t *ipif; 3783 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ 3784 char hbuf[MAC_STR_LEN]; 3785 char sbuf[INET_ADDRSTRLEN]; 3786 const char *failtype; 3787 boolean_t bring_up; 3788 ip_stack_t *ipst = ill->ill_ipst; 3789 3790 switch (((arcn_t *)mp->b_rptr)->arcn_code) { 3791 case AR_CN_READY: 3792 failtype = NULL; 3793 bring_up = B_TRUE; 3794 break; 3795 case AR_CN_FAILED: 3796 failtype = "in use"; 3797 bring_up = B_FALSE; 3798 break; 3799 default: 3800 failtype = "claimed"; 3801 bring_up = B_FALSE; 3802 break; 3803 } 3804 3805 arh = (arh_t *)mp->b_cont->b_rptr; 3806 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3807 3808 /* Handle failures due to probes */ 3809 if (src == 0) { 3810 bcopy((char *)&arh[1] + 2 * arh->arh_hlen + IP_ADDR_LEN, &src, 3811 IP_ADDR_LEN); 3812 } 3813 3814 (void) strlcpy(ibuf, ill->ill_name, sizeof (ibuf)); 3815 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, 3816 sizeof (hbuf)); 3817 (void) ip_dot_addr(src, sbuf); 3818 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3819 3820 if ((ipif->ipif_flags & IPIF_POINTOPOINT) || 3821 ipif->ipif_lcl_addr != src) { 3822 continue; 3823 } 3824 3825 /* 3826 * If we failed on a recovery probe, then restart the timer to 3827 * try again later. 3828 */ 3829 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) && 3830 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3831 ill->ill_net_type == IRE_IF_RESOLVER && 3832 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3833 ipst->ips_ip_dup_recovery > 0 && 3834 ipif->ipif_recovery_id == 0) { 3835 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3836 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3837 continue; 3838 } 3839 3840 /* 3841 * If what we're trying to do has already been done, then do 3842 * nothing. 3843 */ 3844 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0)) 3845 continue; 3846 3847 if (ipif->ipif_id != 0) { 3848 (void) snprintf(ibuf + ill->ill_name_length - 1, 3849 sizeof (ibuf) - ill->ill_name_length + 1, ":%d", 3850 ipif->ipif_id); 3851 } 3852 if (failtype == NULL) { 3853 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf, 3854 ibuf); 3855 } else { 3856 cmn_err(CE_WARN, "%s has duplicate address %s (%s " 3857 "by %s); disabled", ibuf, sbuf, failtype, hbuf); 3858 } 3859 3860 if (bring_up) { 3861 ASSERT(ill->ill_dl_up); 3862 /* 3863 * Free up the ARP delete message so we can allocate 3864 * a fresh one through the normal path. 3865 */ 3866 freemsg(ipif->ipif_arp_del_mp); 3867 ipif->ipif_arp_del_mp = NULL; 3868 if (ipif_resolver_up(ipif, Res_act_initial) != 3869 EINPROGRESS) { 3870 ipif->ipif_addr_ready = 1; 3871 (void) ipif_up_done(ipif); 3872 } 3873 continue; 3874 } 3875 3876 mutex_enter(&ill->ill_lock); 3877 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 3878 ipif->ipif_flags |= IPIF_DUPLICATE; 3879 ill->ill_ipif_dup_count++; 3880 mutex_exit(&ill->ill_lock); 3881 /* 3882 * Already exclusive on the ill; no need to handle deferred 3883 * processing here. 3884 */ 3885 (void) ipif_down(ipif, NULL, NULL); 3886 ipif_down_tail(ipif); 3887 mutex_enter(&ill->ill_lock); 3888 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3889 ill->ill_net_type == IRE_IF_RESOLVER && 3890 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3891 ipst->ips_ip_dup_recovery > 0) { 3892 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3893 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3894 } 3895 mutex_exit(&ill->ill_lock); 3896 } 3897 freemsg(mp); 3898 } 3899 3900 /* ARGSUSED */ 3901 static void 3902 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3903 { 3904 ill_t *ill = rq->q_ptr; 3905 arh_t *arh; 3906 ipaddr_t src; 3907 ipif_t *ipif; 3908 3909 arh = (arh_t *)mp->b_cont->b_rptr; 3910 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3911 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3912 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) 3913 (void) ipif_resolver_up(ipif, Res_act_defend); 3914 } 3915 freemsg(mp); 3916 } 3917 3918 /* 3919 * News from ARP. ARP sends notification of interesting events down 3920 * to its clients using M_CTL messages with the interesting ARP packet 3921 * attached via b_cont. 3922 * The interesting event from a device comes up the corresponding ARP-IP-DEV 3923 * queue as opposed to ARP sending the message to all the clients, i.e. all 3924 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 3925 * table if a cache IRE is found to delete all the entries for the address in 3926 * the packet. 3927 */ 3928 static void 3929 ip_arp_news(queue_t *q, mblk_t *mp) 3930 { 3931 arcn_t *arcn; 3932 arh_t *arh; 3933 ire_t *ire = NULL; 3934 char hbuf[MAC_STR_LEN]; 3935 char sbuf[INET_ADDRSTRLEN]; 3936 ipaddr_t src; 3937 in6_addr_t v6src; 3938 boolean_t isv6 = B_FALSE; 3939 ipif_t *ipif; 3940 ill_t *ill; 3941 ip_stack_t *ipst; 3942 3943 if (CONN_Q(q)) { 3944 conn_t *connp = Q_TO_CONN(q); 3945 3946 ipst = connp->conn_netstack->netstack_ip; 3947 } else { 3948 ill_t *ill = (ill_t *)q->q_ptr; 3949 3950 ipst = ill->ill_ipst; 3951 } 3952 3953 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 3954 if (q->q_next) { 3955 putnext(q, mp); 3956 } else 3957 freemsg(mp); 3958 return; 3959 } 3960 arh = (arh_t *)mp->b_cont->b_rptr; 3961 /* Is it one we are interested in? */ 3962 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 3963 isv6 = B_TRUE; 3964 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 3965 IPV6_ADDR_LEN); 3966 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 3967 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 3968 IP_ADDR_LEN); 3969 } else { 3970 freemsg(mp); 3971 return; 3972 } 3973 3974 ill = q->q_ptr; 3975 3976 arcn = (arcn_t *)mp->b_rptr; 3977 switch (arcn->arcn_code) { 3978 case AR_CN_BOGON: 3979 /* 3980 * Someone is sending ARP packets with a source protocol 3981 * address that we have published and for which we believe our 3982 * entry is authoritative and (when ill_arp_extend is set) 3983 * verified to be unique on the network. 3984 * 3985 * The ARP module internally handles the cases where the sender 3986 * is just probing (for DAD) and where the hardware address of 3987 * a non-authoritative entry has changed. Thus, these are the 3988 * real conflicts, and we have to do resolution. 3989 * 3990 * We back away quickly from the address if it's from DHCP or 3991 * otherwise temporary and hasn't been used recently (or at 3992 * all). We'd like to include "deprecated" addresses here as 3993 * well (as there's no real reason to defend something we're 3994 * discarding), but IPMP "reuses" this flag to mean something 3995 * other than the standard meaning. 3996 * 3997 * If the ARP module above is not extended (meaning that it 3998 * doesn't know how to defend the address), then we just log 3999 * the problem as we always did and continue on. It's not 4000 * right, but there's little else we can do, and those old ATM 4001 * users are going away anyway. 4002 */ 4003 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, 4004 hbuf, sizeof (hbuf)); 4005 (void) ip_dot_addr(src, sbuf); 4006 if (isv6) { 4007 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL, 4008 ipst); 4009 } else { 4010 ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst); 4011 } 4012 if (ire != NULL && IRE_IS_LOCAL(ire)) { 4013 uint32_t now; 4014 uint32_t maxage; 4015 clock_t lused; 4016 uint_t maxdefense; 4017 uint_t defs; 4018 4019 /* 4020 * First, figure out if this address hasn't been used 4021 * in a while. If it hasn't, then it's a better 4022 * candidate for abandoning. 4023 */ 4024 ipif = ire->ire_ipif; 4025 ASSERT(ipif != NULL); 4026 now = gethrestime_sec(); 4027 maxage = now - ire->ire_create_time; 4028 if (maxage > ipst->ips_ip_max_temp_idle) 4029 maxage = ipst->ips_ip_max_temp_idle; 4030 lused = drv_hztousec(ddi_get_lbolt() - 4031 ire->ire_last_used_time) / MICROSEC + 1; 4032 if (lused >= maxage && (ipif->ipif_flags & 4033 (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) 4034 maxdefense = ipst->ips_ip_max_temp_defend; 4035 else 4036 maxdefense = ipst->ips_ip_max_defend; 4037 4038 /* 4039 * Now figure out how many times we've defended 4040 * ourselves. Ignore defenses that happened long in 4041 * the past. 4042 */ 4043 mutex_enter(&ire->ire_lock); 4044 if ((defs = ire->ire_defense_count) > 0 && 4045 now - ire->ire_defense_time > 4046 ipst->ips_ip_defend_interval) { 4047 ire->ire_defense_count = defs = 0; 4048 } 4049 ire->ire_defense_count++; 4050 ire->ire_defense_time = now; 4051 mutex_exit(&ire->ire_lock); 4052 ill_refhold(ill); 4053 ire_refrele(ire); 4054 4055 /* 4056 * If we've defended ourselves too many times already, 4057 * then give up and tear down the interface(s) using 4058 * this address. Otherwise, defend by sending out a 4059 * gratuitous ARP. 4060 */ 4061 if (defs >= maxdefense && ill->ill_arp_extend) { 4062 (void) qwriter_ip(NULL, ill, q, mp, 4063 ip_arp_excl, CUR_OP, B_FALSE); 4064 } else { 4065 cmn_err(CE_WARN, 4066 "node %s is using our IP address %s on %s", 4067 hbuf, sbuf, ill->ill_name); 4068 /* 4069 * If this is an old (ATM) ARP module, then 4070 * don't try to defend the address. Remain 4071 * compatible with the old behavior. Defend 4072 * only with new ARP. 4073 */ 4074 if (ill->ill_arp_extend) { 4075 (void) qwriter_ip(NULL, ill, q, mp, 4076 ip_arp_defend, CUR_OP, B_FALSE); 4077 } else { 4078 ill_refrele(ill); 4079 } 4080 } 4081 return; 4082 } 4083 cmn_err(CE_WARN, 4084 "proxy ARP problem? Node '%s' is using %s on %s", 4085 hbuf, sbuf, ill->ill_name); 4086 if (ire != NULL) 4087 ire_refrele(ire); 4088 break; 4089 case AR_CN_ANNOUNCE: 4090 if (isv6) { 4091 /* 4092 * For XRESOLV interfaces. 4093 * Delete the IRE cache entry and NCE for this 4094 * v6 address 4095 */ 4096 ip_ire_clookup_and_delete_v6(&v6src, ipst); 4097 /* 4098 * If v6src is a non-zero, it's a router address 4099 * as below. Do the same sort of thing to clean 4100 * out off-net IRE_CACHE entries that go through 4101 * the router. 4102 */ 4103 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4104 ire_walk_v6(ire_delete_cache_gw_v6, 4105 (char *)&v6src, ALL_ZONES, ipst); 4106 } 4107 } else { 4108 nce_hw_map_t hwm; 4109 4110 /* 4111 * ARP gives us a copy of any packet where it thinks 4112 * the address has changed, so that we can update our 4113 * caches. We're responsible for caching known answers 4114 * in the current design. We check whether the 4115 * hardware address really has changed in all of our 4116 * entries that have cached this mapping, and if so, we 4117 * blow them away. This way we will immediately pick 4118 * up the rare case of a host changing hardware 4119 * address. 4120 */ 4121 if (src == 0) 4122 break; 4123 hwm.hwm_addr = src; 4124 hwm.hwm_hwlen = arh->arh_hlen; 4125 hwm.hwm_hwaddr = (uchar_t *)(arh + 1); 4126 ndp_walk_common(ipst->ips_ndp4, NULL, 4127 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); 4128 } 4129 break; 4130 case AR_CN_READY: 4131 /* No external v6 resolver has a contract to use this */ 4132 if (isv6) 4133 break; 4134 /* If the link is down, we'll retry this later */ 4135 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) 4136 break; 4137 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, 4138 NULL, NULL, ipst); 4139 if (ipif != NULL) { 4140 /* 4141 * If this is a duplicate recovery, then we now need to 4142 * go exclusive to bring this thing back up. 4143 */ 4144 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == 4145 IPIF_DUPLICATE) { 4146 ipif_refrele(ipif); 4147 ill_refhold(ill); 4148 (void) qwriter_ip(NULL, ill, q, mp, 4149 ip_arp_excl, CUR_OP, B_FALSE); 4150 return; 4151 } 4152 /* 4153 * If this is the first notice that this address is 4154 * ready, then let the user know now. 4155 */ 4156 if ((ipif->ipif_flags & IPIF_UP) && 4157 !ipif->ipif_addr_ready) { 4158 ipif_mask_reply(ipif); 4159 ip_rts_ifmsg(ipif); 4160 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 4161 sctp_update_ipif(ipif, SCTP_IPIF_UP); 4162 } 4163 ipif->ipif_addr_ready = 1; 4164 ipif_refrele(ipif); 4165 } 4166 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp), ipst); 4167 if (ire != NULL) { 4168 ire->ire_defense_count = 0; 4169 ire_refrele(ire); 4170 } 4171 break; 4172 case AR_CN_FAILED: 4173 /* No external v6 resolver has a contract to use this */ 4174 if (isv6) 4175 break; 4176 ill_refhold(ill); 4177 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_excl, CUR_OP, 4178 B_FALSE); 4179 return; 4180 } 4181 freemsg(mp); 4182 } 4183 4184 /* 4185 * Create a mblk suitable for carrying the interface index and/or source link 4186 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 4187 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 4188 * application. 4189 */ 4190 mblk_t * 4191 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid, 4192 ip_stack_t *ipst) 4193 { 4194 mblk_t *mp; 4195 ip_pktinfo_t *pinfo; 4196 ipha_t *ipha; 4197 struct ether_header *pether; 4198 4199 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED); 4200 if (mp == NULL) { 4201 ip1dbg(("ip_add_info: allocation failure.\n")); 4202 return (data_mp); 4203 } 4204 4205 ipha = (ipha_t *)data_mp->b_rptr; 4206 pinfo = (ip_pktinfo_t *)mp->b_rptr; 4207 bzero(pinfo, sizeof (ip_pktinfo_t)); 4208 pinfo->ip_pkt_flags = (uchar_t)flags; 4209 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 4210 4211 if (flags & (IPF_RECVIF | IPF_RECVADDR)) 4212 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 4213 if (flags & IPF_RECVADDR) { 4214 ipif_t *ipif; 4215 ire_t *ire; 4216 4217 /* 4218 * Only valid for V4 4219 */ 4220 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) == 4221 (IPV4_VERSION << 4)); 4222 4223 ipif = ipif_get_next_ipif(NULL, ill); 4224 if (ipif != NULL) { 4225 /* 4226 * Since a decision has already been made to deliver the 4227 * packet, there is no need to test for SECATTR and 4228 * ZONEONLY. 4229 */ 4230 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 0, ipif, 4231 zoneid, NULL, MATCH_IRE_ILL_GROUP, ipst); 4232 if (ire == NULL) { 4233 /* 4234 * packet must have come on a different 4235 * interface. 4236 * Since a decision has already been made to 4237 * deliver the packet, there is no need to test 4238 * for SECATTR and ZONEONLY. 4239 */ 4240 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 0, 4241 ipif, zoneid, NULL, NULL, ipst); 4242 } 4243 4244 if (ire == NULL) { 4245 /* 4246 * This is either a multicast packet or 4247 * the address has been removed since 4248 * the packet was received. 4249 * Return INADDR_ANY so that normal source 4250 * selection occurs for the response. 4251 */ 4252 4253 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4254 } else { 4255 ASSERT(ire->ire_type != IRE_CACHE); 4256 pinfo->ip_pkt_match_addr.s_addr = 4257 ire->ire_src_addr; 4258 ire_refrele(ire); 4259 } 4260 ipif_refrele(ipif); 4261 } else { 4262 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4263 } 4264 } 4265 4266 pether = (struct ether_header *)((char *)ipha 4267 - sizeof (struct ether_header)); 4268 /* 4269 * Make sure the interface is an ethernet type, since this option 4270 * is currently supported only on this type of interface. Also make 4271 * sure we are pointing correctly above db_base. 4272 */ 4273 4274 if ((flags & IPF_RECVSLLA) && 4275 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 4276 (ill->ill_type == IFT_ETHER) && 4277 (ill->ill_net_type == IRE_IF_RESOLVER)) { 4278 4279 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER; 4280 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, 4281 (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL); 4282 } else { 4283 /* 4284 * Clear the bit. Indicate to upper layer that IP is not 4285 * sending this ancillary info. 4286 */ 4287 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA; 4288 } 4289 4290 mp->b_datap->db_type = M_CTL; 4291 mp->b_wptr += sizeof (ip_pktinfo_t); 4292 mp->b_cont = data_mp; 4293 4294 return (mp); 4295 } 4296 4297 /* 4298 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 4299 * part of the bind request. 4300 */ 4301 4302 boolean_t 4303 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 4304 { 4305 ipsec_in_t *ii; 4306 4307 ASSERT(policy_mp != NULL); 4308 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 4309 4310 ii = (ipsec_in_t *)policy_mp->b_rptr; 4311 ASSERT(ii->ipsec_in_type == IPSEC_IN); 4312 4313 connp->conn_policy = ii->ipsec_in_policy; 4314 ii->ipsec_in_policy = NULL; 4315 4316 if (ii->ipsec_in_action != NULL) { 4317 if (connp->conn_latch == NULL) { 4318 connp->conn_latch = iplatch_create(); 4319 if (connp->conn_latch == NULL) 4320 return (B_FALSE); 4321 } 4322 ipsec_latch_inbound(connp->conn_latch, ii); 4323 } 4324 return (B_TRUE); 4325 } 4326 4327 /* 4328 * Upper level protocols (ULP) pass through bind requests to IP for inspection 4329 * and to arrange for power-fanout assist. The ULP is identified by 4330 * adding a single byte at the end of the original bind message. 4331 * A ULP other than UDP or TCP that wishes to be recognized passes 4332 * down a bind with a zero length address. 4333 * 4334 * The binding works as follows: 4335 * - A zero byte address means just bind to the protocol. 4336 * - A four byte address is treated as a request to validate 4337 * that the address is a valid local address, appropriate for 4338 * an application to bind to. This does not affect any fanout 4339 * information in IP. 4340 * - A sizeof sin_t byte address is used to bind to only the local address 4341 * and port. 4342 * - A sizeof ipa_conn_t byte address contains complete fanout information 4343 * consisting of local and remote addresses and ports. In 4344 * this case, the addresses are both validated as appropriate 4345 * for this operation, and, if so, the information is retained 4346 * for use in the inbound fanout. 4347 * 4348 * The ULP (except in the zero-length bind) can append an 4349 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 4350 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 4351 * a copy of the source or destination IRE (source for local bind; 4352 * destination for complete bind). IPSEC_POLICY_SET indicates that the 4353 * policy information contained should be copied on to the conn. 4354 * 4355 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 4356 */ 4357 mblk_t * 4358 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 4359 { 4360 ssize_t len; 4361 struct T_bind_req *tbr; 4362 sin_t *sin; 4363 ipa_conn_t *ac; 4364 uchar_t *ucp; 4365 mblk_t *mp1; 4366 boolean_t ire_requested; 4367 boolean_t ipsec_policy_set = B_FALSE; 4368 int error = 0; 4369 int protocol; 4370 ipa_conn_x_t *acx; 4371 4372 ASSERT(!connp->conn_af_isv6); 4373 connp->conn_pkt_isv6 = B_FALSE; 4374 4375 len = MBLKL(mp); 4376 if (len < (sizeof (*tbr) + 1)) { 4377 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 4378 "ip_bind: bogus msg, len %ld", len); 4379 /* XXX: Need to return something better */ 4380 goto bad_addr; 4381 } 4382 /* Back up and extract the protocol identifier. */ 4383 mp->b_wptr--; 4384 protocol = *mp->b_wptr & 0xFF; 4385 tbr = (struct T_bind_req *)mp->b_rptr; 4386 /* Reset the message type in preparation for shipping it back. */ 4387 DB_TYPE(mp) = M_PCPROTO; 4388 4389 connp->conn_ulp = (uint8_t)protocol; 4390 4391 /* 4392 * Check for a zero length address. This is from a protocol that 4393 * wants to register to receive all packets of its type. 4394 */ 4395 if (tbr->ADDR_length == 0) { 4396 /* 4397 * These protocols are now intercepted in ip_bind_v6(). 4398 * Reject protocol-level binds here for now. 4399 * 4400 * For SCTP raw socket, ICMP sends down a bind with sin_t 4401 * so that the protocol type cannot be SCTP. 4402 */ 4403 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 4404 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 4405 goto bad_addr; 4406 } 4407 4408 /* 4409 * 4410 * The udp module never sends down a zero-length address, 4411 * and allowing this on a labeled system will break MLP 4412 * functionality. 4413 */ 4414 if (is_system_labeled() && protocol == IPPROTO_UDP) 4415 goto bad_addr; 4416 4417 if (connp->conn_mac_exempt) 4418 goto bad_addr; 4419 4420 /* No hash here really. The table is big enough. */ 4421 connp->conn_srcv6 = ipv6_all_zeros; 4422 4423 ipcl_proto_insert(connp, protocol); 4424 4425 tbr->PRIM_type = T_BIND_ACK; 4426 return (mp); 4427 } 4428 4429 /* Extract the address pointer from the message. */ 4430 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 4431 tbr->ADDR_length); 4432 if (ucp == NULL) { 4433 ip1dbg(("ip_bind: no address\n")); 4434 goto bad_addr; 4435 } 4436 if (!OK_32PTR(ucp)) { 4437 ip1dbg(("ip_bind: unaligned address\n")); 4438 goto bad_addr; 4439 } 4440 /* 4441 * Check for trailing mps. 4442 */ 4443 4444 mp1 = mp->b_cont; 4445 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 4446 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); 4447 4448 switch (tbr->ADDR_length) { 4449 default: 4450 ip1dbg(("ip_bind: bad address length %d\n", 4451 (int)tbr->ADDR_length)); 4452 goto bad_addr; 4453 4454 case IP_ADDR_LEN: 4455 /* Verification of local address only */ 4456 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, 4457 ire_requested, ipsec_policy_set, B_FALSE); 4458 break; 4459 4460 case sizeof (sin_t): 4461 sin = (sin_t *)ucp; 4462 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, 4463 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); 4464 break; 4465 4466 case sizeof (ipa_conn_t): 4467 ac = (ipa_conn_t *)ucp; 4468 /* For raw socket, the local port is not set. */ 4469 if (ac->ac_lport == 0) 4470 ac->ac_lport = connp->conn_lport; 4471 /* Always verify destination reachability. */ 4472 error = ip_bind_connected(connp, mp, &ac->ac_laddr, 4473 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, 4474 ipsec_policy_set, B_TRUE, B_TRUE); 4475 break; 4476 4477 case sizeof (ipa_conn_x_t): 4478 acx = (ipa_conn_x_t *)ucp; 4479 /* 4480 * Whether or not to verify destination reachability depends 4481 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 4482 */ 4483 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, 4484 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, 4485 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, 4486 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); 4487 break; 4488 } 4489 if (error == EINPROGRESS) 4490 return (NULL); 4491 else if (error != 0) 4492 goto bad_addr; 4493 /* 4494 * Pass the IPSEC headers size in ire_ipsec_overhead. 4495 * We can't do this in ip_bind_insert_ire because the policy 4496 * may not have been inherited at that point in time and hence 4497 * conn_out_enforce_policy may not be set. 4498 */ 4499 mp1 = mp->b_cont; 4500 if (ire_requested && connp->conn_out_enforce_policy && 4501 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 4502 ire_t *ire = (ire_t *)mp1->b_rptr; 4503 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 4504 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 4505 } 4506 4507 /* Send it home. */ 4508 mp->b_datap->db_type = M_PCPROTO; 4509 tbr->PRIM_type = T_BIND_ACK; 4510 return (mp); 4511 4512 bad_addr: 4513 /* 4514 * If error = -1 then we generate a TBADADDR - otherwise error is 4515 * a unix errno. 4516 */ 4517 if (error > 0) 4518 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 4519 else 4520 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 4521 return (mp); 4522 } 4523 4524 /* 4525 * Here address is verified to be a valid local address. 4526 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 4527 * address is also considered a valid local address. 4528 * In the case of a broadcast/multicast address, however, the 4529 * upper protocol is expected to reset the src address 4530 * to 0 if it sees a IRE_BROADCAST type returned so that 4531 * no packets are emitted with broadcast/multicast address as 4532 * source address (that violates hosts requirements RFC1122) 4533 * The addresses valid for bind are: 4534 * (1) - INADDR_ANY (0) 4535 * (2) - IP address of an UP interface 4536 * (3) - IP address of a DOWN interface 4537 * (4) - valid local IP broadcast addresses. In this case 4538 * the conn will only receive packets destined to 4539 * the specified broadcast address. 4540 * (5) - a multicast address. In this case 4541 * the conn will only receive packets destined to 4542 * the specified multicast address. Note: the 4543 * application still has to issue an 4544 * IP_ADD_MEMBERSHIP socket option. 4545 * 4546 * On error, return -1 for TBADADDR otherwise pass the 4547 * errno with TSYSERR reply. 4548 * 4549 * In all the above cases, the bound address must be valid in the current zone. 4550 * When the address is loopback, multicast or broadcast, there might be many 4551 * matching IREs so bind has to look up based on the zone. 4552 * 4553 * Note: lport is in network byte order. 4554 */ 4555 int 4556 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, 4557 boolean_t ire_requested, boolean_t ipsec_policy_set, 4558 boolean_t fanout_insert) 4559 { 4560 int error = 0; 4561 ire_t *src_ire; 4562 mblk_t *policy_mp; 4563 ipif_t *ipif; 4564 zoneid_t zoneid; 4565 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4566 4567 if (ipsec_policy_set) { 4568 policy_mp = mp->b_cont; 4569 } 4570 4571 /* 4572 * If it was previously connected, conn_fully_bound would have 4573 * been set. 4574 */ 4575 connp->conn_fully_bound = B_FALSE; 4576 4577 src_ire = NULL; 4578 ipif = NULL; 4579 4580 zoneid = IPCL_ZONEID(connp); 4581 4582 if (src_addr) { 4583 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 4584 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 4585 /* 4586 * If an address other than 0.0.0.0 is requested, 4587 * we verify that it is a valid address for bind 4588 * Note: Following code is in if-else-if form for 4589 * readability compared to a condition check. 4590 */ 4591 /* LINTED - statement has no consequent */ 4592 if (IRE_IS_LOCAL(src_ire)) { 4593 /* 4594 * (2) Bind to address of local UP interface 4595 */ 4596 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 4597 /* 4598 * (4) Bind to broadcast address 4599 * Note: permitted only from transports that 4600 * request IRE 4601 */ 4602 if (!ire_requested) 4603 error = EADDRNOTAVAIL; 4604 } else { 4605 /* 4606 * (3) Bind to address of local DOWN interface 4607 * (ipif_lookup_addr() looks up all interfaces 4608 * but we do not get here for UP interfaces 4609 * - case (2) above) 4610 * We put the protocol byte back into the mblk 4611 * since we may come back via ip_wput_nondata() 4612 * later with this mblk if ipif_lookup_addr chooses 4613 * to defer processing. 4614 */ 4615 *mp->b_wptr++ = (char)connp->conn_ulp; 4616 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, 4617 CONNP_TO_WQ(connp), mp, ip_wput_nondata, 4618 &error, ipst)) != NULL) { 4619 ipif_refrele(ipif); 4620 } else if (error == EINPROGRESS) { 4621 if (src_ire != NULL) 4622 ire_refrele(src_ire); 4623 return (EINPROGRESS); 4624 } else if (CLASSD(src_addr)) { 4625 error = 0; 4626 if (src_ire != NULL) 4627 ire_refrele(src_ire); 4628 /* 4629 * (5) bind to multicast address. 4630 * Fake out the IRE returned to upper 4631 * layer to be a broadcast IRE. 4632 */ 4633 src_ire = ire_ctable_lookup( 4634 INADDR_BROADCAST, INADDR_ANY, 4635 IRE_BROADCAST, NULL, zoneid, NULL, 4636 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY), 4637 ipst); 4638 if (src_ire == NULL || !ire_requested) 4639 error = EADDRNOTAVAIL; 4640 } else { 4641 /* 4642 * Not a valid address for bind 4643 */ 4644 error = EADDRNOTAVAIL; 4645 } 4646 /* 4647 * Just to keep it consistent with the processing in 4648 * ip_bind_v4() 4649 */ 4650 mp->b_wptr--; 4651 } 4652 if (error) { 4653 /* Red Alert! Attempting to be a bogon! */ 4654 ip1dbg(("ip_bind: bad src address 0x%x\n", 4655 ntohl(src_addr))); 4656 goto bad_addr; 4657 } 4658 } 4659 4660 /* 4661 * Allow setting new policies. For example, disconnects come 4662 * down as ipa_t bind. As we would have set conn_policy_cached 4663 * to B_TRUE before, we should set it to B_FALSE, so that policy 4664 * can change after the disconnect. 4665 */ 4666 connp->conn_policy_cached = B_FALSE; 4667 4668 /* 4669 * If not fanout_insert this was just an address verification 4670 */ 4671 if (fanout_insert) { 4672 /* 4673 * The addresses have been verified. Time to insert in 4674 * the correct fanout list. 4675 */ 4676 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4677 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4678 connp->conn_lport = lport; 4679 connp->conn_fport = 0; 4680 /* 4681 * Do we need to add a check to reject Multicast packets 4682 * 4683 * We need to make sure that the conn_recv is set to a non-null 4684 * value before we insert the conn into the classifier table. 4685 * This is to avoid a race with an incoming packet which does an 4686 * ipcl_classify(). 4687 */ 4688 if (*mp->b_wptr == IPPROTO_TCP) 4689 connp->conn_recv = tcp_conn_request; 4690 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); 4691 } 4692 4693 if (error == 0) { 4694 if (ire_requested) { 4695 if (!ip_bind_insert_ire(mp, src_ire, NULL, ipst)) { 4696 error = -1; 4697 /* Falls through to bad_addr */ 4698 } 4699 } else if (ipsec_policy_set) { 4700 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4701 error = -1; 4702 /* Falls through to bad_addr */ 4703 } 4704 } 4705 } else if (connp->conn_ulp == IPPROTO_TCP) { 4706 connp->conn_recv = tcp_input; 4707 } 4708 bad_addr: 4709 if (error != 0) { 4710 if (connp->conn_anon_port) { 4711 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4712 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 4713 B_FALSE); 4714 } 4715 connp->conn_mlp_type = mlptSingle; 4716 } 4717 if (src_ire != NULL) 4718 IRE_REFRELE(src_ire); 4719 if (ipsec_policy_set) { 4720 ASSERT(policy_mp == mp->b_cont); 4721 ASSERT(policy_mp != NULL); 4722 freeb(policy_mp); 4723 /* 4724 * As of now assume that nothing else accompanies 4725 * IPSEC_POLICY_SET. 4726 */ 4727 mp->b_cont = NULL; 4728 } 4729 return (error); 4730 } 4731 4732 /* 4733 * Verify that both the source and destination addresses 4734 * are valid. If verify_dst is false, then the destination address may be 4735 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4736 * destination reachability, while tunnels do not. 4737 * Note that we allow connect to broadcast and multicast 4738 * addresses when ire_requested is set. Thus the ULP 4739 * has to check for IRE_BROADCAST and multicast. 4740 * 4741 * Returns zero if ok. 4742 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4743 * (for use with TSYSERR reply). 4744 * 4745 * Note: lport and fport are in network byte order. 4746 */ 4747 int 4748 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, 4749 uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4750 boolean_t ire_requested, boolean_t ipsec_policy_set, 4751 boolean_t fanout_insert, boolean_t verify_dst) 4752 { 4753 ire_t *src_ire; 4754 ire_t *dst_ire; 4755 int error = 0; 4756 int protocol; 4757 mblk_t *policy_mp; 4758 ire_t *sire = NULL; 4759 ire_t *md_dst_ire = NULL; 4760 ire_t *lso_dst_ire = NULL; 4761 ill_t *ill = NULL; 4762 zoneid_t zoneid; 4763 ipaddr_t src_addr = *src_addrp; 4764 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4765 4766 src_ire = dst_ire = NULL; 4767 protocol = *mp->b_wptr & 0xFF; 4768 4769 /* 4770 * If we never got a disconnect before, clear it now. 4771 */ 4772 connp->conn_fully_bound = B_FALSE; 4773 4774 if (ipsec_policy_set) { 4775 policy_mp = mp->b_cont; 4776 } 4777 4778 zoneid = IPCL_ZONEID(connp); 4779 4780 if (CLASSD(dst_addr)) { 4781 /* Pick up an IRE_BROADCAST */ 4782 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4783 NULL, zoneid, MBLK_GETLABEL(mp), 4784 (MATCH_IRE_RECURSIVE | 4785 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | 4786 MATCH_IRE_SECATTR), ipst); 4787 } else { 4788 /* 4789 * If conn_dontroute is set or if conn_nexthop_set is set, 4790 * and onlink ipif is not found set ENETUNREACH error. 4791 */ 4792 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4793 ipif_t *ipif; 4794 4795 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4796 dst_addr : connp->conn_nexthop_v4, zoneid, ipst); 4797 if (ipif == NULL) { 4798 error = ENETUNREACH; 4799 goto bad_addr; 4800 } 4801 ipif_refrele(ipif); 4802 } 4803 4804 if (connp->conn_nexthop_set) { 4805 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4806 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), 4807 MATCH_IRE_SECATTR, ipst); 4808 } else { 4809 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4810 &sire, zoneid, MBLK_GETLABEL(mp), 4811 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4812 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | 4813 MATCH_IRE_SECATTR), ipst); 4814 } 4815 } 4816 /* 4817 * dst_ire can't be a broadcast when not ire_requested. 4818 * We also prevent ire's with src address INADDR_ANY to 4819 * be used, which are created temporarily for 4820 * sending out packets from endpoints that have 4821 * conn_unspec_src set. If verify_dst is true, the destination must be 4822 * reachable. If verify_dst is false, the destination needn't be 4823 * reachable. 4824 * 4825 * If we match on a reject or black hole, then we've got a 4826 * local failure. May as well fail out the connect() attempt, 4827 * since it's never going to succeed. 4828 */ 4829 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4830 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4831 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4832 /* 4833 * If we're verifying destination reachability, we always want 4834 * to complain here. 4835 * 4836 * If we're not verifying destination reachability but the 4837 * destination has a route, we still want to fail on the 4838 * temporary address and broadcast address tests. 4839 */ 4840 if (verify_dst || (dst_ire != NULL)) { 4841 if (ip_debug > 2) { 4842 pr_addr_dbg("ip_bind_connected: bad connected " 4843 "dst %s\n", AF_INET, &dst_addr); 4844 } 4845 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4846 error = ENETUNREACH; 4847 else 4848 error = EHOSTUNREACH; 4849 goto bad_addr; 4850 } 4851 } 4852 4853 /* 4854 * We now know that routing will allow us to reach the destination. 4855 * Check whether Trusted Solaris policy allows communication with this 4856 * host, and pretend that the destination is unreachable if not. 4857 * 4858 * This is never a problem for TCP, since that transport is known to 4859 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 4860 * handling. If the remote is unreachable, it will be detected at that 4861 * point, so there's no reason to check it here. 4862 * 4863 * Note that for sendto (and other datagram-oriented friends), this 4864 * check is done as part of the data path label computation instead. 4865 * The check here is just to make non-TCP connect() report the right 4866 * error. 4867 */ 4868 if (dst_ire != NULL && is_system_labeled() && 4869 !IPCL_IS_TCP(connp) && 4870 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL, 4871 connp->conn_mac_exempt, ipst) != 0) { 4872 error = EHOSTUNREACH; 4873 if (ip_debug > 2) { 4874 pr_addr_dbg("ip_bind_connected: no label for dst %s\n", 4875 AF_INET, &dst_addr); 4876 } 4877 goto bad_addr; 4878 } 4879 4880 /* 4881 * If the app does a connect(), it means that it will most likely 4882 * send more than 1 packet to the destination. It makes sense 4883 * to clear the temporary flag. 4884 */ 4885 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4886 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4887 irb_t *irb = dst_ire->ire_bucket; 4888 4889 rw_enter(&irb->irb_lock, RW_WRITER); 4890 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4891 irb->irb_tmp_ire_cnt--; 4892 rw_exit(&irb->irb_lock); 4893 } 4894 4895 /* 4896 * See if we should notify ULP about LSO/MDT; we do this whether or not 4897 * ire_requested is TRUE, in order to handle active connects; LSO/MDT 4898 * eligibility tests for passive connects are handled separately 4899 * through tcp_adapt_ire(). We do this before the source address 4900 * selection, because dst_ire may change after a call to 4901 * ipif_select_source(). This is a best-effort check, as the 4902 * packet for this connection may not actually go through 4903 * dst_ire->ire_stq, and the exact IRE can only be known after 4904 * calling ip_newroute(). This is why we further check on the 4905 * IRE during LSO/Multidata packet transmission in 4906 * tcp_lsosend()/tcp_multisend(). 4907 */ 4908 if (!ipsec_policy_set && dst_ire != NULL && 4909 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4910 (ill = ire_to_ill(dst_ire), ill != NULL)) { 4911 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 4912 lso_dst_ire = dst_ire; 4913 IRE_REFHOLD(lso_dst_ire); 4914 } else if (ipst->ips_ip_multidata_outbound && 4915 ILL_MDT_CAPABLE(ill)) { 4916 md_dst_ire = dst_ire; 4917 IRE_REFHOLD(md_dst_ire); 4918 } 4919 } 4920 4921 if (dst_ire != NULL && 4922 dst_ire->ire_type == IRE_LOCAL && 4923 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { 4924 /* 4925 * If the IRE belongs to a different zone, look for a matching 4926 * route in the forwarding table and use the source address from 4927 * that route. 4928 */ 4929 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 4930 zoneid, 0, NULL, 4931 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4932 MATCH_IRE_RJ_BHOLE, ipst); 4933 if (src_ire == NULL) { 4934 error = EHOSTUNREACH; 4935 goto bad_addr; 4936 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 4937 if (!(src_ire->ire_type & IRE_HOST)) 4938 error = ENETUNREACH; 4939 else 4940 error = EHOSTUNREACH; 4941 goto bad_addr; 4942 } 4943 if (src_addr == INADDR_ANY) 4944 src_addr = src_ire->ire_src_addr; 4945 ire_refrele(src_ire); 4946 src_ire = NULL; 4947 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 4948 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 4949 src_addr = sire->ire_src_addr; 4950 ire_refrele(dst_ire); 4951 dst_ire = sire; 4952 sire = NULL; 4953 } else { 4954 /* 4955 * Pick a source address so that a proper inbound 4956 * load spreading would happen. 4957 */ 4958 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; 4959 ipif_t *src_ipif = NULL; 4960 ire_t *ipif_ire; 4961 4962 /* 4963 * Supply a local source address such that inbound 4964 * load spreading happens. 4965 * 4966 * Determine the best source address on this ill for 4967 * the destination. 4968 * 4969 * 1) For broadcast, we should return a broadcast ire 4970 * found above so that upper layers know that the 4971 * destination address is a broadcast address. 4972 * 4973 * 2) If this is part of a group, select a better 4974 * source address so that better inbound load 4975 * balancing happens. Do the same if the ipif 4976 * is DEPRECATED. 4977 * 4978 * 3) If the outgoing interface is part of a usesrc 4979 * group, then try selecting a source address from 4980 * the usesrc ILL. 4981 */ 4982 if ((dst_ire->ire_zoneid != zoneid && 4983 dst_ire->ire_zoneid != ALL_ZONES) || 4984 (!(dst_ire->ire_type & IRE_BROADCAST) && 4985 ((dst_ill->ill_group != NULL) || 4986 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 4987 (dst_ill->ill_usesrc_ifindex != 0)))) { 4988 /* 4989 * If the destination is reachable via a 4990 * given gateway, the selected source address 4991 * should be in the same subnet as the gateway. 4992 * Otherwise, the destination is not reachable. 4993 * 4994 * If there are no interfaces on the same subnet 4995 * as the destination, ipif_select_source gives 4996 * first non-deprecated interface which might be 4997 * on a different subnet than the gateway. 4998 * This is not desirable. Hence pass the dst_ire 4999 * source address to ipif_select_source. 5000 * It is sure that the destination is reachable 5001 * with the dst_ire source address subnet. 5002 * So passing dst_ire source address to 5003 * ipif_select_source will make sure that the 5004 * selected source will be on the same subnet 5005 * as dst_ire source address. 5006 */ 5007 ipaddr_t saddr = 5008 dst_ire->ire_ipif->ipif_src_addr; 5009 src_ipif = ipif_select_source(dst_ill, 5010 saddr, zoneid); 5011 if (src_ipif != NULL) { 5012 if (IS_VNI(src_ipif->ipif_ill)) { 5013 /* 5014 * For VNI there is no 5015 * interface route 5016 */ 5017 src_addr = 5018 src_ipif->ipif_src_addr; 5019 } else { 5020 ipif_ire = 5021 ipif_to_ire(src_ipif); 5022 if (ipif_ire != NULL) { 5023 IRE_REFRELE(dst_ire); 5024 dst_ire = ipif_ire; 5025 } 5026 src_addr = 5027 dst_ire->ire_src_addr; 5028 } 5029 ipif_refrele(src_ipif); 5030 } else { 5031 src_addr = dst_ire->ire_src_addr; 5032 } 5033 } else { 5034 src_addr = dst_ire->ire_src_addr; 5035 } 5036 } 5037 } 5038 5039 /* 5040 * We do ire_route_lookup() here (and not 5041 * interface lookup as we assert that 5042 * src_addr should only come from an 5043 * UP interface for hard binding. 5044 */ 5045 ASSERT(src_ire == NULL); 5046 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 5047 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 5048 /* src_ire must be a local|loopback */ 5049 if (!IRE_IS_LOCAL(src_ire)) { 5050 if (ip_debug > 2) { 5051 pr_addr_dbg("ip_bind_connected: bad connected " 5052 "src %s\n", AF_INET, &src_addr); 5053 } 5054 error = EADDRNOTAVAIL; 5055 goto bad_addr; 5056 } 5057 5058 /* 5059 * If the source address is a loopback address, the 5060 * destination had best be local or multicast. 5061 * The transports that can't handle multicast will reject 5062 * those addresses. 5063 */ 5064 if (src_ire->ire_type == IRE_LOOPBACK && 5065 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 5066 ip1dbg(("ip_bind_connected: bad connected loopback\n")); 5067 error = -1; 5068 goto bad_addr; 5069 } 5070 5071 /* 5072 * Allow setting new policies. For example, disconnects come 5073 * down as ipa_t bind. As we would have set conn_policy_cached 5074 * to B_TRUE before, we should set it to B_FALSE, so that policy 5075 * can change after the disconnect. 5076 */ 5077 connp->conn_policy_cached = B_FALSE; 5078 5079 /* 5080 * Set the conn addresses/ports immediately, so the IPsec policy calls 5081 * can handle their passed-in conn's. 5082 */ 5083 5084 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 5085 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 5086 connp->conn_lport = lport; 5087 connp->conn_fport = fport; 5088 *src_addrp = src_addr; 5089 5090 ASSERT(!(ipsec_policy_set && ire_requested)); 5091 if (ire_requested) { 5092 iulp_t *ulp_info = NULL; 5093 5094 /* 5095 * Note that sire will not be NULL if this is an off-link 5096 * connection and there is not cache for that dest yet. 5097 * 5098 * XXX Because of an existing bug, if there are multiple 5099 * default routes, the IRE returned now may not be the actual 5100 * default route used (default routes are chosen in a 5101 * round robin fashion). So if the metrics for different 5102 * default routes are different, we may return the wrong 5103 * metrics. This will not be a problem if the existing 5104 * bug is fixed. 5105 */ 5106 if (sire != NULL) { 5107 ulp_info = &(sire->ire_uinfo); 5108 } 5109 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info, ipst)) { 5110 error = -1; 5111 goto bad_addr; 5112 } 5113 } else if (ipsec_policy_set) { 5114 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 5115 error = -1; 5116 goto bad_addr; 5117 } 5118 } 5119 5120 /* 5121 * Cache IPsec policy in this conn. If we have per-socket policy, 5122 * we'll cache that. If we don't, we'll inherit global policy. 5123 * 5124 * We can't insert until the conn reflects the policy. Note that 5125 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 5126 * connections where we don't have a policy. This is to prevent 5127 * global policy lookups in the inbound path. 5128 * 5129 * If we insert before we set conn_policy_cached, 5130 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 5131 * because global policy cound be non-empty. We normally call 5132 * ipsec_check_policy() for conn_policy_cached connections only if 5133 * ipc_in_enforce_policy is set. But in this case, 5134 * conn_policy_cached can get set anytime since we made the 5135 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 5136 * called, which will make the above assumption false. Thus, we 5137 * need to insert after we set conn_policy_cached. 5138 */ 5139 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 5140 goto bad_addr; 5141 5142 if (fanout_insert) { 5143 /* 5144 * The addresses have been verified. Time to insert in 5145 * the correct fanout list. 5146 * We need to make sure that the conn_recv is set to a non-null 5147 * value before we insert into the classifier table to avoid a 5148 * race with an incoming packet which does an ipcl_classify(). 5149 */ 5150 if (protocol == IPPROTO_TCP) 5151 connp->conn_recv = tcp_input; 5152 error = ipcl_conn_insert(connp, protocol, src_addr, 5153 dst_addr, connp->conn_ports); 5154 } 5155 5156 if (error == 0) { 5157 connp->conn_fully_bound = B_TRUE; 5158 /* 5159 * Our initial checks for LSO/MDT have passed; the IRE is not 5160 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 5161 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into 5162 * ip_xxinfo_return(), which performs further checks 5163 * against them and upon success, returns the LSO/MDT info 5164 * mblk which we will attach to the bind acknowledgment. 5165 */ 5166 if (lso_dst_ire != NULL) { 5167 mblk_t *lsoinfo_mp; 5168 5169 ASSERT(ill->ill_lso_capab != NULL); 5170 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp, 5171 ill->ill_name, ill->ill_lso_capab)) != NULL) 5172 linkb(mp, lsoinfo_mp); 5173 } else if (md_dst_ire != NULL) { 5174 mblk_t *mdinfo_mp; 5175 5176 ASSERT(ill->ill_mdt_capab != NULL); 5177 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 5178 ill->ill_name, ill->ill_mdt_capab)) != NULL) 5179 linkb(mp, mdinfo_mp); 5180 } 5181 } 5182 bad_addr: 5183 if (ipsec_policy_set) { 5184 ASSERT(policy_mp == mp->b_cont); 5185 ASSERT(policy_mp != NULL); 5186 freeb(policy_mp); 5187 /* 5188 * As of now assume that nothing else accompanies 5189 * IPSEC_POLICY_SET. 5190 */ 5191 mp->b_cont = NULL; 5192 } 5193 if (src_ire != NULL) 5194 IRE_REFRELE(src_ire); 5195 if (dst_ire != NULL) 5196 IRE_REFRELE(dst_ire); 5197 if (sire != NULL) 5198 IRE_REFRELE(sire); 5199 if (md_dst_ire != NULL) 5200 IRE_REFRELE(md_dst_ire); 5201 if (lso_dst_ire != NULL) 5202 IRE_REFRELE(lso_dst_ire); 5203 return (error); 5204 } 5205 5206 /* 5207 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 5208 * Prefers dst_ire over src_ire. 5209 */ 5210 static boolean_t 5211 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst) 5212 { 5213 mblk_t *mp1; 5214 ire_t *ret_ire = NULL; 5215 5216 mp1 = mp->b_cont; 5217 ASSERT(mp1 != NULL); 5218 5219 if (ire != NULL) { 5220 /* 5221 * mp1 initialized above to IRE_DB_REQ_TYPE 5222 * appended mblk. Its <upper protocol>'s 5223 * job to make sure there is room. 5224 */ 5225 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 5226 return (0); 5227 5228 mp1->b_datap->db_type = IRE_DB_TYPE; 5229 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 5230 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 5231 ret_ire = (ire_t *)mp1->b_rptr; 5232 /* 5233 * Pass the latest setting of the ip_path_mtu_discovery and 5234 * copy the ulp info if any. 5235 */ 5236 ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? 5237 IPH_DF : 0; 5238 if (ulp_info != NULL) { 5239 bcopy(ulp_info, &(ret_ire->ire_uinfo), 5240 sizeof (iulp_t)); 5241 } 5242 ret_ire->ire_mp = mp1; 5243 } else { 5244 /* 5245 * No IRE was found. Remove IRE mblk. 5246 */ 5247 mp->b_cont = mp1->b_cont; 5248 freeb(mp1); 5249 } 5250 5251 return (1); 5252 } 5253 5254 /* 5255 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 5256 * the final piece where we don't. Return a pointer to the first mblk in the 5257 * result, and update the pointer to the next mblk to chew on. If anything 5258 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 5259 * NULL pointer. 5260 */ 5261 mblk_t * 5262 ip_carve_mp(mblk_t **mpp, ssize_t len) 5263 { 5264 mblk_t *mp0; 5265 mblk_t *mp1; 5266 mblk_t *mp2; 5267 5268 if (!len || !mpp || !(mp0 = *mpp)) 5269 return (NULL); 5270 /* If we aren't going to consume the first mblk, we need a dup. */ 5271 if (mp0->b_wptr - mp0->b_rptr > len) { 5272 mp1 = dupb(mp0); 5273 if (mp1) { 5274 /* Partition the data between the two mblks. */ 5275 mp1->b_wptr = mp1->b_rptr + len; 5276 mp0->b_rptr = mp1->b_wptr; 5277 /* 5278 * after adjustments if mblk not consumed is now 5279 * unaligned, try to align it. If this fails free 5280 * all messages and let upper layer recover. 5281 */ 5282 if (!OK_32PTR(mp0->b_rptr)) { 5283 if (!pullupmsg(mp0, -1)) { 5284 freemsg(mp0); 5285 freemsg(mp1); 5286 *mpp = NULL; 5287 return (NULL); 5288 } 5289 } 5290 } 5291 return (mp1); 5292 } 5293 /* Eat through as many mblks as we need to get len bytes. */ 5294 len -= mp0->b_wptr - mp0->b_rptr; 5295 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 5296 if (mp2->b_wptr - mp2->b_rptr > len) { 5297 /* 5298 * We won't consume the entire last mblk. Like 5299 * above, dup and partition it. 5300 */ 5301 mp1->b_cont = dupb(mp2); 5302 mp1 = mp1->b_cont; 5303 if (!mp1) { 5304 /* 5305 * Trouble. Rather than go to a lot of 5306 * trouble to clean up, we free the messages. 5307 * This won't be any worse than losing it on 5308 * the wire. 5309 */ 5310 freemsg(mp0); 5311 freemsg(mp2); 5312 *mpp = NULL; 5313 return (NULL); 5314 } 5315 mp1->b_wptr = mp1->b_rptr + len; 5316 mp2->b_rptr = mp1->b_wptr; 5317 /* 5318 * after adjustments if mblk not consumed is now 5319 * unaligned, try to align it. If this fails free 5320 * all messages and let upper layer recover. 5321 */ 5322 if (!OK_32PTR(mp2->b_rptr)) { 5323 if (!pullupmsg(mp2, -1)) { 5324 freemsg(mp0); 5325 freemsg(mp2); 5326 *mpp = NULL; 5327 return (NULL); 5328 } 5329 } 5330 *mpp = mp2; 5331 return (mp0); 5332 } 5333 /* Decrement len by the amount we just got. */ 5334 len -= mp2->b_wptr - mp2->b_rptr; 5335 } 5336 /* 5337 * len should be reduced to zero now. If not our caller has 5338 * screwed up. 5339 */ 5340 if (len) { 5341 /* Shouldn't happen! */ 5342 freemsg(mp0); 5343 *mpp = NULL; 5344 return (NULL); 5345 } 5346 /* 5347 * We consumed up to exactly the end of an mblk. Detach the part 5348 * we are returning from the rest of the chain. 5349 */ 5350 mp1->b_cont = NULL; 5351 *mpp = mp2; 5352 return (mp0); 5353 } 5354 5355 /* The ill stream is being unplumbed. Called from ip_close */ 5356 int 5357 ip_modclose(ill_t *ill) 5358 { 5359 5360 boolean_t success; 5361 ipsq_t *ipsq; 5362 ipif_t *ipif; 5363 queue_t *q = ill->ill_rq; 5364 hook_nic_event_t *info; 5365 ip_stack_t *ipst = ill->ill_ipst; 5366 clock_t timeout; 5367 5368 /* 5369 * Wait for the ACKs of all deferred control messages to be processed. 5370 * In particular, we wait for a potential capability reset initiated 5371 * in ip_sioctl_plink() to complete before proceeding. 5372 * 5373 * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms) 5374 * in case the driver never replies. 5375 */ 5376 timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms); 5377 mutex_enter(&ill->ill_lock); 5378 while (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 5379 if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) { 5380 /* Timeout */ 5381 break; 5382 } 5383 } 5384 mutex_exit(&ill->ill_lock); 5385 5386 /* 5387 * Forcibly enter the ipsq after some delay. This is to take 5388 * care of the case when some ioctl does not complete because 5389 * we sent a control message to the driver and it did not 5390 * send us a reply. We want to be able to at least unplumb 5391 * and replumb rather than force the user to reboot the system. 5392 */ 5393 success = ipsq_enter(ill, B_FALSE); 5394 5395 /* 5396 * Open/close/push/pop is guaranteed to be single threaded 5397 * per stream by STREAMS. FS guarantees that all references 5398 * from top are gone before close is called. So there can't 5399 * be another close thread that has set CONDEMNED on this ill. 5400 * and cause ipsq_enter to return failure. 5401 */ 5402 ASSERT(success); 5403 ipsq = ill->ill_phyint->phyint_ipsq; 5404 5405 /* 5406 * Mark it condemned. No new reference will be made to this ill. 5407 * Lookup functions will return an error. Threads that try to 5408 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 5409 * that the refcnt will drop down to zero. 5410 */ 5411 mutex_enter(&ill->ill_lock); 5412 ill->ill_state_flags |= ILL_CONDEMNED; 5413 for (ipif = ill->ill_ipif; ipif != NULL; 5414 ipif = ipif->ipif_next) { 5415 ipif->ipif_state_flags |= IPIF_CONDEMNED; 5416 } 5417 /* 5418 * Wake up anybody waiting to enter the ipsq. ipsq_enter 5419 * returns error if ILL_CONDEMNED is set 5420 */ 5421 cv_broadcast(&ill->ill_cv); 5422 mutex_exit(&ill->ill_lock); 5423 5424 /* 5425 * Send all the deferred control messages downstream which came in 5426 * during the small window right before ipsq_enter(). We do this 5427 * without waiting for the ACKs because all the ACKs for M_PROTO 5428 * messages are ignored in ip_rput() when ILL_CONDEMNED is set. 5429 */ 5430 ill_send_all_deferred_mp(ill); 5431 5432 /* 5433 * Shut down fragmentation reassembly. 5434 * ill_frag_timer won't start a timer again. 5435 * Now cancel any existing timer 5436 */ 5437 (void) untimeout(ill->ill_frag_timer_id); 5438 (void) ill_frag_timeout(ill, 0); 5439 5440 /* 5441 * If MOVE was in progress, clear the 5442 * move_in_progress fields also. 5443 */ 5444 if (ill->ill_move_in_progress) { 5445 ILL_CLEAR_MOVE(ill); 5446 } 5447 5448 /* 5449 * Call ill_delete to bring down the ipifs, ilms and ill on 5450 * this ill. Then wait for the refcnts to drop to zero. 5451 * ill_is_quiescent checks whether the ill is really quiescent. 5452 * Then make sure that threads that are waiting to enter the 5453 * ipsq have seen the error returned by ipsq_enter and have 5454 * gone away. Then we call ill_delete_tail which does the 5455 * DL_UNBIND and DL_DETACH with the driver and then qprocsoff. 5456 */ 5457 ill_delete(ill); 5458 mutex_enter(&ill->ill_lock); 5459 while (!ill_is_quiescent(ill)) 5460 cv_wait(&ill->ill_cv, &ill->ill_lock); 5461 while (ill->ill_waiters) 5462 cv_wait(&ill->ill_cv, &ill->ill_lock); 5463 5464 mutex_exit(&ill->ill_lock); 5465 5466 /* 5467 * ill_delete_tail drops reference on ill_ipst, but we need to keep 5468 * it held until the end of the function since the cleanup 5469 * below needs to be able to use the ip_stack_t. 5470 */ 5471 netstack_hold(ipst->ips_netstack); 5472 5473 /* qprocsoff is called in ill_delete_tail */ 5474 ill_delete_tail(ill); 5475 ASSERT(ill->ill_ipst == NULL); 5476 5477 /* 5478 * Walk through all upper (conn) streams and qenable 5479 * those that have queued data. 5480 * close synchronization needs this to 5481 * be done to ensure that all upper layers blocked 5482 * due to flow control to the closing device 5483 * get unblocked. 5484 */ 5485 ip1dbg(("ip_wsrv: walking\n")); 5486 conn_walk_drain(ipst); 5487 5488 mutex_enter(&ipst->ips_ip_mi_lock); 5489 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill); 5490 mutex_exit(&ipst->ips_ip_mi_lock); 5491 5492 /* 5493 * credp could be null if the open didn't succeed and ip_modopen 5494 * itself calls ip_close. 5495 */ 5496 if (ill->ill_credp != NULL) 5497 crfree(ill->ill_credp); 5498 5499 /* 5500 * Unhook the nic event message from the ill and enqueue it into the nic 5501 * event taskq. 5502 */ 5503 if ((info = ill->ill_nic_event_info) != NULL) { 5504 if (ddi_taskq_dispatch(eventq_queue_nic, 5505 ip_ne_queue_func, 5506 (void *)info, DDI_SLEEP) == DDI_FAILURE) { 5507 ip2dbg(("ip_ioctl_finish:ddi_taskq_dispatch failed\n")); 5508 if (info->hne_data != NULL) 5509 kmem_free(info->hne_data, info->hne_datalen); 5510 kmem_free(info, sizeof (hook_nic_event_t)); 5511 } 5512 ill->ill_nic_event_info = NULL; 5513 } 5514 5515 /* 5516 * Now we are done with the module close pieces that 5517 * need the netstack_t. 5518 */ 5519 netstack_rele(ipst->ips_netstack); 5520 5521 mi_close_free((IDP)ill); 5522 q->q_ptr = WR(q)->q_ptr = NULL; 5523 5524 ipsq_exit(ipsq, B_TRUE, B_TRUE); 5525 5526 return (0); 5527 } 5528 5529 /* 5530 * This is called as part of close() for both IP and UDP 5531 * in order to quiesce the conn. 5532 */ 5533 void 5534 ip_quiesce_conn(conn_t *connp) 5535 { 5536 boolean_t drain_cleanup_reqd = B_FALSE; 5537 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 5538 boolean_t ilg_cleanup_reqd = B_FALSE; 5539 ip_stack_t *ipst; 5540 5541 ASSERT(!IPCL_IS_TCP(connp)); 5542 ipst = connp->conn_netstack->netstack_ip; 5543 5544 /* 5545 * Mark the conn as closing, and this conn must not be 5546 * inserted in future into any list. Eg. conn_drain_insert(), 5547 * won't insert this conn into the conn_drain_list. 5548 * Similarly ill_pending_mp_add() will not add any mp to 5549 * the pending mp list, after this conn has started closing. 5550 * 5551 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 5552 * cannot get set henceforth. 5553 */ 5554 mutex_enter(&connp->conn_lock); 5555 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 5556 connp->conn_state_flags |= CONN_CLOSING; 5557 if (connp->conn_idl != NULL) 5558 drain_cleanup_reqd = B_TRUE; 5559 if (connp->conn_oper_pending_ill != NULL) 5560 conn_ioctl_cleanup_reqd = B_TRUE; 5561 if (connp->conn_ilg_inuse != 0) 5562 ilg_cleanup_reqd = B_TRUE; 5563 mutex_exit(&connp->conn_lock); 5564 5565 if (IPCL_IS_UDP(connp)) 5566 udp_quiesce_conn(connp); 5567 5568 if (conn_ioctl_cleanup_reqd) 5569 conn_ioctl_cleanup(connp); 5570 5571 if (is_system_labeled() && connp->conn_anon_port) { 5572 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 5573 connp->conn_mlp_type, connp->conn_ulp, 5574 ntohs(connp->conn_lport), B_FALSE); 5575 connp->conn_anon_port = 0; 5576 } 5577 connp->conn_mlp_type = mlptSingle; 5578 5579 /* 5580 * Remove this conn from any fanout list it is on. 5581 * and then wait for any threads currently operating 5582 * on this endpoint to finish 5583 */ 5584 ipcl_hash_remove(connp); 5585 5586 /* 5587 * Remove this conn from the drain list, and do 5588 * any other cleanup that may be required. 5589 * (Only non-tcp streams may have a non-null conn_idl. 5590 * TCP streams are never flow controlled, and 5591 * conn_idl will be null) 5592 */ 5593 if (drain_cleanup_reqd) 5594 conn_drain_tail(connp, B_TRUE); 5595 5596 if (connp->conn_rq == ipst->ips_ip_g_mrouter || 5597 connp->conn_wq == ipst->ips_ip_g_mrouter) 5598 (void) ip_mrouter_done(NULL, ipst); 5599 5600 if (ilg_cleanup_reqd) 5601 ilg_delete_all(connp); 5602 5603 conn_delete_ire(connp, NULL); 5604 5605 /* 5606 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 5607 * callers from write side can't be there now because close 5608 * is in progress. The only other caller is ipcl_walk 5609 * which checks for the condemned flag. 5610 */ 5611 mutex_enter(&connp->conn_lock); 5612 connp->conn_state_flags |= CONN_CONDEMNED; 5613 while (connp->conn_ref != 1) 5614 cv_wait(&connp->conn_cv, &connp->conn_lock); 5615 connp->conn_state_flags |= CONN_QUIESCED; 5616 mutex_exit(&connp->conn_lock); 5617 } 5618 5619 /* ARGSUSED */ 5620 int 5621 ip_close(queue_t *q, int flags) 5622 { 5623 conn_t *connp; 5624 5625 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 5626 5627 /* 5628 * Call the appropriate delete routine depending on whether this is 5629 * a module or device. 5630 */ 5631 if (WR(q)->q_next != NULL) { 5632 /* This is a module close */ 5633 return (ip_modclose((ill_t *)q->q_ptr)); 5634 } 5635 5636 connp = q->q_ptr; 5637 ip_quiesce_conn(connp); 5638 5639 qprocsoff(q); 5640 5641 /* 5642 * Now we are truly single threaded on this stream, and can 5643 * delete the things hanging off the connp, and finally the connp. 5644 * We removed this connp from the fanout list, it cannot be 5645 * accessed thru the fanouts, and we already waited for the 5646 * conn_ref to drop to 0. We are already in close, so 5647 * there cannot be any other thread from the top. qprocsoff 5648 * has completed, and service has completed or won't run in 5649 * future. 5650 */ 5651 ASSERT(connp->conn_ref == 1); 5652 5653 /* 5654 * A conn which was previously marked as IPCL_UDP cannot 5655 * retain the flag because it would have been cleared by 5656 * udp_close(). 5657 */ 5658 ASSERT(!IPCL_IS_UDP(connp)); 5659 5660 if (connp->conn_latch != NULL) { 5661 IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack); 5662 connp->conn_latch = NULL; 5663 } 5664 if (connp->conn_policy != NULL) { 5665 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 5666 connp->conn_policy = NULL; 5667 } 5668 if (connp->conn_ipsec_opt_mp != NULL) { 5669 freemsg(connp->conn_ipsec_opt_mp); 5670 connp->conn_ipsec_opt_mp = NULL; 5671 } 5672 5673 inet_minor_free(ip_minor_arena, connp->conn_dev); 5674 5675 connp->conn_ref--; 5676 ipcl_conn_destroy(connp); 5677 5678 q->q_ptr = WR(q)->q_ptr = NULL; 5679 return (0); 5680 } 5681 5682 int 5683 ip_snmpmod_close(queue_t *q) 5684 { 5685 conn_t *connp = Q_TO_CONN(q); 5686 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5687 5688 qprocsoff(q); 5689 5690 if (connp->conn_flags & IPCL_UDPMOD) 5691 udp_close_free(connp); 5692 5693 if (connp->conn_cred != NULL) { 5694 crfree(connp->conn_cred); 5695 connp->conn_cred = NULL; 5696 } 5697 CONN_DEC_REF(connp); 5698 q->q_ptr = WR(q)->q_ptr = NULL; 5699 return (0); 5700 } 5701 5702 /* 5703 * Write side put procedure for TCP module or UDP module instance. TCP/UDP 5704 * as a module is only used for MIB browsers that push TCP/UDP over IP or ARP. 5705 * The only supported primitives are T_SVR4_OPTMGMT_REQ and T_OPTMGMT_REQ. 5706 * M_FLUSH messages and ioctls are only passed downstream; we don't flush our 5707 * queues as we never enqueue messages there and we don't handle any ioctls. 5708 * Everything else is freed. 5709 */ 5710 void 5711 ip_snmpmod_wput(queue_t *q, mblk_t *mp) 5712 { 5713 conn_t *connp = q->q_ptr; 5714 pfi_t setfn; 5715 pfi_t getfn; 5716 5717 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5718 5719 switch (DB_TYPE(mp)) { 5720 case M_PROTO: 5721 case M_PCPROTO: 5722 if ((MBLKL(mp) >= sizeof (t_scalar_t)) && 5723 ((((union T_primitives *)mp->b_rptr)->type == 5724 T_SVR4_OPTMGMT_REQ) || 5725 (((union T_primitives *)mp->b_rptr)->type == 5726 T_OPTMGMT_REQ))) { 5727 /* 5728 * This is the only TPI primitive supported. Its 5729 * handling does not require tcp_t, but it does require 5730 * conn_t to check permissions. 5731 */ 5732 cred_t *cr = DB_CREDDEF(mp, connp->conn_cred); 5733 5734 if (connp->conn_flags & IPCL_TCPMOD) { 5735 setfn = tcp_snmp_set; 5736 getfn = tcp_snmp_get; 5737 } else { 5738 setfn = udp_snmp_set; 5739 getfn = udp_snmp_get; 5740 } 5741 if (!snmpcom_req(q, mp, setfn, getfn, cr)) { 5742 freemsg(mp); 5743 return; 5744 } 5745 } else if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, ENOTSUP)) 5746 != NULL) 5747 qreply(q, mp); 5748 break; 5749 case M_FLUSH: 5750 case M_IOCTL: 5751 putnext(q, mp); 5752 break; 5753 default: 5754 freemsg(mp); 5755 break; 5756 } 5757 } 5758 5759 /* Return the IP checksum for the IP header at "iph". */ 5760 uint16_t 5761 ip_csum_hdr(ipha_t *ipha) 5762 { 5763 uint16_t *uph; 5764 uint32_t sum; 5765 int opt_len; 5766 5767 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - 5768 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 5769 uph = (uint16_t *)ipha; 5770 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 5771 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 5772 if (opt_len > 0) { 5773 do { 5774 sum += uph[10]; 5775 sum += uph[11]; 5776 uph += 2; 5777 } while (--opt_len); 5778 } 5779 sum = (sum & 0xFFFF) + (sum >> 16); 5780 sum = ~(sum + (sum >> 16)) & 0xFFFF; 5781 if (sum == 0xffff) 5782 sum = 0; 5783 return ((uint16_t)sum); 5784 } 5785 5786 /* 5787 * Called when the module is about to be unloaded 5788 */ 5789 void 5790 ip_ddi_destroy(void) 5791 { 5792 tnet_fini(); 5793 5794 sctp_ddi_g_destroy(); 5795 tcp_ddi_g_destroy(); 5796 ipsec_policy_g_destroy(); 5797 ipcl_g_destroy(); 5798 ip_net_g_destroy(); 5799 ip_ire_g_fini(); 5800 inet_minor_destroy(ip_minor_arena); 5801 5802 netstack_unregister(NS_IP); 5803 } 5804 5805 /* 5806 * First step in cleanup. 5807 */ 5808 /* ARGSUSED */ 5809 static void 5810 ip_stack_shutdown(netstackid_t stackid, void *arg) 5811 { 5812 ip_stack_t *ipst = (ip_stack_t *)arg; 5813 5814 #ifdef NS_DEBUG 5815 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid); 5816 #endif 5817 5818 /* Get rid of loopback interfaces and their IREs */ 5819 ip_loopback_cleanup(ipst); 5820 } 5821 5822 /* 5823 * Free the IP stack instance. 5824 */ 5825 static void 5826 ip_stack_fini(netstackid_t stackid, void *arg) 5827 { 5828 ip_stack_t *ipst = (ip_stack_t *)arg; 5829 int ret; 5830 5831 #ifdef NS_DEBUG 5832 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid); 5833 #endif 5834 ipv4_hook_destroy(ipst); 5835 ipv6_hook_destroy(ipst); 5836 ip_net_destroy(ipst); 5837 5838 rw_destroy(&ipst->ips_srcid_lock); 5839 5840 ip_kstat_fini(stackid, ipst->ips_ip_mibkp); 5841 ipst->ips_ip_mibkp = NULL; 5842 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp); 5843 ipst->ips_icmp_mibkp = NULL; 5844 ip_kstat2_fini(stackid, ipst->ips_ip_kstat); 5845 ipst->ips_ip_kstat = NULL; 5846 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics)); 5847 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat); 5848 ipst->ips_ip6_kstat = NULL; 5849 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics)); 5850 5851 nd_free(&ipst->ips_ip_g_nd); 5852 kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr)); 5853 ipst->ips_param_arr = NULL; 5854 kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 5855 ipst->ips_ndp_arr = NULL; 5856 5857 ip_mrouter_stack_destroy(ipst); 5858 5859 mutex_destroy(&ipst->ips_ip_mi_lock); 5860 rw_destroy(&ipst->ips_ipsec_capab_ills_lock); 5861 rw_destroy(&ipst->ips_ill_g_usesrc_lock); 5862 rw_destroy(&ipst->ips_ip_g_nd_lock); 5863 5864 ret = untimeout(ipst->ips_igmp_timeout_id); 5865 if (ret == -1) { 5866 ASSERT(ipst->ips_igmp_timeout_id == 0); 5867 } else { 5868 ASSERT(ipst->ips_igmp_timeout_id != 0); 5869 ipst->ips_igmp_timeout_id = 0; 5870 } 5871 ret = untimeout(ipst->ips_igmp_slowtimeout_id); 5872 if (ret == -1) { 5873 ASSERT(ipst->ips_igmp_slowtimeout_id == 0); 5874 } else { 5875 ASSERT(ipst->ips_igmp_slowtimeout_id != 0); 5876 ipst->ips_igmp_slowtimeout_id = 0; 5877 } 5878 ret = untimeout(ipst->ips_mld_timeout_id); 5879 if (ret == -1) { 5880 ASSERT(ipst->ips_mld_timeout_id == 0); 5881 } else { 5882 ASSERT(ipst->ips_mld_timeout_id != 0); 5883 ipst->ips_mld_timeout_id = 0; 5884 } 5885 ret = untimeout(ipst->ips_mld_slowtimeout_id); 5886 if (ret == -1) { 5887 ASSERT(ipst->ips_mld_slowtimeout_id == 0); 5888 } else { 5889 ASSERT(ipst->ips_mld_slowtimeout_id != 0); 5890 ipst->ips_mld_slowtimeout_id = 0; 5891 } 5892 ret = untimeout(ipst->ips_ip_ire_expire_id); 5893 if (ret == -1) { 5894 ASSERT(ipst->ips_ip_ire_expire_id == 0); 5895 } else { 5896 ASSERT(ipst->ips_ip_ire_expire_id != 0); 5897 ipst->ips_ip_ire_expire_id = 0; 5898 } 5899 5900 mutex_destroy(&ipst->ips_igmp_timer_lock); 5901 mutex_destroy(&ipst->ips_mld_timer_lock); 5902 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock); 5903 mutex_destroy(&ipst->ips_mld_slowtimeout_lock); 5904 mutex_destroy(&ipst->ips_ip_addr_avail_lock); 5905 rw_destroy(&ipst->ips_ill_g_lock); 5906 5907 ip_ire_fini(ipst); 5908 ip6_asp_free(ipst); 5909 conn_drain_fini(ipst); 5910 ipcl_destroy(ipst); 5911 5912 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock); 5913 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock); 5914 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t)); 5915 ipst->ips_ndp4 = NULL; 5916 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t)); 5917 ipst->ips_ndp6 = NULL; 5918 5919 if (ipst->ips_loopback_ksp != NULL) { 5920 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid); 5921 ipst->ips_loopback_ksp = NULL; 5922 } 5923 5924 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t)); 5925 ipst->ips_phyint_g_list = NULL; 5926 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS); 5927 ipst->ips_ill_g_heads = NULL; 5928 5929 kmem_free(ipst, sizeof (*ipst)); 5930 } 5931 5932 /* 5933 * Called when the IP kernel module is loaded into the kernel 5934 */ 5935 void 5936 ip_ddi_init(void) 5937 { 5938 TCP6_MAJ = ddi_name_to_major(TCP6); 5939 TCP_MAJ = ddi_name_to_major(TCP); 5940 SCTP_MAJ = ddi_name_to_major(SCTP); 5941 SCTP6_MAJ = ddi_name_to_major(SCTP6); 5942 5943 ip_input_proc = ip_squeue_switch(ip_squeue_enter); 5944 5945 /* 5946 * For IP and TCP the minor numbers should start from 2 since we have 4 5947 * initial devices: ip, ip6, tcp, tcp6. 5948 */ 5949 if ((ip_minor_arena = inet_minor_create("ip_minor_arena", 5950 INET_MIN_DEV + 2, KM_SLEEP)) == NULL) { 5951 cmn_err(CE_PANIC, 5952 "ip_ddi_init: ip_minor_arena creation failed\n"); 5953 } 5954 5955 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5956 5957 ipcl_g_init(); 5958 ip_ire_g_init(); 5959 ip_net_g_init(); 5960 5961 /* 5962 * We want to be informed each time a stack is created or 5963 * destroyed in the kernel, so we can maintain the 5964 * set of udp_stack_t's. 5965 */ 5966 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown, 5967 ip_stack_fini); 5968 5969 ipsec_policy_g_init(); 5970 tcp_ddi_g_init(); 5971 sctp_ddi_g_init(); 5972 5973 tnet_init(); 5974 } 5975 5976 /* 5977 * Initialize the IP stack instance. 5978 */ 5979 static void * 5980 ip_stack_init(netstackid_t stackid, netstack_t *ns) 5981 { 5982 ip_stack_t *ipst; 5983 ipparam_t *pa; 5984 ipndp_t *na; 5985 5986 #ifdef NS_DEBUG 5987 printf("ip_stack_init(stack %d)\n", stackid); 5988 #endif 5989 5990 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP); 5991 ipst->ips_netstack = ns; 5992 5993 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS, 5994 KM_SLEEP); 5995 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t), 5996 KM_SLEEP); 5997 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5998 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5999 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 6000 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 6001 6002 rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 6003 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6004 ipst->ips_igmp_deferred_next = INFINITY; 6005 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6006 ipst->ips_mld_deferred_next = INFINITY; 6007 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6008 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6009 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 6010 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 6011 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL); 6012 rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 6013 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 6014 6015 ipcl_init(ipst); 6016 ip_ire_init(ipst); 6017 ip6_asp_init(ipst); 6018 ipif_init(ipst); 6019 conn_drain_init(ipst); 6020 ip_mrouter_stack_init(ipst); 6021 6022 ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT; 6023 ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 6024 6025 ipst->ips_ip_multirt_log_interval = 1000; 6026 6027 ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT; 6028 ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT; 6029 ipst->ips_ill_index = 1; 6030 6031 ipst->ips_saved_ip_g_forward = -1; 6032 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */ 6033 6034 pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 6035 ipst->ips_param_arr = pa; 6036 bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr)); 6037 6038 na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP); 6039 ipst->ips_ndp_arr = na; 6040 bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 6041 ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data = 6042 (caddr_t)&ipst->ips_ip_g_forward; 6043 ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data = 6044 (caddr_t)&ipst->ips_ipv6_forward; 6045 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name, 6046 "ip_cgtp_filter") == 0); 6047 ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data = 6048 (caddr_t)&ip_cgtp_filter; 6049 6050 (void) ip_param_register(&ipst->ips_ip_g_nd, 6051 ipst->ips_param_arr, A_CNT(lcl_param_arr), 6052 ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr)); 6053 6054 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst); 6055 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid); 6056 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics); 6057 ipst->ips_ip6_kstat = 6058 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics); 6059 6060 ipst->ips_ipmp_enable_failback = B_TRUE; 6061 6062 ipst->ips_ip_src_id = 1; 6063 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL); 6064 6065 ip_net_init(ipst, ns); 6066 ipv4_hook_init(ipst); 6067 ipv6_hook_init(ipst); 6068 6069 return (ipst); 6070 } 6071 6072 /* 6073 * Allocate and initialize a DLPI template of the specified length. (May be 6074 * called as writer.) 6075 */ 6076 mblk_t * 6077 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 6078 { 6079 mblk_t *mp; 6080 6081 mp = allocb(len, BPRI_MED); 6082 if (!mp) 6083 return (NULL); 6084 6085 /* 6086 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 6087 * of which we don't seem to use) are sent with M_PCPROTO, and 6088 * that other DLPI are M_PROTO. 6089 */ 6090 if (prim == DL_INFO_REQ) { 6091 mp->b_datap->db_type = M_PCPROTO; 6092 } else { 6093 mp->b_datap->db_type = M_PROTO; 6094 } 6095 6096 mp->b_wptr = mp->b_rptr + len; 6097 bzero(mp->b_rptr, len); 6098 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 6099 return (mp); 6100 } 6101 6102 const char * 6103 dlpi_prim_str(int prim) 6104 { 6105 switch (prim) { 6106 case DL_INFO_REQ: return ("DL_INFO_REQ"); 6107 case DL_INFO_ACK: return ("DL_INFO_ACK"); 6108 case DL_ATTACH_REQ: return ("DL_ATTACH_REQ"); 6109 case DL_DETACH_REQ: return ("DL_DETACH_REQ"); 6110 case DL_BIND_REQ: return ("DL_BIND_REQ"); 6111 case DL_BIND_ACK: return ("DL_BIND_ACK"); 6112 case DL_UNBIND_REQ: return ("DL_UNBIND_REQ"); 6113 case DL_OK_ACK: return ("DL_OK_ACK"); 6114 case DL_ERROR_ACK: return ("DL_ERROR_ACK"); 6115 case DL_ENABMULTI_REQ: return ("DL_ENABMULTI_REQ"); 6116 case DL_DISABMULTI_REQ: return ("DL_DISABMULTI_REQ"); 6117 case DL_PROMISCON_REQ: return ("DL_PROMISCON_REQ"); 6118 case DL_PROMISCOFF_REQ: return ("DL_PROMISCOFF_REQ"); 6119 case DL_UNITDATA_REQ: return ("DL_UNITDATA_REQ"); 6120 case DL_UNITDATA_IND: return ("DL_UNITDATA_IND"); 6121 case DL_UDERROR_IND: return ("DL_UDERROR_IND"); 6122 case DL_PHYS_ADDR_REQ: return ("DL_PHYS_ADDR_REQ"); 6123 case DL_PHYS_ADDR_ACK: return ("DL_PHYS_ADDR_ACK"); 6124 case DL_SET_PHYS_ADDR_REQ: return ("DL_SET_PHYS_ADDR_REQ"); 6125 case DL_NOTIFY_REQ: return ("DL_NOTIFY_REQ"); 6126 case DL_NOTIFY_ACK: return ("DL_NOTIFY_ACK"); 6127 case DL_NOTIFY_IND: return ("DL_NOTIFY_IND"); 6128 case DL_CAPABILITY_REQ: return ("DL_CAPABILITY_REQ"); 6129 case DL_CAPABILITY_ACK: return ("DL_CAPABILITY_ACK"); 6130 case DL_CONTROL_REQ: return ("DL_CONTROL_REQ"); 6131 case DL_CONTROL_ACK: return ("DL_CONTROL_ACK"); 6132 default: return ("<unknown primitive>"); 6133 } 6134 } 6135 6136 const char * 6137 dlpi_err_str(int err) 6138 { 6139 switch (err) { 6140 case DL_ACCESS: return ("DL_ACCESS"); 6141 case DL_BADADDR: return ("DL_BADADDR"); 6142 case DL_BADCORR: return ("DL_BADCORR"); 6143 case DL_BADDATA: return ("DL_BADDATA"); 6144 case DL_BADPPA: return ("DL_BADPPA"); 6145 case DL_BADPRIM: return ("DL_BADPRIM"); 6146 case DL_BADQOSPARAM: return ("DL_BADQOSPARAM"); 6147 case DL_BADQOSTYPE: return ("DL_BADQOSTYPE"); 6148 case DL_BADSAP: return ("DL_BADSAP"); 6149 case DL_BADTOKEN: return ("DL_BADTOKEN"); 6150 case DL_BOUND: return ("DL_BOUND"); 6151 case DL_INITFAILED: return ("DL_INITFAILED"); 6152 case DL_NOADDR: return ("DL_NOADDR"); 6153 case DL_NOTINIT: return ("DL_NOTINIT"); 6154 case DL_OUTSTATE: return ("DL_OUTSTATE"); 6155 case DL_SYSERR: return ("DL_SYSERR"); 6156 case DL_UNSUPPORTED: return ("DL_UNSUPPORTED"); 6157 case DL_UNDELIVERABLE: return ("DL_UNDELIVERABLE"); 6158 case DL_NOTSUPPORTED : return ("DL_NOTSUPPORTED "); 6159 case DL_TOOMANY: return ("DL_TOOMANY"); 6160 case DL_NOTENAB: return ("DL_NOTENAB"); 6161 case DL_BUSY: return ("DL_BUSY"); 6162 case DL_NOAUTO: return ("DL_NOAUTO"); 6163 case DL_NOXIDAUTO: return ("DL_NOXIDAUTO"); 6164 case DL_NOTESTAUTO: return ("DL_NOTESTAUTO"); 6165 case DL_XIDAUTO: return ("DL_XIDAUTO"); 6166 case DL_TESTAUTO: return ("DL_TESTAUTO"); 6167 case DL_PENDING: return ("DL_PENDING"); 6168 default: return ("<unknown error>"); 6169 } 6170 } 6171 6172 /* 6173 * Debug formatting routine. Returns a character string representation of the 6174 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 6175 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 6176 * 6177 * Once the ndd table-printing interfaces are removed, this can be changed to 6178 * standard dotted-decimal form. 6179 */ 6180 char * 6181 ip_dot_addr(ipaddr_t addr, char *buf) 6182 { 6183 uint8_t *ap = (uint8_t *)&addr; 6184 6185 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 6186 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 6187 return (buf); 6188 } 6189 6190 /* 6191 * Write the given MAC address as a printable string in the usual colon- 6192 * separated format. 6193 */ 6194 const char * 6195 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 6196 { 6197 char *bp; 6198 6199 if (alen == 0 || buflen < 4) 6200 return ("?"); 6201 bp = buf; 6202 for (;;) { 6203 /* 6204 * If there are more MAC address bytes available, but we won't 6205 * have any room to print them, then add "..." to the string 6206 * instead. See below for the 'magic number' explanation. 6207 */ 6208 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 6209 (void) strcpy(bp, "..."); 6210 break; 6211 } 6212 (void) sprintf(bp, "%02x", *addr++); 6213 bp += 2; 6214 if (--alen == 0) 6215 break; 6216 *bp++ = ':'; 6217 buflen -= 3; 6218 /* 6219 * At this point, based on the first 'if' statement above, 6220 * either alen == 1 and buflen >= 3, or alen > 1 and 6221 * buflen >= 4. The first case leaves room for the final "xx" 6222 * number and trailing NUL byte. The second leaves room for at 6223 * least "...". Thus the apparently 'magic' numbers chosen for 6224 * that statement. 6225 */ 6226 } 6227 return (buf); 6228 } 6229 6230 /* 6231 * Send an ICMP error after patching up the packet appropriately. Returns 6232 * non-zero if the appropriate MIB should be bumped; zero otherwise. 6233 */ 6234 static boolean_t 6235 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 6236 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, 6237 zoneid_t zoneid, ip_stack_t *ipst) 6238 { 6239 ipha_t *ipha; 6240 mblk_t *first_mp; 6241 boolean_t secure; 6242 unsigned char db_type; 6243 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6244 6245 first_mp = mp; 6246 if (mctl_present) { 6247 mp = mp->b_cont; 6248 secure = ipsec_in_is_secure(first_mp); 6249 ASSERT(mp != NULL); 6250 } else { 6251 /* 6252 * If this is an ICMP error being reported - which goes 6253 * up as M_CTLs, we need to convert them to M_DATA till 6254 * we finish checking with global policy because 6255 * ipsec_check_global_policy() assumes M_DATA as clear 6256 * and M_CTL as secure. 6257 */ 6258 db_type = DB_TYPE(mp); 6259 DB_TYPE(mp) = M_DATA; 6260 secure = B_FALSE; 6261 } 6262 /* 6263 * We are generating an icmp error for some inbound packet. 6264 * Called from all ip_fanout_(udp, tcp, proto) functions. 6265 * Before we generate an error, check with global policy 6266 * to see whether this is allowed to enter the system. As 6267 * there is no "conn", we are checking with global policy. 6268 */ 6269 ipha = (ipha_t *)mp->b_rptr; 6270 if (secure || ipss->ipsec_inbound_v4_policy_present) { 6271 first_mp = ipsec_check_global_policy(first_mp, NULL, 6272 ipha, NULL, mctl_present, ipst->ips_netstack); 6273 if (first_mp == NULL) 6274 return (B_FALSE); 6275 } 6276 6277 if (!mctl_present) 6278 DB_TYPE(mp) = db_type; 6279 6280 if (flags & IP_FF_SEND_ICMP) { 6281 if (flags & IP_FF_HDR_COMPLETE) { 6282 if (ip_hdr_complete(ipha, zoneid, ipst)) { 6283 freemsg(first_mp); 6284 return (B_TRUE); 6285 } 6286 } 6287 if (flags & IP_FF_CKSUM) { 6288 /* 6289 * Have to correct checksum since 6290 * the packet might have been 6291 * fragmented and the reassembly code in ip_rput 6292 * does not restore the IP checksum. 6293 */ 6294 ipha->ipha_hdr_checksum = 0; 6295 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6296 } 6297 switch (icmp_type) { 6298 case ICMP_DEST_UNREACHABLE: 6299 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid, 6300 ipst); 6301 break; 6302 default: 6303 freemsg(first_mp); 6304 break; 6305 } 6306 } else { 6307 freemsg(first_mp); 6308 return (B_FALSE); 6309 } 6310 6311 return (B_TRUE); 6312 } 6313 6314 /* 6315 * Used to send an ICMP error message when a packet is received for 6316 * a protocol that is not supported. The mblk passed as argument 6317 * is consumed by this function. 6318 */ 6319 void 6320 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid, 6321 ip_stack_t *ipst) 6322 { 6323 mblk_t *mp; 6324 ipha_t *ipha; 6325 ill_t *ill; 6326 ipsec_in_t *ii; 6327 6328 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6329 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6330 6331 mp = ipsec_mp->b_cont; 6332 ipsec_mp->b_cont = NULL; 6333 ipha = (ipha_t *)mp->b_rptr; 6334 /* Get ill from index in ipsec_in_t. */ 6335 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 6336 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL, 6337 ipst); 6338 if (ill != NULL) { 6339 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 6340 if (ip_fanout_send_icmp(q, mp, flags, 6341 ICMP_DEST_UNREACHABLE, 6342 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) { 6343 BUMP_MIB(ill->ill_ip_mib, 6344 ipIfStatsInUnknownProtos); 6345 } 6346 } else { 6347 if (ip_fanout_send_icmp_v6(q, mp, flags, 6348 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 6349 0, B_FALSE, zoneid, ipst)) { 6350 BUMP_MIB(ill->ill_ip_mib, 6351 ipIfStatsInUnknownProtos); 6352 } 6353 } 6354 ill_refrele(ill); 6355 } else { /* re-link for the freemsg() below. */ 6356 ipsec_mp->b_cont = mp; 6357 } 6358 6359 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 6360 freemsg(ipsec_mp); 6361 } 6362 6363 /* 6364 * See if the inbound datagram has had IPsec processing applied to it. 6365 */ 6366 boolean_t 6367 ipsec_in_is_secure(mblk_t *ipsec_mp) 6368 { 6369 ipsec_in_t *ii; 6370 6371 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6372 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6373 6374 if (ii->ipsec_in_loopback) { 6375 return (ii->ipsec_in_secure); 6376 } else { 6377 return (ii->ipsec_in_ah_sa != NULL || 6378 ii->ipsec_in_esp_sa != NULL || 6379 ii->ipsec_in_decaps); 6380 } 6381 } 6382 6383 /* 6384 * Handle protocols with which IP is less intimate. There 6385 * can be more than one stream bound to a particular 6386 * protocol. When this is the case, normally each one gets a copy 6387 * of any incoming packets. 6388 * 6389 * IPSEC NOTE : 6390 * 6391 * Don't allow a secure packet going up a non-secure connection. 6392 * We don't allow this because 6393 * 6394 * 1) Reply might go out in clear which will be dropped at 6395 * the sending side. 6396 * 2) If the reply goes out in clear it will give the 6397 * adversary enough information for getting the key in 6398 * most of the cases. 6399 * 6400 * Moreover getting a secure packet when we expect clear 6401 * implies that SA's were added without checking for 6402 * policy on both ends. This should not happen once ISAKMP 6403 * is used to negotiate SAs as SAs will be added only after 6404 * verifying the policy. 6405 * 6406 * NOTE : If the packet was tunneled and not multicast we only send 6407 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 6408 * back to delivering packets to AF_INET6 raw sockets. 6409 * 6410 * IPQoS Notes: 6411 * Once we have determined the client, invoke IPPF processing. 6412 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6413 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6414 * ip_policy will be false. 6415 * 6416 * Zones notes: 6417 * Currently only applications in the global zone can create raw sockets for 6418 * protocols other than ICMP. So unlike the broadcast / multicast case of 6419 * ip_fanout_udp(), we only send a copy of the packet to streams in the 6420 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 6421 */ 6422 static void 6423 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 6424 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 6425 zoneid_t zoneid) 6426 { 6427 queue_t *rq; 6428 mblk_t *mp1, *first_mp1; 6429 uint_t protocol = ipha->ipha_protocol; 6430 ipaddr_t dst; 6431 boolean_t one_only; 6432 mblk_t *first_mp = mp; 6433 boolean_t secure; 6434 uint32_t ill_index; 6435 conn_t *connp, *first_connp, *next_connp; 6436 connf_t *connfp; 6437 boolean_t shared_addr; 6438 mib2_ipIfStatsEntry_t *mibptr; 6439 ip_stack_t *ipst = recv_ill->ill_ipst; 6440 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6441 6442 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 6443 if (mctl_present) { 6444 mp = first_mp->b_cont; 6445 secure = ipsec_in_is_secure(first_mp); 6446 ASSERT(mp != NULL); 6447 } else { 6448 secure = B_FALSE; 6449 } 6450 dst = ipha->ipha_dst; 6451 /* 6452 * If the packet was tunneled and not multicast we only send to it 6453 * the first match. 6454 */ 6455 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 6456 !CLASSD(dst)); 6457 6458 shared_addr = (zoneid == ALL_ZONES); 6459 if (shared_addr) { 6460 /* 6461 * We don't allow multilevel ports for raw IP, so no need to 6462 * check for that here. 6463 */ 6464 zoneid = tsol_packet_to_zoneid(mp); 6465 } 6466 6467 connfp = &ipst->ips_ipcl_proto_fanout[protocol]; 6468 mutex_enter(&connfp->connf_lock); 6469 connp = connfp->connf_head; 6470 for (connp = connfp->connf_head; connp != NULL; 6471 connp = connp->conn_next) { 6472 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, 6473 zoneid) && 6474 (!is_system_labeled() || 6475 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6476 connp))) 6477 break; 6478 } 6479 6480 if (connp == NULL || connp->conn_upq == NULL) { 6481 /* 6482 * No one bound to these addresses. Is 6483 * there a client that wants all 6484 * unclaimed datagrams? 6485 */ 6486 mutex_exit(&connfp->connf_lock); 6487 /* 6488 * Check for IPPROTO_ENCAP... 6489 */ 6490 if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) { 6491 /* 6492 * If an IPsec mblk is here on a multicast 6493 * tunnel (using ip_mroute stuff), check policy here, 6494 * THEN ship off to ip_mroute_decap(). 6495 * 6496 * BTW, If I match a configured IP-in-IP 6497 * tunnel, this path will not be reached, and 6498 * ip_mroute_decap will never be called. 6499 */ 6500 first_mp = ipsec_check_global_policy(first_mp, connp, 6501 ipha, NULL, mctl_present, ipst->ips_netstack); 6502 if (first_mp != NULL) { 6503 if (mctl_present) 6504 freeb(first_mp); 6505 ip_mroute_decap(q, mp, ill); 6506 } /* Else we already freed everything! */ 6507 } else { 6508 /* 6509 * Otherwise send an ICMP protocol unreachable. 6510 */ 6511 if (ip_fanout_send_icmp(q, first_mp, flags, 6512 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 6513 mctl_present, zoneid, ipst)) { 6514 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos); 6515 } 6516 } 6517 return; 6518 } 6519 CONN_INC_REF(connp); 6520 first_connp = connp; 6521 6522 /* 6523 * Only send message to one tunnel driver by immediately 6524 * terminating the loop. 6525 */ 6526 connp = one_only ? NULL : connp->conn_next; 6527 6528 for (;;) { 6529 while (connp != NULL) { 6530 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 6531 flags, zoneid) && 6532 (!is_system_labeled() || 6533 tsol_receive_local(mp, &dst, IPV4_VERSION, 6534 shared_addr, connp))) 6535 break; 6536 connp = connp->conn_next; 6537 } 6538 6539 /* 6540 * Copy the packet. 6541 */ 6542 if (connp == NULL || connp->conn_upq == NULL || 6543 (((first_mp1 = dupmsg(first_mp)) == NULL) && 6544 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 6545 /* 6546 * No more interested clients or memory 6547 * allocation failed 6548 */ 6549 connp = first_connp; 6550 break; 6551 } 6552 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 6553 CONN_INC_REF(connp); 6554 mutex_exit(&connfp->connf_lock); 6555 rq = connp->conn_rq; 6556 if (!canputnext(rq)) { 6557 if (flags & IP_FF_RAWIP) { 6558 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6559 } else { 6560 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6561 } 6562 6563 freemsg(first_mp1); 6564 } else { 6565 /* 6566 * Don't enforce here if we're an actual tunnel - 6567 * let "tun" do it instead. 6568 */ 6569 if (!IPCL_IS_IPTUN(connp) && 6570 (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || 6571 secure)) { 6572 first_mp1 = ipsec_check_inbound_policy 6573 (first_mp1, connp, ipha, NULL, 6574 mctl_present); 6575 } 6576 if (first_mp1 != NULL) { 6577 int in_flags = 0; 6578 /* 6579 * ip_fanout_proto also gets called from 6580 * icmp_inbound_error_fanout, in which case 6581 * the msg type is M_CTL. Don't add info 6582 * in this case for the time being. In future 6583 * when there is a need for knowing the 6584 * inbound iface index for ICMP error msgs, 6585 * then this can be changed. 6586 */ 6587 if (connp->conn_recvif) 6588 in_flags = IPF_RECVIF; 6589 /* 6590 * The ULP may support IP_RECVPKTINFO for both 6591 * IP v4 and v6 so pass the appropriate argument 6592 * based on conn IP version. 6593 */ 6594 if (connp->conn_ip_recvpktinfo) { 6595 if (connp->conn_af_isv6) { 6596 /* 6597 * V6 only needs index 6598 */ 6599 in_flags |= IPF_RECVIF; 6600 } else { 6601 /* 6602 * V4 needs index + 6603 * matching address. 6604 */ 6605 in_flags |= IPF_RECVADDR; 6606 } 6607 } 6608 if ((in_flags != 0) && 6609 (mp->b_datap->db_type != M_CTL)) { 6610 /* 6611 * the actual data will be 6612 * contained in b_cont upon 6613 * successful return of the 6614 * following call else 6615 * original mblk is returned 6616 */ 6617 ASSERT(recv_ill != NULL); 6618 mp1 = ip_add_info(mp1, recv_ill, 6619 in_flags, IPCL_ZONEID(connp), ipst); 6620 } 6621 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6622 if (mctl_present) 6623 freeb(first_mp1); 6624 putnext(rq, mp1); 6625 } 6626 } 6627 mutex_enter(&connfp->connf_lock); 6628 /* Follow the next pointer before releasing the conn. */ 6629 next_connp = connp->conn_next; 6630 CONN_DEC_REF(connp); 6631 connp = next_connp; 6632 } 6633 6634 /* Last one. Send it upstream. */ 6635 mutex_exit(&connfp->connf_lock); 6636 6637 /* 6638 * If this packet is coming from icmp_inbound_error_fanout ip_policy 6639 * will be set to false. 6640 */ 6641 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6642 ill_index = ill->ill_phyint->phyint_ifindex; 6643 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6644 if (mp == NULL) { 6645 CONN_DEC_REF(connp); 6646 if (mctl_present) { 6647 freeb(first_mp); 6648 } 6649 return; 6650 } 6651 } 6652 6653 rq = connp->conn_rq; 6654 if (!canputnext(rq)) { 6655 if (flags & IP_FF_RAWIP) { 6656 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6657 } else { 6658 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6659 } 6660 6661 freemsg(first_mp); 6662 } else { 6663 if (IPCL_IS_IPTUN(connp)) { 6664 /* 6665 * Tunneled packet. We enforce policy in the tunnel 6666 * module itself. 6667 * 6668 * Send the WHOLE packet up (incl. IPSEC_IN) without 6669 * a policy check. 6670 */ 6671 putnext(rq, first_mp); 6672 CONN_DEC_REF(connp); 6673 return; 6674 } 6675 6676 if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) { 6677 first_mp = ipsec_check_inbound_policy(first_mp, connp, 6678 ipha, NULL, mctl_present); 6679 } 6680 6681 if (first_mp != NULL) { 6682 int in_flags = 0; 6683 6684 /* 6685 * ip_fanout_proto also gets called 6686 * from icmp_inbound_error_fanout, in 6687 * which case the msg type is M_CTL. 6688 * Don't add info in this case for time 6689 * being. In future when there is a 6690 * need for knowing the inbound iface 6691 * index for ICMP error msgs, then this 6692 * can be changed 6693 */ 6694 if (connp->conn_recvif) 6695 in_flags = IPF_RECVIF; 6696 if (connp->conn_ip_recvpktinfo) { 6697 if (connp->conn_af_isv6) { 6698 /* 6699 * V6 only needs index 6700 */ 6701 in_flags |= IPF_RECVIF; 6702 } else { 6703 /* 6704 * V4 needs index + 6705 * matching address. 6706 */ 6707 in_flags |= IPF_RECVADDR; 6708 } 6709 } 6710 if ((in_flags != 0) && 6711 (mp->b_datap->db_type != M_CTL)) { 6712 6713 /* 6714 * the actual data will be contained in 6715 * b_cont upon successful return 6716 * of the following call else original 6717 * mblk is returned 6718 */ 6719 ASSERT(recv_ill != NULL); 6720 mp = ip_add_info(mp, recv_ill, 6721 in_flags, IPCL_ZONEID(connp), ipst); 6722 } 6723 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6724 putnext(rq, mp); 6725 if (mctl_present) 6726 freeb(first_mp); 6727 } 6728 } 6729 CONN_DEC_REF(connp); 6730 } 6731 6732 /* 6733 * Fanout for TCP packets 6734 * The caller puts <fport, lport> in the ports parameter. 6735 * 6736 * IPQoS Notes 6737 * Before sending it to the client, invoke IPPF processing. 6738 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6739 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6740 * ip_policy is false. 6741 */ 6742 static void 6743 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 6744 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 6745 { 6746 mblk_t *first_mp; 6747 boolean_t secure; 6748 uint32_t ill_index; 6749 int ip_hdr_len; 6750 tcph_t *tcph; 6751 boolean_t syn_present = B_FALSE; 6752 conn_t *connp; 6753 ip_stack_t *ipst = recv_ill->ill_ipst; 6754 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6755 6756 ASSERT(recv_ill != NULL); 6757 6758 first_mp = mp; 6759 if (mctl_present) { 6760 ASSERT(first_mp->b_datap->db_type == M_CTL); 6761 mp = first_mp->b_cont; 6762 secure = ipsec_in_is_secure(first_mp); 6763 ASSERT(mp != NULL); 6764 } else { 6765 secure = B_FALSE; 6766 } 6767 6768 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 6769 6770 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 6771 zoneid, ipst)) == NULL) { 6772 /* 6773 * No connected connection or listener. Send a 6774 * TH_RST via tcp_xmit_listeners_reset. 6775 */ 6776 6777 /* Initiate IPPf processing, if needed. */ 6778 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 6779 uint32_t ill_index; 6780 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6781 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 6782 if (first_mp == NULL) 6783 return; 6784 } 6785 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6786 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", 6787 zoneid)); 6788 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6789 ipst->ips_netstack->netstack_tcp); 6790 return; 6791 } 6792 6793 /* 6794 * Allocate the SYN for the TCP connection here itself 6795 */ 6796 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 6797 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 6798 if (IPCL_IS_TCP(connp)) { 6799 squeue_t *sqp; 6800 6801 /* 6802 * For fused tcp loopback, assign the eager's 6803 * squeue to be that of the active connect's. 6804 * Note that we don't check for IP_FF_LOOPBACK 6805 * here since this routine gets called only 6806 * for loopback (unlike the IPv6 counterpart). 6807 */ 6808 ASSERT(Q_TO_CONN(q) != NULL); 6809 if (do_tcp_fusion && 6810 !CONN_INBOUND_POLICY_PRESENT(connp, ipss) && 6811 !secure && 6812 !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy && 6813 IPCL_IS_TCP(Q_TO_CONN(q))) { 6814 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 6815 sqp = Q_TO_CONN(q)->conn_sqp; 6816 } else { 6817 sqp = IP_SQUEUE_GET(lbolt); 6818 } 6819 6820 mp->b_datap->db_struioflag |= STRUIO_EAGER; 6821 DB_CKSUMSTART(mp) = (intptr_t)sqp; 6822 syn_present = B_TRUE; 6823 } 6824 } 6825 6826 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 6827 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 6828 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6829 if ((flags & TH_RST) || (flags & TH_URG)) { 6830 CONN_DEC_REF(connp); 6831 freemsg(first_mp); 6832 return; 6833 } 6834 if (flags & TH_ACK) { 6835 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6836 ipst->ips_netstack->netstack_tcp); 6837 CONN_DEC_REF(connp); 6838 return; 6839 } 6840 6841 CONN_DEC_REF(connp); 6842 freemsg(first_mp); 6843 return; 6844 } 6845 6846 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6847 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6848 NULL, mctl_present); 6849 if (first_mp == NULL) { 6850 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6851 CONN_DEC_REF(connp); 6852 return; 6853 } 6854 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 6855 ASSERT(syn_present); 6856 if (mctl_present) { 6857 ASSERT(first_mp != mp); 6858 first_mp->b_datap->db_struioflag |= 6859 STRUIO_POLICY; 6860 } else { 6861 ASSERT(first_mp == mp); 6862 mp->b_datap->db_struioflag &= 6863 ~STRUIO_EAGER; 6864 mp->b_datap->db_struioflag |= 6865 STRUIO_POLICY; 6866 } 6867 } else { 6868 /* 6869 * Discard first_mp early since we're dealing with a 6870 * fully-connected conn_t and tcp doesn't do policy in 6871 * this case. 6872 */ 6873 if (mctl_present) { 6874 freeb(first_mp); 6875 mctl_present = B_FALSE; 6876 } 6877 first_mp = mp; 6878 } 6879 } 6880 6881 /* 6882 * Initiate policy processing here if needed. If we get here from 6883 * icmp_inbound_error_fanout, ip_policy is false. 6884 */ 6885 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6886 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6887 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6888 if (mp == NULL) { 6889 CONN_DEC_REF(connp); 6890 if (mctl_present) 6891 freeb(first_mp); 6892 return; 6893 } else if (mctl_present) { 6894 ASSERT(first_mp != mp); 6895 first_mp->b_cont = mp; 6896 } else { 6897 first_mp = mp; 6898 } 6899 } 6900 6901 6902 6903 /* Handle socket options. */ 6904 if (!syn_present && 6905 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6906 /* Add header */ 6907 ASSERT(recv_ill != NULL); 6908 /* 6909 * Since tcp does not support IP_RECVPKTINFO for V4, only pass 6910 * IPF_RECVIF. 6911 */ 6912 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp), 6913 ipst); 6914 if (mp == NULL) { 6915 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6916 CONN_DEC_REF(connp); 6917 if (mctl_present) 6918 freeb(first_mp); 6919 return; 6920 } else if (mctl_present) { 6921 /* 6922 * ip_add_info might return a new mp. 6923 */ 6924 ASSERT(first_mp != mp); 6925 first_mp->b_cont = mp; 6926 } else { 6927 first_mp = mp; 6928 } 6929 } 6930 6931 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6932 if (IPCL_IS_TCP(connp)) { 6933 (*ip_input_proc)(connp->conn_sqp, first_mp, 6934 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); 6935 } else { 6936 putnext(connp->conn_rq, first_mp); 6937 CONN_DEC_REF(connp); 6938 } 6939 } 6940 6941 /* 6942 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 6943 * We are responsible for disposing of mp, such as by freemsg() or putnext() 6944 * Caller is responsible for dropping references to the conn, and freeing 6945 * first_mp. 6946 * 6947 * IPQoS Notes 6948 * Before sending it to the client, invoke IPPF processing. Policy processing 6949 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 6950 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 6951 * ip_wput_local, ip_policy is false. 6952 */ 6953 static void 6954 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 6955 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 6956 boolean_t ip_policy) 6957 { 6958 boolean_t mctl_present = (first_mp != NULL); 6959 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 6960 uint32_t ill_index; 6961 ip_stack_t *ipst = recv_ill->ill_ipst; 6962 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6963 6964 ASSERT(ill != NULL); 6965 6966 if (mctl_present) 6967 first_mp->b_cont = mp; 6968 else 6969 first_mp = mp; 6970 6971 if (CONN_UDP_FLOWCTLD(connp)) { 6972 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 6973 freemsg(first_mp); 6974 return; 6975 } 6976 6977 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6978 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6979 NULL, mctl_present); 6980 if (first_mp == NULL) { 6981 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 6982 return; /* Freed by ipsec_check_inbound_policy(). */ 6983 } 6984 } 6985 if (mctl_present) 6986 freeb(first_mp); 6987 6988 /* Handle options. */ 6989 if (connp->conn_recvif) 6990 in_flags = IPF_RECVIF; 6991 /* 6992 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag 6993 * passed to ip_add_info is based on IP version of connp. 6994 */ 6995 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6996 if (connp->conn_af_isv6) { 6997 /* 6998 * V6 only needs index 6999 */ 7000 in_flags |= IPF_RECVIF; 7001 } else { 7002 /* 7003 * V4 needs index + matching address. 7004 */ 7005 in_flags |= IPF_RECVADDR; 7006 } 7007 } 7008 7009 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 7010 in_flags |= IPF_RECVSLLA; 7011 7012 /* 7013 * Initiate IPPF processing here, if needed. Note first_mp won't be 7014 * freed if the packet is dropped. The caller will do so. 7015 */ 7016 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 7017 ill_index = recv_ill->ill_phyint->phyint_ifindex; 7018 ip_process(IPP_LOCAL_IN, &mp, ill_index); 7019 if (mp == NULL) { 7020 return; 7021 } 7022 } 7023 if ((in_flags != 0) && 7024 (mp->b_datap->db_type != M_CTL)) { 7025 /* 7026 * The actual data will be contained in b_cont 7027 * upon successful return of the following call 7028 * else original mblk is returned 7029 */ 7030 ASSERT(recv_ill != NULL); 7031 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp), 7032 ipst); 7033 } 7034 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 7035 /* Send it upstream */ 7036 CONN_UDP_RECV(connp, mp); 7037 } 7038 7039 /* 7040 * Fanout for UDP packets. 7041 * The caller puts <fport, lport> in the ports parameter. 7042 * 7043 * If SO_REUSEADDR is set all multicast and broadcast packets 7044 * will be delivered to all streams bound to the same port. 7045 * 7046 * Zones notes: 7047 * Multicast and broadcast packets will be distributed to streams in all zones. 7048 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 7049 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 7050 * packets. To maintain this behavior with multiple zones, the conns are grouped 7051 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 7052 * each zone. If unset, all the following conns in the same zone are skipped. 7053 */ 7054 static void 7055 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 7056 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 7057 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 7058 { 7059 uint32_t dstport, srcport; 7060 ipaddr_t dst; 7061 mblk_t *first_mp; 7062 boolean_t secure; 7063 in6_addr_t v6src; 7064 conn_t *connp; 7065 connf_t *connfp; 7066 conn_t *first_connp; 7067 conn_t *next_connp; 7068 mblk_t *mp1, *first_mp1; 7069 ipaddr_t src; 7070 zoneid_t last_zoneid; 7071 boolean_t reuseaddr; 7072 boolean_t shared_addr; 7073 ip_stack_t *ipst; 7074 7075 ASSERT(recv_ill != NULL); 7076 ipst = recv_ill->ill_ipst; 7077 7078 first_mp = mp; 7079 if (mctl_present) { 7080 mp = first_mp->b_cont; 7081 first_mp->b_cont = NULL; 7082 secure = ipsec_in_is_secure(first_mp); 7083 ASSERT(mp != NULL); 7084 } else { 7085 first_mp = NULL; 7086 secure = B_FALSE; 7087 } 7088 7089 /* Extract ports in net byte order */ 7090 dstport = htons(ntohl(ports) & 0xFFFF); 7091 srcport = htons(ntohl(ports) >> 16); 7092 dst = ipha->ipha_dst; 7093 src = ipha->ipha_src; 7094 7095 shared_addr = (zoneid == ALL_ZONES); 7096 if (shared_addr) { 7097 /* 7098 * No need to handle exclusive-stack zones since ALL_ZONES 7099 * only applies to the shared stack. 7100 */ 7101 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 7102 if (zoneid == ALL_ZONES) 7103 zoneid = tsol_packet_to_zoneid(mp); 7104 } 7105 7106 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7107 mutex_enter(&connfp->connf_lock); 7108 connp = connfp->connf_head; 7109 if (!broadcast && !CLASSD(dst)) { 7110 /* 7111 * Not broadcast or multicast. Send to the one (first) 7112 * client we find. No need to check conn_wantpacket() 7113 * since IP_BOUND_IF/conn_incoming_ill does not apply to 7114 * IPv4 unicast packets. 7115 */ 7116 while ((connp != NULL) && 7117 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || 7118 !IPCL_ZONE_MATCH(connp, zoneid))) { 7119 connp = connp->conn_next; 7120 } 7121 7122 if (connp == NULL || connp->conn_upq == NULL) 7123 goto notfound; 7124 7125 if (is_system_labeled() && 7126 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7127 connp)) 7128 goto notfound; 7129 7130 CONN_INC_REF(connp); 7131 mutex_exit(&connfp->connf_lock); 7132 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7133 flags, recv_ill, ip_policy); 7134 IP_STAT(ipst, ip_udp_fannorm); 7135 CONN_DEC_REF(connp); 7136 return; 7137 } 7138 7139 /* 7140 * Broadcast and multicast case 7141 * 7142 * Need to check conn_wantpacket(). 7143 * If SO_REUSEADDR has been set on the first we send the 7144 * packet to all clients that have joined the group and 7145 * match the port. 7146 */ 7147 7148 while (connp != NULL) { 7149 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 7150 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7151 (!is_system_labeled() || 7152 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7153 connp))) 7154 break; 7155 connp = connp->conn_next; 7156 } 7157 7158 if (connp == NULL || connp->conn_upq == NULL) 7159 goto notfound; 7160 7161 first_connp = connp; 7162 /* 7163 * When SO_REUSEADDR is not set, send the packet only to the first 7164 * matching connection in its zone by keeping track of the zoneid. 7165 */ 7166 reuseaddr = first_connp->conn_reuseaddr; 7167 last_zoneid = first_connp->conn_zoneid; 7168 7169 CONN_INC_REF(connp); 7170 connp = connp->conn_next; 7171 for (;;) { 7172 while (connp != NULL) { 7173 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 7174 (reuseaddr || connp->conn_zoneid != last_zoneid) && 7175 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7176 (!is_system_labeled() || 7177 tsol_receive_local(mp, &dst, IPV4_VERSION, 7178 shared_addr, connp))) 7179 break; 7180 connp = connp->conn_next; 7181 } 7182 /* 7183 * Just copy the data part alone. The mctl part is 7184 * needed just for verifying policy and it is never 7185 * sent up. 7186 */ 7187 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7188 ((mp1 = copymsg(mp)) == NULL))) { 7189 /* 7190 * No more interested clients or memory 7191 * allocation failed 7192 */ 7193 connp = first_connp; 7194 break; 7195 } 7196 if (connp->conn_zoneid != last_zoneid) { 7197 /* 7198 * Update the zoneid so that the packet isn't sent to 7199 * any more conns in the same zone unless SO_REUSEADDR 7200 * is set. 7201 */ 7202 reuseaddr = connp->conn_reuseaddr; 7203 last_zoneid = connp->conn_zoneid; 7204 } 7205 if (first_mp != NULL) { 7206 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7207 ipsec_info_type == IPSEC_IN); 7208 first_mp1 = ipsec_in_tag(first_mp, NULL, 7209 ipst->ips_netstack); 7210 if (first_mp1 == NULL) { 7211 freemsg(mp1); 7212 connp = first_connp; 7213 break; 7214 } 7215 } else { 7216 first_mp1 = NULL; 7217 } 7218 CONN_INC_REF(connp); 7219 mutex_exit(&connfp->connf_lock); 7220 /* 7221 * IPQoS notes: We don't send the packet for policy 7222 * processing here, will do it for the last one (below). 7223 * i.e. we do it per-packet now, but if we do policy 7224 * processing per-conn, then we would need to do it 7225 * here too. 7226 */ 7227 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7228 ipha, flags, recv_ill, B_FALSE); 7229 mutex_enter(&connfp->connf_lock); 7230 /* Follow the next pointer before releasing the conn. */ 7231 next_connp = connp->conn_next; 7232 IP_STAT(ipst, ip_udp_fanmb); 7233 CONN_DEC_REF(connp); 7234 connp = next_connp; 7235 } 7236 7237 /* Last one. Send it upstream. */ 7238 mutex_exit(&connfp->connf_lock); 7239 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7240 recv_ill, ip_policy); 7241 IP_STAT(ipst, ip_udp_fanmb); 7242 CONN_DEC_REF(connp); 7243 return; 7244 7245 notfound: 7246 7247 mutex_exit(&connfp->connf_lock); 7248 IP_STAT(ipst, ip_udp_fanothers); 7249 /* 7250 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 7251 * have already been matched above, since they live in the IPv4 7252 * fanout tables. This implies we only need to 7253 * check for IPv6 in6addr_any endpoints here. 7254 * Thus we compare using ipv6_all_zeros instead of the destination 7255 * address, except for the multicast group membership lookup which 7256 * uses the IPv4 destination. 7257 */ 7258 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 7259 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7260 mutex_enter(&connfp->connf_lock); 7261 connp = connfp->connf_head; 7262 if (!broadcast && !CLASSD(dst)) { 7263 while (connp != NULL) { 7264 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7265 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && 7266 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7267 !connp->conn_ipv6_v6only) 7268 break; 7269 connp = connp->conn_next; 7270 } 7271 7272 if (connp != NULL && is_system_labeled() && 7273 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7274 connp)) 7275 connp = NULL; 7276 7277 if (connp == NULL || connp->conn_upq == NULL) { 7278 /* 7279 * No one bound to this port. Is 7280 * there a client that wants all 7281 * unclaimed datagrams? 7282 */ 7283 mutex_exit(&connfp->connf_lock); 7284 7285 if (mctl_present) 7286 first_mp->b_cont = mp; 7287 else 7288 first_mp = mp; 7289 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP]. 7290 connf_head != NULL) { 7291 ip_fanout_proto(q, first_mp, ill, ipha, 7292 flags | IP_FF_RAWIP, mctl_present, 7293 ip_policy, recv_ill, zoneid); 7294 } else { 7295 if (ip_fanout_send_icmp(q, first_mp, flags, 7296 ICMP_DEST_UNREACHABLE, 7297 ICMP_PORT_UNREACHABLE, 7298 mctl_present, zoneid, ipst)) { 7299 BUMP_MIB(ill->ill_ip_mib, 7300 udpIfStatsNoPorts); 7301 } 7302 } 7303 return; 7304 } 7305 7306 CONN_INC_REF(connp); 7307 mutex_exit(&connfp->connf_lock); 7308 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7309 flags, recv_ill, ip_policy); 7310 CONN_DEC_REF(connp); 7311 return; 7312 } 7313 /* 7314 * IPv4 multicast packet being delivered to an AF_INET6 7315 * in6addr_any endpoint. 7316 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 7317 * and not conn_wantpacket_v6() since any multicast membership is 7318 * for an IPv4-mapped multicast address. 7319 * The packet is sent to all clients in all zones that have joined the 7320 * group and match the port. 7321 */ 7322 while (connp != NULL) { 7323 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7324 srcport, v6src) && 7325 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7326 (!is_system_labeled() || 7327 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7328 connp))) 7329 break; 7330 connp = connp->conn_next; 7331 } 7332 7333 if (connp == NULL || connp->conn_upq == NULL) { 7334 /* 7335 * No one bound to this port. Is 7336 * there a client that wants all 7337 * unclaimed datagrams? 7338 */ 7339 mutex_exit(&connfp->connf_lock); 7340 7341 if (mctl_present) 7342 first_mp->b_cont = mp; 7343 else 7344 first_mp = mp; 7345 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head != 7346 NULL) { 7347 ip_fanout_proto(q, first_mp, ill, ipha, 7348 flags | IP_FF_RAWIP, mctl_present, ip_policy, 7349 recv_ill, zoneid); 7350 } else { 7351 /* 7352 * We used to attempt to send an icmp error here, but 7353 * since this is known to be a multicast packet 7354 * and we don't send icmp errors in response to 7355 * multicast, just drop the packet and give up sooner. 7356 */ 7357 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 7358 freemsg(first_mp); 7359 } 7360 return; 7361 } 7362 7363 first_connp = connp; 7364 7365 CONN_INC_REF(connp); 7366 connp = connp->conn_next; 7367 for (;;) { 7368 while (connp != NULL) { 7369 if (IPCL_UDP_MATCH_V6(connp, dstport, 7370 ipv6_all_zeros, srcport, v6src) && 7371 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7372 (!is_system_labeled() || 7373 tsol_receive_local(mp, &dst, IPV4_VERSION, 7374 shared_addr, connp))) 7375 break; 7376 connp = connp->conn_next; 7377 } 7378 /* 7379 * Just copy the data part alone. The mctl part is 7380 * needed just for verifying policy and it is never 7381 * sent up. 7382 */ 7383 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7384 ((mp1 = copymsg(mp)) == NULL))) { 7385 /* 7386 * No more intested clients or memory 7387 * allocation failed 7388 */ 7389 connp = first_connp; 7390 break; 7391 } 7392 if (first_mp != NULL) { 7393 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7394 ipsec_info_type == IPSEC_IN); 7395 first_mp1 = ipsec_in_tag(first_mp, NULL, 7396 ipst->ips_netstack); 7397 if (first_mp1 == NULL) { 7398 freemsg(mp1); 7399 connp = first_connp; 7400 break; 7401 } 7402 } else { 7403 first_mp1 = NULL; 7404 } 7405 CONN_INC_REF(connp); 7406 mutex_exit(&connfp->connf_lock); 7407 /* 7408 * IPQoS notes: We don't send the packet for policy 7409 * processing here, will do it for the last one (below). 7410 * i.e. we do it per-packet now, but if we do policy 7411 * processing per-conn, then we would need to do it 7412 * here too. 7413 */ 7414 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7415 ipha, flags, recv_ill, B_FALSE); 7416 mutex_enter(&connfp->connf_lock); 7417 /* Follow the next pointer before releasing the conn. */ 7418 next_connp = connp->conn_next; 7419 CONN_DEC_REF(connp); 7420 connp = next_connp; 7421 } 7422 7423 /* Last one. Send it upstream. */ 7424 mutex_exit(&connfp->connf_lock); 7425 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7426 recv_ill, ip_policy); 7427 CONN_DEC_REF(connp); 7428 } 7429 7430 /* 7431 * Complete the ip_wput header so that it 7432 * is possible to generate ICMP 7433 * errors. 7434 */ 7435 int 7436 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst) 7437 { 7438 ire_t *ire; 7439 7440 if (ipha->ipha_src == INADDR_ANY) { 7441 ire = ire_lookup_local(zoneid, ipst); 7442 if (ire == NULL) { 7443 ip1dbg(("ip_hdr_complete: no source IRE\n")); 7444 return (1); 7445 } 7446 ipha->ipha_src = ire->ire_addr; 7447 ire_refrele(ire); 7448 } 7449 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 7450 ipha->ipha_hdr_checksum = 0; 7451 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 7452 return (0); 7453 } 7454 7455 /* 7456 * Nobody should be sending 7457 * packets up this stream 7458 */ 7459 static void 7460 ip_lrput(queue_t *q, mblk_t *mp) 7461 { 7462 mblk_t *mp1; 7463 7464 switch (mp->b_datap->db_type) { 7465 case M_FLUSH: 7466 /* Turn around */ 7467 if (*mp->b_rptr & FLUSHW) { 7468 *mp->b_rptr &= ~FLUSHR; 7469 qreply(q, mp); 7470 return; 7471 } 7472 break; 7473 } 7474 /* Could receive messages that passed through ar_rput */ 7475 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 7476 mp1->b_prev = mp1->b_next = NULL; 7477 freemsg(mp); 7478 } 7479 7480 /* Nobody should be sending packets down this stream */ 7481 /* ARGSUSED */ 7482 void 7483 ip_lwput(queue_t *q, mblk_t *mp) 7484 { 7485 freemsg(mp); 7486 } 7487 7488 /* 7489 * Move the first hop in any source route to ipha_dst and remove that part of 7490 * the source route. Called by other protocols. Errors in option formatting 7491 * are ignored - will be handled by ip_wput_options Return the final 7492 * destination (either ipha_dst or the last entry in a source route.) 7493 */ 7494 ipaddr_t 7495 ip_massage_options(ipha_t *ipha, netstack_t *ns) 7496 { 7497 ipoptp_t opts; 7498 uchar_t *opt; 7499 uint8_t optval; 7500 uint8_t optlen; 7501 ipaddr_t dst; 7502 int i; 7503 ire_t *ire; 7504 ip_stack_t *ipst = ns->netstack_ip; 7505 7506 ip2dbg(("ip_massage_options\n")); 7507 dst = ipha->ipha_dst; 7508 for (optval = ipoptp_first(&opts, ipha); 7509 optval != IPOPT_EOL; 7510 optval = ipoptp_next(&opts)) { 7511 opt = opts.ipoptp_cur; 7512 switch (optval) { 7513 uint8_t off; 7514 case IPOPT_SSRR: 7515 case IPOPT_LSRR: 7516 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 7517 ip1dbg(("ip_massage_options: bad src route\n")); 7518 break; 7519 } 7520 optlen = opts.ipoptp_len; 7521 off = opt[IPOPT_OFFSET]; 7522 off--; 7523 redo_srr: 7524 if (optlen < IP_ADDR_LEN || 7525 off > optlen - IP_ADDR_LEN) { 7526 /* End of source route */ 7527 ip1dbg(("ip_massage_options: end of SR\n")); 7528 break; 7529 } 7530 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 7531 ip1dbg(("ip_massage_options: next hop 0x%x\n", 7532 ntohl(dst))); 7533 /* 7534 * Check if our address is present more than 7535 * once as consecutive hops in source route. 7536 * XXX verify per-interface ip_forwarding 7537 * for source route? 7538 */ 7539 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 7540 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 7541 if (ire != NULL) { 7542 ire_refrele(ire); 7543 off += IP_ADDR_LEN; 7544 goto redo_srr; 7545 } 7546 if (dst == htonl(INADDR_LOOPBACK)) { 7547 ip1dbg(("ip_massage_options: loopback addr in " 7548 "source route!\n")); 7549 break; 7550 } 7551 /* 7552 * Update ipha_dst to be the first hop and remove the 7553 * first hop from the source route (by overwriting 7554 * part of the option with NOP options). 7555 */ 7556 ipha->ipha_dst = dst; 7557 /* Put the last entry in dst */ 7558 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 7559 3; 7560 bcopy(&opt[off], &dst, IP_ADDR_LEN); 7561 7562 ip1dbg(("ip_massage_options: last hop 0x%x\n", 7563 ntohl(dst))); 7564 /* Move down and overwrite */ 7565 opt[IP_ADDR_LEN] = opt[0]; 7566 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 7567 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 7568 for (i = 0; i < IP_ADDR_LEN; i++) 7569 opt[i] = IPOPT_NOP; 7570 break; 7571 } 7572 } 7573 return (dst); 7574 } 7575 7576 /* 7577 * This function's job is to forward data to the reverse tunnel (FA->HA) 7578 * after doing a few checks. It is assumed that the incoming interface 7579 * of the packet is always different than the outgoing interface and the 7580 * ire_type of the found ire has to be a non-resolver type. 7581 * 7582 * IPQoS notes 7583 * IP policy is invoked twice for a forwarded packet, once on the read side 7584 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 7585 * enabled. 7586 */ 7587 static void 7588 ip_mrtun_forward(ire_t *ire, ill_t *in_ill, mblk_t *mp) 7589 { 7590 ipha_t *ipha; 7591 queue_t *q; 7592 uint32_t pkt_len; 7593 #define rptr ((uchar_t *)ipha) 7594 uint32_t sum; 7595 uint32_t max_frag; 7596 mblk_t *first_mp; 7597 uint32_t ill_index; 7598 ipxmit_state_t pktxmit_state; 7599 ill_t *out_ill; 7600 ip_stack_t *ipst = in_ill->ill_ipst; 7601 7602 ASSERT(ire != NULL); 7603 ASSERT(ire->ire_ipif->ipif_net_type == IRE_IF_NORESOLVER); 7604 ASSERT(ire->ire_stq != NULL); 7605 7606 /* Initiate read side IPPF processing */ 7607 if (IPP_ENABLED(IPP_FWD_IN, ipst)) { 7608 ill_index = in_ill->ill_phyint->phyint_ifindex; 7609 ip_process(IPP_FWD_IN, &mp, ill_index); 7610 if (mp == NULL) { 7611 ip2dbg(("ip_mrtun_forward: inbound pkt " 7612 "dropped during IPPF processing\n")); 7613 return; 7614 } 7615 } 7616 7617 if (((in_ill->ill_flags & ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 7618 ILLF_ROUTER) == 0) || 7619 (in_ill == (ill_t *)ire->ire_stq->q_ptr)) { 7620 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsForwProhibits); 7621 ip0dbg(("ip_mrtun_forward: Can't forward :" 7622 "forwarding is not turned on\n")); 7623 goto drop_pkt; 7624 } 7625 7626 /* 7627 * Don't forward if the interface is down 7628 */ 7629 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 7630 goto discard_pkt; 7631 } 7632 7633 ipha = (ipha_t *)mp->b_rptr; 7634 pkt_len = ntohs(ipha->ipha_length); 7635 /* Adjust the checksum to reflect the ttl decrement. */ 7636 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 7637 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 7638 if (ipha->ipha_ttl-- <= 1) { 7639 if (ip_csum_hdr(ipha)) { 7640 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInCksumErrs); 7641 goto drop_pkt; 7642 } 7643 q = ire->ire_stq; 7644 if ((first_mp = allocb(sizeof (ipsec_info_t), 7645 BPRI_HI)) == NULL) { 7646 goto discard_pkt; 7647 } 7648 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsForwProhibits); 7649 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7650 /* Sent by forwarding path, and router is global zone */ 7651 icmp_time_exceeded(q, first_mp, ICMP_TTL_EXCEEDED, 7652 GLOBAL_ZONEID, ipst); 7653 return; 7654 } 7655 7656 /* Get the ill_index of the ILL */ 7657 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 7658 7659 /* 7660 * This location is chosen for the placement of the forwarding hook 7661 * because at this point we know that we have a path out for the 7662 * packet but haven't yet applied any logic (such as fragmenting) 7663 * that happen as part of transmitting the packet out. 7664 */ 7665 out_ill = ire->ire_ipif->ipif_ill; 7666 7667 DTRACE_PROBE4(ip4__forwarding__start, 7668 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 7669 7670 FW_HOOKS(ipst->ips_ip4_forwarding_event, 7671 ipst->ips_ipv4firewall_forwarding, 7672 in_ill, out_ill, ipha, mp, mp, ipst); 7673 7674 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 7675 7676 if (mp == NULL) 7677 return; 7678 pkt_len = ntohs(ipha->ipha_length); 7679 7680 /* 7681 * ip_mrtun_forward is only used by foreign agent to reverse 7682 * tunnel the incoming packet. So it does not do any option 7683 * processing for source routing. 7684 */ 7685 max_frag = ire->ire_max_frag; 7686 if (pkt_len > max_frag) { 7687 /* 7688 * It needs fragging on its way out. We haven't 7689 * verified the header checksum yet. Since we 7690 * are going to put a surely good checksum in the 7691 * outgoing header, we have to make sure that it 7692 * was good coming in. 7693 */ 7694 if (ip_csum_hdr(ipha)) { 7695 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInCksumErrs); 7696 goto drop_pkt; 7697 } 7698 7699 /* Initiate write side IPPF processing */ 7700 if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { 7701 ip_process(IPP_FWD_OUT, &mp, ill_index); 7702 if (mp == NULL) { 7703 ip2dbg(("ip_mrtun_forward: outbound pkt "\ 7704 "dropped/deferred during ip policy "\ 7705 "processing\n")); 7706 return; 7707 } 7708 } 7709 if ((first_mp = allocb(sizeof (ipsec_info_t), 7710 BPRI_HI)) == NULL) { 7711 goto discard_pkt; 7712 } 7713 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7714 mp = first_mp; 7715 7716 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID, ipst); 7717 return; 7718 } 7719 7720 ip2dbg(("ip_mrtun_forward: ire type (%d)\n", ire->ire_type)); 7721 7722 ASSERT(ire->ire_ipif != NULL); 7723 7724 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 7725 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 7726 FW_HOOKS(ipst->ips_ip4_physical_out_event, 7727 ipst->ips_ipv4firewall_physical_out, 7728 NULL, out_ill, ipha, mp, mp, ipst); 7729 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 7730 if (mp == NULL) 7731 return; 7732 7733 /* Now send the packet to the tunnel interface */ 7734 mp->b_prev = SET_BPREV_FLAG(IPP_FWD_OUT); 7735 q = ire->ire_stq; 7736 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_FALSE); 7737 if ((pktxmit_state == SEND_FAILED) || 7738 (pktxmit_state == LLHDR_RESLV_FAILED)) { 7739 ip2dbg(("ip_mrtun_forward: failed to send packet to ill %p\n", 7740 q->q_ptr)); 7741 } 7742 7743 return; 7744 discard_pkt: 7745 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInDiscards); 7746 drop_pkt:; 7747 ip2dbg(("ip_mrtun_forward: dropping pkt\n")); 7748 freemsg(mp); 7749 #undef rptr 7750 } 7751 7752 /* 7753 * Fills the ipsec_out_t data structure with appropriate fields and 7754 * prepends it to mp which contains the IP hdr + data that was meant 7755 * to be forwarded. Please note that ipsec_out_info data structure 7756 * is used here to communicate the outgoing ill path at ip_wput() 7757 * for the ICMP error packet. This has nothing to do with ipsec IP 7758 * security. ipsec_out_t is really used to pass the info to the module 7759 * IP where this information cannot be extracted from conn. 7760 * This functions is called by ip_mrtun_forward(). 7761 */ 7762 void 7763 ip_ipsec_out_prepend(mblk_t *first_mp, mblk_t *mp, ill_t *xmit_ill) 7764 { 7765 ipsec_out_t *io; 7766 7767 ASSERT(xmit_ill != NULL); 7768 first_mp->b_datap->db_type = M_CTL; 7769 first_mp->b_wptr += sizeof (ipsec_info_t); 7770 /* 7771 * This is to pass info to ip_wput in absence of conn. 7772 * ipsec_out_secure will be B_FALSE because of this. 7773 * Thus ipsec_out_secure being B_FALSE indicates that 7774 * this is not IPSEC security related information. 7775 */ 7776 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 7777 io = (ipsec_out_t *)first_mp->b_rptr; 7778 io->ipsec_out_type = IPSEC_OUT; 7779 io->ipsec_out_len = sizeof (ipsec_out_t); 7780 first_mp->b_cont = mp; 7781 io->ipsec_out_ill_index = 7782 xmit_ill->ill_phyint->phyint_ifindex; 7783 io->ipsec_out_xmit_if = B_TRUE; 7784 io->ipsec_out_ns = xmit_ill->ill_ipst->ips_netstack; 7785 } 7786 7787 /* 7788 * Return the network mask 7789 * associated with the specified address. 7790 */ 7791 ipaddr_t 7792 ip_net_mask(ipaddr_t addr) 7793 { 7794 uchar_t *up = (uchar_t *)&addr; 7795 ipaddr_t mask = 0; 7796 uchar_t *maskp = (uchar_t *)&mask; 7797 7798 #if defined(__i386) || defined(__amd64) 7799 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 7800 #endif 7801 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 7802 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 7803 #endif 7804 if (CLASSD(addr)) { 7805 maskp[0] = 0xF0; 7806 return (mask); 7807 } 7808 if (addr == 0) 7809 return (0); 7810 maskp[0] = 0xFF; 7811 if ((up[0] & 0x80) == 0) 7812 return (mask); 7813 7814 maskp[1] = 0xFF; 7815 if ((up[0] & 0xC0) == 0x80) 7816 return (mask); 7817 7818 maskp[2] = 0xFF; 7819 if ((up[0] & 0xE0) == 0xC0) 7820 return (mask); 7821 7822 /* Must be experimental or multicast, indicate as much */ 7823 return ((ipaddr_t)0); 7824 } 7825 7826 /* 7827 * Select an ill for the packet by considering load spreading across 7828 * a different ill in the group if dst_ill is part of some group. 7829 */ 7830 ill_t * 7831 ip_newroute_get_dst_ill(ill_t *dst_ill) 7832 { 7833 ill_t *ill; 7834 7835 /* 7836 * We schedule irrespective of whether the source address is 7837 * INADDR_ANY or not. illgrp_scheduler returns a held ill. 7838 */ 7839 ill = illgrp_scheduler(dst_ill); 7840 if (ill == NULL) 7841 return (NULL); 7842 7843 /* 7844 * For groups with names ip_sioctl_groupname ensures that all 7845 * ills are of same type. For groups without names, ifgrp_insert 7846 * ensures this. 7847 */ 7848 ASSERT(dst_ill->ill_type == ill->ill_type); 7849 7850 return (ill); 7851 } 7852 7853 /* 7854 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. 7855 */ 7856 ill_t * 7857 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6, 7858 ip_stack_t *ipst) 7859 { 7860 ill_t *ret_ill; 7861 7862 ASSERT(ifindex != 0); 7863 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL, 7864 ipst); 7865 if (ret_ill == NULL || 7866 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { 7867 if (isv6) { 7868 if (ill != NULL) { 7869 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7870 } else { 7871 BUMP_MIB(&ipst->ips_ip6_mib, 7872 ipIfStatsOutDiscards); 7873 } 7874 ip1dbg(("ip_grab_attach_ill (IPv6): " 7875 "bad ifindex %d.\n", ifindex)); 7876 } else { 7877 if (ill != NULL) { 7878 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7879 } else { 7880 BUMP_MIB(&ipst->ips_ip_mib, 7881 ipIfStatsOutDiscards); 7882 } 7883 ip1dbg(("ip_grab_attach_ill (IPv4): " 7884 "bad ifindex %d.\n", ifindex)); 7885 } 7886 if (ret_ill != NULL) 7887 ill_refrele(ret_ill); 7888 freemsg(first_mp); 7889 return (NULL); 7890 } 7891 7892 return (ret_ill); 7893 } 7894 7895 /* 7896 * IPv4 - 7897 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 7898 * out a packet to a destination address for which we do not have specific 7899 * (or sufficient) routing information. 7900 * 7901 * NOTE : These are the scopes of some of the variables that point at IRE, 7902 * which needs to be followed while making any future modifications 7903 * to avoid memory leaks. 7904 * 7905 * - ire and sire are the entries looked up initially by 7906 * ire_ftable_lookup. 7907 * - ipif_ire is used to hold the interface ire associated with 7908 * the new cache ire. But it's scope is limited, so we always REFRELE 7909 * it before branching out to error paths. 7910 * - save_ire is initialized before ire_create, so that ire returned 7911 * by ire_create will not over-write the ire. We REFRELE save_ire 7912 * before breaking out of the switch. 7913 * 7914 * Thus on failures, we have to REFRELE only ire and sire, if they 7915 * are not NULL. 7916 */ 7917 void 7918 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, ill_t *in_ill, conn_t *connp, 7919 zoneid_t zoneid, ip_stack_t *ipst) 7920 { 7921 areq_t *areq; 7922 ipaddr_t gw = 0; 7923 ire_t *ire = NULL; 7924 mblk_t *res_mp; 7925 ipaddr_t *addrp; 7926 ipaddr_t nexthop_addr; 7927 ipif_t *src_ipif = NULL; 7928 ill_t *dst_ill = NULL; 7929 ipha_t *ipha; 7930 ire_t *sire = NULL; 7931 mblk_t *first_mp; 7932 ire_t *save_ire; 7933 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ 7934 ushort_t ire_marks = 0; 7935 boolean_t mctl_present; 7936 ipsec_out_t *io; 7937 mblk_t *saved_mp; 7938 ire_t *first_sire = NULL; 7939 mblk_t *copy_mp = NULL; 7940 mblk_t *xmit_mp = NULL; 7941 ipaddr_t save_dst; 7942 uint32_t multirt_flags = 7943 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 7944 boolean_t multirt_is_resolvable; 7945 boolean_t multirt_resolve_next; 7946 boolean_t do_attach_ill = B_FALSE; 7947 boolean_t ip_nexthop = B_FALSE; 7948 tsol_ire_gw_secattr_t *attrp = NULL; 7949 tsol_gcgrp_t *gcgrp = NULL; 7950 tsol_gcgrp_addr_t ga; 7951 7952 if (ip_debug > 2) { 7953 /* ip1dbg */ 7954 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 7955 } 7956 7957 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7958 if (mctl_present) { 7959 io = (ipsec_out_t *)first_mp->b_rptr; 7960 ASSERT(io->ipsec_out_type == IPSEC_OUT); 7961 ASSERT(zoneid == io->ipsec_out_zoneid); 7962 ASSERT(zoneid != ALL_ZONES); 7963 } 7964 7965 ipha = (ipha_t *)mp->b_rptr; 7966 7967 /* All multicast lookups come through ip_newroute_ipif() */ 7968 if (CLASSD(dst)) { 7969 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 7970 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 7971 freemsg(first_mp); 7972 return; 7973 } 7974 7975 if (mctl_present && io->ipsec_out_attach_if) { 7976 /* ip_grab_attach_ill returns a held ill */ 7977 attach_ill = ip_grab_attach_ill(NULL, first_mp, 7978 io->ipsec_out_ill_index, B_FALSE, ipst); 7979 7980 /* Failure case frees things for us. */ 7981 if (attach_ill == NULL) 7982 return; 7983 7984 /* 7985 * Check if we need an ire that will not be 7986 * looked up by anybody else i.e. HIDDEN. 7987 */ 7988 if (ill_is_probeonly(attach_ill)) 7989 ire_marks = IRE_MARK_HIDDEN; 7990 } 7991 if (mctl_present && io->ipsec_out_ip_nexthop) { 7992 ip_nexthop = B_TRUE; 7993 nexthop_addr = io->ipsec_out_nexthop_addr; 7994 } 7995 /* 7996 * If this IRE is created for forwarding or it is not for 7997 * traffic for congestion controlled protocols, mark it as temporary. 7998 */ 7999 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 8000 ire_marks |= IRE_MARK_TEMPORARY; 8001 8002 /* 8003 * Get what we can from ire_ftable_lookup which will follow an IRE 8004 * chain until it gets the most specific information available. 8005 * For example, we know that there is no IRE_CACHE for this dest, 8006 * but there may be an IRE_OFFSUBNET which specifies a gateway. 8007 * ire_ftable_lookup will look up the gateway, etc. 8008 * Check if in_ill != NULL. If it is true, the packet must be 8009 * from an incoming interface where RTA_SRCIFP is set. 8010 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 8011 * to the destination, of equal netmask length in the forward table, 8012 * will be recursively explored. If no information is available 8013 * for the final gateway of that route, we force the returned ire 8014 * to be equal to sire using MATCH_IRE_PARENT. 8015 * At least, in this case we have a starting point (in the buckets) 8016 * to look for other routes to the destination in the forward table. 8017 * This is actually used only for multirouting, where a list 8018 * of routes has to be processed in sequence. 8019 * 8020 * In the process of coming up with the most specific information, 8021 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry 8022 * for the gateway (i.e., one for which the ire_nce->nce_state is 8023 * not yet ND_REACHABLE, and is in the middle of arp resolution). 8024 * Two caveats when handling incomplete ire's in ip_newroute: 8025 * - we should be careful when accessing its ire_nce (specifically 8026 * the nce_res_mp) ast it might change underneath our feet, and, 8027 * - not all legacy code path callers are prepared to handle 8028 * incomplete ire's, so we should not create/add incomplete 8029 * ire_cache entries here. (See discussion about temporary solution 8030 * further below). 8031 * 8032 * In order to minimize packet dropping, and to preserve existing 8033 * behavior, we treat this case as if there were no IRE_CACHE for the 8034 * gateway, and instead use the IF_RESOLVER ire to send out 8035 * another request to ARP (this is achieved by passing the 8036 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the 8037 * arp response comes back in ip_wput_nondata, we will create 8038 * a per-dst ire_cache that has an ND_COMPLETE ire. 8039 * 8040 * Note that this is a temporary solution; the correct solution is 8041 * to create an incomplete per-dst ire_cache entry, and send the 8042 * packet out when the gw's nce is resolved. In order to achieve this, 8043 * all packet processing must have been completed prior to calling 8044 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need 8045 * to be modified to accomodate this solution. 8046 */ 8047 if (in_ill != NULL) { 8048 ire = ire_srcif_table_lookup(dst, IRE_IF_RESOLVER, NULL, 8049 in_ill, MATCH_IRE_TYPE); 8050 } else if (ip_nexthop) { 8051 /* 8052 * The first time we come here, we look for an IRE_INTERFACE 8053 * entry for the specified nexthop, set the dst to be the 8054 * nexthop address and create an IRE_CACHE entry for the 8055 * nexthop. The next time around, we are able to find an 8056 * IRE_CACHE entry for the nexthop, set the gateway to be the 8057 * nexthop address and create an IRE_CACHE entry for the 8058 * destination address via the specified nexthop. 8059 */ 8060 ire = ire_cache_lookup(nexthop_addr, zoneid, 8061 MBLK_GETLABEL(mp), ipst); 8062 if (ire != NULL) { 8063 gw = nexthop_addr; 8064 ire_marks |= IRE_MARK_PRIVATE_ADDR; 8065 } else { 8066 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 8067 IRE_INTERFACE, NULL, NULL, zoneid, 0, 8068 MBLK_GETLABEL(mp), 8069 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 8070 ipst); 8071 if (ire != NULL) { 8072 dst = nexthop_addr; 8073 } 8074 } 8075 } else if (attach_ill == NULL) { 8076 ire = ire_ftable_lookup(dst, 0, 0, 0, 8077 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), 8078 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 8079 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | 8080 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE, 8081 ipst); 8082 } else { 8083 /* 8084 * attach_ill is set only for communicating with 8085 * on-link hosts. So, don't look for DEFAULT. 8086 */ 8087 ipif_t *attach_ipif; 8088 8089 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 8090 if (attach_ipif == NULL) { 8091 ill_refrele(attach_ill); 8092 goto icmp_err_ret; 8093 } 8094 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, 8095 &sire, zoneid, 0, MBLK_GETLABEL(mp), 8096 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL | 8097 MATCH_IRE_SECATTR, ipst); 8098 ipif_refrele(attach_ipif); 8099 } 8100 ip3dbg(("ip_newroute: ire_ftable_lookup() " 8101 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 8102 8103 /* 8104 * This loop is run only once in most cases. 8105 * We loop to resolve further routes only when the destination 8106 * can be reached through multiple RTF_MULTIRT-flagged ires. 8107 */ 8108 do { 8109 /* Clear the previous iteration's values */ 8110 if (src_ipif != NULL) { 8111 ipif_refrele(src_ipif); 8112 src_ipif = NULL; 8113 } 8114 if (dst_ill != NULL) { 8115 ill_refrele(dst_ill); 8116 dst_ill = NULL; 8117 } 8118 8119 multirt_resolve_next = B_FALSE; 8120 /* 8121 * We check if packets have to be multirouted. 8122 * In this case, given the current <ire, sire> couple, 8123 * we look for the next suitable <ire, sire>. 8124 * This check is done in ire_multirt_lookup(), 8125 * which applies various criteria to find the next route 8126 * to resolve. ire_multirt_lookup() leaves <ire, sire> 8127 * unchanged if it detects it has not been tried yet. 8128 */ 8129 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8130 ip3dbg(("ip_newroute: starting next_resolution " 8131 "with first_mp %p, tag %d\n", 8132 (void *)first_mp, 8133 MULTIRT_DEBUG_TAGGED(first_mp))); 8134 8135 ASSERT(sire != NULL); 8136 multirt_is_resolvable = 8137 ire_multirt_lookup(&ire, &sire, multirt_flags, 8138 MBLK_GETLABEL(mp), ipst); 8139 8140 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 8141 "ire %p, sire %p\n", 8142 multirt_is_resolvable, 8143 (void *)ire, (void *)sire)); 8144 8145 if (!multirt_is_resolvable) { 8146 /* 8147 * No more multirt route to resolve; give up 8148 * (all routes resolved or no more 8149 * resolvable routes). 8150 */ 8151 if (ire != NULL) { 8152 ire_refrele(ire); 8153 ire = NULL; 8154 } 8155 } else { 8156 ASSERT(sire != NULL); 8157 ASSERT(ire != NULL); 8158 /* 8159 * We simply use first_sire as a flag that 8160 * indicates if a resolvable multirt route 8161 * has already been found. 8162 * If it is not the case, we may have to send 8163 * an ICMP error to report that the 8164 * destination is unreachable. 8165 * We do not IRE_REFHOLD first_sire. 8166 */ 8167 if (first_sire == NULL) { 8168 first_sire = sire; 8169 } 8170 } 8171 } 8172 if (ire == NULL) { 8173 if (ip_debug > 3) { 8174 /* ip2dbg */ 8175 pr_addr_dbg("ip_newroute: " 8176 "can't resolve %s\n", AF_INET, &dst); 8177 } 8178 ip3dbg(("ip_newroute: " 8179 "ire %p, sire %p, first_sire %p\n", 8180 (void *)ire, (void *)sire, (void *)first_sire)); 8181 8182 if (sire != NULL) { 8183 ire_refrele(sire); 8184 sire = NULL; 8185 } 8186 8187 if (first_sire != NULL) { 8188 /* 8189 * At least one multirt route has been found 8190 * in the same call to ip_newroute(); 8191 * there is no need to report an ICMP error. 8192 * first_sire was not IRE_REFHOLDed. 8193 */ 8194 MULTIRT_DEBUG_UNTAG(first_mp); 8195 freemsg(first_mp); 8196 return; 8197 } 8198 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 8199 RTA_DST, ipst); 8200 if (attach_ill != NULL) 8201 ill_refrele(attach_ill); 8202 goto icmp_err_ret; 8203 } 8204 8205 /* 8206 * When RTA_SRCIFP is used to add a route, then an interface 8207 * route is added in the source interface's routing table. 8208 * If the outgoing interface of this route is of type 8209 * IRE_IF_RESOLVER, then upon creation of the ire, 8210 * ire_nce->nce_res_mp is set to NULL. 8211 * Later, when this route is first used for forwarding 8212 * a packet, ip_newroute() is called 8213 * to resolve the hardware address of the outgoing ipif. 8214 * We do not come here for IRE_IF_NORESOLVER entries in the 8215 * source interface based table. We only come here if the 8216 * outgoing interface is a resolver interface and we don't 8217 * have the ire_nce->nce_res_mp information yet. 8218 * If in_ill is not null that means it is called from 8219 * ip_rput. 8220 */ 8221 8222 ASSERT(ire->ire_in_ill == NULL || 8223 (ire->ire_type == IRE_IF_RESOLVER && 8224 ire->ire_nce != NULL && ire->ire_nce->nce_res_mp == NULL)); 8225 8226 /* 8227 * Verify that the returned IRE does not have either 8228 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 8229 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 8230 */ 8231 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 8232 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 8233 if (attach_ill != NULL) 8234 ill_refrele(attach_ill); 8235 goto icmp_err_ret; 8236 } 8237 /* 8238 * Increment the ire_ob_pkt_count field for ire if it is an 8239 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 8240 * increment the same for the parent IRE, sire, if it is some 8241 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST 8242 * and HOST_REDIRECT). 8243 */ 8244 if ((ire->ire_type & IRE_INTERFACE) != 0) { 8245 UPDATE_OB_PKT_COUNT(ire); 8246 ire->ire_last_used_time = lbolt; 8247 } 8248 8249 if (sire != NULL) { 8250 gw = sire->ire_gateway_addr; 8251 ASSERT((sire->ire_type & (IRE_CACHETABLE | 8252 IRE_INTERFACE)) == 0); 8253 UPDATE_OB_PKT_COUNT(sire); 8254 sire->ire_last_used_time = lbolt; 8255 } 8256 /* 8257 * We have a route to reach the destination. 8258 * 8259 * 1) If the interface is part of ill group, try to get a new 8260 * ill taking load spreading into account. 8261 * 8262 * 2) After selecting the ill, get a source address that 8263 * might create good inbound load spreading. 8264 * ipif_select_source does this for us. 8265 * 8266 * If the application specified the ill (ifindex), we still 8267 * load spread. Only if the packets needs to go out 8268 * specifically on a given ill e.g. binding to 8269 * IPIF_NOFAILOVER address, then we don't try to use a 8270 * different ill for load spreading. 8271 */ 8272 if (attach_ill == NULL) { 8273 /* 8274 * Don't perform outbound load spreading in the 8275 * case of an RTF_MULTIRT route, as we actually 8276 * typically want to replicate outgoing packets 8277 * through particular interfaces. 8278 */ 8279 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8280 dst_ill = ire->ire_ipif->ipif_ill; 8281 /* for uniformity */ 8282 ill_refhold(dst_ill); 8283 } else { 8284 /* 8285 * If we are here trying to create an IRE_CACHE 8286 * for an offlink destination and have the 8287 * IRE_CACHE for the next hop and the latter is 8288 * using virtual IP source address selection i.e 8289 * it's ire->ire_ipif is pointing to a virtual 8290 * network interface (vni) then 8291 * ip_newroute_get_dst_ll() will return the vni 8292 * interface as the dst_ill. Since the vni is 8293 * virtual i.e not associated with any physical 8294 * interface, it cannot be the dst_ill, hence 8295 * in such a case call ip_newroute_get_dst_ll() 8296 * with the stq_ill instead of the ire_ipif ILL. 8297 * The function returns a refheld ill. 8298 */ 8299 if ((ire->ire_type == IRE_CACHE) && 8300 IS_VNI(ire->ire_ipif->ipif_ill)) 8301 dst_ill = ip_newroute_get_dst_ill( 8302 ire->ire_stq->q_ptr); 8303 else 8304 dst_ill = ip_newroute_get_dst_ill( 8305 ire->ire_ipif->ipif_ill); 8306 } 8307 if (dst_ill == NULL) { 8308 if (ip_debug > 2) { 8309 pr_addr_dbg("ip_newroute: " 8310 "no dst ill for dst" 8311 " %s\n", AF_INET, &dst); 8312 } 8313 goto icmp_err_ret; 8314 } 8315 } else { 8316 dst_ill = ire->ire_ipif->ipif_ill; 8317 /* for uniformity */ 8318 ill_refhold(dst_ill); 8319 /* 8320 * We should have found a route matching ill as we 8321 * called ire_ftable_lookup with MATCH_IRE_ILL. 8322 * Rather than asserting, when there is a mismatch, 8323 * we just drop the packet. 8324 */ 8325 if (dst_ill != attach_ill) { 8326 ip0dbg(("ip_newroute: Packet dropped as " 8327 "IPIF_NOFAILOVER ill is %s, " 8328 "ire->ire_ipif->ipif_ill is %s\n", 8329 attach_ill->ill_name, 8330 dst_ill->ill_name)); 8331 ill_refrele(attach_ill); 8332 goto icmp_err_ret; 8333 } 8334 } 8335 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ 8336 if (attach_ill != NULL) { 8337 ill_refrele(attach_ill); 8338 attach_ill = NULL; 8339 do_attach_ill = B_TRUE; 8340 } 8341 ASSERT(dst_ill != NULL); 8342 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 8343 8344 /* 8345 * Pick the best source address from dst_ill. 8346 * 8347 * 1) If it is part of a multipathing group, we would 8348 * like to spread the inbound packets across different 8349 * interfaces. ipif_select_source picks a random source 8350 * across the different ills in the group. 8351 * 8352 * 2) If it is not part of a multipathing group, we try 8353 * to pick the source address from the destination 8354 * route. Clustering assumes that when we have multiple 8355 * prefixes hosted on an interface, the prefix of the 8356 * source address matches the prefix of the destination 8357 * route. We do this only if the address is not 8358 * DEPRECATED. 8359 * 8360 * 3) If the conn is in a different zone than the ire, we 8361 * need to pick a source address from the right zone. 8362 * 8363 * NOTE : If we hit case (1) above, the prefix of the source 8364 * address picked may not match the prefix of the 8365 * destination routes prefix as ipif_select_source 8366 * does not look at "dst" while picking a source 8367 * address. 8368 * If we want the same behavior as (2), we will need 8369 * to change the behavior of ipif_select_source. 8370 */ 8371 ASSERT(src_ipif == NULL); 8372 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 8373 /* 8374 * The RTF_SETSRC flag is set in the parent ire (sire). 8375 * Check that the ipif matching the requested source 8376 * address still exists. 8377 */ 8378 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 8379 zoneid, NULL, NULL, NULL, NULL, ipst); 8380 } 8381 if (src_ipif == NULL) { 8382 ire_marks |= IRE_MARK_USESRC_CHECK; 8383 if ((dst_ill->ill_group != NULL) || 8384 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 8385 (connp != NULL && ire->ire_zoneid != zoneid && 8386 ire->ire_zoneid != ALL_ZONES) || 8387 (dst_ill->ill_usesrc_ifindex != 0)) { 8388 /* 8389 * If the destination is reachable via a 8390 * given gateway, the selected source address 8391 * should be in the same subnet as the gateway. 8392 * Otherwise, the destination is not reachable. 8393 * 8394 * If there are no interfaces on the same subnet 8395 * as the destination, ipif_select_source gives 8396 * first non-deprecated interface which might be 8397 * on a different subnet than the gateway. 8398 * This is not desirable. Hence pass the dst_ire 8399 * source address to ipif_select_source. 8400 * It is sure that the destination is reachable 8401 * with the dst_ire source address subnet. 8402 * So passing dst_ire source address to 8403 * ipif_select_source will make sure that the 8404 * selected source will be on the same subnet 8405 * as dst_ire source address. 8406 */ 8407 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 8408 src_ipif = ipif_select_source(dst_ill, saddr, 8409 zoneid); 8410 if (src_ipif == NULL) { 8411 if (ip_debug > 2) { 8412 pr_addr_dbg("ip_newroute: " 8413 "no src for dst %s ", 8414 AF_INET, &dst); 8415 printf("through interface %s\n", 8416 dst_ill->ill_name); 8417 } 8418 goto icmp_err_ret; 8419 } 8420 } else { 8421 src_ipif = ire->ire_ipif; 8422 ASSERT(src_ipif != NULL); 8423 /* hold src_ipif for uniformity */ 8424 ipif_refhold(src_ipif); 8425 } 8426 } 8427 8428 /* 8429 * Assign a source address while we have the conn. 8430 * We can't have ip_wput_ire pick a source address when the 8431 * packet returns from arp since we need to look at 8432 * conn_unspec_src and conn_zoneid, and we lose the conn when 8433 * going through arp. 8434 * 8435 * NOTE : ip_newroute_v6 does not have this piece of code as 8436 * it uses ip6i to store this information. 8437 */ 8438 if (ipha->ipha_src == INADDR_ANY && 8439 (connp == NULL || !connp->conn_unspec_src)) { 8440 ipha->ipha_src = src_ipif->ipif_src_addr; 8441 } 8442 if (ip_debug > 3) { 8443 /* ip2dbg */ 8444 pr_addr_dbg("ip_newroute: first hop %s\n", 8445 AF_INET, &gw); 8446 } 8447 ip2dbg(("\tire type %s (%d)\n", 8448 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 8449 8450 /* 8451 * The TTL of multirouted packets is bounded by the 8452 * ip_multirt_ttl ndd variable. 8453 */ 8454 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8455 /* Force TTL of multirouted packets */ 8456 if ((ipst->ips_ip_multirt_ttl > 0) && 8457 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 8458 ip2dbg(("ip_newroute: forcing multirt TTL " 8459 "to %d (was %d), dst 0x%08x\n", 8460 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 8461 ntohl(sire->ire_addr))); 8462 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 8463 } 8464 } 8465 /* 8466 * At this point in ip_newroute(), ire is either the 8467 * IRE_CACHE of the next-hop gateway for an off-subnet 8468 * destination or an IRE_INTERFACE type that should be used 8469 * to resolve an on-subnet destination or an on-subnet 8470 * next-hop gateway. 8471 * 8472 * In the IRE_CACHE case, we have the following : 8473 * 8474 * 1) src_ipif - used for getting a source address. 8475 * 8476 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8477 * means packets using this IRE_CACHE will go out on 8478 * dst_ill. 8479 * 8480 * 3) The IRE sire will point to the prefix that is the 8481 * longest matching route for the destination. These 8482 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST. 8483 * 8484 * The newly created IRE_CACHE entry for the off-subnet 8485 * destination is tied to both the prefix route and the 8486 * interface route used to resolve the next-hop gateway 8487 * via the ire_phandle and ire_ihandle fields, 8488 * respectively. 8489 * 8490 * In the IRE_INTERFACE case, we have the following : 8491 * 8492 * 1) src_ipif - used for getting a source address. 8493 * 8494 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8495 * means packets using the IRE_CACHE that we will build 8496 * here will go out on dst_ill. 8497 * 8498 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 8499 * to be created will only be tied to the IRE_INTERFACE 8500 * that was derived from the ire_ihandle field. 8501 * 8502 * If sire is non-NULL, it means the destination is 8503 * off-link and we will first create the IRE_CACHE for the 8504 * gateway. Next time through ip_newroute, we will create 8505 * the IRE_CACHE for the final destination as described 8506 * above. 8507 * 8508 * In both cases, after the current resolution has been 8509 * completed (or possibly initialised, in the IRE_INTERFACE 8510 * case), the loop may be re-entered to attempt the resolution 8511 * of another RTF_MULTIRT route. 8512 * 8513 * When an IRE_CACHE entry for the off-subnet destination is 8514 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 8515 * for further processing in emission loops. 8516 */ 8517 save_ire = ire; 8518 switch (ire->ire_type) { 8519 case IRE_CACHE: { 8520 ire_t *ipif_ire; 8521 mblk_t *ire_fp_mp; 8522 8523 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); 8524 if (gw == 0) 8525 gw = ire->ire_gateway_addr; 8526 /* 8527 * We need 3 ire's to create a new cache ire for an 8528 * off-link destination from the cache ire of the 8529 * gateway. 8530 * 8531 * 1. The prefix ire 'sire' (Note that this does 8532 * not apply to the conn_nexthop_set case) 8533 * 2. The cache ire of the gateway 'ire' 8534 * 3. The interface ire 'ipif_ire' 8535 * 8536 * We have (1) and (2). We lookup (3) below. 8537 * 8538 * If there is no interface route to the gateway, 8539 * it is a race condition, where we found the cache 8540 * but the interface route has been deleted. 8541 */ 8542 if (ip_nexthop) { 8543 ipif_ire = ire_ihandle_lookup_onlink(ire); 8544 } else { 8545 ipif_ire = 8546 ire_ihandle_lookup_offlink(ire, sire); 8547 } 8548 if (ipif_ire == NULL) { 8549 ip1dbg(("ip_newroute: " 8550 "ire_ihandle_lookup_offlink failed\n")); 8551 goto icmp_err_ret; 8552 } 8553 /* 8554 * XXX We are using the same res_mp 8555 * (DL_UNITDATA_REQ) though the save_ire is not 8556 * pointing at the same ill. 8557 * This is incorrect. We need to send it up to the 8558 * resolver to get the right res_mp. For ethernets 8559 * this may be okay (ill_type == DL_ETHER). 8560 */ 8561 res_mp = save_ire->ire_nce->nce_res_mp; 8562 ire_fp_mp = NULL; 8563 /* 8564 * save_ire's nce_fp_mp can't change since it is 8565 * not an IRE_MIPRTUN or IRE_BROADCAST 8566 * LOCK_IRE_FP_MP does not do any useful work in 8567 * the case of IRE_CACHE. So we don't use it below. 8568 */ 8569 if (save_ire->ire_stq == dst_ill->ill_wq) 8570 ire_fp_mp = save_ire->ire_nce->nce_fp_mp; 8571 8572 /* 8573 * Check cached gateway IRE for any security 8574 * attributes; if found, associate the gateway 8575 * credentials group to the destination IRE. 8576 */ 8577 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 8578 mutex_enter(&attrp->igsa_lock); 8579 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 8580 GCGRP_REFHOLD(gcgrp); 8581 mutex_exit(&attrp->igsa_lock); 8582 } 8583 8584 ire = ire_create( 8585 (uchar_t *)&dst, /* dest address */ 8586 (uchar_t *)&ip_g_all_ones, /* mask */ 8587 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8588 (uchar_t *)&gw, /* gateway address */ 8589 NULL, 8590 &save_ire->ire_max_frag, 8591 ire_fp_mp, /* Fast Path header */ 8592 dst_ill->ill_rq, /* recv-from queue */ 8593 dst_ill->ill_wq, /* send-to queue */ 8594 IRE_CACHE, /* IRE type */ 8595 res_mp, 8596 src_ipif, 8597 in_ill, /* incoming ill */ 8598 (sire != NULL) ? 8599 sire->ire_mask : 0, /* Parent mask */ 8600 (sire != NULL) ? 8601 sire->ire_phandle : 0, /* Parent handle */ 8602 ipif_ire->ire_ihandle, /* Interface handle */ 8603 (sire != NULL) ? (sire->ire_flags & 8604 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 8605 (sire != NULL) ? 8606 &(sire->ire_uinfo) : &(save_ire->ire_uinfo), 8607 NULL, 8608 gcgrp, 8609 ipst); 8610 8611 if (ire == NULL) { 8612 if (gcgrp != NULL) { 8613 GCGRP_REFRELE(gcgrp); 8614 gcgrp = NULL; 8615 } 8616 ire_refrele(ipif_ire); 8617 ire_refrele(save_ire); 8618 break; 8619 } 8620 8621 /* reference now held by IRE */ 8622 gcgrp = NULL; 8623 8624 ire->ire_marks |= ire_marks; 8625 8626 /* 8627 * Prevent sire and ipif_ire from getting deleted. 8628 * The newly created ire is tied to both of them via 8629 * the phandle and ihandle respectively. 8630 */ 8631 if (sire != NULL) { 8632 IRB_REFHOLD(sire->ire_bucket); 8633 /* Has it been removed already ? */ 8634 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 8635 IRB_REFRELE(sire->ire_bucket); 8636 ire_refrele(ipif_ire); 8637 ire_refrele(save_ire); 8638 break; 8639 } 8640 } 8641 8642 IRB_REFHOLD(ipif_ire->ire_bucket); 8643 /* Has it been removed already ? */ 8644 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 8645 IRB_REFRELE(ipif_ire->ire_bucket); 8646 if (sire != NULL) 8647 IRB_REFRELE(sire->ire_bucket); 8648 ire_refrele(ipif_ire); 8649 ire_refrele(save_ire); 8650 break; 8651 } 8652 8653 xmit_mp = first_mp; 8654 /* 8655 * In the case of multirouting, a copy 8656 * of the packet is done before its sending. 8657 * The copy is used to attempt another 8658 * route resolution, in a next loop. 8659 */ 8660 if (ire->ire_flags & RTF_MULTIRT) { 8661 copy_mp = copymsg(first_mp); 8662 if (copy_mp != NULL) { 8663 xmit_mp = copy_mp; 8664 MULTIRT_DEBUG_TAG(first_mp); 8665 } 8666 } 8667 ire_add_then_send(q, ire, xmit_mp); 8668 ire_refrele(save_ire); 8669 8670 /* Assert that sire is not deleted yet. */ 8671 if (sire != NULL) { 8672 ASSERT(sire->ire_ptpn != NULL); 8673 IRB_REFRELE(sire->ire_bucket); 8674 } 8675 8676 /* Assert that ipif_ire is not deleted yet. */ 8677 ASSERT(ipif_ire->ire_ptpn != NULL); 8678 IRB_REFRELE(ipif_ire->ire_bucket); 8679 ire_refrele(ipif_ire); 8680 8681 /* 8682 * If copy_mp is not NULL, multirouting was 8683 * requested. We loop to initiate a next 8684 * route resolution attempt, starting from sire. 8685 */ 8686 if (copy_mp != NULL) { 8687 /* 8688 * Search for the next unresolved 8689 * multirt route. 8690 */ 8691 copy_mp = NULL; 8692 ipif_ire = NULL; 8693 ire = NULL; 8694 multirt_resolve_next = B_TRUE; 8695 continue; 8696 } 8697 if (sire != NULL) 8698 ire_refrele(sire); 8699 ipif_refrele(src_ipif); 8700 ill_refrele(dst_ill); 8701 return; 8702 } 8703 case IRE_IF_NORESOLVER: { 8704 /* 8705 * We have what we need to build an IRE_CACHE. 8706 * 8707 * Create a new res_mp with the IP gateway address 8708 * in destination address in the DLPI hdr if the 8709 * physical length is exactly 4 bytes. 8710 */ 8711 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 8712 uchar_t *addr; 8713 8714 if (gw) 8715 addr = (uchar_t *)&gw; 8716 else 8717 addr = (uchar_t *)&dst; 8718 8719 res_mp = ill_dlur_gen(addr, 8720 dst_ill->ill_phys_addr_length, 8721 dst_ill->ill_sap, 8722 dst_ill->ill_sap_length); 8723 8724 if (res_mp == NULL) { 8725 ip1dbg(("ip_newroute: res_mp NULL\n")); 8726 break; 8727 } 8728 } else if (dst_ill->ill_resolver_mp == NULL) { 8729 ip1dbg(("ip_newroute: dst_ill %p " 8730 "for IF_NORESOLV ire %p has " 8731 "no ill_resolver_mp\n", 8732 (void *)dst_ill, (void *)ire)); 8733 break; 8734 } else { 8735 res_mp = NULL; 8736 } 8737 8738 /* 8739 * TSol note: We are creating the ire cache for the 8740 * destination 'dst'. If 'dst' is offlink, going 8741 * through the first hop 'gw', the security attributes 8742 * of 'dst' must be set to point to the gateway 8743 * credentials of gateway 'gw'. If 'dst' is onlink, it 8744 * is possible that 'dst' is a potential gateway that is 8745 * referenced by some route that has some security 8746 * attributes. Thus in the former case, we need to do a 8747 * gcgrp_lookup of 'gw' while in the latter case we 8748 * need to do gcgrp_lookup of 'dst' itself. 8749 */ 8750 ga.ga_af = AF_INET; 8751 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, 8752 &ga.ga_addr); 8753 gcgrp = gcgrp_lookup(&ga, B_FALSE); 8754 8755 ire = ire_create( 8756 (uchar_t *)&dst, /* dest address */ 8757 (uchar_t *)&ip_g_all_ones, /* mask */ 8758 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8759 (uchar_t *)&gw, /* gateway address */ 8760 NULL, 8761 &save_ire->ire_max_frag, 8762 NULL, /* Fast Path header */ 8763 dst_ill->ill_rq, /* recv-from queue */ 8764 dst_ill->ill_wq, /* send-to queue */ 8765 IRE_CACHE, 8766 res_mp, 8767 src_ipif, 8768 in_ill, /* Incoming ill */ 8769 save_ire->ire_mask, /* Parent mask */ 8770 (sire != NULL) ? /* Parent handle */ 8771 sire->ire_phandle : 0, 8772 save_ire->ire_ihandle, /* Interface handle */ 8773 (sire != NULL) ? sire->ire_flags & 8774 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 8775 &(save_ire->ire_uinfo), 8776 NULL, 8777 gcgrp, 8778 ipst); 8779 8780 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) 8781 freeb(res_mp); 8782 8783 if (ire == NULL) { 8784 if (gcgrp != NULL) { 8785 GCGRP_REFRELE(gcgrp); 8786 gcgrp = NULL; 8787 } 8788 ire_refrele(save_ire); 8789 break; 8790 } 8791 8792 /* reference now held by IRE */ 8793 gcgrp = NULL; 8794 8795 ire->ire_marks |= ire_marks; 8796 8797 /* Prevent save_ire from getting deleted */ 8798 IRB_REFHOLD(save_ire->ire_bucket); 8799 /* Has it been removed already ? */ 8800 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8801 IRB_REFRELE(save_ire->ire_bucket); 8802 ire_refrele(save_ire); 8803 break; 8804 } 8805 8806 /* 8807 * In the case of multirouting, a copy 8808 * of the packet is made before it is sent. 8809 * The copy is used in the next 8810 * loop to attempt another resolution. 8811 */ 8812 xmit_mp = first_mp; 8813 if ((sire != NULL) && 8814 (sire->ire_flags & RTF_MULTIRT)) { 8815 copy_mp = copymsg(first_mp); 8816 if (copy_mp != NULL) { 8817 xmit_mp = copy_mp; 8818 MULTIRT_DEBUG_TAG(first_mp); 8819 } 8820 } 8821 ire_add_then_send(q, ire, xmit_mp); 8822 8823 /* Assert that it is not deleted yet. */ 8824 ASSERT(save_ire->ire_ptpn != NULL); 8825 IRB_REFRELE(save_ire->ire_bucket); 8826 ire_refrele(save_ire); 8827 8828 if (copy_mp != NULL) { 8829 /* 8830 * If we found a (no)resolver, we ignore any 8831 * trailing top priority IRE_CACHE in further 8832 * loops. This ensures that we do not omit any 8833 * (no)resolver. 8834 * This IRE_CACHE, if any, will be processed 8835 * by another thread entering ip_newroute(). 8836 * IRE_CACHE entries, if any, will be processed 8837 * by another thread entering ip_newroute(), 8838 * (upon resolver response, for instance). 8839 * This aims to force parallel multirt 8840 * resolutions as soon as a packet must be sent. 8841 * In the best case, after the tx of only one 8842 * packet, all reachable routes are resolved. 8843 * Otherwise, the resolution of all RTF_MULTIRT 8844 * routes would require several emissions. 8845 */ 8846 multirt_flags &= ~MULTIRT_CACHEGW; 8847 8848 /* 8849 * Search for the next unresolved multirt 8850 * route. 8851 */ 8852 copy_mp = NULL; 8853 save_ire = NULL; 8854 ire = NULL; 8855 multirt_resolve_next = B_TRUE; 8856 continue; 8857 } 8858 8859 /* 8860 * Don't need sire anymore 8861 */ 8862 if (sire != NULL) 8863 ire_refrele(sire); 8864 8865 ipif_refrele(src_ipif); 8866 ill_refrele(dst_ill); 8867 return; 8868 } 8869 case IRE_IF_RESOLVER: 8870 /* 8871 * We can't build an IRE_CACHE yet, but at least we 8872 * found a resolver that can help. 8873 */ 8874 res_mp = dst_ill->ill_resolver_mp; 8875 if (!OK_RESOLVER_MP(res_mp)) 8876 break; 8877 8878 /* 8879 * To be at this point in the code with a non-zero gw 8880 * means that dst is reachable through a gateway that 8881 * we have never resolved. By changing dst to the gw 8882 * addr we resolve the gateway first. 8883 * When ire_add_then_send() tries to put the IP dg 8884 * to dst, it will reenter ip_newroute() at which 8885 * time we will find the IRE_CACHE for the gw and 8886 * create another IRE_CACHE in case IRE_CACHE above. 8887 */ 8888 if (gw != INADDR_ANY) { 8889 /* 8890 * The source ipif that was determined above was 8891 * relative to the destination address, not the 8892 * gateway's. If src_ipif was not taken out of 8893 * the IRE_IF_RESOLVER entry, we'll need to call 8894 * ipif_select_source() again. 8895 */ 8896 if (src_ipif != ire->ire_ipif) { 8897 ipif_refrele(src_ipif); 8898 src_ipif = ipif_select_source(dst_ill, 8899 gw, zoneid); 8900 if (src_ipif == NULL) { 8901 if (ip_debug > 2) { 8902 pr_addr_dbg( 8903 "ip_newroute: no " 8904 "src for gw %s ", 8905 AF_INET, &gw); 8906 printf("through " 8907 "interface %s\n", 8908 dst_ill->ill_name); 8909 } 8910 goto icmp_err_ret; 8911 } 8912 } 8913 save_dst = dst; 8914 dst = gw; 8915 gw = INADDR_ANY; 8916 } 8917 8918 /* 8919 * We obtain a partial IRE_CACHE which we will pass 8920 * along with the resolver query. When the response 8921 * comes back it will be there ready for us to add. 8922 * The ire_max_frag is atomically set under the 8923 * irebucket lock in ire_add_v[46]. 8924 */ 8925 8926 ire = ire_create_mp( 8927 (uchar_t *)&dst, /* dest address */ 8928 (uchar_t *)&ip_g_all_ones, /* mask */ 8929 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8930 (uchar_t *)&gw, /* gateway address */ 8931 NULL, /* no in_src_addr */ 8932 NULL, /* ire_max_frag */ 8933 NULL, /* Fast Path header */ 8934 dst_ill->ill_rq, /* recv-from queue */ 8935 dst_ill->ill_wq, /* send-to queue */ 8936 IRE_CACHE, 8937 NULL, 8938 src_ipif, /* Interface ipif */ 8939 in_ill, /* Incoming ILL */ 8940 save_ire->ire_mask, /* Parent mask */ 8941 0, 8942 save_ire->ire_ihandle, /* Interface handle */ 8943 0, /* flags if any */ 8944 &(save_ire->ire_uinfo), 8945 NULL, 8946 NULL, 8947 ipst); 8948 8949 if (ire == NULL) { 8950 ire_refrele(save_ire); 8951 break; 8952 } 8953 8954 if ((sire != NULL) && 8955 (sire->ire_flags & RTF_MULTIRT)) { 8956 copy_mp = copymsg(first_mp); 8957 if (copy_mp != NULL) 8958 MULTIRT_DEBUG_TAG(copy_mp); 8959 } 8960 8961 ire->ire_marks |= ire_marks; 8962 8963 /* 8964 * Construct message chain for the resolver 8965 * of the form: 8966 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8967 * Packet could contain a IPSEC_OUT mp. 8968 * 8969 * NOTE : ire will be added later when the response 8970 * comes back from ARP. If the response does not 8971 * come back, ARP frees the packet. For this reason, 8972 * we can't REFHOLD the bucket of save_ire to prevent 8973 * deletions. We may not be able to REFRELE the bucket 8974 * if the response never comes back. Thus, before 8975 * adding the ire, ire_add_v4 will make sure that the 8976 * interface route does not get deleted. This is the 8977 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 8978 * where we can always prevent deletions because of 8979 * the synchronous nature of adding IRES i.e 8980 * ire_add_then_send is called after creating the IRE. 8981 */ 8982 ASSERT(ire->ire_mp != NULL); 8983 ire->ire_mp->b_cont = first_mp; 8984 /* Have saved_mp handy, for cleanup if canput fails */ 8985 saved_mp = mp; 8986 mp = copyb(res_mp); 8987 if (mp == NULL) { 8988 /* Prepare for cleanup */ 8989 mp = saved_mp; /* pkt */ 8990 ire_delete(ire); /* ire_mp */ 8991 ire = NULL; 8992 ire_refrele(save_ire); 8993 if (copy_mp != NULL) { 8994 MULTIRT_DEBUG_UNTAG(copy_mp); 8995 freemsg(copy_mp); 8996 copy_mp = NULL; 8997 } 8998 break; 8999 } 9000 linkb(mp, ire->ire_mp); 9001 9002 /* 9003 * Fill in the source and dest addrs for the resolver. 9004 * NOTE: this depends on memory layouts imposed by 9005 * ill_init(). 9006 */ 9007 areq = (areq_t *)mp->b_rptr; 9008 addrp = (ipaddr_t *)((char *)areq + 9009 areq->areq_sender_addr_offset); 9010 if (do_attach_ill) { 9011 /* 9012 * This is bind to no failover case. 9013 * arp packet also must go out on attach_ill. 9014 */ 9015 ASSERT(ipha->ipha_src != NULL); 9016 *addrp = ipha->ipha_src; 9017 } else { 9018 *addrp = save_ire->ire_src_addr; 9019 } 9020 9021 ire_refrele(save_ire); 9022 addrp = (ipaddr_t *)((char *)areq + 9023 areq->areq_target_addr_offset); 9024 *addrp = dst; 9025 /* Up to the resolver. */ 9026 if (canputnext(dst_ill->ill_rq) && 9027 !(dst_ill->ill_arp_closing)) { 9028 putnext(dst_ill->ill_rq, mp); 9029 ire = NULL; 9030 if (copy_mp != NULL) { 9031 /* 9032 * If we found a resolver, we ignore 9033 * any trailing top priority IRE_CACHE 9034 * in the further loops. This ensures 9035 * that we do not omit any resolver. 9036 * IRE_CACHE entries, if any, will be 9037 * processed next time we enter 9038 * ip_newroute(). 9039 */ 9040 multirt_flags &= ~MULTIRT_CACHEGW; 9041 /* 9042 * Search for the next unresolved 9043 * multirt route. 9044 */ 9045 first_mp = copy_mp; 9046 copy_mp = NULL; 9047 /* Prepare the next resolution loop. */ 9048 mp = first_mp; 9049 EXTRACT_PKT_MP(mp, first_mp, 9050 mctl_present); 9051 if (mctl_present) 9052 io = (ipsec_out_t *) 9053 first_mp->b_rptr; 9054 ipha = (ipha_t *)mp->b_rptr; 9055 9056 ASSERT(sire != NULL); 9057 9058 dst = save_dst; 9059 multirt_resolve_next = B_TRUE; 9060 continue; 9061 } 9062 9063 if (sire != NULL) 9064 ire_refrele(sire); 9065 9066 /* 9067 * The response will come back in ip_wput 9068 * with db_type IRE_DB_TYPE. 9069 */ 9070 ipif_refrele(src_ipif); 9071 ill_refrele(dst_ill); 9072 return; 9073 } else { 9074 /* Prepare for cleanup */ 9075 DTRACE_PROBE1(ip__newroute__drop, mblk_t *, 9076 mp); 9077 mp->b_cont = NULL; 9078 freeb(mp); /* areq */ 9079 /* 9080 * this is an ire that is not added to the 9081 * cache. ire_freemblk will handle the release 9082 * of any resources associated with the ire. 9083 */ 9084 ire_delete(ire); /* ire_mp */ 9085 mp = saved_mp; /* pkt */ 9086 ire = NULL; 9087 if (copy_mp != NULL) { 9088 MULTIRT_DEBUG_UNTAG(copy_mp); 9089 freemsg(copy_mp); 9090 copy_mp = NULL; 9091 } 9092 break; 9093 } 9094 default: 9095 break; 9096 } 9097 } while (multirt_resolve_next); 9098 9099 ip1dbg(("ip_newroute: dropped\n")); 9100 /* Did this packet originate externally? */ 9101 if (mp->b_prev) { 9102 mp->b_next = NULL; 9103 mp->b_prev = NULL; 9104 if (in_ill != NULL) { 9105 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInDiscards); 9106 } else { 9107 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 9108 } 9109 } else { 9110 if (dst_ill != NULL) { 9111 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards); 9112 } else { 9113 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 9114 } 9115 } 9116 ASSERT(copy_mp == NULL); 9117 MULTIRT_DEBUG_UNTAG(first_mp); 9118 freemsg(first_mp); 9119 if (ire != NULL) 9120 ire_refrele(ire); 9121 if (sire != NULL) 9122 ire_refrele(sire); 9123 if (src_ipif != NULL) 9124 ipif_refrele(src_ipif); 9125 if (dst_ill != NULL) 9126 ill_refrele(dst_ill); 9127 return; 9128 9129 icmp_err_ret: 9130 ip1dbg(("ip_newroute: no route\n")); 9131 if (src_ipif != NULL) 9132 ipif_refrele(src_ipif); 9133 if (dst_ill != NULL) 9134 ill_refrele(dst_ill); 9135 if (sire != NULL) 9136 ire_refrele(sire); 9137 /* Did this packet originate externally? */ 9138 if (mp->b_prev) { 9139 mp->b_next = NULL; 9140 mp->b_prev = NULL; 9141 if (in_ill != NULL) { 9142 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInNoRoutes); 9143 } else { 9144 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes); 9145 } 9146 q = WR(q); 9147 } else { 9148 /* 9149 * There is no outgoing ill, so just increment the 9150 * system MIB. 9151 */ 9152 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 9153 /* 9154 * Since ip_wput() isn't close to finished, we fill 9155 * in enough of the header for credible error reporting. 9156 */ 9157 if (ip_hdr_complete(ipha, zoneid, ipst)) { 9158 /* Failed */ 9159 MULTIRT_DEBUG_UNTAG(first_mp); 9160 freemsg(first_mp); 9161 if (ire != NULL) 9162 ire_refrele(ire); 9163 return; 9164 } 9165 } 9166 9167 /* 9168 * At this point we will have ire only if RTF_BLACKHOLE 9169 * or RTF_REJECT flags are set on the IRE. It will not 9170 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9171 */ 9172 if (ire != NULL) { 9173 if (ire->ire_flags & RTF_BLACKHOLE) { 9174 ire_refrele(ire); 9175 MULTIRT_DEBUG_UNTAG(first_mp); 9176 freemsg(first_mp); 9177 return; 9178 } 9179 ire_refrele(ire); 9180 } 9181 if (ip_source_routed(ipha, ipst)) { 9182 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, 9183 zoneid, ipst); 9184 return; 9185 } 9186 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9187 } 9188 9189 ip_opt_info_t zero_info; 9190 9191 /* 9192 * IPv4 - 9193 * ip_newroute_ipif is called by ip_wput_multicast and 9194 * ip_rput_forward_multicast whenever we need to send 9195 * out a packet to a destination address for which we do not have specific 9196 * routing information. It is used when the packet will be sent out 9197 * on a specific interface. It is also called by ip_wput() when IP_XMIT_IF 9198 * socket option is set or icmp error message wants to go out on a particular 9199 * interface for a unicast packet. 9200 * 9201 * In most cases, the destination address is resolved thanks to the ipif 9202 * intrinsic resolver. However, there are some cases where the call to 9203 * ip_newroute_ipif must take into account the potential presence of 9204 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 9205 * that uses the interface. This is specified through flags, 9206 * which can be a combination of: 9207 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 9208 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 9209 * and flags. Additionally, the packet source address has to be set to 9210 * the specified address. The caller is thus expected to set this flag 9211 * if the packet has no specific source address yet. 9212 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 9213 * flag, the resulting ire will inherit the flag. All unresolved routes 9214 * to the destination must be explored in the same call to 9215 * ip_newroute_ipif(). 9216 */ 9217 static void 9218 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 9219 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop) 9220 { 9221 areq_t *areq; 9222 ire_t *ire = NULL; 9223 mblk_t *res_mp; 9224 ipaddr_t *addrp; 9225 mblk_t *first_mp; 9226 ire_t *save_ire = NULL; 9227 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ 9228 ipif_t *src_ipif = NULL; 9229 ushort_t ire_marks = 0; 9230 ill_t *dst_ill = NULL; 9231 boolean_t mctl_present; 9232 ipsec_out_t *io; 9233 ipha_t *ipha; 9234 int ihandle = 0; 9235 mblk_t *saved_mp; 9236 ire_t *fire = NULL; 9237 mblk_t *copy_mp = NULL; 9238 boolean_t multirt_resolve_next; 9239 ipaddr_t ipha_dst; 9240 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 9241 9242 /* 9243 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 9244 * here for uniformity 9245 */ 9246 ipif_refhold(ipif); 9247 9248 /* 9249 * This loop is run only once in most cases. 9250 * We loop to resolve further routes only when the destination 9251 * can be reached through multiple RTF_MULTIRT-flagged ires. 9252 */ 9253 do { 9254 if (dst_ill != NULL) { 9255 ill_refrele(dst_ill); 9256 dst_ill = NULL; 9257 } 9258 if (src_ipif != NULL) { 9259 ipif_refrele(src_ipif); 9260 src_ipif = NULL; 9261 } 9262 multirt_resolve_next = B_FALSE; 9263 9264 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 9265 ipif->ipif_ill->ill_name)); 9266 9267 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 9268 if (mctl_present) 9269 io = (ipsec_out_t *)first_mp->b_rptr; 9270 9271 ipha = (ipha_t *)mp->b_rptr; 9272 9273 /* 9274 * Save the packet destination address, we may need it after 9275 * the packet has been consumed. 9276 */ 9277 ipha_dst = ipha->ipha_dst; 9278 9279 /* 9280 * If the interface is a pt-pt interface we look for an 9281 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 9282 * local_address and the pt-pt destination address. Otherwise 9283 * we just match the local address. 9284 * NOTE: dst could be different than ipha->ipha_dst in case 9285 * of sending igmp multicast packets over a point-to-point 9286 * connection. 9287 * Thus we must be careful enough to check ipha_dst to be a 9288 * multicast address, otherwise it will take xmit_if path for 9289 * multicast packets resulting into kernel stack overflow by 9290 * repeated calls to ip_newroute_ipif from ire_send(). 9291 */ 9292 if (CLASSD(ipha_dst) && 9293 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 9294 goto err_ret; 9295 } 9296 9297 /* 9298 * We check if an IRE_OFFSUBNET for the addr that goes through 9299 * ipif exists. We need it to determine if the RTF_SETSRC and/or 9300 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 9301 * propagate its flags to the new ire. 9302 */ 9303 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 9304 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 9305 ip2dbg(("ip_newroute_ipif: " 9306 "ipif_lookup_multi_ire(" 9307 "ipif %p, dst %08x) = fire %p\n", 9308 (void *)ipif, ntohl(dst), (void *)fire)); 9309 } 9310 9311 if (mctl_present && io->ipsec_out_attach_if) { 9312 attach_ill = ip_grab_attach_ill(NULL, first_mp, 9313 io->ipsec_out_ill_index, B_FALSE, ipst); 9314 9315 /* Failure case frees things for us. */ 9316 if (attach_ill == NULL) { 9317 ipif_refrele(ipif); 9318 if (fire != NULL) 9319 ire_refrele(fire); 9320 return; 9321 } 9322 9323 /* 9324 * Check if we need an ire that will not be 9325 * looked up by anybody else i.e. HIDDEN. 9326 */ 9327 if (ill_is_probeonly(attach_ill)) { 9328 ire_marks = IRE_MARK_HIDDEN; 9329 } 9330 /* 9331 * ip_wput passes the right ipif for IPIF_NOFAILOVER 9332 * case. 9333 */ 9334 dst_ill = ipif->ipif_ill; 9335 /* attach_ill has been refheld by ip_grab_attach_ill */ 9336 ASSERT(dst_ill == attach_ill); 9337 } else { 9338 /* 9339 * If this is set by IP_XMIT_IF, then make sure that 9340 * ipif is pointing to the same ill as the IP_XMIT_IF 9341 * specified ill. 9342 */ 9343 ASSERT((connp == NULL) || 9344 (connp->conn_xmit_if_ill == NULL) || 9345 (connp->conn_xmit_if_ill == ipif->ipif_ill)); 9346 /* 9347 * If the interface belongs to an interface group, 9348 * make sure the next possible interface in the group 9349 * is used. This encourages load spreading among 9350 * peers in an interface group. 9351 * Note: load spreading is disabled for RTF_MULTIRT 9352 * routes. 9353 */ 9354 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9355 (fire->ire_flags & RTF_MULTIRT)) { 9356 /* 9357 * Don't perform outbound load spreading 9358 * in the case of an RTF_MULTIRT issued route, 9359 * we actually typically want to replicate 9360 * outgoing packets through particular 9361 * interfaces. 9362 */ 9363 dst_ill = ipif->ipif_ill; 9364 ill_refhold(dst_ill); 9365 } else { 9366 dst_ill = ip_newroute_get_dst_ill( 9367 ipif->ipif_ill); 9368 } 9369 if (dst_ill == NULL) { 9370 if (ip_debug > 2) { 9371 pr_addr_dbg("ip_newroute_ipif: " 9372 "no dst ill for dst %s\n", 9373 AF_INET, &dst); 9374 } 9375 goto err_ret; 9376 } 9377 } 9378 9379 /* 9380 * Pick a source address preferring non-deprecated ones. 9381 * Unlike ip_newroute, we don't do any source address 9382 * selection here since for multicast it really does not help 9383 * in inbound load spreading as in the unicast case. 9384 */ 9385 if ((flags & RTF_SETSRC) && (fire != NULL) && 9386 (fire->ire_flags & RTF_SETSRC)) { 9387 /* 9388 * As requested by flags, an IRE_OFFSUBNET was looked up 9389 * on that interface. This ire has RTF_SETSRC flag, so 9390 * the source address of the packet must be changed. 9391 * Check that the ipif matching the requested source 9392 * address still exists. 9393 */ 9394 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 9395 zoneid, NULL, NULL, NULL, NULL, ipst); 9396 } 9397 if (((ipif->ipif_flags & IPIF_DEPRECATED) || 9398 (connp != NULL && ipif->ipif_zoneid != zoneid && 9399 ipif->ipif_zoneid != ALL_ZONES)) && 9400 (src_ipif == NULL)) { 9401 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 9402 if (src_ipif == NULL) { 9403 if (ip_debug > 2) { 9404 /* ip1dbg */ 9405 pr_addr_dbg("ip_newroute_ipif: " 9406 "no src for dst %s", 9407 AF_INET, &dst); 9408 } 9409 ip1dbg((" through interface %s\n", 9410 dst_ill->ill_name)); 9411 goto err_ret; 9412 } 9413 ipif_refrele(ipif); 9414 ipif = src_ipif; 9415 ipif_refhold(ipif); 9416 } 9417 if (src_ipif == NULL) { 9418 src_ipif = ipif; 9419 ipif_refhold(src_ipif); 9420 } 9421 9422 /* 9423 * Assign a source address while we have the conn. 9424 * We can't have ip_wput_ire pick a source address when the 9425 * packet returns from arp since conn_unspec_src might be set 9426 * and we loose the conn when going through arp. 9427 */ 9428 if (ipha->ipha_src == INADDR_ANY && 9429 (connp == NULL || !connp->conn_unspec_src)) { 9430 ipha->ipha_src = src_ipif->ipif_src_addr; 9431 } 9432 9433 /* 9434 * In case of IP_XMIT_IF, it is possible that the outgoing 9435 * interface does not have an interface ire. 9436 * Example: Thousands of mobileip PPP interfaces to mobile 9437 * nodes. We don't want to create interface ires because 9438 * packets from other mobile nodes must not take the route 9439 * via interface ires to the visiting mobile node without 9440 * going through the home agent, in absence of mobileip 9441 * route optimization. 9442 */ 9443 if (CLASSD(ipha_dst) && (connp == NULL || 9444 connp->conn_xmit_if_ill == NULL) && 9445 infop->ip_opt_ill_index == 0) { 9446 /* ipif_to_ire returns an held ire */ 9447 ire = ipif_to_ire(ipif); 9448 if (ire == NULL) 9449 goto err_ret; 9450 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 9451 goto err_ret; 9452 /* 9453 * ihandle is needed when the ire is added to 9454 * cache table. 9455 */ 9456 save_ire = ire; 9457 ihandle = save_ire->ire_ihandle; 9458 9459 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 9460 "flags %04x\n", 9461 (void *)ire, (void *)ipif, flags)); 9462 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9463 (fire->ire_flags & RTF_MULTIRT)) { 9464 /* 9465 * As requested by flags, an IRE_OFFSUBNET was 9466 * looked up on that interface. This ire has 9467 * RTF_MULTIRT flag, so the resolution loop will 9468 * be re-entered to resolve additional routes on 9469 * other interfaces. For that purpose, a copy of 9470 * the packet is performed at this point. 9471 */ 9472 fire->ire_last_used_time = lbolt; 9473 copy_mp = copymsg(first_mp); 9474 if (copy_mp) { 9475 MULTIRT_DEBUG_TAG(copy_mp); 9476 } 9477 } 9478 if ((flags & RTF_SETSRC) && (fire != NULL) && 9479 (fire->ire_flags & RTF_SETSRC)) { 9480 /* 9481 * As requested by flags, an IRE_OFFSUBET was 9482 * looked up on that interface. This ire has 9483 * RTF_SETSRC flag, so the source address of the 9484 * packet must be changed. 9485 */ 9486 ipha->ipha_src = fire->ire_src_addr; 9487 } 9488 } else { 9489 ASSERT((connp == NULL) || 9490 (connp->conn_xmit_if_ill != NULL) || 9491 (connp->conn_dontroute) || 9492 infop->ip_opt_ill_index != 0); 9493 /* 9494 * The only ways we can come here are: 9495 * 1) IP_XMIT_IF socket option is set 9496 * 2) ICMP error message generated from 9497 * ip_mrtun_forward() routine and it needs 9498 * to go through the specified ill. 9499 * 3) SO_DONTROUTE socket option is set 9500 * 4) IP_PKTINFO option is passed in as ancillary data. 9501 * In all cases, the new ire will not be added 9502 * into cache table. 9503 */ 9504 ire_marks |= IRE_MARK_NOADD; 9505 } 9506 9507 switch (ipif->ipif_net_type) { 9508 case IRE_IF_NORESOLVER: { 9509 /* We have what we need to build an IRE_CACHE. */ 9510 mblk_t *res_mp; 9511 9512 /* 9513 * Create a new res_mp with the 9514 * IP gateway address as destination address in the 9515 * DLPI hdr if the physical length is exactly 4 bytes. 9516 */ 9517 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 9518 res_mp = ill_dlur_gen((uchar_t *)&dst, 9519 dst_ill->ill_phys_addr_length, 9520 dst_ill->ill_sap, 9521 dst_ill->ill_sap_length); 9522 } else if (dst_ill->ill_resolver_mp == NULL) { 9523 ip1dbg(("ip_newroute: dst_ill %p " 9524 "for IF_NORESOLV ire %p has " 9525 "no ill_resolver_mp\n", 9526 (void *)dst_ill, (void *)ire)); 9527 break; 9528 } else { 9529 /* use the value set in ip_ll_subnet_defaults */ 9530 res_mp = ill_dlur_gen(NULL, 9531 dst_ill->ill_phys_addr_length, 9532 dst_ill->ill_sap, 9533 dst_ill->ill_sap_length); 9534 } 9535 9536 if (res_mp == NULL) 9537 break; 9538 /* 9539 * The new ire inherits the IRE_OFFSUBNET flags 9540 * and source address, if this was requested. 9541 */ 9542 ire = ire_create( 9543 (uchar_t *)&dst, /* dest address */ 9544 (uchar_t *)&ip_g_all_ones, /* mask */ 9545 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9546 NULL, /* gateway address */ 9547 NULL, 9548 &ipif->ipif_mtu, 9549 NULL, /* Fast Path header */ 9550 dst_ill->ill_rq, /* recv-from queue */ 9551 dst_ill->ill_wq, /* send-to queue */ 9552 IRE_CACHE, 9553 res_mp, 9554 src_ipif, 9555 NULL, 9556 (save_ire != NULL ? save_ire->ire_mask : 0), 9557 (fire != NULL) ? /* Parent handle */ 9558 fire->ire_phandle : 0, 9559 ihandle, /* Interface handle */ 9560 (fire != NULL) ? 9561 (fire->ire_flags & 9562 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9563 (save_ire == NULL ? &ire_uinfo_null : 9564 &save_ire->ire_uinfo), 9565 NULL, 9566 NULL, 9567 ipst); 9568 9569 freeb(res_mp); 9570 9571 if (ire == NULL) { 9572 if (save_ire != NULL) 9573 ire_refrele(save_ire); 9574 break; 9575 } 9576 9577 ire->ire_marks |= ire_marks; 9578 9579 /* 9580 * If IRE_MARK_NOADD is set then we need to convert 9581 * the max_fragp to a useable value now. This is 9582 * normally done in ire_add_v[46]. We also need to 9583 * associate the ire with an nce (normally would be 9584 * done in ip_wput_nondata()). 9585 * 9586 * Note that IRE_MARK_NOADD packets created here 9587 * do not have a non-null ire_mp pointer. The null 9588 * value of ire_bucket indicates that they were 9589 * never added. 9590 */ 9591 if (ire->ire_marks & IRE_MARK_NOADD) { 9592 uint_t max_frag; 9593 9594 max_frag = *ire->ire_max_fragp; 9595 ire->ire_max_fragp = NULL; 9596 ire->ire_max_frag = max_frag; 9597 9598 if ((ire->ire_nce = ndp_lookup_v4( 9599 ire_to_ill(ire), 9600 (ire->ire_gateway_addr != INADDR_ANY ? 9601 &ire->ire_gateway_addr : &ire->ire_addr), 9602 B_FALSE)) == NULL) { 9603 if (save_ire != NULL) 9604 ire_refrele(save_ire); 9605 break; 9606 } 9607 ASSERT(ire->ire_nce->nce_state == 9608 ND_REACHABLE); 9609 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 9610 } 9611 9612 /* Prevent save_ire from getting deleted */ 9613 if (save_ire != NULL) { 9614 IRB_REFHOLD(save_ire->ire_bucket); 9615 /* Has it been removed already ? */ 9616 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 9617 IRB_REFRELE(save_ire->ire_bucket); 9618 ire_refrele(save_ire); 9619 break; 9620 } 9621 } 9622 9623 ire_add_then_send(q, ire, first_mp); 9624 9625 /* Assert that save_ire is not deleted yet. */ 9626 if (save_ire != NULL) { 9627 ASSERT(save_ire->ire_ptpn != NULL); 9628 IRB_REFRELE(save_ire->ire_bucket); 9629 ire_refrele(save_ire); 9630 save_ire = NULL; 9631 } 9632 if (fire != NULL) { 9633 ire_refrele(fire); 9634 fire = NULL; 9635 } 9636 9637 /* 9638 * the resolution loop is re-entered if this 9639 * was requested through flags and if we 9640 * actually are in a multirouting case. 9641 */ 9642 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9643 boolean_t need_resolve = 9644 ire_multirt_need_resolve(ipha_dst, 9645 MBLK_GETLABEL(copy_mp), ipst); 9646 if (!need_resolve) { 9647 MULTIRT_DEBUG_UNTAG(copy_mp); 9648 freemsg(copy_mp); 9649 copy_mp = NULL; 9650 } else { 9651 /* 9652 * ipif_lookup_group() calls 9653 * ire_lookup_multi() that uses 9654 * ire_ftable_lookup() to find 9655 * an IRE_INTERFACE for the group. 9656 * In the multirt case, 9657 * ire_lookup_multi() then invokes 9658 * ire_multirt_lookup() to find 9659 * the next resolvable ire. 9660 * As a result, we obtain an new 9661 * interface, derived from the 9662 * next ire. 9663 */ 9664 ipif_refrele(ipif); 9665 ipif = ipif_lookup_group(ipha_dst, 9666 zoneid, ipst); 9667 ip2dbg(("ip_newroute_ipif: " 9668 "multirt dst %08x, ipif %p\n", 9669 htonl(dst), (void *)ipif)); 9670 if (ipif != NULL) { 9671 mp = copy_mp; 9672 copy_mp = NULL; 9673 multirt_resolve_next = B_TRUE; 9674 continue; 9675 } else { 9676 freemsg(copy_mp); 9677 } 9678 } 9679 } 9680 if (ipif != NULL) 9681 ipif_refrele(ipif); 9682 ill_refrele(dst_ill); 9683 ipif_refrele(src_ipif); 9684 return; 9685 } 9686 case IRE_IF_RESOLVER: 9687 /* 9688 * We can't build an IRE_CACHE yet, but at least 9689 * we found a resolver that can help. 9690 */ 9691 res_mp = dst_ill->ill_resolver_mp; 9692 if (!OK_RESOLVER_MP(res_mp)) 9693 break; 9694 9695 /* 9696 * We obtain a partial IRE_CACHE which we will pass 9697 * along with the resolver query. When the response 9698 * comes back it will be there ready for us to add. 9699 * The new ire inherits the IRE_OFFSUBNET flags 9700 * and source address, if this was requested. 9701 * The ire_max_frag is atomically set under the 9702 * irebucket lock in ire_add_v[46]. Only in the 9703 * case of IRE_MARK_NOADD, we set it here itself. 9704 */ 9705 ire = ire_create_mp( 9706 (uchar_t *)&dst, /* dest address */ 9707 (uchar_t *)&ip_g_all_ones, /* mask */ 9708 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9709 NULL, /* gateway address */ 9710 NULL, /* no in_src_addr */ 9711 (ire_marks & IRE_MARK_NOADD) ? 9712 ipif->ipif_mtu : 0, /* max_frag */ 9713 NULL, /* Fast path header */ 9714 dst_ill->ill_rq, /* recv-from queue */ 9715 dst_ill->ill_wq, /* send-to queue */ 9716 IRE_CACHE, 9717 NULL, /* let ire_nce_init figure res_mp out */ 9718 src_ipif, 9719 NULL, 9720 (save_ire != NULL ? save_ire->ire_mask : 0), 9721 (fire != NULL) ? /* Parent handle */ 9722 fire->ire_phandle : 0, 9723 ihandle, /* Interface handle */ 9724 (fire != NULL) ? /* flags if any */ 9725 (fire->ire_flags & 9726 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9727 (save_ire == NULL ? &ire_uinfo_null : 9728 &save_ire->ire_uinfo), 9729 NULL, 9730 NULL, 9731 ipst); 9732 9733 if (save_ire != NULL) { 9734 ire_refrele(save_ire); 9735 save_ire = NULL; 9736 } 9737 if (ire == NULL) 9738 break; 9739 9740 ire->ire_marks |= ire_marks; 9741 /* 9742 * Construct message chain for the resolver of the 9743 * form: 9744 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 9745 * 9746 * NOTE : ire will be added later when the response 9747 * comes back from ARP. If the response does not 9748 * come back, ARP frees the packet. For this reason, 9749 * we can't REFHOLD the bucket of save_ire to prevent 9750 * deletions. We may not be able to REFRELE the 9751 * bucket if the response never comes back. 9752 * Thus, before adding the ire, ire_add_v4 will make 9753 * sure that the interface route does not get deleted. 9754 * This is the only case unlike ip_newroute_v6, 9755 * ip_newroute_ipif_v6 where we can always prevent 9756 * deletions because ire_add_then_send is called after 9757 * creating the IRE. 9758 * If IRE_MARK_NOADD is set, then ire_add_then_send 9759 * does not add this IRE into the IRE CACHE. 9760 */ 9761 ASSERT(ire->ire_mp != NULL); 9762 ire->ire_mp->b_cont = first_mp; 9763 /* Have saved_mp handy, for cleanup if canput fails */ 9764 saved_mp = mp; 9765 mp = copyb(res_mp); 9766 if (mp == NULL) { 9767 /* Prepare for cleanup */ 9768 mp = saved_mp; /* pkt */ 9769 ire_delete(ire); /* ire_mp */ 9770 ire = NULL; 9771 if (copy_mp != NULL) { 9772 MULTIRT_DEBUG_UNTAG(copy_mp); 9773 freemsg(copy_mp); 9774 copy_mp = NULL; 9775 } 9776 break; 9777 } 9778 linkb(mp, ire->ire_mp); 9779 9780 /* 9781 * Fill in the source and dest addrs for the resolver. 9782 * NOTE: this depends on memory layouts imposed by 9783 * ill_init(). 9784 */ 9785 areq = (areq_t *)mp->b_rptr; 9786 addrp = (ipaddr_t *)((char *)areq + 9787 areq->areq_sender_addr_offset); 9788 *addrp = ire->ire_src_addr; 9789 addrp = (ipaddr_t *)((char *)areq + 9790 areq->areq_target_addr_offset); 9791 *addrp = dst; 9792 /* Up to the resolver. */ 9793 if (canputnext(dst_ill->ill_rq) && 9794 !(dst_ill->ill_arp_closing)) { 9795 putnext(dst_ill->ill_rq, mp); 9796 /* 9797 * The response will come back in ip_wput 9798 * with db_type IRE_DB_TYPE. 9799 */ 9800 } else { 9801 mp->b_cont = NULL; 9802 freeb(mp); /* areq */ 9803 ire_delete(ire); /* ire_mp */ 9804 saved_mp->b_next = NULL; 9805 saved_mp->b_prev = NULL; 9806 freemsg(first_mp); /* pkt */ 9807 ip2dbg(("ip_newroute_ipif: dropped\n")); 9808 } 9809 9810 if (fire != NULL) { 9811 ire_refrele(fire); 9812 fire = NULL; 9813 } 9814 9815 9816 /* 9817 * The resolution loop is re-entered if this was 9818 * requested through flags and we actually are 9819 * in a multirouting case. 9820 */ 9821 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9822 boolean_t need_resolve = 9823 ire_multirt_need_resolve(ipha_dst, 9824 MBLK_GETLABEL(copy_mp), ipst); 9825 if (!need_resolve) { 9826 MULTIRT_DEBUG_UNTAG(copy_mp); 9827 freemsg(copy_mp); 9828 copy_mp = NULL; 9829 } else { 9830 /* 9831 * ipif_lookup_group() calls 9832 * ire_lookup_multi() that uses 9833 * ire_ftable_lookup() to find 9834 * an IRE_INTERFACE for the group. 9835 * In the multirt case, 9836 * ire_lookup_multi() then invokes 9837 * ire_multirt_lookup() to find 9838 * the next resolvable ire. 9839 * As a result, we obtain an new 9840 * interface, derived from the 9841 * next ire. 9842 */ 9843 ipif_refrele(ipif); 9844 ipif = ipif_lookup_group(ipha_dst, 9845 zoneid, ipst); 9846 if (ipif != NULL) { 9847 mp = copy_mp; 9848 copy_mp = NULL; 9849 multirt_resolve_next = B_TRUE; 9850 continue; 9851 } else { 9852 freemsg(copy_mp); 9853 } 9854 } 9855 } 9856 if (ipif != NULL) 9857 ipif_refrele(ipif); 9858 ill_refrele(dst_ill); 9859 ipif_refrele(src_ipif); 9860 return; 9861 default: 9862 break; 9863 } 9864 } while (multirt_resolve_next); 9865 9866 err_ret: 9867 ip2dbg(("ip_newroute_ipif: dropped\n")); 9868 if (fire != NULL) 9869 ire_refrele(fire); 9870 ipif_refrele(ipif); 9871 /* Did this packet originate externally? */ 9872 if (dst_ill != NULL) 9873 ill_refrele(dst_ill); 9874 if (src_ipif != NULL) 9875 ipif_refrele(src_ipif); 9876 if (mp->b_prev || mp->b_next) { 9877 mp->b_next = NULL; 9878 mp->b_prev = NULL; 9879 } else { 9880 /* 9881 * Since ip_wput() isn't close to finished, we fill 9882 * in enough of the header for credible error reporting. 9883 */ 9884 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 9885 /* Failed */ 9886 freemsg(first_mp); 9887 if (ire != NULL) 9888 ire_refrele(ire); 9889 return; 9890 } 9891 } 9892 /* 9893 * At this point we will have ire only if RTF_BLACKHOLE 9894 * or RTF_REJECT flags are set on the IRE. It will not 9895 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9896 */ 9897 if (ire != NULL) { 9898 if (ire->ire_flags & RTF_BLACKHOLE) { 9899 ire_refrele(ire); 9900 freemsg(first_mp); 9901 return; 9902 } 9903 ire_refrele(ire); 9904 } 9905 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9906 } 9907 9908 /* Name/Value Table Lookup Routine */ 9909 char * 9910 ip_nv_lookup(nv_t *nv, int value) 9911 { 9912 if (!nv) 9913 return (NULL); 9914 for (; nv->nv_name; nv++) { 9915 if (nv->nv_value == value) 9916 return (nv->nv_name); 9917 } 9918 return ("unknown"); 9919 } 9920 9921 /* 9922 * This is a module open, i.e. this is a control stream for access 9923 * to a DLPI device. We allocate an ill_t as the instance data in 9924 * this case. 9925 */ 9926 int 9927 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9928 { 9929 ill_t *ill; 9930 int err; 9931 zoneid_t zoneid; 9932 netstack_t *ns; 9933 ip_stack_t *ipst; 9934 9935 /* 9936 * Prevent unprivileged processes from pushing IP so that 9937 * they can't send raw IP. 9938 */ 9939 if (secpolicy_net_rawaccess(credp) != 0) 9940 return (EPERM); 9941 9942 ns = netstack_find_by_cred(credp); 9943 ASSERT(ns != NULL); 9944 ipst = ns->netstack_ip; 9945 ASSERT(ipst != NULL); 9946 9947 /* 9948 * For exclusive stacks we set the zoneid to zero 9949 * to make IP operate as if in the global zone. 9950 */ 9951 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9952 zoneid = GLOBAL_ZONEID; 9953 else 9954 zoneid = crgetzoneid(credp); 9955 9956 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 9957 q->q_ptr = WR(q)->q_ptr = ill; 9958 ill->ill_ipst = ipst; 9959 ill->ill_zoneid = zoneid; 9960 9961 /* 9962 * ill_init initializes the ill fields and then sends down 9963 * down a DL_INFO_REQ after calling qprocson. 9964 */ 9965 err = ill_init(q, ill); 9966 if (err != 0) { 9967 mi_free(ill); 9968 netstack_rele(ipst->ips_netstack); 9969 q->q_ptr = NULL; 9970 WR(q)->q_ptr = NULL; 9971 return (err); 9972 } 9973 9974 /* ill_init initializes the ipsq marking this thread as writer */ 9975 ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE, B_TRUE); 9976 /* Wait for the DL_INFO_ACK */ 9977 mutex_enter(&ill->ill_lock); 9978 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 9979 /* 9980 * Return value of 0 indicates a pending signal. 9981 */ 9982 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 9983 if (err == 0) { 9984 mutex_exit(&ill->ill_lock); 9985 (void) ip_close(q, 0); 9986 return (EINTR); 9987 } 9988 } 9989 mutex_exit(&ill->ill_lock); 9990 9991 /* 9992 * ip_rput_other could have set an error in ill_error on 9993 * receipt of M_ERROR. 9994 */ 9995 9996 err = ill->ill_error; 9997 if (err != 0) { 9998 (void) ip_close(q, 0); 9999 return (err); 10000 } 10001 10002 ill->ill_credp = credp; 10003 crhold(credp); 10004 10005 mutex_enter(&ipst->ips_ip_mi_lock); 10006 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag, 10007 credp); 10008 mutex_exit(&ipst->ips_ip_mi_lock); 10009 if (err) { 10010 (void) ip_close(q, 0); 10011 return (err); 10012 } 10013 return (0); 10014 } 10015 10016 /* IP open routine. */ 10017 int 10018 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 10019 { 10020 conn_t *connp; 10021 major_t maj; 10022 zoneid_t zoneid; 10023 netstack_t *ns; 10024 ip_stack_t *ipst; 10025 10026 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 10027 10028 /* Allow reopen. */ 10029 if (q->q_ptr != NULL) 10030 return (0); 10031 10032 if (sflag & MODOPEN) { 10033 /* This is a module open */ 10034 return (ip_modopen(q, devp, flag, sflag, credp)); 10035 } 10036 10037 ns = netstack_find_by_cred(credp); 10038 ASSERT(ns != NULL); 10039 ipst = ns->netstack_ip; 10040 ASSERT(ipst != NULL); 10041 10042 /* 10043 * For exclusive stacks we set the zoneid to zero 10044 * to make IP operate as if in the global zone. 10045 */ 10046 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 10047 zoneid = GLOBAL_ZONEID; 10048 else 10049 zoneid = crgetzoneid(credp); 10050 10051 /* 10052 * We are opening as a device. This is an IP client stream, and we 10053 * allocate an conn_t as the instance data. 10054 */ 10055 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack); 10056 10057 /* 10058 * ipcl_conn_create did a netstack_hold. Undo the hold that was 10059 * done by netstack_find_by_cred() 10060 */ 10061 netstack_rele(ipst->ips_netstack); 10062 10063 connp->conn_zoneid = zoneid; 10064 10065 connp->conn_upq = q; 10066 q->q_ptr = WR(q)->q_ptr = connp; 10067 10068 if (flag & SO_SOCKSTR) 10069 connp->conn_flags |= IPCL_SOCKET; 10070 10071 /* Minor tells us which /dev entry was opened */ 10072 if (geteminor(*devp) == IPV6_MINOR) { 10073 connp->conn_flags |= IPCL_ISV6; 10074 connp->conn_af_isv6 = B_TRUE; 10075 ip_setqinfo(q, geteminor(*devp), B_FALSE, ipst); 10076 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 10077 } else { 10078 connp->conn_af_isv6 = B_FALSE; 10079 connp->conn_pkt_isv6 = B_FALSE; 10080 } 10081 10082 if ((connp->conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 10083 /* CONN_DEC_REF takes care of netstack_rele() */ 10084 q->q_ptr = WR(q)->q_ptr = NULL; 10085 CONN_DEC_REF(connp); 10086 return (EBUSY); 10087 } 10088 10089 maj = getemajor(*devp); 10090 *devp = makedevice(maj, (minor_t)connp->conn_dev); 10091 10092 /* 10093 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 10094 */ 10095 connp->conn_cred = credp; 10096 crhold(connp->conn_cred); 10097 10098 /* 10099 * If the caller has the process-wide flag set, then default to MAC 10100 * exempt mode. This allows read-down to unlabeled hosts. 10101 */ 10102 if (getpflags(NET_MAC_AWARE, credp) != 0) 10103 connp->conn_mac_exempt = B_TRUE; 10104 10105 /* 10106 * This should only happen for ndd, netstat, raw socket or other SCTP 10107 * administrative ops. In these cases, we just need a normal conn_t 10108 * with ulp set to IPPROTO_SCTP. All other ops are trapped and 10109 * an error will be returned. 10110 */ 10111 if (maj != SCTP_MAJ && maj != SCTP6_MAJ) { 10112 connp->conn_rq = q; 10113 connp->conn_wq = WR(q); 10114 } else { 10115 connp->conn_ulp = IPPROTO_SCTP; 10116 connp->conn_rq = connp->conn_wq = NULL; 10117 } 10118 /* Non-zero default values */ 10119 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 10120 10121 /* 10122 * Make the conn globally visible to walkers 10123 */ 10124 mutex_enter(&connp->conn_lock); 10125 connp->conn_state_flags &= ~CONN_INCIPIENT; 10126 mutex_exit(&connp->conn_lock); 10127 ASSERT(connp->conn_ref == 1); 10128 10129 qprocson(q); 10130 10131 return (0); 10132 } 10133 10134 /* 10135 * Change q_qinfo based on the value of isv6. 10136 * This can not called on an ill queue. 10137 * Note that there is no race since either q_qinfo works for conn queues - it 10138 * is just an optimization to enter the best wput routine directly. 10139 */ 10140 void 10141 ip_setqinfo(queue_t *q, minor_t minor, boolean_t bump_mib, ip_stack_t *ipst) 10142 { 10143 ASSERT(q->q_flag & QREADR); 10144 ASSERT(WR(q)->q_next == NULL); 10145 ASSERT(q->q_ptr != NULL); 10146 10147 if (minor == IPV6_MINOR) { 10148 if (bump_mib) { 10149 BUMP_MIB(&ipst->ips_ip6_mib, 10150 ipIfStatsOutSwitchIPVersion); 10151 } 10152 q->q_qinfo = &rinit_ipv6; 10153 WR(q)->q_qinfo = &winit_ipv6; 10154 (Q_TO_CONN(q))->conn_pkt_isv6 = B_TRUE; 10155 } else { 10156 if (bump_mib) { 10157 BUMP_MIB(&ipst->ips_ip_mib, 10158 ipIfStatsOutSwitchIPVersion); 10159 } 10160 q->q_qinfo = &iprinit; 10161 WR(q)->q_qinfo = &ipwinit; 10162 (Q_TO_CONN(q))->conn_pkt_isv6 = B_FALSE; 10163 } 10164 10165 } 10166 10167 /* 10168 * See if IPsec needs loading because of the options in mp. 10169 */ 10170 static boolean_t 10171 ipsec_opt_present(mblk_t *mp) 10172 { 10173 uint8_t *optcp, *next_optcp, *opt_endcp; 10174 struct opthdr *opt; 10175 struct T_opthdr *topt; 10176 int opthdr_len; 10177 t_uscalar_t optname, optlevel; 10178 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 10179 ipsec_req_t *ipsr; 10180 10181 /* 10182 * Walk through the mess, and find IP_SEC_OPT. If it's there, 10183 * return TRUE. 10184 */ 10185 10186 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 10187 opt_endcp = optcp + tor->OPT_length; 10188 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10189 opthdr_len = sizeof (struct T_opthdr); 10190 } else { /* O_OPTMGMT_REQ */ 10191 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 10192 opthdr_len = sizeof (struct opthdr); 10193 } 10194 for (; optcp < opt_endcp; optcp = next_optcp) { 10195 if (optcp + opthdr_len > opt_endcp) 10196 return (B_FALSE); /* Not enough option header. */ 10197 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10198 topt = (struct T_opthdr *)optcp; 10199 optlevel = topt->level; 10200 optname = topt->name; 10201 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 10202 } else { 10203 opt = (struct opthdr *)optcp; 10204 optlevel = opt->level; 10205 optname = opt->name; 10206 next_optcp = optcp + opthdr_len + 10207 _TPI_ALIGN_OPT(opt->len); 10208 } 10209 if ((next_optcp < optcp) || /* wraparound pointer space */ 10210 ((next_optcp >= opt_endcp) && /* last option bad len */ 10211 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 10212 return (B_FALSE); /* bad option buffer */ 10213 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 10214 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 10215 /* 10216 * Check to see if it's an all-bypass or all-zeroes 10217 * IPsec request. Don't bother loading IPsec if 10218 * the socket doesn't want to use it. (A good example 10219 * is a bypass request.) 10220 * 10221 * Basically, if any of the non-NEVER bits are set, 10222 * load IPsec. 10223 */ 10224 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 10225 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 10226 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 10227 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 10228 != 0) 10229 return (B_TRUE); 10230 } 10231 } 10232 return (B_FALSE); 10233 } 10234 10235 /* 10236 * If conn is is waiting for ipsec to finish loading, kick it. 10237 */ 10238 /* ARGSUSED */ 10239 static void 10240 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 10241 { 10242 t_scalar_t optreq_prim; 10243 mblk_t *mp; 10244 cred_t *cr; 10245 int err = 0; 10246 10247 /* 10248 * This function is called, after ipsec loading is complete. 10249 * Since IP checks exclusively and atomically (i.e it prevents 10250 * ipsec load from completing until ip_optcom_req completes) 10251 * whether ipsec load is complete, there cannot be a race with IP 10252 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 10253 */ 10254 mutex_enter(&connp->conn_lock); 10255 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 10256 ASSERT(connp->conn_ipsec_opt_mp != NULL); 10257 mp = connp->conn_ipsec_opt_mp; 10258 connp->conn_ipsec_opt_mp = NULL; 10259 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 10260 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); 10261 mutex_exit(&connp->conn_lock); 10262 10263 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 10264 10265 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 10266 if (optreq_prim == T_OPTMGMT_REQ) { 10267 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10268 &ip_opt_obj); 10269 } else { 10270 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 10271 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10272 &ip_opt_obj); 10273 } 10274 if (err != EINPROGRESS) 10275 CONN_OPER_PENDING_DONE(connp); 10276 return; 10277 } 10278 mutex_exit(&connp->conn_lock); 10279 } 10280 10281 /* 10282 * Called from the ipsec_loader thread, outside any perimeter, to tell 10283 * ip qenable any of the queues waiting for the ipsec loader to 10284 * complete. 10285 */ 10286 void 10287 ip_ipsec_load_complete(ipsec_stack_t *ipss) 10288 { 10289 netstack_t *ns = ipss->ipsec_netstack; 10290 10291 ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip); 10292 } 10293 10294 /* 10295 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 10296 * determines the grp on which it has to become exclusive, queues the mp 10297 * and sq draining restarts the optmgmt 10298 */ 10299 static boolean_t 10300 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 10301 { 10302 conn_t *connp = Q_TO_CONN(q); 10303 ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec; 10304 10305 /* 10306 * Take IPsec requests and treat them special. 10307 */ 10308 if (ipsec_opt_present(mp)) { 10309 /* First check if IPsec is loaded. */ 10310 mutex_enter(&ipss->ipsec_loader_lock); 10311 if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) { 10312 mutex_exit(&ipss->ipsec_loader_lock); 10313 return (B_FALSE); 10314 } 10315 mutex_enter(&connp->conn_lock); 10316 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 10317 10318 ASSERT(connp->conn_ipsec_opt_mp == NULL); 10319 connp->conn_ipsec_opt_mp = mp; 10320 mutex_exit(&connp->conn_lock); 10321 mutex_exit(&ipss->ipsec_loader_lock); 10322 10323 ipsec_loader_loadnow(ipss); 10324 return (B_TRUE); 10325 } 10326 return (B_FALSE); 10327 } 10328 10329 /* 10330 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 10331 * all of them are copied to the conn_t. If the req is "zero", the policy is 10332 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 10333 * fields. 10334 * We keep only the latest setting of the policy and thus policy setting 10335 * is not incremental/cumulative. 10336 * 10337 * Requests to set policies with multiple alternative actions will 10338 * go through a different API. 10339 */ 10340 int 10341 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 10342 { 10343 uint_t ah_req = 0; 10344 uint_t esp_req = 0; 10345 uint_t se_req = 0; 10346 ipsec_selkey_t sel; 10347 ipsec_act_t *actp = NULL; 10348 uint_t nact; 10349 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 10350 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 10351 ipsec_policy_root_t *pr; 10352 ipsec_policy_head_t *ph; 10353 int fam; 10354 boolean_t is_pol_reset; 10355 int error = 0; 10356 netstack_t *ns = connp->conn_netstack; 10357 ip_stack_t *ipst = ns->netstack_ip; 10358 ipsec_stack_t *ipss = ns->netstack_ipsec; 10359 10360 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 10361 10362 /* 10363 * The IP_SEC_OPT option does not allow variable length parameters, 10364 * hence a request cannot be NULL. 10365 */ 10366 if (req == NULL) 10367 return (EINVAL); 10368 10369 ah_req = req->ipsr_ah_req; 10370 esp_req = req->ipsr_esp_req; 10371 se_req = req->ipsr_self_encap_req; 10372 10373 /* 10374 * Are we dealing with a request to reset the policy (i.e. 10375 * zero requests). 10376 */ 10377 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 10378 (esp_req & REQ_MASK) == 0 && 10379 (se_req & REQ_MASK) == 0); 10380 10381 if (!is_pol_reset) { 10382 /* 10383 * If we couldn't load IPsec, fail with "protocol 10384 * not supported". 10385 * IPsec may not have been loaded for a request with zero 10386 * policies, so we don't fail in this case. 10387 */ 10388 mutex_enter(&ipss->ipsec_loader_lock); 10389 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 10390 mutex_exit(&ipss->ipsec_loader_lock); 10391 return (EPROTONOSUPPORT); 10392 } 10393 mutex_exit(&ipss->ipsec_loader_lock); 10394 10395 /* 10396 * Test for valid requests. Invalid algorithms 10397 * need to be tested by IPSEC code because new 10398 * algorithms can be added dynamically. 10399 */ 10400 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10401 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10402 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 10403 return (EINVAL); 10404 } 10405 10406 /* 10407 * Only privileged users can issue these 10408 * requests. 10409 */ 10410 if (((ah_req & IPSEC_PREF_NEVER) || 10411 (esp_req & IPSEC_PREF_NEVER) || 10412 (se_req & IPSEC_PREF_NEVER)) && 10413 secpolicy_ip_config(cr, B_FALSE) != 0) { 10414 return (EPERM); 10415 } 10416 10417 /* 10418 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 10419 * are mutually exclusive. 10420 */ 10421 if (((ah_req & REQ_MASK) == REQ_MASK) || 10422 ((esp_req & REQ_MASK) == REQ_MASK) || 10423 ((se_req & REQ_MASK) == REQ_MASK)) { 10424 /* Both of them are set */ 10425 return (EINVAL); 10426 } 10427 } 10428 10429 mutex_enter(&connp->conn_lock); 10430 10431 /* 10432 * If we have already cached policies in ip_bind_connected*(), don't 10433 * let them change now. We cache policies for connections 10434 * whose src,dst [addr, port] is known. 10435 */ 10436 if (connp->conn_policy_cached) { 10437 mutex_exit(&connp->conn_lock); 10438 return (EINVAL); 10439 } 10440 10441 /* 10442 * We have a zero policies, reset the connection policy if already 10443 * set. This will cause the connection to inherit the 10444 * global policy, if any. 10445 */ 10446 if (is_pol_reset) { 10447 if (connp->conn_policy != NULL) { 10448 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack); 10449 connp->conn_policy = NULL; 10450 } 10451 connp->conn_flags &= ~IPCL_CHECK_POLICY; 10452 connp->conn_in_enforce_policy = B_FALSE; 10453 connp->conn_out_enforce_policy = B_FALSE; 10454 mutex_exit(&connp->conn_lock); 10455 return (0); 10456 } 10457 10458 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy, 10459 ipst->ips_netstack); 10460 if (ph == NULL) 10461 goto enomem; 10462 10463 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack); 10464 if (actp == NULL) 10465 goto enomem; 10466 10467 /* 10468 * Always allocate IPv4 policy entries, since they can also 10469 * apply to ipv6 sockets being used in ipv4-compat mode. 10470 */ 10471 bzero(&sel, sizeof (sel)); 10472 sel.ipsl_valid = IPSL_IPV4; 10473 10474 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10475 ipst->ips_netstack); 10476 if (pin4 == NULL) 10477 goto enomem; 10478 10479 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10480 ipst->ips_netstack); 10481 if (pout4 == NULL) 10482 goto enomem; 10483 10484 if (connp->conn_pkt_isv6) { 10485 /* 10486 * We're looking at a v6 socket, also allocate the 10487 * v6-specific entries... 10488 */ 10489 sel.ipsl_valid = IPSL_IPV6; 10490 pin6 = ipsec_policy_create(&sel, actp, nact, 10491 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10492 if (pin6 == NULL) 10493 goto enomem; 10494 10495 pout6 = ipsec_policy_create(&sel, actp, nact, 10496 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10497 if (pout6 == NULL) 10498 goto enomem; 10499 10500 /* 10501 * .. and file them away in the right place. 10502 */ 10503 fam = IPSEC_AF_V6; 10504 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10505 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 10506 ipsec_insert_always(&ph->iph_rulebyid, pin6); 10507 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10508 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 10509 ipsec_insert_always(&ph->iph_rulebyid, pout6); 10510 } 10511 10512 ipsec_actvec_free(actp, nact); 10513 10514 /* 10515 * File the v4 policies. 10516 */ 10517 fam = IPSEC_AF_V4; 10518 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10519 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 10520 ipsec_insert_always(&ph->iph_rulebyid, pin4); 10521 10522 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10523 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 10524 ipsec_insert_always(&ph->iph_rulebyid, pout4); 10525 10526 /* 10527 * If the requests need security, set enforce_policy. 10528 * If the requests are IPSEC_PREF_NEVER, one should 10529 * still set conn_out_enforce_policy so that an ipsec_out 10530 * gets attached in ip_wput. This is needed so that 10531 * for connections that we don't cache policy in ip_bind, 10532 * if global policy matches in ip_wput_attach_policy, we 10533 * don't wrongly inherit global policy. Similarly, we need 10534 * to set conn_in_enforce_policy also so that we don't verify 10535 * policy wrongly. 10536 */ 10537 if ((ah_req & REQ_MASK) != 0 || 10538 (esp_req & REQ_MASK) != 0 || 10539 (se_req & REQ_MASK) != 0) { 10540 connp->conn_in_enforce_policy = B_TRUE; 10541 connp->conn_out_enforce_policy = B_TRUE; 10542 connp->conn_flags |= IPCL_CHECK_POLICY; 10543 } 10544 10545 mutex_exit(&connp->conn_lock); 10546 return (error); 10547 #undef REQ_MASK 10548 10549 /* 10550 * Common memory-allocation-failure exit path. 10551 */ 10552 enomem: 10553 mutex_exit(&connp->conn_lock); 10554 if (actp != NULL) 10555 ipsec_actvec_free(actp, nact); 10556 if (pin4 != NULL) 10557 IPPOL_REFRELE(pin4, ipst->ips_netstack); 10558 if (pout4 != NULL) 10559 IPPOL_REFRELE(pout4, ipst->ips_netstack); 10560 if (pin6 != NULL) 10561 IPPOL_REFRELE(pin6, ipst->ips_netstack); 10562 if (pout6 != NULL) 10563 IPPOL_REFRELE(pout6, ipst->ips_netstack); 10564 return (ENOMEM); 10565 } 10566 10567 /* 10568 * Only for options that pass in an IP addr. Currently only V4 options 10569 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 10570 * So this function assumes level is IPPROTO_IP 10571 */ 10572 int 10573 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 10574 mblk_t *first_mp) 10575 { 10576 ipif_t *ipif = NULL; 10577 int error; 10578 ill_t *ill; 10579 int zoneid; 10580 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10581 10582 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 10583 10584 if (addr != INADDR_ANY || checkonly) { 10585 ASSERT(connp != NULL); 10586 zoneid = IPCL_ZONEID(connp); 10587 if (option == IP_NEXTHOP) { 10588 ipif = ipif_lookup_onlink_addr(addr, 10589 connp->conn_zoneid, ipst); 10590 } else { 10591 ipif = ipif_lookup_addr(addr, NULL, zoneid, 10592 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 10593 &error, ipst); 10594 } 10595 if (ipif == NULL) { 10596 if (error == EINPROGRESS) 10597 return (error); 10598 else if ((option == IP_MULTICAST_IF) || 10599 (option == IP_NEXTHOP)) 10600 return (EHOSTUNREACH); 10601 else 10602 return (EINVAL); 10603 } else if (checkonly) { 10604 if (option == IP_MULTICAST_IF) { 10605 ill = ipif->ipif_ill; 10606 /* not supported by the virtual network iface */ 10607 if (IS_VNI(ill)) { 10608 ipif_refrele(ipif); 10609 return (EINVAL); 10610 } 10611 } 10612 ipif_refrele(ipif); 10613 return (0); 10614 } 10615 ill = ipif->ipif_ill; 10616 mutex_enter(&connp->conn_lock); 10617 mutex_enter(&ill->ill_lock); 10618 if ((ill->ill_state_flags & ILL_CONDEMNED) || 10619 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 10620 mutex_exit(&ill->ill_lock); 10621 mutex_exit(&connp->conn_lock); 10622 ipif_refrele(ipif); 10623 return (option == IP_MULTICAST_IF ? 10624 EHOSTUNREACH : EINVAL); 10625 } 10626 } else { 10627 mutex_enter(&connp->conn_lock); 10628 } 10629 10630 /* None of the options below are supported on the VNI */ 10631 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 10632 mutex_exit(&ill->ill_lock); 10633 mutex_exit(&connp->conn_lock); 10634 ipif_refrele(ipif); 10635 return (EINVAL); 10636 } 10637 10638 switch (option) { 10639 case IP_DONTFAILOVER_IF: 10640 /* 10641 * This option is used by in.mpathd to ensure 10642 * that IPMP probe packets only go out on the 10643 * test interfaces. in.mpathd sets this option 10644 * on the non-failover interfaces. 10645 * For backward compatibility, this option 10646 * implicitly sets IP_MULTICAST_IF, as used 10647 * be done in bind(), so that ip_wput gets 10648 * this ipif to send mcast packets. 10649 */ 10650 if (ipif != NULL) { 10651 ASSERT(addr != INADDR_ANY); 10652 connp->conn_nofailover_ill = ipif->ipif_ill; 10653 connp->conn_multicast_ipif = ipif; 10654 } else { 10655 ASSERT(addr == INADDR_ANY); 10656 connp->conn_nofailover_ill = NULL; 10657 connp->conn_multicast_ipif = NULL; 10658 } 10659 break; 10660 10661 case IP_MULTICAST_IF: 10662 connp->conn_multicast_ipif = ipif; 10663 break; 10664 case IP_NEXTHOP: 10665 connp->conn_nexthop_v4 = addr; 10666 connp->conn_nexthop_set = B_TRUE; 10667 break; 10668 } 10669 10670 if (ipif != NULL) { 10671 mutex_exit(&ill->ill_lock); 10672 mutex_exit(&connp->conn_lock); 10673 ipif_refrele(ipif); 10674 return (0); 10675 } 10676 mutex_exit(&connp->conn_lock); 10677 /* We succeded in cleared the option */ 10678 return (0); 10679 } 10680 10681 /* 10682 * For options that pass in an ifindex specifying the ill. V6 options always 10683 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 10684 */ 10685 int 10686 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 10687 int level, int option, mblk_t *first_mp) 10688 { 10689 ill_t *ill = NULL; 10690 int error = 0; 10691 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10692 10693 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 10694 if (ifindex != 0) { 10695 ASSERT(connp != NULL); 10696 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 10697 first_mp, ip_restart_optmgmt, &error, ipst); 10698 if (ill != NULL) { 10699 if (checkonly) { 10700 /* not supported by the virtual network iface */ 10701 if (IS_VNI(ill)) { 10702 ill_refrele(ill); 10703 return (EINVAL); 10704 } 10705 ill_refrele(ill); 10706 return (0); 10707 } 10708 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 10709 0, NULL)) { 10710 ill_refrele(ill); 10711 ill = NULL; 10712 mutex_enter(&connp->conn_lock); 10713 goto setit; 10714 } 10715 mutex_enter(&connp->conn_lock); 10716 mutex_enter(&ill->ill_lock); 10717 if (ill->ill_state_flags & ILL_CONDEMNED) { 10718 mutex_exit(&ill->ill_lock); 10719 mutex_exit(&connp->conn_lock); 10720 ill_refrele(ill); 10721 ill = NULL; 10722 mutex_enter(&connp->conn_lock); 10723 } 10724 goto setit; 10725 } else if (error == EINPROGRESS) { 10726 return (error); 10727 } else { 10728 error = 0; 10729 } 10730 } 10731 mutex_enter(&connp->conn_lock); 10732 setit: 10733 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 10734 10735 /* 10736 * The options below assume that the ILL (if any) transmits and/or 10737 * receives traffic. Neither of which is true for the virtual network 10738 * interface, so fail setting these on a VNI. 10739 */ 10740 if (IS_VNI(ill)) { 10741 ASSERT(ill != NULL); 10742 mutex_exit(&ill->ill_lock); 10743 mutex_exit(&connp->conn_lock); 10744 ill_refrele(ill); 10745 return (EINVAL); 10746 } 10747 10748 if (level == IPPROTO_IP) { 10749 switch (option) { 10750 case IP_BOUND_IF: 10751 connp->conn_incoming_ill = ill; 10752 connp->conn_outgoing_ill = ill; 10753 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10754 0 : ifindex; 10755 break; 10756 10757 case IP_XMIT_IF: 10758 /* 10759 * Similar to IP_BOUND_IF, but this only 10760 * determines the outgoing interface for 10761 * unicast packets. Also no IRE_CACHE entry 10762 * is added for the destination of the 10763 * outgoing packets. This feature is needed 10764 * for mobile IP. 10765 */ 10766 connp->conn_xmit_if_ill = ill; 10767 connp->conn_orig_xmit_ifindex = (ill == NULL) ? 10768 0 : ifindex; 10769 break; 10770 10771 case IP_MULTICAST_IF: 10772 /* 10773 * This option is an internal special. The socket 10774 * level IP_MULTICAST_IF specifies an 'ipaddr' and 10775 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 10776 * specifies an ifindex and we try first on V6 ill's. 10777 * If we don't find one, we they try using on v4 ill's 10778 * intenally and we come here. 10779 */ 10780 if (!checkonly && ill != NULL) { 10781 ipif_t *ipif; 10782 ipif = ill->ill_ipif; 10783 10784 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 10785 mutex_exit(&ill->ill_lock); 10786 mutex_exit(&connp->conn_lock); 10787 ill_refrele(ill); 10788 ill = NULL; 10789 mutex_enter(&connp->conn_lock); 10790 } else { 10791 connp->conn_multicast_ipif = ipif; 10792 } 10793 } 10794 break; 10795 } 10796 } else { 10797 switch (option) { 10798 case IPV6_BOUND_IF: 10799 connp->conn_incoming_ill = ill; 10800 connp->conn_outgoing_ill = ill; 10801 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10802 0 : ifindex; 10803 break; 10804 10805 case IPV6_BOUND_PIF: 10806 /* 10807 * Limit all transmit to this ill. 10808 * Unlike IPV6_BOUND_IF, using this option 10809 * prevents load spreading and failover from 10810 * happening when the interface is part of the 10811 * group. That's why we don't need to remember 10812 * the ifindex in orig_bound_ifindex as in 10813 * IPV6_BOUND_IF. 10814 */ 10815 connp->conn_outgoing_pill = ill; 10816 break; 10817 10818 case IPV6_DONTFAILOVER_IF: 10819 /* 10820 * This option is used by in.mpathd to ensure 10821 * that IPMP probe packets only go out on the 10822 * test interfaces. in.mpathd sets this option 10823 * on the non-failover interfaces. 10824 */ 10825 connp->conn_nofailover_ill = ill; 10826 /* 10827 * For backward compatibility, this option 10828 * implicitly sets ip_multicast_ill as used in 10829 * IP_MULTICAST_IF so that ip_wput gets 10830 * this ipif to send mcast packets. 10831 */ 10832 connp->conn_multicast_ill = ill; 10833 connp->conn_orig_multicast_ifindex = (ill == NULL) ? 10834 0 : ifindex; 10835 break; 10836 10837 case IPV6_MULTICAST_IF: 10838 /* 10839 * Set conn_multicast_ill to be the IPv6 ill. 10840 * Set conn_multicast_ipif to be an IPv4 ipif 10841 * for ifindex to make IPv4 mapped addresses 10842 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 10843 * Even if no IPv6 ill exists for the ifindex 10844 * we need to check for an IPv4 ifindex in order 10845 * for this to work with mapped addresses. In that 10846 * case only set conn_multicast_ipif. 10847 */ 10848 if (!checkonly) { 10849 if (ifindex == 0) { 10850 connp->conn_multicast_ill = NULL; 10851 connp->conn_orig_multicast_ifindex = 0; 10852 connp->conn_multicast_ipif = NULL; 10853 } else if (ill != NULL) { 10854 connp->conn_multicast_ill = ill; 10855 connp->conn_orig_multicast_ifindex = 10856 ifindex; 10857 } 10858 } 10859 break; 10860 } 10861 } 10862 10863 if (ill != NULL) { 10864 mutex_exit(&ill->ill_lock); 10865 mutex_exit(&connp->conn_lock); 10866 ill_refrele(ill); 10867 return (0); 10868 } 10869 mutex_exit(&connp->conn_lock); 10870 /* 10871 * We succeeded in clearing the option (ifindex == 0) or failed to 10872 * locate the ill and could not set the option (ifindex != 0) 10873 */ 10874 return (ifindex == 0 ? 0 : EINVAL); 10875 } 10876 10877 /* This routine sets socket options. */ 10878 /* ARGSUSED */ 10879 int 10880 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10881 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10882 void *dummy, cred_t *cr, mblk_t *first_mp) 10883 { 10884 int *i1 = (int *)invalp; 10885 conn_t *connp = Q_TO_CONN(q); 10886 int error = 0; 10887 boolean_t checkonly; 10888 ire_t *ire; 10889 boolean_t found; 10890 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10891 10892 switch (optset_context) { 10893 10894 case SETFN_OPTCOM_CHECKONLY: 10895 checkonly = B_TRUE; 10896 /* 10897 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10898 * inlen != 0 implies value supplied and 10899 * we have to "pretend" to set it. 10900 * inlen == 0 implies that there is no 10901 * value part in T_CHECK request and just validation 10902 * done elsewhere should be enough, we just return here. 10903 */ 10904 if (inlen == 0) { 10905 *outlenp = 0; 10906 return (0); 10907 } 10908 break; 10909 case SETFN_OPTCOM_NEGOTIATE: 10910 case SETFN_UD_NEGOTIATE: 10911 case SETFN_CONN_NEGOTIATE: 10912 checkonly = B_FALSE; 10913 break; 10914 default: 10915 /* 10916 * We should never get here 10917 */ 10918 *outlenp = 0; 10919 return (EINVAL); 10920 } 10921 10922 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10923 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10924 10925 /* 10926 * For fixed length options, no sanity check 10927 * of passed in length is done. It is assumed *_optcom_req() 10928 * routines do the right thing. 10929 */ 10930 10931 switch (level) { 10932 case SOL_SOCKET: 10933 /* 10934 * conn_lock protects the bitfields, and is used to 10935 * set the fields atomically. 10936 */ 10937 switch (name) { 10938 case SO_BROADCAST: 10939 if (!checkonly) { 10940 /* TODO: use value someplace? */ 10941 mutex_enter(&connp->conn_lock); 10942 connp->conn_broadcast = *i1 ? 1 : 0; 10943 mutex_exit(&connp->conn_lock); 10944 } 10945 break; /* goto sizeof (int) option return */ 10946 case SO_USELOOPBACK: 10947 if (!checkonly) { 10948 /* TODO: use value someplace? */ 10949 mutex_enter(&connp->conn_lock); 10950 connp->conn_loopback = *i1 ? 1 : 0; 10951 mutex_exit(&connp->conn_lock); 10952 } 10953 break; /* goto sizeof (int) option return */ 10954 case SO_DONTROUTE: 10955 if (!checkonly) { 10956 mutex_enter(&connp->conn_lock); 10957 connp->conn_dontroute = *i1 ? 1 : 0; 10958 mutex_exit(&connp->conn_lock); 10959 } 10960 break; /* goto sizeof (int) option return */ 10961 case SO_REUSEADDR: 10962 if (!checkonly) { 10963 mutex_enter(&connp->conn_lock); 10964 connp->conn_reuseaddr = *i1 ? 1 : 0; 10965 mutex_exit(&connp->conn_lock); 10966 } 10967 break; /* goto sizeof (int) option return */ 10968 case SO_PROTOTYPE: 10969 if (!checkonly) { 10970 mutex_enter(&connp->conn_lock); 10971 connp->conn_proto = *i1; 10972 mutex_exit(&connp->conn_lock); 10973 } 10974 break; /* goto sizeof (int) option return */ 10975 case SO_ALLZONES: 10976 if (!checkonly) { 10977 mutex_enter(&connp->conn_lock); 10978 if (IPCL_IS_BOUND(connp)) { 10979 mutex_exit(&connp->conn_lock); 10980 return (EINVAL); 10981 } 10982 connp->conn_allzones = *i1 != 0 ? 1 : 0; 10983 mutex_exit(&connp->conn_lock); 10984 } 10985 break; /* goto sizeof (int) option return */ 10986 case SO_ANON_MLP: 10987 if (!checkonly) { 10988 mutex_enter(&connp->conn_lock); 10989 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; 10990 mutex_exit(&connp->conn_lock); 10991 } 10992 break; /* goto sizeof (int) option return */ 10993 case SO_MAC_EXEMPT: 10994 if (secpolicy_net_mac_aware(cr) != 0 || 10995 IPCL_IS_BOUND(connp)) 10996 return (EACCES); 10997 if (!checkonly) { 10998 mutex_enter(&connp->conn_lock); 10999 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; 11000 mutex_exit(&connp->conn_lock); 11001 } 11002 break; /* goto sizeof (int) option return */ 11003 default: 11004 /* 11005 * "soft" error (negative) 11006 * option not handled at this level 11007 * Note: Do not modify *outlenp 11008 */ 11009 return (-EINVAL); 11010 } 11011 break; 11012 case IPPROTO_IP: 11013 switch (name) { 11014 case IP_NEXTHOP: 11015 if (secpolicy_ip_config(cr, B_FALSE) != 0) 11016 return (EPERM); 11017 /* FALLTHRU */ 11018 case IP_MULTICAST_IF: 11019 case IP_DONTFAILOVER_IF: { 11020 ipaddr_t addr = *i1; 11021 11022 error = ip_opt_set_ipif(connp, addr, checkonly, name, 11023 first_mp); 11024 if (error != 0) 11025 return (error); 11026 break; /* goto sizeof (int) option return */ 11027 } 11028 11029 case IP_MULTICAST_TTL: 11030 /* Recorded in transport above IP */ 11031 *outvalp = *invalp; 11032 *outlenp = sizeof (uchar_t); 11033 return (0); 11034 case IP_MULTICAST_LOOP: 11035 if (!checkonly) { 11036 mutex_enter(&connp->conn_lock); 11037 connp->conn_multicast_loop = *invalp ? 1 : 0; 11038 mutex_exit(&connp->conn_lock); 11039 } 11040 *outvalp = *invalp; 11041 *outlenp = sizeof (uchar_t); 11042 return (0); 11043 case IP_ADD_MEMBERSHIP: 11044 case MCAST_JOIN_GROUP: 11045 case IP_DROP_MEMBERSHIP: 11046 case MCAST_LEAVE_GROUP: { 11047 struct ip_mreq *mreqp; 11048 struct group_req *greqp; 11049 ire_t *ire; 11050 boolean_t done = B_FALSE; 11051 ipaddr_t group, ifaddr; 11052 struct sockaddr_in *sin; 11053 uint32_t *ifindexp; 11054 boolean_t mcast_opt = B_TRUE; 11055 mcast_record_t fmode; 11056 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 11057 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 11058 11059 switch (name) { 11060 case IP_ADD_MEMBERSHIP: 11061 mcast_opt = B_FALSE; 11062 /* FALLTHRU */ 11063 case MCAST_JOIN_GROUP: 11064 fmode = MODE_IS_EXCLUDE; 11065 optfn = ip_opt_add_group; 11066 break; 11067 11068 case IP_DROP_MEMBERSHIP: 11069 mcast_opt = B_FALSE; 11070 /* FALLTHRU */ 11071 case MCAST_LEAVE_GROUP: 11072 fmode = MODE_IS_INCLUDE; 11073 optfn = ip_opt_delete_group; 11074 break; 11075 } 11076 11077 if (mcast_opt) { 11078 greqp = (struct group_req *)i1; 11079 sin = (struct sockaddr_in *)&greqp->gr_group; 11080 if (sin->sin_family != AF_INET) { 11081 *outlenp = 0; 11082 return (ENOPROTOOPT); 11083 } 11084 group = (ipaddr_t)sin->sin_addr.s_addr; 11085 ifaddr = INADDR_ANY; 11086 ifindexp = &greqp->gr_interface; 11087 } else { 11088 mreqp = (struct ip_mreq *)i1; 11089 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 11090 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 11091 ifindexp = NULL; 11092 } 11093 11094 /* 11095 * In the multirouting case, we need to replicate 11096 * the request on all interfaces that will take part 11097 * in replication. We do so because multirouting is 11098 * reflective, thus we will probably receive multi- 11099 * casts on those interfaces. 11100 * The ip_multirt_apply_membership() succeeds if the 11101 * operation succeeds on at least one interface. 11102 */ 11103 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 11104 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11105 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11106 if (ire != NULL) { 11107 if (ire->ire_flags & RTF_MULTIRT) { 11108 error = ip_multirt_apply_membership( 11109 optfn, ire, connp, checkonly, group, 11110 fmode, INADDR_ANY, first_mp); 11111 done = B_TRUE; 11112 } 11113 ire_refrele(ire); 11114 } 11115 if (!done) { 11116 error = optfn(connp, checkonly, group, ifaddr, 11117 ifindexp, fmode, INADDR_ANY, first_mp); 11118 } 11119 if (error) { 11120 /* 11121 * EINPROGRESS is a soft error, needs retry 11122 * so don't make *outlenp zero. 11123 */ 11124 if (error != EINPROGRESS) 11125 *outlenp = 0; 11126 return (error); 11127 } 11128 /* OK return - copy input buffer into output buffer */ 11129 if (invalp != outvalp) { 11130 /* don't trust bcopy for identical src/dst */ 11131 bcopy(invalp, outvalp, inlen); 11132 } 11133 *outlenp = inlen; 11134 return (0); 11135 } 11136 case IP_BLOCK_SOURCE: 11137 case IP_UNBLOCK_SOURCE: 11138 case IP_ADD_SOURCE_MEMBERSHIP: 11139 case IP_DROP_SOURCE_MEMBERSHIP: 11140 case MCAST_BLOCK_SOURCE: 11141 case MCAST_UNBLOCK_SOURCE: 11142 case MCAST_JOIN_SOURCE_GROUP: 11143 case MCAST_LEAVE_SOURCE_GROUP: { 11144 struct ip_mreq_source *imreqp; 11145 struct group_source_req *gsreqp; 11146 in_addr_t grp, src, ifaddr = INADDR_ANY; 11147 uint32_t ifindex = 0; 11148 mcast_record_t fmode; 11149 struct sockaddr_in *sin; 11150 ire_t *ire; 11151 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 11152 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 11153 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 11154 11155 switch (name) { 11156 case IP_BLOCK_SOURCE: 11157 mcast_opt = B_FALSE; 11158 /* FALLTHRU */ 11159 case MCAST_BLOCK_SOURCE: 11160 fmode = MODE_IS_EXCLUDE; 11161 optfn = ip_opt_add_group; 11162 break; 11163 11164 case IP_UNBLOCK_SOURCE: 11165 mcast_opt = B_FALSE; 11166 /* FALLTHRU */ 11167 case MCAST_UNBLOCK_SOURCE: 11168 fmode = MODE_IS_EXCLUDE; 11169 optfn = ip_opt_delete_group; 11170 break; 11171 11172 case IP_ADD_SOURCE_MEMBERSHIP: 11173 mcast_opt = B_FALSE; 11174 /* FALLTHRU */ 11175 case MCAST_JOIN_SOURCE_GROUP: 11176 fmode = MODE_IS_INCLUDE; 11177 optfn = ip_opt_add_group; 11178 break; 11179 11180 case IP_DROP_SOURCE_MEMBERSHIP: 11181 mcast_opt = B_FALSE; 11182 /* FALLTHRU */ 11183 case MCAST_LEAVE_SOURCE_GROUP: 11184 fmode = MODE_IS_INCLUDE; 11185 optfn = ip_opt_delete_group; 11186 break; 11187 } 11188 11189 if (mcast_opt) { 11190 gsreqp = (struct group_source_req *)i1; 11191 if (gsreqp->gsr_group.ss_family != AF_INET) { 11192 *outlenp = 0; 11193 return (ENOPROTOOPT); 11194 } 11195 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 11196 grp = (ipaddr_t)sin->sin_addr.s_addr; 11197 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 11198 src = (ipaddr_t)sin->sin_addr.s_addr; 11199 ifindex = gsreqp->gsr_interface; 11200 } else { 11201 imreqp = (struct ip_mreq_source *)i1; 11202 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 11203 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 11204 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 11205 } 11206 11207 /* 11208 * In the multirouting case, we need to replicate 11209 * the request as noted in the mcast cases above. 11210 */ 11211 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 11212 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11213 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11214 if (ire != NULL) { 11215 if (ire->ire_flags & RTF_MULTIRT) { 11216 error = ip_multirt_apply_membership( 11217 optfn, ire, connp, checkonly, grp, 11218 fmode, src, first_mp); 11219 done = B_TRUE; 11220 } 11221 ire_refrele(ire); 11222 } 11223 if (!done) { 11224 error = optfn(connp, checkonly, grp, ifaddr, 11225 &ifindex, fmode, src, first_mp); 11226 } 11227 if (error != 0) { 11228 /* 11229 * EINPROGRESS is a soft error, needs retry 11230 * so don't make *outlenp zero. 11231 */ 11232 if (error != EINPROGRESS) 11233 *outlenp = 0; 11234 return (error); 11235 } 11236 /* OK return - copy input buffer into output buffer */ 11237 if (invalp != outvalp) { 11238 bcopy(invalp, outvalp, inlen); 11239 } 11240 *outlenp = inlen; 11241 return (0); 11242 } 11243 case IP_SEC_OPT: 11244 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11245 if (error != 0) { 11246 *outlenp = 0; 11247 return (error); 11248 } 11249 break; 11250 case IP_HDRINCL: 11251 case IP_OPTIONS: 11252 case T_IP_OPTIONS: 11253 case IP_TOS: 11254 case T_IP_TOS: 11255 case IP_TTL: 11256 case IP_RECVDSTADDR: 11257 case IP_RECVOPTS: 11258 /* OK return - copy input buffer into output buffer */ 11259 if (invalp != outvalp) { 11260 /* don't trust bcopy for identical src/dst */ 11261 bcopy(invalp, outvalp, inlen); 11262 } 11263 *outlenp = inlen; 11264 return (0); 11265 case IP_RECVIF: 11266 /* Retrieve the inbound interface index */ 11267 if (!checkonly) { 11268 mutex_enter(&connp->conn_lock); 11269 connp->conn_recvif = *i1 ? 1 : 0; 11270 mutex_exit(&connp->conn_lock); 11271 } 11272 break; /* goto sizeof (int) option return */ 11273 case IP_RECVPKTINFO: 11274 if (!checkonly) { 11275 mutex_enter(&connp->conn_lock); 11276 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11277 mutex_exit(&connp->conn_lock); 11278 } 11279 break; /* goto sizeof (int) option return */ 11280 case IP_RECVSLLA: 11281 /* Retrieve the source link layer address */ 11282 if (!checkonly) { 11283 mutex_enter(&connp->conn_lock); 11284 connp->conn_recvslla = *i1 ? 1 : 0; 11285 mutex_exit(&connp->conn_lock); 11286 } 11287 break; /* goto sizeof (int) option return */ 11288 case MRT_INIT: 11289 case MRT_DONE: 11290 case MRT_ADD_VIF: 11291 case MRT_DEL_VIF: 11292 case MRT_ADD_MFC: 11293 case MRT_DEL_MFC: 11294 case MRT_ASSERT: 11295 if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) { 11296 *outlenp = 0; 11297 return (error); 11298 } 11299 error = ip_mrouter_set((int)name, q, checkonly, 11300 (uchar_t *)invalp, inlen, first_mp); 11301 if (error) { 11302 *outlenp = 0; 11303 return (error); 11304 } 11305 /* OK return - copy input buffer into output buffer */ 11306 if (invalp != outvalp) { 11307 /* don't trust bcopy for identical src/dst */ 11308 bcopy(invalp, outvalp, inlen); 11309 } 11310 *outlenp = inlen; 11311 return (0); 11312 case IP_BOUND_IF: 11313 case IP_XMIT_IF: 11314 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11315 level, name, first_mp); 11316 if (error != 0) 11317 return (error); 11318 break; /* goto sizeof (int) option return */ 11319 11320 case IP_UNSPEC_SRC: 11321 /* Allow sending with a zero source address */ 11322 if (!checkonly) { 11323 mutex_enter(&connp->conn_lock); 11324 connp->conn_unspec_src = *i1 ? 1 : 0; 11325 mutex_exit(&connp->conn_lock); 11326 } 11327 break; /* goto sizeof (int) option return */ 11328 default: 11329 /* 11330 * "soft" error (negative) 11331 * option not handled at this level 11332 * Note: Do not modify *outlenp 11333 */ 11334 return (-EINVAL); 11335 } 11336 break; 11337 case IPPROTO_IPV6: 11338 switch (name) { 11339 case IPV6_BOUND_IF: 11340 case IPV6_BOUND_PIF: 11341 case IPV6_DONTFAILOVER_IF: 11342 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11343 level, name, first_mp); 11344 if (error != 0) 11345 return (error); 11346 break; /* goto sizeof (int) option return */ 11347 11348 case IPV6_MULTICAST_IF: 11349 /* 11350 * The only possible errors are EINPROGRESS and 11351 * EINVAL. EINPROGRESS will be restarted and is not 11352 * a hard error. We call this option on both V4 and V6 11353 * If both return EINVAL, then this call returns 11354 * EINVAL. If at least one of them succeeds we 11355 * return success. 11356 */ 11357 found = B_FALSE; 11358 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11359 level, name, first_mp); 11360 if (error == EINPROGRESS) 11361 return (error); 11362 if (error == 0) 11363 found = B_TRUE; 11364 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11365 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 11366 if (error == 0) 11367 found = B_TRUE; 11368 if (!found) 11369 return (error); 11370 break; /* goto sizeof (int) option return */ 11371 11372 case IPV6_MULTICAST_HOPS: 11373 /* Recorded in transport above IP */ 11374 break; /* goto sizeof (int) option return */ 11375 case IPV6_MULTICAST_LOOP: 11376 if (!checkonly) { 11377 mutex_enter(&connp->conn_lock); 11378 connp->conn_multicast_loop = *i1; 11379 mutex_exit(&connp->conn_lock); 11380 } 11381 break; /* goto sizeof (int) option return */ 11382 case IPV6_JOIN_GROUP: 11383 case MCAST_JOIN_GROUP: 11384 case IPV6_LEAVE_GROUP: 11385 case MCAST_LEAVE_GROUP: { 11386 struct ipv6_mreq *ip_mreqp; 11387 struct group_req *greqp; 11388 ire_t *ire; 11389 boolean_t done = B_FALSE; 11390 in6_addr_t groupv6; 11391 uint32_t ifindex; 11392 boolean_t mcast_opt = B_TRUE; 11393 mcast_record_t fmode; 11394 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11395 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11396 11397 switch (name) { 11398 case IPV6_JOIN_GROUP: 11399 mcast_opt = B_FALSE; 11400 /* FALLTHRU */ 11401 case MCAST_JOIN_GROUP: 11402 fmode = MODE_IS_EXCLUDE; 11403 optfn = ip_opt_add_group_v6; 11404 break; 11405 11406 case IPV6_LEAVE_GROUP: 11407 mcast_opt = B_FALSE; 11408 /* FALLTHRU */ 11409 case MCAST_LEAVE_GROUP: 11410 fmode = MODE_IS_INCLUDE; 11411 optfn = ip_opt_delete_group_v6; 11412 break; 11413 } 11414 11415 if (mcast_opt) { 11416 struct sockaddr_in *sin; 11417 struct sockaddr_in6 *sin6; 11418 greqp = (struct group_req *)i1; 11419 if (greqp->gr_group.ss_family == AF_INET) { 11420 sin = (struct sockaddr_in *) 11421 &(greqp->gr_group); 11422 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 11423 &groupv6); 11424 } else { 11425 sin6 = (struct sockaddr_in6 *) 11426 &(greqp->gr_group); 11427 groupv6 = sin6->sin6_addr; 11428 } 11429 ifindex = greqp->gr_interface; 11430 } else { 11431 ip_mreqp = (struct ipv6_mreq *)i1; 11432 groupv6 = ip_mreqp->ipv6mr_multiaddr; 11433 ifindex = ip_mreqp->ipv6mr_interface; 11434 } 11435 /* 11436 * In the multirouting case, we need to replicate 11437 * the request on all interfaces that will take part 11438 * in replication. We do so because multirouting is 11439 * reflective, thus we will probably receive multi- 11440 * casts on those interfaces. 11441 * The ip_multirt_apply_membership_v6() succeeds if 11442 * the operation succeeds on at least one interface. 11443 */ 11444 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 11445 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11446 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11447 if (ire != NULL) { 11448 if (ire->ire_flags & RTF_MULTIRT) { 11449 error = ip_multirt_apply_membership_v6( 11450 optfn, ire, connp, checkonly, 11451 &groupv6, fmode, &ipv6_all_zeros, 11452 first_mp); 11453 done = B_TRUE; 11454 } 11455 ire_refrele(ire); 11456 } 11457 if (!done) { 11458 error = optfn(connp, checkonly, &groupv6, 11459 ifindex, fmode, &ipv6_all_zeros, first_mp); 11460 } 11461 if (error) { 11462 /* 11463 * EINPROGRESS is a soft error, needs retry 11464 * so don't make *outlenp zero. 11465 */ 11466 if (error != EINPROGRESS) 11467 *outlenp = 0; 11468 return (error); 11469 } 11470 /* OK return - copy input buffer into output buffer */ 11471 if (invalp != outvalp) { 11472 /* don't trust bcopy for identical src/dst */ 11473 bcopy(invalp, outvalp, inlen); 11474 } 11475 *outlenp = inlen; 11476 return (0); 11477 } 11478 case MCAST_BLOCK_SOURCE: 11479 case MCAST_UNBLOCK_SOURCE: 11480 case MCAST_JOIN_SOURCE_GROUP: 11481 case MCAST_LEAVE_SOURCE_GROUP: { 11482 struct group_source_req *gsreqp; 11483 in6_addr_t v6grp, v6src; 11484 uint32_t ifindex; 11485 mcast_record_t fmode; 11486 ire_t *ire; 11487 boolean_t done = B_FALSE; 11488 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11489 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11490 11491 switch (name) { 11492 case MCAST_BLOCK_SOURCE: 11493 fmode = MODE_IS_EXCLUDE; 11494 optfn = ip_opt_add_group_v6; 11495 break; 11496 case MCAST_UNBLOCK_SOURCE: 11497 fmode = MODE_IS_EXCLUDE; 11498 optfn = ip_opt_delete_group_v6; 11499 break; 11500 case MCAST_JOIN_SOURCE_GROUP: 11501 fmode = MODE_IS_INCLUDE; 11502 optfn = ip_opt_add_group_v6; 11503 break; 11504 case MCAST_LEAVE_SOURCE_GROUP: 11505 fmode = MODE_IS_INCLUDE; 11506 optfn = ip_opt_delete_group_v6; 11507 break; 11508 } 11509 11510 gsreqp = (struct group_source_req *)i1; 11511 ifindex = gsreqp->gsr_interface; 11512 if (gsreqp->gsr_group.ss_family == AF_INET) { 11513 struct sockaddr_in *s; 11514 s = (struct sockaddr_in *)&gsreqp->gsr_group; 11515 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 11516 s = (struct sockaddr_in *)&gsreqp->gsr_source; 11517 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 11518 } else { 11519 struct sockaddr_in6 *s6; 11520 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 11521 v6grp = s6->sin6_addr; 11522 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 11523 v6src = s6->sin6_addr; 11524 } 11525 11526 /* 11527 * In the multirouting case, we need to replicate 11528 * the request as noted in the mcast cases above. 11529 */ 11530 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 11531 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11532 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11533 if (ire != NULL) { 11534 if (ire->ire_flags & RTF_MULTIRT) { 11535 error = ip_multirt_apply_membership_v6( 11536 optfn, ire, connp, checkonly, 11537 &v6grp, fmode, &v6src, first_mp); 11538 done = B_TRUE; 11539 } 11540 ire_refrele(ire); 11541 } 11542 if (!done) { 11543 error = optfn(connp, checkonly, &v6grp, 11544 ifindex, fmode, &v6src, first_mp); 11545 } 11546 if (error != 0) { 11547 /* 11548 * EINPROGRESS is a soft error, needs retry 11549 * so don't make *outlenp zero. 11550 */ 11551 if (error != EINPROGRESS) 11552 *outlenp = 0; 11553 return (error); 11554 } 11555 /* OK return - copy input buffer into output buffer */ 11556 if (invalp != outvalp) { 11557 bcopy(invalp, outvalp, inlen); 11558 } 11559 *outlenp = inlen; 11560 return (0); 11561 } 11562 case IPV6_UNICAST_HOPS: 11563 /* Recorded in transport above IP */ 11564 break; /* goto sizeof (int) option return */ 11565 case IPV6_UNSPEC_SRC: 11566 /* Allow sending with a zero source address */ 11567 if (!checkonly) { 11568 mutex_enter(&connp->conn_lock); 11569 connp->conn_unspec_src = *i1 ? 1 : 0; 11570 mutex_exit(&connp->conn_lock); 11571 } 11572 break; /* goto sizeof (int) option return */ 11573 case IPV6_RECVPKTINFO: 11574 if (!checkonly) { 11575 mutex_enter(&connp->conn_lock); 11576 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11577 mutex_exit(&connp->conn_lock); 11578 } 11579 break; /* goto sizeof (int) option return */ 11580 case IPV6_RECVTCLASS: 11581 if (!checkonly) { 11582 if (*i1 < 0 || *i1 > 1) { 11583 return (EINVAL); 11584 } 11585 mutex_enter(&connp->conn_lock); 11586 connp->conn_ipv6_recvtclass = *i1; 11587 mutex_exit(&connp->conn_lock); 11588 } 11589 break; 11590 case IPV6_RECVPATHMTU: 11591 if (!checkonly) { 11592 if (*i1 < 0 || *i1 > 1) { 11593 return (EINVAL); 11594 } 11595 mutex_enter(&connp->conn_lock); 11596 connp->conn_ipv6_recvpathmtu = *i1; 11597 mutex_exit(&connp->conn_lock); 11598 } 11599 break; 11600 case IPV6_RECVHOPLIMIT: 11601 if (!checkonly) { 11602 mutex_enter(&connp->conn_lock); 11603 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 11604 mutex_exit(&connp->conn_lock); 11605 } 11606 break; /* goto sizeof (int) option return */ 11607 case IPV6_RECVHOPOPTS: 11608 if (!checkonly) { 11609 mutex_enter(&connp->conn_lock); 11610 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 11611 mutex_exit(&connp->conn_lock); 11612 } 11613 break; /* goto sizeof (int) option return */ 11614 case IPV6_RECVDSTOPTS: 11615 if (!checkonly) { 11616 mutex_enter(&connp->conn_lock); 11617 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 11618 mutex_exit(&connp->conn_lock); 11619 } 11620 break; /* goto sizeof (int) option return */ 11621 case IPV6_RECVRTHDR: 11622 if (!checkonly) { 11623 mutex_enter(&connp->conn_lock); 11624 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 11625 mutex_exit(&connp->conn_lock); 11626 } 11627 break; /* goto sizeof (int) option return */ 11628 case IPV6_RECVRTHDRDSTOPTS: 11629 if (!checkonly) { 11630 mutex_enter(&connp->conn_lock); 11631 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 11632 mutex_exit(&connp->conn_lock); 11633 } 11634 break; /* goto sizeof (int) option return */ 11635 case IPV6_PKTINFO: 11636 if (inlen == 0) 11637 return (-EINVAL); /* clearing option */ 11638 error = ip6_set_pktinfo(cr, connp, 11639 (struct in6_pktinfo *)invalp, first_mp); 11640 if (error != 0) 11641 *outlenp = 0; 11642 else 11643 *outlenp = inlen; 11644 return (error); 11645 case IPV6_NEXTHOP: { 11646 struct sockaddr_in6 *sin6; 11647 11648 /* Verify that the nexthop is reachable */ 11649 if (inlen == 0) 11650 return (-EINVAL); /* clearing option */ 11651 11652 sin6 = (struct sockaddr_in6 *)invalp; 11653 ire = ire_route_lookup_v6(&sin6->sin6_addr, 11654 0, 0, 0, NULL, NULL, connp->conn_zoneid, 11655 NULL, MATCH_IRE_DEFAULT, ipst); 11656 11657 if (ire == NULL) { 11658 *outlenp = 0; 11659 return (EHOSTUNREACH); 11660 } 11661 ire_refrele(ire); 11662 return (-EINVAL); 11663 } 11664 case IPV6_SEC_OPT: 11665 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11666 if (error != 0) { 11667 *outlenp = 0; 11668 return (error); 11669 } 11670 break; 11671 case IPV6_SRC_PREFERENCES: { 11672 /* 11673 * This is implemented strictly in the ip module 11674 * (here and in tcp_opt_*() to accomodate tcp 11675 * sockets). Modules above ip pass this option 11676 * down here since ip is the only one that needs to 11677 * be aware of source address preferences. 11678 * 11679 * This socket option only affects connected 11680 * sockets that haven't already bound to a specific 11681 * IPv6 address. In other words, sockets that 11682 * don't call bind() with an address other than the 11683 * unspecified address and that call connect(). 11684 * ip_bind_connected_v6() passes these preferences 11685 * to the ipif_select_source_v6() function. 11686 */ 11687 if (inlen != sizeof (uint32_t)) 11688 return (EINVAL); 11689 error = ip6_set_src_preferences(connp, 11690 *(uint32_t *)invalp); 11691 if (error != 0) { 11692 *outlenp = 0; 11693 return (error); 11694 } else { 11695 *outlenp = sizeof (uint32_t); 11696 } 11697 break; 11698 } 11699 case IPV6_V6ONLY: 11700 if (*i1 < 0 || *i1 > 1) { 11701 return (EINVAL); 11702 } 11703 mutex_enter(&connp->conn_lock); 11704 connp->conn_ipv6_v6only = *i1; 11705 mutex_exit(&connp->conn_lock); 11706 break; 11707 default: 11708 return (-EINVAL); 11709 } 11710 break; 11711 default: 11712 /* 11713 * "soft" error (negative) 11714 * option not handled at this level 11715 * Note: Do not modify *outlenp 11716 */ 11717 return (-EINVAL); 11718 } 11719 /* 11720 * Common case of return from an option that is sizeof (int) 11721 */ 11722 *(int *)outvalp = *i1; 11723 *outlenp = sizeof (int); 11724 return (0); 11725 } 11726 11727 /* 11728 * This routine gets default values of certain options whose default 11729 * values are maintained by protocol specific code 11730 */ 11731 /* ARGSUSED */ 11732 int 11733 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 11734 { 11735 int *i1 = (int *)ptr; 11736 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11737 11738 switch (level) { 11739 case IPPROTO_IP: 11740 switch (name) { 11741 case IP_MULTICAST_TTL: 11742 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 11743 return (sizeof (uchar_t)); 11744 case IP_MULTICAST_LOOP: 11745 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 11746 return (sizeof (uchar_t)); 11747 default: 11748 return (-1); 11749 } 11750 case IPPROTO_IPV6: 11751 switch (name) { 11752 case IPV6_UNICAST_HOPS: 11753 *i1 = ipst->ips_ipv6_def_hops; 11754 return (sizeof (int)); 11755 case IPV6_MULTICAST_HOPS: 11756 *i1 = IP_DEFAULT_MULTICAST_TTL; 11757 return (sizeof (int)); 11758 case IPV6_MULTICAST_LOOP: 11759 *i1 = IP_DEFAULT_MULTICAST_LOOP; 11760 return (sizeof (int)); 11761 case IPV6_V6ONLY: 11762 *i1 = 1; 11763 return (sizeof (int)); 11764 default: 11765 return (-1); 11766 } 11767 default: 11768 return (-1); 11769 } 11770 /* NOTREACHED */ 11771 } 11772 11773 /* 11774 * Given a destination address and a pointer to where to put the information 11775 * this routine fills in the mtuinfo. 11776 */ 11777 int 11778 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 11779 struct ip6_mtuinfo *mtuinfo, netstack_t *ns) 11780 { 11781 ire_t *ire; 11782 ip_stack_t *ipst = ns->netstack_ip; 11783 11784 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 11785 return (-1); 11786 11787 bzero(mtuinfo, sizeof (*mtuinfo)); 11788 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11789 mtuinfo->ip6m_addr.sin6_port = port; 11790 mtuinfo->ip6m_addr.sin6_addr = *in6; 11791 11792 ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL, ipst); 11793 if (ire != NULL) { 11794 mtuinfo->ip6m_mtu = ire->ire_max_frag; 11795 ire_refrele(ire); 11796 } else { 11797 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 11798 } 11799 return (sizeof (struct ip6_mtuinfo)); 11800 } 11801 11802 /* 11803 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 11804 * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and 11805 * isn't. This doesn't matter as the error checking is done properly for the 11806 * other MRT options coming in through ip_opt_set. 11807 */ 11808 int 11809 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 11810 { 11811 conn_t *connp = Q_TO_CONN(q); 11812 ipsec_req_t *req = (ipsec_req_t *)ptr; 11813 11814 switch (level) { 11815 case IPPROTO_IP: 11816 switch (name) { 11817 case MRT_VERSION: 11818 case MRT_ASSERT: 11819 (void) ip_mrouter_get(name, q, ptr); 11820 return (sizeof (int)); 11821 case IP_SEC_OPT: 11822 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 11823 case IP_NEXTHOP: 11824 if (connp->conn_nexthop_set) { 11825 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 11826 return (sizeof (ipaddr_t)); 11827 } else 11828 return (0); 11829 case IP_RECVPKTINFO: 11830 *(int *)ptr = connp->conn_ip_recvpktinfo ? 1: 0; 11831 return (sizeof (int)); 11832 default: 11833 break; 11834 } 11835 break; 11836 case IPPROTO_IPV6: 11837 switch (name) { 11838 case IPV6_SEC_OPT: 11839 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 11840 case IPV6_SRC_PREFERENCES: { 11841 return (ip6_get_src_preferences(connp, 11842 (uint32_t *)ptr)); 11843 } 11844 case IPV6_V6ONLY: 11845 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 11846 return (sizeof (int)); 11847 case IPV6_PATHMTU: 11848 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 11849 (struct ip6_mtuinfo *)ptr, 11850 connp->conn_netstack)); 11851 default: 11852 break; 11853 } 11854 break; 11855 default: 11856 break; 11857 } 11858 return (-1); 11859 } 11860 11861 /* Named Dispatch routine to get a current value out of our parameter table. */ 11862 /* ARGSUSED */ 11863 static int 11864 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11865 { 11866 ipparam_t *ippa = (ipparam_t *)cp; 11867 11868 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 11869 return (0); 11870 } 11871 11872 /* ARGSUSED */ 11873 static int 11874 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11875 { 11876 11877 (void) mi_mpprintf(mp, "%d", *(int *)cp); 11878 return (0); 11879 } 11880 11881 /* 11882 * Set ip{,6}_forwarding values. This means walking through all of the 11883 * ill's and toggling their forwarding values. 11884 */ 11885 /* ARGSUSED */ 11886 static int 11887 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11888 { 11889 long new_value; 11890 int *forwarding_value = (int *)cp; 11891 ill_t *walker; 11892 boolean_t isv6; 11893 ill_walk_context_t ctx; 11894 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11895 11896 isv6 = (forwarding_value == &ipst->ips_ipv6_forward); 11897 11898 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11899 new_value < 0 || new_value > 1) { 11900 return (EINVAL); 11901 } 11902 11903 *forwarding_value = new_value; 11904 11905 /* 11906 * Regardless of the current value of ip_forwarding, set all per-ill 11907 * values of ip_forwarding to the value being set. 11908 * 11909 * Bring all the ill's up to date with the new global value. 11910 */ 11911 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 11912 11913 if (isv6) 11914 walker = ILL_START_WALK_V6(&ctx, ipst); 11915 else 11916 walker = ILL_START_WALK_V4(&ctx, ipst); 11917 for (; walker != NULL; walker = ill_next(&ctx, walker)) { 11918 (void) ill_forward_set(q, mp, (new_value != 0), 11919 (caddr_t)walker); 11920 } 11921 rw_exit(&ipst->ips_ill_g_lock); 11922 11923 return (0); 11924 } 11925 11926 /* 11927 * Walk through the param array specified registering each element with the 11928 * Named Dispatch handler. This is called only during init. So it is ok 11929 * not to acquire any locks 11930 */ 11931 static boolean_t 11932 ip_param_register(IDP *ndp, ipparam_t *ippa, size_t ippa_cnt, 11933 ipndp_t *ipnd, size_t ipnd_cnt) 11934 { 11935 for (; ippa_cnt-- > 0; ippa++) { 11936 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 11937 if (!nd_load(ndp, ippa->ip_param_name, 11938 ip_param_get, ip_param_set, (caddr_t)ippa)) { 11939 nd_free(ndp); 11940 return (B_FALSE); 11941 } 11942 } 11943 } 11944 11945 for (; ipnd_cnt-- > 0; ipnd++) { 11946 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 11947 if (!nd_load(ndp, ipnd->ip_ndp_name, 11948 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 11949 ipnd->ip_ndp_data)) { 11950 nd_free(ndp); 11951 return (B_FALSE); 11952 } 11953 } 11954 } 11955 11956 return (B_TRUE); 11957 } 11958 11959 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 11960 /* ARGSUSED */ 11961 static int 11962 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11963 { 11964 long new_value; 11965 ipparam_t *ippa = (ipparam_t *)cp; 11966 11967 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11968 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 11969 return (EINVAL); 11970 } 11971 ippa->ip_param_value = new_value; 11972 return (0); 11973 } 11974 11975 /* 11976 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 11977 * When an ipf is passed here for the first time, if 11978 * we already have in-order fragments on the queue, we convert from the fast- 11979 * path reassembly scheme to the hard-case scheme. From then on, additional 11980 * fragments are reassembled here. We keep track of the start and end offsets 11981 * of each piece, and the number of holes in the chain. When the hole count 11982 * goes to zero, we are done! 11983 * 11984 * The ipf_count will be updated to account for any mblk(s) added (pointed to 11985 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 11986 * ipfb_count and ill_frag_count by the difference of ipf_count before and 11987 * after the call to ip_reassemble(). 11988 */ 11989 int 11990 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 11991 size_t msg_len) 11992 { 11993 uint_t end; 11994 mblk_t *next_mp; 11995 mblk_t *mp1; 11996 uint_t offset; 11997 boolean_t incr_dups = B_TRUE; 11998 boolean_t offset_zero_seen = B_FALSE; 11999 boolean_t pkt_boundary_checked = B_FALSE; 12000 12001 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 12002 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 12003 12004 /* Add in byte count */ 12005 ipf->ipf_count += msg_len; 12006 if (ipf->ipf_end) { 12007 /* 12008 * We were part way through in-order reassembly, but now there 12009 * is a hole. We walk through messages already queued, and 12010 * mark them for hard case reassembly. We know that up till 12011 * now they were in order starting from offset zero. 12012 */ 12013 offset = 0; 12014 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12015 IP_REASS_SET_START(mp1, offset); 12016 if (offset == 0) { 12017 ASSERT(ipf->ipf_nf_hdr_len != 0); 12018 offset = -ipf->ipf_nf_hdr_len; 12019 } 12020 offset += mp1->b_wptr - mp1->b_rptr; 12021 IP_REASS_SET_END(mp1, offset); 12022 } 12023 /* One hole at the end. */ 12024 ipf->ipf_hole_cnt = 1; 12025 /* Brand it as a hard case, forever. */ 12026 ipf->ipf_end = 0; 12027 } 12028 /* Walk through all the new pieces. */ 12029 do { 12030 end = start + (mp->b_wptr - mp->b_rptr); 12031 /* 12032 * If start is 0, decrease 'end' only for the first mblk of 12033 * the fragment. Otherwise 'end' can get wrong value in the 12034 * second pass of the loop if first mblk is exactly the 12035 * size of ipf_nf_hdr_len. 12036 */ 12037 if (start == 0 && !offset_zero_seen) { 12038 /* First segment */ 12039 ASSERT(ipf->ipf_nf_hdr_len != 0); 12040 end -= ipf->ipf_nf_hdr_len; 12041 offset_zero_seen = B_TRUE; 12042 } 12043 next_mp = mp->b_cont; 12044 /* 12045 * We are checking to see if there is any interesing data 12046 * to process. If there isn't and the mblk isn't the 12047 * one which carries the unfragmentable header then we 12048 * drop it. It's possible to have just the unfragmentable 12049 * header come through without any data. That needs to be 12050 * saved. 12051 * 12052 * If the assert at the top of this function holds then the 12053 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 12054 * is infrequently traveled enough that the test is left in 12055 * to protect against future code changes which break that 12056 * invariant. 12057 */ 12058 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 12059 /* Empty. Blast it. */ 12060 IP_REASS_SET_START(mp, 0); 12061 IP_REASS_SET_END(mp, 0); 12062 /* 12063 * If the ipf points to the mblk we are about to free, 12064 * update ipf to point to the next mblk (or NULL 12065 * if none). 12066 */ 12067 if (ipf->ipf_mp->b_cont == mp) 12068 ipf->ipf_mp->b_cont = next_mp; 12069 freeb(mp); 12070 continue; 12071 } 12072 mp->b_cont = NULL; 12073 IP_REASS_SET_START(mp, start); 12074 IP_REASS_SET_END(mp, end); 12075 if (!ipf->ipf_tail_mp) { 12076 ipf->ipf_tail_mp = mp; 12077 ipf->ipf_mp->b_cont = mp; 12078 if (start == 0 || !more) { 12079 ipf->ipf_hole_cnt = 1; 12080 /* 12081 * if the first fragment comes in more than one 12082 * mblk, this loop will be executed for each 12083 * mblk. Need to adjust hole count so exiting 12084 * this routine will leave hole count at 1. 12085 */ 12086 if (next_mp) 12087 ipf->ipf_hole_cnt++; 12088 } else 12089 ipf->ipf_hole_cnt = 2; 12090 continue; 12091 } else if (ipf->ipf_last_frag_seen && !more && 12092 !pkt_boundary_checked) { 12093 /* 12094 * We check datagram boundary only if this fragment 12095 * claims to be the last fragment and we have seen a 12096 * last fragment in the past too. We do this only 12097 * once for a given fragment. 12098 * 12099 * start cannot be 0 here as fragments with start=0 12100 * and MF=0 gets handled as a complete packet. These 12101 * fragments should not reach here. 12102 */ 12103 12104 if (start + msgdsize(mp) != 12105 IP_REASS_END(ipf->ipf_tail_mp)) { 12106 /* 12107 * We have two fragments both of which claim 12108 * to be the last fragment but gives conflicting 12109 * information about the whole datagram size. 12110 * Something fishy is going on. Drop the 12111 * fragment and free up the reassembly list. 12112 */ 12113 return (IP_REASS_FAILED); 12114 } 12115 12116 /* 12117 * We shouldn't come to this code block again for this 12118 * particular fragment. 12119 */ 12120 pkt_boundary_checked = B_TRUE; 12121 } 12122 12123 /* New stuff at or beyond tail? */ 12124 offset = IP_REASS_END(ipf->ipf_tail_mp); 12125 if (start >= offset) { 12126 if (ipf->ipf_last_frag_seen) { 12127 /* current fragment is beyond last fragment */ 12128 return (IP_REASS_FAILED); 12129 } 12130 /* Link it on end. */ 12131 ipf->ipf_tail_mp->b_cont = mp; 12132 ipf->ipf_tail_mp = mp; 12133 if (more) { 12134 if (start != offset) 12135 ipf->ipf_hole_cnt++; 12136 } else if (start == offset && next_mp == NULL) 12137 ipf->ipf_hole_cnt--; 12138 continue; 12139 } 12140 mp1 = ipf->ipf_mp->b_cont; 12141 offset = IP_REASS_START(mp1); 12142 /* New stuff at the front? */ 12143 if (start < offset) { 12144 if (start == 0) { 12145 if (end >= offset) { 12146 /* Nailed the hole at the begining. */ 12147 ipf->ipf_hole_cnt--; 12148 } 12149 } else if (end < offset) { 12150 /* 12151 * A hole, stuff, and a hole where there used 12152 * to be just a hole. 12153 */ 12154 ipf->ipf_hole_cnt++; 12155 } 12156 mp->b_cont = mp1; 12157 /* Check for overlap. */ 12158 while (end > offset) { 12159 if (end < IP_REASS_END(mp1)) { 12160 mp->b_wptr -= end - offset; 12161 IP_REASS_SET_END(mp, offset); 12162 BUMP_MIB(ill->ill_ip_mib, 12163 ipIfStatsReasmPartDups); 12164 break; 12165 } 12166 /* Did we cover another hole? */ 12167 if ((mp1->b_cont && 12168 IP_REASS_END(mp1) != 12169 IP_REASS_START(mp1->b_cont) && 12170 end >= IP_REASS_START(mp1->b_cont)) || 12171 (!ipf->ipf_last_frag_seen && !more)) { 12172 ipf->ipf_hole_cnt--; 12173 } 12174 /* Clip out mp1. */ 12175 if ((mp->b_cont = mp1->b_cont) == NULL) { 12176 /* 12177 * After clipping out mp1, this guy 12178 * is now hanging off the end. 12179 */ 12180 ipf->ipf_tail_mp = mp; 12181 } 12182 IP_REASS_SET_START(mp1, 0); 12183 IP_REASS_SET_END(mp1, 0); 12184 /* Subtract byte count */ 12185 ipf->ipf_count -= mp1->b_datap->db_lim - 12186 mp1->b_datap->db_base; 12187 freeb(mp1); 12188 BUMP_MIB(ill->ill_ip_mib, 12189 ipIfStatsReasmPartDups); 12190 mp1 = mp->b_cont; 12191 if (!mp1) 12192 break; 12193 offset = IP_REASS_START(mp1); 12194 } 12195 ipf->ipf_mp->b_cont = mp; 12196 continue; 12197 } 12198 /* 12199 * The new piece starts somewhere between the start of the head 12200 * and before the end of the tail. 12201 */ 12202 for (; mp1; mp1 = mp1->b_cont) { 12203 offset = IP_REASS_END(mp1); 12204 if (start < offset) { 12205 if (end <= offset) { 12206 /* Nothing new. */ 12207 IP_REASS_SET_START(mp, 0); 12208 IP_REASS_SET_END(mp, 0); 12209 /* Subtract byte count */ 12210 ipf->ipf_count -= mp->b_datap->db_lim - 12211 mp->b_datap->db_base; 12212 if (incr_dups) { 12213 ipf->ipf_num_dups++; 12214 incr_dups = B_FALSE; 12215 } 12216 freeb(mp); 12217 BUMP_MIB(ill->ill_ip_mib, 12218 ipIfStatsReasmDuplicates); 12219 break; 12220 } 12221 /* 12222 * Trim redundant stuff off beginning of new 12223 * piece. 12224 */ 12225 IP_REASS_SET_START(mp, offset); 12226 mp->b_rptr += offset - start; 12227 BUMP_MIB(ill->ill_ip_mib, 12228 ipIfStatsReasmPartDups); 12229 start = offset; 12230 if (!mp1->b_cont) { 12231 /* 12232 * After trimming, this guy is now 12233 * hanging off the end. 12234 */ 12235 mp1->b_cont = mp; 12236 ipf->ipf_tail_mp = mp; 12237 if (!more) { 12238 ipf->ipf_hole_cnt--; 12239 } 12240 break; 12241 } 12242 } 12243 if (start >= IP_REASS_START(mp1->b_cont)) 12244 continue; 12245 /* Fill a hole */ 12246 if (start > offset) 12247 ipf->ipf_hole_cnt++; 12248 mp->b_cont = mp1->b_cont; 12249 mp1->b_cont = mp; 12250 mp1 = mp->b_cont; 12251 offset = IP_REASS_START(mp1); 12252 if (end >= offset) { 12253 ipf->ipf_hole_cnt--; 12254 /* Check for overlap. */ 12255 while (end > offset) { 12256 if (end < IP_REASS_END(mp1)) { 12257 mp->b_wptr -= end - offset; 12258 IP_REASS_SET_END(mp, offset); 12259 /* 12260 * TODO we might bump 12261 * this up twice if there is 12262 * overlap at both ends. 12263 */ 12264 BUMP_MIB(ill->ill_ip_mib, 12265 ipIfStatsReasmPartDups); 12266 break; 12267 } 12268 /* Did we cover another hole? */ 12269 if ((mp1->b_cont && 12270 IP_REASS_END(mp1) 12271 != IP_REASS_START(mp1->b_cont) && 12272 end >= 12273 IP_REASS_START(mp1->b_cont)) || 12274 (!ipf->ipf_last_frag_seen && 12275 !more)) { 12276 ipf->ipf_hole_cnt--; 12277 } 12278 /* Clip out mp1. */ 12279 if ((mp->b_cont = mp1->b_cont) == 12280 NULL) { 12281 /* 12282 * After clipping out mp1, 12283 * this guy is now hanging 12284 * off the end. 12285 */ 12286 ipf->ipf_tail_mp = mp; 12287 } 12288 IP_REASS_SET_START(mp1, 0); 12289 IP_REASS_SET_END(mp1, 0); 12290 /* Subtract byte count */ 12291 ipf->ipf_count -= 12292 mp1->b_datap->db_lim - 12293 mp1->b_datap->db_base; 12294 freeb(mp1); 12295 BUMP_MIB(ill->ill_ip_mib, 12296 ipIfStatsReasmPartDups); 12297 mp1 = mp->b_cont; 12298 if (!mp1) 12299 break; 12300 offset = IP_REASS_START(mp1); 12301 } 12302 } 12303 break; 12304 } 12305 } while (start = end, mp = next_mp); 12306 12307 /* Fragment just processed could be the last one. Remember this fact */ 12308 if (!more) 12309 ipf->ipf_last_frag_seen = B_TRUE; 12310 12311 /* Still got holes? */ 12312 if (ipf->ipf_hole_cnt) 12313 return (IP_REASS_PARTIAL); 12314 /* Clean up overloaded fields to avoid upstream disasters. */ 12315 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12316 IP_REASS_SET_START(mp1, 0); 12317 IP_REASS_SET_END(mp1, 0); 12318 } 12319 return (IP_REASS_COMPLETE); 12320 } 12321 12322 /* 12323 * ipsec processing for the fast path, used for input UDP Packets 12324 */ 12325 static boolean_t 12326 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 12327 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present) 12328 { 12329 uint32_t ill_index; 12330 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 12331 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 12332 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 12333 12334 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12335 /* The ill_index of the incoming ILL */ 12336 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 12337 12338 /* pass packet up to the transport */ 12339 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 12340 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 12341 NULL, mctl_present); 12342 if (*first_mpp == NULL) { 12343 return (B_FALSE); 12344 } 12345 } 12346 12347 /* Initiate IPPF processing for fastpath UDP */ 12348 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 12349 ip_process(IPP_LOCAL_IN, mpp, ill_index); 12350 if (*mpp == NULL) { 12351 ip2dbg(("ip_input_ipsec_process: UDP pkt " 12352 "deferred/dropped during IPPF processing\n")); 12353 return (B_FALSE); 12354 } 12355 } 12356 /* 12357 * We make the checks as below since we are in the fast path 12358 * and want to minimize the number of checks if the IP_RECVIF and/or 12359 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 12360 */ 12361 if (connp->conn_recvif || connp->conn_recvslla || 12362 connp->conn_ip_recvpktinfo) { 12363 if (connp->conn_recvif) { 12364 in_flags = IPF_RECVIF; 12365 } 12366 /* 12367 * UDP supports IP_RECVPKTINFO option for both v4 and v6 12368 * so the flag passed to ip_add_info is based on IP version 12369 * of connp. 12370 */ 12371 if (connp->conn_ip_recvpktinfo) { 12372 if (connp->conn_af_isv6) { 12373 /* 12374 * V6 only needs index 12375 */ 12376 in_flags |= IPF_RECVIF; 12377 } else { 12378 /* 12379 * V4 needs index + matching address. 12380 */ 12381 in_flags |= IPF_RECVADDR; 12382 } 12383 } 12384 if (connp->conn_recvslla) { 12385 in_flags |= IPF_RECVSLLA; 12386 } 12387 /* 12388 * since in_flags are being set ill will be 12389 * referenced in ip_add_info, so it better not 12390 * be NULL. 12391 */ 12392 /* 12393 * the actual data will be contained in b_cont 12394 * upon successful return of the following call. 12395 * If the call fails then the original mblk is 12396 * returned. 12397 */ 12398 *mpp = ip_add_info(*mpp, ill, in_flags, IPCL_ZONEID(connp), 12399 ipst); 12400 } 12401 12402 return (B_TRUE); 12403 } 12404 12405 /* 12406 * Fragmentation reassembly. Each ILL has a hash table for 12407 * queuing packets undergoing reassembly for all IPIFs 12408 * associated with the ILL. The hash is based on the packet 12409 * IP ident field. The ILL frag hash table was allocated 12410 * as a timer block at the time the ILL was created. Whenever 12411 * there is anything on the reassembly queue, the timer will 12412 * be running. Returns B_TRUE if successful else B_FALSE; 12413 * frees mp on failure. 12414 */ 12415 static boolean_t 12416 ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, 12417 uint32_t *cksum_val, uint16_t *cksum_flags) 12418 { 12419 uint32_t frag_offset_flags; 12420 ill_t *ill = (ill_t *)q->q_ptr; 12421 mblk_t *mp = *mpp; 12422 mblk_t *t_mp; 12423 ipaddr_t dst; 12424 uint8_t proto = ipha->ipha_protocol; 12425 uint32_t sum_val; 12426 uint16_t sum_flags; 12427 ipf_t *ipf; 12428 ipf_t **ipfp; 12429 ipfb_t *ipfb; 12430 uint16_t ident; 12431 uint32_t offset; 12432 ipaddr_t src; 12433 uint_t hdr_length; 12434 uint32_t end; 12435 mblk_t *mp1; 12436 mblk_t *tail_mp; 12437 size_t count; 12438 size_t msg_len; 12439 uint8_t ecn_info = 0; 12440 uint32_t packet_size; 12441 boolean_t pruned = B_FALSE; 12442 ip_stack_t *ipst = ill->ill_ipst; 12443 12444 if (cksum_val != NULL) 12445 *cksum_val = 0; 12446 if (cksum_flags != NULL) 12447 *cksum_flags = 0; 12448 12449 /* 12450 * Drop the fragmented as early as possible, if 12451 * we don't have resource(s) to re-assemble. 12452 */ 12453 if (ipst->ips_ip_reass_queue_bytes == 0) { 12454 freemsg(mp); 12455 return (B_FALSE); 12456 } 12457 12458 /* Check for fragmentation offset; return if there's none */ 12459 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 12460 (IPH_MF | IPH_OFFSET)) == 0) 12461 return (B_TRUE); 12462 12463 /* 12464 * We utilize hardware computed checksum info only for UDP since 12465 * IP fragmentation is a normal occurence for the protocol. In 12466 * addition, checksum offload support for IP fragments carrying 12467 * UDP payload is commonly implemented across network adapters. 12468 */ 12469 ASSERT(ill != NULL); 12470 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 12471 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 12472 mblk_t *mp1 = mp->b_cont; 12473 int32_t len; 12474 12475 /* Record checksum information from the packet */ 12476 sum_val = (uint32_t)DB_CKSUM16(mp); 12477 sum_flags = DB_CKSUMFLAGS(mp); 12478 12479 /* IP payload offset from beginning of mblk */ 12480 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 12481 12482 if ((sum_flags & HCK_PARTIALCKSUM) && 12483 (mp1 == NULL || mp1->b_cont == NULL) && 12484 offset >= DB_CKSUMSTART(mp) && 12485 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 12486 uint32_t adj; 12487 /* 12488 * Partial checksum has been calculated by hardware 12489 * and attached to the packet; in addition, any 12490 * prepended extraneous data is even byte aligned. 12491 * If any such data exists, we adjust the checksum; 12492 * this would also handle any postpended data. 12493 */ 12494 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 12495 mp, mp1, len, adj); 12496 12497 /* One's complement subtract extraneous checksum */ 12498 if (adj >= sum_val) 12499 sum_val = ~(adj - sum_val) & 0xFFFF; 12500 else 12501 sum_val -= adj; 12502 } 12503 } else { 12504 sum_val = 0; 12505 sum_flags = 0; 12506 } 12507 12508 /* Clear hardware checksumming flag */ 12509 DB_CKSUMFLAGS(mp) = 0; 12510 12511 ident = ipha->ipha_ident; 12512 offset = (frag_offset_flags << 3) & 0xFFFF; 12513 src = ipha->ipha_src; 12514 dst = ipha->ipha_dst; 12515 hdr_length = IPH_HDR_LENGTH(ipha); 12516 end = ntohs(ipha->ipha_length) - hdr_length; 12517 12518 /* If end == 0 then we have a packet with no data, so just free it */ 12519 if (end == 0) { 12520 freemsg(mp); 12521 return (B_FALSE); 12522 } 12523 12524 /* Record the ECN field info. */ 12525 ecn_info = (ipha->ipha_type_of_service & 0x3); 12526 if (offset != 0) { 12527 /* 12528 * If this isn't the first piece, strip the header, and 12529 * add the offset to the end value. 12530 */ 12531 mp->b_rptr += hdr_length; 12532 end += offset; 12533 } 12534 12535 msg_len = MBLKSIZE(mp); 12536 tail_mp = mp; 12537 while (tail_mp->b_cont != NULL) { 12538 tail_mp = tail_mp->b_cont; 12539 msg_len += MBLKSIZE(tail_mp); 12540 } 12541 12542 /* If the reassembly list for this ILL will get too big, prune it */ 12543 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 12544 ipst->ips_ip_reass_queue_bytes) { 12545 ill_frag_prune(ill, 12546 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 : 12547 (ipst->ips_ip_reass_queue_bytes - msg_len)); 12548 pruned = B_TRUE; 12549 } 12550 12551 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 12552 mutex_enter(&ipfb->ipfb_lock); 12553 12554 ipfp = &ipfb->ipfb_ipf; 12555 /* Try to find an existing fragment queue for this packet. */ 12556 for (;;) { 12557 ipf = ipfp[0]; 12558 if (ipf != NULL) { 12559 /* 12560 * It has to match on ident and src/dst address. 12561 */ 12562 if (ipf->ipf_ident == ident && 12563 ipf->ipf_src == src && 12564 ipf->ipf_dst == dst && 12565 ipf->ipf_protocol == proto) { 12566 /* 12567 * If we have received too many 12568 * duplicate fragments for this packet 12569 * free it. 12570 */ 12571 if (ipf->ipf_num_dups > ip_max_frag_dups) { 12572 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12573 freemsg(mp); 12574 mutex_exit(&ipfb->ipfb_lock); 12575 return (B_FALSE); 12576 } 12577 /* Found it. */ 12578 break; 12579 } 12580 ipfp = &ipf->ipf_hash_next; 12581 continue; 12582 } 12583 12584 /* 12585 * If we pruned the list, do we want to store this new 12586 * fragment?. We apply an optimization here based on the 12587 * fact that most fragments will be received in order. 12588 * So if the offset of this incoming fragment is zero, 12589 * it is the first fragment of a new packet. We will 12590 * keep it. Otherwise drop the fragment, as we have 12591 * probably pruned the packet already (since the 12592 * packet cannot be found). 12593 */ 12594 if (pruned && offset != 0) { 12595 mutex_exit(&ipfb->ipfb_lock); 12596 freemsg(mp); 12597 return (B_FALSE); 12598 } 12599 12600 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) { 12601 /* 12602 * Too many fragmented packets in this hash 12603 * bucket. Free the oldest. 12604 */ 12605 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 12606 } 12607 12608 /* New guy. Allocate a frag message. */ 12609 mp1 = allocb(sizeof (*ipf), BPRI_MED); 12610 if (mp1 == NULL) { 12611 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12612 freemsg(mp); 12613 reass_done: 12614 mutex_exit(&ipfb->ipfb_lock); 12615 return (B_FALSE); 12616 } 12617 12618 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); 12619 mp1->b_cont = mp; 12620 12621 /* Initialize the fragment header. */ 12622 ipf = (ipf_t *)mp1->b_rptr; 12623 ipf->ipf_mp = mp1; 12624 ipf->ipf_ptphn = ipfp; 12625 ipfp[0] = ipf; 12626 ipf->ipf_hash_next = NULL; 12627 ipf->ipf_ident = ident; 12628 ipf->ipf_protocol = proto; 12629 ipf->ipf_src = src; 12630 ipf->ipf_dst = dst; 12631 ipf->ipf_nf_hdr_len = 0; 12632 /* Record reassembly start time. */ 12633 ipf->ipf_timestamp = gethrestime_sec(); 12634 /* Record ipf generation and account for frag header */ 12635 ipf->ipf_gen = ill->ill_ipf_gen++; 12636 ipf->ipf_count = MBLKSIZE(mp1); 12637 ipf->ipf_last_frag_seen = B_FALSE; 12638 ipf->ipf_ecn = ecn_info; 12639 ipf->ipf_num_dups = 0; 12640 ipfb->ipfb_frag_pkts++; 12641 ipf->ipf_checksum = 0; 12642 ipf->ipf_checksum_flags = 0; 12643 12644 /* Store checksum value in fragment header */ 12645 if (sum_flags != 0) { 12646 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12647 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12648 ipf->ipf_checksum = sum_val; 12649 ipf->ipf_checksum_flags = sum_flags; 12650 } 12651 12652 /* 12653 * We handle reassembly two ways. In the easy case, 12654 * where all the fragments show up in order, we do 12655 * minimal bookkeeping, and just clip new pieces on 12656 * the end. If we ever see a hole, then we go off 12657 * to ip_reassemble which has to mark the pieces and 12658 * keep track of the number of holes, etc. Obviously, 12659 * the point of having both mechanisms is so we can 12660 * handle the easy case as efficiently as possible. 12661 */ 12662 if (offset == 0) { 12663 /* Easy case, in-order reassembly so far. */ 12664 ipf->ipf_count += msg_len; 12665 ipf->ipf_tail_mp = tail_mp; 12666 /* 12667 * Keep track of next expected offset in 12668 * ipf_end. 12669 */ 12670 ipf->ipf_end = end; 12671 ipf->ipf_nf_hdr_len = hdr_length; 12672 } else { 12673 /* Hard case, hole at the beginning. */ 12674 ipf->ipf_tail_mp = NULL; 12675 /* 12676 * ipf_end == 0 means that we have given up 12677 * on easy reassembly. 12678 */ 12679 ipf->ipf_end = 0; 12680 12681 /* Forget checksum offload from now on */ 12682 ipf->ipf_checksum_flags = 0; 12683 12684 /* 12685 * ipf_hole_cnt is set by ip_reassemble. 12686 * ipf_count is updated by ip_reassemble. 12687 * No need to check for return value here 12688 * as we don't expect reassembly to complete 12689 * or fail for the first fragment itself. 12690 */ 12691 (void) ip_reassemble(mp, ipf, 12692 (frag_offset_flags & IPH_OFFSET) << 3, 12693 (frag_offset_flags & IPH_MF), ill, msg_len); 12694 } 12695 /* Update per ipfb and ill byte counts */ 12696 ipfb->ipfb_count += ipf->ipf_count; 12697 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12698 ill->ill_frag_count += ipf->ipf_count; 12699 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12700 /* If the frag timer wasn't already going, start it. */ 12701 mutex_enter(&ill->ill_lock); 12702 ill_frag_timer_start(ill); 12703 mutex_exit(&ill->ill_lock); 12704 goto reass_done; 12705 } 12706 12707 /* 12708 * If the packet's flag has changed (it could be coming up 12709 * from an interface different than the previous, therefore 12710 * possibly different checksum capability), then forget about 12711 * any stored checksum states. Otherwise add the value to 12712 * the existing one stored in the fragment header. 12713 */ 12714 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 12715 sum_val += ipf->ipf_checksum; 12716 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12717 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12718 ipf->ipf_checksum = sum_val; 12719 } else if (ipf->ipf_checksum_flags != 0) { 12720 /* Forget checksum offload from now on */ 12721 ipf->ipf_checksum_flags = 0; 12722 } 12723 12724 /* 12725 * We have a new piece of a datagram which is already being 12726 * reassembled. Update the ECN info if all IP fragments 12727 * are ECN capable. If there is one which is not, clear 12728 * all the info. If there is at least one which has CE 12729 * code point, IP needs to report that up to transport. 12730 */ 12731 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 12732 if (ecn_info == IPH_ECN_CE) 12733 ipf->ipf_ecn = IPH_ECN_CE; 12734 } else { 12735 ipf->ipf_ecn = IPH_ECN_NECT; 12736 } 12737 if (offset && ipf->ipf_end == offset) { 12738 /* The new fragment fits at the end */ 12739 ipf->ipf_tail_mp->b_cont = mp; 12740 /* Update the byte count */ 12741 ipf->ipf_count += msg_len; 12742 /* Update per ipfb and ill byte counts */ 12743 ipfb->ipfb_count += msg_len; 12744 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12745 ill->ill_frag_count += msg_len; 12746 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12747 if (frag_offset_flags & IPH_MF) { 12748 /* More to come. */ 12749 ipf->ipf_end = end; 12750 ipf->ipf_tail_mp = tail_mp; 12751 goto reass_done; 12752 } 12753 } else { 12754 /* Go do the hard cases. */ 12755 int ret; 12756 12757 if (offset == 0) 12758 ipf->ipf_nf_hdr_len = hdr_length; 12759 12760 /* Save current byte count */ 12761 count = ipf->ipf_count; 12762 ret = ip_reassemble(mp, ipf, 12763 (frag_offset_flags & IPH_OFFSET) << 3, 12764 (frag_offset_flags & IPH_MF), ill, msg_len); 12765 /* Count of bytes added and subtracted (freeb()ed) */ 12766 count = ipf->ipf_count - count; 12767 if (count) { 12768 /* Update per ipfb and ill byte counts */ 12769 ipfb->ipfb_count += count; 12770 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12771 ill->ill_frag_count += count; 12772 ASSERT(ill->ill_frag_count > 0); 12773 } 12774 if (ret == IP_REASS_PARTIAL) { 12775 goto reass_done; 12776 } else if (ret == IP_REASS_FAILED) { 12777 /* Reassembly failed. Free up all resources */ 12778 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12779 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 12780 IP_REASS_SET_START(t_mp, 0); 12781 IP_REASS_SET_END(t_mp, 0); 12782 } 12783 freemsg(mp); 12784 goto reass_done; 12785 } 12786 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 12787 } 12788 /* 12789 * We have completed reassembly. Unhook the frag header from 12790 * the reassembly list. 12791 * 12792 * Before we free the frag header, record the ECN info 12793 * to report back to the transport. 12794 */ 12795 ecn_info = ipf->ipf_ecn; 12796 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); 12797 ipfp = ipf->ipf_ptphn; 12798 12799 /* We need to supply these to caller */ 12800 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 12801 sum_val = ipf->ipf_checksum; 12802 else 12803 sum_val = 0; 12804 12805 mp1 = ipf->ipf_mp; 12806 count = ipf->ipf_count; 12807 ipf = ipf->ipf_hash_next; 12808 if (ipf != NULL) 12809 ipf->ipf_ptphn = ipfp; 12810 ipfp[0] = ipf; 12811 ill->ill_frag_count -= count; 12812 ASSERT(ipfb->ipfb_count >= count); 12813 ipfb->ipfb_count -= count; 12814 ipfb->ipfb_frag_pkts--; 12815 mutex_exit(&ipfb->ipfb_lock); 12816 /* Ditch the frag header. */ 12817 mp = mp1->b_cont; 12818 12819 freeb(mp1); 12820 12821 /* Restore original IP length in header. */ 12822 packet_size = (uint32_t)msgdsize(mp); 12823 if (packet_size > IP_MAXPACKET) { 12824 freemsg(mp); 12825 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 12826 return (B_FALSE); 12827 } 12828 12829 if (DB_REF(mp) > 1) { 12830 mblk_t *mp2 = copymsg(mp); 12831 12832 freemsg(mp); 12833 if (mp2 == NULL) { 12834 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12835 return (B_FALSE); 12836 } 12837 mp = mp2; 12838 } 12839 ipha = (ipha_t *)mp->b_rptr; 12840 12841 ipha->ipha_length = htons((uint16_t)packet_size); 12842 /* We're now complete, zip the frag state */ 12843 ipha->ipha_fragment_offset_and_flags = 0; 12844 /* Record the ECN info. */ 12845 ipha->ipha_type_of_service &= 0xFC; 12846 ipha->ipha_type_of_service |= ecn_info; 12847 *mpp = mp; 12848 12849 /* Reassembly is successful; return checksum information if needed */ 12850 if (cksum_val != NULL) 12851 *cksum_val = sum_val; 12852 if (cksum_flags != NULL) 12853 *cksum_flags = sum_flags; 12854 12855 return (B_TRUE); 12856 } 12857 12858 /* 12859 * Perform ip header check sum update local options. 12860 * return B_TRUE if all is well, else return B_FALSE and release 12861 * the mp. caller is responsible for decrementing ire ref cnt. 12862 */ 12863 static boolean_t 12864 ip_options_cksum(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12865 ip_stack_t *ipst) 12866 { 12867 mblk_t *first_mp; 12868 boolean_t mctl_present; 12869 uint16_t sum; 12870 12871 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12872 /* 12873 * Don't do the checksum if it has gone through AH/ESP 12874 * processing. 12875 */ 12876 if (!mctl_present) { 12877 sum = ip_csum_hdr(ipha); 12878 if (sum != 0) { 12879 if (ill != NULL) { 12880 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12881 } else { 12882 BUMP_MIB(&ipst->ips_ip_mib, 12883 ipIfStatsInCksumErrs); 12884 } 12885 freemsg(first_mp); 12886 return (B_FALSE); 12887 } 12888 } 12889 12890 if (!ip_rput_local_options(q, mp, ipha, ire, ipst)) { 12891 if (mctl_present) 12892 freeb(first_mp); 12893 return (B_FALSE); 12894 } 12895 12896 return (B_TRUE); 12897 } 12898 12899 /* 12900 * All udp packet are delivered to the local host via this routine. 12901 */ 12902 void 12903 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12904 ill_t *recv_ill) 12905 { 12906 uint32_t sum; 12907 uint32_t u1; 12908 boolean_t mctl_present; 12909 conn_t *connp; 12910 mblk_t *first_mp; 12911 uint16_t *up; 12912 ill_t *ill = (ill_t *)q->q_ptr; 12913 uint16_t reass_hck_flags = 0; 12914 ip_stack_t *ipst; 12915 12916 ASSERT(recv_ill != NULL); 12917 ipst = recv_ill->ill_ipst; 12918 12919 #define rptr ((uchar_t *)ipha) 12920 12921 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12922 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 12923 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12924 ASSERT(ill != NULL); 12925 12926 /* 12927 * FAST PATH for udp packets 12928 */ 12929 12930 /* u1 is # words of IP options */ 12931 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 12932 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12933 12934 /* IP options present */ 12935 if (u1 != 0) 12936 goto ipoptions; 12937 12938 /* Check the IP header checksum. */ 12939 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12940 /* Clear the IP header h/w cksum flag */ 12941 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12942 } else { 12943 #define uph ((uint16_t *)ipha) 12944 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 12945 uph[6] + uph[7] + uph[8] + uph[9]; 12946 #undef uph 12947 /* finish doing IP checksum */ 12948 sum = (sum & 0xFFFF) + (sum >> 16); 12949 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12950 /* 12951 * Don't verify header checksum if this packet is coming 12952 * back from AH/ESP as we already did it. 12953 */ 12954 if (!mctl_present && sum != 0 && sum != 0xFFFF) { 12955 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12956 freemsg(first_mp); 12957 return; 12958 } 12959 } 12960 12961 /* 12962 * Count for SNMP of inbound packets for ire. 12963 * if mctl is present this might be a secure packet and 12964 * has already been counted for in ip_proto_input(). 12965 */ 12966 if (!mctl_present) { 12967 UPDATE_IB_PKT_COUNT(ire); 12968 ire->ire_last_used_time = lbolt; 12969 } 12970 12971 /* packet part of fragmented IP packet? */ 12972 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12973 if (u1 & (IPH_MF | IPH_OFFSET)) { 12974 goto fragmented; 12975 } 12976 12977 /* u1 = IP header length (20 bytes) */ 12978 u1 = IP_SIMPLE_HDR_LENGTH; 12979 12980 /* packet does not contain complete IP & UDP headers */ 12981 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 12982 goto udppullup; 12983 12984 /* up points to UDP header */ 12985 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 12986 #define iphs ((uint16_t *)ipha) 12987 12988 /* if udp hdr cksum != 0, then need to checksum udp packet */ 12989 if (up[3] != 0) { 12990 mblk_t *mp1 = mp->b_cont; 12991 boolean_t cksum_err; 12992 uint16_t hck_flags = 0; 12993 12994 /* Pseudo-header checksum */ 12995 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12996 iphs[9] + up[2]; 12997 12998 /* 12999 * Revert to software checksum calculation if the interface 13000 * isn't capable of checksum offload or if IPsec is present. 13001 */ 13002 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 13003 hck_flags = DB_CKSUMFLAGS(mp); 13004 13005 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13006 IP_STAT(ipst, ip_in_sw_cksum); 13007 13008 IP_CKSUM_RECV(hck_flags, u1, 13009 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13010 (int32_t)((uchar_t *)up - rptr), 13011 mp, mp1, cksum_err); 13012 13013 if (cksum_err) { 13014 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 13015 if (hck_flags & HCK_FULLCKSUM) 13016 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 13017 else if (hck_flags & HCK_PARTIALCKSUM) 13018 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 13019 else 13020 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 13021 13022 freemsg(first_mp); 13023 return; 13024 } 13025 } 13026 13027 /* Non-fragmented broadcast or multicast packet? */ 13028 if (ire->ire_type == IRE_BROADCAST) 13029 goto udpslowpath; 13030 13031 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 13032 ire->ire_zoneid, ipst)) != NULL) { 13033 ASSERT(connp->conn_upq != NULL); 13034 IP_STAT(ipst, ip_udp_fast_path); 13035 13036 if (CONN_UDP_FLOWCTLD(connp)) { 13037 freemsg(mp); 13038 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 13039 } else { 13040 if (!mctl_present) { 13041 BUMP_MIB(ill->ill_ip_mib, 13042 ipIfStatsHCInDelivers); 13043 } 13044 /* 13045 * mp and first_mp can change. 13046 */ 13047 if (ip_udp_check(q, connp, recv_ill, 13048 ipha, &mp, &first_mp, mctl_present)) { 13049 /* Send it upstream */ 13050 CONN_UDP_RECV(connp, mp); 13051 } 13052 } 13053 /* 13054 * freeb() cannot deal with null mblk being passed 13055 * in and first_mp can be set to null in the call 13056 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 13057 */ 13058 if (mctl_present && first_mp != NULL) { 13059 freeb(first_mp); 13060 } 13061 CONN_DEC_REF(connp); 13062 return; 13063 } 13064 13065 /* 13066 * if we got here we know the packet is not fragmented and 13067 * has no options. The classifier could not find a conn_t and 13068 * most likely its an icmp packet so send it through slow path. 13069 */ 13070 13071 goto udpslowpath; 13072 13073 ipoptions: 13074 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 13075 goto slow_done; 13076 } 13077 13078 UPDATE_IB_PKT_COUNT(ire); 13079 ire->ire_last_used_time = lbolt; 13080 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13081 if (u1 & (IPH_MF | IPH_OFFSET)) { 13082 fragmented: 13083 /* 13084 * "sum" and "reass_hck_flags" are non-zero if the 13085 * reassembled packet has a valid hardware computed 13086 * checksum information associated with it. 13087 */ 13088 if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) 13089 goto slow_done; 13090 /* 13091 * Make sure that first_mp points back to mp as 13092 * the mp we came in with could have changed in 13093 * ip_rput_fragment(). 13094 */ 13095 ASSERT(!mctl_present); 13096 ipha = (ipha_t *)mp->b_rptr; 13097 first_mp = mp; 13098 } 13099 13100 /* Now we have a complete datagram, destined for this machine. */ 13101 u1 = IPH_HDR_LENGTH(ipha); 13102 /* Pull up the UDP header, if necessary. */ 13103 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 13104 udppullup: 13105 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 13106 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13107 freemsg(first_mp); 13108 goto slow_done; 13109 } 13110 ipha = (ipha_t *)mp->b_rptr; 13111 } 13112 13113 /* 13114 * Validate the checksum for the reassembled packet; for the 13115 * pullup case we calculate the payload checksum in software. 13116 */ 13117 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 13118 if (up[3] != 0) { 13119 boolean_t cksum_err; 13120 13121 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13122 IP_STAT(ipst, ip_in_sw_cksum); 13123 13124 IP_CKSUM_RECV_REASS(reass_hck_flags, 13125 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 13126 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 13127 iphs[9] + up[2], sum, cksum_err); 13128 13129 if (cksum_err) { 13130 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 13131 13132 if (reass_hck_flags & HCK_FULLCKSUM) 13133 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 13134 else if (reass_hck_flags & HCK_PARTIALCKSUM) 13135 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 13136 else 13137 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 13138 13139 freemsg(first_mp); 13140 goto slow_done; 13141 } 13142 } 13143 udpslowpath: 13144 13145 /* Clear hardware checksum flag to be safe */ 13146 DB_CKSUMFLAGS(mp) = 0; 13147 13148 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 13149 (ire->ire_type == IRE_BROADCAST), 13150 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IPINFO, 13151 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 13152 13153 slow_done: 13154 IP_STAT(ipst, ip_udp_slow_path); 13155 return; 13156 13157 #undef iphs 13158 #undef rptr 13159 } 13160 13161 /* ARGSUSED */ 13162 static mblk_t * 13163 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13164 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 13165 ill_rx_ring_t *ill_ring) 13166 { 13167 conn_t *connp; 13168 uint32_t sum; 13169 uint32_t u1; 13170 uint16_t *up; 13171 int offset; 13172 ssize_t len; 13173 mblk_t *mp1; 13174 boolean_t syn_present = B_FALSE; 13175 tcph_t *tcph; 13176 uint_t ip_hdr_len; 13177 ill_t *ill = (ill_t *)q->q_ptr; 13178 zoneid_t zoneid = ire->ire_zoneid; 13179 boolean_t cksum_err; 13180 uint16_t hck_flags = 0; 13181 ip_stack_t *ipst = recv_ill->ill_ipst; 13182 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 13183 13184 #define rptr ((uchar_t *)ipha) 13185 13186 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 13187 ASSERT(ill != NULL); 13188 13189 /* 13190 * FAST PATH for tcp packets 13191 */ 13192 13193 /* u1 is # words of IP options */ 13194 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13195 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13196 13197 /* IP options present */ 13198 if (u1) { 13199 goto ipoptions; 13200 } else { 13201 /* Check the IP header checksum. */ 13202 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 13203 /* Clear the IP header h/w cksum flag */ 13204 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 13205 } else { 13206 #define uph ((uint16_t *)ipha) 13207 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13208 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13209 #undef uph 13210 /* finish doing IP checksum */ 13211 sum = (sum & 0xFFFF) + (sum >> 16); 13212 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13213 /* 13214 * Don't verify header checksum if this packet 13215 * is coming back from AH/ESP as we already did it. 13216 */ 13217 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 13218 BUMP_MIB(ill->ill_ip_mib, 13219 ipIfStatsInCksumErrs); 13220 goto error; 13221 } 13222 } 13223 } 13224 13225 if (!mctl_present) { 13226 UPDATE_IB_PKT_COUNT(ire); 13227 ire->ire_last_used_time = lbolt; 13228 } 13229 13230 /* packet part of fragmented IP packet? */ 13231 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13232 if (u1 & (IPH_MF | IPH_OFFSET)) { 13233 goto fragmented; 13234 } 13235 13236 /* u1 = IP header length (20 bytes) */ 13237 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 13238 13239 /* does packet contain IP+TCP headers? */ 13240 len = mp->b_wptr - rptr; 13241 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 13242 IP_STAT(ipst, ip_tcppullup); 13243 goto tcppullup; 13244 } 13245 13246 /* TCP options present? */ 13247 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 13248 13249 /* 13250 * If options need to be pulled up, then goto tcpoptions. 13251 * otherwise we are still in the fast path 13252 */ 13253 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 13254 IP_STAT(ipst, ip_tcpoptions); 13255 goto tcpoptions; 13256 } 13257 13258 /* multiple mblks of tcp data? */ 13259 if ((mp1 = mp->b_cont) != NULL) { 13260 /* more then two? */ 13261 if (mp1->b_cont != NULL) { 13262 IP_STAT(ipst, ip_multipkttcp); 13263 goto multipkttcp; 13264 } 13265 len += mp1->b_wptr - mp1->b_rptr; 13266 } 13267 13268 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 13269 13270 /* part of pseudo checksum */ 13271 13272 /* TCP datagram length */ 13273 u1 = len - IP_SIMPLE_HDR_LENGTH; 13274 13275 #define iphs ((uint16_t *)ipha) 13276 13277 #ifdef _BIG_ENDIAN 13278 u1 += IPPROTO_TCP; 13279 #else 13280 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13281 #endif 13282 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13283 13284 /* 13285 * Revert to software checksum calculation if the interface 13286 * isn't capable of checksum offload or if IPsec is present. 13287 */ 13288 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 13289 hck_flags = DB_CKSUMFLAGS(mp); 13290 13291 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13292 IP_STAT(ipst, ip_in_sw_cksum); 13293 13294 IP_CKSUM_RECV(hck_flags, u1, 13295 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13296 (int32_t)((uchar_t *)up - rptr), 13297 mp, mp1, cksum_err); 13298 13299 if (cksum_err) { 13300 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13301 13302 if (hck_flags & HCK_FULLCKSUM) 13303 IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err); 13304 else if (hck_flags & HCK_PARTIALCKSUM) 13305 IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err); 13306 else 13307 IP_STAT(ipst, ip_tcp_in_sw_cksum_err); 13308 13309 goto error; 13310 } 13311 13312 try_again: 13313 13314 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 13315 zoneid, ipst)) == NULL) { 13316 /* Send the TH_RST */ 13317 goto no_conn; 13318 } 13319 13320 /* 13321 * TCP FAST PATH for AF_INET socket. 13322 * 13323 * TCP fast path to avoid extra work. An AF_INET socket type 13324 * does not have facility to receive extra information via 13325 * ip_process or ip_add_info. Also, when the connection was 13326 * established, we made a check if this connection is impacted 13327 * by any global IPSec policy or per connection policy (a 13328 * policy that comes in effect later will not apply to this 13329 * connection). Since all this can be determined at the 13330 * connection establishment time, a quick check of flags 13331 * can avoid extra work. 13332 */ 13333 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 13334 !IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13335 ASSERT(first_mp == mp); 13336 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13337 SET_SQUEUE(mp, tcp_rput_data, connp); 13338 return (mp); 13339 } 13340 13341 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 13342 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 13343 if (IPCL_IS_TCP(connp)) { 13344 mp->b_datap->db_struioflag |= STRUIO_EAGER; 13345 DB_CKSUMSTART(mp) = 13346 (intptr_t)ip_squeue_get(ill_ring); 13347 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 13348 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13349 BUMP_MIB(ill->ill_ip_mib, 13350 ipIfStatsHCInDelivers); 13351 SET_SQUEUE(mp, connp->conn_recv, connp); 13352 return (mp); 13353 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 13354 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13355 BUMP_MIB(ill->ill_ip_mib, 13356 ipIfStatsHCInDelivers); 13357 ip_squeue_enter_unbound++; 13358 SET_SQUEUE(mp, tcp_conn_request_unbound, 13359 connp); 13360 return (mp); 13361 } 13362 syn_present = B_TRUE; 13363 } 13364 13365 } 13366 13367 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 13368 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13369 13370 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13371 /* No need to send this packet to TCP */ 13372 if ((flags & TH_RST) || (flags & TH_URG)) { 13373 CONN_DEC_REF(connp); 13374 freemsg(first_mp); 13375 return (NULL); 13376 } 13377 if (flags & TH_ACK) { 13378 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 13379 ipst->ips_netstack->netstack_tcp); 13380 CONN_DEC_REF(connp); 13381 return (NULL); 13382 } 13383 13384 CONN_DEC_REF(connp); 13385 freemsg(first_mp); 13386 return (NULL); 13387 } 13388 13389 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 13390 first_mp = ipsec_check_inbound_policy(first_mp, connp, 13391 ipha, NULL, mctl_present); 13392 if (first_mp == NULL) { 13393 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13394 CONN_DEC_REF(connp); 13395 return (NULL); 13396 } 13397 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 13398 ASSERT(syn_present); 13399 if (mctl_present) { 13400 ASSERT(first_mp != mp); 13401 first_mp->b_datap->db_struioflag |= 13402 STRUIO_POLICY; 13403 } else { 13404 ASSERT(first_mp == mp); 13405 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 13406 mp->b_datap->db_struioflag |= STRUIO_POLICY; 13407 } 13408 } else { 13409 /* 13410 * Discard first_mp early since we're dealing with a 13411 * fully-connected conn_t and tcp doesn't do policy in 13412 * this case. 13413 */ 13414 if (mctl_present) { 13415 freeb(first_mp); 13416 mctl_present = B_FALSE; 13417 } 13418 first_mp = mp; 13419 } 13420 } 13421 13422 /* Initiate IPPF processing for fastpath */ 13423 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13424 uint32_t ill_index; 13425 13426 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13427 ip_process(IPP_LOCAL_IN, &mp, ill_index); 13428 if (mp == NULL) { 13429 ip2dbg(("ip_input_ipsec_process: TCP pkt " 13430 "deferred/dropped during IPPF processing\n")); 13431 CONN_DEC_REF(connp); 13432 if (mctl_present) 13433 freeb(first_mp); 13434 return (NULL); 13435 } else if (mctl_present) { 13436 /* 13437 * ip_process might return a new mp. 13438 */ 13439 ASSERT(first_mp != mp); 13440 first_mp->b_cont = mp; 13441 } else { 13442 first_mp = mp; 13443 } 13444 13445 } 13446 13447 if (!syn_present && connp->conn_ip_recvpktinfo) { 13448 /* 13449 * TCP does not support IP_RECVPKTINFO for v4 so lets 13450 * make sure IPF_RECVIF is passed to ip_add_info. 13451 */ 13452 mp = ip_add_info(mp, recv_ill, flags|IPF_RECVIF, 13453 IPCL_ZONEID(connp), ipst); 13454 if (mp == NULL) { 13455 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13456 CONN_DEC_REF(connp); 13457 if (mctl_present) 13458 freeb(first_mp); 13459 return (NULL); 13460 } else if (mctl_present) { 13461 /* 13462 * ip_add_info might return a new mp. 13463 */ 13464 ASSERT(first_mp != mp); 13465 first_mp->b_cont = mp; 13466 } else { 13467 first_mp = mp; 13468 } 13469 } 13470 13471 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13472 if (IPCL_IS_TCP(connp)) { 13473 SET_SQUEUE(first_mp, connp->conn_recv, connp); 13474 return (first_mp); 13475 } else { 13476 putnext(connp->conn_rq, first_mp); 13477 CONN_DEC_REF(connp); 13478 return (NULL); 13479 } 13480 13481 no_conn: 13482 /* Initiate IPPf processing, if needed. */ 13483 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13484 uint32_t ill_index; 13485 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13486 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 13487 if (first_mp == NULL) { 13488 return (NULL); 13489 } 13490 } 13491 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13492 13493 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid, 13494 ipst->ips_netstack->netstack_tcp); 13495 return (NULL); 13496 ipoptions: 13497 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) { 13498 goto slow_done; 13499 } 13500 13501 UPDATE_IB_PKT_COUNT(ire); 13502 ire->ire_last_used_time = lbolt; 13503 13504 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13505 if (u1 & (IPH_MF | IPH_OFFSET)) { 13506 fragmented: 13507 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 13508 if (mctl_present) 13509 freeb(first_mp); 13510 goto slow_done; 13511 } 13512 /* 13513 * Make sure that first_mp points back to mp as 13514 * the mp we came in with could have changed in 13515 * ip_rput_fragment(). 13516 */ 13517 ASSERT(!mctl_present); 13518 ipha = (ipha_t *)mp->b_rptr; 13519 first_mp = mp; 13520 } 13521 13522 /* Now we have a complete datagram, destined for this machine. */ 13523 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 13524 13525 len = mp->b_wptr - mp->b_rptr; 13526 /* Pull up a minimal TCP header, if necessary. */ 13527 if (len < (u1 + 20)) { 13528 tcppullup: 13529 if (!pullupmsg(mp, u1 + 20)) { 13530 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13531 goto error; 13532 } 13533 ipha = (ipha_t *)mp->b_rptr; 13534 len = mp->b_wptr - mp->b_rptr; 13535 } 13536 13537 /* 13538 * Extract the offset field from the TCP header. As usual, we 13539 * try to help the compiler more than the reader. 13540 */ 13541 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 13542 if (offset != 5) { 13543 tcpoptions: 13544 if (offset < 5) { 13545 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13546 goto error; 13547 } 13548 /* 13549 * There must be TCP options. 13550 * Make sure we can grab them. 13551 */ 13552 offset <<= 2; 13553 offset += u1; 13554 if (len < offset) { 13555 if (!pullupmsg(mp, offset)) { 13556 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13557 goto error; 13558 } 13559 ipha = (ipha_t *)mp->b_rptr; 13560 len = mp->b_wptr - rptr; 13561 } 13562 } 13563 13564 /* Get the total packet length in len, including headers. */ 13565 if (mp->b_cont) { 13566 multipkttcp: 13567 len = msgdsize(mp); 13568 } 13569 13570 /* 13571 * Check the TCP checksum by pulling together the pseudo- 13572 * header checksum, and passing it to ip_csum to be added in 13573 * with the TCP datagram. 13574 * 13575 * Since we are not using the hwcksum if available we must 13576 * clear the flag. We may come here via tcppullup or tcpoptions. 13577 * If either of these fails along the way the mblk is freed. 13578 * If this logic ever changes and mblk is reused to say send 13579 * ICMP's back, then this flag may need to be cleared in 13580 * other places as well. 13581 */ 13582 DB_CKSUMFLAGS(mp) = 0; 13583 13584 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 13585 13586 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 13587 #ifdef _BIG_ENDIAN 13588 u1 += IPPROTO_TCP; 13589 #else 13590 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13591 #endif 13592 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13593 /* 13594 * Not M_DATA mblk or its a dup, so do the checksum now. 13595 */ 13596 IP_STAT(ipst, ip_in_sw_cksum); 13597 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 13598 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13599 goto error; 13600 } 13601 13602 IP_STAT(ipst, ip_tcp_slow_path); 13603 goto try_again; 13604 #undef iphs 13605 #undef rptr 13606 13607 error: 13608 freemsg(first_mp); 13609 slow_done: 13610 return (NULL); 13611 } 13612 13613 /* ARGSUSED */ 13614 static void 13615 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13616 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 13617 { 13618 conn_t *connp; 13619 uint32_t sum; 13620 uint32_t u1; 13621 ssize_t len; 13622 sctp_hdr_t *sctph; 13623 zoneid_t zoneid = ire->ire_zoneid; 13624 uint32_t pktsum; 13625 uint32_t calcsum; 13626 uint32_t ports; 13627 in6_addr_t map_src, map_dst; 13628 ill_t *ill = (ill_t *)q->q_ptr; 13629 ip_stack_t *ipst; 13630 sctp_stack_t *sctps; 13631 13632 ASSERT(recv_ill != NULL); 13633 ipst = recv_ill->ill_ipst; 13634 sctps = ipst->ips_netstack->netstack_sctp; 13635 13636 #define rptr ((uchar_t *)ipha) 13637 13638 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 13639 ASSERT(ill != NULL); 13640 13641 /* u1 is # words of IP options */ 13642 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13643 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13644 13645 /* IP options present */ 13646 if (u1 > 0) { 13647 goto ipoptions; 13648 } else { 13649 /* Check the IP header checksum. */ 13650 if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 13651 #define uph ((uint16_t *)ipha) 13652 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13653 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13654 #undef uph 13655 /* finish doing IP checksum */ 13656 sum = (sum & 0xFFFF) + (sum >> 16); 13657 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13658 /* 13659 * Don't verify header checksum if this packet 13660 * is coming back from AH/ESP as we already did it. 13661 */ 13662 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 13663 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 13664 goto error; 13665 } 13666 } 13667 /* 13668 * Since there is no SCTP h/w cksum support yet, just 13669 * clear the flag. 13670 */ 13671 DB_CKSUMFLAGS(mp) = 0; 13672 } 13673 13674 /* 13675 * Don't verify header checksum if this packet is coming 13676 * back from AH/ESP as we already did it. 13677 */ 13678 if (!mctl_present) { 13679 UPDATE_IB_PKT_COUNT(ire); 13680 ire->ire_last_used_time = lbolt; 13681 } 13682 13683 /* packet part of fragmented IP packet? */ 13684 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13685 if (u1 & (IPH_MF | IPH_OFFSET)) 13686 goto fragmented; 13687 13688 /* u1 = IP header length (20 bytes) */ 13689 u1 = IP_SIMPLE_HDR_LENGTH; 13690 13691 find_sctp_client: 13692 /* Pullup if we don't have the sctp common header. */ 13693 len = MBLKL(mp); 13694 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 13695 if (mp->b_cont == NULL || 13696 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 13697 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13698 goto error; 13699 } 13700 ipha = (ipha_t *)mp->b_rptr; 13701 len = MBLKL(mp); 13702 } 13703 13704 sctph = (sctp_hdr_t *)(rptr + u1); 13705 #ifdef DEBUG 13706 if (!skip_sctp_cksum) { 13707 #endif 13708 pktsum = sctph->sh_chksum; 13709 sctph->sh_chksum = 0; 13710 calcsum = sctp_cksum(mp, u1); 13711 if (calcsum != pktsum) { 13712 BUMP_MIB(&sctps->sctps_mib, sctpChecksumError); 13713 goto error; 13714 } 13715 sctph->sh_chksum = pktsum; 13716 #ifdef DEBUG /* skip_sctp_cksum */ 13717 } 13718 #endif 13719 /* get the ports */ 13720 ports = *(uint32_t *)&sctph->sh_sport; 13721 13722 IRE_REFRELE(ire); 13723 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 13724 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 13725 if ((connp = sctp_fanout(&map_src, &map_dst, ports, zoneid, mp, 13726 sctps)) == NULL) { 13727 /* Check for raw socket or OOTB handling */ 13728 goto no_conn; 13729 } 13730 13731 /* Found a client; up it goes */ 13732 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13733 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 13734 return; 13735 13736 no_conn: 13737 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 13738 ports, mctl_present, flags, B_TRUE, zoneid); 13739 return; 13740 13741 ipoptions: 13742 DB_CKSUMFLAGS(mp) = 0; 13743 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) 13744 goto slow_done; 13745 13746 UPDATE_IB_PKT_COUNT(ire); 13747 ire->ire_last_used_time = lbolt; 13748 13749 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13750 if (u1 & (IPH_MF | IPH_OFFSET)) { 13751 fragmented: 13752 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) 13753 goto slow_done; 13754 /* 13755 * Make sure that first_mp points back to mp as 13756 * the mp we came in with could have changed in 13757 * ip_rput_fragment(). 13758 */ 13759 ASSERT(!mctl_present); 13760 ipha = (ipha_t *)mp->b_rptr; 13761 first_mp = mp; 13762 } 13763 13764 /* Now we have a complete datagram, destined for this machine. */ 13765 u1 = IPH_HDR_LENGTH(ipha); 13766 goto find_sctp_client; 13767 #undef iphs 13768 #undef rptr 13769 13770 error: 13771 freemsg(first_mp); 13772 slow_done: 13773 IRE_REFRELE(ire); 13774 } 13775 13776 #define VER_BITS 0xF0 13777 #define VERSION_6 0x60 13778 13779 static boolean_t 13780 ip_rput_multimblk_ipoptions(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t **iphapp, 13781 ipaddr_t *dstp, ip_stack_t *ipst) 13782 { 13783 uint_t opt_len; 13784 ipha_t *ipha; 13785 ssize_t len; 13786 uint_t pkt_len; 13787 13788 ASSERT(ill != NULL); 13789 IP_STAT(ipst, ip_ipoptions); 13790 ipha = *iphapp; 13791 13792 #define rptr ((uchar_t *)ipha) 13793 /* Assume no IPv6 packets arrive over the IPv4 queue */ 13794 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 13795 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); 13796 freemsg(mp); 13797 return (B_FALSE); 13798 } 13799 13800 /* multiple mblk or too short */ 13801 pkt_len = ntohs(ipha->ipha_length); 13802 13803 /* Get the number of words of IP options in the IP header. */ 13804 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 13805 if (opt_len) { 13806 /* IP Options present! Validate and process. */ 13807 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 13808 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13809 goto done; 13810 } 13811 /* 13812 * Recompute complete header length and make sure we 13813 * have access to all of it. 13814 */ 13815 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 13816 if (len > (mp->b_wptr - rptr)) { 13817 if (len > pkt_len) { 13818 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13819 goto done; 13820 } 13821 if (!pullupmsg(mp, len)) { 13822 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13823 goto done; 13824 } 13825 ipha = (ipha_t *)mp->b_rptr; 13826 } 13827 /* 13828 * Go off to ip_rput_options which returns the next hop 13829 * destination address, which may have been affected 13830 * by source routing. 13831 */ 13832 IP_STAT(ipst, ip_opt); 13833 if (ip_rput_options(q, mp, ipha, dstp, ipst) == -1) { 13834 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13835 return (B_FALSE); 13836 } 13837 } 13838 *iphapp = ipha; 13839 return (B_TRUE); 13840 done: 13841 /* clear b_prev - used by ip_mroute_decap */ 13842 mp->b_prev = NULL; 13843 freemsg(mp); 13844 return (B_FALSE); 13845 #undef rptr 13846 } 13847 13848 /* 13849 * Deal with the fact that there is no ire for the destination. 13850 * The incoming ill (in_ill) is passed in to ip_newroute only 13851 * in the case of packets coming from mobile ip forward tunnel. 13852 * It must be null otherwise. 13853 */ 13854 static ire_t * 13855 ip_rput_noire(queue_t *q, ill_t *in_ill, mblk_t *mp, int ll_multicast, 13856 ipaddr_t dst) 13857 { 13858 ipha_t *ipha; 13859 ill_t *ill; 13860 ire_t *ire; 13861 boolean_t check_multirt = B_FALSE; 13862 ip_stack_t *ipst; 13863 13864 ipha = (ipha_t *)mp->b_rptr; 13865 ill = (ill_t *)q->q_ptr; 13866 13867 ASSERT(ill != NULL); 13868 ipst = ill->ill_ipst; 13869 13870 /* 13871 * No IRE for this destination, so it can't be for us. 13872 * Unless we are forwarding, drop the packet. 13873 * We have to let source routed packets through 13874 * since we don't yet know if they are 'ping -l' 13875 * packets i.e. if they will go out over the 13876 * same interface as they came in on. 13877 */ 13878 if (ll_multicast) { 13879 freemsg(mp); 13880 return (NULL); 13881 } 13882 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) { 13883 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13884 freemsg(mp); 13885 return (NULL); 13886 } 13887 13888 /* 13889 * Mark this packet as having originated externally. 13890 * 13891 * For non-forwarding code path, ire_send later double 13892 * checks this interface to see if it is still exists 13893 * post-ARP resolution. 13894 * 13895 * Also, IPQOS uses this to differentiate between 13896 * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP 13897 * QOS packet processing in ip_wput_attach_llhdr(). 13898 * The QoS module can mark the b_band for a fastpath message 13899 * or the dl_priority field in a unitdata_req header for 13900 * CoS marking. This info can only be found in 13901 * ip_wput_attach_llhdr(). 13902 */ 13903 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 13904 /* 13905 * Clear the indication that this may have a hardware checksum 13906 * as we are not using it 13907 */ 13908 DB_CKSUMFLAGS(mp) = 0; 13909 13910 if (in_ill != NULL) { 13911 /* 13912 * Now hand the packet to ip_newroute. 13913 */ 13914 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID, ipst); 13915 return (NULL); 13916 } 13917 ire = ire_forward(dst, &check_multirt, NULL, NULL, 13918 MBLK_GETLABEL(mp), ipst); 13919 13920 if (ire == NULL && check_multirt) { 13921 /* Let ip_newroute handle CGTP */ 13922 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID, ipst); 13923 return (NULL); 13924 } 13925 13926 if (ire != NULL) 13927 return (ire); 13928 13929 mp->b_prev = mp->b_next = 0; 13930 /* send icmp unreachable */ 13931 q = WR(q); 13932 /* Sent by forwarding path, and router is global zone */ 13933 if (ip_source_routed(ipha, ipst)) { 13934 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, 13935 GLOBAL_ZONEID, ipst); 13936 } else { 13937 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 13938 ipst); 13939 } 13940 13941 return (NULL); 13942 13943 } 13944 13945 /* 13946 * check ip header length and align it. 13947 */ 13948 static boolean_t 13949 ip_check_and_align_header(queue_t *q, mblk_t *mp, ip_stack_t *ipst) 13950 { 13951 ssize_t len; 13952 ill_t *ill; 13953 ipha_t *ipha; 13954 13955 len = MBLKL(mp); 13956 13957 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13958 ill = (ill_t *)q->q_ptr; 13959 13960 if (!OK_32PTR(mp->b_rptr)) 13961 IP_STAT(ipst, ip_notaligned1); 13962 else 13963 IP_STAT(ipst, ip_notaligned2); 13964 /* Guard against bogus device drivers */ 13965 if (len < 0) { 13966 /* clear b_prev - used by ip_mroute_decap */ 13967 mp->b_prev = NULL; 13968 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13969 freemsg(mp); 13970 return (B_FALSE); 13971 } 13972 13973 if (ip_rput_pullups++ == 0) { 13974 ipha = (ipha_t *)mp->b_rptr; 13975 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 13976 "ip_check_and_align_header: %s forced us to " 13977 " pullup pkt, hdr len %ld, hdr addr %p", 13978 ill->ill_name, len, ipha); 13979 } 13980 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 13981 /* clear b_prev - used by ip_mroute_decap */ 13982 mp->b_prev = NULL; 13983 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13984 freemsg(mp); 13985 return (B_FALSE); 13986 } 13987 } 13988 return (B_TRUE); 13989 } 13990 13991 static boolean_t 13992 ip_rput_notforus(queue_t **qp, mblk_t *mp, ire_t *ire, ill_t *ill) 13993 { 13994 ill_group_t *ill_group; 13995 ill_group_t *ire_group; 13996 queue_t *q; 13997 ill_t *ire_ill; 13998 uint_t ill_ifindex; 13999 ip_stack_t *ipst = ill->ill_ipst; 14000 14001 q = *qp; 14002 /* 14003 * We need to check to make sure the packet came in 14004 * on the queue associated with the destination IRE. 14005 * Note that for multicast packets and broadcast packets sent to 14006 * a broadcast address which is shared between multiple interfaces 14007 * we should not do this since we just got a random broadcast ire. 14008 */ 14009 if (ire->ire_rfq && ire->ire_type != IRE_BROADCAST) { 14010 boolean_t check_multi = B_TRUE; 14011 14012 /* 14013 * This packet came in on an interface other than the 14014 * one associated with the destination address. 14015 * "Gateway" it to the appropriate interface here. 14016 * As long as the ills belong to the same group, 14017 * we don't consider them to arriving on the wrong 14018 * interface. Thus, when the switch is doing inbound 14019 * load spreading, we won't drop packets when we 14020 * are doing strict multihoming checks. Note, the 14021 * same holds true for 'usesrc groups' where the 14022 * destination address may belong to another interface 14023 * to allow multipathing to happen 14024 */ 14025 ill_group = ill->ill_group; 14026 ire_ill = (ill_t *)(ire->ire_rfq)->q_ptr; 14027 ill_ifindex = ill->ill_usesrc_ifindex; 14028 ire_group = ire_ill->ill_group; 14029 14030 /* 14031 * If it's part of the same IPMP group, or if it's a legal 14032 * address on the 'usesrc' interface, then bypass strict 14033 * checks. 14034 */ 14035 if (ill_group != NULL && ill_group == ire_group) { 14036 check_multi = B_FALSE; 14037 } else if (ill_ifindex != 0 && 14038 ill_ifindex == ire_ill->ill_phyint->phyint_ifindex) { 14039 check_multi = B_FALSE; 14040 } 14041 14042 if (check_multi && 14043 ipst->ips_ip_strict_dst_multihoming && 14044 ((ill->ill_flags & 14045 ire->ire_ipif->ipif_ill->ill_flags & 14046 ILLF_ROUTER) == 0)) { 14047 /* Drop packet */ 14048 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14049 freemsg(mp); 14050 return (B_TRUE); 14051 } 14052 14053 /* 14054 * Change the queue (for non-virtual destination network 14055 * interfaces) and ip_rput_local will be called with the right 14056 * queue 14057 */ 14058 q = ire->ire_rfq; 14059 } 14060 /* Must be broadcast. We'll take it. */ 14061 *qp = q; 14062 return (B_FALSE); 14063 } 14064 14065 ire_t * 14066 ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) 14067 { 14068 ipha_t *ipha; 14069 ipaddr_t ip_dst, ip_src; 14070 ire_t *src_ire = NULL; 14071 ill_t *stq_ill; 14072 uint_t hlen; 14073 uint_t pkt_len; 14074 uint32_t sum; 14075 queue_t *dev_q; 14076 boolean_t check_multirt = B_FALSE; 14077 ip_stack_t *ipst = ill->ill_ipst; 14078 14079 ipha = (ipha_t *)mp->b_rptr; 14080 14081 /* 14082 * Martian Address Filtering [RFC 1812, Section 5.3.7] 14083 * The loopback address check for both src and dst has already 14084 * been checked in ip_input 14085 */ 14086 ip_dst = ntohl(dst); 14087 ip_src = ntohl(ipha->ipha_src); 14088 14089 if (ip_dst == INADDR_ANY || IN_BADCLASS(ip_dst) || 14090 IN_CLASSD(ip_src)) { 14091 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14092 goto drop; 14093 } 14094 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14095 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 14096 14097 if (src_ire != NULL) { 14098 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14099 goto drop; 14100 } 14101 14102 14103 /* No ire cache of nexthop. So first create one */ 14104 if (ire == NULL) { 14105 ire = ire_forward(dst, &check_multirt, NULL, NULL, NULL, ipst); 14106 /* 14107 * We only come to ip_fast_forward if ip_cgtp_filter is 14108 * is not set. So upon return from ire_forward 14109 * check_multirt should remain as false. 14110 */ 14111 ASSERT(!check_multirt); 14112 if (ire == NULL) { 14113 /* An attempt was made to forward the packet */ 14114 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14115 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14116 mp->b_prev = mp->b_next = 0; 14117 /* send icmp unreachable */ 14118 /* Sent by forwarding path, and router is global zone */ 14119 if (ip_source_routed(ipha, ipst)) { 14120 icmp_unreachable(ill->ill_wq, mp, 14121 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, 14122 ipst); 14123 } else { 14124 icmp_unreachable(ill->ill_wq, mp, 14125 ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 14126 ipst); 14127 } 14128 return (ire); 14129 } 14130 } 14131 14132 /* 14133 * Forwarding fastpath exception case: 14134 * If either of the follwoing case is true, we take 14135 * the slowpath 14136 * o forwarding is not enabled 14137 * o incoming and outgoing interface are the same, or the same 14138 * IPMP group 14139 * o corresponding ire is in incomplete state 14140 * o packet needs fragmentation 14141 * 14142 * The codeflow from here on is thus: 14143 * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 14144 */ 14145 pkt_len = ntohs(ipha->ipha_length); 14146 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 14147 if (!(stq_ill->ill_flags & ILLF_ROUTER) || 14148 !(ill->ill_flags & ILLF_ROUTER) || 14149 (ill == stq_ill) || 14150 (ill->ill_group != NULL && ill->ill_group == stq_ill->ill_group) || 14151 (ire->ire_nce == NULL) || 14152 (ire->ire_nce->nce_state != ND_REACHABLE) || 14153 (pkt_len > ire->ire_max_frag) || 14154 ipha->ipha_ttl <= 1) { 14155 ip_rput_process_forward(ill->ill_rq, mp, ire, 14156 ipha, ill, B_FALSE); 14157 return (ire); 14158 } 14159 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14160 14161 DTRACE_PROBE4(ip4__forwarding__start, 14162 ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 14163 14164 FW_HOOKS(ipst->ips_ip4_forwarding_event, 14165 ipst->ips_ipv4firewall_forwarding, 14166 ill, stq_ill, ipha, mp, mp, ipst); 14167 14168 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 14169 14170 if (mp == NULL) 14171 goto drop; 14172 14173 mp->b_datap->db_struioun.cksum.flags = 0; 14174 /* Adjust the checksum to reflect the ttl decrement. */ 14175 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 14176 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 14177 ipha->ipha_ttl--; 14178 14179 dev_q = ire->ire_stq->q_next; 14180 if ((dev_q->q_next != NULL || 14181 dev_q->q_first != NULL) && !canput(dev_q)) { 14182 goto indiscard; 14183 } 14184 14185 hlen = ire->ire_nce->nce_fp_mp != NULL ? 14186 MBLKL(ire->ire_nce->nce_fp_mp) : 0; 14187 14188 if (hlen != 0 || ire->ire_nce->nce_res_mp != NULL) { 14189 mblk_t *mpip = mp; 14190 14191 mp = ip_wput_attach_llhdr(mpip, ire, 0, 0); 14192 if (mp != NULL) { 14193 DTRACE_PROBE4(ip4__physical__out__start, 14194 ill_t *, NULL, ill_t *, stq_ill, 14195 ipha_t *, ipha, mblk_t *, mp); 14196 FW_HOOKS(ipst->ips_ip4_physical_out_event, 14197 ipst->ips_ipv4firewall_physical_out, 14198 NULL, stq_ill, ipha, mp, mpip, ipst); 14199 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, 14200 mp); 14201 if (mp == NULL) 14202 goto drop; 14203 14204 UPDATE_IB_PKT_COUNT(ire); 14205 ire->ire_last_used_time = lbolt; 14206 BUMP_MIB(stq_ill->ill_ip_mib, 14207 ipIfStatsHCOutForwDatagrams); 14208 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 14209 UPDATE_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutOctets, 14210 pkt_len); 14211 putnext(ire->ire_stq, mp); 14212 return (ire); 14213 } 14214 } 14215 14216 indiscard: 14217 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14218 drop: 14219 if (mp != NULL) 14220 freemsg(mp); 14221 if (src_ire != NULL) 14222 ire_refrele(src_ire); 14223 return (ire); 14224 14225 } 14226 14227 /* 14228 * This function is called in the forwarding slowpath, when 14229 * either the ire lacks the link-layer address, or the packet needs 14230 * further processing(eg. fragmentation), before transmission. 14231 */ 14232 14233 static void 14234 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14235 ill_t *ill, boolean_t ll_multicast) 14236 { 14237 ill_group_t *ill_group; 14238 ill_group_t *ire_group; 14239 queue_t *dev_q; 14240 ire_t *src_ire; 14241 ip_stack_t *ipst = ill->ill_ipst; 14242 14243 ASSERT(ire->ire_stq != NULL); 14244 14245 mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ 14246 mp->b_next = NULL; /* ip_rput_noire sets dst here */ 14247 14248 if (ll_multicast != 0) { 14249 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14250 goto drop_pkt; 14251 } 14252 14253 /* 14254 * check if ipha_src is a broadcast address. Note that this 14255 * check is redundant when we get here from ip_fast_forward() 14256 * which has already done this check. However, since we can 14257 * also get here from ip_rput_process_broadcast() or, for 14258 * for the slow path through ip_fast_forward(), we perform 14259 * the check again for code-reusability 14260 */ 14261 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14262 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 14263 if (src_ire != NULL || ntohl(ipha->ipha_dst) == INADDR_ANY || 14264 IN_BADCLASS(ntohl(ipha->ipha_dst))) { 14265 if (src_ire != NULL) 14266 ire_refrele(src_ire); 14267 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14268 ip2dbg(("ip_rput_process_forward: Received packet with" 14269 " bad src/dst address on %s\n", ill->ill_name)); 14270 goto drop_pkt; 14271 } 14272 14273 ill_group = ill->ill_group; 14274 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 14275 /* 14276 * Check if we want to forward this one at this time. 14277 * We allow source routed packets on a host provided that 14278 * they go out the same interface or same interface group 14279 * as they came in on. 14280 * 14281 * XXX To be quicker, we may wish to not chase pointers to 14282 * get the ILLF_ROUTER flag and instead store the 14283 * forwarding policy in the ire. An unfortunate 14284 * side-effect of that would be requiring an ire flush 14285 * whenever the ILLF_ROUTER flag changes. 14286 */ 14287 if (((ill->ill_flags & 14288 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 14289 ILLF_ROUTER) == 0) && 14290 !(ip_source_routed(ipha, ipst) && (ire->ire_rfq == q || 14291 (ill_group != NULL && ill_group == ire_group)))) { 14292 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14293 if (ip_source_routed(ipha, ipst)) { 14294 q = WR(q); 14295 /* 14296 * Clear the indication that this may have 14297 * hardware checksum as we are not using it. 14298 */ 14299 DB_CKSUMFLAGS(mp) = 0; 14300 /* Sent by forwarding path, and router is global zone */ 14301 icmp_unreachable(q, mp, 14302 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); 14303 return; 14304 } 14305 goto drop_pkt; 14306 } 14307 14308 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14309 14310 /* Packet is being forwarded. Turning off hwcksum flag. */ 14311 DB_CKSUMFLAGS(mp) = 0; 14312 if (ipst->ips_ip_g_send_redirects) { 14313 /* 14314 * Check whether the incoming interface and outgoing 14315 * interface is part of the same group. If so, 14316 * send redirects. 14317 * 14318 * Check the source address to see if it originated 14319 * on the same logical subnet it is going back out on. 14320 * If so, we should be able to send it a redirect. 14321 * Avoid sending a redirect if the destination 14322 * is directly connected (i.e., ipha_dst is the same 14323 * as ire_gateway_addr or the ire_addr of the 14324 * nexthop IRE_CACHE ), or if the packet was source 14325 * routed out this interface. 14326 */ 14327 ipaddr_t src, nhop; 14328 mblk_t *mp1; 14329 ire_t *nhop_ire = NULL; 14330 14331 /* 14332 * Check whether ire_rfq and q are from the same ill 14333 * or if they are not same, they at least belong 14334 * to the same group. If so, send redirects. 14335 */ 14336 if ((ire->ire_rfq == q || 14337 (ill_group != NULL && ill_group == ire_group)) && 14338 !ip_source_routed(ipha, ipst)) { 14339 14340 nhop = (ire->ire_gateway_addr != 0 ? 14341 ire->ire_gateway_addr : ire->ire_addr); 14342 14343 if (ipha->ipha_dst == nhop) { 14344 /* 14345 * We avoid sending a redirect if the 14346 * destination is directly connected 14347 * because it is possible that multiple 14348 * IP subnets may have been configured on 14349 * the link, and the source may not 14350 * be on the same subnet as ip destination, 14351 * even though they are on the same 14352 * physical link. 14353 */ 14354 goto sendit; 14355 } 14356 14357 src = ipha->ipha_src; 14358 14359 /* 14360 * We look up the interface ire for the nexthop, 14361 * to see if ipha_src is in the same subnet 14362 * as the nexthop. 14363 * 14364 * Note that, if, in the future, IRE_CACHE entries 14365 * are obsoleted, this lookup will not be needed, 14366 * as the ire passed to this function will be the 14367 * same as the nhop_ire computed below. 14368 */ 14369 nhop_ire = ire_ftable_lookup(nhop, 0, 0, 14370 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 14371 0, NULL, MATCH_IRE_TYPE, ipst); 14372 14373 if (nhop_ire != NULL) { 14374 if ((src & nhop_ire->ire_mask) == 14375 (nhop & nhop_ire->ire_mask)) { 14376 /* 14377 * The source is directly connected. 14378 * Just copy the ip header (which is 14379 * in the first mblk) 14380 */ 14381 mp1 = copyb(mp); 14382 if (mp1 != NULL) { 14383 icmp_send_redirect(WR(q), mp1, 14384 nhop, ipst); 14385 } 14386 } 14387 ire_refrele(nhop_ire); 14388 } 14389 } 14390 } 14391 sendit: 14392 dev_q = ire->ire_stq->q_next; 14393 if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { 14394 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14395 freemsg(mp); 14396 return; 14397 } 14398 14399 ip_rput_forward(ire, ipha, mp, ill); 14400 return; 14401 14402 drop_pkt: 14403 ip2dbg(("ip_rput_process_forward: drop pkt\n")); 14404 freemsg(mp); 14405 } 14406 14407 ire_t * 14408 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14409 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 14410 { 14411 queue_t *q; 14412 uint16_t hcksumflags; 14413 ip_stack_t *ipst = ill->ill_ipst; 14414 14415 q = *qp; 14416 14417 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts); 14418 14419 /* 14420 * Clear the indication that this may have hardware 14421 * checksum as we are not using it for forwarding. 14422 */ 14423 hcksumflags = DB_CKSUMFLAGS(mp); 14424 DB_CKSUMFLAGS(mp) = 0; 14425 14426 /* 14427 * Directed broadcast forwarding: if the packet came in over a 14428 * different interface then it is routed out over we can forward it. 14429 */ 14430 if (ipha->ipha_protocol == IPPROTO_TCP) { 14431 ire_refrele(ire); 14432 freemsg(mp); 14433 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14434 return (NULL); 14435 } 14436 /* 14437 * For multicast we have set dst to be INADDR_BROADCAST 14438 * for delivering to all STREAMS. IRE_MARK_NORECV is really 14439 * only for broadcast packets. 14440 */ 14441 if (!CLASSD(ipha->ipha_dst)) { 14442 ire_t *new_ire; 14443 ipif_t *ipif; 14444 /* 14445 * For ill groups, as the switch duplicates broadcasts 14446 * across all the ports, we need to filter out and 14447 * send up only one copy. There is one copy for every 14448 * broadcast address on each ill. Thus, we look for a 14449 * specific IRE on this ill and look at IRE_MARK_NORECV 14450 * later to see whether this ill is eligible to receive 14451 * them or not. ill_nominate_bcast_rcv() nominates only 14452 * one set of IREs for receiving. 14453 */ 14454 14455 ipif = ipif_get_next_ipif(NULL, ill); 14456 if (ipif == NULL) { 14457 ire_refrele(ire); 14458 freemsg(mp); 14459 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14460 return (NULL); 14461 } 14462 new_ire = ire_ctable_lookup(dst, 0, 0, 14463 ipif, ALL_ZONES, NULL, MATCH_IRE_ILL, ipst); 14464 ipif_refrele(ipif); 14465 14466 if (new_ire != NULL) { 14467 if (new_ire->ire_marks & IRE_MARK_NORECV) { 14468 ire_refrele(ire); 14469 ire_refrele(new_ire); 14470 freemsg(mp); 14471 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14472 return (NULL); 14473 } 14474 /* 14475 * In the special case of multirouted broadcast 14476 * packets, we unconditionally need to "gateway" 14477 * them to the appropriate interface here. 14478 * In the normal case, this cannot happen, because 14479 * there is no broadcast IRE tagged with the 14480 * RTF_MULTIRT flag. 14481 */ 14482 if (new_ire->ire_flags & RTF_MULTIRT) { 14483 ire_refrele(new_ire); 14484 if (ire->ire_rfq != NULL) { 14485 q = ire->ire_rfq; 14486 *qp = q; 14487 } 14488 } else { 14489 ire_refrele(ire); 14490 ire = new_ire; 14491 } 14492 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 14493 if (!ipst->ips_ip_g_forward_directed_bcast) { 14494 /* 14495 * Free the message if 14496 * ip_g_forward_directed_bcast is turned 14497 * off for non-local broadcast. 14498 */ 14499 ire_refrele(ire); 14500 freemsg(mp); 14501 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14502 return (NULL); 14503 } 14504 } else { 14505 /* 14506 * This CGTP packet successfully passed the 14507 * CGTP filter, but the related CGTP 14508 * broadcast IRE has not been found, 14509 * meaning that the redundant ipif is 14510 * probably down. However, if we discarded 14511 * this packet, its duplicate would be 14512 * filtered out by the CGTP filter so none 14513 * of them would get through. So we keep 14514 * going with this one. 14515 */ 14516 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 14517 if (ire->ire_rfq != NULL) { 14518 q = ire->ire_rfq; 14519 *qp = q; 14520 } 14521 } 14522 } 14523 if (ipst->ips_ip_g_forward_directed_bcast && ll_multicast == 0) { 14524 /* 14525 * Verify that there are not more then one 14526 * IRE_BROADCAST with this broadcast address which 14527 * has ire_stq set. 14528 * TODO: simplify, loop over all IRE's 14529 */ 14530 ire_t *ire1; 14531 int num_stq = 0; 14532 mblk_t *mp1; 14533 14534 /* Find the first one with ire_stq set */ 14535 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 14536 for (ire1 = ire; ire1 && 14537 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 14538 ire1 = ire1->ire_next) 14539 ; 14540 if (ire1) { 14541 ire_refrele(ire); 14542 ire = ire1; 14543 IRE_REFHOLD(ire); 14544 } 14545 14546 /* Check if there are additional ones with stq set */ 14547 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 14548 if (ire->ire_addr != ire1->ire_addr) 14549 break; 14550 if (ire1->ire_stq) { 14551 num_stq++; 14552 break; 14553 } 14554 } 14555 rw_exit(&ire->ire_bucket->irb_lock); 14556 if (num_stq == 1 && ire->ire_stq != NULL) { 14557 ip1dbg(("ip_rput_process_broadcast: directed " 14558 "broadcast to 0x%x\n", 14559 ntohl(ire->ire_addr))); 14560 mp1 = copymsg(mp); 14561 if (mp1) { 14562 switch (ipha->ipha_protocol) { 14563 case IPPROTO_UDP: 14564 ip_udp_input(q, mp1, ipha, ire, ill); 14565 break; 14566 default: 14567 ip_proto_input(q, mp1, ipha, ire, ill); 14568 break; 14569 } 14570 } 14571 /* 14572 * Adjust ttl to 2 (1+1 - the forward engine 14573 * will decrement it by one. 14574 */ 14575 if (ip_csum_hdr(ipha)) { 14576 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 14577 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 14578 freemsg(mp); 14579 ire_refrele(ire); 14580 return (NULL); 14581 } 14582 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1; 14583 ipha->ipha_hdr_checksum = 0; 14584 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14585 ip_rput_process_forward(q, mp, ire, ipha, 14586 ill, ll_multicast); 14587 ire_refrele(ire); 14588 return (NULL); 14589 } 14590 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 14591 ntohl(ire->ire_addr))); 14592 } 14593 14594 14595 /* Restore any hardware checksum flags */ 14596 DB_CKSUMFLAGS(mp) = hcksumflags; 14597 return (ire); 14598 } 14599 14600 /* ARGSUSED */ 14601 static boolean_t 14602 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 14603 int *ll_multicast, ipaddr_t *dstp) 14604 { 14605 ip_stack_t *ipst = ill->ill_ipst; 14606 14607 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts); 14608 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, 14609 ntohs(ipha->ipha_length)); 14610 14611 /* 14612 * Forward packets only if we have joined the allmulti 14613 * group on this interface. 14614 */ 14615 if (ipst->ips_ip_g_mrouter && ill->ill_join_allmulti) { 14616 int retval; 14617 14618 /* 14619 * Clear the indication that this may have hardware 14620 * checksum as we are not using it. 14621 */ 14622 DB_CKSUMFLAGS(mp) = 0; 14623 retval = ip_mforward(ill, ipha, mp); 14624 /* ip_mforward updates mib variables if needed */ 14625 /* clear b_prev - used by ip_mroute_decap */ 14626 mp->b_prev = NULL; 14627 14628 switch (retval) { 14629 case 0: 14630 /* 14631 * pkt is okay and arrived on phyint. 14632 * 14633 * If we are running as a multicast router 14634 * we need to see all IGMP and/or PIM packets. 14635 */ 14636 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 14637 (ipha->ipha_protocol == IPPROTO_PIM)) { 14638 goto done; 14639 } 14640 break; 14641 case -1: 14642 /* pkt is mal-formed, toss it */ 14643 goto drop_pkt; 14644 case 1: 14645 /* pkt is okay and arrived on a tunnel */ 14646 /* 14647 * If we are running a multicast router 14648 * we need to see all igmp packets. 14649 */ 14650 if (ipha->ipha_protocol == IPPROTO_IGMP) { 14651 *dstp = INADDR_BROADCAST; 14652 *ll_multicast = 1; 14653 return (B_FALSE); 14654 } 14655 14656 goto drop_pkt; 14657 } 14658 } 14659 14660 ILM_WALKER_HOLD(ill); 14661 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 14662 /* 14663 * This might just be caused by the fact that 14664 * multiple IP Multicast addresses map to the same 14665 * link layer multicast - no need to increment counter! 14666 */ 14667 ILM_WALKER_RELE(ill); 14668 freemsg(mp); 14669 return (B_TRUE); 14670 } 14671 ILM_WALKER_RELE(ill); 14672 done: 14673 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 14674 /* 14675 * This assumes the we deliver to all streams for multicast 14676 * and broadcast packets. 14677 */ 14678 *dstp = INADDR_BROADCAST; 14679 *ll_multicast = 1; 14680 return (B_FALSE); 14681 drop_pkt: 14682 ip2dbg(("ip_rput: drop pkt\n")); 14683 freemsg(mp); 14684 return (B_TRUE); 14685 } 14686 14687 static boolean_t 14688 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 14689 int *ll_multicast, mblk_t **mpp) 14690 { 14691 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 14692 boolean_t must_copy = B_FALSE; 14693 struct iocblk *iocp; 14694 ipha_t *ipha; 14695 ip_stack_t *ipst = ill->ill_ipst; 14696 14697 #define rptr ((uchar_t *)ipha) 14698 14699 first_mp = *first_mpp; 14700 mp = *mpp; 14701 14702 ASSERT(first_mp == mp); 14703 14704 /* 14705 * if db_ref > 1 then copymsg and free original. Packet may be 14706 * changed and do not want other entity who has a reference to this 14707 * message to trip over the changes. This is a blind change because 14708 * trying to catch all places that might change packet is too 14709 * difficult (since it may be a module above this one) 14710 * 14711 * This corresponds to the non-fast path case. We walk down the full 14712 * chain in this case, and check the db_ref count of all the dblks, 14713 * and do a copymsg if required. It is possible that the db_ref counts 14714 * of the data blocks in the mblk chain can be different. 14715 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 14716 * count of 1, followed by a M_DATA block with a ref count of 2, if 14717 * 'snoop' is running. 14718 */ 14719 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 14720 if (mp1->b_datap->db_ref > 1) { 14721 must_copy = B_TRUE; 14722 break; 14723 } 14724 } 14725 14726 if (must_copy) { 14727 mp1 = copymsg(mp); 14728 if (mp1 == NULL) { 14729 for (mp1 = mp; mp1 != NULL; 14730 mp1 = mp1->b_cont) { 14731 mp1->b_next = NULL; 14732 mp1->b_prev = NULL; 14733 } 14734 freemsg(mp); 14735 if (ill != NULL) { 14736 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14737 } else { 14738 BUMP_MIB(&ipst->ips_ip_mib, 14739 ipIfStatsInDiscards); 14740 } 14741 return (B_TRUE); 14742 } 14743 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 14744 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 14745 /* Copy b_prev - used by ip_mroute_decap */ 14746 to_mp->b_prev = from_mp->b_prev; 14747 from_mp->b_prev = NULL; 14748 } 14749 *first_mpp = first_mp = mp1; 14750 freemsg(mp); 14751 mp = mp1; 14752 *mpp = mp1; 14753 } 14754 14755 ipha = (ipha_t *)mp->b_rptr; 14756 14757 /* 14758 * previous code has a case for M_DATA. 14759 * We want to check how that happens. 14760 */ 14761 ASSERT(first_mp->b_datap->db_type != M_DATA); 14762 switch (first_mp->b_datap->db_type) { 14763 case M_PROTO: 14764 case M_PCPROTO: 14765 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 14766 DL_UNITDATA_IND) { 14767 /* Go handle anything other than data elsewhere. */ 14768 ip_rput_dlpi(q, mp); 14769 return (B_TRUE); 14770 } 14771 *ll_multicast = ((dl_unitdata_ind_t *)rptr)->dl_group_address; 14772 /* Ditch the DLPI header. */ 14773 mp1 = mp->b_cont; 14774 ASSERT(first_mp == mp); 14775 *first_mpp = mp1; 14776 freeb(mp); 14777 *mpp = mp1; 14778 return (B_FALSE); 14779 case M_IOCACK: 14780 ip1dbg(("got iocack ")); 14781 iocp = (struct iocblk *)mp->b_rptr; 14782 switch (iocp->ioc_cmd) { 14783 case DL_IOC_HDR_INFO: 14784 ill = (ill_t *)q->q_ptr; 14785 ill_fastpath_ack(ill, mp); 14786 return (B_TRUE); 14787 case SIOCSTUNPARAM: 14788 case OSIOCSTUNPARAM: 14789 /* Go through qwriter_ip */ 14790 break; 14791 case SIOCGTUNPARAM: 14792 case OSIOCGTUNPARAM: 14793 ip_rput_other(NULL, q, mp, NULL); 14794 return (B_TRUE); 14795 default: 14796 putnext(q, mp); 14797 return (B_TRUE); 14798 } 14799 /* FALLTHRU */ 14800 case M_ERROR: 14801 case M_HANGUP: 14802 /* 14803 * Since this is on the ill stream we unconditionally 14804 * bump up the refcount 14805 */ 14806 ill_refhold(ill); 14807 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, 14808 B_FALSE); 14809 return (B_TRUE); 14810 case M_CTL: 14811 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 14812 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 14813 IPHADA_M_CTL)) { 14814 /* 14815 * It's an IPsec accelerated packet. 14816 * Make sure that the ill from which we received the 14817 * packet has enabled IPsec hardware acceleration. 14818 */ 14819 if (!(ill->ill_capabilities & 14820 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 14821 /* IPsec kstats: bean counter */ 14822 freemsg(mp); 14823 return (B_TRUE); 14824 } 14825 14826 /* 14827 * Make mp point to the mblk following the M_CTL, 14828 * then process according to type of mp. 14829 * After this processing, first_mp will point to 14830 * the data-attributes and mp to the pkt following 14831 * the M_CTL. 14832 */ 14833 mp = first_mp->b_cont; 14834 if (mp == NULL) { 14835 freemsg(first_mp); 14836 return (B_TRUE); 14837 } 14838 /* 14839 * A Hardware Accelerated packet can only be M_DATA 14840 * ESP or AH packet. 14841 */ 14842 if (mp->b_datap->db_type != M_DATA) { 14843 /* non-M_DATA IPsec accelerated packet */ 14844 IPSECHW_DEBUG(IPSECHW_PKT, 14845 ("non-M_DATA IPsec accelerated pkt\n")); 14846 freemsg(first_mp); 14847 return (B_TRUE); 14848 } 14849 ipha = (ipha_t *)mp->b_rptr; 14850 if (ipha->ipha_protocol != IPPROTO_AH && 14851 ipha->ipha_protocol != IPPROTO_ESP) { 14852 IPSECHW_DEBUG(IPSECHW_PKT, 14853 ("non-M_DATA IPsec accelerated pkt\n")); 14854 freemsg(first_mp); 14855 return (B_TRUE); 14856 } 14857 *mpp = mp; 14858 return (B_FALSE); 14859 } 14860 putnext(q, mp); 14861 return (B_TRUE); 14862 case M_FLUSH: 14863 if (*mp->b_rptr & FLUSHW) { 14864 *mp->b_rptr &= ~FLUSHR; 14865 qreply(q, mp); 14866 return (B_TRUE); 14867 } 14868 freemsg(mp); 14869 return (B_TRUE); 14870 case M_IOCNAK: 14871 ip1dbg(("got iocnak ")); 14872 iocp = (struct iocblk *)mp->b_rptr; 14873 switch (iocp->ioc_cmd) { 14874 case DL_IOC_HDR_INFO: 14875 case SIOCSTUNPARAM: 14876 case OSIOCSTUNPARAM: 14877 /* 14878 * Since this is on the ill stream we unconditionally 14879 * bump up the refcount 14880 */ 14881 ill_refhold(ill); 14882 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, 14883 CUR_OP, B_FALSE); 14884 return (B_TRUE); 14885 case SIOCGTUNPARAM: 14886 case OSIOCGTUNPARAM: 14887 ip_rput_other(NULL, q, mp, NULL); 14888 return (B_TRUE); 14889 default: 14890 break; 14891 } 14892 /* FALLTHRU */ 14893 default: 14894 putnext(q, mp); 14895 return (B_TRUE); 14896 } 14897 } 14898 14899 /* Read side put procedure. Packets coming from the wire arrive here. */ 14900 void 14901 ip_rput(queue_t *q, mblk_t *mp) 14902 { 14903 ill_t *ill; 14904 ip_stack_t *ipst; 14905 14906 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 14907 14908 ill = (ill_t *)q->q_ptr; 14909 ipst = ill->ill_ipst; 14910 14911 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 14912 union DL_primitives *dl; 14913 14914 /* 14915 * Things are opening or closing. Only accept DLPI control 14916 * messages. In the open case, the ill->ill_ipif has not yet 14917 * been created. In the close case, things hanging off the 14918 * ill could have been freed already. In either case it 14919 * may not be safe to proceed further. 14920 */ 14921 14922 dl = (union DL_primitives *)mp->b_rptr; 14923 if ((mp->b_datap->db_type != M_PCPROTO) || 14924 (dl->dl_primitive == DL_UNITDATA_IND)) { 14925 /* 14926 * Also SIOC[GS]TUN* ioctls can come here. 14927 */ 14928 inet_freemsg(mp); 14929 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14930 "ip_input_end: q %p (%S)", q, "uninit"); 14931 return; 14932 } 14933 } 14934 14935 /* 14936 * if db_ref > 1 then copymsg and free original. Packet may be 14937 * changed and we do not want the other entity who has a reference to 14938 * this message to trip over the changes. This is a blind change because 14939 * trying to catch all places that might change the packet is too 14940 * difficult. 14941 * 14942 * This corresponds to the fast path case, where we have a chain of 14943 * M_DATA mblks. We check the db_ref count of only the 1st data block 14944 * in the mblk chain. There doesn't seem to be a reason why a device 14945 * driver would send up data with varying db_ref counts in the mblk 14946 * chain. In any case the Fast path is a private interface, and our 14947 * drivers don't do such a thing. Given the above assumption, there is 14948 * no need to walk down the entire mblk chain (which could have a 14949 * potential performance problem) 14950 */ 14951 if (mp->b_datap->db_ref > 1) { 14952 mblk_t *mp1; 14953 boolean_t adjusted = B_FALSE; 14954 IP_STAT(ipst, ip_db_ref); 14955 14956 /* 14957 * The IP_RECVSLLA option depends on having the link layer 14958 * header. First check that: 14959 * a> the underlying device is of type ether, since this 14960 * option is currently supported only over ethernet. 14961 * b> there is enough room to copy over the link layer header. 14962 * 14963 * Once the checks are done, adjust rptr so that the link layer 14964 * header will be copied via copymsg. Note that, IFT_ETHER may 14965 * be returned by some non-ethernet drivers but in this case the 14966 * second check will fail. 14967 */ 14968 if (ill->ill_type == IFT_ETHER && 14969 (mp->b_rptr - mp->b_datap->db_base) >= 14970 sizeof (struct ether_header)) { 14971 mp->b_rptr -= sizeof (struct ether_header); 14972 adjusted = B_TRUE; 14973 } 14974 mp1 = copymsg(mp); 14975 if (mp1 == NULL) { 14976 mp->b_next = NULL; 14977 /* clear b_prev - used by ip_mroute_decap */ 14978 mp->b_prev = NULL; 14979 freemsg(mp); 14980 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14981 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14982 "ip_rput_end: q %p (%S)", q, "copymsg"); 14983 return; 14984 } 14985 if (adjusted) { 14986 /* 14987 * Copy is done. Restore the pointer in the _new_ mblk 14988 */ 14989 mp1->b_rptr += sizeof (struct ether_header); 14990 } 14991 /* Copy b_prev - used by ip_mroute_decap */ 14992 mp1->b_prev = mp->b_prev; 14993 mp->b_prev = NULL; 14994 freemsg(mp); 14995 mp = mp1; 14996 } 14997 14998 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14999 "ip_rput_end: q %p (%S)", q, "end"); 15000 15001 ip_input(ill, NULL, mp, NULL); 15002 } 15003 15004 /* 15005 * Direct read side procedure capable of dealing with chains. GLDv3 based 15006 * drivers call this function directly with mblk chains while STREAMS 15007 * read side procedure ip_rput() calls this for single packet with ip_ring 15008 * set to NULL to process one packet at a time. 15009 * 15010 * The ill will always be valid if this function is called directly from 15011 * the driver. 15012 * 15013 * If ip_input() is called from GLDv3: 15014 * 15015 * - This must be a non-VLAN IP stream. 15016 * - 'mp' is either an untagged or a special priority-tagged packet. 15017 * - Any VLAN tag that was in the MAC header has been stripped. 15018 * 15019 * If the IP header in packet is not 32-bit aligned, every message in the 15020 * chain will be aligned before further operations. This is required on SPARC 15021 * platform. 15022 */ 15023 /* ARGSUSED */ 15024 void 15025 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, 15026 struct mac_header_info_s *mhip) 15027 { 15028 ipaddr_t dst = NULL; 15029 ipaddr_t prev_dst; 15030 ire_t *ire = NULL; 15031 ipha_t *ipha; 15032 uint_t pkt_len; 15033 ssize_t len; 15034 uint_t opt_len; 15035 int ll_multicast; 15036 int cgtp_flt_pkt; 15037 queue_t *q = ill->ill_rq; 15038 squeue_t *curr_sqp = NULL; 15039 mblk_t *head = NULL; 15040 mblk_t *tail = NULL; 15041 mblk_t *first_mp; 15042 mblk_t *mp; 15043 mblk_t *dmp; 15044 int cnt = 0; 15045 ip_stack_t *ipst = ill->ill_ipst; 15046 15047 ASSERT(mp_chain != NULL); 15048 ASSERT(ill != NULL); 15049 15050 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 15051 15052 #define rptr ((uchar_t *)ipha) 15053 15054 while (mp_chain != NULL) { 15055 first_mp = mp = mp_chain; 15056 mp_chain = mp_chain->b_next; 15057 mp->b_next = NULL; 15058 ll_multicast = 0; 15059 15060 /* 15061 * We do ire caching from one iteration to 15062 * another. In the event the packet chain contains 15063 * all packets from the same dst, this caching saves 15064 * an ire_cache_lookup for each of the succeeding 15065 * packets in a packet chain. 15066 */ 15067 prev_dst = dst; 15068 15069 /* 15070 * Check and align the IP header. 15071 */ 15072 if (DB_TYPE(mp) == M_DATA) { 15073 dmp = mp; 15074 } else if (DB_TYPE(mp) == M_PROTO && 15075 *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { 15076 dmp = mp->b_cont; 15077 } else { 15078 dmp = NULL; 15079 } 15080 if (dmp != NULL) { 15081 /* 15082 * IP header ptr not aligned? 15083 * OR IP header not complete in first mblk 15084 */ 15085 if (!OK_32PTR(dmp->b_rptr) || 15086 MBLKL(dmp) < IP_SIMPLE_HDR_LENGTH) { 15087 if (!ip_check_and_align_header(q, dmp, ipst)) 15088 continue; 15089 } 15090 } 15091 15092 /* 15093 * ip_input fast path 15094 */ 15095 15096 /* mblk type is not M_DATA */ 15097 if (DB_TYPE(mp) != M_DATA) { 15098 if (ip_rput_process_notdata(q, &first_mp, ill, 15099 &ll_multicast, &mp)) 15100 continue; 15101 } 15102 15103 /* Make sure its an M_DATA and that its aligned */ 15104 ASSERT(DB_TYPE(mp) == M_DATA); 15105 ASSERT(DB_REF(mp) == 1 && OK_32PTR(mp->b_rptr)); 15106 15107 ipha = (ipha_t *)mp->b_rptr; 15108 len = mp->b_wptr - rptr; 15109 pkt_len = ntohs(ipha->ipha_length); 15110 15111 /* 15112 * We must count all incoming packets, even if they end 15113 * up being dropped later on. 15114 */ 15115 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 15116 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); 15117 15118 /* multiple mblk or too short */ 15119 len -= pkt_len; 15120 if (len != 0) { 15121 /* 15122 * Make sure we have data length consistent 15123 * with the IP header. 15124 */ 15125 if (mp->b_cont == NULL) { 15126 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15127 BUMP_MIB(ill->ill_ip_mib, 15128 ipIfStatsInHdrErrors); 15129 ip2dbg(("ip_input: drop pkt\n")); 15130 freemsg(mp); 15131 continue; 15132 } 15133 mp->b_wptr = rptr + pkt_len; 15134 } else if ((len += msgdsize(mp->b_cont)) != 0) { 15135 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15136 BUMP_MIB(ill->ill_ip_mib, 15137 ipIfStatsInHdrErrors); 15138 ip2dbg(("ip_input: drop pkt\n")); 15139 freemsg(mp); 15140 continue; 15141 } 15142 (void) adjmsg(mp, -len); 15143 IP_STAT(ipst, ip_multimblk3); 15144 } 15145 } 15146 15147 /* Obtain the dst of the current packet */ 15148 dst = ipha->ipha_dst; 15149 15150 if (IP_LOOPBACK_ADDR(dst) || 15151 IP_LOOPBACK_ADDR(ipha->ipha_src)) { 15152 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors); 15153 cmn_err(CE_CONT, "dst %X src %X\n", 15154 dst, ipha->ipha_src); 15155 freemsg(mp); 15156 continue; 15157 } 15158 15159 /* 15160 * The event for packets being received from a 'physical' 15161 * interface is placed after validation of the source and/or 15162 * destination address as being local so that packets can be 15163 * redirected to loopback addresses using ipnat. 15164 */ 15165 DTRACE_PROBE4(ip4__physical__in__start, 15166 ill_t *, ill, ill_t *, NULL, 15167 ipha_t *, ipha, mblk_t *, first_mp); 15168 15169 FW_HOOKS(ipst->ips_ip4_physical_in_event, 15170 ipst->ips_ipv4firewall_physical_in, 15171 ill, NULL, ipha, first_mp, mp, ipst); 15172 15173 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 15174 15175 if (first_mp == NULL) { 15176 continue; 15177 } 15178 dst = ipha->ipha_dst; 15179 15180 /* 15181 * Attach any necessary label information to 15182 * this packet 15183 */ 15184 if (is_system_labeled() && 15185 !tsol_get_pkt_label(mp, IPV4_VERSION)) { 15186 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 15187 freemsg(mp); 15188 continue; 15189 } 15190 15191 /* 15192 * Reuse the cached ire only if the ipha_dst of the previous 15193 * packet is the same as the current packet AND it is not 15194 * INADDR_ANY. 15195 */ 15196 if (!(dst == prev_dst && dst != INADDR_ANY) && 15197 (ire != NULL)) { 15198 ire_refrele(ire); 15199 ire = NULL; 15200 } 15201 opt_len = ipha->ipha_version_and_hdr_length - 15202 IP_SIMPLE_HDR_VERSION; 15203 15204 /* 15205 * Check to see if we can take the fastpath. 15206 * That is possible if the following conditions are met 15207 * o Tsol disabled 15208 * o CGTP disabled 15209 * o ipp_action_count is 0 15210 * o Mobile IP not running 15211 * o no options in the packet 15212 * o not a RSVP packet 15213 * o not a multicast packet 15214 */ 15215 if (!is_system_labeled() && 15216 !ip_cgtp_filter && ipp_action_count == 0 && 15217 ill->ill_mrtun_refcnt == 0 && ill->ill_srcif_refcnt == 0 && 15218 opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && 15219 !ll_multicast && !CLASSD(dst)) { 15220 if (ire == NULL) 15221 ire = ire_cache_lookup(dst, ALL_ZONES, NULL, 15222 ipst); 15223 15224 /* incoming packet is for forwarding */ 15225 if (ire == NULL || (ire->ire_type & IRE_CACHE)) { 15226 ire = ip_fast_forward(ire, dst, ill, mp); 15227 continue; 15228 } 15229 /* incoming packet is for local consumption */ 15230 if (ire->ire_type & IRE_LOCAL) 15231 goto local; 15232 } 15233 15234 /* 15235 * Disable ire caching for anything more complex 15236 * than the simple fast path case we checked for above. 15237 */ 15238 if (ire != NULL) { 15239 ire_refrele(ire); 15240 ire = NULL; 15241 } 15242 15243 /* Full-blown slow path */ 15244 if (opt_len != 0) { 15245 if (len != 0) 15246 IP_STAT(ipst, ip_multimblk4); 15247 else 15248 IP_STAT(ipst, ip_ipoptions); 15249 if (!ip_rput_multimblk_ipoptions(q, ill, mp, &ipha, 15250 &dst, ipst)) 15251 continue; 15252 } 15253 15254 /* 15255 * Invoke the CGTP (multirouting) filtering module to process 15256 * the incoming packet. Packets identified as duplicates 15257 * must be discarded. Filtering is active only if the 15258 * the ip_cgtp_filter ndd variable is non-zero. 15259 * 15260 * Only applies to the shared stack since the filter_ops 15261 * do not carry an ip_stack_t or zoneid. 15262 */ 15263 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 15264 if (ip_cgtp_filter && (ip_cgtp_filter_ops != NULL) && 15265 ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) { 15266 cgtp_flt_pkt = 15267 ip_cgtp_filter_ops->cfo_filter(q, mp); 15268 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 15269 freemsg(first_mp); 15270 continue; 15271 } 15272 } 15273 15274 /* 15275 * If rsvpd is running, let RSVP daemon handle its processing 15276 * and forwarding of RSVP multicast/unicast packets. 15277 * If rsvpd is not running but mrouted is running, RSVP 15278 * multicast packets are forwarded as multicast traffic 15279 * and RSVP unicast packets are forwarded by unicast router. 15280 * If neither rsvpd nor mrouted is running, RSVP multicast 15281 * packets are not forwarded, but the unicast packets are 15282 * forwarded like unicast traffic. 15283 */ 15284 if (ipha->ipha_protocol == IPPROTO_RSVP && 15285 ipst->ips_ipcl_proto_fanout[IPPROTO_RSVP].connf_head != 15286 NULL) { 15287 /* RSVP packet and rsvpd running. Treat as ours */ 15288 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 15289 /* 15290 * This assumes that we deliver to all streams for 15291 * multicast and broadcast packets. 15292 * We have to force ll_multicast to 1 to handle the 15293 * M_DATA messages passed in from ip_mroute_decap. 15294 */ 15295 dst = INADDR_BROADCAST; 15296 ll_multicast = 1; 15297 } else if (CLASSD(dst)) { 15298 /* packet is multicast */ 15299 mp->b_next = NULL; 15300 if (ip_rput_process_multicast(q, mp, ill, ipha, 15301 &ll_multicast, &dst)) 15302 continue; 15303 } 15304 15305 15306 /* 15307 * Check if the packet is coming from the Mobile IP 15308 * forward tunnel interface 15309 */ 15310 if (ill->ill_srcif_refcnt > 0) { 15311 ire = ire_srcif_table_lookup(dst, IRE_INTERFACE, 15312 NULL, ill, MATCH_IRE_TYPE); 15313 if (ire != NULL && ire->ire_nce->nce_res_mp == NULL && 15314 ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER) { 15315 15316 /* We need to resolve the link layer info */ 15317 ire_refrele(ire); 15318 ire = NULL; 15319 (void) ip_rput_noire(q, (ill_t *)q->q_ptr, mp, 15320 ll_multicast, dst); 15321 continue; 15322 } 15323 } 15324 15325 if (ire == NULL) { 15326 ire = ire_cache_lookup(dst, ALL_ZONES, 15327 MBLK_GETLABEL(mp), ipst); 15328 } 15329 15330 /* 15331 * If mipagent is running and reverse tunnel is created as per 15332 * mobile node request, then any packet coming through the 15333 * incoming interface from the mobile-node, should be reverse 15334 * tunneled to it's home agent except those that are destined 15335 * to foreign agent only. 15336 * This needs source address based ire lookup. The routing 15337 * entries for source address based lookup are only created by 15338 * mipagent program only when a reverse tunnel is created. 15339 * Reference : RFC2002, RFC2344 15340 */ 15341 if (ill->ill_mrtun_refcnt > 0) { 15342 ipaddr_t srcaddr; 15343 ire_t *tmp_ire; 15344 15345 tmp_ire = ire; /* Save, we might need it later */ 15346 if (ire == NULL || (ire->ire_type != IRE_LOCAL && 15347 ire->ire_type != IRE_BROADCAST)) { 15348 srcaddr = ipha->ipha_src; 15349 ire = ire_mrtun_lookup(srcaddr, ill); 15350 if (ire != NULL) { 15351 /* 15352 * Should not be getting iphada packet 15353 * here. we should only get those for 15354 * IRE_LOCAL traffic, excluded above. 15355 * Fail-safe (drop packet) in the event 15356 * hardware is misbehaving. 15357 */ 15358 if (first_mp != mp) { 15359 /* IPsec KSTATS: beancount me */ 15360 freemsg(first_mp); 15361 } else { 15362 /* 15363 * This packet must be forwarded 15364 * to Reverse Tunnel 15365 */ 15366 ip_mrtun_forward(ire, ill, mp); 15367 } 15368 ire_refrele(ire); 15369 ire = NULL; 15370 if (tmp_ire != NULL) { 15371 ire_refrele(tmp_ire); 15372 tmp_ire = NULL; 15373 } 15374 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15375 "ip_input_end: q %p (%S)", 15376 q, "uninit"); 15377 continue; 15378 } 15379 } 15380 /* 15381 * If this packet is from a non-mobilenode or a 15382 * mobile-node which does not request reverse 15383 * tunnel service 15384 */ 15385 ire = tmp_ire; 15386 } 15387 15388 15389 /* 15390 * If we reach here that means the incoming packet satisfies 15391 * one of the following conditions: 15392 * - packet is from a mobile node which does not request 15393 * reverse tunnel 15394 * - packet is from a non-mobile node, which is the most 15395 * common case 15396 * - packet is from a reverse tunnel enabled mobile node 15397 * and destined to foreign agent only 15398 */ 15399 15400 if (ire == NULL) { 15401 /* 15402 * No IRE for this destination, so it can't be for us. 15403 * Unless we are forwarding, drop the packet. 15404 * We have to let source routed packets through 15405 * since we don't yet know if they are 'ping -l' 15406 * packets i.e. if they will go out over the 15407 * same interface as they came in on. 15408 */ 15409 ire = ip_rput_noire(q, NULL, mp, ll_multicast, dst); 15410 if (ire == NULL) 15411 continue; 15412 } 15413 15414 /* 15415 * Broadcast IRE may indicate either broadcast or 15416 * multicast packet 15417 */ 15418 if (ire->ire_type == IRE_BROADCAST) { 15419 /* 15420 * Skip broadcast checks if packet is UDP multicast; 15421 * we'd rather not enter ip_rput_process_broadcast() 15422 * unless the packet is broadcast for real, since 15423 * that routine is a no-op for multicast. 15424 */ 15425 if (ipha->ipha_protocol != IPPROTO_UDP || 15426 !CLASSD(ipha->ipha_dst)) { 15427 ire = ip_rput_process_broadcast(&q, mp, 15428 ire, ipha, ill, dst, cgtp_flt_pkt, 15429 ll_multicast); 15430 if (ire == NULL) 15431 continue; 15432 } 15433 } else if (ire->ire_stq != NULL) { 15434 /* fowarding? */ 15435 ip_rput_process_forward(q, mp, ire, ipha, ill, 15436 ll_multicast); 15437 /* ip_rput_process_forward consumed the packet */ 15438 continue; 15439 } 15440 15441 local: 15442 /* packet not for us */ 15443 if (ire->ire_rfq != q) { 15444 if (ip_rput_notforus(&q, mp, ire, ill)) 15445 continue; 15446 } 15447 15448 switch (ipha->ipha_protocol) { 15449 case IPPROTO_TCP: 15450 ASSERT(first_mp == mp); 15451 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 15452 mp, 0, q, ip_ring)) != NULL) { 15453 if (curr_sqp == NULL) { 15454 curr_sqp = GET_SQUEUE(mp); 15455 ASSERT(cnt == 0); 15456 cnt++; 15457 head = tail = mp; 15458 } else if (curr_sqp == GET_SQUEUE(mp)) { 15459 ASSERT(tail != NULL); 15460 cnt++; 15461 tail->b_next = mp; 15462 tail = mp; 15463 } else { 15464 /* 15465 * A different squeue. Send the 15466 * chain for the previous squeue on 15467 * its way. This shouldn't happen 15468 * often unless interrupt binding 15469 * changes. 15470 */ 15471 IP_STAT(ipst, ip_input_multi_squeue); 15472 squeue_enter_chain(curr_sqp, head, 15473 tail, cnt, SQTAG_IP_INPUT); 15474 curr_sqp = GET_SQUEUE(mp); 15475 head = mp; 15476 tail = mp; 15477 cnt = 1; 15478 } 15479 } 15480 continue; 15481 case IPPROTO_UDP: 15482 ASSERT(first_mp == mp); 15483 ip_udp_input(q, mp, ipha, ire, ill); 15484 continue; 15485 case IPPROTO_SCTP: 15486 ASSERT(first_mp == mp); 15487 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 15488 q, dst); 15489 /* ire has been released by ip_sctp_input */ 15490 ire = NULL; 15491 continue; 15492 default: 15493 ip_proto_input(q, first_mp, ipha, ire, ill); 15494 continue; 15495 } 15496 } 15497 15498 if (ire != NULL) 15499 ire_refrele(ire); 15500 15501 if (head != NULL) 15502 squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); 15503 15504 /* 15505 * This code is there just to make netperf/ttcp look good. 15506 * 15507 * Its possible that after being in polling mode (and having cleared 15508 * the backlog), squeues have turned the interrupt frequency higher 15509 * to improve latency at the expense of more CPU utilization (less 15510 * packets per interrupts or more number of interrupts). Workloads 15511 * like ttcp/netperf do manage to tickle polling once in a while 15512 * but for the remaining time, stay in higher interrupt mode since 15513 * their packet arrival rate is pretty uniform and this shows up 15514 * as higher CPU utilization. Since people care about CPU utilization 15515 * while running netperf/ttcp, turn the interrupt frequency back to 15516 * normal/default if polling has not been used in ip_poll_normal_ticks. 15517 */ 15518 if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { 15519 if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { 15520 ip_ring->rr_poll_state &= ~ILL_POLLING; 15521 ip_ring->rr_blank(ip_ring->rr_handle, 15522 ip_ring->rr_normal_blank_time, 15523 ip_ring->rr_normal_pkt_cnt); 15524 } 15525 } 15526 15527 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15528 "ip_input_end: q %p (%S)", q, "end"); 15529 #undef rptr 15530 } 15531 15532 static void 15533 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 15534 t_uscalar_t err) 15535 { 15536 if (dl_err == DL_SYSERR) { 15537 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15538 "%s: %s failed: DL_SYSERR (errno %u)\n", 15539 ill->ill_name, dlpi_prim_str(prim), err); 15540 return; 15541 } 15542 15543 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15544 "%s: %s failed: %s\n", ill->ill_name, dlpi_prim_str(prim), 15545 dlpi_err_str(dl_err)); 15546 } 15547 15548 /* 15549 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 15550 * than DL_UNITDATA_IND messages. If we need to process this message 15551 * exclusively, we call qwriter_ip, in which case we also need to call 15552 * ill_refhold before that, since qwriter_ip does an ill_refrele. 15553 */ 15554 void 15555 ip_rput_dlpi(queue_t *q, mblk_t *mp) 15556 { 15557 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15558 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15559 ill_t *ill; 15560 15561 ip1dbg(("ip_rput_dlpi")); 15562 ill = (ill_t *)q->q_ptr; 15563 switch (dloa->dl_primitive) { 15564 case DL_ERROR_ACK: 15565 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK %s (0x%x): " 15566 "%s (0x%x), unix %u\n", ill->ill_name, 15567 dlpi_prim_str(dlea->dl_error_primitive), 15568 dlea->dl_error_primitive, 15569 dlpi_err_str(dlea->dl_errno), 15570 dlea->dl_errno, 15571 dlea->dl_unix_errno)); 15572 switch (dlea->dl_error_primitive) { 15573 case DL_UNBIND_REQ: 15574 mutex_enter(&ill->ill_lock); 15575 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15576 cv_signal(&ill->ill_cv); 15577 mutex_exit(&ill->ill_lock); 15578 /* FALLTHRU */ 15579 case DL_NOTIFY_REQ: 15580 case DL_ATTACH_REQ: 15581 case DL_DETACH_REQ: 15582 case DL_INFO_REQ: 15583 case DL_BIND_REQ: 15584 case DL_ENABMULTI_REQ: 15585 case DL_PHYS_ADDR_REQ: 15586 case DL_CAPABILITY_REQ: 15587 case DL_CONTROL_REQ: 15588 /* 15589 * Refhold the ill to match qwriter_ip which does a 15590 * refrele. Since this is on the ill stream we 15591 * unconditionally bump up the refcount without 15592 * checking for ILL_CAN_LOOKUP 15593 */ 15594 ill_refhold(ill); 15595 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15596 CUR_OP, B_FALSE); 15597 return; 15598 case DL_DISABMULTI_REQ: 15599 freemsg(mp); /* Don't want to pass this up */ 15600 return; 15601 default: 15602 break; 15603 } 15604 ip_dlpi_error(ill, dlea->dl_error_primitive, 15605 dlea->dl_errno, dlea->dl_unix_errno); 15606 freemsg(mp); 15607 return; 15608 case DL_INFO_ACK: 15609 case DL_BIND_ACK: 15610 case DL_PHYS_ADDR_ACK: 15611 case DL_NOTIFY_ACK: 15612 case DL_CAPABILITY_ACK: 15613 case DL_CONTROL_ACK: 15614 /* 15615 * Refhold the ill to match qwriter_ip which does a refrele 15616 * Since this is on the ill stream we unconditionally 15617 * bump up the refcount without doing ILL_CAN_LOOKUP. 15618 */ 15619 ill_refhold(ill); 15620 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15621 CUR_OP, B_FALSE); 15622 return; 15623 case DL_NOTIFY_IND: 15624 ill_refhold(ill); 15625 /* 15626 * The DL_NOTIFY_IND is an asynchronous message that has no 15627 * relation to the current ioctl in progress (if any). Hence we 15628 * pass in NEW_OP in this case. 15629 */ 15630 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15631 NEW_OP, B_FALSE); 15632 return; 15633 case DL_OK_ACK: 15634 ip1dbg(("ip_rput: DL_OK_ACK for %s\n", 15635 dlpi_prim_str((int)dloa->dl_correct_primitive))); 15636 switch (dloa->dl_correct_primitive) { 15637 case DL_UNBIND_REQ: 15638 mutex_enter(&ill->ill_lock); 15639 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15640 cv_signal(&ill->ill_cv); 15641 mutex_exit(&ill->ill_lock); 15642 /* FALLTHRU */ 15643 case DL_ATTACH_REQ: 15644 case DL_DETACH_REQ: 15645 /* 15646 * Refhold the ill to match qwriter_ip which does a 15647 * refrele. Since this is on the ill stream we 15648 * unconditionally bump up the refcount 15649 */ 15650 ill_refhold(ill); 15651 qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15652 CUR_OP, B_FALSE); 15653 return; 15654 case DL_ENABMULTI_REQ: 15655 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15656 ill->ill_dlpi_multicast_state = IDS_OK; 15657 break; 15658 15659 } 15660 break; 15661 default: 15662 break; 15663 } 15664 freemsg(mp); 15665 } 15666 15667 /* 15668 * Handling of DLPI messages that require exclusive access to the ipsq. 15669 * 15670 * Need to do ill_pending_mp_release on ioctl completion, which could 15671 * happen here. (along with mi_copy_done) 15672 */ 15673 /* ARGSUSED */ 15674 static void 15675 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15676 { 15677 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15678 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15679 int err = 0; 15680 ill_t *ill; 15681 ipif_t *ipif = NULL; 15682 mblk_t *mp1 = NULL; 15683 conn_t *connp = NULL; 15684 t_uscalar_t paddrreq; 15685 mblk_t *mp_hw; 15686 boolean_t success; 15687 boolean_t ioctl_aborted = B_FALSE; 15688 boolean_t log = B_TRUE; 15689 hook_nic_event_t *info; 15690 ip_stack_t *ipst; 15691 15692 ip1dbg(("ip_rput_dlpi_writer ..")); 15693 ill = (ill_t *)q->q_ptr; 15694 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 15695 15696 ASSERT(IAM_WRITER_ILL(ill)); 15697 15698 ipst = ill->ill_ipst; 15699 15700 /* 15701 * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. 15702 * both are null or non-null. However we can assert that only 15703 * after grabbing the ipsq_lock. So we don't make any assertion 15704 * here and in other places in the code. 15705 */ 15706 ipif = ipsq->ipsq_pending_ipif; 15707 /* 15708 * The current ioctl could have been aborted by the user and a new 15709 * ioctl to bring up another ill could have started. We could still 15710 * get a response from the driver later. 15711 */ 15712 if (ipif != NULL && ipif->ipif_ill != ill) 15713 ioctl_aborted = B_TRUE; 15714 15715 switch (dloa->dl_primitive) { 15716 case DL_ERROR_ACK: 15717 switch (dlea->dl_error_primitive) { 15718 case DL_UNBIND_REQ: 15719 case DL_ATTACH_REQ: 15720 case DL_DETACH_REQ: 15721 case DL_INFO_REQ: 15722 ill_dlpi_done(ill, dlea->dl_error_primitive); 15723 break; 15724 case DL_NOTIFY_REQ: 15725 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15726 log = B_FALSE; 15727 break; 15728 case DL_PHYS_ADDR_REQ: 15729 /* 15730 * For IPv6 only, there are two additional 15731 * phys_addr_req's sent to the driver to get the 15732 * IPv6 token and lla. This allows IP to acquire 15733 * the hardware address format for a given interface 15734 * without having built in knowledge of the hardware 15735 * address. ill_phys_addr_pend keeps track of the last 15736 * DL_PAR sent so we know which response we are 15737 * dealing with. ill_dlpi_done will update 15738 * ill_phys_addr_pend when it sends the next req. 15739 * We don't complete the IOCTL until all three DL_PARs 15740 * have been attempted, so set *_len to 0 and break. 15741 */ 15742 paddrreq = ill->ill_phys_addr_pend; 15743 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15744 if (paddrreq == DL_IPV6_TOKEN) { 15745 ill->ill_token_length = 0; 15746 log = B_FALSE; 15747 break; 15748 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 15749 ill->ill_nd_lla_len = 0; 15750 log = B_FALSE; 15751 break; 15752 } 15753 /* 15754 * Something went wrong with the DL_PHYS_ADDR_REQ. 15755 * We presumably have an IOCTL hanging out waiting 15756 * for completion. Find it and complete the IOCTL 15757 * with the error noted. 15758 * However, ill_dl_phys was called on an ill queue 15759 * (from SIOCSLIFNAME), thus conn_pending_ill is not 15760 * set. But the ioctl is known to be pending on ill_wq. 15761 */ 15762 if (!ill->ill_ifname_pending) 15763 break; 15764 ill->ill_ifname_pending = 0; 15765 if (!ioctl_aborted) 15766 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15767 if (mp1 != NULL) { 15768 /* 15769 * This operation (SIOCSLIFNAME) must have 15770 * happened on the ill. Assert there is no conn 15771 */ 15772 ASSERT(connp == NULL); 15773 q = ill->ill_wq; 15774 } 15775 break; 15776 case DL_BIND_REQ: 15777 ill_dlpi_done(ill, DL_BIND_REQ); 15778 if (ill->ill_ifname_pending) 15779 break; 15780 /* 15781 * Something went wrong with the bind. We presumably 15782 * have an IOCTL hanging out waiting for completion. 15783 * Find it, take down the interface that was coming 15784 * up, and complete the IOCTL with the error noted. 15785 */ 15786 if (!ioctl_aborted) 15787 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15788 if (mp1 != NULL) { 15789 /* 15790 * This operation (SIOCSLIFFLAGS) must have 15791 * happened from a conn. 15792 */ 15793 ASSERT(connp != NULL); 15794 q = CONNP_TO_WQ(connp); 15795 if (ill->ill_move_in_progress) { 15796 ILL_CLEAR_MOVE(ill); 15797 } 15798 (void) ipif_down(ipif, NULL, NULL); 15799 /* error is set below the switch */ 15800 } 15801 break; 15802 case DL_ENABMULTI_REQ: 15803 ip1dbg(("DL_ERROR_ACK to enabmulti\n")); 15804 15805 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15806 ill->ill_dlpi_multicast_state = IDS_FAILED; 15807 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 15808 ipif_t *ipif; 15809 15810 log = B_FALSE; 15811 printf("ip: joining multicasts failed (%d)" 15812 " on %s - will use link layer " 15813 "broadcasts for multicast\n", 15814 dlea->dl_errno, ill->ill_name); 15815 15816 /* 15817 * Set up the multicast mapping alone. 15818 * writer, so ok to access ill->ill_ipif 15819 * without any lock. 15820 */ 15821 ipif = ill->ill_ipif; 15822 mutex_enter(&ill->ill_phyint->phyint_lock); 15823 ill->ill_phyint->phyint_flags |= 15824 PHYI_MULTI_BCAST; 15825 mutex_exit(&ill->ill_phyint->phyint_lock); 15826 15827 if (!ill->ill_isv6) { 15828 (void) ipif_arp_setup_multicast(ipif, 15829 NULL); 15830 } else { 15831 (void) ipif_ndp_setup_multicast(ipif, 15832 NULL); 15833 } 15834 } 15835 freemsg(mp); /* Don't want to pass this up */ 15836 return; 15837 case DL_CAPABILITY_REQ: 15838 case DL_CONTROL_REQ: 15839 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 15840 "DL_CAPABILITY/CONTROL REQ\n")); 15841 ill_dlpi_done(ill, dlea->dl_error_primitive); 15842 ill->ill_dlpi_capab_state = IDS_FAILED; 15843 freemsg(mp); 15844 return; 15845 } 15846 /* 15847 * Note the error for IOCTL completion (mp1 is set when 15848 * ready to complete ioctl). If ill_ifname_pending_err is 15849 * set, an error occured during plumbing (ill_ifname_pending), 15850 * so we want to report that error. 15851 * 15852 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 15853 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 15854 * expected to get errack'd if the driver doesn't support 15855 * these flags (e.g. ethernet). log will be set to B_FALSE 15856 * if these error conditions are encountered. 15857 */ 15858 if (mp1 != NULL) { 15859 if (ill->ill_ifname_pending_err != 0) { 15860 err = ill->ill_ifname_pending_err; 15861 ill->ill_ifname_pending_err = 0; 15862 } else { 15863 err = dlea->dl_unix_errno ? 15864 dlea->dl_unix_errno : ENXIO; 15865 } 15866 /* 15867 * If we're plumbing an interface and an error hasn't already 15868 * been saved, set ill_ifname_pending_err to the error passed 15869 * up. Ignore the error if log is B_FALSE (see comment above). 15870 */ 15871 } else if (log && ill->ill_ifname_pending && 15872 ill->ill_ifname_pending_err == 0) { 15873 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 15874 dlea->dl_unix_errno : ENXIO; 15875 } 15876 15877 if (log) 15878 ip_dlpi_error(ill, dlea->dl_error_primitive, 15879 dlea->dl_errno, dlea->dl_unix_errno); 15880 break; 15881 case DL_CAPABILITY_ACK: { 15882 boolean_t reneg_flag = B_FALSE; 15883 /* Call a routine to handle this one. */ 15884 ill_dlpi_done(ill, DL_CAPABILITY_REQ); 15885 /* 15886 * Check if the ACK is due to renegotiation case since we 15887 * will need to send a new CAPABILITY_REQ later. 15888 */ 15889 if (ill->ill_dlpi_capab_state == IDS_RENEG) { 15890 /* This is the ack for a renogiation case */ 15891 reneg_flag = B_TRUE; 15892 ill->ill_dlpi_capab_state = IDS_UNKNOWN; 15893 } 15894 ill_capability_ack(ill, mp); 15895 if (reneg_flag) 15896 ill_capability_probe(ill); 15897 break; 15898 } 15899 case DL_CONTROL_ACK: 15900 /* We treat all of these as "fire and forget" */ 15901 ill_dlpi_done(ill, DL_CONTROL_REQ); 15902 break; 15903 case DL_INFO_ACK: 15904 /* Call a routine to handle this one. */ 15905 ill_dlpi_done(ill, DL_INFO_REQ); 15906 ip_ll_subnet_defaults(ill, mp); 15907 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 15908 return; 15909 case DL_BIND_ACK: 15910 /* 15911 * We should have an IOCTL waiting on this unless 15912 * sent by ill_dl_phys, in which case just return 15913 */ 15914 ill_dlpi_done(ill, DL_BIND_REQ); 15915 if (ill->ill_ifname_pending) 15916 break; 15917 15918 if (!ioctl_aborted) 15919 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15920 if (mp1 == NULL) 15921 break; 15922 /* 15923 * Because mp1 was added by ill_dl_up(), and it always 15924 * passes a valid connp, connp must be valid here. 15925 */ 15926 ASSERT(connp != NULL); 15927 q = CONNP_TO_WQ(connp); 15928 15929 /* 15930 * We are exclusive. So nothing can change even after 15931 * we get the pending mp. If need be we can put it back 15932 * and restart, as in calling ipif_arp_up() below. 15933 */ 15934 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 15935 15936 mutex_enter(&ill->ill_lock); 15937 15938 ill->ill_dl_up = 1; 15939 15940 if ((info = ill->ill_nic_event_info) != NULL) { 15941 ip2dbg(("ip_rput_dlpi_writer: unexpected nic event %d " 15942 "attached for %s\n", info->hne_event, 15943 ill->ill_name)); 15944 if (info->hne_data != NULL) 15945 kmem_free(info->hne_data, info->hne_datalen); 15946 kmem_free(info, sizeof (hook_nic_event_t)); 15947 } 15948 15949 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 15950 if (info != NULL) { 15951 info->hne_nic = ill->ill_phyint->phyint_ifindex; 15952 info->hne_lif = 0; 15953 info->hne_event = NE_UP; 15954 info->hne_data = NULL; 15955 info->hne_datalen = 0; 15956 info->hne_family = ill->ill_isv6 ? 15957 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data; 15958 } else 15959 ip2dbg(("ip_rput_dlpi_writer: could not attach UP nic " 15960 "event information for %s (ENOMEM)\n", 15961 ill->ill_name)); 15962 15963 ill->ill_nic_event_info = info; 15964 15965 mutex_exit(&ill->ill_lock); 15966 15967 /* 15968 * Now bring up the resolver; when that is complete, we'll 15969 * create IREs. Note that we intentionally mirror what 15970 * ipif_up() would have done, because we got here by way of 15971 * ill_dl_up(), which stopped ipif_up()'s processing. 15972 */ 15973 if (ill->ill_isv6) { 15974 /* 15975 * v6 interfaces. 15976 * Unlike ARP which has to do another bind 15977 * and attach, once we get here we are 15978 * done with NDP. Except in the case of 15979 * ILLF_XRESOLV, in which case we send an 15980 * AR_INTERFACE_UP to the external resolver. 15981 * If all goes well, the ioctl will complete 15982 * in ip_rput(). If there's an error, we 15983 * complete it here. 15984 */ 15985 err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr); 15986 if (err == 0) { 15987 if (ill->ill_flags & ILLF_XRESOLV) { 15988 mutex_enter(&connp->conn_lock); 15989 mutex_enter(&ill->ill_lock); 15990 success = ipsq_pending_mp_add( 15991 connp, ipif, q, mp1, 0); 15992 mutex_exit(&ill->ill_lock); 15993 mutex_exit(&connp->conn_lock); 15994 if (success) { 15995 err = ipif_resolver_up(ipif, 15996 Res_act_initial); 15997 if (err == EINPROGRESS) { 15998 freemsg(mp); 15999 return; 16000 } 16001 ASSERT(err != 0); 16002 mp1 = ipsq_pending_mp_get(ipsq, 16003 &connp); 16004 ASSERT(mp1 != NULL); 16005 } else { 16006 /* conn has started closing */ 16007 err = EINTR; 16008 } 16009 } else { /* Non XRESOLV interface */ 16010 (void) ipif_resolver_up(ipif, 16011 Res_act_initial); 16012 err = ipif_up_done_v6(ipif); 16013 } 16014 } 16015 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 16016 /* 16017 * ARP and other v4 external resolvers. 16018 * Leave the pending mblk intact so that 16019 * the ioctl completes in ip_rput(). 16020 */ 16021 mutex_enter(&connp->conn_lock); 16022 mutex_enter(&ill->ill_lock); 16023 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 16024 mutex_exit(&ill->ill_lock); 16025 mutex_exit(&connp->conn_lock); 16026 if (success) { 16027 err = ipif_resolver_up(ipif, Res_act_initial); 16028 if (err == EINPROGRESS) { 16029 freemsg(mp); 16030 return; 16031 } 16032 ASSERT(err != 0); 16033 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16034 } else { 16035 /* The conn has started closing */ 16036 err = EINTR; 16037 } 16038 } else { 16039 /* 16040 * This one is complete. Reply to pending ioctl. 16041 */ 16042 (void) ipif_resolver_up(ipif, Res_act_initial); 16043 err = ipif_up_done(ipif); 16044 } 16045 16046 if ((err == 0) && (ill->ill_up_ipifs)) { 16047 err = ill_up_ipifs(ill, q, mp1); 16048 if (err == EINPROGRESS) { 16049 freemsg(mp); 16050 return; 16051 } 16052 } 16053 16054 if (ill->ill_up_ipifs) { 16055 ill_group_cleanup(ill); 16056 } 16057 16058 break; 16059 case DL_NOTIFY_IND: { 16060 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 16061 ire_t *ire; 16062 boolean_t need_ire_walk_v4 = B_FALSE; 16063 boolean_t need_ire_walk_v6 = B_FALSE; 16064 16065 switch (notify->dl_notification) { 16066 case DL_NOTE_PHYS_ADDR: 16067 err = ill_set_phys_addr(ill, mp); 16068 break; 16069 16070 case DL_NOTE_FASTPATH_FLUSH: 16071 ill_fastpath_flush(ill); 16072 break; 16073 16074 case DL_NOTE_SDU_SIZE: 16075 /* 16076 * Change the MTU size of the interface, of all 16077 * attached ipif's, and of all relevant ire's. The 16078 * new value's a uint32_t at notify->dl_data. 16079 * Mtu change Vs. new ire creation - protocol below. 16080 * 16081 * a Mark the ipif as IPIF_CHANGING. 16082 * b Set the new mtu in the ipif. 16083 * c Change the ire_max_frag on all affected ires 16084 * d Unmark the IPIF_CHANGING 16085 * 16086 * To see how the protocol works, assume an interface 16087 * route is also being added simultaneously by 16088 * ip_rt_add and let 'ipif' be the ipif referenced by 16089 * the ire. If the ire is created before step a, 16090 * it will be cleaned up by step c. If the ire is 16091 * created after step d, it will see the new value of 16092 * ipif_mtu. Any attempt to create the ire between 16093 * steps a to d will fail because of the IPIF_CHANGING 16094 * flag. Note that ire_create() is passed a pointer to 16095 * the ipif_mtu, and not the value. During ire_add 16096 * under the bucket lock, the ire_max_frag of the 16097 * new ire being created is set from the ipif/ire from 16098 * which it is being derived. 16099 */ 16100 mutex_enter(&ill->ill_lock); 16101 ill->ill_max_frag = (uint_t)notify->dl_data; 16102 16103 /* 16104 * If an SIOCSLIFLNKINFO has changed the ill_max_mtu 16105 * leave it alone 16106 */ 16107 if (ill->ill_mtu_userspecified) { 16108 mutex_exit(&ill->ill_lock); 16109 break; 16110 } 16111 ill->ill_max_mtu = ill->ill_max_frag; 16112 if (ill->ill_isv6) { 16113 if (ill->ill_max_mtu < IPV6_MIN_MTU) 16114 ill->ill_max_mtu = IPV6_MIN_MTU; 16115 } else { 16116 if (ill->ill_max_mtu < IP_MIN_MTU) 16117 ill->ill_max_mtu = IP_MIN_MTU; 16118 } 16119 for (ipif = ill->ill_ipif; ipif != NULL; 16120 ipif = ipif->ipif_next) { 16121 /* 16122 * Don't override the mtu if the user 16123 * has explicitly set it. 16124 */ 16125 if (ipif->ipif_flags & IPIF_FIXEDMTU) 16126 continue; 16127 ipif->ipif_mtu = (uint_t)notify->dl_data; 16128 if (ipif->ipif_isv6) 16129 ire = ipif_to_ire_v6(ipif); 16130 else 16131 ire = ipif_to_ire(ipif); 16132 if (ire != NULL) { 16133 ire->ire_max_frag = ipif->ipif_mtu; 16134 ire_refrele(ire); 16135 } 16136 if (ipif->ipif_flags & IPIF_UP) { 16137 if (ill->ill_isv6) 16138 need_ire_walk_v6 = B_TRUE; 16139 else 16140 need_ire_walk_v4 = B_TRUE; 16141 } 16142 } 16143 mutex_exit(&ill->ill_lock); 16144 if (need_ire_walk_v4) 16145 ire_walk_v4(ill_mtu_change, (char *)ill, 16146 ALL_ZONES, ipst); 16147 if (need_ire_walk_v6) 16148 ire_walk_v6(ill_mtu_change, (char *)ill, 16149 ALL_ZONES, ipst); 16150 break; 16151 case DL_NOTE_LINK_UP: 16152 case DL_NOTE_LINK_DOWN: { 16153 /* 16154 * We are writer. ill / phyint / ipsq assocs stable. 16155 * The RUNNING flag reflects the state of the link. 16156 */ 16157 phyint_t *phyint = ill->ill_phyint; 16158 uint64_t new_phyint_flags; 16159 boolean_t changed = B_FALSE; 16160 boolean_t went_up; 16161 16162 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 16163 mutex_enter(&phyint->phyint_lock); 16164 new_phyint_flags = went_up ? 16165 phyint->phyint_flags | PHYI_RUNNING : 16166 phyint->phyint_flags & ~PHYI_RUNNING; 16167 if (new_phyint_flags != phyint->phyint_flags) { 16168 phyint->phyint_flags = new_phyint_flags; 16169 changed = B_TRUE; 16170 } 16171 mutex_exit(&phyint->phyint_lock); 16172 /* 16173 * ill_restart_dad handles the DAD restart and routing 16174 * socket notification logic. 16175 */ 16176 if (changed) { 16177 ill_restart_dad(phyint->phyint_illv4, went_up); 16178 ill_restart_dad(phyint->phyint_illv6, went_up); 16179 } 16180 break; 16181 } 16182 case DL_NOTE_PROMISC_ON_PHYS: 16183 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 16184 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 16185 mutex_enter(&ill->ill_lock); 16186 ill->ill_promisc_on_phys = B_TRUE; 16187 mutex_exit(&ill->ill_lock); 16188 break; 16189 case DL_NOTE_PROMISC_OFF_PHYS: 16190 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 16191 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 16192 mutex_enter(&ill->ill_lock); 16193 ill->ill_promisc_on_phys = B_FALSE; 16194 mutex_exit(&ill->ill_lock); 16195 break; 16196 case DL_NOTE_CAPAB_RENEG: 16197 /* 16198 * Something changed on the driver side. 16199 * It wants us to renegotiate the capabilities 16200 * on this ill. The most likely cause is the 16201 * aggregation interface under us where a 16202 * port got added or went away. 16203 * 16204 * We reset the capabilities and set the 16205 * state to IDS_RENG so that when the ack 16206 * comes back, we can start the 16207 * renegotiation process. 16208 */ 16209 ill_capability_reset(ill); 16210 ill->ill_dlpi_capab_state = IDS_RENEG; 16211 break; 16212 default: 16213 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 16214 "type 0x%x for DL_NOTIFY_IND\n", 16215 notify->dl_notification)); 16216 break; 16217 } 16218 16219 /* 16220 * As this is an asynchronous operation, we 16221 * should not call ill_dlpi_done 16222 */ 16223 break; 16224 } 16225 case DL_NOTIFY_ACK: { 16226 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 16227 16228 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 16229 ill->ill_note_link = 1; 16230 ill_dlpi_done(ill, DL_NOTIFY_REQ); 16231 break; 16232 } 16233 case DL_PHYS_ADDR_ACK: { 16234 /* 16235 * As part of plumbing the interface via SIOCSLIFNAME, 16236 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, 16237 * whose answers we receive here. As each answer is received, 16238 * we call ill_dlpi_done() to dispatch the next request as 16239 * we're processing the current one. Once all answers have 16240 * been received, we use ipsq_pending_mp_get() to dequeue the 16241 * outstanding IOCTL and reply to it. (Because ill_dl_phys() 16242 * is invoked from an ill queue, conn_oper_pending_ill is not 16243 * available, but we know the ioctl is pending on ill_wq.) 16244 */ 16245 uint_t paddrlen, paddroff; 16246 16247 paddrreq = ill->ill_phys_addr_pend; 16248 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; 16249 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; 16250 16251 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 16252 if (paddrreq == DL_IPV6_TOKEN) { 16253 /* 16254 * bcopy to low-order bits of ill_token 16255 * 16256 * XXX Temporary hack - currently, all known tokens 16257 * are 64 bits, so I'll cheat for the moment. 16258 */ 16259 bcopy(mp->b_rptr + paddroff, 16260 &ill->ill_token.s6_addr32[2], paddrlen); 16261 ill->ill_token_length = paddrlen; 16262 break; 16263 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 16264 ASSERT(ill->ill_nd_lla_mp == NULL); 16265 ill_set_ndmp(ill, mp, paddroff, paddrlen); 16266 mp = NULL; 16267 break; 16268 } 16269 16270 ASSERT(paddrreq == DL_CURR_PHYS_ADDR); 16271 ASSERT(ill->ill_phys_addr_mp == NULL); 16272 if (!ill->ill_ifname_pending) 16273 break; 16274 ill->ill_ifname_pending = 0; 16275 if (!ioctl_aborted) 16276 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16277 if (mp1 != NULL) { 16278 ASSERT(connp == NULL); 16279 q = ill->ill_wq; 16280 } 16281 /* 16282 * If any error acks received during the plumbing sequence, 16283 * ill_ifname_pending_err will be set. Break out and send up 16284 * the error to the pending ioctl. 16285 */ 16286 if (ill->ill_ifname_pending_err != 0) { 16287 err = ill->ill_ifname_pending_err; 16288 ill->ill_ifname_pending_err = 0; 16289 break; 16290 } 16291 16292 ill->ill_phys_addr_mp = mp; 16293 ill->ill_phys_addr = mp->b_rptr + paddroff; 16294 mp = NULL; 16295 16296 /* 16297 * If paddrlen is zero, the DLPI provider doesn't support 16298 * physical addresses. The other two tests were historical 16299 * workarounds for bugs in our former PPP implementation, but 16300 * now other things have grown dependencies on them -- e.g., 16301 * the tun module specifies a dl_addr_length of zero in its 16302 * DL_BIND_ACK, but then specifies an incorrect value in its 16303 * DL_PHYS_ADDR_ACK. These bogus checks need to be removed, 16304 * but only after careful testing ensures that all dependent 16305 * broken DLPI providers have been fixed. 16306 */ 16307 if (paddrlen == 0 || ill->ill_phys_addr_length == 0 || 16308 ill->ill_phys_addr_length == IP_ADDR_LEN) { 16309 ill->ill_phys_addr = NULL; 16310 } else if (paddrlen != ill->ill_phys_addr_length) { 16311 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", 16312 paddrlen, ill->ill_phys_addr_length)); 16313 err = EINVAL; 16314 break; 16315 } 16316 16317 if (ill->ill_nd_lla_mp == NULL) { 16318 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { 16319 err = ENOMEM; 16320 break; 16321 } 16322 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); 16323 } 16324 16325 /* 16326 * Set the interface token. If the zeroth interface address 16327 * is unspecified, then set it to the link local address. 16328 */ 16329 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 16330 (void) ill_setdefaulttoken(ill); 16331 16332 ASSERT(ill->ill_ipif->ipif_id == 0); 16333 if (ipif != NULL && 16334 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { 16335 in6_addr_t ov6addr; 16336 16337 ov6addr = ipif->ipif_v6lcl_addr; 16338 (void) ipif_setlinklocal(ipif); 16339 sctp_update_ipif_addr(ipif, ov6addr); 16340 } 16341 break; 16342 } 16343 case DL_OK_ACK: 16344 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 16345 dlpi_prim_str((int)dloa->dl_correct_primitive), 16346 dloa->dl_correct_primitive)); 16347 switch (dloa->dl_correct_primitive) { 16348 case DL_UNBIND_REQ: 16349 case DL_ATTACH_REQ: 16350 case DL_DETACH_REQ: 16351 ill_dlpi_done(ill, dloa->dl_correct_primitive); 16352 break; 16353 } 16354 break; 16355 default: 16356 break; 16357 } 16358 16359 freemsg(mp); 16360 if (mp1 != NULL) { 16361 /* 16362 * The operation must complete without EINPROGRESS 16363 * since ipsq_pending_mp_get() has removed the mblk 16364 * from ipsq_pending_mp. Otherwise, the operation 16365 * will be stuck forever in the ipsq. 16366 */ 16367 ASSERT(err != EINPROGRESS); 16368 16369 switch (ipsq->ipsq_current_ioctl) { 16370 case 0: 16371 ipsq_current_finish(ipsq); 16372 break; 16373 16374 case SIOCLIFADDIF: 16375 case SIOCSLIFNAME: 16376 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 16377 break; 16378 16379 default: 16380 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 16381 break; 16382 } 16383 } 16384 } 16385 16386 /* 16387 * ip_rput_other is called by ip_rput to handle messages modifying the global 16388 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 16389 */ 16390 /* ARGSUSED */ 16391 void 16392 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 16393 { 16394 ill_t *ill; 16395 struct iocblk *iocp; 16396 mblk_t *mp1; 16397 conn_t *connp = NULL; 16398 16399 ip1dbg(("ip_rput_other ")); 16400 ill = (ill_t *)q->q_ptr; 16401 /* 16402 * This routine is not a writer in the case of SIOCGTUNPARAM 16403 * in which case ipsq is NULL. 16404 */ 16405 if (ipsq != NULL) { 16406 ASSERT(IAM_WRITER_IPSQ(ipsq)); 16407 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 16408 } 16409 16410 switch (mp->b_datap->db_type) { 16411 case M_ERROR: 16412 case M_HANGUP: 16413 /* 16414 * The device has a problem. We force the ILL down. It can 16415 * be brought up again manually using SIOCSIFFLAGS (via 16416 * ifconfig or equivalent). 16417 */ 16418 ASSERT(ipsq != NULL); 16419 if (mp->b_rptr < mp->b_wptr) 16420 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 16421 if (ill->ill_error == 0) 16422 ill->ill_error = ENXIO; 16423 if (!ill_down_start(q, mp)) 16424 return; 16425 ipif_all_down_tail(ipsq, q, mp, NULL); 16426 break; 16427 case M_IOCACK: 16428 iocp = (struct iocblk *)mp->b_rptr; 16429 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 16430 switch (iocp->ioc_cmd) { 16431 case SIOCSTUNPARAM: 16432 case OSIOCSTUNPARAM: 16433 ASSERT(ipsq != NULL); 16434 /* 16435 * Finish socket ioctl passed through to tun. 16436 * We should have an IOCTL waiting on this. 16437 */ 16438 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16439 if (ill->ill_isv6) { 16440 struct iftun_req *ta; 16441 16442 /* 16443 * if a source or destination is 16444 * being set, try and set the link 16445 * local address for the tunnel 16446 */ 16447 ta = (struct iftun_req *)mp->b_cont-> 16448 b_cont->b_rptr; 16449 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 16450 ipif_set_tun_llink(ill, ta); 16451 } 16452 16453 } 16454 if (mp1 != NULL) { 16455 /* 16456 * Now copy back the b_next/b_prev used by 16457 * mi code for the mi_copy* functions. 16458 * See ip_sioctl_tunparam() for the reason. 16459 * Also protect against missing b_cont. 16460 */ 16461 if (mp->b_cont != NULL) { 16462 mp->b_cont->b_next = 16463 mp1->b_cont->b_next; 16464 mp->b_cont->b_prev = 16465 mp1->b_cont->b_prev; 16466 } 16467 inet_freemsg(mp1); 16468 ASSERT(connp != NULL); 16469 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16470 iocp->ioc_error, NO_COPYOUT, ipsq); 16471 } else { 16472 ASSERT(connp == NULL); 16473 putnext(q, mp); 16474 } 16475 break; 16476 case SIOCGTUNPARAM: 16477 case OSIOCGTUNPARAM: 16478 /* 16479 * This is really M_IOCDATA from the tunnel driver. 16480 * convert back and complete the ioctl. 16481 * We should have an IOCTL waiting on this. 16482 */ 16483 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 16484 if (mp1) { 16485 /* 16486 * Now copy back the b_next/b_prev used by 16487 * mi code for the mi_copy* functions. 16488 * See ip_sioctl_tunparam() for the reason. 16489 * Also protect against missing b_cont. 16490 */ 16491 if (mp->b_cont != NULL) { 16492 mp->b_cont->b_next = 16493 mp1->b_cont->b_next; 16494 mp->b_cont->b_prev = 16495 mp1->b_cont->b_prev; 16496 } 16497 inet_freemsg(mp1); 16498 if (iocp->ioc_error == 0) 16499 mp->b_datap->db_type = M_IOCDATA; 16500 ASSERT(connp != NULL); 16501 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16502 iocp->ioc_error, COPYOUT, NULL); 16503 } else { 16504 ASSERT(connp == NULL); 16505 putnext(q, mp); 16506 } 16507 break; 16508 default: 16509 break; 16510 } 16511 break; 16512 case M_IOCNAK: 16513 iocp = (struct iocblk *)mp->b_rptr; 16514 16515 switch (iocp->ioc_cmd) { 16516 int mode; 16517 16518 case DL_IOC_HDR_INFO: 16519 /* 16520 * If this was the first attempt turn of the 16521 * fastpath probing. 16522 */ 16523 mutex_enter(&ill->ill_lock); 16524 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 16525 ill->ill_dlpi_fastpath_state = IDS_FAILED; 16526 mutex_exit(&ill->ill_lock); 16527 ill_fastpath_nack(ill); 16528 ip1dbg(("ip_rput: DLPI fastpath off on " 16529 "interface %s\n", 16530 ill->ill_name)); 16531 } else { 16532 mutex_exit(&ill->ill_lock); 16533 } 16534 freemsg(mp); 16535 break; 16536 case SIOCSTUNPARAM: 16537 case OSIOCSTUNPARAM: 16538 ASSERT(ipsq != NULL); 16539 /* 16540 * Finish socket ioctl passed through to tun 16541 * We should have an IOCTL waiting on this. 16542 */ 16543 /* FALLTHRU */ 16544 case SIOCGTUNPARAM: 16545 case OSIOCGTUNPARAM: 16546 /* 16547 * This is really M_IOCDATA from the tunnel driver. 16548 * convert back and complete the ioctl. 16549 * We should have an IOCTL waiting on this. 16550 */ 16551 if (iocp->ioc_cmd == SIOCGTUNPARAM || 16552 iocp->ioc_cmd == OSIOCGTUNPARAM) { 16553 mp1 = ill_pending_mp_get(ill, &connp, 16554 iocp->ioc_id); 16555 mode = COPYOUT; 16556 ipsq = NULL; 16557 } else { 16558 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16559 mode = NO_COPYOUT; 16560 } 16561 if (mp1 != NULL) { 16562 /* 16563 * Now copy back the b_next/b_prev used by 16564 * mi code for the mi_copy* functions. 16565 * See ip_sioctl_tunparam() for the reason. 16566 * Also protect against missing b_cont. 16567 */ 16568 if (mp->b_cont != NULL) { 16569 mp->b_cont->b_next = 16570 mp1->b_cont->b_next; 16571 mp->b_cont->b_prev = 16572 mp1->b_cont->b_prev; 16573 } 16574 inet_freemsg(mp1); 16575 if (iocp->ioc_error == 0) 16576 iocp->ioc_error = EINVAL; 16577 ASSERT(connp != NULL); 16578 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16579 iocp->ioc_error, mode, ipsq); 16580 } else { 16581 ASSERT(connp == NULL); 16582 putnext(q, mp); 16583 } 16584 break; 16585 default: 16586 break; 16587 } 16588 default: 16589 break; 16590 } 16591 } 16592 16593 /* 16594 * NOTE : This function does not ire_refrele the ire argument passed in. 16595 * 16596 * IPQoS notes 16597 * IP policy is invoked twice for a forwarded packet, once on the read side 16598 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 16599 * enabled. An additional parameter, in_ill, has been added for this purpose. 16600 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 16601 * because ip_mroute drops this information. 16602 * 16603 */ 16604 void 16605 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 16606 { 16607 uint32_t pkt_len; 16608 queue_t *q; 16609 uint32_t sum; 16610 #define rptr ((uchar_t *)ipha) 16611 uint32_t max_frag; 16612 uint32_t ill_index; 16613 ill_t *out_ill; 16614 mib2_ipIfStatsEntry_t *mibptr; 16615 ip_stack_t *ipst = in_ill->ill_ipst; 16616 16617 /* Get the ill_index of the incoming ILL */ 16618 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 16619 mibptr = (in_ill != NULL) ? in_ill->ill_ip_mib : &ipst->ips_ip_mib; 16620 16621 /* Initiate Read side IPPF processing */ 16622 if (IPP_ENABLED(IPP_FWD_IN, ipst)) { 16623 ip_process(IPP_FWD_IN, &mp, ill_index); 16624 if (mp == NULL) { 16625 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 16626 "during IPPF processing\n")); 16627 return; 16628 } 16629 } 16630 16631 pkt_len = ntohs(ipha->ipha_length); 16632 16633 /* Adjust the checksum to reflect the ttl decrement. */ 16634 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 16635 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 16636 16637 if (ipha->ipha_ttl-- <= 1) { 16638 if (ip_csum_hdr(ipha)) { 16639 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16640 goto drop_pkt; 16641 } 16642 /* 16643 * Note: ire_stq this will be NULL for multicast 16644 * datagrams using the long path through arp (the IRE 16645 * is not an IRE_CACHE). This should not cause 16646 * problems since we don't generate ICMP errors for 16647 * multicast packets. 16648 */ 16649 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16650 q = ire->ire_stq; 16651 if (q != NULL) { 16652 /* Sent by forwarding path, and router is global zone */ 16653 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, 16654 GLOBAL_ZONEID, ipst); 16655 } else 16656 freemsg(mp); 16657 return; 16658 } 16659 16660 /* 16661 * Don't forward if the interface is down 16662 */ 16663 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 16664 BUMP_MIB(mibptr, ipIfStatsInDiscards); 16665 ip2dbg(("ip_rput_forward:interface is down\n")); 16666 goto drop_pkt; 16667 } 16668 16669 /* Get the ill_index of the outgoing ILL */ 16670 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 16671 16672 out_ill = ire->ire_ipif->ipif_ill; 16673 16674 DTRACE_PROBE4(ip4__forwarding__start, 16675 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16676 16677 FW_HOOKS(ipst->ips_ip4_forwarding_event, 16678 ipst->ips_ipv4firewall_forwarding, 16679 in_ill, out_ill, ipha, mp, mp, ipst); 16680 16681 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 16682 16683 if (mp == NULL) 16684 return; 16685 pkt_len = ntohs(ipha->ipha_length); 16686 16687 if (is_system_labeled()) { 16688 mblk_t *mp1; 16689 16690 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 16691 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16692 goto drop_pkt; 16693 } 16694 /* Size may have changed */ 16695 mp = mp1; 16696 ipha = (ipha_t *)mp->b_rptr; 16697 pkt_len = ntohs(ipha->ipha_length); 16698 } 16699 16700 /* Check if there are options to update */ 16701 if (!IS_SIMPLE_IPH(ipha)) { 16702 if (ip_csum_hdr(ipha)) { 16703 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16704 goto drop_pkt; 16705 } 16706 if (ip_rput_forward_options(mp, ipha, ire, ipst)) { 16707 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16708 return; 16709 } 16710 16711 ipha->ipha_hdr_checksum = 0; 16712 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 16713 } 16714 max_frag = ire->ire_max_frag; 16715 if (pkt_len > max_frag) { 16716 /* 16717 * It needs fragging on its way out. We haven't 16718 * verified the header checksum yet. Since we 16719 * are going to put a surely good checksum in the 16720 * outgoing header, we have to make sure that it 16721 * was good coming in. 16722 */ 16723 if (ip_csum_hdr(ipha)) { 16724 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16725 goto drop_pkt; 16726 } 16727 /* Initiate Write side IPPF processing */ 16728 if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { 16729 ip_process(IPP_FWD_OUT, &mp, ill_index); 16730 if (mp == NULL) { 16731 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 16732 " during IPPF processing\n")); 16733 return; 16734 } 16735 } 16736 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID, ipst); 16737 ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); 16738 return; 16739 } 16740 16741 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 16742 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16743 FW_HOOKS(ipst->ips_ip4_physical_out_event, 16744 ipst->ips_ipv4firewall_physical_out, 16745 NULL, out_ill, ipha, mp, mp, ipst); 16746 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 16747 if (mp == NULL) 16748 return; 16749 16750 mp->b_prev = (mblk_t *)IPP_FWD_OUT; 16751 ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); 16752 (void) ip_xmit_v4(mp, ire, NULL, B_FALSE); 16753 /* ip_xmit_v4 always consumes the packet */ 16754 return; 16755 16756 drop_pkt:; 16757 ip1dbg(("ip_rput_forward: drop pkt\n")); 16758 freemsg(mp); 16759 #undef rptr 16760 } 16761 16762 void 16763 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 16764 { 16765 ire_t *ire; 16766 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 16767 16768 ASSERT(!ipif->ipif_isv6); 16769 /* 16770 * Find an IRE which matches the destination and the outgoing 16771 * queue in the cache table. All we need is an IRE_CACHE which 16772 * is pointing at ipif->ipif_ill. If it is part of some ill group, 16773 * then it is enough to have some IRE_CACHE in the group. 16774 */ 16775 if (ipif->ipif_flags & IPIF_POINTOPOINT) 16776 dst = ipif->ipif_pp_dst_addr; 16777 16778 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MBLK_GETLABEL(mp), 16779 MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR, ipst); 16780 if (ire == NULL) { 16781 /* 16782 * Mark this packet to make it be delivered to 16783 * ip_rput_forward after the new ire has been 16784 * created. 16785 */ 16786 mp->b_prev = NULL; 16787 mp->b_next = mp; 16788 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 16789 NULL, 0, GLOBAL_ZONEID, &zero_info); 16790 } else { 16791 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 16792 IRE_REFRELE(ire); 16793 } 16794 } 16795 16796 /* Update any source route, record route or timestamp options */ 16797 static int 16798 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) 16799 { 16800 ipoptp_t opts; 16801 uchar_t *opt; 16802 uint8_t optval; 16803 uint8_t optlen; 16804 ipaddr_t dst; 16805 uint32_t ts; 16806 ire_t *dst_ire = NULL; 16807 ire_t *tmp_ire = NULL; 16808 timestruc_t now; 16809 16810 ip2dbg(("ip_rput_forward_options\n")); 16811 dst = ipha->ipha_dst; 16812 for (optval = ipoptp_first(&opts, ipha); 16813 optval != IPOPT_EOL; 16814 optval = ipoptp_next(&opts)) { 16815 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 16816 opt = opts.ipoptp_cur; 16817 optlen = opts.ipoptp_len; 16818 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 16819 optval, opts.ipoptp_len)); 16820 switch (optval) { 16821 uint32_t off; 16822 case IPOPT_SSRR: 16823 case IPOPT_LSRR: 16824 /* Check if adminstratively disabled */ 16825 if (!ipst->ips_ip_forward_src_routed) { 16826 if (ire->ire_stq != NULL) { 16827 /* 16828 * Sent by forwarding path, and router 16829 * is global zone 16830 */ 16831 icmp_unreachable(ire->ire_stq, mp, 16832 ICMP_SOURCE_ROUTE_FAILED, 16833 GLOBAL_ZONEID, ipst); 16834 } else { 16835 ip0dbg(("ip_rput_forward_options: " 16836 "unable to send unreach\n")); 16837 freemsg(mp); 16838 } 16839 return (-1); 16840 } 16841 16842 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16843 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16844 if (dst_ire == NULL) { 16845 /* 16846 * Must be partial since ip_rput_options 16847 * checked for strict. 16848 */ 16849 break; 16850 } 16851 off = opt[IPOPT_OFFSET]; 16852 off--; 16853 redo_srr: 16854 if (optlen < IP_ADDR_LEN || 16855 off > optlen - IP_ADDR_LEN) { 16856 /* End of source route */ 16857 ip1dbg(( 16858 "ip_rput_forward_options: end of SR\n")); 16859 ire_refrele(dst_ire); 16860 break; 16861 } 16862 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16863 bcopy(&ire->ire_src_addr, (char *)opt + off, 16864 IP_ADDR_LEN); 16865 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 16866 ntohl(dst))); 16867 16868 /* 16869 * Check if our address is present more than 16870 * once as consecutive hops in source route. 16871 */ 16872 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16873 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16874 if (tmp_ire != NULL) { 16875 ire_refrele(tmp_ire); 16876 off += IP_ADDR_LEN; 16877 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16878 goto redo_srr; 16879 } 16880 ipha->ipha_dst = dst; 16881 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16882 ire_refrele(dst_ire); 16883 break; 16884 case IPOPT_RR: 16885 off = opt[IPOPT_OFFSET]; 16886 off--; 16887 if (optlen < IP_ADDR_LEN || 16888 off > optlen - IP_ADDR_LEN) { 16889 /* No more room - ignore */ 16890 ip1dbg(( 16891 "ip_rput_forward_options: end of RR\n")); 16892 break; 16893 } 16894 bcopy(&ire->ire_src_addr, (char *)opt + off, 16895 IP_ADDR_LEN); 16896 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16897 break; 16898 case IPOPT_TS: 16899 /* Insert timestamp if there is room */ 16900 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16901 case IPOPT_TS_TSONLY: 16902 off = IPOPT_TS_TIMELEN; 16903 break; 16904 case IPOPT_TS_PRESPEC: 16905 case IPOPT_TS_PRESPEC_RFC791: 16906 /* Verify that the address matched */ 16907 off = opt[IPOPT_OFFSET] - 1; 16908 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16909 dst_ire = ire_ctable_lookup(dst, 0, 16910 IRE_LOCAL, NULL, ALL_ZONES, NULL, 16911 MATCH_IRE_TYPE, ipst); 16912 if (dst_ire == NULL) { 16913 /* Not for us */ 16914 break; 16915 } 16916 ire_refrele(dst_ire); 16917 /* FALLTHRU */ 16918 case IPOPT_TS_TSANDADDR: 16919 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16920 break; 16921 default: 16922 /* 16923 * ip_*put_options should have already 16924 * dropped this packet. 16925 */ 16926 cmn_err(CE_PANIC, "ip_rput_forward_options: " 16927 "unknown IT - bug in ip_rput_options?\n"); 16928 return (0); /* Keep "lint" happy */ 16929 } 16930 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 16931 /* Increase overflow counter */ 16932 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 16933 opt[IPOPT_POS_OV_FLG] = 16934 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 16935 (off << 4)); 16936 break; 16937 } 16938 off = opt[IPOPT_OFFSET] - 1; 16939 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16940 case IPOPT_TS_PRESPEC: 16941 case IPOPT_TS_PRESPEC_RFC791: 16942 case IPOPT_TS_TSANDADDR: 16943 bcopy(&ire->ire_src_addr, 16944 (char *)opt + off, IP_ADDR_LEN); 16945 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16946 /* FALLTHRU */ 16947 case IPOPT_TS_TSONLY: 16948 off = opt[IPOPT_OFFSET] - 1; 16949 /* Compute # of milliseconds since midnight */ 16950 gethrestime(&now); 16951 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 16952 now.tv_nsec / (NANOSEC / MILLISEC); 16953 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 16954 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 16955 break; 16956 } 16957 break; 16958 } 16959 } 16960 return (0); 16961 } 16962 16963 /* 16964 * This is called after processing at least one of AH/ESP headers. 16965 * 16966 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 16967 * the actual, physical interface on which the packet was received, 16968 * but, when ip_strict_dst_multihoming is set to 1, could be the 16969 * interface which had the ipha_dst configured when the packet went 16970 * through ip_rput. The ill_index corresponding to the recv_ill 16971 * is saved in ipsec_in_rill_index 16972 */ 16973 void 16974 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 16975 { 16976 mblk_t *mp; 16977 ipaddr_t dst; 16978 in6_addr_t *v6dstp; 16979 ipha_t *ipha; 16980 ip6_t *ip6h; 16981 ipsec_in_t *ii; 16982 boolean_t ill_need_rele = B_FALSE; 16983 boolean_t rill_need_rele = B_FALSE; 16984 boolean_t ire_need_rele = B_FALSE; 16985 netstack_t *ns; 16986 ip_stack_t *ipst; 16987 16988 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 16989 ASSERT(ii->ipsec_in_ill_index != 0); 16990 ns = ii->ipsec_in_ns; 16991 ASSERT(ii->ipsec_in_ns != NULL); 16992 ipst = ns->netstack_ip; 16993 16994 mp = ipsec_mp->b_cont; 16995 ASSERT(mp != NULL); 16996 16997 16998 if (ill == NULL) { 16999 ASSERT(recv_ill == NULL); 17000 /* 17001 * We need to get the original queue on which ip_rput_local 17002 * or ip_rput_data_v6 was called. 17003 */ 17004 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 17005 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL, ipst); 17006 ill_need_rele = B_TRUE; 17007 17008 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 17009 recv_ill = ill_lookup_on_ifindex( 17010 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 17011 NULL, NULL, NULL, NULL, ipst); 17012 rill_need_rele = B_TRUE; 17013 } else { 17014 recv_ill = ill; 17015 } 17016 17017 if ((ill == NULL) || (recv_ill == NULL)) { 17018 ip0dbg(("ip_fanout_proto_again: interface " 17019 "disappeared\n")); 17020 if (ill != NULL) 17021 ill_refrele(ill); 17022 if (recv_ill != NULL) 17023 ill_refrele(recv_ill); 17024 freemsg(ipsec_mp); 17025 return; 17026 } 17027 } 17028 17029 ASSERT(ill != NULL && recv_ill != NULL); 17030 17031 if (mp->b_datap->db_type == M_CTL) { 17032 /* 17033 * AH/ESP is returning the ICMP message after 17034 * removing their headers. Fanout again till 17035 * it gets to the right protocol. 17036 */ 17037 if (ii->ipsec_in_v4) { 17038 icmph_t *icmph; 17039 int iph_hdr_length; 17040 int hdr_length; 17041 17042 ipha = (ipha_t *)mp->b_rptr; 17043 iph_hdr_length = IPH_HDR_LENGTH(ipha); 17044 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 17045 ipha = (ipha_t *)&icmph[1]; 17046 hdr_length = IPH_HDR_LENGTH(ipha); 17047 /* 17048 * icmp_inbound_error_fanout may need to do pullupmsg. 17049 * Reset the type to M_DATA. 17050 */ 17051 mp->b_datap->db_type = M_DATA; 17052 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 17053 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 17054 B_FALSE, ill, ii->ipsec_in_zoneid); 17055 } else { 17056 icmp6_t *icmp6; 17057 int hdr_length; 17058 17059 ip6h = (ip6_t *)mp->b_rptr; 17060 /* Don't call hdr_length_v6() unless you have to. */ 17061 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 17062 hdr_length = ip_hdr_length_v6(mp, ip6h); 17063 else 17064 hdr_length = IPV6_HDR_LEN; 17065 17066 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 17067 /* 17068 * icmp_inbound_error_fanout_v6 may need to do 17069 * pullupmsg. Reset the type to M_DATA. 17070 */ 17071 mp->b_datap->db_type = M_DATA; 17072 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 17073 ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); 17074 } 17075 if (ill_need_rele) 17076 ill_refrele(ill); 17077 if (rill_need_rele) 17078 ill_refrele(recv_ill); 17079 return; 17080 } 17081 17082 if (ii->ipsec_in_v4) { 17083 ipha = (ipha_t *)mp->b_rptr; 17084 dst = ipha->ipha_dst; 17085 if (CLASSD(dst)) { 17086 /* 17087 * Multicast has to be delivered to all streams. 17088 */ 17089 dst = INADDR_BROADCAST; 17090 } 17091 17092 if (ire == NULL) { 17093 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, 17094 MBLK_GETLABEL(mp), ipst); 17095 if (ire == NULL) { 17096 if (ill_need_rele) 17097 ill_refrele(ill); 17098 if (rill_need_rele) 17099 ill_refrele(recv_ill); 17100 ip1dbg(("ip_fanout_proto_again: " 17101 "IRE not found")); 17102 freemsg(ipsec_mp); 17103 return; 17104 } 17105 ire_need_rele = B_TRUE; 17106 } 17107 17108 switch (ipha->ipha_protocol) { 17109 case IPPROTO_UDP: 17110 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 17111 recv_ill); 17112 if (ire_need_rele) 17113 ire_refrele(ire); 17114 break; 17115 case IPPROTO_TCP: 17116 if (!ire_need_rele) 17117 IRE_REFHOLD(ire); 17118 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 17119 ire, ipsec_mp, 0, ill->ill_rq, NULL); 17120 IRE_REFRELE(ire); 17121 if (mp != NULL) 17122 squeue_enter_chain(GET_SQUEUE(mp), mp, 17123 mp, 1, SQTAG_IP_PROTO_AGAIN); 17124 break; 17125 case IPPROTO_SCTP: 17126 if (!ire_need_rele) 17127 IRE_REFHOLD(ire); 17128 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 17129 ipsec_mp, 0, ill->ill_rq, dst); 17130 break; 17131 default: 17132 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 17133 recv_ill); 17134 if (ire_need_rele) 17135 ire_refrele(ire); 17136 break; 17137 } 17138 } else { 17139 uint32_t rput_flags = 0; 17140 17141 ip6h = (ip6_t *)mp->b_rptr; 17142 v6dstp = &ip6h->ip6_dst; 17143 /* 17144 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 17145 * address. 17146 * 17147 * Currently, we don't store that state in the IPSEC_IN 17148 * message, and we may need to. 17149 */ 17150 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 17151 IP6_IN_LLMCAST : 0); 17152 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 17153 NULL, NULL); 17154 } 17155 if (ill_need_rele) 17156 ill_refrele(ill); 17157 if (rill_need_rele) 17158 ill_refrele(recv_ill); 17159 } 17160 17161 /* 17162 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 17163 * returns 'true' if there are still fragments left on the queue, in 17164 * which case we restart the timer. 17165 */ 17166 void 17167 ill_frag_timer(void *arg) 17168 { 17169 ill_t *ill = (ill_t *)arg; 17170 boolean_t frag_pending; 17171 ip_stack_t *ipst = ill->ill_ipst; 17172 17173 mutex_enter(&ill->ill_lock); 17174 ASSERT(!ill->ill_fragtimer_executing); 17175 if (ill->ill_state_flags & ILL_CONDEMNED) { 17176 ill->ill_frag_timer_id = 0; 17177 mutex_exit(&ill->ill_lock); 17178 return; 17179 } 17180 ill->ill_fragtimer_executing = 1; 17181 mutex_exit(&ill->ill_lock); 17182 17183 frag_pending = ill_frag_timeout(ill, ipst->ips_ip_g_frag_timeout); 17184 17185 /* 17186 * Restart the timer, if we have fragments pending or if someone 17187 * wanted us to be scheduled again. 17188 */ 17189 mutex_enter(&ill->ill_lock); 17190 ill->ill_fragtimer_executing = 0; 17191 ill->ill_frag_timer_id = 0; 17192 if (frag_pending || ill->ill_fragtimer_needrestart) 17193 ill_frag_timer_start(ill); 17194 mutex_exit(&ill->ill_lock); 17195 } 17196 17197 void 17198 ill_frag_timer_start(ill_t *ill) 17199 { 17200 ip_stack_t *ipst = ill->ill_ipst; 17201 17202 ASSERT(MUTEX_HELD(&ill->ill_lock)); 17203 17204 /* If the ill is closing or opening don't proceed */ 17205 if (ill->ill_state_flags & ILL_CONDEMNED) 17206 return; 17207 17208 if (ill->ill_fragtimer_executing) { 17209 /* 17210 * ill_frag_timer is currently executing. Just record the 17211 * the fact that we want the timer to be restarted. 17212 * ill_frag_timer will post a timeout before it returns, 17213 * ensuring it will be called again. 17214 */ 17215 ill->ill_fragtimer_needrestart = 1; 17216 return; 17217 } 17218 17219 if (ill->ill_frag_timer_id == 0) { 17220 /* 17221 * The timer is neither running nor is the timeout handler 17222 * executing. Post a timeout so that ill_frag_timer will be 17223 * called 17224 */ 17225 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 17226 MSEC_TO_TICK(ipst->ips_ip_g_frag_timo_ms >> 1)); 17227 ill->ill_fragtimer_needrestart = 0; 17228 } 17229 } 17230 17231 /* 17232 * This routine is needed for loopback when forwarding multicasts. 17233 * 17234 * IPQoS Notes: 17235 * IPPF processing is done in fanout routines. 17236 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 17237 * processing for IPSec packets is done when it comes back in clear. 17238 * NOTE : The callers of this function need to do the ire_refrele for the 17239 * ire that is being passed in. 17240 */ 17241 void 17242 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17243 ill_t *recv_ill) 17244 { 17245 ill_t *ill = (ill_t *)q->q_ptr; 17246 uint32_t sum; 17247 uint32_t u1; 17248 uint32_t u2; 17249 int hdr_length; 17250 boolean_t mctl_present; 17251 mblk_t *first_mp = mp; 17252 mblk_t *hada_mp = NULL; 17253 ipha_t *inner_ipha; 17254 ip_stack_t *ipst; 17255 17256 ASSERT(recv_ill != NULL); 17257 ipst = recv_ill->ill_ipst; 17258 17259 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 17260 "ip_rput_locl_start: q %p", q); 17261 17262 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17263 ASSERT(ill != NULL); 17264 17265 17266 #define rptr ((uchar_t *)ipha) 17267 #define iphs ((uint16_t *)ipha) 17268 17269 /* 17270 * no UDP or TCP packet should come here anymore. 17271 */ 17272 ASSERT((ipha->ipha_protocol != IPPROTO_TCP) && 17273 (ipha->ipha_protocol != IPPROTO_UDP)); 17274 17275 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 17276 if (mctl_present && 17277 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 17278 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 17279 17280 /* 17281 * It's an IPsec accelerated packet. 17282 * Keep a pointer to the data attributes around until 17283 * we allocate the ipsec_info_t. 17284 */ 17285 IPSECHW_DEBUG(IPSECHW_PKT, 17286 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 17287 hada_mp = first_mp; 17288 hada_mp->b_cont = NULL; 17289 /* 17290 * Since it is accelerated, it comes directly from 17291 * the ill and the data attributes is followed by 17292 * the packet data. 17293 */ 17294 ASSERT(mp->b_datap->db_type != M_CTL); 17295 first_mp = mp; 17296 mctl_present = B_FALSE; 17297 } 17298 17299 /* 17300 * IF M_CTL is not present, then ipsec_in_is_secure 17301 * should return B_TRUE. There is a case where loopback 17302 * packets has an M_CTL in the front with all the 17303 * IPSEC options set to IPSEC_PREF_NEVER - which means 17304 * ipsec_in_is_secure will return B_FALSE. As loopback 17305 * packets never comes here, it is safe to ASSERT the 17306 * following. 17307 */ 17308 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 17309 17310 17311 /* u1 is # words of IP options */ 17312 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 17313 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 17314 17315 if (u1) { 17316 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 17317 if (hada_mp != NULL) 17318 freemsg(hada_mp); 17319 return; 17320 } 17321 } else { 17322 /* Check the IP header checksum. */ 17323 #define uph ((uint16_t *)ipha) 17324 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 17325 uph[6] + uph[7] + uph[8] + uph[9]; 17326 #undef uph 17327 /* finish doing IP checksum */ 17328 sum = (sum & 0xFFFF) + (sum >> 16); 17329 sum = ~(sum + (sum >> 16)) & 0xFFFF; 17330 /* 17331 * Don't verify header checksum if this packet is coming 17332 * back from AH/ESP as we already did it. 17333 */ 17334 if (!mctl_present && (sum && sum != 0xFFFF)) { 17335 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 17336 goto drop_pkt; 17337 } 17338 } 17339 17340 /* 17341 * Count for SNMP of inbound packets for ire. As ip_proto_input 17342 * might be called more than once for secure packets, count only 17343 * the first time. 17344 */ 17345 if (!mctl_present) { 17346 UPDATE_IB_PKT_COUNT(ire); 17347 ire->ire_last_used_time = lbolt; 17348 } 17349 17350 /* Check for fragmentation offset. */ 17351 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 17352 u1 = u2 & (IPH_MF | IPH_OFFSET); 17353 if (u1) { 17354 /* 17355 * We re-assemble fragments before we do the AH/ESP 17356 * processing. Thus, M_CTL should not be present 17357 * while we are re-assembling. 17358 */ 17359 ASSERT(!mctl_present); 17360 ASSERT(first_mp == mp); 17361 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 17362 return; 17363 } 17364 /* 17365 * Make sure that first_mp points back to mp as 17366 * the mp we came in with could have changed in 17367 * ip_rput_fragment(). 17368 */ 17369 ipha = (ipha_t *)mp->b_rptr; 17370 first_mp = mp; 17371 } 17372 17373 /* 17374 * Clear hardware checksumming flag as it is currently only 17375 * used by TCP and UDP. 17376 */ 17377 DB_CKSUMFLAGS(mp) = 0; 17378 17379 /* Now we have a complete datagram, destined for this machine. */ 17380 u1 = IPH_HDR_LENGTH(ipha); 17381 switch (ipha->ipha_protocol) { 17382 case IPPROTO_ICMP: { 17383 ire_t *ire_zone; 17384 ilm_t *ilm; 17385 mblk_t *mp1; 17386 zoneid_t last_zoneid; 17387 17388 if (CLASSD(ipha->ipha_dst) && 17389 !(recv_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 17390 ASSERT(ire->ire_type == IRE_BROADCAST); 17391 /* 17392 * In the multicast case, applications may have joined 17393 * the group from different zones, so we need to deliver 17394 * the packet to each of them. Loop through the 17395 * multicast memberships structures (ilm) on the receive 17396 * ill and send a copy of the packet up each matching 17397 * one. However, we don't do this for multicasts sent on 17398 * the loopback interface (PHYI_LOOPBACK flag set) as 17399 * they must stay in the sender's zone. 17400 * 17401 * ilm_add_v6() ensures that ilms in the same zone are 17402 * contiguous in the ill_ilm list. We use this property 17403 * to avoid sending duplicates needed when two 17404 * applications in the same zone join the same group on 17405 * different logical interfaces: we ignore the ilm if 17406 * its zoneid is the same as the last matching one. 17407 * In addition, the sending of the packet for 17408 * ire_zoneid is delayed until all of the other ilms 17409 * have been exhausted. 17410 */ 17411 last_zoneid = -1; 17412 ILM_WALKER_HOLD(recv_ill); 17413 for (ilm = recv_ill->ill_ilm; ilm != NULL; 17414 ilm = ilm->ilm_next) { 17415 if ((ilm->ilm_flags & ILM_DELETED) || 17416 ipha->ipha_dst != ilm->ilm_addr || 17417 ilm->ilm_zoneid == last_zoneid || 17418 ilm->ilm_zoneid == ire->ire_zoneid || 17419 ilm->ilm_zoneid == ALL_ZONES || 17420 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 17421 continue; 17422 mp1 = ip_copymsg(first_mp); 17423 if (mp1 == NULL) 17424 continue; 17425 icmp_inbound(q, mp1, B_TRUE, ill, 17426 0, sum, mctl_present, B_TRUE, 17427 recv_ill, ilm->ilm_zoneid); 17428 last_zoneid = ilm->ilm_zoneid; 17429 } 17430 ILM_WALKER_RELE(recv_ill); 17431 } else if (ire->ire_type == IRE_BROADCAST) { 17432 /* 17433 * In the broadcast case, there may be many zones 17434 * which need a copy of the packet delivered to them. 17435 * There is one IRE_BROADCAST per broadcast address 17436 * and per zone; we walk those using a helper function. 17437 * In addition, the sending of the packet for ire is 17438 * delayed until all of the other ires have been 17439 * processed. 17440 */ 17441 IRB_REFHOLD(ire->ire_bucket); 17442 ire_zone = NULL; 17443 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 17444 ire)) != NULL) { 17445 mp1 = ip_copymsg(first_mp); 17446 if (mp1 == NULL) 17447 continue; 17448 17449 UPDATE_IB_PKT_COUNT(ire_zone); 17450 ire_zone->ire_last_used_time = lbolt; 17451 icmp_inbound(q, mp1, B_TRUE, ill, 17452 0, sum, mctl_present, B_TRUE, 17453 recv_ill, ire_zone->ire_zoneid); 17454 } 17455 IRB_REFRELE(ire->ire_bucket); 17456 } 17457 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 17458 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 17459 ire->ire_zoneid); 17460 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17461 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 17462 return; 17463 } 17464 case IPPROTO_IGMP: 17465 /* 17466 * If we are not willing to accept IGMP packets in clear, 17467 * then check with global policy. 17468 */ 17469 if (ipst->ips_igmp_accept_clear_messages == 0) { 17470 first_mp = ipsec_check_global_policy(first_mp, NULL, 17471 ipha, NULL, mctl_present, ipst->ips_netstack); 17472 if (first_mp == NULL) 17473 return; 17474 } 17475 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17476 freemsg(first_mp); 17477 ip1dbg(("ip_proto_input: zone all cannot accept raw")); 17478 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17479 return; 17480 } 17481 if ((mp = igmp_input(q, mp, ill)) == NULL) { 17482 /* Bad packet - discarded by igmp_input */ 17483 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17484 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 17485 if (mctl_present) 17486 freeb(first_mp); 17487 return; 17488 } 17489 /* 17490 * igmp_input() may have returned the pulled up message. 17491 * So first_mp and ipha need to be reinitialized. 17492 */ 17493 ipha = (ipha_t *)mp->b_rptr; 17494 if (mctl_present) 17495 first_mp->b_cont = mp; 17496 else 17497 first_mp = mp; 17498 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17499 connf_head != NULL) { 17500 /* No user-level listener for IGMP packets */ 17501 goto drop_pkt; 17502 } 17503 /* deliver to local raw users */ 17504 break; 17505 case IPPROTO_PIM: 17506 /* 17507 * If we are not willing to accept PIM packets in clear, 17508 * then check with global policy. 17509 */ 17510 if (ipst->ips_pim_accept_clear_messages == 0) { 17511 first_mp = ipsec_check_global_policy(first_mp, NULL, 17512 ipha, NULL, mctl_present, ipst->ips_netstack); 17513 if (first_mp == NULL) 17514 return; 17515 } 17516 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17517 freemsg(first_mp); 17518 ip1dbg(("ip_proto_input: zone all cannot accept PIM")); 17519 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17520 return; 17521 } 17522 if (pim_input(q, mp, ill) != 0) { 17523 /* Bad packet - discarded by pim_input */ 17524 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17525 "ip_rput_locl_end: q %p (%S)", q, "pim"); 17526 if (mctl_present) 17527 freeb(first_mp); 17528 return; 17529 } 17530 17531 /* 17532 * pim_input() may have pulled up the message so ipha needs to 17533 * be reinitialized. 17534 */ 17535 ipha = (ipha_t *)mp->b_rptr; 17536 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17537 connf_head != NULL) { 17538 /* No user-level listener for PIM packets */ 17539 goto drop_pkt; 17540 } 17541 /* deliver to local raw users */ 17542 break; 17543 case IPPROTO_ENCAP: 17544 /* 17545 * Handle self-encapsulated packets (IP-in-IP where 17546 * the inner addresses == the outer addresses). 17547 */ 17548 hdr_length = IPH_HDR_LENGTH(ipha); 17549 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 17550 mp->b_wptr) { 17551 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 17552 sizeof (ipha_t) - mp->b_rptr)) { 17553 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17554 freemsg(first_mp); 17555 return; 17556 } 17557 ipha = (ipha_t *)mp->b_rptr; 17558 } 17559 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 17560 /* 17561 * Check the sanity of the inner IP header. 17562 */ 17563 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 17564 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17565 freemsg(first_mp); 17566 return; 17567 } 17568 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 17569 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17570 freemsg(first_mp); 17571 return; 17572 } 17573 if (inner_ipha->ipha_src == ipha->ipha_src && 17574 inner_ipha->ipha_dst == ipha->ipha_dst) { 17575 ipsec_in_t *ii; 17576 17577 /* 17578 * Self-encapsulated tunnel packet. Remove 17579 * the outer IP header and fanout again. 17580 * We also need to make sure that the inner 17581 * header is pulled up until options. 17582 */ 17583 mp->b_rptr = (uchar_t *)inner_ipha; 17584 ipha = inner_ipha; 17585 hdr_length = IPH_HDR_LENGTH(ipha); 17586 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 17587 if (!pullupmsg(mp, (uchar_t *)ipha + 17588 + hdr_length - mp->b_rptr)) { 17589 freemsg(first_mp); 17590 return; 17591 } 17592 ipha = (ipha_t *)mp->b_rptr; 17593 } 17594 if (!mctl_present) { 17595 ASSERT(first_mp == mp); 17596 /* 17597 * This means that somebody is sending 17598 * Self-encapsualted packets without AH/ESP. 17599 * If AH/ESP was present, we would have already 17600 * allocated the first_mp. 17601 */ 17602 first_mp = ipsec_in_alloc(B_TRUE, 17603 ipst->ips_netstack); 17604 if (first_mp == NULL) { 17605 ip1dbg(("ip_proto_input: IPSEC_IN " 17606 "allocation failure.\n")); 17607 BUMP_MIB(ill->ill_ip_mib, 17608 ipIfStatsInDiscards); 17609 freemsg(mp); 17610 return; 17611 } 17612 first_mp->b_cont = mp; 17613 } 17614 /* 17615 * We generally store the ill_index if we need to 17616 * do IPSEC processing as we lose the ill queue when 17617 * we come back. But in this case, we never should 17618 * have to store the ill_index here as it should have 17619 * been stored previously when we processed the 17620 * AH/ESP header in this routine or for non-ipsec 17621 * cases, we still have the queue. But for some bad 17622 * packets from the wire, we can get to IPSEC after 17623 * this and we better store the index for that case. 17624 */ 17625 ill = (ill_t *)q->q_ptr; 17626 ii = (ipsec_in_t *)first_mp->b_rptr; 17627 ii->ipsec_in_ill_index = 17628 ill->ill_phyint->phyint_ifindex; 17629 ii->ipsec_in_rill_index = 17630 recv_ill->ill_phyint->phyint_ifindex; 17631 if (ii->ipsec_in_decaps) { 17632 /* 17633 * This packet is self-encapsulated multiple 17634 * times. We don't want to recurse infinitely. 17635 * To keep it simple, drop the packet. 17636 */ 17637 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17638 freemsg(first_mp); 17639 return; 17640 } 17641 ii->ipsec_in_decaps = B_TRUE; 17642 ip_fanout_proto_again(first_mp, recv_ill, recv_ill, 17643 ire); 17644 return; 17645 } 17646 break; 17647 case IPPROTO_AH: 17648 case IPPROTO_ESP: { 17649 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 17650 17651 /* 17652 * Fast path for AH/ESP. If this is the first time 17653 * we are sending a datagram to AH/ESP, allocate 17654 * a IPSEC_IN message and prepend it. Otherwise, 17655 * just fanout. 17656 */ 17657 17658 int ipsec_rc; 17659 ipsec_in_t *ii; 17660 netstack_t *ns = ipst->ips_netstack; 17661 17662 IP_STAT(ipst, ipsec_proto_ahesp); 17663 if (!mctl_present) { 17664 ASSERT(first_mp == mp); 17665 first_mp = ipsec_in_alloc(B_TRUE, ns); 17666 if (first_mp == NULL) { 17667 ip1dbg(("ip_proto_input: IPSEC_IN " 17668 "allocation failure.\n")); 17669 freemsg(hada_mp); /* okay ifnull */ 17670 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17671 freemsg(mp); 17672 return; 17673 } 17674 /* 17675 * Store the ill_index so that when we come back 17676 * from IPSEC we ride on the same queue. 17677 */ 17678 ill = (ill_t *)q->q_ptr; 17679 ii = (ipsec_in_t *)first_mp->b_rptr; 17680 ii->ipsec_in_ill_index = 17681 ill->ill_phyint->phyint_ifindex; 17682 ii->ipsec_in_rill_index = 17683 recv_ill->ill_phyint->phyint_ifindex; 17684 first_mp->b_cont = mp; 17685 /* 17686 * Cache hardware acceleration info. 17687 */ 17688 if (hada_mp != NULL) { 17689 IPSECHW_DEBUG(IPSECHW_PKT, 17690 ("ip_rput_local: caching data attr.\n")); 17691 ii->ipsec_in_accelerated = B_TRUE; 17692 ii->ipsec_in_da = hada_mp; 17693 hada_mp = NULL; 17694 } 17695 } else { 17696 ii = (ipsec_in_t *)first_mp->b_rptr; 17697 } 17698 17699 if (!ipsec_loaded(ipss)) { 17700 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 17701 ire->ire_zoneid, ipst); 17702 return; 17703 } 17704 17705 ns = ipst->ips_netstack; 17706 /* select inbound SA and have IPsec process the pkt */ 17707 if (ipha->ipha_protocol == IPPROTO_ESP) { 17708 esph_t *esph = ipsec_inbound_esp_sa(first_mp, ns); 17709 if (esph == NULL) 17710 return; 17711 ASSERT(ii->ipsec_in_esp_sa != NULL); 17712 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 17713 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 17714 first_mp, esph); 17715 } else { 17716 ah_t *ah = ipsec_inbound_ah_sa(first_mp, ns); 17717 if (ah == NULL) 17718 return; 17719 ASSERT(ii->ipsec_in_ah_sa != NULL); 17720 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 17721 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 17722 first_mp, ah); 17723 } 17724 17725 switch (ipsec_rc) { 17726 case IPSEC_STATUS_SUCCESS: 17727 break; 17728 case IPSEC_STATUS_FAILED: 17729 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17730 /* FALLTHRU */ 17731 case IPSEC_STATUS_PENDING: 17732 return; 17733 } 17734 /* we're done with IPsec processing, send it up */ 17735 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 17736 return; 17737 } 17738 default: 17739 break; 17740 } 17741 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { 17742 ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", 17743 ire->ire_zoneid)); 17744 goto drop_pkt; 17745 } 17746 /* 17747 * Handle protocols with which IP is less intimate. There 17748 * can be more than one stream bound to a particular 17749 * protocol. When this is the case, each one gets a copy 17750 * of any incoming packets. 17751 */ 17752 ip_fanout_proto(q, first_mp, ill, ipha, 17753 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 17754 B_TRUE, recv_ill, ire->ire_zoneid); 17755 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17756 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 17757 return; 17758 17759 drop_pkt: 17760 freemsg(first_mp); 17761 if (hada_mp != NULL) 17762 freeb(hada_mp); 17763 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17764 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 17765 #undef rptr 17766 #undef iphs 17767 17768 } 17769 17770 /* 17771 * Update any source route, record route or timestamp options. 17772 * Check that we are at end of strict source route. 17773 * The options have already been checked for sanity in ip_rput_options(). 17774 */ 17775 static boolean_t 17776 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17777 ip_stack_t *ipst) 17778 { 17779 ipoptp_t opts; 17780 uchar_t *opt; 17781 uint8_t optval; 17782 uint8_t optlen; 17783 ipaddr_t dst; 17784 uint32_t ts; 17785 ire_t *dst_ire; 17786 timestruc_t now; 17787 zoneid_t zoneid; 17788 ill_t *ill; 17789 17790 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17791 17792 ip2dbg(("ip_rput_local_options\n")); 17793 17794 for (optval = ipoptp_first(&opts, ipha); 17795 optval != IPOPT_EOL; 17796 optval = ipoptp_next(&opts)) { 17797 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 17798 opt = opts.ipoptp_cur; 17799 optlen = opts.ipoptp_len; 17800 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 17801 optval, optlen)); 17802 switch (optval) { 17803 uint32_t off; 17804 case IPOPT_SSRR: 17805 case IPOPT_LSRR: 17806 off = opt[IPOPT_OFFSET]; 17807 off--; 17808 if (optlen < IP_ADDR_LEN || 17809 off > optlen - IP_ADDR_LEN) { 17810 /* End of source route */ 17811 ip1dbg(("ip_rput_local_options: end of SR\n")); 17812 break; 17813 } 17814 /* 17815 * This will only happen if two consecutive entries 17816 * in the source route contains our address or if 17817 * it is a packet with a loose source route which 17818 * reaches us before consuming the whole source route 17819 */ 17820 ip1dbg(("ip_rput_local_options: not end of SR\n")); 17821 if (optval == IPOPT_SSRR) { 17822 goto bad_src_route; 17823 } 17824 /* 17825 * Hack: instead of dropping the packet truncate the 17826 * source route to what has been used by filling the 17827 * rest with IPOPT_NOP. 17828 */ 17829 opt[IPOPT_OLEN] = (uint8_t)off; 17830 while (off < optlen) { 17831 opt[off++] = IPOPT_NOP; 17832 } 17833 break; 17834 case IPOPT_RR: 17835 off = opt[IPOPT_OFFSET]; 17836 off--; 17837 if (optlen < IP_ADDR_LEN || 17838 off > optlen - IP_ADDR_LEN) { 17839 /* No more room - ignore */ 17840 ip1dbg(( 17841 "ip_rput_local_options: end of RR\n")); 17842 break; 17843 } 17844 bcopy(&ire->ire_src_addr, (char *)opt + off, 17845 IP_ADDR_LEN); 17846 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17847 break; 17848 case IPOPT_TS: 17849 /* Insert timestamp if there is romm */ 17850 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17851 case IPOPT_TS_TSONLY: 17852 off = IPOPT_TS_TIMELEN; 17853 break; 17854 case IPOPT_TS_PRESPEC: 17855 case IPOPT_TS_PRESPEC_RFC791: 17856 /* Verify that the address matched */ 17857 off = opt[IPOPT_OFFSET] - 1; 17858 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17859 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17860 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 17861 ipst); 17862 if (dst_ire == NULL) { 17863 /* Not for us */ 17864 break; 17865 } 17866 ire_refrele(dst_ire); 17867 /* FALLTHRU */ 17868 case IPOPT_TS_TSANDADDR: 17869 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17870 break; 17871 default: 17872 /* 17873 * ip_*put_options should have already 17874 * dropped this packet. 17875 */ 17876 cmn_err(CE_PANIC, "ip_rput_local_options: " 17877 "unknown IT - bug in ip_rput_options?\n"); 17878 return (B_TRUE); /* Keep "lint" happy */ 17879 } 17880 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 17881 /* Increase overflow counter */ 17882 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 17883 opt[IPOPT_POS_OV_FLG] = 17884 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 17885 (off << 4)); 17886 break; 17887 } 17888 off = opt[IPOPT_OFFSET] - 1; 17889 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17890 case IPOPT_TS_PRESPEC: 17891 case IPOPT_TS_PRESPEC_RFC791: 17892 case IPOPT_TS_TSANDADDR: 17893 bcopy(&ire->ire_src_addr, (char *)opt + off, 17894 IP_ADDR_LEN); 17895 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17896 /* FALLTHRU */ 17897 case IPOPT_TS_TSONLY: 17898 off = opt[IPOPT_OFFSET] - 1; 17899 /* Compute # of milliseconds since midnight */ 17900 gethrestime(&now); 17901 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 17902 now.tv_nsec / (NANOSEC / MILLISEC); 17903 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 17904 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 17905 break; 17906 } 17907 break; 17908 } 17909 } 17910 return (B_TRUE); 17911 17912 bad_src_route: 17913 q = WR(q); 17914 if (q->q_next != NULL) 17915 ill = q->q_ptr; 17916 else 17917 ill = NULL; 17918 17919 /* make sure we clear any indication of a hardware checksum */ 17920 DB_CKSUMFLAGS(mp) = 0; 17921 zoneid = ipif_lookup_addr_zoneid(ipha->ipha_dst, ill, ipst); 17922 if (zoneid == ALL_ZONES) 17923 freemsg(mp); 17924 else 17925 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 17926 return (B_FALSE); 17927 17928 } 17929 17930 /* 17931 * Process IP options in an inbound packet. If an option affects the 17932 * effective destination address, return the next hop address via dstp. 17933 * Returns -1 if something fails in which case an ICMP error has been sent 17934 * and mp freed. 17935 */ 17936 static int 17937 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp, 17938 ip_stack_t *ipst) 17939 { 17940 ipoptp_t opts; 17941 uchar_t *opt; 17942 uint8_t optval; 17943 uint8_t optlen; 17944 ipaddr_t dst; 17945 intptr_t code = 0; 17946 ire_t *ire = NULL; 17947 zoneid_t zoneid; 17948 ill_t *ill; 17949 17950 ip2dbg(("ip_rput_options\n")); 17951 dst = ipha->ipha_dst; 17952 for (optval = ipoptp_first(&opts, ipha); 17953 optval != IPOPT_EOL; 17954 optval = ipoptp_next(&opts)) { 17955 opt = opts.ipoptp_cur; 17956 optlen = opts.ipoptp_len; 17957 ip2dbg(("ip_rput_options: opt %d, len %d\n", 17958 optval, optlen)); 17959 /* 17960 * Note: we need to verify the checksum before we 17961 * modify anything thus this routine only extracts the next 17962 * hop dst from any source route. 17963 */ 17964 switch (optval) { 17965 uint32_t off; 17966 case IPOPT_SSRR: 17967 case IPOPT_LSRR: 17968 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17969 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17970 if (ire == NULL) { 17971 if (optval == IPOPT_SSRR) { 17972 ip1dbg(("ip_rput_options: not next" 17973 " strict source route 0x%x\n", 17974 ntohl(dst))); 17975 code = (char *)&ipha->ipha_dst - 17976 (char *)ipha; 17977 goto param_prob; /* RouterReq's */ 17978 } 17979 ip2dbg(("ip_rput_options: " 17980 "not next source route 0x%x\n", 17981 ntohl(dst))); 17982 break; 17983 } 17984 ire_refrele(ire); 17985 17986 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17987 ip1dbg(( 17988 "ip_rput_options: bad option offset\n")); 17989 code = (char *)&opt[IPOPT_OLEN] - 17990 (char *)ipha; 17991 goto param_prob; 17992 } 17993 off = opt[IPOPT_OFFSET]; 17994 off--; 17995 redo_srr: 17996 if (optlen < IP_ADDR_LEN || 17997 off > optlen - IP_ADDR_LEN) { 17998 /* End of source route */ 17999 ip1dbg(("ip_rput_options: end of SR\n")); 18000 break; 18001 } 18002 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 18003 ip1dbg(("ip_rput_options: next hop 0x%x\n", 18004 ntohl(dst))); 18005 18006 /* 18007 * Check if our address is present more than 18008 * once as consecutive hops in source route. 18009 * XXX verify per-interface ip_forwarding 18010 * for source route? 18011 */ 18012 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 18013 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 18014 18015 if (ire != NULL) { 18016 ire_refrele(ire); 18017 off += IP_ADDR_LEN; 18018 goto redo_srr; 18019 } 18020 18021 if (dst == htonl(INADDR_LOOPBACK)) { 18022 ip1dbg(("ip_rput_options: loopback addr in " 18023 "source route!\n")); 18024 goto bad_src_route; 18025 } 18026 /* 18027 * For strict: verify that dst is directly 18028 * reachable. 18029 */ 18030 if (optval == IPOPT_SSRR) { 18031 ire = ire_ftable_lookup(dst, 0, 0, 18032 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 18033 MBLK_GETLABEL(mp), 18034 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 18035 if (ire == NULL) { 18036 ip1dbg(("ip_rput_options: SSRR not " 18037 "directly reachable: 0x%x\n", 18038 ntohl(dst))); 18039 goto bad_src_route; 18040 } 18041 ire_refrele(ire); 18042 } 18043 /* 18044 * Defer update of the offset and the record route 18045 * until the packet is forwarded. 18046 */ 18047 break; 18048 case IPOPT_RR: 18049 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 18050 ip1dbg(( 18051 "ip_rput_options: bad option offset\n")); 18052 code = (char *)&opt[IPOPT_OLEN] - 18053 (char *)ipha; 18054 goto param_prob; 18055 } 18056 break; 18057 case IPOPT_TS: 18058 /* 18059 * Verify that length >= 5 and that there is either 18060 * room for another timestamp or that the overflow 18061 * counter is not maxed out. 18062 */ 18063 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 18064 if (optlen < IPOPT_MINLEN_IT) { 18065 goto param_prob; 18066 } 18067 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 18068 ip1dbg(( 18069 "ip_rput_options: bad option offset\n")); 18070 code = (char *)&opt[IPOPT_OFFSET] - 18071 (char *)ipha; 18072 goto param_prob; 18073 } 18074 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 18075 case IPOPT_TS_TSONLY: 18076 off = IPOPT_TS_TIMELEN; 18077 break; 18078 case IPOPT_TS_TSANDADDR: 18079 case IPOPT_TS_PRESPEC: 18080 case IPOPT_TS_PRESPEC_RFC791: 18081 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 18082 break; 18083 default: 18084 code = (char *)&opt[IPOPT_POS_OV_FLG] - 18085 (char *)ipha; 18086 goto param_prob; 18087 } 18088 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 18089 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 18090 /* 18091 * No room and the overflow counter is 15 18092 * already. 18093 */ 18094 goto param_prob; 18095 } 18096 break; 18097 } 18098 } 18099 18100 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 18101 *dstp = dst; 18102 return (0); 18103 } 18104 18105 ip1dbg(("ip_rput_options: error processing IP options.")); 18106 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 18107 18108 param_prob: 18109 q = WR(q); 18110 if (q->q_next != NULL) 18111 ill = q->q_ptr; 18112 else 18113 ill = NULL; 18114 18115 /* make sure we clear any indication of a hardware checksum */ 18116 DB_CKSUMFLAGS(mp) = 0; 18117 /* Don't know whether this is for non-global or global/forwarding */ 18118 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 18119 if (zoneid == ALL_ZONES) 18120 freemsg(mp); 18121 else 18122 icmp_param_problem(q, mp, (uint8_t)code, zoneid, ipst); 18123 return (-1); 18124 18125 bad_src_route: 18126 q = WR(q); 18127 if (q->q_next != NULL) 18128 ill = q->q_ptr; 18129 else 18130 ill = NULL; 18131 18132 /* make sure we clear any indication of a hardware checksum */ 18133 DB_CKSUMFLAGS(mp) = 0; 18134 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 18135 if (zoneid == ALL_ZONES) 18136 freemsg(mp); 18137 else 18138 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 18139 return (-1); 18140 } 18141 18142 /* 18143 * IP & ICMP info in >=14 msg's ... 18144 * - ip fixed part (mib2_ip_t) 18145 * - icmp fixed part (mib2_icmp_t) 18146 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 18147 * - ipRouteEntryTable (ip 21) all IPv4 IREs 18148 * - ipNetToMediaEntryTable (ip 22) [filled in by the arp module] 18149 * - ipRouteAttributeTable (ip 102) labeled routes 18150 * - ip multicast membership (ip_member_t) 18151 * - ip multicast source filtering (ip_grpsrc_t) 18152 * - igmp fixed part (struct igmpstat) 18153 * - multicast routing stats (struct mrtstat) 18154 * - multicast routing vifs (array of struct vifctl) 18155 * - multicast routing routes (array of struct mfcctl) 18156 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 18157 * One per ill plus one generic 18158 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 18159 * One per ill plus one generic 18160 * - ipv6RouteEntry all IPv6 IREs 18161 * - ipv6RouteAttributeTable (ip6 102) labeled routes 18162 * - ipv6NetToMediaEntry all Neighbor Cache entries 18163 * - ipv6AddrEntry all IPv6 ipifs 18164 * - ipv6 multicast membership (ipv6_member_t) 18165 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 18166 * 18167 * MIB2_IP_MEDIA is filled in by the arp module with ARP cache entries. 18168 * 18169 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 18170 * already filled in by the caller. 18171 * Return value of 0 indicates that no messages were sent and caller 18172 * should free mpctl. 18173 */ 18174 int 18175 ip_snmp_get(queue_t *q, mblk_t *mpctl) 18176 { 18177 ip_stack_t *ipst; 18178 sctp_stack_t *sctps; 18179 18180 18181 if (q->q_next != NULL) { 18182 ipst = ILLQ_TO_IPST(q); 18183 } else { 18184 ipst = CONNQ_TO_IPST(q); 18185 } 18186 ASSERT(ipst != NULL); 18187 sctps = ipst->ips_netstack->netstack_sctp; 18188 18189 if (mpctl == NULL || mpctl->b_cont == NULL) { 18190 return (0); 18191 } 18192 18193 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl, 18194 ipst)) == NULL) { 18195 return (1); 18196 } 18197 18198 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst)) == NULL) { 18199 return (1); 18200 } 18201 18202 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) { 18203 return (1); 18204 } 18205 18206 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) { 18207 return (1); 18208 } 18209 18210 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) { 18211 return (1); 18212 } 18213 18214 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) { 18215 return (1); 18216 } 18217 18218 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst)) == NULL) { 18219 return (1); 18220 } 18221 18222 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst)) == NULL) { 18223 return (1); 18224 } 18225 18226 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) { 18227 return (1); 18228 } 18229 18230 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) { 18231 return (1); 18232 } 18233 18234 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) { 18235 return (1); 18236 } 18237 18238 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) { 18239 return (1); 18240 } 18241 18242 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) { 18243 return (1); 18244 } 18245 18246 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) { 18247 return (1); 18248 } 18249 18250 if ((mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, ipst)) == NULL) { 18251 return (1); 18252 } 18253 18254 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, ipst); 18255 if (mpctl == NULL) { 18256 return (1); 18257 } 18258 18259 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) { 18260 return (1); 18261 } 18262 freemsg(mpctl); 18263 return (1); 18264 } 18265 18266 18267 /* Get global (legacy) IPv4 statistics */ 18268 static mblk_t * 18269 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib, 18270 ip_stack_t *ipst) 18271 { 18272 mib2_ip_t old_ip_mib; 18273 struct opthdr *optp; 18274 mblk_t *mp2ctl; 18275 18276 /* 18277 * make a copy of the original message 18278 */ 18279 mp2ctl = copymsg(mpctl); 18280 18281 /* fixed length IP structure... */ 18282 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18283 optp->level = MIB2_IP; 18284 optp->name = 0; 18285 SET_MIB(old_ip_mib.ipForwarding, 18286 (WE_ARE_FORWARDING(ipst) ? 1 : 2)); 18287 SET_MIB(old_ip_mib.ipDefaultTTL, 18288 (uint32_t)ipst->ips_ip_def_ttl); 18289 SET_MIB(old_ip_mib.ipReasmTimeout, 18290 ipst->ips_ip_g_frag_timeout); 18291 SET_MIB(old_ip_mib.ipAddrEntrySize, 18292 sizeof (mib2_ipAddrEntry_t)); 18293 SET_MIB(old_ip_mib.ipRouteEntrySize, 18294 sizeof (mib2_ipRouteEntry_t)); 18295 SET_MIB(old_ip_mib.ipNetToMediaEntrySize, 18296 sizeof (mib2_ipNetToMediaEntry_t)); 18297 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 18298 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 18299 SET_MIB(old_ip_mib.ipRouteAttributeSize, 18300 sizeof (mib2_ipAttributeEntry_t)); 18301 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 18302 18303 /* 18304 * Grab the statistics from the new IP MIB 18305 */ 18306 SET_MIB(old_ip_mib.ipInReceives, 18307 (uint32_t)ipmib->ipIfStatsHCInReceives); 18308 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors); 18309 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors); 18310 SET_MIB(old_ip_mib.ipForwDatagrams, 18311 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams); 18312 SET_MIB(old_ip_mib.ipInUnknownProtos, 18313 ipmib->ipIfStatsInUnknownProtos); 18314 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards); 18315 SET_MIB(old_ip_mib.ipInDelivers, 18316 (uint32_t)ipmib->ipIfStatsHCInDelivers); 18317 SET_MIB(old_ip_mib.ipOutRequests, 18318 (uint32_t)ipmib->ipIfStatsHCOutRequests); 18319 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards); 18320 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes); 18321 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds); 18322 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs); 18323 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails); 18324 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs); 18325 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails); 18326 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates); 18327 18328 /* ipRoutingDiscards is not being used */ 18329 SET_MIB(old_ip_mib.ipRoutingDiscards, 0); 18330 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs); 18331 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts); 18332 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs); 18333 SET_MIB(old_ip_mib.ipReasmDuplicates, 18334 ipmib->ipIfStatsReasmDuplicates); 18335 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups); 18336 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits); 18337 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs); 18338 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows); 18339 SET_MIB(old_ip_mib.rawipInOverflows, 18340 ipmib->rawipIfStatsInOverflows); 18341 18342 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded); 18343 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed); 18344 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion); 18345 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion); 18346 SET_MIB(old_ip_mib.ipOutSwitchIPv6, 18347 ipmib->ipIfStatsOutSwitchIPVersion); 18348 18349 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib, 18350 (int)sizeof (old_ip_mib))) { 18351 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 18352 (uint_t)sizeof (old_ip_mib))); 18353 } 18354 18355 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18356 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 18357 (int)optp->level, (int)optp->name, (int)optp->len)); 18358 qreply(q, mpctl); 18359 return (mp2ctl); 18360 } 18361 18362 /* Per interface IPv4 statistics */ 18363 static mblk_t * 18364 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18365 { 18366 struct opthdr *optp; 18367 mblk_t *mp2ctl; 18368 ill_t *ill; 18369 ill_walk_context_t ctx; 18370 mblk_t *mp_tail = NULL; 18371 mib2_ipIfStatsEntry_t global_ip_mib; 18372 18373 /* 18374 * Make a copy of the original message 18375 */ 18376 mp2ctl = copymsg(mpctl); 18377 18378 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18379 optp->level = MIB2_IP; 18380 optp->name = MIB2_IP_TRAFFIC_STATS; 18381 /* Include "unknown interface" ip_mib */ 18382 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 18383 ipst->ips_ip_mib.ipIfStatsIfIndex = 18384 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 18385 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding, 18386 (ipst->ips_ip_g_forward ? 1 : 2)); 18387 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL, 18388 (uint32_t)ipst->ips_ip_def_ttl); 18389 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize, 18390 sizeof (mib2_ipIfStatsEntry_t)); 18391 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize, 18392 sizeof (mib2_ipAddrEntry_t)); 18393 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize, 18394 sizeof (mib2_ipRouteEntry_t)); 18395 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize, 18396 sizeof (mib2_ipNetToMediaEntry_t)); 18397 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize, 18398 sizeof (ip_member_t)); 18399 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize, 18400 sizeof (ip_grpsrc_t)); 18401 18402 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18403 (char *)&ipst->ips_ip_mib, (int)sizeof (ipst->ips_ip_mib))) { 18404 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18405 "failed to allocate %u bytes\n", 18406 (uint_t)sizeof (ipst->ips_ip_mib))); 18407 } 18408 18409 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib)); 18410 18411 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18412 ill = ILL_START_WALK_V4(&ctx, ipst); 18413 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18414 ill->ill_ip_mib->ipIfStatsIfIndex = 18415 ill->ill_phyint->phyint_ifindex; 18416 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 18417 (ipst->ips_ip_g_forward ? 1 : 2)); 18418 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL, 18419 (uint32_t)ipst->ips_ip_def_ttl); 18420 18421 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib); 18422 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18423 (char *)ill->ill_ip_mib, 18424 (int)sizeof (*ill->ill_ip_mib))) { 18425 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18426 "failed to allocate %u bytes\n", 18427 (uint_t)sizeof (*ill->ill_ip_mib))); 18428 } 18429 } 18430 rw_exit(&ipst->ips_ill_g_lock); 18431 18432 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18433 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18434 "level %d, name %d, len %d\n", 18435 (int)optp->level, (int)optp->name, (int)optp->len)); 18436 qreply(q, mpctl); 18437 18438 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst)); 18439 } 18440 18441 /* Global IPv4 ICMP statistics */ 18442 static mblk_t * 18443 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18444 { 18445 struct opthdr *optp; 18446 mblk_t *mp2ctl; 18447 18448 /* 18449 * Make a copy of the original message 18450 */ 18451 mp2ctl = copymsg(mpctl); 18452 18453 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18454 optp->level = MIB2_ICMP; 18455 optp->name = 0; 18456 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib, 18457 (int)sizeof (ipst->ips_icmp_mib))) { 18458 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 18459 (uint_t)sizeof (ipst->ips_icmp_mib))); 18460 } 18461 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18462 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 18463 (int)optp->level, (int)optp->name, (int)optp->len)); 18464 qreply(q, mpctl); 18465 return (mp2ctl); 18466 } 18467 18468 /* Global IPv4 IGMP statistics */ 18469 static mblk_t * 18470 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18471 { 18472 struct opthdr *optp; 18473 mblk_t *mp2ctl; 18474 18475 /* 18476 * make a copy of the original message 18477 */ 18478 mp2ctl = copymsg(mpctl); 18479 18480 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18481 optp->level = EXPER_IGMP; 18482 optp->name = 0; 18483 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat, 18484 (int)sizeof (ipst->ips_igmpstat))) { 18485 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 18486 (uint_t)sizeof (ipst->ips_igmpstat))); 18487 } 18488 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18489 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 18490 (int)optp->level, (int)optp->name, (int)optp->len)); 18491 qreply(q, mpctl); 18492 return (mp2ctl); 18493 } 18494 18495 /* Global IPv4 Multicast Routing statistics */ 18496 static mblk_t * 18497 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18498 { 18499 struct opthdr *optp; 18500 mblk_t *mp2ctl; 18501 18502 /* 18503 * make a copy of the original message 18504 */ 18505 mp2ctl = copymsg(mpctl); 18506 18507 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18508 optp->level = EXPER_DVMRP; 18509 optp->name = 0; 18510 if (!ip_mroute_stats(mpctl->b_cont, ipst)) { 18511 ip0dbg(("ip_mroute_stats: failed\n")); 18512 } 18513 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18514 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 18515 (int)optp->level, (int)optp->name, (int)optp->len)); 18516 qreply(q, mpctl); 18517 return (mp2ctl); 18518 } 18519 18520 /* IPv4 address information */ 18521 static mblk_t * 18522 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18523 { 18524 struct opthdr *optp; 18525 mblk_t *mp2ctl; 18526 mblk_t *mp_tail = NULL; 18527 ill_t *ill; 18528 ipif_t *ipif; 18529 uint_t bitval; 18530 mib2_ipAddrEntry_t mae; 18531 zoneid_t zoneid; 18532 ill_walk_context_t ctx; 18533 18534 /* 18535 * make a copy of the original message 18536 */ 18537 mp2ctl = copymsg(mpctl); 18538 18539 /* ipAddrEntryTable */ 18540 18541 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18542 optp->level = MIB2_IP; 18543 optp->name = MIB2_IP_ADDR; 18544 zoneid = Q_TO_CONN(q)->conn_zoneid; 18545 18546 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18547 ill = ILL_START_WALK_V4(&ctx, ipst); 18548 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18549 for (ipif = ill->ill_ipif; ipif != NULL; 18550 ipif = ipif->ipif_next) { 18551 if (ipif->ipif_zoneid != zoneid && 18552 ipif->ipif_zoneid != ALL_ZONES) 18553 continue; 18554 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18555 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18556 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18557 18558 (void) ipif_get_name(ipif, 18559 mae.ipAdEntIfIndex.o_bytes, 18560 OCTET_LENGTH); 18561 mae.ipAdEntIfIndex.o_length = 18562 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 18563 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 18564 mae.ipAdEntNetMask = ipif->ipif_net_mask; 18565 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 18566 mae.ipAdEntInfo.ae_subnet_len = 18567 ip_mask_to_plen(ipif->ipif_net_mask); 18568 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 18569 for (bitval = 1; 18570 bitval && 18571 !(bitval & ipif->ipif_brd_addr); 18572 bitval <<= 1) 18573 noop; 18574 mae.ipAdEntBcastAddr = bitval; 18575 mae.ipAdEntReasmMaxSize = IP_MAXPACKET; 18576 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 18577 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 18578 mae.ipAdEntInfo.ae_broadcast_addr = 18579 ipif->ipif_brd_addr; 18580 mae.ipAdEntInfo.ae_pp_dst_addr = 18581 ipif->ipif_pp_dst_addr; 18582 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 18583 ill->ill_flags | ill->ill_phyint->phyint_flags; 18584 mae.ipAdEntRetransmitTime = AR_EQ_DEFAULT_XMIT_INTERVAL; 18585 18586 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18587 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 18588 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 18589 "allocate %u bytes\n", 18590 (uint_t)sizeof (mib2_ipAddrEntry_t))); 18591 } 18592 } 18593 } 18594 rw_exit(&ipst->ips_ill_g_lock); 18595 18596 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18597 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 18598 (int)optp->level, (int)optp->name, (int)optp->len)); 18599 qreply(q, mpctl); 18600 return (mp2ctl); 18601 } 18602 18603 /* IPv6 address information */ 18604 static mblk_t * 18605 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18606 { 18607 struct opthdr *optp; 18608 mblk_t *mp2ctl; 18609 mblk_t *mp_tail = NULL; 18610 ill_t *ill; 18611 ipif_t *ipif; 18612 mib2_ipv6AddrEntry_t mae6; 18613 zoneid_t zoneid; 18614 ill_walk_context_t ctx; 18615 18616 /* 18617 * make a copy of the original message 18618 */ 18619 mp2ctl = copymsg(mpctl); 18620 18621 /* ipv6AddrEntryTable */ 18622 18623 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18624 optp->level = MIB2_IP6; 18625 optp->name = MIB2_IP6_ADDR; 18626 zoneid = Q_TO_CONN(q)->conn_zoneid; 18627 18628 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18629 ill = ILL_START_WALK_V6(&ctx, ipst); 18630 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18631 for (ipif = ill->ill_ipif; ipif != NULL; 18632 ipif = ipif->ipif_next) { 18633 if (ipif->ipif_zoneid != zoneid && 18634 ipif->ipif_zoneid != ALL_ZONES) 18635 continue; 18636 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18637 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18638 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18639 18640 (void) ipif_get_name(ipif, 18641 mae6.ipv6AddrIfIndex.o_bytes, 18642 OCTET_LENGTH); 18643 mae6.ipv6AddrIfIndex.o_length = 18644 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 18645 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 18646 mae6.ipv6AddrPfxLength = 18647 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 18648 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 18649 mae6.ipv6AddrInfo.ae_subnet_len = 18650 mae6.ipv6AddrPfxLength; 18651 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 18652 18653 /* Type: stateless(1), stateful(2), unknown(3) */ 18654 if (ipif->ipif_flags & IPIF_ADDRCONF) 18655 mae6.ipv6AddrType = 1; 18656 else 18657 mae6.ipv6AddrType = 2; 18658 /* Anycast: true(1), false(2) */ 18659 if (ipif->ipif_flags & IPIF_ANYCAST) 18660 mae6.ipv6AddrAnycastFlag = 1; 18661 else 18662 mae6.ipv6AddrAnycastFlag = 2; 18663 18664 /* 18665 * Address status: preferred(1), deprecated(2), 18666 * invalid(3), inaccessible(4), unknown(5) 18667 */ 18668 if (ipif->ipif_flags & IPIF_NOLOCAL) 18669 mae6.ipv6AddrStatus = 3; 18670 else if (ipif->ipif_flags & IPIF_DEPRECATED) 18671 mae6.ipv6AddrStatus = 2; 18672 else 18673 mae6.ipv6AddrStatus = 1; 18674 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 18675 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 18676 mae6.ipv6AddrInfo.ae_pp_dst_addr = 18677 ipif->ipif_v6pp_dst_addr; 18678 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 18679 ill->ill_flags | ill->ill_phyint->phyint_flags; 18680 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET; 18681 mae6.ipv6AddrIdentifier = ill->ill_token; 18682 mae6.ipv6AddrIdentifierLen = ill->ill_token_length; 18683 mae6.ipv6AddrReachableTime = ill->ill_reachable_time; 18684 mae6.ipv6AddrRetransmitTime = 18685 ill->ill_reachable_retrans_time; 18686 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18687 (char *)&mae6, 18688 (int)sizeof (mib2_ipv6AddrEntry_t))) { 18689 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 18690 "allocate %u bytes\n", 18691 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 18692 } 18693 } 18694 } 18695 rw_exit(&ipst->ips_ill_g_lock); 18696 18697 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18698 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 18699 (int)optp->level, (int)optp->name, (int)optp->len)); 18700 qreply(q, mpctl); 18701 return (mp2ctl); 18702 } 18703 18704 /* IPv4 multicast group membership. */ 18705 static mblk_t * 18706 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18707 { 18708 struct opthdr *optp; 18709 mblk_t *mp2ctl; 18710 ill_t *ill; 18711 ipif_t *ipif; 18712 ilm_t *ilm; 18713 ip_member_t ipm; 18714 mblk_t *mp_tail = NULL; 18715 ill_walk_context_t ctx; 18716 zoneid_t zoneid; 18717 18718 /* 18719 * make a copy of the original message 18720 */ 18721 mp2ctl = copymsg(mpctl); 18722 zoneid = Q_TO_CONN(q)->conn_zoneid; 18723 18724 /* ipGroupMember table */ 18725 optp = (struct opthdr *)&mpctl->b_rptr[ 18726 sizeof (struct T_optmgmt_ack)]; 18727 optp->level = MIB2_IP; 18728 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 18729 18730 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18731 ill = ILL_START_WALK_V4(&ctx, ipst); 18732 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18733 ILM_WALKER_HOLD(ill); 18734 for (ipif = ill->ill_ipif; ipif != NULL; 18735 ipif = ipif->ipif_next) { 18736 if (ipif->ipif_zoneid != zoneid && 18737 ipif->ipif_zoneid != ALL_ZONES) 18738 continue; /* not this zone */ 18739 (void) ipif_get_name(ipif, 18740 ipm.ipGroupMemberIfIndex.o_bytes, 18741 OCTET_LENGTH); 18742 ipm.ipGroupMemberIfIndex.o_length = 18743 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 18744 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18745 ASSERT(ilm->ilm_ipif != NULL); 18746 ASSERT(ilm->ilm_ill == NULL); 18747 if (ilm->ilm_ipif != ipif) 18748 continue; 18749 ipm.ipGroupMemberAddress = ilm->ilm_addr; 18750 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 18751 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 18752 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18753 (char *)&ipm, (int)sizeof (ipm))) { 18754 ip1dbg(("ip_snmp_get_mib2_ip_group: " 18755 "failed to allocate %u bytes\n", 18756 (uint_t)sizeof (ipm))); 18757 } 18758 } 18759 } 18760 ILM_WALKER_RELE(ill); 18761 } 18762 rw_exit(&ipst->ips_ill_g_lock); 18763 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18764 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18765 (int)optp->level, (int)optp->name, (int)optp->len)); 18766 qreply(q, mpctl); 18767 return (mp2ctl); 18768 } 18769 18770 /* IPv6 multicast group membership. */ 18771 static mblk_t * 18772 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18773 { 18774 struct opthdr *optp; 18775 mblk_t *mp2ctl; 18776 ill_t *ill; 18777 ilm_t *ilm; 18778 ipv6_member_t ipm6; 18779 mblk_t *mp_tail = NULL; 18780 ill_walk_context_t ctx; 18781 zoneid_t zoneid; 18782 18783 /* 18784 * make a copy of the original message 18785 */ 18786 mp2ctl = copymsg(mpctl); 18787 zoneid = Q_TO_CONN(q)->conn_zoneid; 18788 18789 /* ip6GroupMember table */ 18790 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18791 optp->level = MIB2_IP6; 18792 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 18793 18794 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18795 ill = ILL_START_WALK_V6(&ctx, ipst); 18796 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18797 ILM_WALKER_HOLD(ill); 18798 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 18799 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18800 ASSERT(ilm->ilm_ipif == NULL); 18801 ASSERT(ilm->ilm_ill != NULL); 18802 if (ilm->ilm_zoneid != zoneid) 18803 continue; /* not this zone */ 18804 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 18805 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 18806 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 18807 if (!snmp_append_data2(mpctl->b_cont, 18808 &mp_tail, 18809 (char *)&ipm6, (int)sizeof (ipm6))) { 18810 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 18811 "failed to allocate %u bytes\n", 18812 (uint_t)sizeof (ipm6))); 18813 } 18814 } 18815 ILM_WALKER_RELE(ill); 18816 } 18817 rw_exit(&ipst->ips_ill_g_lock); 18818 18819 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18820 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18821 (int)optp->level, (int)optp->name, (int)optp->len)); 18822 qreply(q, mpctl); 18823 return (mp2ctl); 18824 } 18825 18826 /* IP multicast filtered sources */ 18827 static mblk_t * 18828 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18829 { 18830 struct opthdr *optp; 18831 mblk_t *mp2ctl; 18832 ill_t *ill; 18833 ipif_t *ipif; 18834 ilm_t *ilm; 18835 ip_grpsrc_t ips; 18836 mblk_t *mp_tail = NULL; 18837 ill_walk_context_t ctx; 18838 zoneid_t zoneid; 18839 int i; 18840 slist_t *sl; 18841 18842 /* 18843 * make a copy of the original message 18844 */ 18845 mp2ctl = copymsg(mpctl); 18846 zoneid = Q_TO_CONN(q)->conn_zoneid; 18847 18848 /* ipGroupSource table */ 18849 optp = (struct opthdr *)&mpctl->b_rptr[ 18850 sizeof (struct T_optmgmt_ack)]; 18851 optp->level = MIB2_IP; 18852 optp->name = EXPER_IP_GROUP_SOURCES; 18853 18854 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18855 ill = ILL_START_WALK_V4(&ctx, ipst); 18856 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18857 ILM_WALKER_HOLD(ill); 18858 for (ipif = ill->ill_ipif; ipif != NULL; 18859 ipif = ipif->ipif_next) { 18860 if (ipif->ipif_zoneid != zoneid) 18861 continue; /* not this zone */ 18862 (void) ipif_get_name(ipif, 18863 ips.ipGroupSourceIfIndex.o_bytes, 18864 OCTET_LENGTH); 18865 ips.ipGroupSourceIfIndex.o_length = 18866 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 18867 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18868 ASSERT(ilm->ilm_ipif != NULL); 18869 ASSERT(ilm->ilm_ill == NULL); 18870 sl = ilm->ilm_filter; 18871 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 18872 continue; 18873 ips.ipGroupSourceGroup = ilm->ilm_addr; 18874 for (i = 0; i < sl->sl_numsrc; i++) { 18875 if (!IN6_IS_ADDR_V4MAPPED( 18876 &sl->sl_addr[i])) 18877 continue; 18878 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 18879 ips.ipGroupSourceAddress); 18880 if (snmp_append_data2(mpctl->b_cont, 18881 &mp_tail, (char *)&ips, 18882 (int)sizeof (ips)) == 0) { 18883 ip1dbg(("ip_snmp_get_mib2_" 18884 "ip_group_src: failed to " 18885 "allocate %u bytes\n", 18886 (uint_t)sizeof (ips))); 18887 } 18888 } 18889 } 18890 } 18891 ILM_WALKER_RELE(ill); 18892 } 18893 rw_exit(&ipst->ips_ill_g_lock); 18894 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18895 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18896 (int)optp->level, (int)optp->name, (int)optp->len)); 18897 qreply(q, mpctl); 18898 return (mp2ctl); 18899 } 18900 18901 /* IPv6 multicast filtered sources. */ 18902 static mblk_t * 18903 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18904 { 18905 struct opthdr *optp; 18906 mblk_t *mp2ctl; 18907 ill_t *ill; 18908 ilm_t *ilm; 18909 ipv6_grpsrc_t ips6; 18910 mblk_t *mp_tail = NULL; 18911 ill_walk_context_t ctx; 18912 zoneid_t zoneid; 18913 int i; 18914 slist_t *sl; 18915 18916 /* 18917 * make a copy of the original message 18918 */ 18919 mp2ctl = copymsg(mpctl); 18920 zoneid = Q_TO_CONN(q)->conn_zoneid; 18921 18922 /* ip6GroupMember table */ 18923 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18924 optp->level = MIB2_IP6; 18925 optp->name = EXPER_IP6_GROUP_SOURCES; 18926 18927 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18928 ill = ILL_START_WALK_V6(&ctx, ipst); 18929 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18930 ILM_WALKER_HOLD(ill); 18931 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 18932 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18933 ASSERT(ilm->ilm_ipif == NULL); 18934 ASSERT(ilm->ilm_ill != NULL); 18935 sl = ilm->ilm_filter; 18936 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 18937 continue; 18938 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 18939 for (i = 0; i < sl->sl_numsrc; i++) { 18940 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 18941 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18942 (char *)&ips6, (int)sizeof (ips6))) { 18943 ip1dbg(("ip_snmp_get_mib2_ip6_" 18944 "group_src: failed to allocate " 18945 "%u bytes\n", 18946 (uint_t)sizeof (ips6))); 18947 } 18948 } 18949 } 18950 ILM_WALKER_RELE(ill); 18951 } 18952 rw_exit(&ipst->ips_ill_g_lock); 18953 18954 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18955 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18956 (int)optp->level, (int)optp->name, (int)optp->len)); 18957 qreply(q, mpctl); 18958 return (mp2ctl); 18959 } 18960 18961 /* Multicast routing virtual interface table. */ 18962 static mblk_t * 18963 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18964 { 18965 struct opthdr *optp; 18966 mblk_t *mp2ctl; 18967 18968 /* 18969 * make a copy of the original message 18970 */ 18971 mp2ctl = copymsg(mpctl); 18972 18973 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18974 optp->level = EXPER_DVMRP; 18975 optp->name = EXPER_DVMRP_VIF; 18976 if (!ip_mroute_vif(mpctl->b_cont, ipst)) { 18977 ip0dbg(("ip_mroute_vif: failed\n")); 18978 } 18979 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18980 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 18981 (int)optp->level, (int)optp->name, (int)optp->len)); 18982 qreply(q, mpctl); 18983 return (mp2ctl); 18984 } 18985 18986 /* Multicast routing table. */ 18987 static mblk_t * 18988 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18989 { 18990 struct opthdr *optp; 18991 mblk_t *mp2ctl; 18992 18993 /* 18994 * make a copy of the original message 18995 */ 18996 mp2ctl = copymsg(mpctl); 18997 18998 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18999 optp->level = EXPER_DVMRP; 19000 optp->name = EXPER_DVMRP_MRT; 19001 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) { 19002 ip0dbg(("ip_mroute_mrt: failed\n")); 19003 } 19004 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19005 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 19006 (int)optp->level, (int)optp->name, (int)optp->len)); 19007 qreply(q, mpctl); 19008 return (mp2ctl); 19009 } 19010 19011 /* 19012 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 19013 * in one IRE walk. 19014 */ 19015 static mblk_t * 19016 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19017 { 19018 struct opthdr *optp; 19019 mblk_t *mp2ctl; /* Returned */ 19020 mblk_t *mp3ctl; /* nettomedia */ 19021 mblk_t *mp4ctl; /* routeattrs */ 19022 iproutedata_t ird; 19023 zoneid_t zoneid; 19024 19025 /* 19026 * make copies of the original message 19027 * - mp2ctl is returned unchanged to the caller for his use 19028 * - mpctl is sent upstream as ipRouteEntryTable 19029 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 19030 * - mp4ctl is sent upstream as ipRouteAttributeTable 19031 */ 19032 mp2ctl = copymsg(mpctl); 19033 mp3ctl = copymsg(mpctl); 19034 mp4ctl = copymsg(mpctl); 19035 if (mp3ctl == NULL || mp4ctl == NULL) { 19036 freemsg(mp4ctl); 19037 freemsg(mp3ctl); 19038 freemsg(mp2ctl); 19039 freemsg(mpctl); 19040 return (NULL); 19041 } 19042 19043 bzero(&ird, sizeof (ird)); 19044 19045 ird.ird_route.lp_head = mpctl->b_cont; 19046 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 19047 ird.ird_attrs.lp_head = mp4ctl->b_cont; 19048 19049 zoneid = Q_TO_CONN(q)->conn_zoneid; 19050 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst); 19051 if (zoneid == GLOBAL_ZONEID) { 19052 /* 19053 * Those IREs are used by Mobile-IP; since mipagent(1M) 19054 * requires the sys_net_config or sys_ip_config privilege, 19055 * it can only run in the global zone or an exclusive-IP zone, 19056 * and both those have a conn_zoneid == GLOBAL_ZONEID. 19057 */ 19058 ire_walk_srcif_table_v4(ip_snmp_get2_v4, &ird, ipst); 19059 ire_walk_ill_mrtun(0, 0, ip_snmp_get2_v4, &ird, NULL, ipst); 19060 } 19061 19062 /* ipRouteEntryTable in mpctl */ 19063 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19064 optp->level = MIB2_IP; 19065 optp->name = MIB2_IP_ROUTE; 19066 optp->len = msgdsize(ird.ird_route.lp_head); 19067 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19068 (int)optp->level, (int)optp->name, (int)optp->len)); 19069 qreply(q, mpctl); 19070 19071 /* ipNetToMediaEntryTable in mp3ctl */ 19072 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19073 optp->level = MIB2_IP; 19074 optp->name = MIB2_IP_MEDIA; 19075 optp->len = msgdsize(ird.ird_netmedia.lp_head); 19076 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19077 (int)optp->level, (int)optp->name, (int)optp->len)); 19078 qreply(q, mp3ctl); 19079 19080 /* ipRouteAttributeTable in mp4ctl */ 19081 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19082 optp->level = MIB2_IP; 19083 optp->name = EXPER_IP_RTATTR; 19084 optp->len = msgdsize(ird.ird_attrs.lp_head); 19085 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19086 (int)optp->level, (int)optp->name, (int)optp->len)); 19087 if (optp->len == 0) 19088 freemsg(mp4ctl); 19089 else 19090 qreply(q, mp4ctl); 19091 19092 return (mp2ctl); 19093 } 19094 19095 /* 19096 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 19097 * ipv6NetToMediaEntryTable in an NDP walk. 19098 */ 19099 static mblk_t * 19100 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19101 { 19102 struct opthdr *optp; 19103 mblk_t *mp2ctl; /* Returned */ 19104 mblk_t *mp3ctl; /* nettomedia */ 19105 mblk_t *mp4ctl; /* routeattrs */ 19106 iproutedata_t ird; 19107 zoneid_t zoneid; 19108 19109 /* 19110 * make copies of the original message 19111 * - mp2ctl is returned unchanged to the caller for his use 19112 * - mpctl is sent upstream as ipv6RouteEntryTable 19113 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 19114 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 19115 */ 19116 mp2ctl = copymsg(mpctl); 19117 mp3ctl = copymsg(mpctl); 19118 mp4ctl = copymsg(mpctl); 19119 if (mp3ctl == NULL || mp4ctl == NULL) { 19120 freemsg(mp4ctl); 19121 freemsg(mp3ctl); 19122 freemsg(mp2ctl); 19123 freemsg(mpctl); 19124 return (NULL); 19125 } 19126 19127 bzero(&ird, sizeof (ird)); 19128 19129 ird.ird_route.lp_head = mpctl->b_cont; 19130 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 19131 ird.ird_attrs.lp_head = mp4ctl->b_cont; 19132 19133 zoneid = Q_TO_CONN(q)->conn_zoneid; 19134 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst); 19135 19136 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19137 optp->level = MIB2_IP6; 19138 optp->name = MIB2_IP6_ROUTE; 19139 optp->len = msgdsize(ird.ird_route.lp_head); 19140 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19141 (int)optp->level, (int)optp->name, (int)optp->len)); 19142 qreply(q, mpctl); 19143 19144 /* ipv6NetToMediaEntryTable in mp3ctl */ 19145 ndp_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst); 19146 19147 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19148 optp->level = MIB2_IP6; 19149 optp->name = MIB2_IP6_MEDIA; 19150 optp->len = msgdsize(ird.ird_netmedia.lp_head); 19151 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19152 (int)optp->level, (int)optp->name, (int)optp->len)); 19153 qreply(q, mp3ctl); 19154 19155 /* ipv6RouteAttributeTable in mp4ctl */ 19156 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19157 optp->level = MIB2_IP6; 19158 optp->name = EXPER_IP_RTATTR; 19159 optp->len = msgdsize(ird.ird_attrs.lp_head); 19160 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19161 (int)optp->level, (int)optp->name, (int)optp->len)); 19162 if (optp->len == 0) 19163 freemsg(mp4ctl); 19164 else 19165 qreply(q, mp4ctl); 19166 19167 return (mp2ctl); 19168 } 19169 19170 /* 19171 * IPv6 mib: One per ill 19172 */ 19173 static mblk_t * 19174 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19175 { 19176 struct opthdr *optp; 19177 mblk_t *mp2ctl; 19178 ill_t *ill; 19179 ill_walk_context_t ctx; 19180 mblk_t *mp_tail = NULL; 19181 19182 /* 19183 * Make a copy of the original message 19184 */ 19185 mp2ctl = copymsg(mpctl); 19186 19187 /* fixed length IPv6 structure ... */ 19188 19189 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19190 optp->level = MIB2_IP6; 19191 optp->name = 0; 19192 /* Include "unknown interface" ip6_mib */ 19193 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 19194 ipst->ips_ip6_mib.ipIfStatsIfIndex = 19195 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 19196 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding, 19197 ipst->ips_ipv6_forward ? 1 : 2); 19198 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit, 19199 ipst->ips_ipv6_def_hops); 19200 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize, 19201 sizeof (mib2_ipIfStatsEntry_t)); 19202 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize, 19203 sizeof (mib2_ipv6AddrEntry_t)); 19204 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize, 19205 sizeof (mib2_ipv6RouteEntry_t)); 19206 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize, 19207 sizeof (mib2_ipv6NetToMediaEntry_t)); 19208 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize, 19209 sizeof (ipv6_member_t)); 19210 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize, 19211 sizeof (ipv6_grpsrc_t)); 19212 19213 /* 19214 * Synchronize 64- and 32-bit counters 19215 */ 19216 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives, 19217 ipIfStatsHCInReceives); 19218 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers, 19219 ipIfStatsHCInDelivers); 19220 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests, 19221 ipIfStatsHCOutRequests); 19222 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams, 19223 ipIfStatsHCOutForwDatagrams); 19224 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts, 19225 ipIfStatsHCOutMcastPkts); 19226 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts, 19227 ipIfStatsHCInMcastPkts); 19228 19229 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19230 (char *)&ipst->ips_ip6_mib, (int)sizeof (ipst->ips_ip6_mib))) { 19231 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 19232 (uint_t)sizeof (ipst->ips_ip6_mib))); 19233 } 19234 19235 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19236 ill = ILL_START_WALK_V6(&ctx, ipst); 19237 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19238 ill->ill_ip_mib->ipIfStatsIfIndex = 19239 ill->ill_phyint->phyint_ifindex; 19240 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 19241 ipst->ips_ipv6_forward ? 1 : 2); 19242 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit, 19243 ill->ill_max_hops); 19244 19245 /* 19246 * Synchronize 64- and 32-bit counters 19247 */ 19248 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives, 19249 ipIfStatsHCInReceives); 19250 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers, 19251 ipIfStatsHCInDelivers); 19252 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests, 19253 ipIfStatsHCOutRequests); 19254 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams, 19255 ipIfStatsHCOutForwDatagrams); 19256 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts, 19257 ipIfStatsHCOutMcastPkts); 19258 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts, 19259 ipIfStatsHCInMcastPkts); 19260 19261 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19262 (char *)ill->ill_ip_mib, 19263 (int)sizeof (*ill->ill_ip_mib))) { 19264 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 19265 "%u bytes\n", 19266 (uint_t)sizeof (*ill->ill_ip_mib))); 19267 } 19268 } 19269 rw_exit(&ipst->ips_ill_g_lock); 19270 19271 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19272 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 19273 (int)optp->level, (int)optp->name, (int)optp->len)); 19274 qreply(q, mpctl); 19275 return (mp2ctl); 19276 } 19277 19278 /* 19279 * ICMPv6 mib: One per ill 19280 */ 19281 static mblk_t * 19282 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19283 { 19284 struct opthdr *optp; 19285 mblk_t *mp2ctl; 19286 ill_t *ill; 19287 ill_walk_context_t ctx; 19288 mblk_t *mp_tail = NULL; 19289 /* 19290 * Make a copy of the original message 19291 */ 19292 mp2ctl = copymsg(mpctl); 19293 19294 /* fixed length ICMPv6 structure ... */ 19295 19296 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19297 optp->level = MIB2_ICMP6; 19298 optp->name = 0; 19299 /* Include "unknown interface" icmp6_mib */ 19300 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex = 19301 MIB2_UNKNOWN_INTERFACE; /* netstat flag */ 19302 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize = 19303 sizeof (mib2_ipv6IfIcmpEntry_t); 19304 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19305 (char *)&ipst->ips_icmp6_mib, 19306 (int)sizeof (ipst->ips_icmp6_mib))) { 19307 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 19308 (uint_t)sizeof (ipst->ips_icmp6_mib))); 19309 } 19310 19311 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19312 ill = ILL_START_WALK_V6(&ctx, ipst); 19313 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19314 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 19315 ill->ill_phyint->phyint_ifindex; 19316 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19317 (char *)ill->ill_icmp6_mib, 19318 (int)sizeof (*ill->ill_icmp6_mib))) { 19319 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 19320 "%u bytes\n", 19321 (uint_t)sizeof (*ill->ill_icmp6_mib))); 19322 } 19323 } 19324 rw_exit(&ipst->ips_ill_g_lock); 19325 19326 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19327 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 19328 (int)optp->level, (int)optp->name, (int)optp->len)); 19329 qreply(q, mpctl); 19330 return (mp2ctl); 19331 } 19332 19333 /* 19334 * ire_walk routine to create both ipRouteEntryTable and 19335 * ipRouteAttributeTable in one IRE walk 19336 */ 19337 static void 19338 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 19339 { 19340 ill_t *ill; 19341 ipif_t *ipif; 19342 mib2_ipRouteEntry_t *re; 19343 mib2_ipAttributeEntry_t *iae, *iaeptr; 19344 ipaddr_t gw_addr; 19345 tsol_ire_gw_secattr_t *attrp; 19346 tsol_gc_t *gc = NULL; 19347 tsol_gcgrp_t *gcgrp = NULL; 19348 uint_t sacnt = 0; 19349 int i; 19350 19351 ASSERT(ire->ire_ipversion == IPV4_VERSION); 19352 19353 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19354 return; 19355 19356 if ((attrp = ire->ire_gw_secattr) != NULL) { 19357 mutex_enter(&attrp->igsa_lock); 19358 if ((gc = attrp->igsa_gc) != NULL) { 19359 gcgrp = gc->gc_grp; 19360 ASSERT(gcgrp != NULL); 19361 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19362 sacnt = 1; 19363 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19364 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19365 gc = gcgrp->gcgrp_head; 19366 sacnt = gcgrp->gcgrp_count; 19367 } 19368 mutex_exit(&attrp->igsa_lock); 19369 19370 /* do nothing if there's no gc to report */ 19371 if (gc == NULL) { 19372 ASSERT(sacnt == 0); 19373 if (gcgrp != NULL) { 19374 /* we might as well drop the lock now */ 19375 rw_exit(&gcgrp->gcgrp_rwlock); 19376 gcgrp = NULL; 19377 } 19378 attrp = NULL; 19379 } 19380 19381 ASSERT(gc == NULL || (gcgrp != NULL && 19382 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19383 } 19384 ASSERT(sacnt == 0 || gc != NULL); 19385 19386 if (sacnt != 0 && 19387 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19388 kmem_free(re, sizeof (*re)); 19389 rw_exit(&gcgrp->gcgrp_rwlock); 19390 return; 19391 } 19392 19393 /* 19394 * Return all IRE types for route table... let caller pick and choose 19395 */ 19396 re->ipRouteDest = ire->ire_addr; 19397 ipif = ire->ire_ipif; 19398 re->ipRouteIfIndex.o_length = 0; 19399 if (ire->ire_type == IRE_CACHE) { 19400 ill = (ill_t *)ire->ire_stq->q_ptr; 19401 re->ipRouteIfIndex.o_length = 19402 ill->ill_name_length == 0 ? 0 : 19403 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19404 bcopy(ill->ill_name, re->ipRouteIfIndex.o_bytes, 19405 re->ipRouteIfIndex.o_length); 19406 } else if (ipif != NULL) { 19407 (void) ipif_get_name(ipif, re->ipRouteIfIndex.o_bytes, 19408 OCTET_LENGTH); 19409 re->ipRouteIfIndex.o_length = 19410 mi_strlen(re->ipRouteIfIndex.o_bytes); 19411 } 19412 re->ipRouteMetric1 = -1; 19413 re->ipRouteMetric2 = -1; 19414 re->ipRouteMetric3 = -1; 19415 re->ipRouteMetric4 = -1; 19416 19417 gw_addr = ire->ire_gateway_addr; 19418 19419 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 19420 re->ipRouteNextHop = ire->ire_src_addr; 19421 else 19422 re->ipRouteNextHop = gw_addr; 19423 /* indirect(4), direct(3), or invalid(2) */ 19424 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19425 re->ipRouteType = 2; 19426 else 19427 re->ipRouteType = (gw_addr != 0) ? 4 : 3; 19428 re->ipRouteProto = -1; 19429 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 19430 re->ipRouteMask = ire->ire_mask; 19431 re->ipRouteMetric5 = -1; 19432 re->ipRouteInfo.re_max_frag = ire->ire_max_frag; 19433 re->ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 19434 re->ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19435 re->ipRouteInfo.re_ref = ire->ire_refcnt; 19436 re->ipRouteInfo.re_src_addr = ire->ire_src_addr; 19437 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19438 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19439 re->ipRouteInfo.re_flags = ire->ire_flags; 19440 re->ipRouteInfo.re_in_ill.o_length = 0; 19441 19442 if (ire->ire_flags & RTF_DYNAMIC) { 19443 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19444 } else { 19445 re->ipRouteInfo.re_ire_type = ire->ire_type; 19446 } 19447 19448 if (ire->ire_in_ill != NULL) { 19449 re->ipRouteInfo.re_in_ill.o_length = 19450 ire->ire_in_ill->ill_name_length == 0 ? 0 : 19451 MIN(OCTET_LENGTH, ire->ire_in_ill->ill_name_length - 1); 19452 bcopy(ire->ire_in_ill->ill_name, 19453 re->ipRouteInfo.re_in_ill.o_bytes, 19454 re->ipRouteInfo.re_in_ill.o_length); 19455 } 19456 re->ipRouteInfo.re_in_src_addr = ire->ire_in_src_addr; 19457 19458 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19459 (char *)re, (int)sizeof (*re))) { 19460 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19461 (uint_t)sizeof (*re))); 19462 } 19463 19464 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19465 iaeptr->iae_routeidx = ird->ird_idx; 19466 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19467 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19468 } 19469 19470 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19471 (char *)iae, sacnt * sizeof (*iae))) { 19472 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19473 (unsigned)(sacnt * sizeof (*iae)))); 19474 } 19475 19476 /* bump route index for next pass */ 19477 ird->ird_idx++; 19478 19479 kmem_free(re, sizeof (*re)); 19480 if (sacnt != 0) 19481 kmem_free(iae, sacnt * sizeof (*iae)); 19482 19483 if (gcgrp != NULL) 19484 rw_exit(&gcgrp->gcgrp_rwlock); 19485 } 19486 19487 /* 19488 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 19489 */ 19490 static void 19491 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 19492 { 19493 ill_t *ill; 19494 ipif_t *ipif; 19495 mib2_ipv6RouteEntry_t *re; 19496 mib2_ipAttributeEntry_t *iae, *iaeptr; 19497 in6_addr_t gw_addr_v6; 19498 tsol_ire_gw_secattr_t *attrp; 19499 tsol_gc_t *gc = NULL; 19500 tsol_gcgrp_t *gcgrp = NULL; 19501 uint_t sacnt = 0; 19502 int i; 19503 19504 ASSERT(ire->ire_ipversion == IPV6_VERSION); 19505 19506 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19507 return; 19508 19509 if ((attrp = ire->ire_gw_secattr) != NULL) { 19510 mutex_enter(&attrp->igsa_lock); 19511 if ((gc = attrp->igsa_gc) != NULL) { 19512 gcgrp = gc->gc_grp; 19513 ASSERT(gcgrp != NULL); 19514 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19515 sacnt = 1; 19516 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19517 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19518 gc = gcgrp->gcgrp_head; 19519 sacnt = gcgrp->gcgrp_count; 19520 } 19521 mutex_exit(&attrp->igsa_lock); 19522 19523 /* do nothing if there's no gc to report */ 19524 if (gc == NULL) { 19525 ASSERT(sacnt == 0); 19526 if (gcgrp != NULL) { 19527 /* we might as well drop the lock now */ 19528 rw_exit(&gcgrp->gcgrp_rwlock); 19529 gcgrp = NULL; 19530 } 19531 attrp = NULL; 19532 } 19533 19534 ASSERT(gc == NULL || (gcgrp != NULL && 19535 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19536 } 19537 ASSERT(sacnt == 0 || gc != NULL); 19538 19539 if (sacnt != 0 && 19540 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19541 kmem_free(re, sizeof (*re)); 19542 rw_exit(&gcgrp->gcgrp_rwlock); 19543 return; 19544 } 19545 19546 /* 19547 * Return all IRE types for route table... let caller pick and choose 19548 */ 19549 re->ipv6RouteDest = ire->ire_addr_v6; 19550 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 19551 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 19552 re->ipv6RouteIfIndex.o_length = 0; 19553 ipif = ire->ire_ipif; 19554 if (ire->ire_type == IRE_CACHE) { 19555 ill = (ill_t *)ire->ire_stq->q_ptr; 19556 re->ipv6RouteIfIndex.o_length = 19557 ill->ill_name_length == 0 ? 0 : 19558 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19559 bcopy(ill->ill_name, re->ipv6RouteIfIndex.o_bytes, 19560 re->ipv6RouteIfIndex.o_length); 19561 } else if (ipif != NULL) { 19562 (void) ipif_get_name(ipif, re->ipv6RouteIfIndex.o_bytes, 19563 OCTET_LENGTH); 19564 re->ipv6RouteIfIndex.o_length = 19565 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 19566 } 19567 19568 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19569 19570 mutex_enter(&ire->ire_lock); 19571 gw_addr_v6 = ire->ire_gateway_addr_v6; 19572 mutex_exit(&ire->ire_lock); 19573 19574 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 19575 re->ipv6RouteNextHop = ire->ire_src_addr_v6; 19576 else 19577 re->ipv6RouteNextHop = gw_addr_v6; 19578 19579 /* remote(4), local(3), or discard(2) */ 19580 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19581 re->ipv6RouteType = 2; 19582 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 19583 re->ipv6RouteType = 3; 19584 else 19585 re->ipv6RouteType = 4; 19586 19587 re->ipv6RouteProtocol = -1; 19588 re->ipv6RoutePolicy = 0; 19589 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 19590 re->ipv6RouteNextHopRDI = 0; 19591 re->ipv6RouteWeight = 0; 19592 re->ipv6RouteMetric = 0; 19593 re->ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 19594 re->ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 19595 re->ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19596 re->ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 19597 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19598 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19599 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 19600 re->ipv6RouteInfo.re_flags = ire->ire_flags; 19601 19602 if (ire->ire_flags & RTF_DYNAMIC) { 19603 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19604 } else { 19605 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 19606 } 19607 19608 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19609 (char *)re, (int)sizeof (*re))) { 19610 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19611 (uint_t)sizeof (*re))); 19612 } 19613 19614 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19615 iaeptr->iae_routeidx = ird->ird_idx; 19616 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19617 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19618 } 19619 19620 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19621 (char *)iae, sacnt * sizeof (*iae))) { 19622 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19623 (unsigned)(sacnt * sizeof (*iae)))); 19624 } 19625 19626 /* bump route index for next pass */ 19627 ird->ird_idx++; 19628 19629 kmem_free(re, sizeof (*re)); 19630 if (sacnt != 0) 19631 kmem_free(iae, sacnt * sizeof (*iae)); 19632 19633 if (gcgrp != NULL) 19634 rw_exit(&gcgrp->gcgrp_rwlock); 19635 } 19636 19637 /* 19638 * ndp_walk routine to create ipv6NetToMediaEntryTable 19639 */ 19640 static int 19641 ip_snmp_get2_v6_media(nce_t *nce, iproutedata_t *ird) 19642 { 19643 ill_t *ill; 19644 mib2_ipv6NetToMediaEntry_t ntme; 19645 dl_unitdata_req_t *dl; 19646 19647 ill = nce->nce_ill; 19648 if (ill->ill_isv6 == B_FALSE) /* skip arpce entry */ 19649 return (0); 19650 19651 /* 19652 * Neighbor cache entry attached to IRE with on-link 19653 * destination. 19654 */ 19655 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 19656 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 19657 if ((ill->ill_flags & ILLF_XRESOLV) && 19658 (nce->nce_res_mp != NULL)) { 19659 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 19660 ntme.ipv6NetToMediaPhysAddress.o_length = 19661 dl->dl_dest_addr_length; 19662 } else { 19663 ntme.ipv6NetToMediaPhysAddress.o_length = 19664 ill->ill_phys_addr_length; 19665 } 19666 if (nce->nce_res_mp != NULL) { 19667 bcopy((char *)nce->nce_res_mp->b_rptr + 19668 NCE_LL_ADDR_OFFSET(ill), 19669 ntme.ipv6NetToMediaPhysAddress.o_bytes, 19670 ntme.ipv6NetToMediaPhysAddress.o_length); 19671 } else { 19672 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 19673 ill->ill_phys_addr_length); 19674 } 19675 /* 19676 * Note: Returns ND_* states. Should be: 19677 * reachable(1), stale(2), delay(3), probe(4), 19678 * invalid(5), unknown(6) 19679 */ 19680 ntme.ipv6NetToMediaState = nce->nce_state; 19681 ntme.ipv6NetToMediaLastUpdated = 0; 19682 19683 /* other(1), dynamic(2), static(3), local(4) */ 19684 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 19685 ntme.ipv6NetToMediaType = 4; 19686 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 19687 ntme.ipv6NetToMediaType = 1; 19688 } else { 19689 ntme.ipv6NetToMediaType = 2; 19690 } 19691 19692 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 19693 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 19694 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 19695 (uint_t)sizeof (ntme))); 19696 } 19697 return (0); 19698 } 19699 19700 /* 19701 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 19702 */ 19703 /* ARGSUSED */ 19704 int 19705 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 19706 { 19707 switch (level) { 19708 case MIB2_IP: 19709 case MIB2_ICMP: 19710 switch (name) { 19711 default: 19712 break; 19713 } 19714 return (1); 19715 default: 19716 return (1); 19717 } 19718 } 19719 19720 /* 19721 * When there exists both a 64- and 32-bit counter of a particular type 19722 * (i.e., InReceives), only the 64-bit counters are added. 19723 */ 19724 void 19725 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2) 19726 { 19727 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors); 19728 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors); 19729 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes); 19730 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors); 19731 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos); 19732 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts); 19733 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards); 19734 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards); 19735 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs); 19736 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails); 19737 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates); 19738 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds); 19739 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs); 19740 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails); 19741 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes); 19742 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates); 19743 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups); 19744 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits); 19745 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs); 19746 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows); 19747 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows); 19748 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion, 19749 o2->ipIfStatsInWrongIPVersion); 19750 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion, 19751 o2->ipIfStatsInWrongIPVersion); 19752 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion, 19753 o2->ipIfStatsOutSwitchIPVersion); 19754 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives); 19755 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets); 19756 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams, 19757 o2->ipIfStatsHCInForwDatagrams); 19758 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers); 19759 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests); 19760 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams, 19761 o2->ipIfStatsHCOutForwDatagrams); 19762 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds); 19763 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits); 19764 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets); 19765 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts); 19766 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets); 19767 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts); 19768 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets, 19769 o2->ipIfStatsHCOutMcastOctets); 19770 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts); 19771 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts); 19772 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded); 19773 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed); 19774 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs); 19775 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs); 19776 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts); 19777 } 19778 19779 void 19780 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2) 19781 { 19782 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs); 19783 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors); 19784 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs); 19785 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs); 19786 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds); 19787 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems); 19788 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs); 19789 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos); 19790 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies); 19791 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits, 19792 o2->ipv6IfIcmpInRouterSolicits); 19793 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements, 19794 o2->ipv6IfIcmpInRouterAdvertisements); 19795 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits, 19796 o2->ipv6IfIcmpInNeighborSolicits); 19797 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements, 19798 o2->ipv6IfIcmpInNeighborAdvertisements); 19799 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects); 19800 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries, 19801 o2->ipv6IfIcmpInGroupMembQueries); 19802 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses, 19803 o2->ipv6IfIcmpInGroupMembResponses); 19804 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions, 19805 o2->ipv6IfIcmpInGroupMembReductions); 19806 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs); 19807 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors); 19808 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs, 19809 o2->ipv6IfIcmpOutDestUnreachs); 19810 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs, 19811 o2->ipv6IfIcmpOutAdminProhibs); 19812 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds); 19813 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems, 19814 o2->ipv6IfIcmpOutParmProblems); 19815 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs); 19816 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos); 19817 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies); 19818 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits, 19819 o2->ipv6IfIcmpOutRouterSolicits); 19820 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements, 19821 o2->ipv6IfIcmpOutRouterAdvertisements); 19822 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits, 19823 o2->ipv6IfIcmpOutNeighborSolicits); 19824 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements, 19825 o2->ipv6IfIcmpOutNeighborAdvertisements); 19826 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects); 19827 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries, 19828 o2->ipv6IfIcmpOutGroupMembQueries); 19829 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses, 19830 o2->ipv6IfIcmpOutGroupMembResponses); 19831 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions, 19832 o2->ipv6IfIcmpOutGroupMembReductions); 19833 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows); 19834 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit); 19835 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements, 19836 o2->ipv6IfIcmpInBadNeighborAdvertisements); 19837 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations, 19838 o2->ipv6IfIcmpInBadNeighborSolicitations); 19839 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects); 19840 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal, 19841 o2->ipv6IfIcmpInGroupMembTotal); 19842 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries, 19843 o2->ipv6IfIcmpInGroupMembBadQueries); 19844 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports, 19845 o2->ipv6IfIcmpInGroupMembBadReports); 19846 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports, 19847 o2->ipv6IfIcmpInGroupMembOurReports); 19848 } 19849 19850 /* 19851 * Called before the options are updated to check if this packet will 19852 * be source routed from here. 19853 * This routine assumes that the options are well formed i.e. that they 19854 * have already been checked. 19855 */ 19856 static boolean_t 19857 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst) 19858 { 19859 ipoptp_t opts; 19860 uchar_t *opt; 19861 uint8_t optval; 19862 uint8_t optlen; 19863 ipaddr_t dst; 19864 ire_t *ire; 19865 19866 if (IS_SIMPLE_IPH(ipha)) { 19867 ip2dbg(("not source routed\n")); 19868 return (B_FALSE); 19869 } 19870 dst = ipha->ipha_dst; 19871 for (optval = ipoptp_first(&opts, ipha); 19872 optval != IPOPT_EOL; 19873 optval = ipoptp_next(&opts)) { 19874 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 19875 opt = opts.ipoptp_cur; 19876 optlen = opts.ipoptp_len; 19877 ip2dbg(("ip_source_routed: opt %d, len %d\n", 19878 optval, optlen)); 19879 switch (optval) { 19880 uint32_t off; 19881 case IPOPT_SSRR: 19882 case IPOPT_LSRR: 19883 /* 19884 * If dst is one of our addresses and there are some 19885 * entries left in the source route return (true). 19886 */ 19887 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 19888 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 19889 if (ire == NULL) { 19890 ip2dbg(("ip_source_routed: not next" 19891 " source route 0x%x\n", 19892 ntohl(dst))); 19893 return (B_FALSE); 19894 } 19895 ire_refrele(ire); 19896 off = opt[IPOPT_OFFSET]; 19897 off--; 19898 if (optlen < IP_ADDR_LEN || 19899 off > optlen - IP_ADDR_LEN) { 19900 /* End of source route */ 19901 ip1dbg(("ip_source_routed: end of SR\n")); 19902 return (B_FALSE); 19903 } 19904 return (B_TRUE); 19905 } 19906 } 19907 ip2dbg(("not source routed\n")); 19908 return (B_FALSE); 19909 } 19910 19911 /* 19912 * Check if the packet contains any source route. 19913 */ 19914 static boolean_t 19915 ip_source_route_included(ipha_t *ipha) 19916 { 19917 ipoptp_t opts; 19918 uint8_t optval; 19919 19920 if (IS_SIMPLE_IPH(ipha)) 19921 return (B_FALSE); 19922 for (optval = ipoptp_first(&opts, ipha); 19923 optval != IPOPT_EOL; 19924 optval = ipoptp_next(&opts)) { 19925 switch (optval) { 19926 case IPOPT_SSRR: 19927 case IPOPT_LSRR: 19928 return (B_TRUE); 19929 } 19930 } 19931 return (B_FALSE); 19932 } 19933 19934 /* 19935 * Called when the IRE expiration timer fires. 19936 */ 19937 void 19938 ip_trash_timer_expire(void *args) 19939 { 19940 int flush_flag = 0; 19941 ire_expire_arg_t iea; 19942 ip_stack_t *ipst = (ip_stack_t *)args; 19943 19944 iea.iea_ipst = ipst; /* No netstack_hold */ 19945 19946 /* 19947 * ip_ire_expire_id is protected by ip_trash_timer_lock. 19948 * This lock makes sure that a new invocation of this function 19949 * that occurs due to an almost immediate timer firing will not 19950 * progress beyond this point until the current invocation is done 19951 */ 19952 mutex_enter(&ipst->ips_ip_trash_timer_lock); 19953 ipst->ips_ip_ire_expire_id = 0; 19954 mutex_exit(&ipst->ips_ip_trash_timer_lock); 19955 19956 /* Periodic timer */ 19957 if (ipst->ips_ip_ire_arp_time_elapsed >= 19958 ipst->ips_ip_ire_arp_interval) { 19959 /* 19960 * Remove all IRE_CACHE entries since they might 19961 * contain arp information. 19962 */ 19963 flush_flag |= FLUSH_ARP_TIME; 19964 ipst->ips_ip_ire_arp_time_elapsed = 0; 19965 IP_STAT(ipst, ip_ire_arp_timer_expired); 19966 } 19967 if (ipst->ips_ip_ire_rd_time_elapsed >= 19968 ipst->ips_ip_ire_redir_interval) { 19969 /* Remove all redirects */ 19970 flush_flag |= FLUSH_REDIRECT_TIME; 19971 ipst->ips_ip_ire_rd_time_elapsed = 0; 19972 IP_STAT(ipst, ip_ire_redirect_timer_expired); 19973 } 19974 if (ipst->ips_ip_ire_pmtu_time_elapsed >= 19975 ipst->ips_ip_ire_pathmtu_interval) { 19976 /* Increase path mtu */ 19977 flush_flag |= FLUSH_MTU_TIME; 19978 ipst->ips_ip_ire_pmtu_time_elapsed = 0; 19979 IP_STAT(ipst, ip_ire_pmtu_timer_expired); 19980 } 19981 19982 /* 19983 * Optimize for the case when there are no redirects in the 19984 * ftable, that is, no need to walk the ftable in that case. 19985 */ 19986 if (flush_flag & (FLUSH_MTU_TIME|FLUSH_ARP_TIME)) { 19987 iea.iea_flush_flag = flush_flag; 19988 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_CACHETABLE, ire_expire, 19989 (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 0, NULL, 19990 ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table, 19991 NULL, ALL_ZONES, ipst); 19992 } 19993 if ((flush_flag & FLUSH_REDIRECT_TIME) && 19994 ipst->ips_ip_redirect_cnt > 0) { 19995 iea.iea_flush_flag = flush_flag; 19996 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_FORWARDTABLE, 19997 ire_expire, (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 19998 0, NULL, 0, NULL, NULL, ALL_ZONES, ipst); 19999 } 20000 if (flush_flag & FLUSH_MTU_TIME) { 20001 /* 20002 * Walk all IPv6 IRE's and update them 20003 * Note that ARP and redirect timers are not 20004 * needed since NUD handles stale entries. 20005 */ 20006 flush_flag = FLUSH_MTU_TIME; 20007 iea.iea_flush_flag = flush_flag; 20008 ire_walk_v6(ire_expire, (char *)(uintptr_t)&iea, 20009 ALL_ZONES, ipst); 20010 } 20011 20012 ipst->ips_ip_ire_arp_time_elapsed += ipst->ips_ip_timer_interval; 20013 ipst->ips_ip_ire_rd_time_elapsed += ipst->ips_ip_timer_interval; 20014 ipst->ips_ip_ire_pmtu_time_elapsed += ipst->ips_ip_timer_interval; 20015 20016 /* 20017 * Hold the lock to serialize timeout calls and prevent 20018 * stale values in ip_ire_expire_id. Otherwise it is possible 20019 * for the timer to fire and a new invocation of this function 20020 * to start before the return value of timeout has been stored 20021 * in ip_ire_expire_id by the current invocation. 20022 */ 20023 mutex_enter(&ipst->ips_ip_trash_timer_lock); 20024 ipst->ips_ip_ire_expire_id = timeout(ip_trash_timer_expire, 20025 (void *)ipst, MSEC_TO_TICK(ipst->ips_ip_timer_interval)); 20026 mutex_exit(&ipst->ips_ip_trash_timer_lock); 20027 } 20028 20029 /* 20030 * Called by the memory allocator subsystem directly, when the system 20031 * is running low on memory. 20032 */ 20033 /* ARGSUSED */ 20034 void 20035 ip_trash_ire_reclaim(void *args) 20036 { 20037 netstack_handle_t nh; 20038 netstack_t *ns; 20039 20040 netstack_next_init(&nh); 20041 while ((ns = netstack_next(&nh)) != NULL) { 20042 ip_trash_ire_reclaim_stack(ns->netstack_ip); 20043 netstack_rele(ns); 20044 } 20045 netstack_next_fini(&nh); 20046 } 20047 20048 static void 20049 ip_trash_ire_reclaim_stack(ip_stack_t *ipst) 20050 { 20051 ire_cache_count_t icc; 20052 ire_cache_reclaim_t icr; 20053 ncc_cache_count_t ncc; 20054 nce_cache_reclaim_t ncr; 20055 uint_t delete_cnt; 20056 /* 20057 * Memory reclaim call back. 20058 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 20059 * Then, with a target of freeing 1/Nth of IRE_CACHE 20060 * entries, determine what fraction to free for 20061 * each category of IRE_CACHE entries giving absolute priority 20062 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 20063 * entry will be freed unless all offlink entries are freed). 20064 */ 20065 icc.icc_total = 0; 20066 icc.icc_unused = 0; 20067 icc.icc_offlink = 0; 20068 icc.icc_pmtu = 0; 20069 icc.icc_onlink = 0; 20070 ire_walk(ire_cache_count, (char *)&icc, ipst); 20071 20072 /* 20073 * Free NCEs for IPv6 like the onlink ires. 20074 */ 20075 ncc.ncc_total = 0; 20076 ncc.ncc_host = 0; 20077 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc, ipst); 20078 20079 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 20080 icc.icc_pmtu + icc.icc_onlink); 20081 delete_cnt = icc.icc_total/ipst->ips_ip_ire_reclaim_fraction; 20082 IP_STAT(ipst, ip_trash_ire_reclaim_calls); 20083 if (delete_cnt == 0) 20084 return; 20085 IP_STAT(ipst, ip_trash_ire_reclaim_success); 20086 /* Always delete all unused offlink entries */ 20087 icr.icr_ipst = ipst; 20088 icr.icr_unused = 1; 20089 if (delete_cnt <= icc.icc_unused) { 20090 /* 20091 * Only need to free unused entries. In other words, 20092 * there are enough unused entries to free to meet our 20093 * target number of freed ire cache entries. 20094 */ 20095 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 20096 ncr.ncr_host = 0; 20097 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 20098 /* 20099 * Only need to free unused entries, plus a fraction of offlink 20100 * entries. It follows from the first if statement that 20101 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 20102 */ 20103 delete_cnt -= icc.icc_unused; 20104 /* Round up # deleted by truncating fraction */ 20105 icr.icr_offlink = icc.icc_offlink / delete_cnt; 20106 icr.icr_pmtu = icr.icr_onlink = 0; 20107 ncr.ncr_host = 0; 20108 } else if (delete_cnt <= 20109 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 20110 /* 20111 * Free all unused and offlink entries, plus a fraction of 20112 * pmtu entries. It follows from the previous if statement 20113 * that icc_pmtu is non-zero, and that 20114 * delete_cnt != icc_unused + icc_offlink. 20115 */ 20116 icr.icr_offlink = 1; 20117 delete_cnt -= icc.icc_unused + icc.icc_offlink; 20118 /* Round up # deleted by truncating fraction */ 20119 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 20120 icr.icr_onlink = 0; 20121 ncr.ncr_host = 0; 20122 } else { 20123 /* 20124 * Free all unused, offlink, and pmtu entries, plus a fraction 20125 * of onlink entries. If we're here, then we know that 20126 * icc_onlink is non-zero, and that 20127 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 20128 */ 20129 icr.icr_offlink = icr.icr_pmtu = 1; 20130 delete_cnt -= icc.icc_unused + icc.icc_offlink + 20131 icc.icc_pmtu; 20132 /* Round up # deleted by truncating fraction */ 20133 icr.icr_onlink = icc.icc_onlink / delete_cnt; 20134 /* Using the same delete fraction as for onlink IREs */ 20135 ncr.ncr_host = ncc.ncc_host / delete_cnt; 20136 } 20137 #ifdef DEBUG 20138 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 20139 "fractions %d/%d/%d/%d\n", 20140 icc.icc_total/ipst->ips_ip_ire_reclaim_fraction, icc.icc_total, 20141 icc.icc_unused, icc.icc_offlink, 20142 icc.icc_pmtu, icc.icc_onlink, 20143 icr.icr_unused, icr.icr_offlink, 20144 icr.icr_pmtu, icr.icr_onlink)); 20145 #endif 20146 ire_walk(ire_cache_reclaim, (char *)&icr, ipst); 20147 if (ncr.ncr_host != 0) 20148 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 20149 (uchar_t *)&ncr, ipst); 20150 #ifdef DEBUG 20151 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 20152 icc.icc_pmtu = 0; icc.icc_onlink = 0; 20153 ire_walk(ire_cache_count, (char *)&icc, ipst); 20154 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 20155 icc.icc_total, icc.icc_unused, icc.icc_offlink, 20156 icc.icc_pmtu, icc.icc_onlink)); 20157 #endif 20158 } 20159 20160 /* 20161 * ip_unbind is called when a copy of an unbind request is received from the 20162 * upper level protocol. We remove this conn from any fanout hash list it is 20163 * on, and zero out the bind information. No reply is expected up above. 20164 */ 20165 mblk_t * 20166 ip_unbind(queue_t *q, mblk_t *mp) 20167 { 20168 conn_t *connp = Q_TO_CONN(q); 20169 20170 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 20171 20172 if (is_system_labeled() && connp->conn_anon_port) { 20173 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 20174 connp->conn_mlp_type, connp->conn_ulp, 20175 ntohs(connp->conn_lport), B_FALSE); 20176 connp->conn_anon_port = 0; 20177 } 20178 connp->conn_mlp_type = mlptSingle; 20179 20180 ipcl_hash_remove(connp); 20181 20182 ASSERT(mp->b_cont == NULL); 20183 /* 20184 * Convert mp into a T_OK_ACK 20185 */ 20186 mp = mi_tpi_ok_ack_alloc(mp); 20187 20188 /* 20189 * should not happen in practice... T_OK_ACK is smaller than the 20190 * original message. 20191 */ 20192 if (mp == NULL) 20193 return (NULL); 20194 20195 /* 20196 * Don't bzero the ports if its TCP since TCP still needs the 20197 * lport to remove it from its own bind hash. TCP will do the 20198 * cleanup. 20199 */ 20200 if (!IPCL_IS_TCP(connp)) 20201 bzero(&connp->u_port, sizeof (connp->u_port)); 20202 20203 return (mp); 20204 } 20205 20206 /* 20207 * Write side put procedure. Outbound data, IOCTLs, responses from 20208 * resolvers, etc, come down through here. 20209 * 20210 * arg2 is always a queue_t *. 20211 * When that queue is an ill_t (i.e. q_next != NULL), then arg must be 20212 * the zoneid. 20213 * When that queue is not an ill_t, then arg must be a conn_t pointer. 20214 */ 20215 void 20216 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 20217 { 20218 ip_output_options(arg, mp, arg2, caller, &zero_info); 20219 } 20220 20221 void 20222 ip_output_options(void *arg, mblk_t *mp, void *arg2, int caller, 20223 ip_opt_info_t *infop) 20224 { 20225 conn_t *connp = NULL; 20226 queue_t *q = (queue_t *)arg2; 20227 ipha_t *ipha; 20228 #define rptr ((uchar_t *)ipha) 20229 ire_t *ire = NULL; 20230 ire_t *sctp_ire = NULL; 20231 uint32_t v_hlen_tos_len; 20232 ipaddr_t dst; 20233 mblk_t *first_mp = NULL; 20234 boolean_t mctl_present; 20235 ipsec_out_t *io; 20236 int match_flags; 20237 ill_t *attach_ill = NULL; 20238 /* Bind to IPIF_NOFAILOVER ill etc. */ 20239 ill_t *xmit_ill = NULL; /* IP_XMIT_IF etc. */ 20240 ipif_t *dst_ipif; 20241 boolean_t multirt_need_resolve = B_FALSE; 20242 mblk_t *copy_mp = NULL; 20243 int err; 20244 zoneid_t zoneid; 20245 int adjust; 20246 uint16_t iplen; 20247 boolean_t need_decref = B_FALSE; 20248 boolean_t ignore_dontroute = B_FALSE; 20249 boolean_t ignore_nexthop = B_FALSE; 20250 boolean_t ip_nexthop = B_FALSE; 20251 ipaddr_t nexthop_addr; 20252 ip_stack_t *ipst; 20253 20254 #ifdef _BIG_ENDIAN 20255 #define V_HLEN (v_hlen_tos_len >> 24) 20256 #else 20257 #define V_HLEN (v_hlen_tos_len & 0xFF) 20258 #endif 20259 20260 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 20261 "ip_wput_start: q %p", q); 20262 20263 /* 20264 * ip_wput fast path 20265 */ 20266 20267 /* is packet from ARP ? */ 20268 if (q->q_next != NULL) { 20269 zoneid = (zoneid_t)(uintptr_t)arg; 20270 goto qnext; 20271 } 20272 20273 connp = (conn_t *)arg; 20274 ASSERT(connp != NULL); 20275 zoneid = connp->conn_zoneid; 20276 ipst = connp->conn_netstack->netstack_ip; 20277 20278 /* is queue flow controlled? */ 20279 if ((q->q_first != NULL || connp->conn_draining) && 20280 (caller == IP_WPUT)) { 20281 ASSERT(!need_decref); 20282 (void) putq(q, mp); 20283 return; 20284 } 20285 20286 /* Multidata transmit? */ 20287 if (DB_TYPE(mp) == M_MULTIDATA) { 20288 /* 20289 * We should never get here, since all Multidata messages 20290 * originating from tcp should have been directed over to 20291 * tcp_multisend() in the first place. 20292 */ 20293 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20294 freemsg(mp); 20295 return; 20296 } else if (DB_TYPE(mp) != M_DATA) 20297 goto notdata; 20298 20299 if (mp->b_flag & MSGHASREF) { 20300 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20301 mp->b_flag &= ~MSGHASREF; 20302 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 20303 need_decref = B_TRUE; 20304 } 20305 ipha = (ipha_t *)mp->b_rptr; 20306 20307 /* is IP header non-aligned or mblk smaller than basic IP header */ 20308 #ifndef SAFETY_BEFORE_SPEED 20309 if (!OK_32PTR(rptr) || 20310 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 20311 goto hdrtoosmall; 20312 #endif 20313 20314 ASSERT(OK_32PTR(ipha)); 20315 20316 /* 20317 * This function assumes that mp points to an IPv4 packet. If it's the 20318 * wrong version, we'll catch it again in ip_output_v6. 20319 * 20320 * Note that this is *only* locally-generated output here, and never 20321 * forwarded data, and that we need to deal only with transports that 20322 * don't know how to label. (TCP, UDP, and ICMP/raw-IP all know how to 20323 * label.) 20324 */ 20325 if (is_system_labeled() && 20326 (ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4) && 20327 !connp->conn_ulp_labeled) { 20328 err = tsol_check_label(BEST_CRED(mp, connp), &mp, &adjust, 20329 connp->conn_mac_exempt, ipst); 20330 ipha = (ipha_t *)mp->b_rptr; 20331 if (err != 0) { 20332 first_mp = mp; 20333 if (err == EINVAL) 20334 goto icmp_parameter_problem; 20335 ip2dbg(("ip_wput: label check failed (%d)\n", err)); 20336 goto discard_pkt; 20337 } 20338 iplen = ntohs(ipha->ipha_length) + adjust; 20339 ipha->ipha_length = htons(iplen); 20340 } 20341 20342 ASSERT(infop != NULL); 20343 20344 if (infop->ip_opt_flags & IP_VERIFY_SRC) { 20345 /* 20346 * IP_PKTINFO ancillary option is present. 20347 * IPCL_ZONEID is used to honor IP_ALLZONES option which 20348 * allows using address of any zone as the source address. 20349 */ 20350 ire = ire_ctable_lookup(ipha->ipha_src, 0, 20351 (IRE_LOCAL|IRE_LOOPBACK), NULL, IPCL_ZONEID(connp), 20352 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 20353 if (ire == NULL) 20354 goto drop_pkt; 20355 ire_refrele(ire); 20356 ire = NULL; 20357 } 20358 20359 /* 20360 * IP_DONTFAILOVER_IF and IP_XMIT_IF have precedence over 20361 * ill index passed in IP_PKTINFO. 20362 */ 20363 if (infop->ip_opt_ill_index != 0 && 20364 connp->conn_xmit_if_ill == NULL && 20365 connp->conn_nofailover_ill == NULL) { 20366 20367 xmit_ill = ill_lookup_on_ifindex( 20368 infop->ip_opt_ill_index, B_FALSE, NULL, NULL, NULL, NULL, 20369 ipst); 20370 20371 if (xmit_ill == NULL || IS_VNI(xmit_ill)) 20372 goto drop_pkt; 20373 /* 20374 * check that there is an ipif belonging 20375 * to our zone. IPCL_ZONEID is not used because 20376 * IP_ALLZONES option is valid only when the ill is 20377 * accessible from all zones i.e has a valid ipif in 20378 * all zones. 20379 */ 20380 if (!ipif_lookup_zoneid_group(xmit_ill, zoneid, 0, NULL)) { 20381 goto drop_pkt; 20382 } 20383 } 20384 20385 /* 20386 * If there is a policy, try to attach an ipsec_out in 20387 * the front. At the end, first_mp either points to a 20388 * M_DATA message or IPSEC_OUT message linked to a 20389 * M_DATA message. We have to do it now as we might 20390 * lose the "conn" if we go through ip_newroute. 20391 */ 20392 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 20393 if (((mp = ipsec_attach_ipsec_out(mp, connp, NULL, 20394 ipha->ipha_protocol, ipst->ips_netstack)) == NULL)) { 20395 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20396 if (need_decref) 20397 CONN_DEC_REF(connp); 20398 return; 20399 } else { 20400 ASSERT(mp->b_datap->db_type == M_CTL); 20401 first_mp = mp; 20402 mp = mp->b_cont; 20403 mctl_present = B_TRUE; 20404 } 20405 } else { 20406 first_mp = mp; 20407 mctl_present = B_FALSE; 20408 } 20409 20410 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20411 20412 /* is wrong version or IP options present */ 20413 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 20414 goto version_hdrlen_check; 20415 dst = ipha->ipha_dst; 20416 20417 if (connp->conn_nofailover_ill != NULL) { 20418 attach_ill = conn_get_held_ill(connp, 20419 &connp->conn_nofailover_ill, &err); 20420 if (err == ILL_LOOKUP_FAILED) { 20421 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20422 if (need_decref) 20423 CONN_DEC_REF(connp); 20424 freemsg(first_mp); 20425 return; 20426 } 20427 } 20428 20429 20430 /* is packet multicast? */ 20431 if (CLASSD(dst)) 20432 goto multicast; 20433 20434 /* 20435 * If xmit_ill is set above due to index passed in ip_pkt_info. It 20436 * takes precedence over conn_dontroute and conn_nexthop_set 20437 */ 20438 if (xmit_ill != NULL) { 20439 goto send_from_ill; 20440 } 20441 20442 if ((connp->conn_dontroute) || (connp->conn_xmit_if_ill != NULL) || 20443 (connp->conn_nexthop_set)) { 20444 /* 20445 * If the destination is a broadcast or a loopback 20446 * address, SO_DONTROUTE, IP_XMIT_IF and IP_NEXTHOP go 20447 * through the standard path. But in the case of local 20448 * destination only SO_DONTROUTE and IP_NEXTHOP go through 20449 * the standard path not IP_XMIT_IF. 20450 */ 20451 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20452 if ((ire == NULL) || ((ire->ire_type != IRE_BROADCAST) && 20453 (ire->ire_type != IRE_LOOPBACK))) { 20454 if ((connp->conn_dontroute || 20455 connp->conn_nexthop_set) && (ire != NULL) && 20456 (ire->ire_type == IRE_LOCAL)) 20457 goto standard_path; 20458 20459 if (ire != NULL) { 20460 ire_refrele(ire); 20461 /* No more access to ire */ 20462 ire = NULL; 20463 } 20464 /* 20465 * bypass routing checks and go directly to 20466 * interface. 20467 */ 20468 if (connp->conn_dontroute) { 20469 goto dontroute; 20470 } else if (connp->conn_nexthop_set) { 20471 ip_nexthop = B_TRUE; 20472 nexthop_addr = connp->conn_nexthop_v4; 20473 goto send_from_ill; 20474 } 20475 20476 /* 20477 * If IP_XMIT_IF socket option is set, 20478 * then we allow unicast and multicast 20479 * packets to go through the ill. It is 20480 * quite possible that the destination 20481 * is not in the ire cache table and we 20482 * do not want to go to ip_newroute() 20483 * instead we call ip_newroute_ipif. 20484 */ 20485 xmit_ill = conn_get_held_ill(connp, 20486 &connp->conn_xmit_if_ill, &err); 20487 if (err == ILL_LOOKUP_FAILED) { 20488 BUMP_MIB(&ipst->ips_ip_mib, 20489 ipIfStatsOutDiscards); 20490 if (attach_ill != NULL) 20491 ill_refrele(attach_ill); 20492 if (need_decref) 20493 CONN_DEC_REF(connp); 20494 freemsg(first_mp); 20495 return; 20496 } 20497 goto send_from_ill; 20498 } 20499 standard_path: 20500 /* Must be a broadcast, a loopback or a local ire */ 20501 if (ire != NULL) { 20502 ire_refrele(ire); 20503 /* No more access to ire */ 20504 ire = NULL; 20505 } 20506 } 20507 20508 if (attach_ill != NULL) 20509 goto send_from_ill; 20510 20511 /* 20512 * We cache IRE_CACHEs to avoid lookups. We don't do 20513 * this for the tcp global queue and listen end point 20514 * as it does not really have a real destination to 20515 * talk to. This is also true for SCTP. 20516 */ 20517 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 20518 !connp->conn_fully_bound) { 20519 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20520 if (ire == NULL) 20521 goto noirefound; 20522 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20523 "ip_wput_end: q %p (%S)", q, "end"); 20524 20525 /* 20526 * Check if the ire has the RTF_MULTIRT flag, inherited 20527 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20528 */ 20529 if (ire->ire_flags & RTF_MULTIRT) { 20530 20531 /* 20532 * Force the TTL of multirouted packets if required. 20533 * The TTL of such packets is bounded by the 20534 * ip_multirt_ttl ndd variable. 20535 */ 20536 if ((ipst->ips_ip_multirt_ttl > 0) && 20537 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20538 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20539 "(was %d), dst 0x%08x\n", 20540 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20541 ntohl(ire->ire_addr))); 20542 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20543 } 20544 /* 20545 * We look at this point if there are pending 20546 * unresolved routes. ire_multirt_resolvable() 20547 * checks in O(n) that all IRE_OFFSUBNET ire 20548 * entries for the packet's destination and 20549 * flagged RTF_MULTIRT are currently resolved. 20550 * If some remain unresolved, we make a copy 20551 * of the current message. It will be used 20552 * to initiate additional route resolutions. 20553 */ 20554 multirt_need_resolve = 20555 ire_multirt_need_resolve(ire->ire_addr, 20556 MBLK_GETLABEL(first_mp), ipst); 20557 ip2dbg(("ip_wput[TCP]: ire %p, " 20558 "multirt_need_resolve %d, first_mp %p\n", 20559 (void *)ire, multirt_need_resolve, 20560 (void *)first_mp)); 20561 if (multirt_need_resolve) { 20562 copy_mp = copymsg(first_mp); 20563 if (copy_mp != NULL) { 20564 MULTIRT_DEBUG_TAG(copy_mp); 20565 } 20566 } 20567 } 20568 20569 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20570 20571 /* 20572 * Try to resolve another multiroute if 20573 * ire_multirt_need_resolve() deemed it necessary. 20574 */ 20575 if (copy_mp != NULL) { 20576 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid, ipst); 20577 } 20578 if (need_decref) 20579 CONN_DEC_REF(connp); 20580 return; 20581 } 20582 20583 /* 20584 * Access to conn_ire_cache. (protected by conn_lock) 20585 * 20586 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 20587 * the ire bucket lock here to check for CONDEMNED as it is okay to 20588 * send a packet or two with the IRE_CACHE that is going away. 20589 * Access to the ire requires an ire refhold on the ire prior to 20590 * its use since an interface unplumb thread may delete the cached 20591 * ire and release the refhold at any time. 20592 * 20593 * Caching an ire in the conn_ire_cache 20594 * 20595 * o Caching an ire pointer in the conn requires a strict check for 20596 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 20597 * ires before cleaning up the conns. So the caching of an ire pointer 20598 * in the conn is done after making sure under the bucket lock that the 20599 * ire has not yet been marked CONDEMNED. Otherwise we will end up 20600 * caching an ire after the unplumb thread has cleaned up the conn. 20601 * If the conn does not send a packet subsequently the unplumb thread 20602 * will be hanging waiting for the ire count to drop to zero. 20603 * 20604 * o We also need to atomically test for a null conn_ire_cache and 20605 * set the conn_ire_cache under the the protection of the conn_lock 20606 * to avoid races among concurrent threads trying to simultaneously 20607 * cache an ire in the conn_ire_cache. 20608 */ 20609 mutex_enter(&connp->conn_lock); 20610 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 20611 20612 if (ire != NULL && ire->ire_addr == dst && 20613 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20614 20615 IRE_REFHOLD(ire); 20616 mutex_exit(&connp->conn_lock); 20617 20618 } else { 20619 boolean_t cached = B_FALSE; 20620 connp->conn_ire_cache = NULL; 20621 mutex_exit(&connp->conn_lock); 20622 /* Release the old ire */ 20623 if (ire != NULL && sctp_ire == NULL) 20624 IRE_REFRELE_NOTR(ire); 20625 20626 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20627 if (ire == NULL) 20628 goto noirefound; 20629 IRE_REFHOLD_NOTR(ire); 20630 20631 mutex_enter(&connp->conn_lock); 20632 if (CONN_CACHE_IRE(connp) && connp->conn_ire_cache == NULL) { 20633 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 20634 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20635 connp->conn_ire_cache = ire; 20636 cached = B_TRUE; 20637 } 20638 rw_exit(&ire->ire_bucket->irb_lock); 20639 } 20640 mutex_exit(&connp->conn_lock); 20641 20642 /* 20643 * We can continue to use the ire but since it was 20644 * not cached, we should drop the extra reference. 20645 */ 20646 if (!cached) 20647 IRE_REFRELE_NOTR(ire); 20648 } 20649 20650 20651 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20652 "ip_wput_end: q %p (%S)", q, "end"); 20653 20654 /* 20655 * Check if the ire has the RTF_MULTIRT flag, inherited 20656 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20657 */ 20658 if (ire->ire_flags & RTF_MULTIRT) { 20659 20660 /* 20661 * Force the TTL of multirouted packets if required. 20662 * The TTL of such packets is bounded by the 20663 * ip_multirt_ttl ndd variable. 20664 */ 20665 if ((ipst->ips_ip_multirt_ttl > 0) && 20666 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20667 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20668 "(was %d), dst 0x%08x\n", 20669 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20670 ntohl(ire->ire_addr))); 20671 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20672 } 20673 20674 /* 20675 * At this point, we check to see if there are any pending 20676 * unresolved routes. ire_multirt_resolvable() 20677 * checks in O(n) that all IRE_OFFSUBNET ire 20678 * entries for the packet's destination and 20679 * flagged RTF_MULTIRT are currently resolved. 20680 * If some remain unresolved, we make a copy 20681 * of the current message. It will be used 20682 * to initiate additional route resolutions. 20683 */ 20684 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 20685 MBLK_GETLABEL(first_mp), ipst); 20686 ip2dbg(("ip_wput[not TCP]: ire %p, " 20687 "multirt_need_resolve %d, first_mp %p\n", 20688 (void *)ire, multirt_need_resolve, (void *)first_mp)); 20689 if (multirt_need_resolve) { 20690 copy_mp = copymsg(first_mp); 20691 if (copy_mp != NULL) { 20692 MULTIRT_DEBUG_TAG(copy_mp); 20693 } 20694 } 20695 } 20696 20697 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20698 20699 /* 20700 * Try to resolve another multiroute if 20701 * ire_multirt_resolvable() deemed it necessary 20702 */ 20703 if (copy_mp != NULL) { 20704 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid, ipst); 20705 } 20706 if (need_decref) 20707 CONN_DEC_REF(connp); 20708 return; 20709 20710 qnext: 20711 /* 20712 * Upper Level Protocols pass down complete IP datagrams 20713 * as M_DATA messages. Everything else is a sideshow. 20714 * 20715 * 1) We could be re-entering ip_wput because of ip_neworute 20716 * in which case we could have a IPSEC_OUT message. We 20717 * need to pass through ip_wput like other datagrams and 20718 * hence cannot branch to ip_wput_nondata. 20719 * 20720 * 2) ARP, AH, ESP, and other clients who are on the module 20721 * instance of IP stream, give us something to deal with. 20722 * We will handle AH and ESP here and rest in ip_wput_nondata. 20723 * 20724 * 3) ICMP replies also could come here. 20725 */ 20726 ipst = ILLQ_TO_IPST(q); 20727 20728 if (DB_TYPE(mp) != M_DATA) { 20729 notdata: 20730 if (DB_TYPE(mp) == M_CTL) { 20731 /* 20732 * M_CTL messages are used by ARP, AH and ESP to 20733 * communicate with IP. We deal with IPSEC_IN and 20734 * IPSEC_OUT here. ip_wput_nondata handles other 20735 * cases. 20736 */ 20737 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 20738 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 20739 first_mp = mp->b_cont; 20740 first_mp->b_flag &= ~MSGHASREF; 20741 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20742 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 20743 CONN_DEC_REF(connp); 20744 connp = NULL; 20745 } 20746 if (ii->ipsec_info_type == IPSEC_IN) { 20747 /* 20748 * Either this message goes back to 20749 * IPSEC for further processing or to 20750 * ULP after policy checks. 20751 */ 20752 ip_fanout_proto_again(mp, NULL, NULL, NULL); 20753 return; 20754 } else if (ii->ipsec_info_type == IPSEC_OUT) { 20755 io = (ipsec_out_t *)ii; 20756 if (io->ipsec_out_proc_begin) { 20757 /* 20758 * IPSEC processing has already started. 20759 * Complete it. 20760 * IPQoS notes: We don't care what is 20761 * in ipsec_out_ill_index since this 20762 * won't be processed for IPQoS policies 20763 * in ipsec_out_process. 20764 */ 20765 ipsec_out_process(q, mp, NULL, 20766 io->ipsec_out_ill_index); 20767 return; 20768 } else { 20769 connp = (q->q_next != NULL) ? 20770 NULL : Q_TO_CONN(q); 20771 first_mp = mp; 20772 mp = mp->b_cont; 20773 mctl_present = B_TRUE; 20774 } 20775 zoneid = io->ipsec_out_zoneid; 20776 ASSERT(zoneid != ALL_ZONES); 20777 } else if (ii->ipsec_info_type == IPSEC_CTL) { 20778 /* 20779 * It's an IPsec control message requesting 20780 * an SADB update to be sent to the IPsec 20781 * hardware acceleration capable ills. 20782 */ 20783 ipsec_ctl_t *ipsec_ctl = 20784 (ipsec_ctl_t *)mp->b_rptr; 20785 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 20786 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 20787 mblk_t *cmp = mp->b_cont; 20788 20789 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 20790 ASSERT(cmp != NULL); 20791 20792 freeb(mp); 20793 ill_ipsec_capab_send_all(satype, cmp, sa, 20794 ipst->ips_netstack); 20795 return; 20796 } else { 20797 /* 20798 * This must be ARP or special TSOL signaling. 20799 */ 20800 ip_wput_nondata(NULL, q, mp, NULL); 20801 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20802 "ip_wput_end: q %p (%S)", q, "nondata"); 20803 return; 20804 } 20805 } else { 20806 /* 20807 * This must be non-(ARP/AH/ESP) messages. 20808 */ 20809 ASSERT(!need_decref); 20810 ip_wput_nondata(NULL, q, mp, NULL); 20811 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20812 "ip_wput_end: q %p (%S)", q, "nondata"); 20813 return; 20814 } 20815 } else { 20816 first_mp = mp; 20817 mctl_present = B_FALSE; 20818 } 20819 20820 ASSERT(first_mp != NULL); 20821 /* 20822 * ICMP echo replies attach an ipsec_out and set ipsec_out_attach_if 20823 * to make sure that this packet goes out on the same interface it 20824 * came in. We handle that here. 20825 */ 20826 if (mctl_present) { 20827 uint_t ifindex; 20828 20829 io = (ipsec_out_t *)first_mp->b_rptr; 20830 if (io->ipsec_out_attach_if || 20831 io->ipsec_out_xmit_if || 20832 io->ipsec_out_ip_nexthop) { 20833 ill_t *ill; 20834 20835 /* 20836 * We may have lost the conn context if we are 20837 * coming here from ip_newroute(). Copy the 20838 * nexthop information. 20839 */ 20840 if (io->ipsec_out_ip_nexthop) { 20841 ip_nexthop = B_TRUE; 20842 nexthop_addr = io->ipsec_out_nexthop_addr; 20843 20844 ipha = (ipha_t *)mp->b_rptr; 20845 dst = ipha->ipha_dst; 20846 goto send_from_ill; 20847 } else { 20848 ASSERT(io->ipsec_out_ill_index != 0); 20849 ifindex = io->ipsec_out_ill_index; 20850 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 20851 NULL, NULL, NULL, NULL, ipst); 20852 /* 20853 * ipsec_out_xmit_if bit is used to tell 20854 * ip_wput to use the ill to send outgoing data 20855 * as we have no conn when data comes from ICMP 20856 * error msg routines. Currently this feature is 20857 * only used by ip_mrtun_forward routine. 20858 */ 20859 if (io->ipsec_out_xmit_if) { 20860 xmit_ill = ill; 20861 if (xmit_ill == NULL) { 20862 ip1dbg(("ip_output:bad ifindex " 20863 "for xmit_ill %d\n", 20864 ifindex)); 20865 freemsg(first_mp); 20866 BUMP_MIB(&ipst->ips_ip_mib, 20867 ipIfStatsOutDiscards); 20868 ASSERT(!need_decref); 20869 return; 20870 } 20871 /* Free up the ipsec_out_t mblk */ 20872 ASSERT(first_mp->b_cont == mp); 20873 first_mp->b_cont = NULL; 20874 freeb(first_mp); 20875 /* Just send the IP header+ICMP+data */ 20876 first_mp = mp; 20877 ipha = (ipha_t *)mp->b_rptr; 20878 dst = ipha->ipha_dst; 20879 goto send_from_ill; 20880 } else { 20881 attach_ill = ill; 20882 } 20883 20884 if (attach_ill == NULL) { 20885 ASSERT(xmit_ill == NULL); 20886 ip1dbg(("ip_output: bad ifindex for " 20887 "(BIND TO IPIF_NOFAILOVER) %d\n", 20888 ifindex)); 20889 freemsg(first_mp); 20890 BUMP_MIB(&ipst->ips_ip_mib, 20891 ipIfStatsOutDiscards); 20892 ASSERT(!need_decref); 20893 return; 20894 } 20895 } 20896 } 20897 } 20898 20899 ASSERT(xmit_ill == NULL); 20900 20901 /* We have a complete IP datagram heading outbound. */ 20902 ipha = (ipha_t *)mp->b_rptr; 20903 20904 #ifndef SPEED_BEFORE_SAFETY 20905 /* 20906 * Make sure we have a full-word aligned message and that at least 20907 * a simple IP header is accessible in the first message. If not, 20908 * try a pullup. 20909 */ 20910 if (!OK_32PTR(rptr) || 20911 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) { 20912 hdrtoosmall: 20913 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 20914 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20915 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 20916 if (first_mp == NULL) 20917 first_mp = mp; 20918 goto discard_pkt; 20919 } 20920 20921 /* This function assumes that mp points to an IPv4 packet. */ 20922 if (is_system_labeled() && q->q_next == NULL && 20923 (*mp->b_rptr & 0xf0) == (IPV4_VERSION << 4) && 20924 !connp->conn_ulp_labeled) { 20925 err = tsol_check_label(BEST_CRED(mp, connp), &mp, 20926 &adjust, connp->conn_mac_exempt, ipst); 20927 ipha = (ipha_t *)mp->b_rptr; 20928 if (first_mp != NULL) 20929 first_mp->b_cont = mp; 20930 if (err != 0) { 20931 if (first_mp == NULL) 20932 first_mp = mp; 20933 if (err == EINVAL) 20934 goto icmp_parameter_problem; 20935 ip2dbg(("ip_wput: label check failed (%d)\n", 20936 err)); 20937 goto discard_pkt; 20938 } 20939 iplen = ntohs(ipha->ipha_length) + adjust; 20940 ipha->ipha_length = htons(iplen); 20941 } 20942 20943 ipha = (ipha_t *)mp->b_rptr; 20944 if (first_mp == NULL) { 20945 ASSERT(attach_ill == NULL && xmit_ill == NULL); 20946 /* 20947 * If we got here because of "goto hdrtoosmall" 20948 * We need to attach a IPSEC_OUT. 20949 */ 20950 if (connp->conn_out_enforce_policy) { 20951 if (((mp = ipsec_attach_ipsec_out(mp, connp, 20952 NULL, ipha->ipha_protocol, 20953 ipst->ips_netstack)) == NULL)) { 20954 BUMP_MIB(&ipst->ips_ip_mib, 20955 ipIfStatsOutDiscards); 20956 if (need_decref) 20957 CONN_DEC_REF(connp); 20958 return; 20959 } else { 20960 ASSERT(mp->b_datap->db_type == M_CTL); 20961 first_mp = mp; 20962 mp = mp->b_cont; 20963 mctl_present = B_TRUE; 20964 } 20965 } else { 20966 first_mp = mp; 20967 mctl_present = B_FALSE; 20968 } 20969 } 20970 } 20971 #endif 20972 20973 /* Most of the code below is written for speed, not readability */ 20974 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20975 20976 /* 20977 * If ip_newroute() fails, we're going to need a full 20978 * header for the icmp wraparound. 20979 */ 20980 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 20981 uint_t v_hlen; 20982 version_hdrlen_check: 20983 ASSERT(first_mp != NULL); 20984 v_hlen = V_HLEN; 20985 /* 20986 * siphon off IPv6 packets coming down from transport 20987 * layer modules here. 20988 * Note: high-order bit carries NUD reachability confirmation 20989 */ 20990 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 20991 /* 20992 * XXX implement a IPv4 and IPv6 packet counter per 20993 * conn and switch when ratio exceeds e.g. 10:1 20994 */ 20995 #ifdef notyet 20996 if (q->q_next == NULL) /* Avoid ill queue */ 20997 ip_setqinfo(RD(q), B_TRUE, B_TRUE, ipst); 20998 #endif 20999 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutWrongIPVersion); 21000 ASSERT(xmit_ill == NULL); 21001 if (attach_ill != NULL) 21002 ill_refrele(attach_ill); 21003 if (need_decref) 21004 mp->b_flag |= MSGHASREF; 21005 (void) ip_output_v6(arg, first_mp, arg2, caller); 21006 return; 21007 } 21008 21009 if ((v_hlen >> 4) != IP_VERSION) { 21010 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21011 "ip_wput_end: q %p (%S)", q, "badvers"); 21012 goto discard_pkt; 21013 } 21014 /* 21015 * Is the header length at least 20 bytes? 21016 * 21017 * Are there enough bytes accessible in the header? If 21018 * not, try a pullup. 21019 */ 21020 v_hlen &= 0xF; 21021 v_hlen <<= 2; 21022 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 21023 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21024 "ip_wput_end: q %p (%S)", q, "badlen"); 21025 goto discard_pkt; 21026 } 21027 if (v_hlen > (mp->b_wptr - rptr)) { 21028 if (!pullupmsg(mp, v_hlen)) { 21029 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21030 "ip_wput_end: q %p (%S)", q, "badpullup2"); 21031 goto discard_pkt; 21032 } 21033 ipha = (ipha_t *)mp->b_rptr; 21034 } 21035 /* 21036 * Move first entry from any source route into ipha_dst and 21037 * verify the options 21038 */ 21039 if (ip_wput_options(q, first_mp, ipha, mctl_present, 21040 zoneid, ipst)) { 21041 ASSERT(xmit_ill == NULL); 21042 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21043 if (attach_ill != NULL) 21044 ill_refrele(attach_ill); 21045 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21046 "ip_wput_end: q %p (%S)", q, "badopts"); 21047 if (need_decref) 21048 CONN_DEC_REF(connp); 21049 return; 21050 } 21051 } 21052 dst = ipha->ipha_dst; 21053 21054 /* 21055 * Try to get an IRE_CACHE for the destination address. If we can't, 21056 * we have to run the packet through ip_newroute which will take 21057 * the appropriate action to arrange for an IRE_CACHE, such as querying 21058 * a resolver, or assigning a default gateway, etc. 21059 */ 21060 if (CLASSD(dst)) { 21061 ipif_t *ipif; 21062 uint32_t setsrc = 0; 21063 21064 multicast: 21065 ASSERT(first_mp != NULL); 21066 ip2dbg(("ip_wput: CLASSD\n")); 21067 if (connp == NULL) { 21068 /* 21069 * Use the first good ipif on the ill. 21070 * XXX Should this ever happen? (Appears 21071 * to show up with just ppp and no ethernet due 21072 * to in.rdisc.) 21073 * However, ire_send should be able to 21074 * call ip_wput_ire directly. 21075 * 21076 * XXX Also, this can happen for ICMP and other packets 21077 * with multicast source addresses. Perhaps we should 21078 * fix things so that we drop the packet in question, 21079 * but for now, just run with it. 21080 */ 21081 ill_t *ill = (ill_t *)q->q_ptr; 21082 21083 /* 21084 * Don't honor attach_if for this case. If ill 21085 * is part of the group, ipif could belong to 21086 * any ill and we cannot maintain attach_ill 21087 * and ipif_ill same anymore and the assert 21088 * below would fail. 21089 */ 21090 if (mctl_present && io->ipsec_out_attach_if) { 21091 io->ipsec_out_ill_index = 0; 21092 io->ipsec_out_attach_if = B_FALSE; 21093 ASSERT(attach_ill != NULL); 21094 ill_refrele(attach_ill); 21095 attach_ill = NULL; 21096 } 21097 21098 ASSERT(attach_ill == NULL); 21099 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 21100 if (ipif == NULL) { 21101 if (need_decref) 21102 CONN_DEC_REF(connp); 21103 freemsg(first_mp); 21104 return; 21105 } 21106 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 21107 ntohl(dst), ill->ill_name)); 21108 } else { 21109 /* 21110 * The order of precedence is IP_XMIT_IF, IP_PKTINFO 21111 * and IP_MULTICAST_IF. 21112 * Block comment above this function explains the 21113 * locking mechanism used here 21114 */ 21115 if (xmit_ill == NULL) { 21116 xmit_ill = conn_get_held_ill(connp, 21117 &connp->conn_xmit_if_ill, &err); 21118 if (err == ILL_LOOKUP_FAILED) { 21119 ip1dbg(("ip_wput: No ill for " 21120 "IP_XMIT_IF\n")); 21121 BUMP_MIB(&ipst->ips_ip_mib, 21122 ipIfStatsOutNoRoutes); 21123 goto drop_pkt; 21124 } 21125 } 21126 21127 if (xmit_ill == NULL) { 21128 ipif = conn_get_held_ipif(connp, 21129 &connp->conn_multicast_ipif, &err); 21130 if (err == IPIF_LOOKUP_FAILED) { 21131 ip1dbg(("ip_wput: No ipif for " 21132 "multicast\n")); 21133 BUMP_MIB(&ipst->ips_ip_mib, 21134 ipIfStatsOutNoRoutes); 21135 goto drop_pkt; 21136 } 21137 } 21138 if (xmit_ill != NULL) { 21139 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21140 if (ipif == NULL) { 21141 ip1dbg(("ip_wput: No ipif for " 21142 "IP_XMIT_IF\n")); 21143 BUMP_MIB(&ipst->ips_ip_mib, 21144 ipIfStatsOutNoRoutes); 21145 goto drop_pkt; 21146 } 21147 } else if (ipif == NULL || ipif->ipif_isv6) { 21148 /* 21149 * We must do this ipif determination here 21150 * else we could pass through ip_newroute 21151 * and come back here without the conn context. 21152 * 21153 * Note: we do late binding i.e. we bind to 21154 * the interface when the first packet is sent. 21155 * For performance reasons we do not rebind on 21156 * each packet but keep the binding until the 21157 * next IP_MULTICAST_IF option. 21158 * 21159 * conn_multicast_{ipif,ill} are shared between 21160 * IPv4 and IPv6 and AF_INET6 sockets can 21161 * send both IPv4 and IPv6 packets. Hence 21162 * we have to check that "isv6" matches above. 21163 */ 21164 if (ipif != NULL) 21165 ipif_refrele(ipif); 21166 ipif = ipif_lookup_group(dst, zoneid, ipst); 21167 if (ipif == NULL) { 21168 ip1dbg(("ip_wput: No ipif for " 21169 "multicast\n")); 21170 BUMP_MIB(&ipst->ips_ip_mib, 21171 ipIfStatsOutNoRoutes); 21172 goto drop_pkt; 21173 } 21174 err = conn_set_held_ipif(connp, 21175 &connp->conn_multicast_ipif, ipif); 21176 if (err == IPIF_LOOKUP_FAILED) { 21177 ipif_refrele(ipif); 21178 ip1dbg(("ip_wput: No ipif for " 21179 "multicast\n")); 21180 BUMP_MIB(&ipst->ips_ip_mib, 21181 ipIfStatsOutNoRoutes); 21182 goto drop_pkt; 21183 } 21184 } 21185 } 21186 ASSERT(!ipif->ipif_isv6); 21187 /* 21188 * As we may lose the conn by the time we reach ip_wput_ire, 21189 * we copy conn_multicast_loop and conn_dontroute on to an 21190 * ipsec_out. In case if this datagram goes out secure, 21191 * we need the ill_index also. Copy that also into the 21192 * ipsec_out. 21193 */ 21194 if (mctl_present) { 21195 io = (ipsec_out_t *)first_mp->b_rptr; 21196 ASSERT(first_mp->b_datap->db_type == M_CTL); 21197 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21198 } else { 21199 ASSERT(mp == first_mp); 21200 if ((first_mp = allocb(sizeof (ipsec_info_t), 21201 BPRI_HI)) == NULL) { 21202 ipif_refrele(ipif); 21203 first_mp = mp; 21204 goto discard_pkt; 21205 } 21206 first_mp->b_datap->db_type = M_CTL; 21207 first_mp->b_wptr += sizeof (ipsec_info_t); 21208 /* ipsec_out_secure is B_FALSE now */ 21209 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 21210 io = (ipsec_out_t *)first_mp->b_rptr; 21211 io->ipsec_out_type = IPSEC_OUT; 21212 io->ipsec_out_len = sizeof (ipsec_out_t); 21213 io->ipsec_out_use_global_policy = B_TRUE; 21214 io->ipsec_out_ns = ipst->ips_netstack; 21215 first_mp->b_cont = mp; 21216 mctl_present = B_TRUE; 21217 } 21218 if (attach_ill != NULL) { 21219 ASSERT(attach_ill == ipif->ipif_ill); 21220 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21221 21222 /* 21223 * Check if we need an ire that will not be 21224 * looked up by anybody else i.e. HIDDEN. 21225 */ 21226 if (ill_is_probeonly(attach_ill)) { 21227 match_flags |= MATCH_IRE_MARK_HIDDEN; 21228 } 21229 io->ipsec_out_ill_index = 21230 attach_ill->ill_phyint->phyint_ifindex; 21231 io->ipsec_out_attach_if = B_TRUE; 21232 } else { 21233 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 21234 io->ipsec_out_ill_index = 21235 ipif->ipif_ill->ill_phyint->phyint_ifindex; 21236 } 21237 if (connp != NULL) { 21238 io->ipsec_out_multicast_loop = 21239 connp->conn_multicast_loop; 21240 io->ipsec_out_dontroute = connp->conn_dontroute; 21241 io->ipsec_out_zoneid = connp->conn_zoneid; 21242 } 21243 /* 21244 * If the application uses IP_MULTICAST_IF with 21245 * different logical addresses of the same ILL, we 21246 * need to make sure that the soruce address of 21247 * the packet matches the logical IP address used 21248 * in the option. We do it by initializing ipha_src 21249 * here. This should keep IPSEC also happy as 21250 * when we return from IPSEC processing, we don't 21251 * have to worry about getting the right address on 21252 * the packet. Thus it is sufficient to look for 21253 * IRE_CACHE using MATCH_IRE_ILL rathen than 21254 * MATCH_IRE_IPIF. 21255 * 21256 * NOTE : We need to do it for non-secure case also as 21257 * this might go out secure if there is a global policy 21258 * match in ip_wput_ire. For bind to IPIF_NOFAILOVER 21259 * address, the source should be initialized already and 21260 * hence we won't be initializing here. 21261 * 21262 * As we do not have the ire yet, it is possible that 21263 * we set the source address here and then later discover 21264 * that the ire implies the source address to be assigned 21265 * through the RTF_SETSRC flag. 21266 * In that case, the setsrc variable will remind us 21267 * that overwritting the source address by the one 21268 * of the RTF_SETSRC-flagged ire is allowed. 21269 */ 21270 if (ipha->ipha_src == INADDR_ANY && 21271 (connp == NULL || !connp->conn_unspec_src)) { 21272 ipha->ipha_src = ipif->ipif_src_addr; 21273 setsrc = RTF_SETSRC; 21274 } 21275 /* 21276 * Find an IRE which matches the destination and the outgoing 21277 * queue (i.e. the outgoing interface.) 21278 * For loopback use a unicast IP address for 21279 * the ire lookup. 21280 */ 21281 if (ipif->ipif_ill->ill_phyint->phyint_flags & 21282 PHYI_LOOPBACK) { 21283 dst = ipif->ipif_lcl_addr; 21284 } 21285 /* 21286 * If IP_XMIT_IF is set, we branch out to ip_newroute_ipif. 21287 * We don't need to lookup ire in ctable as the packet 21288 * needs to be sent to the destination through the specified 21289 * ill irrespective of ires in the cache table. 21290 */ 21291 ire = NULL; 21292 if (xmit_ill == NULL) { 21293 ire = ire_ctable_lookup(dst, 0, 0, ipif, 21294 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21295 } 21296 21297 /* 21298 * refrele attach_ill as its not needed anymore. 21299 */ 21300 if (attach_ill != NULL) { 21301 ill_refrele(attach_ill); 21302 attach_ill = NULL; 21303 } 21304 21305 if (ire == NULL) { 21306 /* 21307 * Multicast loopback and multicast forwarding is 21308 * done in ip_wput_ire. 21309 * 21310 * Mark this packet to make it be delivered to 21311 * ip_wput_ire after the new ire has been 21312 * created. 21313 * 21314 * The call to ip_newroute_ipif takes into account 21315 * the setsrc reminder. In any case, we take care 21316 * of the RTF_MULTIRT flag. 21317 */ 21318 mp->b_prev = mp->b_next = NULL; 21319 if (xmit_ill == NULL || 21320 xmit_ill->ill_ipif_up_count > 0) { 21321 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 21322 setsrc | RTF_MULTIRT, zoneid, infop); 21323 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21324 "ip_wput_end: q %p (%S)", q, "noire"); 21325 } else { 21326 freemsg(first_mp); 21327 } 21328 ipif_refrele(ipif); 21329 if (xmit_ill != NULL) 21330 ill_refrele(xmit_ill); 21331 if (need_decref) 21332 CONN_DEC_REF(connp); 21333 return; 21334 } 21335 21336 ipif_refrele(ipif); 21337 ipif = NULL; 21338 ASSERT(xmit_ill == NULL); 21339 21340 /* 21341 * Honor the RTF_SETSRC flag for multicast packets, 21342 * if allowed by the setsrc reminder. 21343 */ 21344 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 21345 ipha->ipha_src = ire->ire_src_addr; 21346 } 21347 21348 /* 21349 * Unconditionally force the TTL to 1 for 21350 * multirouted multicast packets: 21351 * multirouted multicast should not cross 21352 * multicast routers. 21353 */ 21354 if (ire->ire_flags & RTF_MULTIRT) { 21355 if (ipha->ipha_ttl > 1) { 21356 ip2dbg(("ip_wput: forcing multicast " 21357 "multirt TTL to 1 (was %d), dst 0x%08x\n", 21358 ipha->ipha_ttl, ntohl(ire->ire_addr))); 21359 ipha->ipha_ttl = 1; 21360 } 21361 } 21362 } else { 21363 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 21364 if ((ire != NULL) && (ire->ire_type & 21365 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 21366 ignore_dontroute = B_TRUE; 21367 ignore_nexthop = B_TRUE; 21368 } 21369 if (ire != NULL) { 21370 ire_refrele(ire); 21371 ire = NULL; 21372 } 21373 /* 21374 * Guard against coming in from arp in which case conn is NULL. 21375 * Also guard against non M_DATA with dontroute set but 21376 * destined to local, loopback or broadcast addresses. 21377 */ 21378 if (connp != NULL && connp->conn_dontroute && 21379 !ignore_dontroute) { 21380 dontroute: 21381 /* 21382 * Set TTL to 1 if SO_DONTROUTE is set to prevent 21383 * routing protocols from seeing false direct 21384 * connectivity. 21385 */ 21386 ipha->ipha_ttl = 1; 21387 /* 21388 * If IP_XMIT_IF is also set (conn_xmit_if_ill != NULL) 21389 * along with SO_DONTROUTE, higher precedence is 21390 * given to IP_XMIT_IF and the IP_XMIT_IF ipif is used. 21391 */ 21392 if (connp->conn_xmit_if_ill == NULL) { 21393 /* If suitable ipif not found, drop packet */ 21394 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid, 21395 ipst); 21396 if (dst_ipif == NULL) { 21397 ip1dbg(("ip_wput: no route for " 21398 "dst using SO_DONTROUTE\n")); 21399 BUMP_MIB(&ipst->ips_ip_mib, 21400 ipIfStatsOutNoRoutes); 21401 mp->b_prev = mp->b_next = NULL; 21402 if (first_mp == NULL) 21403 first_mp = mp; 21404 goto drop_pkt; 21405 } else { 21406 /* 21407 * If suitable ipif has been found, set 21408 * xmit_ill to the corresponding 21409 * ipif_ill because we'll be following 21410 * the IP_XMIT_IF logic. 21411 */ 21412 ASSERT(xmit_ill == NULL); 21413 xmit_ill = dst_ipif->ipif_ill; 21414 mutex_enter(&xmit_ill->ill_lock); 21415 if (!ILL_CAN_LOOKUP(xmit_ill)) { 21416 mutex_exit(&xmit_ill->ill_lock); 21417 xmit_ill = NULL; 21418 ipif_refrele(dst_ipif); 21419 ip1dbg(("ip_wput: no route for" 21420 " dst using" 21421 " SO_DONTROUTE\n")); 21422 BUMP_MIB(&ipst->ips_ip_mib, 21423 ipIfStatsOutNoRoutes); 21424 mp->b_prev = mp->b_next = NULL; 21425 if (first_mp == NULL) 21426 first_mp = mp; 21427 goto drop_pkt; 21428 } 21429 ill_refhold_locked(xmit_ill); 21430 mutex_exit(&xmit_ill->ill_lock); 21431 ipif_refrele(dst_ipif); 21432 } 21433 } 21434 21435 } 21436 /* 21437 * If we are bound to IPIF_NOFAILOVER address, look for 21438 * an IRE_CACHE matching the ill. 21439 */ 21440 send_from_ill: 21441 if (attach_ill != NULL) { 21442 ipif_t *attach_ipif; 21443 21444 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21445 21446 /* 21447 * Check if we need an ire that will not be 21448 * looked up by anybody else i.e. HIDDEN. 21449 */ 21450 if (ill_is_probeonly(attach_ill)) { 21451 match_flags |= MATCH_IRE_MARK_HIDDEN; 21452 } 21453 21454 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 21455 if (attach_ipif == NULL) { 21456 ip1dbg(("ip_wput: No ipif for attach_ill\n")); 21457 goto discard_pkt; 21458 } 21459 ire = ire_ctable_lookup(dst, 0, 0, attach_ipif, 21460 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21461 ipif_refrele(attach_ipif); 21462 } else if (xmit_ill != NULL || (connp != NULL && 21463 connp->conn_xmit_if_ill != NULL)) { 21464 /* 21465 * Mark this packet as originated locally 21466 */ 21467 mp->b_prev = mp->b_next = NULL; 21468 /* 21469 * xmit_ill could be NULL if SO_DONTROUTE 21470 * is also set. 21471 */ 21472 if (xmit_ill == NULL) { 21473 xmit_ill = conn_get_held_ill(connp, 21474 &connp->conn_xmit_if_ill, &err); 21475 if (err == ILL_LOOKUP_FAILED) { 21476 BUMP_MIB(&ipst->ips_ip_mib, 21477 ipIfStatsOutDiscards); 21478 if (need_decref) 21479 CONN_DEC_REF(connp); 21480 freemsg(first_mp); 21481 return; 21482 } 21483 if (xmit_ill == NULL) { 21484 if (connp->conn_dontroute) 21485 goto dontroute; 21486 goto send_from_ill; 21487 } 21488 } 21489 /* 21490 * Could be SO_DONTROUTE case also. 21491 * check at least one interface is UP as 21492 * specified by this ILL 21493 */ 21494 if (xmit_ill->ill_ipif_up_count > 0) { 21495 ipif_t *ipif; 21496 21497 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21498 if (ipif == NULL) { 21499 ip1dbg(("ip_output: " 21500 "xmit_ill NULL ipif\n")); 21501 goto drop_pkt; 21502 } 21503 /* 21504 * Look for a ire that is part of the group, 21505 * if found use it else call ip_newroute_ipif. 21506 * IPCL_ZONEID is not used for matching because 21507 * IP_ALLZONES option is valid only when the 21508 * ill is accessible from all zones i.e has a 21509 * valid ipif in all zones. 21510 */ 21511 match_flags = MATCH_IRE_ILL_GROUP | 21512 MATCH_IRE_SECATTR; 21513 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 21514 MBLK_GETLABEL(mp), match_flags, ipst); 21515 /* 21516 * If an ire exists use it or else create 21517 * an ire but don't add it to the cache. 21518 * Adding an ire may cause issues with 21519 * asymmetric routing. 21520 * In case of multiroute always act as if 21521 * ire does not exist. 21522 */ 21523 if (ire == NULL || 21524 ire->ire_flags & RTF_MULTIRT) { 21525 if (ire != NULL) 21526 ire_refrele(ire); 21527 ip_newroute_ipif(q, first_mp, ipif, 21528 dst, connp, 0, zoneid, infop); 21529 ipif_refrele(ipif); 21530 ip1dbg(("ip_wput: ip_unicast_if\n")); 21531 ill_refrele(xmit_ill); 21532 if (need_decref) 21533 CONN_DEC_REF(connp); 21534 return; 21535 } 21536 ipif_refrele(ipif); 21537 } else { 21538 goto drop_pkt; 21539 } 21540 } else if (ip_nexthop || (connp != NULL && 21541 (connp->conn_nexthop_set)) && !ignore_nexthop) { 21542 if (!ip_nexthop) { 21543 ip_nexthop = B_TRUE; 21544 nexthop_addr = connp->conn_nexthop_v4; 21545 } 21546 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 21547 MATCH_IRE_GW; 21548 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 21549 NULL, zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21550 } else { 21551 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), 21552 ipst); 21553 } 21554 if (!ire) { 21555 /* 21556 * Make sure we don't load spread if this 21557 * is IPIF_NOFAILOVER case. 21558 */ 21559 if ((attach_ill != NULL) || 21560 (ip_nexthop && !ignore_nexthop)) { 21561 if (mctl_present) { 21562 io = (ipsec_out_t *)first_mp->b_rptr; 21563 ASSERT(first_mp->b_datap->db_type == 21564 M_CTL); 21565 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21566 } else { 21567 ASSERT(mp == first_mp); 21568 first_mp = allocb( 21569 sizeof (ipsec_info_t), BPRI_HI); 21570 if (first_mp == NULL) { 21571 first_mp = mp; 21572 goto discard_pkt; 21573 } 21574 first_mp->b_datap->db_type = M_CTL; 21575 first_mp->b_wptr += 21576 sizeof (ipsec_info_t); 21577 /* ipsec_out_secure is B_FALSE now */ 21578 bzero(first_mp->b_rptr, 21579 sizeof (ipsec_info_t)); 21580 io = (ipsec_out_t *)first_mp->b_rptr; 21581 io->ipsec_out_type = IPSEC_OUT; 21582 io->ipsec_out_len = 21583 sizeof (ipsec_out_t); 21584 io->ipsec_out_use_global_policy = 21585 B_TRUE; 21586 io->ipsec_out_ns = ipst->ips_netstack; 21587 first_mp->b_cont = mp; 21588 mctl_present = B_TRUE; 21589 } 21590 if (attach_ill != NULL) { 21591 io->ipsec_out_ill_index = attach_ill-> 21592 ill_phyint->phyint_ifindex; 21593 io->ipsec_out_attach_if = B_TRUE; 21594 } else { 21595 io->ipsec_out_ip_nexthop = ip_nexthop; 21596 io->ipsec_out_nexthop_addr = 21597 nexthop_addr; 21598 } 21599 } 21600 noirefound: 21601 /* 21602 * Mark this packet as having originated on 21603 * this machine. This will be noted in 21604 * ire_add_then_send, which needs to know 21605 * whether to run it back through ip_wput or 21606 * ip_rput following successful resolution. 21607 */ 21608 mp->b_prev = NULL; 21609 mp->b_next = NULL; 21610 ip_newroute(q, first_mp, dst, NULL, connp, zoneid, 21611 ipst); 21612 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21613 "ip_wput_end: q %p (%S)", q, "newroute"); 21614 if (attach_ill != NULL) 21615 ill_refrele(attach_ill); 21616 if (xmit_ill != NULL) 21617 ill_refrele(xmit_ill); 21618 if (need_decref) 21619 CONN_DEC_REF(connp); 21620 return; 21621 } 21622 } 21623 21624 /* We now know where we are going with it. */ 21625 21626 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21627 "ip_wput_end: q %p (%S)", q, "end"); 21628 21629 /* 21630 * Check if the ire has the RTF_MULTIRT flag, inherited 21631 * from an IRE_OFFSUBNET ire entry in ip_newroute. 21632 */ 21633 if (ire->ire_flags & RTF_MULTIRT) { 21634 /* 21635 * Force the TTL of multirouted packets if required. 21636 * The TTL of such packets is bounded by the 21637 * ip_multirt_ttl ndd variable. 21638 */ 21639 if ((ipst->ips_ip_multirt_ttl > 0) && 21640 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 21641 ip2dbg(("ip_wput: forcing multirt TTL to %d " 21642 "(was %d), dst 0x%08x\n", 21643 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 21644 ntohl(ire->ire_addr))); 21645 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 21646 } 21647 /* 21648 * At this point, we check to see if there are any pending 21649 * unresolved routes. ire_multirt_resolvable() 21650 * checks in O(n) that all IRE_OFFSUBNET ire 21651 * entries for the packet's destination and 21652 * flagged RTF_MULTIRT are currently resolved. 21653 * If some remain unresolved, we make a copy 21654 * of the current message. It will be used 21655 * to initiate additional route resolutions. 21656 */ 21657 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 21658 MBLK_GETLABEL(first_mp), ipst); 21659 ip2dbg(("ip_wput[noirefound]: ire %p, " 21660 "multirt_need_resolve %d, first_mp %p\n", 21661 (void *)ire, multirt_need_resolve, (void *)first_mp)); 21662 if (multirt_need_resolve) { 21663 copy_mp = copymsg(first_mp); 21664 if (copy_mp != NULL) { 21665 MULTIRT_DEBUG_TAG(copy_mp); 21666 } 21667 } 21668 } 21669 21670 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 21671 /* 21672 * Try to resolve another multiroute if 21673 * ire_multirt_resolvable() deemed it necessary. 21674 * At this point, we need to distinguish 21675 * multicasts from other packets. For multicasts, 21676 * we call ip_newroute_ipif() and request that both 21677 * multirouting and setsrc flags are checked. 21678 */ 21679 if (copy_mp != NULL) { 21680 if (CLASSD(dst)) { 21681 ipif_t *ipif = ipif_lookup_group(dst, zoneid, ipst); 21682 if (ipif) { 21683 ASSERT(infop->ip_opt_ill_index == 0); 21684 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 21685 RTF_SETSRC | RTF_MULTIRT, zoneid, infop); 21686 ipif_refrele(ipif); 21687 } else { 21688 MULTIRT_DEBUG_UNTAG(copy_mp); 21689 freemsg(copy_mp); 21690 copy_mp = NULL; 21691 } 21692 } else { 21693 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid, ipst); 21694 } 21695 } 21696 if (attach_ill != NULL) 21697 ill_refrele(attach_ill); 21698 if (xmit_ill != NULL) 21699 ill_refrele(xmit_ill); 21700 if (need_decref) 21701 CONN_DEC_REF(connp); 21702 return; 21703 21704 icmp_parameter_problem: 21705 /* could not have originated externally */ 21706 ASSERT(mp->b_prev == NULL); 21707 if (ip_hdr_complete(ipha, zoneid, ipst) == 0) { 21708 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 21709 /* it's the IP header length that's in trouble */ 21710 icmp_param_problem(q, first_mp, 0, zoneid, ipst); 21711 first_mp = NULL; 21712 } 21713 21714 discard_pkt: 21715 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21716 drop_pkt: 21717 ip1dbg(("ip_wput: dropped packet\n")); 21718 if (ire != NULL) 21719 ire_refrele(ire); 21720 if (need_decref) 21721 CONN_DEC_REF(connp); 21722 freemsg(first_mp); 21723 if (attach_ill != NULL) 21724 ill_refrele(attach_ill); 21725 if (xmit_ill != NULL) 21726 ill_refrele(xmit_ill); 21727 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21728 "ip_wput_end: q %p (%S)", q, "droppkt"); 21729 } 21730 21731 /* 21732 * If this is a conn_t queue, then we pass in the conn. This includes the 21733 * zoneid. 21734 * Otherwise, this is a message coming back from ARP or for an ill_t queue, 21735 * in which case we use the global zoneid since those are all part of 21736 * the global zone. 21737 */ 21738 void 21739 ip_wput(queue_t *q, mblk_t *mp) 21740 { 21741 if (CONN_Q(q)) 21742 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 21743 else 21744 ip_output(GLOBAL_ZONEID, mp, q, IP_WPUT); 21745 } 21746 21747 /* 21748 * 21749 * The following rules must be observed when accessing any ipif or ill 21750 * that has been cached in the conn. Typically conn_nofailover_ill, 21751 * conn_xmit_if_ill, conn_multicast_ipif and conn_multicast_ill. 21752 * 21753 * Access: The ipif or ill pointed to from the conn can be accessed under 21754 * the protection of the conn_lock or after it has been refheld under the 21755 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 21756 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 21757 * The reason for this is that a concurrent unplumb could actually be 21758 * cleaning up these cached pointers by walking the conns and might have 21759 * finished cleaning up the conn in question. The macros check that an 21760 * unplumb has not yet started on the ipif or ill. 21761 * 21762 * Caching: An ipif or ill pointer may be cached in the conn only after 21763 * making sure that an unplumb has not started. So the caching is done 21764 * while holding both the conn_lock and the ill_lock and after using the 21765 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 21766 * flag before starting the cleanup of conns. 21767 * 21768 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 21769 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 21770 * or a reference to the ipif or a reference to an ire that references the 21771 * ipif. An ipif does not change its ill except for failover/failback. Since 21772 * failover/failback happens only after bringing down the ipif and making sure 21773 * the ipif refcnt has gone to zero and holding the ill_g_lock and ill_lock 21774 * the above holds. 21775 */ 21776 ipif_t * 21777 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 21778 { 21779 ipif_t *ipif; 21780 ill_t *ill; 21781 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 21782 21783 *err = 0; 21784 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 21785 mutex_enter(&connp->conn_lock); 21786 ipif = *ipifp; 21787 if (ipif != NULL) { 21788 ill = ipif->ipif_ill; 21789 mutex_enter(&ill->ill_lock); 21790 if (IPIF_CAN_LOOKUP(ipif)) { 21791 ipif_refhold_locked(ipif); 21792 mutex_exit(&ill->ill_lock); 21793 mutex_exit(&connp->conn_lock); 21794 rw_exit(&ipst->ips_ill_g_lock); 21795 return (ipif); 21796 } else { 21797 *err = IPIF_LOOKUP_FAILED; 21798 } 21799 mutex_exit(&ill->ill_lock); 21800 } 21801 mutex_exit(&connp->conn_lock); 21802 rw_exit(&ipst->ips_ill_g_lock); 21803 return (NULL); 21804 } 21805 21806 ill_t * 21807 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 21808 { 21809 ill_t *ill; 21810 21811 *err = 0; 21812 mutex_enter(&connp->conn_lock); 21813 ill = *illp; 21814 if (ill != NULL) { 21815 mutex_enter(&ill->ill_lock); 21816 if (ILL_CAN_LOOKUP(ill)) { 21817 ill_refhold_locked(ill); 21818 mutex_exit(&ill->ill_lock); 21819 mutex_exit(&connp->conn_lock); 21820 return (ill); 21821 } else { 21822 *err = ILL_LOOKUP_FAILED; 21823 } 21824 mutex_exit(&ill->ill_lock); 21825 } 21826 mutex_exit(&connp->conn_lock); 21827 return (NULL); 21828 } 21829 21830 static int 21831 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 21832 { 21833 ill_t *ill; 21834 21835 ill = ipif->ipif_ill; 21836 mutex_enter(&connp->conn_lock); 21837 mutex_enter(&ill->ill_lock); 21838 if (IPIF_CAN_LOOKUP(ipif)) { 21839 *ipifp = ipif; 21840 mutex_exit(&ill->ill_lock); 21841 mutex_exit(&connp->conn_lock); 21842 return (0); 21843 } 21844 mutex_exit(&ill->ill_lock); 21845 mutex_exit(&connp->conn_lock); 21846 return (IPIF_LOOKUP_FAILED); 21847 } 21848 21849 /* 21850 * This is called if the outbound datagram needs fragmentation. 21851 * 21852 * NOTE : This function does not ire_refrele the ire argument passed in. 21853 */ 21854 static void 21855 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire, zoneid_t zoneid, 21856 ip_stack_t *ipst) 21857 { 21858 ipha_t *ipha; 21859 mblk_t *mp; 21860 uint32_t v_hlen_tos_len; 21861 uint32_t max_frag; 21862 uint32_t frag_flag; 21863 boolean_t dont_use; 21864 21865 if (ipsec_mp->b_datap->db_type == M_CTL) { 21866 mp = ipsec_mp->b_cont; 21867 } else { 21868 mp = ipsec_mp; 21869 } 21870 21871 ipha = (ipha_t *)mp->b_rptr; 21872 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21873 21874 #ifdef _BIG_ENDIAN 21875 #define V_HLEN (v_hlen_tos_len >> 24) 21876 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21877 #else 21878 #define V_HLEN (v_hlen_tos_len & 0xFF) 21879 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21880 #endif 21881 21882 #ifndef SPEED_BEFORE_SAFETY 21883 /* 21884 * Check that ipha_length is consistent with 21885 * the mblk length 21886 */ 21887 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 21888 ip0dbg(("Packet length mismatch: %d, %ld\n", 21889 LENGTH, msgdsize(mp))); 21890 freemsg(ipsec_mp); 21891 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21892 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 21893 "packet length mismatch"); 21894 return; 21895 } 21896 #endif 21897 /* 21898 * Don't use frag_flag if pre-built packet or source 21899 * routed or if multicast (since multicast packets do not solicit 21900 * ICMP "packet too big" messages). Get the values of 21901 * max_frag and frag_flag atomically by acquiring the 21902 * ire_lock. 21903 */ 21904 mutex_enter(&ire->ire_lock); 21905 max_frag = ire->ire_max_frag; 21906 frag_flag = ire->ire_frag_flag; 21907 mutex_exit(&ire->ire_lock); 21908 21909 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 21910 (V_HLEN != IP_SIMPLE_HDR_VERSION && 21911 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 21912 21913 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 21914 (dont_use ? 0 : frag_flag), zoneid, ipst); 21915 } 21916 21917 /* 21918 * Used for deciding the MSS size for the upper layer. Thus 21919 * we need to check the outbound policy values in the conn. 21920 */ 21921 int 21922 conn_ipsec_length(conn_t *connp) 21923 { 21924 ipsec_latch_t *ipl; 21925 21926 ipl = connp->conn_latch; 21927 if (ipl == NULL) 21928 return (0); 21929 21930 if (ipl->ipl_out_policy == NULL) 21931 return (0); 21932 21933 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 21934 } 21935 21936 /* 21937 * Returns an estimate of the IPSEC headers size. This is used if 21938 * we don't want to call into IPSEC to get the exact size. 21939 */ 21940 int 21941 ipsec_out_extra_length(mblk_t *ipsec_mp) 21942 { 21943 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21944 ipsec_action_t *a; 21945 21946 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21947 if (!io->ipsec_out_secure) 21948 return (0); 21949 21950 a = io->ipsec_out_act; 21951 21952 if (a == NULL) { 21953 ASSERT(io->ipsec_out_policy != NULL); 21954 a = io->ipsec_out_policy->ipsp_act; 21955 } 21956 ASSERT(a != NULL); 21957 21958 return (a->ipa_ovhd); 21959 } 21960 21961 /* 21962 * Returns an estimate of the IPSEC headers size. This is used if 21963 * we don't want to call into IPSEC to get the exact size. 21964 */ 21965 int 21966 ipsec_in_extra_length(mblk_t *ipsec_mp) 21967 { 21968 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21969 ipsec_action_t *a; 21970 21971 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21972 21973 a = ii->ipsec_in_action; 21974 return (a == NULL ? 0 : a->ipa_ovhd); 21975 } 21976 21977 /* 21978 * If there are any source route options, return the true final 21979 * destination. Otherwise, return the destination. 21980 */ 21981 ipaddr_t 21982 ip_get_dst(ipha_t *ipha) 21983 { 21984 ipoptp_t opts; 21985 uchar_t *opt; 21986 uint8_t optval; 21987 uint8_t optlen; 21988 ipaddr_t dst; 21989 uint32_t off; 21990 21991 dst = ipha->ipha_dst; 21992 21993 if (IS_SIMPLE_IPH(ipha)) 21994 return (dst); 21995 21996 for (optval = ipoptp_first(&opts, ipha); 21997 optval != IPOPT_EOL; 21998 optval = ipoptp_next(&opts)) { 21999 opt = opts.ipoptp_cur; 22000 optlen = opts.ipoptp_len; 22001 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 22002 switch (optval) { 22003 case IPOPT_SSRR: 22004 case IPOPT_LSRR: 22005 off = opt[IPOPT_OFFSET]; 22006 /* 22007 * If one of the conditions is true, it means 22008 * end of options and dst already has the right 22009 * value. 22010 */ 22011 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 22012 off = optlen - IP_ADDR_LEN; 22013 bcopy(&opt[off], &dst, IP_ADDR_LEN); 22014 } 22015 return (dst); 22016 default: 22017 break; 22018 } 22019 } 22020 22021 return (dst); 22022 } 22023 22024 mblk_t * 22025 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 22026 conn_t *connp, boolean_t unspec_src, zoneid_t zoneid) 22027 { 22028 ipsec_out_t *io; 22029 mblk_t *first_mp; 22030 boolean_t policy_present; 22031 ip_stack_t *ipst; 22032 ipsec_stack_t *ipss; 22033 22034 ASSERT(ire != NULL); 22035 ipst = ire->ire_ipst; 22036 ipss = ipst->ips_netstack->netstack_ipsec; 22037 22038 first_mp = mp; 22039 if (mp->b_datap->db_type == M_CTL) { 22040 io = (ipsec_out_t *)first_mp->b_rptr; 22041 /* 22042 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 22043 * 22044 * 1) There is per-socket policy (including cached global 22045 * policy) or a policy on the IP-in-IP tunnel. 22046 * 2) There is no per-socket policy, but it is 22047 * a multicast packet that needs to go out 22048 * on a specific interface. This is the case 22049 * where (ip_wput and ip_wput_multicast) attaches 22050 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 22051 * 22052 * In case (2) we check with global policy to 22053 * see if there is a match and set the ill_index 22054 * appropriately so that we can lookup the ire 22055 * properly in ip_wput_ipsec_out. 22056 */ 22057 22058 /* 22059 * ipsec_out_use_global_policy is set to B_FALSE 22060 * in ipsec_in_to_out(). Refer to that function for 22061 * details. 22062 */ 22063 if ((io->ipsec_out_latch == NULL) && 22064 (io->ipsec_out_use_global_policy)) { 22065 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 22066 ire, connp, unspec_src, zoneid)); 22067 } 22068 if (!io->ipsec_out_secure) { 22069 /* 22070 * If this is not a secure packet, drop 22071 * the IPSEC_OUT mp and treat it as a clear 22072 * packet. This happens when we are sending 22073 * a ICMP reply back to a clear packet. See 22074 * ipsec_in_to_out() for details. 22075 */ 22076 mp = first_mp->b_cont; 22077 freeb(first_mp); 22078 } 22079 return (mp); 22080 } 22081 /* 22082 * See whether we need to attach a global policy here. We 22083 * don't depend on the conn (as it could be null) for deciding 22084 * what policy this datagram should go through because it 22085 * should have happened in ip_wput if there was some 22086 * policy. This normally happens for connections which are not 22087 * fully bound preventing us from caching policies in 22088 * ip_bind. Packets coming from the TCP listener/global queue 22089 * - which are non-hard_bound - could also be affected by 22090 * applying policy here. 22091 * 22092 * If this packet is coming from tcp global queue or listener, 22093 * we will be applying policy here. This may not be *right* 22094 * if these packets are coming from the detached connection as 22095 * it could have gone in clear before. This happens only if a 22096 * TCP connection started when there is no policy and somebody 22097 * added policy before it became detached. Thus packets of the 22098 * detached connection could go out secure and the other end 22099 * would drop it because it will be expecting in clear. The 22100 * converse is not true i.e if somebody starts a TCP 22101 * connection and deletes the policy, all the packets will 22102 * still go out with the policy that existed before deleting 22103 * because ip_unbind sends up policy information which is used 22104 * by TCP on subsequent ip_wputs. The right solution is to fix 22105 * TCP to attach a dummy IPSEC_OUT and set 22106 * ipsec_out_use_global_policy to B_FALSE. As this might 22107 * affect performance for normal cases, we are not doing it. 22108 * Thus, set policy before starting any TCP connections. 22109 * 22110 * NOTE - We might apply policy even for a hard bound connection 22111 * - for which we cached policy in ip_bind - if somebody added 22112 * global policy after we inherited the policy in ip_bind. 22113 * This means that the packets that were going out in clear 22114 * previously would start going secure and hence get dropped 22115 * on the other side. To fix this, TCP attaches a dummy 22116 * ipsec_out and make sure that we don't apply global policy. 22117 */ 22118 if (ipha != NULL) 22119 policy_present = ipss->ipsec_outbound_v4_policy_present; 22120 else 22121 policy_present = ipss->ipsec_outbound_v6_policy_present; 22122 if (!policy_present) 22123 return (mp); 22124 22125 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src, 22126 zoneid)); 22127 } 22128 22129 ire_t * 22130 conn_set_outgoing_ill(conn_t *connp, ire_t *ire, ill_t **conn_outgoing_ill) 22131 { 22132 ipaddr_t addr; 22133 ire_t *save_ire; 22134 irb_t *irb; 22135 ill_group_t *illgrp; 22136 int err; 22137 22138 save_ire = ire; 22139 addr = ire->ire_addr; 22140 22141 ASSERT(ire->ire_type == IRE_BROADCAST); 22142 22143 illgrp = connp->conn_outgoing_ill->ill_group; 22144 if (illgrp == NULL) { 22145 *conn_outgoing_ill = conn_get_held_ill(connp, 22146 &connp->conn_outgoing_ill, &err); 22147 if (err == ILL_LOOKUP_FAILED) { 22148 ire_refrele(save_ire); 22149 return (NULL); 22150 } 22151 return (save_ire); 22152 } 22153 /* 22154 * If IP_BOUND_IF has been done, conn_outgoing_ill will be set. 22155 * If it is part of the group, we need to send on the ire 22156 * that has been cleared of IRE_MARK_NORECV and that belongs 22157 * to this group. This is okay as IP_BOUND_IF really means 22158 * any ill in the group. We depend on the fact that the 22159 * first ire in the group is always cleared of IRE_MARK_NORECV 22160 * if such an ire exists. This is possible only if you have 22161 * at least one ill in the group that has not failed. 22162 * 22163 * First get to the ire that matches the address and group. 22164 * 22165 * We don't look for an ire with a matching zoneid because a given zone 22166 * won't always have broadcast ires on all ills in the group. 22167 */ 22168 irb = ire->ire_bucket; 22169 rw_enter(&irb->irb_lock, RW_READER); 22170 if (ire->ire_marks & IRE_MARK_NORECV) { 22171 /* 22172 * If the current zone only has an ire broadcast for this 22173 * address marked NORECV, the ire we want is ahead in the 22174 * bucket, so we look it up deliberately ignoring the zoneid. 22175 */ 22176 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 22177 if (ire->ire_addr != addr) 22178 continue; 22179 /* skip over deleted ires */ 22180 if (ire->ire_marks & IRE_MARK_CONDEMNED) 22181 continue; 22182 } 22183 } 22184 while (ire != NULL) { 22185 /* 22186 * If a new interface is coming up, we could end up 22187 * seeing the loopback ire and the non-loopback ire 22188 * may not have been added yet. So check for ire_stq 22189 */ 22190 if (ire->ire_stq != NULL && (ire->ire_addr != addr || 22191 ire->ire_ipif->ipif_ill->ill_group == illgrp)) { 22192 break; 22193 } 22194 ire = ire->ire_next; 22195 } 22196 if (ire != NULL && ire->ire_addr == addr && 22197 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 22198 IRE_REFHOLD(ire); 22199 rw_exit(&irb->irb_lock); 22200 ire_refrele(save_ire); 22201 *conn_outgoing_ill = ire_to_ill(ire); 22202 /* 22203 * Refhold the ill to make the conn_outgoing_ill 22204 * independent of the ire. ip_wput_ire goes in a loop 22205 * and may refrele the ire. Since we have an ire at this 22206 * point we don't need to use ILL_CAN_LOOKUP on the ill. 22207 */ 22208 ill_refhold(*conn_outgoing_ill); 22209 return (ire); 22210 } 22211 rw_exit(&irb->irb_lock); 22212 ip1dbg(("conn_set_outgoing_ill: No matching ire\n")); 22213 /* 22214 * If we can't find a suitable ire, return the original ire. 22215 */ 22216 return (save_ire); 22217 } 22218 22219 /* 22220 * This function does the ire_refrele of the ire passed in as the 22221 * argument. As this function looks up more ires i.e broadcast ires, 22222 * it needs to REFRELE them. Currently, for simplicity we don't 22223 * differentiate the one passed in and looked up here. We always 22224 * REFRELE. 22225 * IPQoS Notes: 22226 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 22227 * IPSec packets are done in ipsec_out_process. 22228 * 22229 */ 22230 void 22231 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller, 22232 zoneid_t zoneid) 22233 { 22234 ipha_t *ipha; 22235 #define rptr ((uchar_t *)ipha) 22236 queue_t *stq; 22237 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 22238 uint32_t v_hlen_tos_len; 22239 uint32_t ttl_protocol; 22240 ipaddr_t src; 22241 ipaddr_t dst; 22242 uint32_t cksum; 22243 ipaddr_t orig_src; 22244 ire_t *ire1; 22245 mblk_t *next_mp; 22246 uint_t hlen; 22247 uint16_t *up; 22248 uint32_t max_frag = ire->ire_max_frag; 22249 ill_t *ill = ire_to_ill(ire); 22250 int clusterwide; 22251 uint16_t ip_hdr_included; /* IP header included by ULP? */ 22252 int ipsec_len; 22253 mblk_t *first_mp; 22254 ipsec_out_t *io; 22255 boolean_t conn_dontroute; /* conn value for multicast */ 22256 boolean_t conn_multicast_loop; /* conn value for multicast */ 22257 boolean_t multicast_forward; /* Should we forward ? */ 22258 boolean_t unspec_src; 22259 ill_t *conn_outgoing_ill = NULL; 22260 ill_t *ire_ill; 22261 ill_t *ire1_ill; 22262 ill_t *out_ill; 22263 uint32_t ill_index = 0; 22264 boolean_t multirt_send = B_FALSE; 22265 int err; 22266 ipxmit_state_t pktxmit_state; 22267 ip_stack_t *ipst = ire->ire_ipst; 22268 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 22269 22270 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 22271 "ip_wput_ire_start: q %p", q); 22272 22273 multicast_forward = B_FALSE; 22274 unspec_src = (connp != NULL && connp->conn_unspec_src); 22275 22276 if (ire->ire_flags & RTF_MULTIRT) { 22277 /* 22278 * Multirouting case. The bucket where ire is stored 22279 * probably holds other RTF_MULTIRT flagged ire 22280 * to the destination. In this call to ip_wput_ire, 22281 * we attempt to send the packet through all 22282 * those ires. Thus, we first ensure that ire is the 22283 * first RTF_MULTIRT ire in the bucket, 22284 * before walking the ire list. 22285 */ 22286 ire_t *first_ire; 22287 irb_t *irb = ire->ire_bucket; 22288 ASSERT(irb != NULL); 22289 22290 /* Make sure we do not omit any multiroute ire. */ 22291 IRB_REFHOLD(irb); 22292 for (first_ire = irb->irb_ire; 22293 first_ire != NULL; 22294 first_ire = first_ire->ire_next) { 22295 if ((first_ire->ire_flags & RTF_MULTIRT) && 22296 (first_ire->ire_addr == ire->ire_addr) && 22297 !(first_ire->ire_marks & 22298 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 22299 break; 22300 } 22301 22302 if ((first_ire != NULL) && (first_ire != ire)) { 22303 IRE_REFHOLD(first_ire); 22304 ire_refrele(ire); 22305 ire = first_ire; 22306 ill = ire_to_ill(ire); 22307 } 22308 IRB_REFRELE(irb); 22309 } 22310 22311 /* 22312 * conn_outgoing_ill is used only in the broadcast loop. 22313 * for performance we don't grab the mutexs in the fastpath 22314 */ 22315 if ((connp != NULL) && 22316 (connp->conn_xmit_if_ill == NULL) && 22317 (ire->ire_type == IRE_BROADCAST) && 22318 ((connp->conn_nofailover_ill != NULL) || 22319 (connp->conn_outgoing_ill != NULL))) { 22320 /* 22321 * Bind to IPIF_NOFAILOVER address overrides IP_BOUND_IF 22322 * option. So, see if this endpoint is bound to a 22323 * IPIF_NOFAILOVER address. If so, honor it. This implies 22324 * that if the interface is failed, we will still send 22325 * the packet on the same ill which is what we want. 22326 */ 22327 conn_outgoing_ill = conn_get_held_ill(connp, 22328 &connp->conn_nofailover_ill, &err); 22329 if (err == ILL_LOOKUP_FAILED) { 22330 ire_refrele(ire); 22331 freemsg(mp); 22332 return; 22333 } 22334 if (conn_outgoing_ill == NULL) { 22335 /* 22336 * Choose a good ill in the group to send the 22337 * packets on. 22338 */ 22339 ire = conn_set_outgoing_ill(connp, ire, 22340 &conn_outgoing_ill); 22341 if (ire == NULL) { 22342 freemsg(mp); 22343 return; 22344 } 22345 } 22346 } 22347 22348 if (mp->b_datap->db_type != M_CTL) { 22349 ipha = (ipha_t *)mp->b_rptr; 22350 } else { 22351 io = (ipsec_out_t *)mp->b_rptr; 22352 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22353 ASSERT(zoneid == io->ipsec_out_zoneid); 22354 ASSERT(zoneid != ALL_ZONES); 22355 ipha = (ipha_t *)mp->b_cont->b_rptr; 22356 dst = ipha->ipha_dst; 22357 /* 22358 * For the multicast case, ipsec_out carries conn_dontroute and 22359 * conn_multicast_loop as conn may not be available here. We 22360 * need this for multicast loopback and forwarding which is done 22361 * later in the code. 22362 */ 22363 if (CLASSD(dst)) { 22364 conn_dontroute = io->ipsec_out_dontroute; 22365 conn_multicast_loop = io->ipsec_out_multicast_loop; 22366 /* 22367 * If conn_dontroute is not set or conn_multicast_loop 22368 * is set, we need to do forwarding/loopback. For 22369 * datagrams from ip_wput_multicast, conn_dontroute is 22370 * set to B_TRUE and conn_multicast_loop is set to 22371 * B_FALSE so that we neither do forwarding nor 22372 * loopback. 22373 */ 22374 if (!conn_dontroute || conn_multicast_loop) 22375 multicast_forward = B_TRUE; 22376 } 22377 } 22378 22379 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 22380 ire->ire_zoneid != ALL_ZONES) { 22381 /* 22382 * When a zone sends a packet to another zone, we try to deliver 22383 * the packet under the same conditions as if the destination 22384 * was a real node on the network. To do so, we look for a 22385 * matching route in the forwarding table. 22386 * RTF_REJECT and RTF_BLACKHOLE are handled just like 22387 * ip_newroute() does. 22388 * Note that IRE_LOCAL are special, since they are used 22389 * when the zoneid doesn't match in some cases. This means that 22390 * we need to handle ipha_src differently since ire_src_addr 22391 * belongs to the receiving zone instead of the sending zone. 22392 * When ip_restrict_interzone_loopback is set, then 22393 * ire_cache_lookup() ensures that IRE_LOCAL are only used 22394 * for loopback between zones when the logical "Ethernet" would 22395 * have looped them back. 22396 */ 22397 ire_t *src_ire; 22398 22399 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 22400 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 22401 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE), ipst); 22402 if (src_ire != NULL && 22403 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) && 22404 (!ipst->ips_ip_restrict_interzone_loopback || 22405 ire_local_same_ill_group(ire, src_ire))) { 22406 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 22407 ipha->ipha_src = src_ire->ire_src_addr; 22408 ire_refrele(src_ire); 22409 } else { 22410 ire_refrele(ire); 22411 if (conn_outgoing_ill != NULL) 22412 ill_refrele(conn_outgoing_ill); 22413 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 22414 if (src_ire != NULL) { 22415 if (src_ire->ire_flags & RTF_BLACKHOLE) { 22416 ire_refrele(src_ire); 22417 freemsg(mp); 22418 return; 22419 } 22420 ire_refrele(src_ire); 22421 } 22422 if (ip_hdr_complete(ipha, zoneid, ipst)) { 22423 /* Failed */ 22424 freemsg(mp); 22425 return; 22426 } 22427 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, zoneid, 22428 ipst); 22429 return; 22430 } 22431 } 22432 22433 if (mp->b_datap->db_type == M_CTL || 22434 ipss->ipsec_outbound_v4_policy_present) { 22435 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 22436 unspec_src, zoneid); 22437 if (mp == NULL) { 22438 ire_refrele(ire); 22439 if (conn_outgoing_ill != NULL) 22440 ill_refrele(conn_outgoing_ill); 22441 return; 22442 } 22443 } 22444 22445 first_mp = mp; 22446 ipsec_len = 0; 22447 22448 if (first_mp->b_datap->db_type == M_CTL) { 22449 io = (ipsec_out_t *)first_mp->b_rptr; 22450 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22451 mp = first_mp->b_cont; 22452 ipsec_len = ipsec_out_extra_length(first_mp); 22453 ASSERT(ipsec_len >= 0); 22454 /* We already picked up the zoneid from the M_CTL above */ 22455 ASSERT(zoneid == io->ipsec_out_zoneid); 22456 ASSERT(zoneid != ALL_ZONES); 22457 22458 /* 22459 * Drop M_CTL here if IPsec processing is not needed. 22460 * (Non-IPsec use of M_CTL extracted any information it 22461 * needed above). 22462 */ 22463 if (ipsec_len == 0) { 22464 freeb(first_mp); 22465 first_mp = mp; 22466 } 22467 } 22468 22469 /* 22470 * Fast path for ip_wput_ire 22471 */ 22472 22473 ipha = (ipha_t *)mp->b_rptr; 22474 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 22475 dst = ipha->ipha_dst; 22476 22477 /* 22478 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 22479 * if the socket is a SOCK_RAW type. The transport checksum should 22480 * be provided in the pre-built packet, so we don't need to compute it. 22481 * Also, other application set flags, like DF, should not be altered. 22482 * Other transport MUST pass down zero. 22483 */ 22484 ip_hdr_included = ipha->ipha_ident; 22485 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 22486 22487 if (CLASSD(dst)) { 22488 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 22489 ntohl(dst), 22490 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 22491 ntohl(ire->ire_addr))); 22492 } 22493 22494 /* Macros to extract header fields from data already in registers */ 22495 #ifdef _BIG_ENDIAN 22496 #define V_HLEN (v_hlen_tos_len >> 24) 22497 #define LENGTH (v_hlen_tos_len & 0xFFFF) 22498 #define PROTO (ttl_protocol & 0xFF) 22499 #else 22500 #define V_HLEN (v_hlen_tos_len & 0xFF) 22501 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 22502 #define PROTO (ttl_protocol >> 8) 22503 #endif 22504 22505 22506 orig_src = src = ipha->ipha_src; 22507 /* (The loop back to "another" is explained down below.) */ 22508 another:; 22509 /* 22510 * Assign an ident value for this packet. We assign idents on 22511 * a per destination basis out of the IRE. There could be 22512 * other threads targeting the same destination, so we have to 22513 * arrange for a atomic increment. Note that we use a 32-bit 22514 * atomic add because it has better performance than its 22515 * 16-bit sibling. 22516 * 22517 * If running in cluster mode and if the source address 22518 * belongs to a replicated service then vector through 22519 * cl_inet_ipident vector to allocate ip identifier 22520 * NOTE: This is a contract private interface with the 22521 * clustering group. 22522 */ 22523 clusterwide = 0; 22524 if (cl_inet_ipident) { 22525 ASSERT(cl_inet_isclusterwide); 22526 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 22527 AF_INET, (uint8_t *)(uintptr_t)src)) { 22528 ipha->ipha_ident = (*cl_inet_ipident)(IPPROTO_IP, 22529 AF_INET, (uint8_t *)(uintptr_t)src, 22530 (uint8_t *)(uintptr_t)dst); 22531 clusterwide = 1; 22532 } 22533 } 22534 if (!clusterwide) { 22535 ipha->ipha_ident = 22536 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 22537 } 22538 22539 #ifndef _BIG_ENDIAN 22540 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 22541 #endif 22542 22543 /* 22544 * Set source address unless sent on an ill or conn_unspec_src is set. 22545 * This is needed to obey conn_unspec_src when packets go through 22546 * ip_newroute + arp. 22547 * Assumes ip_newroute{,_multi} sets the source address as well. 22548 */ 22549 if (src == INADDR_ANY && !unspec_src) { 22550 /* 22551 * Assign the appropriate source address from the IRE if none 22552 * was specified. 22553 */ 22554 ASSERT(ire->ire_ipversion == IPV4_VERSION); 22555 22556 /* 22557 * With IP multipathing, broadcast packets are sent on the ire 22558 * that has been cleared of IRE_MARK_NORECV and that belongs to 22559 * the group. However, this ire might not be in the same zone so 22560 * we can't always use its source address. We look for a 22561 * broadcast ire in the same group and in the right zone. 22562 */ 22563 if (ire->ire_type == IRE_BROADCAST && 22564 ire->ire_zoneid != zoneid) { 22565 ire_t *src_ire = ire_ctable_lookup(dst, 0, 22566 IRE_BROADCAST, ire->ire_ipif, zoneid, NULL, 22567 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 22568 if (src_ire != NULL) { 22569 src = src_ire->ire_src_addr; 22570 ire_refrele(src_ire); 22571 } else { 22572 ire_refrele(ire); 22573 if (conn_outgoing_ill != NULL) 22574 ill_refrele(conn_outgoing_ill); 22575 freemsg(first_mp); 22576 if (ill != NULL) { 22577 BUMP_MIB(ill->ill_ip_mib, 22578 ipIfStatsOutDiscards); 22579 } else { 22580 BUMP_MIB(&ipst->ips_ip_mib, 22581 ipIfStatsOutDiscards); 22582 } 22583 return; 22584 } 22585 } else { 22586 src = ire->ire_src_addr; 22587 } 22588 22589 if (connp == NULL) { 22590 ip1dbg(("ip_wput_ire: no connp and no src " 22591 "address for dst 0x%x, using src 0x%x\n", 22592 ntohl(dst), 22593 ntohl(src))); 22594 } 22595 ipha->ipha_src = src; 22596 } 22597 stq = ire->ire_stq; 22598 22599 /* 22600 * We only allow ire chains for broadcasts since there will 22601 * be multiple IRE_CACHE entries for the same multicast 22602 * address (one per ipif). 22603 */ 22604 next_mp = NULL; 22605 22606 /* broadcast packet */ 22607 if (ire->ire_type == IRE_BROADCAST) 22608 goto broadcast; 22609 22610 /* loopback ? */ 22611 if (stq == NULL) 22612 goto nullstq; 22613 22614 /* The ill_index for outbound ILL */ 22615 ill_index = Q_TO_INDEX(stq); 22616 22617 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 22618 ttl_protocol = ((uint16_t *)ipha)[4]; 22619 22620 /* pseudo checksum (do it in parts for IP header checksum) */ 22621 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 22622 22623 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 22624 queue_t *dev_q = stq->q_next; 22625 22626 /* flow controlled */ 22627 if ((dev_q->q_next || dev_q->q_first) && 22628 !canput(dev_q)) 22629 goto blocked; 22630 if ((PROTO == IPPROTO_UDP) && 22631 (ip_hdr_included != IP_HDR_INCLUDED)) { 22632 hlen = (V_HLEN & 0xF) << 2; 22633 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22634 if (*up != 0) { 22635 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 22636 hlen, LENGTH, max_frag, ipsec_len, cksum); 22637 /* Software checksum? */ 22638 if (DB_CKSUMFLAGS(mp) == 0) { 22639 IP_STAT(ipst, ip_out_sw_cksum); 22640 IP_STAT_UPDATE(ipst, 22641 ip_udp_out_sw_cksum_bytes, 22642 LENGTH - hlen); 22643 } 22644 } 22645 } 22646 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 22647 hlen = (V_HLEN & 0xF) << 2; 22648 if (PROTO == IPPROTO_TCP) { 22649 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22650 /* 22651 * The packet header is processed once and for all, even 22652 * in the multirouting case. We disable hardware 22653 * checksum if the packet is multirouted, as it will be 22654 * replicated via several interfaces, and not all of 22655 * them may have this capability. 22656 */ 22657 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 22658 LENGTH, max_frag, ipsec_len, cksum); 22659 /* Software checksum? */ 22660 if (DB_CKSUMFLAGS(mp) == 0) { 22661 IP_STAT(ipst, ip_out_sw_cksum); 22662 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22663 LENGTH - hlen); 22664 } 22665 } else { 22666 sctp_hdr_t *sctph; 22667 22668 ASSERT(PROTO == IPPROTO_SCTP); 22669 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22670 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22671 /* 22672 * Zero out the checksum field to ensure proper 22673 * checksum calculation. 22674 */ 22675 sctph->sh_chksum = 0; 22676 #ifdef DEBUG 22677 if (!skip_sctp_cksum) 22678 #endif 22679 sctph->sh_chksum = sctp_cksum(mp, hlen); 22680 } 22681 } 22682 22683 /* 22684 * If this is a multicast packet and originated from ip_wput 22685 * we need to do loopback and forwarding checks. If it comes 22686 * from ip_wput_multicast, we SHOULD not do this. 22687 */ 22688 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 22689 22690 /* checksum */ 22691 cksum += ttl_protocol; 22692 22693 /* fragment the packet */ 22694 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 22695 goto fragmentit; 22696 /* 22697 * Don't use frag_flag if packet is pre-built or source 22698 * routed or if multicast (since multicast packets do 22699 * not solicit ICMP "packet too big" messages). 22700 */ 22701 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22702 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22703 !ip_source_route_included(ipha)) && 22704 !CLASSD(ipha->ipha_dst)) 22705 ipha->ipha_fragment_offset_and_flags |= 22706 htons(ire->ire_frag_flag); 22707 22708 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22709 /* calculate IP header checksum */ 22710 cksum += ipha->ipha_ident; 22711 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 22712 cksum += ipha->ipha_fragment_offset_and_flags; 22713 22714 /* IP options present */ 22715 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22716 if (hlen) 22717 goto checksumoptions; 22718 22719 /* calculate hdr checksum */ 22720 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22721 cksum = ~(cksum + (cksum >> 16)); 22722 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22723 } 22724 if (ipsec_len != 0) { 22725 /* 22726 * We will do the rest of the processing after 22727 * we come back from IPSEC in ip_wput_ipsec_out(). 22728 */ 22729 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 22730 22731 io = (ipsec_out_t *)first_mp->b_rptr; 22732 io->ipsec_out_ill_index = ((ill_t *)stq->q_ptr)-> 22733 ill_phyint->phyint_ifindex; 22734 22735 ipsec_out_process(q, first_mp, ire, ill_index); 22736 ire_refrele(ire); 22737 if (conn_outgoing_ill != NULL) 22738 ill_refrele(conn_outgoing_ill); 22739 return; 22740 } 22741 22742 /* 22743 * In most cases, the emission loop below is entered only 22744 * once. Only in the case where the ire holds the 22745 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 22746 * flagged ires in the bucket, and send the packet 22747 * through all crossed RTF_MULTIRT routes. 22748 */ 22749 if (ire->ire_flags & RTF_MULTIRT) { 22750 multirt_send = B_TRUE; 22751 } 22752 do { 22753 if (multirt_send) { 22754 irb_t *irb; 22755 /* 22756 * We are in a multiple send case, need to get 22757 * the next ire and make a duplicate of the packet. 22758 * ire1 holds here the next ire to process in the 22759 * bucket. If multirouting is expected, 22760 * any non-RTF_MULTIRT ire that has the 22761 * right destination address is ignored. 22762 */ 22763 irb = ire->ire_bucket; 22764 ASSERT(irb != NULL); 22765 22766 IRB_REFHOLD(irb); 22767 for (ire1 = ire->ire_next; 22768 ire1 != NULL; 22769 ire1 = ire1->ire_next) { 22770 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 22771 continue; 22772 if (ire1->ire_addr != ire->ire_addr) 22773 continue; 22774 if (ire1->ire_marks & 22775 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 22776 continue; 22777 22778 /* Got one */ 22779 IRE_REFHOLD(ire1); 22780 break; 22781 } 22782 IRB_REFRELE(irb); 22783 22784 if (ire1 != NULL) { 22785 next_mp = copyb(mp); 22786 if ((next_mp == NULL) || 22787 ((mp->b_cont != NULL) && 22788 ((next_mp->b_cont = 22789 dupmsg(mp->b_cont)) == NULL))) { 22790 freemsg(next_mp); 22791 next_mp = NULL; 22792 ire_refrele(ire1); 22793 ire1 = NULL; 22794 } 22795 } 22796 22797 /* Last multiroute ire; don't loop anymore. */ 22798 if (ire1 == NULL) { 22799 multirt_send = B_FALSE; 22800 } 22801 } 22802 22803 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 22804 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 22805 mblk_t *, mp); 22806 FW_HOOKS(ipst->ips_ip4_physical_out_event, 22807 ipst->ips_ipv4firewall_physical_out, 22808 NULL, ire->ire_ipif->ipif_ill, ipha, mp, mp, ipst); 22809 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 22810 if (mp == NULL) 22811 goto release_ire_and_ill; 22812 22813 mp->b_prev = SET_BPREV_FLAG(IPP_LOCAL_OUT); 22814 DTRACE_PROBE2(ip__xmit__1, mblk_t *, mp, ire_t *, ire); 22815 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_TRUE); 22816 if ((pktxmit_state == SEND_FAILED) || 22817 (pktxmit_state == LLHDR_RESLV_FAILED)) { 22818 ip2dbg(("ip_wput_ire: ip_xmit_v4 failed" 22819 "- packet dropped\n")); 22820 release_ire_and_ill: 22821 ire_refrele(ire); 22822 if (next_mp != NULL) { 22823 freemsg(next_mp); 22824 ire_refrele(ire1); 22825 } 22826 if (conn_outgoing_ill != NULL) 22827 ill_refrele(conn_outgoing_ill); 22828 return; 22829 } 22830 22831 if (CLASSD(dst)) { 22832 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastPkts); 22833 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastOctets, 22834 LENGTH); 22835 } 22836 22837 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22838 "ip_wput_ire_end: q %p (%S)", 22839 q, "last copy out"); 22840 IRE_REFRELE(ire); 22841 22842 if (multirt_send) { 22843 ASSERT(ire1); 22844 /* 22845 * Proceed with the next RTF_MULTIRT ire, 22846 * Also set up the send-to queue accordingly. 22847 */ 22848 ire = ire1; 22849 ire1 = NULL; 22850 stq = ire->ire_stq; 22851 mp = next_mp; 22852 next_mp = NULL; 22853 ipha = (ipha_t *)mp->b_rptr; 22854 ill_index = Q_TO_INDEX(stq); 22855 ill = (ill_t *)stq->q_ptr; 22856 } 22857 } while (multirt_send); 22858 if (conn_outgoing_ill != NULL) 22859 ill_refrele(conn_outgoing_ill); 22860 return; 22861 22862 /* 22863 * ire->ire_type == IRE_BROADCAST (minimize diffs) 22864 */ 22865 broadcast: 22866 { 22867 /* 22868 * Avoid broadcast storms by setting the ttl to 1 22869 * for broadcasts. This parameter can be set 22870 * via ndd, so make sure that for the SO_DONTROUTE 22871 * case that ipha_ttl is always set to 1. 22872 * In the event that we are replying to incoming 22873 * ICMP packets, conn could be NULL. 22874 */ 22875 if ((connp != NULL) && connp->conn_dontroute) 22876 ipha->ipha_ttl = 1; 22877 else 22878 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl; 22879 22880 /* 22881 * Note that we are not doing a IRB_REFHOLD here. 22882 * Actually we don't care if the list changes i.e 22883 * if somebody deletes an IRE from the list while 22884 * we drop the lock, the next time we come around 22885 * ire_next will be NULL and hence we won't send 22886 * out multiple copies which is fine. 22887 */ 22888 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 22889 ire1 = ire->ire_next; 22890 if (conn_outgoing_ill != NULL) { 22891 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 22892 ASSERT(ire1 == ire->ire_next); 22893 if (ire1 != NULL && ire1->ire_addr == dst) { 22894 ire_refrele(ire); 22895 ire = ire1; 22896 IRE_REFHOLD(ire); 22897 ire1 = ire->ire_next; 22898 continue; 22899 } 22900 rw_exit(&ire->ire_bucket->irb_lock); 22901 /* Did not find a matching ill */ 22902 ip1dbg(("ip_wput_ire: broadcast with no " 22903 "matching IP_BOUND_IF ill %s\n", 22904 conn_outgoing_ill->ill_name)); 22905 freemsg(first_mp); 22906 if (ire != NULL) 22907 ire_refrele(ire); 22908 ill_refrele(conn_outgoing_ill); 22909 return; 22910 } 22911 } else if (ire1 != NULL && ire1->ire_addr == dst) { 22912 /* 22913 * If the next IRE has the same address and is not one 22914 * of the two copies that we need to send, try to see 22915 * whether this copy should be sent at all. This 22916 * assumes that we insert loopbacks first and then 22917 * non-loopbacks. This is acheived by inserting the 22918 * loopback always before non-loopback. 22919 * This is used to send a single copy of a broadcast 22920 * packet out all physical interfaces that have an 22921 * matching IRE_BROADCAST while also looping 22922 * back one copy (to ip_wput_local) for each 22923 * matching physical interface. However, we avoid 22924 * sending packets out different logical that match by 22925 * having ipif_up/ipif_down supress duplicate 22926 * IRE_BROADCASTS. 22927 * 22928 * This feature is currently used to get broadcasts 22929 * sent to multiple interfaces, when the broadcast 22930 * address being used applies to multiple interfaces. 22931 * For example, a whole net broadcast will be 22932 * replicated on every connected subnet of 22933 * the target net. 22934 * 22935 * Each zone has its own set of IRE_BROADCASTs, so that 22936 * we're able to distribute inbound packets to multiple 22937 * zones who share a broadcast address. We avoid looping 22938 * back outbound packets in different zones but on the 22939 * same ill, as the application would see duplicates. 22940 * 22941 * If the interfaces are part of the same group, 22942 * we would want to send only one copy out for 22943 * whole group. 22944 * 22945 * This logic assumes that ire_add_v4() groups the 22946 * IRE_BROADCAST entries so that those with the same 22947 * ire_addr and ill_group are kept together. 22948 */ 22949 ire_ill = ire->ire_ipif->ipif_ill; 22950 if (ire->ire_stq == NULL && ire1->ire_stq != NULL) { 22951 if (ire_ill->ill_group != NULL && 22952 (ire->ire_marks & IRE_MARK_NORECV)) { 22953 /* 22954 * If the current zone only has an ire 22955 * broadcast for this address marked 22956 * NORECV, the ire we want is ahead in 22957 * the bucket, so we look it up 22958 * deliberately ignoring the zoneid. 22959 */ 22960 for (ire1 = ire->ire_bucket->irb_ire; 22961 ire1 != NULL; 22962 ire1 = ire1->ire_next) { 22963 ire1_ill = 22964 ire1->ire_ipif->ipif_ill; 22965 if (ire1->ire_addr != dst) 22966 continue; 22967 /* skip over the current ire */ 22968 if (ire1 == ire) 22969 continue; 22970 /* skip over deleted ires */ 22971 if (ire1->ire_marks & 22972 IRE_MARK_CONDEMNED) 22973 continue; 22974 /* 22975 * non-loopback ire in our 22976 * group: use it for the next 22977 * pass in the loop 22978 */ 22979 if (ire1->ire_stq != NULL && 22980 ire1_ill->ill_group == 22981 ire_ill->ill_group) 22982 break; 22983 } 22984 } 22985 } else { 22986 while (ire1 != NULL && ire1->ire_addr == dst) { 22987 ire1_ill = ire1->ire_ipif->ipif_ill; 22988 /* 22989 * We can have two broadcast ires on the 22990 * same ill in different zones; here 22991 * we'll send a copy of the packet on 22992 * each ill and the fanout code will 22993 * call conn_wantpacket() to check that 22994 * the zone has the broadcast address 22995 * configured on the ill. If the two 22996 * ires are in the same group we only 22997 * send one copy up. 22998 */ 22999 if (ire1_ill != ire_ill && 23000 (ire1_ill->ill_group == NULL || 23001 ire_ill->ill_group == NULL || 23002 ire1_ill->ill_group != 23003 ire_ill->ill_group)) { 23004 break; 23005 } 23006 ire1 = ire1->ire_next; 23007 } 23008 } 23009 } 23010 ASSERT(multirt_send == B_FALSE); 23011 if (ire1 != NULL && ire1->ire_addr == dst) { 23012 if ((ire->ire_flags & RTF_MULTIRT) && 23013 (ire1->ire_flags & RTF_MULTIRT)) { 23014 /* 23015 * We are in the multirouting case. 23016 * The message must be sent at least 23017 * on both ires. These ires have been 23018 * inserted AFTER the standard ones 23019 * in ip_rt_add(). There are thus no 23020 * other ire entries for the destination 23021 * address in the rest of the bucket 23022 * that do not have the RTF_MULTIRT 23023 * flag. We don't process a copy 23024 * of the message here. This will be 23025 * done in the final sending loop. 23026 */ 23027 multirt_send = B_TRUE; 23028 } else { 23029 next_mp = ip_copymsg(first_mp); 23030 if (next_mp != NULL) 23031 IRE_REFHOLD(ire1); 23032 } 23033 } 23034 rw_exit(&ire->ire_bucket->irb_lock); 23035 } 23036 23037 if (stq) { 23038 /* 23039 * A non-NULL send-to queue means this packet is going 23040 * out of this machine. 23041 */ 23042 out_ill = (ill_t *)stq->q_ptr; 23043 23044 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutRequests); 23045 ttl_protocol = ((uint16_t *)ipha)[4]; 23046 /* 23047 * We accumulate the pseudo header checksum in cksum. 23048 * This is pretty hairy code, so watch close. One 23049 * thing to keep in mind is that UDP and TCP have 23050 * stored their respective datagram lengths in their 23051 * checksum fields. This lines things up real nice. 23052 */ 23053 cksum = (dst >> 16) + (dst & 0xFFFF) + 23054 (src >> 16) + (src & 0xFFFF); 23055 /* 23056 * We assume the udp checksum field contains the 23057 * length, so to compute the pseudo header checksum, 23058 * all we need is the protocol number and src/dst. 23059 */ 23060 /* Provide the checksums for UDP and TCP. */ 23061 if ((PROTO == IPPROTO_TCP) && 23062 (ip_hdr_included != IP_HDR_INCLUDED)) { 23063 /* hlen gets the number of uchar_ts in the IP header */ 23064 hlen = (V_HLEN & 0xF) << 2; 23065 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 23066 IP_STAT(ipst, ip_out_sw_cksum); 23067 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 23068 LENGTH - hlen); 23069 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 23070 if (*up == 0) 23071 *up = 0xFFFF; 23072 } else if (PROTO == IPPROTO_SCTP && 23073 (ip_hdr_included != IP_HDR_INCLUDED)) { 23074 sctp_hdr_t *sctph; 23075 23076 hlen = (V_HLEN & 0xF) << 2; 23077 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 23078 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 23079 sctph->sh_chksum = 0; 23080 #ifdef DEBUG 23081 if (!skip_sctp_cksum) 23082 #endif 23083 sctph->sh_chksum = sctp_cksum(mp, hlen); 23084 } else { 23085 queue_t *dev_q = stq->q_next; 23086 23087 if ((dev_q->q_next || dev_q->q_first) && 23088 !canput(dev_q)) { 23089 blocked: 23090 ipha->ipha_ident = ip_hdr_included; 23091 /* 23092 * If we don't have a conn to apply 23093 * backpressure, free the message. 23094 * In the ire_send path, we don't know 23095 * the position to requeue the packet. Rather 23096 * than reorder packets, we just drop this 23097 * packet. 23098 */ 23099 if (ipst->ips_ip_output_queue && 23100 connp != NULL && 23101 caller != IRE_SEND) { 23102 if (caller == IP_WSRV) { 23103 connp->conn_did_putbq = 1; 23104 (void) putbq(connp->conn_wq, 23105 first_mp); 23106 conn_drain_insert(connp); 23107 /* 23108 * This is the service thread, 23109 * and the queue is already 23110 * noenabled. The check for 23111 * canput and the putbq is not 23112 * atomic. So we need to check 23113 * again. 23114 */ 23115 if (canput(stq->q_next)) 23116 connp->conn_did_putbq 23117 = 0; 23118 IP_STAT(ipst, ip_conn_flputbq); 23119 } else { 23120 /* 23121 * We are not the service proc. 23122 * ip_wsrv will be scheduled or 23123 * is already running. 23124 */ 23125 (void) putq(connp->conn_wq, 23126 first_mp); 23127 } 23128 } else { 23129 out_ill = (ill_t *)stq->q_ptr; 23130 BUMP_MIB(out_ill->ill_ip_mib, 23131 ipIfStatsOutDiscards); 23132 freemsg(first_mp); 23133 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23134 "ip_wput_ire_end: q %p (%S)", 23135 q, "discard"); 23136 } 23137 ire_refrele(ire); 23138 if (next_mp) { 23139 ire_refrele(ire1); 23140 freemsg(next_mp); 23141 } 23142 if (conn_outgoing_ill != NULL) 23143 ill_refrele(conn_outgoing_ill); 23144 return; 23145 } 23146 if ((PROTO == IPPROTO_UDP) && 23147 (ip_hdr_included != IP_HDR_INCLUDED)) { 23148 /* 23149 * hlen gets the number of uchar_ts in the 23150 * IP header 23151 */ 23152 hlen = (V_HLEN & 0xF) << 2; 23153 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 23154 max_frag = ire->ire_max_frag; 23155 if (*up != 0) { 23156 IP_CKSUM_XMIT(ire_ill, ire, mp, ipha, 23157 up, PROTO, hlen, LENGTH, max_frag, 23158 ipsec_len, cksum); 23159 /* Software checksum? */ 23160 if (DB_CKSUMFLAGS(mp) == 0) { 23161 IP_STAT(ipst, ip_out_sw_cksum); 23162 IP_STAT_UPDATE(ipst, 23163 ip_udp_out_sw_cksum_bytes, 23164 LENGTH - hlen); 23165 } 23166 } 23167 } 23168 } 23169 /* 23170 * Need to do this even when fragmenting. The local 23171 * loopback can be done without computing checksums 23172 * but forwarding out other interface must be done 23173 * after the IP checksum (and ULP checksums) have been 23174 * computed. 23175 * 23176 * NOTE : multicast_forward is set only if this packet 23177 * originated from ip_wput. For packets originating from 23178 * ip_wput_multicast, it is not set. 23179 */ 23180 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 23181 multi_loopback: 23182 ip2dbg(("ip_wput: multicast, loop %d\n", 23183 conn_multicast_loop)); 23184 23185 /* Forget header checksum offload */ 23186 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 23187 23188 /* 23189 * Local loopback of multicasts? Check the 23190 * ill. 23191 * 23192 * Note that the loopback function will not come 23193 * in through ip_rput - it will only do the 23194 * client fanout thus we need to do an mforward 23195 * as well. The is different from the BSD 23196 * logic. 23197 */ 23198 if (ill != NULL) { 23199 ilm_t *ilm; 23200 23201 ILM_WALKER_HOLD(ill); 23202 ilm = ilm_lookup_ill(ill, ipha->ipha_dst, 23203 ALL_ZONES); 23204 ILM_WALKER_RELE(ill); 23205 if (ilm != NULL) { 23206 /* 23207 * Pass along the virtual output q. 23208 * ip_wput_local() will distribute the 23209 * packet to all the matching zones, 23210 * except the sending zone when 23211 * IP_MULTICAST_LOOP is false. 23212 */ 23213 ip_multicast_loopback(q, ill, first_mp, 23214 conn_multicast_loop ? 0 : 23215 IP_FF_NO_MCAST_LOOP, zoneid); 23216 } 23217 } 23218 if (ipha->ipha_ttl == 0) { 23219 /* 23220 * 0 => only to this host i.e. we are 23221 * done. We are also done if this was the 23222 * loopback interface since it is sufficient 23223 * to loopback one copy of a multicast packet. 23224 */ 23225 freemsg(first_mp); 23226 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23227 "ip_wput_ire_end: q %p (%S)", 23228 q, "loopback"); 23229 ire_refrele(ire); 23230 if (conn_outgoing_ill != NULL) 23231 ill_refrele(conn_outgoing_ill); 23232 return; 23233 } 23234 /* 23235 * ILLF_MULTICAST is checked in ip_newroute 23236 * i.e. we don't need to check it here since 23237 * all IRE_CACHEs come from ip_newroute. 23238 * For multicast traffic, SO_DONTROUTE is interpreted 23239 * to mean only send the packet out the interface 23240 * (optionally specified with IP_MULTICAST_IF) 23241 * and do not forward it out additional interfaces. 23242 * RSVP and the rsvp daemon is an example of a 23243 * protocol and user level process that 23244 * handles it's own routing. Hence, it uses the 23245 * SO_DONTROUTE option to accomplish this. 23246 */ 23247 23248 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 23249 ill != NULL) { 23250 /* Unconditionally redo the checksum */ 23251 ipha->ipha_hdr_checksum = 0; 23252 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23253 23254 /* 23255 * If this needs to go out secure, we need 23256 * to wait till we finish the IPSEC 23257 * processing. 23258 */ 23259 if (ipsec_len == 0 && 23260 ip_mforward(ill, ipha, mp)) { 23261 freemsg(first_mp); 23262 ip1dbg(("ip_wput: mforward failed\n")); 23263 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23264 "ip_wput_ire_end: q %p (%S)", 23265 q, "mforward failed"); 23266 ire_refrele(ire); 23267 if (conn_outgoing_ill != NULL) 23268 ill_refrele(conn_outgoing_ill); 23269 return; 23270 } 23271 } 23272 } 23273 max_frag = ire->ire_max_frag; 23274 cksum += ttl_protocol; 23275 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 23276 /* No fragmentation required for this one. */ 23277 /* 23278 * Don't use frag_flag if packet is pre-built or source 23279 * routed or if multicast (since multicast packets do 23280 * not solicit ICMP "packet too big" messages). 23281 */ 23282 if ((ip_hdr_included != IP_HDR_INCLUDED) && 23283 (V_HLEN == IP_SIMPLE_HDR_VERSION || 23284 !ip_source_route_included(ipha)) && 23285 !CLASSD(ipha->ipha_dst)) 23286 ipha->ipha_fragment_offset_and_flags |= 23287 htons(ire->ire_frag_flag); 23288 23289 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 23290 /* Complete the IP header checksum. */ 23291 cksum += ipha->ipha_ident; 23292 cksum += (v_hlen_tos_len >> 16)+ 23293 (v_hlen_tos_len & 0xFFFF); 23294 cksum += ipha->ipha_fragment_offset_and_flags; 23295 hlen = (V_HLEN & 0xF) - 23296 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 23297 if (hlen) { 23298 checksumoptions: 23299 /* 23300 * Account for the IP Options in the IP 23301 * header checksum. 23302 */ 23303 up = (uint16_t *)(rptr+ 23304 IP_SIMPLE_HDR_LENGTH); 23305 do { 23306 cksum += up[0]; 23307 cksum += up[1]; 23308 up += 2; 23309 } while (--hlen); 23310 } 23311 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 23312 cksum = ~(cksum + (cksum >> 16)); 23313 ipha->ipha_hdr_checksum = (uint16_t)cksum; 23314 } 23315 if (ipsec_len != 0) { 23316 ipsec_out_process(q, first_mp, ire, ill_index); 23317 if (!next_mp) { 23318 ire_refrele(ire); 23319 if (conn_outgoing_ill != NULL) 23320 ill_refrele(conn_outgoing_ill); 23321 return; 23322 } 23323 goto next; 23324 } 23325 23326 /* 23327 * multirt_send has already been handled 23328 * for broadcast, but not yet for multicast 23329 * or IP options. 23330 */ 23331 if (next_mp == NULL) { 23332 if (ire->ire_flags & RTF_MULTIRT) { 23333 multirt_send = B_TRUE; 23334 } 23335 } 23336 23337 /* 23338 * In most cases, the emission loop below is 23339 * entered only once. Only in the case where 23340 * the ire holds the RTF_MULTIRT flag, do we loop 23341 * to process all RTF_MULTIRT ires in the bucket, 23342 * and send the packet through all crossed 23343 * RTF_MULTIRT routes. 23344 */ 23345 do { 23346 if (multirt_send) { 23347 irb_t *irb; 23348 23349 irb = ire->ire_bucket; 23350 ASSERT(irb != NULL); 23351 /* 23352 * We are in a multiple send case, 23353 * need to get the next IRE and make 23354 * a duplicate of the packet. 23355 */ 23356 IRB_REFHOLD(irb); 23357 for (ire1 = ire->ire_next; 23358 ire1 != NULL; 23359 ire1 = ire1->ire_next) { 23360 if (!(ire1->ire_flags & 23361 RTF_MULTIRT)) 23362 continue; 23363 if (ire1->ire_addr != 23364 ire->ire_addr) 23365 continue; 23366 if (ire1->ire_marks & 23367 (IRE_MARK_CONDEMNED| 23368 IRE_MARK_HIDDEN)) 23369 continue; 23370 23371 /* Got one */ 23372 IRE_REFHOLD(ire1); 23373 break; 23374 } 23375 IRB_REFRELE(irb); 23376 23377 if (ire1 != NULL) { 23378 next_mp = copyb(mp); 23379 if ((next_mp == NULL) || 23380 ((mp->b_cont != NULL) && 23381 ((next_mp->b_cont = 23382 dupmsg(mp->b_cont)) 23383 == NULL))) { 23384 freemsg(next_mp); 23385 next_mp = NULL; 23386 ire_refrele(ire1); 23387 ire1 = NULL; 23388 } 23389 } 23390 23391 /* 23392 * Last multiroute ire; don't loop 23393 * anymore. The emission is over 23394 * and next_mp is NULL. 23395 */ 23396 if (ire1 == NULL) { 23397 multirt_send = B_FALSE; 23398 } 23399 } 23400 23401 out_ill = ire->ire_ipif->ipif_ill; 23402 DTRACE_PROBE4(ip4__physical__out__start, 23403 ill_t *, NULL, 23404 ill_t *, out_ill, 23405 ipha_t *, ipha, mblk_t *, mp); 23406 FW_HOOKS(ipst->ips_ip4_physical_out_event, 23407 ipst->ips_ipv4firewall_physical_out, 23408 NULL, out_ill, ipha, mp, mp, ipst); 23409 DTRACE_PROBE1(ip4__physical__out__end, 23410 mblk_t *, mp); 23411 if (mp == NULL) 23412 goto release_ire_and_ill_2; 23413 23414 ASSERT(ipsec_len == 0); 23415 mp->b_prev = 23416 SET_BPREV_FLAG(IPP_LOCAL_OUT); 23417 DTRACE_PROBE2(ip__xmit__2, 23418 mblk_t *, mp, ire_t *, ire); 23419 pktxmit_state = ip_xmit_v4(mp, ire, 23420 NULL, B_TRUE); 23421 if ((pktxmit_state == SEND_FAILED) || 23422 (pktxmit_state == LLHDR_RESLV_FAILED)) { 23423 release_ire_and_ill_2: 23424 if (next_mp) { 23425 freemsg(next_mp); 23426 ire_refrele(ire1); 23427 } 23428 ire_refrele(ire); 23429 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23430 "ip_wput_ire_end: q %p (%S)", 23431 q, "discard MDATA"); 23432 if (conn_outgoing_ill != NULL) 23433 ill_refrele(conn_outgoing_ill); 23434 return; 23435 } 23436 23437 if (CLASSD(dst)) { 23438 BUMP_MIB(out_ill->ill_ip_mib, 23439 ipIfStatsHCOutMcastPkts); 23440 UPDATE_MIB(out_ill->ill_ip_mib, 23441 ipIfStatsHCOutMcastOctets, 23442 LENGTH); 23443 } else if (ire->ire_type == IRE_BROADCAST) { 23444 BUMP_MIB(out_ill->ill_ip_mib, 23445 ipIfStatsHCOutBcastPkts); 23446 } 23447 23448 if (multirt_send) { 23449 /* 23450 * We are in a multiple send case, 23451 * need to re-enter the sending loop 23452 * using the next ire. 23453 */ 23454 ire_refrele(ire); 23455 ire = ire1; 23456 stq = ire->ire_stq; 23457 mp = next_mp; 23458 next_mp = NULL; 23459 ipha = (ipha_t *)mp->b_rptr; 23460 ill_index = Q_TO_INDEX(stq); 23461 } 23462 } while (multirt_send); 23463 23464 if (!next_mp) { 23465 /* 23466 * Last copy going out (the ultra-common 23467 * case). Note that we intentionally replicate 23468 * the putnext rather than calling it before 23469 * the next_mp check in hopes of a little 23470 * tail-call action out of the compiler. 23471 */ 23472 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23473 "ip_wput_ire_end: q %p (%S)", 23474 q, "last copy out(1)"); 23475 ire_refrele(ire); 23476 if (conn_outgoing_ill != NULL) 23477 ill_refrele(conn_outgoing_ill); 23478 return; 23479 } 23480 /* More copies going out below. */ 23481 } else { 23482 int offset; 23483 fragmentit: 23484 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23485 /* 23486 * If this would generate a icmp_frag_needed message, 23487 * we need to handle it before we do the IPSEC 23488 * processing. Otherwise, we need to strip the IPSEC 23489 * headers before we send up the message to the ULPs 23490 * which becomes messy and difficult. 23491 */ 23492 if (ipsec_len != 0) { 23493 if ((max_frag < (unsigned int)(LENGTH + 23494 ipsec_len)) && (offset & IPH_DF)) { 23495 out_ill = (ill_t *)stq->q_ptr; 23496 BUMP_MIB(out_ill->ill_ip_mib, 23497 ipIfStatsOutFragFails); 23498 BUMP_MIB(out_ill->ill_ip_mib, 23499 ipIfStatsOutFragReqds); 23500 ipha->ipha_hdr_checksum = 0; 23501 ipha->ipha_hdr_checksum = 23502 (uint16_t)ip_csum_hdr(ipha); 23503 icmp_frag_needed(ire->ire_stq, first_mp, 23504 max_frag, zoneid, ipst); 23505 if (!next_mp) { 23506 ire_refrele(ire); 23507 if (conn_outgoing_ill != NULL) { 23508 ill_refrele( 23509 conn_outgoing_ill); 23510 } 23511 return; 23512 } 23513 } else { 23514 /* 23515 * This won't cause a icmp_frag_needed 23516 * message. to be generated. Send it on 23517 * the wire. Note that this could still 23518 * cause fragmentation and all we 23519 * do is the generation of the message 23520 * to the ULP if needed before IPSEC. 23521 */ 23522 if (!next_mp) { 23523 ipsec_out_process(q, first_mp, 23524 ire, ill_index); 23525 TRACE_2(TR_FAC_IP, 23526 TR_IP_WPUT_IRE_END, 23527 "ip_wput_ire_end: q %p " 23528 "(%S)", q, 23529 "last ipsec_out_process"); 23530 ire_refrele(ire); 23531 if (conn_outgoing_ill != NULL) { 23532 ill_refrele( 23533 conn_outgoing_ill); 23534 } 23535 return; 23536 } 23537 ipsec_out_process(q, first_mp, 23538 ire, ill_index); 23539 } 23540 } else { 23541 /* 23542 * Initiate IPPF processing. For 23543 * fragmentable packets we finish 23544 * all QOS packet processing before 23545 * calling: 23546 * ip_wput_ire_fragmentit->ip_wput_frag 23547 */ 23548 23549 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23550 ip_process(IPP_LOCAL_OUT, &mp, 23551 ill_index); 23552 if (mp == NULL) { 23553 out_ill = (ill_t *)stq->q_ptr; 23554 BUMP_MIB(out_ill->ill_ip_mib, 23555 ipIfStatsOutDiscards); 23556 if (next_mp != NULL) { 23557 freemsg(next_mp); 23558 ire_refrele(ire1); 23559 } 23560 ire_refrele(ire); 23561 TRACE_2(TR_FAC_IP, 23562 TR_IP_WPUT_IRE_END, 23563 "ip_wput_ire: q %p (%S)", 23564 q, "discard MDATA"); 23565 if (conn_outgoing_ill != NULL) { 23566 ill_refrele( 23567 conn_outgoing_ill); 23568 } 23569 return; 23570 } 23571 } 23572 if (!next_mp) { 23573 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23574 "ip_wput_ire_end: q %p (%S)", 23575 q, "last fragmentation"); 23576 ip_wput_ire_fragmentit(mp, ire, 23577 zoneid, ipst); 23578 ire_refrele(ire); 23579 if (conn_outgoing_ill != NULL) 23580 ill_refrele(conn_outgoing_ill); 23581 return; 23582 } 23583 ip_wput_ire_fragmentit(mp, ire, zoneid, ipst); 23584 } 23585 } 23586 } else { 23587 nullstq: 23588 /* A NULL stq means the destination address is local. */ 23589 UPDATE_OB_PKT_COUNT(ire); 23590 ire->ire_last_used_time = lbolt; 23591 ASSERT(ire->ire_ipif != NULL); 23592 if (!next_mp) { 23593 /* 23594 * Is there an "in" and "out" for traffic local 23595 * to a host (loopback)? The code in Solaris doesn't 23596 * explicitly draw a line in its code for in vs out, 23597 * so we've had to draw a line in the sand: ip_wput_ire 23598 * is considered to be the "output" side and 23599 * ip_wput_local to be the "input" side. 23600 */ 23601 out_ill = ire->ire_ipif->ipif_ill; 23602 23603 DTRACE_PROBE4(ip4__loopback__out__start, 23604 ill_t *, NULL, ill_t *, out_ill, 23605 ipha_t *, ipha, mblk_t *, first_mp); 23606 23607 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23608 ipst->ips_ipv4firewall_loopback_out, 23609 NULL, out_ill, ipha, first_mp, mp, ipst); 23610 23611 DTRACE_PROBE1(ip4__loopback__out_end, 23612 mblk_t *, first_mp); 23613 23614 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23615 "ip_wput_ire_end: q %p (%S)", 23616 q, "local address"); 23617 23618 if (first_mp != NULL) 23619 ip_wput_local(q, out_ill, ipha, 23620 first_mp, ire, 0, ire->ire_zoneid); 23621 ire_refrele(ire); 23622 if (conn_outgoing_ill != NULL) 23623 ill_refrele(conn_outgoing_ill); 23624 return; 23625 } 23626 23627 out_ill = ire->ire_ipif->ipif_ill; 23628 23629 DTRACE_PROBE4(ip4__loopback__out__start, 23630 ill_t *, NULL, ill_t *, out_ill, 23631 ipha_t *, ipha, mblk_t *, first_mp); 23632 23633 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23634 ipst->ips_ipv4firewall_loopback_out, 23635 NULL, out_ill, ipha, first_mp, mp, ipst); 23636 23637 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, first_mp); 23638 23639 if (first_mp != NULL) 23640 ip_wput_local(q, out_ill, ipha, 23641 first_mp, ire, 0, ire->ire_zoneid); 23642 } 23643 next: 23644 /* 23645 * More copies going out to additional interfaces. 23646 * ire1 has already been held. We don't need the 23647 * "ire" anymore. 23648 */ 23649 ire_refrele(ire); 23650 ire = ire1; 23651 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 23652 mp = next_mp; 23653 ASSERT(ire->ire_ipversion == IPV4_VERSION); 23654 ill = ire_to_ill(ire); 23655 first_mp = mp; 23656 if (ipsec_len != 0) { 23657 ASSERT(first_mp->b_datap->db_type == M_CTL); 23658 mp = mp->b_cont; 23659 } 23660 dst = ire->ire_addr; 23661 ipha = (ipha_t *)mp->b_rptr; 23662 /* 23663 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 23664 * Restore ipha_ident "no checksum" flag. 23665 */ 23666 src = orig_src; 23667 ipha->ipha_ident = ip_hdr_included; 23668 goto another; 23669 23670 #undef rptr 23671 #undef Q_TO_INDEX 23672 } 23673 23674 /* 23675 * Routine to allocate a message that is used to notify the ULP about MDT. 23676 * The caller may provide a pointer to the link-layer MDT capabilities, 23677 * or NULL if MDT is to be disabled on the stream. 23678 */ 23679 mblk_t * 23680 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 23681 { 23682 mblk_t *mp; 23683 ip_mdt_info_t *mdti; 23684 ill_mdt_capab_t *idst; 23685 23686 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 23687 DB_TYPE(mp) = M_CTL; 23688 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 23689 mdti = (ip_mdt_info_t *)mp->b_rptr; 23690 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 23691 idst = &(mdti->mdt_capab); 23692 23693 /* 23694 * If the caller provides us with the capability, copy 23695 * it over into our notification message; otherwise 23696 * we zero out the capability portion. 23697 */ 23698 if (isrc != NULL) 23699 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23700 else 23701 bzero((caddr_t)idst, sizeof (*idst)); 23702 } 23703 return (mp); 23704 } 23705 23706 /* 23707 * Routine which determines whether MDT can be enabled on the destination 23708 * IRE and IPC combination, and if so, allocates and returns the MDT 23709 * notification mblk that may be used by ULP. We also check if we need to 23710 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 23711 * MDT usage in the past have been lifted. This gets called during IP 23712 * and ULP binding. 23713 */ 23714 mblk_t * 23715 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23716 ill_mdt_capab_t *mdt_cap) 23717 { 23718 mblk_t *mp; 23719 boolean_t rc = B_FALSE; 23720 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23721 23722 ASSERT(dst_ire != NULL); 23723 ASSERT(connp != NULL); 23724 ASSERT(mdt_cap != NULL); 23725 23726 /* 23727 * Currently, we only support simple TCP/{IPv4,IPv6} with 23728 * Multidata, which is handled in tcp_multisend(). This 23729 * is the reason why we do all these checks here, to ensure 23730 * that we don't enable Multidata for the cases which we 23731 * can't handle at the moment. 23732 */ 23733 do { 23734 /* Only do TCP at the moment */ 23735 if (connp->conn_ulp != IPPROTO_TCP) 23736 break; 23737 23738 /* 23739 * IPSEC outbound policy present? Note that we get here 23740 * after calling ipsec_conn_cache_policy() where the global 23741 * policy checking is performed. conn_latch will be 23742 * non-NULL as long as there's a policy defined, 23743 * i.e. conn_out_enforce_policy may be NULL in such case 23744 * when the connection is non-secure, and hence we check 23745 * further if the latch refers to an outbound policy. 23746 */ 23747 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 23748 break; 23749 23750 /* CGTP (multiroute) is enabled? */ 23751 if (dst_ire->ire_flags & RTF_MULTIRT) 23752 break; 23753 23754 /* Outbound IPQoS enabled? */ 23755 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23756 /* 23757 * In this case, we disable MDT for this and all 23758 * future connections going over the interface. 23759 */ 23760 mdt_cap->ill_mdt_on = 0; 23761 break; 23762 } 23763 23764 /* socket option(s) present? */ 23765 if (!CONN_IS_LSO_MD_FASTPATH(connp)) 23766 break; 23767 23768 rc = B_TRUE; 23769 /* CONSTCOND */ 23770 } while (0); 23771 23772 /* Remember the result */ 23773 connp->conn_mdt_ok = rc; 23774 23775 if (!rc) 23776 return (NULL); 23777 else if (!mdt_cap->ill_mdt_on) { 23778 /* 23779 * If MDT has been previously turned off in the past, and we 23780 * currently can do MDT (due to IPQoS policy removal, etc.) 23781 * then enable it for this interface. 23782 */ 23783 mdt_cap->ill_mdt_on = 1; 23784 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 23785 "interface %s\n", ill_name)); 23786 } 23787 23788 /* Allocate the MDT info mblk */ 23789 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 23790 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 23791 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23792 return (NULL); 23793 } 23794 return (mp); 23795 } 23796 23797 /* 23798 * Routine to allocate a message that is used to notify the ULP about LSO. 23799 * The caller may provide a pointer to the link-layer LSO capabilities, 23800 * or NULL if LSO is to be disabled on the stream. 23801 */ 23802 mblk_t * 23803 ip_lsoinfo_alloc(ill_lso_capab_t *isrc) 23804 { 23805 mblk_t *mp; 23806 ip_lso_info_t *lsoi; 23807 ill_lso_capab_t *idst; 23808 23809 if ((mp = allocb(sizeof (*lsoi), BPRI_HI)) != NULL) { 23810 DB_TYPE(mp) = M_CTL; 23811 mp->b_wptr = mp->b_rptr + sizeof (*lsoi); 23812 lsoi = (ip_lso_info_t *)mp->b_rptr; 23813 lsoi->lso_info_id = LSO_IOC_INFO_UPDATE; 23814 idst = &(lsoi->lso_capab); 23815 23816 /* 23817 * If the caller provides us with the capability, copy 23818 * it over into our notification message; otherwise 23819 * we zero out the capability portion. 23820 */ 23821 if (isrc != NULL) 23822 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23823 else 23824 bzero((caddr_t)idst, sizeof (*idst)); 23825 } 23826 return (mp); 23827 } 23828 23829 /* 23830 * Routine which determines whether LSO can be enabled on the destination 23831 * IRE and IPC combination, and if so, allocates and returns the LSO 23832 * notification mblk that may be used by ULP. We also check if we need to 23833 * turn LSO back to 'on' when certain restrictions prohibiting us to allow 23834 * LSO usage in the past have been lifted. This gets called during IP 23835 * and ULP binding. 23836 */ 23837 mblk_t * 23838 ip_lsoinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23839 ill_lso_capab_t *lso_cap) 23840 { 23841 mblk_t *mp; 23842 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23843 23844 ASSERT(dst_ire != NULL); 23845 ASSERT(connp != NULL); 23846 ASSERT(lso_cap != NULL); 23847 23848 connp->conn_lso_ok = B_TRUE; 23849 23850 if ((connp->conn_ulp != IPPROTO_TCP) || 23851 CONN_IPSEC_OUT_ENCAPSULATED(connp) || 23852 (dst_ire->ire_flags & RTF_MULTIRT) || 23853 !CONN_IS_LSO_MD_FASTPATH(connp) || 23854 (IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 23855 connp->conn_lso_ok = B_FALSE; 23856 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23857 /* 23858 * Disable LSO for this and all future connections going 23859 * over the interface. 23860 */ 23861 lso_cap->ill_lso_on = 0; 23862 } 23863 } 23864 23865 if (!connp->conn_lso_ok) 23866 return (NULL); 23867 else if (!lso_cap->ill_lso_on) { 23868 /* 23869 * If LSO has been previously turned off in the past, and we 23870 * currently can do LSO (due to IPQoS policy removal, etc.) 23871 * then enable it for this interface. 23872 */ 23873 lso_cap->ill_lso_on = 1; 23874 ip1dbg(("ip_mdinfo_return: reenabling LSO for interface %s\n", 23875 ill_name)); 23876 } 23877 23878 /* Allocate the LSO info mblk */ 23879 if ((mp = ip_lsoinfo_alloc(lso_cap)) == NULL) 23880 ip0dbg(("ip_lsoinfo_return: can't enable LSO for " 23881 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23882 23883 return (mp); 23884 } 23885 23886 /* 23887 * Create destination address attribute, and fill it with the physical 23888 * destination address and SAP taken from the template DL_UNITDATA_REQ 23889 * message block. 23890 */ 23891 boolean_t 23892 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 23893 { 23894 dl_unitdata_req_t *dlurp; 23895 pattr_t *pa; 23896 pattrinfo_t pa_info; 23897 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 23898 uint_t das_len, das_off; 23899 23900 ASSERT(dlmp != NULL); 23901 23902 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 23903 das_len = dlurp->dl_dest_addr_length; 23904 das_off = dlurp->dl_dest_addr_offset; 23905 23906 pa_info.type = PATTR_DSTADDRSAP; 23907 pa_info.len = sizeof (**das) + das_len - 1; 23908 23909 /* create and associate the attribute */ 23910 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23911 if (pa != NULL) { 23912 ASSERT(*das != NULL); 23913 (*das)->addr_is_group = 0; 23914 (*das)->addr_len = (uint8_t)das_len; 23915 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 23916 } 23917 23918 return (pa != NULL); 23919 } 23920 23921 /* 23922 * Create hardware checksum attribute and fill it with the values passed. 23923 */ 23924 boolean_t 23925 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 23926 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 23927 { 23928 pattr_t *pa; 23929 pattrinfo_t pa_info; 23930 23931 ASSERT(mmd != NULL); 23932 23933 pa_info.type = PATTR_HCKSUM; 23934 pa_info.len = sizeof (pattr_hcksum_t); 23935 23936 /* create and associate the attribute */ 23937 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23938 if (pa != NULL) { 23939 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 23940 23941 hck->hcksum_start_offset = start_offset; 23942 hck->hcksum_stuff_offset = stuff_offset; 23943 hck->hcksum_end_offset = end_offset; 23944 hck->hcksum_flags = flags; 23945 } 23946 return (pa != NULL); 23947 } 23948 23949 /* 23950 * Create zerocopy attribute and fill it with the specified flags 23951 */ 23952 boolean_t 23953 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 23954 { 23955 pattr_t *pa; 23956 pattrinfo_t pa_info; 23957 23958 ASSERT(mmd != NULL); 23959 pa_info.type = PATTR_ZCOPY; 23960 pa_info.len = sizeof (pattr_zcopy_t); 23961 23962 /* create and associate the attribute */ 23963 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23964 if (pa != NULL) { 23965 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 23966 23967 zcopy->zcopy_flags = flags; 23968 } 23969 return (pa != NULL); 23970 } 23971 23972 /* 23973 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 23974 * block chain. We could rewrite to handle arbitrary message block chains but 23975 * that would make the code complicated and slow. Right now there three 23976 * restrictions: 23977 * 23978 * 1. The first message block must contain the complete IP header and 23979 * at least 1 byte of payload data. 23980 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 23981 * so that we can use a single Multidata message. 23982 * 3. No frag must be distributed over two or more message blocks so 23983 * that we don't need more than two packet descriptors per frag. 23984 * 23985 * The above restrictions allow us to support userland applications (which 23986 * will send down a single message block) and NFS over UDP (which will 23987 * send down a chain of at most three message blocks). 23988 * 23989 * We also don't use MDT for payloads with less than or equal to 23990 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 23991 */ 23992 boolean_t 23993 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 23994 { 23995 int blocks; 23996 ssize_t total, missing, size; 23997 23998 ASSERT(mp != NULL); 23999 ASSERT(hdr_len > 0); 24000 24001 size = MBLKL(mp) - hdr_len; 24002 if (size <= 0) 24003 return (B_FALSE); 24004 24005 /* The first mblk contains the header and some payload. */ 24006 blocks = 1; 24007 total = size; 24008 size %= len; 24009 missing = (size == 0) ? 0 : (len - size); 24010 mp = mp->b_cont; 24011 24012 while (mp != NULL) { 24013 /* 24014 * Give up if we encounter a zero length message block. 24015 * In practice, this should rarely happen and therefore 24016 * not worth the trouble of freeing and re-linking the 24017 * mblk from the chain to handle such case. 24018 */ 24019 if ((size = MBLKL(mp)) == 0) 24020 return (B_FALSE); 24021 24022 /* Too many payload buffers for a single Multidata message? */ 24023 if (++blocks > MULTIDATA_MAX_PBUFS) 24024 return (B_FALSE); 24025 24026 total += size; 24027 /* Is a frag distributed over two or more message blocks? */ 24028 if (missing > size) 24029 return (B_FALSE); 24030 size -= missing; 24031 24032 size %= len; 24033 missing = (size == 0) ? 0 : (len - size); 24034 24035 mp = mp->b_cont; 24036 } 24037 24038 return (total > ip_wput_frag_mdt_min); 24039 } 24040 24041 /* 24042 * Outbound IPv4 fragmentation routine using MDT. 24043 */ 24044 static void 24045 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 24046 uint32_t frag_flag, int offset) 24047 { 24048 ipha_t *ipha_orig; 24049 int i1, ip_data_end; 24050 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 24051 mblk_t *hdr_mp, *md_mp = NULL; 24052 unsigned char *hdr_ptr, *pld_ptr; 24053 multidata_t *mmd; 24054 ip_pdescinfo_t pdi; 24055 ill_t *ill; 24056 ip_stack_t *ipst = ire->ire_ipst; 24057 24058 ASSERT(DB_TYPE(mp) == M_DATA); 24059 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 24060 24061 ill = ire_to_ill(ire); 24062 ASSERT(ill != NULL); 24063 24064 ipha_orig = (ipha_t *)mp->b_rptr; 24065 mp->b_rptr += sizeof (ipha_t); 24066 24067 /* Calculate how many packets we will send out */ 24068 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 24069 pkts = (i1 + len - 1) / len; 24070 ASSERT(pkts > 1); 24071 24072 /* Allocate a message block which will hold all the IP Headers. */ 24073 wroff = ipst->ips_ip_wroff_extra; 24074 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 24075 24076 i1 = pkts * hdr_chunk_len; 24077 /* 24078 * Create the header buffer, Multidata and destination address 24079 * and SAP attribute that should be associated with it. 24080 */ 24081 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 24082 ((hdr_mp->b_wptr += i1), 24083 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 24084 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 24085 freemsg(mp); 24086 if (md_mp == NULL) { 24087 freemsg(hdr_mp); 24088 } else { 24089 free_mmd: IP_STAT(ipst, ip_frag_mdt_discarded); 24090 freemsg(md_mp); 24091 } 24092 IP_STAT(ipst, ip_frag_mdt_allocfail); 24093 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 24094 return; 24095 } 24096 IP_STAT(ipst, ip_frag_mdt_allocd); 24097 24098 /* 24099 * Add a payload buffer to the Multidata; this operation must not 24100 * fail, or otherwise our logic in this routine is broken. There 24101 * is no memory allocation done by the routine, so any returned 24102 * failure simply tells us that we've done something wrong. 24103 * 24104 * A failure tells us that either we're adding the same payload 24105 * buffer more than once, or we're trying to add more buffers than 24106 * allowed. None of the above cases should happen, and we panic 24107 * because either there's horrible heap corruption, and/or 24108 * programming mistake. 24109 */ 24110 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24111 goto pbuf_panic; 24112 24113 hdr_ptr = hdr_mp->b_rptr; 24114 pld_ptr = mp->b_rptr; 24115 24116 /* Establish the ending byte offset, based on the starting offset. */ 24117 offset <<= 3; 24118 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 24119 IP_SIMPLE_HDR_LENGTH; 24120 24121 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 24122 24123 while (pld_ptr < mp->b_wptr) { 24124 ipha_t *ipha; 24125 uint16_t offset_and_flags; 24126 uint16_t ip_len; 24127 int error; 24128 24129 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 24130 ipha = (ipha_t *)(hdr_ptr + wroff); 24131 ASSERT(OK_32PTR(ipha)); 24132 *ipha = *ipha_orig; 24133 24134 if (ip_data_end - offset > len) { 24135 offset_and_flags = IPH_MF; 24136 } else { 24137 /* 24138 * Last frag. Set len to the length of this last piece. 24139 */ 24140 len = ip_data_end - offset; 24141 /* A frag of a frag might have IPH_MF non-zero */ 24142 offset_and_flags = 24143 ntohs(ipha->ipha_fragment_offset_and_flags) & 24144 IPH_MF; 24145 } 24146 offset_and_flags |= (uint16_t)(offset >> 3); 24147 offset_and_flags |= (uint16_t)frag_flag; 24148 /* Store the offset and flags in the IP header. */ 24149 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24150 24151 /* Store the length in the IP header. */ 24152 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 24153 ipha->ipha_length = htons(ip_len); 24154 24155 /* 24156 * Set the IP header checksum. Note that mp is just 24157 * the header, so this is easy to pass to ip_csum. 24158 */ 24159 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24160 24161 /* 24162 * Record offset and size of header and data of the next packet 24163 * in the multidata message. 24164 */ 24165 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 24166 PDESC_PLD_INIT(&pdi); 24167 i1 = MIN(mp->b_wptr - pld_ptr, len); 24168 ASSERT(i1 > 0); 24169 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 24170 if (i1 == len) { 24171 pld_ptr += len; 24172 } else { 24173 i1 = len - i1; 24174 mp = mp->b_cont; 24175 ASSERT(mp != NULL); 24176 ASSERT(MBLKL(mp) >= i1); 24177 /* 24178 * Attach the next payload message block to the 24179 * multidata message. 24180 */ 24181 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24182 goto pbuf_panic; 24183 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 24184 pld_ptr = mp->b_rptr + i1; 24185 } 24186 24187 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 24188 KM_NOSLEEP)) == NULL) { 24189 /* 24190 * Any failure other than ENOMEM indicates that we 24191 * have passed in invalid pdesc info or parameters 24192 * to mmd_addpdesc, which must not happen. 24193 * 24194 * EINVAL is a result of failure on boundary checks 24195 * against the pdesc info contents. It should not 24196 * happen, and we panic because either there's 24197 * horrible heap corruption, and/or programming 24198 * mistake. 24199 */ 24200 if (error != ENOMEM) { 24201 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 24202 "pdesc logic error detected for " 24203 "mmd %p pinfo %p (%d)\n", 24204 (void *)mmd, (void *)&pdi, error); 24205 /* NOTREACHED */ 24206 } 24207 IP_STAT(ipst, ip_frag_mdt_addpdescfail); 24208 /* Free unattached payload message blocks as well */ 24209 md_mp->b_cont = mp->b_cont; 24210 goto free_mmd; 24211 } 24212 24213 /* Advance fragment offset. */ 24214 offset += len; 24215 24216 /* Advance to location for next header in the buffer. */ 24217 hdr_ptr += hdr_chunk_len; 24218 24219 /* Did we reach the next payload message block? */ 24220 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 24221 mp = mp->b_cont; 24222 /* 24223 * Attach the next message block with payload 24224 * data to the multidata message. 24225 */ 24226 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24227 goto pbuf_panic; 24228 pld_ptr = mp->b_rptr; 24229 } 24230 } 24231 24232 ASSERT(hdr_mp->b_wptr == hdr_ptr); 24233 ASSERT(mp->b_wptr == pld_ptr); 24234 24235 /* Update IP statistics */ 24236 IP_STAT_UPDATE(ipst, ip_frag_mdt_pkt_out, pkts); 24237 24238 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates, pkts); 24239 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs); 24240 24241 len = ntohs(ipha_orig->ipha_length) + (pkts - 1) * IP_SIMPLE_HDR_LENGTH; 24242 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, pkts); 24243 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, len); 24244 24245 if (pkt_type == OB_PKT) { 24246 ire->ire_ob_pkt_count += pkts; 24247 if (ire->ire_ipif != NULL) 24248 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 24249 } else { 24250 /* 24251 * The type is IB_PKT in the forwarding path and in 24252 * the mobile IP case when the packet is being reverse- 24253 * tunneled to the home agent. 24254 */ 24255 ire->ire_ib_pkt_count += pkts; 24256 ASSERT(!IRE_IS_LOCAL(ire)); 24257 if (ire->ire_type & IRE_BROADCAST) { 24258 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 24259 } else { 24260 UPDATE_MIB(ill->ill_ip_mib, 24261 ipIfStatsHCOutForwDatagrams, pkts); 24262 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 24263 } 24264 } 24265 ire->ire_last_used_time = lbolt; 24266 /* Send it down */ 24267 putnext(ire->ire_stq, md_mp); 24268 return; 24269 24270 pbuf_panic: 24271 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 24272 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 24273 pbuf_idx); 24274 /* NOTREACHED */ 24275 } 24276 24277 /* 24278 * Outbound IP fragmentation routine. 24279 * 24280 * NOTE : This routine does not ire_refrele the ire that is passed in 24281 * as the argument. 24282 */ 24283 static void 24284 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 24285 uint32_t frag_flag, zoneid_t zoneid, ip_stack_t *ipst) 24286 { 24287 int i1; 24288 mblk_t *ll_hdr_mp; 24289 int ll_hdr_len; 24290 int hdr_len; 24291 mblk_t *hdr_mp; 24292 ipha_t *ipha; 24293 int ip_data_end; 24294 int len; 24295 mblk_t *mp = mp_orig, *mp1; 24296 int offset; 24297 queue_t *q; 24298 uint32_t v_hlen_tos_len; 24299 mblk_t *first_mp; 24300 boolean_t mctl_present; 24301 ill_t *ill; 24302 ill_t *out_ill; 24303 mblk_t *xmit_mp; 24304 mblk_t *carve_mp; 24305 ire_t *ire1 = NULL; 24306 ire_t *save_ire = NULL; 24307 mblk_t *next_mp = NULL; 24308 boolean_t last_frag = B_FALSE; 24309 boolean_t multirt_send = B_FALSE; 24310 ire_t *first_ire = NULL; 24311 irb_t *irb = NULL; 24312 mib2_ipIfStatsEntry_t *mibptr = NULL; 24313 24314 ill = ire_to_ill(ire); 24315 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 24316 24317 BUMP_MIB(mibptr, ipIfStatsOutFragReqds); 24318 24319 /* 24320 * IPSEC does not allow hw accelerated packets to be fragmented 24321 * This check is made in ip_wput_ipsec_out prior to coming here 24322 * via ip_wput_ire_fragmentit. 24323 * 24324 * If at this point we have an ire whose ARP request has not 24325 * been sent out, we call ip_xmit_v4->ire_arpresolve to trigger 24326 * sending of ARP query and change ire's state to ND_INCOMPLETE. 24327 * This packet and all fragmentable packets for this ire will 24328 * continue to get dropped while ire_nce->nce_state remains in 24329 * ND_INCOMPLETE. Post-ARP resolution, after ire's nce_state changes to 24330 * ND_REACHABLE, all subsquent large packets for this ire will 24331 * get fragemented and sent out by this function. 24332 */ 24333 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 24334 /* If nce_state is ND_INITIAL, trigger ARP query */ 24335 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 24336 ip1dbg(("ip_wput_frag: mac address for ire is unresolved" 24337 " - dropping packet\n")); 24338 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24339 freemsg(mp); 24340 return; 24341 } 24342 24343 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 24344 "ip_wput_frag_start:"); 24345 24346 if (mp->b_datap->db_type == M_CTL) { 24347 first_mp = mp; 24348 mp_orig = mp = mp->b_cont; 24349 mctl_present = B_TRUE; 24350 } else { 24351 first_mp = mp; 24352 mctl_present = B_FALSE; 24353 } 24354 24355 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 24356 ipha = (ipha_t *)mp->b_rptr; 24357 24358 /* 24359 * If the Don't Fragment flag is on, generate an ICMP destination 24360 * unreachable, fragmentation needed. 24361 */ 24362 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 24363 if (offset & IPH_DF) { 24364 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24365 /* 24366 * Need to compute hdr checksum if called from ip_wput_ire. 24367 * Note that ip_rput_forward verifies the checksum before 24368 * calling this routine so in that case this is a noop. 24369 */ 24370 ipha->ipha_hdr_checksum = 0; 24371 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24372 icmp_frag_needed(ire->ire_stq, first_mp, max_frag, zoneid, 24373 ipst); 24374 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24375 "ip_wput_frag_end:(%S)", 24376 "don't fragment"); 24377 return; 24378 } 24379 if (mctl_present) 24380 freeb(first_mp); 24381 /* 24382 * Establish the starting offset. May not be zero if we are fragging 24383 * a fragment that is being forwarded. 24384 */ 24385 offset = offset & IPH_OFFSET; 24386 24387 /* TODO why is this test needed? */ 24388 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 24389 if (((max_frag - LENGTH) & ~7) < 8) { 24390 /* TODO: notify ulp somehow */ 24391 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24392 freemsg(mp); 24393 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24394 "ip_wput_frag_end:(%S)", 24395 "len < 8"); 24396 return; 24397 } 24398 24399 hdr_len = (V_HLEN & 0xF) << 2; 24400 24401 ipha->ipha_hdr_checksum = 0; 24402 24403 /* 24404 * Establish the number of bytes maximum per frag, after putting 24405 * in the header. 24406 */ 24407 len = (max_frag - hdr_len) & ~7; 24408 24409 /* Check if we can use MDT to send out the frags. */ 24410 ASSERT(!IRE_IS_LOCAL(ire)); 24411 if (hdr_len == IP_SIMPLE_HDR_LENGTH && 24412 ipst->ips_ip_multidata_outbound && 24413 !(ire->ire_flags & RTF_MULTIRT) && 24414 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 24415 ill != NULL && ILL_MDT_CAPABLE(ill) && 24416 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 24417 ASSERT(ill->ill_mdt_capab != NULL); 24418 if (!ill->ill_mdt_capab->ill_mdt_on) { 24419 /* 24420 * If MDT has been previously turned off in the past, 24421 * and we currently can do MDT (due to IPQoS policy 24422 * removal, etc.) then enable it for this interface. 24423 */ 24424 ill->ill_mdt_capab->ill_mdt_on = 1; 24425 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 24426 ill->ill_name)); 24427 } 24428 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 24429 offset); 24430 return; 24431 } 24432 24433 /* Get a copy of the header for the trailing frags */ 24434 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst); 24435 if (!hdr_mp) { 24436 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24437 freemsg(mp); 24438 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24439 "ip_wput_frag_end:(%S)", 24440 "couldn't copy hdr"); 24441 return; 24442 } 24443 if (DB_CRED(mp) != NULL) 24444 mblk_setcred(hdr_mp, DB_CRED(mp)); 24445 24446 /* Store the starting offset, with the MoreFrags flag. */ 24447 i1 = offset | IPH_MF | frag_flag; 24448 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 24449 24450 /* Establish the ending byte offset, based on the starting offset. */ 24451 offset <<= 3; 24452 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 24453 24454 /* Store the length of the first fragment in the IP header. */ 24455 i1 = len + hdr_len; 24456 ASSERT(i1 <= IP_MAXPACKET); 24457 ipha->ipha_length = htons((uint16_t)i1); 24458 24459 /* 24460 * Compute the IP header checksum for the first frag. We have to 24461 * watch out that we stop at the end of the header. 24462 */ 24463 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24464 24465 /* 24466 * Now carve off the first frag. Note that this will include the 24467 * original IP header. 24468 */ 24469 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 24470 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24471 freeb(hdr_mp); 24472 freemsg(mp_orig); 24473 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24474 "ip_wput_frag_end:(%S)", 24475 "couldn't carve first"); 24476 return; 24477 } 24478 24479 /* 24480 * Multirouting case. Each fragment is replicated 24481 * via all non-condemned RTF_MULTIRT routes 24482 * currently resolved. 24483 * We ensure that first_ire is the first RTF_MULTIRT 24484 * ire in the bucket. 24485 */ 24486 if (ire->ire_flags & RTF_MULTIRT) { 24487 irb = ire->ire_bucket; 24488 ASSERT(irb != NULL); 24489 24490 multirt_send = B_TRUE; 24491 24492 /* Make sure we do not omit any multiroute ire. */ 24493 IRB_REFHOLD(irb); 24494 for (first_ire = irb->irb_ire; 24495 first_ire != NULL; 24496 first_ire = first_ire->ire_next) { 24497 if ((first_ire->ire_flags & RTF_MULTIRT) && 24498 (first_ire->ire_addr == ire->ire_addr) && 24499 !(first_ire->ire_marks & 24500 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 24501 break; 24502 } 24503 24504 if (first_ire != NULL) { 24505 if (first_ire != ire) { 24506 IRE_REFHOLD(first_ire); 24507 /* 24508 * Do not release the ire passed in 24509 * as the argument. 24510 */ 24511 ire = first_ire; 24512 } else { 24513 first_ire = NULL; 24514 } 24515 } 24516 IRB_REFRELE(irb); 24517 24518 /* 24519 * Save the first ire; we will need to restore it 24520 * for the trailing frags. 24521 * We REFHOLD save_ire, as each iterated ire will be 24522 * REFRELEd. 24523 */ 24524 save_ire = ire; 24525 IRE_REFHOLD(save_ire); 24526 } 24527 24528 /* 24529 * First fragment emission loop. 24530 * In most cases, the emission loop below is entered only 24531 * once. Only in the case where the ire holds the RTF_MULTIRT 24532 * flag, do we loop to process all RTF_MULTIRT ires in the 24533 * bucket, and send the fragment through all crossed 24534 * RTF_MULTIRT routes. 24535 */ 24536 do { 24537 if (ire->ire_flags & RTF_MULTIRT) { 24538 /* 24539 * We are in a multiple send case, need to get 24540 * the next ire and make a copy of the packet. 24541 * ire1 holds here the next ire to process in the 24542 * bucket. If multirouting is expected, 24543 * any non-RTF_MULTIRT ire that has the 24544 * right destination address is ignored. 24545 * 24546 * We have to take into account the MTU of 24547 * each walked ire. max_frag is set by the 24548 * the caller and generally refers to 24549 * the primary ire entry. Here we ensure that 24550 * no route with a lower MTU will be used, as 24551 * fragments are carved once for all ires, 24552 * then replicated. 24553 */ 24554 ASSERT(irb != NULL); 24555 IRB_REFHOLD(irb); 24556 for (ire1 = ire->ire_next; 24557 ire1 != NULL; 24558 ire1 = ire1->ire_next) { 24559 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 24560 continue; 24561 if (ire1->ire_addr != ire->ire_addr) 24562 continue; 24563 if (ire1->ire_marks & 24564 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 24565 continue; 24566 /* 24567 * Ensure we do not exceed the MTU 24568 * of the next route. 24569 */ 24570 if (ire1->ire_max_frag < max_frag) { 24571 ip_multirt_bad_mtu(ire1, max_frag); 24572 continue; 24573 } 24574 24575 /* Got one. */ 24576 IRE_REFHOLD(ire1); 24577 break; 24578 } 24579 IRB_REFRELE(irb); 24580 24581 if (ire1 != NULL) { 24582 next_mp = copyb(mp); 24583 if ((next_mp == NULL) || 24584 ((mp->b_cont != NULL) && 24585 ((next_mp->b_cont = 24586 dupmsg(mp->b_cont)) == NULL))) { 24587 freemsg(next_mp); 24588 next_mp = NULL; 24589 ire_refrele(ire1); 24590 ire1 = NULL; 24591 } 24592 } 24593 24594 /* Last multiroute ire; don't loop anymore. */ 24595 if (ire1 == NULL) { 24596 multirt_send = B_FALSE; 24597 } 24598 } 24599 24600 ll_hdr_len = 0; 24601 LOCK_IRE_FP_MP(ire); 24602 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24603 if (ll_hdr_mp != NULL) { 24604 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24605 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 24606 } else { 24607 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24608 } 24609 24610 /* If there is a transmit header, get a copy for this frag. */ 24611 /* 24612 * TODO: should check db_ref before calling ip_carve_mp since 24613 * it might give us a dup. 24614 */ 24615 if (!ll_hdr_mp) { 24616 /* No xmit header. */ 24617 xmit_mp = mp; 24618 24619 /* We have a link-layer header that can fit in our mblk. */ 24620 } else if (mp->b_datap->db_ref == 1 && 24621 ll_hdr_len != 0 && 24622 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24623 /* M_DATA fastpath */ 24624 mp->b_rptr -= ll_hdr_len; 24625 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 24626 xmit_mp = mp; 24627 24628 /* Corner case if copyb has failed */ 24629 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 24630 UNLOCK_IRE_FP_MP(ire); 24631 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24632 freeb(hdr_mp); 24633 freemsg(mp); 24634 freemsg(mp_orig); 24635 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24636 "ip_wput_frag_end:(%S)", 24637 "discard"); 24638 24639 if (multirt_send) { 24640 ASSERT(ire1); 24641 ASSERT(next_mp); 24642 24643 freemsg(next_mp); 24644 ire_refrele(ire1); 24645 } 24646 if (save_ire != NULL) 24647 IRE_REFRELE(save_ire); 24648 24649 if (first_ire != NULL) 24650 ire_refrele(first_ire); 24651 return; 24652 24653 /* 24654 * Case of res_mp OR the fastpath mp can't fit 24655 * in the mblk 24656 */ 24657 } else { 24658 xmit_mp->b_cont = mp; 24659 if (DB_CRED(mp) != NULL) 24660 mblk_setcred(xmit_mp, DB_CRED(mp)); 24661 /* 24662 * Get priority marking, if any. 24663 * We propagate the CoS marking from the 24664 * original packet that went to QoS processing 24665 * in ip_wput_ire to the newly carved mp. 24666 */ 24667 if (DB_TYPE(xmit_mp) == M_DATA) 24668 xmit_mp->b_band = mp->b_band; 24669 } 24670 UNLOCK_IRE_FP_MP(ire); 24671 24672 q = ire->ire_stq; 24673 out_ill = (ill_t *)q->q_ptr; 24674 24675 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24676 24677 DTRACE_PROBE4(ip4__physical__out__start, 24678 ill_t *, NULL, ill_t *, out_ill, 24679 ipha_t *, ipha, mblk_t *, xmit_mp); 24680 24681 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24682 ipst->ips_ipv4firewall_physical_out, 24683 NULL, out_ill, ipha, xmit_mp, mp, ipst); 24684 24685 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, xmit_mp); 24686 24687 if (xmit_mp != NULL) { 24688 putnext(q, xmit_mp); 24689 24690 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 24691 UPDATE_MIB(out_ill->ill_ip_mib, 24692 ipIfStatsHCOutOctets, i1); 24693 24694 if (pkt_type != OB_PKT) { 24695 /* 24696 * Update the packet count and MIB stats 24697 * of trailing RTF_MULTIRT ires. 24698 */ 24699 UPDATE_OB_PKT_COUNT(ire); 24700 BUMP_MIB(out_ill->ill_ip_mib, 24701 ipIfStatsOutFragReqds); 24702 } 24703 } 24704 24705 if (multirt_send) { 24706 /* 24707 * We are in a multiple send case; look for 24708 * the next ire and re-enter the loop. 24709 */ 24710 ASSERT(ire1); 24711 ASSERT(next_mp); 24712 /* REFRELE the current ire before looping */ 24713 ire_refrele(ire); 24714 ire = ire1; 24715 ire1 = NULL; 24716 mp = next_mp; 24717 next_mp = NULL; 24718 } 24719 } while (multirt_send); 24720 24721 ASSERT(ire1 == NULL); 24722 24723 /* Restore the original ire; we need it for the trailing frags */ 24724 if (save_ire != NULL) { 24725 /* REFRELE the last iterated ire */ 24726 ire_refrele(ire); 24727 /* save_ire has been REFHOLDed */ 24728 ire = save_ire; 24729 save_ire = NULL; 24730 q = ire->ire_stq; 24731 } 24732 24733 if (pkt_type == OB_PKT) { 24734 UPDATE_OB_PKT_COUNT(ire); 24735 } else { 24736 out_ill = (ill_t *)q->q_ptr; 24737 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 24738 UPDATE_IB_PKT_COUNT(ire); 24739 } 24740 24741 /* Advance the offset to the second frag starting point. */ 24742 offset += len; 24743 /* 24744 * Update hdr_len from the copied header - there might be less options 24745 * in the later fragments. 24746 */ 24747 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 24748 /* Loop until done. */ 24749 for (;;) { 24750 uint16_t offset_and_flags; 24751 uint16_t ip_len; 24752 24753 if (ip_data_end - offset > len) { 24754 /* 24755 * Carve off the appropriate amount from the original 24756 * datagram. 24757 */ 24758 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24759 mp = NULL; 24760 break; 24761 } 24762 /* 24763 * More frags after this one. Get another copy 24764 * of the header. 24765 */ 24766 if (carve_mp->b_datap->db_ref == 1 && 24767 hdr_mp->b_wptr - hdr_mp->b_rptr < 24768 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24769 /* Inline IP header */ 24770 carve_mp->b_rptr -= hdr_mp->b_wptr - 24771 hdr_mp->b_rptr; 24772 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24773 hdr_mp->b_wptr - hdr_mp->b_rptr); 24774 mp = carve_mp; 24775 } else { 24776 if (!(mp = copyb(hdr_mp))) { 24777 freemsg(carve_mp); 24778 break; 24779 } 24780 /* Get priority marking, if any. */ 24781 mp->b_band = carve_mp->b_band; 24782 mp->b_cont = carve_mp; 24783 } 24784 ipha = (ipha_t *)mp->b_rptr; 24785 offset_and_flags = IPH_MF; 24786 } else { 24787 /* 24788 * Last frag. Consume the header. Set len to 24789 * the length of this last piece. 24790 */ 24791 len = ip_data_end - offset; 24792 24793 /* 24794 * Carve off the appropriate amount from the original 24795 * datagram. 24796 */ 24797 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24798 mp = NULL; 24799 break; 24800 } 24801 if (carve_mp->b_datap->db_ref == 1 && 24802 hdr_mp->b_wptr - hdr_mp->b_rptr < 24803 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24804 /* Inline IP header */ 24805 carve_mp->b_rptr -= hdr_mp->b_wptr - 24806 hdr_mp->b_rptr; 24807 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24808 hdr_mp->b_wptr - hdr_mp->b_rptr); 24809 mp = carve_mp; 24810 freeb(hdr_mp); 24811 hdr_mp = mp; 24812 } else { 24813 mp = hdr_mp; 24814 /* Get priority marking, if any. */ 24815 mp->b_band = carve_mp->b_band; 24816 mp->b_cont = carve_mp; 24817 } 24818 ipha = (ipha_t *)mp->b_rptr; 24819 /* A frag of a frag might have IPH_MF non-zero */ 24820 offset_and_flags = 24821 ntohs(ipha->ipha_fragment_offset_and_flags) & 24822 IPH_MF; 24823 } 24824 offset_and_flags |= (uint16_t)(offset >> 3); 24825 offset_and_flags |= (uint16_t)frag_flag; 24826 /* Store the offset and flags in the IP header. */ 24827 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24828 24829 /* Store the length in the IP header. */ 24830 ip_len = (uint16_t)(len + hdr_len); 24831 ipha->ipha_length = htons(ip_len); 24832 24833 /* 24834 * Set the IP header checksum. Note that mp is just 24835 * the header, so this is easy to pass to ip_csum. 24836 */ 24837 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24838 24839 /* Attach a transmit header, if any, and ship it. */ 24840 if (pkt_type == OB_PKT) { 24841 UPDATE_OB_PKT_COUNT(ire); 24842 } else { 24843 out_ill = (ill_t *)q->q_ptr; 24844 BUMP_MIB(out_ill->ill_ip_mib, 24845 ipIfStatsHCOutForwDatagrams); 24846 UPDATE_IB_PKT_COUNT(ire); 24847 } 24848 24849 if (ire->ire_flags & RTF_MULTIRT) { 24850 irb = ire->ire_bucket; 24851 ASSERT(irb != NULL); 24852 24853 multirt_send = B_TRUE; 24854 24855 /* 24856 * Save the original ire; we will need to restore it 24857 * for the tailing frags. 24858 */ 24859 save_ire = ire; 24860 IRE_REFHOLD(save_ire); 24861 } 24862 /* 24863 * Emission loop for this fragment, similar 24864 * to what is done for the first fragment. 24865 */ 24866 do { 24867 if (multirt_send) { 24868 /* 24869 * We are in a multiple send case, need to get 24870 * the next ire and make a copy of the packet. 24871 */ 24872 ASSERT(irb != NULL); 24873 IRB_REFHOLD(irb); 24874 for (ire1 = ire->ire_next; 24875 ire1 != NULL; 24876 ire1 = ire1->ire_next) { 24877 if (!(ire1->ire_flags & RTF_MULTIRT)) 24878 continue; 24879 if (ire1->ire_addr != ire->ire_addr) 24880 continue; 24881 if (ire1->ire_marks & 24882 (IRE_MARK_CONDEMNED| 24883 IRE_MARK_HIDDEN)) 24884 continue; 24885 /* 24886 * Ensure we do not exceed the MTU 24887 * of the next route. 24888 */ 24889 if (ire1->ire_max_frag < max_frag) { 24890 ip_multirt_bad_mtu(ire1, 24891 max_frag); 24892 continue; 24893 } 24894 24895 /* Got one. */ 24896 IRE_REFHOLD(ire1); 24897 break; 24898 } 24899 IRB_REFRELE(irb); 24900 24901 if (ire1 != NULL) { 24902 next_mp = copyb(mp); 24903 if ((next_mp == NULL) || 24904 ((mp->b_cont != NULL) && 24905 ((next_mp->b_cont = 24906 dupmsg(mp->b_cont)) == NULL))) { 24907 freemsg(next_mp); 24908 next_mp = NULL; 24909 ire_refrele(ire1); 24910 ire1 = NULL; 24911 } 24912 } 24913 24914 /* Last multiroute ire; don't loop anymore. */ 24915 if (ire1 == NULL) { 24916 multirt_send = B_FALSE; 24917 } 24918 } 24919 24920 /* Update transmit header */ 24921 ll_hdr_len = 0; 24922 LOCK_IRE_FP_MP(ire); 24923 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24924 if (ll_hdr_mp != NULL) { 24925 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24926 ll_hdr_len = MBLKL(ll_hdr_mp); 24927 } else { 24928 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24929 } 24930 24931 if (!ll_hdr_mp) { 24932 xmit_mp = mp; 24933 24934 /* 24935 * We have link-layer header that can fit in 24936 * our mblk. 24937 */ 24938 } else if (mp->b_datap->db_ref == 1 && 24939 ll_hdr_len != 0 && 24940 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24941 /* M_DATA fastpath */ 24942 mp->b_rptr -= ll_hdr_len; 24943 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 24944 ll_hdr_len); 24945 xmit_mp = mp; 24946 24947 /* 24948 * Case of res_mp OR the fastpath mp can't fit 24949 * in the mblk 24950 */ 24951 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 24952 xmit_mp->b_cont = mp; 24953 if (DB_CRED(mp) != NULL) 24954 mblk_setcred(xmit_mp, DB_CRED(mp)); 24955 /* Get priority marking, if any. */ 24956 if (DB_TYPE(xmit_mp) == M_DATA) 24957 xmit_mp->b_band = mp->b_band; 24958 24959 /* Corner case if copyb failed */ 24960 } else { 24961 /* 24962 * Exit both the replication and 24963 * fragmentation loops. 24964 */ 24965 UNLOCK_IRE_FP_MP(ire); 24966 goto drop_pkt; 24967 } 24968 UNLOCK_IRE_FP_MP(ire); 24969 24970 mp1 = mp; 24971 out_ill = (ill_t *)q->q_ptr; 24972 24973 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24974 24975 DTRACE_PROBE4(ip4__physical__out__start, 24976 ill_t *, NULL, ill_t *, out_ill, 24977 ipha_t *, ipha, mblk_t *, xmit_mp); 24978 24979 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24980 ipst->ips_ipv4firewall_physical_out, 24981 NULL, out_ill, ipha, xmit_mp, mp, ipst); 24982 24983 DTRACE_PROBE1(ip4__physical__out__end, 24984 mblk_t *, xmit_mp); 24985 24986 if (mp != mp1 && hdr_mp == mp1) 24987 hdr_mp = mp; 24988 if (mp != mp1 && mp_orig == mp1) 24989 mp_orig = mp; 24990 24991 if (xmit_mp != NULL) { 24992 putnext(q, xmit_mp); 24993 24994 BUMP_MIB(out_ill->ill_ip_mib, 24995 ipIfStatsHCOutTransmits); 24996 UPDATE_MIB(out_ill->ill_ip_mib, 24997 ipIfStatsHCOutOctets, ip_len); 24998 24999 if (pkt_type != OB_PKT) { 25000 /* 25001 * Update the packet count of trailing 25002 * RTF_MULTIRT ires. 25003 */ 25004 UPDATE_OB_PKT_COUNT(ire); 25005 } 25006 } 25007 25008 /* All done if we just consumed the hdr_mp. */ 25009 if (mp == hdr_mp) { 25010 last_frag = B_TRUE; 25011 BUMP_MIB(out_ill->ill_ip_mib, 25012 ipIfStatsOutFragOKs); 25013 } 25014 25015 if (multirt_send) { 25016 /* 25017 * We are in a multiple send case; look for 25018 * the next ire and re-enter the loop. 25019 */ 25020 ASSERT(ire1); 25021 ASSERT(next_mp); 25022 /* REFRELE the current ire before looping */ 25023 ire_refrele(ire); 25024 ire = ire1; 25025 ire1 = NULL; 25026 q = ire->ire_stq; 25027 mp = next_mp; 25028 next_mp = NULL; 25029 } 25030 } while (multirt_send); 25031 /* 25032 * Restore the original ire; we need it for the 25033 * trailing frags 25034 */ 25035 if (save_ire != NULL) { 25036 ASSERT(ire1 == NULL); 25037 /* REFRELE the last iterated ire */ 25038 ire_refrele(ire); 25039 /* save_ire has been REFHOLDed */ 25040 ire = save_ire; 25041 q = ire->ire_stq; 25042 save_ire = NULL; 25043 } 25044 25045 if (last_frag) { 25046 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 25047 "ip_wput_frag_end:(%S)", 25048 "consumed hdr_mp"); 25049 25050 if (first_ire != NULL) 25051 ire_refrele(first_ire); 25052 return; 25053 } 25054 /* Otherwise, advance and loop. */ 25055 offset += len; 25056 } 25057 25058 drop_pkt: 25059 /* Clean up following allocation failure. */ 25060 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 25061 freemsg(mp); 25062 if (mp != hdr_mp) 25063 freeb(hdr_mp); 25064 if (mp != mp_orig) 25065 freemsg(mp_orig); 25066 25067 if (save_ire != NULL) 25068 IRE_REFRELE(save_ire); 25069 if (first_ire != NULL) 25070 ire_refrele(first_ire); 25071 25072 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 25073 "ip_wput_frag_end:(%S)", 25074 "end--alloc failure"); 25075 } 25076 25077 /* 25078 * Copy the header plus those options which have the copy bit set 25079 */ 25080 static mblk_t * 25081 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst) 25082 { 25083 mblk_t *mp; 25084 uchar_t *up; 25085 25086 /* 25087 * Quick check if we need to look for options without the copy bit 25088 * set 25089 */ 25090 mp = allocb(ipst->ips_ip_wroff_extra + hdr_len, BPRI_HI); 25091 if (!mp) 25092 return (mp); 25093 mp->b_rptr += ipst->ips_ip_wroff_extra; 25094 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 25095 bcopy(rptr, mp->b_rptr, hdr_len); 25096 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra; 25097 return (mp); 25098 } 25099 up = mp->b_rptr; 25100 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 25101 up += IP_SIMPLE_HDR_LENGTH; 25102 rptr += IP_SIMPLE_HDR_LENGTH; 25103 hdr_len -= IP_SIMPLE_HDR_LENGTH; 25104 while (hdr_len > 0) { 25105 uint32_t optval; 25106 uint32_t optlen; 25107 25108 optval = *rptr; 25109 if (optval == IPOPT_EOL) 25110 break; 25111 if (optval == IPOPT_NOP) 25112 optlen = 1; 25113 else 25114 optlen = rptr[1]; 25115 if (optval & IPOPT_COPY) { 25116 bcopy(rptr, up, optlen); 25117 up += optlen; 25118 } 25119 rptr += optlen; 25120 hdr_len -= optlen; 25121 } 25122 /* 25123 * Make sure that we drop an even number of words by filling 25124 * with EOL to the next word boundary. 25125 */ 25126 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 25127 hdr_len & 0x3; hdr_len++) 25128 *up++ = IPOPT_EOL; 25129 mp->b_wptr = up; 25130 /* Update header length */ 25131 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 25132 return (mp); 25133 } 25134 25135 /* 25136 * Delivery to local recipients including fanout to multiple recipients. 25137 * Does not do checksumming of UDP/TCP. 25138 * Note: q should be the read side queue for either the ill or conn. 25139 * Note: rq should be the read side q for the lower (ill) stream. 25140 * We don't send packets to IPPF processing, thus the last argument 25141 * to all the fanout calls are B_FALSE. 25142 */ 25143 void 25144 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 25145 int fanout_flags, zoneid_t zoneid) 25146 { 25147 uint32_t protocol; 25148 mblk_t *first_mp; 25149 boolean_t mctl_present; 25150 int ire_type; 25151 #define rptr ((uchar_t *)ipha) 25152 ip_stack_t *ipst = ill->ill_ipst; 25153 25154 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 25155 "ip_wput_local_start: q %p", q); 25156 25157 if (ire != NULL) { 25158 ire_type = ire->ire_type; 25159 } else { 25160 /* 25161 * Only ip_multicast_loopback() calls us with a NULL ire. If the 25162 * packet is not multicast, we can't tell the ire type. 25163 */ 25164 ASSERT(CLASSD(ipha->ipha_dst)); 25165 ire_type = IRE_BROADCAST; 25166 } 25167 25168 first_mp = mp; 25169 if (first_mp->b_datap->db_type == M_CTL) { 25170 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 25171 if (!io->ipsec_out_secure) { 25172 /* 25173 * This ipsec_out_t was allocated in ip_wput 25174 * for multicast packets to store the ill_index. 25175 * As this is being delivered locally, we don't 25176 * need this anymore. 25177 */ 25178 mp = first_mp->b_cont; 25179 freeb(first_mp); 25180 first_mp = mp; 25181 mctl_present = B_FALSE; 25182 } else { 25183 /* 25184 * Convert IPSEC_OUT to IPSEC_IN, preserving all 25185 * security properties for the looped-back packet. 25186 */ 25187 mctl_present = B_TRUE; 25188 mp = first_mp->b_cont; 25189 ASSERT(mp != NULL); 25190 ipsec_out_to_in(first_mp); 25191 } 25192 } else { 25193 mctl_present = B_FALSE; 25194 } 25195 25196 DTRACE_PROBE4(ip4__loopback__in__start, 25197 ill_t *, ill, ill_t *, NULL, 25198 ipha_t *, ipha, mblk_t *, first_mp); 25199 25200 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 25201 ipst->ips_ipv4firewall_loopback_in, 25202 ill, NULL, ipha, first_mp, mp, ipst); 25203 25204 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, first_mp); 25205 25206 if (first_mp == NULL) 25207 return; 25208 25209 ipst->ips_loopback_packets++; 25210 25211 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 25212 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 25213 if (!IS_SIMPLE_IPH(ipha)) { 25214 ip_wput_local_options(ipha, ipst); 25215 } 25216 25217 protocol = ipha->ipha_protocol; 25218 switch (protocol) { 25219 case IPPROTO_ICMP: { 25220 ire_t *ire_zone; 25221 ilm_t *ilm; 25222 mblk_t *mp1; 25223 zoneid_t last_zoneid; 25224 25225 if (CLASSD(ipha->ipha_dst) && 25226 !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 25227 ASSERT(ire_type == IRE_BROADCAST); 25228 /* 25229 * In the multicast case, applications may have joined 25230 * the group from different zones, so we need to deliver 25231 * the packet to each of them. Loop through the 25232 * multicast memberships structures (ilm) on the receive 25233 * ill and send a copy of the packet up each matching 25234 * one. However, we don't do this for multicasts sent on 25235 * the loopback interface (PHYI_LOOPBACK flag set) as 25236 * they must stay in the sender's zone. 25237 * 25238 * ilm_add_v6() ensures that ilms in the same zone are 25239 * contiguous in the ill_ilm list. We use this property 25240 * to avoid sending duplicates needed when two 25241 * applications in the same zone join the same group on 25242 * different logical interfaces: we ignore the ilm if 25243 * it's zoneid is the same as the last matching one. 25244 * In addition, the sending of the packet for 25245 * ire_zoneid is delayed until all of the other ilms 25246 * have been exhausted. 25247 */ 25248 last_zoneid = -1; 25249 ILM_WALKER_HOLD(ill); 25250 for (ilm = ill->ill_ilm; ilm != NULL; 25251 ilm = ilm->ilm_next) { 25252 if ((ilm->ilm_flags & ILM_DELETED) || 25253 ipha->ipha_dst != ilm->ilm_addr || 25254 ilm->ilm_zoneid == last_zoneid || 25255 ilm->ilm_zoneid == zoneid || 25256 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 25257 continue; 25258 mp1 = ip_copymsg(first_mp); 25259 if (mp1 == NULL) 25260 continue; 25261 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25262 mctl_present, B_FALSE, ill, 25263 ilm->ilm_zoneid); 25264 last_zoneid = ilm->ilm_zoneid; 25265 } 25266 ILM_WALKER_RELE(ill); 25267 /* 25268 * Loopback case: the sending endpoint has 25269 * IP_MULTICAST_LOOP disabled, therefore we don't 25270 * dispatch the multicast packet to the sending zone. 25271 */ 25272 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 25273 freemsg(first_mp); 25274 return; 25275 } 25276 } else if (ire_type == IRE_BROADCAST) { 25277 /* 25278 * In the broadcast case, there may be many zones 25279 * which need a copy of the packet delivered to them. 25280 * There is one IRE_BROADCAST per broadcast address 25281 * and per zone; we walk those using a helper function. 25282 * In addition, the sending of the packet for zoneid is 25283 * delayed until all of the other ires have been 25284 * processed. 25285 */ 25286 IRB_REFHOLD(ire->ire_bucket); 25287 ire_zone = NULL; 25288 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 25289 ire)) != NULL) { 25290 mp1 = ip_copymsg(first_mp); 25291 if (mp1 == NULL) 25292 continue; 25293 25294 UPDATE_IB_PKT_COUNT(ire_zone); 25295 ire_zone->ire_last_used_time = lbolt; 25296 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25297 mctl_present, B_FALSE, ill, 25298 ire_zone->ire_zoneid); 25299 } 25300 IRB_REFRELE(ire->ire_bucket); 25301 } 25302 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 25303 0, mctl_present, B_FALSE, ill, zoneid); 25304 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25305 "ip_wput_local_end: q %p (%S)", 25306 q, "icmp"); 25307 return; 25308 } 25309 case IPPROTO_IGMP: 25310 if ((mp = igmp_input(q, mp, ill)) == NULL) { 25311 /* Bad packet - discarded by igmp_input */ 25312 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25313 "ip_wput_local_end: q %p (%S)", 25314 q, "igmp_input--bad packet"); 25315 if (mctl_present) 25316 freeb(first_mp); 25317 return; 25318 } 25319 /* 25320 * igmp_input() may have returned the pulled up message. 25321 * So first_mp and ipha need to be reinitialized. 25322 */ 25323 ipha = (ipha_t *)mp->b_rptr; 25324 if (mctl_present) 25325 first_mp->b_cont = mp; 25326 else 25327 first_mp = mp; 25328 /* deliver to local raw users */ 25329 break; 25330 case IPPROTO_ENCAP: 25331 /* 25332 * This case is covered by either ip_fanout_proto, or by 25333 * the above security processing for self-tunneled packets. 25334 */ 25335 break; 25336 case IPPROTO_UDP: { 25337 uint16_t *up; 25338 uint32_t ports; 25339 25340 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 25341 UDP_PORTS_OFFSET); 25342 /* Force a 'valid' checksum. */ 25343 up[3] = 0; 25344 25345 ports = *(uint32_t *)up; 25346 ip_fanout_udp(q, first_mp, ill, ipha, ports, 25347 (ire_type == IRE_BROADCAST), 25348 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25349 IP_FF_SEND_SLLA | IP_FF_IPINFO, mctl_present, B_FALSE, 25350 ill, zoneid); 25351 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25352 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 25353 return; 25354 } 25355 case IPPROTO_TCP: { 25356 25357 /* 25358 * For TCP, discard broadcast packets. 25359 */ 25360 if ((ushort_t)ire_type == IRE_BROADCAST) { 25361 freemsg(first_mp); 25362 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 25363 ip2dbg(("ip_wput_local: discard broadcast\n")); 25364 return; 25365 } 25366 25367 if (mp->b_datap->db_type == M_DATA) { 25368 /* 25369 * M_DATA mblk, so init mblk (chain) for no struio(). 25370 */ 25371 mblk_t *mp1 = mp; 25372 25373 do 25374 mp1->b_datap->db_struioflag = 0; 25375 while ((mp1 = mp1->b_cont) != NULL); 25376 } 25377 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 25378 <= mp->b_wptr); 25379 ip_fanout_tcp(q, first_mp, ill, ipha, 25380 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25381 IP_FF_SYN_ADDIRE | IP_FF_IPINFO, 25382 mctl_present, B_FALSE, zoneid); 25383 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25384 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 25385 return; 25386 } 25387 case IPPROTO_SCTP: 25388 { 25389 uint32_t ports; 25390 25391 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 25392 ip_fanout_sctp(first_mp, ill, ipha, ports, 25393 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25394 IP_FF_IPINFO, mctl_present, B_FALSE, zoneid); 25395 return; 25396 } 25397 25398 default: 25399 break; 25400 } 25401 /* 25402 * Find a client for some other protocol. We give 25403 * copies to multiple clients, if more than one is 25404 * bound. 25405 */ 25406 ip_fanout_proto(q, first_mp, ill, ipha, 25407 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 25408 mctl_present, B_FALSE, ill, zoneid); 25409 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25410 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 25411 #undef rptr 25412 } 25413 25414 /* 25415 * Update any source route, record route, or timestamp options. 25416 * Check that we are at end of strict source route. 25417 * The options have been sanity checked by ip_wput_options(). 25418 */ 25419 static void 25420 ip_wput_local_options(ipha_t *ipha, ip_stack_t *ipst) 25421 { 25422 ipoptp_t opts; 25423 uchar_t *opt; 25424 uint8_t optval; 25425 uint8_t optlen; 25426 ipaddr_t dst; 25427 uint32_t ts; 25428 ire_t *ire; 25429 timestruc_t now; 25430 25431 ip2dbg(("ip_wput_local_options\n")); 25432 for (optval = ipoptp_first(&opts, ipha); 25433 optval != IPOPT_EOL; 25434 optval = ipoptp_next(&opts)) { 25435 opt = opts.ipoptp_cur; 25436 optlen = opts.ipoptp_len; 25437 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 25438 switch (optval) { 25439 uint32_t off; 25440 case IPOPT_SSRR: 25441 case IPOPT_LSRR: 25442 off = opt[IPOPT_OFFSET]; 25443 off--; 25444 if (optlen < IP_ADDR_LEN || 25445 off > optlen - IP_ADDR_LEN) { 25446 /* End of source route */ 25447 break; 25448 } 25449 /* 25450 * This will only happen if two consecutive entries 25451 * in the source route contains our address or if 25452 * it is a packet with a loose source route which 25453 * reaches us before consuming the whole source route 25454 */ 25455 ip1dbg(("ip_wput_local_options: not end of SR\n")); 25456 if (optval == IPOPT_SSRR) { 25457 return; 25458 } 25459 /* 25460 * Hack: instead of dropping the packet truncate the 25461 * source route to what has been used by filling the 25462 * rest with IPOPT_NOP. 25463 */ 25464 opt[IPOPT_OLEN] = (uint8_t)off; 25465 while (off < optlen) { 25466 opt[off++] = IPOPT_NOP; 25467 } 25468 break; 25469 case IPOPT_RR: 25470 off = opt[IPOPT_OFFSET]; 25471 off--; 25472 if (optlen < IP_ADDR_LEN || 25473 off > optlen - IP_ADDR_LEN) { 25474 /* No more room - ignore */ 25475 ip1dbg(( 25476 "ip_wput_forward_options: end of RR\n")); 25477 break; 25478 } 25479 dst = htonl(INADDR_LOOPBACK); 25480 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25481 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25482 break; 25483 case IPOPT_TS: 25484 /* Insert timestamp if there is romm */ 25485 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25486 case IPOPT_TS_TSONLY: 25487 off = IPOPT_TS_TIMELEN; 25488 break; 25489 case IPOPT_TS_PRESPEC: 25490 case IPOPT_TS_PRESPEC_RFC791: 25491 /* Verify that the address matched */ 25492 off = opt[IPOPT_OFFSET] - 1; 25493 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 25494 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 25495 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 25496 ipst); 25497 if (ire == NULL) { 25498 /* Not for us */ 25499 break; 25500 } 25501 ire_refrele(ire); 25502 /* FALLTHRU */ 25503 case IPOPT_TS_TSANDADDR: 25504 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 25505 break; 25506 default: 25507 /* 25508 * ip_*put_options should have already 25509 * dropped this packet. 25510 */ 25511 cmn_err(CE_PANIC, "ip_wput_local_options: " 25512 "unknown IT - bug in ip_wput_options?\n"); 25513 return; /* Keep "lint" happy */ 25514 } 25515 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 25516 /* Increase overflow counter */ 25517 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 25518 opt[IPOPT_POS_OV_FLG] = (uint8_t) 25519 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 25520 (off << 4); 25521 break; 25522 } 25523 off = opt[IPOPT_OFFSET] - 1; 25524 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25525 case IPOPT_TS_PRESPEC: 25526 case IPOPT_TS_PRESPEC_RFC791: 25527 case IPOPT_TS_TSANDADDR: 25528 dst = htonl(INADDR_LOOPBACK); 25529 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25530 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25531 /* FALLTHRU */ 25532 case IPOPT_TS_TSONLY: 25533 off = opt[IPOPT_OFFSET] - 1; 25534 /* Compute # of milliseconds since midnight */ 25535 gethrestime(&now); 25536 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 25537 now.tv_nsec / (NANOSEC / MILLISEC); 25538 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 25539 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 25540 break; 25541 } 25542 break; 25543 } 25544 } 25545 } 25546 25547 /* 25548 * Send out a multicast packet on interface ipif. 25549 * The sender does not have an conn. 25550 * Caller verifies that this isn't a PHYI_LOOPBACK. 25551 */ 25552 void 25553 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif, zoneid_t zoneid) 25554 { 25555 ipha_t *ipha; 25556 ire_t *ire; 25557 ipaddr_t dst; 25558 mblk_t *first_mp; 25559 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 25560 25561 /* igmp_sendpkt always allocates a ipsec_out_t */ 25562 ASSERT(mp->b_datap->db_type == M_CTL); 25563 ASSERT(!ipif->ipif_isv6); 25564 ASSERT(!(ipif->ipif_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)); 25565 25566 first_mp = mp; 25567 mp = first_mp->b_cont; 25568 ASSERT(mp->b_datap->db_type == M_DATA); 25569 ipha = (ipha_t *)mp->b_rptr; 25570 25571 /* 25572 * Find an IRE which matches the destination and the outgoing 25573 * queue (i.e. the outgoing interface.) 25574 */ 25575 if (ipif->ipif_flags & IPIF_POINTOPOINT) 25576 dst = ipif->ipif_pp_dst_addr; 25577 else 25578 dst = ipha->ipha_dst; 25579 /* 25580 * The source address has already been initialized by the 25581 * caller and hence matching on ILL (MATCH_IRE_ILL) would 25582 * be sufficient rather than MATCH_IRE_IPIF. 25583 * 25584 * This function is used for sending IGMP packets. We need 25585 * to make sure that we send the packet out of the interface 25586 * (ipif->ipif_ill) where we joined the group. This is to 25587 * prevent from switches doing IGMP snooping to send us multicast 25588 * packets for a given group on the interface we have joined. 25589 * If we can't find an ire, igmp_sendpkt has already initialized 25590 * ipsec_out_attach_if so that this will not be load spread in 25591 * ip_newroute_ipif. 25592 */ 25593 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, NULL, 25594 MATCH_IRE_ILL, ipst); 25595 if (!ire) { 25596 /* 25597 * Mark this packet to make it be delivered to 25598 * ip_wput_ire after the new ire has been 25599 * created. 25600 */ 25601 mp->b_prev = NULL; 25602 mp->b_next = NULL; 25603 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC, 25604 zoneid, &zero_info); 25605 return; 25606 } 25607 25608 /* 25609 * Honor the RTF_SETSRC flag; this is the only case 25610 * where we force this addr whatever the current src addr is, 25611 * because this address is set by igmp_sendpkt(), and 25612 * cannot be specified by any user. 25613 */ 25614 if (ire->ire_flags & RTF_SETSRC) { 25615 ipha->ipha_src = ire->ire_src_addr; 25616 } 25617 25618 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE, zoneid); 25619 } 25620 25621 /* 25622 * NOTE : This function does not ire_refrele the ire argument passed in. 25623 * 25624 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 25625 * failure. The nce_fp_mp can vanish any time in the case of IRE_MIPRTUN 25626 * and IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 25627 * the ire_lock to access the nce_fp_mp in this case. 25628 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 25629 * prepending a fastpath message IPQoS processing must precede it, we also set 25630 * the b_band of the fastpath message to that of the mblk returned by IPQoS 25631 * (IPQoS might have set the b_band for CoS marking). 25632 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 25633 * must follow it so that IPQoS can mark the dl_priority field for CoS 25634 * marking, if needed. 25635 */ 25636 static mblk_t * 25637 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, uint32_t ill_index) 25638 { 25639 uint_t hlen; 25640 ipha_t *ipha; 25641 mblk_t *mp1; 25642 boolean_t qos_done = B_FALSE; 25643 uchar_t *ll_hdr; 25644 ip_stack_t *ipst = ire->ire_ipst; 25645 25646 #define rptr ((uchar_t *)ipha) 25647 25648 ipha = (ipha_t *)mp->b_rptr; 25649 hlen = 0; 25650 LOCK_IRE_FP_MP(ire); 25651 if ((mp1 = ire->ire_nce->nce_fp_mp) != NULL) { 25652 ASSERT(DB_TYPE(mp1) == M_DATA); 25653 /* Initiate IPPF processing */ 25654 if ((proc != 0) && IPP_ENABLED(proc, ipst)) { 25655 UNLOCK_IRE_FP_MP(ire); 25656 ip_process(proc, &mp, ill_index); 25657 if (mp == NULL) 25658 return (NULL); 25659 25660 ipha = (ipha_t *)mp->b_rptr; 25661 LOCK_IRE_FP_MP(ire); 25662 if ((mp1 = ire->ire_nce->nce_fp_mp) == NULL) { 25663 qos_done = B_TRUE; 25664 goto no_fp_mp; 25665 } 25666 ASSERT(DB_TYPE(mp1) == M_DATA); 25667 } 25668 hlen = MBLKL(mp1); 25669 /* 25670 * Check if we have enough room to prepend fastpath 25671 * header 25672 */ 25673 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 25674 ll_hdr = rptr - hlen; 25675 bcopy(mp1->b_rptr, ll_hdr, hlen); 25676 /* 25677 * Set the b_rptr to the start of the link layer 25678 * header 25679 */ 25680 mp->b_rptr = ll_hdr; 25681 mp1 = mp; 25682 } else { 25683 mp1 = copyb(mp1); 25684 if (mp1 == NULL) 25685 goto unlock_err; 25686 mp1->b_band = mp->b_band; 25687 mp1->b_cont = mp; 25688 /* 25689 * certain system generated traffic may not 25690 * have cred/label in ip header block. This 25691 * is true even for a labeled system. But for 25692 * labeled traffic, inherit the label in the 25693 * new header. 25694 */ 25695 if (DB_CRED(mp) != NULL) 25696 mblk_setcred(mp1, DB_CRED(mp)); 25697 /* 25698 * XXX disable ICK_VALID and compute checksum 25699 * here; can happen if nce_fp_mp changes and 25700 * it can't be copied now due to insufficient 25701 * space. (unlikely, fp mp can change, but it 25702 * does not increase in length) 25703 */ 25704 } 25705 UNLOCK_IRE_FP_MP(ire); 25706 } else { 25707 no_fp_mp: 25708 mp1 = copyb(ire->ire_nce->nce_res_mp); 25709 if (mp1 == NULL) { 25710 unlock_err: 25711 UNLOCK_IRE_FP_MP(ire); 25712 freemsg(mp); 25713 return (NULL); 25714 } 25715 UNLOCK_IRE_FP_MP(ire); 25716 mp1->b_cont = mp; 25717 /* 25718 * certain system generated traffic may not 25719 * have cred/label in ip header block. This 25720 * is true even for a labeled system. But for 25721 * labeled traffic, inherit the label in the 25722 * new header. 25723 */ 25724 if (DB_CRED(mp) != NULL) 25725 mblk_setcred(mp1, DB_CRED(mp)); 25726 if (!qos_done && (proc != 0) && IPP_ENABLED(proc, ipst)) { 25727 ip_process(proc, &mp1, ill_index); 25728 if (mp1 == NULL) 25729 return (NULL); 25730 } 25731 } 25732 return (mp1); 25733 #undef rptr 25734 } 25735 25736 /* 25737 * Finish the outbound IPsec processing for an IPv6 packet. This function 25738 * is called from ipsec_out_process() if the IPsec packet was processed 25739 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25740 * asynchronously. 25741 */ 25742 void 25743 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 25744 ire_t *ire_arg) 25745 { 25746 in6_addr_t *v6dstp; 25747 ire_t *ire; 25748 mblk_t *mp; 25749 ip6_t *ip6h1; 25750 uint_t ill_index; 25751 ipsec_out_t *io; 25752 boolean_t attach_if, hwaccel; 25753 uint32_t flags = IP6_NO_IPPOLICY; 25754 int match_flags; 25755 zoneid_t zoneid; 25756 boolean_t ill_need_rele = B_FALSE; 25757 boolean_t ire_need_rele = B_FALSE; 25758 ip_stack_t *ipst; 25759 25760 mp = ipsec_mp->b_cont; 25761 ip6h1 = (ip6_t *)mp->b_rptr; 25762 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25763 ASSERT(io->ipsec_out_ns != NULL); 25764 ipst = io->ipsec_out_ns->netstack_ip; 25765 ill_index = io->ipsec_out_ill_index; 25766 if (io->ipsec_out_reachable) { 25767 flags |= IPV6_REACHABILITY_CONFIRMATION; 25768 } 25769 attach_if = io->ipsec_out_attach_if; 25770 hwaccel = io->ipsec_out_accelerated; 25771 zoneid = io->ipsec_out_zoneid; 25772 ASSERT(zoneid != ALL_ZONES); 25773 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25774 /* Multicast addresses should have non-zero ill_index. */ 25775 v6dstp = &ip6h->ip6_dst; 25776 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 25777 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 25778 ASSERT(!attach_if || ill_index != 0); 25779 if (ill_index != 0) { 25780 if (ill == NULL) { 25781 ill = ip_grab_attach_ill(NULL, ipsec_mp, ill_index, 25782 B_TRUE, ipst); 25783 25784 /* Failure case frees things for us. */ 25785 if (ill == NULL) 25786 return; 25787 25788 ill_need_rele = B_TRUE; 25789 } 25790 /* 25791 * If this packet needs to go out on a particular interface 25792 * honor it. 25793 */ 25794 if (attach_if) { 25795 match_flags = MATCH_IRE_ILL; 25796 25797 /* 25798 * Check if we need an ire that will not be 25799 * looked up by anybody else i.e. HIDDEN. 25800 */ 25801 if (ill_is_probeonly(ill)) { 25802 match_flags |= MATCH_IRE_MARK_HIDDEN; 25803 } 25804 } 25805 } 25806 ASSERT(mp != NULL); 25807 25808 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 25809 boolean_t unspec_src; 25810 ipif_t *ipif; 25811 25812 /* 25813 * Use the ill_index to get the right ill. 25814 */ 25815 unspec_src = io->ipsec_out_unspec_src; 25816 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25817 if (ipif == NULL) { 25818 if (ill_need_rele) 25819 ill_refrele(ill); 25820 freemsg(ipsec_mp); 25821 return; 25822 } 25823 25824 if (ire_arg != NULL) { 25825 ire = ire_arg; 25826 } else { 25827 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25828 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25829 ire_need_rele = B_TRUE; 25830 } 25831 if (ire != NULL) { 25832 ipif_refrele(ipif); 25833 /* 25834 * XXX Do the multicast forwarding now, as the IPSEC 25835 * processing has been done. 25836 */ 25837 goto send; 25838 } 25839 25840 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 25841 mp->b_prev = NULL; 25842 mp->b_next = NULL; 25843 25844 /* 25845 * If the IPsec packet was processed asynchronously, 25846 * drop it now. 25847 */ 25848 if (q == NULL) { 25849 if (ill_need_rele) 25850 ill_refrele(ill); 25851 freemsg(ipsec_mp); 25852 return; 25853 } 25854 25855 ip_newroute_ipif_v6(q, ipsec_mp, ipif, *v6dstp, 25856 unspec_src, zoneid); 25857 ipif_refrele(ipif); 25858 } else { 25859 if (attach_if) { 25860 ipif_t *ipif; 25861 25862 ipif = ipif_get_next_ipif(NULL, ill); 25863 if (ipif == NULL) { 25864 if (ill_need_rele) 25865 ill_refrele(ill); 25866 freemsg(ipsec_mp); 25867 return; 25868 } 25869 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25870 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25871 ire_need_rele = B_TRUE; 25872 ipif_refrele(ipif); 25873 } else { 25874 if (ire_arg != NULL) { 25875 ire = ire_arg; 25876 } else { 25877 ire = ire_cache_lookup_v6(v6dstp, zoneid, NULL, 25878 ipst); 25879 ire_need_rele = B_TRUE; 25880 } 25881 } 25882 if (ire != NULL) 25883 goto send; 25884 /* 25885 * ire disappeared underneath. 25886 * 25887 * What we need to do here is the ip_newroute 25888 * logic to get the ire without doing the IPSEC 25889 * processing. Follow the same old path. But this 25890 * time, ip_wput or ire_add_then_send will call us 25891 * directly as all the IPSEC operations are done. 25892 */ 25893 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 25894 mp->b_prev = NULL; 25895 mp->b_next = NULL; 25896 25897 /* 25898 * If the IPsec packet was processed asynchronously, 25899 * drop it now. 25900 */ 25901 if (q == NULL) { 25902 if (ill_need_rele) 25903 ill_refrele(ill); 25904 freemsg(ipsec_mp); 25905 return; 25906 } 25907 25908 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 25909 zoneid, ipst); 25910 } 25911 if (ill != NULL && ill_need_rele) 25912 ill_refrele(ill); 25913 return; 25914 send: 25915 if (ill != NULL && ill_need_rele) 25916 ill_refrele(ill); 25917 25918 /* Local delivery */ 25919 if (ire->ire_stq == NULL) { 25920 ill_t *out_ill; 25921 ASSERT(q != NULL); 25922 25923 /* PFHooks: LOOPBACK_OUT */ 25924 out_ill = ire->ire_ipif->ipif_ill; 25925 25926 DTRACE_PROBE4(ip6__loopback__out__start, 25927 ill_t *, NULL, ill_t *, out_ill, 25928 ip6_t *, ip6h1, mblk_t *, ipsec_mp); 25929 25930 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 25931 ipst->ips_ipv6firewall_loopback_out, 25932 NULL, out_ill, ip6h1, ipsec_mp, mp, ipst); 25933 25934 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, ipsec_mp); 25935 25936 if (ipsec_mp != NULL) 25937 ip_wput_local_v6(RD(q), out_ill, 25938 ip6h, ipsec_mp, ire, 0); 25939 if (ire_need_rele) 25940 ire_refrele(ire); 25941 return; 25942 } 25943 /* 25944 * Everything is done. Send it out on the wire. 25945 * We force the insertion of a fragment header using the 25946 * IPH_FRAG_HDR flag in two cases: 25947 * - after reception of an ICMPv6 "packet too big" message 25948 * with a MTU < 1280 (cf. RFC 2460 section 5) 25949 * - for multirouted IPv6 packets, so that the receiver can 25950 * discard duplicates according to their fragment identifier 25951 */ 25952 /* XXX fix flow control problems. */ 25953 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 25954 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 25955 if (hwaccel) { 25956 /* 25957 * hardware acceleration does not handle these 25958 * "slow path" cases. 25959 */ 25960 /* IPsec KSTATS: should bump bean counter here. */ 25961 if (ire_need_rele) 25962 ire_refrele(ire); 25963 freemsg(ipsec_mp); 25964 return; 25965 } 25966 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 25967 (mp->b_cont ? msgdsize(mp) : 25968 mp->b_wptr - (uchar_t *)ip6h)) { 25969 /* IPsec KSTATS: should bump bean counter here. */ 25970 ip0dbg(("Packet length mismatch: %d, %ld\n", 25971 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 25972 msgdsize(mp))); 25973 if (ire_need_rele) 25974 ire_refrele(ire); 25975 freemsg(ipsec_mp); 25976 return; 25977 } 25978 ASSERT(mp->b_prev == NULL); 25979 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 25980 ntohs(ip6h->ip6_plen) + 25981 IPV6_HDR_LEN, ire->ire_max_frag)); 25982 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 25983 ire->ire_max_frag); 25984 } else { 25985 UPDATE_OB_PKT_COUNT(ire); 25986 ire->ire_last_used_time = lbolt; 25987 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 25988 } 25989 if (ire_need_rele) 25990 ire_refrele(ire); 25991 freeb(ipsec_mp); 25992 } 25993 25994 void 25995 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 25996 { 25997 mblk_t *hada_mp; /* attributes M_CTL mblk */ 25998 da_ipsec_t *hada; /* data attributes */ 25999 ill_t *ill = (ill_t *)q->q_ptr; 26000 26001 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 26002 26003 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 26004 /* IPsec KSTATS: Bump lose counter here! */ 26005 freemsg(mp); 26006 return; 26007 } 26008 26009 /* 26010 * It's an IPsec packet that must be 26011 * accelerated by the Provider, and the 26012 * outbound ill is IPsec acceleration capable. 26013 * Prepends the mblk with an IPHADA_M_CTL, and ship it 26014 * to the ill. 26015 * IPsec KSTATS: should bump packet counter here. 26016 */ 26017 26018 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 26019 if (hada_mp == NULL) { 26020 /* IPsec KSTATS: should bump packet counter here. */ 26021 freemsg(mp); 26022 return; 26023 } 26024 26025 hada_mp->b_datap->db_type = M_CTL; 26026 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 26027 hada_mp->b_cont = mp; 26028 26029 hada = (da_ipsec_t *)hada_mp->b_rptr; 26030 bzero(hada, sizeof (da_ipsec_t)); 26031 hada->da_type = IPHADA_M_CTL; 26032 26033 putnext(q, hada_mp); 26034 } 26035 26036 /* 26037 * Finish the outbound IPsec processing. This function is called from 26038 * ipsec_out_process() if the IPsec packet was processed 26039 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 26040 * asynchronously. 26041 */ 26042 void 26043 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 26044 ire_t *ire_arg) 26045 { 26046 uint32_t v_hlen_tos_len; 26047 ipaddr_t dst; 26048 ipif_t *ipif = NULL; 26049 ire_t *ire; 26050 ire_t *ire1 = NULL; 26051 mblk_t *next_mp = NULL; 26052 uint32_t max_frag; 26053 boolean_t multirt_send = B_FALSE; 26054 mblk_t *mp; 26055 mblk_t *mp1; 26056 ipha_t *ipha1; 26057 uint_t ill_index; 26058 ipsec_out_t *io; 26059 boolean_t attach_if; 26060 int match_flags, offset; 26061 irb_t *irb = NULL; 26062 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 26063 zoneid_t zoneid; 26064 uint32_t cksum; 26065 uint16_t *up; 26066 ipxmit_state_t pktxmit_state; 26067 ip_stack_t *ipst; 26068 26069 #ifdef _BIG_ENDIAN 26070 #define LENGTH (v_hlen_tos_len & 0xFFFF) 26071 #else 26072 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 26073 #endif 26074 26075 mp = ipsec_mp->b_cont; 26076 ipha1 = (ipha_t *)mp->b_rptr; 26077 ASSERT(mp != NULL); 26078 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 26079 dst = ipha->ipha_dst; 26080 26081 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26082 ill_index = io->ipsec_out_ill_index; 26083 attach_if = io->ipsec_out_attach_if; 26084 zoneid = io->ipsec_out_zoneid; 26085 ASSERT(zoneid != ALL_ZONES); 26086 ipst = io->ipsec_out_ns->netstack_ip; 26087 ASSERT(io->ipsec_out_ns != NULL); 26088 26089 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 26090 if (ill_index != 0) { 26091 if (ill == NULL) { 26092 ill = ip_grab_attach_ill(NULL, ipsec_mp, 26093 ill_index, B_FALSE, ipst); 26094 26095 /* Failure case frees things for us. */ 26096 if (ill == NULL) 26097 return; 26098 26099 ill_need_rele = B_TRUE; 26100 } 26101 /* 26102 * If this packet needs to go out on a particular interface 26103 * honor it. 26104 */ 26105 if (attach_if) { 26106 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 26107 26108 /* 26109 * Check if we need an ire that will not be 26110 * looked up by anybody else i.e. HIDDEN. 26111 */ 26112 if (ill_is_probeonly(ill)) { 26113 match_flags |= MATCH_IRE_MARK_HIDDEN; 26114 } 26115 } 26116 } 26117 26118 if (CLASSD(dst)) { 26119 boolean_t conn_dontroute; 26120 /* 26121 * Use the ill_index to get the right ipif. 26122 */ 26123 conn_dontroute = io->ipsec_out_dontroute; 26124 if (ill_index == 0) 26125 ipif = ipif_lookup_group(dst, zoneid, ipst); 26126 else 26127 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 26128 if (ipif == NULL) { 26129 ip1dbg(("ip_wput_ipsec_out: No ipif for" 26130 " multicast\n")); 26131 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 26132 freemsg(ipsec_mp); 26133 goto done; 26134 } 26135 /* 26136 * ipha_src has already been intialized with the 26137 * value of the ipif in ip_wput. All we need now is 26138 * an ire to send this downstream. 26139 */ 26140 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 26141 MBLK_GETLABEL(mp), match_flags, ipst); 26142 if (ire != NULL) { 26143 ill_t *ill1; 26144 /* 26145 * Do the multicast forwarding now, as the IPSEC 26146 * processing has been done. 26147 */ 26148 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 26149 (ill1 = ire_to_ill(ire))) { 26150 if (ip_mforward(ill1, ipha, mp)) { 26151 freemsg(ipsec_mp); 26152 ip1dbg(("ip_wput_ipsec_out: mforward " 26153 "failed\n")); 26154 ire_refrele(ire); 26155 goto done; 26156 } 26157 } 26158 goto send; 26159 } 26160 26161 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 26162 mp->b_prev = NULL; 26163 mp->b_next = NULL; 26164 26165 /* 26166 * If the IPsec packet was processed asynchronously, 26167 * drop it now. 26168 */ 26169 if (q == NULL) { 26170 freemsg(ipsec_mp); 26171 goto done; 26172 } 26173 26174 /* 26175 * We may be using a wrong ipif to create the ire. 26176 * But it is okay as the source address is assigned 26177 * for the packet already. Next outbound packet would 26178 * create the IRE with the right IPIF in ip_wput. 26179 * 26180 * Also handle RTF_MULTIRT routes. 26181 */ 26182 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT, 26183 zoneid, &zero_info); 26184 } else { 26185 if (attach_if) { 26186 ire = ire_ctable_lookup(dst, 0, 0, ill->ill_ipif, 26187 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 26188 } else { 26189 if (ire_arg != NULL) { 26190 ire = ire_arg; 26191 ire_need_rele = B_FALSE; 26192 } else { 26193 ire = ire_cache_lookup(dst, zoneid, 26194 MBLK_GETLABEL(mp), ipst); 26195 } 26196 } 26197 if (ire != NULL) { 26198 goto send; 26199 } 26200 26201 /* 26202 * ire disappeared underneath. 26203 * 26204 * What we need to do here is the ip_newroute 26205 * logic to get the ire without doing the IPSEC 26206 * processing. Follow the same old path. But this 26207 * time, ip_wput or ire_add_then_put will call us 26208 * directly as all the IPSEC operations are done. 26209 */ 26210 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 26211 mp->b_prev = NULL; 26212 mp->b_next = NULL; 26213 26214 /* 26215 * If the IPsec packet was processed asynchronously, 26216 * drop it now. 26217 */ 26218 if (q == NULL) { 26219 freemsg(ipsec_mp); 26220 goto done; 26221 } 26222 26223 /* 26224 * Since we're going through ip_newroute() again, we 26225 * need to make sure we don't: 26226 * 26227 * 1.) Trigger the ASSERT() with the ipha_ident 26228 * overloading. 26229 * 2.) Redo transport-layer checksumming, since we've 26230 * already done all that to get this far. 26231 * 26232 * The easiest way not do either of the above is to set 26233 * the ipha_ident field to IP_HDR_INCLUDED. 26234 */ 26235 ipha->ipha_ident = IP_HDR_INCLUDED; 26236 ip_newroute(q, ipsec_mp, dst, NULL, 26237 (CONN_Q(q) ? Q_TO_CONN(q) : NULL), zoneid, ipst); 26238 } 26239 goto done; 26240 send: 26241 if (ipha->ipha_protocol == IPPROTO_UDP && 26242 udp_compute_checksum(ipst->ips_netstack)) { 26243 /* 26244 * ESP NAT-Traversal packet. 26245 * 26246 * Just do software checksum for now. 26247 */ 26248 26249 offset = IP_SIMPLE_HDR_LENGTH + UDP_CHECKSUM_OFFSET; 26250 IP_STAT(ipst, ip_out_sw_cksum); 26251 IP_STAT_UPDATE(ipst, ip_udp_out_sw_cksum_bytes, 26252 ntohs(htons(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH)); 26253 #define iphs ((uint16_t *)ipha) 26254 cksum = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 26255 iphs[9] + ntohs(htons(ipha->ipha_length) - 26256 IP_SIMPLE_HDR_LENGTH); 26257 #undef iphs 26258 if ((cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH, cksum)) == 0) 26259 cksum = 0xFFFF; 26260 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) 26261 if (mp1->b_wptr - mp1->b_rptr >= 26262 offset + sizeof (uint16_t)) { 26263 up = (uint16_t *)(mp1->b_rptr + offset); 26264 *up = cksum; 26265 break; /* out of for loop */ 26266 } else { 26267 offset -= (mp->b_wptr - mp->b_rptr); 26268 } 26269 } /* Otherwise, just keep the all-zero checksum. */ 26270 26271 if (ire->ire_stq == NULL) { 26272 ill_t *out_ill; 26273 /* 26274 * Loopbacks go through ip_wput_local except for one case. 26275 * We come here if we generate a icmp_frag_needed message 26276 * after IPSEC processing is over. When this function calls 26277 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 26278 * icmp_frag_needed. The message generated comes back here 26279 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 26280 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 26281 * source address as it is usually set in ip_wput_ire. As 26282 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 26283 * and we end up here. We can't enter ip_wput_ire once the 26284 * IPSEC processing is over and hence we need to do it here. 26285 */ 26286 ASSERT(q != NULL); 26287 UPDATE_OB_PKT_COUNT(ire); 26288 ire->ire_last_used_time = lbolt; 26289 if (ipha->ipha_src == 0) 26290 ipha->ipha_src = ire->ire_src_addr; 26291 26292 /* PFHooks: LOOPBACK_OUT */ 26293 out_ill = ire->ire_ipif->ipif_ill; 26294 26295 DTRACE_PROBE4(ip4__loopback__out__start, 26296 ill_t *, NULL, ill_t *, out_ill, 26297 ipha_t *, ipha1, mblk_t *, ipsec_mp); 26298 26299 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 26300 ipst->ips_ipv4firewall_loopback_out, 26301 NULL, out_ill, ipha1, ipsec_mp, mp, ipst); 26302 26303 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp); 26304 26305 if (ipsec_mp != NULL) 26306 ip_wput_local(RD(q), out_ill, 26307 ipha, ipsec_mp, ire, 0, zoneid); 26308 if (ire_need_rele) 26309 ire_refrele(ire); 26310 goto done; 26311 } 26312 26313 if (ire->ire_max_frag < (unsigned int)LENGTH) { 26314 /* 26315 * We are through with IPSEC processing. 26316 * Fragment this and send it on the wire. 26317 */ 26318 if (io->ipsec_out_accelerated) { 26319 /* 26320 * The packet has been accelerated but must 26321 * be fragmented. This should not happen 26322 * since AH and ESP must not accelerate 26323 * packets that need fragmentation, however 26324 * the configuration could have changed 26325 * since the AH or ESP processing. 26326 * Drop packet. 26327 * IPsec KSTATS: bump bean counter here. 26328 */ 26329 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 26330 "fragmented accelerated packet!\n")); 26331 freemsg(ipsec_mp); 26332 } else { 26333 ip_wput_ire_fragmentit(ipsec_mp, ire, zoneid, ipst); 26334 } 26335 if (ire_need_rele) 26336 ire_refrele(ire); 26337 goto done; 26338 } 26339 26340 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 26341 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 26342 (void *)ire->ire_ipif, (void *)ipif)); 26343 26344 /* 26345 * Multiroute the secured packet, unless IPsec really 26346 * requires the packet to go out only through a particular 26347 * interface. 26348 */ 26349 if ((ire->ire_flags & RTF_MULTIRT) && !attach_if) { 26350 ire_t *first_ire; 26351 irb = ire->ire_bucket; 26352 ASSERT(irb != NULL); 26353 /* 26354 * This ire has been looked up as the one that 26355 * goes through the given ipif; 26356 * make sure we do not omit any other multiroute ire 26357 * that may be present in the bucket before this one. 26358 */ 26359 IRB_REFHOLD(irb); 26360 for (first_ire = irb->irb_ire; 26361 first_ire != NULL; 26362 first_ire = first_ire->ire_next) { 26363 if ((first_ire->ire_flags & RTF_MULTIRT) && 26364 (first_ire->ire_addr == ire->ire_addr) && 26365 !(first_ire->ire_marks & 26366 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 26367 break; 26368 } 26369 26370 if ((first_ire != NULL) && (first_ire != ire)) { 26371 /* 26372 * Don't change the ire if the packet must 26373 * be fragmented if sent via this new one. 26374 */ 26375 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 26376 IRE_REFHOLD(first_ire); 26377 if (ire_need_rele) 26378 ire_refrele(ire); 26379 else 26380 ire_need_rele = B_TRUE; 26381 ire = first_ire; 26382 } 26383 } 26384 IRB_REFRELE(irb); 26385 26386 multirt_send = B_TRUE; 26387 max_frag = ire->ire_max_frag; 26388 } else { 26389 if ((ire->ire_flags & RTF_MULTIRT) && attach_if) { 26390 ip1dbg(("ip_wput_ipsec_out: ignoring multirouting " 26391 "flag, attach_if %d\n", attach_if)); 26392 } 26393 } 26394 26395 /* 26396 * In most cases, the emission loop below is entered only once. 26397 * Only in the case where the ire holds the RTF_MULTIRT 26398 * flag, we loop to process all RTF_MULTIRT ires in the 26399 * bucket, and send the packet through all crossed 26400 * RTF_MULTIRT routes. 26401 */ 26402 do { 26403 if (multirt_send) { 26404 /* 26405 * ire1 holds here the next ire to process in the 26406 * bucket. If multirouting is expected, 26407 * any non-RTF_MULTIRT ire that has the 26408 * right destination address is ignored. 26409 */ 26410 ASSERT(irb != NULL); 26411 IRB_REFHOLD(irb); 26412 for (ire1 = ire->ire_next; 26413 ire1 != NULL; 26414 ire1 = ire1->ire_next) { 26415 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 26416 continue; 26417 if (ire1->ire_addr != ire->ire_addr) 26418 continue; 26419 if (ire1->ire_marks & 26420 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 26421 continue; 26422 /* No loopback here */ 26423 if (ire1->ire_stq == NULL) 26424 continue; 26425 /* 26426 * Ensure we do not exceed the MTU 26427 * of the next route. 26428 */ 26429 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 26430 ip_multirt_bad_mtu(ire1, max_frag); 26431 continue; 26432 } 26433 26434 IRE_REFHOLD(ire1); 26435 break; 26436 } 26437 IRB_REFRELE(irb); 26438 if (ire1 != NULL) { 26439 /* 26440 * We are in a multiple send case, need to 26441 * make a copy of the packet. 26442 */ 26443 next_mp = copymsg(ipsec_mp); 26444 if (next_mp == NULL) { 26445 ire_refrele(ire1); 26446 ire1 = NULL; 26447 } 26448 } 26449 } 26450 /* 26451 * Everything is done. Send it out on the wire 26452 * 26453 * ip_xmit_v4 will call ip_wput_attach_llhdr and then 26454 * either send it on the wire or, in the case of 26455 * HW acceleration, call ipsec_hw_putnext. 26456 */ 26457 if (ire->ire_nce && 26458 ire->ire_nce->nce_state != ND_REACHABLE) { 26459 DTRACE_PROBE2(ip__wput__ipsec__bail, 26460 (ire_t *), ire, (mblk_t *), ipsec_mp); 26461 /* 26462 * If ire's link-layer is unresolved (this 26463 * would only happen if the incomplete ire 26464 * was added to cachetable via forwarding path) 26465 * don't bother going to ip_xmit_v4. Just drop the 26466 * packet. 26467 * There is a slight risk here, in that, if we 26468 * have the forwarding path create an incomplete 26469 * IRE, then until the IRE is completed, any 26470 * transmitted IPSEC packets will be dropped 26471 * instead of being queued waiting for resolution. 26472 * 26473 * But the likelihood of a forwarding packet and a wput 26474 * packet sending to the same dst at the same time 26475 * and there not yet be an ARP entry for it is small. 26476 * Furthermore, if this actually happens, it might 26477 * be likely that wput would generate multiple 26478 * packets (and forwarding would also have a train 26479 * of packets) for that destination. If this is 26480 * the case, some of them would have been dropped 26481 * anyway, since ARP only queues a few packets while 26482 * waiting for resolution 26483 * 26484 * NOTE: We should really call ip_xmit_v4, 26485 * and let it queue the packet and send the 26486 * ARP query and have ARP come back thus: 26487 * <ARP> ip_wput->ip_output->ip-wput_nondata-> 26488 * ip_xmit_v4->ip_wput_attach_llhdr + ipsec 26489 * hw accel work. But it's too complex to get 26490 * the IPsec hw acceleration approach to fit 26491 * well with ip_xmit_v4 doing ARP without 26492 * doing IPSEC simplification. For now, we just 26493 * poke ip_xmit_v4 to trigger the arp resolve, so 26494 * that we can continue with the send on the next 26495 * attempt. 26496 * 26497 * XXX THis should be revisited, when 26498 * the IPsec/IP interaction is cleaned up 26499 */ 26500 ip1dbg(("ip_wput_ipsec_out: ire is incomplete" 26501 " - dropping packet\n")); 26502 freemsg(ipsec_mp); 26503 /* 26504 * Call ip_xmit_v4() to trigger ARP query 26505 * in case the nce_state is ND_INITIAL 26506 */ 26507 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 26508 goto drop_pkt; 26509 } 26510 26511 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 26512 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha1, 26513 mblk_t *, mp); 26514 FW_HOOKS(ipst->ips_ip4_physical_out_event, 26515 ipst->ips_ipv4firewall_physical_out, 26516 NULL, ire->ire_ipif->ipif_ill, ipha1, mp, mp, ipst); 26517 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 26518 if (mp == NULL) 26519 goto drop_pkt; 26520 26521 ip1dbg(("ip_wput_ipsec_out: calling ip_xmit_v4\n")); 26522 pktxmit_state = ip_xmit_v4(mp, ire, 26523 (io->ipsec_out_accelerated ? io : NULL), B_FALSE); 26524 26525 if ((pktxmit_state == SEND_FAILED) || 26526 (pktxmit_state == LLHDR_RESLV_FAILED)) { 26527 26528 freeb(ipsec_mp); /* ip_xmit_v4 frees the mp */ 26529 drop_pkt: 26530 BUMP_MIB(((ill_t *)ire->ire_stq->q_ptr)->ill_ip_mib, 26531 ipIfStatsOutDiscards); 26532 if (ire_need_rele) 26533 ire_refrele(ire); 26534 if (ire1 != NULL) { 26535 ire_refrele(ire1); 26536 freemsg(next_mp); 26537 } 26538 goto done; 26539 } 26540 26541 freeb(ipsec_mp); 26542 if (ire_need_rele) 26543 ire_refrele(ire); 26544 26545 if (ire1 != NULL) { 26546 ire = ire1; 26547 ire_need_rele = B_TRUE; 26548 ASSERT(next_mp); 26549 ipsec_mp = next_mp; 26550 mp = ipsec_mp->b_cont; 26551 ire1 = NULL; 26552 next_mp = NULL; 26553 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26554 } else { 26555 multirt_send = B_FALSE; 26556 } 26557 } while (multirt_send); 26558 done: 26559 if (ill != NULL && ill_need_rele) 26560 ill_refrele(ill); 26561 if (ipif != NULL) 26562 ipif_refrele(ipif); 26563 } 26564 26565 /* 26566 * Get the ill corresponding to the specified ire, and compare its 26567 * capabilities with the protocol and algorithms specified by the 26568 * the SA obtained from ipsec_out. If they match, annotate the 26569 * ipsec_out structure to indicate that the packet needs acceleration. 26570 * 26571 * 26572 * A packet is eligible for outbound hardware acceleration if the 26573 * following conditions are satisfied: 26574 * 26575 * 1. the packet will not be fragmented 26576 * 2. the provider supports the algorithm 26577 * 3. there is no pending control message being exchanged 26578 * 4. snoop is not attached 26579 * 5. the destination address is not a broadcast or multicast address. 26580 * 26581 * Rationale: 26582 * - Hardware drivers do not support fragmentation with 26583 * the current interface. 26584 * - snoop, multicast, and broadcast may result in exposure of 26585 * a cleartext datagram. 26586 * We check all five of these conditions here. 26587 * 26588 * XXX would like to nuke "ire_t *" parameter here; problem is that 26589 * IRE is only way to figure out if a v4 address is a broadcast and 26590 * thus ineligible for acceleration... 26591 */ 26592 static void 26593 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 26594 { 26595 ipsec_out_t *io; 26596 mblk_t *data_mp; 26597 uint_t plen, overhead; 26598 ip_stack_t *ipst; 26599 26600 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 26601 return; 26602 26603 if (ill == NULL) 26604 return; 26605 ipst = ill->ill_ipst; 26606 /* 26607 * Destination address is a broadcast or multicast. Punt. 26608 */ 26609 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 26610 IRE_LOCAL))) 26611 return; 26612 26613 data_mp = ipsec_mp->b_cont; 26614 26615 if (ill->ill_isv6) { 26616 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 26617 26618 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 26619 return; 26620 26621 plen = ip6h->ip6_plen; 26622 } else { 26623 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 26624 26625 if (CLASSD(ipha->ipha_dst)) 26626 return; 26627 26628 plen = ipha->ipha_length; 26629 } 26630 /* 26631 * Is there a pending DLPI control message being exchanged 26632 * between IP/IPsec and the DLS Provider? If there is, it 26633 * could be a SADB update, and the state of the DLS Provider 26634 * SADB might not be in sync with the SADB maintained by 26635 * IPsec. To avoid dropping packets or using the wrong keying 26636 * material, we do not accelerate this packet. 26637 */ 26638 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 26639 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26640 "ill_dlpi_pending! don't accelerate packet\n")); 26641 return; 26642 } 26643 26644 /* 26645 * Is the Provider in promiscous mode? If it does, we don't 26646 * accelerate the packet since it will bounce back up to the 26647 * listeners in the clear. 26648 */ 26649 if (ill->ill_promisc_on_phys) { 26650 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26651 "ill in promiscous mode, don't accelerate packet\n")); 26652 return; 26653 } 26654 26655 /* 26656 * Will the packet require fragmentation? 26657 */ 26658 26659 /* 26660 * IPsec ESP note: this is a pessimistic estimate, but the same 26661 * as is used elsewhere. 26662 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 26663 * + 2-byte trailer 26664 */ 26665 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 26666 IPSEC_BASE_ESP_HDR_SIZE(sa); 26667 26668 if ((plen + overhead) > ill->ill_max_mtu) 26669 return; 26670 26671 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26672 26673 /* 26674 * Can the ill accelerate this IPsec protocol and algorithm 26675 * specified by the SA? 26676 */ 26677 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 26678 ill->ill_isv6, sa, ipst->ips_netstack)) { 26679 return; 26680 } 26681 26682 /* 26683 * Tell AH or ESP that the outbound ill is capable of 26684 * accelerating this packet. 26685 */ 26686 io->ipsec_out_is_capab_ill = B_TRUE; 26687 } 26688 26689 /* 26690 * Select which AH & ESP SA's to use (if any) for the outbound packet. 26691 * 26692 * If this function returns B_TRUE, the requested SA's have been filled 26693 * into the ipsec_out_*_sa pointers. 26694 * 26695 * If the function returns B_FALSE, the packet has been "consumed", most 26696 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 26697 * 26698 * The SA references created by the protocol-specific "select" 26699 * function will be released when the ipsec_mp is freed, thanks to the 26700 * ipsec_out_free destructor -- see spd.c. 26701 */ 26702 static boolean_t 26703 ipsec_out_select_sa(mblk_t *ipsec_mp) 26704 { 26705 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 26706 ipsec_out_t *io; 26707 ipsec_policy_t *pp; 26708 ipsec_action_t *ap; 26709 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26710 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26711 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26712 26713 if (!io->ipsec_out_secure) { 26714 /* 26715 * We came here by mistake. 26716 * Don't bother with ipsec processing 26717 * We should "discourage" this path in the future. 26718 */ 26719 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26720 return (B_FALSE); 26721 } 26722 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26723 ASSERT((io->ipsec_out_policy != NULL) || 26724 (io->ipsec_out_act != NULL)); 26725 26726 ASSERT(io->ipsec_out_failed == B_FALSE); 26727 26728 /* 26729 * IPSEC processing has started. 26730 */ 26731 io->ipsec_out_proc_begin = B_TRUE; 26732 ap = io->ipsec_out_act; 26733 if (ap == NULL) { 26734 pp = io->ipsec_out_policy; 26735 ASSERT(pp != NULL); 26736 ap = pp->ipsp_act; 26737 ASSERT(ap != NULL); 26738 } 26739 26740 /* 26741 * We have an action. now, let's select SA's. 26742 * (In the future, we can cache this in the conn_t..) 26743 */ 26744 if (ap->ipa_want_esp) { 26745 if (io->ipsec_out_esp_sa == NULL) { 26746 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 26747 IPPROTO_ESP); 26748 } 26749 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 26750 } 26751 26752 if (ap->ipa_want_ah) { 26753 if (io->ipsec_out_ah_sa == NULL) { 26754 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 26755 IPPROTO_AH); 26756 } 26757 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 26758 /* 26759 * The ESP and AH processing order needs to be preserved 26760 * when both protocols are required (ESP should be applied 26761 * before AH for an outbound packet). Force an ESP ACQUIRE 26762 * when both ESP and AH are required, and an AH ACQUIRE 26763 * is needed. 26764 */ 26765 if (ap->ipa_want_esp && need_ah_acquire) 26766 need_esp_acquire = B_TRUE; 26767 } 26768 26769 /* 26770 * Send an ACQUIRE (extended, regular, or both) if we need one. 26771 * Release SAs that got referenced, but will not be used until we 26772 * acquire _all_ of the SAs we need. 26773 */ 26774 if (need_ah_acquire || need_esp_acquire) { 26775 if (io->ipsec_out_ah_sa != NULL) { 26776 IPSA_REFRELE(io->ipsec_out_ah_sa); 26777 io->ipsec_out_ah_sa = NULL; 26778 } 26779 if (io->ipsec_out_esp_sa != NULL) { 26780 IPSA_REFRELE(io->ipsec_out_esp_sa); 26781 io->ipsec_out_esp_sa = NULL; 26782 } 26783 26784 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 26785 return (B_FALSE); 26786 } 26787 26788 return (B_TRUE); 26789 } 26790 26791 /* 26792 * Process an IPSEC_OUT message and see what you can 26793 * do with it. 26794 * IPQoS Notes: 26795 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 26796 * IPSec. 26797 * XXX would like to nuke ire_t. 26798 * XXX ill_index better be "real" 26799 */ 26800 void 26801 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 26802 { 26803 ipsec_out_t *io; 26804 ipsec_policy_t *pp; 26805 ipsec_action_t *ap; 26806 ipha_t *ipha; 26807 ip6_t *ip6h; 26808 mblk_t *mp; 26809 ill_t *ill; 26810 zoneid_t zoneid; 26811 ipsec_status_t ipsec_rc; 26812 boolean_t ill_need_rele = B_FALSE; 26813 ip_stack_t *ipst; 26814 ipsec_stack_t *ipss; 26815 26816 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26817 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26818 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26819 ipst = io->ipsec_out_ns->netstack_ip; 26820 mp = ipsec_mp->b_cont; 26821 26822 /* 26823 * Initiate IPPF processing. We do it here to account for packets 26824 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 26825 * We can check for ipsec_out_proc_begin even for such packets, as 26826 * they will always be false (asserted below). 26827 */ 26828 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst) && !io->ipsec_out_proc_begin) { 26829 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 26830 io->ipsec_out_ill_index : ill_index); 26831 if (mp == NULL) { 26832 ip2dbg(("ipsec_out_process: packet dropped "\ 26833 "during IPPF processing\n")); 26834 freeb(ipsec_mp); 26835 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26836 return; 26837 } 26838 } 26839 26840 if (!io->ipsec_out_secure) { 26841 /* 26842 * We came here by mistake. 26843 * Don't bother with ipsec processing 26844 * Should "discourage" this path in the future. 26845 */ 26846 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26847 goto done; 26848 } 26849 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26850 ASSERT((io->ipsec_out_policy != NULL) || 26851 (io->ipsec_out_act != NULL)); 26852 ASSERT(io->ipsec_out_failed == B_FALSE); 26853 26854 ipss = ipst->ips_netstack->netstack_ipsec; 26855 if (!ipsec_loaded(ipss)) { 26856 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 26857 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26858 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26859 } else { 26860 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); 26861 } 26862 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 26863 DROPPER(ipss, ipds_ip_ipsec_not_loaded), 26864 &ipss->ipsec_dropper); 26865 return; 26866 } 26867 26868 /* 26869 * IPSEC processing has started. 26870 */ 26871 io->ipsec_out_proc_begin = B_TRUE; 26872 ap = io->ipsec_out_act; 26873 if (ap == NULL) { 26874 pp = io->ipsec_out_policy; 26875 ASSERT(pp != NULL); 26876 ap = pp->ipsp_act; 26877 ASSERT(ap != NULL); 26878 } 26879 26880 /* 26881 * Save the outbound ill index. When the packet comes back 26882 * from IPsec, we make sure the ill hasn't changed or disappeared 26883 * before sending it the accelerated packet. 26884 */ 26885 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 26886 int ifindex; 26887 ill = ire_to_ill(ire); 26888 ifindex = ill->ill_phyint->phyint_ifindex; 26889 io->ipsec_out_capab_ill_index = ifindex; 26890 } 26891 26892 /* 26893 * The order of processing is first insert a IP header if needed. 26894 * Then insert the ESP header and then the AH header. 26895 */ 26896 if ((io->ipsec_out_se_done == B_FALSE) && 26897 (ap->ipa_want_se)) { 26898 /* 26899 * First get the outer IP header before sending 26900 * it to ESP. 26901 */ 26902 ipha_t *oipha, *iipha; 26903 mblk_t *outer_mp, *inner_mp; 26904 26905 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 26906 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 26907 "ipsec_out_process: " 26908 "Self-Encapsulation failed: Out of memory\n"); 26909 freemsg(ipsec_mp); 26910 if (ill != NULL) { 26911 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26912 } else { 26913 BUMP_MIB(&ipst->ips_ip_mib, 26914 ipIfStatsOutDiscards); 26915 } 26916 return; 26917 } 26918 inner_mp = ipsec_mp->b_cont; 26919 ASSERT(inner_mp->b_datap->db_type == M_DATA); 26920 oipha = (ipha_t *)outer_mp->b_rptr; 26921 iipha = (ipha_t *)inner_mp->b_rptr; 26922 *oipha = *iipha; 26923 outer_mp->b_wptr += sizeof (ipha_t); 26924 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 26925 sizeof (ipha_t)); 26926 oipha->ipha_protocol = IPPROTO_ENCAP; 26927 oipha->ipha_version_and_hdr_length = 26928 IP_SIMPLE_HDR_VERSION; 26929 oipha->ipha_hdr_checksum = 0; 26930 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 26931 outer_mp->b_cont = inner_mp; 26932 ipsec_mp->b_cont = outer_mp; 26933 26934 io->ipsec_out_se_done = B_TRUE; 26935 io->ipsec_out_tunnel = B_TRUE; 26936 } 26937 26938 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 26939 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 26940 !ipsec_out_select_sa(ipsec_mp)) 26941 return; 26942 26943 /* 26944 * By now, we know what SA's to use. Toss over to ESP & AH 26945 * to do the heavy lifting. 26946 */ 26947 zoneid = io->ipsec_out_zoneid; 26948 ASSERT(zoneid != ALL_ZONES); 26949 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 26950 ASSERT(io->ipsec_out_esp_sa != NULL); 26951 io->ipsec_out_esp_done = B_TRUE; 26952 /* 26953 * Note that since hw accel can only apply one transform, 26954 * not two, we skip hw accel for ESP if we also have AH 26955 * This is an design limitation of the interface 26956 * which should be revisited. 26957 */ 26958 ASSERT(ire != NULL); 26959 if (io->ipsec_out_ah_sa == NULL) { 26960 ill = (ill_t *)ire->ire_stq->q_ptr; 26961 ipsec_out_is_accelerated(ipsec_mp, 26962 io->ipsec_out_esp_sa, ill, ire); 26963 } 26964 26965 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 26966 switch (ipsec_rc) { 26967 case IPSEC_STATUS_SUCCESS: 26968 break; 26969 case IPSEC_STATUS_FAILED: 26970 if (ill != NULL) { 26971 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26972 } else { 26973 BUMP_MIB(&ipst->ips_ip_mib, 26974 ipIfStatsOutDiscards); 26975 } 26976 /* FALLTHRU */ 26977 case IPSEC_STATUS_PENDING: 26978 return; 26979 } 26980 } 26981 26982 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 26983 ASSERT(io->ipsec_out_ah_sa != NULL); 26984 io->ipsec_out_ah_done = B_TRUE; 26985 if (ire == NULL) { 26986 int idx = io->ipsec_out_capab_ill_index; 26987 ill = ill_lookup_on_ifindex(idx, B_FALSE, 26988 NULL, NULL, NULL, NULL, ipst); 26989 ill_need_rele = B_TRUE; 26990 } else { 26991 ill = (ill_t *)ire->ire_stq->q_ptr; 26992 } 26993 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 26994 ire); 26995 26996 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 26997 switch (ipsec_rc) { 26998 case IPSEC_STATUS_SUCCESS: 26999 break; 27000 case IPSEC_STATUS_FAILED: 27001 if (ill != NULL) { 27002 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 27003 } else { 27004 BUMP_MIB(&ipst->ips_ip_mib, 27005 ipIfStatsOutDiscards); 27006 } 27007 /* FALLTHRU */ 27008 case IPSEC_STATUS_PENDING: 27009 if (ill != NULL && ill_need_rele) 27010 ill_refrele(ill); 27011 return; 27012 } 27013 } 27014 /* 27015 * We are done with IPSEC processing. Send it over 27016 * the wire. 27017 */ 27018 done: 27019 mp = ipsec_mp->b_cont; 27020 ipha = (ipha_t *)mp->b_rptr; 27021 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 27022 ip_wput_ipsec_out(q, ipsec_mp, ipha, ill, ire); 27023 } else { 27024 ip6h = (ip6_t *)ipha; 27025 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ill, ire); 27026 } 27027 if (ill != NULL && ill_need_rele) 27028 ill_refrele(ill); 27029 } 27030 27031 /* ARGSUSED */ 27032 void 27033 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 27034 { 27035 opt_restart_t *or; 27036 int err; 27037 conn_t *connp; 27038 27039 ASSERT(CONN_Q(q)); 27040 connp = Q_TO_CONN(q); 27041 27042 ASSERT(first_mp->b_datap->db_type == M_CTL); 27043 or = (opt_restart_t *)first_mp->b_rptr; 27044 /* 27045 * We don't need to pass any credentials here since this is just 27046 * a restart. The credentials are passed in when svr4_optcom_req 27047 * is called the first time (from ip_wput_nondata). 27048 */ 27049 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 27050 err = svr4_optcom_req(q, first_mp, NULL, 27051 &ip_opt_obj); 27052 } else { 27053 ASSERT(or->or_type == T_OPTMGMT_REQ); 27054 err = tpi_optcom_req(q, first_mp, NULL, 27055 &ip_opt_obj); 27056 } 27057 if (err != EINPROGRESS) { 27058 /* operation is done */ 27059 CONN_OPER_PENDING_DONE(connp); 27060 } 27061 } 27062 27063 /* 27064 * ioctls that go through a down/up sequence may need to wait for the down 27065 * to complete. This involves waiting for the ire and ipif refcnts to go down 27066 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 27067 */ 27068 /* ARGSUSED */ 27069 void 27070 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 27071 { 27072 struct iocblk *iocp; 27073 mblk_t *mp1; 27074 ip_ioctl_cmd_t *ipip; 27075 int err; 27076 sin_t *sin; 27077 struct lifreq *lifr; 27078 struct ifreq *ifr; 27079 27080 iocp = (struct iocblk *)mp->b_rptr; 27081 ASSERT(ipsq != NULL); 27082 /* Existence of mp1 verified in ip_wput_nondata */ 27083 mp1 = mp->b_cont->b_cont; 27084 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27085 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 27086 /* 27087 * Special case where ipsq_current_ipif is not set: 27088 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 27089 * ill could also have become part of a ipmp group in the 27090 * process, we are here as were not able to complete the 27091 * operation in ipif_set_values because we could not become 27092 * exclusive on the new ipsq, In such a case ipsq_current_ipif 27093 * will not be set so we need to set it. 27094 */ 27095 ill_t *ill = q->q_ptr; 27096 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd); 27097 } 27098 ASSERT(ipsq->ipsq_current_ipif != NULL); 27099 27100 if (ipip->ipi_cmd_type == IF_CMD) { 27101 /* This a old style SIOC[GS]IF* command */ 27102 ifr = (struct ifreq *)mp1->b_rptr; 27103 sin = (sin_t *)&ifr->ifr_addr; 27104 } else if (ipip->ipi_cmd_type == LIF_CMD) { 27105 /* This a new style SIOC[GS]LIF* command */ 27106 lifr = (struct lifreq *)mp1->b_rptr; 27107 sin = (sin_t *)&lifr->lifr_addr; 27108 } else { 27109 sin = NULL; 27110 } 27111 27112 err = (*ipip->ipi_func_restart)(ipsq->ipsq_current_ipif, sin, q, mp, 27113 ipip, mp1->b_rptr); 27114 27115 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 27116 } 27117 27118 /* 27119 * ioctl processing 27120 * 27121 * ioctl processing starts with ip_sioctl_copyin_setup which looks up 27122 * the ioctl command in the ioctl tables and determines the copyin data size 27123 * from the ioctl property ipi_copyin_size, and does an mi_copyin() of that 27124 * size. 27125 * 27126 * ioctl processing then continues when the M_IOCDATA makes its way down. 27127 * Now the ioctl is looked up again in the ioctl table, and its properties are 27128 * extracted. The associated 'conn' is then refheld till the end of the ioctl 27129 * and the general ioctl processing function ip_process_ioctl is called. 27130 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 27131 * so goes thru the serialization primitive ipsq_try_enter. Then the 27132 * appropriate function to handle the ioctl is called based on the entry in 27133 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 27134 * which also refreleases the 'conn' that was refheld at the start of the 27135 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 27136 * ip_extract_lifreq_cmn extracts the interface name from the lifreq/ifreq 27137 * struct and looks up the ipif. ip_extract_tunreq handles the case of tunnel. 27138 * 27139 * Many exclusive ioctls go thru an internal down up sequence as part of 27140 * the operation. For example an attempt to change the IP address of an 27141 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 27142 * does all the cleanup such as deleting all ires that use this address. 27143 * Then we need to wait till all references to the interface go away. 27144 */ 27145 void 27146 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 27147 { 27148 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 27149 ip_ioctl_cmd_t *ipip = (ip_ioctl_cmd_t *)arg; 27150 cmd_info_t ci; 27151 int err; 27152 boolean_t entered_ipsq = B_FALSE; 27153 27154 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 27155 27156 if (ipip == NULL) 27157 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27158 27159 /* 27160 * SIOCLIFADDIF needs to go thru a special path since the 27161 * ill may not exist yet. This happens in the case of lo0 27162 * which is created using this ioctl. 27163 */ 27164 if (ipip->ipi_cmd == SIOCLIFADDIF) { 27165 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 27166 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27167 return; 27168 } 27169 27170 ci.ci_ipif = NULL; 27171 switch (ipip->ipi_cmd_type) { 27172 case IF_CMD: 27173 case LIF_CMD: 27174 /* 27175 * ioctls that pass in a [l]ifreq appear here. 27176 * ip_extract_lifreq_cmn returns a refheld ipif in 27177 * ci.ci_ipif 27178 */ 27179 err = ip_extract_lifreq_cmn(q, mp, ipip->ipi_cmd_type, 27180 ipip->ipi_flags, &ci, ip_process_ioctl); 27181 if (err != 0) { 27182 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27183 return; 27184 } 27185 ASSERT(ci.ci_ipif != NULL); 27186 break; 27187 27188 case TUN_CMD: 27189 /* 27190 * SIOC[GS]TUNPARAM appear here. ip_extract_tunreq returns 27191 * a refheld ipif in ci.ci_ipif 27192 */ 27193 err = ip_extract_tunreq(q, mp, &ci.ci_ipif, ip_process_ioctl); 27194 if (err != 0) { 27195 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27196 return; 27197 } 27198 ASSERT(ci.ci_ipif != NULL); 27199 break; 27200 27201 case MISC_CMD: 27202 /* 27203 * ioctls that neither pass in [l]ifreq or iftun_req come here 27204 * For eg. SIOCGLIFCONF will appear here. 27205 */ 27206 switch (ipip->ipi_cmd) { 27207 case IF_UNITSEL: 27208 /* ioctl comes down the ill */ 27209 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 27210 ipif_refhold(ci.ci_ipif); 27211 break; 27212 case SIOCGMSFILTER: 27213 case SIOCSMSFILTER: 27214 case SIOCGIPMSFILTER: 27215 case SIOCSIPMSFILTER: 27216 err = ip_extract_msfilter(q, mp, &ci.ci_ipif, 27217 ip_process_ioctl); 27218 if (err != 0) { 27219 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), 27220 NULL); 27221 } 27222 break; 27223 } 27224 err = 0; 27225 ci.ci_sin = NULL; 27226 ci.ci_sin6 = NULL; 27227 ci.ci_lifr = NULL; 27228 break; 27229 } 27230 27231 /* 27232 * If ipsq is non-null, we are already being called exclusively 27233 */ 27234 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 27235 if (!(ipip->ipi_flags & IPI_WR)) { 27236 /* 27237 * A return value of EINPROGRESS means the ioctl is 27238 * either queued and waiting for some reason or has 27239 * already completed. 27240 */ 27241 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 27242 ci.ci_lifr); 27243 if (ci.ci_ipif != NULL) 27244 ipif_refrele(ci.ci_ipif); 27245 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27246 return; 27247 } 27248 27249 ASSERT(ci.ci_ipif != NULL); 27250 27251 if (ipsq == NULL) { 27252 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, 27253 ip_process_ioctl, NEW_OP, B_TRUE); 27254 entered_ipsq = B_TRUE; 27255 } 27256 /* 27257 * Release the ipif so that ipif_down and friends that wait for 27258 * references to go away are not misled about the current ipif_refcnt 27259 * values. We are writer so we can access the ipif even after releasing 27260 * the ipif. 27261 */ 27262 ipif_refrele(ci.ci_ipif); 27263 if (ipsq == NULL) 27264 return; 27265 27266 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd); 27267 27268 /* 27269 * For most set ioctls that come here, this serves as a single point 27270 * where we set the IPIF_CHANGING flag. This ensures that there won't 27271 * be any new references to the ipif. This helps functions that go 27272 * through this path and end up trying to wait for the refcnts 27273 * associated with the ipif to go down to zero. Some exceptions are 27274 * Failover, Failback, and Groupname commands that operate on more than 27275 * just the ci.ci_ipif. These commands internally determine the 27276 * set of ipif's they operate on and set and clear the IPIF_CHANGING 27277 * flags on that set. Another exception is the Removeif command that 27278 * sets the IPIF_CONDEMNED flag internally after identifying the right 27279 * ipif to operate on. 27280 */ 27281 mutex_enter(&(ci.ci_ipif)->ipif_ill->ill_lock); 27282 if (ipip->ipi_cmd != SIOCLIFREMOVEIF && 27283 ipip->ipi_cmd != SIOCLIFFAILOVER && 27284 ipip->ipi_cmd != SIOCLIFFAILBACK && 27285 ipip->ipi_cmd != SIOCSLIFGROUPNAME) 27286 (ci.ci_ipif)->ipif_state_flags |= IPIF_CHANGING; 27287 mutex_exit(&(ci.ci_ipif)->ipif_ill->ill_lock); 27288 27289 /* 27290 * A return value of EINPROGRESS means the ioctl is 27291 * either queued and waiting for some reason or has 27292 * already completed. 27293 */ 27294 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr); 27295 27296 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 27297 27298 if (entered_ipsq) 27299 ipsq_exit(ipsq, B_TRUE, B_TRUE); 27300 } 27301 27302 /* 27303 * Complete the ioctl. Typically ioctls use the mi package and need to 27304 * do mi_copyout/mi_copy_done. 27305 */ 27306 void 27307 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq) 27308 { 27309 conn_t *connp = NULL; 27310 27311 if (err == EINPROGRESS) 27312 return; 27313 27314 if (CONN_Q(q)) { 27315 connp = Q_TO_CONN(q); 27316 ASSERT(connp->conn_ref >= 2); 27317 } 27318 27319 switch (mode) { 27320 case COPYOUT: 27321 if (err == 0) 27322 mi_copyout(q, mp); 27323 else 27324 mi_copy_done(q, mp, err); 27325 break; 27326 27327 case NO_COPYOUT: 27328 mi_copy_done(q, mp, err); 27329 break; 27330 27331 default: 27332 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */ 27333 break; 27334 } 27335 27336 /* 27337 * The refhold placed at the start of the ioctl is released here. 27338 */ 27339 if (connp != NULL) 27340 CONN_OPER_PENDING_DONE(connp); 27341 27342 if (ipsq != NULL) 27343 ipsq_current_finish(ipsq); 27344 } 27345 27346 /* 27347 * This is called from ip_wput_nondata to resume a deferred TCP bind. 27348 */ 27349 /* ARGSUSED */ 27350 void 27351 ip_resume_tcp_bind(void *arg, mblk_t *mp, void *arg2) 27352 { 27353 conn_t *connp = arg; 27354 tcp_t *tcp; 27355 27356 ASSERT(connp != NULL && IPCL_IS_TCP(connp) && connp->conn_tcp != NULL); 27357 tcp = connp->conn_tcp; 27358 27359 if (connp->conn_tcp->tcp_state == TCPS_CLOSED) 27360 freemsg(mp); 27361 else 27362 tcp_rput_other(tcp, mp); 27363 CONN_OPER_PENDING_DONE(connp); 27364 } 27365 27366 /* Called from ip_wput for all non data messages */ 27367 /* ARGSUSED */ 27368 void 27369 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 27370 { 27371 mblk_t *mp1; 27372 ire_t *ire, *fake_ire; 27373 ill_t *ill; 27374 struct iocblk *iocp; 27375 ip_ioctl_cmd_t *ipip; 27376 cred_t *cr; 27377 conn_t *connp; 27378 int cmd, err; 27379 nce_t *nce; 27380 ipif_t *ipif; 27381 ip_stack_t *ipst; 27382 char *proto_str; 27383 27384 if (CONN_Q(q)) { 27385 connp = Q_TO_CONN(q); 27386 ipst = connp->conn_netstack->netstack_ip; 27387 } else { 27388 connp = NULL; 27389 ipst = ILLQ_TO_IPST(q); 27390 } 27391 27392 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 27393 27394 /* Check if it is a queue to /dev/sctp. */ 27395 if (connp != NULL && connp->conn_ulp == IPPROTO_SCTP && 27396 connp->conn_rq == NULL) { 27397 sctp_wput(q, mp); 27398 return; 27399 } 27400 27401 switch (DB_TYPE(mp)) { 27402 case M_IOCTL: 27403 /* 27404 * IOCTL processing begins in ip_sioctl_copyin_setup which 27405 * will arrange to copy in associated control structures. 27406 */ 27407 ip_sioctl_copyin_setup(q, mp); 27408 return; 27409 case M_IOCDATA: 27410 /* 27411 * Ensure that this is associated with one of our trans- 27412 * parent ioctls. If it's not ours, discard it if we're 27413 * running as a driver, or pass it on if we're a module. 27414 */ 27415 iocp = (struct iocblk *)mp->b_rptr; 27416 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27417 if (ipip == NULL) { 27418 if (q->q_next == NULL) { 27419 goto nak; 27420 } else { 27421 putnext(q, mp); 27422 } 27423 return; 27424 } else if ((q->q_next != NULL) && 27425 !(ipip->ipi_flags & IPI_MODOK)) { 27426 /* 27427 * the ioctl is one we recognise, but is not 27428 * consumed by IP as a module, pass M_IOCDATA 27429 * for processing downstream, but only for 27430 * common Streams ioctls. 27431 */ 27432 if (ipip->ipi_flags & IPI_PASS_DOWN) { 27433 putnext(q, mp); 27434 return; 27435 } else { 27436 goto nak; 27437 } 27438 } 27439 27440 /* IOCTL continuation following copyin or copyout. */ 27441 if (mi_copy_state(q, mp, NULL) == -1) { 27442 /* 27443 * The copy operation failed. mi_copy_state already 27444 * cleaned up, so we're out of here. 27445 */ 27446 return; 27447 } 27448 /* 27449 * If we just completed a copy in, we become writer and 27450 * continue processing in ip_sioctl_copyin_done. If it 27451 * was a copy out, we call mi_copyout again. If there is 27452 * nothing more to copy out, it will complete the IOCTL. 27453 */ 27454 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 27455 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 27456 mi_copy_done(q, mp, EPROTO); 27457 return; 27458 } 27459 /* 27460 * Check for cases that need more copying. A return 27461 * value of 0 means a second copyin has been started, 27462 * so we return; a return value of 1 means no more 27463 * copying is needed, so we continue. 27464 */ 27465 cmd = iocp->ioc_cmd; 27466 if ((cmd == SIOCGMSFILTER || cmd == SIOCSMSFILTER || 27467 cmd == SIOCGIPMSFILTER || cmd == SIOCSIPMSFILTER) && 27468 MI_COPY_COUNT(mp) == 1) { 27469 if (ip_copyin_msfilter(q, mp) == 0) 27470 return; 27471 } 27472 /* 27473 * Refhold the conn, till the ioctl completes. This is 27474 * needed in case the ioctl ends up in the pending mp 27475 * list. Every mp in the ill_pending_mp list and 27476 * the ipsq_pending_mp must have a refhold on the conn 27477 * to resume processing. The refhold is released when 27478 * the ioctl completes. (normally or abnormally) 27479 * In all cases ip_ioctl_finish is called to finish 27480 * the ioctl. 27481 */ 27482 if (connp != NULL) { 27483 /* This is not a reentry */ 27484 ASSERT(ipsq == NULL); 27485 CONN_INC_REF(connp); 27486 } else { 27487 if (!(ipip->ipi_flags & IPI_MODOK)) { 27488 mi_copy_done(q, mp, EINVAL); 27489 return; 27490 } 27491 } 27492 27493 ip_process_ioctl(ipsq, q, mp, ipip); 27494 27495 } else { 27496 mi_copyout(q, mp); 27497 } 27498 return; 27499 nak: 27500 iocp->ioc_error = EINVAL; 27501 mp->b_datap->db_type = M_IOCNAK; 27502 iocp->ioc_count = 0; 27503 qreply(q, mp); 27504 return; 27505 27506 case M_IOCNAK: 27507 /* 27508 * The only way we could get here is if a resolver didn't like 27509 * an IOCTL we sent it. This shouldn't happen. 27510 */ 27511 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 27512 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 27513 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 27514 freemsg(mp); 27515 return; 27516 case M_IOCACK: 27517 /* /dev/ip shouldn't see this */ 27518 if (CONN_Q(q)) 27519 goto nak; 27520 27521 /* Finish socket ioctls passed through to ARP. */ 27522 ip_sioctl_iocack(q, mp); 27523 return; 27524 case M_FLUSH: 27525 if (*mp->b_rptr & FLUSHW) 27526 flushq(q, FLUSHALL); 27527 if (q->q_next) { 27528 /* 27529 * M_FLUSH is sent up to IP by some drivers during 27530 * unbind. ip_rput has already replied to it. We are 27531 * here for the M_FLUSH that we originated in IP 27532 * before sending the unbind request to the driver. 27533 * Just free it as we don't queue packets in IP 27534 * on the write side of the device instance. 27535 */ 27536 freemsg(mp); 27537 return; 27538 } 27539 if (*mp->b_rptr & FLUSHR) { 27540 *mp->b_rptr &= ~FLUSHW; 27541 qreply(q, mp); 27542 return; 27543 } 27544 freemsg(mp); 27545 return; 27546 case IRE_DB_REQ_TYPE: 27547 if (connp == NULL) { 27548 proto_str = "IRE_DB_REQ_TYPE"; 27549 goto protonak; 27550 } 27551 /* An Upper Level Protocol wants a copy of an IRE. */ 27552 ip_ire_req(q, mp); 27553 return; 27554 case M_CTL: 27555 if (mp->b_wptr - mp->b_rptr < sizeof (uint32_t)) 27556 break; 27557 27558 if (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == 27559 TUN_HELLO) { 27560 ASSERT(connp != NULL); 27561 connp->conn_flags |= IPCL_IPTUN; 27562 freeb(mp); 27563 return; 27564 } 27565 27566 if (connp != NULL && *(uint32_t *)mp->b_rptr == 27567 IP_ULP_OUT_LABELED) { 27568 out_labeled_t *olp; 27569 27570 if (mp->b_wptr - mp->b_rptr != sizeof (*olp)) 27571 break; 27572 olp = (out_labeled_t *)mp->b_rptr; 27573 connp->conn_ulp_labeled = olp->out_qnext == q; 27574 freemsg(mp); 27575 return; 27576 } 27577 27578 /* M_CTL messages are used by ARP to tell us things. */ 27579 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 27580 break; 27581 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 27582 case AR_ENTRY_SQUERY: 27583 ip_wput_ctl(q, mp); 27584 return; 27585 case AR_CLIENT_NOTIFY: 27586 ip_arp_news(q, mp); 27587 return; 27588 case AR_DLPIOP_DONE: 27589 ASSERT(q->q_next != NULL); 27590 ill = (ill_t *)q->q_ptr; 27591 /* qwriter_ip releases the refhold */ 27592 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 27593 ill_refhold(ill); 27594 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_done, 27595 CUR_OP, B_FALSE); 27596 return; 27597 case AR_ARP_CLOSING: 27598 /* 27599 * ARP (above us) is closing. If no ARP bringup is 27600 * currently pending, ack the message so that ARP 27601 * can complete its close. Also mark ill_arp_closing 27602 * so that new ARP bringups will fail. If any 27603 * ARP bringup is currently in progress, we will 27604 * ack this when the current ARP bringup completes. 27605 */ 27606 ASSERT(q->q_next != NULL); 27607 ill = (ill_t *)q->q_ptr; 27608 mutex_enter(&ill->ill_lock); 27609 ill->ill_arp_closing = 1; 27610 if (!ill->ill_arp_bringup_pending) { 27611 mutex_exit(&ill->ill_lock); 27612 qreply(q, mp); 27613 } else { 27614 mutex_exit(&ill->ill_lock); 27615 freemsg(mp); 27616 } 27617 return; 27618 case AR_ARP_EXTEND: 27619 /* 27620 * The ARP module above us is capable of duplicate 27621 * address detection. Old ATM drivers will not send 27622 * this message. 27623 */ 27624 ASSERT(q->q_next != NULL); 27625 ill = (ill_t *)q->q_ptr; 27626 ill->ill_arp_extend = B_TRUE; 27627 freemsg(mp); 27628 return; 27629 default: 27630 break; 27631 } 27632 break; 27633 case M_PROTO: 27634 case M_PCPROTO: 27635 /* 27636 * The only PROTO messages we expect are ULP binds and 27637 * copies of option negotiation acknowledgements. 27638 */ 27639 switch (((union T_primitives *)mp->b_rptr)->type) { 27640 case O_T_BIND_REQ: 27641 case T_BIND_REQ: { 27642 /* Request can get queued in bind */ 27643 if (connp == NULL) { 27644 proto_str = "O_T_BIND_REQ/T_BIND_REQ"; 27645 goto protonak; 27646 } 27647 /* 27648 * Both TCP and UDP call ip_bind_{v4,v6}() directly 27649 * instead of going through this path. We only get 27650 * here in the following cases: 27651 * 27652 * a. Bind retries, where ipsq is non-NULL. 27653 * b. T_BIND_REQ is issued from non TCP/UDP 27654 * transport, e.g. icmp for raw socket, 27655 * in which case ipsq will be NULL. 27656 */ 27657 ASSERT(ipsq != NULL || 27658 (!IPCL_IS_TCP(connp) && !IPCL_IS_UDP(connp))); 27659 27660 /* Don't increment refcnt if this is a re-entry */ 27661 if (ipsq == NULL) 27662 CONN_INC_REF(connp); 27663 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 27664 connp, NULL) : ip_bind_v4(q, mp, connp); 27665 if (mp == NULL) 27666 return; 27667 if (IPCL_IS_TCP(connp)) { 27668 /* 27669 * In the case of TCP endpoint we 27670 * come here only for bind retries 27671 */ 27672 ASSERT(ipsq != NULL); 27673 CONN_INC_REF(connp); 27674 squeue_fill(connp->conn_sqp, mp, 27675 ip_resume_tcp_bind, connp, 27676 SQTAG_BIND_RETRY); 27677 return; 27678 } else if (IPCL_IS_UDP(connp)) { 27679 /* 27680 * In the case of UDP endpoint we 27681 * come here only for bind retries 27682 */ 27683 ASSERT(ipsq != NULL); 27684 udp_resume_bind(connp, mp); 27685 return; 27686 } 27687 qreply(q, mp); 27688 CONN_OPER_PENDING_DONE(connp); 27689 return; 27690 } 27691 case T_SVR4_OPTMGMT_REQ: 27692 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 27693 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 27694 27695 if (connp == NULL) { 27696 proto_str = "T_SVR4_OPTMGMT_REQ"; 27697 goto protonak; 27698 } 27699 27700 if (!snmpcom_req(q, mp, ip_snmp_set, 27701 ip_snmp_get, cr)) { 27702 /* 27703 * Call svr4_optcom_req so that it can 27704 * generate the ack. We don't come here 27705 * if this operation is being restarted. 27706 * ip_restart_optmgmt will drop the conn ref. 27707 * In the case of ipsec option after the ipsec 27708 * load is complete conn_restart_ipsec_waiter 27709 * drops the conn ref. 27710 */ 27711 ASSERT(ipsq == NULL); 27712 CONN_INC_REF(connp); 27713 if (ip_check_for_ipsec_opt(q, mp)) 27714 return; 27715 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj); 27716 if (err != EINPROGRESS) { 27717 /* Operation is done */ 27718 CONN_OPER_PENDING_DONE(connp); 27719 } 27720 } 27721 return; 27722 case T_OPTMGMT_REQ: 27723 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 27724 /* 27725 * Note: No snmpcom_req support through new 27726 * T_OPTMGMT_REQ. 27727 * Call tpi_optcom_req so that it can 27728 * generate the ack. 27729 */ 27730 if (connp == NULL) { 27731 proto_str = "T_OPTMGMT_REQ"; 27732 goto protonak; 27733 } 27734 27735 ASSERT(ipsq == NULL); 27736 /* 27737 * We don't come here for restart. ip_restart_optmgmt 27738 * will drop the conn ref. In the case of ipsec option 27739 * after the ipsec load is complete 27740 * conn_restart_ipsec_waiter drops the conn ref. 27741 */ 27742 CONN_INC_REF(connp); 27743 if (ip_check_for_ipsec_opt(q, mp)) 27744 return; 27745 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj); 27746 if (err != EINPROGRESS) { 27747 /* Operation is done */ 27748 CONN_OPER_PENDING_DONE(connp); 27749 } 27750 return; 27751 case T_UNBIND_REQ: 27752 if (connp == NULL) { 27753 proto_str = "T_UNBIND_REQ"; 27754 goto protonak; 27755 } 27756 mp = ip_unbind(q, mp); 27757 qreply(q, mp); 27758 return; 27759 default: 27760 /* 27761 * Have to drop any DLPI messages coming down from 27762 * arp (such as an info_req which would cause ip 27763 * to receive an extra info_ack if it was passed 27764 * through. 27765 */ 27766 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 27767 (int)*(uint_t *)mp->b_rptr)); 27768 freemsg(mp); 27769 return; 27770 } 27771 /* NOTREACHED */ 27772 case IRE_DB_TYPE: { 27773 nce_t *nce; 27774 ill_t *ill; 27775 in6_addr_t gw_addr_v6; 27776 27777 27778 /* 27779 * This is a response back from a resolver. It 27780 * consists of a message chain containing: 27781 * IRE_MBLK-->LL_HDR_MBLK->pkt 27782 * The IRE_MBLK is the one we allocated in ip_newroute. 27783 * The LL_HDR_MBLK is the DLPI header to use to get 27784 * the attached packet, and subsequent ones for the 27785 * same destination, transmitted. 27786 */ 27787 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 27788 break; 27789 /* 27790 * First, check to make sure the resolution succeeded. 27791 * If it failed, the second mblk will be empty. 27792 * If it is, free the chain, dropping the packet. 27793 * (We must ire_delete the ire; that frees the ire mblk) 27794 * We're doing this now to support PVCs for ATM; it's 27795 * a partial xresolv implementation. When we fully implement 27796 * xresolv interfaces, instead of freeing everything here 27797 * we'll initiate neighbor discovery. 27798 * 27799 * For v4 (ARP and other external resolvers) the resolver 27800 * frees the message, so no check is needed. This check 27801 * is required, though, for a full xresolve implementation. 27802 * Including this code here now both shows how external 27803 * resolvers can NACK a resolution request using an 27804 * existing design that has no specific provisions for NACKs, 27805 * and also takes into account that the current non-ARP 27806 * external resolver has been coded to use this method of 27807 * NACKing for all IPv6 (xresolv) cases, 27808 * whether our xresolv implementation is complete or not. 27809 * 27810 */ 27811 ire = (ire_t *)mp->b_rptr; 27812 ill = ire_to_ill(ire); 27813 mp1 = mp->b_cont; /* dl_unitdata_req */ 27814 if (mp1->b_rptr == mp1->b_wptr) { 27815 if (ire->ire_ipversion == IPV6_VERSION) { 27816 /* 27817 * XRESOLV interface. 27818 */ 27819 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27820 mutex_enter(&ire->ire_lock); 27821 gw_addr_v6 = ire->ire_gateway_addr_v6; 27822 mutex_exit(&ire->ire_lock); 27823 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27824 nce = ndp_lookup_v6(ill, 27825 &ire->ire_addr_v6, B_FALSE); 27826 } else { 27827 nce = ndp_lookup_v6(ill, &gw_addr_v6, 27828 B_FALSE); 27829 } 27830 if (nce != NULL) { 27831 nce_resolv_failed(nce); 27832 ndp_delete(nce); 27833 NCE_REFRELE(nce); 27834 } 27835 } 27836 mp->b_cont = NULL; 27837 freemsg(mp1); /* frees the pkt as well */ 27838 ASSERT(ire->ire_nce == NULL); 27839 ire_delete((ire_t *)mp->b_rptr); 27840 return; 27841 } 27842 27843 /* 27844 * Split them into IRE_MBLK and pkt and feed it into 27845 * ire_add_then_send. Then in ire_add_then_send 27846 * the IRE will be added, and then the packet will be 27847 * run back through ip_wput. This time it will make 27848 * it to the wire. 27849 */ 27850 mp->b_cont = NULL; 27851 mp = mp1->b_cont; /* now, mp points to pkt */ 27852 mp1->b_cont = NULL; 27853 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 27854 if (ire->ire_ipversion == IPV6_VERSION) { 27855 /* 27856 * XRESOLV interface. Find the nce and put a copy 27857 * of the dl_unitdata_req in nce_res_mp 27858 */ 27859 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27860 mutex_enter(&ire->ire_lock); 27861 gw_addr_v6 = ire->ire_gateway_addr_v6; 27862 mutex_exit(&ire->ire_lock); 27863 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27864 nce = ndp_lookup_v6(ill, &ire->ire_addr_v6, 27865 B_FALSE); 27866 } else { 27867 nce = ndp_lookup_v6(ill, &gw_addr_v6, B_FALSE); 27868 } 27869 if (nce != NULL) { 27870 /* 27871 * We have to protect nce_res_mp here 27872 * from being accessed by other threads 27873 * while we change the mblk pointer. 27874 * Other functions will also lock the nce when 27875 * accessing nce_res_mp. 27876 * 27877 * The reason we change the mblk pointer 27878 * here rather than copying the resolved address 27879 * into the template is that, unlike with 27880 * ethernet, we have no guarantee that the 27881 * resolved address length will be 27882 * smaller than or equal to the lla length 27883 * with which the template was allocated, 27884 * (for ethernet, they're equal) 27885 * so we have to use the actual resolved 27886 * address mblk - which holds the real 27887 * dl_unitdata_req with the resolved address. 27888 * 27889 * Doing this is the same behavior as was 27890 * previously used in the v4 ARP case. 27891 */ 27892 mutex_enter(&nce->nce_lock); 27893 if (nce->nce_res_mp != NULL) 27894 freemsg(nce->nce_res_mp); 27895 nce->nce_res_mp = mp1; 27896 mutex_exit(&nce->nce_lock); 27897 /* 27898 * We do a fastpath probe here because 27899 * we have resolved the address without 27900 * using Neighbor Discovery. 27901 * In the non-XRESOLV v6 case, the fastpath 27902 * probe is done right after neighbor 27903 * discovery completes. 27904 */ 27905 if (nce->nce_res_mp != NULL) { 27906 int res; 27907 nce_fastpath_list_add(nce); 27908 res = ill_fastpath_probe(ill, 27909 nce->nce_res_mp); 27910 if (res != 0 && res != EAGAIN) 27911 nce_fastpath_list_delete(nce); 27912 } 27913 27914 ire_add_then_send(q, ire, mp); 27915 /* 27916 * Now we have to clean out any packets 27917 * that may have been queued on the nce 27918 * while it was waiting for address resolution 27919 * to complete. 27920 */ 27921 mutex_enter(&nce->nce_lock); 27922 mp1 = nce->nce_qd_mp; 27923 nce->nce_qd_mp = NULL; 27924 mutex_exit(&nce->nce_lock); 27925 while (mp1 != NULL) { 27926 mblk_t *nxt_mp; 27927 queue_t *fwdq = NULL; 27928 ill_t *inbound_ill; 27929 uint_t ifindex; 27930 27931 nxt_mp = mp1->b_next; 27932 mp1->b_next = NULL; 27933 /* 27934 * Retrieve ifindex stored in 27935 * ip_rput_data_v6() 27936 */ 27937 ifindex = 27938 (uint_t)(uintptr_t)mp1->b_prev; 27939 inbound_ill = 27940 ill_lookup_on_ifindex(ifindex, 27941 B_TRUE, NULL, NULL, NULL, 27942 NULL, ipst); 27943 mp1->b_prev = NULL; 27944 if (inbound_ill != NULL) 27945 fwdq = inbound_ill->ill_rq; 27946 27947 if (fwdq != NULL) { 27948 put(fwdq, mp1); 27949 ill_refrele(inbound_ill); 27950 } else 27951 put(WR(ill->ill_rq), mp1); 27952 mp1 = nxt_mp; 27953 } 27954 NCE_REFRELE(nce); 27955 } else { /* nce is NULL; clean up */ 27956 ire_delete(ire); 27957 freemsg(mp); 27958 freemsg(mp1); 27959 return; 27960 } 27961 } else { 27962 nce_t *arpce; 27963 /* 27964 * Link layer resolution succeeded. Recompute the 27965 * ire_nce. 27966 */ 27967 ASSERT(ire->ire_type & (IRE_CACHE|IRE_BROADCAST)); 27968 if ((arpce = ndp_lookup_v4(ill, 27969 (ire->ire_gateway_addr != INADDR_ANY ? 27970 &ire->ire_gateway_addr : &ire->ire_addr), 27971 B_FALSE)) == NULL) { 27972 freeb(ire->ire_mp); 27973 freeb(mp1); 27974 freemsg(mp); 27975 return; 27976 } 27977 mutex_enter(&arpce->nce_lock); 27978 arpce->nce_last = TICK_TO_MSEC(lbolt64); 27979 if (arpce->nce_state == ND_REACHABLE) { 27980 /* 27981 * Someone resolved this before us; 27982 * cleanup the res_mp. Since ire has 27983 * not been added yet, the call to ire_add_v4 27984 * from ire_add_then_send (when a dup is 27985 * detected) will clean up the ire. 27986 */ 27987 freeb(mp1); 27988 } else { 27989 if (arpce->nce_res_mp != NULL) 27990 freemsg(arpce->nce_res_mp); 27991 arpce->nce_res_mp = mp1; 27992 arpce->nce_state = ND_REACHABLE; 27993 } 27994 mutex_exit(&arpce->nce_lock); 27995 if (ire->ire_marks & IRE_MARK_NOADD) { 27996 /* 27997 * this ire will not be added to the ire 27998 * cache table, so we can set the ire_nce 27999 * here, as there are no atomicity constraints. 28000 */ 28001 ire->ire_nce = arpce; 28002 /* 28003 * We are associating this nce with the ire 28004 * so change the nce ref taken in 28005 * ndp_lookup_v4() from 28006 * NCE_REFHOLD to NCE_REFHOLD_NOTR 28007 */ 28008 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 28009 } else { 28010 NCE_REFRELE(arpce); 28011 } 28012 ire_add_then_send(q, ire, mp); 28013 } 28014 return; /* All is well, the packet has been sent. */ 28015 } 28016 case IRE_ARPRESOLVE_TYPE: { 28017 28018 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* fake_ire */ 28019 break; 28020 mp1 = mp->b_cont; /* dl_unitdata_req */ 28021 mp->b_cont = NULL; 28022 /* 28023 * First, check to make sure the resolution succeeded. 28024 * If it failed, the second mblk will be empty. 28025 */ 28026 if (mp1->b_rptr == mp1->b_wptr) { 28027 /* cleanup the incomplete ire, free queued packets */ 28028 freemsg(mp); /* fake ire */ 28029 freeb(mp1); /* dl_unitdata response */ 28030 return; 28031 } 28032 28033 /* 28034 * update any incomplete nce_t found. we lookup the ctable 28035 * and find the nce from the ire->ire_nce because we need 28036 * to pass the ire to ip_xmit_v4 later, and can find both 28037 * ire and nce in one lookup from the ctable. 28038 */ 28039 fake_ire = (ire_t *)mp->b_rptr; 28040 /* 28041 * By the time we come back here from ARP 28042 * the logical outgoing interface of the incomplete ire 28043 * we added in ire_forward could have disappeared, 28044 * causing the incomplete ire to also have 28045 * dissapeared. So we need to retreive the 28046 * proper ipif for the ire before looking 28047 * in ctable; do the ctablelookup based on ire_ipif_seqid 28048 */ 28049 ill = q->q_ptr; 28050 28051 /* Get the outgoing ipif */ 28052 mutex_enter(&ill->ill_lock); 28053 if (ill->ill_state_flags & ILL_CONDEMNED) { 28054 mutex_exit(&ill->ill_lock); 28055 freemsg(mp); /* fake ire */ 28056 freeb(mp1); /* dl_unitdata response */ 28057 return; 28058 } 28059 ipif = ipif_lookup_seqid(ill, fake_ire->ire_ipif_seqid); 28060 28061 if (ipif == NULL) { 28062 mutex_exit(&ill->ill_lock); 28063 ip1dbg(("logical intrf to incomplete ire vanished\n")); 28064 freemsg(mp); 28065 freeb(mp1); 28066 return; 28067 } 28068 ipif_refhold_locked(ipif); 28069 mutex_exit(&ill->ill_lock); 28070 ire = ire_ctable_lookup(fake_ire->ire_addr, 28071 fake_ire->ire_gateway_addr, IRE_CACHE, 28072 ipif, fake_ire->ire_zoneid, NULL, 28073 (MATCH_IRE_GW|MATCH_IRE_IPIF|MATCH_IRE_ZONEONLY), ipst); 28074 ipif_refrele(ipif); 28075 if (ire == NULL) { 28076 /* 28077 * no ire was found; check if there is an nce 28078 * for this lookup; if it has no ire's pointing at it 28079 * cleanup. 28080 */ 28081 if ((nce = ndp_lookup_v4(ill, 28082 (fake_ire->ire_gateway_addr != INADDR_ANY ? 28083 &fake_ire->ire_gateway_addr : &fake_ire->ire_addr), 28084 B_FALSE)) != NULL) { 28085 /* 28086 * cleanup: just reset nce. 28087 * We check for refcnt 2 (one for the nce 28088 * hash list + 1 for the ref taken by 28089 * ndp_lookup_v4) to ensure that there are 28090 * no ire's pointing at the nce. 28091 */ 28092 if (nce->nce_refcnt == 2) { 28093 nce = nce_reinit(nce); 28094 } 28095 if (nce != NULL) 28096 NCE_REFRELE(nce); 28097 } 28098 freeb(mp1); /* dl_unitdata response */ 28099 freemsg(mp); /* fake ire */ 28100 return; 28101 } 28102 nce = ire->ire_nce; 28103 DTRACE_PROBE2(ire__arpresolve__type, 28104 ire_t *, ire, nce_t *, nce); 28105 ASSERT(nce->nce_state != ND_INITIAL); 28106 mutex_enter(&nce->nce_lock); 28107 nce->nce_last = TICK_TO_MSEC(lbolt64); 28108 if (nce->nce_state == ND_REACHABLE) { 28109 /* 28110 * Someone resolved this before us; 28111 * our response is not needed any more. 28112 */ 28113 mutex_exit(&nce->nce_lock); 28114 freeb(mp1); /* dl_unitdata response */ 28115 } else { 28116 if (nce->nce_res_mp != NULL) { 28117 freemsg(nce->nce_res_mp); 28118 /* existing dl_unitdata template */ 28119 } 28120 nce->nce_res_mp = mp1; 28121 nce->nce_state = ND_REACHABLE; 28122 mutex_exit(&nce->nce_lock); 28123 nce_fastpath(nce); 28124 } 28125 /* 28126 * The cached nce_t has been updated to be reachable; 28127 * Set the IRE_MARK_UNCACHED flag and free the fake_ire. 28128 */ 28129 fake_ire->ire_marks &= ~IRE_MARK_UNCACHED; 28130 freemsg(mp); 28131 /* 28132 * send out queued packets. 28133 */ 28134 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 28135 28136 IRE_REFRELE(ire); 28137 return; 28138 } 28139 default: 28140 break; 28141 } 28142 if (q->q_next) { 28143 putnext(q, mp); 28144 } else 28145 freemsg(mp); 28146 return; 28147 28148 protonak: 28149 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str); 28150 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL) 28151 qreply(q, mp); 28152 } 28153 28154 /* 28155 * Process IP options in an outbound packet. Modify the destination if there 28156 * is a source route option. 28157 * Returns non-zero if something fails in which case an ICMP error has been 28158 * sent and mp freed. 28159 */ 28160 static int 28161 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 28162 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 28163 { 28164 ipoptp_t opts; 28165 uchar_t *opt; 28166 uint8_t optval; 28167 uint8_t optlen; 28168 ipaddr_t dst; 28169 intptr_t code = 0; 28170 mblk_t *mp; 28171 ire_t *ire = NULL; 28172 28173 ip2dbg(("ip_wput_options\n")); 28174 mp = ipsec_mp; 28175 if (mctl_present) { 28176 mp = ipsec_mp->b_cont; 28177 } 28178 28179 dst = ipha->ipha_dst; 28180 for (optval = ipoptp_first(&opts, ipha); 28181 optval != IPOPT_EOL; 28182 optval = ipoptp_next(&opts)) { 28183 opt = opts.ipoptp_cur; 28184 optlen = opts.ipoptp_len; 28185 ip2dbg(("ip_wput_options: opt %d, len %d\n", 28186 optval, optlen)); 28187 switch (optval) { 28188 uint32_t off; 28189 case IPOPT_SSRR: 28190 case IPOPT_LSRR: 28191 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28192 ip1dbg(( 28193 "ip_wput_options: bad option offset\n")); 28194 code = (char *)&opt[IPOPT_OLEN] - 28195 (char *)ipha; 28196 goto param_prob; 28197 } 28198 off = opt[IPOPT_OFFSET]; 28199 ip1dbg(("ip_wput_options: next hop 0x%x\n", 28200 ntohl(dst))); 28201 /* 28202 * For strict: verify that dst is directly 28203 * reachable. 28204 */ 28205 if (optval == IPOPT_SSRR) { 28206 ire = ire_ftable_lookup(dst, 0, 0, 28207 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 28208 MBLK_GETLABEL(mp), 28209 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 28210 if (ire == NULL) { 28211 ip1dbg(("ip_wput_options: SSRR not" 28212 " directly reachable: 0x%x\n", 28213 ntohl(dst))); 28214 goto bad_src_route; 28215 } 28216 ire_refrele(ire); 28217 } 28218 break; 28219 case IPOPT_RR: 28220 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28221 ip1dbg(( 28222 "ip_wput_options: bad option offset\n")); 28223 code = (char *)&opt[IPOPT_OLEN] - 28224 (char *)ipha; 28225 goto param_prob; 28226 } 28227 break; 28228 case IPOPT_TS: 28229 /* 28230 * Verify that length >=5 and that there is either 28231 * room for another timestamp or that the overflow 28232 * counter is not maxed out. 28233 */ 28234 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 28235 if (optlen < IPOPT_MINLEN_IT) { 28236 goto param_prob; 28237 } 28238 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28239 ip1dbg(( 28240 "ip_wput_options: bad option offset\n")); 28241 code = (char *)&opt[IPOPT_OFFSET] - 28242 (char *)ipha; 28243 goto param_prob; 28244 } 28245 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 28246 case IPOPT_TS_TSONLY: 28247 off = IPOPT_TS_TIMELEN; 28248 break; 28249 case IPOPT_TS_TSANDADDR: 28250 case IPOPT_TS_PRESPEC: 28251 case IPOPT_TS_PRESPEC_RFC791: 28252 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 28253 break; 28254 default: 28255 code = (char *)&opt[IPOPT_POS_OV_FLG] - 28256 (char *)ipha; 28257 goto param_prob; 28258 } 28259 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 28260 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 28261 /* 28262 * No room and the overflow counter is 15 28263 * already. 28264 */ 28265 goto param_prob; 28266 } 28267 break; 28268 } 28269 } 28270 28271 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 28272 return (0); 28273 28274 ip1dbg(("ip_wput_options: error processing IP options.")); 28275 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 28276 28277 param_prob: 28278 /* 28279 * Since ip_wput() isn't close to finished, we fill 28280 * in enough of the header for credible error reporting. 28281 */ 28282 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28283 /* Failed */ 28284 freemsg(ipsec_mp); 28285 return (-1); 28286 } 28287 icmp_param_problem(q, ipsec_mp, (uint8_t)code, zoneid, ipst); 28288 return (-1); 28289 28290 bad_src_route: 28291 /* 28292 * Since ip_wput() isn't close to finished, we fill 28293 * in enough of the header for credible error reporting. 28294 */ 28295 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28296 /* Failed */ 28297 freemsg(ipsec_mp); 28298 return (-1); 28299 } 28300 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 28301 return (-1); 28302 } 28303 28304 /* 28305 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 28306 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 28307 * thru /etc/system. 28308 */ 28309 #define CONN_MAXDRAINCNT 64 28310 28311 static void 28312 conn_drain_init(ip_stack_t *ipst) 28313 { 28314 int i; 28315 28316 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads; 28317 28318 if ((ipst->ips_conn_drain_list_cnt == 0) || 28319 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 28320 /* 28321 * Default value of the number of drainers is the 28322 * number of cpus, subject to maximum of 8 drainers. 28323 */ 28324 if (boot_max_ncpus != -1) 28325 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 28326 else 28327 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8); 28328 } 28329 28330 ipst->ips_conn_drain_list = kmem_zalloc(ipst->ips_conn_drain_list_cnt * 28331 sizeof (idl_t), KM_SLEEP); 28332 28333 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28334 mutex_init(&ipst->ips_conn_drain_list[i].idl_lock, NULL, 28335 MUTEX_DEFAULT, NULL); 28336 } 28337 } 28338 28339 static void 28340 conn_drain_fini(ip_stack_t *ipst) 28341 { 28342 int i; 28343 28344 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) 28345 mutex_destroy(&ipst->ips_conn_drain_list[i].idl_lock); 28346 kmem_free(ipst->ips_conn_drain_list, 28347 ipst->ips_conn_drain_list_cnt * sizeof (idl_t)); 28348 ipst->ips_conn_drain_list = NULL; 28349 } 28350 28351 /* 28352 * Note: For an overview of how flowcontrol is handled in IP please see the 28353 * IP Flowcontrol notes at the top of this file. 28354 * 28355 * Flow control has blocked us from proceeding. Insert the given conn in one 28356 * of the conn drain lists. These conn wq's will be qenabled later on when 28357 * STREAMS flow control does a backenable. conn_walk_drain will enable 28358 * the first conn in each of these drain lists. Each of these qenabled conns 28359 * in turn enables the next in the list, after it runs, or when it closes, 28360 * thus sustaining the drain process. 28361 * 28362 * The only possible calling sequence is ip_wsrv (on conn) -> ip_wput -> 28363 * conn_drain_insert. Thus there can be only 1 instance of conn_drain_insert 28364 * running at any time, on a given conn, since there can be only 1 service proc 28365 * running on a queue at any time. 28366 */ 28367 void 28368 conn_drain_insert(conn_t *connp) 28369 { 28370 idl_t *idl; 28371 uint_t index; 28372 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28373 28374 mutex_enter(&connp->conn_lock); 28375 if (connp->conn_state_flags & CONN_CLOSING) { 28376 /* 28377 * The conn is closing as a result of which CONN_CLOSING 28378 * is set. Return. 28379 */ 28380 mutex_exit(&connp->conn_lock); 28381 return; 28382 } else if (connp->conn_idl == NULL) { 28383 /* 28384 * Assign the next drain list round robin. We dont' use 28385 * a lock, and thus it may not be strictly round robin. 28386 * Atomicity of load/stores is enough to make sure that 28387 * conn_drain_list_index is always within bounds. 28388 */ 28389 index = ipst->ips_conn_drain_list_index; 28390 ASSERT(index < ipst->ips_conn_drain_list_cnt); 28391 connp->conn_idl = &ipst->ips_conn_drain_list[index]; 28392 index++; 28393 if (index == ipst->ips_conn_drain_list_cnt) 28394 index = 0; 28395 ipst->ips_conn_drain_list_index = index; 28396 } 28397 mutex_exit(&connp->conn_lock); 28398 28399 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28400 if ((connp->conn_drain_prev != NULL) || 28401 (connp->conn_state_flags & CONN_CLOSING)) { 28402 /* 28403 * The conn is already in the drain list, OR 28404 * the conn is closing. We need to check again for 28405 * the closing case again since close can happen 28406 * after we drop the conn_lock, and before we 28407 * acquire the CONN_DRAIN_LIST_LOCK. 28408 */ 28409 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28410 return; 28411 } else { 28412 idl = connp->conn_idl; 28413 } 28414 28415 /* 28416 * The conn is not in the drain list. Insert it at the 28417 * tail of the drain list. The drain list is circular 28418 * and doubly linked. idl_conn points to the 1st element 28419 * in the list. 28420 */ 28421 if (idl->idl_conn == NULL) { 28422 idl->idl_conn = connp; 28423 connp->conn_drain_next = connp; 28424 connp->conn_drain_prev = connp; 28425 } else { 28426 conn_t *head = idl->idl_conn; 28427 28428 connp->conn_drain_next = head; 28429 connp->conn_drain_prev = head->conn_drain_prev; 28430 head->conn_drain_prev->conn_drain_next = connp; 28431 head->conn_drain_prev = connp; 28432 } 28433 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28434 } 28435 28436 /* 28437 * This conn is closing, and we are called from ip_close. OR 28438 * This conn has been serviced by ip_wsrv, and we need to do the tail 28439 * processing. 28440 * If this conn is part of the drain list, we may need to sustain the drain 28441 * process by qenabling the next conn in the drain list. We may also need to 28442 * remove this conn from the list, if it is done. 28443 */ 28444 static void 28445 conn_drain_tail(conn_t *connp, boolean_t closing) 28446 { 28447 idl_t *idl; 28448 28449 /* 28450 * connp->conn_idl is stable at this point, and no lock is needed 28451 * to check it. If we are called from ip_close, close has already 28452 * set CONN_CLOSING, thus freezing the value of conn_idl, and 28453 * called us only because conn_idl is non-null. If we are called thru 28454 * service, conn_idl could be null, but it cannot change because 28455 * service is single-threaded per queue, and there cannot be another 28456 * instance of service trying to call conn_drain_insert on this conn 28457 * now. 28458 */ 28459 ASSERT(!closing || (connp->conn_idl != NULL)); 28460 28461 /* 28462 * If connp->conn_idl is null, the conn has not been inserted into any 28463 * drain list even once since creation of the conn. Just return. 28464 */ 28465 if (connp->conn_idl == NULL) 28466 return; 28467 28468 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28469 28470 if (connp->conn_drain_prev == NULL) { 28471 /* This conn is currently not in the drain list. */ 28472 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28473 return; 28474 } 28475 idl = connp->conn_idl; 28476 if (idl->idl_conn_draining == connp) { 28477 /* 28478 * This conn is the current drainer. If this is the last conn 28479 * in the drain list, we need to do more checks, in the 'if' 28480 * below. Otherwwise we need to just qenable the next conn, 28481 * to sustain the draining, and is handled in the 'else' 28482 * below. 28483 */ 28484 if (connp->conn_drain_next == idl->idl_conn) { 28485 /* 28486 * This conn is the last in this list. This round 28487 * of draining is complete. If idl_repeat is set, 28488 * it means another flow enabling has happened from 28489 * the driver/streams and we need to another round 28490 * of draining. 28491 * If there are more than 2 conns in the drain list, 28492 * do a left rotate by 1, so that all conns except the 28493 * conn at the head move towards the head by 1, and the 28494 * the conn at the head goes to the tail. This attempts 28495 * a more even share for all queues that are being 28496 * drained. 28497 */ 28498 if ((connp->conn_drain_next != connp) && 28499 (idl->idl_conn->conn_drain_next != connp)) { 28500 idl->idl_conn = idl->idl_conn->conn_drain_next; 28501 } 28502 if (idl->idl_repeat) { 28503 qenable(idl->idl_conn->conn_wq); 28504 idl->idl_conn_draining = idl->idl_conn; 28505 idl->idl_repeat = 0; 28506 } else { 28507 idl->idl_conn_draining = NULL; 28508 } 28509 } else { 28510 /* 28511 * If the next queue that we are now qenable'ing, 28512 * is closing, it will remove itself from this list 28513 * and qenable the subsequent queue in ip_close(). 28514 * Serialization is acheived thru idl_lock. 28515 */ 28516 qenable(connp->conn_drain_next->conn_wq); 28517 idl->idl_conn_draining = connp->conn_drain_next; 28518 } 28519 } 28520 if (!connp->conn_did_putbq || closing) { 28521 /* 28522 * Remove ourself from the drain list, if we did not do 28523 * a putbq, or if the conn is closing. 28524 * Note: It is possible that q->q_first is non-null. It means 28525 * that these messages landed after we did a enableok() in 28526 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 28527 * service them. 28528 */ 28529 if (connp->conn_drain_next == connp) { 28530 /* Singleton in the list */ 28531 ASSERT(connp->conn_drain_prev == connp); 28532 idl->idl_conn = NULL; 28533 idl->idl_conn_draining = NULL; 28534 } else { 28535 connp->conn_drain_prev->conn_drain_next = 28536 connp->conn_drain_next; 28537 connp->conn_drain_next->conn_drain_prev = 28538 connp->conn_drain_prev; 28539 if (idl->idl_conn == connp) 28540 idl->idl_conn = connp->conn_drain_next; 28541 ASSERT(idl->idl_conn_draining != connp); 28542 28543 } 28544 connp->conn_drain_next = NULL; 28545 connp->conn_drain_prev = NULL; 28546 } 28547 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28548 } 28549 28550 /* 28551 * Write service routine. Shared perimeter entry point. 28552 * ip_wsrv can be called in any of the following ways. 28553 * 1. The device queue's messages has fallen below the low water mark 28554 * and STREAMS has backenabled the ill_wq. We walk thru all the 28555 * the drain lists and backenable the first conn in each list. 28556 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 28557 * qenabled non-tcp upper layers. We start dequeing messages and call 28558 * ip_wput for each message. 28559 */ 28560 28561 void 28562 ip_wsrv(queue_t *q) 28563 { 28564 conn_t *connp; 28565 ill_t *ill; 28566 mblk_t *mp; 28567 28568 if (q->q_next) { 28569 ill = (ill_t *)q->q_ptr; 28570 if (ill->ill_state_flags == 0) { 28571 /* 28572 * The device flow control has opened up. 28573 * Walk through conn drain lists and qenable the 28574 * first conn in each list. This makes sense only 28575 * if the stream is fully plumbed and setup. 28576 * Hence the if check above. 28577 */ 28578 ip1dbg(("ip_wsrv: walking\n")); 28579 conn_walk_drain(ill->ill_ipst); 28580 } 28581 return; 28582 } 28583 28584 connp = Q_TO_CONN(q); 28585 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 28586 28587 /* 28588 * 1. Set conn_draining flag to signal that service is active. 28589 * 28590 * 2. ip_output determines whether it has been called from service, 28591 * based on the last parameter. If it is IP_WSRV it concludes it 28592 * has been called from service. 28593 * 28594 * 3. Message ordering is preserved by the following logic. 28595 * i. A directly called ip_output (i.e. not thru service) will queue 28596 * the message at the tail, if conn_draining is set (i.e. service 28597 * is running) or if q->q_first is non-null. 28598 * 28599 * ii. If ip_output is called from service, and if ip_output cannot 28600 * putnext due to flow control, it does a putbq. 28601 * 28602 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 28603 * (causing an infinite loop). 28604 */ 28605 ASSERT(!connp->conn_did_putbq); 28606 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 28607 connp->conn_draining = 1; 28608 noenable(q); 28609 while ((mp = getq(q)) != NULL) { 28610 ASSERT(CONN_Q(q)); 28611 28612 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 28613 if (connp->conn_did_putbq) { 28614 /* ip_wput did a putbq */ 28615 break; 28616 } 28617 } 28618 /* 28619 * At this point, a thread coming down from top, calling 28620 * ip_wput, may end up queueing the message. We have not yet 28621 * enabled the queue, so ip_wsrv won't be called again. 28622 * To avoid this race, check q->q_first again (in the loop) 28623 * If the other thread queued the message before we call 28624 * enableok(), we will catch it in the q->q_first check. 28625 * If the other thread queues the message after we call 28626 * enableok(), ip_wsrv will be called again by STREAMS. 28627 */ 28628 connp->conn_draining = 0; 28629 enableok(q); 28630 } 28631 28632 /* Enable the next conn for draining */ 28633 conn_drain_tail(connp, B_FALSE); 28634 28635 connp->conn_did_putbq = 0; 28636 } 28637 28638 /* 28639 * Walk the list of all conn's calling the function provided with the 28640 * specified argument for each. Note that this only walks conn's that 28641 * have been bound. 28642 * Applies to both IPv4 and IPv6. 28643 */ 28644 static void 28645 conn_walk_fanout(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 28646 { 28647 conn_walk_fanout_table(ipst->ips_ipcl_udp_fanout, 28648 ipst->ips_ipcl_udp_fanout_size, 28649 func, arg, zoneid); 28650 conn_walk_fanout_table(ipst->ips_ipcl_conn_fanout, 28651 ipst->ips_ipcl_conn_fanout_size, 28652 func, arg, zoneid); 28653 conn_walk_fanout_table(ipst->ips_ipcl_bind_fanout, 28654 ipst->ips_ipcl_bind_fanout_size, 28655 func, arg, zoneid); 28656 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout, 28657 IPPROTO_MAX, func, arg, zoneid); 28658 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout_v6, 28659 IPPROTO_MAX, func, arg, zoneid); 28660 } 28661 28662 /* 28663 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 28664 * of conns that need to be drained, check if drain is already in progress. 28665 * If so set the idl_repeat bit, indicating that the last conn in the list 28666 * needs to reinitiate the drain once again, for the list. If drain is not 28667 * in progress for the list, initiate the draining, by qenabling the 1st 28668 * conn in the list. The drain is self-sustaining, each qenabled conn will 28669 * in turn qenable the next conn, when it is done/blocked/closing. 28670 */ 28671 static void 28672 conn_walk_drain(ip_stack_t *ipst) 28673 { 28674 int i; 28675 idl_t *idl; 28676 28677 IP_STAT(ipst, ip_conn_walk_drain); 28678 28679 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28680 idl = &ipst->ips_conn_drain_list[i]; 28681 mutex_enter(&idl->idl_lock); 28682 if (idl->idl_conn == NULL) { 28683 mutex_exit(&idl->idl_lock); 28684 continue; 28685 } 28686 /* 28687 * If this list is not being drained currently by 28688 * an ip_wsrv thread, start the process. 28689 */ 28690 if (idl->idl_conn_draining == NULL) { 28691 ASSERT(idl->idl_repeat == 0); 28692 qenable(idl->idl_conn->conn_wq); 28693 idl->idl_conn_draining = idl->idl_conn; 28694 } else { 28695 idl->idl_repeat = 1; 28696 } 28697 mutex_exit(&idl->idl_lock); 28698 } 28699 } 28700 28701 /* 28702 * Walk an conn hash table of `count' buckets, calling func for each entry. 28703 */ 28704 static void 28705 conn_walk_fanout_table(connf_t *connfp, uint_t count, pfv_t func, void *arg, 28706 zoneid_t zoneid) 28707 { 28708 conn_t *connp; 28709 28710 while (count-- > 0) { 28711 mutex_enter(&connfp->connf_lock); 28712 for (connp = connfp->connf_head; connp != NULL; 28713 connp = connp->conn_next) { 28714 if (zoneid == GLOBAL_ZONEID || 28715 zoneid == connp->conn_zoneid) { 28716 CONN_INC_REF(connp); 28717 mutex_exit(&connfp->connf_lock); 28718 (*func)(connp, arg); 28719 mutex_enter(&connfp->connf_lock); 28720 CONN_DEC_REF(connp); 28721 } 28722 } 28723 mutex_exit(&connfp->connf_lock); 28724 connfp++; 28725 } 28726 } 28727 28728 /* ipcl_walk routine invoked for ip_conn_report for each conn. */ 28729 static void 28730 conn_report1(conn_t *connp, void *mp) 28731 { 28732 char buf1[INET6_ADDRSTRLEN]; 28733 char buf2[INET6_ADDRSTRLEN]; 28734 uint_t print_len, buf_len; 28735 28736 ASSERT(connp != NULL); 28737 28738 buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr; 28739 if (buf_len <= 0) 28740 return; 28741 (void) inet_ntop(AF_INET6, &connp->conn_srcv6, buf1, sizeof (buf1)), 28742 (void) inet_ntop(AF_INET6, &connp->conn_remv6, buf2, sizeof (buf2)), 28743 print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len, 28744 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 28745 "%5d %s/%05d %s/%05d\n", 28746 (void *)connp, (void *)CONNP_TO_RQ(connp), 28747 (void *)CONNP_TO_WQ(connp), connp->conn_zoneid, 28748 buf1, connp->conn_lport, 28749 buf2, connp->conn_fport); 28750 if (print_len < buf_len) { 28751 ((mblk_t *)mp)->b_wptr += print_len; 28752 } else { 28753 ((mblk_t *)mp)->b_wptr += buf_len; 28754 } 28755 } 28756 28757 /* 28758 * Named Dispatch routine to produce a formatted report on all conns 28759 * that are listed in one of the fanout tables. 28760 * This report is accessed by using the ndd utility to "get" ND variable 28761 * "ip_conn_status". 28762 */ 28763 /* ARGSUSED */ 28764 static int 28765 ip_conn_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 28766 { 28767 conn_t *connp = Q_TO_CONN(q); 28768 28769 (void) mi_mpprintf(mp, 28770 "CONN " MI_COL_HDRPAD_STR 28771 "rfq " MI_COL_HDRPAD_STR 28772 "stq " MI_COL_HDRPAD_STR 28773 " zone local remote"); 28774 28775 /* 28776 * Because of the ndd constraint, at most we can have 64K buffer 28777 * to put in all conn info. So to be more efficient, just 28778 * allocate a 64K buffer here, assuming we need that large buffer. 28779 * This should be OK as only privileged processes can do ndd /dev/ip. 28780 */ 28781 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 28782 /* The following may work even if we cannot get a large buf. */ 28783 (void) mi_mpprintf(mp, "<< Out of buffer >>\n"); 28784 return (0); 28785 } 28786 28787 conn_walk_fanout(conn_report1, mp->b_cont, connp->conn_zoneid, 28788 connp->conn_netstack->netstack_ip); 28789 return (0); 28790 } 28791 28792 /* 28793 * Determine if the ill and multicast aspects of that packets 28794 * "matches" the conn. 28795 */ 28796 boolean_t 28797 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 28798 zoneid_t zoneid) 28799 { 28800 ill_t *in_ill; 28801 boolean_t found; 28802 ipif_t *ipif; 28803 ire_t *ire; 28804 ipaddr_t dst, src; 28805 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28806 28807 dst = ipha->ipha_dst; 28808 src = ipha->ipha_src; 28809 28810 /* 28811 * conn_incoming_ill is set by IP_BOUND_IF which limits 28812 * unicast, broadcast and multicast reception to 28813 * conn_incoming_ill. conn_wantpacket itself is called 28814 * only for BROADCAST and multicast. 28815 * 28816 * 1) ip_rput supresses duplicate broadcasts if the ill 28817 * is part of a group. Hence, we should be receiving 28818 * just one copy of broadcast for the whole group. 28819 * Thus, if it is part of the group the packet could 28820 * come on any ill of the group and hence we need a 28821 * match on the group. Otherwise, match on ill should 28822 * be sufficient. 28823 * 28824 * 2) ip_rput does not suppress duplicate multicast packets. 28825 * If there are two interfaces in a ill group and we have 28826 * 2 applications (conns) joined a multicast group G on 28827 * both the interfaces, ilm_lookup_ill filter in ip_rput 28828 * will give us two packets because we join G on both the 28829 * interfaces rather than nominating just one interface 28830 * for receiving multicast like broadcast above. So, 28831 * we have to call ilg_lookup_ill to filter out duplicate 28832 * copies, if ill is part of a group. 28833 */ 28834 in_ill = connp->conn_incoming_ill; 28835 if (in_ill != NULL) { 28836 if (in_ill->ill_group == NULL) { 28837 if (in_ill != ill) 28838 return (B_FALSE); 28839 } else if (in_ill->ill_group != ill->ill_group) { 28840 return (B_FALSE); 28841 } 28842 } 28843 28844 if (!CLASSD(dst)) { 28845 if (IPCL_ZONE_MATCH(connp, zoneid)) 28846 return (B_TRUE); 28847 /* 28848 * The conn is in a different zone; we need to check that this 28849 * broadcast address is configured in the application's zone and 28850 * on one ill in the group. 28851 */ 28852 ipif = ipif_get_next_ipif(NULL, ill); 28853 if (ipif == NULL) 28854 return (B_FALSE); 28855 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 28856 connp->conn_zoneid, NULL, 28857 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 28858 ipif_refrele(ipif); 28859 if (ire != NULL) { 28860 ire_refrele(ire); 28861 return (B_TRUE); 28862 } else { 28863 return (B_FALSE); 28864 } 28865 } 28866 28867 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 28868 connp->conn_zoneid == zoneid) { 28869 /* 28870 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 28871 * disabled, therefore we don't dispatch the multicast packet to 28872 * the sending zone. 28873 */ 28874 return (B_FALSE); 28875 } 28876 28877 if ((ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) && 28878 connp->conn_zoneid != zoneid) { 28879 /* 28880 * Multicast packet on the loopback interface: we only match 28881 * conns who joined the group in the specified zone. 28882 */ 28883 return (B_FALSE); 28884 } 28885 28886 if (connp->conn_multi_router) { 28887 /* multicast packet and multicast router socket: send up */ 28888 return (B_TRUE); 28889 } 28890 28891 mutex_enter(&connp->conn_lock); 28892 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 28893 mutex_exit(&connp->conn_lock); 28894 return (found); 28895 } 28896 28897 /* 28898 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 28899 */ 28900 /* ARGSUSED */ 28901 static void 28902 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 28903 { 28904 ill_t *ill = (ill_t *)q->q_ptr; 28905 mblk_t *mp1, *mp2; 28906 ipif_t *ipif; 28907 int err = 0; 28908 conn_t *connp = NULL; 28909 ipsq_t *ipsq; 28910 arc_t *arc; 28911 28912 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 28913 28914 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 28915 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 28916 28917 ASSERT(IAM_WRITER_ILL(ill)); 28918 mp2 = mp->b_cont; 28919 mp->b_cont = NULL; 28920 28921 /* 28922 * We have now received the arp bringup completion message 28923 * from ARP. Mark the arp bringup as done. Also if the arp 28924 * stream has already started closing, send up the AR_ARP_CLOSING 28925 * ack now since ARP is waiting in close for this ack. 28926 */ 28927 mutex_enter(&ill->ill_lock); 28928 ill->ill_arp_bringup_pending = 0; 28929 if (ill->ill_arp_closing) { 28930 mutex_exit(&ill->ill_lock); 28931 /* Let's reuse the mp for sending the ack */ 28932 arc = (arc_t *)mp->b_rptr; 28933 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 28934 arc->arc_cmd = AR_ARP_CLOSING; 28935 qreply(q, mp); 28936 } else { 28937 mutex_exit(&ill->ill_lock); 28938 freeb(mp); 28939 } 28940 28941 ipsq = ill->ill_phyint->phyint_ipsq; 28942 ipif = ipsq->ipsq_pending_ipif; 28943 mp1 = ipsq_pending_mp_get(ipsq, &connp); 28944 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 28945 if (mp1 == NULL) { 28946 /* bringup was aborted by the user */ 28947 freemsg(mp2); 28948 return; 28949 } 28950 28951 /* 28952 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we 28953 * must have an associated conn_t. Otherwise, we're bringing this 28954 * interface back up as part of handling an asynchronous event (e.g., 28955 * physical address change). 28956 */ 28957 if (ipsq->ipsq_current_ioctl != 0) { 28958 ASSERT(connp != NULL); 28959 q = CONNP_TO_WQ(connp); 28960 } else { 28961 ASSERT(connp == NULL); 28962 q = ill->ill_rq; 28963 } 28964 28965 /* 28966 * If the DL_BIND_REQ fails, it is noted 28967 * in arc_name_offset. 28968 */ 28969 err = *((int *)mp2->b_rptr); 28970 if (err == 0) { 28971 if (ipif->ipif_isv6) { 28972 if ((err = ipif_up_done_v6(ipif)) != 0) 28973 ip0dbg(("ip_arp_done: init failed\n")); 28974 } else { 28975 if ((err = ipif_up_done(ipif)) != 0) 28976 ip0dbg(("ip_arp_done: init failed\n")); 28977 } 28978 } else { 28979 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 28980 } 28981 28982 freemsg(mp2); 28983 28984 if ((err == 0) && (ill->ill_up_ipifs)) { 28985 err = ill_up_ipifs(ill, q, mp1); 28986 if (err == EINPROGRESS) 28987 return; 28988 } 28989 28990 if (ill->ill_up_ipifs) 28991 ill_group_cleanup(ill); 28992 28993 /* 28994 * The operation must complete without EINPROGRESS since 28995 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp. 28996 * Otherwise, the operation will be stuck forever in the ipsq. 28997 */ 28998 ASSERT(err != EINPROGRESS); 28999 if (ipsq->ipsq_current_ioctl != 0) 29000 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 29001 else 29002 ipsq_current_finish(ipsq); 29003 } 29004 29005 /* Allocate the private structure */ 29006 static int 29007 ip_priv_alloc(void **bufp) 29008 { 29009 void *buf; 29010 29011 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 29012 return (ENOMEM); 29013 29014 *bufp = buf; 29015 return (0); 29016 } 29017 29018 /* Function to delete the private structure */ 29019 void 29020 ip_priv_free(void *buf) 29021 { 29022 ASSERT(buf != NULL); 29023 kmem_free(buf, sizeof (ip_priv_t)); 29024 } 29025 29026 /* 29027 * The entry point for IPPF processing. 29028 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 29029 * routine just returns. 29030 * 29031 * When called, ip_process generates an ipp_packet_t structure 29032 * which holds the state information for this packet and invokes the 29033 * the classifier (via ipp_packet_process). The classification, depending on 29034 * configured filters, results in a list of actions for this packet. Invoking 29035 * an action may cause the packet to be dropped, in which case the resulting 29036 * mblk (*mpp) is NULL. proc indicates the callout position for 29037 * this packet and ill_index is the interface this packet on or will leave 29038 * on (inbound and outbound resp.). 29039 */ 29040 void 29041 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 29042 { 29043 mblk_t *mp; 29044 ip_priv_t *priv; 29045 ipp_action_id_t aid; 29046 int rc = 0; 29047 ipp_packet_t *pp; 29048 #define IP_CLASS "ip" 29049 29050 /* If the classifier is not loaded, return */ 29051 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 29052 return; 29053 } 29054 29055 mp = *mpp; 29056 ASSERT(mp != NULL); 29057 29058 /* Allocate the packet structure */ 29059 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 29060 if (rc != 0) { 29061 *mpp = NULL; 29062 freemsg(mp); 29063 return; 29064 } 29065 29066 /* Allocate the private structure */ 29067 rc = ip_priv_alloc((void **)&priv); 29068 if (rc != 0) { 29069 *mpp = NULL; 29070 freemsg(mp); 29071 ipp_packet_free(pp); 29072 return; 29073 } 29074 priv->proc = proc; 29075 priv->ill_index = ill_index; 29076 ipp_packet_set_private(pp, priv, ip_priv_free); 29077 ipp_packet_set_data(pp, mp); 29078 29079 /* Invoke the classifier */ 29080 rc = ipp_packet_process(&pp); 29081 if (pp != NULL) { 29082 mp = ipp_packet_get_data(pp); 29083 ipp_packet_free(pp); 29084 if (rc != 0) { 29085 freemsg(mp); 29086 *mpp = NULL; 29087 } 29088 } else { 29089 *mpp = NULL; 29090 } 29091 #undef IP_CLASS 29092 } 29093 29094 /* 29095 * Propagate a multicast group membership operation (add/drop) on 29096 * all the interfaces crossed by the related multirt routes. 29097 * The call is considered successful if the operation succeeds 29098 * on at least one interface. 29099 */ 29100 static int 29101 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 29102 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 29103 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 29104 mblk_t *first_mp) 29105 { 29106 ire_t *ire_gw; 29107 irb_t *irb; 29108 int error = 0; 29109 opt_restart_t *or; 29110 ip_stack_t *ipst = ire->ire_ipst; 29111 29112 irb = ire->ire_bucket; 29113 ASSERT(irb != NULL); 29114 29115 ASSERT(DB_TYPE(first_mp) == M_CTL); 29116 29117 or = (opt_restart_t *)first_mp->b_rptr; 29118 IRB_REFHOLD(irb); 29119 for (; ire != NULL; ire = ire->ire_next) { 29120 if ((ire->ire_flags & RTF_MULTIRT) == 0) 29121 continue; 29122 if (ire->ire_addr != group) 29123 continue; 29124 29125 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 29126 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 29127 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE, ipst); 29128 /* No resolver exists for the gateway; skip this ire. */ 29129 if (ire_gw == NULL) 29130 continue; 29131 29132 /* 29133 * This function can return EINPROGRESS. If so the operation 29134 * will be restarted from ip_restart_optmgmt which will 29135 * call ip_opt_set and option processing will restart for 29136 * this option. So we may end up calling 'fn' more than once. 29137 * This requires that 'fn' is idempotent except for the 29138 * return value. The operation is considered a success if 29139 * it succeeds at least once on any one interface. 29140 */ 29141 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 29142 NULL, fmode, src, first_mp); 29143 if (error == 0) 29144 or->or_private = CGTP_MCAST_SUCCESS; 29145 29146 if (ip_debug > 0) { 29147 ulong_t off; 29148 char *ksym; 29149 ksym = kobj_getsymname((uintptr_t)fn, &off); 29150 ip2dbg(("ip_multirt_apply_membership: " 29151 "called %s, multirt group 0x%08x via itf 0x%08x, " 29152 "error %d [success %u]\n", 29153 ksym ? ksym : "?", 29154 ntohl(group), ntohl(ire_gw->ire_src_addr), 29155 error, or->or_private)); 29156 } 29157 29158 ire_refrele(ire_gw); 29159 if (error == EINPROGRESS) { 29160 IRB_REFRELE(irb); 29161 return (error); 29162 } 29163 } 29164 IRB_REFRELE(irb); 29165 /* 29166 * Consider the call as successful if we succeeded on at least 29167 * one interface. Otherwise, return the last encountered error. 29168 */ 29169 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 29170 } 29171 29172 29173 /* 29174 * Issue a warning regarding a route crossing an interface with an 29175 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 29176 * amount of time is logged. 29177 */ 29178 static void 29179 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 29180 { 29181 hrtime_t current = gethrtime(); 29182 char buf[INET_ADDRSTRLEN]; 29183 ip_stack_t *ipst = ire->ire_ipst; 29184 29185 /* Convert interval in ms to hrtime in ns */ 29186 if (ipst->ips_multirt_bad_mtu_last_time + 29187 ((hrtime_t)ipst->ips_ip_multirt_log_interval * (hrtime_t)1000000) <= 29188 current) { 29189 cmn_err(CE_WARN, "ip: ignoring multiroute " 29190 "to %s, incorrect MTU %u (expected %u)\n", 29191 ip_dot_addr(ire->ire_addr, buf), 29192 ire->ire_max_frag, max_frag); 29193 29194 ipst->ips_multirt_bad_mtu_last_time = current; 29195 } 29196 } 29197 29198 29199 /* 29200 * Get the CGTP (multirouting) filtering status. 29201 * If 0, the CGTP hooks are transparent. 29202 */ 29203 /* ARGSUSED */ 29204 static int 29205 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 29206 { 29207 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 29208 ip_stack_t *ipst = CONNQ_TO_IPST(q); 29209 29210 /* 29211 * Only applies to the shared stack since the filter_ops 29212 * do not carry an ip_stack_t or zoneid. 29213 */ 29214 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 29215 return (ENOTSUP); 29216 29217 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 29218 return (0); 29219 } 29220 29221 29222 /* 29223 * Set the CGTP (multirouting) filtering status. 29224 * If the status is changed from active to transparent 29225 * or from transparent to active, forward the new status 29226 * to the filtering module (if loaded). 29227 */ 29228 /* ARGSUSED */ 29229 static int 29230 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 29231 cred_t *ioc_cr) 29232 { 29233 long new_value; 29234 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 29235 ip_stack_t *ipst = CONNQ_TO_IPST(q); 29236 29237 if (secpolicy_net_config(ioc_cr, B_FALSE) != 0) 29238 return (EPERM); 29239 29240 /* 29241 * Only applies to the shared stack since the filter_ops 29242 * do not carry an ip_stack_t or zoneid. 29243 */ 29244 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 29245 return (ENOTSUP); 29246 29247 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 29248 new_value < 0 || new_value > 1) { 29249 return (EINVAL); 29250 } 29251 29252 /* 29253 * Do not enable CGTP filtering - thus preventing the hooks 29254 * from being invoked - if the version number of the 29255 * filtering module hooks does not match. 29256 */ 29257 if ((ip_cgtp_filter_ops != NULL) && 29258 (ip_cgtp_filter_ops->cfo_filter_rev != CGTP_FILTER_REV)) { 29259 cmn_err(CE_WARN, "IP: CGTP filtering version mismatch " 29260 "(module hooks version %d, expecting %d)\n", 29261 ip_cgtp_filter_ops->cfo_filter_rev, 29262 CGTP_FILTER_REV); 29263 return (ENOTSUP); 29264 } 29265 29266 if ((!*ip_cgtp_filter_value) && new_value) { 29267 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 29268 ip_cgtp_filter_ops == NULL ? 29269 " (module not loaded)" : ""); 29270 } 29271 if (*ip_cgtp_filter_value && (!new_value)) { 29272 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 29273 ip_cgtp_filter_ops == NULL ? 29274 " (module not loaded)" : ""); 29275 } 29276 29277 if (ip_cgtp_filter_ops != NULL) { 29278 int res; 29279 29280 res = ip_cgtp_filter_ops->cfo_change_state(new_value); 29281 if (res) 29282 return (res); 29283 } 29284 29285 *ip_cgtp_filter_value = (boolean_t)new_value; 29286 29287 return (0); 29288 } 29289 29290 29291 /* 29292 * Return the expected CGTP hooks version number. 29293 */ 29294 int 29295 ip_cgtp_filter_supported(void) 29296 { 29297 ip_stack_t *ipst; 29298 int ret; 29299 29300 ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip; 29301 if (ipst == NULL) 29302 return (-1); 29303 ret = ip_cgtp_filter_rev; 29304 netstack_rele(ipst->ips_netstack); 29305 return (ret); 29306 } 29307 29308 29309 /* 29310 * CGTP hooks can be registered by directly touching ip_cgtp_filter_ops 29311 * or by invoking this function. In the first case, the version number 29312 * of the registered structure is checked at hooks activation time 29313 * in ip_cgtp_filter_set(). 29314 * 29315 * Only applies to the shared stack since the filter_ops 29316 * do not carry an ip_stack_t or zoneid. 29317 */ 29318 int 29319 ip_cgtp_filter_register(cgtp_filter_ops_t *ops) 29320 { 29321 ip_stack_t *ipst; 29322 29323 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 29324 return (ENOTSUP); 29325 29326 ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip; 29327 if (ipst == NULL) 29328 return (EINVAL); 29329 29330 ip_cgtp_filter_ops = ops; 29331 netstack_rele(ipst->ips_netstack); 29332 return (0); 29333 } 29334 29335 static squeue_func_t 29336 ip_squeue_switch(int val) 29337 { 29338 squeue_func_t rval = squeue_fill; 29339 29340 switch (val) { 29341 case IP_SQUEUE_ENTER_NODRAIN: 29342 rval = squeue_enter_nodrain; 29343 break; 29344 case IP_SQUEUE_ENTER: 29345 rval = squeue_enter; 29346 break; 29347 default: 29348 break; 29349 } 29350 return (rval); 29351 } 29352 29353 /* ARGSUSED */ 29354 static int 29355 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 29356 caddr_t addr, cred_t *cr) 29357 { 29358 int *v = (int *)addr; 29359 long new_value; 29360 29361 if (secpolicy_net_config(cr, B_FALSE) != 0) 29362 return (EPERM); 29363 29364 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29365 return (EINVAL); 29366 29367 ip_input_proc = ip_squeue_switch(new_value); 29368 *v = new_value; 29369 return (0); 29370 } 29371 29372 /* ARGSUSED */ 29373 static int 29374 ip_int_set(queue_t *q, mblk_t *mp, char *value, 29375 caddr_t addr, cred_t *cr) 29376 { 29377 int *v = (int *)addr; 29378 long new_value; 29379 29380 if (secpolicy_net_config(cr, B_FALSE) != 0) 29381 return (EPERM); 29382 29383 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29384 return (EINVAL); 29385 29386 *v = new_value; 29387 return (0); 29388 } 29389 29390 static void * 29391 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp) 29392 { 29393 kstat_t *ksp; 29394 29395 ip_stat_t template = { 29396 { "ipsec_fanout_proto", KSTAT_DATA_UINT64 }, 29397 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 29398 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 29399 { "ip_udp_fanothers", KSTAT_DATA_UINT64 }, 29400 { "ip_udp_fast_path", KSTAT_DATA_UINT64 }, 29401 { "ip_udp_slow_path", KSTAT_DATA_UINT64 }, 29402 { "ip_udp_input_err", KSTAT_DATA_UINT64 }, 29403 { "ip_tcppullup", KSTAT_DATA_UINT64 }, 29404 { "ip_tcpoptions", KSTAT_DATA_UINT64 }, 29405 { "ip_multipkttcp", KSTAT_DATA_UINT64 }, 29406 { "ip_tcp_fast_path", KSTAT_DATA_UINT64 }, 29407 { "ip_tcp_slow_path", KSTAT_DATA_UINT64 }, 29408 { "ip_tcp_input_error", KSTAT_DATA_UINT64 }, 29409 { "ip_db_ref", KSTAT_DATA_UINT64 }, 29410 { "ip_notaligned1", KSTAT_DATA_UINT64 }, 29411 { "ip_notaligned2", KSTAT_DATA_UINT64 }, 29412 { "ip_multimblk3", KSTAT_DATA_UINT64 }, 29413 { "ip_multimblk4", KSTAT_DATA_UINT64 }, 29414 { "ip_ipoptions", KSTAT_DATA_UINT64 }, 29415 { "ip_classify_fail", KSTAT_DATA_UINT64 }, 29416 { "ip_opt", KSTAT_DATA_UINT64 }, 29417 { "ip_udp_rput_local", KSTAT_DATA_UINT64 }, 29418 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 29419 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 29420 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 29421 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 29422 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 29423 { "ip_trash_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 29424 { "ip_trash_ire_reclaim_success", KSTAT_DATA_UINT64 }, 29425 { "ip_ire_arp_timer_expired", KSTAT_DATA_UINT64 }, 29426 { "ip_ire_redirect_timer_expired", KSTAT_DATA_UINT64 }, 29427 { "ip_ire_pmtu_timer_expired", KSTAT_DATA_UINT64 }, 29428 { "ip_input_multi_squeue", KSTAT_DATA_UINT64 }, 29429 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29430 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29431 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29432 { "ip_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29433 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29434 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29435 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29436 { "ip_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29437 { "ip_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 29438 { "ip_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 29439 { "ip_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 29440 { "ip_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 29441 { "ip_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 29442 }; 29443 29444 ksp = kstat_create_netstack("ip", 0, "ipstat", "net", 29445 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 29446 KSTAT_FLAG_VIRTUAL, stackid); 29447 29448 if (ksp == NULL) 29449 return (NULL); 29450 29451 bcopy(&template, ip_statisticsp, sizeof (template)); 29452 ksp->ks_data = (void *)ip_statisticsp; 29453 ksp->ks_private = (void *)(uintptr_t)stackid; 29454 29455 kstat_install(ksp); 29456 return (ksp); 29457 } 29458 29459 static void 29460 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 29461 { 29462 if (ksp != NULL) { 29463 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29464 kstat_delete_netstack(ksp, stackid); 29465 } 29466 } 29467 29468 static void * 29469 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst) 29470 { 29471 kstat_t *ksp; 29472 29473 ip_named_kstat_t template = { 29474 { "forwarding", KSTAT_DATA_UINT32, 0 }, 29475 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 29476 { "inReceives", KSTAT_DATA_UINT64, 0 }, 29477 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 29478 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 29479 { "forwDatagrams", KSTAT_DATA_UINT64, 0 }, 29480 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 29481 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 29482 { "inDelivers", KSTAT_DATA_UINT64, 0 }, 29483 { "outRequests", KSTAT_DATA_UINT64, 0 }, 29484 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 29485 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 29486 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 29487 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 29488 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 29489 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 29490 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 29491 { "fragFails", KSTAT_DATA_UINT32, 0 }, 29492 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 29493 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 29494 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 29495 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 29496 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 29497 { "inErrs", KSTAT_DATA_UINT32, 0 }, 29498 { "noPorts", KSTAT_DATA_UINT32, 0 }, 29499 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 29500 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 29501 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 29502 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 29503 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 29504 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 29505 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 29506 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 29507 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 29508 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 29509 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 29510 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 29511 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 29512 }; 29513 29514 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 29515 NUM_OF_FIELDS(ip_named_kstat_t), 29516 0, stackid); 29517 if (ksp == NULL || ksp->ks_data == NULL) 29518 return (NULL); 29519 29520 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2; 29521 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl; 29522 template.reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29523 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 29524 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 29525 29526 template.netToMediaEntrySize.value.i32 = 29527 sizeof (mib2_ipNetToMediaEntry_t); 29528 29529 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 29530 29531 bcopy(&template, ksp->ks_data, sizeof (template)); 29532 ksp->ks_update = ip_kstat_update; 29533 ksp->ks_private = (void *)(uintptr_t)stackid; 29534 29535 kstat_install(ksp); 29536 return (ksp); 29537 } 29538 29539 static void 29540 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29541 { 29542 if (ksp != NULL) { 29543 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29544 kstat_delete_netstack(ksp, stackid); 29545 } 29546 } 29547 29548 static int 29549 ip_kstat_update(kstat_t *kp, int rw) 29550 { 29551 ip_named_kstat_t *ipkp; 29552 mib2_ipIfStatsEntry_t ipmib; 29553 ill_walk_context_t ctx; 29554 ill_t *ill; 29555 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29556 netstack_t *ns; 29557 ip_stack_t *ipst; 29558 29559 if (kp == NULL || kp->ks_data == NULL) 29560 return (EIO); 29561 29562 if (rw == KSTAT_WRITE) 29563 return (EACCES); 29564 29565 ns = netstack_find_by_stackid(stackid); 29566 if (ns == NULL) 29567 return (-1); 29568 ipst = ns->netstack_ip; 29569 if (ipst == NULL) { 29570 netstack_rele(ns); 29571 return (-1); 29572 } 29573 ipkp = (ip_named_kstat_t *)kp->ks_data; 29574 29575 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib)); 29576 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 29577 ill = ILL_START_WALK_V4(&ctx, ipst); 29578 for (; ill != NULL; ill = ill_next(&ctx, ill)) 29579 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib); 29580 rw_exit(&ipst->ips_ill_g_lock); 29581 29582 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding; 29583 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL; 29584 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives; 29585 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors; 29586 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors; 29587 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams; 29588 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos; 29589 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards; 29590 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers; 29591 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests; 29592 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards; 29593 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes; 29594 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29595 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds; 29596 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs; 29597 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails; 29598 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs; 29599 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails; 29600 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates; 29601 29602 ipkp->routingDiscards.value.ui32 = 0; 29603 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs; 29604 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts; 29605 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs; 29606 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates; 29607 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups; 29608 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits; 29609 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs; 29610 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows; 29611 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows; 29612 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded; 29613 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed; 29614 29615 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion; 29616 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion; 29617 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion; 29618 29619 netstack_rele(ns); 29620 29621 return (0); 29622 } 29623 29624 static void * 29625 icmp_kstat_init(netstackid_t stackid) 29626 { 29627 kstat_t *ksp; 29628 29629 icmp_named_kstat_t template = { 29630 { "inMsgs", KSTAT_DATA_UINT32 }, 29631 { "inErrors", KSTAT_DATA_UINT32 }, 29632 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 29633 { "inTimeExcds", KSTAT_DATA_UINT32 }, 29634 { "inParmProbs", KSTAT_DATA_UINT32 }, 29635 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 29636 { "inRedirects", KSTAT_DATA_UINT32 }, 29637 { "inEchos", KSTAT_DATA_UINT32 }, 29638 { "inEchoReps", KSTAT_DATA_UINT32 }, 29639 { "inTimestamps", KSTAT_DATA_UINT32 }, 29640 { "inTimestampReps", KSTAT_DATA_UINT32 }, 29641 { "inAddrMasks", KSTAT_DATA_UINT32 }, 29642 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 29643 { "outMsgs", KSTAT_DATA_UINT32 }, 29644 { "outErrors", KSTAT_DATA_UINT32 }, 29645 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 29646 { "outTimeExcds", KSTAT_DATA_UINT32 }, 29647 { "outParmProbs", KSTAT_DATA_UINT32 }, 29648 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 29649 { "outRedirects", KSTAT_DATA_UINT32 }, 29650 { "outEchos", KSTAT_DATA_UINT32 }, 29651 { "outEchoReps", KSTAT_DATA_UINT32 }, 29652 { "outTimestamps", KSTAT_DATA_UINT32 }, 29653 { "outTimestampReps", KSTAT_DATA_UINT32 }, 29654 { "outAddrMasks", KSTAT_DATA_UINT32 }, 29655 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 29656 { "inChksumErrs", KSTAT_DATA_UINT32 }, 29657 { "inUnknowns", KSTAT_DATA_UINT32 }, 29658 { "inFragNeeded", KSTAT_DATA_UINT32 }, 29659 { "outFragNeeded", KSTAT_DATA_UINT32 }, 29660 { "outDrops", KSTAT_DATA_UINT32 }, 29661 { "inOverFlows", KSTAT_DATA_UINT32 }, 29662 { "inBadRedirects", KSTAT_DATA_UINT32 }, 29663 }; 29664 29665 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 29666 NUM_OF_FIELDS(icmp_named_kstat_t), 29667 0, stackid); 29668 if (ksp == NULL || ksp->ks_data == NULL) 29669 return (NULL); 29670 29671 bcopy(&template, ksp->ks_data, sizeof (template)); 29672 29673 ksp->ks_update = icmp_kstat_update; 29674 ksp->ks_private = (void *)(uintptr_t)stackid; 29675 29676 kstat_install(ksp); 29677 return (ksp); 29678 } 29679 29680 static void 29681 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29682 { 29683 if (ksp != NULL) { 29684 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29685 kstat_delete_netstack(ksp, stackid); 29686 } 29687 } 29688 29689 static int 29690 icmp_kstat_update(kstat_t *kp, int rw) 29691 { 29692 icmp_named_kstat_t *icmpkp; 29693 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29694 netstack_t *ns; 29695 ip_stack_t *ipst; 29696 29697 if ((kp == NULL) || (kp->ks_data == NULL)) 29698 return (EIO); 29699 29700 if (rw == KSTAT_WRITE) 29701 return (EACCES); 29702 29703 ns = netstack_find_by_stackid(stackid); 29704 if (ns == NULL) 29705 return (-1); 29706 ipst = ns->netstack_ip; 29707 if (ipst == NULL) { 29708 netstack_rele(ns); 29709 return (-1); 29710 } 29711 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 29712 29713 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs; 29714 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors; 29715 icmpkp->inDestUnreachs.value.ui32 = 29716 ipst->ips_icmp_mib.icmpInDestUnreachs; 29717 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds; 29718 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs; 29719 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs; 29720 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects; 29721 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos; 29722 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps; 29723 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps; 29724 icmpkp->inTimestampReps.value.ui32 = 29725 ipst->ips_icmp_mib.icmpInTimestampReps; 29726 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks; 29727 icmpkp->inAddrMaskReps.value.ui32 = 29728 ipst->ips_icmp_mib.icmpInAddrMaskReps; 29729 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs; 29730 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors; 29731 icmpkp->outDestUnreachs.value.ui32 = 29732 ipst->ips_icmp_mib.icmpOutDestUnreachs; 29733 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds; 29734 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs; 29735 icmpkp->outSrcQuenchs.value.ui32 = 29736 ipst->ips_icmp_mib.icmpOutSrcQuenchs; 29737 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects; 29738 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos; 29739 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps; 29740 icmpkp->outTimestamps.value.ui32 = 29741 ipst->ips_icmp_mib.icmpOutTimestamps; 29742 icmpkp->outTimestampReps.value.ui32 = 29743 ipst->ips_icmp_mib.icmpOutTimestampReps; 29744 icmpkp->outAddrMasks.value.ui32 = 29745 ipst->ips_icmp_mib.icmpOutAddrMasks; 29746 icmpkp->outAddrMaskReps.value.ui32 = 29747 ipst->ips_icmp_mib.icmpOutAddrMaskReps; 29748 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs; 29749 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns; 29750 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded; 29751 icmpkp->outFragNeeded.value.ui32 = 29752 ipst->ips_icmp_mib.icmpOutFragNeeded; 29753 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops; 29754 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows; 29755 icmpkp->inBadRedirects.value.ui32 = 29756 ipst->ips_icmp_mib.icmpInBadRedirects; 29757 29758 netstack_rele(ns); 29759 return (0); 29760 } 29761 29762 /* 29763 * This is the fanout function for raw socket opened for SCTP. Note 29764 * that it is called after SCTP checks that there is no socket which 29765 * wants a packet. Then before SCTP handles this out of the blue packet, 29766 * this function is called to see if there is any raw socket for SCTP. 29767 * If there is and it is bound to the correct address, the packet will 29768 * be sent to that socket. Note that only one raw socket can be bound to 29769 * a port. This is assured in ipcl_sctp_hash_insert(); 29770 */ 29771 void 29772 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 29773 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 29774 zoneid_t zoneid) 29775 { 29776 conn_t *connp; 29777 queue_t *rq; 29778 mblk_t *first_mp; 29779 boolean_t secure; 29780 ip6_t *ip6h; 29781 ip_stack_t *ipst = recv_ill->ill_ipst; 29782 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 29783 29784 first_mp = mp; 29785 if (mctl_present) { 29786 mp = first_mp->b_cont; 29787 secure = ipsec_in_is_secure(first_mp); 29788 ASSERT(mp != NULL); 29789 } else { 29790 secure = B_FALSE; 29791 } 29792 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 29793 29794 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha, ipst); 29795 if (connp == NULL) { 29796 sctp_ootb_input(first_mp, recv_ill, zoneid, mctl_present); 29797 return; 29798 } 29799 rq = connp->conn_rq; 29800 if (!canputnext(rq)) { 29801 CONN_DEC_REF(connp); 29802 BUMP_MIB(recv_ill->ill_ip_mib, rawipIfStatsInOverflows); 29803 freemsg(first_mp); 29804 return; 29805 } 29806 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 29807 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || secure) { 29808 first_mp = ipsec_check_inbound_policy(first_mp, connp, 29809 (isv4 ? ipha : NULL), ip6h, mctl_present); 29810 if (first_mp == NULL) { 29811 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 29812 CONN_DEC_REF(connp); 29813 return; 29814 } 29815 } 29816 /* 29817 * We probably should not send M_CTL message up to 29818 * raw socket. 29819 */ 29820 if (mctl_present) 29821 freeb(first_mp); 29822 29823 /* Initiate IPPF processing here if needed. */ 29824 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) || 29825 (!isv4 && IP6_IN_IPP(flags, ipst))) { 29826 ip_process(IPP_LOCAL_IN, &mp, 29827 recv_ill->ill_phyint->phyint_ifindex); 29828 if (mp == NULL) { 29829 CONN_DEC_REF(connp); 29830 return; 29831 } 29832 } 29833 29834 if (connp->conn_recvif || connp->conn_recvslla || 29835 ((connp->conn_ip_recvpktinfo || 29836 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 29837 (flags & IP_FF_IPINFO))) { 29838 int in_flags = 0; 29839 29840 /* 29841 * Since sctp does not support IP_RECVPKTINFO for v4, only pass 29842 * IPF_RECVIF. 29843 */ 29844 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 29845 in_flags = IPF_RECVIF; 29846 } 29847 if (connp->conn_recvslla) { 29848 in_flags |= IPF_RECVSLLA; 29849 } 29850 if (isv4) { 29851 mp = ip_add_info(mp, recv_ill, in_flags, 29852 IPCL_ZONEID(connp), ipst); 29853 } else { 29854 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 29855 if (mp == NULL) { 29856 BUMP_MIB(recv_ill->ill_ip_mib, 29857 ipIfStatsInDiscards); 29858 CONN_DEC_REF(connp); 29859 return; 29860 } 29861 } 29862 } 29863 29864 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 29865 /* 29866 * We are sending the IPSEC_IN message also up. Refer 29867 * to comments above this function. 29868 */ 29869 putnext(rq, mp); 29870 CONN_DEC_REF(connp); 29871 } 29872 29873 #define UPDATE_IP_MIB_OB_COUNTERS(ill, len) \ 29874 { \ 29875 BUMP_MIB((ill)->ill_ip_mib, ipIfStatsHCOutTransmits); \ 29876 UPDATE_MIB((ill)->ill_ip_mib, ipIfStatsHCOutOctets, (len)); \ 29877 } 29878 /* 29879 * This function should be called only if all packet processing 29880 * including fragmentation is complete. Callers of this function 29881 * must set mp->b_prev to one of these values: 29882 * {0, IPP_FWD_OUT, IPP_LOCAL_OUT} 29883 * prior to handing over the mp as first argument to this function. 29884 * 29885 * If the ire passed by caller is incomplete, this function 29886 * queues the packet and if necessary, sends ARP request and bails. 29887 * If the ire passed is fully resolved, we simply prepend 29888 * the link-layer header to the packet, do ipsec hw acceleration 29889 * work if necessary, and send the packet out on the wire. 29890 * 29891 * NOTE: IPSEC will only call this function with fully resolved 29892 * ires if hw acceleration is involved. 29893 * TODO list : 29894 * a Handle M_MULTIDATA so that 29895 * tcp_multisend->tcp_multisend_data can 29896 * call ip_xmit_v4 directly 29897 * b Handle post-ARP work for fragments so that 29898 * ip_wput_frag can call this function. 29899 */ 29900 ipxmit_state_t 29901 ip_xmit_v4(mblk_t *mp, ire_t *ire, ipsec_out_t *io, boolean_t flow_ctl_enabled) 29902 { 29903 nce_t *arpce; 29904 queue_t *q; 29905 int ill_index; 29906 mblk_t *nxt_mp, *first_mp; 29907 boolean_t xmit_drop = B_FALSE; 29908 ip_proc_t proc; 29909 ill_t *out_ill; 29910 int pkt_len; 29911 29912 arpce = ire->ire_nce; 29913 ASSERT(arpce != NULL); 29914 29915 DTRACE_PROBE2(ip__xmit__v4, ire_t *, ire, nce_t *, arpce); 29916 29917 mutex_enter(&arpce->nce_lock); 29918 switch (arpce->nce_state) { 29919 case ND_REACHABLE: 29920 /* If there are other queued packets, queue this packet */ 29921 if (arpce->nce_qd_mp != NULL) { 29922 if (mp != NULL) 29923 nce_queue_mp_common(arpce, mp, B_FALSE); 29924 mp = arpce->nce_qd_mp; 29925 } 29926 arpce->nce_qd_mp = NULL; 29927 mutex_exit(&arpce->nce_lock); 29928 29929 /* 29930 * Flush the queue. In the common case, where the 29931 * ARP is already resolved, it will go through the 29932 * while loop only once. 29933 */ 29934 while (mp != NULL) { 29935 29936 nxt_mp = mp->b_next; 29937 mp->b_next = NULL; 29938 ASSERT(mp->b_datap->db_type != M_CTL); 29939 pkt_len = ntohs(((ipha_t *)mp->b_rptr)->ipha_length); 29940 /* 29941 * This info is needed for IPQOS to do COS marking 29942 * in ip_wput_attach_llhdr->ip_process. 29943 */ 29944 proc = (ip_proc_t)(uintptr_t)mp->b_prev; 29945 mp->b_prev = NULL; 29946 29947 /* set up ill index for outbound qos processing */ 29948 out_ill = ire->ire_ipif->ipif_ill; 29949 ill_index = out_ill->ill_phyint->phyint_ifindex; 29950 first_mp = ip_wput_attach_llhdr(mp, ire, proc, 29951 ill_index); 29952 if (first_mp == NULL) { 29953 xmit_drop = B_TRUE; 29954 BUMP_MIB(out_ill->ill_ip_mib, 29955 ipIfStatsOutDiscards); 29956 goto next_mp; 29957 } 29958 /* non-ipsec hw accel case */ 29959 if (io == NULL || !io->ipsec_out_accelerated) { 29960 /* send it */ 29961 q = ire->ire_stq; 29962 if (proc == IPP_FWD_OUT) { 29963 UPDATE_IB_PKT_COUNT(ire); 29964 } else { 29965 UPDATE_OB_PKT_COUNT(ire); 29966 } 29967 ire->ire_last_used_time = lbolt; 29968 29969 if (flow_ctl_enabled || canputnext(q)) { 29970 if (proc == IPP_FWD_OUT) { 29971 BUMP_MIB(out_ill->ill_ip_mib, 29972 ipIfStatsHCOutForwDatagrams); 29973 } 29974 UPDATE_IP_MIB_OB_COUNTERS(out_ill, 29975 pkt_len); 29976 29977 putnext(q, first_mp); 29978 } else { 29979 BUMP_MIB(out_ill->ill_ip_mib, 29980 ipIfStatsOutDiscards); 29981 xmit_drop = B_TRUE; 29982 freemsg(first_mp); 29983 } 29984 } else { 29985 /* 29986 * Safety Pup says: make sure this 29987 * is going to the right interface! 29988 */ 29989 ill_t *ill1 = 29990 (ill_t *)ire->ire_stq->q_ptr; 29991 int ifindex = 29992 ill1->ill_phyint->phyint_ifindex; 29993 if (ifindex != 29994 io->ipsec_out_capab_ill_index) { 29995 xmit_drop = B_TRUE; 29996 freemsg(mp); 29997 } else { 29998 UPDATE_IP_MIB_OB_COUNTERS(ill1, 29999 pkt_len); 30000 ipsec_hw_putnext(ire->ire_stq, mp); 30001 } 30002 } 30003 next_mp: 30004 mp = nxt_mp; 30005 } /* while (mp != NULL) */ 30006 if (xmit_drop) 30007 return (SEND_FAILED); 30008 else 30009 return (SEND_PASSED); 30010 30011 case ND_INITIAL: 30012 case ND_INCOMPLETE: 30013 30014 /* 30015 * While we do send off packets to dests that 30016 * use fully-resolved CGTP routes, we do not 30017 * handle unresolved CGTP routes. 30018 */ 30019 ASSERT(!(ire->ire_flags & RTF_MULTIRT)); 30020 ASSERT(io == NULL || !io->ipsec_out_accelerated); 30021 30022 if (mp != NULL) { 30023 /* queue the packet */ 30024 nce_queue_mp_common(arpce, mp, B_FALSE); 30025 } 30026 30027 if (arpce->nce_state == ND_INCOMPLETE) { 30028 mutex_exit(&arpce->nce_lock); 30029 DTRACE_PROBE3(ip__xmit__incomplete, 30030 (ire_t *), ire, (mblk_t *), mp, 30031 (ipsec_out_t *), io); 30032 return (LOOKUP_IN_PROGRESS); 30033 } 30034 30035 arpce->nce_state = ND_INCOMPLETE; 30036 mutex_exit(&arpce->nce_lock); 30037 /* 30038 * Note that ire_add() (called from ire_forward()) 30039 * holds a ref on the ire until ARP is completed. 30040 */ 30041 30042 ire_arpresolve(ire, ire_to_ill(ire)); 30043 return (LOOKUP_IN_PROGRESS); 30044 default: 30045 ASSERT(0); 30046 mutex_exit(&arpce->nce_lock); 30047 return (LLHDR_RESLV_FAILED); 30048 } 30049 } 30050 30051 #undef UPDATE_IP_MIB_OB_COUNTERS 30052 30053 /* 30054 * Return B_TRUE if the buffers differ in length or content. 30055 * This is used for comparing extension header buffers. 30056 * Note that an extension header would be declared different 30057 * even if all that changed was the next header value in that header i.e. 30058 * what really changed is the next extension header. 30059 */ 30060 boolean_t 30061 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 30062 uint_t blen) 30063 { 30064 if (!b_valid) 30065 blen = 0; 30066 30067 if (alen != blen) 30068 return (B_TRUE); 30069 if (alen == 0) 30070 return (B_FALSE); /* Both zero length */ 30071 return (bcmp(abuf, bbuf, alen)); 30072 } 30073 30074 /* 30075 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 30076 * Return B_FALSE if memory allocation fails - don't change any state! 30077 */ 30078 boolean_t 30079 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 30080 const void *src, uint_t srclen) 30081 { 30082 void *dst; 30083 30084 if (!src_valid) 30085 srclen = 0; 30086 30087 ASSERT(*dstlenp == 0); 30088 if (src != NULL && srclen != 0) { 30089 dst = mi_alloc(srclen, BPRI_MED); 30090 if (dst == NULL) 30091 return (B_FALSE); 30092 } else { 30093 dst = NULL; 30094 } 30095 if (*dstp != NULL) 30096 mi_free(*dstp); 30097 *dstp = dst; 30098 *dstlenp = dst == NULL ? 0 : srclen; 30099 return (B_TRUE); 30100 } 30101 30102 /* 30103 * Replace what is in *dst, *dstlen with the source. 30104 * Assumes ip_allocbuf has already been called. 30105 */ 30106 void 30107 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 30108 const void *src, uint_t srclen) 30109 { 30110 if (!src_valid) 30111 srclen = 0; 30112 30113 ASSERT(*dstlenp == srclen); 30114 if (src != NULL && srclen != 0) 30115 bcopy(src, *dstp, srclen); 30116 } 30117 30118 /* 30119 * Free the storage pointed to by the members of an ip6_pkt_t. 30120 */ 30121 void 30122 ip6_pkt_free(ip6_pkt_t *ipp) 30123 { 30124 ASSERT(ipp->ipp_pathmtu == NULL && !(ipp->ipp_fields & IPPF_PATHMTU)); 30125 30126 if (ipp->ipp_fields & IPPF_HOPOPTS) { 30127 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 30128 ipp->ipp_hopopts = NULL; 30129 ipp->ipp_hopoptslen = 0; 30130 } 30131 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 30132 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 30133 ipp->ipp_rtdstopts = NULL; 30134 ipp->ipp_rtdstoptslen = 0; 30135 } 30136 if (ipp->ipp_fields & IPPF_DSTOPTS) { 30137 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 30138 ipp->ipp_dstopts = NULL; 30139 ipp->ipp_dstoptslen = 0; 30140 } 30141 if (ipp->ipp_fields & IPPF_RTHDR) { 30142 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 30143 ipp->ipp_rthdr = NULL; 30144 ipp->ipp_rthdrlen = 0; 30145 } 30146 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 30147 IPPF_RTHDR); 30148 } 30149