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 * It would be nice to have these present only in DEBUG systems, but the 158 * current design of the global symbol checking logic requires them to be 159 * unconditionally present. 160 */ 161 uint_t ip_thread_data; /* TSD key for debug support */ 162 krwlock_t ip_thread_rwlock; 163 list_t ip_thread_list; 164 165 /* 166 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 167 */ 168 169 struct listptr_s { 170 mblk_t *lp_head; /* pointer to the head of the list */ 171 mblk_t *lp_tail; /* pointer to the tail of the list */ 172 }; 173 174 typedef struct listptr_s listptr_t; 175 176 /* 177 * This is used by ip_snmp_get_mib2_ip_route_media and 178 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 179 */ 180 typedef struct iproutedata_s { 181 uint_t ird_idx; 182 listptr_t ird_route; /* ipRouteEntryTable */ 183 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 184 listptr_t ird_attrs; /* ipRouteAttributeTable */ 185 } iproutedata_t; 186 187 /* 188 * Cluster specific hooks. These should be NULL when booted as a non-cluster 189 */ 190 191 /* 192 * Hook functions to enable cluster networking 193 * On non-clustered systems these vectors must always be NULL. 194 * 195 * Hook function to Check ip specified ip address is a shared ip address 196 * in the cluster 197 * 198 */ 199 int (*cl_inet_isclusterwide)(uint8_t protocol, 200 sa_family_t addr_family, uint8_t *laddrp) = NULL; 201 202 /* 203 * Hook function to generate cluster wide ip fragment identifier 204 */ 205 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 206 uint8_t *laddrp, uint8_t *faddrp) = NULL; 207 208 /* 209 * Synchronization notes: 210 * 211 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 212 * MT level protection given by STREAMS. IP uses a combination of its own 213 * internal serialization mechanism and standard Solaris locking techniques. 214 * The internal serialization is per phyint (no IPMP) or per IPMP group. 215 * This is used to serialize plumbing operations, IPMP operations, certain 216 * multicast operations, most set ioctls, igmp/mld timers etc. 217 * 218 * Plumbing is a long sequence of operations involving message 219 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 220 * involved in plumbing operations. A natural model is to serialize these 221 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 222 * parallel without any interference. But various set ioctls on hme0 are best 223 * serialized. However if the system uses IPMP, the operations are easier if 224 * they are serialized on a per IPMP group basis since IPMP operations 225 * happen across ill's of a group. Thus the lowest common denominator is to 226 * serialize most set ioctls, multicast join/leave operations, IPMP operations 227 * igmp/mld timer operations, and processing of DLPI control messages received 228 * from drivers on a per IPMP group basis. If the system does not employ 229 * IPMP the serialization is on a per phyint basis. This serialization is 230 * provided by the ipsq_t and primitives operating on this. Details can 231 * be found in ip_if.c above the core primitives operating on ipsq_t. 232 * 233 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 234 * Simiarly lookup of an ire by a thread also returns a refheld ire. 235 * In addition ipif's and ill's referenced by the ire are also indirectly 236 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 237 * the ipif's address or netmask change as long as an ipif is refheld 238 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the 239 * address of an ipif has to go through the ipsq_t. This ensures that only 240 * 1 such exclusive operation proceeds at any time on the ipif. It then 241 * deletes all ires associated with this ipif, and waits for all refcnts 242 * associated with this ipif to come down to zero. The address is changed 243 * only after the ipif has been quiesced. Then the ipif is brought up again. 244 * More details are described above the comment in ip_sioctl_flags. 245 * 246 * Packet processing is based mostly on IREs and are fully multi-threaded 247 * using standard Solaris MT techniques. 248 * 249 * There are explicit locks in IP to handle: 250 * - The ip_g_head list maintained by mi_open_link() and friends. 251 * 252 * - The reassembly data structures (one lock per hash bucket) 253 * 254 * - conn_lock is meant to protect conn_t fields. The fields actually 255 * protected by conn_lock are documented in the conn_t definition. 256 * 257 * - ire_lock to protect some of the fields of the ire, IRE tables 258 * (one lock per hash bucket). Refer to ip_ire.c for details. 259 * 260 * - ndp_g_lock and nce_lock for protecting NCEs. 261 * 262 * - ill_lock protects fields of the ill and ipif. Details in ip.h 263 * 264 * - ill_g_lock: This is a global reader/writer lock. Protects the following 265 * * The AVL tree based global multi list of all ills. 266 * * The linked list of all ipifs of an ill 267 * * The <ill-ipsq> mapping 268 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next 269 * * The illgroup list threaded by ill_group_next. 270 * * <ill-phyint> association 271 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 272 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion 273 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill 274 * will all have to hold the ill_g_lock as writer for the actual duration 275 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping 276 * may be found in the IPMP section. 277 * 278 * - ill_lock: This is a per ill mutex. 279 * It protects some members of the ill and is documented below. 280 * It also protects the <ill-ipsq> mapping 281 * It also protects the illgroup list threaded by ill_group_next. 282 * It also protects the <ill-phyint> assoc. 283 * It also protects the list of ipifs hanging off the ill. 284 * 285 * - ipsq_lock: This is a per ipsq_t mutex lock. 286 * This protects all the other members of the ipsq struct except 287 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock 288 * 289 * - illgrp_lock: This is a per ill_group mutex lock. 290 * The only thing it protects is the illgrp_ill_schednext member of ill_group 291 * which dictates which is the next ill in an ill_group that is to be chosen 292 * for sending outgoing packets, through creation of an IRE_CACHE that 293 * references this ill. 294 * 295 * - phyint_lock: This is a per phyint mutex lock. Protects just the 296 * phyint_flags 297 * 298 * - ip_g_nd_lock: This is a global reader/writer lock. 299 * Any call to nd_load to load a new parameter to the ND table must hold the 300 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 301 * as reader. 302 * 303 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 304 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 305 * uniqueness check also done atomically. 306 * 307 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 308 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 309 * as a writer when adding or deleting elements from these lists, and 310 * as a reader when walking these lists to send a SADB update to the 311 * IPsec capable ills. 312 * 313 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 314 * group list linked by ill_usesrc_grp_next. It also protects the 315 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 316 * group is being added or deleted. This lock is taken as a reader when 317 * walking the list/group(eg: to get the number of members in a usesrc group). 318 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 319 * field is changing state i.e from NULL to non-NULL or vice-versa. For 320 * example, it is not necessary to take this lock in the initial portion 321 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and 322 * ip_sioctl_flags since the these operations are executed exclusively and 323 * that ensures that the "usesrc group state" cannot change. The "usesrc 324 * group state" change can happen only in the latter part of 325 * ip_sioctl_slifusesrc and in ill_delete. 326 * 327 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications. 328 * 329 * To change the <ill-phyint> association, the ill_g_lock must be held 330 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 331 * must be held. 332 * 333 * To change the <ill-ipsq> association the ill_g_lock must be held as writer 334 * and the ill_lock of the ill in question must be held. 335 * 336 * To change the <ill-illgroup> association the ill_g_lock must be held as 337 * writer and the ill_lock of the ill in question must be held. 338 * 339 * To add or delete an ipif from the list of ipifs hanging off the ill, 340 * ill_g_lock (writer) and ill_lock must be held and the thread must be 341 * a writer on the associated ipsq,. 342 * 343 * To add or delete an ill to the system, the ill_g_lock must be held as 344 * writer and the thread must be a writer on the associated ipsq. 345 * 346 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 347 * must be a writer on the associated ipsq. 348 * 349 * Lock hierarchy 350 * 351 * Some lock hierarchy scenarios are listed below. 352 * 353 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 354 * ill_g_lock -> illgrp_lock -> ill_lock 355 * ill_g_lock -> ill_lock(s) -> phyint_lock 356 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 357 * ill_g_lock -> ip_addr_avail_lock 358 * conn_lock -> irb_lock -> ill_lock -> ire_lock 359 * ill_g_lock -> ip_g_nd_lock 360 * 361 * When more than 1 ill lock is needed to be held, all ill lock addresses 362 * are sorted on address and locked starting from highest addressed lock 363 * downward. 364 * 365 * IPsec scenarios 366 * 367 * ipsa_lock -> ill_g_lock -> ill_lock 368 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 369 * ipsec_capab_ills_lock -> ipsa_lock 370 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 371 * 372 * Trusted Solaris scenarios 373 * 374 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 375 * igsa_lock -> gcdb_lock 376 * gcgrp_rwlock -> ire_lock 377 * gcgrp_rwlock -> gcdb_lock 378 * 379 * 380 * Routing/forwarding table locking notes: 381 * 382 * Lock acquisition order: Radix tree lock, irb_lock. 383 * Requirements: 384 * i. Walker must not hold any locks during the walker callback. 385 * ii Walker must not see a truncated tree during the walk because of any node 386 * deletion. 387 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 388 * in many places in the code to walk the irb list. Thus even if all the 389 * ires in a bucket have been deleted, we still can't free the radix node 390 * until the ires have actually been inactive'd (freed). 391 * 392 * Tree traversal - Need to hold the global tree lock in read mode. 393 * Before dropping the global tree lock, need to either increment the ire_refcnt 394 * to ensure that the radix node can't be deleted. 395 * 396 * Tree add - Need to hold the global tree lock in write mode to add a 397 * radix node. To prevent the node from being deleted, increment the 398 * irb_refcnt, after the node is added to the tree. The ire itself is 399 * added later while holding the irb_lock, but not the tree lock. 400 * 401 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 402 * All associated ires must be inactive (i.e. freed), and irb_refcnt 403 * must be zero. 404 * 405 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 406 * global tree lock (read mode) for traversal. 407 * 408 * IPsec notes : 409 * 410 * IP interacts with the IPsec code (AH/ESP) by tagging a M_CTL message 411 * in front of the actual packet. For outbound datagrams, the M_CTL 412 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 413 * information used by the IPsec code for applying the right level of 414 * protection. The information initialized by IP in the ipsec_out_t 415 * is determined by the per-socket policy or global policy in the system. 416 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 417 * ipsec_info.h) which starts out with nothing in it. It gets filled 418 * with the right information if it goes through the AH/ESP code, which 419 * happens if the incoming packet is secure. The information initialized 420 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 421 * the policy requirements needed by per-socket policy or global policy 422 * is met or not. 423 * 424 * If there is both per-socket policy (set using setsockopt) and there 425 * is also global policy match for the 5 tuples of the socket, 426 * ipsec_override_policy() makes the decision of which one to use. 427 * 428 * For fully connected sockets i.e dst, src [addr, port] is known, 429 * conn_policy_cached is set indicating that policy has been cached. 430 * conn_in_enforce_policy may or may not be set depending on whether 431 * there is a global policy match or per-socket policy match. 432 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 433 * Once the right policy is set on the conn_t, policy cannot change for 434 * this socket. This makes life simpler for TCP (UDP ?) where 435 * re-transmissions go out with the same policy. For symmetry, policy 436 * is cached for fully connected UDP sockets also. Thus if policy is cached, 437 * it also implies that policy is latched i.e policy cannot change 438 * on these sockets. As we have the right policy on the conn, we don't 439 * have to lookup global policy for every outbound and inbound datagram 440 * and thus serving as an optimization. Note that a global policy change 441 * does not affect fully connected sockets if they have policy. If fully 442 * connected sockets did not have any policy associated with it, global 443 * policy change may affect them. 444 * 445 * IP Flow control notes: 446 * 447 * Non-TCP streams are flow controlled by IP. On the send side, if the packet 448 * cannot be sent down to the driver by IP, because of a canput failure, IP 449 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. 450 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained 451 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the 452 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the 453 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains 454 * the queued messages, and removes the conn from the drain list, if all 455 * messages were drained. It also qenables the next conn in the drain list to 456 * continue the drain process. 457 * 458 * In reality the drain list is not a single list, but a configurable number 459 * of lists. The ip_wsrv on the IP module, qenables the first conn in each 460 * list. If the ip_wsrv of the next qenabled conn does not run, because the 461 * stream closes, ip_close takes responsibility to qenable the next conn in 462 * the drain list. The directly called ip_wput path always does a putq, if 463 * it cannot putnext. Thus synchronization problems are handled between 464 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 465 * functions that manipulate this drain list. Furthermore conn_drain_insert 466 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv 467 * running on a queue at any time. conn_drain_tail can be simultaneously called 468 * from both ip_wsrv and ip_close. 469 * 470 * IPQOS notes: 471 * 472 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 473 * and IPQoS modules. IPPF includes hooks in IP at different control points 474 * (callout positions) which direct packets to IPQoS modules for policy 475 * processing. Policies, if present, are global. 476 * 477 * The callout positions are located in the following paths: 478 * o local_in (packets destined for this host) 479 * o local_out (packets orginating from this host ) 480 * o fwd_in (packets forwarded by this m/c - inbound) 481 * o fwd_out (packets forwarded by this m/c - outbound) 482 * Hooks at these callout points can be enabled/disabled using the ndd variable 483 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 484 * By default all the callout positions are enabled. 485 * 486 * Outbound (local_out) 487 * Hooks are placed in ip_wput_ire and ipsec_out_process. 488 * 489 * Inbound (local_in) 490 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 491 * TCP and UDP fanout routines. 492 * 493 * Forwarding (in and out) 494 * Hooks are placed in ip_rput_forward. 495 * 496 * IP Policy Framework processing (IPPF processing) 497 * Policy processing for a packet is initiated by ip_process, which ascertains 498 * that the classifier (ipgpc) is loaded and configured, failing which the 499 * packet resumes normal processing in IP. If the clasifier is present, the 500 * packet is acted upon by one or more IPQoS modules (action instances), per 501 * filters configured in ipgpc and resumes normal IP processing thereafter. 502 * An action instance can drop a packet in course of its processing. 503 * 504 * A boolean variable, ip_policy, is used in all the fanout routines that can 505 * invoke ip_process for a packet. This variable indicates if the packet should 506 * to be sent for policy processing. The variable is set to B_TRUE by default, 507 * i.e. when the routines are invoked in the normal ip procesing path for a 508 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 509 * ip_policy is set to B_FALSE for all the routines called in these two 510 * functions because, in the former case, we don't process loopback traffic 511 * currently while in the latter, the packets have already been processed in 512 * icmp_inbound. 513 * 514 * Zones notes: 515 * 516 * The partitioning rules for networking are as follows: 517 * 1) Packets coming from a zone must have a source address belonging to that 518 * zone. 519 * 2) Packets coming from a zone can only be sent on a physical interface on 520 * which the zone has an IP address. 521 * 3) Between two zones on the same machine, packet delivery is only allowed if 522 * there's a matching route for the destination and zone in the forwarding 523 * table. 524 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 525 * different zones can bind to the same port with the wildcard address 526 * (INADDR_ANY). 527 * 528 * The granularity of interface partitioning is at the logical interface level. 529 * Therefore, every zone has its own IP addresses, and incoming packets can be 530 * attributed to a zone unambiguously. A logical interface is placed into a zone 531 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 532 * structure. Rule (1) is implemented by modifying the source address selection 533 * algorithm so that the list of eligible addresses is filtered based on the 534 * sending process zone. 535 * 536 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 537 * across all zones, depending on their type. Here is the break-up: 538 * 539 * IRE type Shared/exclusive 540 * -------- ---------------- 541 * IRE_BROADCAST Exclusive 542 * IRE_DEFAULT (default routes) Shared (*) 543 * IRE_LOCAL Exclusive (x) 544 * IRE_LOOPBACK Exclusive 545 * IRE_PREFIX (net routes) Shared (*) 546 * IRE_CACHE Exclusive 547 * IRE_IF_NORESOLVER (interface routes) Exclusive 548 * IRE_IF_RESOLVER (interface routes) Exclusive 549 * IRE_HOST (host routes) Shared (*) 550 * 551 * (*) A zone can only use a default or off-subnet route if the gateway is 552 * directly reachable from the zone, that is, if the gateway's address matches 553 * one of the zone's logical interfaces. 554 * 555 * (x) IRE_LOCAL are handled a bit differently, since for all other entries 556 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source 557 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP 558 * address of the zone itself (the destination). Since IRE_LOCAL is used 559 * for communication between zones, ip_wput_ire has special logic to set 560 * the right source address when sending using an IRE_LOCAL. 561 * 562 * Furthermore, when ip_restrict_interzone_loopback is set (the default), 563 * ire_cache_lookup restricts loopback using an IRE_LOCAL 564 * between zone to the case when L2 would have conceptually looped the packet 565 * back, i.e. the loopback which is required since neither Ethernet drivers 566 * nor Ethernet hardware loops them back. This is the case when the normal 567 * routes (ignoring IREs with different zoneids) would send out the packet on 568 * the same ill (or ill group) as the ill with which is IRE_LOCAL is 569 * associated. 570 * 571 * Multiple zones can share a common broadcast address; typically all zones 572 * share the 255.255.255.255 address. Incoming as well as locally originated 573 * broadcast packets must be dispatched to all the zones on the broadcast 574 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 575 * since some zones may not be on the 10.16.72/24 network. To handle this, each 576 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 577 * sent to every zone that has an IRE_BROADCAST entry for the destination 578 * address on the input ill, see conn_wantpacket(). 579 * 580 * Applications in different zones can join the same multicast group address. 581 * For IPv4, group memberships are per-logical interface, so they're already 582 * inherently part of a zone. For IPv6, group memberships are per-physical 583 * interface, so we distinguish IPv6 group memberships based on group address, 584 * interface and zoneid. In both cases, received multicast packets are sent to 585 * every zone for which a group membership entry exists. On IPv6 we need to 586 * check that the target zone still has an address on the receiving physical 587 * interface; it could have been removed since the application issued the 588 * IPV6_JOIN_GROUP. 589 */ 590 591 /* 592 * Squeue Fanout flags: 593 * 0: No fanout. 594 * 1: Fanout across all squeues 595 */ 596 boolean_t ip_squeue_fanout = 0; 597 598 /* 599 * Maximum dups allowed per packet. 600 */ 601 uint_t ip_max_frag_dups = 10; 602 603 #define IS_SIMPLE_IPH(ipha) \ 604 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 605 606 /* RFC1122 Conformance */ 607 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 608 609 #define ILL_MAX_NAMELEN LIFNAMSIZ 610 611 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 612 613 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t); 614 615 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t, 616 ip_stack_t *); 617 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 618 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 619 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); 620 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, 621 mblk_t *, int, ip_stack_t *); 622 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 623 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 624 ill_t *, zoneid_t); 625 static void icmp_options_update(ipha_t *); 626 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t, 627 ip_stack_t *); 628 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, 629 zoneid_t zoneid, ip_stack_t *); 630 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *); 631 static void icmp_redirect(ill_t *, mblk_t *); 632 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t, 633 ip_stack_t *); 634 635 static void ip_arp_news(queue_t *, mblk_t *); 636 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *, 637 ip_stack_t *); 638 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 639 char *ip_dot_addr(ipaddr_t, char *); 640 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 641 int ip_close(queue_t *, int); 642 static char *ip_dot_saddr(uchar_t *, char *); 643 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 644 boolean_t, boolean_t, ill_t *, zoneid_t); 645 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 646 boolean_t, boolean_t, zoneid_t); 647 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 648 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 649 static void ip_lrput(queue_t *, mblk_t *); 650 ipaddr_t ip_net_mask(ipaddr_t); 651 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, conn_t *, zoneid_t, 652 ip_stack_t *); 653 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 654 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *); 655 char *ip_nv_lookup(nv_t *, int); 656 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 657 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 658 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 659 static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t, 660 ipndp_t *, size_t); 661 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 662 void ip_rput(queue_t *, mblk_t *); 663 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 664 void *dummy_arg); 665 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 666 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *, 667 ip_stack_t *); 668 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 669 ire_t *, ip_stack_t *); 670 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *, 671 mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *); 672 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *, 673 ip_stack_t *); 674 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, 675 uint16_t *); 676 int ip_snmp_get(queue_t *, mblk_t *); 677 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, 678 mib2_ipIfStatsEntry_t *, ip_stack_t *); 679 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *, 680 ip_stack_t *); 681 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *); 682 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst); 683 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst); 684 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst); 685 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst); 686 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *, 687 ip_stack_t *ipst); 688 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *, 689 ip_stack_t *ipst); 690 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *, 691 ip_stack_t *ipst); 692 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *, 693 ip_stack_t *ipst); 694 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *, 695 ip_stack_t *ipst); 696 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *, 697 ip_stack_t *ipst); 698 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *, 699 ip_stack_t *ipst); 700 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *, 701 ip_stack_t *ipst); 702 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, 703 ip_stack_t *ipst); 704 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, 705 ip_stack_t *ipst); 706 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 707 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 708 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); 709 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 710 static boolean_t ip_source_routed(ipha_t *, ip_stack_t *); 711 static boolean_t ip_source_route_included(ipha_t *); 712 static void ip_trash_ire_reclaim_stack(ip_stack_t *); 713 714 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, 715 zoneid_t, ip_stack_t *); 716 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *); 717 static void ip_wput_local_options(ipha_t *, ip_stack_t *); 718 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 719 zoneid_t, ip_stack_t *); 720 721 static void conn_drain_init(ip_stack_t *); 722 static void conn_drain_fini(ip_stack_t *); 723 static void conn_drain_tail(conn_t *connp, boolean_t closing); 724 725 static void conn_walk_drain(ip_stack_t *); 726 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, 727 zoneid_t); 728 729 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns); 730 static void ip_stack_shutdown(netstackid_t stackid, void *arg); 731 static void ip_stack_fini(netstackid_t stackid, void *arg); 732 733 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 734 zoneid_t); 735 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 736 void *dummy_arg); 737 738 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 739 740 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 741 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 742 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 743 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 744 745 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 746 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 747 caddr_t, cred_t *); 748 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, 749 caddr_t cp, cred_t *cr); 750 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, 751 cred_t *); 752 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 753 caddr_t cp, cred_t *cr); 754 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 755 cred_t *); 756 static int ipmp_hook_emulation_set(queue_t *, mblk_t *, char *, caddr_t, 757 cred_t *); 758 static squeue_func_t ip_squeue_switch(int); 759 760 static void *ip_kstat_init(netstackid_t, ip_stack_t *); 761 static void ip_kstat_fini(netstackid_t, kstat_t *); 762 static int ip_kstat_update(kstat_t *kp, int rw); 763 static void *icmp_kstat_init(netstackid_t); 764 static void icmp_kstat_fini(netstackid_t, kstat_t *); 765 static int icmp_kstat_update(kstat_t *kp, int rw); 766 static void *ip_kstat2_init(netstackid_t, ip_stat_t *); 767 static void ip_kstat2_fini(netstackid_t, kstat_t *); 768 769 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); 770 771 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 772 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 773 774 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 775 ipha_t *, ill_t *, boolean_t); 776 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 777 778 /* How long, in seconds, we allow frags to hang around. */ 779 #define IP_FRAG_TIMEOUT 60 780 781 /* 782 * Threshold which determines whether MDT should be used when 783 * generating IP fragments; payload size must be greater than 784 * this threshold for MDT to take place. 785 */ 786 #define IP_WPUT_FRAG_MDT_MIN 32768 787 788 /* Setable in /etc/system only */ 789 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 790 791 static long ip_rput_pullups; 792 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 793 794 vmem_t *ip_minor_arena; 795 796 int ip_debug; 797 798 #ifdef DEBUG 799 uint32_t ipsechw_debug = 0; 800 #endif 801 802 /* 803 * Multirouting/CGTP stuff 804 */ 805 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 806 807 /* 808 * XXX following really should only be in a header. Would need more 809 * header and .c clean up first. 810 */ 811 extern optdb_obj_t ip_opt_obj; 812 813 ulong_t ip_squeue_enter_unbound = 0; 814 815 /* 816 * Named Dispatch Parameter Table. 817 * All of these are alterable, within the min/max values given, at run time. 818 */ 819 static ipparam_t lcl_param_arr[] = { 820 /* min max value name */ 821 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 822 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 823 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 824 { 0, 1, 0, "ip_respond_to_timestamp"}, 825 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 826 { 0, 1, 1, "ip_send_redirects"}, 827 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 828 { 0, 10, 0, "ip_debug"}, 829 { 0, 10, 0, "ip_mrtdebug"}, 830 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 831 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 832 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 833 { 1, 255, 255, "ip_def_ttl" }, 834 { 0, 1, 0, "ip_forward_src_routed"}, 835 { 0, 256, 32, "ip_wroff_extra" }, 836 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 837 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 838 { 0, 1, 1, "ip_path_mtu_discovery" }, 839 { 0, 240, 30, "ip_ignore_delete_time" }, 840 { 0, 1, 0, "ip_ignore_redirect" }, 841 { 0, 1, 1, "ip_output_queue" }, 842 { 1, 254, 1, "ip_broadcast_ttl" }, 843 { 0, 99999, 100, "ip_icmp_err_interval" }, 844 { 1, 99999, 10, "ip_icmp_err_burst" }, 845 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 846 { 0, 1, 0, "ip_strict_dst_multihoming" }, 847 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 848 { 0, 1, 0, "ipsec_override_persocket_policy" }, 849 { 0, 1, 1, "icmp_accept_clear_messages" }, 850 { 0, 1, 1, "igmp_accept_clear_messages" }, 851 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 852 "ip_ndp_delay_first_probe_time"}, 853 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 854 "ip_ndp_max_unicast_solicit"}, 855 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 856 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 857 { 0, 1, 0, "ip6_forward_src_routed"}, 858 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 859 { 0, 1, 1, "ip6_send_redirects"}, 860 { 0, 1, 0, "ip6_ignore_redirect" }, 861 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 862 863 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 864 865 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 866 867 { 0, 1, 1, "pim_accept_clear_messages" }, 868 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 869 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 870 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 871 { 0, 15, 0, "ip_policy_mask" }, 872 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 873 { 0, 255, 1, "ip_multirt_ttl" }, 874 { 0, 1, 1, "ip_multidata_outbound" }, 875 { 0, 3600000, 300000, "ip_ndp_defense_interval" }, 876 { 0, 999999, 60*60*24, "ip_max_temp_idle" }, 877 { 0, 1000, 1, "ip_max_temp_defend" }, 878 { 0, 1000, 3, "ip_max_defend" }, 879 { 0, 999999, 30, "ip_defend_interval" }, 880 { 0, 3600000, 300000, "ip_dup_recovery" }, 881 { 0, 1, 1, "ip_restrict_interzone_loopback" }, 882 { 0, 1, 1, "ip_lso_outbound" }, 883 { IGMP_V1_ROUTER, IGMP_V3_ROUTER, IGMP_V3_ROUTER, "igmp_max_version" }, 884 { MLD_V1_ROUTER, MLD_V2_ROUTER, MLD_V2_ROUTER, "mld_max_version" }, 885 #ifdef DEBUG 886 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 887 #else 888 { 0, 0, 0, "" }, 889 #endif 890 }; 891 892 /* 893 * Extended NDP table 894 * The addresses for the first two are filled in to be ips_ip_g_forward 895 * and ips_ipv6_forward at init time. 896 */ 897 static ipndp_t lcl_ndp_arr[] = { 898 /* getf setf data name */ 899 #define IPNDP_IP_FORWARDING_OFFSET 0 900 { ip_param_generic_get, ip_forward_set, NULL, 901 "ip_forwarding" }, 902 #define IPNDP_IP6_FORWARDING_OFFSET 1 903 { ip_param_generic_get, ip_forward_set, NULL, 904 "ip6_forwarding" }, 905 { ip_ill_report, NULL, NULL, 906 "ip_ill_status" }, 907 { ip_ipif_report, NULL, NULL, 908 "ip_ipif_status" }, 909 { ip_ire_report, NULL, NULL, 910 "ipv4_ire_status" }, 911 { ip_ire_report_v6, NULL, NULL, 912 "ipv6_ire_status" }, 913 { ip_conn_report, NULL, NULL, 914 "ip_conn_status" }, 915 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, 916 "ip_rput_pullups" }, 917 { ndp_report, NULL, NULL, 918 "ip_ndp_cache_report" }, 919 { ip_srcid_report, NULL, NULL, 920 "ip_srcid_status" }, 921 { ip_param_generic_get, ip_squeue_profile_set, 922 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, 923 { ip_param_generic_get, ip_squeue_bind_set, 924 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, 925 { ip_param_generic_get, ip_input_proc_set, 926 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 927 { ip_param_generic_get, ip_int_set, 928 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 929 #define IPNDP_CGTP_FILTER_OFFSET 14 930 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL, 931 "ip_cgtp_filter" }, 932 { ip_param_generic_get, ip_int_set, 933 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" }, 934 #define IPNDP_IPMP_HOOK_OFFSET 16 935 { ip_param_generic_get, ipmp_hook_emulation_set, NULL, 936 "ipmp_hook_emulation" }, 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, 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 ARP_CMD, ip_sioctl_arp, NULL }, 1018 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, 1019 ARP_CMD, ip_sioctl_arp, NULL }, 1020 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, 1021 ARP_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, 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, 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, 1262 ip_sioctl_get_lifconf, NULL }, 1263 1264 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, 1265 XARP_CMD, ip_sioctl_arp, NULL }, 1266 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, 1267 XARP_CMD, ip_sioctl_arp, NULL }, 1268 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, 1269 XARP_CMD, ip_sioctl_arp, 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 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1296 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1297 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1298 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1299 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1300 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1301 MSFILT_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 } 1755 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks); 1756 break; 1757 case ICMP_ADDRESS_MASK_REPLY: 1758 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps); 1759 break; 1760 default: 1761 interested = B_TRUE; /* Pass up to transport */ 1762 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns); 1763 break; 1764 } 1765 /* See if there is an ICMP client. */ 1766 if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) { 1767 /* If there is an ICMP client and we want one too, copy it. */ 1768 mblk_t *first_mp1; 1769 1770 if (!interested) { 1771 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1772 ip_policy, recv_ill, zoneid); 1773 return; 1774 } 1775 first_mp1 = ip_copymsg(first_mp); 1776 if (first_mp1 != NULL) { 1777 ip_fanout_proto(q, first_mp1, ill, ipha, 1778 0, mctl_present, ip_policy, recv_ill, zoneid); 1779 } 1780 } else if (!interested) { 1781 freemsg(first_mp); 1782 return; 1783 } else { 1784 /* 1785 * Initiate policy processing for this packet if ip_policy 1786 * is true. 1787 */ 1788 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 1789 ill_index = ill->ill_phyint->phyint_ifindex; 1790 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1791 if (mp == NULL) { 1792 if (mctl_present) { 1793 freeb(first_mp); 1794 } 1795 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1796 return; 1797 } 1798 } 1799 } 1800 /* We want to do something with it. */ 1801 /* Check db_ref to make sure we can modify the packet. */ 1802 if (mp->b_datap->db_ref > 1) { 1803 mblk_t *first_mp1; 1804 1805 first_mp1 = ip_copymsg(first_mp); 1806 freemsg(first_mp); 1807 if (!first_mp1) { 1808 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1809 return; 1810 } 1811 first_mp = first_mp1; 1812 if (mctl_present) { 1813 mp = first_mp->b_cont; 1814 ASSERT(mp != NULL); 1815 } else { 1816 mp = first_mp; 1817 } 1818 ipha = (ipha_t *)mp->b_rptr; 1819 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1820 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1821 } 1822 switch (icmph->icmph_type) { 1823 case ICMP_ADDRESS_MASK_REQUEST: 1824 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1825 if (ipif == NULL) { 1826 freemsg(first_mp); 1827 return; 1828 } 1829 /* 1830 * outging interface must be IPv4 1831 */ 1832 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1833 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1834 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1835 ipif_refrele(ipif); 1836 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps); 1837 break; 1838 case ICMP_ECHO_REQUEST: 1839 icmph->icmph_type = ICMP_ECHO_REPLY; 1840 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps); 1841 break; 1842 case ICMP_TIME_STAMP_REQUEST: { 1843 uint32_t *tsp; 1844 1845 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1846 tsp = (uint32_t *)wptr; 1847 tsp++; /* Skip past 'originate time' */ 1848 /* Compute # of milliseconds since midnight */ 1849 gethrestime(&now); 1850 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1851 now.tv_nsec / (NANOSEC / MILLISEC); 1852 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1853 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1854 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps); 1855 break; 1856 } 1857 default: 1858 ipha = (ipha_t *)&icmph[1]; 1859 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1860 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1861 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1862 freemsg(first_mp); 1863 return; 1864 } 1865 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1866 ipha = (ipha_t *)&icmph[1]; 1867 } 1868 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1869 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1870 freemsg(first_mp); 1871 return; 1872 } 1873 hdr_length = IPH_HDR_LENGTH(ipha); 1874 if (hdr_length < sizeof (ipha_t)) { 1875 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1876 freemsg(first_mp); 1877 return; 1878 } 1879 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 1880 if (!pullupmsg(mp, 1881 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 1882 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1883 freemsg(first_mp); 1884 return; 1885 } 1886 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1887 ipha = (ipha_t *)&icmph[1]; 1888 } 1889 switch (icmph->icmph_type) { 1890 case ICMP_REDIRECT: 1891 /* 1892 * As there is no upper client to deliver, we don't 1893 * need the first_mp any more. 1894 */ 1895 if (mctl_present) { 1896 freeb(first_mp); 1897 } 1898 icmp_redirect(ill, mp); 1899 return; 1900 case ICMP_DEST_UNREACHABLE: 1901 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 1902 if (!icmp_inbound_too_big(icmph, ipha, ill, 1903 zoneid, mp, iph_hdr_length, ipst)) { 1904 freemsg(first_mp); 1905 return; 1906 } 1907 /* 1908 * icmp_inbound_too_big() may alter mp. 1909 * Resynch ipha and icmph accordingly. 1910 */ 1911 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1912 ipha = (ipha_t *)&icmph[1]; 1913 } 1914 /* FALLTHRU */ 1915 default : 1916 /* 1917 * IPQoS notes: Since we have already done IPQoS 1918 * processing we don't want to do it again in 1919 * the fanout routines called by 1920 * icmp_inbound_error_fanout, hence the last 1921 * argument, ip_policy, is B_FALSE. 1922 */ 1923 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 1924 ipha, iph_hdr_length, hdr_length, mctl_present, 1925 B_FALSE, recv_ill, zoneid); 1926 } 1927 return; 1928 } 1929 /* Send out an ICMP packet */ 1930 icmph->icmph_checksum = 0; 1931 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 1932 if (broadcast || CLASSD(ipha->ipha_dst)) { 1933 ipif_t *ipif_chosen; 1934 /* 1935 * Make it look like it was directed to us, so we don't look 1936 * like a fool with a broadcast or multicast source address. 1937 */ 1938 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1939 /* 1940 * Make sure that we haven't grabbed an interface that's DOWN. 1941 */ 1942 if (ipif != NULL) { 1943 ipif_chosen = ipif_select_source(ipif->ipif_ill, 1944 ipha->ipha_src, zoneid); 1945 if (ipif_chosen != NULL) { 1946 ipif_refrele(ipif); 1947 ipif = ipif_chosen; 1948 } 1949 } 1950 if (ipif == NULL) { 1951 ip0dbg(("icmp_inbound: " 1952 "No source for broadcast/multicast:\n" 1953 "\tsrc 0x%x dst 0x%x ill %p " 1954 "ipif_lcl_addr 0x%x\n", 1955 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 1956 (void *)ill, 1957 ill->ill_ipif->ipif_lcl_addr)); 1958 freemsg(first_mp); 1959 return; 1960 } 1961 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1962 ipha->ipha_dst = ipif->ipif_src_addr; 1963 ipif_refrele(ipif); 1964 } 1965 /* Reset time to live. */ 1966 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 1967 { 1968 /* Swap source and destination addresses */ 1969 ipaddr_t tmp; 1970 1971 tmp = ipha->ipha_src; 1972 ipha->ipha_src = ipha->ipha_dst; 1973 ipha->ipha_dst = tmp; 1974 } 1975 ipha->ipha_ident = 0; 1976 if (!IS_SIMPLE_IPH(ipha)) 1977 icmp_options_update(ipha); 1978 1979 /* 1980 * ICMP echo replies should go out on the same interface 1981 * the request came on as probes used by in.mpathd for detecting 1982 * NIC failures are ECHO packets. We turn-off load spreading 1983 * by setting ipsec_in_attach_if to B_TRUE, which is copied 1984 * to ipsec_out_attach_if by ipsec_in_to_out called later in this 1985 * function. This is in turn handled by ip_wput and ip_newroute 1986 * to make sure that the packet goes out on the interface it came 1987 * in on. If we don't turnoff load spreading, the packets might get 1988 * dropped if there are no non-FAILED/INACTIVE interfaces for it 1989 * to go out and in.mpathd would wrongly detect a failure or 1990 * mis-detect a NIC failure for link failure. As load spreading 1991 * can happen only if ill_group is not NULL, we do only for 1992 * that case and this does not affect the normal case. 1993 * 1994 * We turn off load spreading only on echo packets that came from 1995 * on-link hosts. If the interface route has been deleted, this will 1996 * not be enforced as we can't do much. For off-link hosts, as the 1997 * default routes in IPv4 does not typically have an ire_ipif 1998 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. 1999 * Moreover, expecting a default route through this interface may 2000 * not be correct. We use ipha_dst because of the swap above. 2001 */ 2002 onlink = B_FALSE; 2003 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { 2004 /* 2005 * First, we need to make sure that it is not one of our 2006 * local addresses. If we set onlink when it is one of 2007 * our local addresses, we will end up creating IRE_CACHES 2008 * for one of our local addresses. Then, we will never 2009 * accept packets for them afterwards. 2010 */ 2011 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, 2012 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2013 if (src_ire == NULL) { 2014 ipif = ipif_get_next_ipif(NULL, ill); 2015 if (ipif == NULL) { 2016 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2017 freemsg(mp); 2018 return; 2019 } 2020 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 2021 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 2022 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE, ipst); 2023 ipif_refrele(ipif); 2024 if (src_ire != NULL) { 2025 onlink = B_TRUE; 2026 ire_refrele(src_ire); 2027 } 2028 } else { 2029 ire_refrele(src_ire); 2030 } 2031 } 2032 if (!mctl_present) { 2033 /* 2034 * This packet should go out the same way as it 2035 * came in i.e in clear. To make sure that global 2036 * policy will not be applied to this in ip_wput_ire, 2037 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2038 */ 2039 ASSERT(first_mp == mp); 2040 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2041 if (first_mp == NULL) { 2042 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2043 freemsg(mp); 2044 return; 2045 } 2046 ii = (ipsec_in_t *)first_mp->b_rptr; 2047 2048 /* This is not a secure packet */ 2049 ii->ipsec_in_secure = B_FALSE; 2050 if (onlink) { 2051 ii->ipsec_in_attach_if = B_TRUE; 2052 ii->ipsec_in_ill_index = 2053 ill->ill_phyint->phyint_ifindex; 2054 ii->ipsec_in_rill_index = 2055 recv_ill->ill_phyint->phyint_ifindex; 2056 } 2057 first_mp->b_cont = mp; 2058 } else if (onlink) { 2059 ii = (ipsec_in_t *)first_mp->b_rptr; 2060 ii->ipsec_in_attach_if = B_TRUE; 2061 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 2062 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; 2063 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2064 } else { 2065 ii = (ipsec_in_t *)first_mp->b_rptr; 2066 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2067 } 2068 ii->ipsec_in_zoneid = zoneid; 2069 ASSERT(zoneid != ALL_ZONES); 2070 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2071 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2072 return; 2073 } 2074 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 2075 put(WR(q), first_mp); 2076 } 2077 2078 static ipaddr_t 2079 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) 2080 { 2081 conn_t *connp; 2082 connf_t *connfp; 2083 ipaddr_t nexthop_addr = INADDR_ANY; 2084 int hdr_length = IPH_HDR_LENGTH(ipha); 2085 uint16_t *up; 2086 uint32_t ports; 2087 ip_stack_t *ipst = ill->ill_ipst; 2088 2089 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2090 switch (ipha->ipha_protocol) { 2091 case IPPROTO_TCP: 2092 { 2093 tcph_t *tcph; 2094 2095 /* do a reverse lookup */ 2096 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2097 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, 2098 TCPS_LISTEN, ipst); 2099 break; 2100 } 2101 case IPPROTO_UDP: 2102 { 2103 uint32_t dstport, srcport; 2104 2105 ((uint16_t *)&ports)[0] = up[1]; 2106 ((uint16_t *)&ports)[1] = up[0]; 2107 2108 /* Extract ports in net byte order */ 2109 dstport = htons(ntohl(ports) & 0xFFFF); 2110 srcport = htons(ntohl(ports) >> 16); 2111 2112 connfp = &ipst->ips_ipcl_udp_fanout[ 2113 IPCL_UDP_HASH(dstport, ipst)]; 2114 mutex_enter(&connfp->connf_lock); 2115 connp = connfp->connf_head; 2116 2117 /* do a reverse lookup */ 2118 while ((connp != NULL) && 2119 (!IPCL_UDP_MATCH(connp, dstport, 2120 ipha->ipha_src, srcport, ipha->ipha_dst) || 2121 !IPCL_ZONE_MATCH(connp, zoneid))) { 2122 connp = connp->conn_next; 2123 } 2124 if (connp != NULL) 2125 CONN_INC_REF(connp); 2126 mutex_exit(&connfp->connf_lock); 2127 break; 2128 } 2129 case IPPROTO_SCTP: 2130 { 2131 in6_addr_t map_src, map_dst; 2132 2133 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); 2134 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); 2135 ((uint16_t *)&ports)[0] = up[1]; 2136 ((uint16_t *)&ports)[1] = up[0]; 2137 2138 connp = sctp_find_conn(&map_src, &map_dst, ports, 2139 zoneid, ipst->ips_netstack->netstack_sctp); 2140 if (connp == NULL) { 2141 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, 2142 zoneid, ports, ipha, ipst); 2143 } else { 2144 CONN_INC_REF(connp); 2145 SCTP_REFRELE(CONN2SCTP(connp)); 2146 } 2147 break; 2148 } 2149 default: 2150 { 2151 ipha_t ripha; 2152 2153 ripha.ipha_src = ipha->ipha_dst; 2154 ripha.ipha_dst = ipha->ipha_src; 2155 ripha.ipha_protocol = ipha->ipha_protocol; 2156 2157 connfp = &ipst->ips_ipcl_proto_fanout[ 2158 ipha->ipha_protocol]; 2159 mutex_enter(&connfp->connf_lock); 2160 connp = connfp->connf_head; 2161 for (connp = connfp->connf_head; connp != NULL; 2162 connp = connp->conn_next) { 2163 if (IPCL_PROTO_MATCH(connp, 2164 ipha->ipha_protocol, &ripha, ill, 2165 0, zoneid)) { 2166 CONN_INC_REF(connp); 2167 break; 2168 } 2169 } 2170 mutex_exit(&connfp->connf_lock); 2171 } 2172 } 2173 if (connp != NULL) { 2174 if (connp->conn_nexthop_set) 2175 nexthop_addr = connp->conn_nexthop_v4; 2176 CONN_DEC_REF(connp); 2177 } 2178 return (nexthop_addr); 2179 } 2180 2181 /* Table from RFC 1191 */ 2182 static int icmp_frag_size_table[] = 2183 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2184 2185 /* 2186 * Process received ICMP Packet too big. 2187 * After updating any IRE it does the fanout to any matching transport streams. 2188 * Assumes the message has been pulled up till the IP header that caused 2189 * the error. 2190 * 2191 * Returns B_FALSE on failure and B_TRUE on success. 2192 */ 2193 static boolean_t 2194 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, 2195 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length, 2196 ip_stack_t *ipst) 2197 { 2198 ire_t *ire, *first_ire; 2199 int mtu; 2200 int hdr_length; 2201 ipaddr_t nexthop_addr; 2202 2203 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 2204 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 2205 ASSERT(ill != NULL); 2206 2207 hdr_length = IPH_HDR_LENGTH(ipha); 2208 2209 /* Drop if the original packet contained a source route */ 2210 if (ip_source_route_included(ipha)) { 2211 return (B_FALSE); 2212 } 2213 /* 2214 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport 2215 * header. 2216 */ 2217 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2218 mp->b_wptr) { 2219 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2220 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2221 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2222 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n")); 2223 return (B_FALSE); 2224 } 2225 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2226 ipha = (ipha_t *)&icmph[1]; 2227 } 2228 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp); 2229 if (nexthop_addr != INADDR_ANY) { 2230 /* nexthop set */ 2231 first_ire = ire_ctable_lookup(ipha->ipha_dst, 2232 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp), 2233 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, ipst); 2234 } else { 2235 /* nexthop not set */ 2236 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, 2237 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 2238 } 2239 2240 if (!first_ire) { 2241 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", 2242 ntohl(ipha->ipha_dst))); 2243 return (B_FALSE); 2244 } 2245 /* Check for MTU discovery advice as described in RFC 1191 */ 2246 mtu = ntohs(icmph->icmph_du_mtu); 2247 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2248 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2249 ire = ire->ire_next) { 2250 /* 2251 * Look for the connection to which this ICMP message is 2252 * directed. If it has the IP_NEXTHOP option set, then the 2253 * search is limited to IREs with the MATCH_IRE_PRIVATE 2254 * option. Else the search is limited to regular IREs. 2255 */ 2256 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2257 (nexthop_addr != ire->ire_gateway_addr)) || 2258 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2259 (nexthop_addr != INADDR_ANY))) 2260 continue; 2261 2262 mutex_enter(&ire->ire_lock); 2263 if (icmph->icmph_du_zero == 0 && mtu > 68) { 2264 /* Reduce the IRE max frag value as advised. */ 2265 ip1dbg(("Received mtu from router: %d (was %d)\n", 2266 mtu, ire->ire_max_frag)); 2267 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2268 } else { 2269 uint32_t length; 2270 int i; 2271 2272 /* 2273 * Use the table from RFC 1191 to figure out 2274 * the next "plateau" based on the length in 2275 * the original IP packet. 2276 */ 2277 length = ntohs(ipha->ipha_length); 2278 if (ire->ire_max_frag <= length && 2279 ire->ire_max_frag >= length - hdr_length) { 2280 /* 2281 * Handle broken BSD 4.2 systems that 2282 * return the wrong iph_length in ICMP 2283 * errors. 2284 */ 2285 ip1dbg(("Wrong mtu: sent %d, ire %d\n", 2286 length, ire->ire_max_frag)); 2287 length -= hdr_length; 2288 } 2289 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2290 if (length > icmp_frag_size_table[i]) 2291 break; 2292 } 2293 if (i == A_CNT(icmp_frag_size_table)) { 2294 /* Smaller than 68! */ 2295 ip1dbg(("Too big for packet size %d\n", 2296 length)); 2297 ire->ire_max_frag = MIN(ire->ire_max_frag, 576); 2298 ire->ire_frag_flag = 0; 2299 } else { 2300 mtu = icmp_frag_size_table[i]; 2301 ip1dbg(("Calculated mtu %d, packet size %d, " 2302 "before %d", mtu, length, 2303 ire->ire_max_frag)); 2304 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2305 ip1dbg((", after %d\n", ire->ire_max_frag)); 2306 } 2307 /* Record the new max frag size for the ULP. */ 2308 icmph->icmph_du_zero = 0; 2309 icmph->icmph_du_mtu = 2310 htons((uint16_t)ire->ire_max_frag); 2311 } 2312 mutex_exit(&ire->ire_lock); 2313 } 2314 rw_exit(&first_ire->ire_bucket->irb_lock); 2315 ire_refrele(first_ire); 2316 return (B_TRUE); 2317 } 2318 2319 /* 2320 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2321 * calls this function. 2322 */ 2323 static mblk_t * 2324 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2325 { 2326 ipha_t *ipha; 2327 icmph_t *icmph; 2328 ipha_t *in_ipha; 2329 int length; 2330 2331 ASSERT(mp->b_datap->db_type == M_DATA); 2332 2333 /* 2334 * For Self-encapsulated packets, we added an extra IP header 2335 * without the options. Inner IP header is the one from which 2336 * the outer IP header was formed. Thus, we need to remove the 2337 * outer IP header. To do this, we pullup the whole message 2338 * and overlay whatever follows the outer IP header over the 2339 * outer IP header. 2340 */ 2341 2342 if (!pullupmsg(mp, -1)) 2343 return (NULL); 2344 2345 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2346 ipha = (ipha_t *)&icmph[1]; 2347 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2348 2349 /* 2350 * The length that we want to overlay is following the inner 2351 * IP header. Subtracting the IP header + icmp header + outer 2352 * IP header's length should give us the length that we want to 2353 * overlay. 2354 */ 2355 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2356 hdr_length; 2357 /* 2358 * Overlay whatever follows the inner header over the 2359 * outer header. 2360 */ 2361 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2362 2363 /* Set the wptr to account for the outer header */ 2364 mp->b_wptr -= hdr_length; 2365 return (mp); 2366 } 2367 2368 /* 2369 * Try to pass the ICMP message upstream in case the ULP cares. 2370 * 2371 * If the packet that caused the ICMP error is secure, we send 2372 * it to AH/ESP to make sure that the attached packet has a 2373 * valid association. ipha in the code below points to the 2374 * IP header of the packet that caused the error. 2375 * 2376 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2377 * in the context of IPsec. Normally we tell the upper layer 2378 * whenever we send the ire (including ip_bind), the IPsec header 2379 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2380 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2381 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2382 * same thing. As TCP has the IPsec options size that needs to be 2383 * adjusted, we just pass the MTU unchanged. 2384 * 2385 * IFN could have been generated locally or by some router. 2386 * 2387 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2388 * This happens because IP adjusted its value of MTU on an 2389 * earlier IFN message and could not tell the upper layer, 2390 * the new adjusted value of MTU e.g. Packet was encrypted 2391 * or there was not enough information to fanout to upper 2392 * layers. Thus on the next outbound datagram, ip_wput_ire 2393 * generates the IFN, where IPsec processing has *not* been 2394 * done. 2395 * 2396 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2397 * could have generated this. This happens because ire_max_frag 2398 * value in IP was set to a new value, while the IPsec processing 2399 * was being done and after we made the fragmentation check in 2400 * ip_wput_ire. Thus on return from IPsec processing, 2401 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2402 * and generates the IFN. As IPsec processing is over, we fanout 2403 * to AH/ESP to remove the header. 2404 * 2405 * In both these cases, ipsec_in_loopback will be set indicating 2406 * that IFN was generated locally. 2407 * 2408 * ROUTER : IFN could be secure or non-secure. 2409 * 2410 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2411 * packet in error has AH/ESP headers to validate the AH/ESP 2412 * headers. AH/ESP will verify whether there is a valid SA or 2413 * not and send it back. We will fanout again if we have more 2414 * data in the packet. 2415 * 2416 * If the packet in error does not have AH/ESP, we handle it 2417 * like any other case. 2418 * 2419 * * NON_SECURE : If the packet in error has AH/ESP headers, 2420 * we attach a dummy ipsec_in and send it up to AH/ESP 2421 * for validation. AH/ESP will verify whether there is a 2422 * valid SA or not and send it back. We will fanout again if 2423 * we have more data in the packet. 2424 * 2425 * If the packet in error does not have AH/ESP, we handle it 2426 * like any other case. 2427 */ 2428 static void 2429 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2430 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2431 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2432 zoneid_t zoneid) 2433 { 2434 uint16_t *up; /* Pointer to ports in ULP header */ 2435 uint32_t ports; /* reversed ports for fanout */ 2436 ipha_t ripha; /* With reversed addresses */ 2437 mblk_t *first_mp; 2438 ipsec_in_t *ii; 2439 tcph_t *tcph; 2440 conn_t *connp; 2441 ip_stack_t *ipst; 2442 2443 ASSERT(ill != NULL); 2444 2445 ASSERT(recv_ill != NULL); 2446 ipst = recv_ill->ill_ipst; 2447 2448 first_mp = mp; 2449 if (mctl_present) { 2450 mp = first_mp->b_cont; 2451 ASSERT(mp != NULL); 2452 2453 ii = (ipsec_in_t *)first_mp->b_rptr; 2454 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2455 } else { 2456 ii = NULL; 2457 } 2458 2459 switch (ipha->ipha_protocol) { 2460 case IPPROTO_UDP: 2461 /* 2462 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2463 * transport header. 2464 */ 2465 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2466 mp->b_wptr) { 2467 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2468 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2469 goto discard_pkt; 2470 } 2471 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2472 ipha = (ipha_t *)&icmph[1]; 2473 } 2474 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2475 2476 /* 2477 * Attempt to find a client stream based on port. 2478 * Note that we do a reverse lookup since the header is 2479 * in the form we sent it out. 2480 * The ripha header is only used for the IP_UDP_MATCH and we 2481 * only set the src and dst addresses and protocol. 2482 */ 2483 ripha.ipha_src = ipha->ipha_dst; 2484 ripha.ipha_dst = ipha->ipha_src; 2485 ripha.ipha_protocol = ipha->ipha_protocol; 2486 ((uint16_t *)&ports)[0] = up[1]; 2487 ((uint16_t *)&ports)[1] = up[0]; 2488 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2489 ntohl(ipha->ipha_src), ntohs(up[0]), 2490 ntohl(ipha->ipha_dst), ntohs(up[1]), 2491 icmph->icmph_type, icmph->icmph_code)); 2492 2493 /* Have to change db_type after any pullupmsg */ 2494 DB_TYPE(mp) = M_CTL; 2495 2496 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2497 mctl_present, ip_policy, recv_ill, zoneid); 2498 return; 2499 2500 case IPPROTO_TCP: 2501 /* 2502 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2503 * transport header. 2504 */ 2505 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2506 mp->b_wptr) { 2507 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2508 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2509 goto discard_pkt; 2510 } 2511 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2512 ipha = (ipha_t *)&icmph[1]; 2513 } 2514 /* 2515 * Find a TCP client stream for this packet. 2516 * Note that we do a reverse lookup since the header is 2517 * in the form we sent it out. 2518 */ 2519 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2520 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN, 2521 ipst); 2522 if (connp == NULL) 2523 goto discard_pkt; 2524 2525 /* Have to change db_type after any pullupmsg */ 2526 DB_TYPE(mp) = M_CTL; 2527 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 2528 connp, SQTAG_TCP_INPUT_ICMP_ERR); 2529 return; 2530 2531 case IPPROTO_SCTP: 2532 /* 2533 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2534 * transport header. 2535 */ 2536 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2537 mp->b_wptr) { 2538 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2539 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2540 goto discard_pkt; 2541 } 2542 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2543 ipha = (ipha_t *)&icmph[1]; 2544 } 2545 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2546 /* 2547 * Find a SCTP client stream for this packet. 2548 * Note that we do a reverse lookup since the header is 2549 * in the form we sent it out. 2550 * The ripha header is only used for the matching and we 2551 * only set the src and dst addresses, protocol, and version. 2552 */ 2553 ripha.ipha_src = ipha->ipha_dst; 2554 ripha.ipha_dst = ipha->ipha_src; 2555 ripha.ipha_protocol = ipha->ipha_protocol; 2556 ripha.ipha_version_and_hdr_length = 2557 ipha->ipha_version_and_hdr_length; 2558 ((uint16_t *)&ports)[0] = up[1]; 2559 ((uint16_t *)&ports)[1] = up[0]; 2560 2561 /* Have to change db_type after any pullupmsg */ 2562 DB_TYPE(mp) = M_CTL; 2563 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2564 mctl_present, ip_policy, zoneid); 2565 return; 2566 2567 case IPPROTO_ESP: 2568 case IPPROTO_AH: { 2569 int ipsec_rc; 2570 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 2571 2572 /* 2573 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2574 * We will re-use the IPSEC_IN if it is already present as 2575 * AH/ESP will not affect any fields in the IPSEC_IN for 2576 * ICMP errors. If there is no IPSEC_IN, allocate a new 2577 * one and attach it in the front. 2578 */ 2579 if (ii != NULL) { 2580 /* 2581 * ip_fanout_proto_again converts the ICMP errors 2582 * that come back from AH/ESP to M_DATA so that 2583 * if it is non-AH/ESP and we do a pullupmsg in 2584 * this function, it would work. Convert it back 2585 * to M_CTL before we send up as this is a ICMP 2586 * error. This could have been generated locally or 2587 * by some router. Validate the inner IPsec 2588 * headers. 2589 * 2590 * NOTE : ill_index is used by ip_fanout_proto_again 2591 * to locate the ill. 2592 */ 2593 ASSERT(ill != NULL); 2594 ii->ipsec_in_ill_index = 2595 ill->ill_phyint->phyint_ifindex; 2596 ii->ipsec_in_rill_index = 2597 recv_ill->ill_phyint->phyint_ifindex; 2598 DB_TYPE(first_mp->b_cont) = M_CTL; 2599 } else { 2600 /* 2601 * IPSEC_IN is not present. We attach a ipsec_in 2602 * message and send up to IPsec for validating 2603 * and removing the IPsec headers. Clear 2604 * ipsec_in_secure so that when we return 2605 * from IPsec, we don't mistakenly think that this 2606 * is a secure packet came from the network. 2607 * 2608 * NOTE : ill_index is used by ip_fanout_proto_again 2609 * to locate the ill. 2610 */ 2611 ASSERT(first_mp == mp); 2612 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2613 if (first_mp == NULL) { 2614 freemsg(mp); 2615 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2616 return; 2617 } 2618 ii = (ipsec_in_t *)first_mp->b_rptr; 2619 2620 /* This is not a secure packet */ 2621 ii->ipsec_in_secure = B_FALSE; 2622 first_mp->b_cont = mp; 2623 DB_TYPE(mp) = M_CTL; 2624 ASSERT(ill != NULL); 2625 ii->ipsec_in_ill_index = 2626 ill->ill_phyint->phyint_ifindex; 2627 ii->ipsec_in_rill_index = 2628 recv_ill->ill_phyint->phyint_ifindex; 2629 } 2630 ip2dbg(("icmp_inbound_error: ipsec\n")); 2631 2632 if (!ipsec_loaded(ipss)) { 2633 ip_proto_not_sup(q, first_mp, 0, zoneid, ipst); 2634 return; 2635 } 2636 2637 if (ipha->ipha_protocol == IPPROTO_ESP) 2638 ipsec_rc = ipsecesp_icmp_error(first_mp); 2639 else 2640 ipsec_rc = ipsecah_icmp_error(first_mp); 2641 if (ipsec_rc == IPSEC_STATUS_FAILED) 2642 return; 2643 2644 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2645 return; 2646 } 2647 default: 2648 /* 2649 * The ripha header is only used for the lookup and we 2650 * only set the src and dst addresses and protocol. 2651 */ 2652 ripha.ipha_src = ipha->ipha_dst; 2653 ripha.ipha_dst = ipha->ipha_src; 2654 ripha.ipha_protocol = ipha->ipha_protocol; 2655 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2656 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2657 ntohl(ipha->ipha_dst), 2658 icmph->icmph_type, icmph->icmph_code)); 2659 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2660 ipha_t *in_ipha; 2661 2662 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2663 mp->b_wptr) { 2664 if (!pullupmsg(mp, (uchar_t *)ipha + 2665 hdr_length + sizeof (ipha_t) - 2666 mp->b_rptr)) { 2667 goto discard_pkt; 2668 } 2669 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2670 ipha = (ipha_t *)&icmph[1]; 2671 } 2672 /* 2673 * Caller has verified that length has to be 2674 * at least the size of IP header. 2675 */ 2676 ASSERT(hdr_length >= sizeof (ipha_t)); 2677 /* 2678 * Check the sanity of the inner IP header like 2679 * we did for the outer header. 2680 */ 2681 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2682 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2683 goto discard_pkt; 2684 } 2685 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2686 goto discard_pkt; 2687 } 2688 /* Check for Self-encapsulated tunnels */ 2689 if (in_ipha->ipha_src == ipha->ipha_src && 2690 in_ipha->ipha_dst == ipha->ipha_dst) { 2691 2692 mp = icmp_inbound_self_encap_error(mp, 2693 iph_hdr_length, hdr_length); 2694 if (mp == NULL) 2695 goto discard_pkt; 2696 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2697 ipha = (ipha_t *)&icmph[1]; 2698 hdr_length = IPH_HDR_LENGTH(ipha); 2699 /* 2700 * The packet in error is self-encapsualted. 2701 * And we are finding it further encapsulated 2702 * which we could not have possibly generated. 2703 */ 2704 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2705 goto discard_pkt; 2706 } 2707 icmp_inbound_error_fanout(q, ill, first_mp, 2708 icmph, ipha, iph_hdr_length, hdr_length, 2709 mctl_present, ip_policy, recv_ill, zoneid); 2710 return; 2711 } 2712 } 2713 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2714 ipha->ipha_protocol == IPPROTO_IPV6) && 2715 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2716 ii != NULL && 2717 ii->ipsec_in_loopback && 2718 ii->ipsec_in_secure) { 2719 /* 2720 * For IP tunnels that get a looped-back 2721 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2722 * reported new MTU to take into account the IPsec 2723 * headers protecting this configured tunnel. 2724 * 2725 * This allows the tunnel module (tun.c) to blindly 2726 * accept the MTU reported in an ICMP "too big" 2727 * message. 2728 * 2729 * Non-looped back ICMP messages will just be 2730 * handled by the security protocols (if needed), 2731 * and the first subsequent packet will hit this 2732 * path. 2733 */ 2734 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2735 ipsec_in_extra_length(first_mp)); 2736 } 2737 /* Have to change db_type after any pullupmsg */ 2738 DB_TYPE(mp) = M_CTL; 2739 2740 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2741 ip_policy, recv_ill, zoneid); 2742 return; 2743 } 2744 /* NOTREACHED */ 2745 discard_pkt: 2746 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2747 drop_pkt:; 2748 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2749 freemsg(first_mp); 2750 } 2751 2752 /* 2753 * Common IP options parser. 2754 * 2755 * Setup routine: fill in *optp with options-parsing state, then 2756 * tail-call ipoptp_next to return the first option. 2757 */ 2758 uint8_t 2759 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2760 { 2761 uint32_t totallen; /* total length of all options */ 2762 2763 totallen = ipha->ipha_version_and_hdr_length - 2764 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2765 totallen <<= 2; 2766 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2767 optp->ipoptp_end = optp->ipoptp_next + totallen; 2768 optp->ipoptp_flags = 0; 2769 return (ipoptp_next(optp)); 2770 } 2771 2772 /* 2773 * Common IP options parser: extract next option. 2774 */ 2775 uint8_t 2776 ipoptp_next(ipoptp_t *optp) 2777 { 2778 uint8_t *end = optp->ipoptp_end; 2779 uint8_t *cur = optp->ipoptp_next; 2780 uint8_t opt, len, pointer; 2781 2782 /* 2783 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2784 * has been corrupted. 2785 */ 2786 ASSERT(cur <= end); 2787 2788 if (cur == end) 2789 return (IPOPT_EOL); 2790 2791 opt = cur[IPOPT_OPTVAL]; 2792 2793 /* 2794 * Skip any NOP options. 2795 */ 2796 while (opt == IPOPT_NOP) { 2797 cur++; 2798 if (cur == end) 2799 return (IPOPT_EOL); 2800 opt = cur[IPOPT_OPTVAL]; 2801 } 2802 2803 if (opt == IPOPT_EOL) 2804 return (IPOPT_EOL); 2805 2806 /* 2807 * Option requiring a length. 2808 */ 2809 if ((cur + 1) >= end) { 2810 optp->ipoptp_flags |= IPOPTP_ERROR; 2811 return (IPOPT_EOL); 2812 } 2813 len = cur[IPOPT_OLEN]; 2814 if (len < 2) { 2815 optp->ipoptp_flags |= IPOPTP_ERROR; 2816 return (IPOPT_EOL); 2817 } 2818 optp->ipoptp_cur = cur; 2819 optp->ipoptp_len = len; 2820 optp->ipoptp_next = cur + len; 2821 if (cur + len > end) { 2822 optp->ipoptp_flags |= IPOPTP_ERROR; 2823 return (IPOPT_EOL); 2824 } 2825 2826 /* 2827 * For the options which require a pointer field, make sure 2828 * its there, and make sure it points to either something 2829 * inside this option, or the end of the option. 2830 */ 2831 switch (opt) { 2832 case IPOPT_RR: 2833 case IPOPT_TS: 2834 case IPOPT_LSRR: 2835 case IPOPT_SSRR: 2836 if (len <= IPOPT_OFFSET) { 2837 optp->ipoptp_flags |= IPOPTP_ERROR; 2838 return (opt); 2839 } 2840 pointer = cur[IPOPT_OFFSET]; 2841 if (pointer - 1 > len) { 2842 optp->ipoptp_flags |= IPOPTP_ERROR; 2843 return (opt); 2844 } 2845 break; 2846 } 2847 2848 /* 2849 * Sanity check the pointer field based on the type of the 2850 * option. 2851 */ 2852 switch (opt) { 2853 case IPOPT_RR: 2854 case IPOPT_SSRR: 2855 case IPOPT_LSRR: 2856 if (pointer < IPOPT_MINOFF_SR) 2857 optp->ipoptp_flags |= IPOPTP_ERROR; 2858 break; 2859 case IPOPT_TS: 2860 if (pointer < IPOPT_MINOFF_IT) 2861 optp->ipoptp_flags |= IPOPTP_ERROR; 2862 /* 2863 * Note that the Internet Timestamp option also 2864 * contains two four bit fields (the Overflow field, 2865 * and the Flag field), which follow the pointer 2866 * field. We don't need to check that these fields 2867 * fall within the length of the option because this 2868 * was implicitely done above. We've checked that the 2869 * pointer value is at least IPOPT_MINOFF_IT, and that 2870 * it falls within the option. Since IPOPT_MINOFF_IT > 2871 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2872 */ 2873 ASSERT(len > IPOPT_POS_OV_FLG); 2874 break; 2875 } 2876 2877 return (opt); 2878 } 2879 2880 /* 2881 * Use the outgoing IP header to create an IP_OPTIONS option the way 2882 * it was passed down from the application. 2883 */ 2884 int 2885 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) 2886 { 2887 ipoptp_t opts; 2888 const uchar_t *opt; 2889 uint8_t optval; 2890 uint8_t optlen; 2891 uint32_t len = 0; 2892 uchar_t *buf1 = buf; 2893 2894 buf += IP_ADDR_LEN; /* Leave room for final destination */ 2895 len += IP_ADDR_LEN; 2896 bzero(buf1, IP_ADDR_LEN); 2897 2898 /* 2899 * OK to cast away const here, as we don't store through the returned 2900 * opts.ipoptp_cur pointer. 2901 */ 2902 for (optval = ipoptp_first(&opts, (ipha_t *)ipha); 2903 optval != IPOPT_EOL; 2904 optval = ipoptp_next(&opts)) { 2905 int off; 2906 2907 opt = opts.ipoptp_cur; 2908 optlen = opts.ipoptp_len; 2909 switch (optval) { 2910 case IPOPT_SSRR: 2911 case IPOPT_LSRR: 2912 2913 /* 2914 * Insert ipha_dst as the first entry in the source 2915 * route and move down the entries on step. 2916 * The last entry gets placed at buf1. 2917 */ 2918 buf[IPOPT_OPTVAL] = optval; 2919 buf[IPOPT_OLEN] = optlen; 2920 buf[IPOPT_OFFSET] = optlen; 2921 2922 off = optlen - IP_ADDR_LEN; 2923 if (off < 0) { 2924 /* No entries in source route */ 2925 break; 2926 } 2927 /* Last entry in source route */ 2928 bcopy(opt + off, buf1, IP_ADDR_LEN); 2929 off -= IP_ADDR_LEN; 2930 2931 while (off > 0) { 2932 bcopy(opt + off, 2933 buf + off + IP_ADDR_LEN, 2934 IP_ADDR_LEN); 2935 off -= IP_ADDR_LEN; 2936 } 2937 /* ipha_dst into first slot */ 2938 bcopy(&ipha->ipha_dst, 2939 buf + off + IP_ADDR_LEN, 2940 IP_ADDR_LEN); 2941 buf += optlen; 2942 len += optlen; 2943 break; 2944 2945 case IPOPT_COMSEC: 2946 case IPOPT_SECURITY: 2947 /* if passing up a label is not ok, then remove */ 2948 if (is_system_labeled()) 2949 break; 2950 /* FALLTHROUGH */ 2951 default: 2952 bcopy(opt, buf, optlen); 2953 buf += optlen; 2954 len += optlen; 2955 break; 2956 } 2957 } 2958 done: 2959 /* Pad the resulting options */ 2960 while (len & 0x3) { 2961 *buf++ = IPOPT_EOL; 2962 len++; 2963 } 2964 return (len); 2965 } 2966 2967 /* 2968 * Update any record route or timestamp options to include this host. 2969 * Reverse any source route option. 2970 * This routine assumes that the options are well formed i.e. that they 2971 * have already been checked. 2972 */ 2973 static void 2974 icmp_options_update(ipha_t *ipha) 2975 { 2976 ipoptp_t opts; 2977 uchar_t *opt; 2978 uint8_t optval; 2979 ipaddr_t src; /* Our local address */ 2980 ipaddr_t dst; 2981 2982 ip2dbg(("icmp_options_update\n")); 2983 src = ipha->ipha_src; 2984 dst = ipha->ipha_dst; 2985 2986 for (optval = ipoptp_first(&opts, ipha); 2987 optval != IPOPT_EOL; 2988 optval = ipoptp_next(&opts)) { 2989 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 2990 opt = opts.ipoptp_cur; 2991 ip2dbg(("icmp_options_update: opt %d, len %d\n", 2992 optval, opts.ipoptp_len)); 2993 switch (optval) { 2994 int off1, off2; 2995 case IPOPT_SSRR: 2996 case IPOPT_LSRR: 2997 /* 2998 * Reverse the source route. The first entry 2999 * should be the next to last one in the current 3000 * source route (the last entry is our address). 3001 * The last entry should be the final destination. 3002 */ 3003 off1 = IPOPT_MINOFF_SR - 1; 3004 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 3005 if (off2 < 0) { 3006 /* No entries in source route */ 3007 ip1dbg(( 3008 "icmp_options_update: bad src route\n")); 3009 break; 3010 } 3011 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 3012 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 3013 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 3014 off2 -= IP_ADDR_LEN; 3015 3016 while (off1 < off2) { 3017 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 3018 bcopy((char *)opt + off2, (char *)opt + off1, 3019 IP_ADDR_LEN); 3020 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 3021 off1 += IP_ADDR_LEN; 3022 off2 -= IP_ADDR_LEN; 3023 } 3024 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 3025 break; 3026 } 3027 } 3028 } 3029 3030 /* 3031 * Process received ICMP Redirect messages. 3032 */ 3033 static void 3034 icmp_redirect(ill_t *ill, mblk_t *mp) 3035 { 3036 ipha_t *ipha; 3037 int iph_hdr_length; 3038 icmph_t *icmph; 3039 ipha_t *ipha_err; 3040 ire_t *ire; 3041 ire_t *prev_ire; 3042 ire_t *save_ire; 3043 ipaddr_t src, dst, gateway; 3044 iulp_t ulp_info = { 0 }; 3045 int error; 3046 ip_stack_t *ipst; 3047 3048 ASSERT(ill != NULL); 3049 ipst = ill->ill_ipst; 3050 3051 ipha = (ipha_t *)mp->b_rptr; 3052 iph_hdr_length = IPH_HDR_LENGTH(ipha); 3053 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 3054 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 3055 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3056 freemsg(mp); 3057 return; 3058 } 3059 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 3060 ipha_err = (ipha_t *)&icmph[1]; 3061 src = ipha->ipha_src; 3062 dst = ipha_err->ipha_dst; 3063 gateway = icmph->icmph_rd_gateway; 3064 /* Make sure the new gateway is reachable somehow. */ 3065 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 3066 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3067 /* 3068 * Make sure we had a route for the dest in question and that 3069 * that route was pointing to the old gateway (the source of the 3070 * redirect packet.) 3071 */ 3072 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 3073 NULL, MATCH_IRE_GW, ipst); 3074 /* 3075 * Check that 3076 * the redirect was not from ourselves 3077 * the new gateway and the old gateway are directly reachable 3078 */ 3079 if (!prev_ire || 3080 !ire || 3081 ire->ire_type == IRE_LOCAL) { 3082 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3083 freemsg(mp); 3084 if (ire != NULL) 3085 ire_refrele(ire); 3086 if (prev_ire != NULL) 3087 ire_refrele(prev_ire); 3088 return; 3089 } 3090 3091 /* 3092 * Should we use the old ULP info to create the new gateway? From 3093 * a user's perspective, we should inherit the info so that it 3094 * is a "smooth" transition. If we do not do that, then new 3095 * connections going thru the new gateway will have no route metrics, 3096 * which is counter-intuitive to user. From a network point of 3097 * view, this may or may not make sense even though the new gateway 3098 * is still directly connected to us so the route metrics should not 3099 * change much. 3100 * 3101 * But if the old ire_uinfo is not initialized, we do another 3102 * recursive lookup on the dest using the new gateway. There may 3103 * be a route to that. If so, use it to initialize the redirect 3104 * route. 3105 */ 3106 if (prev_ire->ire_uinfo.iulp_set) { 3107 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3108 } else { 3109 ire_t *tmp_ire; 3110 ire_t *sire; 3111 3112 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 3113 ALL_ZONES, 0, NULL, 3114 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT), 3115 ipst); 3116 if (sire != NULL) { 3117 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3118 /* 3119 * If sire != NULL, ire_ftable_lookup() should not 3120 * return a NULL value. 3121 */ 3122 ASSERT(tmp_ire != NULL); 3123 ire_refrele(tmp_ire); 3124 ire_refrele(sire); 3125 } else if (tmp_ire != NULL) { 3126 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 3127 sizeof (iulp_t)); 3128 ire_refrele(tmp_ire); 3129 } 3130 } 3131 if (prev_ire->ire_type == IRE_CACHE) 3132 ire_delete(prev_ire); 3133 ire_refrele(prev_ire); 3134 /* 3135 * TODO: more precise handling for cases 0, 2, 3, the latter two 3136 * require TOS routing 3137 */ 3138 switch (icmph->icmph_code) { 3139 case 0: 3140 case 1: 3141 /* TODO: TOS specificity for cases 2 and 3 */ 3142 case 2: 3143 case 3: 3144 break; 3145 default: 3146 freemsg(mp); 3147 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3148 ire_refrele(ire); 3149 return; 3150 } 3151 /* 3152 * Create a Route Association. This will allow us to remember that 3153 * someone we believe told us to use the particular gateway. 3154 */ 3155 save_ire = ire; 3156 ire = ire_create( 3157 (uchar_t *)&dst, /* dest addr */ 3158 (uchar_t *)&ip_g_all_ones, /* mask */ 3159 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 3160 (uchar_t *)&gateway, /* gateway addr */ 3161 &save_ire->ire_max_frag, /* max frag */ 3162 NULL, /* no src nce */ 3163 NULL, /* no rfq */ 3164 NULL, /* no stq */ 3165 IRE_HOST, 3166 NULL, /* ipif */ 3167 0, /* cmask */ 3168 0, /* phandle */ 3169 0, /* ihandle */ 3170 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 3171 &ulp_info, 3172 NULL, /* tsol_gc_t */ 3173 NULL, /* gcgrp */ 3174 ipst); 3175 3176 if (ire == NULL) { 3177 freemsg(mp); 3178 ire_refrele(save_ire); 3179 return; 3180 } 3181 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); 3182 ire_refrele(save_ire); 3183 atomic_inc_32(&ipst->ips_ip_redirect_cnt); 3184 3185 if (error == 0) { 3186 ire_refrele(ire); /* Held in ire_add_v4 */ 3187 /* tell routing sockets that we received a redirect */ 3188 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 3189 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 3190 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst); 3191 } 3192 3193 /* 3194 * Delete any existing IRE_HOST type redirect ires for this destination. 3195 * This together with the added IRE has the effect of 3196 * modifying an existing redirect. 3197 */ 3198 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL, 3199 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst); 3200 if (prev_ire != NULL) { 3201 if (prev_ire ->ire_flags & RTF_DYNAMIC) 3202 ire_delete(prev_ire); 3203 ire_refrele(prev_ire); 3204 } 3205 3206 freemsg(mp); 3207 } 3208 3209 /* 3210 * Generate an ICMP parameter problem message. 3211 */ 3212 static void 3213 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid, 3214 ip_stack_t *ipst) 3215 { 3216 icmph_t icmph; 3217 boolean_t mctl_present; 3218 mblk_t *first_mp; 3219 3220 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3221 3222 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3223 if (mctl_present) 3224 freeb(first_mp); 3225 return; 3226 } 3227 3228 bzero(&icmph, sizeof (icmph_t)); 3229 icmph.icmph_type = ICMP_PARAM_PROBLEM; 3230 icmph.icmph_pp_ptr = ptr; 3231 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs); 3232 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3233 ipst); 3234 } 3235 3236 /* 3237 * Build and ship an IPv4 ICMP message using the packet data in mp, and 3238 * the ICMP header pointed to by "stuff". (May be called as writer.) 3239 * Note: assumes that icmp_pkt_err_ok has been called to verify that 3240 * an icmp error packet can be sent. 3241 * Assigns an appropriate source address to the packet. If ipha_dst is 3242 * one of our addresses use it for source. Otherwise pick a source based 3243 * on a route lookup back to ipha_src. 3244 * Note that ipha_src must be set here since the 3245 * packet is likely to arrive on an ill queue in ip_wput() which will 3246 * not set a source address. 3247 */ 3248 static void 3249 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, 3250 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 3251 { 3252 ipaddr_t dst; 3253 icmph_t *icmph; 3254 ipha_t *ipha; 3255 uint_t len_needed; 3256 size_t msg_len; 3257 mblk_t *mp1; 3258 ipaddr_t src; 3259 ire_t *ire; 3260 mblk_t *ipsec_mp; 3261 ipsec_out_t *io = NULL; 3262 3263 if (mctl_present) { 3264 /* 3265 * If it is : 3266 * 3267 * 1) a IPSEC_OUT, then this is caused by outbound 3268 * datagram originating on this host. IPsec processing 3269 * may or may not have been done. Refer to comments above 3270 * icmp_inbound_error_fanout for details. 3271 * 3272 * 2) a IPSEC_IN if we are generating a icmp_message 3273 * for an incoming datagram destined for us i.e called 3274 * from ip_fanout_send_icmp. 3275 */ 3276 ipsec_info_t *in; 3277 ipsec_mp = mp; 3278 mp = ipsec_mp->b_cont; 3279 3280 in = (ipsec_info_t *)ipsec_mp->b_rptr; 3281 ipha = (ipha_t *)mp->b_rptr; 3282 3283 ASSERT(in->ipsec_info_type == IPSEC_OUT || 3284 in->ipsec_info_type == IPSEC_IN); 3285 3286 if (in->ipsec_info_type == IPSEC_IN) { 3287 /* 3288 * Convert the IPSEC_IN to IPSEC_OUT. 3289 */ 3290 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3291 BUMP_MIB(&ipst->ips_ip_mib, 3292 ipIfStatsOutDiscards); 3293 return; 3294 } 3295 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3296 } else { 3297 ASSERT(in->ipsec_info_type == IPSEC_OUT); 3298 io = (ipsec_out_t *)in; 3299 /* 3300 * Clear out ipsec_out_proc_begin, so we do a fresh 3301 * ire lookup. 3302 */ 3303 io->ipsec_out_proc_begin = B_FALSE; 3304 } 3305 ASSERT(zoneid == io->ipsec_out_zoneid); 3306 ASSERT(zoneid != ALL_ZONES); 3307 } else { 3308 /* 3309 * This is in clear. The icmp message we are building 3310 * here should go out in clear. 3311 * 3312 * Pardon the convolution of it all, but it's easier to 3313 * allocate a "use cleartext" IPSEC_IN message and convert 3314 * it than it is to allocate a new one. 3315 */ 3316 ipsec_in_t *ii; 3317 ASSERT(DB_TYPE(mp) == M_DATA); 3318 ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 3319 if (ipsec_mp == NULL) { 3320 freemsg(mp); 3321 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3322 return; 3323 } 3324 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3325 3326 /* This is not a secure packet */ 3327 ii->ipsec_in_secure = B_FALSE; 3328 /* 3329 * For trusted extensions using a shared IP address we can 3330 * send using any zoneid. 3331 */ 3332 if (zoneid == ALL_ZONES) 3333 ii->ipsec_in_zoneid = GLOBAL_ZONEID; 3334 else 3335 ii->ipsec_in_zoneid = zoneid; 3336 ipsec_mp->b_cont = mp; 3337 ipha = (ipha_t *)mp->b_rptr; 3338 /* 3339 * Convert the IPSEC_IN to IPSEC_OUT. 3340 */ 3341 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3342 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3343 return; 3344 } 3345 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3346 } 3347 3348 /* Remember our eventual destination */ 3349 dst = ipha->ipha_src; 3350 3351 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 3352 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst); 3353 if (ire != NULL && 3354 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) { 3355 src = ipha->ipha_dst; 3356 } else { 3357 if (ire != NULL) 3358 ire_refrele(ire); 3359 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL, 3360 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY), 3361 ipst); 3362 if (ire == NULL) { 3363 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3364 freemsg(ipsec_mp); 3365 return; 3366 } 3367 src = ire->ire_src_addr; 3368 } 3369 3370 if (ire != NULL) 3371 ire_refrele(ire); 3372 3373 /* 3374 * Check if we can send back more then 8 bytes in addition to 3375 * the IP header. We try to send 64 bytes of data and the internal 3376 * header in the special cases of ipv4 encapsulated ipv4 or ipv6. 3377 */ 3378 len_needed = IPH_HDR_LENGTH(ipha); 3379 if (ipha->ipha_protocol == IPPROTO_ENCAP || 3380 ipha->ipha_protocol == IPPROTO_IPV6) { 3381 3382 if (!pullupmsg(mp, -1)) { 3383 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3384 freemsg(ipsec_mp); 3385 return; 3386 } 3387 ipha = (ipha_t *)mp->b_rptr; 3388 3389 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 3390 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + 3391 len_needed)); 3392 } else { 3393 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed); 3394 3395 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6); 3396 len_needed += ip_hdr_length_v6(mp, ip6h); 3397 } 3398 } 3399 len_needed += ipst->ips_ip_icmp_return; 3400 msg_len = msgdsize(mp); 3401 if (msg_len > len_needed) { 3402 (void) adjmsg(mp, len_needed - msg_len); 3403 msg_len = len_needed; 3404 } 3405 mp1 = allocb_tmpl(sizeof (icmp_ipha) + len, mp); 3406 if (mp1 == NULL) { 3407 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors); 3408 freemsg(ipsec_mp); 3409 return; 3410 } 3411 mp1->b_cont = mp; 3412 mp = mp1; 3413 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 3414 ipsec_mp->b_rptr == (uint8_t *)io && 3415 io->ipsec_out_type == IPSEC_OUT); 3416 ipsec_mp->b_cont = mp; 3417 3418 /* 3419 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 3420 * node generates be accepted in peace by all on-host destinations. 3421 * If we do NOT assume that all on-host destinations trust 3422 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 3423 * (Look for ipsec_out_icmp_loopback). 3424 */ 3425 io->ipsec_out_icmp_loopback = B_TRUE; 3426 3427 ipha = (ipha_t *)mp->b_rptr; 3428 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 3429 *ipha = icmp_ipha; 3430 ipha->ipha_src = src; 3431 ipha->ipha_dst = dst; 3432 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 3433 msg_len += sizeof (icmp_ipha) + len; 3434 if (msg_len > IP_MAXPACKET) { 3435 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 3436 msg_len = IP_MAXPACKET; 3437 } 3438 ipha->ipha_length = htons((uint16_t)msg_len); 3439 icmph = (icmph_t *)&ipha[1]; 3440 bcopy(stuff, icmph, len); 3441 icmph->icmph_checksum = 0; 3442 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 3443 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 3444 put(q, ipsec_mp); 3445 } 3446 3447 /* 3448 * Determine if an ICMP error packet can be sent given the rate limit. 3449 * The limit consists of an average frequency (icmp_pkt_err_interval measured 3450 * in milliseconds) and a burst size. Burst size number of packets can 3451 * be sent arbitrarely closely spaced. 3452 * The state is tracked using two variables to implement an approximate 3453 * token bucket filter: 3454 * icmp_pkt_err_last - lbolt value when the last burst started 3455 * icmp_pkt_err_sent - number of packets sent in current burst 3456 */ 3457 boolean_t 3458 icmp_err_rate_limit(ip_stack_t *ipst) 3459 { 3460 clock_t now = TICK_TO_MSEC(lbolt); 3461 uint_t refilled; /* Number of packets refilled in tbf since last */ 3462 /* Guard against changes by loading into local variable */ 3463 uint_t err_interval = ipst->ips_ip_icmp_err_interval; 3464 3465 if (err_interval == 0) 3466 return (B_FALSE); 3467 3468 if (ipst->ips_icmp_pkt_err_last > now) { 3469 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 3470 ipst->ips_icmp_pkt_err_last = 0; 3471 ipst->ips_icmp_pkt_err_sent = 0; 3472 } 3473 /* 3474 * If we are in a burst update the token bucket filter. 3475 * Update the "last" time to be close to "now" but make sure 3476 * we don't loose precision. 3477 */ 3478 if (ipst->ips_icmp_pkt_err_sent != 0) { 3479 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval; 3480 if (refilled > ipst->ips_icmp_pkt_err_sent) { 3481 ipst->ips_icmp_pkt_err_sent = 0; 3482 } else { 3483 ipst->ips_icmp_pkt_err_sent -= refilled; 3484 ipst->ips_icmp_pkt_err_last += refilled * err_interval; 3485 } 3486 } 3487 if (ipst->ips_icmp_pkt_err_sent == 0) { 3488 /* Start of new burst */ 3489 ipst->ips_icmp_pkt_err_last = now; 3490 } 3491 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) { 3492 ipst->ips_icmp_pkt_err_sent++; 3493 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 3494 ipst->ips_icmp_pkt_err_sent)); 3495 return (B_FALSE); 3496 } 3497 ip1dbg(("icmp_err_rate_limit: dropped\n")); 3498 return (B_TRUE); 3499 } 3500 3501 /* 3502 * Check if it is ok to send an IPv4 ICMP error packet in 3503 * response to the IPv4 packet in mp. 3504 * Free the message and return null if no 3505 * ICMP error packet should be sent. 3506 */ 3507 static mblk_t * 3508 icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst) 3509 { 3510 icmph_t *icmph; 3511 ipha_t *ipha; 3512 uint_t len_needed; 3513 ire_t *src_ire; 3514 ire_t *dst_ire; 3515 3516 if (!mp) 3517 return (NULL); 3518 ipha = (ipha_t *)mp->b_rptr; 3519 if (ip_csum_hdr(ipha)) { 3520 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); 3521 freemsg(mp); 3522 return (NULL); 3523 } 3524 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, 3525 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3526 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, 3527 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3528 if (src_ire != NULL || dst_ire != NULL || 3529 CLASSD(ipha->ipha_dst) || 3530 CLASSD(ipha->ipha_src) || 3531 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3532 /* Note: only errors to the fragment with offset 0 */ 3533 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3534 freemsg(mp); 3535 if (src_ire != NULL) 3536 ire_refrele(src_ire); 3537 if (dst_ire != NULL) 3538 ire_refrele(dst_ire); 3539 return (NULL); 3540 } 3541 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3542 /* 3543 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3544 * errors in response to any ICMP errors. 3545 */ 3546 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3547 if (mp->b_wptr - mp->b_rptr < len_needed) { 3548 if (!pullupmsg(mp, len_needed)) { 3549 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3550 freemsg(mp); 3551 return (NULL); 3552 } 3553 ipha = (ipha_t *)mp->b_rptr; 3554 } 3555 icmph = (icmph_t *) 3556 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3557 switch (icmph->icmph_type) { 3558 case ICMP_DEST_UNREACHABLE: 3559 case ICMP_SOURCE_QUENCH: 3560 case ICMP_TIME_EXCEEDED: 3561 case ICMP_PARAM_PROBLEM: 3562 case ICMP_REDIRECT: 3563 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3564 freemsg(mp); 3565 return (NULL); 3566 default: 3567 break; 3568 } 3569 } 3570 /* 3571 * If this is a labeled system, then check to see if we're allowed to 3572 * send a response to this particular sender. If not, then just drop. 3573 */ 3574 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 3575 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); 3576 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3577 freemsg(mp); 3578 return (NULL); 3579 } 3580 if (icmp_err_rate_limit(ipst)) { 3581 /* 3582 * Only send ICMP error packets every so often. 3583 * This should be done on a per port/source basis, 3584 * but for now this will suffice. 3585 */ 3586 freemsg(mp); 3587 return (NULL); 3588 } 3589 return (mp); 3590 } 3591 3592 /* 3593 * Generate an ICMP redirect message. 3594 */ 3595 static void 3596 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst) 3597 { 3598 icmph_t icmph; 3599 3600 /* 3601 * We are called from ip_rput where we could 3602 * not have attached an IPSEC_IN. 3603 */ 3604 ASSERT(mp->b_datap->db_type == M_DATA); 3605 3606 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3607 return; 3608 } 3609 3610 bzero(&icmph, sizeof (icmph_t)); 3611 icmph.icmph_type = ICMP_REDIRECT; 3612 icmph.icmph_code = 1; 3613 icmph.icmph_rd_gateway = gateway; 3614 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects); 3615 /* Redirects sent by router, and router is global zone */ 3616 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst); 3617 } 3618 3619 /* 3620 * Generate an ICMP time exceeded message. 3621 */ 3622 void 3623 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3624 ip_stack_t *ipst) 3625 { 3626 icmph_t icmph; 3627 boolean_t mctl_present; 3628 mblk_t *first_mp; 3629 3630 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3631 3632 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3633 if (mctl_present) 3634 freeb(first_mp); 3635 return; 3636 } 3637 3638 bzero(&icmph, sizeof (icmph_t)); 3639 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3640 icmph.icmph_code = code; 3641 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds); 3642 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3643 ipst); 3644 } 3645 3646 /* 3647 * Generate an ICMP unreachable message. 3648 */ 3649 void 3650 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3651 ip_stack_t *ipst) 3652 { 3653 icmph_t icmph; 3654 mblk_t *first_mp; 3655 boolean_t mctl_present; 3656 3657 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3658 3659 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3660 if (mctl_present) 3661 freeb(first_mp); 3662 return; 3663 } 3664 3665 bzero(&icmph, sizeof (icmph_t)); 3666 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3667 icmph.icmph_code = code; 3668 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 3669 ip2dbg(("send icmp destination unreachable code %d\n", code)); 3670 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present, 3671 zoneid, ipst); 3672 } 3673 3674 /* 3675 * Attempt to start recovery of an IPv4 interface that's been shut down as a 3676 * duplicate. As long as someone else holds the address, the interface will 3677 * stay down. When that conflict goes away, the interface is brought back up. 3678 * This is done so that accidental shutdowns of addresses aren't made 3679 * permanent. Your server will recover from a failure. 3680 * 3681 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a 3682 * user space process (dhcpagent). 3683 * 3684 * Recovery completes if ARP reports that the address is now ours (via 3685 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation. 3686 * 3687 * This function is entered on a timer expiry; the ID is in ipif_recovery_id. 3688 */ 3689 static void 3690 ipif_dup_recovery(void *arg) 3691 { 3692 ipif_t *ipif = arg; 3693 ill_t *ill = ipif->ipif_ill; 3694 mblk_t *arp_add_mp; 3695 mblk_t *arp_del_mp; 3696 area_t *area; 3697 ip_stack_t *ipst = ill->ill_ipst; 3698 3699 ipif->ipif_recovery_id = 0; 3700 3701 /* 3702 * No lock needed for moving or condemned check, as this is just an 3703 * optimization. 3704 */ 3705 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || 3706 (ipif->ipif_flags & IPIF_POINTOPOINT) || 3707 (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) { 3708 /* No reason to try to bring this address back. */ 3709 return; 3710 } 3711 3712 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL) 3713 goto alloc_fail; 3714 3715 if (ipif->ipif_arp_del_mp == NULL) { 3716 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) 3717 goto alloc_fail; 3718 ipif->ipif_arp_del_mp = arp_del_mp; 3719 } 3720 3721 /* Setting the 'unverified' flag restarts DAD */ 3722 area = (area_t *)arp_add_mp->b_rptr; 3723 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | 3724 ACE_F_UNVERIFIED; 3725 putnext(ill->ill_rq, arp_add_mp); 3726 return; 3727 3728 alloc_fail: 3729 /* 3730 * On allocation failure, just restart the timer. Note that the ipif 3731 * is down here, so no other thread could be trying to start a recovery 3732 * timer. The ill_lock protects the condemned flag and the recovery 3733 * timer ID. 3734 */ 3735 freemsg(arp_add_mp); 3736 mutex_enter(&ill->ill_lock); 3737 if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 && 3738 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 3739 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, 3740 MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3741 } 3742 mutex_exit(&ill->ill_lock); 3743 } 3744 3745 /* 3746 * This is for exclusive changes due to ARP. Either tear down an interface due 3747 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. 3748 */ 3749 /* ARGSUSED */ 3750 static void 3751 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3752 { 3753 ill_t *ill = rq->q_ptr; 3754 arh_t *arh; 3755 ipaddr_t src; 3756 ipif_t *ipif; 3757 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ 3758 char hbuf[MAC_STR_LEN]; 3759 char sbuf[INET_ADDRSTRLEN]; 3760 const char *failtype; 3761 boolean_t bring_up; 3762 ip_stack_t *ipst = ill->ill_ipst; 3763 3764 switch (((arcn_t *)mp->b_rptr)->arcn_code) { 3765 case AR_CN_READY: 3766 failtype = NULL; 3767 bring_up = B_TRUE; 3768 break; 3769 case AR_CN_FAILED: 3770 failtype = "in use"; 3771 bring_up = B_FALSE; 3772 break; 3773 default: 3774 failtype = "claimed"; 3775 bring_up = B_FALSE; 3776 break; 3777 } 3778 3779 arh = (arh_t *)mp->b_cont->b_rptr; 3780 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3781 3782 /* Handle failures due to probes */ 3783 if (src == 0) { 3784 bcopy((char *)&arh[1] + 2 * arh->arh_hlen + IP_ADDR_LEN, &src, 3785 IP_ADDR_LEN); 3786 } 3787 3788 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, 3789 sizeof (hbuf)); 3790 (void) ip_dot_addr(src, sbuf); 3791 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3792 3793 if ((ipif->ipif_flags & IPIF_POINTOPOINT) || 3794 ipif->ipif_lcl_addr != src) { 3795 continue; 3796 } 3797 3798 /* 3799 * If we failed on a recovery probe, then restart the timer to 3800 * try again later. 3801 */ 3802 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) && 3803 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3804 ill->ill_net_type == IRE_IF_RESOLVER && 3805 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3806 ipst->ips_ip_dup_recovery > 0 && 3807 ipif->ipif_recovery_id == 0) { 3808 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3809 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3810 continue; 3811 } 3812 3813 /* 3814 * If what we're trying to do has already been done, then do 3815 * nothing. 3816 */ 3817 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0)) 3818 continue; 3819 3820 ipif_get_name(ipif, ibuf, sizeof (ibuf)); 3821 3822 if (failtype == NULL) { 3823 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf, 3824 ibuf); 3825 } else { 3826 cmn_err(CE_WARN, "%s has duplicate address %s (%s " 3827 "by %s); disabled", ibuf, sbuf, failtype, hbuf); 3828 } 3829 3830 if (bring_up) { 3831 ASSERT(ill->ill_dl_up); 3832 /* 3833 * Free up the ARP delete message so we can allocate 3834 * a fresh one through the normal path. 3835 */ 3836 freemsg(ipif->ipif_arp_del_mp); 3837 ipif->ipif_arp_del_mp = NULL; 3838 if (ipif_resolver_up(ipif, Res_act_initial) != 3839 EINPROGRESS) { 3840 ipif->ipif_addr_ready = 1; 3841 (void) ipif_up_done(ipif); 3842 } 3843 continue; 3844 } 3845 3846 mutex_enter(&ill->ill_lock); 3847 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 3848 ipif->ipif_flags |= IPIF_DUPLICATE; 3849 ill->ill_ipif_dup_count++; 3850 mutex_exit(&ill->ill_lock); 3851 /* 3852 * Already exclusive on the ill; no need to handle deferred 3853 * processing here. 3854 */ 3855 (void) ipif_down(ipif, NULL, NULL); 3856 ipif_down_tail(ipif); 3857 mutex_enter(&ill->ill_lock); 3858 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3859 ill->ill_net_type == IRE_IF_RESOLVER && 3860 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3861 ipst->ips_ip_dup_recovery > 0) { 3862 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3863 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3864 } 3865 mutex_exit(&ill->ill_lock); 3866 } 3867 freemsg(mp); 3868 } 3869 3870 /* ARGSUSED */ 3871 static void 3872 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3873 { 3874 ill_t *ill = rq->q_ptr; 3875 arh_t *arh; 3876 ipaddr_t src; 3877 ipif_t *ipif; 3878 3879 arh = (arh_t *)mp->b_cont->b_rptr; 3880 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3881 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3882 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) 3883 (void) ipif_resolver_up(ipif, Res_act_defend); 3884 } 3885 freemsg(mp); 3886 } 3887 3888 /* 3889 * News from ARP. ARP sends notification of interesting events down 3890 * to its clients using M_CTL messages with the interesting ARP packet 3891 * attached via b_cont. 3892 * The interesting event from a device comes up the corresponding ARP-IP-DEV 3893 * queue as opposed to ARP sending the message to all the clients, i.e. all 3894 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 3895 * table if a cache IRE is found to delete all the entries for the address in 3896 * the packet. 3897 */ 3898 static void 3899 ip_arp_news(queue_t *q, mblk_t *mp) 3900 { 3901 arcn_t *arcn; 3902 arh_t *arh; 3903 ire_t *ire = NULL; 3904 char hbuf[MAC_STR_LEN]; 3905 char sbuf[INET_ADDRSTRLEN]; 3906 ipaddr_t src; 3907 in6_addr_t v6src; 3908 boolean_t isv6 = B_FALSE; 3909 ipif_t *ipif; 3910 ill_t *ill; 3911 ip_stack_t *ipst; 3912 3913 if (CONN_Q(q)) { 3914 conn_t *connp = Q_TO_CONN(q); 3915 3916 ipst = connp->conn_netstack->netstack_ip; 3917 } else { 3918 ill_t *ill = (ill_t *)q->q_ptr; 3919 3920 ipst = ill->ill_ipst; 3921 } 3922 3923 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 3924 if (q->q_next) { 3925 putnext(q, mp); 3926 } else 3927 freemsg(mp); 3928 return; 3929 } 3930 arh = (arh_t *)mp->b_cont->b_rptr; 3931 /* Is it one we are interested in? */ 3932 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 3933 isv6 = B_TRUE; 3934 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 3935 IPV6_ADDR_LEN); 3936 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 3937 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 3938 IP_ADDR_LEN); 3939 } else { 3940 freemsg(mp); 3941 return; 3942 } 3943 3944 ill = q->q_ptr; 3945 3946 arcn = (arcn_t *)mp->b_rptr; 3947 switch (arcn->arcn_code) { 3948 case AR_CN_BOGON: 3949 /* 3950 * Someone is sending ARP packets with a source protocol 3951 * address that we have published and for which we believe our 3952 * entry is authoritative and (when ill_arp_extend is set) 3953 * verified to be unique on the network. 3954 * 3955 * The ARP module internally handles the cases where the sender 3956 * is just probing (for DAD) and where the hardware address of 3957 * a non-authoritative entry has changed. Thus, these are the 3958 * real conflicts, and we have to do resolution. 3959 * 3960 * We back away quickly from the address if it's from DHCP or 3961 * otherwise temporary and hasn't been used recently (or at 3962 * all). We'd like to include "deprecated" addresses here as 3963 * well (as there's no real reason to defend something we're 3964 * discarding), but IPMP "reuses" this flag to mean something 3965 * other than the standard meaning. 3966 * 3967 * If the ARP module above is not extended (meaning that it 3968 * doesn't know how to defend the address), then we just log 3969 * the problem as we always did and continue on. It's not 3970 * right, but there's little else we can do, and those old ATM 3971 * users are going away anyway. 3972 */ 3973 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, 3974 hbuf, sizeof (hbuf)); 3975 (void) ip_dot_addr(src, sbuf); 3976 if (isv6) { 3977 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL, 3978 ipst); 3979 } else { 3980 ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst); 3981 } 3982 if (ire != NULL && IRE_IS_LOCAL(ire)) { 3983 uint32_t now; 3984 uint32_t maxage; 3985 clock_t lused; 3986 uint_t maxdefense; 3987 uint_t defs; 3988 3989 /* 3990 * First, figure out if this address hasn't been used 3991 * in a while. If it hasn't, then it's a better 3992 * candidate for abandoning. 3993 */ 3994 ipif = ire->ire_ipif; 3995 ASSERT(ipif != NULL); 3996 now = gethrestime_sec(); 3997 maxage = now - ire->ire_create_time; 3998 if (maxage > ipst->ips_ip_max_temp_idle) 3999 maxage = ipst->ips_ip_max_temp_idle; 4000 lused = drv_hztousec(ddi_get_lbolt() - 4001 ire->ire_last_used_time) / MICROSEC + 1; 4002 if (lused >= maxage && (ipif->ipif_flags & 4003 (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) 4004 maxdefense = ipst->ips_ip_max_temp_defend; 4005 else 4006 maxdefense = ipst->ips_ip_max_defend; 4007 4008 /* 4009 * Now figure out how many times we've defended 4010 * ourselves. Ignore defenses that happened long in 4011 * the past. 4012 */ 4013 mutex_enter(&ire->ire_lock); 4014 if ((defs = ire->ire_defense_count) > 0 && 4015 now - ire->ire_defense_time > 4016 ipst->ips_ip_defend_interval) { 4017 ire->ire_defense_count = defs = 0; 4018 } 4019 ire->ire_defense_count++; 4020 ire->ire_defense_time = now; 4021 mutex_exit(&ire->ire_lock); 4022 ill_refhold(ill); 4023 ire_refrele(ire); 4024 4025 /* 4026 * If we've defended ourselves too many times already, 4027 * then give up and tear down the interface(s) using 4028 * this address. Otherwise, defend by sending out a 4029 * gratuitous ARP. 4030 */ 4031 if (defs >= maxdefense && ill->ill_arp_extend) { 4032 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, 4033 B_FALSE); 4034 } else { 4035 cmn_err(CE_WARN, 4036 "node %s is using our IP address %s on %s", 4037 hbuf, sbuf, ill->ill_name); 4038 /* 4039 * If this is an old (ATM) ARP module, then 4040 * don't try to defend the address. Remain 4041 * compatible with the old behavior. Defend 4042 * only with new ARP. 4043 */ 4044 if (ill->ill_arp_extend) { 4045 qwriter_ip(ill, q, mp, ip_arp_defend, 4046 NEW_OP, B_FALSE); 4047 } else { 4048 ill_refrele(ill); 4049 } 4050 } 4051 return; 4052 } 4053 cmn_err(CE_WARN, 4054 "proxy ARP problem? Node '%s' is using %s on %s", 4055 hbuf, sbuf, ill->ill_name); 4056 if (ire != NULL) 4057 ire_refrele(ire); 4058 break; 4059 case AR_CN_ANNOUNCE: 4060 if (isv6) { 4061 /* 4062 * For XRESOLV interfaces. 4063 * Delete the IRE cache entry and NCE for this 4064 * v6 address 4065 */ 4066 ip_ire_clookup_and_delete_v6(&v6src, ipst); 4067 /* 4068 * If v6src is a non-zero, it's a router address 4069 * as below. Do the same sort of thing to clean 4070 * out off-net IRE_CACHE entries that go through 4071 * the router. 4072 */ 4073 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4074 ire_walk_v6(ire_delete_cache_gw_v6, 4075 (char *)&v6src, ALL_ZONES, ipst); 4076 } 4077 } else { 4078 nce_hw_map_t hwm; 4079 4080 /* 4081 * ARP gives us a copy of any packet where it thinks 4082 * the address has changed, so that we can update our 4083 * caches. We're responsible for caching known answers 4084 * in the current design. We check whether the 4085 * hardware address really has changed in all of our 4086 * entries that have cached this mapping, and if so, we 4087 * blow them away. This way we will immediately pick 4088 * up the rare case of a host changing hardware 4089 * address. 4090 */ 4091 if (src == 0) 4092 break; 4093 hwm.hwm_addr = src; 4094 hwm.hwm_hwlen = arh->arh_hlen; 4095 hwm.hwm_hwaddr = (uchar_t *)(arh + 1); 4096 NDP_HW_CHANGE_INCR(ipst->ips_ndp4); 4097 ndp_walk_common(ipst->ips_ndp4, NULL, 4098 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); 4099 NDP_HW_CHANGE_DECR(ipst->ips_ndp4); 4100 } 4101 break; 4102 case AR_CN_READY: 4103 /* No external v6 resolver has a contract to use this */ 4104 if (isv6) 4105 break; 4106 /* If the link is down, we'll retry this later */ 4107 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) 4108 break; 4109 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, 4110 NULL, NULL, ipst); 4111 if (ipif != NULL) { 4112 /* 4113 * If this is a duplicate recovery, then we now need to 4114 * go exclusive to bring this thing back up. 4115 */ 4116 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == 4117 IPIF_DUPLICATE) { 4118 ipif_refrele(ipif); 4119 ill_refhold(ill); 4120 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, 4121 B_FALSE); 4122 return; 4123 } 4124 /* 4125 * If this is the first notice that this address is 4126 * ready, then let the user know now. 4127 */ 4128 if ((ipif->ipif_flags & IPIF_UP) && 4129 !ipif->ipif_addr_ready) { 4130 ipif_mask_reply(ipif); 4131 ip_rts_ifmsg(ipif); 4132 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 4133 sctp_update_ipif(ipif, SCTP_IPIF_UP); 4134 } 4135 ipif->ipif_addr_ready = 1; 4136 ipif_refrele(ipif); 4137 } 4138 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp), ipst); 4139 if (ire != NULL) { 4140 ire->ire_defense_count = 0; 4141 ire_refrele(ire); 4142 } 4143 break; 4144 case AR_CN_FAILED: 4145 /* No external v6 resolver has a contract to use this */ 4146 if (isv6) 4147 break; 4148 ill_refhold(ill); 4149 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE); 4150 return; 4151 } 4152 freemsg(mp); 4153 } 4154 4155 /* 4156 * Create a mblk suitable for carrying the interface index and/or source link 4157 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 4158 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 4159 * application. 4160 */ 4161 mblk_t * 4162 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid, 4163 ip_stack_t *ipst) 4164 { 4165 mblk_t *mp; 4166 ip_pktinfo_t *pinfo; 4167 ipha_t *ipha; 4168 struct ether_header *pether; 4169 4170 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED); 4171 if (mp == NULL) { 4172 ip1dbg(("ip_add_info: allocation failure.\n")); 4173 return (data_mp); 4174 } 4175 4176 ipha = (ipha_t *)data_mp->b_rptr; 4177 pinfo = (ip_pktinfo_t *)mp->b_rptr; 4178 bzero(pinfo, sizeof (ip_pktinfo_t)); 4179 pinfo->ip_pkt_flags = (uchar_t)flags; 4180 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 4181 4182 if (flags & (IPF_RECVIF | IPF_RECVADDR)) 4183 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 4184 if (flags & IPF_RECVADDR) { 4185 ipif_t *ipif; 4186 ire_t *ire; 4187 4188 /* 4189 * Only valid for V4 4190 */ 4191 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) == 4192 (IPV4_VERSION << 4)); 4193 4194 ipif = ipif_get_next_ipif(NULL, ill); 4195 if (ipif != NULL) { 4196 /* 4197 * Since a decision has already been made to deliver the 4198 * packet, there is no need to test for SECATTR and 4199 * ZONEONLY. 4200 * When a multicast packet is transmitted 4201 * a cache entry is created for the multicast address. 4202 * When delivering a copy of the packet or when new 4203 * packets are received we do not want to match on the 4204 * cached entry so explicitly match on 4205 * IRE_LOCAL and IRE_LOOPBACK 4206 */ 4207 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 4208 IRE_LOCAL | IRE_LOOPBACK, 4209 ipif, zoneid, NULL, 4210 MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP, ipst); 4211 if (ire == NULL) { 4212 /* 4213 * packet must have come on a different 4214 * interface. 4215 * Since a decision has already been made to 4216 * deliver the packet, there is no need to test 4217 * for SECATTR and ZONEONLY. 4218 * Only match on local and broadcast ire's. 4219 * See detailed comment above. 4220 */ 4221 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 4222 IRE_LOCAL | IRE_LOOPBACK, ipif, zoneid, 4223 NULL, MATCH_IRE_TYPE, ipst); 4224 } 4225 4226 if (ire == NULL) { 4227 /* 4228 * This is either a multicast packet or 4229 * the address has been removed since 4230 * the packet was received. 4231 * Return INADDR_ANY so that normal source 4232 * selection occurs for the response. 4233 */ 4234 4235 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4236 } else { 4237 pinfo->ip_pkt_match_addr.s_addr = 4238 ire->ire_src_addr; 4239 ire_refrele(ire); 4240 } 4241 ipif_refrele(ipif); 4242 } else { 4243 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4244 } 4245 } 4246 4247 pether = (struct ether_header *)((char *)ipha 4248 - sizeof (struct ether_header)); 4249 /* 4250 * Make sure the interface is an ethernet type, since this option 4251 * is currently supported only on this type of interface. Also make 4252 * sure we are pointing correctly above db_base. 4253 */ 4254 4255 if ((flags & IPF_RECVSLLA) && 4256 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 4257 (ill->ill_type == IFT_ETHER) && 4258 (ill->ill_net_type == IRE_IF_RESOLVER)) { 4259 4260 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER; 4261 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, 4262 (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL); 4263 } else { 4264 /* 4265 * Clear the bit. Indicate to upper layer that IP is not 4266 * sending this ancillary info. 4267 */ 4268 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA; 4269 } 4270 4271 mp->b_datap->db_type = M_CTL; 4272 mp->b_wptr += sizeof (ip_pktinfo_t); 4273 mp->b_cont = data_mp; 4274 4275 return (mp); 4276 } 4277 4278 /* 4279 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 4280 * part of the bind request. 4281 */ 4282 4283 boolean_t 4284 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 4285 { 4286 ipsec_in_t *ii; 4287 4288 ASSERT(policy_mp != NULL); 4289 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 4290 4291 ii = (ipsec_in_t *)policy_mp->b_rptr; 4292 ASSERT(ii->ipsec_in_type == IPSEC_IN); 4293 4294 connp->conn_policy = ii->ipsec_in_policy; 4295 ii->ipsec_in_policy = NULL; 4296 4297 if (ii->ipsec_in_action != NULL) { 4298 if (connp->conn_latch == NULL) { 4299 connp->conn_latch = iplatch_create(); 4300 if (connp->conn_latch == NULL) 4301 return (B_FALSE); 4302 } 4303 ipsec_latch_inbound(connp->conn_latch, ii); 4304 } 4305 return (B_TRUE); 4306 } 4307 4308 /* 4309 * Upper level protocols (ULP) pass through bind requests to IP for inspection 4310 * and to arrange for power-fanout assist. The ULP is identified by 4311 * adding a single byte at the end of the original bind message. 4312 * A ULP other than UDP or TCP that wishes to be recognized passes 4313 * down a bind with a zero length address. 4314 * 4315 * The binding works as follows: 4316 * - A zero byte address means just bind to the protocol. 4317 * - A four byte address is treated as a request to validate 4318 * that the address is a valid local address, appropriate for 4319 * an application to bind to. This does not affect any fanout 4320 * information in IP. 4321 * - A sizeof sin_t byte address is used to bind to only the local address 4322 * and port. 4323 * - A sizeof ipa_conn_t byte address contains complete fanout information 4324 * consisting of local and remote addresses and ports. In 4325 * this case, the addresses are both validated as appropriate 4326 * for this operation, and, if so, the information is retained 4327 * for use in the inbound fanout. 4328 * 4329 * The ULP (except in the zero-length bind) can append an 4330 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 4331 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 4332 * a copy of the source or destination IRE (source for local bind; 4333 * destination for complete bind). IPSEC_POLICY_SET indicates that the 4334 * policy information contained should be copied on to the conn. 4335 * 4336 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 4337 */ 4338 mblk_t * 4339 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 4340 { 4341 ssize_t len; 4342 struct T_bind_req *tbr; 4343 sin_t *sin; 4344 ipa_conn_t *ac; 4345 uchar_t *ucp; 4346 mblk_t *mp1; 4347 boolean_t ire_requested; 4348 boolean_t ipsec_policy_set = B_FALSE; 4349 int error = 0; 4350 int protocol; 4351 ipa_conn_x_t *acx; 4352 4353 ASSERT(!connp->conn_af_isv6); 4354 connp->conn_pkt_isv6 = B_FALSE; 4355 4356 len = MBLKL(mp); 4357 if (len < (sizeof (*tbr) + 1)) { 4358 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 4359 "ip_bind: bogus msg, len %ld", len); 4360 /* XXX: Need to return something better */ 4361 goto bad_addr; 4362 } 4363 /* Back up and extract the protocol identifier. */ 4364 mp->b_wptr--; 4365 protocol = *mp->b_wptr & 0xFF; 4366 tbr = (struct T_bind_req *)mp->b_rptr; 4367 /* Reset the message type in preparation for shipping it back. */ 4368 DB_TYPE(mp) = M_PCPROTO; 4369 4370 connp->conn_ulp = (uint8_t)protocol; 4371 4372 /* 4373 * Check for a zero length address. This is from a protocol that 4374 * wants to register to receive all packets of its type. 4375 */ 4376 if (tbr->ADDR_length == 0) { 4377 /* 4378 * These protocols are now intercepted in ip_bind_v6(). 4379 * Reject protocol-level binds here for now. 4380 * 4381 * For SCTP raw socket, ICMP sends down a bind with sin_t 4382 * so that the protocol type cannot be SCTP. 4383 */ 4384 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 4385 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 4386 goto bad_addr; 4387 } 4388 4389 /* 4390 * 4391 * The udp module never sends down a zero-length address, 4392 * and allowing this on a labeled system will break MLP 4393 * functionality. 4394 */ 4395 if (is_system_labeled() && protocol == IPPROTO_UDP) 4396 goto bad_addr; 4397 4398 if (connp->conn_mac_exempt) 4399 goto bad_addr; 4400 4401 /* No hash here really. The table is big enough. */ 4402 connp->conn_srcv6 = ipv6_all_zeros; 4403 4404 ipcl_proto_insert(connp, protocol); 4405 4406 tbr->PRIM_type = T_BIND_ACK; 4407 return (mp); 4408 } 4409 4410 /* Extract the address pointer from the message. */ 4411 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 4412 tbr->ADDR_length); 4413 if (ucp == NULL) { 4414 ip1dbg(("ip_bind: no address\n")); 4415 goto bad_addr; 4416 } 4417 if (!OK_32PTR(ucp)) { 4418 ip1dbg(("ip_bind: unaligned address\n")); 4419 goto bad_addr; 4420 } 4421 /* 4422 * Check for trailing mps. 4423 */ 4424 4425 mp1 = mp->b_cont; 4426 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 4427 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); 4428 4429 switch (tbr->ADDR_length) { 4430 default: 4431 ip1dbg(("ip_bind: bad address length %d\n", 4432 (int)tbr->ADDR_length)); 4433 goto bad_addr; 4434 4435 case IP_ADDR_LEN: 4436 /* Verification of local address only */ 4437 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, 4438 ire_requested, ipsec_policy_set, B_FALSE); 4439 break; 4440 4441 case sizeof (sin_t): 4442 sin = (sin_t *)ucp; 4443 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, 4444 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); 4445 break; 4446 4447 case sizeof (ipa_conn_t): 4448 ac = (ipa_conn_t *)ucp; 4449 /* For raw socket, the local port is not set. */ 4450 if (ac->ac_lport == 0) 4451 ac->ac_lport = connp->conn_lport; 4452 /* Always verify destination reachability. */ 4453 error = ip_bind_connected(connp, mp, &ac->ac_laddr, 4454 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, 4455 ipsec_policy_set, B_TRUE, B_TRUE); 4456 break; 4457 4458 case sizeof (ipa_conn_x_t): 4459 acx = (ipa_conn_x_t *)ucp; 4460 /* 4461 * Whether or not to verify destination reachability depends 4462 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 4463 */ 4464 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, 4465 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, 4466 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, 4467 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); 4468 break; 4469 } 4470 if (error == EINPROGRESS) 4471 return (NULL); 4472 else if (error != 0) 4473 goto bad_addr; 4474 /* 4475 * Pass the IPsec headers size in ire_ipsec_overhead. 4476 * We can't do this in ip_bind_insert_ire because the policy 4477 * may not have been inherited at that point in time and hence 4478 * conn_out_enforce_policy may not be set. 4479 */ 4480 mp1 = mp->b_cont; 4481 if (ire_requested && connp->conn_out_enforce_policy && 4482 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 4483 ire_t *ire = (ire_t *)mp1->b_rptr; 4484 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 4485 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 4486 } 4487 4488 /* Send it home. */ 4489 mp->b_datap->db_type = M_PCPROTO; 4490 tbr->PRIM_type = T_BIND_ACK; 4491 return (mp); 4492 4493 bad_addr: 4494 /* 4495 * If error = -1 then we generate a TBADADDR - otherwise error is 4496 * a unix errno. 4497 */ 4498 if (error > 0) 4499 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 4500 else 4501 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 4502 return (mp); 4503 } 4504 4505 /* 4506 * Here address is verified to be a valid local address. 4507 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 4508 * address is also considered a valid local address. 4509 * In the case of a broadcast/multicast address, however, the 4510 * upper protocol is expected to reset the src address 4511 * to 0 if it sees a IRE_BROADCAST type returned so that 4512 * no packets are emitted with broadcast/multicast address as 4513 * source address (that violates hosts requirements RFC1122) 4514 * The addresses valid for bind are: 4515 * (1) - INADDR_ANY (0) 4516 * (2) - IP address of an UP interface 4517 * (3) - IP address of a DOWN interface 4518 * (4) - valid local IP broadcast addresses. In this case 4519 * the conn will only receive packets destined to 4520 * the specified broadcast address. 4521 * (5) - a multicast address. In this case 4522 * the conn will only receive packets destined to 4523 * the specified multicast address. Note: the 4524 * application still has to issue an 4525 * IP_ADD_MEMBERSHIP socket option. 4526 * 4527 * On error, return -1 for TBADADDR otherwise pass the 4528 * errno with TSYSERR reply. 4529 * 4530 * In all the above cases, the bound address must be valid in the current zone. 4531 * When the address is loopback, multicast or broadcast, there might be many 4532 * matching IREs so bind has to look up based on the zone. 4533 * 4534 * Note: lport is in network byte order. 4535 */ 4536 int 4537 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, 4538 boolean_t ire_requested, boolean_t ipsec_policy_set, 4539 boolean_t fanout_insert) 4540 { 4541 int error = 0; 4542 ire_t *src_ire; 4543 mblk_t *policy_mp; 4544 ipif_t *ipif; 4545 zoneid_t zoneid; 4546 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4547 4548 if (ipsec_policy_set) { 4549 policy_mp = mp->b_cont; 4550 } 4551 4552 /* 4553 * If it was previously connected, conn_fully_bound would have 4554 * been set. 4555 */ 4556 connp->conn_fully_bound = B_FALSE; 4557 4558 src_ire = NULL; 4559 ipif = NULL; 4560 4561 zoneid = IPCL_ZONEID(connp); 4562 4563 if (src_addr) { 4564 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 4565 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 4566 /* 4567 * If an address other than 0.0.0.0 is requested, 4568 * we verify that it is a valid address for bind 4569 * Note: Following code is in if-else-if form for 4570 * readability compared to a condition check. 4571 */ 4572 /* LINTED - statement has no consequent */ 4573 if (IRE_IS_LOCAL(src_ire)) { 4574 /* 4575 * (2) Bind to address of local UP interface 4576 */ 4577 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 4578 /* 4579 * (4) Bind to broadcast address 4580 * Note: permitted only from transports that 4581 * request IRE 4582 */ 4583 if (!ire_requested) 4584 error = EADDRNOTAVAIL; 4585 } else { 4586 /* 4587 * (3) Bind to address of local DOWN interface 4588 * (ipif_lookup_addr() looks up all interfaces 4589 * but we do not get here for UP interfaces 4590 * - case (2) above) 4591 * We put the protocol byte back into the mblk 4592 * since we may come back via ip_wput_nondata() 4593 * later with this mblk if ipif_lookup_addr chooses 4594 * to defer processing. 4595 */ 4596 *mp->b_wptr++ = (char)connp->conn_ulp; 4597 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, 4598 CONNP_TO_WQ(connp), mp, ip_wput_nondata, 4599 &error, ipst)) != NULL) { 4600 ipif_refrele(ipif); 4601 } else if (error == EINPROGRESS) { 4602 if (src_ire != NULL) 4603 ire_refrele(src_ire); 4604 return (EINPROGRESS); 4605 } else if (CLASSD(src_addr)) { 4606 error = 0; 4607 if (src_ire != NULL) 4608 ire_refrele(src_ire); 4609 /* 4610 * (5) bind to multicast address. 4611 * Fake out the IRE returned to upper 4612 * layer to be a broadcast IRE. 4613 */ 4614 src_ire = ire_ctable_lookup( 4615 INADDR_BROADCAST, INADDR_ANY, 4616 IRE_BROADCAST, NULL, zoneid, NULL, 4617 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY), 4618 ipst); 4619 if (src_ire == NULL || !ire_requested) 4620 error = EADDRNOTAVAIL; 4621 } else { 4622 /* 4623 * Not a valid address for bind 4624 */ 4625 error = EADDRNOTAVAIL; 4626 } 4627 /* 4628 * Just to keep it consistent with the processing in 4629 * ip_bind_v4() 4630 */ 4631 mp->b_wptr--; 4632 } 4633 if (error) { 4634 /* Red Alert! Attempting to be a bogon! */ 4635 ip1dbg(("ip_bind: bad src address 0x%x\n", 4636 ntohl(src_addr))); 4637 goto bad_addr; 4638 } 4639 } 4640 4641 /* 4642 * Allow setting new policies. For example, disconnects come 4643 * down as ipa_t bind. As we would have set conn_policy_cached 4644 * to B_TRUE before, we should set it to B_FALSE, so that policy 4645 * can change after the disconnect. 4646 */ 4647 connp->conn_policy_cached = B_FALSE; 4648 4649 /* 4650 * If not fanout_insert this was just an address verification 4651 */ 4652 if (fanout_insert) { 4653 /* 4654 * The addresses have been verified. Time to insert in 4655 * the correct fanout list. 4656 */ 4657 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4658 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4659 connp->conn_lport = lport; 4660 connp->conn_fport = 0; 4661 /* 4662 * Do we need to add a check to reject Multicast packets 4663 * 4664 * We need to make sure that the conn_recv is set to a non-null 4665 * value before we insert the conn into the classifier table. 4666 * This is to avoid a race with an incoming packet which does an 4667 * ipcl_classify(). 4668 */ 4669 if (*mp->b_wptr == IPPROTO_TCP) 4670 connp->conn_recv = tcp_conn_request; 4671 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); 4672 } 4673 4674 if (error == 0) { 4675 if (ire_requested) { 4676 if (!ip_bind_insert_ire(mp, src_ire, NULL, ipst)) { 4677 error = -1; 4678 /* Falls through to bad_addr */ 4679 } 4680 } else if (ipsec_policy_set) { 4681 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4682 error = -1; 4683 /* Falls through to bad_addr */ 4684 } 4685 } 4686 } else if (connp->conn_ulp == IPPROTO_TCP) { 4687 connp->conn_recv = tcp_input; 4688 } 4689 bad_addr: 4690 if (error != 0) { 4691 if (connp->conn_anon_port) { 4692 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4693 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 4694 B_FALSE); 4695 } 4696 connp->conn_mlp_type = mlptSingle; 4697 } 4698 if (src_ire != NULL) 4699 IRE_REFRELE(src_ire); 4700 if (ipsec_policy_set) { 4701 ASSERT(policy_mp == mp->b_cont); 4702 ASSERT(policy_mp != NULL); 4703 freeb(policy_mp); 4704 /* 4705 * As of now assume that nothing else accompanies 4706 * IPSEC_POLICY_SET. 4707 */ 4708 mp->b_cont = NULL; 4709 } 4710 return (error); 4711 } 4712 4713 /* 4714 * Verify that both the source and destination addresses 4715 * are valid. If verify_dst is false, then the destination address may be 4716 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4717 * destination reachability, while tunnels do not. 4718 * Note that we allow connect to broadcast and multicast 4719 * addresses when ire_requested is set. Thus the ULP 4720 * has to check for IRE_BROADCAST and multicast. 4721 * 4722 * Returns zero if ok. 4723 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4724 * (for use with TSYSERR reply). 4725 * 4726 * Note: lport and fport are in network byte order. 4727 */ 4728 int 4729 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, 4730 uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4731 boolean_t ire_requested, boolean_t ipsec_policy_set, 4732 boolean_t fanout_insert, boolean_t verify_dst) 4733 { 4734 ire_t *src_ire; 4735 ire_t *dst_ire; 4736 int error = 0; 4737 int protocol; 4738 mblk_t *policy_mp; 4739 ire_t *sire = NULL; 4740 ire_t *md_dst_ire = NULL; 4741 ire_t *lso_dst_ire = NULL; 4742 ill_t *ill = NULL; 4743 zoneid_t zoneid; 4744 ipaddr_t src_addr = *src_addrp; 4745 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4746 4747 src_ire = dst_ire = NULL; 4748 protocol = *mp->b_wptr & 0xFF; 4749 4750 /* 4751 * If we never got a disconnect before, clear it now. 4752 */ 4753 connp->conn_fully_bound = B_FALSE; 4754 4755 if (ipsec_policy_set) { 4756 policy_mp = mp->b_cont; 4757 } 4758 4759 zoneid = IPCL_ZONEID(connp); 4760 4761 if (CLASSD(dst_addr)) { 4762 /* Pick up an IRE_BROADCAST */ 4763 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4764 NULL, zoneid, MBLK_GETLABEL(mp), 4765 (MATCH_IRE_RECURSIVE | 4766 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | 4767 MATCH_IRE_SECATTR), ipst); 4768 } else { 4769 /* 4770 * If conn_dontroute is set or if conn_nexthop_set is set, 4771 * and onlink ipif is not found set ENETUNREACH error. 4772 */ 4773 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4774 ipif_t *ipif; 4775 4776 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4777 dst_addr : connp->conn_nexthop_v4, zoneid, ipst); 4778 if (ipif == NULL) { 4779 error = ENETUNREACH; 4780 goto bad_addr; 4781 } 4782 ipif_refrele(ipif); 4783 } 4784 4785 if (connp->conn_nexthop_set) { 4786 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4787 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), 4788 MATCH_IRE_SECATTR, ipst); 4789 } else { 4790 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4791 &sire, zoneid, MBLK_GETLABEL(mp), 4792 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4793 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | 4794 MATCH_IRE_SECATTR), ipst); 4795 } 4796 } 4797 /* 4798 * dst_ire can't be a broadcast when not ire_requested. 4799 * We also prevent ire's with src address INADDR_ANY to 4800 * be used, which are created temporarily for 4801 * sending out packets from endpoints that have 4802 * conn_unspec_src set. If verify_dst is true, the destination must be 4803 * reachable. If verify_dst is false, the destination needn't be 4804 * reachable. 4805 * 4806 * If we match on a reject or black hole, then we've got a 4807 * local failure. May as well fail out the connect() attempt, 4808 * since it's never going to succeed. 4809 */ 4810 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4811 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4812 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4813 /* 4814 * If we're verifying destination reachability, we always want 4815 * to complain here. 4816 * 4817 * If we're not verifying destination reachability but the 4818 * destination has a route, we still want to fail on the 4819 * temporary address and broadcast address tests. 4820 */ 4821 if (verify_dst || (dst_ire != NULL)) { 4822 if (ip_debug > 2) { 4823 pr_addr_dbg("ip_bind_connected: bad connected " 4824 "dst %s\n", AF_INET, &dst_addr); 4825 } 4826 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4827 error = ENETUNREACH; 4828 else 4829 error = EHOSTUNREACH; 4830 goto bad_addr; 4831 } 4832 } 4833 4834 /* 4835 * We now know that routing will allow us to reach the destination. 4836 * Check whether Trusted Solaris policy allows communication with this 4837 * host, and pretend that the destination is unreachable if not. 4838 * 4839 * This is never a problem for TCP, since that transport is known to 4840 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 4841 * handling. If the remote is unreachable, it will be detected at that 4842 * point, so there's no reason to check it here. 4843 * 4844 * Note that for sendto (and other datagram-oriented friends), this 4845 * check is done as part of the data path label computation instead. 4846 * The check here is just to make non-TCP connect() report the right 4847 * error. 4848 */ 4849 if (dst_ire != NULL && is_system_labeled() && 4850 !IPCL_IS_TCP(connp) && 4851 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL, 4852 connp->conn_mac_exempt, ipst) != 0) { 4853 error = EHOSTUNREACH; 4854 if (ip_debug > 2) { 4855 pr_addr_dbg("ip_bind_connected: no label for dst %s\n", 4856 AF_INET, &dst_addr); 4857 } 4858 goto bad_addr; 4859 } 4860 4861 /* 4862 * If the app does a connect(), it means that it will most likely 4863 * send more than 1 packet to the destination. It makes sense 4864 * to clear the temporary flag. 4865 */ 4866 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4867 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4868 irb_t *irb = dst_ire->ire_bucket; 4869 4870 rw_enter(&irb->irb_lock, RW_WRITER); 4871 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4872 irb->irb_tmp_ire_cnt--; 4873 rw_exit(&irb->irb_lock); 4874 } 4875 4876 /* 4877 * See if we should notify ULP about LSO/MDT; we do this whether or not 4878 * ire_requested is TRUE, in order to handle active connects; LSO/MDT 4879 * eligibility tests for passive connects are handled separately 4880 * through tcp_adapt_ire(). We do this before the source address 4881 * selection, because dst_ire may change after a call to 4882 * ipif_select_source(). This is a best-effort check, as the 4883 * packet for this connection may not actually go through 4884 * dst_ire->ire_stq, and the exact IRE can only be known after 4885 * calling ip_newroute(). This is why we further check on the 4886 * IRE during LSO/Multidata packet transmission in 4887 * tcp_lsosend()/tcp_multisend(). 4888 */ 4889 if (!ipsec_policy_set && dst_ire != NULL && 4890 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4891 (ill = ire_to_ill(dst_ire), ill != NULL)) { 4892 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 4893 lso_dst_ire = dst_ire; 4894 IRE_REFHOLD(lso_dst_ire); 4895 } else if (ipst->ips_ip_multidata_outbound && 4896 ILL_MDT_CAPABLE(ill)) { 4897 md_dst_ire = dst_ire; 4898 IRE_REFHOLD(md_dst_ire); 4899 } 4900 } 4901 4902 if (dst_ire != NULL && 4903 dst_ire->ire_type == IRE_LOCAL && 4904 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { 4905 /* 4906 * If the IRE belongs to a different zone, look for a matching 4907 * route in the forwarding table and use the source address from 4908 * that route. 4909 */ 4910 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 4911 zoneid, 0, NULL, 4912 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4913 MATCH_IRE_RJ_BHOLE, ipst); 4914 if (src_ire == NULL) { 4915 error = EHOSTUNREACH; 4916 goto bad_addr; 4917 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 4918 if (!(src_ire->ire_type & IRE_HOST)) 4919 error = ENETUNREACH; 4920 else 4921 error = EHOSTUNREACH; 4922 goto bad_addr; 4923 } 4924 if (src_addr == INADDR_ANY) 4925 src_addr = src_ire->ire_src_addr; 4926 ire_refrele(src_ire); 4927 src_ire = NULL; 4928 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 4929 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 4930 src_addr = sire->ire_src_addr; 4931 ire_refrele(dst_ire); 4932 dst_ire = sire; 4933 sire = NULL; 4934 } else { 4935 /* 4936 * Pick a source address so that a proper inbound 4937 * load spreading would happen. 4938 */ 4939 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; 4940 ipif_t *src_ipif = NULL; 4941 ire_t *ipif_ire; 4942 4943 /* 4944 * Supply a local source address such that inbound 4945 * load spreading happens. 4946 * 4947 * Determine the best source address on this ill for 4948 * the destination. 4949 * 4950 * 1) For broadcast, we should return a broadcast ire 4951 * found above so that upper layers know that the 4952 * destination address is a broadcast address. 4953 * 4954 * 2) If this is part of a group, select a better 4955 * source address so that better inbound load 4956 * balancing happens. Do the same if the ipif 4957 * is DEPRECATED. 4958 * 4959 * 3) If the outgoing interface is part of a usesrc 4960 * group, then try selecting a source address from 4961 * the usesrc ILL. 4962 */ 4963 if ((dst_ire->ire_zoneid != zoneid && 4964 dst_ire->ire_zoneid != ALL_ZONES) || 4965 (!(dst_ire->ire_type & IRE_BROADCAST) && 4966 ((dst_ill->ill_group != NULL) || 4967 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 4968 (dst_ill->ill_usesrc_ifindex != 0)))) { 4969 /* 4970 * If the destination is reachable via a 4971 * given gateway, the selected source address 4972 * should be in the same subnet as the gateway. 4973 * Otherwise, the destination is not reachable. 4974 * 4975 * If there are no interfaces on the same subnet 4976 * as the destination, ipif_select_source gives 4977 * first non-deprecated interface which might be 4978 * on a different subnet than the gateway. 4979 * This is not desirable. Hence pass the dst_ire 4980 * source address to ipif_select_source. 4981 * It is sure that the destination is reachable 4982 * with the dst_ire source address subnet. 4983 * So passing dst_ire source address to 4984 * ipif_select_source will make sure that the 4985 * selected source will be on the same subnet 4986 * as dst_ire source address. 4987 */ 4988 ipaddr_t saddr = 4989 dst_ire->ire_ipif->ipif_src_addr; 4990 src_ipif = ipif_select_source(dst_ill, 4991 saddr, zoneid); 4992 if (src_ipif != NULL) { 4993 if (IS_VNI(src_ipif->ipif_ill)) { 4994 /* 4995 * For VNI there is no 4996 * interface route 4997 */ 4998 src_addr = 4999 src_ipif->ipif_src_addr; 5000 } else { 5001 ipif_ire = 5002 ipif_to_ire(src_ipif); 5003 if (ipif_ire != NULL) { 5004 IRE_REFRELE(dst_ire); 5005 dst_ire = ipif_ire; 5006 } 5007 src_addr = 5008 dst_ire->ire_src_addr; 5009 } 5010 ipif_refrele(src_ipif); 5011 } else { 5012 src_addr = dst_ire->ire_src_addr; 5013 } 5014 } else { 5015 src_addr = dst_ire->ire_src_addr; 5016 } 5017 } 5018 } 5019 5020 /* 5021 * We do ire_route_lookup() here (and not 5022 * interface lookup as we assert that 5023 * src_addr should only come from an 5024 * UP interface for hard binding. 5025 */ 5026 ASSERT(src_ire == NULL); 5027 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 5028 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 5029 /* src_ire must be a local|loopback */ 5030 if (!IRE_IS_LOCAL(src_ire)) { 5031 if (ip_debug > 2) { 5032 pr_addr_dbg("ip_bind_connected: bad connected " 5033 "src %s\n", AF_INET, &src_addr); 5034 } 5035 error = EADDRNOTAVAIL; 5036 goto bad_addr; 5037 } 5038 5039 /* 5040 * If the source address is a loopback address, the 5041 * destination had best be local or multicast. 5042 * The transports that can't handle multicast will reject 5043 * those addresses. 5044 */ 5045 if (src_ire->ire_type == IRE_LOOPBACK && 5046 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 5047 ip1dbg(("ip_bind_connected: bad connected loopback\n")); 5048 error = -1; 5049 goto bad_addr; 5050 } 5051 5052 /* 5053 * Allow setting new policies. For example, disconnects come 5054 * down as ipa_t bind. As we would have set conn_policy_cached 5055 * to B_TRUE before, we should set it to B_FALSE, so that policy 5056 * can change after the disconnect. 5057 */ 5058 connp->conn_policy_cached = B_FALSE; 5059 5060 /* 5061 * Set the conn addresses/ports immediately, so the IPsec policy calls 5062 * can handle their passed-in conn's. 5063 */ 5064 5065 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 5066 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 5067 connp->conn_lport = lport; 5068 connp->conn_fport = fport; 5069 *src_addrp = src_addr; 5070 5071 ASSERT(!(ipsec_policy_set && ire_requested)); 5072 if (ire_requested) { 5073 iulp_t *ulp_info = NULL; 5074 5075 /* 5076 * Note that sire will not be NULL if this is an off-link 5077 * connection and there is not cache for that dest yet. 5078 * 5079 * XXX Because of an existing bug, if there are multiple 5080 * default routes, the IRE returned now may not be the actual 5081 * default route used (default routes are chosen in a 5082 * round robin fashion). So if the metrics for different 5083 * default routes are different, we may return the wrong 5084 * metrics. This will not be a problem if the existing 5085 * bug is fixed. 5086 */ 5087 if (sire != NULL) { 5088 ulp_info = &(sire->ire_uinfo); 5089 } 5090 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info, ipst)) { 5091 error = -1; 5092 goto bad_addr; 5093 } 5094 } else if (ipsec_policy_set) { 5095 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 5096 error = -1; 5097 goto bad_addr; 5098 } 5099 } 5100 5101 /* 5102 * Cache IPsec policy in this conn. If we have per-socket policy, 5103 * we'll cache that. If we don't, we'll inherit global policy. 5104 * 5105 * We can't insert until the conn reflects the policy. Note that 5106 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 5107 * connections where we don't have a policy. This is to prevent 5108 * global policy lookups in the inbound path. 5109 * 5110 * If we insert before we set conn_policy_cached, 5111 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 5112 * because global policy cound be non-empty. We normally call 5113 * ipsec_check_policy() for conn_policy_cached connections only if 5114 * ipc_in_enforce_policy is set. But in this case, 5115 * conn_policy_cached can get set anytime since we made the 5116 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 5117 * called, which will make the above assumption false. Thus, we 5118 * need to insert after we set conn_policy_cached. 5119 */ 5120 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 5121 goto bad_addr; 5122 5123 if (fanout_insert) { 5124 /* 5125 * The addresses have been verified. Time to insert in 5126 * the correct fanout list. 5127 * We need to make sure that the conn_recv is set to a non-null 5128 * value before we insert into the classifier table to avoid a 5129 * race with an incoming packet which does an ipcl_classify(). 5130 */ 5131 if (protocol == IPPROTO_TCP) 5132 connp->conn_recv = tcp_input; 5133 error = ipcl_conn_insert(connp, protocol, src_addr, 5134 dst_addr, connp->conn_ports); 5135 } 5136 5137 if (error == 0) { 5138 connp->conn_fully_bound = B_TRUE; 5139 /* 5140 * Our initial checks for LSO/MDT have passed; the IRE is not 5141 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 5142 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into 5143 * ip_xxinfo_return(), which performs further checks 5144 * against them and upon success, returns the LSO/MDT info 5145 * mblk which we will attach to the bind acknowledgment. 5146 */ 5147 if (lso_dst_ire != NULL) { 5148 mblk_t *lsoinfo_mp; 5149 5150 ASSERT(ill->ill_lso_capab != NULL); 5151 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp, 5152 ill->ill_name, ill->ill_lso_capab)) != NULL) 5153 linkb(mp, lsoinfo_mp); 5154 } else if (md_dst_ire != NULL) { 5155 mblk_t *mdinfo_mp; 5156 5157 ASSERT(ill->ill_mdt_capab != NULL); 5158 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 5159 ill->ill_name, ill->ill_mdt_capab)) != NULL) 5160 linkb(mp, mdinfo_mp); 5161 } 5162 } 5163 bad_addr: 5164 if (ipsec_policy_set) { 5165 ASSERT(policy_mp == mp->b_cont); 5166 ASSERT(policy_mp != NULL); 5167 freeb(policy_mp); 5168 /* 5169 * As of now assume that nothing else accompanies 5170 * IPSEC_POLICY_SET. 5171 */ 5172 mp->b_cont = NULL; 5173 } 5174 if (src_ire != NULL) 5175 IRE_REFRELE(src_ire); 5176 if (dst_ire != NULL) 5177 IRE_REFRELE(dst_ire); 5178 if (sire != NULL) 5179 IRE_REFRELE(sire); 5180 if (md_dst_ire != NULL) 5181 IRE_REFRELE(md_dst_ire); 5182 if (lso_dst_ire != NULL) 5183 IRE_REFRELE(lso_dst_ire); 5184 return (error); 5185 } 5186 5187 /* 5188 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 5189 * Prefers dst_ire over src_ire. 5190 */ 5191 static boolean_t 5192 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst) 5193 { 5194 mblk_t *mp1; 5195 ire_t *ret_ire = NULL; 5196 5197 mp1 = mp->b_cont; 5198 ASSERT(mp1 != NULL); 5199 5200 if (ire != NULL) { 5201 /* 5202 * mp1 initialized above to IRE_DB_REQ_TYPE 5203 * appended mblk. Its <upper protocol>'s 5204 * job to make sure there is room. 5205 */ 5206 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 5207 return (0); 5208 5209 mp1->b_datap->db_type = IRE_DB_TYPE; 5210 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 5211 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 5212 ret_ire = (ire_t *)mp1->b_rptr; 5213 /* 5214 * Pass the latest setting of the ip_path_mtu_discovery and 5215 * copy the ulp info if any. 5216 */ 5217 ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? 5218 IPH_DF : 0; 5219 if (ulp_info != NULL) { 5220 bcopy(ulp_info, &(ret_ire->ire_uinfo), 5221 sizeof (iulp_t)); 5222 } 5223 ret_ire->ire_mp = mp1; 5224 } else { 5225 /* 5226 * No IRE was found. Remove IRE mblk. 5227 */ 5228 mp->b_cont = mp1->b_cont; 5229 freeb(mp1); 5230 } 5231 5232 return (1); 5233 } 5234 5235 /* 5236 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 5237 * the final piece where we don't. Return a pointer to the first mblk in the 5238 * result, and update the pointer to the next mblk to chew on. If anything 5239 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 5240 * NULL pointer. 5241 */ 5242 mblk_t * 5243 ip_carve_mp(mblk_t **mpp, ssize_t len) 5244 { 5245 mblk_t *mp0; 5246 mblk_t *mp1; 5247 mblk_t *mp2; 5248 5249 if (!len || !mpp || !(mp0 = *mpp)) 5250 return (NULL); 5251 /* If we aren't going to consume the first mblk, we need a dup. */ 5252 if (mp0->b_wptr - mp0->b_rptr > len) { 5253 mp1 = dupb(mp0); 5254 if (mp1) { 5255 /* Partition the data between the two mblks. */ 5256 mp1->b_wptr = mp1->b_rptr + len; 5257 mp0->b_rptr = mp1->b_wptr; 5258 /* 5259 * after adjustments if mblk not consumed is now 5260 * unaligned, try to align it. If this fails free 5261 * all messages and let upper layer recover. 5262 */ 5263 if (!OK_32PTR(mp0->b_rptr)) { 5264 if (!pullupmsg(mp0, -1)) { 5265 freemsg(mp0); 5266 freemsg(mp1); 5267 *mpp = NULL; 5268 return (NULL); 5269 } 5270 } 5271 } 5272 return (mp1); 5273 } 5274 /* Eat through as many mblks as we need to get len bytes. */ 5275 len -= mp0->b_wptr - mp0->b_rptr; 5276 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 5277 if (mp2->b_wptr - mp2->b_rptr > len) { 5278 /* 5279 * We won't consume the entire last mblk. Like 5280 * above, dup and partition it. 5281 */ 5282 mp1->b_cont = dupb(mp2); 5283 mp1 = mp1->b_cont; 5284 if (!mp1) { 5285 /* 5286 * Trouble. Rather than go to a lot of 5287 * trouble to clean up, we free the messages. 5288 * This won't be any worse than losing it on 5289 * the wire. 5290 */ 5291 freemsg(mp0); 5292 freemsg(mp2); 5293 *mpp = NULL; 5294 return (NULL); 5295 } 5296 mp1->b_wptr = mp1->b_rptr + len; 5297 mp2->b_rptr = mp1->b_wptr; 5298 /* 5299 * after adjustments if mblk not consumed is now 5300 * unaligned, try to align it. If this fails free 5301 * all messages and let upper layer recover. 5302 */ 5303 if (!OK_32PTR(mp2->b_rptr)) { 5304 if (!pullupmsg(mp2, -1)) { 5305 freemsg(mp0); 5306 freemsg(mp2); 5307 *mpp = NULL; 5308 return (NULL); 5309 } 5310 } 5311 *mpp = mp2; 5312 return (mp0); 5313 } 5314 /* Decrement len by the amount we just got. */ 5315 len -= mp2->b_wptr - mp2->b_rptr; 5316 } 5317 /* 5318 * len should be reduced to zero now. If not our caller has 5319 * screwed up. 5320 */ 5321 if (len) { 5322 /* Shouldn't happen! */ 5323 freemsg(mp0); 5324 *mpp = NULL; 5325 return (NULL); 5326 } 5327 /* 5328 * We consumed up to exactly the end of an mblk. Detach the part 5329 * we are returning from the rest of the chain. 5330 */ 5331 mp1->b_cont = NULL; 5332 *mpp = mp2; 5333 return (mp0); 5334 } 5335 5336 /* The ill stream is being unplumbed. Called from ip_close */ 5337 int 5338 ip_modclose(ill_t *ill) 5339 { 5340 boolean_t success; 5341 ipsq_t *ipsq; 5342 ipif_t *ipif; 5343 queue_t *q = ill->ill_rq; 5344 ip_stack_t *ipst = ill->ill_ipst; 5345 clock_t timeout; 5346 5347 /* 5348 * Wait for the ACKs of all deferred control messages to be processed. 5349 * In particular, we wait for a potential capability reset initiated 5350 * in ip_sioctl_plink() to complete before proceeding. 5351 * 5352 * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms) 5353 * in case the driver never replies. 5354 */ 5355 timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms); 5356 mutex_enter(&ill->ill_lock); 5357 while (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 5358 if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) { 5359 /* Timeout */ 5360 break; 5361 } 5362 } 5363 mutex_exit(&ill->ill_lock); 5364 5365 /* 5366 * Forcibly enter the ipsq after some delay. This is to take 5367 * care of the case when some ioctl does not complete because 5368 * we sent a control message to the driver and it did not 5369 * send us a reply. We want to be able to at least unplumb 5370 * and replumb rather than force the user to reboot the system. 5371 */ 5372 success = ipsq_enter(ill, B_FALSE); 5373 5374 /* 5375 * Open/close/push/pop is guaranteed to be single threaded 5376 * per stream by STREAMS. FS guarantees that all references 5377 * from top are gone before close is called. So there can't 5378 * be another close thread that has set CONDEMNED on this ill. 5379 * and cause ipsq_enter to return failure. 5380 */ 5381 ASSERT(success); 5382 ipsq = ill->ill_phyint->phyint_ipsq; 5383 5384 /* 5385 * Mark it condemned. No new reference will be made to this ill. 5386 * Lookup functions will return an error. Threads that try to 5387 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 5388 * that the refcnt will drop down to zero. 5389 */ 5390 mutex_enter(&ill->ill_lock); 5391 ill->ill_state_flags |= ILL_CONDEMNED; 5392 for (ipif = ill->ill_ipif; ipif != NULL; 5393 ipif = ipif->ipif_next) { 5394 ipif->ipif_state_flags |= IPIF_CONDEMNED; 5395 } 5396 /* 5397 * Wake up anybody waiting to enter the ipsq. ipsq_enter 5398 * returns error if ILL_CONDEMNED is set 5399 */ 5400 cv_broadcast(&ill->ill_cv); 5401 mutex_exit(&ill->ill_lock); 5402 5403 /* 5404 * Send all the deferred DLPI messages downstream which came in 5405 * during the small window right before ipsq_enter(). We do this 5406 * without waiting for the ACKs because all the ACKs for M_PROTO 5407 * messages are ignored in ip_rput() when ILL_CONDEMNED is set. 5408 */ 5409 ill_dlpi_send_deferred(ill); 5410 5411 /* 5412 * Shut down fragmentation reassembly. 5413 * ill_frag_timer won't start a timer again. 5414 * Now cancel any existing timer 5415 */ 5416 (void) untimeout(ill->ill_frag_timer_id); 5417 (void) ill_frag_timeout(ill, 0); 5418 5419 /* 5420 * If MOVE was in progress, clear the 5421 * move_in_progress fields also. 5422 */ 5423 if (ill->ill_move_in_progress) { 5424 ILL_CLEAR_MOVE(ill); 5425 } 5426 5427 /* 5428 * Call ill_delete to bring down the ipifs, ilms and ill on 5429 * this ill. Then wait for the refcnts to drop to zero. 5430 * ill_is_quiescent checks whether the ill is really quiescent. 5431 * Then make sure that threads that are waiting to enter the 5432 * ipsq have seen the error returned by ipsq_enter and have 5433 * gone away. Then we call ill_delete_tail which does the 5434 * DL_UNBIND_REQ with the driver and then qprocsoff. 5435 */ 5436 ill_delete(ill); 5437 mutex_enter(&ill->ill_lock); 5438 while (!ill_is_quiescent(ill)) 5439 cv_wait(&ill->ill_cv, &ill->ill_lock); 5440 while (ill->ill_waiters) 5441 cv_wait(&ill->ill_cv, &ill->ill_lock); 5442 5443 mutex_exit(&ill->ill_lock); 5444 5445 /* 5446 * ill_delete_tail drops reference on ill_ipst, but we need to keep 5447 * it held until the end of the function since the cleanup 5448 * below needs to be able to use the ip_stack_t. 5449 */ 5450 netstack_hold(ipst->ips_netstack); 5451 5452 /* qprocsoff is called in ill_delete_tail */ 5453 ill_delete_tail(ill); 5454 ASSERT(ill->ill_ipst == NULL); 5455 5456 /* 5457 * Walk through all upper (conn) streams and qenable 5458 * those that have queued data. 5459 * close synchronization needs this to 5460 * be done to ensure that all upper layers blocked 5461 * due to flow control to the closing device 5462 * get unblocked. 5463 */ 5464 ip1dbg(("ip_wsrv: walking\n")); 5465 conn_walk_drain(ipst); 5466 5467 mutex_enter(&ipst->ips_ip_mi_lock); 5468 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill); 5469 mutex_exit(&ipst->ips_ip_mi_lock); 5470 5471 /* 5472 * credp could be null if the open didn't succeed and ip_modopen 5473 * itself calls ip_close. 5474 */ 5475 if (ill->ill_credp != NULL) 5476 crfree(ill->ill_credp); 5477 5478 mutex_enter(&ill->ill_lock); 5479 ill_nic_info_dispatch(ill); 5480 mutex_exit(&ill->ill_lock); 5481 5482 /* 5483 * Now we are done with the module close pieces that 5484 * need the netstack_t. 5485 */ 5486 netstack_rele(ipst->ips_netstack); 5487 5488 mi_close_free((IDP)ill); 5489 q->q_ptr = WR(q)->q_ptr = NULL; 5490 5491 ipsq_exit(ipsq, B_TRUE, B_TRUE); 5492 5493 return (0); 5494 } 5495 5496 /* 5497 * This is called as part of close() for both IP and UDP 5498 * in order to quiesce the conn. 5499 */ 5500 void 5501 ip_quiesce_conn(conn_t *connp) 5502 { 5503 boolean_t drain_cleanup_reqd = B_FALSE; 5504 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 5505 boolean_t ilg_cleanup_reqd = B_FALSE; 5506 ip_stack_t *ipst; 5507 5508 ASSERT(!IPCL_IS_TCP(connp)); 5509 ipst = connp->conn_netstack->netstack_ip; 5510 5511 /* 5512 * Mark the conn as closing, and this conn must not be 5513 * inserted in future into any list. Eg. conn_drain_insert(), 5514 * won't insert this conn into the conn_drain_list. 5515 * Similarly ill_pending_mp_add() will not add any mp to 5516 * the pending mp list, after this conn has started closing. 5517 * 5518 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 5519 * cannot get set henceforth. 5520 */ 5521 mutex_enter(&connp->conn_lock); 5522 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 5523 connp->conn_state_flags |= CONN_CLOSING; 5524 if (connp->conn_idl != NULL) 5525 drain_cleanup_reqd = B_TRUE; 5526 if (connp->conn_oper_pending_ill != NULL) 5527 conn_ioctl_cleanup_reqd = B_TRUE; 5528 if (connp->conn_ilg_inuse != 0) 5529 ilg_cleanup_reqd = B_TRUE; 5530 mutex_exit(&connp->conn_lock); 5531 5532 if (IPCL_IS_UDP(connp)) 5533 udp_quiesce_conn(connp); 5534 5535 if (conn_ioctl_cleanup_reqd) 5536 conn_ioctl_cleanup(connp); 5537 5538 if (is_system_labeled() && connp->conn_anon_port) { 5539 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 5540 connp->conn_mlp_type, connp->conn_ulp, 5541 ntohs(connp->conn_lport), B_FALSE); 5542 connp->conn_anon_port = 0; 5543 } 5544 connp->conn_mlp_type = mlptSingle; 5545 5546 /* 5547 * Remove this conn from any fanout list it is on. 5548 * and then wait for any threads currently operating 5549 * on this endpoint to finish 5550 */ 5551 ipcl_hash_remove(connp); 5552 5553 /* 5554 * Remove this conn from the drain list, and do 5555 * any other cleanup that may be required. 5556 * (Only non-tcp streams may have a non-null conn_idl. 5557 * TCP streams are never flow controlled, and 5558 * conn_idl will be null) 5559 */ 5560 if (drain_cleanup_reqd) 5561 conn_drain_tail(connp, B_TRUE); 5562 5563 if (connp->conn_rq == ipst->ips_ip_g_mrouter || 5564 connp->conn_wq == ipst->ips_ip_g_mrouter) 5565 (void) ip_mrouter_done(NULL, ipst); 5566 5567 if (ilg_cleanup_reqd) 5568 ilg_delete_all(connp); 5569 5570 conn_delete_ire(connp, NULL); 5571 5572 /* 5573 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 5574 * callers from write side can't be there now because close 5575 * is in progress. The only other caller is ipcl_walk 5576 * which checks for the condemned flag. 5577 */ 5578 mutex_enter(&connp->conn_lock); 5579 connp->conn_state_flags |= CONN_CONDEMNED; 5580 while (connp->conn_ref != 1) 5581 cv_wait(&connp->conn_cv, &connp->conn_lock); 5582 connp->conn_state_flags |= CONN_QUIESCED; 5583 mutex_exit(&connp->conn_lock); 5584 } 5585 5586 /* ARGSUSED */ 5587 int 5588 ip_close(queue_t *q, int flags) 5589 { 5590 conn_t *connp; 5591 5592 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 5593 5594 /* 5595 * Call the appropriate delete routine depending on whether this is 5596 * a module or device. 5597 */ 5598 if (WR(q)->q_next != NULL) { 5599 /* This is a module close */ 5600 return (ip_modclose((ill_t *)q->q_ptr)); 5601 } 5602 5603 connp = q->q_ptr; 5604 ip_quiesce_conn(connp); 5605 5606 qprocsoff(q); 5607 5608 /* 5609 * Now we are truly single threaded on this stream, and can 5610 * delete the things hanging off the connp, and finally the connp. 5611 * We removed this connp from the fanout list, it cannot be 5612 * accessed thru the fanouts, and we already waited for the 5613 * conn_ref to drop to 0. We are already in close, so 5614 * there cannot be any other thread from the top. qprocsoff 5615 * has completed, and service has completed or won't run in 5616 * future. 5617 */ 5618 ASSERT(connp->conn_ref == 1); 5619 5620 /* 5621 * A conn which was previously marked as IPCL_UDP cannot 5622 * retain the flag because it would have been cleared by 5623 * udp_close(). 5624 */ 5625 ASSERT(!IPCL_IS_UDP(connp)); 5626 5627 if (connp->conn_latch != NULL) { 5628 IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack); 5629 connp->conn_latch = NULL; 5630 } 5631 if (connp->conn_policy != NULL) { 5632 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 5633 connp->conn_policy = NULL; 5634 } 5635 if (connp->conn_ipsec_opt_mp != NULL) { 5636 freemsg(connp->conn_ipsec_opt_mp); 5637 connp->conn_ipsec_opt_mp = NULL; 5638 } 5639 5640 inet_minor_free(ip_minor_arena, connp->conn_dev); 5641 5642 connp->conn_ref--; 5643 ipcl_conn_destroy(connp); 5644 5645 q->q_ptr = WR(q)->q_ptr = NULL; 5646 return (0); 5647 } 5648 5649 int 5650 ip_snmpmod_close(queue_t *q) 5651 { 5652 conn_t *connp = Q_TO_CONN(q); 5653 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5654 5655 qprocsoff(q); 5656 5657 if (connp->conn_flags & IPCL_UDPMOD) 5658 udp_close_free(connp); 5659 5660 if (connp->conn_cred != NULL) { 5661 crfree(connp->conn_cred); 5662 connp->conn_cred = NULL; 5663 } 5664 CONN_DEC_REF(connp); 5665 q->q_ptr = WR(q)->q_ptr = NULL; 5666 return (0); 5667 } 5668 5669 /* 5670 * Write side put procedure for TCP module or UDP module instance. TCP/UDP 5671 * as a module is only used for MIB browsers that push TCP/UDP over IP or ARP. 5672 * The only supported primitives are T_SVR4_OPTMGMT_REQ and T_OPTMGMT_REQ. 5673 * M_FLUSH messages and ioctls are only passed downstream; we don't flush our 5674 * queues as we never enqueue messages there and we don't handle any ioctls. 5675 * Everything else is freed. 5676 */ 5677 void 5678 ip_snmpmod_wput(queue_t *q, mblk_t *mp) 5679 { 5680 conn_t *connp = q->q_ptr; 5681 pfi_t setfn; 5682 pfi_t getfn; 5683 5684 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5685 5686 switch (DB_TYPE(mp)) { 5687 case M_PROTO: 5688 case M_PCPROTO: 5689 if ((MBLKL(mp) >= sizeof (t_scalar_t)) && 5690 ((((union T_primitives *)mp->b_rptr)->type == 5691 T_SVR4_OPTMGMT_REQ) || 5692 (((union T_primitives *)mp->b_rptr)->type == 5693 T_OPTMGMT_REQ))) { 5694 /* 5695 * This is the only TPI primitive supported. Its 5696 * handling does not require tcp_t, but it does require 5697 * conn_t to check permissions. 5698 */ 5699 cred_t *cr = DB_CREDDEF(mp, connp->conn_cred); 5700 5701 if (connp->conn_flags & IPCL_TCPMOD) { 5702 setfn = tcp_snmp_set; 5703 getfn = tcp_snmp_get; 5704 } else { 5705 setfn = udp_snmp_set; 5706 getfn = udp_snmp_get; 5707 } 5708 if (!snmpcom_req(q, mp, setfn, getfn, cr)) { 5709 freemsg(mp); 5710 return; 5711 } 5712 } else if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, ENOTSUP)) 5713 != NULL) 5714 qreply(q, mp); 5715 break; 5716 case M_FLUSH: 5717 case M_IOCTL: 5718 putnext(q, mp); 5719 break; 5720 default: 5721 freemsg(mp); 5722 break; 5723 } 5724 } 5725 5726 /* Return the IP checksum for the IP header at "iph". */ 5727 uint16_t 5728 ip_csum_hdr(ipha_t *ipha) 5729 { 5730 uint16_t *uph; 5731 uint32_t sum; 5732 int opt_len; 5733 5734 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - 5735 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 5736 uph = (uint16_t *)ipha; 5737 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 5738 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 5739 if (opt_len > 0) { 5740 do { 5741 sum += uph[10]; 5742 sum += uph[11]; 5743 uph += 2; 5744 } while (--opt_len); 5745 } 5746 sum = (sum & 0xFFFF) + (sum >> 16); 5747 sum = ~(sum + (sum >> 16)) & 0xFFFF; 5748 if (sum == 0xffff) 5749 sum = 0; 5750 return ((uint16_t)sum); 5751 } 5752 5753 /* 5754 * Called when the module is about to be unloaded 5755 */ 5756 void 5757 ip_ddi_destroy(void) 5758 { 5759 tnet_fini(); 5760 5761 sctp_ddi_g_destroy(); 5762 tcp_ddi_g_destroy(); 5763 ipsec_policy_g_destroy(); 5764 ipcl_g_destroy(); 5765 ip_net_g_destroy(); 5766 ip_ire_g_fini(); 5767 inet_minor_destroy(ip_minor_arena); 5768 5769 #ifdef DEBUG 5770 list_destroy(&ip_thread_list); 5771 rw_destroy(&ip_thread_rwlock); 5772 tsd_destroy(&ip_thread_data); 5773 #endif 5774 5775 netstack_unregister(NS_IP); 5776 } 5777 5778 /* 5779 * First step in cleanup. 5780 */ 5781 /* ARGSUSED */ 5782 static void 5783 ip_stack_shutdown(netstackid_t stackid, void *arg) 5784 { 5785 ip_stack_t *ipst = (ip_stack_t *)arg; 5786 5787 #ifdef NS_DEBUG 5788 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid); 5789 #endif 5790 5791 /* Get rid of loopback interfaces and their IREs */ 5792 ip_loopback_cleanup(ipst); 5793 } 5794 5795 /* 5796 * Free the IP stack instance. 5797 */ 5798 static void 5799 ip_stack_fini(netstackid_t stackid, void *arg) 5800 { 5801 ip_stack_t *ipst = (ip_stack_t *)arg; 5802 int ret; 5803 5804 #ifdef NS_DEBUG 5805 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid); 5806 #endif 5807 ipv4_hook_destroy(ipst); 5808 ipv6_hook_destroy(ipst); 5809 ip_net_destroy(ipst); 5810 5811 rw_destroy(&ipst->ips_srcid_lock); 5812 5813 ip_kstat_fini(stackid, ipst->ips_ip_mibkp); 5814 ipst->ips_ip_mibkp = NULL; 5815 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp); 5816 ipst->ips_icmp_mibkp = NULL; 5817 ip_kstat2_fini(stackid, ipst->ips_ip_kstat); 5818 ipst->ips_ip_kstat = NULL; 5819 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics)); 5820 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat); 5821 ipst->ips_ip6_kstat = NULL; 5822 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics)); 5823 5824 nd_free(&ipst->ips_ip_g_nd); 5825 kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr)); 5826 ipst->ips_param_arr = NULL; 5827 kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 5828 ipst->ips_ndp_arr = NULL; 5829 5830 ip_mrouter_stack_destroy(ipst); 5831 5832 mutex_destroy(&ipst->ips_ip_mi_lock); 5833 rw_destroy(&ipst->ips_ipsec_capab_ills_lock); 5834 rw_destroy(&ipst->ips_ill_g_usesrc_lock); 5835 rw_destroy(&ipst->ips_ip_g_nd_lock); 5836 5837 ret = untimeout(ipst->ips_igmp_timeout_id); 5838 if (ret == -1) { 5839 ASSERT(ipst->ips_igmp_timeout_id == 0); 5840 } else { 5841 ASSERT(ipst->ips_igmp_timeout_id != 0); 5842 ipst->ips_igmp_timeout_id = 0; 5843 } 5844 ret = untimeout(ipst->ips_igmp_slowtimeout_id); 5845 if (ret == -1) { 5846 ASSERT(ipst->ips_igmp_slowtimeout_id == 0); 5847 } else { 5848 ASSERT(ipst->ips_igmp_slowtimeout_id != 0); 5849 ipst->ips_igmp_slowtimeout_id = 0; 5850 } 5851 ret = untimeout(ipst->ips_mld_timeout_id); 5852 if (ret == -1) { 5853 ASSERT(ipst->ips_mld_timeout_id == 0); 5854 } else { 5855 ASSERT(ipst->ips_mld_timeout_id != 0); 5856 ipst->ips_mld_timeout_id = 0; 5857 } 5858 ret = untimeout(ipst->ips_mld_slowtimeout_id); 5859 if (ret == -1) { 5860 ASSERT(ipst->ips_mld_slowtimeout_id == 0); 5861 } else { 5862 ASSERT(ipst->ips_mld_slowtimeout_id != 0); 5863 ipst->ips_mld_slowtimeout_id = 0; 5864 } 5865 ret = untimeout(ipst->ips_ip_ire_expire_id); 5866 if (ret == -1) { 5867 ASSERT(ipst->ips_ip_ire_expire_id == 0); 5868 } else { 5869 ASSERT(ipst->ips_ip_ire_expire_id != 0); 5870 ipst->ips_ip_ire_expire_id = 0; 5871 } 5872 5873 mutex_destroy(&ipst->ips_igmp_timer_lock); 5874 mutex_destroy(&ipst->ips_mld_timer_lock); 5875 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock); 5876 mutex_destroy(&ipst->ips_mld_slowtimeout_lock); 5877 mutex_destroy(&ipst->ips_ip_addr_avail_lock); 5878 rw_destroy(&ipst->ips_ill_g_lock); 5879 5880 ip_ire_fini(ipst); 5881 ip6_asp_free(ipst); 5882 conn_drain_fini(ipst); 5883 ipcl_destroy(ipst); 5884 5885 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock); 5886 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock); 5887 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t)); 5888 ipst->ips_ndp4 = NULL; 5889 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t)); 5890 ipst->ips_ndp6 = NULL; 5891 5892 if (ipst->ips_loopback_ksp != NULL) { 5893 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid); 5894 ipst->ips_loopback_ksp = NULL; 5895 } 5896 5897 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t)); 5898 ipst->ips_phyint_g_list = NULL; 5899 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS); 5900 ipst->ips_ill_g_heads = NULL; 5901 5902 kmem_free(ipst, sizeof (*ipst)); 5903 } 5904 5905 /* 5906 * This function is called from the TSD destructor, and is used to debug 5907 * reference count issues in IP. See block comment in <inet/ip_if.h> for 5908 * details. 5909 */ 5910 static void 5911 ip_thread_exit(void *phash) 5912 { 5913 th_hash_t *thh = phash; 5914 5915 rw_enter(&ip_thread_rwlock, RW_WRITER); 5916 list_remove(&ip_thread_list, thh); 5917 rw_exit(&ip_thread_rwlock); 5918 mod_hash_destroy_hash(thh->thh_hash); 5919 kmem_free(thh, sizeof (*thh)); 5920 } 5921 5922 /* 5923 * Called when the IP kernel module is loaded into the kernel 5924 */ 5925 void 5926 ip_ddi_init(void) 5927 { 5928 TCP6_MAJ = ddi_name_to_major(TCP6); 5929 TCP_MAJ = ddi_name_to_major(TCP); 5930 SCTP_MAJ = ddi_name_to_major(SCTP); 5931 SCTP6_MAJ = ddi_name_to_major(SCTP6); 5932 5933 ip_input_proc = ip_squeue_switch(ip_squeue_enter); 5934 5935 /* 5936 * For IP and TCP the minor numbers should start from 2 since we have 4 5937 * initial devices: ip, ip6, tcp, tcp6. 5938 */ 5939 if ((ip_minor_arena = inet_minor_create("ip_minor_arena", 5940 INET_MIN_DEV + 2, KM_SLEEP)) == NULL) { 5941 cmn_err(CE_PANIC, 5942 "ip_ddi_init: ip_minor_arena creation failed\n"); 5943 } 5944 5945 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5946 5947 ipcl_g_init(); 5948 ip_ire_g_init(); 5949 ip_net_g_init(); 5950 5951 #ifdef DEBUG 5952 tsd_create(&ip_thread_data, ip_thread_exit); 5953 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL); 5954 list_create(&ip_thread_list, sizeof (th_hash_t), 5955 offsetof(th_hash_t, thh_link)); 5956 #endif 5957 5958 /* 5959 * We want to be informed each time a stack is created or 5960 * destroyed in the kernel, so we can maintain the 5961 * set of udp_stack_t's. 5962 */ 5963 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown, 5964 ip_stack_fini); 5965 5966 ipsec_policy_g_init(); 5967 tcp_ddi_g_init(); 5968 sctp_ddi_g_init(); 5969 5970 tnet_init(); 5971 } 5972 5973 /* 5974 * Initialize the IP stack instance. 5975 */ 5976 static void * 5977 ip_stack_init(netstackid_t stackid, netstack_t *ns) 5978 { 5979 ip_stack_t *ipst; 5980 ipparam_t *pa; 5981 ipndp_t *na; 5982 5983 #ifdef NS_DEBUG 5984 printf("ip_stack_init(stack %d)\n", stackid); 5985 #endif 5986 5987 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP); 5988 ipst->ips_netstack = ns; 5989 5990 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS, 5991 KM_SLEEP); 5992 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t), 5993 KM_SLEEP); 5994 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5995 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 5996 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 5997 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 5998 5999 rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 6000 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6001 ipst->ips_igmp_deferred_next = INFINITY; 6002 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6003 ipst->ips_mld_deferred_next = INFINITY; 6004 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6005 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6006 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 6007 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 6008 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL); 6009 rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 6010 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 6011 6012 ipcl_init(ipst); 6013 ip_ire_init(ipst); 6014 ip6_asp_init(ipst); 6015 ipif_init(ipst); 6016 conn_drain_init(ipst); 6017 ip_mrouter_stack_init(ipst); 6018 6019 ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT; 6020 ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 6021 6022 ipst->ips_ip_multirt_log_interval = 1000; 6023 6024 ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT; 6025 ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT; 6026 ipst->ips_ill_index = 1; 6027 6028 ipst->ips_saved_ip_g_forward = -1; 6029 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */ 6030 6031 pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 6032 ipst->ips_param_arr = pa; 6033 bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr)); 6034 6035 na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP); 6036 ipst->ips_ndp_arr = na; 6037 bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 6038 ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data = 6039 (caddr_t)&ipst->ips_ip_g_forward; 6040 ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data = 6041 (caddr_t)&ipst->ips_ipv6_forward; 6042 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name, 6043 "ip_cgtp_filter") == 0); 6044 ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data = 6045 (caddr_t)&ipst->ips_ip_cgtp_filter; 6046 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_name, 6047 "ipmp_hook_emulation") == 0); 6048 ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_data = 6049 (caddr_t)&ipst->ips_ipmp_hook_emulation; 6050 6051 (void) ip_param_register(&ipst->ips_ip_g_nd, 6052 ipst->ips_param_arr, A_CNT(lcl_param_arr), 6053 ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr)); 6054 6055 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst); 6056 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid); 6057 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics); 6058 ipst->ips_ip6_kstat = 6059 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics); 6060 6061 ipst->ips_ipmp_enable_failback = B_TRUE; 6062 6063 ipst->ips_ip_src_id = 1; 6064 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL); 6065 6066 ip_net_init(ipst, ns); 6067 ipv4_hook_init(ipst); 6068 ipv6_hook_init(ipst); 6069 6070 return (ipst); 6071 } 6072 6073 /* 6074 * Allocate and initialize a DLPI template of the specified length. (May be 6075 * called as writer.) 6076 */ 6077 mblk_t * 6078 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 6079 { 6080 mblk_t *mp; 6081 6082 mp = allocb(len, BPRI_MED); 6083 if (!mp) 6084 return (NULL); 6085 6086 /* 6087 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 6088 * of which we don't seem to use) are sent with M_PCPROTO, and 6089 * that other DLPI are M_PROTO. 6090 */ 6091 if (prim == DL_INFO_REQ) { 6092 mp->b_datap->db_type = M_PCPROTO; 6093 } else { 6094 mp->b_datap->db_type = M_PROTO; 6095 } 6096 6097 mp->b_wptr = mp->b_rptr + len; 6098 bzero(mp->b_rptr, len); 6099 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 6100 return (mp); 6101 } 6102 6103 const char * 6104 dlpi_prim_str(int prim) 6105 { 6106 switch (prim) { 6107 case DL_INFO_REQ: return ("DL_INFO_REQ"); 6108 case DL_INFO_ACK: return ("DL_INFO_ACK"); 6109 case DL_ATTACH_REQ: return ("DL_ATTACH_REQ"); 6110 case DL_DETACH_REQ: return ("DL_DETACH_REQ"); 6111 case DL_BIND_REQ: return ("DL_BIND_REQ"); 6112 case DL_BIND_ACK: return ("DL_BIND_ACK"); 6113 case DL_UNBIND_REQ: return ("DL_UNBIND_REQ"); 6114 case DL_OK_ACK: return ("DL_OK_ACK"); 6115 case DL_ERROR_ACK: return ("DL_ERROR_ACK"); 6116 case DL_ENABMULTI_REQ: return ("DL_ENABMULTI_REQ"); 6117 case DL_DISABMULTI_REQ: return ("DL_DISABMULTI_REQ"); 6118 case DL_PROMISCON_REQ: return ("DL_PROMISCON_REQ"); 6119 case DL_PROMISCOFF_REQ: return ("DL_PROMISCOFF_REQ"); 6120 case DL_UNITDATA_REQ: return ("DL_UNITDATA_REQ"); 6121 case DL_UNITDATA_IND: return ("DL_UNITDATA_IND"); 6122 case DL_UDERROR_IND: return ("DL_UDERROR_IND"); 6123 case DL_PHYS_ADDR_REQ: return ("DL_PHYS_ADDR_REQ"); 6124 case DL_PHYS_ADDR_ACK: return ("DL_PHYS_ADDR_ACK"); 6125 case DL_SET_PHYS_ADDR_REQ: return ("DL_SET_PHYS_ADDR_REQ"); 6126 case DL_NOTIFY_REQ: return ("DL_NOTIFY_REQ"); 6127 case DL_NOTIFY_ACK: return ("DL_NOTIFY_ACK"); 6128 case DL_NOTIFY_IND: return ("DL_NOTIFY_IND"); 6129 case DL_CAPABILITY_REQ: return ("DL_CAPABILITY_REQ"); 6130 case DL_CAPABILITY_ACK: return ("DL_CAPABILITY_ACK"); 6131 case DL_CONTROL_REQ: return ("DL_CONTROL_REQ"); 6132 case DL_CONTROL_ACK: return ("DL_CONTROL_ACK"); 6133 default: return ("<unknown primitive>"); 6134 } 6135 } 6136 6137 const char * 6138 dlpi_err_str(int err) 6139 { 6140 switch (err) { 6141 case DL_ACCESS: return ("DL_ACCESS"); 6142 case DL_BADADDR: return ("DL_BADADDR"); 6143 case DL_BADCORR: return ("DL_BADCORR"); 6144 case DL_BADDATA: return ("DL_BADDATA"); 6145 case DL_BADPPA: return ("DL_BADPPA"); 6146 case DL_BADPRIM: return ("DL_BADPRIM"); 6147 case DL_BADQOSPARAM: return ("DL_BADQOSPARAM"); 6148 case DL_BADQOSTYPE: return ("DL_BADQOSTYPE"); 6149 case DL_BADSAP: return ("DL_BADSAP"); 6150 case DL_BADTOKEN: return ("DL_BADTOKEN"); 6151 case DL_BOUND: return ("DL_BOUND"); 6152 case DL_INITFAILED: return ("DL_INITFAILED"); 6153 case DL_NOADDR: return ("DL_NOADDR"); 6154 case DL_NOTINIT: return ("DL_NOTINIT"); 6155 case DL_OUTSTATE: return ("DL_OUTSTATE"); 6156 case DL_SYSERR: return ("DL_SYSERR"); 6157 case DL_UNSUPPORTED: return ("DL_UNSUPPORTED"); 6158 case DL_UNDELIVERABLE: return ("DL_UNDELIVERABLE"); 6159 case DL_NOTSUPPORTED : return ("DL_NOTSUPPORTED "); 6160 case DL_TOOMANY: return ("DL_TOOMANY"); 6161 case DL_NOTENAB: return ("DL_NOTENAB"); 6162 case DL_BUSY: return ("DL_BUSY"); 6163 case DL_NOAUTO: return ("DL_NOAUTO"); 6164 case DL_NOXIDAUTO: return ("DL_NOXIDAUTO"); 6165 case DL_NOTESTAUTO: return ("DL_NOTESTAUTO"); 6166 case DL_XIDAUTO: return ("DL_XIDAUTO"); 6167 case DL_TESTAUTO: return ("DL_TESTAUTO"); 6168 case DL_PENDING: return ("DL_PENDING"); 6169 default: return ("<unknown error>"); 6170 } 6171 } 6172 6173 /* 6174 * Debug formatting routine. Returns a character string representation of the 6175 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 6176 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 6177 * 6178 * Once the ndd table-printing interfaces are removed, this can be changed to 6179 * standard dotted-decimal form. 6180 */ 6181 char * 6182 ip_dot_addr(ipaddr_t addr, char *buf) 6183 { 6184 uint8_t *ap = (uint8_t *)&addr; 6185 6186 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 6187 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 6188 return (buf); 6189 } 6190 6191 /* 6192 * Write the given MAC address as a printable string in the usual colon- 6193 * separated format. 6194 */ 6195 const char * 6196 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 6197 { 6198 char *bp; 6199 6200 if (alen == 0 || buflen < 4) 6201 return ("?"); 6202 bp = buf; 6203 for (;;) { 6204 /* 6205 * If there are more MAC address bytes available, but we won't 6206 * have any room to print them, then add "..." to the string 6207 * instead. See below for the 'magic number' explanation. 6208 */ 6209 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 6210 (void) strcpy(bp, "..."); 6211 break; 6212 } 6213 (void) sprintf(bp, "%02x", *addr++); 6214 bp += 2; 6215 if (--alen == 0) 6216 break; 6217 *bp++ = ':'; 6218 buflen -= 3; 6219 /* 6220 * At this point, based on the first 'if' statement above, 6221 * either alen == 1 and buflen >= 3, or alen > 1 and 6222 * buflen >= 4. The first case leaves room for the final "xx" 6223 * number and trailing NUL byte. The second leaves room for at 6224 * least "...". Thus the apparently 'magic' numbers chosen for 6225 * that statement. 6226 */ 6227 } 6228 return (buf); 6229 } 6230 6231 /* 6232 * Send an ICMP error after patching up the packet appropriately. Returns 6233 * non-zero if the appropriate MIB should be bumped; zero otherwise. 6234 */ 6235 static boolean_t 6236 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 6237 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, 6238 zoneid_t zoneid, ip_stack_t *ipst) 6239 { 6240 ipha_t *ipha; 6241 mblk_t *first_mp; 6242 boolean_t secure; 6243 unsigned char db_type; 6244 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6245 6246 first_mp = mp; 6247 if (mctl_present) { 6248 mp = mp->b_cont; 6249 secure = ipsec_in_is_secure(first_mp); 6250 ASSERT(mp != NULL); 6251 } else { 6252 /* 6253 * If this is an ICMP error being reported - which goes 6254 * up as M_CTLs, we need to convert them to M_DATA till 6255 * we finish checking with global policy because 6256 * ipsec_check_global_policy() assumes M_DATA as clear 6257 * and M_CTL as secure. 6258 */ 6259 db_type = DB_TYPE(mp); 6260 DB_TYPE(mp) = M_DATA; 6261 secure = B_FALSE; 6262 } 6263 /* 6264 * We are generating an icmp error for some inbound packet. 6265 * Called from all ip_fanout_(udp, tcp, proto) functions. 6266 * Before we generate an error, check with global policy 6267 * to see whether this is allowed to enter the system. As 6268 * there is no "conn", we are checking with global policy. 6269 */ 6270 ipha = (ipha_t *)mp->b_rptr; 6271 if (secure || ipss->ipsec_inbound_v4_policy_present) { 6272 first_mp = ipsec_check_global_policy(first_mp, NULL, 6273 ipha, NULL, mctl_present, ipst->ips_netstack); 6274 if (first_mp == NULL) 6275 return (B_FALSE); 6276 } 6277 6278 if (!mctl_present) 6279 DB_TYPE(mp) = db_type; 6280 6281 if (flags & IP_FF_SEND_ICMP) { 6282 if (flags & IP_FF_HDR_COMPLETE) { 6283 if (ip_hdr_complete(ipha, zoneid, ipst)) { 6284 freemsg(first_mp); 6285 return (B_TRUE); 6286 } 6287 } 6288 if (flags & IP_FF_CKSUM) { 6289 /* 6290 * Have to correct checksum since 6291 * the packet might have been 6292 * fragmented and the reassembly code in ip_rput 6293 * does not restore the IP checksum. 6294 */ 6295 ipha->ipha_hdr_checksum = 0; 6296 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6297 } 6298 switch (icmp_type) { 6299 case ICMP_DEST_UNREACHABLE: 6300 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid, 6301 ipst); 6302 break; 6303 default: 6304 freemsg(first_mp); 6305 break; 6306 } 6307 } else { 6308 freemsg(first_mp); 6309 return (B_FALSE); 6310 } 6311 6312 return (B_TRUE); 6313 } 6314 6315 /* 6316 * Used to send an ICMP error message when a packet is received for 6317 * a protocol that is not supported. The mblk passed as argument 6318 * is consumed by this function. 6319 */ 6320 void 6321 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid, 6322 ip_stack_t *ipst) 6323 { 6324 mblk_t *mp; 6325 ipha_t *ipha; 6326 ill_t *ill; 6327 ipsec_in_t *ii; 6328 6329 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6330 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6331 6332 mp = ipsec_mp->b_cont; 6333 ipsec_mp->b_cont = NULL; 6334 ipha = (ipha_t *)mp->b_rptr; 6335 /* Get ill from index in ipsec_in_t. */ 6336 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 6337 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL, 6338 ipst); 6339 if (ill != NULL) { 6340 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 6341 if (ip_fanout_send_icmp(q, mp, flags, 6342 ICMP_DEST_UNREACHABLE, 6343 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) { 6344 BUMP_MIB(ill->ill_ip_mib, 6345 ipIfStatsInUnknownProtos); 6346 } 6347 } else { 6348 if (ip_fanout_send_icmp_v6(q, mp, flags, 6349 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 6350 0, B_FALSE, zoneid, ipst)) { 6351 BUMP_MIB(ill->ill_ip_mib, 6352 ipIfStatsInUnknownProtos); 6353 } 6354 } 6355 ill_refrele(ill); 6356 } else { /* re-link for the freemsg() below. */ 6357 ipsec_mp->b_cont = mp; 6358 } 6359 6360 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 6361 freemsg(ipsec_mp); 6362 } 6363 6364 /* 6365 * See if the inbound datagram has had IPsec processing applied to it. 6366 */ 6367 boolean_t 6368 ipsec_in_is_secure(mblk_t *ipsec_mp) 6369 { 6370 ipsec_in_t *ii; 6371 6372 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6373 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6374 6375 if (ii->ipsec_in_loopback) { 6376 return (ii->ipsec_in_secure); 6377 } else { 6378 return (ii->ipsec_in_ah_sa != NULL || 6379 ii->ipsec_in_esp_sa != NULL || 6380 ii->ipsec_in_decaps); 6381 } 6382 } 6383 6384 /* 6385 * Handle protocols with which IP is less intimate. There 6386 * can be more than one stream bound to a particular 6387 * protocol. When this is the case, normally each one gets a copy 6388 * of any incoming packets. 6389 * 6390 * IPsec NOTE : 6391 * 6392 * Don't allow a secure packet going up a non-secure connection. 6393 * We don't allow this because 6394 * 6395 * 1) Reply might go out in clear which will be dropped at 6396 * the sending side. 6397 * 2) If the reply goes out in clear it will give the 6398 * adversary enough information for getting the key in 6399 * most of the cases. 6400 * 6401 * Moreover getting a secure packet when we expect clear 6402 * implies that SA's were added without checking for 6403 * policy on both ends. This should not happen once ISAKMP 6404 * is used to negotiate SAs as SAs will be added only after 6405 * verifying the policy. 6406 * 6407 * NOTE : If the packet was tunneled and not multicast we only send 6408 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 6409 * back to delivering packets to AF_INET6 raw sockets. 6410 * 6411 * IPQoS Notes: 6412 * Once we have determined the client, invoke IPPF processing. 6413 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6414 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6415 * ip_policy will be false. 6416 * 6417 * Zones notes: 6418 * Currently only applications in the global zone can create raw sockets for 6419 * protocols other than ICMP. So unlike the broadcast / multicast case of 6420 * ip_fanout_udp(), we only send a copy of the packet to streams in the 6421 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 6422 */ 6423 static void 6424 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 6425 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 6426 zoneid_t zoneid) 6427 { 6428 queue_t *rq; 6429 mblk_t *mp1, *first_mp1; 6430 uint_t protocol = ipha->ipha_protocol; 6431 ipaddr_t dst; 6432 boolean_t one_only; 6433 mblk_t *first_mp = mp; 6434 boolean_t secure; 6435 uint32_t ill_index; 6436 conn_t *connp, *first_connp, *next_connp; 6437 connf_t *connfp; 6438 boolean_t shared_addr; 6439 mib2_ipIfStatsEntry_t *mibptr; 6440 ip_stack_t *ipst = recv_ill->ill_ipst; 6441 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6442 6443 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 6444 if (mctl_present) { 6445 mp = first_mp->b_cont; 6446 secure = ipsec_in_is_secure(first_mp); 6447 ASSERT(mp != NULL); 6448 } else { 6449 secure = B_FALSE; 6450 } 6451 dst = ipha->ipha_dst; 6452 /* 6453 * If the packet was tunneled and not multicast we only send to it 6454 * the first match. 6455 */ 6456 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 6457 !CLASSD(dst)); 6458 6459 shared_addr = (zoneid == ALL_ZONES); 6460 if (shared_addr) { 6461 /* 6462 * We don't allow multilevel ports for raw IP, so no need to 6463 * check for that here. 6464 */ 6465 zoneid = tsol_packet_to_zoneid(mp); 6466 } 6467 6468 connfp = &ipst->ips_ipcl_proto_fanout[protocol]; 6469 mutex_enter(&connfp->connf_lock); 6470 connp = connfp->connf_head; 6471 for (connp = connfp->connf_head; connp != NULL; 6472 connp = connp->conn_next) { 6473 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, 6474 zoneid) && 6475 (!is_system_labeled() || 6476 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6477 connp))) { 6478 break; 6479 } 6480 } 6481 6482 if (connp == NULL || connp->conn_upq == NULL) { 6483 /* 6484 * No one bound to these addresses. Is 6485 * there a client that wants all 6486 * unclaimed datagrams? 6487 */ 6488 mutex_exit(&connfp->connf_lock); 6489 /* 6490 * Check for IPPROTO_ENCAP... 6491 */ 6492 if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) { 6493 /* 6494 * If an IPsec mblk is here on a multicast 6495 * tunnel (using ip_mroute stuff), check policy here, 6496 * THEN ship off to ip_mroute_decap(). 6497 * 6498 * BTW, If I match a configured IP-in-IP 6499 * tunnel, this path will not be reached, and 6500 * ip_mroute_decap will never be called. 6501 */ 6502 first_mp = ipsec_check_global_policy(first_mp, connp, 6503 ipha, NULL, mctl_present, ipst->ips_netstack); 6504 if (first_mp != NULL) { 6505 if (mctl_present) 6506 freeb(first_mp); 6507 ip_mroute_decap(q, mp, ill); 6508 } /* Else we already freed everything! */ 6509 } else { 6510 /* 6511 * Otherwise send an ICMP protocol unreachable. 6512 */ 6513 if (ip_fanout_send_icmp(q, first_mp, flags, 6514 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 6515 mctl_present, zoneid, ipst)) { 6516 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos); 6517 } 6518 } 6519 return; 6520 } 6521 CONN_INC_REF(connp); 6522 first_connp = connp; 6523 6524 /* 6525 * Only send message to one tunnel driver by immediately 6526 * terminating the loop. 6527 */ 6528 connp = one_only ? NULL : connp->conn_next; 6529 6530 for (;;) { 6531 while (connp != NULL) { 6532 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 6533 flags, zoneid) && 6534 (!is_system_labeled() || 6535 tsol_receive_local(mp, &dst, IPV4_VERSION, 6536 shared_addr, connp))) 6537 break; 6538 connp = connp->conn_next; 6539 } 6540 6541 /* 6542 * Copy the packet. 6543 */ 6544 if (connp == NULL || connp->conn_upq == NULL || 6545 (((first_mp1 = dupmsg(first_mp)) == NULL) && 6546 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 6547 /* 6548 * No more interested clients or memory 6549 * allocation failed 6550 */ 6551 connp = first_connp; 6552 break; 6553 } 6554 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 6555 CONN_INC_REF(connp); 6556 mutex_exit(&connfp->connf_lock); 6557 rq = connp->conn_rq; 6558 if (!canputnext(rq)) { 6559 if (flags & IP_FF_RAWIP) { 6560 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6561 } else { 6562 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6563 } 6564 6565 freemsg(first_mp1); 6566 } else { 6567 /* 6568 * Don't enforce here if we're an actual tunnel - 6569 * let "tun" do it instead. 6570 */ 6571 if (!IPCL_IS_IPTUN(connp) && 6572 (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || 6573 secure)) { 6574 first_mp1 = ipsec_check_inbound_policy 6575 (first_mp1, connp, ipha, NULL, 6576 mctl_present); 6577 } 6578 if (first_mp1 != NULL) { 6579 int in_flags = 0; 6580 /* 6581 * ip_fanout_proto also gets called from 6582 * icmp_inbound_error_fanout, in which case 6583 * the msg type is M_CTL. Don't add info 6584 * in this case for the time being. In future 6585 * when there is a need for knowing the 6586 * inbound iface index for ICMP error msgs, 6587 * then this can be changed. 6588 */ 6589 if (connp->conn_recvif) 6590 in_flags = IPF_RECVIF; 6591 /* 6592 * The ULP may support IP_RECVPKTINFO for both 6593 * IP v4 and v6 so pass the appropriate argument 6594 * based on conn IP version. 6595 */ 6596 if (connp->conn_ip_recvpktinfo) { 6597 if (connp->conn_af_isv6) { 6598 /* 6599 * V6 only needs index 6600 */ 6601 in_flags |= IPF_RECVIF; 6602 } else { 6603 /* 6604 * V4 needs index + 6605 * matching address. 6606 */ 6607 in_flags |= IPF_RECVADDR; 6608 } 6609 } 6610 if ((in_flags != 0) && 6611 (mp->b_datap->db_type != M_CTL)) { 6612 /* 6613 * the actual data will be 6614 * contained in b_cont upon 6615 * successful return of the 6616 * following call else 6617 * original mblk is returned 6618 */ 6619 ASSERT(recv_ill != NULL); 6620 mp1 = ip_add_info(mp1, recv_ill, 6621 in_flags, IPCL_ZONEID(connp), ipst); 6622 } 6623 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6624 if (mctl_present) 6625 freeb(first_mp1); 6626 putnext(rq, mp1); 6627 } 6628 } 6629 mutex_enter(&connfp->connf_lock); 6630 /* Follow the next pointer before releasing the conn. */ 6631 next_connp = connp->conn_next; 6632 CONN_DEC_REF(connp); 6633 connp = next_connp; 6634 } 6635 6636 /* Last one. Send it upstream. */ 6637 mutex_exit(&connfp->connf_lock); 6638 6639 /* 6640 * If this packet is coming from icmp_inbound_error_fanout ip_policy 6641 * will be set to false. 6642 */ 6643 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6644 ill_index = ill->ill_phyint->phyint_ifindex; 6645 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6646 if (mp == NULL) { 6647 CONN_DEC_REF(connp); 6648 if (mctl_present) { 6649 freeb(first_mp); 6650 } 6651 return; 6652 } 6653 } 6654 6655 rq = connp->conn_rq; 6656 if (!canputnext(rq)) { 6657 if (flags & IP_FF_RAWIP) { 6658 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6659 } else { 6660 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6661 } 6662 6663 freemsg(first_mp); 6664 } else { 6665 if (IPCL_IS_IPTUN(connp)) { 6666 /* 6667 * Tunneled packet. We enforce policy in the tunnel 6668 * module itself. 6669 * 6670 * Send the WHOLE packet up (incl. IPSEC_IN) without 6671 * a policy check. 6672 */ 6673 putnext(rq, first_mp); 6674 CONN_DEC_REF(connp); 6675 return; 6676 } 6677 6678 if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) { 6679 first_mp = ipsec_check_inbound_policy(first_mp, connp, 6680 ipha, NULL, mctl_present); 6681 } 6682 6683 if (first_mp != NULL) { 6684 int in_flags = 0; 6685 6686 /* 6687 * ip_fanout_proto also gets called 6688 * from icmp_inbound_error_fanout, in 6689 * which case the msg type is M_CTL. 6690 * Don't add info in this case for time 6691 * being. In future when there is a 6692 * need for knowing the inbound iface 6693 * index for ICMP error msgs, then this 6694 * can be changed 6695 */ 6696 if (connp->conn_recvif) 6697 in_flags = IPF_RECVIF; 6698 if (connp->conn_ip_recvpktinfo) { 6699 if (connp->conn_af_isv6) { 6700 /* 6701 * V6 only needs index 6702 */ 6703 in_flags |= IPF_RECVIF; 6704 } else { 6705 /* 6706 * V4 needs index + 6707 * matching address. 6708 */ 6709 in_flags |= IPF_RECVADDR; 6710 } 6711 } 6712 if ((in_flags != 0) && 6713 (mp->b_datap->db_type != M_CTL)) { 6714 6715 /* 6716 * the actual data will be contained in 6717 * b_cont upon successful return 6718 * of the following call else original 6719 * mblk is returned 6720 */ 6721 ASSERT(recv_ill != NULL); 6722 mp = ip_add_info(mp, recv_ill, 6723 in_flags, IPCL_ZONEID(connp), ipst); 6724 } 6725 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6726 putnext(rq, mp); 6727 if (mctl_present) 6728 freeb(first_mp); 6729 } 6730 } 6731 CONN_DEC_REF(connp); 6732 } 6733 6734 /* 6735 * Fanout for TCP packets 6736 * The caller puts <fport, lport> in the ports parameter. 6737 * 6738 * IPQoS Notes 6739 * Before sending it to the client, invoke IPPF processing. 6740 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6741 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6742 * ip_policy is false. 6743 */ 6744 static void 6745 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 6746 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 6747 { 6748 mblk_t *first_mp; 6749 boolean_t secure; 6750 uint32_t ill_index; 6751 int ip_hdr_len; 6752 tcph_t *tcph; 6753 boolean_t syn_present = B_FALSE; 6754 conn_t *connp; 6755 ip_stack_t *ipst = recv_ill->ill_ipst; 6756 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6757 6758 ASSERT(recv_ill != NULL); 6759 6760 first_mp = mp; 6761 if (mctl_present) { 6762 ASSERT(first_mp->b_datap->db_type == M_CTL); 6763 mp = first_mp->b_cont; 6764 secure = ipsec_in_is_secure(first_mp); 6765 ASSERT(mp != NULL); 6766 } else { 6767 secure = B_FALSE; 6768 } 6769 6770 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 6771 6772 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 6773 zoneid, ipst)) == NULL) { 6774 /* 6775 * No connected connection or listener. Send a 6776 * TH_RST via tcp_xmit_listeners_reset. 6777 */ 6778 6779 /* Initiate IPPf processing, if needed. */ 6780 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 6781 uint32_t ill_index; 6782 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6783 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 6784 if (first_mp == NULL) 6785 return; 6786 } 6787 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6788 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", 6789 zoneid)); 6790 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6791 ipst->ips_netstack->netstack_tcp, NULL); 6792 return; 6793 } 6794 6795 /* 6796 * Allocate the SYN for the TCP connection here itself 6797 */ 6798 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 6799 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 6800 if (IPCL_IS_TCP(connp)) { 6801 squeue_t *sqp; 6802 6803 /* 6804 * For fused tcp loopback, assign the eager's 6805 * squeue to be that of the active connect's. 6806 * Note that we don't check for IP_FF_LOOPBACK 6807 * here since this routine gets called only 6808 * for loopback (unlike the IPv6 counterpart). 6809 */ 6810 ASSERT(Q_TO_CONN(q) != NULL); 6811 if (do_tcp_fusion && 6812 !CONN_INBOUND_POLICY_PRESENT(connp, ipss) && 6813 !secure && 6814 !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy && 6815 IPCL_IS_TCP(Q_TO_CONN(q))) { 6816 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 6817 sqp = Q_TO_CONN(q)->conn_sqp; 6818 } else { 6819 sqp = IP_SQUEUE_GET(lbolt); 6820 } 6821 6822 mp->b_datap->db_struioflag |= STRUIO_EAGER; 6823 DB_CKSUMSTART(mp) = (intptr_t)sqp; 6824 syn_present = B_TRUE; 6825 } 6826 } 6827 6828 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 6829 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 6830 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6831 if ((flags & TH_RST) || (flags & TH_URG)) { 6832 CONN_DEC_REF(connp); 6833 freemsg(first_mp); 6834 return; 6835 } 6836 if (flags & TH_ACK) { 6837 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6838 ipst->ips_netstack->netstack_tcp, connp); 6839 CONN_DEC_REF(connp); 6840 return; 6841 } 6842 6843 CONN_DEC_REF(connp); 6844 freemsg(first_mp); 6845 return; 6846 } 6847 6848 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6849 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6850 NULL, mctl_present); 6851 if (first_mp == NULL) { 6852 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6853 CONN_DEC_REF(connp); 6854 return; 6855 } 6856 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 6857 ASSERT(syn_present); 6858 if (mctl_present) { 6859 ASSERT(first_mp != mp); 6860 first_mp->b_datap->db_struioflag |= 6861 STRUIO_POLICY; 6862 } else { 6863 ASSERT(first_mp == mp); 6864 mp->b_datap->db_struioflag &= 6865 ~STRUIO_EAGER; 6866 mp->b_datap->db_struioflag |= 6867 STRUIO_POLICY; 6868 } 6869 } else { 6870 /* 6871 * Discard first_mp early since we're dealing with a 6872 * fully-connected conn_t and tcp doesn't do policy in 6873 * this case. 6874 */ 6875 if (mctl_present) { 6876 freeb(first_mp); 6877 mctl_present = B_FALSE; 6878 } 6879 first_mp = mp; 6880 } 6881 } 6882 6883 /* 6884 * Initiate policy processing here if needed. If we get here from 6885 * icmp_inbound_error_fanout, ip_policy is false. 6886 */ 6887 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6888 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6889 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6890 if (mp == NULL) { 6891 CONN_DEC_REF(connp); 6892 if (mctl_present) 6893 freeb(first_mp); 6894 return; 6895 } else if (mctl_present) { 6896 ASSERT(first_mp != mp); 6897 first_mp->b_cont = mp; 6898 } else { 6899 first_mp = mp; 6900 } 6901 } 6902 6903 6904 6905 /* Handle socket options. */ 6906 if (!syn_present && 6907 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6908 /* Add header */ 6909 ASSERT(recv_ill != NULL); 6910 /* 6911 * Since tcp does not support IP_RECVPKTINFO for V4, only pass 6912 * IPF_RECVIF. 6913 */ 6914 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp), 6915 ipst); 6916 if (mp == NULL) { 6917 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6918 CONN_DEC_REF(connp); 6919 if (mctl_present) 6920 freeb(first_mp); 6921 return; 6922 } else if (mctl_present) { 6923 /* 6924 * ip_add_info might return a new mp. 6925 */ 6926 ASSERT(first_mp != mp); 6927 first_mp->b_cont = mp; 6928 } else { 6929 first_mp = mp; 6930 } 6931 } 6932 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6933 if (IPCL_IS_TCP(connp)) { 6934 /* do not drain, certain use cases can blow the stack */ 6935 squeue_enter_nodrain(connp->conn_sqp, first_mp, 6936 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); 6937 } else { 6938 putnext(connp->conn_rq, first_mp); 6939 CONN_DEC_REF(connp); 6940 } 6941 } 6942 6943 /* 6944 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or 6945 * pass it along to ESP if the SPI is non-zero. 6946 * 6947 * One of three things can happen, all of which affect the passed-in mblk: 6948 * 6949 * 1.) The packet is stock UDP and has had its zero-SPI stripped. Return TRUE. 6950 * (NOTE: ICMP messages that go through here just get returned.) 6951 * 6952 * 2.) The packet is ESP-in-UDP, has been transformed into an equivalent 6953 * ESP packet, and is passed along to ESP. Return FALSE. 6954 * 6955 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return FALSE. 6956 */ 6957 static boolean_t 6958 zero_spi_check(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 6959 ill_t *recv_ill, ipsec_stack_t *ipss) 6960 { 6961 int shift, plen, iph_len = IPH_HDR_LENGTH(ipha); 6962 udpha_t *udpha; 6963 uint32_t *spi; 6964 uint8_t *orptr; 6965 boolean_t udp_pkt, free_ire; 6966 6967 if (DB_TYPE(mp) == M_CTL) { 6968 /* 6969 * ICMP message with UDP inside. Don't bother stripping, just 6970 * send it up. 6971 * 6972 * NOTE: Any app with UDP_NAT_T_ENDPOINT set is probably going 6973 * to ignore errors set by ICMP anyway ('cause they might be 6974 * forged), but that's the app's decision, not ours. 6975 */ 6976 6977 /* Bunch of reality checks for DEBUG kernels... */ 6978 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); 6979 ASSERT(((ipha_t *)mp->b_rptr)->ipha_protocol == IPPROTO_ICMP); 6980 ASSERT((uint8_t *)ipha != mp->b_rptr); 6981 6982 return (B_TRUE); 6983 } 6984 6985 ASSERT((uint8_t *)ipha == mp->b_rptr); 6986 plen = ntohs(ipha->ipha_length); 6987 6988 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) { 6989 /* 6990 * Most likely a keepalive for the benefit of an intervening 6991 * NAT. These aren't for us, per se, so drop it. 6992 * 6993 * RFC 3947/8 doesn't say for sure what to do for 2-3 6994 * byte packets (keepalives are 1-byte), but we'll drop them 6995 * also. 6996 */ 6997 ip_drop_packet(mp, B_TRUE, recv_ill, NULL, 6998 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper); 6999 return (B_FALSE); 7000 } 7001 7002 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) { 7003 mblk_t *tmp = msgpullup(mp, -1); 7004 7005 /* might as well pull it all up - it might be ESP. */ 7006 if (tmp == NULL) { 7007 ip_drop_packet(mp, B_TRUE, recv_ill, NULL, 7008 DROPPER(ipss, ipds_esp_nomem), 7009 &ipss->ipsec_dropper); 7010 return (B_FALSE); 7011 } 7012 freemsg(mp); 7013 mp = tmp; 7014 } 7015 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t)); 7016 if (*spi == 0) { 7017 /* UDP packet - remove 0-spi. */ 7018 shift = sizeof (uint32_t); 7019 } else { 7020 /* ESP-in-UDP packet - reduce to ESP. */ 7021 ipha->ipha_protocol = IPPROTO_ESP; 7022 shift = sizeof (udpha_t); 7023 } 7024 7025 /* Fix IP header */ 7026 ipha->ipha_length = htons(plen - shift); 7027 ipha->ipha_hdr_checksum = 0; 7028 7029 orptr = mp->b_rptr; 7030 mp->b_rptr += shift; 7031 7032 if (*spi == 0) { 7033 ASSERT((uint8_t *)ipha == orptr); 7034 udpha = (udpha_t *)(orptr + iph_len); 7035 udpha->uha_length = htons(plen - shift - iph_len); 7036 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */ 7037 udp_pkt = B_TRUE; 7038 } else { 7039 udp_pkt = B_FALSE; 7040 } 7041 ovbcopy(orptr, orptr + shift, iph_len); 7042 if (!udp_pkt) /* Punt up for ESP processing. */ { 7043 ipha = (ipha_t *)(orptr + shift); 7044 7045 free_ire = (ire == NULL); 7046 if (free_ire) { 7047 /* Re-acquire ire. */ 7048 ire = ire_cache_lookup(ipha->ipha_dst, ALL_ZONES, NULL, 7049 ipss->ipsec_netstack->netstack_ip); 7050 if (ire == NULL || !(ire->ire_type & IRE_LOCAL)) { 7051 if (ire != NULL) 7052 ire_refrele(ire); 7053 /* 7054 * Do a regular freemsg(), as this is an IP 7055 * error (no local route) not an IPsec one. 7056 */ 7057 freemsg(mp); 7058 } 7059 } 7060 7061 ip_proto_input(q, mp, ipha, ire, recv_ill, B_TRUE); 7062 if (free_ire) 7063 ire_refrele(ire); 7064 } 7065 7066 return (udp_pkt); 7067 } 7068 7069 /* 7070 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 7071 * We are responsible for disposing of mp, such as by freemsg() or putnext() 7072 * Caller is responsible for dropping references to the conn, and freeing 7073 * first_mp. 7074 * 7075 * IPQoS Notes 7076 * Before sending it to the client, invoke IPPF processing. Policy processing 7077 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 7078 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 7079 * ip_wput_local, ip_policy is false. 7080 */ 7081 static void 7082 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 7083 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 7084 boolean_t ip_policy) 7085 { 7086 boolean_t mctl_present = (first_mp != NULL); 7087 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 7088 uint32_t ill_index; 7089 ip_stack_t *ipst = recv_ill->ill_ipst; 7090 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 7091 7092 ASSERT(ill != NULL); 7093 7094 if (mctl_present) 7095 first_mp->b_cont = mp; 7096 else 7097 first_mp = mp; 7098 7099 if (CONN_UDP_FLOWCTLD(connp)) { 7100 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 7101 freemsg(first_mp); 7102 return; 7103 } 7104 7105 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 7106 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 7107 NULL, mctl_present); 7108 if (first_mp == NULL) { 7109 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 7110 return; /* Freed by ipsec_check_inbound_policy(). */ 7111 } 7112 } 7113 if (mctl_present) 7114 freeb(first_mp); 7115 7116 /* Let's hope the compilers utter "branch, predict-not-taken..." ;) */ 7117 if (connp->conn_udp->udp_nat_t_endpoint) { 7118 if (mctl_present) { 7119 /* mctl_present *shouldn't* happen. */ 7120 ip_drop_packet(mp, B_TRUE, NULL, NULL, 7121 DROPPER(ipss, ipds_esp_nat_t_ipsec), 7122 &ipss->ipsec_dropper); 7123 return; 7124 } 7125 7126 if (!zero_spi_check(ill->ill_rq, mp, ipha, NULL, recv_ill, 7127 ipss)) { 7128 return; 7129 } 7130 } 7131 7132 /* Handle options. */ 7133 if (connp->conn_recvif) 7134 in_flags = IPF_RECVIF; 7135 /* 7136 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag 7137 * passed to ip_add_info is based on IP version of connp. 7138 */ 7139 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 7140 if (connp->conn_af_isv6) { 7141 /* 7142 * V6 only needs index 7143 */ 7144 in_flags |= IPF_RECVIF; 7145 } else { 7146 /* 7147 * V4 needs index + matching address. 7148 */ 7149 in_flags |= IPF_RECVADDR; 7150 } 7151 } 7152 7153 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 7154 in_flags |= IPF_RECVSLLA; 7155 7156 /* 7157 * Initiate IPPF processing here, if needed. Note first_mp won't be 7158 * freed if the packet is dropped. The caller will do so. 7159 */ 7160 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 7161 ill_index = recv_ill->ill_phyint->phyint_ifindex; 7162 ip_process(IPP_LOCAL_IN, &mp, ill_index); 7163 if (mp == NULL) { 7164 return; 7165 } 7166 } 7167 if ((in_flags != 0) && 7168 (mp->b_datap->db_type != M_CTL)) { 7169 /* 7170 * The actual data will be contained in b_cont 7171 * upon successful return of the following call 7172 * else original mblk is returned 7173 */ 7174 ASSERT(recv_ill != NULL); 7175 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp), 7176 ipst); 7177 } 7178 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 7179 /* Send it upstream */ 7180 CONN_UDP_RECV(connp, mp); 7181 } 7182 7183 /* 7184 * Fanout for UDP packets. 7185 * The caller puts <fport, lport> in the ports parameter. 7186 * 7187 * If SO_REUSEADDR is set all multicast and broadcast packets 7188 * will be delivered to all streams bound to the same port. 7189 * 7190 * Zones notes: 7191 * Multicast and broadcast packets will be distributed to streams in all zones. 7192 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 7193 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 7194 * packets. To maintain this behavior with multiple zones, the conns are grouped 7195 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 7196 * each zone. If unset, all the following conns in the same zone are skipped. 7197 */ 7198 static void 7199 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 7200 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 7201 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 7202 { 7203 uint32_t dstport, srcport; 7204 ipaddr_t dst; 7205 mblk_t *first_mp; 7206 boolean_t secure; 7207 in6_addr_t v6src; 7208 conn_t *connp; 7209 connf_t *connfp; 7210 conn_t *first_connp; 7211 conn_t *next_connp; 7212 mblk_t *mp1, *first_mp1; 7213 ipaddr_t src; 7214 zoneid_t last_zoneid; 7215 boolean_t reuseaddr; 7216 boolean_t shared_addr; 7217 ip_stack_t *ipst; 7218 7219 ASSERT(recv_ill != NULL); 7220 ipst = recv_ill->ill_ipst; 7221 7222 first_mp = mp; 7223 if (mctl_present) { 7224 mp = first_mp->b_cont; 7225 first_mp->b_cont = NULL; 7226 secure = ipsec_in_is_secure(first_mp); 7227 ASSERT(mp != NULL); 7228 } else { 7229 first_mp = NULL; 7230 secure = B_FALSE; 7231 } 7232 7233 /* Extract ports in net byte order */ 7234 dstport = htons(ntohl(ports) & 0xFFFF); 7235 srcport = htons(ntohl(ports) >> 16); 7236 dst = ipha->ipha_dst; 7237 src = ipha->ipha_src; 7238 7239 shared_addr = (zoneid == ALL_ZONES); 7240 if (shared_addr) { 7241 /* 7242 * No need to handle exclusive-stack zones since ALL_ZONES 7243 * only applies to the shared stack. 7244 */ 7245 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 7246 if (zoneid == ALL_ZONES) 7247 zoneid = tsol_packet_to_zoneid(mp); 7248 } 7249 7250 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7251 mutex_enter(&connfp->connf_lock); 7252 connp = connfp->connf_head; 7253 if (!broadcast && !CLASSD(dst)) { 7254 /* 7255 * Not broadcast or multicast. Send to the one (first) 7256 * client we find. No need to check conn_wantpacket() 7257 * since IP_BOUND_IF/conn_incoming_ill does not apply to 7258 * IPv4 unicast packets. 7259 */ 7260 while ((connp != NULL) && 7261 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || 7262 !IPCL_ZONE_MATCH(connp, zoneid))) { 7263 connp = connp->conn_next; 7264 } 7265 7266 if (connp == NULL || connp->conn_upq == NULL) 7267 goto notfound; 7268 7269 if (is_system_labeled() && 7270 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7271 connp)) 7272 goto notfound; 7273 7274 CONN_INC_REF(connp); 7275 mutex_exit(&connfp->connf_lock); 7276 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7277 flags, recv_ill, ip_policy); 7278 IP_STAT(ipst, ip_udp_fannorm); 7279 CONN_DEC_REF(connp); 7280 return; 7281 } 7282 7283 /* 7284 * Broadcast and multicast case 7285 * 7286 * Need to check conn_wantpacket(). 7287 * If SO_REUSEADDR has been set on the first we send the 7288 * packet to all clients that have joined the group and 7289 * match the port. 7290 */ 7291 7292 while (connp != NULL) { 7293 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 7294 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7295 (!is_system_labeled() || 7296 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7297 connp))) 7298 break; 7299 connp = connp->conn_next; 7300 } 7301 7302 if (connp == NULL || connp->conn_upq == NULL) 7303 goto notfound; 7304 7305 first_connp = connp; 7306 /* 7307 * When SO_REUSEADDR is not set, send the packet only to the first 7308 * matching connection in its zone by keeping track of the zoneid. 7309 */ 7310 reuseaddr = first_connp->conn_reuseaddr; 7311 last_zoneid = first_connp->conn_zoneid; 7312 7313 CONN_INC_REF(connp); 7314 connp = connp->conn_next; 7315 for (;;) { 7316 while (connp != NULL) { 7317 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 7318 (reuseaddr || connp->conn_zoneid != last_zoneid) && 7319 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7320 (!is_system_labeled() || 7321 tsol_receive_local(mp, &dst, IPV4_VERSION, 7322 shared_addr, connp))) 7323 break; 7324 connp = connp->conn_next; 7325 } 7326 /* 7327 * Just copy the data part alone. The mctl part is 7328 * needed just for verifying policy and it is never 7329 * sent up. 7330 */ 7331 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7332 ((mp1 = copymsg(mp)) == NULL))) { 7333 /* 7334 * No more interested clients or memory 7335 * allocation failed 7336 */ 7337 connp = first_connp; 7338 break; 7339 } 7340 if (connp->conn_zoneid != last_zoneid) { 7341 /* 7342 * Update the zoneid so that the packet isn't sent to 7343 * any more conns in the same zone unless SO_REUSEADDR 7344 * is set. 7345 */ 7346 reuseaddr = connp->conn_reuseaddr; 7347 last_zoneid = connp->conn_zoneid; 7348 } 7349 if (first_mp != NULL) { 7350 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7351 ipsec_info_type == IPSEC_IN); 7352 first_mp1 = ipsec_in_tag(first_mp, NULL, 7353 ipst->ips_netstack); 7354 if (first_mp1 == NULL) { 7355 freemsg(mp1); 7356 connp = first_connp; 7357 break; 7358 } 7359 } else { 7360 first_mp1 = NULL; 7361 } 7362 CONN_INC_REF(connp); 7363 mutex_exit(&connfp->connf_lock); 7364 /* 7365 * IPQoS notes: We don't send the packet for policy 7366 * processing here, will do it for the last one (below). 7367 * i.e. we do it per-packet now, but if we do policy 7368 * processing per-conn, then we would need to do it 7369 * here too. 7370 */ 7371 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7372 ipha, flags, recv_ill, B_FALSE); 7373 mutex_enter(&connfp->connf_lock); 7374 /* Follow the next pointer before releasing the conn. */ 7375 next_connp = connp->conn_next; 7376 IP_STAT(ipst, ip_udp_fanmb); 7377 CONN_DEC_REF(connp); 7378 connp = next_connp; 7379 } 7380 7381 /* Last one. Send it upstream. */ 7382 mutex_exit(&connfp->connf_lock); 7383 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7384 recv_ill, ip_policy); 7385 IP_STAT(ipst, ip_udp_fanmb); 7386 CONN_DEC_REF(connp); 7387 return; 7388 7389 notfound: 7390 7391 mutex_exit(&connfp->connf_lock); 7392 IP_STAT(ipst, ip_udp_fanothers); 7393 /* 7394 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 7395 * have already been matched above, since they live in the IPv4 7396 * fanout tables. This implies we only need to 7397 * check for IPv6 in6addr_any endpoints here. 7398 * Thus we compare using ipv6_all_zeros instead of the destination 7399 * address, except for the multicast group membership lookup which 7400 * uses the IPv4 destination. 7401 */ 7402 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 7403 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7404 mutex_enter(&connfp->connf_lock); 7405 connp = connfp->connf_head; 7406 if (!broadcast && !CLASSD(dst)) { 7407 while (connp != NULL) { 7408 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7409 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && 7410 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7411 !connp->conn_ipv6_v6only) 7412 break; 7413 connp = connp->conn_next; 7414 } 7415 7416 if (connp != NULL && is_system_labeled() && 7417 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7418 connp)) 7419 connp = NULL; 7420 7421 if (connp == NULL || connp->conn_upq == NULL) { 7422 /* 7423 * No one bound to this port. Is 7424 * there a client that wants all 7425 * unclaimed datagrams? 7426 */ 7427 mutex_exit(&connfp->connf_lock); 7428 7429 if (mctl_present) 7430 first_mp->b_cont = mp; 7431 else 7432 first_mp = mp; 7433 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP]. 7434 connf_head != NULL) { 7435 ip_fanout_proto(q, first_mp, ill, ipha, 7436 flags | IP_FF_RAWIP, mctl_present, 7437 ip_policy, recv_ill, zoneid); 7438 } else { 7439 if (ip_fanout_send_icmp(q, first_mp, flags, 7440 ICMP_DEST_UNREACHABLE, 7441 ICMP_PORT_UNREACHABLE, 7442 mctl_present, zoneid, ipst)) { 7443 BUMP_MIB(ill->ill_ip_mib, 7444 udpIfStatsNoPorts); 7445 } 7446 } 7447 return; 7448 } 7449 7450 CONN_INC_REF(connp); 7451 mutex_exit(&connfp->connf_lock); 7452 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7453 flags, recv_ill, ip_policy); 7454 CONN_DEC_REF(connp); 7455 return; 7456 } 7457 /* 7458 * IPv4 multicast packet being delivered to an AF_INET6 7459 * in6addr_any endpoint. 7460 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 7461 * and not conn_wantpacket_v6() since any multicast membership is 7462 * for an IPv4-mapped multicast address. 7463 * The packet is sent to all clients in all zones that have joined the 7464 * group and match the port. 7465 */ 7466 while (connp != NULL) { 7467 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7468 srcport, v6src) && 7469 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7470 (!is_system_labeled() || 7471 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7472 connp))) 7473 break; 7474 connp = connp->conn_next; 7475 } 7476 7477 if (connp == NULL || connp->conn_upq == NULL) { 7478 /* 7479 * No one bound to this port. Is 7480 * there a client that wants all 7481 * unclaimed datagrams? 7482 */ 7483 mutex_exit(&connfp->connf_lock); 7484 7485 if (mctl_present) 7486 first_mp->b_cont = mp; 7487 else 7488 first_mp = mp; 7489 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head != 7490 NULL) { 7491 ip_fanout_proto(q, first_mp, ill, ipha, 7492 flags | IP_FF_RAWIP, mctl_present, ip_policy, 7493 recv_ill, zoneid); 7494 } else { 7495 /* 7496 * We used to attempt to send an icmp error here, but 7497 * since this is known to be a multicast packet 7498 * and we don't send icmp errors in response to 7499 * multicast, just drop the packet and give up sooner. 7500 */ 7501 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 7502 freemsg(first_mp); 7503 } 7504 return; 7505 } 7506 7507 first_connp = connp; 7508 7509 CONN_INC_REF(connp); 7510 connp = connp->conn_next; 7511 for (;;) { 7512 while (connp != NULL) { 7513 if (IPCL_UDP_MATCH_V6(connp, dstport, 7514 ipv6_all_zeros, srcport, v6src) && 7515 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7516 (!is_system_labeled() || 7517 tsol_receive_local(mp, &dst, IPV4_VERSION, 7518 shared_addr, connp))) 7519 break; 7520 connp = connp->conn_next; 7521 } 7522 /* 7523 * Just copy the data part alone. The mctl part is 7524 * needed just for verifying policy and it is never 7525 * sent up. 7526 */ 7527 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7528 ((mp1 = copymsg(mp)) == NULL))) { 7529 /* 7530 * No more intested clients or memory 7531 * allocation failed 7532 */ 7533 connp = first_connp; 7534 break; 7535 } 7536 if (first_mp != NULL) { 7537 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7538 ipsec_info_type == IPSEC_IN); 7539 first_mp1 = ipsec_in_tag(first_mp, NULL, 7540 ipst->ips_netstack); 7541 if (first_mp1 == NULL) { 7542 freemsg(mp1); 7543 connp = first_connp; 7544 break; 7545 } 7546 } else { 7547 first_mp1 = NULL; 7548 } 7549 CONN_INC_REF(connp); 7550 mutex_exit(&connfp->connf_lock); 7551 /* 7552 * IPQoS notes: We don't send the packet for policy 7553 * processing here, will do it for the last one (below). 7554 * i.e. we do it per-packet now, but if we do policy 7555 * processing per-conn, then we would need to do it 7556 * here too. 7557 */ 7558 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7559 ipha, flags, recv_ill, B_FALSE); 7560 mutex_enter(&connfp->connf_lock); 7561 /* Follow the next pointer before releasing the conn. */ 7562 next_connp = connp->conn_next; 7563 CONN_DEC_REF(connp); 7564 connp = next_connp; 7565 } 7566 7567 /* Last one. Send it upstream. */ 7568 mutex_exit(&connfp->connf_lock); 7569 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7570 recv_ill, ip_policy); 7571 CONN_DEC_REF(connp); 7572 } 7573 7574 /* 7575 * Complete the ip_wput header so that it 7576 * is possible to generate ICMP 7577 * errors. 7578 */ 7579 int 7580 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst) 7581 { 7582 ire_t *ire; 7583 7584 if (ipha->ipha_src == INADDR_ANY) { 7585 ire = ire_lookup_local(zoneid, ipst); 7586 if (ire == NULL) { 7587 ip1dbg(("ip_hdr_complete: no source IRE\n")); 7588 return (1); 7589 } 7590 ipha->ipha_src = ire->ire_addr; 7591 ire_refrele(ire); 7592 } 7593 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 7594 ipha->ipha_hdr_checksum = 0; 7595 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 7596 return (0); 7597 } 7598 7599 /* 7600 * Nobody should be sending 7601 * packets up this stream 7602 */ 7603 static void 7604 ip_lrput(queue_t *q, mblk_t *mp) 7605 { 7606 mblk_t *mp1; 7607 7608 switch (mp->b_datap->db_type) { 7609 case M_FLUSH: 7610 /* Turn around */ 7611 if (*mp->b_rptr & FLUSHW) { 7612 *mp->b_rptr &= ~FLUSHR; 7613 qreply(q, mp); 7614 return; 7615 } 7616 break; 7617 } 7618 /* Could receive messages that passed through ar_rput */ 7619 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 7620 mp1->b_prev = mp1->b_next = NULL; 7621 freemsg(mp); 7622 } 7623 7624 /* Nobody should be sending packets down this stream */ 7625 /* ARGSUSED */ 7626 void 7627 ip_lwput(queue_t *q, mblk_t *mp) 7628 { 7629 freemsg(mp); 7630 } 7631 7632 /* 7633 * Move the first hop in any source route to ipha_dst and remove that part of 7634 * the source route. Called by other protocols. Errors in option formatting 7635 * are ignored - will be handled by ip_wput_options Return the final 7636 * destination (either ipha_dst or the last entry in a source route.) 7637 */ 7638 ipaddr_t 7639 ip_massage_options(ipha_t *ipha, netstack_t *ns) 7640 { 7641 ipoptp_t opts; 7642 uchar_t *opt; 7643 uint8_t optval; 7644 uint8_t optlen; 7645 ipaddr_t dst; 7646 int i; 7647 ire_t *ire; 7648 ip_stack_t *ipst = ns->netstack_ip; 7649 7650 ip2dbg(("ip_massage_options\n")); 7651 dst = ipha->ipha_dst; 7652 for (optval = ipoptp_first(&opts, ipha); 7653 optval != IPOPT_EOL; 7654 optval = ipoptp_next(&opts)) { 7655 opt = opts.ipoptp_cur; 7656 switch (optval) { 7657 uint8_t off; 7658 case IPOPT_SSRR: 7659 case IPOPT_LSRR: 7660 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 7661 ip1dbg(("ip_massage_options: bad src route\n")); 7662 break; 7663 } 7664 optlen = opts.ipoptp_len; 7665 off = opt[IPOPT_OFFSET]; 7666 off--; 7667 redo_srr: 7668 if (optlen < IP_ADDR_LEN || 7669 off > optlen - IP_ADDR_LEN) { 7670 /* End of source route */ 7671 ip1dbg(("ip_massage_options: end of SR\n")); 7672 break; 7673 } 7674 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 7675 ip1dbg(("ip_massage_options: next hop 0x%x\n", 7676 ntohl(dst))); 7677 /* 7678 * Check if our address is present more than 7679 * once as consecutive hops in source route. 7680 * XXX verify per-interface ip_forwarding 7681 * for source route? 7682 */ 7683 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 7684 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 7685 if (ire != NULL) { 7686 ire_refrele(ire); 7687 off += IP_ADDR_LEN; 7688 goto redo_srr; 7689 } 7690 if (dst == htonl(INADDR_LOOPBACK)) { 7691 ip1dbg(("ip_massage_options: loopback addr in " 7692 "source route!\n")); 7693 break; 7694 } 7695 /* 7696 * Update ipha_dst to be the first hop and remove the 7697 * first hop from the source route (by overwriting 7698 * part of the option with NOP options). 7699 */ 7700 ipha->ipha_dst = dst; 7701 /* Put the last entry in dst */ 7702 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 7703 3; 7704 bcopy(&opt[off], &dst, IP_ADDR_LEN); 7705 7706 ip1dbg(("ip_massage_options: last hop 0x%x\n", 7707 ntohl(dst))); 7708 /* Move down and overwrite */ 7709 opt[IP_ADDR_LEN] = opt[0]; 7710 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 7711 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 7712 for (i = 0; i < IP_ADDR_LEN; i++) 7713 opt[i] = IPOPT_NOP; 7714 break; 7715 } 7716 } 7717 return (dst); 7718 } 7719 7720 /* 7721 * Return the network mask 7722 * associated with the specified address. 7723 */ 7724 ipaddr_t 7725 ip_net_mask(ipaddr_t addr) 7726 { 7727 uchar_t *up = (uchar_t *)&addr; 7728 ipaddr_t mask = 0; 7729 uchar_t *maskp = (uchar_t *)&mask; 7730 7731 #if defined(__i386) || defined(__amd64) 7732 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 7733 #endif 7734 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 7735 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 7736 #endif 7737 if (CLASSD(addr)) { 7738 maskp[0] = 0xF0; 7739 return (mask); 7740 } 7741 if (addr == 0) 7742 return (0); 7743 maskp[0] = 0xFF; 7744 if ((up[0] & 0x80) == 0) 7745 return (mask); 7746 7747 maskp[1] = 0xFF; 7748 if ((up[0] & 0xC0) == 0x80) 7749 return (mask); 7750 7751 maskp[2] = 0xFF; 7752 if ((up[0] & 0xE0) == 0xC0) 7753 return (mask); 7754 7755 /* Must be experimental or multicast, indicate as much */ 7756 return ((ipaddr_t)0); 7757 } 7758 7759 /* 7760 * Select an ill for the packet by considering load spreading across 7761 * a different ill in the group if dst_ill is part of some group. 7762 */ 7763 ill_t * 7764 ip_newroute_get_dst_ill(ill_t *dst_ill) 7765 { 7766 ill_t *ill; 7767 7768 /* 7769 * We schedule irrespective of whether the source address is 7770 * INADDR_ANY or not. illgrp_scheduler returns a held ill. 7771 */ 7772 ill = illgrp_scheduler(dst_ill); 7773 if (ill == NULL) 7774 return (NULL); 7775 7776 /* 7777 * For groups with names ip_sioctl_groupname ensures that all 7778 * ills are of same type. For groups without names, ifgrp_insert 7779 * ensures this. 7780 */ 7781 ASSERT(dst_ill->ill_type == ill->ill_type); 7782 7783 return (ill); 7784 } 7785 7786 /* 7787 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. 7788 */ 7789 ill_t * 7790 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6, 7791 ip_stack_t *ipst) 7792 { 7793 ill_t *ret_ill; 7794 7795 ASSERT(ifindex != 0); 7796 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL, 7797 ipst); 7798 if (ret_ill == NULL || 7799 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { 7800 if (isv6) { 7801 if (ill != NULL) { 7802 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7803 } else { 7804 BUMP_MIB(&ipst->ips_ip6_mib, 7805 ipIfStatsOutDiscards); 7806 } 7807 ip1dbg(("ip_grab_attach_ill (IPv6): " 7808 "bad ifindex %d.\n", ifindex)); 7809 } else { 7810 if (ill != NULL) { 7811 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7812 } else { 7813 BUMP_MIB(&ipst->ips_ip_mib, 7814 ipIfStatsOutDiscards); 7815 } 7816 ip1dbg(("ip_grab_attach_ill (IPv4): " 7817 "bad ifindex %d.\n", ifindex)); 7818 } 7819 if (ret_ill != NULL) 7820 ill_refrele(ret_ill); 7821 freemsg(first_mp); 7822 return (NULL); 7823 } 7824 7825 return (ret_ill); 7826 } 7827 7828 /* 7829 * IPv4 - 7830 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 7831 * out a packet to a destination address for which we do not have specific 7832 * (or sufficient) routing information. 7833 * 7834 * NOTE : These are the scopes of some of the variables that point at IRE, 7835 * which needs to be followed while making any future modifications 7836 * to avoid memory leaks. 7837 * 7838 * - ire and sire are the entries looked up initially by 7839 * ire_ftable_lookup. 7840 * - ipif_ire is used to hold the interface ire associated with 7841 * the new cache ire. But it's scope is limited, so we always REFRELE 7842 * it before branching out to error paths. 7843 * - save_ire is initialized before ire_create, so that ire returned 7844 * by ire_create will not over-write the ire. We REFRELE save_ire 7845 * before breaking out of the switch. 7846 * 7847 * Thus on failures, we have to REFRELE only ire and sire, if they 7848 * are not NULL. 7849 */ 7850 void 7851 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, conn_t *connp, 7852 zoneid_t zoneid, ip_stack_t *ipst) 7853 { 7854 areq_t *areq; 7855 ipaddr_t gw = 0; 7856 ire_t *ire = NULL; 7857 mblk_t *res_mp; 7858 ipaddr_t *addrp; 7859 ipaddr_t nexthop_addr; 7860 ipif_t *src_ipif = NULL; 7861 ill_t *dst_ill = NULL; 7862 ipha_t *ipha; 7863 ire_t *sire = NULL; 7864 mblk_t *first_mp; 7865 ire_t *save_ire; 7866 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ 7867 ushort_t ire_marks = 0; 7868 boolean_t mctl_present; 7869 ipsec_out_t *io; 7870 mblk_t *saved_mp; 7871 ire_t *first_sire = NULL; 7872 mblk_t *copy_mp = NULL; 7873 mblk_t *xmit_mp = NULL; 7874 ipaddr_t save_dst; 7875 uint32_t multirt_flags = 7876 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 7877 boolean_t multirt_is_resolvable; 7878 boolean_t multirt_resolve_next; 7879 boolean_t do_attach_ill = B_FALSE; 7880 boolean_t ip_nexthop = B_FALSE; 7881 tsol_ire_gw_secattr_t *attrp = NULL; 7882 tsol_gcgrp_t *gcgrp = NULL; 7883 tsol_gcgrp_addr_t ga; 7884 7885 if (ip_debug > 2) { 7886 /* ip1dbg */ 7887 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 7888 } 7889 7890 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7891 if (mctl_present) { 7892 io = (ipsec_out_t *)first_mp->b_rptr; 7893 ASSERT(io->ipsec_out_type == IPSEC_OUT); 7894 ASSERT(zoneid == io->ipsec_out_zoneid); 7895 ASSERT(zoneid != ALL_ZONES); 7896 } 7897 7898 ipha = (ipha_t *)mp->b_rptr; 7899 7900 /* All multicast lookups come through ip_newroute_ipif() */ 7901 if (CLASSD(dst)) { 7902 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 7903 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 7904 freemsg(first_mp); 7905 return; 7906 } 7907 7908 if (mctl_present && io->ipsec_out_attach_if) { 7909 /* ip_grab_attach_ill returns a held ill */ 7910 attach_ill = ip_grab_attach_ill(NULL, first_mp, 7911 io->ipsec_out_ill_index, B_FALSE, ipst); 7912 7913 /* Failure case frees things for us. */ 7914 if (attach_ill == NULL) 7915 return; 7916 7917 /* 7918 * Check if we need an ire that will not be 7919 * looked up by anybody else i.e. HIDDEN. 7920 */ 7921 if (ill_is_probeonly(attach_ill)) 7922 ire_marks = IRE_MARK_HIDDEN; 7923 } 7924 if (mctl_present && io->ipsec_out_ip_nexthop) { 7925 ip_nexthop = B_TRUE; 7926 nexthop_addr = io->ipsec_out_nexthop_addr; 7927 } 7928 /* 7929 * If this IRE is created for forwarding or it is not for 7930 * traffic for congestion controlled protocols, mark it as temporary. 7931 */ 7932 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 7933 ire_marks |= IRE_MARK_TEMPORARY; 7934 7935 /* 7936 * Get what we can from ire_ftable_lookup which will follow an IRE 7937 * chain until it gets the most specific information available. 7938 * For example, we know that there is no IRE_CACHE for this dest, 7939 * but there may be an IRE_OFFSUBNET which specifies a gateway. 7940 * ire_ftable_lookup will look up the gateway, etc. 7941 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 7942 * to the destination, of equal netmask length in the forward table, 7943 * will be recursively explored. If no information is available 7944 * for the final gateway of that route, we force the returned ire 7945 * to be equal to sire using MATCH_IRE_PARENT. 7946 * At least, in this case we have a starting point (in the buckets) 7947 * to look for other routes to the destination in the forward table. 7948 * This is actually used only for multirouting, where a list 7949 * of routes has to be processed in sequence. 7950 * 7951 * In the process of coming up with the most specific information, 7952 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry 7953 * for the gateway (i.e., one for which the ire_nce->nce_state is 7954 * not yet ND_REACHABLE, and is in the middle of arp resolution). 7955 * Two caveats when handling incomplete ire's in ip_newroute: 7956 * - we should be careful when accessing its ire_nce (specifically 7957 * the nce_res_mp) ast it might change underneath our feet, and, 7958 * - not all legacy code path callers are prepared to handle 7959 * incomplete ire's, so we should not create/add incomplete 7960 * ire_cache entries here. (See discussion about temporary solution 7961 * further below). 7962 * 7963 * In order to minimize packet dropping, and to preserve existing 7964 * behavior, we treat this case as if there were no IRE_CACHE for the 7965 * gateway, and instead use the IF_RESOLVER ire to send out 7966 * another request to ARP (this is achieved by passing the 7967 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the 7968 * arp response comes back in ip_wput_nondata, we will create 7969 * a per-dst ire_cache that has an ND_COMPLETE ire. 7970 * 7971 * Note that this is a temporary solution; the correct solution is 7972 * to create an incomplete per-dst ire_cache entry, and send the 7973 * packet out when the gw's nce is resolved. In order to achieve this, 7974 * all packet processing must have been completed prior to calling 7975 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need 7976 * to be modified to accomodate this solution. 7977 */ 7978 if (ip_nexthop) { 7979 /* 7980 * The first time we come here, we look for an IRE_INTERFACE 7981 * entry for the specified nexthop, set the dst to be the 7982 * nexthop address and create an IRE_CACHE entry for the 7983 * nexthop. The next time around, we are able to find an 7984 * IRE_CACHE entry for the nexthop, set the gateway to be the 7985 * nexthop address and create an IRE_CACHE entry for the 7986 * destination address via the specified nexthop. 7987 */ 7988 ire = ire_cache_lookup(nexthop_addr, zoneid, 7989 MBLK_GETLABEL(mp), ipst); 7990 if (ire != NULL) { 7991 gw = nexthop_addr; 7992 ire_marks |= IRE_MARK_PRIVATE_ADDR; 7993 } else { 7994 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 7995 IRE_INTERFACE, NULL, NULL, zoneid, 0, 7996 MBLK_GETLABEL(mp), 7997 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 7998 ipst); 7999 if (ire != NULL) { 8000 dst = nexthop_addr; 8001 } 8002 } 8003 } else if (attach_ill == NULL) { 8004 ire = ire_ftable_lookup(dst, 0, 0, 0, 8005 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), 8006 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 8007 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | 8008 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE, 8009 ipst); 8010 } else { 8011 /* 8012 * attach_ill is set only for communicating with 8013 * on-link hosts. So, don't look for DEFAULT. 8014 */ 8015 ipif_t *attach_ipif; 8016 8017 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 8018 if (attach_ipif == NULL) { 8019 ill_refrele(attach_ill); 8020 goto icmp_err_ret; 8021 } 8022 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, 8023 &sire, zoneid, 0, MBLK_GETLABEL(mp), 8024 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL | 8025 MATCH_IRE_SECATTR, ipst); 8026 ipif_refrele(attach_ipif); 8027 } 8028 ip3dbg(("ip_newroute: ire_ftable_lookup() " 8029 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 8030 8031 /* 8032 * This loop is run only once in most cases. 8033 * We loop to resolve further routes only when the destination 8034 * can be reached through multiple RTF_MULTIRT-flagged ires. 8035 */ 8036 do { 8037 /* Clear the previous iteration's values */ 8038 if (src_ipif != NULL) { 8039 ipif_refrele(src_ipif); 8040 src_ipif = NULL; 8041 } 8042 if (dst_ill != NULL) { 8043 ill_refrele(dst_ill); 8044 dst_ill = NULL; 8045 } 8046 8047 multirt_resolve_next = B_FALSE; 8048 /* 8049 * We check if packets have to be multirouted. 8050 * In this case, given the current <ire, sire> couple, 8051 * we look for the next suitable <ire, sire>. 8052 * This check is done in ire_multirt_lookup(), 8053 * which applies various criteria to find the next route 8054 * to resolve. ire_multirt_lookup() leaves <ire, sire> 8055 * unchanged if it detects it has not been tried yet. 8056 */ 8057 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8058 ip3dbg(("ip_newroute: starting next_resolution " 8059 "with first_mp %p, tag %d\n", 8060 (void *)first_mp, 8061 MULTIRT_DEBUG_TAGGED(first_mp))); 8062 8063 ASSERT(sire != NULL); 8064 multirt_is_resolvable = 8065 ire_multirt_lookup(&ire, &sire, multirt_flags, 8066 MBLK_GETLABEL(mp), ipst); 8067 8068 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 8069 "ire %p, sire %p\n", 8070 multirt_is_resolvable, 8071 (void *)ire, (void *)sire)); 8072 8073 if (!multirt_is_resolvable) { 8074 /* 8075 * No more multirt route to resolve; give up 8076 * (all routes resolved or no more 8077 * resolvable routes). 8078 */ 8079 if (ire != NULL) { 8080 ire_refrele(ire); 8081 ire = NULL; 8082 } 8083 } else { 8084 ASSERT(sire != NULL); 8085 ASSERT(ire != NULL); 8086 /* 8087 * We simply use first_sire as a flag that 8088 * indicates if a resolvable multirt route 8089 * has already been found. 8090 * If it is not the case, we may have to send 8091 * an ICMP error to report that the 8092 * destination is unreachable. 8093 * We do not IRE_REFHOLD first_sire. 8094 */ 8095 if (first_sire == NULL) { 8096 first_sire = sire; 8097 } 8098 } 8099 } 8100 if (ire == NULL) { 8101 if (ip_debug > 3) { 8102 /* ip2dbg */ 8103 pr_addr_dbg("ip_newroute: " 8104 "can't resolve %s\n", AF_INET, &dst); 8105 } 8106 ip3dbg(("ip_newroute: " 8107 "ire %p, sire %p, first_sire %p\n", 8108 (void *)ire, (void *)sire, (void *)first_sire)); 8109 8110 if (sire != NULL) { 8111 ire_refrele(sire); 8112 sire = NULL; 8113 } 8114 8115 if (first_sire != NULL) { 8116 /* 8117 * At least one multirt route has been found 8118 * in the same call to ip_newroute(); 8119 * there is no need to report an ICMP error. 8120 * first_sire was not IRE_REFHOLDed. 8121 */ 8122 MULTIRT_DEBUG_UNTAG(first_mp); 8123 freemsg(first_mp); 8124 return; 8125 } 8126 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 8127 RTA_DST, ipst); 8128 if (attach_ill != NULL) 8129 ill_refrele(attach_ill); 8130 goto icmp_err_ret; 8131 } 8132 8133 /* 8134 * Verify that the returned IRE does not have either 8135 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 8136 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 8137 */ 8138 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 8139 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 8140 if (attach_ill != NULL) 8141 ill_refrele(attach_ill); 8142 goto icmp_err_ret; 8143 } 8144 /* 8145 * Increment the ire_ob_pkt_count field for ire if it is an 8146 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 8147 * increment the same for the parent IRE, sire, if it is some 8148 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST) 8149 */ 8150 if ((ire->ire_type & IRE_INTERFACE) != 0) { 8151 UPDATE_OB_PKT_COUNT(ire); 8152 ire->ire_last_used_time = lbolt; 8153 } 8154 8155 if (sire != NULL) { 8156 gw = sire->ire_gateway_addr; 8157 ASSERT((sire->ire_type & (IRE_CACHETABLE | 8158 IRE_INTERFACE)) == 0); 8159 UPDATE_OB_PKT_COUNT(sire); 8160 sire->ire_last_used_time = lbolt; 8161 } 8162 /* 8163 * We have a route to reach the destination. 8164 * 8165 * 1) If the interface is part of ill group, try to get a new 8166 * ill taking load spreading into account. 8167 * 8168 * 2) After selecting the ill, get a source address that 8169 * might create good inbound load spreading. 8170 * ipif_select_source does this for us. 8171 * 8172 * If the application specified the ill (ifindex), we still 8173 * load spread. Only if the packets needs to go out 8174 * specifically on a given ill e.g. binding to 8175 * IPIF_NOFAILOVER address, then we don't try to use a 8176 * different ill for load spreading. 8177 */ 8178 if (attach_ill == NULL) { 8179 /* 8180 * Don't perform outbound load spreading in the 8181 * case of an RTF_MULTIRT route, as we actually 8182 * typically want to replicate outgoing packets 8183 * through particular interfaces. 8184 */ 8185 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8186 dst_ill = ire->ire_ipif->ipif_ill; 8187 /* for uniformity */ 8188 ill_refhold(dst_ill); 8189 } else { 8190 /* 8191 * If we are here trying to create an IRE_CACHE 8192 * for an offlink destination and have the 8193 * IRE_CACHE for the next hop and the latter is 8194 * using virtual IP source address selection i.e 8195 * it's ire->ire_ipif is pointing to a virtual 8196 * network interface (vni) then 8197 * ip_newroute_get_dst_ll() will return the vni 8198 * interface as the dst_ill. Since the vni is 8199 * virtual i.e not associated with any physical 8200 * interface, it cannot be the dst_ill, hence 8201 * in such a case call ip_newroute_get_dst_ll() 8202 * with the stq_ill instead of the ire_ipif ILL. 8203 * The function returns a refheld ill. 8204 */ 8205 if ((ire->ire_type == IRE_CACHE) && 8206 IS_VNI(ire->ire_ipif->ipif_ill)) 8207 dst_ill = ip_newroute_get_dst_ill( 8208 ire->ire_stq->q_ptr); 8209 else 8210 dst_ill = ip_newroute_get_dst_ill( 8211 ire->ire_ipif->ipif_ill); 8212 } 8213 if (dst_ill == NULL) { 8214 if (ip_debug > 2) { 8215 pr_addr_dbg("ip_newroute: " 8216 "no dst ill for dst" 8217 " %s\n", AF_INET, &dst); 8218 } 8219 goto icmp_err_ret; 8220 } 8221 } else { 8222 dst_ill = ire->ire_ipif->ipif_ill; 8223 /* for uniformity */ 8224 ill_refhold(dst_ill); 8225 /* 8226 * We should have found a route matching ill as we 8227 * called ire_ftable_lookup with MATCH_IRE_ILL. 8228 * Rather than asserting, when there is a mismatch, 8229 * we just drop the packet. 8230 */ 8231 if (dst_ill != attach_ill) { 8232 ip0dbg(("ip_newroute: Packet dropped as " 8233 "IPIF_NOFAILOVER ill is %s, " 8234 "ire->ire_ipif->ipif_ill is %s\n", 8235 attach_ill->ill_name, 8236 dst_ill->ill_name)); 8237 ill_refrele(attach_ill); 8238 goto icmp_err_ret; 8239 } 8240 } 8241 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ 8242 if (attach_ill != NULL) { 8243 ill_refrele(attach_ill); 8244 attach_ill = NULL; 8245 do_attach_ill = B_TRUE; 8246 } 8247 ASSERT(dst_ill != NULL); 8248 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 8249 8250 /* 8251 * Pick the best source address from dst_ill. 8252 * 8253 * 1) If it is part of a multipathing group, we would 8254 * like to spread the inbound packets across different 8255 * interfaces. ipif_select_source picks a random source 8256 * across the different ills in the group. 8257 * 8258 * 2) If it is not part of a multipathing group, we try 8259 * to pick the source address from the destination 8260 * route. Clustering assumes that when we have multiple 8261 * prefixes hosted on an interface, the prefix of the 8262 * source address matches the prefix of the destination 8263 * route. We do this only if the address is not 8264 * DEPRECATED. 8265 * 8266 * 3) If the conn is in a different zone than the ire, we 8267 * need to pick a source address from the right zone. 8268 * 8269 * NOTE : If we hit case (1) above, the prefix of the source 8270 * address picked may not match the prefix of the 8271 * destination routes prefix as ipif_select_source 8272 * does not look at "dst" while picking a source 8273 * address. 8274 * If we want the same behavior as (2), we will need 8275 * to change the behavior of ipif_select_source. 8276 */ 8277 ASSERT(src_ipif == NULL); 8278 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 8279 /* 8280 * The RTF_SETSRC flag is set in the parent ire (sire). 8281 * Check that the ipif matching the requested source 8282 * address still exists. 8283 */ 8284 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 8285 zoneid, NULL, NULL, NULL, NULL, ipst); 8286 } 8287 if (src_ipif == NULL) { 8288 ire_marks |= IRE_MARK_USESRC_CHECK; 8289 if ((dst_ill->ill_group != NULL) || 8290 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 8291 (connp != NULL && ire->ire_zoneid != zoneid && 8292 ire->ire_zoneid != ALL_ZONES) || 8293 (dst_ill->ill_usesrc_ifindex != 0)) { 8294 /* 8295 * If the destination is reachable via a 8296 * given gateway, the selected source address 8297 * should be in the same subnet as the gateway. 8298 * Otherwise, the destination is not reachable. 8299 * 8300 * If there are no interfaces on the same subnet 8301 * as the destination, ipif_select_source gives 8302 * first non-deprecated interface which might be 8303 * on a different subnet than the gateway. 8304 * This is not desirable. Hence pass the dst_ire 8305 * source address to ipif_select_source. 8306 * It is sure that the destination is reachable 8307 * with the dst_ire source address subnet. 8308 * So passing dst_ire source address to 8309 * ipif_select_source will make sure that the 8310 * selected source will be on the same subnet 8311 * as dst_ire source address. 8312 */ 8313 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 8314 src_ipif = ipif_select_source(dst_ill, saddr, 8315 zoneid); 8316 if (src_ipif == NULL) { 8317 if (ip_debug > 2) { 8318 pr_addr_dbg("ip_newroute: " 8319 "no src for dst %s ", 8320 AF_INET, &dst); 8321 printf("through interface %s\n", 8322 dst_ill->ill_name); 8323 } 8324 goto icmp_err_ret; 8325 } 8326 } else { 8327 src_ipif = ire->ire_ipif; 8328 ASSERT(src_ipif != NULL); 8329 /* hold src_ipif for uniformity */ 8330 ipif_refhold(src_ipif); 8331 } 8332 } 8333 8334 /* 8335 * Assign a source address while we have the conn. 8336 * We can't have ip_wput_ire pick a source address when the 8337 * packet returns from arp since we need to look at 8338 * conn_unspec_src and conn_zoneid, and we lose the conn when 8339 * going through arp. 8340 * 8341 * NOTE : ip_newroute_v6 does not have this piece of code as 8342 * it uses ip6i to store this information. 8343 */ 8344 if (ipha->ipha_src == INADDR_ANY && 8345 (connp == NULL || !connp->conn_unspec_src)) { 8346 ipha->ipha_src = src_ipif->ipif_src_addr; 8347 } 8348 if (ip_debug > 3) { 8349 /* ip2dbg */ 8350 pr_addr_dbg("ip_newroute: first hop %s\n", 8351 AF_INET, &gw); 8352 } 8353 ip2dbg(("\tire type %s (%d)\n", 8354 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 8355 8356 /* 8357 * The TTL of multirouted packets is bounded by the 8358 * ip_multirt_ttl ndd variable. 8359 */ 8360 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8361 /* Force TTL of multirouted packets */ 8362 if ((ipst->ips_ip_multirt_ttl > 0) && 8363 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 8364 ip2dbg(("ip_newroute: forcing multirt TTL " 8365 "to %d (was %d), dst 0x%08x\n", 8366 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 8367 ntohl(sire->ire_addr))); 8368 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 8369 } 8370 } 8371 /* 8372 * At this point in ip_newroute(), ire is either the 8373 * IRE_CACHE of the next-hop gateway for an off-subnet 8374 * destination or an IRE_INTERFACE type that should be used 8375 * to resolve an on-subnet destination or an on-subnet 8376 * next-hop gateway. 8377 * 8378 * In the IRE_CACHE case, we have the following : 8379 * 8380 * 1) src_ipif - used for getting a source address. 8381 * 8382 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8383 * means packets using this IRE_CACHE will go out on 8384 * dst_ill. 8385 * 8386 * 3) The IRE sire will point to the prefix that is the 8387 * longest matching route for the destination. These 8388 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST. 8389 * 8390 * The newly created IRE_CACHE entry for the off-subnet 8391 * destination is tied to both the prefix route and the 8392 * interface route used to resolve the next-hop gateway 8393 * via the ire_phandle and ire_ihandle fields, 8394 * respectively. 8395 * 8396 * In the IRE_INTERFACE case, we have the following : 8397 * 8398 * 1) src_ipif - used for getting a source address. 8399 * 8400 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8401 * means packets using the IRE_CACHE that we will build 8402 * here will go out on dst_ill. 8403 * 8404 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 8405 * to be created will only be tied to the IRE_INTERFACE 8406 * that was derived from the ire_ihandle field. 8407 * 8408 * If sire is non-NULL, it means the destination is 8409 * off-link and we will first create the IRE_CACHE for the 8410 * gateway. Next time through ip_newroute, we will create 8411 * the IRE_CACHE for the final destination as described 8412 * above. 8413 * 8414 * In both cases, after the current resolution has been 8415 * completed (or possibly initialised, in the IRE_INTERFACE 8416 * case), the loop may be re-entered to attempt the resolution 8417 * of another RTF_MULTIRT route. 8418 * 8419 * When an IRE_CACHE entry for the off-subnet destination is 8420 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 8421 * for further processing in emission loops. 8422 */ 8423 save_ire = ire; 8424 switch (ire->ire_type) { 8425 case IRE_CACHE: { 8426 ire_t *ipif_ire; 8427 8428 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); 8429 if (gw == 0) 8430 gw = ire->ire_gateway_addr; 8431 /* 8432 * We need 3 ire's to create a new cache ire for an 8433 * off-link destination from the cache ire of the 8434 * gateway. 8435 * 8436 * 1. The prefix ire 'sire' (Note that this does 8437 * not apply to the conn_nexthop_set case) 8438 * 2. The cache ire of the gateway 'ire' 8439 * 3. The interface ire 'ipif_ire' 8440 * 8441 * We have (1) and (2). We lookup (3) below. 8442 * 8443 * If there is no interface route to the gateway, 8444 * it is a race condition, where we found the cache 8445 * but the interface route has been deleted. 8446 */ 8447 if (ip_nexthop) { 8448 ipif_ire = ire_ihandle_lookup_onlink(ire); 8449 } else { 8450 ipif_ire = 8451 ire_ihandle_lookup_offlink(ire, sire); 8452 } 8453 if (ipif_ire == NULL) { 8454 ip1dbg(("ip_newroute: " 8455 "ire_ihandle_lookup_offlink failed\n")); 8456 goto icmp_err_ret; 8457 } 8458 8459 /* 8460 * Check cached gateway IRE for any security 8461 * attributes; if found, associate the gateway 8462 * credentials group to the destination IRE. 8463 */ 8464 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 8465 mutex_enter(&attrp->igsa_lock); 8466 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 8467 GCGRP_REFHOLD(gcgrp); 8468 mutex_exit(&attrp->igsa_lock); 8469 } 8470 8471 /* 8472 * XXX For the source of the resolver mp, 8473 * we are using the same DL_UNITDATA_REQ 8474 * (from save_ire->ire_nce->nce_res_mp) 8475 * though the save_ire is not pointing at the same ill. 8476 * This is incorrect. We need to send it up to the 8477 * resolver to get the right res_mp. For ethernets 8478 * this may be okay (ill_type == DL_ETHER). 8479 */ 8480 8481 ire = ire_create( 8482 (uchar_t *)&dst, /* dest address */ 8483 (uchar_t *)&ip_g_all_ones, /* mask */ 8484 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8485 (uchar_t *)&gw, /* gateway address */ 8486 &save_ire->ire_max_frag, 8487 save_ire->ire_nce, /* src nce */ 8488 dst_ill->ill_rq, /* recv-from queue */ 8489 dst_ill->ill_wq, /* send-to queue */ 8490 IRE_CACHE, /* IRE type */ 8491 src_ipif, 8492 (sire != NULL) ? 8493 sire->ire_mask : 0, /* Parent mask */ 8494 (sire != NULL) ? 8495 sire->ire_phandle : 0, /* Parent handle */ 8496 ipif_ire->ire_ihandle, /* Interface handle */ 8497 (sire != NULL) ? (sire->ire_flags & 8498 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 8499 (sire != NULL) ? 8500 &(sire->ire_uinfo) : &(save_ire->ire_uinfo), 8501 NULL, 8502 gcgrp, 8503 ipst); 8504 8505 if (ire == NULL) { 8506 if (gcgrp != NULL) { 8507 GCGRP_REFRELE(gcgrp); 8508 gcgrp = NULL; 8509 } 8510 ire_refrele(ipif_ire); 8511 ire_refrele(save_ire); 8512 break; 8513 } 8514 8515 /* reference now held by IRE */ 8516 gcgrp = NULL; 8517 8518 ire->ire_marks |= ire_marks; 8519 8520 /* 8521 * Prevent sire and ipif_ire from getting deleted. 8522 * The newly created ire is tied to both of them via 8523 * the phandle and ihandle respectively. 8524 */ 8525 if (sire != NULL) { 8526 IRB_REFHOLD(sire->ire_bucket); 8527 /* Has it been removed already ? */ 8528 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 8529 IRB_REFRELE(sire->ire_bucket); 8530 ire_refrele(ipif_ire); 8531 ire_refrele(save_ire); 8532 break; 8533 } 8534 } 8535 8536 IRB_REFHOLD(ipif_ire->ire_bucket); 8537 /* Has it been removed already ? */ 8538 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 8539 IRB_REFRELE(ipif_ire->ire_bucket); 8540 if (sire != NULL) 8541 IRB_REFRELE(sire->ire_bucket); 8542 ire_refrele(ipif_ire); 8543 ire_refrele(save_ire); 8544 break; 8545 } 8546 8547 xmit_mp = first_mp; 8548 /* 8549 * In the case of multirouting, a copy 8550 * of the packet is done before its sending. 8551 * The copy is used to attempt another 8552 * route resolution, in a next loop. 8553 */ 8554 if (ire->ire_flags & RTF_MULTIRT) { 8555 copy_mp = copymsg(first_mp); 8556 if (copy_mp != NULL) { 8557 xmit_mp = copy_mp; 8558 MULTIRT_DEBUG_TAG(first_mp); 8559 } 8560 } 8561 ire_add_then_send(q, ire, xmit_mp); 8562 ire_refrele(save_ire); 8563 8564 /* Assert that sire is not deleted yet. */ 8565 if (sire != NULL) { 8566 ASSERT(sire->ire_ptpn != NULL); 8567 IRB_REFRELE(sire->ire_bucket); 8568 } 8569 8570 /* Assert that ipif_ire is not deleted yet. */ 8571 ASSERT(ipif_ire->ire_ptpn != NULL); 8572 IRB_REFRELE(ipif_ire->ire_bucket); 8573 ire_refrele(ipif_ire); 8574 8575 /* 8576 * If copy_mp is not NULL, multirouting was 8577 * requested. We loop to initiate a next 8578 * route resolution attempt, starting from sire. 8579 */ 8580 if (copy_mp != NULL) { 8581 /* 8582 * Search for the next unresolved 8583 * multirt route. 8584 */ 8585 copy_mp = NULL; 8586 ipif_ire = NULL; 8587 ire = NULL; 8588 multirt_resolve_next = B_TRUE; 8589 continue; 8590 } 8591 if (sire != NULL) 8592 ire_refrele(sire); 8593 ipif_refrele(src_ipif); 8594 ill_refrele(dst_ill); 8595 return; 8596 } 8597 case IRE_IF_NORESOLVER: { 8598 8599 if (dst_ill->ill_phys_addr_length != IP_ADDR_LEN && 8600 dst_ill->ill_resolver_mp == NULL) { 8601 ip1dbg(("ip_newroute: dst_ill %p " 8602 "for IRE_IF_NORESOLVER ire %p has " 8603 "no ill_resolver_mp\n", 8604 (void *)dst_ill, (void *)ire)); 8605 break; 8606 } 8607 8608 /* 8609 * TSol note: We are creating the ire cache for the 8610 * destination 'dst'. If 'dst' is offlink, going 8611 * through the first hop 'gw', the security attributes 8612 * of 'dst' must be set to point to the gateway 8613 * credentials of gateway 'gw'. If 'dst' is onlink, it 8614 * is possible that 'dst' is a potential gateway that is 8615 * referenced by some route that has some security 8616 * attributes. Thus in the former case, we need to do a 8617 * gcgrp_lookup of 'gw' while in the latter case we 8618 * need to do gcgrp_lookup of 'dst' itself. 8619 */ 8620 ga.ga_af = AF_INET; 8621 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, 8622 &ga.ga_addr); 8623 gcgrp = gcgrp_lookup(&ga, B_FALSE); 8624 8625 ire = ire_create( 8626 (uchar_t *)&dst, /* dest address */ 8627 (uchar_t *)&ip_g_all_ones, /* mask */ 8628 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8629 (uchar_t *)&gw, /* gateway address */ 8630 &save_ire->ire_max_frag, 8631 NULL, /* no src nce */ 8632 dst_ill->ill_rq, /* recv-from queue */ 8633 dst_ill->ill_wq, /* send-to queue */ 8634 IRE_CACHE, 8635 src_ipif, 8636 save_ire->ire_mask, /* Parent mask */ 8637 (sire != NULL) ? /* Parent handle */ 8638 sire->ire_phandle : 0, 8639 save_ire->ire_ihandle, /* Interface handle */ 8640 (sire != NULL) ? sire->ire_flags & 8641 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 8642 &(save_ire->ire_uinfo), 8643 NULL, 8644 gcgrp, 8645 ipst); 8646 8647 if (ire == NULL) { 8648 if (gcgrp != NULL) { 8649 GCGRP_REFRELE(gcgrp); 8650 gcgrp = NULL; 8651 } 8652 ire_refrele(save_ire); 8653 break; 8654 } 8655 8656 /* reference now held by IRE */ 8657 gcgrp = NULL; 8658 8659 ire->ire_marks |= ire_marks; 8660 8661 /* Prevent save_ire from getting deleted */ 8662 IRB_REFHOLD(save_ire->ire_bucket); 8663 /* Has it been removed already ? */ 8664 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8665 IRB_REFRELE(save_ire->ire_bucket); 8666 ire_refrele(save_ire); 8667 break; 8668 } 8669 8670 /* 8671 * In the case of multirouting, a copy 8672 * of the packet is made before it is sent. 8673 * The copy is used in the next 8674 * loop to attempt another resolution. 8675 */ 8676 xmit_mp = first_mp; 8677 if ((sire != NULL) && 8678 (sire->ire_flags & RTF_MULTIRT)) { 8679 copy_mp = copymsg(first_mp); 8680 if (copy_mp != NULL) { 8681 xmit_mp = copy_mp; 8682 MULTIRT_DEBUG_TAG(first_mp); 8683 } 8684 } 8685 ire_add_then_send(q, ire, xmit_mp); 8686 8687 /* Assert that it is not deleted yet. */ 8688 ASSERT(save_ire->ire_ptpn != NULL); 8689 IRB_REFRELE(save_ire->ire_bucket); 8690 ire_refrele(save_ire); 8691 8692 if (copy_mp != NULL) { 8693 /* 8694 * If we found a (no)resolver, we ignore any 8695 * trailing top priority IRE_CACHE in further 8696 * loops. This ensures that we do not omit any 8697 * (no)resolver. 8698 * This IRE_CACHE, if any, will be processed 8699 * by another thread entering ip_newroute(). 8700 * IRE_CACHE entries, if any, will be processed 8701 * by another thread entering ip_newroute(), 8702 * (upon resolver response, for instance). 8703 * This aims to force parallel multirt 8704 * resolutions as soon as a packet must be sent. 8705 * In the best case, after the tx of only one 8706 * packet, all reachable routes are resolved. 8707 * Otherwise, the resolution of all RTF_MULTIRT 8708 * routes would require several emissions. 8709 */ 8710 multirt_flags &= ~MULTIRT_CACHEGW; 8711 8712 /* 8713 * Search for the next unresolved multirt 8714 * route. 8715 */ 8716 copy_mp = NULL; 8717 save_ire = NULL; 8718 ire = NULL; 8719 multirt_resolve_next = B_TRUE; 8720 continue; 8721 } 8722 8723 /* 8724 * Don't need sire anymore 8725 */ 8726 if (sire != NULL) 8727 ire_refrele(sire); 8728 8729 ipif_refrele(src_ipif); 8730 ill_refrele(dst_ill); 8731 return; 8732 } 8733 case IRE_IF_RESOLVER: 8734 /* 8735 * We can't build an IRE_CACHE yet, but at least we 8736 * found a resolver that can help. 8737 */ 8738 res_mp = dst_ill->ill_resolver_mp; 8739 if (!OK_RESOLVER_MP(res_mp)) 8740 break; 8741 8742 /* 8743 * To be at this point in the code with a non-zero gw 8744 * means that dst is reachable through a gateway that 8745 * we have never resolved. By changing dst to the gw 8746 * addr we resolve the gateway first. 8747 * When ire_add_then_send() tries to put the IP dg 8748 * to dst, it will reenter ip_newroute() at which 8749 * time we will find the IRE_CACHE for the gw and 8750 * create another IRE_CACHE in case IRE_CACHE above. 8751 */ 8752 if (gw != INADDR_ANY) { 8753 /* 8754 * The source ipif that was determined above was 8755 * relative to the destination address, not the 8756 * gateway's. If src_ipif was not taken out of 8757 * the IRE_IF_RESOLVER entry, we'll need to call 8758 * ipif_select_source() again. 8759 */ 8760 if (src_ipif != ire->ire_ipif) { 8761 ipif_refrele(src_ipif); 8762 src_ipif = ipif_select_source(dst_ill, 8763 gw, zoneid); 8764 if (src_ipif == NULL) { 8765 if (ip_debug > 2) { 8766 pr_addr_dbg( 8767 "ip_newroute: no " 8768 "src for gw %s ", 8769 AF_INET, &gw); 8770 printf("through " 8771 "interface %s\n", 8772 dst_ill->ill_name); 8773 } 8774 goto icmp_err_ret; 8775 } 8776 } 8777 save_dst = dst; 8778 dst = gw; 8779 gw = INADDR_ANY; 8780 } 8781 8782 /* 8783 * We obtain a partial IRE_CACHE which we will pass 8784 * along with the resolver query. When the response 8785 * comes back it will be there ready for us to add. 8786 * The ire_max_frag is atomically set under the 8787 * irebucket lock in ire_add_v[46]. 8788 */ 8789 8790 ire = ire_create_mp( 8791 (uchar_t *)&dst, /* dest address */ 8792 (uchar_t *)&ip_g_all_ones, /* mask */ 8793 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8794 (uchar_t *)&gw, /* gateway address */ 8795 NULL, /* ire_max_frag */ 8796 NULL, /* no src nce */ 8797 dst_ill->ill_rq, /* recv-from queue */ 8798 dst_ill->ill_wq, /* send-to queue */ 8799 IRE_CACHE, 8800 src_ipif, /* Interface ipif */ 8801 save_ire->ire_mask, /* Parent mask */ 8802 0, 8803 save_ire->ire_ihandle, /* Interface handle */ 8804 0, /* flags if any */ 8805 &(save_ire->ire_uinfo), 8806 NULL, 8807 NULL, 8808 ipst); 8809 8810 if (ire == NULL) { 8811 ire_refrele(save_ire); 8812 break; 8813 } 8814 8815 if ((sire != NULL) && 8816 (sire->ire_flags & RTF_MULTIRT)) { 8817 copy_mp = copymsg(first_mp); 8818 if (copy_mp != NULL) 8819 MULTIRT_DEBUG_TAG(copy_mp); 8820 } 8821 8822 ire->ire_marks |= ire_marks; 8823 8824 /* 8825 * Construct message chain for the resolver 8826 * of the form: 8827 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8828 * Packet could contain a IPSEC_OUT mp. 8829 * 8830 * NOTE : ire will be added later when the response 8831 * comes back from ARP. If the response does not 8832 * come back, ARP frees the packet. For this reason, 8833 * we can't REFHOLD the bucket of save_ire to prevent 8834 * deletions. We may not be able to REFRELE the bucket 8835 * if the response never comes back. Thus, before 8836 * adding the ire, ire_add_v4 will make sure that the 8837 * interface route does not get deleted. This is the 8838 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 8839 * where we can always prevent deletions because of 8840 * the synchronous nature of adding IRES i.e 8841 * ire_add_then_send is called after creating the IRE. 8842 */ 8843 ASSERT(ire->ire_mp != NULL); 8844 ire->ire_mp->b_cont = first_mp; 8845 /* Have saved_mp handy, for cleanup if canput fails */ 8846 saved_mp = mp; 8847 mp = copyb(res_mp); 8848 if (mp == NULL) { 8849 /* Prepare for cleanup */ 8850 mp = saved_mp; /* pkt */ 8851 ire_delete(ire); /* ire_mp */ 8852 ire = NULL; 8853 ire_refrele(save_ire); 8854 if (copy_mp != NULL) { 8855 MULTIRT_DEBUG_UNTAG(copy_mp); 8856 freemsg(copy_mp); 8857 copy_mp = NULL; 8858 } 8859 break; 8860 } 8861 linkb(mp, ire->ire_mp); 8862 8863 /* 8864 * Fill in the source and dest addrs for the resolver. 8865 * NOTE: this depends on memory layouts imposed by 8866 * ill_init(). 8867 */ 8868 areq = (areq_t *)mp->b_rptr; 8869 addrp = (ipaddr_t *)((char *)areq + 8870 areq->areq_sender_addr_offset); 8871 if (do_attach_ill) { 8872 /* 8873 * This is bind to no failover case. 8874 * arp packet also must go out on attach_ill. 8875 */ 8876 ASSERT(ipha->ipha_src != NULL); 8877 *addrp = ipha->ipha_src; 8878 } else { 8879 *addrp = save_ire->ire_src_addr; 8880 } 8881 8882 ire_refrele(save_ire); 8883 addrp = (ipaddr_t *)((char *)areq + 8884 areq->areq_target_addr_offset); 8885 *addrp = dst; 8886 /* Up to the resolver. */ 8887 if (canputnext(dst_ill->ill_rq) && 8888 !(dst_ill->ill_arp_closing)) { 8889 putnext(dst_ill->ill_rq, mp); 8890 ire = NULL; 8891 if (copy_mp != NULL) { 8892 /* 8893 * If we found a resolver, we ignore 8894 * any trailing top priority IRE_CACHE 8895 * in the further loops. This ensures 8896 * that we do not omit any resolver. 8897 * IRE_CACHE entries, if any, will be 8898 * processed next time we enter 8899 * ip_newroute(). 8900 */ 8901 multirt_flags &= ~MULTIRT_CACHEGW; 8902 /* 8903 * Search for the next unresolved 8904 * multirt route. 8905 */ 8906 first_mp = copy_mp; 8907 copy_mp = NULL; 8908 /* Prepare the next resolution loop. */ 8909 mp = first_mp; 8910 EXTRACT_PKT_MP(mp, first_mp, 8911 mctl_present); 8912 if (mctl_present) 8913 io = (ipsec_out_t *) 8914 first_mp->b_rptr; 8915 ipha = (ipha_t *)mp->b_rptr; 8916 8917 ASSERT(sire != NULL); 8918 8919 dst = save_dst; 8920 multirt_resolve_next = B_TRUE; 8921 continue; 8922 } 8923 8924 if (sire != NULL) 8925 ire_refrele(sire); 8926 8927 /* 8928 * The response will come back in ip_wput 8929 * with db_type IRE_DB_TYPE. 8930 */ 8931 ipif_refrele(src_ipif); 8932 ill_refrele(dst_ill); 8933 return; 8934 } else { 8935 /* Prepare for cleanup */ 8936 DTRACE_PROBE1(ip__newroute__drop, mblk_t *, 8937 mp); 8938 mp->b_cont = NULL; 8939 freeb(mp); /* areq */ 8940 /* 8941 * this is an ire that is not added to the 8942 * cache. ire_freemblk will handle the release 8943 * of any resources associated with the ire. 8944 */ 8945 ire_delete(ire); /* ire_mp */ 8946 mp = saved_mp; /* pkt */ 8947 ire = NULL; 8948 if (copy_mp != NULL) { 8949 MULTIRT_DEBUG_UNTAG(copy_mp); 8950 freemsg(copy_mp); 8951 copy_mp = NULL; 8952 } 8953 break; 8954 } 8955 default: 8956 break; 8957 } 8958 } while (multirt_resolve_next); 8959 8960 ip1dbg(("ip_newroute: dropped\n")); 8961 /* Did this packet originate externally? */ 8962 if (mp->b_prev) { 8963 mp->b_next = NULL; 8964 mp->b_prev = NULL; 8965 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 8966 } else { 8967 if (dst_ill != NULL) { 8968 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards); 8969 } else { 8970 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 8971 } 8972 } 8973 ASSERT(copy_mp == NULL); 8974 MULTIRT_DEBUG_UNTAG(first_mp); 8975 freemsg(first_mp); 8976 if (ire != NULL) 8977 ire_refrele(ire); 8978 if (sire != NULL) 8979 ire_refrele(sire); 8980 if (src_ipif != NULL) 8981 ipif_refrele(src_ipif); 8982 if (dst_ill != NULL) 8983 ill_refrele(dst_ill); 8984 return; 8985 8986 icmp_err_ret: 8987 ip1dbg(("ip_newroute: no route\n")); 8988 if (src_ipif != NULL) 8989 ipif_refrele(src_ipif); 8990 if (dst_ill != NULL) 8991 ill_refrele(dst_ill); 8992 if (sire != NULL) 8993 ire_refrele(sire); 8994 /* Did this packet originate externally? */ 8995 if (mp->b_prev) { 8996 mp->b_next = NULL; 8997 mp->b_prev = NULL; 8998 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes); 8999 q = WR(q); 9000 } else { 9001 /* 9002 * There is no outgoing ill, so just increment the 9003 * system MIB. 9004 */ 9005 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 9006 /* 9007 * Since ip_wput() isn't close to finished, we fill 9008 * in enough of the header for credible error reporting. 9009 */ 9010 if (ip_hdr_complete(ipha, zoneid, ipst)) { 9011 /* Failed */ 9012 MULTIRT_DEBUG_UNTAG(first_mp); 9013 freemsg(first_mp); 9014 if (ire != NULL) 9015 ire_refrele(ire); 9016 return; 9017 } 9018 } 9019 9020 /* 9021 * At this point we will have ire only if RTF_BLACKHOLE 9022 * or RTF_REJECT flags are set on the IRE. It will not 9023 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9024 */ 9025 if (ire != NULL) { 9026 if (ire->ire_flags & RTF_BLACKHOLE) { 9027 ire_refrele(ire); 9028 MULTIRT_DEBUG_UNTAG(first_mp); 9029 freemsg(first_mp); 9030 return; 9031 } 9032 ire_refrele(ire); 9033 } 9034 if (ip_source_routed(ipha, ipst)) { 9035 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, 9036 zoneid, ipst); 9037 return; 9038 } 9039 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9040 } 9041 9042 ip_opt_info_t zero_info; 9043 9044 /* 9045 * IPv4 - 9046 * ip_newroute_ipif is called by ip_wput_multicast and 9047 * ip_rput_forward_multicast whenever we need to send 9048 * out a packet to a destination address for which we do not have specific 9049 * routing information. It is used when the packet will be sent out 9050 * on a specific interface. It is also called by ip_wput() when IP_XMIT_IF 9051 * socket option is set or icmp error message wants to go out on a particular 9052 * interface for a unicast packet. 9053 * 9054 * In most cases, the destination address is resolved thanks to the ipif 9055 * intrinsic resolver. However, there are some cases where the call to 9056 * ip_newroute_ipif must take into account the potential presence of 9057 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 9058 * that uses the interface. This is specified through flags, 9059 * which can be a combination of: 9060 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 9061 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 9062 * and flags. Additionally, the packet source address has to be set to 9063 * the specified address. The caller is thus expected to set this flag 9064 * if the packet has no specific source address yet. 9065 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 9066 * flag, the resulting ire will inherit the flag. All unresolved routes 9067 * to the destination must be explored in the same call to 9068 * ip_newroute_ipif(). 9069 */ 9070 static void 9071 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 9072 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop) 9073 { 9074 areq_t *areq; 9075 ire_t *ire = NULL; 9076 mblk_t *res_mp; 9077 ipaddr_t *addrp; 9078 mblk_t *first_mp; 9079 ire_t *save_ire = NULL; 9080 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ 9081 ipif_t *src_ipif = NULL; 9082 ushort_t ire_marks = 0; 9083 ill_t *dst_ill = NULL; 9084 boolean_t mctl_present; 9085 ipsec_out_t *io; 9086 ipha_t *ipha; 9087 int ihandle = 0; 9088 mblk_t *saved_mp; 9089 ire_t *fire = NULL; 9090 mblk_t *copy_mp = NULL; 9091 boolean_t multirt_resolve_next; 9092 ipaddr_t ipha_dst; 9093 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 9094 9095 /* 9096 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 9097 * here for uniformity 9098 */ 9099 ipif_refhold(ipif); 9100 9101 /* 9102 * This loop is run only once in most cases. 9103 * We loop to resolve further routes only when the destination 9104 * can be reached through multiple RTF_MULTIRT-flagged ires. 9105 */ 9106 do { 9107 if (dst_ill != NULL) { 9108 ill_refrele(dst_ill); 9109 dst_ill = NULL; 9110 } 9111 if (src_ipif != NULL) { 9112 ipif_refrele(src_ipif); 9113 src_ipif = NULL; 9114 } 9115 multirt_resolve_next = B_FALSE; 9116 9117 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 9118 ipif->ipif_ill->ill_name)); 9119 9120 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 9121 if (mctl_present) 9122 io = (ipsec_out_t *)first_mp->b_rptr; 9123 9124 ipha = (ipha_t *)mp->b_rptr; 9125 9126 /* 9127 * Save the packet destination address, we may need it after 9128 * the packet has been consumed. 9129 */ 9130 ipha_dst = ipha->ipha_dst; 9131 9132 /* 9133 * If the interface is a pt-pt interface we look for an 9134 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 9135 * local_address and the pt-pt destination address. Otherwise 9136 * we just match the local address. 9137 * NOTE: dst could be different than ipha->ipha_dst in case 9138 * of sending igmp multicast packets over a point-to-point 9139 * connection. 9140 * Thus we must be careful enough to check ipha_dst to be a 9141 * multicast address, otherwise it will take xmit_if path for 9142 * multicast packets resulting into kernel stack overflow by 9143 * repeated calls to ip_newroute_ipif from ire_send(). 9144 */ 9145 if (CLASSD(ipha_dst) && 9146 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 9147 goto err_ret; 9148 } 9149 9150 /* 9151 * We check if an IRE_OFFSUBNET for the addr that goes through 9152 * ipif exists. We need it to determine if the RTF_SETSRC and/or 9153 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 9154 * propagate its flags to the new ire. 9155 */ 9156 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 9157 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 9158 ip2dbg(("ip_newroute_ipif: " 9159 "ipif_lookup_multi_ire(" 9160 "ipif %p, dst %08x) = fire %p\n", 9161 (void *)ipif, ntohl(dst), (void *)fire)); 9162 } 9163 9164 if (mctl_present && io->ipsec_out_attach_if) { 9165 attach_ill = ip_grab_attach_ill(NULL, first_mp, 9166 io->ipsec_out_ill_index, B_FALSE, ipst); 9167 9168 /* Failure case frees things for us. */ 9169 if (attach_ill == NULL) { 9170 ipif_refrele(ipif); 9171 if (fire != NULL) 9172 ire_refrele(fire); 9173 return; 9174 } 9175 9176 /* 9177 * Check if we need an ire that will not be 9178 * looked up by anybody else i.e. HIDDEN. 9179 */ 9180 if (ill_is_probeonly(attach_ill)) { 9181 ire_marks = IRE_MARK_HIDDEN; 9182 } 9183 /* 9184 * ip_wput passes the right ipif for IPIF_NOFAILOVER 9185 * case. 9186 */ 9187 dst_ill = ipif->ipif_ill; 9188 /* attach_ill has been refheld by ip_grab_attach_ill */ 9189 ASSERT(dst_ill == attach_ill); 9190 } else { 9191 /* 9192 * If this is set by IP_XMIT_IF, then make sure that 9193 * ipif is pointing to the same ill as the IP_XMIT_IF 9194 * specified ill. 9195 */ 9196 ASSERT((connp == NULL) || 9197 (connp->conn_xmit_if_ill == NULL) || 9198 (connp->conn_xmit_if_ill == ipif->ipif_ill)); 9199 /* 9200 * If the interface belongs to an interface group, 9201 * make sure the next possible interface in the group 9202 * is used. This encourages load spreading among 9203 * peers in an interface group. 9204 * Note: load spreading is disabled for RTF_MULTIRT 9205 * routes. 9206 */ 9207 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9208 (fire->ire_flags & RTF_MULTIRT)) { 9209 /* 9210 * Don't perform outbound load spreading 9211 * in the case of an RTF_MULTIRT issued route, 9212 * we actually typically want to replicate 9213 * outgoing packets through particular 9214 * interfaces. 9215 */ 9216 dst_ill = ipif->ipif_ill; 9217 ill_refhold(dst_ill); 9218 } else { 9219 dst_ill = ip_newroute_get_dst_ill( 9220 ipif->ipif_ill); 9221 } 9222 if (dst_ill == NULL) { 9223 if (ip_debug > 2) { 9224 pr_addr_dbg("ip_newroute_ipif: " 9225 "no dst ill for dst %s\n", 9226 AF_INET, &dst); 9227 } 9228 goto err_ret; 9229 } 9230 } 9231 9232 /* 9233 * Pick a source address preferring non-deprecated ones. 9234 * Unlike ip_newroute, we don't do any source address 9235 * selection here since for multicast it really does not help 9236 * in inbound load spreading as in the unicast case. 9237 */ 9238 if ((flags & RTF_SETSRC) && (fire != NULL) && 9239 (fire->ire_flags & RTF_SETSRC)) { 9240 /* 9241 * As requested by flags, an IRE_OFFSUBNET was looked up 9242 * on that interface. This ire has RTF_SETSRC flag, so 9243 * the source address of the packet must be changed. 9244 * Check that the ipif matching the requested source 9245 * address still exists. 9246 */ 9247 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 9248 zoneid, NULL, NULL, NULL, NULL, ipst); 9249 } 9250 if (((ipif->ipif_flags & IPIF_DEPRECATED) || 9251 (connp != NULL && ipif->ipif_zoneid != zoneid && 9252 ipif->ipif_zoneid != ALL_ZONES)) && 9253 (src_ipif == NULL)) { 9254 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 9255 if (src_ipif == NULL) { 9256 if (ip_debug > 2) { 9257 /* ip1dbg */ 9258 pr_addr_dbg("ip_newroute_ipif: " 9259 "no src for dst %s", 9260 AF_INET, &dst); 9261 } 9262 ip1dbg((" through interface %s\n", 9263 dst_ill->ill_name)); 9264 goto err_ret; 9265 } 9266 ipif_refrele(ipif); 9267 ipif = src_ipif; 9268 ipif_refhold(ipif); 9269 } 9270 if (src_ipif == NULL) { 9271 src_ipif = ipif; 9272 ipif_refhold(src_ipif); 9273 } 9274 9275 /* 9276 * Assign a source address while we have the conn. 9277 * We can't have ip_wput_ire pick a source address when the 9278 * packet returns from arp since conn_unspec_src might be set 9279 * and we loose the conn when going through arp. 9280 */ 9281 if (ipha->ipha_src == INADDR_ANY && 9282 (connp == NULL || !connp->conn_unspec_src)) { 9283 ipha->ipha_src = src_ipif->ipif_src_addr; 9284 } 9285 9286 /* 9287 * In the case of IP_XMIT_IF, it is possible that the 9288 * outgoing interface does not have an interface ire. 9289 */ 9290 if (CLASSD(ipha_dst) && (connp == NULL || 9291 connp->conn_xmit_if_ill == NULL) && 9292 infop->ip_opt_ill_index == 0) { 9293 /* ipif_to_ire returns an held ire */ 9294 ire = ipif_to_ire(ipif); 9295 if (ire == NULL) 9296 goto err_ret; 9297 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 9298 goto err_ret; 9299 /* 9300 * ihandle is needed when the ire is added to 9301 * cache table. 9302 */ 9303 save_ire = ire; 9304 ihandle = save_ire->ire_ihandle; 9305 9306 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 9307 "flags %04x\n", 9308 (void *)ire, (void *)ipif, flags)); 9309 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9310 (fire->ire_flags & RTF_MULTIRT)) { 9311 /* 9312 * As requested by flags, an IRE_OFFSUBNET was 9313 * looked up on that interface. This ire has 9314 * RTF_MULTIRT flag, so the resolution loop will 9315 * be re-entered to resolve additional routes on 9316 * other interfaces. For that purpose, a copy of 9317 * the packet is performed at this point. 9318 */ 9319 fire->ire_last_used_time = lbolt; 9320 copy_mp = copymsg(first_mp); 9321 if (copy_mp) { 9322 MULTIRT_DEBUG_TAG(copy_mp); 9323 } 9324 } 9325 if ((flags & RTF_SETSRC) && (fire != NULL) && 9326 (fire->ire_flags & RTF_SETSRC)) { 9327 /* 9328 * As requested by flags, an IRE_OFFSUBET was 9329 * looked up on that interface. This ire has 9330 * RTF_SETSRC flag, so the source address of the 9331 * packet must be changed. 9332 */ 9333 ipha->ipha_src = fire->ire_src_addr; 9334 } 9335 } else { 9336 ASSERT((connp == NULL) || 9337 (connp->conn_xmit_if_ill != NULL) || 9338 (connp->conn_dontroute) || 9339 infop->ip_opt_ill_index != 0); 9340 /* 9341 * The only ways we can come here are: 9342 * 1) IP_XMIT_IF socket option is set 9343 * 2) SO_DONTROUTE socket option is set 9344 * 3) IP_PKTINFO option is passed in as ancillary data. 9345 * In all cases, the new ire will not be added 9346 * into cache table. 9347 */ 9348 ire_marks |= IRE_MARK_NOADD; 9349 } 9350 9351 switch (ipif->ipif_net_type) { 9352 case IRE_IF_NORESOLVER: { 9353 /* We have what we need to build an IRE_CACHE. */ 9354 9355 if ((dst_ill->ill_phys_addr_length != IP_ADDR_LEN) && 9356 (dst_ill->ill_resolver_mp == NULL)) { 9357 ip1dbg(("ip_newroute_ipif: dst_ill %p " 9358 "for IRE_IF_NORESOLVER ire %p has " 9359 "no ill_resolver_mp\n", 9360 (void *)dst_ill, (void *)ire)); 9361 break; 9362 } 9363 9364 /* 9365 * The new ire inherits the IRE_OFFSUBNET flags 9366 * and source address, if this was requested. 9367 */ 9368 ire = ire_create( 9369 (uchar_t *)&dst, /* dest address */ 9370 (uchar_t *)&ip_g_all_ones, /* mask */ 9371 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9372 NULL, /* gateway address */ 9373 &ipif->ipif_mtu, 9374 NULL, /* no src nce */ 9375 dst_ill->ill_rq, /* recv-from queue */ 9376 dst_ill->ill_wq, /* send-to queue */ 9377 IRE_CACHE, 9378 src_ipif, 9379 (save_ire != NULL ? save_ire->ire_mask : 0), 9380 (fire != NULL) ? /* Parent handle */ 9381 fire->ire_phandle : 0, 9382 ihandle, /* Interface handle */ 9383 (fire != NULL) ? 9384 (fire->ire_flags & 9385 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9386 (save_ire == NULL ? &ire_uinfo_null : 9387 &save_ire->ire_uinfo), 9388 NULL, 9389 NULL, 9390 ipst); 9391 9392 if (ire == NULL) { 9393 if (save_ire != NULL) 9394 ire_refrele(save_ire); 9395 break; 9396 } 9397 9398 ire->ire_marks |= ire_marks; 9399 9400 /* 9401 * If IRE_MARK_NOADD is set then we need to convert 9402 * the max_fragp to a useable value now. This is 9403 * normally done in ire_add_v[46]. We also need to 9404 * associate the ire with an nce (normally would be 9405 * done in ip_wput_nondata()). 9406 * 9407 * Note that IRE_MARK_NOADD packets created here 9408 * do not have a non-null ire_mp pointer. The null 9409 * value of ire_bucket indicates that they were 9410 * never added. 9411 */ 9412 if (ire->ire_marks & IRE_MARK_NOADD) { 9413 uint_t max_frag; 9414 9415 max_frag = *ire->ire_max_fragp; 9416 ire->ire_max_fragp = NULL; 9417 ire->ire_max_frag = max_frag; 9418 9419 if ((ire->ire_nce = ndp_lookup_v4( 9420 ire_to_ill(ire), 9421 (ire->ire_gateway_addr != INADDR_ANY ? 9422 &ire->ire_gateway_addr : &ire->ire_addr), 9423 B_FALSE)) == NULL) { 9424 if (save_ire != NULL) 9425 ire_refrele(save_ire); 9426 break; 9427 } 9428 ASSERT(ire->ire_nce->nce_state == 9429 ND_REACHABLE); 9430 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 9431 } 9432 9433 /* Prevent save_ire from getting deleted */ 9434 if (save_ire != NULL) { 9435 IRB_REFHOLD(save_ire->ire_bucket); 9436 /* Has it been removed already ? */ 9437 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 9438 IRB_REFRELE(save_ire->ire_bucket); 9439 ire_refrele(save_ire); 9440 break; 9441 } 9442 } 9443 9444 ire_add_then_send(q, ire, first_mp); 9445 9446 /* Assert that save_ire is not deleted yet. */ 9447 if (save_ire != NULL) { 9448 ASSERT(save_ire->ire_ptpn != NULL); 9449 IRB_REFRELE(save_ire->ire_bucket); 9450 ire_refrele(save_ire); 9451 save_ire = NULL; 9452 } 9453 if (fire != NULL) { 9454 ire_refrele(fire); 9455 fire = NULL; 9456 } 9457 9458 /* 9459 * the resolution loop is re-entered if this 9460 * was requested through flags and if we 9461 * actually are in a multirouting case. 9462 */ 9463 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9464 boolean_t need_resolve = 9465 ire_multirt_need_resolve(ipha_dst, 9466 MBLK_GETLABEL(copy_mp), ipst); 9467 if (!need_resolve) { 9468 MULTIRT_DEBUG_UNTAG(copy_mp); 9469 freemsg(copy_mp); 9470 copy_mp = NULL; 9471 } else { 9472 /* 9473 * ipif_lookup_group() calls 9474 * ire_lookup_multi() that uses 9475 * ire_ftable_lookup() to find 9476 * an IRE_INTERFACE for the group. 9477 * In the multirt case, 9478 * ire_lookup_multi() then invokes 9479 * ire_multirt_lookup() to find 9480 * the next resolvable ire. 9481 * As a result, we obtain an new 9482 * interface, derived from the 9483 * next ire. 9484 */ 9485 ipif_refrele(ipif); 9486 ipif = ipif_lookup_group(ipha_dst, 9487 zoneid, ipst); 9488 ip2dbg(("ip_newroute_ipif: " 9489 "multirt dst %08x, ipif %p\n", 9490 htonl(dst), (void *)ipif)); 9491 if (ipif != NULL) { 9492 mp = copy_mp; 9493 copy_mp = NULL; 9494 multirt_resolve_next = B_TRUE; 9495 continue; 9496 } else { 9497 freemsg(copy_mp); 9498 } 9499 } 9500 } 9501 if (ipif != NULL) 9502 ipif_refrele(ipif); 9503 ill_refrele(dst_ill); 9504 ipif_refrele(src_ipif); 9505 return; 9506 } 9507 case IRE_IF_RESOLVER: 9508 /* 9509 * We can't build an IRE_CACHE yet, but at least 9510 * we found a resolver that can help. 9511 */ 9512 res_mp = dst_ill->ill_resolver_mp; 9513 if (!OK_RESOLVER_MP(res_mp)) 9514 break; 9515 9516 /* 9517 * We obtain a partial IRE_CACHE which we will pass 9518 * along with the resolver query. When the response 9519 * comes back it will be there ready for us to add. 9520 * The new ire inherits the IRE_OFFSUBNET flags 9521 * and source address, if this was requested. 9522 * The ire_max_frag is atomically set under the 9523 * irebucket lock in ire_add_v[46]. Only in the 9524 * case of IRE_MARK_NOADD, we set it here itself. 9525 */ 9526 ire = ire_create_mp( 9527 (uchar_t *)&dst, /* dest address */ 9528 (uchar_t *)&ip_g_all_ones, /* mask */ 9529 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9530 NULL, /* gateway address */ 9531 (ire_marks & IRE_MARK_NOADD) ? 9532 ipif->ipif_mtu : 0, /* max_frag */ 9533 NULL, /* no src nce */ 9534 dst_ill->ill_rq, /* recv-from queue */ 9535 dst_ill->ill_wq, /* send-to queue */ 9536 IRE_CACHE, 9537 src_ipif, 9538 (save_ire != NULL ? save_ire->ire_mask : 0), 9539 (fire != NULL) ? /* Parent handle */ 9540 fire->ire_phandle : 0, 9541 ihandle, /* Interface handle */ 9542 (fire != NULL) ? /* flags if any */ 9543 (fire->ire_flags & 9544 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9545 (save_ire == NULL ? &ire_uinfo_null : 9546 &save_ire->ire_uinfo), 9547 NULL, 9548 NULL, 9549 ipst); 9550 9551 if (save_ire != NULL) { 9552 ire_refrele(save_ire); 9553 save_ire = NULL; 9554 } 9555 if (ire == NULL) 9556 break; 9557 9558 ire->ire_marks |= ire_marks; 9559 /* 9560 * Construct message chain for the resolver of the 9561 * form: 9562 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 9563 * 9564 * NOTE : ire will be added later when the response 9565 * comes back from ARP. If the response does not 9566 * come back, ARP frees the packet. For this reason, 9567 * we can't REFHOLD the bucket of save_ire to prevent 9568 * deletions. We may not be able to REFRELE the 9569 * bucket if the response never comes back. 9570 * Thus, before adding the ire, ire_add_v4 will make 9571 * sure that the interface route does not get deleted. 9572 * This is the only case unlike ip_newroute_v6, 9573 * ip_newroute_ipif_v6 where we can always prevent 9574 * deletions because ire_add_then_send is called after 9575 * creating the IRE. 9576 * If IRE_MARK_NOADD is set, then ire_add_then_send 9577 * does not add this IRE into the IRE CACHE. 9578 */ 9579 ASSERT(ire->ire_mp != NULL); 9580 ire->ire_mp->b_cont = first_mp; 9581 /* Have saved_mp handy, for cleanup if canput fails */ 9582 saved_mp = mp; 9583 mp = copyb(res_mp); 9584 if (mp == NULL) { 9585 /* Prepare for cleanup */ 9586 mp = saved_mp; /* pkt */ 9587 ire_delete(ire); /* ire_mp */ 9588 ire = NULL; 9589 if (copy_mp != NULL) { 9590 MULTIRT_DEBUG_UNTAG(copy_mp); 9591 freemsg(copy_mp); 9592 copy_mp = NULL; 9593 } 9594 break; 9595 } 9596 linkb(mp, ire->ire_mp); 9597 9598 /* 9599 * Fill in the source and dest addrs for the resolver. 9600 * NOTE: this depends on memory layouts imposed by 9601 * ill_init(). 9602 */ 9603 areq = (areq_t *)mp->b_rptr; 9604 addrp = (ipaddr_t *)((char *)areq + 9605 areq->areq_sender_addr_offset); 9606 *addrp = ire->ire_src_addr; 9607 addrp = (ipaddr_t *)((char *)areq + 9608 areq->areq_target_addr_offset); 9609 *addrp = dst; 9610 /* Up to the resolver. */ 9611 if (canputnext(dst_ill->ill_rq) && 9612 !(dst_ill->ill_arp_closing)) { 9613 putnext(dst_ill->ill_rq, mp); 9614 /* 9615 * The response will come back in ip_wput 9616 * with db_type IRE_DB_TYPE. 9617 */ 9618 } else { 9619 mp->b_cont = NULL; 9620 freeb(mp); /* areq */ 9621 ire_delete(ire); /* ire_mp */ 9622 saved_mp->b_next = NULL; 9623 saved_mp->b_prev = NULL; 9624 freemsg(first_mp); /* pkt */ 9625 ip2dbg(("ip_newroute_ipif: dropped\n")); 9626 } 9627 9628 if (fire != NULL) { 9629 ire_refrele(fire); 9630 fire = NULL; 9631 } 9632 9633 9634 /* 9635 * The resolution loop is re-entered if this was 9636 * requested through flags and we actually are 9637 * in a multirouting case. 9638 */ 9639 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9640 boolean_t need_resolve = 9641 ire_multirt_need_resolve(ipha_dst, 9642 MBLK_GETLABEL(copy_mp), ipst); 9643 if (!need_resolve) { 9644 MULTIRT_DEBUG_UNTAG(copy_mp); 9645 freemsg(copy_mp); 9646 copy_mp = NULL; 9647 } else { 9648 /* 9649 * ipif_lookup_group() calls 9650 * ire_lookup_multi() that uses 9651 * ire_ftable_lookup() to find 9652 * an IRE_INTERFACE for the group. 9653 * In the multirt case, 9654 * ire_lookup_multi() then invokes 9655 * ire_multirt_lookup() to find 9656 * the next resolvable ire. 9657 * As a result, we obtain an new 9658 * interface, derived from the 9659 * next ire. 9660 */ 9661 ipif_refrele(ipif); 9662 ipif = ipif_lookup_group(ipha_dst, 9663 zoneid, ipst); 9664 if (ipif != NULL) { 9665 mp = copy_mp; 9666 copy_mp = NULL; 9667 multirt_resolve_next = B_TRUE; 9668 continue; 9669 } else { 9670 freemsg(copy_mp); 9671 } 9672 } 9673 } 9674 if (ipif != NULL) 9675 ipif_refrele(ipif); 9676 ill_refrele(dst_ill); 9677 ipif_refrele(src_ipif); 9678 return; 9679 default: 9680 break; 9681 } 9682 } while (multirt_resolve_next); 9683 9684 err_ret: 9685 ip2dbg(("ip_newroute_ipif: dropped\n")); 9686 if (fire != NULL) 9687 ire_refrele(fire); 9688 ipif_refrele(ipif); 9689 /* Did this packet originate externally? */ 9690 if (dst_ill != NULL) 9691 ill_refrele(dst_ill); 9692 if (src_ipif != NULL) 9693 ipif_refrele(src_ipif); 9694 if (mp->b_prev || mp->b_next) { 9695 mp->b_next = NULL; 9696 mp->b_prev = NULL; 9697 } else { 9698 /* 9699 * Since ip_wput() isn't close to finished, we fill 9700 * in enough of the header for credible error reporting. 9701 */ 9702 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 9703 /* Failed */ 9704 freemsg(first_mp); 9705 if (ire != NULL) 9706 ire_refrele(ire); 9707 return; 9708 } 9709 } 9710 /* 9711 * At this point we will have ire only if RTF_BLACKHOLE 9712 * or RTF_REJECT flags are set on the IRE. It will not 9713 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9714 */ 9715 if (ire != NULL) { 9716 if (ire->ire_flags & RTF_BLACKHOLE) { 9717 ire_refrele(ire); 9718 freemsg(first_mp); 9719 return; 9720 } 9721 ire_refrele(ire); 9722 } 9723 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9724 } 9725 9726 /* Name/Value Table Lookup Routine */ 9727 char * 9728 ip_nv_lookup(nv_t *nv, int value) 9729 { 9730 if (!nv) 9731 return (NULL); 9732 for (; nv->nv_name; nv++) { 9733 if (nv->nv_value == value) 9734 return (nv->nv_name); 9735 } 9736 return ("unknown"); 9737 } 9738 9739 /* 9740 * This is a module open, i.e. this is a control stream for access 9741 * to a DLPI device. We allocate an ill_t as the instance data in 9742 * this case. 9743 */ 9744 int 9745 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9746 { 9747 ill_t *ill; 9748 int err; 9749 zoneid_t zoneid; 9750 netstack_t *ns; 9751 ip_stack_t *ipst; 9752 9753 /* 9754 * Prevent unprivileged processes from pushing IP so that 9755 * they can't send raw IP. 9756 */ 9757 if (secpolicy_net_rawaccess(credp) != 0) 9758 return (EPERM); 9759 9760 ns = netstack_find_by_cred(credp); 9761 ASSERT(ns != NULL); 9762 ipst = ns->netstack_ip; 9763 ASSERT(ipst != NULL); 9764 9765 /* 9766 * For exclusive stacks we set the zoneid to zero 9767 * to make IP operate as if in the global zone. 9768 */ 9769 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9770 zoneid = GLOBAL_ZONEID; 9771 else 9772 zoneid = crgetzoneid(credp); 9773 9774 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 9775 q->q_ptr = WR(q)->q_ptr = ill; 9776 ill->ill_ipst = ipst; 9777 ill->ill_zoneid = zoneid; 9778 9779 /* 9780 * ill_init initializes the ill fields and then sends down 9781 * down a DL_INFO_REQ after calling qprocson. 9782 */ 9783 err = ill_init(q, ill); 9784 if (err != 0) { 9785 mi_free(ill); 9786 netstack_rele(ipst->ips_netstack); 9787 q->q_ptr = NULL; 9788 WR(q)->q_ptr = NULL; 9789 return (err); 9790 } 9791 9792 /* ill_init initializes the ipsq marking this thread as writer */ 9793 ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE, B_TRUE); 9794 /* Wait for the DL_INFO_ACK */ 9795 mutex_enter(&ill->ill_lock); 9796 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 9797 /* 9798 * Return value of 0 indicates a pending signal. 9799 */ 9800 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 9801 if (err == 0) { 9802 mutex_exit(&ill->ill_lock); 9803 (void) ip_close(q, 0); 9804 return (EINTR); 9805 } 9806 } 9807 mutex_exit(&ill->ill_lock); 9808 9809 /* 9810 * ip_rput_other could have set an error in ill_error on 9811 * receipt of M_ERROR. 9812 */ 9813 9814 err = ill->ill_error; 9815 if (err != 0) { 9816 (void) ip_close(q, 0); 9817 return (err); 9818 } 9819 9820 ill->ill_credp = credp; 9821 crhold(credp); 9822 9823 mutex_enter(&ipst->ips_ip_mi_lock); 9824 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag, 9825 credp); 9826 mutex_exit(&ipst->ips_ip_mi_lock); 9827 if (err) { 9828 (void) ip_close(q, 0); 9829 return (err); 9830 } 9831 return (0); 9832 } 9833 9834 /* IP open routine. */ 9835 int 9836 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9837 { 9838 conn_t *connp; 9839 major_t maj; 9840 zoneid_t zoneid; 9841 netstack_t *ns; 9842 ip_stack_t *ipst; 9843 9844 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 9845 9846 /* Allow reopen. */ 9847 if (q->q_ptr != NULL) 9848 return (0); 9849 9850 if (sflag & MODOPEN) { 9851 /* This is a module open */ 9852 return (ip_modopen(q, devp, flag, sflag, credp)); 9853 } 9854 9855 ns = netstack_find_by_cred(credp); 9856 ASSERT(ns != NULL); 9857 ipst = ns->netstack_ip; 9858 ASSERT(ipst != NULL); 9859 9860 /* 9861 * For exclusive stacks we set the zoneid to zero 9862 * to make IP operate as if in the global zone. 9863 */ 9864 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9865 zoneid = GLOBAL_ZONEID; 9866 else 9867 zoneid = crgetzoneid(credp); 9868 9869 /* 9870 * We are opening as a device. This is an IP client stream, and we 9871 * allocate an conn_t as the instance data. 9872 */ 9873 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack); 9874 9875 /* 9876 * ipcl_conn_create did a netstack_hold. Undo the hold that was 9877 * done by netstack_find_by_cred() 9878 */ 9879 netstack_rele(ipst->ips_netstack); 9880 9881 connp->conn_zoneid = zoneid; 9882 9883 connp->conn_upq = q; 9884 q->q_ptr = WR(q)->q_ptr = connp; 9885 9886 if (flag & SO_SOCKSTR) 9887 connp->conn_flags |= IPCL_SOCKET; 9888 9889 /* Minor tells us which /dev entry was opened */ 9890 if (geteminor(*devp) == IPV6_MINOR) { 9891 connp->conn_flags |= IPCL_ISV6; 9892 connp->conn_af_isv6 = B_TRUE; 9893 ip_setqinfo(q, geteminor(*devp), B_FALSE, ipst); 9894 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9895 } else { 9896 connp->conn_af_isv6 = B_FALSE; 9897 connp->conn_pkt_isv6 = B_FALSE; 9898 } 9899 9900 if ((connp->conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9901 /* CONN_DEC_REF takes care of netstack_rele() */ 9902 q->q_ptr = WR(q)->q_ptr = NULL; 9903 CONN_DEC_REF(connp); 9904 return (EBUSY); 9905 } 9906 9907 maj = getemajor(*devp); 9908 *devp = makedevice(maj, (minor_t)connp->conn_dev); 9909 9910 /* 9911 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 9912 */ 9913 connp->conn_cred = credp; 9914 crhold(connp->conn_cred); 9915 9916 /* 9917 * If the caller has the process-wide flag set, then default to MAC 9918 * exempt mode. This allows read-down to unlabeled hosts. 9919 */ 9920 if (getpflags(NET_MAC_AWARE, credp) != 0) 9921 connp->conn_mac_exempt = B_TRUE; 9922 9923 /* 9924 * This should only happen for ndd, netstat, raw socket or other SCTP 9925 * administrative ops. In these cases, we just need a normal conn_t 9926 * with ulp set to IPPROTO_SCTP. All other ops are trapped and 9927 * an error will be returned. 9928 */ 9929 if (maj != SCTP_MAJ && maj != SCTP6_MAJ) { 9930 connp->conn_rq = q; 9931 connp->conn_wq = WR(q); 9932 } else { 9933 connp->conn_ulp = IPPROTO_SCTP; 9934 connp->conn_rq = connp->conn_wq = NULL; 9935 } 9936 /* Non-zero default values */ 9937 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9938 9939 /* 9940 * Make the conn globally visible to walkers 9941 */ 9942 ASSERT(connp->conn_ref == 1); 9943 mutex_enter(&connp->conn_lock); 9944 connp->conn_state_flags &= ~CONN_INCIPIENT; 9945 mutex_exit(&connp->conn_lock); 9946 9947 qprocson(q); 9948 9949 return (0); 9950 } 9951 9952 /* 9953 * Change q_qinfo based on the value of isv6. 9954 * This can not called on an ill queue. 9955 * Note that there is no race since either q_qinfo works for conn queues - it 9956 * is just an optimization to enter the best wput routine directly. 9957 */ 9958 void 9959 ip_setqinfo(queue_t *q, minor_t minor, boolean_t bump_mib, ip_stack_t *ipst) 9960 { 9961 ASSERT(q->q_flag & QREADR); 9962 ASSERT(WR(q)->q_next == NULL); 9963 ASSERT(q->q_ptr != NULL); 9964 9965 if (minor == IPV6_MINOR) { 9966 if (bump_mib) { 9967 BUMP_MIB(&ipst->ips_ip6_mib, 9968 ipIfStatsOutSwitchIPVersion); 9969 } 9970 q->q_qinfo = &rinit_ipv6; 9971 WR(q)->q_qinfo = &winit_ipv6; 9972 (Q_TO_CONN(q))->conn_pkt_isv6 = B_TRUE; 9973 } else { 9974 if (bump_mib) { 9975 BUMP_MIB(&ipst->ips_ip_mib, 9976 ipIfStatsOutSwitchIPVersion); 9977 } 9978 q->q_qinfo = &iprinit; 9979 WR(q)->q_qinfo = &ipwinit; 9980 (Q_TO_CONN(q))->conn_pkt_isv6 = B_FALSE; 9981 } 9982 9983 } 9984 9985 /* 9986 * See if IPsec needs loading because of the options in mp. 9987 */ 9988 static boolean_t 9989 ipsec_opt_present(mblk_t *mp) 9990 { 9991 uint8_t *optcp, *next_optcp, *opt_endcp; 9992 struct opthdr *opt; 9993 struct T_opthdr *topt; 9994 int opthdr_len; 9995 t_uscalar_t optname, optlevel; 9996 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 9997 ipsec_req_t *ipsr; 9998 9999 /* 10000 * Walk through the mess, and find IP_SEC_OPT. If it's there, 10001 * return TRUE. 10002 */ 10003 10004 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 10005 opt_endcp = optcp + tor->OPT_length; 10006 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10007 opthdr_len = sizeof (struct T_opthdr); 10008 } else { /* O_OPTMGMT_REQ */ 10009 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 10010 opthdr_len = sizeof (struct opthdr); 10011 } 10012 for (; optcp < opt_endcp; optcp = next_optcp) { 10013 if (optcp + opthdr_len > opt_endcp) 10014 return (B_FALSE); /* Not enough option header. */ 10015 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10016 topt = (struct T_opthdr *)optcp; 10017 optlevel = topt->level; 10018 optname = topt->name; 10019 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 10020 } else { 10021 opt = (struct opthdr *)optcp; 10022 optlevel = opt->level; 10023 optname = opt->name; 10024 next_optcp = optcp + opthdr_len + 10025 _TPI_ALIGN_OPT(opt->len); 10026 } 10027 if ((next_optcp < optcp) || /* wraparound pointer space */ 10028 ((next_optcp >= opt_endcp) && /* last option bad len */ 10029 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 10030 return (B_FALSE); /* bad option buffer */ 10031 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 10032 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 10033 /* 10034 * Check to see if it's an all-bypass or all-zeroes 10035 * IPsec request. Don't bother loading IPsec if 10036 * the socket doesn't want to use it. (A good example 10037 * is a bypass request.) 10038 * 10039 * Basically, if any of the non-NEVER bits are set, 10040 * load IPsec. 10041 */ 10042 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 10043 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 10044 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 10045 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 10046 != 0) 10047 return (B_TRUE); 10048 } 10049 } 10050 return (B_FALSE); 10051 } 10052 10053 /* 10054 * If conn is is waiting for ipsec to finish loading, kick it. 10055 */ 10056 /* ARGSUSED */ 10057 static void 10058 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 10059 { 10060 t_scalar_t optreq_prim; 10061 mblk_t *mp; 10062 cred_t *cr; 10063 int err = 0; 10064 10065 /* 10066 * This function is called, after ipsec loading is complete. 10067 * Since IP checks exclusively and atomically (i.e it prevents 10068 * ipsec load from completing until ip_optcom_req completes) 10069 * whether ipsec load is complete, there cannot be a race with IP 10070 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 10071 */ 10072 mutex_enter(&connp->conn_lock); 10073 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 10074 ASSERT(connp->conn_ipsec_opt_mp != NULL); 10075 mp = connp->conn_ipsec_opt_mp; 10076 connp->conn_ipsec_opt_mp = NULL; 10077 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 10078 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); 10079 mutex_exit(&connp->conn_lock); 10080 10081 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 10082 10083 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 10084 if (optreq_prim == T_OPTMGMT_REQ) { 10085 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10086 &ip_opt_obj); 10087 } else { 10088 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 10089 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10090 &ip_opt_obj); 10091 } 10092 if (err != EINPROGRESS) 10093 CONN_OPER_PENDING_DONE(connp); 10094 return; 10095 } 10096 mutex_exit(&connp->conn_lock); 10097 } 10098 10099 /* 10100 * Called from the ipsec_loader thread, outside any perimeter, to tell 10101 * ip qenable any of the queues waiting for the ipsec loader to 10102 * complete. 10103 */ 10104 void 10105 ip_ipsec_load_complete(ipsec_stack_t *ipss) 10106 { 10107 netstack_t *ns = ipss->ipsec_netstack; 10108 10109 ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip); 10110 } 10111 10112 /* 10113 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 10114 * determines the grp on which it has to become exclusive, queues the mp 10115 * and sq draining restarts the optmgmt 10116 */ 10117 static boolean_t 10118 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 10119 { 10120 conn_t *connp = Q_TO_CONN(q); 10121 ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec; 10122 10123 /* 10124 * Take IPsec requests and treat them special. 10125 */ 10126 if (ipsec_opt_present(mp)) { 10127 /* First check if IPsec is loaded. */ 10128 mutex_enter(&ipss->ipsec_loader_lock); 10129 if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) { 10130 mutex_exit(&ipss->ipsec_loader_lock); 10131 return (B_FALSE); 10132 } 10133 mutex_enter(&connp->conn_lock); 10134 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 10135 10136 ASSERT(connp->conn_ipsec_opt_mp == NULL); 10137 connp->conn_ipsec_opt_mp = mp; 10138 mutex_exit(&connp->conn_lock); 10139 mutex_exit(&ipss->ipsec_loader_lock); 10140 10141 ipsec_loader_loadnow(ipss); 10142 return (B_TRUE); 10143 } 10144 return (B_FALSE); 10145 } 10146 10147 /* 10148 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 10149 * all of them are copied to the conn_t. If the req is "zero", the policy is 10150 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 10151 * fields. 10152 * We keep only the latest setting of the policy and thus policy setting 10153 * is not incremental/cumulative. 10154 * 10155 * Requests to set policies with multiple alternative actions will 10156 * go through a different API. 10157 */ 10158 int 10159 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 10160 { 10161 uint_t ah_req = 0; 10162 uint_t esp_req = 0; 10163 uint_t se_req = 0; 10164 ipsec_selkey_t sel; 10165 ipsec_act_t *actp = NULL; 10166 uint_t nact; 10167 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 10168 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 10169 ipsec_policy_root_t *pr; 10170 ipsec_policy_head_t *ph; 10171 int fam; 10172 boolean_t is_pol_reset; 10173 int error = 0; 10174 netstack_t *ns = connp->conn_netstack; 10175 ip_stack_t *ipst = ns->netstack_ip; 10176 ipsec_stack_t *ipss = ns->netstack_ipsec; 10177 10178 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 10179 10180 /* 10181 * The IP_SEC_OPT option does not allow variable length parameters, 10182 * hence a request cannot be NULL. 10183 */ 10184 if (req == NULL) 10185 return (EINVAL); 10186 10187 ah_req = req->ipsr_ah_req; 10188 esp_req = req->ipsr_esp_req; 10189 se_req = req->ipsr_self_encap_req; 10190 10191 /* 10192 * Are we dealing with a request to reset the policy (i.e. 10193 * zero requests). 10194 */ 10195 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 10196 (esp_req & REQ_MASK) == 0 && 10197 (se_req & REQ_MASK) == 0); 10198 10199 if (!is_pol_reset) { 10200 /* 10201 * If we couldn't load IPsec, fail with "protocol 10202 * not supported". 10203 * IPsec may not have been loaded for a request with zero 10204 * policies, so we don't fail in this case. 10205 */ 10206 mutex_enter(&ipss->ipsec_loader_lock); 10207 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 10208 mutex_exit(&ipss->ipsec_loader_lock); 10209 return (EPROTONOSUPPORT); 10210 } 10211 mutex_exit(&ipss->ipsec_loader_lock); 10212 10213 /* 10214 * Test for valid requests. Invalid algorithms 10215 * need to be tested by IPsec code because new 10216 * algorithms can be added dynamically. 10217 */ 10218 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10219 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10220 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 10221 return (EINVAL); 10222 } 10223 10224 /* 10225 * Only privileged users can issue these 10226 * requests. 10227 */ 10228 if (((ah_req & IPSEC_PREF_NEVER) || 10229 (esp_req & IPSEC_PREF_NEVER) || 10230 (se_req & IPSEC_PREF_NEVER)) && 10231 secpolicy_ip_config(cr, B_FALSE) != 0) { 10232 return (EPERM); 10233 } 10234 10235 /* 10236 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 10237 * are mutually exclusive. 10238 */ 10239 if (((ah_req & REQ_MASK) == REQ_MASK) || 10240 ((esp_req & REQ_MASK) == REQ_MASK) || 10241 ((se_req & REQ_MASK) == REQ_MASK)) { 10242 /* Both of them are set */ 10243 return (EINVAL); 10244 } 10245 } 10246 10247 mutex_enter(&connp->conn_lock); 10248 10249 /* 10250 * If we have already cached policies in ip_bind_connected*(), don't 10251 * let them change now. We cache policies for connections 10252 * whose src,dst [addr, port] is known. 10253 */ 10254 if (connp->conn_policy_cached) { 10255 mutex_exit(&connp->conn_lock); 10256 return (EINVAL); 10257 } 10258 10259 /* 10260 * We have a zero policies, reset the connection policy if already 10261 * set. This will cause the connection to inherit the 10262 * global policy, if any. 10263 */ 10264 if (is_pol_reset) { 10265 if (connp->conn_policy != NULL) { 10266 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack); 10267 connp->conn_policy = NULL; 10268 } 10269 connp->conn_flags &= ~IPCL_CHECK_POLICY; 10270 connp->conn_in_enforce_policy = B_FALSE; 10271 connp->conn_out_enforce_policy = B_FALSE; 10272 mutex_exit(&connp->conn_lock); 10273 return (0); 10274 } 10275 10276 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy, 10277 ipst->ips_netstack); 10278 if (ph == NULL) 10279 goto enomem; 10280 10281 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack); 10282 if (actp == NULL) 10283 goto enomem; 10284 10285 /* 10286 * Always allocate IPv4 policy entries, since they can also 10287 * apply to ipv6 sockets being used in ipv4-compat mode. 10288 */ 10289 bzero(&sel, sizeof (sel)); 10290 sel.ipsl_valid = IPSL_IPV4; 10291 10292 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10293 ipst->ips_netstack); 10294 if (pin4 == NULL) 10295 goto enomem; 10296 10297 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10298 ipst->ips_netstack); 10299 if (pout4 == NULL) 10300 goto enomem; 10301 10302 if (connp->conn_pkt_isv6) { 10303 /* 10304 * We're looking at a v6 socket, also allocate the 10305 * v6-specific entries... 10306 */ 10307 sel.ipsl_valid = IPSL_IPV6; 10308 pin6 = ipsec_policy_create(&sel, actp, nact, 10309 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10310 if (pin6 == NULL) 10311 goto enomem; 10312 10313 pout6 = ipsec_policy_create(&sel, actp, nact, 10314 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10315 if (pout6 == NULL) 10316 goto enomem; 10317 10318 /* 10319 * .. and file them away in the right place. 10320 */ 10321 fam = IPSEC_AF_V6; 10322 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10323 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 10324 ipsec_insert_always(&ph->iph_rulebyid, pin6); 10325 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10326 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 10327 ipsec_insert_always(&ph->iph_rulebyid, pout6); 10328 } 10329 10330 ipsec_actvec_free(actp, nact); 10331 10332 /* 10333 * File the v4 policies. 10334 */ 10335 fam = IPSEC_AF_V4; 10336 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10337 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 10338 ipsec_insert_always(&ph->iph_rulebyid, pin4); 10339 10340 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10341 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 10342 ipsec_insert_always(&ph->iph_rulebyid, pout4); 10343 10344 /* 10345 * If the requests need security, set enforce_policy. 10346 * If the requests are IPSEC_PREF_NEVER, one should 10347 * still set conn_out_enforce_policy so that an ipsec_out 10348 * gets attached in ip_wput. This is needed so that 10349 * for connections that we don't cache policy in ip_bind, 10350 * if global policy matches in ip_wput_attach_policy, we 10351 * don't wrongly inherit global policy. Similarly, we need 10352 * to set conn_in_enforce_policy also so that we don't verify 10353 * policy wrongly. 10354 */ 10355 if ((ah_req & REQ_MASK) != 0 || 10356 (esp_req & REQ_MASK) != 0 || 10357 (se_req & REQ_MASK) != 0) { 10358 connp->conn_in_enforce_policy = B_TRUE; 10359 connp->conn_out_enforce_policy = B_TRUE; 10360 connp->conn_flags |= IPCL_CHECK_POLICY; 10361 } 10362 10363 mutex_exit(&connp->conn_lock); 10364 return (error); 10365 #undef REQ_MASK 10366 10367 /* 10368 * Common memory-allocation-failure exit path. 10369 */ 10370 enomem: 10371 mutex_exit(&connp->conn_lock); 10372 if (actp != NULL) 10373 ipsec_actvec_free(actp, nact); 10374 if (pin4 != NULL) 10375 IPPOL_REFRELE(pin4, ipst->ips_netstack); 10376 if (pout4 != NULL) 10377 IPPOL_REFRELE(pout4, ipst->ips_netstack); 10378 if (pin6 != NULL) 10379 IPPOL_REFRELE(pin6, ipst->ips_netstack); 10380 if (pout6 != NULL) 10381 IPPOL_REFRELE(pout6, ipst->ips_netstack); 10382 return (ENOMEM); 10383 } 10384 10385 /* 10386 * Only for options that pass in an IP addr. Currently only V4 options 10387 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 10388 * So this function assumes level is IPPROTO_IP 10389 */ 10390 int 10391 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 10392 mblk_t *first_mp) 10393 { 10394 ipif_t *ipif = NULL; 10395 int error; 10396 ill_t *ill; 10397 int zoneid; 10398 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10399 10400 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 10401 10402 if (addr != INADDR_ANY || checkonly) { 10403 ASSERT(connp != NULL); 10404 zoneid = IPCL_ZONEID(connp); 10405 if (option == IP_NEXTHOP) { 10406 ipif = ipif_lookup_onlink_addr(addr, 10407 connp->conn_zoneid, ipst); 10408 } else { 10409 ipif = ipif_lookup_addr(addr, NULL, zoneid, 10410 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 10411 &error, ipst); 10412 } 10413 if (ipif == NULL) { 10414 if (error == EINPROGRESS) 10415 return (error); 10416 else if ((option == IP_MULTICAST_IF) || 10417 (option == IP_NEXTHOP)) 10418 return (EHOSTUNREACH); 10419 else 10420 return (EINVAL); 10421 } else if (checkonly) { 10422 if (option == IP_MULTICAST_IF) { 10423 ill = ipif->ipif_ill; 10424 /* not supported by the virtual network iface */ 10425 if (IS_VNI(ill)) { 10426 ipif_refrele(ipif); 10427 return (EINVAL); 10428 } 10429 } 10430 ipif_refrele(ipif); 10431 return (0); 10432 } 10433 ill = ipif->ipif_ill; 10434 mutex_enter(&connp->conn_lock); 10435 mutex_enter(&ill->ill_lock); 10436 if ((ill->ill_state_flags & ILL_CONDEMNED) || 10437 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 10438 mutex_exit(&ill->ill_lock); 10439 mutex_exit(&connp->conn_lock); 10440 ipif_refrele(ipif); 10441 return (option == IP_MULTICAST_IF ? 10442 EHOSTUNREACH : EINVAL); 10443 } 10444 } else { 10445 mutex_enter(&connp->conn_lock); 10446 } 10447 10448 /* None of the options below are supported on the VNI */ 10449 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 10450 mutex_exit(&ill->ill_lock); 10451 mutex_exit(&connp->conn_lock); 10452 ipif_refrele(ipif); 10453 return (EINVAL); 10454 } 10455 10456 switch (option) { 10457 case IP_DONTFAILOVER_IF: 10458 /* 10459 * This option is used by in.mpathd to ensure 10460 * that IPMP probe packets only go out on the 10461 * test interfaces. in.mpathd sets this option 10462 * on the non-failover interfaces. 10463 * For backward compatibility, this option 10464 * implicitly sets IP_MULTICAST_IF, as used 10465 * be done in bind(), so that ip_wput gets 10466 * this ipif to send mcast packets. 10467 */ 10468 if (ipif != NULL) { 10469 ASSERT(addr != INADDR_ANY); 10470 connp->conn_nofailover_ill = ipif->ipif_ill; 10471 connp->conn_multicast_ipif = ipif; 10472 } else { 10473 ASSERT(addr == INADDR_ANY); 10474 connp->conn_nofailover_ill = NULL; 10475 connp->conn_multicast_ipif = NULL; 10476 } 10477 break; 10478 10479 case IP_MULTICAST_IF: 10480 connp->conn_multicast_ipif = ipif; 10481 break; 10482 case IP_NEXTHOP: 10483 connp->conn_nexthop_v4 = addr; 10484 connp->conn_nexthop_set = B_TRUE; 10485 break; 10486 } 10487 10488 if (ipif != NULL) { 10489 mutex_exit(&ill->ill_lock); 10490 mutex_exit(&connp->conn_lock); 10491 ipif_refrele(ipif); 10492 return (0); 10493 } 10494 mutex_exit(&connp->conn_lock); 10495 /* We succeded in cleared the option */ 10496 return (0); 10497 } 10498 10499 /* 10500 * For options that pass in an ifindex specifying the ill. V6 options always 10501 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 10502 */ 10503 int 10504 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 10505 int level, int option, mblk_t *first_mp) 10506 { 10507 ill_t *ill = NULL; 10508 int error = 0; 10509 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10510 10511 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 10512 if (ifindex != 0) { 10513 ASSERT(connp != NULL); 10514 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 10515 first_mp, ip_restart_optmgmt, &error, ipst); 10516 if (ill != NULL) { 10517 if (checkonly) { 10518 /* not supported by the virtual network iface */ 10519 if (IS_VNI(ill)) { 10520 ill_refrele(ill); 10521 return (EINVAL); 10522 } 10523 ill_refrele(ill); 10524 return (0); 10525 } 10526 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 10527 0, NULL)) { 10528 ill_refrele(ill); 10529 ill = NULL; 10530 mutex_enter(&connp->conn_lock); 10531 goto setit; 10532 } 10533 mutex_enter(&connp->conn_lock); 10534 mutex_enter(&ill->ill_lock); 10535 if (ill->ill_state_flags & ILL_CONDEMNED) { 10536 mutex_exit(&ill->ill_lock); 10537 mutex_exit(&connp->conn_lock); 10538 ill_refrele(ill); 10539 ill = NULL; 10540 mutex_enter(&connp->conn_lock); 10541 } 10542 goto setit; 10543 } else if (error == EINPROGRESS) { 10544 return (error); 10545 } else { 10546 error = 0; 10547 } 10548 } 10549 mutex_enter(&connp->conn_lock); 10550 setit: 10551 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 10552 10553 /* 10554 * The options below assume that the ILL (if any) transmits and/or 10555 * receives traffic. Neither of which is true for the virtual network 10556 * interface, so fail setting these on a VNI. 10557 */ 10558 if (IS_VNI(ill)) { 10559 ASSERT(ill != NULL); 10560 mutex_exit(&ill->ill_lock); 10561 mutex_exit(&connp->conn_lock); 10562 ill_refrele(ill); 10563 return (EINVAL); 10564 } 10565 10566 if (level == IPPROTO_IP) { 10567 switch (option) { 10568 case IP_BOUND_IF: 10569 connp->conn_incoming_ill = ill; 10570 connp->conn_outgoing_ill = ill; 10571 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10572 0 : ifindex; 10573 break; 10574 10575 case IP_XMIT_IF: 10576 /* 10577 * Similar to IP_BOUND_IF, but this only 10578 * determines the outgoing interface for 10579 * unicast packets. Also no IRE_CACHE entry 10580 * is added for the destination of the 10581 * outgoing packets. 10582 */ 10583 connp->conn_xmit_if_ill = ill; 10584 connp->conn_orig_xmit_ifindex = (ill == NULL) ? 10585 0 : ifindex; 10586 break; 10587 10588 case IP_MULTICAST_IF: 10589 /* 10590 * This option is an internal special. The socket 10591 * level IP_MULTICAST_IF specifies an 'ipaddr' and 10592 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 10593 * specifies an ifindex and we try first on V6 ill's. 10594 * If we don't find one, we they try using on v4 ill's 10595 * intenally and we come here. 10596 */ 10597 if (!checkonly && ill != NULL) { 10598 ipif_t *ipif; 10599 ipif = ill->ill_ipif; 10600 10601 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 10602 mutex_exit(&ill->ill_lock); 10603 mutex_exit(&connp->conn_lock); 10604 ill_refrele(ill); 10605 ill = NULL; 10606 mutex_enter(&connp->conn_lock); 10607 } else { 10608 connp->conn_multicast_ipif = ipif; 10609 } 10610 } 10611 break; 10612 } 10613 } else { 10614 switch (option) { 10615 case IPV6_BOUND_IF: 10616 connp->conn_incoming_ill = ill; 10617 connp->conn_outgoing_ill = ill; 10618 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10619 0 : ifindex; 10620 break; 10621 10622 case IPV6_BOUND_PIF: 10623 /* 10624 * Limit all transmit to this ill. 10625 * Unlike IPV6_BOUND_IF, using this option 10626 * prevents load spreading and failover from 10627 * happening when the interface is part of the 10628 * group. That's why we don't need to remember 10629 * the ifindex in orig_bound_ifindex as in 10630 * IPV6_BOUND_IF. 10631 */ 10632 connp->conn_outgoing_pill = ill; 10633 break; 10634 10635 case IPV6_DONTFAILOVER_IF: 10636 /* 10637 * This option is used by in.mpathd to ensure 10638 * that IPMP probe packets only go out on the 10639 * test interfaces. in.mpathd sets this option 10640 * on the non-failover interfaces. 10641 */ 10642 connp->conn_nofailover_ill = ill; 10643 /* 10644 * For backward compatibility, this option 10645 * implicitly sets ip_multicast_ill as used in 10646 * IP_MULTICAST_IF so that ip_wput gets 10647 * this ipif to send mcast packets. 10648 */ 10649 connp->conn_multicast_ill = ill; 10650 connp->conn_orig_multicast_ifindex = (ill == NULL) ? 10651 0 : ifindex; 10652 break; 10653 10654 case IPV6_MULTICAST_IF: 10655 /* 10656 * Set conn_multicast_ill to be the IPv6 ill. 10657 * Set conn_multicast_ipif to be an IPv4 ipif 10658 * for ifindex to make IPv4 mapped addresses 10659 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 10660 * Even if no IPv6 ill exists for the ifindex 10661 * we need to check for an IPv4 ifindex in order 10662 * for this to work with mapped addresses. In that 10663 * case only set conn_multicast_ipif. 10664 */ 10665 if (!checkonly) { 10666 if (ifindex == 0) { 10667 connp->conn_multicast_ill = NULL; 10668 connp->conn_orig_multicast_ifindex = 0; 10669 connp->conn_multicast_ipif = NULL; 10670 } else if (ill != NULL) { 10671 connp->conn_multicast_ill = ill; 10672 connp->conn_orig_multicast_ifindex = 10673 ifindex; 10674 } 10675 } 10676 break; 10677 } 10678 } 10679 10680 if (ill != NULL) { 10681 mutex_exit(&ill->ill_lock); 10682 mutex_exit(&connp->conn_lock); 10683 ill_refrele(ill); 10684 return (0); 10685 } 10686 mutex_exit(&connp->conn_lock); 10687 /* 10688 * We succeeded in clearing the option (ifindex == 0) or failed to 10689 * locate the ill and could not set the option (ifindex != 0) 10690 */ 10691 return (ifindex == 0 ? 0 : EINVAL); 10692 } 10693 10694 /* This routine sets socket options. */ 10695 /* ARGSUSED */ 10696 int 10697 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10698 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10699 void *dummy, cred_t *cr, mblk_t *first_mp) 10700 { 10701 int *i1 = (int *)invalp; 10702 conn_t *connp = Q_TO_CONN(q); 10703 int error = 0; 10704 boolean_t checkonly; 10705 ire_t *ire; 10706 boolean_t found; 10707 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10708 10709 switch (optset_context) { 10710 10711 case SETFN_OPTCOM_CHECKONLY: 10712 checkonly = B_TRUE; 10713 /* 10714 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10715 * inlen != 0 implies value supplied and 10716 * we have to "pretend" to set it. 10717 * inlen == 0 implies that there is no 10718 * value part in T_CHECK request and just validation 10719 * done elsewhere should be enough, we just return here. 10720 */ 10721 if (inlen == 0) { 10722 *outlenp = 0; 10723 return (0); 10724 } 10725 break; 10726 case SETFN_OPTCOM_NEGOTIATE: 10727 case SETFN_UD_NEGOTIATE: 10728 case SETFN_CONN_NEGOTIATE: 10729 checkonly = B_FALSE; 10730 break; 10731 default: 10732 /* 10733 * We should never get here 10734 */ 10735 *outlenp = 0; 10736 return (EINVAL); 10737 } 10738 10739 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10740 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10741 10742 /* 10743 * For fixed length options, no sanity check 10744 * of passed in length is done. It is assumed *_optcom_req() 10745 * routines do the right thing. 10746 */ 10747 10748 switch (level) { 10749 case SOL_SOCKET: 10750 /* 10751 * conn_lock protects the bitfields, and is used to 10752 * set the fields atomically. 10753 */ 10754 switch (name) { 10755 case SO_BROADCAST: 10756 if (!checkonly) { 10757 /* TODO: use value someplace? */ 10758 mutex_enter(&connp->conn_lock); 10759 connp->conn_broadcast = *i1 ? 1 : 0; 10760 mutex_exit(&connp->conn_lock); 10761 } 10762 break; /* goto sizeof (int) option return */ 10763 case SO_USELOOPBACK: 10764 if (!checkonly) { 10765 /* TODO: use value someplace? */ 10766 mutex_enter(&connp->conn_lock); 10767 connp->conn_loopback = *i1 ? 1 : 0; 10768 mutex_exit(&connp->conn_lock); 10769 } 10770 break; /* goto sizeof (int) option return */ 10771 case SO_DONTROUTE: 10772 if (!checkonly) { 10773 mutex_enter(&connp->conn_lock); 10774 connp->conn_dontroute = *i1 ? 1 : 0; 10775 mutex_exit(&connp->conn_lock); 10776 } 10777 break; /* goto sizeof (int) option return */ 10778 case SO_REUSEADDR: 10779 if (!checkonly) { 10780 mutex_enter(&connp->conn_lock); 10781 connp->conn_reuseaddr = *i1 ? 1 : 0; 10782 mutex_exit(&connp->conn_lock); 10783 } 10784 break; /* goto sizeof (int) option return */ 10785 case SO_PROTOTYPE: 10786 if (!checkonly) { 10787 mutex_enter(&connp->conn_lock); 10788 connp->conn_proto = *i1; 10789 mutex_exit(&connp->conn_lock); 10790 } 10791 break; /* goto sizeof (int) option return */ 10792 case SO_ALLZONES: 10793 if (!checkonly) { 10794 mutex_enter(&connp->conn_lock); 10795 if (IPCL_IS_BOUND(connp)) { 10796 mutex_exit(&connp->conn_lock); 10797 return (EINVAL); 10798 } 10799 connp->conn_allzones = *i1 != 0 ? 1 : 0; 10800 mutex_exit(&connp->conn_lock); 10801 } 10802 break; /* goto sizeof (int) option return */ 10803 case SO_ANON_MLP: 10804 if (!checkonly) { 10805 mutex_enter(&connp->conn_lock); 10806 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; 10807 mutex_exit(&connp->conn_lock); 10808 } 10809 break; /* goto sizeof (int) option return */ 10810 case SO_MAC_EXEMPT: 10811 if (secpolicy_net_mac_aware(cr) != 0 || 10812 IPCL_IS_BOUND(connp)) 10813 return (EACCES); 10814 if (!checkonly) { 10815 mutex_enter(&connp->conn_lock); 10816 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; 10817 mutex_exit(&connp->conn_lock); 10818 } 10819 break; /* goto sizeof (int) option return */ 10820 default: 10821 /* 10822 * "soft" error (negative) 10823 * option not handled at this level 10824 * Note: Do not modify *outlenp 10825 */ 10826 return (-EINVAL); 10827 } 10828 break; 10829 case IPPROTO_IP: 10830 switch (name) { 10831 case IP_NEXTHOP: 10832 if (secpolicy_ip_config(cr, B_FALSE) != 0) 10833 return (EPERM); 10834 /* FALLTHRU */ 10835 case IP_MULTICAST_IF: 10836 case IP_DONTFAILOVER_IF: { 10837 ipaddr_t addr = *i1; 10838 10839 error = ip_opt_set_ipif(connp, addr, checkonly, name, 10840 first_mp); 10841 if (error != 0) 10842 return (error); 10843 break; /* goto sizeof (int) option return */ 10844 } 10845 10846 case IP_MULTICAST_TTL: 10847 /* Recorded in transport above IP */ 10848 *outvalp = *invalp; 10849 *outlenp = sizeof (uchar_t); 10850 return (0); 10851 case IP_MULTICAST_LOOP: 10852 if (!checkonly) { 10853 mutex_enter(&connp->conn_lock); 10854 connp->conn_multicast_loop = *invalp ? 1 : 0; 10855 mutex_exit(&connp->conn_lock); 10856 } 10857 *outvalp = *invalp; 10858 *outlenp = sizeof (uchar_t); 10859 return (0); 10860 case IP_ADD_MEMBERSHIP: 10861 case MCAST_JOIN_GROUP: 10862 case IP_DROP_MEMBERSHIP: 10863 case MCAST_LEAVE_GROUP: { 10864 struct ip_mreq *mreqp; 10865 struct group_req *greqp; 10866 ire_t *ire; 10867 boolean_t done = B_FALSE; 10868 ipaddr_t group, ifaddr; 10869 struct sockaddr_in *sin; 10870 uint32_t *ifindexp; 10871 boolean_t mcast_opt = B_TRUE; 10872 mcast_record_t fmode; 10873 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10874 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10875 10876 switch (name) { 10877 case IP_ADD_MEMBERSHIP: 10878 mcast_opt = B_FALSE; 10879 /* FALLTHRU */ 10880 case MCAST_JOIN_GROUP: 10881 fmode = MODE_IS_EXCLUDE; 10882 optfn = ip_opt_add_group; 10883 break; 10884 10885 case IP_DROP_MEMBERSHIP: 10886 mcast_opt = B_FALSE; 10887 /* FALLTHRU */ 10888 case MCAST_LEAVE_GROUP: 10889 fmode = MODE_IS_INCLUDE; 10890 optfn = ip_opt_delete_group; 10891 break; 10892 } 10893 10894 if (mcast_opt) { 10895 greqp = (struct group_req *)i1; 10896 sin = (struct sockaddr_in *)&greqp->gr_group; 10897 if (sin->sin_family != AF_INET) { 10898 *outlenp = 0; 10899 return (ENOPROTOOPT); 10900 } 10901 group = (ipaddr_t)sin->sin_addr.s_addr; 10902 ifaddr = INADDR_ANY; 10903 ifindexp = &greqp->gr_interface; 10904 } else { 10905 mreqp = (struct ip_mreq *)i1; 10906 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 10907 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 10908 ifindexp = NULL; 10909 } 10910 10911 /* 10912 * In the multirouting case, we need to replicate 10913 * the request on all interfaces that will take part 10914 * in replication. We do so because multirouting is 10915 * reflective, thus we will probably receive multi- 10916 * casts on those interfaces. 10917 * The ip_multirt_apply_membership() succeeds if the 10918 * operation succeeds on at least one interface. 10919 */ 10920 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 10921 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10922 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 10923 if (ire != NULL) { 10924 if (ire->ire_flags & RTF_MULTIRT) { 10925 error = ip_multirt_apply_membership( 10926 optfn, ire, connp, checkonly, group, 10927 fmode, INADDR_ANY, first_mp); 10928 done = B_TRUE; 10929 } 10930 ire_refrele(ire); 10931 } 10932 if (!done) { 10933 error = optfn(connp, checkonly, group, ifaddr, 10934 ifindexp, fmode, INADDR_ANY, first_mp); 10935 } 10936 if (error) { 10937 /* 10938 * EINPROGRESS is a soft error, needs retry 10939 * so don't make *outlenp zero. 10940 */ 10941 if (error != EINPROGRESS) 10942 *outlenp = 0; 10943 return (error); 10944 } 10945 /* OK return - copy input buffer into output buffer */ 10946 if (invalp != outvalp) { 10947 /* don't trust bcopy for identical src/dst */ 10948 bcopy(invalp, outvalp, inlen); 10949 } 10950 *outlenp = inlen; 10951 return (0); 10952 } 10953 case IP_BLOCK_SOURCE: 10954 case IP_UNBLOCK_SOURCE: 10955 case IP_ADD_SOURCE_MEMBERSHIP: 10956 case IP_DROP_SOURCE_MEMBERSHIP: 10957 case MCAST_BLOCK_SOURCE: 10958 case MCAST_UNBLOCK_SOURCE: 10959 case MCAST_JOIN_SOURCE_GROUP: 10960 case MCAST_LEAVE_SOURCE_GROUP: { 10961 struct ip_mreq_source *imreqp; 10962 struct group_source_req *gsreqp; 10963 in_addr_t grp, src, ifaddr = INADDR_ANY; 10964 uint32_t ifindex = 0; 10965 mcast_record_t fmode; 10966 struct sockaddr_in *sin; 10967 ire_t *ire; 10968 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 10969 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10970 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10971 10972 switch (name) { 10973 case IP_BLOCK_SOURCE: 10974 mcast_opt = B_FALSE; 10975 /* FALLTHRU */ 10976 case MCAST_BLOCK_SOURCE: 10977 fmode = MODE_IS_EXCLUDE; 10978 optfn = ip_opt_add_group; 10979 break; 10980 10981 case IP_UNBLOCK_SOURCE: 10982 mcast_opt = B_FALSE; 10983 /* FALLTHRU */ 10984 case MCAST_UNBLOCK_SOURCE: 10985 fmode = MODE_IS_EXCLUDE; 10986 optfn = ip_opt_delete_group; 10987 break; 10988 10989 case IP_ADD_SOURCE_MEMBERSHIP: 10990 mcast_opt = B_FALSE; 10991 /* FALLTHRU */ 10992 case MCAST_JOIN_SOURCE_GROUP: 10993 fmode = MODE_IS_INCLUDE; 10994 optfn = ip_opt_add_group; 10995 break; 10996 10997 case IP_DROP_SOURCE_MEMBERSHIP: 10998 mcast_opt = B_FALSE; 10999 /* FALLTHRU */ 11000 case MCAST_LEAVE_SOURCE_GROUP: 11001 fmode = MODE_IS_INCLUDE; 11002 optfn = ip_opt_delete_group; 11003 break; 11004 } 11005 11006 if (mcast_opt) { 11007 gsreqp = (struct group_source_req *)i1; 11008 if (gsreqp->gsr_group.ss_family != AF_INET) { 11009 *outlenp = 0; 11010 return (ENOPROTOOPT); 11011 } 11012 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 11013 grp = (ipaddr_t)sin->sin_addr.s_addr; 11014 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 11015 src = (ipaddr_t)sin->sin_addr.s_addr; 11016 ifindex = gsreqp->gsr_interface; 11017 } else { 11018 imreqp = (struct ip_mreq_source *)i1; 11019 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 11020 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 11021 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 11022 } 11023 11024 /* 11025 * In the multirouting case, we need to replicate 11026 * the request as noted in the mcast cases above. 11027 */ 11028 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 11029 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11030 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11031 if (ire != NULL) { 11032 if (ire->ire_flags & RTF_MULTIRT) { 11033 error = ip_multirt_apply_membership( 11034 optfn, ire, connp, checkonly, grp, 11035 fmode, src, first_mp); 11036 done = B_TRUE; 11037 } 11038 ire_refrele(ire); 11039 } 11040 if (!done) { 11041 error = optfn(connp, checkonly, grp, ifaddr, 11042 &ifindex, fmode, src, first_mp); 11043 } 11044 if (error != 0) { 11045 /* 11046 * EINPROGRESS is a soft error, needs retry 11047 * so don't make *outlenp zero. 11048 */ 11049 if (error != EINPROGRESS) 11050 *outlenp = 0; 11051 return (error); 11052 } 11053 /* OK return - copy input buffer into output buffer */ 11054 if (invalp != outvalp) { 11055 bcopy(invalp, outvalp, inlen); 11056 } 11057 *outlenp = inlen; 11058 return (0); 11059 } 11060 case IP_SEC_OPT: 11061 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11062 if (error != 0) { 11063 *outlenp = 0; 11064 return (error); 11065 } 11066 break; 11067 case IP_HDRINCL: 11068 case IP_OPTIONS: 11069 case T_IP_OPTIONS: 11070 case IP_TOS: 11071 case T_IP_TOS: 11072 case IP_TTL: 11073 case IP_RECVDSTADDR: 11074 case IP_RECVOPTS: 11075 /* OK return - copy input buffer into output buffer */ 11076 if (invalp != outvalp) { 11077 /* don't trust bcopy for identical src/dst */ 11078 bcopy(invalp, outvalp, inlen); 11079 } 11080 *outlenp = inlen; 11081 return (0); 11082 case IP_RECVIF: 11083 /* Retrieve the inbound interface index */ 11084 if (!checkonly) { 11085 mutex_enter(&connp->conn_lock); 11086 connp->conn_recvif = *i1 ? 1 : 0; 11087 mutex_exit(&connp->conn_lock); 11088 } 11089 break; /* goto sizeof (int) option return */ 11090 case IP_RECVPKTINFO: 11091 if (!checkonly) { 11092 mutex_enter(&connp->conn_lock); 11093 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11094 mutex_exit(&connp->conn_lock); 11095 } 11096 break; /* goto sizeof (int) option return */ 11097 case IP_RECVSLLA: 11098 /* Retrieve the source link layer address */ 11099 if (!checkonly) { 11100 mutex_enter(&connp->conn_lock); 11101 connp->conn_recvslla = *i1 ? 1 : 0; 11102 mutex_exit(&connp->conn_lock); 11103 } 11104 break; /* goto sizeof (int) option return */ 11105 case MRT_INIT: 11106 case MRT_DONE: 11107 case MRT_ADD_VIF: 11108 case MRT_DEL_VIF: 11109 case MRT_ADD_MFC: 11110 case MRT_DEL_MFC: 11111 case MRT_ASSERT: 11112 if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) { 11113 *outlenp = 0; 11114 return (error); 11115 } 11116 error = ip_mrouter_set((int)name, q, checkonly, 11117 (uchar_t *)invalp, inlen, first_mp); 11118 if (error) { 11119 *outlenp = 0; 11120 return (error); 11121 } 11122 /* OK return - copy input buffer into output buffer */ 11123 if (invalp != outvalp) { 11124 /* don't trust bcopy for identical src/dst */ 11125 bcopy(invalp, outvalp, inlen); 11126 } 11127 *outlenp = inlen; 11128 return (0); 11129 case IP_BOUND_IF: 11130 case IP_XMIT_IF: 11131 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11132 level, name, first_mp); 11133 if (error != 0) 11134 return (error); 11135 break; /* goto sizeof (int) option return */ 11136 11137 case IP_UNSPEC_SRC: 11138 /* Allow sending with a zero source address */ 11139 if (!checkonly) { 11140 mutex_enter(&connp->conn_lock); 11141 connp->conn_unspec_src = *i1 ? 1 : 0; 11142 mutex_exit(&connp->conn_lock); 11143 } 11144 break; /* goto sizeof (int) option return */ 11145 default: 11146 /* 11147 * "soft" error (negative) 11148 * option not handled at this level 11149 * Note: Do not modify *outlenp 11150 */ 11151 return (-EINVAL); 11152 } 11153 break; 11154 case IPPROTO_IPV6: 11155 switch (name) { 11156 case IPV6_BOUND_IF: 11157 case IPV6_BOUND_PIF: 11158 case IPV6_DONTFAILOVER_IF: 11159 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11160 level, name, first_mp); 11161 if (error != 0) 11162 return (error); 11163 break; /* goto sizeof (int) option return */ 11164 11165 case IPV6_MULTICAST_IF: 11166 /* 11167 * The only possible errors are EINPROGRESS and 11168 * EINVAL. EINPROGRESS will be restarted and is not 11169 * a hard error. We call this option on both V4 and V6 11170 * If both return EINVAL, then this call returns 11171 * EINVAL. If at least one of them succeeds we 11172 * return success. 11173 */ 11174 found = B_FALSE; 11175 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11176 level, name, first_mp); 11177 if (error == EINPROGRESS) 11178 return (error); 11179 if (error == 0) 11180 found = B_TRUE; 11181 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11182 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 11183 if (error == 0) 11184 found = B_TRUE; 11185 if (!found) 11186 return (error); 11187 break; /* goto sizeof (int) option return */ 11188 11189 case IPV6_MULTICAST_HOPS: 11190 /* Recorded in transport above IP */ 11191 break; /* goto sizeof (int) option return */ 11192 case IPV6_MULTICAST_LOOP: 11193 if (!checkonly) { 11194 mutex_enter(&connp->conn_lock); 11195 connp->conn_multicast_loop = *i1; 11196 mutex_exit(&connp->conn_lock); 11197 } 11198 break; /* goto sizeof (int) option return */ 11199 case IPV6_JOIN_GROUP: 11200 case MCAST_JOIN_GROUP: 11201 case IPV6_LEAVE_GROUP: 11202 case MCAST_LEAVE_GROUP: { 11203 struct ipv6_mreq *ip_mreqp; 11204 struct group_req *greqp; 11205 ire_t *ire; 11206 boolean_t done = B_FALSE; 11207 in6_addr_t groupv6; 11208 uint32_t ifindex; 11209 boolean_t mcast_opt = B_TRUE; 11210 mcast_record_t fmode; 11211 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11212 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11213 11214 switch (name) { 11215 case IPV6_JOIN_GROUP: 11216 mcast_opt = B_FALSE; 11217 /* FALLTHRU */ 11218 case MCAST_JOIN_GROUP: 11219 fmode = MODE_IS_EXCLUDE; 11220 optfn = ip_opt_add_group_v6; 11221 break; 11222 11223 case IPV6_LEAVE_GROUP: 11224 mcast_opt = B_FALSE; 11225 /* FALLTHRU */ 11226 case MCAST_LEAVE_GROUP: 11227 fmode = MODE_IS_INCLUDE; 11228 optfn = ip_opt_delete_group_v6; 11229 break; 11230 } 11231 11232 if (mcast_opt) { 11233 struct sockaddr_in *sin; 11234 struct sockaddr_in6 *sin6; 11235 greqp = (struct group_req *)i1; 11236 if (greqp->gr_group.ss_family == AF_INET) { 11237 sin = (struct sockaddr_in *) 11238 &(greqp->gr_group); 11239 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 11240 &groupv6); 11241 } else { 11242 sin6 = (struct sockaddr_in6 *) 11243 &(greqp->gr_group); 11244 groupv6 = sin6->sin6_addr; 11245 } 11246 ifindex = greqp->gr_interface; 11247 } else { 11248 ip_mreqp = (struct ipv6_mreq *)i1; 11249 groupv6 = ip_mreqp->ipv6mr_multiaddr; 11250 ifindex = ip_mreqp->ipv6mr_interface; 11251 } 11252 /* 11253 * In the multirouting case, we need to replicate 11254 * the request on all interfaces that will take part 11255 * in replication. We do so because multirouting is 11256 * reflective, thus we will probably receive multi- 11257 * casts on those interfaces. 11258 * The ip_multirt_apply_membership_v6() succeeds if 11259 * the operation succeeds on at least one interface. 11260 */ 11261 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 11262 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11263 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11264 if (ire != NULL) { 11265 if (ire->ire_flags & RTF_MULTIRT) { 11266 error = ip_multirt_apply_membership_v6( 11267 optfn, ire, connp, checkonly, 11268 &groupv6, fmode, &ipv6_all_zeros, 11269 first_mp); 11270 done = B_TRUE; 11271 } 11272 ire_refrele(ire); 11273 } 11274 if (!done) { 11275 error = optfn(connp, checkonly, &groupv6, 11276 ifindex, fmode, &ipv6_all_zeros, first_mp); 11277 } 11278 if (error) { 11279 /* 11280 * EINPROGRESS is a soft error, needs retry 11281 * so don't make *outlenp zero. 11282 */ 11283 if (error != EINPROGRESS) 11284 *outlenp = 0; 11285 return (error); 11286 } 11287 /* OK return - copy input buffer into output buffer */ 11288 if (invalp != outvalp) { 11289 /* don't trust bcopy for identical src/dst */ 11290 bcopy(invalp, outvalp, inlen); 11291 } 11292 *outlenp = inlen; 11293 return (0); 11294 } 11295 case MCAST_BLOCK_SOURCE: 11296 case MCAST_UNBLOCK_SOURCE: 11297 case MCAST_JOIN_SOURCE_GROUP: 11298 case MCAST_LEAVE_SOURCE_GROUP: { 11299 struct group_source_req *gsreqp; 11300 in6_addr_t v6grp, v6src; 11301 uint32_t ifindex; 11302 mcast_record_t fmode; 11303 ire_t *ire; 11304 boolean_t done = B_FALSE; 11305 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11306 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11307 11308 switch (name) { 11309 case MCAST_BLOCK_SOURCE: 11310 fmode = MODE_IS_EXCLUDE; 11311 optfn = ip_opt_add_group_v6; 11312 break; 11313 case MCAST_UNBLOCK_SOURCE: 11314 fmode = MODE_IS_EXCLUDE; 11315 optfn = ip_opt_delete_group_v6; 11316 break; 11317 case MCAST_JOIN_SOURCE_GROUP: 11318 fmode = MODE_IS_INCLUDE; 11319 optfn = ip_opt_add_group_v6; 11320 break; 11321 case MCAST_LEAVE_SOURCE_GROUP: 11322 fmode = MODE_IS_INCLUDE; 11323 optfn = ip_opt_delete_group_v6; 11324 break; 11325 } 11326 11327 gsreqp = (struct group_source_req *)i1; 11328 ifindex = gsreqp->gsr_interface; 11329 if (gsreqp->gsr_group.ss_family == AF_INET) { 11330 struct sockaddr_in *s; 11331 s = (struct sockaddr_in *)&gsreqp->gsr_group; 11332 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 11333 s = (struct sockaddr_in *)&gsreqp->gsr_source; 11334 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 11335 } else { 11336 struct sockaddr_in6 *s6; 11337 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 11338 v6grp = s6->sin6_addr; 11339 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 11340 v6src = s6->sin6_addr; 11341 } 11342 11343 /* 11344 * In the multirouting case, we need to replicate 11345 * the request as noted in the mcast cases above. 11346 */ 11347 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 11348 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11349 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11350 if (ire != NULL) { 11351 if (ire->ire_flags & RTF_MULTIRT) { 11352 error = ip_multirt_apply_membership_v6( 11353 optfn, ire, connp, checkonly, 11354 &v6grp, fmode, &v6src, first_mp); 11355 done = B_TRUE; 11356 } 11357 ire_refrele(ire); 11358 } 11359 if (!done) { 11360 error = optfn(connp, checkonly, &v6grp, 11361 ifindex, fmode, &v6src, first_mp); 11362 } 11363 if (error != 0) { 11364 /* 11365 * EINPROGRESS is a soft error, needs retry 11366 * so don't make *outlenp zero. 11367 */ 11368 if (error != EINPROGRESS) 11369 *outlenp = 0; 11370 return (error); 11371 } 11372 /* OK return - copy input buffer into output buffer */ 11373 if (invalp != outvalp) { 11374 bcopy(invalp, outvalp, inlen); 11375 } 11376 *outlenp = inlen; 11377 return (0); 11378 } 11379 case IPV6_UNICAST_HOPS: 11380 /* Recorded in transport above IP */ 11381 break; /* goto sizeof (int) option return */ 11382 case IPV6_UNSPEC_SRC: 11383 /* Allow sending with a zero source address */ 11384 if (!checkonly) { 11385 mutex_enter(&connp->conn_lock); 11386 connp->conn_unspec_src = *i1 ? 1 : 0; 11387 mutex_exit(&connp->conn_lock); 11388 } 11389 break; /* goto sizeof (int) option return */ 11390 case IPV6_RECVPKTINFO: 11391 if (!checkonly) { 11392 mutex_enter(&connp->conn_lock); 11393 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11394 mutex_exit(&connp->conn_lock); 11395 } 11396 break; /* goto sizeof (int) option return */ 11397 case IPV6_RECVTCLASS: 11398 if (!checkonly) { 11399 if (*i1 < 0 || *i1 > 1) { 11400 return (EINVAL); 11401 } 11402 mutex_enter(&connp->conn_lock); 11403 connp->conn_ipv6_recvtclass = *i1; 11404 mutex_exit(&connp->conn_lock); 11405 } 11406 break; 11407 case IPV6_RECVPATHMTU: 11408 if (!checkonly) { 11409 if (*i1 < 0 || *i1 > 1) { 11410 return (EINVAL); 11411 } 11412 mutex_enter(&connp->conn_lock); 11413 connp->conn_ipv6_recvpathmtu = *i1; 11414 mutex_exit(&connp->conn_lock); 11415 } 11416 break; 11417 case IPV6_RECVHOPLIMIT: 11418 if (!checkonly) { 11419 mutex_enter(&connp->conn_lock); 11420 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 11421 mutex_exit(&connp->conn_lock); 11422 } 11423 break; /* goto sizeof (int) option return */ 11424 case IPV6_RECVHOPOPTS: 11425 if (!checkonly) { 11426 mutex_enter(&connp->conn_lock); 11427 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 11428 mutex_exit(&connp->conn_lock); 11429 } 11430 break; /* goto sizeof (int) option return */ 11431 case IPV6_RECVDSTOPTS: 11432 if (!checkonly) { 11433 mutex_enter(&connp->conn_lock); 11434 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 11435 mutex_exit(&connp->conn_lock); 11436 } 11437 break; /* goto sizeof (int) option return */ 11438 case IPV6_RECVRTHDR: 11439 if (!checkonly) { 11440 mutex_enter(&connp->conn_lock); 11441 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 11442 mutex_exit(&connp->conn_lock); 11443 } 11444 break; /* goto sizeof (int) option return */ 11445 case IPV6_RECVRTHDRDSTOPTS: 11446 if (!checkonly) { 11447 mutex_enter(&connp->conn_lock); 11448 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 11449 mutex_exit(&connp->conn_lock); 11450 } 11451 break; /* goto sizeof (int) option return */ 11452 case IPV6_PKTINFO: 11453 if (inlen == 0) 11454 return (-EINVAL); /* clearing option */ 11455 error = ip6_set_pktinfo(cr, connp, 11456 (struct in6_pktinfo *)invalp, first_mp); 11457 if (error != 0) 11458 *outlenp = 0; 11459 else 11460 *outlenp = inlen; 11461 return (error); 11462 case IPV6_NEXTHOP: { 11463 struct sockaddr_in6 *sin6; 11464 11465 /* Verify that the nexthop is reachable */ 11466 if (inlen == 0) 11467 return (-EINVAL); /* clearing option */ 11468 11469 sin6 = (struct sockaddr_in6 *)invalp; 11470 ire = ire_route_lookup_v6(&sin6->sin6_addr, 11471 0, 0, 0, NULL, NULL, connp->conn_zoneid, 11472 NULL, MATCH_IRE_DEFAULT, ipst); 11473 11474 if (ire == NULL) { 11475 *outlenp = 0; 11476 return (EHOSTUNREACH); 11477 } 11478 ire_refrele(ire); 11479 return (-EINVAL); 11480 } 11481 case IPV6_SEC_OPT: 11482 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11483 if (error != 0) { 11484 *outlenp = 0; 11485 return (error); 11486 } 11487 break; 11488 case IPV6_SRC_PREFERENCES: { 11489 /* 11490 * This is implemented strictly in the ip module 11491 * (here and in tcp_opt_*() to accomodate tcp 11492 * sockets). Modules above ip pass this option 11493 * down here since ip is the only one that needs to 11494 * be aware of source address preferences. 11495 * 11496 * This socket option only affects connected 11497 * sockets that haven't already bound to a specific 11498 * IPv6 address. In other words, sockets that 11499 * don't call bind() with an address other than the 11500 * unspecified address and that call connect(). 11501 * ip_bind_connected_v6() passes these preferences 11502 * to the ipif_select_source_v6() function. 11503 */ 11504 if (inlen != sizeof (uint32_t)) 11505 return (EINVAL); 11506 error = ip6_set_src_preferences(connp, 11507 *(uint32_t *)invalp); 11508 if (error != 0) { 11509 *outlenp = 0; 11510 return (error); 11511 } else { 11512 *outlenp = sizeof (uint32_t); 11513 } 11514 break; 11515 } 11516 case IPV6_V6ONLY: 11517 if (*i1 < 0 || *i1 > 1) { 11518 return (EINVAL); 11519 } 11520 mutex_enter(&connp->conn_lock); 11521 connp->conn_ipv6_v6only = *i1; 11522 mutex_exit(&connp->conn_lock); 11523 break; 11524 default: 11525 return (-EINVAL); 11526 } 11527 break; 11528 default: 11529 /* 11530 * "soft" error (negative) 11531 * option not handled at this level 11532 * Note: Do not modify *outlenp 11533 */ 11534 return (-EINVAL); 11535 } 11536 /* 11537 * Common case of return from an option that is sizeof (int) 11538 */ 11539 *(int *)outvalp = *i1; 11540 *outlenp = sizeof (int); 11541 return (0); 11542 } 11543 11544 /* 11545 * This routine gets default values of certain options whose default 11546 * values are maintained by protocol specific code 11547 */ 11548 /* ARGSUSED */ 11549 int 11550 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 11551 { 11552 int *i1 = (int *)ptr; 11553 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11554 11555 switch (level) { 11556 case IPPROTO_IP: 11557 switch (name) { 11558 case IP_MULTICAST_TTL: 11559 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 11560 return (sizeof (uchar_t)); 11561 case IP_MULTICAST_LOOP: 11562 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 11563 return (sizeof (uchar_t)); 11564 default: 11565 return (-1); 11566 } 11567 case IPPROTO_IPV6: 11568 switch (name) { 11569 case IPV6_UNICAST_HOPS: 11570 *i1 = ipst->ips_ipv6_def_hops; 11571 return (sizeof (int)); 11572 case IPV6_MULTICAST_HOPS: 11573 *i1 = IP_DEFAULT_MULTICAST_TTL; 11574 return (sizeof (int)); 11575 case IPV6_MULTICAST_LOOP: 11576 *i1 = IP_DEFAULT_MULTICAST_LOOP; 11577 return (sizeof (int)); 11578 case IPV6_V6ONLY: 11579 *i1 = 1; 11580 return (sizeof (int)); 11581 default: 11582 return (-1); 11583 } 11584 default: 11585 return (-1); 11586 } 11587 /* NOTREACHED */ 11588 } 11589 11590 /* 11591 * Given a destination address and a pointer to where to put the information 11592 * this routine fills in the mtuinfo. 11593 */ 11594 int 11595 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 11596 struct ip6_mtuinfo *mtuinfo, netstack_t *ns) 11597 { 11598 ire_t *ire; 11599 ip_stack_t *ipst = ns->netstack_ip; 11600 11601 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 11602 return (-1); 11603 11604 bzero(mtuinfo, sizeof (*mtuinfo)); 11605 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11606 mtuinfo->ip6m_addr.sin6_port = port; 11607 mtuinfo->ip6m_addr.sin6_addr = *in6; 11608 11609 ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL, ipst); 11610 if (ire != NULL) { 11611 mtuinfo->ip6m_mtu = ire->ire_max_frag; 11612 ire_refrele(ire); 11613 } else { 11614 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 11615 } 11616 return (sizeof (struct ip6_mtuinfo)); 11617 } 11618 11619 /* 11620 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 11621 * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and 11622 * isn't. This doesn't matter as the error checking is done properly for the 11623 * other MRT options coming in through ip_opt_set. 11624 */ 11625 int 11626 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 11627 { 11628 conn_t *connp = Q_TO_CONN(q); 11629 ipsec_req_t *req = (ipsec_req_t *)ptr; 11630 11631 switch (level) { 11632 case IPPROTO_IP: 11633 switch (name) { 11634 case MRT_VERSION: 11635 case MRT_ASSERT: 11636 (void) ip_mrouter_get(name, q, ptr); 11637 return (sizeof (int)); 11638 case IP_SEC_OPT: 11639 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 11640 case IP_NEXTHOP: 11641 if (connp->conn_nexthop_set) { 11642 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 11643 return (sizeof (ipaddr_t)); 11644 } else 11645 return (0); 11646 case IP_RECVPKTINFO: 11647 *(int *)ptr = connp->conn_ip_recvpktinfo ? 1: 0; 11648 return (sizeof (int)); 11649 default: 11650 break; 11651 } 11652 break; 11653 case IPPROTO_IPV6: 11654 switch (name) { 11655 case IPV6_SEC_OPT: 11656 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 11657 case IPV6_SRC_PREFERENCES: { 11658 return (ip6_get_src_preferences(connp, 11659 (uint32_t *)ptr)); 11660 } 11661 case IPV6_V6ONLY: 11662 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 11663 return (sizeof (int)); 11664 case IPV6_PATHMTU: 11665 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 11666 (struct ip6_mtuinfo *)ptr, connp->conn_netstack)); 11667 default: 11668 break; 11669 } 11670 break; 11671 default: 11672 break; 11673 } 11674 return (-1); 11675 } 11676 11677 /* Named Dispatch routine to get a current value out of our parameter table. */ 11678 /* ARGSUSED */ 11679 static int 11680 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11681 { 11682 ipparam_t *ippa = (ipparam_t *)cp; 11683 11684 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 11685 return (0); 11686 } 11687 11688 /* ARGSUSED */ 11689 static int 11690 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11691 { 11692 11693 (void) mi_mpprintf(mp, "%d", *(int *)cp); 11694 return (0); 11695 } 11696 11697 /* 11698 * Set ip{,6}_forwarding values. This means walking through all of the 11699 * ill's and toggling their forwarding values. 11700 */ 11701 /* ARGSUSED */ 11702 static int 11703 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11704 { 11705 long new_value; 11706 int *forwarding_value = (int *)cp; 11707 ill_t *ill; 11708 boolean_t isv6; 11709 ill_walk_context_t ctx; 11710 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11711 11712 isv6 = (forwarding_value == &ipst->ips_ipv6_forward); 11713 11714 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11715 new_value < 0 || new_value > 1) { 11716 return (EINVAL); 11717 } 11718 11719 *forwarding_value = new_value; 11720 11721 /* 11722 * Regardless of the current value of ip_forwarding, set all per-ill 11723 * values of ip_forwarding to the value being set. 11724 * 11725 * Bring all the ill's up to date with the new global value. 11726 */ 11727 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 11728 11729 if (isv6) 11730 ill = ILL_START_WALK_V6(&ctx, ipst); 11731 else 11732 ill = ILL_START_WALK_V4(&ctx, ipst); 11733 11734 for (; ill != NULL; ill = ill_next(&ctx, ill)) 11735 (void) ill_forward_set(ill, new_value != 0); 11736 11737 rw_exit(&ipst->ips_ill_g_lock); 11738 return (0); 11739 } 11740 11741 /* 11742 * Walk through the param array specified registering each element with the 11743 * Named Dispatch handler. This is called only during init. So it is ok 11744 * not to acquire any locks 11745 */ 11746 static boolean_t 11747 ip_param_register(IDP *ndp, ipparam_t *ippa, size_t ippa_cnt, 11748 ipndp_t *ipnd, size_t ipnd_cnt) 11749 { 11750 for (; ippa_cnt-- > 0; ippa++) { 11751 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 11752 if (!nd_load(ndp, ippa->ip_param_name, 11753 ip_param_get, ip_param_set, (caddr_t)ippa)) { 11754 nd_free(ndp); 11755 return (B_FALSE); 11756 } 11757 } 11758 } 11759 11760 for (; ipnd_cnt-- > 0; ipnd++) { 11761 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 11762 if (!nd_load(ndp, ipnd->ip_ndp_name, 11763 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 11764 ipnd->ip_ndp_data)) { 11765 nd_free(ndp); 11766 return (B_FALSE); 11767 } 11768 } 11769 } 11770 11771 return (B_TRUE); 11772 } 11773 11774 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 11775 /* ARGSUSED */ 11776 static int 11777 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11778 { 11779 long new_value; 11780 ipparam_t *ippa = (ipparam_t *)cp; 11781 11782 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11783 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 11784 return (EINVAL); 11785 } 11786 ippa->ip_param_value = new_value; 11787 return (0); 11788 } 11789 11790 /* 11791 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 11792 * When an ipf is passed here for the first time, if 11793 * we already have in-order fragments on the queue, we convert from the fast- 11794 * path reassembly scheme to the hard-case scheme. From then on, additional 11795 * fragments are reassembled here. We keep track of the start and end offsets 11796 * of each piece, and the number of holes in the chain. When the hole count 11797 * goes to zero, we are done! 11798 * 11799 * The ipf_count will be updated to account for any mblk(s) added (pointed to 11800 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 11801 * ipfb_count and ill_frag_count by the difference of ipf_count before and 11802 * after the call to ip_reassemble(). 11803 */ 11804 int 11805 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 11806 size_t msg_len) 11807 { 11808 uint_t end; 11809 mblk_t *next_mp; 11810 mblk_t *mp1; 11811 uint_t offset; 11812 boolean_t incr_dups = B_TRUE; 11813 boolean_t offset_zero_seen = B_FALSE; 11814 boolean_t pkt_boundary_checked = B_FALSE; 11815 11816 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 11817 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 11818 11819 /* Add in byte count */ 11820 ipf->ipf_count += msg_len; 11821 if (ipf->ipf_end) { 11822 /* 11823 * We were part way through in-order reassembly, but now there 11824 * is a hole. We walk through messages already queued, and 11825 * mark them for hard case reassembly. We know that up till 11826 * now they were in order starting from offset zero. 11827 */ 11828 offset = 0; 11829 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 11830 IP_REASS_SET_START(mp1, offset); 11831 if (offset == 0) { 11832 ASSERT(ipf->ipf_nf_hdr_len != 0); 11833 offset = -ipf->ipf_nf_hdr_len; 11834 } 11835 offset += mp1->b_wptr - mp1->b_rptr; 11836 IP_REASS_SET_END(mp1, offset); 11837 } 11838 /* One hole at the end. */ 11839 ipf->ipf_hole_cnt = 1; 11840 /* Brand it as a hard case, forever. */ 11841 ipf->ipf_end = 0; 11842 } 11843 /* Walk through all the new pieces. */ 11844 do { 11845 end = start + (mp->b_wptr - mp->b_rptr); 11846 /* 11847 * If start is 0, decrease 'end' only for the first mblk of 11848 * the fragment. Otherwise 'end' can get wrong value in the 11849 * second pass of the loop if first mblk is exactly the 11850 * size of ipf_nf_hdr_len. 11851 */ 11852 if (start == 0 && !offset_zero_seen) { 11853 /* First segment */ 11854 ASSERT(ipf->ipf_nf_hdr_len != 0); 11855 end -= ipf->ipf_nf_hdr_len; 11856 offset_zero_seen = B_TRUE; 11857 } 11858 next_mp = mp->b_cont; 11859 /* 11860 * We are checking to see if there is any interesing data 11861 * to process. If there isn't and the mblk isn't the 11862 * one which carries the unfragmentable header then we 11863 * drop it. It's possible to have just the unfragmentable 11864 * header come through without any data. That needs to be 11865 * saved. 11866 * 11867 * If the assert at the top of this function holds then the 11868 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 11869 * is infrequently traveled enough that the test is left in 11870 * to protect against future code changes which break that 11871 * invariant. 11872 */ 11873 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 11874 /* Empty. Blast it. */ 11875 IP_REASS_SET_START(mp, 0); 11876 IP_REASS_SET_END(mp, 0); 11877 /* 11878 * If the ipf points to the mblk we are about to free, 11879 * update ipf to point to the next mblk (or NULL 11880 * if none). 11881 */ 11882 if (ipf->ipf_mp->b_cont == mp) 11883 ipf->ipf_mp->b_cont = next_mp; 11884 freeb(mp); 11885 continue; 11886 } 11887 mp->b_cont = NULL; 11888 IP_REASS_SET_START(mp, start); 11889 IP_REASS_SET_END(mp, end); 11890 if (!ipf->ipf_tail_mp) { 11891 ipf->ipf_tail_mp = mp; 11892 ipf->ipf_mp->b_cont = mp; 11893 if (start == 0 || !more) { 11894 ipf->ipf_hole_cnt = 1; 11895 /* 11896 * if the first fragment comes in more than one 11897 * mblk, this loop will be executed for each 11898 * mblk. Need to adjust hole count so exiting 11899 * this routine will leave hole count at 1. 11900 */ 11901 if (next_mp) 11902 ipf->ipf_hole_cnt++; 11903 } else 11904 ipf->ipf_hole_cnt = 2; 11905 continue; 11906 } else if (ipf->ipf_last_frag_seen && !more && 11907 !pkt_boundary_checked) { 11908 /* 11909 * We check datagram boundary only if this fragment 11910 * claims to be the last fragment and we have seen a 11911 * last fragment in the past too. We do this only 11912 * once for a given fragment. 11913 * 11914 * start cannot be 0 here as fragments with start=0 11915 * and MF=0 gets handled as a complete packet. These 11916 * fragments should not reach here. 11917 */ 11918 11919 if (start + msgdsize(mp) != 11920 IP_REASS_END(ipf->ipf_tail_mp)) { 11921 /* 11922 * We have two fragments both of which claim 11923 * to be the last fragment but gives conflicting 11924 * information about the whole datagram size. 11925 * Something fishy is going on. Drop the 11926 * fragment and free up the reassembly list. 11927 */ 11928 return (IP_REASS_FAILED); 11929 } 11930 11931 /* 11932 * We shouldn't come to this code block again for this 11933 * particular fragment. 11934 */ 11935 pkt_boundary_checked = B_TRUE; 11936 } 11937 11938 /* New stuff at or beyond tail? */ 11939 offset = IP_REASS_END(ipf->ipf_tail_mp); 11940 if (start >= offset) { 11941 if (ipf->ipf_last_frag_seen) { 11942 /* current fragment is beyond last fragment */ 11943 return (IP_REASS_FAILED); 11944 } 11945 /* Link it on end. */ 11946 ipf->ipf_tail_mp->b_cont = mp; 11947 ipf->ipf_tail_mp = mp; 11948 if (more) { 11949 if (start != offset) 11950 ipf->ipf_hole_cnt++; 11951 } else if (start == offset && next_mp == NULL) 11952 ipf->ipf_hole_cnt--; 11953 continue; 11954 } 11955 mp1 = ipf->ipf_mp->b_cont; 11956 offset = IP_REASS_START(mp1); 11957 /* New stuff at the front? */ 11958 if (start < offset) { 11959 if (start == 0) { 11960 if (end >= offset) { 11961 /* Nailed the hole at the begining. */ 11962 ipf->ipf_hole_cnt--; 11963 } 11964 } else if (end < offset) { 11965 /* 11966 * A hole, stuff, and a hole where there used 11967 * to be just a hole. 11968 */ 11969 ipf->ipf_hole_cnt++; 11970 } 11971 mp->b_cont = mp1; 11972 /* Check for overlap. */ 11973 while (end > offset) { 11974 if (end < IP_REASS_END(mp1)) { 11975 mp->b_wptr -= end - offset; 11976 IP_REASS_SET_END(mp, offset); 11977 BUMP_MIB(ill->ill_ip_mib, 11978 ipIfStatsReasmPartDups); 11979 break; 11980 } 11981 /* Did we cover another hole? */ 11982 if ((mp1->b_cont && 11983 IP_REASS_END(mp1) != 11984 IP_REASS_START(mp1->b_cont) && 11985 end >= IP_REASS_START(mp1->b_cont)) || 11986 (!ipf->ipf_last_frag_seen && !more)) { 11987 ipf->ipf_hole_cnt--; 11988 } 11989 /* Clip out mp1. */ 11990 if ((mp->b_cont = mp1->b_cont) == NULL) { 11991 /* 11992 * After clipping out mp1, this guy 11993 * is now hanging off the end. 11994 */ 11995 ipf->ipf_tail_mp = mp; 11996 } 11997 IP_REASS_SET_START(mp1, 0); 11998 IP_REASS_SET_END(mp1, 0); 11999 /* Subtract byte count */ 12000 ipf->ipf_count -= mp1->b_datap->db_lim - 12001 mp1->b_datap->db_base; 12002 freeb(mp1); 12003 BUMP_MIB(ill->ill_ip_mib, 12004 ipIfStatsReasmPartDups); 12005 mp1 = mp->b_cont; 12006 if (!mp1) 12007 break; 12008 offset = IP_REASS_START(mp1); 12009 } 12010 ipf->ipf_mp->b_cont = mp; 12011 continue; 12012 } 12013 /* 12014 * The new piece starts somewhere between the start of the head 12015 * and before the end of the tail. 12016 */ 12017 for (; mp1; mp1 = mp1->b_cont) { 12018 offset = IP_REASS_END(mp1); 12019 if (start < offset) { 12020 if (end <= offset) { 12021 /* Nothing new. */ 12022 IP_REASS_SET_START(mp, 0); 12023 IP_REASS_SET_END(mp, 0); 12024 /* Subtract byte count */ 12025 ipf->ipf_count -= mp->b_datap->db_lim - 12026 mp->b_datap->db_base; 12027 if (incr_dups) { 12028 ipf->ipf_num_dups++; 12029 incr_dups = B_FALSE; 12030 } 12031 freeb(mp); 12032 BUMP_MIB(ill->ill_ip_mib, 12033 ipIfStatsReasmDuplicates); 12034 break; 12035 } 12036 /* 12037 * Trim redundant stuff off beginning of new 12038 * piece. 12039 */ 12040 IP_REASS_SET_START(mp, offset); 12041 mp->b_rptr += offset - start; 12042 BUMP_MIB(ill->ill_ip_mib, 12043 ipIfStatsReasmPartDups); 12044 start = offset; 12045 if (!mp1->b_cont) { 12046 /* 12047 * After trimming, this guy is now 12048 * hanging off the end. 12049 */ 12050 mp1->b_cont = mp; 12051 ipf->ipf_tail_mp = mp; 12052 if (!more) { 12053 ipf->ipf_hole_cnt--; 12054 } 12055 break; 12056 } 12057 } 12058 if (start >= IP_REASS_START(mp1->b_cont)) 12059 continue; 12060 /* Fill a hole */ 12061 if (start > offset) 12062 ipf->ipf_hole_cnt++; 12063 mp->b_cont = mp1->b_cont; 12064 mp1->b_cont = mp; 12065 mp1 = mp->b_cont; 12066 offset = IP_REASS_START(mp1); 12067 if (end >= offset) { 12068 ipf->ipf_hole_cnt--; 12069 /* Check for overlap. */ 12070 while (end > offset) { 12071 if (end < IP_REASS_END(mp1)) { 12072 mp->b_wptr -= end - offset; 12073 IP_REASS_SET_END(mp, offset); 12074 /* 12075 * TODO we might bump 12076 * this up twice if there is 12077 * overlap at both ends. 12078 */ 12079 BUMP_MIB(ill->ill_ip_mib, 12080 ipIfStatsReasmPartDups); 12081 break; 12082 } 12083 /* Did we cover another hole? */ 12084 if ((mp1->b_cont && 12085 IP_REASS_END(mp1) 12086 != IP_REASS_START(mp1->b_cont) && 12087 end >= 12088 IP_REASS_START(mp1->b_cont)) || 12089 (!ipf->ipf_last_frag_seen && 12090 !more)) { 12091 ipf->ipf_hole_cnt--; 12092 } 12093 /* Clip out mp1. */ 12094 if ((mp->b_cont = mp1->b_cont) == 12095 NULL) { 12096 /* 12097 * After clipping out mp1, 12098 * this guy is now hanging 12099 * off the end. 12100 */ 12101 ipf->ipf_tail_mp = mp; 12102 } 12103 IP_REASS_SET_START(mp1, 0); 12104 IP_REASS_SET_END(mp1, 0); 12105 /* Subtract byte count */ 12106 ipf->ipf_count -= 12107 mp1->b_datap->db_lim - 12108 mp1->b_datap->db_base; 12109 freeb(mp1); 12110 BUMP_MIB(ill->ill_ip_mib, 12111 ipIfStatsReasmPartDups); 12112 mp1 = mp->b_cont; 12113 if (!mp1) 12114 break; 12115 offset = IP_REASS_START(mp1); 12116 } 12117 } 12118 break; 12119 } 12120 } while (start = end, mp = next_mp); 12121 12122 /* Fragment just processed could be the last one. Remember this fact */ 12123 if (!more) 12124 ipf->ipf_last_frag_seen = B_TRUE; 12125 12126 /* Still got holes? */ 12127 if (ipf->ipf_hole_cnt) 12128 return (IP_REASS_PARTIAL); 12129 /* Clean up overloaded fields to avoid upstream disasters. */ 12130 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12131 IP_REASS_SET_START(mp1, 0); 12132 IP_REASS_SET_END(mp1, 0); 12133 } 12134 return (IP_REASS_COMPLETE); 12135 } 12136 12137 /* 12138 * ipsec processing for the fast path, used for input UDP Packets 12139 * Returns true if ready for passup to UDP. 12140 * Return false if packet is not passable to UDP (e.g. it failed IPsec policy, 12141 * was an ESP-in-UDP packet, etc.). 12142 */ 12143 static boolean_t 12144 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 12145 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present, ire_t *ire) 12146 { 12147 uint32_t ill_index; 12148 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 12149 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 12150 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 12151 udp_t *udp = connp->conn_udp; 12152 12153 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12154 /* The ill_index of the incoming ILL */ 12155 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 12156 12157 /* pass packet up to the transport */ 12158 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 12159 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 12160 NULL, mctl_present); 12161 if (*first_mpp == NULL) { 12162 return (B_FALSE); 12163 } 12164 } 12165 12166 /* Initiate IPPF processing for fastpath UDP */ 12167 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 12168 ip_process(IPP_LOCAL_IN, mpp, ill_index); 12169 if (*mpp == NULL) { 12170 ip2dbg(("ip_input_ipsec_process: UDP pkt " 12171 "deferred/dropped during IPPF processing\n")); 12172 return (B_FALSE); 12173 } 12174 } 12175 /* 12176 * Remove 0-spi if it's 0, or move everything behind 12177 * the UDP header over it and forward to ESP via 12178 * ip_proto_input(). 12179 */ 12180 if (udp->udp_nat_t_endpoint) { 12181 if (mctl_present) { 12182 /* mctl_present *shouldn't* happen. */ 12183 ip_drop_packet(*first_mpp, B_TRUE, NULL, 12184 NULL, DROPPER(ipss, ipds_esp_nat_t_ipsec), 12185 &ipss->ipsec_dropper); 12186 *first_mpp = NULL; 12187 return (B_FALSE); 12188 } 12189 12190 /* "ill" is "recv_ill" in actuality. */ 12191 if (!zero_spi_check(q, *mpp, ipha, ire, ill, ipss)) 12192 return (B_FALSE); 12193 12194 /* Else continue like a normal UDP packet. */ 12195 } 12196 12197 /* 12198 * We make the checks as below since we are in the fast path 12199 * and want to minimize the number of checks if the IP_RECVIF and/or 12200 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 12201 */ 12202 if (connp->conn_recvif || connp->conn_recvslla || 12203 connp->conn_ip_recvpktinfo) { 12204 if (connp->conn_recvif) { 12205 in_flags = IPF_RECVIF; 12206 } 12207 /* 12208 * UDP supports IP_RECVPKTINFO option for both v4 and v6 12209 * so the flag passed to ip_add_info is based on IP version 12210 * of connp. 12211 */ 12212 if (connp->conn_ip_recvpktinfo) { 12213 if (connp->conn_af_isv6) { 12214 /* 12215 * V6 only needs index 12216 */ 12217 in_flags |= IPF_RECVIF; 12218 } else { 12219 /* 12220 * V4 needs index + matching address. 12221 */ 12222 in_flags |= IPF_RECVADDR; 12223 } 12224 } 12225 if (connp->conn_recvslla) { 12226 in_flags |= IPF_RECVSLLA; 12227 } 12228 /* 12229 * since in_flags are being set ill will be 12230 * referenced in ip_add_info, so it better not 12231 * be NULL. 12232 */ 12233 /* 12234 * the actual data will be contained in b_cont 12235 * upon successful return of the following call. 12236 * If the call fails then the original mblk is 12237 * returned. 12238 */ 12239 *mpp = ip_add_info(*mpp, ill, in_flags, IPCL_ZONEID(connp), 12240 ipst); 12241 } 12242 12243 return (B_TRUE); 12244 } 12245 12246 /* 12247 * Fragmentation reassembly. Each ILL has a hash table for 12248 * queuing packets undergoing reassembly for all IPIFs 12249 * associated with the ILL. The hash is based on the packet 12250 * IP ident field. The ILL frag hash table was allocated 12251 * as a timer block at the time the ILL was created. Whenever 12252 * there is anything on the reassembly queue, the timer will 12253 * be running. Returns B_TRUE if successful else B_FALSE; 12254 * frees mp on failure. 12255 */ 12256 static boolean_t 12257 ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, 12258 uint32_t *cksum_val, uint16_t *cksum_flags) 12259 { 12260 uint32_t frag_offset_flags; 12261 ill_t *ill = (ill_t *)q->q_ptr; 12262 mblk_t *mp = *mpp; 12263 mblk_t *t_mp; 12264 ipaddr_t dst; 12265 uint8_t proto = ipha->ipha_protocol; 12266 uint32_t sum_val; 12267 uint16_t sum_flags; 12268 ipf_t *ipf; 12269 ipf_t **ipfp; 12270 ipfb_t *ipfb; 12271 uint16_t ident; 12272 uint32_t offset; 12273 ipaddr_t src; 12274 uint_t hdr_length; 12275 uint32_t end; 12276 mblk_t *mp1; 12277 mblk_t *tail_mp; 12278 size_t count; 12279 size_t msg_len; 12280 uint8_t ecn_info = 0; 12281 uint32_t packet_size; 12282 boolean_t pruned = B_FALSE; 12283 ip_stack_t *ipst = ill->ill_ipst; 12284 12285 if (cksum_val != NULL) 12286 *cksum_val = 0; 12287 if (cksum_flags != NULL) 12288 *cksum_flags = 0; 12289 12290 /* 12291 * Drop the fragmented as early as possible, if 12292 * we don't have resource(s) to re-assemble. 12293 */ 12294 if (ipst->ips_ip_reass_queue_bytes == 0) { 12295 freemsg(mp); 12296 return (B_FALSE); 12297 } 12298 12299 /* Check for fragmentation offset; return if there's none */ 12300 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 12301 (IPH_MF | IPH_OFFSET)) == 0) 12302 return (B_TRUE); 12303 12304 /* 12305 * We utilize hardware computed checksum info only for UDP since 12306 * IP fragmentation is a normal occurence for the protocol. In 12307 * addition, checksum offload support for IP fragments carrying 12308 * UDP payload is commonly implemented across network adapters. 12309 */ 12310 ASSERT(ill != NULL); 12311 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 12312 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 12313 mblk_t *mp1 = mp->b_cont; 12314 int32_t len; 12315 12316 /* Record checksum information from the packet */ 12317 sum_val = (uint32_t)DB_CKSUM16(mp); 12318 sum_flags = DB_CKSUMFLAGS(mp); 12319 12320 /* IP payload offset from beginning of mblk */ 12321 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 12322 12323 if ((sum_flags & HCK_PARTIALCKSUM) && 12324 (mp1 == NULL || mp1->b_cont == NULL) && 12325 offset >= DB_CKSUMSTART(mp) && 12326 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 12327 uint32_t adj; 12328 /* 12329 * Partial checksum has been calculated by hardware 12330 * and attached to the packet; in addition, any 12331 * prepended extraneous data is even byte aligned. 12332 * If any such data exists, we adjust the checksum; 12333 * this would also handle any postpended data. 12334 */ 12335 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 12336 mp, mp1, len, adj); 12337 12338 /* One's complement subtract extraneous checksum */ 12339 if (adj >= sum_val) 12340 sum_val = ~(adj - sum_val) & 0xFFFF; 12341 else 12342 sum_val -= adj; 12343 } 12344 } else { 12345 sum_val = 0; 12346 sum_flags = 0; 12347 } 12348 12349 /* Clear hardware checksumming flag */ 12350 DB_CKSUMFLAGS(mp) = 0; 12351 12352 ident = ipha->ipha_ident; 12353 offset = (frag_offset_flags << 3) & 0xFFFF; 12354 src = ipha->ipha_src; 12355 dst = ipha->ipha_dst; 12356 hdr_length = IPH_HDR_LENGTH(ipha); 12357 end = ntohs(ipha->ipha_length) - hdr_length; 12358 12359 /* If end == 0 then we have a packet with no data, so just free it */ 12360 if (end == 0) { 12361 freemsg(mp); 12362 return (B_FALSE); 12363 } 12364 12365 /* Record the ECN field info. */ 12366 ecn_info = (ipha->ipha_type_of_service & 0x3); 12367 if (offset != 0) { 12368 /* 12369 * If this isn't the first piece, strip the header, and 12370 * add the offset to the end value. 12371 */ 12372 mp->b_rptr += hdr_length; 12373 end += offset; 12374 } 12375 12376 msg_len = MBLKSIZE(mp); 12377 tail_mp = mp; 12378 while (tail_mp->b_cont != NULL) { 12379 tail_mp = tail_mp->b_cont; 12380 msg_len += MBLKSIZE(tail_mp); 12381 } 12382 12383 /* If the reassembly list for this ILL will get too big, prune it */ 12384 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 12385 ipst->ips_ip_reass_queue_bytes) { 12386 ill_frag_prune(ill, 12387 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 : 12388 (ipst->ips_ip_reass_queue_bytes - msg_len)); 12389 pruned = B_TRUE; 12390 } 12391 12392 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 12393 mutex_enter(&ipfb->ipfb_lock); 12394 12395 ipfp = &ipfb->ipfb_ipf; 12396 /* Try to find an existing fragment queue for this packet. */ 12397 for (;;) { 12398 ipf = ipfp[0]; 12399 if (ipf != NULL) { 12400 /* 12401 * It has to match on ident and src/dst address. 12402 */ 12403 if (ipf->ipf_ident == ident && 12404 ipf->ipf_src == src && 12405 ipf->ipf_dst == dst && 12406 ipf->ipf_protocol == proto) { 12407 /* 12408 * If we have received too many 12409 * duplicate fragments for this packet 12410 * free it. 12411 */ 12412 if (ipf->ipf_num_dups > ip_max_frag_dups) { 12413 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12414 freemsg(mp); 12415 mutex_exit(&ipfb->ipfb_lock); 12416 return (B_FALSE); 12417 } 12418 /* Found it. */ 12419 break; 12420 } 12421 ipfp = &ipf->ipf_hash_next; 12422 continue; 12423 } 12424 12425 /* 12426 * If we pruned the list, do we want to store this new 12427 * fragment?. We apply an optimization here based on the 12428 * fact that most fragments will be received in order. 12429 * So if the offset of this incoming fragment is zero, 12430 * it is the first fragment of a new packet. We will 12431 * keep it. Otherwise drop the fragment, as we have 12432 * probably pruned the packet already (since the 12433 * packet cannot be found). 12434 */ 12435 if (pruned && offset != 0) { 12436 mutex_exit(&ipfb->ipfb_lock); 12437 freemsg(mp); 12438 return (B_FALSE); 12439 } 12440 12441 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) { 12442 /* 12443 * Too many fragmented packets in this hash 12444 * bucket. Free the oldest. 12445 */ 12446 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 12447 } 12448 12449 /* New guy. Allocate a frag message. */ 12450 mp1 = allocb(sizeof (*ipf), BPRI_MED); 12451 if (mp1 == NULL) { 12452 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12453 freemsg(mp); 12454 reass_done: 12455 mutex_exit(&ipfb->ipfb_lock); 12456 return (B_FALSE); 12457 } 12458 12459 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); 12460 mp1->b_cont = mp; 12461 12462 /* Initialize the fragment header. */ 12463 ipf = (ipf_t *)mp1->b_rptr; 12464 ipf->ipf_mp = mp1; 12465 ipf->ipf_ptphn = ipfp; 12466 ipfp[0] = ipf; 12467 ipf->ipf_hash_next = NULL; 12468 ipf->ipf_ident = ident; 12469 ipf->ipf_protocol = proto; 12470 ipf->ipf_src = src; 12471 ipf->ipf_dst = dst; 12472 ipf->ipf_nf_hdr_len = 0; 12473 /* Record reassembly start time. */ 12474 ipf->ipf_timestamp = gethrestime_sec(); 12475 /* Record ipf generation and account for frag header */ 12476 ipf->ipf_gen = ill->ill_ipf_gen++; 12477 ipf->ipf_count = MBLKSIZE(mp1); 12478 ipf->ipf_last_frag_seen = B_FALSE; 12479 ipf->ipf_ecn = ecn_info; 12480 ipf->ipf_num_dups = 0; 12481 ipfb->ipfb_frag_pkts++; 12482 ipf->ipf_checksum = 0; 12483 ipf->ipf_checksum_flags = 0; 12484 12485 /* Store checksum value in fragment header */ 12486 if (sum_flags != 0) { 12487 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12488 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12489 ipf->ipf_checksum = sum_val; 12490 ipf->ipf_checksum_flags = sum_flags; 12491 } 12492 12493 /* 12494 * We handle reassembly two ways. In the easy case, 12495 * where all the fragments show up in order, we do 12496 * minimal bookkeeping, and just clip new pieces on 12497 * the end. If we ever see a hole, then we go off 12498 * to ip_reassemble which has to mark the pieces and 12499 * keep track of the number of holes, etc. Obviously, 12500 * the point of having both mechanisms is so we can 12501 * handle the easy case as efficiently as possible. 12502 */ 12503 if (offset == 0) { 12504 /* Easy case, in-order reassembly so far. */ 12505 ipf->ipf_count += msg_len; 12506 ipf->ipf_tail_mp = tail_mp; 12507 /* 12508 * Keep track of next expected offset in 12509 * ipf_end. 12510 */ 12511 ipf->ipf_end = end; 12512 ipf->ipf_nf_hdr_len = hdr_length; 12513 } else { 12514 /* Hard case, hole at the beginning. */ 12515 ipf->ipf_tail_mp = NULL; 12516 /* 12517 * ipf_end == 0 means that we have given up 12518 * on easy reassembly. 12519 */ 12520 ipf->ipf_end = 0; 12521 12522 /* Forget checksum offload from now on */ 12523 ipf->ipf_checksum_flags = 0; 12524 12525 /* 12526 * ipf_hole_cnt is set by ip_reassemble. 12527 * ipf_count is updated by ip_reassemble. 12528 * No need to check for return value here 12529 * as we don't expect reassembly to complete 12530 * or fail for the first fragment itself. 12531 */ 12532 (void) ip_reassemble(mp, ipf, 12533 (frag_offset_flags & IPH_OFFSET) << 3, 12534 (frag_offset_flags & IPH_MF), ill, msg_len); 12535 } 12536 /* Update per ipfb and ill byte counts */ 12537 ipfb->ipfb_count += ipf->ipf_count; 12538 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12539 ill->ill_frag_count += ipf->ipf_count; 12540 /* If the frag timer wasn't already going, start it. */ 12541 mutex_enter(&ill->ill_lock); 12542 ill_frag_timer_start(ill); 12543 mutex_exit(&ill->ill_lock); 12544 goto reass_done; 12545 } 12546 12547 /* 12548 * If the packet's flag has changed (it could be coming up 12549 * from an interface different than the previous, therefore 12550 * possibly different checksum capability), then forget about 12551 * any stored checksum states. Otherwise add the value to 12552 * the existing one stored in the fragment header. 12553 */ 12554 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 12555 sum_val += ipf->ipf_checksum; 12556 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12557 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12558 ipf->ipf_checksum = sum_val; 12559 } else if (ipf->ipf_checksum_flags != 0) { 12560 /* Forget checksum offload from now on */ 12561 ipf->ipf_checksum_flags = 0; 12562 } 12563 12564 /* 12565 * We have a new piece of a datagram which is already being 12566 * reassembled. Update the ECN info if all IP fragments 12567 * are ECN capable. If there is one which is not, clear 12568 * all the info. If there is at least one which has CE 12569 * code point, IP needs to report that up to transport. 12570 */ 12571 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 12572 if (ecn_info == IPH_ECN_CE) 12573 ipf->ipf_ecn = IPH_ECN_CE; 12574 } else { 12575 ipf->ipf_ecn = IPH_ECN_NECT; 12576 } 12577 if (offset && ipf->ipf_end == offset) { 12578 /* The new fragment fits at the end */ 12579 ipf->ipf_tail_mp->b_cont = mp; 12580 /* Update the byte count */ 12581 ipf->ipf_count += msg_len; 12582 /* Update per ipfb and ill byte counts */ 12583 ipfb->ipfb_count += msg_len; 12584 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12585 ill->ill_frag_count += msg_len; 12586 if (frag_offset_flags & IPH_MF) { 12587 /* More to come. */ 12588 ipf->ipf_end = end; 12589 ipf->ipf_tail_mp = tail_mp; 12590 goto reass_done; 12591 } 12592 } else { 12593 /* Go do the hard cases. */ 12594 int ret; 12595 12596 if (offset == 0) 12597 ipf->ipf_nf_hdr_len = hdr_length; 12598 12599 /* Save current byte count */ 12600 count = ipf->ipf_count; 12601 ret = ip_reassemble(mp, ipf, 12602 (frag_offset_flags & IPH_OFFSET) << 3, 12603 (frag_offset_flags & IPH_MF), ill, msg_len); 12604 /* Count of bytes added and subtracted (freeb()ed) */ 12605 count = ipf->ipf_count - count; 12606 if (count) { 12607 /* Update per ipfb and ill byte counts */ 12608 ipfb->ipfb_count += count; 12609 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12610 ill->ill_frag_count += count; 12611 } 12612 if (ret == IP_REASS_PARTIAL) { 12613 goto reass_done; 12614 } else if (ret == IP_REASS_FAILED) { 12615 /* Reassembly failed. Free up all resources */ 12616 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12617 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 12618 IP_REASS_SET_START(t_mp, 0); 12619 IP_REASS_SET_END(t_mp, 0); 12620 } 12621 freemsg(mp); 12622 goto reass_done; 12623 } 12624 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 12625 } 12626 /* 12627 * We have completed reassembly. Unhook the frag header from 12628 * the reassembly list. 12629 * 12630 * Before we free the frag header, record the ECN info 12631 * to report back to the transport. 12632 */ 12633 ecn_info = ipf->ipf_ecn; 12634 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); 12635 ipfp = ipf->ipf_ptphn; 12636 12637 /* We need to supply these to caller */ 12638 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 12639 sum_val = ipf->ipf_checksum; 12640 else 12641 sum_val = 0; 12642 12643 mp1 = ipf->ipf_mp; 12644 count = ipf->ipf_count; 12645 ipf = ipf->ipf_hash_next; 12646 if (ipf != NULL) 12647 ipf->ipf_ptphn = ipfp; 12648 ipfp[0] = ipf; 12649 ill->ill_frag_count -= count; 12650 ASSERT(ipfb->ipfb_count >= count); 12651 ipfb->ipfb_count -= count; 12652 ipfb->ipfb_frag_pkts--; 12653 mutex_exit(&ipfb->ipfb_lock); 12654 /* Ditch the frag header. */ 12655 mp = mp1->b_cont; 12656 12657 freeb(mp1); 12658 12659 /* Restore original IP length in header. */ 12660 packet_size = (uint32_t)msgdsize(mp); 12661 if (packet_size > IP_MAXPACKET) { 12662 freemsg(mp); 12663 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 12664 return (B_FALSE); 12665 } 12666 12667 if (DB_REF(mp) > 1) { 12668 mblk_t *mp2 = copymsg(mp); 12669 12670 freemsg(mp); 12671 if (mp2 == NULL) { 12672 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12673 return (B_FALSE); 12674 } 12675 mp = mp2; 12676 } 12677 ipha = (ipha_t *)mp->b_rptr; 12678 12679 ipha->ipha_length = htons((uint16_t)packet_size); 12680 /* We're now complete, zip the frag state */ 12681 ipha->ipha_fragment_offset_and_flags = 0; 12682 /* Record the ECN info. */ 12683 ipha->ipha_type_of_service &= 0xFC; 12684 ipha->ipha_type_of_service |= ecn_info; 12685 *mpp = mp; 12686 12687 /* Reassembly is successful; return checksum information if needed */ 12688 if (cksum_val != NULL) 12689 *cksum_val = sum_val; 12690 if (cksum_flags != NULL) 12691 *cksum_flags = sum_flags; 12692 12693 return (B_TRUE); 12694 } 12695 12696 /* 12697 * Perform ip header check sum update local options. 12698 * return B_TRUE if all is well, else return B_FALSE and release 12699 * the mp. caller is responsible for decrementing ire ref cnt. 12700 */ 12701 static boolean_t 12702 ip_options_cksum(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12703 ip_stack_t *ipst) 12704 { 12705 mblk_t *first_mp; 12706 boolean_t mctl_present; 12707 uint16_t sum; 12708 12709 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12710 /* 12711 * Don't do the checksum if it has gone through AH/ESP 12712 * processing. 12713 */ 12714 if (!mctl_present) { 12715 sum = ip_csum_hdr(ipha); 12716 if (sum != 0) { 12717 if (ill != NULL) { 12718 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12719 } else { 12720 BUMP_MIB(&ipst->ips_ip_mib, 12721 ipIfStatsInCksumErrs); 12722 } 12723 freemsg(first_mp); 12724 return (B_FALSE); 12725 } 12726 } 12727 12728 if (!ip_rput_local_options(q, mp, ipha, ire, ipst)) { 12729 if (mctl_present) 12730 freeb(first_mp); 12731 return (B_FALSE); 12732 } 12733 12734 return (B_TRUE); 12735 } 12736 12737 /* 12738 * All udp packet are delivered to the local host via this routine. 12739 */ 12740 void 12741 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12742 ill_t *recv_ill) 12743 { 12744 uint32_t sum; 12745 uint32_t u1; 12746 boolean_t mctl_present; 12747 conn_t *connp; 12748 mblk_t *first_mp; 12749 uint16_t *up; 12750 ill_t *ill = (ill_t *)q->q_ptr; 12751 uint16_t reass_hck_flags = 0; 12752 ip_stack_t *ipst; 12753 12754 ASSERT(recv_ill != NULL); 12755 ipst = recv_ill->ill_ipst; 12756 12757 #define rptr ((uchar_t *)ipha) 12758 12759 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12760 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 12761 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12762 ASSERT(ill != NULL); 12763 12764 /* 12765 * FAST PATH for udp packets 12766 */ 12767 12768 /* u1 is # words of IP options */ 12769 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 12770 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12771 12772 /* IP options present */ 12773 if (u1 != 0) 12774 goto ipoptions; 12775 12776 /* Check the IP header checksum. */ 12777 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12778 /* Clear the IP header h/w cksum flag */ 12779 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12780 } else if (!mctl_present) { 12781 /* 12782 * Don't verify header checksum if this packet is coming 12783 * back from AH/ESP as we already did it. 12784 */ 12785 #define uph ((uint16_t *)ipha) 12786 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 12787 uph[6] + uph[7] + uph[8] + uph[9]; 12788 #undef uph 12789 /* finish doing IP checksum */ 12790 sum = (sum & 0xFFFF) + (sum >> 16); 12791 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12792 if (sum != 0 && sum != 0xFFFF) { 12793 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12794 freemsg(first_mp); 12795 return; 12796 } 12797 } 12798 12799 /* 12800 * Count for SNMP of inbound packets for ire. 12801 * if mctl is present this might be a secure packet and 12802 * has already been counted for in ip_proto_input(). 12803 */ 12804 if (!mctl_present) { 12805 UPDATE_IB_PKT_COUNT(ire); 12806 ire->ire_last_used_time = lbolt; 12807 } 12808 12809 /* packet part of fragmented IP packet? */ 12810 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12811 if (u1 & (IPH_MF | IPH_OFFSET)) { 12812 goto fragmented; 12813 } 12814 12815 /* u1 = IP header length (20 bytes) */ 12816 u1 = IP_SIMPLE_HDR_LENGTH; 12817 12818 /* packet does not contain complete IP & UDP headers */ 12819 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 12820 goto udppullup; 12821 12822 /* up points to UDP header */ 12823 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 12824 #define iphs ((uint16_t *)ipha) 12825 12826 /* if udp hdr cksum != 0, then need to checksum udp packet */ 12827 if (up[3] != 0) { 12828 mblk_t *mp1 = mp->b_cont; 12829 boolean_t cksum_err; 12830 uint16_t hck_flags = 0; 12831 12832 /* Pseudo-header checksum */ 12833 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12834 iphs[9] + up[2]; 12835 12836 /* 12837 * Revert to software checksum calculation if the interface 12838 * isn't capable of checksum offload or if IPsec is present. 12839 */ 12840 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 12841 hck_flags = DB_CKSUMFLAGS(mp); 12842 12843 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12844 IP_STAT(ipst, ip_in_sw_cksum); 12845 12846 IP_CKSUM_RECV(hck_flags, u1, 12847 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 12848 (int32_t)((uchar_t *)up - rptr), 12849 mp, mp1, cksum_err); 12850 12851 if (cksum_err) { 12852 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12853 if (hck_flags & HCK_FULLCKSUM) 12854 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 12855 else if (hck_flags & HCK_PARTIALCKSUM) 12856 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 12857 else 12858 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 12859 12860 freemsg(first_mp); 12861 return; 12862 } 12863 } 12864 12865 /* Non-fragmented broadcast or multicast packet? */ 12866 if (ire->ire_type == IRE_BROADCAST) 12867 goto udpslowpath; 12868 12869 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 12870 ire->ire_zoneid, ipst)) != NULL) { 12871 ASSERT(connp->conn_upq != NULL); 12872 IP_STAT(ipst, ip_udp_fast_path); 12873 12874 if (CONN_UDP_FLOWCTLD(connp)) { 12875 freemsg(mp); 12876 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 12877 } else { 12878 if (!mctl_present) { 12879 BUMP_MIB(ill->ill_ip_mib, 12880 ipIfStatsHCInDelivers); 12881 } 12882 /* 12883 * mp and first_mp can change. 12884 */ 12885 if (ip_udp_check(q, connp, recv_ill, 12886 ipha, &mp, &first_mp, mctl_present, ire)) { 12887 /* Send it upstream */ 12888 CONN_UDP_RECV(connp, mp); 12889 } 12890 } 12891 /* 12892 * freeb() cannot deal with null mblk being passed 12893 * in and first_mp can be set to null in the call 12894 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 12895 */ 12896 if (mctl_present && first_mp != NULL) { 12897 freeb(first_mp); 12898 } 12899 CONN_DEC_REF(connp); 12900 return; 12901 } 12902 12903 /* 12904 * if we got here we know the packet is not fragmented and 12905 * has no options. The classifier could not find a conn_t and 12906 * most likely its an icmp packet so send it through slow path. 12907 */ 12908 12909 goto udpslowpath; 12910 12911 ipoptions: 12912 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 12913 goto slow_done; 12914 } 12915 12916 UPDATE_IB_PKT_COUNT(ire); 12917 ire->ire_last_used_time = lbolt; 12918 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12919 if (u1 & (IPH_MF | IPH_OFFSET)) { 12920 fragmented: 12921 /* 12922 * "sum" and "reass_hck_flags" are non-zero if the 12923 * reassembled packet has a valid hardware computed 12924 * checksum information associated with it. 12925 */ 12926 if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) 12927 goto slow_done; 12928 /* 12929 * Make sure that first_mp points back to mp as 12930 * the mp we came in with could have changed in 12931 * ip_rput_fragment(). 12932 */ 12933 ASSERT(!mctl_present); 12934 ipha = (ipha_t *)mp->b_rptr; 12935 first_mp = mp; 12936 } 12937 12938 /* Now we have a complete datagram, destined for this machine. */ 12939 u1 = IPH_HDR_LENGTH(ipha); 12940 /* Pull up the UDP header, if necessary. */ 12941 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 12942 udppullup: 12943 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 12944 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12945 freemsg(first_mp); 12946 goto slow_done; 12947 } 12948 ipha = (ipha_t *)mp->b_rptr; 12949 } 12950 12951 /* 12952 * Validate the checksum for the reassembled packet; for the 12953 * pullup case we calculate the payload checksum in software. 12954 */ 12955 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 12956 if (up[3] != 0) { 12957 boolean_t cksum_err; 12958 12959 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12960 IP_STAT(ipst, ip_in_sw_cksum); 12961 12962 IP_CKSUM_RECV_REASS(reass_hck_flags, 12963 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 12964 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12965 iphs[9] + up[2], sum, cksum_err); 12966 12967 if (cksum_err) { 12968 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12969 12970 if (reass_hck_flags & HCK_FULLCKSUM) 12971 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 12972 else if (reass_hck_flags & HCK_PARTIALCKSUM) 12973 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 12974 else 12975 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 12976 12977 freemsg(first_mp); 12978 goto slow_done; 12979 } 12980 } 12981 udpslowpath: 12982 12983 /* Clear hardware checksum flag to be safe */ 12984 DB_CKSUMFLAGS(mp) = 0; 12985 12986 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 12987 (ire->ire_type == IRE_BROADCAST), 12988 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IPINFO, 12989 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 12990 12991 slow_done: 12992 IP_STAT(ipst, ip_udp_slow_path); 12993 return; 12994 12995 #undef iphs 12996 #undef rptr 12997 } 12998 12999 /* ARGSUSED */ 13000 static mblk_t * 13001 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13002 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 13003 ill_rx_ring_t *ill_ring) 13004 { 13005 conn_t *connp; 13006 uint32_t sum; 13007 uint32_t u1; 13008 uint16_t *up; 13009 int offset; 13010 ssize_t len; 13011 mblk_t *mp1; 13012 boolean_t syn_present = B_FALSE; 13013 tcph_t *tcph; 13014 uint_t ip_hdr_len; 13015 ill_t *ill = (ill_t *)q->q_ptr; 13016 zoneid_t zoneid = ire->ire_zoneid; 13017 boolean_t cksum_err; 13018 uint16_t hck_flags = 0; 13019 ip_stack_t *ipst = recv_ill->ill_ipst; 13020 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 13021 13022 #define rptr ((uchar_t *)ipha) 13023 13024 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 13025 ASSERT(ill != NULL); 13026 13027 /* 13028 * FAST PATH for tcp packets 13029 */ 13030 13031 /* u1 is # words of IP options */ 13032 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13033 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13034 13035 /* IP options present */ 13036 if (u1) { 13037 goto ipoptions; 13038 } else if (!mctl_present) { 13039 /* Check the IP header checksum. */ 13040 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 13041 /* Clear the IP header h/w cksum flag */ 13042 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 13043 } else if (!mctl_present) { 13044 /* 13045 * Don't verify header checksum if this packet 13046 * is coming back from AH/ESP as we already did it. 13047 */ 13048 #define uph ((uint16_t *)ipha) 13049 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13050 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13051 #undef uph 13052 /* finish doing IP checksum */ 13053 sum = (sum & 0xFFFF) + (sum >> 16); 13054 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13055 if (sum != 0 && sum != 0xFFFF) { 13056 BUMP_MIB(ill->ill_ip_mib, 13057 ipIfStatsInCksumErrs); 13058 goto error; 13059 } 13060 } 13061 } 13062 13063 if (!mctl_present) { 13064 UPDATE_IB_PKT_COUNT(ire); 13065 ire->ire_last_used_time = lbolt; 13066 } 13067 13068 /* packet part of fragmented IP packet? */ 13069 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13070 if (u1 & (IPH_MF | IPH_OFFSET)) { 13071 goto fragmented; 13072 } 13073 13074 /* u1 = IP header length (20 bytes) */ 13075 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 13076 13077 /* does packet contain IP+TCP headers? */ 13078 len = mp->b_wptr - rptr; 13079 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 13080 IP_STAT(ipst, ip_tcppullup); 13081 goto tcppullup; 13082 } 13083 13084 /* TCP options present? */ 13085 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 13086 13087 /* 13088 * If options need to be pulled up, then goto tcpoptions. 13089 * otherwise we are still in the fast path 13090 */ 13091 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 13092 IP_STAT(ipst, ip_tcpoptions); 13093 goto tcpoptions; 13094 } 13095 13096 /* multiple mblks of tcp data? */ 13097 if ((mp1 = mp->b_cont) != NULL) { 13098 /* more then two? */ 13099 if (mp1->b_cont != NULL) { 13100 IP_STAT(ipst, ip_multipkttcp); 13101 goto multipkttcp; 13102 } 13103 len += mp1->b_wptr - mp1->b_rptr; 13104 } 13105 13106 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 13107 13108 /* part of pseudo checksum */ 13109 13110 /* TCP datagram length */ 13111 u1 = len - IP_SIMPLE_HDR_LENGTH; 13112 13113 #define iphs ((uint16_t *)ipha) 13114 13115 #ifdef _BIG_ENDIAN 13116 u1 += IPPROTO_TCP; 13117 #else 13118 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13119 #endif 13120 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13121 13122 /* 13123 * Revert to software checksum calculation if the interface 13124 * isn't capable of checksum offload or if IPsec is present. 13125 */ 13126 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 13127 hck_flags = DB_CKSUMFLAGS(mp); 13128 13129 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13130 IP_STAT(ipst, ip_in_sw_cksum); 13131 13132 IP_CKSUM_RECV(hck_flags, u1, 13133 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13134 (int32_t)((uchar_t *)up - rptr), 13135 mp, mp1, cksum_err); 13136 13137 if (cksum_err) { 13138 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13139 13140 if (hck_flags & HCK_FULLCKSUM) 13141 IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err); 13142 else if (hck_flags & HCK_PARTIALCKSUM) 13143 IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err); 13144 else 13145 IP_STAT(ipst, ip_tcp_in_sw_cksum_err); 13146 13147 goto error; 13148 } 13149 13150 try_again: 13151 13152 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 13153 zoneid, ipst)) == NULL) { 13154 /* Send the TH_RST */ 13155 goto no_conn; 13156 } 13157 13158 /* 13159 * TCP FAST PATH for AF_INET socket. 13160 * 13161 * TCP fast path to avoid extra work. An AF_INET socket type 13162 * does not have facility to receive extra information via 13163 * ip_process or ip_add_info. Also, when the connection was 13164 * established, we made a check if this connection is impacted 13165 * by any global IPsec policy or per connection policy (a 13166 * policy that comes in effect later will not apply to this 13167 * connection). Since all this can be determined at the 13168 * connection establishment time, a quick check of flags 13169 * can avoid extra work. 13170 */ 13171 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 13172 !IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13173 ASSERT(first_mp == mp); 13174 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13175 SET_SQUEUE(mp, tcp_rput_data, connp); 13176 return (mp); 13177 } 13178 13179 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 13180 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 13181 if (IPCL_IS_TCP(connp)) { 13182 mp->b_datap->db_struioflag |= STRUIO_EAGER; 13183 DB_CKSUMSTART(mp) = 13184 (intptr_t)ip_squeue_get(ill_ring); 13185 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 13186 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13187 BUMP_MIB(ill->ill_ip_mib, 13188 ipIfStatsHCInDelivers); 13189 SET_SQUEUE(mp, connp->conn_recv, connp); 13190 return (mp); 13191 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 13192 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13193 BUMP_MIB(ill->ill_ip_mib, 13194 ipIfStatsHCInDelivers); 13195 ip_squeue_enter_unbound++; 13196 SET_SQUEUE(mp, tcp_conn_request_unbound, 13197 connp); 13198 return (mp); 13199 } 13200 syn_present = B_TRUE; 13201 } 13202 13203 } 13204 13205 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 13206 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13207 13208 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13209 /* No need to send this packet to TCP */ 13210 if ((flags & TH_RST) || (flags & TH_URG)) { 13211 CONN_DEC_REF(connp); 13212 freemsg(first_mp); 13213 return (NULL); 13214 } 13215 if (flags & TH_ACK) { 13216 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 13217 ipst->ips_netstack->netstack_tcp, connp); 13218 CONN_DEC_REF(connp); 13219 return (NULL); 13220 } 13221 13222 CONN_DEC_REF(connp); 13223 freemsg(first_mp); 13224 return (NULL); 13225 } 13226 13227 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 13228 first_mp = ipsec_check_inbound_policy(first_mp, connp, 13229 ipha, NULL, mctl_present); 13230 if (first_mp == NULL) { 13231 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13232 CONN_DEC_REF(connp); 13233 return (NULL); 13234 } 13235 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 13236 ASSERT(syn_present); 13237 if (mctl_present) { 13238 ASSERT(first_mp != mp); 13239 first_mp->b_datap->db_struioflag |= 13240 STRUIO_POLICY; 13241 } else { 13242 ASSERT(first_mp == mp); 13243 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 13244 mp->b_datap->db_struioflag |= STRUIO_POLICY; 13245 } 13246 } else { 13247 /* 13248 * Discard first_mp early since we're dealing with a 13249 * fully-connected conn_t and tcp doesn't do policy in 13250 * this case. 13251 */ 13252 if (mctl_present) { 13253 freeb(first_mp); 13254 mctl_present = B_FALSE; 13255 } 13256 first_mp = mp; 13257 } 13258 } 13259 13260 /* Initiate IPPF processing for fastpath */ 13261 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13262 uint32_t ill_index; 13263 13264 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13265 ip_process(IPP_LOCAL_IN, &mp, ill_index); 13266 if (mp == NULL) { 13267 ip2dbg(("ip_input_ipsec_process: TCP pkt " 13268 "deferred/dropped during IPPF processing\n")); 13269 CONN_DEC_REF(connp); 13270 if (mctl_present) 13271 freeb(first_mp); 13272 return (NULL); 13273 } else if (mctl_present) { 13274 /* 13275 * ip_process might return a new mp. 13276 */ 13277 ASSERT(first_mp != mp); 13278 first_mp->b_cont = mp; 13279 } else { 13280 first_mp = mp; 13281 } 13282 13283 } 13284 13285 if (!syn_present && connp->conn_ip_recvpktinfo) { 13286 /* 13287 * TCP does not support IP_RECVPKTINFO for v4 so lets 13288 * make sure IPF_RECVIF is passed to ip_add_info. 13289 */ 13290 mp = ip_add_info(mp, recv_ill, flags|IPF_RECVIF, 13291 IPCL_ZONEID(connp), ipst); 13292 if (mp == NULL) { 13293 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13294 CONN_DEC_REF(connp); 13295 if (mctl_present) 13296 freeb(first_mp); 13297 return (NULL); 13298 } else if (mctl_present) { 13299 /* 13300 * ip_add_info might return a new mp. 13301 */ 13302 ASSERT(first_mp != mp); 13303 first_mp->b_cont = mp; 13304 } else { 13305 first_mp = mp; 13306 } 13307 } 13308 13309 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13310 if (IPCL_IS_TCP(connp)) { 13311 SET_SQUEUE(first_mp, connp->conn_recv, connp); 13312 return (first_mp); 13313 } else { 13314 putnext(connp->conn_rq, first_mp); 13315 CONN_DEC_REF(connp); 13316 return (NULL); 13317 } 13318 13319 no_conn: 13320 /* Initiate IPPf processing, if needed. */ 13321 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13322 uint32_t ill_index; 13323 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13324 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 13325 if (first_mp == NULL) { 13326 return (NULL); 13327 } 13328 } 13329 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13330 13331 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid, 13332 ipst->ips_netstack->netstack_tcp, NULL); 13333 return (NULL); 13334 ipoptions: 13335 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) { 13336 goto slow_done; 13337 } 13338 13339 UPDATE_IB_PKT_COUNT(ire); 13340 ire->ire_last_used_time = lbolt; 13341 13342 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13343 if (u1 & (IPH_MF | IPH_OFFSET)) { 13344 fragmented: 13345 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 13346 if (mctl_present) 13347 freeb(first_mp); 13348 goto slow_done; 13349 } 13350 /* 13351 * Make sure that first_mp points back to mp as 13352 * the mp we came in with could have changed in 13353 * ip_rput_fragment(). 13354 */ 13355 ASSERT(!mctl_present); 13356 ipha = (ipha_t *)mp->b_rptr; 13357 first_mp = mp; 13358 } 13359 13360 /* Now we have a complete datagram, destined for this machine. */ 13361 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 13362 13363 len = mp->b_wptr - mp->b_rptr; 13364 /* Pull up a minimal TCP header, if necessary. */ 13365 if (len < (u1 + 20)) { 13366 tcppullup: 13367 if (!pullupmsg(mp, u1 + 20)) { 13368 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13369 goto error; 13370 } 13371 ipha = (ipha_t *)mp->b_rptr; 13372 len = mp->b_wptr - mp->b_rptr; 13373 } 13374 13375 /* 13376 * Extract the offset field from the TCP header. As usual, we 13377 * try to help the compiler more than the reader. 13378 */ 13379 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 13380 if (offset != 5) { 13381 tcpoptions: 13382 if (offset < 5) { 13383 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13384 goto error; 13385 } 13386 /* 13387 * There must be TCP options. 13388 * Make sure we can grab them. 13389 */ 13390 offset <<= 2; 13391 offset += u1; 13392 if (len < offset) { 13393 if (!pullupmsg(mp, offset)) { 13394 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13395 goto error; 13396 } 13397 ipha = (ipha_t *)mp->b_rptr; 13398 len = mp->b_wptr - rptr; 13399 } 13400 } 13401 13402 /* Get the total packet length in len, including headers. */ 13403 if (mp->b_cont) { 13404 multipkttcp: 13405 len = msgdsize(mp); 13406 } 13407 13408 /* 13409 * Check the TCP checksum by pulling together the pseudo- 13410 * header checksum, and passing it to ip_csum to be added in 13411 * with the TCP datagram. 13412 * 13413 * Since we are not using the hwcksum if available we must 13414 * clear the flag. We may come here via tcppullup or tcpoptions. 13415 * If either of these fails along the way the mblk is freed. 13416 * If this logic ever changes and mblk is reused to say send 13417 * ICMP's back, then this flag may need to be cleared in 13418 * other places as well. 13419 */ 13420 DB_CKSUMFLAGS(mp) = 0; 13421 13422 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 13423 13424 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 13425 #ifdef _BIG_ENDIAN 13426 u1 += IPPROTO_TCP; 13427 #else 13428 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13429 #endif 13430 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13431 /* 13432 * Not M_DATA mblk or its a dup, so do the checksum now. 13433 */ 13434 IP_STAT(ipst, ip_in_sw_cksum); 13435 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 13436 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13437 goto error; 13438 } 13439 13440 IP_STAT(ipst, ip_tcp_slow_path); 13441 goto try_again; 13442 #undef iphs 13443 #undef rptr 13444 13445 error: 13446 freemsg(first_mp); 13447 slow_done: 13448 return (NULL); 13449 } 13450 13451 /* ARGSUSED */ 13452 static void 13453 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13454 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 13455 { 13456 conn_t *connp; 13457 uint32_t sum; 13458 uint32_t u1; 13459 ssize_t len; 13460 sctp_hdr_t *sctph; 13461 zoneid_t zoneid = ire->ire_zoneid; 13462 uint32_t pktsum; 13463 uint32_t calcsum; 13464 uint32_t ports; 13465 in6_addr_t map_src, map_dst; 13466 ill_t *ill = (ill_t *)q->q_ptr; 13467 ip_stack_t *ipst; 13468 sctp_stack_t *sctps; 13469 13470 ASSERT(recv_ill != NULL); 13471 ipst = recv_ill->ill_ipst; 13472 sctps = ipst->ips_netstack->netstack_sctp; 13473 13474 #define rptr ((uchar_t *)ipha) 13475 13476 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 13477 ASSERT(ill != NULL); 13478 13479 /* u1 is # words of IP options */ 13480 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13481 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13482 13483 /* IP options present */ 13484 if (u1 > 0) { 13485 goto ipoptions; 13486 } else { 13487 /* Check the IP header checksum. */ 13488 if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, ill) && 13489 !mctl_present) { 13490 #define uph ((uint16_t *)ipha) 13491 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13492 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13493 #undef uph 13494 /* finish doing IP checksum */ 13495 sum = (sum & 0xFFFF) + (sum >> 16); 13496 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13497 /* 13498 * Don't verify header checksum if this packet 13499 * is coming back from AH/ESP as we already did it. 13500 */ 13501 if (sum != 0 && sum != 0xFFFF) { 13502 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 13503 goto error; 13504 } 13505 } 13506 /* 13507 * Since there is no SCTP h/w cksum support yet, just 13508 * clear the flag. 13509 */ 13510 DB_CKSUMFLAGS(mp) = 0; 13511 } 13512 13513 /* 13514 * Don't verify header checksum if this packet is coming 13515 * back from AH/ESP as we already did it. 13516 */ 13517 if (!mctl_present) { 13518 UPDATE_IB_PKT_COUNT(ire); 13519 ire->ire_last_used_time = lbolt; 13520 } 13521 13522 /* packet part of fragmented IP packet? */ 13523 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13524 if (u1 & (IPH_MF | IPH_OFFSET)) 13525 goto fragmented; 13526 13527 /* u1 = IP header length (20 bytes) */ 13528 u1 = IP_SIMPLE_HDR_LENGTH; 13529 13530 find_sctp_client: 13531 /* Pullup if we don't have the sctp common header. */ 13532 len = MBLKL(mp); 13533 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 13534 if (mp->b_cont == NULL || 13535 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 13536 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13537 goto error; 13538 } 13539 ipha = (ipha_t *)mp->b_rptr; 13540 len = MBLKL(mp); 13541 } 13542 13543 sctph = (sctp_hdr_t *)(rptr + u1); 13544 #ifdef DEBUG 13545 if (!skip_sctp_cksum) { 13546 #endif 13547 pktsum = sctph->sh_chksum; 13548 sctph->sh_chksum = 0; 13549 calcsum = sctp_cksum(mp, u1); 13550 if (calcsum != pktsum) { 13551 BUMP_MIB(&sctps->sctps_mib, sctpChecksumError); 13552 goto error; 13553 } 13554 sctph->sh_chksum = pktsum; 13555 #ifdef DEBUG /* skip_sctp_cksum */ 13556 } 13557 #endif 13558 /* get the ports */ 13559 ports = *(uint32_t *)&sctph->sh_sport; 13560 13561 IRE_REFRELE(ire); 13562 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 13563 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 13564 if ((connp = sctp_fanout(&map_src, &map_dst, ports, zoneid, mp, 13565 sctps)) == NULL) { 13566 /* Check for raw socket or OOTB handling */ 13567 goto no_conn; 13568 } 13569 13570 /* Found a client; up it goes */ 13571 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13572 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 13573 return; 13574 13575 no_conn: 13576 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 13577 ports, mctl_present, flags, B_TRUE, zoneid); 13578 return; 13579 13580 ipoptions: 13581 DB_CKSUMFLAGS(mp) = 0; 13582 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) 13583 goto slow_done; 13584 13585 UPDATE_IB_PKT_COUNT(ire); 13586 ire->ire_last_used_time = lbolt; 13587 13588 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13589 if (u1 & (IPH_MF | IPH_OFFSET)) { 13590 fragmented: 13591 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) 13592 goto slow_done; 13593 /* 13594 * Make sure that first_mp points back to mp as 13595 * the mp we came in with could have changed in 13596 * ip_rput_fragment(). 13597 */ 13598 ASSERT(!mctl_present); 13599 ipha = (ipha_t *)mp->b_rptr; 13600 first_mp = mp; 13601 } 13602 13603 /* Now we have a complete datagram, destined for this machine. */ 13604 u1 = IPH_HDR_LENGTH(ipha); 13605 goto find_sctp_client; 13606 #undef iphs 13607 #undef rptr 13608 13609 error: 13610 freemsg(first_mp); 13611 slow_done: 13612 IRE_REFRELE(ire); 13613 } 13614 13615 #define VER_BITS 0xF0 13616 #define VERSION_6 0x60 13617 13618 static boolean_t 13619 ip_rput_multimblk_ipoptions(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t **iphapp, 13620 ipaddr_t *dstp, ip_stack_t *ipst) 13621 { 13622 uint_t opt_len; 13623 ipha_t *ipha; 13624 ssize_t len; 13625 uint_t pkt_len; 13626 13627 ASSERT(ill != NULL); 13628 IP_STAT(ipst, ip_ipoptions); 13629 ipha = *iphapp; 13630 13631 #define rptr ((uchar_t *)ipha) 13632 /* Assume no IPv6 packets arrive over the IPv4 queue */ 13633 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 13634 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); 13635 freemsg(mp); 13636 return (B_FALSE); 13637 } 13638 13639 /* multiple mblk or too short */ 13640 pkt_len = ntohs(ipha->ipha_length); 13641 13642 /* Get the number of words of IP options in the IP header. */ 13643 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 13644 if (opt_len) { 13645 /* IP Options present! Validate and process. */ 13646 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 13647 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13648 goto done; 13649 } 13650 /* 13651 * Recompute complete header length and make sure we 13652 * have access to all of it. 13653 */ 13654 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 13655 if (len > (mp->b_wptr - rptr)) { 13656 if (len > pkt_len) { 13657 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13658 goto done; 13659 } 13660 if (!pullupmsg(mp, len)) { 13661 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13662 goto done; 13663 } 13664 ipha = (ipha_t *)mp->b_rptr; 13665 } 13666 /* 13667 * Go off to ip_rput_options which returns the next hop 13668 * destination address, which may have been affected 13669 * by source routing. 13670 */ 13671 IP_STAT(ipst, ip_opt); 13672 if (ip_rput_options(q, mp, ipha, dstp, ipst) == -1) { 13673 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13674 return (B_FALSE); 13675 } 13676 } 13677 *iphapp = ipha; 13678 return (B_TRUE); 13679 done: 13680 /* clear b_prev - used by ip_mroute_decap */ 13681 mp->b_prev = NULL; 13682 freemsg(mp); 13683 return (B_FALSE); 13684 #undef rptr 13685 } 13686 13687 /* 13688 * Deal with the fact that there is no ire for the destination. 13689 */ 13690 static ire_t * 13691 ip_rput_noire(queue_t *q, mblk_t *mp, int ll_multicast, ipaddr_t dst) 13692 { 13693 ipha_t *ipha; 13694 ill_t *ill; 13695 ire_t *ire; 13696 boolean_t check_multirt = B_FALSE; 13697 ip_stack_t *ipst; 13698 13699 ipha = (ipha_t *)mp->b_rptr; 13700 ill = (ill_t *)q->q_ptr; 13701 13702 ASSERT(ill != NULL); 13703 ipst = ill->ill_ipst; 13704 13705 /* 13706 * No IRE for this destination, so it can't be for us. 13707 * Unless we are forwarding, drop the packet. 13708 * We have to let source routed packets through 13709 * since we don't yet know if they are 'ping -l' 13710 * packets i.e. if they will go out over the 13711 * same interface as they came in on. 13712 */ 13713 if (ll_multicast) { 13714 freemsg(mp); 13715 return (NULL); 13716 } 13717 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) { 13718 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13719 freemsg(mp); 13720 return (NULL); 13721 } 13722 13723 /* 13724 * Mark this packet as having originated externally. 13725 * 13726 * For non-forwarding code path, ire_send later double 13727 * checks this interface to see if it is still exists 13728 * post-ARP resolution. 13729 * 13730 * Also, IPQOS uses this to differentiate between 13731 * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP 13732 * QOS packet processing in ip_wput_attach_llhdr(). 13733 * The QoS module can mark the b_band for a fastpath message 13734 * or the dl_priority field in a unitdata_req header for 13735 * CoS marking. This info can only be found in 13736 * ip_wput_attach_llhdr(). 13737 */ 13738 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 13739 /* 13740 * Clear the indication that this may have a hardware checksum 13741 * as we are not using it 13742 */ 13743 DB_CKSUMFLAGS(mp) = 0; 13744 13745 ire = ire_forward(dst, &check_multirt, NULL, NULL, 13746 MBLK_GETLABEL(mp), ipst); 13747 13748 if (ire == NULL && check_multirt) { 13749 /* Let ip_newroute handle CGTP */ 13750 ip_newroute(q, mp, dst, NULL, GLOBAL_ZONEID, ipst); 13751 return (NULL); 13752 } 13753 13754 if (ire != NULL) 13755 return (ire); 13756 13757 mp->b_prev = mp->b_next = 0; 13758 /* send icmp unreachable */ 13759 q = WR(q); 13760 /* Sent by forwarding path, and router is global zone */ 13761 if (ip_source_routed(ipha, ipst)) { 13762 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, 13763 GLOBAL_ZONEID, ipst); 13764 } else { 13765 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 13766 ipst); 13767 } 13768 13769 return (NULL); 13770 13771 } 13772 13773 /* 13774 * check ip header length and align it. 13775 */ 13776 static boolean_t 13777 ip_check_and_align_header(queue_t *q, mblk_t *mp, ip_stack_t *ipst) 13778 { 13779 ssize_t len; 13780 ill_t *ill; 13781 ipha_t *ipha; 13782 13783 len = MBLKL(mp); 13784 13785 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13786 ill = (ill_t *)q->q_ptr; 13787 13788 if (!OK_32PTR(mp->b_rptr)) 13789 IP_STAT(ipst, ip_notaligned1); 13790 else 13791 IP_STAT(ipst, ip_notaligned2); 13792 /* Guard against bogus device drivers */ 13793 if (len < 0) { 13794 /* clear b_prev - used by ip_mroute_decap */ 13795 mp->b_prev = NULL; 13796 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13797 freemsg(mp); 13798 return (B_FALSE); 13799 } 13800 13801 if (ip_rput_pullups++ == 0) { 13802 ipha = (ipha_t *)mp->b_rptr; 13803 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 13804 "ip_check_and_align_header: %s forced us to " 13805 " pullup pkt, hdr len %ld, hdr addr %p", 13806 ill->ill_name, len, ipha); 13807 } 13808 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 13809 /* clear b_prev - used by ip_mroute_decap */ 13810 mp->b_prev = NULL; 13811 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13812 freemsg(mp); 13813 return (B_FALSE); 13814 } 13815 } 13816 return (B_TRUE); 13817 } 13818 13819 ire_t * 13820 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill) 13821 { 13822 ire_t *new_ire; 13823 ill_t *ire_ill; 13824 uint_t ifindex; 13825 ip_stack_t *ipst = ill->ill_ipst; 13826 boolean_t strict_check = B_FALSE; 13827 13828 /* 13829 * This packet came in on an interface other than the one associated 13830 * with the first ire we found for the destination address. We do 13831 * another ire lookup here, using the ingress ill, to see if the 13832 * interface is in an interface group. 13833 * As long as the ills belong to the same group, we don't consider 13834 * them to be arriving on the wrong interface. Thus, if the switch 13835 * is doing inbound load spreading, we won't drop packets when the 13836 * ip*_strict_dst_multihoming switch is on. Note, the same holds true 13837 * for 'usesrc groups' where the destination address may belong to 13838 * another interface to allow multipathing to happen. 13839 * We also need to check for IPIF_UNNUMBERED point2point interfaces 13840 * where the local address may not be unique. In this case we were 13841 * at the mercy of the initial ire cache lookup and the IRE_LOCAL it 13842 * actually returned. The new lookup, which is more specific, should 13843 * only find the IRE_LOCAL associated with the ingress ill if one 13844 * exists. 13845 */ 13846 13847 if (ire->ire_ipversion == IPV4_VERSION) { 13848 if (ipst->ips_ip_strict_dst_multihoming) 13849 strict_check = B_TRUE; 13850 new_ire = ire_ctable_lookup(*((ipaddr_t *)addr), 0, IRE_LOCAL, 13851 ill->ill_ipif, ALL_ZONES, NULL, 13852 (MATCH_IRE_TYPE|MATCH_IRE_ILL_GROUP), ipst); 13853 } else { 13854 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr)); 13855 if (ipst->ips_ipv6_strict_dst_multihoming) 13856 strict_check = B_TRUE; 13857 new_ire = ire_ctable_lookup_v6((in6_addr_t *)addr, NULL, 13858 IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL, 13859 (MATCH_IRE_TYPE|MATCH_IRE_ILL_GROUP), ipst); 13860 } 13861 /* 13862 * If the same ire that was returned in ip_input() is found then this 13863 * is an indication that interface groups are in use. The packet 13864 * arrived on a different ill in the group than the one associated with 13865 * the destination address. If a different ire was found then the same 13866 * IP address must be hosted on multiple ills. This is possible with 13867 * unnumbered point2point interfaces. We switch to use this new ire in 13868 * order to have accurate interface statistics. 13869 */ 13870 if (new_ire != NULL) { 13871 if ((new_ire != ire) && (new_ire->ire_rfq != NULL)) { 13872 ire_refrele(ire); 13873 ire = new_ire; 13874 } else { 13875 ire_refrele(new_ire); 13876 } 13877 return (ire); 13878 } else if ((ire->ire_rfq == NULL) && 13879 (ire->ire_ipversion == IPV4_VERSION)) { 13880 /* 13881 * The best match could have been the original ire which 13882 * was created against an IRE_LOCAL on lo0. In the IPv4 case 13883 * the strict multihoming checks are irrelevant as we consider 13884 * local addresses hosted on lo0 to be interface agnostic. We 13885 * only expect a null ire_rfq on IREs which are associated with 13886 * lo0 hence we can return now. 13887 */ 13888 return (ire); 13889 } 13890 13891 /* 13892 * Chase pointers once and store locally. 13893 */ 13894 ire_ill = (ire->ire_rfq == NULL) ? NULL : 13895 (ill_t *)(ire->ire_rfq->q_ptr); 13896 ifindex = ill->ill_usesrc_ifindex; 13897 13898 /* 13899 * Check if it's a legal address on the 'usesrc' interface. 13900 */ 13901 if ((ifindex != 0) && (ire_ill != NULL) && 13902 (ifindex == ire_ill->ill_phyint->phyint_ifindex)) { 13903 return (ire); 13904 } 13905 13906 /* 13907 * If the ip*_strict_dst_multihoming switch is on then we can 13908 * only accept this packet if the interface is marked as routing. 13909 */ 13910 if (!(strict_check)) 13911 return (ire); 13912 13913 if ((ill->ill_flags & ire->ire_ipif->ipif_ill->ill_flags & 13914 ILLF_ROUTER) != 0) { 13915 return (ire); 13916 } 13917 13918 ire_refrele(ire); 13919 return (NULL); 13920 } 13921 13922 ire_t * 13923 ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) 13924 { 13925 ipha_t *ipha; 13926 ipaddr_t ip_dst, ip_src; 13927 ire_t *src_ire = NULL; 13928 ill_t *stq_ill; 13929 uint_t hlen; 13930 uint_t pkt_len; 13931 uint32_t sum; 13932 queue_t *dev_q; 13933 boolean_t check_multirt = B_FALSE; 13934 ip_stack_t *ipst = ill->ill_ipst; 13935 13936 ipha = (ipha_t *)mp->b_rptr; 13937 13938 /* 13939 * Martian Address Filtering [RFC 1812, Section 5.3.7] 13940 * The loopback address check for both src and dst has already 13941 * been checked in ip_input 13942 */ 13943 ip_dst = ntohl(dst); 13944 ip_src = ntohl(ipha->ipha_src); 13945 13946 if (ip_dst == INADDR_ANY || IN_BADCLASS(ip_dst) || 13947 IN_CLASSD(ip_src)) { 13948 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13949 goto drop; 13950 } 13951 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 13952 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 13953 13954 if (src_ire != NULL) { 13955 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13956 goto drop; 13957 } 13958 13959 13960 /* No ire cache of nexthop. So first create one */ 13961 if (ire == NULL) { 13962 ire = ire_forward(dst, &check_multirt, NULL, NULL, NULL, ipst); 13963 /* 13964 * We only come to ip_fast_forward if ip_cgtp_filter is 13965 * is not set. So upon return from ire_forward 13966 * check_multirt should remain as false. 13967 */ 13968 ASSERT(!check_multirt); 13969 if (ire == NULL) { 13970 /* An attempt was made to forward the packet */ 13971 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 13972 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13973 mp->b_prev = mp->b_next = 0; 13974 /* send icmp unreachable */ 13975 /* Sent by forwarding path, and router is global zone */ 13976 if (ip_source_routed(ipha, ipst)) { 13977 icmp_unreachable(ill->ill_wq, mp, 13978 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, 13979 ipst); 13980 } else { 13981 icmp_unreachable(ill->ill_wq, mp, 13982 ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 13983 ipst); 13984 } 13985 return (ire); 13986 } 13987 } 13988 13989 /* 13990 * Forwarding fastpath exception case: 13991 * If either of the follwoing case is true, we take 13992 * the slowpath 13993 * o forwarding is not enabled 13994 * o incoming and outgoing interface are the same, or the same 13995 * IPMP group 13996 * o corresponding ire is in incomplete state 13997 * o packet needs fragmentation 13998 * 13999 * The codeflow from here on is thus: 14000 * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 14001 */ 14002 pkt_len = ntohs(ipha->ipha_length); 14003 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 14004 if (!(stq_ill->ill_flags & ILLF_ROUTER) || 14005 !(ill->ill_flags & ILLF_ROUTER) || 14006 (ill == stq_ill) || 14007 (ill->ill_group != NULL && ill->ill_group == stq_ill->ill_group) || 14008 (ire->ire_nce == NULL) || 14009 (ire->ire_nce->nce_state != ND_REACHABLE) || 14010 (pkt_len > ire->ire_max_frag) || 14011 ipha->ipha_ttl <= 1) { 14012 ip_rput_process_forward(ill->ill_rq, mp, ire, 14013 ipha, ill, B_FALSE); 14014 return (ire); 14015 } 14016 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14017 14018 DTRACE_PROBE4(ip4__forwarding__start, 14019 ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 14020 14021 FW_HOOKS(ipst->ips_ip4_forwarding_event, 14022 ipst->ips_ipv4firewall_forwarding, 14023 ill, stq_ill, ipha, mp, mp, ipst); 14024 14025 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 14026 14027 if (mp == NULL) 14028 goto drop; 14029 14030 mp->b_datap->db_struioun.cksum.flags = 0; 14031 /* Adjust the checksum to reflect the ttl decrement. */ 14032 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 14033 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 14034 ipha->ipha_ttl--; 14035 14036 dev_q = ire->ire_stq->q_next; 14037 if ((dev_q->q_next != NULL || 14038 dev_q->q_first != NULL) && !canput(dev_q)) { 14039 goto indiscard; 14040 } 14041 14042 hlen = ire->ire_nce->nce_fp_mp != NULL ? 14043 MBLKL(ire->ire_nce->nce_fp_mp) : 0; 14044 14045 if (hlen != 0 || ire->ire_nce->nce_res_mp != NULL) { 14046 mblk_t *mpip = mp; 14047 14048 mp = ip_wput_attach_llhdr(mpip, ire, 0, 0); 14049 if (mp != NULL) { 14050 DTRACE_PROBE4(ip4__physical__out__start, 14051 ill_t *, NULL, ill_t *, stq_ill, 14052 ipha_t *, ipha, mblk_t *, mp); 14053 FW_HOOKS(ipst->ips_ip4_physical_out_event, 14054 ipst->ips_ipv4firewall_physical_out, 14055 NULL, stq_ill, ipha, mp, mpip, ipst); 14056 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, 14057 mp); 14058 if (mp == NULL) 14059 goto drop; 14060 14061 UPDATE_IB_PKT_COUNT(ire); 14062 ire->ire_last_used_time = lbolt; 14063 BUMP_MIB(stq_ill->ill_ip_mib, 14064 ipIfStatsHCOutForwDatagrams); 14065 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 14066 UPDATE_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutOctets, 14067 pkt_len); 14068 putnext(ire->ire_stq, mp); 14069 return (ire); 14070 } 14071 } 14072 14073 indiscard: 14074 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14075 drop: 14076 if (mp != NULL) 14077 freemsg(mp); 14078 if (src_ire != NULL) 14079 ire_refrele(src_ire); 14080 return (ire); 14081 14082 } 14083 14084 /* 14085 * This function is called in the forwarding slowpath, when 14086 * either the ire lacks the link-layer address, or the packet needs 14087 * further processing(eg. fragmentation), before transmission. 14088 */ 14089 14090 static void 14091 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14092 ill_t *ill, boolean_t ll_multicast) 14093 { 14094 ill_group_t *ill_group; 14095 ill_group_t *ire_group; 14096 queue_t *dev_q; 14097 ire_t *src_ire; 14098 ip_stack_t *ipst = ill->ill_ipst; 14099 14100 ASSERT(ire->ire_stq != NULL); 14101 14102 mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ 14103 mp->b_next = NULL; /* ip_rput_noire sets dst here */ 14104 14105 if (ll_multicast != 0) { 14106 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14107 goto drop_pkt; 14108 } 14109 14110 /* 14111 * check if ipha_src is a broadcast address. Note that this 14112 * check is redundant when we get here from ip_fast_forward() 14113 * which has already done this check. However, since we can 14114 * also get here from ip_rput_process_broadcast() or, for 14115 * for the slow path through ip_fast_forward(), we perform 14116 * the check again for code-reusability 14117 */ 14118 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14119 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 14120 if (src_ire != NULL || ntohl(ipha->ipha_dst) == INADDR_ANY || 14121 IN_BADCLASS(ntohl(ipha->ipha_dst))) { 14122 if (src_ire != NULL) 14123 ire_refrele(src_ire); 14124 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14125 ip2dbg(("ip_rput_process_forward: Received packet with" 14126 " bad src/dst address on %s\n", ill->ill_name)); 14127 goto drop_pkt; 14128 } 14129 14130 ill_group = ill->ill_group; 14131 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 14132 /* 14133 * Check if we want to forward this one at this time. 14134 * We allow source routed packets on a host provided that 14135 * they go out the same interface or same interface group 14136 * as they came in on. 14137 * 14138 * XXX To be quicker, we may wish to not chase pointers to 14139 * get the ILLF_ROUTER flag and instead store the 14140 * forwarding policy in the ire. An unfortunate 14141 * side-effect of that would be requiring an ire flush 14142 * whenever the ILLF_ROUTER flag changes. 14143 */ 14144 if (((ill->ill_flags & 14145 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 14146 ILLF_ROUTER) == 0) && 14147 !(ip_source_routed(ipha, ipst) && (ire->ire_rfq == q || 14148 (ill_group != NULL && ill_group == ire_group)))) { 14149 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14150 if (ip_source_routed(ipha, ipst)) { 14151 q = WR(q); 14152 /* 14153 * Clear the indication that this may have 14154 * hardware checksum as we are not using it. 14155 */ 14156 DB_CKSUMFLAGS(mp) = 0; 14157 /* Sent by forwarding path, and router is global zone */ 14158 icmp_unreachable(q, mp, 14159 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); 14160 return; 14161 } 14162 goto drop_pkt; 14163 } 14164 14165 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14166 14167 /* Packet is being forwarded. Turning off hwcksum flag. */ 14168 DB_CKSUMFLAGS(mp) = 0; 14169 if (ipst->ips_ip_g_send_redirects) { 14170 /* 14171 * Check whether the incoming interface and outgoing 14172 * interface is part of the same group. If so, 14173 * send redirects. 14174 * 14175 * Check the source address to see if it originated 14176 * on the same logical subnet it is going back out on. 14177 * If so, we should be able to send it a redirect. 14178 * Avoid sending a redirect if the destination 14179 * is directly connected (i.e., ipha_dst is the same 14180 * as ire_gateway_addr or the ire_addr of the 14181 * nexthop IRE_CACHE ), or if the packet was source 14182 * routed out this interface. 14183 */ 14184 ipaddr_t src, nhop; 14185 mblk_t *mp1; 14186 ire_t *nhop_ire = NULL; 14187 14188 /* 14189 * Check whether ire_rfq and q are from the same ill 14190 * or if they are not same, they at least belong 14191 * to the same group. If so, send redirects. 14192 */ 14193 if ((ire->ire_rfq == q || 14194 (ill_group != NULL && ill_group == ire_group)) && 14195 !ip_source_routed(ipha, ipst)) { 14196 14197 nhop = (ire->ire_gateway_addr != 0 ? 14198 ire->ire_gateway_addr : ire->ire_addr); 14199 14200 if (ipha->ipha_dst == nhop) { 14201 /* 14202 * We avoid sending a redirect if the 14203 * destination is directly connected 14204 * because it is possible that multiple 14205 * IP subnets may have been configured on 14206 * the link, and the source may not 14207 * be on the same subnet as ip destination, 14208 * even though they are on the same 14209 * physical link. 14210 */ 14211 goto sendit; 14212 } 14213 14214 src = ipha->ipha_src; 14215 14216 /* 14217 * We look up the interface ire for the nexthop, 14218 * to see if ipha_src is in the same subnet 14219 * as the nexthop. 14220 * 14221 * Note that, if, in the future, IRE_CACHE entries 14222 * are obsoleted, this lookup will not be needed, 14223 * as the ire passed to this function will be the 14224 * same as the nhop_ire computed below. 14225 */ 14226 nhop_ire = ire_ftable_lookup(nhop, 0, 0, 14227 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 14228 0, NULL, MATCH_IRE_TYPE, ipst); 14229 14230 if (nhop_ire != NULL) { 14231 if ((src & nhop_ire->ire_mask) == 14232 (nhop & nhop_ire->ire_mask)) { 14233 /* 14234 * The source is directly connected. 14235 * Just copy the ip header (which is 14236 * in the first mblk) 14237 */ 14238 mp1 = copyb(mp); 14239 if (mp1 != NULL) { 14240 icmp_send_redirect(WR(q), mp1, 14241 nhop, ipst); 14242 } 14243 } 14244 ire_refrele(nhop_ire); 14245 } 14246 } 14247 } 14248 sendit: 14249 dev_q = ire->ire_stq->q_next; 14250 if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { 14251 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14252 freemsg(mp); 14253 return; 14254 } 14255 14256 ip_rput_forward(ire, ipha, mp, ill); 14257 return; 14258 14259 drop_pkt: 14260 ip2dbg(("ip_rput_process_forward: drop pkt\n")); 14261 freemsg(mp); 14262 } 14263 14264 ire_t * 14265 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14266 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 14267 { 14268 queue_t *q; 14269 uint16_t hcksumflags; 14270 ip_stack_t *ipst = ill->ill_ipst; 14271 14272 q = *qp; 14273 14274 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts); 14275 14276 /* 14277 * Clear the indication that this may have hardware 14278 * checksum as we are not using it for forwarding. 14279 */ 14280 hcksumflags = DB_CKSUMFLAGS(mp); 14281 DB_CKSUMFLAGS(mp) = 0; 14282 14283 /* 14284 * Directed broadcast forwarding: if the packet came in over a 14285 * different interface then it is routed out over we can forward it. 14286 */ 14287 if (ipha->ipha_protocol == IPPROTO_TCP) { 14288 ire_refrele(ire); 14289 freemsg(mp); 14290 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14291 return (NULL); 14292 } 14293 /* 14294 * For multicast we have set dst to be INADDR_BROADCAST 14295 * for delivering to all STREAMS. IRE_MARK_NORECV is really 14296 * only for broadcast packets. 14297 */ 14298 if (!CLASSD(ipha->ipha_dst)) { 14299 ire_t *new_ire; 14300 ipif_t *ipif; 14301 /* 14302 * For ill groups, as the switch duplicates broadcasts 14303 * across all the ports, we need to filter out and 14304 * send up only one copy. There is one copy for every 14305 * broadcast address on each ill. Thus, we look for a 14306 * specific IRE on this ill and look at IRE_MARK_NORECV 14307 * later to see whether this ill is eligible to receive 14308 * them or not. ill_nominate_bcast_rcv() nominates only 14309 * one set of IREs for receiving. 14310 */ 14311 14312 ipif = ipif_get_next_ipif(NULL, ill); 14313 if (ipif == NULL) { 14314 ire_refrele(ire); 14315 freemsg(mp); 14316 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14317 return (NULL); 14318 } 14319 new_ire = ire_ctable_lookup(dst, 0, 0, 14320 ipif, ALL_ZONES, NULL, MATCH_IRE_ILL, ipst); 14321 ipif_refrele(ipif); 14322 14323 if (new_ire != NULL) { 14324 if (new_ire->ire_marks & IRE_MARK_NORECV) { 14325 ire_refrele(ire); 14326 ire_refrele(new_ire); 14327 freemsg(mp); 14328 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14329 return (NULL); 14330 } 14331 /* 14332 * In the special case of multirouted broadcast 14333 * packets, we unconditionally need to "gateway" 14334 * them to the appropriate interface here. 14335 * In the normal case, this cannot happen, because 14336 * there is no broadcast IRE tagged with the 14337 * RTF_MULTIRT flag. 14338 */ 14339 if (new_ire->ire_flags & RTF_MULTIRT) { 14340 ire_refrele(new_ire); 14341 if (ire->ire_rfq != NULL) { 14342 q = ire->ire_rfq; 14343 *qp = q; 14344 } 14345 } else { 14346 ire_refrele(ire); 14347 ire = new_ire; 14348 } 14349 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 14350 if (!ipst->ips_ip_g_forward_directed_bcast) { 14351 /* 14352 * Free the message if 14353 * ip_g_forward_directed_bcast is turned 14354 * off for non-local broadcast. 14355 */ 14356 ire_refrele(ire); 14357 freemsg(mp); 14358 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14359 return (NULL); 14360 } 14361 } else { 14362 /* 14363 * This CGTP packet successfully passed the 14364 * CGTP filter, but the related CGTP 14365 * broadcast IRE has not been found, 14366 * meaning that the redundant ipif is 14367 * probably down. However, if we discarded 14368 * this packet, its duplicate would be 14369 * filtered out by the CGTP filter so none 14370 * of them would get through. So we keep 14371 * going with this one. 14372 */ 14373 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 14374 if (ire->ire_rfq != NULL) { 14375 q = ire->ire_rfq; 14376 *qp = q; 14377 } 14378 } 14379 } 14380 if (ipst->ips_ip_g_forward_directed_bcast && ll_multicast == 0) { 14381 /* 14382 * Verify that there are not more then one 14383 * IRE_BROADCAST with this broadcast address which 14384 * has ire_stq set. 14385 * TODO: simplify, loop over all IRE's 14386 */ 14387 ire_t *ire1; 14388 int num_stq = 0; 14389 mblk_t *mp1; 14390 14391 /* Find the first one with ire_stq set */ 14392 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 14393 for (ire1 = ire; ire1 && 14394 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 14395 ire1 = ire1->ire_next) 14396 ; 14397 if (ire1) { 14398 ire_refrele(ire); 14399 ire = ire1; 14400 IRE_REFHOLD(ire); 14401 } 14402 14403 /* Check if there are additional ones with stq set */ 14404 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 14405 if (ire->ire_addr != ire1->ire_addr) 14406 break; 14407 if (ire1->ire_stq) { 14408 num_stq++; 14409 break; 14410 } 14411 } 14412 rw_exit(&ire->ire_bucket->irb_lock); 14413 if (num_stq == 1 && ire->ire_stq != NULL) { 14414 ip1dbg(("ip_rput_process_broadcast: directed " 14415 "broadcast to 0x%x\n", 14416 ntohl(ire->ire_addr))); 14417 mp1 = copymsg(mp); 14418 if (mp1) { 14419 switch (ipha->ipha_protocol) { 14420 case IPPROTO_UDP: 14421 ip_udp_input(q, mp1, ipha, ire, ill); 14422 break; 14423 default: 14424 ip_proto_input(q, mp1, ipha, ire, ill, 14425 B_FALSE); 14426 break; 14427 } 14428 } 14429 /* 14430 * Adjust ttl to 2 (1+1 - the forward engine 14431 * will decrement it by one. 14432 */ 14433 if (ip_csum_hdr(ipha)) { 14434 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 14435 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 14436 freemsg(mp); 14437 ire_refrele(ire); 14438 return (NULL); 14439 } 14440 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1; 14441 ipha->ipha_hdr_checksum = 0; 14442 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14443 ip_rput_process_forward(q, mp, ire, ipha, 14444 ill, ll_multicast); 14445 ire_refrele(ire); 14446 return (NULL); 14447 } 14448 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 14449 ntohl(ire->ire_addr))); 14450 } 14451 14452 14453 /* Restore any hardware checksum flags */ 14454 DB_CKSUMFLAGS(mp) = hcksumflags; 14455 return (ire); 14456 } 14457 14458 /* ARGSUSED */ 14459 static boolean_t 14460 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 14461 int *ll_multicast, ipaddr_t *dstp) 14462 { 14463 ip_stack_t *ipst = ill->ill_ipst; 14464 14465 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts); 14466 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, 14467 ntohs(ipha->ipha_length)); 14468 14469 /* 14470 * Forward packets only if we have joined the allmulti 14471 * group on this interface. 14472 */ 14473 if (ipst->ips_ip_g_mrouter && ill->ill_join_allmulti) { 14474 int retval; 14475 14476 /* 14477 * Clear the indication that this may have hardware 14478 * checksum as we are not using it. 14479 */ 14480 DB_CKSUMFLAGS(mp) = 0; 14481 retval = ip_mforward(ill, ipha, mp); 14482 /* ip_mforward updates mib variables if needed */ 14483 /* clear b_prev - used by ip_mroute_decap */ 14484 mp->b_prev = NULL; 14485 14486 switch (retval) { 14487 case 0: 14488 /* 14489 * pkt is okay and arrived on phyint. 14490 * 14491 * If we are running as a multicast router 14492 * we need to see all IGMP and/or PIM packets. 14493 */ 14494 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 14495 (ipha->ipha_protocol == IPPROTO_PIM)) { 14496 goto done; 14497 } 14498 break; 14499 case -1: 14500 /* pkt is mal-formed, toss it */ 14501 goto drop_pkt; 14502 case 1: 14503 /* pkt is okay and arrived on a tunnel */ 14504 /* 14505 * If we are running a multicast router 14506 * we need to see all igmp packets. 14507 */ 14508 if (ipha->ipha_protocol == IPPROTO_IGMP) { 14509 *dstp = INADDR_BROADCAST; 14510 *ll_multicast = 1; 14511 return (B_FALSE); 14512 } 14513 14514 goto drop_pkt; 14515 } 14516 } 14517 14518 ILM_WALKER_HOLD(ill); 14519 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 14520 /* 14521 * This might just be caused by the fact that 14522 * multiple IP Multicast addresses map to the same 14523 * link layer multicast - no need to increment counter! 14524 */ 14525 ILM_WALKER_RELE(ill); 14526 freemsg(mp); 14527 return (B_TRUE); 14528 } 14529 ILM_WALKER_RELE(ill); 14530 done: 14531 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 14532 /* 14533 * This assumes the we deliver to all streams for multicast 14534 * and broadcast packets. 14535 */ 14536 *dstp = INADDR_BROADCAST; 14537 *ll_multicast = 1; 14538 return (B_FALSE); 14539 drop_pkt: 14540 ip2dbg(("ip_rput: drop pkt\n")); 14541 freemsg(mp); 14542 return (B_TRUE); 14543 } 14544 14545 static boolean_t 14546 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 14547 int *ll_multicast, mblk_t **mpp) 14548 { 14549 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 14550 boolean_t must_copy = B_FALSE; 14551 struct iocblk *iocp; 14552 ipha_t *ipha; 14553 ip_stack_t *ipst = ill->ill_ipst; 14554 14555 #define rptr ((uchar_t *)ipha) 14556 14557 first_mp = *first_mpp; 14558 mp = *mpp; 14559 14560 ASSERT(first_mp == mp); 14561 14562 /* 14563 * if db_ref > 1 then copymsg and free original. Packet may be 14564 * changed and do not want other entity who has a reference to this 14565 * message to trip over the changes. This is a blind change because 14566 * trying to catch all places that might change packet is too 14567 * difficult (since it may be a module above this one) 14568 * 14569 * This corresponds to the non-fast path case. We walk down the full 14570 * chain in this case, and check the db_ref count of all the dblks, 14571 * and do a copymsg if required. It is possible that the db_ref counts 14572 * of the data blocks in the mblk chain can be different. 14573 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 14574 * count of 1, followed by a M_DATA block with a ref count of 2, if 14575 * 'snoop' is running. 14576 */ 14577 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 14578 if (mp1->b_datap->db_ref > 1) { 14579 must_copy = B_TRUE; 14580 break; 14581 } 14582 } 14583 14584 if (must_copy) { 14585 mp1 = copymsg(mp); 14586 if (mp1 == NULL) { 14587 for (mp1 = mp; mp1 != NULL; 14588 mp1 = mp1->b_cont) { 14589 mp1->b_next = NULL; 14590 mp1->b_prev = NULL; 14591 } 14592 freemsg(mp); 14593 if (ill != NULL) { 14594 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14595 } else { 14596 BUMP_MIB(&ipst->ips_ip_mib, 14597 ipIfStatsInDiscards); 14598 } 14599 return (B_TRUE); 14600 } 14601 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 14602 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 14603 /* Copy b_prev - used by ip_mroute_decap */ 14604 to_mp->b_prev = from_mp->b_prev; 14605 from_mp->b_prev = NULL; 14606 } 14607 *first_mpp = first_mp = mp1; 14608 freemsg(mp); 14609 mp = mp1; 14610 *mpp = mp1; 14611 } 14612 14613 ipha = (ipha_t *)mp->b_rptr; 14614 14615 /* 14616 * previous code has a case for M_DATA. 14617 * We want to check how that happens. 14618 */ 14619 ASSERT(first_mp->b_datap->db_type != M_DATA); 14620 switch (first_mp->b_datap->db_type) { 14621 case M_PROTO: 14622 case M_PCPROTO: 14623 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 14624 DL_UNITDATA_IND) { 14625 /* Go handle anything other than data elsewhere. */ 14626 ip_rput_dlpi(q, mp); 14627 return (B_TRUE); 14628 } 14629 *ll_multicast = ((dl_unitdata_ind_t *)rptr)->dl_group_address; 14630 /* Ditch the DLPI header. */ 14631 mp1 = mp->b_cont; 14632 ASSERT(first_mp == mp); 14633 *first_mpp = mp1; 14634 freeb(mp); 14635 *mpp = mp1; 14636 return (B_FALSE); 14637 case M_IOCACK: 14638 ip1dbg(("got iocack ")); 14639 iocp = (struct iocblk *)mp->b_rptr; 14640 switch (iocp->ioc_cmd) { 14641 case DL_IOC_HDR_INFO: 14642 ill = (ill_t *)q->q_ptr; 14643 ill_fastpath_ack(ill, mp); 14644 return (B_TRUE); 14645 case SIOCSTUNPARAM: 14646 case OSIOCSTUNPARAM: 14647 /* Go through qwriter_ip */ 14648 break; 14649 case SIOCGTUNPARAM: 14650 case OSIOCGTUNPARAM: 14651 ip_rput_other(NULL, q, mp, NULL); 14652 return (B_TRUE); 14653 default: 14654 putnext(q, mp); 14655 return (B_TRUE); 14656 } 14657 /* FALLTHRU */ 14658 case M_ERROR: 14659 case M_HANGUP: 14660 /* 14661 * Since this is on the ill stream we unconditionally 14662 * bump up the refcount 14663 */ 14664 ill_refhold(ill); 14665 qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); 14666 return (B_TRUE); 14667 case M_CTL: 14668 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 14669 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 14670 IPHADA_M_CTL)) { 14671 /* 14672 * It's an IPsec accelerated packet. 14673 * Make sure that the ill from which we received the 14674 * packet has enabled IPsec hardware acceleration. 14675 */ 14676 if (!(ill->ill_capabilities & 14677 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 14678 /* IPsec kstats: bean counter */ 14679 freemsg(mp); 14680 return (B_TRUE); 14681 } 14682 14683 /* 14684 * Make mp point to the mblk following the M_CTL, 14685 * then process according to type of mp. 14686 * After this processing, first_mp will point to 14687 * the data-attributes and mp to the pkt following 14688 * the M_CTL. 14689 */ 14690 mp = first_mp->b_cont; 14691 if (mp == NULL) { 14692 freemsg(first_mp); 14693 return (B_TRUE); 14694 } 14695 /* 14696 * A Hardware Accelerated packet can only be M_DATA 14697 * ESP or AH packet. 14698 */ 14699 if (mp->b_datap->db_type != M_DATA) { 14700 /* non-M_DATA IPsec accelerated packet */ 14701 IPSECHW_DEBUG(IPSECHW_PKT, 14702 ("non-M_DATA IPsec accelerated pkt\n")); 14703 freemsg(first_mp); 14704 return (B_TRUE); 14705 } 14706 ipha = (ipha_t *)mp->b_rptr; 14707 if (ipha->ipha_protocol != IPPROTO_AH && 14708 ipha->ipha_protocol != IPPROTO_ESP) { 14709 IPSECHW_DEBUG(IPSECHW_PKT, 14710 ("non-M_DATA IPsec accelerated pkt\n")); 14711 freemsg(first_mp); 14712 return (B_TRUE); 14713 } 14714 *mpp = mp; 14715 return (B_FALSE); 14716 } 14717 putnext(q, mp); 14718 return (B_TRUE); 14719 case M_IOCNAK: 14720 ip1dbg(("got iocnak ")); 14721 iocp = (struct iocblk *)mp->b_rptr; 14722 switch (iocp->ioc_cmd) { 14723 case SIOCSTUNPARAM: 14724 case OSIOCSTUNPARAM: 14725 /* 14726 * Since this is on the ill stream we unconditionally 14727 * bump up the refcount 14728 */ 14729 ill_refhold(ill); 14730 qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); 14731 return (B_TRUE); 14732 case DL_IOC_HDR_INFO: 14733 case SIOCGTUNPARAM: 14734 case OSIOCGTUNPARAM: 14735 ip_rput_other(NULL, q, mp, NULL); 14736 return (B_TRUE); 14737 default: 14738 break; 14739 } 14740 /* FALLTHRU */ 14741 default: 14742 putnext(q, mp); 14743 return (B_TRUE); 14744 } 14745 } 14746 14747 /* Read side put procedure. Packets coming from the wire arrive here. */ 14748 void 14749 ip_rput(queue_t *q, mblk_t *mp) 14750 { 14751 ill_t *ill; 14752 union DL_primitives *dl; 14753 14754 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 14755 14756 ill = (ill_t *)q->q_ptr; 14757 14758 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 14759 /* 14760 * If things are opening or closing, only accept high-priority 14761 * DLPI messages. (On open ill->ill_ipif has not yet been 14762 * created; on close, things hanging off the ill may have been 14763 * freed already.) 14764 */ 14765 dl = (union DL_primitives *)mp->b_rptr; 14766 if (DB_TYPE(mp) != M_PCPROTO || 14767 dl->dl_primitive == DL_UNITDATA_IND) { 14768 /* 14769 * SIOC[GS]TUNPARAM ioctls can come here. 14770 */ 14771 inet_freemsg(mp); 14772 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14773 "ip_rput_end: q %p (%S)", q, "uninit"); 14774 return; 14775 } 14776 } 14777 14778 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14779 "ip_rput_end: q %p (%S)", q, "end"); 14780 14781 ip_input(ill, NULL, mp, NULL); 14782 } 14783 14784 static mblk_t * 14785 ip_fix_dbref(ill_t *ill, mblk_t *mp) 14786 { 14787 mblk_t *mp1; 14788 boolean_t adjusted = B_FALSE; 14789 ip_stack_t *ipst = ill->ill_ipst; 14790 14791 IP_STAT(ipst, ip_db_ref); 14792 /* 14793 * The IP_RECVSLLA option depends on having the 14794 * link layer header. First check that: 14795 * a> the underlying device is of type ether, 14796 * since this option is currently supported only 14797 * over ethernet. 14798 * b> there is enough room to copy over the link 14799 * layer header. 14800 * 14801 * Once the checks are done, adjust rptr so that 14802 * the link layer header will be copied via 14803 * copymsg. Note that, IFT_ETHER may be returned 14804 * by some non-ethernet drivers but in this case 14805 * the second check will fail. 14806 */ 14807 if (ill->ill_type == IFT_ETHER && 14808 (mp->b_rptr - mp->b_datap->db_base) >= 14809 sizeof (struct ether_header)) { 14810 mp->b_rptr -= sizeof (struct ether_header); 14811 adjusted = B_TRUE; 14812 } 14813 mp1 = copymsg(mp); 14814 14815 if (mp1 == NULL) { 14816 mp->b_next = NULL; 14817 /* clear b_prev - used by ip_mroute_decap */ 14818 mp->b_prev = NULL; 14819 freemsg(mp); 14820 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14821 return (NULL); 14822 } 14823 14824 if (adjusted) { 14825 /* 14826 * Copy is done. Restore the pointer in 14827 * the _new_ mblk 14828 */ 14829 mp1->b_rptr += sizeof (struct ether_header); 14830 } 14831 14832 /* Copy b_prev - used by ip_mroute_decap */ 14833 mp1->b_prev = mp->b_prev; 14834 mp->b_prev = NULL; 14835 14836 /* preserve the hardware checksum flags and data, if present */ 14837 if (DB_CKSUMFLAGS(mp) != 0) { 14838 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp); 14839 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp); 14840 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp); 14841 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp); 14842 DB_CKSUM16(mp1) = DB_CKSUM16(mp); 14843 } 14844 14845 freemsg(mp); 14846 return (mp1); 14847 } 14848 14849 /* 14850 * Direct read side procedure capable of dealing with chains. GLDv3 based 14851 * drivers call this function directly with mblk chains while STREAMS 14852 * read side procedure ip_rput() calls this for single packet with ip_ring 14853 * set to NULL to process one packet at a time. 14854 * 14855 * The ill will always be valid if this function is called directly from 14856 * the driver. 14857 * 14858 * If ip_input() is called from GLDv3: 14859 * 14860 * - This must be a non-VLAN IP stream. 14861 * - 'mp' is either an untagged or a special priority-tagged packet. 14862 * - Any VLAN tag that was in the MAC header has been stripped. 14863 * 14864 * If the IP header in packet is not 32-bit aligned, every message in the 14865 * chain will be aligned before further operations. This is required on SPARC 14866 * platform. 14867 */ 14868 /* ARGSUSED */ 14869 void 14870 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, 14871 struct mac_header_info_s *mhip) 14872 { 14873 ipaddr_t dst = NULL; 14874 ipaddr_t prev_dst; 14875 ire_t *ire = NULL; 14876 ipha_t *ipha; 14877 uint_t pkt_len; 14878 ssize_t len; 14879 uint_t opt_len; 14880 int ll_multicast; 14881 int cgtp_flt_pkt; 14882 queue_t *q = ill->ill_rq; 14883 squeue_t *curr_sqp = NULL; 14884 mblk_t *head = NULL; 14885 mblk_t *tail = NULL; 14886 mblk_t *first_mp; 14887 mblk_t *mp; 14888 mblk_t *dmp; 14889 int cnt = 0; 14890 ip_stack_t *ipst = ill->ill_ipst; 14891 14892 ASSERT(mp_chain != NULL); 14893 ASSERT(ill != NULL); 14894 14895 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 14896 14897 #define rptr ((uchar_t *)ipha) 14898 14899 while (mp_chain != NULL) { 14900 first_mp = mp = mp_chain; 14901 mp_chain = mp_chain->b_next; 14902 mp->b_next = NULL; 14903 ll_multicast = 0; 14904 14905 /* 14906 * We do ire caching from one iteration to 14907 * another. In the event the packet chain contains 14908 * all packets from the same dst, this caching saves 14909 * an ire_cache_lookup for each of the succeeding 14910 * packets in a packet chain. 14911 */ 14912 prev_dst = dst; 14913 14914 /* 14915 * if db_ref > 1 then copymsg and free original. Packet 14916 * may be changed and we do not want the other entity 14917 * who has a reference to this message to trip over the 14918 * changes. This is a blind change because trying to 14919 * catch all places that might change the packet is too 14920 * difficult. 14921 * 14922 * This corresponds to the fast path case, where we have 14923 * a chain of M_DATA mblks. We check the db_ref count 14924 * of only the 1st data block in the mblk chain. There 14925 * doesn't seem to be a reason why a device driver would 14926 * send up data with varying db_ref counts in the mblk 14927 * chain. In any case the Fast path is a private 14928 * interface, and our drivers don't do such a thing. 14929 * Given the above assumption, there is no need to walk 14930 * down the entire mblk chain (which could have a 14931 * potential performance problem) 14932 */ 14933 14934 if (DB_REF(mp) > 1) { 14935 if ((mp = ip_fix_dbref(ill, mp)) == NULL) 14936 continue; 14937 } 14938 14939 /* 14940 * Check and align the IP header. 14941 */ 14942 first_mp = mp; 14943 if (DB_TYPE(mp) == M_DATA) { 14944 dmp = mp; 14945 } else if (DB_TYPE(mp) == M_PROTO && 14946 *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { 14947 dmp = mp->b_cont; 14948 } else { 14949 dmp = NULL; 14950 } 14951 if (dmp != NULL) { 14952 /* 14953 * IP header ptr not aligned? 14954 * OR IP header not complete in first mblk 14955 */ 14956 if (!OK_32PTR(dmp->b_rptr) || 14957 MBLKL(dmp) < IP_SIMPLE_HDR_LENGTH) { 14958 if (!ip_check_and_align_header(q, dmp, ipst)) 14959 continue; 14960 } 14961 } 14962 14963 /* 14964 * ip_input fast path 14965 */ 14966 14967 /* mblk type is not M_DATA */ 14968 if (DB_TYPE(mp) != M_DATA) { 14969 if (ip_rput_process_notdata(q, &first_mp, ill, 14970 &ll_multicast, &mp)) 14971 continue; 14972 } 14973 14974 /* Make sure its an M_DATA and that its aligned */ 14975 ASSERT(DB_TYPE(mp) == M_DATA); 14976 ASSERT(DB_REF(mp) == 1 && OK_32PTR(mp->b_rptr)); 14977 14978 ipha = (ipha_t *)mp->b_rptr; 14979 len = mp->b_wptr - rptr; 14980 pkt_len = ntohs(ipha->ipha_length); 14981 14982 /* 14983 * We must count all incoming packets, even if they end 14984 * up being dropped later on. 14985 */ 14986 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 14987 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); 14988 14989 /* multiple mblk or too short */ 14990 len -= pkt_len; 14991 if (len != 0) { 14992 /* 14993 * Make sure we have data length consistent 14994 * with the IP header. 14995 */ 14996 if (mp->b_cont == NULL) { 14997 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 14998 BUMP_MIB(ill->ill_ip_mib, 14999 ipIfStatsInHdrErrors); 15000 ip2dbg(("ip_input: drop pkt\n")); 15001 freemsg(mp); 15002 continue; 15003 } 15004 mp->b_wptr = rptr + pkt_len; 15005 } else if ((len += msgdsize(mp->b_cont)) != 0) { 15006 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15007 BUMP_MIB(ill->ill_ip_mib, 15008 ipIfStatsInHdrErrors); 15009 ip2dbg(("ip_input: drop pkt\n")); 15010 freemsg(mp); 15011 continue; 15012 } 15013 (void) adjmsg(mp, -len); 15014 IP_STAT(ipst, ip_multimblk3); 15015 } 15016 } 15017 15018 /* Obtain the dst of the current packet */ 15019 dst = ipha->ipha_dst; 15020 15021 if (IP_LOOPBACK_ADDR(dst) || 15022 IP_LOOPBACK_ADDR(ipha->ipha_src)) { 15023 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors); 15024 cmn_err(CE_CONT, "dst %X src %X\n", 15025 dst, ipha->ipha_src); 15026 freemsg(mp); 15027 continue; 15028 } 15029 15030 /* 15031 * The event for packets being received from a 'physical' 15032 * interface is placed after validation of the source and/or 15033 * destination address as being local so that packets can be 15034 * redirected to loopback addresses using ipnat. 15035 */ 15036 DTRACE_PROBE4(ip4__physical__in__start, 15037 ill_t *, ill, ill_t *, NULL, 15038 ipha_t *, ipha, mblk_t *, first_mp); 15039 15040 FW_HOOKS(ipst->ips_ip4_physical_in_event, 15041 ipst->ips_ipv4firewall_physical_in, 15042 ill, NULL, ipha, first_mp, mp, ipst); 15043 15044 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 15045 15046 if (first_mp == NULL) { 15047 continue; 15048 } 15049 dst = ipha->ipha_dst; 15050 15051 /* 15052 * Attach any necessary label information to 15053 * this packet 15054 */ 15055 if (is_system_labeled() && 15056 !tsol_get_pkt_label(mp, IPV4_VERSION)) { 15057 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 15058 freemsg(mp); 15059 continue; 15060 } 15061 15062 /* 15063 * Reuse the cached ire only if the ipha_dst of the previous 15064 * packet is the same as the current packet AND it is not 15065 * INADDR_ANY. 15066 */ 15067 if (!(dst == prev_dst && dst != INADDR_ANY) && 15068 (ire != NULL)) { 15069 ire_refrele(ire); 15070 ire = NULL; 15071 } 15072 opt_len = ipha->ipha_version_and_hdr_length - 15073 IP_SIMPLE_HDR_VERSION; 15074 15075 /* 15076 * Check to see if we can take the fastpath. 15077 * That is possible if the following conditions are met 15078 * o Tsol disabled 15079 * o CGTP disabled 15080 * o ipp_action_count is 0 15081 * o no options in the packet 15082 * o not a RSVP packet 15083 * o not a multicast packet 15084 */ 15085 if (!is_system_labeled() && 15086 !ipst->ips_ip_cgtp_filter && ipp_action_count == 0 && 15087 opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && 15088 !ll_multicast && !CLASSD(dst)) { 15089 if (ire == NULL) 15090 ire = ire_cache_lookup(dst, ALL_ZONES, NULL, 15091 ipst); 15092 15093 /* incoming packet is for forwarding */ 15094 if (ire == NULL || (ire->ire_type & IRE_CACHE)) { 15095 ire = ip_fast_forward(ire, dst, ill, mp); 15096 continue; 15097 } 15098 /* incoming packet is for local consumption */ 15099 if (ire->ire_type & IRE_LOCAL) 15100 goto local; 15101 } 15102 15103 /* 15104 * Disable ire caching for anything more complex 15105 * than the simple fast path case we checked for above. 15106 */ 15107 if (ire != NULL) { 15108 ire_refrele(ire); 15109 ire = NULL; 15110 } 15111 15112 /* Full-blown slow path */ 15113 if (opt_len != 0) { 15114 if (len != 0) 15115 IP_STAT(ipst, ip_multimblk4); 15116 else 15117 IP_STAT(ipst, ip_ipoptions); 15118 if (!ip_rput_multimblk_ipoptions(q, ill, mp, &ipha, 15119 &dst, ipst)) 15120 continue; 15121 } 15122 15123 /* 15124 * Invoke the CGTP (multirouting) filtering module to process 15125 * the incoming packet. Packets identified as duplicates 15126 * must be discarded. Filtering is active only if the 15127 * the ip_cgtp_filter ndd variable is non-zero. 15128 */ 15129 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 15130 if (ipst->ips_ip_cgtp_filter && 15131 ipst->ips_ip_cgtp_filter_ops != NULL) { 15132 netstackid_t stackid; 15133 15134 stackid = ipst->ips_netstack->netstack_stackid; 15135 cgtp_flt_pkt = 15136 ipst->ips_ip_cgtp_filter_ops->cfo_filter(stackid, 15137 ill->ill_phyint->phyint_ifindex, mp); 15138 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 15139 freemsg(first_mp); 15140 continue; 15141 } 15142 } 15143 15144 /* 15145 * If rsvpd is running, let RSVP daemon handle its processing 15146 * and forwarding of RSVP multicast/unicast packets. 15147 * If rsvpd is not running but mrouted is running, RSVP 15148 * multicast packets are forwarded as multicast traffic 15149 * and RSVP unicast packets are forwarded by unicast router. 15150 * If neither rsvpd nor mrouted is running, RSVP multicast 15151 * packets are not forwarded, but the unicast packets are 15152 * forwarded like unicast traffic. 15153 */ 15154 if (ipha->ipha_protocol == IPPROTO_RSVP && 15155 ipst->ips_ipcl_proto_fanout[IPPROTO_RSVP].connf_head != 15156 NULL) { 15157 /* RSVP packet and rsvpd running. Treat as ours */ 15158 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 15159 /* 15160 * This assumes that we deliver to all streams for 15161 * multicast and broadcast packets. 15162 * We have to force ll_multicast to 1 to handle the 15163 * M_DATA messages passed in from ip_mroute_decap. 15164 */ 15165 dst = INADDR_BROADCAST; 15166 ll_multicast = 1; 15167 } else if (CLASSD(dst)) { 15168 /* packet is multicast */ 15169 mp->b_next = NULL; 15170 if (ip_rput_process_multicast(q, mp, ill, ipha, 15171 &ll_multicast, &dst)) 15172 continue; 15173 } 15174 15175 if (ire == NULL) { 15176 ire = ire_cache_lookup(dst, ALL_ZONES, 15177 MBLK_GETLABEL(mp), ipst); 15178 } 15179 15180 if (ire == NULL) { 15181 /* 15182 * No IRE for this destination, so it can't be for us. 15183 * Unless we are forwarding, drop the packet. 15184 * We have to let source routed packets through 15185 * since we don't yet know if they are 'ping -l' 15186 * packets i.e. if they will go out over the 15187 * same interface as they came in on. 15188 */ 15189 ire = ip_rput_noire(q, mp, ll_multicast, dst); 15190 if (ire == NULL) 15191 continue; 15192 } 15193 15194 /* 15195 * Broadcast IRE may indicate either broadcast or 15196 * multicast packet 15197 */ 15198 if (ire->ire_type == IRE_BROADCAST) { 15199 /* 15200 * Skip broadcast checks if packet is UDP multicast; 15201 * we'd rather not enter ip_rput_process_broadcast() 15202 * unless the packet is broadcast for real, since 15203 * that routine is a no-op for multicast. 15204 */ 15205 if (ipha->ipha_protocol != IPPROTO_UDP || 15206 !CLASSD(ipha->ipha_dst)) { 15207 ire = ip_rput_process_broadcast(&q, mp, 15208 ire, ipha, ill, dst, cgtp_flt_pkt, 15209 ll_multicast); 15210 if (ire == NULL) 15211 continue; 15212 } 15213 } else if (ire->ire_stq != NULL) { 15214 /* fowarding? */ 15215 ip_rput_process_forward(q, mp, ire, ipha, ill, 15216 ll_multicast); 15217 /* ip_rput_process_forward consumed the packet */ 15218 continue; 15219 } 15220 15221 local: 15222 /* 15223 * If the queue in the ire is different to the ingress queue 15224 * then we need to check to see if we can accept the packet. 15225 * Note that for multicast packets and broadcast packets sent 15226 * to a broadcast address which is shared between multiple 15227 * interfaces we should not do this since we just got a random 15228 * broadcast ire. 15229 */ 15230 if ((ire->ire_rfq != q) && (ire->ire_type != IRE_BROADCAST)) { 15231 if ((ire = ip_check_multihome(&ipha->ipha_dst, ire, 15232 ill)) == NULL) { 15233 /* Drop packet */ 15234 BUMP_MIB(ill->ill_ip_mib, 15235 ipIfStatsForwProhibits); 15236 freemsg(mp); 15237 continue; 15238 } 15239 if (ire->ire_rfq != NULL) 15240 q = ire->ire_rfq; 15241 } 15242 15243 switch (ipha->ipha_protocol) { 15244 case IPPROTO_TCP: 15245 ASSERT(first_mp == mp); 15246 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 15247 mp, 0, q, ip_ring)) != NULL) { 15248 if (curr_sqp == NULL) { 15249 curr_sqp = GET_SQUEUE(mp); 15250 ASSERT(cnt == 0); 15251 cnt++; 15252 head = tail = mp; 15253 } else if (curr_sqp == GET_SQUEUE(mp)) { 15254 ASSERT(tail != NULL); 15255 cnt++; 15256 tail->b_next = mp; 15257 tail = mp; 15258 } else { 15259 /* 15260 * A different squeue. Send the 15261 * chain for the previous squeue on 15262 * its way. This shouldn't happen 15263 * often unless interrupt binding 15264 * changes. 15265 */ 15266 IP_STAT(ipst, ip_input_multi_squeue); 15267 squeue_enter_chain(curr_sqp, head, 15268 tail, cnt, SQTAG_IP_INPUT); 15269 curr_sqp = GET_SQUEUE(mp); 15270 head = mp; 15271 tail = mp; 15272 cnt = 1; 15273 } 15274 } 15275 continue; 15276 case IPPROTO_UDP: 15277 ASSERT(first_mp == mp); 15278 ip_udp_input(q, mp, ipha, ire, ill); 15279 continue; 15280 case IPPROTO_SCTP: 15281 ASSERT(first_mp == mp); 15282 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 15283 q, dst); 15284 /* ire has been released by ip_sctp_input */ 15285 ire = NULL; 15286 continue; 15287 default: 15288 ip_proto_input(q, first_mp, ipha, ire, ill, B_FALSE); 15289 continue; 15290 } 15291 } 15292 15293 if (ire != NULL) 15294 ire_refrele(ire); 15295 15296 if (head != NULL) 15297 squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); 15298 15299 /* 15300 * This code is there just to make netperf/ttcp look good. 15301 * 15302 * Its possible that after being in polling mode (and having cleared 15303 * the backlog), squeues have turned the interrupt frequency higher 15304 * to improve latency at the expense of more CPU utilization (less 15305 * packets per interrupts or more number of interrupts). Workloads 15306 * like ttcp/netperf do manage to tickle polling once in a while 15307 * but for the remaining time, stay in higher interrupt mode since 15308 * their packet arrival rate is pretty uniform and this shows up 15309 * as higher CPU utilization. Since people care about CPU utilization 15310 * while running netperf/ttcp, turn the interrupt frequency back to 15311 * normal/default if polling has not been used in ip_poll_normal_ticks. 15312 */ 15313 if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { 15314 if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { 15315 ip_ring->rr_poll_state &= ~ILL_POLLING; 15316 ip_ring->rr_blank(ip_ring->rr_handle, 15317 ip_ring->rr_normal_blank_time, 15318 ip_ring->rr_normal_pkt_cnt); 15319 } 15320 } 15321 15322 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15323 "ip_input_end: q %p (%S)", q, "end"); 15324 #undef rptr 15325 } 15326 15327 static void 15328 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 15329 t_uscalar_t err) 15330 { 15331 if (dl_err == DL_SYSERR) { 15332 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15333 "%s: %s failed: DL_SYSERR (errno %u)\n", 15334 ill->ill_name, dlpi_prim_str(prim), err); 15335 return; 15336 } 15337 15338 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15339 "%s: %s failed: %s\n", ill->ill_name, dlpi_prim_str(prim), 15340 dlpi_err_str(dl_err)); 15341 } 15342 15343 /* 15344 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 15345 * than DL_UNITDATA_IND messages. If we need to process this message 15346 * exclusively, we call qwriter_ip, in which case we also need to call 15347 * ill_refhold before that, since qwriter_ip does an ill_refrele. 15348 */ 15349 void 15350 ip_rput_dlpi(queue_t *q, mblk_t *mp) 15351 { 15352 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15353 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15354 ill_t *ill = (ill_t *)q->q_ptr; 15355 boolean_t pending; 15356 15357 ip1dbg(("ip_rput_dlpi")); 15358 if (dloa->dl_primitive == DL_ERROR_ACK) { 15359 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK %s (0x%x): " 15360 "%s (0x%x), unix %u\n", ill->ill_name, 15361 dlpi_prim_str(dlea->dl_error_primitive), 15362 dlea->dl_error_primitive, 15363 dlpi_err_str(dlea->dl_errno), 15364 dlea->dl_errno, 15365 dlea->dl_unix_errno)); 15366 } 15367 15368 /* 15369 * If we received an ACK but didn't send a request for it, then it 15370 * can't be part of any pending operation; discard up-front. 15371 */ 15372 switch (dloa->dl_primitive) { 15373 case DL_NOTIFY_IND: 15374 pending = B_TRUE; 15375 break; 15376 case DL_ERROR_ACK: 15377 pending = ill_dlpi_pending(ill, dlea->dl_error_primitive); 15378 break; 15379 case DL_OK_ACK: 15380 pending = ill_dlpi_pending(ill, dloa->dl_correct_primitive); 15381 break; 15382 case DL_INFO_ACK: 15383 pending = ill_dlpi_pending(ill, DL_INFO_REQ); 15384 break; 15385 case DL_BIND_ACK: 15386 pending = ill_dlpi_pending(ill, DL_BIND_REQ); 15387 break; 15388 case DL_PHYS_ADDR_ACK: 15389 pending = ill_dlpi_pending(ill, DL_PHYS_ADDR_REQ); 15390 break; 15391 case DL_NOTIFY_ACK: 15392 pending = ill_dlpi_pending(ill, DL_NOTIFY_REQ); 15393 break; 15394 case DL_CONTROL_ACK: 15395 pending = ill_dlpi_pending(ill, DL_CONTROL_REQ); 15396 break; 15397 case DL_CAPABILITY_ACK: 15398 pending = ill_dlpi_pending(ill, DL_CAPABILITY_REQ); 15399 break; 15400 default: 15401 /* Not a DLPI message we support or were expecting */ 15402 freemsg(mp); 15403 return; 15404 } 15405 15406 if (!pending) { 15407 freemsg(mp); 15408 return; 15409 } 15410 15411 switch (dloa->dl_primitive) { 15412 case DL_ERROR_ACK: 15413 if (dlea->dl_error_primitive == DL_UNBIND_REQ) { 15414 mutex_enter(&ill->ill_lock); 15415 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15416 cv_signal(&ill->ill_cv); 15417 mutex_exit(&ill->ill_lock); 15418 } 15419 break; 15420 15421 case DL_OK_ACK: 15422 ip1dbg(("ip_rput: DL_OK_ACK for %s\n", 15423 dlpi_prim_str((int)dloa->dl_correct_primitive))); 15424 switch (dloa->dl_correct_primitive) { 15425 case DL_UNBIND_REQ: 15426 mutex_enter(&ill->ill_lock); 15427 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15428 cv_signal(&ill->ill_cv); 15429 mutex_exit(&ill->ill_lock); 15430 break; 15431 15432 case DL_ENABMULTI_REQ: 15433 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15434 ill->ill_dlpi_multicast_state = IDS_OK; 15435 break; 15436 } 15437 break; 15438 default: 15439 break; 15440 } 15441 15442 /* 15443 * We know the message is one we're waiting for (or DL_NOTIFY_IND), 15444 * and we need to become writer to continue to process it. If it's not 15445 * a DL_NOTIFY_IND, we assume we're in the middle of an exclusive 15446 * operation and pass CUR_OP. If this isn't true, we'll end up doing 15447 * some work as part of the current exclusive operation that actually 15448 * is not part of it -- which is wrong, but better than the 15449 * alternative of deadlock (if NEW_OP is always used). Someday, we 15450 * should track which DLPI requests have ACKs that we wait on 15451 * synchronously so we can know whether to use CUR_OP or NEW_OP. 15452 * 15453 * As required by qwriter_ip(), we refhold the ill; it will refrele. 15454 * Since this is on the ill stream we unconditionally bump up the 15455 * refcount without doing ILL_CAN_LOOKUP(). 15456 */ 15457 ill_refhold(ill); 15458 if (dloa->dl_primitive == DL_NOTIFY_IND) 15459 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE); 15460 else 15461 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE); 15462 } 15463 15464 /* 15465 * Handling of DLPI messages that require exclusive access to the ipsq. 15466 * 15467 * Need to do ill_pending_mp_release on ioctl completion, which could 15468 * happen here. (along with mi_copy_done) 15469 */ 15470 /* ARGSUSED */ 15471 static void 15472 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15473 { 15474 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15475 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15476 int err = 0; 15477 ill_t *ill; 15478 ipif_t *ipif = NULL; 15479 mblk_t *mp1 = NULL; 15480 conn_t *connp = NULL; 15481 t_uscalar_t paddrreq; 15482 mblk_t *mp_hw; 15483 boolean_t success; 15484 boolean_t ioctl_aborted = B_FALSE; 15485 boolean_t log = B_TRUE; 15486 hook_nic_event_t *info; 15487 ip_stack_t *ipst; 15488 15489 ip1dbg(("ip_rput_dlpi_writer ..")); 15490 ill = (ill_t *)q->q_ptr; 15491 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 15492 15493 ASSERT(IAM_WRITER_ILL(ill)); 15494 15495 ipst = ill->ill_ipst; 15496 15497 /* 15498 * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. 15499 * both are null or non-null. However we can assert that only 15500 * after grabbing the ipsq_lock. So we don't make any assertion 15501 * here and in other places in the code. 15502 */ 15503 ipif = ipsq->ipsq_pending_ipif; 15504 /* 15505 * The current ioctl could have been aborted by the user and a new 15506 * ioctl to bring up another ill could have started. We could still 15507 * get a response from the driver later. 15508 */ 15509 if (ipif != NULL && ipif->ipif_ill != ill) 15510 ioctl_aborted = B_TRUE; 15511 15512 switch (dloa->dl_primitive) { 15513 case DL_ERROR_ACK: 15514 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n", 15515 dlpi_prim_str(dlea->dl_error_primitive))); 15516 15517 switch (dlea->dl_error_primitive) { 15518 case DL_PROMISCON_REQ: 15519 case DL_PROMISCOFF_REQ: 15520 case DL_DISABMULTI_REQ: 15521 case DL_UNBIND_REQ: 15522 case DL_ATTACH_REQ: 15523 case DL_INFO_REQ: 15524 ill_dlpi_done(ill, dlea->dl_error_primitive); 15525 break; 15526 case DL_NOTIFY_REQ: 15527 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15528 log = B_FALSE; 15529 break; 15530 case DL_PHYS_ADDR_REQ: 15531 /* 15532 * For IPv6 only, there are two additional 15533 * phys_addr_req's sent to the driver to get the 15534 * IPv6 token and lla. This allows IP to acquire 15535 * the hardware address format for a given interface 15536 * without having built in knowledge of the hardware 15537 * address. ill_phys_addr_pend keeps track of the last 15538 * DL_PAR sent so we know which response we are 15539 * dealing with. ill_dlpi_done will update 15540 * ill_phys_addr_pend when it sends the next req. 15541 * We don't complete the IOCTL until all three DL_PARs 15542 * have been attempted, so set *_len to 0 and break. 15543 */ 15544 paddrreq = ill->ill_phys_addr_pend; 15545 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15546 if (paddrreq == DL_IPV6_TOKEN) { 15547 ill->ill_token_length = 0; 15548 log = B_FALSE; 15549 break; 15550 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 15551 ill->ill_nd_lla_len = 0; 15552 log = B_FALSE; 15553 break; 15554 } 15555 /* 15556 * Something went wrong with the DL_PHYS_ADDR_REQ. 15557 * We presumably have an IOCTL hanging out waiting 15558 * for completion. Find it and complete the IOCTL 15559 * with the error noted. 15560 * However, ill_dl_phys was called on an ill queue 15561 * (from SIOCSLIFNAME), thus conn_pending_ill is not 15562 * set. But the ioctl is known to be pending on ill_wq. 15563 */ 15564 if (!ill->ill_ifname_pending) 15565 break; 15566 ill->ill_ifname_pending = 0; 15567 if (!ioctl_aborted) 15568 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15569 if (mp1 != NULL) { 15570 /* 15571 * This operation (SIOCSLIFNAME) must have 15572 * happened on the ill. Assert there is no conn 15573 */ 15574 ASSERT(connp == NULL); 15575 q = ill->ill_wq; 15576 } 15577 break; 15578 case DL_BIND_REQ: 15579 ill_dlpi_done(ill, DL_BIND_REQ); 15580 if (ill->ill_ifname_pending) 15581 break; 15582 /* 15583 * Something went wrong with the bind. We presumably 15584 * have an IOCTL hanging out waiting for completion. 15585 * Find it, take down the interface that was coming 15586 * up, and complete the IOCTL with the error noted. 15587 */ 15588 if (!ioctl_aborted) 15589 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15590 if (mp1 != NULL) { 15591 /* 15592 * This operation (SIOCSLIFFLAGS) must have 15593 * happened from a conn. 15594 */ 15595 ASSERT(connp != NULL); 15596 q = CONNP_TO_WQ(connp); 15597 if (ill->ill_move_in_progress) { 15598 ILL_CLEAR_MOVE(ill); 15599 } 15600 (void) ipif_down(ipif, NULL, NULL); 15601 /* error is set below the switch */ 15602 } 15603 break; 15604 case DL_ENABMULTI_REQ: 15605 ill_dlpi_done(ill, DL_ENABMULTI_REQ); 15606 15607 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15608 ill->ill_dlpi_multicast_state = IDS_FAILED; 15609 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 15610 ipif_t *ipif; 15611 15612 printf("ip: joining multicasts failed (%d)" 15613 " on %s - will use link layer " 15614 "broadcasts for multicast\n", 15615 dlea->dl_errno, ill->ill_name); 15616 15617 /* 15618 * Set up the multicast mapping alone. 15619 * writer, so ok to access ill->ill_ipif 15620 * without any lock. 15621 */ 15622 ipif = ill->ill_ipif; 15623 mutex_enter(&ill->ill_phyint->phyint_lock); 15624 ill->ill_phyint->phyint_flags |= 15625 PHYI_MULTI_BCAST; 15626 mutex_exit(&ill->ill_phyint->phyint_lock); 15627 15628 if (!ill->ill_isv6) { 15629 (void) ipif_arp_setup_multicast(ipif, 15630 NULL); 15631 } else { 15632 (void) ipif_ndp_setup_multicast(ipif, 15633 NULL); 15634 } 15635 } 15636 freemsg(mp); /* Don't want to pass this up */ 15637 return; 15638 15639 case DL_CAPABILITY_REQ: 15640 case DL_CONTROL_REQ: 15641 ill_dlpi_done(ill, dlea->dl_error_primitive); 15642 ill->ill_dlpi_capab_state = IDS_FAILED; 15643 freemsg(mp); 15644 return; 15645 } 15646 /* 15647 * Note the error for IOCTL completion (mp1 is set when 15648 * ready to complete ioctl). If ill_ifname_pending_err is 15649 * set, an error occured during plumbing (ill_ifname_pending), 15650 * so we want to report that error. 15651 * 15652 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 15653 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 15654 * expected to get errack'd if the driver doesn't support 15655 * these flags (e.g. ethernet). log will be set to B_FALSE 15656 * if these error conditions are encountered. 15657 */ 15658 if (mp1 != NULL) { 15659 if (ill->ill_ifname_pending_err != 0) { 15660 err = ill->ill_ifname_pending_err; 15661 ill->ill_ifname_pending_err = 0; 15662 } else { 15663 err = dlea->dl_unix_errno ? 15664 dlea->dl_unix_errno : ENXIO; 15665 } 15666 /* 15667 * If we're plumbing an interface and an error hasn't already 15668 * been saved, set ill_ifname_pending_err to the error passed 15669 * up. Ignore the error if log is B_FALSE (see comment above). 15670 */ 15671 } else if (log && ill->ill_ifname_pending && 15672 ill->ill_ifname_pending_err == 0) { 15673 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 15674 dlea->dl_unix_errno : ENXIO; 15675 } 15676 15677 if (log) 15678 ip_dlpi_error(ill, dlea->dl_error_primitive, 15679 dlea->dl_errno, dlea->dl_unix_errno); 15680 break; 15681 case DL_CAPABILITY_ACK: 15682 /* Call a routine to handle this one. */ 15683 ill_dlpi_done(ill, DL_CAPABILITY_REQ); 15684 ill_capability_ack(ill, mp); 15685 15686 /* 15687 * If the ack is due to renegotiation, we will need to send 15688 * a new CAPABILITY_REQ to start the renegotiation. 15689 */ 15690 if (ill->ill_capab_reneg) { 15691 ill->ill_capab_reneg = B_FALSE; 15692 ill_capability_probe(ill); 15693 } 15694 break; 15695 case DL_CONTROL_ACK: 15696 /* We treat all of these as "fire and forget" */ 15697 ill_dlpi_done(ill, DL_CONTROL_REQ); 15698 break; 15699 case DL_INFO_ACK: 15700 /* Call a routine to handle this one. */ 15701 ill_dlpi_done(ill, DL_INFO_REQ); 15702 ip_ll_subnet_defaults(ill, mp); 15703 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 15704 return; 15705 case DL_BIND_ACK: 15706 /* 15707 * We should have an IOCTL waiting on this unless 15708 * sent by ill_dl_phys, in which case just return 15709 */ 15710 ill_dlpi_done(ill, DL_BIND_REQ); 15711 if (ill->ill_ifname_pending) 15712 break; 15713 15714 if (!ioctl_aborted) 15715 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15716 if (mp1 == NULL) 15717 break; 15718 /* 15719 * Because mp1 was added by ill_dl_up(), and it always 15720 * passes a valid connp, connp must be valid here. 15721 */ 15722 ASSERT(connp != NULL); 15723 q = CONNP_TO_WQ(connp); 15724 15725 /* 15726 * We are exclusive. So nothing can change even after 15727 * we get the pending mp. If need be we can put it back 15728 * and restart, as in calling ipif_arp_up() below. 15729 */ 15730 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 15731 15732 mutex_enter(&ill->ill_lock); 15733 15734 ill->ill_dl_up = 1; 15735 15736 if ((info = ill->ill_nic_event_info) != NULL) { 15737 ip2dbg(("ip_rput_dlpi_writer: unexpected nic event %d " 15738 "attached for %s\n", info->hne_event, 15739 ill->ill_name)); 15740 if (info->hne_data != NULL) 15741 kmem_free(info->hne_data, info->hne_datalen); 15742 kmem_free(info, sizeof (hook_nic_event_t)); 15743 } 15744 15745 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 15746 if (info != NULL) { 15747 info->hne_nic = ill->ill_phyint->phyint_hook_ifindex; 15748 info->hne_lif = 0; 15749 info->hne_event = NE_UP; 15750 info->hne_data = NULL; 15751 info->hne_datalen = 0; 15752 info->hne_family = ill->ill_isv6 ? 15753 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data; 15754 } else 15755 ip2dbg(("ip_rput_dlpi_writer: could not attach UP nic " 15756 "event information for %s (ENOMEM)\n", 15757 ill->ill_name)); 15758 15759 ill->ill_nic_event_info = info; 15760 15761 mutex_exit(&ill->ill_lock); 15762 15763 /* 15764 * Now bring up the resolver; when that is complete, we'll 15765 * create IREs. Note that we intentionally mirror what 15766 * ipif_up() would have done, because we got here by way of 15767 * ill_dl_up(), which stopped ipif_up()'s processing. 15768 */ 15769 if (ill->ill_isv6) { 15770 /* 15771 * v6 interfaces. 15772 * Unlike ARP which has to do another bind 15773 * and attach, once we get here we are 15774 * done with NDP. Except in the case of 15775 * ILLF_XRESOLV, in which case we send an 15776 * AR_INTERFACE_UP to the external resolver. 15777 * If all goes well, the ioctl will complete 15778 * in ip_rput(). If there's an error, we 15779 * complete it here. 15780 */ 15781 if ((err = ipif_ndp_up(ipif)) == 0) { 15782 if (ill->ill_flags & ILLF_XRESOLV) { 15783 mutex_enter(&connp->conn_lock); 15784 mutex_enter(&ill->ill_lock); 15785 success = ipsq_pending_mp_add( 15786 connp, ipif, q, mp1, 0); 15787 mutex_exit(&ill->ill_lock); 15788 mutex_exit(&connp->conn_lock); 15789 if (success) { 15790 err = ipif_resolver_up(ipif, 15791 Res_act_initial); 15792 if (err == EINPROGRESS) { 15793 freemsg(mp); 15794 return; 15795 } 15796 ASSERT(err != 0); 15797 mp1 = ipsq_pending_mp_get(ipsq, 15798 &connp); 15799 ASSERT(mp1 != NULL); 15800 } else { 15801 /* conn has started closing */ 15802 err = EINTR; 15803 } 15804 } else { /* Non XRESOLV interface */ 15805 (void) ipif_resolver_up(ipif, 15806 Res_act_initial); 15807 err = ipif_up_done_v6(ipif); 15808 } 15809 } 15810 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 15811 /* 15812 * ARP and other v4 external resolvers. 15813 * Leave the pending mblk intact so that 15814 * the ioctl completes in ip_rput(). 15815 */ 15816 mutex_enter(&connp->conn_lock); 15817 mutex_enter(&ill->ill_lock); 15818 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 15819 mutex_exit(&ill->ill_lock); 15820 mutex_exit(&connp->conn_lock); 15821 if (success) { 15822 err = ipif_resolver_up(ipif, Res_act_initial); 15823 if (err == EINPROGRESS) { 15824 freemsg(mp); 15825 return; 15826 } 15827 ASSERT(err != 0); 15828 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15829 } else { 15830 /* The conn has started closing */ 15831 err = EINTR; 15832 } 15833 } else { 15834 /* 15835 * This one is complete. Reply to pending ioctl. 15836 */ 15837 (void) ipif_resolver_up(ipif, Res_act_initial); 15838 err = ipif_up_done(ipif); 15839 } 15840 15841 if ((err == 0) && (ill->ill_up_ipifs)) { 15842 err = ill_up_ipifs(ill, q, mp1); 15843 if (err == EINPROGRESS) { 15844 freemsg(mp); 15845 return; 15846 } 15847 } 15848 15849 if (ill->ill_up_ipifs) { 15850 ill_group_cleanup(ill); 15851 } 15852 15853 break; 15854 case DL_NOTIFY_IND: { 15855 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 15856 ire_t *ire; 15857 boolean_t need_ire_walk_v4 = B_FALSE; 15858 boolean_t need_ire_walk_v6 = B_FALSE; 15859 15860 switch (notify->dl_notification) { 15861 case DL_NOTE_PHYS_ADDR: 15862 err = ill_set_phys_addr(ill, mp); 15863 break; 15864 15865 case DL_NOTE_FASTPATH_FLUSH: 15866 ill_fastpath_flush(ill); 15867 break; 15868 15869 case DL_NOTE_SDU_SIZE: 15870 /* 15871 * Change the MTU size of the interface, of all 15872 * attached ipif's, and of all relevant ire's. The 15873 * new value's a uint32_t at notify->dl_data. 15874 * Mtu change Vs. new ire creation - protocol below. 15875 * 15876 * a Mark the ipif as IPIF_CHANGING. 15877 * b Set the new mtu in the ipif. 15878 * c Change the ire_max_frag on all affected ires 15879 * d Unmark the IPIF_CHANGING 15880 * 15881 * To see how the protocol works, assume an interface 15882 * route is also being added simultaneously by 15883 * ip_rt_add and let 'ipif' be the ipif referenced by 15884 * the ire. If the ire is created before step a, 15885 * it will be cleaned up by step c. If the ire is 15886 * created after step d, it will see the new value of 15887 * ipif_mtu. Any attempt to create the ire between 15888 * steps a to d will fail because of the IPIF_CHANGING 15889 * flag. Note that ire_create() is passed a pointer to 15890 * the ipif_mtu, and not the value. During ire_add 15891 * under the bucket lock, the ire_max_frag of the 15892 * new ire being created is set from the ipif/ire from 15893 * which it is being derived. 15894 */ 15895 mutex_enter(&ill->ill_lock); 15896 ill->ill_max_frag = (uint_t)notify->dl_data; 15897 15898 /* 15899 * If an SIOCSLIFLNKINFO has changed the ill_max_mtu 15900 * leave it alone 15901 */ 15902 if (ill->ill_mtu_userspecified) { 15903 mutex_exit(&ill->ill_lock); 15904 break; 15905 } 15906 ill->ill_max_mtu = ill->ill_max_frag; 15907 if (ill->ill_isv6) { 15908 if (ill->ill_max_mtu < IPV6_MIN_MTU) 15909 ill->ill_max_mtu = IPV6_MIN_MTU; 15910 } else { 15911 if (ill->ill_max_mtu < IP_MIN_MTU) 15912 ill->ill_max_mtu = IP_MIN_MTU; 15913 } 15914 for (ipif = ill->ill_ipif; ipif != NULL; 15915 ipif = ipif->ipif_next) { 15916 /* 15917 * Don't override the mtu if the user 15918 * has explicitly set it. 15919 */ 15920 if (ipif->ipif_flags & IPIF_FIXEDMTU) 15921 continue; 15922 ipif->ipif_mtu = (uint_t)notify->dl_data; 15923 if (ipif->ipif_isv6) 15924 ire = ipif_to_ire_v6(ipif); 15925 else 15926 ire = ipif_to_ire(ipif); 15927 if (ire != NULL) { 15928 ire->ire_max_frag = ipif->ipif_mtu; 15929 ire_refrele(ire); 15930 } 15931 if (ipif->ipif_flags & IPIF_UP) { 15932 if (ill->ill_isv6) 15933 need_ire_walk_v6 = B_TRUE; 15934 else 15935 need_ire_walk_v4 = B_TRUE; 15936 } 15937 } 15938 mutex_exit(&ill->ill_lock); 15939 if (need_ire_walk_v4) 15940 ire_walk_v4(ill_mtu_change, (char *)ill, 15941 ALL_ZONES, ipst); 15942 if (need_ire_walk_v6) 15943 ire_walk_v6(ill_mtu_change, (char *)ill, 15944 ALL_ZONES, ipst); 15945 break; 15946 case DL_NOTE_LINK_UP: 15947 case DL_NOTE_LINK_DOWN: { 15948 /* 15949 * We are writer. ill / phyint / ipsq assocs stable. 15950 * The RUNNING flag reflects the state of the link. 15951 */ 15952 phyint_t *phyint = ill->ill_phyint; 15953 uint64_t new_phyint_flags; 15954 boolean_t changed = B_FALSE; 15955 boolean_t went_up; 15956 15957 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 15958 mutex_enter(&phyint->phyint_lock); 15959 new_phyint_flags = went_up ? 15960 phyint->phyint_flags | PHYI_RUNNING : 15961 phyint->phyint_flags & ~PHYI_RUNNING; 15962 if (new_phyint_flags != phyint->phyint_flags) { 15963 phyint->phyint_flags = new_phyint_flags; 15964 changed = B_TRUE; 15965 } 15966 mutex_exit(&phyint->phyint_lock); 15967 /* 15968 * ill_restart_dad handles the DAD restart and routing 15969 * socket notification logic. 15970 */ 15971 if (changed) { 15972 ill_restart_dad(phyint->phyint_illv4, went_up); 15973 ill_restart_dad(phyint->phyint_illv6, went_up); 15974 } 15975 break; 15976 } 15977 case DL_NOTE_PROMISC_ON_PHYS: 15978 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 15979 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 15980 mutex_enter(&ill->ill_lock); 15981 ill->ill_promisc_on_phys = B_TRUE; 15982 mutex_exit(&ill->ill_lock); 15983 break; 15984 case DL_NOTE_PROMISC_OFF_PHYS: 15985 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 15986 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 15987 mutex_enter(&ill->ill_lock); 15988 ill->ill_promisc_on_phys = B_FALSE; 15989 mutex_exit(&ill->ill_lock); 15990 break; 15991 case DL_NOTE_CAPAB_RENEG: 15992 /* 15993 * Something changed on the driver side. 15994 * It wants us to renegotiate the capabilities 15995 * on this ill. One possible cause is the aggregation 15996 * interface under us where a port got added or 15997 * went away. 15998 * 15999 * If the capability negotiation is already done 16000 * or is in progress, reset the capabilities and 16001 * mark the ill's ill_capab_reneg to be B_TRUE, 16002 * so that when the ack comes back, we can start 16003 * the renegotiation process. 16004 * 16005 * Note that if ill_capab_reneg is already B_TRUE 16006 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case), 16007 * the capability resetting request has been sent 16008 * and the renegotiation has not been started yet; 16009 * nothing needs to be done in this case. 16010 */ 16011 if (ill->ill_dlpi_capab_state != IDS_UNKNOWN) { 16012 ill_capability_reset(ill); 16013 ill->ill_capab_reneg = B_TRUE; 16014 } 16015 break; 16016 default: 16017 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 16018 "type 0x%x for DL_NOTIFY_IND\n", 16019 notify->dl_notification)); 16020 break; 16021 } 16022 16023 /* 16024 * As this is an asynchronous operation, we 16025 * should not call ill_dlpi_done 16026 */ 16027 break; 16028 } 16029 case DL_NOTIFY_ACK: { 16030 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 16031 16032 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 16033 ill->ill_note_link = 1; 16034 ill_dlpi_done(ill, DL_NOTIFY_REQ); 16035 break; 16036 } 16037 case DL_PHYS_ADDR_ACK: { 16038 /* 16039 * As part of plumbing the interface via SIOCSLIFNAME, 16040 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, 16041 * whose answers we receive here. As each answer is received, 16042 * we call ill_dlpi_done() to dispatch the next request as 16043 * we're processing the current one. Once all answers have 16044 * been received, we use ipsq_pending_mp_get() to dequeue the 16045 * outstanding IOCTL and reply to it. (Because ill_dl_phys() 16046 * is invoked from an ill queue, conn_oper_pending_ill is not 16047 * available, but we know the ioctl is pending on ill_wq.) 16048 */ 16049 uint_t paddrlen, paddroff; 16050 16051 paddrreq = ill->ill_phys_addr_pend; 16052 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; 16053 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; 16054 16055 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 16056 if (paddrreq == DL_IPV6_TOKEN) { 16057 /* 16058 * bcopy to low-order bits of ill_token 16059 * 16060 * XXX Temporary hack - currently, all known tokens 16061 * are 64 bits, so I'll cheat for the moment. 16062 */ 16063 bcopy(mp->b_rptr + paddroff, 16064 &ill->ill_token.s6_addr32[2], paddrlen); 16065 ill->ill_token_length = paddrlen; 16066 break; 16067 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 16068 ASSERT(ill->ill_nd_lla_mp == NULL); 16069 ill_set_ndmp(ill, mp, paddroff, paddrlen); 16070 mp = NULL; 16071 break; 16072 } 16073 16074 ASSERT(paddrreq == DL_CURR_PHYS_ADDR); 16075 ASSERT(ill->ill_phys_addr_mp == NULL); 16076 if (!ill->ill_ifname_pending) 16077 break; 16078 ill->ill_ifname_pending = 0; 16079 if (!ioctl_aborted) 16080 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16081 if (mp1 != NULL) { 16082 ASSERT(connp == NULL); 16083 q = ill->ill_wq; 16084 } 16085 /* 16086 * If any error acks received during the plumbing sequence, 16087 * ill_ifname_pending_err will be set. Break out and send up 16088 * the error to the pending ioctl. 16089 */ 16090 if (ill->ill_ifname_pending_err != 0) { 16091 err = ill->ill_ifname_pending_err; 16092 ill->ill_ifname_pending_err = 0; 16093 break; 16094 } 16095 16096 ill->ill_phys_addr_mp = mp; 16097 ill->ill_phys_addr = mp->b_rptr + paddroff; 16098 mp = NULL; 16099 16100 /* 16101 * If paddrlen is zero, the DLPI provider doesn't support 16102 * physical addresses. The other two tests were historical 16103 * workarounds for bugs in our former PPP implementation, but 16104 * now other things have grown dependencies on them -- e.g., 16105 * the tun module specifies a dl_addr_length of zero in its 16106 * DL_BIND_ACK, but then specifies an incorrect value in its 16107 * DL_PHYS_ADDR_ACK. These bogus checks need to be removed, 16108 * but only after careful testing ensures that all dependent 16109 * broken DLPI providers have been fixed. 16110 */ 16111 if (paddrlen == 0 || ill->ill_phys_addr_length == 0 || 16112 ill->ill_phys_addr_length == IP_ADDR_LEN) { 16113 ill->ill_phys_addr = NULL; 16114 } else if (paddrlen != ill->ill_phys_addr_length) { 16115 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", 16116 paddrlen, ill->ill_phys_addr_length)); 16117 err = EINVAL; 16118 break; 16119 } 16120 16121 if (ill->ill_nd_lla_mp == NULL) { 16122 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { 16123 err = ENOMEM; 16124 break; 16125 } 16126 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); 16127 } 16128 16129 /* 16130 * Set the interface token. If the zeroth interface address 16131 * is unspecified, then set it to the link local address. 16132 */ 16133 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 16134 (void) ill_setdefaulttoken(ill); 16135 16136 ASSERT(ill->ill_ipif->ipif_id == 0); 16137 if (ipif != NULL && 16138 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { 16139 (void) ipif_setlinklocal(ipif); 16140 } 16141 break; 16142 } 16143 case DL_OK_ACK: 16144 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 16145 dlpi_prim_str((int)dloa->dl_correct_primitive), 16146 dloa->dl_correct_primitive)); 16147 switch (dloa->dl_correct_primitive) { 16148 case DL_PROMISCON_REQ: 16149 case DL_PROMISCOFF_REQ: 16150 case DL_ENABMULTI_REQ: 16151 case DL_DISABMULTI_REQ: 16152 case DL_UNBIND_REQ: 16153 case DL_ATTACH_REQ: 16154 ill_dlpi_done(ill, dloa->dl_correct_primitive); 16155 break; 16156 } 16157 break; 16158 default: 16159 break; 16160 } 16161 16162 freemsg(mp); 16163 if (mp1 != NULL) { 16164 /* 16165 * The operation must complete without EINPROGRESS 16166 * since ipsq_pending_mp_get() has removed the mblk 16167 * from ipsq_pending_mp. Otherwise, the operation 16168 * will be stuck forever in the ipsq. 16169 */ 16170 ASSERT(err != EINPROGRESS); 16171 16172 switch (ipsq->ipsq_current_ioctl) { 16173 case 0: 16174 ipsq_current_finish(ipsq); 16175 break; 16176 16177 case SIOCLIFADDIF: 16178 case SIOCSLIFNAME: 16179 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 16180 break; 16181 16182 default: 16183 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 16184 break; 16185 } 16186 } 16187 } 16188 16189 /* 16190 * ip_rput_other is called by ip_rput to handle messages modifying the global 16191 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 16192 */ 16193 /* ARGSUSED */ 16194 void 16195 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 16196 { 16197 ill_t *ill; 16198 struct iocblk *iocp; 16199 mblk_t *mp1; 16200 conn_t *connp = NULL; 16201 16202 ip1dbg(("ip_rput_other ")); 16203 ill = (ill_t *)q->q_ptr; 16204 /* 16205 * This routine is not a writer in the case of SIOCGTUNPARAM 16206 * in which case ipsq is NULL. 16207 */ 16208 if (ipsq != NULL) { 16209 ASSERT(IAM_WRITER_IPSQ(ipsq)); 16210 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 16211 } 16212 16213 switch (mp->b_datap->db_type) { 16214 case M_ERROR: 16215 case M_HANGUP: 16216 /* 16217 * The device has a problem. We force the ILL down. It can 16218 * be brought up again manually using SIOCSIFFLAGS (via 16219 * ifconfig or equivalent). 16220 */ 16221 ASSERT(ipsq != NULL); 16222 if (mp->b_rptr < mp->b_wptr) 16223 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 16224 if (ill->ill_error == 0) 16225 ill->ill_error = ENXIO; 16226 if (!ill_down_start(q, mp)) 16227 return; 16228 ipif_all_down_tail(ipsq, q, mp, NULL); 16229 break; 16230 case M_IOCACK: 16231 iocp = (struct iocblk *)mp->b_rptr; 16232 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 16233 switch (iocp->ioc_cmd) { 16234 case SIOCSTUNPARAM: 16235 case OSIOCSTUNPARAM: 16236 ASSERT(ipsq != NULL); 16237 /* 16238 * Finish socket ioctl passed through to tun. 16239 * We should have an IOCTL waiting on this. 16240 */ 16241 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16242 if (ill->ill_isv6) { 16243 struct iftun_req *ta; 16244 16245 /* 16246 * if a source or destination is 16247 * being set, try and set the link 16248 * local address for the tunnel 16249 */ 16250 ta = (struct iftun_req *)mp->b_cont-> 16251 b_cont->b_rptr; 16252 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 16253 ipif_set_tun_llink(ill, ta); 16254 } 16255 16256 } 16257 if (mp1 != NULL) { 16258 /* 16259 * Now copy back the b_next/b_prev used by 16260 * mi code for the mi_copy* functions. 16261 * See ip_sioctl_tunparam() for the reason. 16262 * Also protect against missing b_cont. 16263 */ 16264 if (mp->b_cont != NULL) { 16265 mp->b_cont->b_next = 16266 mp1->b_cont->b_next; 16267 mp->b_cont->b_prev = 16268 mp1->b_cont->b_prev; 16269 } 16270 inet_freemsg(mp1); 16271 ASSERT(connp != NULL); 16272 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16273 iocp->ioc_error, NO_COPYOUT, ipsq); 16274 } else { 16275 ASSERT(connp == NULL); 16276 putnext(q, mp); 16277 } 16278 break; 16279 case SIOCGTUNPARAM: 16280 case OSIOCGTUNPARAM: 16281 /* 16282 * This is really M_IOCDATA from the tunnel driver. 16283 * convert back and complete the ioctl. 16284 * We should have an IOCTL waiting on this. 16285 */ 16286 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 16287 if (mp1) { 16288 /* 16289 * Now copy back the b_next/b_prev used by 16290 * mi code for the mi_copy* functions. 16291 * See ip_sioctl_tunparam() for the reason. 16292 * Also protect against missing b_cont. 16293 */ 16294 if (mp->b_cont != NULL) { 16295 mp->b_cont->b_next = 16296 mp1->b_cont->b_next; 16297 mp->b_cont->b_prev = 16298 mp1->b_cont->b_prev; 16299 } 16300 inet_freemsg(mp1); 16301 if (iocp->ioc_error == 0) 16302 mp->b_datap->db_type = M_IOCDATA; 16303 ASSERT(connp != NULL); 16304 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16305 iocp->ioc_error, COPYOUT, NULL); 16306 } else { 16307 ASSERT(connp == NULL); 16308 putnext(q, mp); 16309 } 16310 break; 16311 default: 16312 break; 16313 } 16314 break; 16315 case M_IOCNAK: 16316 iocp = (struct iocblk *)mp->b_rptr; 16317 16318 switch (iocp->ioc_cmd) { 16319 int mode; 16320 16321 case DL_IOC_HDR_INFO: 16322 /* 16323 * If this was the first attempt turn of the 16324 * fastpath probing. 16325 */ 16326 mutex_enter(&ill->ill_lock); 16327 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 16328 ill->ill_dlpi_fastpath_state = IDS_FAILED; 16329 mutex_exit(&ill->ill_lock); 16330 ill_fastpath_nack(ill); 16331 ip1dbg(("ip_rput: DLPI fastpath off on " 16332 "interface %s\n", 16333 ill->ill_name)); 16334 } else { 16335 mutex_exit(&ill->ill_lock); 16336 } 16337 freemsg(mp); 16338 break; 16339 case SIOCSTUNPARAM: 16340 case OSIOCSTUNPARAM: 16341 ASSERT(ipsq != NULL); 16342 /* 16343 * Finish socket ioctl passed through to tun 16344 * We should have an IOCTL waiting on this. 16345 */ 16346 /* FALLTHRU */ 16347 case SIOCGTUNPARAM: 16348 case OSIOCGTUNPARAM: 16349 /* 16350 * This is really M_IOCDATA from the tunnel driver. 16351 * convert back and complete the ioctl. 16352 * We should have an IOCTL waiting on this. 16353 */ 16354 if (iocp->ioc_cmd == SIOCGTUNPARAM || 16355 iocp->ioc_cmd == OSIOCGTUNPARAM) { 16356 mp1 = ill_pending_mp_get(ill, &connp, 16357 iocp->ioc_id); 16358 mode = COPYOUT; 16359 ipsq = NULL; 16360 } else { 16361 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16362 mode = NO_COPYOUT; 16363 } 16364 if (mp1 != NULL) { 16365 /* 16366 * Now copy back the b_next/b_prev used by 16367 * mi code for the mi_copy* functions. 16368 * See ip_sioctl_tunparam() for the reason. 16369 * Also protect against missing b_cont. 16370 */ 16371 if (mp->b_cont != NULL) { 16372 mp->b_cont->b_next = 16373 mp1->b_cont->b_next; 16374 mp->b_cont->b_prev = 16375 mp1->b_cont->b_prev; 16376 } 16377 inet_freemsg(mp1); 16378 if (iocp->ioc_error == 0) 16379 iocp->ioc_error = EINVAL; 16380 ASSERT(connp != NULL); 16381 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16382 iocp->ioc_error, mode, ipsq); 16383 } else { 16384 ASSERT(connp == NULL); 16385 putnext(q, mp); 16386 } 16387 break; 16388 default: 16389 break; 16390 } 16391 default: 16392 break; 16393 } 16394 } 16395 16396 /* 16397 * NOTE : This function does not ire_refrele the ire argument passed in. 16398 * 16399 * IPQoS notes 16400 * IP policy is invoked twice for a forwarded packet, once on the read side 16401 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 16402 * enabled. An additional parameter, in_ill, has been added for this purpose. 16403 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 16404 * because ip_mroute drops this information. 16405 * 16406 */ 16407 void 16408 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 16409 { 16410 uint32_t old_pkt_len; 16411 uint32_t pkt_len; 16412 queue_t *q; 16413 uint32_t sum; 16414 #define rptr ((uchar_t *)ipha) 16415 uint32_t max_frag; 16416 uint32_t ill_index; 16417 ill_t *out_ill; 16418 mib2_ipIfStatsEntry_t *mibptr; 16419 ip_stack_t *ipst = in_ill->ill_ipst; 16420 16421 /* Get the ill_index of the incoming ILL */ 16422 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 16423 mibptr = (in_ill != NULL) ? in_ill->ill_ip_mib : &ipst->ips_ip_mib; 16424 16425 /* Initiate Read side IPPF processing */ 16426 if (IPP_ENABLED(IPP_FWD_IN, ipst)) { 16427 ip_process(IPP_FWD_IN, &mp, ill_index); 16428 if (mp == NULL) { 16429 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 16430 "during IPPF processing\n")); 16431 return; 16432 } 16433 } 16434 16435 /* Adjust the checksum to reflect the ttl decrement. */ 16436 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 16437 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 16438 16439 if (ipha->ipha_ttl-- <= 1) { 16440 if (ip_csum_hdr(ipha)) { 16441 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16442 goto drop_pkt; 16443 } 16444 /* 16445 * Note: ire_stq this will be NULL for multicast 16446 * datagrams using the long path through arp (the IRE 16447 * is not an IRE_CACHE). This should not cause 16448 * problems since we don't generate ICMP errors for 16449 * multicast packets. 16450 */ 16451 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16452 q = ire->ire_stq; 16453 if (q != NULL) { 16454 /* Sent by forwarding path, and router is global zone */ 16455 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, 16456 GLOBAL_ZONEID, ipst); 16457 } else 16458 freemsg(mp); 16459 return; 16460 } 16461 16462 /* 16463 * Don't forward if the interface is down 16464 */ 16465 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 16466 BUMP_MIB(mibptr, ipIfStatsInDiscards); 16467 ip2dbg(("ip_rput_forward:interface is down\n")); 16468 goto drop_pkt; 16469 } 16470 16471 /* Get the ill_index of the outgoing ILL */ 16472 out_ill = ire_to_ill(ire); 16473 ill_index = out_ill->ill_phyint->phyint_ifindex; 16474 16475 DTRACE_PROBE4(ip4__forwarding__start, 16476 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16477 16478 FW_HOOKS(ipst->ips_ip4_forwarding_event, 16479 ipst->ips_ipv4firewall_forwarding, 16480 in_ill, out_ill, ipha, mp, mp, ipst); 16481 16482 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 16483 16484 if (mp == NULL) 16485 return; 16486 old_pkt_len = pkt_len = ntohs(ipha->ipha_length); 16487 16488 if (is_system_labeled()) { 16489 mblk_t *mp1; 16490 16491 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 16492 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16493 goto drop_pkt; 16494 } 16495 /* Size may have changed */ 16496 mp = mp1; 16497 ipha = (ipha_t *)mp->b_rptr; 16498 pkt_len = ntohs(ipha->ipha_length); 16499 } 16500 16501 /* Check if there are options to update */ 16502 if (!IS_SIMPLE_IPH(ipha)) { 16503 if (ip_csum_hdr(ipha)) { 16504 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16505 goto drop_pkt; 16506 } 16507 if (ip_rput_forward_options(mp, ipha, ire, ipst)) { 16508 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16509 return; 16510 } 16511 16512 ipha->ipha_hdr_checksum = 0; 16513 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 16514 } 16515 max_frag = ire->ire_max_frag; 16516 if (pkt_len > max_frag) { 16517 /* 16518 * It needs fragging on its way out. We haven't 16519 * verified the header checksum yet. Since we 16520 * are going to put a surely good checksum in the 16521 * outgoing header, we have to make sure that it 16522 * was good coming in. 16523 */ 16524 if (ip_csum_hdr(ipha)) { 16525 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16526 goto drop_pkt; 16527 } 16528 /* Initiate Write side IPPF processing */ 16529 if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { 16530 ip_process(IPP_FWD_OUT, &mp, ill_index); 16531 if (mp == NULL) { 16532 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 16533 " during IPPF processing\n")); 16534 return; 16535 } 16536 } 16537 /* 16538 * Handle labeled packet resizing. 16539 * 16540 * If we have added a label, inform ip_wput_frag() of its 16541 * effect on the MTU for ICMP messages. 16542 */ 16543 if (pkt_len > old_pkt_len) { 16544 uint32_t secopt_size; 16545 16546 secopt_size = pkt_len - old_pkt_len; 16547 if (secopt_size < max_frag) 16548 max_frag -= secopt_size; 16549 } 16550 16551 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID, ipst); 16552 ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); 16553 return; 16554 } 16555 16556 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 16557 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16558 FW_HOOKS(ipst->ips_ip4_physical_out_event, 16559 ipst->ips_ipv4firewall_physical_out, 16560 NULL, out_ill, ipha, mp, mp, ipst); 16561 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 16562 if (mp == NULL) 16563 return; 16564 16565 mp->b_prev = (mblk_t *)IPP_FWD_OUT; 16566 ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); 16567 (void) ip_xmit_v4(mp, ire, NULL, B_FALSE); 16568 /* ip_xmit_v4 always consumes the packet */ 16569 return; 16570 16571 drop_pkt:; 16572 ip1dbg(("ip_rput_forward: drop pkt\n")); 16573 freemsg(mp); 16574 #undef rptr 16575 } 16576 16577 void 16578 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 16579 { 16580 ire_t *ire; 16581 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 16582 16583 ASSERT(!ipif->ipif_isv6); 16584 /* 16585 * Find an IRE which matches the destination and the outgoing 16586 * queue in the cache table. All we need is an IRE_CACHE which 16587 * is pointing at ipif->ipif_ill. If it is part of some ill group, 16588 * then it is enough to have some IRE_CACHE in the group. 16589 */ 16590 if (ipif->ipif_flags & IPIF_POINTOPOINT) 16591 dst = ipif->ipif_pp_dst_addr; 16592 16593 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MBLK_GETLABEL(mp), 16594 MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR, ipst); 16595 if (ire == NULL) { 16596 /* 16597 * Mark this packet to make it be delivered to 16598 * ip_rput_forward after the new ire has been 16599 * created. 16600 */ 16601 mp->b_prev = NULL; 16602 mp->b_next = mp; 16603 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 16604 NULL, 0, GLOBAL_ZONEID, &zero_info); 16605 } else { 16606 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 16607 IRE_REFRELE(ire); 16608 } 16609 } 16610 16611 /* Update any source route, record route or timestamp options */ 16612 static int 16613 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) 16614 { 16615 ipoptp_t opts; 16616 uchar_t *opt; 16617 uint8_t optval; 16618 uint8_t optlen; 16619 ipaddr_t dst; 16620 uint32_t ts; 16621 ire_t *dst_ire = NULL; 16622 ire_t *tmp_ire = NULL; 16623 timestruc_t now; 16624 16625 ip2dbg(("ip_rput_forward_options\n")); 16626 dst = ipha->ipha_dst; 16627 for (optval = ipoptp_first(&opts, ipha); 16628 optval != IPOPT_EOL; 16629 optval = ipoptp_next(&opts)) { 16630 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 16631 opt = opts.ipoptp_cur; 16632 optlen = opts.ipoptp_len; 16633 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 16634 optval, opts.ipoptp_len)); 16635 switch (optval) { 16636 uint32_t off; 16637 case IPOPT_SSRR: 16638 case IPOPT_LSRR: 16639 /* Check if adminstratively disabled */ 16640 if (!ipst->ips_ip_forward_src_routed) { 16641 if (ire->ire_stq != NULL) { 16642 /* 16643 * Sent by forwarding path, and router 16644 * is global zone 16645 */ 16646 icmp_unreachable(ire->ire_stq, mp, 16647 ICMP_SOURCE_ROUTE_FAILED, 16648 GLOBAL_ZONEID, ipst); 16649 } else { 16650 ip0dbg(("ip_rput_forward_options: " 16651 "unable to send unreach\n")); 16652 freemsg(mp); 16653 } 16654 return (-1); 16655 } 16656 16657 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16658 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16659 if (dst_ire == NULL) { 16660 /* 16661 * Must be partial since ip_rput_options 16662 * checked for strict. 16663 */ 16664 break; 16665 } 16666 off = opt[IPOPT_OFFSET]; 16667 off--; 16668 redo_srr: 16669 if (optlen < IP_ADDR_LEN || 16670 off > optlen - IP_ADDR_LEN) { 16671 /* End of source route */ 16672 ip1dbg(( 16673 "ip_rput_forward_options: end of SR\n")); 16674 ire_refrele(dst_ire); 16675 break; 16676 } 16677 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16678 bcopy(&ire->ire_src_addr, (char *)opt + off, 16679 IP_ADDR_LEN); 16680 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 16681 ntohl(dst))); 16682 16683 /* 16684 * Check if our address is present more than 16685 * once as consecutive hops in source route. 16686 */ 16687 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16688 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 16689 if (tmp_ire != NULL) { 16690 ire_refrele(tmp_ire); 16691 off += IP_ADDR_LEN; 16692 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16693 goto redo_srr; 16694 } 16695 ipha->ipha_dst = dst; 16696 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16697 ire_refrele(dst_ire); 16698 break; 16699 case IPOPT_RR: 16700 off = opt[IPOPT_OFFSET]; 16701 off--; 16702 if (optlen < IP_ADDR_LEN || 16703 off > optlen - IP_ADDR_LEN) { 16704 /* No more room - ignore */ 16705 ip1dbg(( 16706 "ip_rput_forward_options: end of RR\n")); 16707 break; 16708 } 16709 bcopy(&ire->ire_src_addr, (char *)opt + off, 16710 IP_ADDR_LEN); 16711 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16712 break; 16713 case IPOPT_TS: 16714 /* Insert timestamp if there is room */ 16715 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16716 case IPOPT_TS_TSONLY: 16717 off = IPOPT_TS_TIMELEN; 16718 break; 16719 case IPOPT_TS_PRESPEC: 16720 case IPOPT_TS_PRESPEC_RFC791: 16721 /* Verify that the address matched */ 16722 off = opt[IPOPT_OFFSET] - 1; 16723 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16724 dst_ire = ire_ctable_lookup(dst, 0, 16725 IRE_LOCAL, NULL, ALL_ZONES, NULL, 16726 MATCH_IRE_TYPE, ipst); 16727 if (dst_ire == NULL) { 16728 /* Not for us */ 16729 break; 16730 } 16731 ire_refrele(dst_ire); 16732 /* FALLTHRU */ 16733 case IPOPT_TS_TSANDADDR: 16734 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16735 break; 16736 default: 16737 /* 16738 * ip_*put_options should have already 16739 * dropped this packet. 16740 */ 16741 cmn_err(CE_PANIC, "ip_rput_forward_options: " 16742 "unknown IT - bug in ip_rput_options?\n"); 16743 return (0); /* Keep "lint" happy */ 16744 } 16745 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 16746 /* Increase overflow counter */ 16747 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 16748 opt[IPOPT_POS_OV_FLG] = 16749 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 16750 (off << 4)); 16751 break; 16752 } 16753 off = opt[IPOPT_OFFSET] - 1; 16754 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16755 case IPOPT_TS_PRESPEC: 16756 case IPOPT_TS_PRESPEC_RFC791: 16757 case IPOPT_TS_TSANDADDR: 16758 bcopy(&ire->ire_src_addr, 16759 (char *)opt + off, IP_ADDR_LEN); 16760 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16761 /* FALLTHRU */ 16762 case IPOPT_TS_TSONLY: 16763 off = opt[IPOPT_OFFSET] - 1; 16764 /* Compute # of milliseconds since midnight */ 16765 gethrestime(&now); 16766 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 16767 now.tv_nsec / (NANOSEC / MILLISEC); 16768 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 16769 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 16770 break; 16771 } 16772 break; 16773 } 16774 } 16775 return (0); 16776 } 16777 16778 /* 16779 * This is called after processing at least one of AH/ESP headers. 16780 * 16781 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 16782 * the actual, physical interface on which the packet was received, 16783 * but, when ip_strict_dst_multihoming is set to 1, could be the 16784 * interface which had the ipha_dst configured when the packet went 16785 * through ip_rput. The ill_index corresponding to the recv_ill 16786 * is saved in ipsec_in_rill_index 16787 * 16788 * NOTE2: The "ire" argument is only used in IPv4 cases. This function 16789 * cannot assume "ire" points to valid data for any IPv6 cases. 16790 */ 16791 void 16792 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 16793 { 16794 mblk_t *mp; 16795 ipaddr_t dst; 16796 in6_addr_t *v6dstp; 16797 ipha_t *ipha; 16798 ip6_t *ip6h; 16799 ipsec_in_t *ii; 16800 boolean_t ill_need_rele = B_FALSE; 16801 boolean_t rill_need_rele = B_FALSE; 16802 boolean_t ire_need_rele = B_FALSE; 16803 netstack_t *ns; 16804 ip_stack_t *ipst; 16805 16806 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 16807 ASSERT(ii->ipsec_in_ill_index != 0); 16808 ns = ii->ipsec_in_ns; 16809 ASSERT(ii->ipsec_in_ns != NULL); 16810 ipst = ns->netstack_ip; 16811 16812 mp = ipsec_mp->b_cont; 16813 ASSERT(mp != NULL); 16814 16815 16816 if (ill == NULL) { 16817 ASSERT(recv_ill == NULL); 16818 /* 16819 * We need to get the original queue on which ip_rput_local 16820 * or ip_rput_data_v6 was called. 16821 */ 16822 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 16823 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL, ipst); 16824 ill_need_rele = B_TRUE; 16825 16826 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 16827 recv_ill = ill_lookup_on_ifindex( 16828 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 16829 NULL, NULL, NULL, NULL, ipst); 16830 rill_need_rele = B_TRUE; 16831 } else { 16832 recv_ill = ill; 16833 } 16834 16835 if ((ill == NULL) || (recv_ill == NULL)) { 16836 ip0dbg(("ip_fanout_proto_again: interface " 16837 "disappeared\n")); 16838 if (ill != NULL) 16839 ill_refrele(ill); 16840 if (recv_ill != NULL) 16841 ill_refrele(recv_ill); 16842 freemsg(ipsec_mp); 16843 return; 16844 } 16845 } 16846 16847 ASSERT(ill != NULL && recv_ill != NULL); 16848 16849 if (mp->b_datap->db_type == M_CTL) { 16850 /* 16851 * AH/ESP is returning the ICMP message after 16852 * removing their headers. Fanout again till 16853 * it gets to the right protocol. 16854 */ 16855 if (ii->ipsec_in_v4) { 16856 icmph_t *icmph; 16857 int iph_hdr_length; 16858 int hdr_length; 16859 16860 ipha = (ipha_t *)mp->b_rptr; 16861 iph_hdr_length = IPH_HDR_LENGTH(ipha); 16862 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 16863 ipha = (ipha_t *)&icmph[1]; 16864 hdr_length = IPH_HDR_LENGTH(ipha); 16865 /* 16866 * icmp_inbound_error_fanout may need to do pullupmsg. 16867 * Reset the type to M_DATA. 16868 */ 16869 mp->b_datap->db_type = M_DATA; 16870 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 16871 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 16872 B_FALSE, ill, ii->ipsec_in_zoneid); 16873 } else { 16874 icmp6_t *icmp6; 16875 int hdr_length; 16876 16877 ip6h = (ip6_t *)mp->b_rptr; 16878 /* Don't call hdr_length_v6() unless you have to. */ 16879 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 16880 hdr_length = ip_hdr_length_v6(mp, ip6h); 16881 else 16882 hdr_length = IPV6_HDR_LEN; 16883 16884 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 16885 /* 16886 * icmp_inbound_error_fanout_v6 may need to do 16887 * pullupmsg. Reset the type to M_DATA. 16888 */ 16889 mp->b_datap->db_type = M_DATA; 16890 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 16891 ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); 16892 } 16893 if (ill_need_rele) 16894 ill_refrele(ill); 16895 if (rill_need_rele) 16896 ill_refrele(recv_ill); 16897 return; 16898 } 16899 16900 if (ii->ipsec_in_v4) { 16901 ipha = (ipha_t *)mp->b_rptr; 16902 dst = ipha->ipha_dst; 16903 if (CLASSD(dst)) { 16904 /* 16905 * Multicast has to be delivered to all streams. 16906 */ 16907 dst = INADDR_BROADCAST; 16908 } 16909 16910 if (ire == NULL) { 16911 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, 16912 MBLK_GETLABEL(mp), ipst); 16913 if (ire == NULL) { 16914 if (ill_need_rele) 16915 ill_refrele(ill); 16916 if (rill_need_rele) 16917 ill_refrele(recv_ill); 16918 ip1dbg(("ip_fanout_proto_again: " 16919 "IRE not found")); 16920 freemsg(ipsec_mp); 16921 return; 16922 } 16923 ire_need_rele = B_TRUE; 16924 } 16925 16926 switch (ipha->ipha_protocol) { 16927 case IPPROTO_UDP: 16928 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 16929 recv_ill); 16930 if (ire_need_rele) 16931 ire_refrele(ire); 16932 break; 16933 case IPPROTO_TCP: 16934 if (!ire_need_rele) 16935 IRE_REFHOLD(ire); 16936 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 16937 ire, ipsec_mp, 0, ill->ill_rq, NULL); 16938 IRE_REFRELE(ire); 16939 if (mp != NULL) 16940 squeue_enter_chain(GET_SQUEUE(mp), mp, 16941 mp, 1, SQTAG_IP_PROTO_AGAIN); 16942 break; 16943 case IPPROTO_SCTP: 16944 if (!ire_need_rele) 16945 IRE_REFHOLD(ire); 16946 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 16947 ipsec_mp, 0, ill->ill_rq, dst); 16948 break; 16949 default: 16950 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 16951 recv_ill, B_FALSE); 16952 if (ire_need_rele) 16953 ire_refrele(ire); 16954 break; 16955 } 16956 } else { 16957 uint32_t rput_flags = 0; 16958 16959 ip6h = (ip6_t *)mp->b_rptr; 16960 v6dstp = &ip6h->ip6_dst; 16961 /* 16962 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 16963 * address. 16964 * 16965 * Currently, we don't store that state in the IPSEC_IN 16966 * message, and we may need to. 16967 */ 16968 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 16969 IP6_IN_LLMCAST : 0); 16970 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 16971 NULL, NULL); 16972 } 16973 if (ill_need_rele) 16974 ill_refrele(ill); 16975 if (rill_need_rele) 16976 ill_refrele(recv_ill); 16977 } 16978 16979 /* 16980 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 16981 * returns 'true' if there are still fragments left on the queue, in 16982 * which case we restart the timer. 16983 */ 16984 void 16985 ill_frag_timer(void *arg) 16986 { 16987 ill_t *ill = (ill_t *)arg; 16988 boolean_t frag_pending; 16989 ip_stack_t *ipst = ill->ill_ipst; 16990 16991 mutex_enter(&ill->ill_lock); 16992 ASSERT(!ill->ill_fragtimer_executing); 16993 if (ill->ill_state_flags & ILL_CONDEMNED) { 16994 ill->ill_frag_timer_id = 0; 16995 mutex_exit(&ill->ill_lock); 16996 return; 16997 } 16998 ill->ill_fragtimer_executing = 1; 16999 mutex_exit(&ill->ill_lock); 17000 17001 frag_pending = ill_frag_timeout(ill, ipst->ips_ip_g_frag_timeout); 17002 17003 /* 17004 * Restart the timer, if we have fragments pending or if someone 17005 * wanted us to be scheduled again. 17006 */ 17007 mutex_enter(&ill->ill_lock); 17008 ill->ill_fragtimer_executing = 0; 17009 ill->ill_frag_timer_id = 0; 17010 if (frag_pending || ill->ill_fragtimer_needrestart) 17011 ill_frag_timer_start(ill); 17012 mutex_exit(&ill->ill_lock); 17013 } 17014 17015 void 17016 ill_frag_timer_start(ill_t *ill) 17017 { 17018 ip_stack_t *ipst = ill->ill_ipst; 17019 17020 ASSERT(MUTEX_HELD(&ill->ill_lock)); 17021 17022 /* If the ill is closing or opening don't proceed */ 17023 if (ill->ill_state_flags & ILL_CONDEMNED) 17024 return; 17025 17026 if (ill->ill_fragtimer_executing) { 17027 /* 17028 * ill_frag_timer is currently executing. Just record the 17029 * the fact that we want the timer to be restarted. 17030 * ill_frag_timer will post a timeout before it returns, 17031 * ensuring it will be called again. 17032 */ 17033 ill->ill_fragtimer_needrestart = 1; 17034 return; 17035 } 17036 17037 if (ill->ill_frag_timer_id == 0) { 17038 /* 17039 * The timer is neither running nor is the timeout handler 17040 * executing. Post a timeout so that ill_frag_timer will be 17041 * called 17042 */ 17043 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 17044 MSEC_TO_TICK(ipst->ips_ip_g_frag_timo_ms >> 1)); 17045 ill->ill_fragtimer_needrestart = 0; 17046 } 17047 } 17048 17049 /* 17050 * This routine is needed for loopback when forwarding multicasts. 17051 * 17052 * IPQoS Notes: 17053 * IPPF processing is done in fanout routines. 17054 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 17055 * processing for IPsec packets is done when it comes back in clear. 17056 * NOTE : The callers of this function need to do the ire_refrele for the 17057 * ire that is being passed in. 17058 */ 17059 void 17060 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17061 ill_t *recv_ill, boolean_t esp_in_udp_packet) 17062 { 17063 ill_t *ill = (ill_t *)q->q_ptr; 17064 uint32_t sum; 17065 uint32_t u1; 17066 uint32_t u2; 17067 int hdr_length; 17068 boolean_t mctl_present; 17069 mblk_t *first_mp = mp; 17070 mblk_t *hada_mp = NULL; 17071 ipha_t *inner_ipha; 17072 ip_stack_t *ipst; 17073 17074 ASSERT(recv_ill != NULL); 17075 ipst = recv_ill->ill_ipst; 17076 17077 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 17078 "ip_rput_locl_start: q %p", q); 17079 17080 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17081 ASSERT(ill != NULL); 17082 17083 17084 #define rptr ((uchar_t *)ipha) 17085 #define iphs ((uint16_t *)ipha) 17086 17087 /* 17088 * no UDP or TCP packet should come here anymore. 17089 */ 17090 ASSERT(ipha->ipha_protocol != IPPROTO_TCP && 17091 ipha->ipha_protocol != IPPROTO_UDP); 17092 17093 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 17094 if (mctl_present && 17095 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 17096 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 17097 17098 /* 17099 * It's an IPsec accelerated packet. 17100 * Keep a pointer to the data attributes around until 17101 * we allocate the ipsec_info_t. 17102 */ 17103 IPSECHW_DEBUG(IPSECHW_PKT, 17104 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 17105 hada_mp = first_mp; 17106 hada_mp->b_cont = NULL; 17107 /* 17108 * Since it is accelerated, it comes directly from 17109 * the ill and the data attributes is followed by 17110 * the packet data. 17111 */ 17112 ASSERT(mp->b_datap->db_type != M_CTL); 17113 first_mp = mp; 17114 mctl_present = B_FALSE; 17115 } 17116 17117 /* 17118 * IF M_CTL is not present, then ipsec_in_is_secure 17119 * should return B_TRUE. There is a case where loopback 17120 * packets has an M_CTL in the front with all the 17121 * IPsec options set to IPSEC_PREF_NEVER - which means 17122 * ipsec_in_is_secure will return B_FALSE. As loopback 17123 * packets never comes here, it is safe to ASSERT the 17124 * following. 17125 */ 17126 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 17127 17128 /* 17129 * Also, we should never have an mctl_present if this is an 17130 * ESP-in-UDP packet. 17131 */ 17132 ASSERT(!mctl_present || !esp_in_udp_packet); 17133 17134 17135 /* u1 is # words of IP options */ 17136 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 17137 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 17138 17139 if (u1 || (!esp_in_udp_packet && !mctl_present)) { 17140 if (u1) { 17141 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 17142 if (hada_mp != NULL) 17143 freemsg(hada_mp); 17144 return; 17145 } 17146 } else { 17147 /* Check the IP header checksum. */ 17148 #define uph ((uint16_t *)ipha) 17149 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 17150 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 17151 #undef uph 17152 /* finish doing IP checksum */ 17153 sum = (sum & 0xFFFF) + (sum >> 16); 17154 sum = ~(sum + (sum >> 16)) & 0xFFFF; 17155 if (sum && sum != 0xFFFF) { 17156 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 17157 goto drop_pkt; 17158 } 17159 } 17160 } 17161 17162 /* 17163 * Count for SNMP of inbound packets for ire. As ip_proto_input 17164 * might be called more than once for secure packets, count only 17165 * the first time. 17166 */ 17167 if (!mctl_present) { 17168 UPDATE_IB_PKT_COUNT(ire); 17169 ire->ire_last_used_time = lbolt; 17170 } 17171 17172 /* Check for fragmentation offset. */ 17173 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 17174 u1 = u2 & (IPH_MF | IPH_OFFSET); 17175 if (u1) { 17176 /* 17177 * We re-assemble fragments before we do the AH/ESP 17178 * processing. Thus, M_CTL should not be present 17179 * while we are re-assembling. 17180 */ 17181 ASSERT(!mctl_present); 17182 ASSERT(first_mp == mp); 17183 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 17184 return; 17185 } 17186 /* 17187 * Make sure that first_mp points back to mp as 17188 * the mp we came in with could have changed in 17189 * ip_rput_fragment(). 17190 */ 17191 ipha = (ipha_t *)mp->b_rptr; 17192 first_mp = mp; 17193 } 17194 17195 /* 17196 * Clear hardware checksumming flag as it is currently only 17197 * used by TCP and UDP. 17198 */ 17199 DB_CKSUMFLAGS(mp) = 0; 17200 17201 /* Now we have a complete datagram, destined for this machine. */ 17202 u1 = IPH_HDR_LENGTH(ipha); 17203 switch (ipha->ipha_protocol) { 17204 case IPPROTO_ICMP: { 17205 ire_t *ire_zone; 17206 ilm_t *ilm; 17207 mblk_t *mp1; 17208 zoneid_t last_zoneid; 17209 17210 if (CLASSD(ipha->ipha_dst) && !IS_LOOPBACK(recv_ill)) { 17211 ASSERT(ire->ire_type == IRE_BROADCAST); 17212 /* 17213 * In the multicast case, applications may have joined 17214 * the group from different zones, so we need to deliver 17215 * the packet to each of them. Loop through the 17216 * multicast memberships structures (ilm) on the receive 17217 * ill and send a copy of the packet up each matching 17218 * one. However, we don't do this for multicasts sent on 17219 * the loopback interface (PHYI_LOOPBACK flag set) as 17220 * they must stay in the sender's zone. 17221 * 17222 * ilm_add_v6() ensures that ilms in the same zone are 17223 * contiguous in the ill_ilm list. We use this property 17224 * to avoid sending duplicates needed when two 17225 * applications in the same zone join the same group on 17226 * different logical interfaces: we ignore the ilm if 17227 * its zoneid is the same as the last matching one. 17228 * In addition, the sending of the packet for 17229 * ire_zoneid is delayed until all of the other ilms 17230 * have been exhausted. 17231 */ 17232 last_zoneid = -1; 17233 ILM_WALKER_HOLD(recv_ill); 17234 for (ilm = recv_ill->ill_ilm; ilm != NULL; 17235 ilm = ilm->ilm_next) { 17236 if ((ilm->ilm_flags & ILM_DELETED) || 17237 ipha->ipha_dst != ilm->ilm_addr || 17238 ilm->ilm_zoneid == last_zoneid || 17239 ilm->ilm_zoneid == ire->ire_zoneid || 17240 ilm->ilm_zoneid == ALL_ZONES || 17241 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 17242 continue; 17243 mp1 = ip_copymsg(first_mp); 17244 if (mp1 == NULL) 17245 continue; 17246 icmp_inbound(q, mp1, B_TRUE, ill, 17247 0, sum, mctl_present, B_TRUE, 17248 recv_ill, ilm->ilm_zoneid); 17249 last_zoneid = ilm->ilm_zoneid; 17250 } 17251 ILM_WALKER_RELE(recv_ill); 17252 } else if (ire->ire_type == IRE_BROADCAST) { 17253 /* 17254 * In the broadcast case, there may be many zones 17255 * which need a copy of the packet delivered to them. 17256 * There is one IRE_BROADCAST per broadcast address 17257 * and per zone; we walk those using a helper function. 17258 * In addition, the sending of the packet for ire is 17259 * delayed until all of the other ires have been 17260 * processed. 17261 */ 17262 IRB_REFHOLD(ire->ire_bucket); 17263 ire_zone = NULL; 17264 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 17265 ire)) != NULL) { 17266 mp1 = ip_copymsg(first_mp); 17267 if (mp1 == NULL) 17268 continue; 17269 17270 UPDATE_IB_PKT_COUNT(ire_zone); 17271 ire_zone->ire_last_used_time = lbolt; 17272 icmp_inbound(q, mp1, B_TRUE, ill, 17273 0, sum, mctl_present, B_TRUE, 17274 recv_ill, ire_zone->ire_zoneid); 17275 } 17276 IRB_REFRELE(ire->ire_bucket); 17277 } 17278 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 17279 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 17280 ire->ire_zoneid); 17281 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17282 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 17283 return; 17284 } 17285 case IPPROTO_IGMP: 17286 /* 17287 * If we are not willing to accept IGMP packets in clear, 17288 * then check with global policy. 17289 */ 17290 if (ipst->ips_igmp_accept_clear_messages == 0) { 17291 first_mp = ipsec_check_global_policy(first_mp, NULL, 17292 ipha, NULL, mctl_present, ipst->ips_netstack); 17293 if (first_mp == NULL) 17294 return; 17295 } 17296 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17297 freemsg(first_mp); 17298 ip1dbg(("ip_proto_input: zone all cannot accept raw")); 17299 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17300 return; 17301 } 17302 if ((mp = igmp_input(q, mp, ill)) == NULL) { 17303 /* Bad packet - discarded by igmp_input */ 17304 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17305 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 17306 if (mctl_present) 17307 freeb(first_mp); 17308 return; 17309 } 17310 /* 17311 * igmp_input() may have returned the pulled up message. 17312 * So first_mp and ipha need to be reinitialized. 17313 */ 17314 ipha = (ipha_t *)mp->b_rptr; 17315 if (mctl_present) 17316 first_mp->b_cont = mp; 17317 else 17318 first_mp = mp; 17319 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17320 connf_head != NULL) { 17321 /* No user-level listener for IGMP packets */ 17322 goto drop_pkt; 17323 } 17324 /* deliver to local raw users */ 17325 break; 17326 case IPPROTO_PIM: 17327 /* 17328 * If we are not willing to accept PIM packets in clear, 17329 * then check with global policy. 17330 */ 17331 if (ipst->ips_pim_accept_clear_messages == 0) { 17332 first_mp = ipsec_check_global_policy(first_mp, NULL, 17333 ipha, NULL, mctl_present, ipst->ips_netstack); 17334 if (first_mp == NULL) 17335 return; 17336 } 17337 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17338 freemsg(first_mp); 17339 ip1dbg(("ip_proto_input: zone all cannot accept PIM")); 17340 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17341 return; 17342 } 17343 if (pim_input(q, mp, ill) != 0) { 17344 /* Bad packet - discarded by pim_input */ 17345 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17346 "ip_rput_locl_end: q %p (%S)", q, "pim"); 17347 if (mctl_present) 17348 freeb(first_mp); 17349 return; 17350 } 17351 17352 /* 17353 * pim_input() may have pulled up the message so ipha needs to 17354 * be reinitialized. 17355 */ 17356 ipha = (ipha_t *)mp->b_rptr; 17357 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17358 connf_head != NULL) { 17359 /* No user-level listener for PIM packets */ 17360 goto drop_pkt; 17361 } 17362 /* deliver to local raw users */ 17363 break; 17364 case IPPROTO_ENCAP: 17365 /* 17366 * Handle self-encapsulated packets (IP-in-IP where 17367 * the inner addresses == the outer addresses). 17368 */ 17369 hdr_length = IPH_HDR_LENGTH(ipha); 17370 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 17371 mp->b_wptr) { 17372 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 17373 sizeof (ipha_t) - mp->b_rptr)) { 17374 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17375 freemsg(first_mp); 17376 return; 17377 } 17378 ipha = (ipha_t *)mp->b_rptr; 17379 } 17380 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 17381 /* 17382 * Check the sanity of the inner IP header. 17383 */ 17384 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 17385 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17386 freemsg(first_mp); 17387 return; 17388 } 17389 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 17390 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17391 freemsg(first_mp); 17392 return; 17393 } 17394 if (inner_ipha->ipha_src == ipha->ipha_src && 17395 inner_ipha->ipha_dst == ipha->ipha_dst) { 17396 ipsec_in_t *ii; 17397 17398 /* 17399 * Self-encapsulated tunnel packet. Remove 17400 * the outer IP header and fanout again. 17401 * We also need to make sure that the inner 17402 * header is pulled up until options. 17403 */ 17404 mp->b_rptr = (uchar_t *)inner_ipha; 17405 ipha = inner_ipha; 17406 hdr_length = IPH_HDR_LENGTH(ipha); 17407 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 17408 if (!pullupmsg(mp, (uchar_t *)ipha + 17409 + hdr_length - mp->b_rptr)) { 17410 freemsg(first_mp); 17411 return; 17412 } 17413 ipha = (ipha_t *)mp->b_rptr; 17414 } 17415 if (!mctl_present) { 17416 ASSERT(first_mp == mp); 17417 /* 17418 * This means that somebody is sending 17419 * Self-encapsualted packets without AH/ESP. 17420 * If AH/ESP was present, we would have already 17421 * allocated the first_mp. 17422 */ 17423 first_mp = ipsec_in_alloc(B_TRUE, 17424 ipst->ips_netstack); 17425 if (first_mp == NULL) { 17426 ip1dbg(("ip_proto_input: IPSEC_IN " 17427 "allocation failure.\n")); 17428 BUMP_MIB(ill->ill_ip_mib, 17429 ipIfStatsInDiscards); 17430 freemsg(mp); 17431 return; 17432 } 17433 first_mp->b_cont = mp; 17434 } 17435 /* 17436 * We generally store the ill_index if we need to 17437 * do IPsec processing as we lose the ill queue when 17438 * we come back. But in this case, we never should 17439 * have to store the ill_index here as it should have 17440 * been stored previously when we processed the 17441 * AH/ESP header in this routine or for non-ipsec 17442 * cases, we still have the queue. But for some bad 17443 * packets from the wire, we can get to IPsec after 17444 * this and we better store the index for that case. 17445 */ 17446 ill = (ill_t *)q->q_ptr; 17447 ii = (ipsec_in_t *)first_mp->b_rptr; 17448 ii->ipsec_in_ill_index = 17449 ill->ill_phyint->phyint_ifindex; 17450 ii->ipsec_in_rill_index = 17451 recv_ill->ill_phyint->phyint_ifindex; 17452 if (ii->ipsec_in_decaps) { 17453 /* 17454 * This packet is self-encapsulated multiple 17455 * times. We don't want to recurse infinitely. 17456 * To keep it simple, drop the packet. 17457 */ 17458 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17459 freemsg(first_mp); 17460 return; 17461 } 17462 ii->ipsec_in_decaps = B_TRUE; 17463 ip_fanout_proto_again(first_mp, recv_ill, recv_ill, 17464 ire); 17465 return; 17466 } 17467 break; 17468 case IPPROTO_AH: 17469 case IPPROTO_ESP: { 17470 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 17471 17472 /* 17473 * Fast path for AH/ESP. If this is the first time 17474 * we are sending a datagram to AH/ESP, allocate 17475 * a IPSEC_IN message and prepend it. Otherwise, 17476 * just fanout. 17477 */ 17478 17479 int ipsec_rc; 17480 ipsec_in_t *ii; 17481 netstack_t *ns = ipst->ips_netstack; 17482 17483 IP_STAT(ipst, ipsec_proto_ahesp); 17484 if (!mctl_present) { 17485 ASSERT(first_mp == mp); 17486 first_mp = ipsec_in_alloc(B_TRUE, ns); 17487 if (first_mp == NULL) { 17488 ip1dbg(("ip_proto_input: IPSEC_IN " 17489 "allocation failure.\n")); 17490 freemsg(hada_mp); /* okay ifnull */ 17491 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17492 freemsg(mp); 17493 return; 17494 } 17495 /* 17496 * Store the ill_index so that when we come back 17497 * from IPsec we ride on the same queue. 17498 */ 17499 ill = (ill_t *)q->q_ptr; 17500 ii = (ipsec_in_t *)first_mp->b_rptr; 17501 ii->ipsec_in_ill_index = 17502 ill->ill_phyint->phyint_ifindex; 17503 ii->ipsec_in_rill_index = 17504 recv_ill->ill_phyint->phyint_ifindex; 17505 first_mp->b_cont = mp; 17506 /* 17507 * Cache hardware acceleration info. 17508 */ 17509 if (hada_mp != NULL) { 17510 IPSECHW_DEBUG(IPSECHW_PKT, 17511 ("ip_rput_local: caching data attr.\n")); 17512 ii->ipsec_in_accelerated = B_TRUE; 17513 ii->ipsec_in_da = hada_mp; 17514 hada_mp = NULL; 17515 } 17516 } else { 17517 ii = (ipsec_in_t *)first_mp->b_rptr; 17518 } 17519 17520 if (!ipsec_loaded(ipss)) { 17521 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 17522 ire->ire_zoneid, ipst); 17523 return; 17524 } 17525 17526 ns = ipst->ips_netstack; 17527 /* select inbound SA and have IPsec process the pkt */ 17528 if (ipha->ipha_protocol == IPPROTO_ESP) { 17529 esph_t *esph = ipsec_inbound_esp_sa(first_mp, ns); 17530 boolean_t esp_in_udp_sa; 17531 if (esph == NULL) 17532 return; 17533 ASSERT(ii->ipsec_in_esp_sa != NULL); 17534 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 17535 esp_in_udp_sa = ((ii->ipsec_in_esp_sa->ipsa_flags & 17536 IPSA_F_NATT) != 0); 17537 /* 17538 * The following is a fancy, but quick, way of saying: 17539 * ESP-in-UDP SA and Raw ESP packet --> drop 17540 * OR 17541 * ESP SA and ESP-in-UDP packet --> drop 17542 */ 17543 if (esp_in_udp_sa != esp_in_udp_packet) { 17544 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17545 ip_drop_packet(first_mp, B_TRUE, ill, NULL, 17546 DROPPER(ns->netstack_ipsec, ipds_esp_no_sa), 17547 &ns->netstack_ipsec->ipsec_dropper); 17548 return; 17549 } 17550 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 17551 first_mp, esph); 17552 } else { 17553 ah_t *ah = ipsec_inbound_ah_sa(first_mp, ns); 17554 if (ah == NULL) 17555 return; 17556 ASSERT(ii->ipsec_in_ah_sa != NULL); 17557 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 17558 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 17559 first_mp, ah); 17560 } 17561 17562 switch (ipsec_rc) { 17563 case IPSEC_STATUS_SUCCESS: 17564 break; 17565 case IPSEC_STATUS_FAILED: 17566 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17567 /* FALLTHRU */ 17568 case IPSEC_STATUS_PENDING: 17569 return; 17570 } 17571 /* we're done with IPsec processing, send it up */ 17572 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 17573 return; 17574 } 17575 default: 17576 break; 17577 } 17578 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { 17579 ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", 17580 ire->ire_zoneid)); 17581 goto drop_pkt; 17582 } 17583 /* 17584 * Handle protocols with which IP is less intimate. There 17585 * can be more than one stream bound to a particular 17586 * protocol. When this is the case, each one gets a copy 17587 * of any incoming packets. 17588 */ 17589 ip_fanout_proto(q, first_mp, ill, ipha, 17590 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 17591 B_TRUE, recv_ill, ire->ire_zoneid); 17592 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17593 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 17594 return; 17595 17596 drop_pkt: 17597 freemsg(first_mp); 17598 if (hada_mp != NULL) 17599 freeb(hada_mp); 17600 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17601 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 17602 #undef rptr 17603 #undef iphs 17604 17605 } 17606 17607 /* 17608 * Update any source route, record route or timestamp options. 17609 * Check that we are at end of strict source route. 17610 * The options have already been checked for sanity in ip_rput_options(). 17611 */ 17612 static boolean_t 17613 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17614 ip_stack_t *ipst) 17615 { 17616 ipoptp_t opts; 17617 uchar_t *opt; 17618 uint8_t optval; 17619 uint8_t optlen; 17620 ipaddr_t dst; 17621 uint32_t ts; 17622 ire_t *dst_ire; 17623 timestruc_t now; 17624 zoneid_t zoneid; 17625 ill_t *ill; 17626 17627 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17628 17629 ip2dbg(("ip_rput_local_options\n")); 17630 17631 for (optval = ipoptp_first(&opts, ipha); 17632 optval != IPOPT_EOL; 17633 optval = ipoptp_next(&opts)) { 17634 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 17635 opt = opts.ipoptp_cur; 17636 optlen = opts.ipoptp_len; 17637 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 17638 optval, optlen)); 17639 switch (optval) { 17640 uint32_t off; 17641 case IPOPT_SSRR: 17642 case IPOPT_LSRR: 17643 off = opt[IPOPT_OFFSET]; 17644 off--; 17645 if (optlen < IP_ADDR_LEN || 17646 off > optlen - IP_ADDR_LEN) { 17647 /* End of source route */ 17648 ip1dbg(("ip_rput_local_options: end of SR\n")); 17649 break; 17650 } 17651 /* 17652 * This will only happen if two consecutive entries 17653 * in the source route contains our address or if 17654 * it is a packet with a loose source route which 17655 * reaches us before consuming the whole source route 17656 */ 17657 ip1dbg(("ip_rput_local_options: not end of SR\n")); 17658 if (optval == IPOPT_SSRR) { 17659 goto bad_src_route; 17660 } 17661 /* 17662 * Hack: instead of dropping the packet truncate the 17663 * source route to what has been used by filling the 17664 * rest with IPOPT_NOP. 17665 */ 17666 opt[IPOPT_OLEN] = (uint8_t)off; 17667 while (off < optlen) { 17668 opt[off++] = IPOPT_NOP; 17669 } 17670 break; 17671 case IPOPT_RR: 17672 off = opt[IPOPT_OFFSET]; 17673 off--; 17674 if (optlen < IP_ADDR_LEN || 17675 off > optlen - IP_ADDR_LEN) { 17676 /* No more room - ignore */ 17677 ip1dbg(( 17678 "ip_rput_local_options: end of RR\n")); 17679 break; 17680 } 17681 bcopy(&ire->ire_src_addr, (char *)opt + off, 17682 IP_ADDR_LEN); 17683 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17684 break; 17685 case IPOPT_TS: 17686 /* Insert timestamp if there is romm */ 17687 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17688 case IPOPT_TS_TSONLY: 17689 off = IPOPT_TS_TIMELEN; 17690 break; 17691 case IPOPT_TS_PRESPEC: 17692 case IPOPT_TS_PRESPEC_RFC791: 17693 /* Verify that the address matched */ 17694 off = opt[IPOPT_OFFSET] - 1; 17695 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17696 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17697 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 17698 ipst); 17699 if (dst_ire == NULL) { 17700 /* Not for us */ 17701 break; 17702 } 17703 ire_refrele(dst_ire); 17704 /* FALLTHRU */ 17705 case IPOPT_TS_TSANDADDR: 17706 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17707 break; 17708 default: 17709 /* 17710 * ip_*put_options should have already 17711 * dropped this packet. 17712 */ 17713 cmn_err(CE_PANIC, "ip_rput_local_options: " 17714 "unknown IT - bug in ip_rput_options?\n"); 17715 return (B_TRUE); /* Keep "lint" happy */ 17716 } 17717 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 17718 /* Increase overflow counter */ 17719 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 17720 opt[IPOPT_POS_OV_FLG] = 17721 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 17722 (off << 4)); 17723 break; 17724 } 17725 off = opt[IPOPT_OFFSET] - 1; 17726 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17727 case IPOPT_TS_PRESPEC: 17728 case IPOPT_TS_PRESPEC_RFC791: 17729 case IPOPT_TS_TSANDADDR: 17730 bcopy(&ire->ire_src_addr, (char *)opt + off, 17731 IP_ADDR_LEN); 17732 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17733 /* FALLTHRU */ 17734 case IPOPT_TS_TSONLY: 17735 off = opt[IPOPT_OFFSET] - 1; 17736 /* Compute # of milliseconds since midnight */ 17737 gethrestime(&now); 17738 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 17739 now.tv_nsec / (NANOSEC / MILLISEC); 17740 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 17741 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 17742 break; 17743 } 17744 break; 17745 } 17746 } 17747 return (B_TRUE); 17748 17749 bad_src_route: 17750 q = WR(q); 17751 if (q->q_next != NULL) 17752 ill = q->q_ptr; 17753 else 17754 ill = NULL; 17755 17756 /* make sure we clear any indication of a hardware checksum */ 17757 DB_CKSUMFLAGS(mp) = 0; 17758 zoneid = ipif_lookup_addr_zoneid(ipha->ipha_dst, ill, ipst); 17759 if (zoneid == ALL_ZONES) 17760 freemsg(mp); 17761 else 17762 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 17763 return (B_FALSE); 17764 17765 } 17766 17767 /* 17768 * Process IP options in an inbound packet. If an option affects the 17769 * effective destination address, return the next hop address via dstp. 17770 * Returns -1 if something fails in which case an ICMP error has been sent 17771 * and mp freed. 17772 */ 17773 static int 17774 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp, 17775 ip_stack_t *ipst) 17776 { 17777 ipoptp_t opts; 17778 uchar_t *opt; 17779 uint8_t optval; 17780 uint8_t optlen; 17781 ipaddr_t dst; 17782 intptr_t code = 0; 17783 ire_t *ire = NULL; 17784 zoneid_t zoneid; 17785 ill_t *ill; 17786 17787 ip2dbg(("ip_rput_options\n")); 17788 dst = ipha->ipha_dst; 17789 for (optval = ipoptp_first(&opts, ipha); 17790 optval != IPOPT_EOL; 17791 optval = ipoptp_next(&opts)) { 17792 opt = opts.ipoptp_cur; 17793 optlen = opts.ipoptp_len; 17794 ip2dbg(("ip_rput_options: opt %d, len %d\n", 17795 optval, optlen)); 17796 /* 17797 * Note: we need to verify the checksum before we 17798 * modify anything thus this routine only extracts the next 17799 * hop dst from any source route. 17800 */ 17801 switch (optval) { 17802 uint32_t off; 17803 case IPOPT_SSRR: 17804 case IPOPT_LSRR: 17805 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17806 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17807 if (ire == NULL) { 17808 if (optval == IPOPT_SSRR) { 17809 ip1dbg(("ip_rput_options: not next" 17810 " strict source route 0x%x\n", 17811 ntohl(dst))); 17812 code = (char *)&ipha->ipha_dst - 17813 (char *)ipha; 17814 goto param_prob; /* RouterReq's */ 17815 } 17816 ip2dbg(("ip_rput_options: " 17817 "not next source route 0x%x\n", 17818 ntohl(dst))); 17819 break; 17820 } 17821 ire_refrele(ire); 17822 17823 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17824 ip1dbg(( 17825 "ip_rput_options: bad option offset\n")); 17826 code = (char *)&opt[IPOPT_OLEN] - 17827 (char *)ipha; 17828 goto param_prob; 17829 } 17830 off = opt[IPOPT_OFFSET]; 17831 off--; 17832 redo_srr: 17833 if (optlen < IP_ADDR_LEN || 17834 off > optlen - IP_ADDR_LEN) { 17835 /* End of source route */ 17836 ip1dbg(("ip_rput_options: end of SR\n")); 17837 break; 17838 } 17839 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17840 ip1dbg(("ip_rput_options: next hop 0x%x\n", 17841 ntohl(dst))); 17842 17843 /* 17844 * Check if our address is present more than 17845 * once as consecutive hops in source route. 17846 * XXX verify per-interface ip_forwarding 17847 * for source route? 17848 */ 17849 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17850 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17851 17852 if (ire != NULL) { 17853 ire_refrele(ire); 17854 off += IP_ADDR_LEN; 17855 goto redo_srr; 17856 } 17857 17858 if (dst == htonl(INADDR_LOOPBACK)) { 17859 ip1dbg(("ip_rput_options: loopback addr in " 17860 "source route!\n")); 17861 goto bad_src_route; 17862 } 17863 /* 17864 * For strict: verify that dst is directly 17865 * reachable. 17866 */ 17867 if (optval == IPOPT_SSRR) { 17868 ire = ire_ftable_lookup(dst, 0, 0, 17869 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 17870 MBLK_GETLABEL(mp), 17871 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 17872 if (ire == NULL) { 17873 ip1dbg(("ip_rput_options: SSRR not " 17874 "directly reachable: 0x%x\n", 17875 ntohl(dst))); 17876 goto bad_src_route; 17877 } 17878 ire_refrele(ire); 17879 } 17880 /* 17881 * Defer update of the offset and the record route 17882 * until the packet is forwarded. 17883 */ 17884 break; 17885 case IPOPT_RR: 17886 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17887 ip1dbg(( 17888 "ip_rput_options: bad option offset\n")); 17889 code = (char *)&opt[IPOPT_OLEN] - 17890 (char *)ipha; 17891 goto param_prob; 17892 } 17893 break; 17894 case IPOPT_TS: 17895 /* 17896 * Verify that length >= 5 and that there is either 17897 * room for another timestamp or that the overflow 17898 * counter is not maxed out. 17899 */ 17900 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 17901 if (optlen < IPOPT_MINLEN_IT) { 17902 goto param_prob; 17903 } 17904 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17905 ip1dbg(( 17906 "ip_rput_options: bad option offset\n")); 17907 code = (char *)&opt[IPOPT_OFFSET] - 17908 (char *)ipha; 17909 goto param_prob; 17910 } 17911 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17912 case IPOPT_TS_TSONLY: 17913 off = IPOPT_TS_TIMELEN; 17914 break; 17915 case IPOPT_TS_TSANDADDR: 17916 case IPOPT_TS_PRESPEC: 17917 case IPOPT_TS_PRESPEC_RFC791: 17918 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17919 break; 17920 default: 17921 code = (char *)&opt[IPOPT_POS_OV_FLG] - 17922 (char *)ipha; 17923 goto param_prob; 17924 } 17925 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 17926 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 17927 /* 17928 * No room and the overflow counter is 15 17929 * already. 17930 */ 17931 goto param_prob; 17932 } 17933 break; 17934 } 17935 } 17936 17937 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 17938 *dstp = dst; 17939 return (0); 17940 } 17941 17942 ip1dbg(("ip_rput_options: error processing IP options.")); 17943 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 17944 17945 param_prob: 17946 q = WR(q); 17947 if (q->q_next != NULL) 17948 ill = q->q_ptr; 17949 else 17950 ill = NULL; 17951 17952 /* make sure we clear any indication of a hardware checksum */ 17953 DB_CKSUMFLAGS(mp) = 0; 17954 /* Don't know whether this is for non-global or global/forwarding */ 17955 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 17956 if (zoneid == ALL_ZONES) 17957 freemsg(mp); 17958 else 17959 icmp_param_problem(q, mp, (uint8_t)code, zoneid, ipst); 17960 return (-1); 17961 17962 bad_src_route: 17963 q = WR(q); 17964 if (q->q_next != NULL) 17965 ill = q->q_ptr; 17966 else 17967 ill = NULL; 17968 17969 /* make sure we clear any indication of a hardware checksum */ 17970 DB_CKSUMFLAGS(mp) = 0; 17971 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 17972 if (zoneid == ALL_ZONES) 17973 freemsg(mp); 17974 else 17975 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 17976 return (-1); 17977 } 17978 17979 /* 17980 * IP & ICMP info in >=14 msg's ... 17981 * - ip fixed part (mib2_ip_t) 17982 * - icmp fixed part (mib2_icmp_t) 17983 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 17984 * - ipRouteEntryTable (ip 21) all IPv4 IREs 17985 * - ipNetToMediaEntryTable (ip 22) [filled in by the arp module] 17986 * - ipRouteAttributeTable (ip 102) labeled routes 17987 * - ip multicast membership (ip_member_t) 17988 * - ip multicast source filtering (ip_grpsrc_t) 17989 * - igmp fixed part (struct igmpstat) 17990 * - multicast routing stats (struct mrtstat) 17991 * - multicast routing vifs (array of struct vifctl) 17992 * - multicast routing routes (array of struct mfcctl) 17993 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 17994 * One per ill plus one generic 17995 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 17996 * One per ill plus one generic 17997 * - ipv6RouteEntry all IPv6 IREs 17998 * - ipv6RouteAttributeTable (ip6 102) labeled routes 17999 * - ipv6NetToMediaEntry all Neighbor Cache entries 18000 * - ipv6AddrEntry all IPv6 ipifs 18001 * - ipv6 multicast membership (ipv6_member_t) 18002 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 18003 * 18004 * MIB2_IP_MEDIA is filled in by the arp module with ARP cache entries. 18005 * 18006 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 18007 * already filled in by the caller. 18008 * Return value of 0 indicates that no messages were sent and caller 18009 * should free mpctl. 18010 */ 18011 int 18012 ip_snmp_get(queue_t *q, mblk_t *mpctl) 18013 { 18014 ip_stack_t *ipst; 18015 sctp_stack_t *sctps; 18016 18017 18018 if (q->q_next != NULL) { 18019 ipst = ILLQ_TO_IPST(q); 18020 } else { 18021 ipst = CONNQ_TO_IPST(q); 18022 } 18023 ASSERT(ipst != NULL); 18024 sctps = ipst->ips_netstack->netstack_sctp; 18025 18026 if (mpctl == NULL || mpctl->b_cont == NULL) { 18027 return (0); 18028 } 18029 18030 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl, 18031 ipst)) == NULL) { 18032 return (1); 18033 } 18034 18035 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst)) == NULL) { 18036 return (1); 18037 } 18038 18039 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) { 18040 return (1); 18041 } 18042 18043 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) { 18044 return (1); 18045 } 18046 18047 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) { 18048 return (1); 18049 } 18050 18051 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) { 18052 return (1); 18053 } 18054 18055 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst)) == NULL) { 18056 return (1); 18057 } 18058 18059 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst)) == NULL) { 18060 return (1); 18061 } 18062 18063 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) { 18064 return (1); 18065 } 18066 18067 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) { 18068 return (1); 18069 } 18070 18071 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) { 18072 return (1); 18073 } 18074 18075 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) { 18076 return (1); 18077 } 18078 18079 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) { 18080 return (1); 18081 } 18082 18083 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) { 18084 return (1); 18085 } 18086 18087 if ((mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, ipst)) == NULL) { 18088 return (1); 18089 } 18090 18091 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, ipst); 18092 if (mpctl == NULL) { 18093 return (1); 18094 } 18095 18096 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) { 18097 return (1); 18098 } 18099 freemsg(mpctl); 18100 return (1); 18101 } 18102 18103 18104 /* Get global (legacy) IPv4 statistics */ 18105 static mblk_t * 18106 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib, 18107 ip_stack_t *ipst) 18108 { 18109 mib2_ip_t old_ip_mib; 18110 struct opthdr *optp; 18111 mblk_t *mp2ctl; 18112 18113 /* 18114 * make a copy of the original message 18115 */ 18116 mp2ctl = copymsg(mpctl); 18117 18118 /* fixed length IP structure... */ 18119 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18120 optp->level = MIB2_IP; 18121 optp->name = 0; 18122 SET_MIB(old_ip_mib.ipForwarding, 18123 (WE_ARE_FORWARDING(ipst) ? 1 : 2)); 18124 SET_MIB(old_ip_mib.ipDefaultTTL, 18125 (uint32_t)ipst->ips_ip_def_ttl); 18126 SET_MIB(old_ip_mib.ipReasmTimeout, 18127 ipst->ips_ip_g_frag_timeout); 18128 SET_MIB(old_ip_mib.ipAddrEntrySize, 18129 sizeof (mib2_ipAddrEntry_t)); 18130 SET_MIB(old_ip_mib.ipRouteEntrySize, 18131 sizeof (mib2_ipRouteEntry_t)); 18132 SET_MIB(old_ip_mib.ipNetToMediaEntrySize, 18133 sizeof (mib2_ipNetToMediaEntry_t)); 18134 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 18135 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 18136 SET_MIB(old_ip_mib.ipRouteAttributeSize, 18137 sizeof (mib2_ipAttributeEntry_t)); 18138 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 18139 18140 /* 18141 * Grab the statistics from the new IP MIB 18142 */ 18143 SET_MIB(old_ip_mib.ipInReceives, 18144 (uint32_t)ipmib->ipIfStatsHCInReceives); 18145 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors); 18146 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors); 18147 SET_MIB(old_ip_mib.ipForwDatagrams, 18148 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams); 18149 SET_MIB(old_ip_mib.ipInUnknownProtos, 18150 ipmib->ipIfStatsInUnknownProtos); 18151 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards); 18152 SET_MIB(old_ip_mib.ipInDelivers, 18153 (uint32_t)ipmib->ipIfStatsHCInDelivers); 18154 SET_MIB(old_ip_mib.ipOutRequests, 18155 (uint32_t)ipmib->ipIfStatsHCOutRequests); 18156 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards); 18157 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes); 18158 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds); 18159 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs); 18160 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails); 18161 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs); 18162 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails); 18163 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates); 18164 18165 /* ipRoutingDiscards is not being used */ 18166 SET_MIB(old_ip_mib.ipRoutingDiscards, 0); 18167 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs); 18168 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts); 18169 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs); 18170 SET_MIB(old_ip_mib.ipReasmDuplicates, 18171 ipmib->ipIfStatsReasmDuplicates); 18172 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups); 18173 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits); 18174 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs); 18175 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows); 18176 SET_MIB(old_ip_mib.rawipInOverflows, 18177 ipmib->rawipIfStatsInOverflows); 18178 18179 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded); 18180 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed); 18181 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion); 18182 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion); 18183 SET_MIB(old_ip_mib.ipOutSwitchIPv6, 18184 ipmib->ipIfStatsOutSwitchIPVersion); 18185 18186 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib, 18187 (int)sizeof (old_ip_mib))) { 18188 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 18189 (uint_t)sizeof (old_ip_mib))); 18190 } 18191 18192 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18193 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 18194 (int)optp->level, (int)optp->name, (int)optp->len)); 18195 qreply(q, mpctl); 18196 return (mp2ctl); 18197 } 18198 18199 /* Per interface IPv4 statistics */ 18200 static mblk_t * 18201 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18202 { 18203 struct opthdr *optp; 18204 mblk_t *mp2ctl; 18205 ill_t *ill; 18206 ill_walk_context_t ctx; 18207 mblk_t *mp_tail = NULL; 18208 mib2_ipIfStatsEntry_t global_ip_mib; 18209 18210 /* 18211 * Make a copy of the original message 18212 */ 18213 mp2ctl = copymsg(mpctl); 18214 18215 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18216 optp->level = MIB2_IP; 18217 optp->name = MIB2_IP_TRAFFIC_STATS; 18218 /* Include "unknown interface" ip_mib */ 18219 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 18220 ipst->ips_ip_mib.ipIfStatsIfIndex = 18221 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 18222 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding, 18223 (ipst->ips_ip_g_forward ? 1 : 2)); 18224 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL, 18225 (uint32_t)ipst->ips_ip_def_ttl); 18226 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize, 18227 sizeof (mib2_ipIfStatsEntry_t)); 18228 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize, 18229 sizeof (mib2_ipAddrEntry_t)); 18230 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize, 18231 sizeof (mib2_ipRouteEntry_t)); 18232 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize, 18233 sizeof (mib2_ipNetToMediaEntry_t)); 18234 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize, 18235 sizeof (ip_member_t)); 18236 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize, 18237 sizeof (ip_grpsrc_t)); 18238 18239 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18240 (char *)&ipst->ips_ip_mib, (int)sizeof (ipst->ips_ip_mib))) { 18241 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18242 "failed to allocate %u bytes\n", 18243 (uint_t)sizeof (ipst->ips_ip_mib))); 18244 } 18245 18246 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib)); 18247 18248 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18249 ill = ILL_START_WALK_V4(&ctx, ipst); 18250 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18251 ill->ill_ip_mib->ipIfStatsIfIndex = 18252 ill->ill_phyint->phyint_ifindex; 18253 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 18254 (ipst->ips_ip_g_forward ? 1 : 2)); 18255 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL, 18256 (uint32_t)ipst->ips_ip_def_ttl); 18257 18258 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib); 18259 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18260 (char *)ill->ill_ip_mib, 18261 (int)sizeof (*ill->ill_ip_mib))) { 18262 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18263 "failed to allocate %u bytes\n", 18264 (uint_t)sizeof (*ill->ill_ip_mib))); 18265 } 18266 } 18267 rw_exit(&ipst->ips_ill_g_lock); 18268 18269 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18270 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18271 "level %d, name %d, len %d\n", 18272 (int)optp->level, (int)optp->name, (int)optp->len)); 18273 qreply(q, mpctl); 18274 18275 if (mp2ctl == NULL) 18276 return (NULL); 18277 18278 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst)); 18279 } 18280 18281 /* Global IPv4 ICMP statistics */ 18282 static mblk_t * 18283 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18284 { 18285 struct opthdr *optp; 18286 mblk_t *mp2ctl; 18287 18288 /* 18289 * Make a copy of the original message 18290 */ 18291 mp2ctl = copymsg(mpctl); 18292 18293 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18294 optp->level = MIB2_ICMP; 18295 optp->name = 0; 18296 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib, 18297 (int)sizeof (ipst->ips_icmp_mib))) { 18298 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 18299 (uint_t)sizeof (ipst->ips_icmp_mib))); 18300 } 18301 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18302 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 18303 (int)optp->level, (int)optp->name, (int)optp->len)); 18304 qreply(q, mpctl); 18305 return (mp2ctl); 18306 } 18307 18308 /* Global IPv4 IGMP statistics */ 18309 static mblk_t * 18310 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18311 { 18312 struct opthdr *optp; 18313 mblk_t *mp2ctl; 18314 18315 /* 18316 * make a copy of the original message 18317 */ 18318 mp2ctl = copymsg(mpctl); 18319 18320 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18321 optp->level = EXPER_IGMP; 18322 optp->name = 0; 18323 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat, 18324 (int)sizeof (ipst->ips_igmpstat))) { 18325 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 18326 (uint_t)sizeof (ipst->ips_igmpstat))); 18327 } 18328 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18329 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 18330 (int)optp->level, (int)optp->name, (int)optp->len)); 18331 qreply(q, mpctl); 18332 return (mp2ctl); 18333 } 18334 18335 /* Global IPv4 Multicast Routing statistics */ 18336 static mblk_t * 18337 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18338 { 18339 struct opthdr *optp; 18340 mblk_t *mp2ctl; 18341 18342 /* 18343 * make a copy of the original message 18344 */ 18345 mp2ctl = copymsg(mpctl); 18346 18347 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18348 optp->level = EXPER_DVMRP; 18349 optp->name = 0; 18350 if (!ip_mroute_stats(mpctl->b_cont, ipst)) { 18351 ip0dbg(("ip_mroute_stats: failed\n")); 18352 } 18353 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18354 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 18355 (int)optp->level, (int)optp->name, (int)optp->len)); 18356 qreply(q, mpctl); 18357 return (mp2ctl); 18358 } 18359 18360 /* IPv4 address information */ 18361 static mblk_t * 18362 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18363 { 18364 struct opthdr *optp; 18365 mblk_t *mp2ctl; 18366 mblk_t *mp_tail = NULL; 18367 ill_t *ill; 18368 ipif_t *ipif; 18369 uint_t bitval; 18370 mib2_ipAddrEntry_t mae; 18371 zoneid_t zoneid; 18372 ill_walk_context_t ctx; 18373 18374 /* 18375 * make a copy of the original message 18376 */ 18377 mp2ctl = copymsg(mpctl); 18378 18379 /* ipAddrEntryTable */ 18380 18381 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18382 optp->level = MIB2_IP; 18383 optp->name = MIB2_IP_ADDR; 18384 zoneid = Q_TO_CONN(q)->conn_zoneid; 18385 18386 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18387 ill = ILL_START_WALK_V4(&ctx, ipst); 18388 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18389 for (ipif = ill->ill_ipif; ipif != NULL; 18390 ipif = ipif->ipif_next) { 18391 if (ipif->ipif_zoneid != zoneid && 18392 ipif->ipif_zoneid != ALL_ZONES) 18393 continue; 18394 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18395 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18396 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18397 18398 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes, 18399 OCTET_LENGTH); 18400 mae.ipAdEntIfIndex.o_length = 18401 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 18402 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 18403 mae.ipAdEntNetMask = ipif->ipif_net_mask; 18404 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 18405 mae.ipAdEntInfo.ae_subnet_len = 18406 ip_mask_to_plen(ipif->ipif_net_mask); 18407 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 18408 for (bitval = 1; 18409 bitval && 18410 !(bitval & ipif->ipif_brd_addr); 18411 bitval <<= 1) 18412 noop; 18413 mae.ipAdEntBcastAddr = bitval; 18414 mae.ipAdEntReasmMaxSize = IP_MAXPACKET; 18415 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 18416 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 18417 mae.ipAdEntInfo.ae_broadcast_addr = 18418 ipif->ipif_brd_addr; 18419 mae.ipAdEntInfo.ae_pp_dst_addr = 18420 ipif->ipif_pp_dst_addr; 18421 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 18422 ill->ill_flags | ill->ill_phyint->phyint_flags; 18423 mae.ipAdEntRetransmitTime = AR_EQ_DEFAULT_XMIT_INTERVAL; 18424 18425 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18426 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 18427 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 18428 "allocate %u bytes\n", 18429 (uint_t)sizeof (mib2_ipAddrEntry_t))); 18430 } 18431 } 18432 } 18433 rw_exit(&ipst->ips_ill_g_lock); 18434 18435 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18436 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 18437 (int)optp->level, (int)optp->name, (int)optp->len)); 18438 qreply(q, mpctl); 18439 return (mp2ctl); 18440 } 18441 18442 /* IPv6 address information */ 18443 static mblk_t * 18444 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18445 { 18446 struct opthdr *optp; 18447 mblk_t *mp2ctl; 18448 mblk_t *mp_tail = NULL; 18449 ill_t *ill; 18450 ipif_t *ipif; 18451 mib2_ipv6AddrEntry_t mae6; 18452 zoneid_t zoneid; 18453 ill_walk_context_t ctx; 18454 18455 /* 18456 * make a copy of the original message 18457 */ 18458 mp2ctl = copymsg(mpctl); 18459 18460 /* ipv6AddrEntryTable */ 18461 18462 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18463 optp->level = MIB2_IP6; 18464 optp->name = MIB2_IP6_ADDR; 18465 zoneid = Q_TO_CONN(q)->conn_zoneid; 18466 18467 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18468 ill = ILL_START_WALK_V6(&ctx, ipst); 18469 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18470 for (ipif = ill->ill_ipif; ipif != NULL; 18471 ipif = ipif->ipif_next) { 18472 if (ipif->ipif_zoneid != zoneid && 18473 ipif->ipif_zoneid != ALL_ZONES) 18474 continue; 18475 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18476 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18477 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18478 18479 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes, 18480 OCTET_LENGTH); 18481 mae6.ipv6AddrIfIndex.o_length = 18482 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 18483 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 18484 mae6.ipv6AddrPfxLength = 18485 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 18486 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 18487 mae6.ipv6AddrInfo.ae_subnet_len = 18488 mae6.ipv6AddrPfxLength; 18489 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 18490 18491 /* Type: stateless(1), stateful(2), unknown(3) */ 18492 if (ipif->ipif_flags & IPIF_ADDRCONF) 18493 mae6.ipv6AddrType = 1; 18494 else 18495 mae6.ipv6AddrType = 2; 18496 /* Anycast: true(1), false(2) */ 18497 if (ipif->ipif_flags & IPIF_ANYCAST) 18498 mae6.ipv6AddrAnycastFlag = 1; 18499 else 18500 mae6.ipv6AddrAnycastFlag = 2; 18501 18502 /* 18503 * Address status: preferred(1), deprecated(2), 18504 * invalid(3), inaccessible(4), unknown(5) 18505 */ 18506 if (ipif->ipif_flags & IPIF_NOLOCAL) 18507 mae6.ipv6AddrStatus = 3; 18508 else if (ipif->ipif_flags & IPIF_DEPRECATED) 18509 mae6.ipv6AddrStatus = 2; 18510 else 18511 mae6.ipv6AddrStatus = 1; 18512 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 18513 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 18514 mae6.ipv6AddrInfo.ae_pp_dst_addr = 18515 ipif->ipif_v6pp_dst_addr; 18516 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 18517 ill->ill_flags | ill->ill_phyint->phyint_flags; 18518 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET; 18519 mae6.ipv6AddrIdentifier = ill->ill_token; 18520 mae6.ipv6AddrIdentifierLen = ill->ill_token_length; 18521 mae6.ipv6AddrReachableTime = ill->ill_reachable_time; 18522 mae6.ipv6AddrRetransmitTime = 18523 ill->ill_reachable_retrans_time; 18524 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18525 (char *)&mae6, 18526 (int)sizeof (mib2_ipv6AddrEntry_t))) { 18527 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 18528 "allocate %u bytes\n", 18529 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 18530 } 18531 } 18532 } 18533 rw_exit(&ipst->ips_ill_g_lock); 18534 18535 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18536 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 18537 (int)optp->level, (int)optp->name, (int)optp->len)); 18538 qreply(q, mpctl); 18539 return (mp2ctl); 18540 } 18541 18542 /* IPv4 multicast group membership. */ 18543 static mblk_t * 18544 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18545 { 18546 struct opthdr *optp; 18547 mblk_t *mp2ctl; 18548 ill_t *ill; 18549 ipif_t *ipif; 18550 ilm_t *ilm; 18551 ip_member_t ipm; 18552 mblk_t *mp_tail = NULL; 18553 ill_walk_context_t ctx; 18554 zoneid_t zoneid; 18555 18556 /* 18557 * make a copy of the original message 18558 */ 18559 mp2ctl = copymsg(mpctl); 18560 zoneid = Q_TO_CONN(q)->conn_zoneid; 18561 18562 /* ipGroupMember table */ 18563 optp = (struct opthdr *)&mpctl->b_rptr[ 18564 sizeof (struct T_optmgmt_ack)]; 18565 optp->level = MIB2_IP; 18566 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 18567 18568 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18569 ill = ILL_START_WALK_V4(&ctx, ipst); 18570 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18571 ILM_WALKER_HOLD(ill); 18572 for (ipif = ill->ill_ipif; ipif != NULL; 18573 ipif = ipif->ipif_next) { 18574 if (ipif->ipif_zoneid != zoneid && 18575 ipif->ipif_zoneid != ALL_ZONES) 18576 continue; /* not this zone */ 18577 ipif_get_name(ipif, ipm.ipGroupMemberIfIndex.o_bytes, 18578 OCTET_LENGTH); 18579 ipm.ipGroupMemberIfIndex.o_length = 18580 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 18581 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18582 ASSERT(ilm->ilm_ipif != NULL); 18583 ASSERT(ilm->ilm_ill == NULL); 18584 if (ilm->ilm_ipif != ipif) 18585 continue; 18586 ipm.ipGroupMemberAddress = ilm->ilm_addr; 18587 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 18588 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 18589 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18590 (char *)&ipm, (int)sizeof (ipm))) { 18591 ip1dbg(("ip_snmp_get_mib2_ip_group: " 18592 "failed to allocate %u bytes\n", 18593 (uint_t)sizeof (ipm))); 18594 } 18595 } 18596 } 18597 ILM_WALKER_RELE(ill); 18598 } 18599 rw_exit(&ipst->ips_ill_g_lock); 18600 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18601 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18602 (int)optp->level, (int)optp->name, (int)optp->len)); 18603 qreply(q, mpctl); 18604 return (mp2ctl); 18605 } 18606 18607 /* IPv6 multicast group membership. */ 18608 static mblk_t * 18609 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18610 { 18611 struct opthdr *optp; 18612 mblk_t *mp2ctl; 18613 ill_t *ill; 18614 ilm_t *ilm; 18615 ipv6_member_t ipm6; 18616 mblk_t *mp_tail = NULL; 18617 ill_walk_context_t ctx; 18618 zoneid_t zoneid; 18619 18620 /* 18621 * make a copy of the original message 18622 */ 18623 mp2ctl = copymsg(mpctl); 18624 zoneid = Q_TO_CONN(q)->conn_zoneid; 18625 18626 /* ip6GroupMember table */ 18627 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18628 optp->level = MIB2_IP6; 18629 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 18630 18631 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18632 ill = ILL_START_WALK_V6(&ctx, ipst); 18633 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18634 ILM_WALKER_HOLD(ill); 18635 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 18636 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18637 ASSERT(ilm->ilm_ipif == NULL); 18638 ASSERT(ilm->ilm_ill != NULL); 18639 if (ilm->ilm_zoneid != zoneid) 18640 continue; /* not this zone */ 18641 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 18642 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 18643 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 18644 if (!snmp_append_data2(mpctl->b_cont, 18645 &mp_tail, 18646 (char *)&ipm6, (int)sizeof (ipm6))) { 18647 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 18648 "failed to allocate %u bytes\n", 18649 (uint_t)sizeof (ipm6))); 18650 } 18651 } 18652 ILM_WALKER_RELE(ill); 18653 } 18654 rw_exit(&ipst->ips_ill_g_lock); 18655 18656 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18657 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18658 (int)optp->level, (int)optp->name, (int)optp->len)); 18659 qreply(q, mpctl); 18660 return (mp2ctl); 18661 } 18662 18663 /* IP multicast filtered sources */ 18664 static mblk_t * 18665 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18666 { 18667 struct opthdr *optp; 18668 mblk_t *mp2ctl; 18669 ill_t *ill; 18670 ipif_t *ipif; 18671 ilm_t *ilm; 18672 ip_grpsrc_t ips; 18673 mblk_t *mp_tail = NULL; 18674 ill_walk_context_t ctx; 18675 zoneid_t zoneid; 18676 int i; 18677 slist_t *sl; 18678 18679 /* 18680 * make a copy of the original message 18681 */ 18682 mp2ctl = copymsg(mpctl); 18683 zoneid = Q_TO_CONN(q)->conn_zoneid; 18684 18685 /* ipGroupSource table */ 18686 optp = (struct opthdr *)&mpctl->b_rptr[ 18687 sizeof (struct T_optmgmt_ack)]; 18688 optp->level = MIB2_IP; 18689 optp->name = EXPER_IP_GROUP_SOURCES; 18690 18691 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18692 ill = ILL_START_WALK_V4(&ctx, ipst); 18693 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18694 ILM_WALKER_HOLD(ill); 18695 for (ipif = ill->ill_ipif; ipif != NULL; 18696 ipif = ipif->ipif_next) { 18697 if (ipif->ipif_zoneid != zoneid) 18698 continue; /* not this zone */ 18699 ipif_get_name(ipif, ips.ipGroupSourceIfIndex.o_bytes, 18700 OCTET_LENGTH); 18701 ips.ipGroupSourceIfIndex.o_length = 18702 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 18703 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18704 ASSERT(ilm->ilm_ipif != NULL); 18705 ASSERT(ilm->ilm_ill == NULL); 18706 sl = ilm->ilm_filter; 18707 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 18708 continue; 18709 ips.ipGroupSourceGroup = ilm->ilm_addr; 18710 for (i = 0; i < sl->sl_numsrc; i++) { 18711 if (!IN6_IS_ADDR_V4MAPPED( 18712 &sl->sl_addr[i])) 18713 continue; 18714 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 18715 ips.ipGroupSourceAddress); 18716 if (snmp_append_data2(mpctl->b_cont, 18717 &mp_tail, (char *)&ips, 18718 (int)sizeof (ips)) == 0) { 18719 ip1dbg(("ip_snmp_get_mib2_" 18720 "ip_group_src: failed to " 18721 "allocate %u bytes\n", 18722 (uint_t)sizeof (ips))); 18723 } 18724 } 18725 } 18726 } 18727 ILM_WALKER_RELE(ill); 18728 } 18729 rw_exit(&ipst->ips_ill_g_lock); 18730 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18731 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18732 (int)optp->level, (int)optp->name, (int)optp->len)); 18733 qreply(q, mpctl); 18734 return (mp2ctl); 18735 } 18736 18737 /* IPv6 multicast filtered sources. */ 18738 static mblk_t * 18739 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18740 { 18741 struct opthdr *optp; 18742 mblk_t *mp2ctl; 18743 ill_t *ill; 18744 ilm_t *ilm; 18745 ipv6_grpsrc_t ips6; 18746 mblk_t *mp_tail = NULL; 18747 ill_walk_context_t ctx; 18748 zoneid_t zoneid; 18749 int i; 18750 slist_t *sl; 18751 18752 /* 18753 * make a copy of the original message 18754 */ 18755 mp2ctl = copymsg(mpctl); 18756 zoneid = Q_TO_CONN(q)->conn_zoneid; 18757 18758 /* ip6GroupMember table */ 18759 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18760 optp->level = MIB2_IP6; 18761 optp->name = EXPER_IP6_GROUP_SOURCES; 18762 18763 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18764 ill = ILL_START_WALK_V6(&ctx, ipst); 18765 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18766 ILM_WALKER_HOLD(ill); 18767 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 18768 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18769 ASSERT(ilm->ilm_ipif == NULL); 18770 ASSERT(ilm->ilm_ill != NULL); 18771 sl = ilm->ilm_filter; 18772 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 18773 continue; 18774 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 18775 for (i = 0; i < sl->sl_numsrc; i++) { 18776 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 18777 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18778 (char *)&ips6, (int)sizeof (ips6))) { 18779 ip1dbg(("ip_snmp_get_mib2_ip6_" 18780 "group_src: failed to allocate " 18781 "%u bytes\n", 18782 (uint_t)sizeof (ips6))); 18783 } 18784 } 18785 } 18786 ILM_WALKER_RELE(ill); 18787 } 18788 rw_exit(&ipst->ips_ill_g_lock); 18789 18790 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18791 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18792 (int)optp->level, (int)optp->name, (int)optp->len)); 18793 qreply(q, mpctl); 18794 return (mp2ctl); 18795 } 18796 18797 /* Multicast routing virtual interface table. */ 18798 static mblk_t * 18799 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18800 { 18801 struct opthdr *optp; 18802 mblk_t *mp2ctl; 18803 18804 /* 18805 * make a copy of the original message 18806 */ 18807 mp2ctl = copymsg(mpctl); 18808 18809 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18810 optp->level = EXPER_DVMRP; 18811 optp->name = EXPER_DVMRP_VIF; 18812 if (!ip_mroute_vif(mpctl->b_cont, ipst)) { 18813 ip0dbg(("ip_mroute_vif: failed\n")); 18814 } 18815 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18816 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 18817 (int)optp->level, (int)optp->name, (int)optp->len)); 18818 qreply(q, mpctl); 18819 return (mp2ctl); 18820 } 18821 18822 /* Multicast routing table. */ 18823 static mblk_t * 18824 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18825 { 18826 struct opthdr *optp; 18827 mblk_t *mp2ctl; 18828 18829 /* 18830 * make a copy of the original message 18831 */ 18832 mp2ctl = copymsg(mpctl); 18833 18834 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18835 optp->level = EXPER_DVMRP; 18836 optp->name = EXPER_DVMRP_MRT; 18837 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) { 18838 ip0dbg(("ip_mroute_mrt: failed\n")); 18839 } 18840 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18841 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 18842 (int)optp->level, (int)optp->name, (int)optp->len)); 18843 qreply(q, mpctl); 18844 return (mp2ctl); 18845 } 18846 18847 /* 18848 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 18849 * in one IRE walk. 18850 */ 18851 static mblk_t * 18852 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18853 { 18854 struct opthdr *optp; 18855 mblk_t *mp2ctl; /* Returned */ 18856 mblk_t *mp3ctl; /* nettomedia */ 18857 mblk_t *mp4ctl; /* routeattrs */ 18858 iproutedata_t ird; 18859 zoneid_t zoneid; 18860 18861 /* 18862 * make copies of the original message 18863 * - mp2ctl is returned unchanged to the caller for his use 18864 * - mpctl is sent upstream as ipRouteEntryTable 18865 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 18866 * - mp4ctl is sent upstream as ipRouteAttributeTable 18867 */ 18868 mp2ctl = copymsg(mpctl); 18869 mp3ctl = copymsg(mpctl); 18870 mp4ctl = copymsg(mpctl); 18871 if (mp3ctl == NULL || mp4ctl == NULL) { 18872 freemsg(mp4ctl); 18873 freemsg(mp3ctl); 18874 freemsg(mp2ctl); 18875 freemsg(mpctl); 18876 return (NULL); 18877 } 18878 18879 bzero(&ird, sizeof (ird)); 18880 18881 ird.ird_route.lp_head = mpctl->b_cont; 18882 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 18883 ird.ird_attrs.lp_head = mp4ctl->b_cont; 18884 18885 zoneid = Q_TO_CONN(q)->conn_zoneid; 18886 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst); 18887 18888 /* ipRouteEntryTable in mpctl */ 18889 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18890 optp->level = MIB2_IP; 18891 optp->name = MIB2_IP_ROUTE; 18892 optp->len = msgdsize(ird.ird_route.lp_head); 18893 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18894 (int)optp->level, (int)optp->name, (int)optp->len)); 18895 qreply(q, mpctl); 18896 18897 /* ipNetToMediaEntryTable in mp3ctl */ 18898 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18899 optp->level = MIB2_IP; 18900 optp->name = MIB2_IP_MEDIA; 18901 optp->len = msgdsize(ird.ird_netmedia.lp_head); 18902 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18903 (int)optp->level, (int)optp->name, (int)optp->len)); 18904 qreply(q, mp3ctl); 18905 18906 /* ipRouteAttributeTable in mp4ctl */ 18907 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18908 optp->level = MIB2_IP; 18909 optp->name = EXPER_IP_RTATTR; 18910 optp->len = msgdsize(ird.ird_attrs.lp_head); 18911 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18912 (int)optp->level, (int)optp->name, (int)optp->len)); 18913 if (optp->len == 0) 18914 freemsg(mp4ctl); 18915 else 18916 qreply(q, mp4ctl); 18917 18918 return (mp2ctl); 18919 } 18920 18921 /* 18922 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 18923 * ipv6NetToMediaEntryTable in an NDP walk. 18924 */ 18925 static mblk_t * 18926 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18927 { 18928 struct opthdr *optp; 18929 mblk_t *mp2ctl; /* Returned */ 18930 mblk_t *mp3ctl; /* nettomedia */ 18931 mblk_t *mp4ctl; /* routeattrs */ 18932 iproutedata_t ird; 18933 zoneid_t zoneid; 18934 18935 /* 18936 * make copies of the original message 18937 * - mp2ctl is returned unchanged to the caller for his use 18938 * - mpctl is sent upstream as ipv6RouteEntryTable 18939 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 18940 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 18941 */ 18942 mp2ctl = copymsg(mpctl); 18943 mp3ctl = copymsg(mpctl); 18944 mp4ctl = copymsg(mpctl); 18945 if (mp3ctl == NULL || mp4ctl == NULL) { 18946 freemsg(mp4ctl); 18947 freemsg(mp3ctl); 18948 freemsg(mp2ctl); 18949 freemsg(mpctl); 18950 return (NULL); 18951 } 18952 18953 bzero(&ird, sizeof (ird)); 18954 18955 ird.ird_route.lp_head = mpctl->b_cont; 18956 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 18957 ird.ird_attrs.lp_head = mp4ctl->b_cont; 18958 18959 zoneid = Q_TO_CONN(q)->conn_zoneid; 18960 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst); 18961 18962 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18963 optp->level = MIB2_IP6; 18964 optp->name = MIB2_IP6_ROUTE; 18965 optp->len = msgdsize(ird.ird_route.lp_head); 18966 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18967 (int)optp->level, (int)optp->name, (int)optp->len)); 18968 qreply(q, mpctl); 18969 18970 /* ipv6NetToMediaEntryTable in mp3ctl */ 18971 ndp_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst); 18972 18973 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18974 optp->level = MIB2_IP6; 18975 optp->name = MIB2_IP6_MEDIA; 18976 optp->len = msgdsize(ird.ird_netmedia.lp_head); 18977 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18978 (int)optp->level, (int)optp->name, (int)optp->len)); 18979 qreply(q, mp3ctl); 18980 18981 /* ipv6RouteAttributeTable in mp4ctl */ 18982 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18983 optp->level = MIB2_IP6; 18984 optp->name = EXPER_IP_RTATTR; 18985 optp->len = msgdsize(ird.ird_attrs.lp_head); 18986 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18987 (int)optp->level, (int)optp->name, (int)optp->len)); 18988 if (optp->len == 0) 18989 freemsg(mp4ctl); 18990 else 18991 qreply(q, mp4ctl); 18992 18993 return (mp2ctl); 18994 } 18995 18996 /* 18997 * IPv6 mib: One per ill 18998 */ 18999 static mblk_t * 19000 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19001 { 19002 struct opthdr *optp; 19003 mblk_t *mp2ctl; 19004 ill_t *ill; 19005 ill_walk_context_t ctx; 19006 mblk_t *mp_tail = NULL; 19007 19008 /* 19009 * Make a copy of the original message 19010 */ 19011 mp2ctl = copymsg(mpctl); 19012 19013 /* fixed length IPv6 structure ... */ 19014 19015 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19016 optp->level = MIB2_IP6; 19017 optp->name = 0; 19018 /* Include "unknown interface" ip6_mib */ 19019 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 19020 ipst->ips_ip6_mib.ipIfStatsIfIndex = 19021 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 19022 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding, 19023 ipst->ips_ipv6_forward ? 1 : 2); 19024 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit, 19025 ipst->ips_ipv6_def_hops); 19026 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize, 19027 sizeof (mib2_ipIfStatsEntry_t)); 19028 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize, 19029 sizeof (mib2_ipv6AddrEntry_t)); 19030 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize, 19031 sizeof (mib2_ipv6RouteEntry_t)); 19032 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize, 19033 sizeof (mib2_ipv6NetToMediaEntry_t)); 19034 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize, 19035 sizeof (ipv6_member_t)); 19036 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize, 19037 sizeof (ipv6_grpsrc_t)); 19038 19039 /* 19040 * Synchronize 64- and 32-bit counters 19041 */ 19042 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives, 19043 ipIfStatsHCInReceives); 19044 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers, 19045 ipIfStatsHCInDelivers); 19046 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests, 19047 ipIfStatsHCOutRequests); 19048 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams, 19049 ipIfStatsHCOutForwDatagrams); 19050 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts, 19051 ipIfStatsHCOutMcastPkts); 19052 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts, 19053 ipIfStatsHCInMcastPkts); 19054 19055 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19056 (char *)&ipst->ips_ip6_mib, (int)sizeof (ipst->ips_ip6_mib))) { 19057 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 19058 (uint_t)sizeof (ipst->ips_ip6_mib))); 19059 } 19060 19061 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19062 ill = ILL_START_WALK_V6(&ctx, ipst); 19063 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19064 ill->ill_ip_mib->ipIfStatsIfIndex = 19065 ill->ill_phyint->phyint_ifindex; 19066 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 19067 ipst->ips_ipv6_forward ? 1 : 2); 19068 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit, 19069 ill->ill_max_hops); 19070 19071 /* 19072 * Synchronize 64- and 32-bit counters 19073 */ 19074 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives, 19075 ipIfStatsHCInReceives); 19076 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers, 19077 ipIfStatsHCInDelivers); 19078 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests, 19079 ipIfStatsHCOutRequests); 19080 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams, 19081 ipIfStatsHCOutForwDatagrams); 19082 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts, 19083 ipIfStatsHCOutMcastPkts); 19084 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts, 19085 ipIfStatsHCInMcastPkts); 19086 19087 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19088 (char *)ill->ill_ip_mib, 19089 (int)sizeof (*ill->ill_ip_mib))) { 19090 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 19091 "%u bytes\n", (uint_t)sizeof (*ill->ill_ip_mib))); 19092 } 19093 } 19094 rw_exit(&ipst->ips_ill_g_lock); 19095 19096 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19097 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 19098 (int)optp->level, (int)optp->name, (int)optp->len)); 19099 qreply(q, mpctl); 19100 return (mp2ctl); 19101 } 19102 19103 /* 19104 * ICMPv6 mib: One per ill 19105 */ 19106 static mblk_t * 19107 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19108 { 19109 struct opthdr *optp; 19110 mblk_t *mp2ctl; 19111 ill_t *ill; 19112 ill_walk_context_t ctx; 19113 mblk_t *mp_tail = NULL; 19114 /* 19115 * Make a copy of the original message 19116 */ 19117 mp2ctl = copymsg(mpctl); 19118 19119 /* fixed length ICMPv6 structure ... */ 19120 19121 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19122 optp->level = MIB2_ICMP6; 19123 optp->name = 0; 19124 /* Include "unknown interface" icmp6_mib */ 19125 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex = 19126 MIB2_UNKNOWN_INTERFACE; /* netstat flag */ 19127 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize = 19128 sizeof (mib2_ipv6IfIcmpEntry_t); 19129 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19130 (char *)&ipst->ips_icmp6_mib, 19131 (int)sizeof (ipst->ips_icmp6_mib))) { 19132 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 19133 (uint_t)sizeof (ipst->ips_icmp6_mib))); 19134 } 19135 19136 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19137 ill = ILL_START_WALK_V6(&ctx, ipst); 19138 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19139 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 19140 ill->ill_phyint->phyint_ifindex; 19141 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19142 (char *)ill->ill_icmp6_mib, 19143 (int)sizeof (*ill->ill_icmp6_mib))) { 19144 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 19145 "%u bytes\n", 19146 (uint_t)sizeof (*ill->ill_icmp6_mib))); 19147 } 19148 } 19149 rw_exit(&ipst->ips_ill_g_lock); 19150 19151 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19152 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 19153 (int)optp->level, (int)optp->name, (int)optp->len)); 19154 qreply(q, mpctl); 19155 return (mp2ctl); 19156 } 19157 19158 /* 19159 * ire_walk routine to create both ipRouteEntryTable and 19160 * ipRouteAttributeTable in one IRE walk 19161 */ 19162 static void 19163 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 19164 { 19165 ill_t *ill; 19166 ipif_t *ipif; 19167 mib2_ipRouteEntry_t *re; 19168 mib2_ipAttributeEntry_t *iae, *iaeptr; 19169 ipaddr_t gw_addr; 19170 tsol_ire_gw_secattr_t *attrp; 19171 tsol_gc_t *gc = NULL; 19172 tsol_gcgrp_t *gcgrp = NULL; 19173 uint_t sacnt = 0; 19174 int i; 19175 19176 ASSERT(ire->ire_ipversion == IPV4_VERSION); 19177 19178 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19179 return; 19180 19181 if ((attrp = ire->ire_gw_secattr) != NULL) { 19182 mutex_enter(&attrp->igsa_lock); 19183 if ((gc = attrp->igsa_gc) != NULL) { 19184 gcgrp = gc->gc_grp; 19185 ASSERT(gcgrp != NULL); 19186 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19187 sacnt = 1; 19188 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19189 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19190 gc = gcgrp->gcgrp_head; 19191 sacnt = gcgrp->gcgrp_count; 19192 } 19193 mutex_exit(&attrp->igsa_lock); 19194 19195 /* do nothing if there's no gc to report */ 19196 if (gc == NULL) { 19197 ASSERT(sacnt == 0); 19198 if (gcgrp != NULL) { 19199 /* we might as well drop the lock now */ 19200 rw_exit(&gcgrp->gcgrp_rwlock); 19201 gcgrp = NULL; 19202 } 19203 attrp = NULL; 19204 } 19205 19206 ASSERT(gc == NULL || (gcgrp != NULL && 19207 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19208 } 19209 ASSERT(sacnt == 0 || gc != NULL); 19210 19211 if (sacnt != 0 && 19212 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19213 kmem_free(re, sizeof (*re)); 19214 rw_exit(&gcgrp->gcgrp_rwlock); 19215 return; 19216 } 19217 19218 /* 19219 * Return all IRE types for route table... let caller pick and choose 19220 */ 19221 re->ipRouteDest = ire->ire_addr; 19222 ipif = ire->ire_ipif; 19223 re->ipRouteIfIndex.o_length = 0; 19224 if (ire->ire_type == IRE_CACHE) { 19225 ill = (ill_t *)ire->ire_stq->q_ptr; 19226 re->ipRouteIfIndex.o_length = 19227 ill->ill_name_length == 0 ? 0 : 19228 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19229 bcopy(ill->ill_name, re->ipRouteIfIndex.o_bytes, 19230 re->ipRouteIfIndex.o_length); 19231 } else if (ipif != NULL) { 19232 ipif_get_name(ipif, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH); 19233 re->ipRouteIfIndex.o_length = 19234 mi_strlen(re->ipRouteIfIndex.o_bytes); 19235 } 19236 re->ipRouteMetric1 = -1; 19237 re->ipRouteMetric2 = -1; 19238 re->ipRouteMetric3 = -1; 19239 re->ipRouteMetric4 = -1; 19240 19241 gw_addr = ire->ire_gateway_addr; 19242 19243 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 19244 re->ipRouteNextHop = ire->ire_src_addr; 19245 else 19246 re->ipRouteNextHop = gw_addr; 19247 /* indirect(4), direct(3), or invalid(2) */ 19248 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19249 re->ipRouteType = 2; 19250 else 19251 re->ipRouteType = (gw_addr != 0) ? 4 : 3; 19252 re->ipRouteProto = -1; 19253 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 19254 re->ipRouteMask = ire->ire_mask; 19255 re->ipRouteMetric5 = -1; 19256 re->ipRouteInfo.re_max_frag = ire->ire_max_frag; 19257 re->ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 19258 re->ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19259 re->ipRouteInfo.re_ref = ire->ire_refcnt; 19260 re->ipRouteInfo.re_src_addr = ire->ire_src_addr; 19261 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19262 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19263 re->ipRouteInfo.re_flags = ire->ire_flags; 19264 19265 if (ire->ire_flags & RTF_DYNAMIC) { 19266 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19267 } else { 19268 re->ipRouteInfo.re_ire_type = ire->ire_type; 19269 } 19270 19271 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19272 (char *)re, (int)sizeof (*re))) { 19273 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19274 (uint_t)sizeof (*re))); 19275 } 19276 19277 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19278 iaeptr->iae_routeidx = ird->ird_idx; 19279 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19280 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19281 } 19282 19283 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19284 (char *)iae, sacnt * sizeof (*iae))) { 19285 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19286 (unsigned)(sacnt * sizeof (*iae)))); 19287 } 19288 19289 /* bump route index for next pass */ 19290 ird->ird_idx++; 19291 19292 kmem_free(re, sizeof (*re)); 19293 if (sacnt != 0) 19294 kmem_free(iae, sacnt * sizeof (*iae)); 19295 19296 if (gcgrp != NULL) 19297 rw_exit(&gcgrp->gcgrp_rwlock); 19298 } 19299 19300 /* 19301 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 19302 */ 19303 static void 19304 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 19305 { 19306 ill_t *ill; 19307 ipif_t *ipif; 19308 mib2_ipv6RouteEntry_t *re; 19309 mib2_ipAttributeEntry_t *iae, *iaeptr; 19310 in6_addr_t gw_addr_v6; 19311 tsol_ire_gw_secattr_t *attrp; 19312 tsol_gc_t *gc = NULL; 19313 tsol_gcgrp_t *gcgrp = NULL; 19314 uint_t sacnt = 0; 19315 int i; 19316 19317 ASSERT(ire->ire_ipversion == IPV6_VERSION); 19318 19319 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19320 return; 19321 19322 if ((attrp = ire->ire_gw_secattr) != NULL) { 19323 mutex_enter(&attrp->igsa_lock); 19324 if ((gc = attrp->igsa_gc) != NULL) { 19325 gcgrp = gc->gc_grp; 19326 ASSERT(gcgrp != NULL); 19327 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19328 sacnt = 1; 19329 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19330 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19331 gc = gcgrp->gcgrp_head; 19332 sacnt = gcgrp->gcgrp_count; 19333 } 19334 mutex_exit(&attrp->igsa_lock); 19335 19336 /* do nothing if there's no gc to report */ 19337 if (gc == NULL) { 19338 ASSERT(sacnt == 0); 19339 if (gcgrp != NULL) { 19340 /* we might as well drop the lock now */ 19341 rw_exit(&gcgrp->gcgrp_rwlock); 19342 gcgrp = NULL; 19343 } 19344 attrp = NULL; 19345 } 19346 19347 ASSERT(gc == NULL || (gcgrp != NULL && 19348 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19349 } 19350 ASSERT(sacnt == 0 || gc != NULL); 19351 19352 if (sacnt != 0 && 19353 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19354 kmem_free(re, sizeof (*re)); 19355 rw_exit(&gcgrp->gcgrp_rwlock); 19356 return; 19357 } 19358 19359 /* 19360 * Return all IRE types for route table... let caller pick and choose 19361 */ 19362 re->ipv6RouteDest = ire->ire_addr_v6; 19363 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 19364 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 19365 re->ipv6RouteIfIndex.o_length = 0; 19366 ipif = ire->ire_ipif; 19367 if (ire->ire_type == IRE_CACHE) { 19368 ill = (ill_t *)ire->ire_stq->q_ptr; 19369 re->ipv6RouteIfIndex.o_length = 19370 ill->ill_name_length == 0 ? 0 : 19371 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19372 bcopy(ill->ill_name, re->ipv6RouteIfIndex.o_bytes, 19373 re->ipv6RouteIfIndex.o_length); 19374 } else if (ipif != NULL) { 19375 ipif_get_name(ipif, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH); 19376 re->ipv6RouteIfIndex.o_length = 19377 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 19378 } 19379 19380 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19381 19382 mutex_enter(&ire->ire_lock); 19383 gw_addr_v6 = ire->ire_gateway_addr_v6; 19384 mutex_exit(&ire->ire_lock); 19385 19386 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 19387 re->ipv6RouteNextHop = ire->ire_src_addr_v6; 19388 else 19389 re->ipv6RouteNextHop = gw_addr_v6; 19390 19391 /* remote(4), local(3), or discard(2) */ 19392 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19393 re->ipv6RouteType = 2; 19394 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 19395 re->ipv6RouteType = 3; 19396 else 19397 re->ipv6RouteType = 4; 19398 19399 re->ipv6RouteProtocol = -1; 19400 re->ipv6RoutePolicy = 0; 19401 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 19402 re->ipv6RouteNextHopRDI = 0; 19403 re->ipv6RouteWeight = 0; 19404 re->ipv6RouteMetric = 0; 19405 re->ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 19406 re->ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 19407 re->ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19408 re->ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 19409 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19410 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19411 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 19412 re->ipv6RouteInfo.re_flags = ire->ire_flags; 19413 19414 if (ire->ire_flags & RTF_DYNAMIC) { 19415 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19416 } else { 19417 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 19418 } 19419 19420 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19421 (char *)re, (int)sizeof (*re))) { 19422 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19423 (uint_t)sizeof (*re))); 19424 } 19425 19426 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19427 iaeptr->iae_routeidx = ird->ird_idx; 19428 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19429 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19430 } 19431 19432 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19433 (char *)iae, sacnt * sizeof (*iae))) { 19434 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19435 (unsigned)(sacnt * sizeof (*iae)))); 19436 } 19437 19438 /* bump route index for next pass */ 19439 ird->ird_idx++; 19440 19441 kmem_free(re, sizeof (*re)); 19442 if (sacnt != 0) 19443 kmem_free(iae, sacnt * sizeof (*iae)); 19444 19445 if (gcgrp != NULL) 19446 rw_exit(&gcgrp->gcgrp_rwlock); 19447 } 19448 19449 /* 19450 * ndp_walk routine to create ipv6NetToMediaEntryTable 19451 */ 19452 static int 19453 ip_snmp_get2_v6_media(nce_t *nce, iproutedata_t *ird) 19454 { 19455 ill_t *ill; 19456 mib2_ipv6NetToMediaEntry_t ntme; 19457 dl_unitdata_req_t *dl; 19458 19459 ill = nce->nce_ill; 19460 if (ill->ill_isv6 == B_FALSE) /* skip arpce entry */ 19461 return (0); 19462 19463 /* 19464 * Neighbor cache entry attached to IRE with on-link 19465 * destination. 19466 */ 19467 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 19468 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 19469 if ((ill->ill_flags & ILLF_XRESOLV) && 19470 (nce->nce_res_mp != NULL)) { 19471 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 19472 ntme.ipv6NetToMediaPhysAddress.o_length = 19473 dl->dl_dest_addr_length; 19474 } else { 19475 ntme.ipv6NetToMediaPhysAddress.o_length = 19476 ill->ill_phys_addr_length; 19477 } 19478 if (nce->nce_res_mp != NULL) { 19479 bcopy((char *)nce->nce_res_mp->b_rptr + 19480 NCE_LL_ADDR_OFFSET(ill), 19481 ntme.ipv6NetToMediaPhysAddress.o_bytes, 19482 ntme.ipv6NetToMediaPhysAddress.o_length); 19483 } else { 19484 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 19485 ill->ill_phys_addr_length); 19486 } 19487 /* 19488 * Note: Returns ND_* states. Should be: 19489 * reachable(1), stale(2), delay(3), probe(4), 19490 * invalid(5), unknown(6) 19491 */ 19492 ntme.ipv6NetToMediaState = nce->nce_state; 19493 ntme.ipv6NetToMediaLastUpdated = 0; 19494 19495 /* other(1), dynamic(2), static(3), local(4) */ 19496 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 19497 ntme.ipv6NetToMediaType = 4; 19498 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 19499 ntme.ipv6NetToMediaType = 1; 19500 } else { 19501 ntme.ipv6NetToMediaType = 2; 19502 } 19503 19504 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 19505 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 19506 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 19507 (uint_t)sizeof (ntme))); 19508 } 19509 return (0); 19510 } 19511 19512 /* 19513 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 19514 */ 19515 /* ARGSUSED */ 19516 int 19517 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 19518 { 19519 switch (level) { 19520 case MIB2_IP: 19521 case MIB2_ICMP: 19522 switch (name) { 19523 default: 19524 break; 19525 } 19526 return (1); 19527 default: 19528 return (1); 19529 } 19530 } 19531 19532 /* 19533 * When there exists both a 64- and 32-bit counter of a particular type 19534 * (i.e., InReceives), only the 64-bit counters are added. 19535 */ 19536 void 19537 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2) 19538 { 19539 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors); 19540 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors); 19541 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes); 19542 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors); 19543 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos); 19544 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts); 19545 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards); 19546 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards); 19547 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs); 19548 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails); 19549 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates); 19550 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds); 19551 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs); 19552 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails); 19553 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes); 19554 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates); 19555 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups); 19556 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits); 19557 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs); 19558 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows); 19559 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows); 19560 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion, 19561 o2->ipIfStatsInWrongIPVersion); 19562 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion, 19563 o2->ipIfStatsInWrongIPVersion); 19564 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion, 19565 o2->ipIfStatsOutSwitchIPVersion); 19566 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives); 19567 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets); 19568 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams, 19569 o2->ipIfStatsHCInForwDatagrams); 19570 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers); 19571 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests); 19572 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams, 19573 o2->ipIfStatsHCOutForwDatagrams); 19574 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds); 19575 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits); 19576 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets); 19577 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts); 19578 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets); 19579 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts); 19580 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets, 19581 o2->ipIfStatsHCOutMcastOctets); 19582 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts); 19583 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts); 19584 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded); 19585 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed); 19586 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs); 19587 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs); 19588 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts); 19589 } 19590 19591 void 19592 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2) 19593 { 19594 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs); 19595 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors); 19596 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs); 19597 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs); 19598 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds); 19599 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems); 19600 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs); 19601 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos); 19602 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies); 19603 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits, 19604 o2->ipv6IfIcmpInRouterSolicits); 19605 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements, 19606 o2->ipv6IfIcmpInRouterAdvertisements); 19607 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits, 19608 o2->ipv6IfIcmpInNeighborSolicits); 19609 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements, 19610 o2->ipv6IfIcmpInNeighborAdvertisements); 19611 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects); 19612 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries, 19613 o2->ipv6IfIcmpInGroupMembQueries); 19614 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses, 19615 o2->ipv6IfIcmpInGroupMembResponses); 19616 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions, 19617 o2->ipv6IfIcmpInGroupMembReductions); 19618 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs); 19619 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors); 19620 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs, 19621 o2->ipv6IfIcmpOutDestUnreachs); 19622 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs, 19623 o2->ipv6IfIcmpOutAdminProhibs); 19624 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds); 19625 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems, 19626 o2->ipv6IfIcmpOutParmProblems); 19627 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs); 19628 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos); 19629 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies); 19630 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits, 19631 o2->ipv6IfIcmpOutRouterSolicits); 19632 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements, 19633 o2->ipv6IfIcmpOutRouterAdvertisements); 19634 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits, 19635 o2->ipv6IfIcmpOutNeighborSolicits); 19636 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements, 19637 o2->ipv6IfIcmpOutNeighborAdvertisements); 19638 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects); 19639 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries, 19640 o2->ipv6IfIcmpOutGroupMembQueries); 19641 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses, 19642 o2->ipv6IfIcmpOutGroupMembResponses); 19643 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions, 19644 o2->ipv6IfIcmpOutGroupMembReductions); 19645 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows); 19646 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit); 19647 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements, 19648 o2->ipv6IfIcmpInBadNeighborAdvertisements); 19649 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations, 19650 o2->ipv6IfIcmpInBadNeighborSolicitations); 19651 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects); 19652 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal, 19653 o2->ipv6IfIcmpInGroupMembTotal); 19654 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries, 19655 o2->ipv6IfIcmpInGroupMembBadQueries); 19656 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports, 19657 o2->ipv6IfIcmpInGroupMembBadReports); 19658 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports, 19659 o2->ipv6IfIcmpInGroupMembOurReports); 19660 } 19661 19662 /* 19663 * Called before the options are updated to check if this packet will 19664 * be source routed from here. 19665 * This routine assumes that the options are well formed i.e. that they 19666 * have already been checked. 19667 */ 19668 static boolean_t 19669 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst) 19670 { 19671 ipoptp_t opts; 19672 uchar_t *opt; 19673 uint8_t optval; 19674 uint8_t optlen; 19675 ipaddr_t dst; 19676 ire_t *ire; 19677 19678 if (IS_SIMPLE_IPH(ipha)) { 19679 ip2dbg(("not source routed\n")); 19680 return (B_FALSE); 19681 } 19682 dst = ipha->ipha_dst; 19683 for (optval = ipoptp_first(&opts, ipha); 19684 optval != IPOPT_EOL; 19685 optval = ipoptp_next(&opts)) { 19686 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 19687 opt = opts.ipoptp_cur; 19688 optlen = opts.ipoptp_len; 19689 ip2dbg(("ip_source_routed: opt %d, len %d\n", 19690 optval, optlen)); 19691 switch (optval) { 19692 uint32_t off; 19693 case IPOPT_SSRR: 19694 case IPOPT_LSRR: 19695 /* 19696 * If dst is one of our addresses and there are some 19697 * entries left in the source route return (true). 19698 */ 19699 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 19700 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 19701 if (ire == NULL) { 19702 ip2dbg(("ip_source_routed: not next" 19703 " source route 0x%x\n", 19704 ntohl(dst))); 19705 return (B_FALSE); 19706 } 19707 ire_refrele(ire); 19708 off = opt[IPOPT_OFFSET]; 19709 off--; 19710 if (optlen < IP_ADDR_LEN || 19711 off > optlen - IP_ADDR_LEN) { 19712 /* End of source route */ 19713 ip1dbg(("ip_source_routed: end of SR\n")); 19714 return (B_FALSE); 19715 } 19716 return (B_TRUE); 19717 } 19718 } 19719 ip2dbg(("not source routed\n")); 19720 return (B_FALSE); 19721 } 19722 19723 /* 19724 * Check if the packet contains any source route. 19725 */ 19726 static boolean_t 19727 ip_source_route_included(ipha_t *ipha) 19728 { 19729 ipoptp_t opts; 19730 uint8_t optval; 19731 19732 if (IS_SIMPLE_IPH(ipha)) 19733 return (B_FALSE); 19734 for (optval = ipoptp_first(&opts, ipha); 19735 optval != IPOPT_EOL; 19736 optval = ipoptp_next(&opts)) { 19737 switch (optval) { 19738 case IPOPT_SSRR: 19739 case IPOPT_LSRR: 19740 return (B_TRUE); 19741 } 19742 } 19743 return (B_FALSE); 19744 } 19745 19746 /* 19747 * Called when the IRE expiration timer fires. 19748 */ 19749 void 19750 ip_trash_timer_expire(void *args) 19751 { 19752 int flush_flag = 0; 19753 ire_expire_arg_t iea; 19754 ip_stack_t *ipst = (ip_stack_t *)args; 19755 19756 iea.iea_ipst = ipst; /* No netstack_hold */ 19757 19758 /* 19759 * ip_ire_expire_id is protected by ip_trash_timer_lock. 19760 * This lock makes sure that a new invocation of this function 19761 * that occurs due to an almost immediate timer firing will not 19762 * progress beyond this point until the current invocation is done 19763 */ 19764 mutex_enter(&ipst->ips_ip_trash_timer_lock); 19765 ipst->ips_ip_ire_expire_id = 0; 19766 mutex_exit(&ipst->ips_ip_trash_timer_lock); 19767 19768 /* Periodic timer */ 19769 if (ipst->ips_ip_ire_arp_time_elapsed >= 19770 ipst->ips_ip_ire_arp_interval) { 19771 /* 19772 * Remove all IRE_CACHE entries since they might 19773 * contain arp information. 19774 */ 19775 flush_flag |= FLUSH_ARP_TIME; 19776 ipst->ips_ip_ire_arp_time_elapsed = 0; 19777 IP_STAT(ipst, ip_ire_arp_timer_expired); 19778 } 19779 if (ipst->ips_ip_ire_rd_time_elapsed >= 19780 ipst->ips_ip_ire_redir_interval) { 19781 /* Remove all redirects */ 19782 flush_flag |= FLUSH_REDIRECT_TIME; 19783 ipst->ips_ip_ire_rd_time_elapsed = 0; 19784 IP_STAT(ipst, ip_ire_redirect_timer_expired); 19785 } 19786 if (ipst->ips_ip_ire_pmtu_time_elapsed >= 19787 ipst->ips_ip_ire_pathmtu_interval) { 19788 /* Increase path mtu */ 19789 flush_flag |= FLUSH_MTU_TIME; 19790 ipst->ips_ip_ire_pmtu_time_elapsed = 0; 19791 IP_STAT(ipst, ip_ire_pmtu_timer_expired); 19792 } 19793 19794 /* 19795 * Optimize for the case when there are no redirects in the 19796 * ftable, that is, no need to walk the ftable in that case. 19797 */ 19798 if (flush_flag & (FLUSH_MTU_TIME|FLUSH_ARP_TIME)) { 19799 iea.iea_flush_flag = flush_flag; 19800 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_CACHETABLE, ire_expire, 19801 (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 0, NULL, 19802 ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table, 19803 NULL, ALL_ZONES, ipst); 19804 } 19805 if ((flush_flag & FLUSH_REDIRECT_TIME) && 19806 ipst->ips_ip_redirect_cnt > 0) { 19807 iea.iea_flush_flag = flush_flag; 19808 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_FORWARDTABLE, 19809 ire_expire, (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 19810 0, NULL, 0, NULL, NULL, ALL_ZONES, ipst); 19811 } 19812 if (flush_flag & FLUSH_MTU_TIME) { 19813 /* 19814 * Walk all IPv6 IRE's and update them 19815 * Note that ARP and redirect timers are not 19816 * needed since NUD handles stale entries. 19817 */ 19818 flush_flag = FLUSH_MTU_TIME; 19819 iea.iea_flush_flag = flush_flag; 19820 ire_walk_v6(ire_expire, (char *)(uintptr_t)&iea, 19821 ALL_ZONES, ipst); 19822 } 19823 19824 ipst->ips_ip_ire_arp_time_elapsed += ipst->ips_ip_timer_interval; 19825 ipst->ips_ip_ire_rd_time_elapsed += ipst->ips_ip_timer_interval; 19826 ipst->ips_ip_ire_pmtu_time_elapsed += ipst->ips_ip_timer_interval; 19827 19828 /* 19829 * Hold the lock to serialize timeout calls and prevent 19830 * stale values in ip_ire_expire_id. Otherwise it is possible 19831 * for the timer to fire and a new invocation of this function 19832 * to start before the return value of timeout has been stored 19833 * in ip_ire_expire_id by the current invocation. 19834 */ 19835 mutex_enter(&ipst->ips_ip_trash_timer_lock); 19836 ipst->ips_ip_ire_expire_id = timeout(ip_trash_timer_expire, 19837 (void *)ipst, MSEC_TO_TICK(ipst->ips_ip_timer_interval)); 19838 mutex_exit(&ipst->ips_ip_trash_timer_lock); 19839 } 19840 19841 /* 19842 * Called by the memory allocator subsystem directly, when the system 19843 * is running low on memory. 19844 */ 19845 /* ARGSUSED */ 19846 void 19847 ip_trash_ire_reclaim(void *args) 19848 { 19849 netstack_handle_t nh; 19850 netstack_t *ns; 19851 19852 netstack_next_init(&nh); 19853 while ((ns = netstack_next(&nh)) != NULL) { 19854 ip_trash_ire_reclaim_stack(ns->netstack_ip); 19855 netstack_rele(ns); 19856 } 19857 netstack_next_fini(&nh); 19858 } 19859 19860 static void 19861 ip_trash_ire_reclaim_stack(ip_stack_t *ipst) 19862 { 19863 ire_cache_count_t icc; 19864 ire_cache_reclaim_t icr; 19865 ncc_cache_count_t ncc; 19866 nce_cache_reclaim_t ncr; 19867 uint_t delete_cnt; 19868 /* 19869 * Memory reclaim call back. 19870 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 19871 * Then, with a target of freeing 1/Nth of IRE_CACHE 19872 * entries, determine what fraction to free for 19873 * each category of IRE_CACHE entries giving absolute priority 19874 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 19875 * entry will be freed unless all offlink entries are freed). 19876 */ 19877 icc.icc_total = 0; 19878 icc.icc_unused = 0; 19879 icc.icc_offlink = 0; 19880 icc.icc_pmtu = 0; 19881 icc.icc_onlink = 0; 19882 ire_walk(ire_cache_count, (char *)&icc, ipst); 19883 19884 /* 19885 * Free NCEs for IPv6 like the onlink ires. 19886 */ 19887 ncc.ncc_total = 0; 19888 ncc.ncc_host = 0; 19889 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc, ipst); 19890 19891 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 19892 icc.icc_pmtu + icc.icc_onlink); 19893 delete_cnt = icc.icc_total/ipst->ips_ip_ire_reclaim_fraction; 19894 IP_STAT(ipst, ip_trash_ire_reclaim_calls); 19895 if (delete_cnt == 0) 19896 return; 19897 IP_STAT(ipst, ip_trash_ire_reclaim_success); 19898 /* Always delete all unused offlink entries */ 19899 icr.icr_ipst = ipst; 19900 icr.icr_unused = 1; 19901 if (delete_cnt <= icc.icc_unused) { 19902 /* 19903 * Only need to free unused entries. In other words, 19904 * there are enough unused entries to free to meet our 19905 * target number of freed ire cache entries. 19906 */ 19907 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 19908 ncr.ncr_host = 0; 19909 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 19910 /* 19911 * Only need to free unused entries, plus a fraction of offlink 19912 * entries. It follows from the first if statement that 19913 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 19914 */ 19915 delete_cnt -= icc.icc_unused; 19916 /* Round up # deleted by truncating fraction */ 19917 icr.icr_offlink = icc.icc_offlink / delete_cnt; 19918 icr.icr_pmtu = icr.icr_onlink = 0; 19919 ncr.ncr_host = 0; 19920 } else if (delete_cnt <= 19921 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 19922 /* 19923 * Free all unused and offlink entries, plus a fraction of 19924 * pmtu entries. It follows from the previous if statement 19925 * that icc_pmtu is non-zero, and that 19926 * delete_cnt != icc_unused + icc_offlink. 19927 */ 19928 icr.icr_offlink = 1; 19929 delete_cnt -= icc.icc_unused + icc.icc_offlink; 19930 /* Round up # deleted by truncating fraction */ 19931 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 19932 icr.icr_onlink = 0; 19933 ncr.ncr_host = 0; 19934 } else { 19935 /* 19936 * Free all unused, offlink, and pmtu entries, plus a fraction 19937 * of onlink entries. If we're here, then we know that 19938 * icc_onlink is non-zero, and that 19939 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 19940 */ 19941 icr.icr_offlink = icr.icr_pmtu = 1; 19942 delete_cnt -= icc.icc_unused + icc.icc_offlink + 19943 icc.icc_pmtu; 19944 /* Round up # deleted by truncating fraction */ 19945 icr.icr_onlink = icc.icc_onlink / delete_cnt; 19946 /* Using the same delete fraction as for onlink IREs */ 19947 ncr.ncr_host = ncc.ncc_host / delete_cnt; 19948 } 19949 #ifdef DEBUG 19950 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 19951 "fractions %d/%d/%d/%d\n", 19952 icc.icc_total/ipst->ips_ip_ire_reclaim_fraction, icc.icc_total, 19953 icc.icc_unused, icc.icc_offlink, 19954 icc.icc_pmtu, icc.icc_onlink, 19955 icr.icr_unused, icr.icr_offlink, 19956 icr.icr_pmtu, icr.icr_onlink)); 19957 #endif 19958 ire_walk(ire_cache_reclaim, (char *)&icr, ipst); 19959 if (ncr.ncr_host != 0) 19960 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 19961 (uchar_t *)&ncr, ipst); 19962 #ifdef DEBUG 19963 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 19964 icc.icc_pmtu = 0; icc.icc_onlink = 0; 19965 ire_walk(ire_cache_count, (char *)&icc, ipst); 19966 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 19967 icc.icc_total, icc.icc_unused, icc.icc_offlink, 19968 icc.icc_pmtu, icc.icc_onlink)); 19969 #endif 19970 } 19971 19972 /* 19973 * ip_unbind is called when a copy of an unbind request is received from the 19974 * upper level protocol. We remove this conn from any fanout hash list it is 19975 * on, and zero out the bind information. No reply is expected up above. 19976 */ 19977 mblk_t * 19978 ip_unbind(queue_t *q, mblk_t *mp) 19979 { 19980 conn_t *connp = Q_TO_CONN(q); 19981 19982 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 19983 19984 if (is_system_labeled() && connp->conn_anon_port) { 19985 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 19986 connp->conn_mlp_type, connp->conn_ulp, 19987 ntohs(connp->conn_lport), B_FALSE); 19988 connp->conn_anon_port = 0; 19989 } 19990 connp->conn_mlp_type = mlptSingle; 19991 19992 ipcl_hash_remove(connp); 19993 19994 ASSERT(mp->b_cont == NULL); 19995 /* 19996 * Convert mp into a T_OK_ACK 19997 */ 19998 mp = mi_tpi_ok_ack_alloc(mp); 19999 20000 /* 20001 * should not happen in practice... T_OK_ACK is smaller than the 20002 * original message. 20003 */ 20004 if (mp == NULL) 20005 return (NULL); 20006 20007 /* 20008 * Don't bzero the ports if its TCP since TCP still needs the 20009 * lport to remove it from its own bind hash. TCP will do the 20010 * cleanup. 20011 */ 20012 if (!IPCL_IS_TCP(connp)) 20013 bzero(&connp->u_port, sizeof (connp->u_port)); 20014 20015 return (mp); 20016 } 20017 20018 /* 20019 * Write side put procedure. Outbound data, IOCTLs, responses from 20020 * resolvers, etc, come down through here. 20021 * 20022 * arg2 is always a queue_t *. 20023 * When that queue is an ill_t (i.e. q_next != NULL), then arg must be 20024 * the zoneid. 20025 * When that queue is not an ill_t, then arg must be a conn_t pointer. 20026 */ 20027 void 20028 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 20029 { 20030 ip_output_options(arg, mp, arg2, caller, &zero_info); 20031 } 20032 20033 void 20034 ip_output_options(void *arg, mblk_t *mp, void *arg2, int caller, 20035 ip_opt_info_t *infop) 20036 { 20037 conn_t *connp = NULL; 20038 queue_t *q = (queue_t *)arg2; 20039 ipha_t *ipha; 20040 #define rptr ((uchar_t *)ipha) 20041 ire_t *ire = NULL; 20042 ire_t *sctp_ire = NULL; 20043 uint32_t v_hlen_tos_len; 20044 ipaddr_t dst; 20045 mblk_t *first_mp = NULL; 20046 boolean_t mctl_present; 20047 ipsec_out_t *io; 20048 int match_flags; 20049 ill_t *attach_ill = NULL; 20050 /* Bind to IPIF_NOFAILOVER ill etc. */ 20051 ill_t *xmit_ill = NULL; /* IP_XMIT_IF etc. */ 20052 ipif_t *dst_ipif; 20053 boolean_t multirt_need_resolve = B_FALSE; 20054 mblk_t *copy_mp = NULL; 20055 int err; 20056 zoneid_t zoneid; 20057 int adjust; 20058 uint16_t iplen; 20059 boolean_t need_decref = B_FALSE; 20060 boolean_t ignore_dontroute = B_FALSE; 20061 boolean_t ignore_nexthop = B_FALSE; 20062 boolean_t ip_nexthop = B_FALSE; 20063 ipaddr_t nexthop_addr; 20064 ip_stack_t *ipst; 20065 20066 #ifdef _BIG_ENDIAN 20067 #define V_HLEN (v_hlen_tos_len >> 24) 20068 #else 20069 #define V_HLEN (v_hlen_tos_len & 0xFF) 20070 #endif 20071 20072 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 20073 "ip_wput_start: q %p", q); 20074 20075 /* 20076 * ip_wput fast path 20077 */ 20078 20079 /* is packet from ARP ? */ 20080 if (q->q_next != NULL) { 20081 zoneid = (zoneid_t)(uintptr_t)arg; 20082 goto qnext; 20083 } 20084 20085 connp = (conn_t *)arg; 20086 ASSERT(connp != NULL); 20087 zoneid = connp->conn_zoneid; 20088 ipst = connp->conn_netstack->netstack_ip; 20089 20090 /* is queue flow controlled? */ 20091 if ((q->q_first != NULL || connp->conn_draining) && 20092 (caller == IP_WPUT)) { 20093 ASSERT(!need_decref); 20094 (void) putq(q, mp); 20095 return; 20096 } 20097 20098 /* Multidata transmit? */ 20099 if (DB_TYPE(mp) == M_MULTIDATA) { 20100 /* 20101 * We should never get here, since all Multidata messages 20102 * originating from tcp should have been directed over to 20103 * tcp_multisend() in the first place. 20104 */ 20105 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20106 freemsg(mp); 20107 return; 20108 } else if (DB_TYPE(mp) != M_DATA) 20109 goto notdata; 20110 20111 if (mp->b_flag & MSGHASREF) { 20112 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20113 mp->b_flag &= ~MSGHASREF; 20114 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 20115 need_decref = B_TRUE; 20116 } 20117 ipha = (ipha_t *)mp->b_rptr; 20118 20119 /* is IP header non-aligned or mblk smaller than basic IP header */ 20120 #ifndef SAFETY_BEFORE_SPEED 20121 if (!OK_32PTR(rptr) || 20122 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 20123 goto hdrtoosmall; 20124 #endif 20125 20126 ASSERT(OK_32PTR(ipha)); 20127 20128 /* 20129 * This function assumes that mp points to an IPv4 packet. If it's the 20130 * wrong version, we'll catch it again in ip_output_v6. 20131 * 20132 * Note that this is *only* locally-generated output here, and never 20133 * forwarded data, and that we need to deal only with transports that 20134 * don't know how to label. (TCP, UDP, and ICMP/raw-IP all know how to 20135 * label.) 20136 */ 20137 if (is_system_labeled() && 20138 (ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4) && 20139 !connp->conn_ulp_labeled) { 20140 err = tsol_check_label(BEST_CRED(mp, connp), &mp, &adjust, 20141 connp->conn_mac_exempt, ipst); 20142 ipha = (ipha_t *)mp->b_rptr; 20143 if (err != 0) { 20144 first_mp = mp; 20145 if (err == EINVAL) 20146 goto icmp_parameter_problem; 20147 ip2dbg(("ip_wput: label check failed (%d)\n", err)); 20148 goto discard_pkt; 20149 } 20150 iplen = ntohs(ipha->ipha_length) + adjust; 20151 ipha->ipha_length = htons(iplen); 20152 } 20153 20154 ASSERT(infop != NULL); 20155 20156 if (infop->ip_opt_flags & IP_VERIFY_SRC) { 20157 /* 20158 * IP_PKTINFO ancillary option is present. 20159 * IPCL_ZONEID is used to honor IP_ALLZONES option which 20160 * allows using address of any zone as the source address. 20161 */ 20162 ire = ire_ctable_lookup(ipha->ipha_src, 0, 20163 (IRE_LOCAL|IRE_LOOPBACK), NULL, IPCL_ZONEID(connp), 20164 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 20165 if (ire == NULL) 20166 goto drop_pkt; 20167 ire_refrele(ire); 20168 ire = NULL; 20169 } 20170 20171 /* 20172 * IP_DONTFAILOVER_IF and IP_XMIT_IF have precedence over 20173 * ill index passed in IP_PKTINFO. 20174 */ 20175 if (infop->ip_opt_ill_index != 0 && 20176 connp->conn_xmit_if_ill == NULL && 20177 connp->conn_nofailover_ill == NULL) { 20178 20179 xmit_ill = ill_lookup_on_ifindex( 20180 infop->ip_opt_ill_index, B_FALSE, NULL, NULL, NULL, NULL, 20181 ipst); 20182 20183 if (xmit_ill == NULL || IS_VNI(xmit_ill)) 20184 goto drop_pkt; 20185 /* 20186 * check that there is an ipif belonging 20187 * to our zone. IPCL_ZONEID is not used because 20188 * IP_ALLZONES option is valid only when the ill is 20189 * accessible from all zones i.e has a valid ipif in 20190 * all zones. 20191 */ 20192 if (!ipif_lookup_zoneid_group(xmit_ill, zoneid, 0, NULL)) { 20193 goto drop_pkt; 20194 } 20195 } 20196 20197 /* 20198 * If there is a policy, try to attach an ipsec_out in 20199 * the front. At the end, first_mp either points to a 20200 * M_DATA message or IPSEC_OUT message linked to a 20201 * M_DATA message. We have to do it now as we might 20202 * lose the "conn" if we go through ip_newroute. 20203 */ 20204 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 20205 if (((mp = ipsec_attach_ipsec_out(&mp, connp, NULL, 20206 ipha->ipha_protocol, ipst->ips_netstack)) == NULL)) { 20207 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20208 if (need_decref) 20209 CONN_DEC_REF(connp); 20210 return; 20211 } else { 20212 ASSERT(mp->b_datap->db_type == M_CTL); 20213 first_mp = mp; 20214 mp = mp->b_cont; 20215 mctl_present = B_TRUE; 20216 } 20217 } else { 20218 first_mp = mp; 20219 mctl_present = B_FALSE; 20220 } 20221 20222 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20223 20224 /* is wrong version or IP options present */ 20225 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 20226 goto version_hdrlen_check; 20227 dst = ipha->ipha_dst; 20228 20229 if (connp->conn_nofailover_ill != NULL) { 20230 attach_ill = conn_get_held_ill(connp, 20231 &connp->conn_nofailover_ill, &err); 20232 if (err == ILL_LOOKUP_FAILED) { 20233 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20234 if (need_decref) 20235 CONN_DEC_REF(connp); 20236 freemsg(first_mp); 20237 return; 20238 } 20239 } 20240 20241 20242 /* is packet multicast? */ 20243 if (CLASSD(dst)) 20244 goto multicast; 20245 20246 /* 20247 * If xmit_ill is set above due to index passed in ip_pkt_info. It 20248 * takes precedence over conn_dontroute and conn_nexthop_set 20249 */ 20250 if (xmit_ill != NULL) { 20251 goto send_from_ill; 20252 } 20253 20254 if ((connp->conn_dontroute) || (connp->conn_xmit_if_ill != NULL) || 20255 (connp->conn_nexthop_set)) { 20256 /* 20257 * If the destination is a broadcast or a loopback 20258 * address, SO_DONTROUTE, IP_XMIT_IF and IP_NEXTHOP go 20259 * through the standard path. But in the case of local 20260 * destination only SO_DONTROUTE and IP_NEXTHOP go through 20261 * the standard path not IP_XMIT_IF. 20262 */ 20263 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20264 if ((ire == NULL) || ((ire->ire_type != IRE_BROADCAST) && 20265 (ire->ire_type != IRE_LOOPBACK))) { 20266 if ((connp->conn_dontroute || 20267 connp->conn_nexthop_set) && (ire != NULL) && 20268 (ire->ire_type == IRE_LOCAL)) 20269 goto standard_path; 20270 20271 if (ire != NULL) { 20272 ire_refrele(ire); 20273 /* No more access to ire */ 20274 ire = NULL; 20275 } 20276 /* 20277 * bypass routing checks and go directly to 20278 * interface. 20279 */ 20280 if (connp->conn_dontroute) { 20281 goto dontroute; 20282 } else if (connp->conn_nexthop_set) { 20283 ip_nexthop = B_TRUE; 20284 nexthop_addr = connp->conn_nexthop_v4; 20285 goto send_from_ill; 20286 } 20287 20288 /* 20289 * If IP_XMIT_IF socket option is set, 20290 * then we allow unicast and multicast 20291 * packets to go through the ill. It is 20292 * quite possible that the destination 20293 * is not in the ire cache table and we 20294 * do not want to go to ip_newroute() 20295 * instead we call ip_newroute_ipif. 20296 */ 20297 xmit_ill = conn_get_held_ill(connp, 20298 &connp->conn_xmit_if_ill, &err); 20299 if (err == ILL_LOOKUP_FAILED) { 20300 BUMP_MIB(&ipst->ips_ip_mib, 20301 ipIfStatsOutDiscards); 20302 if (attach_ill != NULL) 20303 ill_refrele(attach_ill); 20304 if (need_decref) 20305 CONN_DEC_REF(connp); 20306 freemsg(first_mp); 20307 return; 20308 } 20309 goto send_from_ill; 20310 } 20311 standard_path: 20312 /* Must be a broadcast, a loopback or a local ire */ 20313 if (ire != NULL) { 20314 ire_refrele(ire); 20315 /* No more access to ire */ 20316 ire = NULL; 20317 } 20318 } 20319 20320 if (attach_ill != NULL) 20321 goto send_from_ill; 20322 20323 /* 20324 * We cache IRE_CACHEs to avoid lookups. We don't do 20325 * this for the tcp global queue and listen end point 20326 * as it does not really have a real destination to 20327 * talk to. This is also true for SCTP. 20328 */ 20329 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 20330 !connp->conn_fully_bound) { 20331 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20332 if (ire == NULL) 20333 goto noirefound; 20334 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20335 "ip_wput_end: q %p (%S)", q, "end"); 20336 20337 /* 20338 * Check if the ire has the RTF_MULTIRT flag, inherited 20339 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20340 */ 20341 if (ire->ire_flags & RTF_MULTIRT) { 20342 20343 /* 20344 * Force the TTL of multirouted packets if required. 20345 * The TTL of such packets is bounded by the 20346 * ip_multirt_ttl ndd variable. 20347 */ 20348 if ((ipst->ips_ip_multirt_ttl > 0) && 20349 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20350 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20351 "(was %d), dst 0x%08x\n", 20352 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20353 ntohl(ire->ire_addr))); 20354 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20355 } 20356 /* 20357 * We look at this point if there are pending 20358 * unresolved routes. ire_multirt_resolvable() 20359 * checks in O(n) that all IRE_OFFSUBNET ire 20360 * entries for the packet's destination and 20361 * flagged RTF_MULTIRT are currently resolved. 20362 * If some remain unresolved, we make a copy 20363 * of the current message. It will be used 20364 * to initiate additional route resolutions. 20365 */ 20366 multirt_need_resolve = 20367 ire_multirt_need_resolve(ire->ire_addr, 20368 MBLK_GETLABEL(first_mp), ipst); 20369 ip2dbg(("ip_wput[TCP]: ire %p, " 20370 "multirt_need_resolve %d, first_mp %p\n", 20371 (void *)ire, multirt_need_resolve, 20372 (void *)first_mp)); 20373 if (multirt_need_resolve) { 20374 copy_mp = copymsg(first_mp); 20375 if (copy_mp != NULL) { 20376 MULTIRT_DEBUG_TAG(copy_mp); 20377 } 20378 } 20379 } 20380 20381 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20382 20383 /* 20384 * Try to resolve another multiroute if 20385 * ire_multirt_need_resolve() deemed it necessary. 20386 */ 20387 if (copy_mp != NULL) 20388 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 20389 if (need_decref) 20390 CONN_DEC_REF(connp); 20391 return; 20392 } 20393 20394 /* 20395 * Access to conn_ire_cache. (protected by conn_lock) 20396 * 20397 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 20398 * the ire bucket lock here to check for CONDEMNED as it is okay to 20399 * send a packet or two with the IRE_CACHE that is going away. 20400 * Access to the ire requires an ire refhold on the ire prior to 20401 * its use since an interface unplumb thread may delete the cached 20402 * ire and release the refhold at any time. 20403 * 20404 * Caching an ire in the conn_ire_cache 20405 * 20406 * o Caching an ire pointer in the conn requires a strict check for 20407 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 20408 * ires before cleaning up the conns. So the caching of an ire pointer 20409 * in the conn is done after making sure under the bucket lock that the 20410 * ire has not yet been marked CONDEMNED. Otherwise we will end up 20411 * caching an ire after the unplumb thread has cleaned up the conn. 20412 * If the conn does not send a packet subsequently the unplumb thread 20413 * will be hanging waiting for the ire count to drop to zero. 20414 * 20415 * o We also need to atomically test for a null conn_ire_cache and 20416 * set the conn_ire_cache under the the protection of the conn_lock 20417 * to avoid races among concurrent threads trying to simultaneously 20418 * cache an ire in the conn_ire_cache. 20419 */ 20420 mutex_enter(&connp->conn_lock); 20421 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 20422 20423 if (ire != NULL && ire->ire_addr == dst && 20424 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20425 20426 IRE_REFHOLD(ire); 20427 mutex_exit(&connp->conn_lock); 20428 20429 } else { 20430 boolean_t cached = B_FALSE; 20431 connp->conn_ire_cache = NULL; 20432 mutex_exit(&connp->conn_lock); 20433 /* Release the old ire */ 20434 if (ire != NULL && sctp_ire == NULL) 20435 IRE_REFRELE_NOTR(ire); 20436 20437 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 20438 if (ire == NULL) 20439 goto noirefound; 20440 IRE_REFHOLD_NOTR(ire); 20441 20442 mutex_enter(&connp->conn_lock); 20443 if (CONN_CACHE_IRE(connp) && connp->conn_ire_cache == NULL) { 20444 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 20445 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20446 if (connp->conn_ulp == IPPROTO_TCP) 20447 TCP_CHECK_IREINFO(connp->conn_tcp, ire); 20448 connp->conn_ire_cache = ire; 20449 cached = B_TRUE; 20450 } 20451 rw_exit(&ire->ire_bucket->irb_lock); 20452 } 20453 mutex_exit(&connp->conn_lock); 20454 20455 /* 20456 * We can continue to use the ire but since it was 20457 * not cached, we should drop the extra reference. 20458 */ 20459 if (!cached) 20460 IRE_REFRELE_NOTR(ire); 20461 } 20462 20463 20464 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20465 "ip_wput_end: q %p (%S)", q, "end"); 20466 20467 /* 20468 * Check if the ire has the RTF_MULTIRT flag, inherited 20469 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20470 */ 20471 if (ire->ire_flags & RTF_MULTIRT) { 20472 20473 /* 20474 * Force the TTL of multirouted packets if required. 20475 * The TTL of such packets is bounded by the 20476 * ip_multirt_ttl ndd variable. 20477 */ 20478 if ((ipst->ips_ip_multirt_ttl > 0) && 20479 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20480 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20481 "(was %d), dst 0x%08x\n", 20482 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20483 ntohl(ire->ire_addr))); 20484 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20485 } 20486 20487 /* 20488 * At this point, we check to see if there are any pending 20489 * unresolved routes. ire_multirt_resolvable() 20490 * checks in O(n) that all IRE_OFFSUBNET ire 20491 * entries for the packet's destination and 20492 * flagged RTF_MULTIRT are currently resolved. 20493 * If some remain unresolved, we make a copy 20494 * of the current message. It will be used 20495 * to initiate additional route resolutions. 20496 */ 20497 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 20498 MBLK_GETLABEL(first_mp), ipst); 20499 ip2dbg(("ip_wput[not TCP]: ire %p, " 20500 "multirt_need_resolve %d, first_mp %p\n", 20501 (void *)ire, multirt_need_resolve, (void *)first_mp)); 20502 if (multirt_need_resolve) { 20503 copy_mp = copymsg(first_mp); 20504 if (copy_mp != NULL) { 20505 MULTIRT_DEBUG_TAG(copy_mp); 20506 } 20507 } 20508 } 20509 20510 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20511 20512 /* 20513 * Try to resolve another multiroute if 20514 * ire_multirt_resolvable() deemed it necessary 20515 */ 20516 if (copy_mp != NULL) 20517 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 20518 if (need_decref) 20519 CONN_DEC_REF(connp); 20520 return; 20521 20522 qnext: 20523 /* 20524 * Upper Level Protocols pass down complete IP datagrams 20525 * as M_DATA messages. Everything else is a sideshow. 20526 * 20527 * 1) We could be re-entering ip_wput because of ip_neworute 20528 * in which case we could have a IPSEC_OUT message. We 20529 * need to pass through ip_wput like other datagrams and 20530 * hence cannot branch to ip_wput_nondata. 20531 * 20532 * 2) ARP, AH, ESP, and other clients who are on the module 20533 * instance of IP stream, give us something to deal with. 20534 * We will handle AH and ESP here and rest in ip_wput_nondata. 20535 * 20536 * 3) ICMP replies also could come here. 20537 */ 20538 ipst = ILLQ_TO_IPST(q); 20539 20540 if (DB_TYPE(mp) != M_DATA) { 20541 notdata: 20542 if (DB_TYPE(mp) == M_CTL) { 20543 /* 20544 * M_CTL messages are used by ARP, AH and ESP to 20545 * communicate with IP. We deal with IPSEC_IN and 20546 * IPSEC_OUT here. ip_wput_nondata handles other 20547 * cases. 20548 */ 20549 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 20550 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 20551 first_mp = mp->b_cont; 20552 first_mp->b_flag &= ~MSGHASREF; 20553 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20554 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 20555 CONN_DEC_REF(connp); 20556 connp = NULL; 20557 } 20558 if (ii->ipsec_info_type == IPSEC_IN) { 20559 /* 20560 * Either this message goes back to 20561 * IPsec for further processing or to 20562 * ULP after policy checks. 20563 */ 20564 ip_fanout_proto_again(mp, NULL, NULL, NULL); 20565 return; 20566 } else if (ii->ipsec_info_type == IPSEC_OUT) { 20567 io = (ipsec_out_t *)ii; 20568 if (io->ipsec_out_proc_begin) { 20569 /* 20570 * IPsec processing has already started. 20571 * Complete it. 20572 * IPQoS notes: We don't care what is 20573 * in ipsec_out_ill_index since this 20574 * won't be processed for IPQoS policies 20575 * in ipsec_out_process. 20576 */ 20577 ipsec_out_process(q, mp, NULL, 20578 io->ipsec_out_ill_index); 20579 return; 20580 } else { 20581 connp = (q->q_next != NULL) ? 20582 NULL : Q_TO_CONN(q); 20583 first_mp = mp; 20584 mp = mp->b_cont; 20585 mctl_present = B_TRUE; 20586 } 20587 zoneid = io->ipsec_out_zoneid; 20588 ASSERT(zoneid != ALL_ZONES); 20589 } else if (ii->ipsec_info_type == IPSEC_CTL) { 20590 /* 20591 * It's an IPsec control message requesting 20592 * an SADB update to be sent to the IPsec 20593 * hardware acceleration capable ills. 20594 */ 20595 ipsec_ctl_t *ipsec_ctl = 20596 (ipsec_ctl_t *)mp->b_rptr; 20597 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 20598 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 20599 mblk_t *cmp = mp->b_cont; 20600 20601 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 20602 ASSERT(cmp != NULL); 20603 20604 freeb(mp); 20605 ill_ipsec_capab_send_all(satype, cmp, sa, 20606 ipst->ips_netstack); 20607 return; 20608 } else { 20609 /* 20610 * This must be ARP or special TSOL signaling. 20611 */ 20612 ip_wput_nondata(NULL, q, mp, NULL); 20613 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20614 "ip_wput_end: q %p (%S)", q, "nondata"); 20615 return; 20616 } 20617 } else { 20618 /* 20619 * This must be non-(ARP/AH/ESP) messages. 20620 */ 20621 ASSERT(!need_decref); 20622 ip_wput_nondata(NULL, q, mp, NULL); 20623 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20624 "ip_wput_end: q %p (%S)", q, "nondata"); 20625 return; 20626 } 20627 } else { 20628 first_mp = mp; 20629 mctl_present = B_FALSE; 20630 } 20631 20632 ASSERT(first_mp != NULL); 20633 /* 20634 * ICMP echo replies attach an ipsec_out and set ipsec_out_attach_if 20635 * to make sure that this packet goes out on the same interface it 20636 * came in. We handle that here. 20637 */ 20638 if (mctl_present) { 20639 uint_t ifindex; 20640 20641 io = (ipsec_out_t *)first_mp->b_rptr; 20642 if (io->ipsec_out_attach_if || io->ipsec_out_ip_nexthop) { 20643 /* 20644 * We may have lost the conn context if we are 20645 * coming here from ip_newroute(). Copy the 20646 * nexthop information. 20647 */ 20648 if (io->ipsec_out_ip_nexthop) { 20649 ip_nexthop = B_TRUE; 20650 nexthop_addr = io->ipsec_out_nexthop_addr; 20651 20652 ipha = (ipha_t *)mp->b_rptr; 20653 dst = ipha->ipha_dst; 20654 goto send_from_ill; 20655 } else { 20656 ASSERT(io->ipsec_out_ill_index != 0); 20657 ifindex = io->ipsec_out_ill_index; 20658 attach_ill = ill_lookup_on_ifindex(ifindex, 20659 B_FALSE, NULL, NULL, NULL, NULL, ipst); 20660 if (attach_ill == NULL) { 20661 ASSERT(xmit_ill == NULL); 20662 ip1dbg(("ip_output: bad ifindex for " 20663 "(BIND TO IPIF_NOFAILOVER) %d\n", 20664 ifindex)); 20665 freemsg(first_mp); 20666 BUMP_MIB(&ipst->ips_ip_mib, 20667 ipIfStatsOutDiscards); 20668 ASSERT(!need_decref); 20669 return; 20670 } 20671 } 20672 } 20673 } 20674 20675 ASSERT(xmit_ill == NULL); 20676 20677 /* We have a complete IP datagram heading outbound. */ 20678 ipha = (ipha_t *)mp->b_rptr; 20679 20680 #ifndef SPEED_BEFORE_SAFETY 20681 /* 20682 * Make sure we have a full-word aligned message and that at least 20683 * a simple IP header is accessible in the first message. If not, 20684 * try a pullup. 20685 */ 20686 if (!OK_32PTR(rptr) || 20687 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) { 20688 hdrtoosmall: 20689 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 20690 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20691 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 20692 if (first_mp == NULL) 20693 first_mp = mp; 20694 goto discard_pkt; 20695 } 20696 20697 /* This function assumes that mp points to an IPv4 packet. */ 20698 if (is_system_labeled() && q->q_next == NULL && 20699 (*mp->b_rptr & 0xf0) == (IPV4_VERSION << 4) && 20700 !connp->conn_ulp_labeled) { 20701 err = tsol_check_label(BEST_CRED(mp, connp), &mp, 20702 &adjust, connp->conn_mac_exempt, ipst); 20703 ipha = (ipha_t *)mp->b_rptr; 20704 if (first_mp != NULL) 20705 first_mp->b_cont = mp; 20706 if (err != 0) { 20707 if (first_mp == NULL) 20708 first_mp = mp; 20709 if (err == EINVAL) 20710 goto icmp_parameter_problem; 20711 ip2dbg(("ip_wput: label check failed (%d)\n", 20712 err)); 20713 goto discard_pkt; 20714 } 20715 iplen = ntohs(ipha->ipha_length) + adjust; 20716 ipha->ipha_length = htons(iplen); 20717 } 20718 20719 ipha = (ipha_t *)mp->b_rptr; 20720 if (first_mp == NULL) { 20721 ASSERT(attach_ill == NULL && xmit_ill == NULL); 20722 /* 20723 * If we got here because of "goto hdrtoosmall" 20724 * We need to attach a IPSEC_OUT. 20725 */ 20726 if (connp->conn_out_enforce_policy) { 20727 if (((mp = ipsec_attach_ipsec_out(&mp, connp, 20728 NULL, ipha->ipha_protocol, 20729 ipst->ips_netstack)) == NULL)) { 20730 BUMP_MIB(&ipst->ips_ip_mib, 20731 ipIfStatsOutDiscards); 20732 if (need_decref) 20733 CONN_DEC_REF(connp); 20734 return; 20735 } else { 20736 ASSERT(mp->b_datap->db_type == M_CTL); 20737 first_mp = mp; 20738 mp = mp->b_cont; 20739 mctl_present = B_TRUE; 20740 } 20741 } else { 20742 first_mp = mp; 20743 mctl_present = B_FALSE; 20744 } 20745 } 20746 } 20747 #endif 20748 20749 /* Most of the code below is written for speed, not readability */ 20750 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20751 20752 /* 20753 * If ip_newroute() fails, we're going to need a full 20754 * header for the icmp wraparound. 20755 */ 20756 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 20757 uint_t v_hlen; 20758 version_hdrlen_check: 20759 ASSERT(first_mp != NULL); 20760 v_hlen = V_HLEN; 20761 /* 20762 * siphon off IPv6 packets coming down from transport 20763 * layer modules here. 20764 * Note: high-order bit carries NUD reachability confirmation 20765 */ 20766 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 20767 /* 20768 * XXX implement a IPv4 and IPv6 packet counter per 20769 * conn and switch when ratio exceeds e.g. 10:1 20770 */ 20771 #ifdef notyet 20772 if (q->q_next == NULL) /* Avoid ill queue */ 20773 ip_setqinfo(RD(q), B_TRUE, B_TRUE, ipst); 20774 #endif 20775 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutWrongIPVersion); 20776 ASSERT(xmit_ill == NULL); 20777 if (attach_ill != NULL) 20778 ill_refrele(attach_ill); 20779 if (need_decref) 20780 mp->b_flag |= MSGHASREF; 20781 (void) ip_output_v6(arg, first_mp, arg2, caller); 20782 return; 20783 } 20784 20785 if ((v_hlen >> 4) != IP_VERSION) { 20786 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20787 "ip_wput_end: q %p (%S)", q, "badvers"); 20788 goto discard_pkt; 20789 } 20790 /* 20791 * Is the header length at least 20 bytes? 20792 * 20793 * Are there enough bytes accessible in the header? If 20794 * not, try a pullup. 20795 */ 20796 v_hlen &= 0xF; 20797 v_hlen <<= 2; 20798 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 20799 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20800 "ip_wput_end: q %p (%S)", q, "badlen"); 20801 goto discard_pkt; 20802 } 20803 if (v_hlen > (mp->b_wptr - rptr)) { 20804 if (!pullupmsg(mp, v_hlen)) { 20805 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20806 "ip_wput_end: q %p (%S)", q, "badpullup2"); 20807 goto discard_pkt; 20808 } 20809 ipha = (ipha_t *)mp->b_rptr; 20810 } 20811 /* 20812 * Move first entry from any source route into ipha_dst and 20813 * verify the options 20814 */ 20815 if (ip_wput_options(q, first_mp, ipha, mctl_present, 20816 zoneid, ipst)) { 20817 ASSERT(xmit_ill == NULL); 20818 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20819 if (attach_ill != NULL) 20820 ill_refrele(attach_ill); 20821 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20822 "ip_wput_end: q %p (%S)", q, "badopts"); 20823 if (need_decref) 20824 CONN_DEC_REF(connp); 20825 return; 20826 } 20827 } 20828 dst = ipha->ipha_dst; 20829 20830 /* 20831 * Try to get an IRE_CACHE for the destination address. If we can't, 20832 * we have to run the packet through ip_newroute which will take 20833 * the appropriate action to arrange for an IRE_CACHE, such as querying 20834 * a resolver, or assigning a default gateway, etc. 20835 */ 20836 if (CLASSD(dst)) { 20837 ipif_t *ipif; 20838 uint32_t setsrc = 0; 20839 20840 multicast: 20841 ASSERT(first_mp != NULL); 20842 ip2dbg(("ip_wput: CLASSD\n")); 20843 if (connp == NULL) { 20844 /* 20845 * Use the first good ipif on the ill. 20846 * XXX Should this ever happen? (Appears 20847 * to show up with just ppp and no ethernet due 20848 * to in.rdisc.) 20849 * However, ire_send should be able to 20850 * call ip_wput_ire directly. 20851 * 20852 * XXX Also, this can happen for ICMP and other packets 20853 * with multicast source addresses. Perhaps we should 20854 * fix things so that we drop the packet in question, 20855 * but for now, just run with it. 20856 */ 20857 ill_t *ill = (ill_t *)q->q_ptr; 20858 20859 /* 20860 * Don't honor attach_if for this case. If ill 20861 * is part of the group, ipif could belong to 20862 * any ill and we cannot maintain attach_ill 20863 * and ipif_ill same anymore and the assert 20864 * below would fail. 20865 */ 20866 if (mctl_present && io->ipsec_out_attach_if) { 20867 io->ipsec_out_ill_index = 0; 20868 io->ipsec_out_attach_if = B_FALSE; 20869 ASSERT(attach_ill != NULL); 20870 ill_refrele(attach_ill); 20871 attach_ill = NULL; 20872 } 20873 20874 ASSERT(attach_ill == NULL); 20875 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 20876 if (ipif == NULL) { 20877 if (need_decref) 20878 CONN_DEC_REF(connp); 20879 freemsg(first_mp); 20880 return; 20881 } 20882 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 20883 ntohl(dst), ill->ill_name)); 20884 } else { 20885 /* 20886 * The order of precedence is IP_XMIT_IF, IP_PKTINFO 20887 * and IP_MULTICAST_IF. 20888 * Block comment above this function explains the 20889 * locking mechanism used here 20890 */ 20891 if (xmit_ill == NULL) { 20892 xmit_ill = conn_get_held_ill(connp, 20893 &connp->conn_xmit_if_ill, &err); 20894 if (err == ILL_LOOKUP_FAILED) { 20895 ip1dbg(("ip_wput: No ill for " 20896 "IP_XMIT_IF\n")); 20897 BUMP_MIB(&ipst->ips_ip_mib, 20898 ipIfStatsOutNoRoutes); 20899 goto drop_pkt; 20900 } 20901 } 20902 20903 if (xmit_ill == NULL) { 20904 ipif = conn_get_held_ipif(connp, 20905 &connp->conn_multicast_ipif, &err); 20906 if (err == IPIF_LOOKUP_FAILED) { 20907 ip1dbg(("ip_wput: No ipif for " 20908 "multicast\n")); 20909 BUMP_MIB(&ipst->ips_ip_mib, 20910 ipIfStatsOutNoRoutes); 20911 goto drop_pkt; 20912 } 20913 } 20914 if (xmit_ill != NULL) { 20915 ipif = ipif_get_next_ipif(NULL, xmit_ill); 20916 if (ipif == NULL) { 20917 ip1dbg(("ip_wput: No ipif for " 20918 "IP_XMIT_IF\n")); 20919 BUMP_MIB(&ipst->ips_ip_mib, 20920 ipIfStatsOutNoRoutes); 20921 goto drop_pkt; 20922 } 20923 } else if (ipif == NULL || ipif->ipif_isv6) { 20924 /* 20925 * We must do this ipif determination here 20926 * else we could pass through ip_newroute 20927 * and come back here without the conn context. 20928 * 20929 * Note: we do late binding i.e. we bind to 20930 * the interface when the first packet is sent. 20931 * For performance reasons we do not rebind on 20932 * each packet but keep the binding until the 20933 * next IP_MULTICAST_IF option. 20934 * 20935 * conn_multicast_{ipif,ill} are shared between 20936 * IPv4 and IPv6 and AF_INET6 sockets can 20937 * send both IPv4 and IPv6 packets. Hence 20938 * we have to check that "isv6" matches above. 20939 */ 20940 if (ipif != NULL) 20941 ipif_refrele(ipif); 20942 ipif = ipif_lookup_group(dst, zoneid, ipst); 20943 if (ipif == NULL) { 20944 ip1dbg(("ip_wput: No ipif for " 20945 "multicast\n")); 20946 BUMP_MIB(&ipst->ips_ip_mib, 20947 ipIfStatsOutNoRoutes); 20948 goto drop_pkt; 20949 } 20950 err = conn_set_held_ipif(connp, 20951 &connp->conn_multicast_ipif, ipif); 20952 if (err == IPIF_LOOKUP_FAILED) { 20953 ipif_refrele(ipif); 20954 ip1dbg(("ip_wput: No ipif for " 20955 "multicast\n")); 20956 BUMP_MIB(&ipst->ips_ip_mib, 20957 ipIfStatsOutNoRoutes); 20958 goto drop_pkt; 20959 } 20960 } 20961 } 20962 ASSERT(!ipif->ipif_isv6); 20963 /* 20964 * As we may lose the conn by the time we reach ip_wput_ire, 20965 * we copy conn_multicast_loop and conn_dontroute on to an 20966 * ipsec_out. In case if this datagram goes out secure, 20967 * we need the ill_index also. Copy that also into the 20968 * ipsec_out. 20969 */ 20970 if (mctl_present) { 20971 io = (ipsec_out_t *)first_mp->b_rptr; 20972 ASSERT(first_mp->b_datap->db_type == M_CTL); 20973 ASSERT(io->ipsec_out_type == IPSEC_OUT); 20974 } else { 20975 ASSERT(mp == first_mp); 20976 if ((first_mp = allocb(sizeof (ipsec_info_t), 20977 BPRI_HI)) == NULL) { 20978 ipif_refrele(ipif); 20979 first_mp = mp; 20980 goto discard_pkt; 20981 } 20982 first_mp->b_datap->db_type = M_CTL; 20983 first_mp->b_wptr += sizeof (ipsec_info_t); 20984 /* ipsec_out_secure is B_FALSE now */ 20985 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 20986 io = (ipsec_out_t *)first_mp->b_rptr; 20987 io->ipsec_out_type = IPSEC_OUT; 20988 io->ipsec_out_len = sizeof (ipsec_out_t); 20989 io->ipsec_out_use_global_policy = B_TRUE; 20990 io->ipsec_out_ns = ipst->ips_netstack; 20991 first_mp->b_cont = mp; 20992 mctl_present = B_TRUE; 20993 } 20994 if (attach_ill != NULL) { 20995 ASSERT(attach_ill == ipif->ipif_ill); 20996 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 20997 20998 /* 20999 * Check if we need an ire that will not be 21000 * looked up by anybody else i.e. HIDDEN. 21001 */ 21002 if (ill_is_probeonly(attach_ill)) { 21003 match_flags |= MATCH_IRE_MARK_HIDDEN; 21004 } 21005 io->ipsec_out_ill_index = 21006 attach_ill->ill_phyint->phyint_ifindex; 21007 io->ipsec_out_attach_if = B_TRUE; 21008 } else { 21009 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 21010 io->ipsec_out_ill_index = 21011 ipif->ipif_ill->ill_phyint->phyint_ifindex; 21012 } 21013 if (connp != NULL) { 21014 io->ipsec_out_multicast_loop = 21015 connp->conn_multicast_loop; 21016 io->ipsec_out_dontroute = connp->conn_dontroute; 21017 io->ipsec_out_zoneid = connp->conn_zoneid; 21018 } 21019 /* 21020 * If the application uses IP_MULTICAST_IF with 21021 * different logical addresses of the same ILL, we 21022 * need to make sure that the soruce address of 21023 * the packet matches the logical IP address used 21024 * in the option. We do it by initializing ipha_src 21025 * here. This should keep IPsec also happy as 21026 * when we return from IPsec processing, we don't 21027 * have to worry about getting the right address on 21028 * the packet. Thus it is sufficient to look for 21029 * IRE_CACHE using MATCH_IRE_ILL rathen than 21030 * MATCH_IRE_IPIF. 21031 * 21032 * NOTE : We need to do it for non-secure case also as 21033 * this might go out secure if there is a global policy 21034 * match in ip_wput_ire. For bind to IPIF_NOFAILOVER 21035 * address, the source should be initialized already and 21036 * hence we won't be initializing here. 21037 * 21038 * As we do not have the ire yet, it is possible that 21039 * we set the source address here and then later discover 21040 * that the ire implies the source address to be assigned 21041 * through the RTF_SETSRC flag. 21042 * In that case, the setsrc variable will remind us 21043 * that overwritting the source address by the one 21044 * of the RTF_SETSRC-flagged ire is allowed. 21045 */ 21046 if (ipha->ipha_src == INADDR_ANY && 21047 (connp == NULL || !connp->conn_unspec_src)) { 21048 ipha->ipha_src = ipif->ipif_src_addr; 21049 setsrc = RTF_SETSRC; 21050 } 21051 /* 21052 * Find an IRE which matches the destination and the outgoing 21053 * queue (i.e. the outgoing interface.) 21054 * For loopback use a unicast IP address for 21055 * the ire lookup. 21056 */ 21057 if (IS_LOOPBACK(ipif->ipif_ill)) 21058 dst = ipif->ipif_lcl_addr; 21059 21060 /* 21061 * If IP_XMIT_IF is set, we branch out to ip_newroute_ipif. 21062 * We don't need to lookup ire in ctable as the packet 21063 * needs to be sent to the destination through the specified 21064 * ill irrespective of ires in the cache table. 21065 */ 21066 ire = NULL; 21067 if (xmit_ill == NULL) { 21068 ire = ire_ctable_lookup(dst, 0, 0, ipif, 21069 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21070 } 21071 21072 /* 21073 * refrele attach_ill as its not needed anymore. 21074 */ 21075 if (attach_ill != NULL) { 21076 ill_refrele(attach_ill); 21077 attach_ill = NULL; 21078 } 21079 21080 if (ire == NULL) { 21081 /* 21082 * Multicast loopback and multicast forwarding is 21083 * done in ip_wput_ire. 21084 * 21085 * Mark this packet to make it be delivered to 21086 * ip_wput_ire after the new ire has been 21087 * created. 21088 * 21089 * The call to ip_newroute_ipif takes into account 21090 * the setsrc reminder. In any case, we take care 21091 * of the RTF_MULTIRT flag. 21092 */ 21093 mp->b_prev = mp->b_next = NULL; 21094 if (xmit_ill == NULL || 21095 xmit_ill->ill_ipif_up_count > 0) { 21096 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 21097 setsrc | RTF_MULTIRT, zoneid, infop); 21098 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21099 "ip_wput_end: q %p (%S)", q, "noire"); 21100 } else { 21101 freemsg(first_mp); 21102 } 21103 ipif_refrele(ipif); 21104 if (xmit_ill != NULL) 21105 ill_refrele(xmit_ill); 21106 if (need_decref) 21107 CONN_DEC_REF(connp); 21108 return; 21109 } 21110 21111 ipif_refrele(ipif); 21112 ipif = NULL; 21113 ASSERT(xmit_ill == NULL); 21114 21115 /* 21116 * Honor the RTF_SETSRC flag for multicast packets, 21117 * if allowed by the setsrc reminder. 21118 */ 21119 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 21120 ipha->ipha_src = ire->ire_src_addr; 21121 } 21122 21123 /* 21124 * Unconditionally force the TTL to 1 for 21125 * multirouted multicast packets: 21126 * multirouted multicast should not cross 21127 * multicast routers. 21128 */ 21129 if (ire->ire_flags & RTF_MULTIRT) { 21130 if (ipha->ipha_ttl > 1) { 21131 ip2dbg(("ip_wput: forcing multicast " 21132 "multirt TTL to 1 (was %d), dst 0x%08x\n", 21133 ipha->ipha_ttl, ntohl(ire->ire_addr))); 21134 ipha->ipha_ttl = 1; 21135 } 21136 } 21137 } else { 21138 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), ipst); 21139 if ((ire != NULL) && (ire->ire_type & 21140 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 21141 ignore_dontroute = B_TRUE; 21142 ignore_nexthop = B_TRUE; 21143 } 21144 if (ire != NULL) { 21145 ire_refrele(ire); 21146 ire = NULL; 21147 } 21148 /* 21149 * Guard against coming in from arp in which case conn is NULL. 21150 * Also guard against non M_DATA with dontroute set but 21151 * destined to local, loopback or broadcast addresses. 21152 */ 21153 if (connp != NULL && connp->conn_dontroute && 21154 !ignore_dontroute) { 21155 dontroute: 21156 /* 21157 * Set TTL to 1 if SO_DONTROUTE is set to prevent 21158 * routing protocols from seeing false direct 21159 * connectivity. 21160 */ 21161 ipha->ipha_ttl = 1; 21162 /* 21163 * If IP_XMIT_IF is also set (conn_xmit_if_ill != NULL) 21164 * along with SO_DONTROUTE, higher precedence is 21165 * given to IP_XMIT_IF and the IP_XMIT_IF ipif is used. 21166 */ 21167 if (connp->conn_xmit_if_ill == NULL) { 21168 /* If suitable ipif not found, drop packet */ 21169 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid, 21170 ipst); 21171 if (dst_ipif == NULL) { 21172 ip1dbg(("ip_wput: no route for " 21173 "dst using SO_DONTROUTE\n")); 21174 BUMP_MIB(&ipst->ips_ip_mib, 21175 ipIfStatsOutNoRoutes); 21176 mp->b_prev = mp->b_next = NULL; 21177 if (first_mp == NULL) 21178 first_mp = mp; 21179 goto drop_pkt; 21180 } else { 21181 /* 21182 * If suitable ipif has been found, set 21183 * xmit_ill to the corresponding 21184 * ipif_ill because we'll be following 21185 * the IP_XMIT_IF logic. 21186 */ 21187 ASSERT(xmit_ill == NULL); 21188 xmit_ill = dst_ipif->ipif_ill; 21189 mutex_enter(&xmit_ill->ill_lock); 21190 if (!ILL_CAN_LOOKUP(xmit_ill)) { 21191 mutex_exit(&xmit_ill->ill_lock); 21192 xmit_ill = NULL; 21193 ipif_refrele(dst_ipif); 21194 ip1dbg(("ip_wput: no route for" 21195 " dst using" 21196 " SO_DONTROUTE\n")); 21197 BUMP_MIB(&ipst->ips_ip_mib, 21198 ipIfStatsOutNoRoutes); 21199 mp->b_prev = mp->b_next = NULL; 21200 if (first_mp == NULL) 21201 first_mp = mp; 21202 goto drop_pkt; 21203 } 21204 ill_refhold_locked(xmit_ill); 21205 mutex_exit(&xmit_ill->ill_lock); 21206 ipif_refrele(dst_ipif); 21207 } 21208 } 21209 21210 } 21211 /* 21212 * If we are bound to IPIF_NOFAILOVER address, look for 21213 * an IRE_CACHE matching the ill. 21214 */ 21215 send_from_ill: 21216 if (attach_ill != NULL) { 21217 ipif_t *attach_ipif; 21218 21219 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21220 21221 /* 21222 * Check if we need an ire that will not be 21223 * looked up by anybody else i.e. HIDDEN. 21224 */ 21225 if (ill_is_probeonly(attach_ill)) { 21226 match_flags |= MATCH_IRE_MARK_HIDDEN; 21227 } 21228 21229 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 21230 if (attach_ipif == NULL) { 21231 ip1dbg(("ip_wput: No ipif for attach_ill\n")); 21232 goto discard_pkt; 21233 } 21234 ire = ire_ctable_lookup(dst, 0, 0, attach_ipif, 21235 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21236 ipif_refrele(attach_ipif); 21237 } else if (xmit_ill != NULL || (connp != NULL && 21238 connp->conn_xmit_if_ill != NULL)) { 21239 /* 21240 * Mark this packet as originated locally 21241 */ 21242 mp->b_prev = mp->b_next = NULL; 21243 /* 21244 * xmit_ill could be NULL if SO_DONTROUTE 21245 * is also set. 21246 */ 21247 if (xmit_ill == NULL) { 21248 xmit_ill = conn_get_held_ill(connp, 21249 &connp->conn_xmit_if_ill, &err); 21250 if (err == ILL_LOOKUP_FAILED) { 21251 BUMP_MIB(&ipst->ips_ip_mib, 21252 ipIfStatsOutDiscards); 21253 if (need_decref) 21254 CONN_DEC_REF(connp); 21255 freemsg(first_mp); 21256 return; 21257 } 21258 if (xmit_ill == NULL) { 21259 if (connp->conn_dontroute) 21260 goto dontroute; 21261 goto send_from_ill; 21262 } 21263 } 21264 /* 21265 * Could be SO_DONTROUTE case also. 21266 * check at least one interface is UP as 21267 * specified by this ILL 21268 */ 21269 if (xmit_ill->ill_ipif_up_count > 0) { 21270 ipif_t *ipif; 21271 21272 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21273 if (ipif == NULL) { 21274 ip1dbg(("ip_output: " 21275 "xmit_ill NULL ipif\n")); 21276 goto drop_pkt; 21277 } 21278 /* 21279 * Look for a ire that is part of the group, 21280 * if found use it else call ip_newroute_ipif. 21281 * IPCL_ZONEID is not used for matching because 21282 * IP_ALLZONES option is valid only when the 21283 * ill is accessible from all zones i.e has a 21284 * valid ipif in all zones. 21285 */ 21286 match_flags = MATCH_IRE_ILL_GROUP | 21287 MATCH_IRE_SECATTR; 21288 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 21289 MBLK_GETLABEL(mp), match_flags, ipst); 21290 /* 21291 * If an ire exists use it or else create 21292 * an ire but don't add it to the cache. 21293 * Adding an ire may cause issues with 21294 * asymmetric routing. 21295 * In case of multiroute always act as if 21296 * ire does not exist. 21297 */ 21298 if (ire == NULL || 21299 ire->ire_flags & RTF_MULTIRT) { 21300 if (ire != NULL) 21301 ire_refrele(ire); 21302 ip_newroute_ipif(q, first_mp, ipif, 21303 dst, connp, 0, zoneid, infop); 21304 ipif_refrele(ipif); 21305 ip1dbg(("ip_wput: ip_unicast_if\n")); 21306 ill_refrele(xmit_ill); 21307 if (need_decref) 21308 CONN_DEC_REF(connp); 21309 return; 21310 } 21311 ipif_refrele(ipif); 21312 } else { 21313 goto drop_pkt; 21314 } 21315 } else if (ip_nexthop || (connp != NULL && 21316 (connp->conn_nexthop_set)) && !ignore_nexthop) { 21317 if (!ip_nexthop) { 21318 ip_nexthop = B_TRUE; 21319 nexthop_addr = connp->conn_nexthop_v4; 21320 } 21321 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 21322 MATCH_IRE_GW; 21323 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 21324 NULL, zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 21325 } else { 21326 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp), 21327 ipst); 21328 } 21329 if (!ire) { 21330 /* 21331 * Make sure we don't load spread if this 21332 * is IPIF_NOFAILOVER case. 21333 */ 21334 if ((attach_ill != NULL) || 21335 (ip_nexthop && !ignore_nexthop)) { 21336 if (mctl_present) { 21337 io = (ipsec_out_t *)first_mp->b_rptr; 21338 ASSERT(first_mp->b_datap->db_type == 21339 M_CTL); 21340 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21341 } else { 21342 ASSERT(mp == first_mp); 21343 first_mp = allocb( 21344 sizeof (ipsec_info_t), BPRI_HI); 21345 if (first_mp == NULL) { 21346 first_mp = mp; 21347 goto discard_pkt; 21348 } 21349 first_mp->b_datap->db_type = M_CTL; 21350 first_mp->b_wptr += 21351 sizeof (ipsec_info_t); 21352 /* ipsec_out_secure is B_FALSE now */ 21353 bzero(first_mp->b_rptr, 21354 sizeof (ipsec_info_t)); 21355 io = (ipsec_out_t *)first_mp->b_rptr; 21356 io->ipsec_out_type = IPSEC_OUT; 21357 io->ipsec_out_len = 21358 sizeof (ipsec_out_t); 21359 io->ipsec_out_use_global_policy = 21360 B_TRUE; 21361 io->ipsec_out_ns = ipst->ips_netstack; 21362 first_mp->b_cont = mp; 21363 mctl_present = B_TRUE; 21364 } 21365 if (attach_ill != NULL) { 21366 io->ipsec_out_ill_index = attach_ill-> 21367 ill_phyint->phyint_ifindex; 21368 io->ipsec_out_attach_if = B_TRUE; 21369 } else { 21370 io->ipsec_out_ip_nexthop = ip_nexthop; 21371 io->ipsec_out_nexthop_addr = 21372 nexthop_addr; 21373 } 21374 } 21375 noirefound: 21376 /* 21377 * Mark this packet as having originated on 21378 * this machine. This will be noted in 21379 * ire_add_then_send, which needs to know 21380 * whether to run it back through ip_wput or 21381 * ip_rput following successful resolution. 21382 */ 21383 mp->b_prev = NULL; 21384 mp->b_next = NULL; 21385 ip_newroute(q, first_mp, dst, connp, zoneid, ipst); 21386 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21387 "ip_wput_end: q %p (%S)", q, "newroute"); 21388 if (attach_ill != NULL) 21389 ill_refrele(attach_ill); 21390 if (xmit_ill != NULL) 21391 ill_refrele(xmit_ill); 21392 if (need_decref) 21393 CONN_DEC_REF(connp); 21394 return; 21395 } 21396 } 21397 21398 /* We now know where we are going with it. */ 21399 21400 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21401 "ip_wput_end: q %p (%S)", q, "end"); 21402 21403 /* 21404 * Check if the ire has the RTF_MULTIRT flag, inherited 21405 * from an IRE_OFFSUBNET ire entry in ip_newroute. 21406 */ 21407 if (ire->ire_flags & RTF_MULTIRT) { 21408 /* 21409 * Force the TTL of multirouted packets if required. 21410 * The TTL of such packets is bounded by the 21411 * ip_multirt_ttl ndd variable. 21412 */ 21413 if ((ipst->ips_ip_multirt_ttl > 0) && 21414 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 21415 ip2dbg(("ip_wput: forcing multirt TTL to %d " 21416 "(was %d), dst 0x%08x\n", 21417 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 21418 ntohl(ire->ire_addr))); 21419 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 21420 } 21421 /* 21422 * At this point, we check to see if there are any pending 21423 * unresolved routes. ire_multirt_resolvable() 21424 * checks in O(n) that all IRE_OFFSUBNET ire 21425 * entries for the packet's destination and 21426 * flagged RTF_MULTIRT are currently resolved. 21427 * If some remain unresolved, we make a copy 21428 * of the current message. It will be used 21429 * to initiate additional route resolutions. 21430 */ 21431 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 21432 MBLK_GETLABEL(first_mp), ipst); 21433 ip2dbg(("ip_wput[noirefound]: ire %p, " 21434 "multirt_need_resolve %d, first_mp %p\n", 21435 (void *)ire, multirt_need_resolve, (void *)first_mp)); 21436 if (multirt_need_resolve) { 21437 copy_mp = copymsg(first_mp); 21438 if (copy_mp != NULL) { 21439 MULTIRT_DEBUG_TAG(copy_mp); 21440 } 21441 } 21442 } 21443 21444 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 21445 /* 21446 * Try to resolve another multiroute if 21447 * ire_multirt_resolvable() deemed it necessary. 21448 * At this point, we need to distinguish 21449 * multicasts from other packets. For multicasts, 21450 * we call ip_newroute_ipif() and request that both 21451 * multirouting and setsrc flags are checked. 21452 */ 21453 if (copy_mp != NULL) { 21454 if (CLASSD(dst)) { 21455 ipif_t *ipif = ipif_lookup_group(dst, zoneid, ipst); 21456 if (ipif) { 21457 ASSERT(infop->ip_opt_ill_index == 0); 21458 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 21459 RTF_SETSRC | RTF_MULTIRT, zoneid, infop); 21460 ipif_refrele(ipif); 21461 } else { 21462 MULTIRT_DEBUG_UNTAG(copy_mp); 21463 freemsg(copy_mp); 21464 copy_mp = NULL; 21465 } 21466 } else { 21467 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 21468 } 21469 } 21470 if (attach_ill != NULL) 21471 ill_refrele(attach_ill); 21472 if (xmit_ill != NULL) 21473 ill_refrele(xmit_ill); 21474 if (need_decref) 21475 CONN_DEC_REF(connp); 21476 return; 21477 21478 icmp_parameter_problem: 21479 /* could not have originated externally */ 21480 ASSERT(mp->b_prev == NULL); 21481 if (ip_hdr_complete(ipha, zoneid, ipst) == 0) { 21482 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 21483 /* it's the IP header length that's in trouble */ 21484 icmp_param_problem(q, first_mp, 0, zoneid, ipst); 21485 first_mp = NULL; 21486 } 21487 21488 discard_pkt: 21489 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21490 drop_pkt: 21491 ip1dbg(("ip_wput: dropped packet\n")); 21492 if (ire != NULL) 21493 ire_refrele(ire); 21494 if (need_decref) 21495 CONN_DEC_REF(connp); 21496 freemsg(first_mp); 21497 if (attach_ill != NULL) 21498 ill_refrele(attach_ill); 21499 if (xmit_ill != NULL) 21500 ill_refrele(xmit_ill); 21501 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21502 "ip_wput_end: q %p (%S)", q, "droppkt"); 21503 } 21504 21505 /* 21506 * If this is a conn_t queue, then we pass in the conn. This includes the 21507 * zoneid. 21508 * Otherwise, this is a message coming back from ARP or for an ill_t queue, 21509 * in which case we use the global zoneid since those are all part of 21510 * the global zone. 21511 */ 21512 void 21513 ip_wput(queue_t *q, mblk_t *mp) 21514 { 21515 if (CONN_Q(q)) 21516 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 21517 else 21518 ip_output(GLOBAL_ZONEID, mp, q, IP_WPUT); 21519 } 21520 21521 /* 21522 * 21523 * The following rules must be observed when accessing any ipif or ill 21524 * that has been cached in the conn. Typically conn_nofailover_ill, 21525 * conn_xmit_if_ill, conn_multicast_ipif and conn_multicast_ill. 21526 * 21527 * Access: The ipif or ill pointed to from the conn can be accessed under 21528 * the protection of the conn_lock or after it has been refheld under the 21529 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 21530 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 21531 * The reason for this is that a concurrent unplumb could actually be 21532 * cleaning up these cached pointers by walking the conns and might have 21533 * finished cleaning up the conn in question. The macros check that an 21534 * unplumb has not yet started on the ipif or ill. 21535 * 21536 * Caching: An ipif or ill pointer may be cached in the conn only after 21537 * making sure that an unplumb has not started. So the caching is done 21538 * while holding both the conn_lock and the ill_lock and after using the 21539 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 21540 * flag before starting the cleanup of conns. 21541 * 21542 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 21543 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 21544 * or a reference to the ipif or a reference to an ire that references the 21545 * ipif. An ipif does not change its ill except for failover/failback. Since 21546 * failover/failback happens only after bringing down the ipif and making sure 21547 * the ipif refcnt has gone to zero and holding the ill_g_lock and ill_lock 21548 * the above holds. 21549 */ 21550 ipif_t * 21551 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 21552 { 21553 ipif_t *ipif; 21554 ill_t *ill; 21555 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 21556 21557 *err = 0; 21558 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 21559 mutex_enter(&connp->conn_lock); 21560 ipif = *ipifp; 21561 if (ipif != NULL) { 21562 ill = ipif->ipif_ill; 21563 mutex_enter(&ill->ill_lock); 21564 if (IPIF_CAN_LOOKUP(ipif)) { 21565 ipif_refhold_locked(ipif); 21566 mutex_exit(&ill->ill_lock); 21567 mutex_exit(&connp->conn_lock); 21568 rw_exit(&ipst->ips_ill_g_lock); 21569 return (ipif); 21570 } else { 21571 *err = IPIF_LOOKUP_FAILED; 21572 } 21573 mutex_exit(&ill->ill_lock); 21574 } 21575 mutex_exit(&connp->conn_lock); 21576 rw_exit(&ipst->ips_ill_g_lock); 21577 return (NULL); 21578 } 21579 21580 ill_t * 21581 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 21582 { 21583 ill_t *ill; 21584 21585 *err = 0; 21586 mutex_enter(&connp->conn_lock); 21587 ill = *illp; 21588 if (ill != NULL) { 21589 mutex_enter(&ill->ill_lock); 21590 if (ILL_CAN_LOOKUP(ill)) { 21591 ill_refhold_locked(ill); 21592 mutex_exit(&ill->ill_lock); 21593 mutex_exit(&connp->conn_lock); 21594 return (ill); 21595 } else { 21596 *err = ILL_LOOKUP_FAILED; 21597 } 21598 mutex_exit(&ill->ill_lock); 21599 } 21600 mutex_exit(&connp->conn_lock); 21601 return (NULL); 21602 } 21603 21604 static int 21605 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 21606 { 21607 ill_t *ill; 21608 21609 ill = ipif->ipif_ill; 21610 mutex_enter(&connp->conn_lock); 21611 mutex_enter(&ill->ill_lock); 21612 if (IPIF_CAN_LOOKUP(ipif)) { 21613 *ipifp = ipif; 21614 mutex_exit(&ill->ill_lock); 21615 mutex_exit(&connp->conn_lock); 21616 return (0); 21617 } 21618 mutex_exit(&ill->ill_lock); 21619 mutex_exit(&connp->conn_lock); 21620 return (IPIF_LOOKUP_FAILED); 21621 } 21622 21623 /* 21624 * This is called if the outbound datagram needs fragmentation. 21625 * 21626 * NOTE : This function does not ire_refrele the ire argument passed in. 21627 */ 21628 static void 21629 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire, zoneid_t zoneid, 21630 ip_stack_t *ipst) 21631 { 21632 ipha_t *ipha; 21633 mblk_t *mp; 21634 uint32_t v_hlen_tos_len; 21635 uint32_t max_frag; 21636 uint32_t frag_flag; 21637 boolean_t dont_use; 21638 21639 if (ipsec_mp->b_datap->db_type == M_CTL) { 21640 mp = ipsec_mp->b_cont; 21641 } else { 21642 mp = ipsec_mp; 21643 } 21644 21645 ipha = (ipha_t *)mp->b_rptr; 21646 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21647 21648 #ifdef _BIG_ENDIAN 21649 #define V_HLEN (v_hlen_tos_len >> 24) 21650 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21651 #else 21652 #define V_HLEN (v_hlen_tos_len & 0xFF) 21653 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21654 #endif 21655 21656 #ifndef SPEED_BEFORE_SAFETY 21657 /* 21658 * Check that ipha_length is consistent with 21659 * the mblk length 21660 */ 21661 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 21662 ip0dbg(("Packet length mismatch: %d, %ld\n", 21663 LENGTH, msgdsize(mp))); 21664 freemsg(ipsec_mp); 21665 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21666 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 21667 "packet length mismatch"); 21668 return; 21669 } 21670 #endif 21671 /* 21672 * Don't use frag_flag if pre-built packet or source 21673 * routed or if multicast (since multicast packets do not solicit 21674 * ICMP "packet too big" messages). Get the values of 21675 * max_frag and frag_flag atomically by acquiring the 21676 * ire_lock. 21677 */ 21678 mutex_enter(&ire->ire_lock); 21679 max_frag = ire->ire_max_frag; 21680 frag_flag = ire->ire_frag_flag; 21681 mutex_exit(&ire->ire_lock); 21682 21683 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 21684 (V_HLEN != IP_SIMPLE_HDR_VERSION && 21685 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 21686 21687 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 21688 (dont_use ? 0 : frag_flag), zoneid, ipst); 21689 } 21690 21691 /* 21692 * Used for deciding the MSS size for the upper layer. Thus 21693 * we need to check the outbound policy values in the conn. 21694 */ 21695 int 21696 conn_ipsec_length(conn_t *connp) 21697 { 21698 ipsec_latch_t *ipl; 21699 21700 ipl = connp->conn_latch; 21701 if (ipl == NULL) 21702 return (0); 21703 21704 if (ipl->ipl_out_policy == NULL) 21705 return (0); 21706 21707 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 21708 } 21709 21710 /* 21711 * Returns an estimate of the IPsec headers size. This is used if 21712 * we don't want to call into IPsec to get the exact size. 21713 */ 21714 int 21715 ipsec_out_extra_length(mblk_t *ipsec_mp) 21716 { 21717 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21718 ipsec_action_t *a; 21719 21720 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21721 if (!io->ipsec_out_secure) 21722 return (0); 21723 21724 a = io->ipsec_out_act; 21725 21726 if (a == NULL) { 21727 ASSERT(io->ipsec_out_policy != NULL); 21728 a = io->ipsec_out_policy->ipsp_act; 21729 } 21730 ASSERT(a != NULL); 21731 21732 return (a->ipa_ovhd); 21733 } 21734 21735 /* 21736 * Returns an estimate of the IPsec headers size. This is used if 21737 * we don't want to call into IPsec to get the exact size. 21738 */ 21739 int 21740 ipsec_in_extra_length(mblk_t *ipsec_mp) 21741 { 21742 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21743 ipsec_action_t *a; 21744 21745 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21746 21747 a = ii->ipsec_in_action; 21748 return (a == NULL ? 0 : a->ipa_ovhd); 21749 } 21750 21751 /* 21752 * If there are any source route options, return the true final 21753 * destination. Otherwise, return the destination. 21754 */ 21755 ipaddr_t 21756 ip_get_dst(ipha_t *ipha) 21757 { 21758 ipoptp_t opts; 21759 uchar_t *opt; 21760 uint8_t optval; 21761 uint8_t optlen; 21762 ipaddr_t dst; 21763 uint32_t off; 21764 21765 dst = ipha->ipha_dst; 21766 21767 if (IS_SIMPLE_IPH(ipha)) 21768 return (dst); 21769 21770 for (optval = ipoptp_first(&opts, ipha); 21771 optval != IPOPT_EOL; 21772 optval = ipoptp_next(&opts)) { 21773 opt = opts.ipoptp_cur; 21774 optlen = opts.ipoptp_len; 21775 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 21776 switch (optval) { 21777 case IPOPT_SSRR: 21778 case IPOPT_LSRR: 21779 off = opt[IPOPT_OFFSET]; 21780 /* 21781 * If one of the conditions is true, it means 21782 * end of options and dst already has the right 21783 * value. 21784 */ 21785 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 21786 off = optlen - IP_ADDR_LEN; 21787 bcopy(&opt[off], &dst, IP_ADDR_LEN); 21788 } 21789 return (dst); 21790 default: 21791 break; 21792 } 21793 } 21794 21795 return (dst); 21796 } 21797 21798 mblk_t * 21799 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 21800 conn_t *connp, boolean_t unspec_src, zoneid_t zoneid) 21801 { 21802 ipsec_out_t *io; 21803 mblk_t *first_mp; 21804 boolean_t policy_present; 21805 ip_stack_t *ipst; 21806 ipsec_stack_t *ipss; 21807 21808 ASSERT(ire != NULL); 21809 ipst = ire->ire_ipst; 21810 ipss = ipst->ips_netstack->netstack_ipsec; 21811 21812 first_mp = mp; 21813 if (mp->b_datap->db_type == M_CTL) { 21814 io = (ipsec_out_t *)first_mp->b_rptr; 21815 /* 21816 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 21817 * 21818 * 1) There is per-socket policy (including cached global 21819 * policy) or a policy on the IP-in-IP tunnel. 21820 * 2) There is no per-socket policy, but it is 21821 * a multicast packet that needs to go out 21822 * on a specific interface. This is the case 21823 * where (ip_wput and ip_wput_multicast) attaches 21824 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 21825 * 21826 * In case (2) we check with global policy to 21827 * see if there is a match and set the ill_index 21828 * appropriately so that we can lookup the ire 21829 * properly in ip_wput_ipsec_out. 21830 */ 21831 21832 /* 21833 * ipsec_out_use_global_policy is set to B_FALSE 21834 * in ipsec_in_to_out(). Refer to that function for 21835 * details. 21836 */ 21837 if ((io->ipsec_out_latch == NULL) && 21838 (io->ipsec_out_use_global_policy)) { 21839 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 21840 ire, connp, unspec_src, zoneid)); 21841 } 21842 if (!io->ipsec_out_secure) { 21843 /* 21844 * If this is not a secure packet, drop 21845 * the IPSEC_OUT mp and treat it as a clear 21846 * packet. This happens when we are sending 21847 * a ICMP reply back to a clear packet. See 21848 * ipsec_in_to_out() for details. 21849 */ 21850 mp = first_mp->b_cont; 21851 freeb(first_mp); 21852 } 21853 return (mp); 21854 } 21855 /* 21856 * See whether we need to attach a global policy here. We 21857 * don't depend on the conn (as it could be null) for deciding 21858 * what policy this datagram should go through because it 21859 * should have happened in ip_wput if there was some 21860 * policy. This normally happens for connections which are not 21861 * fully bound preventing us from caching policies in 21862 * ip_bind. Packets coming from the TCP listener/global queue 21863 * - which are non-hard_bound - could also be affected by 21864 * applying policy here. 21865 * 21866 * If this packet is coming from tcp global queue or listener, 21867 * we will be applying policy here. This may not be *right* 21868 * if these packets are coming from the detached connection as 21869 * it could have gone in clear before. This happens only if a 21870 * TCP connection started when there is no policy and somebody 21871 * added policy before it became detached. Thus packets of the 21872 * detached connection could go out secure and the other end 21873 * would drop it because it will be expecting in clear. The 21874 * converse is not true i.e if somebody starts a TCP 21875 * connection and deletes the policy, all the packets will 21876 * still go out with the policy that existed before deleting 21877 * because ip_unbind sends up policy information which is used 21878 * by TCP on subsequent ip_wputs. The right solution is to fix 21879 * TCP to attach a dummy IPSEC_OUT and set 21880 * ipsec_out_use_global_policy to B_FALSE. As this might 21881 * affect performance for normal cases, we are not doing it. 21882 * Thus, set policy before starting any TCP connections. 21883 * 21884 * NOTE - We might apply policy even for a hard bound connection 21885 * - for which we cached policy in ip_bind - if somebody added 21886 * global policy after we inherited the policy in ip_bind. 21887 * This means that the packets that were going out in clear 21888 * previously would start going secure and hence get dropped 21889 * on the other side. To fix this, TCP attaches a dummy 21890 * ipsec_out and make sure that we don't apply global policy. 21891 */ 21892 if (ipha != NULL) 21893 policy_present = ipss->ipsec_outbound_v4_policy_present; 21894 else 21895 policy_present = ipss->ipsec_outbound_v6_policy_present; 21896 if (!policy_present) 21897 return (mp); 21898 21899 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src, 21900 zoneid)); 21901 } 21902 21903 ire_t * 21904 conn_set_outgoing_ill(conn_t *connp, ire_t *ire, ill_t **conn_outgoing_ill) 21905 { 21906 ipaddr_t addr; 21907 ire_t *save_ire; 21908 irb_t *irb; 21909 ill_group_t *illgrp; 21910 int err; 21911 21912 save_ire = ire; 21913 addr = ire->ire_addr; 21914 21915 ASSERT(ire->ire_type == IRE_BROADCAST); 21916 21917 illgrp = connp->conn_outgoing_ill->ill_group; 21918 if (illgrp == NULL) { 21919 *conn_outgoing_ill = conn_get_held_ill(connp, 21920 &connp->conn_outgoing_ill, &err); 21921 if (err == ILL_LOOKUP_FAILED) { 21922 ire_refrele(save_ire); 21923 return (NULL); 21924 } 21925 return (save_ire); 21926 } 21927 /* 21928 * If IP_BOUND_IF has been done, conn_outgoing_ill will be set. 21929 * If it is part of the group, we need to send on the ire 21930 * that has been cleared of IRE_MARK_NORECV and that belongs 21931 * to this group. This is okay as IP_BOUND_IF really means 21932 * any ill in the group. We depend on the fact that the 21933 * first ire in the group is always cleared of IRE_MARK_NORECV 21934 * if such an ire exists. This is possible only if you have 21935 * at least one ill in the group that has not failed. 21936 * 21937 * First get to the ire that matches the address and group. 21938 * 21939 * We don't look for an ire with a matching zoneid because a given zone 21940 * won't always have broadcast ires on all ills in the group. 21941 */ 21942 irb = ire->ire_bucket; 21943 rw_enter(&irb->irb_lock, RW_READER); 21944 if (ire->ire_marks & IRE_MARK_NORECV) { 21945 /* 21946 * If the current zone only has an ire broadcast for this 21947 * address marked NORECV, the ire we want is ahead in the 21948 * bucket, so we look it up deliberately ignoring the zoneid. 21949 */ 21950 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 21951 if (ire->ire_addr != addr) 21952 continue; 21953 /* skip over deleted ires */ 21954 if (ire->ire_marks & IRE_MARK_CONDEMNED) 21955 continue; 21956 } 21957 } 21958 while (ire != NULL) { 21959 /* 21960 * If a new interface is coming up, we could end up 21961 * seeing the loopback ire and the non-loopback ire 21962 * may not have been added yet. So check for ire_stq 21963 */ 21964 if (ire->ire_stq != NULL && (ire->ire_addr != addr || 21965 ire->ire_ipif->ipif_ill->ill_group == illgrp)) { 21966 break; 21967 } 21968 ire = ire->ire_next; 21969 } 21970 if (ire != NULL && ire->ire_addr == addr && 21971 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 21972 IRE_REFHOLD(ire); 21973 rw_exit(&irb->irb_lock); 21974 ire_refrele(save_ire); 21975 *conn_outgoing_ill = ire_to_ill(ire); 21976 /* 21977 * Refhold the ill to make the conn_outgoing_ill 21978 * independent of the ire. ip_wput_ire goes in a loop 21979 * and may refrele the ire. Since we have an ire at this 21980 * point we don't need to use ILL_CAN_LOOKUP on the ill. 21981 */ 21982 ill_refhold(*conn_outgoing_ill); 21983 return (ire); 21984 } 21985 rw_exit(&irb->irb_lock); 21986 ip1dbg(("conn_set_outgoing_ill: No matching ire\n")); 21987 /* 21988 * If we can't find a suitable ire, return the original ire. 21989 */ 21990 return (save_ire); 21991 } 21992 21993 /* 21994 * This function does the ire_refrele of the ire passed in as the 21995 * argument. As this function looks up more ires i.e broadcast ires, 21996 * it needs to REFRELE them. Currently, for simplicity we don't 21997 * differentiate the one passed in and looked up here. We always 21998 * REFRELE. 21999 * IPQoS Notes: 22000 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 22001 * IPsec packets are done in ipsec_out_process. 22002 * 22003 */ 22004 void 22005 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller, 22006 zoneid_t zoneid) 22007 { 22008 ipha_t *ipha; 22009 #define rptr ((uchar_t *)ipha) 22010 queue_t *stq; 22011 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 22012 uint32_t v_hlen_tos_len; 22013 uint32_t ttl_protocol; 22014 ipaddr_t src; 22015 ipaddr_t dst; 22016 uint32_t cksum; 22017 ipaddr_t orig_src; 22018 ire_t *ire1; 22019 mblk_t *next_mp; 22020 uint_t hlen; 22021 uint16_t *up; 22022 uint32_t max_frag = ire->ire_max_frag; 22023 ill_t *ill = ire_to_ill(ire); 22024 int clusterwide; 22025 uint16_t ip_hdr_included; /* IP header included by ULP? */ 22026 int ipsec_len; 22027 mblk_t *first_mp; 22028 ipsec_out_t *io; 22029 boolean_t conn_dontroute; /* conn value for multicast */ 22030 boolean_t conn_multicast_loop; /* conn value for multicast */ 22031 boolean_t multicast_forward; /* Should we forward ? */ 22032 boolean_t unspec_src; 22033 ill_t *conn_outgoing_ill = NULL; 22034 ill_t *ire_ill; 22035 ill_t *ire1_ill; 22036 ill_t *out_ill; 22037 uint32_t ill_index = 0; 22038 boolean_t multirt_send = B_FALSE; 22039 int err; 22040 ipxmit_state_t pktxmit_state; 22041 ip_stack_t *ipst = ire->ire_ipst; 22042 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 22043 22044 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 22045 "ip_wput_ire_start: q %p", q); 22046 22047 multicast_forward = B_FALSE; 22048 unspec_src = (connp != NULL && connp->conn_unspec_src); 22049 22050 if (ire->ire_flags & RTF_MULTIRT) { 22051 /* 22052 * Multirouting case. The bucket where ire is stored 22053 * probably holds other RTF_MULTIRT flagged ire 22054 * to the destination. In this call to ip_wput_ire, 22055 * we attempt to send the packet through all 22056 * those ires. Thus, we first ensure that ire is the 22057 * first RTF_MULTIRT ire in the bucket, 22058 * before walking the ire list. 22059 */ 22060 ire_t *first_ire; 22061 irb_t *irb = ire->ire_bucket; 22062 ASSERT(irb != NULL); 22063 22064 /* Make sure we do not omit any multiroute ire. */ 22065 IRB_REFHOLD(irb); 22066 for (first_ire = irb->irb_ire; 22067 first_ire != NULL; 22068 first_ire = first_ire->ire_next) { 22069 if ((first_ire->ire_flags & RTF_MULTIRT) && 22070 (first_ire->ire_addr == ire->ire_addr) && 22071 !(first_ire->ire_marks & 22072 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) { 22073 break; 22074 } 22075 } 22076 22077 if ((first_ire != NULL) && (first_ire != ire)) { 22078 IRE_REFHOLD(first_ire); 22079 ire_refrele(ire); 22080 ire = first_ire; 22081 ill = ire_to_ill(ire); 22082 } 22083 IRB_REFRELE(irb); 22084 } 22085 22086 /* 22087 * conn_outgoing_ill is used only in the broadcast loop. 22088 * for performance we don't grab the mutexs in the fastpath 22089 */ 22090 if ((connp != NULL) && 22091 (connp->conn_xmit_if_ill == NULL) && 22092 (ire->ire_type == IRE_BROADCAST) && 22093 ((connp->conn_nofailover_ill != NULL) || 22094 (connp->conn_outgoing_ill != NULL))) { 22095 /* 22096 * Bind to IPIF_NOFAILOVER address overrides IP_BOUND_IF 22097 * option. So, see if this endpoint is bound to a 22098 * IPIF_NOFAILOVER address. If so, honor it. This implies 22099 * that if the interface is failed, we will still send 22100 * the packet on the same ill which is what we want. 22101 */ 22102 conn_outgoing_ill = conn_get_held_ill(connp, 22103 &connp->conn_nofailover_ill, &err); 22104 if (err == ILL_LOOKUP_FAILED) { 22105 ire_refrele(ire); 22106 freemsg(mp); 22107 return; 22108 } 22109 if (conn_outgoing_ill == NULL) { 22110 /* 22111 * Choose a good ill in the group to send the 22112 * packets on. 22113 */ 22114 ire = conn_set_outgoing_ill(connp, ire, 22115 &conn_outgoing_ill); 22116 if (ire == NULL) { 22117 freemsg(mp); 22118 return; 22119 } 22120 } 22121 } 22122 22123 if (mp->b_datap->db_type != M_CTL) { 22124 ipha = (ipha_t *)mp->b_rptr; 22125 } else { 22126 io = (ipsec_out_t *)mp->b_rptr; 22127 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22128 ASSERT(zoneid == io->ipsec_out_zoneid); 22129 ASSERT(zoneid != ALL_ZONES); 22130 ipha = (ipha_t *)mp->b_cont->b_rptr; 22131 dst = ipha->ipha_dst; 22132 /* 22133 * For the multicast case, ipsec_out carries conn_dontroute and 22134 * conn_multicast_loop as conn may not be available here. We 22135 * need this for multicast loopback and forwarding which is done 22136 * later in the code. 22137 */ 22138 if (CLASSD(dst)) { 22139 conn_dontroute = io->ipsec_out_dontroute; 22140 conn_multicast_loop = io->ipsec_out_multicast_loop; 22141 /* 22142 * If conn_dontroute is not set or conn_multicast_loop 22143 * is set, we need to do forwarding/loopback. For 22144 * datagrams from ip_wput_multicast, conn_dontroute is 22145 * set to B_TRUE and conn_multicast_loop is set to 22146 * B_FALSE so that we neither do forwarding nor 22147 * loopback. 22148 */ 22149 if (!conn_dontroute || conn_multicast_loop) 22150 multicast_forward = B_TRUE; 22151 } 22152 } 22153 22154 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 22155 ire->ire_zoneid != ALL_ZONES) { 22156 /* 22157 * When a zone sends a packet to another zone, we try to deliver 22158 * the packet under the same conditions as if the destination 22159 * was a real node on the network. To do so, we look for a 22160 * matching route in the forwarding table. 22161 * RTF_REJECT and RTF_BLACKHOLE are handled just like 22162 * ip_newroute() does. 22163 * Note that IRE_LOCAL are special, since they are used 22164 * when the zoneid doesn't match in some cases. This means that 22165 * we need to handle ipha_src differently since ire_src_addr 22166 * belongs to the receiving zone instead of the sending zone. 22167 * When ip_restrict_interzone_loopback is set, then 22168 * ire_cache_lookup() ensures that IRE_LOCAL are only used 22169 * for loopback between zones when the logical "Ethernet" would 22170 * have looped them back. 22171 */ 22172 ire_t *src_ire; 22173 22174 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 22175 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 22176 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE), ipst); 22177 if (src_ire != NULL && 22178 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) && 22179 (!ipst->ips_ip_restrict_interzone_loopback || 22180 ire_local_same_ill_group(ire, src_ire))) { 22181 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 22182 ipha->ipha_src = src_ire->ire_src_addr; 22183 ire_refrele(src_ire); 22184 } else { 22185 ire_refrele(ire); 22186 if (conn_outgoing_ill != NULL) 22187 ill_refrele(conn_outgoing_ill); 22188 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 22189 if (src_ire != NULL) { 22190 if (src_ire->ire_flags & RTF_BLACKHOLE) { 22191 ire_refrele(src_ire); 22192 freemsg(mp); 22193 return; 22194 } 22195 ire_refrele(src_ire); 22196 } 22197 if (ip_hdr_complete(ipha, zoneid, ipst)) { 22198 /* Failed */ 22199 freemsg(mp); 22200 return; 22201 } 22202 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, zoneid, 22203 ipst); 22204 return; 22205 } 22206 } 22207 22208 if (mp->b_datap->db_type == M_CTL || 22209 ipss->ipsec_outbound_v4_policy_present) { 22210 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 22211 unspec_src, zoneid); 22212 if (mp == NULL) { 22213 ire_refrele(ire); 22214 if (conn_outgoing_ill != NULL) 22215 ill_refrele(conn_outgoing_ill); 22216 return; 22217 } 22218 } 22219 22220 first_mp = mp; 22221 ipsec_len = 0; 22222 22223 if (first_mp->b_datap->db_type == M_CTL) { 22224 io = (ipsec_out_t *)first_mp->b_rptr; 22225 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22226 mp = first_mp->b_cont; 22227 ipsec_len = ipsec_out_extra_length(first_mp); 22228 ASSERT(ipsec_len >= 0); 22229 /* We already picked up the zoneid from the M_CTL above */ 22230 ASSERT(zoneid == io->ipsec_out_zoneid); 22231 ASSERT(zoneid != ALL_ZONES); 22232 22233 /* 22234 * Drop M_CTL here if IPsec processing is not needed. 22235 * (Non-IPsec use of M_CTL extracted any information it 22236 * needed above). 22237 */ 22238 if (ipsec_len == 0) { 22239 freeb(first_mp); 22240 first_mp = mp; 22241 } 22242 } 22243 22244 /* 22245 * Fast path for ip_wput_ire 22246 */ 22247 22248 ipha = (ipha_t *)mp->b_rptr; 22249 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 22250 dst = ipha->ipha_dst; 22251 22252 /* 22253 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 22254 * if the socket is a SOCK_RAW type. The transport checksum should 22255 * be provided in the pre-built packet, so we don't need to compute it. 22256 * Also, other application set flags, like DF, should not be altered. 22257 * Other transport MUST pass down zero. 22258 */ 22259 ip_hdr_included = ipha->ipha_ident; 22260 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 22261 22262 if (CLASSD(dst)) { 22263 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 22264 ntohl(dst), 22265 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 22266 ntohl(ire->ire_addr))); 22267 } 22268 22269 /* Macros to extract header fields from data already in registers */ 22270 #ifdef _BIG_ENDIAN 22271 #define V_HLEN (v_hlen_tos_len >> 24) 22272 #define LENGTH (v_hlen_tos_len & 0xFFFF) 22273 #define PROTO (ttl_protocol & 0xFF) 22274 #else 22275 #define V_HLEN (v_hlen_tos_len & 0xFF) 22276 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 22277 #define PROTO (ttl_protocol >> 8) 22278 #endif 22279 22280 22281 orig_src = src = ipha->ipha_src; 22282 /* (The loop back to "another" is explained down below.) */ 22283 another:; 22284 /* 22285 * Assign an ident value for this packet. We assign idents on 22286 * a per destination basis out of the IRE. There could be 22287 * other threads targeting the same destination, so we have to 22288 * arrange for a atomic increment. Note that we use a 32-bit 22289 * atomic add because it has better performance than its 22290 * 16-bit sibling. 22291 * 22292 * If running in cluster mode and if the source address 22293 * belongs to a replicated service then vector through 22294 * cl_inet_ipident vector to allocate ip identifier 22295 * NOTE: This is a contract private interface with the 22296 * clustering group. 22297 */ 22298 clusterwide = 0; 22299 if (cl_inet_ipident) { 22300 ASSERT(cl_inet_isclusterwide); 22301 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 22302 AF_INET, (uint8_t *)(uintptr_t)src)) { 22303 ipha->ipha_ident = (*cl_inet_ipident)(IPPROTO_IP, 22304 AF_INET, (uint8_t *)(uintptr_t)src, 22305 (uint8_t *)(uintptr_t)dst); 22306 clusterwide = 1; 22307 } 22308 } 22309 if (!clusterwide) { 22310 ipha->ipha_ident = 22311 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 22312 } 22313 22314 #ifndef _BIG_ENDIAN 22315 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 22316 #endif 22317 22318 /* 22319 * Set source address unless sent on an ill or conn_unspec_src is set. 22320 * This is needed to obey conn_unspec_src when packets go through 22321 * ip_newroute + arp. 22322 * Assumes ip_newroute{,_multi} sets the source address as well. 22323 */ 22324 if (src == INADDR_ANY && !unspec_src) { 22325 /* 22326 * Assign the appropriate source address from the IRE if none 22327 * was specified. 22328 */ 22329 ASSERT(ire->ire_ipversion == IPV4_VERSION); 22330 22331 /* 22332 * With IP multipathing, broadcast packets are sent on the ire 22333 * that has been cleared of IRE_MARK_NORECV and that belongs to 22334 * the group. However, this ire might not be in the same zone so 22335 * we can't always use its source address. We look for a 22336 * broadcast ire in the same group and in the right zone. 22337 */ 22338 if (ire->ire_type == IRE_BROADCAST && 22339 ire->ire_zoneid != zoneid) { 22340 ire_t *src_ire = ire_ctable_lookup(dst, 0, 22341 IRE_BROADCAST, ire->ire_ipif, zoneid, NULL, 22342 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 22343 if (src_ire != NULL) { 22344 src = src_ire->ire_src_addr; 22345 ire_refrele(src_ire); 22346 } else { 22347 ire_refrele(ire); 22348 if (conn_outgoing_ill != NULL) 22349 ill_refrele(conn_outgoing_ill); 22350 freemsg(first_mp); 22351 if (ill != NULL) { 22352 BUMP_MIB(ill->ill_ip_mib, 22353 ipIfStatsOutDiscards); 22354 } else { 22355 BUMP_MIB(&ipst->ips_ip_mib, 22356 ipIfStatsOutDiscards); 22357 } 22358 return; 22359 } 22360 } else { 22361 src = ire->ire_src_addr; 22362 } 22363 22364 if (connp == NULL) { 22365 ip1dbg(("ip_wput_ire: no connp and no src " 22366 "address for dst 0x%x, using src 0x%x\n", 22367 ntohl(dst), 22368 ntohl(src))); 22369 } 22370 ipha->ipha_src = src; 22371 } 22372 stq = ire->ire_stq; 22373 22374 /* 22375 * We only allow ire chains for broadcasts since there will 22376 * be multiple IRE_CACHE entries for the same multicast 22377 * address (one per ipif). 22378 */ 22379 next_mp = NULL; 22380 22381 /* broadcast packet */ 22382 if (ire->ire_type == IRE_BROADCAST) 22383 goto broadcast; 22384 22385 /* loopback ? */ 22386 if (stq == NULL) 22387 goto nullstq; 22388 22389 /* The ill_index for outbound ILL */ 22390 ill_index = Q_TO_INDEX(stq); 22391 22392 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 22393 ttl_protocol = ((uint16_t *)ipha)[4]; 22394 22395 /* pseudo checksum (do it in parts for IP header checksum) */ 22396 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 22397 22398 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 22399 queue_t *dev_q = stq->q_next; 22400 22401 /* flow controlled */ 22402 if ((dev_q->q_next || dev_q->q_first) && 22403 !canput(dev_q)) 22404 goto blocked; 22405 if ((PROTO == IPPROTO_UDP) && 22406 (ip_hdr_included != IP_HDR_INCLUDED)) { 22407 hlen = (V_HLEN & 0xF) << 2; 22408 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22409 if (*up != 0) { 22410 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 22411 hlen, LENGTH, max_frag, ipsec_len, cksum); 22412 /* Software checksum? */ 22413 if (DB_CKSUMFLAGS(mp) == 0) { 22414 IP_STAT(ipst, ip_out_sw_cksum); 22415 IP_STAT_UPDATE(ipst, 22416 ip_udp_out_sw_cksum_bytes, 22417 LENGTH - hlen); 22418 } 22419 } 22420 } 22421 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 22422 hlen = (V_HLEN & 0xF) << 2; 22423 if (PROTO == IPPROTO_TCP) { 22424 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22425 /* 22426 * The packet header is processed once and for all, even 22427 * in the multirouting case. We disable hardware 22428 * checksum if the packet is multirouted, as it will be 22429 * replicated via several interfaces, and not all of 22430 * them may have this capability. 22431 */ 22432 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 22433 LENGTH, max_frag, ipsec_len, cksum); 22434 /* Software checksum? */ 22435 if (DB_CKSUMFLAGS(mp) == 0) { 22436 IP_STAT(ipst, ip_out_sw_cksum); 22437 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22438 LENGTH - hlen); 22439 } 22440 } else { 22441 sctp_hdr_t *sctph; 22442 22443 ASSERT(PROTO == IPPROTO_SCTP); 22444 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22445 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22446 /* 22447 * Zero out the checksum field to ensure proper 22448 * checksum calculation. 22449 */ 22450 sctph->sh_chksum = 0; 22451 #ifdef DEBUG 22452 if (!skip_sctp_cksum) 22453 #endif 22454 sctph->sh_chksum = sctp_cksum(mp, hlen); 22455 } 22456 } 22457 22458 /* 22459 * If this is a multicast packet and originated from ip_wput 22460 * we need to do loopback and forwarding checks. If it comes 22461 * from ip_wput_multicast, we SHOULD not do this. 22462 */ 22463 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 22464 22465 /* checksum */ 22466 cksum += ttl_protocol; 22467 22468 /* fragment the packet */ 22469 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 22470 goto fragmentit; 22471 /* 22472 * Don't use frag_flag if packet is pre-built or source 22473 * routed or if multicast (since multicast packets do 22474 * not solicit ICMP "packet too big" messages). 22475 */ 22476 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22477 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22478 !ip_source_route_included(ipha)) && 22479 !CLASSD(ipha->ipha_dst)) 22480 ipha->ipha_fragment_offset_and_flags |= 22481 htons(ire->ire_frag_flag); 22482 22483 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22484 /* calculate IP header checksum */ 22485 cksum += ipha->ipha_ident; 22486 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 22487 cksum += ipha->ipha_fragment_offset_and_flags; 22488 22489 /* IP options present */ 22490 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22491 if (hlen) 22492 goto checksumoptions; 22493 22494 /* calculate hdr checksum */ 22495 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22496 cksum = ~(cksum + (cksum >> 16)); 22497 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22498 } 22499 if (ipsec_len != 0) { 22500 /* 22501 * We will do the rest of the processing after 22502 * we come back from IPsec in ip_wput_ipsec_out(). 22503 */ 22504 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 22505 22506 io = (ipsec_out_t *)first_mp->b_rptr; 22507 io->ipsec_out_ill_index = ((ill_t *)stq->q_ptr)-> 22508 ill_phyint->phyint_ifindex; 22509 22510 ipsec_out_process(q, first_mp, ire, ill_index); 22511 ire_refrele(ire); 22512 if (conn_outgoing_ill != NULL) 22513 ill_refrele(conn_outgoing_ill); 22514 return; 22515 } 22516 22517 /* 22518 * In most cases, the emission loop below is entered only 22519 * once. Only in the case where the ire holds the 22520 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 22521 * flagged ires in the bucket, and send the packet 22522 * through all crossed RTF_MULTIRT routes. 22523 */ 22524 if (ire->ire_flags & RTF_MULTIRT) { 22525 multirt_send = B_TRUE; 22526 } 22527 do { 22528 if (multirt_send) { 22529 irb_t *irb; 22530 /* 22531 * We are in a multiple send case, need to get 22532 * the next ire and make a duplicate of the packet. 22533 * ire1 holds here the next ire to process in the 22534 * bucket. If multirouting is expected, 22535 * any non-RTF_MULTIRT ire that has the 22536 * right destination address is ignored. 22537 */ 22538 irb = ire->ire_bucket; 22539 ASSERT(irb != NULL); 22540 22541 IRB_REFHOLD(irb); 22542 for (ire1 = ire->ire_next; 22543 ire1 != NULL; 22544 ire1 = ire1->ire_next) { 22545 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 22546 continue; 22547 if (ire1->ire_addr != ire->ire_addr) 22548 continue; 22549 if (ire1->ire_marks & 22550 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 22551 continue; 22552 22553 /* Got one */ 22554 IRE_REFHOLD(ire1); 22555 break; 22556 } 22557 IRB_REFRELE(irb); 22558 22559 if (ire1 != NULL) { 22560 next_mp = copyb(mp); 22561 if ((next_mp == NULL) || 22562 ((mp->b_cont != NULL) && 22563 ((next_mp->b_cont = 22564 dupmsg(mp->b_cont)) == NULL))) { 22565 freemsg(next_mp); 22566 next_mp = NULL; 22567 ire_refrele(ire1); 22568 ire1 = NULL; 22569 } 22570 } 22571 22572 /* Last multiroute ire; don't loop anymore. */ 22573 if (ire1 == NULL) { 22574 multirt_send = B_FALSE; 22575 } 22576 } 22577 22578 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 22579 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 22580 mblk_t *, mp); 22581 FW_HOOKS(ipst->ips_ip4_physical_out_event, 22582 ipst->ips_ipv4firewall_physical_out, 22583 NULL, ire->ire_ipif->ipif_ill, ipha, mp, mp, ipst); 22584 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 22585 if (mp == NULL) 22586 goto release_ire_and_ill; 22587 22588 mp->b_prev = SET_BPREV_FLAG(IPP_LOCAL_OUT); 22589 DTRACE_PROBE2(ip__xmit__1, mblk_t *, mp, ire_t *, ire); 22590 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_TRUE); 22591 if ((pktxmit_state == SEND_FAILED) || 22592 (pktxmit_state == LLHDR_RESLV_FAILED)) { 22593 ip2dbg(("ip_wput_ire: ip_xmit_v4 failed" 22594 "- packet dropped\n")); 22595 release_ire_and_ill: 22596 ire_refrele(ire); 22597 if (next_mp != NULL) { 22598 freemsg(next_mp); 22599 ire_refrele(ire1); 22600 } 22601 if (conn_outgoing_ill != NULL) 22602 ill_refrele(conn_outgoing_ill); 22603 return; 22604 } 22605 22606 if (CLASSD(dst)) { 22607 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastPkts); 22608 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastOctets, 22609 LENGTH); 22610 } 22611 22612 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22613 "ip_wput_ire_end: q %p (%S)", 22614 q, "last copy out"); 22615 IRE_REFRELE(ire); 22616 22617 if (multirt_send) { 22618 ASSERT(ire1); 22619 /* 22620 * Proceed with the next RTF_MULTIRT ire, 22621 * Also set up the send-to queue accordingly. 22622 */ 22623 ire = ire1; 22624 ire1 = NULL; 22625 stq = ire->ire_stq; 22626 mp = next_mp; 22627 next_mp = NULL; 22628 ipha = (ipha_t *)mp->b_rptr; 22629 ill_index = Q_TO_INDEX(stq); 22630 ill = (ill_t *)stq->q_ptr; 22631 } 22632 } while (multirt_send); 22633 if (conn_outgoing_ill != NULL) 22634 ill_refrele(conn_outgoing_ill); 22635 return; 22636 22637 /* 22638 * ire->ire_type == IRE_BROADCAST (minimize diffs) 22639 */ 22640 broadcast: 22641 { 22642 /* 22643 * Avoid broadcast storms by setting the ttl to 1 22644 * for broadcasts. This parameter can be set 22645 * via ndd, so make sure that for the SO_DONTROUTE 22646 * case that ipha_ttl is always set to 1. 22647 * In the event that we are replying to incoming 22648 * ICMP packets, conn could be NULL. 22649 */ 22650 if ((connp != NULL) && connp->conn_dontroute) 22651 ipha->ipha_ttl = 1; 22652 else 22653 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl; 22654 22655 /* 22656 * Note that we are not doing a IRB_REFHOLD here. 22657 * Actually we don't care if the list changes i.e 22658 * if somebody deletes an IRE from the list while 22659 * we drop the lock, the next time we come around 22660 * ire_next will be NULL and hence we won't send 22661 * out multiple copies which is fine. 22662 */ 22663 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 22664 ire1 = ire->ire_next; 22665 if (conn_outgoing_ill != NULL) { 22666 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 22667 ASSERT(ire1 == ire->ire_next); 22668 if (ire1 != NULL && ire1->ire_addr == dst) { 22669 ire_refrele(ire); 22670 ire = ire1; 22671 IRE_REFHOLD(ire); 22672 ire1 = ire->ire_next; 22673 continue; 22674 } 22675 rw_exit(&ire->ire_bucket->irb_lock); 22676 /* Did not find a matching ill */ 22677 ip1dbg(("ip_wput_ire: broadcast with no " 22678 "matching IP_BOUND_IF ill %s\n", 22679 conn_outgoing_ill->ill_name)); 22680 freemsg(first_mp); 22681 if (ire != NULL) 22682 ire_refrele(ire); 22683 ill_refrele(conn_outgoing_ill); 22684 return; 22685 } 22686 } else if (ire1 != NULL && ire1->ire_addr == dst) { 22687 /* 22688 * If the next IRE has the same address and is not one 22689 * of the two copies that we need to send, try to see 22690 * whether this copy should be sent at all. This 22691 * assumes that we insert loopbacks first and then 22692 * non-loopbacks. This is acheived by inserting the 22693 * loopback always before non-loopback. 22694 * This is used to send a single copy of a broadcast 22695 * packet out all physical interfaces that have an 22696 * matching IRE_BROADCAST while also looping 22697 * back one copy (to ip_wput_local) for each 22698 * matching physical interface. However, we avoid 22699 * sending packets out different logical that match by 22700 * having ipif_up/ipif_down supress duplicate 22701 * IRE_BROADCASTS. 22702 * 22703 * This feature is currently used to get broadcasts 22704 * sent to multiple interfaces, when the broadcast 22705 * address being used applies to multiple interfaces. 22706 * For example, a whole net broadcast will be 22707 * replicated on every connected subnet of 22708 * the target net. 22709 * 22710 * Each zone has its own set of IRE_BROADCASTs, so that 22711 * we're able to distribute inbound packets to multiple 22712 * zones who share a broadcast address. We avoid looping 22713 * back outbound packets in different zones but on the 22714 * same ill, as the application would see duplicates. 22715 * 22716 * If the interfaces are part of the same group, 22717 * we would want to send only one copy out for 22718 * whole group. 22719 * 22720 * This logic assumes that ire_add_v4() groups the 22721 * IRE_BROADCAST entries so that those with the same 22722 * ire_addr and ill_group are kept together. 22723 */ 22724 ire_ill = ire->ire_ipif->ipif_ill; 22725 if (ire->ire_stq == NULL && ire1->ire_stq != NULL) { 22726 if (ire_ill->ill_group != NULL && 22727 (ire->ire_marks & IRE_MARK_NORECV)) { 22728 /* 22729 * If the current zone only has an ire 22730 * broadcast for this address marked 22731 * NORECV, the ire we want is ahead in 22732 * the bucket, so we look it up 22733 * deliberately ignoring the zoneid. 22734 */ 22735 for (ire1 = ire->ire_bucket->irb_ire; 22736 ire1 != NULL; 22737 ire1 = ire1->ire_next) { 22738 ire1_ill = 22739 ire1->ire_ipif->ipif_ill; 22740 if (ire1->ire_addr != dst) 22741 continue; 22742 /* skip over the current ire */ 22743 if (ire1 == ire) 22744 continue; 22745 /* skip over deleted ires */ 22746 if (ire1->ire_marks & 22747 IRE_MARK_CONDEMNED) 22748 continue; 22749 /* 22750 * non-loopback ire in our 22751 * group: use it for the next 22752 * pass in the loop 22753 */ 22754 if (ire1->ire_stq != NULL && 22755 ire1_ill->ill_group == 22756 ire_ill->ill_group) 22757 break; 22758 } 22759 } 22760 } else { 22761 while (ire1 != NULL && ire1->ire_addr == dst) { 22762 ire1_ill = ire1->ire_ipif->ipif_ill; 22763 /* 22764 * We can have two broadcast ires on the 22765 * same ill in different zones; here 22766 * we'll send a copy of the packet on 22767 * each ill and the fanout code will 22768 * call conn_wantpacket() to check that 22769 * the zone has the broadcast address 22770 * configured on the ill. If the two 22771 * ires are in the same group we only 22772 * send one copy up. 22773 */ 22774 if (ire1_ill != ire_ill && 22775 (ire1_ill->ill_group == NULL || 22776 ire_ill->ill_group == NULL || 22777 ire1_ill->ill_group != 22778 ire_ill->ill_group)) { 22779 break; 22780 } 22781 ire1 = ire1->ire_next; 22782 } 22783 } 22784 } 22785 ASSERT(multirt_send == B_FALSE); 22786 if (ire1 != NULL && ire1->ire_addr == dst) { 22787 if ((ire->ire_flags & RTF_MULTIRT) && 22788 (ire1->ire_flags & RTF_MULTIRT)) { 22789 /* 22790 * We are in the multirouting case. 22791 * The message must be sent at least 22792 * on both ires. These ires have been 22793 * inserted AFTER the standard ones 22794 * in ip_rt_add(). There are thus no 22795 * other ire entries for the destination 22796 * address in the rest of the bucket 22797 * that do not have the RTF_MULTIRT 22798 * flag. We don't process a copy 22799 * of the message here. This will be 22800 * done in the final sending loop. 22801 */ 22802 multirt_send = B_TRUE; 22803 } else { 22804 next_mp = ip_copymsg(first_mp); 22805 if (next_mp != NULL) 22806 IRE_REFHOLD(ire1); 22807 } 22808 } 22809 rw_exit(&ire->ire_bucket->irb_lock); 22810 } 22811 22812 if (stq) { 22813 /* 22814 * A non-NULL send-to queue means this packet is going 22815 * out of this machine. 22816 */ 22817 out_ill = (ill_t *)stq->q_ptr; 22818 22819 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutRequests); 22820 ttl_protocol = ((uint16_t *)ipha)[4]; 22821 /* 22822 * We accumulate the pseudo header checksum in cksum. 22823 * This is pretty hairy code, so watch close. One 22824 * thing to keep in mind is that UDP and TCP have 22825 * stored their respective datagram lengths in their 22826 * checksum fields. This lines things up real nice. 22827 */ 22828 cksum = (dst >> 16) + (dst & 0xFFFF) + 22829 (src >> 16) + (src & 0xFFFF); 22830 /* 22831 * We assume the udp checksum field contains the 22832 * length, so to compute the pseudo header checksum, 22833 * all we need is the protocol number and src/dst. 22834 */ 22835 /* Provide the checksums for UDP and TCP. */ 22836 if ((PROTO == IPPROTO_TCP) && 22837 (ip_hdr_included != IP_HDR_INCLUDED)) { 22838 /* hlen gets the number of uchar_ts in the IP header */ 22839 hlen = (V_HLEN & 0xF) << 2; 22840 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22841 IP_STAT(ipst, ip_out_sw_cksum); 22842 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22843 LENGTH - hlen); 22844 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 22845 } else if (PROTO == IPPROTO_SCTP && 22846 (ip_hdr_included != IP_HDR_INCLUDED)) { 22847 sctp_hdr_t *sctph; 22848 22849 hlen = (V_HLEN & 0xF) << 2; 22850 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22851 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22852 sctph->sh_chksum = 0; 22853 #ifdef DEBUG 22854 if (!skip_sctp_cksum) 22855 #endif 22856 sctph->sh_chksum = sctp_cksum(mp, hlen); 22857 } else { 22858 queue_t *dev_q = stq->q_next; 22859 22860 if ((dev_q->q_next || dev_q->q_first) && 22861 !canput(dev_q)) { 22862 blocked: 22863 ipha->ipha_ident = ip_hdr_included; 22864 /* 22865 * If we don't have a conn to apply 22866 * backpressure, free the message. 22867 * In the ire_send path, we don't know 22868 * the position to requeue the packet. Rather 22869 * than reorder packets, we just drop this 22870 * packet. 22871 */ 22872 if (ipst->ips_ip_output_queue && 22873 connp != NULL && 22874 caller != IRE_SEND) { 22875 if (caller == IP_WSRV) { 22876 connp->conn_did_putbq = 1; 22877 (void) putbq(connp->conn_wq, 22878 first_mp); 22879 conn_drain_insert(connp); 22880 /* 22881 * This is the service thread, 22882 * and the queue is already 22883 * noenabled. The check for 22884 * canput and the putbq is not 22885 * atomic. So we need to check 22886 * again. 22887 */ 22888 if (canput(stq->q_next)) 22889 connp->conn_did_putbq 22890 = 0; 22891 IP_STAT(ipst, ip_conn_flputbq); 22892 } else { 22893 /* 22894 * We are not the service proc. 22895 * ip_wsrv will be scheduled or 22896 * is already running. 22897 */ 22898 (void) putq(connp->conn_wq, 22899 first_mp); 22900 } 22901 } else { 22902 out_ill = (ill_t *)stq->q_ptr; 22903 BUMP_MIB(out_ill->ill_ip_mib, 22904 ipIfStatsOutDiscards); 22905 freemsg(first_mp); 22906 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22907 "ip_wput_ire_end: q %p (%S)", 22908 q, "discard"); 22909 } 22910 ire_refrele(ire); 22911 if (next_mp) { 22912 ire_refrele(ire1); 22913 freemsg(next_mp); 22914 } 22915 if (conn_outgoing_ill != NULL) 22916 ill_refrele(conn_outgoing_ill); 22917 return; 22918 } 22919 if ((PROTO == IPPROTO_UDP) && 22920 (ip_hdr_included != IP_HDR_INCLUDED)) { 22921 /* 22922 * hlen gets the number of uchar_ts in the 22923 * IP header 22924 */ 22925 hlen = (V_HLEN & 0xF) << 2; 22926 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22927 max_frag = ire->ire_max_frag; 22928 if (*up != 0) { 22929 IP_CKSUM_XMIT(ire_ill, ire, mp, ipha, 22930 up, PROTO, hlen, LENGTH, max_frag, 22931 ipsec_len, cksum); 22932 /* Software checksum? */ 22933 if (DB_CKSUMFLAGS(mp) == 0) { 22934 IP_STAT(ipst, ip_out_sw_cksum); 22935 IP_STAT_UPDATE(ipst, 22936 ip_udp_out_sw_cksum_bytes, 22937 LENGTH - hlen); 22938 } 22939 } 22940 } 22941 } 22942 /* 22943 * Need to do this even when fragmenting. The local 22944 * loopback can be done without computing checksums 22945 * but forwarding out other interface must be done 22946 * after the IP checksum (and ULP checksums) have been 22947 * computed. 22948 * 22949 * NOTE : multicast_forward is set only if this packet 22950 * originated from ip_wput. For packets originating from 22951 * ip_wput_multicast, it is not set. 22952 */ 22953 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 22954 multi_loopback: 22955 ip2dbg(("ip_wput: multicast, loop %d\n", 22956 conn_multicast_loop)); 22957 22958 /* Forget header checksum offload */ 22959 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 22960 22961 /* 22962 * Local loopback of multicasts? Check the 22963 * ill. 22964 * 22965 * Note that the loopback function will not come 22966 * in through ip_rput - it will only do the 22967 * client fanout thus we need to do an mforward 22968 * as well. The is different from the BSD 22969 * logic. 22970 */ 22971 if (ill != NULL) { 22972 ilm_t *ilm; 22973 22974 ILM_WALKER_HOLD(ill); 22975 ilm = ilm_lookup_ill(ill, ipha->ipha_dst, 22976 ALL_ZONES); 22977 ILM_WALKER_RELE(ill); 22978 if (ilm != NULL) { 22979 /* 22980 * Pass along the virtual output q. 22981 * ip_wput_local() will distribute the 22982 * packet to all the matching zones, 22983 * except the sending zone when 22984 * IP_MULTICAST_LOOP is false. 22985 */ 22986 ip_multicast_loopback(q, ill, first_mp, 22987 conn_multicast_loop ? 0 : 22988 IP_FF_NO_MCAST_LOOP, zoneid); 22989 } 22990 } 22991 if (ipha->ipha_ttl == 0) { 22992 /* 22993 * 0 => only to this host i.e. we are 22994 * done. We are also done if this was the 22995 * loopback interface since it is sufficient 22996 * to loopback one copy of a multicast packet. 22997 */ 22998 freemsg(first_mp); 22999 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23000 "ip_wput_ire_end: q %p (%S)", 23001 q, "loopback"); 23002 ire_refrele(ire); 23003 if (conn_outgoing_ill != NULL) 23004 ill_refrele(conn_outgoing_ill); 23005 return; 23006 } 23007 /* 23008 * ILLF_MULTICAST is checked in ip_newroute 23009 * i.e. we don't need to check it here since 23010 * all IRE_CACHEs come from ip_newroute. 23011 * For multicast traffic, SO_DONTROUTE is interpreted 23012 * to mean only send the packet out the interface 23013 * (optionally specified with IP_MULTICAST_IF) 23014 * and do not forward it out additional interfaces. 23015 * RSVP and the rsvp daemon is an example of a 23016 * protocol and user level process that 23017 * handles it's own routing. Hence, it uses the 23018 * SO_DONTROUTE option to accomplish this. 23019 */ 23020 23021 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 23022 ill != NULL) { 23023 /* Unconditionally redo the checksum */ 23024 ipha->ipha_hdr_checksum = 0; 23025 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23026 23027 /* 23028 * If this needs to go out secure, we need 23029 * to wait till we finish the IPsec 23030 * processing. 23031 */ 23032 if (ipsec_len == 0 && 23033 ip_mforward(ill, ipha, mp)) { 23034 freemsg(first_mp); 23035 ip1dbg(("ip_wput: mforward failed\n")); 23036 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23037 "ip_wput_ire_end: q %p (%S)", 23038 q, "mforward failed"); 23039 ire_refrele(ire); 23040 if (conn_outgoing_ill != NULL) 23041 ill_refrele(conn_outgoing_ill); 23042 return; 23043 } 23044 } 23045 } 23046 max_frag = ire->ire_max_frag; 23047 cksum += ttl_protocol; 23048 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 23049 /* No fragmentation required for this one. */ 23050 /* 23051 * Don't use frag_flag if packet is pre-built or source 23052 * routed or if multicast (since multicast packets do 23053 * not solicit ICMP "packet too big" messages). 23054 */ 23055 if ((ip_hdr_included != IP_HDR_INCLUDED) && 23056 (V_HLEN == IP_SIMPLE_HDR_VERSION || 23057 !ip_source_route_included(ipha)) && 23058 !CLASSD(ipha->ipha_dst)) 23059 ipha->ipha_fragment_offset_and_flags |= 23060 htons(ire->ire_frag_flag); 23061 23062 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 23063 /* Complete the IP header checksum. */ 23064 cksum += ipha->ipha_ident; 23065 cksum += (v_hlen_tos_len >> 16)+ 23066 (v_hlen_tos_len & 0xFFFF); 23067 cksum += ipha->ipha_fragment_offset_and_flags; 23068 hlen = (V_HLEN & 0xF) - 23069 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 23070 if (hlen) { 23071 checksumoptions: 23072 /* 23073 * Account for the IP Options in the IP 23074 * header checksum. 23075 */ 23076 up = (uint16_t *)(rptr+ 23077 IP_SIMPLE_HDR_LENGTH); 23078 do { 23079 cksum += up[0]; 23080 cksum += up[1]; 23081 up += 2; 23082 } while (--hlen); 23083 } 23084 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 23085 cksum = ~(cksum + (cksum >> 16)); 23086 ipha->ipha_hdr_checksum = (uint16_t)cksum; 23087 } 23088 if (ipsec_len != 0) { 23089 ipsec_out_process(q, first_mp, ire, ill_index); 23090 if (!next_mp) { 23091 ire_refrele(ire); 23092 if (conn_outgoing_ill != NULL) 23093 ill_refrele(conn_outgoing_ill); 23094 return; 23095 } 23096 goto next; 23097 } 23098 23099 /* 23100 * multirt_send has already been handled 23101 * for broadcast, but not yet for multicast 23102 * or IP options. 23103 */ 23104 if (next_mp == NULL) { 23105 if (ire->ire_flags & RTF_MULTIRT) { 23106 multirt_send = B_TRUE; 23107 } 23108 } 23109 23110 /* 23111 * In most cases, the emission loop below is 23112 * entered only once. Only in the case where 23113 * the ire holds the RTF_MULTIRT flag, do we loop 23114 * to process all RTF_MULTIRT ires in the bucket, 23115 * and send the packet through all crossed 23116 * RTF_MULTIRT routes. 23117 */ 23118 do { 23119 if (multirt_send) { 23120 irb_t *irb; 23121 23122 irb = ire->ire_bucket; 23123 ASSERT(irb != NULL); 23124 /* 23125 * We are in a multiple send case, 23126 * need to get the next IRE and make 23127 * a duplicate of the packet. 23128 */ 23129 IRB_REFHOLD(irb); 23130 for (ire1 = ire->ire_next; 23131 ire1 != NULL; 23132 ire1 = ire1->ire_next) { 23133 if (!(ire1->ire_flags & 23134 RTF_MULTIRT)) { 23135 continue; 23136 } 23137 if (ire1->ire_addr != 23138 ire->ire_addr) { 23139 continue; 23140 } 23141 if (ire1->ire_marks & 23142 (IRE_MARK_CONDEMNED| 23143 IRE_MARK_HIDDEN)) { 23144 continue; 23145 } 23146 23147 /* Got one */ 23148 IRE_REFHOLD(ire1); 23149 break; 23150 } 23151 IRB_REFRELE(irb); 23152 23153 if (ire1 != NULL) { 23154 next_mp = copyb(mp); 23155 if ((next_mp == NULL) || 23156 ((mp->b_cont != NULL) && 23157 ((next_mp->b_cont = 23158 dupmsg(mp->b_cont)) 23159 == NULL))) { 23160 freemsg(next_mp); 23161 next_mp = NULL; 23162 ire_refrele(ire1); 23163 ire1 = NULL; 23164 } 23165 } 23166 23167 /* 23168 * Last multiroute ire; don't loop 23169 * anymore. The emission is over 23170 * and next_mp is NULL. 23171 */ 23172 if (ire1 == NULL) { 23173 multirt_send = B_FALSE; 23174 } 23175 } 23176 23177 out_ill = ire_to_ill(ire); 23178 DTRACE_PROBE4(ip4__physical__out__start, 23179 ill_t *, NULL, 23180 ill_t *, out_ill, 23181 ipha_t *, ipha, mblk_t *, mp); 23182 FW_HOOKS(ipst->ips_ip4_physical_out_event, 23183 ipst->ips_ipv4firewall_physical_out, 23184 NULL, out_ill, ipha, mp, mp, ipst); 23185 DTRACE_PROBE1(ip4__physical__out__end, 23186 mblk_t *, mp); 23187 if (mp == NULL) 23188 goto release_ire_and_ill_2; 23189 23190 ASSERT(ipsec_len == 0); 23191 mp->b_prev = 23192 SET_BPREV_FLAG(IPP_LOCAL_OUT); 23193 DTRACE_PROBE2(ip__xmit__2, 23194 mblk_t *, mp, ire_t *, ire); 23195 pktxmit_state = ip_xmit_v4(mp, ire, 23196 NULL, B_TRUE); 23197 if ((pktxmit_state == SEND_FAILED) || 23198 (pktxmit_state == LLHDR_RESLV_FAILED)) { 23199 release_ire_and_ill_2: 23200 if (next_mp) { 23201 freemsg(next_mp); 23202 ire_refrele(ire1); 23203 } 23204 ire_refrele(ire); 23205 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23206 "ip_wput_ire_end: q %p (%S)", 23207 q, "discard MDATA"); 23208 if (conn_outgoing_ill != NULL) 23209 ill_refrele(conn_outgoing_ill); 23210 return; 23211 } 23212 23213 if (CLASSD(dst)) { 23214 BUMP_MIB(out_ill->ill_ip_mib, 23215 ipIfStatsHCOutMcastPkts); 23216 UPDATE_MIB(out_ill->ill_ip_mib, 23217 ipIfStatsHCOutMcastOctets, 23218 LENGTH); 23219 } else if (ire->ire_type == IRE_BROADCAST) { 23220 BUMP_MIB(out_ill->ill_ip_mib, 23221 ipIfStatsHCOutBcastPkts); 23222 } 23223 23224 if (multirt_send) { 23225 /* 23226 * We are in a multiple send case, 23227 * need to re-enter the sending loop 23228 * using the next ire. 23229 */ 23230 ire_refrele(ire); 23231 ire = ire1; 23232 stq = ire->ire_stq; 23233 mp = next_mp; 23234 next_mp = NULL; 23235 ipha = (ipha_t *)mp->b_rptr; 23236 ill_index = Q_TO_INDEX(stq); 23237 } 23238 } while (multirt_send); 23239 23240 if (!next_mp) { 23241 /* 23242 * Last copy going out (the ultra-common 23243 * case). Note that we intentionally replicate 23244 * the putnext rather than calling it before 23245 * the next_mp check in hopes of a little 23246 * tail-call action out of the compiler. 23247 */ 23248 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23249 "ip_wput_ire_end: q %p (%S)", 23250 q, "last copy out(1)"); 23251 ire_refrele(ire); 23252 if (conn_outgoing_ill != NULL) 23253 ill_refrele(conn_outgoing_ill); 23254 return; 23255 } 23256 /* More copies going out below. */ 23257 } else { 23258 int offset; 23259 fragmentit: 23260 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23261 /* 23262 * If this would generate a icmp_frag_needed message, 23263 * we need to handle it before we do the IPsec 23264 * processing. Otherwise, we need to strip the IPsec 23265 * headers before we send up the message to the ULPs 23266 * which becomes messy and difficult. 23267 */ 23268 if (ipsec_len != 0) { 23269 if ((max_frag < (unsigned int)(LENGTH + 23270 ipsec_len)) && (offset & IPH_DF)) { 23271 out_ill = (ill_t *)stq->q_ptr; 23272 BUMP_MIB(out_ill->ill_ip_mib, 23273 ipIfStatsOutFragFails); 23274 BUMP_MIB(out_ill->ill_ip_mib, 23275 ipIfStatsOutFragReqds); 23276 ipha->ipha_hdr_checksum = 0; 23277 ipha->ipha_hdr_checksum = 23278 (uint16_t)ip_csum_hdr(ipha); 23279 icmp_frag_needed(ire->ire_stq, first_mp, 23280 max_frag, zoneid, ipst); 23281 if (!next_mp) { 23282 ire_refrele(ire); 23283 if (conn_outgoing_ill != NULL) { 23284 ill_refrele( 23285 conn_outgoing_ill); 23286 } 23287 return; 23288 } 23289 } else { 23290 /* 23291 * This won't cause a icmp_frag_needed 23292 * message. to be generated. Send it on 23293 * the wire. Note that this could still 23294 * cause fragmentation and all we 23295 * do is the generation of the message 23296 * to the ULP if needed before IPsec. 23297 */ 23298 if (!next_mp) { 23299 ipsec_out_process(q, first_mp, 23300 ire, ill_index); 23301 TRACE_2(TR_FAC_IP, 23302 TR_IP_WPUT_IRE_END, 23303 "ip_wput_ire_end: q %p " 23304 "(%S)", q, 23305 "last ipsec_out_process"); 23306 ire_refrele(ire); 23307 if (conn_outgoing_ill != NULL) { 23308 ill_refrele( 23309 conn_outgoing_ill); 23310 } 23311 return; 23312 } 23313 ipsec_out_process(q, first_mp, 23314 ire, ill_index); 23315 } 23316 } else { 23317 /* 23318 * Initiate IPPF processing. For 23319 * fragmentable packets we finish 23320 * all QOS packet processing before 23321 * calling: 23322 * ip_wput_ire_fragmentit->ip_wput_frag 23323 */ 23324 23325 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23326 ip_process(IPP_LOCAL_OUT, &mp, 23327 ill_index); 23328 if (mp == NULL) { 23329 out_ill = (ill_t *)stq->q_ptr; 23330 BUMP_MIB(out_ill->ill_ip_mib, 23331 ipIfStatsOutDiscards); 23332 if (next_mp != NULL) { 23333 freemsg(next_mp); 23334 ire_refrele(ire1); 23335 } 23336 ire_refrele(ire); 23337 TRACE_2(TR_FAC_IP, 23338 TR_IP_WPUT_IRE_END, 23339 "ip_wput_ire: q %p (%S)", 23340 q, "discard MDATA"); 23341 if (conn_outgoing_ill != NULL) { 23342 ill_refrele( 23343 conn_outgoing_ill); 23344 } 23345 return; 23346 } 23347 } 23348 if (!next_mp) { 23349 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23350 "ip_wput_ire_end: q %p (%S)", 23351 q, "last fragmentation"); 23352 ip_wput_ire_fragmentit(mp, ire, 23353 zoneid, ipst); 23354 ire_refrele(ire); 23355 if (conn_outgoing_ill != NULL) 23356 ill_refrele(conn_outgoing_ill); 23357 return; 23358 } 23359 ip_wput_ire_fragmentit(mp, ire, zoneid, ipst); 23360 } 23361 } 23362 } else { 23363 nullstq: 23364 /* A NULL stq means the destination address is local. */ 23365 UPDATE_OB_PKT_COUNT(ire); 23366 ire->ire_last_used_time = lbolt; 23367 ASSERT(ire->ire_ipif != NULL); 23368 if (!next_mp) { 23369 /* 23370 * Is there an "in" and "out" for traffic local 23371 * to a host (loopback)? The code in Solaris doesn't 23372 * explicitly draw a line in its code for in vs out, 23373 * so we've had to draw a line in the sand: ip_wput_ire 23374 * is considered to be the "output" side and 23375 * ip_wput_local to be the "input" side. 23376 */ 23377 out_ill = ire_to_ill(ire); 23378 23379 DTRACE_PROBE4(ip4__loopback__out__start, 23380 ill_t *, NULL, ill_t *, out_ill, 23381 ipha_t *, ipha, mblk_t *, first_mp); 23382 23383 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23384 ipst->ips_ipv4firewall_loopback_out, 23385 NULL, out_ill, ipha, first_mp, mp, ipst); 23386 23387 DTRACE_PROBE1(ip4__loopback__out_end, 23388 mblk_t *, first_mp); 23389 23390 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23391 "ip_wput_ire_end: q %p (%S)", 23392 q, "local address"); 23393 23394 if (first_mp != NULL) 23395 ip_wput_local(q, out_ill, ipha, 23396 first_mp, ire, 0, ire->ire_zoneid); 23397 ire_refrele(ire); 23398 if (conn_outgoing_ill != NULL) 23399 ill_refrele(conn_outgoing_ill); 23400 return; 23401 } 23402 23403 out_ill = ire_to_ill(ire); 23404 23405 DTRACE_PROBE4(ip4__loopback__out__start, 23406 ill_t *, NULL, ill_t *, out_ill, 23407 ipha_t *, ipha, mblk_t *, first_mp); 23408 23409 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23410 ipst->ips_ipv4firewall_loopback_out, 23411 NULL, out_ill, ipha, first_mp, mp, ipst); 23412 23413 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, first_mp); 23414 23415 if (first_mp != NULL) 23416 ip_wput_local(q, out_ill, ipha, 23417 first_mp, ire, 0, ire->ire_zoneid); 23418 } 23419 next: 23420 /* 23421 * More copies going out to additional interfaces. 23422 * ire1 has already been held. We don't need the 23423 * "ire" anymore. 23424 */ 23425 ire_refrele(ire); 23426 ire = ire1; 23427 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 23428 mp = next_mp; 23429 ASSERT(ire->ire_ipversion == IPV4_VERSION); 23430 ill = ire_to_ill(ire); 23431 first_mp = mp; 23432 if (ipsec_len != 0) { 23433 ASSERT(first_mp->b_datap->db_type == M_CTL); 23434 mp = mp->b_cont; 23435 } 23436 dst = ire->ire_addr; 23437 ipha = (ipha_t *)mp->b_rptr; 23438 /* 23439 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 23440 * Restore ipha_ident "no checksum" flag. 23441 */ 23442 src = orig_src; 23443 ipha->ipha_ident = ip_hdr_included; 23444 goto another; 23445 23446 #undef rptr 23447 #undef Q_TO_INDEX 23448 } 23449 23450 /* 23451 * Routine to allocate a message that is used to notify the ULP about MDT. 23452 * The caller may provide a pointer to the link-layer MDT capabilities, 23453 * or NULL if MDT is to be disabled on the stream. 23454 */ 23455 mblk_t * 23456 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 23457 { 23458 mblk_t *mp; 23459 ip_mdt_info_t *mdti; 23460 ill_mdt_capab_t *idst; 23461 23462 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 23463 DB_TYPE(mp) = M_CTL; 23464 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 23465 mdti = (ip_mdt_info_t *)mp->b_rptr; 23466 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 23467 idst = &(mdti->mdt_capab); 23468 23469 /* 23470 * If the caller provides us with the capability, copy 23471 * it over into our notification message; otherwise 23472 * we zero out the capability portion. 23473 */ 23474 if (isrc != NULL) 23475 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23476 else 23477 bzero((caddr_t)idst, sizeof (*idst)); 23478 } 23479 return (mp); 23480 } 23481 23482 /* 23483 * Routine which determines whether MDT can be enabled on the destination 23484 * IRE and IPC combination, and if so, allocates and returns the MDT 23485 * notification mblk that may be used by ULP. We also check if we need to 23486 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 23487 * MDT usage in the past have been lifted. This gets called during IP 23488 * and ULP binding. 23489 */ 23490 mblk_t * 23491 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23492 ill_mdt_capab_t *mdt_cap) 23493 { 23494 mblk_t *mp; 23495 boolean_t rc = B_FALSE; 23496 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23497 23498 ASSERT(dst_ire != NULL); 23499 ASSERT(connp != NULL); 23500 ASSERT(mdt_cap != NULL); 23501 23502 /* 23503 * Currently, we only support simple TCP/{IPv4,IPv6} with 23504 * Multidata, which is handled in tcp_multisend(). This 23505 * is the reason why we do all these checks here, to ensure 23506 * that we don't enable Multidata for the cases which we 23507 * can't handle at the moment. 23508 */ 23509 do { 23510 /* Only do TCP at the moment */ 23511 if (connp->conn_ulp != IPPROTO_TCP) 23512 break; 23513 23514 /* 23515 * IPsec outbound policy present? Note that we get here 23516 * after calling ipsec_conn_cache_policy() where the global 23517 * policy checking is performed. conn_latch will be 23518 * non-NULL as long as there's a policy defined, 23519 * i.e. conn_out_enforce_policy may be NULL in such case 23520 * when the connection is non-secure, and hence we check 23521 * further if the latch refers to an outbound policy. 23522 */ 23523 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 23524 break; 23525 23526 /* CGTP (multiroute) is enabled? */ 23527 if (dst_ire->ire_flags & RTF_MULTIRT) 23528 break; 23529 23530 /* Outbound IPQoS enabled? */ 23531 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23532 /* 23533 * In this case, we disable MDT for this and all 23534 * future connections going over the interface. 23535 */ 23536 mdt_cap->ill_mdt_on = 0; 23537 break; 23538 } 23539 23540 /* socket option(s) present? */ 23541 if (!CONN_IS_LSO_MD_FASTPATH(connp)) 23542 break; 23543 23544 rc = B_TRUE; 23545 /* CONSTCOND */ 23546 } while (0); 23547 23548 /* Remember the result */ 23549 connp->conn_mdt_ok = rc; 23550 23551 if (!rc) 23552 return (NULL); 23553 else if (!mdt_cap->ill_mdt_on) { 23554 /* 23555 * If MDT has been previously turned off in the past, and we 23556 * currently can do MDT (due to IPQoS policy removal, etc.) 23557 * then enable it for this interface. 23558 */ 23559 mdt_cap->ill_mdt_on = 1; 23560 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 23561 "interface %s\n", ill_name)); 23562 } 23563 23564 /* Allocate the MDT info mblk */ 23565 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 23566 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 23567 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23568 return (NULL); 23569 } 23570 return (mp); 23571 } 23572 23573 /* 23574 * Routine to allocate a message that is used to notify the ULP about LSO. 23575 * The caller may provide a pointer to the link-layer LSO capabilities, 23576 * or NULL if LSO is to be disabled on the stream. 23577 */ 23578 mblk_t * 23579 ip_lsoinfo_alloc(ill_lso_capab_t *isrc) 23580 { 23581 mblk_t *mp; 23582 ip_lso_info_t *lsoi; 23583 ill_lso_capab_t *idst; 23584 23585 if ((mp = allocb(sizeof (*lsoi), BPRI_HI)) != NULL) { 23586 DB_TYPE(mp) = M_CTL; 23587 mp->b_wptr = mp->b_rptr + sizeof (*lsoi); 23588 lsoi = (ip_lso_info_t *)mp->b_rptr; 23589 lsoi->lso_info_id = LSO_IOC_INFO_UPDATE; 23590 idst = &(lsoi->lso_capab); 23591 23592 /* 23593 * If the caller provides us with the capability, copy 23594 * it over into our notification message; otherwise 23595 * we zero out the capability portion. 23596 */ 23597 if (isrc != NULL) 23598 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23599 else 23600 bzero((caddr_t)idst, sizeof (*idst)); 23601 } 23602 return (mp); 23603 } 23604 23605 /* 23606 * Routine which determines whether LSO can be enabled on the destination 23607 * IRE and IPC combination, and if so, allocates and returns the LSO 23608 * notification mblk that may be used by ULP. We also check if we need to 23609 * turn LSO back to 'on' when certain restrictions prohibiting us to allow 23610 * LSO usage in the past have been lifted. This gets called during IP 23611 * and ULP binding. 23612 */ 23613 mblk_t * 23614 ip_lsoinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23615 ill_lso_capab_t *lso_cap) 23616 { 23617 mblk_t *mp; 23618 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23619 23620 ASSERT(dst_ire != NULL); 23621 ASSERT(connp != NULL); 23622 ASSERT(lso_cap != NULL); 23623 23624 connp->conn_lso_ok = B_TRUE; 23625 23626 if ((connp->conn_ulp != IPPROTO_TCP) || 23627 CONN_IPSEC_OUT_ENCAPSULATED(connp) || 23628 (dst_ire->ire_flags & RTF_MULTIRT) || 23629 !CONN_IS_LSO_MD_FASTPATH(connp) || 23630 (IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 23631 connp->conn_lso_ok = B_FALSE; 23632 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23633 /* 23634 * Disable LSO for this and all future connections going 23635 * over the interface. 23636 */ 23637 lso_cap->ill_lso_on = 0; 23638 } 23639 } 23640 23641 if (!connp->conn_lso_ok) 23642 return (NULL); 23643 else if (!lso_cap->ill_lso_on) { 23644 /* 23645 * If LSO has been previously turned off in the past, and we 23646 * currently can do LSO (due to IPQoS policy removal, etc.) 23647 * then enable it for this interface. 23648 */ 23649 lso_cap->ill_lso_on = 1; 23650 ip1dbg(("ip_mdinfo_return: reenabling LSO for interface %s\n", 23651 ill_name)); 23652 } 23653 23654 /* Allocate the LSO info mblk */ 23655 if ((mp = ip_lsoinfo_alloc(lso_cap)) == NULL) 23656 ip0dbg(("ip_lsoinfo_return: can't enable LSO for " 23657 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23658 23659 return (mp); 23660 } 23661 23662 /* 23663 * Create destination address attribute, and fill it with the physical 23664 * destination address and SAP taken from the template DL_UNITDATA_REQ 23665 * message block. 23666 */ 23667 boolean_t 23668 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 23669 { 23670 dl_unitdata_req_t *dlurp; 23671 pattr_t *pa; 23672 pattrinfo_t pa_info; 23673 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 23674 uint_t das_len, das_off; 23675 23676 ASSERT(dlmp != NULL); 23677 23678 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 23679 das_len = dlurp->dl_dest_addr_length; 23680 das_off = dlurp->dl_dest_addr_offset; 23681 23682 pa_info.type = PATTR_DSTADDRSAP; 23683 pa_info.len = sizeof (**das) + das_len - 1; 23684 23685 /* create and associate the attribute */ 23686 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23687 if (pa != NULL) { 23688 ASSERT(*das != NULL); 23689 (*das)->addr_is_group = 0; 23690 (*das)->addr_len = (uint8_t)das_len; 23691 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 23692 } 23693 23694 return (pa != NULL); 23695 } 23696 23697 /* 23698 * Create hardware checksum attribute and fill it with the values passed. 23699 */ 23700 boolean_t 23701 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 23702 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 23703 { 23704 pattr_t *pa; 23705 pattrinfo_t pa_info; 23706 23707 ASSERT(mmd != NULL); 23708 23709 pa_info.type = PATTR_HCKSUM; 23710 pa_info.len = sizeof (pattr_hcksum_t); 23711 23712 /* create and associate the attribute */ 23713 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23714 if (pa != NULL) { 23715 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 23716 23717 hck->hcksum_start_offset = start_offset; 23718 hck->hcksum_stuff_offset = stuff_offset; 23719 hck->hcksum_end_offset = end_offset; 23720 hck->hcksum_flags = flags; 23721 } 23722 return (pa != NULL); 23723 } 23724 23725 /* 23726 * Create zerocopy attribute and fill it with the specified flags 23727 */ 23728 boolean_t 23729 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 23730 { 23731 pattr_t *pa; 23732 pattrinfo_t pa_info; 23733 23734 ASSERT(mmd != NULL); 23735 pa_info.type = PATTR_ZCOPY; 23736 pa_info.len = sizeof (pattr_zcopy_t); 23737 23738 /* create and associate the attribute */ 23739 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23740 if (pa != NULL) { 23741 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 23742 23743 zcopy->zcopy_flags = flags; 23744 } 23745 return (pa != NULL); 23746 } 23747 23748 /* 23749 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 23750 * block chain. We could rewrite to handle arbitrary message block chains but 23751 * that would make the code complicated and slow. Right now there three 23752 * restrictions: 23753 * 23754 * 1. The first message block must contain the complete IP header and 23755 * at least 1 byte of payload data. 23756 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 23757 * so that we can use a single Multidata message. 23758 * 3. No frag must be distributed over two or more message blocks so 23759 * that we don't need more than two packet descriptors per frag. 23760 * 23761 * The above restrictions allow us to support userland applications (which 23762 * will send down a single message block) and NFS over UDP (which will 23763 * send down a chain of at most three message blocks). 23764 * 23765 * We also don't use MDT for payloads with less than or equal to 23766 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 23767 */ 23768 boolean_t 23769 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 23770 { 23771 int blocks; 23772 ssize_t total, missing, size; 23773 23774 ASSERT(mp != NULL); 23775 ASSERT(hdr_len > 0); 23776 23777 size = MBLKL(mp) - hdr_len; 23778 if (size <= 0) 23779 return (B_FALSE); 23780 23781 /* The first mblk contains the header and some payload. */ 23782 blocks = 1; 23783 total = size; 23784 size %= len; 23785 missing = (size == 0) ? 0 : (len - size); 23786 mp = mp->b_cont; 23787 23788 while (mp != NULL) { 23789 /* 23790 * Give up if we encounter a zero length message block. 23791 * In practice, this should rarely happen and therefore 23792 * not worth the trouble of freeing and re-linking the 23793 * mblk from the chain to handle such case. 23794 */ 23795 if ((size = MBLKL(mp)) == 0) 23796 return (B_FALSE); 23797 23798 /* Too many payload buffers for a single Multidata message? */ 23799 if (++blocks > MULTIDATA_MAX_PBUFS) 23800 return (B_FALSE); 23801 23802 total += size; 23803 /* Is a frag distributed over two or more message blocks? */ 23804 if (missing > size) 23805 return (B_FALSE); 23806 size -= missing; 23807 23808 size %= len; 23809 missing = (size == 0) ? 0 : (len - size); 23810 23811 mp = mp->b_cont; 23812 } 23813 23814 return (total > ip_wput_frag_mdt_min); 23815 } 23816 23817 /* 23818 * Outbound IPv4 fragmentation routine using MDT. 23819 */ 23820 static void 23821 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 23822 uint32_t frag_flag, int offset) 23823 { 23824 ipha_t *ipha_orig; 23825 int i1, ip_data_end; 23826 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 23827 mblk_t *hdr_mp, *md_mp = NULL; 23828 unsigned char *hdr_ptr, *pld_ptr; 23829 multidata_t *mmd; 23830 ip_pdescinfo_t pdi; 23831 ill_t *ill; 23832 ip_stack_t *ipst = ire->ire_ipst; 23833 23834 ASSERT(DB_TYPE(mp) == M_DATA); 23835 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 23836 23837 ill = ire_to_ill(ire); 23838 ASSERT(ill != NULL); 23839 23840 ipha_orig = (ipha_t *)mp->b_rptr; 23841 mp->b_rptr += sizeof (ipha_t); 23842 23843 /* Calculate how many packets we will send out */ 23844 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 23845 pkts = (i1 + len - 1) / len; 23846 ASSERT(pkts > 1); 23847 23848 /* Allocate a message block which will hold all the IP Headers. */ 23849 wroff = ipst->ips_ip_wroff_extra; 23850 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 23851 23852 i1 = pkts * hdr_chunk_len; 23853 /* 23854 * Create the header buffer, Multidata and destination address 23855 * and SAP attribute that should be associated with it. 23856 */ 23857 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 23858 ((hdr_mp->b_wptr += i1), 23859 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 23860 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 23861 freemsg(mp); 23862 if (md_mp == NULL) { 23863 freemsg(hdr_mp); 23864 } else { 23865 free_mmd: IP_STAT(ipst, ip_frag_mdt_discarded); 23866 freemsg(md_mp); 23867 } 23868 IP_STAT(ipst, ip_frag_mdt_allocfail); 23869 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 23870 return; 23871 } 23872 IP_STAT(ipst, ip_frag_mdt_allocd); 23873 23874 /* 23875 * Add a payload buffer to the Multidata; this operation must not 23876 * fail, or otherwise our logic in this routine is broken. There 23877 * is no memory allocation done by the routine, so any returned 23878 * failure simply tells us that we've done something wrong. 23879 * 23880 * A failure tells us that either we're adding the same payload 23881 * buffer more than once, or we're trying to add more buffers than 23882 * allowed. None of the above cases should happen, and we panic 23883 * because either there's horrible heap corruption, and/or 23884 * programming mistake. 23885 */ 23886 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23887 goto pbuf_panic; 23888 23889 hdr_ptr = hdr_mp->b_rptr; 23890 pld_ptr = mp->b_rptr; 23891 23892 /* Establish the ending byte offset, based on the starting offset. */ 23893 offset <<= 3; 23894 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 23895 IP_SIMPLE_HDR_LENGTH; 23896 23897 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 23898 23899 while (pld_ptr < mp->b_wptr) { 23900 ipha_t *ipha; 23901 uint16_t offset_and_flags; 23902 uint16_t ip_len; 23903 int error; 23904 23905 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 23906 ipha = (ipha_t *)(hdr_ptr + wroff); 23907 ASSERT(OK_32PTR(ipha)); 23908 *ipha = *ipha_orig; 23909 23910 if (ip_data_end - offset > len) { 23911 offset_and_flags = IPH_MF; 23912 } else { 23913 /* 23914 * Last frag. Set len to the length of this last piece. 23915 */ 23916 len = ip_data_end - offset; 23917 /* A frag of a frag might have IPH_MF non-zero */ 23918 offset_and_flags = 23919 ntohs(ipha->ipha_fragment_offset_and_flags) & 23920 IPH_MF; 23921 } 23922 offset_and_flags |= (uint16_t)(offset >> 3); 23923 offset_and_flags |= (uint16_t)frag_flag; 23924 /* Store the offset and flags in the IP header. */ 23925 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 23926 23927 /* Store the length in the IP header. */ 23928 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 23929 ipha->ipha_length = htons(ip_len); 23930 23931 /* 23932 * Set the IP header checksum. Note that mp is just 23933 * the header, so this is easy to pass to ip_csum. 23934 */ 23935 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23936 23937 /* 23938 * Record offset and size of header and data of the next packet 23939 * in the multidata message. 23940 */ 23941 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 23942 PDESC_PLD_INIT(&pdi); 23943 i1 = MIN(mp->b_wptr - pld_ptr, len); 23944 ASSERT(i1 > 0); 23945 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 23946 if (i1 == len) { 23947 pld_ptr += len; 23948 } else { 23949 i1 = len - i1; 23950 mp = mp->b_cont; 23951 ASSERT(mp != NULL); 23952 ASSERT(MBLKL(mp) >= i1); 23953 /* 23954 * Attach the next payload message block to the 23955 * multidata message. 23956 */ 23957 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23958 goto pbuf_panic; 23959 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 23960 pld_ptr = mp->b_rptr + i1; 23961 } 23962 23963 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 23964 KM_NOSLEEP)) == NULL) { 23965 /* 23966 * Any failure other than ENOMEM indicates that we 23967 * have passed in invalid pdesc info or parameters 23968 * to mmd_addpdesc, which must not happen. 23969 * 23970 * EINVAL is a result of failure on boundary checks 23971 * against the pdesc info contents. It should not 23972 * happen, and we panic because either there's 23973 * horrible heap corruption, and/or programming 23974 * mistake. 23975 */ 23976 if (error != ENOMEM) { 23977 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 23978 "pdesc logic error detected for " 23979 "mmd %p pinfo %p (%d)\n", 23980 (void *)mmd, (void *)&pdi, error); 23981 /* NOTREACHED */ 23982 } 23983 IP_STAT(ipst, ip_frag_mdt_addpdescfail); 23984 /* Free unattached payload message blocks as well */ 23985 md_mp->b_cont = mp->b_cont; 23986 goto free_mmd; 23987 } 23988 23989 /* Advance fragment offset. */ 23990 offset += len; 23991 23992 /* Advance to location for next header in the buffer. */ 23993 hdr_ptr += hdr_chunk_len; 23994 23995 /* Did we reach the next payload message block? */ 23996 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 23997 mp = mp->b_cont; 23998 /* 23999 * Attach the next message block with payload 24000 * data to the multidata message. 24001 */ 24002 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24003 goto pbuf_panic; 24004 pld_ptr = mp->b_rptr; 24005 } 24006 } 24007 24008 ASSERT(hdr_mp->b_wptr == hdr_ptr); 24009 ASSERT(mp->b_wptr == pld_ptr); 24010 24011 /* Update IP statistics */ 24012 IP_STAT_UPDATE(ipst, ip_frag_mdt_pkt_out, pkts); 24013 24014 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates, pkts); 24015 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs); 24016 24017 len = ntohs(ipha_orig->ipha_length) + (pkts - 1) * IP_SIMPLE_HDR_LENGTH; 24018 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, pkts); 24019 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, len); 24020 24021 if (pkt_type == OB_PKT) { 24022 ire->ire_ob_pkt_count += pkts; 24023 if (ire->ire_ipif != NULL) 24024 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 24025 } else { 24026 /* The type is IB_PKT in the forwarding path. */ 24027 ire->ire_ib_pkt_count += pkts; 24028 ASSERT(!IRE_IS_LOCAL(ire)); 24029 if (ire->ire_type & IRE_BROADCAST) { 24030 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 24031 } else { 24032 UPDATE_MIB(ill->ill_ip_mib, 24033 ipIfStatsHCOutForwDatagrams, pkts); 24034 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 24035 } 24036 } 24037 ire->ire_last_used_time = lbolt; 24038 /* Send it down */ 24039 putnext(ire->ire_stq, md_mp); 24040 return; 24041 24042 pbuf_panic: 24043 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 24044 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 24045 pbuf_idx); 24046 /* NOTREACHED */ 24047 } 24048 24049 /* 24050 * Outbound IP fragmentation routine. 24051 * 24052 * NOTE : This routine does not ire_refrele the ire that is passed in 24053 * as the argument. 24054 */ 24055 static void 24056 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 24057 uint32_t frag_flag, zoneid_t zoneid, ip_stack_t *ipst) 24058 { 24059 int i1; 24060 mblk_t *ll_hdr_mp; 24061 int ll_hdr_len; 24062 int hdr_len; 24063 mblk_t *hdr_mp; 24064 ipha_t *ipha; 24065 int ip_data_end; 24066 int len; 24067 mblk_t *mp = mp_orig, *mp1; 24068 int offset; 24069 queue_t *q; 24070 uint32_t v_hlen_tos_len; 24071 mblk_t *first_mp; 24072 boolean_t mctl_present; 24073 ill_t *ill; 24074 ill_t *out_ill; 24075 mblk_t *xmit_mp; 24076 mblk_t *carve_mp; 24077 ire_t *ire1 = NULL; 24078 ire_t *save_ire = NULL; 24079 mblk_t *next_mp = NULL; 24080 boolean_t last_frag = B_FALSE; 24081 boolean_t multirt_send = B_FALSE; 24082 ire_t *first_ire = NULL; 24083 irb_t *irb = NULL; 24084 mib2_ipIfStatsEntry_t *mibptr = NULL; 24085 24086 ill = ire_to_ill(ire); 24087 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 24088 24089 BUMP_MIB(mibptr, ipIfStatsOutFragReqds); 24090 24091 if (max_frag == 0) { 24092 ip1dbg(("ip_wput_frag: ire frag size is 0" 24093 " - dropping packet\n")); 24094 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24095 freemsg(mp); 24096 return; 24097 } 24098 24099 /* 24100 * IPsec does not allow hw accelerated packets to be fragmented 24101 * This check is made in ip_wput_ipsec_out prior to coming here 24102 * via ip_wput_ire_fragmentit. 24103 * 24104 * If at this point we have an ire whose ARP request has not 24105 * been sent out, we call ip_xmit_v4->ire_arpresolve to trigger 24106 * sending of ARP query and change ire's state to ND_INCOMPLETE. 24107 * This packet and all fragmentable packets for this ire will 24108 * continue to get dropped while ire_nce->nce_state remains in 24109 * ND_INCOMPLETE. Post-ARP resolution, after ire's nce_state changes to 24110 * ND_REACHABLE, all subsquent large packets for this ire will 24111 * get fragemented and sent out by this function. 24112 */ 24113 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 24114 /* If nce_state is ND_INITIAL, trigger ARP query */ 24115 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 24116 ip1dbg(("ip_wput_frag: mac address for ire is unresolved" 24117 " - dropping packet\n")); 24118 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24119 freemsg(mp); 24120 return; 24121 } 24122 24123 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 24124 "ip_wput_frag_start:"); 24125 24126 if (mp->b_datap->db_type == M_CTL) { 24127 first_mp = mp; 24128 mp_orig = mp = mp->b_cont; 24129 mctl_present = B_TRUE; 24130 } else { 24131 first_mp = mp; 24132 mctl_present = B_FALSE; 24133 } 24134 24135 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 24136 ipha = (ipha_t *)mp->b_rptr; 24137 24138 /* 24139 * If the Don't Fragment flag is on, generate an ICMP destination 24140 * unreachable, fragmentation needed. 24141 */ 24142 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 24143 if (offset & IPH_DF) { 24144 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24145 if (is_system_labeled()) { 24146 max_frag = tsol_pmtu_adjust(mp, ire->ire_max_frag, 24147 ire->ire_max_frag - max_frag, AF_INET); 24148 } 24149 /* 24150 * Need to compute hdr checksum if called from ip_wput_ire. 24151 * Note that ip_rput_forward verifies the checksum before 24152 * calling this routine so in that case this is a noop. 24153 */ 24154 ipha->ipha_hdr_checksum = 0; 24155 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24156 icmp_frag_needed(ire->ire_stq, first_mp, max_frag, zoneid, 24157 ipst); 24158 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24159 "ip_wput_frag_end:(%S)", 24160 "don't fragment"); 24161 return; 24162 } 24163 /* 24164 * Labeled systems adjust max_frag if they add a label 24165 * to send the correct path mtu. We need the real mtu since we 24166 * are fragmenting the packet after label adjustment. 24167 */ 24168 if (is_system_labeled()) 24169 max_frag = ire->ire_max_frag; 24170 if (mctl_present) 24171 freeb(first_mp); 24172 /* 24173 * Establish the starting offset. May not be zero if we are fragging 24174 * a fragment that is being forwarded. 24175 */ 24176 offset = offset & IPH_OFFSET; 24177 24178 /* TODO why is this test needed? */ 24179 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 24180 if (((max_frag - LENGTH) & ~7) < 8) { 24181 /* TODO: notify ulp somehow */ 24182 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24183 freemsg(mp); 24184 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24185 "ip_wput_frag_end:(%S)", 24186 "len < 8"); 24187 return; 24188 } 24189 24190 hdr_len = (V_HLEN & 0xF) << 2; 24191 24192 ipha->ipha_hdr_checksum = 0; 24193 24194 /* 24195 * Establish the number of bytes maximum per frag, after putting 24196 * in the header. 24197 */ 24198 len = (max_frag - hdr_len) & ~7; 24199 24200 /* Check if we can use MDT to send out the frags. */ 24201 ASSERT(!IRE_IS_LOCAL(ire)); 24202 if (hdr_len == IP_SIMPLE_HDR_LENGTH && 24203 ipst->ips_ip_multidata_outbound && 24204 !(ire->ire_flags & RTF_MULTIRT) && 24205 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 24206 ill != NULL && ILL_MDT_CAPABLE(ill) && 24207 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 24208 ASSERT(ill->ill_mdt_capab != NULL); 24209 if (!ill->ill_mdt_capab->ill_mdt_on) { 24210 /* 24211 * If MDT has been previously turned off in the past, 24212 * and we currently can do MDT (due to IPQoS policy 24213 * removal, etc.) then enable it for this interface. 24214 */ 24215 ill->ill_mdt_capab->ill_mdt_on = 1; 24216 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 24217 ill->ill_name)); 24218 } 24219 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 24220 offset); 24221 return; 24222 } 24223 24224 /* Get a copy of the header for the trailing frags */ 24225 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst); 24226 if (!hdr_mp) { 24227 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24228 freemsg(mp); 24229 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24230 "ip_wput_frag_end:(%S)", 24231 "couldn't copy hdr"); 24232 return; 24233 } 24234 if (DB_CRED(mp) != NULL) 24235 mblk_setcred(hdr_mp, DB_CRED(mp)); 24236 24237 /* Store the starting offset, with the MoreFrags flag. */ 24238 i1 = offset | IPH_MF | frag_flag; 24239 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 24240 24241 /* Establish the ending byte offset, based on the starting offset. */ 24242 offset <<= 3; 24243 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 24244 24245 /* Store the length of the first fragment in the IP header. */ 24246 i1 = len + hdr_len; 24247 ASSERT(i1 <= IP_MAXPACKET); 24248 ipha->ipha_length = htons((uint16_t)i1); 24249 24250 /* 24251 * Compute the IP header checksum for the first frag. We have to 24252 * watch out that we stop at the end of the header. 24253 */ 24254 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24255 24256 /* 24257 * Now carve off the first frag. Note that this will include the 24258 * original IP header. 24259 */ 24260 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 24261 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24262 freeb(hdr_mp); 24263 freemsg(mp_orig); 24264 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24265 "ip_wput_frag_end:(%S)", 24266 "couldn't carve first"); 24267 return; 24268 } 24269 24270 /* 24271 * Multirouting case. Each fragment is replicated 24272 * via all non-condemned RTF_MULTIRT routes 24273 * currently resolved. 24274 * We ensure that first_ire is the first RTF_MULTIRT 24275 * ire in the bucket. 24276 */ 24277 if (ire->ire_flags & RTF_MULTIRT) { 24278 irb = ire->ire_bucket; 24279 ASSERT(irb != NULL); 24280 24281 multirt_send = B_TRUE; 24282 24283 /* Make sure we do not omit any multiroute ire. */ 24284 IRB_REFHOLD(irb); 24285 for (first_ire = irb->irb_ire; 24286 first_ire != NULL; 24287 first_ire = first_ire->ire_next) { 24288 if ((first_ire->ire_flags & RTF_MULTIRT) && 24289 (first_ire->ire_addr == ire->ire_addr) && 24290 !(first_ire->ire_marks & 24291 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) { 24292 break; 24293 } 24294 } 24295 24296 if (first_ire != NULL) { 24297 if (first_ire != ire) { 24298 IRE_REFHOLD(first_ire); 24299 /* 24300 * Do not release the ire passed in 24301 * as the argument. 24302 */ 24303 ire = first_ire; 24304 } else { 24305 first_ire = NULL; 24306 } 24307 } 24308 IRB_REFRELE(irb); 24309 24310 /* 24311 * Save the first ire; we will need to restore it 24312 * for the trailing frags. 24313 * We REFHOLD save_ire, as each iterated ire will be 24314 * REFRELEd. 24315 */ 24316 save_ire = ire; 24317 IRE_REFHOLD(save_ire); 24318 } 24319 24320 /* 24321 * First fragment emission loop. 24322 * In most cases, the emission loop below is entered only 24323 * once. Only in the case where the ire holds the RTF_MULTIRT 24324 * flag, do we loop to process all RTF_MULTIRT ires in the 24325 * bucket, and send the fragment through all crossed 24326 * RTF_MULTIRT routes. 24327 */ 24328 do { 24329 if (ire->ire_flags & RTF_MULTIRT) { 24330 /* 24331 * We are in a multiple send case, need to get 24332 * the next ire and make a copy of the packet. 24333 * ire1 holds here the next ire to process in the 24334 * bucket. If multirouting is expected, 24335 * any non-RTF_MULTIRT ire that has the 24336 * right destination address is ignored. 24337 * 24338 * We have to take into account the MTU of 24339 * each walked ire. max_frag is set by the 24340 * the caller and generally refers to 24341 * the primary ire entry. Here we ensure that 24342 * no route with a lower MTU will be used, as 24343 * fragments are carved once for all ires, 24344 * then replicated. 24345 */ 24346 ASSERT(irb != NULL); 24347 IRB_REFHOLD(irb); 24348 for (ire1 = ire->ire_next; 24349 ire1 != NULL; 24350 ire1 = ire1->ire_next) { 24351 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 24352 continue; 24353 if (ire1->ire_addr != ire->ire_addr) 24354 continue; 24355 if (ire1->ire_marks & 24356 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 24357 continue; 24358 /* 24359 * Ensure we do not exceed the MTU 24360 * of the next route. 24361 */ 24362 if (ire1->ire_max_frag < max_frag) { 24363 ip_multirt_bad_mtu(ire1, max_frag); 24364 continue; 24365 } 24366 24367 /* Got one. */ 24368 IRE_REFHOLD(ire1); 24369 break; 24370 } 24371 IRB_REFRELE(irb); 24372 24373 if (ire1 != NULL) { 24374 next_mp = copyb(mp); 24375 if ((next_mp == NULL) || 24376 ((mp->b_cont != NULL) && 24377 ((next_mp->b_cont = 24378 dupmsg(mp->b_cont)) == NULL))) { 24379 freemsg(next_mp); 24380 next_mp = NULL; 24381 ire_refrele(ire1); 24382 ire1 = NULL; 24383 } 24384 } 24385 24386 /* Last multiroute ire; don't loop anymore. */ 24387 if (ire1 == NULL) { 24388 multirt_send = B_FALSE; 24389 } 24390 } 24391 24392 ll_hdr_len = 0; 24393 LOCK_IRE_FP_MP(ire); 24394 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24395 if (ll_hdr_mp != NULL) { 24396 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24397 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 24398 } else { 24399 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24400 } 24401 24402 /* If there is a transmit header, get a copy for this frag. */ 24403 /* 24404 * TODO: should check db_ref before calling ip_carve_mp since 24405 * it might give us a dup. 24406 */ 24407 if (!ll_hdr_mp) { 24408 /* No xmit header. */ 24409 xmit_mp = mp; 24410 24411 /* We have a link-layer header that can fit in our mblk. */ 24412 } else if (mp->b_datap->db_ref == 1 && 24413 ll_hdr_len != 0 && 24414 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24415 /* M_DATA fastpath */ 24416 mp->b_rptr -= ll_hdr_len; 24417 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 24418 xmit_mp = mp; 24419 24420 /* Corner case if copyb has failed */ 24421 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 24422 UNLOCK_IRE_FP_MP(ire); 24423 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24424 freeb(hdr_mp); 24425 freemsg(mp); 24426 freemsg(mp_orig); 24427 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24428 "ip_wput_frag_end:(%S)", 24429 "discard"); 24430 24431 if (multirt_send) { 24432 ASSERT(ire1); 24433 ASSERT(next_mp); 24434 24435 freemsg(next_mp); 24436 ire_refrele(ire1); 24437 } 24438 if (save_ire != NULL) 24439 IRE_REFRELE(save_ire); 24440 24441 if (first_ire != NULL) 24442 ire_refrele(first_ire); 24443 return; 24444 24445 /* 24446 * Case of res_mp OR the fastpath mp can't fit 24447 * in the mblk 24448 */ 24449 } else { 24450 xmit_mp->b_cont = mp; 24451 if (DB_CRED(mp) != NULL) 24452 mblk_setcred(xmit_mp, DB_CRED(mp)); 24453 /* 24454 * Get priority marking, if any. 24455 * We propagate the CoS marking from the 24456 * original packet that went to QoS processing 24457 * in ip_wput_ire to the newly carved mp. 24458 */ 24459 if (DB_TYPE(xmit_mp) == M_DATA) 24460 xmit_mp->b_band = mp->b_band; 24461 } 24462 UNLOCK_IRE_FP_MP(ire); 24463 24464 q = ire->ire_stq; 24465 out_ill = (ill_t *)q->q_ptr; 24466 24467 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24468 24469 DTRACE_PROBE4(ip4__physical__out__start, 24470 ill_t *, NULL, ill_t *, out_ill, 24471 ipha_t *, ipha, mblk_t *, xmit_mp); 24472 24473 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24474 ipst->ips_ipv4firewall_physical_out, 24475 NULL, out_ill, ipha, xmit_mp, mp, ipst); 24476 24477 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, xmit_mp); 24478 24479 if (xmit_mp != NULL) { 24480 putnext(q, xmit_mp); 24481 24482 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 24483 UPDATE_MIB(out_ill->ill_ip_mib, 24484 ipIfStatsHCOutOctets, i1); 24485 24486 if (pkt_type != OB_PKT) { 24487 /* 24488 * Update the packet count and MIB stats 24489 * of trailing RTF_MULTIRT ires. 24490 */ 24491 UPDATE_OB_PKT_COUNT(ire); 24492 BUMP_MIB(out_ill->ill_ip_mib, 24493 ipIfStatsOutFragReqds); 24494 } 24495 } 24496 24497 if (multirt_send) { 24498 /* 24499 * We are in a multiple send case; look for 24500 * the next ire and re-enter the loop. 24501 */ 24502 ASSERT(ire1); 24503 ASSERT(next_mp); 24504 /* REFRELE the current ire before looping */ 24505 ire_refrele(ire); 24506 ire = ire1; 24507 ire1 = NULL; 24508 mp = next_mp; 24509 next_mp = NULL; 24510 } 24511 } while (multirt_send); 24512 24513 ASSERT(ire1 == NULL); 24514 24515 /* Restore the original ire; we need it for the trailing frags */ 24516 if (save_ire != NULL) { 24517 /* REFRELE the last iterated ire */ 24518 ire_refrele(ire); 24519 /* save_ire has been REFHOLDed */ 24520 ire = save_ire; 24521 save_ire = NULL; 24522 q = ire->ire_stq; 24523 } 24524 24525 if (pkt_type == OB_PKT) { 24526 UPDATE_OB_PKT_COUNT(ire); 24527 } else { 24528 out_ill = (ill_t *)q->q_ptr; 24529 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 24530 UPDATE_IB_PKT_COUNT(ire); 24531 } 24532 24533 /* Advance the offset to the second frag starting point. */ 24534 offset += len; 24535 /* 24536 * Update hdr_len from the copied header - there might be less options 24537 * in the later fragments. 24538 */ 24539 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 24540 /* Loop until done. */ 24541 for (;;) { 24542 uint16_t offset_and_flags; 24543 uint16_t ip_len; 24544 24545 if (ip_data_end - offset > len) { 24546 /* 24547 * Carve off the appropriate amount from the original 24548 * datagram. 24549 */ 24550 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24551 mp = NULL; 24552 break; 24553 } 24554 /* 24555 * More frags after this one. Get another copy 24556 * of the header. 24557 */ 24558 if (carve_mp->b_datap->db_ref == 1 && 24559 hdr_mp->b_wptr - hdr_mp->b_rptr < 24560 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24561 /* Inline IP header */ 24562 carve_mp->b_rptr -= hdr_mp->b_wptr - 24563 hdr_mp->b_rptr; 24564 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24565 hdr_mp->b_wptr - hdr_mp->b_rptr); 24566 mp = carve_mp; 24567 } else { 24568 if (!(mp = copyb(hdr_mp))) { 24569 freemsg(carve_mp); 24570 break; 24571 } 24572 /* Get priority marking, if any. */ 24573 mp->b_band = carve_mp->b_band; 24574 mp->b_cont = carve_mp; 24575 } 24576 ipha = (ipha_t *)mp->b_rptr; 24577 offset_and_flags = IPH_MF; 24578 } else { 24579 /* 24580 * Last frag. Consume the header. Set len to 24581 * the length of this last piece. 24582 */ 24583 len = ip_data_end - offset; 24584 24585 /* 24586 * Carve off the appropriate amount from the original 24587 * datagram. 24588 */ 24589 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24590 mp = NULL; 24591 break; 24592 } 24593 if (carve_mp->b_datap->db_ref == 1 && 24594 hdr_mp->b_wptr - hdr_mp->b_rptr < 24595 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24596 /* Inline IP header */ 24597 carve_mp->b_rptr -= hdr_mp->b_wptr - 24598 hdr_mp->b_rptr; 24599 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24600 hdr_mp->b_wptr - hdr_mp->b_rptr); 24601 mp = carve_mp; 24602 freeb(hdr_mp); 24603 hdr_mp = mp; 24604 } else { 24605 mp = hdr_mp; 24606 /* Get priority marking, if any. */ 24607 mp->b_band = carve_mp->b_band; 24608 mp->b_cont = carve_mp; 24609 } 24610 ipha = (ipha_t *)mp->b_rptr; 24611 /* A frag of a frag might have IPH_MF non-zero */ 24612 offset_and_flags = 24613 ntohs(ipha->ipha_fragment_offset_and_flags) & 24614 IPH_MF; 24615 } 24616 offset_and_flags |= (uint16_t)(offset >> 3); 24617 offset_and_flags |= (uint16_t)frag_flag; 24618 /* Store the offset and flags in the IP header. */ 24619 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24620 24621 /* Store the length in the IP header. */ 24622 ip_len = (uint16_t)(len + hdr_len); 24623 ipha->ipha_length = htons(ip_len); 24624 24625 /* 24626 * Set the IP header checksum. Note that mp is just 24627 * the header, so this is easy to pass to ip_csum. 24628 */ 24629 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24630 24631 /* Attach a transmit header, if any, and ship it. */ 24632 if (pkt_type == OB_PKT) { 24633 UPDATE_OB_PKT_COUNT(ire); 24634 } else { 24635 out_ill = (ill_t *)q->q_ptr; 24636 BUMP_MIB(out_ill->ill_ip_mib, 24637 ipIfStatsHCOutForwDatagrams); 24638 UPDATE_IB_PKT_COUNT(ire); 24639 } 24640 24641 if (ire->ire_flags & RTF_MULTIRT) { 24642 irb = ire->ire_bucket; 24643 ASSERT(irb != NULL); 24644 24645 multirt_send = B_TRUE; 24646 24647 /* 24648 * Save the original ire; we will need to restore it 24649 * for the tailing frags. 24650 */ 24651 save_ire = ire; 24652 IRE_REFHOLD(save_ire); 24653 } 24654 /* 24655 * Emission loop for this fragment, similar 24656 * to what is done for the first fragment. 24657 */ 24658 do { 24659 if (multirt_send) { 24660 /* 24661 * We are in a multiple send case, need to get 24662 * the next ire and make a copy of the packet. 24663 */ 24664 ASSERT(irb != NULL); 24665 IRB_REFHOLD(irb); 24666 for (ire1 = ire->ire_next; 24667 ire1 != NULL; 24668 ire1 = ire1->ire_next) { 24669 if (!(ire1->ire_flags & RTF_MULTIRT)) 24670 continue; 24671 if (ire1->ire_addr != ire->ire_addr) 24672 continue; 24673 if (ire1->ire_marks & 24674 (IRE_MARK_CONDEMNED| 24675 IRE_MARK_HIDDEN)) { 24676 continue; 24677 } 24678 /* 24679 * Ensure we do not exceed the MTU 24680 * of the next route. 24681 */ 24682 if (ire1->ire_max_frag < max_frag) { 24683 ip_multirt_bad_mtu(ire1, 24684 max_frag); 24685 continue; 24686 } 24687 24688 /* Got one. */ 24689 IRE_REFHOLD(ire1); 24690 break; 24691 } 24692 IRB_REFRELE(irb); 24693 24694 if (ire1 != NULL) { 24695 next_mp = copyb(mp); 24696 if ((next_mp == NULL) || 24697 ((mp->b_cont != NULL) && 24698 ((next_mp->b_cont = 24699 dupmsg(mp->b_cont)) == NULL))) { 24700 freemsg(next_mp); 24701 next_mp = NULL; 24702 ire_refrele(ire1); 24703 ire1 = NULL; 24704 } 24705 } 24706 24707 /* Last multiroute ire; don't loop anymore. */ 24708 if (ire1 == NULL) { 24709 multirt_send = B_FALSE; 24710 } 24711 } 24712 24713 /* Update transmit header */ 24714 ll_hdr_len = 0; 24715 LOCK_IRE_FP_MP(ire); 24716 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24717 if (ll_hdr_mp != NULL) { 24718 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24719 ll_hdr_len = MBLKL(ll_hdr_mp); 24720 } else { 24721 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24722 } 24723 24724 if (!ll_hdr_mp) { 24725 xmit_mp = mp; 24726 24727 /* 24728 * We have link-layer header that can fit in 24729 * our mblk. 24730 */ 24731 } else if (mp->b_datap->db_ref == 1 && 24732 ll_hdr_len != 0 && 24733 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24734 /* M_DATA fastpath */ 24735 mp->b_rptr -= ll_hdr_len; 24736 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 24737 ll_hdr_len); 24738 xmit_mp = mp; 24739 24740 /* 24741 * Case of res_mp OR the fastpath mp can't fit 24742 * in the mblk 24743 */ 24744 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 24745 xmit_mp->b_cont = mp; 24746 if (DB_CRED(mp) != NULL) 24747 mblk_setcred(xmit_mp, DB_CRED(mp)); 24748 /* Get priority marking, if any. */ 24749 if (DB_TYPE(xmit_mp) == M_DATA) 24750 xmit_mp->b_band = mp->b_band; 24751 24752 /* Corner case if copyb failed */ 24753 } else { 24754 /* 24755 * Exit both the replication and 24756 * fragmentation loops. 24757 */ 24758 UNLOCK_IRE_FP_MP(ire); 24759 goto drop_pkt; 24760 } 24761 UNLOCK_IRE_FP_MP(ire); 24762 24763 mp1 = mp; 24764 out_ill = (ill_t *)q->q_ptr; 24765 24766 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24767 24768 DTRACE_PROBE4(ip4__physical__out__start, 24769 ill_t *, NULL, ill_t *, out_ill, 24770 ipha_t *, ipha, mblk_t *, xmit_mp); 24771 24772 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24773 ipst->ips_ipv4firewall_physical_out, 24774 NULL, out_ill, ipha, xmit_mp, mp, ipst); 24775 24776 DTRACE_PROBE1(ip4__physical__out__end, 24777 mblk_t *, xmit_mp); 24778 24779 if (mp != mp1 && hdr_mp == mp1) 24780 hdr_mp = mp; 24781 if (mp != mp1 && mp_orig == mp1) 24782 mp_orig = mp; 24783 24784 if (xmit_mp != NULL) { 24785 putnext(q, xmit_mp); 24786 24787 BUMP_MIB(out_ill->ill_ip_mib, 24788 ipIfStatsHCOutTransmits); 24789 UPDATE_MIB(out_ill->ill_ip_mib, 24790 ipIfStatsHCOutOctets, ip_len); 24791 24792 if (pkt_type != OB_PKT) { 24793 /* 24794 * Update the packet count of trailing 24795 * RTF_MULTIRT ires. 24796 */ 24797 UPDATE_OB_PKT_COUNT(ire); 24798 } 24799 } 24800 24801 /* All done if we just consumed the hdr_mp. */ 24802 if (mp == hdr_mp) { 24803 last_frag = B_TRUE; 24804 BUMP_MIB(out_ill->ill_ip_mib, 24805 ipIfStatsOutFragOKs); 24806 } 24807 24808 if (multirt_send) { 24809 /* 24810 * We are in a multiple send case; look for 24811 * the next ire and re-enter the loop. 24812 */ 24813 ASSERT(ire1); 24814 ASSERT(next_mp); 24815 /* REFRELE the current ire before looping */ 24816 ire_refrele(ire); 24817 ire = ire1; 24818 ire1 = NULL; 24819 q = ire->ire_stq; 24820 mp = next_mp; 24821 next_mp = NULL; 24822 } 24823 } while (multirt_send); 24824 /* 24825 * Restore the original ire; we need it for the 24826 * trailing frags 24827 */ 24828 if (save_ire != NULL) { 24829 ASSERT(ire1 == NULL); 24830 /* REFRELE the last iterated ire */ 24831 ire_refrele(ire); 24832 /* save_ire has been REFHOLDed */ 24833 ire = save_ire; 24834 q = ire->ire_stq; 24835 save_ire = NULL; 24836 } 24837 24838 if (last_frag) { 24839 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24840 "ip_wput_frag_end:(%S)", 24841 "consumed hdr_mp"); 24842 24843 if (first_ire != NULL) 24844 ire_refrele(first_ire); 24845 return; 24846 } 24847 /* Otherwise, advance and loop. */ 24848 offset += len; 24849 } 24850 24851 drop_pkt: 24852 /* Clean up following allocation failure. */ 24853 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24854 freemsg(mp); 24855 if (mp != hdr_mp) 24856 freeb(hdr_mp); 24857 if (mp != mp_orig) 24858 freemsg(mp_orig); 24859 24860 if (save_ire != NULL) 24861 IRE_REFRELE(save_ire); 24862 if (first_ire != NULL) 24863 ire_refrele(first_ire); 24864 24865 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24866 "ip_wput_frag_end:(%S)", 24867 "end--alloc failure"); 24868 } 24869 24870 /* 24871 * Copy the header plus those options which have the copy bit set 24872 */ 24873 static mblk_t * 24874 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst) 24875 { 24876 mblk_t *mp; 24877 uchar_t *up; 24878 24879 /* 24880 * Quick check if we need to look for options without the copy bit 24881 * set 24882 */ 24883 mp = allocb(ipst->ips_ip_wroff_extra + hdr_len, BPRI_HI); 24884 if (!mp) 24885 return (mp); 24886 mp->b_rptr += ipst->ips_ip_wroff_extra; 24887 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 24888 bcopy(rptr, mp->b_rptr, hdr_len); 24889 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra; 24890 return (mp); 24891 } 24892 up = mp->b_rptr; 24893 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 24894 up += IP_SIMPLE_HDR_LENGTH; 24895 rptr += IP_SIMPLE_HDR_LENGTH; 24896 hdr_len -= IP_SIMPLE_HDR_LENGTH; 24897 while (hdr_len > 0) { 24898 uint32_t optval; 24899 uint32_t optlen; 24900 24901 optval = *rptr; 24902 if (optval == IPOPT_EOL) 24903 break; 24904 if (optval == IPOPT_NOP) 24905 optlen = 1; 24906 else 24907 optlen = rptr[1]; 24908 if (optval & IPOPT_COPY) { 24909 bcopy(rptr, up, optlen); 24910 up += optlen; 24911 } 24912 rptr += optlen; 24913 hdr_len -= optlen; 24914 } 24915 /* 24916 * Make sure that we drop an even number of words by filling 24917 * with EOL to the next word boundary. 24918 */ 24919 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 24920 hdr_len & 0x3; hdr_len++) 24921 *up++ = IPOPT_EOL; 24922 mp->b_wptr = up; 24923 /* Update header length */ 24924 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 24925 return (mp); 24926 } 24927 24928 /* 24929 * Delivery to local recipients including fanout to multiple recipients. 24930 * Does not do checksumming of UDP/TCP. 24931 * Note: q should be the read side queue for either the ill or conn. 24932 * Note: rq should be the read side q for the lower (ill) stream. 24933 * We don't send packets to IPPF processing, thus the last argument 24934 * to all the fanout calls are B_FALSE. 24935 */ 24936 void 24937 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 24938 int fanout_flags, zoneid_t zoneid) 24939 { 24940 uint32_t protocol; 24941 mblk_t *first_mp; 24942 boolean_t mctl_present; 24943 int ire_type; 24944 #define rptr ((uchar_t *)ipha) 24945 ip_stack_t *ipst = ill->ill_ipst; 24946 24947 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 24948 "ip_wput_local_start: q %p", q); 24949 24950 if (ire != NULL) { 24951 ire_type = ire->ire_type; 24952 } else { 24953 /* 24954 * Only ip_multicast_loopback() calls us with a NULL ire. If the 24955 * packet is not multicast, we can't tell the ire type. 24956 */ 24957 ASSERT(CLASSD(ipha->ipha_dst)); 24958 ire_type = IRE_BROADCAST; 24959 } 24960 24961 first_mp = mp; 24962 if (first_mp->b_datap->db_type == M_CTL) { 24963 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 24964 if (!io->ipsec_out_secure) { 24965 /* 24966 * This ipsec_out_t was allocated in ip_wput 24967 * for multicast packets to store the ill_index. 24968 * As this is being delivered locally, we don't 24969 * need this anymore. 24970 */ 24971 mp = first_mp->b_cont; 24972 freeb(first_mp); 24973 first_mp = mp; 24974 mctl_present = B_FALSE; 24975 } else { 24976 /* 24977 * Convert IPSEC_OUT to IPSEC_IN, preserving all 24978 * security properties for the looped-back packet. 24979 */ 24980 mctl_present = B_TRUE; 24981 mp = first_mp->b_cont; 24982 ASSERT(mp != NULL); 24983 ipsec_out_to_in(first_mp); 24984 } 24985 } else { 24986 mctl_present = B_FALSE; 24987 } 24988 24989 DTRACE_PROBE4(ip4__loopback__in__start, 24990 ill_t *, ill, ill_t *, NULL, 24991 ipha_t *, ipha, mblk_t *, first_mp); 24992 24993 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 24994 ipst->ips_ipv4firewall_loopback_in, 24995 ill, NULL, ipha, first_mp, mp, ipst); 24996 24997 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, first_mp); 24998 24999 if (first_mp == NULL) 25000 return; 25001 25002 ipst->ips_loopback_packets++; 25003 25004 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 25005 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 25006 if (!IS_SIMPLE_IPH(ipha)) { 25007 ip_wput_local_options(ipha, ipst); 25008 } 25009 25010 protocol = ipha->ipha_protocol; 25011 switch (protocol) { 25012 case IPPROTO_ICMP: { 25013 ire_t *ire_zone; 25014 ilm_t *ilm; 25015 mblk_t *mp1; 25016 zoneid_t last_zoneid; 25017 25018 if (CLASSD(ipha->ipha_dst) && !IS_LOOPBACK(ill)) { 25019 ASSERT(ire_type == IRE_BROADCAST); 25020 /* 25021 * In the multicast case, applications may have joined 25022 * the group from different zones, so we need to deliver 25023 * the packet to each of them. Loop through the 25024 * multicast memberships structures (ilm) on the receive 25025 * ill and send a copy of the packet up each matching 25026 * one. However, we don't do this for multicasts sent on 25027 * the loopback interface (PHYI_LOOPBACK flag set) as 25028 * they must stay in the sender's zone. 25029 * 25030 * ilm_add_v6() ensures that ilms in the same zone are 25031 * contiguous in the ill_ilm list. We use this property 25032 * to avoid sending duplicates needed when two 25033 * applications in the same zone join the same group on 25034 * different logical interfaces: we ignore the ilm if 25035 * it's zoneid is the same as the last matching one. 25036 * In addition, the sending of the packet for 25037 * ire_zoneid is delayed until all of the other ilms 25038 * have been exhausted. 25039 */ 25040 last_zoneid = -1; 25041 ILM_WALKER_HOLD(ill); 25042 for (ilm = ill->ill_ilm; ilm != NULL; 25043 ilm = ilm->ilm_next) { 25044 if ((ilm->ilm_flags & ILM_DELETED) || 25045 ipha->ipha_dst != ilm->ilm_addr || 25046 ilm->ilm_zoneid == last_zoneid || 25047 ilm->ilm_zoneid == zoneid || 25048 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 25049 continue; 25050 mp1 = ip_copymsg(first_mp); 25051 if (mp1 == NULL) 25052 continue; 25053 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25054 mctl_present, B_FALSE, ill, 25055 ilm->ilm_zoneid); 25056 last_zoneid = ilm->ilm_zoneid; 25057 } 25058 ILM_WALKER_RELE(ill); 25059 /* 25060 * Loopback case: the sending endpoint has 25061 * IP_MULTICAST_LOOP disabled, therefore we don't 25062 * dispatch the multicast packet to the sending zone. 25063 */ 25064 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 25065 freemsg(first_mp); 25066 return; 25067 } 25068 } else if (ire_type == IRE_BROADCAST) { 25069 /* 25070 * In the broadcast case, there may be many zones 25071 * which need a copy of the packet delivered to them. 25072 * There is one IRE_BROADCAST per broadcast address 25073 * and per zone; we walk those using a helper function. 25074 * In addition, the sending of the packet for zoneid is 25075 * delayed until all of the other ires have been 25076 * processed. 25077 */ 25078 IRB_REFHOLD(ire->ire_bucket); 25079 ire_zone = NULL; 25080 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 25081 ire)) != NULL) { 25082 mp1 = ip_copymsg(first_mp); 25083 if (mp1 == NULL) 25084 continue; 25085 25086 UPDATE_IB_PKT_COUNT(ire_zone); 25087 ire_zone->ire_last_used_time = lbolt; 25088 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25089 mctl_present, B_FALSE, ill, 25090 ire_zone->ire_zoneid); 25091 } 25092 IRB_REFRELE(ire->ire_bucket); 25093 } 25094 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 25095 0, mctl_present, B_FALSE, ill, zoneid); 25096 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25097 "ip_wput_local_end: q %p (%S)", 25098 q, "icmp"); 25099 return; 25100 } 25101 case IPPROTO_IGMP: 25102 if ((mp = igmp_input(q, mp, ill)) == NULL) { 25103 /* Bad packet - discarded by igmp_input */ 25104 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25105 "ip_wput_local_end: q %p (%S)", 25106 q, "igmp_input--bad packet"); 25107 if (mctl_present) 25108 freeb(first_mp); 25109 return; 25110 } 25111 /* 25112 * igmp_input() may have returned the pulled up message. 25113 * So first_mp and ipha need to be reinitialized. 25114 */ 25115 ipha = (ipha_t *)mp->b_rptr; 25116 if (mctl_present) 25117 first_mp->b_cont = mp; 25118 else 25119 first_mp = mp; 25120 /* deliver to local raw users */ 25121 break; 25122 case IPPROTO_ENCAP: 25123 /* 25124 * This case is covered by either ip_fanout_proto, or by 25125 * the above security processing for self-tunneled packets. 25126 */ 25127 break; 25128 case IPPROTO_UDP: { 25129 uint16_t *up; 25130 uint32_t ports; 25131 25132 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 25133 UDP_PORTS_OFFSET); 25134 /* Force a 'valid' checksum. */ 25135 up[3] = 0; 25136 25137 ports = *(uint32_t *)up; 25138 ip_fanout_udp(q, first_mp, ill, ipha, ports, 25139 (ire_type == IRE_BROADCAST), 25140 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25141 IP_FF_SEND_SLLA | IP_FF_IPINFO, mctl_present, B_FALSE, 25142 ill, zoneid); 25143 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25144 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 25145 return; 25146 } 25147 case IPPROTO_TCP: { 25148 25149 /* 25150 * For TCP, discard broadcast packets. 25151 */ 25152 if ((ushort_t)ire_type == IRE_BROADCAST) { 25153 freemsg(first_mp); 25154 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 25155 ip2dbg(("ip_wput_local: discard broadcast\n")); 25156 return; 25157 } 25158 25159 if (mp->b_datap->db_type == M_DATA) { 25160 /* 25161 * M_DATA mblk, so init mblk (chain) for no struio(). 25162 */ 25163 mblk_t *mp1 = mp; 25164 25165 do { 25166 mp1->b_datap->db_struioflag = 0; 25167 } while ((mp1 = mp1->b_cont) != NULL); 25168 } 25169 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 25170 <= mp->b_wptr); 25171 ip_fanout_tcp(q, first_mp, ill, ipha, 25172 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25173 IP_FF_SYN_ADDIRE | IP_FF_IPINFO, 25174 mctl_present, B_FALSE, zoneid); 25175 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25176 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 25177 return; 25178 } 25179 case IPPROTO_SCTP: 25180 { 25181 uint32_t ports; 25182 25183 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 25184 ip_fanout_sctp(first_mp, ill, ipha, ports, 25185 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25186 IP_FF_IPINFO, mctl_present, B_FALSE, zoneid); 25187 return; 25188 } 25189 25190 default: 25191 break; 25192 } 25193 /* 25194 * Find a client for some other protocol. We give 25195 * copies to multiple clients, if more than one is 25196 * bound. 25197 */ 25198 ip_fanout_proto(q, first_mp, ill, ipha, 25199 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 25200 mctl_present, B_FALSE, ill, zoneid); 25201 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25202 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 25203 #undef rptr 25204 } 25205 25206 /* 25207 * Update any source route, record route, or timestamp options. 25208 * Check that we are at end of strict source route. 25209 * The options have been sanity checked by ip_wput_options(). 25210 */ 25211 static void 25212 ip_wput_local_options(ipha_t *ipha, ip_stack_t *ipst) 25213 { 25214 ipoptp_t opts; 25215 uchar_t *opt; 25216 uint8_t optval; 25217 uint8_t optlen; 25218 ipaddr_t dst; 25219 uint32_t ts; 25220 ire_t *ire; 25221 timestruc_t now; 25222 25223 ip2dbg(("ip_wput_local_options\n")); 25224 for (optval = ipoptp_first(&opts, ipha); 25225 optval != IPOPT_EOL; 25226 optval = ipoptp_next(&opts)) { 25227 opt = opts.ipoptp_cur; 25228 optlen = opts.ipoptp_len; 25229 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 25230 switch (optval) { 25231 uint32_t off; 25232 case IPOPT_SSRR: 25233 case IPOPT_LSRR: 25234 off = opt[IPOPT_OFFSET]; 25235 off--; 25236 if (optlen < IP_ADDR_LEN || 25237 off > optlen - IP_ADDR_LEN) { 25238 /* End of source route */ 25239 break; 25240 } 25241 /* 25242 * This will only happen if two consecutive entries 25243 * in the source route contains our address or if 25244 * it is a packet with a loose source route which 25245 * reaches us before consuming the whole source route 25246 */ 25247 ip1dbg(("ip_wput_local_options: not end of SR\n")); 25248 if (optval == IPOPT_SSRR) { 25249 return; 25250 } 25251 /* 25252 * Hack: instead of dropping the packet truncate the 25253 * source route to what has been used by filling the 25254 * rest with IPOPT_NOP. 25255 */ 25256 opt[IPOPT_OLEN] = (uint8_t)off; 25257 while (off < optlen) { 25258 opt[off++] = IPOPT_NOP; 25259 } 25260 break; 25261 case IPOPT_RR: 25262 off = opt[IPOPT_OFFSET]; 25263 off--; 25264 if (optlen < IP_ADDR_LEN || 25265 off > optlen - IP_ADDR_LEN) { 25266 /* No more room - ignore */ 25267 ip1dbg(( 25268 "ip_wput_forward_options: end of RR\n")); 25269 break; 25270 } 25271 dst = htonl(INADDR_LOOPBACK); 25272 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25273 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25274 break; 25275 case IPOPT_TS: 25276 /* Insert timestamp if there is romm */ 25277 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25278 case IPOPT_TS_TSONLY: 25279 off = IPOPT_TS_TIMELEN; 25280 break; 25281 case IPOPT_TS_PRESPEC: 25282 case IPOPT_TS_PRESPEC_RFC791: 25283 /* Verify that the address matched */ 25284 off = opt[IPOPT_OFFSET] - 1; 25285 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 25286 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 25287 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 25288 ipst); 25289 if (ire == NULL) { 25290 /* Not for us */ 25291 break; 25292 } 25293 ire_refrele(ire); 25294 /* FALLTHRU */ 25295 case IPOPT_TS_TSANDADDR: 25296 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 25297 break; 25298 default: 25299 /* 25300 * ip_*put_options should have already 25301 * dropped this packet. 25302 */ 25303 cmn_err(CE_PANIC, "ip_wput_local_options: " 25304 "unknown IT - bug in ip_wput_options?\n"); 25305 return; /* Keep "lint" happy */ 25306 } 25307 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 25308 /* Increase overflow counter */ 25309 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 25310 opt[IPOPT_POS_OV_FLG] = (uint8_t) 25311 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 25312 (off << 4); 25313 break; 25314 } 25315 off = opt[IPOPT_OFFSET] - 1; 25316 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25317 case IPOPT_TS_PRESPEC: 25318 case IPOPT_TS_PRESPEC_RFC791: 25319 case IPOPT_TS_TSANDADDR: 25320 dst = htonl(INADDR_LOOPBACK); 25321 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25322 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25323 /* FALLTHRU */ 25324 case IPOPT_TS_TSONLY: 25325 off = opt[IPOPT_OFFSET] - 1; 25326 /* Compute # of milliseconds since midnight */ 25327 gethrestime(&now); 25328 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 25329 now.tv_nsec / (NANOSEC / MILLISEC); 25330 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 25331 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 25332 break; 25333 } 25334 break; 25335 } 25336 } 25337 } 25338 25339 /* 25340 * Send out a multicast packet on interface ipif. 25341 * The sender does not have an conn. 25342 * Caller verifies that this isn't a PHYI_LOOPBACK. 25343 */ 25344 void 25345 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif, zoneid_t zoneid) 25346 { 25347 ipha_t *ipha; 25348 ire_t *ire; 25349 ipaddr_t dst; 25350 mblk_t *first_mp; 25351 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 25352 25353 /* igmp_sendpkt always allocates a ipsec_out_t */ 25354 ASSERT(mp->b_datap->db_type == M_CTL); 25355 ASSERT(!ipif->ipif_isv6); 25356 ASSERT(!IS_LOOPBACK(ipif->ipif_ill)); 25357 25358 first_mp = mp; 25359 mp = first_mp->b_cont; 25360 ASSERT(mp->b_datap->db_type == M_DATA); 25361 ipha = (ipha_t *)mp->b_rptr; 25362 25363 /* 25364 * Find an IRE which matches the destination and the outgoing 25365 * queue (i.e. the outgoing interface.) 25366 */ 25367 if (ipif->ipif_flags & IPIF_POINTOPOINT) 25368 dst = ipif->ipif_pp_dst_addr; 25369 else 25370 dst = ipha->ipha_dst; 25371 /* 25372 * The source address has already been initialized by the 25373 * caller and hence matching on ILL (MATCH_IRE_ILL) would 25374 * be sufficient rather than MATCH_IRE_IPIF. 25375 * 25376 * This function is used for sending IGMP packets. We need 25377 * to make sure that we send the packet out of the interface 25378 * (ipif->ipif_ill) where we joined the group. This is to 25379 * prevent from switches doing IGMP snooping to send us multicast 25380 * packets for a given group on the interface we have joined. 25381 * If we can't find an ire, igmp_sendpkt has already initialized 25382 * ipsec_out_attach_if so that this will not be load spread in 25383 * ip_newroute_ipif. 25384 */ 25385 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, NULL, 25386 MATCH_IRE_ILL, ipst); 25387 if (!ire) { 25388 /* 25389 * Mark this packet to make it be delivered to 25390 * ip_wput_ire after the new ire has been 25391 * created. 25392 */ 25393 mp->b_prev = NULL; 25394 mp->b_next = NULL; 25395 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC, 25396 zoneid, &zero_info); 25397 return; 25398 } 25399 25400 /* 25401 * Honor the RTF_SETSRC flag; this is the only case 25402 * where we force this addr whatever the current src addr is, 25403 * because this address is set by igmp_sendpkt(), and 25404 * cannot be specified by any user. 25405 */ 25406 if (ire->ire_flags & RTF_SETSRC) { 25407 ipha->ipha_src = ire->ire_src_addr; 25408 } 25409 25410 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE, zoneid); 25411 } 25412 25413 /* 25414 * NOTE : This function does not ire_refrele the ire argument passed in. 25415 * 25416 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 25417 * failure. The nce_fp_mp can vanish any time in the case of 25418 * IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 25419 * the ire_lock to access the nce_fp_mp in this case. 25420 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 25421 * prepending a fastpath message IPQoS processing must precede it, we also set 25422 * the b_band of the fastpath message to that of the mblk returned by IPQoS 25423 * (IPQoS might have set the b_band for CoS marking). 25424 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 25425 * must follow it so that IPQoS can mark the dl_priority field for CoS 25426 * marking, if needed. 25427 */ 25428 static mblk_t * 25429 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, uint32_t ill_index) 25430 { 25431 uint_t hlen; 25432 ipha_t *ipha; 25433 mblk_t *mp1; 25434 boolean_t qos_done = B_FALSE; 25435 uchar_t *ll_hdr; 25436 ip_stack_t *ipst = ire->ire_ipst; 25437 25438 #define rptr ((uchar_t *)ipha) 25439 25440 ipha = (ipha_t *)mp->b_rptr; 25441 hlen = 0; 25442 LOCK_IRE_FP_MP(ire); 25443 if ((mp1 = ire->ire_nce->nce_fp_mp) != NULL) { 25444 ASSERT(DB_TYPE(mp1) == M_DATA); 25445 /* Initiate IPPF processing */ 25446 if ((proc != 0) && IPP_ENABLED(proc, ipst)) { 25447 UNLOCK_IRE_FP_MP(ire); 25448 ip_process(proc, &mp, ill_index); 25449 if (mp == NULL) 25450 return (NULL); 25451 25452 ipha = (ipha_t *)mp->b_rptr; 25453 LOCK_IRE_FP_MP(ire); 25454 if ((mp1 = ire->ire_nce->nce_fp_mp) == NULL) { 25455 qos_done = B_TRUE; 25456 goto no_fp_mp; 25457 } 25458 ASSERT(DB_TYPE(mp1) == M_DATA); 25459 } 25460 hlen = MBLKL(mp1); 25461 /* 25462 * Check if we have enough room to prepend fastpath 25463 * header 25464 */ 25465 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 25466 ll_hdr = rptr - hlen; 25467 bcopy(mp1->b_rptr, ll_hdr, hlen); 25468 /* 25469 * Set the b_rptr to the start of the link layer 25470 * header 25471 */ 25472 mp->b_rptr = ll_hdr; 25473 mp1 = mp; 25474 } else { 25475 mp1 = copyb(mp1); 25476 if (mp1 == NULL) 25477 goto unlock_err; 25478 mp1->b_band = mp->b_band; 25479 mp1->b_cont = mp; 25480 /* 25481 * certain system generated traffic may not 25482 * have cred/label in ip header block. This 25483 * is true even for a labeled system. But for 25484 * labeled traffic, inherit the label in the 25485 * new header. 25486 */ 25487 if (DB_CRED(mp) != NULL) 25488 mblk_setcred(mp1, DB_CRED(mp)); 25489 /* 25490 * XXX disable ICK_VALID and compute checksum 25491 * here; can happen if nce_fp_mp changes and 25492 * it can't be copied now due to insufficient 25493 * space. (unlikely, fp mp can change, but it 25494 * does not increase in length) 25495 */ 25496 } 25497 UNLOCK_IRE_FP_MP(ire); 25498 } else { 25499 no_fp_mp: 25500 mp1 = copyb(ire->ire_nce->nce_res_mp); 25501 if (mp1 == NULL) { 25502 unlock_err: 25503 UNLOCK_IRE_FP_MP(ire); 25504 freemsg(mp); 25505 return (NULL); 25506 } 25507 UNLOCK_IRE_FP_MP(ire); 25508 mp1->b_cont = mp; 25509 /* 25510 * certain system generated traffic may not 25511 * have cred/label in ip header block. This 25512 * is true even for a labeled system. But for 25513 * labeled traffic, inherit the label in the 25514 * new header. 25515 */ 25516 if (DB_CRED(mp) != NULL) 25517 mblk_setcred(mp1, DB_CRED(mp)); 25518 if (!qos_done && (proc != 0) && IPP_ENABLED(proc, ipst)) { 25519 ip_process(proc, &mp1, ill_index); 25520 if (mp1 == NULL) 25521 return (NULL); 25522 } 25523 } 25524 return (mp1); 25525 #undef rptr 25526 } 25527 25528 /* 25529 * Finish the outbound IPsec processing for an IPv6 packet. This function 25530 * is called from ipsec_out_process() if the IPsec packet was processed 25531 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25532 * asynchronously. 25533 */ 25534 void 25535 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 25536 ire_t *ire_arg) 25537 { 25538 in6_addr_t *v6dstp; 25539 ire_t *ire; 25540 mblk_t *mp; 25541 ip6_t *ip6h1; 25542 uint_t ill_index; 25543 ipsec_out_t *io; 25544 boolean_t attach_if, hwaccel; 25545 uint32_t flags = IP6_NO_IPPOLICY; 25546 int match_flags; 25547 zoneid_t zoneid; 25548 boolean_t ill_need_rele = B_FALSE; 25549 boolean_t ire_need_rele = B_FALSE; 25550 ip_stack_t *ipst; 25551 25552 mp = ipsec_mp->b_cont; 25553 ip6h1 = (ip6_t *)mp->b_rptr; 25554 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25555 ASSERT(io->ipsec_out_ns != NULL); 25556 ipst = io->ipsec_out_ns->netstack_ip; 25557 ill_index = io->ipsec_out_ill_index; 25558 if (io->ipsec_out_reachable) { 25559 flags |= IPV6_REACHABILITY_CONFIRMATION; 25560 } 25561 attach_if = io->ipsec_out_attach_if; 25562 hwaccel = io->ipsec_out_accelerated; 25563 zoneid = io->ipsec_out_zoneid; 25564 ASSERT(zoneid != ALL_ZONES); 25565 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25566 /* Multicast addresses should have non-zero ill_index. */ 25567 v6dstp = &ip6h->ip6_dst; 25568 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 25569 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 25570 ASSERT(!attach_if || ill_index != 0); 25571 if (ill_index != 0) { 25572 if (ill == NULL) { 25573 ill = ip_grab_attach_ill(NULL, ipsec_mp, ill_index, 25574 B_TRUE, ipst); 25575 25576 /* Failure case frees things for us. */ 25577 if (ill == NULL) 25578 return; 25579 25580 ill_need_rele = B_TRUE; 25581 } 25582 /* 25583 * If this packet needs to go out on a particular interface 25584 * honor it. 25585 */ 25586 if (attach_if) { 25587 match_flags = MATCH_IRE_ILL; 25588 25589 /* 25590 * Check if we need an ire that will not be 25591 * looked up by anybody else i.e. HIDDEN. 25592 */ 25593 if (ill_is_probeonly(ill)) { 25594 match_flags |= MATCH_IRE_MARK_HIDDEN; 25595 } 25596 } 25597 } 25598 ASSERT(mp != NULL); 25599 25600 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 25601 boolean_t unspec_src; 25602 ipif_t *ipif; 25603 25604 /* 25605 * Use the ill_index to get the right ill. 25606 */ 25607 unspec_src = io->ipsec_out_unspec_src; 25608 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25609 if (ipif == NULL) { 25610 if (ill_need_rele) 25611 ill_refrele(ill); 25612 freemsg(ipsec_mp); 25613 return; 25614 } 25615 25616 if (ire_arg != NULL) { 25617 ire = ire_arg; 25618 } else { 25619 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25620 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25621 ire_need_rele = B_TRUE; 25622 } 25623 if (ire != NULL) { 25624 ipif_refrele(ipif); 25625 /* 25626 * XXX Do the multicast forwarding now, as the IPsec 25627 * processing has been done. 25628 */ 25629 goto send; 25630 } 25631 25632 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 25633 mp->b_prev = NULL; 25634 mp->b_next = NULL; 25635 25636 /* 25637 * If the IPsec packet was processed asynchronously, 25638 * drop it now. 25639 */ 25640 if (q == NULL) { 25641 if (ill_need_rele) 25642 ill_refrele(ill); 25643 freemsg(ipsec_mp); 25644 return; 25645 } 25646 25647 ip_newroute_ipif_v6(q, ipsec_mp, ipif, *v6dstp, 25648 unspec_src, zoneid); 25649 ipif_refrele(ipif); 25650 } else { 25651 if (attach_if) { 25652 ipif_t *ipif; 25653 25654 ipif = ipif_get_next_ipif(NULL, ill); 25655 if (ipif == NULL) { 25656 if (ill_need_rele) 25657 ill_refrele(ill); 25658 freemsg(ipsec_mp); 25659 return; 25660 } 25661 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25662 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25663 ire_need_rele = B_TRUE; 25664 ipif_refrele(ipif); 25665 } else { 25666 if (ire_arg != NULL) { 25667 ire = ire_arg; 25668 } else { 25669 ire = ire_cache_lookup_v6(v6dstp, zoneid, NULL, 25670 ipst); 25671 ire_need_rele = B_TRUE; 25672 } 25673 } 25674 if (ire != NULL) 25675 goto send; 25676 /* 25677 * ire disappeared underneath. 25678 * 25679 * What we need to do here is the ip_newroute 25680 * logic to get the ire without doing the IPsec 25681 * processing. Follow the same old path. But this 25682 * time, ip_wput or ire_add_then_send will call us 25683 * directly as all the IPsec operations are done. 25684 */ 25685 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 25686 mp->b_prev = NULL; 25687 mp->b_next = NULL; 25688 25689 /* 25690 * If the IPsec packet was processed asynchronously, 25691 * drop it now. 25692 */ 25693 if (q == NULL) { 25694 if (ill_need_rele) 25695 ill_refrele(ill); 25696 freemsg(ipsec_mp); 25697 return; 25698 } 25699 25700 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 25701 zoneid, ipst); 25702 } 25703 if (ill != NULL && ill_need_rele) 25704 ill_refrele(ill); 25705 return; 25706 send: 25707 if (ill != NULL && ill_need_rele) 25708 ill_refrele(ill); 25709 25710 /* Local delivery */ 25711 if (ire->ire_stq == NULL) { 25712 ill_t *out_ill; 25713 ASSERT(q != NULL); 25714 25715 /* PFHooks: LOOPBACK_OUT */ 25716 out_ill = ire_to_ill(ire); 25717 25718 DTRACE_PROBE4(ip6__loopback__out__start, 25719 ill_t *, NULL, ill_t *, out_ill, 25720 ip6_t *, ip6h1, mblk_t *, ipsec_mp); 25721 25722 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 25723 ipst->ips_ipv6firewall_loopback_out, 25724 NULL, out_ill, ip6h1, ipsec_mp, mp, ipst); 25725 25726 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, ipsec_mp); 25727 25728 if (ipsec_mp != NULL) 25729 ip_wput_local_v6(RD(q), out_ill, 25730 ip6h, ipsec_mp, ire, 0); 25731 if (ire_need_rele) 25732 ire_refrele(ire); 25733 return; 25734 } 25735 /* 25736 * Everything is done. Send it out on the wire. 25737 * We force the insertion of a fragment header using the 25738 * IPH_FRAG_HDR flag in two cases: 25739 * - after reception of an ICMPv6 "packet too big" message 25740 * with a MTU < 1280 (cf. RFC 2460 section 5) 25741 * - for multirouted IPv6 packets, so that the receiver can 25742 * discard duplicates according to their fragment identifier 25743 */ 25744 /* XXX fix flow control problems. */ 25745 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 25746 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 25747 if (hwaccel) { 25748 /* 25749 * hardware acceleration does not handle these 25750 * "slow path" cases. 25751 */ 25752 /* IPsec KSTATS: should bump bean counter here. */ 25753 if (ire_need_rele) 25754 ire_refrele(ire); 25755 freemsg(ipsec_mp); 25756 return; 25757 } 25758 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 25759 (mp->b_cont ? msgdsize(mp) : 25760 mp->b_wptr - (uchar_t *)ip6h)) { 25761 /* IPsec KSTATS: should bump bean counter here. */ 25762 ip0dbg(("Packet length mismatch: %d, %ld\n", 25763 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 25764 msgdsize(mp))); 25765 if (ire_need_rele) 25766 ire_refrele(ire); 25767 freemsg(ipsec_mp); 25768 return; 25769 } 25770 ASSERT(mp->b_prev == NULL); 25771 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 25772 ntohs(ip6h->ip6_plen) + 25773 IPV6_HDR_LEN, ire->ire_max_frag)); 25774 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 25775 ire->ire_max_frag); 25776 } else { 25777 UPDATE_OB_PKT_COUNT(ire); 25778 ire->ire_last_used_time = lbolt; 25779 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 25780 } 25781 if (ire_need_rele) 25782 ire_refrele(ire); 25783 freeb(ipsec_mp); 25784 } 25785 25786 void 25787 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 25788 { 25789 mblk_t *hada_mp; /* attributes M_CTL mblk */ 25790 da_ipsec_t *hada; /* data attributes */ 25791 ill_t *ill = (ill_t *)q->q_ptr; 25792 25793 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 25794 25795 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 25796 /* IPsec KSTATS: Bump lose counter here! */ 25797 freemsg(mp); 25798 return; 25799 } 25800 25801 /* 25802 * It's an IPsec packet that must be 25803 * accelerated by the Provider, and the 25804 * outbound ill is IPsec acceleration capable. 25805 * Prepends the mblk with an IPHADA_M_CTL, and ship it 25806 * to the ill. 25807 * IPsec KSTATS: should bump packet counter here. 25808 */ 25809 25810 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 25811 if (hada_mp == NULL) { 25812 /* IPsec KSTATS: should bump packet counter here. */ 25813 freemsg(mp); 25814 return; 25815 } 25816 25817 hada_mp->b_datap->db_type = M_CTL; 25818 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 25819 hada_mp->b_cont = mp; 25820 25821 hada = (da_ipsec_t *)hada_mp->b_rptr; 25822 bzero(hada, sizeof (da_ipsec_t)); 25823 hada->da_type = IPHADA_M_CTL; 25824 25825 putnext(q, hada_mp); 25826 } 25827 25828 /* 25829 * Finish the outbound IPsec processing. This function is called from 25830 * ipsec_out_process() if the IPsec packet was processed 25831 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25832 * asynchronously. 25833 */ 25834 void 25835 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 25836 ire_t *ire_arg) 25837 { 25838 uint32_t v_hlen_tos_len; 25839 ipaddr_t dst; 25840 ipif_t *ipif = NULL; 25841 ire_t *ire; 25842 ire_t *ire1 = NULL; 25843 mblk_t *next_mp = NULL; 25844 uint32_t max_frag; 25845 boolean_t multirt_send = B_FALSE; 25846 mblk_t *mp; 25847 ipha_t *ipha1; 25848 uint_t ill_index; 25849 ipsec_out_t *io; 25850 boolean_t attach_if; 25851 int match_flags; 25852 irb_t *irb = NULL; 25853 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 25854 zoneid_t zoneid; 25855 ipxmit_state_t pktxmit_state; 25856 ip_stack_t *ipst; 25857 25858 #ifdef _BIG_ENDIAN 25859 #define LENGTH (v_hlen_tos_len & 0xFFFF) 25860 #else 25861 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 25862 #endif 25863 25864 mp = ipsec_mp->b_cont; 25865 ipha1 = (ipha_t *)mp->b_rptr; 25866 ASSERT(mp != NULL); 25867 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 25868 dst = ipha->ipha_dst; 25869 25870 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25871 ill_index = io->ipsec_out_ill_index; 25872 attach_if = io->ipsec_out_attach_if; 25873 zoneid = io->ipsec_out_zoneid; 25874 ASSERT(zoneid != ALL_ZONES); 25875 ipst = io->ipsec_out_ns->netstack_ip; 25876 ASSERT(io->ipsec_out_ns != NULL); 25877 25878 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25879 if (ill_index != 0) { 25880 if (ill == NULL) { 25881 ill = ip_grab_attach_ill(NULL, ipsec_mp, 25882 ill_index, B_FALSE, ipst); 25883 25884 /* Failure case frees things for us. */ 25885 if (ill == NULL) 25886 return; 25887 25888 ill_need_rele = B_TRUE; 25889 } 25890 /* 25891 * If this packet needs to go out on a particular interface 25892 * honor it. 25893 */ 25894 if (attach_if) { 25895 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 25896 25897 /* 25898 * Check if we need an ire that will not be 25899 * looked up by anybody else i.e. HIDDEN. 25900 */ 25901 if (ill_is_probeonly(ill)) { 25902 match_flags |= MATCH_IRE_MARK_HIDDEN; 25903 } 25904 } 25905 } 25906 25907 if (CLASSD(dst)) { 25908 boolean_t conn_dontroute; 25909 /* 25910 * Use the ill_index to get the right ipif. 25911 */ 25912 conn_dontroute = io->ipsec_out_dontroute; 25913 if (ill_index == 0) 25914 ipif = ipif_lookup_group(dst, zoneid, ipst); 25915 else 25916 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25917 if (ipif == NULL) { 25918 ip1dbg(("ip_wput_ipsec_out: No ipif for" 25919 " multicast\n")); 25920 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 25921 freemsg(ipsec_mp); 25922 goto done; 25923 } 25924 /* 25925 * ipha_src has already been intialized with the 25926 * value of the ipif in ip_wput. All we need now is 25927 * an ire to send this downstream. 25928 */ 25929 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 25930 MBLK_GETLABEL(mp), match_flags, ipst); 25931 if (ire != NULL) { 25932 ill_t *ill1; 25933 /* 25934 * Do the multicast forwarding now, as the IPsec 25935 * processing has been done. 25936 */ 25937 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 25938 (ill1 = ire_to_ill(ire))) { 25939 if (ip_mforward(ill1, ipha, mp)) { 25940 freemsg(ipsec_mp); 25941 ip1dbg(("ip_wput_ipsec_out: mforward " 25942 "failed\n")); 25943 ire_refrele(ire); 25944 goto done; 25945 } 25946 } 25947 goto send; 25948 } 25949 25950 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 25951 mp->b_prev = NULL; 25952 mp->b_next = NULL; 25953 25954 /* 25955 * If the IPsec packet was processed asynchronously, 25956 * drop it now. 25957 */ 25958 if (q == NULL) { 25959 freemsg(ipsec_mp); 25960 goto done; 25961 } 25962 25963 /* 25964 * We may be using a wrong ipif to create the ire. 25965 * But it is okay as the source address is assigned 25966 * for the packet already. Next outbound packet would 25967 * create the IRE with the right IPIF in ip_wput. 25968 * 25969 * Also handle RTF_MULTIRT routes. 25970 */ 25971 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT, 25972 zoneid, &zero_info); 25973 } else { 25974 if (attach_if) { 25975 ire = ire_ctable_lookup(dst, 0, 0, ill->ill_ipif, 25976 zoneid, MBLK_GETLABEL(mp), match_flags, ipst); 25977 } else { 25978 if (ire_arg != NULL) { 25979 ire = ire_arg; 25980 ire_need_rele = B_FALSE; 25981 } else { 25982 ire = ire_cache_lookup(dst, zoneid, 25983 MBLK_GETLABEL(mp), ipst); 25984 } 25985 } 25986 if (ire != NULL) { 25987 goto send; 25988 } 25989 25990 /* 25991 * ire disappeared underneath. 25992 * 25993 * What we need to do here is the ip_newroute 25994 * logic to get the ire without doing the IPsec 25995 * processing. Follow the same old path. But this 25996 * time, ip_wput or ire_add_then_put will call us 25997 * directly as all the IPsec operations are done. 25998 */ 25999 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 26000 mp->b_prev = NULL; 26001 mp->b_next = NULL; 26002 26003 /* 26004 * If the IPsec packet was processed asynchronously, 26005 * drop it now. 26006 */ 26007 if (q == NULL) { 26008 freemsg(ipsec_mp); 26009 goto done; 26010 } 26011 26012 /* 26013 * Since we're going through ip_newroute() again, we 26014 * need to make sure we don't: 26015 * 26016 * 1.) Trigger the ASSERT() with the ipha_ident 26017 * overloading. 26018 * 2.) Redo transport-layer checksumming, since we've 26019 * already done all that to get this far. 26020 * 26021 * The easiest way not do either of the above is to set 26022 * the ipha_ident field to IP_HDR_INCLUDED. 26023 */ 26024 ipha->ipha_ident = IP_HDR_INCLUDED; 26025 ip_newroute(q, ipsec_mp, dst, (CONN_Q(q) ? Q_TO_CONN(q) : NULL), 26026 zoneid, ipst); 26027 } 26028 goto done; 26029 send: 26030 if (ire->ire_stq == NULL) { 26031 ill_t *out_ill; 26032 /* 26033 * Loopbacks go through ip_wput_local except for one case. 26034 * We come here if we generate a icmp_frag_needed message 26035 * after IPsec processing is over. When this function calls 26036 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 26037 * icmp_frag_needed. The message generated comes back here 26038 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 26039 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 26040 * source address as it is usually set in ip_wput_ire. As 26041 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 26042 * and we end up here. We can't enter ip_wput_ire once the 26043 * IPsec processing is over and hence we need to do it here. 26044 */ 26045 ASSERT(q != NULL); 26046 UPDATE_OB_PKT_COUNT(ire); 26047 ire->ire_last_used_time = lbolt; 26048 if (ipha->ipha_src == 0) 26049 ipha->ipha_src = ire->ire_src_addr; 26050 26051 /* PFHooks: LOOPBACK_OUT */ 26052 out_ill = ire_to_ill(ire); 26053 26054 DTRACE_PROBE4(ip4__loopback__out__start, 26055 ill_t *, NULL, ill_t *, out_ill, 26056 ipha_t *, ipha1, mblk_t *, ipsec_mp); 26057 26058 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 26059 ipst->ips_ipv4firewall_loopback_out, 26060 NULL, out_ill, ipha1, ipsec_mp, mp, ipst); 26061 26062 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp); 26063 26064 if (ipsec_mp != NULL) 26065 ip_wput_local(RD(q), out_ill, 26066 ipha, ipsec_mp, ire, 0, zoneid); 26067 if (ire_need_rele) 26068 ire_refrele(ire); 26069 goto done; 26070 } 26071 26072 if (ire->ire_max_frag < (unsigned int)LENGTH) { 26073 /* 26074 * We are through with IPsec processing. 26075 * Fragment this and send it on the wire. 26076 */ 26077 if (io->ipsec_out_accelerated) { 26078 /* 26079 * The packet has been accelerated but must 26080 * be fragmented. This should not happen 26081 * since AH and ESP must not accelerate 26082 * packets that need fragmentation, however 26083 * the configuration could have changed 26084 * since the AH or ESP processing. 26085 * Drop packet. 26086 * IPsec KSTATS: bump bean counter here. 26087 */ 26088 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 26089 "fragmented accelerated packet!\n")); 26090 freemsg(ipsec_mp); 26091 } else { 26092 ip_wput_ire_fragmentit(ipsec_mp, ire, zoneid, ipst); 26093 } 26094 if (ire_need_rele) 26095 ire_refrele(ire); 26096 goto done; 26097 } 26098 26099 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 26100 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 26101 (void *)ire->ire_ipif, (void *)ipif)); 26102 26103 /* 26104 * Multiroute the secured packet, unless IPsec really 26105 * requires the packet to go out only through a particular 26106 * interface. 26107 */ 26108 if ((ire->ire_flags & RTF_MULTIRT) && !attach_if) { 26109 ire_t *first_ire; 26110 irb = ire->ire_bucket; 26111 ASSERT(irb != NULL); 26112 /* 26113 * This ire has been looked up as the one that 26114 * goes through the given ipif; 26115 * make sure we do not omit any other multiroute ire 26116 * that may be present in the bucket before this one. 26117 */ 26118 IRB_REFHOLD(irb); 26119 for (first_ire = irb->irb_ire; 26120 first_ire != NULL; 26121 first_ire = first_ire->ire_next) { 26122 if ((first_ire->ire_flags & RTF_MULTIRT) && 26123 (first_ire->ire_addr == ire->ire_addr) && 26124 !(first_ire->ire_marks & 26125 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) { 26126 break; 26127 } 26128 } 26129 26130 if ((first_ire != NULL) && (first_ire != ire)) { 26131 /* 26132 * Don't change the ire if the packet must 26133 * be fragmented if sent via this new one. 26134 */ 26135 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 26136 IRE_REFHOLD(first_ire); 26137 if (ire_need_rele) 26138 ire_refrele(ire); 26139 else 26140 ire_need_rele = B_TRUE; 26141 ire = first_ire; 26142 } 26143 } 26144 IRB_REFRELE(irb); 26145 26146 multirt_send = B_TRUE; 26147 max_frag = ire->ire_max_frag; 26148 } else { 26149 if ((ire->ire_flags & RTF_MULTIRT) && attach_if) { 26150 ip1dbg(("ip_wput_ipsec_out: ignoring multirouting " 26151 "flag, attach_if %d\n", attach_if)); 26152 } 26153 } 26154 26155 /* 26156 * In most cases, the emission loop below is entered only once. 26157 * Only in the case where the ire holds the RTF_MULTIRT 26158 * flag, we loop to process all RTF_MULTIRT ires in the 26159 * bucket, and send the packet through all crossed 26160 * RTF_MULTIRT routes. 26161 */ 26162 do { 26163 if (multirt_send) { 26164 /* 26165 * ire1 holds here the next ire to process in the 26166 * bucket. If multirouting is expected, 26167 * any non-RTF_MULTIRT ire that has the 26168 * right destination address is ignored. 26169 */ 26170 ASSERT(irb != NULL); 26171 IRB_REFHOLD(irb); 26172 for (ire1 = ire->ire_next; 26173 ire1 != NULL; 26174 ire1 = ire1->ire_next) { 26175 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 26176 continue; 26177 if (ire1->ire_addr != ire->ire_addr) 26178 continue; 26179 if (ire1->ire_marks & 26180 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 26181 continue; 26182 /* No loopback here */ 26183 if (ire1->ire_stq == NULL) 26184 continue; 26185 /* 26186 * Ensure we do not exceed the MTU 26187 * of the next route. 26188 */ 26189 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 26190 ip_multirt_bad_mtu(ire1, max_frag); 26191 continue; 26192 } 26193 26194 IRE_REFHOLD(ire1); 26195 break; 26196 } 26197 IRB_REFRELE(irb); 26198 if (ire1 != NULL) { 26199 /* 26200 * We are in a multiple send case, need to 26201 * make a copy of the packet. 26202 */ 26203 next_mp = copymsg(ipsec_mp); 26204 if (next_mp == NULL) { 26205 ire_refrele(ire1); 26206 ire1 = NULL; 26207 } 26208 } 26209 } 26210 /* 26211 * Everything is done. Send it out on the wire 26212 * 26213 * ip_xmit_v4 will call ip_wput_attach_llhdr and then 26214 * either send it on the wire or, in the case of 26215 * HW acceleration, call ipsec_hw_putnext. 26216 */ 26217 if (ire->ire_nce && 26218 ire->ire_nce->nce_state != ND_REACHABLE) { 26219 DTRACE_PROBE2(ip__wput__ipsec__bail, 26220 (ire_t *), ire, (mblk_t *), ipsec_mp); 26221 /* 26222 * If ire's link-layer is unresolved (this 26223 * would only happen if the incomplete ire 26224 * was added to cachetable via forwarding path) 26225 * don't bother going to ip_xmit_v4. Just drop the 26226 * packet. 26227 * There is a slight risk here, in that, if we 26228 * have the forwarding path create an incomplete 26229 * IRE, then until the IRE is completed, any 26230 * transmitted IPsec packets will be dropped 26231 * instead of being queued waiting for resolution. 26232 * 26233 * But the likelihood of a forwarding packet and a wput 26234 * packet sending to the same dst at the same time 26235 * and there not yet be an ARP entry for it is small. 26236 * Furthermore, if this actually happens, it might 26237 * be likely that wput would generate multiple 26238 * packets (and forwarding would also have a train 26239 * of packets) for that destination. If this is 26240 * the case, some of them would have been dropped 26241 * anyway, since ARP only queues a few packets while 26242 * waiting for resolution 26243 * 26244 * NOTE: We should really call ip_xmit_v4, 26245 * and let it queue the packet and send the 26246 * ARP query and have ARP come back thus: 26247 * <ARP> ip_wput->ip_output->ip-wput_nondata-> 26248 * ip_xmit_v4->ip_wput_attach_llhdr + ipsec 26249 * hw accel work. But it's too complex to get 26250 * the IPsec hw acceleration approach to fit 26251 * well with ip_xmit_v4 doing ARP without 26252 * doing IPsec simplification. For now, we just 26253 * poke ip_xmit_v4 to trigger the arp resolve, so 26254 * that we can continue with the send on the next 26255 * attempt. 26256 * 26257 * XXX THis should be revisited, when 26258 * the IPsec/IP interaction is cleaned up 26259 */ 26260 ip1dbg(("ip_wput_ipsec_out: ire is incomplete" 26261 " - dropping packet\n")); 26262 freemsg(ipsec_mp); 26263 /* 26264 * Call ip_xmit_v4() to trigger ARP query 26265 * in case the nce_state is ND_INITIAL 26266 */ 26267 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 26268 goto drop_pkt; 26269 } 26270 26271 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 26272 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha1, 26273 mblk_t *, ipsec_mp); 26274 FW_HOOKS(ipst->ips_ip4_physical_out_event, 26275 ipst->ips_ipv4firewall_physical_out, 26276 NULL, ire->ire_ipif->ipif_ill, ipha1, ipsec_mp, mp, ipst); 26277 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, ipsec_mp); 26278 if (ipsec_mp == NULL) 26279 goto drop_pkt; 26280 26281 ip1dbg(("ip_wput_ipsec_out: calling ip_xmit_v4\n")); 26282 pktxmit_state = ip_xmit_v4(mp, ire, 26283 (io->ipsec_out_accelerated ? io : NULL), B_FALSE); 26284 26285 if ((pktxmit_state == SEND_FAILED) || 26286 (pktxmit_state == LLHDR_RESLV_FAILED)) { 26287 26288 freeb(ipsec_mp); /* ip_xmit_v4 frees the mp */ 26289 drop_pkt: 26290 BUMP_MIB(((ill_t *)ire->ire_stq->q_ptr)->ill_ip_mib, 26291 ipIfStatsOutDiscards); 26292 if (ire_need_rele) 26293 ire_refrele(ire); 26294 if (ire1 != NULL) { 26295 ire_refrele(ire1); 26296 freemsg(next_mp); 26297 } 26298 goto done; 26299 } 26300 26301 freeb(ipsec_mp); 26302 if (ire_need_rele) 26303 ire_refrele(ire); 26304 26305 if (ire1 != NULL) { 26306 ire = ire1; 26307 ire_need_rele = B_TRUE; 26308 ASSERT(next_mp); 26309 ipsec_mp = next_mp; 26310 mp = ipsec_mp->b_cont; 26311 ire1 = NULL; 26312 next_mp = NULL; 26313 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26314 } else { 26315 multirt_send = B_FALSE; 26316 } 26317 } while (multirt_send); 26318 done: 26319 if (ill != NULL && ill_need_rele) 26320 ill_refrele(ill); 26321 if (ipif != NULL) 26322 ipif_refrele(ipif); 26323 } 26324 26325 /* 26326 * Get the ill corresponding to the specified ire, and compare its 26327 * capabilities with the protocol and algorithms specified by the 26328 * the SA obtained from ipsec_out. If they match, annotate the 26329 * ipsec_out structure to indicate that the packet needs acceleration. 26330 * 26331 * 26332 * A packet is eligible for outbound hardware acceleration if the 26333 * following conditions are satisfied: 26334 * 26335 * 1. the packet will not be fragmented 26336 * 2. the provider supports the algorithm 26337 * 3. there is no pending control message being exchanged 26338 * 4. snoop is not attached 26339 * 5. the destination address is not a broadcast or multicast address. 26340 * 26341 * Rationale: 26342 * - Hardware drivers do not support fragmentation with 26343 * the current interface. 26344 * - snoop, multicast, and broadcast may result in exposure of 26345 * a cleartext datagram. 26346 * We check all five of these conditions here. 26347 * 26348 * XXX would like to nuke "ire_t *" parameter here; problem is that 26349 * IRE is only way to figure out if a v4 address is a broadcast and 26350 * thus ineligible for acceleration... 26351 */ 26352 static void 26353 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 26354 { 26355 ipsec_out_t *io; 26356 mblk_t *data_mp; 26357 uint_t plen, overhead; 26358 ip_stack_t *ipst; 26359 26360 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 26361 return; 26362 26363 if (ill == NULL) 26364 return; 26365 ipst = ill->ill_ipst; 26366 /* 26367 * Destination address is a broadcast or multicast. Punt. 26368 */ 26369 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 26370 IRE_LOCAL))) 26371 return; 26372 26373 data_mp = ipsec_mp->b_cont; 26374 26375 if (ill->ill_isv6) { 26376 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 26377 26378 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 26379 return; 26380 26381 plen = ip6h->ip6_plen; 26382 } else { 26383 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 26384 26385 if (CLASSD(ipha->ipha_dst)) 26386 return; 26387 26388 plen = ipha->ipha_length; 26389 } 26390 /* 26391 * Is there a pending DLPI control message being exchanged 26392 * between IP/IPsec and the DLS Provider? If there is, it 26393 * could be a SADB update, and the state of the DLS Provider 26394 * SADB might not be in sync with the SADB maintained by 26395 * IPsec. To avoid dropping packets or using the wrong keying 26396 * material, we do not accelerate this packet. 26397 */ 26398 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 26399 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26400 "ill_dlpi_pending! don't accelerate packet\n")); 26401 return; 26402 } 26403 26404 /* 26405 * Is the Provider in promiscous mode? If it does, we don't 26406 * accelerate the packet since it will bounce back up to the 26407 * listeners in the clear. 26408 */ 26409 if (ill->ill_promisc_on_phys) { 26410 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26411 "ill in promiscous mode, don't accelerate packet\n")); 26412 return; 26413 } 26414 26415 /* 26416 * Will the packet require fragmentation? 26417 */ 26418 26419 /* 26420 * IPsec ESP note: this is a pessimistic estimate, but the same 26421 * as is used elsewhere. 26422 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 26423 * + 2-byte trailer 26424 */ 26425 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 26426 IPSEC_BASE_ESP_HDR_SIZE(sa); 26427 26428 if ((plen + overhead) > ill->ill_max_mtu) 26429 return; 26430 26431 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26432 26433 /* 26434 * Can the ill accelerate this IPsec protocol and algorithm 26435 * specified by the SA? 26436 */ 26437 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 26438 ill->ill_isv6, sa, ipst->ips_netstack)) { 26439 return; 26440 } 26441 26442 /* 26443 * Tell AH or ESP that the outbound ill is capable of 26444 * accelerating this packet. 26445 */ 26446 io->ipsec_out_is_capab_ill = B_TRUE; 26447 } 26448 26449 /* 26450 * Select which AH & ESP SA's to use (if any) for the outbound packet. 26451 * 26452 * If this function returns B_TRUE, the requested SA's have been filled 26453 * into the ipsec_out_*_sa pointers. 26454 * 26455 * If the function returns B_FALSE, the packet has been "consumed", most 26456 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 26457 * 26458 * The SA references created by the protocol-specific "select" 26459 * function will be released when the ipsec_mp is freed, thanks to the 26460 * ipsec_out_free destructor -- see spd.c. 26461 */ 26462 static boolean_t 26463 ipsec_out_select_sa(mblk_t *ipsec_mp) 26464 { 26465 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 26466 ipsec_out_t *io; 26467 ipsec_policy_t *pp; 26468 ipsec_action_t *ap; 26469 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26470 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26471 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26472 26473 if (!io->ipsec_out_secure) { 26474 /* 26475 * We came here by mistake. 26476 * Don't bother with ipsec processing 26477 * We should "discourage" this path in the future. 26478 */ 26479 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26480 return (B_FALSE); 26481 } 26482 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26483 ASSERT((io->ipsec_out_policy != NULL) || 26484 (io->ipsec_out_act != NULL)); 26485 26486 ASSERT(io->ipsec_out_failed == B_FALSE); 26487 26488 /* 26489 * IPsec processing has started. 26490 */ 26491 io->ipsec_out_proc_begin = B_TRUE; 26492 ap = io->ipsec_out_act; 26493 if (ap == NULL) { 26494 pp = io->ipsec_out_policy; 26495 ASSERT(pp != NULL); 26496 ap = pp->ipsp_act; 26497 ASSERT(ap != NULL); 26498 } 26499 26500 /* 26501 * We have an action. now, let's select SA's. 26502 * (In the future, we can cache this in the conn_t..) 26503 */ 26504 if (ap->ipa_want_esp) { 26505 if (io->ipsec_out_esp_sa == NULL) { 26506 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 26507 IPPROTO_ESP); 26508 } 26509 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 26510 } 26511 26512 if (ap->ipa_want_ah) { 26513 if (io->ipsec_out_ah_sa == NULL) { 26514 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 26515 IPPROTO_AH); 26516 } 26517 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 26518 /* 26519 * The ESP and AH processing order needs to be preserved 26520 * when both protocols are required (ESP should be applied 26521 * before AH for an outbound packet). Force an ESP ACQUIRE 26522 * when both ESP and AH are required, and an AH ACQUIRE 26523 * is needed. 26524 */ 26525 if (ap->ipa_want_esp && need_ah_acquire) 26526 need_esp_acquire = B_TRUE; 26527 } 26528 26529 /* 26530 * Send an ACQUIRE (extended, regular, or both) if we need one. 26531 * Release SAs that got referenced, but will not be used until we 26532 * acquire _all_ of the SAs we need. 26533 */ 26534 if (need_ah_acquire || need_esp_acquire) { 26535 if (io->ipsec_out_ah_sa != NULL) { 26536 IPSA_REFRELE(io->ipsec_out_ah_sa); 26537 io->ipsec_out_ah_sa = NULL; 26538 } 26539 if (io->ipsec_out_esp_sa != NULL) { 26540 IPSA_REFRELE(io->ipsec_out_esp_sa); 26541 io->ipsec_out_esp_sa = NULL; 26542 } 26543 26544 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 26545 return (B_FALSE); 26546 } 26547 26548 return (B_TRUE); 26549 } 26550 26551 /* 26552 * Process an IPSEC_OUT message and see what you can 26553 * do with it. 26554 * IPQoS Notes: 26555 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 26556 * IPsec. 26557 * XXX would like to nuke ire_t. 26558 * XXX ill_index better be "real" 26559 */ 26560 void 26561 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 26562 { 26563 ipsec_out_t *io; 26564 ipsec_policy_t *pp; 26565 ipsec_action_t *ap; 26566 ipha_t *ipha; 26567 ip6_t *ip6h; 26568 mblk_t *mp; 26569 ill_t *ill; 26570 zoneid_t zoneid; 26571 ipsec_status_t ipsec_rc; 26572 boolean_t ill_need_rele = B_FALSE; 26573 ip_stack_t *ipst; 26574 ipsec_stack_t *ipss; 26575 26576 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26577 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26578 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26579 ipst = io->ipsec_out_ns->netstack_ip; 26580 mp = ipsec_mp->b_cont; 26581 26582 /* 26583 * Initiate IPPF processing. We do it here to account for packets 26584 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 26585 * We can check for ipsec_out_proc_begin even for such packets, as 26586 * they will always be false (asserted below). 26587 */ 26588 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst) && !io->ipsec_out_proc_begin) { 26589 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 26590 io->ipsec_out_ill_index : ill_index); 26591 if (mp == NULL) { 26592 ip2dbg(("ipsec_out_process: packet dropped "\ 26593 "during IPPF processing\n")); 26594 freeb(ipsec_mp); 26595 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26596 return; 26597 } 26598 } 26599 26600 if (!io->ipsec_out_secure) { 26601 /* 26602 * We came here by mistake. 26603 * Don't bother with ipsec processing 26604 * Should "discourage" this path in the future. 26605 */ 26606 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26607 goto done; 26608 } 26609 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26610 ASSERT((io->ipsec_out_policy != NULL) || 26611 (io->ipsec_out_act != NULL)); 26612 ASSERT(io->ipsec_out_failed == B_FALSE); 26613 26614 ipss = ipst->ips_netstack->netstack_ipsec; 26615 if (!ipsec_loaded(ipss)) { 26616 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 26617 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26618 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26619 } else { 26620 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); 26621 } 26622 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 26623 DROPPER(ipss, ipds_ip_ipsec_not_loaded), 26624 &ipss->ipsec_dropper); 26625 return; 26626 } 26627 26628 /* 26629 * IPsec processing has started. 26630 */ 26631 io->ipsec_out_proc_begin = B_TRUE; 26632 ap = io->ipsec_out_act; 26633 if (ap == NULL) { 26634 pp = io->ipsec_out_policy; 26635 ASSERT(pp != NULL); 26636 ap = pp->ipsp_act; 26637 ASSERT(ap != NULL); 26638 } 26639 26640 /* 26641 * Save the outbound ill index. When the packet comes back 26642 * from IPsec, we make sure the ill hasn't changed or disappeared 26643 * before sending it the accelerated packet. 26644 */ 26645 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 26646 int ifindex; 26647 ill = ire_to_ill(ire); 26648 ifindex = ill->ill_phyint->phyint_ifindex; 26649 io->ipsec_out_capab_ill_index = ifindex; 26650 } 26651 26652 /* 26653 * The order of processing is first insert a IP header if needed. 26654 * Then insert the ESP header and then the AH header. 26655 */ 26656 if ((io->ipsec_out_se_done == B_FALSE) && 26657 (ap->ipa_want_se)) { 26658 /* 26659 * First get the outer IP header before sending 26660 * it to ESP. 26661 */ 26662 ipha_t *oipha, *iipha; 26663 mblk_t *outer_mp, *inner_mp; 26664 26665 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 26666 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 26667 "ipsec_out_process: " 26668 "Self-Encapsulation failed: Out of memory\n"); 26669 freemsg(ipsec_mp); 26670 if (ill != NULL) { 26671 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26672 } else { 26673 BUMP_MIB(&ipst->ips_ip_mib, 26674 ipIfStatsOutDiscards); 26675 } 26676 return; 26677 } 26678 inner_mp = ipsec_mp->b_cont; 26679 ASSERT(inner_mp->b_datap->db_type == M_DATA); 26680 oipha = (ipha_t *)outer_mp->b_rptr; 26681 iipha = (ipha_t *)inner_mp->b_rptr; 26682 *oipha = *iipha; 26683 outer_mp->b_wptr += sizeof (ipha_t); 26684 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 26685 sizeof (ipha_t)); 26686 oipha->ipha_protocol = IPPROTO_ENCAP; 26687 oipha->ipha_version_and_hdr_length = 26688 IP_SIMPLE_HDR_VERSION; 26689 oipha->ipha_hdr_checksum = 0; 26690 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 26691 outer_mp->b_cont = inner_mp; 26692 ipsec_mp->b_cont = outer_mp; 26693 26694 io->ipsec_out_se_done = B_TRUE; 26695 io->ipsec_out_tunnel = B_TRUE; 26696 } 26697 26698 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 26699 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 26700 !ipsec_out_select_sa(ipsec_mp)) 26701 return; 26702 26703 /* 26704 * By now, we know what SA's to use. Toss over to ESP & AH 26705 * to do the heavy lifting. 26706 */ 26707 zoneid = io->ipsec_out_zoneid; 26708 ASSERT(zoneid != ALL_ZONES); 26709 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 26710 ASSERT(io->ipsec_out_esp_sa != NULL); 26711 io->ipsec_out_esp_done = B_TRUE; 26712 /* 26713 * Note that since hw accel can only apply one transform, 26714 * not two, we skip hw accel for ESP if we also have AH 26715 * This is an design limitation of the interface 26716 * which should be revisited. 26717 */ 26718 ASSERT(ire != NULL); 26719 if (io->ipsec_out_ah_sa == NULL) { 26720 ill = (ill_t *)ire->ire_stq->q_ptr; 26721 ipsec_out_is_accelerated(ipsec_mp, 26722 io->ipsec_out_esp_sa, ill, ire); 26723 } 26724 26725 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 26726 switch (ipsec_rc) { 26727 case IPSEC_STATUS_SUCCESS: 26728 break; 26729 case IPSEC_STATUS_FAILED: 26730 if (ill != NULL) { 26731 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26732 } else { 26733 BUMP_MIB(&ipst->ips_ip_mib, 26734 ipIfStatsOutDiscards); 26735 } 26736 /* FALLTHRU */ 26737 case IPSEC_STATUS_PENDING: 26738 return; 26739 } 26740 } 26741 26742 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 26743 ASSERT(io->ipsec_out_ah_sa != NULL); 26744 io->ipsec_out_ah_done = B_TRUE; 26745 if (ire == NULL) { 26746 int idx = io->ipsec_out_capab_ill_index; 26747 ill = ill_lookup_on_ifindex(idx, B_FALSE, 26748 NULL, NULL, NULL, NULL, ipst); 26749 ill_need_rele = B_TRUE; 26750 } else { 26751 ill = (ill_t *)ire->ire_stq->q_ptr; 26752 } 26753 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 26754 ire); 26755 26756 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 26757 switch (ipsec_rc) { 26758 case IPSEC_STATUS_SUCCESS: 26759 break; 26760 case IPSEC_STATUS_FAILED: 26761 if (ill != NULL) { 26762 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26763 } else { 26764 BUMP_MIB(&ipst->ips_ip_mib, 26765 ipIfStatsOutDiscards); 26766 } 26767 /* FALLTHRU */ 26768 case IPSEC_STATUS_PENDING: 26769 if (ill != NULL && ill_need_rele) 26770 ill_refrele(ill); 26771 return; 26772 } 26773 } 26774 /* 26775 * We are done with IPsec processing. Send it over 26776 * the wire. 26777 */ 26778 done: 26779 mp = ipsec_mp->b_cont; 26780 ipha = (ipha_t *)mp->b_rptr; 26781 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26782 ip_wput_ipsec_out(q, ipsec_mp, ipha, ill, ire); 26783 } else { 26784 ip6h = (ip6_t *)ipha; 26785 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ill, ire); 26786 } 26787 if (ill != NULL && ill_need_rele) 26788 ill_refrele(ill); 26789 } 26790 26791 /* ARGSUSED */ 26792 void 26793 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 26794 { 26795 opt_restart_t *or; 26796 int err; 26797 conn_t *connp; 26798 26799 ASSERT(CONN_Q(q)); 26800 connp = Q_TO_CONN(q); 26801 26802 ASSERT(first_mp->b_datap->db_type == M_CTL); 26803 or = (opt_restart_t *)first_mp->b_rptr; 26804 /* 26805 * We don't need to pass any credentials here since this is just 26806 * a restart. The credentials are passed in when svr4_optcom_req 26807 * is called the first time (from ip_wput_nondata). 26808 */ 26809 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 26810 err = svr4_optcom_req(q, first_mp, NULL, 26811 &ip_opt_obj); 26812 } else { 26813 ASSERT(or->or_type == T_OPTMGMT_REQ); 26814 err = tpi_optcom_req(q, first_mp, NULL, 26815 &ip_opt_obj); 26816 } 26817 if (err != EINPROGRESS) { 26818 /* operation is done */ 26819 CONN_OPER_PENDING_DONE(connp); 26820 } 26821 } 26822 26823 /* 26824 * ioctls that go through a down/up sequence may need to wait for the down 26825 * to complete. This involves waiting for the ire and ipif refcnts to go down 26826 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 26827 */ 26828 /* ARGSUSED */ 26829 void 26830 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 26831 { 26832 struct iocblk *iocp; 26833 mblk_t *mp1; 26834 ip_ioctl_cmd_t *ipip; 26835 int err; 26836 sin_t *sin; 26837 struct lifreq *lifr; 26838 struct ifreq *ifr; 26839 26840 iocp = (struct iocblk *)mp->b_rptr; 26841 ASSERT(ipsq != NULL); 26842 /* Existence of mp1 verified in ip_wput_nondata */ 26843 mp1 = mp->b_cont->b_cont; 26844 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26845 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 26846 /* 26847 * Special case where ipsq_current_ipif is not set: 26848 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 26849 * ill could also have become part of a ipmp group in the 26850 * process, we are here as were not able to complete the 26851 * operation in ipif_set_values because we could not become 26852 * exclusive on the new ipsq, In such a case ipsq_current_ipif 26853 * will not be set so we need to set it. 26854 */ 26855 ill_t *ill = q->q_ptr; 26856 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd); 26857 } 26858 ASSERT(ipsq->ipsq_current_ipif != NULL); 26859 26860 if (ipip->ipi_cmd_type == IF_CMD) { 26861 /* This a old style SIOC[GS]IF* command */ 26862 ifr = (struct ifreq *)mp1->b_rptr; 26863 sin = (sin_t *)&ifr->ifr_addr; 26864 } else if (ipip->ipi_cmd_type == LIF_CMD) { 26865 /* This a new style SIOC[GS]LIF* command */ 26866 lifr = (struct lifreq *)mp1->b_rptr; 26867 sin = (sin_t *)&lifr->lifr_addr; 26868 } else { 26869 sin = NULL; 26870 } 26871 26872 err = (*ipip->ipi_func_restart)(ipsq->ipsq_current_ipif, sin, q, mp, 26873 ipip, mp1->b_rptr); 26874 26875 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 26876 } 26877 26878 /* 26879 * ioctl processing 26880 * 26881 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up 26882 * the ioctl command in the ioctl tables, determines the copyin data size 26883 * from the ipi_copyin_size field, and does an mi_copyin() of that size. 26884 * 26885 * ioctl processing then continues when the M_IOCDATA makes its way down to 26886 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its 26887 * associated 'conn' is refheld till the end of the ioctl and the general 26888 * ioctl processing function ip_process_ioctl() is called to extract the 26889 * arguments and process the ioctl. To simplify extraction, ioctl commands 26890 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a 26891 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq()) 26892 * is used to extract the ioctl's arguments. 26893 * 26894 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 26895 * so goes thru the serialization primitive ipsq_try_enter. Then the 26896 * appropriate function to handle the ioctl is called based on the entry in 26897 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 26898 * which also refreleases the 'conn' that was refheld at the start of the 26899 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 26900 * 26901 * Many exclusive ioctls go thru an internal down up sequence as part of 26902 * the operation. For example an attempt to change the IP address of an 26903 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 26904 * does all the cleanup such as deleting all ires that use this address. 26905 * Then we need to wait till all references to the interface go away. 26906 */ 26907 void 26908 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 26909 { 26910 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 26911 ip_ioctl_cmd_t *ipip = arg; 26912 ip_extract_func_t *extract_funcp; 26913 cmd_info_t ci; 26914 int err; 26915 boolean_t entered_ipsq = B_FALSE; 26916 26917 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 26918 26919 if (ipip == NULL) 26920 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26921 26922 /* 26923 * SIOCLIFADDIF needs to go thru a special path since the 26924 * ill may not exist yet. This happens in the case of lo0 26925 * which is created using this ioctl. 26926 */ 26927 if (ipip->ipi_cmd == SIOCLIFADDIF) { 26928 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 26929 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26930 return; 26931 } 26932 26933 ci.ci_ipif = NULL; 26934 if (ipip->ipi_cmd_type == MISC_CMD) { 26935 /* 26936 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF. 26937 */ 26938 if (ipip->ipi_cmd == IF_UNITSEL) { 26939 /* ioctl comes down the ill */ 26940 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 26941 ipif_refhold(ci.ci_ipif); 26942 } 26943 err = 0; 26944 ci.ci_sin = NULL; 26945 ci.ci_sin6 = NULL; 26946 ci.ci_lifr = NULL; 26947 } else { 26948 switch (ipip->ipi_cmd_type) { 26949 case IF_CMD: 26950 case LIF_CMD: 26951 extract_funcp = ip_extract_lifreq; 26952 break; 26953 26954 case ARP_CMD: 26955 case XARP_CMD: 26956 extract_funcp = ip_extract_arpreq; 26957 break; 26958 26959 case TUN_CMD: 26960 extract_funcp = ip_extract_tunreq; 26961 break; 26962 26963 case MSFILT_CMD: 26964 extract_funcp = ip_extract_msfilter; 26965 break; 26966 26967 default: 26968 ASSERT(0); 26969 } 26970 26971 err = (*extract_funcp)(q, mp, ipip, &ci, ip_process_ioctl); 26972 if (err != 0) { 26973 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26974 return; 26975 } 26976 26977 /* 26978 * All of the extraction functions return a refheld ipif. 26979 */ 26980 ASSERT(ci.ci_ipif != NULL); 26981 } 26982 26983 /* 26984 * If ipsq is non-null, we are already being called exclusively 26985 */ 26986 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 26987 if (!(ipip->ipi_flags & IPI_WR)) { 26988 /* 26989 * A return value of EINPROGRESS means the ioctl is 26990 * either queued and waiting for some reason or has 26991 * already completed. 26992 */ 26993 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 26994 ci.ci_lifr); 26995 if (ci.ci_ipif != NULL) 26996 ipif_refrele(ci.ci_ipif); 26997 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26998 return; 26999 } 27000 27001 ASSERT(ci.ci_ipif != NULL); 27002 27003 if (ipsq == NULL) { 27004 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, 27005 ip_process_ioctl, NEW_OP, B_TRUE); 27006 entered_ipsq = B_TRUE; 27007 } 27008 /* 27009 * Release the ipif so that ipif_down and friends that wait for 27010 * references to go away are not misled about the current ipif_refcnt 27011 * values. We are writer so we can access the ipif even after releasing 27012 * the ipif. 27013 */ 27014 ipif_refrele(ci.ci_ipif); 27015 if (ipsq == NULL) 27016 return; 27017 27018 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd); 27019 27020 /* 27021 * For most set ioctls that come here, this serves as a single point 27022 * where we set the IPIF_CHANGING flag. This ensures that there won't 27023 * be any new references to the ipif. This helps functions that go 27024 * through this path and end up trying to wait for the refcnts 27025 * associated with the ipif to go down to zero. Some exceptions are 27026 * Failover, Failback, and Groupname commands that operate on more than 27027 * just the ci.ci_ipif. These commands internally determine the 27028 * set of ipif's they operate on and set and clear the IPIF_CHANGING 27029 * flags on that set. Another exception is the Removeif command that 27030 * sets the IPIF_CONDEMNED flag internally after identifying the right 27031 * ipif to operate on. 27032 */ 27033 mutex_enter(&(ci.ci_ipif)->ipif_ill->ill_lock); 27034 if (ipip->ipi_cmd != SIOCLIFREMOVEIF && 27035 ipip->ipi_cmd != SIOCLIFFAILOVER && 27036 ipip->ipi_cmd != SIOCLIFFAILBACK && 27037 ipip->ipi_cmd != SIOCSLIFGROUPNAME) 27038 (ci.ci_ipif)->ipif_state_flags |= IPIF_CHANGING; 27039 mutex_exit(&(ci.ci_ipif)->ipif_ill->ill_lock); 27040 27041 /* 27042 * A return value of EINPROGRESS means the ioctl is 27043 * either queued and waiting for some reason or has 27044 * already completed. 27045 */ 27046 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr); 27047 27048 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 27049 27050 if (entered_ipsq) 27051 ipsq_exit(ipsq, B_TRUE, B_TRUE); 27052 } 27053 27054 /* 27055 * Complete the ioctl. Typically ioctls use the mi package and need to 27056 * do mi_copyout/mi_copy_done. 27057 */ 27058 void 27059 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq) 27060 { 27061 conn_t *connp = NULL; 27062 27063 if (err == EINPROGRESS) 27064 return; 27065 27066 if (CONN_Q(q)) { 27067 connp = Q_TO_CONN(q); 27068 ASSERT(connp->conn_ref >= 2); 27069 } 27070 27071 switch (mode) { 27072 case COPYOUT: 27073 if (err == 0) 27074 mi_copyout(q, mp); 27075 else 27076 mi_copy_done(q, mp, err); 27077 break; 27078 27079 case NO_COPYOUT: 27080 mi_copy_done(q, mp, err); 27081 break; 27082 27083 default: 27084 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */ 27085 break; 27086 } 27087 27088 /* 27089 * The refhold placed at the start of the ioctl is released here. 27090 */ 27091 if (connp != NULL) 27092 CONN_OPER_PENDING_DONE(connp); 27093 27094 if (ipsq != NULL) 27095 ipsq_current_finish(ipsq); 27096 } 27097 27098 /* 27099 * This is called from ip_wput_nondata to resume a deferred TCP bind. 27100 */ 27101 /* ARGSUSED */ 27102 void 27103 ip_resume_tcp_bind(void *arg, mblk_t *mp, void *arg2) 27104 { 27105 conn_t *connp = arg; 27106 tcp_t *tcp; 27107 27108 ASSERT(connp != NULL && IPCL_IS_TCP(connp) && connp->conn_tcp != NULL); 27109 tcp = connp->conn_tcp; 27110 27111 if (connp->conn_tcp->tcp_state == TCPS_CLOSED) 27112 freemsg(mp); 27113 else 27114 tcp_rput_other(tcp, mp); 27115 CONN_OPER_PENDING_DONE(connp); 27116 } 27117 27118 /* Called from ip_wput for all non data messages */ 27119 /* ARGSUSED */ 27120 void 27121 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 27122 { 27123 mblk_t *mp1; 27124 ire_t *ire, *fake_ire; 27125 ill_t *ill; 27126 struct iocblk *iocp; 27127 ip_ioctl_cmd_t *ipip; 27128 cred_t *cr; 27129 conn_t *connp; 27130 int err; 27131 nce_t *nce; 27132 ipif_t *ipif; 27133 ip_stack_t *ipst; 27134 char *proto_str; 27135 27136 if (CONN_Q(q)) { 27137 connp = Q_TO_CONN(q); 27138 ipst = connp->conn_netstack->netstack_ip; 27139 } else { 27140 connp = NULL; 27141 ipst = ILLQ_TO_IPST(q); 27142 } 27143 27144 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 27145 27146 /* Check if it is a queue to /dev/sctp. */ 27147 if (connp != NULL && connp->conn_ulp == IPPROTO_SCTP && 27148 connp->conn_rq == NULL) { 27149 sctp_wput(q, mp); 27150 return; 27151 } 27152 27153 switch (DB_TYPE(mp)) { 27154 case M_IOCTL: 27155 /* 27156 * IOCTL processing begins in ip_sioctl_copyin_setup which 27157 * will arrange to copy in associated control structures. 27158 */ 27159 ip_sioctl_copyin_setup(q, mp); 27160 return; 27161 case M_IOCDATA: 27162 /* 27163 * Ensure that this is associated with one of our trans- 27164 * parent ioctls. If it's not ours, discard it if we're 27165 * running as a driver, or pass it on if we're a module. 27166 */ 27167 iocp = (struct iocblk *)mp->b_rptr; 27168 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27169 if (ipip == NULL) { 27170 if (q->q_next == NULL) { 27171 goto nak; 27172 } else { 27173 putnext(q, mp); 27174 } 27175 return; 27176 } 27177 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) { 27178 /* 27179 * the ioctl is one we recognise, but is not 27180 * consumed by IP as a module, pass M_IOCDATA 27181 * for processing downstream, but only for 27182 * common Streams ioctls. 27183 */ 27184 if (ipip->ipi_flags & IPI_PASS_DOWN) { 27185 putnext(q, mp); 27186 return; 27187 } else { 27188 goto nak; 27189 } 27190 } 27191 27192 /* IOCTL continuation following copyin or copyout. */ 27193 if (mi_copy_state(q, mp, NULL) == -1) { 27194 /* 27195 * The copy operation failed. mi_copy_state already 27196 * cleaned up, so we're out of here. 27197 */ 27198 return; 27199 } 27200 /* 27201 * If we just completed a copy in, we become writer and 27202 * continue processing in ip_sioctl_copyin_done. If it 27203 * was a copy out, we call mi_copyout again. If there is 27204 * nothing more to copy out, it will complete the IOCTL. 27205 */ 27206 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 27207 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 27208 mi_copy_done(q, mp, EPROTO); 27209 return; 27210 } 27211 /* 27212 * Check for cases that need more copying. A return 27213 * value of 0 means a second copyin has been started, 27214 * so we return; a return value of 1 means no more 27215 * copying is needed, so we continue. 27216 */ 27217 if (ipip->ipi_cmd_type == MSFILT_CMD && 27218 MI_COPY_COUNT(mp) == 1) { 27219 if (ip_copyin_msfilter(q, mp) == 0) 27220 return; 27221 } 27222 /* 27223 * Refhold the conn, till the ioctl completes. This is 27224 * needed in case the ioctl ends up in the pending mp 27225 * list. Every mp in the ill_pending_mp list and 27226 * the ipsq_pending_mp must have a refhold on the conn 27227 * to resume processing. The refhold is released when 27228 * the ioctl completes. (normally or abnormally) 27229 * In all cases ip_ioctl_finish is called to finish 27230 * the ioctl. 27231 */ 27232 if (connp != NULL) { 27233 /* This is not a reentry */ 27234 ASSERT(ipsq == NULL); 27235 CONN_INC_REF(connp); 27236 } else { 27237 if (!(ipip->ipi_flags & IPI_MODOK)) { 27238 mi_copy_done(q, mp, EINVAL); 27239 return; 27240 } 27241 } 27242 27243 ip_process_ioctl(ipsq, q, mp, ipip); 27244 27245 } else { 27246 mi_copyout(q, mp); 27247 } 27248 return; 27249 nak: 27250 iocp->ioc_error = EINVAL; 27251 mp->b_datap->db_type = M_IOCNAK; 27252 iocp->ioc_count = 0; 27253 qreply(q, mp); 27254 return; 27255 27256 case M_IOCNAK: 27257 /* 27258 * The only way we could get here is if a resolver didn't like 27259 * an IOCTL we sent it. This shouldn't happen. 27260 */ 27261 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 27262 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 27263 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 27264 freemsg(mp); 27265 return; 27266 case M_IOCACK: 27267 /* /dev/ip shouldn't see this */ 27268 if (CONN_Q(q)) 27269 goto nak; 27270 27271 /* Finish socket ioctls passed through to ARP. */ 27272 ip_sioctl_iocack(q, mp); 27273 return; 27274 case M_FLUSH: 27275 if (*mp->b_rptr & FLUSHW) 27276 flushq(q, FLUSHALL); 27277 if (q->q_next) { 27278 putnext(q, mp); 27279 return; 27280 } 27281 if (*mp->b_rptr & FLUSHR) { 27282 *mp->b_rptr &= ~FLUSHW; 27283 qreply(q, mp); 27284 return; 27285 } 27286 freemsg(mp); 27287 return; 27288 case IRE_DB_REQ_TYPE: 27289 if (connp == NULL) { 27290 proto_str = "IRE_DB_REQ_TYPE"; 27291 goto protonak; 27292 } 27293 /* An Upper Level Protocol wants a copy of an IRE. */ 27294 ip_ire_req(q, mp); 27295 return; 27296 case M_CTL: 27297 if (mp->b_wptr - mp->b_rptr < sizeof (uint32_t)) 27298 break; 27299 27300 if (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == 27301 TUN_HELLO) { 27302 ASSERT(connp != NULL); 27303 connp->conn_flags |= IPCL_IPTUN; 27304 freeb(mp); 27305 return; 27306 } 27307 27308 if (connp != NULL && *(uint32_t *)mp->b_rptr == 27309 IP_ULP_OUT_LABELED) { 27310 out_labeled_t *olp; 27311 27312 if (mp->b_wptr - mp->b_rptr != sizeof (*olp)) 27313 break; 27314 olp = (out_labeled_t *)mp->b_rptr; 27315 connp->conn_ulp_labeled = olp->out_qnext == q; 27316 freemsg(mp); 27317 return; 27318 } 27319 27320 /* M_CTL messages are used by ARP to tell us things. */ 27321 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 27322 break; 27323 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 27324 case AR_ENTRY_SQUERY: 27325 ip_wput_ctl(q, mp); 27326 return; 27327 case AR_CLIENT_NOTIFY: 27328 ip_arp_news(q, mp); 27329 return; 27330 case AR_DLPIOP_DONE: 27331 ASSERT(q->q_next != NULL); 27332 ill = (ill_t *)q->q_ptr; 27333 /* qwriter_ip releases the refhold */ 27334 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 27335 ill_refhold(ill); 27336 qwriter_ip(ill, q, mp, ip_arp_done, CUR_OP, B_FALSE); 27337 return; 27338 case AR_ARP_CLOSING: 27339 /* 27340 * ARP (above us) is closing. If no ARP bringup is 27341 * currently pending, ack the message so that ARP 27342 * can complete its close. Also mark ill_arp_closing 27343 * so that new ARP bringups will fail. If any 27344 * ARP bringup is currently in progress, we will 27345 * ack this when the current ARP bringup completes. 27346 */ 27347 ASSERT(q->q_next != NULL); 27348 ill = (ill_t *)q->q_ptr; 27349 mutex_enter(&ill->ill_lock); 27350 ill->ill_arp_closing = 1; 27351 if (!ill->ill_arp_bringup_pending) { 27352 mutex_exit(&ill->ill_lock); 27353 qreply(q, mp); 27354 } else { 27355 mutex_exit(&ill->ill_lock); 27356 freemsg(mp); 27357 } 27358 return; 27359 case AR_ARP_EXTEND: 27360 /* 27361 * The ARP module above us is capable of duplicate 27362 * address detection. Old ATM drivers will not send 27363 * this message. 27364 */ 27365 ASSERT(q->q_next != NULL); 27366 ill = (ill_t *)q->q_ptr; 27367 ill->ill_arp_extend = B_TRUE; 27368 freemsg(mp); 27369 return; 27370 default: 27371 break; 27372 } 27373 break; 27374 case M_PROTO: 27375 case M_PCPROTO: 27376 /* 27377 * The only PROTO messages we expect are ULP binds and 27378 * copies of option negotiation acknowledgements. 27379 */ 27380 switch (((union T_primitives *)mp->b_rptr)->type) { 27381 case O_T_BIND_REQ: 27382 case T_BIND_REQ: { 27383 /* Request can get queued in bind */ 27384 if (connp == NULL) { 27385 proto_str = "O_T_BIND_REQ/T_BIND_REQ"; 27386 goto protonak; 27387 } 27388 /* 27389 * Both TCP and UDP call ip_bind_{v4,v6}() directly 27390 * instead of going through this path. We only get 27391 * here in the following cases: 27392 * 27393 * a. Bind retries, where ipsq is non-NULL. 27394 * b. T_BIND_REQ is issued from non TCP/UDP 27395 * transport, e.g. icmp for raw socket, 27396 * in which case ipsq will be NULL. 27397 */ 27398 ASSERT(ipsq != NULL || 27399 (!IPCL_IS_TCP(connp) && !IPCL_IS_UDP(connp))); 27400 27401 /* Don't increment refcnt if this is a re-entry */ 27402 if (ipsq == NULL) 27403 CONN_INC_REF(connp); 27404 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 27405 connp, NULL) : ip_bind_v4(q, mp, connp); 27406 if (mp == NULL) 27407 return; 27408 if (IPCL_IS_TCP(connp)) { 27409 /* 27410 * In the case of TCP endpoint we 27411 * come here only for bind retries 27412 */ 27413 ASSERT(ipsq != NULL); 27414 CONN_INC_REF(connp); 27415 squeue_fill(connp->conn_sqp, mp, 27416 ip_resume_tcp_bind, connp, 27417 SQTAG_BIND_RETRY); 27418 return; 27419 } else if (IPCL_IS_UDP(connp)) { 27420 /* 27421 * In the case of UDP endpoint we 27422 * come here only for bind retries 27423 */ 27424 ASSERT(ipsq != NULL); 27425 udp_resume_bind(connp, mp); 27426 return; 27427 } 27428 qreply(q, mp); 27429 CONN_OPER_PENDING_DONE(connp); 27430 return; 27431 } 27432 case T_SVR4_OPTMGMT_REQ: 27433 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 27434 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 27435 27436 if (connp == NULL) { 27437 proto_str = "T_SVR4_OPTMGMT_REQ"; 27438 goto protonak; 27439 } 27440 27441 if (!snmpcom_req(q, mp, ip_snmp_set, 27442 ip_snmp_get, cr)) { 27443 /* 27444 * Call svr4_optcom_req so that it can 27445 * generate the ack. We don't come here 27446 * if this operation is being restarted. 27447 * ip_restart_optmgmt will drop the conn ref. 27448 * In the case of ipsec option after the ipsec 27449 * load is complete conn_restart_ipsec_waiter 27450 * drops the conn ref. 27451 */ 27452 ASSERT(ipsq == NULL); 27453 CONN_INC_REF(connp); 27454 if (ip_check_for_ipsec_opt(q, mp)) 27455 return; 27456 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj); 27457 if (err != EINPROGRESS) { 27458 /* Operation is done */ 27459 CONN_OPER_PENDING_DONE(connp); 27460 } 27461 } 27462 return; 27463 case T_OPTMGMT_REQ: 27464 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 27465 /* 27466 * Note: No snmpcom_req support through new 27467 * T_OPTMGMT_REQ. 27468 * Call tpi_optcom_req so that it can 27469 * generate the ack. 27470 */ 27471 if (connp == NULL) { 27472 proto_str = "T_OPTMGMT_REQ"; 27473 goto protonak; 27474 } 27475 27476 ASSERT(ipsq == NULL); 27477 /* 27478 * We don't come here for restart. ip_restart_optmgmt 27479 * will drop the conn ref. In the case of ipsec option 27480 * after the ipsec load is complete 27481 * conn_restart_ipsec_waiter drops the conn ref. 27482 */ 27483 CONN_INC_REF(connp); 27484 if (ip_check_for_ipsec_opt(q, mp)) 27485 return; 27486 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj); 27487 if (err != EINPROGRESS) { 27488 /* Operation is done */ 27489 CONN_OPER_PENDING_DONE(connp); 27490 } 27491 return; 27492 case T_UNBIND_REQ: 27493 if (connp == NULL) { 27494 proto_str = "T_UNBIND_REQ"; 27495 goto protonak; 27496 } 27497 mp = ip_unbind(q, mp); 27498 qreply(q, mp); 27499 return; 27500 default: 27501 /* 27502 * Have to drop any DLPI messages coming down from 27503 * arp (such as an info_req which would cause ip 27504 * to receive an extra info_ack if it was passed 27505 * through. 27506 */ 27507 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 27508 (int)*(uint_t *)mp->b_rptr)); 27509 freemsg(mp); 27510 return; 27511 } 27512 /* NOTREACHED */ 27513 case IRE_DB_TYPE: { 27514 nce_t *nce; 27515 ill_t *ill; 27516 in6_addr_t gw_addr_v6; 27517 27518 27519 /* 27520 * This is a response back from a resolver. It 27521 * consists of a message chain containing: 27522 * IRE_MBLK-->LL_HDR_MBLK->pkt 27523 * The IRE_MBLK is the one we allocated in ip_newroute. 27524 * The LL_HDR_MBLK is the DLPI header to use to get 27525 * the attached packet, and subsequent ones for the 27526 * same destination, transmitted. 27527 */ 27528 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 27529 break; 27530 /* 27531 * First, check to make sure the resolution succeeded. 27532 * If it failed, the second mblk will be empty. 27533 * If it is, free the chain, dropping the packet. 27534 * (We must ire_delete the ire; that frees the ire mblk) 27535 * We're doing this now to support PVCs for ATM; it's 27536 * a partial xresolv implementation. When we fully implement 27537 * xresolv interfaces, instead of freeing everything here 27538 * we'll initiate neighbor discovery. 27539 * 27540 * For v4 (ARP and other external resolvers) the resolver 27541 * frees the message, so no check is needed. This check 27542 * is required, though, for a full xresolve implementation. 27543 * Including this code here now both shows how external 27544 * resolvers can NACK a resolution request using an 27545 * existing design that has no specific provisions for NACKs, 27546 * and also takes into account that the current non-ARP 27547 * external resolver has been coded to use this method of 27548 * NACKing for all IPv6 (xresolv) cases, 27549 * whether our xresolv implementation is complete or not. 27550 * 27551 */ 27552 ire = (ire_t *)mp->b_rptr; 27553 ill = ire_to_ill(ire); 27554 mp1 = mp->b_cont; /* dl_unitdata_req */ 27555 if (mp1->b_rptr == mp1->b_wptr) { 27556 if (ire->ire_ipversion == IPV6_VERSION) { 27557 /* 27558 * XRESOLV interface. 27559 */ 27560 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27561 mutex_enter(&ire->ire_lock); 27562 gw_addr_v6 = ire->ire_gateway_addr_v6; 27563 mutex_exit(&ire->ire_lock); 27564 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27565 nce = ndp_lookup_v6(ill, 27566 &ire->ire_addr_v6, B_FALSE); 27567 } else { 27568 nce = ndp_lookup_v6(ill, &gw_addr_v6, 27569 B_FALSE); 27570 } 27571 if (nce != NULL) { 27572 nce_resolv_failed(nce); 27573 ndp_delete(nce); 27574 NCE_REFRELE(nce); 27575 } 27576 } 27577 mp->b_cont = NULL; 27578 freemsg(mp1); /* frees the pkt as well */ 27579 ASSERT(ire->ire_nce == NULL); 27580 ire_delete((ire_t *)mp->b_rptr); 27581 return; 27582 } 27583 27584 /* 27585 * Split them into IRE_MBLK and pkt and feed it into 27586 * ire_add_then_send. Then in ire_add_then_send 27587 * the IRE will be added, and then the packet will be 27588 * run back through ip_wput. This time it will make 27589 * it to the wire. 27590 */ 27591 mp->b_cont = NULL; 27592 mp = mp1->b_cont; /* now, mp points to pkt */ 27593 mp1->b_cont = NULL; 27594 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 27595 if (ire->ire_ipversion == IPV6_VERSION) { 27596 /* 27597 * XRESOLV interface. Find the nce and put a copy 27598 * of the dl_unitdata_req in nce_res_mp 27599 */ 27600 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27601 mutex_enter(&ire->ire_lock); 27602 gw_addr_v6 = ire->ire_gateway_addr_v6; 27603 mutex_exit(&ire->ire_lock); 27604 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27605 nce = ndp_lookup_v6(ill, &ire->ire_addr_v6, 27606 B_FALSE); 27607 } else { 27608 nce = ndp_lookup_v6(ill, &gw_addr_v6, B_FALSE); 27609 } 27610 if (nce != NULL) { 27611 /* 27612 * We have to protect nce_res_mp here 27613 * from being accessed by other threads 27614 * while we change the mblk pointer. 27615 * Other functions will also lock the nce when 27616 * accessing nce_res_mp. 27617 * 27618 * The reason we change the mblk pointer 27619 * here rather than copying the resolved address 27620 * into the template is that, unlike with 27621 * ethernet, we have no guarantee that the 27622 * resolved address length will be 27623 * smaller than or equal to the lla length 27624 * with which the template was allocated, 27625 * (for ethernet, they're equal) 27626 * so we have to use the actual resolved 27627 * address mblk - which holds the real 27628 * dl_unitdata_req with the resolved address. 27629 * 27630 * Doing this is the same behavior as was 27631 * previously used in the v4 ARP case. 27632 */ 27633 mutex_enter(&nce->nce_lock); 27634 if (nce->nce_res_mp != NULL) 27635 freemsg(nce->nce_res_mp); 27636 nce->nce_res_mp = mp1; 27637 mutex_exit(&nce->nce_lock); 27638 /* 27639 * We do a fastpath probe here because 27640 * we have resolved the address without 27641 * using Neighbor Discovery. 27642 * In the non-XRESOLV v6 case, the fastpath 27643 * probe is done right after neighbor 27644 * discovery completes. 27645 */ 27646 if (nce->nce_res_mp != NULL) { 27647 int res; 27648 nce_fastpath_list_add(nce); 27649 res = ill_fastpath_probe(ill, 27650 nce->nce_res_mp); 27651 if (res != 0 && res != EAGAIN) 27652 nce_fastpath_list_delete(nce); 27653 } 27654 27655 ire_add_then_send(q, ire, mp); 27656 /* 27657 * Now we have to clean out any packets 27658 * that may have been queued on the nce 27659 * while it was waiting for address resolution 27660 * to complete. 27661 */ 27662 mutex_enter(&nce->nce_lock); 27663 mp1 = nce->nce_qd_mp; 27664 nce->nce_qd_mp = NULL; 27665 mutex_exit(&nce->nce_lock); 27666 while (mp1 != NULL) { 27667 mblk_t *nxt_mp; 27668 queue_t *fwdq = NULL; 27669 ill_t *inbound_ill; 27670 uint_t ifindex; 27671 27672 nxt_mp = mp1->b_next; 27673 mp1->b_next = NULL; 27674 /* 27675 * Retrieve ifindex stored in 27676 * ip_rput_data_v6() 27677 */ 27678 ifindex = 27679 (uint_t)(uintptr_t)mp1->b_prev; 27680 inbound_ill = 27681 ill_lookup_on_ifindex(ifindex, 27682 B_TRUE, NULL, NULL, NULL, 27683 NULL, ipst); 27684 mp1->b_prev = NULL; 27685 if (inbound_ill != NULL) 27686 fwdq = inbound_ill->ill_rq; 27687 27688 if (fwdq != NULL) { 27689 put(fwdq, mp1); 27690 ill_refrele(inbound_ill); 27691 } else 27692 put(WR(ill->ill_rq), mp1); 27693 mp1 = nxt_mp; 27694 } 27695 NCE_REFRELE(nce); 27696 } else { /* nce is NULL; clean up */ 27697 ire_delete(ire); 27698 freemsg(mp); 27699 freemsg(mp1); 27700 return; 27701 } 27702 } else { 27703 nce_t *arpce; 27704 /* 27705 * Link layer resolution succeeded. Recompute the 27706 * ire_nce. 27707 */ 27708 ASSERT(ire->ire_type & (IRE_CACHE|IRE_BROADCAST)); 27709 if ((arpce = ndp_lookup_v4(ill, 27710 (ire->ire_gateway_addr != INADDR_ANY ? 27711 &ire->ire_gateway_addr : &ire->ire_addr), 27712 B_FALSE)) == NULL) { 27713 freeb(ire->ire_mp); 27714 freeb(mp1); 27715 freemsg(mp); 27716 return; 27717 } 27718 mutex_enter(&arpce->nce_lock); 27719 arpce->nce_last = TICK_TO_MSEC(lbolt64); 27720 if (arpce->nce_state == ND_REACHABLE) { 27721 /* 27722 * Someone resolved this before us; 27723 * cleanup the res_mp. Since ire has 27724 * not been added yet, the call to ire_add_v4 27725 * from ire_add_then_send (when a dup is 27726 * detected) will clean up the ire. 27727 */ 27728 freeb(mp1); 27729 } else { 27730 ASSERT(arpce->nce_res_mp == NULL); 27731 arpce->nce_res_mp = mp1; 27732 arpce->nce_state = ND_REACHABLE; 27733 } 27734 mutex_exit(&arpce->nce_lock); 27735 if (ire->ire_marks & IRE_MARK_NOADD) { 27736 /* 27737 * this ire will not be added to the ire 27738 * cache table, so we can set the ire_nce 27739 * here, as there are no atomicity constraints. 27740 */ 27741 ire->ire_nce = arpce; 27742 /* 27743 * We are associating this nce with the ire 27744 * so change the nce ref taken in 27745 * ndp_lookup_v4() from 27746 * NCE_REFHOLD to NCE_REFHOLD_NOTR 27747 */ 27748 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 27749 } else { 27750 NCE_REFRELE(arpce); 27751 } 27752 ire_add_then_send(q, ire, mp); 27753 } 27754 return; /* All is well, the packet has been sent. */ 27755 } 27756 case IRE_ARPRESOLVE_TYPE: { 27757 27758 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* fake_ire */ 27759 break; 27760 mp1 = mp->b_cont; /* dl_unitdata_req */ 27761 mp->b_cont = NULL; 27762 /* 27763 * First, check to make sure the resolution succeeded. 27764 * If it failed, the second mblk will be empty. 27765 */ 27766 if (mp1->b_rptr == mp1->b_wptr) { 27767 /* cleanup the incomplete ire, free queued packets */ 27768 freemsg(mp); /* fake ire */ 27769 freeb(mp1); /* dl_unitdata response */ 27770 return; 27771 } 27772 27773 /* 27774 * update any incomplete nce_t found. we lookup the ctable 27775 * and find the nce from the ire->ire_nce because we need 27776 * to pass the ire to ip_xmit_v4 later, and can find both 27777 * ire and nce in one lookup from the ctable. 27778 */ 27779 fake_ire = (ire_t *)mp->b_rptr; 27780 /* 27781 * By the time we come back here from ARP 27782 * the logical outgoing interface of the incomplete ire 27783 * we added in ire_forward could have disappeared, 27784 * causing the incomplete ire to also have 27785 * dissapeared. So we need to retreive the 27786 * proper ipif for the ire before looking 27787 * in ctable; do the ctablelookup based on ire_ipif_seqid 27788 */ 27789 ill = q->q_ptr; 27790 27791 /* Get the outgoing ipif */ 27792 mutex_enter(&ill->ill_lock); 27793 if (ill->ill_state_flags & ILL_CONDEMNED) { 27794 mutex_exit(&ill->ill_lock); 27795 freemsg(mp); /* fake ire */ 27796 freeb(mp1); /* dl_unitdata response */ 27797 return; 27798 } 27799 ipif = ipif_lookup_seqid(ill, fake_ire->ire_ipif_seqid); 27800 27801 if (ipif == NULL) { 27802 mutex_exit(&ill->ill_lock); 27803 ip1dbg(("logical intrf to incomplete ire vanished\n")); 27804 freemsg(mp); 27805 freeb(mp1); 27806 return; 27807 } 27808 ipif_refhold_locked(ipif); 27809 mutex_exit(&ill->ill_lock); 27810 ire = ire_ctable_lookup(fake_ire->ire_addr, 27811 fake_ire->ire_gateway_addr, IRE_CACHE, 27812 ipif, fake_ire->ire_zoneid, NULL, 27813 (MATCH_IRE_GW|MATCH_IRE_IPIF|MATCH_IRE_ZONEONLY), ipst); 27814 ipif_refrele(ipif); 27815 if (ire == NULL) { 27816 /* 27817 * no ire was found; check if there is an nce 27818 * for this lookup; if it has no ire's pointing at it 27819 * cleanup. 27820 */ 27821 if ((nce = ndp_lookup_v4(ill, 27822 (fake_ire->ire_gateway_addr != INADDR_ANY ? 27823 &fake_ire->ire_gateway_addr : &fake_ire->ire_addr), 27824 B_FALSE)) != NULL) { 27825 /* 27826 * cleanup: 27827 * We check for refcnt 2 (one for the nce 27828 * hash list + 1 for the ref taken by 27829 * ndp_lookup_v4) to check that there are 27830 * no ire's pointing at the nce. 27831 */ 27832 if (nce->nce_refcnt == 2) 27833 ndp_delete(nce); 27834 NCE_REFRELE(nce); 27835 } 27836 freeb(mp1); /* dl_unitdata response */ 27837 freemsg(mp); /* fake ire */ 27838 return; 27839 } 27840 nce = ire->ire_nce; 27841 DTRACE_PROBE2(ire__arpresolve__type, 27842 ire_t *, ire, nce_t *, nce); 27843 ASSERT(nce->nce_state != ND_INITIAL); 27844 mutex_enter(&nce->nce_lock); 27845 nce->nce_last = TICK_TO_MSEC(lbolt64); 27846 if (nce->nce_state == ND_REACHABLE) { 27847 /* 27848 * Someone resolved this before us; 27849 * our response is not needed any more. 27850 */ 27851 mutex_exit(&nce->nce_lock); 27852 freeb(mp1); /* dl_unitdata response */ 27853 } else { 27854 ASSERT(nce->nce_res_mp == NULL); 27855 nce->nce_res_mp = mp1; 27856 nce->nce_state = ND_REACHABLE; 27857 mutex_exit(&nce->nce_lock); 27858 nce_fastpath(nce); 27859 } 27860 /* 27861 * The cached nce_t has been updated to be reachable; 27862 * Clear the IRE_MARK_UNCACHED flag and free the fake_ire. 27863 */ 27864 fake_ire->ire_marks &= ~IRE_MARK_UNCACHED; 27865 freemsg(mp); 27866 /* 27867 * send out queued packets. 27868 */ 27869 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 27870 27871 IRE_REFRELE(ire); 27872 return; 27873 } 27874 default: 27875 break; 27876 } 27877 if (q->q_next) { 27878 putnext(q, mp); 27879 } else 27880 freemsg(mp); 27881 return; 27882 27883 protonak: 27884 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str); 27885 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL) 27886 qreply(q, mp); 27887 } 27888 27889 /* 27890 * Process IP options in an outbound packet. Modify the destination if there 27891 * is a source route option. 27892 * Returns non-zero if something fails in which case an ICMP error has been 27893 * sent and mp freed. 27894 */ 27895 static int 27896 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 27897 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 27898 { 27899 ipoptp_t opts; 27900 uchar_t *opt; 27901 uint8_t optval; 27902 uint8_t optlen; 27903 ipaddr_t dst; 27904 intptr_t code = 0; 27905 mblk_t *mp; 27906 ire_t *ire = NULL; 27907 27908 ip2dbg(("ip_wput_options\n")); 27909 mp = ipsec_mp; 27910 if (mctl_present) { 27911 mp = ipsec_mp->b_cont; 27912 } 27913 27914 dst = ipha->ipha_dst; 27915 for (optval = ipoptp_first(&opts, ipha); 27916 optval != IPOPT_EOL; 27917 optval = ipoptp_next(&opts)) { 27918 opt = opts.ipoptp_cur; 27919 optlen = opts.ipoptp_len; 27920 ip2dbg(("ip_wput_options: opt %d, len %d\n", 27921 optval, optlen)); 27922 switch (optval) { 27923 uint32_t off; 27924 case IPOPT_SSRR: 27925 case IPOPT_LSRR: 27926 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27927 ip1dbg(( 27928 "ip_wput_options: bad option offset\n")); 27929 code = (char *)&opt[IPOPT_OLEN] - 27930 (char *)ipha; 27931 goto param_prob; 27932 } 27933 off = opt[IPOPT_OFFSET]; 27934 ip1dbg(("ip_wput_options: next hop 0x%x\n", 27935 ntohl(dst))); 27936 /* 27937 * For strict: verify that dst is directly 27938 * reachable. 27939 */ 27940 if (optval == IPOPT_SSRR) { 27941 ire = ire_ftable_lookup(dst, 0, 0, 27942 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 27943 MBLK_GETLABEL(mp), 27944 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 27945 if (ire == NULL) { 27946 ip1dbg(("ip_wput_options: SSRR not" 27947 " directly reachable: 0x%x\n", 27948 ntohl(dst))); 27949 goto bad_src_route; 27950 } 27951 ire_refrele(ire); 27952 } 27953 break; 27954 case IPOPT_RR: 27955 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27956 ip1dbg(( 27957 "ip_wput_options: bad option offset\n")); 27958 code = (char *)&opt[IPOPT_OLEN] - 27959 (char *)ipha; 27960 goto param_prob; 27961 } 27962 break; 27963 case IPOPT_TS: 27964 /* 27965 * Verify that length >=5 and that there is either 27966 * room for another timestamp or that the overflow 27967 * counter is not maxed out. 27968 */ 27969 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 27970 if (optlen < IPOPT_MINLEN_IT) { 27971 goto param_prob; 27972 } 27973 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27974 ip1dbg(( 27975 "ip_wput_options: bad option offset\n")); 27976 code = (char *)&opt[IPOPT_OFFSET] - 27977 (char *)ipha; 27978 goto param_prob; 27979 } 27980 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 27981 case IPOPT_TS_TSONLY: 27982 off = IPOPT_TS_TIMELEN; 27983 break; 27984 case IPOPT_TS_TSANDADDR: 27985 case IPOPT_TS_PRESPEC: 27986 case IPOPT_TS_PRESPEC_RFC791: 27987 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 27988 break; 27989 default: 27990 code = (char *)&opt[IPOPT_POS_OV_FLG] - 27991 (char *)ipha; 27992 goto param_prob; 27993 } 27994 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 27995 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 27996 /* 27997 * No room and the overflow counter is 15 27998 * already. 27999 */ 28000 goto param_prob; 28001 } 28002 break; 28003 } 28004 } 28005 28006 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 28007 return (0); 28008 28009 ip1dbg(("ip_wput_options: error processing IP options.")); 28010 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 28011 28012 param_prob: 28013 /* 28014 * Since ip_wput() isn't close to finished, we fill 28015 * in enough of the header for credible error reporting. 28016 */ 28017 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28018 /* Failed */ 28019 freemsg(ipsec_mp); 28020 return (-1); 28021 } 28022 icmp_param_problem(q, ipsec_mp, (uint8_t)code, zoneid, ipst); 28023 return (-1); 28024 28025 bad_src_route: 28026 /* 28027 * Since ip_wput() isn't close to finished, we fill 28028 * in enough of the header for credible error reporting. 28029 */ 28030 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28031 /* Failed */ 28032 freemsg(ipsec_mp); 28033 return (-1); 28034 } 28035 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 28036 return (-1); 28037 } 28038 28039 /* 28040 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 28041 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 28042 * thru /etc/system. 28043 */ 28044 #define CONN_MAXDRAINCNT 64 28045 28046 static void 28047 conn_drain_init(ip_stack_t *ipst) 28048 { 28049 int i; 28050 28051 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads; 28052 28053 if ((ipst->ips_conn_drain_list_cnt == 0) || 28054 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 28055 /* 28056 * Default value of the number of drainers is the 28057 * number of cpus, subject to maximum of 8 drainers. 28058 */ 28059 if (boot_max_ncpus != -1) 28060 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 28061 else 28062 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8); 28063 } 28064 28065 ipst->ips_conn_drain_list = kmem_zalloc(ipst->ips_conn_drain_list_cnt * 28066 sizeof (idl_t), KM_SLEEP); 28067 28068 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28069 mutex_init(&ipst->ips_conn_drain_list[i].idl_lock, NULL, 28070 MUTEX_DEFAULT, NULL); 28071 } 28072 } 28073 28074 static void 28075 conn_drain_fini(ip_stack_t *ipst) 28076 { 28077 int i; 28078 28079 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) 28080 mutex_destroy(&ipst->ips_conn_drain_list[i].idl_lock); 28081 kmem_free(ipst->ips_conn_drain_list, 28082 ipst->ips_conn_drain_list_cnt * sizeof (idl_t)); 28083 ipst->ips_conn_drain_list = NULL; 28084 } 28085 28086 /* 28087 * Note: For an overview of how flowcontrol is handled in IP please see the 28088 * IP Flowcontrol notes at the top of this file. 28089 * 28090 * Flow control has blocked us from proceeding. Insert the given conn in one 28091 * of the conn drain lists. These conn wq's will be qenabled later on when 28092 * STREAMS flow control does a backenable. conn_walk_drain will enable 28093 * the first conn in each of these drain lists. Each of these qenabled conns 28094 * in turn enables the next in the list, after it runs, or when it closes, 28095 * thus sustaining the drain process. 28096 * 28097 * The only possible calling sequence is ip_wsrv (on conn) -> ip_wput -> 28098 * conn_drain_insert. Thus there can be only 1 instance of conn_drain_insert 28099 * running at any time, on a given conn, since there can be only 1 service proc 28100 * running on a queue at any time. 28101 */ 28102 void 28103 conn_drain_insert(conn_t *connp) 28104 { 28105 idl_t *idl; 28106 uint_t index; 28107 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28108 28109 mutex_enter(&connp->conn_lock); 28110 if (connp->conn_state_flags & CONN_CLOSING) { 28111 /* 28112 * The conn is closing as a result of which CONN_CLOSING 28113 * is set. Return. 28114 */ 28115 mutex_exit(&connp->conn_lock); 28116 return; 28117 } else if (connp->conn_idl == NULL) { 28118 /* 28119 * Assign the next drain list round robin. We dont' use 28120 * a lock, and thus it may not be strictly round robin. 28121 * Atomicity of load/stores is enough to make sure that 28122 * conn_drain_list_index is always within bounds. 28123 */ 28124 index = ipst->ips_conn_drain_list_index; 28125 ASSERT(index < ipst->ips_conn_drain_list_cnt); 28126 connp->conn_idl = &ipst->ips_conn_drain_list[index]; 28127 index++; 28128 if (index == ipst->ips_conn_drain_list_cnt) 28129 index = 0; 28130 ipst->ips_conn_drain_list_index = index; 28131 } 28132 mutex_exit(&connp->conn_lock); 28133 28134 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28135 if ((connp->conn_drain_prev != NULL) || 28136 (connp->conn_state_flags & CONN_CLOSING)) { 28137 /* 28138 * The conn is already in the drain list, OR 28139 * the conn is closing. We need to check again for 28140 * the closing case again since close can happen 28141 * after we drop the conn_lock, and before we 28142 * acquire the CONN_DRAIN_LIST_LOCK. 28143 */ 28144 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28145 return; 28146 } else { 28147 idl = connp->conn_idl; 28148 } 28149 28150 /* 28151 * The conn is not in the drain list. Insert it at the 28152 * tail of the drain list. The drain list is circular 28153 * and doubly linked. idl_conn points to the 1st element 28154 * in the list. 28155 */ 28156 if (idl->idl_conn == NULL) { 28157 idl->idl_conn = connp; 28158 connp->conn_drain_next = connp; 28159 connp->conn_drain_prev = connp; 28160 } else { 28161 conn_t *head = idl->idl_conn; 28162 28163 connp->conn_drain_next = head; 28164 connp->conn_drain_prev = head->conn_drain_prev; 28165 head->conn_drain_prev->conn_drain_next = connp; 28166 head->conn_drain_prev = connp; 28167 } 28168 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28169 } 28170 28171 /* 28172 * This conn is closing, and we are called from ip_close. OR 28173 * This conn has been serviced by ip_wsrv, and we need to do the tail 28174 * processing. 28175 * If this conn is part of the drain list, we may need to sustain the drain 28176 * process by qenabling the next conn in the drain list. We may also need to 28177 * remove this conn from the list, if it is done. 28178 */ 28179 static void 28180 conn_drain_tail(conn_t *connp, boolean_t closing) 28181 { 28182 idl_t *idl; 28183 28184 /* 28185 * connp->conn_idl is stable at this point, and no lock is needed 28186 * to check it. If we are called from ip_close, close has already 28187 * set CONN_CLOSING, thus freezing the value of conn_idl, and 28188 * called us only because conn_idl is non-null. If we are called thru 28189 * service, conn_idl could be null, but it cannot change because 28190 * service is single-threaded per queue, and there cannot be another 28191 * instance of service trying to call conn_drain_insert on this conn 28192 * now. 28193 */ 28194 ASSERT(!closing || (connp->conn_idl != NULL)); 28195 28196 /* 28197 * If connp->conn_idl is null, the conn has not been inserted into any 28198 * drain list even once since creation of the conn. Just return. 28199 */ 28200 if (connp->conn_idl == NULL) 28201 return; 28202 28203 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28204 28205 if (connp->conn_drain_prev == NULL) { 28206 /* This conn is currently not in the drain list. */ 28207 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28208 return; 28209 } 28210 idl = connp->conn_idl; 28211 if (idl->idl_conn_draining == connp) { 28212 /* 28213 * This conn is the current drainer. If this is the last conn 28214 * in the drain list, we need to do more checks, in the 'if' 28215 * below. Otherwwise we need to just qenable the next conn, 28216 * to sustain the draining, and is handled in the 'else' 28217 * below. 28218 */ 28219 if (connp->conn_drain_next == idl->idl_conn) { 28220 /* 28221 * This conn is the last in this list. This round 28222 * of draining is complete. If idl_repeat is set, 28223 * it means another flow enabling has happened from 28224 * the driver/streams and we need to another round 28225 * of draining. 28226 * If there are more than 2 conns in the drain list, 28227 * do a left rotate by 1, so that all conns except the 28228 * conn at the head move towards the head by 1, and the 28229 * the conn at the head goes to the tail. This attempts 28230 * a more even share for all queues that are being 28231 * drained. 28232 */ 28233 if ((connp->conn_drain_next != connp) && 28234 (idl->idl_conn->conn_drain_next != connp)) { 28235 idl->idl_conn = idl->idl_conn->conn_drain_next; 28236 } 28237 if (idl->idl_repeat) { 28238 qenable(idl->idl_conn->conn_wq); 28239 idl->idl_conn_draining = idl->idl_conn; 28240 idl->idl_repeat = 0; 28241 } else { 28242 idl->idl_conn_draining = NULL; 28243 } 28244 } else { 28245 /* 28246 * If the next queue that we are now qenable'ing, 28247 * is closing, it will remove itself from this list 28248 * and qenable the subsequent queue in ip_close(). 28249 * Serialization is acheived thru idl_lock. 28250 */ 28251 qenable(connp->conn_drain_next->conn_wq); 28252 idl->idl_conn_draining = connp->conn_drain_next; 28253 } 28254 } 28255 if (!connp->conn_did_putbq || closing) { 28256 /* 28257 * Remove ourself from the drain list, if we did not do 28258 * a putbq, or if the conn is closing. 28259 * Note: It is possible that q->q_first is non-null. It means 28260 * that these messages landed after we did a enableok() in 28261 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 28262 * service them. 28263 */ 28264 if (connp->conn_drain_next == connp) { 28265 /* Singleton in the list */ 28266 ASSERT(connp->conn_drain_prev == connp); 28267 idl->idl_conn = NULL; 28268 idl->idl_conn_draining = NULL; 28269 } else { 28270 connp->conn_drain_prev->conn_drain_next = 28271 connp->conn_drain_next; 28272 connp->conn_drain_next->conn_drain_prev = 28273 connp->conn_drain_prev; 28274 if (idl->idl_conn == connp) 28275 idl->idl_conn = connp->conn_drain_next; 28276 ASSERT(idl->idl_conn_draining != connp); 28277 28278 } 28279 connp->conn_drain_next = NULL; 28280 connp->conn_drain_prev = NULL; 28281 } 28282 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28283 } 28284 28285 /* 28286 * Write service routine. Shared perimeter entry point. 28287 * ip_wsrv can be called in any of the following ways. 28288 * 1. The device queue's messages has fallen below the low water mark 28289 * and STREAMS has backenabled the ill_wq. We walk thru all the 28290 * the drain lists and backenable the first conn in each list. 28291 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 28292 * qenabled non-tcp upper layers. We start dequeing messages and call 28293 * ip_wput for each message. 28294 */ 28295 28296 void 28297 ip_wsrv(queue_t *q) 28298 { 28299 conn_t *connp; 28300 ill_t *ill; 28301 mblk_t *mp; 28302 28303 if (q->q_next) { 28304 ill = (ill_t *)q->q_ptr; 28305 if (ill->ill_state_flags == 0) { 28306 /* 28307 * The device flow control has opened up. 28308 * Walk through conn drain lists and qenable the 28309 * first conn in each list. This makes sense only 28310 * if the stream is fully plumbed and setup. 28311 * Hence the if check above. 28312 */ 28313 ip1dbg(("ip_wsrv: walking\n")); 28314 conn_walk_drain(ill->ill_ipst); 28315 } 28316 return; 28317 } 28318 28319 connp = Q_TO_CONN(q); 28320 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 28321 28322 /* 28323 * 1. Set conn_draining flag to signal that service is active. 28324 * 28325 * 2. ip_output determines whether it has been called from service, 28326 * based on the last parameter. If it is IP_WSRV it concludes it 28327 * has been called from service. 28328 * 28329 * 3. Message ordering is preserved by the following logic. 28330 * i. A directly called ip_output (i.e. not thru service) will queue 28331 * the message at the tail, if conn_draining is set (i.e. service 28332 * is running) or if q->q_first is non-null. 28333 * 28334 * ii. If ip_output is called from service, and if ip_output cannot 28335 * putnext due to flow control, it does a putbq. 28336 * 28337 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 28338 * (causing an infinite loop). 28339 */ 28340 ASSERT(!connp->conn_did_putbq); 28341 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 28342 connp->conn_draining = 1; 28343 noenable(q); 28344 while ((mp = getq(q)) != NULL) { 28345 ASSERT(CONN_Q(q)); 28346 28347 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 28348 if (connp->conn_did_putbq) { 28349 /* ip_wput did a putbq */ 28350 break; 28351 } 28352 } 28353 /* 28354 * At this point, a thread coming down from top, calling 28355 * ip_wput, may end up queueing the message. We have not yet 28356 * enabled the queue, so ip_wsrv won't be called again. 28357 * To avoid this race, check q->q_first again (in the loop) 28358 * If the other thread queued the message before we call 28359 * enableok(), we will catch it in the q->q_first check. 28360 * If the other thread queues the message after we call 28361 * enableok(), ip_wsrv will be called again by STREAMS. 28362 */ 28363 connp->conn_draining = 0; 28364 enableok(q); 28365 } 28366 28367 /* Enable the next conn for draining */ 28368 conn_drain_tail(connp, B_FALSE); 28369 28370 connp->conn_did_putbq = 0; 28371 } 28372 28373 /* 28374 * Walk the list of all conn's calling the function provided with the 28375 * specified argument for each. Note that this only walks conn's that 28376 * have been bound. 28377 * Applies to both IPv4 and IPv6. 28378 */ 28379 static void 28380 conn_walk_fanout(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst) 28381 { 28382 conn_walk_fanout_table(ipst->ips_ipcl_udp_fanout, 28383 ipst->ips_ipcl_udp_fanout_size, 28384 func, arg, zoneid); 28385 conn_walk_fanout_table(ipst->ips_ipcl_conn_fanout, 28386 ipst->ips_ipcl_conn_fanout_size, 28387 func, arg, zoneid); 28388 conn_walk_fanout_table(ipst->ips_ipcl_bind_fanout, 28389 ipst->ips_ipcl_bind_fanout_size, 28390 func, arg, zoneid); 28391 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout, 28392 IPPROTO_MAX, func, arg, zoneid); 28393 conn_walk_fanout_table(ipst->ips_ipcl_proto_fanout_v6, 28394 IPPROTO_MAX, func, arg, zoneid); 28395 } 28396 28397 /* 28398 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 28399 * of conns that need to be drained, check if drain is already in progress. 28400 * If so set the idl_repeat bit, indicating that the last conn in the list 28401 * needs to reinitiate the drain once again, for the list. If drain is not 28402 * in progress for the list, initiate the draining, by qenabling the 1st 28403 * conn in the list. The drain is self-sustaining, each qenabled conn will 28404 * in turn qenable the next conn, when it is done/blocked/closing. 28405 */ 28406 static void 28407 conn_walk_drain(ip_stack_t *ipst) 28408 { 28409 int i; 28410 idl_t *idl; 28411 28412 IP_STAT(ipst, ip_conn_walk_drain); 28413 28414 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28415 idl = &ipst->ips_conn_drain_list[i]; 28416 mutex_enter(&idl->idl_lock); 28417 if (idl->idl_conn == NULL) { 28418 mutex_exit(&idl->idl_lock); 28419 continue; 28420 } 28421 /* 28422 * If this list is not being drained currently by 28423 * an ip_wsrv thread, start the process. 28424 */ 28425 if (idl->idl_conn_draining == NULL) { 28426 ASSERT(idl->idl_repeat == 0); 28427 qenable(idl->idl_conn->conn_wq); 28428 idl->idl_conn_draining = idl->idl_conn; 28429 } else { 28430 idl->idl_repeat = 1; 28431 } 28432 mutex_exit(&idl->idl_lock); 28433 } 28434 } 28435 28436 /* 28437 * Walk an conn hash table of `count' buckets, calling func for each entry. 28438 */ 28439 static void 28440 conn_walk_fanout_table(connf_t *connfp, uint_t count, pfv_t func, void *arg, 28441 zoneid_t zoneid) 28442 { 28443 conn_t *connp; 28444 28445 while (count-- > 0) { 28446 mutex_enter(&connfp->connf_lock); 28447 for (connp = connfp->connf_head; connp != NULL; 28448 connp = connp->conn_next) { 28449 if (zoneid == GLOBAL_ZONEID || 28450 zoneid == connp->conn_zoneid) { 28451 CONN_INC_REF(connp); 28452 mutex_exit(&connfp->connf_lock); 28453 (*func)(connp, arg); 28454 mutex_enter(&connfp->connf_lock); 28455 CONN_DEC_REF(connp); 28456 } 28457 } 28458 mutex_exit(&connfp->connf_lock); 28459 connfp++; 28460 } 28461 } 28462 28463 /* ipcl_walk routine invoked for ip_conn_report for each conn. */ 28464 static void 28465 conn_report1(conn_t *connp, void *mp) 28466 { 28467 char buf1[INET6_ADDRSTRLEN]; 28468 char buf2[INET6_ADDRSTRLEN]; 28469 uint_t print_len, buf_len; 28470 28471 ASSERT(connp != NULL); 28472 28473 buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr; 28474 if (buf_len <= 0) 28475 return; 28476 (void) inet_ntop(AF_INET6, &connp->conn_srcv6, buf1, sizeof (buf1)); 28477 (void) inet_ntop(AF_INET6, &connp->conn_remv6, buf2, sizeof (buf2)); 28478 print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len, 28479 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 28480 "%5d %s/%05d %s/%05d\n", 28481 (void *)connp, (void *)CONNP_TO_RQ(connp), 28482 (void *)CONNP_TO_WQ(connp), connp->conn_zoneid, 28483 buf1, connp->conn_lport, 28484 buf2, connp->conn_fport); 28485 if (print_len < buf_len) { 28486 ((mblk_t *)mp)->b_wptr += print_len; 28487 } else { 28488 ((mblk_t *)mp)->b_wptr += buf_len; 28489 } 28490 } 28491 28492 /* 28493 * Named Dispatch routine to produce a formatted report on all conns 28494 * that are listed in one of the fanout tables. 28495 * This report is accessed by using the ndd utility to "get" ND variable 28496 * "ip_conn_status". 28497 */ 28498 /* ARGSUSED */ 28499 static int 28500 ip_conn_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 28501 { 28502 conn_t *connp = Q_TO_CONN(q); 28503 28504 (void) mi_mpprintf(mp, 28505 "CONN " MI_COL_HDRPAD_STR 28506 "rfq " MI_COL_HDRPAD_STR 28507 "stq " MI_COL_HDRPAD_STR 28508 " zone local remote"); 28509 28510 /* 28511 * Because of the ndd constraint, at most we can have 64K buffer 28512 * to put in all conn info. So to be more efficient, just 28513 * allocate a 64K buffer here, assuming we need that large buffer. 28514 * This should be OK as only privileged processes can do ndd /dev/ip. 28515 */ 28516 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 28517 /* The following may work even if we cannot get a large buf. */ 28518 (void) mi_mpprintf(mp, "<< Out of buffer >>\n"); 28519 return (0); 28520 } 28521 28522 conn_walk_fanout(conn_report1, mp->b_cont, connp->conn_zoneid, 28523 connp->conn_netstack->netstack_ip); 28524 return (0); 28525 } 28526 28527 /* 28528 * Determine if the ill and multicast aspects of that packets 28529 * "matches" the conn. 28530 */ 28531 boolean_t 28532 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 28533 zoneid_t zoneid) 28534 { 28535 ill_t *in_ill; 28536 boolean_t found; 28537 ipif_t *ipif; 28538 ire_t *ire; 28539 ipaddr_t dst, src; 28540 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28541 28542 dst = ipha->ipha_dst; 28543 src = ipha->ipha_src; 28544 28545 /* 28546 * conn_incoming_ill is set by IP_BOUND_IF which limits 28547 * unicast, broadcast and multicast reception to 28548 * conn_incoming_ill. conn_wantpacket itself is called 28549 * only for BROADCAST and multicast. 28550 * 28551 * 1) ip_rput supresses duplicate broadcasts if the ill 28552 * is part of a group. Hence, we should be receiving 28553 * just one copy of broadcast for the whole group. 28554 * Thus, if it is part of the group the packet could 28555 * come on any ill of the group and hence we need a 28556 * match on the group. Otherwise, match on ill should 28557 * be sufficient. 28558 * 28559 * 2) ip_rput does not suppress duplicate multicast packets. 28560 * If there are two interfaces in a ill group and we have 28561 * 2 applications (conns) joined a multicast group G on 28562 * both the interfaces, ilm_lookup_ill filter in ip_rput 28563 * will give us two packets because we join G on both the 28564 * interfaces rather than nominating just one interface 28565 * for receiving multicast like broadcast above. So, 28566 * we have to call ilg_lookup_ill to filter out duplicate 28567 * copies, if ill is part of a group. 28568 */ 28569 in_ill = connp->conn_incoming_ill; 28570 if (in_ill != NULL) { 28571 if (in_ill->ill_group == NULL) { 28572 if (in_ill != ill) 28573 return (B_FALSE); 28574 } else if (in_ill->ill_group != ill->ill_group) { 28575 return (B_FALSE); 28576 } 28577 } 28578 28579 if (!CLASSD(dst)) { 28580 if (IPCL_ZONE_MATCH(connp, zoneid)) 28581 return (B_TRUE); 28582 /* 28583 * The conn is in a different zone; we need to check that this 28584 * broadcast address is configured in the application's zone and 28585 * on one ill in the group. 28586 */ 28587 ipif = ipif_get_next_ipif(NULL, ill); 28588 if (ipif == NULL) 28589 return (B_FALSE); 28590 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 28591 connp->conn_zoneid, NULL, 28592 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP), ipst); 28593 ipif_refrele(ipif); 28594 if (ire != NULL) { 28595 ire_refrele(ire); 28596 return (B_TRUE); 28597 } else { 28598 return (B_FALSE); 28599 } 28600 } 28601 28602 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 28603 connp->conn_zoneid == zoneid) { 28604 /* 28605 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 28606 * disabled, therefore we don't dispatch the multicast packet to 28607 * the sending zone. 28608 */ 28609 return (B_FALSE); 28610 } 28611 28612 if (IS_LOOPBACK(ill) && connp->conn_zoneid != zoneid) { 28613 /* 28614 * Multicast packet on the loopback interface: we only match 28615 * conns who joined the group in the specified zone. 28616 */ 28617 return (B_FALSE); 28618 } 28619 28620 if (connp->conn_multi_router) { 28621 /* multicast packet and multicast router socket: send up */ 28622 return (B_TRUE); 28623 } 28624 28625 mutex_enter(&connp->conn_lock); 28626 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 28627 mutex_exit(&connp->conn_lock); 28628 return (found); 28629 } 28630 28631 /* 28632 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 28633 */ 28634 /* ARGSUSED */ 28635 static void 28636 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 28637 { 28638 ill_t *ill = (ill_t *)q->q_ptr; 28639 mblk_t *mp1, *mp2; 28640 ipif_t *ipif; 28641 int err = 0; 28642 conn_t *connp = NULL; 28643 ipsq_t *ipsq; 28644 arc_t *arc; 28645 28646 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 28647 28648 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 28649 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 28650 28651 ASSERT(IAM_WRITER_ILL(ill)); 28652 mp2 = mp->b_cont; 28653 mp->b_cont = NULL; 28654 28655 /* 28656 * We have now received the arp bringup completion message 28657 * from ARP. Mark the arp bringup as done. Also if the arp 28658 * stream has already started closing, send up the AR_ARP_CLOSING 28659 * ack now since ARP is waiting in close for this ack. 28660 */ 28661 mutex_enter(&ill->ill_lock); 28662 ill->ill_arp_bringup_pending = 0; 28663 if (ill->ill_arp_closing) { 28664 mutex_exit(&ill->ill_lock); 28665 /* Let's reuse the mp for sending the ack */ 28666 arc = (arc_t *)mp->b_rptr; 28667 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 28668 arc->arc_cmd = AR_ARP_CLOSING; 28669 qreply(q, mp); 28670 } else { 28671 mutex_exit(&ill->ill_lock); 28672 freeb(mp); 28673 } 28674 28675 ipsq = ill->ill_phyint->phyint_ipsq; 28676 ipif = ipsq->ipsq_pending_ipif; 28677 mp1 = ipsq_pending_mp_get(ipsq, &connp); 28678 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 28679 if (mp1 == NULL) { 28680 /* bringup was aborted by the user */ 28681 freemsg(mp2); 28682 return; 28683 } 28684 28685 /* 28686 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we 28687 * must have an associated conn_t. Otherwise, we're bringing this 28688 * interface back up as part of handling an asynchronous event (e.g., 28689 * physical address change). 28690 */ 28691 if (ipsq->ipsq_current_ioctl != 0) { 28692 ASSERT(connp != NULL); 28693 q = CONNP_TO_WQ(connp); 28694 } else { 28695 ASSERT(connp == NULL); 28696 q = ill->ill_rq; 28697 } 28698 28699 /* 28700 * If the DL_BIND_REQ fails, it is noted 28701 * in arc_name_offset. 28702 */ 28703 err = *((int *)mp2->b_rptr); 28704 if (err == 0) { 28705 if (ipif->ipif_isv6) { 28706 if ((err = ipif_up_done_v6(ipif)) != 0) 28707 ip0dbg(("ip_arp_done: init failed\n")); 28708 } else { 28709 if ((err = ipif_up_done(ipif)) != 0) 28710 ip0dbg(("ip_arp_done: init failed\n")); 28711 } 28712 } else { 28713 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 28714 } 28715 28716 freemsg(mp2); 28717 28718 if ((err == 0) && (ill->ill_up_ipifs)) { 28719 err = ill_up_ipifs(ill, q, mp1); 28720 if (err == EINPROGRESS) 28721 return; 28722 } 28723 28724 if (ill->ill_up_ipifs) 28725 ill_group_cleanup(ill); 28726 28727 /* 28728 * The operation must complete without EINPROGRESS since 28729 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp. 28730 * Otherwise, the operation will be stuck forever in the ipsq. 28731 */ 28732 ASSERT(err != EINPROGRESS); 28733 if (ipsq->ipsq_current_ioctl != 0) 28734 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 28735 else 28736 ipsq_current_finish(ipsq); 28737 } 28738 28739 /* Allocate the private structure */ 28740 static int 28741 ip_priv_alloc(void **bufp) 28742 { 28743 void *buf; 28744 28745 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 28746 return (ENOMEM); 28747 28748 *bufp = buf; 28749 return (0); 28750 } 28751 28752 /* Function to delete the private structure */ 28753 void 28754 ip_priv_free(void *buf) 28755 { 28756 ASSERT(buf != NULL); 28757 kmem_free(buf, sizeof (ip_priv_t)); 28758 } 28759 28760 /* 28761 * The entry point for IPPF processing. 28762 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 28763 * routine just returns. 28764 * 28765 * When called, ip_process generates an ipp_packet_t structure 28766 * which holds the state information for this packet and invokes the 28767 * the classifier (via ipp_packet_process). The classification, depending on 28768 * configured filters, results in a list of actions for this packet. Invoking 28769 * an action may cause the packet to be dropped, in which case the resulting 28770 * mblk (*mpp) is NULL. proc indicates the callout position for 28771 * this packet and ill_index is the interface this packet on or will leave 28772 * on (inbound and outbound resp.). 28773 */ 28774 void 28775 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 28776 { 28777 mblk_t *mp; 28778 ip_priv_t *priv; 28779 ipp_action_id_t aid; 28780 int rc = 0; 28781 ipp_packet_t *pp; 28782 #define IP_CLASS "ip" 28783 28784 /* If the classifier is not loaded, return */ 28785 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 28786 return; 28787 } 28788 28789 mp = *mpp; 28790 ASSERT(mp != NULL); 28791 28792 /* Allocate the packet structure */ 28793 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 28794 if (rc != 0) { 28795 *mpp = NULL; 28796 freemsg(mp); 28797 return; 28798 } 28799 28800 /* Allocate the private structure */ 28801 rc = ip_priv_alloc((void **)&priv); 28802 if (rc != 0) { 28803 *mpp = NULL; 28804 freemsg(mp); 28805 ipp_packet_free(pp); 28806 return; 28807 } 28808 priv->proc = proc; 28809 priv->ill_index = ill_index; 28810 ipp_packet_set_private(pp, priv, ip_priv_free); 28811 ipp_packet_set_data(pp, mp); 28812 28813 /* Invoke the classifier */ 28814 rc = ipp_packet_process(&pp); 28815 if (pp != NULL) { 28816 mp = ipp_packet_get_data(pp); 28817 ipp_packet_free(pp); 28818 if (rc != 0) { 28819 freemsg(mp); 28820 *mpp = NULL; 28821 } 28822 } else { 28823 *mpp = NULL; 28824 } 28825 #undef IP_CLASS 28826 } 28827 28828 /* 28829 * Propagate a multicast group membership operation (add/drop) on 28830 * all the interfaces crossed by the related multirt routes. 28831 * The call is considered successful if the operation succeeds 28832 * on at least one interface. 28833 */ 28834 static int 28835 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 28836 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 28837 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 28838 mblk_t *first_mp) 28839 { 28840 ire_t *ire_gw; 28841 irb_t *irb; 28842 int error = 0; 28843 opt_restart_t *or; 28844 ip_stack_t *ipst = ire->ire_ipst; 28845 28846 irb = ire->ire_bucket; 28847 ASSERT(irb != NULL); 28848 28849 ASSERT(DB_TYPE(first_mp) == M_CTL); 28850 28851 or = (opt_restart_t *)first_mp->b_rptr; 28852 IRB_REFHOLD(irb); 28853 for (; ire != NULL; ire = ire->ire_next) { 28854 if ((ire->ire_flags & RTF_MULTIRT) == 0) 28855 continue; 28856 if (ire->ire_addr != group) 28857 continue; 28858 28859 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 28860 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 28861 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE, ipst); 28862 /* No resolver exists for the gateway; skip this ire. */ 28863 if (ire_gw == NULL) 28864 continue; 28865 28866 /* 28867 * This function can return EINPROGRESS. If so the operation 28868 * will be restarted from ip_restart_optmgmt which will 28869 * call ip_opt_set and option processing will restart for 28870 * this option. So we may end up calling 'fn' more than once. 28871 * This requires that 'fn' is idempotent except for the 28872 * return value. The operation is considered a success if 28873 * it succeeds at least once on any one interface. 28874 */ 28875 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 28876 NULL, fmode, src, first_mp); 28877 if (error == 0) 28878 or->or_private = CGTP_MCAST_SUCCESS; 28879 28880 if (ip_debug > 0) { 28881 ulong_t off; 28882 char *ksym; 28883 ksym = kobj_getsymname((uintptr_t)fn, &off); 28884 ip2dbg(("ip_multirt_apply_membership: " 28885 "called %s, multirt group 0x%08x via itf 0x%08x, " 28886 "error %d [success %u]\n", 28887 ksym ? ksym : "?", 28888 ntohl(group), ntohl(ire_gw->ire_src_addr), 28889 error, or->or_private)); 28890 } 28891 28892 ire_refrele(ire_gw); 28893 if (error == EINPROGRESS) { 28894 IRB_REFRELE(irb); 28895 return (error); 28896 } 28897 } 28898 IRB_REFRELE(irb); 28899 /* 28900 * Consider the call as successful if we succeeded on at least 28901 * one interface. Otherwise, return the last encountered error. 28902 */ 28903 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 28904 } 28905 28906 28907 /* 28908 * Issue a warning regarding a route crossing an interface with an 28909 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 28910 * amount of time is logged. 28911 */ 28912 static void 28913 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 28914 { 28915 hrtime_t current = gethrtime(); 28916 char buf[INET_ADDRSTRLEN]; 28917 ip_stack_t *ipst = ire->ire_ipst; 28918 28919 /* Convert interval in ms to hrtime in ns */ 28920 if (ipst->ips_multirt_bad_mtu_last_time + 28921 ((hrtime_t)ipst->ips_ip_multirt_log_interval * (hrtime_t)1000000) <= 28922 current) { 28923 cmn_err(CE_WARN, "ip: ignoring multiroute " 28924 "to %s, incorrect MTU %u (expected %u)\n", 28925 ip_dot_addr(ire->ire_addr, buf), 28926 ire->ire_max_frag, max_frag); 28927 28928 ipst->ips_multirt_bad_mtu_last_time = current; 28929 } 28930 } 28931 28932 28933 /* 28934 * Get the CGTP (multirouting) filtering status. 28935 * If 0, the CGTP hooks are transparent. 28936 */ 28937 /* ARGSUSED */ 28938 static int 28939 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 28940 { 28941 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28942 28943 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 28944 return (0); 28945 } 28946 28947 28948 /* 28949 * Set the CGTP (multirouting) filtering status. 28950 * If the status is changed from active to transparent 28951 * or from transparent to active, forward the new status 28952 * to the filtering module (if loaded). 28953 */ 28954 /* ARGSUSED */ 28955 static int 28956 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 28957 cred_t *ioc_cr) 28958 { 28959 long new_value; 28960 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28961 ip_stack_t *ipst = CONNQ_TO_IPST(q); 28962 28963 if (secpolicy_ip_config(ioc_cr, B_FALSE) != 0) 28964 return (EPERM); 28965 28966 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 28967 new_value < 0 || new_value > 1) { 28968 return (EINVAL); 28969 } 28970 28971 if ((!*ip_cgtp_filter_value) && new_value) { 28972 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 28973 ipst->ips_ip_cgtp_filter_ops == NULL ? 28974 " (module not loaded)" : ""); 28975 } 28976 if (*ip_cgtp_filter_value && (!new_value)) { 28977 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 28978 ipst->ips_ip_cgtp_filter_ops == NULL ? 28979 " (module not loaded)" : ""); 28980 } 28981 28982 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 28983 int res; 28984 netstackid_t stackid; 28985 28986 stackid = ipst->ips_netstack->netstack_stackid; 28987 res = ipst->ips_ip_cgtp_filter_ops->cfo_change_state(stackid, 28988 new_value); 28989 if (res) 28990 return (res); 28991 } 28992 28993 *ip_cgtp_filter_value = (boolean_t)new_value; 28994 28995 return (0); 28996 } 28997 28998 28999 /* 29000 * Return the expected CGTP hooks version number. 29001 */ 29002 int 29003 ip_cgtp_filter_supported(void) 29004 { 29005 return (ip_cgtp_filter_rev); 29006 } 29007 29008 29009 /* 29010 * CGTP hooks can be registered by invoking this function. 29011 * Checks that the version number matches. 29012 */ 29013 int 29014 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops) 29015 { 29016 netstack_t *ns; 29017 ip_stack_t *ipst; 29018 29019 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 29020 return (ENOTSUP); 29021 29022 ns = netstack_find_by_stackid(stackid); 29023 if (ns == NULL) 29024 return (EINVAL); 29025 ipst = ns->netstack_ip; 29026 ASSERT(ipst != NULL); 29027 29028 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 29029 netstack_rele(ns); 29030 return (EALREADY); 29031 } 29032 29033 ipst->ips_ip_cgtp_filter_ops = ops; 29034 netstack_rele(ns); 29035 return (0); 29036 } 29037 29038 /* 29039 * CGTP hooks can be unregistered by invoking this function. 29040 * Returns ENXIO if there was no registration. 29041 * Returns EBUSY if the ndd variable has not been turned off. 29042 */ 29043 int 29044 ip_cgtp_filter_unregister(netstackid_t stackid) 29045 { 29046 netstack_t *ns; 29047 ip_stack_t *ipst; 29048 29049 ns = netstack_find_by_stackid(stackid); 29050 if (ns == NULL) 29051 return (EINVAL); 29052 ipst = ns->netstack_ip; 29053 ASSERT(ipst != NULL); 29054 29055 if (ipst->ips_ip_cgtp_filter) { 29056 netstack_rele(ns); 29057 return (EBUSY); 29058 } 29059 29060 if (ipst->ips_ip_cgtp_filter_ops == NULL) { 29061 netstack_rele(ns); 29062 return (ENXIO); 29063 } 29064 ipst->ips_ip_cgtp_filter_ops = NULL; 29065 netstack_rele(ns); 29066 return (0); 29067 } 29068 29069 /* 29070 * Check whether there is a CGTP filter registration. 29071 * Returns non-zero if there is a registration, otherwise returns zero. 29072 * Note: returns zero if bad stackid. 29073 */ 29074 int 29075 ip_cgtp_filter_is_registered(netstackid_t stackid) 29076 { 29077 netstack_t *ns; 29078 ip_stack_t *ipst; 29079 int ret; 29080 29081 ns = netstack_find_by_stackid(stackid); 29082 if (ns == NULL) 29083 return (0); 29084 ipst = ns->netstack_ip; 29085 ASSERT(ipst != NULL); 29086 29087 if (ipst->ips_ip_cgtp_filter_ops != NULL) 29088 ret = 1; 29089 else 29090 ret = 0; 29091 29092 netstack_rele(ns); 29093 return (ret); 29094 } 29095 29096 static squeue_func_t 29097 ip_squeue_switch(int val) 29098 { 29099 squeue_func_t rval = squeue_fill; 29100 29101 switch (val) { 29102 case IP_SQUEUE_ENTER_NODRAIN: 29103 rval = squeue_enter_nodrain; 29104 break; 29105 case IP_SQUEUE_ENTER: 29106 rval = squeue_enter; 29107 break; 29108 default: 29109 break; 29110 } 29111 return (rval); 29112 } 29113 29114 /* ARGSUSED */ 29115 static int 29116 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 29117 caddr_t addr, cred_t *cr) 29118 { 29119 int *v = (int *)addr; 29120 long new_value; 29121 29122 if (secpolicy_net_config(cr, B_FALSE) != 0) 29123 return (EPERM); 29124 29125 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29126 return (EINVAL); 29127 29128 ip_input_proc = ip_squeue_switch(new_value); 29129 *v = new_value; 29130 return (0); 29131 } 29132 29133 /* ARGSUSED */ 29134 static int 29135 ip_int_set(queue_t *q, mblk_t *mp, char *value, 29136 caddr_t addr, cred_t *cr) 29137 { 29138 int *v = (int *)addr; 29139 long new_value; 29140 29141 if (secpolicy_net_config(cr, B_FALSE) != 0) 29142 return (EPERM); 29143 29144 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29145 return (EINVAL); 29146 29147 *v = new_value; 29148 return (0); 29149 } 29150 29151 /* 29152 * Handle changes to ipmp_hook_emulation ndd variable. 29153 * Need to update phyint_hook_ifindex. 29154 * Also generate a nic plumb event should a new ifidex be assigned to a group. 29155 */ 29156 static void 29157 ipmp_hook_emulation_changed(ip_stack_t *ipst) 29158 { 29159 phyint_t *phyi; 29160 phyint_t *phyi_tmp; 29161 char *groupname; 29162 int namelen; 29163 ill_t *ill; 29164 boolean_t new_group; 29165 29166 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 29167 /* 29168 * Group indicies are stored in the phyint - a common structure 29169 * to both IPv4 and IPv6. 29170 */ 29171 phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index); 29172 for (; phyi != NULL; 29173 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 29174 phyi, AVL_AFTER)) { 29175 /* Ignore the ones that do not have a group */ 29176 if (phyi->phyint_groupname_len == 0) 29177 continue; 29178 29179 /* 29180 * Look for other phyint in group. 29181 * Clear name/namelen so the lookup doesn't find ourselves. 29182 */ 29183 namelen = phyi->phyint_groupname_len; 29184 groupname = phyi->phyint_groupname; 29185 phyi->phyint_groupname_len = 0; 29186 phyi->phyint_groupname = NULL; 29187 29188 phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst); 29189 /* Restore */ 29190 phyi->phyint_groupname_len = namelen; 29191 phyi->phyint_groupname = groupname; 29192 29193 new_group = B_FALSE; 29194 if (ipst->ips_ipmp_hook_emulation) { 29195 /* 29196 * If the group already exists and has already 29197 * been assigned a group ifindex, we use the existing 29198 * group_ifindex, otherwise we pick a new group_ifindex 29199 * here. 29200 */ 29201 if (phyi_tmp != NULL && 29202 phyi_tmp->phyint_group_ifindex != 0) { 29203 phyi->phyint_group_ifindex = 29204 phyi_tmp->phyint_group_ifindex; 29205 } else { 29206 /* XXX We need a recovery strategy here. */ 29207 if (!ip_assign_ifindex( 29208 &phyi->phyint_group_ifindex, ipst)) 29209 cmn_err(CE_PANIC, 29210 "ip_assign_ifindex() failed"); 29211 new_group = B_TRUE; 29212 } 29213 } else { 29214 phyi->phyint_group_ifindex = 0; 29215 } 29216 if (ipst->ips_ipmp_hook_emulation) 29217 phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex; 29218 else 29219 phyi->phyint_hook_ifindex = phyi->phyint_ifindex; 29220 29221 /* 29222 * For IP Filter to find out the relationship between 29223 * names and interface indicies, we need to generate 29224 * a NE_PLUMB event when a new group can appear. 29225 * We always generate events when a new interface appears 29226 * (even when ipmp_hook_emulation is set) so there 29227 * is no need to generate NE_PLUMB events when 29228 * ipmp_hook_emulation is turned off. 29229 * And since it isn't critical for IP Filter to get 29230 * the NE_UNPLUMB events we skip those here. 29231 */ 29232 if (new_group) { 29233 /* 29234 * First phyint in group - generate group PLUMB event. 29235 * Since we are not running inside the ipsq we do 29236 * the dispatch immediately. 29237 */ 29238 if (phyi->phyint_illv4 != NULL) 29239 ill = phyi->phyint_illv4; 29240 else 29241 ill = phyi->phyint_illv6; 29242 29243 if (ill != NULL) { 29244 mutex_enter(&ill->ill_lock); 29245 ill_nic_info_plumb(ill, B_TRUE); 29246 ill_nic_info_dispatch(ill); 29247 mutex_exit(&ill->ill_lock); 29248 } 29249 } 29250 } 29251 rw_exit(&ipst->ips_ill_g_lock); 29252 } 29253 29254 /* ARGSUSED */ 29255 static int 29256 ipmp_hook_emulation_set(queue_t *q, mblk_t *mp, char *value, 29257 caddr_t addr, cred_t *cr) 29258 { 29259 int *v = (int *)addr; 29260 long new_value; 29261 ip_stack_t *ipst = CONNQ_TO_IPST(q); 29262 29263 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29264 return (EINVAL); 29265 29266 if (*v != new_value) { 29267 *v = new_value; 29268 ipmp_hook_emulation_changed(ipst); 29269 } 29270 return (0); 29271 } 29272 29273 static void * 29274 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp) 29275 { 29276 kstat_t *ksp; 29277 29278 ip_stat_t template = { 29279 { "ipsec_fanout_proto", KSTAT_DATA_UINT64 }, 29280 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 29281 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 29282 { "ip_udp_fanothers", KSTAT_DATA_UINT64 }, 29283 { "ip_udp_fast_path", KSTAT_DATA_UINT64 }, 29284 { "ip_udp_slow_path", KSTAT_DATA_UINT64 }, 29285 { "ip_udp_input_err", KSTAT_DATA_UINT64 }, 29286 { "ip_tcppullup", KSTAT_DATA_UINT64 }, 29287 { "ip_tcpoptions", KSTAT_DATA_UINT64 }, 29288 { "ip_multipkttcp", KSTAT_DATA_UINT64 }, 29289 { "ip_tcp_fast_path", KSTAT_DATA_UINT64 }, 29290 { "ip_tcp_slow_path", KSTAT_DATA_UINT64 }, 29291 { "ip_tcp_input_error", KSTAT_DATA_UINT64 }, 29292 { "ip_db_ref", KSTAT_DATA_UINT64 }, 29293 { "ip_notaligned1", KSTAT_DATA_UINT64 }, 29294 { "ip_notaligned2", KSTAT_DATA_UINT64 }, 29295 { "ip_multimblk3", KSTAT_DATA_UINT64 }, 29296 { "ip_multimblk4", KSTAT_DATA_UINT64 }, 29297 { "ip_ipoptions", KSTAT_DATA_UINT64 }, 29298 { "ip_classify_fail", KSTAT_DATA_UINT64 }, 29299 { "ip_opt", KSTAT_DATA_UINT64 }, 29300 { "ip_udp_rput_local", KSTAT_DATA_UINT64 }, 29301 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 29302 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 29303 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 29304 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 29305 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 29306 { "ip_trash_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 29307 { "ip_trash_ire_reclaim_success", KSTAT_DATA_UINT64 }, 29308 { "ip_ire_arp_timer_expired", KSTAT_DATA_UINT64 }, 29309 { "ip_ire_redirect_timer_expired", KSTAT_DATA_UINT64 }, 29310 { "ip_ire_pmtu_timer_expired", KSTAT_DATA_UINT64 }, 29311 { "ip_input_multi_squeue", KSTAT_DATA_UINT64 }, 29312 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29313 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29314 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29315 { "ip_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29316 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29317 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29318 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29319 { "ip_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29320 { "ip_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 29321 { "ip_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 29322 { "ip_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 29323 { "ip_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 29324 { "ip_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 29325 }; 29326 29327 ksp = kstat_create_netstack("ip", 0, "ipstat", "net", 29328 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 29329 KSTAT_FLAG_VIRTUAL, stackid); 29330 29331 if (ksp == NULL) 29332 return (NULL); 29333 29334 bcopy(&template, ip_statisticsp, sizeof (template)); 29335 ksp->ks_data = (void *)ip_statisticsp; 29336 ksp->ks_private = (void *)(uintptr_t)stackid; 29337 29338 kstat_install(ksp); 29339 return (ksp); 29340 } 29341 29342 static void 29343 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 29344 { 29345 if (ksp != NULL) { 29346 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29347 kstat_delete_netstack(ksp, stackid); 29348 } 29349 } 29350 29351 static void * 29352 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst) 29353 { 29354 kstat_t *ksp; 29355 29356 ip_named_kstat_t template = { 29357 { "forwarding", KSTAT_DATA_UINT32, 0 }, 29358 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 29359 { "inReceives", KSTAT_DATA_UINT64, 0 }, 29360 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 29361 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 29362 { "forwDatagrams", KSTAT_DATA_UINT64, 0 }, 29363 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 29364 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 29365 { "inDelivers", KSTAT_DATA_UINT64, 0 }, 29366 { "outRequests", KSTAT_DATA_UINT64, 0 }, 29367 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 29368 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 29369 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 29370 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 29371 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 29372 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 29373 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 29374 { "fragFails", KSTAT_DATA_UINT32, 0 }, 29375 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 29376 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 29377 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 29378 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 29379 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 29380 { "inErrs", KSTAT_DATA_UINT32, 0 }, 29381 { "noPorts", KSTAT_DATA_UINT32, 0 }, 29382 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 29383 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 29384 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 29385 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 29386 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 29387 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 29388 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 29389 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 29390 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 29391 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 29392 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 29393 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 29394 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 29395 }; 29396 29397 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 29398 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid); 29399 if (ksp == NULL || ksp->ks_data == NULL) 29400 return (NULL); 29401 29402 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2; 29403 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl; 29404 template.reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29405 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 29406 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 29407 29408 template.netToMediaEntrySize.value.i32 = 29409 sizeof (mib2_ipNetToMediaEntry_t); 29410 29411 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 29412 29413 bcopy(&template, ksp->ks_data, sizeof (template)); 29414 ksp->ks_update = ip_kstat_update; 29415 ksp->ks_private = (void *)(uintptr_t)stackid; 29416 29417 kstat_install(ksp); 29418 return (ksp); 29419 } 29420 29421 static void 29422 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29423 { 29424 if (ksp != NULL) { 29425 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29426 kstat_delete_netstack(ksp, stackid); 29427 } 29428 } 29429 29430 static int 29431 ip_kstat_update(kstat_t *kp, int rw) 29432 { 29433 ip_named_kstat_t *ipkp; 29434 mib2_ipIfStatsEntry_t ipmib; 29435 ill_walk_context_t ctx; 29436 ill_t *ill; 29437 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29438 netstack_t *ns; 29439 ip_stack_t *ipst; 29440 29441 if (kp == NULL || kp->ks_data == NULL) 29442 return (EIO); 29443 29444 if (rw == KSTAT_WRITE) 29445 return (EACCES); 29446 29447 ns = netstack_find_by_stackid(stackid); 29448 if (ns == NULL) 29449 return (-1); 29450 ipst = ns->netstack_ip; 29451 if (ipst == NULL) { 29452 netstack_rele(ns); 29453 return (-1); 29454 } 29455 ipkp = (ip_named_kstat_t *)kp->ks_data; 29456 29457 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib)); 29458 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 29459 ill = ILL_START_WALK_V4(&ctx, ipst); 29460 for (; ill != NULL; ill = ill_next(&ctx, ill)) 29461 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib); 29462 rw_exit(&ipst->ips_ill_g_lock); 29463 29464 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding; 29465 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL; 29466 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives; 29467 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors; 29468 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors; 29469 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams; 29470 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos; 29471 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards; 29472 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers; 29473 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests; 29474 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards; 29475 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes; 29476 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29477 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds; 29478 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs; 29479 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails; 29480 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs; 29481 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails; 29482 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates; 29483 29484 ipkp->routingDiscards.value.ui32 = 0; 29485 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs; 29486 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts; 29487 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs; 29488 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates; 29489 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups; 29490 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits; 29491 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs; 29492 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows; 29493 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows; 29494 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded; 29495 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed; 29496 29497 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion; 29498 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion; 29499 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion; 29500 29501 netstack_rele(ns); 29502 29503 return (0); 29504 } 29505 29506 static void * 29507 icmp_kstat_init(netstackid_t stackid) 29508 { 29509 kstat_t *ksp; 29510 29511 icmp_named_kstat_t template = { 29512 { "inMsgs", KSTAT_DATA_UINT32 }, 29513 { "inErrors", KSTAT_DATA_UINT32 }, 29514 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 29515 { "inTimeExcds", KSTAT_DATA_UINT32 }, 29516 { "inParmProbs", KSTAT_DATA_UINT32 }, 29517 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 29518 { "inRedirects", KSTAT_DATA_UINT32 }, 29519 { "inEchos", KSTAT_DATA_UINT32 }, 29520 { "inEchoReps", KSTAT_DATA_UINT32 }, 29521 { "inTimestamps", KSTAT_DATA_UINT32 }, 29522 { "inTimestampReps", KSTAT_DATA_UINT32 }, 29523 { "inAddrMasks", KSTAT_DATA_UINT32 }, 29524 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 29525 { "outMsgs", KSTAT_DATA_UINT32 }, 29526 { "outErrors", KSTAT_DATA_UINT32 }, 29527 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 29528 { "outTimeExcds", KSTAT_DATA_UINT32 }, 29529 { "outParmProbs", KSTAT_DATA_UINT32 }, 29530 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 29531 { "outRedirects", KSTAT_DATA_UINT32 }, 29532 { "outEchos", KSTAT_DATA_UINT32 }, 29533 { "outEchoReps", KSTAT_DATA_UINT32 }, 29534 { "outTimestamps", KSTAT_DATA_UINT32 }, 29535 { "outTimestampReps", KSTAT_DATA_UINT32 }, 29536 { "outAddrMasks", KSTAT_DATA_UINT32 }, 29537 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 29538 { "inChksumErrs", KSTAT_DATA_UINT32 }, 29539 { "inUnknowns", KSTAT_DATA_UINT32 }, 29540 { "inFragNeeded", KSTAT_DATA_UINT32 }, 29541 { "outFragNeeded", KSTAT_DATA_UINT32 }, 29542 { "outDrops", KSTAT_DATA_UINT32 }, 29543 { "inOverFlows", KSTAT_DATA_UINT32 }, 29544 { "inBadRedirects", KSTAT_DATA_UINT32 }, 29545 }; 29546 29547 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 29548 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid); 29549 if (ksp == NULL || ksp->ks_data == NULL) 29550 return (NULL); 29551 29552 bcopy(&template, ksp->ks_data, sizeof (template)); 29553 29554 ksp->ks_update = icmp_kstat_update; 29555 ksp->ks_private = (void *)(uintptr_t)stackid; 29556 29557 kstat_install(ksp); 29558 return (ksp); 29559 } 29560 29561 static void 29562 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29563 { 29564 if (ksp != NULL) { 29565 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29566 kstat_delete_netstack(ksp, stackid); 29567 } 29568 } 29569 29570 static int 29571 icmp_kstat_update(kstat_t *kp, int rw) 29572 { 29573 icmp_named_kstat_t *icmpkp; 29574 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29575 netstack_t *ns; 29576 ip_stack_t *ipst; 29577 29578 if ((kp == NULL) || (kp->ks_data == NULL)) 29579 return (EIO); 29580 29581 if (rw == KSTAT_WRITE) 29582 return (EACCES); 29583 29584 ns = netstack_find_by_stackid(stackid); 29585 if (ns == NULL) 29586 return (-1); 29587 ipst = ns->netstack_ip; 29588 if (ipst == NULL) { 29589 netstack_rele(ns); 29590 return (-1); 29591 } 29592 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 29593 29594 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs; 29595 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors; 29596 icmpkp->inDestUnreachs.value.ui32 = 29597 ipst->ips_icmp_mib.icmpInDestUnreachs; 29598 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds; 29599 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs; 29600 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs; 29601 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects; 29602 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos; 29603 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps; 29604 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps; 29605 icmpkp->inTimestampReps.value.ui32 = 29606 ipst->ips_icmp_mib.icmpInTimestampReps; 29607 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks; 29608 icmpkp->inAddrMaskReps.value.ui32 = 29609 ipst->ips_icmp_mib.icmpInAddrMaskReps; 29610 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs; 29611 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors; 29612 icmpkp->outDestUnreachs.value.ui32 = 29613 ipst->ips_icmp_mib.icmpOutDestUnreachs; 29614 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds; 29615 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs; 29616 icmpkp->outSrcQuenchs.value.ui32 = 29617 ipst->ips_icmp_mib.icmpOutSrcQuenchs; 29618 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects; 29619 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos; 29620 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps; 29621 icmpkp->outTimestamps.value.ui32 = 29622 ipst->ips_icmp_mib.icmpOutTimestamps; 29623 icmpkp->outTimestampReps.value.ui32 = 29624 ipst->ips_icmp_mib.icmpOutTimestampReps; 29625 icmpkp->outAddrMasks.value.ui32 = 29626 ipst->ips_icmp_mib.icmpOutAddrMasks; 29627 icmpkp->outAddrMaskReps.value.ui32 = 29628 ipst->ips_icmp_mib.icmpOutAddrMaskReps; 29629 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs; 29630 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns; 29631 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded; 29632 icmpkp->outFragNeeded.value.ui32 = 29633 ipst->ips_icmp_mib.icmpOutFragNeeded; 29634 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops; 29635 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows; 29636 icmpkp->inBadRedirects.value.ui32 = 29637 ipst->ips_icmp_mib.icmpInBadRedirects; 29638 29639 netstack_rele(ns); 29640 return (0); 29641 } 29642 29643 /* 29644 * This is the fanout function for raw socket opened for SCTP. Note 29645 * that it is called after SCTP checks that there is no socket which 29646 * wants a packet. Then before SCTP handles this out of the blue packet, 29647 * this function is called to see if there is any raw socket for SCTP. 29648 * If there is and it is bound to the correct address, the packet will 29649 * be sent to that socket. Note that only one raw socket can be bound to 29650 * a port. This is assured in ipcl_sctp_hash_insert(); 29651 */ 29652 void 29653 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 29654 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 29655 zoneid_t zoneid) 29656 { 29657 conn_t *connp; 29658 queue_t *rq; 29659 mblk_t *first_mp; 29660 boolean_t secure; 29661 ip6_t *ip6h; 29662 ip_stack_t *ipst = recv_ill->ill_ipst; 29663 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 29664 29665 first_mp = mp; 29666 if (mctl_present) { 29667 mp = first_mp->b_cont; 29668 secure = ipsec_in_is_secure(first_mp); 29669 ASSERT(mp != NULL); 29670 } else { 29671 secure = B_FALSE; 29672 } 29673 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 29674 29675 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha, ipst); 29676 if (connp == NULL) { 29677 sctp_ootb_input(first_mp, recv_ill, zoneid, mctl_present); 29678 return; 29679 } 29680 rq = connp->conn_rq; 29681 if (!canputnext(rq)) { 29682 CONN_DEC_REF(connp); 29683 BUMP_MIB(recv_ill->ill_ip_mib, rawipIfStatsInOverflows); 29684 freemsg(first_mp); 29685 return; 29686 } 29687 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 29688 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || secure) { 29689 first_mp = ipsec_check_inbound_policy(first_mp, connp, 29690 (isv4 ? ipha : NULL), ip6h, mctl_present); 29691 if (first_mp == NULL) { 29692 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 29693 CONN_DEC_REF(connp); 29694 return; 29695 } 29696 } 29697 /* 29698 * We probably should not send M_CTL message up to 29699 * raw socket. 29700 */ 29701 if (mctl_present) 29702 freeb(first_mp); 29703 29704 /* Initiate IPPF processing here if needed. */ 29705 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) || 29706 (!isv4 && IP6_IN_IPP(flags, ipst))) { 29707 ip_process(IPP_LOCAL_IN, &mp, 29708 recv_ill->ill_phyint->phyint_ifindex); 29709 if (mp == NULL) { 29710 CONN_DEC_REF(connp); 29711 return; 29712 } 29713 } 29714 29715 if (connp->conn_recvif || connp->conn_recvslla || 29716 ((connp->conn_ip_recvpktinfo || 29717 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 29718 (flags & IP_FF_IPINFO))) { 29719 int in_flags = 0; 29720 29721 /* 29722 * Since sctp does not support IP_RECVPKTINFO for v4, only pass 29723 * IPF_RECVIF. 29724 */ 29725 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 29726 in_flags = IPF_RECVIF; 29727 } 29728 if (connp->conn_recvslla) { 29729 in_flags |= IPF_RECVSLLA; 29730 } 29731 if (isv4) { 29732 mp = ip_add_info(mp, recv_ill, in_flags, 29733 IPCL_ZONEID(connp), ipst); 29734 } else { 29735 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 29736 if (mp == NULL) { 29737 BUMP_MIB(recv_ill->ill_ip_mib, 29738 ipIfStatsInDiscards); 29739 CONN_DEC_REF(connp); 29740 return; 29741 } 29742 } 29743 } 29744 29745 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 29746 /* 29747 * We are sending the IPSEC_IN message also up. Refer 29748 * to comments above this function. 29749 */ 29750 putnext(rq, mp); 29751 CONN_DEC_REF(connp); 29752 } 29753 29754 #define UPDATE_IP_MIB_OB_COUNTERS(ill, len) \ 29755 { \ 29756 BUMP_MIB((ill)->ill_ip_mib, ipIfStatsHCOutTransmits); \ 29757 UPDATE_MIB((ill)->ill_ip_mib, ipIfStatsHCOutOctets, (len)); \ 29758 } 29759 /* 29760 * This function should be called only if all packet processing 29761 * including fragmentation is complete. Callers of this function 29762 * must set mp->b_prev to one of these values: 29763 * {0, IPP_FWD_OUT, IPP_LOCAL_OUT} 29764 * prior to handing over the mp as first argument to this function. 29765 * 29766 * If the ire passed by caller is incomplete, this function 29767 * queues the packet and if necessary, sends ARP request and bails. 29768 * If the ire passed is fully resolved, we simply prepend 29769 * the link-layer header to the packet, do ipsec hw acceleration 29770 * work if necessary, and send the packet out on the wire. 29771 * 29772 * NOTE: IPsec will only call this function with fully resolved 29773 * ires if hw acceleration is involved. 29774 * TODO list : 29775 * a Handle M_MULTIDATA so that 29776 * tcp_multisend->tcp_multisend_data can 29777 * call ip_xmit_v4 directly 29778 * b Handle post-ARP work for fragments so that 29779 * ip_wput_frag can call this function. 29780 */ 29781 ipxmit_state_t 29782 ip_xmit_v4(mblk_t *mp, ire_t *ire, ipsec_out_t *io, boolean_t flow_ctl_enabled) 29783 { 29784 nce_t *arpce; 29785 queue_t *q; 29786 int ill_index; 29787 mblk_t *nxt_mp, *first_mp; 29788 boolean_t xmit_drop = B_FALSE; 29789 ip_proc_t proc; 29790 ill_t *out_ill; 29791 int pkt_len; 29792 29793 arpce = ire->ire_nce; 29794 ASSERT(arpce != NULL); 29795 29796 DTRACE_PROBE2(ip__xmit__v4, ire_t *, ire, nce_t *, arpce); 29797 29798 mutex_enter(&arpce->nce_lock); 29799 switch (arpce->nce_state) { 29800 case ND_REACHABLE: 29801 /* If there are other queued packets, queue this packet */ 29802 if (arpce->nce_qd_mp != NULL) { 29803 if (mp != NULL) 29804 nce_queue_mp_common(arpce, mp, B_FALSE); 29805 mp = arpce->nce_qd_mp; 29806 } 29807 arpce->nce_qd_mp = NULL; 29808 mutex_exit(&arpce->nce_lock); 29809 29810 /* 29811 * Flush the queue. In the common case, where the 29812 * ARP is already resolved, it will go through the 29813 * while loop only once. 29814 */ 29815 while (mp != NULL) { 29816 29817 nxt_mp = mp->b_next; 29818 mp->b_next = NULL; 29819 ASSERT(mp->b_datap->db_type != M_CTL); 29820 pkt_len = ntohs(((ipha_t *)mp->b_rptr)->ipha_length); 29821 /* 29822 * This info is needed for IPQOS to do COS marking 29823 * in ip_wput_attach_llhdr->ip_process. 29824 */ 29825 proc = (ip_proc_t)(uintptr_t)mp->b_prev; 29826 mp->b_prev = NULL; 29827 29828 /* set up ill index for outbound qos processing */ 29829 out_ill = ire_to_ill(ire); 29830 ill_index = out_ill->ill_phyint->phyint_ifindex; 29831 first_mp = ip_wput_attach_llhdr(mp, ire, proc, 29832 ill_index); 29833 if (first_mp == NULL) { 29834 xmit_drop = B_TRUE; 29835 BUMP_MIB(out_ill->ill_ip_mib, 29836 ipIfStatsOutDiscards); 29837 goto next_mp; 29838 } 29839 /* non-ipsec hw accel case */ 29840 if (io == NULL || !io->ipsec_out_accelerated) { 29841 /* send it */ 29842 q = ire->ire_stq; 29843 if (proc == IPP_FWD_OUT) { 29844 UPDATE_IB_PKT_COUNT(ire); 29845 } else { 29846 UPDATE_OB_PKT_COUNT(ire); 29847 } 29848 ire->ire_last_used_time = lbolt; 29849 29850 if (flow_ctl_enabled || canputnext(q)) { 29851 if (proc == IPP_FWD_OUT) { 29852 29853 BUMP_MIB(out_ill->ill_ip_mib, 29854 ipIfStatsHCOutForwDatagrams); 29855 29856 } 29857 UPDATE_IP_MIB_OB_COUNTERS(out_ill, 29858 pkt_len); 29859 29860 putnext(q, first_mp); 29861 } else { 29862 BUMP_MIB(out_ill->ill_ip_mib, 29863 ipIfStatsOutDiscards); 29864 xmit_drop = B_TRUE; 29865 freemsg(first_mp); 29866 } 29867 } else { 29868 /* 29869 * Safety Pup says: make sure this 29870 * is going to the right interface! 29871 */ 29872 ill_t *ill1 = 29873 (ill_t *)ire->ire_stq->q_ptr; 29874 int ifindex = 29875 ill1->ill_phyint->phyint_ifindex; 29876 if (ifindex != 29877 io->ipsec_out_capab_ill_index) { 29878 xmit_drop = B_TRUE; 29879 freemsg(mp); 29880 } else { 29881 UPDATE_IP_MIB_OB_COUNTERS(ill1, 29882 pkt_len); 29883 ipsec_hw_putnext(ire->ire_stq, mp); 29884 } 29885 } 29886 next_mp: 29887 mp = nxt_mp; 29888 } /* while (mp != NULL) */ 29889 if (xmit_drop) 29890 return (SEND_FAILED); 29891 else 29892 return (SEND_PASSED); 29893 29894 case ND_INITIAL: 29895 case ND_INCOMPLETE: 29896 29897 /* 29898 * While we do send off packets to dests that 29899 * use fully-resolved CGTP routes, we do not 29900 * handle unresolved CGTP routes. 29901 */ 29902 ASSERT(!(ire->ire_flags & RTF_MULTIRT)); 29903 ASSERT(io == NULL || !io->ipsec_out_accelerated); 29904 29905 if (mp != NULL) { 29906 /* queue the packet */ 29907 nce_queue_mp_common(arpce, mp, B_FALSE); 29908 } 29909 29910 if (arpce->nce_state == ND_INCOMPLETE) { 29911 mutex_exit(&arpce->nce_lock); 29912 DTRACE_PROBE3(ip__xmit__incomplete, 29913 (ire_t *), ire, (mblk_t *), mp, 29914 (ipsec_out_t *), io); 29915 return (LOOKUP_IN_PROGRESS); 29916 } 29917 29918 arpce->nce_state = ND_INCOMPLETE; 29919 mutex_exit(&arpce->nce_lock); 29920 /* 29921 * Note that ire_add() (called from ire_forward()) 29922 * holds a ref on the ire until ARP is completed. 29923 */ 29924 29925 ire_arpresolve(ire, ire_to_ill(ire)); 29926 return (LOOKUP_IN_PROGRESS); 29927 default: 29928 ASSERT(0); 29929 mutex_exit(&arpce->nce_lock); 29930 return (LLHDR_RESLV_FAILED); 29931 } 29932 } 29933 29934 #undef UPDATE_IP_MIB_OB_COUNTERS 29935 29936 /* 29937 * Return B_TRUE if the buffers differ in length or content. 29938 * This is used for comparing extension header buffers. 29939 * Note that an extension header would be declared different 29940 * even if all that changed was the next header value in that header i.e. 29941 * what really changed is the next extension header. 29942 */ 29943 boolean_t 29944 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 29945 uint_t blen) 29946 { 29947 if (!b_valid) 29948 blen = 0; 29949 29950 if (alen != blen) 29951 return (B_TRUE); 29952 if (alen == 0) 29953 return (B_FALSE); /* Both zero length */ 29954 return (bcmp(abuf, bbuf, alen)); 29955 } 29956 29957 /* 29958 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 29959 * Return B_FALSE if memory allocation fails - don't change any state! 29960 */ 29961 boolean_t 29962 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29963 const void *src, uint_t srclen) 29964 { 29965 void *dst; 29966 29967 if (!src_valid) 29968 srclen = 0; 29969 29970 ASSERT(*dstlenp == 0); 29971 if (src != NULL && srclen != 0) { 29972 dst = mi_alloc(srclen, BPRI_MED); 29973 if (dst == NULL) 29974 return (B_FALSE); 29975 } else { 29976 dst = NULL; 29977 } 29978 if (*dstp != NULL) 29979 mi_free(*dstp); 29980 *dstp = dst; 29981 *dstlenp = dst == NULL ? 0 : srclen; 29982 return (B_TRUE); 29983 } 29984 29985 /* 29986 * Replace what is in *dst, *dstlen with the source. 29987 * Assumes ip_allocbuf has already been called. 29988 */ 29989 void 29990 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29991 const void *src, uint_t srclen) 29992 { 29993 if (!src_valid) 29994 srclen = 0; 29995 29996 ASSERT(*dstlenp == srclen); 29997 if (src != NULL && srclen != 0) 29998 bcopy(src, *dstp, srclen); 29999 } 30000 30001 /* 30002 * Free the storage pointed to by the members of an ip6_pkt_t. 30003 */ 30004 void 30005 ip6_pkt_free(ip6_pkt_t *ipp) 30006 { 30007 ASSERT(ipp->ipp_pathmtu == NULL && !(ipp->ipp_fields & IPPF_PATHMTU)); 30008 30009 if (ipp->ipp_fields & IPPF_HOPOPTS) { 30010 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 30011 ipp->ipp_hopopts = NULL; 30012 ipp->ipp_hopoptslen = 0; 30013 } 30014 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 30015 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 30016 ipp->ipp_rtdstopts = NULL; 30017 ipp->ipp_rtdstoptslen = 0; 30018 } 30019 if (ipp->ipp_fields & IPPF_DSTOPTS) { 30020 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 30021 ipp->ipp_dstopts = NULL; 30022 ipp->ipp_dstoptslen = 0; 30023 } 30024 if (ipp->ipp_fields & IPPF_RTHDR) { 30025 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 30026 ipp->ipp_rthdr = NULL; 30027 ipp->ipp_rthdrlen = 0; 30028 } 30029 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 30030 IPPF_RTHDR); 30031 } 30032