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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #include <sys/types.h> 29 #include <sys/stream.h> 30 #include <sys/dlpi.h> 31 #include <sys/stropts.h> 32 #include <sys/sysmacros.h> 33 #include <sys/strsubr.h> 34 #include <sys/strlog.h> 35 #include <sys/strsun.h> 36 #include <sys/zone.h> 37 #define _SUN_TPI_VERSION 2 38 #include <sys/tihdr.h> 39 #include <sys/xti_inet.h> 40 #include <sys/ddi.h> 41 #include <sys/cmn_err.h> 42 #include <sys/debug.h> 43 #include <sys/kobj.h> 44 #include <sys/modctl.h> 45 #include <sys/atomic.h> 46 #include <sys/policy.h> 47 #include <sys/priv.h> 48 #include <sys/taskq.h> 49 50 #include <sys/systm.h> 51 #include <sys/param.h> 52 #include <sys/kmem.h> 53 #include <sys/sdt.h> 54 #include <sys/socket.h> 55 #include <sys/vtrace.h> 56 #include <sys/isa_defs.h> 57 #include <sys/mac.h> 58 #include <net/if.h> 59 #include <net/if_arp.h> 60 #include <net/route.h> 61 #include <sys/sockio.h> 62 #include <netinet/in.h> 63 #include <net/if_dl.h> 64 65 #include <inet/common.h> 66 #include <inet/mi.h> 67 #include <inet/mib2.h> 68 #include <inet/nd.h> 69 #include <inet/arp.h> 70 #include <inet/snmpcom.h> 71 #include <inet/optcom.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/ip_ndp.h> 91 #include <inet/ip_listutils.h> 92 #include <netinet/igmp.h> 93 #include <netinet/ip_mroute.h> 94 #include <inet/ipp_common.h> 95 96 #include <net/pfkeyv2.h> 97 #include <inet/ipsec_info.h> 98 #include <inet/sadb.h> 99 #include <inet/ipsec_impl.h> 100 #include <sys/iphada.h> 101 #include <inet/tun.h> 102 #include <inet/ipdrop.h> 103 #include <inet/ip_netinfo.h> 104 105 #include <sys/ethernet.h> 106 #include <net/if_types.h> 107 #include <sys/cpuvar.h> 108 109 #include <ipp/ipp.h> 110 #include <ipp/ipp_impl.h> 111 #include <ipp/ipgpc/ipgpc.h> 112 113 #include <sys/multidata.h> 114 #include <sys/pattr.h> 115 116 #include <inet/ipclassifier.h> 117 #include <inet/sctp_ip.h> 118 #include <inet/sctp/sctp_impl.h> 119 #include <inet/udp_impl.h> 120 #include <inet/rawip_impl.h> 121 #include <inet/rts_impl.h> 122 123 #include <sys/tsol/label.h> 124 #include <sys/tsol/tnet.h> 125 126 #include <rpc/pmap_prot.h> 127 #include <sys/squeue_impl.h> 128 129 /* 130 * Values for squeue switch: 131 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN 132 * IP_SQUEUE_ENTER: SQ_PROCESS 133 * IP_SQUEUE_FILL: SQ_FILL 134 */ 135 int ip_squeue_enter = 2; /* Setable in /etc/system */ 136 137 int ip_squeue_flag; 138 #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x)) 139 140 /* 141 * Setable in /etc/system 142 */ 143 int ip_poll_normal_ms = 100; 144 int ip_poll_normal_ticks = 0; 145 int ip_modclose_ackwait_ms = 3000; 146 147 /* 148 * It would be nice to have these present only in DEBUG systems, but the 149 * current design of the global symbol checking logic requires them to be 150 * unconditionally present. 151 */ 152 uint_t ip_thread_data; /* TSD key for debug support */ 153 krwlock_t ip_thread_rwlock; 154 list_t ip_thread_list; 155 156 /* 157 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 158 */ 159 160 struct listptr_s { 161 mblk_t *lp_head; /* pointer to the head of the list */ 162 mblk_t *lp_tail; /* pointer to the tail of the list */ 163 }; 164 165 typedef struct listptr_s listptr_t; 166 167 /* 168 * This is used by ip_snmp_get_mib2_ip_route_media and 169 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 170 */ 171 typedef struct iproutedata_s { 172 uint_t ird_idx; 173 uint_t ird_flags; /* see below */ 174 listptr_t ird_route; /* ipRouteEntryTable */ 175 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 176 listptr_t ird_attrs; /* ipRouteAttributeTable */ 177 } iproutedata_t; 178 179 #define IRD_REPORT_TESTHIDDEN 0x01 /* include IRE_MARK_TESTHIDDEN routes */ 180 181 /* 182 * Cluster specific hooks. These should be NULL when booted as a non-cluster 183 */ 184 185 /* 186 * Hook functions to enable cluster networking 187 * On non-clustered systems these vectors must always be NULL. 188 * 189 * Hook function to Check ip specified ip address is a shared ip address 190 * in the cluster 191 * 192 */ 193 int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 194 sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL; 195 196 /* 197 * Hook function to generate cluster wide ip fragment identifier 198 */ 199 uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 200 sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp, 201 void *args) = NULL; 202 203 /* 204 * Hook function to generate cluster wide SPI. 205 */ 206 void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t, 207 void *) = NULL; 208 209 /* 210 * Hook function to verify if the SPI is already utlized. 211 */ 212 213 int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL; 214 215 /* 216 * Hook function to delete the SPI from the cluster wide repository. 217 */ 218 219 void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL; 220 221 /* 222 * Hook function to inform the cluster when packet received on an IDLE SA 223 */ 224 225 void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t, 226 in6_addr_t, in6_addr_t, void *) = NULL; 227 228 /* 229 * Synchronization notes: 230 * 231 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 232 * MT level protection given by STREAMS. IP uses a combination of its own 233 * internal serialization mechanism and standard Solaris locking techniques. 234 * The internal serialization is per phyint. This is used to serialize 235 * plumbing operations, certain multicast operations, most set ioctls, 236 * igmp/mld timers etc. 237 * 238 * Plumbing is a long sequence of operations involving message 239 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 240 * involved in plumbing operations. A natural model is to serialize these 241 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 242 * parallel without any interference. But various set ioctls on hme0 are best 243 * serialized, along with multicast join/leave operations, igmp/mld timer 244 * operations, and processing of DLPI control messages received from drivers 245 * on a per phyint basis. This serialization is provided by the ipsq_t and 246 * primitives operating on this. Details can be found in ip_if.c above the 247 * core primitives operating on ipsq_t. 248 * 249 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 250 * Simiarly lookup of an ire by a thread also returns a refheld ire. 251 * In addition ipif's and ill's referenced by the ire are also indirectly 252 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 253 * the ipif's address or netmask change as long as an ipif is refheld 254 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the 255 * address of an ipif has to go through the ipsq_t. This ensures that only 256 * 1 such exclusive operation proceeds at any time on the ipif. It then 257 * deletes all ires associated with this ipif, and waits for all refcnts 258 * associated with this ipif to come down to zero. The address is changed 259 * only after the ipif has been quiesced. Then the ipif is brought up again. 260 * More details are described above the comment in ip_sioctl_flags. 261 * 262 * Packet processing is based mostly on IREs and are fully multi-threaded 263 * using standard Solaris MT techniques. 264 * 265 * There are explicit locks in IP to handle: 266 * - The ip_g_head list maintained by mi_open_link() and friends. 267 * 268 * - The reassembly data structures (one lock per hash bucket) 269 * 270 * - conn_lock is meant to protect conn_t fields. The fields actually 271 * protected by conn_lock are documented in the conn_t definition. 272 * 273 * - ire_lock to protect some of the fields of the ire, IRE tables 274 * (one lock per hash bucket). Refer to ip_ire.c for details. 275 * 276 * - ndp_g_lock and nce_lock for protecting NCEs. 277 * 278 * - ill_lock protects fields of the ill and ipif. Details in ip.h 279 * 280 * - ill_g_lock: This is a global reader/writer lock. Protects the following 281 * * The AVL tree based global multi list of all ills. 282 * * The linked list of all ipifs of an ill 283 * * The <ipsq-xop> mapping 284 * * <ill-phyint> association 285 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 286 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the 287 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as 288 * writer for the actual duration of the insertion/deletion/change. 289 * 290 * - ill_lock: This is a per ill mutex. 291 * It protects some members of the ill_t struct; see ip.h for details. 292 * It also protects the <ill-phyint> assoc. 293 * It also protects the list of ipifs hanging off the ill. 294 * 295 * - ipsq_lock: This is a per ipsq_t mutex lock. 296 * This protects some members of the ipsq_t struct; see ip.h for details. 297 * It also protects the <ipsq-ipxop> mapping 298 * 299 * - ipx_lock: This is a per ipxop_t mutex lock. 300 * This protects some members of the ipxop_t struct; see ip.h for details. 301 * 302 * - phyint_lock: This is a per phyint mutex lock. Protects just the 303 * phyint_flags 304 * 305 * - ip_g_nd_lock: This is a global reader/writer lock. 306 * Any call to nd_load to load a new parameter to the ND table must hold the 307 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 308 * as reader. 309 * 310 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 311 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 312 * uniqueness check also done atomically. 313 * 314 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 315 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 316 * as a writer when adding or deleting elements from these lists, and 317 * as a reader when walking these lists to send a SADB update to the 318 * IPsec capable ills. 319 * 320 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 321 * group list linked by ill_usesrc_grp_next. It also protects the 322 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 323 * group is being added or deleted. This lock is taken as a reader when 324 * walking the list/group(eg: to get the number of members in a usesrc group). 325 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 326 * field is changing state i.e from NULL to non-NULL or vice-versa. For 327 * example, it is not necessary to take this lock in the initial portion 328 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these 329 * operations are executed exclusively and that ensures that the "usesrc 330 * group state" cannot change. The "usesrc group state" change can happen 331 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete. 332 * 333 * Changing <ill-phyint>, <ipsq-xop> assocications: 334 * 335 * To change the <ill-phyint> association, the ill_g_lock must be held 336 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 337 * must be held. 338 * 339 * To change the <ipsq-xop> association, the ill_g_lock must be held as 340 * writer, the ipsq_lock must be held, and one must be writer on the ipsq. 341 * This is only done when ills are added or removed from IPMP groups. 342 * 343 * To add or delete an ipif from the list of ipifs hanging off the ill, 344 * ill_g_lock (writer) and ill_lock must be held and the thread must be 345 * a writer on the associated ipsq. 346 * 347 * To add or delete an ill to the system, the ill_g_lock must be held as 348 * writer and the thread must be a writer on the associated ipsq. 349 * 350 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 351 * must be a writer on the associated ipsq. 352 * 353 * Lock hierarchy 354 * 355 * Some lock hierarchy scenarios are listed below. 356 * 357 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock 358 * ill_g_lock -> ill_lock(s) -> phyint_lock 359 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 360 * ill_g_lock -> ip_addr_avail_lock 361 * conn_lock -> irb_lock -> ill_lock -> ire_lock 362 * ill_g_lock -> ip_g_nd_lock 363 * 364 * When more than 1 ill lock is needed to be held, all ill lock addresses 365 * are sorted on address and locked starting from highest addressed lock 366 * downward. 367 * 368 * IPsec scenarios 369 * 370 * ipsa_lock -> ill_g_lock -> ill_lock 371 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 372 * ipsec_capab_ills_lock -> ipsa_lock 373 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 374 * 375 * Trusted Solaris scenarios 376 * 377 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 378 * igsa_lock -> gcdb_lock 379 * gcgrp_rwlock -> ire_lock 380 * gcgrp_rwlock -> gcdb_lock 381 * 382 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking 383 * 384 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock 385 * sq_lock -> conn_lock -> QLOCK(q) 386 * ill_lock -> ft_lock -> fe_lock 387 * 388 * Routing/forwarding table locking notes: 389 * 390 * Lock acquisition order: Radix tree lock, irb_lock. 391 * Requirements: 392 * i. Walker must not hold any locks during the walker callback. 393 * ii Walker must not see a truncated tree during the walk because of any node 394 * deletion. 395 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 396 * in many places in the code to walk the irb list. Thus even if all the 397 * ires in a bucket have been deleted, we still can't free the radix node 398 * until the ires have actually been inactive'd (freed). 399 * 400 * Tree traversal - Need to hold the global tree lock in read mode. 401 * Before dropping the global tree lock, need to either increment the ire_refcnt 402 * to ensure that the radix node can't be deleted. 403 * 404 * Tree add - Need to hold the global tree lock in write mode to add a 405 * radix node. To prevent the node from being deleted, increment the 406 * irb_refcnt, after the node is added to the tree. The ire itself is 407 * added later while holding the irb_lock, but not the tree lock. 408 * 409 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 410 * All associated ires must be inactive (i.e. freed), and irb_refcnt 411 * must be zero. 412 * 413 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 414 * global tree lock (read mode) for traversal. 415 * 416 * IPsec notes : 417 * 418 * IP interacts with the IPsec code (AH/ESP) by tagging a M_CTL message 419 * in front of the actual packet. For outbound datagrams, the M_CTL 420 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 421 * information used by the IPsec code for applying the right level of 422 * protection. The information initialized by IP in the ipsec_out_t 423 * is determined by the per-socket policy or global policy in the system. 424 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 425 * ipsec_info.h) which starts out with nothing in it. It gets filled 426 * with the right information if it goes through the AH/ESP code, which 427 * happens if the incoming packet is secure. The information initialized 428 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 429 * the policy requirements needed by per-socket policy or global policy 430 * is met or not. 431 * 432 * If there is both per-socket policy (set using setsockopt) and there 433 * is also global policy match for the 5 tuples of the socket, 434 * ipsec_override_policy() makes the decision of which one to use. 435 * 436 * For fully connected sockets i.e dst, src [addr, port] is known, 437 * conn_policy_cached is set indicating that policy has been cached. 438 * conn_in_enforce_policy may or may not be set depending on whether 439 * there is a global policy match or per-socket policy match. 440 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 441 * Once the right policy is set on the conn_t, policy cannot change for 442 * this socket. This makes life simpler for TCP (UDP ?) where 443 * re-transmissions go out with the same policy. For symmetry, policy 444 * is cached for fully connected UDP sockets also. Thus if policy is cached, 445 * it also implies that policy is latched i.e policy cannot change 446 * on these sockets. As we have the right policy on the conn, we don't 447 * have to lookup global policy for every outbound and inbound datagram 448 * and thus serving as an optimization. Note that a global policy change 449 * does not affect fully connected sockets if they have policy. If fully 450 * connected sockets did not have any policy associated with it, global 451 * policy change may affect them. 452 * 453 * IP Flow control notes: 454 * --------------------- 455 * Non-TCP streams are flow controlled by IP. The way this is accomplished 456 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When 457 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into 458 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS 459 * functions. 460 * 461 * Per Tx ring udp flow control: 462 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in 463 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true). 464 * 465 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer. 466 * To achieve best performance, outgoing traffic need to be fanned out among 467 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send 468 * traffic out of the NIC and it takes a fanout hint. UDP connections pass 469 * the address of connp as fanout hint to mac_tx(). Under flow controlled 470 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This 471 * cookie points to a specific Tx ring that is blocked. The cookie is used to 472 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t 473 * point to drain_lists (idl_t's). These drain list will store the blocked UDP 474 * connp's. The drain list is not a single list but a configurable number of 475 * lists. 476 * 477 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t 478 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE 479 * which is equal to 128. This array in turn contains a pointer to idl_t[], 480 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain 481 * list will point to the list of connp's that are flow controlled. 482 * 483 * --------------- ------- ------- ------- 484 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|--> 485 * | --------------- ------- ------- ------- 486 * | --------------- ------- ------- ------- 487 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|--> 488 * ---------------- | --------------- ------- ------- ------- 489 * |idl_tx_list[0]|->| --------------- ------- ------- ------- 490 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|--> 491 * | --------------- ------- ------- ------- 492 * . . . . . 493 * | --------------- ------- ------- ------- 494 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|--> 495 * --------------- ------- ------- ------- 496 * --------------- ------- ------- ------- 497 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|--> 498 * | --------------- ------- ------- ------- 499 * | --------------- ------- ------- ------- 500 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|--> 501 * |idl_tx_list[1]|->| --------------- ------- ------- ------- 502 * ---------------- | . . . . 503 * | --------------- ------- ------- ------- 504 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|--> 505 * --------------- ------- ------- ------- 506 * ..... 507 * ---------------- 508 * |idl_tx_list[n]|-> ... 509 * ---------------- 510 * 511 * When mac_tx() returns a cookie, the cookie is used to hash into a 512 * idl_tx_list in ips_idl_tx_list[] array. Then conn_drain_insert() is 513 * called passing idl_tx_list. The connp gets inserted in a drain list 514 * pointed to by idl_tx_list. conn_drain_list() asserts flow control for 515 * the sockets (non stream based) and sets QFULL condition for conn_wq. 516 * connp->conn_direct_blocked will be set to indicate the blocked 517 * condition. 518 * 519 * GLDv3 mac layer calls ill_flow_enable() when flow control is relieved. 520 * A cookie is passed in the call to ill_flow_enable() that identifies the 521 * blocked Tx ring. This cookie is used to get to the idl_tx_list that 522 * contains the blocked connp's. conn_walk_drain() uses the idl_tx_list_t 523 * and goes through each of the drain list (q)enabling the conn_wq of the 524 * first conn in each of the drain list. This causes ip_wsrv to run for the 525 * conn. ip_wsrv drains the queued messages, and removes the conn from the 526 * drain list, if all messages were drained. It also qenables the next conn 527 * in the drain list to continue the drain process. 528 * 529 * In reality the drain list is not a single list, but a configurable number 530 * of lists. conn_drain_walk() in the IP module, qenables the first conn in 531 * each list. If the ip_wsrv of the next qenabled conn does not run, because 532 * the stream closes, ip_close takes responsibility to qenable the next conn 533 * in the drain list. conn_drain_insert and conn_drain_tail are the only 534 * functions that manipulate this drain list. conn_drain_insert is called in 535 * ip_wput context itself (as opposed to from ip_wsrv context for STREAMS 536 * case -- see below). The synchronization between drain insertion and flow 537 * control wakeup is handled by using idl_txl->txl_lock. 538 * 539 * Flow control using STREAMS: 540 * When ILL_DIRECT_CAPABLE() is not TRUE, STREAMS flow control mechanism 541 * is used. On the send side, if the packet cannot be sent down to the 542 * driver by IP, because of a canput failure, IP does a putq on the conn_wq. 543 * This will cause ip_wsrv to run on the conn_wq. ip_wsrv in turn, inserts 544 * the conn in a list of conn's that need to be drained when the flow 545 * control condition subsides. The blocked connps are put in first member 546 * of ips_idl_tx_list[] array. Ultimately STREAMS backenables the ip_wsrv 547 * on the IP module. It calls conn_walk_drain() passing ips_idl_tx_list[0]. 548 * ips_idl_tx_list[0] contains the drain lists of blocked conns. The 549 * conn_wq of the first conn in the drain lists is (q)enabled to run. 550 * ip_wsrv on this conn drains the queued messages, and removes the conn 551 * from the drain list, if all messages were drained. It also qenables the 552 * next conn in the drain list to continue the drain process. 553 * 554 * If the ip_wsrv of the next qenabled conn does not run, because the 555 * stream closes, ip_close takes responsibility to qenable the next conn in 556 * the drain list. The directly called ip_wput path always does a putq, if 557 * it cannot putnext. Thus synchronization problems are handled between 558 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 559 * functions that manipulate this drain list. Furthermore conn_drain_insert 560 * is called only from ip_wsrv for the STREAMS case, and there can be only 1 561 * instance of ip_wsrv running on a queue at any time. conn_drain_tail can 562 * be simultaneously called from both ip_wsrv and ip_close. 563 * 564 * IPQOS notes: 565 * 566 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 567 * and IPQoS modules. IPPF includes hooks in IP at different control points 568 * (callout positions) which direct packets to IPQoS modules for policy 569 * processing. Policies, if present, are global. 570 * 571 * The callout positions are located in the following paths: 572 * o local_in (packets destined for this host) 573 * o local_out (packets orginating from this host ) 574 * o fwd_in (packets forwarded by this m/c - inbound) 575 * o fwd_out (packets forwarded by this m/c - outbound) 576 * Hooks at these callout points can be enabled/disabled using the ndd variable 577 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 578 * By default all the callout positions are enabled. 579 * 580 * Outbound (local_out) 581 * Hooks are placed in ip_wput_ire and ipsec_out_process. 582 * 583 * Inbound (local_in) 584 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 585 * TCP and UDP fanout routines. 586 * 587 * Forwarding (in and out) 588 * Hooks are placed in ip_rput_forward. 589 * 590 * IP Policy Framework processing (IPPF processing) 591 * Policy processing for a packet is initiated by ip_process, which ascertains 592 * that the classifier (ipgpc) is loaded and configured, failing which the 593 * packet resumes normal processing in IP. If the clasifier is present, the 594 * packet is acted upon by one or more IPQoS modules (action instances), per 595 * filters configured in ipgpc and resumes normal IP processing thereafter. 596 * An action instance can drop a packet in course of its processing. 597 * 598 * A boolean variable, ip_policy, is used in all the fanout routines that can 599 * invoke ip_process for a packet. This variable indicates if the packet should 600 * to be sent for policy processing. The variable is set to B_TRUE by default, 601 * i.e. when the routines are invoked in the normal ip procesing path for a 602 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 603 * ip_policy is set to B_FALSE for all the routines called in these two 604 * functions because, in the former case, we don't process loopback traffic 605 * currently while in the latter, the packets have already been processed in 606 * icmp_inbound. 607 * 608 * Zones notes: 609 * 610 * The partitioning rules for networking are as follows: 611 * 1) Packets coming from a zone must have a source address belonging to that 612 * zone. 613 * 2) Packets coming from a zone can only be sent on a physical interface on 614 * which the zone has an IP address. 615 * 3) Between two zones on the same machine, packet delivery is only allowed if 616 * there's a matching route for the destination and zone in the forwarding 617 * table. 618 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 619 * different zones can bind to the same port with the wildcard address 620 * (INADDR_ANY). 621 * 622 * The granularity of interface partitioning is at the logical interface level. 623 * Therefore, every zone has its own IP addresses, and incoming packets can be 624 * attributed to a zone unambiguously. A logical interface is placed into a zone 625 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 626 * structure. Rule (1) is implemented by modifying the source address selection 627 * algorithm so that the list of eligible addresses is filtered based on the 628 * sending process zone. 629 * 630 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 631 * across all zones, depending on their type. Here is the break-up: 632 * 633 * IRE type Shared/exclusive 634 * -------- ---------------- 635 * IRE_BROADCAST Exclusive 636 * IRE_DEFAULT (default routes) Shared (*) 637 * IRE_LOCAL Exclusive (x) 638 * IRE_LOOPBACK Exclusive 639 * IRE_PREFIX (net routes) Shared (*) 640 * IRE_CACHE Exclusive 641 * IRE_IF_NORESOLVER (interface routes) Exclusive 642 * IRE_IF_RESOLVER (interface routes) Exclusive 643 * IRE_HOST (host routes) Shared (*) 644 * 645 * (*) A zone can only use a default or off-subnet route if the gateway is 646 * directly reachable from the zone, that is, if the gateway's address matches 647 * one of the zone's logical interfaces. 648 * 649 * (x) IRE_LOCAL are handled a bit differently, since for all other entries 650 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source 651 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP 652 * address of the zone itself (the destination). Since IRE_LOCAL is used 653 * for communication between zones, ip_wput_ire has special logic to set 654 * the right source address when sending using an IRE_LOCAL. 655 * 656 * Furthermore, when ip_restrict_interzone_loopback is set (the default), 657 * ire_cache_lookup restricts loopback using an IRE_LOCAL 658 * between zone to the case when L2 would have conceptually looped the packet 659 * back, i.e. the loopback which is required since neither Ethernet drivers 660 * nor Ethernet hardware loops them back. This is the case when the normal 661 * routes (ignoring IREs with different zoneids) would send out the packet on 662 * the same ill as the ill with which is IRE_LOCAL is associated. 663 * 664 * Multiple zones can share a common broadcast address; typically all zones 665 * share the 255.255.255.255 address. Incoming as well as locally originated 666 * broadcast packets must be dispatched to all the zones on the broadcast 667 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 668 * since some zones may not be on the 10.16.72/24 network. To handle this, each 669 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 670 * sent to every zone that has an IRE_BROADCAST entry for the destination 671 * address on the input ill, see conn_wantpacket(). 672 * 673 * Applications in different zones can join the same multicast group address. 674 * For IPv4, group memberships are per-logical interface, so they're already 675 * inherently part of a zone. For IPv6, group memberships are per-physical 676 * interface, so we distinguish IPv6 group memberships based on group address, 677 * interface and zoneid. In both cases, received multicast packets are sent to 678 * every zone for which a group membership entry exists. On IPv6 we need to 679 * check that the target zone still has an address on the receiving physical 680 * interface; it could have been removed since the application issued the 681 * IPV6_JOIN_GROUP. 682 */ 683 684 /* 685 * Squeue Fanout flags: 686 * 0: No fanout. 687 * 1: Fanout across all squeues 688 */ 689 boolean_t ip_squeue_fanout = 0; 690 691 /* 692 * Maximum dups allowed per packet. 693 */ 694 uint_t ip_max_frag_dups = 10; 695 696 #define IS_SIMPLE_IPH(ipha) \ 697 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 698 699 /* RFC 1122 Conformance */ 700 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 701 702 #define ILL_MAX_NAMELEN LIFNAMSIZ 703 704 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 705 706 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag, 707 cred_t *credp, boolean_t isv6); 708 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t, 709 ipha_t **); 710 711 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t, 712 ip_stack_t *); 713 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 714 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 715 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); 716 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, 717 mblk_t *, int, ip_stack_t *); 718 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 719 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 720 ill_t *, zoneid_t); 721 static void icmp_options_update(ipha_t *); 722 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t, 723 ip_stack_t *); 724 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, 725 zoneid_t zoneid, ip_stack_t *); 726 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *); 727 static void icmp_redirect(ill_t *, mblk_t *); 728 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t, 729 ip_stack_t *); 730 731 static void ip_arp_news(queue_t *, mblk_t *); 732 static boolean_t ip_bind_get_ire_v4(mblk_t **, ire_t *, iulp_t *, ip_stack_t *); 733 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 734 char *ip_dot_addr(ipaddr_t, char *); 735 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 736 int ip_close(queue_t *, int); 737 static char *ip_dot_saddr(uchar_t *, char *); 738 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 739 boolean_t, boolean_t, ill_t *, zoneid_t); 740 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 741 boolean_t, boolean_t, zoneid_t); 742 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 743 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 744 static void ip_lrput(queue_t *, mblk_t *); 745 ipaddr_t ip_net_mask(ipaddr_t); 746 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, conn_t *, zoneid_t, 747 ip_stack_t *); 748 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 749 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *); 750 char *ip_nv_lookup(nv_t *, int); 751 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 752 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 753 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 754 static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t, 755 ipndp_t *, size_t); 756 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 757 void ip_rput(queue_t *, mblk_t *); 758 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 759 void *dummy_arg); 760 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 761 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *, 762 ip_stack_t *); 763 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 764 ire_t *, ip_stack_t *); 765 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *, 766 mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *); 767 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *, 768 ip_stack_t *); 769 static boolean_t ip_rput_fragment(ill_t *, ill_t *, mblk_t **, ipha_t *, 770 uint32_t *, uint16_t *); 771 int ip_snmp_get(queue_t *, mblk_t *, int); 772 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, 773 mib2_ipIfStatsEntry_t *, ip_stack_t *); 774 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *, 775 ip_stack_t *); 776 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *); 777 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst); 778 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst); 779 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst); 780 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst); 781 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *, 782 ip_stack_t *ipst); 783 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *, 784 ip_stack_t *ipst); 785 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *, 786 ip_stack_t *ipst); 787 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *, 788 ip_stack_t *ipst); 789 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *, 790 ip_stack_t *ipst); 791 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *, 792 ip_stack_t *ipst); 793 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *, 794 ip_stack_t *ipst); 795 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *, 796 ip_stack_t *ipst); 797 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int, 798 ip_stack_t *ipst); 799 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int, 800 ip_stack_t *ipst); 801 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 802 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 803 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); 804 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 805 static boolean_t ip_source_routed(ipha_t *, ip_stack_t *); 806 static boolean_t ip_source_route_included(ipha_t *); 807 static void ip_trash_ire_reclaim_stack(ip_stack_t *); 808 809 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, 810 zoneid_t, ip_stack_t *, conn_t *); 811 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *, 812 mblk_t *); 813 static void ip_wput_local_options(ipha_t *, ip_stack_t *); 814 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 815 zoneid_t, ip_stack_t *); 816 817 static void conn_drain_init(ip_stack_t *); 818 static void conn_drain_fini(ip_stack_t *); 819 static void conn_drain_tail(conn_t *connp, boolean_t closing); 820 821 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *); 822 static void conn_setqfull(conn_t *); 823 static void conn_clrqfull(conn_t *); 824 825 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns); 826 static void ip_stack_shutdown(netstackid_t stackid, void *arg); 827 static void ip_stack_fini(netstackid_t stackid, void *arg); 828 829 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 830 zoneid_t); 831 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 832 void *dummy_arg); 833 834 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 835 836 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 837 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 838 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 839 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 840 841 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 842 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 843 caddr_t, cred_t *); 844 extern int ip_helper_stream_setup(queue_t *, dev_t *, int, int, 845 cred_t *, boolean_t); 846 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 847 caddr_t cp, cred_t *cr); 848 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 849 cred_t *); 850 static int ip_squeue_switch(int); 851 852 static void *ip_kstat_init(netstackid_t, ip_stack_t *); 853 static void ip_kstat_fini(netstackid_t, kstat_t *); 854 static int ip_kstat_update(kstat_t *kp, int rw); 855 static void *icmp_kstat_init(netstackid_t); 856 static void icmp_kstat_fini(netstackid_t, kstat_t *); 857 static int icmp_kstat_update(kstat_t *kp, int rw); 858 static void *ip_kstat2_init(netstackid_t, ip_stat_t *); 859 static void ip_kstat2_fini(netstackid_t, kstat_t *); 860 861 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 862 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 863 864 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 865 ipha_t *, ill_t *, boolean_t, boolean_t); 866 867 static void ipobs_init(ip_stack_t *); 868 static void ipobs_fini(ip_stack_t *); 869 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 870 871 /* How long, in seconds, we allow frags to hang around. */ 872 #define IP_FRAG_TIMEOUT 15 873 #define IPV6_FRAG_TIMEOUT 60 874 875 /* 876 * Threshold which determines whether MDT should be used when 877 * generating IP fragments; payload size must be greater than 878 * this threshold for MDT to take place. 879 */ 880 #define IP_WPUT_FRAG_MDT_MIN 32768 881 882 /* Setable in /etc/system only */ 883 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 884 885 static long ip_rput_pullups; 886 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 887 888 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */ 889 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */ 890 891 int ip_debug; 892 893 #ifdef DEBUG 894 uint32_t ipsechw_debug = 0; 895 #endif 896 897 /* 898 * Multirouting/CGTP stuff 899 */ 900 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 901 902 /* 903 * XXX following really should only be in a header. Would need more 904 * header and .c clean up first. 905 */ 906 extern optdb_obj_t ip_opt_obj; 907 908 ulong_t ip_squeue_enter_unbound = 0; 909 910 /* 911 * Named Dispatch Parameter Table. 912 * All of these are alterable, within the min/max values given, at run time. 913 */ 914 static ipparam_t lcl_param_arr[] = { 915 /* min max value name */ 916 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 917 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 918 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 919 { 0, 1, 0, "ip_respond_to_timestamp"}, 920 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 921 { 0, 1, 1, "ip_send_redirects"}, 922 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 923 { 0, 10, 0, "ip_mrtdebug"}, 924 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 925 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 926 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 927 { 1, 255, 255, "ip_def_ttl" }, 928 { 0, 1, 0, "ip_forward_src_routed"}, 929 { 0, 256, 32, "ip_wroff_extra" }, 930 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 931 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 932 { 0, 1, 1, "ip_path_mtu_discovery" }, 933 { 0, 240, 30, "ip_ignore_delete_time" }, 934 { 0, 1, 0, "ip_ignore_redirect" }, 935 { 0, 1, 1, "ip_output_queue" }, 936 { 1, 254, 1, "ip_broadcast_ttl" }, 937 { 0, 99999, 100, "ip_icmp_err_interval" }, 938 { 1, 99999, 10, "ip_icmp_err_burst" }, 939 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 940 { 0, 1, 0, "ip_strict_dst_multihoming" }, 941 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 942 { 0, 1, 0, "ipsec_override_persocket_policy" }, 943 { 0, 1, 1, "icmp_accept_clear_messages" }, 944 { 0, 1, 1, "igmp_accept_clear_messages" }, 945 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 946 "ip_ndp_delay_first_probe_time"}, 947 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 948 "ip_ndp_max_unicast_solicit"}, 949 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 950 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 951 { 0, 1, 0, "ip6_forward_src_routed"}, 952 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 953 { 0, 1, 1, "ip6_send_redirects"}, 954 { 0, 1, 0, "ip6_ignore_redirect" }, 955 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 956 957 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 958 959 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 960 961 { 0, 1, 1, "pim_accept_clear_messages" }, 962 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 963 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 964 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 965 { 0, 15, 0, "ip_policy_mask" }, 966 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 967 { 0, 255, 1, "ip_multirt_ttl" }, 968 { 0, 1, 1, "ip_multidata_outbound" }, 969 { 0, 3600000, 300000, "ip_ndp_defense_interval" }, 970 { 0, 999999, 60*60*24, "ip_max_temp_idle" }, 971 { 0, 1000, 1, "ip_max_temp_defend" }, 972 { 0, 1000, 3, "ip_max_defend" }, 973 { 0, 999999, 30, "ip_defend_interval" }, 974 { 0, 3600000, 300000, "ip_dup_recovery" }, 975 { 0, 1, 1, "ip_restrict_interzone_loopback" }, 976 { 0, 1, 1, "ip_lso_outbound" }, 977 { IGMP_V1_ROUTER, IGMP_V3_ROUTER, IGMP_V3_ROUTER, "igmp_max_version" }, 978 { MLD_V1_ROUTER, MLD_V2_ROUTER, MLD_V2_ROUTER, "mld_max_version" }, 979 { 68, 65535, 576, "ip_pmtu_min" }, 980 #ifdef DEBUG 981 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 982 #else 983 { 0, 0, 0, "" }, 984 #endif 985 }; 986 987 /* 988 * Extended NDP table 989 * The addresses for the first two are filled in to be ips_ip_g_forward 990 * and ips_ipv6_forward at init time. 991 */ 992 static ipndp_t lcl_ndp_arr[] = { 993 /* getf setf data name */ 994 #define IPNDP_IP_FORWARDING_OFFSET 0 995 { ip_param_generic_get, ip_forward_set, NULL, 996 "ip_forwarding" }, 997 #define IPNDP_IP6_FORWARDING_OFFSET 1 998 { ip_param_generic_get, ip_forward_set, NULL, 999 "ip6_forwarding" }, 1000 { ip_param_generic_get, ip_input_proc_set, 1001 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 1002 { ip_param_generic_get, ip_int_set, 1003 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 1004 #define IPNDP_CGTP_FILTER_OFFSET 4 1005 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL, 1006 "ip_cgtp_filter" }, 1007 { ip_param_generic_get, ip_int_set, (caddr_t)&ip_debug, 1008 "ip_debug" }, 1009 }; 1010 1011 /* 1012 * Table of IP ioctls encoding the various properties of the ioctl and 1013 * indexed based on the last byte of the ioctl command. Occasionally there 1014 * is a clash, and there is more than 1 ioctl with the same last byte. 1015 * In such a case 1 ioctl is encoded in the ndx table and the remaining 1016 * ioctls are encoded in the misc table. An entry in the ndx table is 1017 * retrieved by indexing on the last byte of the ioctl command and comparing 1018 * the ioctl command with the value in the ndx table. In the event of a 1019 * mismatch the misc table is then searched sequentially for the desired 1020 * ioctl command. 1021 * 1022 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 1023 */ 1024 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 1025 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1026 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1027 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1028 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1029 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1030 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1031 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1032 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1033 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1034 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1035 1036 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 1037 MISC_CMD, ip_siocaddrt, NULL }, 1038 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 1039 MISC_CMD, ip_siocdelrt, NULL }, 1040 1041 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1042 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1043 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD, 1044 IF_CMD, ip_sioctl_get_addr, NULL }, 1045 1046 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1047 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1048 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 1049 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL }, 1050 1051 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 1052 IPI_PRIV | IPI_WR, 1053 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1054 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 1055 IPI_MODOK | IPI_GET_CMD, 1056 IF_CMD, ip_sioctl_get_flags, NULL }, 1057 1058 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1059 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1060 1061 /* copyin size cannot be coded for SIOCGIFCONF */ 1062 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD, 1063 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1064 1065 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1066 IF_CMD, ip_sioctl_mtu, NULL }, 1067 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD, 1068 IF_CMD, ip_sioctl_get_mtu, NULL }, 1069 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 1070 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL }, 1071 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1072 IF_CMD, ip_sioctl_brdaddr, NULL }, 1073 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 1074 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL }, 1075 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1076 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1077 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 1078 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL }, 1079 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 1080 IF_CMD, ip_sioctl_metric, NULL }, 1081 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1082 1083 /* See 166-168 below for extended SIOC*XARP ioctls */ 1084 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR, 1085 ARP_CMD, ip_sioctl_arp, NULL }, 1086 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD, 1087 ARP_CMD, ip_sioctl_arp, NULL }, 1088 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR, 1089 ARP_CMD, ip_sioctl_arp, NULL }, 1090 1091 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1092 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1093 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1094 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1095 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1096 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1097 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1098 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1099 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1100 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1101 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1102 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1103 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1104 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1105 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1106 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1107 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1108 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1109 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1110 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1111 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1112 1113 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 1114 MISC_CMD, if_unitsel, if_unitsel_restart }, 1115 1116 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1117 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1118 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1119 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1120 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1121 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1122 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1123 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1124 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1125 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1126 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1127 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1128 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1129 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1130 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1131 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1132 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1133 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1134 1135 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 1136 IPI_PRIV | IPI_WR | IPI_MODOK, 1137 IF_CMD, ip_sioctl_sifname, NULL }, 1138 1139 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1140 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1141 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1142 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1143 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1144 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1145 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1146 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1147 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1148 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1149 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1150 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1151 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1152 1153 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD, 1154 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 1155 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD, 1156 IF_CMD, ip_sioctl_get_muxid, NULL }, 1157 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 1158 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL }, 1159 1160 /* Both if and lif variants share same func */ 1161 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD, 1162 IF_CMD, ip_sioctl_get_lifindex, NULL }, 1163 /* Both if and lif variants share same func */ 1164 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 1165 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL }, 1166 1167 /* copyin size cannot be coded for SIOCGIFCONF */ 1168 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD, 1169 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1170 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1171 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1172 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1173 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1174 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1175 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1176 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1177 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1178 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1179 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1180 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1181 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1182 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1183 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1184 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1185 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1186 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1187 1188 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 1189 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif, 1190 ip_sioctl_removeif_restart }, 1191 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 1192 IPI_GET_CMD | IPI_PRIV | IPI_WR, 1193 LIF_CMD, ip_sioctl_addif, NULL }, 1194 #define SIOCLIFADDR_NDX 112 1195 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1196 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1197 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 1198 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL }, 1199 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1200 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1201 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 1202 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 1203 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 1204 IPI_PRIV | IPI_WR, 1205 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1206 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 1207 IPI_GET_CMD | IPI_MODOK, 1208 LIF_CMD, ip_sioctl_get_flags, NULL }, 1209 1210 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1211 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1212 1213 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, 1214 ip_sioctl_get_lifconf, NULL }, 1215 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1216 LIF_CMD, ip_sioctl_mtu, NULL }, 1217 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD, 1218 LIF_CMD, ip_sioctl_get_mtu, NULL }, 1219 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 1220 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 1221 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1222 LIF_CMD, ip_sioctl_brdaddr, NULL }, 1223 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 1224 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL }, 1225 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1226 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1227 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 1228 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL }, 1229 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1230 LIF_CMD, ip_sioctl_metric, NULL }, 1231 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1232 IPI_PRIV | IPI_WR | IPI_MODOK, 1233 LIF_CMD, ip_sioctl_slifname, 1234 ip_sioctl_slifname_restart }, 1235 1236 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD, 1237 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1238 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1239 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL }, 1240 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1241 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL }, 1242 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1243 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1244 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1245 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 }, 1246 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1247 LIF_CMD, ip_sioctl_token, NULL }, 1248 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1249 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL }, 1250 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1251 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1252 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1253 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL }, 1254 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1255 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1256 1257 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1258 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1259 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1260 LIF_CMD, ip_siocdelndp_v6, NULL }, 1261 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1262 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1263 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1264 LIF_CMD, ip_siocsetndp_v6, NULL }, 1265 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1266 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1267 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1268 MISC_CMD, ip_sioctl_tonlink, NULL }, 1269 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1270 MISC_CMD, ip_sioctl_tmysite, NULL }, 1271 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), 0, 1272 TUN_CMD, ip_sioctl_tunparam, NULL }, 1273 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), 1274 IPI_PRIV | IPI_WR, 1275 TUN_CMD, ip_sioctl_tunparam, NULL }, 1276 1277 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1278 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1279 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1280 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1281 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1282 1283 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1284 1285 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD, 1286 LIF_CMD, ip_sioctl_get_binding, NULL }, 1287 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1288 IPI_PRIV | IPI_WR, 1289 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1290 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1291 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL }, 1292 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t), 1293 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL }, 1294 1295 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1296 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1297 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1298 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1299 1300 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1301 1302 /* These are handled in ip_sioctl_copyin_setup itself */ 1303 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1304 MISC_CMD, NULL, NULL }, 1305 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1306 MISC_CMD, NULL, NULL }, 1307 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1308 1309 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, 1310 ip_sioctl_get_lifconf, NULL }, 1311 1312 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR, 1313 XARP_CMD, ip_sioctl_arp, NULL }, 1314 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD, 1315 XARP_CMD, ip_sioctl_arp, NULL }, 1316 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR, 1317 XARP_CMD, ip_sioctl_arp, NULL }, 1318 1319 /* SIOCPOPSOCKFS is not handled by IP */ 1320 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1321 1322 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1323 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1324 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1325 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone, 1326 ip_sioctl_slifzone_restart }, 1327 /* 172-174 are SCTP ioctls and not handled by IP */ 1328 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1329 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1330 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1331 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1332 IPI_GET_CMD, LIF_CMD, 1333 ip_sioctl_get_lifusesrc, 0 }, 1334 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1335 IPI_PRIV | IPI_WR, 1336 LIF_CMD, ip_sioctl_slifusesrc, 1337 NULL }, 1338 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1339 ip_sioctl_get_lifsrcof, NULL }, 1340 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1341 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1342 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1343 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1344 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1345 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1346 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1347 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1348 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1349 /* SIOCSENABLESDP is handled by SDP */ 1350 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL }, 1351 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL }, 1352 }; 1353 1354 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1355 1356 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1357 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), 1358 IPI_GET_CMD, TUN_CMD, ip_sioctl_tunparam, NULL }, 1359 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, 1360 TUN_CMD, ip_sioctl_tunparam, NULL }, 1361 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1362 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1363 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1364 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1365 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, 1366 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1367 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1368 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD, 1369 MISC_CMD, mrt_ioctl}, 1370 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD, 1371 MISC_CMD, mrt_ioctl}, 1372 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD, 1373 MISC_CMD, mrt_ioctl} 1374 }; 1375 1376 int ip_misc_ioctl_count = 1377 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1378 1379 int conn_drain_nthreads; /* Number of drainers reqd. */ 1380 /* Settable in /etc/system */ 1381 /* Defined in ip_ire.c */ 1382 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1383 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1384 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1385 1386 static nv_t ire_nv_arr[] = { 1387 { IRE_BROADCAST, "BROADCAST" }, 1388 { IRE_LOCAL, "LOCAL" }, 1389 { IRE_LOOPBACK, "LOOPBACK" }, 1390 { IRE_CACHE, "CACHE" }, 1391 { IRE_DEFAULT, "DEFAULT" }, 1392 { IRE_PREFIX, "PREFIX" }, 1393 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1394 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1395 { IRE_HOST, "HOST" }, 1396 { 0 } 1397 }; 1398 1399 nv_t *ire_nv_tbl = ire_nv_arr; 1400 1401 /* Simple ICMP IP Header Template */ 1402 static ipha_t icmp_ipha = { 1403 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1404 }; 1405 1406 struct module_info ip_mod_info = { 1407 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT, 1408 IP_MOD_LOWAT 1409 }; 1410 1411 /* 1412 * Duplicate static symbols within a module confuses mdb; so we avoid the 1413 * problem by making the symbols here distinct from those in udp.c. 1414 */ 1415 1416 /* 1417 * Entry points for IP as a device and as a module. 1418 * FIXME: down the road we might want a separate module and driver qinit. 1419 * We have separate open functions for the /dev/ip and /dev/ip6 devices. 1420 */ 1421 static struct qinit iprinitv4 = { 1422 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL, 1423 &ip_mod_info 1424 }; 1425 1426 struct qinit iprinitv6 = { 1427 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL, 1428 &ip_mod_info 1429 }; 1430 1431 static struct qinit ipwinitv4 = { 1432 (pfi_t)ip_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL, 1433 &ip_mod_info 1434 }; 1435 1436 struct qinit ipwinitv6 = { 1437 (pfi_t)ip_wput_v6, (pfi_t)ip_wsrv, NULL, NULL, NULL, 1438 &ip_mod_info 1439 }; 1440 1441 static struct qinit iplrinit = { 1442 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL, 1443 &ip_mod_info 1444 }; 1445 1446 static struct qinit iplwinit = { 1447 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL, 1448 &ip_mod_info 1449 }; 1450 1451 /* For AF_INET aka /dev/ip */ 1452 struct streamtab ipinfov4 = { 1453 &iprinitv4, &ipwinitv4, &iplrinit, &iplwinit 1454 }; 1455 1456 /* For AF_INET6 aka /dev/ip6 */ 1457 struct streamtab ipinfov6 = { 1458 &iprinitv6, &ipwinitv6, &iplrinit, &iplwinit 1459 }; 1460 1461 #ifdef DEBUG 1462 static boolean_t skip_sctp_cksum = B_FALSE; 1463 #endif 1464 1465 /* 1466 * Prepend the zoneid using an ipsec_out_t for later use by functions like 1467 * ip_rput_v6(), ip_output(), etc. If the message 1468 * block already has a M_CTL at the front of it, then simply set the zoneid 1469 * appropriately. 1470 */ 1471 mblk_t * 1472 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst) 1473 { 1474 mblk_t *first_mp; 1475 ipsec_out_t *io; 1476 1477 ASSERT(zoneid != ALL_ZONES); 1478 if (mp->b_datap->db_type == M_CTL) { 1479 io = (ipsec_out_t *)mp->b_rptr; 1480 ASSERT(io->ipsec_out_type == IPSEC_OUT); 1481 io->ipsec_out_zoneid = zoneid; 1482 return (mp); 1483 } 1484 1485 first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack); 1486 if (first_mp == NULL) 1487 return (NULL); 1488 io = (ipsec_out_t *)first_mp->b_rptr; 1489 /* This is not a secure packet */ 1490 io->ipsec_out_secure = B_FALSE; 1491 io->ipsec_out_zoneid = zoneid; 1492 first_mp->b_cont = mp; 1493 return (first_mp); 1494 } 1495 1496 /* 1497 * Copy an M_CTL-tagged message, preserving reference counts appropriately. 1498 */ 1499 mblk_t * 1500 ip_copymsg(mblk_t *mp) 1501 { 1502 mblk_t *nmp; 1503 ipsec_info_t *in; 1504 1505 if (mp->b_datap->db_type != M_CTL) 1506 return (copymsg(mp)); 1507 1508 in = (ipsec_info_t *)mp->b_rptr; 1509 1510 /* 1511 * Note that M_CTL is also used for delivering ICMP error messages 1512 * upstream to transport layers. 1513 */ 1514 if (in->ipsec_info_type != IPSEC_OUT && 1515 in->ipsec_info_type != IPSEC_IN) 1516 return (copymsg(mp)); 1517 1518 nmp = copymsg(mp->b_cont); 1519 1520 if (in->ipsec_info_type == IPSEC_OUT) { 1521 return (ipsec_out_tag(mp, nmp, 1522 ((ipsec_out_t *)in)->ipsec_out_ns)); 1523 } else { 1524 return (ipsec_in_tag(mp, nmp, 1525 ((ipsec_in_t *)in)->ipsec_in_ns)); 1526 } 1527 } 1528 1529 /* Generate an ICMP fragmentation needed message. */ 1530 static void 1531 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid, 1532 ip_stack_t *ipst) 1533 { 1534 icmph_t icmph; 1535 mblk_t *first_mp; 1536 boolean_t mctl_present; 1537 1538 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1539 1540 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 1541 if (mctl_present) 1542 freeb(first_mp); 1543 return; 1544 } 1545 1546 bzero(&icmph, sizeof (icmph_t)); 1547 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1548 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1549 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1550 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded); 1551 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 1552 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 1553 ipst); 1554 } 1555 1556 /* 1557 * icmp_inbound deals with ICMP messages in the following ways. 1558 * 1559 * 1) It needs to send a reply back and possibly delivering it 1560 * to the "interested" upper clients. 1561 * 2) It needs to send it to the upper clients only. 1562 * 3) It needs to change some values in IP only. 1563 * 4) It needs to change some values in IP and upper layers e.g TCP. 1564 * 1565 * We need to accomodate icmp messages coming in clear until we get 1566 * everything secure from the wire. If icmp_accept_clear_messages 1567 * is zero we check with the global policy and act accordingly. If 1568 * it is non-zero, we accept the message without any checks. But 1569 * *this does not mean* that this will be delivered to the upper 1570 * clients. By accepting we might send replies back, change our MTU 1571 * value etc. but delivery to the ULP/clients depends on their policy 1572 * dispositions. 1573 * 1574 * We handle the above 4 cases in the context of IPsec in the 1575 * following way : 1576 * 1577 * 1) Send the reply back in the same way as the request came in. 1578 * If it came in encrypted, it goes out encrypted. If it came in 1579 * clear, it goes out in clear. Thus, this will prevent chosen 1580 * plain text attack. 1581 * 2) The client may or may not expect things to come in secure. 1582 * If it comes in secure, the policy constraints are checked 1583 * before delivering it to the upper layers. If it comes in 1584 * clear, ipsec_inbound_accept_clear will decide whether to 1585 * accept this in clear or not. In both the cases, if the returned 1586 * message (IP header + 8 bytes) that caused the icmp message has 1587 * AH/ESP headers, it is sent up to AH/ESP for validation before 1588 * sending up. If there are only 8 bytes of returned message, then 1589 * upper client will not be notified. 1590 * 3) Check with global policy to see whether it matches the constaints. 1591 * But this will be done only if icmp_accept_messages_in_clear is 1592 * zero. 1593 * 4) If we need to change both in IP and ULP, then the decision taken 1594 * while affecting the values in IP and while delivering up to TCP 1595 * should be the same. 1596 * 1597 * There are two cases. 1598 * 1599 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1600 * failed), we will not deliver it to the ULP, even though they 1601 * are *willing* to accept in *clear*. This is fine as our global 1602 * disposition to icmp messages asks us reject the datagram. 1603 * 1604 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1605 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1606 * to deliver it to ULP (policy failed), it can lead to 1607 * consistency problems. The cases known at this time are 1608 * ICMP_DESTINATION_UNREACHABLE messages with following code 1609 * values : 1610 * 1611 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1612 * and Upper layer rejects. Then the communication will 1613 * come to a stop. This is solved by making similar decisions 1614 * at both levels. Currently, when we are unable to deliver 1615 * to the Upper Layer (due to policy failures) while IP has 1616 * adjusted ire_max_frag, the next outbound datagram would 1617 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1618 * will be with the right level of protection. Thus the right 1619 * value will be communicated even if we are not able to 1620 * communicate when we get from the wire initially. But this 1621 * assumes there would be at least one outbound datagram after 1622 * IP has adjusted its ire_max_frag value. To make things 1623 * simpler, we accept in clear after the validation of 1624 * AH/ESP headers. 1625 * 1626 * - Other ICMP ERRORS : We may not be able to deliver it to the 1627 * upper layer depending on the level of protection the upper 1628 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1629 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1630 * should be accepted in clear when the Upper layer expects secure. 1631 * Thus the communication may get aborted by some bad ICMP 1632 * packets. 1633 * 1634 * IPQoS Notes: 1635 * The only instance when a packet is sent for processing is when there 1636 * isn't an ICMP client and if we are interested in it. 1637 * If there is a client, IPPF processing will take place in the 1638 * ip_fanout_proto routine. 1639 * 1640 * Zones notes: 1641 * The packet is only processed in the context of the specified zone: typically 1642 * only this zone will reply to an echo request, and only interested clients in 1643 * this zone will receive a copy of the packet. This means that the caller must 1644 * call icmp_inbound() for each relevant zone. 1645 */ 1646 static void 1647 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, 1648 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, 1649 ill_t *recv_ill, zoneid_t zoneid) 1650 { 1651 icmph_t *icmph; 1652 ipha_t *ipha; 1653 int iph_hdr_length; 1654 int hdr_length; 1655 boolean_t interested; 1656 uint32_t ts; 1657 uchar_t *wptr; 1658 ipif_t *ipif; 1659 mblk_t *first_mp; 1660 ipsec_in_t *ii; 1661 timestruc_t now; 1662 uint32_t ill_index; 1663 ip_stack_t *ipst; 1664 1665 ASSERT(ill != NULL); 1666 ipst = ill->ill_ipst; 1667 1668 first_mp = mp; 1669 if (mctl_present) { 1670 mp = first_mp->b_cont; 1671 ASSERT(mp != NULL); 1672 } 1673 1674 ipha = (ipha_t *)mp->b_rptr; 1675 if (ipst->ips_icmp_accept_clear_messages == 0) { 1676 first_mp = ipsec_check_global_policy(first_mp, NULL, 1677 ipha, NULL, mctl_present, ipst->ips_netstack); 1678 if (first_mp == NULL) 1679 return; 1680 } 1681 1682 /* 1683 * On a labeled system, we have to check whether the zone itself is 1684 * permitted to receive raw traffic. 1685 */ 1686 if (is_system_labeled()) { 1687 if (zoneid == ALL_ZONES) 1688 zoneid = tsol_packet_to_zoneid(mp); 1689 if (!tsol_can_accept_raw(mp, B_FALSE)) { 1690 ip1dbg(("icmp_inbound: zone %d can't receive raw", 1691 zoneid)); 1692 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1693 freemsg(first_mp); 1694 return; 1695 } 1696 } 1697 1698 /* 1699 * We have accepted the ICMP message. It means that we will 1700 * respond to the packet if needed. It may not be delivered 1701 * to the upper client depending on the policy constraints 1702 * and the disposition in ipsec_inbound_accept_clear. 1703 */ 1704 1705 ASSERT(ill != NULL); 1706 1707 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs); 1708 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1709 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { 1710 /* Last chance to get real. */ 1711 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { 1712 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1713 freemsg(first_mp); 1714 return; 1715 } 1716 /* Refresh iph following the pullup. */ 1717 ipha = (ipha_t *)mp->b_rptr; 1718 } 1719 /* ICMP header checksum, including checksum field, should be zero. */ 1720 if (sum_valid ? (sum != 0 && sum != 0xFFFF) : 1721 IP_CSUM(mp, iph_hdr_length, 0)) { 1722 BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs); 1723 freemsg(first_mp); 1724 return; 1725 } 1726 /* The IP header will always be a multiple of four bytes */ 1727 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1728 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, 1729 icmph->icmph_code)); 1730 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1731 /* We will set "interested" to "true" if we want a copy */ 1732 interested = B_FALSE; 1733 switch (icmph->icmph_type) { 1734 case ICMP_ECHO_REPLY: 1735 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps); 1736 break; 1737 case ICMP_DEST_UNREACHABLE: 1738 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1739 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded); 1740 interested = B_TRUE; /* Pass up to transport */ 1741 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs); 1742 break; 1743 case ICMP_SOURCE_QUENCH: 1744 interested = B_TRUE; /* Pass up to transport */ 1745 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs); 1746 break; 1747 case ICMP_REDIRECT: 1748 if (!ipst->ips_ip_ignore_redirect) 1749 interested = B_TRUE; 1750 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects); 1751 break; 1752 case ICMP_ECHO_REQUEST: 1753 /* 1754 * Whether to respond to echo requests that come in as IP 1755 * broadcasts or as IP multicast is subject to debate 1756 * (what isn't?). We aim to please, you pick it. 1757 * Default is do it. 1758 */ 1759 if (!broadcast && !CLASSD(ipha->ipha_dst)) { 1760 /* unicast: always respond */ 1761 interested = B_TRUE; 1762 } else if (CLASSD(ipha->ipha_dst)) { 1763 /* multicast: respond based on tunable */ 1764 interested = ipst->ips_ip_g_resp_to_echo_mcast; 1765 } else if (broadcast) { 1766 /* broadcast: respond based on tunable */ 1767 interested = ipst->ips_ip_g_resp_to_echo_bcast; 1768 } 1769 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos); 1770 break; 1771 case ICMP_ROUTER_ADVERTISEMENT: 1772 case ICMP_ROUTER_SOLICITATION: 1773 break; 1774 case ICMP_TIME_EXCEEDED: 1775 interested = B_TRUE; /* Pass up to transport */ 1776 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds); 1777 break; 1778 case ICMP_PARAM_PROBLEM: 1779 interested = B_TRUE; /* Pass up to transport */ 1780 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs); 1781 break; 1782 case ICMP_TIME_STAMP_REQUEST: 1783 /* Response to Time Stamp Requests is local policy. */ 1784 if (ipst->ips_ip_g_resp_to_timestamp && 1785 /* So is whether to respond if it was an IP broadcast. */ 1786 (!broadcast || ipst->ips_ip_g_resp_to_timestamp_bcast)) { 1787 int tstamp_len = 3 * sizeof (uint32_t); 1788 1789 if (wptr + tstamp_len > mp->b_wptr) { 1790 if (!pullupmsg(mp, wptr + tstamp_len - 1791 mp->b_rptr)) { 1792 BUMP_MIB(ill->ill_ip_mib, 1793 ipIfStatsInDiscards); 1794 freemsg(first_mp); 1795 return; 1796 } 1797 /* Refresh ipha following the pullup. */ 1798 ipha = (ipha_t *)mp->b_rptr; 1799 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1800 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1801 } 1802 interested = B_TRUE; 1803 } 1804 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps); 1805 break; 1806 case ICMP_TIME_STAMP_REPLY: 1807 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps); 1808 break; 1809 case ICMP_INFO_REQUEST: 1810 /* Per RFC 1122 3.2.2.7, ignore this. */ 1811 case ICMP_INFO_REPLY: 1812 break; 1813 case ICMP_ADDRESS_MASK_REQUEST: 1814 if ((ipst->ips_ip_respond_to_address_mask_broadcast || 1815 !broadcast) && 1816 /* TODO m_pullup of complete header? */ 1817 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) { 1818 interested = B_TRUE; 1819 } 1820 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks); 1821 break; 1822 case ICMP_ADDRESS_MASK_REPLY: 1823 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps); 1824 break; 1825 default: 1826 interested = B_TRUE; /* Pass up to transport */ 1827 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns); 1828 break; 1829 } 1830 /* See if there is an ICMP client. */ 1831 if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) { 1832 /* If there is an ICMP client and we want one too, copy it. */ 1833 mblk_t *first_mp1; 1834 1835 if (!interested) { 1836 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1837 ip_policy, recv_ill, zoneid); 1838 return; 1839 } 1840 first_mp1 = ip_copymsg(first_mp); 1841 if (first_mp1 != NULL) { 1842 ip_fanout_proto(q, first_mp1, ill, ipha, 1843 0, mctl_present, ip_policy, recv_ill, zoneid); 1844 } 1845 } else if (!interested) { 1846 freemsg(first_mp); 1847 return; 1848 } else { 1849 /* 1850 * Initiate policy processing for this packet if ip_policy 1851 * is true. 1852 */ 1853 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 1854 ill_index = ill->ill_phyint->phyint_ifindex; 1855 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1856 if (mp == NULL) { 1857 if (mctl_present) { 1858 freeb(first_mp); 1859 } 1860 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1861 return; 1862 } 1863 } 1864 } 1865 /* We want to do something with it. */ 1866 /* Check db_ref to make sure we can modify the packet. */ 1867 if (mp->b_datap->db_ref > 1) { 1868 mblk_t *first_mp1; 1869 1870 first_mp1 = ip_copymsg(first_mp); 1871 freemsg(first_mp); 1872 if (!first_mp1) { 1873 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1874 return; 1875 } 1876 first_mp = first_mp1; 1877 if (mctl_present) { 1878 mp = first_mp->b_cont; 1879 ASSERT(mp != NULL); 1880 } else { 1881 mp = first_mp; 1882 } 1883 ipha = (ipha_t *)mp->b_rptr; 1884 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1885 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1886 } 1887 switch (icmph->icmph_type) { 1888 case ICMP_ADDRESS_MASK_REQUEST: 1889 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1890 if (ipif == NULL) { 1891 freemsg(first_mp); 1892 return; 1893 } 1894 /* 1895 * outging interface must be IPv4 1896 */ 1897 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1898 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1899 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1900 ipif_refrele(ipif); 1901 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps); 1902 break; 1903 case ICMP_ECHO_REQUEST: 1904 icmph->icmph_type = ICMP_ECHO_REPLY; 1905 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps); 1906 break; 1907 case ICMP_TIME_STAMP_REQUEST: { 1908 uint32_t *tsp; 1909 1910 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1911 tsp = (uint32_t *)wptr; 1912 tsp++; /* Skip past 'originate time' */ 1913 /* Compute # of milliseconds since midnight */ 1914 gethrestime(&now); 1915 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1916 now.tv_nsec / (NANOSEC / MILLISEC); 1917 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1918 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1919 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps); 1920 break; 1921 } 1922 default: 1923 ipha = (ipha_t *)&icmph[1]; 1924 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1925 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1926 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1927 freemsg(first_mp); 1928 return; 1929 } 1930 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1931 ipha = (ipha_t *)&icmph[1]; 1932 } 1933 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1934 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1935 freemsg(first_mp); 1936 return; 1937 } 1938 hdr_length = IPH_HDR_LENGTH(ipha); 1939 if (hdr_length < sizeof (ipha_t)) { 1940 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1941 freemsg(first_mp); 1942 return; 1943 } 1944 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 1945 if (!pullupmsg(mp, 1946 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 1947 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1948 freemsg(first_mp); 1949 return; 1950 } 1951 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1952 ipha = (ipha_t *)&icmph[1]; 1953 } 1954 switch (icmph->icmph_type) { 1955 case ICMP_REDIRECT: 1956 /* 1957 * As there is no upper client to deliver, we don't 1958 * need the first_mp any more. 1959 */ 1960 if (mctl_present) { 1961 freeb(first_mp); 1962 } 1963 icmp_redirect(ill, mp); 1964 return; 1965 case ICMP_DEST_UNREACHABLE: 1966 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 1967 if (!icmp_inbound_too_big(icmph, ipha, ill, 1968 zoneid, mp, iph_hdr_length, ipst)) { 1969 freemsg(first_mp); 1970 return; 1971 } 1972 /* 1973 * icmp_inbound_too_big() may alter mp. 1974 * Resynch ipha and icmph accordingly. 1975 */ 1976 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1977 ipha = (ipha_t *)&icmph[1]; 1978 } 1979 /* FALLTHRU */ 1980 default : 1981 /* 1982 * IPQoS notes: Since we have already done IPQoS 1983 * processing we don't want to do it again in 1984 * the fanout routines called by 1985 * icmp_inbound_error_fanout, hence the last 1986 * argument, ip_policy, is B_FALSE. 1987 */ 1988 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 1989 ipha, iph_hdr_length, hdr_length, mctl_present, 1990 B_FALSE, recv_ill, zoneid); 1991 } 1992 return; 1993 } 1994 /* Send out an ICMP packet */ 1995 icmph->icmph_checksum = 0; 1996 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 1997 if (broadcast || CLASSD(ipha->ipha_dst)) { 1998 ipif_t *ipif_chosen; 1999 /* 2000 * Make it look like it was directed to us, so we don't look 2001 * like a fool with a broadcast or multicast source address. 2002 */ 2003 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 2004 /* 2005 * Make sure that we haven't grabbed an interface that's DOWN. 2006 */ 2007 if (ipif != NULL) { 2008 ipif_chosen = ipif_select_source(ipif->ipif_ill, 2009 ipha->ipha_src, zoneid); 2010 if (ipif_chosen != NULL) { 2011 ipif_refrele(ipif); 2012 ipif = ipif_chosen; 2013 } 2014 } 2015 if (ipif == NULL) { 2016 ip0dbg(("icmp_inbound: " 2017 "No source for broadcast/multicast:\n" 2018 "\tsrc 0x%x dst 0x%x ill %p " 2019 "ipif_lcl_addr 0x%x\n", 2020 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 2021 (void *)ill, 2022 ill->ill_ipif->ipif_lcl_addr)); 2023 freemsg(first_mp); 2024 return; 2025 } 2026 ASSERT(ipif != NULL && !ipif->ipif_isv6); 2027 ipha->ipha_dst = ipif->ipif_src_addr; 2028 ipif_refrele(ipif); 2029 } 2030 /* Reset time to live. */ 2031 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 2032 { 2033 /* Swap source and destination addresses */ 2034 ipaddr_t tmp; 2035 2036 tmp = ipha->ipha_src; 2037 ipha->ipha_src = ipha->ipha_dst; 2038 ipha->ipha_dst = tmp; 2039 } 2040 ipha->ipha_ident = 0; 2041 if (!IS_SIMPLE_IPH(ipha)) 2042 icmp_options_update(ipha); 2043 2044 if (!mctl_present) { 2045 /* 2046 * This packet should go out the same way as it 2047 * came in i.e in clear. To make sure that global 2048 * policy will not be applied to this in ip_wput_ire, 2049 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2050 */ 2051 ASSERT(first_mp == mp); 2052 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2053 if (first_mp == NULL) { 2054 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2055 freemsg(mp); 2056 return; 2057 } 2058 ii = (ipsec_in_t *)first_mp->b_rptr; 2059 2060 /* This is not a secure packet */ 2061 ii->ipsec_in_secure = B_FALSE; 2062 first_mp->b_cont = mp; 2063 } else { 2064 ii = (ipsec_in_t *)first_mp->b_rptr; 2065 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ 2066 } 2067 ii->ipsec_in_zoneid = zoneid; 2068 ASSERT(zoneid != ALL_ZONES); 2069 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2070 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2071 return; 2072 } 2073 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 2074 put(WR(q), first_mp); 2075 } 2076 2077 static ipaddr_t 2078 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) 2079 { 2080 conn_t *connp; 2081 connf_t *connfp; 2082 ipaddr_t nexthop_addr = INADDR_ANY; 2083 int hdr_length = IPH_HDR_LENGTH(ipha); 2084 uint16_t *up; 2085 uint32_t ports; 2086 ip_stack_t *ipst = ill->ill_ipst; 2087 2088 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2089 switch (ipha->ipha_protocol) { 2090 case IPPROTO_TCP: 2091 { 2092 tcph_t *tcph; 2093 2094 /* do a reverse lookup */ 2095 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2096 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, 2097 TCPS_LISTEN, ipst); 2098 break; 2099 } 2100 case IPPROTO_UDP: 2101 { 2102 uint32_t dstport, srcport; 2103 2104 ((uint16_t *)&ports)[0] = up[1]; 2105 ((uint16_t *)&ports)[1] = up[0]; 2106 2107 /* Extract ports in net byte order */ 2108 dstport = htons(ntohl(ports) & 0xFFFF); 2109 srcport = htons(ntohl(ports) >> 16); 2110 2111 connfp = &ipst->ips_ipcl_udp_fanout[ 2112 IPCL_UDP_HASH(dstport, ipst)]; 2113 mutex_enter(&connfp->connf_lock); 2114 connp = connfp->connf_head; 2115 2116 /* do a reverse lookup */ 2117 while ((connp != NULL) && 2118 (!IPCL_UDP_MATCH(connp, dstport, 2119 ipha->ipha_src, srcport, ipha->ipha_dst) || 2120 !IPCL_ZONE_MATCH(connp, zoneid))) { 2121 connp = connp->conn_next; 2122 } 2123 if (connp != NULL) 2124 CONN_INC_REF(connp); 2125 mutex_exit(&connfp->connf_lock); 2126 break; 2127 } 2128 case IPPROTO_SCTP: 2129 { 2130 in6_addr_t map_src, map_dst; 2131 2132 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); 2133 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); 2134 ((uint16_t *)&ports)[0] = up[1]; 2135 ((uint16_t *)&ports)[1] = up[0]; 2136 2137 connp = sctp_find_conn(&map_src, &map_dst, ports, 2138 zoneid, ipst->ips_netstack->netstack_sctp); 2139 if (connp == NULL) { 2140 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, 2141 zoneid, ports, ipha, ipst); 2142 } else { 2143 CONN_INC_REF(connp); 2144 SCTP_REFRELE(CONN2SCTP(connp)); 2145 } 2146 break; 2147 } 2148 default: 2149 { 2150 ipha_t ripha; 2151 2152 ripha.ipha_src = ipha->ipha_dst; 2153 ripha.ipha_dst = ipha->ipha_src; 2154 ripha.ipha_protocol = ipha->ipha_protocol; 2155 2156 connfp = &ipst->ips_ipcl_proto_fanout[ 2157 ipha->ipha_protocol]; 2158 mutex_enter(&connfp->connf_lock); 2159 connp = connfp->connf_head; 2160 for (connp = connfp->connf_head; connp != NULL; 2161 connp = connp->conn_next) { 2162 if (IPCL_PROTO_MATCH(connp, 2163 ipha->ipha_protocol, &ripha, ill, 2164 0, zoneid)) { 2165 CONN_INC_REF(connp); 2166 break; 2167 } 2168 } 2169 mutex_exit(&connfp->connf_lock); 2170 } 2171 } 2172 if (connp != NULL) { 2173 if (connp->conn_nexthop_set) 2174 nexthop_addr = connp->conn_nexthop_v4; 2175 CONN_DEC_REF(connp); 2176 } 2177 return (nexthop_addr); 2178 } 2179 2180 /* Table from RFC 1191 */ 2181 static int icmp_frag_size_table[] = 2182 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2183 2184 /* 2185 * Process received ICMP Packet too big. 2186 * After updating any IRE it does the fanout to any matching transport streams. 2187 * Assumes the message has been pulled up till the IP header that caused 2188 * the error. 2189 * 2190 * Returns B_FALSE on failure and B_TRUE on success. 2191 */ 2192 static boolean_t 2193 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, 2194 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length, 2195 ip_stack_t *ipst) 2196 { 2197 ire_t *ire, *first_ire; 2198 int mtu, orig_mtu; 2199 int hdr_length; 2200 ipaddr_t nexthop_addr; 2201 boolean_t disable_pmtud; 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, msg_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 2246 /* Check for MTU discovery advice as described in RFC 1191 */ 2247 mtu = ntohs(icmph->icmph_du_mtu); 2248 orig_mtu = mtu; 2249 disable_pmtud = B_FALSE; 2250 2251 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2252 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2253 ire = ire->ire_next) { 2254 /* 2255 * Look for the connection to which this ICMP message is 2256 * directed. If it has the IP_NEXTHOP option set, then the 2257 * search is limited to IREs with the MATCH_IRE_PRIVATE 2258 * option. Else the search is limited to regular IREs. 2259 */ 2260 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2261 (nexthop_addr != ire->ire_gateway_addr)) || 2262 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2263 (nexthop_addr != INADDR_ANY))) 2264 continue; 2265 2266 mutex_enter(&ire->ire_lock); 2267 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) { 2268 uint32_t length; 2269 int i; 2270 2271 /* 2272 * Use the table from RFC 1191 to figure out 2273 * the next "plateau" based on the length in 2274 * the original IP packet. 2275 */ 2276 length = ntohs(ipha->ipha_length); 2277 DTRACE_PROBE2(ip4__pmtu__guess, ire_t *, ire, 2278 uint32_t, length); 2279 if (ire->ire_max_frag <= length && 2280 ire->ire_max_frag >= length - hdr_length) { 2281 /* 2282 * Handle broken BSD 4.2 systems that 2283 * return the wrong iph_length in ICMP 2284 * errors. 2285 */ 2286 length -= hdr_length; 2287 } 2288 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2289 if (length > icmp_frag_size_table[i]) 2290 break; 2291 } 2292 if (i == A_CNT(icmp_frag_size_table)) { 2293 /* Smaller than 68! */ 2294 disable_pmtud = B_TRUE; 2295 mtu = ipst->ips_ip_pmtu_min; 2296 } else { 2297 mtu = icmp_frag_size_table[i]; 2298 if (mtu < ipst->ips_ip_pmtu_min) { 2299 mtu = ipst->ips_ip_pmtu_min; 2300 disable_pmtud = B_TRUE; 2301 } 2302 } 2303 /* Fool the ULP into believing our guessed PMTU. */ 2304 icmph->icmph_du_zero = 0; 2305 icmph->icmph_du_mtu = htons(mtu); 2306 } 2307 if (disable_pmtud) 2308 ire->ire_frag_flag = 0; 2309 /* Reduce the IRE max frag value as advised. */ 2310 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2311 if (ire->ire_max_frag == mtu) { 2312 /* Decreased it */ 2313 ire->ire_marks |= IRE_MARK_PMTU; 2314 } 2315 mutex_exit(&ire->ire_lock); 2316 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, ire_t *, 2317 ire, int, orig_mtu, int, mtu); 2318 } 2319 rw_exit(&first_ire->ire_bucket->irb_lock); 2320 ire_refrele(first_ire); 2321 return (B_TRUE); 2322 } 2323 2324 /* 2325 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2326 * calls this function. 2327 */ 2328 static mblk_t * 2329 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2330 { 2331 ipha_t *ipha; 2332 icmph_t *icmph; 2333 ipha_t *in_ipha; 2334 int length; 2335 2336 ASSERT(mp->b_datap->db_type == M_DATA); 2337 2338 /* 2339 * For Self-encapsulated packets, we added an extra IP header 2340 * without the options. Inner IP header is the one from which 2341 * the outer IP header was formed. Thus, we need to remove the 2342 * outer IP header. To do this, we pullup the whole message 2343 * and overlay whatever follows the outer IP header over the 2344 * outer IP header. 2345 */ 2346 2347 if (!pullupmsg(mp, -1)) 2348 return (NULL); 2349 2350 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2351 ipha = (ipha_t *)&icmph[1]; 2352 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2353 2354 /* 2355 * The length that we want to overlay is following the inner 2356 * IP header. Subtracting the IP header + icmp header + outer 2357 * IP header's length should give us the length that we want to 2358 * overlay. 2359 */ 2360 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2361 hdr_length; 2362 /* 2363 * Overlay whatever follows the inner header over the 2364 * outer header. 2365 */ 2366 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2367 2368 /* Set the wptr to account for the outer header */ 2369 mp->b_wptr -= hdr_length; 2370 return (mp); 2371 } 2372 2373 /* 2374 * Try to pass the ICMP message upstream in case the ULP cares. 2375 * 2376 * If the packet that caused the ICMP error is secure, we send 2377 * it to AH/ESP to make sure that the attached packet has a 2378 * valid association. ipha in the code below points to the 2379 * IP header of the packet that caused the error. 2380 * 2381 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2382 * in the context of IPsec. Normally we tell the upper layer 2383 * whenever we send the ire (including ip_bind), the IPsec header 2384 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2385 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2386 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2387 * same thing. As TCP has the IPsec options size that needs to be 2388 * adjusted, we just pass the MTU unchanged. 2389 * 2390 * IFN could have been generated locally or by some router. 2391 * 2392 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2393 * This happens because IP adjusted its value of MTU on an 2394 * earlier IFN message and could not tell the upper layer, 2395 * the new adjusted value of MTU e.g. Packet was encrypted 2396 * or there was not enough information to fanout to upper 2397 * layers. Thus on the next outbound datagram, ip_wput_ire 2398 * generates the IFN, where IPsec processing has *not* been 2399 * done. 2400 * 2401 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2402 * could have generated this. This happens because ire_max_frag 2403 * value in IP was set to a new value, while the IPsec processing 2404 * was being done and after we made the fragmentation check in 2405 * ip_wput_ire. Thus on return from IPsec processing, 2406 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2407 * and generates the IFN. As IPsec processing is over, we fanout 2408 * to AH/ESP to remove the header. 2409 * 2410 * In both these cases, ipsec_in_loopback will be set indicating 2411 * that IFN was generated locally. 2412 * 2413 * ROUTER : IFN could be secure or non-secure. 2414 * 2415 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2416 * packet in error has AH/ESP headers to validate the AH/ESP 2417 * headers. AH/ESP will verify whether there is a valid SA or 2418 * not and send it back. We will fanout again if we have more 2419 * data in the packet. 2420 * 2421 * If the packet in error does not have AH/ESP, we handle it 2422 * like any other case. 2423 * 2424 * * NON_SECURE : If the packet in error has AH/ESP headers, 2425 * we attach a dummy ipsec_in and send it up to AH/ESP 2426 * for validation. AH/ESP will verify whether there is a 2427 * valid SA or not and send it back. We will fanout again if 2428 * we have more data in the packet. 2429 * 2430 * If the packet in error does not have AH/ESP, we handle it 2431 * like any other case. 2432 */ 2433 static void 2434 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2435 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2436 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2437 zoneid_t zoneid) 2438 { 2439 uint16_t *up; /* Pointer to ports in ULP header */ 2440 uint32_t ports; /* reversed ports for fanout */ 2441 ipha_t ripha; /* With reversed addresses */ 2442 mblk_t *first_mp; 2443 ipsec_in_t *ii; 2444 tcph_t *tcph; 2445 conn_t *connp; 2446 ip_stack_t *ipst; 2447 2448 ASSERT(ill != NULL); 2449 2450 ASSERT(recv_ill != NULL); 2451 ipst = recv_ill->ill_ipst; 2452 2453 first_mp = mp; 2454 if (mctl_present) { 2455 mp = first_mp->b_cont; 2456 ASSERT(mp != NULL); 2457 2458 ii = (ipsec_in_t *)first_mp->b_rptr; 2459 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2460 } else { 2461 ii = NULL; 2462 } 2463 2464 switch (ipha->ipha_protocol) { 2465 case IPPROTO_UDP: 2466 /* 2467 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2468 * transport header. 2469 */ 2470 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2471 mp->b_wptr) { 2472 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2473 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2474 goto discard_pkt; 2475 } 2476 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2477 ipha = (ipha_t *)&icmph[1]; 2478 } 2479 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2480 2481 /* 2482 * Attempt to find a client stream based on port. 2483 * Note that we do a reverse lookup since the header is 2484 * in the form we sent it out. 2485 * The ripha header is only used for the IP_UDP_MATCH and we 2486 * only set the src and dst addresses and protocol. 2487 */ 2488 ripha.ipha_src = ipha->ipha_dst; 2489 ripha.ipha_dst = ipha->ipha_src; 2490 ripha.ipha_protocol = ipha->ipha_protocol; 2491 ((uint16_t *)&ports)[0] = up[1]; 2492 ((uint16_t *)&ports)[1] = up[0]; 2493 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2494 ntohl(ipha->ipha_src), ntohs(up[0]), 2495 ntohl(ipha->ipha_dst), ntohs(up[1]), 2496 icmph->icmph_type, icmph->icmph_code)); 2497 2498 /* Have to change db_type after any pullupmsg */ 2499 DB_TYPE(mp) = M_CTL; 2500 2501 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2502 mctl_present, ip_policy, recv_ill, zoneid); 2503 return; 2504 2505 case IPPROTO_TCP: 2506 /* 2507 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2508 * transport header. 2509 */ 2510 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2511 mp->b_wptr) { 2512 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2513 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2514 goto discard_pkt; 2515 } 2516 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2517 ipha = (ipha_t *)&icmph[1]; 2518 } 2519 /* 2520 * Find a TCP client stream for this packet. 2521 * Note that we do a reverse lookup since the header is 2522 * in the form we sent it out. 2523 */ 2524 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2525 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN, 2526 ipst); 2527 if (connp == NULL) 2528 goto discard_pkt; 2529 2530 /* Have to change db_type after any pullupmsg */ 2531 DB_TYPE(mp) = M_CTL; 2532 SQUEUE_ENTER_ONE(connp->conn_sqp, first_mp, tcp_input, connp, 2533 SQ_FILL, SQTAG_TCP_INPUT_ICMP_ERR); 2534 return; 2535 2536 case IPPROTO_SCTP: 2537 /* 2538 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2539 * transport header. 2540 */ 2541 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2542 mp->b_wptr) { 2543 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2544 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2545 goto discard_pkt; 2546 } 2547 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2548 ipha = (ipha_t *)&icmph[1]; 2549 } 2550 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2551 /* 2552 * Find a SCTP client stream for this packet. 2553 * Note that we do a reverse lookup since the header is 2554 * in the form we sent it out. 2555 * The ripha header is only used for the matching and we 2556 * only set the src and dst addresses, protocol, and version. 2557 */ 2558 ripha.ipha_src = ipha->ipha_dst; 2559 ripha.ipha_dst = ipha->ipha_src; 2560 ripha.ipha_protocol = ipha->ipha_protocol; 2561 ripha.ipha_version_and_hdr_length = 2562 ipha->ipha_version_and_hdr_length; 2563 ((uint16_t *)&ports)[0] = up[1]; 2564 ((uint16_t *)&ports)[1] = up[0]; 2565 2566 /* Have to change db_type after any pullupmsg */ 2567 DB_TYPE(mp) = M_CTL; 2568 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2569 mctl_present, ip_policy, zoneid); 2570 return; 2571 2572 case IPPROTO_ESP: 2573 case IPPROTO_AH: { 2574 int ipsec_rc; 2575 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 2576 2577 /* 2578 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2579 * We will re-use the IPSEC_IN if it is already present as 2580 * AH/ESP will not affect any fields in the IPSEC_IN for 2581 * ICMP errors. If there is no IPSEC_IN, allocate a new 2582 * one and attach it in the front. 2583 */ 2584 if (ii != NULL) { 2585 /* 2586 * ip_fanout_proto_again converts the ICMP errors 2587 * that come back from AH/ESP to M_DATA so that 2588 * if it is non-AH/ESP and we do a pullupmsg in 2589 * this function, it would work. Convert it back 2590 * to M_CTL before we send up as this is a ICMP 2591 * error. This could have been generated locally or 2592 * by some router. Validate the inner IPsec 2593 * headers. 2594 * 2595 * NOTE : ill_index is used by ip_fanout_proto_again 2596 * to locate the ill. 2597 */ 2598 ASSERT(ill != NULL); 2599 ii->ipsec_in_ill_index = 2600 ill->ill_phyint->phyint_ifindex; 2601 ii->ipsec_in_rill_index = 2602 recv_ill->ill_phyint->phyint_ifindex; 2603 DB_TYPE(first_mp->b_cont) = M_CTL; 2604 } else { 2605 /* 2606 * IPSEC_IN is not present. We attach a ipsec_in 2607 * message and send up to IPsec for validating 2608 * and removing the IPsec headers. Clear 2609 * ipsec_in_secure so that when we return 2610 * from IPsec, we don't mistakenly think that this 2611 * is a secure packet came from the network. 2612 * 2613 * NOTE : ill_index is used by ip_fanout_proto_again 2614 * to locate the ill. 2615 */ 2616 ASSERT(first_mp == mp); 2617 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 2618 if (first_mp == NULL) { 2619 freemsg(mp); 2620 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2621 return; 2622 } 2623 ii = (ipsec_in_t *)first_mp->b_rptr; 2624 2625 /* This is not a secure packet */ 2626 ii->ipsec_in_secure = B_FALSE; 2627 first_mp->b_cont = mp; 2628 DB_TYPE(mp) = M_CTL; 2629 ASSERT(ill != NULL); 2630 ii->ipsec_in_ill_index = 2631 ill->ill_phyint->phyint_ifindex; 2632 ii->ipsec_in_rill_index = 2633 recv_ill->ill_phyint->phyint_ifindex; 2634 } 2635 ip2dbg(("icmp_inbound_error: ipsec\n")); 2636 2637 if (!ipsec_loaded(ipss)) { 2638 ip_proto_not_sup(q, first_mp, 0, zoneid, ipst); 2639 return; 2640 } 2641 2642 if (ipha->ipha_protocol == IPPROTO_ESP) 2643 ipsec_rc = ipsecesp_icmp_error(first_mp); 2644 else 2645 ipsec_rc = ipsecah_icmp_error(first_mp); 2646 if (ipsec_rc == IPSEC_STATUS_FAILED) 2647 return; 2648 2649 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2650 return; 2651 } 2652 default: 2653 /* 2654 * The ripha header is only used for the lookup and we 2655 * only set the src and dst addresses and protocol. 2656 */ 2657 ripha.ipha_src = ipha->ipha_dst; 2658 ripha.ipha_dst = ipha->ipha_src; 2659 ripha.ipha_protocol = ipha->ipha_protocol; 2660 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2661 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2662 ntohl(ipha->ipha_dst), 2663 icmph->icmph_type, icmph->icmph_code)); 2664 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2665 ipha_t *in_ipha; 2666 2667 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2668 mp->b_wptr) { 2669 if (!pullupmsg(mp, (uchar_t *)ipha + 2670 hdr_length + sizeof (ipha_t) - 2671 mp->b_rptr)) { 2672 goto discard_pkt; 2673 } 2674 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2675 ipha = (ipha_t *)&icmph[1]; 2676 } 2677 /* 2678 * Caller has verified that length has to be 2679 * at least the size of IP header. 2680 */ 2681 ASSERT(hdr_length >= sizeof (ipha_t)); 2682 /* 2683 * Check the sanity of the inner IP header like 2684 * we did for the outer header. 2685 */ 2686 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2687 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2688 goto discard_pkt; 2689 } 2690 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2691 goto discard_pkt; 2692 } 2693 /* Check for Self-encapsulated tunnels */ 2694 if (in_ipha->ipha_src == ipha->ipha_src && 2695 in_ipha->ipha_dst == ipha->ipha_dst) { 2696 2697 mp = icmp_inbound_self_encap_error(mp, 2698 iph_hdr_length, hdr_length); 2699 if (mp == NULL) 2700 goto discard_pkt; 2701 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2702 ipha = (ipha_t *)&icmph[1]; 2703 hdr_length = IPH_HDR_LENGTH(ipha); 2704 /* 2705 * The packet in error is self-encapsualted. 2706 * And we are finding it further encapsulated 2707 * which we could not have possibly generated. 2708 */ 2709 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2710 goto discard_pkt; 2711 } 2712 icmp_inbound_error_fanout(q, ill, first_mp, 2713 icmph, ipha, iph_hdr_length, hdr_length, 2714 mctl_present, ip_policy, recv_ill, zoneid); 2715 return; 2716 } 2717 } 2718 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2719 ipha->ipha_protocol == IPPROTO_IPV6) && 2720 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2721 ii != NULL && 2722 ii->ipsec_in_loopback && 2723 ii->ipsec_in_secure) { 2724 /* 2725 * For IP tunnels that get a looped-back 2726 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2727 * reported new MTU to take into account the IPsec 2728 * headers protecting this configured tunnel. 2729 * 2730 * This allows the tunnel module (tun.c) to blindly 2731 * accept the MTU reported in an ICMP "too big" 2732 * message. 2733 * 2734 * Non-looped back ICMP messages will just be 2735 * handled by the security protocols (if needed), 2736 * and the first subsequent packet will hit this 2737 * path. 2738 */ 2739 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2740 ipsec_in_extra_length(first_mp)); 2741 } 2742 /* Have to change db_type after any pullupmsg */ 2743 DB_TYPE(mp) = M_CTL; 2744 2745 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2746 ip_policy, recv_ill, zoneid); 2747 return; 2748 } 2749 /* NOTREACHED */ 2750 discard_pkt: 2751 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2752 drop_pkt:; 2753 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2754 freemsg(first_mp); 2755 } 2756 2757 /* 2758 * Common IP options parser. 2759 * 2760 * Setup routine: fill in *optp with options-parsing state, then 2761 * tail-call ipoptp_next to return the first option. 2762 */ 2763 uint8_t 2764 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2765 { 2766 uint32_t totallen; /* total length of all options */ 2767 2768 totallen = ipha->ipha_version_and_hdr_length - 2769 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2770 totallen <<= 2; 2771 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2772 optp->ipoptp_end = optp->ipoptp_next + totallen; 2773 optp->ipoptp_flags = 0; 2774 return (ipoptp_next(optp)); 2775 } 2776 2777 /* 2778 * Common IP options parser: extract next option. 2779 */ 2780 uint8_t 2781 ipoptp_next(ipoptp_t *optp) 2782 { 2783 uint8_t *end = optp->ipoptp_end; 2784 uint8_t *cur = optp->ipoptp_next; 2785 uint8_t opt, len, pointer; 2786 2787 /* 2788 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2789 * has been corrupted. 2790 */ 2791 ASSERT(cur <= end); 2792 2793 if (cur == end) 2794 return (IPOPT_EOL); 2795 2796 opt = cur[IPOPT_OPTVAL]; 2797 2798 /* 2799 * Skip any NOP options. 2800 */ 2801 while (opt == IPOPT_NOP) { 2802 cur++; 2803 if (cur == end) 2804 return (IPOPT_EOL); 2805 opt = cur[IPOPT_OPTVAL]; 2806 } 2807 2808 if (opt == IPOPT_EOL) 2809 return (IPOPT_EOL); 2810 2811 /* 2812 * Option requiring a length. 2813 */ 2814 if ((cur + 1) >= end) { 2815 optp->ipoptp_flags |= IPOPTP_ERROR; 2816 return (IPOPT_EOL); 2817 } 2818 len = cur[IPOPT_OLEN]; 2819 if (len < 2) { 2820 optp->ipoptp_flags |= IPOPTP_ERROR; 2821 return (IPOPT_EOL); 2822 } 2823 optp->ipoptp_cur = cur; 2824 optp->ipoptp_len = len; 2825 optp->ipoptp_next = cur + len; 2826 if (cur + len > end) { 2827 optp->ipoptp_flags |= IPOPTP_ERROR; 2828 return (IPOPT_EOL); 2829 } 2830 2831 /* 2832 * For the options which require a pointer field, make sure 2833 * its there, and make sure it points to either something 2834 * inside this option, or the end of the option. 2835 */ 2836 switch (opt) { 2837 case IPOPT_RR: 2838 case IPOPT_TS: 2839 case IPOPT_LSRR: 2840 case IPOPT_SSRR: 2841 if (len <= IPOPT_OFFSET) { 2842 optp->ipoptp_flags |= IPOPTP_ERROR; 2843 return (opt); 2844 } 2845 pointer = cur[IPOPT_OFFSET]; 2846 if (pointer - 1 > len) { 2847 optp->ipoptp_flags |= IPOPTP_ERROR; 2848 return (opt); 2849 } 2850 break; 2851 } 2852 2853 /* 2854 * Sanity check the pointer field based on the type of the 2855 * option. 2856 */ 2857 switch (opt) { 2858 case IPOPT_RR: 2859 case IPOPT_SSRR: 2860 case IPOPT_LSRR: 2861 if (pointer < IPOPT_MINOFF_SR) 2862 optp->ipoptp_flags |= IPOPTP_ERROR; 2863 break; 2864 case IPOPT_TS: 2865 if (pointer < IPOPT_MINOFF_IT) 2866 optp->ipoptp_flags |= IPOPTP_ERROR; 2867 /* 2868 * Note that the Internet Timestamp option also 2869 * contains two four bit fields (the Overflow field, 2870 * and the Flag field), which follow the pointer 2871 * field. We don't need to check that these fields 2872 * fall within the length of the option because this 2873 * was implicitely done above. We've checked that the 2874 * pointer value is at least IPOPT_MINOFF_IT, and that 2875 * it falls within the option. Since IPOPT_MINOFF_IT > 2876 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2877 */ 2878 ASSERT(len > IPOPT_POS_OV_FLG); 2879 break; 2880 } 2881 2882 return (opt); 2883 } 2884 2885 /* 2886 * Use the outgoing IP header to create an IP_OPTIONS option the way 2887 * it was passed down from the application. 2888 */ 2889 int 2890 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) 2891 { 2892 ipoptp_t opts; 2893 const uchar_t *opt; 2894 uint8_t optval; 2895 uint8_t optlen; 2896 uint32_t len = 0; 2897 uchar_t *buf1 = buf; 2898 2899 buf += IP_ADDR_LEN; /* Leave room for final destination */ 2900 len += IP_ADDR_LEN; 2901 bzero(buf1, IP_ADDR_LEN); 2902 2903 /* 2904 * OK to cast away const here, as we don't store through the returned 2905 * opts.ipoptp_cur pointer. 2906 */ 2907 for (optval = ipoptp_first(&opts, (ipha_t *)ipha); 2908 optval != IPOPT_EOL; 2909 optval = ipoptp_next(&opts)) { 2910 int off; 2911 2912 opt = opts.ipoptp_cur; 2913 optlen = opts.ipoptp_len; 2914 switch (optval) { 2915 case IPOPT_SSRR: 2916 case IPOPT_LSRR: 2917 2918 /* 2919 * Insert ipha_dst as the first entry in the source 2920 * route and move down the entries on step. 2921 * The last entry gets placed at buf1. 2922 */ 2923 buf[IPOPT_OPTVAL] = optval; 2924 buf[IPOPT_OLEN] = optlen; 2925 buf[IPOPT_OFFSET] = optlen; 2926 2927 off = optlen - IP_ADDR_LEN; 2928 if (off < 0) { 2929 /* No entries in source route */ 2930 break; 2931 } 2932 /* Last entry in source route */ 2933 bcopy(opt + off, buf1, IP_ADDR_LEN); 2934 off -= IP_ADDR_LEN; 2935 2936 while (off > 0) { 2937 bcopy(opt + off, 2938 buf + off + IP_ADDR_LEN, 2939 IP_ADDR_LEN); 2940 off -= IP_ADDR_LEN; 2941 } 2942 /* ipha_dst into first slot */ 2943 bcopy(&ipha->ipha_dst, 2944 buf + off + IP_ADDR_LEN, 2945 IP_ADDR_LEN); 2946 buf += optlen; 2947 len += optlen; 2948 break; 2949 2950 case IPOPT_COMSEC: 2951 case IPOPT_SECURITY: 2952 /* if passing up a label is not ok, then remove */ 2953 if (is_system_labeled()) 2954 break; 2955 /* FALLTHROUGH */ 2956 default: 2957 bcopy(opt, buf, optlen); 2958 buf += optlen; 2959 len += optlen; 2960 break; 2961 } 2962 } 2963 done: 2964 /* Pad the resulting options */ 2965 while (len & 0x3) { 2966 *buf++ = IPOPT_EOL; 2967 len++; 2968 } 2969 return (len); 2970 } 2971 2972 /* 2973 * Update any record route or timestamp options to include this host. 2974 * Reverse any source route option. 2975 * This routine assumes that the options are well formed i.e. that they 2976 * have already been checked. 2977 */ 2978 static void 2979 icmp_options_update(ipha_t *ipha) 2980 { 2981 ipoptp_t opts; 2982 uchar_t *opt; 2983 uint8_t optval; 2984 ipaddr_t src; /* Our local address */ 2985 ipaddr_t dst; 2986 2987 ip2dbg(("icmp_options_update\n")); 2988 src = ipha->ipha_src; 2989 dst = ipha->ipha_dst; 2990 2991 for (optval = ipoptp_first(&opts, ipha); 2992 optval != IPOPT_EOL; 2993 optval = ipoptp_next(&opts)) { 2994 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 2995 opt = opts.ipoptp_cur; 2996 ip2dbg(("icmp_options_update: opt %d, len %d\n", 2997 optval, opts.ipoptp_len)); 2998 switch (optval) { 2999 int off1, off2; 3000 case IPOPT_SSRR: 3001 case IPOPT_LSRR: 3002 /* 3003 * Reverse the source route. The first entry 3004 * should be the next to last one in the current 3005 * source route (the last entry is our address). 3006 * The last entry should be the final destination. 3007 */ 3008 off1 = IPOPT_MINOFF_SR - 1; 3009 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 3010 if (off2 < 0) { 3011 /* No entries in source route */ 3012 ip1dbg(( 3013 "icmp_options_update: bad src route\n")); 3014 break; 3015 } 3016 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 3017 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 3018 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 3019 off2 -= IP_ADDR_LEN; 3020 3021 while (off1 < off2) { 3022 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 3023 bcopy((char *)opt + off2, (char *)opt + off1, 3024 IP_ADDR_LEN); 3025 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 3026 off1 += IP_ADDR_LEN; 3027 off2 -= IP_ADDR_LEN; 3028 } 3029 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 3030 break; 3031 } 3032 } 3033 } 3034 3035 /* 3036 * Process received ICMP Redirect messages. 3037 */ 3038 static void 3039 icmp_redirect(ill_t *ill, mblk_t *mp) 3040 { 3041 ipha_t *ipha; 3042 int iph_hdr_length; 3043 icmph_t *icmph; 3044 ipha_t *ipha_err; 3045 ire_t *ire; 3046 ire_t *prev_ire; 3047 ire_t *save_ire; 3048 ipaddr_t src, dst, gateway; 3049 iulp_t ulp_info = { 0 }; 3050 int error; 3051 ip_stack_t *ipst; 3052 3053 ASSERT(ill != NULL); 3054 ipst = ill->ill_ipst; 3055 3056 ipha = (ipha_t *)mp->b_rptr; 3057 iph_hdr_length = IPH_HDR_LENGTH(ipha); 3058 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 3059 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 3060 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3061 freemsg(mp); 3062 return; 3063 } 3064 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 3065 ipha_err = (ipha_t *)&icmph[1]; 3066 src = ipha->ipha_src; 3067 dst = ipha_err->ipha_dst; 3068 gateway = icmph->icmph_rd_gateway; 3069 /* Make sure the new gateway is reachable somehow. */ 3070 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 3071 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3072 /* 3073 * Make sure we had a route for the dest in question and that 3074 * that route was pointing to the old gateway (the source of the 3075 * redirect packet.) 3076 */ 3077 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 3078 NULL, MATCH_IRE_GW, ipst); 3079 /* 3080 * Check that 3081 * the redirect was not from ourselves 3082 * the new gateway and the old gateway are directly reachable 3083 */ 3084 if (!prev_ire || 3085 !ire || 3086 ire->ire_type == IRE_LOCAL) { 3087 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3088 freemsg(mp); 3089 if (ire != NULL) 3090 ire_refrele(ire); 3091 if (prev_ire != NULL) 3092 ire_refrele(prev_ire); 3093 return; 3094 } 3095 3096 /* 3097 * Should we use the old ULP info to create the new gateway? From 3098 * a user's perspective, we should inherit the info so that it 3099 * is a "smooth" transition. If we do not do that, then new 3100 * connections going thru the new gateway will have no route metrics, 3101 * which is counter-intuitive to user. From a network point of 3102 * view, this may or may not make sense even though the new gateway 3103 * is still directly connected to us so the route metrics should not 3104 * change much. 3105 * 3106 * But if the old ire_uinfo is not initialized, we do another 3107 * recursive lookup on the dest using the new gateway. There may 3108 * be a route to that. If so, use it to initialize the redirect 3109 * route. 3110 */ 3111 if (prev_ire->ire_uinfo.iulp_set) { 3112 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3113 } else { 3114 ire_t *tmp_ire; 3115 ire_t *sire; 3116 3117 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 3118 ALL_ZONES, 0, NULL, 3119 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT), 3120 ipst); 3121 if (sire != NULL) { 3122 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3123 /* 3124 * If sire != NULL, ire_ftable_lookup() should not 3125 * return a NULL value. 3126 */ 3127 ASSERT(tmp_ire != NULL); 3128 ire_refrele(tmp_ire); 3129 ire_refrele(sire); 3130 } else if (tmp_ire != NULL) { 3131 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 3132 sizeof (iulp_t)); 3133 ire_refrele(tmp_ire); 3134 } 3135 } 3136 if (prev_ire->ire_type == IRE_CACHE) 3137 ire_delete(prev_ire); 3138 ire_refrele(prev_ire); 3139 /* 3140 * TODO: more precise handling for cases 0, 2, 3, the latter two 3141 * require TOS routing 3142 */ 3143 switch (icmph->icmph_code) { 3144 case 0: 3145 case 1: 3146 /* TODO: TOS specificity for cases 2 and 3 */ 3147 case 2: 3148 case 3: 3149 break; 3150 default: 3151 freemsg(mp); 3152 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 3153 ire_refrele(ire); 3154 return; 3155 } 3156 /* 3157 * Create a Route Association. This will allow us to remember that 3158 * someone we believe told us to use the particular gateway. 3159 */ 3160 save_ire = ire; 3161 ire = ire_create( 3162 (uchar_t *)&dst, /* dest addr */ 3163 (uchar_t *)&ip_g_all_ones, /* mask */ 3164 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 3165 (uchar_t *)&gateway, /* gateway addr */ 3166 &save_ire->ire_max_frag, /* max frag */ 3167 NULL, /* no src nce */ 3168 NULL, /* no rfq */ 3169 NULL, /* no stq */ 3170 IRE_HOST, 3171 NULL, /* ipif */ 3172 0, /* cmask */ 3173 0, /* phandle */ 3174 0, /* ihandle */ 3175 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 3176 &ulp_info, 3177 NULL, /* tsol_gc_t */ 3178 NULL, /* gcgrp */ 3179 ipst); 3180 3181 if (ire == NULL) { 3182 freemsg(mp); 3183 ire_refrele(save_ire); 3184 return; 3185 } 3186 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); 3187 ire_refrele(save_ire); 3188 atomic_inc_32(&ipst->ips_ip_redirect_cnt); 3189 3190 if (error == 0) { 3191 ire_refrele(ire); /* Held in ire_add_v4 */ 3192 /* tell routing sockets that we received a redirect */ 3193 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 3194 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 3195 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst); 3196 } 3197 3198 /* 3199 * Delete any existing IRE_HOST type redirect ires for this destination. 3200 * This together with the added IRE has the effect of 3201 * modifying an existing redirect. 3202 */ 3203 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL, 3204 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst); 3205 if (prev_ire != NULL) { 3206 if (prev_ire ->ire_flags & RTF_DYNAMIC) 3207 ire_delete(prev_ire); 3208 ire_refrele(prev_ire); 3209 } 3210 3211 freemsg(mp); 3212 } 3213 3214 /* 3215 * Generate an ICMP parameter problem message. 3216 */ 3217 static void 3218 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid, 3219 ip_stack_t *ipst) 3220 { 3221 icmph_t icmph; 3222 boolean_t mctl_present; 3223 mblk_t *first_mp; 3224 3225 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3226 3227 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3228 if (mctl_present) 3229 freeb(first_mp); 3230 return; 3231 } 3232 3233 bzero(&icmph, sizeof (icmph_t)); 3234 icmph.icmph_type = ICMP_PARAM_PROBLEM; 3235 icmph.icmph_pp_ptr = ptr; 3236 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs); 3237 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3238 ipst); 3239 } 3240 3241 /* 3242 * Build and ship an IPv4 ICMP message using the packet data in mp, and 3243 * the ICMP header pointed to by "stuff". (May be called as writer.) 3244 * Note: assumes that icmp_pkt_err_ok has been called to verify that 3245 * an icmp error packet can be sent. 3246 * Assigns an appropriate source address to the packet. If ipha_dst is 3247 * one of our addresses use it for source. Otherwise pick a source based 3248 * on a route lookup back to ipha_src. 3249 * Note that ipha_src must be set here since the 3250 * packet is likely to arrive on an ill queue in ip_wput() which will 3251 * not set a source address. 3252 */ 3253 static void 3254 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, 3255 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 3256 { 3257 ipaddr_t dst; 3258 icmph_t *icmph; 3259 ipha_t *ipha; 3260 uint_t len_needed; 3261 size_t msg_len; 3262 mblk_t *mp1; 3263 ipaddr_t src; 3264 ire_t *ire; 3265 mblk_t *ipsec_mp; 3266 ipsec_out_t *io = NULL; 3267 3268 if (mctl_present) { 3269 /* 3270 * If it is : 3271 * 3272 * 1) a IPSEC_OUT, then this is caused by outbound 3273 * datagram originating on this host. IPsec processing 3274 * may or may not have been done. Refer to comments above 3275 * icmp_inbound_error_fanout for details. 3276 * 3277 * 2) a IPSEC_IN if we are generating a icmp_message 3278 * for an incoming datagram destined for us i.e called 3279 * from ip_fanout_send_icmp. 3280 */ 3281 ipsec_info_t *in; 3282 ipsec_mp = mp; 3283 mp = ipsec_mp->b_cont; 3284 3285 in = (ipsec_info_t *)ipsec_mp->b_rptr; 3286 ipha = (ipha_t *)mp->b_rptr; 3287 3288 ASSERT(in->ipsec_info_type == IPSEC_OUT || 3289 in->ipsec_info_type == IPSEC_IN); 3290 3291 if (in->ipsec_info_type == IPSEC_IN) { 3292 /* 3293 * Convert the IPSEC_IN to IPSEC_OUT. 3294 */ 3295 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3296 BUMP_MIB(&ipst->ips_ip_mib, 3297 ipIfStatsOutDiscards); 3298 return; 3299 } 3300 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3301 } else { 3302 ASSERT(in->ipsec_info_type == IPSEC_OUT); 3303 io = (ipsec_out_t *)in; 3304 /* 3305 * Clear out ipsec_out_proc_begin, so we do a fresh 3306 * ire lookup. 3307 */ 3308 io->ipsec_out_proc_begin = B_FALSE; 3309 } 3310 ASSERT(zoneid != ALL_ZONES); 3311 /* 3312 * The IPSEC_IN (now an IPSEC_OUT) didn't have its zoneid 3313 * initialized. We need to do that now. 3314 */ 3315 io->ipsec_out_zoneid = zoneid; 3316 } else { 3317 /* 3318 * This is in clear. The icmp message we are building 3319 * here should go out in clear. 3320 * 3321 * Pardon the convolution of it all, but it's easier to 3322 * allocate a "use cleartext" IPSEC_IN message and convert 3323 * it than it is to allocate a new one. 3324 */ 3325 ipsec_in_t *ii; 3326 ASSERT(DB_TYPE(mp) == M_DATA); 3327 ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); 3328 if (ipsec_mp == NULL) { 3329 freemsg(mp); 3330 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3331 return; 3332 } 3333 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3334 3335 /* This is not a secure packet */ 3336 ii->ipsec_in_secure = B_FALSE; 3337 /* 3338 * For trusted extensions using a shared IP address we can 3339 * send using any zoneid. 3340 */ 3341 if (zoneid == ALL_ZONES) 3342 ii->ipsec_in_zoneid = GLOBAL_ZONEID; 3343 else 3344 ii->ipsec_in_zoneid = zoneid; 3345 ipsec_mp->b_cont = mp; 3346 ipha = (ipha_t *)mp->b_rptr; 3347 /* 3348 * Convert the IPSEC_IN to IPSEC_OUT. 3349 */ 3350 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3351 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3352 return; 3353 } 3354 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3355 } 3356 3357 /* Remember our eventual destination */ 3358 dst = ipha->ipha_src; 3359 3360 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 3361 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst); 3362 if (ire != NULL && 3363 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) { 3364 src = ipha->ipha_dst; 3365 } else { 3366 if (ire != NULL) 3367 ire_refrele(ire); 3368 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL, 3369 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY), 3370 ipst); 3371 if (ire == NULL) { 3372 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 3373 freemsg(ipsec_mp); 3374 return; 3375 } 3376 src = ire->ire_src_addr; 3377 } 3378 3379 if (ire != NULL) 3380 ire_refrele(ire); 3381 3382 /* 3383 * Check if we can send back more then 8 bytes in addition to 3384 * the IP header. We try to send 64 bytes of data and the internal 3385 * header in the special cases of ipv4 encapsulated ipv4 or ipv6. 3386 */ 3387 len_needed = IPH_HDR_LENGTH(ipha); 3388 if (ipha->ipha_protocol == IPPROTO_ENCAP || 3389 ipha->ipha_protocol == IPPROTO_IPV6) { 3390 3391 if (!pullupmsg(mp, -1)) { 3392 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 3393 freemsg(ipsec_mp); 3394 return; 3395 } 3396 ipha = (ipha_t *)mp->b_rptr; 3397 3398 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 3399 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + 3400 len_needed)); 3401 } else { 3402 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed); 3403 3404 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6); 3405 len_needed += ip_hdr_length_v6(mp, ip6h); 3406 } 3407 } 3408 len_needed += ipst->ips_ip_icmp_return; 3409 msg_len = msgdsize(mp); 3410 if (msg_len > len_needed) { 3411 (void) adjmsg(mp, len_needed - msg_len); 3412 msg_len = len_needed; 3413 } 3414 /* Make sure we propagate the cred/label for TX */ 3415 mp1 = allocb_tmpl(sizeof (icmp_ipha) + len, mp); 3416 if (mp1 == NULL) { 3417 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors); 3418 freemsg(ipsec_mp); 3419 return; 3420 } 3421 mp1->b_cont = mp; 3422 mp = mp1; 3423 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 3424 ipsec_mp->b_rptr == (uint8_t *)io && 3425 io->ipsec_out_type == IPSEC_OUT); 3426 ipsec_mp->b_cont = mp; 3427 3428 /* 3429 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 3430 * node generates be accepted in peace by all on-host destinations. 3431 * If we do NOT assume that all on-host destinations trust 3432 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 3433 * (Look for ipsec_out_icmp_loopback). 3434 */ 3435 io->ipsec_out_icmp_loopback = B_TRUE; 3436 3437 ipha = (ipha_t *)mp->b_rptr; 3438 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 3439 *ipha = icmp_ipha; 3440 ipha->ipha_src = src; 3441 ipha->ipha_dst = dst; 3442 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 3443 msg_len += sizeof (icmp_ipha) + len; 3444 if (msg_len > IP_MAXPACKET) { 3445 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 3446 msg_len = IP_MAXPACKET; 3447 } 3448 ipha->ipha_length = htons((uint16_t)msg_len); 3449 icmph = (icmph_t *)&ipha[1]; 3450 bcopy(stuff, icmph, len); 3451 icmph->icmph_checksum = 0; 3452 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 3453 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 3454 put(q, ipsec_mp); 3455 } 3456 3457 /* 3458 * Determine if an ICMP error packet can be sent given the rate limit. 3459 * The limit consists of an average frequency (icmp_pkt_err_interval measured 3460 * in milliseconds) and a burst size. Burst size number of packets can 3461 * be sent arbitrarely closely spaced. 3462 * The state is tracked using two variables to implement an approximate 3463 * token bucket filter: 3464 * icmp_pkt_err_last - lbolt value when the last burst started 3465 * icmp_pkt_err_sent - number of packets sent in current burst 3466 */ 3467 boolean_t 3468 icmp_err_rate_limit(ip_stack_t *ipst) 3469 { 3470 clock_t now = TICK_TO_MSEC(lbolt); 3471 uint_t refilled; /* Number of packets refilled in tbf since last */ 3472 /* Guard against changes by loading into local variable */ 3473 uint_t err_interval = ipst->ips_ip_icmp_err_interval; 3474 3475 if (err_interval == 0) 3476 return (B_FALSE); 3477 3478 if (ipst->ips_icmp_pkt_err_last > now) { 3479 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 3480 ipst->ips_icmp_pkt_err_last = 0; 3481 ipst->ips_icmp_pkt_err_sent = 0; 3482 } 3483 /* 3484 * If we are in a burst update the token bucket filter. 3485 * Update the "last" time to be close to "now" but make sure 3486 * we don't loose precision. 3487 */ 3488 if (ipst->ips_icmp_pkt_err_sent != 0) { 3489 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval; 3490 if (refilled > ipst->ips_icmp_pkt_err_sent) { 3491 ipst->ips_icmp_pkt_err_sent = 0; 3492 } else { 3493 ipst->ips_icmp_pkt_err_sent -= refilled; 3494 ipst->ips_icmp_pkt_err_last += refilled * err_interval; 3495 } 3496 } 3497 if (ipst->ips_icmp_pkt_err_sent == 0) { 3498 /* Start of new burst */ 3499 ipst->ips_icmp_pkt_err_last = now; 3500 } 3501 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) { 3502 ipst->ips_icmp_pkt_err_sent++; 3503 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 3504 ipst->ips_icmp_pkt_err_sent)); 3505 return (B_FALSE); 3506 } 3507 ip1dbg(("icmp_err_rate_limit: dropped\n")); 3508 return (B_TRUE); 3509 } 3510 3511 /* 3512 * Check if it is ok to send an IPv4 ICMP error packet in 3513 * response to the IPv4 packet in mp. 3514 * Free the message and return null if no 3515 * ICMP error packet should be sent. 3516 */ 3517 static mblk_t * 3518 icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst) 3519 { 3520 icmph_t *icmph; 3521 ipha_t *ipha; 3522 uint_t len_needed; 3523 ire_t *src_ire; 3524 ire_t *dst_ire; 3525 3526 if (!mp) 3527 return (NULL); 3528 ipha = (ipha_t *)mp->b_rptr; 3529 if (ip_csum_hdr(ipha)) { 3530 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); 3531 freemsg(mp); 3532 return (NULL); 3533 } 3534 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, 3535 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3536 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, 3537 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 3538 if (src_ire != NULL || dst_ire != NULL || 3539 CLASSD(ipha->ipha_dst) || 3540 CLASSD(ipha->ipha_src) || 3541 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3542 /* Note: only errors to the fragment with offset 0 */ 3543 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3544 freemsg(mp); 3545 if (src_ire != NULL) 3546 ire_refrele(src_ire); 3547 if (dst_ire != NULL) 3548 ire_refrele(dst_ire); 3549 return (NULL); 3550 } 3551 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3552 /* 3553 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3554 * errors in response to any ICMP errors. 3555 */ 3556 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3557 if (mp->b_wptr - mp->b_rptr < len_needed) { 3558 if (!pullupmsg(mp, len_needed)) { 3559 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3560 freemsg(mp); 3561 return (NULL); 3562 } 3563 ipha = (ipha_t *)mp->b_rptr; 3564 } 3565 icmph = (icmph_t *) 3566 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3567 switch (icmph->icmph_type) { 3568 case ICMP_DEST_UNREACHABLE: 3569 case ICMP_SOURCE_QUENCH: 3570 case ICMP_TIME_EXCEEDED: 3571 case ICMP_PARAM_PROBLEM: 3572 case ICMP_REDIRECT: 3573 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3574 freemsg(mp); 3575 return (NULL); 3576 default: 3577 break; 3578 } 3579 } 3580 /* 3581 * If this is a labeled system, then check to see if we're allowed to 3582 * send a response to this particular sender. If not, then just drop. 3583 */ 3584 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 3585 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); 3586 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3587 freemsg(mp); 3588 return (NULL); 3589 } 3590 if (icmp_err_rate_limit(ipst)) { 3591 /* 3592 * Only send ICMP error packets every so often. 3593 * This should be done on a per port/source basis, 3594 * but for now this will suffice. 3595 */ 3596 freemsg(mp); 3597 return (NULL); 3598 } 3599 return (mp); 3600 } 3601 3602 /* 3603 * Generate an ICMP redirect message. 3604 */ 3605 static void 3606 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst) 3607 { 3608 icmph_t icmph; 3609 3610 /* 3611 * We are called from ip_rput where we could 3612 * not have attached an IPSEC_IN. 3613 */ 3614 ASSERT(mp->b_datap->db_type == M_DATA); 3615 3616 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3617 return; 3618 } 3619 3620 bzero(&icmph, sizeof (icmph_t)); 3621 icmph.icmph_type = ICMP_REDIRECT; 3622 icmph.icmph_code = 1; 3623 icmph.icmph_rd_gateway = gateway; 3624 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects); 3625 /* Redirects sent by router, and router is global zone */ 3626 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst); 3627 } 3628 3629 /* 3630 * Generate an ICMP time exceeded message. 3631 */ 3632 void 3633 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3634 ip_stack_t *ipst) 3635 { 3636 icmph_t icmph; 3637 boolean_t mctl_present; 3638 mblk_t *first_mp; 3639 3640 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3641 3642 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3643 if (mctl_present) 3644 freeb(first_mp); 3645 return; 3646 } 3647 3648 bzero(&icmph, sizeof (icmph_t)); 3649 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3650 icmph.icmph_code = code; 3651 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds); 3652 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, 3653 ipst); 3654 } 3655 3656 /* 3657 * Generate an ICMP unreachable message. 3658 */ 3659 void 3660 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, 3661 ip_stack_t *ipst) 3662 { 3663 icmph_t icmph; 3664 mblk_t *first_mp; 3665 boolean_t mctl_present; 3666 3667 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3668 3669 if (!(mp = icmp_pkt_err_ok(mp, ipst))) { 3670 if (mctl_present) 3671 freeb(first_mp); 3672 return; 3673 } 3674 3675 bzero(&icmph, sizeof (icmph_t)); 3676 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3677 icmph.icmph_code = code; 3678 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 3679 ip2dbg(("send icmp destination unreachable code %d\n", code)); 3680 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present, 3681 zoneid, ipst); 3682 } 3683 3684 /* 3685 * Attempt to start recovery of an IPv4 interface that's been shut down as a 3686 * duplicate. As long as someone else holds the address, the interface will 3687 * stay down. When that conflict goes away, the interface is brought back up. 3688 * This is done so that accidental shutdowns of addresses aren't made 3689 * permanent. Your server will recover from a failure. 3690 * 3691 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a 3692 * user space process (dhcpagent). 3693 * 3694 * Recovery completes if ARP reports that the address is now ours (via 3695 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation. 3696 * 3697 * This function is entered on a timer expiry; the ID is in ipif_recovery_id. 3698 */ 3699 static void 3700 ipif_dup_recovery(void *arg) 3701 { 3702 ipif_t *ipif = arg; 3703 ill_t *ill = ipif->ipif_ill; 3704 mblk_t *arp_add_mp; 3705 mblk_t *arp_del_mp; 3706 ip_stack_t *ipst = ill->ill_ipst; 3707 3708 ipif->ipif_recovery_id = 0; 3709 3710 /* 3711 * No lock needed for moving or condemned check, as this is just an 3712 * optimization. 3713 */ 3714 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || 3715 (ipif->ipif_flags & IPIF_POINTOPOINT) || 3716 (ipif->ipif_state_flags & (IPIF_CONDEMNED))) { 3717 /* No reason to try to bring this address back. */ 3718 return; 3719 } 3720 3721 /* ACE_F_UNVERIFIED restarts DAD */ 3722 if ((arp_add_mp = ipif_area_alloc(ipif, ACE_F_UNVERIFIED)) == NULL) 3723 goto alloc_fail; 3724 3725 if (ipif->ipif_arp_del_mp == NULL) { 3726 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) 3727 goto alloc_fail; 3728 ipif->ipif_arp_del_mp = arp_del_mp; 3729 } 3730 3731 putnext(ill->ill_rq, arp_add_mp); 3732 return; 3733 3734 alloc_fail: 3735 /* 3736 * On allocation failure, just restart the timer. Note that the ipif 3737 * is down here, so no other thread could be trying to start a recovery 3738 * timer. The ill_lock protects the condemned flag and the recovery 3739 * timer ID. 3740 */ 3741 freemsg(arp_add_mp); 3742 mutex_enter(&ill->ill_lock); 3743 if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 && 3744 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 3745 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, 3746 MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3747 } 3748 mutex_exit(&ill->ill_lock); 3749 } 3750 3751 /* 3752 * This is for exclusive changes due to ARP. Either tear down an interface due 3753 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. 3754 */ 3755 /* ARGSUSED */ 3756 static void 3757 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3758 { 3759 ill_t *ill = rq->q_ptr; 3760 arh_t *arh; 3761 ipaddr_t src; 3762 ipif_t *ipif; 3763 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ 3764 char hbuf[MAC_STR_LEN]; 3765 char sbuf[INET_ADDRSTRLEN]; 3766 const char *failtype; 3767 boolean_t bring_up; 3768 ip_stack_t *ipst = ill->ill_ipst; 3769 3770 switch (((arcn_t *)mp->b_rptr)->arcn_code) { 3771 case AR_CN_READY: 3772 failtype = NULL; 3773 bring_up = B_TRUE; 3774 break; 3775 case AR_CN_FAILED: 3776 failtype = "in use"; 3777 bring_up = B_FALSE; 3778 break; 3779 default: 3780 failtype = "claimed"; 3781 bring_up = B_FALSE; 3782 break; 3783 } 3784 3785 arh = (arh_t *)mp->b_cont->b_rptr; 3786 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 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 ASSERT(ill->ill_move_ipif == NULL); 3843 } 3844 continue; 3845 } 3846 3847 mutex_enter(&ill->ill_lock); 3848 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 3849 ipif->ipif_flags |= IPIF_DUPLICATE; 3850 ill->ill_ipif_dup_count++; 3851 mutex_exit(&ill->ill_lock); 3852 /* 3853 * Already exclusive on the ill; no need to handle deferred 3854 * processing here. 3855 */ 3856 (void) ipif_down(ipif, NULL, NULL); 3857 ipif_down_tail(ipif); 3858 mutex_enter(&ill->ill_lock); 3859 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3860 ill->ill_net_type == IRE_IF_RESOLVER && 3861 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3862 ipst->ips_ip_dup_recovery > 0) { 3863 ASSERT(ipif->ipif_recovery_id == 0); 3864 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3865 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); 3866 } 3867 mutex_exit(&ill->ill_lock); 3868 } 3869 freemsg(mp); 3870 } 3871 3872 /* ARGSUSED */ 3873 static void 3874 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3875 { 3876 ill_t *ill = rq->q_ptr; 3877 arh_t *arh; 3878 ipaddr_t src; 3879 ipif_t *ipif; 3880 3881 arh = (arh_t *)mp->b_cont->b_rptr; 3882 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3883 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3884 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) 3885 (void) ipif_resolver_up(ipif, Res_act_defend); 3886 } 3887 freemsg(mp); 3888 } 3889 3890 /* 3891 * News from ARP. ARP sends notification of interesting events down 3892 * to its clients using M_CTL messages with the interesting ARP packet 3893 * attached via b_cont. 3894 * The interesting event from a device comes up the corresponding ARP-IP-DEV 3895 * queue as opposed to ARP sending the message to all the clients, i.e. all 3896 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 3897 * table if a cache IRE is found to delete all the entries for the address in 3898 * the packet. 3899 */ 3900 static void 3901 ip_arp_news(queue_t *q, mblk_t *mp) 3902 { 3903 arcn_t *arcn; 3904 arh_t *arh; 3905 ire_t *ire = NULL; 3906 char hbuf[MAC_STR_LEN]; 3907 char sbuf[INET_ADDRSTRLEN]; 3908 ipaddr_t src; 3909 in6_addr_t v6src; 3910 boolean_t isv6 = B_FALSE; 3911 ipif_t *ipif; 3912 ill_t *ill; 3913 ip_stack_t *ipst; 3914 3915 if (CONN_Q(q)) { 3916 conn_t *connp = Q_TO_CONN(q); 3917 3918 ipst = connp->conn_netstack->netstack_ip; 3919 } else { 3920 ill_t *ill = (ill_t *)q->q_ptr; 3921 3922 ipst = ill->ill_ipst; 3923 } 3924 3925 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 3926 if (q->q_next) { 3927 putnext(q, mp); 3928 } else 3929 freemsg(mp); 3930 return; 3931 } 3932 arh = (arh_t *)mp->b_cont->b_rptr; 3933 /* Is it one we are interested in? */ 3934 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 3935 isv6 = B_TRUE; 3936 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 3937 IPV6_ADDR_LEN); 3938 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 3939 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 3940 IP_ADDR_LEN); 3941 } else { 3942 freemsg(mp); 3943 return; 3944 } 3945 3946 ill = q->q_ptr; 3947 3948 arcn = (arcn_t *)mp->b_rptr; 3949 switch (arcn->arcn_code) { 3950 case AR_CN_BOGON: 3951 /* 3952 * Someone is sending ARP packets with a source protocol 3953 * address that we have published and for which we believe our 3954 * entry is authoritative and (when ill_arp_extend is set) 3955 * verified to be unique on the network. 3956 * 3957 * The ARP module internally handles the cases where the sender 3958 * is just probing (for DAD) and where the hardware address of 3959 * a non-authoritative entry has changed. Thus, these are the 3960 * real conflicts, and we have to do resolution. 3961 * 3962 * We back away quickly from the address if it's from DHCP or 3963 * otherwise temporary and hasn't been used recently (or at 3964 * all). We'd like to include "deprecated" addresses here as 3965 * well (as there's no real reason to defend something we're 3966 * discarding), but IPMP "reuses" this flag to mean something 3967 * other than the standard meaning. 3968 * 3969 * If the ARP module above is not extended (meaning that it 3970 * doesn't know how to defend the address), then we just log 3971 * the problem as we always did and continue on. It's not 3972 * right, but there's little else we can do, and those old ATM 3973 * users are going away anyway. 3974 */ 3975 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, 3976 hbuf, sizeof (hbuf)); 3977 (void) ip_dot_addr(src, sbuf); 3978 if (isv6) { 3979 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL, 3980 ipst); 3981 } else { 3982 ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst); 3983 } 3984 if (ire != NULL && IRE_IS_LOCAL(ire)) { 3985 uint32_t now; 3986 uint32_t maxage; 3987 clock_t lused; 3988 uint_t maxdefense; 3989 uint_t defs; 3990 3991 /* 3992 * First, figure out if this address hasn't been used 3993 * in a while. If it hasn't, then it's a better 3994 * candidate for abandoning. 3995 */ 3996 ipif = ire->ire_ipif; 3997 ASSERT(ipif != NULL); 3998 now = gethrestime_sec(); 3999 maxage = now - ire->ire_create_time; 4000 if (maxage > ipst->ips_ip_max_temp_idle) 4001 maxage = ipst->ips_ip_max_temp_idle; 4002 lused = drv_hztousec(ddi_get_lbolt() - 4003 ire->ire_last_used_time) / MICROSEC + 1; 4004 if (lused >= maxage && (ipif->ipif_flags & 4005 (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) 4006 maxdefense = ipst->ips_ip_max_temp_defend; 4007 else 4008 maxdefense = ipst->ips_ip_max_defend; 4009 4010 /* 4011 * Now figure out how many times we've defended 4012 * ourselves. Ignore defenses that happened long in 4013 * the past. 4014 */ 4015 mutex_enter(&ire->ire_lock); 4016 if ((defs = ire->ire_defense_count) > 0 && 4017 now - ire->ire_defense_time > 4018 ipst->ips_ip_defend_interval) { 4019 ire->ire_defense_count = defs = 0; 4020 } 4021 ire->ire_defense_count++; 4022 ire->ire_defense_time = now; 4023 mutex_exit(&ire->ire_lock); 4024 ill_refhold(ill); 4025 ire_refrele(ire); 4026 4027 /* 4028 * If we've defended ourselves too many times already, 4029 * then give up and tear down the interface(s) using 4030 * this address. Otherwise, defend by sending out a 4031 * gratuitous ARP. 4032 */ 4033 if (defs >= maxdefense && ill->ill_arp_extend) { 4034 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, 4035 B_FALSE); 4036 } else { 4037 cmn_err(CE_WARN, 4038 "node %s is using our IP address %s on %s", 4039 hbuf, sbuf, ill->ill_name); 4040 /* 4041 * If this is an old (ATM) ARP module, then 4042 * don't try to defend the address. Remain 4043 * compatible with the old behavior. Defend 4044 * only with new ARP. 4045 */ 4046 if (ill->ill_arp_extend) { 4047 qwriter_ip(ill, q, mp, ip_arp_defend, 4048 NEW_OP, B_FALSE); 4049 } else { 4050 ill_refrele(ill); 4051 } 4052 } 4053 return; 4054 } 4055 cmn_err(CE_WARN, 4056 "proxy ARP problem? Node '%s' is using %s on %s", 4057 hbuf, sbuf, ill->ill_name); 4058 if (ire != NULL) 4059 ire_refrele(ire); 4060 break; 4061 case AR_CN_ANNOUNCE: 4062 if (isv6) { 4063 /* 4064 * For XRESOLV interfaces. 4065 * Delete the IRE cache entry and NCE for this 4066 * v6 address 4067 */ 4068 ip_ire_clookup_and_delete_v6(&v6src, ipst); 4069 /* 4070 * If v6src is a non-zero, it's a router address 4071 * as below. Do the same sort of thing to clean 4072 * out off-net IRE_CACHE entries that go through 4073 * the router. 4074 */ 4075 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4076 ire_walk_v6(ire_delete_cache_gw_v6, 4077 (char *)&v6src, ALL_ZONES, ipst); 4078 } 4079 } else { 4080 nce_hw_map_t hwm; 4081 4082 /* 4083 * ARP gives us a copy of any packet where it thinks 4084 * the address has changed, so that we can update our 4085 * caches. We're responsible for caching known answers 4086 * in the current design. We check whether the 4087 * hardware address really has changed in all of our 4088 * entries that have cached this mapping, and if so, we 4089 * blow them away. This way we will immediately pick 4090 * up the rare case of a host changing hardware 4091 * address. 4092 */ 4093 if (src == 0) 4094 break; 4095 hwm.hwm_addr = src; 4096 hwm.hwm_hwlen = arh->arh_hlen; 4097 hwm.hwm_hwaddr = (uchar_t *)(arh + 1); 4098 NDP_HW_CHANGE_INCR(ipst->ips_ndp4); 4099 ndp_walk_common(ipst->ips_ndp4, NULL, 4100 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); 4101 NDP_HW_CHANGE_DECR(ipst->ips_ndp4); 4102 } 4103 break; 4104 case AR_CN_READY: 4105 /* No external v6 resolver has a contract to use this */ 4106 if (isv6) 4107 break; 4108 /* If the link is down, we'll retry this later */ 4109 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) 4110 break; 4111 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, 4112 NULL, NULL, ipst); 4113 if (ipif != NULL) { 4114 /* 4115 * If this is a duplicate recovery, then we now need to 4116 * go exclusive to bring this thing back up. 4117 */ 4118 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == 4119 IPIF_DUPLICATE) { 4120 ipif_refrele(ipif); 4121 ill_refhold(ill); 4122 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, 4123 B_FALSE); 4124 return; 4125 } 4126 /* 4127 * If this is the first notice that this address is 4128 * ready, then let the user know now. 4129 */ 4130 if ((ipif->ipif_flags & IPIF_UP) && 4131 !ipif->ipif_addr_ready) { 4132 ipif_mask_reply(ipif); 4133 ipif_up_notify(ipif); 4134 } 4135 ipif->ipif_addr_ready = 1; 4136 ipif_refrele(ipif); 4137 } 4138 ire = ire_cache_lookup(src, ALL_ZONES, msg_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 if (!ill->ill_arp_extend) { 4149 (void) mac_colon_addr((uint8_t *)(arh + 1), 4150 arh->arh_hlen, hbuf, sizeof (hbuf)); 4151 (void) ip_dot_addr(src, sbuf); 4152 4153 cmn_err(CE_WARN, 4154 "node %s is using our IP address %s on %s", 4155 hbuf, sbuf, ill->ill_name); 4156 break; 4157 } 4158 ill_refhold(ill); 4159 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE); 4160 return; 4161 } 4162 freemsg(mp); 4163 } 4164 4165 /* 4166 * Create a mblk suitable for carrying the interface index and/or source link 4167 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 4168 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 4169 * application. 4170 */ 4171 mblk_t * 4172 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid, 4173 ip_stack_t *ipst) 4174 { 4175 mblk_t *mp; 4176 ip_pktinfo_t *pinfo; 4177 ipha_t *ipha; 4178 struct ether_header *pether; 4179 boolean_t ipmp_ill_held = B_FALSE; 4180 4181 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED); 4182 if (mp == NULL) { 4183 ip1dbg(("ip_add_info: allocation failure.\n")); 4184 return (data_mp); 4185 } 4186 4187 ipha = (ipha_t *)data_mp->b_rptr; 4188 pinfo = (ip_pktinfo_t *)mp->b_rptr; 4189 bzero(pinfo, sizeof (ip_pktinfo_t)); 4190 pinfo->ip_pkt_flags = (uchar_t)flags; 4191 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 4192 4193 pether = (struct ether_header *)((char *)ipha 4194 - sizeof (struct ether_header)); 4195 4196 /* 4197 * Make sure the interface is an ethernet type, since this option 4198 * is currently supported only on this type of interface. Also make 4199 * sure we are pointing correctly above db_base. 4200 */ 4201 if ((flags & IPF_RECVSLLA) && 4202 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 4203 (ill->ill_type == IFT_ETHER) && 4204 (ill->ill_net_type == IRE_IF_RESOLVER)) { 4205 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER; 4206 bcopy(pether->ether_shost.ether_addr_octet, 4207 pinfo->ip_pkt_slla.sdl_data, ETHERADDRL); 4208 } else { 4209 /* 4210 * Clear the bit. Indicate to upper layer that IP is not 4211 * sending this ancillary info. 4212 */ 4213 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA; 4214 } 4215 4216 /* 4217 * If `ill' is in an IPMP group, use the IPMP ill to determine 4218 * IPF_RECVIF and IPF_RECVADDR. (This currently assumes that 4219 * IPF_RECVADDR support on test addresses is not needed.) 4220 * 4221 * Note that `ill' may already be an IPMP ill if e.g. we're 4222 * processing a packet looped back to an IPMP data address 4223 * (since those IRE_LOCALs are tied to IPMP ills). 4224 */ 4225 if (IS_UNDER_IPMP(ill)) { 4226 if ((ill = ipmp_ill_hold_ipmp_ill(ill)) == NULL) { 4227 ip1dbg(("ip_add_info: cannot hold IPMP ill.\n")); 4228 freemsg(mp); 4229 return (data_mp); 4230 } 4231 ipmp_ill_held = B_TRUE; 4232 } 4233 4234 if (flags & (IPF_RECVIF | IPF_RECVADDR)) 4235 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 4236 if (flags & IPF_RECVADDR) { 4237 ipif_t *ipif; 4238 ire_t *ire; 4239 4240 /* 4241 * Only valid for V4 4242 */ 4243 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) == 4244 (IPV4_VERSION << 4)); 4245 4246 ipif = ipif_get_next_ipif(NULL, ill); 4247 if (ipif != NULL) { 4248 /* 4249 * Since a decision has already been made to deliver the 4250 * packet, there is no need to test for SECATTR and 4251 * ZONEONLY. 4252 * When a multicast packet is transmitted 4253 * a cache entry is created for the multicast address. 4254 * When delivering a copy of the packet or when new 4255 * packets are received we do not want to match on the 4256 * cached entry so explicitly match on 4257 * IRE_LOCAL and IRE_LOOPBACK 4258 */ 4259 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 4260 IRE_LOCAL | IRE_LOOPBACK, 4261 ipif, zoneid, NULL, 4262 MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst); 4263 if (ire == NULL) { 4264 /* 4265 * packet must have come on a different 4266 * interface. 4267 * Since a decision has already been made to 4268 * deliver the packet, there is no need to test 4269 * for SECATTR and ZONEONLY. 4270 * Only match on local and broadcast ire's. 4271 * See detailed comment above. 4272 */ 4273 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 4274 IRE_LOCAL | IRE_LOOPBACK, ipif, zoneid, 4275 NULL, MATCH_IRE_TYPE, ipst); 4276 } 4277 4278 if (ire == NULL) { 4279 /* 4280 * This is either a multicast packet or 4281 * the address has been removed since 4282 * the packet was received. 4283 * Return INADDR_ANY so that normal source 4284 * selection occurs for the response. 4285 */ 4286 4287 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4288 } else { 4289 pinfo->ip_pkt_match_addr.s_addr = 4290 ire->ire_src_addr; 4291 ire_refrele(ire); 4292 } 4293 ipif_refrele(ipif); 4294 } else { 4295 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4296 } 4297 } 4298 4299 if (ipmp_ill_held) 4300 ill_refrele(ill); 4301 4302 mp->b_datap->db_type = M_CTL; 4303 mp->b_wptr += sizeof (ip_pktinfo_t); 4304 mp->b_cont = data_mp; 4305 4306 return (mp); 4307 } 4308 4309 /* 4310 * Used to determine the most accurate cred_t to use for TX. 4311 * First priority is SCM_UCRED having set the label in the message, 4312 * which is used for MLP on UDP. Second priority is the open credentials 4313 * with the peer's label (aka conn_effective_cred), which is needed for 4314 * MLP on TCP/SCTP and for MAC-Exempt. Last priority is the open credentials. 4315 */ 4316 cred_t * 4317 ip_best_cred(mblk_t *mp, conn_t *connp, pid_t *pidp) 4318 { 4319 cred_t *cr; 4320 4321 cr = msg_getcred(mp, pidp); 4322 if (cr != NULL && crgetlabel(cr) != NULL) 4323 return (cr); 4324 *pidp = NOPID; 4325 return (CONN_CRED(connp)); 4326 } 4327 4328 /* 4329 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 4330 * part of the bind request. 4331 */ 4332 4333 boolean_t 4334 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 4335 { 4336 ipsec_in_t *ii; 4337 4338 ASSERT(policy_mp != NULL); 4339 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 4340 4341 ii = (ipsec_in_t *)policy_mp->b_rptr; 4342 ASSERT(ii->ipsec_in_type == IPSEC_IN); 4343 4344 connp->conn_policy = ii->ipsec_in_policy; 4345 ii->ipsec_in_policy = NULL; 4346 4347 if (ii->ipsec_in_action != NULL) { 4348 if (connp->conn_latch == NULL) { 4349 connp->conn_latch = iplatch_create(); 4350 if (connp->conn_latch == NULL) 4351 return (B_FALSE); 4352 } 4353 ipsec_latch_inbound(connp->conn_latch, ii); 4354 } 4355 return (B_TRUE); 4356 } 4357 4358 static void 4359 ip_bind_post_handling(conn_t *connp, mblk_t *mp, boolean_t ire_requested) 4360 { 4361 /* 4362 * Pass the IPsec headers size in ire_ipsec_overhead. 4363 * We can't do this in ip_bind_get_ire because the policy 4364 * may not have been inherited at that point in time and hence 4365 * conn_out_enforce_policy may not be set. 4366 */ 4367 if (ire_requested && connp->conn_out_enforce_policy && 4368 mp != NULL && DB_TYPE(mp) == IRE_DB_REQ_TYPE) { 4369 ire_t *ire = (ire_t *)mp->b_rptr; 4370 ASSERT(MBLKL(mp) >= sizeof (ire_t)); 4371 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 4372 } 4373 } 4374 4375 /* 4376 * Upper level protocols (ULP) pass through bind requests to IP for inspection 4377 * and to arrange for power-fanout assist. The ULP is identified by 4378 * adding a single byte at the end of the original bind message. 4379 * A ULP other than UDP or TCP that wishes to be recognized passes 4380 * down a bind with a zero length address. 4381 * 4382 * The binding works as follows: 4383 * - A zero byte address means just bind to the protocol. 4384 * - A four byte address is treated as a request to validate 4385 * that the address is a valid local address, appropriate for 4386 * an application to bind to. This does not affect any fanout 4387 * information in IP. 4388 * - A sizeof sin_t byte address is used to bind to only the local address 4389 * and port. 4390 * - A sizeof ipa_conn_t byte address contains complete fanout information 4391 * consisting of local and remote addresses and ports. In 4392 * this case, the addresses are both validated as appropriate 4393 * for this operation, and, if so, the information is retained 4394 * for use in the inbound fanout. 4395 * 4396 * The ULP (except in the zero-length bind) can append an 4397 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 4398 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 4399 * a copy of the source or destination IRE (source for local bind; 4400 * destination for complete bind). IPSEC_POLICY_SET indicates that the 4401 * policy information contained should be copied on to the conn. 4402 * 4403 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 4404 */ 4405 mblk_t * 4406 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 4407 { 4408 ssize_t len; 4409 struct T_bind_req *tbr; 4410 sin_t *sin; 4411 ipa_conn_t *ac; 4412 uchar_t *ucp; 4413 mblk_t *mp1; 4414 boolean_t ire_requested; 4415 int error = 0; 4416 int protocol; 4417 ipa_conn_x_t *acx; 4418 cred_t *cr; 4419 4420 /* 4421 * All Solaris components should pass a db_credp 4422 * for this TPI message, hence we ASSERT. 4423 * But in case there is some other M_PROTO that looks 4424 * like a TPI message sent by some other kernel 4425 * component, we check and return an error. 4426 */ 4427 cr = msg_getcred(mp, NULL); 4428 ASSERT(cr != NULL); 4429 if (cr == NULL) { 4430 error = EINVAL; 4431 goto bad_addr; 4432 } 4433 4434 ASSERT(!connp->conn_af_isv6); 4435 connp->conn_pkt_isv6 = B_FALSE; 4436 4437 len = MBLKL(mp); 4438 if (len < (sizeof (*tbr) + 1)) { 4439 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 4440 "ip_bind: bogus msg, len %ld", len); 4441 /* XXX: Need to return something better */ 4442 goto bad_addr; 4443 } 4444 /* Back up and extract the protocol identifier. */ 4445 mp->b_wptr--; 4446 protocol = *mp->b_wptr & 0xFF; 4447 tbr = (struct T_bind_req *)mp->b_rptr; 4448 /* Reset the message type in preparation for shipping it back. */ 4449 DB_TYPE(mp) = M_PCPROTO; 4450 4451 connp->conn_ulp = (uint8_t)protocol; 4452 4453 /* 4454 * Check for a zero length address. This is from a protocol that 4455 * wants to register to receive all packets of its type. 4456 */ 4457 if (tbr->ADDR_length == 0) { 4458 /* 4459 * These protocols are now intercepted in ip_bind_v6(). 4460 * Reject protocol-level binds here for now. 4461 * 4462 * For SCTP raw socket, ICMP sends down a bind with sin_t 4463 * so that the protocol type cannot be SCTP. 4464 */ 4465 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 4466 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 4467 goto bad_addr; 4468 } 4469 4470 /* 4471 * 4472 * The udp module never sends down a zero-length address, 4473 * and allowing this on a labeled system will break MLP 4474 * functionality. 4475 */ 4476 if (is_system_labeled() && protocol == IPPROTO_UDP) 4477 goto bad_addr; 4478 4479 if (connp->conn_mac_exempt) 4480 goto bad_addr; 4481 4482 /* No hash here really. The table is big enough. */ 4483 connp->conn_srcv6 = ipv6_all_zeros; 4484 4485 ipcl_proto_insert(connp, protocol); 4486 4487 tbr->PRIM_type = T_BIND_ACK; 4488 return (mp); 4489 } 4490 4491 /* Extract the address pointer from the message. */ 4492 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 4493 tbr->ADDR_length); 4494 if (ucp == NULL) { 4495 ip1dbg(("ip_bind: no address\n")); 4496 goto bad_addr; 4497 } 4498 if (!OK_32PTR(ucp)) { 4499 ip1dbg(("ip_bind: unaligned address\n")); 4500 goto bad_addr; 4501 } 4502 /* 4503 * Check for trailing mps. 4504 */ 4505 4506 mp1 = mp->b_cont; 4507 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 4508 4509 switch (tbr->ADDR_length) { 4510 default: 4511 ip1dbg(("ip_bind: bad address length %d\n", 4512 (int)tbr->ADDR_length)); 4513 goto bad_addr; 4514 4515 case IP_ADDR_LEN: 4516 /* Verification of local address only */ 4517 error = ip_bind_laddr_v4(connp, &mp1, protocol, 4518 *(ipaddr_t *)ucp, 0, B_FALSE); 4519 break; 4520 4521 case sizeof (sin_t): 4522 sin = (sin_t *)ucp; 4523 error = ip_bind_laddr_v4(connp, &mp1, protocol, 4524 sin->sin_addr.s_addr, sin->sin_port, B_TRUE); 4525 break; 4526 4527 case sizeof (ipa_conn_t): 4528 ac = (ipa_conn_t *)ucp; 4529 /* For raw socket, the local port is not set. */ 4530 if (ac->ac_lport == 0) 4531 ac->ac_lport = connp->conn_lport; 4532 /* Always verify destination reachability. */ 4533 error = ip_bind_connected_v4(connp, &mp1, protocol, 4534 &ac->ac_laddr, ac->ac_lport, ac->ac_faddr, ac->ac_fport, 4535 B_TRUE, B_TRUE, cr); 4536 break; 4537 4538 case sizeof (ipa_conn_x_t): 4539 acx = (ipa_conn_x_t *)ucp; 4540 /* 4541 * Whether or not to verify destination reachability depends 4542 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 4543 */ 4544 error = ip_bind_connected_v4(connp, &mp1, protocol, 4545 &acx->acx_conn.ac_laddr, acx->acx_conn.ac_lport, 4546 acx->acx_conn.ac_faddr, acx->acx_conn.ac_fport, 4547 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0, cr); 4548 break; 4549 } 4550 ASSERT(error != EINPROGRESS); 4551 if (error != 0) 4552 goto bad_addr; 4553 4554 ip_bind_post_handling(connp, mp->b_cont, ire_requested); 4555 4556 /* Send it home. */ 4557 mp->b_datap->db_type = M_PCPROTO; 4558 tbr->PRIM_type = T_BIND_ACK; 4559 return (mp); 4560 4561 bad_addr: 4562 /* 4563 * If error = -1 then we generate a TBADADDR - otherwise error is 4564 * a unix errno. 4565 */ 4566 if (error > 0) 4567 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 4568 else 4569 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 4570 return (mp); 4571 } 4572 4573 /* 4574 * Here address is verified to be a valid local address. 4575 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 4576 * address is also considered a valid local address. 4577 * In the case of a broadcast/multicast address, however, the 4578 * upper protocol is expected to reset the src address 4579 * to 0 if it sees a IRE_BROADCAST type returned so that 4580 * no packets are emitted with broadcast/multicast address as 4581 * source address (that violates hosts requirements RFC 1122) 4582 * The addresses valid for bind are: 4583 * (1) - INADDR_ANY (0) 4584 * (2) - IP address of an UP interface 4585 * (3) - IP address of a DOWN interface 4586 * (4) - valid local IP broadcast addresses. In this case 4587 * the conn will only receive packets destined to 4588 * the specified broadcast address. 4589 * (5) - a multicast address. In this case 4590 * the conn will only receive packets destined to 4591 * the specified multicast address. Note: the 4592 * application still has to issue an 4593 * IP_ADD_MEMBERSHIP socket option. 4594 * 4595 * On error, return -1 for TBADADDR otherwise pass the 4596 * errno with TSYSERR reply. 4597 * 4598 * In all the above cases, the bound address must be valid in the current zone. 4599 * When the address is loopback, multicast or broadcast, there might be many 4600 * matching IREs so bind has to look up based on the zone. 4601 * 4602 * Note: lport is in network byte order. 4603 * 4604 */ 4605 int 4606 ip_bind_laddr_v4(conn_t *connp, mblk_t **mpp, uint8_t protocol, 4607 ipaddr_t src_addr, uint16_t lport, boolean_t fanout_insert) 4608 { 4609 int error = 0; 4610 ire_t *src_ire; 4611 zoneid_t zoneid; 4612 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4613 mblk_t *mp = NULL; 4614 boolean_t ire_requested = B_FALSE; 4615 boolean_t ipsec_policy_set = B_FALSE; 4616 4617 if (mpp) 4618 mp = *mpp; 4619 4620 if (mp != NULL) { 4621 ire_requested = (DB_TYPE(mp) == IRE_DB_REQ_TYPE); 4622 ipsec_policy_set = (DB_TYPE(mp) == IPSEC_POLICY_SET); 4623 } 4624 4625 /* 4626 * If it was previously connected, conn_fully_bound would have 4627 * been set. 4628 */ 4629 connp->conn_fully_bound = B_FALSE; 4630 4631 src_ire = NULL; 4632 4633 zoneid = IPCL_ZONEID(connp); 4634 4635 if (src_addr) { 4636 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 4637 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 4638 /* 4639 * If an address other than 0.0.0.0 is requested, 4640 * we verify that it is a valid address for bind 4641 * Note: Following code is in if-else-if form for 4642 * readability compared to a condition check. 4643 */ 4644 /* LINTED - statement has no consequence */ 4645 if (IRE_IS_LOCAL(src_ire)) { 4646 /* 4647 * (2) Bind to address of local UP interface 4648 */ 4649 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 4650 /* 4651 * (4) Bind to broadcast address 4652 * Note: permitted only from transports that 4653 * request IRE 4654 */ 4655 if (!ire_requested) 4656 error = EADDRNOTAVAIL; 4657 } else { 4658 /* 4659 * (3) Bind to address of local DOWN interface 4660 * (ipif_lookup_addr() looks up all interfaces 4661 * but we do not get here for UP interfaces 4662 * - case (2) above) 4663 */ 4664 /* LINTED - statement has no consequent */ 4665 if (ip_addr_exists(src_addr, zoneid, ipst)) { 4666 /* The address exists */ 4667 } else if (CLASSD(src_addr)) { 4668 error = 0; 4669 if (src_ire != NULL) 4670 ire_refrele(src_ire); 4671 /* 4672 * (5) bind to multicast address. 4673 * Fake out the IRE returned to upper 4674 * layer to be a broadcast IRE. 4675 */ 4676 src_ire = ire_ctable_lookup( 4677 INADDR_BROADCAST, INADDR_ANY, 4678 IRE_BROADCAST, NULL, zoneid, NULL, 4679 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY), 4680 ipst); 4681 if (src_ire == NULL || !ire_requested) 4682 error = EADDRNOTAVAIL; 4683 } else { 4684 /* 4685 * Not a valid address for bind 4686 */ 4687 error = EADDRNOTAVAIL; 4688 } 4689 } 4690 if (error) { 4691 /* Red Alert! Attempting to be a bogon! */ 4692 ip1dbg(("ip_bind_laddr_v4: bad src address 0x%x\n", 4693 ntohl(src_addr))); 4694 goto bad_addr; 4695 } 4696 } 4697 4698 /* 4699 * Allow setting new policies. For example, disconnects come 4700 * down as ipa_t bind. As we would have set conn_policy_cached 4701 * to B_TRUE before, we should set it to B_FALSE, so that policy 4702 * can change after the disconnect. 4703 */ 4704 connp->conn_policy_cached = B_FALSE; 4705 4706 /* 4707 * If not fanout_insert this was just an address verification 4708 */ 4709 if (fanout_insert) { 4710 /* 4711 * The addresses have been verified. Time to insert in 4712 * the correct fanout list. 4713 */ 4714 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4715 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4716 connp->conn_lport = lport; 4717 connp->conn_fport = 0; 4718 /* 4719 * Do we need to add a check to reject Multicast packets 4720 */ 4721 error = ipcl_bind_insert(connp, protocol, src_addr, lport); 4722 } 4723 4724 if (error == 0) { 4725 if (ire_requested) { 4726 if (!ip_bind_get_ire_v4(mpp, src_ire, NULL, ipst)) { 4727 error = -1; 4728 /* Falls through to bad_addr */ 4729 } 4730 } else if (ipsec_policy_set) { 4731 if (!ip_bind_ipsec_policy_set(connp, mp)) { 4732 error = -1; 4733 /* Falls through to bad_addr */ 4734 } 4735 } 4736 } 4737 bad_addr: 4738 if (error != 0) { 4739 if (connp->conn_anon_port) { 4740 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4741 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 4742 B_FALSE); 4743 } 4744 connp->conn_mlp_type = mlptSingle; 4745 } 4746 if (src_ire != NULL) 4747 IRE_REFRELE(src_ire); 4748 return (error); 4749 } 4750 4751 int 4752 ip_proto_bind_laddr_v4(conn_t *connp, mblk_t **ire_mpp, uint8_t protocol, 4753 ipaddr_t src_addr, uint16_t lport, boolean_t fanout_insert) 4754 { 4755 int error; 4756 mblk_t *mp = NULL; 4757 boolean_t ire_requested; 4758 4759 if (ire_mpp) 4760 mp = *ire_mpp; 4761 ire_requested = (mp != NULL && DB_TYPE(mp) == IRE_DB_REQ_TYPE); 4762 4763 ASSERT(!connp->conn_af_isv6); 4764 connp->conn_pkt_isv6 = B_FALSE; 4765 connp->conn_ulp = protocol; 4766 4767 error = ip_bind_laddr_v4(connp, ire_mpp, protocol, src_addr, lport, 4768 fanout_insert); 4769 if (error == 0) { 4770 ip_bind_post_handling(connp, ire_mpp ? *ire_mpp : NULL, 4771 ire_requested); 4772 } else if (error < 0) { 4773 error = -TBADADDR; 4774 } 4775 return (error); 4776 } 4777 4778 /* 4779 * Verify that both the source and destination addresses 4780 * are valid. If verify_dst is false, then the destination address may be 4781 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4782 * destination reachability, while tunnels do not. 4783 * Note that we allow connect to broadcast and multicast 4784 * addresses when ire_requested is set. Thus the ULP 4785 * has to check for IRE_BROADCAST and multicast. 4786 * 4787 * Returns zero if ok. 4788 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4789 * (for use with TSYSERR reply). 4790 * 4791 * Note: lport and fport are in network byte order. 4792 */ 4793 int 4794 ip_bind_connected_v4(conn_t *connp, mblk_t **mpp, uint8_t protocol, 4795 ipaddr_t *src_addrp, uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4796 boolean_t fanout_insert, boolean_t verify_dst, cred_t *cr) 4797 { 4798 4799 ire_t *src_ire; 4800 ire_t *dst_ire; 4801 int error = 0; 4802 ire_t *sire = NULL; 4803 ire_t *md_dst_ire = NULL; 4804 ire_t *lso_dst_ire = NULL; 4805 ill_t *ill = NULL; 4806 zoneid_t zoneid; 4807 ipaddr_t src_addr = *src_addrp; 4808 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4809 mblk_t *mp = NULL; 4810 boolean_t ire_requested = B_FALSE; 4811 boolean_t ipsec_policy_set = B_FALSE; 4812 ts_label_t *tsl = NULL; 4813 cred_t *effective_cred = NULL; 4814 4815 if (mpp) 4816 mp = *mpp; 4817 4818 if (mp != NULL) { 4819 ire_requested = (DB_TYPE(mp) == IRE_DB_REQ_TYPE); 4820 ipsec_policy_set = (DB_TYPE(mp) == IPSEC_POLICY_SET); 4821 } 4822 4823 src_ire = dst_ire = NULL; 4824 4825 /* 4826 * If we never got a disconnect before, clear it now. 4827 */ 4828 connp->conn_fully_bound = B_FALSE; 4829 4830 zoneid = IPCL_ZONEID(connp); 4831 4832 /* 4833 * Check whether Trusted Solaris policy allows communication with this 4834 * host, and pretend that the destination is unreachable if not. 4835 * 4836 * This is never a problem for TCP, since that transport is known to 4837 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 4838 * handling. If the remote is unreachable, it will be detected at that 4839 * point, so there's no reason to check it here. 4840 * 4841 * Note that for sendto (and other datagram-oriented friends), this 4842 * check is done as part of the data path label computation instead. 4843 * The check here is just to make non-TCP connect() report the right 4844 * error. 4845 */ 4846 if (is_system_labeled() && !IPCL_IS_TCP(connp)) { 4847 if ((error = tsol_check_dest(cr, &dst_addr, IPV4_VERSION, 4848 connp->conn_mac_exempt, &effective_cred)) != 0) { 4849 if (ip_debug > 2) { 4850 pr_addr_dbg( 4851 "ip_bind_connected_v4:" 4852 " no label for dst %s\n", 4853 AF_INET, &dst_addr); 4854 } 4855 goto bad_addr; 4856 } 4857 4858 /* 4859 * tsol_check_dest() may have created a new cred with 4860 * a modified security label. Use that cred if it exists 4861 * for ire lookups. 4862 */ 4863 if (effective_cred == NULL) { 4864 tsl = crgetlabel(cr); 4865 } else { 4866 tsl = crgetlabel(effective_cred); 4867 } 4868 } 4869 4870 if (CLASSD(dst_addr)) { 4871 /* Pick up an IRE_BROADCAST */ 4872 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4873 NULL, zoneid, tsl, 4874 (MATCH_IRE_RECURSIVE | 4875 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | 4876 MATCH_IRE_SECATTR), ipst); 4877 } else { 4878 /* 4879 * If conn_dontroute is set or if conn_nexthop_set is set, 4880 * and onlink ipif is not found set ENETUNREACH error. 4881 */ 4882 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4883 ipif_t *ipif; 4884 4885 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4886 dst_addr : connp->conn_nexthop_v4, zoneid, ipst); 4887 if (ipif == NULL) { 4888 error = ENETUNREACH; 4889 goto bad_addr; 4890 } 4891 ipif_refrele(ipif); 4892 } 4893 4894 if (connp->conn_nexthop_set) { 4895 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4896 0, 0, NULL, NULL, zoneid, tsl, 4897 MATCH_IRE_SECATTR, ipst); 4898 } else { 4899 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4900 &sire, zoneid, tsl, 4901 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4902 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | 4903 MATCH_IRE_SECATTR), ipst); 4904 } 4905 } 4906 /* 4907 * dst_ire can't be a broadcast when not ire_requested. 4908 * We also prevent ire's with src address INADDR_ANY to 4909 * be used, which are created temporarily for 4910 * sending out packets from endpoints that have 4911 * conn_unspec_src set. If verify_dst is true, the destination must be 4912 * reachable. If verify_dst is false, the destination needn't be 4913 * reachable. 4914 * 4915 * If we match on a reject or black hole, then we've got a 4916 * local failure. May as well fail out the connect() attempt, 4917 * since it's never going to succeed. 4918 */ 4919 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4920 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4921 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4922 /* 4923 * If we're verifying destination reachability, we always want 4924 * to complain here. 4925 * 4926 * If we're not verifying destination reachability but the 4927 * destination has a route, we still want to fail on the 4928 * temporary address and broadcast address tests. 4929 */ 4930 if (verify_dst || (dst_ire != NULL)) { 4931 if (ip_debug > 2) { 4932 pr_addr_dbg("ip_bind_connected_v4:" 4933 "bad connected dst %s\n", 4934 AF_INET, &dst_addr); 4935 } 4936 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4937 error = ENETUNREACH; 4938 else 4939 error = EHOSTUNREACH; 4940 goto bad_addr; 4941 } 4942 } 4943 4944 /* 4945 * If the app does a connect(), it means that it will most likely 4946 * send more than 1 packet to the destination. It makes sense 4947 * to clear the temporary flag. 4948 */ 4949 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4950 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4951 irb_t *irb = dst_ire->ire_bucket; 4952 4953 rw_enter(&irb->irb_lock, RW_WRITER); 4954 /* 4955 * We need to recheck for IRE_MARK_TEMPORARY after acquiring 4956 * the lock to guarantee irb_tmp_ire_cnt. 4957 */ 4958 if (dst_ire->ire_marks & IRE_MARK_TEMPORARY) { 4959 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4960 irb->irb_tmp_ire_cnt--; 4961 } 4962 rw_exit(&irb->irb_lock); 4963 } 4964 4965 /* 4966 * See if we should notify ULP about LSO/MDT; we do this whether or not 4967 * ire_requested is TRUE, in order to handle active connects; LSO/MDT 4968 * eligibility tests for passive connects are handled separately 4969 * through tcp_adapt_ire(). We do this before the source address 4970 * selection, because dst_ire may change after a call to 4971 * ipif_select_source(). This is a best-effort check, as the 4972 * packet for this connection may not actually go through 4973 * dst_ire->ire_stq, and the exact IRE can only be known after 4974 * calling ip_newroute(). This is why we further check on the 4975 * IRE during LSO/Multidata packet transmission in 4976 * tcp_lsosend()/tcp_multisend(). 4977 */ 4978 if (!ipsec_policy_set && dst_ire != NULL && 4979 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4980 (ill = ire_to_ill(dst_ire), ill != NULL)) { 4981 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 4982 lso_dst_ire = dst_ire; 4983 IRE_REFHOLD(lso_dst_ire); 4984 } else if (ipst->ips_ip_multidata_outbound && 4985 ILL_MDT_CAPABLE(ill)) { 4986 md_dst_ire = dst_ire; 4987 IRE_REFHOLD(md_dst_ire); 4988 } 4989 } 4990 4991 if (dst_ire != NULL && dst_ire->ire_type == IRE_LOCAL && 4992 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { 4993 /* 4994 * If the IRE belongs to a different zone, look for a matching 4995 * route in the forwarding table and use the source address from 4996 * that route. 4997 */ 4998 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 4999 zoneid, 0, NULL, 5000 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 5001 MATCH_IRE_RJ_BHOLE, ipst); 5002 if (src_ire == NULL) { 5003 error = EHOSTUNREACH; 5004 goto bad_addr; 5005 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 5006 if (!(src_ire->ire_type & IRE_HOST)) 5007 error = ENETUNREACH; 5008 else 5009 error = EHOSTUNREACH; 5010 goto bad_addr; 5011 } 5012 if (src_addr == INADDR_ANY) 5013 src_addr = src_ire->ire_src_addr; 5014 ire_refrele(src_ire); 5015 src_ire = NULL; 5016 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 5017 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 5018 src_addr = sire->ire_src_addr; 5019 ire_refrele(dst_ire); 5020 dst_ire = sire; 5021 sire = NULL; 5022 } else { 5023 /* 5024 * Pick a source address so that a proper inbound 5025 * load spreading would happen. 5026 */ 5027 ill_t *ire_ill = dst_ire->ire_ipif->ipif_ill; 5028 ipif_t *src_ipif = NULL; 5029 ire_t *ipif_ire; 5030 5031 /* 5032 * Supply a local source address such that inbound 5033 * load spreading happens. 5034 * 5035 * Determine the best source address on this ill for 5036 * the destination. 5037 * 5038 * 1) For broadcast, we should return a broadcast ire 5039 * found above so that upper layers know that the 5040 * destination address is a broadcast address. 5041 * 5042 * 2) If the ipif is DEPRECATED, select a better 5043 * source address. Similarly, if the ipif is on 5044 * the IPMP meta-interface, pick a source address 5045 * at random to improve inbound load spreading. 5046 * 5047 * 3) If the outgoing interface is part of a usesrc 5048 * group, then try selecting a source address from 5049 * the usesrc ILL. 5050 */ 5051 if ((dst_ire->ire_zoneid != zoneid && 5052 dst_ire->ire_zoneid != ALL_ZONES) || 5053 (!(dst_ire->ire_flags & RTF_SETSRC)) && 5054 (!(dst_ire->ire_type & IRE_BROADCAST) && 5055 (IS_IPMP(ire_ill) || 5056 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 5057 (ire_ill->ill_usesrc_ifindex != 0)))) { 5058 /* 5059 * If the destination is reachable via a 5060 * given gateway, the selected source address 5061 * should be in the same subnet as the gateway. 5062 * Otherwise, the destination is not reachable. 5063 * 5064 * If there are no interfaces on the same subnet 5065 * as the destination, ipif_select_source gives 5066 * first non-deprecated interface which might be 5067 * on a different subnet than the gateway. 5068 * This is not desirable. Hence pass the dst_ire 5069 * source address to ipif_select_source. 5070 * It is sure that the destination is reachable 5071 * with the dst_ire source address subnet. 5072 * So passing dst_ire source address to 5073 * ipif_select_source will make sure that the 5074 * selected source will be on the same subnet 5075 * as dst_ire source address. 5076 */ 5077 ipaddr_t saddr = 5078 dst_ire->ire_ipif->ipif_src_addr; 5079 src_ipif = ipif_select_source(ire_ill, 5080 saddr, zoneid); 5081 if (src_ipif != NULL) { 5082 if (IS_VNI(src_ipif->ipif_ill)) { 5083 /* 5084 * For VNI there is no 5085 * interface route 5086 */ 5087 src_addr = 5088 src_ipif->ipif_src_addr; 5089 } else { 5090 ipif_ire = 5091 ipif_to_ire(src_ipif); 5092 if (ipif_ire != NULL) { 5093 IRE_REFRELE(dst_ire); 5094 dst_ire = ipif_ire; 5095 } 5096 src_addr = 5097 dst_ire->ire_src_addr; 5098 } 5099 ipif_refrele(src_ipif); 5100 } else { 5101 src_addr = dst_ire->ire_src_addr; 5102 } 5103 } else { 5104 src_addr = dst_ire->ire_src_addr; 5105 } 5106 } 5107 } 5108 5109 /* 5110 * We do ire_route_lookup() here (and not 5111 * interface lookup as we assert that 5112 * src_addr should only come from an 5113 * UP interface for hard binding. 5114 */ 5115 ASSERT(src_ire == NULL); 5116 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 5117 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); 5118 /* src_ire must be a local|loopback */ 5119 if (!IRE_IS_LOCAL(src_ire)) { 5120 if (ip_debug > 2) { 5121 pr_addr_dbg("ip_bind_connected_v4: bad connected " 5122 "src %s\n", AF_INET, &src_addr); 5123 } 5124 error = EADDRNOTAVAIL; 5125 goto bad_addr; 5126 } 5127 5128 /* 5129 * If the source address is a loopback address, the 5130 * destination had best be local or multicast. 5131 * The transports that can't handle multicast will reject 5132 * those addresses. 5133 */ 5134 if (src_ire->ire_type == IRE_LOOPBACK && 5135 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 5136 ip1dbg(("ip_bind_connected_v4: bad connected loopback\n")); 5137 error = -1; 5138 goto bad_addr; 5139 } 5140 5141 /* 5142 * Allow setting new policies. For example, disconnects come 5143 * down as ipa_t bind. As we would have set conn_policy_cached 5144 * to B_TRUE before, we should set it to B_FALSE, so that policy 5145 * can change after the disconnect. 5146 */ 5147 connp->conn_policy_cached = B_FALSE; 5148 5149 /* 5150 * Set the conn addresses/ports immediately, so the IPsec policy calls 5151 * can handle their passed-in conn's. 5152 */ 5153 5154 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 5155 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 5156 connp->conn_lport = lport; 5157 connp->conn_fport = fport; 5158 *src_addrp = src_addr; 5159 5160 ASSERT(!(ipsec_policy_set && ire_requested)); 5161 if (ire_requested) { 5162 iulp_t *ulp_info = NULL; 5163 5164 /* 5165 * Note that sire will not be NULL if this is an off-link 5166 * connection and there is not cache for that dest yet. 5167 * 5168 * XXX Because of an existing bug, if there are multiple 5169 * default routes, the IRE returned now may not be the actual 5170 * default route used (default routes are chosen in a 5171 * round robin fashion). So if the metrics for different 5172 * default routes are different, we may return the wrong 5173 * metrics. This will not be a problem if the existing 5174 * bug is fixed. 5175 */ 5176 if (sire != NULL) { 5177 ulp_info = &(sire->ire_uinfo); 5178 } 5179 if (!ip_bind_get_ire_v4(mpp, dst_ire, ulp_info, ipst)) { 5180 error = -1; 5181 goto bad_addr; 5182 } 5183 mp = *mpp; 5184 } else if (ipsec_policy_set) { 5185 if (!ip_bind_ipsec_policy_set(connp, mp)) { 5186 error = -1; 5187 goto bad_addr; 5188 } 5189 } 5190 5191 /* 5192 * Cache IPsec policy in this conn. If we have per-socket policy, 5193 * we'll cache that. If we don't, we'll inherit global policy. 5194 * 5195 * We can't insert until the conn reflects the policy. Note that 5196 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 5197 * connections where we don't have a policy. This is to prevent 5198 * global policy lookups in the inbound path. 5199 * 5200 * If we insert before we set conn_policy_cached, 5201 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 5202 * because global policy cound be non-empty. We normally call 5203 * ipsec_check_policy() for conn_policy_cached connections only if 5204 * ipc_in_enforce_policy is set. But in this case, 5205 * conn_policy_cached can get set anytime since we made the 5206 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 5207 * called, which will make the above assumption false. Thus, we 5208 * need to insert after we set conn_policy_cached. 5209 */ 5210 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 5211 goto bad_addr; 5212 5213 if (fanout_insert) { 5214 /* 5215 * The addresses have been verified. Time to insert in 5216 * the correct fanout list. 5217 */ 5218 error = ipcl_conn_insert(connp, protocol, src_addr, 5219 dst_addr, connp->conn_ports); 5220 } 5221 5222 if (error == 0) { 5223 connp->conn_fully_bound = B_TRUE; 5224 /* 5225 * Our initial checks for LSO/MDT have passed; the IRE is not 5226 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 5227 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into 5228 * ip_xxinfo_return(), which performs further checks 5229 * against them and upon success, returns the LSO/MDT info 5230 * mblk which we will attach to the bind acknowledgment. 5231 */ 5232 if (lso_dst_ire != NULL) { 5233 mblk_t *lsoinfo_mp; 5234 5235 ASSERT(ill->ill_lso_capab != NULL); 5236 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp, 5237 ill->ill_name, ill->ill_lso_capab)) != NULL) { 5238 if (mp == NULL) { 5239 *mpp = lsoinfo_mp; 5240 } else { 5241 linkb(mp, lsoinfo_mp); 5242 } 5243 } 5244 } else if (md_dst_ire != NULL) { 5245 mblk_t *mdinfo_mp; 5246 5247 ASSERT(ill->ill_mdt_capab != NULL); 5248 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 5249 ill->ill_name, ill->ill_mdt_capab)) != NULL) { 5250 if (mp == NULL) { 5251 *mpp = mdinfo_mp; 5252 } else { 5253 linkb(mp, mdinfo_mp); 5254 } 5255 } 5256 } 5257 } 5258 bad_addr: 5259 if (ipsec_policy_set) { 5260 ASSERT(mp != NULL); 5261 freeb(mp); 5262 /* 5263 * As of now assume that nothing else accompanies 5264 * IPSEC_POLICY_SET. 5265 */ 5266 *mpp = NULL; 5267 } 5268 if (src_ire != NULL) 5269 IRE_REFRELE(src_ire); 5270 if (dst_ire != NULL) 5271 IRE_REFRELE(dst_ire); 5272 if (sire != NULL) 5273 IRE_REFRELE(sire); 5274 if (md_dst_ire != NULL) 5275 IRE_REFRELE(md_dst_ire); 5276 if (lso_dst_ire != NULL) 5277 IRE_REFRELE(lso_dst_ire); 5278 if (effective_cred != NULL) 5279 crfree(effective_cred); 5280 return (error); 5281 } 5282 5283 int 5284 ip_proto_bind_connected_v4(conn_t *connp, mblk_t **ire_mpp, uint8_t protocol, 5285 ipaddr_t *src_addrp, uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 5286 boolean_t fanout_insert, boolean_t verify_dst, cred_t *cr) 5287 { 5288 int error; 5289 mblk_t *mp = NULL; 5290 boolean_t ire_requested; 5291 5292 if (ire_mpp) 5293 mp = *ire_mpp; 5294 ire_requested = (mp != NULL && DB_TYPE(mp) == IRE_DB_REQ_TYPE); 5295 5296 ASSERT(!connp->conn_af_isv6); 5297 connp->conn_pkt_isv6 = B_FALSE; 5298 connp->conn_ulp = protocol; 5299 5300 /* For raw socket, the local port is not set. */ 5301 if (lport == 0) 5302 lport = connp->conn_lport; 5303 error = ip_bind_connected_v4(connp, ire_mpp, protocol, 5304 src_addrp, lport, dst_addr, fport, fanout_insert, verify_dst, cr); 5305 if (error == 0) { 5306 ip_bind_post_handling(connp, ire_mpp ? *ire_mpp : NULL, 5307 ire_requested); 5308 } else if (error < 0) { 5309 error = -TBADADDR; 5310 } 5311 return (error); 5312 } 5313 5314 /* 5315 * Get the ire in *mpp. Returns false if it fails (due to lack of space). 5316 * Prefers dst_ire over src_ire. 5317 */ 5318 static boolean_t 5319 ip_bind_get_ire_v4(mblk_t **mpp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst) 5320 { 5321 mblk_t *mp = *mpp; 5322 ire_t *ret_ire; 5323 5324 ASSERT(mp != NULL); 5325 5326 if (ire != NULL) { 5327 /* 5328 * mp initialized above to IRE_DB_REQ_TYPE 5329 * appended mblk. Its <upper protocol>'s 5330 * job to make sure there is room. 5331 */ 5332 if ((mp->b_datap->db_lim - mp->b_rptr) < sizeof (ire_t)) 5333 return (B_FALSE); 5334 5335 mp->b_datap->db_type = IRE_DB_TYPE; 5336 mp->b_wptr = mp->b_rptr + sizeof (ire_t); 5337 bcopy(ire, mp->b_rptr, sizeof (ire_t)); 5338 ret_ire = (ire_t *)mp->b_rptr; 5339 /* 5340 * Pass the latest setting of the ip_path_mtu_discovery and 5341 * copy the ulp info if any. 5342 */ 5343 ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? 5344 IPH_DF : 0; 5345 if (ulp_info != NULL) { 5346 bcopy(ulp_info, &(ret_ire->ire_uinfo), 5347 sizeof (iulp_t)); 5348 } 5349 ret_ire->ire_mp = mp; 5350 } else { 5351 /* 5352 * No IRE was found. Remove IRE mblk. 5353 */ 5354 *mpp = mp->b_cont; 5355 freeb(mp); 5356 } 5357 return (B_TRUE); 5358 } 5359 5360 /* 5361 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 5362 * the final piece where we don't. Return a pointer to the first mblk in the 5363 * result, and update the pointer to the next mblk to chew on. If anything 5364 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 5365 * NULL pointer. 5366 */ 5367 mblk_t * 5368 ip_carve_mp(mblk_t **mpp, ssize_t len) 5369 { 5370 mblk_t *mp0; 5371 mblk_t *mp1; 5372 mblk_t *mp2; 5373 5374 if (!len || !mpp || !(mp0 = *mpp)) 5375 return (NULL); 5376 /* If we aren't going to consume the first mblk, we need a dup. */ 5377 if (mp0->b_wptr - mp0->b_rptr > len) { 5378 mp1 = dupb(mp0); 5379 if (mp1) { 5380 /* Partition the data between the two mblks. */ 5381 mp1->b_wptr = mp1->b_rptr + len; 5382 mp0->b_rptr = mp1->b_wptr; 5383 /* 5384 * after adjustments if mblk not consumed is now 5385 * unaligned, try to align it. If this fails free 5386 * all messages and let upper layer recover. 5387 */ 5388 if (!OK_32PTR(mp0->b_rptr)) { 5389 if (!pullupmsg(mp0, -1)) { 5390 freemsg(mp0); 5391 freemsg(mp1); 5392 *mpp = NULL; 5393 return (NULL); 5394 } 5395 } 5396 } 5397 return (mp1); 5398 } 5399 /* Eat through as many mblks as we need to get len bytes. */ 5400 len -= mp0->b_wptr - mp0->b_rptr; 5401 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 5402 if (mp2->b_wptr - mp2->b_rptr > len) { 5403 /* 5404 * We won't consume the entire last mblk. Like 5405 * above, dup and partition it. 5406 */ 5407 mp1->b_cont = dupb(mp2); 5408 mp1 = mp1->b_cont; 5409 if (!mp1) { 5410 /* 5411 * Trouble. Rather than go to a lot of 5412 * trouble to clean up, we free the messages. 5413 * This won't be any worse than losing it on 5414 * the wire. 5415 */ 5416 freemsg(mp0); 5417 freemsg(mp2); 5418 *mpp = NULL; 5419 return (NULL); 5420 } 5421 mp1->b_wptr = mp1->b_rptr + len; 5422 mp2->b_rptr = mp1->b_wptr; 5423 /* 5424 * after adjustments if mblk not consumed is now 5425 * unaligned, try to align it. If this fails free 5426 * all messages and let upper layer recover. 5427 */ 5428 if (!OK_32PTR(mp2->b_rptr)) { 5429 if (!pullupmsg(mp2, -1)) { 5430 freemsg(mp0); 5431 freemsg(mp2); 5432 *mpp = NULL; 5433 return (NULL); 5434 } 5435 } 5436 *mpp = mp2; 5437 return (mp0); 5438 } 5439 /* Decrement len by the amount we just got. */ 5440 len -= mp2->b_wptr - mp2->b_rptr; 5441 } 5442 /* 5443 * len should be reduced to zero now. If not our caller has 5444 * screwed up. 5445 */ 5446 if (len) { 5447 /* Shouldn't happen! */ 5448 freemsg(mp0); 5449 *mpp = NULL; 5450 return (NULL); 5451 } 5452 /* 5453 * We consumed up to exactly the end of an mblk. Detach the part 5454 * we are returning from the rest of the chain. 5455 */ 5456 mp1->b_cont = NULL; 5457 *mpp = mp2; 5458 return (mp0); 5459 } 5460 5461 /* The ill stream is being unplumbed. Called from ip_close */ 5462 int 5463 ip_modclose(ill_t *ill) 5464 { 5465 boolean_t success; 5466 ipsq_t *ipsq; 5467 ipif_t *ipif; 5468 queue_t *q = ill->ill_rq; 5469 ip_stack_t *ipst = ill->ill_ipst; 5470 int i; 5471 5472 /* 5473 * The punlink prior to this may have initiated a capability 5474 * negotiation. But ipsq_enter will block until that finishes or 5475 * times out. 5476 */ 5477 success = ipsq_enter(ill, B_FALSE, NEW_OP); 5478 5479 /* 5480 * Open/close/push/pop is guaranteed to be single threaded 5481 * per stream by STREAMS. FS guarantees that all references 5482 * from top are gone before close is called. So there can't 5483 * be another close thread that has set CONDEMNED on this ill. 5484 * and cause ipsq_enter to return failure. 5485 */ 5486 ASSERT(success); 5487 ipsq = ill->ill_phyint->phyint_ipsq; 5488 5489 /* 5490 * Mark it condemned. No new reference will be made to this ill. 5491 * Lookup functions will return an error. Threads that try to 5492 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 5493 * that the refcnt will drop down to zero. 5494 */ 5495 mutex_enter(&ill->ill_lock); 5496 ill->ill_state_flags |= ILL_CONDEMNED; 5497 for (ipif = ill->ill_ipif; ipif != NULL; 5498 ipif = ipif->ipif_next) { 5499 ipif->ipif_state_flags |= IPIF_CONDEMNED; 5500 } 5501 /* 5502 * Wake up anybody waiting to enter the ipsq. ipsq_enter 5503 * returns error if ILL_CONDEMNED is set 5504 */ 5505 cv_broadcast(&ill->ill_cv); 5506 mutex_exit(&ill->ill_lock); 5507 5508 /* 5509 * Send all the deferred DLPI messages downstream which came in 5510 * during the small window right before ipsq_enter(). We do this 5511 * without waiting for the ACKs because all the ACKs for M_PROTO 5512 * messages are ignored in ip_rput() when ILL_CONDEMNED is set. 5513 */ 5514 ill_dlpi_send_deferred(ill); 5515 5516 /* 5517 * Shut down fragmentation reassembly. 5518 * ill_frag_timer won't start a timer again. 5519 * Now cancel any existing timer 5520 */ 5521 (void) untimeout(ill->ill_frag_timer_id); 5522 (void) ill_frag_timeout(ill, 0); 5523 5524 /* 5525 * Call ill_delete to bring down the ipifs, ilms and ill on 5526 * this ill. Then wait for the refcnts to drop to zero. 5527 * ill_is_freeable checks whether the ill is really quiescent. 5528 * Then make sure that threads that are waiting to enter the 5529 * ipsq have seen the error returned by ipsq_enter and have 5530 * gone away. Then we call ill_delete_tail which does the 5531 * DL_UNBIND_REQ with the driver and then qprocsoff. 5532 */ 5533 ill_delete(ill); 5534 mutex_enter(&ill->ill_lock); 5535 while (!ill_is_freeable(ill)) 5536 cv_wait(&ill->ill_cv, &ill->ill_lock); 5537 while (ill->ill_waiters) 5538 cv_wait(&ill->ill_cv, &ill->ill_lock); 5539 5540 mutex_exit(&ill->ill_lock); 5541 5542 /* 5543 * ill_delete_tail drops reference on ill_ipst, but we need to keep 5544 * it held until the end of the function since the cleanup 5545 * below needs to be able to use the ip_stack_t. 5546 */ 5547 netstack_hold(ipst->ips_netstack); 5548 5549 /* qprocsoff is done via ill_delete_tail */ 5550 ill_delete_tail(ill); 5551 ASSERT(ill->ill_ipst == NULL); 5552 5553 /* 5554 * Walk through all upper (conn) streams and qenable 5555 * those that have queued data. 5556 * close synchronization needs this to 5557 * be done to ensure that all upper layers blocked 5558 * due to flow control to the closing device 5559 * get unblocked. 5560 */ 5561 ip1dbg(("ip_wsrv: walking\n")); 5562 for (i = 0; i < TX_FANOUT_SIZE; i++) { 5563 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]); 5564 } 5565 5566 mutex_enter(&ipst->ips_ip_mi_lock); 5567 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill); 5568 mutex_exit(&ipst->ips_ip_mi_lock); 5569 5570 /* 5571 * credp could be null if the open didn't succeed and ip_modopen 5572 * itself calls ip_close. 5573 */ 5574 if (ill->ill_credp != NULL) 5575 crfree(ill->ill_credp); 5576 5577 /* 5578 * Now we are done with the module close pieces that 5579 * need the netstack_t. 5580 */ 5581 netstack_rele(ipst->ips_netstack); 5582 5583 mi_close_free((IDP)ill); 5584 q->q_ptr = WR(q)->q_ptr = NULL; 5585 5586 ipsq_exit(ipsq); 5587 5588 return (0); 5589 } 5590 5591 /* 5592 * This is called as part of close() for IP, UDP, ICMP, and RTS 5593 * in order to quiesce the conn. 5594 */ 5595 void 5596 ip_quiesce_conn(conn_t *connp) 5597 { 5598 boolean_t drain_cleanup_reqd = B_FALSE; 5599 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 5600 boolean_t ilg_cleanup_reqd = B_FALSE; 5601 ip_stack_t *ipst; 5602 5603 ASSERT(!IPCL_IS_TCP(connp)); 5604 ipst = connp->conn_netstack->netstack_ip; 5605 5606 /* 5607 * Mark the conn as closing, and this conn must not be 5608 * inserted in future into any list. Eg. conn_drain_insert(), 5609 * won't insert this conn into the conn_drain_list. 5610 * Similarly ill_pending_mp_add() will not add any mp to 5611 * the pending mp list, after this conn has started closing. 5612 * 5613 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 5614 * cannot get set henceforth. 5615 */ 5616 mutex_enter(&connp->conn_lock); 5617 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 5618 connp->conn_state_flags |= CONN_CLOSING; 5619 if (connp->conn_idl != NULL) 5620 drain_cleanup_reqd = B_TRUE; 5621 if (connp->conn_oper_pending_ill != NULL) 5622 conn_ioctl_cleanup_reqd = B_TRUE; 5623 if (connp->conn_dhcpinit_ill != NULL) { 5624 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0); 5625 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit); 5626 connp->conn_dhcpinit_ill = NULL; 5627 } 5628 if (connp->conn_ilg_inuse != 0) 5629 ilg_cleanup_reqd = B_TRUE; 5630 mutex_exit(&connp->conn_lock); 5631 5632 if (conn_ioctl_cleanup_reqd) 5633 conn_ioctl_cleanup(connp); 5634 5635 if (is_system_labeled() && connp->conn_anon_port) { 5636 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 5637 connp->conn_mlp_type, connp->conn_ulp, 5638 ntohs(connp->conn_lport), B_FALSE); 5639 connp->conn_anon_port = 0; 5640 } 5641 connp->conn_mlp_type = mlptSingle; 5642 5643 /* 5644 * Remove this conn from any fanout list it is on. 5645 * and then wait for any threads currently operating 5646 * on this endpoint to finish 5647 */ 5648 ipcl_hash_remove(connp); 5649 5650 /* 5651 * Remove this conn from the drain list, and do 5652 * any other cleanup that may be required. 5653 * (Only non-tcp streams may have a non-null conn_idl. 5654 * TCP streams are never flow controlled, and 5655 * conn_idl will be null) 5656 */ 5657 if (drain_cleanup_reqd) 5658 conn_drain_tail(connp, B_TRUE); 5659 5660 if (connp == ipst->ips_ip_g_mrouter) 5661 (void) ip_mrouter_done(NULL, ipst); 5662 5663 if (ilg_cleanup_reqd) 5664 ilg_delete_all(connp); 5665 5666 conn_delete_ire(connp, NULL); 5667 5668 /* 5669 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 5670 * callers from write side can't be there now because close 5671 * is in progress. The only other caller is ipcl_walk 5672 * which checks for the condemned flag. 5673 */ 5674 mutex_enter(&connp->conn_lock); 5675 connp->conn_state_flags |= CONN_CONDEMNED; 5676 while (connp->conn_ref != 1) 5677 cv_wait(&connp->conn_cv, &connp->conn_lock); 5678 connp->conn_state_flags |= CONN_QUIESCED; 5679 mutex_exit(&connp->conn_lock); 5680 } 5681 5682 /* ARGSUSED */ 5683 int 5684 ip_close(queue_t *q, int flags) 5685 { 5686 conn_t *connp; 5687 5688 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 5689 5690 /* 5691 * Call the appropriate delete routine depending on whether this is 5692 * a module or device. 5693 */ 5694 if (WR(q)->q_next != NULL) { 5695 /* This is a module close */ 5696 return (ip_modclose((ill_t *)q->q_ptr)); 5697 } 5698 5699 connp = q->q_ptr; 5700 ip_quiesce_conn(connp); 5701 5702 qprocsoff(q); 5703 5704 /* 5705 * Now we are truly single threaded on this stream, and can 5706 * delete the things hanging off the connp, and finally the connp. 5707 * We removed this connp from the fanout list, it cannot be 5708 * accessed thru the fanouts, and we already waited for the 5709 * conn_ref to drop to 0. We are already in close, so 5710 * there cannot be any other thread from the top. qprocsoff 5711 * has completed, and service has completed or won't run in 5712 * future. 5713 */ 5714 ASSERT(connp->conn_ref == 1); 5715 5716 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 5717 5718 connp->conn_ref--; 5719 ipcl_conn_destroy(connp); 5720 5721 q->q_ptr = WR(q)->q_ptr = NULL; 5722 return (0); 5723 } 5724 5725 /* 5726 * Wapper around putnext() so that ip_rts_request can merely use 5727 * conn_recv. 5728 */ 5729 /*ARGSUSED2*/ 5730 static void 5731 ip_conn_input(void *arg1, mblk_t *mp, void *arg2) 5732 { 5733 conn_t *connp = (conn_t *)arg1; 5734 5735 putnext(connp->conn_rq, mp); 5736 } 5737 5738 /* 5739 * Called when the module is about to be unloaded 5740 */ 5741 void 5742 ip_ddi_destroy(void) 5743 { 5744 tnet_fini(); 5745 5746 icmp_ddi_g_destroy(); 5747 rts_ddi_g_destroy(); 5748 udp_ddi_g_destroy(); 5749 sctp_ddi_g_destroy(); 5750 tcp_ddi_g_destroy(); 5751 ipsec_policy_g_destroy(); 5752 ipcl_g_destroy(); 5753 ip_net_g_destroy(); 5754 ip_ire_g_fini(); 5755 inet_minor_destroy(ip_minor_arena_sa); 5756 #if defined(_LP64) 5757 inet_minor_destroy(ip_minor_arena_la); 5758 #endif 5759 5760 #ifdef DEBUG 5761 list_destroy(&ip_thread_list); 5762 rw_destroy(&ip_thread_rwlock); 5763 tsd_destroy(&ip_thread_data); 5764 #endif 5765 5766 netstack_unregister(NS_IP); 5767 } 5768 5769 /* 5770 * First step in cleanup. 5771 */ 5772 /* ARGSUSED */ 5773 static void 5774 ip_stack_shutdown(netstackid_t stackid, void *arg) 5775 { 5776 ip_stack_t *ipst = (ip_stack_t *)arg; 5777 5778 #ifdef NS_DEBUG 5779 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid); 5780 #endif 5781 5782 /* Get rid of loopback interfaces and their IREs */ 5783 ip_loopback_cleanup(ipst); 5784 5785 /* 5786 * The *_hook_shutdown()s start the process of notifying any 5787 * consumers that things are going away.... nothing is destroyed. 5788 */ 5789 ipv4_hook_shutdown(ipst); 5790 ipv6_hook_shutdown(ipst); 5791 5792 mutex_enter(&ipst->ips_capab_taskq_lock); 5793 ipst->ips_capab_taskq_quit = B_TRUE; 5794 cv_signal(&ipst->ips_capab_taskq_cv); 5795 mutex_exit(&ipst->ips_capab_taskq_lock); 5796 5797 mutex_enter(&ipst->ips_mrt_lock); 5798 ipst->ips_mrt_flags |= IP_MRT_STOP; 5799 cv_signal(&ipst->ips_mrt_cv); 5800 mutex_exit(&ipst->ips_mrt_lock); 5801 } 5802 5803 /* 5804 * Free the IP stack instance. 5805 */ 5806 static void 5807 ip_stack_fini(netstackid_t stackid, void *arg) 5808 { 5809 ip_stack_t *ipst = (ip_stack_t *)arg; 5810 int ret; 5811 5812 #ifdef NS_DEBUG 5813 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid); 5814 #endif 5815 /* 5816 * At this point, all of the notifications that the events and 5817 * protocols are going away have been run, meaning that we can 5818 * now set about starting to clean things up. 5819 */ 5820 ipv4_hook_destroy(ipst); 5821 ipv6_hook_destroy(ipst); 5822 ip_net_destroy(ipst); 5823 5824 mutex_destroy(&ipst->ips_capab_taskq_lock); 5825 cv_destroy(&ipst->ips_capab_taskq_cv); 5826 list_destroy(&ipst->ips_capab_taskq_list); 5827 5828 mutex_enter(&ipst->ips_mrt_lock); 5829 while (!(ipst->ips_mrt_flags & IP_MRT_DONE)) 5830 cv_wait(&ipst->ips_mrt_done_cv, &ipst->ips_mrt_lock); 5831 mutex_destroy(&ipst->ips_mrt_lock); 5832 cv_destroy(&ipst->ips_mrt_cv); 5833 cv_destroy(&ipst->ips_mrt_done_cv); 5834 5835 ipmp_destroy(ipst); 5836 rw_destroy(&ipst->ips_srcid_lock); 5837 5838 ip_kstat_fini(stackid, ipst->ips_ip_mibkp); 5839 ipst->ips_ip_mibkp = NULL; 5840 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp); 5841 ipst->ips_icmp_mibkp = NULL; 5842 ip_kstat2_fini(stackid, ipst->ips_ip_kstat); 5843 ipst->ips_ip_kstat = NULL; 5844 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics)); 5845 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat); 5846 ipst->ips_ip6_kstat = NULL; 5847 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics)); 5848 5849 nd_free(&ipst->ips_ip_g_nd); 5850 kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr)); 5851 ipst->ips_param_arr = NULL; 5852 kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 5853 ipst->ips_ndp_arr = NULL; 5854 5855 ip_mrouter_stack_destroy(ipst); 5856 5857 mutex_destroy(&ipst->ips_ip_mi_lock); 5858 rw_destroy(&ipst->ips_ipsec_capab_ills_lock); 5859 rw_destroy(&ipst->ips_ill_g_usesrc_lock); 5860 rw_destroy(&ipst->ips_ip_g_nd_lock); 5861 5862 ret = untimeout(ipst->ips_igmp_timeout_id); 5863 if (ret == -1) { 5864 ASSERT(ipst->ips_igmp_timeout_id == 0); 5865 } else { 5866 ASSERT(ipst->ips_igmp_timeout_id != 0); 5867 ipst->ips_igmp_timeout_id = 0; 5868 } 5869 ret = untimeout(ipst->ips_igmp_slowtimeout_id); 5870 if (ret == -1) { 5871 ASSERT(ipst->ips_igmp_slowtimeout_id == 0); 5872 } else { 5873 ASSERT(ipst->ips_igmp_slowtimeout_id != 0); 5874 ipst->ips_igmp_slowtimeout_id = 0; 5875 } 5876 ret = untimeout(ipst->ips_mld_timeout_id); 5877 if (ret == -1) { 5878 ASSERT(ipst->ips_mld_timeout_id == 0); 5879 } else { 5880 ASSERT(ipst->ips_mld_timeout_id != 0); 5881 ipst->ips_mld_timeout_id = 0; 5882 } 5883 ret = untimeout(ipst->ips_mld_slowtimeout_id); 5884 if (ret == -1) { 5885 ASSERT(ipst->ips_mld_slowtimeout_id == 0); 5886 } else { 5887 ASSERT(ipst->ips_mld_slowtimeout_id != 0); 5888 ipst->ips_mld_slowtimeout_id = 0; 5889 } 5890 ret = untimeout(ipst->ips_ip_ire_expire_id); 5891 if (ret == -1) { 5892 ASSERT(ipst->ips_ip_ire_expire_id == 0); 5893 } else { 5894 ASSERT(ipst->ips_ip_ire_expire_id != 0); 5895 ipst->ips_ip_ire_expire_id = 0; 5896 } 5897 5898 mutex_destroy(&ipst->ips_igmp_timer_lock); 5899 mutex_destroy(&ipst->ips_mld_timer_lock); 5900 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock); 5901 mutex_destroy(&ipst->ips_mld_slowtimeout_lock); 5902 mutex_destroy(&ipst->ips_ip_addr_avail_lock); 5903 rw_destroy(&ipst->ips_ill_g_lock); 5904 5905 ipobs_fini(ipst); 5906 ip_ire_fini(ipst); 5907 ip6_asp_free(ipst); 5908 conn_drain_fini(ipst); 5909 ipcl_destroy(ipst); 5910 5911 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock); 5912 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock); 5913 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t)); 5914 ipst->ips_ndp4 = NULL; 5915 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t)); 5916 ipst->ips_ndp6 = NULL; 5917 5918 if (ipst->ips_loopback_ksp != NULL) { 5919 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid); 5920 ipst->ips_loopback_ksp = NULL; 5921 } 5922 5923 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t)); 5924 ipst->ips_phyint_g_list = NULL; 5925 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS); 5926 ipst->ips_ill_g_heads = NULL; 5927 5928 ldi_ident_release(ipst->ips_ldi_ident); 5929 kmem_free(ipst, sizeof (*ipst)); 5930 } 5931 5932 /* 5933 * This function is called from the TSD destructor, and is used to debug 5934 * reference count issues in IP. See block comment in <inet/ip_if.h> for 5935 * details. 5936 */ 5937 static void 5938 ip_thread_exit(void *phash) 5939 { 5940 th_hash_t *thh = phash; 5941 5942 rw_enter(&ip_thread_rwlock, RW_WRITER); 5943 list_remove(&ip_thread_list, thh); 5944 rw_exit(&ip_thread_rwlock); 5945 mod_hash_destroy_hash(thh->thh_hash); 5946 kmem_free(thh, sizeof (*thh)); 5947 } 5948 5949 /* 5950 * Called when the IP kernel module is loaded into the kernel 5951 */ 5952 void 5953 ip_ddi_init(void) 5954 { 5955 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter); 5956 5957 /* 5958 * For IP and TCP the minor numbers should start from 2 since we have 4 5959 * initial devices: ip, ip6, tcp, tcp6. 5960 */ 5961 /* 5962 * If this is a 64-bit kernel, then create two separate arenas - 5963 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the 5964 * other for socket apps in the range 2^^18 through 2^^32-1. 5965 */ 5966 ip_minor_arena_la = NULL; 5967 ip_minor_arena_sa = NULL; 5968 #if defined(_LP64) 5969 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", 5970 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) { 5971 cmn_err(CE_PANIC, 5972 "ip_ddi_init: ip_minor_arena_sa creation failed\n"); 5973 } 5974 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la", 5975 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) { 5976 cmn_err(CE_PANIC, 5977 "ip_ddi_init: ip_minor_arena_la creation failed\n"); 5978 } 5979 #else 5980 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", 5981 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) { 5982 cmn_err(CE_PANIC, 5983 "ip_ddi_init: ip_minor_arena_sa creation failed\n"); 5984 } 5985 #endif 5986 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5987 5988 ipcl_g_init(); 5989 ip_ire_g_init(); 5990 ip_net_g_init(); 5991 5992 #ifdef DEBUG 5993 tsd_create(&ip_thread_data, ip_thread_exit); 5994 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL); 5995 list_create(&ip_thread_list, sizeof (th_hash_t), 5996 offsetof(th_hash_t, thh_link)); 5997 #endif 5998 5999 /* 6000 * We want to be informed each time a stack is created or 6001 * destroyed in the kernel, so we can maintain the 6002 * set of udp_stack_t's. 6003 */ 6004 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown, 6005 ip_stack_fini); 6006 6007 ipsec_policy_g_init(); 6008 tcp_ddi_g_init(); 6009 sctp_ddi_g_init(); 6010 6011 tnet_init(); 6012 6013 udp_ddi_g_init(); 6014 rts_ddi_g_init(); 6015 icmp_ddi_g_init(); 6016 } 6017 6018 /* 6019 * Initialize the IP stack instance. 6020 */ 6021 static void * 6022 ip_stack_init(netstackid_t stackid, netstack_t *ns) 6023 { 6024 ip_stack_t *ipst; 6025 ipparam_t *pa; 6026 ipndp_t *na; 6027 major_t major; 6028 6029 #ifdef NS_DEBUG 6030 printf("ip_stack_init(stack %d)\n", stackid); 6031 #endif 6032 6033 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP); 6034 ipst->ips_netstack = ns; 6035 6036 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS, 6037 KM_SLEEP); 6038 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t), 6039 KM_SLEEP); 6040 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 6041 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 6042 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 6043 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 6044 6045 rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 6046 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6047 ipst->ips_igmp_deferred_next = INFINITY; 6048 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 6049 ipst->ips_mld_deferred_next = INFINITY; 6050 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6051 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 6052 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 6053 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 6054 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL); 6055 rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 6056 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 6057 6058 ipcl_init(ipst); 6059 ip_ire_init(ipst); 6060 ip6_asp_init(ipst); 6061 ipif_init(ipst); 6062 conn_drain_init(ipst); 6063 ip_mrouter_stack_init(ipst); 6064 6065 ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT; 6066 ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 6067 ipst->ips_ipv6_frag_timeout = IPV6_FRAG_TIMEOUT; 6068 ipst->ips_ipv6_frag_timo_ms = IPV6_FRAG_TIMEOUT * 1000; 6069 6070 ipst->ips_ip_multirt_log_interval = 1000; 6071 6072 ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT; 6073 ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT; 6074 ipst->ips_ill_index = 1; 6075 6076 ipst->ips_saved_ip_g_forward = -1; 6077 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */ 6078 6079 pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 6080 ipst->ips_param_arr = pa; 6081 bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr)); 6082 6083 na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP); 6084 ipst->ips_ndp_arr = na; 6085 bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); 6086 ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data = 6087 (caddr_t)&ipst->ips_ip_g_forward; 6088 ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data = 6089 (caddr_t)&ipst->ips_ipv6_forward; 6090 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name, 6091 "ip_cgtp_filter") == 0); 6092 ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data = 6093 (caddr_t)&ipst->ips_ip_cgtp_filter; 6094 6095 (void) ip_param_register(&ipst->ips_ip_g_nd, 6096 ipst->ips_param_arr, A_CNT(lcl_param_arr), 6097 ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr)); 6098 6099 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst); 6100 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid); 6101 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics); 6102 ipst->ips_ip6_kstat = 6103 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics); 6104 6105 ipst->ips_ip_src_id = 1; 6106 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL); 6107 6108 ipobs_init(ipst); 6109 ip_net_init(ipst, ns); 6110 ipv4_hook_init(ipst); 6111 ipv6_hook_init(ipst); 6112 ipmp_init(ipst); 6113 6114 /* 6115 * Create the taskq dispatcher thread and initialize related stuff. 6116 */ 6117 ipst->ips_capab_taskq_thread = thread_create(NULL, 0, 6118 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri); 6119 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL); 6120 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL); 6121 list_create(&ipst->ips_capab_taskq_list, sizeof (mblk_t), 6122 offsetof(mblk_t, b_next)); 6123 6124 /* 6125 * Create the mcast_restart_timers_thread() worker thread. 6126 */ 6127 mutex_init(&ipst->ips_mrt_lock, NULL, MUTEX_DEFAULT, NULL); 6128 cv_init(&ipst->ips_mrt_cv, NULL, CV_DEFAULT, NULL); 6129 cv_init(&ipst->ips_mrt_done_cv, NULL, CV_DEFAULT, NULL); 6130 ipst->ips_mrt_thread = thread_create(NULL, 0, 6131 mcast_restart_timers_thread, ipst, 0, &p0, TS_RUN, minclsyspri); 6132 6133 major = mod_name_to_major(INET_NAME); 6134 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident); 6135 return (ipst); 6136 } 6137 6138 /* 6139 * Allocate and initialize a DLPI template of the specified length. (May be 6140 * called as writer.) 6141 */ 6142 mblk_t * 6143 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 6144 { 6145 mblk_t *mp; 6146 6147 mp = allocb(len, BPRI_MED); 6148 if (!mp) 6149 return (NULL); 6150 6151 /* 6152 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 6153 * of which we don't seem to use) are sent with M_PCPROTO, and 6154 * that other DLPI are M_PROTO. 6155 */ 6156 if (prim == DL_INFO_REQ) { 6157 mp->b_datap->db_type = M_PCPROTO; 6158 } else { 6159 mp->b_datap->db_type = M_PROTO; 6160 } 6161 6162 mp->b_wptr = mp->b_rptr + len; 6163 bzero(mp->b_rptr, len); 6164 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 6165 return (mp); 6166 } 6167 6168 /* 6169 * Allocate and initialize a DLPI notification. (May be called as writer.) 6170 */ 6171 mblk_t * 6172 ip_dlnotify_alloc(uint_t notification, uint_t data) 6173 { 6174 dl_notify_ind_t *notifyp; 6175 mblk_t *mp; 6176 6177 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL) 6178 return (NULL); 6179 6180 notifyp = (dl_notify_ind_t *)mp->b_rptr; 6181 notifyp->dl_notification = notification; 6182 notifyp->dl_data = data; 6183 return (mp); 6184 } 6185 6186 /* 6187 * Debug formatting routine. Returns a character string representation of the 6188 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 6189 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 6190 * 6191 * Once the ndd table-printing interfaces are removed, this can be changed to 6192 * standard dotted-decimal form. 6193 */ 6194 char * 6195 ip_dot_addr(ipaddr_t addr, char *buf) 6196 { 6197 uint8_t *ap = (uint8_t *)&addr; 6198 6199 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 6200 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 6201 return (buf); 6202 } 6203 6204 /* 6205 * Write the given MAC address as a printable string in the usual colon- 6206 * separated format. 6207 */ 6208 const char * 6209 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 6210 { 6211 char *bp; 6212 6213 if (alen == 0 || buflen < 4) 6214 return ("?"); 6215 bp = buf; 6216 for (;;) { 6217 /* 6218 * If there are more MAC address bytes available, but we won't 6219 * have any room to print them, then add "..." to the string 6220 * instead. See below for the 'magic number' explanation. 6221 */ 6222 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 6223 (void) strcpy(bp, "..."); 6224 break; 6225 } 6226 (void) sprintf(bp, "%02x", *addr++); 6227 bp += 2; 6228 if (--alen == 0) 6229 break; 6230 *bp++ = ':'; 6231 buflen -= 3; 6232 /* 6233 * At this point, based on the first 'if' statement above, 6234 * either alen == 1 and buflen >= 3, or alen > 1 and 6235 * buflen >= 4. The first case leaves room for the final "xx" 6236 * number and trailing NUL byte. The second leaves room for at 6237 * least "...". Thus the apparently 'magic' numbers chosen for 6238 * that statement. 6239 */ 6240 } 6241 return (buf); 6242 } 6243 6244 /* 6245 * Send an ICMP error after patching up the packet appropriately. Returns 6246 * non-zero if the appropriate MIB should be bumped; zero otherwise. 6247 */ 6248 static boolean_t 6249 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 6250 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, 6251 zoneid_t zoneid, ip_stack_t *ipst) 6252 { 6253 ipha_t *ipha; 6254 mblk_t *first_mp; 6255 boolean_t secure; 6256 unsigned char db_type; 6257 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6258 6259 first_mp = mp; 6260 if (mctl_present) { 6261 mp = mp->b_cont; 6262 secure = ipsec_in_is_secure(first_mp); 6263 ASSERT(mp != NULL); 6264 } else { 6265 /* 6266 * If this is an ICMP error being reported - which goes 6267 * up as M_CTLs, we need to convert them to M_DATA till 6268 * we finish checking with global policy because 6269 * ipsec_check_global_policy() assumes M_DATA as clear 6270 * and M_CTL as secure. 6271 */ 6272 db_type = DB_TYPE(mp); 6273 DB_TYPE(mp) = M_DATA; 6274 secure = B_FALSE; 6275 } 6276 /* 6277 * We are generating an icmp error for some inbound packet. 6278 * Called from all ip_fanout_(udp, tcp, proto) functions. 6279 * Before we generate an error, check with global policy 6280 * to see whether this is allowed to enter the system. As 6281 * there is no "conn", we are checking with global policy. 6282 */ 6283 ipha = (ipha_t *)mp->b_rptr; 6284 if (secure || ipss->ipsec_inbound_v4_policy_present) { 6285 first_mp = ipsec_check_global_policy(first_mp, NULL, 6286 ipha, NULL, mctl_present, ipst->ips_netstack); 6287 if (first_mp == NULL) 6288 return (B_FALSE); 6289 } 6290 6291 if (!mctl_present) 6292 DB_TYPE(mp) = db_type; 6293 6294 if (flags & IP_FF_SEND_ICMP) { 6295 if (flags & IP_FF_HDR_COMPLETE) { 6296 if (ip_hdr_complete(ipha, zoneid, ipst)) { 6297 freemsg(first_mp); 6298 return (B_TRUE); 6299 } 6300 } 6301 if (flags & IP_FF_CKSUM) { 6302 /* 6303 * Have to correct checksum since 6304 * the packet might have been 6305 * fragmented and the reassembly code in ip_rput 6306 * does not restore the IP checksum. 6307 */ 6308 ipha->ipha_hdr_checksum = 0; 6309 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6310 } 6311 switch (icmp_type) { 6312 case ICMP_DEST_UNREACHABLE: 6313 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid, 6314 ipst); 6315 break; 6316 default: 6317 freemsg(first_mp); 6318 break; 6319 } 6320 } else { 6321 freemsg(first_mp); 6322 return (B_FALSE); 6323 } 6324 6325 return (B_TRUE); 6326 } 6327 6328 /* 6329 * Used to send an ICMP error message when a packet is received for 6330 * a protocol that is not supported. The mblk passed as argument 6331 * is consumed by this function. 6332 */ 6333 void 6334 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid, 6335 ip_stack_t *ipst) 6336 { 6337 mblk_t *mp; 6338 ipha_t *ipha; 6339 ill_t *ill; 6340 ipsec_in_t *ii; 6341 6342 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6343 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6344 6345 mp = ipsec_mp->b_cont; 6346 ipsec_mp->b_cont = NULL; 6347 ipha = (ipha_t *)mp->b_rptr; 6348 /* Get ill from index in ipsec_in_t. */ 6349 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 6350 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL, 6351 ipst); 6352 if (ill != NULL) { 6353 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 6354 if (ip_fanout_send_icmp(q, mp, flags, 6355 ICMP_DEST_UNREACHABLE, 6356 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) { 6357 BUMP_MIB(ill->ill_ip_mib, 6358 ipIfStatsInUnknownProtos); 6359 } 6360 } else { 6361 if (ip_fanout_send_icmp_v6(q, mp, flags, 6362 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 6363 0, B_FALSE, zoneid, ipst)) { 6364 BUMP_MIB(ill->ill_ip_mib, 6365 ipIfStatsInUnknownProtos); 6366 } 6367 } 6368 ill_refrele(ill); 6369 } else { /* re-link for the freemsg() below. */ 6370 ipsec_mp->b_cont = mp; 6371 } 6372 6373 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 6374 freemsg(ipsec_mp); 6375 } 6376 6377 /* 6378 * See if the inbound datagram has had IPsec processing applied to it. 6379 */ 6380 boolean_t 6381 ipsec_in_is_secure(mblk_t *ipsec_mp) 6382 { 6383 ipsec_in_t *ii; 6384 6385 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6386 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6387 6388 if (ii->ipsec_in_loopback) { 6389 return (ii->ipsec_in_secure); 6390 } else { 6391 return (ii->ipsec_in_ah_sa != NULL || 6392 ii->ipsec_in_esp_sa != NULL || 6393 ii->ipsec_in_decaps); 6394 } 6395 } 6396 6397 /* 6398 * Handle protocols with which IP is less intimate. There 6399 * can be more than one stream bound to a particular 6400 * protocol. When this is the case, normally each one gets a copy 6401 * of any incoming packets. 6402 * 6403 * IPsec NOTE : 6404 * 6405 * Don't allow a secure packet going up a non-secure connection. 6406 * We don't allow this because 6407 * 6408 * 1) Reply might go out in clear which will be dropped at 6409 * the sending side. 6410 * 2) If the reply goes out in clear it will give the 6411 * adversary enough information for getting the key in 6412 * most of the cases. 6413 * 6414 * Moreover getting a secure packet when we expect clear 6415 * implies that SA's were added without checking for 6416 * policy on both ends. This should not happen once ISAKMP 6417 * is used to negotiate SAs as SAs will be added only after 6418 * verifying the policy. 6419 * 6420 * NOTE : If the packet was tunneled and not multicast we only send 6421 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 6422 * back to delivering packets to AF_INET6 raw sockets. 6423 * 6424 * IPQoS Notes: 6425 * Once we have determined the client, invoke IPPF processing. 6426 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6427 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6428 * ip_policy will be false. 6429 * 6430 * Zones notes: 6431 * Currently only applications in the global zone can create raw sockets for 6432 * protocols other than ICMP. So unlike the broadcast / multicast case of 6433 * ip_fanout_udp(), we only send a copy of the packet to streams in the 6434 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 6435 */ 6436 static void 6437 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 6438 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 6439 zoneid_t zoneid) 6440 { 6441 queue_t *rq; 6442 mblk_t *mp1, *first_mp1; 6443 uint_t protocol = ipha->ipha_protocol; 6444 ipaddr_t dst; 6445 boolean_t one_only; 6446 mblk_t *first_mp = mp; 6447 boolean_t secure; 6448 uint32_t ill_index; 6449 conn_t *connp, *first_connp, *next_connp; 6450 connf_t *connfp; 6451 boolean_t shared_addr; 6452 mib2_ipIfStatsEntry_t *mibptr; 6453 ip_stack_t *ipst = recv_ill->ill_ipst; 6454 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6455 6456 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 6457 if (mctl_present) { 6458 mp = first_mp->b_cont; 6459 secure = ipsec_in_is_secure(first_mp); 6460 ASSERT(mp != NULL); 6461 } else { 6462 secure = B_FALSE; 6463 } 6464 dst = ipha->ipha_dst; 6465 /* 6466 * If the packet was tunneled and not multicast we only send to it 6467 * the first match. 6468 */ 6469 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 6470 !CLASSD(dst)); 6471 6472 shared_addr = (zoneid == ALL_ZONES); 6473 if (shared_addr) { 6474 /* 6475 * We don't allow multilevel ports for raw IP, so no need to 6476 * check for that here. 6477 */ 6478 zoneid = tsol_packet_to_zoneid(mp); 6479 } 6480 6481 connfp = &ipst->ips_ipcl_proto_fanout[protocol]; 6482 mutex_enter(&connfp->connf_lock); 6483 connp = connfp->connf_head; 6484 for (connp = connfp->connf_head; connp != NULL; 6485 connp = connp->conn_next) { 6486 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, 6487 zoneid) && 6488 (!is_system_labeled() || 6489 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6490 connp))) { 6491 break; 6492 } 6493 } 6494 6495 if (connp == NULL) { 6496 /* 6497 * No one bound to these addresses. Is 6498 * there a client that wants all 6499 * unclaimed datagrams? 6500 */ 6501 mutex_exit(&connfp->connf_lock); 6502 /* 6503 * Check for IPPROTO_ENCAP... 6504 */ 6505 if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) { 6506 /* 6507 * If an IPsec mblk is here on a multicast 6508 * tunnel (using ip_mroute stuff), check policy here, 6509 * THEN ship off to ip_mroute_decap(). 6510 * 6511 * BTW, If I match a configured IP-in-IP 6512 * tunnel, this path will not be reached, and 6513 * ip_mroute_decap will never be called. 6514 */ 6515 first_mp = ipsec_check_global_policy(first_mp, connp, 6516 ipha, NULL, mctl_present, ipst->ips_netstack); 6517 if (first_mp != NULL) { 6518 if (mctl_present) 6519 freeb(first_mp); 6520 ip_mroute_decap(q, mp, ill); 6521 } /* Else we already freed everything! */ 6522 } else { 6523 /* 6524 * Otherwise send an ICMP protocol unreachable. 6525 */ 6526 if (ip_fanout_send_icmp(q, first_mp, flags, 6527 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 6528 mctl_present, zoneid, ipst)) { 6529 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos); 6530 } 6531 } 6532 return; 6533 } 6534 6535 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_upq != NULL); 6536 6537 CONN_INC_REF(connp); 6538 first_connp = connp; 6539 6540 /* 6541 * Only send message to one tunnel driver by immediately 6542 * terminating the loop. 6543 */ 6544 connp = one_only ? NULL : connp->conn_next; 6545 6546 for (;;) { 6547 while (connp != NULL) { 6548 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 6549 flags, zoneid) && 6550 (!is_system_labeled() || 6551 tsol_receive_local(mp, &dst, IPV4_VERSION, 6552 shared_addr, connp))) 6553 break; 6554 connp = connp->conn_next; 6555 } 6556 6557 /* 6558 * Copy the packet. 6559 */ 6560 if (connp == NULL || 6561 (((first_mp1 = dupmsg(first_mp)) == NULL) && 6562 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 6563 /* 6564 * No more interested clients or memory 6565 * allocation failed 6566 */ 6567 connp = first_connp; 6568 break; 6569 } 6570 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL); 6571 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 6572 CONN_INC_REF(connp); 6573 mutex_exit(&connfp->connf_lock); 6574 rq = connp->conn_rq; 6575 6576 /* 6577 * Check flow control 6578 */ 6579 if ((IPCL_IS_NONSTR(connp) && PROTO_FLOW_CNTRLD(connp)) || 6580 (!IPCL_IS_NONSTR(connp) && !canputnext(rq))) { 6581 if (flags & IP_FF_RAWIP) { 6582 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6583 } else { 6584 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6585 } 6586 6587 freemsg(first_mp1); 6588 } else { 6589 /* 6590 * Don't enforce here if we're an actual tunnel - 6591 * let "tun" do it instead. 6592 */ 6593 if (!IPCL_IS_IPTUN(connp) && 6594 (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || 6595 secure)) { 6596 first_mp1 = ipsec_check_inbound_policy 6597 (first_mp1, connp, ipha, NULL, 6598 mctl_present); 6599 } 6600 if (first_mp1 != NULL) { 6601 int in_flags = 0; 6602 /* 6603 * ip_fanout_proto also gets called from 6604 * icmp_inbound_error_fanout, in which case 6605 * the msg type is M_CTL. Don't add info 6606 * in this case for the time being. In future 6607 * when there is a need for knowing the 6608 * inbound iface index for ICMP error msgs, 6609 * then this can be changed. 6610 */ 6611 if (connp->conn_recvif) 6612 in_flags = IPF_RECVIF; 6613 /* 6614 * The ULP may support IP_RECVPKTINFO for both 6615 * IP v4 and v6 so pass the appropriate argument 6616 * based on conn IP version. 6617 */ 6618 if (connp->conn_ip_recvpktinfo) { 6619 if (connp->conn_af_isv6) { 6620 /* 6621 * V6 only needs index 6622 */ 6623 in_flags |= IPF_RECVIF; 6624 } else { 6625 /* 6626 * V4 needs index + 6627 * matching address. 6628 */ 6629 in_flags |= IPF_RECVADDR; 6630 } 6631 } 6632 if ((in_flags != 0) && 6633 (mp->b_datap->db_type != M_CTL)) { 6634 /* 6635 * the actual data will be 6636 * contained in b_cont upon 6637 * successful return of the 6638 * following call else 6639 * original mblk is returned 6640 */ 6641 ASSERT(recv_ill != NULL); 6642 mp1 = ip_add_info(mp1, recv_ill, 6643 in_flags, IPCL_ZONEID(connp), ipst); 6644 } 6645 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6646 if (mctl_present) 6647 freeb(first_mp1); 6648 (connp->conn_recv)(connp, mp1, NULL); 6649 } 6650 } 6651 mutex_enter(&connfp->connf_lock); 6652 /* Follow the next pointer before releasing the conn. */ 6653 next_connp = connp->conn_next; 6654 CONN_DEC_REF(connp); 6655 connp = next_connp; 6656 } 6657 6658 /* Last one. Send it upstream. */ 6659 mutex_exit(&connfp->connf_lock); 6660 6661 /* 6662 * If this packet is coming from icmp_inbound_error_fanout ip_policy 6663 * will be set to false. 6664 */ 6665 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6666 ill_index = ill->ill_phyint->phyint_ifindex; 6667 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6668 if (mp == NULL) { 6669 CONN_DEC_REF(connp); 6670 if (mctl_present) { 6671 freeb(first_mp); 6672 } 6673 return; 6674 } 6675 } 6676 6677 rq = connp->conn_rq; 6678 /* 6679 * Check flow control 6680 */ 6681 if ((IPCL_IS_NONSTR(connp) && PROTO_FLOW_CNTRLD(connp)) || 6682 (!IPCL_IS_NONSTR(connp) && !canputnext(rq))) { 6683 if (flags & IP_FF_RAWIP) { 6684 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6685 } else { 6686 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 6687 } 6688 6689 freemsg(first_mp); 6690 } else { 6691 if (IPCL_IS_IPTUN(connp)) { 6692 /* 6693 * Tunneled packet. We enforce policy in the tunnel 6694 * module itself. 6695 * 6696 * Send the WHOLE packet up (incl. IPSEC_IN) without 6697 * a policy check. 6698 * FIXME to use conn_recv for tun later. 6699 */ 6700 putnext(rq, first_mp); 6701 CONN_DEC_REF(connp); 6702 return; 6703 } 6704 6705 if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) { 6706 first_mp = ipsec_check_inbound_policy(first_mp, connp, 6707 ipha, NULL, mctl_present); 6708 } 6709 6710 if (first_mp != NULL) { 6711 int in_flags = 0; 6712 6713 /* 6714 * ip_fanout_proto also gets called 6715 * from icmp_inbound_error_fanout, in 6716 * which case the msg type is M_CTL. 6717 * Don't add info in this case for time 6718 * being. In future when there is a 6719 * need for knowing the inbound iface 6720 * index for ICMP error msgs, then this 6721 * can be changed 6722 */ 6723 if (connp->conn_recvif) 6724 in_flags = IPF_RECVIF; 6725 if (connp->conn_ip_recvpktinfo) { 6726 if (connp->conn_af_isv6) { 6727 /* 6728 * V6 only needs index 6729 */ 6730 in_flags |= IPF_RECVIF; 6731 } else { 6732 /* 6733 * V4 needs index + 6734 * matching address. 6735 */ 6736 in_flags |= IPF_RECVADDR; 6737 } 6738 } 6739 if ((in_flags != 0) && 6740 (mp->b_datap->db_type != M_CTL)) { 6741 6742 /* 6743 * the actual data will be contained in 6744 * b_cont upon successful return 6745 * of the following call else original 6746 * mblk is returned 6747 */ 6748 ASSERT(recv_ill != NULL); 6749 mp = ip_add_info(mp, recv_ill, 6750 in_flags, IPCL_ZONEID(connp), ipst); 6751 } 6752 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6753 (connp->conn_recv)(connp, mp, NULL); 6754 if (mctl_present) 6755 freeb(first_mp); 6756 } 6757 } 6758 CONN_DEC_REF(connp); 6759 } 6760 6761 /* 6762 * Serialize tcp resets by calling tcp_xmit_reset_serialize through 6763 * SQUEUE_ENTER_ONE(SQ_FILL). We do this to ensure the reset is handled on 6764 * the correct squeue, in this case the same squeue as a valid listener with 6765 * no current connection state for the packet we are processing. The function 6766 * is called for synchronizing both IPv4 and IPv6. 6767 */ 6768 void 6769 ip_xmit_reset_serialize(mblk_t *mp, int hdrlen, zoneid_t zoneid, 6770 tcp_stack_t *tcps, conn_t *connp) 6771 { 6772 mblk_t *rst_mp; 6773 tcp_xmit_reset_event_t *eventp; 6774 6775 rst_mp = allocb(sizeof (tcp_xmit_reset_event_t), BPRI_HI); 6776 6777 if (rst_mp == NULL) { 6778 freemsg(mp); 6779 return; 6780 } 6781 6782 rst_mp->b_datap->db_type = M_PROTO; 6783 rst_mp->b_wptr += sizeof (tcp_xmit_reset_event_t); 6784 6785 eventp = (tcp_xmit_reset_event_t *)rst_mp->b_rptr; 6786 eventp->tcp_xre_event = TCP_XRE_EVENT_IP_FANOUT_TCP; 6787 eventp->tcp_xre_iphdrlen = hdrlen; 6788 eventp->tcp_xre_zoneid = zoneid; 6789 eventp->tcp_xre_tcps = tcps; 6790 6791 rst_mp->b_cont = mp; 6792 mp = rst_mp; 6793 6794 /* 6795 * Increment the connref, this ref will be released by the squeue 6796 * framework. 6797 */ 6798 CONN_INC_REF(connp); 6799 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_xmit_reset, connp, 6800 SQ_FILL, SQTAG_XMIT_EARLY_RESET); 6801 } 6802 6803 /* 6804 * Fanout for TCP packets 6805 * The caller puts <fport, lport> in the ports parameter. 6806 * 6807 * IPQoS Notes 6808 * Before sending it to the client, invoke IPPF processing. 6809 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6810 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6811 * ip_policy is false. 6812 */ 6813 static void 6814 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 6815 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 6816 { 6817 mblk_t *first_mp; 6818 boolean_t secure; 6819 uint32_t ill_index; 6820 int ip_hdr_len; 6821 tcph_t *tcph; 6822 boolean_t syn_present = B_FALSE; 6823 conn_t *connp; 6824 ip_stack_t *ipst = recv_ill->ill_ipst; 6825 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 6826 6827 ASSERT(recv_ill != NULL); 6828 6829 first_mp = mp; 6830 if (mctl_present) { 6831 ASSERT(first_mp->b_datap->db_type == M_CTL); 6832 mp = first_mp->b_cont; 6833 secure = ipsec_in_is_secure(first_mp); 6834 ASSERT(mp != NULL); 6835 } else { 6836 secure = B_FALSE; 6837 } 6838 6839 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 6840 6841 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 6842 zoneid, ipst)) == NULL) { 6843 /* 6844 * No connected connection or listener. Send a 6845 * TH_RST via tcp_xmit_listeners_reset. 6846 */ 6847 6848 /* Initiate IPPf processing, if needed. */ 6849 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 6850 uint32_t ill_index; 6851 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6852 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 6853 if (first_mp == NULL) 6854 return; 6855 } 6856 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6857 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", 6858 zoneid)); 6859 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, 6860 ipst->ips_netstack->netstack_tcp, NULL); 6861 return; 6862 } 6863 6864 /* 6865 * Allocate the SYN for the TCP connection here itself 6866 */ 6867 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 6868 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 6869 if (IPCL_IS_TCP(connp)) { 6870 squeue_t *sqp; 6871 6872 /* 6873 * If the queue belongs to a conn, and fused tcp 6874 * loopback is enabled, assign the eager's squeue 6875 * to be that of the active connect's. Note that 6876 * we don't check for IP_FF_LOOPBACK here since this 6877 * routine gets called only for loopback (unlike the 6878 * IPv6 counterpart). 6879 */ 6880 if (do_tcp_fusion && 6881 CONN_Q(q) && IPCL_IS_TCP(Q_TO_CONN(q)) && 6882 !CONN_INBOUND_POLICY_PRESENT(connp, ipss) && 6883 !secure && 6884 !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy) { 6885 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 6886 sqp = Q_TO_CONN(q)->conn_sqp; 6887 } else { 6888 sqp = IP_SQUEUE_GET(lbolt); 6889 } 6890 6891 mp->b_datap->db_struioflag |= STRUIO_EAGER; 6892 DB_CKSUMSTART(mp) = (intptr_t)sqp; 6893 syn_present = B_TRUE; 6894 } 6895 } 6896 6897 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 6898 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 6899 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6900 if ((flags & TH_RST) || (flags & TH_URG)) { 6901 CONN_DEC_REF(connp); 6902 freemsg(first_mp); 6903 return; 6904 } 6905 if (flags & TH_ACK) { 6906 ip_xmit_reset_serialize(first_mp, ip_hdr_len, zoneid, 6907 ipst->ips_netstack->netstack_tcp, connp); 6908 CONN_DEC_REF(connp); 6909 return; 6910 } 6911 6912 CONN_DEC_REF(connp); 6913 freemsg(first_mp); 6914 return; 6915 } 6916 6917 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 6918 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6919 NULL, mctl_present); 6920 if (first_mp == NULL) { 6921 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6922 CONN_DEC_REF(connp); 6923 return; 6924 } 6925 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 6926 ASSERT(syn_present); 6927 if (mctl_present) { 6928 ASSERT(first_mp != mp); 6929 first_mp->b_datap->db_struioflag |= 6930 STRUIO_POLICY; 6931 } else { 6932 ASSERT(first_mp == mp); 6933 mp->b_datap->db_struioflag &= 6934 ~STRUIO_EAGER; 6935 mp->b_datap->db_struioflag |= 6936 STRUIO_POLICY; 6937 } 6938 } else { 6939 /* 6940 * Discard first_mp early since we're dealing with a 6941 * fully-connected conn_t and tcp doesn't do policy in 6942 * this case. 6943 */ 6944 if (mctl_present) { 6945 freeb(first_mp); 6946 mctl_present = B_FALSE; 6947 } 6948 first_mp = mp; 6949 } 6950 } 6951 6952 /* 6953 * Initiate policy processing here if needed. If we get here from 6954 * icmp_inbound_error_fanout, ip_policy is false. 6955 */ 6956 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 6957 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6958 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6959 if (mp == NULL) { 6960 CONN_DEC_REF(connp); 6961 if (mctl_present) 6962 freeb(first_mp); 6963 return; 6964 } else if (mctl_present) { 6965 ASSERT(first_mp != mp); 6966 first_mp->b_cont = mp; 6967 } else { 6968 first_mp = mp; 6969 } 6970 } 6971 6972 /* Handle socket options. */ 6973 if (!syn_present && 6974 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6975 /* Add header */ 6976 ASSERT(recv_ill != NULL); 6977 /* 6978 * Since tcp does not support IP_RECVPKTINFO for V4, only pass 6979 * IPF_RECVIF. 6980 */ 6981 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp), 6982 ipst); 6983 if (mp == NULL) { 6984 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6985 CONN_DEC_REF(connp); 6986 if (mctl_present) 6987 freeb(first_mp); 6988 return; 6989 } else if (mctl_present) { 6990 /* 6991 * ip_add_info might return a new mp. 6992 */ 6993 ASSERT(first_mp != mp); 6994 first_mp->b_cont = mp; 6995 } else { 6996 first_mp = mp; 6997 } 6998 } 6999 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 7000 if (IPCL_IS_TCP(connp)) { 7001 /* do not drain, certain use cases can blow the stack */ 7002 SQUEUE_ENTER_ONE(connp->conn_sqp, first_mp, connp->conn_recv, 7003 connp, SQ_NODRAIN, SQTAG_IP_FANOUT_TCP); 7004 } else { 7005 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */ 7006 (connp->conn_recv)(connp, first_mp, NULL); 7007 CONN_DEC_REF(connp); 7008 } 7009 } 7010 7011 /* 7012 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or 7013 * pass it along to ESP if the SPI is non-zero. Returns TRUE if the mblk 7014 * is not consumed. 7015 * 7016 * One of four things can happen, all of which affect the passed-in mblk: 7017 * 7018 * 1.) ICMP messages that go through here just get returned TRUE. 7019 * 7020 * 2.) The packet is stock UDP and gets its zero-SPI stripped. Return TRUE. 7021 * 7022 * 3.) The packet is ESP-in-UDP, gets transformed into an equivalent 7023 * ESP packet, and is passed along to ESP for consumption. Return FALSE. 7024 * 7025 * 4.) The packet is an ESP-in-UDP Keepalive. Drop it and return FALSE. 7026 */ 7027 static boolean_t 7028 zero_spi_check(queue_t *q, mblk_t *mp, ire_t *ire, ill_t *recv_ill, 7029 ipsec_stack_t *ipss) 7030 { 7031 int shift, plen, iph_len; 7032 ipha_t *ipha; 7033 udpha_t *udpha; 7034 uint32_t *spi; 7035 uint32_t esp_ports; 7036 uint8_t *orptr; 7037 boolean_t free_ire; 7038 7039 if (DB_TYPE(mp) == M_CTL) { 7040 /* 7041 * ICMP message with UDP inside. Don't bother stripping, just 7042 * send it up. 7043 * 7044 * NOTE: Any app with UDP_NAT_T_ENDPOINT set is probably going 7045 * to ignore errors set by ICMP anyway ('cause they might be 7046 * forged), but that's the app's decision, not ours. 7047 */ 7048 7049 /* Bunch of reality checks for DEBUG kernels... */ 7050 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); 7051 ASSERT(((ipha_t *)mp->b_rptr)->ipha_protocol == IPPROTO_ICMP); 7052 7053 return (B_TRUE); 7054 } 7055 7056 ipha = (ipha_t *)mp->b_rptr; 7057 iph_len = IPH_HDR_LENGTH(ipha); 7058 plen = ntohs(ipha->ipha_length); 7059 7060 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) { 7061 /* 7062 * Most likely a keepalive for the benefit of an intervening 7063 * NAT. These aren't for us, per se, so drop it. 7064 * 7065 * RFC 3947/8 doesn't say for sure what to do for 2-3 7066 * byte packets (keepalives are 1-byte), but we'll drop them 7067 * also. 7068 */ 7069 ip_drop_packet(mp, B_TRUE, recv_ill, NULL, 7070 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper); 7071 return (B_FALSE); 7072 } 7073 7074 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) { 7075 /* might as well pull it all up - it might be ESP. */ 7076 if (!pullupmsg(mp, -1)) { 7077 ip_drop_packet(mp, B_TRUE, recv_ill, NULL, 7078 DROPPER(ipss, ipds_esp_nomem), 7079 &ipss->ipsec_dropper); 7080 return (B_FALSE); 7081 } 7082 7083 ipha = (ipha_t *)mp->b_rptr; 7084 } 7085 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t)); 7086 if (*spi == 0) { 7087 /* UDP packet - remove 0-spi. */ 7088 shift = sizeof (uint32_t); 7089 } else { 7090 /* ESP-in-UDP packet - reduce to ESP. */ 7091 ipha->ipha_protocol = IPPROTO_ESP; 7092 shift = sizeof (udpha_t); 7093 } 7094 7095 /* Fix IP header */ 7096 ipha->ipha_length = htons(plen - shift); 7097 ipha->ipha_hdr_checksum = 0; 7098 7099 orptr = mp->b_rptr; 7100 mp->b_rptr += shift; 7101 7102 udpha = (udpha_t *)(orptr + iph_len); 7103 if (*spi == 0) { 7104 ASSERT((uint8_t *)ipha == orptr); 7105 udpha->uha_length = htons(plen - shift - iph_len); 7106 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */ 7107 esp_ports = 0; 7108 } else { 7109 esp_ports = *((uint32_t *)udpha); 7110 ASSERT(esp_ports != 0); 7111 } 7112 ovbcopy(orptr, orptr + shift, iph_len); 7113 if (esp_ports != 0) /* Punt up for ESP processing. */ { 7114 ipha = (ipha_t *)(orptr + shift); 7115 7116 free_ire = (ire == NULL); 7117 if (free_ire) { 7118 /* Re-acquire ire. */ 7119 ire = ire_cache_lookup(ipha->ipha_dst, ALL_ZONES, NULL, 7120 ipss->ipsec_netstack->netstack_ip); 7121 if (ire == NULL || !(ire->ire_type & IRE_LOCAL)) { 7122 if (ire != NULL) 7123 ire_refrele(ire); 7124 /* 7125 * Do a regular freemsg(), as this is an IP 7126 * error (no local route) not an IPsec one. 7127 */ 7128 freemsg(mp); 7129 } 7130 } 7131 7132 ip_proto_input(q, mp, ipha, ire, recv_ill, esp_ports); 7133 if (free_ire) 7134 ire_refrele(ire); 7135 } 7136 7137 return (esp_ports == 0); 7138 } 7139 7140 /* 7141 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 7142 * We are responsible for disposing of mp, such as by freemsg() or putnext() 7143 * Caller is responsible for dropping references to the conn, and freeing 7144 * first_mp. 7145 * 7146 * IPQoS Notes 7147 * Before sending it to the client, invoke IPPF processing. Policy processing 7148 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 7149 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 7150 * ip_wput_local, ip_policy is false. 7151 */ 7152 static void 7153 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 7154 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 7155 boolean_t ip_policy) 7156 { 7157 boolean_t mctl_present = (first_mp != NULL); 7158 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 7159 uint32_t ill_index; 7160 ip_stack_t *ipst = recv_ill->ill_ipst; 7161 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 7162 7163 ASSERT(ill != NULL); 7164 7165 if (mctl_present) 7166 first_mp->b_cont = mp; 7167 else 7168 first_mp = mp; 7169 7170 if ((IPCL_IS_NONSTR(connp) && PROTO_FLOW_CNTRLD(connp)) || 7171 (!IPCL_IS_NONSTR(connp) && CONN_UDP_FLOWCTLD(connp))) { 7172 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 7173 freemsg(first_mp); 7174 return; 7175 } 7176 7177 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { 7178 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 7179 NULL, mctl_present); 7180 /* Freed by ipsec_check_inbound_policy(). */ 7181 if (first_mp == NULL) { 7182 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 7183 return; 7184 } 7185 } 7186 if (mctl_present) 7187 freeb(first_mp); 7188 7189 /* Let's hope the compilers utter "branch, predict-not-taken..." ;) */ 7190 if (connp->conn_udp->udp_nat_t_endpoint) { 7191 if (mctl_present) { 7192 /* mctl_present *shouldn't* happen. */ 7193 ip_drop_packet(mp, B_TRUE, NULL, NULL, 7194 DROPPER(ipss, ipds_esp_nat_t_ipsec), 7195 &ipss->ipsec_dropper); 7196 return; 7197 } 7198 7199 if (!zero_spi_check(ill->ill_rq, mp, NULL, recv_ill, ipss)) 7200 return; 7201 } 7202 7203 /* Handle options. */ 7204 if (connp->conn_recvif) 7205 in_flags = IPF_RECVIF; 7206 /* 7207 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag 7208 * passed to ip_add_info is based on IP version of connp. 7209 */ 7210 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 7211 if (connp->conn_af_isv6) { 7212 /* 7213 * V6 only needs index 7214 */ 7215 in_flags |= IPF_RECVIF; 7216 } else { 7217 /* 7218 * V4 needs index + matching address. 7219 */ 7220 in_flags |= IPF_RECVADDR; 7221 } 7222 } 7223 7224 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 7225 in_flags |= IPF_RECVSLLA; 7226 7227 /* 7228 * Initiate IPPF processing here, if needed. Note first_mp won't be 7229 * freed if the packet is dropped. The caller will do so. 7230 */ 7231 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { 7232 ill_index = recv_ill->ill_phyint->phyint_ifindex; 7233 ip_process(IPP_LOCAL_IN, &mp, ill_index); 7234 if (mp == NULL) { 7235 return; 7236 } 7237 } 7238 if ((in_flags != 0) && 7239 (mp->b_datap->db_type != M_CTL)) { 7240 /* 7241 * The actual data will be contained in b_cont 7242 * upon successful return of the following call 7243 * else original mblk is returned 7244 */ 7245 ASSERT(recv_ill != NULL); 7246 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp), 7247 ipst); 7248 } 7249 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 7250 /* Send it upstream */ 7251 (connp->conn_recv)(connp, mp, NULL); 7252 } 7253 7254 /* 7255 * Fanout for UDP packets. 7256 * The caller puts <fport, lport> in the ports parameter. 7257 * 7258 * If SO_REUSEADDR is set all multicast and broadcast packets 7259 * will be delivered to all streams bound to the same port. 7260 * 7261 * Zones notes: 7262 * Multicast and broadcast packets will be distributed to streams in all zones. 7263 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 7264 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 7265 * packets. To maintain this behavior with multiple zones, the conns are grouped 7266 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 7267 * each zone. If unset, all the following conns in the same zone are skipped. 7268 */ 7269 static void 7270 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 7271 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 7272 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 7273 { 7274 uint32_t dstport, srcport; 7275 ipaddr_t dst; 7276 mblk_t *first_mp; 7277 boolean_t secure; 7278 in6_addr_t v6src; 7279 conn_t *connp; 7280 connf_t *connfp; 7281 conn_t *first_connp; 7282 conn_t *next_connp; 7283 mblk_t *mp1, *first_mp1; 7284 ipaddr_t src; 7285 zoneid_t last_zoneid; 7286 boolean_t reuseaddr; 7287 boolean_t shared_addr; 7288 boolean_t unlabeled; 7289 ip_stack_t *ipst; 7290 7291 ASSERT(recv_ill != NULL); 7292 ipst = recv_ill->ill_ipst; 7293 7294 first_mp = mp; 7295 if (mctl_present) { 7296 mp = first_mp->b_cont; 7297 first_mp->b_cont = NULL; 7298 secure = ipsec_in_is_secure(first_mp); 7299 ASSERT(mp != NULL); 7300 } else { 7301 first_mp = NULL; 7302 secure = B_FALSE; 7303 } 7304 7305 /* Extract ports in net byte order */ 7306 dstport = htons(ntohl(ports) & 0xFFFF); 7307 srcport = htons(ntohl(ports) >> 16); 7308 dst = ipha->ipha_dst; 7309 src = ipha->ipha_src; 7310 7311 unlabeled = B_FALSE; 7312 if (is_system_labeled()) 7313 /* Cred cannot be null on IPv4 */ 7314 unlabeled = (msg_getlabel(mp)->tsl_flags & 7315 TSLF_UNLABELED) != 0; 7316 shared_addr = (zoneid == ALL_ZONES); 7317 if (shared_addr) { 7318 /* 7319 * No need to handle exclusive-stack zones since ALL_ZONES 7320 * only applies to the shared stack. 7321 */ 7322 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 7323 /* 7324 * If no shared MLP is found, tsol_mlp_findzone returns 7325 * ALL_ZONES. In that case, we assume it's SLP, and 7326 * search for the zone based on the packet label. 7327 * 7328 * If there is such a zone, we prefer to find a 7329 * connection in it. Otherwise, we look for a 7330 * MAC-exempt connection in any zone whose label 7331 * dominates the default label on the packet. 7332 */ 7333 if (zoneid == ALL_ZONES) 7334 zoneid = tsol_packet_to_zoneid(mp); 7335 else 7336 unlabeled = B_FALSE; 7337 } 7338 7339 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7340 mutex_enter(&connfp->connf_lock); 7341 connp = connfp->connf_head; 7342 if (!broadcast && !CLASSD(dst)) { 7343 /* 7344 * Not broadcast or multicast. Send to the one (first) 7345 * client we find. No need to check conn_wantpacket() 7346 * since IP_BOUND_IF/conn_incoming_ill does not apply to 7347 * IPv4 unicast packets. 7348 */ 7349 while ((connp != NULL) && 7350 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || 7351 (!IPCL_ZONE_MATCH(connp, zoneid) && 7352 !(unlabeled && connp->conn_mac_exempt && shared_addr)))) { 7353 /* 7354 * We keep searching since the conn did not match, 7355 * or its zone did not match and it is not either 7356 * an allzones conn or a mac exempt conn (if the 7357 * sender is unlabeled.) 7358 */ 7359 connp = connp->conn_next; 7360 } 7361 7362 if (connp == NULL || 7363 !IPCL_IS_NONSTR(connp) && connp->conn_upq == NULL) 7364 goto notfound; 7365 7366 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_upq != NULL); 7367 7368 if (is_system_labeled() && 7369 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7370 connp)) 7371 goto notfound; 7372 7373 CONN_INC_REF(connp); 7374 mutex_exit(&connfp->connf_lock); 7375 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7376 flags, recv_ill, ip_policy); 7377 IP_STAT(ipst, ip_udp_fannorm); 7378 CONN_DEC_REF(connp); 7379 return; 7380 } 7381 7382 /* 7383 * Broadcast and multicast case 7384 * 7385 * Need to check conn_wantpacket(). 7386 * If SO_REUSEADDR has been set on the first we send the 7387 * packet to all clients that have joined the group and 7388 * match the port. 7389 */ 7390 7391 while (connp != NULL) { 7392 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 7393 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7394 (!is_system_labeled() || 7395 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7396 connp))) 7397 break; 7398 connp = connp->conn_next; 7399 } 7400 7401 if (connp == NULL || 7402 !IPCL_IS_NONSTR(connp) && connp->conn_upq == NULL) 7403 goto notfound; 7404 7405 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_upq != NULL); 7406 7407 first_connp = connp; 7408 /* 7409 * When SO_REUSEADDR is not set, send the packet only to the first 7410 * matching connection in its zone by keeping track of the zoneid. 7411 */ 7412 reuseaddr = first_connp->conn_reuseaddr; 7413 last_zoneid = first_connp->conn_zoneid; 7414 7415 CONN_INC_REF(connp); 7416 connp = connp->conn_next; 7417 for (;;) { 7418 while (connp != NULL) { 7419 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 7420 (reuseaddr || connp->conn_zoneid != last_zoneid) && 7421 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7422 (!is_system_labeled() || 7423 tsol_receive_local(mp, &dst, IPV4_VERSION, 7424 shared_addr, connp))) 7425 break; 7426 connp = connp->conn_next; 7427 } 7428 /* 7429 * Just copy the data part alone. The mctl part is 7430 * needed just for verifying policy and it is never 7431 * sent up. 7432 */ 7433 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7434 ((mp1 = copymsg(mp)) == NULL))) { 7435 /* 7436 * No more interested clients or memory 7437 * allocation failed 7438 */ 7439 connp = first_connp; 7440 break; 7441 } 7442 if (connp->conn_zoneid != last_zoneid) { 7443 /* 7444 * Update the zoneid so that the packet isn't sent to 7445 * any more conns in the same zone unless SO_REUSEADDR 7446 * is set. 7447 */ 7448 reuseaddr = connp->conn_reuseaddr; 7449 last_zoneid = connp->conn_zoneid; 7450 } 7451 if (first_mp != NULL) { 7452 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7453 ipsec_info_type == IPSEC_IN); 7454 first_mp1 = ipsec_in_tag(first_mp, NULL, 7455 ipst->ips_netstack); 7456 if (first_mp1 == NULL) { 7457 freemsg(mp1); 7458 connp = first_connp; 7459 break; 7460 } 7461 } else { 7462 first_mp1 = NULL; 7463 } 7464 CONN_INC_REF(connp); 7465 mutex_exit(&connfp->connf_lock); 7466 /* 7467 * IPQoS notes: We don't send the packet for policy 7468 * processing here, will do it for the last one (below). 7469 * i.e. we do it per-packet now, but if we do policy 7470 * processing per-conn, then we would need to do it 7471 * here too. 7472 */ 7473 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7474 ipha, flags, recv_ill, B_FALSE); 7475 mutex_enter(&connfp->connf_lock); 7476 /* Follow the next pointer before releasing the conn. */ 7477 next_connp = connp->conn_next; 7478 IP_STAT(ipst, ip_udp_fanmb); 7479 CONN_DEC_REF(connp); 7480 connp = next_connp; 7481 } 7482 7483 /* Last one. Send it upstream. */ 7484 mutex_exit(&connfp->connf_lock); 7485 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7486 recv_ill, ip_policy); 7487 IP_STAT(ipst, ip_udp_fanmb); 7488 CONN_DEC_REF(connp); 7489 return; 7490 7491 notfound: 7492 7493 mutex_exit(&connfp->connf_lock); 7494 IP_STAT(ipst, ip_udp_fanothers); 7495 /* 7496 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 7497 * have already been matched above, since they live in the IPv4 7498 * fanout tables. This implies we only need to 7499 * check for IPv6 in6addr_any endpoints here. 7500 * Thus we compare using ipv6_all_zeros instead of the destination 7501 * address, except for the multicast group membership lookup which 7502 * uses the IPv4 destination. 7503 */ 7504 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 7505 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; 7506 mutex_enter(&connfp->connf_lock); 7507 connp = connfp->connf_head; 7508 if (!broadcast && !CLASSD(dst)) { 7509 while (connp != NULL) { 7510 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7511 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && 7512 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7513 !connp->conn_ipv6_v6only) 7514 break; 7515 connp = connp->conn_next; 7516 } 7517 7518 if (connp != NULL && is_system_labeled() && 7519 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7520 connp)) 7521 connp = NULL; 7522 7523 if (connp == NULL || 7524 !IPCL_IS_NONSTR(connp) && connp->conn_upq == NULL) { 7525 /* 7526 * No one bound to this port. Is 7527 * there a client that wants all 7528 * unclaimed datagrams? 7529 */ 7530 mutex_exit(&connfp->connf_lock); 7531 7532 if (mctl_present) 7533 first_mp->b_cont = mp; 7534 else 7535 first_mp = mp; 7536 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP]. 7537 connf_head != NULL) { 7538 ip_fanout_proto(q, first_mp, ill, ipha, 7539 flags | IP_FF_RAWIP, mctl_present, 7540 ip_policy, recv_ill, zoneid); 7541 } else { 7542 if (ip_fanout_send_icmp(q, first_mp, flags, 7543 ICMP_DEST_UNREACHABLE, 7544 ICMP_PORT_UNREACHABLE, 7545 mctl_present, zoneid, ipst)) { 7546 BUMP_MIB(ill->ill_ip_mib, 7547 udpIfStatsNoPorts); 7548 } 7549 } 7550 return; 7551 } 7552 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_upq != NULL); 7553 7554 CONN_INC_REF(connp); 7555 mutex_exit(&connfp->connf_lock); 7556 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7557 flags, recv_ill, ip_policy); 7558 CONN_DEC_REF(connp); 7559 return; 7560 } 7561 /* 7562 * IPv4 multicast packet being delivered to an AF_INET6 7563 * in6addr_any endpoint. 7564 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 7565 * and not conn_wantpacket_v6() since any multicast membership is 7566 * for an IPv4-mapped multicast address. 7567 * The packet is sent to all clients in all zones that have joined the 7568 * group and match the port. 7569 */ 7570 while (connp != NULL) { 7571 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7572 srcport, v6src) && 7573 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7574 (!is_system_labeled() || 7575 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7576 connp))) 7577 break; 7578 connp = connp->conn_next; 7579 } 7580 7581 if (connp == NULL || 7582 !IPCL_IS_NONSTR(connp) && connp->conn_upq == NULL) { 7583 /* 7584 * No one bound to this port. Is 7585 * there a client that wants all 7586 * unclaimed datagrams? 7587 */ 7588 mutex_exit(&connfp->connf_lock); 7589 7590 if (mctl_present) 7591 first_mp->b_cont = mp; 7592 else 7593 first_mp = mp; 7594 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head != 7595 NULL) { 7596 ip_fanout_proto(q, first_mp, ill, ipha, 7597 flags | IP_FF_RAWIP, mctl_present, ip_policy, 7598 recv_ill, zoneid); 7599 } else { 7600 /* 7601 * We used to attempt to send an icmp error here, but 7602 * since this is known to be a multicast packet 7603 * and we don't send icmp errors in response to 7604 * multicast, just drop the packet and give up sooner. 7605 */ 7606 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 7607 freemsg(first_mp); 7608 } 7609 return; 7610 } 7611 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_upq != NULL); 7612 7613 first_connp = connp; 7614 7615 CONN_INC_REF(connp); 7616 connp = connp->conn_next; 7617 for (;;) { 7618 while (connp != NULL) { 7619 if (IPCL_UDP_MATCH_V6(connp, dstport, 7620 ipv6_all_zeros, srcport, v6src) && 7621 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7622 (!is_system_labeled() || 7623 tsol_receive_local(mp, &dst, IPV4_VERSION, 7624 shared_addr, connp))) 7625 break; 7626 connp = connp->conn_next; 7627 } 7628 /* 7629 * Just copy the data part alone. The mctl part is 7630 * needed just for verifying policy and it is never 7631 * sent up. 7632 */ 7633 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7634 ((mp1 = copymsg(mp)) == NULL))) { 7635 /* 7636 * No more intested clients or memory 7637 * allocation failed 7638 */ 7639 connp = first_connp; 7640 break; 7641 } 7642 if (first_mp != NULL) { 7643 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7644 ipsec_info_type == IPSEC_IN); 7645 first_mp1 = ipsec_in_tag(first_mp, NULL, 7646 ipst->ips_netstack); 7647 if (first_mp1 == NULL) { 7648 freemsg(mp1); 7649 connp = first_connp; 7650 break; 7651 } 7652 } else { 7653 first_mp1 = NULL; 7654 } 7655 CONN_INC_REF(connp); 7656 mutex_exit(&connfp->connf_lock); 7657 /* 7658 * IPQoS notes: We don't send the packet for policy 7659 * processing here, will do it for the last one (below). 7660 * i.e. we do it per-packet now, but if we do policy 7661 * processing per-conn, then we would need to do it 7662 * here too. 7663 */ 7664 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7665 ipha, flags, recv_ill, B_FALSE); 7666 mutex_enter(&connfp->connf_lock); 7667 /* Follow the next pointer before releasing the conn. */ 7668 next_connp = connp->conn_next; 7669 CONN_DEC_REF(connp); 7670 connp = next_connp; 7671 } 7672 7673 /* Last one. Send it upstream. */ 7674 mutex_exit(&connfp->connf_lock); 7675 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7676 recv_ill, ip_policy); 7677 CONN_DEC_REF(connp); 7678 } 7679 7680 /* 7681 * Complete the ip_wput header so that it 7682 * is possible to generate ICMP 7683 * errors. 7684 */ 7685 int 7686 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst) 7687 { 7688 ire_t *ire; 7689 7690 if (ipha->ipha_src == INADDR_ANY) { 7691 ire = ire_lookup_local(zoneid, ipst); 7692 if (ire == NULL) { 7693 ip1dbg(("ip_hdr_complete: no source IRE\n")); 7694 return (1); 7695 } 7696 ipha->ipha_src = ire->ire_addr; 7697 ire_refrele(ire); 7698 } 7699 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 7700 ipha->ipha_hdr_checksum = 0; 7701 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 7702 return (0); 7703 } 7704 7705 /* 7706 * Nobody should be sending 7707 * packets up this stream 7708 */ 7709 static void 7710 ip_lrput(queue_t *q, mblk_t *mp) 7711 { 7712 mblk_t *mp1; 7713 7714 switch (mp->b_datap->db_type) { 7715 case M_FLUSH: 7716 /* Turn around */ 7717 if (*mp->b_rptr & FLUSHW) { 7718 *mp->b_rptr &= ~FLUSHR; 7719 qreply(q, mp); 7720 return; 7721 } 7722 break; 7723 } 7724 /* Could receive messages that passed through ar_rput */ 7725 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 7726 mp1->b_prev = mp1->b_next = NULL; 7727 freemsg(mp); 7728 } 7729 7730 /* Nobody should be sending packets down this stream */ 7731 /* ARGSUSED */ 7732 void 7733 ip_lwput(queue_t *q, mblk_t *mp) 7734 { 7735 freemsg(mp); 7736 } 7737 7738 /* 7739 * Move the first hop in any source route to ipha_dst and remove that part of 7740 * the source route. Called by other protocols. Errors in option formatting 7741 * are ignored - will be handled by ip_wput_options Return the final 7742 * destination (either ipha_dst or the last entry in a source route.) 7743 */ 7744 ipaddr_t 7745 ip_massage_options(ipha_t *ipha, netstack_t *ns) 7746 { 7747 ipoptp_t opts; 7748 uchar_t *opt; 7749 uint8_t optval; 7750 uint8_t optlen; 7751 ipaddr_t dst; 7752 int i; 7753 ire_t *ire; 7754 ip_stack_t *ipst = ns->netstack_ip; 7755 7756 ip2dbg(("ip_massage_options\n")); 7757 dst = ipha->ipha_dst; 7758 for (optval = ipoptp_first(&opts, ipha); 7759 optval != IPOPT_EOL; 7760 optval = ipoptp_next(&opts)) { 7761 opt = opts.ipoptp_cur; 7762 switch (optval) { 7763 uint8_t off; 7764 case IPOPT_SSRR: 7765 case IPOPT_LSRR: 7766 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 7767 ip1dbg(("ip_massage_options: bad src route\n")); 7768 break; 7769 } 7770 optlen = opts.ipoptp_len; 7771 off = opt[IPOPT_OFFSET]; 7772 off--; 7773 redo_srr: 7774 if (optlen < IP_ADDR_LEN || 7775 off > optlen - IP_ADDR_LEN) { 7776 /* End of source route */ 7777 ip1dbg(("ip_massage_options: end of SR\n")); 7778 break; 7779 } 7780 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 7781 ip1dbg(("ip_massage_options: next hop 0x%x\n", 7782 ntohl(dst))); 7783 /* 7784 * Check if our address is present more than 7785 * once as consecutive hops in source route. 7786 * XXX verify per-interface ip_forwarding 7787 * for source route? 7788 */ 7789 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 7790 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 7791 if (ire != NULL) { 7792 ire_refrele(ire); 7793 off += IP_ADDR_LEN; 7794 goto redo_srr; 7795 } 7796 if (dst == htonl(INADDR_LOOPBACK)) { 7797 ip1dbg(("ip_massage_options: loopback addr in " 7798 "source route!\n")); 7799 break; 7800 } 7801 /* 7802 * Update ipha_dst to be the first hop and remove the 7803 * first hop from the source route (by overwriting 7804 * part of the option with NOP options). 7805 */ 7806 ipha->ipha_dst = dst; 7807 /* Put the last entry in dst */ 7808 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 7809 3; 7810 bcopy(&opt[off], &dst, IP_ADDR_LEN); 7811 7812 ip1dbg(("ip_massage_options: last hop 0x%x\n", 7813 ntohl(dst))); 7814 /* Move down and overwrite */ 7815 opt[IP_ADDR_LEN] = opt[0]; 7816 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 7817 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 7818 for (i = 0; i < IP_ADDR_LEN; i++) 7819 opt[i] = IPOPT_NOP; 7820 break; 7821 } 7822 } 7823 return (dst); 7824 } 7825 7826 /* 7827 * Return the network mask 7828 * associated with the specified address. 7829 */ 7830 ipaddr_t 7831 ip_net_mask(ipaddr_t addr) 7832 { 7833 uchar_t *up = (uchar_t *)&addr; 7834 ipaddr_t mask = 0; 7835 uchar_t *maskp = (uchar_t *)&mask; 7836 7837 #if defined(__i386) || defined(__amd64) 7838 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 7839 #endif 7840 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 7841 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 7842 #endif 7843 if (CLASSD(addr)) { 7844 maskp[0] = 0xF0; 7845 return (mask); 7846 } 7847 7848 /* We assume Class E default netmask to be 32 */ 7849 if (CLASSE(addr)) 7850 return (0xffffffffU); 7851 7852 if (addr == 0) 7853 return (0); 7854 maskp[0] = 0xFF; 7855 if ((up[0] & 0x80) == 0) 7856 return (mask); 7857 7858 maskp[1] = 0xFF; 7859 if ((up[0] & 0xC0) == 0x80) 7860 return (mask); 7861 7862 maskp[2] = 0xFF; 7863 if ((up[0] & 0xE0) == 0xC0) 7864 return (mask); 7865 7866 /* Otherwise return no mask */ 7867 return ((ipaddr_t)0); 7868 } 7869 7870 /* 7871 * Helper ill lookup function used by IPsec. 7872 */ 7873 ill_t * 7874 ip_grab_ill(mblk_t *first_mp, int ifindex, boolean_t isv6, ip_stack_t *ipst) 7875 { 7876 ill_t *ret_ill; 7877 7878 ASSERT(ifindex != 0); 7879 7880 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL, 7881 ipst); 7882 if (ret_ill == NULL) { 7883 if (isv6) { 7884 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); 7885 ip1dbg(("ip_grab_ill (IPv6): bad ifindex %d.\n", 7886 ifindex)); 7887 } else { 7888 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 7889 ip1dbg(("ip_grab_ill (IPv4): bad ifindex %d.\n", 7890 ifindex)); 7891 } 7892 freemsg(first_mp); 7893 return (NULL); 7894 } 7895 return (ret_ill); 7896 } 7897 7898 /* 7899 * IPv4 - 7900 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 7901 * out a packet to a destination address for which we do not have specific 7902 * (or sufficient) routing information. 7903 * 7904 * NOTE : These are the scopes of some of the variables that point at IRE, 7905 * which needs to be followed while making any future modifications 7906 * to avoid memory leaks. 7907 * 7908 * - ire and sire are the entries looked up initially by 7909 * ire_ftable_lookup. 7910 * - ipif_ire is used to hold the interface ire associated with 7911 * the new cache ire. But it's scope is limited, so we always REFRELE 7912 * it before branching out to error paths. 7913 * - save_ire is initialized before ire_create, so that ire returned 7914 * by ire_create will not over-write the ire. We REFRELE save_ire 7915 * before breaking out of the switch. 7916 * 7917 * Thus on failures, we have to REFRELE only ire and sire, if they 7918 * are not NULL. 7919 */ 7920 void 7921 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, conn_t *connp, 7922 zoneid_t zoneid, ip_stack_t *ipst) 7923 { 7924 areq_t *areq; 7925 ipaddr_t gw = 0; 7926 ire_t *ire = NULL; 7927 mblk_t *res_mp; 7928 ipaddr_t *addrp; 7929 ipaddr_t nexthop_addr; 7930 ipif_t *src_ipif = NULL; 7931 ill_t *dst_ill = NULL; 7932 ipha_t *ipha; 7933 ire_t *sire = NULL; 7934 mblk_t *first_mp; 7935 ire_t *save_ire; 7936 ushort_t ire_marks = 0; 7937 boolean_t mctl_present; 7938 ipsec_out_t *io; 7939 mblk_t *saved_mp; 7940 ire_t *first_sire = NULL; 7941 mblk_t *copy_mp = NULL; 7942 mblk_t *xmit_mp = NULL; 7943 ipaddr_t save_dst; 7944 uint32_t multirt_flags = 7945 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 7946 boolean_t multirt_is_resolvable; 7947 boolean_t multirt_resolve_next; 7948 boolean_t unspec_src; 7949 boolean_t ip_nexthop = B_FALSE; 7950 tsol_ire_gw_secattr_t *attrp = NULL; 7951 tsol_gcgrp_t *gcgrp = NULL; 7952 tsol_gcgrp_addr_t ga; 7953 7954 if (ip_debug > 2) { 7955 /* ip1dbg */ 7956 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 7957 } 7958 7959 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7960 if (mctl_present) { 7961 io = (ipsec_out_t *)first_mp->b_rptr; 7962 ASSERT(io->ipsec_out_type == IPSEC_OUT); 7963 ASSERT(zoneid == io->ipsec_out_zoneid); 7964 ASSERT(zoneid != ALL_ZONES); 7965 } 7966 7967 ipha = (ipha_t *)mp->b_rptr; 7968 7969 /* All multicast lookups come through ip_newroute_ipif() */ 7970 if (CLASSD(dst)) { 7971 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 7972 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 7973 freemsg(first_mp); 7974 return; 7975 } 7976 7977 if (mctl_present && io->ipsec_out_ip_nexthop) { 7978 ip_nexthop = B_TRUE; 7979 nexthop_addr = io->ipsec_out_nexthop_addr; 7980 } 7981 /* 7982 * If this IRE is created for forwarding or it is not for 7983 * traffic for congestion controlled protocols, mark it as temporary. 7984 */ 7985 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 7986 ire_marks |= IRE_MARK_TEMPORARY; 7987 7988 /* 7989 * Get what we can from ire_ftable_lookup which will follow an IRE 7990 * chain until it gets the most specific information available. 7991 * For example, we know that there is no IRE_CACHE for this dest, 7992 * but there may be an IRE_OFFSUBNET which specifies a gateway. 7993 * ire_ftable_lookup will look up the gateway, etc. 7994 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 7995 * to the destination, of equal netmask length in the forward table, 7996 * will be recursively explored. If no information is available 7997 * for the final gateway of that route, we force the returned ire 7998 * to be equal to sire using MATCH_IRE_PARENT. 7999 * At least, in this case we have a starting point (in the buckets) 8000 * to look for other routes to the destination in the forward table. 8001 * This is actually used only for multirouting, where a list 8002 * of routes has to be processed in sequence. 8003 * 8004 * In the process of coming up with the most specific information, 8005 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry 8006 * for the gateway (i.e., one for which the ire_nce->nce_state is 8007 * not yet ND_REACHABLE, and is in the middle of arp resolution). 8008 * Two caveats when handling incomplete ire's in ip_newroute: 8009 * - we should be careful when accessing its ire_nce (specifically 8010 * the nce_res_mp) ast it might change underneath our feet, and, 8011 * - not all legacy code path callers are prepared to handle 8012 * incomplete ire's, so we should not create/add incomplete 8013 * ire_cache entries here. (See discussion about temporary solution 8014 * further below). 8015 * 8016 * In order to minimize packet dropping, and to preserve existing 8017 * behavior, we treat this case as if there were no IRE_CACHE for the 8018 * gateway, and instead use the IF_RESOLVER ire to send out 8019 * another request to ARP (this is achieved by passing the 8020 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the 8021 * arp response comes back in ip_wput_nondata, we will create 8022 * a per-dst ire_cache that has an ND_COMPLETE ire. 8023 * 8024 * Note that this is a temporary solution; the correct solution is 8025 * to create an incomplete per-dst ire_cache entry, and send the 8026 * packet out when the gw's nce is resolved. In order to achieve this, 8027 * all packet processing must have been completed prior to calling 8028 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need 8029 * to be modified to accomodate this solution. 8030 */ 8031 if (ip_nexthop) { 8032 /* 8033 * The first time we come here, we look for an IRE_INTERFACE 8034 * entry for the specified nexthop, set the dst to be the 8035 * nexthop address and create an IRE_CACHE entry for the 8036 * nexthop. The next time around, we are able to find an 8037 * IRE_CACHE entry for the nexthop, set the gateway to be the 8038 * nexthop address and create an IRE_CACHE entry for the 8039 * destination address via the specified nexthop. 8040 */ 8041 ire = ire_cache_lookup(nexthop_addr, zoneid, 8042 msg_getlabel(mp), ipst); 8043 if (ire != NULL) { 8044 gw = nexthop_addr; 8045 ire_marks |= IRE_MARK_PRIVATE_ADDR; 8046 } else { 8047 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 8048 IRE_INTERFACE, NULL, NULL, zoneid, 0, 8049 msg_getlabel(mp), 8050 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 8051 ipst); 8052 if (ire != NULL) { 8053 dst = nexthop_addr; 8054 } 8055 } 8056 } else { 8057 ire = ire_ftable_lookup(dst, 0, 0, 0, 8058 NULL, &sire, zoneid, 0, msg_getlabel(mp), 8059 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 8060 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | 8061 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE, 8062 ipst); 8063 } 8064 8065 ip3dbg(("ip_newroute: ire_ftable_lookup() " 8066 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 8067 8068 /* 8069 * This loop is run only once in most cases. 8070 * We loop to resolve further routes only when the destination 8071 * can be reached through multiple RTF_MULTIRT-flagged ires. 8072 */ 8073 do { 8074 /* Clear the previous iteration's values */ 8075 if (src_ipif != NULL) { 8076 ipif_refrele(src_ipif); 8077 src_ipif = NULL; 8078 } 8079 if (dst_ill != NULL) { 8080 ill_refrele(dst_ill); 8081 dst_ill = NULL; 8082 } 8083 8084 multirt_resolve_next = B_FALSE; 8085 /* 8086 * We check if packets have to be multirouted. 8087 * In this case, given the current <ire, sire> couple, 8088 * we look for the next suitable <ire, sire>. 8089 * This check is done in ire_multirt_lookup(), 8090 * which applies various criteria to find the next route 8091 * to resolve. ire_multirt_lookup() leaves <ire, sire> 8092 * unchanged if it detects it has not been tried yet. 8093 */ 8094 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8095 ip3dbg(("ip_newroute: starting next_resolution " 8096 "with first_mp %p, tag %d\n", 8097 (void *)first_mp, 8098 MULTIRT_DEBUG_TAGGED(first_mp))); 8099 8100 ASSERT(sire != NULL); 8101 multirt_is_resolvable = 8102 ire_multirt_lookup(&ire, &sire, multirt_flags, 8103 msg_getlabel(mp), ipst); 8104 8105 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 8106 "ire %p, sire %p\n", 8107 multirt_is_resolvable, 8108 (void *)ire, (void *)sire)); 8109 8110 if (!multirt_is_resolvable) { 8111 /* 8112 * No more multirt route to resolve; give up 8113 * (all routes resolved or no more 8114 * resolvable routes). 8115 */ 8116 if (ire != NULL) { 8117 ire_refrele(ire); 8118 ire = NULL; 8119 } 8120 } else { 8121 ASSERT(sire != NULL); 8122 ASSERT(ire != NULL); 8123 /* 8124 * We simply use first_sire as a flag that 8125 * indicates if a resolvable multirt route 8126 * has already been found. 8127 * If it is not the case, we may have to send 8128 * an ICMP error to report that the 8129 * destination is unreachable. 8130 * We do not IRE_REFHOLD first_sire. 8131 */ 8132 if (first_sire == NULL) { 8133 first_sire = sire; 8134 } 8135 } 8136 } 8137 if (ire == NULL) { 8138 if (ip_debug > 3) { 8139 /* ip2dbg */ 8140 pr_addr_dbg("ip_newroute: " 8141 "can't resolve %s\n", AF_INET, &dst); 8142 } 8143 ip3dbg(("ip_newroute: " 8144 "ire %p, sire %p, first_sire %p\n", 8145 (void *)ire, (void *)sire, (void *)first_sire)); 8146 8147 if (sire != NULL) { 8148 ire_refrele(sire); 8149 sire = NULL; 8150 } 8151 8152 if (first_sire != NULL) { 8153 /* 8154 * At least one multirt route has been found 8155 * in the same call to ip_newroute(); 8156 * there is no need to report an ICMP error. 8157 * first_sire was not IRE_REFHOLDed. 8158 */ 8159 MULTIRT_DEBUG_UNTAG(first_mp); 8160 freemsg(first_mp); 8161 return; 8162 } 8163 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 8164 RTA_DST, ipst); 8165 goto icmp_err_ret; 8166 } 8167 8168 /* 8169 * Verify that the returned IRE does not have either 8170 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 8171 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 8172 */ 8173 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 8174 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 8175 goto icmp_err_ret; 8176 } 8177 /* 8178 * Increment the ire_ob_pkt_count field for ire if it is an 8179 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 8180 * increment the same for the parent IRE, sire, if it is some 8181 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST) 8182 */ 8183 if ((ire->ire_type & IRE_INTERFACE) != 0) { 8184 UPDATE_OB_PKT_COUNT(ire); 8185 ire->ire_last_used_time = lbolt; 8186 } 8187 8188 if (sire != NULL) { 8189 gw = sire->ire_gateway_addr; 8190 ASSERT((sire->ire_type & (IRE_CACHETABLE | 8191 IRE_INTERFACE)) == 0); 8192 UPDATE_OB_PKT_COUNT(sire); 8193 sire->ire_last_used_time = lbolt; 8194 } 8195 /* 8196 * We have a route to reach the destination. Find the 8197 * appropriate ill, then get a source address using 8198 * ipif_select_source(). 8199 * 8200 * If we are here trying to create an IRE_CACHE for an offlink 8201 * destination and have an IRE_CACHE entry for VNI, then use 8202 * ire_stq instead since VNI's queue is a black hole. 8203 */ 8204 if ((ire->ire_type == IRE_CACHE) && 8205 IS_VNI(ire->ire_ipif->ipif_ill)) { 8206 dst_ill = ire->ire_stq->q_ptr; 8207 ill_refhold(dst_ill); 8208 } else { 8209 ill_t *ill = ire->ire_ipif->ipif_ill; 8210 8211 if (IS_IPMP(ill)) { 8212 dst_ill = 8213 ipmp_illgrp_hold_next_ill(ill->ill_grp); 8214 } else { 8215 dst_ill = ill; 8216 ill_refhold(dst_ill); 8217 } 8218 } 8219 8220 if (dst_ill == NULL) { 8221 if (ip_debug > 2) { 8222 pr_addr_dbg("ip_newroute: no dst " 8223 "ill for dst %s\n", AF_INET, &dst); 8224 } 8225 goto icmp_err_ret; 8226 } 8227 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 8228 8229 /* 8230 * Pick the best source address from dst_ill. 8231 * 8232 * 1) Try to pick the source address from the destination 8233 * route. Clustering assumes that when we have multiple 8234 * prefixes hosted on an interface, the prefix of the 8235 * source address matches the prefix of the destination 8236 * route. We do this only if the address is not 8237 * DEPRECATED. 8238 * 8239 * 2) If the conn is in a different zone than the ire, we 8240 * need to pick a source address from the right zone. 8241 */ 8242 ASSERT(src_ipif == NULL); 8243 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 8244 /* 8245 * The RTF_SETSRC flag is set in the parent ire (sire). 8246 * Check that the ipif matching the requested source 8247 * address still exists. 8248 */ 8249 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 8250 zoneid, NULL, NULL, NULL, NULL, ipst); 8251 } 8252 8253 unspec_src = (connp != NULL && connp->conn_unspec_src); 8254 8255 if (src_ipif == NULL && 8256 (!unspec_src || ipha->ipha_src != INADDR_ANY)) { 8257 ire_marks |= IRE_MARK_USESRC_CHECK; 8258 if (!IS_UNDER_IPMP(ire->ire_ipif->ipif_ill) && 8259 IS_IPMP(ire->ire_ipif->ipif_ill) || 8260 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 8261 (connp != NULL && ire->ire_zoneid != zoneid && 8262 ire->ire_zoneid != ALL_ZONES) || 8263 (dst_ill->ill_usesrc_ifindex != 0)) { 8264 /* 8265 * If the destination is reachable via a 8266 * given gateway, the selected source address 8267 * should be in the same subnet as the gateway. 8268 * Otherwise, the destination is not reachable. 8269 * 8270 * If there are no interfaces on the same subnet 8271 * as the destination, ipif_select_source gives 8272 * first non-deprecated interface which might be 8273 * on a different subnet than the gateway. 8274 * This is not desirable. Hence pass the dst_ire 8275 * source address to ipif_select_source. 8276 * It is sure that the destination is reachable 8277 * with the dst_ire source address subnet. 8278 * So passing dst_ire source address to 8279 * ipif_select_source will make sure that the 8280 * selected source will be on the same subnet 8281 * as dst_ire source address. 8282 */ 8283 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 8284 8285 src_ipif = ipif_select_source(dst_ill, saddr, 8286 zoneid); 8287 if (src_ipif == NULL) { 8288 if (ip_debug > 2) { 8289 pr_addr_dbg("ip_newroute: " 8290 "no src for dst %s ", 8291 AF_INET, &dst); 8292 printf("on interface %s\n", 8293 dst_ill->ill_name); 8294 } 8295 goto icmp_err_ret; 8296 } 8297 } else { 8298 src_ipif = ire->ire_ipif; 8299 ASSERT(src_ipif != NULL); 8300 /* hold src_ipif for uniformity */ 8301 ipif_refhold(src_ipif); 8302 } 8303 } 8304 8305 /* 8306 * Assign a source address while we have the conn. 8307 * We can't have ip_wput_ire pick a source address when the 8308 * packet returns from arp since we need to look at 8309 * conn_unspec_src and conn_zoneid, and we lose the conn when 8310 * going through arp. 8311 * 8312 * NOTE : ip_newroute_v6 does not have this piece of code as 8313 * it uses ip6i to store this information. 8314 */ 8315 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 8316 ipha->ipha_src = src_ipif->ipif_src_addr; 8317 8318 if (ip_debug > 3) { 8319 /* ip2dbg */ 8320 pr_addr_dbg("ip_newroute: first hop %s\n", 8321 AF_INET, &gw); 8322 } 8323 ip2dbg(("\tire type %s (%d)\n", 8324 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 8325 8326 /* 8327 * The TTL of multirouted packets is bounded by the 8328 * ip_multirt_ttl ndd variable. 8329 */ 8330 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8331 /* Force TTL of multirouted packets */ 8332 if ((ipst->ips_ip_multirt_ttl > 0) && 8333 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 8334 ip2dbg(("ip_newroute: forcing multirt TTL " 8335 "to %d (was %d), dst 0x%08x\n", 8336 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 8337 ntohl(sire->ire_addr))); 8338 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 8339 } 8340 } 8341 /* 8342 * At this point in ip_newroute(), ire is either the 8343 * IRE_CACHE of the next-hop gateway for an off-subnet 8344 * destination or an IRE_INTERFACE type that should be used 8345 * to resolve an on-subnet destination or an on-subnet 8346 * next-hop gateway. 8347 * 8348 * In the IRE_CACHE case, we have the following : 8349 * 8350 * 1) src_ipif - used for getting a source address. 8351 * 8352 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8353 * means packets using this IRE_CACHE will go out on 8354 * dst_ill. 8355 * 8356 * 3) The IRE sire will point to the prefix that is the 8357 * longest matching route for the destination. These 8358 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST. 8359 * 8360 * The newly created IRE_CACHE entry for the off-subnet 8361 * destination is tied to both the prefix route and the 8362 * interface route used to resolve the next-hop gateway 8363 * via the ire_phandle and ire_ihandle fields, 8364 * respectively. 8365 * 8366 * In the IRE_INTERFACE case, we have the following : 8367 * 8368 * 1) src_ipif - used for getting a source address. 8369 * 8370 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8371 * means packets using the IRE_CACHE that we will build 8372 * here will go out on dst_ill. 8373 * 8374 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 8375 * to be created will only be tied to the IRE_INTERFACE 8376 * that was derived from the ire_ihandle field. 8377 * 8378 * If sire is non-NULL, it means the destination is 8379 * off-link and we will first create the IRE_CACHE for the 8380 * gateway. Next time through ip_newroute, we will create 8381 * the IRE_CACHE for the final destination as described 8382 * above. 8383 * 8384 * In both cases, after the current resolution has been 8385 * completed (or possibly initialised, in the IRE_INTERFACE 8386 * case), the loop may be re-entered to attempt the resolution 8387 * of another RTF_MULTIRT route. 8388 * 8389 * When an IRE_CACHE entry for the off-subnet destination is 8390 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 8391 * for further processing in emission loops. 8392 */ 8393 save_ire = ire; 8394 switch (ire->ire_type) { 8395 case IRE_CACHE: { 8396 ire_t *ipif_ire; 8397 8398 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); 8399 if (gw == 0) 8400 gw = ire->ire_gateway_addr; 8401 /* 8402 * We need 3 ire's to create a new cache ire for an 8403 * off-link destination from the cache ire of the 8404 * gateway. 8405 * 8406 * 1. The prefix ire 'sire' (Note that this does 8407 * not apply to the conn_nexthop_set case) 8408 * 2. The cache ire of the gateway 'ire' 8409 * 3. The interface ire 'ipif_ire' 8410 * 8411 * We have (1) and (2). We lookup (3) below. 8412 * 8413 * If there is no interface route to the gateway, 8414 * it is a race condition, where we found the cache 8415 * but the interface route has been deleted. 8416 */ 8417 if (ip_nexthop) { 8418 ipif_ire = ire_ihandle_lookup_onlink(ire); 8419 } else { 8420 ipif_ire = 8421 ire_ihandle_lookup_offlink(ire, sire); 8422 } 8423 if (ipif_ire == NULL) { 8424 ip1dbg(("ip_newroute: " 8425 "ire_ihandle_lookup_offlink failed\n")); 8426 goto icmp_err_ret; 8427 } 8428 8429 /* 8430 * Check cached gateway IRE for any security 8431 * attributes; if found, associate the gateway 8432 * credentials group to the destination IRE. 8433 */ 8434 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 8435 mutex_enter(&attrp->igsa_lock); 8436 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 8437 GCGRP_REFHOLD(gcgrp); 8438 mutex_exit(&attrp->igsa_lock); 8439 } 8440 8441 /* 8442 * XXX For the source of the resolver mp, 8443 * we are using the same DL_UNITDATA_REQ 8444 * (from save_ire->ire_nce->nce_res_mp) 8445 * though the save_ire is not pointing at the same ill. 8446 * This is incorrect. We need to send it up to the 8447 * resolver to get the right res_mp. For ethernets 8448 * this may be okay (ill_type == DL_ETHER). 8449 */ 8450 8451 ire = ire_create( 8452 (uchar_t *)&dst, /* dest address */ 8453 (uchar_t *)&ip_g_all_ones, /* mask */ 8454 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8455 (uchar_t *)&gw, /* gateway address */ 8456 &save_ire->ire_max_frag, 8457 save_ire->ire_nce, /* src nce */ 8458 dst_ill->ill_rq, /* recv-from queue */ 8459 dst_ill->ill_wq, /* send-to queue */ 8460 IRE_CACHE, /* IRE type */ 8461 src_ipif, 8462 (sire != NULL) ? 8463 sire->ire_mask : 0, /* Parent mask */ 8464 (sire != NULL) ? 8465 sire->ire_phandle : 0, /* Parent handle */ 8466 ipif_ire->ire_ihandle, /* Interface handle */ 8467 (sire != NULL) ? (sire->ire_flags & 8468 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 8469 (sire != NULL) ? 8470 &(sire->ire_uinfo) : &(save_ire->ire_uinfo), 8471 NULL, 8472 gcgrp, 8473 ipst); 8474 8475 if (ire == NULL) { 8476 if (gcgrp != NULL) { 8477 GCGRP_REFRELE(gcgrp); 8478 gcgrp = NULL; 8479 } 8480 ire_refrele(ipif_ire); 8481 ire_refrele(save_ire); 8482 break; 8483 } 8484 8485 /* reference now held by IRE */ 8486 gcgrp = NULL; 8487 8488 ire->ire_marks |= ire_marks; 8489 8490 /* 8491 * Prevent sire and ipif_ire from getting deleted. 8492 * The newly created ire is tied to both of them via 8493 * the phandle and ihandle respectively. 8494 */ 8495 if (sire != NULL) { 8496 IRB_REFHOLD(sire->ire_bucket); 8497 /* Has it been removed already ? */ 8498 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 8499 IRB_REFRELE(sire->ire_bucket); 8500 ire_refrele(ipif_ire); 8501 ire_refrele(save_ire); 8502 break; 8503 } 8504 } 8505 8506 IRB_REFHOLD(ipif_ire->ire_bucket); 8507 /* Has it been removed already ? */ 8508 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 8509 IRB_REFRELE(ipif_ire->ire_bucket); 8510 if (sire != NULL) 8511 IRB_REFRELE(sire->ire_bucket); 8512 ire_refrele(ipif_ire); 8513 ire_refrele(save_ire); 8514 break; 8515 } 8516 8517 xmit_mp = first_mp; 8518 /* 8519 * In the case of multirouting, a copy 8520 * of the packet is done before its sending. 8521 * The copy is used to attempt another 8522 * route resolution, in a next loop. 8523 */ 8524 if (ire->ire_flags & RTF_MULTIRT) { 8525 copy_mp = copymsg(first_mp); 8526 if (copy_mp != NULL) { 8527 xmit_mp = copy_mp; 8528 MULTIRT_DEBUG_TAG(first_mp); 8529 } 8530 } 8531 8532 ire_add_then_send(q, ire, xmit_mp); 8533 ire_refrele(save_ire); 8534 8535 /* Assert that sire is not deleted yet. */ 8536 if (sire != NULL) { 8537 ASSERT(sire->ire_ptpn != NULL); 8538 IRB_REFRELE(sire->ire_bucket); 8539 } 8540 8541 /* Assert that ipif_ire is not deleted yet. */ 8542 ASSERT(ipif_ire->ire_ptpn != NULL); 8543 IRB_REFRELE(ipif_ire->ire_bucket); 8544 ire_refrele(ipif_ire); 8545 8546 /* 8547 * If copy_mp is not NULL, multirouting was 8548 * requested. We loop to initiate a next 8549 * route resolution attempt, starting from sire. 8550 */ 8551 if (copy_mp != NULL) { 8552 /* 8553 * Search for the next unresolved 8554 * multirt route. 8555 */ 8556 copy_mp = NULL; 8557 ipif_ire = NULL; 8558 ire = NULL; 8559 multirt_resolve_next = B_TRUE; 8560 continue; 8561 } 8562 if (sire != NULL) 8563 ire_refrele(sire); 8564 ipif_refrele(src_ipif); 8565 ill_refrele(dst_ill); 8566 return; 8567 } 8568 case IRE_IF_NORESOLVER: { 8569 if (dst_ill->ill_phys_addr_length != IP_ADDR_LEN && 8570 dst_ill->ill_resolver_mp == NULL) { 8571 ip1dbg(("ip_newroute: dst_ill %p " 8572 "for IRE_IF_NORESOLVER ire %p has " 8573 "no ill_resolver_mp\n", 8574 (void *)dst_ill, (void *)ire)); 8575 break; 8576 } 8577 8578 /* 8579 * TSol note: We are creating the ire cache for the 8580 * destination 'dst'. If 'dst' is offlink, going 8581 * through the first hop 'gw', the security attributes 8582 * of 'dst' must be set to point to the gateway 8583 * credentials of gateway 'gw'. If 'dst' is onlink, it 8584 * is possible that 'dst' is a potential gateway that is 8585 * referenced by some route that has some security 8586 * attributes. Thus in the former case, we need to do a 8587 * gcgrp_lookup of 'gw' while in the latter case we 8588 * need to do gcgrp_lookup of 'dst' itself. 8589 */ 8590 ga.ga_af = AF_INET; 8591 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, 8592 &ga.ga_addr); 8593 gcgrp = gcgrp_lookup(&ga, B_FALSE); 8594 8595 ire = ire_create( 8596 (uchar_t *)&dst, /* dest address */ 8597 (uchar_t *)&ip_g_all_ones, /* mask */ 8598 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8599 (uchar_t *)&gw, /* gateway address */ 8600 &save_ire->ire_max_frag, 8601 NULL, /* no src nce */ 8602 dst_ill->ill_rq, /* recv-from queue */ 8603 dst_ill->ill_wq, /* send-to queue */ 8604 IRE_CACHE, 8605 src_ipif, 8606 save_ire->ire_mask, /* Parent mask */ 8607 (sire != NULL) ? /* Parent handle */ 8608 sire->ire_phandle : 0, 8609 save_ire->ire_ihandle, /* Interface handle */ 8610 (sire != NULL) ? sire->ire_flags & 8611 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 8612 &(save_ire->ire_uinfo), 8613 NULL, 8614 gcgrp, 8615 ipst); 8616 8617 if (ire == NULL) { 8618 if (gcgrp != NULL) { 8619 GCGRP_REFRELE(gcgrp); 8620 gcgrp = NULL; 8621 } 8622 ire_refrele(save_ire); 8623 break; 8624 } 8625 8626 /* reference now held by IRE */ 8627 gcgrp = NULL; 8628 8629 ire->ire_marks |= ire_marks; 8630 8631 /* Prevent save_ire from getting deleted */ 8632 IRB_REFHOLD(save_ire->ire_bucket); 8633 /* Has it been removed already ? */ 8634 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8635 IRB_REFRELE(save_ire->ire_bucket); 8636 ire_refrele(save_ire); 8637 break; 8638 } 8639 8640 /* 8641 * In the case of multirouting, a copy 8642 * of the packet is made before it is sent. 8643 * The copy is used in the next 8644 * loop to attempt another resolution. 8645 */ 8646 xmit_mp = first_mp; 8647 if ((sire != NULL) && 8648 (sire->ire_flags & RTF_MULTIRT)) { 8649 copy_mp = copymsg(first_mp); 8650 if (copy_mp != NULL) { 8651 xmit_mp = copy_mp; 8652 MULTIRT_DEBUG_TAG(first_mp); 8653 } 8654 } 8655 ire_add_then_send(q, ire, xmit_mp); 8656 8657 /* Assert that it is not deleted yet. */ 8658 ASSERT(save_ire->ire_ptpn != NULL); 8659 IRB_REFRELE(save_ire->ire_bucket); 8660 ire_refrele(save_ire); 8661 8662 if (copy_mp != NULL) { 8663 /* 8664 * If we found a (no)resolver, we ignore any 8665 * trailing top priority IRE_CACHE in further 8666 * loops. This ensures that we do not omit any 8667 * (no)resolver. 8668 * This IRE_CACHE, if any, will be processed 8669 * by another thread entering ip_newroute(). 8670 * IRE_CACHE entries, if any, will be processed 8671 * by another thread entering ip_newroute(), 8672 * (upon resolver response, for instance). 8673 * This aims to force parallel multirt 8674 * resolutions as soon as a packet must be sent. 8675 * In the best case, after the tx of only one 8676 * packet, all reachable routes are resolved. 8677 * Otherwise, the resolution of all RTF_MULTIRT 8678 * routes would require several emissions. 8679 */ 8680 multirt_flags &= ~MULTIRT_CACHEGW; 8681 8682 /* 8683 * Search for the next unresolved multirt 8684 * route. 8685 */ 8686 copy_mp = NULL; 8687 save_ire = NULL; 8688 ire = NULL; 8689 multirt_resolve_next = B_TRUE; 8690 continue; 8691 } 8692 8693 /* 8694 * Don't need sire anymore 8695 */ 8696 if (sire != NULL) 8697 ire_refrele(sire); 8698 8699 ipif_refrele(src_ipif); 8700 ill_refrele(dst_ill); 8701 return; 8702 } 8703 case IRE_IF_RESOLVER: 8704 /* 8705 * We can't build an IRE_CACHE yet, but at least we 8706 * found a resolver that can help. 8707 */ 8708 res_mp = dst_ill->ill_resolver_mp; 8709 if (!OK_RESOLVER_MP(res_mp)) 8710 break; 8711 8712 /* 8713 * To be at this point in the code with a non-zero gw 8714 * means that dst is reachable through a gateway that 8715 * we have never resolved. By changing dst to the gw 8716 * addr we resolve the gateway first. 8717 * When ire_add_then_send() tries to put the IP dg 8718 * to dst, it will reenter ip_newroute() at which 8719 * time we will find the IRE_CACHE for the gw and 8720 * create another IRE_CACHE in case IRE_CACHE above. 8721 */ 8722 if (gw != INADDR_ANY) { 8723 /* 8724 * The source ipif that was determined above was 8725 * relative to the destination address, not the 8726 * gateway's. If src_ipif was not taken out of 8727 * the IRE_IF_RESOLVER entry, we'll need to call 8728 * ipif_select_source() again. 8729 */ 8730 if (src_ipif != ire->ire_ipif) { 8731 ipif_refrele(src_ipif); 8732 src_ipif = ipif_select_source(dst_ill, 8733 gw, zoneid); 8734 if (src_ipif == NULL) { 8735 if (ip_debug > 2) { 8736 pr_addr_dbg( 8737 "ip_newroute: no " 8738 "src for gw %s ", 8739 AF_INET, &gw); 8740 printf("on " 8741 "interface %s\n", 8742 dst_ill->ill_name); 8743 } 8744 goto icmp_err_ret; 8745 } 8746 } 8747 save_dst = dst; 8748 dst = gw; 8749 gw = INADDR_ANY; 8750 } 8751 8752 /* 8753 * We obtain a partial IRE_CACHE which we will pass 8754 * along with the resolver query. When the response 8755 * comes back it will be there ready for us to add. 8756 * The ire_max_frag is atomically set under the 8757 * irebucket lock in ire_add_v[46]. 8758 */ 8759 8760 ire = ire_create_mp( 8761 (uchar_t *)&dst, /* dest address */ 8762 (uchar_t *)&ip_g_all_ones, /* mask */ 8763 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8764 (uchar_t *)&gw, /* gateway address */ 8765 NULL, /* ire_max_frag */ 8766 NULL, /* no src nce */ 8767 dst_ill->ill_rq, /* recv-from queue */ 8768 dst_ill->ill_wq, /* send-to queue */ 8769 IRE_CACHE, 8770 src_ipif, /* Interface ipif */ 8771 save_ire->ire_mask, /* Parent mask */ 8772 0, 8773 save_ire->ire_ihandle, /* Interface handle */ 8774 0, /* flags if any */ 8775 &(save_ire->ire_uinfo), 8776 NULL, 8777 NULL, 8778 ipst); 8779 8780 if (ire == NULL) { 8781 ire_refrele(save_ire); 8782 break; 8783 } 8784 8785 if ((sire != NULL) && 8786 (sire->ire_flags & RTF_MULTIRT)) { 8787 copy_mp = copymsg(first_mp); 8788 if (copy_mp != NULL) 8789 MULTIRT_DEBUG_TAG(copy_mp); 8790 } 8791 8792 ire->ire_marks |= ire_marks; 8793 8794 /* 8795 * Construct message chain for the resolver 8796 * of the form: 8797 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8798 * Packet could contain a IPSEC_OUT mp. 8799 * 8800 * NOTE : ire will be added later when the response 8801 * comes back from ARP. If the response does not 8802 * come back, ARP frees the packet. For this reason, 8803 * we can't REFHOLD the bucket of save_ire to prevent 8804 * deletions. We may not be able to REFRELE the bucket 8805 * if the response never comes back. Thus, before 8806 * adding the ire, ire_add_v4 will make sure that the 8807 * interface route does not get deleted. This is the 8808 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 8809 * where we can always prevent deletions because of 8810 * the synchronous nature of adding IRES i.e 8811 * ire_add_then_send is called after creating the IRE. 8812 */ 8813 ASSERT(ire->ire_mp != NULL); 8814 ire->ire_mp->b_cont = first_mp; 8815 /* Have saved_mp handy, for cleanup if canput fails */ 8816 saved_mp = mp; 8817 mp = copyb(res_mp); 8818 if (mp == NULL) { 8819 /* Prepare for cleanup */ 8820 mp = saved_mp; /* pkt */ 8821 ire_delete(ire); /* ire_mp */ 8822 ire = NULL; 8823 ire_refrele(save_ire); 8824 if (copy_mp != NULL) { 8825 MULTIRT_DEBUG_UNTAG(copy_mp); 8826 freemsg(copy_mp); 8827 copy_mp = NULL; 8828 } 8829 break; 8830 } 8831 linkb(mp, ire->ire_mp); 8832 8833 /* 8834 * Fill in the source and dest addrs for the resolver. 8835 * NOTE: this depends on memory layouts imposed by 8836 * ill_init(). 8837 */ 8838 areq = (areq_t *)mp->b_rptr; 8839 addrp = (ipaddr_t *)((char *)areq + 8840 areq->areq_sender_addr_offset); 8841 *addrp = save_ire->ire_src_addr; 8842 8843 ire_refrele(save_ire); 8844 addrp = (ipaddr_t *)((char *)areq + 8845 areq->areq_target_addr_offset); 8846 *addrp = dst; 8847 /* Up to the resolver. */ 8848 if (canputnext(dst_ill->ill_rq) && 8849 !(dst_ill->ill_arp_closing)) { 8850 putnext(dst_ill->ill_rq, mp); 8851 ire = NULL; 8852 if (copy_mp != NULL) { 8853 /* 8854 * If we found a resolver, we ignore 8855 * any trailing top priority IRE_CACHE 8856 * in the further loops. This ensures 8857 * that we do not omit any resolver. 8858 * IRE_CACHE entries, if any, will be 8859 * processed next time we enter 8860 * ip_newroute(). 8861 */ 8862 multirt_flags &= ~MULTIRT_CACHEGW; 8863 /* 8864 * Search for the next unresolved 8865 * multirt route. 8866 */ 8867 first_mp = copy_mp; 8868 copy_mp = NULL; 8869 /* Prepare the next resolution loop. */ 8870 mp = first_mp; 8871 EXTRACT_PKT_MP(mp, first_mp, 8872 mctl_present); 8873 if (mctl_present) 8874 io = (ipsec_out_t *) 8875 first_mp->b_rptr; 8876 ipha = (ipha_t *)mp->b_rptr; 8877 8878 ASSERT(sire != NULL); 8879 8880 dst = save_dst; 8881 multirt_resolve_next = B_TRUE; 8882 continue; 8883 } 8884 8885 if (sire != NULL) 8886 ire_refrele(sire); 8887 8888 /* 8889 * The response will come back in ip_wput 8890 * with db_type IRE_DB_TYPE. 8891 */ 8892 ipif_refrele(src_ipif); 8893 ill_refrele(dst_ill); 8894 return; 8895 } else { 8896 /* Prepare for cleanup */ 8897 DTRACE_PROBE1(ip__newroute__drop, mblk_t *, 8898 mp); 8899 mp->b_cont = NULL; 8900 freeb(mp); /* areq */ 8901 /* 8902 * this is an ire that is not added to the 8903 * cache. ire_freemblk will handle the release 8904 * of any resources associated with the ire. 8905 */ 8906 ire_delete(ire); /* ire_mp */ 8907 mp = saved_mp; /* pkt */ 8908 ire = NULL; 8909 if (copy_mp != NULL) { 8910 MULTIRT_DEBUG_UNTAG(copy_mp); 8911 freemsg(copy_mp); 8912 copy_mp = NULL; 8913 } 8914 break; 8915 } 8916 default: 8917 break; 8918 } 8919 } while (multirt_resolve_next); 8920 8921 ip1dbg(("ip_newroute: dropped\n")); 8922 /* Did this packet originate externally? */ 8923 if (mp->b_prev) { 8924 mp->b_next = NULL; 8925 mp->b_prev = NULL; 8926 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 8927 } else { 8928 if (dst_ill != NULL) { 8929 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards); 8930 } else { 8931 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 8932 } 8933 } 8934 ASSERT(copy_mp == NULL); 8935 MULTIRT_DEBUG_UNTAG(first_mp); 8936 freemsg(first_mp); 8937 if (ire != NULL) 8938 ire_refrele(ire); 8939 if (sire != NULL) 8940 ire_refrele(sire); 8941 if (src_ipif != NULL) 8942 ipif_refrele(src_ipif); 8943 if (dst_ill != NULL) 8944 ill_refrele(dst_ill); 8945 return; 8946 8947 icmp_err_ret: 8948 ip1dbg(("ip_newroute: no route\n")); 8949 if (src_ipif != NULL) 8950 ipif_refrele(src_ipif); 8951 if (dst_ill != NULL) 8952 ill_refrele(dst_ill); 8953 if (sire != NULL) 8954 ire_refrele(sire); 8955 /* Did this packet originate externally? */ 8956 if (mp->b_prev) { 8957 mp->b_next = NULL; 8958 mp->b_prev = NULL; 8959 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes); 8960 q = WR(q); 8961 } else { 8962 /* 8963 * There is no outgoing ill, so just increment the 8964 * system MIB. 8965 */ 8966 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 8967 /* 8968 * Since ip_wput() isn't close to finished, we fill 8969 * in enough of the header for credible error reporting. 8970 */ 8971 if (ip_hdr_complete(ipha, zoneid, ipst)) { 8972 /* Failed */ 8973 MULTIRT_DEBUG_UNTAG(first_mp); 8974 freemsg(first_mp); 8975 if (ire != NULL) 8976 ire_refrele(ire); 8977 return; 8978 } 8979 } 8980 8981 /* 8982 * At this point we will have ire only if RTF_BLACKHOLE 8983 * or RTF_REJECT flags are set on the IRE. It will not 8984 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 8985 */ 8986 if (ire != NULL) { 8987 if (ire->ire_flags & RTF_BLACKHOLE) { 8988 ire_refrele(ire); 8989 MULTIRT_DEBUG_UNTAG(first_mp); 8990 freemsg(first_mp); 8991 return; 8992 } 8993 ire_refrele(ire); 8994 } 8995 if (ip_source_routed(ipha, ipst)) { 8996 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, 8997 zoneid, ipst); 8998 return; 8999 } 9000 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9001 } 9002 9003 ip_opt_info_t zero_info; 9004 9005 /* 9006 * IPv4 - 9007 * ip_newroute_ipif is called by ip_wput_multicast and 9008 * ip_rput_forward_multicast whenever we need to send 9009 * out a packet to a destination address for which we do not have specific 9010 * routing information. It is used when the packet will be sent out 9011 * on a specific interface. It is also called by ip_wput() when IP_BOUND_IF 9012 * socket option is set or icmp error message wants to go out on a particular 9013 * interface for a unicast packet. 9014 * 9015 * In most cases, the destination address is resolved thanks to the ipif 9016 * intrinsic resolver. However, there are some cases where the call to 9017 * ip_newroute_ipif must take into account the potential presence of 9018 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 9019 * that uses the interface. This is specified through flags, 9020 * which can be a combination of: 9021 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 9022 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 9023 * and flags. Additionally, the packet source address has to be set to 9024 * the specified address. The caller is thus expected to set this flag 9025 * if the packet has no specific source address yet. 9026 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 9027 * flag, the resulting ire will inherit the flag. All unresolved routes 9028 * to the destination must be explored in the same call to 9029 * ip_newroute_ipif(). 9030 */ 9031 static void 9032 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 9033 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop) 9034 { 9035 areq_t *areq; 9036 ire_t *ire = NULL; 9037 mblk_t *res_mp; 9038 ipaddr_t *addrp; 9039 mblk_t *first_mp; 9040 ire_t *save_ire = NULL; 9041 ipif_t *src_ipif = NULL; 9042 ushort_t ire_marks = 0; 9043 ill_t *dst_ill = NULL; 9044 ipha_t *ipha; 9045 mblk_t *saved_mp; 9046 ire_t *fire = NULL; 9047 mblk_t *copy_mp = NULL; 9048 boolean_t multirt_resolve_next; 9049 boolean_t unspec_src; 9050 ipaddr_t ipha_dst; 9051 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 9052 9053 /* 9054 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 9055 * here for uniformity 9056 */ 9057 ipif_refhold(ipif); 9058 9059 /* 9060 * This loop is run only once in most cases. 9061 * We loop to resolve further routes only when the destination 9062 * can be reached through multiple RTF_MULTIRT-flagged ires. 9063 */ 9064 do { 9065 if (dst_ill != NULL) { 9066 ill_refrele(dst_ill); 9067 dst_ill = NULL; 9068 } 9069 if (src_ipif != NULL) { 9070 ipif_refrele(src_ipif); 9071 src_ipif = NULL; 9072 } 9073 multirt_resolve_next = B_FALSE; 9074 9075 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 9076 ipif->ipif_ill->ill_name)); 9077 9078 first_mp = mp; 9079 if (DB_TYPE(mp) == M_CTL) 9080 mp = mp->b_cont; 9081 ipha = (ipha_t *)mp->b_rptr; 9082 9083 /* 9084 * Save the packet destination address, we may need it after 9085 * the packet has been consumed. 9086 */ 9087 ipha_dst = ipha->ipha_dst; 9088 9089 /* 9090 * If the interface is a pt-pt interface we look for an 9091 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 9092 * local_address and the pt-pt destination address. Otherwise 9093 * we just match the local address. 9094 * NOTE: dst could be different than ipha->ipha_dst in case 9095 * of sending igmp multicast packets over a point-to-point 9096 * connection. 9097 * Thus we must be careful enough to check ipha_dst to be a 9098 * multicast address, otherwise it will take xmit_if path for 9099 * multicast packets resulting into kernel stack overflow by 9100 * repeated calls to ip_newroute_ipif from ire_send(). 9101 */ 9102 if (CLASSD(ipha_dst) && 9103 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 9104 goto err_ret; 9105 } 9106 9107 /* 9108 * We check if an IRE_OFFSUBNET for the addr that goes through 9109 * ipif exists. We need it to determine if the RTF_SETSRC and/or 9110 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 9111 * propagate its flags to the new ire. 9112 */ 9113 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 9114 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 9115 ip2dbg(("ip_newroute_ipif: " 9116 "ipif_lookup_multi_ire(" 9117 "ipif %p, dst %08x) = fire %p\n", 9118 (void *)ipif, ntohl(dst), (void *)fire)); 9119 } 9120 9121 /* 9122 * Note: While we pick a dst_ill we are really only 9123 * interested in the ill for load spreading. The source 9124 * ipif is determined by source address selection below. 9125 */ 9126 if (IS_IPMP(ipif->ipif_ill)) { 9127 ipmp_illgrp_t *illg = ipif->ipif_ill->ill_grp; 9128 9129 if (CLASSD(ipha_dst)) 9130 dst_ill = ipmp_illgrp_hold_cast_ill(illg); 9131 else 9132 dst_ill = ipmp_illgrp_hold_next_ill(illg); 9133 } else { 9134 dst_ill = ipif->ipif_ill; 9135 ill_refhold(dst_ill); 9136 } 9137 9138 if (dst_ill == NULL) { 9139 if (ip_debug > 2) { 9140 pr_addr_dbg("ip_newroute_ipif: no dst ill " 9141 "for dst %s\n", AF_INET, &dst); 9142 } 9143 goto err_ret; 9144 } 9145 9146 /* 9147 * Pick a source address preferring non-deprecated ones. 9148 * Unlike ip_newroute, we don't do any source address 9149 * selection here since for multicast it really does not help 9150 * in inbound load spreading as in the unicast case. 9151 */ 9152 if ((flags & RTF_SETSRC) && (fire != NULL) && 9153 (fire->ire_flags & RTF_SETSRC)) { 9154 /* 9155 * As requested by flags, an IRE_OFFSUBNET was looked up 9156 * on that interface. This ire has RTF_SETSRC flag, so 9157 * the source address of the packet must be changed. 9158 * Check that the ipif matching the requested source 9159 * address still exists. 9160 */ 9161 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 9162 zoneid, NULL, NULL, NULL, NULL, ipst); 9163 } 9164 9165 unspec_src = (connp != NULL && connp->conn_unspec_src); 9166 9167 if (!IS_UNDER_IPMP(ipif->ipif_ill) && 9168 (IS_IPMP(ipif->ipif_ill) || 9169 (!ipif->ipif_isv6 && ipif->ipif_lcl_addr == INADDR_ANY) || 9170 (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_UP)) != IPIF_UP || 9171 (connp != NULL && ipif->ipif_zoneid != zoneid && 9172 ipif->ipif_zoneid != ALL_ZONES)) && 9173 (src_ipif == NULL) && 9174 (!unspec_src || ipha->ipha_src != INADDR_ANY)) { 9175 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 9176 if (src_ipif == NULL) { 9177 if (ip_debug > 2) { 9178 /* ip1dbg */ 9179 pr_addr_dbg("ip_newroute_ipif: " 9180 "no src for dst %s", 9181 AF_INET, &dst); 9182 } 9183 ip1dbg((" on interface %s\n", 9184 dst_ill->ill_name)); 9185 goto err_ret; 9186 } 9187 ipif_refrele(ipif); 9188 ipif = src_ipif; 9189 ipif_refhold(ipif); 9190 } 9191 if (src_ipif == NULL) { 9192 src_ipif = ipif; 9193 ipif_refhold(src_ipif); 9194 } 9195 9196 /* 9197 * Assign a source address while we have the conn. 9198 * We can't have ip_wput_ire pick a source address when the 9199 * packet returns from arp since conn_unspec_src might be set 9200 * and we lose the conn when going through arp. 9201 */ 9202 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 9203 ipha->ipha_src = src_ipif->ipif_src_addr; 9204 9205 /* 9206 * In the case of IP_BOUND_IF and IP_PKTINFO, it is possible 9207 * that the outgoing interface does not have an interface ire. 9208 */ 9209 if (CLASSD(ipha_dst) && (connp == NULL || 9210 connp->conn_outgoing_ill == NULL) && 9211 infop->ip_opt_ill_index == 0) { 9212 /* ipif_to_ire returns an held ire */ 9213 ire = ipif_to_ire(ipif); 9214 if (ire == NULL) 9215 goto err_ret; 9216 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 9217 goto err_ret; 9218 save_ire = ire; 9219 9220 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 9221 "flags %04x\n", 9222 (void *)ire, (void *)ipif, flags)); 9223 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9224 (fire->ire_flags & RTF_MULTIRT)) { 9225 /* 9226 * As requested by flags, an IRE_OFFSUBNET was 9227 * looked up on that interface. This ire has 9228 * RTF_MULTIRT flag, so the resolution loop will 9229 * be re-entered to resolve additional routes on 9230 * other interfaces. For that purpose, a copy of 9231 * the packet is performed at this point. 9232 */ 9233 fire->ire_last_used_time = lbolt; 9234 copy_mp = copymsg(first_mp); 9235 if (copy_mp) { 9236 MULTIRT_DEBUG_TAG(copy_mp); 9237 } 9238 } 9239 if ((flags & RTF_SETSRC) && (fire != NULL) && 9240 (fire->ire_flags & RTF_SETSRC)) { 9241 /* 9242 * As requested by flags, an IRE_OFFSUBET was 9243 * looked up on that interface. This ire has 9244 * RTF_SETSRC flag, so the source address of the 9245 * packet must be changed. 9246 */ 9247 ipha->ipha_src = fire->ire_src_addr; 9248 } 9249 } else { 9250 /* 9251 * The only ways we can come here are: 9252 * 1) IP_BOUND_IF socket option is set 9253 * 2) SO_DONTROUTE socket option is set 9254 * 3) IP_PKTINFO option is passed in as ancillary data. 9255 * In all cases, the new ire will not be added 9256 * into cache table. 9257 */ 9258 ASSERT(connp == NULL || connp->conn_dontroute || 9259 connp->conn_outgoing_ill != NULL || 9260 infop->ip_opt_ill_index != 0); 9261 ire_marks |= IRE_MARK_NOADD; 9262 } 9263 9264 switch (ipif->ipif_net_type) { 9265 case IRE_IF_NORESOLVER: { 9266 /* We have what we need to build an IRE_CACHE. */ 9267 9268 if ((dst_ill->ill_phys_addr_length != IP_ADDR_LEN) && 9269 (dst_ill->ill_resolver_mp == NULL)) { 9270 ip1dbg(("ip_newroute_ipif: dst_ill %p " 9271 "for IRE_IF_NORESOLVER ire %p has " 9272 "no ill_resolver_mp\n", 9273 (void *)dst_ill, (void *)ire)); 9274 break; 9275 } 9276 9277 /* 9278 * The new ire inherits the IRE_OFFSUBNET flags 9279 * and source address, if this was requested. 9280 */ 9281 ire = ire_create( 9282 (uchar_t *)&dst, /* dest address */ 9283 (uchar_t *)&ip_g_all_ones, /* mask */ 9284 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9285 NULL, /* gateway address */ 9286 &ipif->ipif_mtu, 9287 NULL, /* no src nce */ 9288 dst_ill->ill_rq, /* recv-from queue */ 9289 dst_ill->ill_wq, /* send-to queue */ 9290 IRE_CACHE, 9291 src_ipif, 9292 (save_ire != NULL ? save_ire->ire_mask : 0), 9293 (fire != NULL) ? /* Parent handle */ 9294 fire->ire_phandle : 0, 9295 (save_ire != NULL) ? /* Interface handle */ 9296 save_ire->ire_ihandle : 0, 9297 (fire != NULL) ? 9298 (fire->ire_flags & 9299 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9300 (save_ire == NULL ? &ire_uinfo_null : 9301 &save_ire->ire_uinfo), 9302 NULL, 9303 NULL, 9304 ipst); 9305 9306 if (ire == NULL) { 9307 if (save_ire != NULL) 9308 ire_refrele(save_ire); 9309 break; 9310 } 9311 9312 ire->ire_marks |= ire_marks; 9313 9314 /* 9315 * If IRE_MARK_NOADD is set then we need to convert 9316 * the max_fragp to a useable value now. This is 9317 * normally done in ire_add_v[46]. We also need to 9318 * associate the ire with an nce (normally would be 9319 * done in ip_wput_nondata()). 9320 * 9321 * Note that IRE_MARK_NOADD packets created here 9322 * do not have a non-null ire_mp pointer. The null 9323 * value of ire_bucket indicates that they were 9324 * never added. 9325 */ 9326 if (ire->ire_marks & IRE_MARK_NOADD) { 9327 uint_t max_frag; 9328 9329 max_frag = *ire->ire_max_fragp; 9330 ire->ire_max_fragp = NULL; 9331 ire->ire_max_frag = max_frag; 9332 9333 if ((ire->ire_nce = ndp_lookup_v4( 9334 ire_to_ill(ire), 9335 (ire->ire_gateway_addr != INADDR_ANY ? 9336 &ire->ire_gateway_addr : &ire->ire_addr), 9337 B_FALSE)) == NULL) { 9338 if (save_ire != NULL) 9339 ire_refrele(save_ire); 9340 break; 9341 } 9342 ASSERT(ire->ire_nce->nce_state == 9343 ND_REACHABLE); 9344 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 9345 } 9346 9347 /* Prevent save_ire from getting deleted */ 9348 if (save_ire != NULL) { 9349 IRB_REFHOLD(save_ire->ire_bucket); 9350 /* Has it been removed already ? */ 9351 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 9352 IRB_REFRELE(save_ire->ire_bucket); 9353 ire_refrele(save_ire); 9354 break; 9355 } 9356 } 9357 9358 ire_add_then_send(q, ire, first_mp); 9359 9360 /* Assert that save_ire is not deleted yet. */ 9361 if (save_ire != NULL) { 9362 ASSERT(save_ire->ire_ptpn != NULL); 9363 IRB_REFRELE(save_ire->ire_bucket); 9364 ire_refrele(save_ire); 9365 save_ire = NULL; 9366 } 9367 if (fire != NULL) { 9368 ire_refrele(fire); 9369 fire = NULL; 9370 } 9371 9372 /* 9373 * the resolution loop is re-entered if this 9374 * was requested through flags and if we 9375 * actually are in a multirouting case. 9376 */ 9377 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9378 boolean_t need_resolve = 9379 ire_multirt_need_resolve(ipha_dst, 9380 msg_getlabel(copy_mp), ipst); 9381 if (!need_resolve) { 9382 MULTIRT_DEBUG_UNTAG(copy_mp); 9383 freemsg(copy_mp); 9384 copy_mp = NULL; 9385 } else { 9386 /* 9387 * ipif_lookup_group() calls 9388 * ire_lookup_multi() that uses 9389 * ire_ftable_lookup() to find 9390 * an IRE_INTERFACE for the group. 9391 * In the multirt case, 9392 * ire_lookup_multi() then invokes 9393 * ire_multirt_lookup() to find 9394 * the next resolvable ire. 9395 * As a result, we obtain an new 9396 * interface, derived from the 9397 * next ire. 9398 */ 9399 ipif_refrele(ipif); 9400 ipif = ipif_lookup_group(ipha_dst, 9401 zoneid, ipst); 9402 ip2dbg(("ip_newroute_ipif: " 9403 "multirt dst %08x, ipif %p\n", 9404 htonl(dst), (void *)ipif)); 9405 if (ipif != NULL) { 9406 mp = copy_mp; 9407 copy_mp = NULL; 9408 multirt_resolve_next = B_TRUE; 9409 continue; 9410 } else { 9411 freemsg(copy_mp); 9412 } 9413 } 9414 } 9415 if (ipif != NULL) 9416 ipif_refrele(ipif); 9417 ill_refrele(dst_ill); 9418 ipif_refrele(src_ipif); 9419 return; 9420 } 9421 case IRE_IF_RESOLVER: 9422 /* 9423 * We can't build an IRE_CACHE yet, but at least 9424 * we found a resolver that can help. 9425 */ 9426 res_mp = dst_ill->ill_resolver_mp; 9427 if (!OK_RESOLVER_MP(res_mp)) 9428 break; 9429 9430 /* 9431 * We obtain a partial IRE_CACHE which we will pass 9432 * along with the resolver query. When the response 9433 * comes back it will be there ready for us to add. 9434 * The new ire inherits the IRE_OFFSUBNET flags 9435 * and source address, if this was requested. 9436 * The ire_max_frag is atomically set under the 9437 * irebucket lock in ire_add_v[46]. Only in the 9438 * case of IRE_MARK_NOADD, we set it here itself. 9439 */ 9440 ire = ire_create_mp( 9441 (uchar_t *)&dst, /* dest address */ 9442 (uchar_t *)&ip_g_all_ones, /* mask */ 9443 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9444 NULL, /* gateway address */ 9445 (ire_marks & IRE_MARK_NOADD) ? 9446 ipif->ipif_mtu : 0, /* max_frag */ 9447 NULL, /* no src nce */ 9448 dst_ill->ill_rq, /* recv-from queue */ 9449 dst_ill->ill_wq, /* send-to queue */ 9450 IRE_CACHE, 9451 src_ipif, 9452 (save_ire != NULL ? save_ire->ire_mask : 0), 9453 (fire != NULL) ? /* Parent handle */ 9454 fire->ire_phandle : 0, 9455 (save_ire != NULL) ? /* Interface handle */ 9456 save_ire->ire_ihandle : 0, 9457 (fire != NULL) ? /* flags if any */ 9458 (fire->ire_flags & 9459 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9460 (save_ire == NULL ? &ire_uinfo_null : 9461 &save_ire->ire_uinfo), 9462 NULL, 9463 NULL, 9464 ipst); 9465 9466 if (save_ire != NULL) { 9467 ire_refrele(save_ire); 9468 save_ire = NULL; 9469 } 9470 if (ire == NULL) 9471 break; 9472 9473 ire->ire_marks |= ire_marks; 9474 /* 9475 * Construct message chain for the resolver of the 9476 * form: 9477 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 9478 * 9479 * NOTE : ire will be added later when the response 9480 * comes back from ARP. If the response does not 9481 * come back, ARP frees the packet. For this reason, 9482 * we can't REFHOLD the bucket of save_ire to prevent 9483 * deletions. We may not be able to REFRELE the 9484 * bucket if the response never comes back. 9485 * Thus, before adding the ire, ire_add_v4 will make 9486 * sure that the interface route does not get deleted. 9487 * This is the only case unlike ip_newroute_v6, 9488 * ip_newroute_ipif_v6 where we can always prevent 9489 * deletions because ire_add_then_send is called after 9490 * creating the IRE. 9491 * If IRE_MARK_NOADD is set, then ire_add_then_send 9492 * does not add this IRE into the IRE CACHE. 9493 */ 9494 ASSERT(ire->ire_mp != NULL); 9495 ire->ire_mp->b_cont = first_mp; 9496 /* Have saved_mp handy, for cleanup if canput fails */ 9497 saved_mp = mp; 9498 mp = copyb(res_mp); 9499 if (mp == NULL) { 9500 /* Prepare for cleanup */ 9501 mp = saved_mp; /* pkt */ 9502 ire_delete(ire); /* ire_mp */ 9503 ire = NULL; 9504 if (copy_mp != NULL) { 9505 MULTIRT_DEBUG_UNTAG(copy_mp); 9506 freemsg(copy_mp); 9507 copy_mp = NULL; 9508 } 9509 break; 9510 } 9511 linkb(mp, ire->ire_mp); 9512 9513 /* 9514 * Fill in the source and dest addrs for the resolver. 9515 * NOTE: this depends on memory layouts imposed by 9516 * ill_init(). There are corner cases above where we 9517 * might've created the IRE with an INADDR_ANY source 9518 * address (e.g., if the zeroth ipif on an underlying 9519 * ill in an IPMP group is 0.0.0.0, but another ipif 9520 * on the ill has a usable test address). If so, tell 9521 * ARP to use ipha_src as its sender address. 9522 */ 9523 areq = (areq_t *)mp->b_rptr; 9524 addrp = (ipaddr_t *)((char *)areq + 9525 areq->areq_sender_addr_offset); 9526 if (ire->ire_src_addr != INADDR_ANY) 9527 *addrp = ire->ire_src_addr; 9528 else 9529 *addrp = ipha->ipha_src; 9530 addrp = (ipaddr_t *)((char *)areq + 9531 areq->areq_target_addr_offset); 9532 *addrp = dst; 9533 /* Up to the resolver. */ 9534 if (canputnext(dst_ill->ill_rq) && 9535 !(dst_ill->ill_arp_closing)) { 9536 putnext(dst_ill->ill_rq, mp); 9537 /* 9538 * The response will come back in ip_wput 9539 * with db_type IRE_DB_TYPE. 9540 */ 9541 } else { 9542 mp->b_cont = NULL; 9543 freeb(mp); /* areq */ 9544 ire_delete(ire); /* ire_mp */ 9545 saved_mp->b_next = NULL; 9546 saved_mp->b_prev = NULL; 9547 freemsg(first_mp); /* pkt */ 9548 ip2dbg(("ip_newroute_ipif: dropped\n")); 9549 } 9550 9551 if (fire != NULL) { 9552 ire_refrele(fire); 9553 fire = NULL; 9554 } 9555 9556 /* 9557 * The resolution loop is re-entered if this was 9558 * requested through flags and we actually are 9559 * in a multirouting case. 9560 */ 9561 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9562 boolean_t need_resolve = 9563 ire_multirt_need_resolve(ipha_dst, 9564 msg_getlabel(copy_mp), ipst); 9565 if (!need_resolve) { 9566 MULTIRT_DEBUG_UNTAG(copy_mp); 9567 freemsg(copy_mp); 9568 copy_mp = NULL; 9569 } else { 9570 /* 9571 * ipif_lookup_group() calls 9572 * ire_lookup_multi() that uses 9573 * ire_ftable_lookup() to find 9574 * an IRE_INTERFACE for the group. 9575 * In the multirt case, 9576 * ire_lookup_multi() then invokes 9577 * ire_multirt_lookup() to find 9578 * the next resolvable ire. 9579 * As a result, we obtain an new 9580 * interface, derived from the 9581 * next ire. 9582 */ 9583 ipif_refrele(ipif); 9584 ipif = ipif_lookup_group(ipha_dst, 9585 zoneid, ipst); 9586 if (ipif != NULL) { 9587 mp = copy_mp; 9588 copy_mp = NULL; 9589 multirt_resolve_next = B_TRUE; 9590 continue; 9591 } else { 9592 freemsg(copy_mp); 9593 } 9594 } 9595 } 9596 if (ipif != NULL) 9597 ipif_refrele(ipif); 9598 ill_refrele(dst_ill); 9599 ipif_refrele(src_ipif); 9600 return; 9601 default: 9602 break; 9603 } 9604 } while (multirt_resolve_next); 9605 9606 err_ret: 9607 ip2dbg(("ip_newroute_ipif: dropped\n")); 9608 if (fire != NULL) 9609 ire_refrele(fire); 9610 ipif_refrele(ipif); 9611 /* Did this packet originate externally? */ 9612 if (dst_ill != NULL) 9613 ill_refrele(dst_ill); 9614 if (src_ipif != NULL) 9615 ipif_refrele(src_ipif); 9616 if (mp->b_prev || mp->b_next) { 9617 mp->b_next = NULL; 9618 mp->b_prev = NULL; 9619 } else { 9620 /* 9621 * Since ip_wput() isn't close to finished, we fill 9622 * in enough of the header for credible error reporting. 9623 */ 9624 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 9625 /* Failed */ 9626 freemsg(first_mp); 9627 if (ire != NULL) 9628 ire_refrele(ire); 9629 return; 9630 } 9631 } 9632 /* 9633 * At this point we will have ire only if RTF_BLACKHOLE 9634 * or RTF_REJECT flags are set on the IRE. It will not 9635 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9636 */ 9637 if (ire != NULL) { 9638 if (ire->ire_flags & RTF_BLACKHOLE) { 9639 ire_refrele(ire); 9640 freemsg(first_mp); 9641 return; 9642 } 9643 ire_refrele(ire); 9644 } 9645 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); 9646 } 9647 9648 /* Name/Value Table Lookup Routine */ 9649 char * 9650 ip_nv_lookup(nv_t *nv, int value) 9651 { 9652 if (!nv) 9653 return (NULL); 9654 for (; nv->nv_name; nv++) { 9655 if (nv->nv_value == value) 9656 return (nv->nv_name); 9657 } 9658 return ("unknown"); 9659 } 9660 9661 /* 9662 * This is a module open, i.e. this is a control stream for access 9663 * to a DLPI device. We allocate an ill_t as the instance data in 9664 * this case. 9665 */ 9666 int 9667 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9668 { 9669 ill_t *ill; 9670 int err; 9671 zoneid_t zoneid; 9672 netstack_t *ns; 9673 ip_stack_t *ipst; 9674 9675 /* 9676 * Prevent unprivileged processes from pushing IP so that 9677 * they can't send raw IP. 9678 */ 9679 if (secpolicy_net_rawaccess(credp) != 0) 9680 return (EPERM); 9681 9682 ns = netstack_find_by_cred(credp); 9683 ASSERT(ns != NULL); 9684 ipst = ns->netstack_ip; 9685 ASSERT(ipst != NULL); 9686 9687 /* 9688 * For exclusive stacks we set the zoneid to zero 9689 * to make IP operate as if in the global zone. 9690 */ 9691 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9692 zoneid = GLOBAL_ZONEID; 9693 else 9694 zoneid = crgetzoneid(credp); 9695 9696 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 9697 q->q_ptr = WR(q)->q_ptr = ill; 9698 ill->ill_ipst = ipst; 9699 ill->ill_zoneid = zoneid; 9700 9701 /* 9702 * ill_init initializes the ill fields and then sends down 9703 * down a DL_INFO_REQ after calling qprocson. 9704 */ 9705 err = ill_init(q, ill); 9706 if (err != 0) { 9707 mi_free(ill); 9708 netstack_rele(ipst->ips_netstack); 9709 q->q_ptr = NULL; 9710 WR(q)->q_ptr = NULL; 9711 return (err); 9712 } 9713 9714 /* ill_init initializes the ipsq marking this thread as writer */ 9715 ipsq_exit(ill->ill_phyint->phyint_ipsq); 9716 /* Wait for the DL_INFO_ACK */ 9717 mutex_enter(&ill->ill_lock); 9718 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 9719 /* 9720 * Return value of 0 indicates a pending signal. 9721 */ 9722 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 9723 if (err == 0) { 9724 mutex_exit(&ill->ill_lock); 9725 (void) ip_close(q, 0); 9726 return (EINTR); 9727 } 9728 } 9729 mutex_exit(&ill->ill_lock); 9730 9731 /* 9732 * ip_rput_other could have set an error in ill_error on 9733 * receipt of M_ERROR. 9734 */ 9735 9736 err = ill->ill_error; 9737 if (err != 0) { 9738 (void) ip_close(q, 0); 9739 return (err); 9740 } 9741 9742 ill->ill_credp = credp; 9743 crhold(credp); 9744 9745 mutex_enter(&ipst->ips_ip_mi_lock); 9746 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag, 9747 credp); 9748 mutex_exit(&ipst->ips_ip_mi_lock); 9749 if (err) { 9750 (void) ip_close(q, 0); 9751 return (err); 9752 } 9753 return (0); 9754 } 9755 9756 /* For /dev/ip aka AF_INET open */ 9757 int 9758 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9759 { 9760 return (ip_open(q, devp, flag, sflag, credp, B_FALSE)); 9761 } 9762 9763 /* For /dev/ip6 aka AF_INET6 open */ 9764 int 9765 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9766 { 9767 return (ip_open(q, devp, flag, sflag, credp, B_TRUE)); 9768 } 9769 9770 /* IP open routine. */ 9771 int 9772 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9773 boolean_t isv6) 9774 { 9775 conn_t *connp; 9776 major_t maj; 9777 zoneid_t zoneid; 9778 netstack_t *ns; 9779 ip_stack_t *ipst; 9780 9781 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 9782 9783 /* Allow reopen. */ 9784 if (q->q_ptr != NULL) 9785 return (0); 9786 9787 if (sflag & MODOPEN) { 9788 /* This is a module open */ 9789 return (ip_modopen(q, devp, flag, sflag, credp)); 9790 } 9791 9792 if ((flag & ~(FKLYR)) == IP_HELPER_STR) { 9793 /* 9794 * Non streams based socket looking for a stream 9795 * to access IP 9796 */ 9797 return (ip_helper_stream_setup(q, devp, flag, sflag, 9798 credp, isv6)); 9799 } 9800 9801 ns = netstack_find_by_cred(credp); 9802 ASSERT(ns != NULL); 9803 ipst = ns->netstack_ip; 9804 ASSERT(ipst != NULL); 9805 9806 /* 9807 * For exclusive stacks we set the zoneid to zero 9808 * to make IP operate as if in the global zone. 9809 */ 9810 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 9811 zoneid = GLOBAL_ZONEID; 9812 else 9813 zoneid = crgetzoneid(credp); 9814 9815 /* 9816 * We are opening as a device. This is an IP client stream, and we 9817 * allocate an conn_t as the instance data. 9818 */ 9819 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack); 9820 9821 /* 9822 * ipcl_conn_create did a netstack_hold. Undo the hold that was 9823 * done by netstack_find_by_cred() 9824 */ 9825 netstack_rele(ipst->ips_netstack); 9826 9827 connp->conn_zoneid = zoneid; 9828 connp->conn_sqp = NULL; 9829 connp->conn_initial_sqp = NULL; 9830 connp->conn_final_sqp = NULL; 9831 9832 connp->conn_upq = q; 9833 q->q_ptr = WR(q)->q_ptr = connp; 9834 9835 if (flag & SO_SOCKSTR) 9836 connp->conn_flags |= IPCL_SOCKET; 9837 9838 /* Minor tells us which /dev entry was opened */ 9839 if (isv6) { 9840 connp->conn_flags |= IPCL_ISV6; 9841 connp->conn_af_isv6 = B_TRUE; 9842 ip_setpktversion(connp, isv6, B_FALSE, ipst); 9843 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9844 } else { 9845 connp->conn_af_isv6 = B_FALSE; 9846 connp->conn_pkt_isv6 = B_FALSE; 9847 } 9848 9849 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9850 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9851 connp->conn_minor_arena = ip_minor_arena_la; 9852 } else { 9853 /* 9854 * Either minor numbers in the large arena were exhausted 9855 * or a non socket application is doing the open. 9856 * Try to allocate from the small arena. 9857 */ 9858 if ((connp->conn_dev = 9859 inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9860 /* CONN_DEC_REF takes care of netstack_rele() */ 9861 q->q_ptr = WR(q)->q_ptr = NULL; 9862 CONN_DEC_REF(connp); 9863 return (EBUSY); 9864 } 9865 connp->conn_minor_arena = ip_minor_arena_sa; 9866 } 9867 9868 maj = getemajor(*devp); 9869 *devp = makedevice(maj, (minor_t)connp->conn_dev); 9870 9871 /* 9872 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 9873 */ 9874 connp->conn_cred = credp; 9875 9876 /* 9877 * Handle IP_RTS_REQUEST and other ioctls which use conn_recv 9878 */ 9879 connp->conn_recv = ip_conn_input; 9880 9881 crhold(connp->conn_cred); 9882 9883 /* 9884 * If the caller has the process-wide flag set, then default to MAC 9885 * exempt mode. This allows read-down to unlabeled hosts. 9886 */ 9887 if (getpflags(NET_MAC_AWARE, credp) != 0) 9888 connp->conn_mac_exempt = B_TRUE; 9889 9890 connp->conn_rq = q; 9891 connp->conn_wq = WR(q); 9892 9893 /* Non-zero default values */ 9894 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9895 9896 /* 9897 * Make the conn globally visible to walkers 9898 */ 9899 ASSERT(connp->conn_ref == 1); 9900 mutex_enter(&connp->conn_lock); 9901 connp->conn_state_flags &= ~CONN_INCIPIENT; 9902 mutex_exit(&connp->conn_lock); 9903 9904 qprocson(q); 9905 9906 return (0); 9907 } 9908 9909 /* 9910 * Change the output format (IPv4 vs. IPv6) for a conn_t. 9911 * Note that there is no race since either ip_output function works - it 9912 * is just an optimization to enter the best ip_output routine directly. 9913 */ 9914 void 9915 ip_setpktversion(conn_t *connp, boolean_t isv6, boolean_t bump_mib, 9916 ip_stack_t *ipst) 9917 { 9918 if (isv6) { 9919 if (bump_mib) { 9920 BUMP_MIB(&ipst->ips_ip6_mib, 9921 ipIfStatsOutSwitchIPVersion); 9922 } 9923 connp->conn_send = ip_output_v6; 9924 connp->conn_pkt_isv6 = B_TRUE; 9925 } else { 9926 if (bump_mib) { 9927 BUMP_MIB(&ipst->ips_ip_mib, 9928 ipIfStatsOutSwitchIPVersion); 9929 } 9930 connp->conn_send = ip_output; 9931 connp->conn_pkt_isv6 = B_FALSE; 9932 } 9933 9934 } 9935 9936 /* 9937 * See if IPsec needs loading because of the options in mp. 9938 */ 9939 static boolean_t 9940 ipsec_opt_present(mblk_t *mp) 9941 { 9942 uint8_t *optcp, *next_optcp, *opt_endcp; 9943 struct opthdr *opt; 9944 struct T_opthdr *topt; 9945 int opthdr_len; 9946 t_uscalar_t optname, optlevel; 9947 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 9948 ipsec_req_t *ipsr; 9949 9950 /* 9951 * Walk through the mess, and find IP_SEC_OPT. If it's there, 9952 * return TRUE. 9953 */ 9954 9955 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 9956 opt_endcp = optcp + tor->OPT_length; 9957 if (tor->PRIM_type == T_OPTMGMT_REQ) { 9958 opthdr_len = sizeof (struct T_opthdr); 9959 } else { /* O_OPTMGMT_REQ */ 9960 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 9961 opthdr_len = sizeof (struct opthdr); 9962 } 9963 for (; optcp < opt_endcp; optcp = next_optcp) { 9964 if (optcp + opthdr_len > opt_endcp) 9965 return (B_FALSE); /* Not enough option header. */ 9966 if (tor->PRIM_type == T_OPTMGMT_REQ) { 9967 topt = (struct T_opthdr *)optcp; 9968 optlevel = topt->level; 9969 optname = topt->name; 9970 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 9971 } else { 9972 opt = (struct opthdr *)optcp; 9973 optlevel = opt->level; 9974 optname = opt->name; 9975 next_optcp = optcp + opthdr_len + 9976 _TPI_ALIGN_OPT(opt->len); 9977 } 9978 if ((next_optcp < optcp) || /* wraparound pointer space */ 9979 ((next_optcp >= opt_endcp) && /* last option bad len */ 9980 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 9981 return (B_FALSE); /* bad option buffer */ 9982 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 9983 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 9984 /* 9985 * Check to see if it's an all-bypass or all-zeroes 9986 * IPsec request. Don't bother loading IPsec if 9987 * the socket doesn't want to use it. (A good example 9988 * is a bypass request.) 9989 * 9990 * Basically, if any of the non-NEVER bits are set, 9991 * load IPsec. 9992 */ 9993 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 9994 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 9995 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 9996 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 9997 != 0) 9998 return (B_TRUE); 9999 } 10000 } 10001 return (B_FALSE); 10002 } 10003 10004 /* 10005 * If conn is is waiting for ipsec to finish loading, kick it. 10006 */ 10007 /* ARGSUSED */ 10008 static void 10009 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 10010 { 10011 t_scalar_t optreq_prim; 10012 mblk_t *mp; 10013 cred_t *cr; 10014 int err = 0; 10015 10016 /* 10017 * This function is called, after ipsec loading is complete. 10018 * Since IP checks exclusively and atomically (i.e it prevents 10019 * ipsec load from completing until ip_optcom_req completes) 10020 * whether ipsec load is complete, there cannot be a race with IP 10021 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 10022 */ 10023 mutex_enter(&connp->conn_lock); 10024 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 10025 ASSERT(connp->conn_ipsec_opt_mp != NULL); 10026 mp = connp->conn_ipsec_opt_mp; 10027 connp->conn_ipsec_opt_mp = NULL; 10028 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 10029 mutex_exit(&connp->conn_lock); 10030 10031 /* 10032 * All Solaris components should pass a db_credp 10033 * for this TPI message, hence we ASSERT. 10034 * But in case there is some other M_PROTO that looks 10035 * like a TPI message sent by some other kernel 10036 * component, we check and return an error. 10037 */ 10038 cr = msg_getcred(mp, NULL); 10039 ASSERT(cr != NULL); 10040 if (cr == NULL) { 10041 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 10042 if (mp != NULL) 10043 qreply(connp->conn_wq, mp); 10044 return; 10045 } 10046 10047 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 10048 10049 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 10050 if (optreq_prim == T_OPTMGMT_REQ) { 10051 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10052 &ip_opt_obj, B_FALSE); 10053 } else { 10054 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 10055 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10056 &ip_opt_obj, B_FALSE); 10057 } 10058 if (err != EINPROGRESS) 10059 CONN_OPER_PENDING_DONE(connp); 10060 return; 10061 } 10062 mutex_exit(&connp->conn_lock); 10063 } 10064 10065 /* 10066 * Called from the ipsec_loader thread, outside any perimeter, to tell 10067 * ip qenable any of the queues waiting for the ipsec loader to 10068 * complete. 10069 */ 10070 void 10071 ip_ipsec_load_complete(ipsec_stack_t *ipss) 10072 { 10073 netstack_t *ns = ipss->ipsec_netstack; 10074 10075 ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip); 10076 } 10077 10078 /* 10079 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 10080 * determines the grp on which it has to become exclusive, queues the mp 10081 * and IPSQ draining restarts the optmgmt 10082 */ 10083 static boolean_t 10084 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 10085 { 10086 conn_t *connp = Q_TO_CONN(q); 10087 ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec; 10088 10089 /* 10090 * Take IPsec requests and treat them special. 10091 */ 10092 if (ipsec_opt_present(mp)) { 10093 /* First check if IPsec is loaded. */ 10094 mutex_enter(&ipss->ipsec_loader_lock); 10095 if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) { 10096 mutex_exit(&ipss->ipsec_loader_lock); 10097 return (B_FALSE); 10098 } 10099 mutex_enter(&connp->conn_lock); 10100 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 10101 10102 ASSERT(connp->conn_ipsec_opt_mp == NULL); 10103 connp->conn_ipsec_opt_mp = mp; 10104 mutex_exit(&connp->conn_lock); 10105 mutex_exit(&ipss->ipsec_loader_lock); 10106 10107 ipsec_loader_loadnow(ipss); 10108 return (B_TRUE); 10109 } 10110 return (B_FALSE); 10111 } 10112 10113 /* 10114 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 10115 * all of them are copied to the conn_t. If the req is "zero", the policy is 10116 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 10117 * fields. 10118 * We keep only the latest setting of the policy and thus policy setting 10119 * is not incremental/cumulative. 10120 * 10121 * Requests to set policies with multiple alternative actions will 10122 * go through a different API. 10123 */ 10124 int 10125 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 10126 { 10127 uint_t ah_req = 0; 10128 uint_t esp_req = 0; 10129 uint_t se_req = 0; 10130 ipsec_selkey_t sel; 10131 ipsec_act_t *actp = NULL; 10132 uint_t nact; 10133 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 10134 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 10135 ipsec_policy_root_t *pr; 10136 ipsec_policy_head_t *ph; 10137 int fam; 10138 boolean_t is_pol_reset; 10139 int error = 0; 10140 netstack_t *ns = connp->conn_netstack; 10141 ip_stack_t *ipst = ns->netstack_ip; 10142 ipsec_stack_t *ipss = ns->netstack_ipsec; 10143 10144 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 10145 10146 /* 10147 * The IP_SEC_OPT option does not allow variable length parameters, 10148 * hence a request cannot be NULL. 10149 */ 10150 if (req == NULL) 10151 return (EINVAL); 10152 10153 ah_req = req->ipsr_ah_req; 10154 esp_req = req->ipsr_esp_req; 10155 se_req = req->ipsr_self_encap_req; 10156 10157 /* Don't allow setting self-encap without one or more of AH/ESP. */ 10158 if (se_req != 0 && esp_req == 0 && ah_req == 0) 10159 return (EINVAL); 10160 10161 /* 10162 * Are we dealing with a request to reset the policy (i.e. 10163 * zero requests). 10164 */ 10165 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 10166 (esp_req & REQ_MASK) == 0 && 10167 (se_req & REQ_MASK) == 0); 10168 10169 if (!is_pol_reset) { 10170 /* 10171 * If we couldn't load IPsec, fail with "protocol 10172 * not supported". 10173 * IPsec may not have been loaded for a request with zero 10174 * policies, so we don't fail in this case. 10175 */ 10176 mutex_enter(&ipss->ipsec_loader_lock); 10177 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 10178 mutex_exit(&ipss->ipsec_loader_lock); 10179 return (EPROTONOSUPPORT); 10180 } 10181 mutex_exit(&ipss->ipsec_loader_lock); 10182 10183 /* 10184 * Test for valid requests. Invalid algorithms 10185 * need to be tested by IPsec code because new 10186 * algorithms can be added dynamically. 10187 */ 10188 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10189 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10190 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 10191 return (EINVAL); 10192 } 10193 10194 /* 10195 * Only privileged users can issue these 10196 * requests. 10197 */ 10198 if (((ah_req & IPSEC_PREF_NEVER) || 10199 (esp_req & IPSEC_PREF_NEVER) || 10200 (se_req & IPSEC_PREF_NEVER)) && 10201 secpolicy_ip_config(cr, B_FALSE) != 0) { 10202 return (EPERM); 10203 } 10204 10205 /* 10206 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 10207 * are mutually exclusive. 10208 */ 10209 if (((ah_req & REQ_MASK) == REQ_MASK) || 10210 ((esp_req & REQ_MASK) == REQ_MASK) || 10211 ((se_req & REQ_MASK) == REQ_MASK)) { 10212 /* Both of them are set */ 10213 return (EINVAL); 10214 } 10215 } 10216 10217 mutex_enter(&connp->conn_lock); 10218 10219 /* 10220 * If we have already cached policies in ip_bind_connected*(), don't 10221 * let them change now. We cache policies for connections 10222 * whose src,dst [addr, port] is known. 10223 */ 10224 if (connp->conn_policy_cached) { 10225 mutex_exit(&connp->conn_lock); 10226 return (EINVAL); 10227 } 10228 10229 /* 10230 * We have a zero policies, reset the connection policy if already 10231 * set. This will cause the connection to inherit the 10232 * global policy, if any. 10233 */ 10234 if (is_pol_reset) { 10235 if (connp->conn_policy != NULL) { 10236 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack); 10237 connp->conn_policy = NULL; 10238 } 10239 connp->conn_flags &= ~IPCL_CHECK_POLICY; 10240 connp->conn_in_enforce_policy = B_FALSE; 10241 connp->conn_out_enforce_policy = B_FALSE; 10242 mutex_exit(&connp->conn_lock); 10243 return (0); 10244 } 10245 10246 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy, 10247 ipst->ips_netstack); 10248 if (ph == NULL) 10249 goto enomem; 10250 10251 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack); 10252 if (actp == NULL) 10253 goto enomem; 10254 10255 /* 10256 * Always allocate IPv4 policy entries, since they can also 10257 * apply to ipv6 sockets being used in ipv4-compat mode. 10258 */ 10259 bzero(&sel, sizeof (sel)); 10260 sel.ipsl_valid = IPSL_IPV4; 10261 10262 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10263 ipst->ips_netstack); 10264 if (pin4 == NULL) 10265 goto enomem; 10266 10267 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, 10268 ipst->ips_netstack); 10269 if (pout4 == NULL) 10270 goto enomem; 10271 10272 if (connp->conn_af_isv6) { 10273 /* 10274 * We're looking at a v6 socket, also allocate the 10275 * v6-specific entries... 10276 */ 10277 sel.ipsl_valid = IPSL_IPV6; 10278 pin6 = ipsec_policy_create(&sel, actp, nact, 10279 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10280 if (pin6 == NULL) 10281 goto enomem; 10282 10283 pout6 = ipsec_policy_create(&sel, actp, nact, 10284 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); 10285 if (pout6 == NULL) 10286 goto enomem; 10287 10288 /* 10289 * .. and file them away in the right place. 10290 */ 10291 fam = IPSEC_AF_V6; 10292 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10293 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 10294 ipsec_insert_always(&ph->iph_rulebyid, pin6); 10295 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10296 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 10297 ipsec_insert_always(&ph->iph_rulebyid, pout6); 10298 } 10299 10300 ipsec_actvec_free(actp, nact); 10301 10302 /* 10303 * File the v4 policies. 10304 */ 10305 fam = IPSEC_AF_V4; 10306 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10307 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 10308 ipsec_insert_always(&ph->iph_rulebyid, pin4); 10309 10310 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10311 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 10312 ipsec_insert_always(&ph->iph_rulebyid, pout4); 10313 10314 /* 10315 * If the requests need security, set enforce_policy. 10316 * If the requests are IPSEC_PREF_NEVER, one should 10317 * still set conn_out_enforce_policy so that an ipsec_out 10318 * gets attached in ip_wput. This is needed so that 10319 * for connections that we don't cache policy in ip_bind, 10320 * if global policy matches in ip_wput_attach_policy, we 10321 * don't wrongly inherit global policy. Similarly, we need 10322 * to set conn_in_enforce_policy also so that we don't verify 10323 * policy wrongly. 10324 */ 10325 if ((ah_req & REQ_MASK) != 0 || 10326 (esp_req & REQ_MASK) != 0 || 10327 (se_req & REQ_MASK) != 0) { 10328 connp->conn_in_enforce_policy = B_TRUE; 10329 connp->conn_out_enforce_policy = B_TRUE; 10330 connp->conn_flags |= IPCL_CHECK_POLICY; 10331 } 10332 10333 mutex_exit(&connp->conn_lock); 10334 return (error); 10335 #undef REQ_MASK 10336 10337 /* 10338 * Common memory-allocation-failure exit path. 10339 */ 10340 enomem: 10341 mutex_exit(&connp->conn_lock); 10342 if (actp != NULL) 10343 ipsec_actvec_free(actp, nact); 10344 if (pin4 != NULL) 10345 IPPOL_REFRELE(pin4, ipst->ips_netstack); 10346 if (pout4 != NULL) 10347 IPPOL_REFRELE(pout4, ipst->ips_netstack); 10348 if (pin6 != NULL) 10349 IPPOL_REFRELE(pin6, ipst->ips_netstack); 10350 if (pout6 != NULL) 10351 IPPOL_REFRELE(pout6, ipst->ips_netstack); 10352 return (ENOMEM); 10353 } 10354 10355 /* 10356 * Only for options that pass in an IP addr. Currently only V4 options 10357 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 10358 * So this function assumes level is IPPROTO_IP 10359 */ 10360 int 10361 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 10362 mblk_t *first_mp) 10363 { 10364 ipif_t *ipif = NULL; 10365 int error; 10366 ill_t *ill; 10367 int zoneid; 10368 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10369 10370 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 10371 10372 if (addr != INADDR_ANY || checkonly) { 10373 ASSERT(connp != NULL); 10374 zoneid = IPCL_ZONEID(connp); 10375 if (option == IP_NEXTHOP) { 10376 ipif = ipif_lookup_onlink_addr(addr, 10377 connp->conn_zoneid, ipst); 10378 } else { 10379 ipif = ipif_lookup_addr(addr, NULL, zoneid, 10380 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 10381 &error, ipst); 10382 } 10383 if (ipif == NULL) { 10384 if (error == EINPROGRESS) 10385 return (error); 10386 if ((option == IP_MULTICAST_IF) || 10387 (option == IP_NEXTHOP)) 10388 return (EHOSTUNREACH); 10389 else 10390 return (EINVAL); 10391 } else if (checkonly) { 10392 if (option == IP_MULTICAST_IF) { 10393 ill = ipif->ipif_ill; 10394 /* not supported by the virtual network iface */ 10395 if (IS_VNI(ill)) { 10396 ipif_refrele(ipif); 10397 return (EINVAL); 10398 } 10399 } 10400 ipif_refrele(ipif); 10401 return (0); 10402 } 10403 ill = ipif->ipif_ill; 10404 mutex_enter(&connp->conn_lock); 10405 mutex_enter(&ill->ill_lock); 10406 if ((ill->ill_state_flags & ILL_CONDEMNED) || 10407 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 10408 mutex_exit(&ill->ill_lock); 10409 mutex_exit(&connp->conn_lock); 10410 ipif_refrele(ipif); 10411 return (option == IP_MULTICAST_IF ? 10412 EHOSTUNREACH : EINVAL); 10413 } 10414 } else { 10415 mutex_enter(&connp->conn_lock); 10416 } 10417 10418 /* None of the options below are supported on the VNI */ 10419 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 10420 mutex_exit(&ill->ill_lock); 10421 mutex_exit(&connp->conn_lock); 10422 ipif_refrele(ipif); 10423 return (EINVAL); 10424 } 10425 10426 switch (option) { 10427 case IP_MULTICAST_IF: 10428 connp->conn_multicast_ipif = ipif; 10429 break; 10430 case IP_NEXTHOP: 10431 connp->conn_nexthop_v4 = addr; 10432 connp->conn_nexthop_set = B_TRUE; 10433 break; 10434 } 10435 10436 if (ipif != NULL) { 10437 mutex_exit(&ill->ill_lock); 10438 mutex_exit(&connp->conn_lock); 10439 ipif_refrele(ipif); 10440 return (0); 10441 } 10442 mutex_exit(&connp->conn_lock); 10443 /* We succeded in cleared the option */ 10444 return (0); 10445 } 10446 10447 /* 10448 * For options that pass in an ifindex specifying the ill. V6 options always 10449 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 10450 */ 10451 int 10452 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 10453 int level, int option, mblk_t *first_mp) 10454 { 10455 ill_t *ill = NULL; 10456 int error = 0; 10457 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10458 10459 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 10460 if (ifindex != 0) { 10461 ASSERT(connp != NULL); 10462 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 10463 first_mp, ip_restart_optmgmt, &error, ipst); 10464 if (ill != NULL) { 10465 if (checkonly) { 10466 /* not supported by the virtual network iface */ 10467 if (IS_VNI(ill)) { 10468 ill_refrele(ill); 10469 return (EINVAL); 10470 } 10471 ill_refrele(ill); 10472 return (0); 10473 } 10474 if (!ipif_lookup_zoneid(ill, connp->conn_zoneid, 10475 0, NULL)) { 10476 ill_refrele(ill); 10477 ill = NULL; 10478 mutex_enter(&connp->conn_lock); 10479 goto setit; 10480 } 10481 mutex_enter(&connp->conn_lock); 10482 mutex_enter(&ill->ill_lock); 10483 if (ill->ill_state_flags & ILL_CONDEMNED) { 10484 mutex_exit(&ill->ill_lock); 10485 mutex_exit(&connp->conn_lock); 10486 ill_refrele(ill); 10487 ill = NULL; 10488 mutex_enter(&connp->conn_lock); 10489 } 10490 goto setit; 10491 } else if (error == EINPROGRESS) { 10492 return (error); 10493 } else { 10494 error = 0; 10495 } 10496 } 10497 mutex_enter(&connp->conn_lock); 10498 setit: 10499 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 10500 10501 /* 10502 * The options below assume that the ILL (if any) transmits and/or 10503 * receives traffic. Neither of which is true for the virtual network 10504 * interface, so fail setting these on a VNI. 10505 */ 10506 if (IS_VNI(ill)) { 10507 ASSERT(ill != NULL); 10508 mutex_exit(&ill->ill_lock); 10509 mutex_exit(&connp->conn_lock); 10510 ill_refrele(ill); 10511 return (EINVAL); 10512 } 10513 10514 if (level == IPPROTO_IP) { 10515 switch (option) { 10516 case IP_BOUND_IF: 10517 connp->conn_incoming_ill = ill; 10518 connp->conn_outgoing_ill = ill; 10519 break; 10520 10521 case IP_MULTICAST_IF: 10522 /* 10523 * This option is an internal special. The socket 10524 * level IP_MULTICAST_IF specifies an 'ipaddr' and 10525 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 10526 * specifies an ifindex and we try first on V6 ill's. 10527 * If we don't find one, we they try using on v4 ill's 10528 * intenally and we come here. 10529 */ 10530 if (!checkonly && ill != NULL) { 10531 ipif_t *ipif; 10532 ipif = ill->ill_ipif; 10533 10534 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 10535 mutex_exit(&ill->ill_lock); 10536 mutex_exit(&connp->conn_lock); 10537 ill_refrele(ill); 10538 ill = NULL; 10539 mutex_enter(&connp->conn_lock); 10540 } else { 10541 connp->conn_multicast_ipif = ipif; 10542 } 10543 } 10544 break; 10545 10546 case IP_DHCPINIT_IF: 10547 if (connp->conn_dhcpinit_ill != NULL) { 10548 /* 10549 * We've locked the conn so conn_cleanup_ill() 10550 * cannot clear conn_dhcpinit_ill -- so it's 10551 * safe to access the ill. 10552 */ 10553 ill_t *oill = connp->conn_dhcpinit_ill; 10554 10555 ASSERT(oill->ill_dhcpinit != 0); 10556 atomic_dec_32(&oill->ill_dhcpinit); 10557 connp->conn_dhcpinit_ill = NULL; 10558 } 10559 10560 if (ill != NULL) { 10561 connp->conn_dhcpinit_ill = ill; 10562 atomic_inc_32(&ill->ill_dhcpinit); 10563 } 10564 break; 10565 } 10566 } else { 10567 switch (option) { 10568 case IPV6_BOUND_IF: 10569 connp->conn_incoming_ill = ill; 10570 connp->conn_outgoing_ill = ill; 10571 break; 10572 10573 case IPV6_MULTICAST_IF: 10574 /* 10575 * Set conn_multicast_ill to be the IPv6 ill. 10576 * Set conn_multicast_ipif to be an IPv4 ipif 10577 * for ifindex to make IPv4 mapped addresses 10578 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 10579 * Even if no IPv6 ill exists for the ifindex 10580 * we need to check for an IPv4 ifindex in order 10581 * for this to work with mapped addresses. In that 10582 * case only set conn_multicast_ipif. 10583 */ 10584 if (!checkonly) { 10585 if (ifindex == 0) { 10586 connp->conn_multicast_ill = NULL; 10587 connp->conn_multicast_ipif = NULL; 10588 } else if (ill != NULL) { 10589 connp->conn_multicast_ill = ill; 10590 } 10591 } 10592 break; 10593 } 10594 } 10595 10596 if (ill != NULL) { 10597 mutex_exit(&ill->ill_lock); 10598 mutex_exit(&connp->conn_lock); 10599 ill_refrele(ill); 10600 return (0); 10601 } 10602 mutex_exit(&connp->conn_lock); 10603 /* 10604 * We succeeded in clearing the option (ifindex == 0) or failed to 10605 * locate the ill and could not set the option (ifindex != 0) 10606 */ 10607 return (ifindex == 0 ? 0 : EINVAL); 10608 } 10609 10610 /* This routine sets socket options. */ 10611 /* ARGSUSED */ 10612 int 10613 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10614 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10615 void *dummy, cred_t *cr, mblk_t *first_mp) 10616 { 10617 int *i1 = (int *)invalp; 10618 conn_t *connp = Q_TO_CONN(q); 10619 int error = 0; 10620 boolean_t checkonly; 10621 ire_t *ire; 10622 boolean_t found; 10623 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 10624 10625 switch (optset_context) { 10626 10627 case SETFN_OPTCOM_CHECKONLY: 10628 checkonly = B_TRUE; 10629 /* 10630 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10631 * inlen != 0 implies value supplied and 10632 * we have to "pretend" to set it. 10633 * inlen == 0 implies that there is no 10634 * value part in T_CHECK request and just validation 10635 * done elsewhere should be enough, we just return here. 10636 */ 10637 if (inlen == 0) { 10638 *outlenp = 0; 10639 return (0); 10640 } 10641 break; 10642 case SETFN_OPTCOM_NEGOTIATE: 10643 case SETFN_UD_NEGOTIATE: 10644 case SETFN_CONN_NEGOTIATE: 10645 checkonly = B_FALSE; 10646 break; 10647 default: 10648 /* 10649 * We should never get here 10650 */ 10651 *outlenp = 0; 10652 return (EINVAL); 10653 } 10654 10655 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10656 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10657 10658 /* 10659 * For fixed length options, no sanity check 10660 * of passed in length is done. It is assumed *_optcom_req() 10661 * routines do the right thing. 10662 */ 10663 10664 switch (level) { 10665 case SOL_SOCKET: 10666 /* 10667 * conn_lock protects the bitfields, and is used to 10668 * set the fields atomically. 10669 */ 10670 switch (name) { 10671 case SO_BROADCAST: 10672 if (!checkonly) { 10673 /* TODO: use value someplace? */ 10674 mutex_enter(&connp->conn_lock); 10675 connp->conn_broadcast = *i1 ? 1 : 0; 10676 mutex_exit(&connp->conn_lock); 10677 } 10678 break; /* goto sizeof (int) option return */ 10679 case SO_USELOOPBACK: 10680 if (!checkonly) { 10681 /* TODO: use value someplace? */ 10682 mutex_enter(&connp->conn_lock); 10683 connp->conn_loopback = *i1 ? 1 : 0; 10684 mutex_exit(&connp->conn_lock); 10685 } 10686 break; /* goto sizeof (int) option return */ 10687 case SO_DONTROUTE: 10688 if (!checkonly) { 10689 mutex_enter(&connp->conn_lock); 10690 connp->conn_dontroute = *i1 ? 1 : 0; 10691 mutex_exit(&connp->conn_lock); 10692 } 10693 break; /* goto sizeof (int) option return */ 10694 case SO_REUSEADDR: 10695 if (!checkonly) { 10696 mutex_enter(&connp->conn_lock); 10697 connp->conn_reuseaddr = *i1 ? 1 : 0; 10698 mutex_exit(&connp->conn_lock); 10699 } 10700 break; /* goto sizeof (int) option return */ 10701 case SO_PROTOTYPE: 10702 if (!checkonly) { 10703 mutex_enter(&connp->conn_lock); 10704 connp->conn_proto = *i1; 10705 mutex_exit(&connp->conn_lock); 10706 } 10707 break; /* goto sizeof (int) option return */ 10708 case SO_ALLZONES: 10709 if (!checkonly) { 10710 mutex_enter(&connp->conn_lock); 10711 if (IPCL_IS_BOUND(connp)) { 10712 mutex_exit(&connp->conn_lock); 10713 return (EINVAL); 10714 } 10715 connp->conn_allzones = *i1 != 0 ? 1 : 0; 10716 mutex_exit(&connp->conn_lock); 10717 } 10718 break; /* goto sizeof (int) option return */ 10719 case SO_ANON_MLP: 10720 if (!checkonly) { 10721 mutex_enter(&connp->conn_lock); 10722 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; 10723 mutex_exit(&connp->conn_lock); 10724 } 10725 break; /* goto sizeof (int) option return */ 10726 case SO_MAC_EXEMPT: 10727 if (secpolicy_net_mac_aware(cr) != 0 || 10728 IPCL_IS_BOUND(connp)) 10729 return (EACCES); 10730 if (!checkonly) { 10731 mutex_enter(&connp->conn_lock); 10732 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; 10733 mutex_exit(&connp->conn_lock); 10734 } 10735 break; /* goto sizeof (int) option return */ 10736 default: 10737 /* 10738 * "soft" error (negative) 10739 * option not handled at this level 10740 * Note: Do not modify *outlenp 10741 */ 10742 return (-EINVAL); 10743 } 10744 break; 10745 case IPPROTO_IP: 10746 switch (name) { 10747 case IP_NEXTHOP: 10748 if (secpolicy_ip_config(cr, B_FALSE) != 0) 10749 return (EPERM); 10750 /* FALLTHRU */ 10751 case IP_MULTICAST_IF: { 10752 ipaddr_t addr = *i1; 10753 10754 error = ip_opt_set_ipif(connp, addr, checkonly, name, 10755 first_mp); 10756 if (error != 0) 10757 return (error); 10758 break; /* goto sizeof (int) option return */ 10759 } 10760 10761 case IP_MULTICAST_TTL: 10762 /* Recorded in transport above IP */ 10763 *outvalp = *invalp; 10764 *outlenp = sizeof (uchar_t); 10765 return (0); 10766 case IP_MULTICAST_LOOP: 10767 if (!checkonly) { 10768 mutex_enter(&connp->conn_lock); 10769 connp->conn_multicast_loop = *invalp ? 1 : 0; 10770 mutex_exit(&connp->conn_lock); 10771 } 10772 *outvalp = *invalp; 10773 *outlenp = sizeof (uchar_t); 10774 return (0); 10775 case IP_ADD_MEMBERSHIP: 10776 case MCAST_JOIN_GROUP: 10777 case IP_DROP_MEMBERSHIP: 10778 case MCAST_LEAVE_GROUP: { 10779 struct ip_mreq *mreqp; 10780 struct group_req *greqp; 10781 ire_t *ire; 10782 boolean_t done = B_FALSE; 10783 ipaddr_t group, ifaddr; 10784 struct sockaddr_in *sin; 10785 uint32_t *ifindexp; 10786 boolean_t mcast_opt = B_TRUE; 10787 mcast_record_t fmode; 10788 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10789 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10790 10791 switch (name) { 10792 case IP_ADD_MEMBERSHIP: 10793 mcast_opt = B_FALSE; 10794 /* FALLTHRU */ 10795 case MCAST_JOIN_GROUP: 10796 fmode = MODE_IS_EXCLUDE; 10797 optfn = ip_opt_add_group; 10798 break; 10799 10800 case IP_DROP_MEMBERSHIP: 10801 mcast_opt = B_FALSE; 10802 /* FALLTHRU */ 10803 case MCAST_LEAVE_GROUP: 10804 fmode = MODE_IS_INCLUDE; 10805 optfn = ip_opt_delete_group; 10806 break; 10807 } 10808 10809 if (mcast_opt) { 10810 greqp = (struct group_req *)i1; 10811 sin = (struct sockaddr_in *)&greqp->gr_group; 10812 if (sin->sin_family != AF_INET) { 10813 *outlenp = 0; 10814 return (ENOPROTOOPT); 10815 } 10816 group = (ipaddr_t)sin->sin_addr.s_addr; 10817 ifaddr = INADDR_ANY; 10818 ifindexp = &greqp->gr_interface; 10819 } else { 10820 mreqp = (struct ip_mreq *)i1; 10821 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 10822 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 10823 ifindexp = NULL; 10824 } 10825 10826 /* 10827 * In the multirouting case, we need to replicate 10828 * the request on all interfaces that will take part 10829 * in replication. We do so because multirouting is 10830 * reflective, thus we will probably receive multi- 10831 * casts on those interfaces. 10832 * The ip_multirt_apply_membership() succeeds if the 10833 * operation succeeds on at least one interface. 10834 */ 10835 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 10836 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10837 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 10838 if (ire != NULL) { 10839 if (ire->ire_flags & RTF_MULTIRT) { 10840 error = ip_multirt_apply_membership( 10841 optfn, ire, connp, checkonly, group, 10842 fmode, INADDR_ANY, first_mp); 10843 done = B_TRUE; 10844 } 10845 ire_refrele(ire); 10846 } 10847 if (!done) { 10848 error = optfn(connp, checkonly, group, ifaddr, 10849 ifindexp, fmode, INADDR_ANY, first_mp); 10850 } 10851 if (error) { 10852 /* 10853 * EINPROGRESS is a soft error, needs retry 10854 * so don't make *outlenp zero. 10855 */ 10856 if (error != EINPROGRESS) 10857 *outlenp = 0; 10858 return (error); 10859 } 10860 /* OK return - copy input buffer into output buffer */ 10861 if (invalp != outvalp) { 10862 /* don't trust bcopy for identical src/dst */ 10863 bcopy(invalp, outvalp, inlen); 10864 } 10865 *outlenp = inlen; 10866 return (0); 10867 } 10868 case IP_BLOCK_SOURCE: 10869 case IP_UNBLOCK_SOURCE: 10870 case IP_ADD_SOURCE_MEMBERSHIP: 10871 case IP_DROP_SOURCE_MEMBERSHIP: 10872 case MCAST_BLOCK_SOURCE: 10873 case MCAST_UNBLOCK_SOURCE: 10874 case MCAST_JOIN_SOURCE_GROUP: 10875 case MCAST_LEAVE_SOURCE_GROUP: { 10876 struct ip_mreq_source *imreqp; 10877 struct group_source_req *gsreqp; 10878 in_addr_t grp, src, ifaddr = INADDR_ANY; 10879 uint32_t ifindex = 0; 10880 mcast_record_t fmode; 10881 struct sockaddr_in *sin; 10882 ire_t *ire; 10883 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 10884 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10885 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10886 10887 switch (name) { 10888 case IP_BLOCK_SOURCE: 10889 mcast_opt = B_FALSE; 10890 /* FALLTHRU */ 10891 case MCAST_BLOCK_SOURCE: 10892 fmode = MODE_IS_EXCLUDE; 10893 optfn = ip_opt_add_group; 10894 break; 10895 10896 case IP_UNBLOCK_SOURCE: 10897 mcast_opt = B_FALSE; 10898 /* FALLTHRU */ 10899 case MCAST_UNBLOCK_SOURCE: 10900 fmode = MODE_IS_EXCLUDE; 10901 optfn = ip_opt_delete_group; 10902 break; 10903 10904 case IP_ADD_SOURCE_MEMBERSHIP: 10905 mcast_opt = B_FALSE; 10906 /* FALLTHRU */ 10907 case MCAST_JOIN_SOURCE_GROUP: 10908 fmode = MODE_IS_INCLUDE; 10909 optfn = ip_opt_add_group; 10910 break; 10911 10912 case IP_DROP_SOURCE_MEMBERSHIP: 10913 mcast_opt = B_FALSE; 10914 /* FALLTHRU */ 10915 case MCAST_LEAVE_SOURCE_GROUP: 10916 fmode = MODE_IS_INCLUDE; 10917 optfn = ip_opt_delete_group; 10918 break; 10919 } 10920 10921 if (mcast_opt) { 10922 gsreqp = (struct group_source_req *)i1; 10923 if (gsreqp->gsr_group.ss_family != AF_INET) { 10924 *outlenp = 0; 10925 return (ENOPROTOOPT); 10926 } 10927 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 10928 grp = (ipaddr_t)sin->sin_addr.s_addr; 10929 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 10930 src = (ipaddr_t)sin->sin_addr.s_addr; 10931 ifindex = gsreqp->gsr_interface; 10932 } else { 10933 imreqp = (struct ip_mreq_source *)i1; 10934 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 10935 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 10936 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 10937 } 10938 10939 /* 10940 * In the multirouting case, we need to replicate 10941 * the request as noted in the mcast cases above. 10942 */ 10943 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 10944 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10945 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 10946 if (ire != NULL) { 10947 if (ire->ire_flags & RTF_MULTIRT) { 10948 error = ip_multirt_apply_membership( 10949 optfn, ire, connp, checkonly, grp, 10950 fmode, src, first_mp); 10951 done = B_TRUE; 10952 } 10953 ire_refrele(ire); 10954 } 10955 if (!done) { 10956 error = optfn(connp, checkonly, grp, ifaddr, 10957 &ifindex, fmode, src, first_mp); 10958 } 10959 if (error != 0) { 10960 /* 10961 * EINPROGRESS is a soft error, needs retry 10962 * so don't make *outlenp zero. 10963 */ 10964 if (error != EINPROGRESS) 10965 *outlenp = 0; 10966 return (error); 10967 } 10968 /* OK return - copy input buffer into output buffer */ 10969 if (invalp != outvalp) { 10970 bcopy(invalp, outvalp, inlen); 10971 } 10972 *outlenp = inlen; 10973 return (0); 10974 } 10975 case IP_SEC_OPT: 10976 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 10977 if (error != 0) { 10978 *outlenp = 0; 10979 return (error); 10980 } 10981 break; 10982 case IP_HDRINCL: 10983 case IP_OPTIONS: 10984 case T_IP_OPTIONS: 10985 case IP_TOS: 10986 case T_IP_TOS: 10987 case IP_TTL: 10988 case IP_RECVDSTADDR: 10989 case IP_RECVOPTS: 10990 /* OK return - copy input buffer into output buffer */ 10991 if (invalp != outvalp) { 10992 /* don't trust bcopy for identical src/dst */ 10993 bcopy(invalp, outvalp, inlen); 10994 } 10995 *outlenp = inlen; 10996 return (0); 10997 case IP_RECVIF: 10998 /* Retrieve the inbound interface index */ 10999 if (!checkonly) { 11000 mutex_enter(&connp->conn_lock); 11001 connp->conn_recvif = *i1 ? 1 : 0; 11002 mutex_exit(&connp->conn_lock); 11003 } 11004 break; /* goto sizeof (int) option return */ 11005 case IP_RECVPKTINFO: 11006 if (!checkonly) { 11007 mutex_enter(&connp->conn_lock); 11008 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11009 mutex_exit(&connp->conn_lock); 11010 } 11011 break; /* goto sizeof (int) option return */ 11012 case IP_RECVSLLA: 11013 /* Retrieve the source link layer address */ 11014 if (!checkonly) { 11015 mutex_enter(&connp->conn_lock); 11016 connp->conn_recvslla = *i1 ? 1 : 0; 11017 mutex_exit(&connp->conn_lock); 11018 } 11019 break; /* goto sizeof (int) option return */ 11020 case MRT_INIT: 11021 case MRT_DONE: 11022 case MRT_ADD_VIF: 11023 case MRT_DEL_VIF: 11024 case MRT_ADD_MFC: 11025 case MRT_DEL_MFC: 11026 case MRT_ASSERT: 11027 if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) { 11028 *outlenp = 0; 11029 return (error); 11030 } 11031 error = ip_mrouter_set((int)name, q, checkonly, 11032 (uchar_t *)invalp, inlen, first_mp); 11033 if (error) { 11034 *outlenp = 0; 11035 return (error); 11036 } 11037 /* OK return - copy input buffer into output buffer */ 11038 if (invalp != outvalp) { 11039 /* don't trust bcopy for identical src/dst */ 11040 bcopy(invalp, outvalp, inlen); 11041 } 11042 *outlenp = inlen; 11043 return (0); 11044 case IP_BOUND_IF: 11045 case IP_DHCPINIT_IF: 11046 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11047 level, name, first_mp); 11048 if (error != 0) 11049 return (error); 11050 break; /* goto sizeof (int) option return */ 11051 11052 case IP_UNSPEC_SRC: 11053 /* Allow sending with a zero source address */ 11054 if (!checkonly) { 11055 mutex_enter(&connp->conn_lock); 11056 connp->conn_unspec_src = *i1 ? 1 : 0; 11057 mutex_exit(&connp->conn_lock); 11058 } 11059 break; /* goto sizeof (int) option return */ 11060 default: 11061 /* 11062 * "soft" error (negative) 11063 * option not handled at this level 11064 * Note: Do not modify *outlenp 11065 */ 11066 return (-EINVAL); 11067 } 11068 break; 11069 case IPPROTO_IPV6: 11070 switch (name) { 11071 case IPV6_BOUND_IF: 11072 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11073 level, name, first_mp); 11074 if (error != 0) 11075 return (error); 11076 break; /* goto sizeof (int) option return */ 11077 11078 case IPV6_MULTICAST_IF: 11079 /* 11080 * The only possible errors are EINPROGRESS and 11081 * EINVAL. EINPROGRESS will be restarted and is not 11082 * a hard error. We call this option on both V4 and V6 11083 * If both return EINVAL, then this call returns 11084 * EINVAL. If at least one of them succeeds we 11085 * return success. 11086 */ 11087 found = B_FALSE; 11088 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11089 level, name, first_mp); 11090 if (error == EINPROGRESS) 11091 return (error); 11092 if (error == 0) 11093 found = B_TRUE; 11094 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11095 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 11096 if (error == 0) 11097 found = B_TRUE; 11098 if (!found) 11099 return (error); 11100 break; /* goto sizeof (int) option return */ 11101 11102 case IPV6_MULTICAST_HOPS: 11103 /* Recorded in transport above IP */ 11104 break; /* goto sizeof (int) option return */ 11105 case IPV6_MULTICAST_LOOP: 11106 if (!checkonly) { 11107 mutex_enter(&connp->conn_lock); 11108 connp->conn_multicast_loop = *i1; 11109 mutex_exit(&connp->conn_lock); 11110 } 11111 break; /* goto sizeof (int) option return */ 11112 case IPV6_JOIN_GROUP: 11113 case MCAST_JOIN_GROUP: 11114 case IPV6_LEAVE_GROUP: 11115 case MCAST_LEAVE_GROUP: { 11116 struct ipv6_mreq *ip_mreqp; 11117 struct group_req *greqp; 11118 ire_t *ire; 11119 boolean_t done = B_FALSE; 11120 in6_addr_t groupv6; 11121 uint32_t ifindex; 11122 boolean_t mcast_opt = B_TRUE; 11123 mcast_record_t fmode; 11124 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11125 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11126 11127 switch (name) { 11128 case IPV6_JOIN_GROUP: 11129 mcast_opt = B_FALSE; 11130 /* FALLTHRU */ 11131 case MCAST_JOIN_GROUP: 11132 fmode = MODE_IS_EXCLUDE; 11133 optfn = ip_opt_add_group_v6; 11134 break; 11135 11136 case IPV6_LEAVE_GROUP: 11137 mcast_opt = B_FALSE; 11138 /* FALLTHRU */ 11139 case MCAST_LEAVE_GROUP: 11140 fmode = MODE_IS_INCLUDE; 11141 optfn = ip_opt_delete_group_v6; 11142 break; 11143 } 11144 11145 if (mcast_opt) { 11146 struct sockaddr_in *sin; 11147 struct sockaddr_in6 *sin6; 11148 greqp = (struct group_req *)i1; 11149 if (greqp->gr_group.ss_family == AF_INET) { 11150 sin = (struct sockaddr_in *) 11151 &(greqp->gr_group); 11152 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 11153 &groupv6); 11154 } else { 11155 sin6 = (struct sockaddr_in6 *) 11156 &(greqp->gr_group); 11157 groupv6 = sin6->sin6_addr; 11158 } 11159 ifindex = greqp->gr_interface; 11160 } else { 11161 ip_mreqp = (struct ipv6_mreq *)i1; 11162 groupv6 = ip_mreqp->ipv6mr_multiaddr; 11163 ifindex = ip_mreqp->ipv6mr_interface; 11164 } 11165 /* 11166 * In the multirouting case, we need to replicate 11167 * the request on all interfaces that will take part 11168 * in replication. We do so because multirouting is 11169 * reflective, thus we will probably receive multi- 11170 * casts on those interfaces. 11171 * The ip_multirt_apply_membership_v6() succeeds if 11172 * the operation succeeds on at least one interface. 11173 */ 11174 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 11175 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11176 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11177 if (ire != NULL) { 11178 if (ire->ire_flags & RTF_MULTIRT) { 11179 error = ip_multirt_apply_membership_v6( 11180 optfn, ire, connp, checkonly, 11181 &groupv6, fmode, &ipv6_all_zeros, 11182 first_mp); 11183 done = B_TRUE; 11184 } 11185 ire_refrele(ire); 11186 } 11187 if (!done) { 11188 error = optfn(connp, checkonly, &groupv6, 11189 ifindex, fmode, &ipv6_all_zeros, first_mp); 11190 } 11191 if (error) { 11192 /* 11193 * EINPROGRESS is a soft error, needs retry 11194 * so don't make *outlenp zero. 11195 */ 11196 if (error != EINPROGRESS) 11197 *outlenp = 0; 11198 return (error); 11199 } 11200 /* OK return - copy input buffer into output buffer */ 11201 if (invalp != outvalp) { 11202 /* don't trust bcopy for identical src/dst */ 11203 bcopy(invalp, outvalp, inlen); 11204 } 11205 *outlenp = inlen; 11206 return (0); 11207 } 11208 case MCAST_BLOCK_SOURCE: 11209 case MCAST_UNBLOCK_SOURCE: 11210 case MCAST_JOIN_SOURCE_GROUP: 11211 case MCAST_LEAVE_SOURCE_GROUP: { 11212 struct group_source_req *gsreqp; 11213 in6_addr_t v6grp, v6src; 11214 uint32_t ifindex; 11215 mcast_record_t fmode; 11216 ire_t *ire; 11217 boolean_t done = B_FALSE; 11218 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11219 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11220 11221 switch (name) { 11222 case MCAST_BLOCK_SOURCE: 11223 fmode = MODE_IS_EXCLUDE; 11224 optfn = ip_opt_add_group_v6; 11225 break; 11226 case MCAST_UNBLOCK_SOURCE: 11227 fmode = MODE_IS_EXCLUDE; 11228 optfn = ip_opt_delete_group_v6; 11229 break; 11230 case MCAST_JOIN_SOURCE_GROUP: 11231 fmode = MODE_IS_INCLUDE; 11232 optfn = ip_opt_add_group_v6; 11233 break; 11234 case MCAST_LEAVE_SOURCE_GROUP: 11235 fmode = MODE_IS_INCLUDE; 11236 optfn = ip_opt_delete_group_v6; 11237 break; 11238 } 11239 11240 gsreqp = (struct group_source_req *)i1; 11241 ifindex = gsreqp->gsr_interface; 11242 if (gsreqp->gsr_group.ss_family == AF_INET) { 11243 struct sockaddr_in *s; 11244 s = (struct sockaddr_in *)&gsreqp->gsr_group; 11245 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 11246 s = (struct sockaddr_in *)&gsreqp->gsr_source; 11247 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 11248 } else { 11249 struct sockaddr_in6 *s6; 11250 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 11251 v6grp = s6->sin6_addr; 11252 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 11253 v6src = s6->sin6_addr; 11254 } 11255 11256 /* 11257 * In the multirouting case, we need to replicate 11258 * the request as noted in the mcast cases above. 11259 */ 11260 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 11261 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11262 MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); 11263 if (ire != NULL) { 11264 if (ire->ire_flags & RTF_MULTIRT) { 11265 error = ip_multirt_apply_membership_v6( 11266 optfn, ire, connp, checkonly, 11267 &v6grp, fmode, &v6src, first_mp); 11268 done = B_TRUE; 11269 } 11270 ire_refrele(ire); 11271 } 11272 if (!done) { 11273 error = optfn(connp, checkonly, &v6grp, 11274 ifindex, fmode, &v6src, first_mp); 11275 } 11276 if (error != 0) { 11277 /* 11278 * EINPROGRESS is a soft error, needs retry 11279 * so don't make *outlenp zero. 11280 */ 11281 if (error != EINPROGRESS) 11282 *outlenp = 0; 11283 return (error); 11284 } 11285 /* OK return - copy input buffer into output buffer */ 11286 if (invalp != outvalp) { 11287 bcopy(invalp, outvalp, inlen); 11288 } 11289 *outlenp = inlen; 11290 return (0); 11291 } 11292 case IPV6_UNICAST_HOPS: 11293 /* Recorded in transport above IP */ 11294 break; /* goto sizeof (int) option return */ 11295 case IPV6_UNSPEC_SRC: 11296 /* Allow sending with a zero source address */ 11297 if (!checkonly) { 11298 mutex_enter(&connp->conn_lock); 11299 connp->conn_unspec_src = *i1 ? 1 : 0; 11300 mutex_exit(&connp->conn_lock); 11301 } 11302 break; /* goto sizeof (int) option return */ 11303 case IPV6_RECVPKTINFO: 11304 if (!checkonly) { 11305 mutex_enter(&connp->conn_lock); 11306 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11307 mutex_exit(&connp->conn_lock); 11308 } 11309 break; /* goto sizeof (int) option return */ 11310 case IPV6_RECVTCLASS: 11311 if (!checkonly) { 11312 if (*i1 < 0 || *i1 > 1) { 11313 return (EINVAL); 11314 } 11315 mutex_enter(&connp->conn_lock); 11316 connp->conn_ipv6_recvtclass = *i1; 11317 mutex_exit(&connp->conn_lock); 11318 } 11319 break; 11320 case IPV6_RECVPATHMTU: 11321 if (!checkonly) { 11322 if (*i1 < 0 || *i1 > 1) { 11323 return (EINVAL); 11324 } 11325 mutex_enter(&connp->conn_lock); 11326 connp->conn_ipv6_recvpathmtu = *i1; 11327 mutex_exit(&connp->conn_lock); 11328 } 11329 break; 11330 case IPV6_RECVHOPLIMIT: 11331 if (!checkonly) { 11332 mutex_enter(&connp->conn_lock); 11333 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 11334 mutex_exit(&connp->conn_lock); 11335 } 11336 break; /* goto sizeof (int) option return */ 11337 case IPV6_RECVHOPOPTS: 11338 if (!checkonly) { 11339 mutex_enter(&connp->conn_lock); 11340 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 11341 mutex_exit(&connp->conn_lock); 11342 } 11343 break; /* goto sizeof (int) option return */ 11344 case IPV6_RECVDSTOPTS: 11345 if (!checkonly) { 11346 mutex_enter(&connp->conn_lock); 11347 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 11348 mutex_exit(&connp->conn_lock); 11349 } 11350 break; /* goto sizeof (int) option return */ 11351 case IPV6_RECVRTHDR: 11352 if (!checkonly) { 11353 mutex_enter(&connp->conn_lock); 11354 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 11355 mutex_exit(&connp->conn_lock); 11356 } 11357 break; /* goto sizeof (int) option return */ 11358 case IPV6_RECVRTHDRDSTOPTS: 11359 if (!checkonly) { 11360 mutex_enter(&connp->conn_lock); 11361 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 11362 mutex_exit(&connp->conn_lock); 11363 } 11364 break; /* goto sizeof (int) option return */ 11365 case IPV6_PKTINFO: 11366 if (inlen == 0) 11367 return (-EINVAL); /* clearing option */ 11368 error = ip6_set_pktinfo(cr, connp, 11369 (struct in6_pktinfo *)invalp); 11370 if (error != 0) 11371 *outlenp = 0; 11372 else 11373 *outlenp = inlen; 11374 return (error); 11375 case IPV6_NEXTHOP: { 11376 struct sockaddr_in6 *sin6; 11377 11378 /* Verify that the nexthop is reachable */ 11379 if (inlen == 0) 11380 return (-EINVAL); /* clearing option */ 11381 11382 sin6 = (struct sockaddr_in6 *)invalp; 11383 ire = ire_route_lookup_v6(&sin6->sin6_addr, 11384 0, 0, 0, NULL, NULL, connp->conn_zoneid, 11385 NULL, MATCH_IRE_DEFAULT, ipst); 11386 11387 if (ire == NULL) { 11388 *outlenp = 0; 11389 return (EHOSTUNREACH); 11390 } 11391 ire_refrele(ire); 11392 return (-EINVAL); 11393 } 11394 case IPV6_SEC_OPT: 11395 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11396 if (error != 0) { 11397 *outlenp = 0; 11398 return (error); 11399 } 11400 break; 11401 case IPV6_SRC_PREFERENCES: { 11402 /* 11403 * This is implemented strictly in the ip module 11404 * (here and in tcp_opt_*() to accomodate tcp 11405 * sockets). Modules above ip pass this option 11406 * down here since ip is the only one that needs to 11407 * be aware of source address preferences. 11408 * 11409 * This socket option only affects connected 11410 * sockets that haven't already bound to a specific 11411 * IPv6 address. In other words, sockets that 11412 * don't call bind() with an address other than the 11413 * unspecified address and that call connect(). 11414 * ip_bind_connected_v6() passes these preferences 11415 * to the ipif_select_source_v6() function. 11416 */ 11417 if (inlen != sizeof (uint32_t)) 11418 return (EINVAL); 11419 error = ip6_set_src_preferences(connp, 11420 *(uint32_t *)invalp); 11421 if (error != 0) { 11422 *outlenp = 0; 11423 return (error); 11424 } else { 11425 *outlenp = sizeof (uint32_t); 11426 } 11427 break; 11428 } 11429 case IPV6_V6ONLY: 11430 if (*i1 < 0 || *i1 > 1) { 11431 return (EINVAL); 11432 } 11433 mutex_enter(&connp->conn_lock); 11434 connp->conn_ipv6_v6only = *i1; 11435 mutex_exit(&connp->conn_lock); 11436 break; 11437 default: 11438 return (-EINVAL); 11439 } 11440 break; 11441 default: 11442 /* 11443 * "soft" error (negative) 11444 * option not handled at this level 11445 * Note: Do not modify *outlenp 11446 */ 11447 return (-EINVAL); 11448 } 11449 /* 11450 * Common case of return from an option that is sizeof (int) 11451 */ 11452 *(int *)outvalp = *i1; 11453 *outlenp = sizeof (int); 11454 return (0); 11455 } 11456 11457 /* 11458 * This routine gets default values of certain options whose default 11459 * values are maintained by protocol specific code 11460 */ 11461 /* ARGSUSED */ 11462 int 11463 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 11464 { 11465 int *i1 = (int *)ptr; 11466 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11467 11468 switch (level) { 11469 case IPPROTO_IP: 11470 switch (name) { 11471 case IP_MULTICAST_TTL: 11472 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 11473 return (sizeof (uchar_t)); 11474 case IP_MULTICAST_LOOP: 11475 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 11476 return (sizeof (uchar_t)); 11477 default: 11478 return (-1); 11479 } 11480 case IPPROTO_IPV6: 11481 switch (name) { 11482 case IPV6_UNICAST_HOPS: 11483 *i1 = ipst->ips_ipv6_def_hops; 11484 return (sizeof (int)); 11485 case IPV6_MULTICAST_HOPS: 11486 *i1 = IP_DEFAULT_MULTICAST_TTL; 11487 return (sizeof (int)); 11488 case IPV6_MULTICAST_LOOP: 11489 *i1 = IP_DEFAULT_MULTICAST_LOOP; 11490 return (sizeof (int)); 11491 case IPV6_V6ONLY: 11492 *i1 = 1; 11493 return (sizeof (int)); 11494 default: 11495 return (-1); 11496 } 11497 default: 11498 return (-1); 11499 } 11500 /* NOTREACHED */ 11501 } 11502 11503 /* 11504 * Given a destination address and a pointer to where to put the information 11505 * this routine fills in the mtuinfo. 11506 */ 11507 int 11508 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 11509 struct ip6_mtuinfo *mtuinfo, netstack_t *ns) 11510 { 11511 ire_t *ire; 11512 ip_stack_t *ipst = ns->netstack_ip; 11513 11514 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 11515 return (-1); 11516 11517 bzero(mtuinfo, sizeof (*mtuinfo)); 11518 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11519 mtuinfo->ip6m_addr.sin6_port = port; 11520 mtuinfo->ip6m_addr.sin6_addr = *in6; 11521 11522 ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL, ipst); 11523 if (ire != NULL) { 11524 mtuinfo->ip6m_mtu = ire->ire_max_frag; 11525 ire_refrele(ire); 11526 } else { 11527 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 11528 } 11529 return (sizeof (struct ip6_mtuinfo)); 11530 } 11531 11532 /* 11533 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 11534 * checking of cred and that ip_g_mrouter is set should be done and 11535 * isn't. This doesn't matter as the error checking is done properly for the 11536 * other MRT options coming in through ip_opt_set. 11537 */ 11538 int 11539 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 11540 { 11541 conn_t *connp = Q_TO_CONN(q); 11542 ipsec_req_t *req = (ipsec_req_t *)ptr; 11543 11544 switch (level) { 11545 case IPPROTO_IP: 11546 switch (name) { 11547 case MRT_VERSION: 11548 case MRT_ASSERT: 11549 (void) ip_mrouter_get(name, q, ptr); 11550 return (sizeof (int)); 11551 case IP_SEC_OPT: 11552 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 11553 case IP_NEXTHOP: 11554 if (connp->conn_nexthop_set) { 11555 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 11556 return (sizeof (ipaddr_t)); 11557 } else 11558 return (0); 11559 case IP_RECVPKTINFO: 11560 *(int *)ptr = connp->conn_ip_recvpktinfo ? 1: 0; 11561 return (sizeof (int)); 11562 default: 11563 break; 11564 } 11565 break; 11566 case IPPROTO_IPV6: 11567 switch (name) { 11568 case IPV6_SEC_OPT: 11569 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 11570 case IPV6_SRC_PREFERENCES: { 11571 return (ip6_get_src_preferences(connp, 11572 (uint32_t *)ptr)); 11573 } 11574 case IPV6_V6ONLY: 11575 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 11576 return (sizeof (int)); 11577 case IPV6_PATHMTU: 11578 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 11579 (struct ip6_mtuinfo *)ptr, connp->conn_netstack)); 11580 default: 11581 break; 11582 } 11583 break; 11584 default: 11585 break; 11586 } 11587 return (-1); 11588 } 11589 /* Named Dispatch routine to get a current value out of our parameter table. */ 11590 /* ARGSUSED */ 11591 static int 11592 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11593 { 11594 ipparam_t *ippa = (ipparam_t *)cp; 11595 11596 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 11597 return (0); 11598 } 11599 11600 /* ARGSUSED */ 11601 static int 11602 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11603 { 11604 11605 (void) mi_mpprintf(mp, "%d", *(int *)cp); 11606 return (0); 11607 } 11608 11609 /* 11610 * Set ip{,6}_forwarding values. This means walking through all of the 11611 * ill's and toggling their forwarding values. 11612 */ 11613 /* ARGSUSED */ 11614 static int 11615 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11616 { 11617 long new_value; 11618 int *forwarding_value = (int *)cp; 11619 ill_t *ill; 11620 boolean_t isv6; 11621 ill_walk_context_t ctx; 11622 ip_stack_t *ipst = CONNQ_TO_IPST(q); 11623 11624 isv6 = (forwarding_value == &ipst->ips_ipv6_forward); 11625 11626 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11627 new_value < 0 || new_value > 1) { 11628 return (EINVAL); 11629 } 11630 11631 *forwarding_value = new_value; 11632 11633 /* 11634 * Regardless of the current value of ip_forwarding, set all per-ill 11635 * values of ip_forwarding to the value being set. 11636 * 11637 * Bring all the ill's up to date with the new global value. 11638 */ 11639 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 11640 11641 if (isv6) 11642 ill = ILL_START_WALK_V6(&ctx, ipst); 11643 else 11644 ill = ILL_START_WALK_V4(&ctx, ipst); 11645 11646 for (; ill != NULL; ill = ill_next(&ctx, ill)) 11647 (void) ill_forward_set(ill, new_value != 0); 11648 11649 rw_exit(&ipst->ips_ill_g_lock); 11650 return (0); 11651 } 11652 11653 /* 11654 * Walk through the param array specified registering each element with the 11655 * Named Dispatch handler. This is called only during init. So it is ok 11656 * not to acquire any locks 11657 */ 11658 static boolean_t 11659 ip_param_register(IDP *ndp, ipparam_t *ippa, size_t ippa_cnt, 11660 ipndp_t *ipnd, size_t ipnd_cnt) 11661 { 11662 for (; ippa_cnt-- > 0; ippa++) { 11663 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 11664 if (!nd_load(ndp, ippa->ip_param_name, 11665 ip_param_get, ip_param_set, (caddr_t)ippa)) { 11666 nd_free(ndp); 11667 return (B_FALSE); 11668 } 11669 } 11670 } 11671 11672 for (; ipnd_cnt-- > 0; ipnd++) { 11673 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 11674 if (!nd_load(ndp, ipnd->ip_ndp_name, 11675 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 11676 ipnd->ip_ndp_data)) { 11677 nd_free(ndp); 11678 return (B_FALSE); 11679 } 11680 } 11681 } 11682 11683 return (B_TRUE); 11684 } 11685 11686 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 11687 /* ARGSUSED */ 11688 static int 11689 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11690 { 11691 long new_value; 11692 ipparam_t *ippa = (ipparam_t *)cp; 11693 11694 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11695 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 11696 return (EINVAL); 11697 } 11698 ippa->ip_param_value = new_value; 11699 return (0); 11700 } 11701 11702 /* 11703 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 11704 * When an ipf is passed here for the first time, if 11705 * we already have in-order fragments on the queue, we convert from the fast- 11706 * path reassembly scheme to the hard-case scheme. From then on, additional 11707 * fragments are reassembled here. We keep track of the start and end offsets 11708 * of each piece, and the number of holes in the chain. When the hole count 11709 * goes to zero, we are done! 11710 * 11711 * The ipf_count will be updated to account for any mblk(s) added (pointed to 11712 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 11713 * ipfb_count and ill_frag_count by the difference of ipf_count before and 11714 * after the call to ip_reassemble(). 11715 */ 11716 int 11717 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 11718 size_t msg_len) 11719 { 11720 uint_t end; 11721 mblk_t *next_mp; 11722 mblk_t *mp1; 11723 uint_t offset; 11724 boolean_t incr_dups = B_TRUE; 11725 boolean_t offset_zero_seen = B_FALSE; 11726 boolean_t pkt_boundary_checked = B_FALSE; 11727 11728 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 11729 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 11730 11731 /* Add in byte count */ 11732 ipf->ipf_count += msg_len; 11733 if (ipf->ipf_end) { 11734 /* 11735 * We were part way through in-order reassembly, but now there 11736 * is a hole. We walk through messages already queued, and 11737 * mark them for hard case reassembly. We know that up till 11738 * now they were in order starting from offset zero. 11739 */ 11740 offset = 0; 11741 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 11742 IP_REASS_SET_START(mp1, offset); 11743 if (offset == 0) { 11744 ASSERT(ipf->ipf_nf_hdr_len != 0); 11745 offset = -ipf->ipf_nf_hdr_len; 11746 } 11747 offset += mp1->b_wptr - mp1->b_rptr; 11748 IP_REASS_SET_END(mp1, offset); 11749 } 11750 /* One hole at the end. */ 11751 ipf->ipf_hole_cnt = 1; 11752 /* Brand it as a hard case, forever. */ 11753 ipf->ipf_end = 0; 11754 } 11755 /* Walk through all the new pieces. */ 11756 do { 11757 end = start + (mp->b_wptr - mp->b_rptr); 11758 /* 11759 * If start is 0, decrease 'end' only for the first mblk of 11760 * the fragment. Otherwise 'end' can get wrong value in the 11761 * second pass of the loop if first mblk is exactly the 11762 * size of ipf_nf_hdr_len. 11763 */ 11764 if (start == 0 && !offset_zero_seen) { 11765 /* First segment */ 11766 ASSERT(ipf->ipf_nf_hdr_len != 0); 11767 end -= ipf->ipf_nf_hdr_len; 11768 offset_zero_seen = B_TRUE; 11769 } 11770 next_mp = mp->b_cont; 11771 /* 11772 * We are checking to see if there is any interesing data 11773 * to process. If there isn't and the mblk isn't the 11774 * one which carries the unfragmentable header then we 11775 * drop it. It's possible to have just the unfragmentable 11776 * header come through without any data. That needs to be 11777 * saved. 11778 * 11779 * If the assert at the top of this function holds then the 11780 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 11781 * is infrequently traveled enough that the test is left in 11782 * to protect against future code changes which break that 11783 * invariant. 11784 */ 11785 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 11786 /* Empty. Blast it. */ 11787 IP_REASS_SET_START(mp, 0); 11788 IP_REASS_SET_END(mp, 0); 11789 /* 11790 * If the ipf points to the mblk we are about to free, 11791 * update ipf to point to the next mblk (or NULL 11792 * if none). 11793 */ 11794 if (ipf->ipf_mp->b_cont == mp) 11795 ipf->ipf_mp->b_cont = next_mp; 11796 freeb(mp); 11797 continue; 11798 } 11799 mp->b_cont = NULL; 11800 IP_REASS_SET_START(mp, start); 11801 IP_REASS_SET_END(mp, end); 11802 if (!ipf->ipf_tail_mp) { 11803 ipf->ipf_tail_mp = mp; 11804 ipf->ipf_mp->b_cont = mp; 11805 if (start == 0 || !more) { 11806 ipf->ipf_hole_cnt = 1; 11807 /* 11808 * if the first fragment comes in more than one 11809 * mblk, this loop will be executed for each 11810 * mblk. Need to adjust hole count so exiting 11811 * this routine will leave hole count at 1. 11812 */ 11813 if (next_mp) 11814 ipf->ipf_hole_cnt++; 11815 } else 11816 ipf->ipf_hole_cnt = 2; 11817 continue; 11818 } else if (ipf->ipf_last_frag_seen && !more && 11819 !pkt_boundary_checked) { 11820 /* 11821 * We check datagram boundary only if this fragment 11822 * claims to be the last fragment and we have seen a 11823 * last fragment in the past too. We do this only 11824 * once for a given fragment. 11825 * 11826 * start cannot be 0 here as fragments with start=0 11827 * and MF=0 gets handled as a complete packet. These 11828 * fragments should not reach here. 11829 */ 11830 11831 if (start + msgdsize(mp) != 11832 IP_REASS_END(ipf->ipf_tail_mp)) { 11833 /* 11834 * We have two fragments both of which claim 11835 * to be the last fragment but gives conflicting 11836 * information about the whole datagram size. 11837 * Something fishy is going on. Drop the 11838 * fragment and free up the reassembly list. 11839 */ 11840 return (IP_REASS_FAILED); 11841 } 11842 11843 /* 11844 * We shouldn't come to this code block again for this 11845 * particular fragment. 11846 */ 11847 pkt_boundary_checked = B_TRUE; 11848 } 11849 11850 /* New stuff at or beyond tail? */ 11851 offset = IP_REASS_END(ipf->ipf_tail_mp); 11852 if (start >= offset) { 11853 if (ipf->ipf_last_frag_seen) { 11854 /* current fragment is beyond last fragment */ 11855 return (IP_REASS_FAILED); 11856 } 11857 /* Link it on end. */ 11858 ipf->ipf_tail_mp->b_cont = mp; 11859 ipf->ipf_tail_mp = mp; 11860 if (more) { 11861 if (start != offset) 11862 ipf->ipf_hole_cnt++; 11863 } else if (start == offset && next_mp == NULL) 11864 ipf->ipf_hole_cnt--; 11865 continue; 11866 } 11867 mp1 = ipf->ipf_mp->b_cont; 11868 offset = IP_REASS_START(mp1); 11869 /* New stuff at the front? */ 11870 if (start < offset) { 11871 if (start == 0) { 11872 if (end >= offset) { 11873 /* Nailed the hole at the begining. */ 11874 ipf->ipf_hole_cnt--; 11875 } 11876 } else if (end < offset) { 11877 /* 11878 * A hole, stuff, and a hole where there used 11879 * to be just a hole. 11880 */ 11881 ipf->ipf_hole_cnt++; 11882 } 11883 mp->b_cont = mp1; 11884 /* Check for overlap. */ 11885 while (end > offset) { 11886 if (end < IP_REASS_END(mp1)) { 11887 mp->b_wptr -= end - offset; 11888 IP_REASS_SET_END(mp, offset); 11889 BUMP_MIB(ill->ill_ip_mib, 11890 ipIfStatsReasmPartDups); 11891 break; 11892 } 11893 /* Did we cover another hole? */ 11894 if ((mp1->b_cont && 11895 IP_REASS_END(mp1) != 11896 IP_REASS_START(mp1->b_cont) && 11897 end >= IP_REASS_START(mp1->b_cont)) || 11898 (!ipf->ipf_last_frag_seen && !more)) { 11899 ipf->ipf_hole_cnt--; 11900 } 11901 /* Clip out mp1. */ 11902 if ((mp->b_cont = mp1->b_cont) == NULL) { 11903 /* 11904 * After clipping out mp1, this guy 11905 * is now hanging off the end. 11906 */ 11907 ipf->ipf_tail_mp = mp; 11908 } 11909 IP_REASS_SET_START(mp1, 0); 11910 IP_REASS_SET_END(mp1, 0); 11911 /* Subtract byte count */ 11912 ipf->ipf_count -= mp1->b_datap->db_lim - 11913 mp1->b_datap->db_base; 11914 freeb(mp1); 11915 BUMP_MIB(ill->ill_ip_mib, 11916 ipIfStatsReasmPartDups); 11917 mp1 = mp->b_cont; 11918 if (!mp1) 11919 break; 11920 offset = IP_REASS_START(mp1); 11921 } 11922 ipf->ipf_mp->b_cont = mp; 11923 continue; 11924 } 11925 /* 11926 * The new piece starts somewhere between the start of the head 11927 * and before the end of the tail. 11928 */ 11929 for (; mp1; mp1 = mp1->b_cont) { 11930 offset = IP_REASS_END(mp1); 11931 if (start < offset) { 11932 if (end <= offset) { 11933 /* Nothing new. */ 11934 IP_REASS_SET_START(mp, 0); 11935 IP_REASS_SET_END(mp, 0); 11936 /* Subtract byte count */ 11937 ipf->ipf_count -= mp->b_datap->db_lim - 11938 mp->b_datap->db_base; 11939 if (incr_dups) { 11940 ipf->ipf_num_dups++; 11941 incr_dups = B_FALSE; 11942 } 11943 freeb(mp); 11944 BUMP_MIB(ill->ill_ip_mib, 11945 ipIfStatsReasmDuplicates); 11946 break; 11947 } 11948 /* 11949 * Trim redundant stuff off beginning of new 11950 * piece. 11951 */ 11952 IP_REASS_SET_START(mp, offset); 11953 mp->b_rptr += offset - start; 11954 BUMP_MIB(ill->ill_ip_mib, 11955 ipIfStatsReasmPartDups); 11956 start = offset; 11957 if (!mp1->b_cont) { 11958 /* 11959 * After trimming, this guy is now 11960 * hanging off the end. 11961 */ 11962 mp1->b_cont = mp; 11963 ipf->ipf_tail_mp = mp; 11964 if (!more) { 11965 ipf->ipf_hole_cnt--; 11966 } 11967 break; 11968 } 11969 } 11970 if (start >= IP_REASS_START(mp1->b_cont)) 11971 continue; 11972 /* Fill a hole */ 11973 if (start > offset) 11974 ipf->ipf_hole_cnt++; 11975 mp->b_cont = mp1->b_cont; 11976 mp1->b_cont = mp; 11977 mp1 = mp->b_cont; 11978 offset = IP_REASS_START(mp1); 11979 if (end >= offset) { 11980 ipf->ipf_hole_cnt--; 11981 /* Check for overlap. */ 11982 while (end > offset) { 11983 if (end < IP_REASS_END(mp1)) { 11984 mp->b_wptr -= end - offset; 11985 IP_REASS_SET_END(mp, offset); 11986 /* 11987 * TODO we might bump 11988 * this up twice if there is 11989 * overlap at both ends. 11990 */ 11991 BUMP_MIB(ill->ill_ip_mib, 11992 ipIfStatsReasmPartDups); 11993 break; 11994 } 11995 /* Did we cover another hole? */ 11996 if ((mp1->b_cont && 11997 IP_REASS_END(mp1) 11998 != IP_REASS_START(mp1->b_cont) && 11999 end >= 12000 IP_REASS_START(mp1->b_cont)) || 12001 (!ipf->ipf_last_frag_seen && 12002 !more)) { 12003 ipf->ipf_hole_cnt--; 12004 } 12005 /* Clip out mp1. */ 12006 if ((mp->b_cont = mp1->b_cont) == 12007 NULL) { 12008 /* 12009 * After clipping out mp1, 12010 * this guy is now hanging 12011 * off the end. 12012 */ 12013 ipf->ipf_tail_mp = mp; 12014 } 12015 IP_REASS_SET_START(mp1, 0); 12016 IP_REASS_SET_END(mp1, 0); 12017 /* Subtract byte count */ 12018 ipf->ipf_count -= 12019 mp1->b_datap->db_lim - 12020 mp1->b_datap->db_base; 12021 freeb(mp1); 12022 BUMP_MIB(ill->ill_ip_mib, 12023 ipIfStatsReasmPartDups); 12024 mp1 = mp->b_cont; 12025 if (!mp1) 12026 break; 12027 offset = IP_REASS_START(mp1); 12028 } 12029 } 12030 break; 12031 } 12032 } while (start = end, mp = next_mp); 12033 12034 /* Fragment just processed could be the last one. Remember this fact */ 12035 if (!more) 12036 ipf->ipf_last_frag_seen = B_TRUE; 12037 12038 /* Still got holes? */ 12039 if (ipf->ipf_hole_cnt) 12040 return (IP_REASS_PARTIAL); 12041 /* Clean up overloaded fields to avoid upstream disasters. */ 12042 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12043 IP_REASS_SET_START(mp1, 0); 12044 IP_REASS_SET_END(mp1, 0); 12045 } 12046 return (IP_REASS_COMPLETE); 12047 } 12048 12049 /* 12050 * ipsec processing for the fast path, used for input UDP Packets 12051 * Returns true if ready for passup to UDP. 12052 * Return false if packet is not passable to UDP (e.g. it failed IPsec policy, 12053 * was an ESP-in-UDP packet, etc.). 12054 */ 12055 static boolean_t 12056 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 12057 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present, ire_t *ire) 12058 { 12059 uint32_t ill_index; 12060 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 12061 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 12062 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 12063 udp_t *udp = connp->conn_udp; 12064 12065 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12066 /* The ill_index of the incoming ILL */ 12067 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 12068 12069 /* pass packet up to the transport */ 12070 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 12071 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 12072 NULL, mctl_present); 12073 if (*first_mpp == NULL) { 12074 return (B_FALSE); 12075 } 12076 } 12077 12078 /* Initiate IPPF processing for fastpath UDP */ 12079 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 12080 ip_process(IPP_LOCAL_IN, mpp, ill_index); 12081 if (*mpp == NULL) { 12082 ip2dbg(("ip_input_ipsec_process: UDP pkt " 12083 "deferred/dropped during IPPF processing\n")); 12084 return (B_FALSE); 12085 } 12086 } 12087 /* 12088 * Remove 0-spi if it's 0, or move everything behind 12089 * the UDP header over it and forward to ESP via 12090 * ip_proto_input(). 12091 */ 12092 if (udp->udp_nat_t_endpoint) { 12093 if (mctl_present) { 12094 /* mctl_present *shouldn't* happen. */ 12095 ip_drop_packet(*first_mpp, B_TRUE, NULL, 12096 NULL, DROPPER(ipss, ipds_esp_nat_t_ipsec), 12097 &ipss->ipsec_dropper); 12098 *first_mpp = NULL; 12099 return (B_FALSE); 12100 } 12101 12102 /* "ill" is "recv_ill" in actuality. */ 12103 if (!zero_spi_check(q, *mpp, ire, ill, ipss)) 12104 return (B_FALSE); 12105 12106 /* Else continue like a normal UDP packet. */ 12107 } 12108 12109 /* 12110 * We make the checks as below since we are in the fast path 12111 * and want to minimize the number of checks if the IP_RECVIF and/or 12112 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 12113 */ 12114 if (connp->conn_recvif || connp->conn_recvslla || 12115 connp->conn_ip_recvpktinfo) { 12116 if (connp->conn_recvif) { 12117 in_flags = IPF_RECVIF; 12118 } 12119 /* 12120 * UDP supports IP_RECVPKTINFO option for both v4 and v6 12121 * so the flag passed to ip_add_info is based on IP version 12122 * of connp. 12123 */ 12124 if (connp->conn_ip_recvpktinfo) { 12125 if (connp->conn_af_isv6) { 12126 /* 12127 * V6 only needs index 12128 */ 12129 in_flags |= IPF_RECVIF; 12130 } else { 12131 /* 12132 * V4 needs index + matching address. 12133 */ 12134 in_flags |= IPF_RECVADDR; 12135 } 12136 } 12137 if (connp->conn_recvslla) { 12138 in_flags |= IPF_RECVSLLA; 12139 } 12140 /* 12141 * since in_flags are being set ill will be 12142 * referenced in ip_add_info, so it better not 12143 * be NULL. 12144 */ 12145 /* 12146 * the actual data will be contained in b_cont 12147 * upon successful return of the following call. 12148 * If the call fails then the original mblk is 12149 * returned. 12150 */ 12151 *mpp = ip_add_info(*mpp, ill, in_flags, IPCL_ZONEID(connp), 12152 ipst); 12153 } 12154 12155 return (B_TRUE); 12156 } 12157 12158 /* 12159 * Fragmentation reassembly. Each ILL has a hash table for 12160 * queuing packets undergoing reassembly for all IPIFs 12161 * associated with the ILL. The hash is based on the packet 12162 * IP ident field. The ILL frag hash table was allocated 12163 * as a timer block at the time the ILL was created. Whenever 12164 * there is anything on the reassembly queue, the timer will 12165 * be running. Returns B_TRUE if successful else B_FALSE; 12166 * frees mp on failure. 12167 */ 12168 static boolean_t 12169 ip_rput_fragment(ill_t *ill, ill_t *recv_ill, mblk_t **mpp, ipha_t *ipha, 12170 uint32_t *cksum_val, uint16_t *cksum_flags) 12171 { 12172 uint32_t frag_offset_flags; 12173 mblk_t *mp = *mpp; 12174 mblk_t *t_mp; 12175 ipaddr_t dst; 12176 uint8_t proto = ipha->ipha_protocol; 12177 uint32_t sum_val; 12178 uint16_t sum_flags; 12179 ipf_t *ipf; 12180 ipf_t **ipfp; 12181 ipfb_t *ipfb; 12182 uint16_t ident; 12183 uint32_t offset; 12184 ipaddr_t src; 12185 uint_t hdr_length; 12186 uint32_t end; 12187 mblk_t *mp1; 12188 mblk_t *tail_mp; 12189 size_t count; 12190 size_t msg_len; 12191 uint8_t ecn_info = 0; 12192 uint32_t packet_size; 12193 boolean_t pruned = B_FALSE; 12194 ip_stack_t *ipst = ill->ill_ipst; 12195 12196 if (cksum_val != NULL) 12197 *cksum_val = 0; 12198 if (cksum_flags != NULL) 12199 *cksum_flags = 0; 12200 12201 /* 12202 * Drop the fragmented as early as possible, if 12203 * we don't have resource(s) to re-assemble. 12204 */ 12205 if (ipst->ips_ip_reass_queue_bytes == 0) { 12206 freemsg(mp); 12207 return (B_FALSE); 12208 } 12209 12210 /* Check for fragmentation offset; return if there's none */ 12211 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 12212 (IPH_MF | IPH_OFFSET)) == 0) 12213 return (B_TRUE); 12214 12215 /* 12216 * We utilize hardware computed checksum info only for UDP since 12217 * IP fragmentation is a normal occurrence for the protocol. In 12218 * addition, checksum offload support for IP fragments carrying 12219 * UDP payload is commonly implemented across network adapters. 12220 */ 12221 ASSERT(recv_ill != NULL); 12222 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(recv_ill) && 12223 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 12224 mblk_t *mp1 = mp->b_cont; 12225 int32_t len; 12226 12227 /* Record checksum information from the packet */ 12228 sum_val = (uint32_t)DB_CKSUM16(mp); 12229 sum_flags = DB_CKSUMFLAGS(mp); 12230 12231 /* IP payload offset from beginning of mblk */ 12232 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 12233 12234 if ((sum_flags & HCK_PARTIALCKSUM) && 12235 (mp1 == NULL || mp1->b_cont == NULL) && 12236 offset >= DB_CKSUMSTART(mp) && 12237 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 12238 uint32_t adj; 12239 /* 12240 * Partial checksum has been calculated by hardware 12241 * and attached to the packet; in addition, any 12242 * prepended extraneous data is even byte aligned. 12243 * If any such data exists, we adjust the checksum; 12244 * this would also handle any postpended data. 12245 */ 12246 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 12247 mp, mp1, len, adj); 12248 12249 /* One's complement subtract extraneous checksum */ 12250 if (adj >= sum_val) 12251 sum_val = ~(adj - sum_val) & 0xFFFF; 12252 else 12253 sum_val -= adj; 12254 } 12255 } else { 12256 sum_val = 0; 12257 sum_flags = 0; 12258 } 12259 12260 /* Clear hardware checksumming flag */ 12261 DB_CKSUMFLAGS(mp) = 0; 12262 12263 ident = ipha->ipha_ident; 12264 offset = (frag_offset_flags << 3) & 0xFFFF; 12265 src = ipha->ipha_src; 12266 dst = ipha->ipha_dst; 12267 hdr_length = IPH_HDR_LENGTH(ipha); 12268 end = ntohs(ipha->ipha_length) - hdr_length; 12269 12270 /* If end == 0 then we have a packet with no data, so just free it */ 12271 if (end == 0) { 12272 freemsg(mp); 12273 return (B_FALSE); 12274 } 12275 12276 /* Record the ECN field info. */ 12277 ecn_info = (ipha->ipha_type_of_service & 0x3); 12278 if (offset != 0) { 12279 /* 12280 * If this isn't the first piece, strip the header, and 12281 * add the offset to the end value. 12282 */ 12283 mp->b_rptr += hdr_length; 12284 end += offset; 12285 } 12286 12287 msg_len = MBLKSIZE(mp); 12288 tail_mp = mp; 12289 while (tail_mp->b_cont != NULL) { 12290 tail_mp = tail_mp->b_cont; 12291 msg_len += MBLKSIZE(tail_mp); 12292 } 12293 12294 /* If the reassembly list for this ILL will get too big, prune it */ 12295 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 12296 ipst->ips_ip_reass_queue_bytes) { 12297 ill_frag_prune(ill, 12298 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 : 12299 (ipst->ips_ip_reass_queue_bytes - msg_len)); 12300 pruned = B_TRUE; 12301 } 12302 12303 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 12304 mutex_enter(&ipfb->ipfb_lock); 12305 12306 ipfp = &ipfb->ipfb_ipf; 12307 /* Try to find an existing fragment queue for this packet. */ 12308 for (;;) { 12309 ipf = ipfp[0]; 12310 if (ipf != NULL) { 12311 /* 12312 * It has to match on ident and src/dst address. 12313 */ 12314 if (ipf->ipf_ident == ident && 12315 ipf->ipf_src == src && 12316 ipf->ipf_dst == dst && 12317 ipf->ipf_protocol == proto) { 12318 /* 12319 * If we have received too many 12320 * duplicate fragments for this packet 12321 * free it. 12322 */ 12323 if (ipf->ipf_num_dups > ip_max_frag_dups) { 12324 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12325 freemsg(mp); 12326 mutex_exit(&ipfb->ipfb_lock); 12327 return (B_FALSE); 12328 } 12329 /* Found it. */ 12330 break; 12331 } 12332 ipfp = &ipf->ipf_hash_next; 12333 continue; 12334 } 12335 12336 /* 12337 * If we pruned the list, do we want to store this new 12338 * fragment?. We apply an optimization here based on the 12339 * fact that most fragments will be received in order. 12340 * So if the offset of this incoming fragment is zero, 12341 * it is the first fragment of a new packet. We will 12342 * keep it. Otherwise drop the fragment, as we have 12343 * probably pruned the packet already (since the 12344 * packet cannot be found). 12345 */ 12346 if (pruned && offset != 0) { 12347 mutex_exit(&ipfb->ipfb_lock); 12348 freemsg(mp); 12349 return (B_FALSE); 12350 } 12351 12352 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) { 12353 /* 12354 * Too many fragmented packets in this hash 12355 * bucket. Free the oldest. 12356 */ 12357 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 12358 } 12359 12360 /* New guy. Allocate a frag message. */ 12361 mp1 = allocb(sizeof (*ipf), BPRI_MED); 12362 if (mp1 == NULL) { 12363 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12364 freemsg(mp); 12365 reass_done: 12366 mutex_exit(&ipfb->ipfb_lock); 12367 return (B_FALSE); 12368 } 12369 12370 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); 12371 mp1->b_cont = mp; 12372 12373 /* Initialize the fragment header. */ 12374 ipf = (ipf_t *)mp1->b_rptr; 12375 ipf->ipf_mp = mp1; 12376 ipf->ipf_ptphn = ipfp; 12377 ipfp[0] = ipf; 12378 ipf->ipf_hash_next = NULL; 12379 ipf->ipf_ident = ident; 12380 ipf->ipf_protocol = proto; 12381 ipf->ipf_src = src; 12382 ipf->ipf_dst = dst; 12383 ipf->ipf_nf_hdr_len = 0; 12384 /* Record reassembly start time. */ 12385 ipf->ipf_timestamp = gethrestime_sec(); 12386 /* Record ipf generation and account for frag header */ 12387 ipf->ipf_gen = ill->ill_ipf_gen++; 12388 ipf->ipf_count = MBLKSIZE(mp1); 12389 ipf->ipf_last_frag_seen = B_FALSE; 12390 ipf->ipf_ecn = ecn_info; 12391 ipf->ipf_num_dups = 0; 12392 ipfb->ipfb_frag_pkts++; 12393 ipf->ipf_checksum = 0; 12394 ipf->ipf_checksum_flags = 0; 12395 12396 /* Store checksum value in fragment header */ 12397 if (sum_flags != 0) { 12398 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12399 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12400 ipf->ipf_checksum = sum_val; 12401 ipf->ipf_checksum_flags = sum_flags; 12402 } 12403 12404 /* 12405 * We handle reassembly two ways. In the easy case, 12406 * where all the fragments show up in order, we do 12407 * minimal bookkeeping, and just clip new pieces on 12408 * the end. If we ever see a hole, then we go off 12409 * to ip_reassemble which has to mark the pieces and 12410 * keep track of the number of holes, etc. Obviously, 12411 * the point of having both mechanisms is so we can 12412 * handle the easy case as efficiently as possible. 12413 */ 12414 if (offset == 0) { 12415 /* Easy case, in-order reassembly so far. */ 12416 ipf->ipf_count += msg_len; 12417 ipf->ipf_tail_mp = tail_mp; 12418 /* 12419 * Keep track of next expected offset in 12420 * ipf_end. 12421 */ 12422 ipf->ipf_end = end; 12423 ipf->ipf_nf_hdr_len = hdr_length; 12424 } else { 12425 /* Hard case, hole at the beginning. */ 12426 ipf->ipf_tail_mp = NULL; 12427 /* 12428 * ipf_end == 0 means that we have given up 12429 * on easy reassembly. 12430 */ 12431 ipf->ipf_end = 0; 12432 12433 /* Forget checksum offload from now on */ 12434 ipf->ipf_checksum_flags = 0; 12435 12436 /* 12437 * ipf_hole_cnt is set by ip_reassemble. 12438 * ipf_count is updated by ip_reassemble. 12439 * No need to check for return value here 12440 * as we don't expect reassembly to complete 12441 * or fail for the first fragment itself. 12442 */ 12443 (void) ip_reassemble(mp, ipf, 12444 (frag_offset_flags & IPH_OFFSET) << 3, 12445 (frag_offset_flags & IPH_MF), ill, msg_len); 12446 } 12447 /* Update per ipfb and ill byte counts */ 12448 ipfb->ipfb_count += ipf->ipf_count; 12449 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12450 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count); 12451 /* If the frag timer wasn't already going, start it. */ 12452 mutex_enter(&ill->ill_lock); 12453 ill_frag_timer_start(ill); 12454 mutex_exit(&ill->ill_lock); 12455 goto reass_done; 12456 } 12457 12458 /* 12459 * If the packet's flag has changed (it could be coming up 12460 * from an interface different than the previous, therefore 12461 * possibly different checksum capability), then forget about 12462 * any stored checksum states. Otherwise add the value to 12463 * the existing one stored in the fragment header. 12464 */ 12465 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 12466 sum_val += ipf->ipf_checksum; 12467 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12468 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12469 ipf->ipf_checksum = sum_val; 12470 } else if (ipf->ipf_checksum_flags != 0) { 12471 /* Forget checksum offload from now on */ 12472 ipf->ipf_checksum_flags = 0; 12473 } 12474 12475 /* 12476 * We have a new piece of a datagram which is already being 12477 * reassembled. Update the ECN info if all IP fragments 12478 * are ECN capable. If there is one which is not, clear 12479 * all the info. If there is at least one which has CE 12480 * code point, IP needs to report that up to transport. 12481 */ 12482 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 12483 if (ecn_info == IPH_ECN_CE) 12484 ipf->ipf_ecn = IPH_ECN_CE; 12485 } else { 12486 ipf->ipf_ecn = IPH_ECN_NECT; 12487 } 12488 if (offset && ipf->ipf_end == offset) { 12489 /* The new fragment fits at the end */ 12490 ipf->ipf_tail_mp->b_cont = mp; 12491 /* Update the byte count */ 12492 ipf->ipf_count += msg_len; 12493 /* Update per ipfb and ill byte counts */ 12494 ipfb->ipfb_count += msg_len; 12495 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12496 atomic_add_32(&ill->ill_frag_count, msg_len); 12497 if (frag_offset_flags & IPH_MF) { 12498 /* More to come. */ 12499 ipf->ipf_end = end; 12500 ipf->ipf_tail_mp = tail_mp; 12501 goto reass_done; 12502 } 12503 } else { 12504 /* Go do the hard cases. */ 12505 int ret; 12506 12507 if (offset == 0) 12508 ipf->ipf_nf_hdr_len = hdr_length; 12509 12510 /* Save current byte count */ 12511 count = ipf->ipf_count; 12512 ret = ip_reassemble(mp, ipf, 12513 (frag_offset_flags & IPH_OFFSET) << 3, 12514 (frag_offset_flags & IPH_MF), ill, msg_len); 12515 /* Count of bytes added and subtracted (freeb()ed) */ 12516 count = ipf->ipf_count - count; 12517 if (count) { 12518 /* Update per ipfb and ill byte counts */ 12519 ipfb->ipfb_count += count; 12520 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12521 atomic_add_32(&ill->ill_frag_count, count); 12522 } 12523 if (ret == IP_REASS_PARTIAL) { 12524 goto reass_done; 12525 } else if (ret == IP_REASS_FAILED) { 12526 /* Reassembly failed. Free up all resources */ 12527 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12528 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 12529 IP_REASS_SET_START(t_mp, 0); 12530 IP_REASS_SET_END(t_mp, 0); 12531 } 12532 freemsg(mp); 12533 goto reass_done; 12534 } 12535 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 12536 } 12537 /* 12538 * We have completed reassembly. Unhook the frag header from 12539 * the reassembly list. 12540 * 12541 * Before we free the frag header, record the ECN info 12542 * to report back to the transport. 12543 */ 12544 ecn_info = ipf->ipf_ecn; 12545 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); 12546 ipfp = ipf->ipf_ptphn; 12547 12548 /* We need to supply these to caller */ 12549 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 12550 sum_val = ipf->ipf_checksum; 12551 else 12552 sum_val = 0; 12553 12554 mp1 = ipf->ipf_mp; 12555 count = ipf->ipf_count; 12556 ipf = ipf->ipf_hash_next; 12557 if (ipf != NULL) 12558 ipf->ipf_ptphn = ipfp; 12559 ipfp[0] = ipf; 12560 atomic_add_32(&ill->ill_frag_count, -count); 12561 ASSERT(ipfb->ipfb_count >= count); 12562 ipfb->ipfb_count -= count; 12563 ipfb->ipfb_frag_pkts--; 12564 mutex_exit(&ipfb->ipfb_lock); 12565 /* Ditch the frag header. */ 12566 mp = mp1->b_cont; 12567 12568 freeb(mp1); 12569 12570 /* Restore original IP length in header. */ 12571 packet_size = (uint32_t)msgdsize(mp); 12572 if (packet_size > IP_MAXPACKET) { 12573 freemsg(mp); 12574 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 12575 return (B_FALSE); 12576 } 12577 12578 if (DB_REF(mp) > 1) { 12579 mblk_t *mp2 = copymsg(mp); 12580 12581 freemsg(mp); 12582 if (mp2 == NULL) { 12583 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12584 return (B_FALSE); 12585 } 12586 mp = mp2; 12587 } 12588 ipha = (ipha_t *)mp->b_rptr; 12589 12590 ipha->ipha_length = htons((uint16_t)packet_size); 12591 /* We're now complete, zip the frag state */ 12592 ipha->ipha_fragment_offset_and_flags = 0; 12593 /* Record the ECN info. */ 12594 ipha->ipha_type_of_service &= 0xFC; 12595 ipha->ipha_type_of_service |= ecn_info; 12596 *mpp = mp; 12597 12598 /* Reassembly is successful; return checksum information if needed */ 12599 if (cksum_val != NULL) 12600 *cksum_val = sum_val; 12601 if (cksum_flags != NULL) 12602 *cksum_flags = sum_flags; 12603 12604 return (B_TRUE); 12605 } 12606 12607 /* 12608 * Perform ip header check sum update local options. 12609 * return B_TRUE if all is well, else return B_FALSE and release 12610 * the mp. caller is responsible for decrementing ire ref cnt. 12611 */ 12612 static boolean_t 12613 ip_options_cksum(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12614 ip_stack_t *ipst) 12615 { 12616 mblk_t *first_mp; 12617 boolean_t mctl_present; 12618 uint16_t sum; 12619 12620 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12621 /* 12622 * Don't do the checksum if it has gone through AH/ESP 12623 * processing. 12624 */ 12625 if (!mctl_present) { 12626 sum = ip_csum_hdr(ipha); 12627 if (sum != 0) { 12628 if (ill != NULL) { 12629 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12630 } else { 12631 BUMP_MIB(&ipst->ips_ip_mib, 12632 ipIfStatsInCksumErrs); 12633 } 12634 freemsg(first_mp); 12635 return (B_FALSE); 12636 } 12637 } 12638 12639 if (!ip_rput_local_options(q, mp, ipha, ire, ipst)) { 12640 if (mctl_present) 12641 freeb(first_mp); 12642 return (B_FALSE); 12643 } 12644 12645 return (B_TRUE); 12646 } 12647 12648 /* 12649 * All udp packet are delivered to the local host via this routine. 12650 */ 12651 void 12652 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12653 ill_t *recv_ill) 12654 { 12655 uint32_t sum; 12656 uint32_t u1; 12657 boolean_t mctl_present; 12658 conn_t *connp; 12659 mblk_t *first_mp; 12660 uint16_t *up; 12661 ill_t *ill = (ill_t *)q->q_ptr; 12662 uint16_t reass_hck_flags = 0; 12663 ip_stack_t *ipst; 12664 12665 ASSERT(recv_ill != NULL); 12666 ipst = recv_ill->ill_ipst; 12667 12668 #define rptr ((uchar_t *)ipha) 12669 12670 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12671 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 12672 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12673 ASSERT(ill != NULL); 12674 12675 /* 12676 * FAST PATH for udp packets 12677 */ 12678 12679 /* u1 is # words of IP options */ 12680 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 12681 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12682 12683 /* IP options present */ 12684 if (u1 != 0) 12685 goto ipoptions; 12686 12687 /* Check the IP header checksum. */ 12688 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, recv_ill)) { 12689 /* Clear the IP header h/w cksum flag */ 12690 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12691 } else if (!mctl_present) { 12692 /* 12693 * Don't verify header checksum if this packet is coming 12694 * back from AH/ESP as we already did it. 12695 */ 12696 #define uph ((uint16_t *)ipha) 12697 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 12698 uph[6] + uph[7] + uph[8] + uph[9]; 12699 #undef uph 12700 /* finish doing IP checksum */ 12701 sum = (sum & 0xFFFF) + (sum >> 16); 12702 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12703 if (sum != 0 && sum != 0xFFFF) { 12704 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12705 freemsg(first_mp); 12706 return; 12707 } 12708 } 12709 12710 /* 12711 * Count for SNMP of inbound packets for ire. 12712 * if mctl is present this might be a secure packet and 12713 * has already been counted for in ip_proto_input(). 12714 */ 12715 if (!mctl_present) { 12716 UPDATE_IB_PKT_COUNT(ire); 12717 ire->ire_last_used_time = lbolt; 12718 } 12719 12720 /* packet part of fragmented IP packet? */ 12721 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12722 if (u1 & (IPH_MF | IPH_OFFSET)) { 12723 goto fragmented; 12724 } 12725 12726 /* u1 = IP header length (20 bytes) */ 12727 u1 = IP_SIMPLE_HDR_LENGTH; 12728 12729 /* packet does not contain complete IP & UDP headers */ 12730 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 12731 goto udppullup; 12732 12733 /* up points to UDP header */ 12734 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 12735 #define iphs ((uint16_t *)ipha) 12736 12737 /* if udp hdr cksum != 0, then need to checksum udp packet */ 12738 if (up[3] != 0) { 12739 mblk_t *mp1 = mp->b_cont; 12740 boolean_t cksum_err; 12741 uint16_t hck_flags = 0; 12742 12743 /* Pseudo-header checksum */ 12744 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12745 iphs[9] + up[2]; 12746 12747 /* 12748 * Revert to software checksum calculation if the interface 12749 * isn't capable of checksum offload or if IPsec is present. 12750 */ 12751 if (ILL_HCKSUM_CAPABLE(recv_ill) && !mctl_present && dohwcksum) 12752 hck_flags = DB_CKSUMFLAGS(mp); 12753 12754 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12755 IP_STAT(ipst, ip_in_sw_cksum); 12756 12757 IP_CKSUM_RECV(hck_flags, u1, 12758 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 12759 (int32_t)((uchar_t *)up - rptr), 12760 mp, mp1, cksum_err); 12761 12762 if (cksum_err) { 12763 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12764 if (hck_flags & HCK_FULLCKSUM) 12765 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 12766 else if (hck_flags & HCK_PARTIALCKSUM) 12767 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 12768 else 12769 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 12770 12771 freemsg(first_mp); 12772 return; 12773 } 12774 } 12775 12776 /* Non-fragmented broadcast or multicast packet? */ 12777 if (ire->ire_type == IRE_BROADCAST) 12778 goto udpslowpath; 12779 12780 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 12781 ire->ire_zoneid, ipst)) != NULL) { 12782 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_upq != NULL); 12783 IP_STAT(ipst, ip_udp_fast_path); 12784 12785 if ((IPCL_IS_NONSTR(connp) && PROTO_FLOW_CNTRLD(connp)) || 12786 (!IPCL_IS_NONSTR(connp) && CONN_UDP_FLOWCTLD(connp))) { 12787 freemsg(mp); 12788 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 12789 } else { 12790 if (!mctl_present) { 12791 BUMP_MIB(ill->ill_ip_mib, 12792 ipIfStatsHCInDelivers); 12793 } 12794 /* 12795 * mp and first_mp can change. 12796 */ 12797 if (ip_udp_check(q, connp, recv_ill, 12798 ipha, &mp, &first_mp, mctl_present, ire)) { 12799 /* Send it upstream */ 12800 (connp->conn_recv)(connp, mp, NULL); 12801 } 12802 } 12803 /* 12804 * freeb() cannot deal with null mblk being passed 12805 * in and first_mp can be set to null in the call 12806 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 12807 */ 12808 if (mctl_present && first_mp != NULL) { 12809 freeb(first_mp); 12810 } 12811 CONN_DEC_REF(connp); 12812 return; 12813 } 12814 12815 /* 12816 * if we got here we know the packet is not fragmented and 12817 * has no options. The classifier could not find a conn_t and 12818 * most likely its an icmp packet so send it through slow path. 12819 */ 12820 12821 goto udpslowpath; 12822 12823 ipoptions: 12824 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 12825 goto slow_done; 12826 } 12827 12828 UPDATE_IB_PKT_COUNT(ire); 12829 ire->ire_last_used_time = lbolt; 12830 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12831 if (u1 & (IPH_MF | IPH_OFFSET)) { 12832 fragmented: 12833 /* 12834 * "sum" and "reass_hck_flags" are non-zero if the 12835 * reassembled packet has a valid hardware computed 12836 * checksum information associated with it. 12837 */ 12838 if (!ip_rput_fragment(ill, recv_ill, &mp, ipha, &sum, 12839 &reass_hck_flags)) { 12840 goto slow_done; 12841 } 12842 12843 /* 12844 * Make sure that first_mp points back to mp as 12845 * the mp we came in with could have changed in 12846 * ip_rput_fragment(). 12847 */ 12848 ASSERT(!mctl_present); 12849 ipha = (ipha_t *)mp->b_rptr; 12850 first_mp = mp; 12851 } 12852 12853 /* Now we have a complete datagram, destined for this machine. */ 12854 u1 = IPH_HDR_LENGTH(ipha); 12855 /* Pull up the UDP header, if necessary. */ 12856 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 12857 udppullup: 12858 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 12859 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12860 freemsg(first_mp); 12861 goto slow_done; 12862 } 12863 ipha = (ipha_t *)mp->b_rptr; 12864 } 12865 12866 /* 12867 * Validate the checksum for the reassembled packet; for the 12868 * pullup case we calculate the payload checksum in software. 12869 */ 12870 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 12871 if (up[3] != 0) { 12872 boolean_t cksum_err; 12873 12874 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12875 IP_STAT(ipst, ip_in_sw_cksum); 12876 12877 IP_CKSUM_RECV_REASS(reass_hck_flags, 12878 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 12879 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12880 iphs[9] + up[2], sum, cksum_err); 12881 12882 if (cksum_err) { 12883 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12884 12885 if (reass_hck_flags & HCK_FULLCKSUM) 12886 IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); 12887 else if (reass_hck_flags & HCK_PARTIALCKSUM) 12888 IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); 12889 else 12890 IP_STAT(ipst, ip_udp_in_sw_cksum_err); 12891 12892 freemsg(first_mp); 12893 goto slow_done; 12894 } 12895 } 12896 udpslowpath: 12897 12898 /* Clear hardware checksum flag to be safe */ 12899 DB_CKSUMFLAGS(mp) = 0; 12900 12901 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 12902 (ire->ire_type == IRE_BROADCAST), 12903 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IPINFO, 12904 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 12905 12906 slow_done: 12907 IP_STAT(ipst, ip_udp_slow_path); 12908 return; 12909 12910 #undef iphs 12911 #undef rptr 12912 } 12913 12914 /* ARGSUSED */ 12915 static mblk_t * 12916 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 12917 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 12918 ill_rx_ring_t *ill_ring) 12919 { 12920 conn_t *connp; 12921 uint32_t sum; 12922 uint32_t u1; 12923 uint16_t *up; 12924 int offset; 12925 ssize_t len; 12926 mblk_t *mp1; 12927 boolean_t syn_present = B_FALSE; 12928 tcph_t *tcph; 12929 uint_t tcph_flags; 12930 uint_t ip_hdr_len; 12931 ill_t *ill = (ill_t *)q->q_ptr; 12932 zoneid_t zoneid = ire->ire_zoneid; 12933 boolean_t cksum_err; 12934 uint16_t hck_flags = 0; 12935 ip_stack_t *ipst = recv_ill->ill_ipst; 12936 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 12937 12938 #define rptr ((uchar_t *)ipha) 12939 12940 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 12941 ASSERT(ill != NULL); 12942 12943 /* 12944 * FAST PATH for tcp packets 12945 */ 12946 12947 /* u1 is # words of IP options */ 12948 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 12949 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12950 12951 /* IP options present */ 12952 if (u1) { 12953 goto ipoptions; 12954 } else if (!mctl_present) { 12955 /* Check the IP header checksum. */ 12956 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, recv_ill)) { 12957 /* Clear the IP header h/w cksum flag */ 12958 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12959 } else if (!mctl_present) { 12960 /* 12961 * Don't verify header checksum if this packet 12962 * is coming back from AH/ESP as we already did it. 12963 */ 12964 #define uph ((uint16_t *)ipha) 12965 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 12966 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 12967 #undef uph 12968 /* finish doing IP checksum */ 12969 sum = (sum & 0xFFFF) + (sum >> 16); 12970 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12971 if (sum != 0 && sum != 0xFFFF) { 12972 BUMP_MIB(ill->ill_ip_mib, 12973 ipIfStatsInCksumErrs); 12974 goto error; 12975 } 12976 } 12977 } 12978 12979 if (!mctl_present) { 12980 UPDATE_IB_PKT_COUNT(ire); 12981 ire->ire_last_used_time = lbolt; 12982 } 12983 12984 /* packet part of fragmented IP packet? */ 12985 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12986 if (u1 & (IPH_MF | IPH_OFFSET)) { 12987 goto fragmented; 12988 } 12989 12990 /* u1 = IP header length (20 bytes) */ 12991 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 12992 12993 /* does packet contain IP+TCP headers? */ 12994 len = mp->b_wptr - rptr; 12995 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 12996 IP_STAT(ipst, ip_tcppullup); 12997 goto tcppullup; 12998 } 12999 13000 /* TCP options present? */ 13001 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 13002 13003 /* 13004 * If options need to be pulled up, then goto tcpoptions. 13005 * otherwise we are still in the fast path 13006 */ 13007 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 13008 IP_STAT(ipst, ip_tcpoptions); 13009 goto tcpoptions; 13010 } 13011 13012 /* multiple mblks of tcp data? */ 13013 if ((mp1 = mp->b_cont) != NULL) { 13014 IP_STAT(ipst, ip_multipkttcp); 13015 len += msgdsize(mp1); 13016 } 13017 13018 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 13019 13020 /* part of pseudo checksum */ 13021 13022 /* TCP datagram length */ 13023 u1 = len - IP_SIMPLE_HDR_LENGTH; 13024 13025 #define iphs ((uint16_t *)ipha) 13026 13027 #ifdef _BIG_ENDIAN 13028 u1 += IPPROTO_TCP; 13029 #else 13030 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13031 #endif 13032 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13033 13034 /* 13035 * Revert to software checksum calculation if the interface 13036 * isn't capable of checksum offload or if IPsec is present. 13037 */ 13038 if (ILL_HCKSUM_CAPABLE(recv_ill) && !mctl_present && dohwcksum) 13039 hck_flags = DB_CKSUMFLAGS(mp); 13040 13041 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13042 IP_STAT(ipst, ip_in_sw_cksum); 13043 13044 IP_CKSUM_RECV(hck_flags, u1, 13045 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13046 (int32_t)((uchar_t *)up - rptr), 13047 mp, mp1, cksum_err); 13048 13049 if (cksum_err) { 13050 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13051 13052 if (hck_flags & HCK_FULLCKSUM) 13053 IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err); 13054 else if (hck_flags & HCK_PARTIALCKSUM) 13055 IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err); 13056 else 13057 IP_STAT(ipst, ip_tcp_in_sw_cksum_err); 13058 13059 goto error; 13060 } 13061 13062 try_again: 13063 13064 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, 13065 zoneid, ipst)) == NULL) { 13066 /* Send the TH_RST */ 13067 goto no_conn; 13068 } 13069 13070 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 13071 tcph_flags = tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG); 13072 13073 /* 13074 * TCP FAST PATH for AF_INET socket. 13075 * 13076 * TCP fast path to avoid extra work. An AF_INET socket type 13077 * does not have facility to receive extra information via 13078 * ip_process or ip_add_info. Also, when the connection was 13079 * established, we made a check if this connection is impacted 13080 * by any global IPsec policy or per connection policy (a 13081 * policy that comes in effect later will not apply to this 13082 * connection). Since all this can be determined at the 13083 * connection establishment time, a quick check of flags 13084 * can avoid extra work. 13085 */ 13086 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 13087 !IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13088 ASSERT(first_mp == mp); 13089 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13090 if (tcph_flags != (TH_SYN | TH_ACK)) { 13091 SET_SQUEUE(mp, tcp_rput_data, connp); 13092 return (mp); 13093 } 13094 mp->b_datap->db_struioflag |= STRUIO_CONNECT; 13095 DB_CKSUMSTART(mp) = (intptr_t)ip_squeue_get(ill_ring); 13096 SET_SQUEUE(mp, tcp_input, connp); 13097 return (mp); 13098 } 13099 13100 if (tcph_flags == TH_SYN) { 13101 if (IPCL_IS_TCP(connp)) { 13102 mp->b_datap->db_struioflag |= STRUIO_EAGER; 13103 DB_CKSUMSTART(mp) = 13104 (intptr_t)ip_squeue_get(ill_ring); 13105 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 13106 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13107 BUMP_MIB(ill->ill_ip_mib, 13108 ipIfStatsHCInDelivers); 13109 SET_SQUEUE(mp, connp->conn_recv, connp); 13110 return (mp); 13111 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 13112 !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { 13113 BUMP_MIB(ill->ill_ip_mib, 13114 ipIfStatsHCInDelivers); 13115 ip_squeue_enter_unbound++; 13116 SET_SQUEUE(mp, tcp_conn_request_unbound, 13117 connp); 13118 return (mp); 13119 } 13120 syn_present = B_TRUE; 13121 } 13122 } 13123 13124 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 13125 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13126 13127 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13128 /* No need to send this packet to TCP */ 13129 if ((flags & TH_RST) || (flags & TH_URG)) { 13130 CONN_DEC_REF(connp); 13131 freemsg(first_mp); 13132 return (NULL); 13133 } 13134 if (flags & TH_ACK) { 13135 ip_xmit_reset_serialize(first_mp, ip_hdr_len, zoneid, 13136 ipst->ips_netstack->netstack_tcp, connp); 13137 CONN_DEC_REF(connp); 13138 return (NULL); 13139 } 13140 13141 CONN_DEC_REF(connp); 13142 freemsg(first_mp); 13143 return (NULL); 13144 } 13145 13146 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 13147 first_mp = ipsec_check_inbound_policy(first_mp, connp, 13148 ipha, NULL, mctl_present); 13149 if (first_mp == NULL) { 13150 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13151 CONN_DEC_REF(connp); 13152 return (NULL); 13153 } 13154 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 13155 ASSERT(syn_present); 13156 if (mctl_present) { 13157 ASSERT(first_mp != mp); 13158 first_mp->b_datap->db_struioflag |= 13159 STRUIO_POLICY; 13160 } else { 13161 ASSERT(first_mp == mp); 13162 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 13163 mp->b_datap->db_struioflag |= STRUIO_POLICY; 13164 } 13165 } else { 13166 /* 13167 * Discard first_mp early since we're dealing with a 13168 * fully-connected conn_t and tcp doesn't do policy in 13169 * this case. 13170 */ 13171 if (mctl_present) { 13172 freeb(first_mp); 13173 mctl_present = B_FALSE; 13174 } 13175 first_mp = mp; 13176 } 13177 } 13178 13179 /* Initiate IPPF processing for fastpath */ 13180 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13181 uint32_t ill_index; 13182 13183 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13184 ip_process(IPP_LOCAL_IN, &mp, ill_index); 13185 if (mp == NULL) { 13186 ip2dbg(("ip_input_ipsec_process: TCP pkt " 13187 "deferred/dropped during IPPF processing\n")); 13188 CONN_DEC_REF(connp); 13189 if (mctl_present) 13190 freeb(first_mp); 13191 return (NULL); 13192 } else if (mctl_present) { 13193 /* 13194 * ip_process might return a new mp. 13195 */ 13196 ASSERT(first_mp != mp); 13197 first_mp->b_cont = mp; 13198 } else { 13199 first_mp = mp; 13200 } 13201 13202 } 13203 13204 if (!syn_present && connp->conn_ip_recvpktinfo) { 13205 /* 13206 * TCP does not support IP_RECVPKTINFO for v4 so lets 13207 * make sure IPF_RECVIF is passed to ip_add_info. 13208 */ 13209 mp = ip_add_info(mp, recv_ill, flags|IPF_RECVIF, 13210 IPCL_ZONEID(connp), ipst); 13211 if (mp == NULL) { 13212 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13213 CONN_DEC_REF(connp); 13214 if (mctl_present) 13215 freeb(first_mp); 13216 return (NULL); 13217 } else if (mctl_present) { 13218 /* 13219 * ip_add_info might return a new mp. 13220 */ 13221 ASSERT(first_mp != mp); 13222 first_mp->b_cont = mp; 13223 } else { 13224 first_mp = mp; 13225 } 13226 } 13227 13228 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13229 if (IPCL_IS_TCP(connp)) { 13230 SET_SQUEUE(first_mp, connp->conn_recv, connp); 13231 return (first_mp); 13232 } else { 13233 /* SOCK_RAW, IPPROTO_TCP case */ 13234 (connp->conn_recv)(connp, first_mp, NULL); 13235 CONN_DEC_REF(connp); 13236 return (NULL); 13237 } 13238 13239 no_conn: 13240 /* Initiate IPPf processing, if needed. */ 13241 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 13242 uint32_t ill_index; 13243 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13244 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 13245 if (first_mp == NULL) { 13246 return (NULL); 13247 } 13248 } 13249 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13250 13251 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid, 13252 ipst->ips_netstack->netstack_tcp, NULL); 13253 return (NULL); 13254 ipoptions: 13255 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) { 13256 goto slow_done; 13257 } 13258 13259 UPDATE_IB_PKT_COUNT(ire); 13260 ire->ire_last_used_time = lbolt; 13261 13262 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13263 if (u1 & (IPH_MF | IPH_OFFSET)) { 13264 fragmented: 13265 if (!ip_rput_fragment(ill, recv_ill, &mp, ipha, NULL, NULL)) { 13266 if (mctl_present) 13267 freeb(first_mp); 13268 goto slow_done; 13269 } 13270 /* 13271 * Make sure that first_mp points back to mp as 13272 * the mp we came in with could have changed in 13273 * ip_rput_fragment(). 13274 */ 13275 ASSERT(!mctl_present); 13276 ipha = (ipha_t *)mp->b_rptr; 13277 first_mp = mp; 13278 } 13279 13280 /* Now we have a complete datagram, destined for this machine. */ 13281 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 13282 13283 len = mp->b_wptr - mp->b_rptr; 13284 /* Pull up a minimal TCP header, if necessary. */ 13285 if (len < (u1 + 20)) { 13286 tcppullup: 13287 if (!pullupmsg(mp, u1 + 20)) { 13288 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13289 goto error; 13290 } 13291 ipha = (ipha_t *)mp->b_rptr; 13292 len = mp->b_wptr - mp->b_rptr; 13293 } 13294 13295 /* 13296 * Extract the offset field from the TCP header. As usual, we 13297 * try to help the compiler more than the reader. 13298 */ 13299 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 13300 if (offset != 5) { 13301 tcpoptions: 13302 if (offset < 5) { 13303 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13304 goto error; 13305 } 13306 /* 13307 * There must be TCP options. 13308 * Make sure we can grab them. 13309 */ 13310 offset <<= 2; 13311 offset += u1; 13312 if (len < offset) { 13313 if (!pullupmsg(mp, offset)) { 13314 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13315 goto error; 13316 } 13317 ipha = (ipha_t *)mp->b_rptr; 13318 len = mp->b_wptr - rptr; 13319 } 13320 } 13321 13322 /* Get the total packet length in len, including headers. */ 13323 if (mp->b_cont) 13324 len = msgdsize(mp); 13325 13326 /* 13327 * Check the TCP checksum by pulling together the pseudo- 13328 * header checksum, and passing it to ip_csum to be added in 13329 * with the TCP datagram. 13330 * 13331 * Since we are not using the hwcksum if available we must 13332 * clear the flag. We may come here via tcppullup or tcpoptions. 13333 * If either of these fails along the way the mblk is freed. 13334 * If this logic ever changes and mblk is reused to say send 13335 * ICMP's back, then this flag may need to be cleared in 13336 * other places as well. 13337 */ 13338 DB_CKSUMFLAGS(mp) = 0; 13339 13340 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 13341 13342 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 13343 #ifdef _BIG_ENDIAN 13344 u1 += IPPROTO_TCP; 13345 #else 13346 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13347 #endif 13348 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13349 /* 13350 * Not M_DATA mblk or its a dup, so do the checksum now. 13351 */ 13352 IP_STAT(ipst, ip_in_sw_cksum); 13353 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 13354 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13355 goto error; 13356 } 13357 13358 IP_STAT(ipst, ip_tcp_slow_path); 13359 goto try_again; 13360 #undef iphs 13361 #undef rptr 13362 13363 error: 13364 freemsg(first_mp); 13365 slow_done: 13366 return (NULL); 13367 } 13368 13369 /* ARGSUSED */ 13370 static void 13371 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13372 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 13373 { 13374 conn_t *connp; 13375 uint32_t sum; 13376 uint32_t u1; 13377 ssize_t len; 13378 sctp_hdr_t *sctph; 13379 zoneid_t zoneid = ire->ire_zoneid; 13380 uint32_t pktsum; 13381 uint32_t calcsum; 13382 uint32_t ports; 13383 in6_addr_t map_src, map_dst; 13384 ill_t *ill = (ill_t *)q->q_ptr; 13385 ip_stack_t *ipst; 13386 sctp_stack_t *sctps; 13387 boolean_t sctp_csum_err = B_FALSE; 13388 13389 ASSERT(recv_ill != NULL); 13390 ipst = recv_ill->ill_ipst; 13391 sctps = ipst->ips_netstack->netstack_sctp; 13392 13393 #define rptr ((uchar_t *)ipha) 13394 13395 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 13396 ASSERT(ill != NULL); 13397 13398 /* u1 is # words of IP options */ 13399 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13400 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13401 13402 /* IP options present */ 13403 if (u1 > 0) { 13404 goto ipoptions; 13405 } else { 13406 /* Check the IP header checksum. */ 13407 if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, recv_ill) && 13408 !mctl_present) { 13409 #define uph ((uint16_t *)ipha) 13410 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13411 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13412 #undef uph 13413 /* finish doing IP checksum */ 13414 sum = (sum & 0xFFFF) + (sum >> 16); 13415 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13416 /* 13417 * Don't verify header checksum if this packet 13418 * is coming back from AH/ESP as we already did it. 13419 */ 13420 if (sum != 0 && sum != 0xFFFF) { 13421 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 13422 goto error; 13423 } 13424 } 13425 /* 13426 * Since there is no SCTP h/w cksum support yet, just 13427 * clear the flag. 13428 */ 13429 DB_CKSUMFLAGS(mp) = 0; 13430 } 13431 13432 /* 13433 * Don't verify header checksum if this packet is coming 13434 * back from AH/ESP as we already did it. 13435 */ 13436 if (!mctl_present) { 13437 UPDATE_IB_PKT_COUNT(ire); 13438 ire->ire_last_used_time = lbolt; 13439 } 13440 13441 /* packet part of fragmented IP packet? */ 13442 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13443 if (u1 & (IPH_MF | IPH_OFFSET)) 13444 goto fragmented; 13445 13446 /* u1 = IP header length (20 bytes) */ 13447 u1 = IP_SIMPLE_HDR_LENGTH; 13448 13449 find_sctp_client: 13450 /* Pullup if we don't have the sctp common header. */ 13451 len = MBLKL(mp); 13452 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 13453 if (mp->b_cont == NULL || 13454 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 13455 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13456 goto error; 13457 } 13458 ipha = (ipha_t *)mp->b_rptr; 13459 len = MBLKL(mp); 13460 } 13461 13462 sctph = (sctp_hdr_t *)(rptr + u1); 13463 #ifdef DEBUG 13464 if (!skip_sctp_cksum) { 13465 #endif 13466 pktsum = sctph->sh_chksum; 13467 sctph->sh_chksum = 0; 13468 calcsum = sctp_cksum(mp, u1); 13469 sctph->sh_chksum = pktsum; 13470 if (calcsum != pktsum) 13471 sctp_csum_err = B_TRUE; 13472 #ifdef DEBUG /* skip_sctp_cksum */ 13473 } 13474 #endif 13475 /* get the ports */ 13476 ports = *(uint32_t *)&sctph->sh_sport; 13477 13478 IRE_REFRELE(ire); 13479 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 13480 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 13481 if (sctp_csum_err) { 13482 /* 13483 * No potential sctp checksum errors go to the Sun 13484 * sctp stack however they might be Adler-32 summed 13485 * packets a userland stack bound to a raw IP socket 13486 * could reasonably use. Note though that Adler-32 is 13487 * a long deprecated algorithm and customer sctp 13488 * networks should eventually migrate to CRC-32 at 13489 * which time this facility should be removed. 13490 */ 13491 flags |= IP_FF_SCTP_CSUM_ERR; 13492 goto no_conn; 13493 } 13494 if ((connp = sctp_fanout(&map_src, &map_dst, ports, zoneid, mp, 13495 sctps)) == NULL) { 13496 /* Check for raw socket or OOTB handling */ 13497 goto no_conn; 13498 } 13499 13500 /* Found a client; up it goes */ 13501 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13502 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 13503 return; 13504 13505 no_conn: 13506 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 13507 ports, mctl_present, flags, B_TRUE, zoneid); 13508 return; 13509 13510 ipoptions: 13511 DB_CKSUMFLAGS(mp) = 0; 13512 if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) 13513 goto slow_done; 13514 13515 UPDATE_IB_PKT_COUNT(ire); 13516 ire->ire_last_used_time = lbolt; 13517 13518 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13519 if (u1 & (IPH_MF | IPH_OFFSET)) { 13520 fragmented: 13521 if (!ip_rput_fragment(ill, recv_ill, &mp, ipha, NULL, NULL)) 13522 goto slow_done; 13523 /* 13524 * Make sure that first_mp points back to mp as 13525 * the mp we came in with could have changed in 13526 * ip_rput_fragment(). 13527 */ 13528 ASSERT(!mctl_present); 13529 ipha = (ipha_t *)mp->b_rptr; 13530 first_mp = mp; 13531 } 13532 13533 /* Now we have a complete datagram, destined for this machine. */ 13534 u1 = IPH_HDR_LENGTH(ipha); 13535 goto find_sctp_client; 13536 #undef iphs 13537 #undef rptr 13538 13539 error: 13540 freemsg(first_mp); 13541 slow_done: 13542 IRE_REFRELE(ire); 13543 } 13544 13545 #define VER_BITS 0xF0 13546 #define VERSION_6 0x60 13547 13548 static boolean_t 13549 ip_rput_multimblk_ipoptions(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t **iphapp, 13550 ipaddr_t *dstp, ip_stack_t *ipst) 13551 { 13552 uint_t opt_len; 13553 ipha_t *ipha; 13554 ssize_t len; 13555 uint_t pkt_len; 13556 13557 ASSERT(ill != NULL); 13558 IP_STAT(ipst, ip_ipoptions); 13559 ipha = *iphapp; 13560 13561 #define rptr ((uchar_t *)ipha) 13562 /* Assume no IPv6 packets arrive over the IPv4 queue */ 13563 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 13564 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); 13565 freemsg(mp); 13566 return (B_FALSE); 13567 } 13568 13569 /* multiple mblk or too short */ 13570 pkt_len = ntohs(ipha->ipha_length); 13571 13572 /* Get the number of words of IP options in the IP header. */ 13573 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 13574 if (opt_len) { 13575 /* IP Options present! Validate and process. */ 13576 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 13577 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13578 goto done; 13579 } 13580 /* 13581 * Recompute complete header length and make sure we 13582 * have access to all of it. 13583 */ 13584 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 13585 if (len > (mp->b_wptr - rptr)) { 13586 if (len > pkt_len) { 13587 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13588 goto done; 13589 } 13590 if (!pullupmsg(mp, len)) { 13591 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13592 goto done; 13593 } 13594 ipha = (ipha_t *)mp->b_rptr; 13595 } 13596 /* 13597 * Go off to ip_rput_options which returns the next hop 13598 * destination address, which may have been affected 13599 * by source routing. 13600 */ 13601 IP_STAT(ipst, ip_opt); 13602 if (ip_rput_options(q, mp, ipha, dstp, ipst) == -1) { 13603 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13604 return (B_FALSE); 13605 } 13606 } 13607 *iphapp = ipha; 13608 return (B_TRUE); 13609 done: 13610 /* clear b_prev - used by ip_mroute_decap */ 13611 mp->b_prev = NULL; 13612 freemsg(mp); 13613 return (B_FALSE); 13614 #undef rptr 13615 } 13616 13617 /* 13618 * Deal with the fact that there is no ire for the destination. 13619 */ 13620 static ire_t * 13621 ip_rput_noire(queue_t *q, mblk_t *mp, int ll_multicast, ipaddr_t dst) 13622 { 13623 ipha_t *ipha; 13624 ill_t *ill; 13625 ire_t *ire; 13626 ip_stack_t *ipst; 13627 enum ire_forward_action ret_action; 13628 13629 ipha = (ipha_t *)mp->b_rptr; 13630 ill = (ill_t *)q->q_ptr; 13631 13632 ASSERT(ill != NULL); 13633 ipst = ill->ill_ipst; 13634 13635 /* 13636 * No IRE for this destination, so it can't be for us. 13637 * Unless we are forwarding, drop the packet. 13638 * We have to let source routed packets through 13639 * since we don't yet know if they are 'ping -l' 13640 * packets i.e. if they will go out over the 13641 * same interface as they came in on. 13642 */ 13643 if (ll_multicast) { 13644 freemsg(mp); 13645 return (NULL); 13646 } 13647 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) { 13648 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13649 freemsg(mp); 13650 return (NULL); 13651 } 13652 13653 /* 13654 * Mark this packet as having originated externally. 13655 * 13656 * For non-forwarding code path, ire_send later double 13657 * checks this interface to see if it is still exists 13658 * post-ARP resolution. 13659 * 13660 * Also, IPQOS uses this to differentiate between 13661 * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP 13662 * QOS packet processing in ip_wput_attach_llhdr(). 13663 * The QoS module can mark the b_band for a fastpath message 13664 * or the dl_priority field in a unitdata_req header for 13665 * CoS marking. This info can only be found in 13666 * ip_wput_attach_llhdr(). 13667 */ 13668 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 13669 /* 13670 * Clear the indication that this may have a hardware checksum 13671 * as we are not using it 13672 */ 13673 DB_CKSUMFLAGS(mp) = 0; 13674 13675 ire = ire_forward(dst, &ret_action, NULL, NULL, 13676 msg_getlabel(mp), ipst); 13677 13678 if (ire == NULL && ret_action == Forward_check_multirt) { 13679 /* Let ip_newroute handle CGTP */ 13680 ip_newroute(q, mp, dst, NULL, GLOBAL_ZONEID, ipst); 13681 return (NULL); 13682 } 13683 13684 if (ire != NULL) 13685 return (ire); 13686 13687 mp->b_prev = mp->b_next = 0; 13688 13689 if (ret_action == Forward_blackhole) { 13690 freemsg(mp); 13691 return (NULL); 13692 } 13693 /* send icmp unreachable */ 13694 q = WR(q); 13695 /* Sent by forwarding path, and router is global zone */ 13696 if (ip_source_routed(ipha, ipst)) { 13697 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, 13698 GLOBAL_ZONEID, ipst); 13699 } else { 13700 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, 13701 ipst); 13702 } 13703 13704 return (NULL); 13705 13706 } 13707 13708 /* 13709 * check ip header length and align it. 13710 */ 13711 static boolean_t 13712 ip_check_and_align_header(queue_t *q, mblk_t *mp, ip_stack_t *ipst) 13713 { 13714 ssize_t len; 13715 ill_t *ill; 13716 ipha_t *ipha; 13717 13718 len = MBLKL(mp); 13719 13720 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13721 ill = (ill_t *)q->q_ptr; 13722 13723 if (!OK_32PTR(mp->b_rptr)) 13724 IP_STAT(ipst, ip_notaligned1); 13725 else 13726 IP_STAT(ipst, ip_notaligned2); 13727 /* Guard against bogus device drivers */ 13728 if (len < 0) { 13729 /* clear b_prev - used by ip_mroute_decap */ 13730 mp->b_prev = NULL; 13731 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13732 freemsg(mp); 13733 return (B_FALSE); 13734 } 13735 13736 if (ip_rput_pullups++ == 0) { 13737 ipha = (ipha_t *)mp->b_rptr; 13738 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 13739 "ip_check_and_align_header: %s forced us to " 13740 " pullup pkt, hdr len %ld, hdr addr %p", 13741 ill->ill_name, len, (void *)ipha); 13742 } 13743 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 13744 /* clear b_prev - used by ip_mroute_decap */ 13745 mp->b_prev = NULL; 13746 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13747 freemsg(mp); 13748 return (B_FALSE); 13749 } 13750 } 13751 return (B_TRUE); 13752 } 13753 13754 /* 13755 * Handle the situation where a packet came in on `ill' but matched an IRE 13756 * whose ire_rfq doesn't match `ill'. We return the IRE that should be used 13757 * for interface statistics. 13758 */ 13759 ire_t * 13760 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill) 13761 { 13762 ire_t *new_ire; 13763 ill_t *ire_ill; 13764 uint_t ifindex; 13765 ip_stack_t *ipst = ill->ill_ipst; 13766 boolean_t strict_check = B_FALSE; 13767 13768 /* 13769 * IPMP common case: if IRE and ILL are in the same group, there's no 13770 * issue (e.g. packet received on an underlying interface matched an 13771 * IRE_LOCAL on its associated group interface). 13772 */ 13773 if (ire->ire_rfq != NULL && 13774 IS_IN_SAME_ILLGRP(ill, (ill_t *)ire->ire_rfq->q_ptr)) { 13775 return (ire); 13776 } 13777 13778 /* 13779 * Do another ire lookup here, using the ingress ill, to see if the 13780 * interface is in a usesrc group. 13781 * As long as the ills belong to the same group, we don't consider 13782 * them to be arriving on the wrong interface. Thus, if the switch 13783 * is doing inbound load spreading, we won't drop packets when the 13784 * ip*_strict_dst_multihoming switch is on. 13785 * We also need to check for IPIF_UNNUMBERED point2point interfaces 13786 * where the local address may not be unique. In this case we were 13787 * at the mercy of the initial ire cache lookup and the IRE_LOCAL it 13788 * actually returned. The new lookup, which is more specific, should 13789 * only find the IRE_LOCAL associated with the ingress ill if one 13790 * exists. 13791 */ 13792 13793 if (ire->ire_ipversion == IPV4_VERSION) { 13794 if (ipst->ips_ip_strict_dst_multihoming) 13795 strict_check = B_TRUE; 13796 new_ire = ire_ctable_lookup(*((ipaddr_t *)addr), 0, IRE_LOCAL, 13797 ill->ill_ipif, ALL_ZONES, NULL, 13798 (MATCH_IRE_TYPE|MATCH_IRE_ILL), ipst); 13799 } else { 13800 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr)); 13801 if (ipst->ips_ipv6_strict_dst_multihoming) 13802 strict_check = B_TRUE; 13803 new_ire = ire_ctable_lookup_v6((in6_addr_t *)addr, NULL, 13804 IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL, 13805 (MATCH_IRE_TYPE|MATCH_IRE_ILL), ipst); 13806 } 13807 /* 13808 * If the same ire that was returned in ip_input() is found then this 13809 * is an indication that usesrc groups are in use. The packet 13810 * arrived on a different ill in the group than the one associated with 13811 * the destination address. If a different ire was found then the same 13812 * IP address must be hosted on multiple ills. This is possible with 13813 * unnumbered point2point interfaces. We switch to use this new ire in 13814 * order to have accurate interface statistics. 13815 */ 13816 if (new_ire != NULL) { 13817 if ((new_ire != ire) && (new_ire->ire_rfq != NULL)) { 13818 ire_refrele(ire); 13819 ire = new_ire; 13820 } else { 13821 ire_refrele(new_ire); 13822 } 13823 return (ire); 13824 } else if ((ire->ire_rfq == NULL) && 13825 (ire->ire_ipversion == IPV4_VERSION)) { 13826 /* 13827 * The best match could have been the original ire which 13828 * was created against an IRE_LOCAL on lo0. In the IPv4 case 13829 * the strict multihoming checks are irrelevant as we consider 13830 * local addresses hosted on lo0 to be interface agnostic. We 13831 * only expect a null ire_rfq on IREs which are associated with 13832 * lo0 hence we can return now. 13833 */ 13834 return (ire); 13835 } 13836 13837 /* 13838 * Chase pointers once and store locally. 13839 */ 13840 ire_ill = (ire->ire_rfq == NULL) ? NULL : 13841 (ill_t *)(ire->ire_rfq->q_ptr); 13842 ifindex = ill->ill_usesrc_ifindex; 13843 13844 /* 13845 * Check if it's a legal address on the 'usesrc' interface. 13846 */ 13847 if ((ifindex != 0) && (ire_ill != NULL) && 13848 (ifindex == ire_ill->ill_phyint->phyint_ifindex)) { 13849 return (ire); 13850 } 13851 13852 /* 13853 * If the ip*_strict_dst_multihoming switch is on then we can 13854 * only accept this packet if the interface is marked as routing. 13855 */ 13856 if (!(strict_check)) 13857 return (ire); 13858 13859 if ((ill->ill_flags & ire->ire_ipif->ipif_ill->ill_flags & 13860 ILLF_ROUTER) != 0) { 13861 return (ire); 13862 } 13863 13864 ire_refrele(ire); 13865 return (NULL); 13866 } 13867 13868 /* 13869 * 13870 * This is the fast forward path. If we are here, we dont need to 13871 * worry about RSVP, CGTP, or TSol. Furthermore the ftable lookup 13872 * needed to find the nexthop in this case is much simpler 13873 */ 13874 ire_t * 13875 ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) 13876 { 13877 ipha_t *ipha; 13878 ire_t *src_ire; 13879 ill_t *stq_ill; 13880 uint_t hlen; 13881 uint_t pkt_len; 13882 uint32_t sum; 13883 queue_t *dev_q; 13884 ip_stack_t *ipst = ill->ill_ipst; 13885 mblk_t *fpmp; 13886 enum ire_forward_action ret_action; 13887 13888 ipha = (ipha_t *)mp->b_rptr; 13889 13890 if (ire != NULL && 13891 ire->ire_zoneid != GLOBAL_ZONEID && 13892 ire->ire_zoneid != ALL_ZONES) { 13893 /* 13894 * Should only use IREs that are visible to the global 13895 * zone for forwarding. 13896 */ 13897 ire_refrele(ire); 13898 ire = ire_cache_lookup(dst, GLOBAL_ZONEID, NULL, ipst); 13899 /* 13900 * ire_cache_lookup() can return ire of IRE_LOCAL in 13901 * transient cases. In such case, just drop the packet 13902 */ 13903 if (ire->ire_type != IRE_CACHE) 13904 goto drop; 13905 } 13906 13907 /* 13908 * Martian Address Filtering [RFC 1812, Section 5.3.7] 13909 * The loopback address check for both src and dst has already 13910 * been checked in ip_input 13911 */ 13912 13913 if (dst == INADDR_ANY || CLASSD(ipha->ipha_src)) { 13914 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13915 goto drop; 13916 } 13917 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 13918 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 13919 13920 if (src_ire != NULL) { 13921 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13922 ire_refrele(src_ire); 13923 goto drop; 13924 } 13925 13926 /* No ire cache of nexthop. So first create one */ 13927 if (ire == NULL) { 13928 13929 ire = ire_forward_simple(dst, &ret_action, ipst); 13930 13931 /* 13932 * We only come to ip_fast_forward if ip_cgtp_filter 13933 * is not set. So ire_forward() should not return with 13934 * Forward_check_multirt as the next action. 13935 */ 13936 ASSERT(ret_action != Forward_check_multirt); 13937 if (ire == NULL) { 13938 /* An attempt was made to forward the packet */ 13939 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 13940 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13941 mp->b_prev = mp->b_next = 0; 13942 /* send icmp unreachable */ 13943 /* Sent by forwarding path, and router is global zone */ 13944 if (ret_action == Forward_ret_icmp_err) { 13945 if (ip_source_routed(ipha, ipst)) { 13946 icmp_unreachable(ill->ill_wq, mp, 13947 ICMP_SOURCE_ROUTE_FAILED, 13948 GLOBAL_ZONEID, ipst); 13949 } else { 13950 icmp_unreachable(ill->ill_wq, mp, 13951 ICMP_HOST_UNREACHABLE, 13952 GLOBAL_ZONEID, ipst); 13953 } 13954 } else { 13955 freemsg(mp); 13956 } 13957 return (NULL); 13958 } 13959 } 13960 13961 /* 13962 * Forwarding fastpath exception case: 13963 * If any of the following are true, we take the slowpath: 13964 * o forwarding is not enabled 13965 * o incoming and outgoing interface are the same, or in the same 13966 * IPMP group. 13967 * o corresponding ire is in incomplete state 13968 * o packet needs fragmentation 13969 * o ARP cache is not resolved 13970 * 13971 * The codeflow from here on is thus: 13972 * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 13973 */ 13974 pkt_len = ntohs(ipha->ipha_length); 13975 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 13976 if (!(stq_ill->ill_flags & ILLF_ROUTER) || 13977 (ill == stq_ill) || IS_IN_SAME_ILLGRP(ill, stq_ill) || 13978 (ire->ire_nce == NULL) || 13979 (pkt_len > ire->ire_max_frag) || 13980 ((fpmp = ire->ire_nce->nce_fp_mp) == NULL) || 13981 ((hlen = MBLKL(fpmp)) > MBLKHEAD(mp)) || 13982 ipha->ipha_ttl <= 1) { 13983 ip_rput_process_forward(ill->ill_rq, mp, ire, 13984 ipha, ill, B_FALSE, B_TRUE); 13985 return (ire); 13986 } 13987 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 13988 13989 DTRACE_PROBE4(ip4__forwarding__start, 13990 ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 13991 13992 FW_HOOKS(ipst->ips_ip4_forwarding_event, 13993 ipst->ips_ipv4firewall_forwarding, 13994 ill, stq_ill, ipha, mp, mp, 0, ipst); 13995 13996 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 13997 13998 if (mp == NULL) 13999 goto drop; 14000 14001 mp->b_datap->db_struioun.cksum.flags = 0; 14002 /* Adjust the checksum to reflect the ttl decrement. */ 14003 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 14004 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 14005 ipha->ipha_ttl--; 14006 14007 /* 14008 * Write the link layer header. We can do this safely here, 14009 * because we have already tested to make sure that the IP 14010 * policy is not set, and that we have a fast path destination 14011 * header. 14012 */ 14013 mp->b_rptr -= hlen; 14014 bcopy(fpmp->b_rptr, mp->b_rptr, hlen); 14015 14016 UPDATE_IB_PKT_COUNT(ire); 14017 ire->ire_last_used_time = lbolt; 14018 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 14019 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 14020 UPDATE_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutOctets, pkt_len); 14021 14022 if (!ILL_DIRECT_CAPABLE(stq_ill) || DB_TYPE(mp) != M_DATA) { 14023 dev_q = ire->ire_stq->q_next; 14024 if (DEV_Q_FLOW_BLOCKED(dev_q)) 14025 goto indiscard; 14026 } 14027 14028 DTRACE_PROBE4(ip4__physical__out__start, 14029 ill_t *, NULL, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 14030 FW_HOOKS(ipst->ips_ip4_physical_out_event, 14031 ipst->ips_ipv4firewall_physical_out, 14032 NULL, stq_ill, ipha, mp, mp, 0, ipst); 14033 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 14034 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, 14035 ipha, __dtrace_ipsr_ill_t *, stq_ill, ipha_t *, ipha, 14036 ip6_t *, NULL, int, 0); 14037 14038 if (mp != NULL) { 14039 if (ipst->ips_ipobs_enabled) { 14040 zoneid_t szone; 14041 14042 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 14043 ipst, ALL_ZONES); 14044 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 14045 ALL_ZONES, ill, IPV4_VERSION, hlen, ipst); 14046 } 14047 ILL_SEND_TX(stq_ill, ire, dst, mp, IP_DROP_ON_NO_DESC, NULL); 14048 } 14049 return (ire); 14050 14051 indiscard: 14052 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14053 drop: 14054 if (mp != NULL) 14055 freemsg(mp); 14056 return (ire); 14057 14058 } 14059 14060 /* 14061 * This function is called in the forwarding slowpath, when 14062 * either the ire lacks the link-layer address, or the packet needs 14063 * further processing(eg. fragmentation), before transmission. 14064 */ 14065 14066 static void 14067 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14068 ill_t *ill, boolean_t ll_multicast, boolean_t from_ip_fast_forward) 14069 { 14070 queue_t *dev_q; 14071 ire_t *src_ire; 14072 ip_stack_t *ipst = ill->ill_ipst; 14073 boolean_t same_illgrp = B_FALSE; 14074 14075 ASSERT(ire->ire_stq != NULL); 14076 14077 mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ 14078 mp->b_next = NULL; /* ip_rput_noire sets dst here */ 14079 14080 /* 14081 * If the caller of this function is ip_fast_forward() skip the 14082 * next three checks as it does not apply. 14083 */ 14084 if (from_ip_fast_forward) 14085 goto skip; 14086 14087 if (ll_multicast != 0) { 14088 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14089 goto drop_pkt; 14090 } 14091 14092 /* 14093 * check if ipha_src is a broadcast address. Note that this 14094 * check is redundant when we get here from ip_fast_forward() 14095 * which has already done this check. However, since we can 14096 * also get here from ip_rput_process_broadcast() or, for 14097 * for the slow path through ip_fast_forward(), we perform 14098 * the check again for code-reusability 14099 */ 14100 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14101 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 14102 if (src_ire != NULL || ipha->ipha_dst == INADDR_ANY) { 14103 if (src_ire != NULL) 14104 ire_refrele(src_ire); 14105 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14106 ip2dbg(("ip_rput_process_forward: Received packet with" 14107 " bad src/dst address on %s\n", ill->ill_name)); 14108 goto drop_pkt; 14109 } 14110 14111 /* 14112 * Check if we want to forward this one at this time. 14113 * We allow source routed packets on a host provided that 14114 * they go out the same ill or illgrp as they came in on. 14115 * 14116 * XXX To be quicker, we may wish to not chase pointers to 14117 * get the ILLF_ROUTER flag and instead store the 14118 * forwarding policy in the ire. An unfortunate 14119 * side-effect of that would be requiring an ire flush 14120 * whenever the ILLF_ROUTER flag changes. 14121 */ 14122 skip: 14123 same_illgrp = IS_IN_SAME_ILLGRP(ill, (ill_t *)ire->ire_rfq->q_ptr); 14124 14125 if (((ill->ill_flags & 14126 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & ILLF_ROUTER) == 0) && 14127 !(ip_source_routed(ipha, ipst) && 14128 (ire->ire_rfq == q || same_illgrp))) { 14129 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14130 if (ip_source_routed(ipha, ipst)) { 14131 q = WR(q); 14132 /* 14133 * Clear the indication that this may have 14134 * hardware checksum as we are not using it. 14135 */ 14136 DB_CKSUMFLAGS(mp) = 0; 14137 /* Sent by forwarding path, and router is global zone */ 14138 icmp_unreachable(q, mp, 14139 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); 14140 return; 14141 } 14142 goto drop_pkt; 14143 } 14144 14145 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14146 14147 /* Packet is being forwarded. Turning off hwcksum flag. */ 14148 DB_CKSUMFLAGS(mp) = 0; 14149 if (ipst->ips_ip_g_send_redirects) { 14150 /* 14151 * Check whether the incoming interface and outgoing 14152 * interface is part of the same group. If so, 14153 * send redirects. 14154 * 14155 * Check the source address to see if it originated 14156 * on the same logical subnet it is going back out on. 14157 * If so, we should be able to send it a redirect. 14158 * Avoid sending a redirect if the destination 14159 * is directly connected (i.e., ipha_dst is the same 14160 * as ire_gateway_addr or the ire_addr of the 14161 * nexthop IRE_CACHE ), or if the packet was source 14162 * routed out this interface. 14163 */ 14164 ipaddr_t src, nhop; 14165 mblk_t *mp1; 14166 ire_t *nhop_ire = NULL; 14167 14168 /* 14169 * Check whether ire_rfq and q are from the same ill or illgrp. 14170 * If so, send redirects. 14171 */ 14172 if ((ire->ire_rfq == q || same_illgrp) && 14173 !ip_source_routed(ipha, ipst)) { 14174 14175 nhop = (ire->ire_gateway_addr != 0 ? 14176 ire->ire_gateway_addr : ire->ire_addr); 14177 14178 if (ipha->ipha_dst == nhop) { 14179 /* 14180 * We avoid sending a redirect if the 14181 * destination is directly connected 14182 * because it is possible that multiple 14183 * IP subnets may have been configured on 14184 * the link, and the source may not 14185 * be on the same subnet as ip destination, 14186 * even though they are on the same 14187 * physical link. 14188 */ 14189 goto sendit; 14190 } 14191 14192 src = ipha->ipha_src; 14193 14194 /* 14195 * We look up the interface ire for the nexthop, 14196 * to see if ipha_src is in the same subnet 14197 * as the nexthop. 14198 * 14199 * Note that, if, in the future, IRE_CACHE entries 14200 * are obsoleted, this lookup will not be needed, 14201 * as the ire passed to this function will be the 14202 * same as the nhop_ire computed below. 14203 */ 14204 nhop_ire = ire_ftable_lookup(nhop, 0, 0, 14205 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 14206 0, NULL, MATCH_IRE_TYPE, ipst); 14207 14208 if (nhop_ire != NULL) { 14209 if ((src & nhop_ire->ire_mask) == 14210 (nhop & nhop_ire->ire_mask)) { 14211 /* 14212 * The source is directly connected. 14213 * Just copy the ip header (which is 14214 * in the first mblk) 14215 */ 14216 mp1 = copyb(mp); 14217 if (mp1 != NULL) { 14218 icmp_send_redirect(WR(q), mp1, 14219 nhop, ipst); 14220 } 14221 } 14222 ire_refrele(nhop_ire); 14223 } 14224 } 14225 } 14226 sendit: 14227 dev_q = ire->ire_stq->q_next; 14228 if (DEV_Q_FLOW_BLOCKED(dev_q)) { 14229 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14230 freemsg(mp); 14231 return; 14232 } 14233 14234 ip_rput_forward(ire, ipha, mp, ill); 14235 return; 14236 14237 drop_pkt: 14238 ip2dbg(("ip_rput_process_forward: drop pkt\n")); 14239 freemsg(mp); 14240 } 14241 14242 ire_t * 14243 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14244 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 14245 { 14246 queue_t *q; 14247 uint16_t hcksumflags; 14248 ip_stack_t *ipst = ill->ill_ipst; 14249 14250 q = *qp; 14251 14252 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts); 14253 14254 /* 14255 * Clear the indication that this may have hardware 14256 * checksum as we are not using it for forwarding. 14257 */ 14258 hcksumflags = DB_CKSUMFLAGS(mp); 14259 DB_CKSUMFLAGS(mp) = 0; 14260 14261 /* 14262 * Directed broadcast forwarding: if the packet came in over a 14263 * different interface then it is routed out over we can forward it. 14264 */ 14265 if (ipha->ipha_protocol == IPPROTO_TCP) { 14266 ire_refrele(ire); 14267 freemsg(mp); 14268 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14269 return (NULL); 14270 } 14271 /* 14272 * For multicast we have set dst to be INADDR_BROADCAST 14273 * for delivering to all STREAMS. 14274 */ 14275 if (!CLASSD(ipha->ipha_dst)) { 14276 ire_t *new_ire; 14277 ipif_t *ipif; 14278 14279 ipif = ipif_get_next_ipif(NULL, ill); 14280 if (ipif == NULL) { 14281 discard: ire_refrele(ire); 14282 freemsg(mp); 14283 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14284 return (NULL); 14285 } 14286 new_ire = ire_ctable_lookup(dst, 0, 0, 14287 ipif, ALL_ZONES, NULL, MATCH_IRE_ILL, ipst); 14288 ipif_refrele(ipif); 14289 14290 if (new_ire != NULL) { 14291 /* 14292 * If the matching IRE_BROADCAST is part of an IPMP 14293 * group, then drop the packet unless our ill has been 14294 * nominated to receive for the group. 14295 */ 14296 if (IS_IPMP(new_ire->ire_ipif->ipif_ill) && 14297 new_ire->ire_rfq != q) { 14298 ire_refrele(new_ire); 14299 goto discard; 14300 } 14301 14302 /* 14303 * In the special case of multirouted broadcast 14304 * packets, we unconditionally need to "gateway" 14305 * them to the appropriate interface here. 14306 * In the normal case, this cannot happen, because 14307 * there is no broadcast IRE tagged with the 14308 * RTF_MULTIRT flag. 14309 */ 14310 if (new_ire->ire_flags & RTF_MULTIRT) { 14311 ire_refrele(new_ire); 14312 if (ire->ire_rfq != NULL) { 14313 q = ire->ire_rfq; 14314 *qp = q; 14315 } 14316 } else { 14317 ire_refrele(ire); 14318 ire = new_ire; 14319 } 14320 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 14321 if (!ipst->ips_ip_g_forward_directed_bcast) { 14322 /* 14323 * Free the message if 14324 * ip_g_forward_directed_bcast is turned 14325 * off for non-local broadcast. 14326 */ 14327 ire_refrele(ire); 14328 freemsg(mp); 14329 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14330 return (NULL); 14331 } 14332 } else { 14333 /* 14334 * This CGTP packet successfully passed the 14335 * CGTP filter, but the related CGTP 14336 * broadcast IRE has not been found, 14337 * meaning that the redundant ipif is 14338 * probably down. However, if we discarded 14339 * this packet, its duplicate would be 14340 * filtered out by the CGTP filter so none 14341 * of them would get through. So we keep 14342 * going with this one. 14343 */ 14344 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 14345 if (ire->ire_rfq != NULL) { 14346 q = ire->ire_rfq; 14347 *qp = q; 14348 } 14349 } 14350 } 14351 if (ipst->ips_ip_g_forward_directed_bcast && ll_multicast == 0) { 14352 /* 14353 * Verify that there are not more then one 14354 * IRE_BROADCAST with this broadcast address which 14355 * has ire_stq set. 14356 * TODO: simplify, loop over all IRE's 14357 */ 14358 ire_t *ire1; 14359 int num_stq = 0; 14360 mblk_t *mp1; 14361 14362 /* Find the first one with ire_stq set */ 14363 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 14364 for (ire1 = ire; ire1 && 14365 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 14366 ire1 = ire1->ire_next) 14367 ; 14368 if (ire1) { 14369 ire_refrele(ire); 14370 ire = ire1; 14371 IRE_REFHOLD(ire); 14372 } 14373 14374 /* Check if there are additional ones with stq set */ 14375 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 14376 if (ire->ire_addr != ire1->ire_addr) 14377 break; 14378 if (ire1->ire_stq) { 14379 num_stq++; 14380 break; 14381 } 14382 } 14383 rw_exit(&ire->ire_bucket->irb_lock); 14384 if (num_stq == 1 && ire->ire_stq != NULL) { 14385 ip1dbg(("ip_rput_process_broadcast: directed " 14386 "broadcast to 0x%x\n", 14387 ntohl(ire->ire_addr))); 14388 mp1 = copymsg(mp); 14389 if (mp1) { 14390 switch (ipha->ipha_protocol) { 14391 case IPPROTO_UDP: 14392 ip_udp_input(q, mp1, ipha, ire, ill); 14393 break; 14394 default: 14395 ip_proto_input(q, mp1, ipha, ire, ill, 14396 0); 14397 break; 14398 } 14399 } 14400 /* 14401 * Adjust ttl to 2 (1+1 - the forward engine 14402 * will decrement it by one. 14403 */ 14404 if (ip_csum_hdr(ipha)) { 14405 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 14406 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 14407 freemsg(mp); 14408 ire_refrele(ire); 14409 return (NULL); 14410 } 14411 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1; 14412 ipha->ipha_hdr_checksum = 0; 14413 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14414 ip_rput_process_forward(q, mp, ire, ipha, 14415 ill, ll_multicast, B_FALSE); 14416 ire_refrele(ire); 14417 return (NULL); 14418 } 14419 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 14420 ntohl(ire->ire_addr))); 14421 } 14422 14423 /* Restore any hardware checksum flags */ 14424 DB_CKSUMFLAGS(mp) = hcksumflags; 14425 return (ire); 14426 } 14427 14428 /* ARGSUSED */ 14429 static boolean_t 14430 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 14431 int *ll_multicast, ipaddr_t *dstp) 14432 { 14433 ip_stack_t *ipst = ill->ill_ipst; 14434 14435 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts); 14436 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, 14437 ntohs(ipha->ipha_length)); 14438 14439 /* 14440 * So that we don't end up with dups, only one ill in an IPMP group is 14441 * nominated to receive multicast traffic. 14442 */ 14443 if (IS_UNDER_IPMP(ill) && !ill->ill_nom_cast) 14444 goto drop_pkt; 14445 14446 /* 14447 * Forward packets only if we have joined the allmulti 14448 * group on this interface. 14449 */ 14450 if (ipst->ips_ip_g_mrouter && ill->ill_join_allmulti) { 14451 int retval; 14452 14453 /* 14454 * Clear the indication that this may have hardware 14455 * checksum as we are not using it. 14456 */ 14457 DB_CKSUMFLAGS(mp) = 0; 14458 retval = ip_mforward(ill, ipha, mp); 14459 /* ip_mforward updates mib variables if needed */ 14460 /* clear b_prev - used by ip_mroute_decap */ 14461 mp->b_prev = NULL; 14462 14463 switch (retval) { 14464 case 0: 14465 /* 14466 * pkt is okay and arrived on phyint. 14467 * 14468 * If we are running as a multicast router 14469 * we need to see all IGMP and/or PIM packets. 14470 */ 14471 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 14472 (ipha->ipha_protocol == IPPROTO_PIM)) { 14473 goto done; 14474 } 14475 break; 14476 case -1: 14477 /* pkt is mal-formed, toss it */ 14478 goto drop_pkt; 14479 case 1: 14480 /* pkt is okay and arrived on a tunnel */ 14481 /* 14482 * If we are running a multicast router 14483 * we need to see all igmp packets. 14484 */ 14485 if (ipha->ipha_protocol == IPPROTO_IGMP) { 14486 *dstp = INADDR_BROADCAST; 14487 *ll_multicast = 1; 14488 return (B_FALSE); 14489 } 14490 14491 goto drop_pkt; 14492 } 14493 } 14494 14495 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 14496 /* 14497 * This might just be caused by the fact that 14498 * multiple IP Multicast addresses map to the same 14499 * link layer multicast - no need to increment counter! 14500 */ 14501 freemsg(mp); 14502 return (B_TRUE); 14503 } 14504 done: 14505 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 14506 /* 14507 * This assumes the we deliver to all streams for multicast 14508 * and broadcast packets. 14509 */ 14510 *dstp = INADDR_BROADCAST; 14511 *ll_multicast = 1; 14512 return (B_FALSE); 14513 drop_pkt: 14514 ip2dbg(("ip_rput: drop pkt\n")); 14515 freemsg(mp); 14516 return (B_TRUE); 14517 } 14518 14519 /* 14520 * This function is used to both return an indication of whether or not 14521 * the packet received is a non-unicast packet (by way of the DL_UNITDATA_IND) 14522 * and in doing so, determine whether or not it is broadcast vs multicast. 14523 * For it to be a broadcast packet, we must have the appropriate mblk_t 14524 * hanging off the ill_t. If this is either not present or doesn't match 14525 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed 14526 * to be multicast. Thus NICs that have no broadcast address (or no 14527 * capability for one, such as point to point links) cannot return as 14528 * the packet being broadcast. The use of HPE_BROADCAST/HPE_MULTICAST as 14529 * the return values simplifies the current use of the return value of this 14530 * function, which is to pass through the multicast/broadcast characteristic 14531 * to consumers of the netinfo/pfhooks API. While this is not cast in stone, 14532 * changing the return value to some other symbol demands the appropriate 14533 * "translation" when hpe_flags is set prior to calling hook_run() for 14534 * packet events. 14535 */ 14536 int 14537 ip_get_dlpi_mbcast(ill_t *ill, mblk_t *mb) 14538 { 14539 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr; 14540 mblk_t *bmp; 14541 14542 if (ind->dl_group_address) { 14543 if (ind->dl_dest_addr_offset > sizeof (*ind) && 14544 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < 14545 MBLKL(mb) && 14546 (bmp = ill->ill_bcast_mp) != NULL) { 14547 dl_unitdata_req_t *dlur; 14548 uint8_t *bphys_addr; 14549 14550 dlur = (dl_unitdata_req_t *)bmp->b_rptr; 14551 if (ill->ill_sap_length < 0) 14552 bphys_addr = (uchar_t *)dlur + 14553 dlur->dl_dest_addr_offset; 14554 else 14555 bphys_addr = (uchar_t *)dlur + 14556 dlur->dl_dest_addr_offset + 14557 ill->ill_sap_length; 14558 14559 if (bcmp(mb->b_rptr + ind->dl_dest_addr_offset, 14560 bphys_addr, ind->dl_dest_addr_length) == 0) { 14561 return (HPE_BROADCAST); 14562 } 14563 return (HPE_MULTICAST); 14564 } 14565 return (HPE_MULTICAST); 14566 } 14567 return (0); 14568 } 14569 14570 static boolean_t 14571 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 14572 int *ll_multicast, mblk_t **mpp) 14573 { 14574 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 14575 boolean_t must_copy = B_FALSE; 14576 struct iocblk *iocp; 14577 ipha_t *ipha; 14578 ip_stack_t *ipst = ill->ill_ipst; 14579 14580 #define rptr ((uchar_t *)ipha) 14581 14582 first_mp = *first_mpp; 14583 mp = *mpp; 14584 14585 ASSERT(first_mp == mp); 14586 14587 /* 14588 * if db_ref > 1 then copymsg and free original. Packet may be 14589 * changed and do not want other entity who has a reference to this 14590 * message to trip over the changes. This is a blind change because 14591 * trying to catch all places that might change packet is too 14592 * difficult (since it may be a module above this one) 14593 * 14594 * This corresponds to the non-fast path case. We walk down the full 14595 * chain in this case, and check the db_ref count of all the dblks, 14596 * and do a copymsg if required. It is possible that the db_ref counts 14597 * of the data blocks in the mblk chain can be different. 14598 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 14599 * count of 1, followed by a M_DATA block with a ref count of 2, if 14600 * 'snoop' is running. 14601 */ 14602 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 14603 if (mp1->b_datap->db_ref > 1) { 14604 must_copy = B_TRUE; 14605 break; 14606 } 14607 } 14608 14609 if (must_copy) { 14610 mp1 = copymsg(mp); 14611 if (mp1 == NULL) { 14612 for (mp1 = mp; mp1 != NULL; 14613 mp1 = mp1->b_cont) { 14614 mp1->b_next = NULL; 14615 mp1->b_prev = NULL; 14616 } 14617 freemsg(mp); 14618 if (ill != NULL) { 14619 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14620 } else { 14621 BUMP_MIB(&ipst->ips_ip_mib, 14622 ipIfStatsInDiscards); 14623 } 14624 return (B_TRUE); 14625 } 14626 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 14627 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 14628 /* Copy b_prev - used by ip_mroute_decap */ 14629 to_mp->b_prev = from_mp->b_prev; 14630 from_mp->b_prev = NULL; 14631 } 14632 *first_mpp = first_mp = mp1; 14633 freemsg(mp); 14634 mp = mp1; 14635 *mpp = mp1; 14636 } 14637 14638 ipha = (ipha_t *)mp->b_rptr; 14639 14640 /* 14641 * previous code has a case for M_DATA. 14642 * We want to check how that happens. 14643 */ 14644 ASSERT(first_mp->b_datap->db_type != M_DATA); 14645 switch (first_mp->b_datap->db_type) { 14646 case M_PROTO: 14647 case M_PCPROTO: 14648 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 14649 DL_UNITDATA_IND) { 14650 /* Go handle anything other than data elsewhere. */ 14651 ip_rput_dlpi(q, mp); 14652 return (B_TRUE); 14653 } 14654 14655 *ll_multicast = ip_get_dlpi_mbcast(ill, mp); 14656 /* Ditch the DLPI header. */ 14657 mp1 = mp->b_cont; 14658 ASSERT(first_mp == mp); 14659 *first_mpp = mp1; 14660 freeb(mp); 14661 *mpp = mp1; 14662 return (B_FALSE); 14663 case M_IOCACK: 14664 ip1dbg(("got iocack ")); 14665 iocp = (struct iocblk *)mp->b_rptr; 14666 switch (iocp->ioc_cmd) { 14667 case DL_IOC_HDR_INFO: 14668 ill = (ill_t *)q->q_ptr; 14669 ill_fastpath_ack(ill, mp); 14670 return (B_TRUE); 14671 case SIOCSTUNPARAM: 14672 case OSIOCSTUNPARAM: 14673 /* Go through qwriter_ip */ 14674 break; 14675 case SIOCGTUNPARAM: 14676 case OSIOCGTUNPARAM: 14677 ip_rput_other(NULL, q, mp, NULL); 14678 return (B_TRUE); 14679 default: 14680 putnext(q, mp); 14681 return (B_TRUE); 14682 } 14683 /* FALLTHRU */ 14684 case M_ERROR: 14685 case M_HANGUP: 14686 /* 14687 * Since this is on the ill stream we unconditionally 14688 * bump up the refcount 14689 */ 14690 ill_refhold(ill); 14691 qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); 14692 return (B_TRUE); 14693 case M_CTL: 14694 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 14695 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 14696 IPHADA_M_CTL)) { 14697 /* 14698 * It's an IPsec accelerated packet. 14699 * Make sure that the ill from which we received the 14700 * packet has enabled IPsec hardware acceleration. 14701 */ 14702 if (!(ill->ill_capabilities & 14703 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 14704 /* IPsec kstats: bean counter */ 14705 freemsg(mp); 14706 return (B_TRUE); 14707 } 14708 14709 /* 14710 * Make mp point to the mblk following the M_CTL, 14711 * then process according to type of mp. 14712 * After this processing, first_mp will point to 14713 * the data-attributes and mp to the pkt following 14714 * the M_CTL. 14715 */ 14716 mp = first_mp->b_cont; 14717 if (mp == NULL) { 14718 freemsg(first_mp); 14719 return (B_TRUE); 14720 } 14721 /* 14722 * A Hardware Accelerated packet can only be M_DATA 14723 * ESP or AH packet. 14724 */ 14725 if (mp->b_datap->db_type != M_DATA) { 14726 /* non-M_DATA IPsec accelerated packet */ 14727 IPSECHW_DEBUG(IPSECHW_PKT, 14728 ("non-M_DATA IPsec accelerated pkt\n")); 14729 freemsg(first_mp); 14730 return (B_TRUE); 14731 } 14732 ipha = (ipha_t *)mp->b_rptr; 14733 if (ipha->ipha_protocol != IPPROTO_AH && 14734 ipha->ipha_protocol != IPPROTO_ESP) { 14735 IPSECHW_DEBUG(IPSECHW_PKT, 14736 ("non-M_DATA IPsec accelerated pkt\n")); 14737 freemsg(first_mp); 14738 return (B_TRUE); 14739 } 14740 *mpp = mp; 14741 return (B_FALSE); 14742 } 14743 putnext(q, mp); 14744 return (B_TRUE); 14745 case M_IOCNAK: 14746 ip1dbg(("got iocnak ")); 14747 iocp = (struct iocblk *)mp->b_rptr; 14748 switch (iocp->ioc_cmd) { 14749 case SIOCSTUNPARAM: 14750 case OSIOCSTUNPARAM: 14751 /* 14752 * Since this is on the ill stream we unconditionally 14753 * bump up the refcount 14754 */ 14755 ill_refhold(ill); 14756 qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); 14757 return (B_TRUE); 14758 case DL_IOC_HDR_INFO: 14759 case SIOCGTUNPARAM: 14760 case OSIOCGTUNPARAM: 14761 ip_rput_other(NULL, q, mp, NULL); 14762 return (B_TRUE); 14763 default: 14764 break; 14765 } 14766 /* FALLTHRU */ 14767 default: 14768 putnext(q, mp); 14769 return (B_TRUE); 14770 } 14771 } 14772 14773 /* Read side put procedure. Packets coming from the wire arrive here. */ 14774 void 14775 ip_rput(queue_t *q, mblk_t *mp) 14776 { 14777 ill_t *ill; 14778 union DL_primitives *dl; 14779 14780 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 14781 14782 ill = (ill_t *)q->q_ptr; 14783 14784 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 14785 /* 14786 * If things are opening or closing, only accept high-priority 14787 * DLPI messages. (On open ill->ill_ipif has not yet been 14788 * created; on close, things hanging off the ill may have been 14789 * freed already.) 14790 */ 14791 dl = (union DL_primitives *)mp->b_rptr; 14792 if (DB_TYPE(mp) != M_PCPROTO || 14793 dl->dl_primitive == DL_UNITDATA_IND) { 14794 /* 14795 * SIOC[GS]TUNPARAM ioctls can come here. 14796 */ 14797 inet_freemsg(mp); 14798 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14799 "ip_rput_end: q %p (%S)", q, "uninit"); 14800 return; 14801 } 14802 } 14803 14804 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14805 "ip_rput_end: q %p (%S)", q, "end"); 14806 14807 ip_input(ill, NULL, mp, NULL); 14808 } 14809 14810 static mblk_t * 14811 ip_fix_dbref(ill_t *ill, mblk_t *mp) 14812 { 14813 mblk_t *mp1; 14814 boolean_t adjusted = B_FALSE; 14815 ip_stack_t *ipst = ill->ill_ipst; 14816 14817 IP_STAT(ipst, ip_db_ref); 14818 /* 14819 * The IP_RECVSLLA option depends on having the 14820 * link layer header. First check that: 14821 * a> the underlying device is of type ether, 14822 * since this option is currently supported only 14823 * over ethernet. 14824 * b> there is enough room to copy over the link 14825 * layer header. 14826 * 14827 * Once the checks are done, adjust rptr so that 14828 * the link layer header will be copied via 14829 * copymsg. Note that, IFT_ETHER may be returned 14830 * by some non-ethernet drivers but in this case 14831 * the second check will fail. 14832 */ 14833 if (ill->ill_type == IFT_ETHER && 14834 (mp->b_rptr - mp->b_datap->db_base) >= 14835 sizeof (struct ether_header)) { 14836 mp->b_rptr -= sizeof (struct ether_header); 14837 adjusted = B_TRUE; 14838 } 14839 mp1 = copymsg(mp); 14840 14841 if (mp1 == NULL) { 14842 mp->b_next = NULL; 14843 /* clear b_prev - used by ip_mroute_decap */ 14844 mp->b_prev = NULL; 14845 freemsg(mp); 14846 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14847 return (NULL); 14848 } 14849 14850 if (adjusted) { 14851 /* 14852 * Copy is done. Restore the pointer in 14853 * the _new_ mblk 14854 */ 14855 mp1->b_rptr += sizeof (struct ether_header); 14856 } 14857 14858 /* Copy b_prev - used by ip_mroute_decap */ 14859 mp1->b_prev = mp->b_prev; 14860 mp->b_prev = NULL; 14861 14862 /* preserve the hardware checksum flags and data, if present */ 14863 if (DB_CKSUMFLAGS(mp) != 0) { 14864 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp); 14865 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp); 14866 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp); 14867 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp); 14868 DB_CKSUM16(mp1) = DB_CKSUM16(mp); 14869 } 14870 14871 freemsg(mp); 14872 return (mp1); 14873 } 14874 14875 #define ADD_TO_CHAIN(head, tail, cnt, mp) { \ 14876 if (tail != NULL) \ 14877 tail->b_next = mp; \ 14878 else \ 14879 head = mp; \ 14880 tail = mp; \ 14881 cnt++; \ 14882 } 14883 14884 /* 14885 * Direct read side procedure capable of dealing with chains. GLDv3 based 14886 * drivers call this function directly with mblk chains while STREAMS 14887 * read side procedure ip_rput() calls this for single packet with ip_ring 14888 * set to NULL to process one packet at a time. 14889 * 14890 * The ill will always be valid if this function is called directly from 14891 * the driver. 14892 * 14893 * If ip_input() is called from GLDv3: 14894 * 14895 * - This must be a non-VLAN IP stream. 14896 * - 'mp' is either an untagged or a special priority-tagged packet. 14897 * - Any VLAN tag that was in the MAC header has been stripped. 14898 * 14899 * If the IP header in packet is not 32-bit aligned, every message in the 14900 * chain will be aligned before further operations. This is required on SPARC 14901 * platform. 14902 */ 14903 /* ARGSUSED */ 14904 void 14905 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, 14906 struct mac_header_info_s *mhip) 14907 { 14908 ipaddr_t dst = NULL; 14909 ipaddr_t prev_dst; 14910 ire_t *ire = NULL; 14911 ipha_t *ipha; 14912 uint_t pkt_len; 14913 ssize_t len; 14914 uint_t opt_len; 14915 int ll_multicast; 14916 int cgtp_flt_pkt; 14917 queue_t *q = ill->ill_rq; 14918 squeue_t *curr_sqp = NULL; 14919 mblk_t *head = NULL; 14920 mblk_t *tail = NULL; 14921 mblk_t *first_mp; 14922 int cnt = 0; 14923 ip_stack_t *ipst = ill->ill_ipst; 14924 mblk_t *mp; 14925 mblk_t *dmp; 14926 uint8_t tag; 14927 14928 ASSERT(mp_chain != NULL); 14929 ASSERT(ill != NULL); 14930 14931 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 14932 14933 tag = (ip_ring != NULL) ? SQTAG_IP_INPUT_RX_RING : SQTAG_IP_INPUT; 14934 14935 #define rptr ((uchar_t *)ipha) 14936 14937 while (mp_chain != NULL) { 14938 mp = mp_chain; 14939 mp_chain = mp_chain->b_next; 14940 mp->b_next = NULL; 14941 ll_multicast = 0; 14942 14943 /* 14944 * We do ire caching from one iteration to 14945 * another. In the event the packet chain contains 14946 * all packets from the same dst, this caching saves 14947 * an ire_cache_lookup for each of the succeeding 14948 * packets in a packet chain. 14949 */ 14950 prev_dst = dst; 14951 14952 /* 14953 * if db_ref > 1 then copymsg and free original. Packet 14954 * may be changed and we do not want the other entity 14955 * who has a reference to this message to trip over the 14956 * changes. This is a blind change because trying to 14957 * catch all places that might change the packet is too 14958 * difficult. 14959 * 14960 * This corresponds to the fast path case, where we have 14961 * a chain of M_DATA mblks. We check the db_ref count 14962 * of only the 1st data block in the mblk chain. There 14963 * doesn't seem to be a reason why a device driver would 14964 * send up data with varying db_ref counts in the mblk 14965 * chain. In any case the Fast path is a private 14966 * interface, and our drivers don't do such a thing. 14967 * Given the above assumption, there is no need to walk 14968 * down the entire mblk chain (which could have a 14969 * potential performance problem) 14970 * 14971 * The "(DB_REF(mp) > 1)" check was moved from ip_rput() 14972 * to here because of exclusive ip stacks and vnics. 14973 * Packets transmitted from exclusive stack over vnic 14974 * can have db_ref > 1 and when it gets looped back to 14975 * another vnic in a different zone, you have ip_input() 14976 * getting dblks with db_ref > 1. So if someone 14977 * complains of TCP performance under this scenario, 14978 * take a serious look here on the impact of copymsg(). 14979 */ 14980 14981 if (DB_REF(mp) > 1) { 14982 if ((mp = ip_fix_dbref(ill, mp)) == NULL) 14983 continue; 14984 } 14985 14986 /* 14987 * Check and align the IP header. 14988 */ 14989 first_mp = mp; 14990 if (DB_TYPE(mp) == M_DATA) { 14991 dmp = mp; 14992 } else if (DB_TYPE(mp) == M_PROTO && 14993 *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { 14994 dmp = mp->b_cont; 14995 } else { 14996 dmp = NULL; 14997 } 14998 if (dmp != NULL) { 14999 /* 15000 * IP header ptr not aligned? 15001 * OR IP header not complete in first mblk 15002 */ 15003 if (!OK_32PTR(dmp->b_rptr) || 15004 MBLKL(dmp) < IP_SIMPLE_HDR_LENGTH) { 15005 if (!ip_check_and_align_header(q, dmp, ipst)) 15006 continue; 15007 } 15008 } 15009 15010 /* 15011 * ip_input fast path 15012 */ 15013 15014 /* mblk type is not M_DATA */ 15015 if (DB_TYPE(mp) != M_DATA) { 15016 if (ip_rput_process_notdata(q, &first_mp, ill, 15017 &ll_multicast, &mp)) 15018 continue; 15019 15020 /* 15021 * The only way we can get here is if we had a 15022 * packet that was either a DL_UNITDATA_IND or 15023 * an M_CTL for an IPsec accelerated packet. 15024 * 15025 * In either case, the first_mp will point to 15026 * the leading M_PROTO or M_CTL. 15027 */ 15028 ASSERT(first_mp != NULL); 15029 } else if (mhip != NULL) { 15030 /* 15031 * ll_multicast is set here so that it is ready 15032 * for easy use with FW_HOOKS(). ip_get_dlpi_mbcast 15033 * manipulates ll_multicast in the same fashion when 15034 * called from ip_rput_process_notdata. 15035 */ 15036 switch (mhip->mhi_dsttype) { 15037 case MAC_ADDRTYPE_MULTICAST : 15038 ll_multicast = HPE_MULTICAST; 15039 break; 15040 case MAC_ADDRTYPE_BROADCAST : 15041 ll_multicast = HPE_BROADCAST; 15042 break; 15043 default : 15044 break; 15045 } 15046 } 15047 15048 /* Only M_DATA can come here and it is always aligned */ 15049 ASSERT(DB_TYPE(mp) == M_DATA); 15050 ASSERT(DB_REF(mp) == 1 && OK_32PTR(mp->b_rptr)); 15051 15052 ipha = (ipha_t *)mp->b_rptr; 15053 len = mp->b_wptr - rptr; 15054 pkt_len = ntohs(ipha->ipha_length); 15055 15056 /* 15057 * We must count all incoming packets, even if they end 15058 * up being dropped later on. 15059 */ 15060 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 15061 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); 15062 15063 /* multiple mblk or too short */ 15064 len -= pkt_len; 15065 if (len != 0) { 15066 /* 15067 * Make sure we have data length consistent 15068 * with the IP header. 15069 */ 15070 if (mp->b_cont == NULL) { 15071 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15072 BUMP_MIB(ill->ill_ip_mib, 15073 ipIfStatsInHdrErrors); 15074 ip2dbg(("ip_input: drop pkt\n")); 15075 freemsg(mp); 15076 continue; 15077 } 15078 mp->b_wptr = rptr + pkt_len; 15079 } else if ((len += msgdsize(mp->b_cont)) != 0) { 15080 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 15081 BUMP_MIB(ill->ill_ip_mib, 15082 ipIfStatsInHdrErrors); 15083 ip2dbg(("ip_input: drop pkt\n")); 15084 freemsg(mp); 15085 continue; 15086 } 15087 (void) adjmsg(mp, -len); 15088 IP_STAT(ipst, ip_multimblk3); 15089 } 15090 } 15091 15092 /* Obtain the dst of the current packet */ 15093 dst = ipha->ipha_dst; 15094 15095 DTRACE_IP7(receive, mblk_t *, first_mp, conn_t *, NULL, 15096 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill, ipha_t *, 15097 ipha, ip6_t *, NULL, int, 0); 15098 15099 /* 15100 * The following test for loopback is faster than 15101 * IP_LOOPBACK_ADDR(), because it avoids any bitwise 15102 * operations. 15103 * Note that these addresses are always in network byte order 15104 */ 15105 if (((*(uchar_t *)&ipha->ipha_dst) == 127) || 15106 ((*(uchar_t *)&ipha->ipha_src) == 127)) { 15107 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors); 15108 freemsg(mp); 15109 continue; 15110 } 15111 15112 /* 15113 * The event for packets being received from a 'physical' 15114 * interface is placed after validation of the source and/or 15115 * destination address as being local so that packets can be 15116 * redirected to loopback addresses using ipnat. 15117 */ 15118 DTRACE_PROBE4(ip4__physical__in__start, 15119 ill_t *, ill, ill_t *, NULL, 15120 ipha_t *, ipha, mblk_t *, first_mp); 15121 15122 FW_HOOKS(ipst->ips_ip4_physical_in_event, 15123 ipst->ips_ipv4firewall_physical_in, 15124 ill, NULL, ipha, first_mp, mp, ll_multicast, ipst); 15125 15126 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 15127 15128 if (first_mp == NULL) { 15129 continue; 15130 } 15131 dst = ipha->ipha_dst; 15132 /* 15133 * Attach any necessary label information to 15134 * this packet 15135 */ 15136 if (is_system_labeled() && 15137 !tsol_get_pkt_label(mp, IPV4_VERSION)) { 15138 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 15139 freemsg(mp); 15140 continue; 15141 } 15142 15143 if (ipst->ips_ipobs_enabled) { 15144 zoneid_t dzone; 15145 15146 /* 15147 * On the inbound path the src zone will be unknown as 15148 * this packet has come from the wire. 15149 */ 15150 dzone = ip_get_zoneid_v4(dst, mp, ipst, ALL_ZONES); 15151 ipobs_hook(mp, IPOBS_HOOK_INBOUND, ALL_ZONES, dzone, 15152 ill, IPV4_VERSION, 0, ipst); 15153 } 15154 15155 /* 15156 * Reuse the cached ire only if the ipha_dst of the previous 15157 * packet is the same as the current packet AND it is not 15158 * INADDR_ANY. 15159 */ 15160 if (!(dst == prev_dst && dst != INADDR_ANY) && 15161 (ire != NULL)) { 15162 ire_refrele(ire); 15163 ire = NULL; 15164 } 15165 15166 opt_len = ipha->ipha_version_and_hdr_length - 15167 IP_SIMPLE_HDR_VERSION; 15168 15169 /* 15170 * Check to see if we can take the fastpath. 15171 * That is possible if the following conditions are met 15172 * o Tsol disabled 15173 * o CGTP disabled 15174 * o ipp_action_count is 0 15175 * o no options in the packet 15176 * o not a RSVP packet 15177 * o not a multicast packet 15178 * o ill not in IP_DHCPINIT_IF mode 15179 */ 15180 if (!is_system_labeled() && 15181 !ipst->ips_ip_cgtp_filter && ipp_action_count == 0 && 15182 opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && 15183 !ll_multicast && !CLASSD(dst) && ill->ill_dhcpinit == 0) { 15184 if (ire == NULL) 15185 ire = ire_cache_lookup_simple(dst, ipst); 15186 /* 15187 * Unless forwarding is enabled, dont call 15188 * ip_fast_forward(). Incoming packet is for forwarding 15189 */ 15190 if ((ill->ill_flags & ILLF_ROUTER) && 15191 (ire == NULL || (ire->ire_type & IRE_CACHE))) { 15192 ire = ip_fast_forward(ire, dst, ill, mp); 15193 continue; 15194 } 15195 /* incoming packet is for local consumption */ 15196 if ((ire != NULL) && (ire->ire_type & IRE_LOCAL)) 15197 goto local; 15198 } 15199 15200 /* 15201 * Disable ire caching for anything more complex 15202 * than the simple fast path case we checked for above. 15203 */ 15204 if (ire != NULL) { 15205 ire_refrele(ire); 15206 ire = NULL; 15207 } 15208 15209 /* 15210 * Brutal hack for DHCPv4 unicast: RFC2131 allows a DHCP 15211 * server to unicast DHCP packets to a DHCP client using the 15212 * IP address it is offering to the client. This can be 15213 * disabled through the "broadcast bit", but not all DHCP 15214 * servers honor that bit. Therefore, to interoperate with as 15215 * many DHCP servers as possible, the DHCP client allows the 15216 * server to unicast, but we treat those packets as broadcast 15217 * here. Note that we don't rewrite the packet itself since 15218 * (a) that would mess up the checksums and (b) the DHCP 15219 * client conn is bound to INADDR_ANY so ip_fanout_udp() will 15220 * hand it the packet regardless. 15221 */ 15222 if (ill->ill_dhcpinit != 0 && 15223 IS_SIMPLE_IPH(ipha) && ipha->ipha_protocol == IPPROTO_UDP && 15224 pullupmsg(mp, sizeof (ipha_t) + sizeof (udpha_t)) == 1) { 15225 udpha_t *udpha; 15226 15227 /* 15228 * Reload ipha since pullupmsg() can change b_rptr. 15229 */ 15230 ipha = (ipha_t *)mp->b_rptr; 15231 udpha = (udpha_t *)&ipha[1]; 15232 15233 if (ntohs(udpha->uha_dst_port) == IPPORT_BOOTPC) { 15234 DTRACE_PROBE2(ip4__dhcpinit__pkt, ill_t *, ill, 15235 mblk_t *, mp); 15236 dst = INADDR_BROADCAST; 15237 } 15238 } 15239 15240 /* Full-blown slow path */ 15241 if (opt_len != 0) { 15242 if (len != 0) 15243 IP_STAT(ipst, ip_multimblk4); 15244 else 15245 IP_STAT(ipst, ip_ipoptions); 15246 if (!ip_rput_multimblk_ipoptions(q, ill, mp, &ipha, 15247 &dst, ipst)) 15248 continue; 15249 } 15250 15251 /* 15252 * Invoke the CGTP (multirouting) filtering module to process 15253 * the incoming packet. Packets identified as duplicates 15254 * must be discarded. Filtering is active only if the 15255 * the ip_cgtp_filter ndd variable is non-zero. 15256 */ 15257 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 15258 if (ipst->ips_ip_cgtp_filter && 15259 ipst->ips_ip_cgtp_filter_ops != NULL) { 15260 netstackid_t stackid; 15261 15262 stackid = ipst->ips_netstack->netstack_stackid; 15263 cgtp_flt_pkt = 15264 ipst->ips_ip_cgtp_filter_ops->cfo_filter(stackid, 15265 ill->ill_phyint->phyint_ifindex, mp); 15266 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 15267 freemsg(first_mp); 15268 continue; 15269 } 15270 } 15271 15272 /* 15273 * If rsvpd is running, let RSVP daemon handle its processing 15274 * and forwarding of RSVP multicast/unicast packets. 15275 * If rsvpd is not running but mrouted is running, RSVP 15276 * multicast packets are forwarded as multicast traffic 15277 * and RSVP unicast packets are forwarded by unicast router. 15278 * If neither rsvpd nor mrouted is running, RSVP multicast 15279 * packets are not forwarded, but the unicast packets are 15280 * forwarded like unicast traffic. 15281 */ 15282 if (ipha->ipha_protocol == IPPROTO_RSVP && 15283 ipst->ips_ipcl_proto_fanout[IPPROTO_RSVP].connf_head != 15284 NULL) { 15285 /* RSVP packet and rsvpd running. Treat as ours */ 15286 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 15287 /* 15288 * This assumes that we deliver to all streams for 15289 * multicast and broadcast packets. 15290 * We have to force ll_multicast to 1 to handle the 15291 * M_DATA messages passed in from ip_mroute_decap. 15292 */ 15293 dst = INADDR_BROADCAST; 15294 ll_multicast = 1; 15295 } else if (CLASSD(dst)) { 15296 /* packet is multicast */ 15297 mp->b_next = NULL; 15298 if (ip_rput_process_multicast(q, mp, ill, ipha, 15299 &ll_multicast, &dst)) 15300 continue; 15301 } 15302 15303 if (ire == NULL) { 15304 ire = ire_cache_lookup(dst, ALL_ZONES, 15305 msg_getlabel(mp), ipst); 15306 } 15307 15308 if (ire != NULL && ire->ire_stq != NULL && 15309 ire->ire_zoneid != GLOBAL_ZONEID && 15310 ire->ire_zoneid != ALL_ZONES) { 15311 /* 15312 * Should only use IREs that are visible from the 15313 * global zone for forwarding. 15314 */ 15315 ire_refrele(ire); 15316 ire = ire_cache_lookup(dst, GLOBAL_ZONEID, 15317 msg_getlabel(mp), ipst); 15318 } 15319 15320 if (ire == NULL) { 15321 /* 15322 * No IRE for this destination, so it can't be for us. 15323 * Unless we are forwarding, drop the packet. 15324 * We have to let source routed packets through 15325 * since we don't yet know if they are 'ping -l' 15326 * packets i.e. if they will go out over the 15327 * same interface as they came in on. 15328 */ 15329 ire = ip_rput_noire(q, mp, ll_multicast, dst); 15330 if (ire == NULL) 15331 continue; 15332 } 15333 15334 /* 15335 * Broadcast IRE may indicate either broadcast or 15336 * multicast packet 15337 */ 15338 if (ire->ire_type == IRE_BROADCAST) { 15339 /* 15340 * Skip broadcast checks if packet is UDP multicast; 15341 * we'd rather not enter ip_rput_process_broadcast() 15342 * unless the packet is broadcast for real, since 15343 * that routine is a no-op for multicast. 15344 */ 15345 if (ipha->ipha_protocol != IPPROTO_UDP || 15346 !CLASSD(ipha->ipha_dst)) { 15347 ire = ip_rput_process_broadcast(&q, mp, 15348 ire, ipha, ill, dst, cgtp_flt_pkt, 15349 ll_multicast); 15350 if (ire == NULL) 15351 continue; 15352 } 15353 } else if (ire->ire_stq != NULL) { 15354 /* fowarding? */ 15355 ip_rput_process_forward(q, mp, ire, ipha, ill, 15356 ll_multicast, B_FALSE); 15357 /* ip_rput_process_forward consumed the packet */ 15358 continue; 15359 } 15360 15361 local: 15362 /* 15363 * If the queue in the ire is different to the ingress queue 15364 * then we need to check to see if we can accept the packet. 15365 * Note that for multicast packets and broadcast packets sent 15366 * to a broadcast address which is shared between multiple 15367 * interfaces we should not do this since we just got a random 15368 * broadcast ire. 15369 */ 15370 if ((ire->ire_rfq != q) && (ire->ire_type != IRE_BROADCAST)) { 15371 ire = ip_check_multihome(&ipha->ipha_dst, ire, ill); 15372 if (ire == NULL) { 15373 /* Drop packet */ 15374 BUMP_MIB(ill->ill_ip_mib, 15375 ipIfStatsForwProhibits); 15376 freemsg(mp); 15377 continue; 15378 } 15379 if (ire->ire_rfq != NULL) 15380 q = ire->ire_rfq; 15381 } 15382 15383 switch (ipha->ipha_protocol) { 15384 case IPPROTO_TCP: 15385 ASSERT(first_mp == mp); 15386 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 15387 mp, 0, q, ip_ring)) != NULL) { 15388 if (curr_sqp == NULL) { 15389 curr_sqp = GET_SQUEUE(mp); 15390 ASSERT(cnt == 0); 15391 cnt++; 15392 head = tail = mp; 15393 } else if (curr_sqp == GET_SQUEUE(mp)) { 15394 ASSERT(tail != NULL); 15395 cnt++; 15396 tail->b_next = mp; 15397 tail = mp; 15398 } else { 15399 /* 15400 * A different squeue. Send the 15401 * chain for the previous squeue on 15402 * its way. This shouldn't happen 15403 * often unless interrupt binding 15404 * changes. 15405 */ 15406 IP_STAT(ipst, ip_input_multi_squeue); 15407 SQUEUE_ENTER(curr_sqp, head, 15408 tail, cnt, SQ_PROCESS, tag); 15409 curr_sqp = GET_SQUEUE(mp); 15410 head = mp; 15411 tail = mp; 15412 cnt = 1; 15413 } 15414 } 15415 continue; 15416 case IPPROTO_UDP: 15417 ASSERT(first_mp == mp); 15418 ip_udp_input(q, mp, ipha, ire, ill); 15419 continue; 15420 case IPPROTO_SCTP: 15421 ASSERT(first_mp == mp); 15422 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 15423 q, dst); 15424 /* ire has been released by ip_sctp_input */ 15425 ire = NULL; 15426 continue; 15427 default: 15428 ip_proto_input(q, first_mp, ipha, ire, ill, 0); 15429 continue; 15430 } 15431 } 15432 15433 if (ire != NULL) 15434 ire_refrele(ire); 15435 15436 if (head != NULL) 15437 SQUEUE_ENTER(curr_sqp, head, tail, cnt, SQ_PROCESS, tag); 15438 15439 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15440 "ip_input_end: q %p (%S)", q, "end"); 15441 #undef rptr 15442 } 15443 15444 /* 15445 * ip_accept_tcp() - This function is called by the squeue when it retrieves 15446 * a chain of packets in the poll mode. The packets have gone through the 15447 * data link processing but not IP processing. For performance and latency 15448 * reasons, the squeue wants to process the chain in line instead of feeding 15449 * it back via ip_input path. 15450 * 15451 * So this is a light weight function which checks to see if the packets 15452 * retrived are indeed TCP packets (TCP squeue always polls TCP soft ring 15453 * but we still do the paranoid check) meant for local machine and we don't 15454 * have labels etc enabled. Packets that meet the criterion are returned to 15455 * the squeue and processed inline while the rest go via ip_input path. 15456 */ 15457 /*ARGSUSED*/ 15458 mblk_t * 15459 ip_accept_tcp(ill_t *ill, ill_rx_ring_t *ip_ring, squeue_t *target_sqp, 15460 mblk_t *mp_chain, mblk_t **last, uint_t *cnt) 15461 { 15462 mblk_t *mp; 15463 ipaddr_t dst = NULL; 15464 ipaddr_t prev_dst; 15465 ire_t *ire = NULL; 15466 ipha_t *ipha; 15467 uint_t pkt_len; 15468 ssize_t len; 15469 uint_t opt_len; 15470 queue_t *q = ill->ill_rq; 15471 squeue_t *curr_sqp; 15472 mblk_t *ahead = NULL; /* Accepted head */ 15473 mblk_t *atail = NULL; /* Accepted tail */ 15474 uint_t acnt = 0; /* Accepted count */ 15475 mblk_t *utail = NULL; /* Unaccepted head */ 15476 mblk_t *uhead = NULL; /* Unaccepted tail */ 15477 uint_t ucnt = 0; /* Unaccepted cnt */ 15478 ip_stack_t *ipst = ill->ill_ipst; 15479 15480 *cnt = 0; 15481 15482 ASSERT(ill != NULL); 15483 ASSERT(ip_ring != NULL); 15484 15485 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_accept_tcp: q %p", q); 15486 15487 #define rptr ((uchar_t *)ipha) 15488 15489 while (mp_chain != NULL) { 15490 mp = mp_chain; 15491 mp_chain = mp_chain->b_next; 15492 mp->b_next = NULL; 15493 15494 /* 15495 * We do ire caching from one iteration to 15496 * another. In the event the packet chain contains 15497 * all packets from the same dst, this caching saves 15498 * an ire_cache_lookup for each of the succeeding 15499 * packets in a packet chain. 15500 */ 15501 prev_dst = dst; 15502 15503 ipha = (ipha_t *)mp->b_rptr; 15504 len = mp->b_wptr - rptr; 15505 15506 ASSERT(!MBLK_RX_FANOUT_SLOWPATH(mp, ipha)); 15507 15508 /* 15509 * If it is a non TCP packet, or doesn't have H/W cksum, 15510 * or doesn't have min len, reject. 15511 */ 15512 if ((ipha->ipha_protocol != IPPROTO_TCP) || (len < 15513 (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH))) { 15514 ADD_TO_CHAIN(uhead, utail, ucnt, mp); 15515 continue; 15516 } 15517 15518 pkt_len = ntohs(ipha->ipha_length); 15519 if (len != pkt_len) { 15520 if (len > pkt_len) { 15521 mp->b_wptr = rptr + pkt_len; 15522 } else { 15523 ADD_TO_CHAIN(uhead, utail, ucnt, mp); 15524 continue; 15525 } 15526 } 15527 15528 opt_len = ipha->ipha_version_and_hdr_length - 15529 IP_SIMPLE_HDR_VERSION; 15530 dst = ipha->ipha_dst; 15531 15532 /* IP version bad or there are IP options */ 15533 if (opt_len && (!ip_rput_multimblk_ipoptions(q, ill, 15534 mp, &ipha, &dst, ipst))) 15535 continue; 15536 15537 if (is_system_labeled() || (ill->ill_dhcpinit != 0) || 15538 (ipst->ips_ip_cgtp_filter && 15539 ipst->ips_ip_cgtp_filter_ops != NULL)) { 15540 ADD_TO_CHAIN(uhead, utail, ucnt, mp); 15541 continue; 15542 } 15543 15544 /* 15545 * Reuse the cached ire only if the ipha_dst of the previous 15546 * packet is the same as the current packet AND it is not 15547 * INADDR_ANY. 15548 */ 15549 if (!(dst == prev_dst && dst != INADDR_ANY) && 15550 (ire != NULL)) { 15551 ire_refrele(ire); 15552 ire = NULL; 15553 } 15554 15555 if (ire == NULL) 15556 ire = ire_cache_lookup_simple(dst, ipst); 15557 15558 /* 15559 * Unless forwarding is enabled, dont call 15560 * ip_fast_forward(). Incoming packet is for forwarding 15561 */ 15562 if ((ill->ill_flags & ILLF_ROUTER) && 15563 (ire == NULL || (ire->ire_type & IRE_CACHE))) { 15564 15565 DTRACE_PROBE4(ip4__physical__in__start, 15566 ill_t *, ill, ill_t *, NULL, 15567 ipha_t *, ipha, mblk_t *, mp); 15568 15569 FW_HOOKS(ipst->ips_ip4_physical_in_event, 15570 ipst->ips_ipv4firewall_physical_in, 15571 ill, NULL, ipha, mp, mp, 0, ipst); 15572 15573 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, mp); 15574 15575 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 15576 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, 15577 pkt_len); 15578 15579 if (mp != NULL) 15580 ire = ip_fast_forward(ire, dst, ill, mp); 15581 continue; 15582 } 15583 15584 /* incoming packet is for local consumption */ 15585 if ((ire != NULL) && (ire->ire_type & IRE_LOCAL)) 15586 goto local_accept; 15587 15588 /* 15589 * Disable ire caching for anything more complex 15590 * than the simple fast path case we checked for above. 15591 */ 15592 if (ire != NULL) { 15593 ire_refrele(ire); 15594 ire = NULL; 15595 } 15596 15597 ire = ire_cache_lookup(dst, ALL_ZONES, msg_getlabel(mp), 15598 ipst); 15599 if (ire == NULL || ire->ire_type == IRE_BROADCAST || 15600 ire->ire_stq != NULL) { 15601 ADD_TO_CHAIN(uhead, utail, ucnt, mp); 15602 if (ire != NULL) { 15603 ire_refrele(ire); 15604 ire = NULL; 15605 } 15606 continue; 15607 } 15608 15609 local_accept: 15610 15611 if (ire->ire_rfq != q) { 15612 ADD_TO_CHAIN(uhead, utail, ucnt, mp); 15613 if (ire != NULL) { 15614 ire_refrele(ire); 15615 ire = NULL; 15616 } 15617 continue; 15618 } 15619 15620 /* 15621 * The event for packets being received from a 'physical' 15622 * interface is placed after validation of the source and/or 15623 * destination address as being local so that packets can be 15624 * redirected to loopback addresses using ipnat. 15625 */ 15626 DTRACE_PROBE4(ip4__physical__in__start, 15627 ill_t *, ill, ill_t *, NULL, 15628 ipha_t *, ipha, mblk_t *, mp); 15629 15630 FW_HOOKS(ipst->ips_ip4_physical_in_event, 15631 ipst->ips_ipv4firewall_physical_in, 15632 ill, NULL, ipha, mp, mp, 0, ipst); 15633 15634 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, mp); 15635 15636 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 15637 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); 15638 15639 if (mp != NULL && 15640 (mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, mp, 15641 0, q, ip_ring)) != NULL) { 15642 if ((curr_sqp = GET_SQUEUE(mp)) == target_sqp) { 15643 ADD_TO_CHAIN(ahead, atail, acnt, mp); 15644 } else { 15645 SQUEUE_ENTER(curr_sqp, mp, mp, 1, 15646 SQ_FILL, SQTAG_IP_INPUT); 15647 } 15648 } 15649 } 15650 15651 if (ire != NULL) 15652 ire_refrele(ire); 15653 15654 if (uhead != NULL) 15655 ip_input(ill, ip_ring, uhead, NULL); 15656 15657 if (ahead != NULL) { 15658 *last = atail; 15659 *cnt = acnt; 15660 return (ahead); 15661 } 15662 15663 return (NULL); 15664 #undef rptr 15665 } 15666 15667 static void 15668 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 15669 t_uscalar_t err) 15670 { 15671 if (dl_err == DL_SYSERR) { 15672 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15673 "%s: %s failed: DL_SYSERR (errno %u)\n", 15674 ill->ill_name, dl_primstr(prim), err); 15675 return; 15676 } 15677 15678 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15679 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim), 15680 dl_errstr(dl_err)); 15681 } 15682 15683 /* 15684 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 15685 * than DL_UNITDATA_IND messages. If we need to process this message 15686 * exclusively, we call qwriter_ip, in which case we also need to call 15687 * ill_refhold before that, since qwriter_ip does an ill_refrele. 15688 */ 15689 void 15690 ip_rput_dlpi(queue_t *q, mblk_t *mp) 15691 { 15692 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15693 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15694 ill_t *ill = q->q_ptr; 15695 t_uscalar_t prim = dloa->dl_primitive; 15696 t_uscalar_t reqprim = DL_PRIM_INVAL; 15697 15698 ip1dbg(("ip_rput_dlpi")); 15699 15700 /* 15701 * If we received an ACK but didn't send a request for it, then it 15702 * can't be part of any pending operation; discard up-front. 15703 */ 15704 switch (prim) { 15705 case DL_ERROR_ACK: 15706 reqprim = dlea->dl_error_primitive; 15707 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s " 15708 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim), 15709 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno, 15710 dlea->dl_unix_errno)); 15711 break; 15712 case DL_OK_ACK: 15713 reqprim = dloa->dl_correct_primitive; 15714 break; 15715 case DL_INFO_ACK: 15716 reqprim = DL_INFO_REQ; 15717 break; 15718 case DL_BIND_ACK: 15719 reqprim = DL_BIND_REQ; 15720 break; 15721 case DL_PHYS_ADDR_ACK: 15722 reqprim = DL_PHYS_ADDR_REQ; 15723 break; 15724 case DL_NOTIFY_ACK: 15725 reqprim = DL_NOTIFY_REQ; 15726 break; 15727 case DL_CONTROL_ACK: 15728 reqprim = DL_CONTROL_REQ; 15729 break; 15730 case DL_CAPABILITY_ACK: 15731 reqprim = DL_CAPABILITY_REQ; 15732 break; 15733 } 15734 15735 if (prim != DL_NOTIFY_IND) { 15736 if (reqprim == DL_PRIM_INVAL || 15737 !ill_dlpi_pending(ill, reqprim)) { 15738 /* Not a DLPI message we support or expected */ 15739 freemsg(mp); 15740 return; 15741 } 15742 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim), 15743 dl_primstr(reqprim))); 15744 } 15745 15746 switch (reqprim) { 15747 case DL_UNBIND_REQ: 15748 /* 15749 * NOTE: we mark the unbind as complete even if we got a 15750 * DL_ERROR_ACK, since there's not much else we can do. 15751 */ 15752 mutex_enter(&ill->ill_lock); 15753 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15754 cv_signal(&ill->ill_cv); 15755 mutex_exit(&ill->ill_lock); 15756 break; 15757 15758 case DL_ENABMULTI_REQ: 15759 if (prim == DL_OK_ACK) { 15760 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15761 ill->ill_dlpi_multicast_state = IDS_OK; 15762 } 15763 break; 15764 } 15765 15766 /* 15767 * The message is one we're waiting for (or DL_NOTIFY_IND), but we 15768 * need to become writer to continue to process it. Because an 15769 * exclusive operation doesn't complete until replies to all queued 15770 * DLPI messages have been received, we know we're in the middle of an 15771 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND). 15772 * 15773 * As required by qwriter_ip(), we refhold the ill; it will refrele. 15774 * Since this is on the ill stream we unconditionally bump up the 15775 * refcount without doing ILL_CAN_LOOKUP(). 15776 */ 15777 ill_refhold(ill); 15778 if (prim == DL_NOTIFY_IND) 15779 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE); 15780 else 15781 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE); 15782 } 15783 15784 /* 15785 * Handling of DLPI messages that require exclusive access to the ipsq. 15786 * 15787 * Need to do ill_pending_mp_release on ioctl completion, which could 15788 * happen here. (along with mi_copy_done) 15789 */ 15790 /* ARGSUSED */ 15791 static void 15792 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15793 { 15794 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15795 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15796 int err = 0; 15797 ill_t *ill; 15798 ipif_t *ipif = NULL; 15799 mblk_t *mp1 = NULL; 15800 conn_t *connp = NULL; 15801 t_uscalar_t paddrreq; 15802 mblk_t *mp_hw; 15803 boolean_t success; 15804 boolean_t ioctl_aborted = B_FALSE; 15805 boolean_t log = B_TRUE; 15806 ip_stack_t *ipst; 15807 15808 ip1dbg(("ip_rput_dlpi_writer ..")); 15809 ill = (ill_t *)q->q_ptr; 15810 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop); 15811 ASSERT(IAM_WRITER_ILL(ill)); 15812 15813 ipst = ill->ill_ipst; 15814 15815 ipif = ipsq->ipsq_xop->ipx_pending_ipif; 15816 /* 15817 * The current ioctl could have been aborted by the user and a new 15818 * ioctl to bring up another ill could have started. We could still 15819 * get a response from the driver later. 15820 */ 15821 if (ipif != NULL && ipif->ipif_ill != ill) 15822 ioctl_aborted = B_TRUE; 15823 15824 switch (dloa->dl_primitive) { 15825 case DL_ERROR_ACK: 15826 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n", 15827 dl_primstr(dlea->dl_error_primitive))); 15828 15829 switch (dlea->dl_error_primitive) { 15830 case DL_DISABMULTI_REQ: 15831 ill_dlpi_done(ill, dlea->dl_error_primitive); 15832 break; 15833 case DL_PROMISCON_REQ: 15834 case DL_PROMISCOFF_REQ: 15835 case DL_UNBIND_REQ: 15836 case DL_ATTACH_REQ: 15837 case DL_INFO_REQ: 15838 ill_dlpi_done(ill, dlea->dl_error_primitive); 15839 break; 15840 case DL_NOTIFY_REQ: 15841 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15842 log = B_FALSE; 15843 break; 15844 case DL_PHYS_ADDR_REQ: 15845 /* 15846 * For IPv6 only, there are two additional 15847 * phys_addr_req's sent to the driver to get the 15848 * IPv6 token and lla. This allows IP to acquire 15849 * the hardware address format for a given interface 15850 * without having built in knowledge of the hardware 15851 * address. ill_phys_addr_pend keeps track of the last 15852 * DL_PAR sent so we know which response we are 15853 * dealing with. ill_dlpi_done will update 15854 * ill_phys_addr_pend when it sends the next req. 15855 * We don't complete the IOCTL until all three DL_PARs 15856 * have been attempted, so set *_len to 0 and break. 15857 */ 15858 paddrreq = ill->ill_phys_addr_pend; 15859 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15860 if (paddrreq == DL_IPV6_TOKEN) { 15861 ill->ill_token_length = 0; 15862 log = B_FALSE; 15863 break; 15864 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 15865 ill->ill_nd_lla_len = 0; 15866 log = B_FALSE; 15867 break; 15868 } 15869 /* 15870 * Something went wrong with the DL_PHYS_ADDR_REQ. 15871 * We presumably have an IOCTL hanging out waiting 15872 * for completion. Find it and complete the IOCTL 15873 * with the error noted. 15874 * However, ill_dl_phys was called on an ill queue 15875 * (from SIOCSLIFNAME), thus conn_pending_ill is not 15876 * set. But the ioctl is known to be pending on ill_wq. 15877 */ 15878 if (!ill->ill_ifname_pending) 15879 break; 15880 ill->ill_ifname_pending = 0; 15881 if (!ioctl_aborted) 15882 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15883 if (mp1 != NULL) { 15884 /* 15885 * This operation (SIOCSLIFNAME) must have 15886 * happened on the ill. Assert there is no conn 15887 */ 15888 ASSERT(connp == NULL); 15889 q = ill->ill_wq; 15890 } 15891 break; 15892 case DL_BIND_REQ: 15893 ill_dlpi_done(ill, DL_BIND_REQ); 15894 if (ill->ill_ifname_pending) 15895 break; 15896 /* 15897 * Something went wrong with the bind. We presumably 15898 * have an IOCTL hanging out waiting for completion. 15899 * Find it, take down the interface that was coming 15900 * up, and complete the IOCTL with the error noted. 15901 */ 15902 if (!ioctl_aborted) 15903 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15904 if (mp1 != NULL) { 15905 /* 15906 * This might be a result of a DL_NOTE_REPLUMB 15907 * notification. In that case, connp is NULL. 15908 */ 15909 if (connp != NULL) 15910 q = CONNP_TO_WQ(connp); 15911 15912 (void) ipif_down(ipif, NULL, NULL); 15913 /* error is set below the switch */ 15914 } 15915 break; 15916 case DL_ENABMULTI_REQ: 15917 ill_dlpi_done(ill, DL_ENABMULTI_REQ); 15918 15919 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15920 ill->ill_dlpi_multicast_state = IDS_FAILED; 15921 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 15922 ipif_t *ipif; 15923 15924 printf("ip: joining multicasts failed (%d)" 15925 " on %s - will use link layer " 15926 "broadcasts for multicast\n", 15927 dlea->dl_errno, ill->ill_name); 15928 15929 /* 15930 * Set up the multicast mapping alone. 15931 * writer, so ok to access ill->ill_ipif 15932 * without any lock. 15933 */ 15934 ipif = ill->ill_ipif; 15935 mutex_enter(&ill->ill_phyint->phyint_lock); 15936 ill->ill_phyint->phyint_flags |= 15937 PHYI_MULTI_BCAST; 15938 mutex_exit(&ill->ill_phyint->phyint_lock); 15939 15940 if (!ill->ill_isv6) { 15941 (void) ipif_arp_setup_multicast(ipif, 15942 NULL); 15943 } else { 15944 (void) ipif_ndp_setup_multicast(ipif, 15945 NULL); 15946 } 15947 } 15948 freemsg(mp); /* Don't want to pass this up */ 15949 return; 15950 case DL_CONTROL_REQ: 15951 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 15952 "DL_CONTROL_REQ\n")); 15953 ill_dlpi_done(ill, dlea->dl_error_primitive); 15954 freemsg(mp); 15955 return; 15956 case DL_CAPABILITY_REQ: 15957 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 15958 "DL_CAPABILITY REQ\n")); 15959 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT) 15960 ill->ill_dlpi_capab_state = IDCS_FAILED; 15961 ill_capability_done(ill); 15962 freemsg(mp); 15963 return; 15964 } 15965 /* 15966 * Note the error for IOCTL completion (mp1 is set when 15967 * ready to complete ioctl). If ill_ifname_pending_err is 15968 * set, an error occured during plumbing (ill_ifname_pending), 15969 * so we want to report that error. 15970 * 15971 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 15972 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 15973 * expected to get errack'd if the driver doesn't support 15974 * these flags (e.g. ethernet). log will be set to B_FALSE 15975 * if these error conditions are encountered. 15976 */ 15977 if (mp1 != NULL) { 15978 if (ill->ill_ifname_pending_err != 0) { 15979 err = ill->ill_ifname_pending_err; 15980 ill->ill_ifname_pending_err = 0; 15981 } else { 15982 err = dlea->dl_unix_errno ? 15983 dlea->dl_unix_errno : ENXIO; 15984 } 15985 /* 15986 * If we're plumbing an interface and an error hasn't already 15987 * been saved, set ill_ifname_pending_err to the error passed 15988 * up. Ignore the error if log is B_FALSE (see comment above). 15989 */ 15990 } else if (log && ill->ill_ifname_pending && 15991 ill->ill_ifname_pending_err == 0) { 15992 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 15993 dlea->dl_unix_errno : ENXIO; 15994 } 15995 15996 if (log) 15997 ip_dlpi_error(ill, dlea->dl_error_primitive, 15998 dlea->dl_errno, dlea->dl_unix_errno); 15999 break; 16000 case DL_CAPABILITY_ACK: 16001 ill_capability_ack(ill, mp); 16002 /* 16003 * The message has been handed off to ill_capability_ack 16004 * and must not be freed below 16005 */ 16006 mp = NULL; 16007 break; 16008 16009 case DL_CONTROL_ACK: 16010 /* We treat all of these as "fire and forget" */ 16011 ill_dlpi_done(ill, DL_CONTROL_REQ); 16012 break; 16013 case DL_INFO_ACK: 16014 /* Call a routine to handle this one. */ 16015 ill_dlpi_done(ill, DL_INFO_REQ); 16016 ip_ll_subnet_defaults(ill, mp); 16017 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 16018 return; 16019 case DL_BIND_ACK: 16020 /* 16021 * We should have an IOCTL waiting on this unless 16022 * sent by ill_dl_phys, in which case just return 16023 */ 16024 ill_dlpi_done(ill, DL_BIND_REQ); 16025 if (ill->ill_ifname_pending) 16026 break; 16027 16028 if (!ioctl_aborted) 16029 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16030 if (mp1 == NULL) 16031 break; 16032 /* 16033 * mp1 was added by ill_dl_up(). if that is a result of 16034 * a DL_NOTE_REPLUMB notification, connp could be NULL. 16035 */ 16036 if (connp != NULL) 16037 q = CONNP_TO_WQ(connp); 16038 16039 /* 16040 * We are exclusive. So nothing can change even after 16041 * we get the pending mp. If need be we can put it back 16042 * and restart, as in calling ipif_arp_up() below. 16043 */ 16044 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 16045 16046 mutex_enter(&ill->ill_lock); 16047 ill->ill_dl_up = 1; 16048 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0); 16049 mutex_exit(&ill->ill_lock); 16050 16051 /* 16052 * Now bring up the resolver; when that is complete, we'll 16053 * create IREs. Note that we intentionally mirror what 16054 * ipif_up() would have done, because we got here by way of 16055 * ill_dl_up(), which stopped ipif_up()'s processing. 16056 */ 16057 if (ill->ill_isv6) { 16058 if (ill->ill_flags & ILLF_XRESOLV) { 16059 if (connp != NULL) 16060 mutex_enter(&connp->conn_lock); 16061 mutex_enter(&ill->ill_lock); 16062 success = ipsq_pending_mp_add(connp, ipif, q, 16063 mp1, 0); 16064 mutex_exit(&ill->ill_lock); 16065 if (connp != NULL) 16066 mutex_exit(&connp->conn_lock); 16067 if (success) { 16068 err = ipif_resolver_up(ipif, 16069 Res_act_initial); 16070 if (err == EINPROGRESS) { 16071 freemsg(mp); 16072 return; 16073 } 16074 ASSERT(err != 0); 16075 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16076 ASSERT(mp1 != NULL); 16077 } else { 16078 /* conn has started closing */ 16079 err = EINTR; 16080 } 16081 } else { /* Non XRESOLV interface */ 16082 (void) ipif_resolver_up(ipif, Res_act_initial); 16083 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0) 16084 err = ipif_up_done_v6(ipif); 16085 } 16086 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 16087 /* 16088 * ARP and other v4 external resolvers. 16089 * Leave the pending mblk intact so that 16090 * the ioctl completes in ip_rput(). 16091 */ 16092 if (connp != NULL) 16093 mutex_enter(&connp->conn_lock); 16094 mutex_enter(&ill->ill_lock); 16095 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 16096 mutex_exit(&ill->ill_lock); 16097 if (connp != NULL) 16098 mutex_exit(&connp->conn_lock); 16099 if (success) { 16100 err = ipif_resolver_up(ipif, Res_act_initial); 16101 if (err == EINPROGRESS) { 16102 freemsg(mp); 16103 return; 16104 } 16105 ASSERT(err != 0); 16106 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16107 } else { 16108 /* The conn has started closing */ 16109 err = EINTR; 16110 } 16111 } else { 16112 /* 16113 * This one is complete. Reply to pending ioctl. 16114 */ 16115 (void) ipif_resolver_up(ipif, Res_act_initial); 16116 err = ipif_up_done(ipif); 16117 } 16118 16119 if ((err == 0) && (ill->ill_up_ipifs)) { 16120 err = ill_up_ipifs(ill, q, mp1); 16121 if (err == EINPROGRESS) { 16122 freemsg(mp); 16123 return; 16124 } 16125 } 16126 16127 /* 16128 * If we have a moved ipif to bring up, and everything has 16129 * succeeded to this point, bring it up on the IPMP ill. 16130 * Otherwise, leave it down -- the admin can try to bring it 16131 * up by hand if need be. 16132 */ 16133 if (ill->ill_move_ipif != NULL) { 16134 if (err != 0) { 16135 ill->ill_move_ipif = NULL; 16136 } else { 16137 ipif = ill->ill_move_ipif; 16138 ill->ill_move_ipif = NULL; 16139 err = ipif_up(ipif, q, mp1); 16140 if (err == EINPROGRESS) { 16141 freemsg(mp); 16142 return; 16143 } 16144 } 16145 } 16146 break; 16147 16148 case DL_NOTIFY_IND: { 16149 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 16150 ire_t *ire; 16151 uint_t orig_mtu; 16152 boolean_t need_ire_walk_v4 = B_FALSE; 16153 boolean_t need_ire_walk_v6 = B_FALSE; 16154 16155 switch (notify->dl_notification) { 16156 case DL_NOTE_PHYS_ADDR: 16157 err = ill_set_phys_addr(ill, mp); 16158 break; 16159 16160 case DL_NOTE_REPLUMB: 16161 /* 16162 * Directly return after calling ill_replumb(). 16163 * Note that we should not free mp as it is reused 16164 * in the ill_replumb() function. 16165 */ 16166 err = ill_replumb(ill, mp); 16167 return; 16168 16169 case DL_NOTE_FASTPATH_FLUSH: 16170 ill_fastpath_flush(ill); 16171 break; 16172 16173 case DL_NOTE_SDU_SIZE: 16174 /* 16175 * Change the MTU size of the interface, of all 16176 * attached ipif's, and of all relevant ire's. The 16177 * new value's a uint32_t at notify->dl_data. 16178 * Mtu change Vs. new ire creation - protocol below. 16179 * 16180 * a Mark the ipif as IPIF_CHANGING. 16181 * b Set the new mtu in the ipif. 16182 * c Change the ire_max_frag on all affected ires 16183 * d Unmark the IPIF_CHANGING 16184 * 16185 * To see how the protocol works, assume an interface 16186 * route is also being added simultaneously by 16187 * ip_rt_add and let 'ipif' be the ipif referenced by 16188 * the ire. If the ire is created before step a, 16189 * it will be cleaned up by step c. If the ire is 16190 * created after step d, it will see the new value of 16191 * ipif_mtu. Any attempt to create the ire between 16192 * steps a to d will fail because of the IPIF_CHANGING 16193 * flag. Note that ire_create() is passed a pointer to 16194 * the ipif_mtu, and not the value. During ire_add 16195 * under the bucket lock, the ire_max_frag of the 16196 * new ire being created is set from the ipif/ire from 16197 * which it is being derived. 16198 */ 16199 mutex_enter(&ill->ill_lock); 16200 16201 orig_mtu = ill->ill_max_mtu; 16202 ill->ill_max_frag = (uint_t)notify->dl_data; 16203 ill->ill_max_mtu = (uint_t)notify->dl_data; 16204 16205 /* 16206 * If ill_user_mtu was set (via SIOCSLIFLNKINFO), 16207 * clamp ill_max_mtu at it. 16208 */ 16209 if (ill->ill_user_mtu != 0 && 16210 ill->ill_user_mtu < ill->ill_max_mtu) 16211 ill->ill_max_mtu = ill->ill_user_mtu; 16212 16213 /* 16214 * If the MTU is unchanged, we're done. 16215 */ 16216 if (orig_mtu == ill->ill_max_mtu) { 16217 mutex_exit(&ill->ill_lock); 16218 break; 16219 } 16220 16221 if (ill->ill_isv6) { 16222 if (ill->ill_max_mtu < IPV6_MIN_MTU) 16223 ill->ill_max_mtu = IPV6_MIN_MTU; 16224 } else { 16225 if (ill->ill_max_mtu < IP_MIN_MTU) 16226 ill->ill_max_mtu = IP_MIN_MTU; 16227 } 16228 for (ipif = ill->ill_ipif; ipif != NULL; 16229 ipif = ipif->ipif_next) { 16230 /* 16231 * Don't override the mtu if the user 16232 * has explicitly set it. 16233 */ 16234 if (ipif->ipif_flags & IPIF_FIXEDMTU) 16235 continue; 16236 ipif->ipif_mtu = (uint_t)notify->dl_data; 16237 if (ipif->ipif_isv6) 16238 ire = ipif_to_ire_v6(ipif); 16239 else 16240 ire = ipif_to_ire(ipif); 16241 if (ire != NULL) { 16242 ire->ire_max_frag = ipif->ipif_mtu; 16243 ire_refrele(ire); 16244 } 16245 if (ipif->ipif_flags & IPIF_UP) { 16246 if (ill->ill_isv6) 16247 need_ire_walk_v6 = B_TRUE; 16248 else 16249 need_ire_walk_v4 = B_TRUE; 16250 } 16251 } 16252 mutex_exit(&ill->ill_lock); 16253 if (need_ire_walk_v4) 16254 ire_walk_v4(ill_mtu_change, (char *)ill, 16255 ALL_ZONES, ipst); 16256 if (need_ire_walk_v6) 16257 ire_walk_v6(ill_mtu_change, (char *)ill, 16258 ALL_ZONES, ipst); 16259 16260 /* 16261 * Refresh IPMP meta-interface MTU if necessary. 16262 */ 16263 if (IS_UNDER_IPMP(ill)) 16264 ipmp_illgrp_refresh_mtu(ill->ill_grp); 16265 break; 16266 16267 case DL_NOTE_LINK_UP: 16268 case DL_NOTE_LINK_DOWN: { 16269 /* 16270 * We are writer. ill / phyint / ipsq assocs stable. 16271 * The RUNNING flag reflects the state of the link. 16272 */ 16273 phyint_t *phyint = ill->ill_phyint; 16274 uint64_t new_phyint_flags; 16275 boolean_t changed = B_FALSE; 16276 boolean_t went_up; 16277 16278 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 16279 mutex_enter(&phyint->phyint_lock); 16280 16281 new_phyint_flags = went_up ? 16282 phyint->phyint_flags | PHYI_RUNNING : 16283 phyint->phyint_flags & ~PHYI_RUNNING; 16284 16285 if (IS_IPMP(ill)) { 16286 new_phyint_flags = went_up ? 16287 new_phyint_flags & ~PHYI_FAILED : 16288 new_phyint_flags | PHYI_FAILED; 16289 } 16290 16291 if (new_phyint_flags != phyint->phyint_flags) { 16292 phyint->phyint_flags = new_phyint_flags; 16293 changed = B_TRUE; 16294 } 16295 mutex_exit(&phyint->phyint_lock); 16296 /* 16297 * ill_restart_dad handles the DAD restart and routing 16298 * socket notification logic. 16299 */ 16300 if (changed) { 16301 ill_restart_dad(phyint->phyint_illv4, went_up); 16302 ill_restart_dad(phyint->phyint_illv6, went_up); 16303 } 16304 break; 16305 } 16306 case DL_NOTE_PROMISC_ON_PHYS: { 16307 phyint_t *phyint = ill->ill_phyint; 16308 16309 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 16310 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 16311 mutex_enter(&phyint->phyint_lock); 16312 phyint->phyint_flags |= PHYI_PROMISC; 16313 mutex_exit(&phyint->phyint_lock); 16314 break; 16315 } 16316 case DL_NOTE_PROMISC_OFF_PHYS: { 16317 phyint_t *phyint = ill->ill_phyint; 16318 16319 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 16320 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 16321 mutex_enter(&phyint->phyint_lock); 16322 phyint->phyint_flags &= ~PHYI_PROMISC; 16323 mutex_exit(&phyint->phyint_lock); 16324 break; 16325 } 16326 case DL_NOTE_CAPAB_RENEG: 16327 /* 16328 * Something changed on the driver side. 16329 * It wants us to renegotiate the capabilities 16330 * on this ill. One possible cause is the aggregation 16331 * interface under us where a port got added or 16332 * went away. 16333 * 16334 * If the capability negotiation is already done 16335 * or is in progress, reset the capabilities and 16336 * mark the ill's ill_capab_reneg to be B_TRUE, 16337 * so that when the ack comes back, we can start 16338 * the renegotiation process. 16339 * 16340 * Note that if ill_capab_reneg is already B_TRUE 16341 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case), 16342 * the capability resetting request has been sent 16343 * and the renegotiation has not been started yet; 16344 * nothing needs to be done in this case. 16345 */ 16346 ipsq_current_start(ipsq, ill->ill_ipif, 0); 16347 ill_capability_reset(ill, B_TRUE); 16348 ipsq_current_finish(ipsq); 16349 break; 16350 default: 16351 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 16352 "type 0x%x for DL_NOTIFY_IND\n", 16353 notify->dl_notification)); 16354 break; 16355 } 16356 16357 /* 16358 * As this is an asynchronous operation, we 16359 * should not call ill_dlpi_done 16360 */ 16361 break; 16362 } 16363 case DL_NOTIFY_ACK: { 16364 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 16365 16366 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 16367 ill->ill_note_link = 1; 16368 ill_dlpi_done(ill, DL_NOTIFY_REQ); 16369 break; 16370 } 16371 case DL_PHYS_ADDR_ACK: { 16372 /* 16373 * As part of plumbing the interface via SIOCSLIFNAME, 16374 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, 16375 * whose answers we receive here. As each answer is received, 16376 * we call ill_dlpi_done() to dispatch the next request as 16377 * we're processing the current one. Once all answers have 16378 * been received, we use ipsq_pending_mp_get() to dequeue the 16379 * outstanding IOCTL and reply to it. (Because ill_dl_phys() 16380 * is invoked from an ill queue, conn_oper_pending_ill is not 16381 * available, but we know the ioctl is pending on ill_wq.) 16382 */ 16383 uint_t paddrlen, paddroff; 16384 16385 paddrreq = ill->ill_phys_addr_pend; 16386 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; 16387 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; 16388 16389 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 16390 if (paddrreq == DL_IPV6_TOKEN) { 16391 /* 16392 * bcopy to low-order bits of ill_token 16393 * 16394 * XXX Temporary hack - currently, all known tokens 16395 * are 64 bits, so I'll cheat for the moment. 16396 */ 16397 bcopy(mp->b_rptr + paddroff, 16398 &ill->ill_token.s6_addr32[2], paddrlen); 16399 ill->ill_token_length = paddrlen; 16400 break; 16401 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 16402 ASSERT(ill->ill_nd_lla_mp == NULL); 16403 ill_set_ndmp(ill, mp, paddroff, paddrlen); 16404 mp = NULL; 16405 break; 16406 } 16407 16408 ASSERT(paddrreq == DL_CURR_PHYS_ADDR); 16409 ASSERT(ill->ill_phys_addr_mp == NULL); 16410 if (!ill->ill_ifname_pending) 16411 break; 16412 ill->ill_ifname_pending = 0; 16413 if (!ioctl_aborted) 16414 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16415 if (mp1 != NULL) { 16416 ASSERT(connp == NULL); 16417 q = ill->ill_wq; 16418 } 16419 /* 16420 * If any error acks received during the plumbing sequence, 16421 * ill_ifname_pending_err will be set. Break out and send up 16422 * the error to the pending ioctl. 16423 */ 16424 if (ill->ill_ifname_pending_err != 0) { 16425 err = ill->ill_ifname_pending_err; 16426 ill->ill_ifname_pending_err = 0; 16427 break; 16428 } 16429 16430 ill->ill_phys_addr_mp = mp; 16431 ill->ill_phys_addr = mp->b_rptr + paddroff; 16432 mp = NULL; 16433 16434 /* 16435 * If paddrlen is zero, the DLPI provider doesn't support 16436 * physical addresses. The other two tests were historical 16437 * workarounds for bugs in our former PPP implementation, but 16438 * now other things have grown dependencies on them -- e.g., 16439 * the tun module specifies a dl_addr_length of zero in its 16440 * DL_BIND_ACK, but then specifies an incorrect value in its 16441 * DL_PHYS_ADDR_ACK. These bogus checks need to be removed, 16442 * but only after careful testing ensures that all dependent 16443 * broken DLPI providers have been fixed. 16444 */ 16445 if (paddrlen == 0 || ill->ill_phys_addr_length == 0 || 16446 ill->ill_phys_addr_length == IP_ADDR_LEN) { 16447 ill->ill_phys_addr = NULL; 16448 } else if (paddrlen != ill->ill_phys_addr_length) { 16449 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", 16450 paddrlen, ill->ill_phys_addr_length)); 16451 err = EINVAL; 16452 break; 16453 } 16454 16455 if (ill->ill_nd_lla_mp == NULL) { 16456 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { 16457 err = ENOMEM; 16458 break; 16459 } 16460 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); 16461 } 16462 16463 /* 16464 * Set the interface token. If the zeroth interface address 16465 * is unspecified, then set it to the link local address. 16466 */ 16467 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 16468 (void) ill_setdefaulttoken(ill); 16469 16470 ASSERT(ill->ill_ipif->ipif_id == 0); 16471 if (ipif != NULL && 16472 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { 16473 (void) ipif_setlinklocal(ipif); 16474 } 16475 break; 16476 } 16477 case DL_OK_ACK: 16478 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 16479 dl_primstr((int)dloa->dl_correct_primitive), 16480 dloa->dl_correct_primitive)); 16481 switch (dloa->dl_correct_primitive) { 16482 case DL_ENABMULTI_REQ: 16483 case DL_DISABMULTI_REQ: 16484 ill_dlpi_done(ill, dloa->dl_correct_primitive); 16485 break; 16486 case DL_PROMISCON_REQ: 16487 case DL_PROMISCOFF_REQ: 16488 case DL_UNBIND_REQ: 16489 case DL_ATTACH_REQ: 16490 ill_dlpi_done(ill, dloa->dl_correct_primitive); 16491 break; 16492 } 16493 break; 16494 default: 16495 break; 16496 } 16497 16498 freemsg(mp); 16499 if (mp1 == NULL) 16500 return; 16501 16502 /* 16503 * The operation must complete without EINPROGRESS since 16504 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise, 16505 * the operation will be stuck forever inside the IPSQ. 16506 */ 16507 ASSERT(err != EINPROGRESS); 16508 16509 switch (ipsq->ipsq_xop->ipx_current_ioctl) { 16510 case 0: 16511 ipsq_current_finish(ipsq); 16512 break; 16513 16514 case SIOCSLIFNAME: 16515 case IF_UNITSEL: { 16516 ill_t *ill_other = ILL_OTHER(ill); 16517 16518 /* 16519 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the 16520 * ill has a peer which is in an IPMP group, then place ill 16521 * into the same group. One catch: although ifconfig plumbs 16522 * the appropriate IPMP meta-interface prior to plumbing this 16523 * ill, it is possible for multiple ifconfig applications to 16524 * race (or for another application to adjust plumbing), in 16525 * which case the IPMP meta-interface we need will be missing. 16526 * If so, kick the phyint out of the group. 16527 */ 16528 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) { 16529 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp; 16530 ipmp_illgrp_t *illg; 16531 16532 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4; 16533 if (illg == NULL) 16534 ipmp_phyint_leave_grp(ill->ill_phyint); 16535 else 16536 ipmp_ill_join_illgrp(ill, illg); 16537 } 16538 16539 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL) 16540 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 16541 else 16542 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 16543 break; 16544 } 16545 case SIOCLIFADDIF: 16546 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 16547 break; 16548 16549 default: 16550 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 16551 break; 16552 } 16553 } 16554 16555 /* 16556 * ip_rput_other is called by ip_rput to handle messages modifying the global 16557 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 16558 */ 16559 /* ARGSUSED */ 16560 void 16561 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 16562 { 16563 ill_t *ill = q->q_ptr; 16564 struct iocblk *iocp; 16565 mblk_t *mp1; 16566 conn_t *connp = NULL; 16567 16568 ip1dbg(("ip_rput_other ")); 16569 if (ipsq != NULL) { 16570 ASSERT(IAM_WRITER_IPSQ(ipsq)); 16571 ASSERT(ipsq->ipsq_xop == 16572 ill->ill_phyint->phyint_ipsq->ipsq_xop); 16573 } 16574 16575 switch (mp->b_datap->db_type) { 16576 case M_ERROR: 16577 case M_HANGUP: 16578 /* 16579 * The device has a problem. We force the ILL down. It can 16580 * be brought up again manually using SIOCSIFFLAGS (via 16581 * ifconfig or equivalent). 16582 */ 16583 ASSERT(ipsq != NULL); 16584 if (mp->b_rptr < mp->b_wptr) 16585 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 16586 if (ill->ill_error == 0) 16587 ill->ill_error = ENXIO; 16588 if (!ill_down_start(q, mp)) 16589 return; 16590 ipif_all_down_tail(ipsq, q, mp, NULL); 16591 break; 16592 case M_IOCACK: 16593 iocp = (struct iocblk *)mp->b_rptr; 16594 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 16595 switch (iocp->ioc_cmd) { 16596 case SIOCSTUNPARAM: 16597 case OSIOCSTUNPARAM: 16598 ASSERT(ipsq != NULL); 16599 /* 16600 * Finish socket ioctl passed through to tun. 16601 * We should have an IOCTL waiting on this. 16602 */ 16603 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16604 if (ill->ill_isv6) { 16605 struct iftun_req *ta; 16606 16607 /* 16608 * if a source or destination is 16609 * being set, try and set the link 16610 * local address for the tunnel 16611 */ 16612 ta = (struct iftun_req *)mp->b_cont-> 16613 b_cont->b_rptr; 16614 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 16615 ipif_set_tun_llink(ill, ta); 16616 } 16617 16618 } 16619 if (mp1 != NULL) { 16620 /* 16621 * Now copy back the b_next/b_prev used by 16622 * mi code for the mi_copy* functions. 16623 * See ip_sioctl_tunparam() for the reason. 16624 * Also protect against missing b_cont. 16625 */ 16626 if (mp->b_cont != NULL) { 16627 mp->b_cont->b_next = 16628 mp1->b_cont->b_next; 16629 mp->b_cont->b_prev = 16630 mp1->b_cont->b_prev; 16631 } 16632 inet_freemsg(mp1); 16633 ASSERT(connp != NULL); 16634 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16635 iocp->ioc_error, NO_COPYOUT, ipsq); 16636 } else { 16637 ASSERT(connp == NULL); 16638 putnext(q, mp); 16639 } 16640 break; 16641 case SIOCGTUNPARAM: 16642 case OSIOCGTUNPARAM: 16643 /* 16644 * This is really M_IOCDATA from the tunnel driver. 16645 * convert back and complete the ioctl. 16646 * We should have an IOCTL waiting on this. 16647 */ 16648 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 16649 if (mp1) { 16650 /* 16651 * Now copy back the b_next/b_prev used by 16652 * mi code for the mi_copy* functions. 16653 * See ip_sioctl_tunparam() for the reason. 16654 * Also protect against missing b_cont. 16655 */ 16656 if (mp->b_cont != NULL) { 16657 mp->b_cont->b_next = 16658 mp1->b_cont->b_next; 16659 mp->b_cont->b_prev = 16660 mp1->b_cont->b_prev; 16661 } 16662 inet_freemsg(mp1); 16663 if (iocp->ioc_error == 0) 16664 mp->b_datap->db_type = M_IOCDATA; 16665 ASSERT(connp != NULL); 16666 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16667 iocp->ioc_error, COPYOUT, NULL); 16668 } else { 16669 ASSERT(connp == NULL); 16670 putnext(q, mp); 16671 } 16672 break; 16673 default: 16674 break; 16675 } 16676 break; 16677 case M_IOCNAK: 16678 iocp = (struct iocblk *)mp->b_rptr; 16679 16680 switch (iocp->ioc_cmd) { 16681 int mode; 16682 16683 case DL_IOC_HDR_INFO: 16684 /* 16685 * If this was the first attempt, turn off the 16686 * fastpath probing. 16687 */ 16688 mutex_enter(&ill->ill_lock); 16689 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 16690 ill->ill_dlpi_fastpath_state = IDS_FAILED; 16691 mutex_exit(&ill->ill_lock); 16692 ill_fastpath_nack(ill); 16693 ip1dbg(("ip_rput: DLPI fastpath off on " 16694 "interface %s\n", 16695 ill->ill_name)); 16696 } else { 16697 mutex_exit(&ill->ill_lock); 16698 } 16699 freemsg(mp); 16700 break; 16701 case SIOCSTUNPARAM: 16702 case OSIOCSTUNPARAM: 16703 ASSERT(ipsq != NULL); 16704 /* 16705 * Finish socket ioctl passed through to tun 16706 * We should have an IOCTL waiting on this. 16707 */ 16708 /* FALLTHRU */ 16709 case SIOCGTUNPARAM: 16710 case OSIOCGTUNPARAM: 16711 /* 16712 * This is really M_IOCDATA from the tunnel driver. 16713 * convert back and complete the ioctl. 16714 * We should have an IOCTL waiting on this. 16715 */ 16716 if (iocp->ioc_cmd == SIOCGTUNPARAM || 16717 iocp->ioc_cmd == OSIOCGTUNPARAM) { 16718 mp1 = ill_pending_mp_get(ill, &connp, 16719 iocp->ioc_id); 16720 mode = COPYOUT; 16721 ipsq = NULL; 16722 } else { 16723 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16724 mode = NO_COPYOUT; 16725 } 16726 if (mp1 != NULL) { 16727 /* 16728 * Now copy back the b_next/b_prev used by 16729 * mi code for the mi_copy* functions. 16730 * See ip_sioctl_tunparam() for the reason. 16731 * Also protect against missing b_cont. 16732 */ 16733 if (mp->b_cont != NULL) { 16734 mp->b_cont->b_next = 16735 mp1->b_cont->b_next; 16736 mp->b_cont->b_prev = 16737 mp1->b_cont->b_prev; 16738 } 16739 inet_freemsg(mp1); 16740 if (iocp->ioc_error == 0) 16741 iocp->ioc_error = EINVAL; 16742 ASSERT(connp != NULL); 16743 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16744 iocp->ioc_error, mode, ipsq); 16745 } else { 16746 ASSERT(connp == NULL); 16747 putnext(q, mp); 16748 } 16749 break; 16750 default: 16751 break; 16752 } 16753 default: 16754 break; 16755 } 16756 } 16757 16758 /* 16759 * NOTE : This function does not ire_refrele the ire argument passed in. 16760 * 16761 * IPQoS notes 16762 * IP policy is invoked twice for a forwarded packet, once on the read side 16763 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 16764 * enabled. An additional parameter, in_ill, has been added for this purpose. 16765 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 16766 * because ip_mroute drops this information. 16767 * 16768 */ 16769 void 16770 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 16771 { 16772 uint32_t old_pkt_len; 16773 uint32_t pkt_len; 16774 queue_t *q; 16775 uint32_t sum; 16776 #define rptr ((uchar_t *)ipha) 16777 uint32_t max_frag; 16778 uint32_t ill_index; 16779 ill_t *out_ill; 16780 mib2_ipIfStatsEntry_t *mibptr; 16781 ip_stack_t *ipst = ((ill_t *)(ire->ire_stq->q_ptr))->ill_ipst; 16782 16783 /* Get the ill_index of the incoming ILL */ 16784 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 16785 mibptr = (in_ill != NULL) ? in_ill->ill_ip_mib : &ipst->ips_ip_mib; 16786 16787 /* Initiate Read side IPPF processing */ 16788 if (IPP_ENABLED(IPP_FWD_IN, ipst)) { 16789 ip_process(IPP_FWD_IN, &mp, ill_index); 16790 if (mp == NULL) { 16791 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 16792 "during IPPF processing\n")); 16793 return; 16794 } 16795 } 16796 16797 /* Adjust the checksum to reflect the ttl decrement. */ 16798 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 16799 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 16800 16801 if (ipha->ipha_ttl-- <= 1) { 16802 if (ip_csum_hdr(ipha)) { 16803 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16804 goto drop_pkt; 16805 } 16806 /* 16807 * Note: ire_stq this will be NULL for multicast 16808 * datagrams using the long path through arp (the IRE 16809 * is not an IRE_CACHE). This should not cause 16810 * problems since we don't generate ICMP errors for 16811 * multicast packets. 16812 */ 16813 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16814 q = ire->ire_stq; 16815 if (q != NULL) { 16816 /* Sent by forwarding path, and router is global zone */ 16817 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, 16818 GLOBAL_ZONEID, ipst); 16819 } else 16820 freemsg(mp); 16821 return; 16822 } 16823 16824 /* 16825 * Don't forward if the interface is down 16826 */ 16827 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 16828 BUMP_MIB(mibptr, ipIfStatsInDiscards); 16829 ip2dbg(("ip_rput_forward:interface is down\n")); 16830 goto drop_pkt; 16831 } 16832 16833 /* Get the ill_index of the outgoing ILL */ 16834 out_ill = ire_to_ill(ire); 16835 ill_index = out_ill->ill_phyint->phyint_ifindex; 16836 16837 DTRACE_PROBE4(ip4__forwarding__start, 16838 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16839 16840 FW_HOOKS(ipst->ips_ip4_forwarding_event, 16841 ipst->ips_ipv4firewall_forwarding, 16842 in_ill, out_ill, ipha, mp, mp, 0, ipst); 16843 16844 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 16845 16846 if (mp == NULL) 16847 return; 16848 old_pkt_len = pkt_len = ntohs(ipha->ipha_length); 16849 16850 if (is_system_labeled()) { 16851 mblk_t *mp1; 16852 16853 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 16854 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16855 goto drop_pkt; 16856 } 16857 /* Size may have changed */ 16858 mp = mp1; 16859 ipha = (ipha_t *)mp->b_rptr; 16860 pkt_len = ntohs(ipha->ipha_length); 16861 } 16862 16863 /* Check if there are options to update */ 16864 if (!IS_SIMPLE_IPH(ipha)) { 16865 if (ip_csum_hdr(ipha)) { 16866 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16867 goto drop_pkt; 16868 } 16869 if (ip_rput_forward_options(mp, ipha, ire, ipst)) { 16870 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16871 return; 16872 } 16873 16874 ipha->ipha_hdr_checksum = 0; 16875 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 16876 } 16877 max_frag = ire->ire_max_frag; 16878 if (pkt_len > max_frag) { 16879 /* 16880 * It needs fragging on its way out. We haven't 16881 * verified the header checksum yet. Since we 16882 * are going to put a surely good checksum in the 16883 * outgoing header, we have to make sure that it 16884 * was good coming in. 16885 */ 16886 if (ip_csum_hdr(ipha)) { 16887 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16888 goto drop_pkt; 16889 } 16890 /* Initiate Write side IPPF processing */ 16891 if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { 16892 ip_process(IPP_FWD_OUT, &mp, ill_index); 16893 if (mp == NULL) { 16894 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 16895 " during IPPF processing\n")); 16896 return; 16897 } 16898 } 16899 /* 16900 * Handle labeled packet resizing. 16901 * 16902 * If we have added a label, inform ip_wput_frag() of its 16903 * effect on the MTU for ICMP messages. 16904 */ 16905 if (pkt_len > old_pkt_len) { 16906 uint32_t secopt_size; 16907 16908 secopt_size = pkt_len - old_pkt_len; 16909 if (secopt_size < max_frag) 16910 max_frag -= secopt_size; 16911 } 16912 16913 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, 16914 GLOBAL_ZONEID, ipst, NULL); 16915 ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); 16916 return; 16917 } 16918 16919 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 16920 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16921 FW_HOOKS(ipst->ips_ip4_physical_out_event, 16922 ipst->ips_ipv4firewall_physical_out, 16923 NULL, out_ill, ipha, mp, mp, 0, ipst); 16924 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 16925 if (mp == NULL) 16926 return; 16927 16928 mp->b_prev = (mblk_t *)IPP_FWD_OUT; 16929 ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); 16930 (void) ip_xmit_v4(mp, ire, NULL, B_FALSE, NULL); 16931 /* ip_xmit_v4 always consumes the packet */ 16932 return; 16933 16934 drop_pkt:; 16935 ip1dbg(("ip_rput_forward: drop pkt\n")); 16936 freemsg(mp); 16937 #undef rptr 16938 } 16939 16940 void 16941 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 16942 { 16943 ire_t *ire; 16944 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 16945 16946 ASSERT(!ipif->ipif_isv6); 16947 /* 16948 * Find an IRE which matches the destination and the outgoing 16949 * queue in the cache table. All we need is an IRE_CACHE which 16950 * is pointing at ipif->ipif_ill. 16951 */ 16952 if (ipif->ipif_flags & IPIF_POINTOPOINT) 16953 dst = ipif->ipif_pp_dst_addr; 16954 16955 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, msg_getlabel(mp), 16956 MATCH_IRE_ILL | MATCH_IRE_SECATTR, ipst); 16957 if (ire == NULL) { 16958 /* 16959 * Mark this packet to make it be delivered to 16960 * ip_rput_forward after the new ire has been 16961 * created. 16962 */ 16963 mp->b_prev = NULL; 16964 mp->b_next = mp; 16965 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 16966 NULL, 0, GLOBAL_ZONEID, &zero_info); 16967 } else { 16968 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 16969 IRE_REFRELE(ire); 16970 } 16971 } 16972 16973 /* Update any source route, record route or timestamp options */ 16974 static int 16975 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) 16976 { 16977 ipoptp_t opts; 16978 uchar_t *opt; 16979 uint8_t optval; 16980 uint8_t optlen; 16981 ipaddr_t dst; 16982 uint32_t ts; 16983 ire_t *dst_ire = NULL; 16984 ire_t *tmp_ire = NULL; 16985 timestruc_t now; 16986 16987 ip2dbg(("ip_rput_forward_options\n")); 16988 dst = ipha->ipha_dst; 16989 for (optval = ipoptp_first(&opts, ipha); 16990 optval != IPOPT_EOL; 16991 optval = ipoptp_next(&opts)) { 16992 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 16993 opt = opts.ipoptp_cur; 16994 optlen = opts.ipoptp_len; 16995 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 16996 optval, opts.ipoptp_len)); 16997 switch (optval) { 16998 uint32_t off; 16999 case IPOPT_SSRR: 17000 case IPOPT_LSRR: 17001 /* Check if adminstratively disabled */ 17002 if (!ipst->ips_ip_forward_src_routed) { 17003 if (ire->ire_stq != NULL) { 17004 /* 17005 * Sent by forwarding path, and router 17006 * is global zone 17007 */ 17008 icmp_unreachable(ire->ire_stq, mp, 17009 ICMP_SOURCE_ROUTE_FAILED, 17010 GLOBAL_ZONEID, ipst); 17011 } else { 17012 ip0dbg(("ip_rput_forward_options: " 17013 "unable to send unreach\n")); 17014 freemsg(mp); 17015 } 17016 return (-1); 17017 } 17018 17019 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17020 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17021 if (dst_ire == NULL) { 17022 /* 17023 * Must be partial since ip_rput_options 17024 * checked for strict. 17025 */ 17026 break; 17027 } 17028 off = opt[IPOPT_OFFSET]; 17029 off--; 17030 redo_srr: 17031 if (optlen < IP_ADDR_LEN || 17032 off > optlen - IP_ADDR_LEN) { 17033 /* End of source route */ 17034 ip1dbg(( 17035 "ip_rput_forward_options: end of SR\n")); 17036 ire_refrele(dst_ire); 17037 break; 17038 } 17039 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17040 bcopy(&ire->ire_src_addr, (char *)opt + off, 17041 IP_ADDR_LEN); 17042 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 17043 ntohl(dst))); 17044 17045 /* 17046 * Check if our address is present more than 17047 * once as consecutive hops in source route. 17048 */ 17049 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17050 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 17051 if (tmp_ire != NULL) { 17052 ire_refrele(tmp_ire); 17053 off += IP_ADDR_LEN; 17054 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17055 goto redo_srr; 17056 } 17057 ipha->ipha_dst = dst; 17058 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17059 ire_refrele(dst_ire); 17060 break; 17061 case IPOPT_RR: 17062 off = opt[IPOPT_OFFSET]; 17063 off--; 17064 if (optlen < IP_ADDR_LEN || 17065 off > optlen - IP_ADDR_LEN) { 17066 /* No more room - ignore */ 17067 ip1dbg(( 17068 "ip_rput_forward_options: end of RR\n")); 17069 break; 17070 } 17071 bcopy(&ire->ire_src_addr, (char *)opt + off, 17072 IP_ADDR_LEN); 17073 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17074 break; 17075 case IPOPT_TS: 17076 /* Insert timestamp if there is room */ 17077 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17078 case IPOPT_TS_TSONLY: 17079 off = IPOPT_TS_TIMELEN; 17080 break; 17081 case IPOPT_TS_PRESPEC: 17082 case IPOPT_TS_PRESPEC_RFC791: 17083 /* Verify that the address matched */ 17084 off = opt[IPOPT_OFFSET] - 1; 17085 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17086 dst_ire = ire_ctable_lookup(dst, 0, 17087 IRE_LOCAL, NULL, ALL_ZONES, NULL, 17088 MATCH_IRE_TYPE, ipst); 17089 if (dst_ire == NULL) { 17090 /* Not for us */ 17091 break; 17092 } 17093 ire_refrele(dst_ire); 17094 /* FALLTHRU */ 17095 case IPOPT_TS_TSANDADDR: 17096 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17097 break; 17098 default: 17099 /* 17100 * ip_*put_options should have already 17101 * dropped this packet. 17102 */ 17103 cmn_err(CE_PANIC, "ip_rput_forward_options: " 17104 "unknown IT - bug in ip_rput_options?\n"); 17105 return (0); /* Keep "lint" happy */ 17106 } 17107 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 17108 /* Increase overflow counter */ 17109 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 17110 opt[IPOPT_POS_OV_FLG] = 17111 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 17112 (off << 4)); 17113 break; 17114 } 17115 off = opt[IPOPT_OFFSET] - 1; 17116 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17117 case IPOPT_TS_PRESPEC: 17118 case IPOPT_TS_PRESPEC_RFC791: 17119 case IPOPT_TS_TSANDADDR: 17120 bcopy(&ire->ire_src_addr, 17121 (char *)opt + off, IP_ADDR_LEN); 17122 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17123 /* FALLTHRU */ 17124 case IPOPT_TS_TSONLY: 17125 off = opt[IPOPT_OFFSET] - 1; 17126 /* Compute # of milliseconds since midnight */ 17127 gethrestime(&now); 17128 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 17129 now.tv_nsec / (NANOSEC / MILLISEC); 17130 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 17131 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 17132 break; 17133 } 17134 break; 17135 } 17136 } 17137 return (0); 17138 } 17139 17140 /* 17141 * This is called after processing at least one of AH/ESP headers. 17142 * 17143 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 17144 * the actual, physical interface on which the packet was received, 17145 * but, when ip_strict_dst_multihoming is set to 1, could be the 17146 * interface which had the ipha_dst configured when the packet went 17147 * through ip_rput. The ill_index corresponding to the recv_ill 17148 * is saved in ipsec_in_rill_index 17149 * 17150 * NOTE2: The "ire" argument is only used in IPv4 cases. This function 17151 * cannot assume "ire" points to valid data for any IPv6 cases. 17152 */ 17153 void 17154 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 17155 { 17156 mblk_t *mp; 17157 ipaddr_t dst; 17158 in6_addr_t *v6dstp; 17159 ipha_t *ipha; 17160 ip6_t *ip6h; 17161 ipsec_in_t *ii; 17162 boolean_t ill_need_rele = B_FALSE; 17163 boolean_t rill_need_rele = B_FALSE; 17164 boolean_t ire_need_rele = B_FALSE; 17165 netstack_t *ns; 17166 ip_stack_t *ipst; 17167 17168 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 17169 ASSERT(ii->ipsec_in_ill_index != 0); 17170 ns = ii->ipsec_in_ns; 17171 ASSERT(ii->ipsec_in_ns != NULL); 17172 ipst = ns->netstack_ip; 17173 17174 mp = ipsec_mp->b_cont; 17175 ASSERT(mp != NULL); 17176 17177 if (ill == NULL) { 17178 ASSERT(recv_ill == NULL); 17179 /* 17180 * We need to get the original queue on which ip_rput_local 17181 * or ip_rput_data_v6 was called. 17182 */ 17183 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 17184 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL, ipst); 17185 ill_need_rele = B_TRUE; 17186 17187 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 17188 recv_ill = ill_lookup_on_ifindex( 17189 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 17190 NULL, NULL, NULL, NULL, ipst); 17191 rill_need_rele = B_TRUE; 17192 } else { 17193 recv_ill = ill; 17194 } 17195 17196 if ((ill == NULL) || (recv_ill == NULL)) { 17197 ip0dbg(("ip_fanout_proto_again: interface " 17198 "disappeared\n")); 17199 if (ill != NULL) 17200 ill_refrele(ill); 17201 if (recv_ill != NULL) 17202 ill_refrele(recv_ill); 17203 freemsg(ipsec_mp); 17204 return; 17205 } 17206 } 17207 17208 ASSERT(ill != NULL && recv_ill != NULL); 17209 17210 if (mp->b_datap->db_type == M_CTL) { 17211 /* 17212 * AH/ESP is returning the ICMP message after 17213 * removing their headers. Fanout again till 17214 * it gets to the right protocol. 17215 */ 17216 if (ii->ipsec_in_v4) { 17217 icmph_t *icmph; 17218 int iph_hdr_length; 17219 int hdr_length; 17220 17221 ipha = (ipha_t *)mp->b_rptr; 17222 iph_hdr_length = IPH_HDR_LENGTH(ipha); 17223 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 17224 ipha = (ipha_t *)&icmph[1]; 17225 hdr_length = IPH_HDR_LENGTH(ipha); 17226 /* 17227 * icmp_inbound_error_fanout may need to do pullupmsg. 17228 * Reset the type to M_DATA. 17229 */ 17230 mp->b_datap->db_type = M_DATA; 17231 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 17232 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 17233 B_FALSE, ill, ii->ipsec_in_zoneid); 17234 } else { 17235 icmp6_t *icmp6; 17236 int hdr_length; 17237 17238 ip6h = (ip6_t *)mp->b_rptr; 17239 /* Don't call hdr_length_v6() unless you have to. */ 17240 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 17241 hdr_length = ip_hdr_length_v6(mp, ip6h); 17242 else 17243 hdr_length = IPV6_HDR_LEN; 17244 17245 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 17246 /* 17247 * icmp_inbound_error_fanout_v6 may need to do 17248 * pullupmsg. Reset the type to M_DATA. 17249 */ 17250 mp->b_datap->db_type = M_DATA; 17251 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 17252 ip6h, icmp6, ill, recv_ill, B_TRUE, 17253 ii->ipsec_in_zoneid); 17254 } 17255 if (ill_need_rele) 17256 ill_refrele(ill); 17257 if (rill_need_rele) 17258 ill_refrele(recv_ill); 17259 return; 17260 } 17261 17262 if (ii->ipsec_in_v4) { 17263 ipha = (ipha_t *)mp->b_rptr; 17264 dst = ipha->ipha_dst; 17265 if (CLASSD(dst)) { 17266 /* 17267 * Multicast has to be delivered to all streams. 17268 */ 17269 dst = INADDR_BROADCAST; 17270 } 17271 17272 if (ire == NULL) { 17273 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, 17274 msg_getlabel(mp), ipst); 17275 if (ire == NULL) { 17276 if (ill_need_rele) 17277 ill_refrele(ill); 17278 if (rill_need_rele) 17279 ill_refrele(recv_ill); 17280 ip1dbg(("ip_fanout_proto_again: " 17281 "IRE not found")); 17282 freemsg(ipsec_mp); 17283 return; 17284 } 17285 ire_need_rele = B_TRUE; 17286 } 17287 17288 switch (ipha->ipha_protocol) { 17289 case IPPROTO_UDP: 17290 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 17291 recv_ill); 17292 if (ire_need_rele) 17293 ire_refrele(ire); 17294 break; 17295 case IPPROTO_TCP: 17296 if (!ire_need_rele) 17297 IRE_REFHOLD(ire); 17298 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 17299 ire, ipsec_mp, 0, ill->ill_rq, NULL); 17300 IRE_REFRELE(ire); 17301 if (mp != NULL) { 17302 SQUEUE_ENTER(GET_SQUEUE(mp), mp, 17303 mp, 1, SQ_PROCESS, 17304 SQTAG_IP_PROTO_AGAIN); 17305 } 17306 break; 17307 case IPPROTO_SCTP: 17308 if (!ire_need_rele) 17309 IRE_REFHOLD(ire); 17310 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 17311 ipsec_mp, 0, ill->ill_rq, dst); 17312 break; 17313 default: 17314 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 17315 recv_ill, 0); 17316 if (ire_need_rele) 17317 ire_refrele(ire); 17318 break; 17319 } 17320 } else { 17321 uint32_t rput_flags = 0; 17322 17323 ip6h = (ip6_t *)mp->b_rptr; 17324 v6dstp = &ip6h->ip6_dst; 17325 /* 17326 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 17327 * address. 17328 * 17329 * Currently, we don't store that state in the IPSEC_IN 17330 * message, and we may need to. 17331 */ 17332 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 17333 IP6_IN_LLMCAST : 0); 17334 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 17335 NULL, NULL); 17336 } 17337 if (ill_need_rele) 17338 ill_refrele(ill); 17339 if (rill_need_rele) 17340 ill_refrele(recv_ill); 17341 } 17342 17343 /* 17344 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 17345 * returns 'true' if there are still fragments left on the queue, in 17346 * which case we restart the timer. 17347 */ 17348 void 17349 ill_frag_timer(void *arg) 17350 { 17351 ill_t *ill = (ill_t *)arg; 17352 boolean_t frag_pending; 17353 ip_stack_t *ipst = ill->ill_ipst; 17354 time_t timeout; 17355 17356 mutex_enter(&ill->ill_lock); 17357 ASSERT(!ill->ill_fragtimer_executing); 17358 if (ill->ill_state_flags & ILL_CONDEMNED) { 17359 ill->ill_frag_timer_id = 0; 17360 mutex_exit(&ill->ill_lock); 17361 return; 17362 } 17363 ill->ill_fragtimer_executing = 1; 17364 mutex_exit(&ill->ill_lock); 17365 17366 if (ill->ill_isv6) 17367 timeout = ipst->ips_ipv6_frag_timeout; 17368 else 17369 timeout = ipst->ips_ip_g_frag_timeout; 17370 17371 frag_pending = ill_frag_timeout(ill, timeout); 17372 17373 /* 17374 * Restart the timer, if we have fragments pending or if someone 17375 * wanted us to be scheduled again. 17376 */ 17377 mutex_enter(&ill->ill_lock); 17378 ill->ill_fragtimer_executing = 0; 17379 ill->ill_frag_timer_id = 0; 17380 if (frag_pending || ill->ill_fragtimer_needrestart) 17381 ill_frag_timer_start(ill); 17382 mutex_exit(&ill->ill_lock); 17383 } 17384 17385 void 17386 ill_frag_timer_start(ill_t *ill) 17387 { 17388 ip_stack_t *ipst = ill->ill_ipst; 17389 clock_t timeo_ms; 17390 17391 ASSERT(MUTEX_HELD(&ill->ill_lock)); 17392 17393 /* If the ill is closing or opening don't proceed */ 17394 if (ill->ill_state_flags & ILL_CONDEMNED) 17395 return; 17396 17397 if (ill->ill_fragtimer_executing) { 17398 /* 17399 * ill_frag_timer is currently executing. Just record the 17400 * the fact that we want the timer to be restarted. 17401 * ill_frag_timer will post a timeout before it returns, 17402 * ensuring it will be called again. 17403 */ 17404 ill->ill_fragtimer_needrestart = 1; 17405 return; 17406 } 17407 17408 if (ill->ill_frag_timer_id == 0) { 17409 if (ill->ill_isv6) 17410 timeo_ms = ipst->ips_ipv6_frag_timo_ms; 17411 else 17412 timeo_ms = ipst->ips_ip_g_frag_timo_ms; 17413 /* 17414 * The timer is neither running nor is the timeout handler 17415 * executing. Post a timeout so that ill_frag_timer will be 17416 * called 17417 */ 17418 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 17419 MSEC_TO_TICK(timeo_ms >> 1)); 17420 ill->ill_fragtimer_needrestart = 0; 17421 } 17422 } 17423 17424 /* 17425 * This routine is needed for loopback when forwarding multicasts. 17426 * 17427 * IPQoS Notes: 17428 * IPPF processing is done in fanout routines. 17429 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 17430 * processing for IPsec packets is done when it comes back in clear. 17431 * NOTE : The callers of this function need to do the ire_refrele for the 17432 * ire that is being passed in. 17433 */ 17434 void 17435 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 17436 ill_t *recv_ill, uint32_t esp_udp_ports) 17437 { 17438 boolean_t esp_in_udp_packet = (esp_udp_ports != 0); 17439 ill_t *ill = (ill_t *)q->q_ptr; 17440 uint32_t sum; 17441 uint32_t u1; 17442 uint32_t u2; 17443 int hdr_length; 17444 boolean_t mctl_present; 17445 mblk_t *first_mp = mp; 17446 mblk_t *hada_mp = NULL; 17447 ipha_t *inner_ipha; 17448 ip_stack_t *ipst; 17449 17450 ASSERT(recv_ill != NULL); 17451 ipst = recv_ill->ill_ipst; 17452 17453 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 17454 "ip_rput_locl_start: q %p", q); 17455 17456 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17457 ASSERT(ill != NULL); 17458 17459 #define rptr ((uchar_t *)ipha) 17460 #define iphs ((uint16_t *)ipha) 17461 17462 /* 17463 * no UDP or TCP packet should come here anymore. 17464 */ 17465 ASSERT(ipha->ipha_protocol != IPPROTO_TCP && 17466 ipha->ipha_protocol != IPPROTO_UDP); 17467 17468 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 17469 if (mctl_present && 17470 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 17471 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 17472 17473 /* 17474 * It's an IPsec accelerated packet. 17475 * Keep a pointer to the data attributes around until 17476 * we allocate the ipsec_info_t. 17477 */ 17478 IPSECHW_DEBUG(IPSECHW_PKT, 17479 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 17480 hada_mp = first_mp; 17481 hada_mp->b_cont = NULL; 17482 /* 17483 * Since it is accelerated, it comes directly from 17484 * the ill and the data attributes is followed by 17485 * the packet data. 17486 */ 17487 ASSERT(mp->b_datap->db_type != M_CTL); 17488 first_mp = mp; 17489 mctl_present = B_FALSE; 17490 } 17491 17492 /* 17493 * IF M_CTL is not present, then ipsec_in_is_secure 17494 * should return B_TRUE. There is a case where loopback 17495 * packets has an M_CTL in the front with all the 17496 * IPsec options set to IPSEC_PREF_NEVER - which means 17497 * ipsec_in_is_secure will return B_FALSE. As loopback 17498 * packets never comes here, it is safe to ASSERT the 17499 * following. 17500 */ 17501 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 17502 17503 /* 17504 * Also, we should never have an mctl_present if this is an 17505 * ESP-in-UDP packet. 17506 */ 17507 ASSERT(!mctl_present || !esp_in_udp_packet); 17508 17509 /* u1 is # words of IP options */ 17510 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 17511 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 17512 17513 /* 17514 * Don't verify header checksum if we just removed UDP header or 17515 * packet is coming back from AH/ESP. 17516 */ 17517 if (!esp_in_udp_packet && !mctl_present) { 17518 if (u1) { 17519 if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { 17520 if (hada_mp != NULL) 17521 freemsg(hada_mp); 17522 return; 17523 } 17524 } else { 17525 /* Check the IP header checksum. */ 17526 #define uph ((uint16_t *)ipha) 17527 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 17528 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 17529 #undef uph 17530 /* finish doing IP checksum */ 17531 sum = (sum & 0xFFFF) + (sum >> 16); 17532 sum = ~(sum + (sum >> 16)) & 0xFFFF; 17533 if (sum && sum != 0xFFFF) { 17534 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 17535 goto drop_pkt; 17536 } 17537 } 17538 } 17539 17540 /* 17541 * Count for SNMP of inbound packets for ire. As ip_proto_input 17542 * might be called more than once for secure packets, count only 17543 * the first time. 17544 */ 17545 if (!mctl_present) { 17546 UPDATE_IB_PKT_COUNT(ire); 17547 ire->ire_last_used_time = lbolt; 17548 } 17549 17550 /* Check for fragmentation offset. */ 17551 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 17552 u1 = u2 & (IPH_MF | IPH_OFFSET); 17553 if (u1) { 17554 /* 17555 * We re-assemble fragments before we do the AH/ESP 17556 * processing. Thus, M_CTL should not be present 17557 * while we are re-assembling. 17558 */ 17559 ASSERT(!mctl_present); 17560 ASSERT(first_mp == mp); 17561 if (!ip_rput_fragment(ill, recv_ill, &mp, ipha, NULL, NULL)) 17562 return; 17563 17564 /* 17565 * Make sure that first_mp points back to mp as 17566 * the mp we came in with could have changed in 17567 * ip_rput_fragment(). 17568 */ 17569 ipha = (ipha_t *)mp->b_rptr; 17570 first_mp = mp; 17571 } 17572 17573 /* 17574 * Clear hardware checksumming flag as it is currently only 17575 * used by TCP and UDP. 17576 */ 17577 DB_CKSUMFLAGS(mp) = 0; 17578 17579 /* Now we have a complete datagram, destined for this machine. */ 17580 u1 = IPH_HDR_LENGTH(ipha); 17581 switch (ipha->ipha_protocol) { 17582 case IPPROTO_ICMP: { 17583 ire_t *ire_zone; 17584 ilm_t *ilm; 17585 mblk_t *mp1; 17586 zoneid_t last_zoneid; 17587 ilm_walker_t ilw; 17588 17589 if (CLASSD(ipha->ipha_dst) && !IS_LOOPBACK(recv_ill)) { 17590 ASSERT(ire->ire_type == IRE_BROADCAST); 17591 17592 /* 17593 * In the multicast case, applications may have joined 17594 * the group from different zones, so we need to deliver 17595 * the packet to each of them. Loop through the 17596 * multicast memberships structures (ilm) on the receive 17597 * ill and send a copy of the packet up each matching 17598 * one. However, we don't do this for multicasts sent on 17599 * the loopback interface (PHYI_LOOPBACK flag set) as 17600 * they must stay in the sender's zone. 17601 * 17602 * ilm_add_v6() ensures that ilms in the same zone are 17603 * contiguous in the ill_ilm list. We use this property 17604 * to avoid sending duplicates needed when two 17605 * applications in the same zone join the same group on 17606 * different logical interfaces: we ignore the ilm if 17607 * its zoneid is the same as the last matching one. 17608 * In addition, the sending of the packet for 17609 * ire_zoneid is delayed until all of the other ilms 17610 * have been exhausted. 17611 */ 17612 last_zoneid = -1; 17613 ilm = ilm_walker_start(&ilw, recv_ill); 17614 for (; ilm != NULL; ilm = ilm_walker_step(&ilw, ilm)) { 17615 if (ipha->ipha_dst != ilm->ilm_addr || 17616 ilm->ilm_zoneid == last_zoneid || 17617 ilm->ilm_zoneid == ire->ire_zoneid || 17618 ilm->ilm_zoneid == ALL_ZONES || 17619 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 17620 continue; 17621 mp1 = ip_copymsg(first_mp); 17622 if (mp1 == NULL) 17623 continue; 17624 icmp_inbound(q, mp1, B_TRUE, ilw.ilw_walk_ill, 17625 0, sum, mctl_present, B_TRUE, 17626 recv_ill, ilm->ilm_zoneid); 17627 last_zoneid = ilm->ilm_zoneid; 17628 } 17629 ilm_walker_finish(&ilw); 17630 } else if (ire->ire_type == IRE_BROADCAST) { 17631 /* 17632 * In the broadcast case, there may be many zones 17633 * which need a copy of the packet delivered to them. 17634 * There is one IRE_BROADCAST per broadcast address 17635 * and per zone; we walk those using a helper function. 17636 * In addition, the sending of the packet for ire is 17637 * delayed until all of the other ires have been 17638 * processed. 17639 */ 17640 IRB_REFHOLD(ire->ire_bucket); 17641 ire_zone = NULL; 17642 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 17643 ire)) != NULL) { 17644 mp1 = ip_copymsg(first_mp); 17645 if (mp1 == NULL) 17646 continue; 17647 17648 UPDATE_IB_PKT_COUNT(ire_zone); 17649 ire_zone->ire_last_used_time = lbolt; 17650 icmp_inbound(q, mp1, B_TRUE, ill, 17651 0, sum, mctl_present, B_TRUE, 17652 recv_ill, ire_zone->ire_zoneid); 17653 } 17654 IRB_REFRELE(ire->ire_bucket); 17655 } 17656 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 17657 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 17658 ire->ire_zoneid); 17659 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17660 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 17661 return; 17662 } 17663 case IPPROTO_IGMP: 17664 /* 17665 * If we are not willing to accept IGMP packets in clear, 17666 * then check with global policy. 17667 */ 17668 if (ipst->ips_igmp_accept_clear_messages == 0) { 17669 first_mp = ipsec_check_global_policy(first_mp, NULL, 17670 ipha, NULL, mctl_present, ipst->ips_netstack); 17671 if (first_mp == NULL) 17672 return; 17673 } 17674 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17675 freemsg(first_mp); 17676 ip1dbg(("ip_proto_input: zone all cannot accept raw")); 17677 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17678 return; 17679 } 17680 if ((mp = igmp_input(q, mp, ill)) == NULL) { 17681 /* Bad packet - discarded by igmp_input */ 17682 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17683 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 17684 if (mctl_present) 17685 freeb(first_mp); 17686 return; 17687 } 17688 /* 17689 * igmp_input() may have returned the pulled up message. 17690 * So first_mp and ipha need to be reinitialized. 17691 */ 17692 ipha = (ipha_t *)mp->b_rptr; 17693 if (mctl_present) 17694 first_mp->b_cont = mp; 17695 else 17696 first_mp = mp; 17697 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17698 connf_head != NULL) { 17699 /* No user-level listener for IGMP packets */ 17700 goto drop_pkt; 17701 } 17702 /* deliver to local raw users */ 17703 break; 17704 case IPPROTO_PIM: 17705 /* 17706 * If we are not willing to accept PIM packets in clear, 17707 * then check with global policy. 17708 */ 17709 if (ipst->ips_pim_accept_clear_messages == 0) { 17710 first_mp = ipsec_check_global_policy(first_mp, NULL, 17711 ipha, NULL, mctl_present, ipst->ips_netstack); 17712 if (first_mp == NULL) 17713 return; 17714 } 17715 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17716 freemsg(first_mp); 17717 ip1dbg(("ip_proto_input: zone all cannot accept PIM")); 17718 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17719 return; 17720 } 17721 if (pim_input(q, mp, ill) != 0) { 17722 /* Bad packet - discarded by pim_input */ 17723 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17724 "ip_rput_locl_end: q %p (%S)", q, "pim"); 17725 if (mctl_present) 17726 freeb(first_mp); 17727 return; 17728 } 17729 17730 /* 17731 * pim_input() may have pulled up the message so ipha needs to 17732 * be reinitialized. 17733 */ 17734 ipha = (ipha_t *)mp->b_rptr; 17735 if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. 17736 connf_head != NULL) { 17737 /* No user-level listener for PIM packets */ 17738 goto drop_pkt; 17739 } 17740 /* deliver to local raw users */ 17741 break; 17742 case IPPROTO_ENCAP: 17743 /* 17744 * Handle self-encapsulated packets (IP-in-IP where 17745 * the inner addresses == the outer addresses). 17746 */ 17747 hdr_length = IPH_HDR_LENGTH(ipha); 17748 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 17749 mp->b_wptr) { 17750 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 17751 sizeof (ipha_t) - mp->b_rptr)) { 17752 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17753 freemsg(first_mp); 17754 return; 17755 } 17756 ipha = (ipha_t *)mp->b_rptr; 17757 } 17758 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 17759 /* 17760 * Check the sanity of the inner IP header. 17761 */ 17762 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 17763 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17764 freemsg(first_mp); 17765 return; 17766 } 17767 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 17768 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17769 freemsg(first_mp); 17770 return; 17771 } 17772 if (inner_ipha->ipha_src == ipha->ipha_src && 17773 inner_ipha->ipha_dst == ipha->ipha_dst) { 17774 ipsec_in_t *ii; 17775 17776 /* 17777 * Self-encapsulated tunnel packet. Remove 17778 * the outer IP header and fanout again. 17779 * We also need to make sure that the inner 17780 * header is pulled up until options. 17781 */ 17782 mp->b_rptr = (uchar_t *)inner_ipha; 17783 ipha = inner_ipha; 17784 hdr_length = IPH_HDR_LENGTH(ipha); 17785 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 17786 if (!pullupmsg(mp, (uchar_t *)ipha + 17787 + hdr_length - mp->b_rptr)) { 17788 freemsg(first_mp); 17789 return; 17790 } 17791 ipha = (ipha_t *)mp->b_rptr; 17792 } 17793 if (hdr_length > sizeof (ipha_t)) { 17794 /* We got options on the inner packet. */ 17795 ipaddr_t dst = ipha->ipha_dst; 17796 17797 if (ip_rput_options(q, mp, ipha, &dst, ipst) == 17798 -1) { 17799 /* Bad options! */ 17800 return; 17801 } 17802 if (dst != ipha->ipha_dst) { 17803 /* 17804 * Someone put a source-route in 17805 * the inside header of a self- 17806 * encapsulated packet. Drop it 17807 * with extreme prejudice and let 17808 * the sender know. 17809 */ 17810 icmp_unreachable(q, first_mp, 17811 ICMP_SOURCE_ROUTE_FAILED, 17812 recv_ill->ill_zoneid, ipst); 17813 return; 17814 } 17815 } 17816 if (!mctl_present) { 17817 ASSERT(first_mp == mp); 17818 /* 17819 * This means that somebody is sending 17820 * Self-encapsualted packets without AH/ESP. 17821 * If AH/ESP was present, we would have already 17822 * allocated the first_mp. 17823 * 17824 * Send this packet to find a tunnel endpoint. 17825 * if I can't find one, an ICMP 17826 * PROTOCOL_UNREACHABLE will get sent. 17827 */ 17828 goto fanout; 17829 } 17830 /* 17831 * We generally store the ill_index if we need to 17832 * do IPsec processing as we lose the ill queue when 17833 * we come back. But in this case, we never should 17834 * have to store the ill_index here as it should have 17835 * been stored previously when we processed the 17836 * AH/ESP header in this routine or for non-ipsec 17837 * cases, we still have the queue. But for some bad 17838 * packets from the wire, we can get to IPsec after 17839 * this and we better store the index for that case. 17840 */ 17841 ill = (ill_t *)q->q_ptr; 17842 ii = (ipsec_in_t *)first_mp->b_rptr; 17843 ii->ipsec_in_ill_index = 17844 ill->ill_phyint->phyint_ifindex; 17845 ii->ipsec_in_rill_index = 17846 recv_ill->ill_phyint->phyint_ifindex; 17847 if (ii->ipsec_in_decaps) { 17848 /* 17849 * This packet is self-encapsulated multiple 17850 * times. We don't want to recurse infinitely. 17851 * To keep it simple, drop the packet. 17852 */ 17853 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17854 freemsg(first_mp); 17855 return; 17856 } 17857 ii->ipsec_in_decaps = B_TRUE; 17858 ip_fanout_proto_again(first_mp, recv_ill, recv_ill, 17859 ire); 17860 return; 17861 } 17862 break; 17863 case IPPROTO_AH: 17864 case IPPROTO_ESP: { 17865 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 17866 17867 /* 17868 * Fast path for AH/ESP. If this is the first time 17869 * we are sending a datagram to AH/ESP, allocate 17870 * a IPSEC_IN message and prepend it. Otherwise, 17871 * just fanout. 17872 */ 17873 17874 int ipsec_rc; 17875 ipsec_in_t *ii; 17876 netstack_t *ns = ipst->ips_netstack; 17877 17878 IP_STAT(ipst, ipsec_proto_ahesp); 17879 if (!mctl_present) { 17880 ASSERT(first_mp == mp); 17881 first_mp = ipsec_in_alloc(B_TRUE, ns); 17882 if (first_mp == NULL) { 17883 ip1dbg(("ip_proto_input: IPSEC_IN " 17884 "allocation failure.\n")); 17885 freemsg(hada_mp); /* okay ifnull */ 17886 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17887 freemsg(mp); 17888 return; 17889 } 17890 /* 17891 * Store the ill_index so that when we come back 17892 * from IPsec we ride on the same queue. 17893 */ 17894 ill = (ill_t *)q->q_ptr; 17895 ii = (ipsec_in_t *)first_mp->b_rptr; 17896 ii->ipsec_in_ill_index = 17897 ill->ill_phyint->phyint_ifindex; 17898 ii->ipsec_in_rill_index = 17899 recv_ill->ill_phyint->phyint_ifindex; 17900 first_mp->b_cont = mp; 17901 /* 17902 * Cache hardware acceleration info. 17903 */ 17904 if (hada_mp != NULL) { 17905 IPSECHW_DEBUG(IPSECHW_PKT, 17906 ("ip_rput_local: caching data attr.\n")); 17907 ii->ipsec_in_accelerated = B_TRUE; 17908 ii->ipsec_in_da = hada_mp; 17909 hada_mp = NULL; 17910 } 17911 } else { 17912 ii = (ipsec_in_t *)first_mp->b_rptr; 17913 } 17914 17915 ii->ipsec_in_esp_udp_ports = esp_udp_ports; 17916 17917 if (!ipsec_loaded(ipss)) { 17918 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 17919 ire->ire_zoneid, ipst); 17920 return; 17921 } 17922 17923 ns = ipst->ips_netstack; 17924 /* select inbound SA and have IPsec process the pkt */ 17925 if (ipha->ipha_protocol == IPPROTO_ESP) { 17926 esph_t *esph = ipsec_inbound_esp_sa(first_mp, ns); 17927 boolean_t esp_in_udp_sa; 17928 if (esph == NULL) 17929 return; 17930 ASSERT(ii->ipsec_in_esp_sa != NULL); 17931 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 17932 esp_in_udp_sa = ((ii->ipsec_in_esp_sa->ipsa_flags & 17933 IPSA_F_NATT) != 0); 17934 /* 17935 * The following is a fancy, but quick, way of saying: 17936 * ESP-in-UDP SA and Raw ESP packet --> drop 17937 * OR 17938 * ESP SA and ESP-in-UDP packet --> drop 17939 */ 17940 if (esp_in_udp_sa != esp_in_udp_packet) { 17941 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17942 ip_drop_packet(first_mp, B_TRUE, ill, NULL, 17943 DROPPER(ns->netstack_ipsec, ipds_esp_no_sa), 17944 &ns->netstack_ipsec->ipsec_dropper); 17945 return; 17946 } 17947 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 17948 first_mp, esph); 17949 } else { 17950 ah_t *ah = ipsec_inbound_ah_sa(first_mp, ns); 17951 if (ah == NULL) 17952 return; 17953 ASSERT(ii->ipsec_in_ah_sa != NULL); 17954 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 17955 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 17956 first_mp, ah); 17957 } 17958 17959 switch (ipsec_rc) { 17960 case IPSEC_STATUS_SUCCESS: 17961 break; 17962 case IPSEC_STATUS_FAILED: 17963 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17964 /* FALLTHRU */ 17965 case IPSEC_STATUS_PENDING: 17966 return; 17967 } 17968 /* we're done with IPsec processing, send it up */ 17969 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 17970 return; 17971 } 17972 default: 17973 break; 17974 } 17975 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { 17976 ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", 17977 ire->ire_zoneid)); 17978 goto drop_pkt; 17979 } 17980 /* 17981 * Handle protocols with which IP is less intimate. There 17982 * can be more than one stream bound to a particular 17983 * protocol. When this is the case, each one gets a copy 17984 * of any incoming packets. 17985 */ 17986 fanout: 17987 ip_fanout_proto(q, first_mp, ill, ipha, 17988 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 17989 B_TRUE, recv_ill, ire->ire_zoneid); 17990 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17991 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 17992 return; 17993 17994 drop_pkt: 17995 freemsg(first_mp); 17996 if (hada_mp != NULL) 17997 freeb(hada_mp); 17998 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17999 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 18000 #undef rptr 18001 #undef iphs 18002 18003 } 18004 18005 /* 18006 * Update any source route, record route or timestamp options. 18007 * Check that we are at end of strict source route. 18008 * The options have already been checked for sanity in ip_rput_options(). 18009 */ 18010 static boolean_t 18011 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 18012 ip_stack_t *ipst) 18013 { 18014 ipoptp_t opts; 18015 uchar_t *opt; 18016 uint8_t optval; 18017 uint8_t optlen; 18018 ipaddr_t dst; 18019 uint32_t ts; 18020 ire_t *dst_ire; 18021 timestruc_t now; 18022 zoneid_t zoneid; 18023 ill_t *ill; 18024 18025 ASSERT(ire->ire_ipversion == IPV4_VERSION); 18026 18027 ip2dbg(("ip_rput_local_options\n")); 18028 18029 for (optval = ipoptp_first(&opts, ipha); 18030 optval != IPOPT_EOL; 18031 optval = ipoptp_next(&opts)) { 18032 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 18033 opt = opts.ipoptp_cur; 18034 optlen = opts.ipoptp_len; 18035 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 18036 optval, optlen)); 18037 switch (optval) { 18038 uint32_t off; 18039 case IPOPT_SSRR: 18040 case IPOPT_LSRR: 18041 off = opt[IPOPT_OFFSET]; 18042 off--; 18043 if (optlen < IP_ADDR_LEN || 18044 off > optlen - IP_ADDR_LEN) { 18045 /* End of source route */ 18046 ip1dbg(("ip_rput_local_options: end of SR\n")); 18047 break; 18048 } 18049 /* 18050 * This will only happen if two consecutive entries 18051 * in the source route contains our address or if 18052 * it is a packet with a loose source route which 18053 * reaches us before consuming the whole source route 18054 */ 18055 ip1dbg(("ip_rput_local_options: not end of SR\n")); 18056 if (optval == IPOPT_SSRR) { 18057 goto bad_src_route; 18058 } 18059 /* 18060 * Hack: instead of dropping the packet truncate the 18061 * source route to what has been used by filling the 18062 * rest with IPOPT_NOP. 18063 */ 18064 opt[IPOPT_OLEN] = (uint8_t)off; 18065 while (off < optlen) { 18066 opt[off++] = IPOPT_NOP; 18067 } 18068 break; 18069 case IPOPT_RR: 18070 off = opt[IPOPT_OFFSET]; 18071 off--; 18072 if (optlen < IP_ADDR_LEN || 18073 off > optlen - IP_ADDR_LEN) { 18074 /* No more room - ignore */ 18075 ip1dbg(( 18076 "ip_rput_local_options: end of RR\n")); 18077 break; 18078 } 18079 bcopy(&ire->ire_src_addr, (char *)opt + off, 18080 IP_ADDR_LEN); 18081 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 18082 break; 18083 case IPOPT_TS: 18084 /* Insert timestamp if there is romm */ 18085 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 18086 case IPOPT_TS_TSONLY: 18087 off = IPOPT_TS_TIMELEN; 18088 break; 18089 case IPOPT_TS_PRESPEC: 18090 case IPOPT_TS_PRESPEC_RFC791: 18091 /* Verify that the address matched */ 18092 off = opt[IPOPT_OFFSET] - 1; 18093 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 18094 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 18095 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 18096 ipst); 18097 if (dst_ire == NULL) { 18098 /* Not for us */ 18099 break; 18100 } 18101 ire_refrele(dst_ire); 18102 /* FALLTHRU */ 18103 case IPOPT_TS_TSANDADDR: 18104 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 18105 break; 18106 default: 18107 /* 18108 * ip_*put_options should have already 18109 * dropped this packet. 18110 */ 18111 cmn_err(CE_PANIC, "ip_rput_local_options: " 18112 "unknown IT - bug in ip_rput_options?\n"); 18113 return (B_TRUE); /* Keep "lint" happy */ 18114 } 18115 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 18116 /* Increase overflow counter */ 18117 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 18118 opt[IPOPT_POS_OV_FLG] = 18119 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 18120 (off << 4)); 18121 break; 18122 } 18123 off = opt[IPOPT_OFFSET] - 1; 18124 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 18125 case IPOPT_TS_PRESPEC: 18126 case IPOPT_TS_PRESPEC_RFC791: 18127 case IPOPT_TS_TSANDADDR: 18128 bcopy(&ire->ire_src_addr, (char *)opt + off, 18129 IP_ADDR_LEN); 18130 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 18131 /* FALLTHRU */ 18132 case IPOPT_TS_TSONLY: 18133 off = opt[IPOPT_OFFSET] - 1; 18134 /* Compute # of milliseconds since midnight */ 18135 gethrestime(&now); 18136 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 18137 now.tv_nsec / (NANOSEC / MILLISEC); 18138 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 18139 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 18140 break; 18141 } 18142 break; 18143 } 18144 } 18145 return (B_TRUE); 18146 18147 bad_src_route: 18148 q = WR(q); 18149 if (q->q_next != NULL) 18150 ill = q->q_ptr; 18151 else 18152 ill = NULL; 18153 18154 /* make sure we clear any indication of a hardware checksum */ 18155 DB_CKSUMFLAGS(mp) = 0; 18156 zoneid = ipif_lookup_addr_zoneid(ipha->ipha_dst, ill, ipst); 18157 if (zoneid == ALL_ZONES) 18158 freemsg(mp); 18159 else 18160 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 18161 return (B_FALSE); 18162 18163 } 18164 18165 /* 18166 * Process IP options in an inbound packet. If an option affects the 18167 * effective destination address, return the next hop address via dstp. 18168 * Returns -1 if something fails in which case an ICMP error has been sent 18169 * and mp freed. 18170 */ 18171 static int 18172 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp, 18173 ip_stack_t *ipst) 18174 { 18175 ipoptp_t opts; 18176 uchar_t *opt; 18177 uint8_t optval; 18178 uint8_t optlen; 18179 ipaddr_t dst; 18180 intptr_t code = 0; 18181 ire_t *ire = NULL; 18182 zoneid_t zoneid; 18183 ill_t *ill; 18184 18185 ip2dbg(("ip_rput_options\n")); 18186 dst = ipha->ipha_dst; 18187 for (optval = ipoptp_first(&opts, ipha); 18188 optval != IPOPT_EOL; 18189 optval = ipoptp_next(&opts)) { 18190 opt = opts.ipoptp_cur; 18191 optlen = opts.ipoptp_len; 18192 ip2dbg(("ip_rput_options: opt %d, len %d\n", 18193 optval, optlen)); 18194 /* 18195 * Note: we need to verify the checksum before we 18196 * modify anything thus this routine only extracts the next 18197 * hop dst from any source route. 18198 */ 18199 switch (optval) { 18200 uint32_t off; 18201 case IPOPT_SSRR: 18202 case IPOPT_LSRR: 18203 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 18204 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 18205 if (ire == NULL) { 18206 if (optval == IPOPT_SSRR) { 18207 ip1dbg(("ip_rput_options: not next" 18208 " strict source route 0x%x\n", 18209 ntohl(dst))); 18210 code = (char *)&ipha->ipha_dst - 18211 (char *)ipha; 18212 goto param_prob; /* RouterReq's */ 18213 } 18214 ip2dbg(("ip_rput_options: " 18215 "not next source route 0x%x\n", 18216 ntohl(dst))); 18217 break; 18218 } 18219 ire_refrele(ire); 18220 18221 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 18222 ip1dbg(( 18223 "ip_rput_options: bad option offset\n")); 18224 code = (char *)&opt[IPOPT_OLEN] - 18225 (char *)ipha; 18226 goto param_prob; 18227 } 18228 off = opt[IPOPT_OFFSET]; 18229 off--; 18230 redo_srr: 18231 if (optlen < IP_ADDR_LEN || 18232 off > optlen - IP_ADDR_LEN) { 18233 /* End of source route */ 18234 ip1dbg(("ip_rput_options: end of SR\n")); 18235 break; 18236 } 18237 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 18238 ip1dbg(("ip_rput_options: next hop 0x%x\n", 18239 ntohl(dst))); 18240 18241 /* 18242 * Check if our address is present more than 18243 * once as consecutive hops in source route. 18244 * XXX verify per-interface ip_forwarding 18245 * for source route? 18246 */ 18247 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 18248 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 18249 18250 if (ire != NULL) { 18251 ire_refrele(ire); 18252 off += IP_ADDR_LEN; 18253 goto redo_srr; 18254 } 18255 18256 if (dst == htonl(INADDR_LOOPBACK)) { 18257 ip1dbg(("ip_rput_options: loopback addr in " 18258 "source route!\n")); 18259 goto bad_src_route; 18260 } 18261 /* 18262 * For strict: verify that dst is directly 18263 * reachable. 18264 */ 18265 if (optval == IPOPT_SSRR) { 18266 ire = ire_ftable_lookup(dst, 0, 0, 18267 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 18268 msg_getlabel(mp), 18269 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 18270 if (ire == NULL) { 18271 ip1dbg(("ip_rput_options: SSRR not " 18272 "directly reachable: 0x%x\n", 18273 ntohl(dst))); 18274 goto bad_src_route; 18275 } 18276 ire_refrele(ire); 18277 } 18278 /* 18279 * Defer update of the offset and the record route 18280 * until the packet is forwarded. 18281 */ 18282 break; 18283 case IPOPT_RR: 18284 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 18285 ip1dbg(( 18286 "ip_rput_options: bad option offset\n")); 18287 code = (char *)&opt[IPOPT_OLEN] - 18288 (char *)ipha; 18289 goto param_prob; 18290 } 18291 break; 18292 case IPOPT_TS: 18293 /* 18294 * Verify that length >= 5 and that there is either 18295 * room for another timestamp or that the overflow 18296 * counter is not maxed out. 18297 */ 18298 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 18299 if (optlen < IPOPT_MINLEN_IT) { 18300 goto param_prob; 18301 } 18302 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 18303 ip1dbg(( 18304 "ip_rput_options: bad option offset\n")); 18305 code = (char *)&opt[IPOPT_OFFSET] - 18306 (char *)ipha; 18307 goto param_prob; 18308 } 18309 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 18310 case IPOPT_TS_TSONLY: 18311 off = IPOPT_TS_TIMELEN; 18312 break; 18313 case IPOPT_TS_TSANDADDR: 18314 case IPOPT_TS_PRESPEC: 18315 case IPOPT_TS_PRESPEC_RFC791: 18316 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 18317 break; 18318 default: 18319 code = (char *)&opt[IPOPT_POS_OV_FLG] - 18320 (char *)ipha; 18321 goto param_prob; 18322 } 18323 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 18324 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 18325 /* 18326 * No room and the overflow counter is 15 18327 * already. 18328 */ 18329 goto param_prob; 18330 } 18331 break; 18332 } 18333 } 18334 18335 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 18336 *dstp = dst; 18337 return (0); 18338 } 18339 18340 ip1dbg(("ip_rput_options: error processing IP options.")); 18341 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 18342 18343 param_prob: 18344 q = WR(q); 18345 if (q->q_next != NULL) 18346 ill = q->q_ptr; 18347 else 18348 ill = NULL; 18349 18350 /* make sure we clear any indication of a hardware checksum */ 18351 DB_CKSUMFLAGS(mp) = 0; 18352 /* Don't know whether this is for non-global or global/forwarding */ 18353 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 18354 if (zoneid == ALL_ZONES) 18355 freemsg(mp); 18356 else 18357 icmp_param_problem(q, mp, (uint8_t)code, zoneid, ipst); 18358 return (-1); 18359 18360 bad_src_route: 18361 q = WR(q); 18362 if (q->q_next != NULL) 18363 ill = q->q_ptr; 18364 else 18365 ill = NULL; 18366 18367 /* make sure we clear any indication of a hardware checksum */ 18368 DB_CKSUMFLAGS(mp) = 0; 18369 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 18370 if (zoneid == ALL_ZONES) 18371 freemsg(mp); 18372 else 18373 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 18374 return (-1); 18375 } 18376 18377 /* 18378 * IP & ICMP info in >=14 msg's ... 18379 * - ip fixed part (mib2_ip_t) 18380 * - icmp fixed part (mib2_icmp_t) 18381 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 18382 * - ipRouteEntryTable (ip 21) all IPv4 IREs 18383 * - ipNetToMediaEntryTable (ip 22) [filled in by the arp module] 18384 * - ipRouteAttributeTable (ip 102) labeled routes 18385 * - ip multicast membership (ip_member_t) 18386 * - ip multicast source filtering (ip_grpsrc_t) 18387 * - igmp fixed part (struct igmpstat) 18388 * - multicast routing stats (struct mrtstat) 18389 * - multicast routing vifs (array of struct vifctl) 18390 * - multicast routing routes (array of struct mfcctl) 18391 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 18392 * One per ill plus one generic 18393 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 18394 * One per ill plus one generic 18395 * - ipv6RouteEntry all IPv6 IREs 18396 * - ipv6RouteAttributeTable (ip6 102) labeled routes 18397 * - ipv6NetToMediaEntry all Neighbor Cache entries 18398 * - ipv6AddrEntry all IPv6 ipifs 18399 * - ipv6 multicast membership (ipv6_member_t) 18400 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 18401 * 18402 * MIB2_IP_MEDIA is filled in by the arp module with ARP cache entries. 18403 * 18404 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 18405 * already filled in by the caller. 18406 * Return value of 0 indicates that no messages were sent and caller 18407 * should free mpctl. 18408 */ 18409 int 18410 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level) 18411 { 18412 ip_stack_t *ipst; 18413 sctp_stack_t *sctps; 18414 18415 if (q->q_next != NULL) { 18416 ipst = ILLQ_TO_IPST(q); 18417 } else { 18418 ipst = CONNQ_TO_IPST(q); 18419 } 18420 ASSERT(ipst != NULL); 18421 sctps = ipst->ips_netstack->netstack_sctp; 18422 18423 if (mpctl == NULL || mpctl->b_cont == NULL) { 18424 return (0); 18425 } 18426 18427 /* 18428 * For the purposes of the (broken) packet shell use 18429 * of the level we make sure MIB2_TCP/MIB2_UDP can be used 18430 * to make TCP and UDP appear first in the list of mib items. 18431 * TBD: We could expand this and use it in netstat so that 18432 * the kernel doesn't have to produce large tables (connections, 18433 * routes, etc) when netstat only wants the statistics or a particular 18434 * table. 18435 */ 18436 if (!(level == MIB2_TCP || level == MIB2_UDP)) { 18437 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) { 18438 return (1); 18439 } 18440 } 18441 18442 if (level != MIB2_TCP) { 18443 if ((mpctl = udp_snmp_get(q, mpctl)) == NULL) { 18444 return (1); 18445 } 18446 } 18447 18448 if (level != MIB2_UDP) { 18449 if ((mpctl = tcp_snmp_get(q, mpctl)) == NULL) { 18450 return (1); 18451 } 18452 } 18453 18454 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl, 18455 ipst)) == NULL) { 18456 return (1); 18457 } 18458 18459 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst)) == NULL) { 18460 return (1); 18461 } 18462 18463 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) { 18464 return (1); 18465 } 18466 18467 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) { 18468 return (1); 18469 } 18470 18471 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) { 18472 return (1); 18473 } 18474 18475 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) { 18476 return (1); 18477 } 18478 18479 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst)) == NULL) { 18480 return (1); 18481 } 18482 18483 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst)) == NULL) { 18484 return (1); 18485 } 18486 18487 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) { 18488 return (1); 18489 } 18490 18491 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) { 18492 return (1); 18493 } 18494 18495 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) { 18496 return (1); 18497 } 18498 18499 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) { 18500 return (1); 18501 } 18502 18503 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) { 18504 return (1); 18505 } 18506 18507 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) { 18508 return (1); 18509 } 18510 18511 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst); 18512 if (mpctl == NULL) 18513 return (1); 18514 18515 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst); 18516 if (mpctl == NULL) 18517 return (1); 18518 18519 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) { 18520 return (1); 18521 } 18522 freemsg(mpctl); 18523 return (1); 18524 } 18525 18526 /* Get global (legacy) IPv4 statistics */ 18527 static mblk_t * 18528 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib, 18529 ip_stack_t *ipst) 18530 { 18531 mib2_ip_t old_ip_mib; 18532 struct opthdr *optp; 18533 mblk_t *mp2ctl; 18534 18535 /* 18536 * make a copy of the original message 18537 */ 18538 mp2ctl = copymsg(mpctl); 18539 18540 /* fixed length IP structure... */ 18541 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18542 optp->level = MIB2_IP; 18543 optp->name = 0; 18544 SET_MIB(old_ip_mib.ipForwarding, 18545 (WE_ARE_FORWARDING(ipst) ? 1 : 2)); 18546 SET_MIB(old_ip_mib.ipDefaultTTL, 18547 (uint32_t)ipst->ips_ip_def_ttl); 18548 SET_MIB(old_ip_mib.ipReasmTimeout, 18549 ipst->ips_ip_g_frag_timeout); 18550 SET_MIB(old_ip_mib.ipAddrEntrySize, 18551 sizeof (mib2_ipAddrEntry_t)); 18552 SET_MIB(old_ip_mib.ipRouteEntrySize, 18553 sizeof (mib2_ipRouteEntry_t)); 18554 SET_MIB(old_ip_mib.ipNetToMediaEntrySize, 18555 sizeof (mib2_ipNetToMediaEntry_t)); 18556 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 18557 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 18558 SET_MIB(old_ip_mib.ipRouteAttributeSize, 18559 sizeof (mib2_ipAttributeEntry_t)); 18560 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 18561 18562 /* 18563 * Grab the statistics from the new IP MIB 18564 */ 18565 SET_MIB(old_ip_mib.ipInReceives, 18566 (uint32_t)ipmib->ipIfStatsHCInReceives); 18567 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors); 18568 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors); 18569 SET_MIB(old_ip_mib.ipForwDatagrams, 18570 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams); 18571 SET_MIB(old_ip_mib.ipInUnknownProtos, 18572 ipmib->ipIfStatsInUnknownProtos); 18573 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards); 18574 SET_MIB(old_ip_mib.ipInDelivers, 18575 (uint32_t)ipmib->ipIfStatsHCInDelivers); 18576 SET_MIB(old_ip_mib.ipOutRequests, 18577 (uint32_t)ipmib->ipIfStatsHCOutRequests); 18578 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards); 18579 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes); 18580 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds); 18581 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs); 18582 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails); 18583 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs); 18584 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails); 18585 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates); 18586 18587 /* ipRoutingDiscards is not being used */ 18588 SET_MIB(old_ip_mib.ipRoutingDiscards, 0); 18589 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs); 18590 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts); 18591 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs); 18592 SET_MIB(old_ip_mib.ipReasmDuplicates, 18593 ipmib->ipIfStatsReasmDuplicates); 18594 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups); 18595 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits); 18596 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs); 18597 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows); 18598 SET_MIB(old_ip_mib.rawipInOverflows, 18599 ipmib->rawipIfStatsInOverflows); 18600 18601 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded); 18602 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed); 18603 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion); 18604 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion); 18605 SET_MIB(old_ip_mib.ipOutSwitchIPv6, 18606 ipmib->ipIfStatsOutSwitchIPVersion); 18607 18608 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib, 18609 (int)sizeof (old_ip_mib))) { 18610 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 18611 (uint_t)sizeof (old_ip_mib))); 18612 } 18613 18614 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18615 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 18616 (int)optp->level, (int)optp->name, (int)optp->len)); 18617 qreply(q, mpctl); 18618 return (mp2ctl); 18619 } 18620 18621 /* Per interface IPv4 statistics */ 18622 static mblk_t * 18623 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18624 { 18625 struct opthdr *optp; 18626 mblk_t *mp2ctl; 18627 ill_t *ill; 18628 ill_walk_context_t ctx; 18629 mblk_t *mp_tail = NULL; 18630 mib2_ipIfStatsEntry_t global_ip_mib; 18631 18632 /* 18633 * Make a copy of the original message 18634 */ 18635 mp2ctl = copymsg(mpctl); 18636 18637 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18638 optp->level = MIB2_IP; 18639 optp->name = MIB2_IP_TRAFFIC_STATS; 18640 /* Include "unknown interface" ip_mib */ 18641 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 18642 ipst->ips_ip_mib.ipIfStatsIfIndex = 18643 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 18644 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding, 18645 (ipst->ips_ip_g_forward ? 1 : 2)); 18646 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL, 18647 (uint32_t)ipst->ips_ip_def_ttl); 18648 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize, 18649 sizeof (mib2_ipIfStatsEntry_t)); 18650 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize, 18651 sizeof (mib2_ipAddrEntry_t)); 18652 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize, 18653 sizeof (mib2_ipRouteEntry_t)); 18654 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize, 18655 sizeof (mib2_ipNetToMediaEntry_t)); 18656 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize, 18657 sizeof (ip_member_t)); 18658 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize, 18659 sizeof (ip_grpsrc_t)); 18660 18661 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18662 (char *)&ipst->ips_ip_mib, (int)sizeof (ipst->ips_ip_mib))) { 18663 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18664 "failed to allocate %u bytes\n", 18665 (uint_t)sizeof (ipst->ips_ip_mib))); 18666 } 18667 18668 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib)); 18669 18670 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18671 ill = ILL_START_WALK_V4(&ctx, ipst); 18672 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18673 ill->ill_ip_mib->ipIfStatsIfIndex = 18674 ill->ill_phyint->phyint_ifindex; 18675 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 18676 (ipst->ips_ip_g_forward ? 1 : 2)); 18677 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL, 18678 (uint32_t)ipst->ips_ip_def_ttl); 18679 18680 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib); 18681 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18682 (char *)ill->ill_ip_mib, 18683 (int)sizeof (*ill->ill_ip_mib))) { 18684 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18685 "failed to allocate %u bytes\n", 18686 (uint_t)sizeof (*ill->ill_ip_mib))); 18687 } 18688 } 18689 rw_exit(&ipst->ips_ill_g_lock); 18690 18691 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18692 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18693 "level %d, name %d, len %d\n", 18694 (int)optp->level, (int)optp->name, (int)optp->len)); 18695 qreply(q, mpctl); 18696 18697 if (mp2ctl == NULL) 18698 return (NULL); 18699 18700 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst)); 18701 } 18702 18703 /* Global IPv4 ICMP statistics */ 18704 static mblk_t * 18705 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18706 { 18707 struct opthdr *optp; 18708 mblk_t *mp2ctl; 18709 18710 /* 18711 * Make a copy of the original message 18712 */ 18713 mp2ctl = copymsg(mpctl); 18714 18715 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18716 optp->level = MIB2_ICMP; 18717 optp->name = 0; 18718 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib, 18719 (int)sizeof (ipst->ips_icmp_mib))) { 18720 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 18721 (uint_t)sizeof (ipst->ips_icmp_mib))); 18722 } 18723 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18724 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 18725 (int)optp->level, (int)optp->name, (int)optp->len)); 18726 qreply(q, mpctl); 18727 return (mp2ctl); 18728 } 18729 18730 /* Global IPv4 IGMP statistics */ 18731 static mblk_t * 18732 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18733 { 18734 struct opthdr *optp; 18735 mblk_t *mp2ctl; 18736 18737 /* 18738 * make a copy of the original message 18739 */ 18740 mp2ctl = copymsg(mpctl); 18741 18742 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18743 optp->level = EXPER_IGMP; 18744 optp->name = 0; 18745 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat, 18746 (int)sizeof (ipst->ips_igmpstat))) { 18747 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 18748 (uint_t)sizeof (ipst->ips_igmpstat))); 18749 } 18750 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18751 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 18752 (int)optp->level, (int)optp->name, (int)optp->len)); 18753 qreply(q, mpctl); 18754 return (mp2ctl); 18755 } 18756 18757 /* Global IPv4 Multicast Routing statistics */ 18758 static mblk_t * 18759 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18760 { 18761 struct opthdr *optp; 18762 mblk_t *mp2ctl; 18763 18764 /* 18765 * make a copy of the original message 18766 */ 18767 mp2ctl = copymsg(mpctl); 18768 18769 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18770 optp->level = EXPER_DVMRP; 18771 optp->name = 0; 18772 if (!ip_mroute_stats(mpctl->b_cont, ipst)) { 18773 ip0dbg(("ip_mroute_stats: failed\n")); 18774 } 18775 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18776 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 18777 (int)optp->level, (int)optp->name, (int)optp->len)); 18778 qreply(q, mpctl); 18779 return (mp2ctl); 18780 } 18781 18782 /* IPv4 address information */ 18783 static mblk_t * 18784 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18785 { 18786 struct opthdr *optp; 18787 mblk_t *mp2ctl; 18788 mblk_t *mp_tail = NULL; 18789 ill_t *ill; 18790 ipif_t *ipif; 18791 uint_t bitval; 18792 mib2_ipAddrEntry_t mae; 18793 zoneid_t zoneid; 18794 ill_walk_context_t ctx; 18795 18796 /* 18797 * make a copy of the original message 18798 */ 18799 mp2ctl = copymsg(mpctl); 18800 18801 /* ipAddrEntryTable */ 18802 18803 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18804 optp->level = MIB2_IP; 18805 optp->name = MIB2_IP_ADDR; 18806 zoneid = Q_TO_CONN(q)->conn_zoneid; 18807 18808 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18809 ill = ILL_START_WALK_V4(&ctx, ipst); 18810 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18811 for (ipif = ill->ill_ipif; ipif != NULL; 18812 ipif = ipif->ipif_next) { 18813 if (ipif->ipif_zoneid != zoneid && 18814 ipif->ipif_zoneid != ALL_ZONES) 18815 continue; 18816 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18817 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18818 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18819 18820 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes, 18821 OCTET_LENGTH); 18822 mae.ipAdEntIfIndex.o_length = 18823 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 18824 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 18825 mae.ipAdEntNetMask = ipif->ipif_net_mask; 18826 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 18827 mae.ipAdEntInfo.ae_subnet_len = 18828 ip_mask_to_plen(ipif->ipif_net_mask); 18829 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 18830 for (bitval = 1; 18831 bitval && 18832 !(bitval & ipif->ipif_brd_addr); 18833 bitval <<= 1) 18834 noop; 18835 mae.ipAdEntBcastAddr = bitval; 18836 mae.ipAdEntReasmMaxSize = IP_MAXPACKET; 18837 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 18838 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 18839 mae.ipAdEntInfo.ae_broadcast_addr = 18840 ipif->ipif_brd_addr; 18841 mae.ipAdEntInfo.ae_pp_dst_addr = 18842 ipif->ipif_pp_dst_addr; 18843 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 18844 ill->ill_flags | ill->ill_phyint->phyint_flags; 18845 mae.ipAdEntRetransmitTime = AR_EQ_DEFAULT_XMIT_INTERVAL; 18846 18847 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18848 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 18849 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 18850 "allocate %u bytes\n", 18851 (uint_t)sizeof (mib2_ipAddrEntry_t))); 18852 } 18853 } 18854 } 18855 rw_exit(&ipst->ips_ill_g_lock); 18856 18857 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18858 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 18859 (int)optp->level, (int)optp->name, (int)optp->len)); 18860 qreply(q, mpctl); 18861 return (mp2ctl); 18862 } 18863 18864 /* IPv6 address information */ 18865 static mblk_t * 18866 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18867 { 18868 struct opthdr *optp; 18869 mblk_t *mp2ctl; 18870 mblk_t *mp_tail = NULL; 18871 ill_t *ill; 18872 ipif_t *ipif; 18873 mib2_ipv6AddrEntry_t mae6; 18874 zoneid_t zoneid; 18875 ill_walk_context_t ctx; 18876 18877 /* 18878 * make a copy of the original message 18879 */ 18880 mp2ctl = copymsg(mpctl); 18881 18882 /* ipv6AddrEntryTable */ 18883 18884 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18885 optp->level = MIB2_IP6; 18886 optp->name = MIB2_IP6_ADDR; 18887 zoneid = Q_TO_CONN(q)->conn_zoneid; 18888 18889 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18890 ill = ILL_START_WALK_V6(&ctx, ipst); 18891 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18892 for (ipif = ill->ill_ipif; ipif != NULL; 18893 ipif = ipif->ipif_next) { 18894 if (ipif->ipif_zoneid != zoneid && 18895 ipif->ipif_zoneid != ALL_ZONES) 18896 continue; 18897 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18898 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18899 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18900 18901 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes, 18902 OCTET_LENGTH); 18903 mae6.ipv6AddrIfIndex.o_length = 18904 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 18905 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 18906 mae6.ipv6AddrPfxLength = 18907 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 18908 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 18909 mae6.ipv6AddrInfo.ae_subnet_len = 18910 mae6.ipv6AddrPfxLength; 18911 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 18912 18913 /* Type: stateless(1), stateful(2), unknown(3) */ 18914 if (ipif->ipif_flags & IPIF_ADDRCONF) 18915 mae6.ipv6AddrType = 1; 18916 else 18917 mae6.ipv6AddrType = 2; 18918 /* Anycast: true(1), false(2) */ 18919 if (ipif->ipif_flags & IPIF_ANYCAST) 18920 mae6.ipv6AddrAnycastFlag = 1; 18921 else 18922 mae6.ipv6AddrAnycastFlag = 2; 18923 18924 /* 18925 * Address status: preferred(1), deprecated(2), 18926 * invalid(3), inaccessible(4), unknown(5) 18927 */ 18928 if (ipif->ipif_flags & IPIF_NOLOCAL) 18929 mae6.ipv6AddrStatus = 3; 18930 else if (ipif->ipif_flags & IPIF_DEPRECATED) 18931 mae6.ipv6AddrStatus = 2; 18932 else 18933 mae6.ipv6AddrStatus = 1; 18934 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 18935 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 18936 mae6.ipv6AddrInfo.ae_pp_dst_addr = 18937 ipif->ipif_v6pp_dst_addr; 18938 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 18939 ill->ill_flags | ill->ill_phyint->phyint_flags; 18940 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET; 18941 mae6.ipv6AddrIdentifier = ill->ill_token; 18942 mae6.ipv6AddrIdentifierLen = ill->ill_token_length; 18943 mae6.ipv6AddrReachableTime = ill->ill_reachable_time; 18944 mae6.ipv6AddrRetransmitTime = 18945 ill->ill_reachable_retrans_time; 18946 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18947 (char *)&mae6, 18948 (int)sizeof (mib2_ipv6AddrEntry_t))) { 18949 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 18950 "allocate %u bytes\n", 18951 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 18952 } 18953 } 18954 } 18955 rw_exit(&ipst->ips_ill_g_lock); 18956 18957 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18958 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 18959 (int)optp->level, (int)optp->name, (int)optp->len)); 18960 qreply(q, mpctl); 18961 return (mp2ctl); 18962 } 18963 18964 /* IPv4 multicast group membership. */ 18965 static mblk_t * 18966 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 18967 { 18968 struct opthdr *optp; 18969 mblk_t *mp2ctl; 18970 ill_t *ill; 18971 ipif_t *ipif; 18972 ilm_t *ilm; 18973 ip_member_t ipm; 18974 mblk_t *mp_tail = NULL; 18975 ill_walk_context_t ctx; 18976 zoneid_t zoneid; 18977 ilm_walker_t ilw; 18978 18979 /* 18980 * make a copy of the original message 18981 */ 18982 mp2ctl = copymsg(mpctl); 18983 zoneid = Q_TO_CONN(q)->conn_zoneid; 18984 18985 /* ipGroupMember table */ 18986 optp = (struct opthdr *)&mpctl->b_rptr[ 18987 sizeof (struct T_optmgmt_ack)]; 18988 optp->level = MIB2_IP; 18989 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 18990 18991 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 18992 ill = ILL_START_WALK_V4(&ctx, ipst); 18993 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18994 if (IS_UNDER_IPMP(ill)) 18995 continue; 18996 18997 ilm = ilm_walker_start(&ilw, ill); 18998 for (ipif = ill->ill_ipif; ipif != NULL; 18999 ipif = ipif->ipif_next) { 19000 if (ipif->ipif_zoneid != zoneid && 19001 ipif->ipif_zoneid != ALL_ZONES) 19002 continue; /* not this zone */ 19003 ipif_get_name(ipif, ipm.ipGroupMemberIfIndex.o_bytes, 19004 OCTET_LENGTH); 19005 ipm.ipGroupMemberIfIndex.o_length = 19006 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 19007 for (; ilm != NULL; ilm = ilm_walker_step(&ilw, ilm)) { 19008 ASSERT(ilm->ilm_ipif != NULL); 19009 ASSERT(ilm->ilm_ill == NULL); 19010 if (ilm->ilm_ipif != ipif) 19011 continue; 19012 ipm.ipGroupMemberAddress = ilm->ilm_addr; 19013 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 19014 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 19015 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19016 (char *)&ipm, (int)sizeof (ipm))) { 19017 ip1dbg(("ip_snmp_get_mib2_ip_group: " 19018 "failed to allocate %u bytes\n", 19019 (uint_t)sizeof (ipm))); 19020 } 19021 } 19022 } 19023 ilm_walker_finish(&ilw); 19024 } 19025 rw_exit(&ipst->ips_ill_g_lock); 19026 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19027 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 19028 (int)optp->level, (int)optp->name, (int)optp->len)); 19029 qreply(q, mpctl); 19030 return (mp2ctl); 19031 } 19032 19033 /* IPv6 multicast group membership. */ 19034 static mblk_t * 19035 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19036 { 19037 struct opthdr *optp; 19038 mblk_t *mp2ctl; 19039 ill_t *ill; 19040 ilm_t *ilm; 19041 ipv6_member_t ipm6; 19042 mblk_t *mp_tail = NULL; 19043 ill_walk_context_t ctx; 19044 zoneid_t zoneid; 19045 ilm_walker_t ilw; 19046 19047 /* 19048 * make a copy of the original message 19049 */ 19050 mp2ctl = copymsg(mpctl); 19051 zoneid = Q_TO_CONN(q)->conn_zoneid; 19052 19053 /* ip6GroupMember table */ 19054 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19055 optp->level = MIB2_IP6; 19056 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 19057 19058 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19059 ill = ILL_START_WALK_V6(&ctx, ipst); 19060 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19061 if (IS_UNDER_IPMP(ill)) 19062 continue; 19063 19064 ilm = ilm_walker_start(&ilw, ill); 19065 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 19066 for (; ilm != NULL; ilm = ilm_walker_step(&ilw, ilm)) { 19067 ASSERT(ilm->ilm_ipif == NULL); 19068 ASSERT(ilm->ilm_ill != NULL); 19069 if (ilm->ilm_zoneid != zoneid) 19070 continue; /* not this zone */ 19071 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 19072 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 19073 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 19074 if (!snmp_append_data2(mpctl->b_cont, 19075 &mp_tail, 19076 (char *)&ipm6, (int)sizeof (ipm6))) { 19077 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 19078 "failed to allocate %u bytes\n", 19079 (uint_t)sizeof (ipm6))); 19080 } 19081 } 19082 ilm_walker_finish(&ilw); 19083 } 19084 rw_exit(&ipst->ips_ill_g_lock); 19085 19086 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19087 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 19088 (int)optp->level, (int)optp->name, (int)optp->len)); 19089 qreply(q, mpctl); 19090 return (mp2ctl); 19091 } 19092 19093 /* IP multicast filtered sources */ 19094 static mblk_t * 19095 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19096 { 19097 struct opthdr *optp; 19098 mblk_t *mp2ctl; 19099 ill_t *ill; 19100 ipif_t *ipif; 19101 ilm_t *ilm; 19102 ip_grpsrc_t ips; 19103 mblk_t *mp_tail = NULL; 19104 ill_walk_context_t ctx; 19105 zoneid_t zoneid; 19106 int i; 19107 slist_t *sl; 19108 ilm_walker_t ilw; 19109 19110 /* 19111 * make a copy of the original message 19112 */ 19113 mp2ctl = copymsg(mpctl); 19114 zoneid = Q_TO_CONN(q)->conn_zoneid; 19115 19116 /* ipGroupSource table */ 19117 optp = (struct opthdr *)&mpctl->b_rptr[ 19118 sizeof (struct T_optmgmt_ack)]; 19119 optp->level = MIB2_IP; 19120 optp->name = EXPER_IP_GROUP_SOURCES; 19121 19122 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19123 ill = ILL_START_WALK_V4(&ctx, ipst); 19124 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19125 if (IS_UNDER_IPMP(ill)) 19126 continue; 19127 19128 ilm = ilm_walker_start(&ilw, ill); 19129 for (ipif = ill->ill_ipif; ipif != NULL; 19130 ipif = ipif->ipif_next) { 19131 if (ipif->ipif_zoneid != zoneid) 19132 continue; /* not this zone */ 19133 ipif_get_name(ipif, ips.ipGroupSourceIfIndex.o_bytes, 19134 OCTET_LENGTH); 19135 ips.ipGroupSourceIfIndex.o_length = 19136 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 19137 for (; ilm != NULL; ilm = ilm_walker_step(&ilw, ilm)) { 19138 ASSERT(ilm->ilm_ipif != NULL); 19139 ASSERT(ilm->ilm_ill == NULL); 19140 sl = ilm->ilm_filter; 19141 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 19142 continue; 19143 ips.ipGroupSourceGroup = ilm->ilm_addr; 19144 for (i = 0; i < sl->sl_numsrc; i++) { 19145 if (!IN6_IS_ADDR_V4MAPPED( 19146 &sl->sl_addr[i])) 19147 continue; 19148 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 19149 ips.ipGroupSourceAddress); 19150 if (snmp_append_data2(mpctl->b_cont, 19151 &mp_tail, (char *)&ips, 19152 (int)sizeof (ips)) == 0) { 19153 ip1dbg(("ip_snmp_get_mib2_" 19154 "ip_group_src: failed to " 19155 "allocate %u bytes\n", 19156 (uint_t)sizeof (ips))); 19157 } 19158 } 19159 } 19160 } 19161 ilm_walker_finish(&ilw); 19162 } 19163 rw_exit(&ipst->ips_ill_g_lock); 19164 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19165 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 19166 (int)optp->level, (int)optp->name, (int)optp->len)); 19167 qreply(q, mpctl); 19168 return (mp2ctl); 19169 } 19170 19171 /* IPv6 multicast filtered sources. */ 19172 static mblk_t * 19173 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19174 { 19175 struct opthdr *optp; 19176 mblk_t *mp2ctl; 19177 ill_t *ill; 19178 ilm_t *ilm; 19179 ipv6_grpsrc_t ips6; 19180 mblk_t *mp_tail = NULL; 19181 ill_walk_context_t ctx; 19182 zoneid_t zoneid; 19183 int i; 19184 slist_t *sl; 19185 ilm_walker_t ilw; 19186 19187 /* 19188 * make a copy of the original message 19189 */ 19190 mp2ctl = copymsg(mpctl); 19191 zoneid = Q_TO_CONN(q)->conn_zoneid; 19192 19193 /* ip6GroupMember table */ 19194 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19195 optp->level = MIB2_IP6; 19196 optp->name = EXPER_IP6_GROUP_SOURCES; 19197 19198 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19199 ill = ILL_START_WALK_V6(&ctx, ipst); 19200 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19201 if (IS_UNDER_IPMP(ill)) 19202 continue; 19203 19204 ilm = ilm_walker_start(&ilw, ill); 19205 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 19206 for (; ilm != NULL; ilm = ilm_walker_step(&ilw, ilm)) { 19207 ASSERT(ilm->ilm_ipif == NULL); 19208 ASSERT(ilm->ilm_ill != NULL); 19209 sl = ilm->ilm_filter; 19210 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 19211 continue; 19212 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 19213 for (i = 0; i < sl->sl_numsrc; i++) { 19214 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 19215 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19216 (char *)&ips6, (int)sizeof (ips6))) { 19217 ip1dbg(("ip_snmp_get_mib2_ip6_" 19218 "group_src: failed to allocate " 19219 "%u bytes\n", 19220 (uint_t)sizeof (ips6))); 19221 } 19222 } 19223 } 19224 ilm_walker_finish(&ilw); 19225 } 19226 rw_exit(&ipst->ips_ill_g_lock); 19227 19228 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19229 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 19230 (int)optp->level, (int)optp->name, (int)optp->len)); 19231 qreply(q, mpctl); 19232 return (mp2ctl); 19233 } 19234 19235 /* Multicast routing virtual interface table. */ 19236 static mblk_t * 19237 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19238 { 19239 struct opthdr *optp; 19240 mblk_t *mp2ctl; 19241 19242 /* 19243 * make a copy of the original message 19244 */ 19245 mp2ctl = copymsg(mpctl); 19246 19247 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19248 optp->level = EXPER_DVMRP; 19249 optp->name = EXPER_DVMRP_VIF; 19250 if (!ip_mroute_vif(mpctl->b_cont, ipst)) { 19251 ip0dbg(("ip_mroute_vif: failed\n")); 19252 } 19253 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19254 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 19255 (int)optp->level, (int)optp->name, (int)optp->len)); 19256 qreply(q, mpctl); 19257 return (mp2ctl); 19258 } 19259 19260 /* Multicast routing table. */ 19261 static mblk_t * 19262 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19263 { 19264 struct opthdr *optp; 19265 mblk_t *mp2ctl; 19266 19267 /* 19268 * make a copy of the original message 19269 */ 19270 mp2ctl = copymsg(mpctl); 19271 19272 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19273 optp->level = EXPER_DVMRP; 19274 optp->name = EXPER_DVMRP_MRT; 19275 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) { 19276 ip0dbg(("ip_mroute_mrt: failed\n")); 19277 } 19278 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19279 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 19280 (int)optp->level, (int)optp->name, (int)optp->len)); 19281 qreply(q, mpctl); 19282 return (mp2ctl); 19283 } 19284 19285 /* 19286 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 19287 * in one IRE walk. 19288 */ 19289 static mblk_t * 19290 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level, 19291 ip_stack_t *ipst) 19292 { 19293 struct opthdr *optp; 19294 mblk_t *mp2ctl; /* Returned */ 19295 mblk_t *mp3ctl; /* nettomedia */ 19296 mblk_t *mp4ctl; /* routeattrs */ 19297 iproutedata_t ird; 19298 zoneid_t zoneid; 19299 19300 /* 19301 * make copies of the original message 19302 * - mp2ctl is returned unchanged to the caller for his use 19303 * - mpctl is sent upstream as ipRouteEntryTable 19304 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 19305 * - mp4ctl is sent upstream as ipRouteAttributeTable 19306 */ 19307 mp2ctl = copymsg(mpctl); 19308 mp3ctl = copymsg(mpctl); 19309 mp4ctl = copymsg(mpctl); 19310 if (mp3ctl == NULL || mp4ctl == NULL) { 19311 freemsg(mp4ctl); 19312 freemsg(mp3ctl); 19313 freemsg(mp2ctl); 19314 freemsg(mpctl); 19315 return (NULL); 19316 } 19317 19318 bzero(&ird, sizeof (ird)); 19319 19320 ird.ird_route.lp_head = mpctl->b_cont; 19321 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 19322 ird.ird_attrs.lp_head = mp4ctl->b_cont; 19323 /* 19324 * If the level has been set the special EXPER_IP_AND_TESTHIDDEN 19325 * value, then also include IRE_MARK_TESTHIDDEN IREs. This is 19326 * intended a temporary solution until a proper MIB API is provided 19327 * that provides complete filtering/caller-opt-in. 19328 */ 19329 if (level == EXPER_IP_AND_TESTHIDDEN) 19330 ird.ird_flags |= IRD_REPORT_TESTHIDDEN; 19331 19332 zoneid = Q_TO_CONN(q)->conn_zoneid; 19333 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst); 19334 19335 /* ipRouteEntryTable in mpctl */ 19336 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19337 optp->level = MIB2_IP; 19338 optp->name = MIB2_IP_ROUTE; 19339 optp->len = msgdsize(ird.ird_route.lp_head); 19340 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19341 (int)optp->level, (int)optp->name, (int)optp->len)); 19342 qreply(q, mpctl); 19343 19344 /* ipNetToMediaEntryTable in mp3ctl */ 19345 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19346 optp->level = MIB2_IP; 19347 optp->name = MIB2_IP_MEDIA; 19348 optp->len = msgdsize(ird.ird_netmedia.lp_head); 19349 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19350 (int)optp->level, (int)optp->name, (int)optp->len)); 19351 qreply(q, mp3ctl); 19352 19353 /* ipRouteAttributeTable in mp4ctl */ 19354 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19355 optp->level = MIB2_IP; 19356 optp->name = EXPER_IP_RTATTR; 19357 optp->len = msgdsize(ird.ird_attrs.lp_head); 19358 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 19359 (int)optp->level, (int)optp->name, (int)optp->len)); 19360 if (optp->len == 0) 19361 freemsg(mp4ctl); 19362 else 19363 qreply(q, mp4ctl); 19364 19365 return (mp2ctl); 19366 } 19367 19368 /* 19369 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 19370 * ipv6NetToMediaEntryTable in an NDP walk. 19371 */ 19372 static mblk_t * 19373 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level, 19374 ip_stack_t *ipst) 19375 { 19376 struct opthdr *optp; 19377 mblk_t *mp2ctl; /* Returned */ 19378 mblk_t *mp3ctl; /* nettomedia */ 19379 mblk_t *mp4ctl; /* routeattrs */ 19380 iproutedata_t ird; 19381 zoneid_t zoneid; 19382 19383 /* 19384 * make copies of the original message 19385 * - mp2ctl is returned unchanged to the caller for his use 19386 * - mpctl is sent upstream as ipv6RouteEntryTable 19387 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 19388 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 19389 */ 19390 mp2ctl = copymsg(mpctl); 19391 mp3ctl = copymsg(mpctl); 19392 mp4ctl = copymsg(mpctl); 19393 if (mp3ctl == NULL || mp4ctl == NULL) { 19394 freemsg(mp4ctl); 19395 freemsg(mp3ctl); 19396 freemsg(mp2ctl); 19397 freemsg(mpctl); 19398 return (NULL); 19399 } 19400 19401 bzero(&ird, sizeof (ird)); 19402 19403 ird.ird_route.lp_head = mpctl->b_cont; 19404 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 19405 ird.ird_attrs.lp_head = mp4ctl->b_cont; 19406 /* 19407 * If the level has been set the special EXPER_IP_AND_TESTHIDDEN 19408 * value, then also include IRE_MARK_TESTHIDDEN IREs. This is 19409 * intended a temporary solution until a proper MIB API is provided 19410 * that provides complete filtering/caller-opt-in. 19411 */ 19412 if (level == EXPER_IP_AND_TESTHIDDEN) 19413 ird.ird_flags |= IRD_REPORT_TESTHIDDEN; 19414 19415 zoneid = Q_TO_CONN(q)->conn_zoneid; 19416 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst); 19417 19418 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19419 optp->level = MIB2_IP6; 19420 optp->name = MIB2_IP6_ROUTE; 19421 optp->len = msgdsize(ird.ird_route.lp_head); 19422 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19423 (int)optp->level, (int)optp->name, (int)optp->len)); 19424 qreply(q, mpctl); 19425 19426 /* ipv6NetToMediaEntryTable in mp3ctl */ 19427 ndp_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst); 19428 19429 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19430 optp->level = MIB2_IP6; 19431 optp->name = MIB2_IP6_MEDIA; 19432 optp->len = msgdsize(ird.ird_netmedia.lp_head); 19433 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19434 (int)optp->level, (int)optp->name, (int)optp->len)); 19435 qreply(q, mp3ctl); 19436 19437 /* ipv6RouteAttributeTable in mp4ctl */ 19438 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19439 optp->level = MIB2_IP6; 19440 optp->name = EXPER_IP_RTATTR; 19441 optp->len = msgdsize(ird.ird_attrs.lp_head); 19442 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 19443 (int)optp->level, (int)optp->name, (int)optp->len)); 19444 if (optp->len == 0) 19445 freemsg(mp4ctl); 19446 else 19447 qreply(q, mp4ctl); 19448 19449 return (mp2ctl); 19450 } 19451 19452 /* 19453 * IPv6 mib: One per ill 19454 */ 19455 static mblk_t * 19456 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19457 { 19458 struct opthdr *optp; 19459 mblk_t *mp2ctl; 19460 ill_t *ill; 19461 ill_walk_context_t ctx; 19462 mblk_t *mp_tail = NULL; 19463 19464 /* 19465 * Make a copy of the original message 19466 */ 19467 mp2ctl = copymsg(mpctl); 19468 19469 /* fixed length IPv6 structure ... */ 19470 19471 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19472 optp->level = MIB2_IP6; 19473 optp->name = 0; 19474 /* Include "unknown interface" ip6_mib */ 19475 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 19476 ipst->ips_ip6_mib.ipIfStatsIfIndex = 19477 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 19478 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding, 19479 ipst->ips_ipv6_forward ? 1 : 2); 19480 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit, 19481 ipst->ips_ipv6_def_hops); 19482 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize, 19483 sizeof (mib2_ipIfStatsEntry_t)); 19484 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize, 19485 sizeof (mib2_ipv6AddrEntry_t)); 19486 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize, 19487 sizeof (mib2_ipv6RouteEntry_t)); 19488 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize, 19489 sizeof (mib2_ipv6NetToMediaEntry_t)); 19490 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize, 19491 sizeof (ipv6_member_t)); 19492 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize, 19493 sizeof (ipv6_grpsrc_t)); 19494 19495 /* 19496 * Synchronize 64- and 32-bit counters 19497 */ 19498 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives, 19499 ipIfStatsHCInReceives); 19500 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers, 19501 ipIfStatsHCInDelivers); 19502 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests, 19503 ipIfStatsHCOutRequests); 19504 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams, 19505 ipIfStatsHCOutForwDatagrams); 19506 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts, 19507 ipIfStatsHCOutMcastPkts); 19508 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts, 19509 ipIfStatsHCInMcastPkts); 19510 19511 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19512 (char *)&ipst->ips_ip6_mib, (int)sizeof (ipst->ips_ip6_mib))) { 19513 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 19514 (uint_t)sizeof (ipst->ips_ip6_mib))); 19515 } 19516 19517 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19518 ill = ILL_START_WALK_V6(&ctx, ipst); 19519 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19520 ill->ill_ip_mib->ipIfStatsIfIndex = 19521 ill->ill_phyint->phyint_ifindex; 19522 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 19523 ipst->ips_ipv6_forward ? 1 : 2); 19524 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit, 19525 ill->ill_max_hops); 19526 19527 /* 19528 * Synchronize 64- and 32-bit counters 19529 */ 19530 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives, 19531 ipIfStatsHCInReceives); 19532 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers, 19533 ipIfStatsHCInDelivers); 19534 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests, 19535 ipIfStatsHCOutRequests); 19536 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams, 19537 ipIfStatsHCOutForwDatagrams); 19538 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts, 19539 ipIfStatsHCOutMcastPkts); 19540 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts, 19541 ipIfStatsHCInMcastPkts); 19542 19543 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19544 (char *)ill->ill_ip_mib, 19545 (int)sizeof (*ill->ill_ip_mib))) { 19546 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 19547 "%u bytes\n", (uint_t)sizeof (*ill->ill_ip_mib))); 19548 } 19549 } 19550 rw_exit(&ipst->ips_ill_g_lock); 19551 19552 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19553 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 19554 (int)optp->level, (int)optp->name, (int)optp->len)); 19555 qreply(q, mpctl); 19556 return (mp2ctl); 19557 } 19558 19559 /* 19560 * ICMPv6 mib: One per ill 19561 */ 19562 static mblk_t * 19563 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 19564 { 19565 struct opthdr *optp; 19566 mblk_t *mp2ctl; 19567 ill_t *ill; 19568 ill_walk_context_t ctx; 19569 mblk_t *mp_tail = NULL; 19570 /* 19571 * Make a copy of the original message 19572 */ 19573 mp2ctl = copymsg(mpctl); 19574 19575 /* fixed length ICMPv6 structure ... */ 19576 19577 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 19578 optp->level = MIB2_ICMP6; 19579 optp->name = 0; 19580 /* Include "unknown interface" icmp6_mib */ 19581 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex = 19582 MIB2_UNKNOWN_INTERFACE; /* netstat flag */ 19583 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize = 19584 sizeof (mib2_ipv6IfIcmpEntry_t); 19585 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19586 (char *)&ipst->ips_icmp6_mib, 19587 (int)sizeof (ipst->ips_icmp6_mib))) { 19588 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 19589 (uint_t)sizeof (ipst->ips_icmp6_mib))); 19590 } 19591 19592 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 19593 ill = ILL_START_WALK_V6(&ctx, ipst); 19594 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19595 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 19596 ill->ill_phyint->phyint_ifindex; 19597 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19598 (char *)ill->ill_icmp6_mib, 19599 (int)sizeof (*ill->ill_icmp6_mib))) { 19600 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 19601 "%u bytes\n", 19602 (uint_t)sizeof (*ill->ill_icmp6_mib))); 19603 } 19604 } 19605 rw_exit(&ipst->ips_ill_g_lock); 19606 19607 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19608 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 19609 (int)optp->level, (int)optp->name, (int)optp->len)); 19610 qreply(q, mpctl); 19611 return (mp2ctl); 19612 } 19613 19614 /* 19615 * ire_walk routine to create both ipRouteEntryTable and 19616 * ipRouteAttributeTable in one IRE walk 19617 */ 19618 static void 19619 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 19620 { 19621 ill_t *ill; 19622 ipif_t *ipif; 19623 mib2_ipRouteEntry_t *re; 19624 mib2_ipAttributeEntry_t *iae, *iaeptr; 19625 ipaddr_t gw_addr; 19626 tsol_ire_gw_secattr_t *attrp; 19627 tsol_gc_t *gc = NULL; 19628 tsol_gcgrp_t *gcgrp = NULL; 19629 uint_t sacnt = 0; 19630 int i; 19631 19632 ASSERT(ire->ire_ipversion == IPV4_VERSION); 19633 19634 if (!(ird->ird_flags & IRD_REPORT_TESTHIDDEN) && 19635 ire->ire_marks & IRE_MARK_TESTHIDDEN) { 19636 return; 19637 } 19638 19639 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19640 return; 19641 19642 if ((attrp = ire->ire_gw_secattr) != NULL) { 19643 mutex_enter(&attrp->igsa_lock); 19644 if ((gc = attrp->igsa_gc) != NULL) { 19645 gcgrp = gc->gc_grp; 19646 ASSERT(gcgrp != NULL); 19647 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19648 sacnt = 1; 19649 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19650 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19651 gc = gcgrp->gcgrp_head; 19652 sacnt = gcgrp->gcgrp_count; 19653 } 19654 mutex_exit(&attrp->igsa_lock); 19655 19656 /* do nothing if there's no gc to report */ 19657 if (gc == NULL) { 19658 ASSERT(sacnt == 0); 19659 if (gcgrp != NULL) { 19660 /* we might as well drop the lock now */ 19661 rw_exit(&gcgrp->gcgrp_rwlock); 19662 gcgrp = NULL; 19663 } 19664 attrp = NULL; 19665 } 19666 19667 ASSERT(gc == NULL || (gcgrp != NULL && 19668 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19669 } 19670 ASSERT(sacnt == 0 || gc != NULL); 19671 19672 if (sacnt != 0 && 19673 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19674 kmem_free(re, sizeof (*re)); 19675 rw_exit(&gcgrp->gcgrp_rwlock); 19676 return; 19677 } 19678 19679 /* 19680 * Return all IRE types for route table... let caller pick and choose 19681 */ 19682 re->ipRouteDest = ire->ire_addr; 19683 ipif = ire->ire_ipif; 19684 re->ipRouteIfIndex.o_length = 0; 19685 if (ire->ire_type == IRE_CACHE) { 19686 ill = (ill_t *)ire->ire_stq->q_ptr; 19687 re->ipRouteIfIndex.o_length = 19688 ill->ill_name_length == 0 ? 0 : 19689 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19690 bcopy(ill->ill_name, re->ipRouteIfIndex.o_bytes, 19691 re->ipRouteIfIndex.o_length); 19692 } else if (ipif != NULL) { 19693 ipif_get_name(ipif, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH); 19694 re->ipRouteIfIndex.o_length = 19695 mi_strlen(re->ipRouteIfIndex.o_bytes); 19696 } 19697 re->ipRouteMetric1 = -1; 19698 re->ipRouteMetric2 = -1; 19699 re->ipRouteMetric3 = -1; 19700 re->ipRouteMetric4 = -1; 19701 19702 gw_addr = ire->ire_gateway_addr; 19703 19704 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 19705 re->ipRouteNextHop = ire->ire_src_addr; 19706 else 19707 re->ipRouteNextHop = gw_addr; 19708 /* indirect(4), direct(3), or invalid(2) */ 19709 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19710 re->ipRouteType = 2; 19711 else 19712 re->ipRouteType = (gw_addr != 0) ? 4 : 3; 19713 re->ipRouteProto = -1; 19714 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 19715 re->ipRouteMask = ire->ire_mask; 19716 re->ipRouteMetric5 = -1; 19717 re->ipRouteInfo.re_max_frag = ire->ire_max_frag; 19718 re->ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 19719 re->ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19720 re->ipRouteInfo.re_ref = ire->ire_refcnt; 19721 re->ipRouteInfo.re_src_addr = ire->ire_src_addr; 19722 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19723 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19724 re->ipRouteInfo.re_flags = ire->ire_flags; 19725 19726 if (ire->ire_flags & RTF_DYNAMIC) { 19727 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19728 } else { 19729 re->ipRouteInfo.re_ire_type = ire->ire_type; 19730 } 19731 19732 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19733 (char *)re, (int)sizeof (*re))) { 19734 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19735 (uint_t)sizeof (*re))); 19736 } 19737 19738 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19739 iaeptr->iae_routeidx = ird->ird_idx; 19740 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19741 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19742 } 19743 19744 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19745 (char *)iae, sacnt * sizeof (*iae))) { 19746 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19747 (unsigned)(sacnt * sizeof (*iae)))); 19748 } 19749 19750 /* bump route index for next pass */ 19751 ird->ird_idx++; 19752 19753 kmem_free(re, sizeof (*re)); 19754 if (sacnt != 0) 19755 kmem_free(iae, sacnt * sizeof (*iae)); 19756 19757 if (gcgrp != NULL) 19758 rw_exit(&gcgrp->gcgrp_rwlock); 19759 } 19760 19761 /* 19762 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 19763 */ 19764 static void 19765 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 19766 { 19767 ill_t *ill; 19768 ipif_t *ipif; 19769 mib2_ipv6RouteEntry_t *re; 19770 mib2_ipAttributeEntry_t *iae, *iaeptr; 19771 in6_addr_t gw_addr_v6; 19772 tsol_ire_gw_secattr_t *attrp; 19773 tsol_gc_t *gc = NULL; 19774 tsol_gcgrp_t *gcgrp = NULL; 19775 uint_t sacnt = 0; 19776 int i; 19777 19778 ASSERT(ire->ire_ipversion == IPV6_VERSION); 19779 19780 if (!(ird->ird_flags & IRD_REPORT_TESTHIDDEN) && 19781 ire->ire_marks & IRE_MARK_TESTHIDDEN) { 19782 return; 19783 } 19784 19785 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19786 return; 19787 19788 if ((attrp = ire->ire_gw_secattr) != NULL) { 19789 mutex_enter(&attrp->igsa_lock); 19790 if ((gc = attrp->igsa_gc) != NULL) { 19791 gcgrp = gc->gc_grp; 19792 ASSERT(gcgrp != NULL); 19793 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19794 sacnt = 1; 19795 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19796 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19797 gc = gcgrp->gcgrp_head; 19798 sacnt = gcgrp->gcgrp_count; 19799 } 19800 mutex_exit(&attrp->igsa_lock); 19801 19802 /* do nothing if there's no gc to report */ 19803 if (gc == NULL) { 19804 ASSERT(sacnt == 0); 19805 if (gcgrp != NULL) { 19806 /* we might as well drop the lock now */ 19807 rw_exit(&gcgrp->gcgrp_rwlock); 19808 gcgrp = NULL; 19809 } 19810 attrp = NULL; 19811 } 19812 19813 ASSERT(gc == NULL || (gcgrp != NULL && 19814 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19815 } 19816 ASSERT(sacnt == 0 || gc != NULL); 19817 19818 if (sacnt != 0 && 19819 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19820 kmem_free(re, sizeof (*re)); 19821 rw_exit(&gcgrp->gcgrp_rwlock); 19822 return; 19823 } 19824 19825 /* 19826 * Return all IRE types for route table... let caller pick and choose 19827 */ 19828 re->ipv6RouteDest = ire->ire_addr_v6; 19829 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 19830 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 19831 re->ipv6RouteIfIndex.o_length = 0; 19832 ipif = ire->ire_ipif; 19833 if (ire->ire_type == IRE_CACHE) { 19834 ill = (ill_t *)ire->ire_stq->q_ptr; 19835 re->ipv6RouteIfIndex.o_length = 19836 ill->ill_name_length == 0 ? 0 : 19837 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19838 bcopy(ill->ill_name, re->ipv6RouteIfIndex.o_bytes, 19839 re->ipv6RouteIfIndex.o_length); 19840 } else if (ipif != NULL) { 19841 ipif_get_name(ipif, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH); 19842 re->ipv6RouteIfIndex.o_length = 19843 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 19844 } 19845 19846 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19847 19848 mutex_enter(&ire->ire_lock); 19849 gw_addr_v6 = ire->ire_gateway_addr_v6; 19850 mutex_exit(&ire->ire_lock); 19851 19852 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 19853 re->ipv6RouteNextHop = ire->ire_src_addr_v6; 19854 else 19855 re->ipv6RouteNextHop = gw_addr_v6; 19856 19857 /* remote(4), local(3), or discard(2) */ 19858 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19859 re->ipv6RouteType = 2; 19860 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 19861 re->ipv6RouteType = 3; 19862 else 19863 re->ipv6RouteType = 4; 19864 19865 re->ipv6RouteProtocol = -1; 19866 re->ipv6RoutePolicy = 0; 19867 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 19868 re->ipv6RouteNextHopRDI = 0; 19869 re->ipv6RouteWeight = 0; 19870 re->ipv6RouteMetric = 0; 19871 re->ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 19872 re->ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 19873 re->ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19874 re->ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 19875 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19876 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19877 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 19878 re->ipv6RouteInfo.re_flags = ire->ire_flags; 19879 19880 if (ire->ire_flags & RTF_DYNAMIC) { 19881 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19882 } else { 19883 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 19884 } 19885 19886 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19887 (char *)re, (int)sizeof (*re))) { 19888 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19889 (uint_t)sizeof (*re))); 19890 } 19891 19892 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19893 iaeptr->iae_routeidx = ird->ird_idx; 19894 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19895 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19896 } 19897 19898 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19899 (char *)iae, sacnt * sizeof (*iae))) { 19900 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19901 (unsigned)(sacnt * sizeof (*iae)))); 19902 } 19903 19904 /* bump route index for next pass */ 19905 ird->ird_idx++; 19906 19907 kmem_free(re, sizeof (*re)); 19908 if (sacnt != 0) 19909 kmem_free(iae, sacnt * sizeof (*iae)); 19910 19911 if (gcgrp != NULL) 19912 rw_exit(&gcgrp->gcgrp_rwlock); 19913 } 19914 19915 /* 19916 * ndp_walk routine to create ipv6NetToMediaEntryTable 19917 */ 19918 static int 19919 ip_snmp_get2_v6_media(nce_t *nce, iproutedata_t *ird) 19920 { 19921 ill_t *ill; 19922 mib2_ipv6NetToMediaEntry_t ntme; 19923 dl_unitdata_req_t *dl; 19924 19925 ill = nce->nce_ill; 19926 if (ill->ill_isv6 == B_FALSE) /* skip arpce entry */ 19927 return (0); 19928 19929 /* 19930 * Neighbor cache entry attached to IRE with on-link 19931 * destination. 19932 */ 19933 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 19934 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 19935 if ((ill->ill_flags & ILLF_XRESOLV) && 19936 (nce->nce_res_mp != NULL)) { 19937 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 19938 ntme.ipv6NetToMediaPhysAddress.o_length = 19939 dl->dl_dest_addr_length; 19940 } else { 19941 ntme.ipv6NetToMediaPhysAddress.o_length = 19942 ill->ill_phys_addr_length; 19943 } 19944 if (nce->nce_res_mp != NULL) { 19945 bcopy((char *)nce->nce_res_mp->b_rptr + 19946 NCE_LL_ADDR_OFFSET(ill), 19947 ntme.ipv6NetToMediaPhysAddress.o_bytes, 19948 ntme.ipv6NetToMediaPhysAddress.o_length); 19949 } else { 19950 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 19951 ill->ill_phys_addr_length); 19952 } 19953 /* 19954 * Note: Returns ND_* states. Should be: 19955 * reachable(1), stale(2), delay(3), probe(4), 19956 * invalid(5), unknown(6) 19957 */ 19958 ntme.ipv6NetToMediaState = nce->nce_state; 19959 ntme.ipv6NetToMediaLastUpdated = 0; 19960 19961 /* other(1), dynamic(2), static(3), local(4) */ 19962 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 19963 ntme.ipv6NetToMediaType = 4; 19964 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 19965 ntme.ipv6NetToMediaType = 1; 19966 } else { 19967 ntme.ipv6NetToMediaType = 2; 19968 } 19969 19970 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 19971 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 19972 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 19973 (uint_t)sizeof (ntme))); 19974 } 19975 return (0); 19976 } 19977 19978 /* 19979 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 19980 */ 19981 /* ARGSUSED */ 19982 int 19983 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 19984 { 19985 switch (level) { 19986 case MIB2_IP: 19987 case MIB2_ICMP: 19988 switch (name) { 19989 default: 19990 break; 19991 } 19992 return (1); 19993 default: 19994 return (1); 19995 } 19996 } 19997 19998 /* 19999 * When there exists both a 64- and 32-bit counter of a particular type 20000 * (i.e., InReceives), only the 64-bit counters are added. 20001 */ 20002 void 20003 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2) 20004 { 20005 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors); 20006 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors); 20007 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes); 20008 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors); 20009 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos); 20010 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts); 20011 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards); 20012 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards); 20013 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs); 20014 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails); 20015 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates); 20016 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds); 20017 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs); 20018 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails); 20019 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes); 20020 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates); 20021 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups); 20022 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits); 20023 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs); 20024 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows); 20025 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows); 20026 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion, 20027 o2->ipIfStatsInWrongIPVersion); 20028 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion, 20029 o2->ipIfStatsInWrongIPVersion); 20030 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion, 20031 o2->ipIfStatsOutSwitchIPVersion); 20032 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives); 20033 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets); 20034 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams, 20035 o2->ipIfStatsHCInForwDatagrams); 20036 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers); 20037 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests); 20038 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams, 20039 o2->ipIfStatsHCOutForwDatagrams); 20040 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds); 20041 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits); 20042 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets); 20043 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts); 20044 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets); 20045 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts); 20046 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets, 20047 o2->ipIfStatsHCOutMcastOctets); 20048 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts); 20049 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts); 20050 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded); 20051 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed); 20052 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs); 20053 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs); 20054 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts); 20055 } 20056 20057 void 20058 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2) 20059 { 20060 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs); 20061 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors); 20062 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs); 20063 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs); 20064 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds); 20065 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems); 20066 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs); 20067 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos); 20068 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies); 20069 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits, 20070 o2->ipv6IfIcmpInRouterSolicits); 20071 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements, 20072 o2->ipv6IfIcmpInRouterAdvertisements); 20073 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits, 20074 o2->ipv6IfIcmpInNeighborSolicits); 20075 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements, 20076 o2->ipv6IfIcmpInNeighborAdvertisements); 20077 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects); 20078 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries, 20079 o2->ipv6IfIcmpInGroupMembQueries); 20080 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses, 20081 o2->ipv6IfIcmpInGroupMembResponses); 20082 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions, 20083 o2->ipv6IfIcmpInGroupMembReductions); 20084 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs); 20085 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors); 20086 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs, 20087 o2->ipv6IfIcmpOutDestUnreachs); 20088 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs, 20089 o2->ipv6IfIcmpOutAdminProhibs); 20090 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds); 20091 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems, 20092 o2->ipv6IfIcmpOutParmProblems); 20093 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs); 20094 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos); 20095 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies); 20096 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits, 20097 o2->ipv6IfIcmpOutRouterSolicits); 20098 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements, 20099 o2->ipv6IfIcmpOutRouterAdvertisements); 20100 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits, 20101 o2->ipv6IfIcmpOutNeighborSolicits); 20102 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements, 20103 o2->ipv6IfIcmpOutNeighborAdvertisements); 20104 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects); 20105 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries, 20106 o2->ipv6IfIcmpOutGroupMembQueries); 20107 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses, 20108 o2->ipv6IfIcmpOutGroupMembResponses); 20109 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions, 20110 o2->ipv6IfIcmpOutGroupMembReductions); 20111 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows); 20112 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit); 20113 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements, 20114 o2->ipv6IfIcmpInBadNeighborAdvertisements); 20115 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations, 20116 o2->ipv6IfIcmpInBadNeighborSolicitations); 20117 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects); 20118 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal, 20119 o2->ipv6IfIcmpInGroupMembTotal); 20120 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries, 20121 o2->ipv6IfIcmpInGroupMembBadQueries); 20122 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports, 20123 o2->ipv6IfIcmpInGroupMembBadReports); 20124 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports, 20125 o2->ipv6IfIcmpInGroupMembOurReports); 20126 } 20127 20128 /* 20129 * Called before the options are updated to check if this packet will 20130 * be source routed from here. 20131 * This routine assumes that the options are well formed i.e. that they 20132 * have already been checked. 20133 */ 20134 static boolean_t 20135 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst) 20136 { 20137 ipoptp_t opts; 20138 uchar_t *opt; 20139 uint8_t optval; 20140 uint8_t optlen; 20141 ipaddr_t dst; 20142 ire_t *ire; 20143 20144 if (IS_SIMPLE_IPH(ipha)) { 20145 ip2dbg(("not source routed\n")); 20146 return (B_FALSE); 20147 } 20148 dst = ipha->ipha_dst; 20149 for (optval = ipoptp_first(&opts, ipha); 20150 optval != IPOPT_EOL; 20151 optval = ipoptp_next(&opts)) { 20152 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 20153 opt = opts.ipoptp_cur; 20154 optlen = opts.ipoptp_len; 20155 ip2dbg(("ip_source_routed: opt %d, len %d\n", 20156 optval, optlen)); 20157 switch (optval) { 20158 uint32_t off; 20159 case IPOPT_SSRR: 20160 case IPOPT_LSRR: 20161 /* 20162 * If dst is one of our addresses and there are some 20163 * entries left in the source route return (true). 20164 */ 20165 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 20166 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 20167 if (ire == NULL) { 20168 ip2dbg(("ip_source_routed: not next" 20169 " source route 0x%x\n", 20170 ntohl(dst))); 20171 return (B_FALSE); 20172 } 20173 ire_refrele(ire); 20174 off = opt[IPOPT_OFFSET]; 20175 off--; 20176 if (optlen < IP_ADDR_LEN || 20177 off > optlen - IP_ADDR_LEN) { 20178 /* End of source route */ 20179 ip1dbg(("ip_source_routed: end of SR\n")); 20180 return (B_FALSE); 20181 } 20182 return (B_TRUE); 20183 } 20184 } 20185 ip2dbg(("not source routed\n")); 20186 return (B_FALSE); 20187 } 20188 20189 /* 20190 * Check if the packet contains any source route. 20191 */ 20192 static boolean_t 20193 ip_source_route_included(ipha_t *ipha) 20194 { 20195 ipoptp_t opts; 20196 uint8_t optval; 20197 20198 if (IS_SIMPLE_IPH(ipha)) 20199 return (B_FALSE); 20200 for (optval = ipoptp_first(&opts, ipha); 20201 optval != IPOPT_EOL; 20202 optval = ipoptp_next(&opts)) { 20203 switch (optval) { 20204 case IPOPT_SSRR: 20205 case IPOPT_LSRR: 20206 return (B_TRUE); 20207 } 20208 } 20209 return (B_FALSE); 20210 } 20211 20212 /* 20213 * Called when the IRE expiration timer fires. 20214 */ 20215 void 20216 ip_trash_timer_expire(void *args) 20217 { 20218 int flush_flag = 0; 20219 ire_expire_arg_t iea; 20220 ip_stack_t *ipst = (ip_stack_t *)args; 20221 20222 iea.iea_ipst = ipst; /* No netstack_hold */ 20223 20224 /* 20225 * ip_ire_expire_id is protected by ip_trash_timer_lock. 20226 * This lock makes sure that a new invocation of this function 20227 * that occurs due to an almost immediate timer firing will not 20228 * progress beyond this point until the current invocation is done 20229 */ 20230 mutex_enter(&ipst->ips_ip_trash_timer_lock); 20231 ipst->ips_ip_ire_expire_id = 0; 20232 mutex_exit(&ipst->ips_ip_trash_timer_lock); 20233 20234 /* Periodic timer */ 20235 if (ipst->ips_ip_ire_arp_time_elapsed >= 20236 ipst->ips_ip_ire_arp_interval) { 20237 /* 20238 * Remove all IRE_CACHE entries since they might 20239 * contain arp information. 20240 */ 20241 flush_flag |= FLUSH_ARP_TIME; 20242 ipst->ips_ip_ire_arp_time_elapsed = 0; 20243 IP_STAT(ipst, ip_ire_arp_timer_expired); 20244 } 20245 if (ipst->ips_ip_ire_rd_time_elapsed >= 20246 ipst->ips_ip_ire_redir_interval) { 20247 /* Remove all redirects */ 20248 flush_flag |= FLUSH_REDIRECT_TIME; 20249 ipst->ips_ip_ire_rd_time_elapsed = 0; 20250 IP_STAT(ipst, ip_ire_redirect_timer_expired); 20251 } 20252 if (ipst->ips_ip_ire_pmtu_time_elapsed >= 20253 ipst->ips_ip_ire_pathmtu_interval) { 20254 /* Increase path mtu */ 20255 flush_flag |= FLUSH_MTU_TIME; 20256 ipst->ips_ip_ire_pmtu_time_elapsed = 0; 20257 IP_STAT(ipst, ip_ire_pmtu_timer_expired); 20258 } 20259 20260 /* 20261 * Optimize for the case when there are no redirects in the 20262 * ftable, that is, no need to walk the ftable in that case. 20263 */ 20264 if (flush_flag & (FLUSH_MTU_TIME|FLUSH_ARP_TIME)) { 20265 iea.iea_flush_flag = flush_flag; 20266 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_CACHETABLE, ire_expire, 20267 (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 0, NULL, 20268 ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table, 20269 NULL, ALL_ZONES, ipst); 20270 } 20271 if ((flush_flag & FLUSH_REDIRECT_TIME) && 20272 ipst->ips_ip_redirect_cnt > 0) { 20273 iea.iea_flush_flag = flush_flag; 20274 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_FORWARDTABLE, 20275 ire_expire, (char *)(uintptr_t)&iea, IP_MASK_TABLE_SIZE, 20276 0, NULL, 0, NULL, NULL, ALL_ZONES, ipst); 20277 } 20278 if (flush_flag & FLUSH_MTU_TIME) { 20279 /* 20280 * Walk all IPv6 IRE's and update them 20281 * Note that ARP and redirect timers are not 20282 * needed since NUD handles stale entries. 20283 */ 20284 flush_flag = FLUSH_MTU_TIME; 20285 iea.iea_flush_flag = flush_flag; 20286 ire_walk_v6(ire_expire, (char *)(uintptr_t)&iea, 20287 ALL_ZONES, ipst); 20288 } 20289 20290 ipst->ips_ip_ire_arp_time_elapsed += ipst->ips_ip_timer_interval; 20291 ipst->ips_ip_ire_rd_time_elapsed += ipst->ips_ip_timer_interval; 20292 ipst->ips_ip_ire_pmtu_time_elapsed += ipst->ips_ip_timer_interval; 20293 20294 /* 20295 * Hold the lock to serialize timeout calls and prevent 20296 * stale values in ip_ire_expire_id. Otherwise it is possible 20297 * for the timer to fire and a new invocation of this function 20298 * to start before the return value of timeout has been stored 20299 * in ip_ire_expire_id by the current invocation. 20300 */ 20301 mutex_enter(&ipst->ips_ip_trash_timer_lock); 20302 ipst->ips_ip_ire_expire_id = timeout(ip_trash_timer_expire, 20303 (void *)ipst, MSEC_TO_TICK(ipst->ips_ip_timer_interval)); 20304 mutex_exit(&ipst->ips_ip_trash_timer_lock); 20305 } 20306 20307 /* 20308 * Called by the memory allocator subsystem directly, when the system 20309 * is running low on memory. 20310 */ 20311 /* ARGSUSED */ 20312 void 20313 ip_trash_ire_reclaim(void *args) 20314 { 20315 netstack_handle_t nh; 20316 netstack_t *ns; 20317 20318 netstack_next_init(&nh); 20319 while ((ns = netstack_next(&nh)) != NULL) { 20320 ip_trash_ire_reclaim_stack(ns->netstack_ip); 20321 netstack_rele(ns); 20322 } 20323 netstack_next_fini(&nh); 20324 } 20325 20326 static void 20327 ip_trash_ire_reclaim_stack(ip_stack_t *ipst) 20328 { 20329 ire_cache_count_t icc; 20330 ire_cache_reclaim_t icr; 20331 ncc_cache_count_t ncc; 20332 nce_cache_reclaim_t ncr; 20333 uint_t delete_cnt; 20334 /* 20335 * Memory reclaim call back. 20336 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 20337 * Then, with a target of freeing 1/Nth of IRE_CACHE 20338 * entries, determine what fraction to free for 20339 * each category of IRE_CACHE entries giving absolute priority 20340 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 20341 * entry will be freed unless all offlink entries are freed). 20342 */ 20343 icc.icc_total = 0; 20344 icc.icc_unused = 0; 20345 icc.icc_offlink = 0; 20346 icc.icc_pmtu = 0; 20347 icc.icc_onlink = 0; 20348 ire_walk(ire_cache_count, (char *)&icc, ipst); 20349 20350 /* 20351 * Free NCEs for IPv6 like the onlink ires. 20352 */ 20353 ncc.ncc_total = 0; 20354 ncc.ncc_host = 0; 20355 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc, ipst); 20356 20357 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 20358 icc.icc_pmtu + icc.icc_onlink); 20359 delete_cnt = icc.icc_total/ipst->ips_ip_ire_reclaim_fraction; 20360 IP_STAT(ipst, ip_trash_ire_reclaim_calls); 20361 if (delete_cnt == 0) 20362 return; 20363 IP_STAT(ipst, ip_trash_ire_reclaim_success); 20364 /* Always delete all unused offlink entries */ 20365 icr.icr_ipst = ipst; 20366 icr.icr_unused = 1; 20367 if (delete_cnt <= icc.icc_unused) { 20368 /* 20369 * Only need to free unused entries. In other words, 20370 * there are enough unused entries to free to meet our 20371 * target number of freed ire cache entries. 20372 */ 20373 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 20374 ncr.ncr_host = 0; 20375 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 20376 /* 20377 * Only need to free unused entries, plus a fraction of offlink 20378 * entries. It follows from the first if statement that 20379 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 20380 */ 20381 delete_cnt -= icc.icc_unused; 20382 /* Round up # deleted by truncating fraction */ 20383 icr.icr_offlink = icc.icc_offlink / delete_cnt; 20384 icr.icr_pmtu = icr.icr_onlink = 0; 20385 ncr.ncr_host = 0; 20386 } else if (delete_cnt <= 20387 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 20388 /* 20389 * Free all unused and offlink entries, plus a fraction of 20390 * pmtu entries. It follows from the previous if statement 20391 * that icc_pmtu is non-zero, and that 20392 * delete_cnt != icc_unused + icc_offlink. 20393 */ 20394 icr.icr_offlink = 1; 20395 delete_cnt -= icc.icc_unused + icc.icc_offlink; 20396 /* Round up # deleted by truncating fraction */ 20397 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 20398 icr.icr_onlink = 0; 20399 ncr.ncr_host = 0; 20400 } else { 20401 /* 20402 * Free all unused, offlink, and pmtu entries, plus a fraction 20403 * of onlink entries. If we're here, then we know that 20404 * icc_onlink is non-zero, and that 20405 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 20406 */ 20407 icr.icr_offlink = icr.icr_pmtu = 1; 20408 delete_cnt -= icc.icc_unused + icc.icc_offlink + 20409 icc.icc_pmtu; 20410 /* Round up # deleted by truncating fraction */ 20411 icr.icr_onlink = icc.icc_onlink / delete_cnt; 20412 /* Using the same delete fraction as for onlink IREs */ 20413 ncr.ncr_host = ncc.ncc_host / delete_cnt; 20414 } 20415 #ifdef DEBUG 20416 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 20417 "fractions %d/%d/%d/%d\n", 20418 icc.icc_total/ipst->ips_ip_ire_reclaim_fraction, icc.icc_total, 20419 icc.icc_unused, icc.icc_offlink, 20420 icc.icc_pmtu, icc.icc_onlink, 20421 icr.icr_unused, icr.icr_offlink, 20422 icr.icr_pmtu, icr.icr_onlink)); 20423 #endif 20424 ire_walk(ire_cache_reclaim, (char *)&icr, ipst); 20425 if (ncr.ncr_host != 0) 20426 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 20427 (uchar_t *)&ncr, ipst); 20428 #ifdef DEBUG 20429 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 20430 icc.icc_pmtu = 0; icc.icc_onlink = 0; 20431 ire_walk(ire_cache_count, (char *)&icc, ipst); 20432 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 20433 icc.icc_total, icc.icc_unused, icc.icc_offlink, 20434 icc.icc_pmtu, icc.icc_onlink)); 20435 #endif 20436 } 20437 20438 /* 20439 * ip_unbind is called when a copy of an unbind request is received from the 20440 * upper level protocol. We remove this conn from any fanout hash list it is 20441 * on, and zero out the bind information. No reply is expected up above. 20442 */ 20443 void 20444 ip_unbind(conn_t *connp) 20445 { 20446 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 20447 20448 if (is_system_labeled() && connp->conn_anon_port) { 20449 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 20450 connp->conn_mlp_type, connp->conn_ulp, 20451 ntohs(connp->conn_lport), B_FALSE); 20452 connp->conn_anon_port = 0; 20453 } 20454 connp->conn_mlp_type = mlptSingle; 20455 20456 ipcl_hash_remove(connp); 20457 20458 } 20459 20460 /* 20461 * Write side put procedure. Outbound data, IOCTLs, responses from 20462 * resolvers, etc, come down through here. 20463 * 20464 * arg2 is always a queue_t *. 20465 * When that queue is an ill_t (i.e. q_next != NULL), then arg must be 20466 * the zoneid. 20467 * When that queue is not an ill_t, then arg must be a conn_t pointer. 20468 */ 20469 void 20470 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 20471 { 20472 ip_output_options(arg, mp, arg2, caller, &zero_info); 20473 } 20474 20475 void 20476 ip_output_options(void *arg, mblk_t *mp, void *arg2, int caller, 20477 ip_opt_info_t *infop) 20478 { 20479 conn_t *connp = NULL; 20480 queue_t *q = (queue_t *)arg2; 20481 ipha_t *ipha; 20482 #define rptr ((uchar_t *)ipha) 20483 ire_t *ire = NULL; 20484 ire_t *sctp_ire = NULL; 20485 uint32_t v_hlen_tos_len; 20486 ipaddr_t dst; 20487 mblk_t *first_mp = NULL; 20488 boolean_t mctl_present; 20489 ipsec_out_t *io; 20490 int match_flags; 20491 ill_t *xmit_ill = NULL; /* IP_PKTINFO etc. */ 20492 ipif_t *dst_ipif; 20493 boolean_t multirt_need_resolve = B_FALSE; 20494 mblk_t *copy_mp = NULL; 20495 int err = 0; 20496 zoneid_t zoneid; 20497 boolean_t need_decref = B_FALSE; 20498 boolean_t ignore_dontroute = B_FALSE; 20499 boolean_t ignore_nexthop = B_FALSE; 20500 boolean_t ip_nexthop = B_FALSE; 20501 ipaddr_t nexthop_addr; 20502 ip_stack_t *ipst; 20503 20504 #ifdef _BIG_ENDIAN 20505 #define V_HLEN (v_hlen_tos_len >> 24) 20506 #else 20507 #define V_HLEN (v_hlen_tos_len & 0xFF) 20508 #endif 20509 20510 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 20511 "ip_wput_start: q %p", q); 20512 20513 /* 20514 * ip_wput fast path 20515 */ 20516 20517 /* is packet from ARP ? */ 20518 if (q->q_next != NULL) { 20519 zoneid = (zoneid_t)(uintptr_t)arg; 20520 goto qnext; 20521 } 20522 20523 connp = (conn_t *)arg; 20524 ASSERT(connp != NULL); 20525 zoneid = connp->conn_zoneid; 20526 ipst = connp->conn_netstack->netstack_ip; 20527 ASSERT(ipst != NULL); 20528 20529 /* is queue flow controlled? */ 20530 if ((q->q_first != NULL || connp->conn_draining) && 20531 (caller == IP_WPUT)) { 20532 ASSERT(!need_decref); 20533 ASSERT(!IP_FLOW_CONTROLLED_ULP(connp->conn_ulp)); 20534 (void) putq(q, mp); 20535 return; 20536 } 20537 20538 /* Multidata transmit? */ 20539 if (DB_TYPE(mp) == M_MULTIDATA) { 20540 /* 20541 * We should never get here, since all Multidata messages 20542 * originating from tcp should have been directed over to 20543 * tcp_multisend() in the first place. 20544 */ 20545 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20546 freemsg(mp); 20547 return; 20548 } else if (DB_TYPE(mp) != M_DATA) 20549 goto notdata; 20550 20551 if (mp->b_flag & MSGHASREF) { 20552 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20553 mp->b_flag &= ~MSGHASREF; 20554 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 20555 need_decref = B_TRUE; 20556 } 20557 ipha = (ipha_t *)mp->b_rptr; 20558 20559 /* is IP header non-aligned or mblk smaller than basic IP header */ 20560 #ifndef SAFETY_BEFORE_SPEED 20561 if (!OK_32PTR(rptr) || 20562 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 20563 goto hdrtoosmall; 20564 #endif 20565 20566 ASSERT(OK_32PTR(ipha)); 20567 20568 /* 20569 * This function assumes that mp points to an IPv4 packet. If it's the 20570 * wrong version, we'll catch it again in ip_output_v6. 20571 * 20572 * Note that this is *only* locally-generated output here, and never 20573 * forwarded data, and that we need to deal only with transports that 20574 * don't know how to label. (TCP, UDP, and ICMP/raw-IP all know how to 20575 * label.) 20576 */ 20577 if (is_system_labeled() && 20578 (ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4) && 20579 !connp->conn_ulp_labeled) { 20580 cred_t *credp; 20581 pid_t pid; 20582 20583 credp = BEST_CRED(mp, connp, &pid); 20584 err = tsol_check_label(credp, &mp, 20585 connp->conn_mac_exempt, ipst, pid); 20586 ipha = (ipha_t *)mp->b_rptr; 20587 if (err != 0) { 20588 first_mp = mp; 20589 if (err == EINVAL) 20590 goto icmp_parameter_problem; 20591 ip2dbg(("ip_wput: label check failed (%d)\n", err)); 20592 goto discard_pkt; 20593 } 20594 } 20595 20596 ASSERT(infop != NULL); 20597 20598 if (infop->ip_opt_flags & IP_VERIFY_SRC) { 20599 /* 20600 * IP_PKTINFO ancillary option is present. 20601 * IPCL_ZONEID is used to honor IP_ALLZONES option which 20602 * allows using address of any zone as the source address. 20603 */ 20604 ire = ire_ctable_lookup(ipha->ipha_src, 0, 20605 (IRE_LOCAL|IRE_LOOPBACK), NULL, IPCL_ZONEID(connp), 20606 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 20607 if (ire == NULL) 20608 goto drop_pkt; 20609 ire_refrele(ire); 20610 ire = NULL; 20611 } 20612 20613 /* 20614 * IP_BOUND_IF has precedence over the ill index passed in IP_PKTINFO. 20615 */ 20616 if (infop->ip_opt_ill_index != 0 && connp->conn_outgoing_ill == NULL) { 20617 xmit_ill = ill_lookup_on_ifindex(infop->ip_opt_ill_index, 20618 B_FALSE, NULL, NULL, NULL, NULL, ipst); 20619 20620 if (xmit_ill == NULL || IS_VNI(xmit_ill)) 20621 goto drop_pkt; 20622 /* 20623 * check that there is an ipif belonging 20624 * to our zone. IPCL_ZONEID is not used because 20625 * IP_ALLZONES option is valid only when the ill is 20626 * accessible from all zones i.e has a valid ipif in 20627 * all zones. 20628 */ 20629 if (!ipif_lookup_zoneid(xmit_ill, zoneid, 0, NULL)) { 20630 goto drop_pkt; 20631 } 20632 } 20633 20634 /* 20635 * If there is a policy, try to attach an ipsec_out in 20636 * the front. At the end, first_mp either points to a 20637 * M_DATA message or IPSEC_OUT message linked to a 20638 * M_DATA message. We have to do it now as we might 20639 * lose the "conn" if we go through ip_newroute. 20640 */ 20641 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 20642 if (((mp = ipsec_attach_ipsec_out(&mp, connp, NULL, 20643 ipha->ipha_protocol, ipst->ips_netstack)) == NULL)) { 20644 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 20645 if (need_decref) 20646 CONN_DEC_REF(connp); 20647 return; 20648 } else { 20649 ASSERT(mp->b_datap->db_type == M_CTL); 20650 first_mp = mp; 20651 mp = mp->b_cont; 20652 mctl_present = B_TRUE; 20653 } 20654 } else { 20655 first_mp = mp; 20656 mctl_present = B_FALSE; 20657 } 20658 20659 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20660 20661 /* is wrong version or IP options present */ 20662 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 20663 goto version_hdrlen_check; 20664 dst = ipha->ipha_dst; 20665 20666 /* If IP_BOUND_IF has been set, use that ill. */ 20667 if (connp->conn_outgoing_ill != NULL) { 20668 xmit_ill = conn_get_held_ill(connp, 20669 &connp->conn_outgoing_ill, &err); 20670 if (err == ILL_LOOKUP_FAILED) 20671 goto drop_pkt; 20672 20673 goto send_from_ill; 20674 } 20675 20676 /* is packet multicast? */ 20677 if (CLASSD(dst)) 20678 goto multicast; 20679 20680 /* 20681 * If xmit_ill is set above due to index passed in ip_pkt_info. It 20682 * takes precedence over conn_dontroute and conn_nexthop_set 20683 */ 20684 if (xmit_ill != NULL) 20685 goto send_from_ill; 20686 20687 if (connp->conn_dontroute || connp->conn_nexthop_set) { 20688 /* 20689 * If the destination is a broadcast, local, or loopback 20690 * address, SO_DONTROUTE and IP_NEXTHOP go through the 20691 * standard path. 20692 */ 20693 ire = ire_cache_lookup(dst, zoneid, msg_getlabel(mp), ipst); 20694 if ((ire == NULL) || (ire->ire_type & 20695 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)) == 0) { 20696 if (ire != NULL) { 20697 ire_refrele(ire); 20698 /* No more access to ire */ 20699 ire = NULL; 20700 } 20701 /* 20702 * bypass routing checks and go directly to interface. 20703 */ 20704 if (connp->conn_dontroute) 20705 goto dontroute; 20706 20707 ASSERT(connp->conn_nexthop_set); 20708 ip_nexthop = B_TRUE; 20709 nexthop_addr = connp->conn_nexthop_v4; 20710 goto send_from_ill; 20711 } 20712 20713 /* Must be a broadcast, a loopback or a local ire */ 20714 ire_refrele(ire); 20715 /* No more access to ire */ 20716 ire = NULL; 20717 } 20718 20719 /* 20720 * We cache IRE_CACHEs to avoid lookups. We don't do 20721 * this for the tcp global queue and listen end point 20722 * as it does not really have a real destination to 20723 * talk to. This is also true for SCTP. 20724 */ 20725 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 20726 !connp->conn_fully_bound) { 20727 ire = ire_cache_lookup(dst, zoneid, msg_getlabel(mp), ipst); 20728 if (ire == NULL) 20729 goto noirefound; 20730 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20731 "ip_wput_end: q %p (%S)", q, "end"); 20732 20733 /* 20734 * Check if the ire has the RTF_MULTIRT flag, inherited 20735 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20736 */ 20737 if (ire->ire_flags & RTF_MULTIRT) { 20738 20739 /* 20740 * Force the TTL of multirouted packets if required. 20741 * The TTL of such packets is bounded by the 20742 * ip_multirt_ttl ndd variable. 20743 */ 20744 if ((ipst->ips_ip_multirt_ttl > 0) && 20745 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20746 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20747 "(was %d), dst 0x%08x\n", 20748 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20749 ntohl(ire->ire_addr))); 20750 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20751 } 20752 /* 20753 * We look at this point if there are pending 20754 * unresolved routes. ire_multirt_resolvable() 20755 * checks in O(n) that all IRE_OFFSUBNET ire 20756 * entries for the packet's destination and 20757 * flagged RTF_MULTIRT are currently resolved. 20758 * If some remain unresolved, we make a copy 20759 * of the current message. It will be used 20760 * to initiate additional route resolutions. 20761 */ 20762 multirt_need_resolve = 20763 ire_multirt_need_resolve(ire->ire_addr, 20764 msg_getlabel(first_mp), ipst); 20765 ip2dbg(("ip_wput[TCP]: ire %p, " 20766 "multirt_need_resolve %d, first_mp %p\n", 20767 (void *)ire, multirt_need_resolve, 20768 (void *)first_mp)); 20769 if (multirt_need_resolve) { 20770 copy_mp = copymsg(first_mp); 20771 if (copy_mp != NULL) { 20772 MULTIRT_DEBUG_TAG(copy_mp); 20773 } 20774 } 20775 } 20776 20777 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20778 20779 /* 20780 * Try to resolve another multiroute if 20781 * ire_multirt_need_resolve() deemed it necessary. 20782 */ 20783 if (copy_mp != NULL) 20784 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 20785 if (need_decref) 20786 CONN_DEC_REF(connp); 20787 return; 20788 } 20789 20790 /* 20791 * Access to conn_ire_cache. (protected by conn_lock) 20792 * 20793 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 20794 * the ire bucket lock here to check for CONDEMNED as it is okay to 20795 * send a packet or two with the IRE_CACHE that is going away. 20796 * Access to the ire requires an ire refhold on the ire prior to 20797 * its use since an interface unplumb thread may delete the cached 20798 * ire and release the refhold at any time. 20799 * 20800 * Caching an ire in the conn_ire_cache 20801 * 20802 * o Caching an ire pointer in the conn requires a strict check for 20803 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 20804 * ires before cleaning up the conns. So the caching of an ire pointer 20805 * in the conn is done after making sure under the bucket lock that the 20806 * ire has not yet been marked CONDEMNED. Otherwise we will end up 20807 * caching an ire after the unplumb thread has cleaned up the conn. 20808 * If the conn does not send a packet subsequently the unplumb thread 20809 * will be hanging waiting for the ire count to drop to zero. 20810 * 20811 * o We also need to atomically test for a null conn_ire_cache and 20812 * set the conn_ire_cache under the the protection of the conn_lock 20813 * to avoid races among concurrent threads trying to simultaneously 20814 * cache an ire in the conn_ire_cache. 20815 */ 20816 mutex_enter(&connp->conn_lock); 20817 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 20818 20819 if (ire != NULL && ire->ire_addr == dst && 20820 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20821 20822 IRE_REFHOLD(ire); 20823 mutex_exit(&connp->conn_lock); 20824 20825 } else { 20826 boolean_t cached = B_FALSE; 20827 connp->conn_ire_cache = NULL; 20828 mutex_exit(&connp->conn_lock); 20829 /* Release the old ire */ 20830 if (ire != NULL && sctp_ire == NULL) 20831 IRE_REFRELE_NOTR(ire); 20832 20833 ire = ire_cache_lookup(dst, zoneid, msg_getlabel(mp), ipst); 20834 if (ire == NULL) 20835 goto noirefound; 20836 IRE_REFHOLD_NOTR(ire); 20837 20838 mutex_enter(&connp->conn_lock); 20839 if (CONN_CACHE_IRE(connp) && connp->conn_ire_cache == NULL) { 20840 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 20841 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20842 if (connp->conn_ulp == IPPROTO_TCP) 20843 TCP_CHECK_IREINFO(connp->conn_tcp, ire); 20844 connp->conn_ire_cache = ire; 20845 cached = B_TRUE; 20846 } 20847 rw_exit(&ire->ire_bucket->irb_lock); 20848 } 20849 mutex_exit(&connp->conn_lock); 20850 20851 /* 20852 * We can continue to use the ire but since it was 20853 * not cached, we should drop the extra reference. 20854 */ 20855 if (!cached) 20856 IRE_REFRELE_NOTR(ire); 20857 } 20858 20859 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20860 "ip_wput_end: q %p (%S)", q, "end"); 20861 20862 /* 20863 * Check if the ire has the RTF_MULTIRT flag, inherited 20864 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20865 */ 20866 if (ire->ire_flags & RTF_MULTIRT) { 20867 /* 20868 * Force the TTL of multirouted packets if required. 20869 * The TTL of such packets is bounded by the 20870 * ip_multirt_ttl ndd variable. 20871 */ 20872 if ((ipst->ips_ip_multirt_ttl > 0) && 20873 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 20874 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20875 "(was %d), dst 0x%08x\n", 20876 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 20877 ntohl(ire->ire_addr))); 20878 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 20879 } 20880 20881 /* 20882 * At this point, we check to see if there are any pending 20883 * unresolved routes. ire_multirt_resolvable() 20884 * checks in O(n) that all IRE_OFFSUBNET ire 20885 * entries for the packet's destination and 20886 * flagged RTF_MULTIRT are currently resolved. 20887 * If some remain unresolved, we make a copy 20888 * of the current message. It will be used 20889 * to initiate additional route resolutions. 20890 */ 20891 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 20892 msg_getlabel(first_mp), ipst); 20893 ip2dbg(("ip_wput[not TCP]: ire %p, " 20894 "multirt_need_resolve %d, first_mp %p\n", 20895 (void *)ire, multirt_need_resolve, (void *)first_mp)); 20896 if (multirt_need_resolve) { 20897 copy_mp = copymsg(first_mp); 20898 if (copy_mp != NULL) { 20899 MULTIRT_DEBUG_TAG(copy_mp); 20900 } 20901 } 20902 } 20903 20904 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20905 20906 /* 20907 * Try to resolve another multiroute if 20908 * ire_multirt_resolvable() deemed it necessary 20909 */ 20910 if (copy_mp != NULL) 20911 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 20912 if (need_decref) 20913 CONN_DEC_REF(connp); 20914 return; 20915 20916 qnext: 20917 /* 20918 * Upper Level Protocols pass down complete IP datagrams 20919 * as M_DATA messages. Everything else is a sideshow. 20920 * 20921 * 1) We could be re-entering ip_wput because of ip_neworute 20922 * in which case we could have a IPSEC_OUT message. We 20923 * need to pass through ip_wput like other datagrams and 20924 * hence cannot branch to ip_wput_nondata. 20925 * 20926 * 2) ARP, AH, ESP, and other clients who are on the module 20927 * instance of IP stream, give us something to deal with. 20928 * We will handle AH and ESP here and rest in ip_wput_nondata. 20929 * 20930 * 3) ICMP replies also could come here. 20931 */ 20932 ipst = ILLQ_TO_IPST(q); 20933 20934 if (DB_TYPE(mp) != M_DATA) { 20935 notdata: 20936 if (DB_TYPE(mp) == M_CTL) { 20937 /* 20938 * M_CTL messages are used by ARP, AH and ESP to 20939 * communicate with IP. We deal with IPSEC_IN and 20940 * IPSEC_OUT here. ip_wput_nondata handles other 20941 * cases. 20942 */ 20943 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 20944 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 20945 first_mp = mp->b_cont; 20946 first_mp->b_flag &= ~MSGHASREF; 20947 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20948 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 20949 CONN_DEC_REF(connp); 20950 connp = NULL; 20951 } 20952 if (ii->ipsec_info_type == IPSEC_IN) { 20953 /* 20954 * Either this message goes back to 20955 * IPsec for further processing or to 20956 * ULP after policy checks. 20957 */ 20958 ip_fanout_proto_again(mp, NULL, NULL, NULL); 20959 return; 20960 } else if (ii->ipsec_info_type == IPSEC_OUT) { 20961 io = (ipsec_out_t *)ii; 20962 if (io->ipsec_out_proc_begin) { 20963 /* 20964 * IPsec processing has already started. 20965 * Complete it. 20966 * IPQoS notes: We don't care what is 20967 * in ipsec_out_ill_index since this 20968 * won't be processed for IPQoS policies 20969 * in ipsec_out_process. 20970 */ 20971 ipsec_out_process(q, mp, NULL, 20972 io->ipsec_out_ill_index); 20973 return; 20974 } else { 20975 connp = (q->q_next != NULL) ? 20976 NULL : Q_TO_CONN(q); 20977 first_mp = mp; 20978 mp = mp->b_cont; 20979 mctl_present = B_TRUE; 20980 } 20981 zoneid = io->ipsec_out_zoneid; 20982 ASSERT(zoneid != ALL_ZONES); 20983 } else if (ii->ipsec_info_type == IPSEC_CTL) { 20984 /* 20985 * It's an IPsec control message requesting 20986 * an SADB update to be sent to the IPsec 20987 * hardware acceleration capable ills. 20988 */ 20989 ipsec_ctl_t *ipsec_ctl = 20990 (ipsec_ctl_t *)mp->b_rptr; 20991 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 20992 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 20993 mblk_t *cmp = mp->b_cont; 20994 20995 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 20996 ASSERT(cmp != NULL); 20997 20998 freeb(mp); 20999 ill_ipsec_capab_send_all(satype, cmp, sa, 21000 ipst->ips_netstack); 21001 return; 21002 } else { 21003 /* 21004 * This must be ARP or special TSOL signaling. 21005 */ 21006 ip_wput_nondata(NULL, q, mp, NULL); 21007 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21008 "ip_wput_end: q %p (%S)", q, "nondata"); 21009 return; 21010 } 21011 } else { 21012 /* 21013 * This must be non-(ARP/AH/ESP) messages. 21014 */ 21015 ASSERT(!need_decref); 21016 ip_wput_nondata(NULL, q, mp, NULL); 21017 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21018 "ip_wput_end: q %p (%S)", q, "nondata"); 21019 return; 21020 } 21021 } else { 21022 first_mp = mp; 21023 mctl_present = B_FALSE; 21024 } 21025 21026 ASSERT(first_mp != NULL); 21027 21028 if (mctl_present) { 21029 io = (ipsec_out_t *)first_mp->b_rptr; 21030 if (io->ipsec_out_ip_nexthop) { 21031 /* 21032 * We may have lost the conn context if we are 21033 * coming here from ip_newroute(). Copy the 21034 * nexthop information. 21035 */ 21036 ip_nexthop = B_TRUE; 21037 nexthop_addr = io->ipsec_out_nexthop_addr; 21038 21039 ipha = (ipha_t *)mp->b_rptr; 21040 dst = ipha->ipha_dst; 21041 goto send_from_ill; 21042 } 21043 } 21044 21045 ASSERT(xmit_ill == NULL); 21046 21047 /* We have a complete IP datagram heading outbound. */ 21048 ipha = (ipha_t *)mp->b_rptr; 21049 21050 #ifndef SPEED_BEFORE_SAFETY 21051 /* 21052 * Make sure we have a full-word aligned message and that at least 21053 * a simple IP header is accessible in the first message. If not, 21054 * try a pullup. For labeled systems we need to always take this 21055 * path as M_CTLs are "notdata" but have trailing data to process. 21056 */ 21057 if (!OK_32PTR(rptr) || 21058 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH || is_system_labeled()) { 21059 hdrtoosmall: 21060 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 21061 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21062 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 21063 if (first_mp == NULL) 21064 first_mp = mp; 21065 goto discard_pkt; 21066 } 21067 21068 /* This function assumes that mp points to an IPv4 packet. */ 21069 if (is_system_labeled() && 21070 (*mp->b_rptr & 0xf0) == (IPV4_VERSION << 4) && 21071 (connp == NULL || !connp->conn_ulp_labeled)) { 21072 cred_t *credp; 21073 pid_t pid; 21074 21075 if (connp != NULL) { 21076 credp = BEST_CRED(mp, connp, &pid); 21077 err = tsol_check_label(credp, &mp, 21078 connp->conn_mac_exempt, ipst, pid); 21079 } else if ((credp = msg_getcred(mp, &pid)) != NULL) { 21080 err = tsol_check_label(credp, &mp, 21081 B_FALSE, ipst, pid); 21082 } 21083 ipha = (ipha_t *)mp->b_rptr; 21084 if (mctl_present) 21085 first_mp->b_cont = mp; 21086 else 21087 first_mp = mp; 21088 if (err != 0) { 21089 if (err == EINVAL) 21090 goto icmp_parameter_problem; 21091 ip2dbg(("ip_wput: label check failed (%d)\n", 21092 err)); 21093 goto discard_pkt; 21094 } 21095 } 21096 21097 ipha = (ipha_t *)mp->b_rptr; 21098 if (first_mp == NULL) { 21099 ASSERT(xmit_ill == NULL); 21100 /* 21101 * If we got here because of "goto hdrtoosmall" 21102 * We need to attach a IPSEC_OUT. 21103 */ 21104 if (connp->conn_out_enforce_policy) { 21105 if (((mp = ipsec_attach_ipsec_out(&mp, connp, 21106 NULL, ipha->ipha_protocol, 21107 ipst->ips_netstack)) == NULL)) { 21108 BUMP_MIB(&ipst->ips_ip_mib, 21109 ipIfStatsOutDiscards); 21110 if (need_decref) 21111 CONN_DEC_REF(connp); 21112 return; 21113 } else { 21114 ASSERT(mp->b_datap->db_type == M_CTL); 21115 first_mp = mp; 21116 mp = mp->b_cont; 21117 mctl_present = B_TRUE; 21118 } 21119 } else { 21120 first_mp = mp; 21121 mctl_present = B_FALSE; 21122 } 21123 } 21124 } 21125 #endif 21126 21127 /* Most of the code below is written for speed, not readability */ 21128 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21129 21130 /* 21131 * If ip_newroute() fails, we're going to need a full 21132 * header for the icmp wraparound. 21133 */ 21134 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 21135 uint_t v_hlen; 21136 version_hdrlen_check: 21137 ASSERT(first_mp != NULL); 21138 v_hlen = V_HLEN; 21139 /* 21140 * siphon off IPv6 packets coming down from transport 21141 * layer modules here. 21142 * Note: high-order bit carries NUD reachability confirmation 21143 */ 21144 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 21145 /* 21146 * FIXME: assume that callers of ip_output* call 21147 * the right version? 21148 */ 21149 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutWrongIPVersion); 21150 ASSERT(xmit_ill == NULL); 21151 if (need_decref) 21152 mp->b_flag |= MSGHASREF; 21153 (void) ip_output_v6(arg, first_mp, arg2, caller); 21154 return; 21155 } 21156 21157 if ((v_hlen >> 4) != IP_VERSION) { 21158 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21159 "ip_wput_end: q %p (%S)", q, "badvers"); 21160 goto discard_pkt; 21161 } 21162 /* 21163 * Is the header length at least 20 bytes? 21164 * 21165 * Are there enough bytes accessible in the header? If 21166 * not, try a pullup. 21167 */ 21168 v_hlen &= 0xF; 21169 v_hlen <<= 2; 21170 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 21171 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21172 "ip_wput_end: q %p (%S)", q, "badlen"); 21173 goto discard_pkt; 21174 } 21175 if (v_hlen > (mp->b_wptr - rptr)) { 21176 if (!pullupmsg(mp, v_hlen)) { 21177 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21178 "ip_wput_end: q %p (%S)", q, "badpullup2"); 21179 goto discard_pkt; 21180 } 21181 ipha = (ipha_t *)mp->b_rptr; 21182 } 21183 /* 21184 * Move first entry from any source route into ipha_dst and 21185 * verify the options 21186 */ 21187 if (ip_wput_options(q, first_mp, ipha, mctl_present, 21188 zoneid, ipst)) { 21189 ASSERT(xmit_ill == NULL); 21190 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21191 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21192 "ip_wput_end: q %p (%S)", q, "badopts"); 21193 if (need_decref) 21194 CONN_DEC_REF(connp); 21195 return; 21196 } 21197 } 21198 dst = ipha->ipha_dst; 21199 21200 /* 21201 * Try to get an IRE_CACHE for the destination address. If we can't, 21202 * we have to run the packet through ip_newroute which will take 21203 * the appropriate action to arrange for an IRE_CACHE, such as querying 21204 * a resolver, or assigning a default gateway, etc. 21205 */ 21206 if (CLASSD(dst)) { 21207 ipif_t *ipif; 21208 uint32_t setsrc = 0; 21209 21210 multicast: 21211 ASSERT(first_mp != NULL); 21212 ip2dbg(("ip_wput: CLASSD\n")); 21213 if (connp == NULL) { 21214 /* 21215 * Use the first good ipif on the ill. 21216 * XXX Should this ever happen? (Appears 21217 * to show up with just ppp and no ethernet due 21218 * to in.rdisc.) 21219 * However, ire_send should be able to 21220 * call ip_wput_ire directly. 21221 * 21222 * XXX Also, this can happen for ICMP and other packets 21223 * with multicast source addresses. Perhaps we should 21224 * fix things so that we drop the packet in question, 21225 * but for now, just run with it. 21226 */ 21227 ill_t *ill = (ill_t *)q->q_ptr; 21228 21229 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 21230 if (ipif == NULL) { 21231 if (need_decref) 21232 CONN_DEC_REF(connp); 21233 freemsg(first_mp); 21234 return; 21235 } 21236 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 21237 ntohl(dst), ill->ill_name)); 21238 } else { 21239 /* 21240 * The order of precedence is IP_BOUND_IF, IP_PKTINFO 21241 * and IP_MULTICAST_IF. The block comment above this 21242 * function explains the locking mechanism used here. 21243 */ 21244 if (xmit_ill == NULL) { 21245 xmit_ill = conn_get_held_ill(connp, 21246 &connp->conn_outgoing_ill, &err); 21247 if (err == ILL_LOOKUP_FAILED) { 21248 ip1dbg(("ip_wput: No ill for " 21249 "IP_BOUND_IF\n")); 21250 BUMP_MIB(&ipst->ips_ip_mib, 21251 ipIfStatsOutNoRoutes); 21252 goto drop_pkt; 21253 } 21254 } 21255 21256 if (xmit_ill == NULL) { 21257 ipif = conn_get_held_ipif(connp, 21258 &connp->conn_multicast_ipif, &err); 21259 if (err == IPIF_LOOKUP_FAILED) { 21260 ip1dbg(("ip_wput: No ipif for " 21261 "multicast\n")); 21262 BUMP_MIB(&ipst->ips_ip_mib, 21263 ipIfStatsOutNoRoutes); 21264 goto drop_pkt; 21265 } 21266 } 21267 if (xmit_ill != NULL) { 21268 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21269 if (ipif == NULL) { 21270 ip1dbg(("ip_wput: No ipif for " 21271 "xmit_ill\n")); 21272 BUMP_MIB(&ipst->ips_ip_mib, 21273 ipIfStatsOutNoRoutes); 21274 goto drop_pkt; 21275 } 21276 } else if (ipif == NULL || ipif->ipif_isv6) { 21277 /* 21278 * We must do this ipif determination here 21279 * else we could pass through ip_newroute 21280 * and come back here without the conn context. 21281 * 21282 * Note: we do late binding i.e. we bind to 21283 * the interface when the first packet is sent. 21284 * For performance reasons we do not rebind on 21285 * each packet but keep the binding until the 21286 * next IP_MULTICAST_IF option. 21287 * 21288 * conn_multicast_{ipif,ill} are shared between 21289 * IPv4 and IPv6 and AF_INET6 sockets can 21290 * send both IPv4 and IPv6 packets. Hence 21291 * we have to check that "isv6" matches above. 21292 */ 21293 if (ipif != NULL) 21294 ipif_refrele(ipif); 21295 ipif = ipif_lookup_group(dst, zoneid, ipst); 21296 if (ipif == NULL) { 21297 ip1dbg(("ip_wput: No ipif for " 21298 "multicast\n")); 21299 BUMP_MIB(&ipst->ips_ip_mib, 21300 ipIfStatsOutNoRoutes); 21301 goto drop_pkt; 21302 } 21303 err = conn_set_held_ipif(connp, 21304 &connp->conn_multicast_ipif, ipif); 21305 if (err == IPIF_LOOKUP_FAILED) { 21306 ipif_refrele(ipif); 21307 ip1dbg(("ip_wput: No ipif for " 21308 "multicast\n")); 21309 BUMP_MIB(&ipst->ips_ip_mib, 21310 ipIfStatsOutNoRoutes); 21311 goto drop_pkt; 21312 } 21313 } 21314 } 21315 ASSERT(!ipif->ipif_isv6); 21316 /* 21317 * As we may lose the conn by the time we reach ip_wput_ire, 21318 * we copy conn_multicast_loop and conn_dontroute on to an 21319 * ipsec_out. In case if this datagram goes out secure, 21320 * we need the ill_index also. Copy that also into the 21321 * ipsec_out. 21322 */ 21323 if (mctl_present) { 21324 io = (ipsec_out_t *)first_mp->b_rptr; 21325 ASSERT(first_mp->b_datap->db_type == M_CTL); 21326 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21327 } else { 21328 ASSERT(mp == first_mp); 21329 if ((first_mp = allocb(sizeof (ipsec_info_t), 21330 BPRI_HI)) == NULL) { 21331 ipif_refrele(ipif); 21332 first_mp = mp; 21333 goto discard_pkt; 21334 } 21335 first_mp->b_datap->db_type = M_CTL; 21336 first_mp->b_wptr += sizeof (ipsec_info_t); 21337 /* ipsec_out_secure is B_FALSE now */ 21338 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 21339 io = (ipsec_out_t *)first_mp->b_rptr; 21340 io->ipsec_out_type = IPSEC_OUT; 21341 io->ipsec_out_len = sizeof (ipsec_out_t); 21342 io->ipsec_out_use_global_policy = B_TRUE; 21343 io->ipsec_out_ns = ipst->ips_netstack; 21344 first_mp->b_cont = mp; 21345 mctl_present = B_TRUE; 21346 } 21347 21348 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21349 io->ipsec_out_ill_index = 21350 ipif->ipif_ill->ill_phyint->phyint_ifindex; 21351 21352 if (connp != NULL) { 21353 io->ipsec_out_multicast_loop = 21354 connp->conn_multicast_loop; 21355 io->ipsec_out_dontroute = connp->conn_dontroute; 21356 io->ipsec_out_zoneid = connp->conn_zoneid; 21357 } 21358 /* 21359 * If the application uses IP_MULTICAST_IF with 21360 * different logical addresses of the same ILL, we 21361 * need to make sure that the soruce address of 21362 * the packet matches the logical IP address used 21363 * in the option. We do it by initializing ipha_src 21364 * here. This should keep IPsec also happy as 21365 * when we return from IPsec processing, we don't 21366 * have to worry about getting the right address on 21367 * the packet. Thus it is sufficient to look for 21368 * IRE_CACHE using MATCH_IRE_ILL rathen than 21369 * MATCH_IRE_IPIF. 21370 * 21371 * NOTE : We need to do it for non-secure case also as 21372 * this might go out secure if there is a global policy 21373 * match in ip_wput_ire. 21374 * 21375 * As we do not have the ire yet, it is possible that 21376 * we set the source address here and then later discover 21377 * that the ire implies the source address to be assigned 21378 * through the RTF_SETSRC flag. 21379 * In that case, the setsrc variable will remind us 21380 * that overwritting the source address by the one 21381 * of the RTF_SETSRC-flagged ire is allowed. 21382 */ 21383 if (ipha->ipha_src == INADDR_ANY && 21384 (connp == NULL || !connp->conn_unspec_src)) { 21385 ipha->ipha_src = ipif->ipif_src_addr; 21386 setsrc = RTF_SETSRC; 21387 } 21388 /* 21389 * Find an IRE which matches the destination and the outgoing 21390 * queue (i.e. the outgoing interface.) 21391 * For loopback use a unicast IP address for 21392 * the ire lookup. 21393 */ 21394 if (IS_LOOPBACK(ipif->ipif_ill)) 21395 dst = ipif->ipif_lcl_addr; 21396 21397 /* 21398 * If xmit_ill is set, we branch out to ip_newroute_ipif. 21399 * We don't need to lookup ire in ctable as the packet 21400 * needs to be sent to the destination through the specified 21401 * ill irrespective of ires in the cache table. 21402 */ 21403 ire = NULL; 21404 if (xmit_ill == NULL) { 21405 ire = ire_ctable_lookup(dst, 0, 0, ipif, 21406 zoneid, msg_getlabel(mp), match_flags, ipst); 21407 } 21408 21409 if (ire == NULL) { 21410 /* 21411 * Multicast loopback and multicast forwarding is 21412 * done in ip_wput_ire. 21413 * 21414 * Mark this packet to make it be delivered to 21415 * ip_wput_ire after the new ire has been 21416 * created. 21417 * 21418 * The call to ip_newroute_ipif takes into account 21419 * the setsrc reminder. In any case, we take care 21420 * of the RTF_MULTIRT flag. 21421 */ 21422 mp->b_prev = mp->b_next = NULL; 21423 if (xmit_ill == NULL || 21424 xmit_ill->ill_ipif_up_count > 0) { 21425 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 21426 setsrc | RTF_MULTIRT, zoneid, infop); 21427 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21428 "ip_wput_end: q %p (%S)", q, "noire"); 21429 } else { 21430 freemsg(first_mp); 21431 } 21432 ipif_refrele(ipif); 21433 if (xmit_ill != NULL) 21434 ill_refrele(xmit_ill); 21435 if (need_decref) 21436 CONN_DEC_REF(connp); 21437 return; 21438 } 21439 21440 ipif_refrele(ipif); 21441 ipif = NULL; 21442 ASSERT(xmit_ill == NULL); 21443 21444 /* 21445 * Honor the RTF_SETSRC flag for multicast packets, 21446 * if allowed by the setsrc reminder. 21447 */ 21448 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 21449 ipha->ipha_src = ire->ire_src_addr; 21450 } 21451 21452 /* 21453 * Unconditionally force the TTL to 1 for 21454 * multirouted multicast packets: 21455 * multirouted multicast should not cross 21456 * multicast routers. 21457 */ 21458 if (ire->ire_flags & RTF_MULTIRT) { 21459 if (ipha->ipha_ttl > 1) { 21460 ip2dbg(("ip_wput: forcing multicast " 21461 "multirt TTL to 1 (was %d), dst 0x%08x\n", 21462 ipha->ipha_ttl, ntohl(ire->ire_addr))); 21463 ipha->ipha_ttl = 1; 21464 } 21465 } 21466 } else { 21467 ire = ire_cache_lookup(dst, zoneid, msg_getlabel(mp), ipst); 21468 if ((ire != NULL) && (ire->ire_type & 21469 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 21470 ignore_dontroute = B_TRUE; 21471 ignore_nexthop = B_TRUE; 21472 } 21473 if (ire != NULL) { 21474 ire_refrele(ire); 21475 ire = NULL; 21476 } 21477 /* 21478 * Guard against coming in from arp in which case conn is NULL. 21479 * Also guard against non M_DATA with dontroute set but 21480 * destined to local, loopback or broadcast addresses. 21481 */ 21482 if (connp != NULL && connp->conn_dontroute && 21483 !ignore_dontroute) { 21484 dontroute: 21485 /* 21486 * Set TTL to 1 if SO_DONTROUTE is set to prevent 21487 * routing protocols from seeing false direct 21488 * connectivity. 21489 */ 21490 ipha->ipha_ttl = 1; 21491 /* If suitable ipif not found, drop packet */ 21492 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid, ipst); 21493 if (dst_ipif == NULL) { 21494 noroute: 21495 ip1dbg(("ip_wput: no route for dst using" 21496 " SO_DONTROUTE\n")); 21497 BUMP_MIB(&ipst->ips_ip_mib, 21498 ipIfStatsOutNoRoutes); 21499 mp->b_prev = mp->b_next = NULL; 21500 if (first_mp == NULL) 21501 first_mp = mp; 21502 goto drop_pkt; 21503 } else { 21504 /* 21505 * If suitable ipif has been found, set 21506 * xmit_ill to the corresponding 21507 * ipif_ill because we'll be using the 21508 * send_from_ill logic below. 21509 */ 21510 ASSERT(xmit_ill == NULL); 21511 xmit_ill = dst_ipif->ipif_ill; 21512 mutex_enter(&xmit_ill->ill_lock); 21513 if (!ILL_CAN_LOOKUP(xmit_ill)) { 21514 mutex_exit(&xmit_ill->ill_lock); 21515 xmit_ill = NULL; 21516 ipif_refrele(dst_ipif); 21517 goto noroute; 21518 } 21519 ill_refhold_locked(xmit_ill); 21520 mutex_exit(&xmit_ill->ill_lock); 21521 ipif_refrele(dst_ipif); 21522 } 21523 } 21524 21525 send_from_ill: 21526 if (xmit_ill != NULL) { 21527 ipif_t *ipif; 21528 21529 /* 21530 * Mark this packet as originated locally 21531 */ 21532 mp->b_prev = mp->b_next = NULL; 21533 21534 /* 21535 * Could be SO_DONTROUTE case also. 21536 * Verify that at least one ipif is up on the ill. 21537 */ 21538 if (xmit_ill->ill_ipif_up_count == 0) { 21539 ip1dbg(("ip_output: xmit_ill %s is down\n", 21540 xmit_ill->ill_name)); 21541 goto drop_pkt; 21542 } 21543 21544 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21545 if (ipif == NULL) { 21546 ip1dbg(("ip_output: xmit_ill %s NULL ipif\n", 21547 xmit_ill->ill_name)); 21548 goto drop_pkt; 21549 } 21550 21551 match_flags = 0; 21552 if (IS_UNDER_IPMP(xmit_ill)) 21553 match_flags |= MATCH_IRE_MARK_TESTHIDDEN; 21554 21555 /* 21556 * Look for a ire that is part of the group, 21557 * if found use it else call ip_newroute_ipif. 21558 * IPCL_ZONEID is not used for matching because 21559 * IP_ALLZONES option is valid only when the 21560 * ill is accessible from all zones i.e has a 21561 * valid ipif in all zones. 21562 */ 21563 match_flags |= MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21564 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 21565 msg_getlabel(mp), match_flags, ipst); 21566 /* 21567 * If an ire exists use it or else create 21568 * an ire but don't add it to the cache. 21569 * Adding an ire may cause issues with 21570 * asymmetric routing. 21571 * In case of multiroute always act as if 21572 * ire does not exist. 21573 */ 21574 if (ire == NULL || ire->ire_flags & RTF_MULTIRT) { 21575 if (ire != NULL) 21576 ire_refrele(ire); 21577 ip_newroute_ipif(q, first_mp, ipif, 21578 dst, connp, 0, zoneid, infop); 21579 ipif_refrele(ipif); 21580 ip1dbg(("ip_output: xmit_ill via %s\n", 21581 xmit_ill->ill_name)); 21582 ill_refrele(xmit_ill); 21583 if (need_decref) 21584 CONN_DEC_REF(connp); 21585 return; 21586 } 21587 ipif_refrele(ipif); 21588 } else if (ip_nexthop || (connp != NULL && 21589 (connp->conn_nexthop_set)) && !ignore_nexthop) { 21590 if (!ip_nexthop) { 21591 ip_nexthop = B_TRUE; 21592 nexthop_addr = connp->conn_nexthop_v4; 21593 } 21594 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 21595 MATCH_IRE_GW; 21596 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 21597 NULL, zoneid, msg_getlabel(mp), match_flags, ipst); 21598 } else { 21599 ire = ire_cache_lookup(dst, zoneid, msg_getlabel(mp), 21600 ipst); 21601 } 21602 if (!ire) { 21603 if (ip_nexthop && !ignore_nexthop) { 21604 if (mctl_present) { 21605 io = (ipsec_out_t *)first_mp->b_rptr; 21606 ASSERT(first_mp->b_datap->db_type == 21607 M_CTL); 21608 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21609 } else { 21610 ASSERT(mp == first_mp); 21611 first_mp = allocb( 21612 sizeof (ipsec_info_t), BPRI_HI); 21613 if (first_mp == NULL) { 21614 first_mp = mp; 21615 goto discard_pkt; 21616 } 21617 first_mp->b_datap->db_type = M_CTL; 21618 first_mp->b_wptr += 21619 sizeof (ipsec_info_t); 21620 /* ipsec_out_secure is B_FALSE now */ 21621 bzero(first_mp->b_rptr, 21622 sizeof (ipsec_info_t)); 21623 io = (ipsec_out_t *)first_mp->b_rptr; 21624 io->ipsec_out_type = IPSEC_OUT; 21625 io->ipsec_out_len = 21626 sizeof (ipsec_out_t); 21627 io->ipsec_out_use_global_policy = 21628 B_TRUE; 21629 io->ipsec_out_ns = ipst->ips_netstack; 21630 first_mp->b_cont = mp; 21631 mctl_present = B_TRUE; 21632 } 21633 io->ipsec_out_ip_nexthop = ip_nexthop; 21634 io->ipsec_out_nexthop_addr = nexthop_addr; 21635 } 21636 noirefound: 21637 /* 21638 * Mark this packet as having originated on 21639 * this machine. This will be noted in 21640 * ire_add_then_send, which needs to know 21641 * whether to run it back through ip_wput or 21642 * ip_rput following successful resolution. 21643 */ 21644 mp->b_prev = NULL; 21645 mp->b_next = NULL; 21646 ip_newroute(q, first_mp, dst, connp, zoneid, ipst); 21647 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21648 "ip_wput_end: q %p (%S)", q, "newroute"); 21649 if (xmit_ill != NULL) 21650 ill_refrele(xmit_ill); 21651 if (need_decref) 21652 CONN_DEC_REF(connp); 21653 return; 21654 } 21655 } 21656 21657 /* We now know where we are going with it. */ 21658 21659 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21660 "ip_wput_end: q %p (%S)", q, "end"); 21661 21662 /* 21663 * Check if the ire has the RTF_MULTIRT flag, inherited 21664 * from an IRE_OFFSUBNET ire entry in ip_newroute. 21665 */ 21666 if (ire->ire_flags & RTF_MULTIRT) { 21667 /* 21668 * Force the TTL of multirouted packets if required. 21669 * The TTL of such packets is bounded by the 21670 * ip_multirt_ttl ndd variable. 21671 */ 21672 if ((ipst->ips_ip_multirt_ttl > 0) && 21673 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { 21674 ip2dbg(("ip_wput: forcing multirt TTL to %d " 21675 "(was %d), dst 0x%08x\n", 21676 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, 21677 ntohl(ire->ire_addr))); 21678 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; 21679 } 21680 /* 21681 * At this point, we check to see if there are any pending 21682 * unresolved routes. ire_multirt_resolvable() 21683 * checks in O(n) that all IRE_OFFSUBNET ire 21684 * entries for the packet's destination and 21685 * flagged RTF_MULTIRT are currently resolved. 21686 * If some remain unresolved, we make a copy 21687 * of the current message. It will be used 21688 * to initiate additional route resolutions. 21689 */ 21690 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 21691 msg_getlabel(first_mp), ipst); 21692 ip2dbg(("ip_wput[noirefound]: ire %p, " 21693 "multirt_need_resolve %d, first_mp %p\n", 21694 (void *)ire, multirt_need_resolve, (void *)first_mp)); 21695 if (multirt_need_resolve) { 21696 copy_mp = copymsg(first_mp); 21697 if (copy_mp != NULL) { 21698 MULTIRT_DEBUG_TAG(copy_mp); 21699 } 21700 } 21701 } 21702 21703 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 21704 /* 21705 * Try to resolve another multiroute if 21706 * ire_multirt_resolvable() deemed it necessary. 21707 * At this point, we need to distinguish 21708 * multicasts from other packets. For multicasts, 21709 * we call ip_newroute_ipif() and request that both 21710 * multirouting and setsrc flags are checked. 21711 */ 21712 if (copy_mp != NULL) { 21713 if (CLASSD(dst)) { 21714 ipif_t *ipif = ipif_lookup_group(dst, zoneid, ipst); 21715 if (ipif) { 21716 ASSERT(infop->ip_opt_ill_index == 0); 21717 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 21718 RTF_SETSRC | RTF_MULTIRT, zoneid, infop); 21719 ipif_refrele(ipif); 21720 } else { 21721 MULTIRT_DEBUG_UNTAG(copy_mp); 21722 freemsg(copy_mp); 21723 copy_mp = NULL; 21724 } 21725 } else { 21726 ip_newroute(q, copy_mp, dst, connp, zoneid, ipst); 21727 } 21728 } 21729 if (xmit_ill != NULL) 21730 ill_refrele(xmit_ill); 21731 if (need_decref) 21732 CONN_DEC_REF(connp); 21733 return; 21734 21735 icmp_parameter_problem: 21736 /* could not have originated externally */ 21737 ASSERT(mp->b_prev == NULL); 21738 if (ip_hdr_complete(ipha, zoneid, ipst) == 0) { 21739 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 21740 /* it's the IP header length that's in trouble */ 21741 icmp_param_problem(q, first_mp, 0, zoneid, ipst); 21742 first_mp = NULL; 21743 } 21744 21745 discard_pkt: 21746 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 21747 drop_pkt: 21748 ip1dbg(("ip_wput: dropped packet\n")); 21749 if (ire != NULL) 21750 ire_refrele(ire); 21751 if (need_decref) 21752 CONN_DEC_REF(connp); 21753 freemsg(first_mp); 21754 if (xmit_ill != NULL) 21755 ill_refrele(xmit_ill); 21756 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21757 "ip_wput_end: q %p (%S)", q, "droppkt"); 21758 } 21759 21760 /* 21761 * If this is a conn_t queue, then we pass in the conn. This includes the 21762 * zoneid. 21763 * Otherwise, this is a message coming back from ARP or for an ill_t queue, 21764 * in which case we use the global zoneid since those are all part of 21765 * the global zone. 21766 */ 21767 void 21768 ip_wput(queue_t *q, mblk_t *mp) 21769 { 21770 if (CONN_Q(q)) 21771 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 21772 else 21773 ip_output(GLOBAL_ZONEID, mp, q, IP_WPUT); 21774 } 21775 21776 /* 21777 * 21778 * The following rules must be observed when accessing any ipif or ill 21779 * that has been cached in the conn. Typically conn_outgoing_ill, 21780 * conn_multicast_ipif and conn_multicast_ill. 21781 * 21782 * Access: The ipif or ill pointed to from the conn can be accessed under 21783 * the protection of the conn_lock or after it has been refheld under the 21784 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 21785 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 21786 * The reason for this is that a concurrent unplumb could actually be 21787 * cleaning up these cached pointers by walking the conns and might have 21788 * finished cleaning up the conn in question. The macros check that an 21789 * unplumb has not yet started on the ipif or ill. 21790 * 21791 * Caching: An ipif or ill pointer may be cached in the conn only after 21792 * making sure that an unplumb has not started. So the caching is done 21793 * while holding both the conn_lock and the ill_lock and after using the 21794 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 21795 * flag before starting the cleanup of conns. 21796 * 21797 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 21798 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 21799 * or a reference to the ipif or a reference to an ire that references the 21800 * ipif. An ipif only changes its ill when migrating from an underlying ill 21801 * to an IPMP ill in ipif_up(). 21802 */ 21803 ipif_t * 21804 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 21805 { 21806 ipif_t *ipif; 21807 ill_t *ill; 21808 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 21809 21810 *err = 0; 21811 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 21812 mutex_enter(&connp->conn_lock); 21813 ipif = *ipifp; 21814 if (ipif != NULL) { 21815 ill = ipif->ipif_ill; 21816 mutex_enter(&ill->ill_lock); 21817 if (IPIF_CAN_LOOKUP(ipif)) { 21818 ipif_refhold_locked(ipif); 21819 mutex_exit(&ill->ill_lock); 21820 mutex_exit(&connp->conn_lock); 21821 rw_exit(&ipst->ips_ill_g_lock); 21822 return (ipif); 21823 } else { 21824 *err = IPIF_LOOKUP_FAILED; 21825 } 21826 mutex_exit(&ill->ill_lock); 21827 } 21828 mutex_exit(&connp->conn_lock); 21829 rw_exit(&ipst->ips_ill_g_lock); 21830 return (NULL); 21831 } 21832 21833 ill_t * 21834 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 21835 { 21836 ill_t *ill; 21837 21838 *err = 0; 21839 mutex_enter(&connp->conn_lock); 21840 ill = *illp; 21841 if (ill != NULL) { 21842 mutex_enter(&ill->ill_lock); 21843 if (ILL_CAN_LOOKUP(ill)) { 21844 ill_refhold_locked(ill); 21845 mutex_exit(&ill->ill_lock); 21846 mutex_exit(&connp->conn_lock); 21847 return (ill); 21848 } else { 21849 *err = ILL_LOOKUP_FAILED; 21850 } 21851 mutex_exit(&ill->ill_lock); 21852 } 21853 mutex_exit(&connp->conn_lock); 21854 return (NULL); 21855 } 21856 21857 static int 21858 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 21859 { 21860 ill_t *ill; 21861 21862 ill = ipif->ipif_ill; 21863 mutex_enter(&connp->conn_lock); 21864 mutex_enter(&ill->ill_lock); 21865 if (IPIF_CAN_LOOKUP(ipif)) { 21866 *ipifp = ipif; 21867 mutex_exit(&ill->ill_lock); 21868 mutex_exit(&connp->conn_lock); 21869 return (0); 21870 } 21871 mutex_exit(&ill->ill_lock); 21872 mutex_exit(&connp->conn_lock); 21873 return (IPIF_LOOKUP_FAILED); 21874 } 21875 21876 /* 21877 * This is called if the outbound datagram needs fragmentation. 21878 * 21879 * NOTE : This function does not ire_refrele the ire argument passed in. 21880 */ 21881 static void 21882 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire, zoneid_t zoneid, 21883 ip_stack_t *ipst, conn_t *connp) 21884 { 21885 ipha_t *ipha; 21886 mblk_t *mp; 21887 uint32_t v_hlen_tos_len; 21888 uint32_t max_frag; 21889 uint32_t frag_flag; 21890 boolean_t dont_use; 21891 21892 if (ipsec_mp->b_datap->db_type == M_CTL) { 21893 mp = ipsec_mp->b_cont; 21894 } else { 21895 mp = ipsec_mp; 21896 } 21897 21898 ipha = (ipha_t *)mp->b_rptr; 21899 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21900 21901 #ifdef _BIG_ENDIAN 21902 #define V_HLEN (v_hlen_tos_len >> 24) 21903 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21904 #else 21905 #define V_HLEN (v_hlen_tos_len & 0xFF) 21906 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21907 #endif 21908 21909 #ifndef SPEED_BEFORE_SAFETY 21910 /* 21911 * Check that ipha_length is consistent with 21912 * the mblk length 21913 */ 21914 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 21915 ip0dbg(("Packet length mismatch: %d, %ld\n", 21916 LENGTH, msgdsize(mp))); 21917 freemsg(ipsec_mp); 21918 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21919 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 21920 "packet length mismatch"); 21921 return; 21922 } 21923 #endif 21924 /* 21925 * Don't use frag_flag if pre-built packet or source 21926 * routed or if multicast (since multicast packets do not solicit 21927 * ICMP "packet too big" messages). Get the values of 21928 * max_frag and frag_flag atomically by acquiring the 21929 * ire_lock. 21930 */ 21931 mutex_enter(&ire->ire_lock); 21932 max_frag = ire->ire_max_frag; 21933 frag_flag = ire->ire_frag_flag; 21934 mutex_exit(&ire->ire_lock); 21935 21936 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 21937 (V_HLEN != IP_SIMPLE_HDR_VERSION && 21938 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 21939 21940 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 21941 (dont_use ? 0 : frag_flag), zoneid, ipst, connp); 21942 } 21943 21944 /* 21945 * Used for deciding the MSS size for the upper layer. Thus 21946 * we need to check the outbound policy values in the conn. 21947 */ 21948 int 21949 conn_ipsec_length(conn_t *connp) 21950 { 21951 ipsec_latch_t *ipl; 21952 21953 ipl = connp->conn_latch; 21954 if (ipl == NULL) 21955 return (0); 21956 21957 if (ipl->ipl_out_policy == NULL) 21958 return (0); 21959 21960 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 21961 } 21962 21963 /* 21964 * Returns an estimate of the IPsec headers size. This is used if 21965 * we don't want to call into IPsec to get the exact size. 21966 */ 21967 int 21968 ipsec_out_extra_length(mblk_t *ipsec_mp) 21969 { 21970 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21971 ipsec_action_t *a; 21972 21973 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21974 if (!io->ipsec_out_secure) 21975 return (0); 21976 21977 a = io->ipsec_out_act; 21978 21979 if (a == NULL) { 21980 ASSERT(io->ipsec_out_policy != NULL); 21981 a = io->ipsec_out_policy->ipsp_act; 21982 } 21983 ASSERT(a != NULL); 21984 21985 return (a->ipa_ovhd); 21986 } 21987 21988 /* 21989 * Returns an estimate of the IPsec headers size. This is used if 21990 * we don't want to call into IPsec to get the exact size. 21991 */ 21992 int 21993 ipsec_in_extra_length(mblk_t *ipsec_mp) 21994 { 21995 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21996 ipsec_action_t *a; 21997 21998 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21999 22000 a = ii->ipsec_in_action; 22001 return (a == NULL ? 0 : a->ipa_ovhd); 22002 } 22003 22004 /* 22005 * If there are any source route options, return the true final 22006 * destination. Otherwise, return the destination. 22007 */ 22008 ipaddr_t 22009 ip_get_dst(ipha_t *ipha) 22010 { 22011 ipoptp_t opts; 22012 uchar_t *opt; 22013 uint8_t optval; 22014 uint8_t optlen; 22015 ipaddr_t dst; 22016 uint32_t off; 22017 22018 dst = ipha->ipha_dst; 22019 22020 if (IS_SIMPLE_IPH(ipha)) 22021 return (dst); 22022 22023 for (optval = ipoptp_first(&opts, ipha); 22024 optval != IPOPT_EOL; 22025 optval = ipoptp_next(&opts)) { 22026 opt = opts.ipoptp_cur; 22027 optlen = opts.ipoptp_len; 22028 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 22029 switch (optval) { 22030 case IPOPT_SSRR: 22031 case IPOPT_LSRR: 22032 off = opt[IPOPT_OFFSET]; 22033 /* 22034 * If one of the conditions is true, it means 22035 * end of options and dst already has the right 22036 * value. 22037 */ 22038 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 22039 off = optlen - IP_ADDR_LEN; 22040 bcopy(&opt[off], &dst, IP_ADDR_LEN); 22041 } 22042 return (dst); 22043 default: 22044 break; 22045 } 22046 } 22047 22048 return (dst); 22049 } 22050 22051 mblk_t * 22052 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 22053 conn_t *connp, boolean_t unspec_src, zoneid_t zoneid) 22054 { 22055 ipsec_out_t *io; 22056 mblk_t *first_mp; 22057 boolean_t policy_present; 22058 ip_stack_t *ipst; 22059 ipsec_stack_t *ipss; 22060 22061 ASSERT(ire != NULL); 22062 ipst = ire->ire_ipst; 22063 ipss = ipst->ips_netstack->netstack_ipsec; 22064 22065 first_mp = mp; 22066 if (mp->b_datap->db_type == M_CTL) { 22067 io = (ipsec_out_t *)first_mp->b_rptr; 22068 /* 22069 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 22070 * 22071 * 1) There is per-socket policy (including cached global 22072 * policy) or a policy on the IP-in-IP tunnel. 22073 * 2) There is no per-socket policy, but it is 22074 * a multicast packet that needs to go out 22075 * on a specific interface. This is the case 22076 * where (ip_wput and ip_wput_multicast) attaches 22077 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 22078 * 22079 * In case (2) we check with global policy to 22080 * see if there is a match and set the ill_index 22081 * appropriately so that we can lookup the ire 22082 * properly in ip_wput_ipsec_out. 22083 */ 22084 22085 /* 22086 * ipsec_out_use_global_policy is set to B_FALSE 22087 * in ipsec_in_to_out(). Refer to that function for 22088 * details. 22089 */ 22090 if ((io->ipsec_out_latch == NULL) && 22091 (io->ipsec_out_use_global_policy)) { 22092 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 22093 ire, connp, unspec_src, zoneid)); 22094 } 22095 if (!io->ipsec_out_secure) { 22096 /* 22097 * If this is not a secure packet, drop 22098 * the IPSEC_OUT mp and treat it as a clear 22099 * packet. This happens when we are sending 22100 * a ICMP reply back to a clear packet. See 22101 * ipsec_in_to_out() for details. 22102 */ 22103 mp = first_mp->b_cont; 22104 freeb(first_mp); 22105 } 22106 return (mp); 22107 } 22108 /* 22109 * See whether we need to attach a global policy here. We 22110 * don't depend on the conn (as it could be null) for deciding 22111 * what policy this datagram should go through because it 22112 * should have happened in ip_wput if there was some 22113 * policy. This normally happens for connections which are not 22114 * fully bound preventing us from caching policies in 22115 * ip_bind. Packets coming from the TCP listener/global queue 22116 * - which are non-hard_bound - could also be affected by 22117 * applying policy here. 22118 * 22119 * If this packet is coming from tcp global queue or listener, 22120 * we will be applying policy here. This may not be *right* 22121 * if these packets are coming from the detached connection as 22122 * it could have gone in clear before. This happens only if a 22123 * TCP connection started when there is no policy and somebody 22124 * added policy before it became detached. Thus packets of the 22125 * detached connection could go out secure and the other end 22126 * would drop it because it will be expecting in clear. The 22127 * converse is not true i.e if somebody starts a TCP 22128 * connection and deletes the policy, all the packets will 22129 * still go out with the policy that existed before deleting 22130 * because ip_unbind sends up policy information which is used 22131 * by TCP on subsequent ip_wputs. The right solution is to fix 22132 * TCP to attach a dummy IPSEC_OUT and set 22133 * ipsec_out_use_global_policy to B_FALSE. As this might 22134 * affect performance for normal cases, we are not doing it. 22135 * Thus, set policy before starting any TCP connections. 22136 * 22137 * NOTE - We might apply policy even for a hard bound connection 22138 * - for which we cached policy in ip_bind - if somebody added 22139 * global policy after we inherited the policy in ip_bind. 22140 * This means that the packets that were going out in clear 22141 * previously would start going secure and hence get dropped 22142 * on the other side. To fix this, TCP attaches a dummy 22143 * ipsec_out and make sure that we don't apply global policy. 22144 */ 22145 if (ipha != NULL) 22146 policy_present = ipss->ipsec_outbound_v4_policy_present; 22147 else 22148 policy_present = ipss->ipsec_outbound_v6_policy_present; 22149 if (!policy_present) 22150 return (mp); 22151 22152 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src, 22153 zoneid)); 22154 } 22155 22156 /* 22157 * This function does the ire_refrele of the ire passed in as the 22158 * argument. As this function looks up more ires i.e broadcast ires, 22159 * it needs to REFRELE them. Currently, for simplicity we don't 22160 * differentiate the one passed in and looked up here. We always 22161 * REFRELE. 22162 * IPQoS Notes: 22163 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 22164 * IPsec packets are done in ipsec_out_process. 22165 */ 22166 void 22167 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller, 22168 zoneid_t zoneid) 22169 { 22170 ipha_t *ipha; 22171 #define rptr ((uchar_t *)ipha) 22172 queue_t *stq; 22173 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 22174 uint32_t v_hlen_tos_len; 22175 uint32_t ttl_protocol; 22176 ipaddr_t src; 22177 ipaddr_t dst; 22178 uint32_t cksum; 22179 ipaddr_t orig_src; 22180 ire_t *ire1; 22181 mblk_t *next_mp; 22182 uint_t hlen; 22183 uint16_t *up; 22184 uint32_t max_frag = ire->ire_max_frag; 22185 ill_t *ill = ire_to_ill(ire); 22186 int clusterwide; 22187 uint16_t ip_hdr_included; /* IP header included by ULP? */ 22188 int ipsec_len; 22189 mblk_t *first_mp; 22190 ipsec_out_t *io; 22191 boolean_t conn_dontroute; /* conn value for multicast */ 22192 boolean_t conn_multicast_loop; /* conn value for multicast */ 22193 boolean_t multicast_forward; /* Should we forward ? */ 22194 boolean_t unspec_src; 22195 ill_t *conn_outgoing_ill = NULL; 22196 ill_t *ire_ill; 22197 ill_t *ire1_ill; 22198 ill_t *out_ill; 22199 uint32_t ill_index = 0; 22200 boolean_t multirt_send = B_FALSE; 22201 int err; 22202 ipxmit_state_t pktxmit_state; 22203 ip_stack_t *ipst = ire->ire_ipst; 22204 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 22205 22206 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 22207 "ip_wput_ire_start: q %p", q); 22208 22209 multicast_forward = B_FALSE; 22210 unspec_src = (connp != NULL && connp->conn_unspec_src); 22211 22212 if (ire->ire_flags & RTF_MULTIRT) { 22213 /* 22214 * Multirouting case. The bucket where ire is stored 22215 * probably holds other RTF_MULTIRT flagged ire 22216 * to the destination. In this call to ip_wput_ire, 22217 * we attempt to send the packet through all 22218 * those ires. Thus, we first ensure that ire is the 22219 * first RTF_MULTIRT ire in the bucket, 22220 * before walking the ire list. 22221 */ 22222 ire_t *first_ire; 22223 irb_t *irb = ire->ire_bucket; 22224 ASSERT(irb != NULL); 22225 22226 /* Make sure we do not omit any multiroute ire. */ 22227 IRB_REFHOLD(irb); 22228 for (first_ire = irb->irb_ire; 22229 first_ire != NULL; 22230 first_ire = first_ire->ire_next) { 22231 if ((first_ire->ire_flags & RTF_MULTIRT) && 22232 (first_ire->ire_addr == ire->ire_addr) && 22233 !(first_ire->ire_marks & 22234 (IRE_MARK_CONDEMNED | IRE_MARK_TESTHIDDEN))) 22235 break; 22236 } 22237 22238 if ((first_ire != NULL) && (first_ire != ire)) { 22239 IRE_REFHOLD(first_ire); 22240 ire_refrele(ire); 22241 ire = first_ire; 22242 ill = ire_to_ill(ire); 22243 } 22244 IRB_REFRELE(irb); 22245 } 22246 22247 /* 22248 * conn_outgoing_ill variable is used only in the broadcast loop. 22249 * for performance we don't grab the mutexs in the fastpath 22250 */ 22251 if (ire->ire_type == IRE_BROADCAST && connp != NULL && 22252 connp->conn_outgoing_ill != NULL) { 22253 conn_outgoing_ill = conn_get_held_ill(connp, 22254 &connp->conn_outgoing_ill, &err); 22255 if (err == ILL_LOOKUP_FAILED) { 22256 ire_refrele(ire); 22257 freemsg(mp); 22258 return; 22259 } 22260 } 22261 22262 if (mp->b_datap->db_type != M_CTL) { 22263 ipha = (ipha_t *)mp->b_rptr; 22264 } else { 22265 io = (ipsec_out_t *)mp->b_rptr; 22266 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22267 ASSERT(zoneid == io->ipsec_out_zoneid); 22268 ASSERT(zoneid != ALL_ZONES); 22269 ipha = (ipha_t *)mp->b_cont->b_rptr; 22270 dst = ipha->ipha_dst; 22271 /* 22272 * For the multicast case, ipsec_out carries conn_dontroute and 22273 * conn_multicast_loop as conn may not be available here. We 22274 * need this for multicast loopback and forwarding which is done 22275 * later in the code. 22276 */ 22277 if (CLASSD(dst)) { 22278 conn_dontroute = io->ipsec_out_dontroute; 22279 conn_multicast_loop = io->ipsec_out_multicast_loop; 22280 /* 22281 * If conn_dontroute is not set or conn_multicast_loop 22282 * is set, we need to do forwarding/loopback. For 22283 * datagrams from ip_wput_multicast, conn_dontroute is 22284 * set to B_TRUE and conn_multicast_loop is set to 22285 * B_FALSE so that we neither do forwarding nor 22286 * loopback. 22287 */ 22288 if (!conn_dontroute || conn_multicast_loop) 22289 multicast_forward = B_TRUE; 22290 } 22291 } 22292 22293 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 22294 ire->ire_zoneid != ALL_ZONES) { 22295 /* 22296 * When a zone sends a packet to another zone, we try to deliver 22297 * the packet under the same conditions as if the destination 22298 * was a real node on the network. To do so, we look for a 22299 * matching route in the forwarding table. 22300 * RTF_REJECT and RTF_BLACKHOLE are handled just like 22301 * ip_newroute() does. 22302 * Note that IRE_LOCAL are special, since they are used 22303 * when the zoneid doesn't match in some cases. This means that 22304 * we need to handle ipha_src differently since ire_src_addr 22305 * belongs to the receiving zone instead of the sending zone. 22306 * When ip_restrict_interzone_loopback is set, then 22307 * ire_cache_lookup() ensures that IRE_LOCAL are only used 22308 * for loopback between zones when the logical "Ethernet" would 22309 * have looped them back. 22310 */ 22311 ire_t *src_ire; 22312 22313 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 22314 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 22315 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE), ipst); 22316 if (src_ire != NULL && 22317 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) && 22318 (!ipst->ips_ip_restrict_interzone_loopback || 22319 ire_local_same_lan(ire, src_ire))) { 22320 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 22321 ipha->ipha_src = src_ire->ire_src_addr; 22322 ire_refrele(src_ire); 22323 } else { 22324 ire_refrele(ire); 22325 if (conn_outgoing_ill != NULL) 22326 ill_refrele(conn_outgoing_ill); 22327 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 22328 if (src_ire != NULL) { 22329 if (src_ire->ire_flags & RTF_BLACKHOLE) { 22330 ire_refrele(src_ire); 22331 freemsg(mp); 22332 return; 22333 } 22334 ire_refrele(src_ire); 22335 } 22336 if (ip_hdr_complete(ipha, zoneid, ipst)) { 22337 /* Failed */ 22338 freemsg(mp); 22339 return; 22340 } 22341 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, zoneid, 22342 ipst); 22343 return; 22344 } 22345 } 22346 22347 if (mp->b_datap->db_type == M_CTL || 22348 ipss->ipsec_outbound_v4_policy_present) { 22349 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 22350 unspec_src, zoneid); 22351 if (mp == NULL) { 22352 ire_refrele(ire); 22353 if (conn_outgoing_ill != NULL) 22354 ill_refrele(conn_outgoing_ill); 22355 return; 22356 } 22357 /* 22358 * Trusted Extensions supports all-zones interfaces, so 22359 * zoneid == ALL_ZONES is valid, but IPsec maps ALL_ZONES to 22360 * the global zone. 22361 */ 22362 if (zoneid == ALL_ZONES && mp->b_datap->db_type == M_CTL) { 22363 io = (ipsec_out_t *)mp->b_rptr; 22364 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22365 zoneid = io->ipsec_out_zoneid; 22366 } 22367 } 22368 22369 first_mp = mp; 22370 ipsec_len = 0; 22371 22372 if (first_mp->b_datap->db_type == M_CTL) { 22373 io = (ipsec_out_t *)first_mp->b_rptr; 22374 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22375 mp = first_mp->b_cont; 22376 ipsec_len = ipsec_out_extra_length(first_mp); 22377 ASSERT(ipsec_len >= 0); 22378 /* We already picked up the zoneid from the M_CTL above */ 22379 ASSERT(zoneid == io->ipsec_out_zoneid); 22380 ASSERT(zoneid != ALL_ZONES); 22381 22382 /* 22383 * Drop M_CTL here if IPsec processing is not needed. 22384 * (Non-IPsec use of M_CTL extracted any information it 22385 * needed above). 22386 */ 22387 if (ipsec_len == 0) { 22388 freeb(first_mp); 22389 first_mp = mp; 22390 } 22391 } 22392 22393 /* 22394 * Fast path for ip_wput_ire 22395 */ 22396 22397 ipha = (ipha_t *)mp->b_rptr; 22398 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 22399 dst = ipha->ipha_dst; 22400 22401 /* 22402 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 22403 * if the socket is a SOCK_RAW type. The transport checksum should 22404 * be provided in the pre-built packet, so we don't need to compute it. 22405 * Also, other application set flags, like DF, should not be altered. 22406 * Other transport MUST pass down zero. 22407 */ 22408 ip_hdr_included = ipha->ipha_ident; 22409 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 22410 22411 if (CLASSD(dst)) { 22412 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 22413 ntohl(dst), 22414 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 22415 ntohl(ire->ire_addr))); 22416 } 22417 22418 /* Macros to extract header fields from data already in registers */ 22419 #ifdef _BIG_ENDIAN 22420 #define V_HLEN (v_hlen_tos_len >> 24) 22421 #define LENGTH (v_hlen_tos_len & 0xFFFF) 22422 #define PROTO (ttl_protocol & 0xFF) 22423 #else 22424 #define V_HLEN (v_hlen_tos_len & 0xFF) 22425 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 22426 #define PROTO (ttl_protocol >> 8) 22427 #endif 22428 22429 orig_src = src = ipha->ipha_src; 22430 /* (The loop back to "another" is explained down below.) */ 22431 another:; 22432 /* 22433 * Assign an ident value for this packet. We assign idents on 22434 * a per destination basis out of the IRE. There could be 22435 * other threads targeting the same destination, so we have to 22436 * arrange for a atomic increment. Note that we use a 32-bit 22437 * atomic add because it has better performance than its 22438 * 16-bit sibling. 22439 * 22440 * If running in cluster mode and if the source address 22441 * belongs to a replicated service then vector through 22442 * cl_inet_ipident vector to allocate ip identifier 22443 * NOTE: This is a contract private interface with the 22444 * clustering group. 22445 */ 22446 clusterwide = 0; 22447 if (cl_inet_ipident) { 22448 ASSERT(cl_inet_isclusterwide); 22449 netstackid_t stack_id = ipst->ips_netstack->netstack_stackid; 22450 22451 if ((*cl_inet_isclusterwide)(stack_id, IPPROTO_IP, 22452 AF_INET, (uint8_t *)(uintptr_t)src, NULL)) { 22453 ipha->ipha_ident = (*cl_inet_ipident)(stack_id, 22454 IPPROTO_IP, AF_INET, (uint8_t *)(uintptr_t)src, 22455 (uint8_t *)(uintptr_t)dst, NULL); 22456 clusterwide = 1; 22457 } 22458 } 22459 if (!clusterwide) { 22460 ipha->ipha_ident = 22461 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 22462 } 22463 22464 #ifndef _BIG_ENDIAN 22465 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 22466 #endif 22467 22468 /* 22469 * Set source address unless sent on an ill or conn_unspec_src is set. 22470 * This is needed to obey conn_unspec_src when packets go through 22471 * ip_newroute + arp. 22472 * Assumes ip_newroute{,_multi} sets the source address as well. 22473 */ 22474 if (src == INADDR_ANY && !unspec_src) { 22475 /* 22476 * Assign the appropriate source address from the IRE if none 22477 * was specified. 22478 */ 22479 ASSERT(ire->ire_ipversion == IPV4_VERSION); 22480 22481 src = ire->ire_src_addr; 22482 if (connp == NULL) { 22483 ip1dbg(("ip_wput_ire: no connp and no src " 22484 "address for dst 0x%x, using src 0x%x\n", 22485 ntohl(dst), 22486 ntohl(src))); 22487 } 22488 ipha->ipha_src = src; 22489 } 22490 stq = ire->ire_stq; 22491 22492 /* 22493 * We only allow ire chains for broadcasts since there will 22494 * be multiple IRE_CACHE entries for the same multicast 22495 * address (one per ipif). 22496 */ 22497 next_mp = NULL; 22498 22499 /* broadcast packet */ 22500 if (ire->ire_type == IRE_BROADCAST) 22501 goto broadcast; 22502 22503 /* loopback ? */ 22504 if (stq == NULL) 22505 goto nullstq; 22506 22507 /* The ill_index for outbound ILL */ 22508 ill_index = Q_TO_INDEX(stq); 22509 22510 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 22511 ttl_protocol = ((uint16_t *)ipha)[4]; 22512 22513 /* pseudo checksum (do it in parts for IP header checksum) */ 22514 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 22515 22516 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 22517 queue_t *dev_q = stq->q_next; 22518 22519 /* 22520 * For DIRECT_CAPABLE, we do flow control at 22521 * the time of sending the packet. See 22522 * ILL_SEND_TX(). 22523 */ 22524 if (!ILL_DIRECT_CAPABLE((ill_t *)stq->q_ptr) && 22525 (DEV_Q_FLOW_BLOCKED(dev_q))) 22526 goto blocked; 22527 22528 if ((PROTO == IPPROTO_UDP) && 22529 (ip_hdr_included != IP_HDR_INCLUDED)) { 22530 hlen = (V_HLEN & 0xF) << 2; 22531 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22532 if (*up != 0) { 22533 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 22534 hlen, LENGTH, max_frag, ipsec_len, cksum); 22535 /* Software checksum? */ 22536 if (DB_CKSUMFLAGS(mp) == 0) { 22537 IP_STAT(ipst, ip_out_sw_cksum); 22538 IP_STAT_UPDATE(ipst, 22539 ip_udp_out_sw_cksum_bytes, 22540 LENGTH - hlen); 22541 } 22542 } 22543 } 22544 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 22545 hlen = (V_HLEN & 0xF) << 2; 22546 if (PROTO == IPPROTO_TCP) { 22547 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22548 /* 22549 * The packet header is processed once and for all, even 22550 * in the multirouting case. We disable hardware 22551 * checksum if the packet is multirouted, as it will be 22552 * replicated via several interfaces, and not all of 22553 * them may have this capability. 22554 */ 22555 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 22556 LENGTH, max_frag, ipsec_len, cksum); 22557 /* Software checksum? */ 22558 if (DB_CKSUMFLAGS(mp) == 0) { 22559 IP_STAT(ipst, ip_out_sw_cksum); 22560 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22561 LENGTH - hlen); 22562 } 22563 } else { 22564 sctp_hdr_t *sctph; 22565 22566 ASSERT(PROTO == IPPROTO_SCTP); 22567 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22568 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22569 /* 22570 * Zero out the checksum field to ensure proper 22571 * checksum calculation. 22572 */ 22573 sctph->sh_chksum = 0; 22574 #ifdef DEBUG 22575 if (!skip_sctp_cksum) 22576 #endif 22577 sctph->sh_chksum = sctp_cksum(mp, hlen); 22578 } 22579 } 22580 22581 /* 22582 * If this is a multicast packet and originated from ip_wput 22583 * we need to do loopback and forwarding checks. If it comes 22584 * from ip_wput_multicast, we SHOULD not do this. 22585 */ 22586 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 22587 22588 /* checksum */ 22589 cksum += ttl_protocol; 22590 22591 /* fragment the packet */ 22592 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 22593 goto fragmentit; 22594 /* 22595 * Don't use frag_flag if packet is pre-built or source 22596 * routed or if multicast (since multicast packets do 22597 * not solicit ICMP "packet too big" messages). 22598 */ 22599 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22600 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22601 !ip_source_route_included(ipha)) && 22602 !CLASSD(ipha->ipha_dst)) 22603 ipha->ipha_fragment_offset_and_flags |= 22604 htons(ire->ire_frag_flag); 22605 22606 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22607 /* calculate IP header checksum */ 22608 cksum += ipha->ipha_ident; 22609 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 22610 cksum += ipha->ipha_fragment_offset_and_flags; 22611 22612 /* IP options present */ 22613 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22614 if (hlen) 22615 goto checksumoptions; 22616 22617 /* calculate hdr checksum */ 22618 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22619 cksum = ~(cksum + (cksum >> 16)); 22620 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22621 } 22622 if (ipsec_len != 0) { 22623 /* 22624 * We will do the rest of the processing after 22625 * we come back from IPsec in ip_wput_ipsec_out(). 22626 */ 22627 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 22628 22629 io = (ipsec_out_t *)first_mp->b_rptr; 22630 io->ipsec_out_ill_index = 22631 ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 22632 ipsec_out_process(q, first_mp, ire, 0); 22633 ire_refrele(ire); 22634 if (conn_outgoing_ill != NULL) 22635 ill_refrele(conn_outgoing_ill); 22636 return; 22637 } 22638 22639 /* 22640 * In most cases, the emission loop below is entered only 22641 * once. Only in the case where the ire holds the 22642 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 22643 * flagged ires in the bucket, and send the packet 22644 * through all crossed RTF_MULTIRT routes. 22645 */ 22646 if (ire->ire_flags & RTF_MULTIRT) { 22647 multirt_send = B_TRUE; 22648 } 22649 do { 22650 if (multirt_send) { 22651 irb_t *irb; 22652 /* 22653 * We are in a multiple send case, need to get 22654 * the next ire and make a duplicate of the packet. 22655 * ire1 holds here the next ire to process in the 22656 * bucket. If multirouting is expected, 22657 * any non-RTF_MULTIRT ire that has the 22658 * right destination address is ignored. 22659 */ 22660 irb = ire->ire_bucket; 22661 ASSERT(irb != NULL); 22662 22663 IRB_REFHOLD(irb); 22664 for (ire1 = ire->ire_next; 22665 ire1 != NULL; 22666 ire1 = ire1->ire_next) { 22667 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 22668 continue; 22669 if (ire1->ire_addr != ire->ire_addr) 22670 continue; 22671 if (ire1->ire_marks & 22672 (IRE_MARK_CONDEMNED | IRE_MARK_TESTHIDDEN)) 22673 continue; 22674 22675 /* Got one */ 22676 IRE_REFHOLD(ire1); 22677 break; 22678 } 22679 IRB_REFRELE(irb); 22680 22681 if (ire1 != NULL) { 22682 next_mp = copyb(mp); 22683 if ((next_mp == NULL) || 22684 ((mp->b_cont != NULL) && 22685 ((next_mp->b_cont = 22686 dupmsg(mp->b_cont)) == NULL))) { 22687 freemsg(next_mp); 22688 next_mp = NULL; 22689 ire_refrele(ire1); 22690 ire1 = NULL; 22691 } 22692 } 22693 22694 /* Last multiroute ire; don't loop anymore. */ 22695 if (ire1 == NULL) { 22696 multirt_send = B_FALSE; 22697 } 22698 } 22699 22700 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 22701 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 22702 mblk_t *, mp); 22703 FW_HOOKS(ipst->ips_ip4_physical_out_event, 22704 ipst->ips_ipv4firewall_physical_out, 22705 NULL, ire->ire_ipif->ipif_ill, ipha, mp, mp, 0, ipst); 22706 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 22707 22708 if (mp == NULL) 22709 goto release_ire_and_ill; 22710 22711 if (ipst->ips_ipobs_enabled) { 22712 zoneid_t szone; 22713 22714 /* 22715 * On the outbound path the destination zone will be 22716 * unknown as we're sending this packet out on the 22717 * wire. 22718 */ 22719 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, ipst, 22720 ALL_ZONES); 22721 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES, 22722 ire->ire_ipif->ipif_ill, IPV4_VERSION, 0, ipst); 22723 } 22724 mp->b_prev = SET_BPREV_FLAG(IPP_LOCAL_OUT); 22725 DTRACE_PROBE2(ip__xmit__1, mblk_t *, mp, ire_t *, ire); 22726 22727 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_TRUE, connp); 22728 22729 if ((pktxmit_state == SEND_FAILED) || 22730 (pktxmit_state == LLHDR_RESLV_FAILED)) { 22731 ip2dbg(("ip_wput_ire: ip_xmit_v4 failed" 22732 "- packet dropped\n")); 22733 release_ire_and_ill: 22734 ire_refrele(ire); 22735 if (next_mp != NULL) { 22736 freemsg(next_mp); 22737 ire_refrele(ire1); 22738 } 22739 if (conn_outgoing_ill != NULL) 22740 ill_refrele(conn_outgoing_ill); 22741 return; 22742 } 22743 22744 if (CLASSD(dst)) { 22745 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastPkts); 22746 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastOctets, 22747 LENGTH); 22748 } 22749 22750 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22751 "ip_wput_ire_end: q %p (%S)", 22752 q, "last copy out"); 22753 IRE_REFRELE(ire); 22754 22755 if (multirt_send) { 22756 ASSERT(ire1); 22757 /* 22758 * Proceed with the next RTF_MULTIRT ire, 22759 * Also set up the send-to queue accordingly. 22760 */ 22761 ire = ire1; 22762 ire1 = NULL; 22763 stq = ire->ire_stq; 22764 mp = next_mp; 22765 next_mp = NULL; 22766 ipha = (ipha_t *)mp->b_rptr; 22767 ill_index = Q_TO_INDEX(stq); 22768 ill = (ill_t *)stq->q_ptr; 22769 } 22770 } while (multirt_send); 22771 if (conn_outgoing_ill != NULL) 22772 ill_refrele(conn_outgoing_ill); 22773 return; 22774 22775 /* 22776 * ire->ire_type == IRE_BROADCAST (minimize diffs) 22777 */ 22778 broadcast: 22779 { 22780 /* 22781 * To avoid broadcast storms, we usually set the TTL to 1 for 22782 * broadcasts. However, if SO_DONTROUTE isn't set, this value 22783 * can be overridden stack-wide through the ip_broadcast_ttl 22784 * ndd tunable, or on a per-connection basis through the 22785 * IP_BROADCAST_TTL socket option. 22786 * 22787 * In the event that we are replying to incoming ICMP packets, 22788 * connp could be NULL. 22789 */ 22790 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl; 22791 if (connp != NULL) { 22792 if (connp->conn_dontroute) 22793 ipha->ipha_ttl = 1; 22794 else if (connp->conn_broadcast_ttl != 0) 22795 ipha->ipha_ttl = connp->conn_broadcast_ttl; 22796 } 22797 22798 /* 22799 * Note that we are not doing a IRB_REFHOLD here. 22800 * Actually we don't care if the list changes i.e 22801 * if somebody deletes an IRE from the list while 22802 * we drop the lock, the next time we come around 22803 * ire_next will be NULL and hence we won't send 22804 * out multiple copies which is fine. 22805 */ 22806 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 22807 ire1 = ire->ire_next; 22808 if (conn_outgoing_ill != NULL) { 22809 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 22810 ASSERT(ire1 == ire->ire_next); 22811 if (ire1 != NULL && ire1->ire_addr == dst) { 22812 ire_refrele(ire); 22813 ire = ire1; 22814 IRE_REFHOLD(ire); 22815 ire1 = ire->ire_next; 22816 continue; 22817 } 22818 rw_exit(&ire->ire_bucket->irb_lock); 22819 /* Did not find a matching ill */ 22820 ip1dbg(("ip_wput_ire: broadcast with no " 22821 "matching IP_BOUND_IF ill %s dst %x\n", 22822 conn_outgoing_ill->ill_name, dst)); 22823 freemsg(first_mp); 22824 if (ire != NULL) 22825 ire_refrele(ire); 22826 ill_refrele(conn_outgoing_ill); 22827 return; 22828 } 22829 } else if (ire1 != NULL && ire1->ire_addr == dst) { 22830 /* 22831 * If the next IRE has the same address and is not one 22832 * of the two copies that we need to send, try to see 22833 * whether this copy should be sent at all. This 22834 * assumes that we insert loopbacks first and then 22835 * non-loopbacks. This is acheived by inserting the 22836 * loopback always before non-loopback. 22837 * This is used to send a single copy of a broadcast 22838 * packet out all physical interfaces that have an 22839 * matching IRE_BROADCAST while also looping 22840 * back one copy (to ip_wput_local) for each 22841 * matching physical interface. However, we avoid 22842 * sending packets out different logical that match by 22843 * having ipif_up/ipif_down supress duplicate 22844 * IRE_BROADCASTS. 22845 * 22846 * This feature is currently used to get broadcasts 22847 * sent to multiple interfaces, when the broadcast 22848 * address being used applies to multiple interfaces. 22849 * For example, a whole net broadcast will be 22850 * replicated on every connected subnet of 22851 * the target net. 22852 * 22853 * Each zone has its own set of IRE_BROADCASTs, so that 22854 * we're able to distribute inbound packets to multiple 22855 * zones who share a broadcast address. We avoid looping 22856 * back outbound packets in different zones but on the 22857 * same ill, as the application would see duplicates. 22858 * 22859 * This logic assumes that ire_add_v4() groups the 22860 * IRE_BROADCAST entries so that those with the same 22861 * ire_addr are kept together. 22862 */ 22863 ire_ill = ire->ire_ipif->ipif_ill; 22864 if (ire->ire_stq != NULL || ire1->ire_stq == NULL) { 22865 while (ire1 != NULL && ire1->ire_addr == dst) { 22866 ire1_ill = ire1->ire_ipif->ipif_ill; 22867 if (ire1_ill != ire_ill) 22868 break; 22869 ire1 = ire1->ire_next; 22870 } 22871 } 22872 } 22873 ASSERT(multirt_send == B_FALSE); 22874 if (ire1 != NULL && ire1->ire_addr == dst) { 22875 if ((ire->ire_flags & RTF_MULTIRT) && 22876 (ire1->ire_flags & RTF_MULTIRT)) { 22877 /* 22878 * We are in the multirouting case. 22879 * The message must be sent at least 22880 * on both ires. These ires have been 22881 * inserted AFTER the standard ones 22882 * in ip_rt_add(). There are thus no 22883 * other ire entries for the destination 22884 * address in the rest of the bucket 22885 * that do not have the RTF_MULTIRT 22886 * flag. We don't process a copy 22887 * of the message here. This will be 22888 * done in the final sending loop. 22889 */ 22890 multirt_send = B_TRUE; 22891 } else { 22892 next_mp = ip_copymsg(first_mp); 22893 if (next_mp != NULL) 22894 IRE_REFHOLD(ire1); 22895 } 22896 } 22897 rw_exit(&ire->ire_bucket->irb_lock); 22898 } 22899 22900 if (stq) { 22901 /* 22902 * A non-NULL send-to queue means this packet is going 22903 * out of this machine. 22904 */ 22905 out_ill = (ill_t *)stq->q_ptr; 22906 22907 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutRequests); 22908 ttl_protocol = ((uint16_t *)ipha)[4]; 22909 /* 22910 * We accumulate the pseudo header checksum in cksum. 22911 * This is pretty hairy code, so watch close. One 22912 * thing to keep in mind is that UDP and TCP have 22913 * stored their respective datagram lengths in their 22914 * checksum fields. This lines things up real nice. 22915 */ 22916 cksum = (dst >> 16) + (dst & 0xFFFF) + 22917 (src >> 16) + (src & 0xFFFF); 22918 /* 22919 * We assume the udp checksum field contains the 22920 * length, so to compute the pseudo header checksum, 22921 * all we need is the protocol number and src/dst. 22922 */ 22923 /* Provide the checksums for UDP and TCP. */ 22924 if ((PROTO == IPPROTO_TCP) && 22925 (ip_hdr_included != IP_HDR_INCLUDED)) { 22926 /* hlen gets the number of uchar_ts in the IP header */ 22927 hlen = (V_HLEN & 0xF) << 2; 22928 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22929 IP_STAT(ipst, ip_out_sw_cksum); 22930 IP_STAT_UPDATE(ipst, ip_tcp_out_sw_cksum_bytes, 22931 LENGTH - hlen); 22932 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 22933 } else if (PROTO == IPPROTO_SCTP && 22934 (ip_hdr_included != IP_HDR_INCLUDED)) { 22935 sctp_hdr_t *sctph; 22936 22937 hlen = (V_HLEN & 0xF) << 2; 22938 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22939 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22940 sctph->sh_chksum = 0; 22941 #ifdef DEBUG 22942 if (!skip_sctp_cksum) 22943 #endif 22944 sctph->sh_chksum = sctp_cksum(mp, hlen); 22945 } else { 22946 queue_t *dev_q = stq->q_next; 22947 22948 if (!ILL_DIRECT_CAPABLE((ill_t *)stq->q_ptr) && 22949 (DEV_Q_FLOW_BLOCKED(dev_q))) { 22950 blocked: 22951 ipha->ipha_ident = ip_hdr_included; 22952 /* 22953 * If we don't have a conn to apply 22954 * backpressure, free the message. 22955 * In the ire_send path, we don't know 22956 * the position to requeue the packet. Rather 22957 * than reorder packets, we just drop this 22958 * packet. 22959 */ 22960 if (ipst->ips_ip_output_queue && 22961 connp != NULL && 22962 caller != IRE_SEND) { 22963 if (caller == IP_WSRV) { 22964 idl_tx_list_t *idl_txl; 22965 22966 idl_txl = 22967 &ipst->ips_idl_tx_list[0]; 22968 connp->conn_did_putbq = 1; 22969 (void) putbq(connp->conn_wq, 22970 first_mp); 22971 conn_drain_insert(connp, 22972 idl_txl); 22973 /* 22974 * This is the service thread, 22975 * and the queue is already 22976 * noenabled. The check for 22977 * canput and the putbq is not 22978 * atomic. So we need to check 22979 * again. 22980 */ 22981 if (canput(stq->q_next)) 22982 connp->conn_did_putbq 22983 = 0; 22984 IP_STAT(ipst, ip_conn_flputbq); 22985 } else { 22986 /* 22987 * We are not the service proc. 22988 * ip_wsrv will be scheduled or 22989 * is already running. 22990 */ 22991 22992 (void) putq(connp->conn_wq, 22993 first_mp); 22994 } 22995 } else { 22996 out_ill = (ill_t *)stq->q_ptr; 22997 BUMP_MIB(out_ill->ill_ip_mib, 22998 ipIfStatsOutDiscards); 22999 freemsg(first_mp); 23000 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23001 "ip_wput_ire_end: q %p (%S)", 23002 q, "discard"); 23003 } 23004 ire_refrele(ire); 23005 if (next_mp) { 23006 ire_refrele(ire1); 23007 freemsg(next_mp); 23008 } 23009 if (conn_outgoing_ill != NULL) 23010 ill_refrele(conn_outgoing_ill); 23011 return; 23012 } 23013 if ((PROTO == IPPROTO_UDP) && 23014 (ip_hdr_included != IP_HDR_INCLUDED)) { 23015 /* 23016 * hlen gets the number of uchar_ts in the 23017 * IP header 23018 */ 23019 hlen = (V_HLEN & 0xF) << 2; 23020 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 23021 max_frag = ire->ire_max_frag; 23022 if (*up != 0) { 23023 IP_CKSUM_XMIT(out_ill, ire, mp, ipha, 23024 up, PROTO, hlen, LENGTH, max_frag, 23025 ipsec_len, cksum); 23026 /* Software checksum? */ 23027 if (DB_CKSUMFLAGS(mp) == 0) { 23028 IP_STAT(ipst, ip_out_sw_cksum); 23029 IP_STAT_UPDATE(ipst, 23030 ip_udp_out_sw_cksum_bytes, 23031 LENGTH - hlen); 23032 } 23033 } 23034 } 23035 } 23036 /* 23037 * Need to do this even when fragmenting. The local 23038 * loopback can be done without computing checksums 23039 * but forwarding out other interface must be done 23040 * after the IP checksum (and ULP checksums) have been 23041 * computed. 23042 * 23043 * NOTE : multicast_forward is set only if this packet 23044 * originated from ip_wput. For packets originating from 23045 * ip_wput_multicast, it is not set. 23046 */ 23047 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 23048 multi_loopback: 23049 ip2dbg(("ip_wput: multicast, loop %d\n", 23050 conn_multicast_loop)); 23051 23052 /* Forget header checksum offload */ 23053 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 23054 23055 /* 23056 * Local loopback of multicasts? Check the 23057 * ill. 23058 * 23059 * Note that the loopback function will not come 23060 * in through ip_rput - it will only do the 23061 * client fanout thus we need to do an mforward 23062 * as well. The is different from the BSD 23063 * logic. 23064 */ 23065 if (ill != NULL) { 23066 if (ilm_lookup_ill(ill, ipha->ipha_dst, 23067 ALL_ZONES) != NULL) { 23068 /* 23069 * Pass along the virtual output q. 23070 * ip_wput_local() will distribute the 23071 * packet to all the matching zones, 23072 * except the sending zone when 23073 * IP_MULTICAST_LOOP is false. 23074 */ 23075 ip_multicast_loopback(q, ill, first_mp, 23076 conn_multicast_loop ? 0 : 23077 IP_FF_NO_MCAST_LOOP, zoneid); 23078 } 23079 } 23080 if (ipha->ipha_ttl == 0) { 23081 /* 23082 * 0 => only to this host i.e. we are 23083 * done. We are also done if this was the 23084 * loopback interface since it is sufficient 23085 * to loopback one copy of a multicast packet. 23086 */ 23087 freemsg(first_mp); 23088 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23089 "ip_wput_ire_end: q %p (%S)", 23090 q, "loopback"); 23091 ire_refrele(ire); 23092 if (conn_outgoing_ill != NULL) 23093 ill_refrele(conn_outgoing_ill); 23094 return; 23095 } 23096 /* 23097 * ILLF_MULTICAST is checked in ip_newroute 23098 * i.e. we don't need to check it here since 23099 * all IRE_CACHEs come from ip_newroute. 23100 * For multicast traffic, SO_DONTROUTE is interpreted 23101 * to mean only send the packet out the interface 23102 * (optionally specified with IP_MULTICAST_IF) 23103 * and do not forward it out additional interfaces. 23104 * RSVP and the rsvp daemon is an example of a 23105 * protocol and user level process that 23106 * handles it's own routing. Hence, it uses the 23107 * SO_DONTROUTE option to accomplish this. 23108 */ 23109 23110 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 23111 ill != NULL) { 23112 /* Unconditionally redo the checksum */ 23113 ipha->ipha_hdr_checksum = 0; 23114 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23115 23116 /* 23117 * If this needs to go out secure, we need 23118 * to wait till we finish the IPsec 23119 * processing. 23120 */ 23121 if (ipsec_len == 0 && 23122 ip_mforward(ill, ipha, mp)) { 23123 freemsg(first_mp); 23124 ip1dbg(("ip_wput: mforward failed\n")); 23125 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23126 "ip_wput_ire_end: q %p (%S)", 23127 q, "mforward failed"); 23128 ire_refrele(ire); 23129 if (conn_outgoing_ill != NULL) 23130 ill_refrele(conn_outgoing_ill); 23131 return; 23132 } 23133 } 23134 } 23135 max_frag = ire->ire_max_frag; 23136 cksum += ttl_protocol; 23137 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 23138 /* No fragmentation required for this one. */ 23139 /* 23140 * Don't use frag_flag if packet is pre-built or source 23141 * routed or if multicast (since multicast packets do 23142 * not solicit ICMP "packet too big" messages). 23143 */ 23144 if ((ip_hdr_included != IP_HDR_INCLUDED) && 23145 (V_HLEN == IP_SIMPLE_HDR_VERSION || 23146 !ip_source_route_included(ipha)) && 23147 !CLASSD(ipha->ipha_dst)) 23148 ipha->ipha_fragment_offset_and_flags |= 23149 htons(ire->ire_frag_flag); 23150 23151 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 23152 /* Complete the IP header checksum. */ 23153 cksum += ipha->ipha_ident; 23154 cksum += (v_hlen_tos_len >> 16)+ 23155 (v_hlen_tos_len & 0xFFFF); 23156 cksum += ipha->ipha_fragment_offset_and_flags; 23157 hlen = (V_HLEN & 0xF) - 23158 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 23159 if (hlen) { 23160 checksumoptions: 23161 /* 23162 * Account for the IP Options in the IP 23163 * header checksum. 23164 */ 23165 up = (uint16_t *)(rptr+ 23166 IP_SIMPLE_HDR_LENGTH); 23167 do { 23168 cksum += up[0]; 23169 cksum += up[1]; 23170 up += 2; 23171 } while (--hlen); 23172 } 23173 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 23174 cksum = ~(cksum + (cksum >> 16)); 23175 ipha->ipha_hdr_checksum = (uint16_t)cksum; 23176 } 23177 if (ipsec_len != 0) { 23178 ipsec_out_process(q, first_mp, ire, ill_index); 23179 if (!next_mp) { 23180 ire_refrele(ire); 23181 if (conn_outgoing_ill != NULL) 23182 ill_refrele(conn_outgoing_ill); 23183 return; 23184 } 23185 goto next; 23186 } 23187 23188 /* 23189 * multirt_send has already been handled 23190 * for broadcast, but not yet for multicast 23191 * or IP options. 23192 */ 23193 if (next_mp == NULL) { 23194 if (ire->ire_flags & RTF_MULTIRT) { 23195 multirt_send = B_TRUE; 23196 } 23197 } 23198 23199 /* 23200 * In most cases, the emission loop below is 23201 * entered only once. Only in the case where 23202 * the ire holds the RTF_MULTIRT flag, do we loop 23203 * to process all RTF_MULTIRT ires in the bucket, 23204 * and send the packet through all crossed 23205 * RTF_MULTIRT routes. 23206 */ 23207 do { 23208 if (multirt_send) { 23209 irb_t *irb; 23210 23211 irb = ire->ire_bucket; 23212 ASSERT(irb != NULL); 23213 /* 23214 * We are in a multiple send case, 23215 * need to get the next IRE and make 23216 * a duplicate of the packet. 23217 */ 23218 IRB_REFHOLD(irb); 23219 for (ire1 = ire->ire_next; 23220 ire1 != NULL; 23221 ire1 = ire1->ire_next) { 23222 if (!(ire1->ire_flags & 23223 RTF_MULTIRT)) 23224 continue; 23225 23226 if (ire1->ire_addr != 23227 ire->ire_addr) 23228 continue; 23229 23230 if (ire1->ire_marks & 23231 (IRE_MARK_CONDEMNED | 23232 IRE_MARK_TESTHIDDEN)) 23233 continue; 23234 23235 /* Got one */ 23236 IRE_REFHOLD(ire1); 23237 break; 23238 } 23239 IRB_REFRELE(irb); 23240 23241 if (ire1 != NULL) { 23242 next_mp = copyb(mp); 23243 if ((next_mp == NULL) || 23244 ((mp->b_cont != NULL) && 23245 ((next_mp->b_cont = 23246 dupmsg(mp->b_cont)) 23247 == NULL))) { 23248 freemsg(next_mp); 23249 next_mp = NULL; 23250 ire_refrele(ire1); 23251 ire1 = NULL; 23252 } 23253 } 23254 23255 /* 23256 * Last multiroute ire; don't loop 23257 * anymore. The emission is over 23258 * and next_mp is NULL. 23259 */ 23260 if (ire1 == NULL) { 23261 multirt_send = B_FALSE; 23262 } 23263 } 23264 23265 out_ill = ire_to_ill(ire); 23266 DTRACE_PROBE4(ip4__physical__out__start, 23267 ill_t *, NULL, 23268 ill_t *, out_ill, 23269 ipha_t *, ipha, mblk_t *, mp); 23270 FW_HOOKS(ipst->ips_ip4_physical_out_event, 23271 ipst->ips_ipv4firewall_physical_out, 23272 NULL, out_ill, ipha, mp, mp, 0, ipst); 23273 DTRACE_PROBE1(ip4__physical__out__end, 23274 mblk_t *, mp); 23275 if (mp == NULL) 23276 goto release_ire_and_ill_2; 23277 23278 ASSERT(ipsec_len == 0); 23279 mp->b_prev = 23280 SET_BPREV_FLAG(IPP_LOCAL_OUT); 23281 DTRACE_PROBE2(ip__xmit__2, 23282 mblk_t *, mp, ire_t *, ire); 23283 pktxmit_state = ip_xmit_v4(mp, ire, 23284 NULL, B_TRUE, connp); 23285 if ((pktxmit_state == SEND_FAILED) || 23286 (pktxmit_state == LLHDR_RESLV_FAILED)) { 23287 release_ire_and_ill_2: 23288 if (next_mp) { 23289 freemsg(next_mp); 23290 ire_refrele(ire1); 23291 } 23292 ire_refrele(ire); 23293 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23294 "ip_wput_ire_end: q %p (%S)", 23295 q, "discard MDATA"); 23296 if (conn_outgoing_ill != NULL) 23297 ill_refrele(conn_outgoing_ill); 23298 return; 23299 } 23300 23301 if (CLASSD(dst)) { 23302 BUMP_MIB(out_ill->ill_ip_mib, 23303 ipIfStatsHCOutMcastPkts); 23304 UPDATE_MIB(out_ill->ill_ip_mib, 23305 ipIfStatsHCOutMcastOctets, 23306 LENGTH); 23307 } else if (ire->ire_type == IRE_BROADCAST) { 23308 BUMP_MIB(out_ill->ill_ip_mib, 23309 ipIfStatsHCOutBcastPkts); 23310 } 23311 23312 if (multirt_send) { 23313 /* 23314 * We are in a multiple send case, 23315 * need to re-enter the sending loop 23316 * using the next ire. 23317 */ 23318 ire_refrele(ire); 23319 ire = ire1; 23320 stq = ire->ire_stq; 23321 mp = next_mp; 23322 next_mp = NULL; 23323 ipha = (ipha_t *)mp->b_rptr; 23324 ill_index = Q_TO_INDEX(stq); 23325 } 23326 } while (multirt_send); 23327 23328 if (!next_mp) { 23329 /* 23330 * Last copy going out (the ultra-common 23331 * case). Note that we intentionally replicate 23332 * the putnext rather than calling it before 23333 * the next_mp check in hopes of a little 23334 * tail-call action out of the compiler. 23335 */ 23336 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23337 "ip_wput_ire_end: q %p (%S)", 23338 q, "last copy out(1)"); 23339 ire_refrele(ire); 23340 if (conn_outgoing_ill != NULL) 23341 ill_refrele(conn_outgoing_ill); 23342 return; 23343 } 23344 /* More copies going out below. */ 23345 } else { 23346 int offset; 23347 fragmentit: 23348 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23349 /* 23350 * If this would generate a icmp_frag_needed message, 23351 * we need to handle it before we do the IPsec 23352 * processing. Otherwise, we need to strip the IPsec 23353 * headers before we send up the message to the ULPs 23354 * which becomes messy and difficult. 23355 */ 23356 if (ipsec_len != 0) { 23357 if ((max_frag < (unsigned int)(LENGTH + 23358 ipsec_len)) && (offset & IPH_DF)) { 23359 out_ill = (ill_t *)stq->q_ptr; 23360 BUMP_MIB(out_ill->ill_ip_mib, 23361 ipIfStatsOutFragFails); 23362 BUMP_MIB(out_ill->ill_ip_mib, 23363 ipIfStatsOutFragReqds); 23364 ipha->ipha_hdr_checksum = 0; 23365 ipha->ipha_hdr_checksum = 23366 (uint16_t)ip_csum_hdr(ipha); 23367 icmp_frag_needed(ire->ire_stq, first_mp, 23368 max_frag, zoneid, ipst); 23369 if (!next_mp) { 23370 ire_refrele(ire); 23371 if (conn_outgoing_ill != NULL) { 23372 ill_refrele( 23373 conn_outgoing_ill); 23374 } 23375 return; 23376 } 23377 } else { 23378 /* 23379 * This won't cause a icmp_frag_needed 23380 * message. to be generated. Send it on 23381 * the wire. Note that this could still 23382 * cause fragmentation and all we 23383 * do is the generation of the message 23384 * to the ULP if needed before IPsec. 23385 */ 23386 if (!next_mp) { 23387 ipsec_out_process(q, first_mp, 23388 ire, ill_index); 23389 TRACE_2(TR_FAC_IP, 23390 TR_IP_WPUT_IRE_END, 23391 "ip_wput_ire_end: q %p " 23392 "(%S)", q, 23393 "last ipsec_out_process"); 23394 ire_refrele(ire); 23395 if (conn_outgoing_ill != NULL) { 23396 ill_refrele( 23397 conn_outgoing_ill); 23398 } 23399 return; 23400 } 23401 ipsec_out_process(q, first_mp, 23402 ire, ill_index); 23403 } 23404 } else { 23405 /* 23406 * Initiate IPPF processing. For 23407 * fragmentable packets we finish 23408 * all QOS packet processing before 23409 * calling: 23410 * ip_wput_ire_fragmentit->ip_wput_frag 23411 */ 23412 23413 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23414 ip_process(IPP_LOCAL_OUT, &mp, 23415 ill_index); 23416 if (mp == NULL) { 23417 out_ill = (ill_t *)stq->q_ptr; 23418 BUMP_MIB(out_ill->ill_ip_mib, 23419 ipIfStatsOutDiscards); 23420 if (next_mp != NULL) { 23421 freemsg(next_mp); 23422 ire_refrele(ire1); 23423 } 23424 ire_refrele(ire); 23425 TRACE_2(TR_FAC_IP, 23426 TR_IP_WPUT_IRE_END, 23427 "ip_wput_ire: q %p (%S)", 23428 q, "discard MDATA"); 23429 if (conn_outgoing_ill != NULL) { 23430 ill_refrele( 23431 conn_outgoing_ill); 23432 } 23433 return; 23434 } 23435 } 23436 if (!next_mp) { 23437 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23438 "ip_wput_ire_end: q %p (%S)", 23439 q, "last fragmentation"); 23440 ip_wput_ire_fragmentit(mp, ire, 23441 zoneid, ipst, connp); 23442 ire_refrele(ire); 23443 if (conn_outgoing_ill != NULL) 23444 ill_refrele(conn_outgoing_ill); 23445 return; 23446 } 23447 ip_wput_ire_fragmentit(mp, ire, 23448 zoneid, ipst, connp); 23449 } 23450 } 23451 } else { 23452 nullstq: 23453 /* A NULL stq means the destination address is local. */ 23454 UPDATE_OB_PKT_COUNT(ire); 23455 ire->ire_last_used_time = lbolt; 23456 ASSERT(ire->ire_ipif != NULL); 23457 if (!next_mp) { 23458 /* 23459 * Is there an "in" and "out" for traffic local 23460 * to a host (loopback)? The code in Solaris doesn't 23461 * explicitly draw a line in its code for in vs out, 23462 * so we've had to draw a line in the sand: ip_wput_ire 23463 * is considered to be the "output" side and 23464 * ip_wput_local to be the "input" side. 23465 */ 23466 out_ill = ire_to_ill(ire); 23467 23468 /* 23469 * DTrace this as ip:::send. A blocked packet will 23470 * fire the send probe, but not the receive probe. 23471 */ 23472 DTRACE_IP7(send, mblk_t *, first_mp, conn_t *, NULL, 23473 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, out_ill, 23474 ipha_t *, ipha, ip6_t *, NULL, int, 1); 23475 23476 DTRACE_PROBE4(ip4__loopback__out__start, 23477 ill_t *, NULL, ill_t *, out_ill, 23478 ipha_t *, ipha, mblk_t *, first_mp); 23479 23480 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23481 ipst->ips_ipv4firewall_loopback_out, 23482 NULL, out_ill, ipha, first_mp, mp, 0, ipst); 23483 23484 DTRACE_PROBE1(ip4__loopback__out_end, 23485 mblk_t *, first_mp); 23486 23487 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23488 "ip_wput_ire_end: q %p (%S)", 23489 q, "local address"); 23490 23491 if (first_mp != NULL) 23492 ip_wput_local(q, out_ill, ipha, 23493 first_mp, ire, 0, ire->ire_zoneid); 23494 ire_refrele(ire); 23495 if (conn_outgoing_ill != NULL) 23496 ill_refrele(conn_outgoing_ill); 23497 return; 23498 } 23499 23500 out_ill = ire_to_ill(ire); 23501 23502 /* 23503 * DTrace this as ip:::send. A blocked packet will fire the 23504 * send probe, but not the receive probe. 23505 */ 23506 DTRACE_IP7(send, mblk_t *, first_mp, conn_t *, NULL, 23507 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, out_ill, 23508 ipha_t *, ipha, ip6_t *, NULL, int, 1); 23509 23510 DTRACE_PROBE4(ip4__loopback__out__start, 23511 ill_t *, NULL, ill_t *, out_ill, 23512 ipha_t *, ipha, mblk_t *, first_mp); 23513 23514 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 23515 ipst->ips_ipv4firewall_loopback_out, 23516 NULL, out_ill, ipha, first_mp, mp, 0, ipst); 23517 23518 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, first_mp); 23519 23520 if (first_mp != NULL) 23521 ip_wput_local(q, out_ill, ipha, 23522 first_mp, ire, 0, ire->ire_zoneid); 23523 } 23524 next: 23525 /* 23526 * More copies going out to additional interfaces. 23527 * ire1 has already been held. We don't need the 23528 * "ire" anymore. 23529 */ 23530 ire_refrele(ire); 23531 ire = ire1; 23532 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 23533 mp = next_mp; 23534 ASSERT(ire->ire_ipversion == IPV4_VERSION); 23535 ill = ire_to_ill(ire); 23536 first_mp = mp; 23537 if (ipsec_len != 0) { 23538 ASSERT(first_mp->b_datap->db_type == M_CTL); 23539 mp = mp->b_cont; 23540 } 23541 dst = ire->ire_addr; 23542 ipha = (ipha_t *)mp->b_rptr; 23543 /* 23544 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 23545 * Restore ipha_ident "no checksum" flag. 23546 */ 23547 src = orig_src; 23548 ipha->ipha_ident = ip_hdr_included; 23549 goto another; 23550 23551 #undef rptr 23552 #undef Q_TO_INDEX 23553 } 23554 23555 /* 23556 * Routine to allocate a message that is used to notify the ULP about MDT. 23557 * The caller may provide a pointer to the link-layer MDT capabilities, 23558 * or NULL if MDT is to be disabled on the stream. 23559 */ 23560 mblk_t * 23561 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 23562 { 23563 mblk_t *mp; 23564 ip_mdt_info_t *mdti; 23565 ill_mdt_capab_t *idst; 23566 23567 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 23568 DB_TYPE(mp) = M_CTL; 23569 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 23570 mdti = (ip_mdt_info_t *)mp->b_rptr; 23571 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 23572 idst = &(mdti->mdt_capab); 23573 23574 /* 23575 * If the caller provides us with the capability, copy 23576 * it over into our notification message; otherwise 23577 * we zero out the capability portion. 23578 */ 23579 if (isrc != NULL) 23580 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23581 else 23582 bzero((caddr_t)idst, sizeof (*idst)); 23583 } 23584 return (mp); 23585 } 23586 23587 /* 23588 * Routine which determines whether MDT can be enabled on the destination 23589 * IRE and IPC combination, and if so, allocates and returns the MDT 23590 * notification mblk that may be used by ULP. We also check if we need to 23591 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 23592 * MDT usage in the past have been lifted. This gets called during IP 23593 * and ULP binding. 23594 */ 23595 mblk_t * 23596 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23597 ill_mdt_capab_t *mdt_cap) 23598 { 23599 mblk_t *mp; 23600 boolean_t rc = B_FALSE; 23601 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23602 23603 ASSERT(dst_ire != NULL); 23604 ASSERT(connp != NULL); 23605 ASSERT(mdt_cap != NULL); 23606 23607 /* 23608 * Currently, we only support simple TCP/{IPv4,IPv6} with 23609 * Multidata, which is handled in tcp_multisend(). This 23610 * is the reason why we do all these checks here, to ensure 23611 * that we don't enable Multidata for the cases which we 23612 * can't handle at the moment. 23613 */ 23614 do { 23615 /* Only do TCP at the moment */ 23616 if (connp->conn_ulp != IPPROTO_TCP) 23617 break; 23618 23619 /* 23620 * IPsec outbound policy present? Note that we get here 23621 * after calling ipsec_conn_cache_policy() where the global 23622 * policy checking is performed. conn_latch will be 23623 * non-NULL as long as there's a policy defined, 23624 * i.e. conn_out_enforce_policy may be NULL in such case 23625 * when the connection is non-secure, and hence we check 23626 * further if the latch refers to an outbound policy. 23627 */ 23628 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 23629 break; 23630 23631 /* CGTP (multiroute) is enabled? */ 23632 if (dst_ire->ire_flags & RTF_MULTIRT) 23633 break; 23634 23635 /* Outbound IPQoS enabled? */ 23636 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23637 /* 23638 * In this case, we disable MDT for this and all 23639 * future connections going over the interface. 23640 */ 23641 mdt_cap->ill_mdt_on = 0; 23642 break; 23643 } 23644 23645 /* socket option(s) present? */ 23646 if (!CONN_IS_LSO_MD_FASTPATH(connp)) 23647 break; 23648 23649 rc = B_TRUE; 23650 /* CONSTCOND */ 23651 } while (0); 23652 23653 /* Remember the result */ 23654 connp->conn_mdt_ok = rc; 23655 23656 if (!rc) 23657 return (NULL); 23658 else if (!mdt_cap->ill_mdt_on) { 23659 /* 23660 * If MDT has been previously turned off in the past, and we 23661 * currently can do MDT (due to IPQoS policy removal, etc.) 23662 * then enable it for this interface. 23663 */ 23664 mdt_cap->ill_mdt_on = 1; 23665 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 23666 "interface %s\n", ill_name)); 23667 } 23668 23669 /* Allocate the MDT info mblk */ 23670 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 23671 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 23672 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23673 return (NULL); 23674 } 23675 return (mp); 23676 } 23677 23678 /* 23679 * Routine to allocate a message that is used to notify the ULP about LSO. 23680 * The caller may provide a pointer to the link-layer LSO capabilities, 23681 * or NULL if LSO is to be disabled on the stream. 23682 */ 23683 mblk_t * 23684 ip_lsoinfo_alloc(ill_lso_capab_t *isrc) 23685 { 23686 mblk_t *mp; 23687 ip_lso_info_t *lsoi; 23688 ill_lso_capab_t *idst; 23689 23690 if ((mp = allocb(sizeof (*lsoi), BPRI_HI)) != NULL) { 23691 DB_TYPE(mp) = M_CTL; 23692 mp->b_wptr = mp->b_rptr + sizeof (*lsoi); 23693 lsoi = (ip_lso_info_t *)mp->b_rptr; 23694 lsoi->lso_info_id = LSO_IOC_INFO_UPDATE; 23695 idst = &(lsoi->lso_capab); 23696 23697 /* 23698 * If the caller provides us with the capability, copy 23699 * it over into our notification message; otherwise 23700 * we zero out the capability portion. 23701 */ 23702 if (isrc != NULL) 23703 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23704 else 23705 bzero((caddr_t)idst, sizeof (*idst)); 23706 } 23707 return (mp); 23708 } 23709 23710 /* 23711 * Routine which determines whether LSO can be enabled on the destination 23712 * IRE and IPC combination, and if so, allocates and returns the LSO 23713 * notification mblk that may be used by ULP. We also check if we need to 23714 * turn LSO back to 'on' when certain restrictions prohibiting us to allow 23715 * LSO usage in the past have been lifted. This gets called during IP 23716 * and ULP binding. 23717 */ 23718 mblk_t * 23719 ip_lsoinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23720 ill_lso_capab_t *lso_cap) 23721 { 23722 mblk_t *mp; 23723 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 23724 23725 ASSERT(dst_ire != NULL); 23726 ASSERT(connp != NULL); 23727 ASSERT(lso_cap != NULL); 23728 23729 connp->conn_lso_ok = B_TRUE; 23730 23731 if ((connp->conn_ulp != IPPROTO_TCP) || 23732 CONN_IPSEC_OUT_ENCAPSULATED(connp) || 23733 (dst_ire->ire_flags & RTF_MULTIRT) || 23734 !CONN_IS_LSO_MD_FASTPATH(connp) || 23735 (IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 23736 connp->conn_lso_ok = B_FALSE; 23737 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 23738 /* 23739 * Disable LSO for this and all future connections going 23740 * over the interface. 23741 */ 23742 lso_cap->ill_lso_on = 0; 23743 } 23744 } 23745 23746 if (!connp->conn_lso_ok) 23747 return (NULL); 23748 else if (!lso_cap->ill_lso_on) { 23749 /* 23750 * If LSO has been previously turned off in the past, and we 23751 * currently can do LSO (due to IPQoS policy removal, etc.) 23752 * then enable it for this interface. 23753 */ 23754 lso_cap->ill_lso_on = 1; 23755 ip1dbg(("ip_mdinfo_return: reenabling LSO for interface %s\n", 23756 ill_name)); 23757 } 23758 23759 /* Allocate the LSO info mblk */ 23760 if ((mp = ip_lsoinfo_alloc(lso_cap)) == NULL) 23761 ip0dbg(("ip_lsoinfo_return: can't enable LSO for " 23762 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23763 23764 return (mp); 23765 } 23766 23767 /* 23768 * Create destination address attribute, and fill it with the physical 23769 * destination address and SAP taken from the template DL_UNITDATA_REQ 23770 * message block. 23771 */ 23772 boolean_t 23773 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 23774 { 23775 dl_unitdata_req_t *dlurp; 23776 pattr_t *pa; 23777 pattrinfo_t pa_info; 23778 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 23779 uint_t das_len, das_off; 23780 23781 ASSERT(dlmp != NULL); 23782 23783 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 23784 das_len = dlurp->dl_dest_addr_length; 23785 das_off = dlurp->dl_dest_addr_offset; 23786 23787 pa_info.type = PATTR_DSTADDRSAP; 23788 pa_info.len = sizeof (**das) + das_len - 1; 23789 23790 /* create and associate the attribute */ 23791 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23792 if (pa != NULL) { 23793 ASSERT(*das != NULL); 23794 (*das)->addr_is_group = 0; 23795 (*das)->addr_len = (uint8_t)das_len; 23796 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 23797 } 23798 23799 return (pa != NULL); 23800 } 23801 23802 /* 23803 * Create hardware checksum attribute and fill it with the values passed. 23804 */ 23805 boolean_t 23806 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 23807 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 23808 { 23809 pattr_t *pa; 23810 pattrinfo_t pa_info; 23811 23812 ASSERT(mmd != NULL); 23813 23814 pa_info.type = PATTR_HCKSUM; 23815 pa_info.len = sizeof (pattr_hcksum_t); 23816 23817 /* create and associate the attribute */ 23818 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23819 if (pa != NULL) { 23820 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 23821 23822 hck->hcksum_start_offset = start_offset; 23823 hck->hcksum_stuff_offset = stuff_offset; 23824 hck->hcksum_end_offset = end_offset; 23825 hck->hcksum_flags = flags; 23826 } 23827 return (pa != NULL); 23828 } 23829 23830 /* 23831 * Create zerocopy attribute and fill it with the specified flags 23832 */ 23833 boolean_t 23834 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 23835 { 23836 pattr_t *pa; 23837 pattrinfo_t pa_info; 23838 23839 ASSERT(mmd != NULL); 23840 pa_info.type = PATTR_ZCOPY; 23841 pa_info.len = sizeof (pattr_zcopy_t); 23842 23843 /* create and associate the attribute */ 23844 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23845 if (pa != NULL) { 23846 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 23847 23848 zcopy->zcopy_flags = flags; 23849 } 23850 return (pa != NULL); 23851 } 23852 23853 /* 23854 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 23855 * block chain. We could rewrite to handle arbitrary message block chains but 23856 * that would make the code complicated and slow. Right now there three 23857 * restrictions: 23858 * 23859 * 1. The first message block must contain the complete IP header and 23860 * at least 1 byte of payload data. 23861 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 23862 * so that we can use a single Multidata message. 23863 * 3. No frag must be distributed over two or more message blocks so 23864 * that we don't need more than two packet descriptors per frag. 23865 * 23866 * The above restrictions allow us to support userland applications (which 23867 * will send down a single message block) and NFS over UDP (which will 23868 * send down a chain of at most three message blocks). 23869 * 23870 * We also don't use MDT for payloads with less than or equal to 23871 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 23872 */ 23873 boolean_t 23874 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 23875 { 23876 int blocks; 23877 ssize_t total, missing, size; 23878 23879 ASSERT(mp != NULL); 23880 ASSERT(hdr_len > 0); 23881 23882 size = MBLKL(mp) - hdr_len; 23883 if (size <= 0) 23884 return (B_FALSE); 23885 23886 /* The first mblk contains the header and some payload. */ 23887 blocks = 1; 23888 total = size; 23889 size %= len; 23890 missing = (size == 0) ? 0 : (len - size); 23891 mp = mp->b_cont; 23892 23893 while (mp != NULL) { 23894 /* 23895 * Give up if we encounter a zero length message block. 23896 * In practice, this should rarely happen and therefore 23897 * not worth the trouble of freeing and re-linking the 23898 * mblk from the chain to handle such case. 23899 */ 23900 if ((size = MBLKL(mp)) == 0) 23901 return (B_FALSE); 23902 23903 /* Too many payload buffers for a single Multidata message? */ 23904 if (++blocks > MULTIDATA_MAX_PBUFS) 23905 return (B_FALSE); 23906 23907 total += size; 23908 /* Is a frag distributed over two or more message blocks? */ 23909 if (missing > size) 23910 return (B_FALSE); 23911 size -= missing; 23912 23913 size %= len; 23914 missing = (size == 0) ? 0 : (len - size); 23915 23916 mp = mp->b_cont; 23917 } 23918 23919 return (total > ip_wput_frag_mdt_min); 23920 } 23921 23922 /* 23923 * Outbound IPv4 fragmentation routine using MDT. 23924 */ 23925 static void 23926 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 23927 uint32_t frag_flag, int offset) 23928 { 23929 ipha_t *ipha_orig; 23930 int i1, ip_data_end; 23931 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 23932 mblk_t *hdr_mp, *md_mp = NULL; 23933 unsigned char *hdr_ptr, *pld_ptr; 23934 multidata_t *mmd; 23935 ip_pdescinfo_t pdi; 23936 ill_t *ill; 23937 ip_stack_t *ipst = ire->ire_ipst; 23938 23939 ASSERT(DB_TYPE(mp) == M_DATA); 23940 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 23941 23942 ill = ire_to_ill(ire); 23943 ASSERT(ill != NULL); 23944 23945 ipha_orig = (ipha_t *)mp->b_rptr; 23946 mp->b_rptr += sizeof (ipha_t); 23947 23948 /* Calculate how many packets we will send out */ 23949 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 23950 pkts = (i1 + len - 1) / len; 23951 ASSERT(pkts > 1); 23952 23953 /* Allocate a message block which will hold all the IP Headers. */ 23954 wroff = ipst->ips_ip_wroff_extra; 23955 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 23956 23957 i1 = pkts * hdr_chunk_len; 23958 /* 23959 * Create the header buffer, Multidata and destination address 23960 * and SAP attribute that should be associated with it. 23961 */ 23962 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 23963 ((hdr_mp->b_wptr += i1), 23964 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 23965 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 23966 freemsg(mp); 23967 if (md_mp == NULL) { 23968 freemsg(hdr_mp); 23969 } else { 23970 free_mmd: IP_STAT(ipst, ip_frag_mdt_discarded); 23971 freemsg(md_mp); 23972 } 23973 IP_STAT(ipst, ip_frag_mdt_allocfail); 23974 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 23975 return; 23976 } 23977 IP_STAT(ipst, ip_frag_mdt_allocd); 23978 23979 /* 23980 * Add a payload buffer to the Multidata; this operation must not 23981 * fail, or otherwise our logic in this routine is broken. There 23982 * is no memory allocation done by the routine, so any returned 23983 * failure simply tells us that we've done something wrong. 23984 * 23985 * A failure tells us that either we're adding the same payload 23986 * buffer more than once, or we're trying to add more buffers than 23987 * allowed. None of the above cases should happen, and we panic 23988 * because either there's horrible heap corruption, and/or 23989 * programming mistake. 23990 */ 23991 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23992 goto pbuf_panic; 23993 23994 hdr_ptr = hdr_mp->b_rptr; 23995 pld_ptr = mp->b_rptr; 23996 23997 /* Establish the ending byte offset, based on the starting offset. */ 23998 offset <<= 3; 23999 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 24000 IP_SIMPLE_HDR_LENGTH; 24001 24002 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 24003 24004 while (pld_ptr < mp->b_wptr) { 24005 ipha_t *ipha; 24006 uint16_t offset_and_flags; 24007 uint16_t ip_len; 24008 int error; 24009 24010 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 24011 ipha = (ipha_t *)(hdr_ptr + wroff); 24012 ASSERT(OK_32PTR(ipha)); 24013 *ipha = *ipha_orig; 24014 24015 if (ip_data_end - offset > len) { 24016 offset_and_flags = IPH_MF; 24017 } else { 24018 /* 24019 * Last frag. Set len to the length of this last piece. 24020 */ 24021 len = ip_data_end - offset; 24022 /* A frag of a frag might have IPH_MF non-zero */ 24023 offset_and_flags = 24024 ntohs(ipha->ipha_fragment_offset_and_flags) & 24025 IPH_MF; 24026 } 24027 offset_and_flags |= (uint16_t)(offset >> 3); 24028 offset_and_flags |= (uint16_t)frag_flag; 24029 /* Store the offset and flags in the IP header. */ 24030 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24031 24032 /* Store the length in the IP header. */ 24033 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 24034 ipha->ipha_length = htons(ip_len); 24035 24036 /* 24037 * Set the IP header checksum. Note that mp is just 24038 * the header, so this is easy to pass to ip_csum. 24039 */ 24040 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24041 24042 DTRACE_IP7(send, mblk_t *, md_mp, conn_t *, NULL, void_ip_t *, 24043 ipha, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, ip6_t *, 24044 NULL, int, 0); 24045 24046 /* 24047 * Record offset and size of header and data of the next packet 24048 * in the multidata message. 24049 */ 24050 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 24051 PDESC_PLD_INIT(&pdi); 24052 i1 = MIN(mp->b_wptr - pld_ptr, len); 24053 ASSERT(i1 > 0); 24054 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 24055 if (i1 == len) { 24056 pld_ptr += len; 24057 } else { 24058 i1 = len - i1; 24059 mp = mp->b_cont; 24060 ASSERT(mp != NULL); 24061 ASSERT(MBLKL(mp) >= i1); 24062 /* 24063 * Attach the next payload message block to the 24064 * multidata message. 24065 */ 24066 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24067 goto pbuf_panic; 24068 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 24069 pld_ptr = mp->b_rptr + i1; 24070 } 24071 24072 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 24073 KM_NOSLEEP)) == NULL) { 24074 /* 24075 * Any failure other than ENOMEM indicates that we 24076 * have passed in invalid pdesc info or parameters 24077 * to mmd_addpdesc, which must not happen. 24078 * 24079 * EINVAL is a result of failure on boundary checks 24080 * against the pdesc info contents. It should not 24081 * happen, and we panic because either there's 24082 * horrible heap corruption, and/or programming 24083 * mistake. 24084 */ 24085 if (error != ENOMEM) { 24086 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 24087 "pdesc logic error detected for " 24088 "mmd %p pinfo %p (%d)\n", 24089 (void *)mmd, (void *)&pdi, error); 24090 /* NOTREACHED */ 24091 } 24092 IP_STAT(ipst, ip_frag_mdt_addpdescfail); 24093 /* Free unattached payload message blocks as well */ 24094 md_mp->b_cont = mp->b_cont; 24095 goto free_mmd; 24096 } 24097 24098 /* Advance fragment offset. */ 24099 offset += len; 24100 24101 /* Advance to location for next header in the buffer. */ 24102 hdr_ptr += hdr_chunk_len; 24103 24104 /* Did we reach the next payload message block? */ 24105 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 24106 mp = mp->b_cont; 24107 /* 24108 * Attach the next message block with payload 24109 * data to the multidata message. 24110 */ 24111 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 24112 goto pbuf_panic; 24113 pld_ptr = mp->b_rptr; 24114 } 24115 } 24116 24117 ASSERT(hdr_mp->b_wptr == hdr_ptr); 24118 ASSERT(mp->b_wptr == pld_ptr); 24119 24120 /* Update IP statistics */ 24121 IP_STAT_UPDATE(ipst, ip_frag_mdt_pkt_out, pkts); 24122 24123 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates, pkts); 24124 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs); 24125 24126 len = ntohs(ipha_orig->ipha_length) + (pkts - 1) * IP_SIMPLE_HDR_LENGTH; 24127 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, pkts); 24128 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, len); 24129 24130 if (pkt_type == OB_PKT) { 24131 ire->ire_ob_pkt_count += pkts; 24132 if (ire->ire_ipif != NULL) 24133 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 24134 } else { 24135 /* The type is IB_PKT in the forwarding path. */ 24136 ire->ire_ib_pkt_count += pkts; 24137 ASSERT(!IRE_IS_LOCAL(ire)); 24138 if (ire->ire_type & IRE_BROADCAST) { 24139 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 24140 } else { 24141 UPDATE_MIB(ill->ill_ip_mib, 24142 ipIfStatsHCOutForwDatagrams, pkts); 24143 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 24144 } 24145 } 24146 ire->ire_last_used_time = lbolt; 24147 /* Send it down */ 24148 putnext(ire->ire_stq, md_mp); 24149 return; 24150 24151 pbuf_panic: 24152 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 24153 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 24154 pbuf_idx); 24155 /* NOTREACHED */ 24156 } 24157 24158 /* 24159 * Outbound IP fragmentation routine. 24160 * 24161 * NOTE : This routine does not ire_refrele the ire that is passed in 24162 * as the argument. 24163 */ 24164 static void 24165 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 24166 uint32_t frag_flag, zoneid_t zoneid, ip_stack_t *ipst, conn_t *connp) 24167 { 24168 int i1; 24169 mblk_t *ll_hdr_mp; 24170 int ll_hdr_len; 24171 int hdr_len; 24172 mblk_t *hdr_mp; 24173 ipha_t *ipha; 24174 int ip_data_end; 24175 int len; 24176 mblk_t *mp = mp_orig, *mp1; 24177 int offset; 24178 queue_t *q; 24179 uint32_t v_hlen_tos_len; 24180 mblk_t *first_mp; 24181 boolean_t mctl_present; 24182 ill_t *ill; 24183 ill_t *out_ill; 24184 mblk_t *xmit_mp; 24185 mblk_t *carve_mp; 24186 ire_t *ire1 = NULL; 24187 ire_t *save_ire = NULL; 24188 mblk_t *next_mp = NULL; 24189 boolean_t last_frag = B_FALSE; 24190 boolean_t multirt_send = B_FALSE; 24191 ire_t *first_ire = NULL; 24192 irb_t *irb = NULL; 24193 mib2_ipIfStatsEntry_t *mibptr = NULL; 24194 24195 ill = ire_to_ill(ire); 24196 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; 24197 24198 BUMP_MIB(mibptr, ipIfStatsOutFragReqds); 24199 24200 if (max_frag == 0) { 24201 ip1dbg(("ip_wput_frag: ire frag size is 0" 24202 " - dropping packet\n")); 24203 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24204 freemsg(mp); 24205 return; 24206 } 24207 24208 /* 24209 * IPsec does not allow hw accelerated packets to be fragmented 24210 * This check is made in ip_wput_ipsec_out prior to coming here 24211 * via ip_wput_ire_fragmentit. 24212 * 24213 * If at this point we have an ire whose ARP request has not 24214 * been sent out, we call ip_xmit_v4->ire_arpresolve to trigger 24215 * sending of ARP query and change ire's state to ND_INCOMPLETE. 24216 * This packet and all fragmentable packets for this ire will 24217 * continue to get dropped while ire_nce->nce_state remains in 24218 * ND_INCOMPLETE. Post-ARP resolution, after ire's nce_state changes to 24219 * ND_REACHABLE, all subsquent large packets for this ire will 24220 * get fragemented and sent out by this function. 24221 */ 24222 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 24223 /* If nce_state is ND_INITIAL, trigger ARP query */ 24224 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE, NULL); 24225 ip1dbg(("ip_wput_frag: mac address for ire is unresolved" 24226 " - dropping packet\n")); 24227 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24228 freemsg(mp); 24229 return; 24230 } 24231 24232 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 24233 "ip_wput_frag_start:"); 24234 24235 if (mp->b_datap->db_type == M_CTL) { 24236 first_mp = mp; 24237 mp_orig = mp = mp->b_cont; 24238 mctl_present = B_TRUE; 24239 } else { 24240 first_mp = mp; 24241 mctl_present = B_FALSE; 24242 } 24243 24244 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 24245 ipha = (ipha_t *)mp->b_rptr; 24246 24247 /* 24248 * If the Don't Fragment flag is on, generate an ICMP destination 24249 * unreachable, fragmentation needed. 24250 */ 24251 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 24252 if (offset & IPH_DF) { 24253 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24254 if (is_system_labeled()) { 24255 max_frag = tsol_pmtu_adjust(mp, ire->ire_max_frag, 24256 ire->ire_max_frag - max_frag, AF_INET); 24257 } 24258 /* 24259 * Need to compute hdr checksum if called from ip_wput_ire. 24260 * Note that ip_rput_forward verifies the checksum before 24261 * calling this routine so in that case this is a noop. 24262 */ 24263 ipha->ipha_hdr_checksum = 0; 24264 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24265 icmp_frag_needed(ire->ire_stq, first_mp, max_frag, zoneid, 24266 ipst); 24267 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24268 "ip_wput_frag_end:(%S)", 24269 "don't fragment"); 24270 return; 24271 } 24272 /* 24273 * Labeled systems adjust max_frag if they add a label 24274 * to send the correct path mtu. We need the real mtu since we 24275 * are fragmenting the packet after label adjustment. 24276 */ 24277 if (is_system_labeled()) 24278 max_frag = ire->ire_max_frag; 24279 if (mctl_present) 24280 freeb(first_mp); 24281 /* 24282 * Establish the starting offset. May not be zero if we are fragging 24283 * a fragment that is being forwarded. 24284 */ 24285 offset = offset & IPH_OFFSET; 24286 24287 /* TODO why is this test needed? */ 24288 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 24289 if (((max_frag - LENGTH) & ~7) < 8) { 24290 /* TODO: notify ulp somehow */ 24291 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24292 freemsg(mp); 24293 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24294 "ip_wput_frag_end:(%S)", 24295 "len < 8"); 24296 return; 24297 } 24298 24299 hdr_len = (V_HLEN & 0xF) << 2; 24300 24301 ipha->ipha_hdr_checksum = 0; 24302 24303 /* 24304 * Establish the number of bytes maximum per frag, after putting 24305 * in the header. 24306 */ 24307 len = (max_frag - hdr_len) & ~7; 24308 24309 /* Check if we can use MDT to send out the frags. */ 24310 ASSERT(!IRE_IS_LOCAL(ire)); 24311 if (hdr_len == IP_SIMPLE_HDR_LENGTH && 24312 ipst->ips_ip_multidata_outbound && 24313 !(ire->ire_flags & RTF_MULTIRT) && 24314 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 24315 ill != NULL && ILL_MDT_CAPABLE(ill) && 24316 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 24317 ASSERT(ill->ill_mdt_capab != NULL); 24318 if (!ill->ill_mdt_capab->ill_mdt_on) { 24319 /* 24320 * If MDT has been previously turned off in the past, 24321 * and we currently can do MDT (due to IPQoS policy 24322 * removal, etc.) then enable it for this interface. 24323 */ 24324 ill->ill_mdt_capab->ill_mdt_on = 1; 24325 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 24326 ill->ill_name)); 24327 } 24328 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 24329 offset); 24330 return; 24331 } 24332 24333 /* Get a copy of the header for the trailing frags */ 24334 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst, 24335 mp); 24336 if (!hdr_mp) { 24337 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24338 freemsg(mp); 24339 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24340 "ip_wput_frag_end:(%S)", 24341 "couldn't copy hdr"); 24342 return; 24343 } 24344 24345 /* Store the starting offset, with the MoreFrags flag. */ 24346 i1 = offset | IPH_MF | frag_flag; 24347 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 24348 24349 /* Establish the ending byte offset, based on the starting offset. */ 24350 offset <<= 3; 24351 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 24352 24353 /* Store the length of the first fragment in the IP header. */ 24354 i1 = len + hdr_len; 24355 ASSERT(i1 <= IP_MAXPACKET); 24356 ipha->ipha_length = htons((uint16_t)i1); 24357 24358 /* 24359 * Compute the IP header checksum for the first frag. We have to 24360 * watch out that we stop at the end of the header. 24361 */ 24362 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24363 24364 /* 24365 * Now carve off the first frag. Note that this will include the 24366 * original IP header. 24367 */ 24368 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 24369 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24370 freeb(hdr_mp); 24371 freemsg(mp_orig); 24372 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24373 "ip_wput_frag_end:(%S)", 24374 "couldn't carve first"); 24375 return; 24376 } 24377 24378 /* 24379 * Multirouting case. Each fragment is replicated 24380 * via all non-condemned RTF_MULTIRT routes 24381 * currently resolved. 24382 * We ensure that first_ire is the first RTF_MULTIRT 24383 * ire in the bucket. 24384 */ 24385 if (ire->ire_flags & RTF_MULTIRT) { 24386 irb = ire->ire_bucket; 24387 ASSERT(irb != NULL); 24388 24389 multirt_send = B_TRUE; 24390 24391 /* Make sure we do not omit any multiroute ire. */ 24392 IRB_REFHOLD(irb); 24393 for (first_ire = irb->irb_ire; 24394 first_ire != NULL; 24395 first_ire = first_ire->ire_next) { 24396 if ((first_ire->ire_flags & RTF_MULTIRT) && 24397 (first_ire->ire_addr == ire->ire_addr) && 24398 !(first_ire->ire_marks & 24399 (IRE_MARK_CONDEMNED | IRE_MARK_TESTHIDDEN))) 24400 break; 24401 } 24402 24403 if (first_ire != NULL) { 24404 if (first_ire != ire) { 24405 IRE_REFHOLD(first_ire); 24406 /* 24407 * Do not release the ire passed in 24408 * as the argument. 24409 */ 24410 ire = first_ire; 24411 } else { 24412 first_ire = NULL; 24413 } 24414 } 24415 IRB_REFRELE(irb); 24416 24417 /* 24418 * Save the first ire; we will need to restore it 24419 * for the trailing frags. 24420 * We REFHOLD save_ire, as each iterated ire will be 24421 * REFRELEd. 24422 */ 24423 save_ire = ire; 24424 IRE_REFHOLD(save_ire); 24425 } 24426 24427 /* 24428 * First fragment emission loop. 24429 * In most cases, the emission loop below is entered only 24430 * once. Only in the case where the ire holds the RTF_MULTIRT 24431 * flag, do we loop to process all RTF_MULTIRT ires in the 24432 * bucket, and send the fragment through all crossed 24433 * RTF_MULTIRT routes. 24434 */ 24435 do { 24436 if (ire->ire_flags & RTF_MULTIRT) { 24437 /* 24438 * We are in a multiple send case, need to get 24439 * the next ire and make a copy of the packet. 24440 * ire1 holds here the next ire to process in the 24441 * bucket. If multirouting is expected, 24442 * any non-RTF_MULTIRT ire that has the 24443 * right destination address is ignored. 24444 * 24445 * We have to take into account the MTU of 24446 * each walked ire. max_frag is set by the 24447 * the caller and generally refers to 24448 * the primary ire entry. Here we ensure that 24449 * no route with a lower MTU will be used, as 24450 * fragments are carved once for all ires, 24451 * then replicated. 24452 */ 24453 ASSERT(irb != NULL); 24454 IRB_REFHOLD(irb); 24455 for (ire1 = ire->ire_next; 24456 ire1 != NULL; 24457 ire1 = ire1->ire_next) { 24458 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 24459 continue; 24460 if (ire1->ire_addr != ire->ire_addr) 24461 continue; 24462 if (ire1->ire_marks & 24463 (IRE_MARK_CONDEMNED | IRE_MARK_TESTHIDDEN)) 24464 continue; 24465 /* 24466 * Ensure we do not exceed the MTU 24467 * of the next route. 24468 */ 24469 if (ire1->ire_max_frag < max_frag) { 24470 ip_multirt_bad_mtu(ire1, max_frag); 24471 continue; 24472 } 24473 24474 /* Got one. */ 24475 IRE_REFHOLD(ire1); 24476 break; 24477 } 24478 IRB_REFRELE(irb); 24479 24480 if (ire1 != NULL) { 24481 next_mp = copyb(mp); 24482 if ((next_mp == NULL) || 24483 ((mp->b_cont != NULL) && 24484 ((next_mp->b_cont = 24485 dupmsg(mp->b_cont)) == NULL))) { 24486 freemsg(next_mp); 24487 next_mp = NULL; 24488 ire_refrele(ire1); 24489 ire1 = NULL; 24490 } 24491 } 24492 24493 /* Last multiroute ire; don't loop anymore. */ 24494 if (ire1 == NULL) { 24495 multirt_send = B_FALSE; 24496 } 24497 } 24498 24499 ll_hdr_len = 0; 24500 LOCK_IRE_FP_MP(ire); 24501 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24502 if (ll_hdr_mp != NULL) { 24503 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24504 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 24505 } else { 24506 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24507 } 24508 24509 /* If there is a transmit header, get a copy for this frag. */ 24510 /* 24511 * TODO: should check db_ref before calling ip_carve_mp since 24512 * it might give us a dup. 24513 */ 24514 if (!ll_hdr_mp) { 24515 /* No xmit header. */ 24516 xmit_mp = mp; 24517 24518 /* We have a link-layer header that can fit in our mblk. */ 24519 } else if (mp->b_datap->db_ref == 1 && 24520 ll_hdr_len != 0 && 24521 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24522 /* M_DATA fastpath */ 24523 mp->b_rptr -= ll_hdr_len; 24524 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 24525 xmit_mp = mp; 24526 24527 /* Corner case if copyb has failed */ 24528 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 24529 UNLOCK_IRE_FP_MP(ire); 24530 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24531 freeb(hdr_mp); 24532 freemsg(mp); 24533 freemsg(mp_orig); 24534 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24535 "ip_wput_frag_end:(%S)", 24536 "discard"); 24537 24538 if (multirt_send) { 24539 ASSERT(ire1); 24540 ASSERT(next_mp); 24541 24542 freemsg(next_mp); 24543 ire_refrele(ire1); 24544 } 24545 if (save_ire != NULL) 24546 IRE_REFRELE(save_ire); 24547 24548 if (first_ire != NULL) 24549 ire_refrele(first_ire); 24550 return; 24551 24552 /* 24553 * Case of res_mp OR the fastpath mp can't fit 24554 * in the mblk 24555 */ 24556 } else { 24557 xmit_mp->b_cont = mp; 24558 24559 /* 24560 * Get priority marking, if any. 24561 * We propagate the CoS marking from the 24562 * original packet that went to QoS processing 24563 * in ip_wput_ire to the newly carved mp. 24564 */ 24565 if (DB_TYPE(xmit_mp) == M_DATA) 24566 xmit_mp->b_band = mp->b_band; 24567 } 24568 UNLOCK_IRE_FP_MP(ire); 24569 24570 q = ire->ire_stq; 24571 out_ill = (ill_t *)q->q_ptr; 24572 24573 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24574 24575 DTRACE_PROBE4(ip4__physical__out__start, 24576 ill_t *, NULL, ill_t *, out_ill, 24577 ipha_t *, ipha, mblk_t *, xmit_mp); 24578 24579 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24580 ipst->ips_ipv4firewall_physical_out, 24581 NULL, out_ill, ipha, xmit_mp, mp, 0, ipst); 24582 24583 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, xmit_mp); 24584 24585 if (xmit_mp != NULL) { 24586 DTRACE_IP7(send, mblk_t *, xmit_mp, conn_t *, NULL, 24587 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, out_ill, 24588 ipha_t *, ipha, ip6_t *, NULL, int, 0); 24589 24590 ILL_SEND_TX(out_ill, ire, connp, xmit_mp, 0, connp); 24591 24592 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 24593 UPDATE_MIB(out_ill->ill_ip_mib, 24594 ipIfStatsHCOutOctets, i1); 24595 24596 if (pkt_type != OB_PKT) { 24597 /* 24598 * Update the packet count and MIB stats 24599 * of trailing RTF_MULTIRT ires. 24600 */ 24601 UPDATE_OB_PKT_COUNT(ire); 24602 BUMP_MIB(out_ill->ill_ip_mib, 24603 ipIfStatsOutFragReqds); 24604 } 24605 } 24606 24607 if (multirt_send) { 24608 /* 24609 * We are in a multiple send case; look for 24610 * the next ire and re-enter the loop. 24611 */ 24612 ASSERT(ire1); 24613 ASSERT(next_mp); 24614 /* REFRELE the current ire before looping */ 24615 ire_refrele(ire); 24616 ire = ire1; 24617 ire1 = NULL; 24618 mp = next_mp; 24619 next_mp = NULL; 24620 } 24621 } while (multirt_send); 24622 24623 ASSERT(ire1 == NULL); 24624 24625 /* Restore the original ire; we need it for the trailing frags */ 24626 if (save_ire != NULL) { 24627 /* REFRELE the last iterated ire */ 24628 ire_refrele(ire); 24629 /* save_ire has been REFHOLDed */ 24630 ire = save_ire; 24631 save_ire = NULL; 24632 q = ire->ire_stq; 24633 } 24634 24635 if (pkt_type == OB_PKT) { 24636 UPDATE_OB_PKT_COUNT(ire); 24637 } else { 24638 out_ill = (ill_t *)q->q_ptr; 24639 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 24640 UPDATE_IB_PKT_COUNT(ire); 24641 } 24642 24643 /* Advance the offset to the second frag starting point. */ 24644 offset += len; 24645 /* 24646 * Update hdr_len from the copied header - there might be less options 24647 * in the later fragments. 24648 */ 24649 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 24650 /* Loop until done. */ 24651 for (;;) { 24652 uint16_t offset_and_flags; 24653 uint16_t ip_len; 24654 24655 if (ip_data_end - offset > len) { 24656 /* 24657 * Carve off the appropriate amount from the original 24658 * datagram. 24659 */ 24660 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24661 mp = NULL; 24662 break; 24663 } 24664 /* 24665 * More frags after this one. Get another copy 24666 * of the header. 24667 */ 24668 if (carve_mp->b_datap->db_ref == 1 && 24669 hdr_mp->b_wptr - hdr_mp->b_rptr < 24670 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24671 /* Inline IP header */ 24672 carve_mp->b_rptr -= hdr_mp->b_wptr - 24673 hdr_mp->b_rptr; 24674 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24675 hdr_mp->b_wptr - hdr_mp->b_rptr); 24676 mp = carve_mp; 24677 } else { 24678 if (!(mp = copyb(hdr_mp))) { 24679 freemsg(carve_mp); 24680 break; 24681 } 24682 /* Get priority marking, if any. */ 24683 mp->b_band = carve_mp->b_band; 24684 mp->b_cont = carve_mp; 24685 } 24686 ipha = (ipha_t *)mp->b_rptr; 24687 offset_and_flags = IPH_MF; 24688 } else { 24689 /* 24690 * Last frag. Consume the header. Set len to 24691 * the length of this last piece. 24692 */ 24693 len = ip_data_end - offset; 24694 24695 /* 24696 * Carve off the appropriate amount from the original 24697 * datagram. 24698 */ 24699 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24700 mp = NULL; 24701 break; 24702 } 24703 if (carve_mp->b_datap->db_ref == 1 && 24704 hdr_mp->b_wptr - hdr_mp->b_rptr < 24705 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24706 /* Inline IP header */ 24707 carve_mp->b_rptr -= hdr_mp->b_wptr - 24708 hdr_mp->b_rptr; 24709 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24710 hdr_mp->b_wptr - hdr_mp->b_rptr); 24711 mp = carve_mp; 24712 freeb(hdr_mp); 24713 hdr_mp = mp; 24714 } else { 24715 mp = hdr_mp; 24716 /* Get priority marking, if any. */ 24717 mp->b_band = carve_mp->b_band; 24718 mp->b_cont = carve_mp; 24719 } 24720 ipha = (ipha_t *)mp->b_rptr; 24721 /* A frag of a frag might have IPH_MF non-zero */ 24722 offset_and_flags = 24723 ntohs(ipha->ipha_fragment_offset_and_flags) & 24724 IPH_MF; 24725 } 24726 offset_and_flags |= (uint16_t)(offset >> 3); 24727 offset_and_flags |= (uint16_t)frag_flag; 24728 /* Store the offset and flags in the IP header. */ 24729 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24730 24731 /* Store the length in the IP header. */ 24732 ip_len = (uint16_t)(len + hdr_len); 24733 ipha->ipha_length = htons(ip_len); 24734 24735 /* 24736 * Set the IP header checksum. Note that mp is just 24737 * the header, so this is easy to pass to ip_csum. 24738 */ 24739 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24740 24741 /* Attach a transmit header, if any, and ship it. */ 24742 if (pkt_type == OB_PKT) { 24743 UPDATE_OB_PKT_COUNT(ire); 24744 } else { 24745 out_ill = (ill_t *)q->q_ptr; 24746 BUMP_MIB(out_ill->ill_ip_mib, 24747 ipIfStatsHCOutForwDatagrams); 24748 UPDATE_IB_PKT_COUNT(ire); 24749 } 24750 24751 if (ire->ire_flags & RTF_MULTIRT) { 24752 irb = ire->ire_bucket; 24753 ASSERT(irb != NULL); 24754 24755 multirt_send = B_TRUE; 24756 24757 /* 24758 * Save the original ire; we will need to restore it 24759 * for the tailing frags. 24760 */ 24761 save_ire = ire; 24762 IRE_REFHOLD(save_ire); 24763 } 24764 /* 24765 * Emission loop for this fragment, similar 24766 * to what is done for the first fragment. 24767 */ 24768 do { 24769 if (multirt_send) { 24770 /* 24771 * We are in a multiple send case, need to get 24772 * the next ire and make a copy of the packet. 24773 */ 24774 ASSERT(irb != NULL); 24775 IRB_REFHOLD(irb); 24776 for (ire1 = ire->ire_next; 24777 ire1 != NULL; 24778 ire1 = ire1->ire_next) { 24779 if (!(ire1->ire_flags & RTF_MULTIRT)) 24780 continue; 24781 if (ire1->ire_addr != ire->ire_addr) 24782 continue; 24783 if (ire1->ire_marks & 24784 (IRE_MARK_CONDEMNED | 24785 IRE_MARK_TESTHIDDEN)) 24786 continue; 24787 /* 24788 * Ensure we do not exceed the MTU 24789 * of the next route. 24790 */ 24791 if (ire1->ire_max_frag < max_frag) { 24792 ip_multirt_bad_mtu(ire1, 24793 max_frag); 24794 continue; 24795 } 24796 24797 /* Got one. */ 24798 IRE_REFHOLD(ire1); 24799 break; 24800 } 24801 IRB_REFRELE(irb); 24802 24803 if (ire1 != NULL) { 24804 next_mp = copyb(mp); 24805 if ((next_mp == NULL) || 24806 ((mp->b_cont != NULL) && 24807 ((next_mp->b_cont = 24808 dupmsg(mp->b_cont)) == NULL))) { 24809 freemsg(next_mp); 24810 next_mp = NULL; 24811 ire_refrele(ire1); 24812 ire1 = NULL; 24813 } 24814 } 24815 24816 /* Last multiroute ire; don't loop anymore. */ 24817 if (ire1 == NULL) { 24818 multirt_send = B_FALSE; 24819 } 24820 } 24821 24822 /* Update transmit header */ 24823 ll_hdr_len = 0; 24824 LOCK_IRE_FP_MP(ire); 24825 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24826 if (ll_hdr_mp != NULL) { 24827 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24828 ll_hdr_len = MBLKL(ll_hdr_mp); 24829 } else { 24830 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24831 } 24832 24833 if (!ll_hdr_mp) { 24834 xmit_mp = mp; 24835 24836 /* 24837 * We have link-layer header that can fit in 24838 * our mblk. 24839 */ 24840 } else if (mp->b_datap->db_ref == 1 && 24841 ll_hdr_len != 0 && 24842 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24843 /* M_DATA fastpath */ 24844 mp->b_rptr -= ll_hdr_len; 24845 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 24846 ll_hdr_len); 24847 xmit_mp = mp; 24848 24849 /* 24850 * Case of res_mp OR the fastpath mp can't fit 24851 * in the mblk 24852 */ 24853 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 24854 xmit_mp->b_cont = mp; 24855 /* Get priority marking, if any. */ 24856 if (DB_TYPE(xmit_mp) == M_DATA) 24857 xmit_mp->b_band = mp->b_band; 24858 24859 /* Corner case if copyb failed */ 24860 } else { 24861 /* 24862 * Exit both the replication and 24863 * fragmentation loops. 24864 */ 24865 UNLOCK_IRE_FP_MP(ire); 24866 goto drop_pkt; 24867 } 24868 UNLOCK_IRE_FP_MP(ire); 24869 24870 mp1 = mp; 24871 out_ill = (ill_t *)q->q_ptr; 24872 24873 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24874 24875 DTRACE_PROBE4(ip4__physical__out__start, 24876 ill_t *, NULL, ill_t *, out_ill, 24877 ipha_t *, ipha, mblk_t *, xmit_mp); 24878 24879 FW_HOOKS(ipst->ips_ip4_physical_out_event, 24880 ipst->ips_ipv4firewall_physical_out, 24881 NULL, out_ill, ipha, xmit_mp, mp, 0, ipst); 24882 24883 DTRACE_PROBE1(ip4__physical__out__end, 24884 mblk_t *, xmit_mp); 24885 24886 if (mp != mp1 && hdr_mp == mp1) 24887 hdr_mp = mp; 24888 if (mp != mp1 && mp_orig == mp1) 24889 mp_orig = mp; 24890 24891 if (xmit_mp != NULL) { 24892 DTRACE_IP7(send, mblk_t *, xmit_mp, conn_t *, 24893 NULL, void_ip_t *, ipha, 24894 __dtrace_ipsr_ill_t *, out_ill, ipha_t *, 24895 ipha, ip6_t *, NULL, int, 0); 24896 24897 ILL_SEND_TX(out_ill, ire, connp, 24898 xmit_mp, 0, connp); 24899 24900 BUMP_MIB(out_ill->ill_ip_mib, 24901 ipIfStatsHCOutTransmits); 24902 UPDATE_MIB(out_ill->ill_ip_mib, 24903 ipIfStatsHCOutOctets, ip_len); 24904 24905 if (pkt_type != OB_PKT) { 24906 /* 24907 * Update the packet count of trailing 24908 * RTF_MULTIRT ires. 24909 */ 24910 UPDATE_OB_PKT_COUNT(ire); 24911 } 24912 } 24913 24914 /* All done if we just consumed the hdr_mp. */ 24915 if (mp == hdr_mp) { 24916 last_frag = B_TRUE; 24917 BUMP_MIB(out_ill->ill_ip_mib, 24918 ipIfStatsOutFragOKs); 24919 } 24920 24921 if (multirt_send) { 24922 /* 24923 * We are in a multiple send case; look for 24924 * the next ire and re-enter the loop. 24925 */ 24926 ASSERT(ire1); 24927 ASSERT(next_mp); 24928 /* REFRELE the current ire before looping */ 24929 ire_refrele(ire); 24930 ire = ire1; 24931 ire1 = NULL; 24932 q = ire->ire_stq; 24933 mp = next_mp; 24934 next_mp = NULL; 24935 } 24936 } while (multirt_send); 24937 /* 24938 * Restore the original ire; we need it for the 24939 * trailing frags 24940 */ 24941 if (save_ire != NULL) { 24942 ASSERT(ire1 == NULL); 24943 /* REFRELE the last iterated ire */ 24944 ire_refrele(ire); 24945 /* save_ire has been REFHOLDed */ 24946 ire = save_ire; 24947 q = ire->ire_stq; 24948 save_ire = NULL; 24949 } 24950 24951 if (last_frag) { 24952 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24953 "ip_wput_frag_end:(%S)", 24954 "consumed hdr_mp"); 24955 24956 if (first_ire != NULL) 24957 ire_refrele(first_ire); 24958 return; 24959 } 24960 /* Otherwise, advance and loop. */ 24961 offset += len; 24962 } 24963 24964 drop_pkt: 24965 /* Clean up following allocation failure. */ 24966 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24967 freemsg(mp); 24968 if (mp != hdr_mp) 24969 freeb(hdr_mp); 24970 if (mp != mp_orig) 24971 freemsg(mp_orig); 24972 24973 if (save_ire != NULL) 24974 IRE_REFRELE(save_ire); 24975 if (first_ire != NULL) 24976 ire_refrele(first_ire); 24977 24978 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24979 "ip_wput_frag_end:(%S)", 24980 "end--alloc failure"); 24981 } 24982 24983 /* 24984 * Copy the header plus those options which have the copy bit set 24985 * src is the template to make sure we preserve the cred for TX purposes. 24986 */ 24987 static mblk_t * 24988 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst, 24989 mblk_t *src) 24990 { 24991 mblk_t *mp; 24992 uchar_t *up; 24993 24994 /* 24995 * Quick check if we need to look for options without the copy bit 24996 * set 24997 */ 24998 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src); 24999 if (!mp) 25000 return (mp); 25001 mp->b_rptr += ipst->ips_ip_wroff_extra; 25002 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 25003 bcopy(rptr, mp->b_rptr, hdr_len); 25004 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra; 25005 return (mp); 25006 } 25007 up = mp->b_rptr; 25008 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 25009 up += IP_SIMPLE_HDR_LENGTH; 25010 rptr += IP_SIMPLE_HDR_LENGTH; 25011 hdr_len -= IP_SIMPLE_HDR_LENGTH; 25012 while (hdr_len > 0) { 25013 uint32_t optval; 25014 uint32_t optlen; 25015 25016 optval = *rptr; 25017 if (optval == IPOPT_EOL) 25018 break; 25019 if (optval == IPOPT_NOP) 25020 optlen = 1; 25021 else 25022 optlen = rptr[1]; 25023 if (optval & IPOPT_COPY) { 25024 bcopy(rptr, up, optlen); 25025 up += optlen; 25026 } 25027 rptr += optlen; 25028 hdr_len -= optlen; 25029 } 25030 /* 25031 * Make sure that we drop an even number of words by filling 25032 * with EOL to the next word boundary. 25033 */ 25034 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 25035 hdr_len & 0x3; hdr_len++) 25036 *up++ = IPOPT_EOL; 25037 mp->b_wptr = up; 25038 /* Update header length */ 25039 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 25040 return (mp); 25041 } 25042 25043 /* 25044 * Delivery to local recipients including fanout to multiple recipients. 25045 * Does not do checksumming of UDP/TCP. 25046 * Note: q should be the read side queue for either the ill or conn. 25047 * Note: rq should be the read side q for the lower (ill) stream. 25048 * We don't send packets to IPPF processing, thus the last argument 25049 * to all the fanout calls are B_FALSE. 25050 */ 25051 void 25052 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 25053 int fanout_flags, zoneid_t zoneid) 25054 { 25055 uint32_t protocol; 25056 mblk_t *first_mp; 25057 boolean_t mctl_present; 25058 int ire_type; 25059 #define rptr ((uchar_t *)ipha) 25060 ip_stack_t *ipst = ill->ill_ipst; 25061 25062 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 25063 "ip_wput_local_start: q %p", q); 25064 25065 if (ire != NULL) { 25066 ire_type = ire->ire_type; 25067 } else { 25068 /* 25069 * Only ip_multicast_loopback() calls us with a NULL ire. If the 25070 * packet is not multicast, we can't tell the ire type. 25071 */ 25072 ASSERT(CLASSD(ipha->ipha_dst)); 25073 ire_type = IRE_BROADCAST; 25074 } 25075 25076 first_mp = mp; 25077 if (first_mp->b_datap->db_type == M_CTL) { 25078 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 25079 if (!io->ipsec_out_secure) { 25080 /* 25081 * This ipsec_out_t was allocated in ip_wput 25082 * for multicast packets to store the ill_index. 25083 * As this is being delivered locally, we don't 25084 * need this anymore. 25085 */ 25086 mp = first_mp->b_cont; 25087 freeb(first_mp); 25088 first_mp = mp; 25089 mctl_present = B_FALSE; 25090 } else { 25091 /* 25092 * Convert IPSEC_OUT to IPSEC_IN, preserving all 25093 * security properties for the looped-back packet. 25094 */ 25095 mctl_present = B_TRUE; 25096 mp = first_mp->b_cont; 25097 ASSERT(mp != NULL); 25098 ipsec_out_to_in(first_mp); 25099 } 25100 } else { 25101 mctl_present = B_FALSE; 25102 } 25103 25104 DTRACE_PROBE4(ip4__loopback__in__start, 25105 ill_t *, ill, ill_t *, NULL, 25106 ipha_t *, ipha, mblk_t *, first_mp); 25107 25108 FW_HOOKS(ipst->ips_ip4_loopback_in_event, 25109 ipst->ips_ipv4firewall_loopback_in, 25110 ill, NULL, ipha, first_mp, mp, 0, ipst); 25111 25112 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, first_mp); 25113 25114 if (first_mp == NULL) 25115 return; 25116 25117 if (ipst->ips_ipobs_enabled) { 25118 zoneid_t szone, dzone, lookup_zoneid = ALL_ZONES; 25119 zoneid_t stackzoneid = netstackid_to_zoneid( 25120 ipst->ips_netstack->netstack_stackid); 25121 25122 dzone = (stackzoneid == GLOBAL_ZONEID) ? zoneid : stackzoneid; 25123 /* 25124 * 127.0.0.1 is special, as we cannot lookup its zoneid by 25125 * address. Restrict the lookup below to the destination zone. 25126 */ 25127 if (ipha->ipha_src == ntohl(INADDR_LOOPBACK)) 25128 lookup_zoneid = zoneid; 25129 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, ipst, 25130 lookup_zoneid); 25131 ipobs_hook(mp, IPOBS_HOOK_LOCAL, szone, dzone, ill, 25132 IPV4_VERSION, 0, ipst); 25133 } 25134 25135 DTRACE_IP7(receive, mblk_t *, first_mp, conn_t *, NULL, void_ip_t *, 25136 ipha, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, ip6_t *, NULL, 25137 int, 1); 25138 25139 ipst->ips_loopback_packets++; 25140 25141 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 25142 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 25143 if (!IS_SIMPLE_IPH(ipha)) { 25144 ip_wput_local_options(ipha, ipst); 25145 } 25146 25147 protocol = ipha->ipha_protocol; 25148 switch (protocol) { 25149 case IPPROTO_ICMP: { 25150 ire_t *ire_zone; 25151 ilm_t *ilm; 25152 mblk_t *mp1; 25153 zoneid_t last_zoneid; 25154 ilm_walker_t ilw; 25155 25156 if (CLASSD(ipha->ipha_dst) && !IS_LOOPBACK(ill)) { 25157 ASSERT(ire_type == IRE_BROADCAST); 25158 /* 25159 * In the multicast case, applications may have joined 25160 * the group from different zones, so we need to deliver 25161 * the packet to each of them. Loop through the 25162 * multicast memberships structures (ilm) on the receive 25163 * ill and send a copy of the packet up each matching 25164 * one. However, we don't do this for multicasts sent on 25165 * the loopback interface (PHYI_LOOPBACK flag set) as 25166 * they must stay in the sender's zone. 25167 * 25168 * ilm_add_v6() ensures that ilms in the same zone are 25169 * contiguous in the ill_ilm list. We use this property 25170 * to avoid sending duplicates needed when two 25171 * applications in the same zone join the same group on 25172 * different logical interfaces: we ignore the ilm if 25173 * it's zoneid is the same as the last matching one. 25174 * In addition, the sending of the packet for 25175 * ire_zoneid is delayed until all of the other ilms 25176 * have been exhausted. 25177 */ 25178 last_zoneid = -1; 25179 ilm = ilm_walker_start(&ilw, ill); 25180 for (; ilm != NULL; ilm = ilm_walker_step(&ilw, ilm)) { 25181 if (ipha->ipha_dst != ilm->ilm_addr || 25182 ilm->ilm_zoneid == last_zoneid || 25183 ilm->ilm_zoneid == zoneid || 25184 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 25185 continue; 25186 mp1 = ip_copymsg(first_mp); 25187 if (mp1 == NULL) 25188 continue; 25189 icmp_inbound(q, mp1, B_TRUE, ilw.ilw_walk_ill, 25190 0, 0, mctl_present, B_FALSE, ill, 25191 ilm->ilm_zoneid); 25192 last_zoneid = ilm->ilm_zoneid; 25193 } 25194 ilm_walker_finish(&ilw); 25195 /* 25196 * Loopback case: the sending endpoint has 25197 * IP_MULTICAST_LOOP disabled, therefore we don't 25198 * dispatch the multicast packet to the sending zone. 25199 */ 25200 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 25201 freemsg(first_mp); 25202 return; 25203 } 25204 } else if (ire_type == IRE_BROADCAST) { 25205 /* 25206 * In the broadcast case, there may be many zones 25207 * which need a copy of the packet delivered to them. 25208 * There is one IRE_BROADCAST per broadcast address 25209 * and per zone; we walk those using a helper function. 25210 * In addition, the sending of the packet for zoneid is 25211 * delayed until all of the other ires have been 25212 * processed. 25213 */ 25214 IRB_REFHOLD(ire->ire_bucket); 25215 ire_zone = NULL; 25216 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 25217 ire)) != NULL) { 25218 mp1 = ip_copymsg(first_mp); 25219 if (mp1 == NULL) 25220 continue; 25221 25222 UPDATE_IB_PKT_COUNT(ire_zone); 25223 ire_zone->ire_last_used_time = lbolt; 25224 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 25225 mctl_present, B_FALSE, ill, 25226 ire_zone->ire_zoneid); 25227 } 25228 IRB_REFRELE(ire->ire_bucket); 25229 } 25230 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 25231 0, mctl_present, B_FALSE, ill, zoneid); 25232 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25233 "ip_wput_local_end: q %p (%S)", 25234 q, "icmp"); 25235 return; 25236 } 25237 case IPPROTO_IGMP: 25238 if ((mp = igmp_input(q, mp, ill)) == NULL) { 25239 /* Bad packet - discarded by igmp_input */ 25240 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25241 "ip_wput_local_end: q %p (%S)", 25242 q, "igmp_input--bad packet"); 25243 if (mctl_present) 25244 freeb(first_mp); 25245 return; 25246 } 25247 /* 25248 * igmp_input() may have returned the pulled up message. 25249 * So first_mp and ipha need to be reinitialized. 25250 */ 25251 ipha = (ipha_t *)mp->b_rptr; 25252 if (mctl_present) 25253 first_mp->b_cont = mp; 25254 else 25255 first_mp = mp; 25256 /* deliver to local raw users */ 25257 break; 25258 case IPPROTO_ENCAP: 25259 /* 25260 * This case is covered by either ip_fanout_proto, or by 25261 * the above security processing for self-tunneled packets. 25262 */ 25263 break; 25264 case IPPROTO_UDP: { 25265 uint16_t *up; 25266 uint32_t ports; 25267 25268 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 25269 UDP_PORTS_OFFSET); 25270 /* Force a 'valid' checksum. */ 25271 up[3] = 0; 25272 25273 ports = *(uint32_t *)up; 25274 ip_fanout_udp(q, first_mp, ill, ipha, ports, 25275 (ire_type == IRE_BROADCAST), 25276 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25277 IP_FF_SEND_SLLA | IP_FF_IPINFO, mctl_present, B_FALSE, 25278 ill, zoneid); 25279 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25280 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 25281 return; 25282 } 25283 case IPPROTO_TCP: { 25284 25285 /* 25286 * For TCP, discard broadcast packets. 25287 */ 25288 if ((ushort_t)ire_type == IRE_BROADCAST) { 25289 freemsg(first_mp); 25290 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 25291 ip2dbg(("ip_wput_local: discard broadcast\n")); 25292 return; 25293 } 25294 25295 if (mp->b_datap->db_type == M_DATA) { 25296 /* 25297 * M_DATA mblk, so init mblk (chain) for no struio(). 25298 */ 25299 mblk_t *mp1 = mp; 25300 25301 do { 25302 mp1->b_datap->db_struioflag = 0; 25303 } while ((mp1 = mp1->b_cont) != NULL); 25304 } 25305 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 25306 <= mp->b_wptr); 25307 ip_fanout_tcp(q, first_mp, ill, ipha, 25308 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25309 IP_FF_SYN_ADDIRE | IP_FF_IPINFO, 25310 mctl_present, B_FALSE, zoneid); 25311 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25312 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 25313 return; 25314 } 25315 case IPPROTO_SCTP: 25316 { 25317 uint32_t ports; 25318 25319 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 25320 ip_fanout_sctp(first_mp, ill, ipha, ports, 25321 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25322 IP_FF_IPINFO, mctl_present, B_FALSE, zoneid); 25323 return; 25324 } 25325 25326 default: 25327 break; 25328 } 25329 /* 25330 * Find a client for some other protocol. We give 25331 * copies to multiple clients, if more than one is 25332 * bound. 25333 */ 25334 ip_fanout_proto(q, first_mp, ill, ipha, 25335 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 25336 mctl_present, B_FALSE, ill, zoneid); 25337 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25338 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 25339 #undef rptr 25340 } 25341 25342 /* 25343 * Update any source route, record route, or timestamp options. 25344 * Check that we are at end of strict source route. 25345 * The options have been sanity checked by ip_wput_options(). 25346 */ 25347 static void 25348 ip_wput_local_options(ipha_t *ipha, ip_stack_t *ipst) 25349 { 25350 ipoptp_t opts; 25351 uchar_t *opt; 25352 uint8_t optval; 25353 uint8_t optlen; 25354 ipaddr_t dst; 25355 uint32_t ts; 25356 ire_t *ire; 25357 timestruc_t now; 25358 25359 ip2dbg(("ip_wput_local_options\n")); 25360 for (optval = ipoptp_first(&opts, ipha); 25361 optval != IPOPT_EOL; 25362 optval = ipoptp_next(&opts)) { 25363 opt = opts.ipoptp_cur; 25364 optlen = opts.ipoptp_len; 25365 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 25366 switch (optval) { 25367 uint32_t off; 25368 case IPOPT_SSRR: 25369 case IPOPT_LSRR: 25370 off = opt[IPOPT_OFFSET]; 25371 off--; 25372 if (optlen < IP_ADDR_LEN || 25373 off > optlen - IP_ADDR_LEN) { 25374 /* End of source route */ 25375 break; 25376 } 25377 /* 25378 * This will only happen if two consecutive entries 25379 * in the source route contains our address or if 25380 * it is a packet with a loose source route which 25381 * reaches us before consuming the whole source route 25382 */ 25383 ip1dbg(("ip_wput_local_options: not end of SR\n")); 25384 if (optval == IPOPT_SSRR) { 25385 return; 25386 } 25387 /* 25388 * Hack: instead of dropping the packet truncate the 25389 * source route to what has been used by filling the 25390 * rest with IPOPT_NOP. 25391 */ 25392 opt[IPOPT_OLEN] = (uint8_t)off; 25393 while (off < optlen) { 25394 opt[off++] = IPOPT_NOP; 25395 } 25396 break; 25397 case IPOPT_RR: 25398 off = opt[IPOPT_OFFSET]; 25399 off--; 25400 if (optlen < IP_ADDR_LEN || 25401 off > optlen - IP_ADDR_LEN) { 25402 /* No more room - ignore */ 25403 ip1dbg(( 25404 "ip_wput_forward_options: end of RR\n")); 25405 break; 25406 } 25407 dst = htonl(INADDR_LOOPBACK); 25408 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25409 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25410 break; 25411 case IPOPT_TS: 25412 /* Insert timestamp if there is romm */ 25413 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25414 case IPOPT_TS_TSONLY: 25415 off = IPOPT_TS_TIMELEN; 25416 break; 25417 case IPOPT_TS_PRESPEC: 25418 case IPOPT_TS_PRESPEC_RFC791: 25419 /* Verify that the address matched */ 25420 off = opt[IPOPT_OFFSET] - 1; 25421 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 25422 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 25423 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 25424 ipst); 25425 if (ire == NULL) { 25426 /* Not for us */ 25427 break; 25428 } 25429 ire_refrele(ire); 25430 /* FALLTHRU */ 25431 case IPOPT_TS_TSANDADDR: 25432 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 25433 break; 25434 default: 25435 /* 25436 * ip_*put_options should have already 25437 * dropped this packet. 25438 */ 25439 cmn_err(CE_PANIC, "ip_wput_local_options: " 25440 "unknown IT - bug in ip_wput_options?\n"); 25441 return; /* Keep "lint" happy */ 25442 } 25443 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 25444 /* Increase overflow counter */ 25445 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 25446 opt[IPOPT_POS_OV_FLG] = (uint8_t) 25447 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 25448 (off << 4); 25449 break; 25450 } 25451 off = opt[IPOPT_OFFSET] - 1; 25452 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25453 case IPOPT_TS_PRESPEC: 25454 case IPOPT_TS_PRESPEC_RFC791: 25455 case IPOPT_TS_TSANDADDR: 25456 dst = htonl(INADDR_LOOPBACK); 25457 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25458 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25459 /* FALLTHRU */ 25460 case IPOPT_TS_TSONLY: 25461 off = opt[IPOPT_OFFSET] - 1; 25462 /* Compute # of milliseconds since midnight */ 25463 gethrestime(&now); 25464 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 25465 now.tv_nsec / (NANOSEC / MILLISEC); 25466 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 25467 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 25468 break; 25469 } 25470 break; 25471 } 25472 } 25473 } 25474 25475 /* 25476 * Send out a multicast packet on interface ipif. 25477 * The sender does not have an conn. 25478 * Caller verifies that this isn't a PHYI_LOOPBACK. 25479 */ 25480 void 25481 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif, zoneid_t zoneid) 25482 { 25483 ipha_t *ipha; 25484 ire_t *ire; 25485 ipaddr_t dst; 25486 mblk_t *first_mp; 25487 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 25488 25489 /* igmp_sendpkt always allocates a ipsec_out_t */ 25490 ASSERT(mp->b_datap->db_type == M_CTL); 25491 ASSERT(!ipif->ipif_isv6); 25492 ASSERT(!IS_LOOPBACK(ipif->ipif_ill)); 25493 25494 first_mp = mp; 25495 mp = first_mp->b_cont; 25496 ASSERT(mp->b_datap->db_type == M_DATA); 25497 ipha = (ipha_t *)mp->b_rptr; 25498 25499 /* 25500 * Find an IRE which matches the destination and the outgoing 25501 * queue (i.e. the outgoing interface.) 25502 */ 25503 if (ipif->ipif_flags & IPIF_POINTOPOINT) 25504 dst = ipif->ipif_pp_dst_addr; 25505 else 25506 dst = ipha->ipha_dst; 25507 /* 25508 * The source address has already been initialized by the 25509 * caller and hence matching on ILL (MATCH_IRE_ILL) would 25510 * be sufficient rather than MATCH_IRE_IPIF. 25511 * 25512 * This function is used for sending IGMP packets. For IPMP, 25513 * we sidestep IGMP snooping issues by sending all multicast 25514 * traffic on a single interface in the IPMP group. 25515 */ 25516 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, NULL, 25517 MATCH_IRE_ILL, ipst); 25518 if (!ire) { 25519 /* 25520 * Mark this packet to make it be delivered to 25521 * ip_wput_ire after the new ire has been 25522 * created. 25523 */ 25524 mp->b_prev = NULL; 25525 mp->b_next = NULL; 25526 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC, 25527 zoneid, &zero_info); 25528 return; 25529 } 25530 25531 /* 25532 * Honor the RTF_SETSRC flag; this is the only case 25533 * where we force this addr whatever the current src addr is, 25534 * because this address is set by igmp_sendpkt(), and 25535 * cannot be specified by any user. 25536 */ 25537 if (ire->ire_flags & RTF_SETSRC) { 25538 ipha->ipha_src = ire->ire_src_addr; 25539 } 25540 25541 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE, zoneid); 25542 } 25543 25544 /* 25545 * NOTE : This function does not ire_refrele the ire argument passed in. 25546 * 25547 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 25548 * failure. The nce_fp_mp can vanish any time in the case of 25549 * IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 25550 * the ire_lock to access the nce_fp_mp in this case. 25551 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 25552 * prepending a fastpath message IPQoS processing must precede it, we also set 25553 * the b_band of the fastpath message to that of the mblk returned by IPQoS 25554 * (IPQoS might have set the b_band for CoS marking). 25555 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 25556 * must follow it so that IPQoS can mark the dl_priority field for CoS 25557 * marking, if needed. 25558 */ 25559 static mblk_t * 25560 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, 25561 uint32_t ill_index, ipha_t **iphap) 25562 { 25563 uint_t hlen; 25564 ipha_t *ipha; 25565 mblk_t *mp1; 25566 boolean_t qos_done = B_FALSE; 25567 uchar_t *ll_hdr; 25568 ip_stack_t *ipst = ire->ire_ipst; 25569 25570 #define rptr ((uchar_t *)ipha) 25571 25572 ipha = (ipha_t *)mp->b_rptr; 25573 hlen = 0; 25574 LOCK_IRE_FP_MP(ire); 25575 if ((mp1 = ire->ire_nce->nce_fp_mp) != NULL) { 25576 ASSERT(DB_TYPE(mp1) == M_DATA); 25577 /* Initiate IPPF processing */ 25578 if ((proc != 0) && IPP_ENABLED(proc, ipst)) { 25579 UNLOCK_IRE_FP_MP(ire); 25580 ip_process(proc, &mp, ill_index); 25581 if (mp == NULL) 25582 return (NULL); 25583 25584 ipha = (ipha_t *)mp->b_rptr; 25585 LOCK_IRE_FP_MP(ire); 25586 if ((mp1 = ire->ire_nce->nce_fp_mp) == NULL) { 25587 qos_done = B_TRUE; 25588 goto no_fp_mp; 25589 } 25590 ASSERT(DB_TYPE(mp1) == M_DATA); 25591 } 25592 hlen = MBLKL(mp1); 25593 /* 25594 * Check if we have enough room to prepend fastpath 25595 * header 25596 */ 25597 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 25598 ll_hdr = rptr - hlen; 25599 bcopy(mp1->b_rptr, ll_hdr, hlen); 25600 /* 25601 * Set the b_rptr to the start of the link layer 25602 * header 25603 */ 25604 mp->b_rptr = ll_hdr; 25605 mp1 = mp; 25606 } else { 25607 mp1 = copyb(mp1); 25608 if (mp1 == NULL) 25609 goto unlock_err; 25610 mp1->b_band = mp->b_band; 25611 mp1->b_cont = mp; 25612 /* 25613 * XXX disable ICK_VALID and compute checksum 25614 * here; can happen if nce_fp_mp changes and 25615 * it can't be copied now due to insufficient 25616 * space. (unlikely, fp mp can change, but it 25617 * does not increase in length) 25618 */ 25619 } 25620 UNLOCK_IRE_FP_MP(ire); 25621 } else { 25622 no_fp_mp: 25623 mp1 = copyb(ire->ire_nce->nce_res_mp); 25624 if (mp1 == NULL) { 25625 unlock_err: 25626 UNLOCK_IRE_FP_MP(ire); 25627 freemsg(mp); 25628 return (NULL); 25629 } 25630 UNLOCK_IRE_FP_MP(ire); 25631 mp1->b_cont = mp; 25632 if (!qos_done && (proc != 0) && IPP_ENABLED(proc, ipst)) { 25633 ip_process(proc, &mp1, ill_index); 25634 if (mp1 == NULL) 25635 return (NULL); 25636 25637 if (mp1->b_cont == NULL) 25638 ipha = NULL; 25639 else 25640 ipha = (ipha_t *)mp1->b_cont->b_rptr; 25641 } 25642 } 25643 25644 *iphap = ipha; 25645 return (mp1); 25646 #undef rptr 25647 } 25648 25649 /* 25650 * Finish the outbound IPsec processing for an IPv6 packet. This function 25651 * is called from ipsec_out_process() if the IPsec packet was processed 25652 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25653 * asynchronously. 25654 */ 25655 void 25656 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 25657 ire_t *ire_arg) 25658 { 25659 in6_addr_t *v6dstp; 25660 ire_t *ire; 25661 mblk_t *mp; 25662 ip6_t *ip6h1; 25663 uint_t ill_index; 25664 ipsec_out_t *io; 25665 boolean_t hwaccel; 25666 uint32_t flags = IP6_NO_IPPOLICY; 25667 int match_flags; 25668 zoneid_t zoneid; 25669 boolean_t ill_need_rele = B_FALSE; 25670 boolean_t ire_need_rele = B_FALSE; 25671 ip_stack_t *ipst; 25672 25673 mp = ipsec_mp->b_cont; 25674 ip6h1 = (ip6_t *)mp->b_rptr; 25675 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25676 ASSERT(io->ipsec_out_ns != NULL); 25677 ipst = io->ipsec_out_ns->netstack_ip; 25678 ill_index = io->ipsec_out_ill_index; 25679 if (io->ipsec_out_reachable) { 25680 flags |= IPV6_REACHABILITY_CONFIRMATION; 25681 } 25682 hwaccel = io->ipsec_out_accelerated; 25683 zoneid = io->ipsec_out_zoneid; 25684 ASSERT(zoneid != ALL_ZONES); 25685 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 25686 /* Multicast addresses should have non-zero ill_index. */ 25687 v6dstp = &ip6h->ip6_dst; 25688 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 25689 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 25690 25691 if (ill == NULL && ill_index != 0) { 25692 ill = ip_grab_ill(ipsec_mp, ill_index, B_TRUE, ipst); 25693 /* Failure case frees things for us. */ 25694 if (ill == NULL) 25695 return; 25696 25697 ill_need_rele = B_TRUE; 25698 } 25699 ASSERT(mp != NULL); 25700 25701 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 25702 boolean_t unspec_src; 25703 ipif_t *ipif; 25704 25705 /* 25706 * Use the ill_index to get the right ill. 25707 */ 25708 unspec_src = io->ipsec_out_unspec_src; 25709 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25710 if (ipif == NULL) { 25711 if (ill_need_rele) 25712 ill_refrele(ill); 25713 freemsg(ipsec_mp); 25714 return; 25715 } 25716 25717 if (ire_arg != NULL) { 25718 ire = ire_arg; 25719 } else { 25720 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25721 zoneid, msg_getlabel(mp), match_flags, ipst); 25722 ire_need_rele = B_TRUE; 25723 } 25724 if (ire != NULL) { 25725 ipif_refrele(ipif); 25726 /* 25727 * XXX Do the multicast forwarding now, as the IPsec 25728 * processing has been done. 25729 */ 25730 goto send; 25731 } 25732 25733 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 25734 mp->b_prev = NULL; 25735 mp->b_next = NULL; 25736 25737 /* 25738 * If the IPsec packet was processed asynchronously, 25739 * drop it now. 25740 */ 25741 if (q == NULL) { 25742 if (ill_need_rele) 25743 ill_refrele(ill); 25744 freemsg(ipsec_mp); 25745 return; 25746 } 25747 25748 ip_newroute_ipif_v6(q, ipsec_mp, ipif, v6dstp, &ip6h->ip6_src, 25749 unspec_src, zoneid); 25750 ipif_refrele(ipif); 25751 } else { 25752 if (ire_arg != NULL) { 25753 ire = ire_arg; 25754 } else { 25755 ire = ire_cache_lookup_v6(v6dstp, zoneid, NULL, ipst); 25756 ire_need_rele = B_TRUE; 25757 } 25758 if (ire != NULL) 25759 goto send; 25760 /* 25761 * ire disappeared underneath. 25762 * 25763 * What we need to do here is the ip_newroute 25764 * logic to get the ire without doing the IPsec 25765 * processing. Follow the same old path. But this 25766 * time, ip_wput or ire_add_then_send will call us 25767 * directly as all the IPsec operations are done. 25768 */ 25769 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 25770 mp->b_prev = NULL; 25771 mp->b_next = NULL; 25772 25773 /* 25774 * If the IPsec packet was processed asynchronously, 25775 * drop it now. 25776 */ 25777 if (q == NULL) { 25778 if (ill_need_rele) 25779 ill_refrele(ill); 25780 freemsg(ipsec_mp); 25781 return; 25782 } 25783 25784 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 25785 zoneid, ipst); 25786 } 25787 if (ill != NULL && ill_need_rele) 25788 ill_refrele(ill); 25789 return; 25790 send: 25791 if (ill != NULL && ill_need_rele) 25792 ill_refrele(ill); 25793 25794 /* Local delivery */ 25795 if (ire->ire_stq == NULL) { 25796 ill_t *out_ill; 25797 ASSERT(q != NULL); 25798 25799 /* PFHooks: LOOPBACK_OUT */ 25800 out_ill = ire_to_ill(ire); 25801 25802 /* 25803 * DTrace this as ip:::send. A blocked packet will fire the 25804 * send probe, but not the receive probe. 25805 */ 25806 DTRACE_IP7(send, mblk_t *, ipsec_mp, conn_t *, NULL, 25807 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, out_ill, 25808 ipha_t *, NULL, ip6_t *, ip6h, int, 1); 25809 25810 DTRACE_PROBE4(ip6__loopback__out__start, 25811 ill_t *, NULL, ill_t *, out_ill, 25812 ip6_t *, ip6h1, mblk_t *, ipsec_mp); 25813 25814 FW_HOOKS6(ipst->ips_ip6_loopback_out_event, 25815 ipst->ips_ipv6firewall_loopback_out, 25816 NULL, out_ill, ip6h1, ipsec_mp, mp, 0, ipst); 25817 25818 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, ipsec_mp); 25819 25820 if (ipsec_mp != NULL) { 25821 ip_wput_local_v6(RD(q), out_ill, 25822 ip6h, ipsec_mp, ire, 0, zoneid); 25823 } 25824 if (ire_need_rele) 25825 ire_refrele(ire); 25826 return; 25827 } 25828 /* 25829 * Everything is done. Send it out on the wire. 25830 * We force the insertion of a fragment header using the 25831 * IPH_FRAG_HDR flag in two cases: 25832 * - after reception of an ICMPv6 "packet too big" message 25833 * with a MTU < 1280 (cf. RFC 2460 section 5) 25834 * - for multirouted IPv6 packets, so that the receiver can 25835 * discard duplicates according to their fragment identifier 25836 */ 25837 /* XXX fix flow control problems. */ 25838 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 25839 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 25840 if (hwaccel) { 25841 /* 25842 * hardware acceleration does not handle these 25843 * "slow path" cases. 25844 */ 25845 /* IPsec KSTATS: should bump bean counter here. */ 25846 if (ire_need_rele) 25847 ire_refrele(ire); 25848 freemsg(ipsec_mp); 25849 return; 25850 } 25851 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 25852 (mp->b_cont ? msgdsize(mp) : 25853 mp->b_wptr - (uchar_t *)ip6h)) { 25854 /* IPsec KSTATS: should bump bean counter here. */ 25855 ip0dbg(("Packet length mismatch: %d, %ld\n", 25856 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 25857 msgdsize(mp))); 25858 if (ire_need_rele) 25859 ire_refrele(ire); 25860 freemsg(ipsec_mp); 25861 return; 25862 } 25863 ASSERT(mp->b_prev == NULL); 25864 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 25865 ntohs(ip6h->ip6_plen) + 25866 IPV6_HDR_LEN, ire->ire_max_frag)); 25867 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 25868 ire->ire_max_frag); 25869 } else { 25870 UPDATE_OB_PKT_COUNT(ire); 25871 ire->ire_last_used_time = lbolt; 25872 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 25873 } 25874 if (ire_need_rele) 25875 ire_refrele(ire); 25876 freeb(ipsec_mp); 25877 } 25878 25879 void 25880 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 25881 { 25882 mblk_t *hada_mp; /* attributes M_CTL mblk */ 25883 da_ipsec_t *hada; /* data attributes */ 25884 ill_t *ill = (ill_t *)q->q_ptr; 25885 25886 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 25887 25888 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 25889 /* IPsec KSTATS: Bump lose counter here! */ 25890 freemsg(mp); 25891 return; 25892 } 25893 25894 /* 25895 * It's an IPsec packet that must be 25896 * accelerated by the Provider, and the 25897 * outbound ill is IPsec acceleration capable. 25898 * Prepends the mblk with an IPHADA_M_CTL, and ship it 25899 * to the ill. 25900 * IPsec KSTATS: should bump packet counter here. 25901 */ 25902 25903 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 25904 if (hada_mp == NULL) { 25905 /* IPsec KSTATS: should bump packet counter here. */ 25906 freemsg(mp); 25907 return; 25908 } 25909 25910 hada_mp->b_datap->db_type = M_CTL; 25911 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 25912 hada_mp->b_cont = mp; 25913 25914 hada = (da_ipsec_t *)hada_mp->b_rptr; 25915 bzero(hada, sizeof (da_ipsec_t)); 25916 hada->da_type = IPHADA_M_CTL; 25917 25918 putnext(q, hada_mp); 25919 } 25920 25921 /* 25922 * Finish the outbound IPsec processing. This function is called from 25923 * ipsec_out_process() if the IPsec packet was processed 25924 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25925 * asynchronously. 25926 */ 25927 void 25928 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 25929 ire_t *ire_arg) 25930 { 25931 uint32_t v_hlen_tos_len; 25932 ipaddr_t dst; 25933 ipif_t *ipif = NULL; 25934 ire_t *ire; 25935 ire_t *ire1 = NULL; 25936 mblk_t *next_mp = NULL; 25937 uint32_t max_frag; 25938 boolean_t multirt_send = B_FALSE; 25939 mblk_t *mp; 25940 ipha_t *ipha1; 25941 uint_t ill_index; 25942 ipsec_out_t *io; 25943 int match_flags; 25944 irb_t *irb = NULL; 25945 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 25946 zoneid_t zoneid; 25947 ipxmit_state_t pktxmit_state; 25948 ip_stack_t *ipst; 25949 25950 #ifdef _BIG_ENDIAN 25951 #define LENGTH (v_hlen_tos_len & 0xFFFF) 25952 #else 25953 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 25954 #endif 25955 25956 mp = ipsec_mp->b_cont; 25957 ipha1 = (ipha_t *)mp->b_rptr; 25958 ASSERT(mp != NULL); 25959 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 25960 dst = ipha->ipha_dst; 25961 25962 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25963 ill_index = io->ipsec_out_ill_index; 25964 zoneid = io->ipsec_out_zoneid; 25965 ASSERT(zoneid != ALL_ZONES); 25966 ipst = io->ipsec_out_ns->netstack_ip; 25967 ASSERT(io->ipsec_out_ns != NULL); 25968 25969 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 25970 if (ill == NULL && ill_index != 0) { 25971 ill = ip_grab_ill(ipsec_mp, ill_index, B_FALSE, ipst); 25972 /* Failure case frees things for us. */ 25973 if (ill == NULL) 25974 return; 25975 25976 ill_need_rele = B_TRUE; 25977 } 25978 25979 if (CLASSD(dst)) { 25980 boolean_t conn_dontroute; 25981 /* 25982 * Use the ill_index to get the right ipif. 25983 */ 25984 conn_dontroute = io->ipsec_out_dontroute; 25985 if (ill_index == 0) 25986 ipif = ipif_lookup_group(dst, zoneid, ipst); 25987 else 25988 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25989 if (ipif == NULL) { 25990 ip1dbg(("ip_wput_ipsec_out: No ipif for" 25991 " multicast\n")); 25992 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); 25993 freemsg(ipsec_mp); 25994 goto done; 25995 } 25996 /* 25997 * ipha_src has already been intialized with the 25998 * value of the ipif in ip_wput. All we need now is 25999 * an ire to send this downstream. 26000 */ 26001 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 26002 msg_getlabel(mp), match_flags, ipst); 26003 if (ire != NULL) { 26004 ill_t *ill1; 26005 /* 26006 * Do the multicast forwarding now, as the IPsec 26007 * processing has been done. 26008 */ 26009 if (ipst->ips_ip_g_mrouter && !conn_dontroute && 26010 (ill1 = ire_to_ill(ire))) { 26011 if (ip_mforward(ill1, ipha, mp)) { 26012 freemsg(ipsec_mp); 26013 ip1dbg(("ip_wput_ipsec_out: mforward " 26014 "failed\n")); 26015 ire_refrele(ire); 26016 goto done; 26017 } 26018 } 26019 goto send; 26020 } 26021 26022 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 26023 mp->b_prev = NULL; 26024 mp->b_next = NULL; 26025 26026 /* 26027 * If the IPsec packet was processed asynchronously, 26028 * drop it now. 26029 */ 26030 if (q == NULL) { 26031 freemsg(ipsec_mp); 26032 goto done; 26033 } 26034 26035 /* 26036 * We may be using a wrong ipif to create the ire. 26037 * But it is okay as the source address is assigned 26038 * for the packet already. Next outbound packet would 26039 * create the IRE with the right IPIF in ip_wput. 26040 * 26041 * Also handle RTF_MULTIRT routes. 26042 */ 26043 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT, 26044 zoneid, &zero_info); 26045 } else { 26046 if (ire_arg != NULL) { 26047 ire = ire_arg; 26048 ire_need_rele = B_FALSE; 26049 } else { 26050 ire = ire_cache_lookup(dst, zoneid, 26051 msg_getlabel(mp), ipst); 26052 } 26053 if (ire != NULL) { 26054 goto send; 26055 } 26056 26057 /* 26058 * ire disappeared underneath. 26059 * 26060 * What we need to do here is the ip_newroute 26061 * logic to get the ire without doing the IPsec 26062 * processing. Follow the same old path. But this 26063 * time, ip_wput or ire_add_then_put will call us 26064 * directly as all the IPsec operations are done. 26065 */ 26066 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 26067 mp->b_prev = NULL; 26068 mp->b_next = NULL; 26069 26070 /* 26071 * If the IPsec packet was processed asynchronously, 26072 * drop it now. 26073 */ 26074 if (q == NULL) { 26075 freemsg(ipsec_mp); 26076 goto done; 26077 } 26078 26079 /* 26080 * Since we're going through ip_newroute() again, we 26081 * need to make sure we don't: 26082 * 26083 * 1.) Trigger the ASSERT() with the ipha_ident 26084 * overloading. 26085 * 2.) Redo transport-layer checksumming, since we've 26086 * already done all that to get this far. 26087 * 26088 * The easiest way not do either of the above is to set 26089 * the ipha_ident field to IP_HDR_INCLUDED. 26090 */ 26091 ipha->ipha_ident = IP_HDR_INCLUDED; 26092 ip_newroute(q, ipsec_mp, dst, (CONN_Q(q) ? Q_TO_CONN(q) : NULL), 26093 zoneid, ipst); 26094 } 26095 goto done; 26096 send: 26097 if (ire->ire_stq == NULL) { 26098 ill_t *out_ill; 26099 /* 26100 * Loopbacks go through ip_wput_local except for one case. 26101 * We come here if we generate a icmp_frag_needed message 26102 * after IPsec processing is over. When this function calls 26103 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 26104 * icmp_frag_needed. The message generated comes back here 26105 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 26106 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 26107 * source address as it is usually set in ip_wput_ire. As 26108 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 26109 * and we end up here. We can't enter ip_wput_ire once the 26110 * IPsec processing is over and hence we need to do it here. 26111 */ 26112 ASSERT(q != NULL); 26113 UPDATE_OB_PKT_COUNT(ire); 26114 ire->ire_last_used_time = lbolt; 26115 if (ipha->ipha_src == 0) 26116 ipha->ipha_src = ire->ire_src_addr; 26117 26118 /* PFHooks: LOOPBACK_OUT */ 26119 out_ill = ire_to_ill(ire); 26120 26121 /* 26122 * DTrace this as ip:::send. A blocked packet will fire the 26123 * send probe, but not the receive probe. 26124 */ 26125 DTRACE_IP7(send, mblk_t *, ipsec_mp, conn_t *, NULL, 26126 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, out_ill, 26127 ipha_t *, ipha, ip6_t *, NULL, int, 1); 26128 26129 DTRACE_PROBE4(ip4__loopback__out__start, 26130 ill_t *, NULL, ill_t *, out_ill, 26131 ipha_t *, ipha1, mblk_t *, ipsec_mp); 26132 26133 FW_HOOKS(ipst->ips_ip4_loopback_out_event, 26134 ipst->ips_ipv4firewall_loopback_out, 26135 NULL, out_ill, ipha1, ipsec_mp, mp, 0, ipst); 26136 26137 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp); 26138 26139 if (ipsec_mp != NULL) 26140 ip_wput_local(RD(q), out_ill, 26141 ipha, ipsec_mp, ire, 0, zoneid); 26142 if (ire_need_rele) 26143 ire_refrele(ire); 26144 goto done; 26145 } 26146 26147 if (ire->ire_max_frag < (unsigned int)LENGTH) { 26148 /* 26149 * We are through with IPsec processing. 26150 * Fragment this and send it on the wire. 26151 */ 26152 if (io->ipsec_out_accelerated) { 26153 /* 26154 * The packet has been accelerated but must 26155 * be fragmented. This should not happen 26156 * since AH and ESP must not accelerate 26157 * packets that need fragmentation, however 26158 * the configuration could have changed 26159 * since the AH or ESP processing. 26160 * Drop packet. 26161 * IPsec KSTATS: bump bean counter here. 26162 */ 26163 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 26164 "fragmented accelerated packet!\n")); 26165 freemsg(ipsec_mp); 26166 } else { 26167 ip_wput_ire_fragmentit(ipsec_mp, ire, 26168 zoneid, ipst, NULL); 26169 } 26170 if (ire_need_rele) 26171 ire_refrele(ire); 26172 goto done; 26173 } 26174 26175 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 26176 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 26177 (void *)ire->ire_ipif, (void *)ipif)); 26178 26179 /* 26180 * Multiroute the secured packet. 26181 */ 26182 if (ire->ire_flags & RTF_MULTIRT) { 26183 ire_t *first_ire; 26184 irb = ire->ire_bucket; 26185 ASSERT(irb != NULL); 26186 /* 26187 * This ire has been looked up as the one that 26188 * goes through the given ipif; 26189 * make sure we do not omit any other multiroute ire 26190 * that may be present in the bucket before this one. 26191 */ 26192 IRB_REFHOLD(irb); 26193 for (first_ire = irb->irb_ire; 26194 first_ire != NULL; 26195 first_ire = first_ire->ire_next) { 26196 if ((first_ire->ire_flags & RTF_MULTIRT) && 26197 (first_ire->ire_addr == ire->ire_addr) && 26198 !(first_ire->ire_marks & 26199 (IRE_MARK_CONDEMNED | IRE_MARK_TESTHIDDEN))) 26200 break; 26201 } 26202 26203 if ((first_ire != NULL) && (first_ire != ire)) { 26204 /* 26205 * Don't change the ire if the packet must 26206 * be fragmented if sent via this new one. 26207 */ 26208 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 26209 IRE_REFHOLD(first_ire); 26210 if (ire_need_rele) 26211 ire_refrele(ire); 26212 else 26213 ire_need_rele = B_TRUE; 26214 ire = first_ire; 26215 } 26216 } 26217 IRB_REFRELE(irb); 26218 26219 multirt_send = B_TRUE; 26220 max_frag = ire->ire_max_frag; 26221 } 26222 26223 /* 26224 * In most cases, the emission loop below is entered only once. 26225 * Only in the case where the ire holds the RTF_MULTIRT 26226 * flag, we loop to process all RTF_MULTIRT ires in the 26227 * bucket, and send the packet through all crossed 26228 * RTF_MULTIRT routes. 26229 */ 26230 do { 26231 if (multirt_send) { 26232 /* 26233 * ire1 holds here the next ire to process in the 26234 * bucket. If multirouting is expected, 26235 * any non-RTF_MULTIRT ire that has the 26236 * right destination address is ignored. 26237 */ 26238 ASSERT(irb != NULL); 26239 IRB_REFHOLD(irb); 26240 for (ire1 = ire->ire_next; 26241 ire1 != NULL; 26242 ire1 = ire1->ire_next) { 26243 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 26244 continue; 26245 if (ire1->ire_addr != ire->ire_addr) 26246 continue; 26247 if (ire1->ire_marks & 26248 (IRE_MARK_CONDEMNED | IRE_MARK_TESTHIDDEN)) 26249 continue; 26250 /* No loopback here */ 26251 if (ire1->ire_stq == NULL) 26252 continue; 26253 /* 26254 * Ensure we do not exceed the MTU 26255 * of the next route. 26256 */ 26257 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 26258 ip_multirt_bad_mtu(ire1, max_frag); 26259 continue; 26260 } 26261 26262 IRE_REFHOLD(ire1); 26263 break; 26264 } 26265 IRB_REFRELE(irb); 26266 if (ire1 != NULL) { 26267 /* 26268 * We are in a multiple send case, need to 26269 * make a copy of the packet. 26270 */ 26271 next_mp = copymsg(ipsec_mp); 26272 if (next_mp == NULL) { 26273 ire_refrele(ire1); 26274 ire1 = NULL; 26275 } 26276 } 26277 } 26278 /* 26279 * Everything is done. Send it out on the wire 26280 * 26281 * ip_xmit_v4 will call ip_wput_attach_llhdr and then 26282 * either send it on the wire or, in the case of 26283 * HW acceleration, call ipsec_hw_putnext. 26284 */ 26285 if (ire->ire_nce && 26286 ire->ire_nce->nce_state != ND_REACHABLE) { 26287 DTRACE_PROBE2(ip__wput__ipsec__bail, 26288 (ire_t *), ire, (mblk_t *), ipsec_mp); 26289 /* 26290 * If ire's link-layer is unresolved (this 26291 * would only happen if the incomplete ire 26292 * was added to cachetable via forwarding path) 26293 * don't bother going to ip_xmit_v4. Just drop the 26294 * packet. 26295 * There is a slight risk here, in that, if we 26296 * have the forwarding path create an incomplete 26297 * IRE, then until the IRE is completed, any 26298 * transmitted IPsec packets will be dropped 26299 * instead of being queued waiting for resolution. 26300 * 26301 * But the likelihood of a forwarding packet and a wput 26302 * packet sending to the same dst at the same time 26303 * and there not yet be an ARP entry for it is small. 26304 * Furthermore, if this actually happens, it might 26305 * be likely that wput would generate multiple 26306 * packets (and forwarding would also have a train 26307 * of packets) for that destination. If this is 26308 * the case, some of them would have been dropped 26309 * anyway, since ARP only queues a few packets while 26310 * waiting for resolution 26311 * 26312 * NOTE: We should really call ip_xmit_v4, 26313 * and let it queue the packet and send the 26314 * ARP query and have ARP come back thus: 26315 * <ARP> ip_wput->ip_output->ip-wput_nondata-> 26316 * ip_xmit_v4->ip_wput_attach_llhdr + ipsec 26317 * hw accel work. But it's too complex to get 26318 * the IPsec hw acceleration approach to fit 26319 * well with ip_xmit_v4 doing ARP without 26320 * doing IPsec simplification. For now, we just 26321 * poke ip_xmit_v4 to trigger the arp resolve, so 26322 * that we can continue with the send on the next 26323 * attempt. 26324 * 26325 * XXX THis should be revisited, when 26326 * the IPsec/IP interaction is cleaned up 26327 */ 26328 ip1dbg(("ip_wput_ipsec_out: ire is incomplete" 26329 " - dropping packet\n")); 26330 freemsg(ipsec_mp); 26331 /* 26332 * Call ip_xmit_v4() to trigger ARP query 26333 * in case the nce_state is ND_INITIAL 26334 */ 26335 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE, NULL); 26336 goto drop_pkt; 26337 } 26338 26339 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 26340 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha1, 26341 mblk_t *, ipsec_mp); 26342 FW_HOOKS(ipst->ips_ip4_physical_out_event, 26343 ipst->ips_ipv4firewall_physical_out, NULL, 26344 ire->ire_ipif->ipif_ill, ipha1, ipsec_mp, mp, 0, ipst); 26345 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, ipsec_mp); 26346 if (ipsec_mp == NULL) 26347 goto drop_pkt; 26348 26349 ip1dbg(("ip_wput_ipsec_out: calling ip_xmit_v4\n")); 26350 pktxmit_state = ip_xmit_v4(mp, ire, 26351 (io->ipsec_out_accelerated ? io : NULL), B_FALSE, NULL); 26352 26353 if ((pktxmit_state == SEND_FAILED) || 26354 (pktxmit_state == LLHDR_RESLV_FAILED)) { 26355 26356 freeb(ipsec_mp); /* ip_xmit_v4 frees the mp */ 26357 drop_pkt: 26358 BUMP_MIB(((ill_t *)ire->ire_stq->q_ptr)->ill_ip_mib, 26359 ipIfStatsOutDiscards); 26360 if (ire_need_rele) 26361 ire_refrele(ire); 26362 if (ire1 != NULL) { 26363 ire_refrele(ire1); 26364 freemsg(next_mp); 26365 } 26366 goto done; 26367 } 26368 26369 freeb(ipsec_mp); 26370 if (ire_need_rele) 26371 ire_refrele(ire); 26372 26373 if (ire1 != NULL) { 26374 ire = ire1; 26375 ire_need_rele = B_TRUE; 26376 ASSERT(next_mp); 26377 ipsec_mp = next_mp; 26378 mp = ipsec_mp->b_cont; 26379 ire1 = NULL; 26380 next_mp = NULL; 26381 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26382 } else { 26383 multirt_send = B_FALSE; 26384 } 26385 } while (multirt_send); 26386 done: 26387 if (ill != NULL && ill_need_rele) 26388 ill_refrele(ill); 26389 if (ipif != NULL) 26390 ipif_refrele(ipif); 26391 } 26392 26393 /* 26394 * Get the ill corresponding to the specified ire, and compare its 26395 * capabilities with the protocol and algorithms specified by the 26396 * the SA obtained from ipsec_out. If they match, annotate the 26397 * ipsec_out structure to indicate that the packet needs acceleration. 26398 * 26399 * 26400 * A packet is eligible for outbound hardware acceleration if the 26401 * following conditions are satisfied: 26402 * 26403 * 1. the packet will not be fragmented 26404 * 2. the provider supports the algorithm 26405 * 3. there is no pending control message being exchanged 26406 * 4. snoop is not attached 26407 * 5. the destination address is not a broadcast or multicast address. 26408 * 26409 * Rationale: 26410 * - Hardware drivers do not support fragmentation with 26411 * the current interface. 26412 * - snoop, multicast, and broadcast may result in exposure of 26413 * a cleartext datagram. 26414 * We check all five of these conditions here. 26415 * 26416 * XXX would like to nuke "ire_t *" parameter here; problem is that 26417 * IRE is only way to figure out if a v4 address is a broadcast and 26418 * thus ineligible for acceleration... 26419 */ 26420 static void 26421 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 26422 { 26423 ipsec_out_t *io; 26424 mblk_t *data_mp; 26425 uint_t plen, overhead; 26426 ip_stack_t *ipst; 26427 phyint_t *phyint; 26428 26429 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 26430 return; 26431 26432 if (ill == NULL) 26433 return; 26434 ipst = ill->ill_ipst; 26435 phyint = ill->ill_phyint; 26436 26437 /* 26438 * Destination address is a broadcast or multicast. Punt. 26439 */ 26440 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 26441 IRE_LOCAL))) 26442 return; 26443 26444 data_mp = ipsec_mp->b_cont; 26445 26446 if (ill->ill_isv6) { 26447 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 26448 26449 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 26450 return; 26451 26452 plen = ip6h->ip6_plen; 26453 } else { 26454 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 26455 26456 if (CLASSD(ipha->ipha_dst)) 26457 return; 26458 26459 plen = ipha->ipha_length; 26460 } 26461 /* 26462 * Is there a pending DLPI control message being exchanged 26463 * between IP/IPsec and the DLS Provider? If there is, it 26464 * could be a SADB update, and the state of the DLS Provider 26465 * SADB might not be in sync with the SADB maintained by 26466 * IPsec. To avoid dropping packets or using the wrong keying 26467 * material, we do not accelerate this packet. 26468 */ 26469 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 26470 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26471 "ill_dlpi_pending! don't accelerate packet\n")); 26472 return; 26473 } 26474 26475 /* 26476 * Is the Provider in promiscous mode? If it does, we don't 26477 * accelerate the packet since it will bounce back up to the 26478 * listeners in the clear. 26479 */ 26480 if (phyint->phyint_flags & PHYI_PROMISC) { 26481 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26482 "ill in promiscous mode, don't accelerate packet\n")); 26483 return; 26484 } 26485 26486 /* 26487 * Will the packet require fragmentation? 26488 */ 26489 26490 /* 26491 * IPsec ESP note: this is a pessimistic estimate, but the same 26492 * as is used elsewhere. 26493 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 26494 * + 2-byte trailer 26495 */ 26496 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 26497 IPSEC_BASE_ESP_HDR_SIZE(sa); 26498 26499 if ((plen + overhead) > ill->ill_max_mtu) 26500 return; 26501 26502 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26503 26504 /* 26505 * Can the ill accelerate this IPsec protocol and algorithm 26506 * specified by the SA? 26507 */ 26508 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 26509 ill->ill_isv6, sa, ipst->ips_netstack)) { 26510 return; 26511 } 26512 26513 /* 26514 * Tell AH or ESP that the outbound ill is capable of 26515 * accelerating this packet. 26516 */ 26517 io->ipsec_out_is_capab_ill = B_TRUE; 26518 } 26519 26520 /* 26521 * Select which AH & ESP SA's to use (if any) for the outbound packet. 26522 * 26523 * If this function returns B_TRUE, the requested SA's have been filled 26524 * into the ipsec_out_*_sa pointers. 26525 * 26526 * If the function returns B_FALSE, the packet has been "consumed", most 26527 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 26528 * 26529 * The SA references created by the protocol-specific "select" 26530 * function will be released when the ipsec_mp is freed, thanks to the 26531 * ipsec_out_free destructor -- see spd.c. 26532 */ 26533 static boolean_t 26534 ipsec_out_select_sa(mblk_t *ipsec_mp) 26535 { 26536 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 26537 ipsec_out_t *io; 26538 ipsec_policy_t *pp; 26539 ipsec_action_t *ap; 26540 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26541 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26542 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26543 26544 if (!io->ipsec_out_secure) { 26545 /* 26546 * We came here by mistake. 26547 * Don't bother with ipsec processing 26548 * We should "discourage" this path in the future. 26549 */ 26550 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26551 return (B_FALSE); 26552 } 26553 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26554 ASSERT((io->ipsec_out_policy != NULL) || 26555 (io->ipsec_out_act != NULL)); 26556 26557 ASSERT(io->ipsec_out_failed == B_FALSE); 26558 26559 /* 26560 * IPsec processing has started. 26561 */ 26562 io->ipsec_out_proc_begin = B_TRUE; 26563 ap = io->ipsec_out_act; 26564 if (ap == NULL) { 26565 pp = io->ipsec_out_policy; 26566 ASSERT(pp != NULL); 26567 ap = pp->ipsp_act; 26568 ASSERT(ap != NULL); 26569 } 26570 26571 /* 26572 * We have an action. now, let's select SA's. 26573 * (In the future, we can cache this in the conn_t..) 26574 */ 26575 if (ap->ipa_want_esp) { 26576 if (io->ipsec_out_esp_sa == NULL) { 26577 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 26578 IPPROTO_ESP); 26579 } 26580 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 26581 } 26582 26583 if (ap->ipa_want_ah) { 26584 if (io->ipsec_out_ah_sa == NULL) { 26585 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 26586 IPPROTO_AH); 26587 } 26588 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 26589 /* 26590 * The ESP and AH processing order needs to be preserved 26591 * when both protocols are required (ESP should be applied 26592 * before AH for an outbound packet). Force an ESP ACQUIRE 26593 * when both ESP and AH are required, and an AH ACQUIRE 26594 * is needed. 26595 */ 26596 if (ap->ipa_want_esp && need_ah_acquire) 26597 need_esp_acquire = B_TRUE; 26598 } 26599 26600 /* 26601 * Send an ACQUIRE (extended, regular, or both) if we need one. 26602 * Release SAs that got referenced, but will not be used until we 26603 * acquire _all_ of the SAs we need. 26604 */ 26605 if (need_ah_acquire || need_esp_acquire) { 26606 if (io->ipsec_out_ah_sa != NULL) { 26607 IPSA_REFRELE(io->ipsec_out_ah_sa); 26608 io->ipsec_out_ah_sa = NULL; 26609 } 26610 if (io->ipsec_out_esp_sa != NULL) { 26611 IPSA_REFRELE(io->ipsec_out_esp_sa); 26612 io->ipsec_out_esp_sa = NULL; 26613 } 26614 26615 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 26616 return (B_FALSE); 26617 } 26618 26619 return (B_TRUE); 26620 } 26621 26622 /* 26623 * Process an IPSEC_OUT message and see what you can 26624 * do with it. 26625 * IPQoS Notes: 26626 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 26627 * IPsec. 26628 * XXX would like to nuke ire_t. 26629 * XXX ill_index better be "real" 26630 */ 26631 void 26632 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 26633 { 26634 ipsec_out_t *io; 26635 ipsec_policy_t *pp; 26636 ipsec_action_t *ap; 26637 ipha_t *ipha; 26638 ip6_t *ip6h; 26639 mblk_t *mp; 26640 ill_t *ill; 26641 zoneid_t zoneid; 26642 ipsec_status_t ipsec_rc; 26643 boolean_t ill_need_rele = B_FALSE; 26644 ip_stack_t *ipst; 26645 ipsec_stack_t *ipss; 26646 26647 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26648 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26649 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26650 ipst = io->ipsec_out_ns->netstack_ip; 26651 mp = ipsec_mp->b_cont; 26652 26653 /* 26654 * Initiate IPPF processing. We do it here to account for packets 26655 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 26656 * We can check for ipsec_out_proc_begin even for such packets, as 26657 * they will always be false (asserted below). 26658 */ 26659 if (IPP_ENABLED(IPP_LOCAL_OUT, ipst) && !io->ipsec_out_proc_begin) { 26660 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 26661 io->ipsec_out_ill_index : ill_index); 26662 if (mp == NULL) { 26663 ip2dbg(("ipsec_out_process: packet dropped "\ 26664 "during IPPF processing\n")); 26665 freeb(ipsec_mp); 26666 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26667 return; 26668 } 26669 } 26670 26671 if (!io->ipsec_out_secure) { 26672 /* 26673 * We came here by mistake. 26674 * Don't bother with ipsec processing 26675 * Should "discourage" this path in the future. 26676 */ 26677 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26678 goto done; 26679 } 26680 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26681 ASSERT((io->ipsec_out_policy != NULL) || 26682 (io->ipsec_out_act != NULL)); 26683 ASSERT(io->ipsec_out_failed == B_FALSE); 26684 26685 ipss = ipst->ips_netstack->netstack_ipsec; 26686 if (!ipsec_loaded(ipss)) { 26687 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 26688 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26689 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 26690 } else { 26691 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); 26692 } 26693 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 26694 DROPPER(ipss, ipds_ip_ipsec_not_loaded), 26695 &ipss->ipsec_dropper); 26696 return; 26697 } 26698 26699 /* 26700 * IPsec processing has started. 26701 */ 26702 io->ipsec_out_proc_begin = B_TRUE; 26703 ap = io->ipsec_out_act; 26704 if (ap == NULL) { 26705 pp = io->ipsec_out_policy; 26706 ASSERT(pp != NULL); 26707 ap = pp->ipsp_act; 26708 ASSERT(ap != NULL); 26709 } 26710 26711 /* 26712 * Save the outbound ill index. When the packet comes back 26713 * from IPsec, we make sure the ill hasn't changed or disappeared 26714 * before sending it the accelerated packet. 26715 */ 26716 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 26717 ill = ire_to_ill(ire); 26718 io->ipsec_out_capab_ill_index = ill->ill_phyint->phyint_ifindex; 26719 } 26720 26721 /* 26722 * The order of processing is first insert a IP header if needed. 26723 * Then insert the ESP header and then the AH header. 26724 */ 26725 if ((io->ipsec_out_se_done == B_FALSE) && 26726 (ap->ipa_want_se)) { 26727 /* 26728 * First get the outer IP header before sending 26729 * it to ESP. 26730 */ 26731 ipha_t *oipha, *iipha; 26732 mblk_t *outer_mp, *inner_mp; 26733 26734 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 26735 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 26736 "ipsec_out_process: " 26737 "Self-Encapsulation failed: Out of memory\n"); 26738 freemsg(ipsec_mp); 26739 if (ill != NULL) { 26740 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26741 } else { 26742 BUMP_MIB(&ipst->ips_ip_mib, 26743 ipIfStatsOutDiscards); 26744 } 26745 return; 26746 } 26747 inner_mp = ipsec_mp->b_cont; 26748 ASSERT(inner_mp->b_datap->db_type == M_DATA); 26749 oipha = (ipha_t *)outer_mp->b_rptr; 26750 iipha = (ipha_t *)inner_mp->b_rptr; 26751 *oipha = *iipha; 26752 outer_mp->b_wptr += sizeof (ipha_t); 26753 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 26754 sizeof (ipha_t)); 26755 oipha->ipha_protocol = IPPROTO_ENCAP; 26756 oipha->ipha_version_and_hdr_length = 26757 IP_SIMPLE_HDR_VERSION; 26758 oipha->ipha_hdr_checksum = 0; 26759 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 26760 outer_mp->b_cont = inner_mp; 26761 ipsec_mp->b_cont = outer_mp; 26762 26763 io->ipsec_out_se_done = B_TRUE; 26764 io->ipsec_out_tunnel = B_TRUE; 26765 } 26766 26767 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 26768 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 26769 !ipsec_out_select_sa(ipsec_mp)) 26770 return; 26771 26772 /* 26773 * By now, we know what SA's to use. Toss over to ESP & AH 26774 * to do the heavy lifting. 26775 */ 26776 zoneid = io->ipsec_out_zoneid; 26777 ASSERT(zoneid != ALL_ZONES); 26778 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 26779 ASSERT(io->ipsec_out_esp_sa != NULL); 26780 io->ipsec_out_esp_done = B_TRUE; 26781 /* 26782 * Note that since hw accel can only apply one transform, 26783 * not two, we skip hw accel for ESP if we also have AH 26784 * This is an design limitation of the interface 26785 * which should be revisited. 26786 */ 26787 ASSERT(ire != NULL); 26788 if (io->ipsec_out_ah_sa == NULL) { 26789 ill = (ill_t *)ire->ire_stq->q_ptr; 26790 ipsec_out_is_accelerated(ipsec_mp, 26791 io->ipsec_out_esp_sa, ill, ire); 26792 } 26793 26794 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 26795 switch (ipsec_rc) { 26796 case IPSEC_STATUS_SUCCESS: 26797 break; 26798 case IPSEC_STATUS_FAILED: 26799 if (ill != NULL) { 26800 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26801 } else { 26802 BUMP_MIB(&ipst->ips_ip_mib, 26803 ipIfStatsOutDiscards); 26804 } 26805 /* FALLTHRU */ 26806 case IPSEC_STATUS_PENDING: 26807 return; 26808 } 26809 } 26810 26811 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 26812 ASSERT(io->ipsec_out_ah_sa != NULL); 26813 io->ipsec_out_ah_done = B_TRUE; 26814 if (ire == NULL) { 26815 int idx = io->ipsec_out_capab_ill_index; 26816 ill = ill_lookup_on_ifindex(idx, B_FALSE, 26817 NULL, NULL, NULL, NULL, ipst); 26818 ill_need_rele = B_TRUE; 26819 } else { 26820 ill = (ill_t *)ire->ire_stq->q_ptr; 26821 } 26822 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 26823 ire); 26824 26825 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 26826 switch (ipsec_rc) { 26827 case IPSEC_STATUS_SUCCESS: 26828 break; 26829 case IPSEC_STATUS_FAILED: 26830 if (ill != NULL) { 26831 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26832 } else { 26833 BUMP_MIB(&ipst->ips_ip_mib, 26834 ipIfStatsOutDiscards); 26835 } 26836 /* FALLTHRU */ 26837 case IPSEC_STATUS_PENDING: 26838 if (ill != NULL && ill_need_rele) 26839 ill_refrele(ill); 26840 return; 26841 } 26842 } 26843 /* 26844 * We are done with IPsec processing. Send it over the wire. 26845 */ 26846 done: 26847 mp = ipsec_mp->b_cont; 26848 ipha = (ipha_t *)mp->b_rptr; 26849 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26850 ip_wput_ipsec_out(q, ipsec_mp, ipha, ire->ire_ipif->ipif_ill, 26851 ire); 26852 } else { 26853 ip6h = (ip6_t *)ipha; 26854 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ire->ire_ipif->ipif_ill, 26855 ire); 26856 } 26857 if (ill != NULL && ill_need_rele) 26858 ill_refrele(ill); 26859 } 26860 26861 /* ARGSUSED */ 26862 void 26863 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 26864 { 26865 opt_restart_t *or; 26866 int err; 26867 conn_t *connp; 26868 cred_t *cr; 26869 26870 ASSERT(CONN_Q(q)); 26871 connp = Q_TO_CONN(q); 26872 26873 ASSERT(first_mp->b_datap->db_type == M_CTL); 26874 or = (opt_restart_t *)first_mp->b_rptr; 26875 /* 26876 * We checked for a db_credp the first time svr4_optcom_req 26877 * was called (from ip_wput_nondata). So we can just ASSERT here. 26878 */ 26879 cr = msg_getcred(first_mp, NULL); 26880 ASSERT(cr != NULL); 26881 26882 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 26883 err = svr4_optcom_req(q, first_mp, cr, 26884 &ip_opt_obj, B_FALSE); 26885 } else { 26886 ASSERT(or->or_type == T_OPTMGMT_REQ); 26887 err = tpi_optcom_req(q, first_mp, cr, 26888 &ip_opt_obj, B_FALSE); 26889 } 26890 if (err != EINPROGRESS) { 26891 /* operation is done */ 26892 CONN_OPER_PENDING_DONE(connp); 26893 } 26894 } 26895 26896 /* 26897 * ioctls that go through a down/up sequence may need to wait for the down 26898 * to complete. This involves waiting for the ire and ipif refcnts to go down 26899 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 26900 */ 26901 /* ARGSUSED */ 26902 void 26903 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 26904 { 26905 struct iocblk *iocp; 26906 mblk_t *mp1; 26907 ip_ioctl_cmd_t *ipip; 26908 int err; 26909 sin_t *sin; 26910 struct lifreq *lifr; 26911 struct ifreq *ifr; 26912 26913 iocp = (struct iocblk *)mp->b_rptr; 26914 ASSERT(ipsq != NULL); 26915 /* Existence of mp1 verified in ip_wput_nondata */ 26916 mp1 = mp->b_cont->b_cont; 26917 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26918 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 26919 /* 26920 * Special case where ipx_current_ipif is not set: 26921 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 26922 * We are here as were not able to complete the operation in 26923 * ipif_set_values because we could not become exclusive on 26924 * the new ipsq. 26925 */ 26926 ill_t *ill = q->q_ptr; 26927 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd); 26928 } 26929 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL); 26930 26931 if (ipip->ipi_cmd_type == IF_CMD) { 26932 /* This a old style SIOC[GS]IF* command */ 26933 ifr = (struct ifreq *)mp1->b_rptr; 26934 sin = (sin_t *)&ifr->ifr_addr; 26935 } else if (ipip->ipi_cmd_type == LIF_CMD) { 26936 /* This a new style SIOC[GS]LIF* command */ 26937 lifr = (struct lifreq *)mp1->b_rptr; 26938 sin = (sin_t *)&lifr->lifr_addr; 26939 } else { 26940 sin = NULL; 26941 } 26942 26943 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin, 26944 q, mp, ipip, mp1->b_rptr); 26945 26946 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 26947 } 26948 26949 /* 26950 * ioctl processing 26951 * 26952 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up 26953 * the ioctl command in the ioctl tables, determines the copyin data size 26954 * from the ipi_copyin_size field, and does an mi_copyin() of that size. 26955 * 26956 * ioctl processing then continues when the M_IOCDATA makes its way down to 26957 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its 26958 * associated 'conn' is refheld till the end of the ioctl and the general 26959 * ioctl processing function ip_process_ioctl() is called to extract the 26960 * arguments and process the ioctl. To simplify extraction, ioctl commands 26961 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a 26962 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq()) 26963 * is used to extract the ioctl's arguments. 26964 * 26965 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 26966 * so goes thru the serialization primitive ipsq_try_enter. Then the 26967 * appropriate function to handle the ioctl is called based on the entry in 26968 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 26969 * which also refreleases the 'conn' that was refheld at the start of the 26970 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 26971 * 26972 * Many exclusive ioctls go thru an internal down up sequence as part of 26973 * the operation. For example an attempt to change the IP address of an 26974 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 26975 * does all the cleanup such as deleting all ires that use this address. 26976 * Then we need to wait till all references to the interface go away. 26977 */ 26978 void 26979 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 26980 { 26981 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 26982 ip_ioctl_cmd_t *ipip = arg; 26983 ip_extract_func_t *extract_funcp; 26984 cmd_info_t ci; 26985 int err; 26986 boolean_t entered_ipsq = B_FALSE; 26987 26988 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 26989 26990 if (ipip == NULL) 26991 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26992 26993 /* 26994 * SIOCLIFADDIF needs to go thru a special path since the 26995 * ill may not exist yet. This happens in the case of lo0 26996 * which is created using this ioctl. 26997 */ 26998 if (ipip->ipi_cmd == SIOCLIFADDIF) { 26999 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 27000 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27001 return; 27002 } 27003 27004 ci.ci_ipif = NULL; 27005 if (ipip->ipi_cmd_type == MISC_CMD) { 27006 /* 27007 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF. 27008 */ 27009 if (ipip->ipi_cmd == IF_UNITSEL) { 27010 /* ioctl comes down the ill */ 27011 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 27012 ipif_refhold(ci.ci_ipif); 27013 } 27014 err = 0; 27015 ci.ci_sin = NULL; 27016 ci.ci_sin6 = NULL; 27017 ci.ci_lifr = NULL; 27018 } else { 27019 switch (ipip->ipi_cmd_type) { 27020 case IF_CMD: 27021 case LIF_CMD: 27022 extract_funcp = ip_extract_lifreq; 27023 break; 27024 27025 case ARP_CMD: 27026 case XARP_CMD: 27027 extract_funcp = ip_extract_arpreq; 27028 break; 27029 27030 case TUN_CMD: 27031 extract_funcp = ip_extract_tunreq; 27032 break; 27033 27034 case MSFILT_CMD: 27035 extract_funcp = ip_extract_msfilter; 27036 break; 27037 27038 default: 27039 ASSERT(0); 27040 } 27041 27042 err = (*extract_funcp)(q, mp, ipip, &ci, ip_process_ioctl); 27043 if (err != 0) { 27044 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27045 return; 27046 } 27047 27048 /* 27049 * All of the extraction functions return a refheld ipif. 27050 */ 27051 ASSERT(ci.ci_ipif != NULL); 27052 } 27053 27054 if (!(ipip->ipi_flags & IPI_WR)) { 27055 /* 27056 * A return value of EINPROGRESS means the ioctl is 27057 * either queued and waiting for some reason or has 27058 * already completed. 27059 */ 27060 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 27061 ci.ci_lifr); 27062 if (ci.ci_ipif != NULL) 27063 ipif_refrele(ci.ci_ipif); 27064 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 27065 return; 27066 } 27067 27068 ASSERT(ci.ci_ipif != NULL); 27069 27070 /* 27071 * If ipsq is non-NULL, we are already being called exclusively. 27072 */ 27073 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 27074 if (ipsq == NULL) { 27075 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl, 27076 NEW_OP, B_TRUE); 27077 if (ipsq == NULL) { 27078 ipif_refrele(ci.ci_ipif); 27079 return; 27080 } 27081 entered_ipsq = B_TRUE; 27082 } 27083 27084 /* 27085 * Release the ipif so that ipif_down and friends that wait for 27086 * references to go away are not misled about the current ipif_refcnt 27087 * values. We are writer so we can access the ipif even after releasing 27088 * the ipif. 27089 */ 27090 ipif_refrele(ci.ci_ipif); 27091 27092 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd); 27093 27094 /* 27095 * A return value of EINPROGRESS means the ioctl is 27096 * either queued and waiting for some reason or has 27097 * already completed. 27098 */ 27099 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr); 27100 27101 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 27102 27103 if (entered_ipsq) 27104 ipsq_exit(ipsq); 27105 } 27106 27107 /* 27108 * Complete the ioctl. Typically ioctls use the mi package and need to 27109 * do mi_copyout/mi_copy_done. 27110 */ 27111 void 27112 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq) 27113 { 27114 conn_t *connp = NULL; 27115 27116 if (err == EINPROGRESS) 27117 return; 27118 27119 if (CONN_Q(q)) { 27120 connp = Q_TO_CONN(q); 27121 ASSERT(connp->conn_ref >= 2); 27122 } 27123 27124 switch (mode) { 27125 case COPYOUT: 27126 if (err == 0) 27127 mi_copyout(q, mp); 27128 else 27129 mi_copy_done(q, mp, err); 27130 break; 27131 27132 case NO_COPYOUT: 27133 mi_copy_done(q, mp, err); 27134 break; 27135 27136 default: 27137 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */ 27138 break; 27139 } 27140 27141 /* 27142 * The refhold placed at the start of the ioctl is released here. 27143 */ 27144 if (connp != NULL) 27145 CONN_OPER_PENDING_DONE(connp); 27146 27147 if (ipsq != NULL) 27148 ipsq_current_finish(ipsq); 27149 } 27150 27151 /* Called from ip_wput for all non data messages */ 27152 /* ARGSUSED */ 27153 void 27154 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 27155 { 27156 mblk_t *mp1; 27157 ire_t *ire, *fake_ire; 27158 ill_t *ill; 27159 struct iocblk *iocp; 27160 ip_ioctl_cmd_t *ipip; 27161 cred_t *cr; 27162 conn_t *connp; 27163 int err; 27164 nce_t *nce; 27165 ipif_t *ipif; 27166 ip_stack_t *ipst; 27167 char *proto_str; 27168 27169 if (CONN_Q(q)) { 27170 connp = Q_TO_CONN(q); 27171 ipst = connp->conn_netstack->netstack_ip; 27172 } else { 27173 connp = NULL; 27174 ipst = ILLQ_TO_IPST(q); 27175 } 27176 27177 switch (DB_TYPE(mp)) { 27178 case M_IOCTL: 27179 /* 27180 * IOCTL processing begins in ip_sioctl_copyin_setup which 27181 * will arrange to copy in associated control structures. 27182 */ 27183 ip_sioctl_copyin_setup(q, mp); 27184 return; 27185 case M_IOCDATA: 27186 /* 27187 * Ensure that this is associated with one of our trans- 27188 * parent ioctls. If it's not ours, discard it if we're 27189 * running as a driver, or pass it on if we're a module. 27190 */ 27191 iocp = (struct iocblk *)mp->b_rptr; 27192 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27193 if (ipip == NULL) { 27194 if (q->q_next == NULL) { 27195 goto nak; 27196 } else { 27197 putnext(q, mp); 27198 } 27199 return; 27200 } 27201 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) { 27202 /* 27203 * the ioctl is one we recognise, but is not 27204 * consumed by IP as a module, pass M_IOCDATA 27205 * for processing downstream, but only for 27206 * common Streams ioctls. 27207 */ 27208 if (ipip->ipi_flags & IPI_PASS_DOWN) { 27209 putnext(q, mp); 27210 return; 27211 } else { 27212 goto nak; 27213 } 27214 } 27215 27216 /* IOCTL continuation following copyin or copyout. */ 27217 if (mi_copy_state(q, mp, NULL) == -1) { 27218 /* 27219 * The copy operation failed. mi_copy_state already 27220 * cleaned up, so we're out of here. 27221 */ 27222 return; 27223 } 27224 /* 27225 * If we just completed a copy in, we become writer and 27226 * continue processing in ip_sioctl_copyin_done. If it 27227 * was a copy out, we call mi_copyout again. If there is 27228 * nothing more to copy out, it will complete the IOCTL. 27229 */ 27230 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 27231 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 27232 mi_copy_done(q, mp, EPROTO); 27233 return; 27234 } 27235 /* 27236 * Check for cases that need more copying. A return 27237 * value of 0 means a second copyin has been started, 27238 * so we return; a return value of 1 means no more 27239 * copying is needed, so we continue. 27240 */ 27241 if (ipip->ipi_cmd_type == MSFILT_CMD && 27242 MI_COPY_COUNT(mp) == 1) { 27243 if (ip_copyin_msfilter(q, mp) == 0) 27244 return; 27245 } 27246 /* 27247 * Refhold the conn, till the ioctl completes. This is 27248 * needed in case the ioctl ends up in the pending mp 27249 * list. Every mp in the ill_pending_mp list and 27250 * the ipx_pending_mp must have a refhold on the conn 27251 * to resume processing. The refhold is released when 27252 * the ioctl completes. (normally or abnormally) 27253 * In all cases ip_ioctl_finish is called to finish 27254 * the ioctl. 27255 */ 27256 if (connp != NULL) { 27257 /* This is not a reentry */ 27258 ASSERT(ipsq == NULL); 27259 CONN_INC_REF(connp); 27260 } else { 27261 if (!(ipip->ipi_flags & IPI_MODOK)) { 27262 mi_copy_done(q, mp, EINVAL); 27263 return; 27264 } 27265 } 27266 27267 ip_process_ioctl(ipsq, q, mp, ipip); 27268 27269 } else { 27270 mi_copyout(q, mp); 27271 } 27272 return; 27273 nak: 27274 iocp->ioc_error = EINVAL; 27275 mp->b_datap->db_type = M_IOCNAK; 27276 iocp->ioc_count = 0; 27277 qreply(q, mp); 27278 return; 27279 27280 case M_IOCNAK: 27281 /* 27282 * The only way we could get here is if a resolver didn't like 27283 * an IOCTL we sent it. This shouldn't happen. 27284 */ 27285 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 27286 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 27287 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 27288 freemsg(mp); 27289 return; 27290 case M_IOCACK: 27291 /* /dev/ip shouldn't see this */ 27292 if (CONN_Q(q)) 27293 goto nak; 27294 27295 /* 27296 * Finish socket ioctls passed through to ARP. We use the 27297 * ioc_cmd values we set in ip_sioctl_arp() to decide whether 27298 * we need to become writer before calling ip_sioctl_iocack(). 27299 * Note that qwriter_ip() will release the refhold, and that a 27300 * refhold is OK without ILL_CAN_LOOKUP() since we're on the 27301 * ill stream. 27302 */ 27303 iocp = (struct iocblk *)mp->b_rptr; 27304 if (iocp->ioc_cmd == AR_ENTRY_SQUERY) { 27305 ip_sioctl_iocack(NULL, q, mp, NULL); 27306 return; 27307 } 27308 27309 ASSERT(iocp->ioc_cmd == AR_ENTRY_DELETE || 27310 iocp->ioc_cmd == AR_ENTRY_ADD); 27311 ill = q->q_ptr; 27312 ill_refhold(ill); 27313 qwriter_ip(ill, q, mp, ip_sioctl_iocack, CUR_OP, B_FALSE); 27314 return; 27315 case M_FLUSH: 27316 if (*mp->b_rptr & FLUSHW) 27317 flushq(q, FLUSHALL); 27318 if (q->q_next) { 27319 putnext(q, mp); 27320 return; 27321 } 27322 if (*mp->b_rptr & FLUSHR) { 27323 *mp->b_rptr &= ~FLUSHW; 27324 qreply(q, mp); 27325 return; 27326 } 27327 freemsg(mp); 27328 return; 27329 case IRE_DB_REQ_TYPE: 27330 if (connp == NULL) { 27331 proto_str = "IRE_DB_REQ_TYPE"; 27332 goto protonak; 27333 } 27334 /* An Upper Level Protocol wants a copy of an IRE. */ 27335 ip_ire_req(q, mp); 27336 return; 27337 case M_CTL: 27338 if (mp->b_wptr - mp->b_rptr < sizeof (uint32_t)) 27339 break; 27340 27341 if (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == 27342 TUN_HELLO) { 27343 ASSERT(connp != NULL); 27344 connp->conn_flags |= IPCL_IPTUN; 27345 freeb(mp); 27346 return; 27347 } 27348 27349 /* M_CTL messages are used by ARP to tell us things. */ 27350 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 27351 break; 27352 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 27353 case AR_ENTRY_SQUERY: 27354 putnext(q, mp); 27355 return; 27356 case AR_CLIENT_NOTIFY: 27357 ip_arp_news(q, mp); 27358 return; 27359 case AR_DLPIOP_DONE: 27360 ASSERT(q->q_next != NULL); 27361 ill = (ill_t *)q->q_ptr; 27362 /* qwriter_ip releases the refhold */ 27363 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 27364 ill_refhold(ill); 27365 qwriter_ip(ill, q, mp, ip_arp_done, CUR_OP, B_FALSE); 27366 return; 27367 case AR_ARP_CLOSING: 27368 /* 27369 * ARP (above us) is closing. If no ARP bringup is 27370 * currently pending, ack the message so that ARP 27371 * can complete its close. Also mark ill_arp_closing 27372 * so that new ARP bringups will fail. If any 27373 * ARP bringup is currently in progress, we will 27374 * ack this when the current ARP bringup completes. 27375 */ 27376 ASSERT(q->q_next != NULL); 27377 ill = (ill_t *)q->q_ptr; 27378 mutex_enter(&ill->ill_lock); 27379 ill->ill_arp_closing = 1; 27380 if (!ill->ill_arp_bringup_pending) { 27381 mutex_exit(&ill->ill_lock); 27382 qreply(q, mp); 27383 } else { 27384 mutex_exit(&ill->ill_lock); 27385 freemsg(mp); 27386 } 27387 return; 27388 case AR_ARP_EXTEND: 27389 /* 27390 * The ARP module above us is capable of duplicate 27391 * address detection. Old ATM drivers will not send 27392 * this message. 27393 */ 27394 ASSERT(q->q_next != NULL); 27395 ill = (ill_t *)q->q_ptr; 27396 ill->ill_arp_extend = B_TRUE; 27397 freemsg(mp); 27398 return; 27399 default: 27400 break; 27401 } 27402 break; 27403 case M_PROTO: 27404 case M_PCPROTO: 27405 /* 27406 * The only PROTO messages we expect are copies of option 27407 * negotiation acknowledgements, AH and ESP bind requests 27408 * are also expected. 27409 */ 27410 switch (((union T_primitives *)mp->b_rptr)->type) { 27411 case O_T_BIND_REQ: 27412 case T_BIND_REQ: { 27413 /* Request can get queued in bind */ 27414 if (connp == NULL) { 27415 proto_str = "O_T_BIND_REQ/T_BIND_REQ"; 27416 goto protonak; 27417 } 27418 /* 27419 * The transports except SCTP call ip_bind_{v4,v6}() 27420 * directly instead of a a putnext. SCTP doesn't 27421 * generate any T_BIND_REQ since it has its own 27422 * fanout data structures. However, ESP and AH 27423 * come in for regular binds; all other cases are 27424 * bind retries. 27425 */ 27426 ASSERT(!IPCL_IS_SCTP(connp)); 27427 27428 /* Don't increment refcnt if this is a re-entry */ 27429 if (ipsq == NULL) 27430 CONN_INC_REF(connp); 27431 27432 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 27433 connp, NULL) : ip_bind_v4(q, mp, connp); 27434 ASSERT(mp != NULL); 27435 27436 ASSERT(!IPCL_IS_TCP(connp)); 27437 ASSERT(!IPCL_IS_UDP(connp)); 27438 ASSERT(!IPCL_IS_RAWIP(connp)); 27439 27440 /* The case of AH and ESP */ 27441 qreply(q, mp); 27442 CONN_OPER_PENDING_DONE(connp); 27443 return; 27444 } 27445 case T_SVR4_OPTMGMT_REQ: 27446 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 27447 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 27448 27449 if (connp == NULL) { 27450 proto_str = "T_SVR4_OPTMGMT_REQ"; 27451 goto protonak; 27452 } 27453 27454 /* 27455 * All Solaris components should pass a db_credp 27456 * for this TPI message, hence we ASSERT. 27457 * But in case there is some other M_PROTO that looks 27458 * like a TPI message sent by some other kernel 27459 * component, we check and return an error. 27460 */ 27461 cr = msg_getcred(mp, NULL); 27462 ASSERT(cr != NULL); 27463 if (cr == NULL) { 27464 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 27465 if (mp != NULL) 27466 qreply(q, mp); 27467 return; 27468 } 27469 27470 if (!snmpcom_req(q, mp, ip_snmp_set, 27471 ip_snmp_get, cr)) { 27472 /* 27473 * Call svr4_optcom_req so that it can 27474 * generate the ack. We don't come here 27475 * if this operation is being restarted. 27476 * ip_restart_optmgmt will drop the conn ref. 27477 * In the case of ipsec option after the ipsec 27478 * load is complete conn_restart_ipsec_waiter 27479 * drops the conn ref. 27480 */ 27481 ASSERT(ipsq == NULL); 27482 CONN_INC_REF(connp); 27483 if (ip_check_for_ipsec_opt(q, mp)) 27484 return; 27485 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj, 27486 B_FALSE); 27487 if (err != EINPROGRESS) { 27488 /* Operation is done */ 27489 CONN_OPER_PENDING_DONE(connp); 27490 } 27491 } 27492 return; 27493 case T_OPTMGMT_REQ: 27494 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 27495 /* 27496 * Note: No snmpcom_req support through new 27497 * T_OPTMGMT_REQ. 27498 * Call tpi_optcom_req so that it can 27499 * generate the ack. 27500 */ 27501 if (connp == NULL) { 27502 proto_str = "T_OPTMGMT_REQ"; 27503 goto protonak; 27504 } 27505 27506 /* 27507 * All Solaris components should pass a db_credp 27508 * for this TPI message, hence we ASSERT. 27509 * But in case there is some other M_PROTO that looks 27510 * like a TPI message sent by some other kernel 27511 * component, we check and return an error. 27512 */ 27513 cr = msg_getcred(mp, NULL); 27514 ASSERT(cr != NULL); 27515 if (cr == NULL) { 27516 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 27517 if (mp != NULL) 27518 qreply(q, mp); 27519 return; 27520 } 27521 ASSERT(ipsq == NULL); 27522 /* 27523 * We don't come here for restart. ip_restart_optmgmt 27524 * will drop the conn ref. In the case of ipsec option 27525 * after the ipsec load is complete 27526 * conn_restart_ipsec_waiter drops the conn ref. 27527 */ 27528 CONN_INC_REF(connp); 27529 if (ip_check_for_ipsec_opt(q, mp)) 27530 return; 27531 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj, B_FALSE); 27532 if (err != EINPROGRESS) { 27533 /* Operation is done */ 27534 CONN_OPER_PENDING_DONE(connp); 27535 } 27536 return; 27537 case T_UNBIND_REQ: 27538 if (connp == NULL) { 27539 proto_str = "T_UNBIND_REQ"; 27540 goto protonak; 27541 } 27542 ip_unbind(Q_TO_CONN(q)); 27543 mp = mi_tpi_ok_ack_alloc(mp); 27544 qreply(q, mp); 27545 return; 27546 default: 27547 /* 27548 * Have to drop any DLPI messages coming down from 27549 * arp (such as an info_req which would cause ip 27550 * to receive an extra info_ack if it was passed 27551 * through. 27552 */ 27553 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 27554 (int)*(uint_t *)mp->b_rptr)); 27555 freemsg(mp); 27556 return; 27557 } 27558 /* NOTREACHED */ 27559 case IRE_DB_TYPE: { 27560 nce_t *nce; 27561 ill_t *ill; 27562 in6_addr_t gw_addr_v6; 27563 27564 /* 27565 * This is a response back from a resolver. It 27566 * consists of a message chain containing: 27567 * IRE_MBLK-->LL_HDR_MBLK->pkt 27568 * The IRE_MBLK is the one we allocated in ip_newroute. 27569 * The LL_HDR_MBLK is the DLPI header to use to get 27570 * the attached packet, and subsequent ones for the 27571 * same destination, transmitted. 27572 */ 27573 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 27574 break; 27575 /* 27576 * First, check to make sure the resolution succeeded. 27577 * If it failed, the second mblk will be empty. 27578 * If it is, free the chain, dropping the packet. 27579 * (We must ire_delete the ire; that frees the ire mblk) 27580 * We're doing this now to support PVCs for ATM; it's 27581 * a partial xresolv implementation. When we fully implement 27582 * xresolv interfaces, instead of freeing everything here 27583 * we'll initiate neighbor discovery. 27584 * 27585 * For v4 (ARP and other external resolvers) the resolver 27586 * frees the message, so no check is needed. This check 27587 * is required, though, for a full xresolve implementation. 27588 * Including this code here now both shows how external 27589 * resolvers can NACK a resolution request using an 27590 * existing design that has no specific provisions for NACKs, 27591 * and also takes into account that the current non-ARP 27592 * external resolver has been coded to use this method of 27593 * NACKing for all IPv6 (xresolv) cases, 27594 * whether our xresolv implementation is complete or not. 27595 * 27596 */ 27597 ire = (ire_t *)mp->b_rptr; 27598 ill = ire_to_ill(ire); 27599 mp1 = mp->b_cont; /* dl_unitdata_req */ 27600 if (mp1->b_rptr == mp1->b_wptr) { 27601 if (ire->ire_ipversion == IPV6_VERSION) { 27602 /* 27603 * XRESOLV interface. 27604 */ 27605 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27606 mutex_enter(&ire->ire_lock); 27607 gw_addr_v6 = ire->ire_gateway_addr_v6; 27608 mutex_exit(&ire->ire_lock); 27609 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27610 nce = ndp_lookup_v6(ill, B_FALSE, 27611 &ire->ire_addr_v6, B_FALSE); 27612 } else { 27613 nce = ndp_lookup_v6(ill, B_FALSE, 27614 &gw_addr_v6, B_FALSE); 27615 } 27616 if (nce != NULL) { 27617 nce_resolv_failed(nce); 27618 ndp_delete(nce); 27619 NCE_REFRELE(nce); 27620 } 27621 } 27622 mp->b_cont = NULL; 27623 freemsg(mp1); /* frees the pkt as well */ 27624 ASSERT(ire->ire_nce == NULL); 27625 ire_delete((ire_t *)mp->b_rptr); 27626 return; 27627 } 27628 27629 /* 27630 * Split them into IRE_MBLK and pkt and feed it into 27631 * ire_add_then_send. Then in ire_add_then_send 27632 * the IRE will be added, and then the packet will be 27633 * run back through ip_wput. This time it will make 27634 * it to the wire. 27635 */ 27636 mp->b_cont = NULL; 27637 mp = mp1->b_cont; /* now, mp points to pkt */ 27638 mp1->b_cont = NULL; 27639 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 27640 if (ire->ire_ipversion == IPV6_VERSION) { 27641 /* 27642 * XRESOLV interface. Find the nce and put a copy 27643 * of the dl_unitdata_req in nce_res_mp 27644 */ 27645 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27646 mutex_enter(&ire->ire_lock); 27647 gw_addr_v6 = ire->ire_gateway_addr_v6; 27648 mutex_exit(&ire->ire_lock); 27649 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27650 nce = ndp_lookup_v6(ill, B_FALSE, 27651 &ire->ire_addr_v6, B_FALSE); 27652 } else { 27653 nce = ndp_lookup_v6(ill, B_FALSE, 27654 &gw_addr_v6, B_FALSE); 27655 } 27656 if (nce != NULL) { 27657 /* 27658 * We have to protect nce_res_mp here 27659 * from being accessed by other threads 27660 * while we change the mblk pointer. 27661 * Other functions will also lock the nce when 27662 * accessing nce_res_mp. 27663 * 27664 * The reason we change the mblk pointer 27665 * here rather than copying the resolved address 27666 * into the template is that, unlike with 27667 * ethernet, we have no guarantee that the 27668 * resolved address length will be 27669 * smaller than or equal to the lla length 27670 * with which the template was allocated, 27671 * (for ethernet, they're equal) 27672 * so we have to use the actual resolved 27673 * address mblk - which holds the real 27674 * dl_unitdata_req with the resolved address. 27675 * 27676 * Doing this is the same behavior as was 27677 * previously used in the v4 ARP case. 27678 */ 27679 mutex_enter(&nce->nce_lock); 27680 if (nce->nce_res_mp != NULL) 27681 freemsg(nce->nce_res_mp); 27682 nce->nce_res_mp = mp1; 27683 mutex_exit(&nce->nce_lock); 27684 /* 27685 * We do a fastpath probe here because 27686 * we have resolved the address without 27687 * using Neighbor Discovery. 27688 * In the non-XRESOLV v6 case, the fastpath 27689 * probe is done right after neighbor 27690 * discovery completes. 27691 */ 27692 if (nce->nce_res_mp != NULL) { 27693 int res; 27694 nce_fastpath_list_add(nce); 27695 res = ill_fastpath_probe(ill, 27696 nce->nce_res_mp); 27697 if (res != 0 && res != EAGAIN) 27698 nce_fastpath_list_delete(nce); 27699 } 27700 27701 ire_add_then_send(q, ire, mp); 27702 /* 27703 * Now we have to clean out any packets 27704 * that may have been queued on the nce 27705 * while it was waiting for address resolution 27706 * to complete. 27707 */ 27708 mutex_enter(&nce->nce_lock); 27709 mp1 = nce->nce_qd_mp; 27710 nce->nce_qd_mp = NULL; 27711 mutex_exit(&nce->nce_lock); 27712 while (mp1 != NULL) { 27713 mblk_t *nxt_mp; 27714 queue_t *fwdq = NULL; 27715 ill_t *inbound_ill; 27716 uint_t ifindex; 27717 27718 nxt_mp = mp1->b_next; 27719 mp1->b_next = NULL; 27720 /* 27721 * Retrieve ifindex stored in 27722 * ip_rput_data_v6() 27723 */ 27724 ifindex = 27725 (uint_t)(uintptr_t)mp1->b_prev; 27726 inbound_ill = 27727 ill_lookup_on_ifindex(ifindex, 27728 B_TRUE, NULL, NULL, NULL, 27729 NULL, ipst); 27730 mp1->b_prev = NULL; 27731 if (inbound_ill != NULL) 27732 fwdq = inbound_ill->ill_rq; 27733 27734 if (fwdq != NULL) { 27735 put(fwdq, mp1); 27736 ill_refrele(inbound_ill); 27737 } else 27738 put(WR(ill->ill_rq), mp1); 27739 mp1 = nxt_mp; 27740 } 27741 NCE_REFRELE(nce); 27742 } else { /* nce is NULL; clean up */ 27743 ire_delete(ire); 27744 freemsg(mp); 27745 freemsg(mp1); 27746 return; 27747 } 27748 } else { 27749 nce_t *arpce; 27750 /* 27751 * Link layer resolution succeeded. Recompute the 27752 * ire_nce. 27753 */ 27754 ASSERT(ire->ire_type & (IRE_CACHE|IRE_BROADCAST)); 27755 if ((arpce = ndp_lookup_v4(ill, 27756 (ire->ire_gateway_addr != INADDR_ANY ? 27757 &ire->ire_gateway_addr : &ire->ire_addr), 27758 B_FALSE)) == NULL) { 27759 freeb(ire->ire_mp); 27760 freeb(mp1); 27761 freemsg(mp); 27762 return; 27763 } 27764 mutex_enter(&arpce->nce_lock); 27765 arpce->nce_last = TICK_TO_MSEC(lbolt64); 27766 if (arpce->nce_state == ND_REACHABLE) { 27767 /* 27768 * Someone resolved this before us; 27769 * cleanup the res_mp. Since ire has 27770 * not been added yet, the call to ire_add_v4 27771 * from ire_add_then_send (when a dup is 27772 * detected) will clean up the ire. 27773 */ 27774 freeb(mp1); 27775 } else { 27776 ASSERT(arpce->nce_res_mp == NULL); 27777 arpce->nce_res_mp = mp1; 27778 arpce->nce_state = ND_REACHABLE; 27779 } 27780 mutex_exit(&arpce->nce_lock); 27781 if (ire->ire_marks & IRE_MARK_NOADD) { 27782 /* 27783 * this ire will not be added to the ire 27784 * cache table, so we can set the ire_nce 27785 * here, as there are no atomicity constraints. 27786 */ 27787 ire->ire_nce = arpce; 27788 /* 27789 * We are associating this nce with the ire 27790 * so change the nce ref taken in 27791 * ndp_lookup_v4() from 27792 * NCE_REFHOLD to NCE_REFHOLD_NOTR 27793 */ 27794 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 27795 } else { 27796 NCE_REFRELE(arpce); 27797 } 27798 ire_add_then_send(q, ire, mp); 27799 } 27800 return; /* All is well, the packet has been sent. */ 27801 } 27802 case IRE_ARPRESOLVE_TYPE: { 27803 27804 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* fake_ire */ 27805 break; 27806 mp1 = mp->b_cont; /* dl_unitdata_req */ 27807 mp->b_cont = NULL; 27808 /* 27809 * First, check to make sure the resolution succeeded. 27810 * If it failed, the second mblk will be empty. 27811 */ 27812 if (mp1->b_rptr == mp1->b_wptr) { 27813 /* cleanup the incomplete ire, free queued packets */ 27814 freemsg(mp); /* fake ire */ 27815 freeb(mp1); /* dl_unitdata response */ 27816 return; 27817 } 27818 27819 /* 27820 * Update any incomplete nce_t found. We search the ctable 27821 * and find the nce from the ire->ire_nce because we need 27822 * to pass the ire to ip_xmit_v4 later, and can find both 27823 * ire and nce in one lookup. 27824 */ 27825 fake_ire = (ire_t *)mp->b_rptr; 27826 27827 /* 27828 * By the time we come back here from ARP the logical outgoing 27829 * interface of the incomplete ire we added in ire_forward() 27830 * could have disappeared, causing the incomplete ire to also 27831 * disappear. So we need to retreive the proper ipif for the 27832 * ire before looking in ctable. In the case of IPMP, the 27833 * ipif may be on the IPMP ill, so look it up based on the 27834 * ire_ipif_ifindex we stashed back in ire_init_common(). 27835 * Then, we can verify that ire_ipif_seqid still exists. 27836 */ 27837 ill = ill_lookup_on_ifindex(fake_ire->ire_ipif_ifindex, B_FALSE, 27838 NULL, NULL, NULL, NULL, ipst); 27839 if (ill == NULL) { 27840 ip1dbg(("ill for incomplete ire vanished\n")); 27841 freemsg(mp); /* fake ire */ 27842 freeb(mp1); /* dl_unitdata response */ 27843 return; 27844 } 27845 27846 /* Get the outgoing ipif */ 27847 mutex_enter(&ill->ill_lock); 27848 ipif = ipif_lookup_seqid(ill, fake_ire->ire_ipif_seqid); 27849 if (ipif == NULL) { 27850 mutex_exit(&ill->ill_lock); 27851 ill_refrele(ill); 27852 ip1dbg(("logical intrf to incomplete ire vanished\n")); 27853 freemsg(mp); /* fake_ire */ 27854 freeb(mp1); /* dl_unitdata response */ 27855 return; 27856 } 27857 27858 ipif_refhold_locked(ipif); 27859 mutex_exit(&ill->ill_lock); 27860 ill_refrele(ill); 27861 ire = ire_arpresolve_lookup(fake_ire->ire_addr, 27862 fake_ire->ire_gateway_addr, ipif, fake_ire->ire_zoneid, 27863 ipst, ((ill_t *)q->q_ptr)->ill_wq); 27864 ipif_refrele(ipif); 27865 if (ire == NULL) { 27866 /* 27867 * no ire was found; check if there is an nce 27868 * for this lookup; if it has no ire's pointing at it 27869 * cleanup. 27870 */ 27871 if ((nce = ndp_lookup_v4(q->q_ptr, 27872 (fake_ire->ire_gateway_addr != INADDR_ANY ? 27873 &fake_ire->ire_gateway_addr : &fake_ire->ire_addr), 27874 B_FALSE)) != NULL) { 27875 /* 27876 * cleanup: 27877 * We check for refcnt 2 (one for the nce 27878 * hash list + 1 for the ref taken by 27879 * ndp_lookup_v4) to check that there are 27880 * no ire's pointing at the nce. 27881 */ 27882 if (nce->nce_refcnt == 2) 27883 ndp_delete(nce); 27884 NCE_REFRELE(nce); 27885 } 27886 freeb(mp1); /* dl_unitdata response */ 27887 freemsg(mp); /* fake ire */ 27888 return; 27889 } 27890 27891 nce = ire->ire_nce; 27892 DTRACE_PROBE2(ire__arpresolve__type, 27893 ire_t *, ire, nce_t *, nce); 27894 mutex_enter(&nce->nce_lock); 27895 nce->nce_last = TICK_TO_MSEC(lbolt64); 27896 if (nce->nce_state == ND_REACHABLE) { 27897 /* 27898 * Someone resolved this before us; 27899 * our response is not needed any more. 27900 */ 27901 mutex_exit(&nce->nce_lock); 27902 freeb(mp1); /* dl_unitdata response */ 27903 } else { 27904 ASSERT(nce->nce_res_mp == NULL); 27905 nce->nce_res_mp = mp1; 27906 nce->nce_state = ND_REACHABLE; 27907 mutex_exit(&nce->nce_lock); 27908 nce_fastpath(nce); 27909 } 27910 /* 27911 * The cached nce_t has been updated to be reachable; 27912 * Clear the IRE_MARK_UNCACHED flag and free the fake_ire. 27913 */ 27914 fake_ire->ire_marks &= ~IRE_MARK_UNCACHED; 27915 freemsg(mp); 27916 /* 27917 * send out queued packets. 27918 */ 27919 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE, NULL); 27920 27921 IRE_REFRELE(ire); 27922 return; 27923 } 27924 default: 27925 break; 27926 } 27927 if (q->q_next) { 27928 putnext(q, mp); 27929 } else 27930 freemsg(mp); 27931 return; 27932 27933 protonak: 27934 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str); 27935 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL) 27936 qreply(q, mp); 27937 } 27938 27939 /* 27940 * Process IP options in an outbound packet. Modify the destination if there 27941 * is a source route option. 27942 * Returns non-zero if something fails in which case an ICMP error has been 27943 * sent and mp freed. 27944 */ 27945 static int 27946 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 27947 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) 27948 { 27949 ipoptp_t opts; 27950 uchar_t *opt; 27951 uint8_t optval; 27952 uint8_t optlen; 27953 ipaddr_t dst; 27954 intptr_t code = 0; 27955 mblk_t *mp; 27956 ire_t *ire = NULL; 27957 27958 ip2dbg(("ip_wput_options\n")); 27959 mp = ipsec_mp; 27960 if (mctl_present) { 27961 mp = ipsec_mp->b_cont; 27962 } 27963 27964 dst = ipha->ipha_dst; 27965 for (optval = ipoptp_first(&opts, ipha); 27966 optval != IPOPT_EOL; 27967 optval = ipoptp_next(&opts)) { 27968 opt = opts.ipoptp_cur; 27969 optlen = opts.ipoptp_len; 27970 ip2dbg(("ip_wput_options: opt %d, len %d\n", 27971 optval, optlen)); 27972 switch (optval) { 27973 uint32_t off; 27974 case IPOPT_SSRR: 27975 case IPOPT_LSRR: 27976 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27977 ip1dbg(( 27978 "ip_wput_options: bad option offset\n")); 27979 code = (char *)&opt[IPOPT_OLEN] - 27980 (char *)ipha; 27981 goto param_prob; 27982 } 27983 off = opt[IPOPT_OFFSET]; 27984 ip1dbg(("ip_wput_options: next hop 0x%x\n", 27985 ntohl(dst))); 27986 /* 27987 * For strict: verify that dst is directly 27988 * reachable. 27989 */ 27990 if (optval == IPOPT_SSRR) { 27991 ire = ire_ftable_lookup(dst, 0, 0, 27992 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 27993 msg_getlabel(mp), 27994 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); 27995 if (ire == NULL) { 27996 ip1dbg(("ip_wput_options: SSRR not" 27997 " directly reachable: 0x%x\n", 27998 ntohl(dst))); 27999 goto bad_src_route; 28000 } 28001 ire_refrele(ire); 28002 } 28003 break; 28004 case IPOPT_RR: 28005 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28006 ip1dbg(( 28007 "ip_wput_options: bad option offset\n")); 28008 code = (char *)&opt[IPOPT_OLEN] - 28009 (char *)ipha; 28010 goto param_prob; 28011 } 28012 break; 28013 case IPOPT_TS: 28014 /* 28015 * Verify that length >=5 and that there is either 28016 * room for another timestamp or that the overflow 28017 * counter is not maxed out. 28018 */ 28019 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 28020 if (optlen < IPOPT_MINLEN_IT) { 28021 goto param_prob; 28022 } 28023 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 28024 ip1dbg(( 28025 "ip_wput_options: bad option offset\n")); 28026 code = (char *)&opt[IPOPT_OFFSET] - 28027 (char *)ipha; 28028 goto param_prob; 28029 } 28030 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 28031 case IPOPT_TS_TSONLY: 28032 off = IPOPT_TS_TIMELEN; 28033 break; 28034 case IPOPT_TS_TSANDADDR: 28035 case IPOPT_TS_PRESPEC: 28036 case IPOPT_TS_PRESPEC_RFC791: 28037 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 28038 break; 28039 default: 28040 code = (char *)&opt[IPOPT_POS_OV_FLG] - 28041 (char *)ipha; 28042 goto param_prob; 28043 } 28044 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 28045 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 28046 /* 28047 * No room and the overflow counter is 15 28048 * already. 28049 */ 28050 goto param_prob; 28051 } 28052 break; 28053 } 28054 } 28055 28056 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 28057 return (0); 28058 28059 ip1dbg(("ip_wput_options: error processing IP options.")); 28060 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 28061 28062 param_prob: 28063 /* 28064 * Since ip_wput() isn't close to finished, we fill 28065 * in enough of the header for credible error reporting. 28066 */ 28067 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28068 /* Failed */ 28069 freemsg(ipsec_mp); 28070 return (-1); 28071 } 28072 icmp_param_problem(q, ipsec_mp, (uint8_t)code, zoneid, ipst); 28073 return (-1); 28074 28075 bad_src_route: 28076 /* 28077 * Since ip_wput() isn't close to finished, we fill 28078 * in enough of the header for credible error reporting. 28079 */ 28080 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { 28081 /* Failed */ 28082 freemsg(ipsec_mp); 28083 return (-1); 28084 } 28085 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); 28086 return (-1); 28087 } 28088 28089 /* 28090 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 28091 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 28092 * thru /etc/system. 28093 */ 28094 #define CONN_MAXDRAINCNT 64 28095 28096 static void 28097 conn_drain_init(ip_stack_t *ipst) 28098 { 28099 int i, j; 28100 idl_tx_list_t *itl_tx; 28101 28102 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads; 28103 28104 if ((ipst->ips_conn_drain_list_cnt == 0) || 28105 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 28106 /* 28107 * Default value of the number of drainers is the 28108 * number of cpus, subject to maximum of 8 drainers. 28109 */ 28110 if (boot_max_ncpus != -1) 28111 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 28112 else 28113 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8); 28114 } 28115 28116 ipst->ips_idl_tx_list = 28117 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP); 28118 for (i = 0; i < TX_FANOUT_SIZE; i++) { 28119 itl_tx = &ipst->ips_idl_tx_list[i]; 28120 itl_tx->txl_drain_list = 28121 kmem_zalloc(ipst->ips_conn_drain_list_cnt * 28122 sizeof (idl_t), KM_SLEEP); 28123 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL); 28124 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) { 28125 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL, 28126 MUTEX_DEFAULT, NULL); 28127 itl_tx->txl_drain_list[j].idl_itl = itl_tx; 28128 } 28129 } 28130 } 28131 28132 static void 28133 conn_drain_fini(ip_stack_t *ipst) 28134 { 28135 int i; 28136 idl_tx_list_t *itl_tx; 28137 28138 for (i = 0; i < TX_FANOUT_SIZE; i++) { 28139 itl_tx = &ipst->ips_idl_tx_list[i]; 28140 kmem_free(itl_tx->txl_drain_list, 28141 ipst->ips_conn_drain_list_cnt * sizeof (idl_t)); 28142 } 28143 kmem_free(ipst->ips_idl_tx_list, 28144 TX_FANOUT_SIZE * sizeof (idl_tx_list_t)); 28145 ipst->ips_idl_tx_list = NULL; 28146 } 28147 28148 /* 28149 * Note: For an overview of how flowcontrol is handled in IP please see the 28150 * IP Flowcontrol notes at the top of this file. 28151 * 28152 * Flow control has blocked us from proceeding. Insert the given conn in one 28153 * of the conn drain lists. These conn wq's will be qenabled later on when 28154 * STREAMS flow control does a backenable. conn_walk_drain will enable 28155 * the first conn in each of these drain lists. Each of these qenabled conns 28156 * in turn enables the next in the list, after it runs, or when it closes, 28157 * thus sustaining the drain process. 28158 */ 28159 void 28160 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list) 28161 { 28162 idl_t *idl = tx_list->txl_drain_list; 28163 uint_t index; 28164 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28165 28166 mutex_enter(&connp->conn_lock); 28167 if (connp->conn_state_flags & CONN_CLOSING) { 28168 /* 28169 * The conn is closing as a result of which CONN_CLOSING 28170 * is set. Return. 28171 */ 28172 mutex_exit(&connp->conn_lock); 28173 return; 28174 } else if (connp->conn_idl == NULL) { 28175 /* 28176 * Assign the next drain list round robin. We dont' use 28177 * a lock, and thus it may not be strictly round robin. 28178 * Atomicity of load/stores is enough to make sure that 28179 * conn_drain_list_index is always within bounds. 28180 */ 28181 index = tx_list->txl_drain_index; 28182 ASSERT(index < ipst->ips_conn_drain_list_cnt); 28183 connp->conn_idl = &tx_list->txl_drain_list[index]; 28184 index++; 28185 if (index == ipst->ips_conn_drain_list_cnt) 28186 index = 0; 28187 tx_list->txl_drain_index = index; 28188 } 28189 mutex_exit(&connp->conn_lock); 28190 28191 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28192 if ((connp->conn_drain_prev != NULL) || 28193 (connp->conn_state_flags & CONN_CLOSING)) { 28194 /* 28195 * The conn is already in the drain list, OR 28196 * the conn is closing. We need to check again for 28197 * the closing case again since close can happen 28198 * after we drop the conn_lock, and before we 28199 * acquire the CONN_DRAIN_LIST_LOCK. 28200 */ 28201 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28202 return; 28203 } else { 28204 idl = connp->conn_idl; 28205 } 28206 28207 /* 28208 * The conn is not in the drain list. Insert it at the 28209 * tail of the drain list. The drain list is circular 28210 * and doubly linked. idl_conn points to the 1st element 28211 * in the list. 28212 */ 28213 if (idl->idl_conn == NULL) { 28214 idl->idl_conn = connp; 28215 connp->conn_drain_next = connp; 28216 connp->conn_drain_prev = connp; 28217 } else { 28218 conn_t *head = idl->idl_conn; 28219 28220 connp->conn_drain_next = head; 28221 connp->conn_drain_prev = head->conn_drain_prev; 28222 head->conn_drain_prev->conn_drain_next = connp; 28223 head->conn_drain_prev = connp; 28224 } 28225 /* 28226 * For non streams based sockets assert flow control. 28227 */ 28228 if (IPCL_IS_NONSTR(connp)) { 28229 DTRACE_PROBE1(su__txq__full, conn_t *, connp); 28230 (*connp->conn_upcalls->su_txq_full) 28231 (connp->conn_upper_handle, B_TRUE); 28232 } else { 28233 conn_setqfull(connp); 28234 noenable(connp->conn_wq); 28235 } 28236 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28237 } 28238 28239 /* 28240 * This conn is closing, and we are called from ip_close. OR 28241 * This conn has been serviced by ip_wsrv, and we need to do the tail 28242 * processing. 28243 * If this conn is part of the drain list, we may need to sustain the drain 28244 * process by qenabling the next conn in the drain list. We may also need to 28245 * remove this conn from the list, if it is done. 28246 */ 28247 static void 28248 conn_drain_tail(conn_t *connp, boolean_t closing) 28249 { 28250 idl_t *idl; 28251 28252 /* 28253 * connp->conn_idl is stable at this point, and no lock is needed 28254 * to check it. If we are called from ip_close, close has already 28255 * set CONN_CLOSING, thus freezing the value of conn_idl, and 28256 * called us only because conn_idl is non-null. If we are called thru 28257 * service, conn_idl could be null, but it cannot change because 28258 * service is single-threaded per queue, and there cannot be another 28259 * instance of service trying to call conn_drain_insert on this conn 28260 * now. 28261 */ 28262 ASSERT(!closing || (connp->conn_idl != NULL)); 28263 28264 /* 28265 * If connp->conn_idl is null, the conn has not been inserted into any 28266 * drain list even once since creation of the conn. Just return. 28267 */ 28268 if (connp->conn_idl == NULL) 28269 return; 28270 28271 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28272 28273 if (connp->conn_drain_prev == NULL) { 28274 /* This conn is currently not in the drain list. */ 28275 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28276 return; 28277 } 28278 idl = connp->conn_idl; 28279 if (idl->idl_conn_draining == connp) { 28280 /* 28281 * This conn is the current drainer. If this is the last conn 28282 * in the drain list, we need to do more checks, in the 'if' 28283 * below. Otherwwise we need to just qenable the next conn, 28284 * to sustain the draining, and is handled in the 'else' 28285 * below. 28286 */ 28287 if (connp->conn_drain_next == idl->idl_conn) { 28288 /* 28289 * This conn is the last in this list. This round 28290 * of draining is complete. If idl_repeat is set, 28291 * it means another flow enabling has happened from 28292 * the driver/streams and we need to another round 28293 * of draining. 28294 * If there are more than 2 conns in the drain list, 28295 * do a left rotate by 1, so that all conns except the 28296 * conn at the head move towards the head by 1, and the 28297 * the conn at the head goes to the tail. This attempts 28298 * a more even share for all queues that are being 28299 * drained. 28300 */ 28301 if ((connp->conn_drain_next != connp) && 28302 (idl->idl_conn->conn_drain_next != connp)) { 28303 idl->idl_conn = idl->idl_conn->conn_drain_next; 28304 } 28305 if (idl->idl_repeat) { 28306 qenable(idl->idl_conn->conn_wq); 28307 idl->idl_conn_draining = idl->idl_conn; 28308 idl->idl_repeat = 0; 28309 } else { 28310 idl->idl_conn_draining = NULL; 28311 } 28312 } else { 28313 /* 28314 * If the next queue that we are now qenable'ing, 28315 * is closing, it will remove itself from this list 28316 * and qenable the subsequent queue in ip_close(). 28317 * Serialization is acheived thru idl_lock. 28318 */ 28319 qenable(connp->conn_drain_next->conn_wq); 28320 idl->idl_conn_draining = connp->conn_drain_next; 28321 } 28322 } 28323 if (!connp->conn_did_putbq || closing) { 28324 /* 28325 * Remove ourself from the drain list, if we did not do 28326 * a putbq, or if the conn is closing. 28327 * Note: It is possible that q->q_first is non-null. It means 28328 * that these messages landed after we did a enableok() in 28329 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 28330 * service them. 28331 */ 28332 if (connp->conn_drain_next == connp) { 28333 /* Singleton in the list */ 28334 ASSERT(connp->conn_drain_prev == connp); 28335 idl->idl_conn = NULL; 28336 idl->idl_conn_draining = NULL; 28337 } else { 28338 connp->conn_drain_prev->conn_drain_next = 28339 connp->conn_drain_next; 28340 connp->conn_drain_next->conn_drain_prev = 28341 connp->conn_drain_prev; 28342 if (idl->idl_conn == connp) 28343 idl->idl_conn = connp->conn_drain_next; 28344 ASSERT(idl->idl_conn_draining != connp); 28345 28346 } 28347 connp->conn_drain_next = NULL; 28348 connp->conn_drain_prev = NULL; 28349 28350 /* 28351 * For non streams based sockets open up flow control. 28352 */ 28353 if (IPCL_IS_NONSTR(connp)) { 28354 (*connp->conn_upcalls->su_txq_full) 28355 (connp->conn_upper_handle, B_FALSE); 28356 } else { 28357 conn_clrqfull(connp); 28358 enableok(connp->conn_wq); 28359 } 28360 } 28361 28362 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28363 } 28364 28365 /* 28366 * Write service routine. Shared perimeter entry point. 28367 * ip_wsrv can be called in any of the following ways. 28368 * 1. The device queue's messages has fallen below the low water mark 28369 * and STREAMS has backenabled the ill_wq. We walk thru all the 28370 * the drain lists and backenable the first conn in each list. 28371 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 28372 * qenabled non-tcp upper layers. We start dequeing messages and call 28373 * ip_wput for each message. 28374 */ 28375 28376 void 28377 ip_wsrv(queue_t *q) 28378 { 28379 conn_t *connp; 28380 ill_t *ill; 28381 mblk_t *mp; 28382 28383 if (q->q_next) { 28384 ill = (ill_t *)q->q_ptr; 28385 if (ill->ill_state_flags == 0) { 28386 ip_stack_t *ipst = ill->ill_ipst; 28387 28388 /* 28389 * The device flow control has opened up. 28390 * Walk through conn drain lists and qenable the 28391 * first conn in each list. This makes sense only 28392 * if the stream is fully plumbed and setup. 28393 * Hence the if check above. 28394 */ 28395 ip1dbg(("ip_wsrv: walking\n")); 28396 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]); 28397 } 28398 return; 28399 } 28400 28401 connp = Q_TO_CONN(q); 28402 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 28403 28404 /* 28405 * 1. Set conn_draining flag to signal that service is active. 28406 * 28407 * 2. ip_output determines whether it has been called from service, 28408 * based on the last parameter. If it is IP_WSRV it concludes it 28409 * has been called from service. 28410 * 28411 * 3. Message ordering is preserved by the following logic. 28412 * i. A directly called ip_output (i.e. not thru service) will queue 28413 * the message at the tail, if conn_draining is set (i.e. service 28414 * is running) or if q->q_first is non-null. 28415 * 28416 * ii. If ip_output is called from service, and if ip_output cannot 28417 * putnext due to flow control, it does a putbq. 28418 * 28419 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 28420 * (causing an infinite loop). 28421 */ 28422 ASSERT(!connp->conn_did_putbq); 28423 28424 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 28425 connp->conn_draining = 1; 28426 noenable(q); 28427 while ((mp = getq(q)) != NULL) { 28428 ASSERT(CONN_Q(q)); 28429 28430 DTRACE_PROBE1(ip__wsrv__ip__output, conn_t *, connp); 28431 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 28432 if (connp->conn_did_putbq) { 28433 /* ip_wput did a putbq */ 28434 break; 28435 } 28436 } 28437 /* 28438 * At this point, a thread coming down from top, calling 28439 * ip_wput, may end up queueing the message. We have not yet 28440 * enabled the queue, so ip_wsrv won't be called again. 28441 * To avoid this race, check q->q_first again (in the loop) 28442 * If the other thread queued the message before we call 28443 * enableok(), we will catch it in the q->q_first check. 28444 * If the other thread queues the message after we call 28445 * enableok(), ip_wsrv will be called again by STREAMS. 28446 */ 28447 connp->conn_draining = 0; 28448 enableok(q); 28449 } 28450 28451 /* Enable the next conn for draining */ 28452 conn_drain_tail(connp, B_FALSE); 28453 28454 /* 28455 * conn_direct_blocked is used to indicate blocked 28456 * condition for direct path (ILL_DIRECT_CAPABLE()). 28457 * This is the only place where it is set without 28458 * checking for ILL_DIRECT_CAPABLE() and setting it 28459 * to 0 is ok even if it is not ILL_DIRECT_CAPABLE(). 28460 */ 28461 if (!connp->conn_did_putbq && connp->conn_direct_blocked) { 28462 DTRACE_PROBE1(ip__wsrv__direct__blocked, conn_t *, connp); 28463 connp->conn_direct_blocked = B_FALSE; 28464 } 28465 28466 connp->conn_did_putbq = 0; 28467 } 28468 28469 /* 28470 * Callback to disable flow control in IP. 28471 * 28472 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability 28473 * is enabled. 28474 * 28475 * When MAC_TX() is not able to send any more packets, dld sets its queue 28476 * to QFULL and enable the STREAMS flow control. Later, when the underlying 28477 * driver is able to continue to send packets, it calls mac_tx_(ring_)update() 28478 * function and wakes up corresponding mac worker threads, which in turn 28479 * calls this callback function, and disables flow control. 28480 */ 28481 void 28482 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie) 28483 { 28484 ill_t *ill = (ill_t *)arg; 28485 ip_stack_t *ipst = ill->ill_ipst; 28486 idl_tx_list_t *idl_txl; 28487 28488 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)]; 28489 mutex_enter(&idl_txl->txl_lock); 28490 /* add code to to set a flag to indicate idl_txl is enabled */ 28491 conn_walk_drain(ipst, idl_txl); 28492 mutex_exit(&idl_txl->txl_lock); 28493 } 28494 28495 /* 28496 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 28497 * of conns that need to be drained, check if drain is already in progress. 28498 * If so set the idl_repeat bit, indicating that the last conn in the list 28499 * needs to reinitiate the drain once again, for the list. If drain is not 28500 * in progress for the list, initiate the draining, by qenabling the 1st 28501 * conn in the list. The drain is self-sustaining, each qenabled conn will 28502 * in turn qenable the next conn, when it is done/blocked/closing. 28503 */ 28504 static void 28505 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list) 28506 { 28507 int i; 28508 idl_t *idl; 28509 28510 IP_STAT(ipst, ip_conn_walk_drain); 28511 28512 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 28513 idl = &tx_list->txl_drain_list[i]; 28514 mutex_enter(&idl->idl_lock); 28515 if (idl->idl_conn == NULL) { 28516 mutex_exit(&idl->idl_lock); 28517 continue; 28518 } 28519 /* 28520 * If this list is not being drained currently by 28521 * an ip_wsrv thread, start the process. 28522 */ 28523 if (idl->idl_conn_draining == NULL) { 28524 ASSERT(idl->idl_repeat == 0); 28525 qenable(idl->idl_conn->conn_wq); 28526 idl->idl_conn_draining = idl->idl_conn; 28527 } else { 28528 idl->idl_repeat = 1; 28529 } 28530 mutex_exit(&idl->idl_lock); 28531 } 28532 } 28533 28534 /* 28535 * Determine if the ill and multicast aspects of that packets 28536 * "matches" the conn. 28537 */ 28538 boolean_t 28539 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 28540 zoneid_t zoneid) 28541 { 28542 ill_t *bound_ill; 28543 boolean_t found; 28544 ipif_t *ipif; 28545 ire_t *ire; 28546 ipaddr_t dst, src; 28547 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 28548 28549 dst = ipha->ipha_dst; 28550 src = ipha->ipha_src; 28551 28552 /* 28553 * conn_incoming_ill is set by IP_BOUND_IF which limits 28554 * unicast, broadcast and multicast reception to 28555 * conn_incoming_ill. conn_wantpacket itself is called 28556 * only for BROADCAST and multicast. 28557 */ 28558 bound_ill = connp->conn_incoming_ill; 28559 if (bound_ill != NULL) { 28560 if (IS_IPMP(bound_ill)) { 28561 if (bound_ill->ill_grp != ill->ill_grp) 28562 return (B_FALSE); 28563 } else { 28564 if (bound_ill != ill) 28565 return (B_FALSE); 28566 } 28567 } 28568 28569 if (!CLASSD(dst)) { 28570 if (IPCL_ZONE_MATCH(connp, zoneid)) 28571 return (B_TRUE); 28572 /* 28573 * The conn is in a different zone; we need to check that this 28574 * broadcast address is configured in the application's zone. 28575 */ 28576 ipif = ipif_get_next_ipif(NULL, ill); 28577 if (ipif == NULL) 28578 return (B_FALSE); 28579 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 28580 connp->conn_zoneid, NULL, 28581 (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst); 28582 ipif_refrele(ipif); 28583 if (ire != NULL) { 28584 ire_refrele(ire); 28585 return (B_TRUE); 28586 } else { 28587 return (B_FALSE); 28588 } 28589 } 28590 28591 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 28592 connp->conn_zoneid == zoneid) { 28593 /* 28594 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 28595 * disabled, therefore we don't dispatch the multicast packet to 28596 * the sending zone. 28597 */ 28598 return (B_FALSE); 28599 } 28600 28601 if (IS_LOOPBACK(ill) && connp->conn_zoneid != zoneid) { 28602 /* 28603 * Multicast packet on the loopback interface: we only match 28604 * conns who joined the group in the specified zone. 28605 */ 28606 return (B_FALSE); 28607 } 28608 28609 if (connp->conn_multi_router) { 28610 /* multicast packet and multicast router socket: send up */ 28611 return (B_TRUE); 28612 } 28613 28614 mutex_enter(&connp->conn_lock); 28615 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 28616 mutex_exit(&connp->conn_lock); 28617 return (found); 28618 } 28619 28620 static void 28621 conn_setqfull(conn_t *connp) 28622 { 28623 queue_t *q = connp->conn_wq; 28624 28625 if (!(q->q_flag & QFULL)) { 28626 mutex_enter(QLOCK(q)); 28627 if (!(q->q_flag & QFULL)) { 28628 /* still need to set QFULL */ 28629 q->q_flag |= QFULL; 28630 mutex_exit(QLOCK(q)); 28631 } else { 28632 mutex_exit(QLOCK(q)); 28633 } 28634 } 28635 } 28636 28637 static void 28638 conn_clrqfull(conn_t *connp) 28639 { 28640 queue_t *q = connp->conn_wq; 28641 28642 if (q->q_flag & QFULL) { 28643 mutex_enter(QLOCK(q)); 28644 if (q->q_flag & QFULL) { 28645 q->q_flag &= ~QFULL; 28646 mutex_exit(QLOCK(q)); 28647 if (q->q_flag & QWANTW) 28648 qbackenable(q, 0); 28649 } else { 28650 mutex_exit(QLOCK(q)); 28651 } 28652 } 28653 } 28654 28655 /* 28656 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 28657 */ 28658 /* ARGSUSED */ 28659 static void 28660 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 28661 { 28662 ill_t *ill = (ill_t *)q->q_ptr; 28663 mblk_t *mp1, *mp2; 28664 ipif_t *ipif; 28665 int err = 0; 28666 conn_t *connp = NULL; 28667 ipsq_t *ipsq; 28668 arc_t *arc; 28669 28670 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 28671 28672 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 28673 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 28674 28675 ASSERT(IAM_WRITER_ILL(ill)); 28676 mp2 = mp->b_cont; 28677 mp->b_cont = NULL; 28678 28679 /* 28680 * We have now received the arp bringup completion message 28681 * from ARP. Mark the arp bringup as done. Also if the arp 28682 * stream has already started closing, send up the AR_ARP_CLOSING 28683 * ack now since ARP is waiting in close for this ack. 28684 */ 28685 mutex_enter(&ill->ill_lock); 28686 ill->ill_arp_bringup_pending = 0; 28687 if (ill->ill_arp_closing) { 28688 mutex_exit(&ill->ill_lock); 28689 /* Let's reuse the mp for sending the ack */ 28690 arc = (arc_t *)mp->b_rptr; 28691 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 28692 arc->arc_cmd = AR_ARP_CLOSING; 28693 qreply(q, mp); 28694 } else { 28695 mutex_exit(&ill->ill_lock); 28696 freeb(mp); 28697 } 28698 28699 ipsq = ill->ill_phyint->phyint_ipsq; 28700 ipif = ipsq->ipsq_xop->ipx_pending_ipif; 28701 mp1 = ipsq_pending_mp_get(ipsq, &connp); 28702 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 28703 if (mp1 == NULL) { 28704 /* bringup was aborted by the user */ 28705 freemsg(mp2); 28706 return; 28707 } 28708 28709 /* 28710 * If an IOCTL is waiting on this (ipx_current_ioctl != 0), then we 28711 * must have an associated conn_t. Otherwise, we're bringing this 28712 * interface back up as part of handling an asynchronous event (e.g., 28713 * physical address change). 28714 */ 28715 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) { 28716 ASSERT(connp != NULL); 28717 q = CONNP_TO_WQ(connp); 28718 } else { 28719 ASSERT(connp == NULL); 28720 q = ill->ill_rq; 28721 } 28722 28723 /* 28724 * If the DL_BIND_REQ fails, it is noted 28725 * in arc_name_offset. 28726 */ 28727 err = *((int *)mp2->b_rptr); 28728 if (err == 0) { 28729 if (ipif->ipif_isv6) { 28730 if ((err = ipif_up_done_v6(ipif)) != 0) 28731 ip0dbg(("ip_arp_done: init failed\n")); 28732 } else { 28733 if ((err = ipif_up_done(ipif)) != 0) 28734 ip0dbg(("ip_arp_done: init failed\n")); 28735 } 28736 } else { 28737 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 28738 } 28739 28740 freemsg(mp2); 28741 28742 if ((err == 0) && (ill->ill_up_ipifs)) { 28743 err = ill_up_ipifs(ill, q, mp1); 28744 if (err == EINPROGRESS) 28745 return; 28746 } 28747 28748 /* 28749 * If we have a moved ipif to bring up, and everything has succeeded 28750 * to this point, bring it up on the IPMP ill. Otherwise, leave it 28751 * down -- the admin can try to bring it up by hand if need be. 28752 */ 28753 if (ill->ill_move_ipif != NULL) { 28754 ipif = ill->ill_move_ipif; 28755 ill->ill_move_ipif = NULL; 28756 if (err == 0) { 28757 err = ipif_up(ipif, q, mp1); 28758 if (err == EINPROGRESS) 28759 return; 28760 } 28761 } 28762 28763 /* 28764 * The operation must complete without EINPROGRESS since 28765 * ipsq_pending_mp_get() has removed the mblk. Otherwise, the 28766 * operation will be stuck forever in the ipsq. 28767 */ 28768 ASSERT(err != EINPROGRESS); 28769 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) 28770 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 28771 else 28772 ipsq_current_finish(ipsq); 28773 } 28774 28775 /* Allocate the private structure */ 28776 static int 28777 ip_priv_alloc(void **bufp) 28778 { 28779 void *buf; 28780 28781 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 28782 return (ENOMEM); 28783 28784 *bufp = buf; 28785 return (0); 28786 } 28787 28788 /* Function to delete the private structure */ 28789 void 28790 ip_priv_free(void *buf) 28791 { 28792 ASSERT(buf != NULL); 28793 kmem_free(buf, sizeof (ip_priv_t)); 28794 } 28795 28796 /* 28797 * The entry point for IPPF processing. 28798 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 28799 * routine just returns. 28800 * 28801 * When called, ip_process generates an ipp_packet_t structure 28802 * which holds the state information for this packet and invokes the 28803 * the classifier (via ipp_packet_process). The classification, depending on 28804 * configured filters, results in a list of actions for this packet. Invoking 28805 * an action may cause the packet to be dropped, in which case the resulting 28806 * mblk (*mpp) is NULL. proc indicates the callout position for 28807 * this packet and ill_index is the interface this packet on or will leave 28808 * on (inbound and outbound resp.). 28809 */ 28810 void 28811 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 28812 { 28813 mblk_t *mp; 28814 ip_priv_t *priv; 28815 ipp_action_id_t aid; 28816 int rc = 0; 28817 ipp_packet_t *pp; 28818 #define IP_CLASS "ip" 28819 28820 /* If the classifier is not loaded, return */ 28821 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 28822 return; 28823 } 28824 28825 mp = *mpp; 28826 ASSERT(mp != NULL); 28827 28828 /* Allocate the packet structure */ 28829 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 28830 if (rc != 0) { 28831 *mpp = NULL; 28832 freemsg(mp); 28833 return; 28834 } 28835 28836 /* Allocate the private structure */ 28837 rc = ip_priv_alloc((void **)&priv); 28838 if (rc != 0) { 28839 *mpp = NULL; 28840 freemsg(mp); 28841 ipp_packet_free(pp); 28842 return; 28843 } 28844 priv->proc = proc; 28845 priv->ill_index = ill_index; 28846 ipp_packet_set_private(pp, priv, ip_priv_free); 28847 ipp_packet_set_data(pp, mp); 28848 28849 /* Invoke the classifier */ 28850 rc = ipp_packet_process(&pp); 28851 if (pp != NULL) { 28852 mp = ipp_packet_get_data(pp); 28853 ipp_packet_free(pp); 28854 if (rc != 0) { 28855 freemsg(mp); 28856 *mpp = NULL; 28857 } 28858 } else { 28859 *mpp = NULL; 28860 } 28861 #undef IP_CLASS 28862 } 28863 28864 /* 28865 * Propagate a multicast group membership operation (add/drop) on 28866 * all the interfaces crossed by the related multirt routes. 28867 * The call is considered successful if the operation succeeds 28868 * on at least one interface. 28869 */ 28870 static int 28871 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 28872 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 28873 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 28874 mblk_t *first_mp) 28875 { 28876 ire_t *ire_gw; 28877 irb_t *irb; 28878 int error = 0; 28879 opt_restart_t *or; 28880 ip_stack_t *ipst = ire->ire_ipst; 28881 28882 irb = ire->ire_bucket; 28883 ASSERT(irb != NULL); 28884 28885 ASSERT(DB_TYPE(first_mp) == M_CTL); 28886 28887 or = (opt_restart_t *)first_mp->b_rptr; 28888 IRB_REFHOLD(irb); 28889 for (; ire != NULL; ire = ire->ire_next) { 28890 if ((ire->ire_flags & RTF_MULTIRT) == 0) 28891 continue; 28892 if (ire->ire_addr != group) 28893 continue; 28894 28895 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 28896 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 28897 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE, ipst); 28898 /* No resolver exists for the gateway; skip this ire. */ 28899 if (ire_gw == NULL) 28900 continue; 28901 28902 /* 28903 * This function can return EINPROGRESS. If so the operation 28904 * will be restarted from ip_restart_optmgmt which will 28905 * call ip_opt_set and option processing will restart for 28906 * this option. So we may end up calling 'fn' more than once. 28907 * This requires that 'fn' is idempotent except for the 28908 * return value. The operation is considered a success if 28909 * it succeeds at least once on any one interface. 28910 */ 28911 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 28912 NULL, fmode, src, first_mp); 28913 if (error == 0) 28914 or->or_private = CGTP_MCAST_SUCCESS; 28915 28916 if (ip_debug > 0) { 28917 ulong_t off; 28918 char *ksym; 28919 ksym = kobj_getsymname((uintptr_t)fn, &off); 28920 ip2dbg(("ip_multirt_apply_membership: " 28921 "called %s, multirt group 0x%08x via itf 0x%08x, " 28922 "error %d [success %u]\n", 28923 ksym ? ksym : "?", 28924 ntohl(group), ntohl(ire_gw->ire_src_addr), 28925 error, or->or_private)); 28926 } 28927 28928 ire_refrele(ire_gw); 28929 if (error == EINPROGRESS) { 28930 IRB_REFRELE(irb); 28931 return (error); 28932 } 28933 } 28934 IRB_REFRELE(irb); 28935 /* 28936 * Consider the call as successful if we succeeded on at least 28937 * one interface. Otherwise, return the last encountered error. 28938 */ 28939 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 28940 } 28941 28942 /* 28943 * Issue a warning regarding a route crossing an interface with an 28944 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 28945 * amount of time is logged. 28946 */ 28947 static void 28948 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 28949 { 28950 hrtime_t current = gethrtime(); 28951 char buf[INET_ADDRSTRLEN]; 28952 ip_stack_t *ipst = ire->ire_ipst; 28953 28954 /* Convert interval in ms to hrtime in ns */ 28955 if (ipst->ips_multirt_bad_mtu_last_time + 28956 ((hrtime_t)ipst->ips_ip_multirt_log_interval * (hrtime_t)1000000) <= 28957 current) { 28958 cmn_err(CE_WARN, "ip: ignoring multiroute " 28959 "to %s, incorrect MTU %u (expected %u)\n", 28960 ip_dot_addr(ire->ire_addr, buf), 28961 ire->ire_max_frag, max_frag); 28962 28963 ipst->ips_multirt_bad_mtu_last_time = current; 28964 } 28965 } 28966 28967 /* 28968 * Get the CGTP (multirouting) filtering status. 28969 * If 0, the CGTP hooks are transparent. 28970 */ 28971 /* ARGSUSED */ 28972 static int 28973 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 28974 { 28975 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28976 28977 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 28978 return (0); 28979 } 28980 28981 /* 28982 * Set the CGTP (multirouting) filtering status. 28983 * If the status is changed from active to transparent 28984 * or from transparent to active, forward the new status 28985 * to the filtering module (if loaded). 28986 */ 28987 /* ARGSUSED */ 28988 static int 28989 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 28990 cred_t *ioc_cr) 28991 { 28992 long new_value; 28993 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28994 ip_stack_t *ipst = CONNQ_TO_IPST(q); 28995 28996 if (secpolicy_ip_config(ioc_cr, B_FALSE) != 0) 28997 return (EPERM); 28998 28999 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 29000 new_value < 0 || new_value > 1) { 29001 return (EINVAL); 29002 } 29003 29004 if ((!*ip_cgtp_filter_value) && new_value) { 29005 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 29006 ipst->ips_ip_cgtp_filter_ops == NULL ? 29007 " (module not loaded)" : ""); 29008 } 29009 if (*ip_cgtp_filter_value && (!new_value)) { 29010 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 29011 ipst->ips_ip_cgtp_filter_ops == NULL ? 29012 " (module not loaded)" : ""); 29013 } 29014 29015 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 29016 int res; 29017 netstackid_t stackid; 29018 29019 stackid = ipst->ips_netstack->netstack_stackid; 29020 res = ipst->ips_ip_cgtp_filter_ops->cfo_change_state(stackid, 29021 new_value); 29022 if (res) 29023 return (res); 29024 } 29025 29026 *ip_cgtp_filter_value = (boolean_t)new_value; 29027 29028 return (0); 29029 } 29030 29031 /* 29032 * Return the expected CGTP hooks version number. 29033 */ 29034 int 29035 ip_cgtp_filter_supported(void) 29036 { 29037 return (ip_cgtp_filter_rev); 29038 } 29039 29040 /* 29041 * CGTP hooks can be registered by invoking this function. 29042 * Checks that the version number matches. 29043 */ 29044 int 29045 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops) 29046 { 29047 netstack_t *ns; 29048 ip_stack_t *ipst; 29049 29050 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 29051 return (ENOTSUP); 29052 29053 ns = netstack_find_by_stackid(stackid); 29054 if (ns == NULL) 29055 return (EINVAL); 29056 ipst = ns->netstack_ip; 29057 ASSERT(ipst != NULL); 29058 29059 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 29060 netstack_rele(ns); 29061 return (EALREADY); 29062 } 29063 29064 ipst->ips_ip_cgtp_filter_ops = ops; 29065 netstack_rele(ns); 29066 return (0); 29067 } 29068 29069 /* 29070 * CGTP hooks can be unregistered by invoking this function. 29071 * Returns ENXIO if there was no registration. 29072 * Returns EBUSY if the ndd variable has not been turned off. 29073 */ 29074 int 29075 ip_cgtp_filter_unregister(netstackid_t stackid) 29076 { 29077 netstack_t *ns; 29078 ip_stack_t *ipst; 29079 29080 ns = netstack_find_by_stackid(stackid); 29081 if (ns == NULL) 29082 return (EINVAL); 29083 ipst = ns->netstack_ip; 29084 ASSERT(ipst != NULL); 29085 29086 if (ipst->ips_ip_cgtp_filter) { 29087 netstack_rele(ns); 29088 return (EBUSY); 29089 } 29090 29091 if (ipst->ips_ip_cgtp_filter_ops == NULL) { 29092 netstack_rele(ns); 29093 return (ENXIO); 29094 } 29095 ipst->ips_ip_cgtp_filter_ops = NULL; 29096 netstack_rele(ns); 29097 return (0); 29098 } 29099 29100 /* 29101 * Check whether there is a CGTP filter registration. 29102 * Returns non-zero if there is a registration, otherwise returns zero. 29103 * Note: returns zero if bad stackid. 29104 */ 29105 int 29106 ip_cgtp_filter_is_registered(netstackid_t stackid) 29107 { 29108 netstack_t *ns; 29109 ip_stack_t *ipst; 29110 int ret; 29111 29112 ns = netstack_find_by_stackid(stackid); 29113 if (ns == NULL) 29114 return (0); 29115 ipst = ns->netstack_ip; 29116 ASSERT(ipst != NULL); 29117 29118 if (ipst->ips_ip_cgtp_filter_ops != NULL) 29119 ret = 1; 29120 else 29121 ret = 0; 29122 29123 netstack_rele(ns); 29124 return (ret); 29125 } 29126 29127 static int 29128 ip_squeue_switch(int val) 29129 { 29130 int rval = SQ_FILL; 29131 29132 switch (val) { 29133 case IP_SQUEUE_ENTER_NODRAIN: 29134 rval = SQ_NODRAIN; 29135 break; 29136 case IP_SQUEUE_ENTER: 29137 rval = SQ_PROCESS; 29138 break; 29139 default: 29140 break; 29141 } 29142 return (rval); 29143 } 29144 29145 /* ARGSUSED */ 29146 static int 29147 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 29148 caddr_t addr, cred_t *cr) 29149 { 29150 int *v = (int *)addr; 29151 long new_value; 29152 29153 if (secpolicy_net_config(cr, B_FALSE) != 0) 29154 return (EPERM); 29155 29156 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29157 return (EINVAL); 29158 29159 ip_squeue_flag = ip_squeue_switch(new_value); 29160 *v = new_value; 29161 return (0); 29162 } 29163 29164 /* 29165 * Handle ndd set of variables which require PRIV_SYS_NET_CONFIG such as 29166 * ip_debug. 29167 */ 29168 /* ARGSUSED */ 29169 static int 29170 ip_int_set(queue_t *q, mblk_t *mp, char *value, 29171 caddr_t addr, cred_t *cr) 29172 { 29173 int *v = (int *)addr; 29174 long new_value; 29175 29176 if (secpolicy_net_config(cr, B_FALSE) != 0) 29177 return (EPERM); 29178 29179 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 29180 return (EINVAL); 29181 29182 *v = new_value; 29183 return (0); 29184 } 29185 29186 static void * 29187 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp) 29188 { 29189 kstat_t *ksp; 29190 29191 ip_stat_t template = { 29192 { "ipsec_fanout_proto", KSTAT_DATA_UINT64 }, 29193 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 29194 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 29195 { "ip_udp_fanothers", KSTAT_DATA_UINT64 }, 29196 { "ip_udp_fast_path", KSTAT_DATA_UINT64 }, 29197 { "ip_udp_slow_path", KSTAT_DATA_UINT64 }, 29198 { "ip_udp_input_err", KSTAT_DATA_UINT64 }, 29199 { "ip_tcppullup", KSTAT_DATA_UINT64 }, 29200 { "ip_tcpoptions", KSTAT_DATA_UINT64 }, 29201 { "ip_multipkttcp", KSTAT_DATA_UINT64 }, 29202 { "ip_tcp_fast_path", KSTAT_DATA_UINT64 }, 29203 { "ip_tcp_slow_path", KSTAT_DATA_UINT64 }, 29204 { "ip_tcp_input_error", KSTAT_DATA_UINT64 }, 29205 { "ip_db_ref", KSTAT_DATA_UINT64 }, 29206 { "ip_notaligned1", KSTAT_DATA_UINT64 }, 29207 { "ip_notaligned2", KSTAT_DATA_UINT64 }, 29208 { "ip_multimblk3", KSTAT_DATA_UINT64 }, 29209 { "ip_multimblk4", KSTAT_DATA_UINT64 }, 29210 { "ip_ipoptions", KSTAT_DATA_UINT64 }, 29211 { "ip_classify_fail", KSTAT_DATA_UINT64 }, 29212 { "ip_opt", KSTAT_DATA_UINT64 }, 29213 { "ip_udp_rput_local", KSTAT_DATA_UINT64 }, 29214 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 29215 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 29216 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 29217 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 29218 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 29219 { "ip_trash_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 29220 { "ip_trash_ire_reclaim_success", KSTAT_DATA_UINT64 }, 29221 { "ip_ire_arp_timer_expired", KSTAT_DATA_UINT64 }, 29222 { "ip_ire_redirect_timer_expired", KSTAT_DATA_UINT64 }, 29223 { "ip_ire_pmtu_timer_expired", KSTAT_DATA_UINT64 }, 29224 { "ip_input_multi_squeue", KSTAT_DATA_UINT64 }, 29225 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29226 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29227 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29228 { "ip_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29229 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 29230 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 29231 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 29232 { "ip_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 29233 { "ip_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 29234 { "ip_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 29235 { "ip_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 29236 { "ip_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 29237 { "ip_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 29238 }; 29239 29240 ksp = kstat_create_netstack("ip", 0, "ipstat", "net", 29241 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 29242 KSTAT_FLAG_VIRTUAL, stackid); 29243 29244 if (ksp == NULL) 29245 return (NULL); 29246 29247 bcopy(&template, ip_statisticsp, sizeof (template)); 29248 ksp->ks_data = (void *)ip_statisticsp; 29249 ksp->ks_private = (void *)(uintptr_t)stackid; 29250 29251 kstat_install(ksp); 29252 return (ksp); 29253 } 29254 29255 static void 29256 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 29257 { 29258 if (ksp != NULL) { 29259 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29260 kstat_delete_netstack(ksp, stackid); 29261 } 29262 } 29263 29264 static void * 29265 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst) 29266 { 29267 kstat_t *ksp; 29268 29269 ip_named_kstat_t template = { 29270 { "forwarding", KSTAT_DATA_UINT32, 0 }, 29271 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 29272 { "inReceives", KSTAT_DATA_UINT64, 0 }, 29273 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 29274 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 29275 { "forwDatagrams", KSTAT_DATA_UINT64, 0 }, 29276 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 29277 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 29278 { "inDelivers", KSTAT_DATA_UINT64, 0 }, 29279 { "outRequests", KSTAT_DATA_UINT64, 0 }, 29280 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 29281 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 29282 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 29283 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 29284 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 29285 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 29286 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 29287 { "fragFails", KSTAT_DATA_UINT32, 0 }, 29288 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 29289 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 29290 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 29291 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 29292 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 29293 { "inErrs", KSTAT_DATA_UINT32, 0 }, 29294 { "noPorts", KSTAT_DATA_UINT32, 0 }, 29295 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 29296 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 29297 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 29298 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 29299 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 29300 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 29301 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 29302 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 29303 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 29304 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 29305 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 29306 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 29307 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 29308 }; 29309 29310 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 29311 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid); 29312 if (ksp == NULL || ksp->ks_data == NULL) 29313 return (NULL); 29314 29315 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2; 29316 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl; 29317 template.reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29318 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 29319 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 29320 29321 template.netToMediaEntrySize.value.i32 = 29322 sizeof (mib2_ipNetToMediaEntry_t); 29323 29324 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 29325 29326 bcopy(&template, ksp->ks_data, sizeof (template)); 29327 ksp->ks_update = ip_kstat_update; 29328 ksp->ks_private = (void *)(uintptr_t)stackid; 29329 29330 kstat_install(ksp); 29331 return (ksp); 29332 } 29333 29334 static void 29335 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29336 { 29337 if (ksp != NULL) { 29338 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29339 kstat_delete_netstack(ksp, stackid); 29340 } 29341 } 29342 29343 static int 29344 ip_kstat_update(kstat_t *kp, int rw) 29345 { 29346 ip_named_kstat_t *ipkp; 29347 mib2_ipIfStatsEntry_t ipmib; 29348 ill_walk_context_t ctx; 29349 ill_t *ill; 29350 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29351 netstack_t *ns; 29352 ip_stack_t *ipst; 29353 29354 if (kp == NULL || kp->ks_data == NULL) 29355 return (EIO); 29356 29357 if (rw == KSTAT_WRITE) 29358 return (EACCES); 29359 29360 ns = netstack_find_by_stackid(stackid); 29361 if (ns == NULL) 29362 return (-1); 29363 ipst = ns->netstack_ip; 29364 if (ipst == NULL) { 29365 netstack_rele(ns); 29366 return (-1); 29367 } 29368 ipkp = (ip_named_kstat_t *)kp->ks_data; 29369 29370 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib)); 29371 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 29372 ill = ILL_START_WALK_V4(&ctx, ipst); 29373 for (; ill != NULL; ill = ill_next(&ctx, ill)) 29374 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib); 29375 rw_exit(&ipst->ips_ill_g_lock); 29376 29377 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding; 29378 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL; 29379 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives; 29380 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors; 29381 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors; 29382 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams; 29383 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos; 29384 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards; 29385 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers; 29386 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests; 29387 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards; 29388 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes; 29389 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_g_frag_timeout; 29390 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds; 29391 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs; 29392 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails; 29393 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs; 29394 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails; 29395 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates; 29396 29397 ipkp->routingDiscards.value.ui32 = 0; 29398 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs; 29399 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts; 29400 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs; 29401 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates; 29402 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups; 29403 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits; 29404 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs; 29405 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows; 29406 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows; 29407 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded; 29408 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed; 29409 29410 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion; 29411 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion; 29412 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion; 29413 29414 netstack_rele(ns); 29415 29416 return (0); 29417 } 29418 29419 static void * 29420 icmp_kstat_init(netstackid_t stackid) 29421 { 29422 kstat_t *ksp; 29423 29424 icmp_named_kstat_t template = { 29425 { "inMsgs", KSTAT_DATA_UINT32 }, 29426 { "inErrors", KSTAT_DATA_UINT32 }, 29427 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 29428 { "inTimeExcds", KSTAT_DATA_UINT32 }, 29429 { "inParmProbs", KSTAT_DATA_UINT32 }, 29430 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 29431 { "inRedirects", KSTAT_DATA_UINT32 }, 29432 { "inEchos", KSTAT_DATA_UINT32 }, 29433 { "inEchoReps", KSTAT_DATA_UINT32 }, 29434 { "inTimestamps", KSTAT_DATA_UINT32 }, 29435 { "inTimestampReps", KSTAT_DATA_UINT32 }, 29436 { "inAddrMasks", KSTAT_DATA_UINT32 }, 29437 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 29438 { "outMsgs", KSTAT_DATA_UINT32 }, 29439 { "outErrors", KSTAT_DATA_UINT32 }, 29440 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 29441 { "outTimeExcds", KSTAT_DATA_UINT32 }, 29442 { "outParmProbs", KSTAT_DATA_UINT32 }, 29443 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 29444 { "outRedirects", KSTAT_DATA_UINT32 }, 29445 { "outEchos", KSTAT_DATA_UINT32 }, 29446 { "outEchoReps", KSTAT_DATA_UINT32 }, 29447 { "outTimestamps", KSTAT_DATA_UINT32 }, 29448 { "outTimestampReps", KSTAT_DATA_UINT32 }, 29449 { "outAddrMasks", KSTAT_DATA_UINT32 }, 29450 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 29451 { "inChksumErrs", KSTAT_DATA_UINT32 }, 29452 { "inUnknowns", KSTAT_DATA_UINT32 }, 29453 { "inFragNeeded", KSTAT_DATA_UINT32 }, 29454 { "outFragNeeded", KSTAT_DATA_UINT32 }, 29455 { "outDrops", KSTAT_DATA_UINT32 }, 29456 { "inOverFlows", KSTAT_DATA_UINT32 }, 29457 { "inBadRedirects", KSTAT_DATA_UINT32 }, 29458 }; 29459 29460 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 29461 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid); 29462 if (ksp == NULL || ksp->ks_data == NULL) 29463 return (NULL); 29464 29465 bcopy(&template, ksp->ks_data, sizeof (template)); 29466 29467 ksp->ks_update = icmp_kstat_update; 29468 ksp->ks_private = (void *)(uintptr_t)stackid; 29469 29470 kstat_install(ksp); 29471 return (ksp); 29472 } 29473 29474 static void 29475 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 29476 { 29477 if (ksp != NULL) { 29478 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 29479 kstat_delete_netstack(ksp, stackid); 29480 } 29481 } 29482 29483 static int 29484 icmp_kstat_update(kstat_t *kp, int rw) 29485 { 29486 icmp_named_kstat_t *icmpkp; 29487 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 29488 netstack_t *ns; 29489 ip_stack_t *ipst; 29490 29491 if ((kp == NULL) || (kp->ks_data == NULL)) 29492 return (EIO); 29493 29494 if (rw == KSTAT_WRITE) 29495 return (EACCES); 29496 29497 ns = netstack_find_by_stackid(stackid); 29498 if (ns == NULL) 29499 return (-1); 29500 ipst = ns->netstack_ip; 29501 if (ipst == NULL) { 29502 netstack_rele(ns); 29503 return (-1); 29504 } 29505 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 29506 29507 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs; 29508 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors; 29509 icmpkp->inDestUnreachs.value.ui32 = 29510 ipst->ips_icmp_mib.icmpInDestUnreachs; 29511 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds; 29512 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs; 29513 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs; 29514 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects; 29515 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos; 29516 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps; 29517 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps; 29518 icmpkp->inTimestampReps.value.ui32 = 29519 ipst->ips_icmp_mib.icmpInTimestampReps; 29520 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks; 29521 icmpkp->inAddrMaskReps.value.ui32 = 29522 ipst->ips_icmp_mib.icmpInAddrMaskReps; 29523 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs; 29524 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors; 29525 icmpkp->outDestUnreachs.value.ui32 = 29526 ipst->ips_icmp_mib.icmpOutDestUnreachs; 29527 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds; 29528 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs; 29529 icmpkp->outSrcQuenchs.value.ui32 = 29530 ipst->ips_icmp_mib.icmpOutSrcQuenchs; 29531 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects; 29532 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos; 29533 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps; 29534 icmpkp->outTimestamps.value.ui32 = 29535 ipst->ips_icmp_mib.icmpOutTimestamps; 29536 icmpkp->outTimestampReps.value.ui32 = 29537 ipst->ips_icmp_mib.icmpOutTimestampReps; 29538 icmpkp->outAddrMasks.value.ui32 = 29539 ipst->ips_icmp_mib.icmpOutAddrMasks; 29540 icmpkp->outAddrMaskReps.value.ui32 = 29541 ipst->ips_icmp_mib.icmpOutAddrMaskReps; 29542 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs; 29543 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns; 29544 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded; 29545 icmpkp->outFragNeeded.value.ui32 = 29546 ipst->ips_icmp_mib.icmpOutFragNeeded; 29547 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops; 29548 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows; 29549 icmpkp->inBadRedirects.value.ui32 = 29550 ipst->ips_icmp_mib.icmpInBadRedirects; 29551 29552 netstack_rele(ns); 29553 return (0); 29554 } 29555 29556 /* 29557 * This is the fanout function for raw socket opened for SCTP. Note 29558 * that it is called after SCTP checks that there is no socket which 29559 * wants a packet. Then before SCTP handles this out of the blue packet, 29560 * this function is called to see if there is any raw socket for SCTP. 29561 * If there is and it is bound to the correct address, the packet will 29562 * be sent to that socket. Note that only one raw socket can be bound to 29563 * a port. This is assured in ipcl_sctp_hash_insert(); 29564 */ 29565 void 29566 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 29567 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 29568 zoneid_t zoneid) 29569 { 29570 conn_t *connp; 29571 queue_t *rq; 29572 mblk_t *first_mp; 29573 boolean_t secure; 29574 ip6_t *ip6h; 29575 ip_stack_t *ipst = recv_ill->ill_ipst; 29576 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 29577 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp; 29578 boolean_t sctp_csum_err = B_FALSE; 29579 29580 if (flags & IP_FF_SCTP_CSUM_ERR) { 29581 sctp_csum_err = B_TRUE; 29582 flags &= ~IP_FF_SCTP_CSUM_ERR; 29583 } 29584 29585 first_mp = mp; 29586 if (mctl_present) { 29587 mp = first_mp->b_cont; 29588 secure = ipsec_in_is_secure(first_mp); 29589 ASSERT(mp != NULL); 29590 } else { 29591 secure = B_FALSE; 29592 } 29593 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 29594 29595 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha, ipst); 29596 if (connp == NULL) { 29597 /* 29598 * Although raw sctp is not summed, OOB chunks must be. 29599 * Drop the packet here if the sctp checksum failed. 29600 */ 29601 if (sctp_csum_err) { 29602 BUMP_MIB(&sctps->sctps_mib, sctpChecksumError); 29603 freemsg(first_mp); 29604 return; 29605 } 29606 sctp_ootb_input(first_mp, recv_ill, zoneid, mctl_present); 29607 return; 29608 } 29609 rq = connp->conn_rq; 29610 if (!canputnext(rq)) { 29611 CONN_DEC_REF(connp); 29612 BUMP_MIB(recv_ill->ill_ip_mib, rawipIfStatsInOverflows); 29613 freemsg(first_mp); 29614 return; 29615 } 29616 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 29617 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || secure) { 29618 first_mp = ipsec_check_inbound_policy(first_mp, connp, 29619 (isv4 ? ipha : NULL), ip6h, mctl_present); 29620 if (first_mp == NULL) { 29621 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 29622 CONN_DEC_REF(connp); 29623 return; 29624 } 29625 } 29626 /* 29627 * We probably should not send M_CTL message up to 29628 * raw socket. 29629 */ 29630 if (mctl_present) 29631 freeb(first_mp); 29632 29633 /* Initiate IPPF processing here if needed. */ 29634 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) || 29635 (!isv4 && IP6_IN_IPP(flags, ipst))) { 29636 ip_process(IPP_LOCAL_IN, &mp, 29637 recv_ill->ill_phyint->phyint_ifindex); 29638 if (mp == NULL) { 29639 CONN_DEC_REF(connp); 29640 return; 29641 } 29642 } 29643 29644 if (connp->conn_recvif || connp->conn_recvslla || 29645 ((connp->conn_ip_recvpktinfo || 29646 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 29647 (flags & IP_FF_IPINFO))) { 29648 int in_flags = 0; 29649 29650 /* 29651 * Since sctp does not support IP_RECVPKTINFO for v4, only pass 29652 * IPF_RECVIF. 29653 */ 29654 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 29655 in_flags = IPF_RECVIF; 29656 } 29657 if (connp->conn_recvslla) { 29658 in_flags |= IPF_RECVSLLA; 29659 } 29660 if (isv4) { 29661 mp = ip_add_info(mp, recv_ill, in_flags, 29662 IPCL_ZONEID(connp), ipst); 29663 } else { 29664 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 29665 if (mp == NULL) { 29666 BUMP_MIB(recv_ill->ill_ip_mib, 29667 ipIfStatsInDiscards); 29668 CONN_DEC_REF(connp); 29669 return; 29670 } 29671 } 29672 } 29673 29674 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 29675 /* 29676 * We are sending the IPSEC_IN message also up. Refer 29677 * to comments above this function. 29678 * This is the SOCK_RAW, IPPROTO_SCTP case. 29679 */ 29680 (connp->conn_recv)(connp, mp, NULL); 29681 CONN_DEC_REF(connp); 29682 } 29683 29684 #define UPDATE_IP_MIB_OB_COUNTERS(ill, len) \ 29685 { \ 29686 BUMP_MIB((ill)->ill_ip_mib, ipIfStatsHCOutTransmits); \ 29687 UPDATE_MIB((ill)->ill_ip_mib, ipIfStatsHCOutOctets, (len)); \ 29688 } 29689 /* 29690 * This function should be called only if all packet processing 29691 * including fragmentation is complete. Callers of this function 29692 * must set mp->b_prev to one of these values: 29693 * {0, IPP_FWD_OUT, IPP_LOCAL_OUT} 29694 * prior to handing over the mp as first argument to this function. 29695 * 29696 * If the ire passed by caller is incomplete, this function 29697 * queues the packet and if necessary, sends ARP request and bails. 29698 * If the ire passed is fully resolved, we simply prepend 29699 * the link-layer header to the packet, do ipsec hw acceleration 29700 * work if necessary, and send the packet out on the wire. 29701 * 29702 * NOTE: IPsec will only call this function with fully resolved 29703 * ires if hw acceleration is involved. 29704 * TODO list : 29705 * a Handle M_MULTIDATA so that 29706 * tcp_multisend->tcp_multisend_data can 29707 * call ip_xmit_v4 directly 29708 * b Handle post-ARP work for fragments so that 29709 * ip_wput_frag can call this function. 29710 */ 29711 ipxmit_state_t 29712 ip_xmit_v4(mblk_t *mp, ire_t *ire, ipsec_out_t *io, 29713 boolean_t flow_ctl_enabled, conn_t *connp) 29714 { 29715 nce_t *arpce; 29716 ipha_t *ipha; 29717 queue_t *q; 29718 int ill_index; 29719 mblk_t *nxt_mp, *first_mp; 29720 boolean_t xmit_drop = B_FALSE; 29721 ip_proc_t proc; 29722 ill_t *out_ill; 29723 int pkt_len; 29724 29725 arpce = ire->ire_nce; 29726 ASSERT(arpce != NULL); 29727 29728 DTRACE_PROBE2(ip__xmit__v4, ire_t *, ire, nce_t *, arpce); 29729 29730 mutex_enter(&arpce->nce_lock); 29731 switch (arpce->nce_state) { 29732 case ND_REACHABLE: 29733 /* If there are other queued packets, queue this packet */ 29734 if (arpce->nce_qd_mp != NULL) { 29735 if (mp != NULL) 29736 nce_queue_mp_common(arpce, mp, B_FALSE); 29737 mp = arpce->nce_qd_mp; 29738 } 29739 arpce->nce_qd_mp = NULL; 29740 mutex_exit(&arpce->nce_lock); 29741 29742 /* 29743 * Flush the queue. In the common case, where the 29744 * ARP is already resolved, it will go through the 29745 * while loop only once. 29746 */ 29747 while (mp != NULL) { 29748 29749 nxt_mp = mp->b_next; 29750 mp->b_next = NULL; 29751 ASSERT(mp->b_datap->db_type != M_CTL); 29752 pkt_len = ntohs(((ipha_t *)mp->b_rptr)->ipha_length); 29753 /* 29754 * This info is needed for IPQOS to do COS marking 29755 * in ip_wput_attach_llhdr->ip_process. 29756 */ 29757 proc = (ip_proc_t)(uintptr_t)mp->b_prev; 29758 mp->b_prev = NULL; 29759 29760 /* set up ill index for outbound qos processing */ 29761 out_ill = ire_to_ill(ire); 29762 ill_index = out_ill->ill_phyint->phyint_ifindex; 29763 first_mp = ip_wput_attach_llhdr(mp, ire, proc, 29764 ill_index, &ipha); 29765 if (first_mp == NULL) { 29766 xmit_drop = B_TRUE; 29767 BUMP_MIB(out_ill->ill_ip_mib, 29768 ipIfStatsOutDiscards); 29769 goto next_mp; 29770 } 29771 29772 /* non-ipsec hw accel case */ 29773 if (io == NULL || !io->ipsec_out_accelerated) { 29774 /* send it */ 29775 q = ire->ire_stq; 29776 if (proc == IPP_FWD_OUT) { 29777 UPDATE_IB_PKT_COUNT(ire); 29778 } else { 29779 UPDATE_OB_PKT_COUNT(ire); 29780 } 29781 ire->ire_last_used_time = lbolt; 29782 29783 if (flow_ctl_enabled || canputnext(q)) { 29784 if (proc == IPP_FWD_OUT) { 29785 29786 BUMP_MIB(out_ill->ill_ip_mib, 29787 ipIfStatsHCOutForwDatagrams); 29788 29789 } 29790 UPDATE_IP_MIB_OB_COUNTERS(out_ill, 29791 pkt_len); 29792 29793 DTRACE_IP7(send, mblk_t *, first_mp, 29794 conn_t *, NULL, void_ip_t *, ipha, 29795 __dtrace_ipsr_ill_t *, out_ill, 29796 ipha_t *, ipha, ip6_t *, NULL, int, 29797 0); 29798 29799 ILL_SEND_TX(out_ill, 29800 ire, connp, first_mp, 0, connp); 29801 } else { 29802 BUMP_MIB(out_ill->ill_ip_mib, 29803 ipIfStatsOutDiscards); 29804 xmit_drop = B_TRUE; 29805 freemsg(first_mp); 29806 } 29807 } else { 29808 /* 29809 * Safety Pup says: make sure this 29810 * is going to the right interface! 29811 */ 29812 ill_t *ill1 = 29813 (ill_t *)ire->ire_stq->q_ptr; 29814 int ifindex = 29815 ill1->ill_phyint->phyint_ifindex; 29816 if (ifindex != 29817 io->ipsec_out_capab_ill_index) { 29818 xmit_drop = B_TRUE; 29819 freemsg(mp); 29820 } else { 29821 UPDATE_IP_MIB_OB_COUNTERS(ill1, 29822 pkt_len); 29823 29824 DTRACE_IP7(send, mblk_t *, first_mp, 29825 conn_t *, NULL, void_ip_t *, ipha, 29826 __dtrace_ipsr_ill_t *, ill1, 29827 ipha_t *, ipha, ip6_t *, NULL, 29828 int, 0); 29829 29830 ipsec_hw_putnext(ire->ire_stq, mp); 29831 } 29832 } 29833 next_mp: 29834 mp = nxt_mp; 29835 } /* while (mp != NULL) */ 29836 if (xmit_drop) 29837 return (SEND_FAILED); 29838 else 29839 return (SEND_PASSED); 29840 29841 case ND_INITIAL: 29842 case ND_INCOMPLETE: 29843 29844 /* 29845 * While we do send off packets to dests that 29846 * use fully-resolved CGTP routes, we do not 29847 * handle unresolved CGTP routes. 29848 */ 29849 ASSERT(!(ire->ire_flags & RTF_MULTIRT)); 29850 ASSERT(io == NULL || !io->ipsec_out_accelerated); 29851 29852 if (mp != NULL) { 29853 /* queue the packet */ 29854 nce_queue_mp_common(arpce, mp, B_FALSE); 29855 } 29856 29857 if (arpce->nce_state == ND_INCOMPLETE) { 29858 mutex_exit(&arpce->nce_lock); 29859 DTRACE_PROBE3(ip__xmit__incomplete, 29860 (ire_t *), ire, (mblk_t *), mp, 29861 (ipsec_out_t *), io); 29862 return (LOOKUP_IN_PROGRESS); 29863 } 29864 29865 arpce->nce_state = ND_INCOMPLETE; 29866 mutex_exit(&arpce->nce_lock); 29867 29868 /* 29869 * Note that ire_add() (called from ire_forward()) 29870 * holds a ref on the ire until ARP is completed. 29871 */ 29872 ire_arpresolve(ire); 29873 return (LOOKUP_IN_PROGRESS); 29874 default: 29875 ASSERT(0); 29876 mutex_exit(&arpce->nce_lock); 29877 return (LLHDR_RESLV_FAILED); 29878 } 29879 } 29880 29881 #undef UPDATE_IP_MIB_OB_COUNTERS 29882 29883 /* 29884 * Return B_TRUE if the buffers differ in length or content. 29885 * This is used for comparing extension header buffers. 29886 * Note that an extension header would be declared different 29887 * even if all that changed was the next header value in that header i.e. 29888 * what really changed is the next extension header. 29889 */ 29890 boolean_t 29891 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 29892 uint_t blen) 29893 { 29894 if (!b_valid) 29895 blen = 0; 29896 29897 if (alen != blen) 29898 return (B_TRUE); 29899 if (alen == 0) 29900 return (B_FALSE); /* Both zero length */ 29901 return (bcmp(abuf, bbuf, alen)); 29902 } 29903 29904 /* 29905 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 29906 * Return B_FALSE if memory allocation fails - don't change any state! 29907 */ 29908 boolean_t 29909 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29910 const void *src, uint_t srclen) 29911 { 29912 void *dst; 29913 29914 if (!src_valid) 29915 srclen = 0; 29916 29917 ASSERT(*dstlenp == 0); 29918 if (src != NULL && srclen != 0) { 29919 dst = mi_alloc(srclen, BPRI_MED); 29920 if (dst == NULL) 29921 return (B_FALSE); 29922 } else { 29923 dst = NULL; 29924 } 29925 if (*dstp != NULL) 29926 mi_free(*dstp); 29927 *dstp = dst; 29928 *dstlenp = dst == NULL ? 0 : srclen; 29929 return (B_TRUE); 29930 } 29931 29932 /* 29933 * Replace what is in *dst, *dstlen with the source. 29934 * Assumes ip_allocbuf has already been called. 29935 */ 29936 void 29937 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29938 const void *src, uint_t srclen) 29939 { 29940 if (!src_valid) 29941 srclen = 0; 29942 29943 ASSERT(*dstlenp == srclen); 29944 if (src != NULL && srclen != 0) 29945 bcopy(src, *dstp, srclen); 29946 } 29947 29948 /* 29949 * Free the storage pointed to by the members of an ip6_pkt_t. 29950 */ 29951 void 29952 ip6_pkt_free(ip6_pkt_t *ipp) 29953 { 29954 ASSERT(ipp->ipp_pathmtu == NULL && !(ipp->ipp_fields & IPPF_PATHMTU)); 29955 29956 if (ipp->ipp_fields & IPPF_HOPOPTS) { 29957 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 29958 ipp->ipp_hopopts = NULL; 29959 ipp->ipp_hopoptslen = 0; 29960 } 29961 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 29962 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 29963 ipp->ipp_rtdstopts = NULL; 29964 ipp->ipp_rtdstoptslen = 0; 29965 } 29966 if (ipp->ipp_fields & IPPF_DSTOPTS) { 29967 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 29968 ipp->ipp_dstopts = NULL; 29969 ipp->ipp_dstoptslen = 0; 29970 } 29971 if (ipp->ipp_fields & IPPF_RTHDR) { 29972 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 29973 ipp->ipp_rthdr = NULL; 29974 ipp->ipp_rthdrlen = 0; 29975 } 29976 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 29977 IPPF_RTHDR); 29978 } 29979 29980 zoneid_t 29981 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_stack_t *ipst, 29982 zoneid_t lookup_zoneid) 29983 { 29984 ire_t *ire; 29985 int ire_flags = MATCH_IRE_TYPE; 29986 zoneid_t zoneid = ALL_ZONES; 29987 29988 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) 29989 return (ALL_ZONES); 29990 29991 if (lookup_zoneid != ALL_ZONES) 29992 ire_flags |= MATCH_IRE_ZONEONLY; 29993 ire = ire_ctable_lookup(addr, NULL, IRE_LOCAL | IRE_LOOPBACK, NULL, 29994 lookup_zoneid, NULL, ire_flags, ipst); 29995 if (ire != NULL) { 29996 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst); 29997 ire_refrele(ire); 29998 } 29999 return (zoneid); 30000 } 30001 30002 zoneid_t 30003 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill, 30004 ip_stack_t *ipst, zoneid_t lookup_zoneid) 30005 { 30006 ire_t *ire; 30007 int ire_flags = MATCH_IRE_TYPE; 30008 zoneid_t zoneid = ALL_ZONES; 30009 ipif_t *ipif_arg = NULL; 30010 30011 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) 30012 return (ALL_ZONES); 30013 30014 if (IN6_IS_ADDR_LINKLOCAL(addr)) { 30015 ire_flags |= MATCH_IRE_ILL; 30016 ipif_arg = ill->ill_ipif; 30017 } 30018 if (lookup_zoneid != ALL_ZONES) 30019 ire_flags |= MATCH_IRE_ZONEONLY; 30020 ire = ire_ctable_lookup_v6(addr, NULL, IRE_LOCAL | IRE_LOOPBACK, 30021 ipif_arg, lookup_zoneid, NULL, ire_flags, ipst); 30022 if (ire != NULL) { 30023 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst); 30024 ire_refrele(ire); 30025 } 30026 return (zoneid); 30027 } 30028 30029 /* 30030 * IP obserability hook support functions. 30031 */ 30032 30033 static void 30034 ipobs_init(ip_stack_t *ipst) 30035 { 30036 ipst->ips_ipobs_enabled = B_FALSE; 30037 list_create(&ipst->ips_ipobs_cb_list, sizeof (ipobs_cb_t), 30038 offsetof(ipobs_cb_t, ipobs_cbnext)); 30039 mutex_init(&ipst->ips_ipobs_cb_lock, NULL, MUTEX_DEFAULT, NULL); 30040 ipst->ips_ipobs_cb_nwalkers = 0; 30041 cv_init(&ipst->ips_ipobs_cb_cv, NULL, CV_DRIVER, NULL); 30042 } 30043 30044 static void 30045 ipobs_fini(ip_stack_t *ipst) 30046 { 30047 ipobs_cb_t *cb; 30048 30049 mutex_enter(&ipst->ips_ipobs_cb_lock); 30050 while (ipst->ips_ipobs_cb_nwalkers != 0) 30051 cv_wait(&ipst->ips_ipobs_cb_cv, &ipst->ips_ipobs_cb_lock); 30052 30053 while ((cb = list_head(&ipst->ips_ipobs_cb_list)) != NULL) { 30054 list_remove(&ipst->ips_ipobs_cb_list, cb); 30055 kmem_free(cb, sizeof (*cb)); 30056 } 30057 list_destroy(&ipst->ips_ipobs_cb_list); 30058 mutex_exit(&ipst->ips_ipobs_cb_lock); 30059 mutex_destroy(&ipst->ips_ipobs_cb_lock); 30060 cv_destroy(&ipst->ips_ipobs_cb_cv); 30061 } 30062 30063 void 30064 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst, 30065 const ill_t *ill, int ipver, uint32_t hlen, ip_stack_t *ipst) 30066 { 30067 mblk_t *mp2; 30068 ipobs_cb_t *ipobs_cb; 30069 ipobs_hook_data_t *ihd; 30070 uint64_t grifindex = 0; 30071 30072 ASSERT(DB_TYPE(mp) == M_DATA); 30073 30074 if (IS_UNDER_IPMP(ill)) 30075 grifindex = ipmp_ill_get_ipmp_ifindex(ill); 30076 30077 mutex_enter(&ipst->ips_ipobs_cb_lock); 30078 ipst->ips_ipobs_cb_nwalkers++; 30079 mutex_exit(&ipst->ips_ipobs_cb_lock); 30080 for (ipobs_cb = list_head(&ipst->ips_ipobs_cb_list); ipobs_cb != NULL; 30081 ipobs_cb = list_next(&ipst->ips_ipobs_cb_list, ipobs_cb)) { 30082 mp2 = allocb(sizeof (ipobs_hook_data_t), BPRI_HI); 30083 if (mp2 != NULL) { 30084 ihd = (ipobs_hook_data_t *)mp2->b_rptr; 30085 if (((ihd->ihd_mp = dupmsg(mp)) == NULL) && 30086 ((ihd->ihd_mp = copymsg(mp)) == NULL)) { 30087 freemsg(mp2); 30088 continue; 30089 } 30090 ihd->ihd_mp->b_rptr += hlen; 30091 ihd->ihd_htype = htype; 30092 ihd->ihd_ipver = ipver; 30093 ihd->ihd_zsrc = zsrc; 30094 ihd->ihd_zdst = zdst; 30095 ihd->ihd_ifindex = ill->ill_phyint->phyint_ifindex; 30096 ihd->ihd_grifindex = grifindex; 30097 ihd->ihd_stack = ipst->ips_netstack; 30098 mp2->b_wptr += sizeof (*ihd); 30099 ipobs_cb->ipobs_cbfunc(mp2); 30100 } 30101 } 30102 mutex_enter(&ipst->ips_ipobs_cb_lock); 30103 ipst->ips_ipobs_cb_nwalkers--; 30104 if (ipst->ips_ipobs_cb_nwalkers == 0) 30105 cv_broadcast(&ipst->ips_ipobs_cb_cv); 30106 mutex_exit(&ipst->ips_ipobs_cb_lock); 30107 } 30108 30109 void 30110 ipobs_register_hook(netstack_t *ns, pfv_t func) 30111 { 30112 ipobs_cb_t *cb; 30113 ip_stack_t *ipst = ns->netstack_ip; 30114 30115 cb = kmem_alloc(sizeof (*cb), KM_SLEEP); 30116 30117 mutex_enter(&ipst->ips_ipobs_cb_lock); 30118 while (ipst->ips_ipobs_cb_nwalkers != 0) 30119 cv_wait(&ipst->ips_ipobs_cb_cv, &ipst->ips_ipobs_cb_lock); 30120 ASSERT(ipst->ips_ipobs_cb_nwalkers == 0); 30121 30122 cb->ipobs_cbfunc = func; 30123 list_insert_head(&ipst->ips_ipobs_cb_list, cb); 30124 ipst->ips_ipobs_enabled = B_TRUE; 30125 mutex_exit(&ipst->ips_ipobs_cb_lock); 30126 } 30127 30128 void 30129 ipobs_unregister_hook(netstack_t *ns, pfv_t func) 30130 { 30131 ipobs_cb_t *curcb; 30132 ip_stack_t *ipst = ns->netstack_ip; 30133 30134 mutex_enter(&ipst->ips_ipobs_cb_lock); 30135 while (ipst->ips_ipobs_cb_nwalkers != 0) 30136 cv_wait(&ipst->ips_ipobs_cb_cv, &ipst->ips_ipobs_cb_lock); 30137 30138 for (curcb = list_head(&ipst->ips_ipobs_cb_list); curcb != NULL; 30139 curcb = list_next(&ipst->ips_ipobs_cb_list, curcb)) { 30140 if (func == curcb->ipobs_cbfunc) { 30141 list_remove(&ipst->ips_ipobs_cb_list, curcb); 30142 kmem_free(curcb, sizeof (*curcb)); 30143 break; 30144 } 30145 } 30146 if (list_is_empty(&ipst->ips_ipobs_cb_list)) 30147 ipst->ips_ipobs_enabled = B_FALSE; 30148 mutex_exit(&ipst->ips_ipobs_cb_lock); 30149 } 30150