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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* Copyright (c) 1990 Mentat Inc. */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/stream.h> 31 #include <sys/dlpi.h> 32 #include <sys/stropts.h> 33 #include <sys/sysmacros.h> 34 #include <sys/strsubr.h> 35 #include <sys/strlog.h> 36 #include <sys/strsun.h> 37 #include <sys/zone.h> 38 #define _SUN_TPI_VERSION 2 39 #include <sys/tihdr.h> 40 #include <sys/xti_inet.h> 41 #include <sys/ddi.h> 42 #include <sys/sunddi.h> 43 #include <sys/cmn_err.h> 44 #include <sys/debug.h> 45 #include <sys/kobj.h> 46 #include <sys/modctl.h> 47 #include <sys/atomic.h> 48 #include <sys/policy.h> 49 #include <sys/priv.h> 50 51 #include <sys/systm.h> 52 #include <sys/param.h> 53 #include <sys/kmem.h> 54 #include <sys/sdt.h> 55 #include <sys/socket.h> 56 #include <sys/vtrace.h> 57 #include <sys/isa_defs.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/kstatcom.h> 72 73 #include <netinet/igmp_var.h> 74 #include <netinet/ip6.h> 75 #include <netinet/icmp6.h> 76 #include <netinet/sctp.h> 77 78 #include <inet/ip.h> 79 #include <inet/ip_impl.h> 80 #include <inet/ip6.h> 81 #include <inet/ip6_asp.h> 82 #include <inet/tcp.h> 83 #include <inet/tcp_impl.h> 84 #include <inet/ip_multi.h> 85 #include <inet/ip_if.h> 86 #include <inet/ip_ire.h> 87 #include <inet/ip_ftable.h> 88 #include <inet/ip_rts.h> 89 #include <inet/optcom.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 <sys/sunddi.h> 121 122 #include <sys/tsol/label.h> 123 #include <sys/tsol/tnet.h> 124 125 #include <rpc/pmap_prot.h> 126 127 /* 128 * Values for squeue switch: 129 * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain 130 * IP_SQUEUE_ENTER: squeue_enter 131 * IP_SQUEUE_FILL: squeue_fill 132 */ 133 int ip_squeue_enter = 2; 134 squeue_func_t ip_input_proc; 135 /* 136 * IP statistics. 137 */ 138 #define IP_STAT(x) (ip_statistics.x.value.ui64++) 139 #define IP_STAT_UPDATE(x, n) (ip_statistics.x.value.ui64 += (n)) 140 #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x)) 141 142 typedef struct ip_stat { 143 kstat_named_t ipsec_fanout_proto; 144 kstat_named_t ip_udp_fannorm; 145 kstat_named_t ip_udp_fanmb; 146 kstat_named_t ip_udp_fanothers; 147 kstat_named_t ip_udp_fast_path; 148 kstat_named_t ip_udp_slow_path; 149 kstat_named_t ip_udp_input_err; 150 kstat_named_t ip_tcppullup; 151 kstat_named_t ip_tcpoptions; 152 kstat_named_t ip_multipkttcp; 153 kstat_named_t ip_tcp_fast_path; 154 kstat_named_t ip_tcp_slow_path; 155 kstat_named_t ip_tcp_input_error; 156 kstat_named_t ip_db_ref; 157 kstat_named_t ip_notaligned1; 158 kstat_named_t ip_notaligned2; 159 kstat_named_t ip_multimblk3; 160 kstat_named_t ip_multimblk4; 161 kstat_named_t ip_ipoptions; 162 kstat_named_t ip_classify_fail; 163 kstat_named_t ip_opt; 164 kstat_named_t ip_udp_rput_local; 165 kstat_named_t ipsec_proto_ahesp; 166 kstat_named_t ip_conn_flputbq; 167 kstat_named_t ip_conn_walk_drain; 168 kstat_named_t ip_out_sw_cksum; 169 kstat_named_t ip_in_sw_cksum; 170 kstat_named_t ip_trash_ire_reclaim_calls; 171 kstat_named_t ip_trash_ire_reclaim_success; 172 kstat_named_t ip_ire_arp_timer_expired; 173 kstat_named_t ip_ire_redirect_timer_expired; 174 kstat_named_t ip_ire_pmtu_timer_expired; 175 kstat_named_t ip_input_multi_squeue; 176 kstat_named_t ip_tcp_in_full_hw_cksum_err; 177 kstat_named_t ip_tcp_in_part_hw_cksum_err; 178 kstat_named_t ip_tcp_in_sw_cksum_err; 179 kstat_named_t ip_tcp_out_sw_cksum_bytes; 180 kstat_named_t ip_udp_in_full_hw_cksum_err; 181 kstat_named_t ip_udp_in_part_hw_cksum_err; 182 kstat_named_t ip_udp_in_sw_cksum_err; 183 kstat_named_t ip_udp_out_sw_cksum_bytes; 184 kstat_named_t ip_frag_mdt_pkt_out; 185 kstat_named_t ip_frag_mdt_discarded; 186 kstat_named_t ip_frag_mdt_allocfail; 187 kstat_named_t ip_frag_mdt_addpdescfail; 188 kstat_named_t ip_frag_mdt_allocd; 189 } ip_stat_t; 190 191 static ip_stat_t ip_statistics = { 192 { "ipsec_fanout_proto", KSTAT_DATA_UINT64 }, 193 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 194 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 195 { "ip_udp_fanothers", KSTAT_DATA_UINT64 }, 196 { "ip_udp_fast_path", KSTAT_DATA_UINT64 }, 197 { "ip_udp_slow_path", KSTAT_DATA_UINT64 }, 198 { "ip_udp_input_err", KSTAT_DATA_UINT64 }, 199 { "ip_tcppullup", KSTAT_DATA_UINT64 }, 200 { "ip_tcpoptions", KSTAT_DATA_UINT64 }, 201 { "ip_multipkttcp", KSTAT_DATA_UINT64 }, 202 { "ip_tcp_fast_path", KSTAT_DATA_UINT64 }, 203 { "ip_tcp_slow_path", KSTAT_DATA_UINT64 }, 204 { "ip_tcp_input_error", KSTAT_DATA_UINT64 }, 205 { "ip_db_ref", KSTAT_DATA_UINT64 }, 206 { "ip_notaligned1", KSTAT_DATA_UINT64 }, 207 { "ip_notaligned2", KSTAT_DATA_UINT64 }, 208 { "ip_multimblk3", KSTAT_DATA_UINT64 }, 209 { "ip_multimblk4", KSTAT_DATA_UINT64 }, 210 { "ip_ipoptions", KSTAT_DATA_UINT64 }, 211 { "ip_classify_fail", KSTAT_DATA_UINT64 }, 212 { "ip_opt", KSTAT_DATA_UINT64 }, 213 { "ip_udp_rput_local", KSTAT_DATA_UINT64 }, 214 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 215 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 216 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 217 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 218 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 219 { "ip_trash_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 220 { "ip_trash_ire_reclaim_success", KSTAT_DATA_UINT64 }, 221 { "ip_ire_arp_timer_expired", KSTAT_DATA_UINT64 }, 222 { "ip_ire_redirect_timer_expired", KSTAT_DATA_UINT64 }, 223 { "ip_ire_pmtu_timer_expired", KSTAT_DATA_UINT64 }, 224 { "ip_input_multi_squeue", KSTAT_DATA_UINT64 }, 225 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 226 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 227 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 228 { "ip_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 229 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 230 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 231 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 232 { "ip_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 233 { "ip_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 234 { "ip_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 235 { "ip_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 236 { "ip_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 237 { "ip_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 238 }; 239 240 static kstat_t *ip_kstat; 241 242 #define TCP6 "tcp6" 243 #define TCP "tcp" 244 #define SCTP "sctp" 245 #define SCTP6 "sctp6" 246 247 major_t TCP6_MAJ; 248 major_t TCP_MAJ; 249 major_t SCTP_MAJ; 250 major_t SCTP6_MAJ; 251 252 int ip_poll_normal_ms = 100; 253 int ip_poll_normal_ticks = 0; 254 255 /* 256 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 257 */ 258 259 struct listptr_s { 260 mblk_t *lp_head; /* pointer to the head of the list */ 261 mblk_t *lp_tail; /* pointer to the tail of the list */ 262 }; 263 264 typedef struct listptr_s listptr_t; 265 266 /* 267 * This is used by ip_snmp_get_mib2_ip_route_media and 268 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 269 */ 270 typedef struct iproutedata_s { 271 uint_t ird_idx; 272 listptr_t ird_route; /* ipRouteEntryTable */ 273 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 274 listptr_t ird_attrs; /* ipRouteAttributeTable */ 275 } iproutedata_t; 276 277 /* 278 * Cluster specific hooks. These should be NULL when booted as a non-cluster 279 */ 280 281 /* 282 * Hook functions to enable cluster networking 283 * On non-clustered systems these vectors must always be NULL. 284 * 285 * Hook function to Check ip specified ip address is a shared ip address 286 * in the cluster 287 * 288 */ 289 int (*cl_inet_isclusterwide)(uint8_t protocol, 290 sa_family_t addr_family, uint8_t *laddrp) = NULL; 291 292 /* 293 * Hook function to generate cluster wide ip fragment identifier 294 */ 295 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 296 uint8_t *laddrp, uint8_t *faddrp) = NULL; 297 298 /* 299 * Synchronization notes: 300 * 301 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 302 * MT level protection given by STREAMS. IP uses a combination of its own 303 * internal serialization mechanism and standard Solaris locking techniques. 304 * The internal serialization is per phyint (no IPMP) or per IPMP group. 305 * This is used to serialize plumbing operations, IPMP operations, certain 306 * multicast operations, most set ioctls, igmp/mld timers etc. 307 * 308 * Plumbing is a long sequence of operations involving message 309 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 310 * involved in plumbing operations. A natural model is to serialize these 311 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 312 * parallel without any interference. But various set ioctls on hme0 are best 313 * serialized. However if the system uses IPMP, the operations are easier if 314 * they are serialized on a per IPMP group basis since IPMP operations 315 * happen across ill's of a group. Thus the lowest common denominator is to 316 * serialize most set ioctls, multicast join/leave operations, IPMP operations 317 * igmp/mld timer operations, and processing of DLPI control messages received 318 * from drivers on a per IPMP group basis. If the system does not employ 319 * IPMP the serialization is on a per phyint basis. This serialization is 320 * provided by the ipsq_t and primitives operating on this. Details can 321 * be found in ip_if.c above the core primitives operating on ipsq_t. 322 * 323 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 324 * Simiarly lookup of an ire by a thread also returns a refheld ire. 325 * In addition ipif's and ill's referenced by the ire are also indirectly 326 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 327 * the ipif's address or netmask change as long as an ipif is refheld 328 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the 329 * address of an ipif has to go through the ipsq_t. This ensures that only 330 * 1 such exclusive operation proceeds at any time on the ipif. It then 331 * deletes all ires associated with this ipif, and waits for all refcnts 332 * associated with this ipif to come down to zero. The address is changed 333 * only after the ipif has been quiesced. Then the ipif is brought up again. 334 * More details are described above the comment in ip_sioctl_flags. 335 * 336 * Packet processing is based mostly on IREs and are fully multi-threaded 337 * using standard Solaris MT techniques. 338 * 339 * There are explicit locks in IP to handle: 340 * - The ip_g_head list maintained by mi_open_link() and friends. 341 * 342 * - The reassembly data structures (one lock per hash bucket) 343 * 344 * - conn_lock is meant to protect conn_t fields. The fields actually 345 * protected by conn_lock are documented in the conn_t definition. 346 * 347 * - ire_lock to protect some of the fields of the ire, IRE tables 348 * (one lock per hash bucket). Refer to ip_ire.c for details. 349 * 350 * - ndp_g_lock and nce_lock for protecting NCEs. 351 * 352 * - ill_lock protects fields of the ill and ipif. Details in ip.h 353 * 354 * - ill_g_lock: This is a global reader/writer lock. Protects the following 355 * * The AVL tree based global multi list of all ills. 356 * * The linked list of all ipifs of an ill 357 * * The <ill-ipsq> mapping 358 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next 359 * * The illgroup list threaded by ill_group_next. 360 * * <ill-phyint> association 361 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 362 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion 363 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill 364 * will all have to hold the ill_g_lock as writer for the actual duration 365 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping 366 * may be found in the IPMP section. 367 * 368 * - ill_lock: This is a per ill mutex. 369 * It protects some members of the ill and is documented below. 370 * It also protects the <ill-ipsq> mapping 371 * It also protects the illgroup list threaded by ill_group_next. 372 * It also protects the <ill-phyint> assoc. 373 * It also protects the list of ipifs hanging off the ill. 374 * 375 * - ipsq_lock: This is a per ipsq_t mutex lock. 376 * This protects all the other members of the ipsq struct except 377 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock 378 * 379 * - illgrp_lock: This is a per ill_group mutex lock. 380 * The only thing it protects is the illgrp_ill_schednext member of ill_group 381 * which dictates which is the next ill in an ill_group that is to be chosen 382 * for sending outgoing packets, through creation of an IRE_CACHE that 383 * references this ill. 384 * 385 * - phyint_lock: This is a per phyint mutex lock. Protects just the 386 * phyint_flags 387 * 388 * - ip_g_nd_lock: This is a global reader/writer lock. 389 * Any call to nd_load to load a new parameter to the ND table must hold the 390 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 391 * as reader. 392 * 393 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 394 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 395 * uniqueness check also done atomically. 396 * 397 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 398 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 399 * as a writer when adding or deleting elements from these lists, and 400 * as a reader when walking these lists to send a SADB update to the 401 * IPsec capable ills. 402 * 403 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 404 * group list linked by ill_usesrc_grp_next. It also protects the 405 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 406 * group is being added or deleted. This lock is taken as a reader when 407 * walking the list/group(eg: to get the number of members in a usesrc group). 408 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 409 * field is changing state i.e from NULL to non-NULL or vice-versa. For 410 * example, it is not necessary to take this lock in the initial portion 411 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and 412 * ip_sioctl_flags since the these operations are executed exclusively and 413 * that ensures that the "usesrc group state" cannot change. The "usesrc 414 * group state" change can happen only in the latter part of 415 * ip_sioctl_slifusesrc and in ill_delete. 416 * 417 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications. 418 * 419 * To change the <ill-phyint> association, the ill_g_lock must be held 420 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 421 * must be held. 422 * 423 * To change the <ill-ipsq> association the ill_g_lock must be held as writer 424 * and the ill_lock of the ill in question must be held. 425 * 426 * To change the <ill-illgroup> association the ill_g_lock must be held as 427 * writer and the ill_lock of the ill in question must be held. 428 * 429 * To add or delete an ipif from the list of ipifs hanging off the ill, 430 * ill_g_lock (writer) and ill_lock must be held and the thread must be 431 * a writer on the associated ipsq,. 432 * 433 * To add or delete an ill to the system, the ill_g_lock must be held as 434 * writer and the thread must be a writer on the associated ipsq. 435 * 436 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 437 * must be a writer on the associated ipsq. 438 * 439 * Lock hierarchy 440 * 441 * Some lock hierarchy scenarios are listed below. 442 * 443 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 444 * ill_g_lock -> illgrp_lock -> ill_lock 445 * ill_g_lock -> ill_lock(s) -> phyint_lock 446 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 447 * ill_g_lock -> ip_addr_avail_lock 448 * conn_lock -> irb_lock -> ill_lock -> ire_lock 449 * ill_g_lock -> ip_g_nd_lock 450 * 451 * When more than 1 ill lock is needed to be held, all ill lock addresses 452 * are sorted on address and locked starting from highest addressed lock 453 * downward. 454 * 455 * Mobile-IP scenarios 456 * 457 * irb_lock -> ill_lock -> ire_mrtun_lock 458 * irb_lock -> ill_lock -> ire_srcif_table_lock 459 * 460 * IPsec scenarios 461 * 462 * ipsa_lock -> ill_g_lock -> ill_lock 463 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 464 * ipsec_capab_ills_lock -> ipsa_lock 465 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 466 * 467 * Trusted Solaris scenarios 468 * 469 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 470 * igsa_lock -> gcdb_lock 471 * gcgrp_rwlock -> ire_lock 472 * gcgrp_rwlock -> gcdb_lock 473 * 474 * 475 * Routing/forwarding table locking notes: 476 * 477 * Lock acquisition order: Radix tree lock, irb_lock. 478 * Requirements: 479 * i. Walker must not hold any locks during the walker callback. 480 * ii Walker must not see a truncated tree during the walk because of any node 481 * deletion. 482 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 483 * in many places in the code to walk the irb list. Thus even if all the 484 * ires in a bucket have been deleted, we still can't free the radix node 485 * until the ires have actually been inactive'd (freed). 486 * 487 * Tree traversal - Need to hold the global tree lock in read mode. 488 * Before dropping the global tree lock, need to either increment the ire_refcnt 489 * to ensure that the radix node can't be deleted. 490 * 491 * Tree add - Need to hold the global tree lock in write mode to add a 492 * radix node. To prevent the node from being deleted, increment the 493 * irb_refcnt, after the node is added to the tree. The ire itself is 494 * added later while holding the irb_lock, but not the tree lock. 495 * 496 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 497 * All associated ires must be inactive (i.e. freed), and irb_refcnt 498 * must be zero. 499 * 500 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 501 * global tree lock (read mode) for traversal. 502 * 503 * IPSEC notes : 504 * 505 * IP interacts with the IPSEC code (AH/ESP) by tagging a M_CTL message 506 * in front of the actual packet. For outbound datagrams, the M_CTL 507 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 508 * information used by the IPSEC code for applying the right level of 509 * protection. The information initialized by IP in the ipsec_out_t 510 * is determined by the per-socket policy or global policy in the system. 511 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 512 * ipsec_info.h) which starts out with nothing in it. It gets filled 513 * with the right information if it goes through the AH/ESP code, which 514 * happens if the incoming packet is secure. The information initialized 515 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 516 * the policy requirements needed by per-socket policy or global policy 517 * is met or not. 518 * 519 * If there is both per-socket policy (set using setsockopt) and there 520 * is also global policy match for the 5 tuples of the socket, 521 * ipsec_override_policy() makes the decision of which one to use. 522 * 523 * For fully connected sockets i.e dst, src [addr, port] is known, 524 * conn_policy_cached is set indicating that policy has been cached. 525 * conn_in_enforce_policy may or may not be set depending on whether 526 * there is a global policy match or per-socket policy match. 527 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 528 * Once the right policy is set on the conn_t, policy cannot change for 529 * this socket. This makes life simpler for TCP (UDP ?) where 530 * re-transmissions go out with the same policy. For symmetry, policy 531 * is cached for fully connected UDP sockets also. Thus if policy is cached, 532 * it also implies that policy is latched i.e policy cannot change 533 * on these sockets. As we have the right policy on the conn, we don't 534 * have to lookup global policy for every outbound and inbound datagram 535 * and thus serving as an optimization. Note that a global policy change 536 * does not affect fully connected sockets if they have policy. If fully 537 * connected sockets did not have any policy associated with it, global 538 * policy change may affect them. 539 * 540 * IP Flow control notes: 541 * 542 * Non-TCP streams are flow controlled by IP. On the send side, if the packet 543 * cannot be sent down to the driver by IP, because of a canput failure, IP 544 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. 545 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained 546 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the 547 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the 548 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains 549 * the queued messages, and removes the conn from the drain list, if all 550 * messages were drained. It also qenables the next conn in the drain list to 551 * continue the drain process. 552 * 553 * In reality the drain list is not a single list, but a configurable number 554 * of lists. The ip_wsrv on the IP module, qenables the first conn in each 555 * list. If the ip_wsrv of the next qenabled conn does not run, because the 556 * stream closes, ip_close takes responsibility to qenable the next conn in 557 * the drain list. The directly called ip_wput path always does a putq, if 558 * it cannot putnext. Thus synchronization problems are handled between 559 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 560 * functions that manipulate this drain list. Furthermore conn_drain_insert 561 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv 562 * running on a queue at any time. conn_drain_tail can be simultaneously called 563 * from both ip_wsrv and ip_close. 564 * 565 * IPQOS notes: 566 * 567 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 568 * and IPQoS modules. IPPF includes hooks in IP at different control points 569 * (callout positions) which direct packets to IPQoS modules for policy 570 * processing. Policies, if present, are global. 571 * 572 * The callout positions are located in the following paths: 573 * o local_in (packets destined for this host) 574 * o local_out (packets orginating from this host ) 575 * o fwd_in (packets forwarded by this m/c - inbound) 576 * o fwd_out (packets forwarded by this m/c - outbound) 577 * Hooks at these callout points can be enabled/disabled using the ndd variable 578 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 579 * By default all the callout positions are enabled. 580 * 581 * Outbound (local_out) 582 * Hooks are placed in ip_wput_ire and ipsec_out_process. 583 * 584 * Inbound (local_in) 585 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 586 * TCP and UDP fanout routines. 587 * 588 * Forwarding (in and out) 589 * Hooks are placed in ip_rput_forward and ip_mrtun_forward. 590 * 591 * IP Policy Framework processing (IPPF processing) 592 * Policy processing for a packet is initiated by ip_process, which ascertains 593 * that the classifier (ipgpc) is loaded and configured, failing which the 594 * packet resumes normal processing in IP. If the clasifier is present, the 595 * packet is acted upon by one or more IPQoS modules (action instances), per 596 * filters configured in ipgpc and resumes normal IP processing thereafter. 597 * An action instance can drop a packet in course of its processing. 598 * 599 * A boolean variable, ip_policy, is used in all the fanout routines that can 600 * invoke ip_process for a packet. This variable indicates if the packet should 601 * to be sent for policy processing. The variable is set to B_TRUE by default, 602 * i.e. when the routines are invoked in the normal ip procesing path for a 603 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 604 * ip_policy is set to B_FALSE for all the routines called in these two 605 * functions because, in the former case, we don't process loopback traffic 606 * currently while in the latter, the packets have already been processed in 607 * icmp_inbound. 608 * 609 * Zones notes: 610 * 611 * The partitioning rules for networking are as follows: 612 * 1) Packets coming from a zone must have a source address belonging to that 613 * zone. 614 * 2) Packets coming from a zone can only be sent on a physical interface on 615 * which the zone has an IP address. 616 * 3) Between two zones on the same machine, packet delivery is only allowed if 617 * there's a matching route for the destination and zone in the forwarding 618 * table. 619 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 620 * different zones can bind to the same port with the wildcard address 621 * (INADDR_ANY). 622 * 623 * The granularity of interface partitioning is at the logical interface level. 624 * Therefore, every zone has its own IP addresses, and incoming packets can be 625 * attributed to a zone unambiguously. A logical interface is placed into a zone 626 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 627 * structure. Rule (1) is implemented by modifying the source address selection 628 * algorithm so that the list of eligible addresses is filtered based on the 629 * sending process zone. 630 * 631 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 632 * across all zones, depending on their type. Here is the break-up: 633 * 634 * IRE type Shared/exclusive 635 * -------- ---------------- 636 * IRE_BROADCAST Exclusive 637 * IRE_DEFAULT (default routes) Shared (*) 638 * IRE_LOCAL Exclusive (x) 639 * IRE_LOOPBACK Exclusive 640 * IRE_PREFIX (net routes) Shared (*) 641 * IRE_CACHE Exclusive 642 * IRE_IF_NORESOLVER (interface routes) Exclusive 643 * IRE_IF_RESOLVER (interface routes) Exclusive 644 * IRE_HOST (host routes) Shared (*) 645 * 646 * (*) A zone can only use a default or off-subnet route if the gateway is 647 * directly reachable from the zone, that is, if the gateway's address matches 648 * one of the zone's logical interfaces. 649 * 650 * (x) IRE_LOCAL are handled a bit differently, since for all other entries 651 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source 652 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP 653 * address of the zone itself (the destination). Since IRE_LOCAL is used 654 * for communication between zones, ip_wput_ire has special logic to set 655 * the right source address when sending using an IRE_LOCAL. 656 * 657 * Furthermore, when ip_restrict_interzone_loopback is set (the default), 658 * ire_cache_lookup restricts loopback using an IRE_LOCAL 659 * between zone to the case when L2 would have conceptually looped the packet 660 * back, i.e. the loopback which is required since neither Ethernet drivers 661 * nor Ethernet hardware loops them back. This is the case when the normal 662 * routes (ignoring IREs with different zoneids) would send out the packet on 663 * the same ill (or ill group) as the ill with which is IRE_LOCAL is 664 * associated. 665 * 666 * Multiple zones can share a common broadcast address; typically all zones 667 * share the 255.255.255.255 address. Incoming as well as locally originated 668 * broadcast packets must be dispatched to all the zones on the broadcast 669 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 670 * since some zones may not be on the 10.16.72/24 network. To handle this, each 671 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 672 * sent to every zone that has an IRE_BROADCAST entry for the destination 673 * address on the input ill, see conn_wantpacket(). 674 * 675 * Applications in different zones can join the same multicast group address. 676 * For IPv4, group memberships are per-logical interface, so they're already 677 * inherently part of a zone. For IPv6, group memberships are per-physical 678 * interface, so we distinguish IPv6 group memberships based on group address, 679 * interface and zoneid. In both cases, received multicast packets are sent to 680 * every zone for which a group membership entry exists. On IPv6 we need to 681 * check that the target zone still has an address on the receiving physical 682 * interface; it could have been removed since the application issued the 683 * IPV6_JOIN_GROUP. 684 */ 685 686 /* 687 * Squeue Fanout flags: 688 * 0: No fanout. 689 * 1: Fanout across all squeues 690 */ 691 boolean_t ip_squeue_fanout = 0; 692 693 /* 694 * Maximum dups allowed per packet. 695 */ 696 uint_t ip_max_frag_dups = 10; 697 698 #define IS_SIMPLE_IPH(ipha) \ 699 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 700 701 /* RFC1122 Conformance */ 702 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 703 704 #define ILL_MAX_NAMELEN LIFNAMSIZ 705 706 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 707 708 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t); 709 static void ip_ipsec_out_prepend(mblk_t *, mblk_t *, ill_t *); 710 711 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t); 712 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 713 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 714 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); 715 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, 716 mblk_t *, int); 717 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 718 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 719 ill_t *, zoneid_t); 720 static void icmp_options_update(ipha_t *); 721 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t); 722 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, 723 zoneid_t zoneid); 724 static mblk_t *icmp_pkt_err_ok(mblk_t *); 725 static void icmp_redirect(mblk_t *); 726 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t); 727 728 static void ip_arp_news(queue_t *, mblk_t *); 729 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *); 730 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 731 char *ip_dot_addr(ipaddr_t, char *); 732 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 733 int ip_close(queue_t *, int); 734 static char *ip_dot_saddr(uchar_t *, char *); 735 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 736 boolean_t, boolean_t, ill_t *, zoneid_t); 737 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 738 boolean_t, boolean_t, zoneid_t); 739 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 740 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 741 static void ip_lrput(queue_t *, mblk_t *); 742 ipaddr_t ip_massage_options(ipha_t *); 743 static void ip_mrtun_forward(ire_t *, ill_t *, mblk_t *); 744 ipaddr_t ip_net_mask(ipaddr_t); 745 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, ill_t *, conn_t *, 746 zoneid_t); 747 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 748 conn_t *, uint32_t, zoneid_t); 749 char *ip_nv_lookup(nv_t *, int); 750 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 751 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 752 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 753 static boolean_t ip_param_register(ipparam_t *, size_t, ipndp_t *, 754 size_t); 755 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 756 void ip_rput(queue_t *, mblk_t *); 757 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 758 void *dummy_arg); 759 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 760 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *); 761 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 762 ire_t *); 763 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *); 764 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, 765 uint16_t *); 766 int ip_snmp_get(queue_t *, mblk_t *); 767 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *); 768 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *); 769 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *); 770 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *); 771 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *); 772 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *); 773 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *); 774 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *); 775 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *); 776 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *); 777 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *); 778 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *); 779 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *); 780 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *); 781 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *); 782 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *); 783 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 784 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 785 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); 786 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 787 static boolean_t ip_source_routed(ipha_t *); 788 static boolean_t ip_source_route_included(ipha_t *); 789 790 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, 791 zoneid_t); 792 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int); 793 static void ip_wput_local_options(ipha_t *); 794 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 795 zoneid_t); 796 797 static void conn_drain_init(void); 798 static void conn_drain_fini(void); 799 static void conn_drain_tail(conn_t *connp, boolean_t closing); 800 801 static void conn_walk_drain(void); 802 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, 803 zoneid_t); 804 805 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 806 zoneid_t); 807 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 808 void *dummy_arg); 809 810 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 811 812 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 813 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 814 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 815 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 816 817 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 818 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 819 caddr_t, cred_t *); 820 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, 821 caddr_t cp, cred_t *cr); 822 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, 823 cred_t *); 824 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 825 caddr_t cp, cred_t *cr); 826 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 827 cred_t *); 828 static squeue_func_t ip_squeue_switch(int); 829 830 static void ip_kstat_init(void); 831 static void ip_kstat_fini(void); 832 static int ip_kstat_update(kstat_t *kp, int rw); 833 static void icmp_kstat_init(void); 834 static void icmp_kstat_fini(void); 835 static int icmp_kstat_update(kstat_t *kp, int rw); 836 837 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); 838 839 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 840 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 841 842 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 843 ipha_t *, ill_t *, boolean_t); 844 845 timeout_id_t ip_ire_expire_id; /* IRE expiration timer. */ 846 static clock_t ip_ire_arp_time_elapsed; /* Time since IRE cache last flushed */ 847 static clock_t ip_ire_rd_time_elapsed; /* ... redirect IREs last flushed */ 848 static clock_t ip_ire_pmtu_time_elapsed; /* Time since path mtu increase */ 849 850 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 851 clock_t icmp_pkt_err_last = 0; /* Time since last icmp_pkt_err */ 852 uint_t icmp_pkt_err_sent = 0; /* Number of packets sent in burst */ 853 854 /* How long, in seconds, we allow frags to hang around. */ 855 #define IP_FRAG_TIMEOUT 60 856 857 time_t ip_g_frag_timeout = IP_FRAG_TIMEOUT; 858 clock_t ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 859 860 /* 861 * Threshold which determines whether MDT should be used when 862 * generating IP fragments; payload size must be greater than 863 * this threshold for MDT to take place. 864 */ 865 #define IP_WPUT_FRAG_MDT_MIN 32768 866 867 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 868 869 /* Protected by ip_mi_lock */ 870 static void *ip_g_head; /* Instance Data List Head */ 871 kmutex_t ip_mi_lock; /* Lock for list of instances */ 872 873 /* Only modified during _init and _fini thus no locking is needed. */ 874 caddr_t ip_g_nd; /* Named Dispatch List Head */ 875 876 877 static long ip_rput_pullups; 878 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 879 880 vmem_t *ip_minor_arena; 881 882 /* 883 * MIB-2 stuff for SNMP (both IP and ICMP) 884 */ 885 mib2_ip_t ip_mib; 886 mib2_icmp_t icmp_mib; 887 888 #ifdef DEBUG 889 uint32_t ipsechw_debug = 0; 890 #endif 891 892 kstat_t *ip_mibkp; /* kstat exporting ip_mib data */ 893 kstat_t *icmp_mibkp; /* kstat exporting icmp_mib data */ 894 895 uint_t loopback_packets = 0; 896 897 /* 898 * Multirouting/CGTP stuff 899 */ 900 cgtp_filter_ops_t *ip_cgtp_filter_ops; /* CGTP hooks */ 901 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 902 boolean_t ip_cgtp_filter; /* Enable/disable CGTP hooks */ 903 /* Interval (in ms) between consecutive 'bad MTU' warnings */ 904 hrtime_t ip_multirt_log_interval = 1000; 905 /* Time since last warning issued. */ 906 static hrtime_t multirt_bad_mtu_last_time = 0; 907 908 kmutex_t ip_trash_timer_lock; 909 krwlock_t ip_g_nd_lock; 910 911 /* 912 * XXX following really should only be in a header. Would need more 913 * header and .c clean up first. 914 */ 915 extern optdb_obj_t ip_opt_obj; 916 917 ulong_t ip_squeue_enter_unbound = 0; 918 919 /* 920 * Named Dispatch Parameter Table. 921 * All of these are alterable, within the min/max values given, at run time. 922 */ 923 static ipparam_t lcl_param_arr[] = { 924 /* min max value name */ 925 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 926 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 927 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 928 { 0, 1, 0, "ip_respond_to_timestamp"}, 929 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 930 { 0, 1, 1, "ip_send_redirects"}, 931 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 932 { 0, 10, 0, "ip_debug"}, 933 { 0, 10, 0, "ip_mrtdebug"}, 934 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 935 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 936 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 937 { 1, 255, 255, "ip_def_ttl" }, 938 { 0, 1, 0, "ip_forward_src_routed"}, 939 { 0, 256, 32, "ip_wroff_extra" }, 940 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 941 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 942 { 0, 1, 1, "ip_path_mtu_discovery" }, 943 { 0, 240, 30, "ip_ignore_delete_time" }, 944 { 0, 1, 0, "ip_ignore_redirect" }, 945 { 0, 1, 1, "ip_output_queue" }, 946 { 1, 254, 1, "ip_broadcast_ttl" }, 947 { 0, 99999, 100, "ip_icmp_err_interval" }, 948 { 1, 99999, 10, "ip_icmp_err_burst" }, 949 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 950 { 0, 1, 0, "ip_strict_dst_multihoming" }, 951 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 952 { 0, 1, 0, "ipsec_override_persocket_policy" }, 953 { 0, 1, 1, "icmp_accept_clear_messages" }, 954 { 0, 1, 1, "igmp_accept_clear_messages" }, 955 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 956 "ip_ndp_delay_first_probe_time"}, 957 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 958 "ip_ndp_max_unicast_solicit"}, 959 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 960 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 961 { 0, 1, 0, "ip6_forward_src_routed"}, 962 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 963 { 0, 1, 1, "ip6_send_redirects"}, 964 { 0, 1, 0, "ip6_ignore_redirect" }, 965 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 966 967 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 968 969 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 970 971 { 0, 1, 1, "pim_accept_clear_messages" }, 972 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 973 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 974 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 975 { 0, 15, 0, "ip_policy_mask" }, 976 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 977 { 0, 255, 1, "ip_multirt_ttl" }, 978 { 0, 1, 1, "ip_multidata_outbound" }, 979 { 0, 3600000, 300000, "ip_ndp_defense_interval" }, 980 { 0, 999999, 60*60*24, "ip_max_temp_idle" }, 981 { 0, 1000, 1, "ip_max_temp_defend" }, 982 { 0, 1000, 3, "ip_max_defend" }, 983 { 0, 999999, 30, "ip_defend_interval" }, 984 { 0, 3600000, 300000, "ip_dup_recovery" }, 985 { 0, 1, 1, "ip_restrict_interzone_loopback" }, 986 #ifdef DEBUG 987 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 988 #endif 989 }; 990 991 ipparam_t *ip_param_arr = lcl_param_arr; 992 993 /* Extended NDP table */ 994 static ipndp_t lcl_ndp_arr[] = { 995 /* getf setf data name */ 996 { ip_param_generic_get, ip_forward_set, (caddr_t)&ip_g_forward, 997 "ip_forwarding" }, 998 { ip_param_generic_get, ip_forward_set, (caddr_t)&ipv6_forward, 999 "ip6_forwarding" }, 1000 { ip_ill_report, NULL, NULL, 1001 "ip_ill_status" }, 1002 { ip_ipif_report, NULL, NULL, 1003 "ip_ipif_status" }, 1004 { ip_ire_report, NULL, NULL, 1005 "ipv4_ire_status" }, 1006 { ip_ire_report_mrtun, NULL, NULL, 1007 "ipv4_mrtun_ire_status" }, 1008 { ip_ire_report_srcif, NULL, NULL, 1009 "ipv4_srcif_ire_status" }, 1010 { ip_ire_report_v6, NULL, NULL, 1011 "ipv6_ire_status" }, 1012 { ip_conn_report, NULL, NULL, 1013 "ip_conn_status" }, 1014 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, 1015 "ip_rput_pullups" }, 1016 { ndp_report, NULL, NULL, 1017 "ip_ndp_cache_report" }, 1018 { ip_srcid_report, NULL, NULL, 1019 "ip_srcid_status" }, 1020 { ip_param_generic_get, ip_squeue_profile_set, 1021 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, 1022 { ip_param_generic_get, ip_squeue_bind_set, 1023 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, 1024 { ip_param_generic_get, ip_input_proc_set, 1025 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 1026 { ip_param_generic_get, ip_int_set, 1027 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 1028 { ip_cgtp_filter_get, ip_cgtp_filter_set, (caddr_t)&ip_cgtp_filter, 1029 "ip_cgtp_filter" }, 1030 { ip_param_generic_get, ip_int_set, 1031 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" } 1032 }; 1033 1034 /* 1035 * ip_g_forward controls IP forwarding. It takes two values: 1036 * 0: IP_FORWARD_NEVER Don't forward packets ever. 1037 * 1: IP_FORWARD_ALWAYS Forward packets for elsewhere. 1038 * 1039 * RFC1122 says there must be a configuration switch to control forwarding, 1040 * but that the default MUST be to not forward packets ever. Implicit 1041 * control based on configuration of multiple interfaces MUST NOT be 1042 * implemented (Section 3.1). SunOS 4.1 did provide the "automatic" capability 1043 * and, in fact, it was the default. That capability is now provided in the 1044 * /etc/rc2.d/S69inet script. 1045 */ 1046 int ip_g_forward = IP_FORWARD_DEFAULT; 1047 1048 /* It also has an IPv6 counterpart. */ 1049 1050 int ipv6_forward = IP_FORWARD_DEFAULT; 1051 1052 /* 1053 * Table of IP ioctls encoding the various properties of the ioctl and 1054 * indexed based on the last byte of the ioctl command. Occasionally there 1055 * is a clash, and there is more than 1 ioctl with the same last byte. 1056 * In such a case 1 ioctl is encoded in the ndx table and the remaining 1057 * ioctls are encoded in the misc table. An entry in the ndx table is 1058 * retrieved by indexing on the last byte of the ioctl command and comparing 1059 * the ioctl command with the value in the ndx table. In the event of a 1060 * mismatch the misc table is then searched sequentially for the desired 1061 * ioctl command. 1062 * 1063 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 1064 */ 1065 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 1066 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1067 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1068 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1069 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1070 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1071 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1072 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1073 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1074 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1075 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1076 1077 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 1078 MISC_CMD, ip_siocaddrt, NULL }, 1079 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 1080 MISC_CMD, ip_siocdelrt, NULL }, 1081 1082 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1083 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1084 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1085 IF_CMD, ip_sioctl_get_addr, NULL }, 1086 1087 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1088 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1089 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 1090 IPI_GET_CMD | IPI_REPL, 1091 IF_CMD, ip_sioctl_get_dstaddr, NULL }, 1092 1093 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 1094 IPI_PRIV | IPI_WR | IPI_REPL, 1095 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1096 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 1097 IPI_MODOK | IPI_GET_CMD | IPI_REPL, 1098 IF_CMD, ip_sioctl_get_flags, NULL }, 1099 1100 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1101 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1102 1103 /* copyin size cannot be coded for SIOCGIFCONF */ 1104 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1105 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1106 1107 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1108 IF_CMD, ip_sioctl_mtu, NULL }, 1109 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1110 IF_CMD, ip_sioctl_get_mtu, NULL }, 1111 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 1112 IPI_GET_CMD | IPI_REPL, 1113 IF_CMD, ip_sioctl_get_brdaddr, NULL }, 1114 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1115 IF_CMD, ip_sioctl_brdaddr, NULL }, 1116 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 1117 IPI_GET_CMD | IPI_REPL, 1118 IF_CMD, ip_sioctl_get_netmask, NULL }, 1119 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1120 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1121 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 1122 IPI_GET_CMD | IPI_REPL, 1123 IF_CMD, ip_sioctl_get_metric, NULL }, 1124 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 1125 IF_CMD, ip_sioctl_metric, NULL }, 1126 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1127 1128 /* See 166-168 below for extended SIOC*XARP ioctls */ 1129 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV, 1130 MISC_CMD, ip_sioctl_arp, NULL }, 1131 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, 1132 MISC_CMD, ip_sioctl_arp, NULL }, 1133 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, 1134 MISC_CMD, ip_sioctl_arp, NULL }, 1135 1136 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1137 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1138 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1139 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1140 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1141 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1142 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1143 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1144 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1145 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1146 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1147 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1148 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1149 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1150 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1151 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1152 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1153 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1154 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1155 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1156 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1157 1158 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 1159 MISC_CMD, if_unitsel, if_unitsel_restart }, 1160 1161 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1162 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1163 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1164 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1165 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1166 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1167 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1168 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1169 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1170 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1171 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1172 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1173 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1174 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1175 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1176 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1177 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1178 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1179 1180 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 1181 IPI_PRIV | IPI_WR | IPI_MODOK, 1182 IF_CMD, ip_sioctl_sifname, NULL }, 1183 1184 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1185 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1186 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1187 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1188 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1189 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1190 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1191 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1192 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1193 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1194 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1195 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1196 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1197 1198 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL, 1199 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 1200 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1201 IF_CMD, ip_sioctl_get_muxid, NULL }, 1202 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 1203 IPI_PRIV | IPI_WR | IPI_REPL, 1204 IF_CMD, ip_sioctl_muxid, NULL }, 1205 1206 /* Both if and lif variants share same func */ 1207 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1208 IF_CMD, ip_sioctl_get_lifindex, NULL }, 1209 /* Both if and lif variants share same func */ 1210 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 1211 IPI_PRIV | IPI_WR | IPI_REPL, 1212 IF_CMD, ip_sioctl_slifindex, NULL }, 1213 1214 /* copyin size cannot be coded for SIOCGIFCONF */ 1215 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1216 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1217 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1218 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1219 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1220 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1221 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1222 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1223 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1224 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1225 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1226 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1227 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1228 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1229 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1230 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1231 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1232 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1233 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1234 1235 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 1236 IPI_PRIV | IPI_WR | IPI_REPL, 1237 LIF_CMD, ip_sioctl_removeif, 1238 ip_sioctl_removeif_restart }, 1239 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 1240 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL, 1241 LIF_CMD, ip_sioctl_addif, NULL }, 1242 #define SIOCLIFADDR_NDX 112 1243 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1244 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1245 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 1246 IPI_GET_CMD | IPI_REPL, 1247 LIF_CMD, ip_sioctl_get_addr, NULL }, 1248 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1249 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1250 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 1251 IPI_GET_CMD | IPI_REPL, 1252 LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 1253 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 1254 IPI_PRIV | IPI_WR | IPI_REPL, 1255 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1256 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 1257 IPI_GET_CMD | IPI_MODOK | IPI_REPL, 1258 LIF_CMD, ip_sioctl_get_flags, NULL }, 1259 1260 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1261 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1262 1263 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1264 ip_sioctl_get_lifconf, NULL }, 1265 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1266 LIF_CMD, ip_sioctl_mtu, NULL }, 1267 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, 1268 LIF_CMD, ip_sioctl_get_mtu, NULL }, 1269 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 1270 IPI_GET_CMD | IPI_REPL, 1271 LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 1272 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1273 LIF_CMD, ip_sioctl_brdaddr, NULL }, 1274 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 1275 IPI_GET_CMD | IPI_REPL, 1276 LIF_CMD, ip_sioctl_get_netmask, NULL }, 1277 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1278 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1279 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 1280 IPI_GET_CMD | IPI_REPL, 1281 LIF_CMD, ip_sioctl_get_metric, NULL }, 1282 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1283 LIF_CMD, ip_sioctl_metric, NULL }, 1284 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1285 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL, 1286 LIF_CMD, ip_sioctl_slifname, 1287 ip_sioctl_slifname_restart }, 1288 1289 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL, 1290 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1291 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1292 IPI_GET_CMD | IPI_REPL, 1293 LIF_CMD, ip_sioctl_get_muxid, NULL }, 1294 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1295 IPI_PRIV | IPI_WR | IPI_REPL, 1296 LIF_CMD, ip_sioctl_muxid, NULL }, 1297 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1298 IPI_GET_CMD | IPI_REPL, 1299 LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1300 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1301 IPI_PRIV | IPI_WR | IPI_REPL, 1302 LIF_CMD, ip_sioctl_slifindex, 0 }, 1303 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1304 LIF_CMD, ip_sioctl_token, NULL }, 1305 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1306 IPI_GET_CMD | IPI_REPL, 1307 LIF_CMD, ip_sioctl_get_token, NULL }, 1308 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1309 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1310 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1311 IPI_GET_CMD | IPI_REPL, 1312 LIF_CMD, ip_sioctl_get_subnet, NULL }, 1313 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1314 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1315 1316 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1317 IPI_GET_CMD | IPI_REPL, 1318 LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1319 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1320 LIF_CMD, ip_siocdelndp_v6, NULL }, 1321 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1322 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1323 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1324 LIF_CMD, ip_siocsetndp_v6, NULL }, 1325 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1326 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1327 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1328 MISC_CMD, ip_sioctl_tonlink, NULL }, 1329 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1330 MISC_CMD, ip_sioctl_tmysite, NULL }, 1331 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL, 1332 TUN_CMD, ip_sioctl_tunparam, NULL }, 1333 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), 1334 IPI_PRIV | IPI_WR, 1335 TUN_CMD, ip_sioctl_tunparam, NULL }, 1336 1337 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1338 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1339 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1340 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1341 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1342 1343 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq), 1344 IPI_PRIV | IPI_WR | IPI_REPL, 1345 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1346 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq), 1347 IPI_PRIV | IPI_WR | IPI_REPL, 1348 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1349 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1350 IPI_PRIV | IPI_WR, 1351 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1352 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1353 IPI_GET_CMD | IPI_REPL, 1354 LIF_CMD, ip_sioctl_get_groupname, NULL }, 1355 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq), 1356 IPI_GET_CMD | IPI_REPL, 1357 LIF_CMD, ip_sioctl_get_oindex, NULL }, 1358 1359 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1360 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1361 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1362 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1363 1364 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1365 LIF_CMD, ip_sioctl_slifoindex, NULL }, 1366 1367 /* These are handled in ip_sioctl_copyin_setup itself */ 1368 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1369 MISC_CMD, NULL, NULL }, 1370 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1371 MISC_CMD, NULL, NULL }, 1372 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1373 1374 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1375 ip_sioctl_get_lifconf, NULL }, 1376 1377 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, 1378 MISC_CMD, ip_sioctl_xarp, NULL }, 1379 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, 1380 MISC_CMD, ip_sioctl_xarp, NULL }, 1381 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, 1382 MISC_CMD, ip_sioctl_xarp, NULL }, 1383 1384 /* SIOCPOPSOCKFS is not handled by IP */ 1385 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1386 1387 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1388 IPI_GET_CMD | IPI_REPL, 1389 LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1390 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1391 IPI_PRIV | IPI_WR | IPI_REPL, 1392 LIF_CMD, ip_sioctl_slifzone, 1393 ip_sioctl_slifzone_restart }, 1394 /* 172-174 are SCTP ioctls and not handled by IP */ 1395 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1396 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1397 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1398 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1399 IPI_GET_CMD, LIF_CMD, 1400 ip_sioctl_get_lifusesrc, 0 }, 1401 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1402 IPI_PRIV | IPI_WR, 1403 LIF_CMD, ip_sioctl_slifusesrc, 1404 NULL }, 1405 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1406 ip_sioctl_get_lifsrcof, NULL }, 1407 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1408 MISC_CMD, ip_sioctl_msfilter, NULL }, 1409 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1410 MISC_CMD, ip_sioctl_msfilter, NULL }, 1411 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1412 MISC_CMD, ip_sioctl_msfilter, NULL }, 1413 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1414 MISC_CMD, ip_sioctl_msfilter, NULL }, 1415 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD, 1416 ip_sioctl_set_ipmpfailback, NULL } 1417 }; 1418 1419 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1420 1421 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1422 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), 1423 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, 1424 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, 1425 TUN_CMD, ip_sioctl_tunparam, NULL }, 1426 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1427 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1428 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1429 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1430 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, 1431 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1432 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1433 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD, 1434 MISC_CMD, mrt_ioctl}, 1435 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD, 1436 MISC_CMD, mrt_ioctl}, 1437 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD, 1438 MISC_CMD, mrt_ioctl} 1439 }; 1440 1441 int ip_misc_ioctl_count = 1442 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1443 1444 static idl_t *conn_drain_list; /* The array of conn drain lists */ 1445 static uint_t conn_drain_list_cnt; /* Total count of conn_drain_list */ 1446 static int conn_drain_list_index; /* Next drain_list to be used */ 1447 int conn_drain_nthreads; /* Number of drainers reqd. */ 1448 /* Settable in /etc/system */ 1449 uint_t ip_redirect_cnt; /* Num of redirect routes in ftable */ 1450 1451 /* Defined in ip_ire.c */ 1452 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1453 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1454 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1455 1456 static nv_t ire_nv_arr[] = { 1457 { IRE_BROADCAST, "BROADCAST" }, 1458 { IRE_LOCAL, "LOCAL" }, 1459 { IRE_LOOPBACK, "LOOPBACK" }, 1460 { IRE_CACHE, "CACHE" }, 1461 { IRE_DEFAULT, "DEFAULT" }, 1462 { IRE_PREFIX, "PREFIX" }, 1463 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1464 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1465 { IRE_HOST, "HOST" }, 1466 { IRE_HOST_REDIRECT, "HOST_REDIRECT" }, 1467 { 0 } 1468 }; 1469 1470 nv_t *ire_nv_tbl = ire_nv_arr; 1471 1472 /* Defined in ip_if.c, protect the list of IPsec capable ills */ 1473 extern krwlock_t ipsec_capab_ills_lock; 1474 1475 /* Defined in ip_netinfo.c */ 1476 extern ddi_taskq_t *eventq_queue_nic; 1477 1478 /* Packet dropper for IP IPsec processing failures */ 1479 ipdropper_t ip_dropper; 1480 1481 /* Simple ICMP IP Header Template */ 1482 static ipha_t icmp_ipha = { 1483 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1484 }; 1485 1486 struct module_info ip_mod_info = { 1487 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024 1488 }; 1489 1490 /* 1491 * Duplicate static symbols within a module confuses mdb; so we avoid the 1492 * problem by making the symbols here distinct from those in udp.c. 1493 */ 1494 1495 static struct qinit iprinit = { 1496 (pfi_t)ip_rput, NULL, ip_open, ip_close, NULL, 1497 &ip_mod_info 1498 }; 1499 1500 static struct qinit ipwinit = { 1501 (pfi_t)ip_wput, (pfi_t)ip_wsrv, ip_open, ip_close, NULL, 1502 &ip_mod_info 1503 }; 1504 1505 static struct qinit iplrinit = { 1506 (pfi_t)ip_lrput, NULL, ip_open, ip_close, NULL, 1507 &ip_mod_info 1508 }; 1509 1510 static struct qinit iplwinit = { 1511 (pfi_t)ip_lwput, NULL, ip_open, ip_close, NULL, 1512 &ip_mod_info 1513 }; 1514 1515 struct streamtab ipinfo = { 1516 &iprinit, &ipwinit, &iplrinit, &iplwinit 1517 }; 1518 1519 #ifdef DEBUG 1520 static boolean_t skip_sctp_cksum = B_FALSE; 1521 #endif 1522 1523 /* 1524 * Prepend the zoneid using an ipsec_out_t for later use by functions like 1525 * ip_rput_v6(), ip_output(), etc. If the message 1526 * block already has a M_CTL at the front of it, then simply set the zoneid 1527 * appropriately. 1528 */ 1529 mblk_t * 1530 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid) 1531 { 1532 mblk_t *first_mp; 1533 ipsec_out_t *io; 1534 1535 ASSERT(zoneid != ALL_ZONES); 1536 if (mp->b_datap->db_type == M_CTL) { 1537 io = (ipsec_out_t *)mp->b_rptr; 1538 ASSERT(io->ipsec_out_type == IPSEC_OUT); 1539 io->ipsec_out_zoneid = zoneid; 1540 return (mp); 1541 } 1542 1543 first_mp = ipsec_alloc_ipsec_out(); 1544 if (first_mp == NULL) 1545 return (NULL); 1546 io = (ipsec_out_t *)first_mp->b_rptr; 1547 /* This is not a secure packet */ 1548 io->ipsec_out_secure = B_FALSE; 1549 io->ipsec_out_zoneid = zoneid; 1550 first_mp->b_cont = mp; 1551 return (first_mp); 1552 } 1553 1554 /* 1555 * Copy an M_CTL-tagged message, preserving reference counts appropriately. 1556 */ 1557 mblk_t * 1558 ip_copymsg(mblk_t *mp) 1559 { 1560 mblk_t *nmp; 1561 ipsec_info_t *in; 1562 1563 if (mp->b_datap->db_type != M_CTL) 1564 return (copymsg(mp)); 1565 1566 in = (ipsec_info_t *)mp->b_rptr; 1567 1568 /* 1569 * Note that M_CTL is also used for delivering ICMP error messages 1570 * upstream to transport layers. 1571 */ 1572 if (in->ipsec_info_type != IPSEC_OUT && 1573 in->ipsec_info_type != IPSEC_IN) 1574 return (copymsg(mp)); 1575 1576 nmp = copymsg(mp->b_cont); 1577 1578 if (in->ipsec_info_type == IPSEC_OUT) 1579 return (ipsec_out_tag(mp, nmp)); 1580 else 1581 return (ipsec_in_tag(mp, nmp)); 1582 } 1583 1584 /* Generate an ICMP fragmentation needed message. */ 1585 static void 1586 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid) 1587 { 1588 icmph_t icmph; 1589 mblk_t *first_mp; 1590 boolean_t mctl_present; 1591 1592 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1593 1594 if (!(mp = icmp_pkt_err_ok(mp))) { 1595 if (mctl_present) 1596 freeb(first_mp); 1597 return; 1598 } 1599 1600 bzero(&icmph, sizeof (icmph_t)); 1601 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1602 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1603 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1604 BUMP_MIB(&icmp_mib, icmpOutFragNeeded); 1605 BUMP_MIB(&icmp_mib, icmpOutDestUnreachs); 1606 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid); 1607 } 1608 1609 /* 1610 * icmp_inbound deals with ICMP messages in the following ways. 1611 * 1612 * 1) It needs to send a reply back and possibly delivering it 1613 * to the "interested" upper clients. 1614 * 2) It needs to send it to the upper clients only. 1615 * 3) It needs to change some values in IP only. 1616 * 4) It needs to change some values in IP and upper layers e.g TCP. 1617 * 1618 * We need to accomodate icmp messages coming in clear until we get 1619 * everything secure from the wire. If icmp_accept_clear_messages 1620 * is zero we check with the global policy and act accordingly. If 1621 * it is non-zero, we accept the message without any checks. But 1622 * *this does not mean* that this will be delivered to the upper 1623 * clients. By accepting we might send replies back, change our MTU 1624 * value etc. but delivery to the ULP/clients depends on their policy 1625 * dispositions. 1626 * 1627 * We handle the above 4 cases in the context of IPSEC in the 1628 * following way : 1629 * 1630 * 1) Send the reply back in the same way as the request came in. 1631 * If it came in encrypted, it goes out encrypted. If it came in 1632 * clear, it goes out in clear. Thus, this will prevent chosen 1633 * plain text attack. 1634 * 2) The client may or may not expect things to come in secure. 1635 * If it comes in secure, the policy constraints are checked 1636 * before delivering it to the upper layers. If it comes in 1637 * clear, ipsec_inbound_accept_clear will decide whether to 1638 * accept this in clear or not. In both the cases, if the returned 1639 * message (IP header + 8 bytes) that caused the icmp message has 1640 * AH/ESP headers, it is sent up to AH/ESP for validation before 1641 * sending up. If there are only 8 bytes of returned message, then 1642 * upper client will not be notified. 1643 * 3) Check with global policy to see whether it matches the constaints. 1644 * But this will be done only if icmp_accept_messages_in_clear is 1645 * zero. 1646 * 4) If we need to change both in IP and ULP, then the decision taken 1647 * while affecting the values in IP and while delivering up to TCP 1648 * should be the same. 1649 * 1650 * There are two cases. 1651 * 1652 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1653 * failed), we will not deliver it to the ULP, even though they 1654 * are *willing* to accept in *clear*. This is fine as our global 1655 * disposition to icmp messages asks us reject the datagram. 1656 * 1657 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1658 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1659 * to deliver it to ULP (policy failed), it can lead to 1660 * consistency problems. The cases known at this time are 1661 * ICMP_DESTINATION_UNREACHABLE messages with following code 1662 * values : 1663 * 1664 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1665 * and Upper layer rejects. Then the communication will 1666 * come to a stop. This is solved by making similar decisions 1667 * at both levels. Currently, when we are unable to deliver 1668 * to the Upper Layer (due to policy failures) while IP has 1669 * adjusted ire_max_frag, the next outbound datagram would 1670 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1671 * will be with the right level of protection. Thus the right 1672 * value will be communicated even if we are not able to 1673 * communicate when we get from the wire initially. But this 1674 * assumes there would be at least one outbound datagram after 1675 * IP has adjusted its ire_max_frag value. To make things 1676 * simpler, we accept in clear after the validation of 1677 * AH/ESP headers. 1678 * 1679 * - Other ICMP ERRORS : We may not be able to deliver it to the 1680 * upper layer depending on the level of protection the upper 1681 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1682 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1683 * should be accepted in clear when the Upper layer expects secure. 1684 * Thus the communication may get aborted by some bad ICMP 1685 * packets. 1686 * 1687 * IPQoS Notes: 1688 * The only instance when a packet is sent for processing is when there 1689 * isn't an ICMP client and if we are interested in it. 1690 * If there is a client, IPPF processing will take place in the 1691 * ip_fanout_proto routine. 1692 * 1693 * Zones notes: 1694 * The packet is only processed in the context of the specified zone: typically 1695 * only this zone will reply to an echo request, and only interested clients in 1696 * this zone will receive a copy of the packet. This means that the caller must 1697 * call icmp_inbound() for each relevant zone. 1698 */ 1699 static void 1700 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, 1701 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, 1702 ill_t *recv_ill, zoneid_t zoneid) 1703 { 1704 icmph_t *icmph; 1705 ipha_t *ipha; 1706 int iph_hdr_length; 1707 int hdr_length; 1708 boolean_t interested; 1709 uint32_t ts; 1710 uchar_t *wptr; 1711 ipif_t *ipif; 1712 mblk_t *first_mp; 1713 ipsec_in_t *ii; 1714 ire_t *src_ire; 1715 boolean_t onlink; 1716 timestruc_t now; 1717 uint32_t ill_index; 1718 1719 ASSERT(ill != NULL); 1720 1721 first_mp = mp; 1722 if (mctl_present) { 1723 mp = first_mp->b_cont; 1724 ASSERT(mp != NULL); 1725 } 1726 1727 ipha = (ipha_t *)mp->b_rptr; 1728 if (icmp_accept_clear_messages == 0) { 1729 first_mp = ipsec_check_global_policy(first_mp, NULL, 1730 ipha, NULL, mctl_present); 1731 if (first_mp == NULL) 1732 return; 1733 } 1734 1735 /* 1736 * On a labeled system, we have to check whether the zone itself is 1737 * permitted to receive raw traffic. 1738 */ 1739 if (is_system_labeled()) { 1740 if (zoneid == ALL_ZONES) 1741 zoneid = tsol_packet_to_zoneid(mp); 1742 if (!tsol_can_accept_raw(mp, B_FALSE)) { 1743 ip1dbg(("icmp_inbound: zone %d can't receive raw", 1744 zoneid)); 1745 BUMP_MIB(&icmp_mib, icmpInErrors); 1746 freemsg(first_mp); 1747 return; 1748 } 1749 } 1750 1751 /* 1752 * We have accepted the ICMP message. It means that we will 1753 * respond to the packet if needed. It may not be delivered 1754 * to the upper client depending on the policy constraints 1755 * and the disposition in ipsec_inbound_accept_clear. 1756 */ 1757 1758 ASSERT(ill != NULL); 1759 1760 BUMP_MIB(&icmp_mib, icmpInMsgs); 1761 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1762 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { 1763 /* Last chance to get real. */ 1764 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { 1765 BUMP_MIB(&icmp_mib, icmpInErrors); 1766 freemsg(first_mp); 1767 return; 1768 } 1769 /* Refresh iph following the pullup. */ 1770 ipha = (ipha_t *)mp->b_rptr; 1771 } 1772 /* ICMP header checksum, including checksum field, should be zero. */ 1773 if (sum_valid ? (sum != 0 && sum != 0xFFFF) : 1774 IP_CSUM(mp, iph_hdr_length, 0)) { 1775 BUMP_MIB(&icmp_mib, icmpInCksumErrs); 1776 freemsg(first_mp); 1777 return; 1778 } 1779 /* The IP header will always be a multiple of four bytes */ 1780 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1781 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, 1782 icmph->icmph_code)); 1783 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1784 /* We will set "interested" to "true" if we want a copy */ 1785 interested = B_FALSE; 1786 switch (icmph->icmph_type) { 1787 case ICMP_ECHO_REPLY: 1788 BUMP_MIB(&icmp_mib, icmpInEchoReps); 1789 break; 1790 case ICMP_DEST_UNREACHABLE: 1791 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1792 BUMP_MIB(&icmp_mib, icmpInFragNeeded); 1793 interested = B_TRUE; /* Pass up to transport */ 1794 BUMP_MIB(&icmp_mib, icmpInDestUnreachs); 1795 break; 1796 case ICMP_SOURCE_QUENCH: 1797 interested = B_TRUE; /* Pass up to transport */ 1798 BUMP_MIB(&icmp_mib, icmpInSrcQuenchs); 1799 break; 1800 case ICMP_REDIRECT: 1801 if (!ip_ignore_redirect) 1802 interested = B_TRUE; 1803 BUMP_MIB(&icmp_mib, icmpInRedirects); 1804 break; 1805 case ICMP_ECHO_REQUEST: 1806 /* 1807 * Whether to respond to echo requests that come in as IP 1808 * broadcasts or as IP multicast is subject to debate 1809 * (what isn't?). We aim to please, you pick it. 1810 * Default is do it. 1811 */ 1812 if (!broadcast && !CLASSD(ipha->ipha_dst)) { 1813 /* unicast: always respond */ 1814 interested = B_TRUE; 1815 } else if (CLASSD(ipha->ipha_dst)) { 1816 /* multicast: respond based on tunable */ 1817 interested = ip_g_resp_to_echo_mcast; 1818 } else if (broadcast) { 1819 /* broadcast: respond based on tunable */ 1820 interested = ip_g_resp_to_echo_bcast; 1821 } 1822 BUMP_MIB(&icmp_mib, icmpInEchos); 1823 break; 1824 case ICMP_ROUTER_ADVERTISEMENT: 1825 case ICMP_ROUTER_SOLICITATION: 1826 break; 1827 case ICMP_TIME_EXCEEDED: 1828 interested = B_TRUE; /* Pass up to transport */ 1829 BUMP_MIB(&icmp_mib, icmpInTimeExcds); 1830 break; 1831 case ICMP_PARAM_PROBLEM: 1832 interested = B_TRUE; /* Pass up to transport */ 1833 BUMP_MIB(&icmp_mib, icmpInParmProbs); 1834 break; 1835 case ICMP_TIME_STAMP_REQUEST: 1836 /* Response to Time Stamp Requests is local policy. */ 1837 if (ip_g_resp_to_timestamp && 1838 /* So is whether to respond if it was an IP broadcast. */ 1839 (!broadcast || ip_g_resp_to_timestamp_bcast)) { 1840 int tstamp_len = 3 * sizeof (uint32_t); 1841 1842 if (wptr + tstamp_len > mp->b_wptr) { 1843 if (!pullupmsg(mp, wptr + tstamp_len - 1844 mp->b_rptr)) { 1845 BUMP_MIB(&ip_mib, ipInDiscards); 1846 freemsg(first_mp); 1847 return; 1848 } 1849 /* Refresh ipha following the pullup. */ 1850 ipha = (ipha_t *)mp->b_rptr; 1851 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1852 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1853 } 1854 interested = B_TRUE; 1855 } 1856 BUMP_MIB(&icmp_mib, icmpInTimestamps); 1857 break; 1858 case ICMP_TIME_STAMP_REPLY: 1859 BUMP_MIB(&icmp_mib, icmpInTimestampReps); 1860 break; 1861 case ICMP_INFO_REQUEST: 1862 /* Per RFC 1122 3.2.2.7, ignore this. */ 1863 case ICMP_INFO_REPLY: 1864 break; 1865 case ICMP_ADDRESS_MASK_REQUEST: 1866 if ((ip_respond_to_address_mask_broadcast || !broadcast) && 1867 /* TODO m_pullup of complete header? */ 1868 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) 1869 interested = B_TRUE; 1870 BUMP_MIB(&icmp_mib, icmpInAddrMasks); 1871 break; 1872 case ICMP_ADDRESS_MASK_REPLY: 1873 BUMP_MIB(&icmp_mib, icmpInAddrMaskReps); 1874 break; 1875 default: 1876 interested = B_TRUE; /* Pass up to transport */ 1877 BUMP_MIB(&icmp_mib, icmpInUnknowns); 1878 break; 1879 } 1880 /* See if there is an ICMP client. */ 1881 if (ipcl_proto_search(IPPROTO_ICMP) != NULL) { 1882 /* If there is an ICMP client and we want one too, copy it. */ 1883 mblk_t *first_mp1; 1884 1885 if (!interested) { 1886 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1887 ip_policy, recv_ill, zoneid); 1888 return; 1889 } 1890 first_mp1 = ip_copymsg(first_mp); 1891 if (first_mp1 != NULL) { 1892 ip_fanout_proto(q, first_mp1, ill, ipha, 1893 0, mctl_present, ip_policy, recv_ill, zoneid); 1894 } 1895 } else if (!interested) { 1896 freemsg(first_mp); 1897 return; 1898 } else { 1899 /* 1900 * Initiate policy processing for this packet if ip_policy 1901 * is true. 1902 */ 1903 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 1904 ill_index = ill->ill_phyint->phyint_ifindex; 1905 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1906 if (mp == NULL) { 1907 if (mctl_present) { 1908 freeb(first_mp); 1909 } 1910 BUMP_MIB(&icmp_mib, icmpInErrors); 1911 return; 1912 } 1913 } 1914 } 1915 /* We want to do something with it. */ 1916 /* Check db_ref to make sure we can modify the packet. */ 1917 if (mp->b_datap->db_ref > 1) { 1918 mblk_t *first_mp1; 1919 1920 first_mp1 = ip_copymsg(first_mp); 1921 freemsg(first_mp); 1922 if (!first_mp1) { 1923 BUMP_MIB(&icmp_mib, icmpOutDrops); 1924 return; 1925 } 1926 first_mp = first_mp1; 1927 if (mctl_present) { 1928 mp = first_mp->b_cont; 1929 ASSERT(mp != NULL); 1930 } else { 1931 mp = first_mp; 1932 } 1933 ipha = (ipha_t *)mp->b_rptr; 1934 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1935 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1936 } 1937 switch (icmph->icmph_type) { 1938 case ICMP_ADDRESS_MASK_REQUEST: 1939 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1940 if (ipif == NULL) { 1941 freemsg(first_mp); 1942 return; 1943 } 1944 /* 1945 * outging interface must be IPv4 1946 */ 1947 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1948 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1949 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1950 ipif_refrele(ipif); 1951 BUMP_MIB(&icmp_mib, icmpOutAddrMaskReps); 1952 break; 1953 case ICMP_ECHO_REQUEST: 1954 icmph->icmph_type = ICMP_ECHO_REPLY; 1955 BUMP_MIB(&icmp_mib, icmpOutEchoReps); 1956 break; 1957 case ICMP_TIME_STAMP_REQUEST: { 1958 uint32_t *tsp; 1959 1960 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1961 tsp = (uint32_t *)wptr; 1962 tsp++; /* Skip past 'originate time' */ 1963 /* Compute # of milliseconds since midnight */ 1964 gethrestime(&now); 1965 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1966 now.tv_nsec / (NANOSEC / MILLISEC); 1967 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1968 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1969 BUMP_MIB(&icmp_mib, icmpOutTimestampReps); 1970 break; 1971 } 1972 default: 1973 ipha = (ipha_t *)&icmph[1]; 1974 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1975 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1976 BUMP_MIB(&ip_mib, ipInDiscards); 1977 freemsg(first_mp); 1978 return; 1979 } 1980 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1981 ipha = (ipha_t *)&icmph[1]; 1982 } 1983 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1984 BUMP_MIB(&ip_mib, ipInDiscards); 1985 freemsg(first_mp); 1986 return; 1987 } 1988 hdr_length = IPH_HDR_LENGTH(ipha); 1989 if (hdr_length < sizeof (ipha_t)) { 1990 BUMP_MIB(&ip_mib, ipInDiscards); 1991 freemsg(first_mp); 1992 return; 1993 } 1994 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 1995 if (!pullupmsg(mp, 1996 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 1997 BUMP_MIB(&ip_mib, ipInDiscards); 1998 freemsg(first_mp); 1999 return; 2000 } 2001 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2002 ipha = (ipha_t *)&icmph[1]; 2003 } 2004 switch (icmph->icmph_type) { 2005 case ICMP_REDIRECT: 2006 /* 2007 * As there is no upper client to deliver, we don't 2008 * need the first_mp any more. 2009 */ 2010 if (mctl_present) { 2011 freeb(first_mp); 2012 } 2013 icmp_redirect(mp); 2014 return; 2015 case ICMP_DEST_UNREACHABLE: 2016 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 2017 if (!icmp_inbound_too_big(icmph, ipha, ill, 2018 zoneid, mp, iph_hdr_length)) { 2019 freemsg(first_mp); 2020 return; 2021 } 2022 /* 2023 * icmp_inbound_too_big() may alter mp. 2024 * Resynch ipha and icmph accordingly. 2025 */ 2026 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2027 ipha = (ipha_t *)&icmph[1]; 2028 } 2029 /* FALLTHRU */ 2030 default : 2031 /* 2032 * IPQoS notes: Since we have already done IPQoS 2033 * processing we don't want to do it again in 2034 * the fanout routines called by 2035 * icmp_inbound_error_fanout, hence the last 2036 * argument, ip_policy, is B_FALSE. 2037 */ 2038 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 2039 ipha, iph_hdr_length, hdr_length, mctl_present, 2040 B_FALSE, recv_ill, zoneid); 2041 } 2042 return; 2043 } 2044 /* Send out an ICMP packet */ 2045 icmph->icmph_checksum = 0; 2046 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 2047 if (icmph->icmph_checksum == 0) 2048 icmph->icmph_checksum = 0xFFFF; 2049 if (broadcast || CLASSD(ipha->ipha_dst)) { 2050 ipif_t *ipif_chosen; 2051 /* 2052 * Make it look like it was directed to us, so we don't look 2053 * like a fool with a broadcast or multicast source address. 2054 */ 2055 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 2056 /* 2057 * Make sure that we haven't grabbed an interface that's DOWN. 2058 */ 2059 if (ipif != NULL) { 2060 ipif_chosen = ipif_select_source(ipif->ipif_ill, 2061 ipha->ipha_src, zoneid); 2062 if (ipif_chosen != NULL) { 2063 ipif_refrele(ipif); 2064 ipif = ipif_chosen; 2065 } 2066 } 2067 if (ipif == NULL) { 2068 ip0dbg(("icmp_inbound: " 2069 "No source for broadcast/multicast:\n" 2070 "\tsrc 0x%x dst 0x%x ill %p " 2071 "ipif_lcl_addr 0x%x\n", 2072 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 2073 (void *)ill, 2074 ill->ill_ipif->ipif_lcl_addr)); 2075 freemsg(first_mp); 2076 return; 2077 } 2078 ASSERT(ipif != NULL && !ipif->ipif_isv6); 2079 ipha->ipha_dst = ipif->ipif_src_addr; 2080 ipif_refrele(ipif); 2081 } 2082 /* Reset time to live. */ 2083 ipha->ipha_ttl = ip_def_ttl; 2084 { 2085 /* Swap source and destination addresses */ 2086 ipaddr_t tmp; 2087 2088 tmp = ipha->ipha_src; 2089 ipha->ipha_src = ipha->ipha_dst; 2090 ipha->ipha_dst = tmp; 2091 } 2092 ipha->ipha_ident = 0; 2093 if (!IS_SIMPLE_IPH(ipha)) 2094 icmp_options_update(ipha); 2095 2096 /* 2097 * ICMP echo replies should go out on the same interface 2098 * the request came on as probes used by in.mpathd for detecting 2099 * NIC failures are ECHO packets. We turn-off load spreading 2100 * by setting ipsec_in_attach_if to B_TRUE, which is copied 2101 * to ipsec_out_attach_if by ipsec_in_to_out called later in this 2102 * function. This is in turn handled by ip_wput and ip_newroute 2103 * to make sure that the packet goes out on the interface it came 2104 * in on. If we don't turnoff load spreading, the packets might get 2105 * dropped if there are no non-FAILED/INACTIVE interfaces for it 2106 * to go out and in.mpathd would wrongly detect a failure or 2107 * mis-detect a NIC failure for link failure. As load spreading 2108 * can happen only if ill_group is not NULL, we do only for 2109 * that case and this does not affect the normal case. 2110 * 2111 * We turn off load spreading only on echo packets that came from 2112 * on-link hosts. If the interface route has been deleted, this will 2113 * not be enforced as we can't do much. For off-link hosts, as the 2114 * default routes in IPv4 does not typically have an ire_ipif 2115 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. 2116 * Moreover, expecting a default route through this interface may 2117 * not be correct. We use ipha_dst because of the swap above. 2118 */ 2119 onlink = B_FALSE; 2120 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { 2121 /* 2122 * First, we need to make sure that it is not one of our 2123 * local addresses. If we set onlink when it is one of 2124 * our local addresses, we will end up creating IRE_CACHES 2125 * for one of our local addresses. Then, we will never 2126 * accept packets for them afterwards. 2127 */ 2128 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, 2129 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 2130 if (src_ire == NULL) { 2131 ipif = ipif_get_next_ipif(NULL, ill); 2132 if (ipif == NULL) { 2133 BUMP_MIB(&ip_mib, ipInDiscards); 2134 freemsg(mp); 2135 return; 2136 } 2137 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 2138 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 2139 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE); 2140 ipif_refrele(ipif); 2141 if (src_ire != NULL) { 2142 onlink = B_TRUE; 2143 ire_refrele(src_ire); 2144 } 2145 } else { 2146 ire_refrele(src_ire); 2147 } 2148 } 2149 if (!mctl_present) { 2150 /* 2151 * This packet should go out the same way as it 2152 * came in i.e in clear. To make sure that global 2153 * policy will not be applied to this in ip_wput_ire, 2154 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2155 */ 2156 ASSERT(first_mp == mp); 2157 if ((first_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 2158 BUMP_MIB(&ip_mib, ipInDiscards); 2159 freemsg(mp); 2160 return; 2161 } 2162 ii = (ipsec_in_t *)first_mp->b_rptr; 2163 2164 /* This is not a secure packet */ 2165 ii->ipsec_in_secure = B_FALSE; 2166 if (onlink) { 2167 ii->ipsec_in_attach_if = B_TRUE; 2168 ii->ipsec_in_ill_index = 2169 ill->ill_phyint->phyint_ifindex; 2170 ii->ipsec_in_rill_index = 2171 recv_ill->ill_phyint->phyint_ifindex; 2172 } 2173 first_mp->b_cont = mp; 2174 } else if (onlink) { 2175 ii = (ipsec_in_t *)first_mp->b_rptr; 2176 ii->ipsec_in_attach_if = B_TRUE; 2177 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 2178 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; 2179 } else { 2180 ii = (ipsec_in_t *)first_mp->b_rptr; 2181 } 2182 ii->ipsec_in_zoneid = zoneid; 2183 ASSERT(zoneid != ALL_ZONES); 2184 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2185 BUMP_MIB(&ip_mib, ipInDiscards); 2186 return; 2187 } 2188 BUMP_MIB(&icmp_mib, icmpOutMsgs); 2189 put(WR(q), first_mp); 2190 } 2191 2192 static ipaddr_t 2193 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) 2194 { 2195 conn_t *connp; 2196 connf_t *connfp; 2197 ipaddr_t nexthop_addr = INADDR_ANY; 2198 int hdr_length = IPH_HDR_LENGTH(ipha); 2199 uint16_t *up; 2200 uint32_t ports; 2201 2202 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2203 switch (ipha->ipha_protocol) { 2204 case IPPROTO_TCP: 2205 { 2206 tcph_t *tcph; 2207 2208 /* do a reverse lookup */ 2209 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2210 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, 2211 TCPS_LISTEN); 2212 break; 2213 } 2214 case IPPROTO_UDP: 2215 { 2216 uint32_t dstport, srcport; 2217 2218 ((uint16_t *)&ports)[0] = up[1]; 2219 ((uint16_t *)&ports)[1] = up[0]; 2220 2221 /* Extract ports in net byte order */ 2222 dstport = htons(ntohl(ports) & 0xFFFF); 2223 srcport = htons(ntohl(ports) >> 16); 2224 2225 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 2226 mutex_enter(&connfp->connf_lock); 2227 connp = connfp->connf_head; 2228 2229 /* do a reverse lookup */ 2230 while ((connp != NULL) && 2231 (!IPCL_UDP_MATCH(connp, dstport, 2232 ipha->ipha_src, srcport, ipha->ipha_dst) || 2233 !IPCL_ZONE_MATCH(connp, zoneid))) { 2234 connp = connp->conn_next; 2235 } 2236 if (connp != NULL) 2237 CONN_INC_REF(connp); 2238 mutex_exit(&connfp->connf_lock); 2239 break; 2240 } 2241 case IPPROTO_SCTP: 2242 { 2243 in6_addr_t map_src, map_dst; 2244 2245 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); 2246 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); 2247 ((uint16_t *)&ports)[0] = up[1]; 2248 ((uint16_t *)&ports)[1] = up[0]; 2249 2250 if ((connp = sctp_find_conn(&map_src, &map_dst, ports, 2251 0, zoneid)) == NULL) { 2252 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, 2253 zoneid, ports, ipha); 2254 } else { 2255 CONN_INC_REF(connp); 2256 SCTP_REFRELE(CONN2SCTP(connp)); 2257 } 2258 break; 2259 } 2260 default: 2261 { 2262 ipha_t ripha; 2263 2264 ripha.ipha_src = ipha->ipha_dst; 2265 ripha.ipha_dst = ipha->ipha_src; 2266 ripha.ipha_protocol = ipha->ipha_protocol; 2267 2268 connfp = &ipcl_proto_fanout[ipha->ipha_protocol]; 2269 mutex_enter(&connfp->connf_lock); 2270 connp = connfp->connf_head; 2271 for (connp = connfp->connf_head; connp != NULL; 2272 connp = connp->conn_next) { 2273 if (IPCL_PROTO_MATCH(connp, 2274 ipha->ipha_protocol, &ripha, ill, 2275 0, zoneid)) { 2276 CONN_INC_REF(connp); 2277 break; 2278 } 2279 } 2280 mutex_exit(&connfp->connf_lock); 2281 } 2282 } 2283 if (connp != NULL) { 2284 if (connp->conn_nexthop_set) 2285 nexthop_addr = connp->conn_nexthop_v4; 2286 CONN_DEC_REF(connp); 2287 } 2288 return (nexthop_addr); 2289 } 2290 2291 /* Table from RFC 1191 */ 2292 static int icmp_frag_size_table[] = 2293 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2294 2295 /* 2296 * Process received ICMP Packet too big. 2297 * After updating any IRE it does the fanout to any matching transport streams. 2298 * Assumes the message has been pulled up till the IP header that caused 2299 * the error. 2300 * 2301 * Returns B_FALSE on failure and B_TRUE on success. 2302 */ 2303 static boolean_t 2304 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, 2305 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length) 2306 { 2307 ire_t *ire, *first_ire; 2308 int mtu; 2309 int hdr_length; 2310 ipaddr_t nexthop_addr; 2311 2312 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 2313 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 2314 2315 hdr_length = IPH_HDR_LENGTH(ipha); 2316 2317 /* Drop if the original packet contained a source route */ 2318 if (ip_source_route_included(ipha)) { 2319 return (B_FALSE); 2320 } 2321 /* 2322 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport 2323 * header. 2324 */ 2325 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2326 mp->b_wptr) { 2327 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2328 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2329 BUMP_MIB(&ip_mib, ipInDiscards); 2330 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n")); 2331 return (B_FALSE); 2332 } 2333 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2334 ipha = (ipha_t *)&icmph[1]; 2335 } 2336 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp); 2337 if (nexthop_addr != INADDR_ANY) { 2338 /* nexthop set */ 2339 first_ire = ire_ctable_lookup(ipha->ipha_dst, 2340 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp), 2341 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW); 2342 } else { 2343 /* nexthop not set */ 2344 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, 2345 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 2346 } 2347 2348 if (!first_ire) { 2349 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", 2350 ntohl(ipha->ipha_dst))); 2351 return (B_FALSE); 2352 } 2353 /* Check for MTU discovery advice as described in RFC 1191 */ 2354 mtu = ntohs(icmph->icmph_du_mtu); 2355 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2356 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2357 ire = ire->ire_next) { 2358 /* 2359 * Look for the connection to which this ICMP message is 2360 * directed. If it has the IP_NEXTHOP option set, then the 2361 * search is limited to IREs with the MATCH_IRE_PRIVATE 2362 * option. Else the search is limited to regular IREs. 2363 */ 2364 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2365 (nexthop_addr != ire->ire_gateway_addr)) || 2366 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2367 (nexthop_addr != INADDR_ANY))) 2368 continue; 2369 2370 mutex_enter(&ire->ire_lock); 2371 if (icmph->icmph_du_zero == 0 && mtu > 68) { 2372 /* Reduce the IRE max frag value as advised. */ 2373 ip1dbg(("Received mtu from router: %d (was %d)\n", 2374 mtu, ire->ire_max_frag)); 2375 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2376 } else { 2377 uint32_t length; 2378 int i; 2379 2380 /* 2381 * Use the table from RFC 1191 to figure out 2382 * the next "plateau" based on the length in 2383 * the original IP packet. 2384 */ 2385 length = ntohs(ipha->ipha_length); 2386 if (ire->ire_max_frag <= length && 2387 ire->ire_max_frag >= length - hdr_length) { 2388 /* 2389 * Handle broken BSD 4.2 systems that 2390 * return the wrong iph_length in ICMP 2391 * errors. 2392 */ 2393 ip1dbg(("Wrong mtu: sent %d, ire %d\n", 2394 length, ire->ire_max_frag)); 2395 length -= hdr_length; 2396 } 2397 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2398 if (length > icmp_frag_size_table[i]) 2399 break; 2400 } 2401 if (i == A_CNT(icmp_frag_size_table)) { 2402 /* Smaller than 68! */ 2403 ip1dbg(("Too big for packet size %d\n", 2404 length)); 2405 ire->ire_max_frag = MIN(ire->ire_max_frag, 576); 2406 ire->ire_frag_flag = 0; 2407 } else { 2408 mtu = icmp_frag_size_table[i]; 2409 ip1dbg(("Calculated mtu %d, packet size %d, " 2410 "before %d", mtu, length, 2411 ire->ire_max_frag)); 2412 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2413 ip1dbg((", after %d\n", ire->ire_max_frag)); 2414 } 2415 /* Record the new max frag size for the ULP. */ 2416 icmph->icmph_du_zero = 0; 2417 icmph->icmph_du_mtu = 2418 htons((uint16_t)ire->ire_max_frag); 2419 } 2420 mutex_exit(&ire->ire_lock); 2421 } 2422 rw_exit(&first_ire->ire_bucket->irb_lock); 2423 ire_refrele(first_ire); 2424 return (B_TRUE); 2425 } 2426 2427 /* 2428 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2429 * calls this function. 2430 */ 2431 static mblk_t * 2432 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2433 { 2434 ipha_t *ipha; 2435 icmph_t *icmph; 2436 ipha_t *in_ipha; 2437 int length; 2438 2439 ASSERT(mp->b_datap->db_type == M_DATA); 2440 2441 /* 2442 * For Self-encapsulated packets, we added an extra IP header 2443 * without the options. Inner IP header is the one from which 2444 * the outer IP header was formed. Thus, we need to remove the 2445 * outer IP header. To do this, we pullup the whole message 2446 * and overlay whatever follows the outer IP header over the 2447 * outer IP header. 2448 */ 2449 2450 if (!pullupmsg(mp, -1)) { 2451 BUMP_MIB(&ip_mib, ipInDiscards); 2452 return (NULL); 2453 } 2454 2455 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2456 ipha = (ipha_t *)&icmph[1]; 2457 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2458 2459 /* 2460 * The length that we want to overlay is following the inner 2461 * IP header. Subtracting the IP header + icmp header + outer 2462 * IP header's length should give us the length that we want to 2463 * overlay. 2464 */ 2465 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2466 hdr_length; 2467 /* 2468 * Overlay whatever follows the inner header over the 2469 * outer header. 2470 */ 2471 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2472 2473 /* Set the wptr to account for the outer header */ 2474 mp->b_wptr -= hdr_length; 2475 return (mp); 2476 } 2477 2478 /* 2479 * Try to pass the ICMP message upstream in case the ULP cares. 2480 * 2481 * If the packet that caused the ICMP error is secure, we send 2482 * it to AH/ESP to make sure that the attached packet has a 2483 * valid association. ipha in the code below points to the 2484 * IP header of the packet that caused the error. 2485 * 2486 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2487 * in the context of IPSEC. Normally we tell the upper layer 2488 * whenever we send the ire (including ip_bind), the IPSEC header 2489 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2490 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2491 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2492 * same thing. As TCP has the IPSEC options size that needs to be 2493 * adjusted, we just pass the MTU unchanged. 2494 * 2495 * IFN could have been generated locally or by some router. 2496 * 2497 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2498 * This happens because IP adjusted its value of MTU on an 2499 * earlier IFN message and could not tell the upper layer, 2500 * the new adjusted value of MTU e.g. Packet was encrypted 2501 * or there was not enough information to fanout to upper 2502 * layers. Thus on the next outbound datagram, ip_wput_ire 2503 * generates the IFN, where IPSEC processing has *not* been 2504 * done. 2505 * 2506 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2507 * could have generated this. This happens because ire_max_frag 2508 * value in IP was set to a new value, while the IPSEC processing 2509 * was being done and after we made the fragmentation check in 2510 * ip_wput_ire. Thus on return from IPSEC processing, 2511 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2512 * and generates the IFN. As IPSEC processing is over, we fanout 2513 * to AH/ESP to remove the header. 2514 * 2515 * In both these cases, ipsec_in_loopback will be set indicating 2516 * that IFN was generated locally. 2517 * 2518 * ROUTER : IFN could be secure or non-secure. 2519 * 2520 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2521 * packet in error has AH/ESP headers to validate the AH/ESP 2522 * headers. AH/ESP will verify whether there is a valid SA or 2523 * not and send it back. We will fanout again if we have more 2524 * data in the packet. 2525 * 2526 * If the packet in error does not have AH/ESP, we handle it 2527 * like any other case. 2528 * 2529 * * NON_SECURE : If the packet in error has AH/ESP headers, 2530 * we attach a dummy ipsec_in and send it up to AH/ESP 2531 * for validation. AH/ESP will verify whether there is a 2532 * valid SA or not and send it back. We will fanout again if 2533 * we have more data in the packet. 2534 * 2535 * If the packet in error does not have AH/ESP, we handle it 2536 * like any other case. 2537 */ 2538 static void 2539 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2540 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2541 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2542 zoneid_t zoneid) 2543 { 2544 uint16_t *up; /* Pointer to ports in ULP header */ 2545 uint32_t ports; /* reversed ports for fanout */ 2546 ipha_t ripha; /* With reversed addresses */ 2547 mblk_t *first_mp; 2548 ipsec_in_t *ii; 2549 tcph_t *tcph; 2550 conn_t *connp; 2551 2552 first_mp = mp; 2553 if (mctl_present) { 2554 mp = first_mp->b_cont; 2555 ASSERT(mp != NULL); 2556 2557 ii = (ipsec_in_t *)first_mp->b_rptr; 2558 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2559 } else { 2560 ii = NULL; 2561 } 2562 2563 switch (ipha->ipha_protocol) { 2564 case IPPROTO_UDP: 2565 /* 2566 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2567 * transport header. 2568 */ 2569 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2570 mp->b_wptr) { 2571 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2572 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2573 BUMP_MIB(&ip_mib, ipInDiscards); 2574 goto drop_pkt; 2575 } 2576 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2577 ipha = (ipha_t *)&icmph[1]; 2578 } 2579 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2580 2581 /* 2582 * Attempt to find a client stream based on port. 2583 * Note that we do a reverse lookup since the header is 2584 * in the form we sent it out. 2585 * The ripha header is only used for the IP_UDP_MATCH and we 2586 * only set the src and dst addresses and protocol. 2587 */ 2588 ripha.ipha_src = ipha->ipha_dst; 2589 ripha.ipha_dst = ipha->ipha_src; 2590 ripha.ipha_protocol = ipha->ipha_protocol; 2591 ((uint16_t *)&ports)[0] = up[1]; 2592 ((uint16_t *)&ports)[1] = up[0]; 2593 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2594 ntohl(ipha->ipha_src), ntohs(up[0]), 2595 ntohl(ipha->ipha_dst), ntohs(up[1]), 2596 icmph->icmph_type, icmph->icmph_code)); 2597 2598 /* Have to change db_type after any pullupmsg */ 2599 DB_TYPE(mp) = M_CTL; 2600 2601 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2602 mctl_present, ip_policy, recv_ill, zoneid); 2603 return; 2604 2605 case IPPROTO_TCP: 2606 /* 2607 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2608 * transport header. 2609 */ 2610 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2611 mp->b_wptr) { 2612 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2613 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2614 BUMP_MIB(&ip_mib, ipInDiscards); 2615 goto drop_pkt; 2616 } 2617 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2618 ipha = (ipha_t *)&icmph[1]; 2619 } 2620 /* 2621 * Find a TCP client stream for this packet. 2622 * Note that we do a reverse lookup since the header is 2623 * in the form we sent it out. 2624 */ 2625 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2626 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN); 2627 if (connp == NULL) { 2628 BUMP_MIB(&ip_mib, ipInDiscards); 2629 goto drop_pkt; 2630 } 2631 2632 /* Have to change db_type after any pullupmsg */ 2633 DB_TYPE(mp) = M_CTL; 2634 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 2635 connp, SQTAG_TCP_INPUT_ICMP_ERR); 2636 return; 2637 2638 case IPPROTO_SCTP: 2639 /* 2640 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2641 * transport header. 2642 */ 2643 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2644 mp->b_wptr) { 2645 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2646 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2647 BUMP_MIB(&ip_mib, ipInDiscards); 2648 goto drop_pkt; 2649 } 2650 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2651 ipha = (ipha_t *)&icmph[1]; 2652 } 2653 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2654 /* 2655 * Find a SCTP client stream for this packet. 2656 * Note that we do a reverse lookup since the header is 2657 * in the form we sent it out. 2658 * The ripha header is only used for the matching and we 2659 * only set the src and dst addresses, protocol, and version. 2660 */ 2661 ripha.ipha_src = ipha->ipha_dst; 2662 ripha.ipha_dst = ipha->ipha_src; 2663 ripha.ipha_protocol = ipha->ipha_protocol; 2664 ripha.ipha_version_and_hdr_length = 2665 ipha->ipha_version_and_hdr_length; 2666 ((uint16_t *)&ports)[0] = up[1]; 2667 ((uint16_t *)&ports)[1] = up[0]; 2668 2669 /* Have to change db_type after any pullupmsg */ 2670 DB_TYPE(mp) = M_CTL; 2671 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2672 mctl_present, ip_policy, 0, zoneid); 2673 return; 2674 2675 case IPPROTO_ESP: 2676 case IPPROTO_AH: { 2677 int ipsec_rc; 2678 2679 /* 2680 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2681 * We will re-use the IPSEC_IN if it is already present as 2682 * AH/ESP will not affect any fields in the IPSEC_IN for 2683 * ICMP errors. If there is no IPSEC_IN, allocate a new 2684 * one and attach it in the front. 2685 */ 2686 if (ii != NULL) { 2687 /* 2688 * ip_fanout_proto_again converts the ICMP errors 2689 * that come back from AH/ESP to M_DATA so that 2690 * if it is non-AH/ESP and we do a pullupmsg in 2691 * this function, it would work. Convert it back 2692 * to M_CTL before we send up as this is a ICMP 2693 * error. This could have been generated locally or 2694 * by some router. Validate the inner IPSEC 2695 * headers. 2696 * 2697 * NOTE : ill_index is used by ip_fanout_proto_again 2698 * to locate the ill. 2699 */ 2700 ASSERT(ill != NULL); 2701 ii->ipsec_in_ill_index = 2702 ill->ill_phyint->phyint_ifindex; 2703 ii->ipsec_in_rill_index = 2704 recv_ill->ill_phyint->phyint_ifindex; 2705 DB_TYPE(first_mp->b_cont) = M_CTL; 2706 } else { 2707 /* 2708 * IPSEC_IN is not present. We attach a ipsec_in 2709 * message and send up to IPSEC for validating 2710 * and removing the IPSEC headers. Clear 2711 * ipsec_in_secure so that when we return 2712 * from IPSEC, we don't mistakenly think that this 2713 * is a secure packet came from the network. 2714 * 2715 * NOTE : ill_index is used by ip_fanout_proto_again 2716 * to locate the ill. 2717 */ 2718 ASSERT(first_mp == mp); 2719 first_mp = ipsec_in_alloc(B_TRUE); 2720 if (first_mp == NULL) { 2721 freemsg(mp); 2722 BUMP_MIB(&ip_mib, ipInDiscards); 2723 return; 2724 } 2725 ii = (ipsec_in_t *)first_mp->b_rptr; 2726 2727 /* This is not a secure packet */ 2728 ii->ipsec_in_secure = B_FALSE; 2729 first_mp->b_cont = mp; 2730 DB_TYPE(mp) = M_CTL; 2731 ASSERT(ill != NULL); 2732 ii->ipsec_in_ill_index = 2733 ill->ill_phyint->phyint_ifindex; 2734 ii->ipsec_in_rill_index = 2735 recv_ill->ill_phyint->phyint_ifindex; 2736 } 2737 ip2dbg(("icmp_inbound_error: ipsec\n")); 2738 2739 if (!ipsec_loaded()) { 2740 ip_proto_not_sup(q, first_mp, 0, zoneid); 2741 return; 2742 } 2743 2744 if (ipha->ipha_protocol == IPPROTO_ESP) 2745 ipsec_rc = ipsecesp_icmp_error(first_mp); 2746 else 2747 ipsec_rc = ipsecah_icmp_error(first_mp); 2748 if (ipsec_rc == IPSEC_STATUS_FAILED) 2749 return; 2750 2751 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2752 return; 2753 } 2754 default: 2755 /* 2756 * The ripha header is only used for the lookup and we 2757 * only set the src and dst addresses and protocol. 2758 */ 2759 ripha.ipha_src = ipha->ipha_dst; 2760 ripha.ipha_dst = ipha->ipha_src; 2761 ripha.ipha_protocol = ipha->ipha_protocol; 2762 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2763 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2764 ntohl(ipha->ipha_dst), 2765 icmph->icmph_type, icmph->icmph_code)); 2766 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2767 ipha_t *in_ipha; 2768 2769 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2770 mp->b_wptr) { 2771 if (!pullupmsg(mp, (uchar_t *)ipha + 2772 hdr_length + sizeof (ipha_t) - 2773 mp->b_rptr)) { 2774 2775 BUMP_MIB(&ip_mib, ipInDiscards); 2776 goto drop_pkt; 2777 } 2778 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2779 ipha = (ipha_t *)&icmph[1]; 2780 } 2781 /* 2782 * Caller has verified that length has to be 2783 * at least the size of IP header. 2784 */ 2785 ASSERT(hdr_length >= sizeof (ipha_t)); 2786 /* 2787 * Check the sanity of the inner IP header like 2788 * we did for the outer header. 2789 */ 2790 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2791 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2792 BUMP_MIB(&ip_mib, ipInDiscards); 2793 goto drop_pkt; 2794 } 2795 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2796 BUMP_MIB(&ip_mib, ipInDiscards); 2797 goto drop_pkt; 2798 } 2799 /* Check for Self-encapsulated tunnels */ 2800 if (in_ipha->ipha_src == ipha->ipha_src && 2801 in_ipha->ipha_dst == ipha->ipha_dst) { 2802 2803 mp = icmp_inbound_self_encap_error(mp, 2804 iph_hdr_length, hdr_length); 2805 if (mp == NULL) 2806 goto drop_pkt; 2807 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2808 ipha = (ipha_t *)&icmph[1]; 2809 hdr_length = IPH_HDR_LENGTH(ipha); 2810 /* 2811 * The packet in error is self-encapsualted. 2812 * And we are finding it further encapsulated 2813 * which we could not have possibly generated. 2814 */ 2815 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2816 BUMP_MIB(&ip_mib, ipInDiscards); 2817 goto drop_pkt; 2818 } 2819 icmp_inbound_error_fanout(q, ill, first_mp, 2820 icmph, ipha, iph_hdr_length, hdr_length, 2821 mctl_present, ip_policy, recv_ill, zoneid); 2822 return; 2823 } 2824 } 2825 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2826 ipha->ipha_protocol == IPPROTO_IPV6) && 2827 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2828 ii != NULL && 2829 ii->ipsec_in_loopback && 2830 ii->ipsec_in_secure) { 2831 /* 2832 * For IP tunnels that get a looped-back 2833 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2834 * reported new MTU to take into account the IPsec 2835 * headers protecting this configured tunnel. 2836 * 2837 * This allows the tunnel module (tun.c) to blindly 2838 * accept the MTU reported in an ICMP "too big" 2839 * message. 2840 * 2841 * Non-looped back ICMP messages will just be 2842 * handled by the security protocols (if needed), 2843 * and the first subsequent packet will hit this 2844 * path. 2845 */ 2846 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2847 ipsec_in_extra_length(first_mp)); 2848 } 2849 /* Have to change db_type after any pullupmsg */ 2850 DB_TYPE(mp) = M_CTL; 2851 2852 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2853 ip_policy, recv_ill, zoneid); 2854 return; 2855 } 2856 /* NOTREACHED */ 2857 drop_pkt:; 2858 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2859 freemsg(first_mp); 2860 } 2861 2862 /* 2863 * Common IP options parser. 2864 * 2865 * Setup routine: fill in *optp with options-parsing state, then 2866 * tail-call ipoptp_next to return the first option. 2867 */ 2868 uint8_t 2869 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2870 { 2871 uint32_t totallen; /* total length of all options */ 2872 2873 totallen = ipha->ipha_version_and_hdr_length - 2874 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2875 totallen <<= 2; 2876 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2877 optp->ipoptp_end = optp->ipoptp_next + totallen; 2878 optp->ipoptp_flags = 0; 2879 return (ipoptp_next(optp)); 2880 } 2881 2882 /* 2883 * Common IP options parser: extract next option. 2884 */ 2885 uint8_t 2886 ipoptp_next(ipoptp_t *optp) 2887 { 2888 uint8_t *end = optp->ipoptp_end; 2889 uint8_t *cur = optp->ipoptp_next; 2890 uint8_t opt, len, pointer; 2891 2892 /* 2893 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2894 * has been corrupted. 2895 */ 2896 ASSERT(cur <= end); 2897 2898 if (cur == end) 2899 return (IPOPT_EOL); 2900 2901 opt = cur[IPOPT_OPTVAL]; 2902 2903 /* 2904 * Skip any NOP options. 2905 */ 2906 while (opt == IPOPT_NOP) { 2907 cur++; 2908 if (cur == end) 2909 return (IPOPT_EOL); 2910 opt = cur[IPOPT_OPTVAL]; 2911 } 2912 2913 if (opt == IPOPT_EOL) 2914 return (IPOPT_EOL); 2915 2916 /* 2917 * Option requiring a length. 2918 */ 2919 if ((cur + 1) >= end) { 2920 optp->ipoptp_flags |= IPOPTP_ERROR; 2921 return (IPOPT_EOL); 2922 } 2923 len = cur[IPOPT_OLEN]; 2924 if (len < 2) { 2925 optp->ipoptp_flags |= IPOPTP_ERROR; 2926 return (IPOPT_EOL); 2927 } 2928 optp->ipoptp_cur = cur; 2929 optp->ipoptp_len = len; 2930 optp->ipoptp_next = cur + len; 2931 if (cur + len > end) { 2932 optp->ipoptp_flags |= IPOPTP_ERROR; 2933 return (IPOPT_EOL); 2934 } 2935 2936 /* 2937 * For the options which require a pointer field, make sure 2938 * its there, and make sure it points to either something 2939 * inside this option, or the end of the option. 2940 */ 2941 switch (opt) { 2942 case IPOPT_RR: 2943 case IPOPT_TS: 2944 case IPOPT_LSRR: 2945 case IPOPT_SSRR: 2946 if (len <= IPOPT_OFFSET) { 2947 optp->ipoptp_flags |= IPOPTP_ERROR; 2948 return (opt); 2949 } 2950 pointer = cur[IPOPT_OFFSET]; 2951 if (pointer - 1 > len) { 2952 optp->ipoptp_flags |= IPOPTP_ERROR; 2953 return (opt); 2954 } 2955 break; 2956 } 2957 2958 /* 2959 * Sanity check the pointer field based on the type of the 2960 * option. 2961 */ 2962 switch (opt) { 2963 case IPOPT_RR: 2964 case IPOPT_SSRR: 2965 case IPOPT_LSRR: 2966 if (pointer < IPOPT_MINOFF_SR) 2967 optp->ipoptp_flags |= IPOPTP_ERROR; 2968 break; 2969 case IPOPT_TS: 2970 if (pointer < IPOPT_MINOFF_IT) 2971 optp->ipoptp_flags |= IPOPTP_ERROR; 2972 /* 2973 * Note that the Internet Timestamp option also 2974 * contains two four bit fields (the Overflow field, 2975 * and the Flag field), which follow the pointer 2976 * field. We don't need to check that these fields 2977 * fall within the length of the option because this 2978 * was implicitely done above. We've checked that the 2979 * pointer value is at least IPOPT_MINOFF_IT, and that 2980 * it falls within the option. Since IPOPT_MINOFF_IT > 2981 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2982 */ 2983 ASSERT(len > IPOPT_POS_OV_FLG); 2984 break; 2985 } 2986 2987 return (opt); 2988 } 2989 2990 /* 2991 * Use the outgoing IP header to create an IP_OPTIONS option the way 2992 * it was passed down from the application. 2993 */ 2994 int 2995 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) 2996 { 2997 ipoptp_t opts; 2998 const uchar_t *opt; 2999 uint8_t optval; 3000 uint8_t optlen; 3001 uint32_t len = 0; 3002 uchar_t *buf1 = buf; 3003 3004 buf += IP_ADDR_LEN; /* Leave room for final destination */ 3005 len += IP_ADDR_LEN; 3006 bzero(buf1, IP_ADDR_LEN); 3007 3008 /* 3009 * OK to cast away const here, as we don't store through the returned 3010 * opts.ipoptp_cur pointer. 3011 */ 3012 for (optval = ipoptp_first(&opts, (ipha_t *)ipha); 3013 optval != IPOPT_EOL; 3014 optval = ipoptp_next(&opts)) { 3015 int off; 3016 3017 opt = opts.ipoptp_cur; 3018 optlen = opts.ipoptp_len; 3019 switch (optval) { 3020 case IPOPT_SSRR: 3021 case IPOPT_LSRR: 3022 3023 /* 3024 * Insert ipha_dst as the first entry in the source 3025 * route and move down the entries on step. 3026 * The last entry gets placed at buf1. 3027 */ 3028 buf[IPOPT_OPTVAL] = optval; 3029 buf[IPOPT_OLEN] = optlen; 3030 buf[IPOPT_OFFSET] = optlen; 3031 3032 off = optlen - IP_ADDR_LEN; 3033 if (off < 0) { 3034 /* No entries in source route */ 3035 break; 3036 } 3037 /* Last entry in source route */ 3038 bcopy(opt + off, buf1, IP_ADDR_LEN); 3039 off -= IP_ADDR_LEN; 3040 3041 while (off > 0) { 3042 bcopy(opt + off, 3043 buf + off + IP_ADDR_LEN, 3044 IP_ADDR_LEN); 3045 off -= IP_ADDR_LEN; 3046 } 3047 /* ipha_dst into first slot */ 3048 bcopy(&ipha->ipha_dst, 3049 buf + off + IP_ADDR_LEN, 3050 IP_ADDR_LEN); 3051 buf += optlen; 3052 len += optlen; 3053 break; 3054 3055 case IPOPT_COMSEC: 3056 case IPOPT_SECURITY: 3057 /* if passing up a label is not ok, then remove */ 3058 if (is_system_labeled()) 3059 break; 3060 /* FALLTHROUGH */ 3061 default: 3062 bcopy(opt, buf, optlen); 3063 buf += optlen; 3064 len += optlen; 3065 break; 3066 } 3067 } 3068 done: 3069 /* Pad the resulting options */ 3070 while (len & 0x3) { 3071 *buf++ = IPOPT_EOL; 3072 len++; 3073 } 3074 return (len); 3075 } 3076 3077 /* 3078 * Update any record route or timestamp options to include this host. 3079 * Reverse any source route option. 3080 * This routine assumes that the options are well formed i.e. that they 3081 * have already been checked. 3082 */ 3083 static void 3084 icmp_options_update(ipha_t *ipha) 3085 { 3086 ipoptp_t opts; 3087 uchar_t *opt; 3088 uint8_t optval; 3089 ipaddr_t src; /* Our local address */ 3090 ipaddr_t dst; 3091 3092 ip2dbg(("icmp_options_update\n")); 3093 src = ipha->ipha_src; 3094 dst = ipha->ipha_dst; 3095 3096 for (optval = ipoptp_first(&opts, ipha); 3097 optval != IPOPT_EOL; 3098 optval = ipoptp_next(&opts)) { 3099 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 3100 opt = opts.ipoptp_cur; 3101 ip2dbg(("icmp_options_update: opt %d, len %d\n", 3102 optval, opts.ipoptp_len)); 3103 switch (optval) { 3104 int off1, off2; 3105 case IPOPT_SSRR: 3106 case IPOPT_LSRR: 3107 /* 3108 * Reverse the source route. The first entry 3109 * should be the next to last one in the current 3110 * source route (the last entry is our address). 3111 * The last entry should be the final destination. 3112 */ 3113 off1 = IPOPT_MINOFF_SR - 1; 3114 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 3115 if (off2 < 0) { 3116 /* No entries in source route */ 3117 ip1dbg(( 3118 "icmp_options_update: bad src route\n")); 3119 break; 3120 } 3121 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 3122 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 3123 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 3124 off2 -= IP_ADDR_LEN; 3125 3126 while (off1 < off2) { 3127 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 3128 bcopy((char *)opt + off2, (char *)opt + off1, 3129 IP_ADDR_LEN); 3130 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 3131 off1 += IP_ADDR_LEN; 3132 off2 -= IP_ADDR_LEN; 3133 } 3134 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 3135 break; 3136 } 3137 } 3138 } 3139 3140 /* 3141 * Process received ICMP Redirect messages. 3142 */ 3143 /* ARGSUSED */ 3144 static void 3145 icmp_redirect(mblk_t *mp) 3146 { 3147 ipha_t *ipha; 3148 int iph_hdr_length; 3149 icmph_t *icmph; 3150 ipha_t *ipha_err; 3151 ire_t *ire; 3152 ire_t *prev_ire; 3153 ire_t *save_ire; 3154 ipaddr_t src, dst, gateway; 3155 iulp_t ulp_info = { 0 }; 3156 int error; 3157 3158 ipha = (ipha_t *)mp->b_rptr; 3159 iph_hdr_length = IPH_HDR_LENGTH(ipha); 3160 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 3161 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 3162 BUMP_MIB(&icmp_mib, icmpInErrors); 3163 freemsg(mp); 3164 return; 3165 } 3166 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 3167 ipha_err = (ipha_t *)&icmph[1]; 3168 src = ipha->ipha_src; 3169 dst = ipha_err->ipha_dst; 3170 gateway = icmph->icmph_rd_gateway; 3171 /* Make sure the new gateway is reachable somehow. */ 3172 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 3173 ALL_ZONES, NULL, MATCH_IRE_TYPE); 3174 /* 3175 * Make sure we had a route for the dest in question and that 3176 * that route was pointing to the old gateway (the source of the 3177 * redirect packet.) 3178 */ 3179 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 3180 NULL, MATCH_IRE_GW); 3181 /* 3182 * Check that 3183 * the redirect was not from ourselves 3184 * the new gateway and the old gateway are directly reachable 3185 */ 3186 if (!prev_ire || 3187 !ire || 3188 ire->ire_type == IRE_LOCAL) { 3189 BUMP_MIB(&icmp_mib, icmpInBadRedirects); 3190 freemsg(mp); 3191 if (ire != NULL) 3192 ire_refrele(ire); 3193 if (prev_ire != NULL) 3194 ire_refrele(prev_ire); 3195 return; 3196 } 3197 3198 /* 3199 * Should we use the old ULP info to create the new gateway? From 3200 * a user's perspective, we should inherit the info so that it 3201 * is a "smooth" transition. If we do not do that, then new 3202 * connections going thru the new gateway will have no route metrics, 3203 * which is counter-intuitive to user. From a network point of 3204 * view, this may or may not make sense even though the new gateway 3205 * is still directly connected to us so the route metrics should not 3206 * change much. 3207 * 3208 * But if the old ire_uinfo is not initialized, we do another 3209 * recursive lookup on the dest using the new gateway. There may 3210 * be a route to that. If so, use it to initialize the redirect 3211 * route. 3212 */ 3213 if (prev_ire->ire_uinfo.iulp_set) { 3214 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3215 } else { 3216 ire_t *tmp_ire; 3217 ire_t *sire; 3218 3219 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 3220 ALL_ZONES, 0, NULL, 3221 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT)); 3222 if (sire != NULL) { 3223 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3224 /* 3225 * If sire != NULL, ire_ftable_lookup() should not 3226 * return a NULL value. 3227 */ 3228 ASSERT(tmp_ire != NULL); 3229 ire_refrele(tmp_ire); 3230 ire_refrele(sire); 3231 } else if (tmp_ire != NULL) { 3232 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 3233 sizeof (iulp_t)); 3234 ire_refrele(tmp_ire); 3235 } 3236 } 3237 if (prev_ire->ire_type == IRE_CACHE) 3238 ire_delete(prev_ire); 3239 ire_refrele(prev_ire); 3240 /* 3241 * TODO: more precise handling for cases 0, 2, 3, the latter two 3242 * require TOS routing 3243 */ 3244 switch (icmph->icmph_code) { 3245 case 0: 3246 case 1: 3247 /* TODO: TOS specificity for cases 2 and 3 */ 3248 case 2: 3249 case 3: 3250 break; 3251 default: 3252 freemsg(mp); 3253 BUMP_MIB(&icmp_mib, icmpInBadRedirects); 3254 ire_refrele(ire); 3255 return; 3256 } 3257 /* 3258 * Create a Route Association. This will allow us to remember that 3259 * someone we believe told us to use the particular gateway. 3260 */ 3261 save_ire = ire; 3262 ire = ire_create( 3263 (uchar_t *)&dst, /* dest addr */ 3264 (uchar_t *)&ip_g_all_ones, /* mask */ 3265 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 3266 (uchar_t *)&gateway, /* gateway addr */ 3267 NULL, /* no in_srcaddr */ 3268 &save_ire->ire_max_frag, /* max frag */ 3269 NULL, /* Fast Path header */ 3270 NULL, /* no rfq */ 3271 NULL, /* no stq */ 3272 IRE_HOST_REDIRECT, 3273 NULL, 3274 NULL, 3275 NULL, 3276 0, 3277 0, 3278 0, 3279 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 3280 &ulp_info, 3281 NULL, 3282 NULL); 3283 3284 if (ire == NULL) { 3285 freemsg(mp); 3286 ire_refrele(save_ire); 3287 return; 3288 } 3289 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); 3290 ire_refrele(save_ire); 3291 atomic_inc_32(&ip_redirect_cnt); 3292 3293 if (error == 0) { 3294 ire_refrele(ire); /* Held in ire_add_v4 */ 3295 /* tell routing sockets that we received a redirect */ 3296 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 3297 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 3298 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR)); 3299 } 3300 3301 /* 3302 * Delete any existing IRE_HOST_REDIRECT for this destination. 3303 * This together with the added IRE has the effect of 3304 * modifying an existing redirect. 3305 */ 3306 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST_REDIRECT, NULL, NULL, 3307 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE)); 3308 if (prev_ire) { 3309 ire_delete(prev_ire); 3310 ire_refrele(prev_ire); 3311 } 3312 3313 freemsg(mp); 3314 } 3315 3316 /* 3317 * Generate an ICMP parameter problem message. 3318 */ 3319 static void 3320 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid) 3321 { 3322 icmph_t icmph; 3323 boolean_t mctl_present; 3324 mblk_t *first_mp; 3325 3326 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3327 3328 if (!(mp = icmp_pkt_err_ok(mp))) { 3329 if (mctl_present) 3330 freeb(first_mp); 3331 return; 3332 } 3333 3334 bzero(&icmph, sizeof (icmph_t)); 3335 icmph.icmph_type = ICMP_PARAM_PROBLEM; 3336 icmph.icmph_pp_ptr = ptr; 3337 BUMP_MIB(&icmp_mib, icmpOutParmProbs); 3338 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid); 3339 } 3340 3341 /* 3342 * Build and ship an IPv4 ICMP message using the packet data in mp, and 3343 * the ICMP header pointed to by "stuff". (May be called as writer.) 3344 * Note: assumes that icmp_pkt_err_ok has been called to verify that 3345 * an icmp error packet can be sent. 3346 * Assigns an appropriate source address to the packet. If ipha_dst is 3347 * one of our addresses use it for source. Otherwise pick a source based 3348 * on a route lookup back to ipha_src. 3349 * Note that ipha_src must be set here since the 3350 * packet is likely to arrive on an ill queue in ip_wput() which will 3351 * not set a source address. 3352 */ 3353 static void 3354 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, 3355 boolean_t mctl_present, zoneid_t zoneid) 3356 { 3357 ipaddr_t dst; 3358 icmph_t *icmph; 3359 ipha_t *ipha; 3360 uint_t len_needed; 3361 size_t msg_len; 3362 mblk_t *mp1; 3363 ipaddr_t src; 3364 ire_t *ire; 3365 mblk_t *ipsec_mp; 3366 ipsec_out_t *io = NULL; 3367 boolean_t xmit_if_on = B_FALSE; 3368 3369 if (mctl_present) { 3370 /* 3371 * If it is : 3372 * 3373 * 1) a IPSEC_OUT, then this is caused by outbound 3374 * datagram originating on this host. IPSEC processing 3375 * may or may not have been done. Refer to comments above 3376 * icmp_inbound_error_fanout for details. 3377 * 3378 * 2) a IPSEC_IN if we are generating a icmp_message 3379 * for an incoming datagram destined for us i.e called 3380 * from ip_fanout_send_icmp. 3381 */ 3382 ipsec_info_t *in; 3383 ipsec_mp = mp; 3384 mp = ipsec_mp->b_cont; 3385 3386 in = (ipsec_info_t *)ipsec_mp->b_rptr; 3387 ipha = (ipha_t *)mp->b_rptr; 3388 3389 ASSERT(in->ipsec_info_type == IPSEC_OUT || 3390 in->ipsec_info_type == IPSEC_IN); 3391 3392 if (in->ipsec_info_type == IPSEC_IN) { 3393 /* 3394 * Convert the IPSEC_IN to IPSEC_OUT. 3395 */ 3396 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3397 BUMP_MIB(&ip_mib, ipOutDiscards); 3398 return; 3399 } 3400 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3401 } else { 3402 ASSERT(in->ipsec_info_type == IPSEC_OUT); 3403 io = (ipsec_out_t *)in; 3404 if (io->ipsec_out_xmit_if) 3405 xmit_if_on = B_TRUE; 3406 /* 3407 * Clear out ipsec_out_proc_begin, so we do a fresh 3408 * ire lookup. 3409 */ 3410 io->ipsec_out_proc_begin = B_FALSE; 3411 } 3412 ASSERT(zoneid == io->ipsec_out_zoneid); 3413 ASSERT(zoneid != ALL_ZONES); 3414 } else { 3415 /* 3416 * This is in clear. The icmp message we are building 3417 * here should go out in clear. 3418 * 3419 * Pardon the convolution of it all, but it's easier to 3420 * allocate a "use cleartext" IPSEC_IN message and convert 3421 * it than it is to allocate a new one. 3422 */ 3423 ipsec_in_t *ii; 3424 ASSERT(DB_TYPE(mp) == M_DATA); 3425 if ((ipsec_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 3426 freemsg(mp); 3427 BUMP_MIB(&ip_mib, ipOutDiscards); 3428 return; 3429 } 3430 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3431 3432 /* This is not a secure packet */ 3433 ii->ipsec_in_secure = B_FALSE; 3434 /* 3435 * For trusted extensions using a shared IP address we can 3436 * send using any zoneid. 3437 */ 3438 if (zoneid == ALL_ZONES) 3439 ii->ipsec_in_zoneid = GLOBAL_ZONEID; 3440 else 3441 ii->ipsec_in_zoneid = zoneid; 3442 ipsec_mp->b_cont = mp; 3443 ipha = (ipha_t *)mp->b_rptr; 3444 /* 3445 * Convert the IPSEC_IN to IPSEC_OUT. 3446 */ 3447 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3448 BUMP_MIB(&ip_mib, ipOutDiscards); 3449 return; 3450 } 3451 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3452 } 3453 3454 /* Remember our eventual destination */ 3455 dst = ipha->ipha_src; 3456 3457 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 3458 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE); 3459 if (ire != NULL && 3460 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) { 3461 src = ipha->ipha_dst; 3462 } else if (!xmit_if_on) { 3463 if (ire != NULL) 3464 ire_refrele(ire); 3465 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL, 3466 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY)); 3467 if (ire == NULL) { 3468 BUMP_MIB(&ip_mib, ipOutNoRoutes); 3469 freemsg(ipsec_mp); 3470 return; 3471 } 3472 src = ire->ire_src_addr; 3473 } else { 3474 ipif_t *ipif = NULL; 3475 ill_t *ill; 3476 /* 3477 * This must be an ICMP error coming from 3478 * ip_mrtun_forward(). The src addr should 3479 * be equal to the IP-addr of the outgoing 3480 * interface. 3481 */ 3482 if (io == NULL) { 3483 /* This is not a IPSEC_OUT type control msg */ 3484 BUMP_MIB(&ip_mib, ipOutNoRoutes); 3485 freemsg(ipsec_mp); 3486 return; 3487 } 3488 ill = ill_lookup_on_ifindex(io->ipsec_out_ill_index, B_FALSE, 3489 NULL, NULL, NULL, NULL); 3490 if (ill != NULL) { 3491 ipif = ipif_get_next_ipif(NULL, ill); 3492 ill_refrele(ill); 3493 } 3494 if (ipif == NULL) { 3495 BUMP_MIB(&ip_mib, ipOutNoRoutes); 3496 freemsg(ipsec_mp); 3497 return; 3498 } 3499 src = ipif->ipif_src_addr; 3500 ipif_refrele(ipif); 3501 } 3502 3503 if (ire != NULL) 3504 ire_refrele(ire); 3505 3506 /* 3507 * Check if we can send back more then 8 bytes in addition 3508 * to the IP header. We will include as much as 64 bytes. 3509 */ 3510 len_needed = IPH_HDR_LENGTH(ipha); 3511 if (ipha->ipha_protocol == IPPROTO_ENCAP && 3512 (uchar_t *)ipha + len_needed + 1 <= mp->b_wptr) { 3513 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + len_needed)); 3514 } 3515 len_needed += ip_icmp_return; 3516 msg_len = msgdsize(mp); 3517 if (msg_len > len_needed) { 3518 (void) adjmsg(mp, len_needed - msg_len); 3519 msg_len = len_needed; 3520 } 3521 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_HI); 3522 if (mp1 == NULL) { 3523 BUMP_MIB(&icmp_mib, icmpOutErrors); 3524 freemsg(ipsec_mp); 3525 return; 3526 } 3527 /* 3528 * On an unlabeled system, dblks don't necessarily have creds. 3529 */ 3530 ASSERT(!is_system_labeled() || DB_CRED(mp) != NULL); 3531 if (DB_CRED(mp) != NULL) 3532 mblk_setcred(mp1, DB_CRED(mp)); 3533 mp1->b_cont = mp; 3534 mp = mp1; 3535 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 3536 ipsec_mp->b_rptr == (uint8_t *)io && 3537 io->ipsec_out_type == IPSEC_OUT); 3538 ipsec_mp->b_cont = mp; 3539 3540 /* 3541 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 3542 * node generates be accepted in peace by all on-host destinations. 3543 * If we do NOT assume that all on-host destinations trust 3544 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 3545 * (Look for ipsec_out_icmp_loopback). 3546 */ 3547 io->ipsec_out_icmp_loopback = B_TRUE; 3548 3549 ipha = (ipha_t *)mp->b_rptr; 3550 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 3551 *ipha = icmp_ipha; 3552 ipha->ipha_src = src; 3553 ipha->ipha_dst = dst; 3554 ipha->ipha_ttl = ip_def_ttl; 3555 msg_len += sizeof (icmp_ipha) + len; 3556 if (msg_len > IP_MAXPACKET) { 3557 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 3558 msg_len = IP_MAXPACKET; 3559 } 3560 ipha->ipha_length = htons((uint16_t)msg_len); 3561 icmph = (icmph_t *)&ipha[1]; 3562 bcopy(stuff, icmph, len); 3563 icmph->icmph_checksum = 0; 3564 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 3565 if (icmph->icmph_checksum == 0) 3566 icmph->icmph_checksum = 0xFFFF; 3567 BUMP_MIB(&icmp_mib, icmpOutMsgs); 3568 put(q, ipsec_mp); 3569 } 3570 3571 /* 3572 * Determine if an ICMP error packet can be sent given the rate limit. 3573 * The limit consists of an average frequency (icmp_pkt_err_interval measured 3574 * in milliseconds) and a burst size. Burst size number of packets can 3575 * be sent arbitrarely closely spaced. 3576 * The state is tracked using two variables to implement an approximate 3577 * token bucket filter: 3578 * icmp_pkt_err_last - lbolt value when the last burst started 3579 * icmp_pkt_err_sent - number of packets sent in current burst 3580 */ 3581 boolean_t 3582 icmp_err_rate_limit(void) 3583 { 3584 clock_t now = TICK_TO_MSEC(lbolt); 3585 uint_t refilled; /* Number of packets refilled in tbf since last */ 3586 uint_t err_interval = ip_icmp_err_interval; /* Guard against changes */ 3587 3588 if (err_interval == 0) 3589 return (B_FALSE); 3590 3591 if (icmp_pkt_err_last > now) { 3592 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 3593 icmp_pkt_err_last = 0; 3594 icmp_pkt_err_sent = 0; 3595 } 3596 /* 3597 * If we are in a burst update the token bucket filter. 3598 * Update the "last" time to be close to "now" but make sure 3599 * we don't loose precision. 3600 */ 3601 if (icmp_pkt_err_sent != 0) { 3602 refilled = (now - icmp_pkt_err_last)/err_interval; 3603 if (refilled > icmp_pkt_err_sent) { 3604 icmp_pkt_err_sent = 0; 3605 } else { 3606 icmp_pkt_err_sent -= refilled; 3607 icmp_pkt_err_last += refilled * err_interval; 3608 } 3609 } 3610 if (icmp_pkt_err_sent == 0) { 3611 /* Start of new burst */ 3612 icmp_pkt_err_last = now; 3613 } 3614 if (icmp_pkt_err_sent < ip_icmp_err_burst) { 3615 icmp_pkt_err_sent++; 3616 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 3617 icmp_pkt_err_sent)); 3618 return (B_FALSE); 3619 } 3620 ip1dbg(("icmp_err_rate_limit: dropped\n")); 3621 return (B_TRUE); 3622 } 3623 3624 /* 3625 * Check if it is ok to send an IPv4 ICMP error packet in 3626 * response to the IPv4 packet in mp. 3627 * Free the message and return null if no 3628 * ICMP error packet should be sent. 3629 */ 3630 static mblk_t * 3631 icmp_pkt_err_ok(mblk_t *mp) 3632 { 3633 icmph_t *icmph; 3634 ipha_t *ipha; 3635 uint_t len_needed; 3636 ire_t *src_ire; 3637 ire_t *dst_ire; 3638 3639 if (!mp) 3640 return (NULL); 3641 ipha = (ipha_t *)mp->b_rptr; 3642 if (ip_csum_hdr(ipha)) { 3643 BUMP_MIB(&ip_mib, ipInCksumErrs); 3644 freemsg(mp); 3645 return (NULL); 3646 } 3647 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, 3648 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 3649 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, 3650 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 3651 if (src_ire != NULL || dst_ire != NULL || 3652 CLASSD(ipha->ipha_dst) || 3653 CLASSD(ipha->ipha_src) || 3654 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3655 /* Note: only errors to the fragment with offset 0 */ 3656 BUMP_MIB(&icmp_mib, icmpOutDrops); 3657 freemsg(mp); 3658 if (src_ire != NULL) 3659 ire_refrele(src_ire); 3660 if (dst_ire != NULL) 3661 ire_refrele(dst_ire); 3662 return (NULL); 3663 } 3664 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3665 /* 3666 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3667 * errors in response to any ICMP errors. 3668 */ 3669 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3670 if (mp->b_wptr - mp->b_rptr < len_needed) { 3671 if (!pullupmsg(mp, len_needed)) { 3672 BUMP_MIB(&icmp_mib, icmpInErrors); 3673 freemsg(mp); 3674 return (NULL); 3675 } 3676 ipha = (ipha_t *)mp->b_rptr; 3677 } 3678 icmph = (icmph_t *) 3679 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3680 switch (icmph->icmph_type) { 3681 case ICMP_DEST_UNREACHABLE: 3682 case ICMP_SOURCE_QUENCH: 3683 case ICMP_TIME_EXCEEDED: 3684 case ICMP_PARAM_PROBLEM: 3685 case ICMP_REDIRECT: 3686 BUMP_MIB(&icmp_mib, icmpOutDrops); 3687 freemsg(mp); 3688 return (NULL); 3689 default: 3690 break; 3691 } 3692 } 3693 /* 3694 * If this is a labeled system, then check to see if we're allowed to 3695 * send a response to this particular sender. If not, then just drop. 3696 */ 3697 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 3698 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); 3699 BUMP_MIB(&icmp_mib, icmpOutDrops); 3700 freemsg(mp); 3701 return (NULL); 3702 } 3703 if (icmp_err_rate_limit()) { 3704 /* 3705 * Only send ICMP error packets every so often. 3706 * This should be done on a per port/source basis, 3707 * but for now this will suffice. 3708 */ 3709 freemsg(mp); 3710 return (NULL); 3711 } 3712 return (mp); 3713 } 3714 3715 /* 3716 * Generate an ICMP redirect message. 3717 */ 3718 static void 3719 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway) 3720 { 3721 icmph_t icmph; 3722 3723 /* 3724 * We are called from ip_rput where we could 3725 * not have attached an IPSEC_IN. 3726 */ 3727 ASSERT(mp->b_datap->db_type == M_DATA); 3728 3729 if (!(mp = icmp_pkt_err_ok(mp))) { 3730 return; 3731 } 3732 3733 bzero(&icmph, sizeof (icmph_t)); 3734 icmph.icmph_type = ICMP_REDIRECT; 3735 icmph.icmph_code = 1; 3736 icmph.icmph_rd_gateway = gateway; 3737 BUMP_MIB(&icmp_mib, icmpOutRedirects); 3738 /* Redirects sent by router, and router is global zone */ 3739 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID); 3740 } 3741 3742 /* 3743 * Generate an ICMP time exceeded message. 3744 */ 3745 void 3746 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid) 3747 { 3748 icmph_t icmph; 3749 boolean_t mctl_present; 3750 mblk_t *first_mp; 3751 3752 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3753 3754 if (!(mp = icmp_pkt_err_ok(mp))) { 3755 if (mctl_present) 3756 freeb(first_mp); 3757 return; 3758 } 3759 3760 bzero(&icmph, sizeof (icmph_t)); 3761 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3762 icmph.icmph_code = code; 3763 BUMP_MIB(&icmp_mib, icmpOutTimeExcds); 3764 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid); 3765 } 3766 3767 /* 3768 * Generate an ICMP unreachable message. 3769 */ 3770 void 3771 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid) 3772 { 3773 icmph_t icmph; 3774 mblk_t *first_mp; 3775 boolean_t mctl_present; 3776 3777 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3778 3779 if (!(mp = icmp_pkt_err_ok(mp))) { 3780 if (mctl_present) 3781 freeb(first_mp); 3782 return; 3783 } 3784 3785 bzero(&icmph, sizeof (icmph_t)); 3786 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3787 icmph.icmph_code = code; 3788 BUMP_MIB(&icmp_mib, icmpOutDestUnreachs); 3789 ip2dbg(("send icmp destination unreachable code %d\n", code)); 3790 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present, 3791 zoneid); 3792 } 3793 3794 /* 3795 * Attempt to start recovery of an IPv4 interface that's been shut down as a 3796 * duplicate. As long as someone else holds the address, the interface will 3797 * stay down. When that conflict goes away, the interface is brought back up. 3798 * This is done so that accidental shutdowns of addresses aren't made 3799 * permanent. Your server will recover from a failure. 3800 * 3801 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a 3802 * user space process (dhcpagent). 3803 * 3804 * Recovery completes if ARP reports that the address is now ours (via 3805 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation. 3806 * 3807 * This function is entered on a timer expiry; the ID is in ipif_recovery_id. 3808 */ 3809 static void 3810 ipif_dup_recovery(void *arg) 3811 { 3812 ipif_t *ipif = arg; 3813 ill_t *ill = ipif->ipif_ill; 3814 mblk_t *arp_add_mp; 3815 mblk_t *arp_del_mp; 3816 area_t *area; 3817 3818 ipif->ipif_recovery_id = 0; 3819 3820 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || 3821 (ipif->ipif_flags & IPIF_POINTOPOINT)) { 3822 /* No reason to try to bring this address back. */ 3823 return; 3824 } 3825 3826 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL) 3827 goto alloc_fail; 3828 3829 if (ipif->ipif_arp_del_mp == NULL) { 3830 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) 3831 goto alloc_fail; 3832 ipif->ipif_arp_del_mp = arp_del_mp; 3833 } 3834 3835 /* Setting the 'unverified' flag restarts DAD */ 3836 area = (area_t *)arp_add_mp->b_rptr; 3837 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | 3838 ACE_F_UNVERIFIED; 3839 putnext(ill->ill_rq, arp_add_mp); 3840 return; 3841 3842 alloc_fail: 3843 /* On allocation failure, just restart the timer */ 3844 freemsg(arp_add_mp); 3845 if (ip_dup_recovery > 0) { 3846 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, 3847 MSEC_TO_TICK(ip_dup_recovery)); 3848 } 3849 } 3850 3851 /* 3852 * This is for exclusive changes due to ARP. Either tear down an interface due 3853 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. 3854 */ 3855 /* ARGSUSED */ 3856 static void 3857 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3858 { 3859 ill_t *ill = rq->q_ptr; 3860 arh_t *arh; 3861 ipaddr_t src; 3862 ipif_t *ipif; 3863 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ 3864 char hbuf[MAC_STR_LEN]; 3865 char sbuf[INET_ADDRSTRLEN]; 3866 const char *failtype; 3867 boolean_t bring_up; 3868 3869 switch (((arcn_t *)mp->b_rptr)->arcn_code) { 3870 case AR_CN_READY: 3871 failtype = NULL; 3872 bring_up = B_TRUE; 3873 break; 3874 case AR_CN_FAILED: 3875 failtype = "in use"; 3876 bring_up = B_FALSE; 3877 break; 3878 default: 3879 failtype = "claimed"; 3880 bring_up = B_FALSE; 3881 break; 3882 } 3883 3884 arh = (arh_t *)mp->b_cont->b_rptr; 3885 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3886 3887 /* Handle failures due to probes */ 3888 if (src == 0) { 3889 bcopy((char *)&arh[1] + 2 * arh->arh_hlen + IP_ADDR_LEN, &src, 3890 IP_ADDR_LEN); 3891 } 3892 3893 (void) strlcpy(ibuf, ill->ill_name, sizeof (ibuf)); 3894 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, 3895 sizeof (hbuf)); 3896 (void) ip_dot_addr(src, sbuf); 3897 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3898 3899 if ((ipif->ipif_flags & IPIF_POINTOPOINT) || 3900 ipif->ipif_lcl_addr != src) { 3901 continue; 3902 } 3903 3904 /* 3905 * If we failed on a recovery probe, then restart the timer to 3906 * try again later. 3907 */ 3908 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) && 3909 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3910 ill->ill_net_type == IRE_IF_RESOLVER && 3911 ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0) { 3912 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3913 ipif, MSEC_TO_TICK(ip_dup_recovery)); 3914 continue; 3915 } 3916 3917 /* 3918 * If what we're trying to do has already been done, then do 3919 * nothing. 3920 */ 3921 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0)) 3922 continue; 3923 3924 if (ipif->ipif_id != 0) { 3925 (void) snprintf(ibuf + ill->ill_name_length - 1, 3926 sizeof (ibuf) - ill->ill_name_length + 1, ":%d", 3927 ipif->ipif_id); 3928 } 3929 if (failtype == NULL) { 3930 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf, 3931 ibuf); 3932 } else { 3933 cmn_err(CE_WARN, "%s has duplicate address %s (%s " 3934 "by %s); disabled", ibuf, sbuf, failtype, hbuf); 3935 } 3936 3937 if (bring_up) { 3938 ASSERT(ill->ill_dl_up); 3939 /* 3940 * Free up the ARP delete message so we can allocate 3941 * a fresh one through the normal path. 3942 */ 3943 freemsg(ipif->ipif_arp_del_mp); 3944 ipif->ipif_arp_del_mp = NULL; 3945 if (ipif_resolver_up(ipif, Res_act_initial) != 3946 EINPROGRESS) { 3947 ipif->ipif_addr_ready = 1; 3948 (void) ipif_up_done(ipif); 3949 } 3950 continue; 3951 } 3952 3953 mutex_enter(&ill->ill_lock); 3954 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 3955 ipif->ipif_flags |= IPIF_DUPLICATE; 3956 ill->ill_ipif_dup_count++; 3957 mutex_exit(&ill->ill_lock); 3958 /* 3959 * Already exclusive on the ill; no need to handle deferred 3960 * processing here. 3961 */ 3962 (void) ipif_down(ipif, NULL, NULL); 3963 ipif_down_tail(ipif); 3964 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3965 ill->ill_net_type == IRE_IF_RESOLVER && 3966 ip_dup_recovery > 0) { 3967 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3968 ipif, MSEC_TO_TICK(ip_dup_recovery)); 3969 } 3970 } 3971 freemsg(mp); 3972 } 3973 3974 /* ARGSUSED */ 3975 static void 3976 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3977 { 3978 ill_t *ill = rq->q_ptr; 3979 arh_t *arh; 3980 ipaddr_t src; 3981 ipif_t *ipif; 3982 3983 arh = (arh_t *)mp->b_cont->b_rptr; 3984 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3985 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3986 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) 3987 (void) ipif_resolver_up(ipif, Res_act_defend); 3988 } 3989 freemsg(mp); 3990 } 3991 3992 /* 3993 * News from ARP. ARP sends notification of interesting events down 3994 * to its clients using M_CTL messages with the interesting ARP packet 3995 * attached via b_cont. 3996 * The interesting event from a device comes up the corresponding ARP-IP-DEV 3997 * queue as opposed to ARP sending the message to all the clients, i.e. all 3998 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 3999 * table if a cache IRE is found to delete all the entries for the address in 4000 * the packet. 4001 */ 4002 static void 4003 ip_arp_news(queue_t *q, mblk_t *mp) 4004 { 4005 arcn_t *arcn; 4006 arh_t *arh; 4007 ire_t *ire = NULL; 4008 char hbuf[MAC_STR_LEN]; 4009 char sbuf[INET_ADDRSTRLEN]; 4010 ipaddr_t src; 4011 in6_addr_t v6src; 4012 boolean_t isv6 = B_FALSE; 4013 ipif_t *ipif; 4014 ill_t *ill; 4015 4016 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 4017 if (q->q_next) { 4018 putnext(q, mp); 4019 } else 4020 freemsg(mp); 4021 return; 4022 } 4023 arh = (arh_t *)mp->b_cont->b_rptr; 4024 /* Is it one we are interested in? */ 4025 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 4026 isv6 = B_TRUE; 4027 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 4028 IPV6_ADDR_LEN); 4029 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 4030 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 4031 IP_ADDR_LEN); 4032 } else { 4033 freemsg(mp); 4034 return; 4035 } 4036 4037 ill = q->q_ptr; 4038 4039 arcn = (arcn_t *)mp->b_rptr; 4040 switch (arcn->arcn_code) { 4041 case AR_CN_BOGON: 4042 /* 4043 * Someone is sending ARP packets with a source protocol 4044 * address that we have published and for which we believe our 4045 * entry is authoritative and (when ill_arp_extend is set) 4046 * verified to be unique on the network. 4047 * 4048 * The ARP module internally handles the cases where the sender 4049 * is just probing (for DAD) and where the hardware address of 4050 * a non-authoritative entry has changed. Thus, these are the 4051 * real conflicts, and we have to do resolution. 4052 * 4053 * We back away quickly from the address if it's from DHCP or 4054 * otherwise temporary and hasn't been used recently (or at 4055 * all). We'd like to include "deprecated" addresses here as 4056 * well (as there's no real reason to defend something we're 4057 * discarding), but IPMP "reuses" this flag to mean something 4058 * other than the standard meaning. 4059 * 4060 * If the ARP module above is not extended (meaning that it 4061 * doesn't know how to defend the address), then we just log 4062 * the problem as we always did and continue on. It's not 4063 * right, but there's little else we can do, and those old ATM 4064 * users are going away anyway. 4065 */ 4066 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, 4067 hbuf, sizeof (hbuf)); 4068 (void) ip_dot_addr(src, sbuf); 4069 if (isv6) 4070 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL); 4071 else 4072 ire = ire_cache_lookup(src, ALL_ZONES, NULL); 4073 4074 if (ire != NULL && IRE_IS_LOCAL(ire)) { 4075 uint32_t now; 4076 uint32_t maxage; 4077 clock_t lused; 4078 uint_t maxdefense; 4079 uint_t defs; 4080 4081 /* 4082 * First, figure out if this address hasn't been used 4083 * in a while. If it hasn't, then it's a better 4084 * candidate for abandoning. 4085 */ 4086 ipif = ire->ire_ipif; 4087 ASSERT(ipif != NULL); 4088 now = gethrestime_sec(); 4089 maxage = now - ire->ire_create_time; 4090 if (maxage > ip_max_temp_idle) 4091 maxage = ip_max_temp_idle; 4092 lused = drv_hztousec(ddi_get_lbolt() - 4093 ire->ire_last_used_time) / MICROSEC + 1; 4094 if (lused >= maxage && (ipif->ipif_flags & 4095 (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) 4096 maxdefense = ip_max_temp_defend; 4097 else 4098 maxdefense = ip_max_defend; 4099 4100 /* 4101 * Now figure out how many times we've defended 4102 * ourselves. Ignore defenses that happened long in 4103 * the past. 4104 */ 4105 mutex_enter(&ire->ire_lock); 4106 if ((defs = ire->ire_defense_count) > 0 && 4107 now - ire->ire_defense_time > ip_defend_interval) { 4108 ire->ire_defense_count = defs = 0; 4109 } 4110 ire->ire_defense_count++; 4111 ire->ire_defense_time = now; 4112 mutex_exit(&ire->ire_lock); 4113 ill_refhold(ill); 4114 ire_refrele(ire); 4115 4116 /* 4117 * If we've defended ourselves too many times already, 4118 * then give up and tear down the interface(s) using 4119 * this address. Otherwise, defend by sending out a 4120 * gratuitous ARP. 4121 */ 4122 if (defs >= maxdefense && ill->ill_arp_extend) { 4123 (void) qwriter_ip(NULL, ill, q, mp, 4124 ip_arp_excl, CUR_OP, B_FALSE); 4125 } else { 4126 cmn_err(CE_WARN, 4127 "node %s is using our IP address %s on %s", 4128 hbuf, sbuf, ill->ill_name); 4129 /* 4130 * If this is an old (ATM) ARP module, then 4131 * don't try to defend the address. Remain 4132 * compatible with the old behavior. Defend 4133 * only with new ARP. 4134 */ 4135 if (ill->ill_arp_extend) { 4136 (void) qwriter_ip(NULL, ill, q, mp, 4137 ip_arp_defend, CUR_OP, B_FALSE); 4138 } else { 4139 ill_refrele(ill); 4140 } 4141 } 4142 return; 4143 } 4144 cmn_err(CE_WARN, 4145 "proxy ARP problem? Node '%s' is using %s on %s", 4146 hbuf, sbuf, ill->ill_name); 4147 if (ire != NULL) 4148 ire_refrele(ire); 4149 break; 4150 case AR_CN_ANNOUNCE: 4151 if (isv6) { 4152 /* 4153 * For XRESOLV interfaces. 4154 * Delete the IRE cache entry and NCE for this 4155 * v6 address 4156 */ 4157 ip_ire_clookup_and_delete_v6(&v6src); 4158 /* 4159 * If v6src is a non-zero, it's a router address 4160 * as below. Do the same sort of thing to clean 4161 * out off-net IRE_CACHE entries that go through 4162 * the router. 4163 */ 4164 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4165 ire_walk_v6(ire_delete_cache_gw_v6, 4166 (char *)&v6src, ALL_ZONES); 4167 } 4168 } else { 4169 nce_hw_map_t hwm; 4170 4171 /* 4172 * ARP gives us a copy of any packet where it thinks 4173 * the address has changed, so that we can update our 4174 * caches. We're responsible for caching known answers 4175 * in the current design. We check whether the 4176 * hardware address really has changed in all of our 4177 * entries that have cached this mapping, and if so, we 4178 * blow them away. This way we will immediately pick 4179 * up the rare case of a host changing hardware 4180 * address. 4181 */ 4182 if (src == 0) 4183 break; 4184 hwm.hwm_addr = src; 4185 hwm.hwm_hwlen = arh->arh_hlen; 4186 hwm.hwm_hwaddr = (uchar_t *)(arh + 1); 4187 ndp_walk_common(&ndp4, NULL, 4188 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); 4189 } 4190 break; 4191 case AR_CN_READY: 4192 /* No external v6 resolver has a contract to use this */ 4193 if (isv6) 4194 break; 4195 /* If the link is down, we'll retry this later */ 4196 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) 4197 break; 4198 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, 4199 NULL, NULL); 4200 if (ipif != NULL) { 4201 /* 4202 * If this is a duplicate recovery, then we now need to 4203 * go exclusive to bring this thing back up. 4204 */ 4205 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == 4206 IPIF_DUPLICATE) { 4207 ipif_refrele(ipif); 4208 ill_refhold(ill); 4209 (void) qwriter_ip(NULL, ill, q, mp, 4210 ip_arp_excl, CUR_OP, B_FALSE); 4211 return; 4212 } 4213 /* 4214 * If this is the first notice that this address is 4215 * ready, then let the user know now. 4216 */ 4217 if ((ipif->ipif_flags & IPIF_UP) && 4218 !ipif->ipif_addr_ready) { 4219 ipif_mask_reply(ipif); 4220 ip_rts_ifmsg(ipif); 4221 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 4222 sctp_update_ipif(ipif, SCTP_IPIF_UP); 4223 } 4224 ipif->ipif_addr_ready = 1; 4225 ipif_refrele(ipif); 4226 } 4227 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp)); 4228 if (ire != NULL) { 4229 ire->ire_defense_count = 0; 4230 ire_refrele(ire); 4231 } 4232 break; 4233 case AR_CN_FAILED: 4234 /* No external v6 resolver has a contract to use this */ 4235 if (isv6) 4236 break; 4237 ill_refhold(ill); 4238 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_excl, CUR_OP, 4239 B_FALSE); 4240 return; 4241 } 4242 freemsg(mp); 4243 } 4244 4245 /* 4246 * Create a mblk suitable for carrying the interface index and/or source link 4247 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 4248 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 4249 * application. 4250 */ 4251 mblk_t * 4252 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags) 4253 { 4254 mblk_t *mp; 4255 in_pktinfo_t *pinfo; 4256 ipha_t *ipha; 4257 struct ether_header *pether; 4258 4259 mp = allocb(sizeof (in_pktinfo_t), BPRI_MED); 4260 if (mp == NULL) { 4261 ip1dbg(("ip_add_info: allocation failure.\n")); 4262 return (data_mp); 4263 } 4264 4265 ipha = (ipha_t *)data_mp->b_rptr; 4266 pinfo = (in_pktinfo_t *)mp->b_rptr; 4267 bzero(pinfo, sizeof (in_pktinfo_t)); 4268 pinfo->in_pkt_flags = (uchar_t)flags; 4269 pinfo->in_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 4270 4271 if (flags & IPF_RECVIF) 4272 pinfo->in_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 4273 4274 pether = (struct ether_header *)((char *)ipha 4275 - sizeof (struct ether_header)); 4276 /* 4277 * Make sure the interface is an ethernet type, since this option 4278 * is currently supported only on this type of interface. Also make 4279 * sure we are pointing correctly above db_base. 4280 */ 4281 4282 if ((flags & IPF_RECVSLLA) && 4283 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 4284 (ill->ill_type == IFT_ETHER) && 4285 (ill->ill_net_type == IRE_IF_RESOLVER)) { 4286 4287 pinfo->in_pkt_slla.sdl_type = IFT_ETHER; 4288 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, 4289 (uchar_t *)pinfo->in_pkt_slla.sdl_data, ETHERADDRL); 4290 } else { 4291 /* 4292 * Clear the bit. Indicate to upper layer that IP is not 4293 * sending this ancillary info. 4294 */ 4295 pinfo->in_pkt_flags = pinfo->in_pkt_flags & ~IPF_RECVSLLA; 4296 } 4297 4298 mp->b_datap->db_type = M_CTL; 4299 mp->b_wptr += sizeof (in_pktinfo_t); 4300 mp->b_cont = data_mp; 4301 4302 return (mp); 4303 } 4304 4305 /* 4306 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 4307 * part of the bind request. 4308 */ 4309 4310 boolean_t 4311 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 4312 { 4313 ipsec_in_t *ii; 4314 4315 ASSERT(policy_mp != NULL); 4316 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 4317 4318 ii = (ipsec_in_t *)policy_mp->b_rptr; 4319 ASSERT(ii->ipsec_in_type == IPSEC_IN); 4320 4321 connp->conn_policy = ii->ipsec_in_policy; 4322 ii->ipsec_in_policy = NULL; 4323 4324 if (ii->ipsec_in_action != NULL) { 4325 if (connp->conn_latch == NULL) { 4326 connp->conn_latch = iplatch_create(); 4327 if (connp->conn_latch == NULL) 4328 return (B_FALSE); 4329 } 4330 ipsec_latch_inbound(connp->conn_latch, ii); 4331 } 4332 return (B_TRUE); 4333 } 4334 4335 /* 4336 * Upper level protocols (ULP) pass through bind requests to IP for inspection 4337 * and to arrange for power-fanout assist. The ULP is identified by 4338 * adding a single byte at the end of the original bind message. 4339 * A ULP other than UDP or TCP that wishes to be recognized passes 4340 * down a bind with a zero length address. 4341 * 4342 * The binding works as follows: 4343 * - A zero byte address means just bind to the protocol. 4344 * - A four byte address is treated as a request to validate 4345 * that the address is a valid local address, appropriate for 4346 * an application to bind to. This does not affect any fanout 4347 * information in IP. 4348 * - A sizeof sin_t byte address is used to bind to only the local address 4349 * and port. 4350 * - A sizeof ipa_conn_t byte address contains complete fanout information 4351 * consisting of local and remote addresses and ports. In 4352 * this case, the addresses are both validated as appropriate 4353 * for this operation, and, if so, the information is retained 4354 * for use in the inbound fanout. 4355 * 4356 * The ULP (except in the zero-length bind) can append an 4357 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 4358 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 4359 * a copy of the source or destination IRE (source for local bind; 4360 * destination for complete bind). IPSEC_POLICY_SET indicates that the 4361 * policy information contained should be copied on to the conn. 4362 * 4363 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 4364 */ 4365 mblk_t * 4366 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 4367 { 4368 ssize_t len; 4369 struct T_bind_req *tbr; 4370 sin_t *sin; 4371 ipa_conn_t *ac; 4372 uchar_t *ucp; 4373 mblk_t *mp1; 4374 boolean_t ire_requested; 4375 boolean_t ipsec_policy_set = B_FALSE; 4376 int error = 0; 4377 int protocol; 4378 ipa_conn_x_t *acx; 4379 4380 ASSERT(!connp->conn_af_isv6); 4381 connp->conn_pkt_isv6 = B_FALSE; 4382 4383 len = MBLKL(mp); 4384 if (len < (sizeof (*tbr) + 1)) { 4385 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 4386 "ip_bind: bogus msg, len %ld", len); 4387 /* XXX: Need to return something better */ 4388 goto bad_addr; 4389 } 4390 /* Back up and extract the protocol identifier. */ 4391 mp->b_wptr--; 4392 protocol = *mp->b_wptr & 0xFF; 4393 tbr = (struct T_bind_req *)mp->b_rptr; 4394 /* Reset the message type in preparation for shipping it back. */ 4395 DB_TYPE(mp) = M_PCPROTO; 4396 4397 connp->conn_ulp = (uint8_t)protocol; 4398 4399 /* 4400 * Check for a zero length address. This is from a protocol that 4401 * wants to register to receive all packets of its type. 4402 */ 4403 if (tbr->ADDR_length == 0) { 4404 /* 4405 * These protocols are now intercepted in ip_bind_v6(). 4406 * Reject protocol-level binds here for now. 4407 * 4408 * For SCTP raw socket, ICMP sends down a bind with sin_t 4409 * so that the protocol type cannot be SCTP. 4410 */ 4411 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 4412 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 4413 goto bad_addr; 4414 } 4415 4416 /* 4417 * 4418 * The udp module never sends down a zero-length address, 4419 * and allowing this on a labeled system will break MLP 4420 * functionality. 4421 */ 4422 if (is_system_labeled() && protocol == IPPROTO_UDP) 4423 goto bad_addr; 4424 4425 if (connp->conn_mac_exempt) 4426 goto bad_addr; 4427 4428 /* No hash here really. The table is big enough. */ 4429 connp->conn_srcv6 = ipv6_all_zeros; 4430 4431 ipcl_proto_insert(connp, protocol); 4432 4433 tbr->PRIM_type = T_BIND_ACK; 4434 return (mp); 4435 } 4436 4437 /* Extract the address pointer from the message. */ 4438 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 4439 tbr->ADDR_length); 4440 if (ucp == NULL) { 4441 ip1dbg(("ip_bind: no address\n")); 4442 goto bad_addr; 4443 } 4444 if (!OK_32PTR(ucp)) { 4445 ip1dbg(("ip_bind: unaligned address\n")); 4446 goto bad_addr; 4447 } 4448 /* 4449 * Check for trailing mps. 4450 */ 4451 4452 mp1 = mp->b_cont; 4453 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 4454 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); 4455 4456 switch (tbr->ADDR_length) { 4457 default: 4458 ip1dbg(("ip_bind: bad address length %d\n", 4459 (int)tbr->ADDR_length)); 4460 goto bad_addr; 4461 4462 case IP_ADDR_LEN: 4463 /* Verification of local address only */ 4464 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, 4465 ire_requested, ipsec_policy_set, B_FALSE); 4466 break; 4467 4468 case sizeof (sin_t): 4469 sin = (sin_t *)ucp; 4470 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, 4471 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); 4472 if (protocol == IPPROTO_TCP) 4473 connp->conn_recv = tcp_conn_request; 4474 break; 4475 4476 case sizeof (ipa_conn_t): 4477 ac = (ipa_conn_t *)ucp; 4478 /* For raw socket, the local port is not set. */ 4479 if (ac->ac_lport == 0) 4480 ac->ac_lport = connp->conn_lport; 4481 /* Always verify destination reachability. */ 4482 error = ip_bind_connected(connp, mp, &ac->ac_laddr, 4483 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, 4484 ipsec_policy_set, B_TRUE, B_TRUE); 4485 if (protocol == IPPROTO_TCP) 4486 connp->conn_recv = tcp_input; 4487 break; 4488 4489 case sizeof (ipa_conn_x_t): 4490 acx = (ipa_conn_x_t *)ucp; 4491 /* 4492 * Whether or not to verify destination reachability depends 4493 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 4494 */ 4495 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, 4496 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, 4497 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, 4498 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); 4499 if (protocol == IPPROTO_TCP) 4500 connp->conn_recv = tcp_input; 4501 break; 4502 } 4503 if (error == EINPROGRESS) 4504 return (NULL); 4505 else if (error != 0) 4506 goto bad_addr; 4507 /* 4508 * Pass the IPSEC headers size in ire_ipsec_overhead. 4509 * We can't do this in ip_bind_insert_ire because the policy 4510 * may not have been inherited at that point in time and hence 4511 * conn_out_enforce_policy may not be set. 4512 */ 4513 mp1 = mp->b_cont; 4514 if (ire_requested && connp->conn_out_enforce_policy && 4515 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 4516 ire_t *ire = (ire_t *)mp1->b_rptr; 4517 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 4518 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 4519 } 4520 4521 /* Send it home. */ 4522 mp->b_datap->db_type = M_PCPROTO; 4523 tbr->PRIM_type = T_BIND_ACK; 4524 return (mp); 4525 4526 bad_addr: 4527 /* 4528 * If error = -1 then we generate a TBADADDR - otherwise error is 4529 * a unix errno. 4530 */ 4531 if (error > 0) 4532 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 4533 else 4534 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 4535 return (mp); 4536 } 4537 4538 /* 4539 * Here address is verified to be a valid local address. 4540 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 4541 * address is also considered a valid local address. 4542 * In the case of a broadcast/multicast address, however, the 4543 * upper protocol is expected to reset the src address 4544 * to 0 if it sees a IRE_BROADCAST type returned so that 4545 * no packets are emitted with broadcast/multicast address as 4546 * source address (that violates hosts requirements RFC1122) 4547 * The addresses valid for bind are: 4548 * (1) - INADDR_ANY (0) 4549 * (2) - IP address of an UP interface 4550 * (3) - IP address of a DOWN interface 4551 * (4) - valid local IP broadcast addresses. In this case 4552 * the conn will only receive packets destined to 4553 * the specified broadcast address. 4554 * (5) - a multicast address. In this case 4555 * the conn will only receive packets destined to 4556 * the specified multicast address. Note: the 4557 * application still has to issue an 4558 * IP_ADD_MEMBERSHIP socket option. 4559 * 4560 * On error, return -1 for TBADADDR otherwise pass the 4561 * errno with TSYSERR reply. 4562 * 4563 * In all the above cases, the bound address must be valid in the current zone. 4564 * When the address is loopback, multicast or broadcast, there might be many 4565 * matching IREs so bind has to look up based on the zone. 4566 * 4567 * Note: lport is in network byte order. 4568 */ 4569 int 4570 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, 4571 boolean_t ire_requested, boolean_t ipsec_policy_set, 4572 boolean_t fanout_insert) 4573 { 4574 int error = 0; 4575 ire_t *src_ire; 4576 mblk_t *policy_mp; 4577 ipif_t *ipif; 4578 zoneid_t zoneid; 4579 4580 if (ipsec_policy_set) { 4581 policy_mp = mp->b_cont; 4582 } 4583 4584 /* 4585 * If it was previously connected, conn_fully_bound would have 4586 * been set. 4587 */ 4588 connp->conn_fully_bound = B_FALSE; 4589 4590 src_ire = NULL; 4591 ipif = NULL; 4592 4593 zoneid = IPCL_ZONEID(connp); 4594 4595 if (src_addr) { 4596 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 4597 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY); 4598 /* 4599 * If an address other than 0.0.0.0 is requested, 4600 * we verify that it is a valid address for bind 4601 * Note: Following code is in if-else-if form for 4602 * readability compared to a condition check. 4603 */ 4604 /* LINTED - statement has no consequent */ 4605 if (IRE_IS_LOCAL(src_ire)) { 4606 /* 4607 * (2) Bind to address of local UP interface 4608 */ 4609 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 4610 /* 4611 * (4) Bind to broadcast address 4612 * Note: permitted only from transports that 4613 * request IRE 4614 */ 4615 if (!ire_requested) 4616 error = EADDRNOTAVAIL; 4617 } else { 4618 /* 4619 * (3) Bind to address of local DOWN interface 4620 * (ipif_lookup_addr() looks up all interfaces 4621 * but we do not get here for UP interfaces 4622 * - case (2) above) 4623 * We put the protocol byte back into the mblk 4624 * since we may come back via ip_wput_nondata() 4625 * later with this mblk if ipif_lookup_addr chooses 4626 * to defer processing. 4627 */ 4628 *mp->b_wptr++ = (char)connp->conn_ulp; 4629 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, 4630 CONNP_TO_WQ(connp), mp, ip_wput_nondata, 4631 &error)) != NULL) { 4632 ipif_refrele(ipif); 4633 } else if (error == EINPROGRESS) { 4634 if (src_ire != NULL) 4635 ire_refrele(src_ire); 4636 return (EINPROGRESS); 4637 } else if (CLASSD(src_addr)) { 4638 error = 0; 4639 if (src_ire != NULL) 4640 ire_refrele(src_ire); 4641 /* 4642 * (5) bind to multicast address. 4643 * Fake out the IRE returned to upper 4644 * layer to be a broadcast IRE. 4645 */ 4646 src_ire = ire_ctable_lookup( 4647 INADDR_BROADCAST, INADDR_ANY, 4648 IRE_BROADCAST, NULL, zoneid, NULL, 4649 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY)); 4650 if (src_ire == NULL || !ire_requested) 4651 error = EADDRNOTAVAIL; 4652 } else { 4653 /* 4654 * Not a valid address for bind 4655 */ 4656 error = EADDRNOTAVAIL; 4657 } 4658 /* 4659 * Just to keep it consistent with the processing in 4660 * ip_bind_v4() 4661 */ 4662 mp->b_wptr--; 4663 } 4664 if (error) { 4665 /* Red Alert! Attempting to be a bogon! */ 4666 ip1dbg(("ip_bind: bad src address 0x%x\n", 4667 ntohl(src_addr))); 4668 goto bad_addr; 4669 } 4670 } 4671 4672 /* 4673 * Allow setting new policies. For example, disconnects come 4674 * down as ipa_t bind. As we would have set conn_policy_cached 4675 * to B_TRUE before, we should set it to B_FALSE, so that policy 4676 * can change after the disconnect. 4677 */ 4678 connp->conn_policy_cached = B_FALSE; 4679 4680 /* 4681 * If not fanout_insert this was just an address verification 4682 */ 4683 if (fanout_insert) { 4684 /* 4685 * The addresses have been verified. Time to insert in 4686 * the correct fanout list. 4687 */ 4688 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4689 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4690 connp->conn_lport = lport; 4691 connp->conn_fport = 0; 4692 /* 4693 * Do we need to add a check to reject Multicast packets 4694 */ 4695 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); 4696 } 4697 4698 if (error == 0) { 4699 if (ire_requested) { 4700 if (!ip_bind_insert_ire(mp, src_ire, NULL)) { 4701 error = -1; 4702 /* Falls through to bad_addr */ 4703 } 4704 } else if (ipsec_policy_set) { 4705 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4706 error = -1; 4707 /* Falls through to bad_addr */ 4708 } 4709 } 4710 } 4711 bad_addr: 4712 if (error != 0) { 4713 if (connp->conn_anon_port) { 4714 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4715 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 4716 B_FALSE); 4717 } 4718 connp->conn_mlp_type = mlptSingle; 4719 } 4720 if (src_ire != NULL) 4721 IRE_REFRELE(src_ire); 4722 if (ipsec_policy_set) { 4723 ASSERT(policy_mp == mp->b_cont); 4724 ASSERT(policy_mp != NULL); 4725 freeb(policy_mp); 4726 /* 4727 * As of now assume that nothing else accompanies 4728 * IPSEC_POLICY_SET. 4729 */ 4730 mp->b_cont = NULL; 4731 } 4732 return (error); 4733 } 4734 4735 /* 4736 * Verify that both the source and destination addresses 4737 * are valid. If verify_dst is false, then the destination address may be 4738 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4739 * destination reachability, while tunnels do not. 4740 * Note that we allow connect to broadcast and multicast 4741 * addresses when ire_requested is set. Thus the ULP 4742 * has to check for IRE_BROADCAST and multicast. 4743 * 4744 * Returns zero if ok. 4745 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4746 * (for use with TSYSERR reply). 4747 * 4748 * Note: lport and fport are in network byte order. 4749 */ 4750 int 4751 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, 4752 uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4753 boolean_t ire_requested, boolean_t ipsec_policy_set, 4754 boolean_t fanout_insert, boolean_t verify_dst) 4755 { 4756 ire_t *src_ire; 4757 ire_t *dst_ire; 4758 int error = 0; 4759 int protocol; 4760 mblk_t *policy_mp; 4761 ire_t *sire = NULL; 4762 ire_t *md_dst_ire = NULL; 4763 ill_t *md_ill = NULL; 4764 zoneid_t zoneid; 4765 ipaddr_t src_addr = *src_addrp; 4766 4767 src_ire = dst_ire = NULL; 4768 protocol = *mp->b_wptr & 0xFF; 4769 4770 /* 4771 * If we never got a disconnect before, clear it now. 4772 */ 4773 connp->conn_fully_bound = B_FALSE; 4774 4775 if (ipsec_policy_set) { 4776 policy_mp = mp->b_cont; 4777 } 4778 4779 zoneid = IPCL_ZONEID(connp); 4780 4781 if (CLASSD(dst_addr)) { 4782 /* Pick up an IRE_BROADCAST */ 4783 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4784 NULL, zoneid, MBLK_GETLABEL(mp), 4785 (MATCH_IRE_RECURSIVE | 4786 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | 4787 MATCH_IRE_SECATTR)); 4788 } else { 4789 /* 4790 * If conn_dontroute is set or if conn_nexthop_set is set, 4791 * and onlink ipif is not found set ENETUNREACH error. 4792 */ 4793 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4794 ipif_t *ipif; 4795 4796 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4797 dst_addr : connp->conn_nexthop_v4, 4798 connp->conn_zoneid); 4799 if (ipif == NULL) { 4800 error = ENETUNREACH; 4801 goto bad_addr; 4802 } 4803 ipif_refrele(ipif); 4804 } 4805 4806 if (connp->conn_nexthop_set) { 4807 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4808 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), 4809 MATCH_IRE_SECATTR); 4810 } else { 4811 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4812 &sire, zoneid, MBLK_GETLABEL(mp), 4813 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4814 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | 4815 MATCH_IRE_SECATTR)); 4816 } 4817 } 4818 /* 4819 * dst_ire can't be a broadcast when not ire_requested. 4820 * We also prevent ire's with src address INADDR_ANY to 4821 * be used, which are created temporarily for 4822 * sending out packets from endpoints that have 4823 * conn_unspec_src set. If verify_dst is true, the destination must be 4824 * reachable. If verify_dst is false, the destination needn't be 4825 * reachable. 4826 * 4827 * If we match on a reject or black hole, then we've got a 4828 * local failure. May as well fail out the connect() attempt, 4829 * since it's never going to succeed. 4830 */ 4831 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4832 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4833 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4834 /* 4835 * If we're verifying destination reachability, we always want 4836 * to complain here. 4837 * 4838 * If we're not verifying destination reachability but the 4839 * destination has a route, we still want to fail on the 4840 * temporary address and broadcast address tests. 4841 */ 4842 if (verify_dst || (dst_ire != NULL)) { 4843 if (ip_debug > 2) { 4844 pr_addr_dbg("ip_bind_connected: bad connected " 4845 "dst %s\n", AF_INET, &dst_addr); 4846 } 4847 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4848 error = ENETUNREACH; 4849 else 4850 error = EHOSTUNREACH; 4851 goto bad_addr; 4852 } 4853 } 4854 4855 /* 4856 * We now know that routing will allow us to reach the destination. 4857 * Check whether Trusted Solaris policy allows communication with this 4858 * host, and pretend that the destination is unreachable if not. 4859 * 4860 * This is never a problem for TCP, since that transport is known to 4861 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 4862 * handling. If the remote is unreachable, it will be detected at that 4863 * point, so there's no reason to check it here. 4864 * 4865 * Note that for sendto (and other datagram-oriented friends), this 4866 * check is done as part of the data path label computation instead. 4867 * The check here is just to make non-TCP connect() report the right 4868 * error. 4869 */ 4870 if (dst_ire != NULL && is_system_labeled() && 4871 !IPCL_IS_TCP(connp) && 4872 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL, 4873 connp->conn_mac_exempt) != 0) { 4874 error = EHOSTUNREACH; 4875 if (ip_debug > 2) { 4876 pr_addr_dbg("ip_bind_connected: no label for dst %s\n", 4877 AF_INET, &dst_addr); 4878 } 4879 goto bad_addr; 4880 } 4881 4882 /* 4883 * If the app does a connect(), it means that it will most likely 4884 * send more than 1 packet to the destination. It makes sense 4885 * to clear the temporary flag. 4886 */ 4887 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4888 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4889 irb_t *irb = dst_ire->ire_bucket; 4890 4891 rw_enter(&irb->irb_lock, RW_WRITER); 4892 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4893 irb->irb_tmp_ire_cnt--; 4894 rw_exit(&irb->irb_lock); 4895 } 4896 4897 /* 4898 * See if we should notify ULP about MDT; we do this whether or not 4899 * ire_requested is TRUE, in order to handle active connects; MDT 4900 * eligibility tests for passive connects are handled separately 4901 * through tcp_adapt_ire(). We do this before the source address 4902 * selection, because dst_ire may change after a call to 4903 * ipif_select_source(). This is a best-effort check, as the 4904 * packet for this connection may not actually go through 4905 * dst_ire->ire_stq, and the exact IRE can only be known after 4906 * calling ip_newroute(). This is why we further check on the 4907 * IRE during Multidata packet transmission in tcp_multisend(). 4908 */ 4909 if (ip_multidata_outbound && !ipsec_policy_set && dst_ire != NULL && 4910 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4911 (md_ill = ire_to_ill(dst_ire), md_ill != NULL) && 4912 ILL_MDT_CAPABLE(md_ill)) { 4913 md_dst_ire = dst_ire; 4914 IRE_REFHOLD(md_dst_ire); 4915 } 4916 4917 if (dst_ire != NULL && 4918 dst_ire->ire_type == IRE_LOCAL && 4919 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { 4920 /* 4921 * If the IRE belongs to a different zone, look for a matching 4922 * route in the forwarding table and use the source address from 4923 * that route. 4924 */ 4925 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 4926 zoneid, 0, NULL, 4927 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4928 MATCH_IRE_RJ_BHOLE); 4929 if (src_ire == NULL) { 4930 error = EHOSTUNREACH; 4931 goto bad_addr; 4932 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 4933 if (!(src_ire->ire_type & IRE_HOST)) 4934 error = ENETUNREACH; 4935 else 4936 error = EHOSTUNREACH; 4937 goto bad_addr; 4938 } 4939 if (src_addr == INADDR_ANY) 4940 src_addr = src_ire->ire_src_addr; 4941 ire_refrele(src_ire); 4942 src_ire = NULL; 4943 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 4944 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 4945 src_addr = sire->ire_src_addr; 4946 ire_refrele(dst_ire); 4947 dst_ire = sire; 4948 sire = NULL; 4949 } else { 4950 /* 4951 * Pick a source address so that a proper inbound 4952 * load spreading would happen. 4953 */ 4954 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; 4955 ipif_t *src_ipif = NULL; 4956 ire_t *ipif_ire; 4957 4958 /* 4959 * Supply a local source address such that inbound 4960 * load spreading happens. 4961 * 4962 * Determine the best source address on this ill for 4963 * the destination. 4964 * 4965 * 1) For broadcast, we should return a broadcast ire 4966 * found above so that upper layers know that the 4967 * destination address is a broadcast address. 4968 * 4969 * 2) If this is part of a group, select a better 4970 * source address so that better inbound load 4971 * balancing happens. Do the same if the ipif 4972 * is DEPRECATED. 4973 * 4974 * 3) If the outgoing interface is part of a usesrc 4975 * group, then try selecting a source address from 4976 * the usesrc ILL. 4977 */ 4978 if ((dst_ire->ire_zoneid != zoneid && 4979 dst_ire->ire_zoneid != ALL_ZONES) || 4980 (!(dst_ire->ire_type & IRE_BROADCAST) && 4981 ((dst_ill->ill_group != NULL) || 4982 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 4983 (dst_ill->ill_usesrc_ifindex != 0)))) { 4984 /* 4985 * If the destination is reachable via a 4986 * given gateway, the selected source address 4987 * should be in the same subnet as the gateway. 4988 * Otherwise, the destination is not reachable. 4989 * 4990 * If there are no interfaces on the same subnet 4991 * as the destination, ipif_select_source gives 4992 * first non-deprecated interface which might be 4993 * on a different subnet than the gateway. 4994 * This is not desirable. Hence pass the dst_ire 4995 * source address to ipif_select_source. 4996 * It is sure that the destination is reachable 4997 * with the dst_ire source address subnet. 4998 * So passing dst_ire source address to 4999 * ipif_select_source will make sure that the 5000 * selected source will be on the same subnet 5001 * as dst_ire source address. 5002 */ 5003 ipaddr_t saddr = 5004 dst_ire->ire_ipif->ipif_src_addr; 5005 src_ipif = ipif_select_source(dst_ill, 5006 saddr, zoneid); 5007 if (src_ipif != NULL) { 5008 if (IS_VNI(src_ipif->ipif_ill)) { 5009 /* 5010 * For VNI there is no 5011 * interface route 5012 */ 5013 src_addr = 5014 src_ipif->ipif_src_addr; 5015 } else { 5016 ipif_ire = 5017 ipif_to_ire(src_ipif); 5018 if (ipif_ire != NULL) { 5019 IRE_REFRELE(dst_ire); 5020 dst_ire = ipif_ire; 5021 } 5022 src_addr = 5023 dst_ire->ire_src_addr; 5024 } 5025 ipif_refrele(src_ipif); 5026 } else { 5027 src_addr = dst_ire->ire_src_addr; 5028 } 5029 } else { 5030 src_addr = dst_ire->ire_src_addr; 5031 } 5032 } 5033 } 5034 5035 /* 5036 * We do ire_route_lookup() here (and not 5037 * interface lookup as we assert that 5038 * src_addr should only come from an 5039 * UP interface for hard binding. 5040 */ 5041 ASSERT(src_ire == NULL); 5042 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 5043 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY); 5044 /* src_ire must be a local|loopback */ 5045 if (!IRE_IS_LOCAL(src_ire)) { 5046 if (ip_debug > 2) { 5047 pr_addr_dbg("ip_bind_connected: bad connected " 5048 "src %s\n", AF_INET, &src_addr); 5049 } 5050 error = EADDRNOTAVAIL; 5051 goto bad_addr; 5052 } 5053 5054 /* 5055 * If the source address is a loopback address, the 5056 * destination had best be local or multicast. 5057 * The transports that can't handle multicast will reject 5058 * those addresses. 5059 */ 5060 if (src_ire->ire_type == IRE_LOOPBACK && 5061 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 5062 ip1dbg(("ip_bind_connected: bad connected loopback\n")); 5063 error = -1; 5064 goto bad_addr; 5065 } 5066 5067 /* 5068 * Allow setting new policies. For example, disconnects come 5069 * down as ipa_t bind. As we would have set conn_policy_cached 5070 * to B_TRUE before, we should set it to B_FALSE, so that policy 5071 * can change after the disconnect. 5072 */ 5073 connp->conn_policy_cached = B_FALSE; 5074 5075 /* 5076 * Set the conn addresses/ports immediately, so the IPsec policy calls 5077 * can handle their passed-in conn's. 5078 */ 5079 5080 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 5081 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 5082 connp->conn_lport = lport; 5083 connp->conn_fport = fport; 5084 *src_addrp = src_addr; 5085 5086 ASSERT(!(ipsec_policy_set && ire_requested)); 5087 if (ire_requested) { 5088 iulp_t *ulp_info = NULL; 5089 5090 /* 5091 * Note that sire will not be NULL if this is an off-link 5092 * connection and there is not cache for that dest yet. 5093 * 5094 * XXX Because of an existing bug, if there are multiple 5095 * default routes, the IRE returned now may not be the actual 5096 * default route used (default routes are chosen in a 5097 * round robin fashion). So if the metrics for different 5098 * default routes are different, we may return the wrong 5099 * metrics. This will not be a problem if the existing 5100 * bug is fixed. 5101 */ 5102 if (sire != NULL) { 5103 ulp_info = &(sire->ire_uinfo); 5104 } 5105 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info)) { 5106 error = -1; 5107 goto bad_addr; 5108 } 5109 } else if (ipsec_policy_set) { 5110 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 5111 error = -1; 5112 goto bad_addr; 5113 } 5114 } 5115 5116 /* 5117 * Cache IPsec policy in this conn. If we have per-socket policy, 5118 * we'll cache that. If we don't, we'll inherit global policy. 5119 * 5120 * We can't insert until the conn reflects the policy. Note that 5121 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 5122 * connections where we don't have a policy. This is to prevent 5123 * global policy lookups in the inbound path. 5124 * 5125 * If we insert before we set conn_policy_cached, 5126 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 5127 * because global policy cound be non-empty. We normally call 5128 * ipsec_check_policy() for conn_policy_cached connections only if 5129 * ipc_in_enforce_policy is set. But in this case, 5130 * conn_policy_cached can get set anytime since we made the 5131 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 5132 * called, which will make the above assumption false. Thus, we 5133 * need to insert after we set conn_policy_cached. 5134 */ 5135 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 5136 goto bad_addr; 5137 5138 if (fanout_insert) { 5139 /* 5140 * The addresses have been verified. Time to insert in 5141 * the correct fanout list. 5142 */ 5143 error = ipcl_conn_insert(connp, protocol, src_addr, 5144 dst_addr, connp->conn_ports); 5145 } 5146 5147 if (error == 0) { 5148 connp->conn_fully_bound = B_TRUE; 5149 /* 5150 * Our initial checks for MDT have passed; the IRE is not 5151 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 5152 * be supporting MDT. Pass the IRE, IPC and ILL into 5153 * ip_mdinfo_return(), which performs further checks 5154 * against them and upon success, returns the MDT info 5155 * mblk which we will attach to the bind acknowledgment. 5156 */ 5157 if (md_dst_ire != NULL) { 5158 mblk_t *mdinfo_mp; 5159 5160 ASSERT(md_ill != NULL); 5161 ASSERT(md_ill->ill_mdt_capab != NULL); 5162 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 5163 md_ill->ill_name, md_ill->ill_mdt_capab)) != NULL) 5164 linkb(mp, mdinfo_mp); 5165 } 5166 } 5167 bad_addr: 5168 if (ipsec_policy_set) { 5169 ASSERT(policy_mp == mp->b_cont); 5170 ASSERT(policy_mp != NULL); 5171 freeb(policy_mp); 5172 /* 5173 * As of now assume that nothing else accompanies 5174 * IPSEC_POLICY_SET. 5175 */ 5176 mp->b_cont = NULL; 5177 } 5178 if (src_ire != NULL) 5179 IRE_REFRELE(src_ire); 5180 if (dst_ire != NULL) 5181 IRE_REFRELE(dst_ire); 5182 if (sire != NULL) 5183 IRE_REFRELE(sire); 5184 if (md_dst_ire != NULL) 5185 IRE_REFRELE(md_dst_ire); 5186 return (error); 5187 } 5188 5189 /* 5190 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 5191 * Prefers dst_ire over src_ire. 5192 */ 5193 static boolean_t 5194 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info) 5195 { 5196 mblk_t *mp1; 5197 ire_t *ret_ire = NULL; 5198 5199 mp1 = mp->b_cont; 5200 ASSERT(mp1 != NULL); 5201 5202 if (ire != NULL) { 5203 /* 5204 * mp1 initialized above to IRE_DB_REQ_TYPE 5205 * appended mblk. Its <upper protocol>'s 5206 * job to make sure there is room. 5207 */ 5208 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 5209 return (0); 5210 5211 mp1->b_datap->db_type = IRE_DB_TYPE; 5212 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 5213 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 5214 ret_ire = (ire_t *)mp1->b_rptr; 5215 /* 5216 * Pass the latest setting of the ip_path_mtu_discovery and 5217 * copy the ulp info if any. 5218 */ 5219 ret_ire->ire_frag_flag |= (ip_path_mtu_discovery) ? 5220 IPH_DF : 0; 5221 if (ulp_info != NULL) { 5222 bcopy(ulp_info, &(ret_ire->ire_uinfo), 5223 sizeof (iulp_t)); 5224 } 5225 ret_ire->ire_mp = mp1; 5226 } else { 5227 /* 5228 * No IRE was found. Remove IRE mblk. 5229 */ 5230 mp->b_cont = mp1->b_cont; 5231 freeb(mp1); 5232 } 5233 5234 return (1); 5235 } 5236 5237 /* 5238 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 5239 * the final piece where we don't. Return a pointer to the first mblk in the 5240 * result, and update the pointer to the next mblk to chew on. If anything 5241 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 5242 * NULL pointer. 5243 */ 5244 mblk_t * 5245 ip_carve_mp(mblk_t **mpp, ssize_t len) 5246 { 5247 mblk_t *mp0; 5248 mblk_t *mp1; 5249 mblk_t *mp2; 5250 5251 if (!len || !mpp || !(mp0 = *mpp)) 5252 return (NULL); 5253 /* If we aren't going to consume the first mblk, we need a dup. */ 5254 if (mp0->b_wptr - mp0->b_rptr > len) { 5255 mp1 = dupb(mp0); 5256 if (mp1) { 5257 /* Partition the data between the two mblks. */ 5258 mp1->b_wptr = mp1->b_rptr + len; 5259 mp0->b_rptr = mp1->b_wptr; 5260 /* 5261 * after adjustments if mblk not consumed is now 5262 * unaligned, try to align it. If this fails free 5263 * all messages and let upper layer recover. 5264 */ 5265 if (!OK_32PTR(mp0->b_rptr)) { 5266 if (!pullupmsg(mp0, -1)) { 5267 freemsg(mp0); 5268 freemsg(mp1); 5269 *mpp = NULL; 5270 return (NULL); 5271 } 5272 } 5273 } 5274 return (mp1); 5275 } 5276 /* Eat through as many mblks as we need to get len bytes. */ 5277 len -= mp0->b_wptr - mp0->b_rptr; 5278 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 5279 if (mp2->b_wptr - mp2->b_rptr > len) { 5280 /* 5281 * We won't consume the entire last mblk. Like 5282 * above, dup and partition it. 5283 */ 5284 mp1->b_cont = dupb(mp2); 5285 mp1 = mp1->b_cont; 5286 if (!mp1) { 5287 /* 5288 * Trouble. Rather than go to a lot of 5289 * trouble to clean up, we free the messages. 5290 * This won't be any worse than losing it on 5291 * the wire. 5292 */ 5293 freemsg(mp0); 5294 freemsg(mp2); 5295 *mpp = NULL; 5296 return (NULL); 5297 } 5298 mp1->b_wptr = mp1->b_rptr + len; 5299 mp2->b_rptr = mp1->b_wptr; 5300 /* 5301 * after adjustments if mblk not consumed is now 5302 * unaligned, try to align it. If this fails free 5303 * all messages and let upper layer recover. 5304 */ 5305 if (!OK_32PTR(mp2->b_rptr)) { 5306 if (!pullupmsg(mp2, -1)) { 5307 freemsg(mp0); 5308 freemsg(mp2); 5309 *mpp = NULL; 5310 return (NULL); 5311 } 5312 } 5313 *mpp = mp2; 5314 return (mp0); 5315 } 5316 /* Decrement len by the amount we just got. */ 5317 len -= mp2->b_wptr - mp2->b_rptr; 5318 } 5319 /* 5320 * len should be reduced to zero now. If not our caller has 5321 * screwed up. 5322 */ 5323 if (len) { 5324 /* Shouldn't happen! */ 5325 freemsg(mp0); 5326 *mpp = NULL; 5327 return (NULL); 5328 } 5329 /* 5330 * We consumed up to exactly the end of an mblk. Detach the part 5331 * we are returning from the rest of the chain. 5332 */ 5333 mp1->b_cont = NULL; 5334 *mpp = mp2; 5335 return (mp0); 5336 } 5337 5338 /* The ill stream is being unplumbed. Called from ip_close */ 5339 int 5340 ip_modclose(ill_t *ill) 5341 { 5342 5343 boolean_t success; 5344 ipsq_t *ipsq; 5345 ipif_t *ipif; 5346 queue_t *q = ill->ill_rq; 5347 hook_nic_event_t *info; 5348 5349 /* 5350 * Forcibly enter the ipsq after some delay. This is to take 5351 * care of the case when some ioctl does not complete because 5352 * we sent a control message to the driver and it did not 5353 * send us a reply. We want to be able to at least unplumb 5354 * and replumb rather than force the user to reboot the system. 5355 */ 5356 success = ipsq_enter(ill, B_FALSE); 5357 5358 /* 5359 * Open/close/push/pop is guaranteed to be single threaded 5360 * per stream by STREAMS. FS guarantees that all references 5361 * from top are gone before close is called. So there can't 5362 * be another close thread that has set CONDEMNED on this ill. 5363 * and cause ipsq_enter to return failure. 5364 */ 5365 ASSERT(success); 5366 ipsq = ill->ill_phyint->phyint_ipsq; 5367 5368 /* 5369 * Mark it condemned. No new reference will be made to this ill. 5370 * Lookup functions will return an error. Threads that try to 5371 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 5372 * that the refcnt will drop down to zero. 5373 */ 5374 mutex_enter(&ill->ill_lock); 5375 ill->ill_state_flags |= ILL_CONDEMNED; 5376 for (ipif = ill->ill_ipif; ipif != NULL; 5377 ipif = ipif->ipif_next) { 5378 ipif->ipif_state_flags |= IPIF_CONDEMNED; 5379 } 5380 /* 5381 * Wake up anybody waiting to enter the ipsq. ipsq_enter 5382 * returns error if ILL_CONDEMNED is set 5383 */ 5384 cv_broadcast(&ill->ill_cv); 5385 mutex_exit(&ill->ill_lock); 5386 5387 /* 5388 * Shut down fragmentation reassembly. 5389 * ill_frag_timer won't start a timer again. 5390 * Now cancel any existing timer 5391 */ 5392 (void) untimeout(ill->ill_frag_timer_id); 5393 (void) ill_frag_timeout(ill, 0); 5394 5395 /* 5396 * If MOVE was in progress, clear the 5397 * move_in_progress fields also. 5398 */ 5399 if (ill->ill_move_in_progress) { 5400 ILL_CLEAR_MOVE(ill); 5401 } 5402 5403 /* 5404 * Call ill_delete to bring down the ipifs, ilms and ill on 5405 * this ill. Then wait for the refcnts to drop to zero. 5406 * ill_is_quiescent checks whether the ill is really quiescent. 5407 * Then make sure that threads that are waiting to enter the 5408 * ipsq have seen the error returned by ipsq_enter and have 5409 * gone away. Then we call ill_delete_tail which does the 5410 * DL_UNBIND and DL_DETACH with the driver and then qprocsoff. 5411 */ 5412 ill_delete(ill); 5413 mutex_enter(&ill->ill_lock); 5414 while (!ill_is_quiescent(ill)) 5415 cv_wait(&ill->ill_cv, &ill->ill_lock); 5416 while (ill->ill_waiters) 5417 cv_wait(&ill->ill_cv, &ill->ill_lock); 5418 5419 mutex_exit(&ill->ill_lock); 5420 5421 /* qprocsoff is called in ill_delete_tail */ 5422 ill_delete_tail(ill); 5423 5424 /* 5425 * Walk through all upper (conn) streams and qenable 5426 * those that have queued data. 5427 * close synchronization needs this to 5428 * be done to ensure that all upper layers blocked 5429 * due to flow control to the closing device 5430 * get unblocked. 5431 */ 5432 ip1dbg(("ip_wsrv: walking\n")); 5433 conn_walk_drain(); 5434 5435 mutex_enter(&ip_mi_lock); 5436 mi_close_unlink(&ip_g_head, (IDP)ill); 5437 mutex_exit(&ip_mi_lock); 5438 5439 /* 5440 * credp could be null if the open didn't succeed and ip_modopen 5441 * itself calls ip_close. 5442 */ 5443 if (ill->ill_credp != NULL) 5444 crfree(ill->ill_credp); 5445 5446 /* 5447 * Unhook the nic event message from the ill and enqueue it into the nic 5448 * event taskq. 5449 */ 5450 if ((info = ill->ill_nic_event_info) != NULL) { 5451 if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, 5452 (void *)info, DDI_SLEEP) == DDI_FAILURE) { 5453 ip2dbg(("ip_ioctl_finish:ddi_taskq_dispatch failed\n")); 5454 if (info->hne_data != NULL) 5455 kmem_free(info->hne_data, info->hne_datalen); 5456 kmem_free(info, sizeof (hook_nic_event_t)); 5457 } 5458 ill->ill_nic_event_info = NULL; 5459 } 5460 5461 mi_close_free((IDP)ill); 5462 q->q_ptr = WR(q)->q_ptr = NULL; 5463 5464 ipsq_exit(ipsq, B_TRUE, B_TRUE); 5465 5466 return (0); 5467 } 5468 5469 /* 5470 * This is called as part of close() for both IP and UDP 5471 * in order to quiesce the conn. 5472 */ 5473 void 5474 ip_quiesce_conn(conn_t *connp) 5475 { 5476 boolean_t drain_cleanup_reqd = B_FALSE; 5477 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 5478 boolean_t ilg_cleanup_reqd = B_FALSE; 5479 5480 ASSERT(!IPCL_IS_TCP(connp)); 5481 5482 /* 5483 * Mark the conn as closing, and this conn must not be 5484 * inserted in future into any list. Eg. conn_drain_insert(), 5485 * won't insert this conn into the conn_drain_list. 5486 * Similarly ill_pending_mp_add() will not add any mp to 5487 * the pending mp list, after this conn has started closing. 5488 * 5489 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 5490 * cannot get set henceforth. 5491 */ 5492 mutex_enter(&connp->conn_lock); 5493 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 5494 connp->conn_state_flags |= CONN_CLOSING; 5495 if (connp->conn_idl != NULL) 5496 drain_cleanup_reqd = B_TRUE; 5497 if (connp->conn_oper_pending_ill != NULL) 5498 conn_ioctl_cleanup_reqd = B_TRUE; 5499 if (connp->conn_ilg_inuse != 0) 5500 ilg_cleanup_reqd = B_TRUE; 5501 mutex_exit(&connp->conn_lock); 5502 5503 if (IPCL_IS_UDP(connp)) 5504 udp_quiesce_conn(connp); 5505 5506 if (conn_ioctl_cleanup_reqd) 5507 conn_ioctl_cleanup(connp); 5508 5509 if (is_system_labeled() && connp->conn_anon_port) { 5510 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 5511 connp->conn_mlp_type, connp->conn_ulp, 5512 ntohs(connp->conn_lport), B_FALSE); 5513 connp->conn_anon_port = 0; 5514 } 5515 connp->conn_mlp_type = mlptSingle; 5516 5517 /* 5518 * Remove this conn from any fanout list it is on. 5519 * and then wait for any threads currently operating 5520 * on this endpoint to finish 5521 */ 5522 ipcl_hash_remove(connp); 5523 5524 /* 5525 * Remove this conn from the drain list, and do 5526 * any other cleanup that may be required. 5527 * (Only non-tcp streams may have a non-null conn_idl. 5528 * TCP streams are never flow controlled, and 5529 * conn_idl will be null) 5530 */ 5531 if (drain_cleanup_reqd) 5532 conn_drain_tail(connp, B_TRUE); 5533 5534 if (connp->conn_rq == ip_g_mrouter || connp->conn_wq == ip_g_mrouter) 5535 (void) ip_mrouter_done(NULL); 5536 5537 if (ilg_cleanup_reqd) 5538 ilg_delete_all(connp); 5539 5540 conn_delete_ire(connp, NULL); 5541 5542 /* 5543 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 5544 * callers from write side can't be there now because close 5545 * is in progress. The only other caller is ipcl_walk 5546 * which checks for the condemned flag. 5547 */ 5548 mutex_enter(&connp->conn_lock); 5549 connp->conn_state_flags |= CONN_CONDEMNED; 5550 while (connp->conn_ref != 1) 5551 cv_wait(&connp->conn_cv, &connp->conn_lock); 5552 connp->conn_state_flags |= CONN_QUIESCED; 5553 mutex_exit(&connp->conn_lock); 5554 } 5555 5556 /* ARGSUSED */ 5557 int 5558 ip_close(queue_t *q, int flags) 5559 { 5560 conn_t *connp; 5561 5562 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 5563 5564 /* 5565 * Call the appropriate delete routine depending on whether this is 5566 * a module or device. 5567 */ 5568 if (WR(q)->q_next != NULL) { 5569 /* This is a module close */ 5570 return (ip_modclose((ill_t *)q->q_ptr)); 5571 } 5572 5573 connp = q->q_ptr; 5574 ip_quiesce_conn(connp); 5575 5576 qprocsoff(q); 5577 5578 /* 5579 * Now we are truly single threaded on this stream, and can 5580 * delete the things hanging off the connp, and finally the connp. 5581 * We removed this connp from the fanout list, it cannot be 5582 * accessed thru the fanouts, and we already waited for the 5583 * conn_ref to drop to 0. We are already in close, so 5584 * there cannot be any other thread from the top. qprocsoff 5585 * has completed, and service has completed or won't run in 5586 * future. 5587 */ 5588 ASSERT(connp->conn_ref == 1); 5589 5590 /* 5591 * A conn which was previously marked as IPCL_UDP cannot 5592 * retain the flag because it would have been cleared by 5593 * udp_close(). 5594 */ 5595 ASSERT(!IPCL_IS_UDP(connp)); 5596 5597 if (connp->conn_latch != NULL) { 5598 IPLATCH_REFRELE(connp->conn_latch); 5599 connp->conn_latch = NULL; 5600 } 5601 if (connp->conn_policy != NULL) { 5602 IPPH_REFRELE(connp->conn_policy); 5603 connp->conn_policy = NULL; 5604 } 5605 if (connp->conn_ipsec_opt_mp != NULL) { 5606 freemsg(connp->conn_ipsec_opt_mp); 5607 connp->conn_ipsec_opt_mp = NULL; 5608 } 5609 5610 inet_minor_free(ip_minor_arena, connp->conn_dev); 5611 5612 connp->conn_ref--; 5613 ipcl_conn_destroy(connp); 5614 5615 q->q_ptr = WR(q)->q_ptr = NULL; 5616 return (0); 5617 } 5618 5619 int 5620 ip_snmpmod_close(queue_t *q) 5621 { 5622 conn_t *connp = Q_TO_CONN(q); 5623 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5624 5625 qprocsoff(q); 5626 5627 if (connp->conn_flags & IPCL_UDPMOD) 5628 udp_close_free(connp); 5629 5630 if (connp->conn_cred != NULL) { 5631 crfree(connp->conn_cred); 5632 connp->conn_cred = NULL; 5633 } 5634 CONN_DEC_REF(connp); 5635 q->q_ptr = WR(q)->q_ptr = NULL; 5636 return (0); 5637 } 5638 5639 /* 5640 * Write side put procedure for TCP module or UDP module instance. TCP/UDP 5641 * as a module is only used for MIB browsers that push TCP/UDP over IP or ARP. 5642 * The only supported primitives are T_SVR4_OPTMGMT_REQ and T_OPTMGMT_REQ. 5643 * M_FLUSH messages and ioctls are only passed downstream; we don't flush our 5644 * queues as we never enqueue messages there and we don't handle any ioctls. 5645 * Everything else is freed. 5646 */ 5647 void 5648 ip_snmpmod_wput(queue_t *q, mblk_t *mp) 5649 { 5650 conn_t *connp = q->q_ptr; 5651 pfi_t setfn; 5652 pfi_t getfn; 5653 5654 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5655 5656 switch (DB_TYPE(mp)) { 5657 case M_PROTO: 5658 case M_PCPROTO: 5659 if ((MBLKL(mp) >= sizeof (t_scalar_t)) && 5660 ((((union T_primitives *)mp->b_rptr)->type == 5661 T_SVR4_OPTMGMT_REQ) || 5662 (((union T_primitives *)mp->b_rptr)->type == 5663 T_OPTMGMT_REQ))) { 5664 /* 5665 * This is the only TPI primitive supported. Its 5666 * handling does not require tcp_t, but it does require 5667 * conn_t to check permissions. 5668 */ 5669 cred_t *cr = DB_CREDDEF(mp, connp->conn_cred); 5670 5671 if (connp->conn_flags & IPCL_TCPMOD) { 5672 setfn = tcp_snmp_set; 5673 getfn = tcp_snmp_get; 5674 } else { 5675 setfn = udp_snmp_set; 5676 getfn = udp_snmp_get; 5677 } 5678 if (!snmpcom_req(q, mp, setfn, getfn, cr)) { 5679 freemsg(mp); 5680 return; 5681 } 5682 } else if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, ENOTSUP)) 5683 != NULL) 5684 qreply(q, mp); 5685 break; 5686 case M_FLUSH: 5687 case M_IOCTL: 5688 putnext(q, mp); 5689 break; 5690 default: 5691 freemsg(mp); 5692 break; 5693 } 5694 } 5695 5696 /* Return the IP checksum for the IP header at "iph". */ 5697 uint16_t 5698 ip_csum_hdr(ipha_t *ipha) 5699 { 5700 uint16_t *uph; 5701 uint32_t sum; 5702 int opt_len; 5703 5704 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - 5705 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 5706 uph = (uint16_t *)ipha; 5707 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 5708 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 5709 if (opt_len > 0) { 5710 do { 5711 sum += uph[10]; 5712 sum += uph[11]; 5713 uph += 2; 5714 } while (--opt_len); 5715 } 5716 sum = (sum & 0xFFFF) + (sum >> 16); 5717 sum = ~(sum + (sum >> 16)) & 0xFFFF; 5718 if (sum == 0xffff) 5719 sum = 0; 5720 return ((uint16_t)sum); 5721 } 5722 5723 void 5724 ip_ddi_destroy(void) 5725 { 5726 ipv4_hook_destroy(); 5727 ipv6_hook_destroy(); 5728 ip_net_destroy(); 5729 5730 tnet_fini(); 5731 tcp_ddi_destroy(); 5732 sctp_ddi_destroy(); 5733 ipsec_loader_destroy(); 5734 ipsec_policy_destroy(); 5735 ipsec_kstat_destroy(); 5736 nd_free(&ip_g_nd); 5737 mutex_destroy(&igmp_timer_lock); 5738 mutex_destroy(&mld_timer_lock); 5739 mutex_destroy(&igmp_slowtimeout_lock); 5740 mutex_destroy(&mld_slowtimeout_lock); 5741 mutex_destroy(&ip_mi_lock); 5742 mutex_destroy(&rts_clients.connf_lock); 5743 ip_ire_fini(); 5744 ip6_asp_free(); 5745 conn_drain_fini(); 5746 ipcl_destroy(); 5747 inet_minor_destroy(ip_minor_arena); 5748 icmp_kstat_fini(); 5749 ip_kstat_fini(); 5750 rw_destroy(&ipsec_capab_ills_lock); 5751 rw_destroy(&ill_g_usesrc_lock); 5752 ip_drop_unregister(&ip_dropper); 5753 } 5754 5755 5756 void 5757 ip_ddi_init(void) 5758 { 5759 TCP6_MAJ = ddi_name_to_major(TCP6); 5760 TCP_MAJ = ddi_name_to_major(TCP); 5761 SCTP_MAJ = ddi_name_to_major(SCTP); 5762 SCTP6_MAJ = ddi_name_to_major(SCTP6); 5763 5764 ip_input_proc = ip_squeue_switch(ip_squeue_enter); 5765 5766 /* IP's IPsec code calls the packet dropper */ 5767 ip_drop_register(&ip_dropper, "IP IPsec processing"); 5768 5769 if (!ip_g_nd) { 5770 if (!ip_param_register(lcl_param_arr, A_CNT(lcl_param_arr), 5771 lcl_ndp_arr, A_CNT(lcl_ndp_arr))) { 5772 nd_free(&ip_g_nd); 5773 } 5774 } 5775 5776 ipsec_loader_init(); 5777 ipsec_policy_init(); 5778 ipsec_kstat_init(); 5779 rw_init(&ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 5780 mutex_init(&igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5781 mutex_init(&mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5782 mutex_init(&igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5783 mutex_init(&mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5784 mutex_init(&ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 5785 mutex_init(&ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 5786 rw_init(&ill_g_lock, NULL, RW_DEFAULT, NULL); 5787 rw_init(&ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 5788 rw_init(&ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 5789 5790 /* 5791 * For IP and TCP the minor numbers should start from 2 since we have 4 5792 * initial devices: ip, ip6, tcp, tcp6. 5793 */ 5794 if ((ip_minor_arena = inet_minor_create("ip_minor_arena", 5795 INET_MIN_DEV + 2, KM_SLEEP)) == NULL) { 5796 cmn_err(CE_PANIC, 5797 "ip_ddi_init: ip_minor_arena creation failed\n"); 5798 } 5799 5800 ipcl_init(); 5801 mutex_init(&rts_clients.connf_lock, NULL, MUTEX_DEFAULT, NULL); 5802 ip_ire_init(); 5803 ip6_asp_init(); 5804 ipif_init(); 5805 conn_drain_init(); 5806 tcp_ddi_init(); 5807 sctp_ddi_init(); 5808 5809 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5810 5811 if ((ip_kstat = kstat_create("ip", 0, "ipstat", 5812 "net", KSTAT_TYPE_NAMED, 5813 sizeof (ip_statistics) / sizeof (kstat_named_t), 5814 KSTAT_FLAG_VIRTUAL)) != NULL) { 5815 ip_kstat->ks_data = &ip_statistics; 5816 kstat_install(ip_kstat); 5817 } 5818 ip_kstat_init(); 5819 ip6_kstat_init(); 5820 icmp_kstat_init(); 5821 ipsec_loader_start(); 5822 tnet_init(); 5823 5824 ip_net_init(); 5825 ipv4_hook_init(); 5826 ipv6_hook_init(); 5827 } 5828 5829 /* 5830 * Allocate and initialize a DLPI template of the specified length. (May be 5831 * called as writer.) 5832 */ 5833 mblk_t * 5834 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 5835 { 5836 mblk_t *mp; 5837 5838 mp = allocb(len, BPRI_MED); 5839 if (!mp) 5840 return (NULL); 5841 5842 /* 5843 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 5844 * of which we don't seem to use) are sent with M_PCPROTO, and 5845 * that other DLPI are M_PROTO. 5846 */ 5847 if (prim == DL_INFO_REQ) { 5848 mp->b_datap->db_type = M_PCPROTO; 5849 } else { 5850 mp->b_datap->db_type = M_PROTO; 5851 } 5852 5853 mp->b_wptr = mp->b_rptr + len; 5854 bzero(mp->b_rptr, len); 5855 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 5856 return (mp); 5857 } 5858 5859 const char * 5860 dlpi_prim_str(int prim) 5861 { 5862 switch (prim) { 5863 case DL_INFO_REQ: return ("DL_INFO_REQ"); 5864 case DL_INFO_ACK: return ("DL_INFO_ACK"); 5865 case DL_ATTACH_REQ: return ("DL_ATTACH_REQ"); 5866 case DL_DETACH_REQ: return ("DL_DETACH_REQ"); 5867 case DL_BIND_REQ: return ("DL_BIND_REQ"); 5868 case DL_BIND_ACK: return ("DL_BIND_ACK"); 5869 case DL_UNBIND_REQ: return ("DL_UNBIND_REQ"); 5870 case DL_OK_ACK: return ("DL_OK_ACK"); 5871 case DL_ERROR_ACK: return ("DL_ERROR_ACK"); 5872 case DL_ENABMULTI_REQ: return ("DL_ENABMULTI_REQ"); 5873 case DL_DISABMULTI_REQ: return ("DL_DISABMULTI_REQ"); 5874 case DL_PROMISCON_REQ: return ("DL_PROMISCON_REQ"); 5875 case DL_PROMISCOFF_REQ: return ("DL_PROMISCOFF_REQ"); 5876 case DL_UNITDATA_REQ: return ("DL_UNITDATA_REQ"); 5877 case DL_UNITDATA_IND: return ("DL_UNITDATA_IND"); 5878 case DL_UDERROR_IND: return ("DL_UDERROR_IND"); 5879 case DL_PHYS_ADDR_REQ: return ("DL_PHYS_ADDR_REQ"); 5880 case DL_PHYS_ADDR_ACK: return ("DL_PHYS_ADDR_ACK"); 5881 case DL_SET_PHYS_ADDR_REQ: return ("DL_SET_PHYS_ADDR_REQ"); 5882 case DL_NOTIFY_REQ: return ("DL_NOTIFY_REQ"); 5883 case DL_NOTIFY_ACK: return ("DL_NOTIFY_ACK"); 5884 case DL_NOTIFY_IND: return ("DL_NOTIFY_IND"); 5885 case DL_CAPABILITY_REQ: return ("DL_CAPABILITY_REQ"); 5886 case DL_CAPABILITY_ACK: return ("DL_CAPABILITY_ACK"); 5887 case DL_CONTROL_REQ: return ("DL_CONTROL_REQ"); 5888 case DL_CONTROL_ACK: return ("DL_CONTROL_ACK"); 5889 default: return ("<unknown primitive>"); 5890 } 5891 } 5892 5893 const char * 5894 dlpi_err_str(int err) 5895 { 5896 switch (err) { 5897 case DL_ACCESS: return ("DL_ACCESS"); 5898 case DL_BADADDR: return ("DL_BADADDR"); 5899 case DL_BADCORR: return ("DL_BADCORR"); 5900 case DL_BADDATA: return ("DL_BADDATA"); 5901 case DL_BADPPA: return ("DL_BADPPA"); 5902 case DL_BADPRIM: return ("DL_BADPRIM"); 5903 case DL_BADQOSPARAM: return ("DL_BADQOSPARAM"); 5904 case DL_BADQOSTYPE: return ("DL_BADQOSTYPE"); 5905 case DL_BADSAP: return ("DL_BADSAP"); 5906 case DL_BADTOKEN: return ("DL_BADTOKEN"); 5907 case DL_BOUND: return ("DL_BOUND"); 5908 case DL_INITFAILED: return ("DL_INITFAILED"); 5909 case DL_NOADDR: return ("DL_NOADDR"); 5910 case DL_NOTINIT: return ("DL_NOTINIT"); 5911 case DL_OUTSTATE: return ("DL_OUTSTATE"); 5912 case DL_SYSERR: return ("DL_SYSERR"); 5913 case DL_UNSUPPORTED: return ("DL_UNSUPPORTED"); 5914 case DL_UNDELIVERABLE: return ("DL_UNDELIVERABLE"); 5915 case DL_NOTSUPPORTED : return ("DL_NOTSUPPORTED "); 5916 case DL_TOOMANY: return ("DL_TOOMANY"); 5917 case DL_NOTENAB: return ("DL_NOTENAB"); 5918 case DL_BUSY: return ("DL_BUSY"); 5919 case DL_NOAUTO: return ("DL_NOAUTO"); 5920 case DL_NOXIDAUTO: return ("DL_NOXIDAUTO"); 5921 case DL_NOTESTAUTO: return ("DL_NOTESTAUTO"); 5922 case DL_XIDAUTO: return ("DL_XIDAUTO"); 5923 case DL_TESTAUTO: return ("DL_TESTAUTO"); 5924 case DL_PENDING: return ("DL_PENDING"); 5925 default: return ("<unknown error>"); 5926 } 5927 } 5928 5929 /* 5930 * Debug formatting routine. Returns a character string representation of the 5931 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 5932 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 5933 * 5934 * Once the ndd table-printing interfaces are removed, this can be changed to 5935 * standard dotted-decimal form. 5936 */ 5937 char * 5938 ip_dot_addr(ipaddr_t addr, char *buf) 5939 { 5940 uint8_t *ap = (uint8_t *)&addr; 5941 5942 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 5943 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 5944 return (buf); 5945 } 5946 5947 /* 5948 * Write the given MAC address as a printable string in the usual colon- 5949 * separated format. 5950 */ 5951 const char * 5952 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 5953 { 5954 char *bp; 5955 5956 if (alen == 0 || buflen < 4) 5957 return ("?"); 5958 bp = buf; 5959 for (;;) { 5960 /* 5961 * If there are more MAC address bytes available, but we won't 5962 * have any room to print them, then add "..." to the string 5963 * instead. See below for the 'magic number' explanation. 5964 */ 5965 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 5966 (void) strcpy(bp, "..."); 5967 break; 5968 } 5969 (void) sprintf(bp, "%02x", *addr++); 5970 bp += 2; 5971 if (--alen == 0) 5972 break; 5973 *bp++ = ':'; 5974 buflen -= 3; 5975 /* 5976 * At this point, based on the first 'if' statement above, 5977 * either alen == 1 and buflen >= 3, or alen > 1 and 5978 * buflen >= 4. The first case leaves room for the final "xx" 5979 * number and trailing NUL byte. The second leaves room for at 5980 * least "...". Thus the apparently 'magic' numbers chosen for 5981 * that statement. 5982 */ 5983 } 5984 return (buf); 5985 } 5986 5987 /* 5988 * Send an ICMP error after patching up the packet appropriately. Returns 5989 * non-zero if the appropriate MIB should be bumped; zero otherwise. 5990 */ 5991 static boolean_t 5992 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 5993 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, zoneid_t zoneid) 5994 { 5995 ipha_t *ipha; 5996 mblk_t *first_mp; 5997 boolean_t secure; 5998 unsigned char db_type; 5999 6000 first_mp = mp; 6001 if (mctl_present) { 6002 mp = mp->b_cont; 6003 secure = ipsec_in_is_secure(first_mp); 6004 ASSERT(mp != NULL); 6005 } else { 6006 /* 6007 * If this is an ICMP error being reported - which goes 6008 * up as M_CTLs, we need to convert them to M_DATA till 6009 * we finish checking with global policy because 6010 * ipsec_check_global_policy() assumes M_DATA as clear 6011 * and M_CTL as secure. 6012 */ 6013 db_type = DB_TYPE(mp); 6014 DB_TYPE(mp) = M_DATA; 6015 secure = B_FALSE; 6016 } 6017 /* 6018 * We are generating an icmp error for some inbound packet. 6019 * Called from all ip_fanout_(udp, tcp, proto) functions. 6020 * Before we generate an error, check with global policy 6021 * to see whether this is allowed to enter the system. As 6022 * there is no "conn", we are checking with global policy. 6023 */ 6024 ipha = (ipha_t *)mp->b_rptr; 6025 if (secure || ipsec_inbound_v4_policy_present) { 6026 first_mp = ipsec_check_global_policy(first_mp, NULL, 6027 ipha, NULL, mctl_present); 6028 if (first_mp == NULL) 6029 return (B_FALSE); 6030 } 6031 6032 if (!mctl_present) 6033 DB_TYPE(mp) = db_type; 6034 6035 if (flags & IP_FF_SEND_ICMP) { 6036 if (flags & IP_FF_HDR_COMPLETE) { 6037 if (ip_hdr_complete(ipha, zoneid)) { 6038 freemsg(first_mp); 6039 return (B_TRUE); 6040 } 6041 } 6042 if (flags & IP_FF_CKSUM) { 6043 /* 6044 * Have to correct checksum since 6045 * the packet might have been 6046 * fragmented and the reassembly code in ip_rput 6047 * does not restore the IP checksum. 6048 */ 6049 ipha->ipha_hdr_checksum = 0; 6050 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6051 } 6052 switch (icmp_type) { 6053 case ICMP_DEST_UNREACHABLE: 6054 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid); 6055 break; 6056 default: 6057 freemsg(first_mp); 6058 break; 6059 } 6060 } else { 6061 freemsg(first_mp); 6062 return (B_FALSE); 6063 } 6064 6065 return (B_TRUE); 6066 } 6067 6068 /* 6069 * Used to send an ICMP error message when a packet is received for 6070 * a protocol that is not supported. The mblk passed as argument 6071 * is consumed by this function. 6072 */ 6073 void 6074 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid) 6075 { 6076 mblk_t *mp; 6077 ipha_t *ipha; 6078 ill_t *ill; 6079 ipsec_in_t *ii; 6080 6081 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6082 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6083 6084 mp = ipsec_mp->b_cont; 6085 ipsec_mp->b_cont = NULL; 6086 ipha = (ipha_t *)mp->b_rptr; 6087 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 6088 if (ip_fanout_send_icmp(q, mp, flags, ICMP_DEST_UNREACHABLE, 6089 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid)) { 6090 BUMP_MIB(&ip_mib, ipInUnknownProtos); 6091 } 6092 } else { 6093 /* Get ill from index in ipsec_in_t. */ 6094 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 6095 B_TRUE, NULL, NULL, NULL, NULL); 6096 if (ill != NULL) { 6097 if (ip_fanout_send_icmp_v6(q, mp, flags, 6098 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 6099 0, B_FALSE, zoneid)) { 6100 BUMP_MIB(ill->ill_ip6_mib, ipv6InUnknownProtos); 6101 } 6102 6103 ill_refrele(ill); 6104 } else { /* re-link for the freemsg() below. */ 6105 ipsec_mp->b_cont = mp; 6106 } 6107 } 6108 6109 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 6110 freemsg(ipsec_mp); 6111 } 6112 6113 /* 6114 * See if the inbound datagram has had IPsec processing applied to it. 6115 */ 6116 boolean_t 6117 ipsec_in_is_secure(mblk_t *ipsec_mp) 6118 { 6119 ipsec_in_t *ii; 6120 6121 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6122 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6123 6124 if (ii->ipsec_in_loopback) { 6125 return (ii->ipsec_in_secure); 6126 } else { 6127 return (ii->ipsec_in_ah_sa != NULL || 6128 ii->ipsec_in_esp_sa != NULL || 6129 ii->ipsec_in_decaps); 6130 } 6131 } 6132 6133 /* 6134 * Handle protocols with which IP is less intimate. There 6135 * can be more than one stream bound to a particular 6136 * protocol. When this is the case, normally each one gets a copy 6137 * of any incoming packets. 6138 * 6139 * IPSEC NOTE : 6140 * 6141 * Don't allow a secure packet going up a non-secure connection. 6142 * We don't allow this because 6143 * 6144 * 1) Reply might go out in clear which will be dropped at 6145 * the sending side. 6146 * 2) If the reply goes out in clear it will give the 6147 * adversary enough information for getting the key in 6148 * most of the cases. 6149 * 6150 * Moreover getting a secure packet when we expect clear 6151 * implies that SA's were added without checking for 6152 * policy on both ends. This should not happen once ISAKMP 6153 * is used to negotiate SAs as SAs will be added only after 6154 * verifying the policy. 6155 * 6156 * NOTE : If the packet was tunneled and not multicast we only send 6157 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 6158 * back to delivering packets to AF_INET6 raw sockets. 6159 * 6160 * IPQoS Notes: 6161 * Once we have determined the client, invoke IPPF processing. 6162 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6163 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6164 * ip_policy will be false. 6165 * 6166 * Zones notes: 6167 * Currently only applications in the global zone can create raw sockets for 6168 * protocols other than ICMP. So unlike the broadcast / multicast case of 6169 * ip_fanout_udp(), we only send a copy of the packet to streams in the 6170 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 6171 */ 6172 static void 6173 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 6174 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 6175 zoneid_t zoneid) 6176 { 6177 queue_t *rq; 6178 mblk_t *mp1, *first_mp1; 6179 uint_t protocol = ipha->ipha_protocol; 6180 ipaddr_t dst; 6181 boolean_t one_only; 6182 mblk_t *first_mp = mp; 6183 boolean_t secure; 6184 uint32_t ill_index; 6185 conn_t *connp, *first_connp, *next_connp; 6186 connf_t *connfp; 6187 boolean_t shared_addr; 6188 6189 if (mctl_present) { 6190 mp = first_mp->b_cont; 6191 secure = ipsec_in_is_secure(first_mp); 6192 ASSERT(mp != NULL); 6193 } else { 6194 secure = B_FALSE; 6195 } 6196 dst = ipha->ipha_dst; 6197 /* 6198 * If the packet was tunneled and not multicast we only send to it 6199 * the first match. 6200 */ 6201 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 6202 !CLASSD(dst)); 6203 6204 shared_addr = (zoneid == ALL_ZONES); 6205 if (shared_addr) { 6206 /* 6207 * We don't allow multilevel ports for raw IP, so no need to 6208 * check for that here. 6209 */ 6210 zoneid = tsol_packet_to_zoneid(mp); 6211 } 6212 6213 connfp = &ipcl_proto_fanout[protocol]; 6214 mutex_enter(&connfp->connf_lock); 6215 connp = connfp->connf_head; 6216 for (connp = connfp->connf_head; connp != NULL; 6217 connp = connp->conn_next) { 6218 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, 6219 zoneid) && 6220 (!is_system_labeled() || 6221 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6222 connp))) 6223 break; 6224 } 6225 6226 if (connp == NULL || connp->conn_upq == NULL) { 6227 /* 6228 * No one bound to these addresses. Is 6229 * there a client that wants all 6230 * unclaimed datagrams? 6231 */ 6232 mutex_exit(&connfp->connf_lock); 6233 /* 6234 * Check for IPPROTO_ENCAP... 6235 */ 6236 if (protocol == IPPROTO_ENCAP && ip_g_mrouter) { 6237 /* 6238 * XXX If an IPsec mblk is here on a multicast 6239 * tunnel (using ip_mroute stuff), what should 6240 * I do? 6241 * 6242 * For now, just free the IPsec mblk before 6243 * passing it up to the multicast routing 6244 * stuff. 6245 * 6246 * BTW, If I match a configured IP-in-IP 6247 * tunnel, ip_mroute_decap will never be 6248 * called. 6249 */ 6250 if (mp != first_mp) 6251 freeb(first_mp); 6252 ip_mroute_decap(q, mp); 6253 } else { 6254 /* 6255 * Otherwise send an ICMP protocol unreachable. 6256 */ 6257 if (ip_fanout_send_icmp(q, first_mp, flags, 6258 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 6259 mctl_present, zoneid)) { 6260 BUMP_MIB(&ip_mib, ipInUnknownProtos); 6261 } 6262 } 6263 return; 6264 } 6265 CONN_INC_REF(connp); 6266 first_connp = connp; 6267 6268 /* 6269 * Only send message to one tunnel driver by immediately 6270 * terminating the loop. 6271 */ 6272 connp = one_only ? NULL : connp->conn_next; 6273 6274 for (;;) { 6275 while (connp != NULL) { 6276 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 6277 flags, zoneid) && 6278 (!is_system_labeled() || 6279 tsol_receive_local(mp, &dst, IPV4_VERSION, 6280 shared_addr, connp))) 6281 break; 6282 connp = connp->conn_next; 6283 } 6284 6285 /* 6286 * Copy the packet. 6287 */ 6288 if (connp == NULL || connp->conn_upq == NULL || 6289 (((first_mp1 = dupmsg(first_mp)) == NULL) && 6290 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 6291 /* 6292 * No more interested clients or memory 6293 * allocation failed 6294 */ 6295 connp = first_connp; 6296 break; 6297 } 6298 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 6299 CONN_INC_REF(connp); 6300 mutex_exit(&connfp->connf_lock); 6301 rq = connp->conn_rq; 6302 if (!canputnext(rq)) { 6303 if (flags & IP_FF_RAWIP) { 6304 BUMP_MIB(&ip_mib, rawipInOverflows); 6305 } else { 6306 BUMP_MIB(&icmp_mib, icmpInOverflows); 6307 } 6308 6309 freemsg(first_mp1); 6310 } else { 6311 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 6312 first_mp1 = ipsec_check_inbound_policy 6313 (first_mp1, connp, ipha, NULL, 6314 mctl_present); 6315 } 6316 if (first_mp1 != NULL) { 6317 /* 6318 * ip_fanout_proto also gets called from 6319 * icmp_inbound_error_fanout, in which case 6320 * the msg type is M_CTL. Don't add info 6321 * in this case for the time being. In future 6322 * when there is a need for knowing the 6323 * inbound iface index for ICMP error msgs, 6324 * then this can be changed. 6325 */ 6326 if ((connp->conn_recvif != 0) && 6327 (mp->b_datap->db_type != M_CTL)) { 6328 /* 6329 * the actual data will be 6330 * contained in b_cont upon 6331 * successful return of the 6332 * following call else 6333 * original mblk is returned 6334 */ 6335 ASSERT(recv_ill != NULL); 6336 mp1 = ip_add_info(mp1, recv_ill, 6337 IPF_RECVIF); 6338 } 6339 BUMP_MIB(&ip_mib, ipInDelivers); 6340 if (mctl_present) 6341 freeb(first_mp1); 6342 putnext(rq, mp1); 6343 } 6344 } 6345 mutex_enter(&connfp->connf_lock); 6346 /* Follow the next pointer before releasing the conn. */ 6347 next_connp = connp->conn_next; 6348 CONN_DEC_REF(connp); 6349 connp = next_connp; 6350 } 6351 6352 /* Last one. Send it upstream. */ 6353 mutex_exit(&connfp->connf_lock); 6354 6355 /* 6356 * If this packet is coming from icmp_inbound_error_fanout ip_policy 6357 * will be set to false. 6358 */ 6359 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 6360 ill_index = ill->ill_phyint->phyint_ifindex; 6361 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6362 if (mp == NULL) { 6363 CONN_DEC_REF(connp); 6364 if (mctl_present) { 6365 freeb(first_mp); 6366 } 6367 return; 6368 } 6369 } 6370 6371 rq = connp->conn_rq; 6372 if (!canputnext(rq)) { 6373 if (flags & IP_FF_RAWIP) { 6374 BUMP_MIB(&ip_mib, rawipInOverflows); 6375 } else { 6376 BUMP_MIB(&icmp_mib, icmpInOverflows); 6377 } 6378 6379 freemsg(first_mp); 6380 } else { 6381 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 6382 first_mp = ipsec_check_inbound_policy(first_mp, connp, 6383 ipha, NULL, mctl_present); 6384 } 6385 if (first_mp != NULL) { 6386 /* 6387 * ip_fanout_proto also gets called 6388 * from icmp_inbound_error_fanout, in 6389 * which case the msg type is M_CTL. 6390 * Don't add info in this case for time 6391 * being. In future when there is a 6392 * need for knowing the inbound iface 6393 * index for ICMP error msgs, then this 6394 * can be changed 6395 */ 6396 if ((connp->conn_recvif != 0) && 6397 (mp->b_datap->db_type != M_CTL)) { 6398 /* 6399 * the actual data will be contained in 6400 * b_cont upon successful return 6401 * of the following call else original 6402 * mblk is returned 6403 */ 6404 ASSERT(recv_ill != NULL); 6405 mp = ip_add_info(mp, recv_ill, IPF_RECVIF); 6406 } 6407 BUMP_MIB(&ip_mib, ipInDelivers); 6408 putnext(rq, mp); 6409 if (mctl_present) 6410 freeb(first_mp); 6411 } 6412 } 6413 CONN_DEC_REF(connp); 6414 } 6415 6416 /* 6417 * Fanout for TCP packets 6418 * The caller puts <fport, lport> in the ports parameter. 6419 * 6420 * IPQoS Notes 6421 * Before sending it to the client, invoke IPPF processing. 6422 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6423 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6424 * ip_policy is false. 6425 */ 6426 static void 6427 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 6428 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 6429 { 6430 mblk_t *first_mp; 6431 boolean_t secure; 6432 uint32_t ill_index; 6433 int ip_hdr_len; 6434 tcph_t *tcph; 6435 boolean_t syn_present = B_FALSE; 6436 conn_t *connp; 6437 6438 first_mp = mp; 6439 if (mctl_present) { 6440 ASSERT(first_mp->b_datap->db_type == M_CTL); 6441 mp = first_mp->b_cont; 6442 secure = ipsec_in_is_secure(first_mp); 6443 ASSERT(mp != NULL); 6444 } else { 6445 secure = B_FALSE; 6446 } 6447 6448 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 6449 6450 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid)) == 6451 NULL) { 6452 /* 6453 * No connected connection or listener. Send a 6454 * TH_RST via tcp_xmit_listeners_reset. 6455 */ 6456 6457 /* Initiate IPPf processing, if needed. */ 6458 if (IPP_ENABLED(IPP_LOCAL_IN)) { 6459 uint32_t ill_index; 6460 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6461 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 6462 if (first_mp == NULL) 6463 return; 6464 } 6465 BUMP_MIB(&ip_mib, ipInDelivers); 6466 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", 6467 zoneid)); 6468 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid); 6469 return; 6470 } 6471 6472 /* 6473 * Allocate the SYN for the TCP connection here itself 6474 */ 6475 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 6476 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 6477 if (IPCL_IS_TCP(connp)) { 6478 squeue_t *sqp; 6479 6480 /* 6481 * For fused tcp loopback, assign the eager's 6482 * squeue to be that of the active connect's. 6483 * Note that we don't check for IP_FF_LOOPBACK 6484 * here since this routine gets called only 6485 * for loopback (unlike the IPv6 counterpart). 6486 */ 6487 ASSERT(Q_TO_CONN(q) != NULL); 6488 if (do_tcp_fusion && 6489 !CONN_INBOUND_POLICY_PRESENT(connp) && !secure && 6490 !IPP_ENABLED(IPP_LOCAL_IN) && !ip_policy && 6491 IPCL_IS_TCP(Q_TO_CONN(q))) { 6492 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 6493 sqp = Q_TO_CONN(q)->conn_sqp; 6494 } else { 6495 sqp = IP_SQUEUE_GET(lbolt); 6496 } 6497 6498 mp->b_datap->db_struioflag |= STRUIO_EAGER; 6499 DB_CKSUMSTART(mp) = (intptr_t)sqp; 6500 syn_present = B_TRUE; 6501 } 6502 } 6503 6504 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 6505 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 6506 if ((flags & TH_RST) || (flags & TH_URG)) { 6507 CONN_DEC_REF(connp); 6508 freemsg(first_mp); 6509 return; 6510 } 6511 if (flags & TH_ACK) { 6512 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid); 6513 CONN_DEC_REF(connp); 6514 return; 6515 } 6516 6517 CONN_DEC_REF(connp); 6518 freemsg(first_mp); 6519 return; 6520 } 6521 6522 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 6523 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6524 NULL, mctl_present); 6525 if (first_mp == NULL) { 6526 CONN_DEC_REF(connp); 6527 return; 6528 } 6529 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 6530 ASSERT(syn_present); 6531 if (mctl_present) { 6532 ASSERT(first_mp != mp); 6533 first_mp->b_datap->db_struioflag |= 6534 STRUIO_POLICY; 6535 } else { 6536 ASSERT(first_mp == mp); 6537 mp->b_datap->db_struioflag &= 6538 ~STRUIO_EAGER; 6539 mp->b_datap->db_struioflag |= 6540 STRUIO_POLICY; 6541 } 6542 } else { 6543 /* 6544 * Discard first_mp early since we're dealing with a 6545 * fully-connected conn_t and tcp doesn't do policy in 6546 * this case. 6547 */ 6548 if (mctl_present) { 6549 freeb(first_mp); 6550 mctl_present = B_FALSE; 6551 } 6552 first_mp = mp; 6553 } 6554 } 6555 6556 /* 6557 * Initiate policy processing here if needed. If we get here from 6558 * icmp_inbound_error_fanout, ip_policy is false. 6559 */ 6560 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 6561 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6562 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6563 if (mp == NULL) { 6564 CONN_DEC_REF(connp); 6565 if (mctl_present) 6566 freeb(first_mp); 6567 return; 6568 } else if (mctl_present) { 6569 ASSERT(first_mp != mp); 6570 first_mp->b_cont = mp; 6571 } else { 6572 first_mp = mp; 6573 } 6574 } 6575 6576 6577 6578 /* Handle IPv6 socket options. */ 6579 if (!syn_present && 6580 connp->conn_ipv6_recvpktinfo && (flags & IP_FF_IP6INFO)) { 6581 /* Add header */ 6582 ASSERT(recv_ill != NULL); 6583 mp = ip_add_info(mp, recv_ill, IPF_RECVIF); 6584 if (mp == NULL) { 6585 CONN_DEC_REF(connp); 6586 if (mctl_present) 6587 freeb(first_mp); 6588 return; 6589 } else if (mctl_present) { 6590 /* 6591 * ip_add_info might return a new mp. 6592 */ 6593 ASSERT(first_mp != mp); 6594 first_mp->b_cont = mp; 6595 } else { 6596 first_mp = mp; 6597 } 6598 } 6599 6600 BUMP_MIB(&ip_mib, ipInDelivers); 6601 if (IPCL_IS_TCP(connp)) { 6602 (*ip_input_proc)(connp->conn_sqp, first_mp, 6603 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); 6604 } else { 6605 putnext(connp->conn_rq, first_mp); 6606 CONN_DEC_REF(connp); 6607 } 6608 } 6609 6610 /* 6611 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 6612 * We are responsible for disposing of mp, such as by freemsg() or putnext() 6613 * Caller is responsible for dropping references to the conn, and freeing 6614 * first_mp. 6615 * 6616 * IPQoS Notes 6617 * Before sending it to the client, invoke IPPF processing. Policy processing 6618 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 6619 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 6620 * ip_wput_local, ip_policy is false. 6621 */ 6622 static void 6623 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 6624 boolean_t secure, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 6625 boolean_t ip_policy) 6626 { 6627 boolean_t mctl_present = (first_mp != NULL); 6628 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 6629 uint32_t ill_index; 6630 6631 if (mctl_present) 6632 first_mp->b_cont = mp; 6633 else 6634 first_mp = mp; 6635 6636 if (CONN_UDP_FLOWCTLD(connp)) { 6637 BUMP_MIB(&ip_mib, udpInOverflows); 6638 freemsg(first_mp); 6639 return; 6640 } 6641 6642 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 6643 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6644 NULL, mctl_present); 6645 if (first_mp == NULL) 6646 return; /* Freed by ipsec_check_inbound_policy(). */ 6647 } 6648 if (mctl_present) 6649 freeb(first_mp); 6650 6651 if (connp->conn_recvif) 6652 in_flags = IPF_RECVIF; 6653 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 6654 in_flags |= IPF_RECVSLLA; 6655 6656 /* Handle IPv6 options. */ 6657 if (connp->conn_ipv6_recvpktinfo && (flags & IP_FF_IP6INFO)) 6658 in_flags |= IPF_RECVIF; 6659 6660 /* 6661 * Initiate IPPF processing here, if needed. Note first_mp won't be 6662 * freed if the packet is dropped. The caller will do so. 6663 */ 6664 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 6665 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6666 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6667 if (mp == NULL) { 6668 return; 6669 } 6670 } 6671 if ((in_flags != 0) && 6672 (mp->b_datap->db_type != M_CTL)) { 6673 /* 6674 * The actual data will be contained in b_cont 6675 * upon successful return of the following call 6676 * else original mblk is returned 6677 */ 6678 ASSERT(recv_ill != NULL); 6679 mp = ip_add_info(mp, recv_ill, in_flags); 6680 } 6681 BUMP_MIB(&ip_mib, ipInDelivers); 6682 6683 /* Send it upstream */ 6684 CONN_UDP_RECV(connp, mp); 6685 } 6686 6687 /* 6688 * Fanout for UDP packets. 6689 * The caller puts <fport, lport> in the ports parameter. 6690 * 6691 * If SO_REUSEADDR is set all multicast and broadcast packets 6692 * will be delivered to all streams bound to the same port. 6693 * 6694 * Zones notes: 6695 * Multicast and broadcast packets will be distributed to streams in all zones. 6696 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 6697 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 6698 * packets. To maintain this behavior with multiple zones, the conns are grouped 6699 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 6700 * each zone. If unset, all the following conns in the same zone are skipped. 6701 */ 6702 static void 6703 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 6704 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 6705 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 6706 { 6707 uint32_t dstport, srcport; 6708 ipaddr_t dst; 6709 mblk_t *first_mp; 6710 boolean_t secure; 6711 in6_addr_t v6src; 6712 conn_t *connp; 6713 connf_t *connfp; 6714 conn_t *first_connp; 6715 conn_t *next_connp; 6716 mblk_t *mp1, *first_mp1; 6717 ipaddr_t src; 6718 zoneid_t last_zoneid; 6719 boolean_t reuseaddr; 6720 boolean_t shared_addr; 6721 6722 first_mp = mp; 6723 if (mctl_present) { 6724 mp = first_mp->b_cont; 6725 first_mp->b_cont = NULL; 6726 secure = ipsec_in_is_secure(first_mp); 6727 ASSERT(mp != NULL); 6728 } else { 6729 first_mp = NULL; 6730 secure = B_FALSE; 6731 } 6732 6733 /* Extract ports in net byte order */ 6734 dstport = htons(ntohl(ports) & 0xFFFF); 6735 srcport = htons(ntohl(ports) >> 16); 6736 dst = ipha->ipha_dst; 6737 src = ipha->ipha_src; 6738 6739 shared_addr = (zoneid == ALL_ZONES); 6740 if (shared_addr) { 6741 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 6742 if (zoneid == ALL_ZONES) 6743 zoneid = tsol_packet_to_zoneid(mp); 6744 } 6745 6746 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 6747 mutex_enter(&connfp->connf_lock); 6748 connp = connfp->connf_head; 6749 if (!broadcast && !CLASSD(dst)) { 6750 /* 6751 * Not broadcast or multicast. Send to the one (first) 6752 * client we find. No need to check conn_wantpacket() 6753 * since IP_BOUND_IF/conn_incoming_ill does not apply to 6754 * IPv4 unicast packets. 6755 */ 6756 while ((connp != NULL) && 6757 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || 6758 !IPCL_ZONE_MATCH(connp, zoneid))) { 6759 connp = connp->conn_next; 6760 } 6761 6762 if (connp == NULL || connp->conn_upq == NULL) 6763 goto notfound; 6764 6765 if (is_system_labeled() && 6766 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6767 connp)) 6768 goto notfound; 6769 6770 CONN_INC_REF(connp); 6771 mutex_exit(&connfp->connf_lock); 6772 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, 6773 recv_ill, ip_policy); 6774 IP_STAT(ip_udp_fannorm); 6775 CONN_DEC_REF(connp); 6776 return; 6777 } 6778 6779 /* 6780 * Broadcast and multicast case 6781 * 6782 * Need to check conn_wantpacket(). 6783 * If SO_REUSEADDR has been set on the first we send the 6784 * packet to all clients that have joined the group and 6785 * match the port. 6786 */ 6787 6788 while (connp != NULL) { 6789 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 6790 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 6791 (!is_system_labeled() || 6792 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6793 connp))) 6794 break; 6795 connp = connp->conn_next; 6796 } 6797 6798 if (connp == NULL || connp->conn_upq == NULL) 6799 goto notfound; 6800 6801 first_connp = connp; 6802 /* 6803 * When SO_REUSEADDR is not set, send the packet only to the first 6804 * matching connection in its zone by keeping track of the zoneid. 6805 */ 6806 reuseaddr = first_connp->conn_reuseaddr; 6807 last_zoneid = first_connp->conn_zoneid; 6808 6809 CONN_INC_REF(connp); 6810 connp = connp->conn_next; 6811 for (;;) { 6812 while (connp != NULL) { 6813 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 6814 (reuseaddr || connp->conn_zoneid != last_zoneid) && 6815 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 6816 (!is_system_labeled() || 6817 tsol_receive_local(mp, &dst, IPV4_VERSION, 6818 shared_addr, connp))) 6819 break; 6820 connp = connp->conn_next; 6821 } 6822 /* 6823 * Just copy the data part alone. The mctl part is 6824 * needed just for verifying policy and it is never 6825 * sent up. 6826 */ 6827 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 6828 ((mp1 = copymsg(mp)) == NULL))) { 6829 /* 6830 * No more interested clients or memory 6831 * allocation failed 6832 */ 6833 connp = first_connp; 6834 break; 6835 } 6836 if (connp->conn_zoneid != last_zoneid) { 6837 /* 6838 * Update the zoneid so that the packet isn't sent to 6839 * any more conns in the same zone unless SO_REUSEADDR 6840 * is set. 6841 */ 6842 reuseaddr = connp->conn_reuseaddr; 6843 last_zoneid = connp->conn_zoneid; 6844 } 6845 if (first_mp != NULL) { 6846 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 6847 ipsec_info_type == IPSEC_IN); 6848 first_mp1 = ipsec_in_tag(first_mp, NULL); 6849 if (first_mp1 == NULL) { 6850 freemsg(mp1); 6851 connp = first_connp; 6852 break; 6853 } 6854 } else { 6855 first_mp1 = NULL; 6856 } 6857 CONN_INC_REF(connp); 6858 mutex_exit(&connfp->connf_lock); 6859 /* 6860 * IPQoS notes: We don't send the packet for policy 6861 * processing here, will do it for the last one (below). 6862 * i.e. we do it per-packet now, but if we do policy 6863 * processing per-conn, then we would need to do it 6864 * here too. 6865 */ 6866 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, 6867 ipha, flags, recv_ill, B_FALSE); 6868 mutex_enter(&connfp->connf_lock); 6869 /* Follow the next pointer before releasing the conn. */ 6870 next_connp = connp->conn_next; 6871 IP_STAT(ip_udp_fanmb); 6872 CONN_DEC_REF(connp); 6873 connp = next_connp; 6874 } 6875 6876 /* Last one. Send it upstream. */ 6877 mutex_exit(&connfp->connf_lock); 6878 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, recv_ill, 6879 ip_policy); 6880 IP_STAT(ip_udp_fanmb); 6881 CONN_DEC_REF(connp); 6882 return; 6883 6884 notfound: 6885 6886 mutex_exit(&connfp->connf_lock); 6887 IP_STAT(ip_udp_fanothers); 6888 /* 6889 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 6890 * have already been matched above, since they live in the IPv4 6891 * fanout tables. This implies we only need to 6892 * check for IPv6 in6addr_any endpoints here. 6893 * Thus we compare using ipv6_all_zeros instead of the destination 6894 * address, except for the multicast group membership lookup which 6895 * uses the IPv4 destination. 6896 */ 6897 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 6898 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 6899 mutex_enter(&connfp->connf_lock); 6900 connp = connfp->connf_head; 6901 if (!broadcast && !CLASSD(dst)) { 6902 while (connp != NULL) { 6903 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 6904 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && 6905 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 6906 !connp->conn_ipv6_v6only) 6907 break; 6908 connp = connp->conn_next; 6909 } 6910 6911 if (connp != NULL && is_system_labeled() && 6912 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6913 connp)) 6914 connp = NULL; 6915 6916 if (connp == NULL || connp->conn_upq == NULL) { 6917 /* 6918 * No one bound to this port. Is 6919 * there a client that wants all 6920 * unclaimed datagrams? 6921 */ 6922 mutex_exit(&connfp->connf_lock); 6923 6924 if (mctl_present) 6925 first_mp->b_cont = mp; 6926 else 6927 first_mp = mp; 6928 if (ipcl_proto_search(IPPROTO_UDP) != NULL) { 6929 ip_fanout_proto(q, first_mp, ill, ipha, 6930 flags | IP_FF_RAWIP, mctl_present, 6931 ip_policy, recv_ill, zoneid); 6932 } else { 6933 if (ip_fanout_send_icmp(q, first_mp, flags, 6934 ICMP_DEST_UNREACHABLE, 6935 ICMP_PORT_UNREACHABLE, 6936 mctl_present, zoneid)) { 6937 BUMP_MIB(&ip_mib, udpNoPorts); 6938 } 6939 } 6940 return; 6941 } 6942 6943 CONN_INC_REF(connp); 6944 mutex_exit(&connfp->connf_lock); 6945 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, 6946 recv_ill, ip_policy); 6947 CONN_DEC_REF(connp); 6948 return; 6949 } 6950 /* 6951 * IPv4 multicast packet being delivered to an AF_INET6 6952 * in6addr_any endpoint. 6953 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 6954 * and not conn_wantpacket_v6() since any multicast membership is 6955 * for an IPv4-mapped multicast address. 6956 * The packet is sent to all clients in all zones that have joined the 6957 * group and match the port. 6958 */ 6959 while (connp != NULL) { 6960 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 6961 srcport, v6src) && 6962 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 6963 (!is_system_labeled() || 6964 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6965 connp))) 6966 break; 6967 connp = connp->conn_next; 6968 } 6969 6970 if (connp == NULL || connp->conn_upq == NULL) { 6971 /* 6972 * No one bound to this port. Is 6973 * there a client that wants all 6974 * unclaimed datagrams? 6975 */ 6976 mutex_exit(&connfp->connf_lock); 6977 6978 if (mctl_present) 6979 first_mp->b_cont = mp; 6980 else 6981 first_mp = mp; 6982 if (ipcl_proto_search(IPPROTO_UDP) != NULL) { 6983 ip_fanout_proto(q, first_mp, ill, ipha, 6984 flags | IP_FF_RAWIP, mctl_present, ip_policy, 6985 recv_ill, zoneid); 6986 } else { 6987 /* 6988 * We used to attempt to send an icmp error here, but 6989 * since this is known to be a multicast packet 6990 * and we don't send icmp errors in response to 6991 * multicast, just drop the packet and give up sooner. 6992 */ 6993 BUMP_MIB(&ip_mib, udpNoPorts); 6994 freemsg(first_mp); 6995 } 6996 return; 6997 } 6998 6999 first_connp = connp; 7000 7001 CONN_INC_REF(connp); 7002 connp = connp->conn_next; 7003 for (;;) { 7004 while (connp != NULL) { 7005 if (IPCL_UDP_MATCH_V6(connp, dstport, 7006 ipv6_all_zeros, srcport, v6src) && 7007 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7008 (!is_system_labeled() || 7009 tsol_receive_local(mp, &dst, IPV4_VERSION, 7010 shared_addr, connp))) 7011 break; 7012 connp = connp->conn_next; 7013 } 7014 /* 7015 * Just copy the data part alone. The mctl part is 7016 * needed just for verifying policy and it is never 7017 * sent up. 7018 */ 7019 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7020 ((mp1 = copymsg(mp)) == NULL))) { 7021 /* 7022 * No more intested clients or memory 7023 * allocation failed 7024 */ 7025 connp = first_connp; 7026 break; 7027 } 7028 if (first_mp != NULL) { 7029 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7030 ipsec_info_type == IPSEC_IN); 7031 first_mp1 = ipsec_in_tag(first_mp, NULL); 7032 if (first_mp1 == NULL) { 7033 freemsg(mp1); 7034 connp = first_connp; 7035 break; 7036 } 7037 } else { 7038 first_mp1 = NULL; 7039 } 7040 CONN_INC_REF(connp); 7041 mutex_exit(&connfp->connf_lock); 7042 /* 7043 * IPQoS notes: We don't send the packet for policy 7044 * processing here, will do it for the last one (below). 7045 * i.e. we do it per-packet now, but if we do policy 7046 * processing per-conn, then we would need to do it 7047 * here too. 7048 */ 7049 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, 7050 ipha, flags, recv_ill, B_FALSE); 7051 mutex_enter(&connfp->connf_lock); 7052 /* Follow the next pointer before releasing the conn. */ 7053 next_connp = connp->conn_next; 7054 CONN_DEC_REF(connp); 7055 connp = next_connp; 7056 } 7057 7058 /* Last one. Send it upstream. */ 7059 mutex_exit(&connfp->connf_lock); 7060 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, recv_ill, 7061 ip_policy); 7062 CONN_DEC_REF(connp); 7063 } 7064 7065 /* 7066 * Complete the ip_wput header so that it 7067 * is possible to generate ICMP 7068 * errors. 7069 */ 7070 int 7071 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid) 7072 { 7073 ire_t *ire; 7074 7075 if (ipha->ipha_src == INADDR_ANY) { 7076 ire = ire_lookup_local(zoneid); 7077 if (ire == NULL) { 7078 ip1dbg(("ip_hdr_complete: no source IRE\n")); 7079 return (1); 7080 } 7081 ipha->ipha_src = ire->ire_addr; 7082 ire_refrele(ire); 7083 } 7084 ipha->ipha_ttl = ip_def_ttl; 7085 ipha->ipha_hdr_checksum = 0; 7086 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 7087 return (0); 7088 } 7089 7090 /* 7091 * Nobody should be sending 7092 * packets up this stream 7093 */ 7094 static void 7095 ip_lrput(queue_t *q, mblk_t *mp) 7096 { 7097 mblk_t *mp1; 7098 7099 switch (mp->b_datap->db_type) { 7100 case M_FLUSH: 7101 /* Turn around */ 7102 if (*mp->b_rptr & FLUSHW) { 7103 *mp->b_rptr &= ~FLUSHR; 7104 qreply(q, mp); 7105 return; 7106 } 7107 break; 7108 } 7109 /* Could receive messages that passed through ar_rput */ 7110 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 7111 mp1->b_prev = mp1->b_next = NULL; 7112 freemsg(mp); 7113 } 7114 7115 /* Nobody should be sending packets down this stream */ 7116 /* ARGSUSED */ 7117 void 7118 ip_lwput(queue_t *q, mblk_t *mp) 7119 { 7120 freemsg(mp); 7121 } 7122 7123 /* 7124 * Move the first hop in any source route to ipha_dst and remove that part of 7125 * the source route. Called by other protocols. Errors in option formatting 7126 * are ignored - will be handled by ip_wput_options Return the final 7127 * destination (either ipha_dst or the last entry in a source route.) 7128 */ 7129 ipaddr_t 7130 ip_massage_options(ipha_t *ipha) 7131 { 7132 ipoptp_t opts; 7133 uchar_t *opt; 7134 uint8_t optval; 7135 uint8_t optlen; 7136 ipaddr_t dst; 7137 int i; 7138 ire_t *ire; 7139 7140 ip2dbg(("ip_massage_options\n")); 7141 dst = ipha->ipha_dst; 7142 for (optval = ipoptp_first(&opts, ipha); 7143 optval != IPOPT_EOL; 7144 optval = ipoptp_next(&opts)) { 7145 opt = opts.ipoptp_cur; 7146 switch (optval) { 7147 uint8_t off; 7148 case IPOPT_SSRR: 7149 case IPOPT_LSRR: 7150 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 7151 ip1dbg(("ip_massage_options: bad src route\n")); 7152 break; 7153 } 7154 optlen = opts.ipoptp_len; 7155 off = opt[IPOPT_OFFSET]; 7156 off--; 7157 redo_srr: 7158 if (optlen < IP_ADDR_LEN || 7159 off > optlen - IP_ADDR_LEN) { 7160 /* End of source route */ 7161 ip1dbg(("ip_massage_options: end of SR\n")); 7162 break; 7163 } 7164 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 7165 ip1dbg(("ip_massage_options: next hop 0x%x\n", 7166 ntohl(dst))); 7167 /* 7168 * Check if our address is present more than 7169 * once as consecutive hops in source route. 7170 * XXX verify per-interface ip_forwarding 7171 * for source route? 7172 */ 7173 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 7174 ALL_ZONES, NULL, MATCH_IRE_TYPE); 7175 if (ire != NULL) { 7176 ire_refrele(ire); 7177 off += IP_ADDR_LEN; 7178 goto redo_srr; 7179 } 7180 if (dst == htonl(INADDR_LOOPBACK)) { 7181 ip1dbg(("ip_massage_options: loopback addr in " 7182 "source route!\n")); 7183 break; 7184 } 7185 /* 7186 * Update ipha_dst to be the first hop and remove the 7187 * first hop from the source route (by overwriting 7188 * part of the option with NOP options). 7189 */ 7190 ipha->ipha_dst = dst; 7191 /* Put the last entry in dst */ 7192 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 7193 3; 7194 bcopy(&opt[off], &dst, IP_ADDR_LEN); 7195 7196 ip1dbg(("ip_massage_options: last hop 0x%x\n", 7197 ntohl(dst))); 7198 /* Move down and overwrite */ 7199 opt[IP_ADDR_LEN] = opt[0]; 7200 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 7201 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 7202 for (i = 0; i < IP_ADDR_LEN; i++) 7203 opt[i] = IPOPT_NOP; 7204 break; 7205 } 7206 } 7207 return (dst); 7208 } 7209 7210 /* 7211 * This function's job is to forward data to the reverse tunnel (FA->HA) 7212 * after doing a few checks. It is assumed that the incoming interface 7213 * of the packet is always different than the outgoing interface and the 7214 * ire_type of the found ire has to be a non-resolver type. 7215 * 7216 * IPQoS notes 7217 * IP policy is invoked twice for a forwarded packet, once on the read side 7218 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 7219 * enabled. 7220 */ 7221 static void 7222 ip_mrtun_forward(ire_t *ire, ill_t *in_ill, mblk_t *mp) 7223 { 7224 ipha_t *ipha; 7225 queue_t *q; 7226 uint32_t pkt_len; 7227 #define rptr ((uchar_t *)ipha) 7228 uint32_t sum; 7229 uint32_t max_frag; 7230 mblk_t *first_mp; 7231 uint32_t ill_index; 7232 ipxmit_state_t pktxmit_state; 7233 ill_t *out_ill; 7234 7235 ASSERT(ire != NULL); 7236 ASSERT(ire->ire_ipif->ipif_net_type == IRE_IF_NORESOLVER); 7237 ASSERT(ire->ire_stq != NULL); 7238 7239 /* Initiate read side IPPF processing */ 7240 if (IPP_ENABLED(IPP_FWD_IN)) { 7241 ill_index = in_ill->ill_phyint->phyint_ifindex; 7242 ip_process(IPP_FWD_IN, &mp, ill_index); 7243 if (mp == NULL) { 7244 ip2dbg(("ip_mrtun_forward: inbound pkt " 7245 "dropped during IPPF processing\n")); 7246 return; 7247 } 7248 } 7249 7250 if (((in_ill->ill_flags & ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 7251 ILLF_ROUTER) == 0) || 7252 (in_ill == (ill_t *)ire->ire_stq->q_ptr)) { 7253 BUMP_MIB(&ip_mib, ipForwProhibits); 7254 ip0dbg(("ip_mrtun_forward: Can't forward :" 7255 "forwarding is not turned on\n")); 7256 goto drop_pkt; 7257 } 7258 7259 /* 7260 * Don't forward if the interface is down 7261 */ 7262 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 7263 BUMP_MIB(&ip_mib, ipInDiscards); 7264 goto drop_pkt; 7265 } 7266 7267 ipha = (ipha_t *)mp->b_rptr; 7268 pkt_len = ntohs(ipha->ipha_length); 7269 /* Adjust the checksum to reflect the ttl decrement. */ 7270 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 7271 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 7272 if (ipha->ipha_ttl-- <= 1) { 7273 if (ip_csum_hdr(ipha)) { 7274 BUMP_MIB(&ip_mib, ipInCksumErrs); 7275 goto drop_pkt; 7276 } 7277 q = ire->ire_stq; 7278 if ((first_mp = allocb(sizeof (ipsec_info_t), 7279 BPRI_HI)) == NULL) { 7280 goto drop_pkt; 7281 } 7282 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7283 /* Sent by forwarding path, and router is global zone */ 7284 icmp_time_exceeded(q, first_mp, ICMP_TTL_EXCEEDED, 7285 GLOBAL_ZONEID); 7286 return; 7287 } 7288 7289 /* Get the ill_index of the ILL */ 7290 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 7291 7292 /* 7293 * This location is chosen for the placement of the forwarding hook 7294 * because at this point we know that we have a path out for the 7295 * packet but haven't yet applied any logic (such as fragmenting) 7296 * that happen as part of transmitting the packet out. 7297 */ 7298 out_ill = ire->ire_ipif->ipif_ill; 7299 7300 DTRACE_PROBE4(ip4__forwarding__start, 7301 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 7302 7303 FW_HOOKS(ip4_forwarding_event, ipv4firewall_forwarding, 7304 MSG_FWCOOKED_FORWARD, in_ill, out_ill, ipha, mp, mp); 7305 7306 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 7307 7308 if (mp == NULL) 7309 return; 7310 pkt_len = ntohs(ipha->ipha_length); 7311 7312 /* 7313 * ip_mrtun_forward is only used by foreign agent to reverse 7314 * tunnel the incoming packet. So it does not do any option 7315 * processing for source routing. 7316 */ 7317 max_frag = ire->ire_max_frag; 7318 if (pkt_len > max_frag) { 7319 /* 7320 * It needs fragging on its way out. We haven't 7321 * verified the header checksum yet. Since we 7322 * are going to put a surely good checksum in the 7323 * outgoing header, we have to make sure that it 7324 * was good coming in. 7325 */ 7326 if (ip_csum_hdr(ipha)) { 7327 BUMP_MIB(&ip_mib, ipInCksumErrs); 7328 goto drop_pkt; 7329 } 7330 7331 /* Initiate write side IPPF processing */ 7332 if (IPP_ENABLED(IPP_FWD_OUT)) { 7333 ip_process(IPP_FWD_OUT, &mp, ill_index); 7334 if (mp == NULL) { 7335 ip2dbg(("ip_mrtun_forward: outbound pkt "\ 7336 "dropped/deferred during ip policy "\ 7337 "processing\n")); 7338 return; 7339 } 7340 } 7341 if ((first_mp = allocb(sizeof (ipsec_info_t), 7342 BPRI_HI)) == NULL) { 7343 goto drop_pkt; 7344 } 7345 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7346 mp = first_mp; 7347 7348 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID); 7349 return; 7350 } 7351 7352 ip2dbg(("ip_mrtun_forward: ire type (%d)\n", ire->ire_type)); 7353 7354 ASSERT(ire->ire_ipif != NULL); 7355 7356 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 7357 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, mblk_t *, mp); 7358 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 7359 MSG_FWCOOKED_OUT, NULL, out_ill, ipha, mp, mp); 7360 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 7361 if (mp == NULL) 7362 return; 7363 7364 /* Now send the packet to the tunnel interface */ 7365 mp->b_prev = SET_BPREV_FLAG(IPP_FWD_OUT); 7366 q = ire->ire_stq; 7367 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_FALSE); 7368 if ((pktxmit_state == SEND_FAILED) || 7369 (pktxmit_state == LLHDR_RESLV_FAILED)) { 7370 ip2dbg(("ip_mrtun_forward: failed to send packet to ill %p\n", 7371 q->q_ptr)); 7372 } 7373 7374 return; 7375 7376 drop_pkt:; 7377 ip2dbg(("ip_mrtun_forward: dropping pkt\n")); 7378 freemsg(mp); 7379 #undef rptr 7380 } 7381 7382 /* 7383 * Fills the ipsec_out_t data structure with appropriate fields and 7384 * prepends it to mp which contains the IP hdr + data that was meant 7385 * to be forwarded. Please note that ipsec_out_info data structure 7386 * is used here to communicate the outgoing ill path at ip_wput() 7387 * for the ICMP error packet. This has nothing to do with ipsec IP 7388 * security. ipsec_out_t is really used to pass the info to the module 7389 * IP where this information cannot be extracted from conn. 7390 * This functions is called by ip_mrtun_forward(). 7391 */ 7392 void 7393 ip_ipsec_out_prepend(mblk_t *first_mp, mblk_t *mp, ill_t *xmit_ill) 7394 { 7395 ipsec_out_t *io; 7396 7397 ASSERT(xmit_ill != NULL); 7398 first_mp->b_datap->db_type = M_CTL; 7399 first_mp->b_wptr += sizeof (ipsec_info_t); 7400 /* 7401 * This is to pass info to ip_wput in absence of conn. 7402 * ipsec_out_secure will be B_FALSE because of this. 7403 * Thus ipsec_out_secure being B_FALSE indicates that 7404 * this is not IPSEC security related information. 7405 */ 7406 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 7407 io = (ipsec_out_t *)first_mp->b_rptr; 7408 io->ipsec_out_type = IPSEC_OUT; 7409 io->ipsec_out_len = sizeof (ipsec_out_t); 7410 first_mp->b_cont = mp; 7411 io->ipsec_out_ill_index = 7412 xmit_ill->ill_phyint->phyint_ifindex; 7413 io->ipsec_out_xmit_if = B_TRUE; 7414 } 7415 7416 /* 7417 * Return the network mask 7418 * associated with the specified address. 7419 */ 7420 ipaddr_t 7421 ip_net_mask(ipaddr_t addr) 7422 { 7423 uchar_t *up = (uchar_t *)&addr; 7424 ipaddr_t mask = 0; 7425 uchar_t *maskp = (uchar_t *)&mask; 7426 7427 #if defined(__i386) || defined(__amd64) 7428 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 7429 #endif 7430 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 7431 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 7432 #endif 7433 if (CLASSD(addr)) { 7434 maskp[0] = 0xF0; 7435 return (mask); 7436 } 7437 if (addr == 0) 7438 return (0); 7439 maskp[0] = 0xFF; 7440 if ((up[0] & 0x80) == 0) 7441 return (mask); 7442 7443 maskp[1] = 0xFF; 7444 if ((up[0] & 0xC0) == 0x80) 7445 return (mask); 7446 7447 maskp[2] = 0xFF; 7448 if ((up[0] & 0xE0) == 0xC0) 7449 return (mask); 7450 7451 /* Must be experimental or multicast, indicate as much */ 7452 return ((ipaddr_t)0); 7453 } 7454 7455 /* 7456 * Select an ill for the packet by considering load spreading across 7457 * a different ill in the group if dst_ill is part of some group. 7458 */ 7459 ill_t * 7460 ip_newroute_get_dst_ill(ill_t *dst_ill) 7461 { 7462 ill_t *ill; 7463 7464 /* 7465 * We schedule irrespective of whether the source address is 7466 * INADDR_ANY or not. illgrp_scheduler returns a held ill. 7467 */ 7468 ill = illgrp_scheduler(dst_ill); 7469 if (ill == NULL) 7470 return (NULL); 7471 7472 /* 7473 * For groups with names ip_sioctl_groupname ensures that all 7474 * ills are of same type. For groups without names, ifgrp_insert 7475 * ensures this. 7476 */ 7477 ASSERT(dst_ill->ill_type == ill->ill_type); 7478 7479 return (ill); 7480 } 7481 7482 /* 7483 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. 7484 */ 7485 ill_t * 7486 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6) 7487 { 7488 ill_t *ret_ill; 7489 7490 ASSERT(ifindex != 0); 7491 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL); 7492 if (ret_ill == NULL || 7493 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { 7494 if (isv6) { 7495 if (ill != NULL) { 7496 BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards); 7497 } else { 7498 BUMP_MIB(&ip6_mib, ipv6OutDiscards); 7499 } 7500 ip1dbg(("ip_grab_attach_ill (IPv6): " 7501 "bad ifindex %d.\n", ifindex)); 7502 } else { 7503 BUMP_MIB(&ip_mib, ipOutDiscards); 7504 ip1dbg(("ip_grab_attach_ill (IPv4): " 7505 "bad ifindex %d.\n", ifindex)); 7506 } 7507 if (ret_ill != NULL) 7508 ill_refrele(ret_ill); 7509 freemsg(first_mp); 7510 return (NULL); 7511 } 7512 7513 return (ret_ill); 7514 } 7515 7516 /* 7517 * IPv4 - 7518 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 7519 * out a packet to a destination address for which we do not have specific 7520 * (or sufficient) routing information. 7521 * 7522 * NOTE : These are the scopes of some of the variables that point at IRE, 7523 * which needs to be followed while making any future modifications 7524 * to avoid memory leaks. 7525 * 7526 * - ire and sire are the entries looked up initially by 7527 * ire_ftable_lookup. 7528 * - ipif_ire is used to hold the interface ire associated with 7529 * the new cache ire. But it's scope is limited, so we always REFRELE 7530 * it before branching out to error paths. 7531 * - save_ire is initialized before ire_create, so that ire returned 7532 * by ire_create will not over-write the ire. We REFRELE save_ire 7533 * before breaking out of the switch. 7534 * 7535 * Thus on failures, we have to REFRELE only ire and sire, if they 7536 * are not NULL. 7537 */ 7538 void 7539 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, ill_t *in_ill, conn_t *connp, 7540 zoneid_t zoneid) 7541 { 7542 areq_t *areq; 7543 ipaddr_t gw = 0; 7544 ire_t *ire = NULL; 7545 mblk_t *res_mp; 7546 ipaddr_t *addrp; 7547 ipaddr_t nexthop_addr; 7548 ipif_t *src_ipif = NULL; 7549 ill_t *dst_ill = NULL; 7550 ipha_t *ipha; 7551 ire_t *sire = NULL; 7552 mblk_t *first_mp; 7553 ire_t *save_ire; 7554 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ 7555 ushort_t ire_marks = 0; 7556 boolean_t mctl_present; 7557 ipsec_out_t *io; 7558 mblk_t *saved_mp; 7559 ire_t *first_sire = NULL; 7560 mblk_t *copy_mp = NULL; 7561 mblk_t *xmit_mp = NULL; 7562 ipaddr_t save_dst; 7563 uint32_t multirt_flags = 7564 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 7565 boolean_t multirt_is_resolvable; 7566 boolean_t multirt_resolve_next; 7567 boolean_t do_attach_ill = B_FALSE; 7568 boolean_t ip_nexthop = B_FALSE; 7569 tsol_ire_gw_secattr_t *attrp = NULL; 7570 tsol_gcgrp_t *gcgrp = NULL; 7571 tsol_gcgrp_addr_t ga; 7572 7573 if (ip_debug > 2) { 7574 /* ip1dbg */ 7575 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 7576 } 7577 7578 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7579 if (mctl_present) { 7580 io = (ipsec_out_t *)first_mp->b_rptr; 7581 ASSERT(io->ipsec_out_type == IPSEC_OUT); 7582 ASSERT(zoneid == io->ipsec_out_zoneid); 7583 ASSERT(zoneid != ALL_ZONES); 7584 } 7585 7586 ipha = (ipha_t *)mp->b_rptr; 7587 7588 /* All multicast lookups come through ip_newroute_ipif() */ 7589 if (CLASSD(dst)) { 7590 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 7591 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 7592 freemsg(first_mp); 7593 return; 7594 } 7595 7596 if (mctl_present && io->ipsec_out_attach_if) { 7597 /* ip_grab_attach_ill returns a held ill */ 7598 attach_ill = ip_grab_attach_ill(NULL, first_mp, 7599 io->ipsec_out_ill_index, B_FALSE); 7600 7601 /* Failure case frees things for us. */ 7602 if (attach_ill == NULL) 7603 return; 7604 7605 /* 7606 * Check if we need an ire that will not be 7607 * looked up by anybody else i.e. HIDDEN. 7608 */ 7609 if (ill_is_probeonly(attach_ill)) 7610 ire_marks = IRE_MARK_HIDDEN; 7611 } 7612 if (mctl_present && io->ipsec_out_ip_nexthop) { 7613 ip_nexthop = B_TRUE; 7614 nexthop_addr = io->ipsec_out_nexthop_addr; 7615 } 7616 /* 7617 * If this IRE is created for forwarding or it is not for 7618 * traffic for congestion controlled protocols, mark it as temporary. 7619 */ 7620 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 7621 ire_marks |= IRE_MARK_TEMPORARY; 7622 7623 /* 7624 * Get what we can from ire_ftable_lookup which will follow an IRE 7625 * chain until it gets the most specific information available. 7626 * For example, we know that there is no IRE_CACHE for this dest, 7627 * but there may be an IRE_OFFSUBNET which specifies a gateway. 7628 * ire_ftable_lookup will look up the gateway, etc. 7629 * Check if in_ill != NULL. If it is true, the packet must be 7630 * from an incoming interface where RTA_SRCIFP is set. 7631 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 7632 * to the destination, of equal netmask length in the forward table, 7633 * will be recursively explored. If no information is available 7634 * for the final gateway of that route, we force the returned ire 7635 * to be equal to sire using MATCH_IRE_PARENT. 7636 * At least, in this case we have a starting point (in the buckets) 7637 * to look for other routes to the destination in the forward table. 7638 * This is actually used only for multirouting, where a list 7639 * of routes has to be processed in sequence. 7640 * 7641 * In the process of coming up with the most specific information, 7642 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry 7643 * for the gateway (i.e., one for which the ire_nce->nce_state is 7644 * not yet ND_REACHABLE, and is in the middle of arp resolution). 7645 * Two caveats when handling incomplete ire's in ip_newroute: 7646 * - we should be careful when accessing its ire_nce (specifically 7647 * the nce_res_mp) ast it might change underneath our feet, and, 7648 * - not all legacy code path callers are prepared to handle 7649 * incomplete ire's, so we should not create/add incomplete 7650 * ire_cache entries here. (See discussion about temporary solution 7651 * further below). 7652 * 7653 * In order to minimize packet dropping, and to preserve existing 7654 * behavior, we treat this case as if there were no IRE_CACHE for the 7655 * gateway, and instead use the IF_RESOLVER ire to send out 7656 * another request to ARP (this is achieved by passing the 7657 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the 7658 * arp response comes back in ip_wput_nondata, we will create 7659 * a per-dst ire_cache that has an ND_COMPLETE ire. 7660 * 7661 * Note that this is a temporary solution; the correct solution is 7662 * to create an incomplete per-dst ire_cache entry, and send the 7663 * packet out when the gw's nce is resolved. In order to achieve this, 7664 * all packet processing must have been completed prior to calling 7665 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need 7666 * to be modified to accomodate this solution. 7667 */ 7668 if (in_ill != NULL) { 7669 ire = ire_srcif_table_lookup(dst, IRE_IF_RESOLVER, NULL, 7670 in_ill, MATCH_IRE_TYPE); 7671 } else if (ip_nexthop) { 7672 /* 7673 * The first time we come here, we look for an IRE_INTERFACE 7674 * entry for the specified nexthop, set the dst to be the 7675 * nexthop address and create an IRE_CACHE entry for the 7676 * nexthop. The next time around, we are able to find an 7677 * IRE_CACHE entry for the nexthop, set the gateway to be the 7678 * nexthop address and create an IRE_CACHE entry for the 7679 * destination address via the specified nexthop. 7680 */ 7681 ire = ire_cache_lookup(nexthop_addr, zoneid, 7682 MBLK_GETLABEL(mp)); 7683 if (ire != NULL) { 7684 gw = nexthop_addr; 7685 ire_marks |= IRE_MARK_PRIVATE_ADDR; 7686 } else { 7687 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 7688 IRE_INTERFACE, NULL, NULL, zoneid, 0, 7689 MBLK_GETLABEL(mp), 7690 MATCH_IRE_TYPE | MATCH_IRE_SECATTR); 7691 if (ire != NULL) { 7692 dst = nexthop_addr; 7693 } 7694 } 7695 } else if (attach_ill == NULL) { 7696 ire = ire_ftable_lookup(dst, 0, 0, 0, 7697 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), 7698 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 7699 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | 7700 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE); 7701 } else { 7702 /* 7703 * attach_ill is set only for communicating with 7704 * on-link hosts. So, don't look for DEFAULT. 7705 */ 7706 ipif_t *attach_ipif; 7707 7708 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 7709 if (attach_ipif == NULL) { 7710 ill_refrele(attach_ill); 7711 goto icmp_err_ret; 7712 } 7713 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, 7714 &sire, zoneid, 0, MBLK_GETLABEL(mp), 7715 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL | 7716 MATCH_IRE_SECATTR); 7717 ipif_refrele(attach_ipif); 7718 } 7719 ip3dbg(("ip_newroute: ire_ftable_lookup() " 7720 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 7721 7722 /* 7723 * This loop is run only once in most cases. 7724 * We loop to resolve further routes only when the destination 7725 * can be reached through multiple RTF_MULTIRT-flagged ires. 7726 */ 7727 do { 7728 /* Clear the previous iteration's values */ 7729 if (src_ipif != NULL) { 7730 ipif_refrele(src_ipif); 7731 src_ipif = NULL; 7732 } 7733 if (dst_ill != NULL) { 7734 ill_refrele(dst_ill); 7735 dst_ill = NULL; 7736 } 7737 7738 multirt_resolve_next = B_FALSE; 7739 /* 7740 * We check if packets have to be multirouted. 7741 * In this case, given the current <ire, sire> couple, 7742 * we look for the next suitable <ire, sire>. 7743 * This check is done in ire_multirt_lookup(), 7744 * which applies various criteria to find the next route 7745 * to resolve. ire_multirt_lookup() leaves <ire, sire> 7746 * unchanged if it detects it has not been tried yet. 7747 */ 7748 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 7749 ip3dbg(("ip_newroute: starting next_resolution " 7750 "with first_mp %p, tag %d\n", 7751 (void *)first_mp, 7752 MULTIRT_DEBUG_TAGGED(first_mp))); 7753 7754 ASSERT(sire != NULL); 7755 multirt_is_resolvable = 7756 ire_multirt_lookup(&ire, &sire, multirt_flags, 7757 MBLK_GETLABEL(mp)); 7758 7759 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 7760 "ire %p, sire %p\n", 7761 multirt_is_resolvable, 7762 (void *)ire, (void *)sire)); 7763 7764 if (!multirt_is_resolvable) { 7765 /* 7766 * No more multirt route to resolve; give up 7767 * (all routes resolved or no more 7768 * resolvable routes). 7769 */ 7770 if (ire != NULL) { 7771 ire_refrele(ire); 7772 ire = NULL; 7773 } 7774 } else { 7775 ASSERT(sire != NULL); 7776 ASSERT(ire != NULL); 7777 /* 7778 * We simply use first_sire as a flag that 7779 * indicates if a resolvable multirt route 7780 * has already been found. 7781 * If it is not the case, we may have to send 7782 * an ICMP error to report that the 7783 * destination is unreachable. 7784 * We do not IRE_REFHOLD first_sire. 7785 */ 7786 if (first_sire == NULL) { 7787 first_sire = sire; 7788 } 7789 } 7790 } 7791 if (ire == NULL) { 7792 if (ip_debug > 3) { 7793 /* ip2dbg */ 7794 pr_addr_dbg("ip_newroute: " 7795 "can't resolve %s\n", AF_INET, &dst); 7796 } 7797 ip3dbg(("ip_newroute: " 7798 "ire %p, sire %p, first_sire %p\n", 7799 (void *)ire, (void *)sire, (void *)first_sire)); 7800 7801 if (sire != NULL) { 7802 ire_refrele(sire); 7803 sire = NULL; 7804 } 7805 7806 if (first_sire != NULL) { 7807 /* 7808 * At least one multirt route has been found 7809 * in the same call to ip_newroute(); 7810 * there is no need to report an ICMP error. 7811 * first_sire was not IRE_REFHOLDed. 7812 */ 7813 MULTIRT_DEBUG_UNTAG(first_mp); 7814 freemsg(first_mp); 7815 return; 7816 } 7817 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 7818 RTA_DST); 7819 if (attach_ill != NULL) 7820 ill_refrele(attach_ill); 7821 goto icmp_err_ret; 7822 } 7823 7824 /* 7825 * When RTA_SRCIFP is used to add a route, then an interface 7826 * route is added in the source interface's routing table. 7827 * If the outgoing interface of this route is of type 7828 * IRE_IF_RESOLVER, then upon creation of the ire, 7829 * ire_nce->nce_res_mp is set to NULL. 7830 * Later, when this route is first used for forwarding 7831 * a packet, ip_newroute() is called 7832 * to resolve the hardware address of the outgoing ipif. 7833 * We do not come here for IRE_IF_NORESOLVER entries in the 7834 * source interface based table. We only come here if the 7835 * outgoing interface is a resolver interface and we don't 7836 * have the ire_nce->nce_res_mp information yet. 7837 * If in_ill is not null that means it is called from 7838 * ip_rput. 7839 */ 7840 7841 ASSERT(ire->ire_in_ill == NULL || 7842 (ire->ire_type == IRE_IF_RESOLVER && 7843 ire->ire_nce != NULL && ire->ire_nce->nce_res_mp == NULL)); 7844 7845 /* 7846 * Verify that the returned IRE does not have either 7847 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 7848 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 7849 */ 7850 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 7851 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 7852 if (attach_ill != NULL) 7853 ill_refrele(attach_ill); 7854 goto icmp_err_ret; 7855 } 7856 /* 7857 * Increment the ire_ob_pkt_count field for ire if it is an 7858 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 7859 * increment the same for the parent IRE, sire, if it is some 7860 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST 7861 * and HOST_REDIRECT). 7862 */ 7863 if ((ire->ire_type & IRE_INTERFACE) != 0) { 7864 UPDATE_OB_PKT_COUNT(ire); 7865 ire->ire_last_used_time = lbolt; 7866 } 7867 7868 if (sire != NULL) { 7869 gw = sire->ire_gateway_addr; 7870 ASSERT((sire->ire_type & (IRE_CACHETABLE | 7871 IRE_INTERFACE)) == 0); 7872 UPDATE_OB_PKT_COUNT(sire); 7873 sire->ire_last_used_time = lbolt; 7874 } 7875 /* 7876 * We have a route to reach the destination. 7877 * 7878 * 1) If the interface is part of ill group, try to get a new 7879 * ill taking load spreading into account. 7880 * 7881 * 2) After selecting the ill, get a source address that 7882 * might create good inbound load spreading. 7883 * ipif_select_source does this for us. 7884 * 7885 * If the application specified the ill (ifindex), we still 7886 * load spread. Only if the packets needs to go out 7887 * specifically on a given ill e.g. binding to 7888 * IPIF_NOFAILOVER address, then we don't try to use a 7889 * different ill for load spreading. 7890 */ 7891 if (attach_ill == NULL) { 7892 /* 7893 * Don't perform outbound load spreading in the 7894 * case of an RTF_MULTIRT route, as we actually 7895 * typically want to replicate outgoing packets 7896 * through particular interfaces. 7897 */ 7898 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 7899 dst_ill = ire->ire_ipif->ipif_ill; 7900 /* for uniformity */ 7901 ill_refhold(dst_ill); 7902 } else { 7903 /* 7904 * If we are here trying to create an IRE_CACHE 7905 * for an offlink destination and have the 7906 * IRE_CACHE for the next hop and the latter is 7907 * using virtual IP source address selection i.e 7908 * it's ire->ire_ipif is pointing to a virtual 7909 * network interface (vni) then 7910 * ip_newroute_get_dst_ll() will return the vni 7911 * interface as the dst_ill. Since the vni is 7912 * virtual i.e not associated with any physical 7913 * interface, it cannot be the dst_ill, hence 7914 * in such a case call ip_newroute_get_dst_ll() 7915 * with the stq_ill instead of the ire_ipif ILL. 7916 * The function returns a refheld ill. 7917 */ 7918 if ((ire->ire_type == IRE_CACHE) && 7919 IS_VNI(ire->ire_ipif->ipif_ill)) 7920 dst_ill = ip_newroute_get_dst_ill( 7921 ire->ire_stq->q_ptr); 7922 else 7923 dst_ill = ip_newroute_get_dst_ill( 7924 ire->ire_ipif->ipif_ill); 7925 } 7926 if (dst_ill == NULL) { 7927 if (ip_debug > 2) { 7928 pr_addr_dbg("ip_newroute: " 7929 "no dst ill for dst" 7930 " %s\n", AF_INET, &dst); 7931 } 7932 goto icmp_err_ret; 7933 } 7934 } else { 7935 dst_ill = ire->ire_ipif->ipif_ill; 7936 /* for uniformity */ 7937 ill_refhold(dst_ill); 7938 /* 7939 * We should have found a route matching ill as we 7940 * called ire_ftable_lookup with MATCH_IRE_ILL. 7941 * Rather than asserting, when there is a mismatch, 7942 * we just drop the packet. 7943 */ 7944 if (dst_ill != attach_ill) { 7945 ip0dbg(("ip_newroute: Packet dropped as " 7946 "IPIF_NOFAILOVER ill is %s, " 7947 "ire->ire_ipif->ipif_ill is %s\n", 7948 attach_ill->ill_name, 7949 dst_ill->ill_name)); 7950 ill_refrele(attach_ill); 7951 goto icmp_err_ret; 7952 } 7953 } 7954 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ 7955 if (attach_ill != NULL) { 7956 ill_refrele(attach_ill); 7957 attach_ill = NULL; 7958 do_attach_ill = B_TRUE; 7959 } 7960 ASSERT(dst_ill != NULL); 7961 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 7962 7963 /* 7964 * Pick the best source address from dst_ill. 7965 * 7966 * 1) If it is part of a multipathing group, we would 7967 * like to spread the inbound packets across different 7968 * interfaces. ipif_select_source picks a random source 7969 * across the different ills in the group. 7970 * 7971 * 2) If it is not part of a multipathing group, we try 7972 * to pick the source address from the destination 7973 * route. Clustering assumes that when we have multiple 7974 * prefixes hosted on an interface, the prefix of the 7975 * source address matches the prefix of the destination 7976 * route. We do this only if the address is not 7977 * DEPRECATED. 7978 * 7979 * 3) If the conn is in a different zone than the ire, we 7980 * need to pick a source address from the right zone. 7981 * 7982 * NOTE : If we hit case (1) above, the prefix of the source 7983 * address picked may not match the prefix of the 7984 * destination routes prefix as ipif_select_source 7985 * does not look at "dst" while picking a source 7986 * address. 7987 * If we want the same behavior as (2), we will need 7988 * to change the behavior of ipif_select_source. 7989 */ 7990 ASSERT(src_ipif == NULL); 7991 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 7992 /* 7993 * The RTF_SETSRC flag is set in the parent ire (sire). 7994 * Check that the ipif matching the requested source 7995 * address still exists. 7996 */ 7997 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 7998 zoneid, NULL, NULL, NULL, NULL); 7999 } 8000 if (src_ipif == NULL) { 8001 ire_marks |= IRE_MARK_USESRC_CHECK; 8002 if ((dst_ill->ill_group != NULL) || 8003 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 8004 (connp != NULL && ire->ire_zoneid != zoneid && 8005 ire->ire_zoneid != ALL_ZONES) || 8006 (dst_ill->ill_usesrc_ifindex != 0)) { 8007 /* 8008 * If the destination is reachable via a 8009 * given gateway, the selected source address 8010 * should be in the same subnet as the gateway. 8011 * Otherwise, the destination is not reachable. 8012 * 8013 * If there are no interfaces on the same subnet 8014 * as the destination, ipif_select_source gives 8015 * first non-deprecated interface which might be 8016 * on a different subnet than the gateway. 8017 * This is not desirable. Hence pass the dst_ire 8018 * source address to ipif_select_source. 8019 * It is sure that the destination is reachable 8020 * with the dst_ire source address subnet. 8021 * So passing dst_ire source address to 8022 * ipif_select_source will make sure that the 8023 * selected source will be on the same subnet 8024 * as dst_ire source address. 8025 */ 8026 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 8027 src_ipif = ipif_select_source(dst_ill, saddr, 8028 zoneid); 8029 if (src_ipif == NULL) { 8030 if (ip_debug > 2) { 8031 pr_addr_dbg("ip_newroute: " 8032 "no src for dst %s ", 8033 AF_INET, &dst); 8034 printf("through interface %s\n", 8035 dst_ill->ill_name); 8036 } 8037 goto icmp_err_ret; 8038 } 8039 } else { 8040 src_ipif = ire->ire_ipif; 8041 ASSERT(src_ipif != NULL); 8042 /* hold src_ipif for uniformity */ 8043 ipif_refhold(src_ipif); 8044 } 8045 } 8046 8047 /* 8048 * Assign a source address while we have the conn. 8049 * We can't have ip_wput_ire pick a source address when the 8050 * packet returns from arp since we need to look at 8051 * conn_unspec_src and conn_zoneid, and we lose the conn when 8052 * going through arp. 8053 * 8054 * NOTE : ip_newroute_v6 does not have this piece of code as 8055 * it uses ip6i to store this information. 8056 */ 8057 if (ipha->ipha_src == INADDR_ANY && 8058 (connp == NULL || !connp->conn_unspec_src)) { 8059 ipha->ipha_src = src_ipif->ipif_src_addr; 8060 } 8061 if (ip_debug > 3) { 8062 /* ip2dbg */ 8063 pr_addr_dbg("ip_newroute: first hop %s\n", 8064 AF_INET, &gw); 8065 } 8066 ip2dbg(("\tire type %s (%d)\n", 8067 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 8068 8069 /* 8070 * The TTL of multirouted packets is bounded by the 8071 * ip_multirt_ttl ndd variable. 8072 */ 8073 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8074 /* Force TTL of multirouted packets */ 8075 if ((ip_multirt_ttl > 0) && 8076 (ipha->ipha_ttl > ip_multirt_ttl)) { 8077 ip2dbg(("ip_newroute: forcing multirt TTL " 8078 "to %d (was %d), dst 0x%08x\n", 8079 ip_multirt_ttl, ipha->ipha_ttl, 8080 ntohl(sire->ire_addr))); 8081 ipha->ipha_ttl = ip_multirt_ttl; 8082 } 8083 } 8084 /* 8085 * At this point in ip_newroute(), ire is either the 8086 * IRE_CACHE of the next-hop gateway for an off-subnet 8087 * destination or an IRE_INTERFACE type that should be used 8088 * to resolve an on-subnet destination or an on-subnet 8089 * next-hop gateway. 8090 * 8091 * In the IRE_CACHE case, we have the following : 8092 * 8093 * 1) src_ipif - used for getting a source address. 8094 * 8095 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8096 * means packets using this IRE_CACHE will go out on 8097 * dst_ill. 8098 * 8099 * 3) The IRE sire will point to the prefix that is the 8100 * longest matching route for the destination. These 8101 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST, 8102 * and IRE_HOST_REDIRECT. 8103 * 8104 * The newly created IRE_CACHE entry for the off-subnet 8105 * destination is tied to both the prefix route and the 8106 * interface route used to resolve the next-hop gateway 8107 * via the ire_phandle and ire_ihandle fields, 8108 * respectively. 8109 * 8110 * In the IRE_INTERFACE case, we have the following : 8111 * 8112 * 1) src_ipif - used for getting a source address. 8113 * 8114 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8115 * means packets using the IRE_CACHE that we will build 8116 * here will go out on dst_ill. 8117 * 8118 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 8119 * to be created will only be tied to the IRE_INTERFACE 8120 * that was derived from the ire_ihandle field. 8121 * 8122 * If sire is non-NULL, it means the destination is 8123 * off-link and we will first create the IRE_CACHE for the 8124 * gateway. Next time through ip_newroute, we will create 8125 * the IRE_CACHE for the final destination as described 8126 * above. 8127 * 8128 * In both cases, after the current resolution has been 8129 * completed (or possibly initialised, in the IRE_INTERFACE 8130 * case), the loop may be re-entered to attempt the resolution 8131 * of another RTF_MULTIRT route. 8132 * 8133 * When an IRE_CACHE entry for the off-subnet destination is 8134 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 8135 * for further processing in emission loops. 8136 */ 8137 save_ire = ire; 8138 switch (ire->ire_type) { 8139 case IRE_CACHE: { 8140 ire_t *ipif_ire; 8141 mblk_t *ire_fp_mp; 8142 8143 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); 8144 if (gw == 0) 8145 gw = ire->ire_gateway_addr; 8146 /* 8147 * We need 3 ire's to create a new cache ire for an 8148 * off-link destination from the cache ire of the 8149 * gateway. 8150 * 8151 * 1. The prefix ire 'sire' (Note that this does 8152 * not apply to the conn_nexthop_set case) 8153 * 2. The cache ire of the gateway 'ire' 8154 * 3. The interface ire 'ipif_ire' 8155 * 8156 * We have (1) and (2). We lookup (3) below. 8157 * 8158 * If there is no interface route to the gateway, 8159 * it is a race condition, where we found the cache 8160 * but the interface route has been deleted. 8161 */ 8162 if (ip_nexthop) { 8163 ipif_ire = ire_ihandle_lookup_onlink(ire); 8164 } else { 8165 ipif_ire = 8166 ire_ihandle_lookup_offlink(ire, sire); 8167 } 8168 if (ipif_ire == NULL) { 8169 ip1dbg(("ip_newroute: " 8170 "ire_ihandle_lookup_offlink failed\n")); 8171 goto icmp_err_ret; 8172 } 8173 /* 8174 * XXX We are using the same res_mp 8175 * (DL_UNITDATA_REQ) though the save_ire is not 8176 * pointing at the same ill. 8177 * This is incorrect. We need to send it up to the 8178 * resolver to get the right res_mp. For ethernets 8179 * this may be okay (ill_type == DL_ETHER). 8180 */ 8181 res_mp = save_ire->ire_nce->nce_res_mp; 8182 ire_fp_mp = NULL; 8183 /* 8184 * save_ire's nce_fp_mp can't change since it is 8185 * not an IRE_MIPRTUN or IRE_BROADCAST 8186 * LOCK_IRE_FP_MP does not do any useful work in 8187 * the case of IRE_CACHE. So we don't use it below. 8188 */ 8189 if (save_ire->ire_stq == dst_ill->ill_wq) 8190 ire_fp_mp = save_ire->ire_nce->nce_fp_mp; 8191 8192 /* 8193 * Check cached gateway IRE for any security 8194 * attributes; if found, associate the gateway 8195 * credentials group to the destination IRE. 8196 */ 8197 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 8198 mutex_enter(&attrp->igsa_lock); 8199 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 8200 GCGRP_REFHOLD(gcgrp); 8201 mutex_exit(&attrp->igsa_lock); 8202 } 8203 8204 ire = ire_create( 8205 (uchar_t *)&dst, /* dest address */ 8206 (uchar_t *)&ip_g_all_ones, /* mask */ 8207 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8208 (uchar_t *)&gw, /* gateway address */ 8209 NULL, 8210 &save_ire->ire_max_frag, 8211 ire_fp_mp, /* Fast Path header */ 8212 dst_ill->ill_rq, /* recv-from queue */ 8213 dst_ill->ill_wq, /* send-to queue */ 8214 IRE_CACHE, /* IRE type */ 8215 res_mp, 8216 src_ipif, 8217 in_ill, /* incoming ill */ 8218 (sire != NULL) ? 8219 sire->ire_mask : 0, /* Parent mask */ 8220 (sire != NULL) ? 8221 sire->ire_phandle : 0, /* Parent handle */ 8222 ipif_ire->ire_ihandle, /* Interface handle */ 8223 (sire != NULL) ? (sire->ire_flags & 8224 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 8225 (sire != NULL) ? 8226 &(sire->ire_uinfo) : &(save_ire->ire_uinfo), 8227 NULL, 8228 gcgrp); 8229 8230 if (ire == NULL) { 8231 if (gcgrp != NULL) { 8232 GCGRP_REFRELE(gcgrp); 8233 gcgrp = NULL; 8234 } 8235 ire_refrele(ipif_ire); 8236 ire_refrele(save_ire); 8237 break; 8238 } 8239 8240 /* reference now held by IRE */ 8241 gcgrp = NULL; 8242 8243 ire->ire_marks |= ire_marks; 8244 8245 /* 8246 * Prevent sire and ipif_ire from getting deleted. 8247 * The newly created ire is tied to both of them via 8248 * the phandle and ihandle respectively. 8249 */ 8250 if (sire != NULL) { 8251 IRB_REFHOLD(sire->ire_bucket); 8252 /* Has it been removed already ? */ 8253 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 8254 IRB_REFRELE(sire->ire_bucket); 8255 ire_refrele(ipif_ire); 8256 ire_refrele(save_ire); 8257 break; 8258 } 8259 } 8260 8261 IRB_REFHOLD(ipif_ire->ire_bucket); 8262 /* Has it been removed already ? */ 8263 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 8264 IRB_REFRELE(ipif_ire->ire_bucket); 8265 if (sire != NULL) 8266 IRB_REFRELE(sire->ire_bucket); 8267 ire_refrele(ipif_ire); 8268 ire_refrele(save_ire); 8269 break; 8270 } 8271 8272 xmit_mp = first_mp; 8273 /* 8274 * In the case of multirouting, a copy 8275 * of the packet is done before its sending. 8276 * The copy is used to attempt another 8277 * route resolution, in a next loop. 8278 */ 8279 if (ire->ire_flags & RTF_MULTIRT) { 8280 copy_mp = copymsg(first_mp); 8281 if (copy_mp != NULL) { 8282 xmit_mp = copy_mp; 8283 MULTIRT_DEBUG_TAG(first_mp); 8284 } 8285 } 8286 ire_add_then_send(q, ire, xmit_mp); 8287 ire_refrele(save_ire); 8288 8289 /* Assert that sire is not deleted yet. */ 8290 if (sire != NULL) { 8291 ASSERT(sire->ire_ptpn != NULL); 8292 IRB_REFRELE(sire->ire_bucket); 8293 } 8294 8295 /* Assert that ipif_ire is not deleted yet. */ 8296 ASSERT(ipif_ire->ire_ptpn != NULL); 8297 IRB_REFRELE(ipif_ire->ire_bucket); 8298 ire_refrele(ipif_ire); 8299 8300 /* 8301 * If copy_mp is not NULL, multirouting was 8302 * requested. We loop to initiate a next 8303 * route resolution attempt, starting from sire. 8304 */ 8305 if (copy_mp != NULL) { 8306 /* 8307 * Search for the next unresolved 8308 * multirt route. 8309 */ 8310 copy_mp = NULL; 8311 ipif_ire = NULL; 8312 ire = NULL; 8313 multirt_resolve_next = B_TRUE; 8314 continue; 8315 } 8316 if (sire != NULL) 8317 ire_refrele(sire); 8318 ipif_refrele(src_ipif); 8319 ill_refrele(dst_ill); 8320 return; 8321 } 8322 case IRE_IF_NORESOLVER: { 8323 /* 8324 * We have what we need to build an IRE_CACHE. 8325 * 8326 * Create a new res_mp with the IP gateway address 8327 * in destination address in the DLPI hdr if the 8328 * physical length is exactly 4 bytes. 8329 */ 8330 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 8331 uchar_t *addr; 8332 8333 if (gw) 8334 addr = (uchar_t *)&gw; 8335 else 8336 addr = (uchar_t *)&dst; 8337 8338 res_mp = ill_dlur_gen(addr, 8339 dst_ill->ill_phys_addr_length, 8340 dst_ill->ill_sap, 8341 dst_ill->ill_sap_length); 8342 8343 if (res_mp == NULL) { 8344 ip1dbg(("ip_newroute: res_mp NULL\n")); 8345 break; 8346 } 8347 } else { 8348 res_mp = NULL; 8349 } 8350 8351 /* 8352 * TSol note: We are creating the ire cache for the 8353 * destination 'dst'. If 'dst' is offlink, going 8354 * through the first hop 'gw', the security attributes 8355 * of 'dst' must be set to point to the gateway 8356 * credentials of gateway 'gw'. If 'dst' is onlink, it 8357 * is possible that 'dst' is a potential gateway that is 8358 * referenced by some route that has some security 8359 * attributes. Thus in the former case, we need to do a 8360 * gcgrp_lookup of 'gw' while in the latter case we 8361 * need to do gcgrp_lookup of 'dst' itself. 8362 */ 8363 ga.ga_af = AF_INET; 8364 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, 8365 &ga.ga_addr); 8366 gcgrp = gcgrp_lookup(&ga, B_FALSE); 8367 8368 ire = ire_create( 8369 (uchar_t *)&dst, /* dest address */ 8370 (uchar_t *)&ip_g_all_ones, /* mask */ 8371 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8372 (uchar_t *)&gw, /* gateway address */ 8373 NULL, 8374 &save_ire->ire_max_frag, 8375 NULL, /* Fast Path header */ 8376 dst_ill->ill_rq, /* recv-from queue */ 8377 dst_ill->ill_wq, /* send-to queue */ 8378 IRE_CACHE, 8379 res_mp, 8380 src_ipif, 8381 in_ill, /* Incoming ill */ 8382 save_ire->ire_mask, /* Parent mask */ 8383 (sire != NULL) ? /* Parent handle */ 8384 sire->ire_phandle : 0, 8385 save_ire->ire_ihandle, /* Interface handle */ 8386 (sire != NULL) ? sire->ire_flags & 8387 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 8388 &(save_ire->ire_uinfo), 8389 NULL, 8390 gcgrp); 8391 8392 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) 8393 freeb(res_mp); 8394 8395 if (ire == NULL) { 8396 if (gcgrp != NULL) { 8397 GCGRP_REFRELE(gcgrp); 8398 gcgrp = NULL; 8399 } 8400 ire_refrele(save_ire); 8401 break; 8402 } 8403 8404 /* reference now held by IRE */ 8405 gcgrp = NULL; 8406 8407 ire->ire_marks |= ire_marks; 8408 8409 /* Prevent save_ire from getting deleted */ 8410 IRB_REFHOLD(save_ire->ire_bucket); 8411 /* Has it been removed already ? */ 8412 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8413 IRB_REFRELE(save_ire->ire_bucket); 8414 ire_refrele(save_ire); 8415 break; 8416 } 8417 8418 /* 8419 * In the case of multirouting, a copy 8420 * of the packet is made before it is sent. 8421 * The copy is used in the next 8422 * loop to attempt another resolution. 8423 */ 8424 xmit_mp = first_mp; 8425 if ((sire != NULL) && 8426 (sire->ire_flags & RTF_MULTIRT)) { 8427 copy_mp = copymsg(first_mp); 8428 if (copy_mp != NULL) { 8429 xmit_mp = copy_mp; 8430 MULTIRT_DEBUG_TAG(first_mp); 8431 } 8432 } 8433 ire_add_then_send(q, ire, xmit_mp); 8434 8435 /* Assert that it is not deleted yet. */ 8436 ASSERT(save_ire->ire_ptpn != NULL); 8437 IRB_REFRELE(save_ire->ire_bucket); 8438 ire_refrele(save_ire); 8439 8440 if (copy_mp != NULL) { 8441 /* 8442 * If we found a (no)resolver, we ignore any 8443 * trailing top priority IRE_CACHE in further 8444 * loops. This ensures that we do not omit any 8445 * (no)resolver. 8446 * This IRE_CACHE, if any, will be processed 8447 * by another thread entering ip_newroute(). 8448 * IRE_CACHE entries, if any, will be processed 8449 * by another thread entering ip_newroute(), 8450 * (upon resolver response, for instance). 8451 * This aims to force parallel multirt 8452 * resolutions as soon as a packet must be sent. 8453 * In the best case, after the tx of only one 8454 * packet, all reachable routes are resolved. 8455 * Otherwise, the resolution of all RTF_MULTIRT 8456 * routes would require several emissions. 8457 */ 8458 multirt_flags &= ~MULTIRT_CACHEGW; 8459 8460 /* 8461 * Search for the next unresolved multirt 8462 * route. 8463 */ 8464 copy_mp = NULL; 8465 save_ire = NULL; 8466 ire = NULL; 8467 multirt_resolve_next = B_TRUE; 8468 continue; 8469 } 8470 8471 /* 8472 * Don't need sire anymore 8473 */ 8474 if (sire != NULL) 8475 ire_refrele(sire); 8476 8477 ipif_refrele(src_ipif); 8478 ill_refrele(dst_ill); 8479 return; 8480 } 8481 case IRE_IF_RESOLVER: 8482 /* 8483 * We can't build an IRE_CACHE yet, but at least we 8484 * found a resolver that can help. 8485 */ 8486 res_mp = dst_ill->ill_resolver_mp; 8487 if (!OK_RESOLVER_MP(res_mp)) 8488 break; 8489 8490 /* 8491 * To be at this point in the code with a non-zero gw 8492 * means that dst is reachable through a gateway that 8493 * we have never resolved. By changing dst to the gw 8494 * addr we resolve the gateway first. 8495 * When ire_add_then_send() tries to put the IP dg 8496 * to dst, it will reenter ip_newroute() at which 8497 * time we will find the IRE_CACHE for the gw and 8498 * create another IRE_CACHE in case IRE_CACHE above. 8499 */ 8500 if (gw != INADDR_ANY) { 8501 /* 8502 * The source ipif that was determined above was 8503 * relative to the destination address, not the 8504 * gateway's. If src_ipif was not taken out of 8505 * the IRE_IF_RESOLVER entry, we'll need to call 8506 * ipif_select_source() again. 8507 */ 8508 if (src_ipif != ire->ire_ipif) { 8509 ipif_refrele(src_ipif); 8510 src_ipif = ipif_select_source(dst_ill, 8511 gw, zoneid); 8512 if (src_ipif == NULL) { 8513 if (ip_debug > 2) { 8514 pr_addr_dbg( 8515 "ip_newroute: no " 8516 "src for gw %s ", 8517 AF_INET, &gw); 8518 printf("through " 8519 "interface %s\n", 8520 dst_ill->ill_name); 8521 } 8522 goto icmp_err_ret; 8523 } 8524 } 8525 save_dst = dst; 8526 dst = gw; 8527 gw = INADDR_ANY; 8528 } 8529 8530 /* 8531 * We obtain a partial IRE_CACHE which we will pass 8532 * along with the resolver query. When the response 8533 * comes back it will be there ready for us to add. 8534 * The ire_max_frag is atomically set under the 8535 * irebucket lock in ire_add_v[46]. 8536 */ 8537 8538 ire = ire_create_mp( 8539 (uchar_t *)&dst, /* dest address */ 8540 (uchar_t *)&ip_g_all_ones, /* mask */ 8541 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8542 (uchar_t *)&gw, /* gateway address */ 8543 NULL, /* no in_src_addr */ 8544 NULL, /* ire_max_frag */ 8545 NULL, /* Fast Path header */ 8546 dst_ill->ill_rq, /* recv-from queue */ 8547 dst_ill->ill_wq, /* send-to queue */ 8548 IRE_CACHE, 8549 NULL, 8550 src_ipif, /* Interface ipif */ 8551 in_ill, /* Incoming ILL */ 8552 save_ire->ire_mask, /* Parent mask */ 8553 0, 8554 save_ire->ire_ihandle, /* Interface handle */ 8555 0, /* flags if any */ 8556 &(save_ire->ire_uinfo), 8557 NULL, 8558 NULL); 8559 8560 if (ire == NULL) { 8561 ire_refrele(save_ire); 8562 break; 8563 } 8564 8565 if ((sire != NULL) && 8566 (sire->ire_flags & RTF_MULTIRT)) { 8567 copy_mp = copymsg(first_mp); 8568 if (copy_mp != NULL) 8569 MULTIRT_DEBUG_TAG(copy_mp); 8570 } 8571 8572 ire->ire_marks |= ire_marks; 8573 8574 /* 8575 * Construct message chain for the resolver 8576 * of the form: 8577 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8578 * Packet could contain a IPSEC_OUT mp. 8579 * 8580 * NOTE : ire will be added later when the response 8581 * comes back from ARP. If the response does not 8582 * come back, ARP frees the packet. For this reason, 8583 * we can't REFHOLD the bucket of save_ire to prevent 8584 * deletions. We may not be able to REFRELE the bucket 8585 * if the response never comes back. Thus, before 8586 * adding the ire, ire_add_v4 will make sure that the 8587 * interface route does not get deleted. This is the 8588 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 8589 * where we can always prevent deletions because of 8590 * the synchronous nature of adding IRES i.e 8591 * ire_add_then_send is called after creating the IRE. 8592 */ 8593 ASSERT(ire->ire_mp != NULL); 8594 ire->ire_mp->b_cont = first_mp; 8595 /* Have saved_mp handy, for cleanup if canput fails */ 8596 saved_mp = mp; 8597 mp = copyb(res_mp); 8598 if (mp == NULL) { 8599 /* Prepare for cleanup */ 8600 mp = saved_mp; /* pkt */ 8601 ire_delete(ire); /* ire_mp */ 8602 ire = NULL; 8603 ire_refrele(save_ire); 8604 if (copy_mp != NULL) { 8605 MULTIRT_DEBUG_UNTAG(copy_mp); 8606 freemsg(copy_mp); 8607 copy_mp = NULL; 8608 } 8609 break; 8610 } 8611 linkb(mp, ire->ire_mp); 8612 8613 /* 8614 * Fill in the source and dest addrs for the resolver. 8615 * NOTE: this depends on memory layouts imposed by 8616 * ill_init(). 8617 */ 8618 areq = (areq_t *)mp->b_rptr; 8619 addrp = (ipaddr_t *)((char *)areq + 8620 areq->areq_sender_addr_offset); 8621 if (do_attach_ill) { 8622 /* 8623 * This is bind to no failover case. 8624 * arp packet also must go out on attach_ill. 8625 */ 8626 ASSERT(ipha->ipha_src != NULL); 8627 *addrp = ipha->ipha_src; 8628 } else { 8629 *addrp = save_ire->ire_src_addr; 8630 } 8631 8632 ire_refrele(save_ire); 8633 addrp = (ipaddr_t *)((char *)areq + 8634 areq->areq_target_addr_offset); 8635 *addrp = dst; 8636 /* Up to the resolver. */ 8637 if (canputnext(dst_ill->ill_rq) && 8638 !(dst_ill->ill_arp_closing)) { 8639 putnext(dst_ill->ill_rq, mp); 8640 ire = NULL; 8641 if (copy_mp != NULL) { 8642 /* 8643 * If we found a resolver, we ignore 8644 * any trailing top priority IRE_CACHE 8645 * in the further loops. This ensures 8646 * that we do not omit any resolver. 8647 * IRE_CACHE entries, if any, will be 8648 * processed next time we enter 8649 * ip_newroute(). 8650 */ 8651 multirt_flags &= ~MULTIRT_CACHEGW; 8652 /* 8653 * Search for the next unresolved 8654 * multirt route. 8655 */ 8656 first_mp = copy_mp; 8657 copy_mp = NULL; 8658 /* Prepare the next resolution loop. */ 8659 mp = first_mp; 8660 EXTRACT_PKT_MP(mp, first_mp, 8661 mctl_present); 8662 if (mctl_present) 8663 io = (ipsec_out_t *) 8664 first_mp->b_rptr; 8665 ipha = (ipha_t *)mp->b_rptr; 8666 8667 ASSERT(sire != NULL); 8668 8669 dst = save_dst; 8670 multirt_resolve_next = B_TRUE; 8671 continue; 8672 } 8673 8674 if (sire != NULL) 8675 ire_refrele(sire); 8676 8677 /* 8678 * The response will come back in ip_wput 8679 * with db_type IRE_DB_TYPE. 8680 */ 8681 ipif_refrele(src_ipif); 8682 ill_refrele(dst_ill); 8683 return; 8684 } else { 8685 /* Prepare for cleanup */ 8686 DTRACE_PROBE1(ip__newroute__drop, mblk_t *, 8687 mp); 8688 mp->b_cont = NULL; 8689 freeb(mp); /* areq */ 8690 /* 8691 * this is an ire that is not added to the 8692 * cache. ire_freemblk will handle the release 8693 * of any resources associated with the ire. 8694 */ 8695 ire_delete(ire); /* ire_mp */ 8696 mp = saved_mp; /* pkt */ 8697 ire = NULL; 8698 if (copy_mp != NULL) { 8699 MULTIRT_DEBUG_UNTAG(copy_mp); 8700 freemsg(copy_mp); 8701 copy_mp = NULL; 8702 } 8703 break; 8704 } 8705 default: 8706 break; 8707 } 8708 } while (multirt_resolve_next); 8709 8710 ip1dbg(("ip_newroute: dropped\n")); 8711 /* Did this packet originate externally? */ 8712 if (mp->b_prev) { 8713 mp->b_next = NULL; 8714 mp->b_prev = NULL; 8715 BUMP_MIB(&ip_mib, ipInDiscards); 8716 } else { 8717 BUMP_MIB(&ip_mib, ipOutDiscards); 8718 } 8719 ASSERT(copy_mp == NULL); 8720 MULTIRT_DEBUG_UNTAG(first_mp); 8721 freemsg(first_mp); 8722 if (ire != NULL) 8723 ire_refrele(ire); 8724 if (sire != NULL) 8725 ire_refrele(sire); 8726 if (src_ipif != NULL) 8727 ipif_refrele(src_ipif); 8728 if (dst_ill != NULL) 8729 ill_refrele(dst_ill); 8730 return; 8731 8732 icmp_err_ret: 8733 ip1dbg(("ip_newroute: no route\n")); 8734 if (src_ipif != NULL) 8735 ipif_refrele(src_ipif); 8736 if (dst_ill != NULL) 8737 ill_refrele(dst_ill); 8738 if (sire != NULL) 8739 ire_refrele(sire); 8740 /* Did this packet originate externally? */ 8741 if (mp->b_prev) { 8742 mp->b_next = NULL; 8743 mp->b_prev = NULL; 8744 /* XXX ipInNoRoutes */ 8745 q = WR(q); 8746 } else { 8747 /* 8748 * Since ip_wput() isn't close to finished, we fill 8749 * in enough of the header for credible error reporting. 8750 */ 8751 if (ip_hdr_complete(ipha, zoneid)) { 8752 /* Failed */ 8753 MULTIRT_DEBUG_UNTAG(first_mp); 8754 freemsg(first_mp); 8755 if (ire != NULL) 8756 ire_refrele(ire); 8757 return; 8758 } 8759 } 8760 BUMP_MIB(&ip_mib, ipOutNoRoutes); 8761 8762 /* 8763 * At this point we will have ire only if RTF_BLACKHOLE 8764 * or RTF_REJECT flags are set on the IRE. It will not 8765 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 8766 */ 8767 if (ire != NULL) { 8768 if (ire->ire_flags & RTF_BLACKHOLE) { 8769 ire_refrele(ire); 8770 MULTIRT_DEBUG_UNTAG(first_mp); 8771 freemsg(first_mp); 8772 return; 8773 } 8774 ire_refrele(ire); 8775 } 8776 if (ip_source_routed(ipha)) { 8777 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, 8778 zoneid); 8779 return; 8780 } 8781 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid); 8782 } 8783 8784 /* 8785 * IPv4 - 8786 * ip_newroute_ipif is called by ip_wput_multicast and 8787 * ip_rput_forward_multicast whenever we need to send 8788 * out a packet to a destination address for which we do not have specific 8789 * routing information. It is used when the packet will be sent out 8790 * on a specific interface. It is also called by ip_wput() when IP_XMIT_IF 8791 * socket option is set or icmp error message wants to go out on a particular 8792 * interface for a unicast packet. 8793 * 8794 * In most cases, the destination address is resolved thanks to the ipif 8795 * intrinsic resolver. However, there are some cases where the call to 8796 * ip_newroute_ipif must take into account the potential presence of 8797 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 8798 * that uses the interface. This is specified through flags, 8799 * which can be a combination of: 8800 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 8801 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 8802 * and flags. Additionally, the packet source address has to be set to 8803 * the specified address. The caller is thus expected to set this flag 8804 * if the packet has no specific source address yet. 8805 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 8806 * flag, the resulting ire will inherit the flag. All unresolved routes 8807 * to the destination must be explored in the same call to 8808 * ip_newroute_ipif(). 8809 */ 8810 static void 8811 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 8812 conn_t *connp, uint32_t flags, zoneid_t zoneid) 8813 { 8814 areq_t *areq; 8815 ire_t *ire = NULL; 8816 mblk_t *res_mp; 8817 ipaddr_t *addrp; 8818 mblk_t *first_mp; 8819 ire_t *save_ire = NULL; 8820 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ 8821 ipif_t *src_ipif = NULL; 8822 ushort_t ire_marks = 0; 8823 ill_t *dst_ill = NULL; 8824 boolean_t mctl_present; 8825 ipsec_out_t *io; 8826 ipha_t *ipha; 8827 int ihandle = 0; 8828 mblk_t *saved_mp; 8829 ire_t *fire = NULL; 8830 mblk_t *copy_mp = NULL; 8831 boolean_t multirt_resolve_next; 8832 ipaddr_t ipha_dst; 8833 8834 /* 8835 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 8836 * here for uniformity 8837 */ 8838 ipif_refhold(ipif); 8839 8840 /* 8841 * This loop is run only once in most cases. 8842 * We loop to resolve further routes only when the destination 8843 * can be reached through multiple RTF_MULTIRT-flagged ires. 8844 */ 8845 do { 8846 if (dst_ill != NULL) { 8847 ill_refrele(dst_ill); 8848 dst_ill = NULL; 8849 } 8850 if (src_ipif != NULL) { 8851 ipif_refrele(src_ipif); 8852 src_ipif = NULL; 8853 } 8854 multirt_resolve_next = B_FALSE; 8855 8856 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 8857 ipif->ipif_ill->ill_name)); 8858 8859 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 8860 if (mctl_present) 8861 io = (ipsec_out_t *)first_mp->b_rptr; 8862 8863 ipha = (ipha_t *)mp->b_rptr; 8864 8865 /* 8866 * Save the packet destination address, we may need it after 8867 * the packet has been consumed. 8868 */ 8869 ipha_dst = ipha->ipha_dst; 8870 8871 /* 8872 * If the interface is a pt-pt interface we look for an 8873 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 8874 * local_address and the pt-pt destination address. Otherwise 8875 * we just match the local address. 8876 * NOTE: dst could be different than ipha->ipha_dst in case 8877 * of sending igmp multicast packets over a point-to-point 8878 * connection. 8879 * Thus we must be careful enough to check ipha_dst to be a 8880 * multicast address, otherwise it will take xmit_if path for 8881 * multicast packets resulting into kernel stack overflow by 8882 * repeated calls to ip_newroute_ipif from ire_send(). 8883 */ 8884 if (CLASSD(ipha_dst) && 8885 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 8886 goto err_ret; 8887 } 8888 8889 /* 8890 * We check if an IRE_OFFSUBNET for the addr that goes through 8891 * ipif exists. We need it to determine if the RTF_SETSRC and/or 8892 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 8893 * propagate its flags to the new ire. 8894 */ 8895 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 8896 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 8897 ip2dbg(("ip_newroute_ipif: " 8898 "ipif_lookup_multi_ire(" 8899 "ipif %p, dst %08x) = fire %p\n", 8900 (void *)ipif, ntohl(dst), (void *)fire)); 8901 } 8902 8903 if (mctl_present && io->ipsec_out_attach_if) { 8904 attach_ill = ip_grab_attach_ill(NULL, first_mp, 8905 io->ipsec_out_ill_index, B_FALSE); 8906 8907 /* Failure case frees things for us. */ 8908 if (attach_ill == NULL) { 8909 ipif_refrele(ipif); 8910 if (fire != NULL) 8911 ire_refrele(fire); 8912 return; 8913 } 8914 8915 /* 8916 * Check if we need an ire that will not be 8917 * looked up by anybody else i.e. HIDDEN. 8918 */ 8919 if (ill_is_probeonly(attach_ill)) { 8920 ire_marks = IRE_MARK_HIDDEN; 8921 } 8922 /* 8923 * ip_wput passes the right ipif for IPIF_NOFAILOVER 8924 * case. 8925 */ 8926 dst_ill = ipif->ipif_ill; 8927 /* attach_ill has been refheld by ip_grab_attach_ill */ 8928 ASSERT(dst_ill == attach_ill); 8929 } else { 8930 /* 8931 * If this is set by IP_XMIT_IF, then make sure that 8932 * ipif is pointing to the same ill as the IP_XMIT_IF 8933 * specified ill. 8934 */ 8935 ASSERT((connp == NULL) || 8936 (connp->conn_xmit_if_ill == NULL) || 8937 (connp->conn_xmit_if_ill == ipif->ipif_ill)); 8938 /* 8939 * If the interface belongs to an interface group, 8940 * make sure the next possible interface in the group 8941 * is used. This encourages load spreading among 8942 * peers in an interface group. 8943 * Note: load spreading is disabled for RTF_MULTIRT 8944 * routes. 8945 */ 8946 if ((flags & RTF_MULTIRT) && (fire != NULL) && 8947 (fire->ire_flags & RTF_MULTIRT)) { 8948 /* 8949 * Don't perform outbound load spreading 8950 * in the case of an RTF_MULTIRT issued route, 8951 * we actually typically want to replicate 8952 * outgoing packets through particular 8953 * interfaces. 8954 */ 8955 dst_ill = ipif->ipif_ill; 8956 ill_refhold(dst_ill); 8957 } else { 8958 dst_ill = ip_newroute_get_dst_ill( 8959 ipif->ipif_ill); 8960 } 8961 if (dst_ill == NULL) { 8962 if (ip_debug > 2) { 8963 pr_addr_dbg("ip_newroute_ipif: " 8964 "no dst ill for dst %s\n", 8965 AF_INET, &dst); 8966 } 8967 goto err_ret; 8968 } 8969 } 8970 8971 /* 8972 * Pick a source address preferring non-deprecated ones. 8973 * Unlike ip_newroute, we don't do any source address 8974 * selection here since for multicast it really does not help 8975 * in inbound load spreading as in the unicast case. 8976 */ 8977 if ((flags & RTF_SETSRC) && (fire != NULL) && 8978 (fire->ire_flags & RTF_SETSRC)) { 8979 /* 8980 * As requested by flags, an IRE_OFFSUBNET was looked up 8981 * on that interface. This ire has RTF_SETSRC flag, so 8982 * the source address of the packet must be changed. 8983 * Check that the ipif matching the requested source 8984 * address still exists. 8985 */ 8986 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 8987 zoneid, NULL, NULL, NULL, NULL); 8988 } 8989 if (((ipif->ipif_flags & IPIF_DEPRECATED) || 8990 (connp != NULL && ipif->ipif_zoneid != zoneid && 8991 ipif->ipif_zoneid != ALL_ZONES)) && 8992 (src_ipif == NULL)) { 8993 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 8994 if (src_ipif == NULL) { 8995 if (ip_debug > 2) { 8996 /* ip1dbg */ 8997 pr_addr_dbg("ip_newroute_ipif: " 8998 "no src for dst %s", 8999 AF_INET, &dst); 9000 } 9001 ip1dbg((" through interface %s\n", 9002 dst_ill->ill_name)); 9003 goto err_ret; 9004 } 9005 ipif_refrele(ipif); 9006 ipif = src_ipif; 9007 ipif_refhold(ipif); 9008 } 9009 if (src_ipif == NULL) { 9010 src_ipif = ipif; 9011 ipif_refhold(src_ipif); 9012 } 9013 9014 /* 9015 * Assign a source address while we have the conn. 9016 * We can't have ip_wput_ire pick a source address when the 9017 * packet returns from arp since conn_unspec_src might be set 9018 * and we loose the conn when going through arp. 9019 */ 9020 if (ipha->ipha_src == INADDR_ANY && 9021 (connp == NULL || !connp->conn_unspec_src)) { 9022 ipha->ipha_src = src_ipif->ipif_src_addr; 9023 } 9024 9025 /* 9026 * In case of IP_XMIT_IF, it is possible that the outgoing 9027 * interface does not have an interface ire. 9028 * Example: Thousands of mobileip PPP interfaces to mobile 9029 * nodes. We don't want to create interface ires because 9030 * packets from other mobile nodes must not take the route 9031 * via interface ires to the visiting mobile node without 9032 * going through the home agent, in absence of mobileip 9033 * route optimization. 9034 */ 9035 if (CLASSD(ipha_dst) && (connp == NULL || 9036 connp->conn_xmit_if_ill == NULL)) { 9037 /* ipif_to_ire returns an held ire */ 9038 ire = ipif_to_ire(ipif); 9039 if (ire == NULL) 9040 goto err_ret; 9041 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 9042 goto err_ret; 9043 /* 9044 * ihandle is needed when the ire is added to 9045 * cache table. 9046 */ 9047 save_ire = ire; 9048 ihandle = save_ire->ire_ihandle; 9049 9050 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 9051 "flags %04x\n", 9052 (void *)ire, (void *)ipif, flags)); 9053 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9054 (fire->ire_flags & RTF_MULTIRT)) { 9055 /* 9056 * As requested by flags, an IRE_OFFSUBNET was 9057 * looked up on that interface. This ire has 9058 * RTF_MULTIRT flag, so the resolution loop will 9059 * be re-entered to resolve additional routes on 9060 * other interfaces. For that purpose, a copy of 9061 * the packet is performed at this point. 9062 */ 9063 fire->ire_last_used_time = lbolt; 9064 copy_mp = copymsg(first_mp); 9065 if (copy_mp) { 9066 MULTIRT_DEBUG_TAG(copy_mp); 9067 } 9068 } 9069 if ((flags & RTF_SETSRC) && (fire != NULL) && 9070 (fire->ire_flags & RTF_SETSRC)) { 9071 /* 9072 * As requested by flags, an IRE_OFFSUBET was 9073 * looked up on that interface. This ire has 9074 * RTF_SETSRC flag, so the source address of the 9075 * packet must be changed. 9076 */ 9077 ipha->ipha_src = fire->ire_src_addr; 9078 } 9079 } else { 9080 ASSERT((connp == NULL) || 9081 (connp->conn_xmit_if_ill != NULL) || 9082 (connp->conn_dontroute)); 9083 /* 9084 * The only ways we can come here are: 9085 * 1) IP_XMIT_IF socket option is set 9086 * 2) ICMP error message generated from 9087 * ip_mrtun_forward() routine and it needs 9088 * to go through the specified ill. 9089 * 3) SO_DONTROUTE socket option is set 9090 * In all cases, the new ire will not be added 9091 * into cache table. 9092 */ 9093 ire_marks |= IRE_MARK_NOADD; 9094 } 9095 9096 switch (ipif->ipif_net_type) { 9097 case IRE_IF_NORESOLVER: { 9098 /* We have what we need to build an IRE_CACHE. */ 9099 mblk_t *res_mp; 9100 9101 /* 9102 * Create a new res_mp with the 9103 * IP gateway address as destination address in the 9104 * DLPI hdr if the physical length is exactly 4 bytes. 9105 */ 9106 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 9107 res_mp = ill_dlur_gen((uchar_t *)&dst, 9108 dst_ill->ill_phys_addr_length, 9109 dst_ill->ill_sap, 9110 dst_ill->ill_sap_length); 9111 } else { 9112 /* use the value set in ip_ll_subnet_defaults */ 9113 res_mp = ill_dlur_gen(NULL, 9114 dst_ill->ill_phys_addr_length, 9115 dst_ill->ill_sap, 9116 dst_ill->ill_sap_length); 9117 } 9118 9119 if (res_mp == NULL) 9120 break; 9121 /* 9122 * The new ire inherits the IRE_OFFSUBNET flags 9123 * and source address, if this was requested. 9124 */ 9125 ire = ire_create( 9126 (uchar_t *)&dst, /* dest address */ 9127 (uchar_t *)&ip_g_all_ones, /* mask */ 9128 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9129 NULL, /* gateway address */ 9130 NULL, 9131 &ipif->ipif_mtu, 9132 NULL, /* Fast Path header */ 9133 dst_ill->ill_rq, /* recv-from queue */ 9134 dst_ill->ill_wq, /* send-to queue */ 9135 IRE_CACHE, 9136 res_mp, 9137 src_ipif, 9138 NULL, 9139 (save_ire != NULL ? save_ire->ire_mask : 0), 9140 (fire != NULL) ? /* Parent handle */ 9141 fire->ire_phandle : 0, 9142 ihandle, /* Interface handle */ 9143 (fire != NULL) ? 9144 (fire->ire_flags & 9145 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9146 (save_ire == NULL ? &ire_uinfo_null : 9147 &save_ire->ire_uinfo), 9148 NULL, 9149 NULL); 9150 9151 freeb(res_mp); 9152 9153 if (ire == NULL) { 9154 if (save_ire != NULL) 9155 ire_refrele(save_ire); 9156 break; 9157 } 9158 9159 ire->ire_marks |= ire_marks; 9160 9161 /* 9162 * If IRE_MARK_NOADD is set then we need to convert 9163 * the max_fragp to a useable value now. This is 9164 * normally done in ire_add_v[46]. We also need to 9165 * associate the ire with an nce (normally would be 9166 * done in ip_wput_nondata()). 9167 * 9168 * Note that IRE_MARK_NOADD packets created here 9169 * do not have a non-null ire_mp pointer. The null 9170 * value of ire_bucket indicates that they were 9171 * never added. 9172 */ 9173 if (ire->ire_marks & IRE_MARK_NOADD) { 9174 uint_t max_frag; 9175 9176 max_frag = *ire->ire_max_fragp; 9177 ire->ire_max_fragp = NULL; 9178 ire->ire_max_frag = max_frag; 9179 9180 if ((ire->ire_nce = ndp_lookup_v4( 9181 ire_to_ill(ire), 9182 (ire->ire_gateway_addr != INADDR_ANY ? 9183 &ire->ire_gateway_addr : &ire->ire_addr), 9184 B_FALSE)) == NULL) { 9185 if (save_ire != NULL) 9186 ire_refrele(save_ire); 9187 break; 9188 } 9189 ASSERT(ire->ire_nce->nce_state == 9190 ND_REACHABLE); 9191 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 9192 } 9193 9194 /* Prevent save_ire from getting deleted */ 9195 if (save_ire != NULL) { 9196 IRB_REFHOLD(save_ire->ire_bucket); 9197 /* Has it been removed already ? */ 9198 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 9199 IRB_REFRELE(save_ire->ire_bucket); 9200 ire_refrele(save_ire); 9201 break; 9202 } 9203 } 9204 9205 ire_add_then_send(q, ire, first_mp); 9206 9207 /* Assert that save_ire is not deleted yet. */ 9208 if (save_ire != NULL) { 9209 ASSERT(save_ire->ire_ptpn != NULL); 9210 IRB_REFRELE(save_ire->ire_bucket); 9211 ire_refrele(save_ire); 9212 save_ire = NULL; 9213 } 9214 if (fire != NULL) { 9215 ire_refrele(fire); 9216 fire = NULL; 9217 } 9218 9219 /* 9220 * the resolution loop is re-entered if this 9221 * was requested through flags and if we 9222 * actually are in a multirouting case. 9223 */ 9224 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9225 boolean_t need_resolve = 9226 ire_multirt_need_resolve(ipha_dst, 9227 MBLK_GETLABEL(copy_mp)); 9228 if (!need_resolve) { 9229 MULTIRT_DEBUG_UNTAG(copy_mp); 9230 freemsg(copy_mp); 9231 copy_mp = NULL; 9232 } else { 9233 /* 9234 * ipif_lookup_group() calls 9235 * ire_lookup_multi() that uses 9236 * ire_ftable_lookup() to find 9237 * an IRE_INTERFACE for the group. 9238 * In the multirt case, 9239 * ire_lookup_multi() then invokes 9240 * ire_multirt_lookup() to find 9241 * the next resolvable ire. 9242 * As a result, we obtain an new 9243 * interface, derived from the 9244 * next ire. 9245 */ 9246 ipif_refrele(ipif); 9247 ipif = ipif_lookup_group(ipha_dst, 9248 zoneid); 9249 ip2dbg(("ip_newroute_ipif: " 9250 "multirt dst %08x, ipif %p\n", 9251 htonl(dst), (void *)ipif)); 9252 if (ipif != NULL) { 9253 mp = copy_mp; 9254 copy_mp = NULL; 9255 multirt_resolve_next = B_TRUE; 9256 continue; 9257 } else { 9258 freemsg(copy_mp); 9259 } 9260 } 9261 } 9262 if (ipif != NULL) 9263 ipif_refrele(ipif); 9264 ill_refrele(dst_ill); 9265 ipif_refrele(src_ipif); 9266 return; 9267 } 9268 case IRE_IF_RESOLVER: 9269 /* 9270 * We can't build an IRE_CACHE yet, but at least 9271 * we found a resolver that can help. 9272 */ 9273 res_mp = dst_ill->ill_resolver_mp; 9274 if (!OK_RESOLVER_MP(res_mp)) 9275 break; 9276 9277 /* 9278 * We obtain a partial IRE_CACHE which we will pass 9279 * along with the resolver query. When the response 9280 * comes back it will be there ready for us to add. 9281 * The new ire inherits the IRE_OFFSUBNET flags 9282 * and source address, if this was requested. 9283 * The ire_max_frag is atomically set under the 9284 * irebucket lock in ire_add_v[46]. Only in the 9285 * case of IRE_MARK_NOADD, we set it here itself. 9286 */ 9287 ire = ire_create_mp( 9288 (uchar_t *)&dst, /* dest address */ 9289 (uchar_t *)&ip_g_all_ones, /* mask */ 9290 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9291 NULL, /* gateway address */ 9292 NULL, /* no in_src_addr */ 9293 (ire_marks & IRE_MARK_NOADD) ? 9294 ipif->ipif_mtu : 0, /* max_frag */ 9295 NULL, /* Fast path header */ 9296 dst_ill->ill_rq, /* recv-from queue */ 9297 dst_ill->ill_wq, /* send-to queue */ 9298 IRE_CACHE, 9299 NULL, /* let ire_nce_init figure res_mp out */ 9300 src_ipif, 9301 NULL, 9302 (save_ire != NULL ? save_ire->ire_mask : 0), 9303 (fire != NULL) ? /* Parent handle */ 9304 fire->ire_phandle : 0, 9305 ihandle, /* Interface handle */ 9306 (fire != NULL) ? /* flags if any */ 9307 (fire->ire_flags & 9308 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9309 (save_ire == NULL ? &ire_uinfo_null : 9310 &save_ire->ire_uinfo), 9311 NULL, 9312 NULL); 9313 9314 if (save_ire != NULL) { 9315 ire_refrele(save_ire); 9316 save_ire = NULL; 9317 } 9318 if (ire == NULL) 9319 break; 9320 9321 ire->ire_marks |= ire_marks; 9322 /* 9323 * Construct message chain for the resolver of the 9324 * form: 9325 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 9326 * 9327 * NOTE : ire will be added later when the response 9328 * comes back from ARP. If the response does not 9329 * come back, ARP frees the packet. For this reason, 9330 * we can't REFHOLD the bucket of save_ire to prevent 9331 * deletions. We may not be able to REFRELE the 9332 * bucket if the response never comes back. 9333 * Thus, before adding the ire, ire_add_v4 will make 9334 * sure that the interface route does not get deleted. 9335 * This is the only case unlike ip_newroute_v6, 9336 * ip_newroute_ipif_v6 where we can always prevent 9337 * deletions because ire_add_then_send is called after 9338 * creating the IRE. 9339 * If IRE_MARK_NOADD is set, then ire_add_then_send 9340 * does not add this IRE into the IRE CACHE. 9341 */ 9342 ASSERT(ire->ire_mp != NULL); 9343 ire->ire_mp->b_cont = first_mp; 9344 /* Have saved_mp handy, for cleanup if canput fails */ 9345 saved_mp = mp; 9346 mp = copyb(res_mp); 9347 if (mp == NULL) { 9348 /* Prepare for cleanup */ 9349 mp = saved_mp; /* pkt */ 9350 ire_delete(ire); /* ire_mp */ 9351 ire = NULL; 9352 if (copy_mp != NULL) { 9353 MULTIRT_DEBUG_UNTAG(copy_mp); 9354 freemsg(copy_mp); 9355 copy_mp = NULL; 9356 } 9357 break; 9358 } 9359 linkb(mp, ire->ire_mp); 9360 9361 /* 9362 * Fill in the source and dest addrs for the resolver. 9363 * NOTE: this depends on memory layouts imposed by 9364 * ill_init(). 9365 */ 9366 areq = (areq_t *)mp->b_rptr; 9367 addrp = (ipaddr_t *)((char *)areq + 9368 areq->areq_sender_addr_offset); 9369 *addrp = ire->ire_src_addr; 9370 addrp = (ipaddr_t *)((char *)areq + 9371 areq->areq_target_addr_offset); 9372 *addrp = dst; 9373 /* Up to the resolver. */ 9374 if (canputnext(dst_ill->ill_rq) && 9375 !(dst_ill->ill_arp_closing)) { 9376 putnext(dst_ill->ill_rq, mp); 9377 /* 9378 * The response will come back in ip_wput 9379 * with db_type IRE_DB_TYPE. 9380 */ 9381 } else { 9382 mp->b_cont = NULL; 9383 freeb(mp); /* areq */ 9384 ire_delete(ire); /* ire_mp */ 9385 saved_mp->b_next = NULL; 9386 saved_mp->b_prev = NULL; 9387 freemsg(first_mp); /* pkt */ 9388 ip2dbg(("ip_newroute_ipif: dropped\n")); 9389 } 9390 9391 if (fire != NULL) { 9392 ire_refrele(fire); 9393 fire = NULL; 9394 } 9395 9396 9397 /* 9398 * The resolution loop is re-entered if this was 9399 * requested through flags and we actually are 9400 * in a multirouting case. 9401 */ 9402 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9403 boolean_t need_resolve = 9404 ire_multirt_need_resolve(ipha_dst, 9405 MBLK_GETLABEL(copy_mp)); 9406 if (!need_resolve) { 9407 MULTIRT_DEBUG_UNTAG(copy_mp); 9408 freemsg(copy_mp); 9409 copy_mp = NULL; 9410 } else { 9411 /* 9412 * ipif_lookup_group() calls 9413 * ire_lookup_multi() that uses 9414 * ire_ftable_lookup() to find 9415 * an IRE_INTERFACE for the group. 9416 * In the multirt case, 9417 * ire_lookup_multi() then invokes 9418 * ire_multirt_lookup() to find 9419 * the next resolvable ire. 9420 * As a result, we obtain an new 9421 * interface, derived from the 9422 * next ire. 9423 */ 9424 ipif_refrele(ipif); 9425 ipif = ipif_lookup_group(ipha_dst, 9426 zoneid); 9427 if (ipif != NULL) { 9428 mp = copy_mp; 9429 copy_mp = NULL; 9430 multirt_resolve_next = B_TRUE; 9431 continue; 9432 } else { 9433 freemsg(copy_mp); 9434 } 9435 } 9436 } 9437 if (ipif != NULL) 9438 ipif_refrele(ipif); 9439 ill_refrele(dst_ill); 9440 ipif_refrele(src_ipif); 9441 return; 9442 default: 9443 break; 9444 } 9445 } while (multirt_resolve_next); 9446 9447 err_ret: 9448 ip2dbg(("ip_newroute_ipif: dropped\n")); 9449 if (fire != NULL) 9450 ire_refrele(fire); 9451 ipif_refrele(ipif); 9452 /* Did this packet originate externally? */ 9453 if (dst_ill != NULL) 9454 ill_refrele(dst_ill); 9455 if (src_ipif != NULL) 9456 ipif_refrele(src_ipif); 9457 if (mp->b_prev || mp->b_next) { 9458 mp->b_next = NULL; 9459 mp->b_prev = NULL; 9460 } else { 9461 /* 9462 * Since ip_wput() isn't close to finished, we fill 9463 * in enough of the header for credible error reporting. 9464 */ 9465 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 9466 /* Failed */ 9467 freemsg(first_mp); 9468 if (ire != NULL) 9469 ire_refrele(ire); 9470 return; 9471 } 9472 } 9473 /* 9474 * At this point we will have ire only if RTF_BLACKHOLE 9475 * or RTF_REJECT flags are set on the IRE. It will not 9476 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9477 */ 9478 if (ire != NULL) { 9479 if (ire->ire_flags & RTF_BLACKHOLE) { 9480 ire_refrele(ire); 9481 freemsg(first_mp); 9482 return; 9483 } 9484 ire_refrele(ire); 9485 } 9486 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid); 9487 } 9488 9489 /* Name/Value Table Lookup Routine */ 9490 char * 9491 ip_nv_lookup(nv_t *nv, int value) 9492 { 9493 if (!nv) 9494 return (NULL); 9495 for (; nv->nv_name; nv++) { 9496 if (nv->nv_value == value) 9497 return (nv->nv_name); 9498 } 9499 return ("unknown"); 9500 } 9501 9502 /* 9503 * one day it can be patched to 1 from /etc/system for machines that have few 9504 * fast network interfaces feeding multiple cpus. 9505 */ 9506 int ill_stream_putlocks = 0; 9507 9508 /* 9509 * This is a module open, i.e. this is a control stream for access 9510 * to a DLPI device. We allocate an ill_t as the instance data in 9511 * this case. 9512 */ 9513 int 9514 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9515 { 9516 uint32_t mem_cnt; 9517 uint32_t cpu_cnt; 9518 uint32_t min_cnt; 9519 pgcnt_t mem_avail; 9520 ill_t *ill; 9521 int err; 9522 9523 /* 9524 * Prevent unprivileged processes from pushing IP so that 9525 * they can't send raw IP. 9526 */ 9527 if (secpolicy_net_rawaccess(credp) != 0) 9528 return (EPERM); 9529 9530 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 9531 q->q_ptr = WR(q)->q_ptr = ill; 9532 9533 /* 9534 * ill_init initializes the ill fields and then sends down 9535 * down a DL_INFO_REQ after calling qprocson. 9536 */ 9537 err = ill_init(q, ill); 9538 if (err != 0) { 9539 mi_free(ill); 9540 q->q_ptr = NULL; 9541 WR(q)->q_ptr = NULL; 9542 return (err); 9543 } 9544 9545 /* ill_init initializes the ipsq marking this thread as writer */ 9546 ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE, B_TRUE); 9547 /* Wait for the DL_INFO_ACK */ 9548 mutex_enter(&ill->ill_lock); 9549 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 9550 /* 9551 * Return value of 0 indicates a pending signal. 9552 */ 9553 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 9554 if (err == 0) { 9555 mutex_exit(&ill->ill_lock); 9556 (void) ip_close(q, 0); 9557 return (EINTR); 9558 } 9559 } 9560 mutex_exit(&ill->ill_lock); 9561 9562 /* 9563 * ip_rput_other could have set an error in ill_error on 9564 * receipt of M_ERROR. 9565 */ 9566 9567 err = ill->ill_error; 9568 if (err != 0) { 9569 (void) ip_close(q, 0); 9570 return (err); 9571 } 9572 9573 /* 9574 * ip_ire_max_bucket_cnt is sized below based on the memory 9575 * size and the cpu speed of the machine. This is upper 9576 * bounded by the compile time value of ip_ire_max_bucket_cnt 9577 * and is lower bounded by the compile time value of 9578 * ip_ire_min_bucket_cnt. Similar logic applies to 9579 * ip6_ire_max_bucket_cnt. 9580 */ 9581 mem_avail = kmem_avail(); 9582 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 9583 ip_cache_table_size / sizeof (ire_t); 9584 cpu_cnt = CPU->cpu_type_info.pi_clock >> ip_ire_cpu_ratio; 9585 9586 min_cnt = MIN(cpu_cnt, mem_cnt); 9587 if (min_cnt < ip_ire_min_bucket_cnt) 9588 min_cnt = ip_ire_min_bucket_cnt; 9589 if (ip_ire_max_bucket_cnt > min_cnt) { 9590 ip_ire_max_bucket_cnt = min_cnt; 9591 } 9592 9593 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 9594 ip6_cache_table_size / sizeof (ire_t); 9595 min_cnt = MIN(cpu_cnt, mem_cnt); 9596 if (min_cnt < ip6_ire_min_bucket_cnt) 9597 min_cnt = ip6_ire_min_bucket_cnt; 9598 if (ip6_ire_max_bucket_cnt > min_cnt) { 9599 ip6_ire_max_bucket_cnt = min_cnt; 9600 } 9601 9602 ill->ill_credp = credp; 9603 crhold(credp); 9604 9605 mutex_enter(&ip_mi_lock); 9606 err = mi_open_link(&ip_g_head, (IDP)ill, devp, flag, sflag, credp); 9607 mutex_exit(&ip_mi_lock); 9608 if (err) { 9609 (void) ip_close(q, 0); 9610 return (err); 9611 } 9612 return (0); 9613 } 9614 9615 /* IP open routine. */ 9616 int 9617 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9618 { 9619 conn_t *connp; 9620 major_t maj; 9621 9622 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 9623 9624 /* Allow reopen. */ 9625 if (q->q_ptr != NULL) 9626 return (0); 9627 9628 if (sflag & MODOPEN) { 9629 /* This is a module open */ 9630 return (ip_modopen(q, devp, flag, sflag, credp)); 9631 } 9632 9633 /* 9634 * We are opening as a device. This is an IP client stream, and we 9635 * allocate an conn_t as the instance data. 9636 */ 9637 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP); 9638 connp->conn_upq = q; 9639 q->q_ptr = WR(q)->q_ptr = connp; 9640 9641 if (flag & SO_SOCKSTR) 9642 connp->conn_flags |= IPCL_SOCKET; 9643 9644 /* Minor tells us which /dev entry was opened */ 9645 if (geteminor(*devp) == IPV6_MINOR) { 9646 connp->conn_flags |= IPCL_ISV6; 9647 connp->conn_af_isv6 = B_TRUE; 9648 ip_setqinfo(q, geteminor(*devp), B_FALSE); 9649 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9650 } else { 9651 connp->conn_af_isv6 = B_FALSE; 9652 connp->conn_pkt_isv6 = B_FALSE; 9653 } 9654 9655 if ((connp->conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9656 q->q_ptr = WR(q)->q_ptr = NULL; 9657 CONN_DEC_REF(connp); 9658 return (EBUSY); 9659 } 9660 9661 maj = getemajor(*devp); 9662 *devp = makedevice(maj, (minor_t)connp->conn_dev); 9663 9664 /* 9665 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 9666 */ 9667 connp->conn_cred = credp; 9668 crhold(connp->conn_cred); 9669 9670 /* 9671 * If the caller has the process-wide flag set, then default to MAC 9672 * exempt mode. This allows read-down to unlabeled hosts. 9673 */ 9674 if (getpflags(NET_MAC_AWARE, credp) != 0) 9675 connp->conn_mac_exempt = B_TRUE; 9676 9677 connp->conn_zoneid = getzoneid(); 9678 9679 /* 9680 * This should only happen for ndd, netstat, raw socket or other SCTP 9681 * administrative ops. In these cases, we just need a normal conn_t 9682 * with ulp set to IPPROTO_SCTP. All other ops are trapped and 9683 * an error will be returned. 9684 */ 9685 if (maj != SCTP_MAJ && maj != SCTP6_MAJ) { 9686 connp->conn_rq = q; 9687 connp->conn_wq = WR(q); 9688 } else { 9689 connp->conn_ulp = IPPROTO_SCTP; 9690 connp->conn_rq = connp->conn_wq = NULL; 9691 } 9692 /* Non-zero default values */ 9693 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9694 9695 /* 9696 * Make the conn globally visible to walkers 9697 */ 9698 mutex_enter(&connp->conn_lock); 9699 connp->conn_state_flags &= ~CONN_INCIPIENT; 9700 mutex_exit(&connp->conn_lock); 9701 ASSERT(connp->conn_ref == 1); 9702 9703 qprocson(q); 9704 9705 return (0); 9706 } 9707 9708 /* 9709 * Change q_qinfo based on the value of isv6. 9710 * This can not called on an ill queue. 9711 * Note that there is no race since either q_qinfo works for conn queues - it 9712 * is just an optimization to enter the best wput routine directly. 9713 */ 9714 void 9715 ip_setqinfo(queue_t *q, minor_t minor, boolean_t bump_mib) 9716 { 9717 ASSERT(q->q_flag & QREADR); 9718 ASSERT(WR(q)->q_next == NULL); 9719 ASSERT(q->q_ptr != NULL); 9720 9721 if (minor == IPV6_MINOR) { 9722 if (bump_mib) 9723 BUMP_MIB(&ip6_mib, ipv6OutSwitchIPv4); 9724 q->q_qinfo = &rinit_ipv6; 9725 WR(q)->q_qinfo = &winit_ipv6; 9726 (Q_TO_CONN(q))->conn_pkt_isv6 = B_TRUE; 9727 } else { 9728 if (bump_mib) 9729 BUMP_MIB(&ip_mib, ipOutSwitchIPv6); 9730 q->q_qinfo = &iprinit; 9731 WR(q)->q_qinfo = &ipwinit; 9732 (Q_TO_CONN(q))->conn_pkt_isv6 = B_FALSE; 9733 } 9734 9735 } 9736 9737 /* 9738 * See if IPsec needs loading because of the options in mp. 9739 */ 9740 static boolean_t 9741 ipsec_opt_present(mblk_t *mp) 9742 { 9743 uint8_t *optcp, *next_optcp, *opt_endcp; 9744 struct opthdr *opt; 9745 struct T_opthdr *topt; 9746 int opthdr_len; 9747 t_uscalar_t optname, optlevel; 9748 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 9749 ipsec_req_t *ipsr; 9750 9751 /* 9752 * Walk through the mess, and find IP_SEC_OPT. If it's there, 9753 * return TRUE. 9754 */ 9755 9756 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 9757 opt_endcp = optcp + tor->OPT_length; 9758 if (tor->PRIM_type == T_OPTMGMT_REQ) { 9759 opthdr_len = sizeof (struct T_opthdr); 9760 } else { /* O_OPTMGMT_REQ */ 9761 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 9762 opthdr_len = sizeof (struct opthdr); 9763 } 9764 for (; optcp < opt_endcp; optcp = next_optcp) { 9765 if (optcp + opthdr_len > opt_endcp) 9766 return (B_FALSE); /* Not enough option header. */ 9767 if (tor->PRIM_type == T_OPTMGMT_REQ) { 9768 topt = (struct T_opthdr *)optcp; 9769 optlevel = topt->level; 9770 optname = topt->name; 9771 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 9772 } else { 9773 opt = (struct opthdr *)optcp; 9774 optlevel = opt->level; 9775 optname = opt->name; 9776 next_optcp = optcp + opthdr_len + 9777 _TPI_ALIGN_OPT(opt->len); 9778 } 9779 if ((next_optcp < optcp) || /* wraparound pointer space */ 9780 ((next_optcp >= opt_endcp) && /* last option bad len */ 9781 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 9782 return (B_FALSE); /* bad option buffer */ 9783 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 9784 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 9785 /* 9786 * Check to see if it's an all-bypass or all-zeroes 9787 * IPsec request. Don't bother loading IPsec if 9788 * the socket doesn't want to use it. (A good example 9789 * is a bypass request.) 9790 * 9791 * Basically, if any of the non-NEVER bits are set, 9792 * load IPsec. 9793 */ 9794 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 9795 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 9796 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 9797 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 9798 != 0) 9799 return (B_TRUE); 9800 } 9801 } 9802 return (B_FALSE); 9803 } 9804 9805 /* 9806 * If conn is is waiting for ipsec to finish loading, kick it. 9807 */ 9808 /* ARGSUSED */ 9809 static void 9810 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 9811 { 9812 t_scalar_t optreq_prim; 9813 mblk_t *mp; 9814 cred_t *cr; 9815 int err = 0; 9816 9817 /* 9818 * This function is called, after ipsec loading is complete. 9819 * Since IP checks exclusively and atomically (i.e it prevents 9820 * ipsec load from completing until ip_optcom_req completes) 9821 * whether ipsec load is complete, there cannot be a race with IP 9822 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 9823 */ 9824 mutex_enter(&connp->conn_lock); 9825 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 9826 ASSERT(connp->conn_ipsec_opt_mp != NULL); 9827 mp = connp->conn_ipsec_opt_mp; 9828 connp->conn_ipsec_opt_mp = NULL; 9829 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 9830 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); 9831 mutex_exit(&connp->conn_lock); 9832 9833 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 9834 9835 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 9836 if (optreq_prim == T_OPTMGMT_REQ) { 9837 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 9838 &ip_opt_obj); 9839 } else { 9840 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 9841 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 9842 &ip_opt_obj); 9843 } 9844 if (err != EINPROGRESS) 9845 CONN_OPER_PENDING_DONE(connp); 9846 return; 9847 } 9848 mutex_exit(&connp->conn_lock); 9849 } 9850 9851 /* 9852 * Called from the ipsec_loader thread, outside any perimeter, to tell 9853 * ip qenable any of the queues waiting for the ipsec loader to 9854 * complete. 9855 * 9856 * Use ip_mi_lock to be safe here: all modifications of the mi lists 9857 * are done with this lock held, so it's guaranteed that none of the 9858 * links will change along the way. 9859 */ 9860 void 9861 ip_ipsec_load_complete() 9862 { 9863 ipcl_walk(conn_restart_ipsec_waiter, NULL); 9864 } 9865 9866 /* 9867 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 9868 * determines the grp on which it has to become exclusive, queues the mp 9869 * and sq draining restarts the optmgmt 9870 */ 9871 static boolean_t 9872 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 9873 { 9874 conn_t *connp; 9875 9876 /* 9877 * Take IPsec requests and treat them special. 9878 */ 9879 if (ipsec_opt_present(mp)) { 9880 /* First check if IPsec is loaded. */ 9881 mutex_enter(&ipsec_loader_lock); 9882 if (ipsec_loader_state != IPSEC_LOADER_WAIT) { 9883 mutex_exit(&ipsec_loader_lock); 9884 return (B_FALSE); 9885 } 9886 connp = Q_TO_CONN(q); 9887 mutex_enter(&connp->conn_lock); 9888 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 9889 9890 ASSERT(connp->conn_ipsec_opt_mp == NULL); 9891 connp->conn_ipsec_opt_mp = mp; 9892 mutex_exit(&connp->conn_lock); 9893 mutex_exit(&ipsec_loader_lock); 9894 9895 ipsec_loader_loadnow(); 9896 return (B_TRUE); 9897 } 9898 return (B_FALSE); 9899 } 9900 9901 /* 9902 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 9903 * all of them are copied to the conn_t. If the req is "zero", the policy is 9904 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 9905 * fields. 9906 * We keep only the latest setting of the policy and thus policy setting 9907 * is not incremental/cumulative. 9908 * 9909 * Requests to set policies with multiple alternative actions will 9910 * go through a different API. 9911 */ 9912 int 9913 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 9914 { 9915 uint_t ah_req = 0; 9916 uint_t esp_req = 0; 9917 uint_t se_req = 0; 9918 ipsec_selkey_t sel; 9919 ipsec_act_t *actp = NULL; 9920 uint_t nact; 9921 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 9922 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 9923 ipsec_policy_root_t *pr; 9924 ipsec_policy_head_t *ph; 9925 int fam; 9926 boolean_t is_pol_reset; 9927 int error = 0; 9928 9929 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 9930 9931 /* 9932 * The IP_SEC_OPT option does not allow variable length parameters, 9933 * hence a request cannot be NULL. 9934 */ 9935 if (req == NULL) 9936 return (EINVAL); 9937 9938 ah_req = req->ipsr_ah_req; 9939 esp_req = req->ipsr_esp_req; 9940 se_req = req->ipsr_self_encap_req; 9941 9942 /* 9943 * Are we dealing with a request to reset the policy (i.e. 9944 * zero requests). 9945 */ 9946 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 9947 (esp_req & REQ_MASK) == 0 && 9948 (se_req & REQ_MASK) == 0); 9949 9950 if (!is_pol_reset) { 9951 /* 9952 * If we couldn't load IPsec, fail with "protocol 9953 * not supported". 9954 * IPsec may not have been loaded for a request with zero 9955 * policies, so we don't fail in this case. 9956 */ 9957 mutex_enter(&ipsec_loader_lock); 9958 if (ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 9959 mutex_exit(&ipsec_loader_lock); 9960 return (EPROTONOSUPPORT); 9961 } 9962 mutex_exit(&ipsec_loader_lock); 9963 9964 /* 9965 * Test for valid requests. Invalid algorithms 9966 * need to be tested by IPSEC code because new 9967 * algorithms can be added dynamically. 9968 */ 9969 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 9970 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 9971 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 9972 return (EINVAL); 9973 } 9974 9975 /* 9976 * Only privileged users can issue these 9977 * requests. 9978 */ 9979 if (((ah_req & IPSEC_PREF_NEVER) || 9980 (esp_req & IPSEC_PREF_NEVER) || 9981 (se_req & IPSEC_PREF_NEVER)) && 9982 secpolicy_net_config(cr, B_FALSE) != 0) { 9983 return (EPERM); 9984 } 9985 9986 /* 9987 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 9988 * are mutually exclusive. 9989 */ 9990 if (((ah_req & REQ_MASK) == REQ_MASK) || 9991 ((esp_req & REQ_MASK) == REQ_MASK) || 9992 ((se_req & REQ_MASK) == REQ_MASK)) { 9993 /* Both of them are set */ 9994 return (EINVAL); 9995 } 9996 } 9997 9998 mutex_enter(&connp->conn_lock); 9999 10000 /* 10001 * If we have already cached policies in ip_bind_connected*(), don't 10002 * let them change now. We cache policies for connections 10003 * whose src,dst [addr, port] is known. The exception to this is 10004 * tunnels. Tunnels are allowed to change policies after having 10005 * become fully bound. 10006 */ 10007 if (connp->conn_policy_cached && !IPCL_IS_IPTUN(connp)) { 10008 mutex_exit(&connp->conn_lock); 10009 return (EINVAL); 10010 } 10011 10012 /* 10013 * We have a zero policies, reset the connection policy if already 10014 * set. This will cause the connection to inherit the 10015 * global policy, if any. 10016 */ 10017 if (is_pol_reset) { 10018 if (connp->conn_policy != NULL) { 10019 IPPH_REFRELE(connp->conn_policy); 10020 connp->conn_policy = NULL; 10021 } 10022 connp->conn_flags &= ~IPCL_CHECK_POLICY; 10023 connp->conn_in_enforce_policy = B_FALSE; 10024 connp->conn_out_enforce_policy = B_FALSE; 10025 mutex_exit(&connp->conn_lock); 10026 return (0); 10027 } 10028 10029 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy); 10030 if (ph == NULL) 10031 goto enomem; 10032 10033 ipsec_actvec_from_req(req, &actp, &nact); 10034 if (actp == NULL) 10035 goto enomem; 10036 10037 /* 10038 * Always allocate IPv4 policy entries, since they can also 10039 * apply to ipv6 sockets being used in ipv4-compat mode. 10040 */ 10041 bzero(&sel, sizeof (sel)); 10042 sel.ipsl_valid = IPSL_IPV4; 10043 10044 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET); 10045 if (pin4 == NULL) 10046 goto enomem; 10047 10048 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET); 10049 if (pout4 == NULL) 10050 goto enomem; 10051 10052 if (connp->conn_pkt_isv6) { 10053 /* 10054 * We're looking at a v6 socket, also allocate the 10055 * v6-specific entries... 10056 */ 10057 sel.ipsl_valid = IPSL_IPV6; 10058 pin6 = ipsec_policy_create(&sel, actp, nact, 10059 IPSEC_PRIO_SOCKET); 10060 if (pin6 == NULL) 10061 goto enomem; 10062 10063 pout6 = ipsec_policy_create(&sel, actp, nact, 10064 IPSEC_PRIO_SOCKET); 10065 if (pout6 == NULL) 10066 goto enomem; 10067 10068 /* 10069 * .. and file them away in the right place. 10070 */ 10071 fam = IPSEC_AF_V6; 10072 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10073 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 10074 ipsec_insert_always(&ph->iph_rulebyid, pin6); 10075 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10076 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 10077 ipsec_insert_always(&ph->iph_rulebyid, pout6); 10078 } 10079 10080 ipsec_actvec_free(actp, nact); 10081 10082 /* 10083 * File the v4 policies. 10084 */ 10085 fam = IPSEC_AF_V4; 10086 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10087 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 10088 ipsec_insert_always(&ph->iph_rulebyid, pin4); 10089 10090 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10091 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 10092 ipsec_insert_always(&ph->iph_rulebyid, pout4); 10093 10094 /* 10095 * If the requests need security, set enforce_policy. 10096 * If the requests are IPSEC_PREF_NEVER, one should 10097 * still set conn_out_enforce_policy so that an ipsec_out 10098 * gets attached in ip_wput. This is needed so that 10099 * for connections that we don't cache policy in ip_bind, 10100 * if global policy matches in ip_wput_attach_policy, we 10101 * don't wrongly inherit global policy. Similarly, we need 10102 * to set conn_in_enforce_policy also so that we don't verify 10103 * policy wrongly. 10104 */ 10105 if ((ah_req & REQ_MASK) != 0 || 10106 (esp_req & REQ_MASK) != 0 || 10107 (se_req & REQ_MASK) != 0) { 10108 connp->conn_in_enforce_policy = B_TRUE; 10109 connp->conn_out_enforce_policy = B_TRUE; 10110 connp->conn_flags |= IPCL_CHECK_POLICY; 10111 } 10112 10113 /* 10114 * Tunnels are allowed to set policy after having been fully bound. 10115 * If that's the case, cache policy here. 10116 */ 10117 if (IPCL_IS_IPTUN(connp) && connp->conn_fully_bound) 10118 error = ipsec_conn_cache_policy(connp, !connp->conn_af_isv6); 10119 10120 mutex_exit(&connp->conn_lock); 10121 return (error); 10122 #undef REQ_MASK 10123 10124 /* 10125 * Common memory-allocation-failure exit path. 10126 */ 10127 enomem: 10128 mutex_exit(&connp->conn_lock); 10129 if (actp != NULL) 10130 ipsec_actvec_free(actp, nact); 10131 if (pin4 != NULL) 10132 IPPOL_REFRELE(pin4); 10133 if (pout4 != NULL) 10134 IPPOL_REFRELE(pout4); 10135 if (pin6 != NULL) 10136 IPPOL_REFRELE(pin6); 10137 if (pout6 != NULL) 10138 IPPOL_REFRELE(pout6); 10139 return (ENOMEM); 10140 } 10141 10142 /* 10143 * Only for options that pass in an IP addr. Currently only V4 options 10144 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 10145 * So this function assumes level is IPPROTO_IP 10146 */ 10147 int 10148 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 10149 mblk_t *first_mp) 10150 { 10151 ipif_t *ipif = NULL; 10152 int error; 10153 ill_t *ill; 10154 int zoneid; 10155 10156 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 10157 10158 if (addr != INADDR_ANY || checkonly) { 10159 ASSERT(connp != NULL); 10160 zoneid = IPCL_ZONEID(connp); 10161 if (option == IP_NEXTHOP) { 10162 ipif = ipif_lookup_onlink_addr(addr, 10163 connp->conn_zoneid); 10164 } else { 10165 ipif = ipif_lookup_addr(addr, NULL, zoneid, 10166 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 10167 &error); 10168 } 10169 if (ipif == NULL) { 10170 if (error == EINPROGRESS) 10171 return (error); 10172 else if ((option == IP_MULTICAST_IF) || 10173 (option == IP_NEXTHOP)) 10174 return (EHOSTUNREACH); 10175 else 10176 return (EINVAL); 10177 } else if (checkonly) { 10178 if (option == IP_MULTICAST_IF) { 10179 ill = ipif->ipif_ill; 10180 /* not supported by the virtual network iface */ 10181 if (IS_VNI(ill)) { 10182 ipif_refrele(ipif); 10183 return (EINVAL); 10184 } 10185 } 10186 ipif_refrele(ipif); 10187 return (0); 10188 } 10189 ill = ipif->ipif_ill; 10190 mutex_enter(&connp->conn_lock); 10191 mutex_enter(&ill->ill_lock); 10192 if ((ill->ill_state_flags & ILL_CONDEMNED) || 10193 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 10194 mutex_exit(&ill->ill_lock); 10195 mutex_exit(&connp->conn_lock); 10196 ipif_refrele(ipif); 10197 return (option == IP_MULTICAST_IF ? 10198 EHOSTUNREACH : EINVAL); 10199 } 10200 } else { 10201 mutex_enter(&connp->conn_lock); 10202 } 10203 10204 /* None of the options below are supported on the VNI */ 10205 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 10206 mutex_exit(&ill->ill_lock); 10207 mutex_exit(&connp->conn_lock); 10208 ipif_refrele(ipif); 10209 return (EINVAL); 10210 } 10211 10212 switch (option) { 10213 case IP_DONTFAILOVER_IF: 10214 /* 10215 * This option is used by in.mpathd to ensure 10216 * that IPMP probe packets only go out on the 10217 * test interfaces. in.mpathd sets this option 10218 * on the non-failover interfaces. 10219 * For backward compatibility, this option 10220 * implicitly sets IP_MULTICAST_IF, as used 10221 * be done in bind(), so that ip_wput gets 10222 * this ipif to send mcast packets. 10223 */ 10224 if (ipif != NULL) { 10225 ASSERT(addr != INADDR_ANY); 10226 connp->conn_nofailover_ill = ipif->ipif_ill; 10227 connp->conn_multicast_ipif = ipif; 10228 } else { 10229 ASSERT(addr == INADDR_ANY); 10230 connp->conn_nofailover_ill = NULL; 10231 connp->conn_multicast_ipif = NULL; 10232 } 10233 break; 10234 10235 case IP_MULTICAST_IF: 10236 connp->conn_multicast_ipif = ipif; 10237 break; 10238 case IP_NEXTHOP: 10239 connp->conn_nexthop_v4 = addr; 10240 connp->conn_nexthop_set = B_TRUE; 10241 break; 10242 } 10243 10244 if (ipif != NULL) { 10245 mutex_exit(&ill->ill_lock); 10246 mutex_exit(&connp->conn_lock); 10247 ipif_refrele(ipif); 10248 return (0); 10249 } 10250 mutex_exit(&connp->conn_lock); 10251 /* We succeded in cleared the option */ 10252 return (0); 10253 } 10254 10255 /* 10256 * For options that pass in an ifindex specifying the ill. V6 options always 10257 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 10258 */ 10259 int 10260 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 10261 int level, int option, mblk_t *first_mp) 10262 { 10263 ill_t *ill = NULL; 10264 int error = 0; 10265 10266 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 10267 if (ifindex != 0) { 10268 ASSERT(connp != NULL); 10269 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 10270 first_mp, ip_restart_optmgmt, &error); 10271 if (ill != NULL) { 10272 if (checkonly) { 10273 /* not supported by the virtual network iface */ 10274 if (IS_VNI(ill)) { 10275 ill_refrele(ill); 10276 return (EINVAL); 10277 } 10278 ill_refrele(ill); 10279 return (0); 10280 } 10281 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 10282 0, NULL)) { 10283 ill_refrele(ill); 10284 ill = NULL; 10285 mutex_enter(&connp->conn_lock); 10286 goto setit; 10287 } 10288 mutex_enter(&connp->conn_lock); 10289 mutex_enter(&ill->ill_lock); 10290 if (ill->ill_state_flags & ILL_CONDEMNED) { 10291 mutex_exit(&ill->ill_lock); 10292 mutex_exit(&connp->conn_lock); 10293 ill_refrele(ill); 10294 ill = NULL; 10295 mutex_enter(&connp->conn_lock); 10296 } 10297 goto setit; 10298 } else if (error == EINPROGRESS) { 10299 return (error); 10300 } else { 10301 error = 0; 10302 } 10303 } 10304 mutex_enter(&connp->conn_lock); 10305 setit: 10306 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 10307 10308 /* 10309 * The options below assume that the ILL (if any) transmits and/or 10310 * receives traffic. Neither of which is true for the virtual network 10311 * interface, so fail setting these on a VNI. 10312 */ 10313 if (IS_VNI(ill)) { 10314 ASSERT(ill != NULL); 10315 mutex_exit(&ill->ill_lock); 10316 mutex_exit(&connp->conn_lock); 10317 ill_refrele(ill); 10318 return (EINVAL); 10319 } 10320 10321 if (level == IPPROTO_IP) { 10322 switch (option) { 10323 case IP_BOUND_IF: 10324 connp->conn_incoming_ill = ill; 10325 connp->conn_outgoing_ill = ill; 10326 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10327 0 : ifindex; 10328 break; 10329 10330 case IP_XMIT_IF: 10331 /* 10332 * Similar to IP_BOUND_IF, but this only 10333 * determines the outgoing interface for 10334 * unicast packets. Also no IRE_CACHE entry 10335 * is added for the destination of the 10336 * outgoing packets. This feature is needed 10337 * for mobile IP. 10338 */ 10339 connp->conn_xmit_if_ill = ill; 10340 connp->conn_orig_xmit_ifindex = (ill == NULL) ? 10341 0 : ifindex; 10342 break; 10343 10344 case IP_MULTICAST_IF: 10345 /* 10346 * This option is an internal special. The socket 10347 * level IP_MULTICAST_IF specifies an 'ipaddr' and 10348 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 10349 * specifies an ifindex and we try first on V6 ill's. 10350 * If we don't find one, we they try using on v4 ill's 10351 * intenally and we come here. 10352 */ 10353 if (!checkonly && ill != NULL) { 10354 ipif_t *ipif; 10355 ipif = ill->ill_ipif; 10356 10357 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 10358 mutex_exit(&ill->ill_lock); 10359 mutex_exit(&connp->conn_lock); 10360 ill_refrele(ill); 10361 ill = NULL; 10362 mutex_enter(&connp->conn_lock); 10363 } else { 10364 connp->conn_multicast_ipif = ipif; 10365 } 10366 } 10367 break; 10368 } 10369 } else { 10370 switch (option) { 10371 case IPV6_BOUND_IF: 10372 connp->conn_incoming_ill = ill; 10373 connp->conn_outgoing_ill = ill; 10374 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10375 0 : ifindex; 10376 break; 10377 10378 case IPV6_BOUND_PIF: 10379 /* 10380 * Limit all transmit to this ill. 10381 * Unlike IPV6_BOUND_IF, using this option 10382 * prevents load spreading and failover from 10383 * happening when the interface is part of the 10384 * group. That's why we don't need to remember 10385 * the ifindex in orig_bound_ifindex as in 10386 * IPV6_BOUND_IF. 10387 */ 10388 connp->conn_outgoing_pill = ill; 10389 break; 10390 10391 case IPV6_DONTFAILOVER_IF: 10392 /* 10393 * This option is used by in.mpathd to ensure 10394 * that IPMP probe packets only go out on the 10395 * test interfaces. in.mpathd sets this option 10396 * on the non-failover interfaces. 10397 */ 10398 connp->conn_nofailover_ill = ill; 10399 /* 10400 * For backward compatibility, this option 10401 * implicitly sets ip_multicast_ill as used in 10402 * IP_MULTICAST_IF so that ip_wput gets 10403 * this ipif to send mcast packets. 10404 */ 10405 connp->conn_multicast_ill = ill; 10406 connp->conn_orig_multicast_ifindex = (ill == NULL) ? 10407 0 : ifindex; 10408 break; 10409 10410 case IPV6_MULTICAST_IF: 10411 /* 10412 * Set conn_multicast_ill to be the IPv6 ill. 10413 * Set conn_multicast_ipif to be an IPv4 ipif 10414 * for ifindex to make IPv4 mapped addresses 10415 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 10416 * Even if no IPv6 ill exists for the ifindex 10417 * we need to check for an IPv4 ifindex in order 10418 * for this to work with mapped addresses. In that 10419 * case only set conn_multicast_ipif. 10420 */ 10421 if (!checkonly) { 10422 if (ifindex == 0) { 10423 connp->conn_multicast_ill = NULL; 10424 connp->conn_orig_multicast_ifindex = 0; 10425 connp->conn_multicast_ipif = NULL; 10426 } else if (ill != NULL) { 10427 connp->conn_multicast_ill = ill; 10428 connp->conn_orig_multicast_ifindex = 10429 ifindex; 10430 } 10431 } 10432 break; 10433 } 10434 } 10435 10436 if (ill != NULL) { 10437 mutex_exit(&ill->ill_lock); 10438 mutex_exit(&connp->conn_lock); 10439 ill_refrele(ill); 10440 return (0); 10441 } 10442 mutex_exit(&connp->conn_lock); 10443 /* 10444 * We succeeded in clearing the option (ifindex == 0) or failed to 10445 * locate the ill and could not set the option (ifindex != 0) 10446 */ 10447 return (ifindex == 0 ? 0 : EINVAL); 10448 } 10449 10450 /* This routine sets socket options. */ 10451 /* ARGSUSED */ 10452 int 10453 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10454 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10455 void *dummy, cred_t *cr, mblk_t *first_mp) 10456 { 10457 int *i1 = (int *)invalp; 10458 conn_t *connp = Q_TO_CONN(q); 10459 int error = 0; 10460 boolean_t checkonly; 10461 ire_t *ire; 10462 boolean_t found; 10463 10464 switch (optset_context) { 10465 10466 case SETFN_OPTCOM_CHECKONLY: 10467 checkonly = B_TRUE; 10468 /* 10469 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10470 * inlen != 0 implies value supplied and 10471 * we have to "pretend" to set it. 10472 * inlen == 0 implies that there is no 10473 * value part in T_CHECK request and just validation 10474 * done elsewhere should be enough, we just return here. 10475 */ 10476 if (inlen == 0) { 10477 *outlenp = 0; 10478 return (0); 10479 } 10480 break; 10481 case SETFN_OPTCOM_NEGOTIATE: 10482 case SETFN_UD_NEGOTIATE: 10483 case SETFN_CONN_NEGOTIATE: 10484 checkonly = B_FALSE; 10485 break; 10486 default: 10487 /* 10488 * We should never get here 10489 */ 10490 *outlenp = 0; 10491 return (EINVAL); 10492 } 10493 10494 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10495 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10496 10497 /* 10498 * For fixed length options, no sanity check 10499 * of passed in length is done. It is assumed *_optcom_req() 10500 * routines do the right thing. 10501 */ 10502 10503 switch (level) { 10504 case SOL_SOCKET: 10505 /* 10506 * conn_lock protects the bitfields, and is used to 10507 * set the fields atomically. 10508 */ 10509 switch (name) { 10510 case SO_BROADCAST: 10511 if (!checkonly) { 10512 /* TODO: use value someplace? */ 10513 mutex_enter(&connp->conn_lock); 10514 connp->conn_broadcast = *i1 ? 1 : 0; 10515 mutex_exit(&connp->conn_lock); 10516 } 10517 break; /* goto sizeof (int) option return */ 10518 case SO_USELOOPBACK: 10519 if (!checkonly) { 10520 /* TODO: use value someplace? */ 10521 mutex_enter(&connp->conn_lock); 10522 connp->conn_loopback = *i1 ? 1 : 0; 10523 mutex_exit(&connp->conn_lock); 10524 } 10525 break; /* goto sizeof (int) option return */ 10526 case SO_DONTROUTE: 10527 if (!checkonly) { 10528 mutex_enter(&connp->conn_lock); 10529 connp->conn_dontroute = *i1 ? 1 : 0; 10530 mutex_exit(&connp->conn_lock); 10531 } 10532 break; /* goto sizeof (int) option return */ 10533 case SO_REUSEADDR: 10534 if (!checkonly) { 10535 mutex_enter(&connp->conn_lock); 10536 connp->conn_reuseaddr = *i1 ? 1 : 0; 10537 mutex_exit(&connp->conn_lock); 10538 } 10539 break; /* goto sizeof (int) option return */ 10540 case SO_PROTOTYPE: 10541 if (!checkonly) { 10542 mutex_enter(&connp->conn_lock); 10543 connp->conn_proto = *i1; 10544 mutex_exit(&connp->conn_lock); 10545 } 10546 break; /* goto sizeof (int) option return */ 10547 case SO_ALLZONES: 10548 if (!checkonly) { 10549 mutex_enter(&connp->conn_lock); 10550 if (IPCL_IS_BOUND(connp)) { 10551 mutex_exit(&connp->conn_lock); 10552 return (EINVAL); 10553 } 10554 connp->conn_allzones = *i1 != 0 ? 1 : 0; 10555 mutex_exit(&connp->conn_lock); 10556 } 10557 break; /* goto sizeof (int) option return */ 10558 case SO_ANON_MLP: 10559 if (!checkonly) { 10560 mutex_enter(&connp->conn_lock); 10561 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; 10562 mutex_exit(&connp->conn_lock); 10563 } 10564 break; /* goto sizeof (int) option return */ 10565 case SO_MAC_EXEMPT: 10566 if (secpolicy_net_mac_aware(cr) != 0 || 10567 IPCL_IS_BOUND(connp)) 10568 return (EACCES); 10569 if (!checkonly) { 10570 mutex_enter(&connp->conn_lock); 10571 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; 10572 mutex_exit(&connp->conn_lock); 10573 } 10574 break; /* goto sizeof (int) option return */ 10575 default: 10576 /* 10577 * "soft" error (negative) 10578 * option not handled at this level 10579 * Note: Do not modify *outlenp 10580 */ 10581 return (-EINVAL); 10582 } 10583 break; 10584 case IPPROTO_IP: 10585 switch (name) { 10586 case IP_NEXTHOP: 10587 if (secpolicy_net_config(cr, B_FALSE) != 0) 10588 return (EPERM); 10589 /* FALLTHRU */ 10590 case IP_MULTICAST_IF: 10591 case IP_DONTFAILOVER_IF: { 10592 ipaddr_t addr = *i1; 10593 10594 error = ip_opt_set_ipif(connp, addr, checkonly, name, 10595 first_mp); 10596 if (error != 0) 10597 return (error); 10598 break; /* goto sizeof (int) option return */ 10599 } 10600 10601 case IP_MULTICAST_TTL: 10602 /* Recorded in transport above IP */ 10603 *outvalp = *invalp; 10604 *outlenp = sizeof (uchar_t); 10605 return (0); 10606 case IP_MULTICAST_LOOP: 10607 if (!checkonly) { 10608 mutex_enter(&connp->conn_lock); 10609 connp->conn_multicast_loop = *invalp ? 1 : 0; 10610 mutex_exit(&connp->conn_lock); 10611 } 10612 *outvalp = *invalp; 10613 *outlenp = sizeof (uchar_t); 10614 return (0); 10615 case IP_ADD_MEMBERSHIP: 10616 case MCAST_JOIN_GROUP: 10617 case IP_DROP_MEMBERSHIP: 10618 case MCAST_LEAVE_GROUP: { 10619 struct ip_mreq *mreqp; 10620 struct group_req *greqp; 10621 ire_t *ire; 10622 boolean_t done = B_FALSE; 10623 ipaddr_t group, ifaddr; 10624 struct sockaddr_in *sin; 10625 uint32_t *ifindexp; 10626 boolean_t mcast_opt = B_TRUE; 10627 mcast_record_t fmode; 10628 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10629 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10630 10631 switch (name) { 10632 case IP_ADD_MEMBERSHIP: 10633 mcast_opt = B_FALSE; 10634 /* FALLTHRU */ 10635 case MCAST_JOIN_GROUP: 10636 fmode = MODE_IS_EXCLUDE; 10637 optfn = ip_opt_add_group; 10638 break; 10639 10640 case IP_DROP_MEMBERSHIP: 10641 mcast_opt = B_FALSE; 10642 /* FALLTHRU */ 10643 case MCAST_LEAVE_GROUP: 10644 fmode = MODE_IS_INCLUDE; 10645 optfn = ip_opt_delete_group; 10646 break; 10647 } 10648 10649 if (mcast_opt) { 10650 greqp = (struct group_req *)i1; 10651 sin = (struct sockaddr_in *)&greqp->gr_group; 10652 if (sin->sin_family != AF_INET) { 10653 *outlenp = 0; 10654 return (ENOPROTOOPT); 10655 } 10656 group = (ipaddr_t)sin->sin_addr.s_addr; 10657 ifaddr = INADDR_ANY; 10658 ifindexp = &greqp->gr_interface; 10659 } else { 10660 mreqp = (struct ip_mreq *)i1; 10661 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 10662 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 10663 ifindexp = NULL; 10664 } 10665 10666 /* 10667 * In the multirouting case, we need to replicate 10668 * the request on all interfaces that will take part 10669 * in replication. We do so because multirouting is 10670 * reflective, thus we will probably receive multi- 10671 * casts on those interfaces. 10672 * The ip_multirt_apply_membership() succeeds if the 10673 * operation succeeds on at least one interface. 10674 */ 10675 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 10676 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10677 MATCH_IRE_MASK | MATCH_IRE_TYPE); 10678 if (ire != NULL) { 10679 if (ire->ire_flags & RTF_MULTIRT) { 10680 error = ip_multirt_apply_membership( 10681 optfn, ire, connp, checkonly, group, 10682 fmode, INADDR_ANY, first_mp); 10683 done = B_TRUE; 10684 } 10685 ire_refrele(ire); 10686 } 10687 if (!done) { 10688 error = optfn(connp, checkonly, group, ifaddr, 10689 ifindexp, fmode, INADDR_ANY, first_mp); 10690 } 10691 if (error) { 10692 /* 10693 * EINPROGRESS is a soft error, needs retry 10694 * so don't make *outlenp zero. 10695 */ 10696 if (error != EINPROGRESS) 10697 *outlenp = 0; 10698 return (error); 10699 } 10700 /* OK return - copy input buffer into output buffer */ 10701 if (invalp != outvalp) { 10702 /* don't trust bcopy for identical src/dst */ 10703 bcopy(invalp, outvalp, inlen); 10704 } 10705 *outlenp = inlen; 10706 return (0); 10707 } 10708 case IP_BLOCK_SOURCE: 10709 case IP_UNBLOCK_SOURCE: 10710 case IP_ADD_SOURCE_MEMBERSHIP: 10711 case IP_DROP_SOURCE_MEMBERSHIP: 10712 case MCAST_BLOCK_SOURCE: 10713 case MCAST_UNBLOCK_SOURCE: 10714 case MCAST_JOIN_SOURCE_GROUP: 10715 case MCAST_LEAVE_SOURCE_GROUP: { 10716 struct ip_mreq_source *imreqp; 10717 struct group_source_req *gsreqp; 10718 in_addr_t grp, src, ifaddr = INADDR_ANY; 10719 uint32_t ifindex = 0; 10720 mcast_record_t fmode; 10721 struct sockaddr_in *sin; 10722 ire_t *ire; 10723 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 10724 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10725 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10726 10727 switch (name) { 10728 case IP_BLOCK_SOURCE: 10729 mcast_opt = B_FALSE; 10730 /* FALLTHRU */ 10731 case MCAST_BLOCK_SOURCE: 10732 fmode = MODE_IS_EXCLUDE; 10733 optfn = ip_opt_add_group; 10734 break; 10735 10736 case IP_UNBLOCK_SOURCE: 10737 mcast_opt = B_FALSE; 10738 /* FALLTHRU */ 10739 case MCAST_UNBLOCK_SOURCE: 10740 fmode = MODE_IS_EXCLUDE; 10741 optfn = ip_opt_delete_group; 10742 break; 10743 10744 case IP_ADD_SOURCE_MEMBERSHIP: 10745 mcast_opt = B_FALSE; 10746 /* FALLTHRU */ 10747 case MCAST_JOIN_SOURCE_GROUP: 10748 fmode = MODE_IS_INCLUDE; 10749 optfn = ip_opt_add_group; 10750 break; 10751 10752 case IP_DROP_SOURCE_MEMBERSHIP: 10753 mcast_opt = B_FALSE; 10754 /* FALLTHRU */ 10755 case MCAST_LEAVE_SOURCE_GROUP: 10756 fmode = MODE_IS_INCLUDE; 10757 optfn = ip_opt_delete_group; 10758 break; 10759 } 10760 10761 if (mcast_opt) { 10762 gsreqp = (struct group_source_req *)i1; 10763 if (gsreqp->gsr_group.ss_family != AF_INET) { 10764 *outlenp = 0; 10765 return (ENOPROTOOPT); 10766 } 10767 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 10768 grp = (ipaddr_t)sin->sin_addr.s_addr; 10769 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 10770 src = (ipaddr_t)sin->sin_addr.s_addr; 10771 ifindex = gsreqp->gsr_interface; 10772 } else { 10773 imreqp = (struct ip_mreq_source *)i1; 10774 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 10775 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 10776 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 10777 } 10778 10779 /* 10780 * In the multirouting case, we need to replicate 10781 * the request as noted in the mcast cases above. 10782 */ 10783 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 10784 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10785 MATCH_IRE_MASK | MATCH_IRE_TYPE); 10786 if (ire != NULL) { 10787 if (ire->ire_flags & RTF_MULTIRT) { 10788 error = ip_multirt_apply_membership( 10789 optfn, ire, connp, checkonly, grp, 10790 fmode, src, first_mp); 10791 done = B_TRUE; 10792 } 10793 ire_refrele(ire); 10794 } 10795 if (!done) { 10796 error = optfn(connp, checkonly, grp, ifaddr, 10797 &ifindex, fmode, src, first_mp); 10798 } 10799 if (error != 0) { 10800 /* 10801 * EINPROGRESS is a soft error, needs retry 10802 * so don't make *outlenp zero. 10803 */ 10804 if (error != EINPROGRESS) 10805 *outlenp = 0; 10806 return (error); 10807 } 10808 /* OK return - copy input buffer into output buffer */ 10809 if (invalp != outvalp) { 10810 bcopy(invalp, outvalp, inlen); 10811 } 10812 *outlenp = inlen; 10813 return (0); 10814 } 10815 case IP_SEC_OPT: 10816 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 10817 if (error != 0) { 10818 *outlenp = 0; 10819 return (error); 10820 } 10821 break; 10822 case IP_HDRINCL: 10823 case IP_OPTIONS: 10824 case T_IP_OPTIONS: 10825 case IP_TOS: 10826 case T_IP_TOS: 10827 case IP_TTL: 10828 case IP_RECVDSTADDR: 10829 case IP_RECVOPTS: 10830 /* OK return - copy input buffer into output buffer */ 10831 if (invalp != outvalp) { 10832 /* don't trust bcopy for identical src/dst */ 10833 bcopy(invalp, outvalp, inlen); 10834 } 10835 *outlenp = inlen; 10836 return (0); 10837 case IP_RECVIF: 10838 /* Retrieve the inbound interface index */ 10839 if (!checkonly) { 10840 mutex_enter(&connp->conn_lock); 10841 connp->conn_recvif = *i1 ? 1 : 0; 10842 mutex_exit(&connp->conn_lock); 10843 } 10844 break; /* goto sizeof (int) option return */ 10845 case IP_RECVSLLA: 10846 /* Retrieve the source link layer address */ 10847 if (!checkonly) { 10848 mutex_enter(&connp->conn_lock); 10849 connp->conn_recvslla = *i1 ? 1 : 0; 10850 mutex_exit(&connp->conn_lock); 10851 } 10852 break; /* goto sizeof (int) option return */ 10853 case MRT_INIT: 10854 case MRT_DONE: 10855 case MRT_ADD_VIF: 10856 case MRT_DEL_VIF: 10857 case MRT_ADD_MFC: 10858 case MRT_DEL_MFC: 10859 case MRT_ASSERT: 10860 if ((error = secpolicy_net_config(cr, B_FALSE)) != 0) { 10861 *outlenp = 0; 10862 return (error); 10863 } 10864 error = ip_mrouter_set((int)name, q, checkonly, 10865 (uchar_t *)invalp, inlen, first_mp); 10866 if (error) { 10867 *outlenp = 0; 10868 return (error); 10869 } 10870 /* OK return - copy input buffer into output buffer */ 10871 if (invalp != outvalp) { 10872 /* don't trust bcopy for identical src/dst */ 10873 bcopy(invalp, outvalp, inlen); 10874 } 10875 *outlenp = inlen; 10876 return (0); 10877 case IP_BOUND_IF: 10878 case IP_XMIT_IF: 10879 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 10880 level, name, first_mp); 10881 if (error != 0) 10882 return (error); 10883 break; /* goto sizeof (int) option return */ 10884 10885 case IP_UNSPEC_SRC: 10886 /* Allow sending with a zero source address */ 10887 if (!checkonly) { 10888 mutex_enter(&connp->conn_lock); 10889 connp->conn_unspec_src = *i1 ? 1 : 0; 10890 mutex_exit(&connp->conn_lock); 10891 } 10892 break; /* goto sizeof (int) option return */ 10893 default: 10894 /* 10895 * "soft" error (negative) 10896 * option not handled at this level 10897 * Note: Do not modify *outlenp 10898 */ 10899 return (-EINVAL); 10900 } 10901 break; 10902 case IPPROTO_IPV6: 10903 switch (name) { 10904 case IPV6_BOUND_IF: 10905 case IPV6_BOUND_PIF: 10906 case IPV6_DONTFAILOVER_IF: 10907 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 10908 level, name, first_mp); 10909 if (error != 0) 10910 return (error); 10911 break; /* goto sizeof (int) option return */ 10912 10913 case IPV6_MULTICAST_IF: 10914 /* 10915 * The only possible errors are EINPROGRESS and 10916 * EINVAL. EINPROGRESS will be restarted and is not 10917 * a hard error. We call this option on both V4 and V6 10918 * If both return EINVAL, then this call returns 10919 * EINVAL. If at least one of them succeeds we 10920 * return success. 10921 */ 10922 found = B_FALSE; 10923 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 10924 level, name, first_mp); 10925 if (error == EINPROGRESS) 10926 return (error); 10927 if (error == 0) 10928 found = B_TRUE; 10929 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 10930 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 10931 if (error == 0) 10932 found = B_TRUE; 10933 if (!found) 10934 return (error); 10935 break; /* goto sizeof (int) option return */ 10936 10937 case IPV6_MULTICAST_HOPS: 10938 /* Recorded in transport above IP */ 10939 break; /* goto sizeof (int) option return */ 10940 case IPV6_MULTICAST_LOOP: 10941 if (!checkonly) { 10942 mutex_enter(&connp->conn_lock); 10943 connp->conn_multicast_loop = *i1; 10944 mutex_exit(&connp->conn_lock); 10945 } 10946 break; /* goto sizeof (int) option return */ 10947 case IPV6_JOIN_GROUP: 10948 case MCAST_JOIN_GROUP: 10949 case IPV6_LEAVE_GROUP: 10950 case MCAST_LEAVE_GROUP: { 10951 struct ipv6_mreq *ip_mreqp; 10952 struct group_req *greqp; 10953 ire_t *ire; 10954 boolean_t done = B_FALSE; 10955 in6_addr_t groupv6; 10956 uint32_t ifindex; 10957 boolean_t mcast_opt = B_TRUE; 10958 mcast_record_t fmode; 10959 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 10960 int, mcast_record_t, const in6_addr_t *, mblk_t *); 10961 10962 switch (name) { 10963 case IPV6_JOIN_GROUP: 10964 mcast_opt = B_FALSE; 10965 /* FALLTHRU */ 10966 case MCAST_JOIN_GROUP: 10967 fmode = MODE_IS_EXCLUDE; 10968 optfn = ip_opt_add_group_v6; 10969 break; 10970 10971 case IPV6_LEAVE_GROUP: 10972 mcast_opt = B_FALSE; 10973 /* FALLTHRU */ 10974 case MCAST_LEAVE_GROUP: 10975 fmode = MODE_IS_INCLUDE; 10976 optfn = ip_opt_delete_group_v6; 10977 break; 10978 } 10979 10980 if (mcast_opt) { 10981 struct sockaddr_in *sin; 10982 struct sockaddr_in6 *sin6; 10983 greqp = (struct group_req *)i1; 10984 if (greqp->gr_group.ss_family == AF_INET) { 10985 sin = (struct sockaddr_in *) 10986 &(greqp->gr_group); 10987 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 10988 &groupv6); 10989 } else { 10990 sin6 = (struct sockaddr_in6 *) 10991 &(greqp->gr_group); 10992 groupv6 = sin6->sin6_addr; 10993 } 10994 ifindex = greqp->gr_interface; 10995 } else { 10996 ip_mreqp = (struct ipv6_mreq *)i1; 10997 groupv6 = ip_mreqp->ipv6mr_multiaddr; 10998 ifindex = ip_mreqp->ipv6mr_interface; 10999 } 11000 /* 11001 * In the multirouting case, we need to replicate 11002 * the request on all interfaces that will take part 11003 * in replication. We do so because multirouting is 11004 * reflective, thus we will probably receive multi- 11005 * casts on those interfaces. 11006 * The ip_multirt_apply_membership_v6() succeeds if 11007 * the operation succeeds on at least one interface. 11008 */ 11009 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 11010 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11011 MATCH_IRE_MASK | MATCH_IRE_TYPE); 11012 if (ire != NULL) { 11013 if (ire->ire_flags & RTF_MULTIRT) { 11014 error = ip_multirt_apply_membership_v6( 11015 optfn, ire, connp, checkonly, 11016 &groupv6, fmode, &ipv6_all_zeros, 11017 first_mp); 11018 done = B_TRUE; 11019 } 11020 ire_refrele(ire); 11021 } 11022 if (!done) { 11023 error = optfn(connp, checkonly, &groupv6, 11024 ifindex, fmode, &ipv6_all_zeros, first_mp); 11025 } 11026 if (error) { 11027 /* 11028 * EINPROGRESS is a soft error, needs retry 11029 * so don't make *outlenp zero. 11030 */ 11031 if (error != EINPROGRESS) 11032 *outlenp = 0; 11033 return (error); 11034 } 11035 /* OK return - copy input buffer into output buffer */ 11036 if (invalp != outvalp) { 11037 /* don't trust bcopy for identical src/dst */ 11038 bcopy(invalp, outvalp, inlen); 11039 } 11040 *outlenp = inlen; 11041 return (0); 11042 } 11043 case MCAST_BLOCK_SOURCE: 11044 case MCAST_UNBLOCK_SOURCE: 11045 case MCAST_JOIN_SOURCE_GROUP: 11046 case MCAST_LEAVE_SOURCE_GROUP: { 11047 struct group_source_req *gsreqp; 11048 in6_addr_t v6grp, v6src; 11049 uint32_t ifindex; 11050 mcast_record_t fmode; 11051 ire_t *ire; 11052 boolean_t done = B_FALSE; 11053 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11054 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11055 11056 switch (name) { 11057 case MCAST_BLOCK_SOURCE: 11058 fmode = MODE_IS_EXCLUDE; 11059 optfn = ip_opt_add_group_v6; 11060 break; 11061 case MCAST_UNBLOCK_SOURCE: 11062 fmode = MODE_IS_EXCLUDE; 11063 optfn = ip_opt_delete_group_v6; 11064 break; 11065 case MCAST_JOIN_SOURCE_GROUP: 11066 fmode = MODE_IS_INCLUDE; 11067 optfn = ip_opt_add_group_v6; 11068 break; 11069 case MCAST_LEAVE_SOURCE_GROUP: 11070 fmode = MODE_IS_INCLUDE; 11071 optfn = ip_opt_delete_group_v6; 11072 break; 11073 } 11074 11075 gsreqp = (struct group_source_req *)i1; 11076 ifindex = gsreqp->gsr_interface; 11077 if (gsreqp->gsr_group.ss_family == AF_INET) { 11078 struct sockaddr_in *s; 11079 s = (struct sockaddr_in *)&gsreqp->gsr_group; 11080 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 11081 s = (struct sockaddr_in *)&gsreqp->gsr_source; 11082 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 11083 } else { 11084 struct sockaddr_in6 *s6; 11085 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 11086 v6grp = s6->sin6_addr; 11087 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 11088 v6src = s6->sin6_addr; 11089 } 11090 11091 /* 11092 * In the multirouting case, we need to replicate 11093 * the request as noted in the mcast cases above. 11094 */ 11095 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 11096 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11097 MATCH_IRE_MASK | MATCH_IRE_TYPE); 11098 if (ire != NULL) { 11099 if (ire->ire_flags & RTF_MULTIRT) { 11100 error = ip_multirt_apply_membership_v6( 11101 optfn, ire, connp, checkonly, 11102 &v6grp, fmode, &v6src, first_mp); 11103 done = B_TRUE; 11104 } 11105 ire_refrele(ire); 11106 } 11107 if (!done) { 11108 error = optfn(connp, checkonly, &v6grp, 11109 ifindex, fmode, &v6src, first_mp); 11110 } 11111 if (error != 0) { 11112 /* 11113 * EINPROGRESS is a soft error, needs retry 11114 * so don't make *outlenp zero. 11115 */ 11116 if (error != EINPROGRESS) 11117 *outlenp = 0; 11118 return (error); 11119 } 11120 /* OK return - copy input buffer into output buffer */ 11121 if (invalp != outvalp) { 11122 bcopy(invalp, outvalp, inlen); 11123 } 11124 *outlenp = inlen; 11125 return (0); 11126 } 11127 case IPV6_UNICAST_HOPS: 11128 /* Recorded in transport above IP */ 11129 break; /* goto sizeof (int) option return */ 11130 case IPV6_UNSPEC_SRC: 11131 /* Allow sending with a zero source address */ 11132 if (!checkonly) { 11133 mutex_enter(&connp->conn_lock); 11134 connp->conn_unspec_src = *i1 ? 1 : 0; 11135 mutex_exit(&connp->conn_lock); 11136 } 11137 break; /* goto sizeof (int) option return */ 11138 case IPV6_RECVPKTINFO: 11139 if (!checkonly) { 11140 mutex_enter(&connp->conn_lock); 11141 connp->conn_ipv6_recvpktinfo = *i1 ? 1 : 0; 11142 mutex_exit(&connp->conn_lock); 11143 } 11144 break; /* goto sizeof (int) option return */ 11145 case IPV6_RECVTCLASS: 11146 if (!checkonly) { 11147 if (*i1 < 0 || *i1 > 1) { 11148 return (EINVAL); 11149 } 11150 mutex_enter(&connp->conn_lock); 11151 connp->conn_ipv6_recvtclass = *i1; 11152 mutex_exit(&connp->conn_lock); 11153 } 11154 break; 11155 case IPV6_RECVPATHMTU: 11156 if (!checkonly) { 11157 if (*i1 < 0 || *i1 > 1) { 11158 return (EINVAL); 11159 } 11160 mutex_enter(&connp->conn_lock); 11161 connp->conn_ipv6_recvpathmtu = *i1; 11162 mutex_exit(&connp->conn_lock); 11163 } 11164 break; 11165 case IPV6_RECVHOPLIMIT: 11166 if (!checkonly) { 11167 mutex_enter(&connp->conn_lock); 11168 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 11169 mutex_exit(&connp->conn_lock); 11170 } 11171 break; /* goto sizeof (int) option return */ 11172 case IPV6_RECVHOPOPTS: 11173 if (!checkonly) { 11174 mutex_enter(&connp->conn_lock); 11175 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 11176 mutex_exit(&connp->conn_lock); 11177 } 11178 break; /* goto sizeof (int) option return */ 11179 case IPV6_RECVDSTOPTS: 11180 if (!checkonly) { 11181 mutex_enter(&connp->conn_lock); 11182 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 11183 mutex_exit(&connp->conn_lock); 11184 } 11185 break; /* goto sizeof (int) option return */ 11186 case IPV6_RECVRTHDR: 11187 if (!checkonly) { 11188 mutex_enter(&connp->conn_lock); 11189 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 11190 mutex_exit(&connp->conn_lock); 11191 } 11192 break; /* goto sizeof (int) option return */ 11193 case IPV6_RECVRTHDRDSTOPTS: 11194 if (!checkonly) { 11195 mutex_enter(&connp->conn_lock); 11196 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 11197 mutex_exit(&connp->conn_lock); 11198 } 11199 break; /* goto sizeof (int) option return */ 11200 case IPV6_PKTINFO: 11201 if (inlen == 0) 11202 return (-EINVAL); /* clearing option */ 11203 error = ip6_set_pktinfo(cr, connp, 11204 (struct in6_pktinfo *)invalp, first_mp); 11205 if (error != 0) 11206 *outlenp = 0; 11207 else 11208 *outlenp = inlen; 11209 return (error); 11210 case IPV6_NEXTHOP: { 11211 struct sockaddr_in6 *sin6; 11212 11213 /* Verify that the nexthop is reachable */ 11214 if (inlen == 0) 11215 return (-EINVAL); /* clearing option */ 11216 11217 sin6 = (struct sockaddr_in6 *)invalp; 11218 ire = ire_route_lookup_v6(&sin6->sin6_addr, 11219 0, 0, 0, NULL, NULL, connp->conn_zoneid, 11220 NULL, MATCH_IRE_DEFAULT); 11221 11222 if (ire == NULL) { 11223 *outlenp = 0; 11224 return (EHOSTUNREACH); 11225 } 11226 ire_refrele(ire); 11227 return (-EINVAL); 11228 } 11229 case IPV6_SEC_OPT: 11230 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11231 if (error != 0) { 11232 *outlenp = 0; 11233 return (error); 11234 } 11235 break; 11236 case IPV6_SRC_PREFERENCES: { 11237 /* 11238 * This is implemented strictly in the ip module 11239 * (here and in tcp_opt_*() to accomodate tcp 11240 * sockets). Modules above ip pass this option 11241 * down here since ip is the only one that needs to 11242 * be aware of source address preferences. 11243 * 11244 * This socket option only affects connected 11245 * sockets that haven't already bound to a specific 11246 * IPv6 address. In other words, sockets that 11247 * don't call bind() with an address other than the 11248 * unspecified address and that call connect(). 11249 * ip_bind_connected_v6() passes these preferences 11250 * to the ipif_select_source_v6() function. 11251 */ 11252 if (inlen != sizeof (uint32_t)) 11253 return (EINVAL); 11254 error = ip6_set_src_preferences(connp, 11255 *(uint32_t *)invalp); 11256 if (error != 0) { 11257 *outlenp = 0; 11258 return (error); 11259 } else { 11260 *outlenp = sizeof (uint32_t); 11261 } 11262 break; 11263 } 11264 case IPV6_V6ONLY: 11265 if (*i1 < 0 || *i1 > 1) { 11266 return (EINVAL); 11267 } 11268 mutex_enter(&connp->conn_lock); 11269 connp->conn_ipv6_v6only = *i1; 11270 mutex_exit(&connp->conn_lock); 11271 break; 11272 default: 11273 return (-EINVAL); 11274 } 11275 break; 11276 default: 11277 /* 11278 * "soft" error (negative) 11279 * option not handled at this level 11280 * Note: Do not modify *outlenp 11281 */ 11282 return (-EINVAL); 11283 } 11284 /* 11285 * Common case of return from an option that is sizeof (int) 11286 */ 11287 *(int *)outvalp = *i1; 11288 *outlenp = sizeof (int); 11289 return (0); 11290 } 11291 11292 /* 11293 * This routine gets default values of certain options whose default 11294 * values are maintained by protocol specific code 11295 */ 11296 /* ARGSUSED */ 11297 int 11298 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 11299 { 11300 int *i1 = (int *)ptr; 11301 11302 switch (level) { 11303 case IPPROTO_IP: 11304 switch (name) { 11305 case IP_MULTICAST_TTL: 11306 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 11307 return (sizeof (uchar_t)); 11308 case IP_MULTICAST_LOOP: 11309 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 11310 return (sizeof (uchar_t)); 11311 default: 11312 return (-1); 11313 } 11314 case IPPROTO_IPV6: 11315 switch (name) { 11316 case IPV6_UNICAST_HOPS: 11317 *i1 = ipv6_def_hops; 11318 return (sizeof (int)); 11319 case IPV6_MULTICAST_HOPS: 11320 *i1 = IP_DEFAULT_MULTICAST_TTL; 11321 return (sizeof (int)); 11322 case IPV6_MULTICAST_LOOP: 11323 *i1 = IP_DEFAULT_MULTICAST_LOOP; 11324 return (sizeof (int)); 11325 case IPV6_V6ONLY: 11326 *i1 = 1; 11327 return (sizeof (int)); 11328 default: 11329 return (-1); 11330 } 11331 default: 11332 return (-1); 11333 } 11334 /* NOTREACHED */ 11335 } 11336 11337 /* 11338 * Given a destination address and a pointer to where to put the information 11339 * this routine fills in the mtuinfo. 11340 */ 11341 int 11342 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 11343 struct ip6_mtuinfo *mtuinfo) 11344 { 11345 ire_t *ire; 11346 11347 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 11348 return (-1); 11349 11350 bzero(mtuinfo, sizeof (*mtuinfo)); 11351 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11352 mtuinfo->ip6m_addr.sin6_port = port; 11353 mtuinfo->ip6m_addr.sin6_addr = *in6; 11354 11355 ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL); 11356 if (ire != NULL) { 11357 mtuinfo->ip6m_mtu = ire->ire_max_frag; 11358 ire_refrele(ire); 11359 } else { 11360 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 11361 } 11362 return (sizeof (struct ip6_mtuinfo)); 11363 } 11364 11365 /* 11366 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 11367 * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and 11368 * isn't. This doesn't matter as the error checking is done properly for the 11369 * other MRT options coming in through ip_opt_set. 11370 */ 11371 int 11372 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 11373 { 11374 conn_t *connp = Q_TO_CONN(q); 11375 ipsec_req_t *req = (ipsec_req_t *)ptr; 11376 11377 switch (level) { 11378 case IPPROTO_IP: 11379 switch (name) { 11380 case MRT_VERSION: 11381 case MRT_ASSERT: 11382 (void) ip_mrouter_get(name, q, ptr); 11383 return (sizeof (int)); 11384 case IP_SEC_OPT: 11385 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 11386 case IP_NEXTHOP: 11387 if (connp->conn_nexthop_set) { 11388 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 11389 return (sizeof (ipaddr_t)); 11390 } else 11391 return (0); 11392 default: 11393 break; 11394 } 11395 break; 11396 case IPPROTO_IPV6: 11397 switch (name) { 11398 case IPV6_SEC_OPT: 11399 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 11400 case IPV6_SRC_PREFERENCES: { 11401 return (ip6_get_src_preferences(connp, 11402 (uint32_t *)ptr)); 11403 } 11404 case IPV6_V6ONLY: 11405 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 11406 return (sizeof (int)); 11407 case IPV6_PATHMTU: 11408 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 11409 (struct ip6_mtuinfo *)ptr)); 11410 default: 11411 break; 11412 } 11413 break; 11414 default: 11415 break; 11416 } 11417 return (-1); 11418 } 11419 11420 /* Named Dispatch routine to get a current value out of our parameter table. */ 11421 /* ARGSUSED */ 11422 static int 11423 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11424 { 11425 ipparam_t *ippa = (ipparam_t *)cp; 11426 11427 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 11428 return (0); 11429 } 11430 11431 /* ARGSUSED */ 11432 static int 11433 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11434 { 11435 11436 (void) mi_mpprintf(mp, "%d", *(int *)cp); 11437 return (0); 11438 } 11439 11440 /* 11441 * Set ip{,6}_forwarding values. This means walking through all of the 11442 * ill's and toggling their forwarding values. 11443 */ 11444 /* ARGSUSED */ 11445 static int 11446 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11447 { 11448 long new_value; 11449 int *forwarding_value = (int *)cp; 11450 ill_t *walker; 11451 boolean_t isv6 = (forwarding_value == &ipv6_forward); 11452 ill_walk_context_t ctx; 11453 11454 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11455 new_value < 0 || new_value > 1) { 11456 return (EINVAL); 11457 } 11458 11459 *forwarding_value = new_value; 11460 11461 /* 11462 * Regardless of the current value of ip_forwarding, set all per-ill 11463 * values of ip_forwarding to the value being set. 11464 * 11465 * Bring all the ill's up to date with the new global value. 11466 */ 11467 rw_enter(&ill_g_lock, RW_READER); 11468 11469 if (isv6) 11470 walker = ILL_START_WALK_V6(&ctx); 11471 else 11472 walker = ILL_START_WALK_V4(&ctx); 11473 for (; walker != NULL; walker = ill_next(&ctx, walker)) { 11474 (void) ill_forward_set(q, mp, (new_value != 0), 11475 (caddr_t)walker); 11476 } 11477 rw_exit(&ill_g_lock); 11478 11479 return (0); 11480 } 11481 11482 /* 11483 * Walk through the param array specified registering each element with the 11484 * Named Dispatch handler. This is called only during init. So it is ok 11485 * not to acquire any locks 11486 */ 11487 static boolean_t 11488 ip_param_register(ipparam_t *ippa, size_t ippa_cnt, 11489 ipndp_t *ipnd, size_t ipnd_cnt) 11490 { 11491 for (; ippa_cnt-- > 0; ippa++) { 11492 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 11493 if (!nd_load(&ip_g_nd, ippa->ip_param_name, 11494 ip_param_get, ip_param_set, (caddr_t)ippa)) { 11495 nd_free(&ip_g_nd); 11496 return (B_FALSE); 11497 } 11498 } 11499 } 11500 11501 for (; ipnd_cnt-- > 0; ipnd++) { 11502 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 11503 if (!nd_load(&ip_g_nd, ipnd->ip_ndp_name, 11504 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 11505 ipnd->ip_ndp_data)) { 11506 nd_free(&ip_g_nd); 11507 return (B_FALSE); 11508 } 11509 } 11510 } 11511 11512 return (B_TRUE); 11513 } 11514 11515 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 11516 /* ARGSUSED */ 11517 static int 11518 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11519 { 11520 long new_value; 11521 ipparam_t *ippa = (ipparam_t *)cp; 11522 11523 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11524 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 11525 return (EINVAL); 11526 } 11527 ippa->ip_param_value = new_value; 11528 return (0); 11529 } 11530 11531 /* 11532 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 11533 * When an ipf is passed here for the first time, if 11534 * we already have in-order fragments on the queue, we convert from the fast- 11535 * path reassembly scheme to the hard-case scheme. From then on, additional 11536 * fragments are reassembled here. We keep track of the start and end offsets 11537 * of each piece, and the number of holes in the chain. When the hole count 11538 * goes to zero, we are done! 11539 * 11540 * The ipf_count will be updated to account for any mblk(s) added (pointed to 11541 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 11542 * ipfb_count and ill_frag_count by the difference of ipf_count before and 11543 * after the call to ip_reassemble(). 11544 */ 11545 int 11546 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 11547 size_t msg_len) 11548 { 11549 uint_t end; 11550 mblk_t *next_mp; 11551 mblk_t *mp1; 11552 uint_t offset; 11553 boolean_t incr_dups = B_TRUE; 11554 boolean_t offset_zero_seen = B_FALSE; 11555 boolean_t pkt_boundary_checked = B_FALSE; 11556 11557 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 11558 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 11559 11560 /* Add in byte count */ 11561 ipf->ipf_count += msg_len; 11562 if (ipf->ipf_end) { 11563 /* 11564 * We were part way through in-order reassembly, but now there 11565 * is a hole. We walk through messages already queued, and 11566 * mark them for hard case reassembly. We know that up till 11567 * now they were in order starting from offset zero. 11568 */ 11569 offset = 0; 11570 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 11571 IP_REASS_SET_START(mp1, offset); 11572 if (offset == 0) { 11573 ASSERT(ipf->ipf_nf_hdr_len != 0); 11574 offset = -ipf->ipf_nf_hdr_len; 11575 } 11576 offset += mp1->b_wptr - mp1->b_rptr; 11577 IP_REASS_SET_END(mp1, offset); 11578 } 11579 /* One hole at the end. */ 11580 ipf->ipf_hole_cnt = 1; 11581 /* Brand it as a hard case, forever. */ 11582 ipf->ipf_end = 0; 11583 } 11584 /* Walk through all the new pieces. */ 11585 do { 11586 end = start + (mp->b_wptr - mp->b_rptr); 11587 /* 11588 * If start is 0, decrease 'end' only for the first mblk of 11589 * the fragment. Otherwise 'end' can get wrong value in the 11590 * second pass of the loop if first mblk is exactly the 11591 * size of ipf_nf_hdr_len. 11592 */ 11593 if (start == 0 && !offset_zero_seen) { 11594 /* First segment */ 11595 ASSERT(ipf->ipf_nf_hdr_len != 0); 11596 end -= ipf->ipf_nf_hdr_len; 11597 offset_zero_seen = B_TRUE; 11598 } 11599 next_mp = mp->b_cont; 11600 /* 11601 * We are checking to see if there is any interesing data 11602 * to process. If there isn't and the mblk isn't the 11603 * one which carries the unfragmentable header then we 11604 * drop it. It's possible to have just the unfragmentable 11605 * header come through without any data. That needs to be 11606 * saved. 11607 * 11608 * If the assert at the top of this function holds then the 11609 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 11610 * is infrequently traveled enough that the test is left in 11611 * to protect against future code changes which break that 11612 * invariant. 11613 */ 11614 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 11615 /* Empty. Blast it. */ 11616 IP_REASS_SET_START(mp, 0); 11617 IP_REASS_SET_END(mp, 0); 11618 /* 11619 * If the ipf points to the mblk we are about to free, 11620 * update ipf to point to the next mblk (or NULL 11621 * if none). 11622 */ 11623 if (ipf->ipf_mp->b_cont == mp) 11624 ipf->ipf_mp->b_cont = next_mp; 11625 freeb(mp); 11626 continue; 11627 } 11628 mp->b_cont = NULL; 11629 IP_REASS_SET_START(mp, start); 11630 IP_REASS_SET_END(mp, end); 11631 if (!ipf->ipf_tail_mp) { 11632 ipf->ipf_tail_mp = mp; 11633 ipf->ipf_mp->b_cont = mp; 11634 if (start == 0 || !more) { 11635 ipf->ipf_hole_cnt = 1; 11636 /* 11637 * if the first fragment comes in more than one 11638 * mblk, this loop will be executed for each 11639 * mblk. Need to adjust hole count so exiting 11640 * this routine will leave hole count at 1. 11641 */ 11642 if (next_mp) 11643 ipf->ipf_hole_cnt++; 11644 } else 11645 ipf->ipf_hole_cnt = 2; 11646 continue; 11647 } else if (ipf->ipf_last_frag_seen && !more && 11648 !pkt_boundary_checked) { 11649 /* 11650 * We check datagram boundary only if this fragment 11651 * claims to be the last fragment and we have seen a 11652 * last fragment in the past too. We do this only 11653 * once for a given fragment. 11654 * 11655 * start cannot be 0 here as fragments with start=0 11656 * and MF=0 gets handled as a complete packet. These 11657 * fragments should not reach here. 11658 */ 11659 11660 if (start + msgdsize(mp) != 11661 IP_REASS_END(ipf->ipf_tail_mp)) { 11662 /* 11663 * We have two fragments both of which claim 11664 * to be the last fragment but gives conflicting 11665 * information about the whole datagram size. 11666 * Something fishy is going on. Drop the 11667 * fragment and free up the reassembly list. 11668 */ 11669 return (IP_REASS_FAILED); 11670 } 11671 11672 /* 11673 * We shouldn't come to this code block again for this 11674 * particular fragment. 11675 */ 11676 pkt_boundary_checked = B_TRUE; 11677 } 11678 11679 /* New stuff at or beyond tail? */ 11680 offset = IP_REASS_END(ipf->ipf_tail_mp); 11681 if (start >= offset) { 11682 if (ipf->ipf_last_frag_seen) { 11683 /* current fragment is beyond last fragment */ 11684 return (IP_REASS_FAILED); 11685 } 11686 /* Link it on end. */ 11687 ipf->ipf_tail_mp->b_cont = mp; 11688 ipf->ipf_tail_mp = mp; 11689 if (more) { 11690 if (start != offset) 11691 ipf->ipf_hole_cnt++; 11692 } else if (start == offset && next_mp == NULL) 11693 ipf->ipf_hole_cnt--; 11694 continue; 11695 } 11696 mp1 = ipf->ipf_mp->b_cont; 11697 offset = IP_REASS_START(mp1); 11698 /* New stuff at the front? */ 11699 if (start < offset) { 11700 if (start == 0) { 11701 if (end >= offset) { 11702 /* Nailed the hole at the begining. */ 11703 ipf->ipf_hole_cnt--; 11704 } 11705 } else if (end < offset) { 11706 /* 11707 * A hole, stuff, and a hole where there used 11708 * to be just a hole. 11709 */ 11710 ipf->ipf_hole_cnt++; 11711 } 11712 mp->b_cont = mp1; 11713 /* Check for overlap. */ 11714 while (end > offset) { 11715 if (end < IP_REASS_END(mp1)) { 11716 mp->b_wptr -= end - offset; 11717 IP_REASS_SET_END(mp, offset); 11718 if (ill->ill_isv6) { 11719 BUMP_MIB(ill->ill_ip6_mib, 11720 ipv6ReasmPartDups); 11721 } else { 11722 BUMP_MIB(&ip_mib, 11723 ipReasmPartDups); 11724 } 11725 break; 11726 } 11727 /* Did we cover another hole? */ 11728 if ((mp1->b_cont && 11729 IP_REASS_END(mp1) != 11730 IP_REASS_START(mp1->b_cont) && 11731 end >= IP_REASS_START(mp1->b_cont)) || 11732 (!ipf->ipf_last_frag_seen && !more)) { 11733 ipf->ipf_hole_cnt--; 11734 } 11735 /* Clip out mp1. */ 11736 if ((mp->b_cont = mp1->b_cont) == NULL) { 11737 /* 11738 * After clipping out mp1, this guy 11739 * is now hanging off the end. 11740 */ 11741 ipf->ipf_tail_mp = mp; 11742 } 11743 IP_REASS_SET_START(mp1, 0); 11744 IP_REASS_SET_END(mp1, 0); 11745 /* Subtract byte count */ 11746 ipf->ipf_count -= mp1->b_datap->db_lim - 11747 mp1->b_datap->db_base; 11748 freeb(mp1); 11749 if (ill->ill_isv6) { 11750 BUMP_MIB(ill->ill_ip6_mib, 11751 ipv6ReasmPartDups); 11752 } else { 11753 BUMP_MIB(&ip_mib, ipReasmPartDups); 11754 } 11755 mp1 = mp->b_cont; 11756 if (!mp1) 11757 break; 11758 offset = IP_REASS_START(mp1); 11759 } 11760 ipf->ipf_mp->b_cont = mp; 11761 continue; 11762 } 11763 /* 11764 * The new piece starts somewhere between the start of the head 11765 * and before the end of the tail. 11766 */ 11767 for (; mp1; mp1 = mp1->b_cont) { 11768 offset = IP_REASS_END(mp1); 11769 if (start < offset) { 11770 if (end <= offset) { 11771 /* Nothing new. */ 11772 IP_REASS_SET_START(mp, 0); 11773 IP_REASS_SET_END(mp, 0); 11774 /* Subtract byte count */ 11775 ipf->ipf_count -= mp->b_datap->db_lim - 11776 mp->b_datap->db_base; 11777 if (incr_dups) { 11778 ipf->ipf_num_dups++; 11779 incr_dups = B_FALSE; 11780 } 11781 freeb(mp); 11782 if (ill->ill_isv6) { 11783 BUMP_MIB(ill->ill_ip6_mib, 11784 ipv6ReasmDuplicates); 11785 } else { 11786 BUMP_MIB(&ip_mib, 11787 ipReasmDuplicates); 11788 } 11789 break; 11790 } 11791 /* 11792 * Trim redundant stuff off beginning of new 11793 * piece. 11794 */ 11795 IP_REASS_SET_START(mp, offset); 11796 mp->b_rptr += offset - start; 11797 if (ill->ill_isv6) { 11798 BUMP_MIB(ill->ill_ip6_mib, 11799 ipv6ReasmPartDups); 11800 } else { 11801 BUMP_MIB(&ip_mib, ipReasmPartDups); 11802 } 11803 start = offset; 11804 if (!mp1->b_cont) { 11805 /* 11806 * After trimming, this guy is now 11807 * hanging off the end. 11808 */ 11809 mp1->b_cont = mp; 11810 ipf->ipf_tail_mp = mp; 11811 if (!more) { 11812 ipf->ipf_hole_cnt--; 11813 } 11814 break; 11815 } 11816 } 11817 if (start >= IP_REASS_START(mp1->b_cont)) 11818 continue; 11819 /* Fill a hole */ 11820 if (start > offset) 11821 ipf->ipf_hole_cnt++; 11822 mp->b_cont = mp1->b_cont; 11823 mp1->b_cont = mp; 11824 mp1 = mp->b_cont; 11825 offset = IP_REASS_START(mp1); 11826 if (end >= offset) { 11827 ipf->ipf_hole_cnt--; 11828 /* Check for overlap. */ 11829 while (end > offset) { 11830 if (end < IP_REASS_END(mp1)) { 11831 mp->b_wptr -= end - offset; 11832 IP_REASS_SET_END(mp, offset); 11833 /* 11834 * TODO we might bump 11835 * this up twice if there is 11836 * overlap at both ends. 11837 */ 11838 if (ill->ill_isv6) { 11839 BUMP_MIB( 11840 ill->ill_ip6_mib, 11841 ipv6ReasmPartDups); 11842 } else { 11843 BUMP_MIB(&ip_mib, 11844 ipReasmPartDups); 11845 } 11846 break; 11847 } 11848 /* Did we cover another hole? */ 11849 if ((mp1->b_cont && 11850 IP_REASS_END(mp1) 11851 != IP_REASS_START(mp1->b_cont) && 11852 end >= 11853 IP_REASS_START(mp1->b_cont)) || 11854 (!ipf->ipf_last_frag_seen && 11855 !more)) { 11856 ipf->ipf_hole_cnt--; 11857 } 11858 /* Clip out mp1. */ 11859 if ((mp->b_cont = mp1->b_cont) == 11860 NULL) { 11861 /* 11862 * After clipping out mp1, 11863 * this guy is now hanging 11864 * off the end. 11865 */ 11866 ipf->ipf_tail_mp = mp; 11867 } 11868 IP_REASS_SET_START(mp1, 0); 11869 IP_REASS_SET_END(mp1, 0); 11870 /* Subtract byte count */ 11871 ipf->ipf_count -= 11872 mp1->b_datap->db_lim - 11873 mp1->b_datap->db_base; 11874 freeb(mp1); 11875 if (ill->ill_isv6) { 11876 BUMP_MIB(ill->ill_ip6_mib, 11877 ipv6ReasmPartDups); 11878 } else { 11879 BUMP_MIB(&ip_mib, 11880 ipReasmPartDups); 11881 } 11882 mp1 = mp->b_cont; 11883 if (!mp1) 11884 break; 11885 offset = IP_REASS_START(mp1); 11886 } 11887 } 11888 break; 11889 } 11890 } while (start = end, mp = next_mp); 11891 11892 /* Fragment just processed could be the last one. Remember this fact */ 11893 if (!more) 11894 ipf->ipf_last_frag_seen = B_TRUE; 11895 11896 /* Still got holes? */ 11897 if (ipf->ipf_hole_cnt) 11898 return (IP_REASS_PARTIAL); 11899 /* Clean up overloaded fields to avoid upstream disasters. */ 11900 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 11901 IP_REASS_SET_START(mp1, 0); 11902 IP_REASS_SET_END(mp1, 0); 11903 } 11904 return (IP_REASS_COMPLETE); 11905 } 11906 11907 /* 11908 * ipsec processing for the fast path, used for input UDP Packets 11909 */ 11910 static boolean_t 11911 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 11912 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present) 11913 { 11914 uint32_t ill_index; 11915 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 11916 11917 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 11918 /* The ill_index of the incoming ILL */ 11919 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 11920 11921 /* pass packet up to the transport */ 11922 if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) { 11923 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 11924 NULL, mctl_present); 11925 if (*first_mpp == NULL) { 11926 return (B_FALSE); 11927 } 11928 } 11929 11930 /* Initiate IPPF processing for fastpath UDP */ 11931 if (IPP_ENABLED(IPP_LOCAL_IN)) { 11932 ip_process(IPP_LOCAL_IN, mpp, ill_index); 11933 if (*mpp == NULL) { 11934 ip2dbg(("ip_input_ipsec_process: UDP pkt " 11935 "deferred/dropped during IPPF processing\n")); 11936 return (B_FALSE); 11937 } 11938 } 11939 /* 11940 * We make the checks as below since we are in the fast path 11941 * and want to minimize the number of checks if the IP_RECVIF and/or 11942 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 11943 */ 11944 if (connp->conn_recvif || connp->conn_recvslla || 11945 connp->conn_ipv6_recvpktinfo) { 11946 if (connp->conn_recvif || 11947 connp->conn_ipv6_recvpktinfo) { 11948 in_flags = IPF_RECVIF; 11949 } 11950 if (connp->conn_recvslla) { 11951 in_flags |= IPF_RECVSLLA; 11952 } 11953 /* 11954 * since in_flags are being set ill will be 11955 * referenced in ip_add_info, so it better not 11956 * be NULL. 11957 */ 11958 /* 11959 * the actual data will be contained in b_cont 11960 * upon successful return of the following call. 11961 * If the call fails then the original mblk is 11962 * returned. 11963 */ 11964 *mpp = ip_add_info(*mpp, ill, in_flags); 11965 } 11966 11967 return (B_TRUE); 11968 } 11969 11970 /* 11971 * Fragmentation reassembly. Each ILL has a hash table for 11972 * queuing packets undergoing reassembly for all IPIFs 11973 * associated with the ILL. The hash is based on the packet 11974 * IP ident field. The ILL frag hash table was allocated 11975 * as a timer block at the time the ILL was created. Whenever 11976 * there is anything on the reassembly queue, the timer will 11977 * be running. Returns B_TRUE if successful else B_FALSE; 11978 * frees mp on failure. 11979 */ 11980 static boolean_t 11981 ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, 11982 uint32_t *cksum_val, uint16_t *cksum_flags) 11983 { 11984 uint32_t frag_offset_flags; 11985 ill_t *ill = (ill_t *)q->q_ptr; 11986 mblk_t *mp = *mpp; 11987 mblk_t *t_mp; 11988 ipaddr_t dst; 11989 uint8_t proto = ipha->ipha_protocol; 11990 uint32_t sum_val; 11991 uint16_t sum_flags; 11992 ipf_t *ipf; 11993 ipf_t **ipfp; 11994 ipfb_t *ipfb; 11995 uint16_t ident; 11996 uint32_t offset; 11997 ipaddr_t src; 11998 uint_t hdr_length; 11999 uint32_t end; 12000 mblk_t *mp1; 12001 mblk_t *tail_mp; 12002 size_t count; 12003 size_t msg_len; 12004 uint8_t ecn_info = 0; 12005 uint32_t packet_size; 12006 boolean_t pruned = B_FALSE; 12007 12008 if (cksum_val != NULL) 12009 *cksum_val = 0; 12010 if (cksum_flags != NULL) 12011 *cksum_flags = 0; 12012 12013 /* 12014 * Drop the fragmented as early as possible, if 12015 * we don't have resource(s) to re-assemble. 12016 */ 12017 if (ip_reass_queue_bytes == 0) { 12018 freemsg(mp); 12019 return (B_FALSE); 12020 } 12021 12022 /* Check for fragmentation offset; return if there's none */ 12023 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 12024 (IPH_MF | IPH_OFFSET)) == 0) 12025 return (B_TRUE); 12026 12027 /* 12028 * We utilize hardware computed checksum info only for UDP since 12029 * IP fragmentation is a normal occurence for the protocol. In 12030 * addition, checksum offload support for IP fragments carrying 12031 * UDP payload is commonly implemented across network adapters. 12032 */ 12033 ASSERT(ill != NULL); 12034 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 12035 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 12036 mblk_t *mp1 = mp->b_cont; 12037 int32_t len; 12038 12039 /* Record checksum information from the packet */ 12040 sum_val = (uint32_t)DB_CKSUM16(mp); 12041 sum_flags = DB_CKSUMFLAGS(mp); 12042 12043 /* IP payload offset from beginning of mblk */ 12044 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 12045 12046 if ((sum_flags & HCK_PARTIALCKSUM) && 12047 (mp1 == NULL || mp1->b_cont == NULL) && 12048 offset >= DB_CKSUMSTART(mp) && 12049 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 12050 uint32_t adj; 12051 /* 12052 * Partial checksum has been calculated by hardware 12053 * and attached to the packet; in addition, any 12054 * prepended extraneous data is even byte aligned. 12055 * If any such data exists, we adjust the checksum; 12056 * this would also handle any postpended data. 12057 */ 12058 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 12059 mp, mp1, len, adj); 12060 12061 /* One's complement subtract extraneous checksum */ 12062 if (adj >= sum_val) 12063 sum_val = ~(adj - sum_val) & 0xFFFF; 12064 else 12065 sum_val -= adj; 12066 } 12067 } else { 12068 sum_val = 0; 12069 sum_flags = 0; 12070 } 12071 12072 /* Clear hardware checksumming flag */ 12073 DB_CKSUMFLAGS(mp) = 0; 12074 12075 ident = ipha->ipha_ident; 12076 offset = (frag_offset_flags << 3) & 0xFFFF; 12077 src = ipha->ipha_src; 12078 dst = ipha->ipha_dst; 12079 hdr_length = IPH_HDR_LENGTH(ipha); 12080 end = ntohs(ipha->ipha_length) - hdr_length; 12081 12082 /* If end == 0 then we have a packet with no data, so just free it */ 12083 if (end == 0) { 12084 freemsg(mp); 12085 return (B_FALSE); 12086 } 12087 12088 /* Record the ECN field info. */ 12089 ecn_info = (ipha->ipha_type_of_service & 0x3); 12090 if (offset != 0) { 12091 /* 12092 * If this isn't the first piece, strip the header, and 12093 * add the offset to the end value. 12094 */ 12095 mp->b_rptr += hdr_length; 12096 end += offset; 12097 } 12098 12099 msg_len = MBLKSIZE(mp); 12100 tail_mp = mp; 12101 while (tail_mp->b_cont != NULL) { 12102 tail_mp = tail_mp->b_cont; 12103 msg_len += MBLKSIZE(tail_mp); 12104 } 12105 12106 /* If the reassembly list for this ILL will get too big, prune it */ 12107 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 12108 ip_reass_queue_bytes) { 12109 ill_frag_prune(ill, 12110 (ip_reass_queue_bytes < msg_len) ? 0 : 12111 (ip_reass_queue_bytes - msg_len)); 12112 pruned = B_TRUE; 12113 } 12114 12115 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 12116 mutex_enter(&ipfb->ipfb_lock); 12117 12118 ipfp = &ipfb->ipfb_ipf; 12119 /* Try to find an existing fragment queue for this packet. */ 12120 for (;;) { 12121 ipf = ipfp[0]; 12122 if (ipf != NULL) { 12123 /* 12124 * It has to match on ident and src/dst address. 12125 */ 12126 if (ipf->ipf_ident == ident && 12127 ipf->ipf_src == src && 12128 ipf->ipf_dst == dst && 12129 ipf->ipf_protocol == proto) { 12130 /* 12131 * If we have received too many 12132 * duplicate fragments for this packet 12133 * free it. 12134 */ 12135 if (ipf->ipf_num_dups > ip_max_frag_dups) { 12136 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12137 freemsg(mp); 12138 mutex_exit(&ipfb->ipfb_lock); 12139 return (B_FALSE); 12140 } 12141 /* Found it. */ 12142 break; 12143 } 12144 ipfp = &ipf->ipf_hash_next; 12145 continue; 12146 } 12147 12148 /* 12149 * If we pruned the list, do we want to store this new 12150 * fragment?. We apply an optimization here based on the 12151 * fact that most fragments will be received in order. 12152 * So if the offset of this incoming fragment is zero, 12153 * it is the first fragment of a new packet. We will 12154 * keep it. Otherwise drop the fragment, as we have 12155 * probably pruned the packet already (since the 12156 * packet cannot be found). 12157 */ 12158 if (pruned && offset != 0) { 12159 mutex_exit(&ipfb->ipfb_lock); 12160 freemsg(mp); 12161 return (B_FALSE); 12162 } 12163 12164 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS) { 12165 /* 12166 * Too many fragmented packets in this hash 12167 * bucket. Free the oldest. 12168 */ 12169 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 12170 } 12171 12172 /* New guy. Allocate a frag message. */ 12173 mp1 = allocb(sizeof (*ipf), BPRI_MED); 12174 if (mp1 == NULL) { 12175 BUMP_MIB(&ip_mib, ipInDiscards); 12176 freemsg(mp); 12177 reass_done: 12178 mutex_exit(&ipfb->ipfb_lock); 12179 return (B_FALSE); 12180 } 12181 12182 12183 BUMP_MIB(&ip_mib, ipReasmReqds); 12184 mp1->b_cont = mp; 12185 12186 /* Initialize the fragment header. */ 12187 ipf = (ipf_t *)mp1->b_rptr; 12188 ipf->ipf_mp = mp1; 12189 ipf->ipf_ptphn = ipfp; 12190 ipfp[0] = ipf; 12191 ipf->ipf_hash_next = NULL; 12192 ipf->ipf_ident = ident; 12193 ipf->ipf_protocol = proto; 12194 ipf->ipf_src = src; 12195 ipf->ipf_dst = dst; 12196 ipf->ipf_nf_hdr_len = 0; 12197 /* Record reassembly start time. */ 12198 ipf->ipf_timestamp = gethrestime_sec(); 12199 /* Record ipf generation and account for frag header */ 12200 ipf->ipf_gen = ill->ill_ipf_gen++; 12201 ipf->ipf_count = MBLKSIZE(mp1); 12202 ipf->ipf_last_frag_seen = B_FALSE; 12203 ipf->ipf_ecn = ecn_info; 12204 ipf->ipf_num_dups = 0; 12205 ipfb->ipfb_frag_pkts++; 12206 ipf->ipf_checksum = 0; 12207 ipf->ipf_checksum_flags = 0; 12208 12209 /* Store checksum value in fragment header */ 12210 if (sum_flags != 0) { 12211 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12212 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12213 ipf->ipf_checksum = sum_val; 12214 ipf->ipf_checksum_flags = sum_flags; 12215 } 12216 12217 /* 12218 * We handle reassembly two ways. In the easy case, 12219 * where all the fragments show up in order, we do 12220 * minimal bookkeeping, and just clip new pieces on 12221 * the end. If we ever see a hole, then we go off 12222 * to ip_reassemble which has to mark the pieces and 12223 * keep track of the number of holes, etc. Obviously, 12224 * the point of having both mechanisms is so we can 12225 * handle the easy case as efficiently as possible. 12226 */ 12227 if (offset == 0) { 12228 /* Easy case, in-order reassembly so far. */ 12229 ipf->ipf_count += msg_len; 12230 ipf->ipf_tail_mp = tail_mp; 12231 /* 12232 * Keep track of next expected offset in 12233 * ipf_end. 12234 */ 12235 ipf->ipf_end = end; 12236 ipf->ipf_nf_hdr_len = hdr_length; 12237 } else { 12238 /* Hard case, hole at the beginning. */ 12239 ipf->ipf_tail_mp = NULL; 12240 /* 12241 * ipf_end == 0 means that we have given up 12242 * on easy reassembly. 12243 */ 12244 ipf->ipf_end = 0; 12245 12246 /* Forget checksum offload from now on */ 12247 ipf->ipf_checksum_flags = 0; 12248 12249 /* 12250 * ipf_hole_cnt is set by ip_reassemble. 12251 * ipf_count is updated by ip_reassemble. 12252 * No need to check for return value here 12253 * as we don't expect reassembly to complete 12254 * or fail for the first fragment itself. 12255 */ 12256 (void) ip_reassemble(mp, ipf, 12257 (frag_offset_flags & IPH_OFFSET) << 3, 12258 (frag_offset_flags & IPH_MF), ill, msg_len); 12259 } 12260 /* Update per ipfb and ill byte counts */ 12261 ipfb->ipfb_count += ipf->ipf_count; 12262 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12263 ill->ill_frag_count += ipf->ipf_count; 12264 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12265 /* If the frag timer wasn't already going, start it. */ 12266 mutex_enter(&ill->ill_lock); 12267 ill_frag_timer_start(ill); 12268 mutex_exit(&ill->ill_lock); 12269 goto reass_done; 12270 } 12271 12272 /* 12273 * If the packet's flag has changed (it could be coming up 12274 * from an interface different than the previous, therefore 12275 * possibly different checksum capability), then forget about 12276 * any stored checksum states. Otherwise add the value to 12277 * the existing one stored in the fragment header. 12278 */ 12279 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 12280 sum_val += ipf->ipf_checksum; 12281 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12282 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12283 ipf->ipf_checksum = sum_val; 12284 } else if (ipf->ipf_checksum_flags != 0) { 12285 /* Forget checksum offload from now on */ 12286 ipf->ipf_checksum_flags = 0; 12287 } 12288 12289 /* 12290 * We have a new piece of a datagram which is already being 12291 * reassembled. Update the ECN info if all IP fragments 12292 * are ECN capable. If there is one which is not, clear 12293 * all the info. If there is at least one which has CE 12294 * code point, IP needs to report that up to transport. 12295 */ 12296 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 12297 if (ecn_info == IPH_ECN_CE) 12298 ipf->ipf_ecn = IPH_ECN_CE; 12299 } else { 12300 ipf->ipf_ecn = IPH_ECN_NECT; 12301 } 12302 if (offset && ipf->ipf_end == offset) { 12303 /* The new fragment fits at the end */ 12304 ipf->ipf_tail_mp->b_cont = mp; 12305 /* Update the byte count */ 12306 ipf->ipf_count += msg_len; 12307 /* Update per ipfb and ill byte counts */ 12308 ipfb->ipfb_count += msg_len; 12309 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12310 ill->ill_frag_count += msg_len; 12311 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12312 if (frag_offset_flags & IPH_MF) { 12313 /* More to come. */ 12314 ipf->ipf_end = end; 12315 ipf->ipf_tail_mp = tail_mp; 12316 goto reass_done; 12317 } 12318 } else { 12319 /* Go do the hard cases. */ 12320 int ret; 12321 12322 if (offset == 0) 12323 ipf->ipf_nf_hdr_len = hdr_length; 12324 12325 /* Save current byte count */ 12326 count = ipf->ipf_count; 12327 ret = ip_reassemble(mp, ipf, 12328 (frag_offset_flags & IPH_OFFSET) << 3, 12329 (frag_offset_flags & IPH_MF), ill, msg_len); 12330 /* Count of bytes added and subtracted (freeb()ed) */ 12331 count = ipf->ipf_count - count; 12332 if (count) { 12333 /* Update per ipfb and ill byte counts */ 12334 ipfb->ipfb_count += count; 12335 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12336 ill->ill_frag_count += count; 12337 ASSERT(ill->ill_frag_count > 0); 12338 } 12339 if (ret == IP_REASS_PARTIAL) { 12340 goto reass_done; 12341 } else if (ret == IP_REASS_FAILED) { 12342 /* Reassembly failed. Free up all resources */ 12343 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12344 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 12345 IP_REASS_SET_START(t_mp, 0); 12346 IP_REASS_SET_END(t_mp, 0); 12347 } 12348 freemsg(mp); 12349 goto reass_done; 12350 } 12351 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 12352 } 12353 /* 12354 * We have completed reassembly. Unhook the frag header from 12355 * the reassembly list. 12356 * 12357 * Before we free the frag header, record the ECN info 12358 * to report back to the transport. 12359 */ 12360 ecn_info = ipf->ipf_ecn; 12361 BUMP_MIB(&ip_mib, ipReasmOKs); 12362 ipfp = ipf->ipf_ptphn; 12363 12364 /* We need to supply these to caller */ 12365 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 12366 sum_val = ipf->ipf_checksum; 12367 else 12368 sum_val = 0; 12369 12370 mp1 = ipf->ipf_mp; 12371 count = ipf->ipf_count; 12372 ipf = ipf->ipf_hash_next; 12373 if (ipf != NULL) 12374 ipf->ipf_ptphn = ipfp; 12375 ipfp[0] = ipf; 12376 ill->ill_frag_count -= count; 12377 ASSERT(ipfb->ipfb_count >= count); 12378 ipfb->ipfb_count -= count; 12379 ipfb->ipfb_frag_pkts--; 12380 mutex_exit(&ipfb->ipfb_lock); 12381 /* Ditch the frag header. */ 12382 mp = mp1->b_cont; 12383 12384 freeb(mp1); 12385 12386 /* Restore original IP length in header. */ 12387 packet_size = (uint32_t)msgdsize(mp); 12388 if (packet_size > IP_MAXPACKET) { 12389 freemsg(mp); 12390 BUMP_MIB(&ip_mib, ipInHdrErrors); 12391 return (B_FALSE); 12392 } 12393 12394 if (DB_REF(mp) > 1) { 12395 mblk_t *mp2 = copymsg(mp); 12396 12397 freemsg(mp); 12398 if (mp2 == NULL) { 12399 BUMP_MIB(&ip_mib, ipInDiscards); 12400 return (B_FALSE); 12401 } 12402 mp = mp2; 12403 } 12404 ipha = (ipha_t *)mp->b_rptr; 12405 12406 ipha->ipha_length = htons((uint16_t)packet_size); 12407 /* We're now complete, zip the frag state */ 12408 ipha->ipha_fragment_offset_and_flags = 0; 12409 /* Record the ECN info. */ 12410 ipha->ipha_type_of_service &= 0xFC; 12411 ipha->ipha_type_of_service |= ecn_info; 12412 *mpp = mp; 12413 12414 /* Reassembly is successful; return checksum information if needed */ 12415 if (cksum_val != NULL) 12416 *cksum_val = sum_val; 12417 if (cksum_flags != NULL) 12418 *cksum_flags = sum_flags; 12419 12420 return (B_TRUE); 12421 } 12422 12423 /* 12424 * Perform ip header check sum update local options. 12425 * return B_TRUE if all is well, else return B_FALSE and release 12426 * the mp. caller is responsible for decrementing ire ref cnt. 12427 */ 12428 static boolean_t 12429 ip_options_cksum(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire) 12430 { 12431 mblk_t *first_mp; 12432 boolean_t mctl_present; 12433 uint16_t sum; 12434 12435 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12436 /* 12437 * Don't do the checksum if it has gone through AH/ESP 12438 * processing. 12439 */ 12440 if (!mctl_present) { 12441 sum = ip_csum_hdr(ipha); 12442 if (sum != 0) { 12443 BUMP_MIB(&ip_mib, ipInCksumErrs); 12444 freemsg(first_mp); 12445 return (B_FALSE); 12446 } 12447 } 12448 12449 if (!ip_rput_local_options(q, mp, ipha, ire)) { 12450 if (mctl_present) 12451 freeb(first_mp); 12452 return (B_FALSE); 12453 } 12454 12455 return (B_TRUE); 12456 } 12457 12458 /* 12459 * All udp packet are delivered to the local host via this routine. 12460 */ 12461 void 12462 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12463 ill_t *recv_ill) 12464 { 12465 uint32_t sum; 12466 uint32_t u1; 12467 boolean_t mctl_present; 12468 conn_t *connp; 12469 mblk_t *first_mp; 12470 uint16_t *up; 12471 ill_t *ill = (ill_t *)q->q_ptr; 12472 uint16_t reass_hck_flags = 0; 12473 12474 #define rptr ((uchar_t *)ipha) 12475 12476 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12477 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 12478 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12479 12480 /* 12481 * FAST PATH for udp packets 12482 */ 12483 12484 /* u1 is # words of IP options */ 12485 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 12486 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12487 12488 /* IP options present */ 12489 if (u1 != 0) 12490 goto ipoptions; 12491 12492 /* Check the IP header checksum. */ 12493 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12494 /* Clear the IP header h/w cksum flag */ 12495 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12496 } else { 12497 #define uph ((uint16_t *)ipha) 12498 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 12499 uph[6] + uph[7] + uph[8] + uph[9]; 12500 #undef uph 12501 /* finish doing IP checksum */ 12502 sum = (sum & 0xFFFF) + (sum >> 16); 12503 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12504 /* 12505 * Don't verify header checksum if this packet is coming 12506 * back from AH/ESP as we already did it. 12507 */ 12508 if (!mctl_present && sum != 0 && sum != 0xFFFF) { 12509 BUMP_MIB(&ip_mib, ipInCksumErrs); 12510 freemsg(first_mp); 12511 return; 12512 } 12513 } 12514 12515 /* 12516 * Count for SNMP of inbound packets for ire. 12517 * if mctl is present this might be a secure packet and 12518 * has already been counted for in ip_proto_input(). 12519 */ 12520 if (!mctl_present) { 12521 UPDATE_IB_PKT_COUNT(ire); 12522 ire->ire_last_used_time = lbolt; 12523 } 12524 12525 /* packet part of fragmented IP packet? */ 12526 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12527 if (u1 & (IPH_MF | IPH_OFFSET)) { 12528 goto fragmented; 12529 } 12530 12531 /* u1 = IP header length (20 bytes) */ 12532 u1 = IP_SIMPLE_HDR_LENGTH; 12533 12534 /* packet does not contain complete IP & UDP headers */ 12535 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 12536 goto udppullup; 12537 12538 /* up points to UDP header */ 12539 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 12540 #define iphs ((uint16_t *)ipha) 12541 12542 /* if udp hdr cksum != 0, then need to checksum udp packet */ 12543 if (up[3] != 0) { 12544 mblk_t *mp1 = mp->b_cont; 12545 boolean_t cksum_err; 12546 uint16_t hck_flags = 0; 12547 12548 /* Pseudo-header checksum */ 12549 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12550 iphs[9] + up[2]; 12551 12552 /* 12553 * Revert to software checksum calculation if the interface 12554 * isn't capable of checksum offload or if IPsec is present. 12555 */ 12556 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 12557 hck_flags = DB_CKSUMFLAGS(mp); 12558 12559 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12560 IP_STAT(ip_in_sw_cksum); 12561 12562 IP_CKSUM_RECV(hck_flags, u1, 12563 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 12564 (int32_t)((uchar_t *)up - rptr), 12565 mp, mp1, cksum_err); 12566 12567 if (cksum_err) { 12568 BUMP_MIB(&ip_mib, udpInCksumErrs); 12569 12570 if (hck_flags & HCK_FULLCKSUM) 12571 IP_STAT(ip_udp_in_full_hw_cksum_err); 12572 else if (hck_flags & HCK_PARTIALCKSUM) 12573 IP_STAT(ip_udp_in_part_hw_cksum_err); 12574 else 12575 IP_STAT(ip_udp_in_sw_cksum_err); 12576 12577 freemsg(first_mp); 12578 return; 12579 } 12580 } 12581 12582 /* Non-fragmented broadcast or multicast packet? */ 12583 if (ire->ire_type == IRE_BROADCAST) 12584 goto udpslowpath; 12585 12586 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 12587 ire->ire_zoneid)) != NULL) { 12588 ASSERT(connp->conn_upq != NULL); 12589 IP_STAT(ip_udp_fast_path); 12590 12591 if (CONN_UDP_FLOWCTLD(connp)) { 12592 freemsg(mp); 12593 BUMP_MIB(&ip_mib, udpInOverflows); 12594 } else { 12595 if (!mctl_present) { 12596 BUMP_MIB(&ip_mib, ipInDelivers); 12597 } 12598 /* 12599 * mp and first_mp can change. 12600 */ 12601 if (ip_udp_check(q, connp, recv_ill, 12602 ipha, &mp, &first_mp, mctl_present)) { 12603 /* Send it upstream */ 12604 CONN_UDP_RECV(connp, mp); 12605 } 12606 } 12607 /* 12608 * freeb() cannot deal with null mblk being passed 12609 * in and first_mp can be set to null in the call 12610 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 12611 */ 12612 if (mctl_present && first_mp != NULL) { 12613 freeb(first_mp); 12614 } 12615 CONN_DEC_REF(connp); 12616 return; 12617 } 12618 12619 /* 12620 * if we got here we know the packet is not fragmented and 12621 * has no options. The classifier could not find a conn_t and 12622 * most likely its an icmp packet so send it through slow path. 12623 */ 12624 12625 goto udpslowpath; 12626 12627 ipoptions: 12628 if (!ip_options_cksum(q, mp, ipha, ire)) { 12629 goto slow_done; 12630 } 12631 12632 UPDATE_IB_PKT_COUNT(ire); 12633 ire->ire_last_used_time = lbolt; 12634 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12635 if (u1 & (IPH_MF | IPH_OFFSET)) { 12636 fragmented: 12637 /* 12638 * "sum" and "reass_hck_flags" are non-zero if the 12639 * reassembled packet has a valid hardware computed 12640 * checksum information associated with it. 12641 */ 12642 if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) 12643 goto slow_done; 12644 /* 12645 * Make sure that first_mp points back to mp as 12646 * the mp we came in with could have changed in 12647 * ip_rput_fragment(). 12648 */ 12649 ASSERT(!mctl_present); 12650 ipha = (ipha_t *)mp->b_rptr; 12651 first_mp = mp; 12652 } 12653 12654 /* Now we have a complete datagram, destined for this machine. */ 12655 u1 = IPH_HDR_LENGTH(ipha); 12656 /* Pull up the UDP header, if necessary. */ 12657 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 12658 udppullup: 12659 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 12660 BUMP_MIB(&ip_mib, ipInDiscards); 12661 freemsg(first_mp); 12662 goto slow_done; 12663 } 12664 ipha = (ipha_t *)mp->b_rptr; 12665 } 12666 12667 /* 12668 * Validate the checksum for the reassembled packet; for the 12669 * pullup case we calculate the payload checksum in software. 12670 */ 12671 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 12672 if (up[3] != 0) { 12673 boolean_t cksum_err; 12674 12675 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12676 IP_STAT(ip_in_sw_cksum); 12677 12678 IP_CKSUM_RECV_REASS(reass_hck_flags, 12679 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 12680 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12681 iphs[9] + up[2], sum, cksum_err); 12682 12683 if (cksum_err) { 12684 BUMP_MIB(&ip_mib, udpInCksumErrs); 12685 12686 if (reass_hck_flags & HCK_FULLCKSUM) 12687 IP_STAT(ip_udp_in_full_hw_cksum_err); 12688 else if (reass_hck_flags & HCK_PARTIALCKSUM) 12689 IP_STAT(ip_udp_in_part_hw_cksum_err); 12690 else 12691 IP_STAT(ip_udp_in_sw_cksum_err); 12692 12693 freemsg(first_mp); 12694 goto slow_done; 12695 } 12696 } 12697 udpslowpath: 12698 12699 /* Clear hardware checksum flag to be safe */ 12700 DB_CKSUMFLAGS(mp) = 0; 12701 12702 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 12703 (ire->ire_type == IRE_BROADCAST), 12704 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IP6INFO, 12705 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 12706 12707 slow_done: 12708 IP_STAT(ip_udp_slow_path); 12709 return; 12710 12711 #undef iphs 12712 #undef rptr 12713 } 12714 12715 /* ARGSUSED */ 12716 static mblk_t * 12717 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 12718 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 12719 ill_rx_ring_t *ill_ring) 12720 { 12721 conn_t *connp; 12722 uint32_t sum; 12723 uint32_t u1; 12724 uint16_t *up; 12725 int offset; 12726 ssize_t len; 12727 mblk_t *mp1; 12728 boolean_t syn_present = B_FALSE; 12729 tcph_t *tcph; 12730 uint_t ip_hdr_len; 12731 ill_t *ill = (ill_t *)q->q_ptr; 12732 zoneid_t zoneid = ire->ire_zoneid; 12733 boolean_t cksum_err; 12734 uint16_t hck_flags = 0; 12735 12736 #define rptr ((uchar_t *)ipha) 12737 12738 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 12739 12740 /* 12741 * FAST PATH for tcp packets 12742 */ 12743 12744 /* u1 is # words of IP options */ 12745 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 12746 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12747 12748 /* IP options present */ 12749 if (u1) { 12750 goto ipoptions; 12751 } else { 12752 /* Check the IP header checksum. */ 12753 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12754 /* Clear the IP header h/w cksum flag */ 12755 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12756 } else { 12757 #define uph ((uint16_t *)ipha) 12758 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 12759 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 12760 #undef uph 12761 /* finish doing IP checksum */ 12762 sum = (sum & 0xFFFF) + (sum >> 16); 12763 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12764 /* 12765 * Don't verify header checksum if this packet 12766 * is coming back from AH/ESP as we already did it. 12767 */ 12768 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 12769 BUMP_MIB(&ip_mib, ipInCksumErrs); 12770 goto error; 12771 } 12772 } 12773 } 12774 12775 if (!mctl_present) { 12776 UPDATE_IB_PKT_COUNT(ire); 12777 ire->ire_last_used_time = lbolt; 12778 } 12779 12780 /* packet part of fragmented IP packet? */ 12781 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12782 if (u1 & (IPH_MF | IPH_OFFSET)) { 12783 goto fragmented; 12784 } 12785 12786 /* u1 = IP header length (20 bytes) */ 12787 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 12788 12789 /* does packet contain IP+TCP headers? */ 12790 len = mp->b_wptr - rptr; 12791 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 12792 IP_STAT(ip_tcppullup); 12793 goto tcppullup; 12794 } 12795 12796 /* TCP options present? */ 12797 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 12798 12799 /* 12800 * If options need to be pulled up, then goto tcpoptions. 12801 * otherwise we are still in the fast path 12802 */ 12803 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 12804 IP_STAT(ip_tcpoptions); 12805 goto tcpoptions; 12806 } 12807 12808 /* multiple mblks of tcp data? */ 12809 if ((mp1 = mp->b_cont) != NULL) { 12810 /* more then two? */ 12811 if (mp1->b_cont != NULL) { 12812 IP_STAT(ip_multipkttcp); 12813 goto multipkttcp; 12814 } 12815 len += mp1->b_wptr - mp1->b_rptr; 12816 } 12817 12818 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 12819 12820 /* part of pseudo checksum */ 12821 12822 /* TCP datagram length */ 12823 u1 = len - IP_SIMPLE_HDR_LENGTH; 12824 12825 #define iphs ((uint16_t *)ipha) 12826 12827 #ifdef _BIG_ENDIAN 12828 u1 += IPPROTO_TCP; 12829 #else 12830 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 12831 #endif 12832 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 12833 12834 /* 12835 * Revert to software checksum calculation if the interface 12836 * isn't capable of checksum offload or if IPsec is present. 12837 */ 12838 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 12839 hck_flags = DB_CKSUMFLAGS(mp); 12840 12841 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12842 IP_STAT(ip_in_sw_cksum); 12843 12844 IP_CKSUM_RECV(hck_flags, u1, 12845 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 12846 (int32_t)((uchar_t *)up - rptr), 12847 mp, mp1, cksum_err); 12848 12849 if (cksum_err) { 12850 BUMP_MIB(&ip_mib, tcpInErrs); 12851 12852 if (hck_flags & HCK_FULLCKSUM) 12853 IP_STAT(ip_tcp_in_full_hw_cksum_err); 12854 else if (hck_flags & HCK_PARTIALCKSUM) 12855 IP_STAT(ip_tcp_in_part_hw_cksum_err); 12856 else 12857 IP_STAT(ip_tcp_in_sw_cksum_err); 12858 12859 goto error; 12860 } 12861 12862 try_again: 12863 12864 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid)) == 12865 NULL) { 12866 /* Send the TH_RST */ 12867 goto no_conn; 12868 } 12869 12870 /* 12871 * TCP FAST PATH for AF_INET socket. 12872 * 12873 * TCP fast path to avoid extra work. An AF_INET socket type 12874 * does not have facility to receive extra information via 12875 * ip_process or ip_add_info. Also, when the connection was 12876 * established, we made a check if this connection is impacted 12877 * by any global IPSec policy or per connection policy (a 12878 * policy that comes in effect later will not apply to this 12879 * connection). Since all this can be determined at the 12880 * connection establishment time, a quick check of flags 12881 * can avoid extra work. 12882 */ 12883 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 12884 !IPP_ENABLED(IPP_LOCAL_IN)) { 12885 ASSERT(first_mp == mp); 12886 SET_SQUEUE(mp, tcp_rput_data, connp); 12887 return (mp); 12888 } 12889 12890 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 12891 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 12892 if (IPCL_IS_TCP(connp)) { 12893 mp->b_datap->db_struioflag |= STRUIO_EAGER; 12894 DB_CKSUMSTART(mp) = 12895 (intptr_t)ip_squeue_get(ill_ring); 12896 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 12897 !CONN_INBOUND_POLICY_PRESENT(connp)) { 12898 SET_SQUEUE(mp, connp->conn_recv, connp); 12899 return (mp); 12900 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 12901 !CONN_INBOUND_POLICY_PRESENT(connp)) { 12902 ip_squeue_enter_unbound++; 12903 SET_SQUEUE(mp, tcp_conn_request_unbound, 12904 connp); 12905 return (mp); 12906 } 12907 syn_present = B_TRUE; 12908 } 12909 12910 } 12911 12912 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 12913 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12914 12915 /* No need to send this packet to TCP */ 12916 if ((flags & TH_RST) || (flags & TH_URG)) { 12917 CONN_DEC_REF(connp); 12918 freemsg(first_mp); 12919 return (NULL); 12920 } 12921 if (flags & TH_ACK) { 12922 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid); 12923 CONN_DEC_REF(connp); 12924 return (NULL); 12925 } 12926 12927 CONN_DEC_REF(connp); 12928 freemsg(first_mp); 12929 return (NULL); 12930 } 12931 12932 if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) { 12933 first_mp = ipsec_check_inbound_policy(first_mp, connp, 12934 ipha, NULL, mctl_present); 12935 if (first_mp == NULL) { 12936 CONN_DEC_REF(connp); 12937 return (NULL); 12938 } 12939 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 12940 ASSERT(syn_present); 12941 if (mctl_present) { 12942 ASSERT(first_mp != mp); 12943 first_mp->b_datap->db_struioflag |= 12944 STRUIO_POLICY; 12945 } else { 12946 ASSERT(first_mp == mp); 12947 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 12948 mp->b_datap->db_struioflag |= STRUIO_POLICY; 12949 } 12950 } else { 12951 /* 12952 * Discard first_mp early since we're dealing with a 12953 * fully-connected conn_t and tcp doesn't do policy in 12954 * this case. 12955 */ 12956 if (mctl_present) { 12957 freeb(first_mp); 12958 mctl_present = B_FALSE; 12959 } 12960 first_mp = mp; 12961 } 12962 } 12963 12964 /* Initiate IPPF processing for fastpath */ 12965 if (IPP_ENABLED(IPP_LOCAL_IN)) { 12966 uint32_t ill_index; 12967 12968 ill_index = recv_ill->ill_phyint->phyint_ifindex; 12969 ip_process(IPP_LOCAL_IN, &mp, ill_index); 12970 if (mp == NULL) { 12971 ip2dbg(("ip_input_ipsec_process: TCP pkt " 12972 "deferred/dropped during IPPF processing\n")); 12973 CONN_DEC_REF(connp); 12974 if (mctl_present) 12975 freeb(first_mp); 12976 return (NULL); 12977 } else if (mctl_present) { 12978 /* 12979 * ip_process might return a new mp. 12980 */ 12981 ASSERT(first_mp != mp); 12982 first_mp->b_cont = mp; 12983 } else { 12984 first_mp = mp; 12985 } 12986 12987 } 12988 12989 if (!syn_present && connp->conn_ipv6_recvpktinfo) { 12990 mp = ip_add_info(mp, recv_ill, flags); 12991 if (mp == NULL) { 12992 CONN_DEC_REF(connp); 12993 if (mctl_present) 12994 freeb(first_mp); 12995 return (NULL); 12996 } else if (mctl_present) { 12997 /* 12998 * ip_add_info might return a new mp. 12999 */ 13000 ASSERT(first_mp != mp); 13001 first_mp->b_cont = mp; 13002 } else { 13003 first_mp = mp; 13004 } 13005 } 13006 13007 if (IPCL_IS_TCP(connp)) { 13008 SET_SQUEUE(first_mp, connp->conn_recv, connp); 13009 return (first_mp); 13010 } else { 13011 putnext(connp->conn_rq, first_mp); 13012 CONN_DEC_REF(connp); 13013 return (NULL); 13014 } 13015 13016 no_conn: 13017 /* Initiate IPPf processing, if needed. */ 13018 if (IPP_ENABLED(IPP_LOCAL_IN)) { 13019 uint32_t ill_index; 13020 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13021 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 13022 if (first_mp == NULL) { 13023 return (NULL); 13024 } 13025 } 13026 BUMP_MIB(&ip_mib, ipInDelivers); 13027 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid); 13028 return (NULL); 13029 ipoptions: 13030 if (!ip_options_cksum(q, first_mp, ipha, ire)) { 13031 goto slow_done; 13032 } 13033 13034 UPDATE_IB_PKT_COUNT(ire); 13035 ire->ire_last_used_time = lbolt; 13036 13037 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13038 if (u1 & (IPH_MF | IPH_OFFSET)) { 13039 fragmented: 13040 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 13041 if (mctl_present) 13042 freeb(first_mp); 13043 goto slow_done; 13044 } 13045 /* 13046 * Make sure that first_mp points back to mp as 13047 * the mp we came in with could have changed in 13048 * ip_rput_fragment(). 13049 */ 13050 ASSERT(!mctl_present); 13051 ipha = (ipha_t *)mp->b_rptr; 13052 first_mp = mp; 13053 } 13054 13055 /* Now we have a complete datagram, destined for this machine. */ 13056 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 13057 13058 len = mp->b_wptr - mp->b_rptr; 13059 /* Pull up a minimal TCP header, if necessary. */ 13060 if (len < (u1 + 20)) { 13061 tcppullup: 13062 if (!pullupmsg(mp, u1 + 20)) { 13063 BUMP_MIB(&ip_mib, ipInDiscards); 13064 goto error; 13065 } 13066 ipha = (ipha_t *)mp->b_rptr; 13067 len = mp->b_wptr - mp->b_rptr; 13068 } 13069 13070 /* 13071 * Extract the offset field from the TCP header. As usual, we 13072 * try to help the compiler more than the reader. 13073 */ 13074 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 13075 if (offset != 5) { 13076 tcpoptions: 13077 if (offset < 5) { 13078 BUMP_MIB(&ip_mib, ipInDiscards); 13079 goto error; 13080 } 13081 /* 13082 * There must be TCP options. 13083 * Make sure we can grab them. 13084 */ 13085 offset <<= 2; 13086 offset += u1; 13087 if (len < offset) { 13088 if (!pullupmsg(mp, offset)) { 13089 BUMP_MIB(&ip_mib, ipInDiscards); 13090 goto error; 13091 } 13092 ipha = (ipha_t *)mp->b_rptr; 13093 len = mp->b_wptr - rptr; 13094 } 13095 } 13096 13097 /* Get the total packet length in len, including headers. */ 13098 if (mp->b_cont) { 13099 multipkttcp: 13100 len = msgdsize(mp); 13101 } 13102 13103 /* 13104 * Check the TCP checksum by pulling together the pseudo- 13105 * header checksum, and passing it to ip_csum to be added in 13106 * with the TCP datagram. 13107 * 13108 * Since we are not using the hwcksum if available we must 13109 * clear the flag. We may come here via tcppullup or tcpoptions. 13110 * If either of these fails along the way the mblk is freed. 13111 * If this logic ever changes and mblk is reused to say send 13112 * ICMP's back, then this flag may need to be cleared in 13113 * other places as well. 13114 */ 13115 DB_CKSUMFLAGS(mp) = 0; 13116 13117 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 13118 13119 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 13120 #ifdef _BIG_ENDIAN 13121 u1 += IPPROTO_TCP; 13122 #else 13123 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13124 #endif 13125 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13126 /* 13127 * Not M_DATA mblk or its a dup, so do the checksum now. 13128 */ 13129 IP_STAT(ip_in_sw_cksum); 13130 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 13131 BUMP_MIB(&ip_mib, tcpInErrs); 13132 goto error; 13133 } 13134 13135 IP_STAT(ip_tcp_slow_path); 13136 goto try_again; 13137 #undef iphs 13138 #undef rptr 13139 13140 error: 13141 freemsg(first_mp); 13142 slow_done: 13143 return (NULL); 13144 } 13145 13146 /* ARGSUSED */ 13147 static void 13148 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13149 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 13150 { 13151 conn_t *connp; 13152 uint32_t sum; 13153 uint32_t u1; 13154 ssize_t len; 13155 sctp_hdr_t *sctph; 13156 zoneid_t zoneid = ire->ire_zoneid; 13157 uint32_t pktsum; 13158 uint32_t calcsum; 13159 uint32_t ports; 13160 uint_t ipif_seqid; 13161 in6_addr_t map_src, map_dst; 13162 ill_t *ill = (ill_t *)q->q_ptr; 13163 13164 #define rptr ((uchar_t *)ipha) 13165 13166 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 13167 13168 /* u1 is # words of IP options */ 13169 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13170 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13171 13172 /* IP options present */ 13173 if (u1 > 0) { 13174 goto ipoptions; 13175 } else { 13176 /* Check the IP header checksum. */ 13177 if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 13178 #define uph ((uint16_t *)ipha) 13179 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13180 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13181 #undef uph 13182 /* finish doing IP checksum */ 13183 sum = (sum & 0xFFFF) + (sum >> 16); 13184 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13185 /* 13186 * Don't verify header checksum if this packet 13187 * is coming back from AH/ESP as we already did it. 13188 */ 13189 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 13190 BUMP_MIB(&ip_mib, ipInCksumErrs); 13191 goto error; 13192 } 13193 } 13194 /* 13195 * Since there is no SCTP h/w cksum support yet, just 13196 * clear the flag. 13197 */ 13198 DB_CKSUMFLAGS(mp) = 0; 13199 } 13200 13201 /* 13202 * Don't verify header checksum if this packet is coming 13203 * back from AH/ESP as we already did it. 13204 */ 13205 if (!mctl_present) { 13206 UPDATE_IB_PKT_COUNT(ire); 13207 ire->ire_last_used_time = lbolt; 13208 } 13209 13210 /* packet part of fragmented IP packet? */ 13211 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13212 if (u1 & (IPH_MF | IPH_OFFSET)) 13213 goto fragmented; 13214 13215 /* u1 = IP header length (20 bytes) */ 13216 u1 = IP_SIMPLE_HDR_LENGTH; 13217 13218 find_sctp_client: 13219 /* Pullup if we don't have the sctp common header. */ 13220 len = MBLKL(mp); 13221 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 13222 if (mp->b_cont == NULL || 13223 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 13224 BUMP_MIB(&ip_mib, ipInDiscards); 13225 goto error; 13226 } 13227 ipha = (ipha_t *)mp->b_rptr; 13228 len = MBLKL(mp); 13229 } 13230 13231 sctph = (sctp_hdr_t *)(rptr + u1); 13232 #ifdef DEBUG 13233 if (!skip_sctp_cksum) { 13234 #endif 13235 pktsum = sctph->sh_chksum; 13236 sctph->sh_chksum = 0; 13237 calcsum = sctp_cksum(mp, u1); 13238 if (calcsum != pktsum) { 13239 BUMP_MIB(&sctp_mib, sctpChecksumError); 13240 goto error; 13241 } 13242 sctph->sh_chksum = pktsum; 13243 #ifdef DEBUG /* skip_sctp_cksum */ 13244 } 13245 #endif 13246 /* get the ports */ 13247 ports = *(uint32_t *)&sctph->sh_sport; 13248 13249 ipif_seqid = ire->ire_ipif->ipif_seqid; 13250 IRE_REFRELE(ire); 13251 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 13252 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 13253 if ((connp = sctp_fanout(&map_src, &map_dst, ports, ipif_seqid, zoneid, 13254 mp)) == NULL) { 13255 /* Check for raw socket or OOTB handling */ 13256 goto no_conn; 13257 } 13258 13259 /* Found a client; up it goes */ 13260 BUMP_MIB(&ip_mib, ipInDelivers); 13261 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 13262 return; 13263 13264 no_conn: 13265 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 13266 ports, mctl_present, flags, B_TRUE, ipif_seqid, zoneid); 13267 return; 13268 13269 ipoptions: 13270 DB_CKSUMFLAGS(mp) = 0; 13271 if (!ip_options_cksum(q, first_mp, ipha, ire)) 13272 goto slow_done; 13273 13274 UPDATE_IB_PKT_COUNT(ire); 13275 ire->ire_last_used_time = lbolt; 13276 13277 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13278 if (u1 & (IPH_MF | IPH_OFFSET)) { 13279 fragmented: 13280 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) 13281 goto slow_done; 13282 /* 13283 * Make sure that first_mp points back to mp as 13284 * the mp we came in with could have changed in 13285 * ip_rput_fragment(). 13286 */ 13287 ASSERT(!mctl_present); 13288 ipha = (ipha_t *)mp->b_rptr; 13289 first_mp = mp; 13290 } 13291 13292 /* Now we have a complete datagram, destined for this machine. */ 13293 u1 = IPH_HDR_LENGTH(ipha); 13294 goto find_sctp_client; 13295 #undef iphs 13296 #undef rptr 13297 13298 error: 13299 freemsg(first_mp); 13300 slow_done: 13301 IRE_REFRELE(ire); 13302 } 13303 13304 #define VER_BITS 0xF0 13305 #define VERSION_6 0x60 13306 13307 static boolean_t 13308 ip_rput_multimblk_ipoptions(queue_t *q, mblk_t *mp, ipha_t **iphapp, 13309 ipaddr_t *dstp) 13310 { 13311 uint_t opt_len; 13312 ipha_t *ipha; 13313 ssize_t len; 13314 uint_t pkt_len; 13315 13316 IP_STAT(ip_ipoptions); 13317 ipha = *iphapp; 13318 13319 #define rptr ((uchar_t *)ipha) 13320 /* Assume no IPv6 packets arrive over the IPv4 queue */ 13321 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 13322 BUMP_MIB(&ip_mib, ipInIPv6); 13323 freemsg(mp); 13324 return (B_FALSE); 13325 } 13326 13327 /* multiple mblk or too short */ 13328 pkt_len = ntohs(ipha->ipha_length); 13329 13330 /* Get the number of words of IP options in the IP header. */ 13331 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 13332 if (opt_len) { 13333 /* IP Options present! Validate and process. */ 13334 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 13335 BUMP_MIB(&ip_mib, ipInHdrErrors); 13336 goto done; 13337 } 13338 /* 13339 * Recompute complete header length and make sure we 13340 * have access to all of it. 13341 */ 13342 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 13343 if (len > (mp->b_wptr - rptr)) { 13344 if (len > pkt_len) { 13345 BUMP_MIB(&ip_mib, ipInHdrErrors); 13346 goto done; 13347 } 13348 if (!pullupmsg(mp, len)) { 13349 BUMP_MIB(&ip_mib, ipInDiscards); 13350 goto done; 13351 } 13352 ipha = (ipha_t *)mp->b_rptr; 13353 } 13354 /* 13355 * Go off to ip_rput_options which returns the next hop 13356 * destination address, which may have been affected 13357 * by source routing. 13358 */ 13359 IP_STAT(ip_opt); 13360 if (ip_rput_options(q, mp, ipha, dstp) == -1) { 13361 return (B_FALSE); 13362 } 13363 } 13364 *iphapp = ipha; 13365 return (B_TRUE); 13366 done: 13367 /* clear b_prev - used by ip_mroute_decap */ 13368 mp->b_prev = NULL; 13369 freemsg(mp); 13370 return (B_FALSE); 13371 #undef rptr 13372 } 13373 13374 /* 13375 * Deal with the fact that there is no ire for the destination. 13376 * The incoming ill (in_ill) is passed in to ip_newroute only 13377 * in the case of packets coming from mobile ip forward tunnel. 13378 * It must be null otherwise. 13379 */ 13380 static ire_t * 13381 ip_rput_noire(queue_t *q, ill_t *in_ill, mblk_t *mp, int ll_multicast, 13382 ipaddr_t dst) 13383 { 13384 ipha_t *ipha; 13385 ill_t *ill; 13386 ire_t *ire; 13387 boolean_t check_multirt = B_FALSE; 13388 13389 ipha = (ipha_t *)mp->b_rptr; 13390 ill = (ill_t *)q->q_ptr; 13391 13392 ASSERT(ill != NULL); 13393 /* 13394 * No IRE for this destination, so it can't be for us. 13395 * Unless we are forwarding, drop the packet. 13396 * We have to let source routed packets through 13397 * since we don't yet know if they are 'ping -l' 13398 * packets i.e. if they will go out over the 13399 * same interface as they came in on. 13400 */ 13401 if (ll_multicast) { 13402 freemsg(mp); 13403 return (NULL); 13404 } 13405 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha)) { 13406 BUMP_MIB(&ip_mib, ipForwProhibits); 13407 freemsg(mp); 13408 return (NULL); 13409 } 13410 13411 /* 13412 * Mark this packet as having originated externally. 13413 * 13414 * For non-forwarding code path, ire_send later double 13415 * checks this interface to see if it is still exists 13416 * post-ARP resolution. 13417 * 13418 * Also, IPQOS uses this to differentiate between 13419 * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP 13420 * QOS packet processing in ip_wput_attach_llhdr(). 13421 * The QoS module can mark the b_band for a fastpath message 13422 * or the dl_priority field in a unitdata_req header for 13423 * CoS marking. This info can only be found in 13424 * ip_wput_attach_llhdr(). 13425 */ 13426 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 13427 /* 13428 * Clear the indication that this may have a hardware checksum 13429 * as we are not using it 13430 */ 13431 DB_CKSUMFLAGS(mp) = 0; 13432 13433 if (in_ill != NULL) { 13434 /* 13435 * Now hand the packet to ip_newroute. 13436 */ 13437 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID); 13438 return (NULL); 13439 } 13440 ire = ire_forward(dst, &check_multirt, NULL, NULL, 13441 MBLK_GETLABEL(mp)); 13442 13443 if (ire == NULL && check_multirt) { 13444 /* Let ip_newroute handle CGTP */ 13445 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID); 13446 return (NULL); 13447 } 13448 13449 if (ire != NULL) 13450 return (ire); 13451 13452 mp->b_prev = mp->b_next = 0; 13453 /* send icmp unreachable */ 13454 q = WR(q); 13455 /* Sent by forwarding path, and router is global zone */ 13456 if (ip_source_routed(ipha)) { 13457 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, 13458 GLOBAL_ZONEID); 13459 } else { 13460 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID); 13461 } 13462 13463 return (NULL); 13464 13465 } 13466 13467 /* 13468 * check ip header length and align it. 13469 */ 13470 static boolean_t 13471 ip_check_and_align_header(queue_t *q, mblk_t *mp) 13472 { 13473 ssize_t len; 13474 ill_t *ill; 13475 ipha_t *ipha; 13476 13477 len = MBLKL(mp); 13478 13479 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13480 if (!OK_32PTR(mp->b_rptr)) 13481 IP_STAT(ip_notaligned1); 13482 else 13483 IP_STAT(ip_notaligned2); 13484 /* Guard against bogus device drivers */ 13485 if (len < 0) { 13486 /* clear b_prev - used by ip_mroute_decap */ 13487 mp->b_prev = NULL; 13488 BUMP_MIB(&ip_mib, ipInHdrErrors); 13489 freemsg(mp); 13490 return (B_FALSE); 13491 } 13492 13493 if (ip_rput_pullups++ == 0) { 13494 ill = (ill_t *)q->q_ptr; 13495 ipha = (ipha_t *)mp->b_rptr; 13496 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 13497 "ip_check_and_align_header: %s forced us to " 13498 " pullup pkt, hdr len %ld, hdr addr %p", 13499 ill->ill_name, len, ipha); 13500 } 13501 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 13502 /* clear b_prev - used by ip_mroute_decap */ 13503 mp->b_prev = NULL; 13504 BUMP_MIB(&ip_mib, ipInDiscards); 13505 freemsg(mp); 13506 return (B_FALSE); 13507 } 13508 } 13509 return (B_TRUE); 13510 } 13511 13512 static boolean_t 13513 ip_rput_notforus(queue_t **qp, mblk_t *mp, ire_t *ire, ill_t *ill) 13514 { 13515 ill_group_t *ill_group; 13516 ill_group_t *ire_group; 13517 queue_t *q; 13518 ill_t *ire_ill; 13519 uint_t ill_ifindex; 13520 13521 q = *qp; 13522 /* 13523 * We need to check to make sure the packet came in 13524 * on the queue associated with the destination IRE. 13525 * Note that for multicast packets and broadcast packets sent to 13526 * a broadcast address which is shared between multiple interfaces 13527 * we should not do this since we just got a random broadcast ire. 13528 */ 13529 if (ire->ire_rfq && ire->ire_type != IRE_BROADCAST) { 13530 boolean_t check_multi = B_TRUE; 13531 13532 /* 13533 * This packet came in on an interface other than the 13534 * one associated with the destination address. 13535 * "Gateway" it to the appropriate interface here. 13536 * As long as the ills belong to the same group, 13537 * we don't consider them to arriving on the wrong 13538 * interface. Thus, when the switch is doing inbound 13539 * load spreading, we won't drop packets when we 13540 * are doing strict multihoming checks. Note, the 13541 * same holds true for 'usesrc groups' where the 13542 * destination address may belong to another interface 13543 * to allow multipathing to happen 13544 */ 13545 ill_group = ill->ill_group; 13546 ire_ill = (ill_t *)(ire->ire_rfq)->q_ptr; 13547 ill_ifindex = ill->ill_usesrc_ifindex; 13548 ire_group = ire_ill->ill_group; 13549 13550 /* 13551 * If it's part of the same IPMP group, or if it's a legal 13552 * address on the 'usesrc' interface, then bypass strict 13553 * checks. 13554 */ 13555 if (ill_group != NULL && ill_group == ire_group) { 13556 check_multi = B_FALSE; 13557 } else if (ill_ifindex != 0 && 13558 ill_ifindex == ire_ill->ill_phyint->phyint_ifindex) { 13559 check_multi = B_FALSE; 13560 } 13561 13562 if (check_multi && 13563 ip_strict_dst_multihoming && 13564 ((ill->ill_flags & 13565 ire->ire_ipif->ipif_ill->ill_flags & 13566 ILLF_ROUTER) == 0)) { 13567 /* Drop packet */ 13568 BUMP_MIB(&ip_mib, ipForwProhibits); 13569 freemsg(mp); 13570 return (B_TRUE); 13571 } 13572 13573 /* 13574 * Change the queue (for non-virtual destination network 13575 * interfaces) and ip_rput_local will be called with the right 13576 * queue 13577 */ 13578 q = ire->ire_rfq; 13579 } 13580 /* Must be broadcast. We'll take it. */ 13581 *qp = q; 13582 return (B_FALSE); 13583 } 13584 13585 ire_t * 13586 ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) 13587 { 13588 ipha_t *ipha; 13589 ipaddr_t ip_dst, ip_src; 13590 ire_t *src_ire = NULL; 13591 ill_t *stq_ill; 13592 uint_t hlen; 13593 uint32_t sum; 13594 queue_t *dev_q; 13595 boolean_t check_multirt = B_FALSE; 13596 13597 13598 ipha = (ipha_t *)mp->b_rptr; 13599 13600 /* 13601 * Martian Address Filtering [RFC 1812, Section 5.3.7] 13602 * The loopback address check for both src and dst has already 13603 * been checked in ip_input 13604 */ 13605 ip_dst = ntohl(dst); 13606 ip_src = ntohl(ipha->ipha_src); 13607 13608 if (ip_dst == INADDR_ANY || IN_BADCLASS(ip_dst) || 13609 IN_CLASSD(ip_src)) { 13610 BUMP_MIB(&ip_mib, ipForwProhibits); 13611 goto drop; 13612 } 13613 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 13614 ALL_ZONES, NULL, MATCH_IRE_TYPE); 13615 13616 if (src_ire != NULL) { 13617 BUMP_MIB(&ip_mib, ipForwProhibits); 13618 goto drop; 13619 } 13620 13621 /* No ire cache of nexthop. So first create one */ 13622 if (ire == NULL) { 13623 ire = ire_forward(dst, &check_multirt, NULL, NULL, NULL); 13624 /* 13625 * We only come to ip_fast_forward if ip_cgtp_filter is 13626 * is not set. So upon return from ire_forward 13627 * check_multirt should remain as false. 13628 */ 13629 ASSERT(!check_multirt); 13630 if (ire == NULL) { 13631 BUMP_MIB(&ip_mib, ipInDiscards); 13632 mp->b_prev = mp->b_next = 0; 13633 /* send icmp unreachable */ 13634 /* Sent by forwarding path, and router is global zone */ 13635 if (ip_source_routed(ipha)) { 13636 icmp_unreachable(ill->ill_wq, mp, 13637 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID); 13638 } else { 13639 icmp_unreachable(ill->ill_wq, mp, 13640 ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID); 13641 } 13642 return (ire); 13643 } 13644 } 13645 13646 /* 13647 * Forwarding fastpath exception case: 13648 * If either of the follwoing case is true, we take 13649 * the slowpath 13650 * o forwarding is not enabled 13651 * o IPMP is enabled 13652 * o corresponding ire is in incomplete state 13653 * o packet needs fragmentation 13654 * 13655 * The codeflow from here on is thus: 13656 * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 13657 */ 13658 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 13659 if (!(stq_ill->ill_flags & ILLF_ROUTER) || 13660 !(ill->ill_flags & ILLF_ROUTER) || SAME_IPMP_GROUP(ill, stq_ill) || 13661 (ire->ire_nce == NULL) || 13662 (ire->ire_nce->nce_state != ND_REACHABLE) || 13663 (ntohs(ipha->ipha_length) > ire->ire_max_frag) || 13664 ipha->ipha_ttl <= 1) { 13665 ip_rput_process_forward(ill->ill_rq, mp, ire, 13666 ipha, ill, B_FALSE); 13667 return (ire); 13668 } 13669 13670 DTRACE_PROBE4(ip4__forwarding__start, 13671 ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 13672 13673 FW_HOOKS(ip4_forwarding_event, ipv4firewall_forwarding, 13674 MSG_FWCOOKED_FORWARD, ill, stq_ill, ipha, mp, mp); 13675 13676 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 13677 13678 if (mp == NULL) 13679 goto drop; 13680 13681 mp->b_datap->db_struioun.cksum.flags = 0; 13682 /* Adjust the checksum to reflect the ttl decrement. */ 13683 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 13684 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 13685 ipha->ipha_ttl--; 13686 13687 dev_q = ire->ire_stq->q_next; 13688 if ((dev_q->q_next != NULL || 13689 dev_q->q_first != NULL) && !canput(dev_q)) { 13690 goto indiscard; 13691 } 13692 13693 hlen = ire->ire_nce->nce_fp_mp != NULL ? 13694 MBLKL(ire->ire_nce->nce_fp_mp) : 0; 13695 13696 if (hlen != 0 || ire->ire_nce->nce_res_mp != NULL) { 13697 mblk_t *mpip = mp; 13698 13699 mp = ip_wput_attach_llhdr(mpip, ire, 0, 0); 13700 if (mp != NULL) { 13701 DTRACE_PROBE4(ip4__physical__out__start, 13702 ill_t *, NULL, ill_t *, stq_ill, 13703 ipha_t *, ipha, mblk_t *, mp); 13704 FW_HOOKS(ip4_physical_out_event, 13705 ipv4firewall_physical_out, MSG_FWCOOKED_OUT, NULL, 13706 stq_ill, ipha, mp, mpip); 13707 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, 13708 mp); 13709 if (mp == NULL) 13710 goto drop; 13711 13712 UPDATE_IB_PKT_COUNT(ire); 13713 ire->ire_last_used_time = lbolt; 13714 BUMP_MIB(&ip_mib, ipForwDatagrams); 13715 putnext(ire->ire_stq, mp); 13716 return (ire); 13717 } 13718 } 13719 13720 indiscard: 13721 BUMP_MIB(&ip_mib, ipInDiscards); 13722 drop: 13723 if (mp != NULL) 13724 freemsg(mp); 13725 if (src_ire != NULL) 13726 ire_refrele(src_ire); 13727 return (ire); 13728 13729 } 13730 13731 /* 13732 * This function is called in the forwarding slowpath, when 13733 * either the ire lacks the link-layer address, or the packet needs 13734 * further processing(eg. fragmentation), before transmission. 13735 */ 13736 static void 13737 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 13738 ill_t *ill, boolean_t ll_multicast) 13739 { 13740 ill_group_t *ill_group; 13741 ill_group_t *ire_group; 13742 queue_t *dev_q; 13743 ire_t *src_ire; 13744 13745 ASSERT(ire->ire_stq != NULL); 13746 13747 mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ 13748 mp->b_next = NULL; /* ip_rput_noire sets dst here */ 13749 13750 if (ll_multicast != 0) 13751 goto drop_pkt; 13752 13753 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 13754 ALL_ZONES, NULL, MATCH_IRE_TYPE); 13755 if (src_ire != NULL || ntohl(ipha->ipha_dst) == INADDR_ANY || 13756 IN_BADCLASS(ntohl(ipha->ipha_dst))) { 13757 if (src_ire != NULL) 13758 ire_refrele(src_ire); 13759 BUMP_MIB(&ip_mib, ipForwProhibits); 13760 ip2dbg(("ip_rput_process_forward: Received packet with" 13761 " bad src/dst address on %s\n", ill->ill_name)); 13762 } 13763 13764 ill_group = ill->ill_group; 13765 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 13766 /* 13767 * Check if we want to forward this one at this time. 13768 * We allow source routed packets on a host provided that 13769 * they go out the same interface or same interface group 13770 * as they came in on. 13771 * 13772 * XXX To be quicker, we may wish to not chase pointers to 13773 * get the ILLF_ROUTER flag and instead store the 13774 * forwarding policy in the ire. An unfortunate 13775 * side-effect of that would be requiring an ire flush 13776 * whenever the ILLF_ROUTER flag changes. 13777 */ 13778 if (((ill->ill_flags & 13779 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 13780 ILLF_ROUTER) == 0) && 13781 !(ip_source_routed(ipha) && (ire->ire_rfq == q || 13782 (ill_group != NULL && ill_group == ire_group)))) { 13783 BUMP_MIB(&ip_mib, ipForwProhibits); 13784 if (ip_source_routed(ipha)) { 13785 q = WR(q); 13786 /* 13787 * Clear the indication that this may have 13788 * hardware checksum as we are not using it. 13789 */ 13790 DB_CKSUMFLAGS(mp) = 0; 13791 /* Sent by forwarding path, and router is global zone */ 13792 icmp_unreachable(q, mp, 13793 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID); 13794 return; 13795 } 13796 goto drop_pkt; 13797 } 13798 13799 /* Packet is being forwarded. Turning off hwcksum flag. */ 13800 DB_CKSUMFLAGS(mp) = 0; 13801 if (ip_g_send_redirects) { 13802 /* 13803 * Check whether the incoming interface and outgoing 13804 * interface is part of the same group. If so, 13805 * send redirects. 13806 * 13807 * Check the source address to see if it originated 13808 * on the same logical subnet it is going back out on. 13809 * If so, we should be able to send it a redirect. 13810 * Avoid sending a redirect if the destination 13811 * is directly connected (gw_addr == 0), 13812 * or if the packet was source routed out this 13813 * interface. 13814 */ 13815 ipaddr_t src; 13816 mblk_t *mp1; 13817 ire_t *src_ire = NULL; 13818 13819 /* 13820 * Check whether ire_rfq and q are from the same ill 13821 * or if they are not same, they at least belong 13822 * to the same group. If so, send redirects. 13823 */ 13824 if ((ire->ire_rfq == q || 13825 (ill_group != NULL && ill_group == ire_group)) && 13826 (ire->ire_gateway_addr != 0) && 13827 !ip_source_routed(ipha)) { 13828 13829 src = ipha->ipha_src; 13830 src_ire = ire_ftable_lookup(src, 0, 0, 13831 IRE_INTERFACE, ire->ire_ipif, NULL, ALL_ZONES, 13832 0, NULL, MATCH_IRE_IPIF | MATCH_IRE_TYPE); 13833 13834 if (src_ire != NULL) { 13835 /* 13836 * The source is directly connected. 13837 * Just copy the ip header (which is 13838 * in the first mblk) 13839 */ 13840 mp1 = copyb(mp); 13841 if (mp1 != NULL) { 13842 icmp_send_redirect(WR(q), mp1, 13843 ire->ire_gateway_addr); 13844 } 13845 ire_refrele(src_ire); 13846 } 13847 } 13848 } 13849 13850 dev_q = ire->ire_stq->q_next; 13851 if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { 13852 BUMP_MIB(&ip_mib, ipInDiscards); 13853 freemsg(mp); 13854 return; 13855 } 13856 13857 ip_rput_forward(ire, ipha, mp, ill); 13858 return; 13859 13860 drop_pkt: 13861 ip2dbg(("ip_rput_forward: drop pkt\n")); 13862 freemsg(mp); 13863 } 13864 13865 ire_t * 13866 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, 13867 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 13868 { 13869 queue_t *q; 13870 uint16_t hcksumflags; 13871 13872 q = *qp; 13873 13874 /* 13875 * Clear the indication that this may have hardware 13876 * checksum as we are not using it for forwarding. 13877 */ 13878 hcksumflags = DB_CKSUMFLAGS(mp); 13879 DB_CKSUMFLAGS(mp) = 0; 13880 13881 /* 13882 * Directed broadcast forwarding: if the packet came in over a 13883 * different interface then it is routed out over we can forward it. 13884 */ 13885 if (ipha->ipha_protocol == IPPROTO_TCP) { 13886 ire_refrele(ire); 13887 freemsg(mp); 13888 BUMP_MIB(&ip_mib, ipInDiscards); 13889 return (NULL); 13890 } 13891 /* 13892 * For multicast we have set dst to be INADDR_BROADCAST 13893 * for delivering to all STREAMS. IRE_MARK_NORECV is really 13894 * only for broadcast packets. 13895 */ 13896 if (!CLASSD(ipha->ipha_dst)) { 13897 ire_t *new_ire; 13898 ipif_t *ipif; 13899 /* 13900 * For ill groups, as the switch duplicates broadcasts 13901 * across all the ports, we need to filter out and 13902 * send up only one copy. There is one copy for every 13903 * broadcast address on each ill. Thus, we look for a 13904 * specific IRE on this ill and look at IRE_MARK_NORECV 13905 * later to see whether this ill is eligible to receive 13906 * them or not. ill_nominate_bcast_rcv() nominates only 13907 * one set of IREs for receiving. 13908 */ 13909 13910 ipif = ipif_get_next_ipif(NULL, ill); 13911 if (ipif == NULL) { 13912 ire_refrele(ire); 13913 freemsg(mp); 13914 BUMP_MIB(&ip_mib, ipInDiscards); 13915 return (NULL); 13916 } 13917 new_ire = ire_ctable_lookup(dst, 0, 0, 13918 ipif, ALL_ZONES, NULL, MATCH_IRE_ILL); 13919 ipif_refrele(ipif); 13920 13921 if (new_ire != NULL) { 13922 if (new_ire->ire_marks & IRE_MARK_NORECV) { 13923 ire_refrele(ire); 13924 ire_refrele(new_ire); 13925 freemsg(mp); 13926 BUMP_MIB(&ip_mib, ipInDiscards); 13927 return (NULL); 13928 } 13929 /* 13930 * In the special case of multirouted broadcast 13931 * packets, we unconditionally need to "gateway" 13932 * them to the appropriate interface here. 13933 * In the normal case, this cannot happen, because 13934 * there is no broadcast IRE tagged with the 13935 * RTF_MULTIRT flag. 13936 */ 13937 if (new_ire->ire_flags & RTF_MULTIRT) { 13938 ire_refrele(new_ire); 13939 if (ire->ire_rfq != NULL) { 13940 q = ire->ire_rfq; 13941 *qp = q; 13942 } 13943 } else { 13944 ire_refrele(ire); 13945 ire = new_ire; 13946 } 13947 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 13948 if (!ip_g_forward_directed_bcast) { 13949 /* 13950 * Free the message if 13951 * ip_g_forward_directed_bcast is turned 13952 * off for non-local broadcast. 13953 */ 13954 ire_refrele(ire); 13955 freemsg(mp); 13956 BUMP_MIB(&ip_mib, ipInDiscards); 13957 return (NULL); 13958 } 13959 } else { 13960 /* 13961 * This CGTP packet successfully passed the 13962 * CGTP filter, but the related CGTP 13963 * broadcast IRE has not been found, 13964 * meaning that the redundant ipif is 13965 * probably down. However, if we discarded 13966 * this packet, its duplicate would be 13967 * filtered out by the CGTP filter so none 13968 * of them would get through. So we keep 13969 * going with this one. 13970 */ 13971 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 13972 if (ire->ire_rfq != NULL) { 13973 q = ire->ire_rfq; 13974 *qp = q; 13975 } 13976 } 13977 } 13978 if (ip_g_forward_directed_bcast && ll_multicast == 0) { 13979 /* 13980 * Verify that there are not more then one 13981 * IRE_BROADCAST with this broadcast address which 13982 * has ire_stq set. 13983 * TODO: simplify, loop over all IRE's 13984 */ 13985 ire_t *ire1; 13986 int num_stq = 0; 13987 mblk_t *mp1; 13988 13989 /* Find the first one with ire_stq set */ 13990 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 13991 for (ire1 = ire; ire1 && 13992 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 13993 ire1 = ire1->ire_next) 13994 ; 13995 if (ire1) { 13996 ire_refrele(ire); 13997 ire = ire1; 13998 IRE_REFHOLD(ire); 13999 } 14000 14001 /* Check if there are additional ones with stq set */ 14002 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 14003 if (ire->ire_addr != ire1->ire_addr) 14004 break; 14005 if (ire1->ire_stq) { 14006 num_stq++; 14007 break; 14008 } 14009 } 14010 rw_exit(&ire->ire_bucket->irb_lock); 14011 if (num_stq == 1 && ire->ire_stq != NULL) { 14012 ip1dbg(("ip_rput_process_broadcast: directed " 14013 "broadcast to 0x%x\n", 14014 ntohl(ire->ire_addr))); 14015 mp1 = copymsg(mp); 14016 if (mp1) { 14017 switch (ipha->ipha_protocol) { 14018 case IPPROTO_UDP: 14019 ip_udp_input(q, mp1, ipha, ire, ill); 14020 break; 14021 default: 14022 ip_proto_input(q, mp1, ipha, ire, ill); 14023 break; 14024 } 14025 } 14026 /* 14027 * Adjust ttl to 2 (1+1 - the forward engine 14028 * will decrement it by one. 14029 */ 14030 if (ip_csum_hdr(ipha)) { 14031 BUMP_MIB(&ip_mib, ipInCksumErrs); 14032 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 14033 freemsg(mp); 14034 ire_refrele(ire); 14035 return (NULL); 14036 } 14037 ipha->ipha_ttl = ip_broadcast_ttl + 1; 14038 ipha->ipha_hdr_checksum = 0; 14039 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14040 ip_rput_process_forward(q, mp, ire, ipha, 14041 ill, ll_multicast); 14042 ire_refrele(ire); 14043 return (NULL); 14044 } 14045 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 14046 ntohl(ire->ire_addr))); 14047 } 14048 14049 14050 /* Restore any hardware checksum flags */ 14051 DB_CKSUMFLAGS(mp) = hcksumflags; 14052 return (ire); 14053 } 14054 14055 /* ARGSUSED */ 14056 static boolean_t 14057 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 14058 int *ll_multicast, ipaddr_t *dstp) 14059 { 14060 /* 14061 * Forward packets only if we have joined the allmulti 14062 * group on this interface. 14063 */ 14064 if (ip_g_mrouter && ill->ill_join_allmulti) { 14065 int retval; 14066 14067 /* 14068 * Clear the indication that this may have hardware 14069 * checksum as we are not using it. 14070 */ 14071 DB_CKSUMFLAGS(mp) = 0; 14072 retval = ip_mforward(ill, ipha, mp); 14073 /* ip_mforward updates mib variables if needed */ 14074 /* clear b_prev - used by ip_mroute_decap */ 14075 mp->b_prev = NULL; 14076 14077 switch (retval) { 14078 case 0: 14079 /* 14080 * pkt is okay and arrived on phyint. 14081 * 14082 * If we are running as a multicast router 14083 * we need to see all IGMP and/or PIM packets. 14084 */ 14085 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 14086 (ipha->ipha_protocol == IPPROTO_PIM)) { 14087 goto done; 14088 } 14089 break; 14090 case -1: 14091 /* pkt is mal-formed, toss it */ 14092 goto drop_pkt; 14093 case 1: 14094 /* pkt is okay and arrived on a tunnel */ 14095 /* 14096 * If we are running a multicast router 14097 * we need to see all igmp packets. 14098 */ 14099 if (ipha->ipha_protocol == IPPROTO_IGMP) { 14100 *dstp = INADDR_BROADCAST; 14101 *ll_multicast = 1; 14102 return (B_FALSE); 14103 } 14104 14105 goto drop_pkt; 14106 } 14107 } 14108 14109 ILM_WALKER_HOLD(ill); 14110 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 14111 /* 14112 * This might just be caused by the fact that 14113 * multiple IP Multicast addresses map to the same 14114 * link layer multicast - no need to increment counter! 14115 */ 14116 ILM_WALKER_RELE(ill); 14117 freemsg(mp); 14118 return (B_TRUE); 14119 } 14120 ILM_WALKER_RELE(ill); 14121 done: 14122 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 14123 /* 14124 * This assumes the we deliver to all streams for multicast 14125 * and broadcast packets. 14126 */ 14127 *dstp = INADDR_BROADCAST; 14128 *ll_multicast = 1; 14129 return (B_FALSE); 14130 drop_pkt: 14131 ip2dbg(("ip_rput: drop pkt\n")); 14132 freemsg(mp); 14133 return (B_TRUE); 14134 } 14135 14136 static boolean_t 14137 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 14138 int *ll_multicast, mblk_t **mpp) 14139 { 14140 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 14141 boolean_t must_copy = B_FALSE; 14142 struct iocblk *iocp; 14143 ipha_t *ipha; 14144 14145 #define rptr ((uchar_t *)ipha) 14146 14147 first_mp = *first_mpp; 14148 mp = *mpp; 14149 14150 ASSERT(first_mp == mp); 14151 14152 /* 14153 * if db_ref > 1 then copymsg and free original. Packet may be 14154 * changed and do not want other entity who has a reference to this 14155 * message to trip over the changes. This is a blind change because 14156 * trying to catch all places that might change packet is too 14157 * difficult (since it may be a module above this one) 14158 * 14159 * This corresponds to the non-fast path case. We walk down the full 14160 * chain in this case, and check the db_ref count of all the dblks, 14161 * and do a copymsg if required. It is possible that the db_ref counts 14162 * of the data blocks in the mblk chain can be different. 14163 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 14164 * count of 1, followed by a M_DATA block with a ref count of 2, if 14165 * 'snoop' is running. 14166 */ 14167 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 14168 if (mp1->b_datap->db_ref > 1) { 14169 must_copy = B_TRUE; 14170 break; 14171 } 14172 } 14173 14174 if (must_copy) { 14175 mp1 = copymsg(mp); 14176 if (mp1 == NULL) { 14177 for (mp1 = mp; mp1 != NULL; 14178 mp1 = mp1->b_cont) { 14179 mp1->b_next = NULL; 14180 mp1->b_prev = NULL; 14181 } 14182 freemsg(mp); 14183 BUMP_MIB(&ip_mib, ipInDiscards); 14184 return (B_TRUE); 14185 } 14186 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 14187 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 14188 /* Copy b_prev - used by ip_mroute_decap */ 14189 to_mp->b_prev = from_mp->b_prev; 14190 from_mp->b_prev = NULL; 14191 } 14192 *first_mpp = first_mp = mp1; 14193 freemsg(mp); 14194 mp = mp1; 14195 *mpp = mp1; 14196 } 14197 14198 ipha = (ipha_t *)mp->b_rptr; 14199 14200 /* 14201 * previous code has a case for M_DATA. 14202 * We want to check how that happens. 14203 */ 14204 ASSERT(first_mp->b_datap->db_type != M_DATA); 14205 switch (first_mp->b_datap->db_type) { 14206 case M_PROTO: 14207 case M_PCPROTO: 14208 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 14209 DL_UNITDATA_IND) { 14210 /* Go handle anything other than data elsewhere. */ 14211 ip_rput_dlpi(q, mp); 14212 return (B_TRUE); 14213 } 14214 *ll_multicast = ((dl_unitdata_ind_t *)rptr)->dl_group_address; 14215 /* Ditch the DLPI header. */ 14216 mp1 = mp->b_cont; 14217 ASSERT(first_mp == mp); 14218 *first_mpp = mp1; 14219 freeb(mp); 14220 *mpp = mp1; 14221 return (B_FALSE); 14222 case M_IOCACK: 14223 ip1dbg(("got iocack ")); 14224 iocp = (struct iocblk *)mp->b_rptr; 14225 switch (iocp->ioc_cmd) { 14226 case DL_IOC_HDR_INFO: 14227 ill = (ill_t *)q->q_ptr; 14228 ill_fastpath_ack(ill, mp); 14229 return (B_TRUE); 14230 case SIOCSTUNPARAM: 14231 case OSIOCSTUNPARAM: 14232 /* Go through qwriter_ip */ 14233 break; 14234 case SIOCGTUNPARAM: 14235 case OSIOCGTUNPARAM: 14236 ip_rput_other(NULL, q, mp, NULL); 14237 return (B_TRUE); 14238 default: 14239 putnext(q, mp); 14240 return (B_TRUE); 14241 } 14242 /* FALLTHRU */ 14243 case M_ERROR: 14244 case M_HANGUP: 14245 /* 14246 * Since this is on the ill stream we unconditionally 14247 * bump up the refcount 14248 */ 14249 ill_refhold(ill); 14250 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, 14251 B_FALSE); 14252 return (B_TRUE); 14253 case M_CTL: 14254 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 14255 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 14256 IPHADA_M_CTL)) { 14257 /* 14258 * It's an IPsec accelerated packet. 14259 * Make sure that the ill from which we received the 14260 * packet has enabled IPsec hardware acceleration. 14261 */ 14262 if (!(ill->ill_capabilities & 14263 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 14264 /* IPsec kstats: bean counter */ 14265 freemsg(mp); 14266 return (B_TRUE); 14267 } 14268 14269 /* 14270 * Make mp point to the mblk following the M_CTL, 14271 * then process according to type of mp. 14272 * After this processing, first_mp will point to 14273 * the data-attributes and mp to the pkt following 14274 * the M_CTL. 14275 */ 14276 mp = first_mp->b_cont; 14277 if (mp == NULL) { 14278 freemsg(first_mp); 14279 return (B_TRUE); 14280 } 14281 /* 14282 * A Hardware Accelerated packet can only be M_DATA 14283 * ESP or AH packet. 14284 */ 14285 if (mp->b_datap->db_type != M_DATA) { 14286 /* non-M_DATA IPsec accelerated packet */ 14287 IPSECHW_DEBUG(IPSECHW_PKT, 14288 ("non-M_DATA IPsec accelerated pkt\n")); 14289 freemsg(first_mp); 14290 return (B_TRUE); 14291 } 14292 ipha = (ipha_t *)mp->b_rptr; 14293 if (ipha->ipha_protocol != IPPROTO_AH && 14294 ipha->ipha_protocol != IPPROTO_ESP) { 14295 IPSECHW_DEBUG(IPSECHW_PKT, 14296 ("non-M_DATA IPsec accelerated pkt\n")); 14297 freemsg(first_mp); 14298 return (B_TRUE); 14299 } 14300 *mpp = mp; 14301 return (B_FALSE); 14302 } 14303 putnext(q, mp); 14304 return (B_TRUE); 14305 case M_FLUSH: 14306 if (*mp->b_rptr & FLUSHW) { 14307 *mp->b_rptr &= ~FLUSHR; 14308 qreply(q, mp); 14309 return (B_TRUE); 14310 } 14311 freemsg(mp); 14312 return (B_TRUE); 14313 case M_IOCNAK: 14314 ip1dbg(("got iocnak ")); 14315 iocp = (struct iocblk *)mp->b_rptr; 14316 switch (iocp->ioc_cmd) { 14317 case DL_IOC_HDR_INFO: 14318 case SIOCSTUNPARAM: 14319 case OSIOCSTUNPARAM: 14320 /* 14321 * Since this is on the ill stream we unconditionally 14322 * bump up the refcount 14323 */ 14324 ill_refhold(ill); 14325 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, 14326 CUR_OP, B_FALSE); 14327 return (B_TRUE); 14328 case SIOCGTUNPARAM: 14329 case OSIOCGTUNPARAM: 14330 ip_rput_other(NULL, q, mp, NULL); 14331 return (B_TRUE); 14332 default: 14333 break; 14334 } 14335 /* FALLTHRU */ 14336 default: 14337 putnext(q, mp); 14338 return (B_TRUE); 14339 } 14340 } 14341 14342 /* Read side put procedure. Packets coming from the wire arrive here. */ 14343 void 14344 ip_rput(queue_t *q, mblk_t *mp) 14345 { 14346 ill_t *ill; 14347 mblk_t *dmp = NULL; 14348 14349 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 14350 14351 ill = (ill_t *)q->q_ptr; 14352 14353 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 14354 union DL_primitives *dl; 14355 14356 /* 14357 * Things are opening or closing. Only accept DLPI control 14358 * messages. In the open case, the ill->ill_ipif has not yet 14359 * been created. In the close case, things hanging off the 14360 * ill could have been freed already. In either case it 14361 * may not be safe to proceed further. 14362 */ 14363 14364 dl = (union DL_primitives *)mp->b_rptr; 14365 if ((mp->b_datap->db_type != M_PCPROTO) || 14366 (dl->dl_primitive == DL_UNITDATA_IND)) { 14367 /* 14368 * Also SIOC[GS]TUN* ioctls can come here. 14369 */ 14370 inet_freemsg(mp); 14371 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14372 "ip_input_end: q %p (%S)", q, "uninit"); 14373 return; 14374 } 14375 } 14376 14377 /* 14378 * if db_ref > 1 then copymsg and free original. Packet may be 14379 * changed and we do not want the other entity who has a reference to 14380 * this message to trip over the changes. This is a blind change because 14381 * trying to catch all places that might change the packet is too 14382 * difficult. 14383 * 14384 * This corresponds to the fast path case, where we have a chain of 14385 * M_DATA mblks. We check the db_ref count of only the 1st data block 14386 * in the mblk chain. There doesn't seem to be a reason why a device 14387 * driver would send up data with varying db_ref counts in the mblk 14388 * chain. In any case the Fast path is a private interface, and our 14389 * drivers don't do such a thing. Given the above assumption, there is 14390 * no need to walk down the entire mblk chain (which could have a 14391 * potential performance problem) 14392 */ 14393 if (mp->b_datap->db_ref > 1) { 14394 mblk_t *mp1; 14395 boolean_t adjusted = B_FALSE; 14396 IP_STAT(ip_db_ref); 14397 14398 /* 14399 * The IP_RECVSLLA option depends on having the link layer 14400 * header. First check that: 14401 * a> the underlying device is of type ether, since this 14402 * option is currently supported only over ethernet. 14403 * b> there is enough room to copy over the link layer header. 14404 * 14405 * Once the checks are done, adjust rptr so that the link layer 14406 * header will be copied via copymsg. Note that, IFT_ETHER may 14407 * be returned by some non-ethernet drivers but in this case the 14408 * second check will fail. 14409 */ 14410 if (ill->ill_type == IFT_ETHER && 14411 (mp->b_rptr - mp->b_datap->db_base) >= 14412 sizeof (struct ether_header)) { 14413 mp->b_rptr -= sizeof (struct ether_header); 14414 adjusted = B_TRUE; 14415 } 14416 mp1 = copymsg(mp); 14417 if (mp1 == NULL) { 14418 mp->b_next = NULL; 14419 /* clear b_prev - used by ip_mroute_decap */ 14420 mp->b_prev = NULL; 14421 freemsg(mp); 14422 BUMP_MIB(&ip_mib, ipInDiscards); 14423 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14424 "ip_rput_end: q %p (%S)", q, "copymsg"); 14425 return; 14426 } 14427 if (adjusted) { 14428 /* 14429 * Copy is done. Restore the pointer in the _new_ mblk 14430 */ 14431 mp1->b_rptr += sizeof (struct ether_header); 14432 } 14433 /* Copy b_prev - used by ip_mroute_decap */ 14434 mp1->b_prev = mp->b_prev; 14435 mp->b_prev = NULL; 14436 freemsg(mp); 14437 mp = mp1; 14438 } 14439 if (DB_TYPE(mp) == M_DATA) { 14440 dmp = mp; 14441 } else if (DB_TYPE(mp) == M_PROTO && 14442 *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { 14443 dmp = mp->b_cont; 14444 } 14445 if (dmp != NULL) { 14446 /* 14447 * IP header ptr not aligned? 14448 * OR IP header not complete in first mblk 14449 */ 14450 if (!OK_32PTR(dmp->b_rptr) || 14451 (dmp->b_wptr - dmp->b_rptr) < IP_SIMPLE_HDR_LENGTH) { 14452 if (!ip_check_and_align_header(q, dmp)) 14453 return; 14454 } 14455 } 14456 14457 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14458 "ip_rput_end: q %p (%S)", q, "end"); 14459 14460 ip_input(ill, NULL, mp, NULL); 14461 } 14462 14463 /* 14464 * Direct read side procedure capable of dealing with chains. GLDv3 based 14465 * drivers call this function directly with mblk chains while STREAMS 14466 * read side procedure ip_rput() calls this for single packet with ip_ring 14467 * set to NULL to process one packet at a time. 14468 * 14469 * The ill will always be valid if this function is called directly from 14470 * the driver. 14471 * 14472 * If ip_input() is called from GLDv3: 14473 * 14474 * - This must be a non-VLAN IP stream. 14475 * - 'mp' is either an untagged or a special priority-tagged packet. 14476 * - Any VLAN tag that was in the MAC header has been stripped. 14477 * 14478 * Thus, there is no need to adjust b_rptr in this function. 14479 */ 14480 /* ARGSUSED */ 14481 void 14482 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, 14483 struct mac_header_info_s *mhip) 14484 { 14485 ipaddr_t dst = NULL; 14486 ipaddr_t prev_dst; 14487 ire_t *ire = NULL; 14488 ipha_t *ipha; 14489 uint_t pkt_len; 14490 ssize_t len; 14491 uint_t opt_len; 14492 int ll_multicast; 14493 int cgtp_flt_pkt; 14494 queue_t *q = ill->ill_rq; 14495 squeue_t *curr_sqp = NULL; 14496 mblk_t *head = NULL; 14497 mblk_t *tail = NULL; 14498 mblk_t *first_mp; 14499 mblk_t *mp; 14500 int cnt = 0; 14501 14502 ASSERT(mp_chain != NULL); 14503 ASSERT(ill != NULL); 14504 14505 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 14506 14507 #define rptr ((uchar_t *)ipha) 14508 14509 while (mp_chain != NULL) { 14510 first_mp = mp = mp_chain; 14511 mp_chain = mp_chain->b_next; 14512 mp->b_next = NULL; 14513 ll_multicast = 0; 14514 14515 /* 14516 * We do ire caching from one iteration to 14517 * another. In the event the packet chain contains 14518 * all packets from the same dst, this caching saves 14519 * an ire_cache_lookup for each of the succeeding 14520 * packets in a packet chain. 14521 */ 14522 prev_dst = dst; 14523 14524 /* 14525 * ip_input fast path 14526 */ 14527 14528 /* mblk type is not M_DATA */ 14529 if (mp->b_datap->db_type != M_DATA) { 14530 if (ip_rput_process_notdata(q, &first_mp, ill, 14531 &ll_multicast, &mp)) 14532 continue; 14533 } 14534 14535 /* Make sure its an M_DATA and that its aligned */ 14536 ASSERT(mp->b_datap->db_type == M_DATA); 14537 ASSERT(mp->b_datap->db_ref == 1 && OK_32PTR(mp->b_rptr)); 14538 14539 ipha = (ipha_t *)mp->b_rptr; 14540 len = mp->b_wptr - rptr; 14541 14542 BUMP_MIB(&ip_mib, ipInReceives); 14543 14544 14545 /* multiple mblk or too short */ 14546 pkt_len = ntohs(ipha->ipha_length); 14547 len -= pkt_len; 14548 if (len != 0) { 14549 /* 14550 * Make sure we have data length consistent 14551 * with the IP header. 14552 */ 14553 if (mp->b_cont == NULL) { 14554 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 14555 BUMP_MIB(&ip_mib, ipInHdrErrors); 14556 ip2dbg(("ip_input: drop pkt\n")); 14557 freemsg(mp); 14558 continue; 14559 } 14560 mp->b_wptr = rptr + pkt_len; 14561 } else if (len += msgdsize(mp->b_cont)) { 14562 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 14563 BUMP_MIB(&ip_mib, ipInHdrErrors); 14564 ip2dbg(("ip_input: drop pkt\n")); 14565 freemsg(mp); 14566 continue; 14567 } 14568 (void) adjmsg(mp, -len); 14569 IP_STAT(ip_multimblk3); 14570 } 14571 } 14572 14573 /* 14574 * The event for packets being received from a 'physical' 14575 * interface is placed before validation of the source and/or 14576 * destination address as being local so that packets such as 14577 * these that are found on the network can be observed via 14578 * this interface. The checks prior to this have all been 14579 * to do with validating the sanity of the packet - length 14580 * fields vs data in the buffer, buffer size, etc, otherwise 14581 * uninteresting packet flaws that will always lead to them 14582 * being discarded. 14583 */ 14584 DTRACE_PROBE4(ip4__physical__in__start, 14585 ill_t *, ill, ill_t *, NULL, 14586 ipha_t *, ipha, mblk_t *, first_mp); 14587 14588 FW_HOOKS(ip4_physical_in_event, ipv4firewall_physical_in, 14589 MSG_FWCOOKED_IN, ill, NULL, ipha, first_mp, mp); 14590 14591 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 14592 14593 if (first_mp == NULL) { 14594 continue; 14595 } 14596 14597 /* Obtain the dst of the current packet */ 14598 dst = ipha->ipha_dst; 14599 14600 if (IP_LOOPBACK_ADDR(dst) || 14601 IP_LOOPBACK_ADDR(ipha->ipha_src)) { 14602 BUMP_MIB(&ip_mib, ipInAddrErrors); 14603 cmn_err(CE_CONT, "dst %X src %X\n", 14604 dst, ipha->ipha_src); 14605 freemsg(mp); 14606 continue; 14607 } 14608 14609 /* 14610 * The event for packets being received from a 'physical' 14611 * interface is placed after validation of the source and/or 14612 * destination address as being local so that packets can be 14613 * redirected to loopback addresses using ipnat. 14614 */ 14615 DTRACE_PROBE4(ip4__physical__in__start, 14616 ill_t *, ill, ill_t *, NULL, 14617 ipha_t *, ipha, mblk_t *, first_mp); 14618 14619 FW_HOOKS(ip4_physical_in_event, ipv4firewall_physical_in, 14620 MSG_FWCOOKED_IN, ill, NULL, ipha, first_mp, mp); 14621 14622 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 14623 14624 if (first_mp == NULL) { 14625 continue; 14626 } 14627 dst = ipha->ipha_dst; 14628 14629 /* 14630 * Attach any necessary label information to 14631 * this packet 14632 */ 14633 if (is_system_labeled() && 14634 !tsol_get_pkt_label(mp, IPV4_VERSION)) { 14635 BUMP_MIB(&ip_mib, ipInDiscards); 14636 freemsg(mp); 14637 continue; 14638 } 14639 14640 /* 14641 * Reuse the cached ire only if the ipha_dst of the previous 14642 * packet is the same as the current packet AND it is not 14643 * INADDR_ANY. 14644 */ 14645 if (!(dst == prev_dst && dst != INADDR_ANY) && 14646 (ire != NULL)) { 14647 ire_refrele(ire); 14648 ire = NULL; 14649 } 14650 opt_len = ipha->ipha_version_and_hdr_length - 14651 IP_SIMPLE_HDR_VERSION; 14652 14653 /* 14654 * Check to see if we can take the fastpath. 14655 * That is possible if the following conditions are met 14656 * o Tsol disabled 14657 * o CGTP disabled 14658 * o ipp_action_count is 0 14659 * o Mobile IP not running 14660 * o no options in the packet 14661 * o not a RSVP packet 14662 * o not a multicast packet 14663 */ 14664 if (!is_system_labeled() && 14665 !ip_cgtp_filter && ipp_action_count == 0 && 14666 ill->ill_mrtun_refcnt == 0 && ill->ill_srcif_refcnt == 0 && 14667 opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && 14668 !ll_multicast && !CLASSD(dst)) { 14669 if (ire == NULL) 14670 ire = ire_cache_lookup(dst, ALL_ZONES, NULL); 14671 14672 /* incoming packet is for forwarding */ 14673 if (ire == NULL || (ire->ire_type & IRE_CACHE)) { 14674 ire = ip_fast_forward(ire, dst, ill, mp); 14675 continue; 14676 } 14677 /* incoming packet is for local consumption */ 14678 if (ire->ire_type & IRE_LOCAL) 14679 goto local; 14680 } 14681 14682 /* 14683 * Disable ire caching for anything more complex 14684 * than the simple fast path case we checked for above. 14685 */ 14686 if (ire != NULL) { 14687 ire_refrele(ire); 14688 ire = NULL; 14689 } 14690 14691 /* Full-blown slow path */ 14692 if (opt_len != 0) { 14693 if (len != 0) 14694 IP_STAT(ip_multimblk4); 14695 else 14696 IP_STAT(ip_ipoptions); 14697 if (!ip_rput_multimblk_ipoptions(q, mp, &ipha, &dst)) 14698 continue; 14699 } 14700 14701 /* 14702 * Invoke the CGTP (multirouting) filtering module to process 14703 * the incoming packet. Packets identified as duplicates 14704 * must be discarded. Filtering is active only if the 14705 * the ip_cgtp_filter ndd variable is non-zero. 14706 */ 14707 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 14708 if (ip_cgtp_filter && (ip_cgtp_filter_ops != NULL)) { 14709 cgtp_flt_pkt = 14710 ip_cgtp_filter_ops->cfo_filter(q, mp); 14711 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 14712 freemsg(first_mp); 14713 continue; 14714 } 14715 } 14716 14717 /* 14718 * If rsvpd is running, let RSVP daemon handle its processing 14719 * and forwarding of RSVP multicast/unicast packets. 14720 * If rsvpd is not running but mrouted is running, RSVP 14721 * multicast packets are forwarded as multicast traffic 14722 * and RSVP unicast packets are forwarded by unicast router. 14723 * If neither rsvpd nor mrouted is running, RSVP multicast 14724 * packets are not forwarded, but the unicast packets are 14725 * forwarded like unicast traffic. 14726 */ 14727 if (ipha->ipha_protocol == IPPROTO_RSVP && 14728 ipcl_proto_search(IPPROTO_RSVP) != NULL) { 14729 /* RSVP packet and rsvpd running. Treat as ours */ 14730 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 14731 /* 14732 * This assumes that we deliver to all streams for 14733 * multicast and broadcast packets. 14734 * We have to force ll_multicast to 1 to handle the 14735 * M_DATA messages passed in from ip_mroute_decap. 14736 */ 14737 dst = INADDR_BROADCAST; 14738 ll_multicast = 1; 14739 } else if (CLASSD(dst)) { 14740 /* packet is multicast */ 14741 mp->b_next = NULL; 14742 if (ip_rput_process_multicast(q, mp, ill, ipha, 14743 &ll_multicast, &dst)) 14744 continue; 14745 } 14746 14747 14748 /* 14749 * Check if the packet is coming from the Mobile IP 14750 * forward tunnel interface 14751 */ 14752 if (ill->ill_srcif_refcnt > 0) { 14753 ire = ire_srcif_table_lookup(dst, IRE_INTERFACE, 14754 NULL, ill, MATCH_IRE_TYPE); 14755 if (ire != NULL && ire->ire_nce->nce_res_mp == NULL && 14756 ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER) { 14757 14758 /* We need to resolve the link layer info */ 14759 ire_refrele(ire); 14760 ire = NULL; 14761 (void) ip_rput_noire(q, (ill_t *)q->q_ptr, mp, 14762 ll_multicast, dst); 14763 continue; 14764 } 14765 } 14766 14767 if (ire == NULL) { 14768 ire = ire_cache_lookup(dst, ALL_ZONES, 14769 MBLK_GETLABEL(mp)); 14770 } 14771 14772 /* 14773 * If mipagent is running and reverse tunnel is created as per 14774 * mobile node request, then any packet coming through the 14775 * incoming interface from the mobile-node, should be reverse 14776 * tunneled to it's home agent except those that are destined 14777 * to foreign agent only. 14778 * This needs source address based ire lookup. The routing 14779 * entries for source address based lookup are only created by 14780 * mipagent program only when a reverse tunnel is created. 14781 * Reference : RFC2002, RFC2344 14782 */ 14783 if (ill->ill_mrtun_refcnt > 0) { 14784 ipaddr_t srcaddr; 14785 ire_t *tmp_ire; 14786 14787 tmp_ire = ire; /* Save, we might need it later */ 14788 if (ire == NULL || (ire->ire_type != IRE_LOCAL && 14789 ire->ire_type != IRE_BROADCAST)) { 14790 srcaddr = ipha->ipha_src; 14791 ire = ire_mrtun_lookup(srcaddr, ill); 14792 if (ire != NULL) { 14793 /* 14794 * Should not be getting iphada packet 14795 * here. we should only get those for 14796 * IRE_LOCAL traffic, excluded above. 14797 * Fail-safe (drop packet) in the event 14798 * hardware is misbehaving. 14799 */ 14800 if (first_mp != mp) { 14801 /* IPsec KSTATS: beancount me */ 14802 freemsg(first_mp); 14803 } else { 14804 /* 14805 * This packet must be forwarded 14806 * to Reverse Tunnel 14807 */ 14808 ip_mrtun_forward(ire, ill, mp); 14809 } 14810 ire_refrele(ire); 14811 ire = NULL; 14812 if (tmp_ire != NULL) { 14813 ire_refrele(tmp_ire); 14814 tmp_ire = NULL; 14815 } 14816 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14817 "ip_input_end: q %p (%S)", 14818 q, "uninit"); 14819 continue; 14820 } 14821 } 14822 /* 14823 * If this packet is from a non-mobilenode or a 14824 * mobile-node which does not request reverse 14825 * tunnel service 14826 */ 14827 ire = tmp_ire; 14828 } 14829 14830 14831 /* 14832 * If we reach here that means the incoming packet satisfies 14833 * one of the following conditions: 14834 * - packet is from a mobile node which does not request 14835 * reverse tunnel 14836 * - packet is from a non-mobile node, which is the most 14837 * common case 14838 * - packet is from a reverse tunnel enabled mobile node 14839 * and destined to foreign agent only 14840 */ 14841 14842 if (ire == NULL) { 14843 /* 14844 * No IRE for this destination, so it can't be for us. 14845 * Unless we are forwarding, drop the packet. 14846 * We have to let source routed packets through 14847 * since we don't yet know if they are 'ping -l' 14848 * packets i.e. if they will go out over the 14849 * same interface as they came in on. 14850 */ 14851 ire = ip_rput_noire(q, NULL, mp, ll_multicast, dst); 14852 if (ire == NULL) 14853 continue; 14854 } 14855 14856 /* 14857 * Broadcast IRE may indicate either broadcast or 14858 * multicast packet 14859 */ 14860 if (ire->ire_type == IRE_BROADCAST) { 14861 /* 14862 * Skip broadcast checks if packet is UDP multicast; 14863 * we'd rather not enter ip_rput_process_broadcast() 14864 * unless the packet is broadcast for real, since 14865 * that routine is a no-op for multicast. 14866 */ 14867 if (ipha->ipha_protocol != IPPROTO_UDP || 14868 !CLASSD(ipha->ipha_dst)) { 14869 ire = ip_rput_process_broadcast(&q, mp, 14870 ire, ipha, ill, dst, cgtp_flt_pkt, 14871 ll_multicast); 14872 if (ire == NULL) 14873 continue; 14874 } 14875 } else if (ire->ire_stq != NULL) { 14876 /* fowarding? */ 14877 ip_rput_process_forward(q, mp, ire, ipha, ill, 14878 ll_multicast); 14879 /* ip_rput_process_forward consumed the packet */ 14880 continue; 14881 } 14882 14883 local: 14884 /* packet not for us */ 14885 if (ire->ire_rfq != q) { 14886 if (ip_rput_notforus(&q, mp, ire, ill)) 14887 continue; 14888 } 14889 14890 switch (ipha->ipha_protocol) { 14891 case IPPROTO_TCP: 14892 ASSERT(first_mp == mp); 14893 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 14894 mp, 0, q, ip_ring)) != NULL) { 14895 if (curr_sqp == NULL) { 14896 curr_sqp = GET_SQUEUE(mp); 14897 ASSERT(cnt == 0); 14898 cnt++; 14899 head = tail = mp; 14900 } else if (curr_sqp == GET_SQUEUE(mp)) { 14901 ASSERT(tail != NULL); 14902 cnt++; 14903 tail->b_next = mp; 14904 tail = mp; 14905 } else { 14906 /* 14907 * A different squeue. Send the 14908 * chain for the previous squeue on 14909 * its way. This shouldn't happen 14910 * often unless interrupt binding 14911 * changes. 14912 */ 14913 IP_STAT(ip_input_multi_squeue); 14914 squeue_enter_chain(curr_sqp, head, 14915 tail, cnt, SQTAG_IP_INPUT); 14916 curr_sqp = GET_SQUEUE(mp); 14917 head = mp; 14918 tail = mp; 14919 cnt = 1; 14920 } 14921 } 14922 continue; 14923 case IPPROTO_UDP: 14924 ASSERT(first_mp == mp); 14925 ip_udp_input(q, mp, ipha, ire, ill); 14926 continue; 14927 case IPPROTO_SCTP: 14928 ASSERT(first_mp == mp); 14929 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 14930 q, dst); 14931 /* ire has been released by ip_sctp_input */ 14932 ire = NULL; 14933 continue; 14934 default: 14935 ip_proto_input(q, first_mp, ipha, ire, ill); 14936 continue; 14937 } 14938 } 14939 14940 if (ire != NULL) 14941 ire_refrele(ire); 14942 14943 if (head != NULL) 14944 squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); 14945 14946 /* 14947 * This code is there just to make netperf/ttcp look good. 14948 * 14949 * Its possible that after being in polling mode (and having cleared 14950 * the backlog), squeues have turned the interrupt frequency higher 14951 * to improve latency at the expense of more CPU utilization (less 14952 * packets per interrupts or more number of interrupts). Workloads 14953 * like ttcp/netperf do manage to tickle polling once in a while 14954 * but for the remaining time, stay in higher interrupt mode since 14955 * their packet arrival rate is pretty uniform and this shows up 14956 * as higher CPU utilization. Since people care about CPU utilization 14957 * while running netperf/ttcp, turn the interrupt frequency back to 14958 * normal/default if polling has not been used in ip_poll_normal_ticks. 14959 */ 14960 if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { 14961 if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { 14962 ip_ring->rr_poll_state &= ~ILL_POLLING; 14963 ip_ring->rr_blank(ip_ring->rr_handle, 14964 ip_ring->rr_normal_blank_time, 14965 ip_ring->rr_normal_pkt_cnt); 14966 } 14967 } 14968 14969 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14970 "ip_input_end: q %p (%S)", q, "end"); 14971 #undef rptr 14972 } 14973 14974 static void 14975 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 14976 t_uscalar_t err) 14977 { 14978 if (dl_err == DL_SYSERR) { 14979 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 14980 "%s: %s failed: DL_SYSERR (errno %u)\n", 14981 ill->ill_name, dlpi_prim_str(prim), err); 14982 return; 14983 } 14984 14985 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 14986 "%s: %s failed: %s\n", ill->ill_name, dlpi_prim_str(prim), 14987 dlpi_err_str(dl_err)); 14988 } 14989 14990 /* 14991 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 14992 * than DL_UNITDATA_IND messages. If we need to process this message 14993 * exclusively, we call qwriter_ip, in which case we also need to call 14994 * ill_refhold before that, since qwriter_ip does an ill_refrele. 14995 */ 14996 void 14997 ip_rput_dlpi(queue_t *q, mblk_t *mp) 14998 { 14999 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15000 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15001 ill_t *ill; 15002 15003 ip1dbg(("ip_rput_dlpi")); 15004 ill = (ill_t *)q->q_ptr; 15005 switch (dloa->dl_primitive) { 15006 case DL_ERROR_ACK: 15007 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK %s (0x%x): " 15008 "%s (0x%x), unix %u\n", ill->ill_name, 15009 dlpi_prim_str(dlea->dl_error_primitive), 15010 dlea->dl_error_primitive, 15011 dlpi_err_str(dlea->dl_errno), 15012 dlea->dl_errno, 15013 dlea->dl_unix_errno)); 15014 switch (dlea->dl_error_primitive) { 15015 case DL_UNBIND_REQ: 15016 mutex_enter(&ill->ill_lock); 15017 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15018 cv_signal(&ill->ill_cv); 15019 mutex_exit(&ill->ill_lock); 15020 /* FALLTHRU */ 15021 case DL_NOTIFY_REQ: 15022 case DL_ATTACH_REQ: 15023 case DL_DETACH_REQ: 15024 case DL_INFO_REQ: 15025 case DL_BIND_REQ: 15026 case DL_ENABMULTI_REQ: 15027 case DL_PHYS_ADDR_REQ: 15028 case DL_CAPABILITY_REQ: 15029 case DL_CONTROL_REQ: 15030 /* 15031 * Refhold the ill to match qwriter_ip which does a 15032 * refrele. Since this is on the ill stream we 15033 * unconditionally bump up the refcount without 15034 * checking for ILL_CAN_LOOKUP 15035 */ 15036 ill_refhold(ill); 15037 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15038 CUR_OP, B_FALSE); 15039 return; 15040 case DL_DISABMULTI_REQ: 15041 freemsg(mp); /* Don't want to pass this up */ 15042 return; 15043 default: 15044 break; 15045 } 15046 ip_dlpi_error(ill, dlea->dl_error_primitive, 15047 dlea->dl_errno, dlea->dl_unix_errno); 15048 freemsg(mp); 15049 return; 15050 case DL_INFO_ACK: 15051 case DL_BIND_ACK: 15052 case DL_PHYS_ADDR_ACK: 15053 case DL_NOTIFY_ACK: 15054 case DL_CAPABILITY_ACK: 15055 case DL_CONTROL_ACK: 15056 /* 15057 * Refhold the ill to match qwriter_ip which does a refrele 15058 * Since this is on the ill stream we unconditionally 15059 * bump up the refcount without doing ILL_CAN_LOOKUP. 15060 */ 15061 ill_refhold(ill); 15062 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15063 CUR_OP, B_FALSE); 15064 return; 15065 case DL_NOTIFY_IND: 15066 ill_refhold(ill); 15067 /* 15068 * The DL_NOTIFY_IND is an asynchronous message that has no 15069 * relation to the current ioctl in progress (if any). Hence we 15070 * pass in NEW_OP in this case. 15071 */ 15072 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15073 NEW_OP, B_FALSE); 15074 return; 15075 case DL_OK_ACK: 15076 ip1dbg(("ip_rput: DL_OK_ACK for %s\n", 15077 dlpi_prim_str((int)dloa->dl_correct_primitive))); 15078 switch (dloa->dl_correct_primitive) { 15079 case DL_UNBIND_REQ: 15080 mutex_enter(&ill->ill_lock); 15081 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15082 cv_signal(&ill->ill_cv); 15083 mutex_exit(&ill->ill_lock); 15084 /* FALLTHRU */ 15085 case DL_ATTACH_REQ: 15086 case DL_DETACH_REQ: 15087 /* 15088 * Refhold the ill to match qwriter_ip which does a 15089 * refrele. Since this is on the ill stream we 15090 * unconditionally bump up the refcount 15091 */ 15092 ill_refhold(ill); 15093 qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15094 CUR_OP, B_FALSE); 15095 return; 15096 case DL_ENABMULTI_REQ: 15097 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15098 ill->ill_dlpi_multicast_state = IDS_OK; 15099 break; 15100 15101 } 15102 break; 15103 default: 15104 break; 15105 } 15106 freemsg(mp); 15107 } 15108 15109 /* 15110 * Handling of DLPI messages that require exclusive access to the ipsq. 15111 * 15112 * Need to do ill_pending_mp_release on ioctl completion, which could 15113 * happen here. (along with mi_copy_done) 15114 */ 15115 /* ARGSUSED */ 15116 static void 15117 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15118 { 15119 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15120 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15121 int err = 0; 15122 ill_t *ill; 15123 ipif_t *ipif = NULL; 15124 mblk_t *mp1 = NULL; 15125 conn_t *connp = NULL; 15126 t_uscalar_t physaddr_req; 15127 mblk_t *mp_hw; 15128 union DL_primitives *dlp; 15129 boolean_t success; 15130 boolean_t ioctl_aborted = B_FALSE; 15131 boolean_t log = B_TRUE; 15132 hook_nic_event_t *info; 15133 15134 ip1dbg(("ip_rput_dlpi_writer ..")); 15135 ill = (ill_t *)q->q_ptr; 15136 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 15137 15138 ASSERT(IAM_WRITER_ILL(ill)); 15139 15140 /* 15141 * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. 15142 * both are null or non-null. However we can assert that only 15143 * after grabbing the ipsq_lock. So we don't make any assertion 15144 * here and in other places in the code. 15145 */ 15146 ipif = ipsq->ipsq_pending_ipif; 15147 /* 15148 * The current ioctl could have been aborted by the user and a new 15149 * ioctl to bring up another ill could have started. We could still 15150 * get a response from the driver later. 15151 */ 15152 if (ipif != NULL && ipif->ipif_ill != ill) 15153 ioctl_aborted = B_TRUE; 15154 15155 switch (dloa->dl_primitive) { 15156 case DL_ERROR_ACK: 15157 switch (dlea->dl_error_primitive) { 15158 case DL_UNBIND_REQ: 15159 case DL_ATTACH_REQ: 15160 case DL_DETACH_REQ: 15161 case DL_INFO_REQ: 15162 ill_dlpi_done(ill, dlea->dl_error_primitive); 15163 break; 15164 case DL_NOTIFY_REQ: 15165 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15166 log = B_FALSE; 15167 break; 15168 case DL_PHYS_ADDR_REQ: 15169 /* 15170 * For IPv6 only, there are two additional 15171 * phys_addr_req's sent to the driver to get the 15172 * IPv6 token and lla. This allows IP to acquire 15173 * the hardware address format for a given interface 15174 * without having built in knowledge of the hardware 15175 * address. ill_phys_addr_pend keeps track of the last 15176 * DL_PAR sent so we know which response we are 15177 * dealing with. ill_dlpi_done will update 15178 * ill_phys_addr_pend when it sends the next req. 15179 * We don't complete the IOCTL until all three DL_PARs 15180 * have been attempted, so set *_len to 0 and break. 15181 */ 15182 physaddr_req = ill->ill_phys_addr_pend; 15183 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15184 if (physaddr_req == DL_IPV6_TOKEN) { 15185 ill->ill_token_length = 0; 15186 log = B_FALSE; 15187 break; 15188 } else if (physaddr_req == DL_IPV6_LINK_LAYER_ADDR) { 15189 ill->ill_nd_lla_len = 0; 15190 log = B_FALSE; 15191 break; 15192 } 15193 /* 15194 * Something went wrong with the DL_PHYS_ADDR_REQ. 15195 * We presumably have an IOCTL hanging out waiting 15196 * for completion. Find it and complete the IOCTL 15197 * with the error noted. 15198 * However, ill_dl_phys was called on an ill queue 15199 * (from SIOCSLIFNAME), thus conn_pending_ill is not 15200 * set. But the ioctl is known to be pending on ill_wq. 15201 */ 15202 if (!ill->ill_ifname_pending) 15203 break; 15204 ill->ill_ifname_pending = 0; 15205 if (!ioctl_aborted) 15206 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15207 if (mp1 != NULL) { 15208 /* 15209 * This operation (SIOCSLIFNAME) must have 15210 * happened on the ill. Assert there is no conn 15211 */ 15212 ASSERT(connp == NULL); 15213 q = ill->ill_wq; 15214 } 15215 break; 15216 case DL_BIND_REQ: 15217 ill_dlpi_done(ill, DL_BIND_REQ); 15218 if (ill->ill_ifname_pending) 15219 break; 15220 /* 15221 * Something went wrong with the bind. We presumably 15222 * have an IOCTL hanging out waiting for completion. 15223 * Find it, take down the interface that was coming 15224 * up, and complete the IOCTL with the error noted. 15225 */ 15226 if (!ioctl_aborted) 15227 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15228 if (mp1 != NULL) { 15229 /* 15230 * This operation (SIOCSLIFFLAGS) must have 15231 * happened from a conn. 15232 */ 15233 ASSERT(connp != NULL); 15234 q = CONNP_TO_WQ(connp); 15235 if (ill->ill_move_in_progress) { 15236 ILL_CLEAR_MOVE(ill); 15237 } 15238 (void) ipif_down(ipif, NULL, NULL); 15239 /* error is set below the switch */ 15240 } 15241 break; 15242 case DL_ENABMULTI_REQ: 15243 ip1dbg(("DL_ERROR_ACK to enabmulti\n")); 15244 15245 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15246 ill->ill_dlpi_multicast_state = IDS_FAILED; 15247 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 15248 ipif_t *ipif; 15249 15250 log = B_FALSE; 15251 printf("ip: joining multicasts failed (%d)" 15252 " on %s - will use link layer " 15253 "broadcasts for multicast\n", 15254 dlea->dl_errno, ill->ill_name); 15255 15256 /* 15257 * Set up the multicast mapping alone. 15258 * writer, so ok to access ill->ill_ipif 15259 * without any lock. 15260 */ 15261 ipif = ill->ill_ipif; 15262 mutex_enter(&ill->ill_phyint->phyint_lock); 15263 ill->ill_phyint->phyint_flags |= 15264 PHYI_MULTI_BCAST; 15265 mutex_exit(&ill->ill_phyint->phyint_lock); 15266 15267 if (!ill->ill_isv6) { 15268 (void) ipif_arp_setup_multicast(ipif, 15269 NULL); 15270 } else { 15271 (void) ipif_ndp_setup_multicast(ipif, 15272 NULL); 15273 } 15274 } 15275 freemsg(mp); /* Don't want to pass this up */ 15276 return; 15277 case DL_CAPABILITY_REQ: 15278 case DL_CONTROL_REQ: 15279 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 15280 "DL_CAPABILITY/CONTROL REQ\n")); 15281 ill_dlpi_done(ill, dlea->dl_error_primitive); 15282 ill->ill_dlpi_capab_state = IDS_FAILED; 15283 freemsg(mp); 15284 return; 15285 } 15286 /* 15287 * Note the error for IOCTL completion (mp1 is set when 15288 * ready to complete ioctl). If ill_ifname_pending_err is 15289 * set, an error occured during plumbing (ill_ifname_pending), 15290 * so we want to report that error. 15291 * 15292 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 15293 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 15294 * expected to get errack'd if the driver doesn't support 15295 * these flags (e.g. ethernet). log will be set to B_FALSE 15296 * if these error conditions are encountered. 15297 */ 15298 if (mp1 != NULL) { 15299 if (ill->ill_ifname_pending_err != 0) { 15300 err = ill->ill_ifname_pending_err; 15301 ill->ill_ifname_pending_err = 0; 15302 } else { 15303 err = dlea->dl_unix_errno ? 15304 dlea->dl_unix_errno : ENXIO; 15305 } 15306 /* 15307 * If we're plumbing an interface and an error hasn't already 15308 * been saved, set ill_ifname_pending_err to the error passed 15309 * up. Ignore the error if log is B_FALSE (see comment above). 15310 */ 15311 } else if (log && ill->ill_ifname_pending && 15312 ill->ill_ifname_pending_err == 0) { 15313 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 15314 dlea->dl_unix_errno : ENXIO; 15315 } 15316 15317 if (log) 15318 ip_dlpi_error(ill, dlea->dl_error_primitive, 15319 dlea->dl_errno, dlea->dl_unix_errno); 15320 break; 15321 case DL_CAPABILITY_ACK: { 15322 boolean_t reneg_flag = B_FALSE; 15323 /* Call a routine to handle this one. */ 15324 ill_dlpi_done(ill, DL_CAPABILITY_REQ); 15325 /* 15326 * Check if the ACK is due to renegotiation case since we 15327 * will need to send a new CAPABILITY_REQ later. 15328 */ 15329 if (ill->ill_dlpi_capab_state == IDS_RENEG) { 15330 /* This is the ack for a renogiation case */ 15331 reneg_flag = B_TRUE; 15332 ill->ill_dlpi_capab_state = IDS_UNKNOWN; 15333 } 15334 ill_capability_ack(ill, mp); 15335 if (reneg_flag) 15336 ill_capability_probe(ill); 15337 break; 15338 } 15339 case DL_CONTROL_ACK: 15340 /* We treat all of these as "fire and forget" */ 15341 ill_dlpi_done(ill, DL_CONTROL_REQ); 15342 break; 15343 case DL_INFO_ACK: 15344 /* Call a routine to handle this one. */ 15345 ill_dlpi_done(ill, DL_INFO_REQ); 15346 ip_ll_subnet_defaults(ill, mp); 15347 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 15348 return; 15349 case DL_BIND_ACK: 15350 /* 15351 * We should have an IOCTL waiting on this unless 15352 * sent by ill_dl_phys, in which case just return 15353 */ 15354 ill_dlpi_done(ill, DL_BIND_REQ); 15355 if (ill->ill_ifname_pending) 15356 break; 15357 15358 if (!ioctl_aborted) 15359 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15360 if (mp1 == NULL) 15361 break; 15362 ASSERT(connp != NULL); 15363 q = CONNP_TO_WQ(connp); 15364 15365 /* 15366 * We are exclusive. So nothing can change even after 15367 * we get the pending mp. If need be we can put it back 15368 * and restart, as in calling ipif_arp_up() below. 15369 */ 15370 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 15371 15372 mutex_enter(&ill->ill_lock); 15373 15374 ill->ill_dl_up = 1; 15375 15376 if ((info = ill->ill_nic_event_info) != NULL) { 15377 ip2dbg(("ip_rput_dlpi_writer: unexpected nic event %d " 15378 "attached for %s\n", info->hne_event, 15379 ill->ill_name)); 15380 if (info->hne_data != NULL) 15381 kmem_free(info->hne_data, info->hne_datalen); 15382 kmem_free(info, sizeof (hook_nic_event_t)); 15383 } 15384 15385 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 15386 if (info != NULL) { 15387 info->hne_nic = ill->ill_phyint->phyint_ifindex; 15388 info->hne_lif = 0; 15389 info->hne_event = NE_UP; 15390 info->hne_data = NULL; 15391 info->hne_datalen = 0; 15392 info->hne_family = ill->ill_isv6 ? ipv6 : ipv4; 15393 } else 15394 ip2dbg(("ip_rput_dlpi_writer: could not attach UP nic " 15395 "event information for %s (ENOMEM)\n", 15396 ill->ill_name)); 15397 15398 ill->ill_nic_event_info = info; 15399 15400 mutex_exit(&ill->ill_lock); 15401 15402 /* 15403 * Now bring up the resolver; when that is complete, we'll 15404 * create IREs. Note that we intentionally mirror what 15405 * ipif_up() would have done, because we got here by way of 15406 * ill_dl_up(), which stopped ipif_up()'s processing. 15407 */ 15408 if (ill->ill_isv6) { 15409 /* 15410 * v6 interfaces. 15411 * Unlike ARP which has to do another bind 15412 * and attach, once we get here we are 15413 * done with NDP. Except in the case of 15414 * ILLF_XRESOLV, in which case we send an 15415 * AR_INTERFACE_UP to the external resolver. 15416 * If all goes well, the ioctl will complete 15417 * in ip_rput(). If there's an error, we 15418 * complete it here. 15419 */ 15420 err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr, 15421 B_FALSE); 15422 if (err == 0) { 15423 if (ill->ill_flags & ILLF_XRESOLV) { 15424 mutex_enter(&connp->conn_lock); 15425 mutex_enter(&ill->ill_lock); 15426 success = ipsq_pending_mp_add( 15427 connp, ipif, q, mp1, 0); 15428 mutex_exit(&ill->ill_lock); 15429 mutex_exit(&connp->conn_lock); 15430 if (success) { 15431 err = ipif_resolver_up(ipif, 15432 Res_act_initial); 15433 if (err == EINPROGRESS) { 15434 freemsg(mp); 15435 return; 15436 } 15437 ASSERT(err != 0); 15438 mp1 = ipsq_pending_mp_get(ipsq, 15439 &connp); 15440 ASSERT(mp1 != NULL); 15441 } else { 15442 /* conn has started closing */ 15443 err = EINTR; 15444 } 15445 } else { /* Non XRESOLV interface */ 15446 (void) ipif_resolver_up(ipif, 15447 Res_act_initial); 15448 err = ipif_up_done_v6(ipif); 15449 } 15450 } 15451 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 15452 /* 15453 * ARP and other v4 external resolvers. 15454 * Leave the pending mblk intact so that 15455 * the ioctl completes in ip_rput(). 15456 */ 15457 mutex_enter(&connp->conn_lock); 15458 mutex_enter(&ill->ill_lock); 15459 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 15460 mutex_exit(&ill->ill_lock); 15461 mutex_exit(&connp->conn_lock); 15462 if (success) { 15463 err = ipif_resolver_up(ipif, Res_act_initial); 15464 if (err == EINPROGRESS) { 15465 freemsg(mp); 15466 return; 15467 } 15468 ASSERT(err != 0); 15469 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15470 } else { 15471 /* The conn has started closing */ 15472 err = EINTR; 15473 } 15474 } else { 15475 /* 15476 * This one is complete. Reply to pending ioctl. 15477 */ 15478 (void) ipif_resolver_up(ipif, Res_act_initial); 15479 err = ipif_up_done(ipif); 15480 } 15481 15482 if ((err == 0) && (ill->ill_up_ipifs)) { 15483 err = ill_up_ipifs(ill, q, mp1); 15484 if (err == EINPROGRESS) { 15485 freemsg(mp); 15486 return; 15487 } 15488 } 15489 15490 if (ill->ill_up_ipifs) { 15491 ill_group_cleanup(ill); 15492 } 15493 15494 break; 15495 case DL_NOTIFY_IND: { 15496 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 15497 ire_t *ire; 15498 boolean_t need_ire_walk_v4 = B_FALSE; 15499 boolean_t need_ire_walk_v6 = B_FALSE; 15500 15501 /* 15502 * Change the address everywhere we need to. 15503 * What we're getting here is a link-level addr or phys addr. 15504 * The new addr is at notify + notify->dl_addr_offset 15505 * The address length is notify->dl_addr_length; 15506 */ 15507 switch (notify->dl_notification) { 15508 case DL_NOTE_PHYS_ADDR: 15509 mp_hw = copyb(mp); 15510 if (mp_hw == NULL) { 15511 err = ENOMEM; 15512 break; 15513 } 15514 dlp = (union DL_primitives *)mp_hw->b_rptr; 15515 /* 15516 * We currently don't support changing 15517 * the token via DL_NOTIFY_IND. 15518 * When we do support it, we have to consider 15519 * what the implications are with respect to 15520 * the token and the link local address. 15521 */ 15522 mutex_enter(&ill->ill_lock); 15523 if (dlp->notify_ind.dl_data == 15524 DL_IPV6_LINK_LAYER_ADDR) { 15525 if (ill->ill_nd_lla_mp != NULL) 15526 freemsg(ill->ill_nd_lla_mp); 15527 ill->ill_nd_lla_mp = mp_hw; 15528 ill->ill_nd_lla = (uchar_t *)mp_hw->b_rptr + 15529 dlp->notify_ind.dl_addr_offset; 15530 ill->ill_nd_lla_len = 15531 dlp->notify_ind.dl_addr_length - 15532 ABS(ill->ill_sap_length); 15533 mutex_exit(&ill->ill_lock); 15534 break; 15535 } else if (dlp->notify_ind.dl_data == 15536 DL_CURR_PHYS_ADDR) { 15537 if (ill->ill_phys_addr_mp != NULL) 15538 freemsg(ill->ill_phys_addr_mp); 15539 ill->ill_phys_addr_mp = mp_hw; 15540 ill->ill_phys_addr = (uchar_t *)mp_hw->b_rptr + 15541 dlp->notify_ind.dl_addr_offset; 15542 ill->ill_phys_addr_length = 15543 dlp->notify_ind.dl_addr_length - 15544 ABS(ill->ill_sap_length); 15545 if (ill->ill_isv6 && 15546 !(ill->ill_flags & ILLF_XRESOLV)) { 15547 if (ill->ill_nd_lla_mp != NULL) 15548 freemsg(ill->ill_nd_lla_mp); 15549 ill->ill_nd_lla_mp = copyb(mp_hw); 15550 ill->ill_nd_lla = (uchar_t *) 15551 ill->ill_nd_lla_mp->b_rptr + 15552 dlp->notify_ind.dl_addr_offset; 15553 ill->ill_nd_lla_len = 15554 ill->ill_phys_addr_length; 15555 } 15556 } 15557 mutex_exit(&ill->ill_lock); 15558 /* 15559 * Send out gratuitous arp request for our new 15560 * hardware address. 15561 */ 15562 for (ipif = ill->ill_ipif; ipif != NULL; 15563 ipif = ipif->ipif_next) { 15564 if (!(ipif->ipif_flags & IPIF_UP)) 15565 continue; 15566 if (ill->ill_isv6) { 15567 ipif_ndp_down(ipif); 15568 /* 15569 * Set B_TRUE to enable 15570 * ipif_ndp_up() to send out 15571 * unsolicited advertisements. 15572 */ 15573 err = ipif_ndp_up(ipif, 15574 &ipif->ipif_v6lcl_addr, 15575 B_TRUE); 15576 if (err) { 15577 ip1dbg(( 15578 "ip_rput_dlpi_writer: " 15579 "Failed to update ndp " 15580 "err %d\n", err)); 15581 } 15582 } else { 15583 /* 15584 * IPv4 ARP case 15585 * 15586 * Set Res_act_move, as we only want 15587 * ipif_resolver_up to send an 15588 * AR_ENTRY_ADD request up to 15589 * ARP. 15590 */ 15591 err = ipif_resolver_up(ipif, 15592 Res_act_move); 15593 if (err) { 15594 ip1dbg(( 15595 "ip_rput_dlpi_writer: " 15596 "Failed to update arp " 15597 "err %d\n", err)); 15598 } 15599 } 15600 } 15601 /* 15602 * Allow "fall through" to the DL_NOTE_FASTPATH_FLUSH 15603 * case so that all old fastpath information can be 15604 * purged from IRE caches. 15605 */ 15606 /* FALLTHRU */ 15607 case DL_NOTE_FASTPATH_FLUSH: 15608 /* 15609 * Any fastpath probe sent henceforth will get the 15610 * new fp mp. So we first delete any ires that are 15611 * waiting for the fastpath. Then walk all ires and 15612 * delete the ire or delete the fp mp. In the case of 15613 * IRE_MIPRTUN and IRE_BROADCAST it is difficult to 15614 * recreate the ire's without going through a complex 15615 * ipif up/down dance. So we don't delete the ire 15616 * itself, but just the nce_fp_mp for these 2 ire's 15617 * In the case of the other ire's we delete the ire's 15618 * themselves. Access to nce_fp_mp is completely 15619 * protected by ire_lock for IRE_MIPRTUN and 15620 * IRE_BROADCAST. Deleting the ire is preferable in the 15621 * other cases for performance. 15622 */ 15623 if (ill->ill_isv6) { 15624 nce_fastpath_list_dispatch(ill, NULL, NULL); 15625 ndp_walk(ill, (pfi_t)ndp_fastpath_flush, 15626 NULL); 15627 } else { 15628 ire_fastpath_list_dispatch(ill, NULL, NULL); 15629 ire_walk_ill_v4(MATCH_IRE_WQ | MATCH_IRE_TYPE, 15630 IRE_CACHE | IRE_BROADCAST, 15631 ire_fastpath_flush, NULL, ill); 15632 mutex_enter(&ire_mrtun_lock); 15633 if (ire_mrtun_count != 0) { 15634 mutex_exit(&ire_mrtun_lock); 15635 ire_walk_ill_mrtun(MATCH_IRE_WQ, 15636 IRE_MIPRTUN, ire_fastpath_flush, 15637 NULL, ill); 15638 } else { 15639 mutex_exit(&ire_mrtun_lock); 15640 } 15641 } 15642 break; 15643 case DL_NOTE_SDU_SIZE: 15644 /* 15645 * Change the MTU size of the interface, of all 15646 * attached ipif's, and of all relevant ire's. The 15647 * new value's a uint32_t at notify->dl_data. 15648 * Mtu change Vs. new ire creation - protocol below. 15649 * 15650 * a Mark the ipif as IPIF_CHANGING. 15651 * b Set the new mtu in the ipif. 15652 * c Change the ire_max_frag on all affected ires 15653 * d Unmark the IPIF_CHANGING 15654 * 15655 * To see how the protocol works, assume an interface 15656 * route is also being added simultaneously by 15657 * ip_rt_add and let 'ipif' be the ipif referenced by 15658 * the ire. If the ire is created before step a, 15659 * it will be cleaned up by step c. If the ire is 15660 * created after step d, it will see the new value of 15661 * ipif_mtu. Any attempt to create the ire between 15662 * steps a to d will fail because of the IPIF_CHANGING 15663 * flag. Note that ire_create() is passed a pointer to 15664 * the ipif_mtu, and not the value. During ire_add 15665 * under the bucket lock, the ire_max_frag of the 15666 * new ire being created is set from the ipif/ire from 15667 * which it is being derived. 15668 */ 15669 mutex_enter(&ill->ill_lock); 15670 ill->ill_max_frag = (uint_t)notify->dl_data; 15671 15672 /* 15673 * If an SIOCSLIFLNKINFO has changed the ill_max_mtu 15674 * leave it alone 15675 */ 15676 if (ill->ill_mtu_userspecified) { 15677 mutex_exit(&ill->ill_lock); 15678 break; 15679 } 15680 ill->ill_max_mtu = ill->ill_max_frag; 15681 if (ill->ill_isv6) { 15682 if (ill->ill_max_mtu < IPV6_MIN_MTU) 15683 ill->ill_max_mtu = IPV6_MIN_MTU; 15684 } else { 15685 if (ill->ill_max_mtu < IP_MIN_MTU) 15686 ill->ill_max_mtu = IP_MIN_MTU; 15687 } 15688 for (ipif = ill->ill_ipif; ipif != NULL; 15689 ipif = ipif->ipif_next) { 15690 /* 15691 * Don't override the mtu if the user 15692 * has explicitly set it. 15693 */ 15694 if (ipif->ipif_flags & IPIF_FIXEDMTU) 15695 continue; 15696 ipif->ipif_mtu = (uint_t)notify->dl_data; 15697 if (ipif->ipif_isv6) 15698 ire = ipif_to_ire_v6(ipif); 15699 else 15700 ire = ipif_to_ire(ipif); 15701 if (ire != NULL) { 15702 ire->ire_max_frag = ipif->ipif_mtu; 15703 ire_refrele(ire); 15704 } 15705 if (ipif->ipif_flags & IPIF_UP) { 15706 if (ill->ill_isv6) 15707 need_ire_walk_v6 = B_TRUE; 15708 else 15709 need_ire_walk_v4 = B_TRUE; 15710 } 15711 } 15712 mutex_exit(&ill->ill_lock); 15713 if (need_ire_walk_v4) 15714 ire_walk_v4(ill_mtu_change, (char *)ill, 15715 ALL_ZONES); 15716 if (need_ire_walk_v6) 15717 ire_walk_v6(ill_mtu_change, (char *)ill, 15718 ALL_ZONES); 15719 break; 15720 case DL_NOTE_LINK_UP: 15721 case DL_NOTE_LINK_DOWN: { 15722 /* 15723 * We are writer. ill / phyint / ipsq assocs stable. 15724 * The RUNNING flag reflects the state of the link. 15725 */ 15726 phyint_t *phyint = ill->ill_phyint; 15727 uint64_t new_phyint_flags; 15728 boolean_t changed = B_FALSE; 15729 boolean_t went_up; 15730 15731 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 15732 mutex_enter(&phyint->phyint_lock); 15733 new_phyint_flags = went_up ? 15734 phyint->phyint_flags | PHYI_RUNNING : 15735 phyint->phyint_flags & ~PHYI_RUNNING; 15736 if (new_phyint_flags != phyint->phyint_flags) { 15737 phyint->phyint_flags = new_phyint_flags; 15738 changed = B_TRUE; 15739 } 15740 mutex_exit(&phyint->phyint_lock); 15741 /* 15742 * ill_restart_dad handles the DAD restart and routing 15743 * socket notification logic. 15744 */ 15745 if (changed) { 15746 ill_restart_dad(phyint->phyint_illv4, went_up); 15747 ill_restart_dad(phyint->phyint_illv6, went_up); 15748 } 15749 break; 15750 } 15751 case DL_NOTE_PROMISC_ON_PHYS: 15752 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 15753 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 15754 mutex_enter(&ill->ill_lock); 15755 ill->ill_promisc_on_phys = B_TRUE; 15756 mutex_exit(&ill->ill_lock); 15757 break; 15758 case DL_NOTE_PROMISC_OFF_PHYS: 15759 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 15760 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 15761 mutex_enter(&ill->ill_lock); 15762 ill->ill_promisc_on_phys = B_FALSE; 15763 mutex_exit(&ill->ill_lock); 15764 break; 15765 case DL_NOTE_CAPAB_RENEG: 15766 /* 15767 * Something changed on the driver side. 15768 * It wants us to renegotiate the capabilities 15769 * on this ill. The most likely cause is the 15770 * aggregation interface under us where a 15771 * port got added or went away. 15772 * 15773 * We reset the capabilities and set the 15774 * state to IDS_RENG so that when the ack 15775 * comes back, we can start the 15776 * renegotiation process. 15777 */ 15778 ill_capability_reset(ill); 15779 ill->ill_dlpi_capab_state = IDS_RENEG; 15780 break; 15781 default: 15782 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 15783 "type 0x%x for DL_NOTIFY_IND\n", 15784 notify->dl_notification)); 15785 break; 15786 } 15787 15788 /* 15789 * As this is an asynchronous operation, we 15790 * should not call ill_dlpi_done 15791 */ 15792 break; 15793 } 15794 case DL_NOTIFY_ACK: { 15795 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 15796 15797 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 15798 ill->ill_note_link = 1; 15799 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15800 break; 15801 } 15802 case DL_PHYS_ADDR_ACK: { 15803 /* 15804 * We should have an IOCTL waiting on this when request 15805 * sent by ill_dl_phys. 15806 * However, ill_dl_phys was called on an ill queue (from 15807 * SIOCSLIFNAME), thus conn_pending_ill is not set. But the 15808 * ioctl is known to be pending on ill_wq. 15809 * There are two additional phys_addr_req's sent to the 15810 * driver to get the token and lla. ill_phys_addr_pend 15811 * keeps track of the last one sent so we know which 15812 * response we are dealing with. ill_dlpi_done will 15813 * update ill_phys_addr_pend when it sends the next req. 15814 * We don't complete the IOCTL until all three DL_PARs 15815 * have been attempted. 15816 * 15817 * We don't need any lock to update ill_nd_lla* fields, 15818 * since the ill is not yet up, We grab the lock just 15819 * for uniformity with other code that accesses ill_nd_lla. 15820 */ 15821 physaddr_req = ill->ill_phys_addr_pend; 15822 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15823 if (physaddr_req == DL_IPV6_TOKEN || 15824 physaddr_req == DL_IPV6_LINK_LAYER_ADDR) { 15825 if (physaddr_req == DL_IPV6_TOKEN) { 15826 /* 15827 * bcopy to low-order bits of ill_token 15828 * 15829 * XXX Temporary hack - currently, 15830 * all known tokens are 64 bits, 15831 * so I'll cheat for the moment. 15832 */ 15833 dlp = (union DL_primitives *)mp->b_rptr; 15834 15835 mutex_enter(&ill->ill_lock); 15836 bcopy((uchar_t *)(mp->b_rptr + 15837 dlp->physaddr_ack.dl_addr_offset), 15838 (void *)&ill->ill_token.s6_addr32[2], 15839 dlp->physaddr_ack.dl_addr_length); 15840 ill->ill_token_length = 15841 dlp->physaddr_ack.dl_addr_length; 15842 mutex_exit(&ill->ill_lock); 15843 } else { 15844 ASSERT(ill->ill_nd_lla_mp == NULL); 15845 mp_hw = copyb(mp); 15846 if (mp_hw == NULL) { 15847 err = ENOMEM; 15848 break; 15849 } 15850 dlp = (union DL_primitives *)mp_hw->b_rptr; 15851 mutex_enter(&ill->ill_lock); 15852 ill->ill_nd_lla_mp = mp_hw; 15853 ill->ill_nd_lla = (uchar_t *)mp_hw->b_rptr + 15854 dlp->physaddr_ack.dl_addr_offset; 15855 ill->ill_nd_lla_len = 15856 dlp->physaddr_ack.dl_addr_length; 15857 mutex_exit(&ill->ill_lock); 15858 } 15859 break; 15860 } 15861 ASSERT(physaddr_req == DL_CURR_PHYS_ADDR); 15862 ASSERT(ill->ill_phys_addr_mp == NULL); 15863 if (!ill->ill_ifname_pending) 15864 break; 15865 ill->ill_ifname_pending = 0; 15866 if (!ioctl_aborted) 15867 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15868 if (mp1 != NULL) { 15869 ASSERT(connp == NULL); 15870 q = ill->ill_wq; 15871 } 15872 /* 15873 * If any error acks received during the plumbing sequence, 15874 * ill_ifname_pending_err will be set. Break out and send up 15875 * the error to the pending ioctl. 15876 */ 15877 if (ill->ill_ifname_pending_err != 0) { 15878 err = ill->ill_ifname_pending_err; 15879 ill->ill_ifname_pending_err = 0; 15880 break; 15881 } 15882 /* 15883 * Get the interface token. If the zeroth interface 15884 * address is zero then set the address to the link local 15885 * address 15886 */ 15887 mp_hw = copyb(mp); 15888 if (mp_hw == NULL) { 15889 err = ENOMEM; 15890 break; 15891 } 15892 dlp = (union DL_primitives *)mp_hw->b_rptr; 15893 ill->ill_phys_addr_mp = mp_hw; 15894 ill->ill_phys_addr = (uchar_t *)mp_hw->b_rptr + 15895 dlp->physaddr_ack.dl_addr_offset; 15896 if (dlp->physaddr_ack.dl_addr_length == 0 || 15897 ill->ill_phys_addr_length == 0 || 15898 ill->ill_phys_addr_length == IP_ADDR_LEN) { 15899 /* 15900 * Compatibility: atun driver returns a length of 0. 15901 * ipdptp has an ill_phys_addr_length of zero(from 15902 * DL_BIND_ACK) but a non-zero length here. 15903 * ipd has an ill_phys_addr_length of 4(from 15904 * DL_BIND_ACK) but a non-zero length here. 15905 */ 15906 ill->ill_phys_addr = NULL; 15907 } else if (dlp->physaddr_ack.dl_addr_length != 15908 ill->ill_phys_addr_length) { 15909 ip0dbg(("DL_PHYS_ADDR_ACK: " 15910 "Address length mismatch %d %d\n", 15911 dlp->physaddr_ack.dl_addr_length, 15912 ill->ill_phys_addr_length)); 15913 err = EINVAL; 15914 break; 15915 } 15916 mutex_enter(&ill->ill_lock); 15917 if (ill->ill_nd_lla_mp == NULL) { 15918 ill->ill_nd_lla_mp = copyb(mp_hw); 15919 if (ill->ill_nd_lla_mp == NULL) { 15920 err = ENOMEM; 15921 mutex_exit(&ill->ill_lock); 15922 break; 15923 } 15924 ill->ill_nd_lla = 15925 (uchar_t *)ill->ill_nd_lla_mp->b_rptr + 15926 dlp->physaddr_ack.dl_addr_offset; 15927 ill->ill_nd_lla_len = ill->ill_phys_addr_length; 15928 } 15929 mutex_exit(&ill->ill_lock); 15930 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 15931 (void) ill_setdefaulttoken(ill); 15932 15933 /* 15934 * If the ill zero interface has a zero address assign 15935 * it the proper link local address. 15936 */ 15937 ASSERT(ill->ill_ipif->ipif_id == 0); 15938 if (ipif != NULL && 15939 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) 15940 (void) ipif_setlinklocal(ipif); 15941 break; 15942 } 15943 case DL_OK_ACK: 15944 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 15945 dlpi_prim_str((int)dloa->dl_correct_primitive), 15946 dloa->dl_correct_primitive)); 15947 switch (dloa->dl_correct_primitive) { 15948 case DL_UNBIND_REQ: 15949 case DL_ATTACH_REQ: 15950 case DL_DETACH_REQ: 15951 ill_dlpi_done(ill, dloa->dl_correct_primitive); 15952 break; 15953 } 15954 break; 15955 default: 15956 break; 15957 } 15958 15959 freemsg(mp); 15960 if (mp1) { 15961 struct iocblk *iocp; 15962 int mode; 15963 15964 /* 15965 * Complete the waiting IOCTL. For SIOCLIFADDIF or 15966 * SIOCSLIFNAME do a copyout. 15967 */ 15968 iocp = (struct iocblk *)mp1->b_rptr; 15969 15970 if (iocp->ioc_cmd == SIOCLIFADDIF || 15971 iocp->ioc_cmd == SIOCSLIFNAME) 15972 mode = COPYOUT; 15973 else 15974 mode = NO_COPYOUT; 15975 /* 15976 * The ioctl must complete now without EINPROGRESS 15977 * since ipsq_pending_mp_get has removed the ioctl mblk 15978 * from ipsq_pending_mp. Otherwise the ioctl will be 15979 * stuck for ever in the ipsq. 15980 */ 15981 ASSERT(err != EINPROGRESS); 15982 ip_ioctl_finish(q, mp1, err, mode, ipif, ipsq); 15983 15984 } 15985 } 15986 15987 /* 15988 * ip_rput_other is called by ip_rput to handle messages modifying the global 15989 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 15990 */ 15991 /* ARGSUSED */ 15992 void 15993 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15994 { 15995 ill_t *ill; 15996 struct iocblk *iocp; 15997 mblk_t *mp1; 15998 conn_t *connp = NULL; 15999 16000 ip1dbg(("ip_rput_other ")); 16001 ill = (ill_t *)q->q_ptr; 16002 /* 16003 * This routine is not a writer in the case of SIOCGTUNPARAM 16004 * in which case ipsq is NULL. 16005 */ 16006 if (ipsq != NULL) { 16007 ASSERT(IAM_WRITER_IPSQ(ipsq)); 16008 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 16009 } 16010 16011 switch (mp->b_datap->db_type) { 16012 case M_ERROR: 16013 case M_HANGUP: 16014 /* 16015 * The device has a problem. We force the ILL down. It can 16016 * be brought up again manually using SIOCSIFFLAGS (via 16017 * ifconfig or equivalent). 16018 */ 16019 ASSERT(ipsq != NULL); 16020 if (mp->b_rptr < mp->b_wptr) 16021 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 16022 if (ill->ill_error == 0) 16023 ill->ill_error = ENXIO; 16024 if (!ill_down_start(q, mp)) 16025 return; 16026 ipif_all_down_tail(ipsq, q, mp, NULL); 16027 break; 16028 case M_IOCACK: 16029 iocp = (struct iocblk *)mp->b_rptr; 16030 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 16031 switch (iocp->ioc_cmd) { 16032 case SIOCSTUNPARAM: 16033 case OSIOCSTUNPARAM: 16034 ASSERT(ipsq != NULL); 16035 /* 16036 * Finish socket ioctl passed through to tun. 16037 * We should have an IOCTL waiting on this. 16038 */ 16039 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16040 if (ill->ill_isv6) { 16041 struct iftun_req *ta; 16042 16043 /* 16044 * if a source or destination is 16045 * being set, try and set the link 16046 * local address for the tunnel 16047 */ 16048 ta = (struct iftun_req *)mp->b_cont-> 16049 b_cont->b_rptr; 16050 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 16051 ipif_set_tun_llink(ill, ta); 16052 } 16053 16054 } 16055 if (mp1 != NULL) { 16056 /* 16057 * Now copy back the b_next/b_prev used by 16058 * mi code for the mi_copy* functions. 16059 * See ip_sioctl_tunparam() for the reason. 16060 * Also protect against missing b_cont. 16061 */ 16062 if (mp->b_cont != NULL) { 16063 mp->b_cont->b_next = 16064 mp1->b_cont->b_next; 16065 mp->b_cont->b_prev = 16066 mp1->b_cont->b_prev; 16067 } 16068 inet_freemsg(mp1); 16069 ASSERT(ipsq->ipsq_current_ipif != NULL); 16070 ASSERT(connp != NULL); 16071 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16072 iocp->ioc_error, NO_COPYOUT, 16073 ipsq->ipsq_current_ipif, ipsq); 16074 } else { 16075 ASSERT(connp == NULL); 16076 putnext(q, mp); 16077 } 16078 break; 16079 case SIOCGTUNPARAM: 16080 case OSIOCGTUNPARAM: 16081 /* 16082 * This is really M_IOCDATA from the tunnel driver. 16083 * convert back and complete the ioctl. 16084 * We should have an IOCTL waiting on this. 16085 */ 16086 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 16087 if (mp1) { 16088 /* 16089 * Now copy back the b_next/b_prev used by 16090 * mi code for the mi_copy* functions. 16091 * See ip_sioctl_tunparam() for the reason. 16092 * Also protect against missing b_cont. 16093 */ 16094 if (mp->b_cont != NULL) { 16095 mp->b_cont->b_next = 16096 mp1->b_cont->b_next; 16097 mp->b_cont->b_prev = 16098 mp1->b_cont->b_prev; 16099 } 16100 inet_freemsg(mp1); 16101 if (iocp->ioc_error == 0) 16102 mp->b_datap->db_type = M_IOCDATA; 16103 ASSERT(connp != NULL); 16104 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16105 iocp->ioc_error, COPYOUT, NULL, NULL); 16106 } else { 16107 ASSERT(connp == NULL); 16108 putnext(q, mp); 16109 } 16110 break; 16111 default: 16112 break; 16113 } 16114 break; 16115 case M_IOCNAK: 16116 iocp = (struct iocblk *)mp->b_rptr; 16117 16118 switch (iocp->ioc_cmd) { 16119 int mode; 16120 ipif_t *ipif; 16121 16122 case DL_IOC_HDR_INFO: 16123 /* 16124 * If this was the first attempt turn of the 16125 * fastpath probing. 16126 */ 16127 mutex_enter(&ill->ill_lock); 16128 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 16129 ill->ill_dlpi_fastpath_state = IDS_FAILED; 16130 mutex_exit(&ill->ill_lock); 16131 ill_fastpath_nack(ill); 16132 ip1dbg(("ip_rput: DLPI fastpath off on " 16133 "interface %s\n", 16134 ill->ill_name)); 16135 } else { 16136 mutex_exit(&ill->ill_lock); 16137 } 16138 freemsg(mp); 16139 break; 16140 case SIOCSTUNPARAM: 16141 case OSIOCSTUNPARAM: 16142 ASSERT(ipsq != NULL); 16143 /* 16144 * Finish socket ioctl passed through to tun 16145 * We should have an IOCTL waiting on this. 16146 */ 16147 /* FALLTHRU */ 16148 case SIOCGTUNPARAM: 16149 case OSIOCGTUNPARAM: 16150 /* 16151 * This is really M_IOCDATA from the tunnel driver. 16152 * convert back and complete the ioctl. 16153 * We should have an IOCTL waiting on this. 16154 */ 16155 if (iocp->ioc_cmd == SIOCGTUNPARAM || 16156 iocp->ioc_cmd == OSIOCGTUNPARAM) { 16157 mp1 = ill_pending_mp_get(ill, &connp, 16158 iocp->ioc_id); 16159 mode = COPYOUT; 16160 ipsq = NULL; 16161 ipif = NULL; 16162 } else { 16163 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16164 mode = NO_COPYOUT; 16165 ASSERT(ipsq->ipsq_current_ipif != NULL); 16166 ipif = ipsq->ipsq_current_ipif; 16167 } 16168 if (mp1 != NULL) { 16169 /* 16170 * Now copy back the b_next/b_prev used by 16171 * mi code for the mi_copy* functions. 16172 * See ip_sioctl_tunparam() for the reason. 16173 * Also protect against missing b_cont. 16174 */ 16175 if (mp->b_cont != NULL) { 16176 mp->b_cont->b_next = 16177 mp1->b_cont->b_next; 16178 mp->b_cont->b_prev = 16179 mp1->b_cont->b_prev; 16180 } 16181 inet_freemsg(mp1); 16182 if (iocp->ioc_error == 0) 16183 iocp->ioc_error = EINVAL; 16184 ASSERT(connp != NULL); 16185 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16186 iocp->ioc_error, mode, ipif, ipsq); 16187 } else { 16188 ASSERT(connp == NULL); 16189 putnext(q, mp); 16190 } 16191 break; 16192 default: 16193 break; 16194 } 16195 default: 16196 break; 16197 } 16198 } 16199 16200 /* 16201 * NOTE : This function does not ire_refrele the ire argument passed in. 16202 * 16203 * IPQoS notes 16204 * IP policy is invoked twice for a forwarded packet, once on the read side 16205 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 16206 * enabled. An additional parameter, in_ill, has been added for this purpose. 16207 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 16208 * because ip_mroute drops this information. 16209 * 16210 */ 16211 void 16212 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 16213 { 16214 uint32_t pkt_len; 16215 queue_t *q; 16216 uint32_t sum; 16217 #define rptr ((uchar_t *)ipha) 16218 uint32_t max_frag; 16219 uint32_t ill_index; 16220 ill_t *out_ill; 16221 16222 /* Get the ill_index of the incoming ILL */ 16223 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 16224 16225 /* Initiate Read side IPPF processing */ 16226 if (IPP_ENABLED(IPP_FWD_IN)) { 16227 ip_process(IPP_FWD_IN, &mp, ill_index); 16228 if (mp == NULL) { 16229 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 16230 "during IPPF processing\n")); 16231 return; 16232 } 16233 } 16234 16235 pkt_len = ntohs(ipha->ipha_length); 16236 16237 /* Adjust the checksum to reflect the ttl decrement. */ 16238 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 16239 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 16240 16241 if (ipha->ipha_ttl-- <= 1) { 16242 if (ip_csum_hdr(ipha)) { 16243 BUMP_MIB(&ip_mib, ipInCksumErrs); 16244 goto drop_pkt; 16245 } 16246 /* 16247 * Note: ire_stq this will be NULL for multicast 16248 * datagrams using the long path through arp (the IRE 16249 * is not an IRE_CACHE). This should not cause 16250 * problems since we don't generate ICMP errors for 16251 * multicast packets. 16252 */ 16253 q = ire->ire_stq; 16254 if (q != NULL) { 16255 /* Sent by forwarding path, and router is global zone */ 16256 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, 16257 GLOBAL_ZONEID); 16258 } else 16259 freemsg(mp); 16260 return; 16261 } 16262 16263 /* 16264 * Don't forward if the interface is down 16265 */ 16266 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 16267 BUMP_MIB(&ip_mib, ipInDiscards); 16268 ip2dbg(("ip_rput_forward:interface is down\n")); 16269 goto drop_pkt; 16270 } 16271 16272 /* Get the ill_index of the outgoing ILL */ 16273 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 16274 16275 out_ill = ire->ire_ipif->ipif_ill; 16276 16277 DTRACE_PROBE4(ip4__forwarding__start, 16278 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16279 16280 FW_HOOKS(ip4_forwarding_event, ipv4firewall_forwarding, 16281 MSG_FWCOOKED_FORWARD, in_ill, out_ill, ipha, mp, mp); 16282 16283 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 16284 16285 if (mp == NULL) 16286 return; 16287 pkt_len = ntohs(ipha->ipha_length); 16288 16289 if (is_system_labeled()) { 16290 mblk_t *mp1; 16291 16292 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 16293 BUMP_MIB(&ip_mib, ipForwProhibits); 16294 goto drop_pkt; 16295 } 16296 /* Size may have changed */ 16297 mp = mp1; 16298 ipha = (ipha_t *)mp->b_rptr; 16299 pkt_len = ntohs(ipha->ipha_length); 16300 } 16301 16302 /* Check if there are options to update */ 16303 if (!IS_SIMPLE_IPH(ipha)) { 16304 if (ip_csum_hdr(ipha)) { 16305 BUMP_MIB(&ip_mib, ipInCksumErrs); 16306 goto drop_pkt; 16307 } 16308 if (ip_rput_forward_options(mp, ipha, ire)) { 16309 return; 16310 } 16311 16312 ipha->ipha_hdr_checksum = 0; 16313 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 16314 } 16315 max_frag = ire->ire_max_frag; 16316 if (pkt_len > max_frag) { 16317 /* 16318 * It needs fragging on its way out. We haven't 16319 * verified the header checksum yet. Since we 16320 * are going to put a surely good checksum in the 16321 * outgoing header, we have to make sure that it 16322 * was good coming in. 16323 */ 16324 if (ip_csum_hdr(ipha)) { 16325 BUMP_MIB(&ip_mib, ipInCksumErrs); 16326 goto drop_pkt; 16327 } 16328 /* Initiate Write side IPPF processing */ 16329 if (IPP_ENABLED(IPP_FWD_OUT)) { 16330 ip_process(IPP_FWD_OUT, &mp, ill_index); 16331 if (mp == NULL) { 16332 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 16333 " during IPPF processing\n")); 16334 return; 16335 } 16336 } 16337 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID); 16338 ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); 16339 return; 16340 } 16341 16342 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 16343 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, mblk_t *, mp); 16344 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 16345 MSG_FWCOOKED_OUT, NULL, out_ill, ipha, mp, mp); 16346 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 16347 if (mp == NULL) 16348 return; 16349 16350 mp->b_prev = (mblk_t *)IPP_FWD_OUT; 16351 ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); 16352 (void) ip_xmit_v4(mp, ire, NULL, B_FALSE); 16353 /* ip_xmit_v4 always consumes the packet */ 16354 return; 16355 16356 drop_pkt:; 16357 ip1dbg(("ip_rput_forward: drop pkt\n")); 16358 freemsg(mp); 16359 #undef rptr 16360 } 16361 16362 void 16363 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 16364 { 16365 ire_t *ire; 16366 16367 ASSERT(!ipif->ipif_isv6); 16368 /* 16369 * Find an IRE which matches the destination and the outgoing 16370 * queue in the cache table. All we need is an IRE_CACHE which 16371 * is pointing at ipif->ipif_ill. If it is part of some ill group, 16372 * then it is enough to have some IRE_CACHE in the group. 16373 */ 16374 if (ipif->ipif_flags & IPIF_POINTOPOINT) 16375 dst = ipif->ipif_pp_dst_addr; 16376 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MBLK_GETLABEL(mp), 16377 MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR); 16378 if (ire == NULL) { 16379 /* 16380 * Mark this packet to make it be delivered to 16381 * ip_rput_forward after the new ire has been 16382 * created. 16383 */ 16384 mp->b_prev = NULL; 16385 mp->b_next = mp; 16386 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 16387 NULL, 0, GLOBAL_ZONEID); 16388 } else { 16389 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 16390 IRE_REFRELE(ire); 16391 } 16392 } 16393 16394 /* Update any source route, record route or timestamp options */ 16395 static int 16396 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire) 16397 { 16398 ipoptp_t opts; 16399 uchar_t *opt; 16400 uint8_t optval; 16401 uint8_t optlen; 16402 ipaddr_t dst; 16403 uint32_t ts; 16404 ire_t *dst_ire = NULL; 16405 ire_t *tmp_ire = NULL; 16406 timestruc_t now; 16407 16408 ip2dbg(("ip_rput_forward_options\n")); 16409 dst = ipha->ipha_dst; 16410 for (optval = ipoptp_first(&opts, ipha); 16411 optval != IPOPT_EOL; 16412 optval = ipoptp_next(&opts)) { 16413 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 16414 opt = opts.ipoptp_cur; 16415 optlen = opts.ipoptp_len; 16416 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 16417 optval, opts.ipoptp_len)); 16418 switch (optval) { 16419 uint32_t off; 16420 case IPOPT_SSRR: 16421 case IPOPT_LSRR: 16422 /* Check if adminstratively disabled */ 16423 if (!ip_forward_src_routed) { 16424 BUMP_MIB(&ip_mib, ipForwProhibits); 16425 if (ire->ire_stq != NULL) { 16426 /* 16427 * Sent by forwarding path, and router 16428 * is global zone 16429 */ 16430 icmp_unreachable(ire->ire_stq, mp, 16431 ICMP_SOURCE_ROUTE_FAILED, 16432 GLOBAL_ZONEID); 16433 } else { 16434 ip0dbg(("ip_rput_forward_options: " 16435 "unable to send unreach\n")); 16436 freemsg(mp); 16437 } 16438 return (-1); 16439 } 16440 16441 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16442 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 16443 if (dst_ire == NULL) { 16444 /* 16445 * Must be partial since ip_rput_options 16446 * checked for strict. 16447 */ 16448 break; 16449 } 16450 off = opt[IPOPT_OFFSET]; 16451 off--; 16452 redo_srr: 16453 if (optlen < IP_ADDR_LEN || 16454 off > optlen - IP_ADDR_LEN) { 16455 /* End of source route */ 16456 ip1dbg(( 16457 "ip_rput_forward_options: end of SR\n")); 16458 ire_refrele(dst_ire); 16459 break; 16460 } 16461 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16462 bcopy(&ire->ire_src_addr, (char *)opt + off, 16463 IP_ADDR_LEN); 16464 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 16465 ntohl(dst))); 16466 16467 /* 16468 * Check if our address is present more than 16469 * once as consecutive hops in source route. 16470 */ 16471 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16472 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 16473 if (tmp_ire != NULL) { 16474 ire_refrele(tmp_ire); 16475 off += IP_ADDR_LEN; 16476 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16477 goto redo_srr; 16478 } 16479 ipha->ipha_dst = dst; 16480 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16481 ire_refrele(dst_ire); 16482 break; 16483 case IPOPT_RR: 16484 off = opt[IPOPT_OFFSET]; 16485 off--; 16486 if (optlen < IP_ADDR_LEN || 16487 off > optlen - IP_ADDR_LEN) { 16488 /* No more room - ignore */ 16489 ip1dbg(( 16490 "ip_rput_forward_options: end of RR\n")); 16491 break; 16492 } 16493 bcopy(&ire->ire_src_addr, (char *)opt + off, 16494 IP_ADDR_LEN); 16495 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16496 break; 16497 case IPOPT_TS: 16498 /* Insert timestamp if there is room */ 16499 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16500 case IPOPT_TS_TSONLY: 16501 off = IPOPT_TS_TIMELEN; 16502 break; 16503 case IPOPT_TS_PRESPEC: 16504 case IPOPT_TS_PRESPEC_RFC791: 16505 /* Verify that the address matched */ 16506 off = opt[IPOPT_OFFSET] - 1; 16507 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16508 dst_ire = ire_ctable_lookup(dst, 0, 16509 IRE_LOCAL, NULL, ALL_ZONES, NULL, 16510 MATCH_IRE_TYPE); 16511 16512 if (dst_ire == NULL) { 16513 /* Not for us */ 16514 break; 16515 } 16516 ire_refrele(dst_ire); 16517 /* FALLTHRU */ 16518 case IPOPT_TS_TSANDADDR: 16519 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16520 break; 16521 default: 16522 /* 16523 * ip_*put_options should have already 16524 * dropped this packet. 16525 */ 16526 cmn_err(CE_PANIC, "ip_rput_forward_options: " 16527 "unknown IT - bug in ip_rput_options?\n"); 16528 return (0); /* Keep "lint" happy */ 16529 } 16530 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 16531 /* Increase overflow counter */ 16532 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 16533 opt[IPOPT_POS_OV_FLG] = 16534 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 16535 (off << 4)); 16536 break; 16537 } 16538 off = opt[IPOPT_OFFSET] - 1; 16539 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16540 case IPOPT_TS_PRESPEC: 16541 case IPOPT_TS_PRESPEC_RFC791: 16542 case IPOPT_TS_TSANDADDR: 16543 bcopy(&ire->ire_src_addr, 16544 (char *)opt + off, IP_ADDR_LEN); 16545 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16546 /* FALLTHRU */ 16547 case IPOPT_TS_TSONLY: 16548 off = opt[IPOPT_OFFSET] - 1; 16549 /* Compute # of milliseconds since midnight */ 16550 gethrestime(&now); 16551 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 16552 now.tv_nsec / (NANOSEC / MILLISEC); 16553 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 16554 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 16555 break; 16556 } 16557 break; 16558 } 16559 } 16560 return (0); 16561 } 16562 16563 /* 16564 * This is called after processing at least one of AH/ESP headers. 16565 * 16566 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 16567 * the actual, physical interface on which the packet was received, 16568 * but, when ip_strict_dst_multihoming is set to 1, could be the 16569 * interface which had the ipha_dst configured when the packet went 16570 * through ip_rput. The ill_index corresponding to the recv_ill 16571 * is saved in ipsec_in_rill_index 16572 */ 16573 void 16574 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 16575 { 16576 mblk_t *mp; 16577 ipaddr_t dst; 16578 in6_addr_t *v6dstp; 16579 ipha_t *ipha; 16580 ip6_t *ip6h; 16581 ipsec_in_t *ii; 16582 boolean_t ill_need_rele = B_FALSE; 16583 boolean_t rill_need_rele = B_FALSE; 16584 boolean_t ire_need_rele = B_FALSE; 16585 16586 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 16587 ASSERT(ii->ipsec_in_ill_index != 0); 16588 16589 mp = ipsec_mp->b_cont; 16590 ASSERT(mp != NULL); 16591 16592 16593 if (ill == NULL) { 16594 ASSERT(recv_ill == NULL); 16595 /* 16596 * We need to get the original queue on which ip_rput_local 16597 * or ip_rput_data_v6 was called. 16598 */ 16599 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 16600 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL); 16601 ill_need_rele = B_TRUE; 16602 16603 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 16604 recv_ill = ill_lookup_on_ifindex( 16605 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 16606 NULL, NULL, NULL, NULL); 16607 rill_need_rele = B_TRUE; 16608 } else { 16609 recv_ill = ill; 16610 } 16611 16612 if ((ill == NULL) || (recv_ill == NULL)) { 16613 ip0dbg(("ip_fanout_proto_again: interface " 16614 "disappeared\n")); 16615 if (ill != NULL) 16616 ill_refrele(ill); 16617 if (recv_ill != NULL) 16618 ill_refrele(recv_ill); 16619 freemsg(ipsec_mp); 16620 return; 16621 } 16622 } 16623 16624 ASSERT(ill != NULL && recv_ill != NULL); 16625 16626 if (mp->b_datap->db_type == M_CTL) { 16627 /* 16628 * AH/ESP is returning the ICMP message after 16629 * removing their headers. Fanout again till 16630 * it gets to the right protocol. 16631 */ 16632 if (ii->ipsec_in_v4) { 16633 icmph_t *icmph; 16634 int iph_hdr_length; 16635 int hdr_length; 16636 16637 ipha = (ipha_t *)mp->b_rptr; 16638 iph_hdr_length = IPH_HDR_LENGTH(ipha); 16639 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 16640 ipha = (ipha_t *)&icmph[1]; 16641 hdr_length = IPH_HDR_LENGTH(ipha); 16642 /* 16643 * icmp_inbound_error_fanout may need to do pullupmsg. 16644 * Reset the type to M_DATA. 16645 */ 16646 mp->b_datap->db_type = M_DATA; 16647 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 16648 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 16649 B_FALSE, ill, ii->ipsec_in_zoneid); 16650 } else { 16651 icmp6_t *icmp6; 16652 int hdr_length; 16653 16654 ip6h = (ip6_t *)mp->b_rptr; 16655 /* Don't call hdr_length_v6() unless you have to. */ 16656 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 16657 hdr_length = ip_hdr_length_v6(mp, ip6h); 16658 else 16659 hdr_length = IPV6_HDR_LEN; 16660 16661 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 16662 /* 16663 * icmp_inbound_error_fanout_v6 may need to do 16664 * pullupmsg. Reset the type to M_DATA. 16665 */ 16666 mp->b_datap->db_type = M_DATA; 16667 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 16668 ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); 16669 } 16670 if (ill_need_rele) 16671 ill_refrele(ill); 16672 if (rill_need_rele) 16673 ill_refrele(recv_ill); 16674 return; 16675 } 16676 16677 if (ii->ipsec_in_v4) { 16678 ipha = (ipha_t *)mp->b_rptr; 16679 dst = ipha->ipha_dst; 16680 if (CLASSD(dst)) { 16681 /* 16682 * Multicast has to be delivered to all streams. 16683 */ 16684 dst = INADDR_BROADCAST; 16685 } 16686 16687 if (ire == NULL) { 16688 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, 16689 MBLK_GETLABEL(mp)); 16690 if (ire == NULL) { 16691 if (ill_need_rele) 16692 ill_refrele(ill); 16693 if (rill_need_rele) 16694 ill_refrele(recv_ill); 16695 ip1dbg(("ip_fanout_proto_again: " 16696 "IRE not found")); 16697 freemsg(ipsec_mp); 16698 return; 16699 } 16700 ire_need_rele = B_TRUE; 16701 } 16702 16703 switch (ipha->ipha_protocol) { 16704 case IPPROTO_UDP: 16705 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 16706 recv_ill); 16707 if (ire_need_rele) 16708 ire_refrele(ire); 16709 break; 16710 case IPPROTO_TCP: 16711 if (!ire_need_rele) 16712 IRE_REFHOLD(ire); 16713 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 16714 ire, ipsec_mp, 0, ill->ill_rq, NULL); 16715 IRE_REFRELE(ire); 16716 if (mp != NULL) 16717 squeue_enter_chain(GET_SQUEUE(mp), mp, 16718 mp, 1, SQTAG_IP_PROTO_AGAIN); 16719 break; 16720 case IPPROTO_SCTP: 16721 if (!ire_need_rele) 16722 IRE_REFHOLD(ire); 16723 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 16724 ipsec_mp, 0, ill->ill_rq, dst); 16725 break; 16726 default: 16727 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 16728 recv_ill); 16729 if (ire_need_rele) 16730 ire_refrele(ire); 16731 break; 16732 } 16733 } else { 16734 uint32_t rput_flags = 0; 16735 16736 ip6h = (ip6_t *)mp->b_rptr; 16737 v6dstp = &ip6h->ip6_dst; 16738 /* 16739 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 16740 * address. 16741 * 16742 * Currently, we don't store that state in the IPSEC_IN 16743 * message, and we may need to. 16744 */ 16745 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 16746 IP6_IN_LLMCAST : 0); 16747 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 16748 NULL, NULL); 16749 } 16750 if (ill_need_rele) 16751 ill_refrele(ill); 16752 if (rill_need_rele) 16753 ill_refrele(recv_ill); 16754 } 16755 16756 /* 16757 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 16758 * returns 'true' if there are still fragments left on the queue, in 16759 * which case we restart the timer. 16760 */ 16761 void 16762 ill_frag_timer(void *arg) 16763 { 16764 ill_t *ill = (ill_t *)arg; 16765 boolean_t frag_pending; 16766 16767 mutex_enter(&ill->ill_lock); 16768 ASSERT(!ill->ill_fragtimer_executing); 16769 if (ill->ill_state_flags & ILL_CONDEMNED) { 16770 ill->ill_frag_timer_id = 0; 16771 mutex_exit(&ill->ill_lock); 16772 return; 16773 } 16774 ill->ill_fragtimer_executing = 1; 16775 mutex_exit(&ill->ill_lock); 16776 16777 frag_pending = ill_frag_timeout(ill, ip_g_frag_timeout); 16778 16779 /* 16780 * Restart the timer, if we have fragments pending or if someone 16781 * wanted us to be scheduled again. 16782 */ 16783 mutex_enter(&ill->ill_lock); 16784 ill->ill_fragtimer_executing = 0; 16785 ill->ill_frag_timer_id = 0; 16786 if (frag_pending || ill->ill_fragtimer_needrestart) 16787 ill_frag_timer_start(ill); 16788 mutex_exit(&ill->ill_lock); 16789 } 16790 16791 void 16792 ill_frag_timer_start(ill_t *ill) 16793 { 16794 ASSERT(MUTEX_HELD(&ill->ill_lock)); 16795 16796 /* If the ill is closing or opening don't proceed */ 16797 if (ill->ill_state_flags & ILL_CONDEMNED) 16798 return; 16799 16800 if (ill->ill_fragtimer_executing) { 16801 /* 16802 * ill_frag_timer is currently executing. Just record the 16803 * the fact that we want the timer to be restarted. 16804 * ill_frag_timer will post a timeout before it returns, 16805 * ensuring it will be called again. 16806 */ 16807 ill->ill_fragtimer_needrestart = 1; 16808 return; 16809 } 16810 16811 if (ill->ill_frag_timer_id == 0) { 16812 /* 16813 * The timer is neither running nor is the timeout handler 16814 * executing. Post a timeout so that ill_frag_timer will be 16815 * called 16816 */ 16817 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 16818 MSEC_TO_TICK(ip_g_frag_timo_ms >> 1)); 16819 ill->ill_fragtimer_needrestart = 0; 16820 } 16821 } 16822 16823 /* 16824 * This routine is needed for loopback when forwarding multicasts. 16825 * 16826 * IPQoS Notes: 16827 * IPPF processing is done in fanout routines. 16828 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 16829 * processing for IPSec packets is done when it comes back in clear. 16830 * NOTE : The callers of this function need to do the ire_refrele for the 16831 * ire that is being passed in. 16832 */ 16833 void 16834 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 16835 ill_t *recv_ill) 16836 { 16837 ill_t *ill = (ill_t *)q->q_ptr; 16838 uint32_t sum; 16839 uint32_t u1; 16840 uint32_t u2; 16841 int hdr_length; 16842 boolean_t mctl_present; 16843 mblk_t *first_mp = mp; 16844 mblk_t *hada_mp = NULL; 16845 ipha_t *inner_ipha; 16846 16847 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 16848 "ip_rput_locl_start: q %p", q); 16849 16850 ASSERT(ire->ire_ipversion == IPV4_VERSION); 16851 16852 16853 #define rptr ((uchar_t *)ipha) 16854 #define iphs ((uint16_t *)ipha) 16855 16856 /* 16857 * no UDP or TCP packet should come here anymore. 16858 */ 16859 ASSERT((ipha->ipha_protocol != IPPROTO_TCP) && 16860 (ipha->ipha_protocol != IPPROTO_UDP)); 16861 16862 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 16863 if (mctl_present && 16864 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 16865 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 16866 16867 /* 16868 * It's an IPsec accelerated packet. 16869 * Keep a pointer to the data attributes around until 16870 * we allocate the ipsec_info_t. 16871 */ 16872 IPSECHW_DEBUG(IPSECHW_PKT, 16873 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 16874 hada_mp = first_mp; 16875 hada_mp->b_cont = NULL; 16876 /* 16877 * Since it is accelerated, it comes directly from 16878 * the ill and the data attributes is followed by 16879 * the packet data. 16880 */ 16881 ASSERT(mp->b_datap->db_type != M_CTL); 16882 first_mp = mp; 16883 mctl_present = B_FALSE; 16884 } 16885 16886 /* 16887 * IF M_CTL is not present, then ipsec_in_is_secure 16888 * should return B_TRUE. There is a case where loopback 16889 * packets has an M_CTL in the front with all the 16890 * IPSEC options set to IPSEC_PREF_NEVER - which means 16891 * ipsec_in_is_secure will return B_FALSE. As loopback 16892 * packets never comes here, it is safe to ASSERT the 16893 * following. 16894 */ 16895 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 16896 16897 16898 /* u1 is # words of IP options */ 16899 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 16900 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 16901 16902 if (u1) { 16903 if (!ip_options_cksum(q, mp, ipha, ire)) { 16904 if (hada_mp != NULL) 16905 freemsg(hada_mp); 16906 return; 16907 } 16908 } else { 16909 /* Check the IP header checksum. */ 16910 #define uph ((uint16_t *)ipha) 16911 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 16912 uph[6] + uph[7] + uph[8] + uph[9]; 16913 #undef uph 16914 /* finish doing IP checksum */ 16915 sum = (sum & 0xFFFF) + (sum >> 16); 16916 sum = ~(sum + (sum >> 16)) & 0xFFFF; 16917 /* 16918 * Don't verify header checksum if this packet is coming 16919 * back from AH/ESP as we already did it. 16920 */ 16921 if (!mctl_present && (sum && sum != 0xFFFF)) { 16922 BUMP_MIB(&ip_mib, ipInCksumErrs); 16923 goto drop_pkt; 16924 } 16925 } 16926 16927 /* 16928 * Count for SNMP of inbound packets for ire. As ip_proto_input 16929 * might be called more than once for secure packets, count only 16930 * the first time. 16931 */ 16932 if (!mctl_present) { 16933 UPDATE_IB_PKT_COUNT(ire); 16934 ire->ire_last_used_time = lbolt; 16935 } 16936 16937 /* Check for fragmentation offset. */ 16938 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 16939 u1 = u2 & (IPH_MF | IPH_OFFSET); 16940 if (u1) { 16941 /* 16942 * We re-assemble fragments before we do the AH/ESP 16943 * processing. Thus, M_CTL should not be present 16944 * while we are re-assembling. 16945 */ 16946 ASSERT(!mctl_present); 16947 ASSERT(first_mp == mp); 16948 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 16949 return; 16950 } 16951 /* 16952 * Make sure that first_mp points back to mp as 16953 * the mp we came in with could have changed in 16954 * ip_rput_fragment(). 16955 */ 16956 ipha = (ipha_t *)mp->b_rptr; 16957 first_mp = mp; 16958 } 16959 16960 /* 16961 * Clear hardware checksumming flag as it is currently only 16962 * used by TCP and UDP. 16963 */ 16964 DB_CKSUMFLAGS(mp) = 0; 16965 16966 /* Now we have a complete datagram, destined for this machine. */ 16967 u1 = IPH_HDR_LENGTH(ipha); 16968 switch (ipha->ipha_protocol) { 16969 case IPPROTO_ICMP: { 16970 ire_t *ire_zone; 16971 ilm_t *ilm; 16972 mblk_t *mp1; 16973 zoneid_t last_zoneid; 16974 16975 if (CLASSD(ipha->ipha_dst) && 16976 !(recv_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 16977 ASSERT(ire->ire_type == IRE_BROADCAST); 16978 /* 16979 * In the multicast case, applications may have joined 16980 * the group from different zones, so we need to deliver 16981 * the packet to each of them. Loop through the 16982 * multicast memberships structures (ilm) on the receive 16983 * ill and send a copy of the packet up each matching 16984 * one. However, we don't do this for multicasts sent on 16985 * the loopback interface (PHYI_LOOPBACK flag set) as 16986 * they must stay in the sender's zone. 16987 * 16988 * ilm_add_v6() ensures that ilms in the same zone are 16989 * contiguous in the ill_ilm list. We use this property 16990 * to avoid sending duplicates needed when two 16991 * applications in the same zone join the same group on 16992 * different logical interfaces: we ignore the ilm if 16993 * its zoneid is the same as the last matching one. 16994 * In addition, the sending of the packet for 16995 * ire_zoneid is delayed until all of the other ilms 16996 * have been exhausted. 16997 */ 16998 last_zoneid = -1; 16999 ILM_WALKER_HOLD(recv_ill); 17000 for (ilm = recv_ill->ill_ilm; ilm != NULL; 17001 ilm = ilm->ilm_next) { 17002 if ((ilm->ilm_flags & ILM_DELETED) || 17003 ipha->ipha_dst != ilm->ilm_addr || 17004 ilm->ilm_zoneid == last_zoneid || 17005 ilm->ilm_zoneid == ire->ire_zoneid || 17006 ilm->ilm_zoneid == ALL_ZONES || 17007 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 17008 continue; 17009 mp1 = ip_copymsg(first_mp); 17010 if (mp1 == NULL) 17011 continue; 17012 icmp_inbound(q, mp1, B_TRUE, ill, 17013 0, sum, mctl_present, B_TRUE, 17014 recv_ill, ilm->ilm_zoneid); 17015 last_zoneid = ilm->ilm_zoneid; 17016 } 17017 ILM_WALKER_RELE(recv_ill); 17018 } else if (ire->ire_type == IRE_BROADCAST) { 17019 /* 17020 * In the broadcast case, there may be many zones 17021 * which need a copy of the packet delivered to them. 17022 * There is one IRE_BROADCAST per broadcast address 17023 * and per zone; we walk those using a helper function. 17024 * In addition, the sending of the packet for ire is 17025 * delayed until all of the other ires have been 17026 * processed. 17027 */ 17028 IRB_REFHOLD(ire->ire_bucket); 17029 ire_zone = NULL; 17030 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 17031 ire)) != NULL) { 17032 mp1 = ip_copymsg(first_mp); 17033 if (mp1 == NULL) 17034 continue; 17035 17036 UPDATE_IB_PKT_COUNT(ire_zone); 17037 ire_zone->ire_last_used_time = lbolt; 17038 icmp_inbound(q, mp1, B_TRUE, ill, 17039 0, sum, mctl_present, B_TRUE, 17040 recv_ill, ire_zone->ire_zoneid); 17041 } 17042 IRB_REFRELE(ire->ire_bucket); 17043 } 17044 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 17045 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 17046 ire->ire_zoneid); 17047 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17048 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 17049 return; 17050 } 17051 case IPPROTO_IGMP: 17052 /* 17053 * If we are not willing to accept IGMP packets in clear, 17054 * then check with global policy. 17055 */ 17056 if (igmp_accept_clear_messages == 0) { 17057 first_mp = ipsec_check_global_policy(first_mp, NULL, 17058 ipha, NULL, mctl_present); 17059 if (first_mp == NULL) 17060 return; 17061 } 17062 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17063 freemsg(first_mp); 17064 ip1dbg(("ip_proto_input: zone all cannot accept raw")); 17065 BUMP_MIB(&ip_mib, ipInDiscards); 17066 return; 17067 } 17068 if ((mp = igmp_input(q, mp, ill)) == NULL) { 17069 /* Bad packet - discarded by igmp_input */ 17070 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17071 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 17072 if (mctl_present) 17073 freeb(first_mp); 17074 return; 17075 } 17076 /* 17077 * igmp_input() may have returned the pulled up message. 17078 * So first_mp and ipha need to be reinitialized. 17079 */ 17080 ipha = (ipha_t *)mp->b_rptr; 17081 if (mctl_present) 17082 first_mp->b_cont = mp; 17083 else 17084 first_mp = mp; 17085 if (ipcl_proto_search(ipha->ipha_protocol) == NULL) { 17086 /* No user-level listener for IGMP packets */ 17087 goto drop_pkt; 17088 } 17089 /* deliver to local raw users */ 17090 break; 17091 case IPPROTO_PIM: 17092 /* 17093 * If we are not willing to accept PIM packets in clear, 17094 * then check with global policy. 17095 */ 17096 if (pim_accept_clear_messages == 0) { 17097 first_mp = ipsec_check_global_policy(first_mp, NULL, 17098 ipha, NULL, mctl_present); 17099 if (first_mp == NULL) 17100 return; 17101 } 17102 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17103 freemsg(first_mp); 17104 ip1dbg(("ip_proto_input: zone all cannot accept PIM")); 17105 BUMP_MIB(&ip_mib, ipInDiscards); 17106 return; 17107 } 17108 if (pim_input(q, mp) != 0) { 17109 /* Bad packet - discarded by pim_input */ 17110 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17111 "ip_rput_locl_end: q %p (%S)", q, "pim"); 17112 if (mctl_present) 17113 freeb(first_mp); 17114 return; 17115 } 17116 17117 /* 17118 * pim_input() may have pulled up the message so ipha needs to 17119 * be reinitialized. 17120 */ 17121 ipha = (ipha_t *)mp->b_rptr; 17122 if (ipcl_proto_search(ipha->ipha_protocol) == NULL) { 17123 /* No user-level listener for PIM packets */ 17124 goto drop_pkt; 17125 } 17126 /* deliver to local raw users */ 17127 break; 17128 case IPPROTO_ENCAP: 17129 /* 17130 * Handle self-encapsulated packets (IP-in-IP where 17131 * the inner addresses == the outer addresses). 17132 */ 17133 hdr_length = IPH_HDR_LENGTH(ipha); 17134 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 17135 mp->b_wptr) { 17136 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 17137 sizeof (ipha_t) - mp->b_rptr)) { 17138 BUMP_MIB(&ip_mib, ipInDiscards); 17139 freemsg(first_mp); 17140 return; 17141 } 17142 ipha = (ipha_t *)mp->b_rptr; 17143 } 17144 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 17145 /* 17146 * Check the sanity of the inner IP header. 17147 */ 17148 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 17149 BUMP_MIB(&ip_mib, ipInDiscards); 17150 freemsg(first_mp); 17151 return; 17152 } 17153 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 17154 BUMP_MIB(&ip_mib, ipInDiscards); 17155 freemsg(first_mp); 17156 return; 17157 } 17158 if (inner_ipha->ipha_src == ipha->ipha_src && 17159 inner_ipha->ipha_dst == ipha->ipha_dst) { 17160 ipsec_in_t *ii; 17161 17162 /* 17163 * Self-encapsulated tunnel packet. Remove 17164 * the outer IP header and fanout again. 17165 * We also need to make sure that the inner 17166 * header is pulled up until options. 17167 */ 17168 mp->b_rptr = (uchar_t *)inner_ipha; 17169 ipha = inner_ipha; 17170 hdr_length = IPH_HDR_LENGTH(ipha); 17171 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 17172 if (!pullupmsg(mp, (uchar_t *)ipha + 17173 + hdr_length - mp->b_rptr)) { 17174 freemsg(first_mp); 17175 return; 17176 } 17177 ipha = (ipha_t *)mp->b_rptr; 17178 } 17179 if (!mctl_present) { 17180 ASSERT(first_mp == mp); 17181 /* 17182 * This means that somebody is sending 17183 * Self-encapsualted packets without AH/ESP. 17184 * If AH/ESP was present, we would have already 17185 * allocated the first_mp. 17186 */ 17187 if ((first_mp = ipsec_in_alloc(B_TRUE)) == 17188 NULL) { 17189 ip1dbg(("ip_proto_input: IPSEC_IN " 17190 "allocation failure.\n")); 17191 BUMP_MIB(&ip_mib, ipInDiscards); 17192 freemsg(mp); 17193 return; 17194 } 17195 first_mp->b_cont = mp; 17196 } 17197 /* 17198 * We generally store the ill_index if we need to 17199 * do IPSEC processing as we lose the ill queue when 17200 * we come back. But in this case, we never should 17201 * have to store the ill_index here as it should have 17202 * been stored previously when we processed the 17203 * AH/ESP header in this routine or for non-ipsec 17204 * cases, we still have the queue. But for some bad 17205 * packets from the wire, we can get to IPSEC after 17206 * this and we better store the index for that case. 17207 */ 17208 ill = (ill_t *)q->q_ptr; 17209 ii = (ipsec_in_t *)first_mp->b_rptr; 17210 ii->ipsec_in_ill_index = 17211 ill->ill_phyint->phyint_ifindex; 17212 ii->ipsec_in_rill_index = 17213 recv_ill->ill_phyint->phyint_ifindex; 17214 if (ii->ipsec_in_decaps) { 17215 /* 17216 * This packet is self-encapsulated multiple 17217 * times. We don't want to recurse infinitely. 17218 * To keep it simple, drop the packet. 17219 */ 17220 BUMP_MIB(&ip_mib, ipInDiscards); 17221 freemsg(first_mp); 17222 return; 17223 } 17224 ii->ipsec_in_decaps = B_TRUE; 17225 ip_proto_input(q, first_mp, ipha, ire, recv_ill); 17226 return; 17227 } 17228 break; 17229 case IPPROTO_AH: 17230 case IPPROTO_ESP: { 17231 /* 17232 * Fast path for AH/ESP. If this is the first time 17233 * we are sending a datagram to AH/ESP, allocate 17234 * a IPSEC_IN message and prepend it. Otherwise, 17235 * just fanout. 17236 */ 17237 17238 int ipsec_rc; 17239 ipsec_in_t *ii; 17240 17241 IP_STAT(ipsec_proto_ahesp); 17242 if (!mctl_present) { 17243 ASSERT(first_mp == mp); 17244 if ((first_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 17245 ip1dbg(("ip_proto_input: IPSEC_IN " 17246 "allocation failure.\n")); 17247 freemsg(hada_mp); /* okay ifnull */ 17248 BUMP_MIB(&ip_mib, ipInDiscards); 17249 freemsg(mp); 17250 return; 17251 } 17252 /* 17253 * Store the ill_index so that when we come back 17254 * from IPSEC we ride on the same queue. 17255 */ 17256 ill = (ill_t *)q->q_ptr; 17257 ii = (ipsec_in_t *)first_mp->b_rptr; 17258 ii->ipsec_in_ill_index = 17259 ill->ill_phyint->phyint_ifindex; 17260 ii->ipsec_in_rill_index = 17261 recv_ill->ill_phyint->phyint_ifindex; 17262 first_mp->b_cont = mp; 17263 /* 17264 * Cache hardware acceleration info. 17265 */ 17266 if (hada_mp != NULL) { 17267 IPSECHW_DEBUG(IPSECHW_PKT, 17268 ("ip_rput_local: caching data attr.\n")); 17269 ii->ipsec_in_accelerated = B_TRUE; 17270 ii->ipsec_in_da = hada_mp; 17271 hada_mp = NULL; 17272 } 17273 } else { 17274 ii = (ipsec_in_t *)first_mp->b_rptr; 17275 } 17276 17277 if (!ipsec_loaded()) { 17278 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 17279 ire->ire_zoneid); 17280 return; 17281 } 17282 17283 /* select inbound SA and have IPsec process the pkt */ 17284 if (ipha->ipha_protocol == IPPROTO_ESP) { 17285 esph_t *esph = ipsec_inbound_esp_sa(first_mp); 17286 if (esph == NULL) 17287 return; 17288 ASSERT(ii->ipsec_in_esp_sa != NULL); 17289 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 17290 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 17291 first_mp, esph); 17292 } else { 17293 ah_t *ah = ipsec_inbound_ah_sa(first_mp); 17294 if (ah == NULL) 17295 return; 17296 ASSERT(ii->ipsec_in_ah_sa != NULL); 17297 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 17298 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 17299 first_mp, ah); 17300 } 17301 17302 switch (ipsec_rc) { 17303 case IPSEC_STATUS_SUCCESS: 17304 break; 17305 case IPSEC_STATUS_FAILED: 17306 BUMP_MIB(&ip_mib, ipInDiscards); 17307 /* FALLTHRU */ 17308 case IPSEC_STATUS_PENDING: 17309 return; 17310 } 17311 /* we're done with IPsec processing, send it up */ 17312 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 17313 return; 17314 } 17315 default: 17316 break; 17317 } 17318 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { 17319 ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", 17320 ire->ire_zoneid)); 17321 goto drop_pkt; 17322 } 17323 /* 17324 * Handle protocols with which IP is less intimate. There 17325 * can be more than one stream bound to a particular 17326 * protocol. When this is the case, each one gets a copy 17327 * of any incoming packets. 17328 */ 17329 ip_fanout_proto(q, first_mp, ill, ipha, 17330 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 17331 B_TRUE, recv_ill, ire->ire_zoneid); 17332 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17333 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 17334 return; 17335 17336 drop_pkt: 17337 freemsg(first_mp); 17338 if (hada_mp != NULL) 17339 freeb(hada_mp); 17340 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17341 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 17342 #undef rptr 17343 #undef iphs 17344 17345 } 17346 17347 /* 17348 * Update any source route, record route or timestamp options. 17349 * Check that we are at end of strict source route. 17350 * The options have already been checked for sanity in ip_rput_options(). 17351 */ 17352 static boolean_t 17353 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire) 17354 { 17355 ipoptp_t opts; 17356 uchar_t *opt; 17357 uint8_t optval; 17358 uint8_t optlen; 17359 ipaddr_t dst; 17360 uint32_t ts; 17361 ire_t *dst_ire; 17362 timestruc_t now; 17363 zoneid_t zoneid; 17364 ill_t *ill; 17365 17366 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17367 17368 ip2dbg(("ip_rput_local_options\n")); 17369 17370 for (optval = ipoptp_first(&opts, ipha); 17371 optval != IPOPT_EOL; 17372 optval = ipoptp_next(&opts)) { 17373 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 17374 opt = opts.ipoptp_cur; 17375 optlen = opts.ipoptp_len; 17376 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 17377 optval, optlen)); 17378 switch (optval) { 17379 uint32_t off; 17380 case IPOPT_SSRR: 17381 case IPOPT_LSRR: 17382 off = opt[IPOPT_OFFSET]; 17383 off--; 17384 if (optlen < IP_ADDR_LEN || 17385 off > optlen - IP_ADDR_LEN) { 17386 /* End of source route */ 17387 ip1dbg(("ip_rput_local_options: end of SR\n")); 17388 break; 17389 } 17390 /* 17391 * This will only happen if two consecutive entries 17392 * in the source route contains our address or if 17393 * it is a packet with a loose source route which 17394 * reaches us before consuming the whole source route 17395 */ 17396 ip1dbg(("ip_rput_local_options: not end of SR\n")); 17397 if (optval == IPOPT_SSRR) { 17398 goto bad_src_route; 17399 } 17400 /* 17401 * Hack: instead of dropping the packet truncate the 17402 * source route to what has been used by filling the 17403 * rest with IPOPT_NOP. 17404 */ 17405 opt[IPOPT_OLEN] = (uint8_t)off; 17406 while (off < optlen) { 17407 opt[off++] = IPOPT_NOP; 17408 } 17409 break; 17410 case IPOPT_RR: 17411 off = opt[IPOPT_OFFSET]; 17412 off--; 17413 if (optlen < IP_ADDR_LEN || 17414 off > optlen - IP_ADDR_LEN) { 17415 /* No more room - ignore */ 17416 ip1dbg(( 17417 "ip_rput_local_options: end of RR\n")); 17418 break; 17419 } 17420 bcopy(&ire->ire_src_addr, (char *)opt + off, 17421 IP_ADDR_LEN); 17422 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17423 break; 17424 case IPOPT_TS: 17425 /* Insert timestamp if there is romm */ 17426 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17427 case IPOPT_TS_TSONLY: 17428 off = IPOPT_TS_TIMELEN; 17429 break; 17430 case IPOPT_TS_PRESPEC: 17431 case IPOPT_TS_PRESPEC_RFC791: 17432 /* Verify that the address matched */ 17433 off = opt[IPOPT_OFFSET] - 1; 17434 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17435 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17436 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 17437 if (dst_ire == NULL) { 17438 /* Not for us */ 17439 break; 17440 } 17441 ire_refrele(dst_ire); 17442 /* FALLTHRU */ 17443 case IPOPT_TS_TSANDADDR: 17444 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17445 break; 17446 default: 17447 /* 17448 * ip_*put_options should have already 17449 * dropped this packet. 17450 */ 17451 cmn_err(CE_PANIC, "ip_rput_local_options: " 17452 "unknown IT - bug in ip_rput_options?\n"); 17453 return (B_TRUE); /* Keep "lint" happy */ 17454 } 17455 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 17456 /* Increase overflow counter */ 17457 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 17458 opt[IPOPT_POS_OV_FLG] = 17459 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 17460 (off << 4)); 17461 break; 17462 } 17463 off = opt[IPOPT_OFFSET] - 1; 17464 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17465 case IPOPT_TS_PRESPEC: 17466 case IPOPT_TS_PRESPEC_RFC791: 17467 case IPOPT_TS_TSANDADDR: 17468 bcopy(&ire->ire_src_addr, (char *)opt + off, 17469 IP_ADDR_LEN); 17470 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17471 /* FALLTHRU */ 17472 case IPOPT_TS_TSONLY: 17473 off = opt[IPOPT_OFFSET] - 1; 17474 /* Compute # of milliseconds since midnight */ 17475 gethrestime(&now); 17476 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 17477 now.tv_nsec / (NANOSEC / MILLISEC); 17478 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 17479 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 17480 break; 17481 } 17482 break; 17483 } 17484 } 17485 return (B_TRUE); 17486 17487 bad_src_route: 17488 q = WR(q); 17489 if (q->q_next != NULL) 17490 ill = q->q_ptr; 17491 else 17492 ill = NULL; 17493 17494 /* make sure we clear any indication of a hardware checksum */ 17495 DB_CKSUMFLAGS(mp) = 0; 17496 zoneid = ipif_lookup_addr_zoneid(ipha->ipha_dst, ill); 17497 if (zoneid == ALL_ZONES) 17498 freemsg(mp); 17499 else 17500 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid); 17501 return (B_FALSE); 17502 17503 } 17504 17505 /* 17506 * Process IP options in an inbound packet. If an option affects the 17507 * effective destination address, return the next hop address via dstp. 17508 * Returns -1 if something fails in which case an ICMP error has been sent 17509 * and mp freed. 17510 */ 17511 static int 17512 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp) 17513 { 17514 ipoptp_t opts; 17515 uchar_t *opt; 17516 uint8_t optval; 17517 uint8_t optlen; 17518 ipaddr_t dst; 17519 intptr_t code = 0; 17520 ire_t *ire = NULL; 17521 zoneid_t zoneid; 17522 ill_t *ill; 17523 17524 ip2dbg(("ip_rput_options\n")); 17525 dst = ipha->ipha_dst; 17526 for (optval = ipoptp_first(&opts, ipha); 17527 optval != IPOPT_EOL; 17528 optval = ipoptp_next(&opts)) { 17529 opt = opts.ipoptp_cur; 17530 optlen = opts.ipoptp_len; 17531 ip2dbg(("ip_rput_options: opt %d, len %d\n", 17532 optval, optlen)); 17533 /* 17534 * Note: we need to verify the checksum before we 17535 * modify anything thus this routine only extracts the next 17536 * hop dst from any source route. 17537 */ 17538 switch (optval) { 17539 uint32_t off; 17540 case IPOPT_SSRR: 17541 case IPOPT_LSRR: 17542 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17543 ALL_ZONES, NULL, MATCH_IRE_TYPE); 17544 if (ire == NULL) { 17545 if (optval == IPOPT_SSRR) { 17546 ip1dbg(("ip_rput_options: not next" 17547 " strict source route 0x%x\n", 17548 ntohl(dst))); 17549 code = (char *)&ipha->ipha_dst - 17550 (char *)ipha; 17551 goto param_prob; /* RouterReq's */ 17552 } 17553 ip2dbg(("ip_rput_options: " 17554 "not next source route 0x%x\n", 17555 ntohl(dst))); 17556 break; 17557 } 17558 ire_refrele(ire); 17559 17560 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17561 ip1dbg(( 17562 "ip_rput_options: bad option offset\n")); 17563 code = (char *)&opt[IPOPT_OLEN] - 17564 (char *)ipha; 17565 goto param_prob; 17566 } 17567 off = opt[IPOPT_OFFSET]; 17568 off--; 17569 redo_srr: 17570 if (optlen < IP_ADDR_LEN || 17571 off > optlen - IP_ADDR_LEN) { 17572 /* End of source route */ 17573 ip1dbg(("ip_rput_options: end of SR\n")); 17574 break; 17575 } 17576 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17577 ip1dbg(("ip_rput_options: next hop 0x%x\n", 17578 ntohl(dst))); 17579 17580 /* 17581 * Check if our address is present more than 17582 * once as consecutive hops in source route. 17583 * XXX verify per-interface ip_forwarding 17584 * for source route? 17585 */ 17586 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17587 ALL_ZONES, NULL, MATCH_IRE_TYPE); 17588 17589 if (ire != NULL) { 17590 ire_refrele(ire); 17591 off += IP_ADDR_LEN; 17592 goto redo_srr; 17593 } 17594 17595 if (dst == htonl(INADDR_LOOPBACK)) { 17596 ip1dbg(("ip_rput_options: loopback addr in " 17597 "source route!\n")); 17598 goto bad_src_route; 17599 } 17600 /* 17601 * For strict: verify that dst is directly 17602 * reachable. 17603 */ 17604 if (optval == IPOPT_SSRR) { 17605 ire = ire_ftable_lookup(dst, 0, 0, 17606 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 17607 MBLK_GETLABEL(mp), 17608 MATCH_IRE_TYPE | MATCH_IRE_SECATTR); 17609 if (ire == NULL) { 17610 ip1dbg(("ip_rput_options: SSRR not " 17611 "directly reachable: 0x%x\n", 17612 ntohl(dst))); 17613 goto bad_src_route; 17614 } 17615 ire_refrele(ire); 17616 } 17617 /* 17618 * Defer update of the offset and the record route 17619 * until the packet is forwarded. 17620 */ 17621 break; 17622 case IPOPT_RR: 17623 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17624 ip1dbg(( 17625 "ip_rput_options: bad option offset\n")); 17626 code = (char *)&opt[IPOPT_OLEN] - 17627 (char *)ipha; 17628 goto param_prob; 17629 } 17630 break; 17631 case IPOPT_TS: 17632 /* 17633 * Verify that length >= 5 and that there is either 17634 * room for another timestamp or that the overflow 17635 * counter is not maxed out. 17636 */ 17637 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 17638 if (optlen < IPOPT_MINLEN_IT) { 17639 goto param_prob; 17640 } 17641 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17642 ip1dbg(( 17643 "ip_rput_options: bad option offset\n")); 17644 code = (char *)&opt[IPOPT_OFFSET] - 17645 (char *)ipha; 17646 goto param_prob; 17647 } 17648 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17649 case IPOPT_TS_TSONLY: 17650 off = IPOPT_TS_TIMELEN; 17651 break; 17652 case IPOPT_TS_TSANDADDR: 17653 case IPOPT_TS_PRESPEC: 17654 case IPOPT_TS_PRESPEC_RFC791: 17655 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17656 break; 17657 default: 17658 code = (char *)&opt[IPOPT_POS_OV_FLG] - 17659 (char *)ipha; 17660 goto param_prob; 17661 } 17662 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 17663 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 17664 /* 17665 * No room and the overflow counter is 15 17666 * already. 17667 */ 17668 goto param_prob; 17669 } 17670 break; 17671 } 17672 } 17673 17674 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 17675 *dstp = dst; 17676 return (0); 17677 } 17678 17679 ip1dbg(("ip_rput_options: error processing IP options.")); 17680 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 17681 17682 param_prob: 17683 q = WR(q); 17684 if (q->q_next != NULL) 17685 ill = q->q_ptr; 17686 else 17687 ill = NULL; 17688 17689 /* make sure we clear any indication of a hardware checksum */ 17690 DB_CKSUMFLAGS(mp) = 0; 17691 /* Don't know whether this is for non-global or global/forwarding */ 17692 zoneid = ipif_lookup_addr_zoneid(dst, ill); 17693 if (zoneid == ALL_ZONES) 17694 freemsg(mp); 17695 else 17696 icmp_param_problem(q, mp, (uint8_t)code, zoneid); 17697 return (-1); 17698 17699 bad_src_route: 17700 q = WR(q); 17701 if (q->q_next != NULL) 17702 ill = q->q_ptr; 17703 else 17704 ill = NULL; 17705 17706 /* make sure we clear any indication of a hardware checksum */ 17707 DB_CKSUMFLAGS(mp) = 0; 17708 zoneid = ipif_lookup_addr_zoneid(dst, ill); 17709 if (zoneid == ALL_ZONES) 17710 freemsg(mp); 17711 else 17712 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid); 17713 return (-1); 17714 } 17715 17716 /* 17717 * IP & ICMP info in >=14 msg's ... 17718 * - ip fixed part (mib2_ip_t) 17719 * - icmp fixed part (mib2_icmp_t) 17720 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 17721 * - ipRouteEntryTable (ip 21) all IPv4 IREs 17722 * - ipNetToMediaEntryTable (ip 22) IPv4 IREs for on-link destinations 17723 * - ipRouteAttributeTable (ip 102) labeled routes 17724 * - ip multicast membership (ip_member_t) 17725 * - ip multicast source filtering (ip_grpsrc_t) 17726 * - igmp fixed part (struct igmpstat) 17727 * - multicast routing stats (struct mrtstat) 17728 * - multicast routing vifs (array of struct vifctl) 17729 * - multicast routing routes (array of struct mfcctl) 17730 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 17731 * One per ill plus one generic 17732 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 17733 * One per ill plus one generic 17734 * - ipv6RouteEntry all IPv6 IREs 17735 * - ipv6RouteAttributeTable (ip6 102) labeled routes 17736 * - ipv6NetToMediaEntry all Neighbor Cache entries 17737 * - ipv6AddrEntry all IPv6 ipifs 17738 * - ipv6 multicast membership (ipv6_member_t) 17739 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 17740 * 17741 * IP_ROUTE and IP_MEDIA are augmented in arp to include arp cache entries not 17742 * already present. 17743 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 17744 * already filled in by the caller. 17745 * Return value of 0 indicates that no messages were sent and caller 17746 * should free mpctl. 17747 */ 17748 int 17749 ip_snmp_get(queue_t *q, mblk_t *mpctl) 17750 { 17751 17752 if (mpctl == NULL || mpctl->b_cont == NULL) { 17753 return (0); 17754 } 17755 17756 if ((mpctl = ip_snmp_get_mib2_ip(q, mpctl)) == NULL) { 17757 return (1); 17758 } 17759 17760 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl)) == NULL) { 17761 return (1); 17762 } 17763 17764 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl)) == NULL) { 17765 return (1); 17766 } 17767 17768 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl)) == NULL) { 17769 return (1); 17770 } 17771 17772 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl)) == NULL) { 17773 return (1); 17774 } 17775 17776 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl)) == NULL) { 17777 return (1); 17778 } 17779 17780 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl)) == NULL) { 17781 return (1); 17782 } 17783 17784 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl)) == NULL) { 17785 return (1); 17786 } 17787 17788 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl)) == NULL) { 17789 return (1); 17790 } 17791 17792 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl)) == NULL) { 17793 return (1); 17794 } 17795 17796 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl)) == NULL) { 17797 return (1); 17798 } 17799 17800 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl)) == NULL) { 17801 return (1); 17802 } 17803 17804 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl)) == NULL) { 17805 return (1); 17806 } 17807 17808 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl)) == NULL) { 17809 return (1); 17810 } 17811 17812 if ((mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl)) == NULL) { 17813 return (1); 17814 } 17815 17816 if ((mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl)) == NULL) { 17817 return (1); 17818 } 17819 17820 if ((mpctl = sctp_snmp_get_mib2(q, mpctl)) == NULL) { 17821 return (1); 17822 } 17823 freemsg(mpctl); 17824 return (1); 17825 } 17826 17827 17828 /* Get global IPv4 statistics */ 17829 static mblk_t * 17830 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl) 17831 { 17832 struct opthdr *optp; 17833 mblk_t *mp2ctl; 17834 17835 /* 17836 * make a copy of the original message 17837 */ 17838 mp2ctl = copymsg(mpctl); 17839 17840 /* fixed length IP structure... */ 17841 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17842 optp->level = MIB2_IP; 17843 optp->name = 0; 17844 SET_MIB(ip_mib.ipForwarding, 17845 (WE_ARE_FORWARDING ? 1 : 2)); 17846 SET_MIB(ip_mib.ipDefaultTTL, 17847 (uint32_t)ip_def_ttl); 17848 SET_MIB(ip_mib.ipReasmTimeout, 17849 ip_g_frag_timeout); 17850 SET_MIB(ip_mib.ipAddrEntrySize, 17851 sizeof (mib2_ipAddrEntry_t)); 17852 SET_MIB(ip_mib.ipRouteEntrySize, 17853 sizeof (mib2_ipRouteEntry_t)); 17854 SET_MIB(ip_mib.ipNetToMediaEntrySize, 17855 sizeof (mib2_ipNetToMediaEntry_t)); 17856 SET_MIB(ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 17857 SET_MIB(ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 17858 SET_MIB(ip_mib.ipRouteAttributeSize, sizeof (mib2_ipAttributeEntry_t)); 17859 SET_MIB(ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 17860 if (!snmp_append_data(mpctl->b_cont, (char *)&ip_mib, 17861 (int)sizeof (ip_mib))) { 17862 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 17863 (uint_t)sizeof (ip_mib))); 17864 } 17865 17866 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 17867 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 17868 (int)optp->level, (int)optp->name, (int)optp->len)); 17869 qreply(q, mpctl); 17870 return (mp2ctl); 17871 } 17872 17873 /* Global IPv4 ICMP statistics */ 17874 static mblk_t * 17875 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl) 17876 { 17877 struct opthdr *optp; 17878 mblk_t *mp2ctl; 17879 17880 /* 17881 * Make a copy of the original message 17882 */ 17883 mp2ctl = copymsg(mpctl); 17884 17885 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17886 optp->level = MIB2_ICMP; 17887 optp->name = 0; 17888 if (!snmp_append_data(mpctl->b_cont, (char *)&icmp_mib, 17889 (int)sizeof (icmp_mib))) { 17890 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 17891 (uint_t)sizeof (icmp_mib))); 17892 } 17893 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 17894 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 17895 (int)optp->level, (int)optp->name, (int)optp->len)); 17896 qreply(q, mpctl); 17897 return (mp2ctl); 17898 } 17899 17900 /* Global IPv4 IGMP statistics */ 17901 static mblk_t * 17902 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl) 17903 { 17904 struct opthdr *optp; 17905 mblk_t *mp2ctl; 17906 17907 /* 17908 * make a copy of the original message 17909 */ 17910 mp2ctl = copymsg(mpctl); 17911 17912 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17913 optp->level = EXPER_IGMP; 17914 optp->name = 0; 17915 if (!snmp_append_data(mpctl->b_cont, (char *)&igmpstat, 17916 (int)sizeof (igmpstat))) { 17917 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 17918 (uint_t)sizeof (igmpstat))); 17919 } 17920 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 17921 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 17922 (int)optp->level, (int)optp->name, (int)optp->len)); 17923 qreply(q, mpctl); 17924 return (mp2ctl); 17925 } 17926 17927 /* Global IPv4 Multicast Routing statistics */ 17928 static mblk_t * 17929 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl) 17930 { 17931 struct opthdr *optp; 17932 mblk_t *mp2ctl; 17933 17934 /* 17935 * make a copy of the original message 17936 */ 17937 mp2ctl = copymsg(mpctl); 17938 17939 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17940 optp->level = EXPER_DVMRP; 17941 optp->name = 0; 17942 if (!ip_mroute_stats(mpctl->b_cont)) { 17943 ip0dbg(("ip_mroute_stats: failed\n")); 17944 } 17945 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 17946 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 17947 (int)optp->level, (int)optp->name, (int)optp->len)); 17948 qreply(q, mpctl); 17949 return (mp2ctl); 17950 } 17951 17952 /* IPv4 address information */ 17953 static mblk_t * 17954 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl) 17955 { 17956 struct opthdr *optp; 17957 mblk_t *mp2ctl; 17958 mblk_t *mp_tail = NULL; 17959 ill_t *ill; 17960 ipif_t *ipif; 17961 uint_t bitval; 17962 mib2_ipAddrEntry_t mae; 17963 zoneid_t zoneid; 17964 ill_walk_context_t ctx; 17965 17966 /* 17967 * make a copy of the original message 17968 */ 17969 mp2ctl = copymsg(mpctl); 17970 17971 /* ipAddrEntryTable */ 17972 17973 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17974 optp->level = MIB2_IP; 17975 optp->name = MIB2_IP_ADDR; 17976 zoneid = Q_TO_CONN(q)->conn_zoneid; 17977 17978 rw_enter(&ill_g_lock, RW_READER); 17979 ill = ILL_START_WALK_V4(&ctx); 17980 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 17981 for (ipif = ill->ill_ipif; ipif != NULL; 17982 ipif = ipif->ipif_next) { 17983 if (ipif->ipif_zoneid != zoneid && 17984 ipif->ipif_zoneid != ALL_ZONES) 17985 continue; 17986 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 17987 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 17988 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 17989 17990 (void) ipif_get_name(ipif, 17991 mae.ipAdEntIfIndex.o_bytes, 17992 OCTET_LENGTH); 17993 mae.ipAdEntIfIndex.o_length = 17994 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 17995 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 17996 mae.ipAdEntNetMask = ipif->ipif_net_mask; 17997 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 17998 mae.ipAdEntInfo.ae_subnet_len = 17999 ip_mask_to_plen(ipif->ipif_net_mask); 18000 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 18001 for (bitval = 1; 18002 bitval && 18003 !(bitval & ipif->ipif_brd_addr); 18004 bitval <<= 1) 18005 noop; 18006 mae.ipAdEntBcastAddr = bitval; 18007 mae.ipAdEntReasmMaxSize = 65535; 18008 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 18009 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 18010 mae.ipAdEntInfo.ae_broadcast_addr = 18011 ipif->ipif_brd_addr; 18012 mae.ipAdEntInfo.ae_pp_dst_addr = 18013 ipif->ipif_pp_dst_addr; 18014 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 18015 ill->ill_flags | ill->ill_phyint->phyint_flags; 18016 18017 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18018 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 18019 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 18020 "allocate %u bytes\n", 18021 (uint_t)sizeof (mib2_ipAddrEntry_t))); 18022 } 18023 } 18024 } 18025 rw_exit(&ill_g_lock); 18026 18027 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18028 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 18029 (int)optp->level, (int)optp->name, (int)optp->len)); 18030 qreply(q, mpctl); 18031 return (mp2ctl); 18032 } 18033 18034 /* IPv6 address information */ 18035 static mblk_t * 18036 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl) 18037 { 18038 struct opthdr *optp; 18039 mblk_t *mp2ctl; 18040 mblk_t *mp_tail = NULL; 18041 ill_t *ill; 18042 ipif_t *ipif; 18043 mib2_ipv6AddrEntry_t mae6; 18044 zoneid_t zoneid; 18045 ill_walk_context_t ctx; 18046 18047 /* 18048 * make a copy of the original message 18049 */ 18050 mp2ctl = copymsg(mpctl); 18051 18052 /* ipv6AddrEntryTable */ 18053 18054 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18055 optp->level = MIB2_IP6; 18056 optp->name = MIB2_IP6_ADDR; 18057 zoneid = Q_TO_CONN(q)->conn_zoneid; 18058 18059 rw_enter(&ill_g_lock, RW_READER); 18060 ill = ILL_START_WALK_V6(&ctx); 18061 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18062 for (ipif = ill->ill_ipif; ipif != NULL; 18063 ipif = ipif->ipif_next) { 18064 if (ipif->ipif_zoneid != zoneid && 18065 ipif->ipif_zoneid != ALL_ZONES) 18066 continue; 18067 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18068 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18069 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18070 18071 (void) ipif_get_name(ipif, 18072 mae6.ipv6AddrIfIndex.o_bytes, 18073 OCTET_LENGTH); 18074 mae6.ipv6AddrIfIndex.o_length = 18075 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 18076 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 18077 mae6.ipv6AddrPfxLength = 18078 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 18079 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 18080 mae6.ipv6AddrInfo.ae_subnet_len = 18081 mae6.ipv6AddrPfxLength; 18082 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 18083 18084 /* Type: stateless(1), stateful(2), unknown(3) */ 18085 if (ipif->ipif_flags & IPIF_ADDRCONF) 18086 mae6.ipv6AddrType = 1; 18087 else 18088 mae6.ipv6AddrType = 2; 18089 /* Anycast: true(1), false(2) */ 18090 if (ipif->ipif_flags & IPIF_ANYCAST) 18091 mae6.ipv6AddrAnycastFlag = 1; 18092 else 18093 mae6.ipv6AddrAnycastFlag = 2; 18094 18095 /* 18096 * Address status: preferred(1), deprecated(2), 18097 * invalid(3), inaccessible(4), unknown(5) 18098 */ 18099 if (ipif->ipif_flags & IPIF_NOLOCAL) 18100 mae6.ipv6AddrStatus = 3; 18101 else if (ipif->ipif_flags & IPIF_DEPRECATED) 18102 mae6.ipv6AddrStatus = 2; 18103 else 18104 mae6.ipv6AddrStatus = 1; 18105 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 18106 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 18107 mae6.ipv6AddrInfo.ae_pp_dst_addr = 18108 ipif->ipif_v6pp_dst_addr; 18109 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 18110 ill->ill_flags | ill->ill_phyint->phyint_flags; 18111 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18112 (char *)&mae6, 18113 (int)sizeof (mib2_ipv6AddrEntry_t))) { 18114 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 18115 "allocate %u bytes\n", 18116 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 18117 } 18118 } 18119 } 18120 rw_exit(&ill_g_lock); 18121 18122 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18123 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 18124 (int)optp->level, (int)optp->name, (int)optp->len)); 18125 qreply(q, mpctl); 18126 return (mp2ctl); 18127 } 18128 18129 /* IPv4 multicast group membership. */ 18130 static mblk_t * 18131 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl) 18132 { 18133 struct opthdr *optp; 18134 mblk_t *mp2ctl; 18135 ill_t *ill; 18136 ipif_t *ipif; 18137 ilm_t *ilm; 18138 ip_member_t ipm; 18139 mblk_t *mp_tail = NULL; 18140 ill_walk_context_t ctx; 18141 zoneid_t zoneid; 18142 18143 /* 18144 * make a copy of the original message 18145 */ 18146 mp2ctl = copymsg(mpctl); 18147 zoneid = Q_TO_CONN(q)->conn_zoneid; 18148 18149 /* ipGroupMember table */ 18150 optp = (struct opthdr *)&mpctl->b_rptr[ 18151 sizeof (struct T_optmgmt_ack)]; 18152 optp->level = MIB2_IP; 18153 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 18154 18155 rw_enter(&ill_g_lock, RW_READER); 18156 ill = ILL_START_WALK_V4(&ctx); 18157 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18158 ILM_WALKER_HOLD(ill); 18159 for (ipif = ill->ill_ipif; ipif != NULL; 18160 ipif = ipif->ipif_next) { 18161 if (ipif->ipif_zoneid != zoneid && 18162 ipif->ipif_zoneid != ALL_ZONES) 18163 continue; /* not this zone */ 18164 (void) ipif_get_name(ipif, 18165 ipm.ipGroupMemberIfIndex.o_bytes, 18166 OCTET_LENGTH); 18167 ipm.ipGroupMemberIfIndex.o_length = 18168 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 18169 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18170 ASSERT(ilm->ilm_ipif != NULL); 18171 ASSERT(ilm->ilm_ill == NULL); 18172 if (ilm->ilm_ipif != ipif) 18173 continue; 18174 ipm.ipGroupMemberAddress = ilm->ilm_addr; 18175 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 18176 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 18177 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18178 (char *)&ipm, (int)sizeof (ipm))) { 18179 ip1dbg(("ip_snmp_get_mib2_ip_group: " 18180 "failed to allocate %u bytes\n", 18181 (uint_t)sizeof (ipm))); 18182 } 18183 } 18184 } 18185 ILM_WALKER_RELE(ill); 18186 } 18187 rw_exit(&ill_g_lock); 18188 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18189 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18190 (int)optp->level, (int)optp->name, (int)optp->len)); 18191 qreply(q, mpctl); 18192 return (mp2ctl); 18193 } 18194 18195 /* IPv6 multicast group membership. */ 18196 static mblk_t * 18197 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl) 18198 { 18199 struct opthdr *optp; 18200 mblk_t *mp2ctl; 18201 ill_t *ill; 18202 ilm_t *ilm; 18203 ipv6_member_t ipm6; 18204 mblk_t *mp_tail = NULL; 18205 ill_walk_context_t ctx; 18206 zoneid_t zoneid; 18207 18208 /* 18209 * make a copy of the original message 18210 */ 18211 mp2ctl = copymsg(mpctl); 18212 zoneid = Q_TO_CONN(q)->conn_zoneid; 18213 18214 /* ip6GroupMember table */ 18215 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18216 optp->level = MIB2_IP6; 18217 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 18218 18219 rw_enter(&ill_g_lock, RW_READER); 18220 ill = ILL_START_WALK_V6(&ctx); 18221 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18222 ILM_WALKER_HOLD(ill); 18223 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 18224 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18225 ASSERT(ilm->ilm_ipif == NULL); 18226 ASSERT(ilm->ilm_ill != NULL); 18227 if (ilm->ilm_zoneid != zoneid) 18228 continue; /* not this zone */ 18229 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 18230 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 18231 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 18232 if (!snmp_append_data2(mpctl->b_cont, 18233 &mp_tail, 18234 (char *)&ipm6, (int)sizeof (ipm6))) { 18235 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 18236 "failed to allocate %u bytes\n", 18237 (uint_t)sizeof (ipm6))); 18238 } 18239 } 18240 ILM_WALKER_RELE(ill); 18241 } 18242 rw_exit(&ill_g_lock); 18243 18244 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18245 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18246 (int)optp->level, (int)optp->name, (int)optp->len)); 18247 qreply(q, mpctl); 18248 return (mp2ctl); 18249 } 18250 18251 /* IP multicast filtered sources */ 18252 static mblk_t * 18253 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl) 18254 { 18255 struct opthdr *optp; 18256 mblk_t *mp2ctl; 18257 ill_t *ill; 18258 ipif_t *ipif; 18259 ilm_t *ilm; 18260 ip_grpsrc_t ips; 18261 mblk_t *mp_tail = NULL; 18262 ill_walk_context_t ctx; 18263 zoneid_t zoneid; 18264 int i; 18265 slist_t *sl; 18266 18267 /* 18268 * make a copy of the original message 18269 */ 18270 mp2ctl = copymsg(mpctl); 18271 zoneid = Q_TO_CONN(q)->conn_zoneid; 18272 18273 /* ipGroupSource table */ 18274 optp = (struct opthdr *)&mpctl->b_rptr[ 18275 sizeof (struct T_optmgmt_ack)]; 18276 optp->level = MIB2_IP; 18277 optp->name = EXPER_IP_GROUP_SOURCES; 18278 18279 rw_enter(&ill_g_lock, RW_READER); 18280 ill = ILL_START_WALK_V4(&ctx); 18281 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18282 ILM_WALKER_HOLD(ill); 18283 for (ipif = ill->ill_ipif; ipif != NULL; 18284 ipif = ipif->ipif_next) { 18285 if (ipif->ipif_zoneid != zoneid) 18286 continue; /* not this zone */ 18287 (void) ipif_get_name(ipif, 18288 ips.ipGroupSourceIfIndex.o_bytes, 18289 OCTET_LENGTH); 18290 ips.ipGroupSourceIfIndex.o_length = 18291 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 18292 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18293 ASSERT(ilm->ilm_ipif != NULL); 18294 ASSERT(ilm->ilm_ill == NULL); 18295 sl = ilm->ilm_filter; 18296 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 18297 continue; 18298 ips.ipGroupSourceGroup = ilm->ilm_addr; 18299 for (i = 0; i < sl->sl_numsrc; i++) { 18300 if (!IN6_IS_ADDR_V4MAPPED( 18301 &sl->sl_addr[i])) 18302 continue; 18303 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 18304 ips.ipGroupSourceAddress); 18305 if (snmp_append_data2(mpctl->b_cont, 18306 &mp_tail, (char *)&ips, 18307 (int)sizeof (ips)) == 0) { 18308 ip1dbg(("ip_snmp_get_mib2_" 18309 "ip_group_src: failed to " 18310 "allocate %u bytes\n", 18311 (uint_t)sizeof (ips))); 18312 } 18313 } 18314 } 18315 } 18316 ILM_WALKER_RELE(ill); 18317 } 18318 rw_exit(&ill_g_lock); 18319 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18320 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18321 (int)optp->level, (int)optp->name, (int)optp->len)); 18322 qreply(q, mpctl); 18323 return (mp2ctl); 18324 } 18325 18326 /* IPv6 multicast filtered sources. */ 18327 static mblk_t * 18328 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl) 18329 { 18330 struct opthdr *optp; 18331 mblk_t *mp2ctl; 18332 ill_t *ill; 18333 ilm_t *ilm; 18334 ipv6_grpsrc_t ips6; 18335 mblk_t *mp_tail = NULL; 18336 ill_walk_context_t ctx; 18337 zoneid_t zoneid; 18338 int i; 18339 slist_t *sl; 18340 18341 /* 18342 * make a copy of the original message 18343 */ 18344 mp2ctl = copymsg(mpctl); 18345 zoneid = Q_TO_CONN(q)->conn_zoneid; 18346 18347 /* ip6GroupMember table */ 18348 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18349 optp->level = MIB2_IP6; 18350 optp->name = EXPER_IP6_GROUP_SOURCES; 18351 18352 rw_enter(&ill_g_lock, RW_READER); 18353 ill = ILL_START_WALK_V6(&ctx); 18354 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18355 ILM_WALKER_HOLD(ill); 18356 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 18357 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18358 ASSERT(ilm->ilm_ipif == NULL); 18359 ASSERT(ilm->ilm_ill != NULL); 18360 sl = ilm->ilm_filter; 18361 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 18362 continue; 18363 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 18364 for (i = 0; i < sl->sl_numsrc; i++) { 18365 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 18366 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18367 (char *)&ips6, (int)sizeof (ips6))) { 18368 ip1dbg(("ip_snmp_get_mib2_ip6_" 18369 "group_src: failed to allocate " 18370 "%u bytes\n", 18371 (uint_t)sizeof (ips6))); 18372 } 18373 } 18374 } 18375 ILM_WALKER_RELE(ill); 18376 } 18377 rw_exit(&ill_g_lock); 18378 18379 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18380 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18381 (int)optp->level, (int)optp->name, (int)optp->len)); 18382 qreply(q, mpctl); 18383 return (mp2ctl); 18384 } 18385 18386 /* Multicast routing virtual interface table. */ 18387 static mblk_t * 18388 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl) 18389 { 18390 struct opthdr *optp; 18391 mblk_t *mp2ctl; 18392 18393 /* 18394 * make a copy of the original message 18395 */ 18396 mp2ctl = copymsg(mpctl); 18397 18398 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18399 optp->level = EXPER_DVMRP; 18400 optp->name = EXPER_DVMRP_VIF; 18401 if (!ip_mroute_vif(mpctl->b_cont)) { 18402 ip0dbg(("ip_mroute_vif: failed\n")); 18403 } 18404 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18405 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 18406 (int)optp->level, (int)optp->name, (int)optp->len)); 18407 qreply(q, mpctl); 18408 return (mp2ctl); 18409 } 18410 18411 /* Multicast routing table. */ 18412 static mblk_t * 18413 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl) 18414 { 18415 struct opthdr *optp; 18416 mblk_t *mp2ctl; 18417 18418 /* 18419 * make a copy of the original message 18420 */ 18421 mp2ctl = copymsg(mpctl); 18422 18423 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18424 optp->level = EXPER_DVMRP; 18425 optp->name = EXPER_DVMRP_MRT; 18426 if (!ip_mroute_mrt(mpctl->b_cont)) { 18427 ip0dbg(("ip_mroute_mrt: failed\n")); 18428 } 18429 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18430 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 18431 (int)optp->level, (int)optp->name, (int)optp->len)); 18432 qreply(q, mpctl); 18433 return (mp2ctl); 18434 } 18435 18436 /* 18437 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 18438 * in one IRE walk. 18439 */ 18440 static mblk_t * 18441 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl) 18442 { 18443 struct opthdr *optp; 18444 mblk_t *mp2ctl; /* Returned */ 18445 mblk_t *mp3ctl; /* nettomedia */ 18446 mblk_t *mp4ctl; /* routeattrs */ 18447 iproutedata_t ird; 18448 zoneid_t zoneid; 18449 18450 /* 18451 * make copies of the original message 18452 * - mp2ctl is returned unchanged to the caller for his use 18453 * - mpctl is sent upstream as ipRouteEntryTable 18454 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 18455 * - mp4ctl is sent upstream as ipRouteAttributeTable 18456 */ 18457 mp2ctl = copymsg(mpctl); 18458 mp3ctl = copymsg(mpctl); 18459 mp4ctl = copymsg(mpctl); 18460 if (mp3ctl == NULL || mp4ctl == NULL) { 18461 freemsg(mp4ctl); 18462 freemsg(mp3ctl); 18463 freemsg(mp2ctl); 18464 freemsg(mpctl); 18465 return (NULL); 18466 } 18467 18468 bzero(&ird, sizeof (ird)); 18469 18470 ird.ird_route.lp_head = mpctl->b_cont; 18471 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 18472 ird.ird_attrs.lp_head = mp4ctl->b_cont; 18473 18474 zoneid = Q_TO_CONN(q)->conn_zoneid; 18475 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid); 18476 if (zoneid == GLOBAL_ZONEID) { 18477 /* 18478 * Those IREs are used by Mobile-IP; since mipagent(1M) requires 18479 * the sys_net_config privilege, it can only run in the global 18480 * zone, so we don't display these IREs in the other zones. 18481 */ 18482 ire_walk_srcif_table_v4(ip_snmp_get2_v4, &ird); 18483 ire_walk_ill_mrtun(0, 0, ip_snmp_get2_v4, &ird, NULL); 18484 } 18485 18486 /* ipRouteEntryTable in mpctl */ 18487 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18488 optp->level = MIB2_IP; 18489 optp->name = MIB2_IP_ROUTE; 18490 optp->len = msgdsize(ird.ird_route.lp_head); 18491 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18492 (int)optp->level, (int)optp->name, (int)optp->len)); 18493 qreply(q, mpctl); 18494 18495 /* ipNetToMediaEntryTable in mp3ctl */ 18496 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18497 optp->level = MIB2_IP; 18498 optp->name = MIB2_IP_MEDIA; 18499 optp->len = msgdsize(ird.ird_netmedia.lp_head); 18500 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18501 (int)optp->level, (int)optp->name, (int)optp->len)); 18502 qreply(q, mp3ctl); 18503 18504 /* ipRouteAttributeTable in mp4ctl */ 18505 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18506 optp->level = MIB2_IP; 18507 optp->name = EXPER_IP_RTATTR; 18508 optp->len = msgdsize(ird.ird_attrs.lp_head); 18509 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18510 (int)optp->level, (int)optp->name, (int)optp->len)); 18511 if (optp->len == 0) 18512 freemsg(mp4ctl); 18513 else 18514 qreply(q, mp4ctl); 18515 18516 return (mp2ctl); 18517 } 18518 18519 /* 18520 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 18521 * ipv6NetToMediaEntryTable in an NDP walk. 18522 */ 18523 static mblk_t * 18524 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl) 18525 { 18526 struct opthdr *optp; 18527 mblk_t *mp2ctl; /* Returned */ 18528 mblk_t *mp3ctl; /* nettomedia */ 18529 mblk_t *mp4ctl; /* routeattrs */ 18530 iproutedata_t ird; 18531 zoneid_t zoneid; 18532 18533 /* 18534 * make copies of the original message 18535 * - mp2ctl is returned unchanged to the caller for his use 18536 * - mpctl is sent upstream as ipv6RouteEntryTable 18537 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 18538 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 18539 */ 18540 mp2ctl = copymsg(mpctl); 18541 mp3ctl = copymsg(mpctl); 18542 mp4ctl = copymsg(mpctl); 18543 if (mp3ctl == NULL || mp4ctl == NULL) { 18544 freemsg(mp4ctl); 18545 freemsg(mp3ctl); 18546 freemsg(mp2ctl); 18547 freemsg(mpctl); 18548 return (NULL); 18549 } 18550 18551 bzero(&ird, sizeof (ird)); 18552 18553 ird.ird_route.lp_head = mpctl->b_cont; 18554 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 18555 ird.ird_attrs.lp_head = mp4ctl->b_cont; 18556 18557 zoneid = Q_TO_CONN(q)->conn_zoneid; 18558 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid); 18559 18560 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18561 optp->level = MIB2_IP6; 18562 optp->name = MIB2_IP6_ROUTE; 18563 optp->len = msgdsize(ird.ird_route.lp_head); 18564 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18565 (int)optp->level, (int)optp->name, (int)optp->len)); 18566 qreply(q, mpctl); 18567 18568 /* ipv6NetToMediaEntryTable in mp3ctl */ 18569 ndp_walk(NULL, ip_snmp_get2_v6_media, &ird); 18570 18571 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18572 optp->level = MIB2_IP6; 18573 optp->name = MIB2_IP6_MEDIA; 18574 optp->len = msgdsize(ird.ird_netmedia.lp_head); 18575 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18576 (int)optp->level, (int)optp->name, (int)optp->len)); 18577 qreply(q, mp3ctl); 18578 18579 /* ipv6RouteAttributeTable in mp4ctl */ 18580 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18581 optp->level = MIB2_IP6; 18582 optp->name = EXPER_IP_RTATTR; 18583 optp->len = msgdsize(ird.ird_attrs.lp_head); 18584 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18585 (int)optp->level, (int)optp->name, (int)optp->len)); 18586 if (optp->len == 0) 18587 freemsg(mp4ctl); 18588 else 18589 qreply(q, mp4ctl); 18590 18591 return (mp2ctl); 18592 } 18593 18594 /* 18595 * ICMPv6 mib: One per ill 18596 */ 18597 static mblk_t * 18598 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl) 18599 { 18600 struct opthdr *optp; 18601 mblk_t *mp2ctl; 18602 ill_t *ill; 18603 ill_walk_context_t ctx; 18604 mblk_t *mp_tail = NULL; 18605 18606 /* 18607 * Make a copy of the original message 18608 */ 18609 mp2ctl = copymsg(mpctl); 18610 18611 /* fixed length IPv6 structure ... */ 18612 18613 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18614 optp->level = MIB2_IP6; 18615 optp->name = 0; 18616 /* Include "unknown interface" ip6_mib */ 18617 ip6_mib.ipv6IfIndex = 0; /* Flag to netstat */ 18618 SET_MIB(ip6_mib.ipv6Forwarding, ipv6_forward ? 1 : 2); 18619 SET_MIB(ip6_mib.ipv6DefaultHopLimit, ipv6_def_hops); 18620 SET_MIB(ip6_mib.ipv6IfStatsEntrySize, 18621 sizeof (mib2_ipv6IfStatsEntry_t)); 18622 SET_MIB(ip6_mib.ipv6AddrEntrySize, sizeof (mib2_ipv6AddrEntry_t)); 18623 SET_MIB(ip6_mib.ipv6RouteEntrySize, sizeof (mib2_ipv6RouteEntry_t)); 18624 SET_MIB(ip6_mib.ipv6NetToMediaEntrySize, 18625 sizeof (mib2_ipv6NetToMediaEntry_t)); 18626 SET_MIB(ip6_mib.ipv6MemberEntrySize, sizeof (ipv6_member_t)); 18627 SET_MIB(ip6_mib.ipv6GroupSourceEntrySize, sizeof (ipv6_grpsrc_t)); 18628 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, (char *)&ip6_mib, 18629 (int)sizeof (ip6_mib))) { 18630 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 18631 (uint_t)sizeof (ip6_mib))); 18632 } 18633 18634 rw_enter(&ill_g_lock, RW_READER); 18635 ill = ILL_START_WALK_V6(&ctx); 18636 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18637 ill->ill_ip6_mib->ipv6IfIndex = 18638 ill->ill_phyint->phyint_ifindex; 18639 SET_MIB(ill->ill_ip6_mib->ipv6Forwarding, 18640 ipv6_forward ? 1 : 2); 18641 SET_MIB(ill->ill_ip6_mib->ipv6DefaultHopLimit, 18642 ill->ill_max_hops); 18643 SET_MIB(ill->ill_ip6_mib->ipv6IfStatsEntrySize, 18644 sizeof (mib2_ipv6IfStatsEntry_t)); 18645 SET_MIB(ill->ill_ip6_mib->ipv6AddrEntrySize, 18646 sizeof (mib2_ipv6AddrEntry_t)); 18647 SET_MIB(ill->ill_ip6_mib->ipv6RouteEntrySize, 18648 sizeof (mib2_ipv6RouteEntry_t)); 18649 SET_MIB(ill->ill_ip6_mib->ipv6NetToMediaEntrySize, 18650 sizeof (mib2_ipv6NetToMediaEntry_t)); 18651 SET_MIB(ill->ill_ip6_mib->ipv6MemberEntrySize, 18652 sizeof (ipv6_member_t)); 18653 18654 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18655 (char *)ill->ill_ip6_mib, 18656 (int)sizeof (*ill->ill_ip6_mib))) { 18657 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 18658 "%u bytes\n", 18659 (uint_t)sizeof (*ill->ill_ip6_mib))); 18660 } 18661 } 18662 rw_exit(&ill_g_lock); 18663 18664 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18665 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 18666 (int)optp->level, (int)optp->name, (int)optp->len)); 18667 qreply(q, mpctl); 18668 return (mp2ctl); 18669 } 18670 18671 /* 18672 * ICMPv6 mib: One per ill 18673 */ 18674 static mblk_t * 18675 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl) 18676 { 18677 struct opthdr *optp; 18678 mblk_t *mp2ctl; 18679 ill_t *ill; 18680 ill_walk_context_t ctx; 18681 mblk_t *mp_tail = NULL; 18682 /* 18683 * Make a copy of the original message 18684 */ 18685 mp2ctl = copymsg(mpctl); 18686 18687 /* fixed length ICMPv6 structure ... */ 18688 18689 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18690 optp->level = MIB2_ICMP6; 18691 optp->name = 0; 18692 /* Include "unknown interface" icmp6_mib */ 18693 icmp6_mib.ipv6IfIcmpIfIndex = 0; /* Flag to netstat */ 18694 icmp6_mib.ipv6IfIcmpEntrySize = sizeof (mib2_ipv6IfIcmpEntry_t); 18695 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, (char *)&icmp6_mib, 18696 (int)sizeof (icmp6_mib))) { 18697 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 18698 (uint_t)sizeof (icmp6_mib))); 18699 } 18700 18701 rw_enter(&ill_g_lock, RW_READER); 18702 ill = ILL_START_WALK_V6(&ctx); 18703 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18704 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 18705 ill->ill_phyint->phyint_ifindex; 18706 ill->ill_icmp6_mib->ipv6IfIcmpEntrySize = 18707 sizeof (mib2_ipv6IfIcmpEntry_t); 18708 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18709 (char *)ill->ill_icmp6_mib, 18710 (int)sizeof (*ill->ill_icmp6_mib))) { 18711 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 18712 "%u bytes\n", 18713 (uint_t)sizeof (*ill->ill_icmp6_mib))); 18714 } 18715 } 18716 rw_exit(&ill_g_lock); 18717 18718 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18719 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 18720 (int)optp->level, (int)optp->name, (int)optp->len)); 18721 qreply(q, mpctl); 18722 return (mp2ctl); 18723 } 18724 18725 /* 18726 * ire_walk routine to create both ipRouteEntryTable and 18727 * ipNetToMediaEntryTable in one IRE walk 18728 */ 18729 static void 18730 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 18731 { 18732 ill_t *ill; 18733 ipif_t *ipif; 18734 mblk_t *llmp; 18735 dl_unitdata_req_t *dlup; 18736 mib2_ipRouteEntry_t *re; 18737 mib2_ipNetToMediaEntry_t ntme; 18738 mib2_ipAttributeEntry_t *iae, *iaeptr; 18739 ipaddr_t gw_addr; 18740 tsol_ire_gw_secattr_t *attrp; 18741 tsol_gc_t *gc = NULL; 18742 tsol_gcgrp_t *gcgrp = NULL; 18743 uint_t sacnt = 0; 18744 int i; 18745 18746 ASSERT(ire->ire_ipversion == IPV4_VERSION); 18747 18748 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 18749 return; 18750 18751 if ((attrp = ire->ire_gw_secattr) != NULL) { 18752 mutex_enter(&attrp->igsa_lock); 18753 if ((gc = attrp->igsa_gc) != NULL) { 18754 gcgrp = gc->gc_grp; 18755 ASSERT(gcgrp != NULL); 18756 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 18757 sacnt = 1; 18758 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 18759 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 18760 gc = gcgrp->gcgrp_head; 18761 sacnt = gcgrp->gcgrp_count; 18762 } 18763 mutex_exit(&attrp->igsa_lock); 18764 18765 /* do nothing if there's no gc to report */ 18766 if (gc == NULL) { 18767 ASSERT(sacnt == 0); 18768 if (gcgrp != NULL) { 18769 /* we might as well drop the lock now */ 18770 rw_exit(&gcgrp->gcgrp_rwlock); 18771 gcgrp = NULL; 18772 } 18773 attrp = NULL; 18774 } 18775 18776 ASSERT(gc == NULL || (gcgrp != NULL && 18777 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 18778 } 18779 ASSERT(sacnt == 0 || gc != NULL); 18780 18781 if (sacnt != 0 && 18782 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 18783 kmem_free(re, sizeof (*re)); 18784 rw_exit(&gcgrp->gcgrp_rwlock); 18785 return; 18786 } 18787 18788 /* 18789 * Return all IRE types for route table... let caller pick and choose 18790 */ 18791 re->ipRouteDest = ire->ire_addr; 18792 ipif = ire->ire_ipif; 18793 re->ipRouteIfIndex.o_length = 0; 18794 if (ire->ire_type == IRE_CACHE) { 18795 ill = (ill_t *)ire->ire_stq->q_ptr; 18796 re->ipRouteIfIndex.o_length = 18797 ill->ill_name_length == 0 ? 0 : 18798 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 18799 bcopy(ill->ill_name, re->ipRouteIfIndex.o_bytes, 18800 re->ipRouteIfIndex.o_length); 18801 } else if (ipif != NULL) { 18802 (void) ipif_get_name(ipif, re->ipRouteIfIndex.o_bytes, 18803 OCTET_LENGTH); 18804 re->ipRouteIfIndex.o_length = 18805 mi_strlen(re->ipRouteIfIndex.o_bytes); 18806 } 18807 re->ipRouteMetric1 = -1; 18808 re->ipRouteMetric2 = -1; 18809 re->ipRouteMetric3 = -1; 18810 re->ipRouteMetric4 = -1; 18811 18812 gw_addr = ire->ire_gateway_addr; 18813 18814 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 18815 re->ipRouteNextHop = ire->ire_src_addr; 18816 else 18817 re->ipRouteNextHop = gw_addr; 18818 /* indirect(4), direct(3), or invalid(2) */ 18819 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 18820 re->ipRouteType = 2; 18821 else 18822 re->ipRouteType = (gw_addr != 0) ? 4 : 3; 18823 re->ipRouteProto = -1; 18824 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 18825 re->ipRouteMask = ire->ire_mask; 18826 re->ipRouteMetric5 = -1; 18827 re->ipRouteInfo.re_max_frag = ire->ire_max_frag; 18828 re->ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 18829 re->ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 18830 if (ire->ire_nce && 18831 ire->ire_nce->nce_state == ND_REACHABLE) 18832 llmp = ire->ire_nce->nce_res_mp; 18833 else 18834 llmp = NULL; 18835 re->ipRouteInfo.re_ref = ire->ire_refcnt; 18836 re->ipRouteInfo.re_src_addr = ire->ire_src_addr; 18837 re->ipRouteInfo.re_ire_type = ire->ire_type; 18838 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 18839 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 18840 re->ipRouteInfo.re_flags = ire->ire_flags; 18841 re->ipRouteInfo.re_in_ill.o_length = 0; 18842 if (ire->ire_in_ill != NULL) { 18843 re->ipRouteInfo.re_in_ill.o_length = 18844 ire->ire_in_ill->ill_name_length == 0 ? 0 : 18845 MIN(OCTET_LENGTH, ire->ire_in_ill->ill_name_length - 1); 18846 bcopy(ire->ire_in_ill->ill_name, 18847 re->ipRouteInfo.re_in_ill.o_bytes, 18848 re->ipRouteInfo.re_in_ill.o_length); 18849 } 18850 re->ipRouteInfo.re_in_src_addr = ire->ire_in_src_addr; 18851 18852 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 18853 (char *)re, (int)sizeof (*re))) { 18854 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 18855 (uint_t)sizeof (*re))); 18856 } 18857 18858 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 18859 iaeptr->iae_routeidx = ird->ird_idx; 18860 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 18861 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 18862 } 18863 18864 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 18865 (char *)iae, sacnt * sizeof (*iae))) { 18866 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 18867 (unsigned)(sacnt * sizeof (*iae)))); 18868 } 18869 18870 if (ire->ire_type != IRE_CACHE || gw_addr != 0) 18871 goto done; 18872 /* 18873 * only IRE_CACHE entries that are for a directly connected subnet 18874 * get appended to net -> phys addr table 18875 * (others in arp) 18876 */ 18877 ntme.ipNetToMediaIfIndex.o_length = 0; 18878 ill = ire_to_ill(ire); 18879 ASSERT(ill != NULL); 18880 ntme.ipNetToMediaIfIndex.o_length = 18881 ill->ill_name_length == 0 ? 0 : 18882 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 18883 bcopy(ill->ill_name, ntme.ipNetToMediaIfIndex.o_bytes, 18884 ntme.ipNetToMediaIfIndex.o_length); 18885 18886 ntme.ipNetToMediaPhysAddress.o_length = 0; 18887 if (llmp) { 18888 uchar_t *addr; 18889 18890 dlup = (dl_unitdata_req_t *)llmp->b_rptr; 18891 /* Remove sap from address */ 18892 if (ill->ill_sap_length < 0) 18893 addr = llmp->b_rptr + dlup->dl_dest_addr_offset; 18894 else 18895 addr = llmp->b_rptr + dlup->dl_dest_addr_offset + 18896 ill->ill_sap_length; 18897 18898 ntme.ipNetToMediaPhysAddress.o_length = 18899 MIN(OCTET_LENGTH, ill->ill_phys_addr_length); 18900 bcopy(addr, ntme.ipNetToMediaPhysAddress.o_bytes, 18901 ntme.ipNetToMediaPhysAddress.o_length); 18902 } 18903 ntme.ipNetToMediaNetAddress = ire->ire_addr; 18904 /* assume dynamic (may be changed in arp) */ 18905 ntme.ipNetToMediaType = 3; 18906 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (uint32_t); 18907 bcopy(&ire->ire_mask, ntme.ipNetToMediaInfo.ntm_mask.o_bytes, 18908 ntme.ipNetToMediaInfo.ntm_mask.o_length); 18909 ntme.ipNetToMediaInfo.ntm_flags = ACE_F_RESOLVED; 18910 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 18911 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 18912 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 18913 (uint_t)sizeof (ntme))); 18914 } 18915 done: 18916 /* bump route index for next pass */ 18917 ird->ird_idx++; 18918 18919 kmem_free(re, sizeof (*re)); 18920 if (sacnt != 0) 18921 kmem_free(iae, sacnt * sizeof (*iae)); 18922 18923 if (gcgrp != NULL) 18924 rw_exit(&gcgrp->gcgrp_rwlock); 18925 } 18926 18927 /* 18928 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 18929 */ 18930 static void 18931 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 18932 { 18933 ill_t *ill; 18934 ipif_t *ipif; 18935 mib2_ipv6RouteEntry_t *re; 18936 mib2_ipAttributeEntry_t *iae, *iaeptr; 18937 in6_addr_t gw_addr_v6; 18938 tsol_ire_gw_secattr_t *attrp; 18939 tsol_gc_t *gc = NULL; 18940 tsol_gcgrp_t *gcgrp = NULL; 18941 uint_t sacnt = 0; 18942 int i; 18943 18944 ASSERT(ire->ire_ipversion == IPV6_VERSION); 18945 18946 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 18947 return; 18948 18949 if ((attrp = ire->ire_gw_secattr) != NULL) { 18950 mutex_enter(&attrp->igsa_lock); 18951 if ((gc = attrp->igsa_gc) != NULL) { 18952 gcgrp = gc->gc_grp; 18953 ASSERT(gcgrp != NULL); 18954 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 18955 sacnt = 1; 18956 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 18957 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 18958 gc = gcgrp->gcgrp_head; 18959 sacnt = gcgrp->gcgrp_count; 18960 } 18961 mutex_exit(&attrp->igsa_lock); 18962 18963 /* do nothing if there's no gc to report */ 18964 if (gc == NULL) { 18965 ASSERT(sacnt == 0); 18966 if (gcgrp != NULL) { 18967 /* we might as well drop the lock now */ 18968 rw_exit(&gcgrp->gcgrp_rwlock); 18969 gcgrp = NULL; 18970 } 18971 attrp = NULL; 18972 } 18973 18974 ASSERT(gc == NULL || (gcgrp != NULL && 18975 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 18976 } 18977 ASSERT(sacnt == 0 || gc != NULL); 18978 18979 if (sacnt != 0 && 18980 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 18981 kmem_free(re, sizeof (*re)); 18982 rw_exit(&gcgrp->gcgrp_rwlock); 18983 return; 18984 } 18985 18986 /* 18987 * Return all IRE types for route table... let caller pick and choose 18988 */ 18989 re->ipv6RouteDest = ire->ire_addr_v6; 18990 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 18991 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 18992 re->ipv6RouteIfIndex.o_length = 0; 18993 ipif = ire->ire_ipif; 18994 if (ire->ire_type == IRE_CACHE) { 18995 ill = (ill_t *)ire->ire_stq->q_ptr; 18996 re->ipv6RouteIfIndex.o_length = 18997 ill->ill_name_length == 0 ? 0 : 18998 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 18999 bcopy(ill->ill_name, re->ipv6RouteIfIndex.o_bytes, 19000 re->ipv6RouteIfIndex.o_length); 19001 } else if (ipif != NULL) { 19002 (void) ipif_get_name(ipif, re->ipv6RouteIfIndex.o_bytes, 19003 OCTET_LENGTH); 19004 re->ipv6RouteIfIndex.o_length = 19005 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 19006 } 19007 19008 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19009 19010 mutex_enter(&ire->ire_lock); 19011 gw_addr_v6 = ire->ire_gateway_addr_v6; 19012 mutex_exit(&ire->ire_lock); 19013 19014 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 19015 re->ipv6RouteNextHop = ire->ire_src_addr_v6; 19016 else 19017 re->ipv6RouteNextHop = gw_addr_v6; 19018 19019 /* remote(4), local(3), or discard(2) */ 19020 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19021 re->ipv6RouteType = 2; 19022 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 19023 re->ipv6RouteType = 3; 19024 else 19025 re->ipv6RouteType = 4; 19026 19027 re->ipv6RouteProtocol = -1; 19028 re->ipv6RoutePolicy = 0; 19029 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 19030 re->ipv6RouteNextHopRDI = 0; 19031 re->ipv6RouteWeight = 0; 19032 re->ipv6RouteMetric = 0; 19033 re->ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 19034 re->ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 19035 re->ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19036 re->ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 19037 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 19038 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19039 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19040 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 19041 re->ipv6RouteInfo.re_flags = ire->ire_flags; 19042 19043 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19044 (char *)re, (int)sizeof (*re))) { 19045 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19046 (uint_t)sizeof (*re))); 19047 } 19048 19049 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19050 iaeptr->iae_routeidx = ird->ird_idx; 19051 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19052 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19053 } 19054 19055 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19056 (char *)iae, sacnt * sizeof (*iae))) { 19057 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19058 (unsigned)(sacnt * sizeof (*iae)))); 19059 } 19060 19061 /* bump route index for next pass */ 19062 ird->ird_idx++; 19063 19064 kmem_free(re, sizeof (*re)); 19065 if (sacnt != 0) 19066 kmem_free(iae, sacnt * sizeof (*iae)); 19067 19068 if (gcgrp != NULL) 19069 rw_exit(&gcgrp->gcgrp_rwlock); 19070 } 19071 19072 /* 19073 * ndp_walk routine to create ipv6NetToMediaEntryTable 19074 */ 19075 static int 19076 ip_snmp_get2_v6_media(nce_t *nce, iproutedata_t *ird) 19077 { 19078 ill_t *ill; 19079 mib2_ipv6NetToMediaEntry_t ntme; 19080 dl_unitdata_req_t *dl; 19081 19082 ill = nce->nce_ill; 19083 if (ill->ill_isv6 == B_FALSE) /* skip arpce entry */ 19084 return (0); 19085 19086 /* 19087 * Neighbor cache entry attached to IRE with on-link 19088 * destination. 19089 */ 19090 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 19091 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 19092 if ((ill->ill_flags & ILLF_XRESOLV) && 19093 (nce->nce_res_mp != NULL)) { 19094 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 19095 ntme.ipv6NetToMediaPhysAddress.o_length = 19096 dl->dl_dest_addr_length; 19097 } else { 19098 ntme.ipv6NetToMediaPhysAddress.o_length = 19099 ill->ill_phys_addr_length; 19100 } 19101 if (nce->nce_res_mp != NULL) { 19102 bcopy((char *)nce->nce_res_mp->b_rptr + 19103 NCE_LL_ADDR_OFFSET(ill), 19104 ntme.ipv6NetToMediaPhysAddress.o_bytes, 19105 ntme.ipv6NetToMediaPhysAddress.o_length); 19106 } else { 19107 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 19108 ill->ill_phys_addr_length); 19109 } 19110 /* 19111 * Note: Returns ND_* states. Should be: 19112 * reachable(1), stale(2), delay(3), probe(4), 19113 * invalid(5), unknown(6) 19114 */ 19115 ntme.ipv6NetToMediaState = nce->nce_state; 19116 ntme.ipv6NetToMediaLastUpdated = 0; 19117 19118 /* other(1), dynamic(2), static(3), local(4) */ 19119 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 19120 ntme.ipv6NetToMediaType = 4; 19121 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 19122 ntme.ipv6NetToMediaType = 1; 19123 } else { 19124 ntme.ipv6NetToMediaType = 2; 19125 } 19126 19127 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 19128 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 19129 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 19130 (uint_t)sizeof (ntme))); 19131 } 19132 return (0); 19133 } 19134 19135 /* 19136 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 19137 */ 19138 /* ARGSUSED */ 19139 int 19140 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 19141 { 19142 switch (level) { 19143 case MIB2_IP: 19144 case MIB2_ICMP: 19145 switch (name) { 19146 default: 19147 break; 19148 } 19149 return (1); 19150 default: 19151 return (1); 19152 } 19153 } 19154 19155 /* 19156 * Called before the options are updated to check if this packet will 19157 * be source routed from here. 19158 * This routine assumes that the options are well formed i.e. that they 19159 * have already been checked. 19160 */ 19161 static boolean_t 19162 ip_source_routed(ipha_t *ipha) 19163 { 19164 ipoptp_t opts; 19165 uchar_t *opt; 19166 uint8_t optval; 19167 uint8_t optlen; 19168 ipaddr_t dst; 19169 ire_t *ire; 19170 19171 if (IS_SIMPLE_IPH(ipha)) { 19172 ip2dbg(("not source routed\n")); 19173 return (B_FALSE); 19174 } 19175 dst = ipha->ipha_dst; 19176 for (optval = ipoptp_first(&opts, ipha); 19177 optval != IPOPT_EOL; 19178 optval = ipoptp_next(&opts)) { 19179 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 19180 opt = opts.ipoptp_cur; 19181 optlen = opts.ipoptp_len; 19182 ip2dbg(("ip_source_routed: opt %d, len %d\n", 19183 optval, optlen)); 19184 switch (optval) { 19185 uint32_t off; 19186 case IPOPT_SSRR: 19187 case IPOPT_LSRR: 19188 /* 19189 * If dst is one of our addresses and there are some 19190 * entries left in the source route return (true). 19191 */ 19192 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 19193 ALL_ZONES, NULL, MATCH_IRE_TYPE); 19194 if (ire == NULL) { 19195 ip2dbg(("ip_source_routed: not next" 19196 " source route 0x%x\n", 19197 ntohl(dst))); 19198 return (B_FALSE); 19199 } 19200 ire_refrele(ire); 19201 off = opt[IPOPT_OFFSET]; 19202 off--; 19203 if (optlen < IP_ADDR_LEN || 19204 off > optlen - IP_ADDR_LEN) { 19205 /* End of source route */ 19206 ip1dbg(("ip_source_routed: end of SR\n")); 19207 return (B_FALSE); 19208 } 19209 return (B_TRUE); 19210 } 19211 } 19212 ip2dbg(("not source routed\n")); 19213 return (B_FALSE); 19214 } 19215 19216 /* 19217 * Check if the packet contains any source route. 19218 */ 19219 static boolean_t 19220 ip_source_route_included(ipha_t *ipha) 19221 { 19222 ipoptp_t opts; 19223 uint8_t optval; 19224 19225 if (IS_SIMPLE_IPH(ipha)) 19226 return (B_FALSE); 19227 for (optval = ipoptp_first(&opts, ipha); 19228 optval != IPOPT_EOL; 19229 optval = ipoptp_next(&opts)) { 19230 switch (optval) { 19231 case IPOPT_SSRR: 19232 case IPOPT_LSRR: 19233 return (B_TRUE); 19234 } 19235 } 19236 return (B_FALSE); 19237 } 19238 19239 /* 19240 * Called when the IRE expiration timer fires. 19241 */ 19242 /* ARGSUSED */ 19243 void 19244 ip_trash_timer_expire(void *args) 19245 { 19246 int flush_flag = 0; 19247 19248 /* 19249 * ip_ire_expire_id is protected by ip_trash_timer_lock. 19250 * This lock makes sure that a new invocation of this function 19251 * that occurs due to an almost immediate timer firing will not 19252 * progress beyond this point until the current invocation is done 19253 */ 19254 mutex_enter(&ip_trash_timer_lock); 19255 ip_ire_expire_id = 0; 19256 mutex_exit(&ip_trash_timer_lock); 19257 19258 /* Periodic timer */ 19259 if (ip_ire_arp_time_elapsed >= ip_ire_arp_interval) { 19260 /* 19261 * Remove all IRE_CACHE entries since they might 19262 * contain arp information. 19263 */ 19264 flush_flag |= FLUSH_ARP_TIME; 19265 ip_ire_arp_time_elapsed = 0; 19266 IP_STAT(ip_ire_arp_timer_expired); 19267 } 19268 if (ip_ire_rd_time_elapsed >= ip_ire_redir_interval) { 19269 /* Remove all redirects */ 19270 flush_flag |= FLUSH_REDIRECT_TIME; 19271 ip_ire_rd_time_elapsed = 0; 19272 IP_STAT(ip_ire_redirect_timer_expired); 19273 } 19274 if (ip_ire_pmtu_time_elapsed >= ip_ire_pathmtu_interval) { 19275 /* Increase path mtu */ 19276 flush_flag |= FLUSH_MTU_TIME; 19277 ip_ire_pmtu_time_elapsed = 0; 19278 IP_STAT(ip_ire_pmtu_timer_expired); 19279 } 19280 19281 /* 19282 * Optimize for the case when there are no redirects in the 19283 * ftable, that is, no need to walk the ftable in that case. 19284 */ 19285 if (flush_flag & (FLUSH_MTU_TIME|FLUSH_ARP_TIME)) { 19286 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_CACHETABLE, ire_expire, 19287 (char *)(uintptr_t)flush_flag, IP_MASK_TABLE_SIZE, 0, NULL, 19288 ip_cache_table_size, ip_cache_table, NULL, ALL_ZONES); 19289 } 19290 if ((flush_flag & FLUSH_REDIRECT_TIME) && ip_redirect_cnt > 0) { 19291 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_FORWARDTABLE, 19292 ire_expire, (char *)(uintptr_t)flush_flag, 19293 IP_MASK_TABLE_SIZE, 0, NULL, 0, NULL, NULL, ALL_ZONES); 19294 } 19295 if (flush_flag & FLUSH_MTU_TIME) { 19296 /* 19297 * Walk all IPv6 IRE's and update them 19298 * Note that ARP and redirect timers are not 19299 * needed since NUD handles stale entries. 19300 */ 19301 flush_flag = FLUSH_MTU_TIME; 19302 ire_walk_v6(ire_expire, (char *)(uintptr_t)flush_flag, 19303 ALL_ZONES); 19304 } 19305 19306 ip_ire_arp_time_elapsed += ip_timer_interval; 19307 ip_ire_rd_time_elapsed += ip_timer_interval; 19308 ip_ire_pmtu_time_elapsed += ip_timer_interval; 19309 19310 /* 19311 * Hold the lock to serialize timeout calls and prevent 19312 * stale values in ip_ire_expire_id. Otherwise it is possible 19313 * for the timer to fire and a new invocation of this function 19314 * to start before the return value of timeout has been stored 19315 * in ip_ire_expire_id by the current invocation. 19316 */ 19317 mutex_enter(&ip_trash_timer_lock); 19318 ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL, 19319 MSEC_TO_TICK(ip_timer_interval)); 19320 mutex_exit(&ip_trash_timer_lock); 19321 } 19322 19323 /* 19324 * Called by the memory allocator subsystem directly, when the system 19325 * is running low on memory. 19326 */ 19327 /* ARGSUSED */ 19328 void 19329 ip_trash_ire_reclaim(void *args) 19330 { 19331 ire_cache_count_t icc; 19332 ire_cache_reclaim_t icr; 19333 ncc_cache_count_t ncc; 19334 nce_cache_reclaim_t ncr; 19335 uint_t delete_cnt; 19336 /* 19337 * Memory reclaim call back. 19338 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 19339 * Then, with a target of freeing 1/Nth of IRE_CACHE 19340 * entries, determine what fraction to free for 19341 * each category of IRE_CACHE entries giving absolute priority 19342 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 19343 * entry will be freed unless all offlink entries are freed). 19344 */ 19345 icc.icc_total = 0; 19346 icc.icc_unused = 0; 19347 icc.icc_offlink = 0; 19348 icc.icc_pmtu = 0; 19349 icc.icc_onlink = 0; 19350 ire_walk(ire_cache_count, (char *)&icc); 19351 19352 /* 19353 * Free NCEs for IPv6 like the onlink ires. 19354 */ 19355 ncc.ncc_total = 0; 19356 ncc.ncc_host = 0; 19357 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc); 19358 19359 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 19360 icc.icc_pmtu + icc.icc_onlink); 19361 delete_cnt = icc.icc_total/ip_ire_reclaim_fraction; 19362 IP_STAT(ip_trash_ire_reclaim_calls); 19363 if (delete_cnt == 0) 19364 return; 19365 IP_STAT(ip_trash_ire_reclaim_success); 19366 /* Always delete all unused offlink entries */ 19367 icr.icr_unused = 1; 19368 if (delete_cnt <= icc.icc_unused) { 19369 /* 19370 * Only need to free unused entries. In other words, 19371 * there are enough unused entries to free to meet our 19372 * target number of freed ire cache entries. 19373 */ 19374 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 19375 ncr.ncr_host = 0; 19376 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 19377 /* 19378 * Only need to free unused entries, plus a fraction of offlink 19379 * entries. It follows from the first if statement that 19380 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 19381 */ 19382 delete_cnt -= icc.icc_unused; 19383 /* Round up # deleted by truncating fraction */ 19384 icr.icr_offlink = icc.icc_offlink / delete_cnt; 19385 icr.icr_pmtu = icr.icr_onlink = 0; 19386 ncr.ncr_host = 0; 19387 } else if (delete_cnt <= 19388 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 19389 /* 19390 * Free all unused and offlink entries, plus a fraction of 19391 * pmtu entries. It follows from the previous if statement 19392 * that icc_pmtu is non-zero, and that 19393 * delete_cnt != icc_unused + icc_offlink. 19394 */ 19395 icr.icr_offlink = 1; 19396 delete_cnt -= icc.icc_unused + icc.icc_offlink; 19397 /* Round up # deleted by truncating fraction */ 19398 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 19399 icr.icr_onlink = 0; 19400 ncr.ncr_host = 0; 19401 } else { 19402 /* 19403 * Free all unused, offlink, and pmtu entries, plus a fraction 19404 * of onlink entries. If we're here, then we know that 19405 * icc_onlink is non-zero, and that 19406 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 19407 */ 19408 icr.icr_offlink = icr.icr_pmtu = 1; 19409 delete_cnt -= icc.icc_unused + icc.icc_offlink + 19410 icc.icc_pmtu; 19411 /* Round up # deleted by truncating fraction */ 19412 icr.icr_onlink = icc.icc_onlink / delete_cnt; 19413 /* Using the same delete fraction as for onlink IREs */ 19414 ncr.ncr_host = ncc.ncc_host / delete_cnt; 19415 } 19416 #ifdef DEBUG 19417 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 19418 "fractions %d/%d/%d/%d\n", 19419 icc.icc_total/ip_ire_reclaim_fraction, icc.icc_total, 19420 icc.icc_unused, icc.icc_offlink, 19421 icc.icc_pmtu, icc.icc_onlink, 19422 icr.icr_unused, icr.icr_offlink, 19423 icr.icr_pmtu, icr.icr_onlink)); 19424 #endif 19425 ire_walk(ire_cache_reclaim, (char *)&icr); 19426 if (ncr.ncr_host != 0) 19427 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 19428 (uchar_t *)&ncr); 19429 #ifdef DEBUG 19430 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 19431 icc.icc_pmtu = 0; icc.icc_onlink = 0; 19432 ire_walk(ire_cache_count, (char *)&icc); 19433 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 19434 icc.icc_total, icc.icc_unused, icc.icc_offlink, 19435 icc.icc_pmtu, icc.icc_onlink)); 19436 #endif 19437 } 19438 19439 /* 19440 * ip_unbind is called when a copy of an unbind request is received from the 19441 * upper level protocol. We remove this conn from any fanout hash list it is 19442 * on, and zero out the bind information. No reply is expected up above. 19443 */ 19444 mblk_t * 19445 ip_unbind(queue_t *q, mblk_t *mp) 19446 { 19447 conn_t *connp = Q_TO_CONN(q); 19448 19449 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 19450 19451 if (is_system_labeled() && connp->conn_anon_port) { 19452 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 19453 connp->conn_mlp_type, connp->conn_ulp, 19454 ntohs(connp->conn_lport), B_FALSE); 19455 connp->conn_anon_port = 0; 19456 } 19457 connp->conn_mlp_type = mlptSingle; 19458 19459 ipcl_hash_remove(connp); 19460 19461 ASSERT(mp->b_cont == NULL); 19462 /* 19463 * Convert mp into a T_OK_ACK 19464 */ 19465 mp = mi_tpi_ok_ack_alloc(mp); 19466 19467 /* 19468 * should not happen in practice... T_OK_ACK is smaller than the 19469 * original message. 19470 */ 19471 if (mp == NULL) 19472 return (NULL); 19473 19474 /* 19475 * Don't bzero the ports if its TCP since TCP still needs the 19476 * lport to remove it from its own bind hash. TCP will do the 19477 * cleanup. 19478 */ 19479 if (!IPCL_IS_TCP(connp)) 19480 bzero(&connp->u_port, sizeof (connp->u_port)); 19481 19482 return (mp); 19483 } 19484 19485 /* 19486 * Write side put procedure. Outbound data, IOCTLs, responses from 19487 * resolvers, etc, come down through here. 19488 * 19489 * arg2 is always a queue_t *. 19490 * When that queue is an ill_t (i.e. q_next != NULL), then arg must be 19491 * the zoneid. 19492 * When that queue is not an ill_t, then arg must be a conn_t pointer. 19493 */ 19494 void 19495 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 19496 { 19497 conn_t *connp = NULL; 19498 queue_t *q = (queue_t *)arg2; 19499 ipha_t *ipha; 19500 #define rptr ((uchar_t *)ipha) 19501 ire_t *ire = NULL; 19502 ire_t *sctp_ire = NULL; 19503 uint32_t v_hlen_tos_len; 19504 ipaddr_t dst; 19505 mblk_t *first_mp = NULL; 19506 boolean_t mctl_present; 19507 ipsec_out_t *io; 19508 int match_flags; 19509 ill_t *attach_ill = NULL; 19510 /* Bind to IPIF_NOFAILOVER ill etc. */ 19511 ill_t *xmit_ill = NULL; /* IP_XMIT_IF etc. */ 19512 ipif_t *dst_ipif; 19513 boolean_t multirt_need_resolve = B_FALSE; 19514 mblk_t *copy_mp = NULL; 19515 int err; 19516 zoneid_t zoneid; 19517 int adjust; 19518 uint16_t iplen; 19519 boolean_t need_decref = B_FALSE; 19520 boolean_t ignore_dontroute = B_FALSE; 19521 boolean_t ignore_nexthop = B_FALSE; 19522 boolean_t ip_nexthop = B_FALSE; 19523 ipaddr_t nexthop_addr; 19524 19525 #ifdef _BIG_ENDIAN 19526 #define V_HLEN (v_hlen_tos_len >> 24) 19527 #else 19528 #define V_HLEN (v_hlen_tos_len & 0xFF) 19529 #endif 19530 19531 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 19532 "ip_wput_start: q %p", q); 19533 19534 /* 19535 * ip_wput fast path 19536 */ 19537 19538 /* is packet from ARP ? */ 19539 if (q->q_next != NULL) { 19540 zoneid = (zoneid_t)(uintptr_t)arg; 19541 goto qnext; 19542 } 19543 19544 connp = (conn_t *)arg; 19545 ASSERT(connp != NULL); 19546 zoneid = connp->conn_zoneid; 19547 19548 /* is queue flow controlled? */ 19549 if ((q->q_first != NULL || connp->conn_draining) && 19550 (caller == IP_WPUT)) { 19551 ASSERT(!need_decref); 19552 (void) putq(q, mp); 19553 return; 19554 } 19555 19556 /* Multidata transmit? */ 19557 if (DB_TYPE(mp) == M_MULTIDATA) { 19558 /* 19559 * We should never get here, since all Multidata messages 19560 * originating from tcp should have been directed over to 19561 * tcp_multisend() in the first place. 19562 */ 19563 BUMP_MIB(&ip_mib, ipOutDiscards); 19564 freemsg(mp); 19565 return; 19566 } else if (DB_TYPE(mp) != M_DATA) 19567 goto notdata; 19568 19569 if (mp->b_flag & MSGHASREF) { 19570 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 19571 mp->b_flag &= ~MSGHASREF; 19572 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 19573 need_decref = B_TRUE; 19574 } 19575 ipha = (ipha_t *)mp->b_rptr; 19576 19577 /* is IP header non-aligned or mblk smaller than basic IP header */ 19578 #ifndef SAFETY_BEFORE_SPEED 19579 if (!OK_32PTR(rptr) || 19580 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 19581 goto hdrtoosmall; 19582 #endif 19583 19584 ASSERT(OK_32PTR(ipha)); 19585 19586 /* 19587 * This function assumes that mp points to an IPv4 packet. If it's the 19588 * wrong version, we'll catch it again in ip_output_v6. 19589 * 19590 * Note that this is *only* locally-generated output here, and never 19591 * forwarded data, and that we need to deal only with transports that 19592 * don't know how to label. (TCP, UDP, and ICMP/raw-IP all know how to 19593 * label.) 19594 */ 19595 if (is_system_labeled() && 19596 (ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4) && 19597 !connp->conn_ulp_labeled) { 19598 err = tsol_check_label(BEST_CRED(mp, connp), &mp, &adjust, 19599 connp->conn_mac_exempt); 19600 ipha = (ipha_t *)mp->b_rptr; 19601 if (err != 0) { 19602 first_mp = mp; 19603 if (err == EINVAL) 19604 goto icmp_parameter_problem; 19605 ip2dbg(("ip_wput: label check failed (%d)\n", err)); 19606 goto drop_pkt; 19607 } 19608 iplen = ntohs(ipha->ipha_length) + adjust; 19609 ipha->ipha_length = htons(iplen); 19610 } 19611 19612 /* 19613 * If there is a policy, try to attach an ipsec_out in 19614 * the front. At the end, first_mp either points to a 19615 * M_DATA message or IPSEC_OUT message linked to a 19616 * M_DATA message. We have to do it now as we might 19617 * lose the "conn" if we go through ip_newroute. 19618 */ 19619 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 19620 if (((mp = ipsec_attach_ipsec_out(mp, connp, NULL, 19621 ipha->ipha_protocol)) == NULL)) { 19622 if (need_decref) 19623 CONN_DEC_REF(connp); 19624 return; 19625 } else { 19626 ASSERT(mp->b_datap->db_type == M_CTL); 19627 first_mp = mp; 19628 mp = mp->b_cont; 19629 mctl_present = B_TRUE; 19630 } 19631 } else { 19632 first_mp = mp; 19633 mctl_present = B_FALSE; 19634 } 19635 19636 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 19637 19638 /* is wrong version or IP options present */ 19639 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 19640 goto version_hdrlen_check; 19641 dst = ipha->ipha_dst; 19642 19643 if (connp->conn_nofailover_ill != NULL) { 19644 attach_ill = conn_get_held_ill(connp, 19645 &connp->conn_nofailover_ill, &err); 19646 if (err == ILL_LOOKUP_FAILED) { 19647 if (need_decref) 19648 CONN_DEC_REF(connp); 19649 freemsg(first_mp); 19650 return; 19651 } 19652 } 19653 19654 /* is packet multicast? */ 19655 if (CLASSD(dst)) 19656 goto multicast; 19657 19658 if ((connp->conn_dontroute) || (connp->conn_xmit_if_ill != NULL) || 19659 (connp->conn_nexthop_set)) { 19660 /* 19661 * If the destination is a broadcast or a loopback 19662 * address, SO_DONTROUTE, IP_XMIT_IF and IP_NEXTHOP go 19663 * through the standard path. But in the case of local 19664 * destination only SO_DONTROUTE and IP_NEXTHOP go through 19665 * the standard path not IP_XMIT_IF. 19666 */ 19667 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 19668 if ((ire == NULL) || ((ire->ire_type != IRE_BROADCAST) && 19669 (ire->ire_type != IRE_LOOPBACK))) { 19670 if ((connp->conn_dontroute || 19671 connp->conn_nexthop_set) && (ire != NULL) && 19672 (ire->ire_type == IRE_LOCAL)) 19673 goto standard_path; 19674 19675 if (ire != NULL) { 19676 ire_refrele(ire); 19677 /* No more access to ire */ 19678 ire = NULL; 19679 } 19680 /* 19681 * bypass routing checks and go directly to 19682 * interface. 19683 */ 19684 if (connp->conn_dontroute) { 19685 goto dontroute; 19686 } else if (connp->conn_nexthop_set) { 19687 ip_nexthop = B_TRUE; 19688 nexthop_addr = connp->conn_nexthop_v4; 19689 goto send_from_ill; 19690 } 19691 19692 /* 19693 * If IP_XMIT_IF socket option is set, 19694 * then we allow unicast and multicast 19695 * packets to go through the ill. It is 19696 * quite possible that the destination 19697 * is not in the ire cache table and we 19698 * do not want to go to ip_newroute() 19699 * instead we call ip_newroute_ipif. 19700 */ 19701 xmit_ill = conn_get_held_ill(connp, 19702 &connp->conn_xmit_if_ill, &err); 19703 if (err == ILL_LOOKUP_FAILED) { 19704 if (attach_ill != NULL) 19705 ill_refrele(attach_ill); 19706 if (need_decref) 19707 CONN_DEC_REF(connp); 19708 freemsg(first_mp); 19709 return; 19710 } 19711 goto send_from_ill; 19712 } 19713 standard_path: 19714 /* Must be a broadcast, a loopback or a local ire */ 19715 if (ire != NULL) { 19716 ire_refrele(ire); 19717 /* No more access to ire */ 19718 ire = NULL; 19719 } 19720 } 19721 19722 if (attach_ill != NULL) 19723 goto send_from_ill; 19724 19725 /* 19726 * We cache IRE_CACHEs to avoid lookups. We don't do 19727 * this for the tcp global queue and listen end point 19728 * as it does not really have a real destination to 19729 * talk to. This is also true for SCTP. 19730 */ 19731 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 19732 !connp->conn_fully_bound) { 19733 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 19734 if (ire == NULL) 19735 goto noirefound; 19736 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 19737 "ip_wput_end: q %p (%S)", q, "end"); 19738 19739 /* 19740 * Check if the ire has the RTF_MULTIRT flag, inherited 19741 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 19742 */ 19743 if (ire->ire_flags & RTF_MULTIRT) { 19744 19745 /* 19746 * Force the TTL of multirouted packets if required. 19747 * The TTL of such packets is bounded by the 19748 * ip_multirt_ttl ndd variable. 19749 */ 19750 if ((ip_multirt_ttl > 0) && 19751 (ipha->ipha_ttl > ip_multirt_ttl)) { 19752 ip2dbg(("ip_wput: forcing multirt TTL to %d " 19753 "(was %d), dst 0x%08x\n", 19754 ip_multirt_ttl, ipha->ipha_ttl, 19755 ntohl(ire->ire_addr))); 19756 ipha->ipha_ttl = ip_multirt_ttl; 19757 } 19758 /* 19759 * We look at this point if there are pending 19760 * unresolved routes. ire_multirt_resolvable() 19761 * checks in O(n) that all IRE_OFFSUBNET ire 19762 * entries for the packet's destination and 19763 * flagged RTF_MULTIRT are currently resolved. 19764 * If some remain unresolved, we make a copy 19765 * of the current message. It will be used 19766 * to initiate additional route resolutions. 19767 */ 19768 multirt_need_resolve = 19769 ire_multirt_need_resolve(ire->ire_addr, 19770 MBLK_GETLABEL(first_mp)); 19771 ip2dbg(("ip_wput[TCP]: ire %p, " 19772 "multirt_need_resolve %d, first_mp %p\n", 19773 (void *)ire, multirt_need_resolve, 19774 (void *)first_mp)); 19775 if (multirt_need_resolve) { 19776 copy_mp = copymsg(first_mp); 19777 if (copy_mp != NULL) { 19778 MULTIRT_DEBUG_TAG(copy_mp); 19779 } 19780 } 19781 } 19782 19783 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 19784 19785 /* 19786 * Try to resolve another multiroute if 19787 * ire_multirt_need_resolve() deemed it necessary. 19788 */ 19789 if (copy_mp != NULL) { 19790 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid); 19791 } 19792 if (need_decref) 19793 CONN_DEC_REF(connp); 19794 return; 19795 } 19796 19797 /* 19798 * Access to conn_ire_cache. (protected by conn_lock) 19799 * 19800 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 19801 * the ire bucket lock here to check for CONDEMNED as it is okay to 19802 * send a packet or two with the IRE_CACHE that is going away. 19803 * Access to the ire requires an ire refhold on the ire prior to 19804 * its use since an interface unplumb thread may delete the cached 19805 * ire and release the refhold at any time. 19806 * 19807 * Caching an ire in the conn_ire_cache 19808 * 19809 * o Caching an ire pointer in the conn requires a strict check for 19810 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 19811 * ires before cleaning up the conns. So the caching of an ire pointer 19812 * in the conn is done after making sure under the bucket lock that the 19813 * ire has not yet been marked CONDEMNED. Otherwise we will end up 19814 * caching an ire after the unplumb thread has cleaned up the conn. 19815 * If the conn does not send a packet subsequently the unplumb thread 19816 * will be hanging waiting for the ire count to drop to zero. 19817 * 19818 * o We also need to atomically test for a null conn_ire_cache and 19819 * set the conn_ire_cache under the the protection of the conn_lock 19820 * to avoid races among concurrent threads trying to simultaneously 19821 * cache an ire in the conn_ire_cache. 19822 */ 19823 mutex_enter(&connp->conn_lock); 19824 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 19825 19826 if (ire != NULL && ire->ire_addr == dst && 19827 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19828 19829 IRE_REFHOLD(ire); 19830 mutex_exit(&connp->conn_lock); 19831 19832 } else { 19833 boolean_t cached = B_FALSE; 19834 connp->conn_ire_cache = NULL; 19835 mutex_exit(&connp->conn_lock); 19836 /* Release the old ire */ 19837 if (ire != NULL && sctp_ire == NULL) 19838 IRE_REFRELE_NOTR(ire); 19839 19840 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 19841 if (ire == NULL) 19842 goto noirefound; 19843 IRE_REFHOLD_NOTR(ire); 19844 19845 mutex_enter(&connp->conn_lock); 19846 if (!(connp->conn_state_flags & CONN_CLOSING) && 19847 connp->conn_ire_cache == NULL) { 19848 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19849 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19850 connp->conn_ire_cache = ire; 19851 cached = B_TRUE; 19852 } 19853 rw_exit(&ire->ire_bucket->irb_lock); 19854 } 19855 mutex_exit(&connp->conn_lock); 19856 19857 /* 19858 * We can continue to use the ire but since it was 19859 * not cached, we should drop the extra reference. 19860 */ 19861 if (!cached) 19862 IRE_REFRELE_NOTR(ire); 19863 } 19864 19865 19866 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 19867 "ip_wput_end: q %p (%S)", q, "end"); 19868 19869 /* 19870 * Check if the ire has the RTF_MULTIRT flag, inherited 19871 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 19872 */ 19873 if (ire->ire_flags & RTF_MULTIRT) { 19874 19875 /* 19876 * Force the TTL of multirouted packets if required. 19877 * The TTL of such packets is bounded by the 19878 * ip_multirt_ttl ndd variable. 19879 */ 19880 if ((ip_multirt_ttl > 0) && 19881 (ipha->ipha_ttl > ip_multirt_ttl)) { 19882 ip2dbg(("ip_wput: forcing multirt TTL to %d " 19883 "(was %d), dst 0x%08x\n", 19884 ip_multirt_ttl, ipha->ipha_ttl, 19885 ntohl(ire->ire_addr))); 19886 ipha->ipha_ttl = ip_multirt_ttl; 19887 } 19888 19889 /* 19890 * At this point, we check to see if there are any pending 19891 * unresolved routes. ire_multirt_resolvable() 19892 * checks in O(n) that all IRE_OFFSUBNET ire 19893 * entries for the packet's destination and 19894 * flagged RTF_MULTIRT are currently resolved. 19895 * If some remain unresolved, we make a copy 19896 * of the current message. It will be used 19897 * to initiate additional route resolutions. 19898 */ 19899 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 19900 MBLK_GETLABEL(first_mp)); 19901 ip2dbg(("ip_wput[not TCP]: ire %p, " 19902 "multirt_need_resolve %d, first_mp %p\n", 19903 (void *)ire, multirt_need_resolve, (void *)first_mp)); 19904 if (multirt_need_resolve) { 19905 copy_mp = copymsg(first_mp); 19906 if (copy_mp != NULL) { 19907 MULTIRT_DEBUG_TAG(copy_mp); 19908 } 19909 } 19910 } 19911 19912 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 19913 19914 /* 19915 * Try to resolve another multiroute if 19916 * ire_multirt_resolvable() deemed it necessary 19917 */ 19918 if (copy_mp != NULL) { 19919 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid); 19920 } 19921 if (need_decref) 19922 CONN_DEC_REF(connp); 19923 return; 19924 19925 qnext: 19926 /* 19927 * Upper Level Protocols pass down complete IP datagrams 19928 * as M_DATA messages. Everything else is a sideshow. 19929 * 19930 * 1) We could be re-entering ip_wput because of ip_neworute 19931 * in which case we could have a IPSEC_OUT message. We 19932 * need to pass through ip_wput like other datagrams and 19933 * hence cannot branch to ip_wput_nondata. 19934 * 19935 * 2) ARP, AH, ESP, and other clients who are on the module 19936 * instance of IP stream, give us something to deal with. 19937 * We will handle AH and ESP here and rest in ip_wput_nondata. 19938 * 19939 * 3) ICMP replies also could come here. 19940 */ 19941 if (DB_TYPE(mp) != M_DATA) { 19942 notdata: 19943 if (DB_TYPE(mp) == M_CTL) { 19944 /* 19945 * M_CTL messages are used by ARP, AH and ESP to 19946 * communicate with IP. We deal with IPSEC_IN and 19947 * IPSEC_OUT here. ip_wput_nondata handles other 19948 * cases. 19949 */ 19950 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 19951 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 19952 first_mp = mp->b_cont; 19953 first_mp->b_flag &= ~MSGHASREF; 19954 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 19955 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 19956 CONN_DEC_REF(connp); 19957 connp = NULL; 19958 } 19959 if (ii->ipsec_info_type == IPSEC_IN) { 19960 /* 19961 * Either this message goes back to 19962 * IPSEC for further processing or to 19963 * ULP after policy checks. 19964 */ 19965 ip_fanout_proto_again(mp, NULL, NULL, NULL); 19966 return; 19967 } else if (ii->ipsec_info_type == IPSEC_OUT) { 19968 io = (ipsec_out_t *)ii; 19969 if (io->ipsec_out_proc_begin) { 19970 /* 19971 * IPSEC processing has already started. 19972 * Complete it. 19973 * IPQoS notes: We don't care what is 19974 * in ipsec_out_ill_index since this 19975 * won't be processed for IPQoS policies 19976 * in ipsec_out_process. 19977 */ 19978 ipsec_out_process(q, mp, NULL, 19979 io->ipsec_out_ill_index); 19980 return; 19981 } else { 19982 connp = (q->q_next != NULL) ? 19983 NULL : Q_TO_CONN(q); 19984 first_mp = mp; 19985 mp = mp->b_cont; 19986 mctl_present = B_TRUE; 19987 } 19988 zoneid = io->ipsec_out_zoneid; 19989 ASSERT(zoneid != ALL_ZONES); 19990 } else if (ii->ipsec_info_type == IPSEC_CTL) { 19991 /* 19992 * It's an IPsec control message requesting 19993 * an SADB update to be sent to the IPsec 19994 * hardware acceleration capable ills. 19995 */ 19996 ipsec_ctl_t *ipsec_ctl = 19997 (ipsec_ctl_t *)mp->b_rptr; 19998 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 19999 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 20000 mblk_t *cmp = mp->b_cont; 20001 20002 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 20003 ASSERT(cmp != NULL); 20004 20005 freeb(mp); 20006 ill_ipsec_capab_send_all(satype, cmp, sa); 20007 return; 20008 } else { 20009 /* 20010 * This must be ARP or special TSOL signaling. 20011 */ 20012 ip_wput_nondata(NULL, q, mp, NULL); 20013 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20014 "ip_wput_end: q %p (%S)", q, "nondata"); 20015 return; 20016 } 20017 } else { 20018 /* 20019 * This must be non-(ARP/AH/ESP) messages. 20020 */ 20021 ASSERT(!need_decref); 20022 ip_wput_nondata(NULL, q, mp, NULL); 20023 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20024 "ip_wput_end: q %p (%S)", q, "nondata"); 20025 return; 20026 } 20027 } else { 20028 first_mp = mp; 20029 mctl_present = B_FALSE; 20030 } 20031 20032 ASSERT(first_mp != NULL); 20033 /* 20034 * ICMP echo replies attach an ipsec_out and set ipsec_out_attach_if 20035 * to make sure that this packet goes out on the same interface it 20036 * came in. We handle that here. 20037 */ 20038 if (mctl_present) { 20039 uint_t ifindex; 20040 20041 io = (ipsec_out_t *)first_mp->b_rptr; 20042 if (io->ipsec_out_attach_if || 20043 io->ipsec_out_xmit_if || 20044 io->ipsec_out_ip_nexthop) { 20045 ill_t *ill; 20046 20047 /* 20048 * We may have lost the conn context if we are 20049 * coming here from ip_newroute(). Copy the 20050 * nexthop information. 20051 */ 20052 if (io->ipsec_out_ip_nexthop) { 20053 ip_nexthop = B_TRUE; 20054 nexthop_addr = io->ipsec_out_nexthop_addr; 20055 20056 ipha = (ipha_t *)mp->b_rptr; 20057 dst = ipha->ipha_dst; 20058 goto send_from_ill; 20059 } else { 20060 ASSERT(io->ipsec_out_ill_index != 0); 20061 ifindex = io->ipsec_out_ill_index; 20062 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 20063 NULL, NULL, NULL, NULL); 20064 /* 20065 * ipsec_out_xmit_if bit is used to tell 20066 * ip_wput to use the ill to send outgoing data 20067 * as we have no conn when data comes from ICMP 20068 * error msg routines. Currently this feature is 20069 * only used by ip_mrtun_forward routine. 20070 */ 20071 if (io->ipsec_out_xmit_if) { 20072 xmit_ill = ill; 20073 if (xmit_ill == NULL) { 20074 ip1dbg(("ip_output:bad ifindex " 20075 "for xmit_ill %d\n", 20076 ifindex)); 20077 freemsg(first_mp); 20078 BUMP_MIB(&ip_mib, 20079 ipOutDiscards); 20080 ASSERT(!need_decref); 20081 return; 20082 } 20083 /* Free up the ipsec_out_t mblk */ 20084 ASSERT(first_mp->b_cont == mp); 20085 first_mp->b_cont = NULL; 20086 freeb(first_mp); 20087 /* Just send the IP header+ICMP+data */ 20088 first_mp = mp; 20089 ipha = (ipha_t *)mp->b_rptr; 20090 dst = ipha->ipha_dst; 20091 goto send_from_ill; 20092 } else { 20093 attach_ill = ill; 20094 } 20095 20096 if (attach_ill == NULL) { 20097 ASSERT(xmit_ill == NULL); 20098 ip1dbg(("ip_output: bad ifindex for " 20099 "(BIND TO IPIF_NOFAILOVER) %d\n", 20100 ifindex)); 20101 freemsg(first_mp); 20102 BUMP_MIB(&ip_mib, ipOutDiscards); 20103 ASSERT(!need_decref); 20104 return; 20105 } 20106 } 20107 } 20108 } 20109 20110 ASSERT(xmit_ill == NULL); 20111 20112 /* We have a complete IP datagram heading outbound. */ 20113 ipha = (ipha_t *)mp->b_rptr; 20114 20115 #ifndef SPEED_BEFORE_SAFETY 20116 /* 20117 * Make sure we have a full-word aligned message and that at least 20118 * a simple IP header is accessible in the first message. If not, 20119 * try a pullup. 20120 */ 20121 if (!OK_32PTR(rptr) || 20122 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) { 20123 hdrtoosmall: 20124 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 20125 BUMP_MIB(&ip_mib, ipOutDiscards); 20126 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20127 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 20128 if (first_mp == NULL) 20129 first_mp = mp; 20130 goto drop_pkt; 20131 } 20132 20133 /* This function assumes that mp points to an IPv4 packet. */ 20134 if (is_system_labeled() && q->q_next == NULL && 20135 (*mp->b_rptr & 0xf0) == (IPV4_VERSION << 4) && 20136 !connp->conn_ulp_labeled) { 20137 err = tsol_check_label(BEST_CRED(mp, connp), &mp, 20138 &adjust, connp->conn_mac_exempt); 20139 ipha = (ipha_t *)mp->b_rptr; 20140 if (first_mp != NULL) 20141 first_mp->b_cont = mp; 20142 if (err != 0) { 20143 if (first_mp == NULL) 20144 first_mp = mp; 20145 if (err == EINVAL) 20146 goto icmp_parameter_problem; 20147 ip2dbg(("ip_wput: label check failed (%d)\n", 20148 err)); 20149 goto drop_pkt; 20150 } 20151 iplen = ntohs(ipha->ipha_length) + adjust; 20152 ipha->ipha_length = htons(iplen); 20153 } 20154 20155 ipha = (ipha_t *)mp->b_rptr; 20156 if (first_mp == NULL) { 20157 ASSERT(attach_ill == NULL && xmit_ill == NULL); 20158 /* 20159 * If we got here because of "goto hdrtoosmall" 20160 * We need to attach a IPSEC_OUT. 20161 */ 20162 if (connp->conn_out_enforce_policy) { 20163 if (((mp = ipsec_attach_ipsec_out(mp, connp, 20164 NULL, ipha->ipha_protocol)) == NULL)) { 20165 if (need_decref) 20166 CONN_DEC_REF(connp); 20167 return; 20168 } else { 20169 ASSERT(mp->b_datap->db_type == M_CTL); 20170 first_mp = mp; 20171 mp = mp->b_cont; 20172 mctl_present = B_TRUE; 20173 } 20174 } else { 20175 first_mp = mp; 20176 mctl_present = B_FALSE; 20177 } 20178 } 20179 } 20180 #endif 20181 20182 /* Most of the code below is written for speed, not readability */ 20183 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20184 20185 /* 20186 * If ip_newroute() fails, we're going to need a full 20187 * header for the icmp wraparound. 20188 */ 20189 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 20190 uint_t v_hlen; 20191 version_hdrlen_check: 20192 ASSERT(first_mp != NULL); 20193 v_hlen = V_HLEN; 20194 /* 20195 * siphon off IPv6 packets coming down from transport 20196 * layer modules here. 20197 * Note: high-order bit carries NUD reachability confirmation 20198 */ 20199 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 20200 /* 20201 * XXX implement a IPv4 and IPv6 packet counter per 20202 * conn and switch when ratio exceeds e.g. 10:1 20203 */ 20204 #ifdef notyet 20205 if (q->q_next == NULL) /* Avoid ill queue */ 20206 ip_setqinfo(RD(q), B_TRUE, B_TRUE); 20207 #endif 20208 BUMP_MIB(&ip_mib, ipOutIPv6); 20209 ASSERT(xmit_ill == NULL); 20210 if (attach_ill != NULL) 20211 ill_refrele(attach_ill); 20212 if (need_decref) 20213 mp->b_flag |= MSGHASREF; 20214 (void) ip_output_v6(arg, first_mp, arg2, caller); 20215 return; 20216 } 20217 20218 if ((v_hlen >> 4) != IP_VERSION) { 20219 BUMP_MIB(&ip_mib, ipOutDiscards); 20220 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20221 "ip_wput_end: q %p (%S)", q, "badvers"); 20222 goto drop_pkt; 20223 } 20224 /* 20225 * Is the header length at least 20 bytes? 20226 * 20227 * Are there enough bytes accessible in the header? If 20228 * not, try a pullup. 20229 */ 20230 v_hlen &= 0xF; 20231 v_hlen <<= 2; 20232 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 20233 BUMP_MIB(&ip_mib, ipOutDiscards); 20234 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20235 "ip_wput_end: q %p (%S)", q, "badlen"); 20236 goto drop_pkt; 20237 } 20238 if (v_hlen > (mp->b_wptr - rptr)) { 20239 if (!pullupmsg(mp, v_hlen)) { 20240 BUMP_MIB(&ip_mib, ipOutDiscards); 20241 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20242 "ip_wput_end: q %p (%S)", q, "badpullup2"); 20243 goto drop_pkt; 20244 } 20245 ipha = (ipha_t *)mp->b_rptr; 20246 } 20247 /* 20248 * Move first entry from any source route into ipha_dst and 20249 * verify the options 20250 */ 20251 if (ip_wput_options(q, first_mp, ipha, mctl_present, zoneid)) { 20252 ASSERT(xmit_ill == NULL); 20253 if (attach_ill != NULL) 20254 ill_refrele(attach_ill); 20255 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20256 "ip_wput_end: q %p (%S)", q, "badopts"); 20257 if (need_decref) 20258 CONN_DEC_REF(connp); 20259 return; 20260 } 20261 } 20262 dst = ipha->ipha_dst; 20263 20264 /* 20265 * Try to get an IRE_CACHE for the destination address. If we can't, 20266 * we have to run the packet through ip_newroute which will take 20267 * the appropriate action to arrange for an IRE_CACHE, such as querying 20268 * a resolver, or assigning a default gateway, etc. 20269 */ 20270 if (CLASSD(dst)) { 20271 ipif_t *ipif; 20272 uint32_t setsrc = 0; 20273 20274 multicast: 20275 ASSERT(first_mp != NULL); 20276 ASSERT(xmit_ill == NULL); 20277 ip2dbg(("ip_wput: CLASSD\n")); 20278 if (connp == NULL) { 20279 /* 20280 * Use the first good ipif on the ill. 20281 * XXX Should this ever happen? (Appears 20282 * to show up with just ppp and no ethernet due 20283 * to in.rdisc.) 20284 * However, ire_send should be able to 20285 * call ip_wput_ire directly. 20286 * 20287 * XXX Also, this can happen for ICMP and other packets 20288 * with multicast source addresses. Perhaps we should 20289 * fix things so that we drop the packet in question, 20290 * but for now, just run with it. 20291 */ 20292 ill_t *ill = (ill_t *)q->q_ptr; 20293 20294 /* 20295 * Don't honor attach_if for this case. If ill 20296 * is part of the group, ipif could belong to 20297 * any ill and we cannot maintain attach_ill 20298 * and ipif_ill same anymore and the assert 20299 * below would fail. 20300 */ 20301 if (mctl_present && io->ipsec_out_attach_if) { 20302 io->ipsec_out_ill_index = 0; 20303 io->ipsec_out_attach_if = B_FALSE; 20304 ASSERT(attach_ill != NULL); 20305 ill_refrele(attach_ill); 20306 attach_ill = NULL; 20307 } 20308 20309 ASSERT(attach_ill == NULL); 20310 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 20311 if (ipif == NULL) { 20312 if (need_decref) 20313 CONN_DEC_REF(connp); 20314 freemsg(first_mp); 20315 return; 20316 } 20317 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 20318 ntohl(dst), ill->ill_name)); 20319 } else { 20320 /* 20321 * If both IP_MULTICAST_IF and IP_XMIT_IF are set, 20322 * IP_XMIT_IF is honoured. 20323 * Block comment above this function explains the 20324 * locking mechanism used here 20325 */ 20326 xmit_ill = conn_get_held_ill(connp, 20327 &connp->conn_xmit_if_ill, &err); 20328 if (err == ILL_LOOKUP_FAILED) { 20329 ip1dbg(("ip_wput: No ill for IP_XMIT_IF\n")); 20330 goto drop_pkt; 20331 } 20332 if (xmit_ill == NULL) { 20333 ipif = conn_get_held_ipif(connp, 20334 &connp->conn_multicast_ipif, &err); 20335 if (err == IPIF_LOOKUP_FAILED) { 20336 ip1dbg(("ip_wput: No ipif for " 20337 "multicast\n")); 20338 BUMP_MIB(&ip_mib, ipOutNoRoutes); 20339 goto drop_pkt; 20340 } 20341 } 20342 if (xmit_ill != NULL) { 20343 ipif = ipif_get_next_ipif(NULL, xmit_ill); 20344 if (ipif == NULL) { 20345 ip1dbg(("ip_wput: No ipif for " 20346 "IP_XMIT_IF\n")); 20347 BUMP_MIB(&ip_mib, ipOutNoRoutes); 20348 goto drop_pkt; 20349 } 20350 } else if (ipif == NULL || ipif->ipif_isv6) { 20351 /* 20352 * We must do this ipif determination here 20353 * else we could pass through ip_newroute 20354 * and come back here without the conn context. 20355 * 20356 * Note: we do late binding i.e. we bind to 20357 * the interface when the first packet is sent. 20358 * For performance reasons we do not rebind on 20359 * each packet but keep the binding until the 20360 * next IP_MULTICAST_IF option. 20361 * 20362 * conn_multicast_{ipif,ill} are shared between 20363 * IPv4 and IPv6 and AF_INET6 sockets can 20364 * send both IPv4 and IPv6 packets. Hence 20365 * we have to check that "isv6" matches above. 20366 */ 20367 if (ipif != NULL) 20368 ipif_refrele(ipif); 20369 ipif = ipif_lookup_group(dst, zoneid); 20370 if (ipif == NULL) { 20371 ip1dbg(("ip_wput: No ipif for " 20372 "multicast\n")); 20373 BUMP_MIB(&ip_mib, ipOutNoRoutes); 20374 goto drop_pkt; 20375 } 20376 err = conn_set_held_ipif(connp, 20377 &connp->conn_multicast_ipif, ipif); 20378 if (err == IPIF_LOOKUP_FAILED) { 20379 ipif_refrele(ipif); 20380 ip1dbg(("ip_wput: No ipif for " 20381 "multicast\n")); 20382 BUMP_MIB(&ip_mib, ipOutNoRoutes); 20383 goto drop_pkt; 20384 } 20385 } 20386 } 20387 ASSERT(!ipif->ipif_isv6); 20388 /* 20389 * As we may lose the conn by the time we reach ip_wput_ire, 20390 * we copy conn_multicast_loop and conn_dontroute on to an 20391 * ipsec_out. In case if this datagram goes out secure, 20392 * we need the ill_index also. Copy that also into the 20393 * ipsec_out. 20394 */ 20395 if (mctl_present) { 20396 io = (ipsec_out_t *)first_mp->b_rptr; 20397 ASSERT(first_mp->b_datap->db_type == M_CTL); 20398 ASSERT(io->ipsec_out_type == IPSEC_OUT); 20399 } else { 20400 ASSERT(mp == first_mp); 20401 if ((first_mp = allocb(sizeof (ipsec_info_t), 20402 BPRI_HI)) == NULL) { 20403 ipif_refrele(ipif); 20404 first_mp = mp; 20405 goto drop_pkt; 20406 } 20407 first_mp->b_datap->db_type = M_CTL; 20408 first_mp->b_wptr += sizeof (ipsec_info_t); 20409 /* ipsec_out_secure is B_FALSE now */ 20410 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 20411 io = (ipsec_out_t *)first_mp->b_rptr; 20412 io->ipsec_out_type = IPSEC_OUT; 20413 io->ipsec_out_len = sizeof (ipsec_out_t); 20414 io->ipsec_out_use_global_policy = B_TRUE; 20415 first_mp->b_cont = mp; 20416 mctl_present = B_TRUE; 20417 } 20418 if (attach_ill != NULL) { 20419 ASSERT(attach_ill == ipif->ipif_ill); 20420 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 20421 20422 /* 20423 * Check if we need an ire that will not be 20424 * looked up by anybody else i.e. HIDDEN. 20425 */ 20426 if (ill_is_probeonly(attach_ill)) { 20427 match_flags |= MATCH_IRE_MARK_HIDDEN; 20428 } 20429 io->ipsec_out_ill_index = 20430 attach_ill->ill_phyint->phyint_ifindex; 20431 io->ipsec_out_attach_if = B_TRUE; 20432 } else { 20433 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 20434 io->ipsec_out_ill_index = 20435 ipif->ipif_ill->ill_phyint->phyint_ifindex; 20436 } 20437 if (connp != NULL) { 20438 io->ipsec_out_multicast_loop = 20439 connp->conn_multicast_loop; 20440 io->ipsec_out_dontroute = connp->conn_dontroute; 20441 io->ipsec_out_zoneid = connp->conn_zoneid; 20442 } 20443 /* 20444 * If the application uses IP_MULTICAST_IF with 20445 * different logical addresses of the same ILL, we 20446 * need to make sure that the soruce address of 20447 * the packet matches the logical IP address used 20448 * in the option. We do it by initializing ipha_src 20449 * here. This should keep IPSEC also happy as 20450 * when we return from IPSEC processing, we don't 20451 * have to worry about getting the right address on 20452 * the packet. Thus it is sufficient to look for 20453 * IRE_CACHE using MATCH_IRE_ILL rathen than 20454 * MATCH_IRE_IPIF. 20455 * 20456 * NOTE : We need to do it for non-secure case also as 20457 * this might go out secure if there is a global policy 20458 * match in ip_wput_ire. For bind to IPIF_NOFAILOVER 20459 * address, the source should be initialized already and 20460 * hence we won't be initializing here. 20461 * 20462 * As we do not have the ire yet, it is possible that 20463 * we set the source address here and then later discover 20464 * that the ire implies the source address to be assigned 20465 * through the RTF_SETSRC flag. 20466 * In that case, the setsrc variable will remind us 20467 * that overwritting the source address by the one 20468 * of the RTF_SETSRC-flagged ire is allowed. 20469 */ 20470 if (ipha->ipha_src == INADDR_ANY && 20471 (connp == NULL || !connp->conn_unspec_src)) { 20472 ipha->ipha_src = ipif->ipif_src_addr; 20473 setsrc = RTF_SETSRC; 20474 } 20475 /* 20476 * Find an IRE which matches the destination and the outgoing 20477 * queue (i.e. the outgoing interface.) 20478 * For loopback use a unicast IP address for 20479 * the ire lookup. 20480 */ 20481 if (ipif->ipif_ill->ill_phyint->phyint_flags & 20482 PHYI_LOOPBACK) { 20483 dst = ipif->ipif_lcl_addr; 20484 } 20485 /* 20486 * If IP_XMIT_IF is set, we branch out to ip_newroute_ipif. 20487 * We don't need to lookup ire in ctable as the packet 20488 * needs to be sent to the destination through the specified 20489 * ill irrespective of ires in the cache table. 20490 */ 20491 ire = NULL; 20492 if (xmit_ill == NULL) { 20493 ire = ire_ctable_lookup(dst, 0, 0, ipif, 20494 zoneid, MBLK_GETLABEL(mp), match_flags); 20495 } 20496 20497 /* 20498 * refrele attach_ill as its not needed anymore. 20499 */ 20500 if (attach_ill != NULL) { 20501 ill_refrele(attach_ill); 20502 attach_ill = NULL; 20503 } 20504 20505 if (ire == NULL) { 20506 /* 20507 * Multicast loopback and multicast forwarding is 20508 * done in ip_wput_ire. 20509 * 20510 * Mark this packet to make it be delivered to 20511 * ip_wput_ire after the new ire has been 20512 * created. 20513 * 20514 * The call to ip_newroute_ipif takes into account 20515 * the setsrc reminder. In any case, we take care 20516 * of the RTF_MULTIRT flag. 20517 */ 20518 mp->b_prev = mp->b_next = NULL; 20519 if (xmit_ill == NULL || 20520 xmit_ill->ill_ipif_up_count > 0) { 20521 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 20522 setsrc | RTF_MULTIRT, zoneid); 20523 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20524 "ip_wput_end: q %p (%S)", q, "noire"); 20525 } else { 20526 freemsg(first_mp); 20527 } 20528 ipif_refrele(ipif); 20529 if (xmit_ill != NULL) 20530 ill_refrele(xmit_ill); 20531 if (need_decref) 20532 CONN_DEC_REF(connp); 20533 return; 20534 } 20535 20536 ipif_refrele(ipif); 20537 ipif = NULL; 20538 ASSERT(xmit_ill == NULL); 20539 20540 /* 20541 * Honor the RTF_SETSRC flag for multicast packets, 20542 * if allowed by the setsrc reminder. 20543 */ 20544 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 20545 ipha->ipha_src = ire->ire_src_addr; 20546 } 20547 20548 /* 20549 * Unconditionally force the TTL to 1 for 20550 * multirouted multicast packets: 20551 * multirouted multicast should not cross 20552 * multicast routers. 20553 */ 20554 if (ire->ire_flags & RTF_MULTIRT) { 20555 if (ipha->ipha_ttl > 1) { 20556 ip2dbg(("ip_wput: forcing multicast " 20557 "multirt TTL to 1 (was %d), dst 0x%08x\n", 20558 ipha->ipha_ttl, ntohl(ire->ire_addr))); 20559 ipha->ipha_ttl = 1; 20560 } 20561 } 20562 } else { 20563 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 20564 if ((ire != NULL) && (ire->ire_type & 20565 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 20566 ignore_dontroute = B_TRUE; 20567 ignore_nexthop = B_TRUE; 20568 } 20569 if (ire != NULL) { 20570 ire_refrele(ire); 20571 ire = NULL; 20572 } 20573 /* 20574 * Guard against coming in from arp in which case conn is NULL. 20575 * Also guard against non M_DATA with dontroute set but 20576 * destined to local, loopback or broadcast addresses. 20577 */ 20578 if (connp != NULL && connp->conn_dontroute && 20579 !ignore_dontroute) { 20580 dontroute: 20581 /* 20582 * Set TTL to 1 if SO_DONTROUTE is set to prevent 20583 * routing protocols from seeing false direct 20584 * connectivity. 20585 */ 20586 ipha->ipha_ttl = 1; 20587 /* 20588 * If IP_XMIT_IF is also set (conn_xmit_if_ill != NULL) 20589 * along with SO_DONTROUTE, higher precedence is 20590 * given to IP_XMIT_IF and the IP_XMIT_IF ipif is used. 20591 */ 20592 if (connp->conn_xmit_if_ill == NULL) { 20593 /* If suitable ipif not found, drop packet */ 20594 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid); 20595 if (dst_ipif == NULL) { 20596 ip1dbg(("ip_wput: no route for " 20597 "dst using SO_DONTROUTE\n")); 20598 BUMP_MIB(&ip_mib, ipOutNoRoutes); 20599 mp->b_prev = mp->b_next = NULL; 20600 if (first_mp == NULL) 20601 first_mp = mp; 20602 goto drop_pkt; 20603 } else { 20604 /* 20605 * If suitable ipif has been found, set 20606 * xmit_ill to the corresponding 20607 * ipif_ill because we'll be following 20608 * the IP_XMIT_IF logic. 20609 */ 20610 ASSERT(xmit_ill == NULL); 20611 xmit_ill = dst_ipif->ipif_ill; 20612 mutex_enter(&xmit_ill->ill_lock); 20613 if (!ILL_CAN_LOOKUP(xmit_ill)) { 20614 mutex_exit(&xmit_ill->ill_lock); 20615 xmit_ill = NULL; 20616 ipif_refrele(dst_ipif); 20617 ip1dbg(("ip_wput: no route for" 20618 " dst using" 20619 " SO_DONTROUTE\n")); 20620 BUMP_MIB(&ip_mib, 20621 ipOutNoRoutes); 20622 mp->b_prev = mp->b_next = NULL; 20623 if (first_mp == NULL) 20624 first_mp = mp; 20625 goto drop_pkt; 20626 } 20627 ill_refhold_locked(xmit_ill); 20628 mutex_exit(&xmit_ill->ill_lock); 20629 ipif_refrele(dst_ipif); 20630 } 20631 } 20632 20633 } 20634 /* 20635 * If we are bound to IPIF_NOFAILOVER address, look for 20636 * an IRE_CACHE matching the ill. 20637 */ 20638 send_from_ill: 20639 if (attach_ill != NULL) { 20640 ipif_t *attach_ipif; 20641 20642 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 20643 20644 /* 20645 * Check if we need an ire that will not be 20646 * looked up by anybody else i.e. HIDDEN. 20647 */ 20648 if (ill_is_probeonly(attach_ill)) { 20649 match_flags |= MATCH_IRE_MARK_HIDDEN; 20650 } 20651 20652 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 20653 if (attach_ipif == NULL) { 20654 ip1dbg(("ip_wput: No ipif for attach_ill\n")); 20655 goto drop_pkt; 20656 } 20657 ire = ire_ctable_lookup(dst, 0, 0, attach_ipif, 20658 zoneid, MBLK_GETLABEL(mp), match_flags); 20659 ipif_refrele(attach_ipif); 20660 } else if (xmit_ill != NULL || (connp != NULL && 20661 connp->conn_xmit_if_ill != NULL)) { 20662 /* 20663 * Mark this packet as originated locally 20664 */ 20665 mp->b_prev = mp->b_next = NULL; 20666 /* 20667 * xmit_ill could be NULL if SO_DONTROUTE 20668 * is also set. 20669 */ 20670 if (xmit_ill == NULL) { 20671 xmit_ill = conn_get_held_ill(connp, 20672 &connp->conn_xmit_if_ill, &err); 20673 if (err == ILL_LOOKUP_FAILED) { 20674 if (need_decref) 20675 CONN_DEC_REF(connp); 20676 freemsg(first_mp); 20677 return; 20678 } 20679 if (xmit_ill == NULL) { 20680 if (connp->conn_dontroute) 20681 goto dontroute; 20682 goto send_from_ill; 20683 } 20684 } 20685 /* 20686 * could be SO_DONTROUTE case also. 20687 * check at least one interface is UP as 20688 * spcified by this ILL, and then call 20689 * ip_newroute_ipif() 20690 */ 20691 if (xmit_ill->ill_ipif_up_count > 0) { 20692 ipif_t *ipif; 20693 20694 ipif = ipif_get_next_ipif(NULL, xmit_ill); 20695 if (ipif != NULL) { 20696 ip_newroute_ipif(q, first_mp, ipif, 20697 dst, connp, 0, zoneid); 20698 ipif_refrele(ipif); 20699 ip1dbg(("ip_wput: ip_unicast_if\n")); 20700 } 20701 } else { 20702 freemsg(first_mp); 20703 } 20704 ill_refrele(xmit_ill); 20705 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20706 "ip_wput_end: q %p (%S)", q, "unicast_if"); 20707 if (need_decref) 20708 CONN_DEC_REF(connp); 20709 return; 20710 } else if (ip_nexthop || (connp != NULL && 20711 (connp->conn_nexthop_set)) && !ignore_nexthop) { 20712 if (!ip_nexthop) { 20713 ip_nexthop = B_TRUE; 20714 nexthop_addr = connp->conn_nexthop_v4; 20715 } 20716 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 20717 MATCH_IRE_GW; 20718 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 20719 NULL, zoneid, MBLK_GETLABEL(mp), match_flags); 20720 } else { 20721 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 20722 } 20723 if (!ire) { 20724 /* 20725 * Make sure we don't load spread if this 20726 * is IPIF_NOFAILOVER case. 20727 */ 20728 if ((attach_ill != NULL) || 20729 (ip_nexthop && !ignore_nexthop)) { 20730 if (mctl_present) { 20731 io = (ipsec_out_t *)first_mp->b_rptr; 20732 ASSERT(first_mp->b_datap->db_type == 20733 M_CTL); 20734 ASSERT(io->ipsec_out_type == IPSEC_OUT); 20735 } else { 20736 ASSERT(mp == first_mp); 20737 first_mp = allocb( 20738 sizeof (ipsec_info_t), BPRI_HI); 20739 if (first_mp == NULL) { 20740 first_mp = mp; 20741 goto drop_pkt; 20742 } 20743 first_mp->b_datap->db_type = M_CTL; 20744 first_mp->b_wptr += 20745 sizeof (ipsec_info_t); 20746 /* ipsec_out_secure is B_FALSE now */ 20747 bzero(first_mp->b_rptr, 20748 sizeof (ipsec_info_t)); 20749 io = (ipsec_out_t *)first_mp->b_rptr; 20750 io->ipsec_out_type = IPSEC_OUT; 20751 io->ipsec_out_len = 20752 sizeof (ipsec_out_t); 20753 io->ipsec_out_use_global_policy = 20754 B_TRUE; 20755 first_mp->b_cont = mp; 20756 mctl_present = B_TRUE; 20757 } 20758 if (attach_ill != NULL) { 20759 io->ipsec_out_ill_index = attach_ill-> 20760 ill_phyint->phyint_ifindex; 20761 io->ipsec_out_attach_if = B_TRUE; 20762 } else { 20763 io->ipsec_out_ip_nexthop = ip_nexthop; 20764 io->ipsec_out_nexthop_addr = 20765 nexthop_addr; 20766 } 20767 } 20768 noirefound: 20769 /* 20770 * Mark this packet as having originated on 20771 * this machine. This will be noted in 20772 * ire_add_then_send, which needs to know 20773 * whether to run it back through ip_wput or 20774 * ip_rput following successful resolution. 20775 */ 20776 mp->b_prev = NULL; 20777 mp->b_next = NULL; 20778 ip_newroute(q, first_mp, dst, NULL, connp, zoneid); 20779 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20780 "ip_wput_end: q %p (%S)", q, "newroute"); 20781 if (attach_ill != NULL) 20782 ill_refrele(attach_ill); 20783 if (xmit_ill != NULL) 20784 ill_refrele(xmit_ill); 20785 if (need_decref) 20786 CONN_DEC_REF(connp); 20787 return; 20788 } 20789 } 20790 20791 /* We now know where we are going with it. */ 20792 20793 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20794 "ip_wput_end: q %p (%S)", q, "end"); 20795 20796 /* 20797 * Check if the ire has the RTF_MULTIRT flag, inherited 20798 * from an IRE_OFFSUBNET ire entry in ip_newroute. 20799 */ 20800 if (ire->ire_flags & RTF_MULTIRT) { 20801 /* 20802 * Force the TTL of multirouted packets if required. 20803 * The TTL of such packets is bounded by the 20804 * ip_multirt_ttl ndd variable. 20805 */ 20806 if ((ip_multirt_ttl > 0) && 20807 (ipha->ipha_ttl > ip_multirt_ttl)) { 20808 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20809 "(was %d), dst 0x%08x\n", 20810 ip_multirt_ttl, ipha->ipha_ttl, 20811 ntohl(ire->ire_addr))); 20812 ipha->ipha_ttl = ip_multirt_ttl; 20813 } 20814 /* 20815 * At this point, we check to see if there are any pending 20816 * unresolved routes. ire_multirt_resolvable() 20817 * checks in O(n) that all IRE_OFFSUBNET ire 20818 * entries for the packet's destination and 20819 * flagged RTF_MULTIRT are currently resolved. 20820 * If some remain unresolved, we make a copy 20821 * of the current message. It will be used 20822 * to initiate additional route resolutions. 20823 */ 20824 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 20825 MBLK_GETLABEL(first_mp)); 20826 ip2dbg(("ip_wput[noirefound]: ire %p, " 20827 "multirt_need_resolve %d, first_mp %p\n", 20828 (void *)ire, multirt_need_resolve, (void *)first_mp)); 20829 if (multirt_need_resolve) { 20830 copy_mp = copymsg(first_mp); 20831 if (copy_mp != NULL) { 20832 MULTIRT_DEBUG_TAG(copy_mp); 20833 } 20834 } 20835 } 20836 20837 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20838 /* 20839 * Try to resolve another multiroute if 20840 * ire_multirt_resolvable() deemed it necessary. 20841 * At this point, we need to distinguish 20842 * multicasts from other packets. For multicasts, 20843 * we call ip_newroute_ipif() and request that both 20844 * multirouting and setsrc flags are checked. 20845 */ 20846 if (copy_mp != NULL) { 20847 if (CLASSD(dst)) { 20848 ipif_t *ipif = ipif_lookup_group(dst, zoneid); 20849 if (ipif) { 20850 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 20851 RTF_SETSRC | RTF_MULTIRT, zoneid); 20852 ipif_refrele(ipif); 20853 } else { 20854 MULTIRT_DEBUG_UNTAG(copy_mp); 20855 freemsg(copy_mp); 20856 copy_mp = NULL; 20857 } 20858 } else { 20859 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid); 20860 } 20861 } 20862 if (attach_ill != NULL) 20863 ill_refrele(attach_ill); 20864 if (xmit_ill != NULL) 20865 ill_refrele(xmit_ill); 20866 if (need_decref) 20867 CONN_DEC_REF(connp); 20868 return; 20869 20870 icmp_parameter_problem: 20871 /* could not have originated externally */ 20872 ASSERT(mp->b_prev == NULL); 20873 if (ip_hdr_complete(ipha, zoneid) == 0) { 20874 BUMP_MIB(&ip_mib, ipOutNoRoutes); 20875 /* it's the IP header length that's in trouble */ 20876 icmp_param_problem(q, first_mp, 0, zoneid); 20877 first_mp = NULL; 20878 } 20879 20880 drop_pkt: 20881 ip1dbg(("ip_wput: dropped packet\n")); 20882 if (ire != NULL) 20883 ire_refrele(ire); 20884 if (need_decref) 20885 CONN_DEC_REF(connp); 20886 freemsg(first_mp); 20887 if (attach_ill != NULL) 20888 ill_refrele(attach_ill); 20889 if (xmit_ill != NULL) 20890 ill_refrele(xmit_ill); 20891 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20892 "ip_wput_end: q %p (%S)", q, "droppkt"); 20893 } 20894 20895 /* 20896 * If this is a conn_t queue, then we pass in the conn. This includes the 20897 * zoneid. 20898 * Otherwise, this is a message coming back from ARP or for an ill_t queue, 20899 * in which case we use the global zoneid since those are all part of 20900 * the global zone. 20901 */ 20902 void 20903 ip_wput(queue_t *q, mblk_t *mp) 20904 { 20905 if (CONN_Q(q)) 20906 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 20907 else 20908 ip_output(GLOBAL_ZONEID, mp, q, IP_WPUT); 20909 } 20910 20911 /* 20912 * 20913 * The following rules must be observed when accessing any ipif or ill 20914 * that has been cached in the conn. Typically conn_nofailover_ill, 20915 * conn_xmit_if_ill, conn_multicast_ipif and conn_multicast_ill. 20916 * 20917 * Access: The ipif or ill pointed to from the conn can be accessed under 20918 * the protection of the conn_lock or after it has been refheld under the 20919 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 20920 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 20921 * The reason for this is that a concurrent unplumb could actually be 20922 * cleaning up these cached pointers by walking the conns and might have 20923 * finished cleaning up the conn in question. The macros check that an 20924 * unplumb has not yet started on the ipif or ill. 20925 * 20926 * Caching: An ipif or ill pointer may be cached in the conn only after 20927 * making sure that an unplumb has not started. So the caching is done 20928 * while holding both the conn_lock and the ill_lock and after using the 20929 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 20930 * flag before starting the cleanup of conns. 20931 * 20932 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 20933 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 20934 * or a reference to the ipif or a reference to an ire that references the 20935 * ipif. An ipif does not change its ill except for failover/failback. Since 20936 * failover/failback happens only after bringing down the ipif and making sure 20937 * the ipif refcnt has gone to zero and holding the ill_g_lock and ill_lock 20938 * the above holds. 20939 */ 20940 ipif_t * 20941 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 20942 { 20943 ipif_t *ipif; 20944 ill_t *ill; 20945 20946 *err = 0; 20947 rw_enter(&ill_g_lock, RW_READER); 20948 mutex_enter(&connp->conn_lock); 20949 ipif = *ipifp; 20950 if (ipif != NULL) { 20951 ill = ipif->ipif_ill; 20952 mutex_enter(&ill->ill_lock); 20953 if (IPIF_CAN_LOOKUP(ipif)) { 20954 ipif_refhold_locked(ipif); 20955 mutex_exit(&ill->ill_lock); 20956 mutex_exit(&connp->conn_lock); 20957 rw_exit(&ill_g_lock); 20958 return (ipif); 20959 } else { 20960 *err = IPIF_LOOKUP_FAILED; 20961 } 20962 mutex_exit(&ill->ill_lock); 20963 } 20964 mutex_exit(&connp->conn_lock); 20965 rw_exit(&ill_g_lock); 20966 return (NULL); 20967 } 20968 20969 ill_t * 20970 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 20971 { 20972 ill_t *ill; 20973 20974 *err = 0; 20975 mutex_enter(&connp->conn_lock); 20976 ill = *illp; 20977 if (ill != NULL) { 20978 mutex_enter(&ill->ill_lock); 20979 if (ILL_CAN_LOOKUP(ill)) { 20980 ill_refhold_locked(ill); 20981 mutex_exit(&ill->ill_lock); 20982 mutex_exit(&connp->conn_lock); 20983 return (ill); 20984 } else { 20985 *err = ILL_LOOKUP_FAILED; 20986 } 20987 mutex_exit(&ill->ill_lock); 20988 } 20989 mutex_exit(&connp->conn_lock); 20990 return (NULL); 20991 } 20992 20993 static int 20994 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 20995 { 20996 ill_t *ill; 20997 20998 ill = ipif->ipif_ill; 20999 mutex_enter(&connp->conn_lock); 21000 mutex_enter(&ill->ill_lock); 21001 if (IPIF_CAN_LOOKUP(ipif)) { 21002 *ipifp = ipif; 21003 mutex_exit(&ill->ill_lock); 21004 mutex_exit(&connp->conn_lock); 21005 return (0); 21006 } 21007 mutex_exit(&ill->ill_lock); 21008 mutex_exit(&connp->conn_lock); 21009 return (IPIF_LOOKUP_FAILED); 21010 } 21011 21012 /* 21013 * This is called if the outbound datagram needs fragmentation. 21014 * 21015 * NOTE : This function does not ire_refrele the ire argument passed in. 21016 */ 21017 static void 21018 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire, zoneid_t zoneid) 21019 { 21020 ipha_t *ipha; 21021 mblk_t *mp; 21022 uint32_t v_hlen_tos_len; 21023 uint32_t max_frag; 21024 uint32_t frag_flag; 21025 boolean_t dont_use; 21026 21027 if (ipsec_mp->b_datap->db_type == M_CTL) { 21028 mp = ipsec_mp->b_cont; 21029 } else { 21030 mp = ipsec_mp; 21031 } 21032 21033 ipha = (ipha_t *)mp->b_rptr; 21034 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21035 21036 #ifdef _BIG_ENDIAN 21037 #define V_HLEN (v_hlen_tos_len >> 24) 21038 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21039 #else 21040 #define V_HLEN (v_hlen_tos_len & 0xFF) 21041 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21042 #endif 21043 21044 #ifndef SPEED_BEFORE_SAFETY 21045 /* 21046 * Check that ipha_length is consistent with 21047 * the mblk length 21048 */ 21049 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 21050 ip0dbg(("Packet length mismatch: %d, %ld\n", 21051 LENGTH, msgdsize(mp))); 21052 freemsg(ipsec_mp); 21053 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21054 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 21055 "packet length mismatch"); 21056 return; 21057 } 21058 #endif 21059 /* 21060 * Don't use frag_flag if pre-built packet or source 21061 * routed or if multicast (since multicast packets do not solicit 21062 * ICMP "packet too big" messages). Get the values of 21063 * max_frag and frag_flag atomically by acquiring the 21064 * ire_lock. 21065 */ 21066 mutex_enter(&ire->ire_lock); 21067 max_frag = ire->ire_max_frag; 21068 frag_flag = ire->ire_frag_flag; 21069 mutex_exit(&ire->ire_lock); 21070 21071 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 21072 (V_HLEN != IP_SIMPLE_HDR_VERSION && 21073 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 21074 21075 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 21076 (dont_use ? 0 : frag_flag), zoneid); 21077 } 21078 21079 /* 21080 * Used for deciding the MSS size for the upper layer. Thus 21081 * we need to check the outbound policy values in the conn. 21082 */ 21083 int 21084 conn_ipsec_length(conn_t *connp) 21085 { 21086 ipsec_latch_t *ipl; 21087 21088 ipl = connp->conn_latch; 21089 if (ipl == NULL) 21090 return (0); 21091 21092 if (ipl->ipl_out_policy == NULL) 21093 return (0); 21094 21095 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 21096 } 21097 21098 /* 21099 * Returns an estimate of the IPSEC headers size. This is used if 21100 * we don't want to call into IPSEC to get the exact size. 21101 */ 21102 int 21103 ipsec_out_extra_length(mblk_t *ipsec_mp) 21104 { 21105 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21106 ipsec_action_t *a; 21107 21108 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21109 if (!io->ipsec_out_secure) 21110 return (0); 21111 21112 a = io->ipsec_out_act; 21113 21114 if (a == NULL) { 21115 ASSERT(io->ipsec_out_policy != NULL); 21116 a = io->ipsec_out_policy->ipsp_act; 21117 } 21118 ASSERT(a != NULL); 21119 21120 return (a->ipa_ovhd); 21121 } 21122 21123 /* 21124 * Returns an estimate of the IPSEC headers size. This is used if 21125 * we don't want to call into IPSEC to get the exact size. 21126 */ 21127 int 21128 ipsec_in_extra_length(mblk_t *ipsec_mp) 21129 { 21130 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21131 ipsec_action_t *a; 21132 21133 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21134 21135 a = ii->ipsec_in_action; 21136 return (a == NULL ? 0 : a->ipa_ovhd); 21137 } 21138 21139 /* 21140 * If there are any source route options, return the true final 21141 * destination. Otherwise, return the destination. 21142 */ 21143 ipaddr_t 21144 ip_get_dst(ipha_t *ipha) 21145 { 21146 ipoptp_t opts; 21147 uchar_t *opt; 21148 uint8_t optval; 21149 uint8_t optlen; 21150 ipaddr_t dst; 21151 uint32_t off; 21152 21153 dst = ipha->ipha_dst; 21154 21155 if (IS_SIMPLE_IPH(ipha)) 21156 return (dst); 21157 21158 for (optval = ipoptp_first(&opts, ipha); 21159 optval != IPOPT_EOL; 21160 optval = ipoptp_next(&opts)) { 21161 opt = opts.ipoptp_cur; 21162 optlen = opts.ipoptp_len; 21163 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 21164 switch (optval) { 21165 case IPOPT_SSRR: 21166 case IPOPT_LSRR: 21167 off = opt[IPOPT_OFFSET]; 21168 /* 21169 * If one of the conditions is true, it means 21170 * end of options and dst already has the right 21171 * value. 21172 */ 21173 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 21174 off = optlen - IP_ADDR_LEN; 21175 bcopy(&opt[off], &dst, IP_ADDR_LEN); 21176 } 21177 return (dst); 21178 default: 21179 break; 21180 } 21181 } 21182 21183 return (dst); 21184 } 21185 21186 mblk_t * 21187 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 21188 conn_t *connp, boolean_t unspec_src, zoneid_t zoneid) 21189 { 21190 ipsec_out_t *io; 21191 mblk_t *first_mp; 21192 boolean_t policy_present; 21193 21194 first_mp = mp; 21195 if (mp->b_datap->db_type == M_CTL) { 21196 io = (ipsec_out_t *)first_mp->b_rptr; 21197 /* 21198 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 21199 * 21200 * 1) There is per-socket policy (including cached global 21201 * policy). 21202 * 2) There is no per-socket policy, but it is 21203 * a multicast packet that needs to go out 21204 * on a specific interface. This is the case 21205 * where (ip_wput and ip_wput_multicast) attaches 21206 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 21207 * 21208 * In case (2) we check with global policy to 21209 * see if there is a match and set the ill_index 21210 * appropriately so that we can lookup the ire 21211 * properly in ip_wput_ipsec_out. 21212 */ 21213 21214 /* 21215 * ipsec_out_use_global_policy is set to B_FALSE 21216 * in ipsec_in_to_out(). Refer to that function for 21217 * details. 21218 */ 21219 if ((io->ipsec_out_latch == NULL) && 21220 (io->ipsec_out_use_global_policy)) { 21221 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 21222 ire, connp, unspec_src, zoneid)); 21223 } 21224 if (!io->ipsec_out_secure) { 21225 /* 21226 * If this is not a secure packet, drop 21227 * the IPSEC_OUT mp and treat it as a clear 21228 * packet. This happens when we are sending 21229 * a ICMP reply back to a clear packet. See 21230 * ipsec_in_to_out() for details. 21231 */ 21232 mp = first_mp->b_cont; 21233 freeb(first_mp); 21234 } 21235 return (mp); 21236 } 21237 /* 21238 * See whether we need to attach a global policy here. We 21239 * don't depend on the conn (as it could be null) for deciding 21240 * what policy this datagram should go through because it 21241 * should have happened in ip_wput if there was some 21242 * policy. This normally happens for connections which are not 21243 * fully bound preventing us from caching policies in 21244 * ip_bind. Packets coming from the TCP listener/global queue 21245 * - which are non-hard_bound - could also be affected by 21246 * applying policy here. 21247 * 21248 * If this packet is coming from tcp global queue or listener, 21249 * we will be applying policy here. This may not be *right* 21250 * if these packets are coming from the detached connection as 21251 * it could have gone in clear before. This happens only if a 21252 * TCP connection started when there is no policy and somebody 21253 * added policy before it became detached. Thus packets of the 21254 * detached connection could go out secure and the other end 21255 * would drop it because it will be expecting in clear. The 21256 * converse is not true i.e if somebody starts a TCP 21257 * connection and deletes the policy, all the packets will 21258 * still go out with the policy that existed before deleting 21259 * because ip_unbind sends up policy information which is used 21260 * by TCP on subsequent ip_wputs. The right solution is to fix 21261 * TCP to attach a dummy IPSEC_OUT and set 21262 * ipsec_out_use_global_policy to B_FALSE. As this might 21263 * affect performance for normal cases, we are not doing it. 21264 * Thus, set policy before starting any TCP connections. 21265 * 21266 * NOTE - We might apply policy even for a hard bound connection 21267 * - for which we cached policy in ip_bind - if somebody added 21268 * global policy after we inherited the policy in ip_bind. 21269 * This means that the packets that were going out in clear 21270 * previously would start going secure and hence get dropped 21271 * on the other side. To fix this, TCP attaches a dummy 21272 * ipsec_out and make sure that we don't apply global policy. 21273 */ 21274 if (ipha != NULL) 21275 policy_present = ipsec_outbound_v4_policy_present; 21276 else 21277 policy_present = ipsec_outbound_v6_policy_present; 21278 if (!policy_present) 21279 return (mp); 21280 21281 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src, 21282 zoneid)); 21283 } 21284 21285 ire_t * 21286 conn_set_outgoing_ill(conn_t *connp, ire_t *ire, ill_t **conn_outgoing_ill) 21287 { 21288 ipaddr_t addr; 21289 ire_t *save_ire; 21290 irb_t *irb; 21291 ill_group_t *illgrp; 21292 int err; 21293 21294 save_ire = ire; 21295 addr = ire->ire_addr; 21296 21297 ASSERT(ire->ire_type == IRE_BROADCAST); 21298 21299 illgrp = connp->conn_outgoing_ill->ill_group; 21300 if (illgrp == NULL) { 21301 *conn_outgoing_ill = conn_get_held_ill(connp, 21302 &connp->conn_outgoing_ill, &err); 21303 if (err == ILL_LOOKUP_FAILED) { 21304 ire_refrele(save_ire); 21305 return (NULL); 21306 } 21307 return (save_ire); 21308 } 21309 /* 21310 * If IP_BOUND_IF has been done, conn_outgoing_ill will be set. 21311 * If it is part of the group, we need to send on the ire 21312 * that has been cleared of IRE_MARK_NORECV and that belongs 21313 * to this group. This is okay as IP_BOUND_IF really means 21314 * any ill in the group. We depend on the fact that the 21315 * first ire in the group is always cleared of IRE_MARK_NORECV 21316 * if such an ire exists. This is possible only if you have 21317 * at least one ill in the group that has not failed. 21318 * 21319 * First get to the ire that matches the address and group. 21320 * 21321 * We don't look for an ire with a matching zoneid because a given zone 21322 * won't always have broadcast ires on all ills in the group. 21323 */ 21324 irb = ire->ire_bucket; 21325 rw_enter(&irb->irb_lock, RW_READER); 21326 if (ire->ire_marks & IRE_MARK_NORECV) { 21327 /* 21328 * If the current zone only has an ire broadcast for this 21329 * address marked NORECV, the ire we want is ahead in the 21330 * bucket, so we look it up deliberately ignoring the zoneid. 21331 */ 21332 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 21333 if (ire->ire_addr != addr) 21334 continue; 21335 /* skip over deleted ires */ 21336 if (ire->ire_marks & IRE_MARK_CONDEMNED) 21337 continue; 21338 } 21339 } 21340 while (ire != NULL) { 21341 /* 21342 * If a new interface is coming up, we could end up 21343 * seeing the loopback ire and the non-loopback ire 21344 * may not have been added yet. So check for ire_stq 21345 */ 21346 if (ire->ire_stq != NULL && (ire->ire_addr != addr || 21347 ire->ire_ipif->ipif_ill->ill_group == illgrp)) { 21348 break; 21349 } 21350 ire = ire->ire_next; 21351 } 21352 if (ire != NULL && ire->ire_addr == addr && 21353 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 21354 IRE_REFHOLD(ire); 21355 rw_exit(&irb->irb_lock); 21356 ire_refrele(save_ire); 21357 *conn_outgoing_ill = ire_to_ill(ire); 21358 /* 21359 * Refhold the ill to make the conn_outgoing_ill 21360 * independent of the ire. ip_wput_ire goes in a loop 21361 * and may refrele the ire. Since we have an ire at this 21362 * point we don't need to use ILL_CAN_LOOKUP on the ill. 21363 */ 21364 ill_refhold(*conn_outgoing_ill); 21365 return (ire); 21366 } 21367 rw_exit(&irb->irb_lock); 21368 ip1dbg(("conn_set_outgoing_ill: No matching ire\n")); 21369 /* 21370 * If we can't find a suitable ire, return the original ire. 21371 */ 21372 return (save_ire); 21373 } 21374 21375 /* 21376 * This function does the ire_refrele of the ire passed in as the 21377 * argument. As this function looks up more ires i.e broadcast ires, 21378 * it needs to REFRELE them. Currently, for simplicity we don't 21379 * differentiate the one passed in and looked up here. We always 21380 * REFRELE. 21381 * IPQoS Notes: 21382 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 21383 * IPSec packets are done in ipsec_out_process. 21384 * 21385 */ 21386 void 21387 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller, 21388 zoneid_t zoneid) 21389 { 21390 ipha_t *ipha; 21391 #define rptr ((uchar_t *)ipha) 21392 queue_t *stq; 21393 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 21394 uint32_t v_hlen_tos_len; 21395 uint32_t ttl_protocol; 21396 ipaddr_t src; 21397 ipaddr_t dst; 21398 uint32_t cksum; 21399 ipaddr_t orig_src; 21400 ire_t *ire1; 21401 mblk_t *next_mp; 21402 uint_t hlen; 21403 uint16_t *up; 21404 uint32_t max_frag = ire->ire_max_frag; 21405 ill_t *ill = ire_to_ill(ire); 21406 int clusterwide; 21407 uint16_t ip_hdr_included; /* IP header included by ULP? */ 21408 int ipsec_len; 21409 mblk_t *first_mp; 21410 ipsec_out_t *io; 21411 boolean_t conn_dontroute; /* conn value for multicast */ 21412 boolean_t conn_multicast_loop; /* conn value for multicast */ 21413 boolean_t multicast_forward; /* Should we forward ? */ 21414 boolean_t unspec_src; 21415 ill_t *conn_outgoing_ill = NULL; 21416 ill_t *ire_ill; 21417 ill_t *ire1_ill; 21418 ill_t *out_ill; 21419 uint32_t ill_index = 0; 21420 boolean_t multirt_send = B_FALSE; 21421 int err; 21422 ipxmit_state_t pktxmit_state; 21423 21424 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 21425 "ip_wput_ire_start: q %p", q); 21426 21427 multicast_forward = B_FALSE; 21428 unspec_src = (connp != NULL && connp->conn_unspec_src); 21429 21430 if (ire->ire_flags & RTF_MULTIRT) { 21431 /* 21432 * Multirouting case. The bucket where ire is stored 21433 * probably holds other RTF_MULTIRT flagged ire 21434 * to the destination. In this call to ip_wput_ire, 21435 * we attempt to send the packet through all 21436 * those ires. Thus, we first ensure that ire is the 21437 * first RTF_MULTIRT ire in the bucket, 21438 * before walking the ire list. 21439 */ 21440 ire_t *first_ire; 21441 irb_t *irb = ire->ire_bucket; 21442 ASSERT(irb != NULL); 21443 21444 /* Make sure we do not omit any multiroute ire. */ 21445 IRB_REFHOLD(irb); 21446 for (first_ire = irb->irb_ire; 21447 first_ire != NULL; 21448 first_ire = first_ire->ire_next) { 21449 if ((first_ire->ire_flags & RTF_MULTIRT) && 21450 (first_ire->ire_addr == ire->ire_addr) && 21451 !(first_ire->ire_marks & 21452 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 21453 break; 21454 } 21455 21456 if ((first_ire != NULL) && (first_ire != ire)) { 21457 IRE_REFHOLD(first_ire); 21458 ire_refrele(ire); 21459 ire = first_ire; 21460 ill = ire_to_ill(ire); 21461 } 21462 IRB_REFRELE(irb); 21463 } 21464 21465 /* 21466 * conn_outgoing_ill is used only in the broadcast loop. 21467 * for performance we don't grab the mutexs in the fastpath 21468 */ 21469 if ((connp != NULL) && 21470 (connp->conn_xmit_if_ill == NULL) && 21471 (ire->ire_type == IRE_BROADCAST) && 21472 ((connp->conn_nofailover_ill != NULL) || 21473 (connp->conn_outgoing_ill != NULL))) { 21474 /* 21475 * Bind to IPIF_NOFAILOVER address overrides IP_BOUND_IF 21476 * option. So, see if this endpoint is bound to a 21477 * IPIF_NOFAILOVER address. If so, honor it. This implies 21478 * that if the interface is failed, we will still send 21479 * the packet on the same ill which is what we want. 21480 */ 21481 conn_outgoing_ill = conn_get_held_ill(connp, 21482 &connp->conn_nofailover_ill, &err); 21483 if (err == ILL_LOOKUP_FAILED) { 21484 ire_refrele(ire); 21485 freemsg(mp); 21486 return; 21487 } 21488 if (conn_outgoing_ill == NULL) { 21489 /* 21490 * Choose a good ill in the group to send the 21491 * packets on. 21492 */ 21493 ire = conn_set_outgoing_ill(connp, ire, 21494 &conn_outgoing_ill); 21495 if (ire == NULL) { 21496 freemsg(mp); 21497 return; 21498 } 21499 } 21500 } 21501 21502 if (mp->b_datap->db_type != M_CTL) { 21503 ipha = (ipha_t *)mp->b_rptr; 21504 } else { 21505 io = (ipsec_out_t *)mp->b_rptr; 21506 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21507 ASSERT(zoneid == io->ipsec_out_zoneid); 21508 ASSERT(zoneid != ALL_ZONES); 21509 ipha = (ipha_t *)mp->b_cont->b_rptr; 21510 dst = ipha->ipha_dst; 21511 /* 21512 * For the multicast case, ipsec_out carries conn_dontroute and 21513 * conn_multicast_loop as conn may not be available here. We 21514 * need this for multicast loopback and forwarding which is done 21515 * later in the code. 21516 */ 21517 if (CLASSD(dst)) { 21518 conn_dontroute = io->ipsec_out_dontroute; 21519 conn_multicast_loop = io->ipsec_out_multicast_loop; 21520 /* 21521 * If conn_dontroute is not set or conn_multicast_loop 21522 * is set, we need to do forwarding/loopback. For 21523 * datagrams from ip_wput_multicast, conn_dontroute is 21524 * set to B_TRUE and conn_multicast_loop is set to 21525 * B_FALSE so that we neither do forwarding nor 21526 * loopback. 21527 */ 21528 if (!conn_dontroute || conn_multicast_loop) 21529 multicast_forward = B_TRUE; 21530 } 21531 } 21532 21533 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 21534 ire->ire_zoneid != ALL_ZONES) { 21535 /* 21536 * When a zone sends a packet to another zone, we try to deliver 21537 * the packet under the same conditions as if the destination 21538 * was a real node on the network. To do so, we look for a 21539 * matching route in the forwarding table. 21540 * RTF_REJECT and RTF_BLACKHOLE are handled just like 21541 * ip_newroute() does. 21542 * Note that IRE_LOCAL are special, since they are used 21543 * when the zoneid doesn't match in some cases. This means that 21544 * we need to handle ipha_src differently since ire_src_addr 21545 * belongs to the receiving zone instead of the sending zone. 21546 * When ip_restrict_interzone_loopback is set, then 21547 * ire_cache_lookup() ensures that IRE_LOCAL are only used 21548 * for loopback between zones when the logical "Ethernet" would 21549 * have looped them back. 21550 */ 21551 ire_t *src_ire; 21552 21553 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 21554 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 21555 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE)); 21556 if (src_ire != NULL && 21557 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) && 21558 (!ip_restrict_interzone_loopback || 21559 ire_local_same_ill_group(ire, src_ire))) { 21560 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 21561 ipha->ipha_src = src_ire->ire_src_addr; 21562 ire_refrele(src_ire); 21563 } else { 21564 ire_refrele(ire); 21565 if (conn_outgoing_ill != NULL) 21566 ill_refrele(conn_outgoing_ill); 21567 BUMP_MIB(&ip_mib, ipOutNoRoutes); 21568 if (src_ire != NULL) { 21569 if (src_ire->ire_flags & RTF_BLACKHOLE) { 21570 ire_refrele(src_ire); 21571 freemsg(mp); 21572 return; 21573 } 21574 ire_refrele(src_ire); 21575 } 21576 if (ip_hdr_complete(ipha, zoneid)) { 21577 /* Failed */ 21578 freemsg(mp); 21579 return; 21580 } 21581 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, zoneid); 21582 return; 21583 } 21584 } 21585 21586 if (mp->b_datap->db_type == M_CTL || 21587 ipsec_outbound_v4_policy_present) { 21588 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 21589 unspec_src, zoneid); 21590 if (mp == NULL) { 21591 ire_refrele(ire); 21592 if (conn_outgoing_ill != NULL) 21593 ill_refrele(conn_outgoing_ill); 21594 return; 21595 } 21596 } 21597 21598 first_mp = mp; 21599 ipsec_len = 0; 21600 21601 if (first_mp->b_datap->db_type == M_CTL) { 21602 io = (ipsec_out_t *)first_mp->b_rptr; 21603 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21604 mp = first_mp->b_cont; 21605 ipsec_len = ipsec_out_extra_length(first_mp); 21606 ASSERT(ipsec_len >= 0); 21607 /* We already picked up the zoneid from the M_CTL above */ 21608 ASSERT(zoneid == io->ipsec_out_zoneid); 21609 ASSERT(zoneid != ALL_ZONES); 21610 21611 /* 21612 * Drop M_CTL here if IPsec processing is not needed. 21613 * (Non-IPsec use of M_CTL extracted any information it 21614 * needed above). 21615 */ 21616 if (ipsec_len == 0) { 21617 freeb(first_mp); 21618 first_mp = mp; 21619 } 21620 } 21621 21622 /* 21623 * Fast path for ip_wput_ire 21624 */ 21625 21626 ipha = (ipha_t *)mp->b_rptr; 21627 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21628 dst = ipha->ipha_dst; 21629 21630 /* 21631 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 21632 * if the socket is a SOCK_RAW type. The transport checksum should 21633 * be provided in the pre-built packet, so we don't need to compute it. 21634 * Also, other application set flags, like DF, should not be altered. 21635 * Other transport MUST pass down zero. 21636 */ 21637 ip_hdr_included = ipha->ipha_ident; 21638 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21639 21640 if (CLASSD(dst)) { 21641 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 21642 ntohl(dst), 21643 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 21644 ntohl(ire->ire_addr))); 21645 } 21646 21647 /* Macros to extract header fields from data already in registers */ 21648 #ifdef _BIG_ENDIAN 21649 #define V_HLEN (v_hlen_tos_len >> 24) 21650 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21651 #define PROTO (ttl_protocol & 0xFF) 21652 #else 21653 #define V_HLEN (v_hlen_tos_len & 0xFF) 21654 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21655 #define PROTO (ttl_protocol >> 8) 21656 #endif 21657 21658 21659 orig_src = src = ipha->ipha_src; 21660 /* (The loop back to "another" is explained down below.) */ 21661 another:; 21662 /* 21663 * Assign an ident value for this packet. We assign idents on 21664 * a per destination basis out of the IRE. There could be 21665 * other threads targeting the same destination, so we have to 21666 * arrange for a atomic increment. Note that we use a 32-bit 21667 * atomic add because it has better performance than its 21668 * 16-bit sibling. 21669 * 21670 * If running in cluster mode and if the source address 21671 * belongs to a replicated service then vector through 21672 * cl_inet_ipident vector to allocate ip identifier 21673 * NOTE: This is a contract private interface with the 21674 * clustering group. 21675 */ 21676 clusterwide = 0; 21677 if (cl_inet_ipident) { 21678 ASSERT(cl_inet_isclusterwide); 21679 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 21680 AF_INET, (uint8_t *)(uintptr_t)src)) { 21681 ipha->ipha_ident = (*cl_inet_ipident)(IPPROTO_IP, 21682 AF_INET, (uint8_t *)(uintptr_t)src, 21683 (uint8_t *)(uintptr_t)dst); 21684 clusterwide = 1; 21685 } 21686 } 21687 if (!clusterwide) { 21688 ipha->ipha_ident = 21689 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 21690 } 21691 21692 #ifndef _BIG_ENDIAN 21693 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21694 #endif 21695 21696 /* 21697 * Set source address unless sent on an ill or conn_unspec_src is set. 21698 * This is needed to obey conn_unspec_src when packets go through 21699 * ip_newroute + arp. 21700 * Assumes ip_newroute{,_multi} sets the source address as well. 21701 */ 21702 if (src == INADDR_ANY && !unspec_src) { 21703 /* 21704 * Assign the appropriate source address from the IRE if none 21705 * was specified. 21706 */ 21707 ASSERT(ire->ire_ipversion == IPV4_VERSION); 21708 21709 /* 21710 * With IP multipathing, broadcast packets are sent on the ire 21711 * that has been cleared of IRE_MARK_NORECV and that belongs to 21712 * the group. However, this ire might not be in the same zone so 21713 * we can't always use its source address. We look for a 21714 * broadcast ire in the same group and in the right zone. 21715 */ 21716 if (ire->ire_type == IRE_BROADCAST && 21717 ire->ire_zoneid != zoneid) { 21718 ire_t *src_ire = ire_ctable_lookup(dst, 0, 21719 IRE_BROADCAST, ire->ire_ipif, zoneid, NULL, 21720 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP)); 21721 if (src_ire != NULL) { 21722 src = src_ire->ire_src_addr; 21723 ire_refrele(src_ire); 21724 } else { 21725 ire_refrele(ire); 21726 if (conn_outgoing_ill != NULL) 21727 ill_refrele(conn_outgoing_ill); 21728 freemsg(first_mp); 21729 BUMP_MIB(&ip_mib, ipOutDiscards); 21730 return; 21731 } 21732 } else { 21733 src = ire->ire_src_addr; 21734 } 21735 21736 if (connp == NULL) { 21737 ip1dbg(("ip_wput_ire: no connp and no src " 21738 "address for dst 0x%x, using src 0x%x\n", 21739 ntohl(dst), 21740 ntohl(src))); 21741 } 21742 ipha->ipha_src = src; 21743 } 21744 stq = ire->ire_stq; 21745 21746 /* 21747 * We only allow ire chains for broadcasts since there will 21748 * be multiple IRE_CACHE entries for the same multicast 21749 * address (one per ipif). 21750 */ 21751 next_mp = NULL; 21752 21753 /* broadcast packet */ 21754 if (ire->ire_type == IRE_BROADCAST) 21755 goto broadcast; 21756 21757 /* loopback ? */ 21758 if (stq == NULL) 21759 goto nullstq; 21760 21761 /* The ill_index for outbound ILL */ 21762 ill_index = Q_TO_INDEX(stq); 21763 21764 BUMP_MIB(&ip_mib, ipOutRequests); 21765 ttl_protocol = ((uint16_t *)ipha)[4]; 21766 21767 /* pseudo checksum (do it in parts for IP header checksum) */ 21768 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21769 21770 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 21771 queue_t *dev_q = stq->q_next; 21772 21773 /* flow controlled */ 21774 if ((dev_q->q_next || dev_q->q_first) && 21775 !canput(dev_q)) 21776 goto blocked; 21777 if ((PROTO == IPPROTO_UDP) && 21778 (ip_hdr_included != IP_HDR_INCLUDED)) { 21779 hlen = (V_HLEN & 0xF) << 2; 21780 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 21781 if (*up != 0) { 21782 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 21783 hlen, LENGTH, max_frag, ipsec_len, cksum); 21784 /* Software checksum? */ 21785 if (DB_CKSUMFLAGS(mp) == 0) { 21786 IP_STAT(ip_out_sw_cksum); 21787 IP_STAT_UPDATE( 21788 ip_udp_out_sw_cksum_bytes, 21789 LENGTH - hlen); 21790 } 21791 } 21792 } 21793 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 21794 hlen = (V_HLEN & 0xF) << 2; 21795 if (PROTO == IPPROTO_TCP) { 21796 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 21797 /* 21798 * The packet header is processed once and for all, even 21799 * in the multirouting case. We disable hardware 21800 * checksum if the packet is multirouted, as it will be 21801 * replicated via several interfaces, and not all of 21802 * them may have this capability. 21803 */ 21804 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 21805 LENGTH, max_frag, ipsec_len, cksum); 21806 /* Software checksum? */ 21807 if (DB_CKSUMFLAGS(mp) == 0) { 21808 IP_STAT(ip_out_sw_cksum); 21809 IP_STAT_UPDATE(ip_tcp_out_sw_cksum_bytes, 21810 LENGTH - hlen); 21811 } 21812 } else { 21813 sctp_hdr_t *sctph; 21814 21815 ASSERT(PROTO == IPPROTO_SCTP); 21816 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 21817 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 21818 /* 21819 * Zero out the checksum field to ensure proper 21820 * checksum calculation. 21821 */ 21822 sctph->sh_chksum = 0; 21823 #ifdef DEBUG 21824 if (!skip_sctp_cksum) 21825 #endif 21826 sctph->sh_chksum = sctp_cksum(mp, hlen); 21827 } 21828 } 21829 21830 /* 21831 * If this is a multicast packet and originated from ip_wput 21832 * we need to do loopback and forwarding checks. If it comes 21833 * from ip_wput_multicast, we SHOULD not do this. 21834 */ 21835 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 21836 21837 /* checksum */ 21838 cksum += ttl_protocol; 21839 21840 /* fragment the packet */ 21841 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 21842 goto fragmentit; 21843 /* 21844 * Don't use frag_flag if packet is pre-built or source 21845 * routed or if multicast (since multicast packets do 21846 * not solicit ICMP "packet too big" messages). 21847 */ 21848 if ((ip_hdr_included != IP_HDR_INCLUDED) && 21849 (V_HLEN == IP_SIMPLE_HDR_VERSION || 21850 !ip_source_route_included(ipha)) && 21851 !CLASSD(ipha->ipha_dst)) 21852 ipha->ipha_fragment_offset_and_flags |= 21853 htons(ire->ire_frag_flag); 21854 21855 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 21856 /* calculate IP header checksum */ 21857 cksum += ipha->ipha_ident; 21858 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 21859 cksum += ipha->ipha_fragment_offset_and_flags; 21860 21861 /* IP options present */ 21862 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 21863 if (hlen) 21864 goto checksumoptions; 21865 21866 /* calculate hdr checksum */ 21867 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 21868 cksum = ~(cksum + (cksum >> 16)); 21869 ipha->ipha_hdr_checksum = (uint16_t)cksum; 21870 } 21871 if (ipsec_len != 0) { 21872 /* 21873 * We will do the rest of the processing after 21874 * we come back from IPSEC in ip_wput_ipsec_out(). 21875 */ 21876 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 21877 21878 io = (ipsec_out_t *)first_mp->b_rptr; 21879 io->ipsec_out_ill_index = ((ill_t *)stq->q_ptr)-> 21880 ill_phyint->phyint_ifindex; 21881 21882 ipsec_out_process(q, first_mp, ire, ill_index); 21883 ire_refrele(ire); 21884 if (conn_outgoing_ill != NULL) 21885 ill_refrele(conn_outgoing_ill); 21886 return; 21887 } 21888 21889 /* 21890 * In most cases, the emission loop below is entered only 21891 * once. Only in the case where the ire holds the 21892 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 21893 * flagged ires in the bucket, and send the packet 21894 * through all crossed RTF_MULTIRT routes. 21895 */ 21896 if (ire->ire_flags & RTF_MULTIRT) { 21897 multirt_send = B_TRUE; 21898 } 21899 do { 21900 if (multirt_send) { 21901 irb_t *irb; 21902 /* 21903 * We are in a multiple send case, need to get 21904 * the next ire and make a duplicate of the packet. 21905 * ire1 holds here the next ire to process in the 21906 * bucket. If multirouting is expected, 21907 * any non-RTF_MULTIRT ire that has the 21908 * right destination address is ignored. 21909 */ 21910 irb = ire->ire_bucket; 21911 ASSERT(irb != NULL); 21912 21913 IRB_REFHOLD(irb); 21914 for (ire1 = ire->ire_next; 21915 ire1 != NULL; 21916 ire1 = ire1->ire_next) { 21917 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 21918 continue; 21919 if (ire1->ire_addr != ire->ire_addr) 21920 continue; 21921 if (ire1->ire_marks & 21922 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 21923 continue; 21924 21925 /* Got one */ 21926 IRE_REFHOLD(ire1); 21927 break; 21928 } 21929 IRB_REFRELE(irb); 21930 21931 if (ire1 != NULL) { 21932 next_mp = copyb(mp); 21933 if ((next_mp == NULL) || 21934 ((mp->b_cont != NULL) && 21935 ((next_mp->b_cont = 21936 dupmsg(mp->b_cont)) == NULL))) { 21937 freemsg(next_mp); 21938 next_mp = NULL; 21939 ire_refrele(ire1); 21940 ire1 = NULL; 21941 } 21942 } 21943 21944 /* Last multiroute ire; don't loop anymore. */ 21945 if (ire1 == NULL) { 21946 multirt_send = B_FALSE; 21947 } 21948 } 21949 21950 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 21951 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 21952 mblk_t *, mp); 21953 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 21954 MSG_FWCOOKED_OUT, NULL, ire->ire_ipif->ipif_ill, 21955 ipha, mp, mp); 21956 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21957 if (mp == NULL) 21958 goto release_ire_and_ill; 21959 21960 mp->b_prev = SET_BPREV_FLAG(IPP_LOCAL_OUT); 21961 DTRACE_PROBE2(ip__xmit__1, mblk_t *, mp, ire_t *, ire); 21962 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_TRUE); 21963 if ((pktxmit_state == SEND_FAILED) || 21964 (pktxmit_state == LLHDR_RESLV_FAILED)) { 21965 ip2dbg(("ip_wput_ire: ip_xmit_v4 failed" 21966 "- packet dropped\n")); 21967 release_ire_and_ill: 21968 ire_refrele(ire); 21969 if (next_mp != NULL) { 21970 freemsg(next_mp); 21971 ire_refrele(ire1); 21972 } 21973 if (conn_outgoing_ill != NULL) 21974 ill_refrele(conn_outgoing_ill); 21975 return; 21976 } 21977 21978 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21979 "ip_wput_ire_end: q %p (%S)", 21980 q, "last copy out"); 21981 IRE_REFRELE(ire); 21982 21983 if (multirt_send) { 21984 ASSERT(ire1); 21985 /* 21986 * Proceed with the next RTF_MULTIRT ire, 21987 * Also set up the send-to queue accordingly. 21988 */ 21989 ire = ire1; 21990 ire1 = NULL; 21991 stq = ire->ire_stq; 21992 mp = next_mp; 21993 next_mp = NULL; 21994 ipha = (ipha_t *)mp->b_rptr; 21995 ill_index = Q_TO_INDEX(stq); 21996 } 21997 } while (multirt_send); 21998 if (conn_outgoing_ill != NULL) 21999 ill_refrele(conn_outgoing_ill); 22000 return; 22001 22002 /* 22003 * ire->ire_type == IRE_BROADCAST (minimize diffs) 22004 */ 22005 broadcast: 22006 { 22007 /* 22008 * Avoid broadcast storms by setting the ttl to 1 22009 * for broadcasts. This parameter can be set 22010 * via ndd, so make sure that for the SO_DONTROUTE 22011 * case that ipha_ttl is always set to 1. 22012 * In the event that we are replying to incoming 22013 * ICMP packets, conn could be NULL. 22014 */ 22015 if ((connp != NULL) && connp->conn_dontroute) 22016 ipha->ipha_ttl = 1; 22017 else 22018 ipha->ipha_ttl = ip_broadcast_ttl; 22019 22020 /* 22021 * Note that we are not doing a IRB_REFHOLD here. 22022 * Actually we don't care if the list changes i.e 22023 * if somebody deletes an IRE from the list while 22024 * we drop the lock, the next time we come around 22025 * ire_next will be NULL and hence we won't send 22026 * out multiple copies which is fine. 22027 */ 22028 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 22029 ire1 = ire->ire_next; 22030 if (conn_outgoing_ill != NULL) { 22031 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 22032 ASSERT(ire1 == ire->ire_next); 22033 if (ire1 != NULL && ire1->ire_addr == dst) { 22034 ire_refrele(ire); 22035 ire = ire1; 22036 IRE_REFHOLD(ire); 22037 ire1 = ire->ire_next; 22038 continue; 22039 } 22040 rw_exit(&ire->ire_bucket->irb_lock); 22041 /* Did not find a matching ill */ 22042 ip1dbg(("ip_wput_ire: broadcast with no " 22043 "matching IP_BOUND_IF ill %s\n", 22044 conn_outgoing_ill->ill_name)); 22045 freemsg(first_mp); 22046 if (ire != NULL) 22047 ire_refrele(ire); 22048 ill_refrele(conn_outgoing_ill); 22049 return; 22050 } 22051 } else if (ire1 != NULL && ire1->ire_addr == dst) { 22052 /* 22053 * If the next IRE has the same address and is not one 22054 * of the two copies that we need to send, try to see 22055 * whether this copy should be sent at all. This 22056 * assumes that we insert loopbacks first and then 22057 * non-loopbacks. This is acheived by inserting the 22058 * loopback always before non-loopback. 22059 * This is used to send a single copy of a broadcast 22060 * packet out all physical interfaces that have an 22061 * matching IRE_BROADCAST while also looping 22062 * back one copy (to ip_wput_local) for each 22063 * matching physical interface. However, we avoid 22064 * sending packets out different logical that match by 22065 * having ipif_up/ipif_down supress duplicate 22066 * IRE_BROADCASTS. 22067 * 22068 * This feature is currently used to get broadcasts 22069 * sent to multiple interfaces, when the broadcast 22070 * address being used applies to multiple interfaces. 22071 * For example, a whole net broadcast will be 22072 * replicated on every connected subnet of 22073 * the target net. 22074 * 22075 * Each zone has its own set of IRE_BROADCASTs, so that 22076 * we're able to distribute inbound packets to multiple 22077 * zones who share a broadcast address. We avoid looping 22078 * back outbound packets in different zones but on the 22079 * same ill, as the application would see duplicates. 22080 * 22081 * If the interfaces are part of the same group, 22082 * we would want to send only one copy out for 22083 * whole group. 22084 * 22085 * This logic assumes that ire_add_v4() groups the 22086 * IRE_BROADCAST entries so that those with the same 22087 * ire_addr and ill_group are kept together. 22088 */ 22089 ire_ill = ire->ire_ipif->ipif_ill; 22090 if (ire->ire_stq == NULL && ire1->ire_stq != NULL) { 22091 if (ire_ill->ill_group != NULL && 22092 (ire->ire_marks & IRE_MARK_NORECV)) { 22093 /* 22094 * If the current zone only has an ire 22095 * broadcast for this address marked 22096 * NORECV, the ire we want is ahead in 22097 * the bucket, so we look it up 22098 * deliberately ignoring the zoneid. 22099 */ 22100 for (ire1 = ire->ire_bucket->irb_ire; 22101 ire1 != NULL; 22102 ire1 = ire1->ire_next) { 22103 ire1_ill = 22104 ire1->ire_ipif->ipif_ill; 22105 if (ire1->ire_addr != dst) 22106 continue; 22107 /* skip over the current ire */ 22108 if (ire1 == ire) 22109 continue; 22110 /* skip over deleted ires */ 22111 if (ire1->ire_marks & 22112 IRE_MARK_CONDEMNED) 22113 continue; 22114 /* 22115 * non-loopback ire in our 22116 * group: use it for the next 22117 * pass in the loop 22118 */ 22119 if (ire1->ire_stq != NULL && 22120 ire1_ill->ill_group == 22121 ire_ill->ill_group) 22122 break; 22123 } 22124 } 22125 } else { 22126 while (ire1 != NULL && ire1->ire_addr == dst) { 22127 ire1_ill = ire1->ire_ipif->ipif_ill; 22128 /* 22129 * We can have two broadcast ires on the 22130 * same ill in different zones; here 22131 * we'll send a copy of the packet on 22132 * each ill and the fanout code will 22133 * call conn_wantpacket() to check that 22134 * the zone has the broadcast address 22135 * configured on the ill. If the two 22136 * ires are in the same group we only 22137 * send one copy up. 22138 */ 22139 if (ire1_ill != ire_ill && 22140 (ire1_ill->ill_group == NULL || 22141 ire_ill->ill_group == NULL || 22142 ire1_ill->ill_group != 22143 ire_ill->ill_group)) { 22144 break; 22145 } 22146 ire1 = ire1->ire_next; 22147 } 22148 } 22149 } 22150 ASSERT(multirt_send == B_FALSE); 22151 if (ire1 != NULL && ire1->ire_addr == dst) { 22152 if ((ire->ire_flags & RTF_MULTIRT) && 22153 (ire1->ire_flags & RTF_MULTIRT)) { 22154 /* 22155 * We are in the multirouting case. 22156 * The message must be sent at least 22157 * on both ires. These ires have been 22158 * inserted AFTER the standard ones 22159 * in ip_rt_add(). There are thus no 22160 * other ire entries for the destination 22161 * address in the rest of the bucket 22162 * that do not have the RTF_MULTIRT 22163 * flag. We don't process a copy 22164 * of the message here. This will be 22165 * done in the final sending loop. 22166 */ 22167 multirt_send = B_TRUE; 22168 } else { 22169 next_mp = ip_copymsg(first_mp); 22170 if (next_mp != NULL) 22171 IRE_REFHOLD(ire1); 22172 } 22173 } 22174 rw_exit(&ire->ire_bucket->irb_lock); 22175 } 22176 22177 if (stq) { 22178 /* 22179 * A non-NULL send-to queue means this packet is going 22180 * out of this machine. 22181 */ 22182 22183 BUMP_MIB(&ip_mib, ipOutRequests); 22184 ttl_protocol = ((uint16_t *)ipha)[4]; 22185 /* 22186 * We accumulate the pseudo header checksum in cksum. 22187 * This is pretty hairy code, so watch close. One 22188 * thing to keep in mind is that UDP and TCP have 22189 * stored their respective datagram lengths in their 22190 * checksum fields. This lines things up real nice. 22191 */ 22192 cksum = (dst >> 16) + (dst & 0xFFFF) + 22193 (src >> 16) + (src & 0xFFFF); 22194 /* 22195 * We assume the udp checksum field contains the 22196 * length, so to compute the pseudo header checksum, 22197 * all we need is the protocol number and src/dst. 22198 */ 22199 /* Provide the checksums for UDP and TCP. */ 22200 if ((PROTO == IPPROTO_TCP) && 22201 (ip_hdr_included != IP_HDR_INCLUDED)) { 22202 /* hlen gets the number of uchar_ts in the IP header */ 22203 hlen = (V_HLEN & 0xF) << 2; 22204 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22205 IP_STAT(ip_out_sw_cksum); 22206 IP_STAT_UPDATE(ip_tcp_out_sw_cksum_bytes, 22207 LENGTH - hlen); 22208 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 22209 if (*up == 0) 22210 *up = 0xFFFF; 22211 } else if (PROTO == IPPROTO_SCTP && 22212 (ip_hdr_included != IP_HDR_INCLUDED)) { 22213 sctp_hdr_t *sctph; 22214 22215 hlen = (V_HLEN & 0xF) << 2; 22216 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22217 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22218 sctph->sh_chksum = 0; 22219 #ifdef DEBUG 22220 if (!skip_sctp_cksum) 22221 #endif 22222 sctph->sh_chksum = sctp_cksum(mp, hlen); 22223 } else { 22224 queue_t *dev_q = stq->q_next; 22225 22226 if ((dev_q->q_next || dev_q->q_first) && 22227 !canput(dev_q)) { 22228 blocked: 22229 ipha->ipha_ident = ip_hdr_included; 22230 /* 22231 * If we don't have a conn to apply 22232 * backpressure, free the message. 22233 * In the ire_send path, we don't know 22234 * the position to requeue the packet. Rather 22235 * than reorder packets, we just drop this 22236 * packet. 22237 */ 22238 if (ip_output_queue && connp != NULL && 22239 caller != IRE_SEND) { 22240 if (caller == IP_WSRV) { 22241 connp->conn_did_putbq = 1; 22242 (void) putbq(connp->conn_wq, 22243 first_mp); 22244 conn_drain_insert(connp); 22245 /* 22246 * This is the service thread, 22247 * and the queue is already 22248 * noenabled. The check for 22249 * canput and the putbq is not 22250 * atomic. So we need to check 22251 * again. 22252 */ 22253 if (canput(stq->q_next)) 22254 connp->conn_did_putbq 22255 = 0; 22256 IP_STAT(ip_conn_flputbq); 22257 } else { 22258 /* 22259 * We are not the service proc. 22260 * ip_wsrv will be scheduled or 22261 * is already running. 22262 */ 22263 (void) putq(connp->conn_wq, 22264 first_mp); 22265 } 22266 } else { 22267 BUMP_MIB(&ip_mib, ipOutDiscards); 22268 freemsg(first_mp); 22269 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22270 "ip_wput_ire_end: q %p (%S)", 22271 q, "discard"); 22272 } 22273 ire_refrele(ire); 22274 if (next_mp) { 22275 ire_refrele(ire1); 22276 freemsg(next_mp); 22277 } 22278 if (conn_outgoing_ill != NULL) 22279 ill_refrele(conn_outgoing_ill); 22280 return; 22281 } 22282 if ((PROTO == IPPROTO_UDP) && 22283 (ip_hdr_included != IP_HDR_INCLUDED)) { 22284 /* 22285 * hlen gets the number of uchar_ts in the 22286 * IP header 22287 */ 22288 hlen = (V_HLEN & 0xF) << 2; 22289 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22290 max_frag = ire->ire_max_frag; 22291 if (*up != 0) { 22292 IP_CKSUM_XMIT(ire_ill, ire, mp, ipha, 22293 up, PROTO, hlen, LENGTH, max_frag, 22294 ipsec_len, cksum); 22295 /* Software checksum? */ 22296 if (DB_CKSUMFLAGS(mp) == 0) { 22297 IP_STAT(ip_out_sw_cksum); 22298 IP_STAT_UPDATE( 22299 ip_udp_out_sw_cksum_bytes, 22300 LENGTH - hlen); 22301 } 22302 } 22303 } 22304 } 22305 /* 22306 * Need to do this even when fragmenting. The local 22307 * loopback can be done without computing checksums 22308 * but forwarding out other interface must be done 22309 * after the IP checksum (and ULP checksums) have been 22310 * computed. 22311 * 22312 * NOTE : multicast_forward is set only if this packet 22313 * originated from ip_wput. For packets originating from 22314 * ip_wput_multicast, it is not set. 22315 */ 22316 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 22317 multi_loopback: 22318 ip2dbg(("ip_wput: multicast, loop %d\n", 22319 conn_multicast_loop)); 22320 22321 /* Forget header checksum offload */ 22322 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 22323 22324 /* 22325 * Local loopback of multicasts? Check the 22326 * ill. 22327 * 22328 * Note that the loopback function will not come 22329 * in through ip_rput - it will only do the 22330 * client fanout thus we need to do an mforward 22331 * as well. The is different from the BSD 22332 * logic. 22333 */ 22334 if (ill != NULL) { 22335 ilm_t *ilm; 22336 22337 ILM_WALKER_HOLD(ill); 22338 ilm = ilm_lookup_ill(ill, ipha->ipha_dst, 22339 ALL_ZONES); 22340 ILM_WALKER_RELE(ill); 22341 if (ilm != NULL) { 22342 /* 22343 * Pass along the virtual output q. 22344 * ip_wput_local() will distribute the 22345 * packet to all the matching zones, 22346 * except the sending zone when 22347 * IP_MULTICAST_LOOP is false. 22348 */ 22349 ip_multicast_loopback(q, ill, first_mp, 22350 conn_multicast_loop ? 0 : 22351 IP_FF_NO_MCAST_LOOP, zoneid); 22352 } 22353 } 22354 if (ipha->ipha_ttl == 0) { 22355 /* 22356 * 0 => only to this host i.e. we are 22357 * done. We are also done if this was the 22358 * loopback interface since it is sufficient 22359 * to loopback one copy of a multicast packet. 22360 */ 22361 freemsg(first_mp); 22362 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22363 "ip_wput_ire_end: q %p (%S)", 22364 q, "loopback"); 22365 ire_refrele(ire); 22366 if (conn_outgoing_ill != NULL) 22367 ill_refrele(conn_outgoing_ill); 22368 return; 22369 } 22370 /* 22371 * ILLF_MULTICAST is checked in ip_newroute 22372 * i.e. we don't need to check it here since 22373 * all IRE_CACHEs come from ip_newroute. 22374 * For multicast traffic, SO_DONTROUTE is interpreted 22375 * to mean only send the packet out the interface 22376 * (optionally specified with IP_MULTICAST_IF) 22377 * and do not forward it out additional interfaces. 22378 * RSVP and the rsvp daemon is an example of a 22379 * protocol and user level process that 22380 * handles it's own routing. Hence, it uses the 22381 * SO_DONTROUTE option to accomplish this. 22382 */ 22383 22384 if (ip_g_mrouter && !conn_dontroute && ill != NULL) { 22385 /* Unconditionally redo the checksum */ 22386 ipha->ipha_hdr_checksum = 0; 22387 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 22388 22389 /* 22390 * If this needs to go out secure, we need 22391 * to wait till we finish the IPSEC 22392 * processing. 22393 */ 22394 if (ipsec_len == 0 && 22395 ip_mforward(ill, ipha, mp)) { 22396 freemsg(first_mp); 22397 ip1dbg(("ip_wput: mforward failed\n")); 22398 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22399 "ip_wput_ire_end: q %p (%S)", 22400 q, "mforward failed"); 22401 ire_refrele(ire); 22402 if (conn_outgoing_ill != NULL) 22403 ill_refrele(conn_outgoing_ill); 22404 return; 22405 } 22406 } 22407 } 22408 max_frag = ire->ire_max_frag; 22409 cksum += ttl_protocol; 22410 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 22411 /* No fragmentation required for this one. */ 22412 /* 22413 * Don't use frag_flag if packet is pre-built or source 22414 * routed or if multicast (since multicast packets do 22415 * not solicit ICMP "packet too big" messages). 22416 */ 22417 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22418 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22419 !ip_source_route_included(ipha)) && 22420 !CLASSD(ipha->ipha_dst)) 22421 ipha->ipha_fragment_offset_and_flags |= 22422 htons(ire->ire_frag_flag); 22423 22424 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22425 /* Complete the IP header checksum. */ 22426 cksum += ipha->ipha_ident; 22427 cksum += (v_hlen_tos_len >> 16)+ 22428 (v_hlen_tos_len & 0xFFFF); 22429 cksum += ipha->ipha_fragment_offset_and_flags; 22430 hlen = (V_HLEN & 0xF) - 22431 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22432 if (hlen) { 22433 checksumoptions: 22434 /* 22435 * Account for the IP Options in the IP 22436 * header checksum. 22437 */ 22438 up = (uint16_t *)(rptr+ 22439 IP_SIMPLE_HDR_LENGTH); 22440 do { 22441 cksum += up[0]; 22442 cksum += up[1]; 22443 up += 2; 22444 } while (--hlen); 22445 } 22446 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22447 cksum = ~(cksum + (cksum >> 16)); 22448 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22449 } 22450 if (ipsec_len != 0) { 22451 ipsec_out_process(q, first_mp, ire, ill_index); 22452 if (!next_mp) { 22453 ire_refrele(ire); 22454 if (conn_outgoing_ill != NULL) 22455 ill_refrele(conn_outgoing_ill); 22456 return; 22457 } 22458 goto next; 22459 } 22460 22461 /* 22462 * multirt_send has already been handled 22463 * for broadcast, but not yet for multicast 22464 * or IP options. 22465 */ 22466 if (next_mp == NULL) { 22467 if (ire->ire_flags & RTF_MULTIRT) { 22468 multirt_send = B_TRUE; 22469 } 22470 } 22471 22472 /* 22473 * In most cases, the emission loop below is 22474 * entered only once. Only in the case where 22475 * the ire holds the RTF_MULTIRT flag, do we loop 22476 * to process all RTF_MULTIRT ires in the bucket, 22477 * and send the packet through all crossed 22478 * RTF_MULTIRT routes. 22479 */ 22480 do { 22481 if (multirt_send) { 22482 irb_t *irb; 22483 22484 irb = ire->ire_bucket; 22485 ASSERT(irb != NULL); 22486 /* 22487 * We are in a multiple send case, 22488 * need to get the next IRE and make 22489 * a duplicate of the packet. 22490 */ 22491 IRB_REFHOLD(irb); 22492 for (ire1 = ire->ire_next; 22493 ire1 != NULL; 22494 ire1 = ire1->ire_next) { 22495 if (!(ire1->ire_flags & 22496 RTF_MULTIRT)) 22497 continue; 22498 if (ire1->ire_addr != 22499 ire->ire_addr) 22500 continue; 22501 if (ire1->ire_marks & 22502 (IRE_MARK_CONDEMNED| 22503 IRE_MARK_HIDDEN)) 22504 continue; 22505 22506 /* Got one */ 22507 IRE_REFHOLD(ire1); 22508 break; 22509 } 22510 IRB_REFRELE(irb); 22511 22512 if (ire1 != NULL) { 22513 next_mp = copyb(mp); 22514 if ((next_mp == NULL) || 22515 ((mp->b_cont != NULL) && 22516 ((next_mp->b_cont = 22517 dupmsg(mp->b_cont)) 22518 == NULL))) { 22519 freemsg(next_mp); 22520 next_mp = NULL; 22521 ire_refrele(ire1); 22522 ire1 = NULL; 22523 } 22524 } 22525 22526 /* 22527 * Last multiroute ire; don't loop 22528 * anymore. The emission is over 22529 * and next_mp is NULL. 22530 */ 22531 if (ire1 == NULL) { 22532 multirt_send = B_FALSE; 22533 } 22534 } 22535 22536 out_ill = ire->ire_ipif->ipif_ill; 22537 DTRACE_PROBE4(ip4__physical__out__start, 22538 ill_t *, NULL, 22539 ill_t *, out_ill, 22540 ipha_t *, ipha, mblk_t *, mp); 22541 FW_HOOKS(ip4_physical_out_event, 22542 ipv4firewall_physical_out, 22543 MSG_FWCOOKED_OUT, NULL, out_ill, 22544 ipha, mp, mp); 22545 DTRACE_PROBE1(ip4__physical__out__end, 22546 mblk_t *, mp); 22547 if (mp == NULL) 22548 goto release_ire_and_ill_2; 22549 22550 ASSERT(ipsec_len == 0); 22551 mp->b_prev = 22552 SET_BPREV_FLAG(IPP_LOCAL_OUT); 22553 DTRACE_PROBE2(ip__xmit__2, 22554 mblk_t *, mp, ire_t *, ire); 22555 pktxmit_state = ip_xmit_v4(mp, ire, 22556 NULL, B_TRUE); 22557 if ((pktxmit_state == SEND_FAILED) || 22558 (pktxmit_state == LLHDR_RESLV_FAILED)) { 22559 release_ire_and_ill_2: 22560 if (next_mp) { 22561 freemsg(next_mp); 22562 ire_refrele(ire1); 22563 } 22564 ire_refrele(ire); 22565 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22566 "ip_wput_ire_end: q %p (%S)", 22567 q, "discard MDATA"); 22568 if (conn_outgoing_ill != NULL) 22569 ill_refrele(conn_outgoing_ill); 22570 return; 22571 } 22572 22573 if (multirt_send) { 22574 /* 22575 * We are in a multiple send case, 22576 * need to re-enter the sending loop 22577 * using the next ire. 22578 */ 22579 ire_refrele(ire); 22580 ire = ire1; 22581 stq = ire->ire_stq; 22582 mp = next_mp; 22583 next_mp = NULL; 22584 ipha = (ipha_t *)mp->b_rptr; 22585 ill_index = Q_TO_INDEX(stq); 22586 } 22587 } while (multirt_send); 22588 22589 if (!next_mp) { 22590 /* 22591 * Last copy going out (the ultra-common 22592 * case). Note that we intentionally replicate 22593 * the putnext rather than calling it before 22594 * the next_mp check in hopes of a little 22595 * tail-call action out of the compiler. 22596 */ 22597 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22598 "ip_wput_ire_end: q %p (%S)", 22599 q, "last copy out(1)"); 22600 ire_refrele(ire); 22601 if (conn_outgoing_ill != NULL) 22602 ill_refrele(conn_outgoing_ill); 22603 return; 22604 } 22605 /* More copies going out below. */ 22606 } else { 22607 int offset; 22608 fragmentit: 22609 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 22610 /* 22611 * If this would generate a icmp_frag_needed message, 22612 * we need to handle it before we do the IPSEC 22613 * processing. Otherwise, we need to strip the IPSEC 22614 * headers before we send up the message to the ULPs 22615 * which becomes messy and difficult. 22616 */ 22617 if (ipsec_len != 0) { 22618 if ((max_frag < (unsigned int)(LENGTH + 22619 ipsec_len)) && (offset & IPH_DF)) { 22620 22621 BUMP_MIB(&ip_mib, ipFragFails); 22622 ipha->ipha_hdr_checksum = 0; 22623 ipha->ipha_hdr_checksum = 22624 (uint16_t)ip_csum_hdr(ipha); 22625 icmp_frag_needed(ire->ire_stq, first_mp, 22626 max_frag, zoneid); 22627 if (!next_mp) { 22628 ire_refrele(ire); 22629 if (conn_outgoing_ill != NULL) { 22630 ill_refrele( 22631 conn_outgoing_ill); 22632 } 22633 return; 22634 } 22635 } else { 22636 /* 22637 * This won't cause a icmp_frag_needed 22638 * message. to be gnerated. Send it on 22639 * the wire. Note that this could still 22640 * cause fragmentation and all we 22641 * do is the generation of the message 22642 * to the ULP if needed before IPSEC. 22643 */ 22644 if (!next_mp) { 22645 ipsec_out_process(q, first_mp, 22646 ire, ill_index); 22647 TRACE_2(TR_FAC_IP, 22648 TR_IP_WPUT_IRE_END, 22649 "ip_wput_ire_end: q %p " 22650 "(%S)", q, 22651 "last ipsec_out_process"); 22652 ire_refrele(ire); 22653 if (conn_outgoing_ill != NULL) { 22654 ill_refrele( 22655 conn_outgoing_ill); 22656 } 22657 return; 22658 } 22659 ipsec_out_process(q, first_mp, 22660 ire, ill_index); 22661 } 22662 } else { 22663 /* 22664 * Initiate IPPF processing. For 22665 * fragmentable packets we finish 22666 * all QOS packet processing before 22667 * calling: 22668 * ip_wput_ire_fragmentit->ip_wput_frag 22669 */ 22670 22671 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 22672 ip_process(IPP_LOCAL_OUT, &mp, 22673 ill_index); 22674 if (mp == NULL) { 22675 BUMP_MIB(&ip_mib, 22676 ipOutDiscards); 22677 if (next_mp != NULL) { 22678 freemsg(next_mp); 22679 ire_refrele(ire1); 22680 } 22681 ire_refrele(ire); 22682 TRACE_2(TR_FAC_IP, 22683 TR_IP_WPUT_IRE_END, 22684 "ip_wput_ire: q %p (%S)", 22685 q, "discard MDATA"); 22686 if (conn_outgoing_ill != NULL) { 22687 ill_refrele( 22688 conn_outgoing_ill); 22689 } 22690 return; 22691 } 22692 } 22693 if (!next_mp) { 22694 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22695 "ip_wput_ire_end: q %p (%S)", 22696 q, "last fragmentation"); 22697 ip_wput_ire_fragmentit(mp, ire, 22698 zoneid); 22699 ire_refrele(ire); 22700 if (conn_outgoing_ill != NULL) 22701 ill_refrele(conn_outgoing_ill); 22702 return; 22703 } 22704 ip_wput_ire_fragmentit(mp, ire, zoneid); 22705 } 22706 } 22707 } else { 22708 nullstq: 22709 /* A NULL stq means the destination address is local. */ 22710 UPDATE_OB_PKT_COUNT(ire); 22711 ire->ire_last_used_time = lbolt; 22712 ASSERT(ire->ire_ipif != NULL); 22713 if (!next_mp) { 22714 /* 22715 * Is there an "in" and "out" for traffic local 22716 * to a host (loopback)? The code in Solaris doesn't 22717 * explicitly draw a line in its code for in vs out, 22718 * so we've had to draw a line in the sand: ip_wput_ire 22719 * is considered to be the "output" side and 22720 * ip_wput_local to be the "input" side. 22721 */ 22722 out_ill = ire->ire_ipif->ipif_ill; 22723 22724 DTRACE_PROBE4(ip4__loopback__out__start, 22725 ill_t *, NULL, ill_t *, out_ill, 22726 ipha_t *, ipha, mblk_t *, first_mp); 22727 22728 FW_HOOKS(ip4_loopback_out_event, 22729 ipv4firewall_loopback_out, MSG_FWCOOKED_OUT, 22730 NULL, out_ill, ipha, first_mp, mp); 22731 22732 DTRACE_PROBE1(ip4__loopback__out_end, 22733 mblk_t *, first_mp); 22734 22735 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22736 "ip_wput_ire_end: q %p (%S)", 22737 q, "local address"); 22738 22739 if (first_mp != NULL) 22740 ip_wput_local(q, out_ill, ipha, 22741 first_mp, ire, 0, ire->ire_zoneid); 22742 ire_refrele(ire); 22743 if (conn_outgoing_ill != NULL) 22744 ill_refrele(conn_outgoing_ill); 22745 return; 22746 } 22747 22748 out_ill = ire->ire_ipif->ipif_ill; 22749 22750 DTRACE_PROBE4(ip4__loopback__out__start, 22751 ill_t *, NULL, ill_t *, out_ill, 22752 ipha_t *, ipha, mblk_t *, first_mp); 22753 22754 FW_HOOKS(ip4_loopback_out_event, ipv4firewall_loopback_out, 22755 MSG_FWCOOKED_OUT, NULL, out_ill, ipha, first_mp, mp); 22756 22757 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, first_mp); 22758 22759 if (first_mp != NULL) 22760 ip_wput_local(q, out_ill, ipha, 22761 first_mp, ire, 0, ire->ire_zoneid); 22762 } 22763 next: 22764 /* 22765 * More copies going out to additional interfaces. 22766 * ire1 has already been held. We don't need the 22767 * "ire" anymore. 22768 */ 22769 ire_refrele(ire); 22770 ire = ire1; 22771 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 22772 mp = next_mp; 22773 ASSERT(ire->ire_ipversion == IPV4_VERSION); 22774 ill = ire_to_ill(ire); 22775 first_mp = mp; 22776 if (ipsec_len != 0) { 22777 ASSERT(first_mp->b_datap->db_type == M_CTL); 22778 mp = mp->b_cont; 22779 } 22780 dst = ire->ire_addr; 22781 ipha = (ipha_t *)mp->b_rptr; 22782 /* 22783 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 22784 * Restore ipha_ident "no checksum" flag. 22785 */ 22786 src = orig_src; 22787 ipha->ipha_ident = ip_hdr_included; 22788 goto another; 22789 22790 #undef rptr 22791 #undef Q_TO_INDEX 22792 } 22793 22794 /* 22795 * Routine to allocate a message that is used to notify the ULP about MDT. 22796 * The caller may provide a pointer to the link-layer MDT capabilities, 22797 * or NULL if MDT is to be disabled on the stream. 22798 */ 22799 mblk_t * 22800 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 22801 { 22802 mblk_t *mp; 22803 ip_mdt_info_t *mdti; 22804 ill_mdt_capab_t *idst; 22805 22806 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 22807 DB_TYPE(mp) = M_CTL; 22808 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 22809 mdti = (ip_mdt_info_t *)mp->b_rptr; 22810 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 22811 idst = &(mdti->mdt_capab); 22812 22813 /* 22814 * If the caller provides us with the capability, copy 22815 * it over into our notification message; otherwise 22816 * we zero out the capability portion. 22817 */ 22818 if (isrc != NULL) 22819 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 22820 else 22821 bzero((caddr_t)idst, sizeof (*idst)); 22822 } 22823 return (mp); 22824 } 22825 22826 /* 22827 * Routine which determines whether MDT can be enabled on the destination 22828 * IRE and IPC combination, and if so, allocates and returns the MDT 22829 * notification mblk that may be used by ULP. We also check if we need to 22830 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 22831 * MDT usage in the past have been lifted. This gets called during IP 22832 * and ULP binding. 22833 */ 22834 mblk_t * 22835 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 22836 ill_mdt_capab_t *mdt_cap) 22837 { 22838 mblk_t *mp; 22839 boolean_t rc = B_FALSE; 22840 22841 ASSERT(dst_ire != NULL); 22842 ASSERT(connp != NULL); 22843 ASSERT(mdt_cap != NULL); 22844 22845 /* 22846 * Currently, we only support simple TCP/{IPv4,IPv6} with 22847 * Multidata, which is handled in tcp_multisend(). This 22848 * is the reason why we do all these checks here, to ensure 22849 * that we don't enable Multidata for the cases which we 22850 * can't handle at the moment. 22851 */ 22852 do { 22853 /* Only do TCP at the moment */ 22854 if (connp->conn_ulp != IPPROTO_TCP) 22855 break; 22856 22857 /* 22858 * IPSEC outbound policy present? Note that we get here 22859 * after calling ipsec_conn_cache_policy() where the global 22860 * policy checking is performed. conn_latch will be 22861 * non-NULL as long as there's a policy defined, 22862 * i.e. conn_out_enforce_policy may be NULL in such case 22863 * when the connection is non-secure, and hence we check 22864 * further if the latch refers to an outbound policy. 22865 */ 22866 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 22867 break; 22868 22869 /* CGTP (multiroute) is enabled? */ 22870 if (dst_ire->ire_flags & RTF_MULTIRT) 22871 break; 22872 22873 /* Outbound IPQoS enabled? */ 22874 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 22875 /* 22876 * In this case, we disable MDT for this and all 22877 * future connections going over the interface. 22878 */ 22879 mdt_cap->ill_mdt_on = 0; 22880 break; 22881 } 22882 22883 /* socket option(s) present? */ 22884 if (!CONN_IS_MD_FASTPATH(connp)) 22885 break; 22886 22887 rc = B_TRUE; 22888 /* CONSTCOND */ 22889 } while (0); 22890 22891 /* Remember the result */ 22892 connp->conn_mdt_ok = rc; 22893 22894 if (!rc) 22895 return (NULL); 22896 else if (!mdt_cap->ill_mdt_on) { 22897 /* 22898 * If MDT has been previously turned off in the past, and we 22899 * currently can do MDT (due to IPQoS policy removal, etc.) 22900 * then enable it for this interface. 22901 */ 22902 mdt_cap->ill_mdt_on = 1; 22903 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 22904 "interface %s\n", ill_name)); 22905 } 22906 22907 /* Allocate the MDT info mblk */ 22908 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 22909 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 22910 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 22911 return (NULL); 22912 } 22913 return (mp); 22914 } 22915 22916 /* 22917 * Create destination address attribute, and fill it with the physical 22918 * destination address and SAP taken from the template DL_UNITDATA_REQ 22919 * message block. 22920 */ 22921 boolean_t 22922 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 22923 { 22924 dl_unitdata_req_t *dlurp; 22925 pattr_t *pa; 22926 pattrinfo_t pa_info; 22927 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 22928 uint_t das_len, das_off; 22929 22930 ASSERT(dlmp != NULL); 22931 22932 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 22933 das_len = dlurp->dl_dest_addr_length; 22934 das_off = dlurp->dl_dest_addr_offset; 22935 22936 pa_info.type = PATTR_DSTADDRSAP; 22937 pa_info.len = sizeof (**das) + das_len - 1; 22938 22939 /* create and associate the attribute */ 22940 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 22941 if (pa != NULL) { 22942 ASSERT(*das != NULL); 22943 (*das)->addr_is_group = 0; 22944 (*das)->addr_len = (uint8_t)das_len; 22945 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 22946 } 22947 22948 return (pa != NULL); 22949 } 22950 22951 /* 22952 * Create hardware checksum attribute and fill it with the values passed. 22953 */ 22954 boolean_t 22955 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 22956 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 22957 { 22958 pattr_t *pa; 22959 pattrinfo_t pa_info; 22960 22961 ASSERT(mmd != NULL); 22962 22963 pa_info.type = PATTR_HCKSUM; 22964 pa_info.len = sizeof (pattr_hcksum_t); 22965 22966 /* create and associate the attribute */ 22967 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 22968 if (pa != NULL) { 22969 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 22970 22971 hck->hcksum_start_offset = start_offset; 22972 hck->hcksum_stuff_offset = stuff_offset; 22973 hck->hcksum_end_offset = end_offset; 22974 hck->hcksum_flags = flags; 22975 } 22976 return (pa != NULL); 22977 } 22978 22979 /* 22980 * Create zerocopy attribute and fill it with the specified flags 22981 */ 22982 boolean_t 22983 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 22984 { 22985 pattr_t *pa; 22986 pattrinfo_t pa_info; 22987 22988 ASSERT(mmd != NULL); 22989 pa_info.type = PATTR_ZCOPY; 22990 pa_info.len = sizeof (pattr_zcopy_t); 22991 22992 /* create and associate the attribute */ 22993 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 22994 if (pa != NULL) { 22995 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 22996 22997 zcopy->zcopy_flags = flags; 22998 } 22999 return (pa != NULL); 23000 } 23001 23002 /* 23003 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 23004 * block chain. We could rewrite to handle arbitrary message block chains but 23005 * that would make the code complicated and slow. Right now there three 23006 * restrictions: 23007 * 23008 * 1. The first message block must contain the complete IP header and 23009 * at least 1 byte of payload data. 23010 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 23011 * so that we can use a single Multidata message. 23012 * 3. No frag must be distributed over two or more message blocks so 23013 * that we don't need more than two packet descriptors per frag. 23014 * 23015 * The above restrictions allow us to support userland applications (which 23016 * will send down a single message block) and NFS over UDP (which will 23017 * send down a chain of at most three message blocks). 23018 * 23019 * We also don't use MDT for payloads with less than or equal to 23020 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 23021 */ 23022 boolean_t 23023 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 23024 { 23025 int blocks; 23026 ssize_t total, missing, size; 23027 23028 ASSERT(mp != NULL); 23029 ASSERT(hdr_len > 0); 23030 23031 size = MBLKL(mp) - hdr_len; 23032 if (size <= 0) 23033 return (B_FALSE); 23034 23035 /* The first mblk contains the header and some payload. */ 23036 blocks = 1; 23037 total = size; 23038 size %= len; 23039 missing = (size == 0) ? 0 : (len - size); 23040 mp = mp->b_cont; 23041 23042 while (mp != NULL) { 23043 /* 23044 * Give up if we encounter a zero length message block. 23045 * In practice, this should rarely happen and therefore 23046 * not worth the trouble of freeing and re-linking the 23047 * mblk from the chain to handle such case. 23048 */ 23049 if ((size = MBLKL(mp)) == 0) 23050 return (B_FALSE); 23051 23052 /* Too many payload buffers for a single Multidata message? */ 23053 if (++blocks > MULTIDATA_MAX_PBUFS) 23054 return (B_FALSE); 23055 23056 total += size; 23057 /* Is a frag distributed over two or more message blocks? */ 23058 if (missing > size) 23059 return (B_FALSE); 23060 size -= missing; 23061 23062 size %= len; 23063 missing = (size == 0) ? 0 : (len - size); 23064 23065 mp = mp->b_cont; 23066 } 23067 23068 return (total > ip_wput_frag_mdt_min); 23069 } 23070 23071 /* 23072 * Outbound IPv4 fragmentation routine using MDT. 23073 */ 23074 static void 23075 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 23076 uint32_t frag_flag, int offset) 23077 { 23078 ipha_t *ipha_orig; 23079 int i1, ip_data_end; 23080 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 23081 mblk_t *hdr_mp, *md_mp = NULL; 23082 unsigned char *hdr_ptr, *pld_ptr; 23083 multidata_t *mmd; 23084 ip_pdescinfo_t pdi; 23085 23086 ASSERT(DB_TYPE(mp) == M_DATA); 23087 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 23088 23089 ipha_orig = (ipha_t *)mp->b_rptr; 23090 mp->b_rptr += sizeof (ipha_t); 23091 23092 /* Calculate how many packets we will send out */ 23093 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 23094 pkts = (i1 + len - 1) / len; 23095 ASSERT(pkts > 1); 23096 23097 /* Allocate a message block which will hold all the IP Headers. */ 23098 wroff = ip_wroff_extra; 23099 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 23100 23101 i1 = pkts * hdr_chunk_len; 23102 /* 23103 * Create the header buffer, Multidata and destination address 23104 * and SAP attribute that should be associated with it. 23105 */ 23106 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 23107 ((hdr_mp->b_wptr += i1), 23108 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 23109 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 23110 freemsg(mp); 23111 if (md_mp == NULL) { 23112 freemsg(hdr_mp); 23113 } else { 23114 free_mmd: IP_STAT(ip_frag_mdt_discarded); 23115 freemsg(md_mp); 23116 } 23117 IP_STAT(ip_frag_mdt_allocfail); 23118 UPDATE_MIB(&ip_mib, ipOutDiscards, pkts); 23119 return; 23120 } 23121 IP_STAT(ip_frag_mdt_allocd); 23122 23123 /* 23124 * Add a payload buffer to the Multidata; this operation must not 23125 * fail, or otherwise our logic in this routine is broken. There 23126 * is no memory allocation done by the routine, so any returned 23127 * failure simply tells us that we've done something wrong. 23128 * 23129 * A failure tells us that either we're adding the same payload 23130 * buffer more than once, or we're trying to add more buffers than 23131 * allowed. None of the above cases should happen, and we panic 23132 * because either there's horrible heap corruption, and/or 23133 * programming mistake. 23134 */ 23135 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23136 goto pbuf_panic; 23137 23138 hdr_ptr = hdr_mp->b_rptr; 23139 pld_ptr = mp->b_rptr; 23140 23141 /* Establish the ending byte offset, based on the starting offset. */ 23142 offset <<= 3; 23143 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 23144 IP_SIMPLE_HDR_LENGTH; 23145 23146 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 23147 23148 while (pld_ptr < mp->b_wptr) { 23149 ipha_t *ipha; 23150 uint16_t offset_and_flags; 23151 uint16_t ip_len; 23152 int error; 23153 23154 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 23155 ipha = (ipha_t *)(hdr_ptr + wroff); 23156 ASSERT(OK_32PTR(ipha)); 23157 *ipha = *ipha_orig; 23158 23159 if (ip_data_end - offset > len) { 23160 offset_and_flags = IPH_MF; 23161 } else { 23162 /* 23163 * Last frag. Set len to the length of this last piece. 23164 */ 23165 len = ip_data_end - offset; 23166 /* A frag of a frag might have IPH_MF non-zero */ 23167 offset_and_flags = 23168 ntohs(ipha->ipha_fragment_offset_and_flags) & 23169 IPH_MF; 23170 } 23171 offset_and_flags |= (uint16_t)(offset >> 3); 23172 offset_and_flags |= (uint16_t)frag_flag; 23173 /* Store the offset and flags in the IP header. */ 23174 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 23175 23176 /* Store the length in the IP header. */ 23177 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 23178 ipha->ipha_length = htons(ip_len); 23179 23180 /* 23181 * Set the IP header checksum. Note that mp is just 23182 * the header, so this is easy to pass to ip_csum. 23183 */ 23184 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23185 23186 /* 23187 * Record offset and size of header and data of the next packet 23188 * in the multidata message. 23189 */ 23190 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 23191 PDESC_PLD_INIT(&pdi); 23192 i1 = MIN(mp->b_wptr - pld_ptr, len); 23193 ASSERT(i1 > 0); 23194 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 23195 if (i1 == len) { 23196 pld_ptr += len; 23197 } else { 23198 i1 = len - i1; 23199 mp = mp->b_cont; 23200 ASSERT(mp != NULL); 23201 ASSERT(MBLKL(mp) >= i1); 23202 /* 23203 * Attach the next payload message block to the 23204 * multidata message. 23205 */ 23206 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23207 goto pbuf_panic; 23208 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 23209 pld_ptr = mp->b_rptr + i1; 23210 } 23211 23212 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 23213 KM_NOSLEEP)) == NULL) { 23214 /* 23215 * Any failure other than ENOMEM indicates that we 23216 * have passed in invalid pdesc info or parameters 23217 * to mmd_addpdesc, which must not happen. 23218 * 23219 * EINVAL is a result of failure on boundary checks 23220 * against the pdesc info contents. It should not 23221 * happen, and we panic because either there's 23222 * horrible heap corruption, and/or programming 23223 * mistake. 23224 */ 23225 if (error != ENOMEM) { 23226 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 23227 "pdesc logic error detected for " 23228 "mmd %p pinfo %p (%d)\n", 23229 (void *)mmd, (void *)&pdi, error); 23230 /* NOTREACHED */ 23231 } 23232 IP_STAT(ip_frag_mdt_addpdescfail); 23233 /* Free unattached payload message blocks as well */ 23234 md_mp->b_cont = mp->b_cont; 23235 goto free_mmd; 23236 } 23237 23238 /* Advance fragment offset. */ 23239 offset += len; 23240 23241 /* Advance to location for next header in the buffer. */ 23242 hdr_ptr += hdr_chunk_len; 23243 23244 /* Did we reach the next payload message block? */ 23245 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 23246 mp = mp->b_cont; 23247 /* 23248 * Attach the next message block with payload 23249 * data to the multidata message. 23250 */ 23251 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23252 goto pbuf_panic; 23253 pld_ptr = mp->b_rptr; 23254 } 23255 } 23256 23257 ASSERT(hdr_mp->b_wptr == hdr_ptr); 23258 ASSERT(mp->b_wptr == pld_ptr); 23259 23260 /* Update IP statistics */ 23261 UPDATE_MIB(&ip_mib, ipFragCreates, pkts); 23262 BUMP_MIB(&ip_mib, ipFragOKs); 23263 IP_STAT_UPDATE(ip_frag_mdt_pkt_out, pkts); 23264 23265 if (pkt_type == OB_PKT) { 23266 ire->ire_ob_pkt_count += pkts; 23267 if (ire->ire_ipif != NULL) 23268 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 23269 } else { 23270 /* 23271 * The type is IB_PKT in the forwarding path and in 23272 * the mobile IP case when the packet is being reverse- 23273 * tunneled to the home agent. 23274 */ 23275 ire->ire_ib_pkt_count += pkts; 23276 ASSERT(!IRE_IS_LOCAL(ire)); 23277 if (ire->ire_type & IRE_BROADCAST) 23278 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 23279 else 23280 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 23281 } 23282 ire->ire_last_used_time = lbolt; 23283 /* Send it down */ 23284 putnext(ire->ire_stq, md_mp); 23285 return; 23286 23287 pbuf_panic: 23288 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 23289 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 23290 pbuf_idx); 23291 /* NOTREACHED */ 23292 } 23293 23294 /* 23295 * Outbound IP fragmentation routine. 23296 * 23297 * NOTE : This routine does not ire_refrele the ire that is passed in 23298 * as the argument. 23299 */ 23300 static void 23301 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 23302 uint32_t frag_flag, zoneid_t zoneid) 23303 { 23304 int i1; 23305 mblk_t *ll_hdr_mp; 23306 int ll_hdr_len; 23307 int hdr_len; 23308 mblk_t *hdr_mp; 23309 ipha_t *ipha; 23310 int ip_data_end; 23311 int len; 23312 mblk_t *mp = mp_orig, *mp1; 23313 int offset; 23314 queue_t *q; 23315 uint32_t v_hlen_tos_len; 23316 mblk_t *first_mp; 23317 boolean_t mctl_present; 23318 ill_t *ill; 23319 ill_t *out_ill; 23320 mblk_t *xmit_mp; 23321 mblk_t *carve_mp; 23322 ire_t *ire1 = NULL; 23323 ire_t *save_ire = NULL; 23324 mblk_t *next_mp = NULL; 23325 boolean_t last_frag = B_FALSE; 23326 boolean_t multirt_send = B_FALSE; 23327 ire_t *first_ire = NULL; 23328 irb_t *irb = NULL; 23329 23330 /* 23331 * IPSEC does not allow hw accelerated packets to be fragmented 23332 * This check is made in ip_wput_ipsec_out prior to coming here 23333 * via ip_wput_ire_fragmentit. 23334 * 23335 * If at this point we have an ire whose ARP request has not 23336 * been sent out, we call ip_xmit_v4->ire_arpresolve to trigger 23337 * sending of ARP query and change ire's state to ND_INCOMPLETE. 23338 * This packet and all fragmentable packets for this ire will 23339 * continue to get dropped while ire_nce->nce_state remains in 23340 * ND_INCOMPLETE. Post-ARP resolution, after ire's nce_state changes to 23341 * ND_REACHABLE, all subsquent large packets for this ire will 23342 * get fragemented and sent out by this function. 23343 */ 23344 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 23345 /* If nce_state is ND_INITIAL, trigger ARP query */ 23346 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 23347 ip1dbg(("ip_wput_frag: mac address for ire is unresolved" 23348 " - dropping packet\n")); 23349 BUMP_MIB(&ip_mib, ipFragFails); 23350 freemsg(mp); 23351 return; 23352 } 23353 23354 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 23355 "ip_wput_frag_start:"); 23356 23357 if (mp->b_datap->db_type == M_CTL) { 23358 first_mp = mp; 23359 mp_orig = mp = mp->b_cont; 23360 mctl_present = B_TRUE; 23361 } else { 23362 first_mp = mp; 23363 mctl_present = B_FALSE; 23364 } 23365 23366 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 23367 ipha = (ipha_t *)mp->b_rptr; 23368 23369 /* 23370 * If the Don't Fragment flag is on, generate an ICMP destination 23371 * unreachable, fragmentation needed. 23372 */ 23373 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23374 if (offset & IPH_DF) { 23375 BUMP_MIB(&ip_mib, ipFragFails); 23376 /* 23377 * Need to compute hdr checksum if called from ip_wput_ire. 23378 * Note that ip_rput_forward verifies the checksum before 23379 * calling this routine so in that case this is a noop. 23380 */ 23381 ipha->ipha_hdr_checksum = 0; 23382 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23383 icmp_frag_needed(ire->ire_stq, first_mp, max_frag, zoneid); 23384 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 23385 "ip_wput_frag_end:(%S)", 23386 "don't fragment"); 23387 return; 23388 } 23389 if (mctl_present) 23390 freeb(first_mp); 23391 /* 23392 * Establish the starting offset. May not be zero if we are fragging 23393 * a fragment that is being forwarded. 23394 */ 23395 offset = offset & IPH_OFFSET; 23396 23397 /* TODO why is this test needed? */ 23398 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 23399 if (((max_frag - LENGTH) & ~7) < 8) { 23400 /* TODO: notify ulp somehow */ 23401 BUMP_MIB(&ip_mib, ipFragFails); 23402 freemsg(mp); 23403 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 23404 "ip_wput_frag_end:(%S)", 23405 "len < 8"); 23406 return; 23407 } 23408 23409 hdr_len = (V_HLEN & 0xF) << 2; 23410 23411 ipha->ipha_hdr_checksum = 0; 23412 23413 /* 23414 * Establish the number of bytes maximum per frag, after putting 23415 * in the header. 23416 */ 23417 len = (max_frag - hdr_len) & ~7; 23418 23419 /* Check if we can use MDT to send out the frags. */ 23420 ASSERT(!IRE_IS_LOCAL(ire)); 23421 if (hdr_len == IP_SIMPLE_HDR_LENGTH && ip_multidata_outbound && 23422 !(ire->ire_flags & RTF_MULTIRT) && !IPP_ENABLED(IPP_LOCAL_OUT) && 23423 (ill = ire_to_ill(ire)) != NULL && ILL_MDT_CAPABLE(ill) && 23424 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 23425 ASSERT(ill->ill_mdt_capab != NULL); 23426 if (!ill->ill_mdt_capab->ill_mdt_on) { 23427 /* 23428 * If MDT has been previously turned off in the past, 23429 * and we currently can do MDT (due to IPQoS policy 23430 * removal, etc.) then enable it for this interface. 23431 */ 23432 ill->ill_mdt_capab->ill_mdt_on = 1; 23433 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 23434 ill->ill_name)); 23435 } 23436 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 23437 offset); 23438 return; 23439 } 23440 23441 /* Get a copy of the header for the trailing frags */ 23442 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset); 23443 if (!hdr_mp) { 23444 BUMP_MIB(&ip_mib, ipOutDiscards); 23445 freemsg(mp); 23446 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 23447 "ip_wput_frag_end:(%S)", 23448 "couldn't copy hdr"); 23449 return; 23450 } 23451 if (DB_CRED(mp) != NULL) 23452 mblk_setcred(hdr_mp, DB_CRED(mp)); 23453 23454 /* Store the starting offset, with the MoreFrags flag. */ 23455 i1 = offset | IPH_MF | frag_flag; 23456 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 23457 23458 /* Establish the ending byte offset, based on the starting offset. */ 23459 offset <<= 3; 23460 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 23461 23462 /* Store the length of the first fragment in the IP header. */ 23463 i1 = len + hdr_len; 23464 ASSERT(i1 <= IP_MAXPACKET); 23465 ipha->ipha_length = htons((uint16_t)i1); 23466 23467 /* 23468 * Compute the IP header checksum for the first frag. We have to 23469 * watch out that we stop at the end of the header. 23470 */ 23471 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23472 23473 /* 23474 * Now carve off the first frag. Note that this will include the 23475 * original IP header. 23476 */ 23477 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 23478 BUMP_MIB(&ip_mib, ipOutDiscards); 23479 freeb(hdr_mp); 23480 freemsg(mp_orig); 23481 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 23482 "ip_wput_frag_end:(%S)", 23483 "couldn't carve first"); 23484 return; 23485 } 23486 23487 /* 23488 * Multirouting case. Each fragment is replicated 23489 * via all non-condemned RTF_MULTIRT routes 23490 * currently resolved. 23491 * We ensure that first_ire is the first RTF_MULTIRT 23492 * ire in the bucket. 23493 */ 23494 if (ire->ire_flags & RTF_MULTIRT) { 23495 irb = ire->ire_bucket; 23496 ASSERT(irb != NULL); 23497 23498 multirt_send = B_TRUE; 23499 23500 /* Make sure we do not omit any multiroute ire. */ 23501 IRB_REFHOLD(irb); 23502 for (first_ire = irb->irb_ire; 23503 first_ire != NULL; 23504 first_ire = first_ire->ire_next) { 23505 if ((first_ire->ire_flags & RTF_MULTIRT) && 23506 (first_ire->ire_addr == ire->ire_addr) && 23507 !(first_ire->ire_marks & 23508 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 23509 break; 23510 } 23511 23512 if (first_ire != NULL) { 23513 if (first_ire != ire) { 23514 IRE_REFHOLD(first_ire); 23515 /* 23516 * Do not release the ire passed in 23517 * as the argument. 23518 */ 23519 ire = first_ire; 23520 } else { 23521 first_ire = NULL; 23522 } 23523 } 23524 IRB_REFRELE(irb); 23525 23526 /* 23527 * Save the first ire; we will need to restore it 23528 * for the trailing frags. 23529 * We REFHOLD save_ire, as each iterated ire will be 23530 * REFRELEd. 23531 */ 23532 save_ire = ire; 23533 IRE_REFHOLD(save_ire); 23534 } 23535 23536 /* 23537 * First fragment emission loop. 23538 * In most cases, the emission loop below is entered only 23539 * once. Only in the case where the ire holds the RTF_MULTIRT 23540 * flag, do we loop to process all RTF_MULTIRT ires in the 23541 * bucket, and send the fragment through all crossed 23542 * RTF_MULTIRT routes. 23543 */ 23544 do { 23545 if (ire->ire_flags & RTF_MULTIRT) { 23546 /* 23547 * We are in a multiple send case, need to get 23548 * the next ire and make a copy of the packet. 23549 * ire1 holds here the next ire to process in the 23550 * bucket. If multirouting is expected, 23551 * any non-RTF_MULTIRT ire that has the 23552 * right destination address is ignored. 23553 * 23554 * We have to take into account the MTU of 23555 * each walked ire. max_frag is set by the 23556 * the caller and generally refers to 23557 * the primary ire entry. Here we ensure that 23558 * no route with a lower MTU will be used, as 23559 * fragments are carved once for all ires, 23560 * then replicated. 23561 */ 23562 ASSERT(irb != NULL); 23563 IRB_REFHOLD(irb); 23564 for (ire1 = ire->ire_next; 23565 ire1 != NULL; 23566 ire1 = ire1->ire_next) { 23567 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 23568 continue; 23569 if (ire1->ire_addr != ire->ire_addr) 23570 continue; 23571 if (ire1->ire_marks & 23572 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 23573 continue; 23574 /* 23575 * Ensure we do not exceed the MTU 23576 * of the next route. 23577 */ 23578 if (ire1->ire_max_frag < max_frag) { 23579 ip_multirt_bad_mtu(ire1, max_frag); 23580 continue; 23581 } 23582 23583 /* Got one. */ 23584 IRE_REFHOLD(ire1); 23585 break; 23586 } 23587 IRB_REFRELE(irb); 23588 23589 if (ire1 != NULL) { 23590 next_mp = copyb(mp); 23591 if ((next_mp == NULL) || 23592 ((mp->b_cont != NULL) && 23593 ((next_mp->b_cont = 23594 dupmsg(mp->b_cont)) == NULL))) { 23595 freemsg(next_mp); 23596 next_mp = NULL; 23597 ire_refrele(ire1); 23598 ire1 = NULL; 23599 } 23600 } 23601 23602 /* Last multiroute ire; don't loop anymore. */ 23603 if (ire1 == NULL) { 23604 multirt_send = B_FALSE; 23605 } 23606 } 23607 23608 ll_hdr_len = 0; 23609 LOCK_IRE_FP_MP(ire); 23610 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 23611 if (ll_hdr_mp != NULL) { 23612 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 23613 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 23614 } else { 23615 ll_hdr_mp = ire->ire_nce->nce_res_mp; 23616 } 23617 23618 /* If there is a transmit header, get a copy for this frag. */ 23619 /* 23620 * TODO: should check db_ref before calling ip_carve_mp since 23621 * it might give us a dup. 23622 */ 23623 if (!ll_hdr_mp) { 23624 /* No xmit header. */ 23625 xmit_mp = mp; 23626 23627 /* We have a link-layer header that can fit in our mblk. */ 23628 } else if (mp->b_datap->db_ref == 1 && 23629 ll_hdr_len != 0 && 23630 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 23631 /* M_DATA fastpath */ 23632 mp->b_rptr -= ll_hdr_len; 23633 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 23634 xmit_mp = mp; 23635 23636 /* Corner case if copyb has failed */ 23637 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 23638 UNLOCK_IRE_FP_MP(ire); 23639 BUMP_MIB(&ip_mib, ipOutDiscards); 23640 freeb(hdr_mp); 23641 freemsg(mp); 23642 freemsg(mp_orig); 23643 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 23644 "ip_wput_frag_end:(%S)", 23645 "discard"); 23646 23647 if (multirt_send) { 23648 ASSERT(ire1); 23649 ASSERT(next_mp); 23650 23651 freemsg(next_mp); 23652 ire_refrele(ire1); 23653 } 23654 if (save_ire != NULL) 23655 IRE_REFRELE(save_ire); 23656 23657 if (first_ire != NULL) 23658 ire_refrele(first_ire); 23659 return; 23660 23661 /* 23662 * Case of res_mp OR the fastpath mp can't fit 23663 * in the mblk 23664 */ 23665 } else { 23666 xmit_mp->b_cont = mp; 23667 if (DB_CRED(mp) != NULL) 23668 mblk_setcred(xmit_mp, DB_CRED(mp)); 23669 /* 23670 * Get priority marking, if any. 23671 * We propagate the CoS marking from the 23672 * original packet that went to QoS processing 23673 * in ip_wput_ire to the newly carved mp. 23674 */ 23675 if (DB_TYPE(xmit_mp) == M_DATA) 23676 xmit_mp->b_band = mp->b_band; 23677 } 23678 UNLOCK_IRE_FP_MP(ire); 23679 q = ire->ire_stq; 23680 BUMP_MIB(&ip_mib, ipFragCreates); 23681 23682 out_ill = (ill_t *)q->q_ptr; 23683 23684 DTRACE_PROBE4(ip4__physical__out__start, 23685 ill_t *, NULL, ill_t *, out_ill, 23686 ipha_t *, ipha, mblk_t *, xmit_mp); 23687 23688 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 23689 MSG_FWCOOKED_OUT, NULL, out_ill, ipha, xmit_mp, mp); 23690 23691 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, xmit_mp); 23692 23693 if (xmit_mp != NULL) { 23694 putnext(q, xmit_mp); 23695 if (pkt_type != OB_PKT) { 23696 /* 23697 * Update the packet count of trailing 23698 * RTF_MULTIRT ires. 23699 */ 23700 UPDATE_OB_PKT_COUNT(ire); 23701 } 23702 } 23703 23704 if (multirt_send) { 23705 /* 23706 * We are in a multiple send case; look for 23707 * the next ire and re-enter the loop. 23708 */ 23709 ASSERT(ire1); 23710 ASSERT(next_mp); 23711 /* REFRELE the current ire before looping */ 23712 ire_refrele(ire); 23713 ire = ire1; 23714 ire1 = NULL; 23715 mp = next_mp; 23716 next_mp = NULL; 23717 } 23718 } while (multirt_send); 23719 23720 ASSERT(ire1 == NULL); 23721 23722 /* Restore the original ire; we need it for the trailing frags */ 23723 if (save_ire != NULL) { 23724 /* REFRELE the last iterated ire */ 23725 ire_refrele(ire); 23726 /* save_ire has been REFHOLDed */ 23727 ire = save_ire; 23728 save_ire = NULL; 23729 q = ire->ire_stq; 23730 } 23731 23732 if (pkt_type == OB_PKT) { 23733 UPDATE_OB_PKT_COUNT(ire); 23734 } else { 23735 UPDATE_IB_PKT_COUNT(ire); 23736 } 23737 23738 /* Advance the offset to the second frag starting point. */ 23739 offset += len; 23740 /* 23741 * Update hdr_len from the copied header - there might be less options 23742 * in the later fragments. 23743 */ 23744 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 23745 /* Loop until done. */ 23746 for (;;) { 23747 uint16_t offset_and_flags; 23748 uint16_t ip_len; 23749 23750 if (ip_data_end - offset > len) { 23751 /* 23752 * Carve off the appropriate amount from the original 23753 * datagram. 23754 */ 23755 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 23756 mp = NULL; 23757 break; 23758 } 23759 /* 23760 * More frags after this one. Get another copy 23761 * of the header. 23762 */ 23763 if (carve_mp->b_datap->db_ref == 1 && 23764 hdr_mp->b_wptr - hdr_mp->b_rptr < 23765 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 23766 /* Inline IP header */ 23767 carve_mp->b_rptr -= hdr_mp->b_wptr - 23768 hdr_mp->b_rptr; 23769 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 23770 hdr_mp->b_wptr - hdr_mp->b_rptr); 23771 mp = carve_mp; 23772 } else { 23773 if (!(mp = copyb(hdr_mp))) { 23774 freemsg(carve_mp); 23775 break; 23776 } 23777 /* Get priority marking, if any. */ 23778 mp->b_band = carve_mp->b_band; 23779 mp->b_cont = carve_mp; 23780 } 23781 ipha = (ipha_t *)mp->b_rptr; 23782 offset_and_flags = IPH_MF; 23783 } else { 23784 /* 23785 * Last frag. Consume the header. Set len to 23786 * the length of this last piece. 23787 */ 23788 len = ip_data_end - offset; 23789 23790 /* 23791 * Carve off the appropriate amount from the original 23792 * datagram. 23793 */ 23794 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 23795 mp = NULL; 23796 break; 23797 } 23798 if (carve_mp->b_datap->db_ref == 1 && 23799 hdr_mp->b_wptr - hdr_mp->b_rptr < 23800 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 23801 /* Inline IP header */ 23802 carve_mp->b_rptr -= hdr_mp->b_wptr - 23803 hdr_mp->b_rptr; 23804 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 23805 hdr_mp->b_wptr - hdr_mp->b_rptr); 23806 mp = carve_mp; 23807 freeb(hdr_mp); 23808 hdr_mp = mp; 23809 } else { 23810 mp = hdr_mp; 23811 /* Get priority marking, if any. */ 23812 mp->b_band = carve_mp->b_band; 23813 mp->b_cont = carve_mp; 23814 } 23815 ipha = (ipha_t *)mp->b_rptr; 23816 /* A frag of a frag might have IPH_MF non-zero */ 23817 offset_and_flags = 23818 ntohs(ipha->ipha_fragment_offset_and_flags) & 23819 IPH_MF; 23820 } 23821 offset_and_flags |= (uint16_t)(offset >> 3); 23822 offset_and_flags |= (uint16_t)frag_flag; 23823 /* Store the offset and flags in the IP header. */ 23824 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 23825 23826 /* Store the length in the IP header. */ 23827 ip_len = (uint16_t)(len + hdr_len); 23828 ipha->ipha_length = htons(ip_len); 23829 23830 /* 23831 * Set the IP header checksum. Note that mp is just 23832 * the header, so this is easy to pass to ip_csum. 23833 */ 23834 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23835 23836 /* Attach a transmit header, if any, and ship it. */ 23837 if (pkt_type == OB_PKT) { 23838 UPDATE_OB_PKT_COUNT(ire); 23839 } else { 23840 UPDATE_IB_PKT_COUNT(ire); 23841 } 23842 23843 if (ire->ire_flags & RTF_MULTIRT) { 23844 irb = ire->ire_bucket; 23845 ASSERT(irb != NULL); 23846 23847 multirt_send = B_TRUE; 23848 23849 /* 23850 * Save the original ire; we will need to restore it 23851 * for the tailing frags. 23852 */ 23853 save_ire = ire; 23854 IRE_REFHOLD(save_ire); 23855 } 23856 /* 23857 * Emission loop for this fragment, similar 23858 * to what is done for the first fragment. 23859 */ 23860 do { 23861 if (multirt_send) { 23862 /* 23863 * We are in a multiple send case, need to get 23864 * the next ire and make a copy of the packet. 23865 */ 23866 ASSERT(irb != NULL); 23867 IRB_REFHOLD(irb); 23868 for (ire1 = ire->ire_next; 23869 ire1 != NULL; 23870 ire1 = ire1->ire_next) { 23871 if (!(ire1->ire_flags & RTF_MULTIRT)) 23872 continue; 23873 if (ire1->ire_addr != ire->ire_addr) 23874 continue; 23875 if (ire1->ire_marks & 23876 (IRE_MARK_CONDEMNED| 23877 IRE_MARK_HIDDEN)) 23878 continue; 23879 /* 23880 * Ensure we do not exceed the MTU 23881 * of the next route. 23882 */ 23883 if (ire1->ire_max_frag < max_frag) { 23884 ip_multirt_bad_mtu(ire1, 23885 max_frag); 23886 continue; 23887 } 23888 23889 /* Got one. */ 23890 IRE_REFHOLD(ire1); 23891 break; 23892 } 23893 IRB_REFRELE(irb); 23894 23895 if (ire1 != NULL) { 23896 next_mp = copyb(mp); 23897 if ((next_mp == NULL) || 23898 ((mp->b_cont != NULL) && 23899 ((next_mp->b_cont = 23900 dupmsg(mp->b_cont)) == NULL))) { 23901 freemsg(next_mp); 23902 next_mp = NULL; 23903 ire_refrele(ire1); 23904 ire1 = NULL; 23905 } 23906 } 23907 23908 /* Last multiroute ire; don't loop anymore. */ 23909 if (ire1 == NULL) { 23910 multirt_send = B_FALSE; 23911 } 23912 } 23913 23914 /* Update transmit header */ 23915 ll_hdr_len = 0; 23916 LOCK_IRE_FP_MP(ire); 23917 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 23918 if (ll_hdr_mp != NULL) { 23919 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 23920 ll_hdr_len = MBLKL(ll_hdr_mp); 23921 } else { 23922 ll_hdr_mp = ire->ire_nce->nce_res_mp; 23923 } 23924 23925 if (!ll_hdr_mp) { 23926 xmit_mp = mp; 23927 23928 /* 23929 * We have link-layer header that can fit in 23930 * our mblk. 23931 */ 23932 } else if (mp->b_datap->db_ref == 1 && 23933 ll_hdr_len != 0 && 23934 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 23935 /* M_DATA fastpath */ 23936 mp->b_rptr -= ll_hdr_len; 23937 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 23938 ll_hdr_len); 23939 xmit_mp = mp; 23940 23941 /* 23942 * Case of res_mp OR the fastpath mp can't fit 23943 * in the mblk 23944 */ 23945 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 23946 xmit_mp->b_cont = mp; 23947 if (DB_CRED(mp) != NULL) 23948 mblk_setcred(xmit_mp, DB_CRED(mp)); 23949 /* Get priority marking, if any. */ 23950 if (DB_TYPE(xmit_mp) == M_DATA) 23951 xmit_mp->b_band = mp->b_band; 23952 23953 /* Corner case if copyb failed */ 23954 } else { 23955 /* 23956 * Exit both the replication and 23957 * fragmentation loops. 23958 */ 23959 UNLOCK_IRE_FP_MP(ire); 23960 goto drop_pkt; 23961 } 23962 UNLOCK_IRE_FP_MP(ire); 23963 BUMP_MIB(&ip_mib, ipFragCreates); 23964 23965 mp1 = mp; 23966 out_ill = (ill_t *)q->q_ptr; 23967 23968 DTRACE_PROBE4(ip4__physical__out__start, 23969 ill_t *, NULL, ill_t *, out_ill, 23970 ipha_t *, ipha, mblk_t *, xmit_mp); 23971 23972 FW_HOOKS(ip4_physical_out_event, 23973 ipv4firewall_physical_out, MSG_FWCOOKED_OUT, 23974 NULL, out_ill, ipha, xmit_mp, mp); 23975 23976 DTRACE_PROBE1(ip4__physical__out__end, 23977 mblk_t *, xmit_mp); 23978 23979 if (mp != mp1 && hdr_mp == mp1) 23980 hdr_mp = mp; 23981 if (mp != mp1 && mp_orig == mp1) 23982 mp_orig = mp; 23983 23984 if (xmit_mp != NULL) { 23985 putnext(q, xmit_mp); 23986 23987 if (pkt_type != OB_PKT) { 23988 /* 23989 * Update the packet count of trailing 23990 * RTF_MULTIRT ires. 23991 */ 23992 UPDATE_OB_PKT_COUNT(ire); 23993 } 23994 } 23995 23996 /* All done if we just consumed the hdr_mp. */ 23997 if (mp == hdr_mp) { 23998 last_frag = B_TRUE; 23999 } 24000 24001 if (multirt_send) { 24002 /* 24003 * We are in a multiple send case; look for 24004 * the next ire and re-enter the loop. 24005 */ 24006 ASSERT(ire1); 24007 ASSERT(next_mp); 24008 /* REFRELE the current ire before looping */ 24009 ire_refrele(ire); 24010 ire = ire1; 24011 ire1 = NULL; 24012 q = ire->ire_stq; 24013 mp = next_mp; 24014 next_mp = NULL; 24015 } 24016 } while (multirt_send); 24017 /* 24018 * Restore the original ire; we need it for the 24019 * trailing frags 24020 */ 24021 if (save_ire != NULL) { 24022 ASSERT(ire1 == NULL); 24023 /* REFRELE the last iterated ire */ 24024 ire_refrele(ire); 24025 /* save_ire has been REFHOLDed */ 24026 ire = save_ire; 24027 q = ire->ire_stq; 24028 save_ire = NULL; 24029 } 24030 24031 if (last_frag) { 24032 BUMP_MIB(&ip_mib, ipFragOKs); 24033 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24034 "ip_wput_frag_end:(%S)", 24035 "consumed hdr_mp"); 24036 24037 if (first_ire != NULL) 24038 ire_refrele(first_ire); 24039 return; 24040 } 24041 /* Otherwise, advance and loop. */ 24042 offset += len; 24043 } 24044 24045 drop_pkt: 24046 /* Clean up following allocation failure. */ 24047 BUMP_MIB(&ip_mib, ipOutDiscards); 24048 freemsg(mp); 24049 if (mp != hdr_mp) 24050 freeb(hdr_mp); 24051 if (mp != mp_orig) 24052 freemsg(mp_orig); 24053 24054 if (save_ire != NULL) 24055 IRE_REFRELE(save_ire); 24056 if (first_ire != NULL) 24057 ire_refrele(first_ire); 24058 24059 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24060 "ip_wput_frag_end:(%S)", 24061 "end--alloc failure"); 24062 } 24063 24064 /* 24065 * Copy the header plus those options which have the copy bit set 24066 */ 24067 static mblk_t * 24068 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset) 24069 { 24070 mblk_t *mp; 24071 uchar_t *up; 24072 24073 /* 24074 * Quick check if we need to look for options without the copy bit 24075 * set 24076 */ 24077 mp = allocb(ip_wroff_extra + hdr_len, BPRI_HI); 24078 if (!mp) 24079 return (mp); 24080 mp->b_rptr += ip_wroff_extra; 24081 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 24082 bcopy(rptr, mp->b_rptr, hdr_len); 24083 mp->b_wptr += hdr_len + ip_wroff_extra; 24084 return (mp); 24085 } 24086 up = mp->b_rptr; 24087 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 24088 up += IP_SIMPLE_HDR_LENGTH; 24089 rptr += IP_SIMPLE_HDR_LENGTH; 24090 hdr_len -= IP_SIMPLE_HDR_LENGTH; 24091 while (hdr_len > 0) { 24092 uint32_t optval; 24093 uint32_t optlen; 24094 24095 optval = *rptr; 24096 if (optval == IPOPT_EOL) 24097 break; 24098 if (optval == IPOPT_NOP) 24099 optlen = 1; 24100 else 24101 optlen = rptr[1]; 24102 if (optval & IPOPT_COPY) { 24103 bcopy(rptr, up, optlen); 24104 up += optlen; 24105 } 24106 rptr += optlen; 24107 hdr_len -= optlen; 24108 } 24109 /* 24110 * Make sure that we drop an even number of words by filling 24111 * with EOL to the next word boundary. 24112 */ 24113 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 24114 hdr_len & 0x3; hdr_len++) 24115 *up++ = IPOPT_EOL; 24116 mp->b_wptr = up; 24117 /* Update header length */ 24118 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 24119 return (mp); 24120 } 24121 24122 /* 24123 * Delivery to local recipients including fanout to multiple recipients. 24124 * Does not do checksumming of UDP/TCP. 24125 * Note: q should be the read side queue for either the ill or conn. 24126 * Note: rq should be the read side q for the lower (ill) stream. 24127 * We don't send packets to IPPF processing, thus the last argument 24128 * to all the fanout calls are B_FALSE. 24129 */ 24130 void 24131 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 24132 int fanout_flags, zoneid_t zoneid) 24133 { 24134 uint32_t protocol; 24135 mblk_t *first_mp; 24136 boolean_t mctl_present; 24137 int ire_type; 24138 #define rptr ((uchar_t *)ipha) 24139 24140 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 24141 "ip_wput_local_start: q %p", q); 24142 24143 if (ire != NULL) { 24144 ire_type = ire->ire_type; 24145 } else { 24146 /* 24147 * Only ip_multicast_loopback() calls us with a NULL ire. If the 24148 * packet is not multicast, we can't tell the ire type. 24149 */ 24150 ASSERT(CLASSD(ipha->ipha_dst)); 24151 ire_type = IRE_BROADCAST; 24152 } 24153 24154 first_mp = mp; 24155 if (first_mp->b_datap->db_type == M_CTL) { 24156 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 24157 if (!io->ipsec_out_secure) { 24158 /* 24159 * This ipsec_out_t was allocated in ip_wput 24160 * for multicast packets to store the ill_index. 24161 * As this is being delivered locally, we don't 24162 * need this anymore. 24163 */ 24164 mp = first_mp->b_cont; 24165 freeb(first_mp); 24166 first_mp = mp; 24167 mctl_present = B_FALSE; 24168 } else { 24169 mctl_present = B_TRUE; 24170 mp = first_mp->b_cont; 24171 ASSERT(mp != NULL); 24172 ipsec_out_to_in(first_mp); 24173 } 24174 } else { 24175 mctl_present = B_FALSE; 24176 } 24177 24178 DTRACE_PROBE4(ip4__loopback__in__start, 24179 ill_t *, ill, ill_t *, NULL, 24180 ipha_t *, ipha, mblk_t *, first_mp); 24181 24182 FW_HOOKS(ip4_loopback_in_event, ipv4firewall_loopback_in, 24183 MSG_FWCOOKED_IN, ill, NULL, ipha, first_mp, mp); 24184 24185 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, first_mp); 24186 24187 if (first_mp == NULL) 24188 return; 24189 24190 loopback_packets++; 24191 24192 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 24193 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 24194 if (!IS_SIMPLE_IPH(ipha)) { 24195 ip_wput_local_options(ipha); 24196 } 24197 24198 protocol = ipha->ipha_protocol; 24199 switch (protocol) { 24200 case IPPROTO_ICMP: { 24201 ire_t *ire_zone; 24202 ilm_t *ilm; 24203 mblk_t *mp1; 24204 zoneid_t last_zoneid; 24205 24206 if (CLASSD(ipha->ipha_dst) && 24207 !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 24208 ASSERT(ire_type == IRE_BROADCAST); 24209 /* 24210 * In the multicast case, applications may have joined 24211 * the group from different zones, so we need to deliver 24212 * the packet to each of them. Loop through the 24213 * multicast memberships structures (ilm) on the receive 24214 * ill and send a copy of the packet up each matching 24215 * one. However, we don't do this for multicasts sent on 24216 * the loopback interface (PHYI_LOOPBACK flag set) as 24217 * they must stay in the sender's zone. 24218 * 24219 * ilm_add_v6() ensures that ilms in the same zone are 24220 * contiguous in the ill_ilm list. We use this property 24221 * to avoid sending duplicates needed when two 24222 * applications in the same zone join the same group on 24223 * different logical interfaces: we ignore the ilm if 24224 * it's zoneid is the same as the last matching one. 24225 * In addition, the sending of the packet for 24226 * ire_zoneid is delayed until all of the other ilms 24227 * have been exhausted. 24228 */ 24229 last_zoneid = -1; 24230 ILM_WALKER_HOLD(ill); 24231 for (ilm = ill->ill_ilm; ilm != NULL; 24232 ilm = ilm->ilm_next) { 24233 if ((ilm->ilm_flags & ILM_DELETED) || 24234 ipha->ipha_dst != ilm->ilm_addr || 24235 ilm->ilm_zoneid == last_zoneid || 24236 ilm->ilm_zoneid == zoneid || 24237 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 24238 continue; 24239 mp1 = ip_copymsg(first_mp); 24240 if (mp1 == NULL) 24241 continue; 24242 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 24243 mctl_present, B_FALSE, ill, 24244 ilm->ilm_zoneid); 24245 last_zoneid = ilm->ilm_zoneid; 24246 } 24247 ILM_WALKER_RELE(ill); 24248 /* 24249 * Loopback case: the sending endpoint has 24250 * IP_MULTICAST_LOOP disabled, therefore we don't 24251 * dispatch the multicast packet to the sending zone. 24252 */ 24253 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 24254 freemsg(first_mp); 24255 return; 24256 } 24257 } else if (ire_type == IRE_BROADCAST) { 24258 /* 24259 * In the broadcast case, there may be many zones 24260 * which need a copy of the packet delivered to them. 24261 * There is one IRE_BROADCAST per broadcast address 24262 * and per zone; we walk those using a helper function. 24263 * In addition, the sending of the packet for zoneid is 24264 * delayed until all of the other ires have been 24265 * processed. 24266 */ 24267 IRB_REFHOLD(ire->ire_bucket); 24268 ire_zone = NULL; 24269 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 24270 ire)) != NULL) { 24271 mp1 = ip_copymsg(first_mp); 24272 if (mp1 == NULL) 24273 continue; 24274 24275 UPDATE_IB_PKT_COUNT(ire_zone); 24276 ire_zone->ire_last_used_time = lbolt; 24277 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 24278 mctl_present, B_FALSE, ill, 24279 ire_zone->ire_zoneid); 24280 } 24281 IRB_REFRELE(ire->ire_bucket); 24282 } 24283 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 24284 0, mctl_present, B_FALSE, ill, zoneid); 24285 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24286 "ip_wput_local_end: q %p (%S)", 24287 q, "icmp"); 24288 return; 24289 } 24290 case IPPROTO_IGMP: 24291 if ((mp = igmp_input(q, mp, ill)) == NULL) { 24292 /* Bad packet - discarded by igmp_input */ 24293 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24294 "ip_wput_local_end: q %p (%S)", 24295 q, "igmp_input--bad packet"); 24296 if (mctl_present) 24297 freeb(first_mp); 24298 return; 24299 } 24300 /* 24301 * igmp_input() may have returned the pulled up message. 24302 * So first_mp and ipha need to be reinitialized. 24303 */ 24304 ipha = (ipha_t *)mp->b_rptr; 24305 if (mctl_present) 24306 first_mp->b_cont = mp; 24307 else 24308 first_mp = mp; 24309 /* deliver to local raw users */ 24310 break; 24311 case IPPROTO_ENCAP: 24312 /* 24313 * This case is covered by either ip_fanout_proto, or by 24314 * the above security processing for self-tunneled packets. 24315 */ 24316 break; 24317 case IPPROTO_UDP: { 24318 uint16_t *up; 24319 uint32_t ports; 24320 24321 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 24322 UDP_PORTS_OFFSET); 24323 /* Force a 'valid' checksum. */ 24324 up[3] = 0; 24325 24326 ports = *(uint32_t *)up; 24327 ip_fanout_udp(q, first_mp, ill, ipha, ports, 24328 (ire_type == IRE_BROADCAST), 24329 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 24330 IP_FF_SEND_SLLA | IP_FF_IP6INFO, mctl_present, B_FALSE, 24331 ill, zoneid); 24332 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24333 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 24334 return; 24335 } 24336 case IPPROTO_TCP: { 24337 24338 /* 24339 * For TCP, discard broadcast packets. 24340 */ 24341 if ((ushort_t)ire_type == IRE_BROADCAST) { 24342 freemsg(first_mp); 24343 BUMP_MIB(&ip_mib, ipInDiscards); 24344 ip2dbg(("ip_wput_local: discard broadcast\n")); 24345 return; 24346 } 24347 24348 if (mp->b_datap->db_type == M_DATA) { 24349 /* 24350 * M_DATA mblk, so init mblk (chain) for no struio(). 24351 */ 24352 mblk_t *mp1 = mp; 24353 24354 do 24355 mp1->b_datap->db_struioflag = 0; 24356 while ((mp1 = mp1->b_cont) != NULL); 24357 } 24358 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 24359 <= mp->b_wptr); 24360 ip_fanout_tcp(q, first_mp, ill, ipha, 24361 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 24362 IP_FF_SYN_ADDIRE | IP_FF_IP6INFO, 24363 mctl_present, B_FALSE, zoneid); 24364 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24365 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 24366 return; 24367 } 24368 case IPPROTO_SCTP: 24369 { 24370 uint32_t ports; 24371 24372 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 24373 ip_fanout_sctp(first_mp, ill, ipha, ports, 24374 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 24375 IP_FF_IP6INFO, 24376 mctl_present, B_FALSE, 0, zoneid); 24377 return; 24378 } 24379 24380 default: 24381 break; 24382 } 24383 /* 24384 * Find a client for some other protocol. We give 24385 * copies to multiple clients, if more than one is 24386 * bound. 24387 */ 24388 ip_fanout_proto(q, first_mp, ill, ipha, 24389 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 24390 mctl_present, B_FALSE, ill, zoneid); 24391 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24392 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 24393 #undef rptr 24394 } 24395 24396 /* 24397 * Update any source route, record route, or timestamp options. 24398 * Check that we are at end of strict source route. 24399 * The options have been sanity checked by ip_wput_options(). 24400 */ 24401 static void 24402 ip_wput_local_options(ipha_t *ipha) 24403 { 24404 ipoptp_t opts; 24405 uchar_t *opt; 24406 uint8_t optval; 24407 uint8_t optlen; 24408 ipaddr_t dst; 24409 uint32_t ts; 24410 ire_t *ire; 24411 timestruc_t now; 24412 24413 ip2dbg(("ip_wput_local_options\n")); 24414 for (optval = ipoptp_first(&opts, ipha); 24415 optval != IPOPT_EOL; 24416 optval = ipoptp_next(&opts)) { 24417 opt = opts.ipoptp_cur; 24418 optlen = opts.ipoptp_len; 24419 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 24420 switch (optval) { 24421 uint32_t off; 24422 case IPOPT_SSRR: 24423 case IPOPT_LSRR: 24424 off = opt[IPOPT_OFFSET]; 24425 off--; 24426 if (optlen < IP_ADDR_LEN || 24427 off > optlen - IP_ADDR_LEN) { 24428 /* End of source route */ 24429 break; 24430 } 24431 /* 24432 * This will only happen if two consecutive entries 24433 * in the source route contains our address or if 24434 * it is a packet with a loose source route which 24435 * reaches us before consuming the whole source route 24436 */ 24437 ip1dbg(("ip_wput_local_options: not end of SR\n")); 24438 if (optval == IPOPT_SSRR) { 24439 return; 24440 } 24441 /* 24442 * Hack: instead of dropping the packet truncate the 24443 * source route to what has been used by filling the 24444 * rest with IPOPT_NOP. 24445 */ 24446 opt[IPOPT_OLEN] = (uint8_t)off; 24447 while (off < optlen) { 24448 opt[off++] = IPOPT_NOP; 24449 } 24450 break; 24451 case IPOPT_RR: 24452 off = opt[IPOPT_OFFSET]; 24453 off--; 24454 if (optlen < IP_ADDR_LEN || 24455 off > optlen - IP_ADDR_LEN) { 24456 /* No more room - ignore */ 24457 ip1dbg(( 24458 "ip_wput_forward_options: end of RR\n")); 24459 break; 24460 } 24461 dst = htonl(INADDR_LOOPBACK); 24462 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 24463 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 24464 break; 24465 case IPOPT_TS: 24466 /* Insert timestamp if there is romm */ 24467 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 24468 case IPOPT_TS_TSONLY: 24469 off = IPOPT_TS_TIMELEN; 24470 break; 24471 case IPOPT_TS_PRESPEC: 24472 case IPOPT_TS_PRESPEC_RFC791: 24473 /* Verify that the address matched */ 24474 off = opt[IPOPT_OFFSET] - 1; 24475 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 24476 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 24477 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 24478 if (ire == NULL) { 24479 /* Not for us */ 24480 break; 24481 } 24482 ire_refrele(ire); 24483 /* FALLTHRU */ 24484 case IPOPT_TS_TSANDADDR: 24485 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 24486 break; 24487 default: 24488 /* 24489 * ip_*put_options should have already 24490 * dropped this packet. 24491 */ 24492 cmn_err(CE_PANIC, "ip_wput_local_options: " 24493 "unknown IT - bug in ip_wput_options?\n"); 24494 return; /* Keep "lint" happy */ 24495 } 24496 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 24497 /* Increase overflow counter */ 24498 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 24499 opt[IPOPT_POS_OV_FLG] = (uint8_t) 24500 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 24501 (off << 4); 24502 break; 24503 } 24504 off = opt[IPOPT_OFFSET] - 1; 24505 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 24506 case IPOPT_TS_PRESPEC: 24507 case IPOPT_TS_PRESPEC_RFC791: 24508 case IPOPT_TS_TSANDADDR: 24509 dst = htonl(INADDR_LOOPBACK); 24510 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 24511 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 24512 /* FALLTHRU */ 24513 case IPOPT_TS_TSONLY: 24514 off = opt[IPOPT_OFFSET] - 1; 24515 /* Compute # of milliseconds since midnight */ 24516 gethrestime(&now); 24517 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 24518 now.tv_nsec / (NANOSEC / MILLISEC); 24519 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 24520 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 24521 break; 24522 } 24523 break; 24524 } 24525 } 24526 } 24527 24528 /* 24529 * Send out a multicast packet on interface ipif. 24530 * The sender does not have an conn. 24531 * Caller verifies that this isn't a PHYI_LOOPBACK. 24532 */ 24533 void 24534 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif, zoneid_t zoneid) 24535 { 24536 ipha_t *ipha; 24537 ire_t *ire; 24538 ipaddr_t dst; 24539 mblk_t *first_mp; 24540 24541 /* igmp_sendpkt always allocates a ipsec_out_t */ 24542 ASSERT(mp->b_datap->db_type == M_CTL); 24543 ASSERT(!ipif->ipif_isv6); 24544 ASSERT(!(ipif->ipif_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)); 24545 24546 first_mp = mp; 24547 mp = first_mp->b_cont; 24548 ASSERT(mp->b_datap->db_type == M_DATA); 24549 ipha = (ipha_t *)mp->b_rptr; 24550 24551 /* 24552 * Find an IRE which matches the destination and the outgoing 24553 * queue (i.e. the outgoing interface.) 24554 */ 24555 if (ipif->ipif_flags & IPIF_POINTOPOINT) 24556 dst = ipif->ipif_pp_dst_addr; 24557 else 24558 dst = ipha->ipha_dst; 24559 /* 24560 * The source address has already been initialized by the 24561 * caller and hence matching on ILL (MATCH_IRE_ILL) would 24562 * be sufficient rather than MATCH_IRE_IPIF. 24563 * 24564 * This function is used for sending IGMP packets. We need 24565 * to make sure that we send the packet out of the interface 24566 * (ipif->ipif_ill) where we joined the group. This is to 24567 * prevent from switches doing IGMP snooping to send us multicast 24568 * packets for a given group on the interface we have joined. 24569 * If we can't find an ire, igmp_sendpkt has already initialized 24570 * ipsec_out_attach_if so that this will not be load spread in 24571 * ip_newroute_ipif. 24572 */ 24573 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, NULL, 24574 MATCH_IRE_ILL); 24575 if (!ire) { 24576 /* 24577 * Mark this packet to make it be delivered to 24578 * ip_wput_ire after the new ire has been 24579 * created. 24580 */ 24581 mp->b_prev = NULL; 24582 mp->b_next = NULL; 24583 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC, 24584 zoneid); 24585 return; 24586 } 24587 24588 /* 24589 * Honor the RTF_SETSRC flag; this is the only case 24590 * where we force this addr whatever the current src addr is, 24591 * because this address is set by igmp_sendpkt(), and 24592 * cannot be specified by any user. 24593 */ 24594 if (ire->ire_flags & RTF_SETSRC) { 24595 ipha->ipha_src = ire->ire_src_addr; 24596 } 24597 24598 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE, zoneid); 24599 } 24600 24601 /* 24602 * NOTE : This function does not ire_refrele the ire argument passed in. 24603 * 24604 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 24605 * failure. The nce_fp_mp can vanish any time in the case of IRE_MIPRTUN 24606 * and IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 24607 * the ire_lock to access the nce_fp_mp in this case. 24608 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 24609 * prepending a fastpath message IPQoS processing must precede it, we also set 24610 * the b_band of the fastpath message to that of the mblk returned by IPQoS 24611 * (IPQoS might have set the b_band for CoS marking). 24612 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 24613 * must follow it so that IPQoS can mark the dl_priority field for CoS 24614 * marking, if needed. 24615 */ 24616 static mblk_t * 24617 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, uint32_t ill_index) 24618 { 24619 uint_t hlen; 24620 ipha_t *ipha; 24621 mblk_t *mp1; 24622 boolean_t qos_done = B_FALSE; 24623 uchar_t *ll_hdr; 24624 24625 #define rptr ((uchar_t *)ipha) 24626 24627 ipha = (ipha_t *)mp->b_rptr; 24628 hlen = 0; 24629 LOCK_IRE_FP_MP(ire); 24630 if ((mp1 = ire->ire_nce->nce_fp_mp) != NULL) { 24631 ASSERT(DB_TYPE(mp1) == M_DATA); 24632 /* Initiate IPPF processing */ 24633 if ((proc != 0) && IPP_ENABLED(proc)) { 24634 UNLOCK_IRE_FP_MP(ire); 24635 ip_process(proc, &mp, ill_index); 24636 if (mp == NULL) 24637 return (NULL); 24638 24639 ipha = (ipha_t *)mp->b_rptr; 24640 LOCK_IRE_FP_MP(ire); 24641 if ((mp1 = ire->ire_nce->nce_fp_mp) == NULL) { 24642 qos_done = B_TRUE; 24643 goto no_fp_mp; 24644 } 24645 ASSERT(DB_TYPE(mp1) == M_DATA); 24646 } 24647 hlen = MBLKL(mp1); 24648 /* 24649 * Check if we have enough room to prepend fastpath 24650 * header 24651 */ 24652 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 24653 ll_hdr = rptr - hlen; 24654 bcopy(mp1->b_rptr, ll_hdr, hlen); 24655 /* 24656 * Set the b_rptr to the start of the link layer 24657 * header 24658 */ 24659 mp->b_rptr = ll_hdr; 24660 mp1 = mp; 24661 } else { 24662 mp1 = copyb(mp1); 24663 if (mp1 == NULL) 24664 goto unlock_err; 24665 mp1->b_band = mp->b_band; 24666 mp1->b_cont = mp; 24667 /* 24668 * certain system generated traffic may not 24669 * have cred/label in ip header block. This 24670 * is true even for a labeled system. But for 24671 * labeled traffic, inherit the label in the 24672 * new header. 24673 */ 24674 if (DB_CRED(mp) != NULL) 24675 mblk_setcred(mp1, DB_CRED(mp)); 24676 /* 24677 * XXX disable ICK_VALID and compute checksum 24678 * here; can happen if nce_fp_mp changes and 24679 * it can't be copied now due to insufficient 24680 * space. (unlikely, fp mp can change, but it 24681 * does not increase in length) 24682 */ 24683 } 24684 UNLOCK_IRE_FP_MP(ire); 24685 } else { 24686 no_fp_mp: 24687 mp1 = copyb(ire->ire_nce->nce_res_mp); 24688 if (mp1 == NULL) { 24689 unlock_err: 24690 UNLOCK_IRE_FP_MP(ire); 24691 freemsg(mp); 24692 return (NULL); 24693 } 24694 UNLOCK_IRE_FP_MP(ire); 24695 mp1->b_cont = mp; 24696 /* 24697 * certain system generated traffic may not 24698 * have cred/label in ip header block. This 24699 * is true even for a labeled system. But for 24700 * labeled traffic, inherit the label in the 24701 * new header. 24702 */ 24703 if (DB_CRED(mp) != NULL) 24704 mblk_setcred(mp1, DB_CRED(mp)); 24705 if (!qos_done && (proc != 0) && IPP_ENABLED(proc)) { 24706 ip_process(proc, &mp1, ill_index); 24707 if (mp1 == NULL) 24708 return (NULL); 24709 } 24710 } 24711 return (mp1); 24712 #undef rptr 24713 } 24714 24715 /* 24716 * Finish the outbound IPsec processing for an IPv6 packet. This function 24717 * is called from ipsec_out_process() if the IPsec packet was processed 24718 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 24719 * asynchronously. 24720 */ 24721 void 24722 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 24723 ire_t *ire_arg) 24724 { 24725 in6_addr_t *v6dstp; 24726 ire_t *ire; 24727 mblk_t *mp; 24728 uint_t ill_index; 24729 ipsec_out_t *io; 24730 boolean_t attach_if, hwaccel; 24731 uint32_t flags = IP6_NO_IPPOLICY; 24732 int match_flags; 24733 zoneid_t zoneid; 24734 boolean_t ill_need_rele = B_FALSE; 24735 boolean_t ire_need_rele = B_FALSE; 24736 ill_t *out_ill; 24737 24738 mp = ipsec_mp->b_cont; 24739 io = (ipsec_out_t *)ipsec_mp->b_rptr; 24740 ill_index = io->ipsec_out_ill_index; 24741 if (io->ipsec_out_reachable) { 24742 flags |= IPV6_REACHABILITY_CONFIRMATION; 24743 } 24744 attach_if = io->ipsec_out_attach_if; 24745 hwaccel = io->ipsec_out_accelerated; 24746 zoneid = io->ipsec_out_zoneid; 24747 ASSERT(zoneid != ALL_ZONES); 24748 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 24749 /* Multicast addresses should have non-zero ill_index. */ 24750 v6dstp = &ip6h->ip6_dst; 24751 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 24752 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 24753 ASSERT(!attach_if || ill_index != 0); 24754 if (ill_index != 0) { 24755 if (ill == NULL) { 24756 ill = ip_grab_attach_ill(NULL, ipsec_mp, ill_index, 24757 B_TRUE); 24758 24759 /* Failure case frees things for us. */ 24760 if (ill == NULL) 24761 return; 24762 24763 ill_need_rele = B_TRUE; 24764 } 24765 /* 24766 * If this packet needs to go out on a particular interface 24767 * honor it. 24768 */ 24769 if (attach_if) { 24770 match_flags = MATCH_IRE_ILL; 24771 24772 /* 24773 * Check if we need an ire that will not be 24774 * looked up by anybody else i.e. HIDDEN. 24775 */ 24776 if (ill_is_probeonly(ill)) { 24777 match_flags |= MATCH_IRE_MARK_HIDDEN; 24778 } 24779 } 24780 } 24781 ASSERT(mp != NULL); 24782 24783 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 24784 boolean_t unspec_src; 24785 ipif_t *ipif; 24786 24787 /* 24788 * Use the ill_index to get the right ill. 24789 */ 24790 unspec_src = io->ipsec_out_unspec_src; 24791 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 24792 if (ipif == NULL) { 24793 if (ill_need_rele) 24794 ill_refrele(ill); 24795 freemsg(ipsec_mp); 24796 return; 24797 } 24798 24799 if (ire_arg != NULL) { 24800 ire = ire_arg; 24801 } else { 24802 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 24803 zoneid, MBLK_GETLABEL(mp), match_flags); 24804 ire_need_rele = B_TRUE; 24805 } 24806 if (ire != NULL) { 24807 ipif_refrele(ipif); 24808 /* 24809 * XXX Do the multicast forwarding now, as the IPSEC 24810 * processing has been done. 24811 */ 24812 goto send; 24813 } 24814 24815 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 24816 mp->b_prev = NULL; 24817 mp->b_next = NULL; 24818 24819 /* 24820 * If the IPsec packet was processed asynchronously, 24821 * drop it now. 24822 */ 24823 if (q == NULL) { 24824 if (ill_need_rele) 24825 ill_refrele(ill); 24826 freemsg(ipsec_mp); 24827 return; 24828 } 24829 24830 ip_newroute_ipif_v6(q, ipsec_mp, ipif, *v6dstp, 24831 unspec_src, zoneid); 24832 ipif_refrele(ipif); 24833 } else { 24834 if (attach_if) { 24835 ipif_t *ipif; 24836 24837 ipif = ipif_get_next_ipif(NULL, ill); 24838 if (ipif == NULL) { 24839 if (ill_need_rele) 24840 ill_refrele(ill); 24841 freemsg(ipsec_mp); 24842 return; 24843 } 24844 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 24845 zoneid, MBLK_GETLABEL(mp), match_flags); 24846 ire_need_rele = B_TRUE; 24847 ipif_refrele(ipif); 24848 } else { 24849 if (ire_arg != NULL) { 24850 ire = ire_arg; 24851 } else { 24852 ire = ire_cache_lookup_v6(v6dstp, zoneid, NULL); 24853 ire_need_rele = B_TRUE; 24854 } 24855 } 24856 if (ire != NULL) 24857 goto send; 24858 /* 24859 * ire disappeared underneath. 24860 * 24861 * What we need to do here is the ip_newroute 24862 * logic to get the ire without doing the IPSEC 24863 * processing. Follow the same old path. But this 24864 * time, ip_wput or ire_add_then_send will call us 24865 * directly as all the IPSEC operations are done. 24866 */ 24867 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 24868 mp->b_prev = NULL; 24869 mp->b_next = NULL; 24870 24871 /* 24872 * If the IPsec packet was processed asynchronously, 24873 * drop it now. 24874 */ 24875 if (q == NULL) { 24876 if (ill_need_rele) 24877 ill_refrele(ill); 24878 freemsg(ipsec_mp); 24879 return; 24880 } 24881 24882 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 24883 zoneid); 24884 } 24885 if (ill != NULL && ill_need_rele) 24886 ill_refrele(ill); 24887 return; 24888 send: 24889 if (ill != NULL && ill_need_rele) 24890 ill_refrele(ill); 24891 24892 /* Local delivery */ 24893 if (ire->ire_stq == NULL) { 24894 ASSERT(q != NULL); 24895 24896 /* PFHooks: LOOPBACK_OUT */ 24897 out_ill = ire->ire_ipif->ipif_ill; 24898 24899 DTRACE_PROBE4(ip6__loopback__out__start, 24900 ill_t *, NULL, ill_t *, out_ill, 24901 ip6_t *, ip6h, mblk_t *, ipsec_mp); 24902 24903 FW_HOOKS6(ip6_loopback_out_event, ipv6firewall_loopback_out, 24904 MSG_FWCOOKED_OUT, NULL, out_ill, ip6h, 24905 ipsec_mp, mp); 24906 24907 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, ipsec_mp); 24908 24909 if (ipsec_mp != NULL) 24910 ip_wput_local_v6(RD(q), out_ill, 24911 ip6h, ipsec_mp, ire, 0); 24912 if (ire_need_rele) 24913 ire_refrele(ire); 24914 return; 24915 } 24916 /* 24917 * Everything is done. Send it out on the wire. 24918 * We force the insertion of a fragment header using the 24919 * IPH_FRAG_HDR flag in two cases: 24920 * - after reception of an ICMPv6 "packet too big" message 24921 * with a MTU < 1280 (cf. RFC 2460 section 5) 24922 * - for multirouted IPv6 packets, so that the receiver can 24923 * discard duplicates according to their fragment identifier 24924 */ 24925 /* XXX fix flow control problems. */ 24926 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 24927 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 24928 if (hwaccel) { 24929 /* 24930 * hardware acceleration does not handle these 24931 * "slow path" cases. 24932 */ 24933 /* IPsec KSTATS: should bump bean counter here. */ 24934 if (ire_need_rele) 24935 ire_refrele(ire); 24936 freemsg(ipsec_mp); 24937 return; 24938 } 24939 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 24940 (mp->b_cont ? msgdsize(mp) : 24941 mp->b_wptr - (uchar_t *)ip6h)) { 24942 /* IPsec KSTATS: should bump bean counter here. */ 24943 ip0dbg(("Packet length mismatch: %d, %ld\n", 24944 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 24945 msgdsize(mp))); 24946 if (ire_need_rele) 24947 ire_refrele(ire); 24948 freemsg(ipsec_mp); 24949 return; 24950 } 24951 ASSERT(mp->b_prev == NULL); 24952 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 24953 ntohs(ip6h->ip6_plen) + 24954 IPV6_HDR_LEN, ire->ire_max_frag)); 24955 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 24956 ire->ire_max_frag); 24957 } else { 24958 UPDATE_OB_PKT_COUNT(ire); 24959 ire->ire_last_used_time = lbolt; 24960 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 24961 } 24962 if (ire_need_rele) 24963 ire_refrele(ire); 24964 freeb(ipsec_mp); 24965 } 24966 24967 void 24968 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 24969 { 24970 mblk_t *hada_mp; /* attributes M_CTL mblk */ 24971 da_ipsec_t *hada; /* data attributes */ 24972 ill_t *ill = (ill_t *)q->q_ptr; 24973 24974 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 24975 24976 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 24977 /* IPsec KSTATS: Bump lose counter here! */ 24978 freemsg(mp); 24979 return; 24980 } 24981 24982 /* 24983 * It's an IPsec packet that must be 24984 * accelerated by the Provider, and the 24985 * outbound ill is IPsec acceleration capable. 24986 * Prepends the mblk with an IPHADA_M_CTL, and ship it 24987 * to the ill. 24988 * IPsec KSTATS: should bump packet counter here. 24989 */ 24990 24991 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 24992 if (hada_mp == NULL) { 24993 /* IPsec KSTATS: should bump packet counter here. */ 24994 freemsg(mp); 24995 return; 24996 } 24997 24998 hada_mp->b_datap->db_type = M_CTL; 24999 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 25000 hada_mp->b_cont = mp; 25001 25002 hada = (da_ipsec_t *)hada_mp->b_rptr; 25003 bzero(hada, sizeof (da_ipsec_t)); 25004 hada->da_type = IPHADA_M_CTL; 25005 25006 putnext(q, hada_mp); 25007 } 25008 25009 /* 25010 * Finish the outbound IPsec processing. This function is called from 25011 * ipsec_out_process() if the IPsec packet was processed 25012 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25013 * asynchronously. 25014 */ 25015 void 25016 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 25017 ire_t *ire_arg) 25018 { 25019 uint32_t v_hlen_tos_len; 25020 ipaddr_t dst; 25021 ipif_t *ipif = NULL; 25022 ire_t *ire; 25023 ire_t *ire1 = NULL; 25024 mblk_t *next_mp = NULL; 25025 uint32_t max_frag; 25026 boolean_t multirt_send = B_FALSE; 25027 mblk_t *mp; 25028 mblk_t *mp1; 25029 uint_t ill_index; 25030 ipsec_out_t *io; 25031 boolean_t attach_if; 25032 int match_flags, offset; 25033 irb_t *irb = NULL; 25034 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 25035 zoneid_t zoneid; 25036 uint32_t cksum; 25037 uint16_t *up; 25038 ipxmit_state_t pktxmit_state; 25039 ill_t *out_ill; 25040 #ifdef _BIG_ENDIAN 25041 #define LENGTH (v_hlen_tos_len & 0xFFFF) 25042 #else 25043 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 25044 #endif 25045 25046 mp = ipsec_mp->b_cont; 25047 ASSERT(mp != NULL); 25048 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 25049 dst = ipha->ipha_dst; 25050 25051 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25052 ill_index = io->ipsec_out_ill_index; 25053 attach_if = io->ipsec_out_attach_if; 25054 zoneid = io->ipsec_out_zoneid; 25055 ASSERT(zoneid != ALL_ZONES); 25056 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25057 if (ill_index != 0) { 25058 if (ill == NULL) { 25059 ill = ip_grab_attach_ill(NULL, ipsec_mp, 25060 ill_index, B_FALSE); 25061 25062 /* Failure case frees things for us. */ 25063 if (ill == NULL) 25064 return; 25065 25066 ill_need_rele = B_TRUE; 25067 } 25068 /* 25069 * If this packet needs to go out on a particular interface 25070 * honor it. 25071 */ 25072 if (attach_if) { 25073 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 25074 25075 /* 25076 * Check if we need an ire that will not be 25077 * looked up by anybody else i.e. HIDDEN. 25078 */ 25079 if (ill_is_probeonly(ill)) { 25080 match_flags |= MATCH_IRE_MARK_HIDDEN; 25081 } 25082 } 25083 } 25084 25085 if (CLASSD(dst)) { 25086 boolean_t conn_dontroute; 25087 /* 25088 * Use the ill_index to get the right ipif. 25089 */ 25090 conn_dontroute = io->ipsec_out_dontroute; 25091 if (ill_index == 0) 25092 ipif = ipif_lookup_group(dst, zoneid); 25093 else 25094 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25095 if (ipif == NULL) { 25096 ip1dbg(("ip_wput_ipsec_out: No ipif for" 25097 " multicast\n")); 25098 BUMP_MIB(&ip_mib, ipOutNoRoutes); 25099 freemsg(ipsec_mp); 25100 goto done; 25101 } 25102 /* 25103 * ipha_src has already been intialized with the 25104 * value of the ipif in ip_wput. All we need now is 25105 * an ire to send this downstream. 25106 */ 25107 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 25108 MBLK_GETLABEL(mp), match_flags); 25109 if (ire != NULL) { 25110 ill_t *ill1; 25111 /* 25112 * Do the multicast forwarding now, as the IPSEC 25113 * processing has been done. 25114 */ 25115 if (ip_g_mrouter && !conn_dontroute && 25116 (ill1 = ire_to_ill(ire))) { 25117 if (ip_mforward(ill1, ipha, mp)) { 25118 freemsg(ipsec_mp); 25119 ip1dbg(("ip_wput_ipsec_out: mforward " 25120 "failed\n")); 25121 ire_refrele(ire); 25122 goto done; 25123 } 25124 } 25125 goto send; 25126 } 25127 25128 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 25129 mp->b_prev = NULL; 25130 mp->b_next = NULL; 25131 25132 /* 25133 * If the IPsec packet was processed asynchronously, 25134 * drop it now. 25135 */ 25136 if (q == NULL) { 25137 freemsg(ipsec_mp); 25138 goto done; 25139 } 25140 25141 /* 25142 * We may be using a wrong ipif to create the ire. 25143 * But it is okay as the source address is assigned 25144 * for the packet already. Next outbound packet would 25145 * create the IRE with the right IPIF in ip_wput. 25146 * 25147 * Also handle RTF_MULTIRT routes. 25148 */ 25149 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT, 25150 zoneid); 25151 } else { 25152 if (attach_if) { 25153 ire = ire_ctable_lookup(dst, 0, 0, ill->ill_ipif, 25154 zoneid, MBLK_GETLABEL(mp), match_flags); 25155 } else { 25156 if (ire_arg != NULL) { 25157 ire = ire_arg; 25158 ire_need_rele = B_FALSE; 25159 } else { 25160 ire = ire_cache_lookup(dst, zoneid, 25161 MBLK_GETLABEL(mp)); 25162 } 25163 } 25164 if (ire != NULL) { 25165 goto send; 25166 } 25167 25168 /* 25169 * ire disappeared underneath. 25170 * 25171 * What we need to do here is the ip_newroute 25172 * logic to get the ire without doing the IPSEC 25173 * processing. Follow the same old path. But this 25174 * time, ip_wput or ire_add_then_put will call us 25175 * directly as all the IPSEC operations are done. 25176 */ 25177 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 25178 mp->b_prev = NULL; 25179 mp->b_next = NULL; 25180 25181 /* 25182 * If the IPsec packet was processed asynchronously, 25183 * drop it now. 25184 */ 25185 if (q == NULL) { 25186 freemsg(ipsec_mp); 25187 goto done; 25188 } 25189 25190 /* 25191 * Since we're going through ip_newroute() again, we 25192 * need to make sure we don't: 25193 * 25194 * 1.) Trigger the ASSERT() with the ipha_ident 25195 * overloading. 25196 * 2.) Redo transport-layer checksumming, since we've 25197 * already done all that to get this far. 25198 * 25199 * The easiest way not do either of the above is to set 25200 * the ipha_ident field to IP_HDR_INCLUDED. 25201 */ 25202 ipha->ipha_ident = IP_HDR_INCLUDED; 25203 ip_newroute(q, ipsec_mp, dst, NULL, 25204 (CONN_Q(q) ? Q_TO_CONN(q) : NULL), zoneid); 25205 } 25206 goto done; 25207 send: 25208 if (ipha->ipha_protocol == IPPROTO_UDP && udp_compute_checksum()) { 25209 /* 25210 * ESP NAT-Traversal packet. 25211 * 25212 * Just do software checksum for now. 25213 */ 25214 25215 offset = IP_SIMPLE_HDR_LENGTH + UDP_CHECKSUM_OFFSET; 25216 IP_STAT(ip_out_sw_cksum); 25217 IP_STAT_UPDATE(ip_udp_out_sw_cksum_bytes, 25218 ntohs(htons(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH)); 25219 #define iphs ((uint16_t *)ipha) 25220 cksum = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 25221 iphs[9] + ntohs(htons(ipha->ipha_length) - 25222 IP_SIMPLE_HDR_LENGTH); 25223 #undef iphs 25224 if ((cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH, cksum)) == 0) 25225 cksum = 0xFFFF; 25226 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) 25227 if (mp1->b_wptr - mp1->b_rptr >= 25228 offset + sizeof (uint16_t)) { 25229 up = (uint16_t *)(mp1->b_rptr + offset); 25230 *up = cksum; 25231 break; /* out of for loop */ 25232 } else { 25233 offset -= (mp->b_wptr - mp->b_rptr); 25234 } 25235 } /* Otherwise, just keep the all-zero checksum. */ 25236 25237 if (ire->ire_stq == NULL) { 25238 /* 25239 * Loopbacks go through ip_wput_local except for one case. 25240 * We come here if we generate a icmp_frag_needed message 25241 * after IPSEC processing is over. When this function calls 25242 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 25243 * icmp_frag_needed. The message generated comes back here 25244 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 25245 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 25246 * source address as it is usually set in ip_wput_ire. As 25247 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 25248 * and we end up here. We can't enter ip_wput_ire once the 25249 * IPSEC processing is over and hence we need to do it here. 25250 */ 25251 ASSERT(q != NULL); 25252 UPDATE_OB_PKT_COUNT(ire); 25253 ire->ire_last_used_time = lbolt; 25254 if (ipha->ipha_src == 0) 25255 ipha->ipha_src = ire->ire_src_addr; 25256 25257 /* PFHooks: LOOPBACK_OUT */ 25258 out_ill = ire->ire_ipif->ipif_ill; 25259 25260 DTRACE_PROBE4(ip4__loopback__out__start, 25261 ill_t *, NULL, ill_t *, out_ill, 25262 ipha_t *, ipha, mblk_t *, ipsec_mp); 25263 25264 FW_HOOKS(ip4_loopback_out_event, ipv4firewall_loopback_out, 25265 MSG_FWCOOKED_OUT, NULL, out_ill, ipha, ipsec_mp, mp); 25266 25267 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp); 25268 25269 if (ipsec_mp != NULL) 25270 ip_wput_local(RD(q), out_ill, 25271 ipha, ipsec_mp, ire, 0, zoneid); 25272 if (ire_need_rele) 25273 ire_refrele(ire); 25274 goto done; 25275 } 25276 25277 if (ire->ire_max_frag < (unsigned int)LENGTH) { 25278 /* 25279 * We are through with IPSEC processing. 25280 * Fragment this and send it on the wire. 25281 */ 25282 if (io->ipsec_out_accelerated) { 25283 /* 25284 * The packet has been accelerated but must 25285 * be fragmented. This should not happen 25286 * since AH and ESP must not accelerate 25287 * packets that need fragmentation, however 25288 * the configuration could have changed 25289 * since the AH or ESP processing. 25290 * Drop packet. 25291 * IPsec KSTATS: bump bean counter here. 25292 */ 25293 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 25294 "fragmented accelerated packet!\n")); 25295 freemsg(ipsec_mp); 25296 } else { 25297 ip_wput_ire_fragmentit(ipsec_mp, ire, zoneid); 25298 } 25299 if (ire_need_rele) 25300 ire_refrele(ire); 25301 goto done; 25302 } 25303 25304 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 25305 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 25306 (void *)ire->ire_ipif, (void *)ipif)); 25307 25308 /* 25309 * Multiroute the secured packet, unless IPsec really 25310 * requires the packet to go out only through a particular 25311 * interface. 25312 */ 25313 if ((ire->ire_flags & RTF_MULTIRT) && !attach_if) { 25314 ire_t *first_ire; 25315 irb = ire->ire_bucket; 25316 ASSERT(irb != NULL); 25317 /* 25318 * This ire has been looked up as the one that 25319 * goes through the given ipif; 25320 * make sure we do not omit any other multiroute ire 25321 * that may be present in the bucket before this one. 25322 */ 25323 IRB_REFHOLD(irb); 25324 for (first_ire = irb->irb_ire; 25325 first_ire != NULL; 25326 first_ire = first_ire->ire_next) { 25327 if ((first_ire->ire_flags & RTF_MULTIRT) && 25328 (first_ire->ire_addr == ire->ire_addr) && 25329 !(first_ire->ire_marks & 25330 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 25331 break; 25332 } 25333 25334 if ((first_ire != NULL) && (first_ire != ire)) { 25335 /* 25336 * Don't change the ire if the packet must 25337 * be fragmented if sent via this new one. 25338 */ 25339 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 25340 IRE_REFHOLD(first_ire); 25341 if (ire_need_rele) 25342 ire_refrele(ire); 25343 else 25344 ire_need_rele = B_TRUE; 25345 ire = first_ire; 25346 } 25347 } 25348 IRB_REFRELE(irb); 25349 25350 multirt_send = B_TRUE; 25351 max_frag = ire->ire_max_frag; 25352 } else { 25353 if ((ire->ire_flags & RTF_MULTIRT) && attach_if) { 25354 ip1dbg(("ip_wput_ipsec_out: ignoring multirouting " 25355 "flag, attach_if %d\n", attach_if)); 25356 } 25357 } 25358 25359 /* 25360 * In most cases, the emission loop below is entered only once. 25361 * Only in the case where the ire holds the RTF_MULTIRT 25362 * flag, we loop to process all RTF_MULTIRT ires in the 25363 * bucket, and send the packet through all crossed 25364 * RTF_MULTIRT routes. 25365 */ 25366 do { 25367 if (multirt_send) { 25368 /* 25369 * ire1 holds here the next ire to process in the 25370 * bucket. If multirouting is expected, 25371 * any non-RTF_MULTIRT ire that has the 25372 * right destination address is ignored. 25373 */ 25374 ASSERT(irb != NULL); 25375 IRB_REFHOLD(irb); 25376 for (ire1 = ire->ire_next; 25377 ire1 != NULL; 25378 ire1 = ire1->ire_next) { 25379 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 25380 continue; 25381 if (ire1->ire_addr != ire->ire_addr) 25382 continue; 25383 if (ire1->ire_marks & 25384 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 25385 continue; 25386 /* No loopback here */ 25387 if (ire1->ire_stq == NULL) 25388 continue; 25389 /* 25390 * Ensure we do not exceed the MTU 25391 * of the next route. 25392 */ 25393 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 25394 ip_multirt_bad_mtu(ire1, max_frag); 25395 continue; 25396 } 25397 25398 IRE_REFHOLD(ire1); 25399 break; 25400 } 25401 IRB_REFRELE(irb); 25402 if (ire1 != NULL) { 25403 /* 25404 * We are in a multiple send case, need to 25405 * make a copy of the packet. 25406 */ 25407 next_mp = copymsg(ipsec_mp); 25408 if (next_mp == NULL) { 25409 ire_refrele(ire1); 25410 ire1 = NULL; 25411 } 25412 } 25413 } 25414 /* 25415 * Everything is done. Send it out on the wire 25416 * 25417 * ip_xmit_v4 will call ip_wput_attach_llhdr and then 25418 * either send it on the wire or, in the case of 25419 * HW acceleration, call ipsec_hw_putnext. 25420 */ 25421 if (ire->ire_nce && 25422 ire->ire_nce->nce_state != ND_REACHABLE) { 25423 DTRACE_PROBE2(ip__wput__ipsec__bail, 25424 (ire_t *), ire, (mblk_t *), ipsec_mp); 25425 /* 25426 * If ire's link-layer is unresolved (this 25427 * would only happen if the incomplete ire 25428 * was added to cachetable via forwarding path) 25429 * don't bother going to ip_xmit_v4. Just drop the 25430 * packet. 25431 * There is a slight risk here, in that, if we 25432 * have the forwarding path create an incomplete 25433 * IRE, then until the IRE is completed, any 25434 * transmitted IPSEC packets will be dropped 25435 * instead of being queued waiting for resolution. 25436 * 25437 * But the likelihood of a forwarding packet and a wput 25438 * packet sending to the same dst at the same time 25439 * and there not yet be an ARP entry for it is small. 25440 * Furthermore, if this actually happens, it might 25441 * be likely that wput would generate multiple 25442 * packets (and forwarding would also have a train 25443 * of packets) for that destination. If this is 25444 * the case, some of them would have been dropped 25445 * anyway, since ARP only queues a few packets while 25446 * waiting for resolution 25447 * 25448 * NOTE: We should really call ip_xmit_v4, 25449 * and let it queue the packet and send the 25450 * ARP query and have ARP come back thus: 25451 * <ARP> ip_wput->ip_output->ip-wput_nondata-> 25452 * ip_xmit_v4->ip_wput_attach_llhdr + ipsec 25453 * hw accel work. But it's too complex to get 25454 * the IPsec hw acceleration approach to fit 25455 * well with ip_xmit_v4 doing ARP without 25456 * doing IPSEC simplification. For now, we just 25457 * poke ip_xmit_v4 to trigger the arp resolve, so 25458 * that we can continue with the send on the next 25459 * attempt. 25460 * 25461 * XXX THis should be revisited, when 25462 * the IPsec/IP interaction is cleaned up 25463 */ 25464 ip1dbg(("ip_wput_ipsec_out: ire is incomplete" 25465 " - dropping packet\n")); 25466 freemsg(ipsec_mp); 25467 /* 25468 * Call ip_xmit_v4() to trigger ARP query 25469 * in case the nce_state is ND_INITIAL 25470 */ 25471 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 25472 goto drop_pkt; 25473 } 25474 25475 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 25476 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 25477 mblk_t *, mp); 25478 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 25479 MSG_FWCOOKED_OUT, NULL, out_ill, ipha, mp, mp); 25480 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 25481 if (mp == NULL) 25482 goto drop_pkt; 25483 25484 ip1dbg(("ip_wput_ipsec_out: calling ip_xmit_v4\n")); 25485 pktxmit_state = ip_xmit_v4(mp, ire, 25486 (io->ipsec_out_accelerated ? io : NULL), B_FALSE); 25487 25488 if ((pktxmit_state == SEND_FAILED) || 25489 (pktxmit_state == LLHDR_RESLV_FAILED)) { 25490 25491 freeb(ipsec_mp); /* ip_xmit_v4 frees the mp */ 25492 drop_pkt: 25493 BUMP_MIB(&ip_mib, ipOutDiscards); 25494 if (ire_need_rele) 25495 ire_refrele(ire); 25496 if (ire1 != NULL) { 25497 ire_refrele(ire1); 25498 freemsg(next_mp); 25499 } 25500 goto done; 25501 } 25502 25503 freeb(ipsec_mp); 25504 if (ire_need_rele) 25505 ire_refrele(ire); 25506 25507 if (ire1 != NULL) { 25508 ire = ire1; 25509 ire_need_rele = B_TRUE; 25510 ASSERT(next_mp); 25511 ipsec_mp = next_mp; 25512 mp = ipsec_mp->b_cont; 25513 ire1 = NULL; 25514 next_mp = NULL; 25515 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25516 } else { 25517 multirt_send = B_FALSE; 25518 } 25519 } while (multirt_send); 25520 done: 25521 if (ill != NULL && ill_need_rele) 25522 ill_refrele(ill); 25523 if (ipif != NULL) 25524 ipif_refrele(ipif); 25525 } 25526 25527 /* 25528 * Get the ill corresponding to the specified ire, and compare its 25529 * capabilities with the protocol and algorithms specified by the 25530 * the SA obtained from ipsec_out. If they match, annotate the 25531 * ipsec_out structure to indicate that the packet needs acceleration. 25532 * 25533 * 25534 * A packet is eligible for outbound hardware acceleration if the 25535 * following conditions are satisfied: 25536 * 25537 * 1. the packet will not be fragmented 25538 * 2. the provider supports the algorithm 25539 * 3. there is no pending control message being exchanged 25540 * 4. snoop is not attached 25541 * 5. the destination address is not a broadcast or multicast address. 25542 * 25543 * Rationale: 25544 * - Hardware drivers do not support fragmentation with 25545 * the current interface. 25546 * - snoop, multicast, and broadcast may result in exposure of 25547 * a cleartext datagram. 25548 * We check all five of these conditions here. 25549 * 25550 * XXX would like to nuke "ire_t *" parameter here; problem is that 25551 * IRE is only way to figure out if a v4 address is a broadcast and 25552 * thus ineligible for acceleration... 25553 */ 25554 static void 25555 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 25556 { 25557 ipsec_out_t *io; 25558 mblk_t *data_mp; 25559 uint_t plen, overhead; 25560 25561 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 25562 return; 25563 25564 if (ill == NULL) 25565 return; 25566 25567 /* 25568 * Destination address is a broadcast or multicast. Punt. 25569 */ 25570 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 25571 IRE_LOCAL))) 25572 return; 25573 25574 data_mp = ipsec_mp->b_cont; 25575 25576 if (ill->ill_isv6) { 25577 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 25578 25579 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 25580 return; 25581 25582 plen = ip6h->ip6_plen; 25583 } else { 25584 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 25585 25586 if (CLASSD(ipha->ipha_dst)) 25587 return; 25588 25589 plen = ipha->ipha_length; 25590 } 25591 /* 25592 * Is there a pending DLPI control message being exchanged 25593 * between IP/IPsec and the DLS Provider? If there is, it 25594 * could be a SADB update, and the state of the DLS Provider 25595 * SADB might not be in sync with the SADB maintained by 25596 * IPsec. To avoid dropping packets or using the wrong keying 25597 * material, we do not accelerate this packet. 25598 */ 25599 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 25600 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 25601 "ill_dlpi_pending! don't accelerate packet\n")); 25602 return; 25603 } 25604 25605 /* 25606 * Is the Provider in promiscous mode? If it does, we don't 25607 * accelerate the packet since it will bounce back up to the 25608 * listeners in the clear. 25609 */ 25610 if (ill->ill_promisc_on_phys) { 25611 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 25612 "ill in promiscous mode, don't accelerate packet\n")); 25613 return; 25614 } 25615 25616 /* 25617 * Will the packet require fragmentation? 25618 */ 25619 25620 /* 25621 * IPsec ESP note: this is a pessimistic estimate, but the same 25622 * as is used elsewhere. 25623 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 25624 * + 2-byte trailer 25625 */ 25626 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 25627 IPSEC_BASE_ESP_HDR_SIZE(sa); 25628 25629 if ((plen + overhead) > ill->ill_max_mtu) 25630 return; 25631 25632 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25633 25634 /* 25635 * Can the ill accelerate this IPsec protocol and algorithm 25636 * specified by the SA? 25637 */ 25638 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 25639 ill->ill_isv6, sa)) { 25640 return; 25641 } 25642 25643 /* 25644 * Tell AH or ESP that the outbound ill is capable of 25645 * accelerating this packet. 25646 */ 25647 io->ipsec_out_is_capab_ill = B_TRUE; 25648 } 25649 25650 /* 25651 * Select which AH & ESP SA's to use (if any) for the outbound packet. 25652 * 25653 * If this function returns B_TRUE, the requested SA's have been filled 25654 * into the ipsec_out_*_sa pointers. 25655 * 25656 * If the function returns B_FALSE, the packet has been "consumed", most 25657 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 25658 * 25659 * The SA references created by the protocol-specific "select" 25660 * function will be released when the ipsec_mp is freed, thanks to the 25661 * ipsec_out_free destructor -- see spd.c. 25662 */ 25663 static boolean_t 25664 ipsec_out_select_sa(mblk_t *ipsec_mp) 25665 { 25666 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 25667 ipsec_out_t *io; 25668 ipsec_policy_t *pp; 25669 ipsec_action_t *ap; 25670 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25671 ASSERT(io->ipsec_out_type == IPSEC_OUT); 25672 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 25673 25674 if (!io->ipsec_out_secure) { 25675 /* 25676 * We came here by mistake. 25677 * Don't bother with ipsec processing 25678 * We should "discourage" this path in the future. 25679 */ 25680 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 25681 return (B_FALSE); 25682 } 25683 ASSERT(io->ipsec_out_need_policy == B_FALSE); 25684 ASSERT((io->ipsec_out_policy != NULL) || 25685 (io->ipsec_out_act != NULL)); 25686 25687 ASSERT(io->ipsec_out_failed == B_FALSE); 25688 25689 /* 25690 * IPSEC processing has started. 25691 */ 25692 io->ipsec_out_proc_begin = B_TRUE; 25693 ap = io->ipsec_out_act; 25694 if (ap == NULL) { 25695 pp = io->ipsec_out_policy; 25696 ASSERT(pp != NULL); 25697 ap = pp->ipsp_act; 25698 ASSERT(ap != NULL); 25699 } 25700 25701 /* 25702 * We have an action. now, let's select SA's. 25703 * (In the future, we can cache this in the conn_t..) 25704 */ 25705 if (ap->ipa_want_esp) { 25706 if (io->ipsec_out_esp_sa == NULL) { 25707 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 25708 IPPROTO_ESP); 25709 } 25710 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 25711 } 25712 25713 if (ap->ipa_want_ah) { 25714 if (io->ipsec_out_ah_sa == NULL) { 25715 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 25716 IPPROTO_AH); 25717 } 25718 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 25719 /* 25720 * The ESP and AH processing order needs to be preserved 25721 * when both protocols are required (ESP should be applied 25722 * before AH for an outbound packet). Force an ESP ACQUIRE 25723 * when both ESP and AH are required, and an AH ACQUIRE 25724 * is needed. 25725 */ 25726 if (ap->ipa_want_esp && need_ah_acquire) 25727 need_esp_acquire = B_TRUE; 25728 } 25729 25730 /* 25731 * Send an ACQUIRE (extended, regular, or both) if we need one. 25732 * Release SAs that got referenced, but will not be used until we 25733 * acquire _all_ of the SAs we need. 25734 */ 25735 if (need_ah_acquire || need_esp_acquire) { 25736 if (io->ipsec_out_ah_sa != NULL) { 25737 IPSA_REFRELE(io->ipsec_out_ah_sa); 25738 io->ipsec_out_ah_sa = NULL; 25739 } 25740 if (io->ipsec_out_esp_sa != NULL) { 25741 IPSA_REFRELE(io->ipsec_out_esp_sa); 25742 io->ipsec_out_esp_sa = NULL; 25743 } 25744 25745 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 25746 return (B_FALSE); 25747 } 25748 25749 return (B_TRUE); 25750 } 25751 25752 /* 25753 * Process an IPSEC_OUT message and see what you can 25754 * do with it. 25755 * IPQoS Notes: 25756 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 25757 * IPSec. 25758 * XXX would like to nuke ire_t. 25759 * XXX ill_index better be "real" 25760 */ 25761 void 25762 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 25763 { 25764 ipsec_out_t *io; 25765 ipsec_policy_t *pp; 25766 ipsec_action_t *ap; 25767 ipha_t *ipha; 25768 ip6_t *ip6h; 25769 mblk_t *mp; 25770 ill_t *ill; 25771 zoneid_t zoneid; 25772 ipsec_status_t ipsec_rc; 25773 boolean_t ill_need_rele = B_FALSE; 25774 25775 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25776 ASSERT(io->ipsec_out_type == IPSEC_OUT); 25777 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 25778 mp = ipsec_mp->b_cont; 25779 25780 /* 25781 * Initiate IPPF processing. We do it here to account for packets 25782 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 25783 * We can check for ipsec_out_proc_begin even for such packets, as 25784 * they will always be false (asserted below). 25785 */ 25786 if (IPP_ENABLED(IPP_LOCAL_OUT) && !io->ipsec_out_proc_begin) { 25787 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 25788 io->ipsec_out_ill_index : ill_index); 25789 if (mp == NULL) { 25790 ip2dbg(("ipsec_out_process: packet dropped "\ 25791 "during IPPF processing\n")); 25792 freeb(ipsec_mp); 25793 BUMP_MIB(&ip_mib, ipOutDiscards); 25794 return; 25795 } 25796 } 25797 25798 if (!io->ipsec_out_secure) { 25799 /* 25800 * We came here by mistake. 25801 * Don't bother with ipsec processing 25802 * Should "discourage" this path in the future. 25803 */ 25804 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 25805 goto done; 25806 } 25807 ASSERT(io->ipsec_out_need_policy == B_FALSE); 25808 ASSERT((io->ipsec_out_policy != NULL) || 25809 (io->ipsec_out_act != NULL)); 25810 ASSERT(io->ipsec_out_failed == B_FALSE); 25811 25812 if (!ipsec_loaded()) { 25813 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 25814 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 25815 BUMP_MIB(&ip_mib, ipOutDiscards); 25816 } else { 25817 BUMP_MIB(&ip6_mib, ipv6OutDiscards); 25818 } 25819 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 25820 &ipdrops_ip_ipsec_not_loaded, &ip_dropper); 25821 return; 25822 } 25823 25824 /* 25825 * IPSEC processing has started. 25826 */ 25827 io->ipsec_out_proc_begin = B_TRUE; 25828 ap = io->ipsec_out_act; 25829 if (ap == NULL) { 25830 pp = io->ipsec_out_policy; 25831 ASSERT(pp != NULL); 25832 ap = pp->ipsp_act; 25833 ASSERT(ap != NULL); 25834 } 25835 25836 /* 25837 * Save the outbound ill index. When the packet comes back 25838 * from IPsec, we make sure the ill hasn't changed or disappeared 25839 * before sending it the accelerated packet. 25840 */ 25841 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 25842 int ifindex; 25843 ill = ire_to_ill(ire); 25844 ifindex = ill->ill_phyint->phyint_ifindex; 25845 io->ipsec_out_capab_ill_index = ifindex; 25846 } 25847 25848 /* 25849 * The order of processing is first insert a IP header if needed. 25850 * Then insert the ESP header and then the AH header. 25851 */ 25852 if ((io->ipsec_out_se_done == B_FALSE) && 25853 (ap->ipa_want_se)) { 25854 /* 25855 * First get the outer IP header before sending 25856 * it to ESP. 25857 */ 25858 ipha_t *oipha, *iipha; 25859 mblk_t *outer_mp, *inner_mp; 25860 25861 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 25862 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 25863 "ipsec_out_process: " 25864 "Self-Encapsulation failed: Out of memory\n"); 25865 freemsg(ipsec_mp); 25866 BUMP_MIB(&ip_mib, ipOutDiscards); 25867 return; 25868 } 25869 inner_mp = ipsec_mp->b_cont; 25870 ASSERT(inner_mp->b_datap->db_type == M_DATA); 25871 oipha = (ipha_t *)outer_mp->b_rptr; 25872 iipha = (ipha_t *)inner_mp->b_rptr; 25873 *oipha = *iipha; 25874 outer_mp->b_wptr += sizeof (ipha_t); 25875 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 25876 sizeof (ipha_t)); 25877 oipha->ipha_protocol = IPPROTO_ENCAP; 25878 oipha->ipha_version_and_hdr_length = 25879 IP_SIMPLE_HDR_VERSION; 25880 oipha->ipha_hdr_checksum = 0; 25881 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 25882 outer_mp->b_cont = inner_mp; 25883 ipsec_mp->b_cont = outer_mp; 25884 25885 io->ipsec_out_se_done = B_TRUE; 25886 io->ipsec_out_encaps = B_TRUE; 25887 } 25888 25889 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 25890 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 25891 !ipsec_out_select_sa(ipsec_mp)) 25892 return; 25893 25894 /* 25895 * By now, we know what SA's to use. Toss over to ESP & AH 25896 * to do the heavy lifting. 25897 */ 25898 zoneid = io->ipsec_out_zoneid; 25899 ASSERT(zoneid != ALL_ZONES); 25900 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 25901 ASSERT(io->ipsec_out_esp_sa != NULL); 25902 io->ipsec_out_esp_done = B_TRUE; 25903 /* 25904 * Note that since hw accel can only apply one transform, 25905 * not two, we skip hw accel for ESP if we also have AH 25906 * This is an design limitation of the interface 25907 * which should be revisited. 25908 */ 25909 ASSERT(ire != NULL); 25910 if (io->ipsec_out_ah_sa == NULL) { 25911 ill = (ill_t *)ire->ire_stq->q_ptr; 25912 ipsec_out_is_accelerated(ipsec_mp, 25913 io->ipsec_out_esp_sa, ill, ire); 25914 } 25915 25916 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 25917 switch (ipsec_rc) { 25918 case IPSEC_STATUS_SUCCESS: 25919 break; 25920 case IPSEC_STATUS_FAILED: 25921 BUMP_MIB(&ip_mib, ipOutDiscards); 25922 /* FALLTHRU */ 25923 case IPSEC_STATUS_PENDING: 25924 return; 25925 } 25926 } 25927 25928 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 25929 ASSERT(io->ipsec_out_ah_sa != NULL); 25930 io->ipsec_out_ah_done = B_TRUE; 25931 if (ire == NULL) { 25932 int idx = io->ipsec_out_capab_ill_index; 25933 ill = ill_lookup_on_ifindex(idx, B_FALSE, 25934 NULL, NULL, NULL, NULL); 25935 ill_need_rele = B_TRUE; 25936 } else { 25937 ill = (ill_t *)ire->ire_stq->q_ptr; 25938 } 25939 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 25940 ire); 25941 25942 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 25943 switch (ipsec_rc) { 25944 case IPSEC_STATUS_SUCCESS: 25945 break; 25946 case IPSEC_STATUS_FAILED: 25947 BUMP_MIB(&ip_mib, ipOutDiscards); 25948 /* FALLTHRU */ 25949 case IPSEC_STATUS_PENDING: 25950 if (ill != NULL && ill_need_rele) 25951 ill_refrele(ill); 25952 return; 25953 } 25954 } 25955 /* 25956 * We are done with IPSEC processing. Send it over 25957 * the wire. 25958 */ 25959 done: 25960 mp = ipsec_mp->b_cont; 25961 ipha = (ipha_t *)mp->b_rptr; 25962 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 25963 ip_wput_ipsec_out(q, ipsec_mp, ipha, ill, ire); 25964 } else { 25965 ip6h = (ip6_t *)ipha; 25966 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ill, ire); 25967 } 25968 if (ill != NULL && ill_need_rele) 25969 ill_refrele(ill); 25970 } 25971 25972 /* ARGSUSED */ 25973 void 25974 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 25975 { 25976 opt_restart_t *or; 25977 int err; 25978 conn_t *connp; 25979 25980 ASSERT(CONN_Q(q)); 25981 connp = Q_TO_CONN(q); 25982 25983 ASSERT(first_mp->b_datap->db_type == M_CTL); 25984 or = (opt_restart_t *)first_mp->b_rptr; 25985 /* 25986 * We don't need to pass any credentials here since this is just 25987 * a restart. The credentials are passed in when svr4_optcom_req 25988 * is called the first time (from ip_wput_nondata). 25989 */ 25990 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 25991 err = svr4_optcom_req(q, first_mp, NULL, 25992 &ip_opt_obj); 25993 } else { 25994 ASSERT(or->or_type == T_OPTMGMT_REQ); 25995 err = tpi_optcom_req(q, first_mp, NULL, 25996 &ip_opt_obj); 25997 } 25998 if (err != EINPROGRESS) { 25999 /* operation is done */ 26000 CONN_OPER_PENDING_DONE(connp); 26001 } 26002 } 26003 26004 /* 26005 * ioctls that go through a down/up sequence may need to wait for the down 26006 * to complete. This involves waiting for the ire and ipif refcnts to go down 26007 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 26008 */ 26009 /* ARGSUSED */ 26010 void 26011 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 26012 { 26013 struct iocblk *iocp; 26014 mblk_t *mp1; 26015 ipif_t *ipif; 26016 ip_ioctl_cmd_t *ipip; 26017 int err; 26018 sin_t *sin; 26019 struct lifreq *lifr; 26020 struct ifreq *ifr; 26021 26022 iocp = (struct iocblk *)mp->b_rptr; 26023 ASSERT(ipsq != NULL); 26024 /* Existence of mp1 verified in ip_wput_nondata */ 26025 mp1 = mp->b_cont->b_cont; 26026 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26027 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 26028 ill_t *ill; 26029 /* 26030 * Special case where ipsq_current_ipif may not be set. 26031 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 26032 * ill could also have become part of a ipmp group in the 26033 * process, we are here as were not able to complete the 26034 * operation in ipif_set_values because we could not become 26035 * exclusive on the new ipsq, In such a case ipsq_current_ipif 26036 * will not be set so we need to set it. 26037 */ 26038 ill = (ill_t *)q->q_ptr; 26039 ipsq->ipsq_current_ipif = ill->ill_ipif; 26040 ipsq->ipsq_last_cmd = ipip->ipi_cmd; 26041 } 26042 26043 ipif = ipsq->ipsq_current_ipif; 26044 ASSERT(ipif != NULL); 26045 if (ipip->ipi_cmd_type == IF_CMD) { 26046 /* This a old style SIOC[GS]IF* command */ 26047 ifr = (struct ifreq *)mp1->b_rptr; 26048 sin = (sin_t *)&ifr->ifr_addr; 26049 } else if (ipip->ipi_cmd_type == LIF_CMD) { 26050 /* This a new style SIOC[GS]LIF* command */ 26051 lifr = (struct lifreq *)mp1->b_rptr; 26052 sin = (sin_t *)&lifr->lifr_addr; 26053 } else { 26054 sin = NULL; 26055 } 26056 26057 err = (*ipip->ipi_func_restart)(ipif, sin, q, mp, ipip, 26058 (void *)mp1->b_rptr); 26059 26060 /* SIOCLIFREMOVEIF could have removed the ipif */ 26061 ip_ioctl_finish(q, mp, err, 26062 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 26063 ipip->ipi_cmd == SIOCLIFREMOVEIF ? NULL : ipif, ipsq); 26064 } 26065 26066 /* 26067 * ioctl processing 26068 * 26069 * ioctl processing starts with ip_sioctl_copyin_setup which looks up 26070 * the ioctl command in the ioctl tables and determines the copyin data size 26071 * from the ioctl property ipi_copyin_size, and does an mi_copyin() of that 26072 * size. 26073 * 26074 * ioctl processing then continues when the M_IOCDATA makes its way down. 26075 * Now the ioctl is looked up again in the ioctl table, and its properties are 26076 * extracted. The associated 'conn' is then refheld till the end of the ioctl 26077 * and the general ioctl processing function ip_process_ioctl is called. 26078 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 26079 * so goes thru the serialization primitive ipsq_try_enter. Then the 26080 * appropriate function to handle the ioctl is called based on the entry in 26081 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 26082 * which also refreleases the 'conn' that was refheld at the start of the 26083 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 26084 * ip_extract_lifreq_cmn extracts the interface name from the lifreq/ifreq 26085 * struct and looks up the ipif. ip_extract_tunreq handles the case of tunnel. 26086 * 26087 * Many exclusive ioctls go thru an internal down up sequence as part of 26088 * the operation. For example an attempt to change the IP address of an 26089 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 26090 * does all the cleanup such as deleting all ires that use this address. 26091 * Then we need to wait till all references to the interface go away. 26092 */ 26093 void 26094 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 26095 { 26096 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 26097 ip_ioctl_cmd_t *ipip = (ip_ioctl_cmd_t *)arg; 26098 cmd_info_t ci; 26099 int err; 26100 boolean_t entered_ipsq = B_FALSE; 26101 26102 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 26103 26104 if (ipip == NULL) 26105 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26106 26107 /* 26108 * SIOCLIFADDIF needs to go thru a special path since the 26109 * ill may not exist yet. This happens in the case of lo0 26110 * which is created using this ioctl. 26111 */ 26112 if (ipip->ipi_cmd == SIOCLIFADDIF) { 26113 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 26114 ip_ioctl_finish(q, mp, err, 26115 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 26116 NULL, NULL); 26117 return; 26118 } 26119 26120 ci.ci_ipif = NULL; 26121 switch (ipip->ipi_cmd_type) { 26122 case IF_CMD: 26123 case LIF_CMD: 26124 /* 26125 * ioctls that pass in a [l]ifreq appear here. 26126 * ip_extract_lifreq_cmn returns a refheld ipif in 26127 * ci.ci_ipif 26128 */ 26129 err = ip_extract_lifreq_cmn(q, mp, ipip->ipi_cmd_type, 26130 ipip->ipi_flags, &ci, ip_process_ioctl); 26131 if (err != 0) { 26132 ip_ioctl_finish(q, mp, err, 26133 ipip->ipi_flags & IPI_GET_CMD ? 26134 COPYOUT : NO_COPYOUT, NULL, NULL); 26135 return; 26136 } 26137 ASSERT(ci.ci_ipif != NULL); 26138 break; 26139 26140 case TUN_CMD: 26141 /* 26142 * SIOC[GS]TUNPARAM appear here. ip_extract_tunreq returns 26143 * a refheld ipif in ci.ci_ipif 26144 */ 26145 err = ip_extract_tunreq(q, mp, &ci.ci_ipif, ip_process_ioctl); 26146 if (err != 0) { 26147 ip_ioctl_finish(q, mp, err, 26148 ipip->ipi_flags & IPI_GET_CMD ? 26149 COPYOUT : NO_COPYOUT, NULL, NULL); 26150 return; 26151 } 26152 ASSERT(ci.ci_ipif != NULL); 26153 break; 26154 26155 case MISC_CMD: 26156 /* 26157 * ioctls that neither pass in [l]ifreq or iftun_req come here 26158 * For eg. SIOCGLIFCONF will appear here. 26159 */ 26160 switch (ipip->ipi_cmd) { 26161 case IF_UNITSEL: 26162 /* ioctl comes down the ill */ 26163 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 26164 ipif_refhold(ci.ci_ipif); 26165 break; 26166 case SIOCGMSFILTER: 26167 case SIOCSMSFILTER: 26168 case SIOCGIPMSFILTER: 26169 case SIOCSIPMSFILTER: 26170 err = ip_extract_msfilter(q, mp, &ci.ci_ipif, 26171 ip_process_ioctl); 26172 if (err != 0) { 26173 ip_ioctl_finish(q, mp, err, 26174 ipip->ipi_flags & IPI_GET_CMD ? 26175 COPYOUT : NO_COPYOUT, NULL, NULL); 26176 return; 26177 } 26178 break; 26179 } 26180 err = 0; 26181 ci.ci_sin = NULL; 26182 ci.ci_sin6 = NULL; 26183 ci.ci_lifr = NULL; 26184 break; 26185 } 26186 26187 /* 26188 * If ipsq is non-null, we are already being called exclusively 26189 */ 26190 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 26191 if (!(ipip->ipi_flags & IPI_WR)) { 26192 /* 26193 * A return value of EINPROGRESS means the ioctl is 26194 * either queued and waiting for some reason or has 26195 * already completed. 26196 */ 26197 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 26198 ci.ci_lifr); 26199 if (ci.ci_ipif != NULL) 26200 ipif_refrele(ci.ci_ipif); 26201 ip_ioctl_finish(q, mp, err, 26202 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 26203 NULL, NULL); 26204 return; 26205 } 26206 26207 ASSERT(ci.ci_ipif != NULL); 26208 26209 if (ipsq == NULL) { 26210 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, 26211 ip_process_ioctl, NEW_OP, B_TRUE); 26212 entered_ipsq = B_TRUE; 26213 } 26214 /* 26215 * Release the ipif so that ipif_down and friends that wait for 26216 * references to go away are not misled about the current ipif_refcnt 26217 * values. We are writer so we can access the ipif even after releasing 26218 * the ipif. 26219 */ 26220 ipif_refrele(ci.ci_ipif); 26221 if (ipsq == NULL) 26222 return; 26223 26224 mutex_enter(&ipsq->ipsq_lock); 26225 ASSERT(ipsq->ipsq_current_ipif == NULL); 26226 ipsq->ipsq_current_ipif = ci.ci_ipif; 26227 ipsq->ipsq_last_cmd = ipip->ipi_cmd; 26228 mutex_exit(&ipsq->ipsq_lock); 26229 mutex_enter(&(ci.ci_ipif)->ipif_ill->ill_lock); 26230 /* 26231 * For most set ioctls that come here, this serves as a single point 26232 * where we set the IPIF_CHANGING flag. This ensures that there won't 26233 * be any new references to the ipif. This helps functions that go 26234 * through this path and end up trying to wait for the refcnts 26235 * associated with the ipif to go down to zero. Some exceptions are 26236 * Failover, Failback, and Groupname commands that operate on more than 26237 * just the ci.ci_ipif. These commands internally determine the 26238 * set of ipif's they operate on and set and clear the IPIF_CHANGING 26239 * flags on that set. Another exception is the Removeif command that 26240 * sets the IPIF_CONDEMNED flag internally after identifying the right 26241 * ipif to operate on. 26242 */ 26243 if (ipip->ipi_cmd != SIOCLIFREMOVEIF && 26244 ipip->ipi_cmd != SIOCLIFFAILOVER && 26245 ipip->ipi_cmd != SIOCLIFFAILBACK && 26246 ipip->ipi_cmd != SIOCSLIFGROUPNAME) 26247 (ci.ci_ipif)->ipif_state_flags |= IPIF_CHANGING; 26248 mutex_exit(&(ci.ci_ipif)->ipif_ill->ill_lock); 26249 26250 /* 26251 * A return value of EINPROGRESS means the ioctl is 26252 * either queued and waiting for some reason or has 26253 * already completed. 26254 */ 26255 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 26256 ci.ci_lifr); 26257 26258 /* SIOCLIFREMOVEIF could have removed the ipif */ 26259 ip_ioctl_finish(q, mp, err, 26260 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 26261 ipip->ipi_cmd == SIOCLIFREMOVEIF ? NULL : ci.ci_ipif, ipsq); 26262 26263 if (entered_ipsq) 26264 ipsq_exit(ipsq, B_TRUE, B_TRUE); 26265 } 26266 26267 /* 26268 * Complete the ioctl. Typically ioctls use the mi package and need to 26269 * do mi_copyout/mi_copy_done. 26270 */ 26271 void 26272 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, 26273 ipif_t *ipif, ipsq_t *ipsq) 26274 { 26275 conn_t *connp = NULL; 26276 hook_nic_event_t *info; 26277 26278 if (err == EINPROGRESS) 26279 return; 26280 26281 if (CONN_Q(q)) { 26282 connp = Q_TO_CONN(q); 26283 ASSERT(connp->conn_ref >= 2); 26284 } 26285 26286 switch (mode) { 26287 case COPYOUT: 26288 if (err == 0) 26289 mi_copyout(q, mp); 26290 else 26291 mi_copy_done(q, mp, err); 26292 break; 26293 26294 case NO_COPYOUT: 26295 mi_copy_done(q, mp, err); 26296 break; 26297 26298 default: 26299 /* An ioctl aborted through a conn close would take this path */ 26300 break; 26301 } 26302 26303 /* 26304 * The refhold placed at the start of the ioctl is released here. 26305 */ 26306 if (connp != NULL) 26307 CONN_OPER_PENDING_DONE(connp); 26308 26309 /* 26310 * If the ioctl were an exclusive ioctl it would have set 26311 * IPIF_CHANGING at the start of the ioctl which is undone here. 26312 */ 26313 if (ipif != NULL) { 26314 mutex_enter(&(ipif)->ipif_ill->ill_lock); 26315 ipif->ipif_state_flags &= ~IPIF_CHANGING; 26316 26317 /* 26318 * Unhook the nic event message from the ill and enqueue it into 26319 * the nic event taskq. 26320 */ 26321 if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) { 26322 if (ddi_taskq_dispatch(eventq_queue_nic, 26323 ip_ne_queue_func, (void *)info, DDI_SLEEP) 26324 == DDI_FAILURE) { 26325 ip2dbg(("ip_ioctl_finish: ddi_taskq_dispatch" 26326 "failed\n")); 26327 if (info->hne_data != NULL) 26328 kmem_free(info->hne_data, 26329 info->hne_datalen); 26330 kmem_free(info, sizeof (hook_nic_event_t)); 26331 } 26332 26333 ipif->ipif_ill->ill_nic_event_info = NULL; 26334 } 26335 26336 mutex_exit(&(ipif)->ipif_ill->ill_lock); 26337 } 26338 26339 /* 26340 * Clear the current ipif in the ipsq at the completion of the ioctl. 26341 * Note that a non-null ipsq_current_ipif prevents new ioctls from 26342 * entering the ipsq 26343 */ 26344 if (ipsq != NULL) { 26345 mutex_enter(&ipsq->ipsq_lock); 26346 ipsq->ipsq_current_ipif = NULL; 26347 mutex_exit(&ipsq->ipsq_lock); 26348 } 26349 } 26350 26351 /* 26352 * This is called from ip_wput_nondata to resume a deferred TCP bind. 26353 */ 26354 /* ARGSUSED */ 26355 void 26356 ip_resume_tcp_bind(void *arg, mblk_t *mp, void *arg2) 26357 { 26358 conn_t *connp = arg; 26359 tcp_t *tcp; 26360 26361 ASSERT(connp != NULL && IPCL_IS_TCP(connp) && connp->conn_tcp != NULL); 26362 tcp = connp->conn_tcp; 26363 26364 if (connp->conn_tcp->tcp_state == TCPS_CLOSED) 26365 freemsg(mp); 26366 else 26367 tcp_rput_other(tcp, mp); 26368 CONN_OPER_PENDING_DONE(connp); 26369 } 26370 26371 /* Called from ip_wput for all non data messages */ 26372 /* ARGSUSED */ 26373 void 26374 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 26375 { 26376 mblk_t *mp1; 26377 ire_t *ire, *fake_ire; 26378 ill_t *ill; 26379 struct iocblk *iocp; 26380 ip_ioctl_cmd_t *ipip; 26381 cred_t *cr; 26382 conn_t *connp = NULL; 26383 int cmd, err; 26384 nce_t *nce; 26385 ipif_t *ipif; 26386 26387 if (CONN_Q(q)) 26388 connp = Q_TO_CONN(q); 26389 26390 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 26391 26392 /* Check if it is a queue to /dev/sctp. */ 26393 if (connp != NULL && connp->conn_ulp == IPPROTO_SCTP && 26394 connp->conn_rq == NULL) { 26395 sctp_wput(q, mp); 26396 return; 26397 } 26398 26399 switch (DB_TYPE(mp)) { 26400 case M_IOCTL: 26401 /* 26402 * IOCTL processing begins in ip_sioctl_copyin_setup which 26403 * will arrange to copy in associated control structures. 26404 */ 26405 ip_sioctl_copyin_setup(q, mp); 26406 return; 26407 case M_IOCDATA: 26408 /* 26409 * Ensure that this is associated with one of our trans- 26410 * parent ioctls. If it's not ours, discard it if we're 26411 * running as a driver, or pass it on if we're a module. 26412 */ 26413 iocp = (struct iocblk *)mp->b_rptr; 26414 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26415 if (ipip == NULL) { 26416 if (q->q_next == NULL) { 26417 goto nak; 26418 } else { 26419 putnext(q, mp); 26420 } 26421 return; 26422 } else if ((q->q_next != NULL) && 26423 !(ipip->ipi_flags & IPI_MODOK)) { 26424 /* 26425 * the ioctl is one we recognise, but is not 26426 * consumed by IP as a module, pass M_IOCDATA 26427 * for processing downstream, but only for 26428 * common Streams ioctls. 26429 */ 26430 if (ipip->ipi_flags & IPI_PASS_DOWN) { 26431 putnext(q, mp); 26432 return; 26433 } else { 26434 goto nak; 26435 } 26436 } 26437 26438 /* IOCTL continuation following copyin or copyout. */ 26439 if (mi_copy_state(q, mp, NULL) == -1) { 26440 /* 26441 * The copy operation failed. mi_copy_state already 26442 * cleaned up, so we're out of here. 26443 */ 26444 return; 26445 } 26446 /* 26447 * If we just completed a copy in, we become writer and 26448 * continue processing in ip_sioctl_copyin_done. If it 26449 * was a copy out, we call mi_copyout again. If there is 26450 * nothing more to copy out, it will complete the IOCTL. 26451 */ 26452 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 26453 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 26454 mi_copy_done(q, mp, EPROTO); 26455 return; 26456 } 26457 /* 26458 * Check for cases that need more copying. A return 26459 * value of 0 means a second copyin has been started, 26460 * so we return; a return value of 1 means no more 26461 * copying is needed, so we continue. 26462 */ 26463 cmd = iocp->ioc_cmd; 26464 if ((cmd == SIOCGMSFILTER || cmd == SIOCSMSFILTER || 26465 cmd == SIOCGIPMSFILTER || cmd == SIOCSIPMSFILTER) && 26466 MI_COPY_COUNT(mp) == 1) { 26467 if (ip_copyin_msfilter(q, mp) == 0) 26468 return; 26469 } 26470 /* 26471 * Refhold the conn, till the ioctl completes. This is 26472 * needed in case the ioctl ends up in the pending mp 26473 * list. Every mp in the ill_pending_mp list and 26474 * the ipsq_pending_mp must have a refhold on the conn 26475 * to resume processing. The refhold is released when 26476 * the ioctl completes. (normally or abnormally) 26477 * In all cases ip_ioctl_finish is called to finish 26478 * the ioctl. 26479 */ 26480 if (connp != NULL) { 26481 /* This is not a reentry */ 26482 ASSERT(ipsq == NULL); 26483 CONN_INC_REF(connp); 26484 } else { 26485 if (!(ipip->ipi_flags & IPI_MODOK)) { 26486 mi_copy_done(q, mp, EINVAL); 26487 return; 26488 } 26489 } 26490 26491 ip_process_ioctl(ipsq, q, mp, ipip); 26492 26493 } else { 26494 mi_copyout(q, mp); 26495 } 26496 return; 26497 nak: 26498 iocp->ioc_error = EINVAL; 26499 mp->b_datap->db_type = M_IOCNAK; 26500 iocp->ioc_count = 0; 26501 qreply(q, mp); 26502 return; 26503 26504 case M_IOCNAK: 26505 /* 26506 * The only way we could get here is if a resolver didn't like 26507 * an IOCTL we sent it. This shouldn't happen. 26508 */ 26509 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 26510 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 26511 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 26512 freemsg(mp); 26513 return; 26514 case M_IOCACK: 26515 /* Finish socket ioctls passed through to ARP. */ 26516 ip_sioctl_iocack(q, mp); 26517 return; 26518 case M_FLUSH: 26519 if (*mp->b_rptr & FLUSHW) 26520 flushq(q, FLUSHALL); 26521 if (q->q_next) { 26522 /* 26523 * M_FLUSH is sent up to IP by some drivers during 26524 * unbind. ip_rput has already replied to it. We are 26525 * here for the M_FLUSH that we originated in IP 26526 * before sending the unbind request to the driver. 26527 * Just free it as we don't queue packets in IP 26528 * on the write side of the device instance. 26529 */ 26530 freemsg(mp); 26531 return; 26532 } 26533 if (*mp->b_rptr & FLUSHR) { 26534 *mp->b_rptr &= ~FLUSHW; 26535 qreply(q, mp); 26536 return; 26537 } 26538 freemsg(mp); 26539 return; 26540 case IRE_DB_REQ_TYPE: 26541 /* An Upper Level Protocol wants a copy of an IRE. */ 26542 ip_ire_req(q, mp); 26543 return; 26544 case M_CTL: 26545 if (mp->b_wptr - mp->b_rptr < sizeof (uint32_t)) 26546 break; 26547 26548 if (connp != NULL && *(uint32_t *)mp->b_rptr == 26549 IP_ULP_OUT_LABELED) { 26550 out_labeled_t *olp; 26551 26552 if (mp->b_wptr - mp->b_rptr != sizeof (*olp)) 26553 break; 26554 olp = (out_labeled_t *)mp->b_rptr; 26555 connp->conn_ulp_labeled = olp->out_qnext == q; 26556 freemsg(mp); 26557 return; 26558 } 26559 26560 /* M_CTL messages are used by ARP to tell us things. */ 26561 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 26562 break; 26563 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 26564 case AR_ENTRY_SQUERY: 26565 ip_wput_ctl(q, mp); 26566 return; 26567 case AR_CLIENT_NOTIFY: 26568 ip_arp_news(q, mp); 26569 return; 26570 case AR_DLPIOP_DONE: 26571 ASSERT(q->q_next != NULL); 26572 ill = (ill_t *)q->q_ptr; 26573 /* qwriter_ip releases the refhold */ 26574 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 26575 ill_refhold(ill); 26576 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_done, 26577 CUR_OP, B_FALSE); 26578 return; 26579 case AR_ARP_CLOSING: 26580 /* 26581 * ARP (above us) is closing. If no ARP bringup is 26582 * currently pending, ack the message so that ARP 26583 * can complete its close. Also mark ill_arp_closing 26584 * so that new ARP bringups will fail. If any 26585 * ARP bringup is currently in progress, we will 26586 * ack this when the current ARP bringup completes. 26587 */ 26588 ASSERT(q->q_next != NULL); 26589 ill = (ill_t *)q->q_ptr; 26590 mutex_enter(&ill->ill_lock); 26591 ill->ill_arp_closing = 1; 26592 if (!ill->ill_arp_bringup_pending) { 26593 mutex_exit(&ill->ill_lock); 26594 qreply(q, mp); 26595 } else { 26596 mutex_exit(&ill->ill_lock); 26597 freemsg(mp); 26598 } 26599 return; 26600 case AR_ARP_EXTEND: 26601 /* 26602 * The ARP module above us is capable of duplicate 26603 * address detection. Old ATM drivers will not send 26604 * this message. 26605 */ 26606 ASSERT(q->q_next != NULL); 26607 ill = (ill_t *)q->q_ptr; 26608 ill->ill_arp_extend = B_TRUE; 26609 freemsg(mp); 26610 return; 26611 default: 26612 break; 26613 } 26614 break; 26615 case M_PROTO: 26616 case M_PCPROTO: 26617 /* 26618 * The only PROTO messages we expect are ULP binds and 26619 * copies of option negotiation acknowledgements. 26620 */ 26621 switch (((union T_primitives *)mp->b_rptr)->type) { 26622 case O_T_BIND_REQ: 26623 case T_BIND_REQ: { 26624 /* Request can get queued in bind */ 26625 ASSERT(connp != NULL); 26626 /* 26627 * Both TCP and UDP call ip_bind_{v4,v6}() directly 26628 * instead of going through this path. We only get 26629 * here in the following cases: 26630 * 26631 * a. Bind retries, where ipsq is non-NULL. 26632 * b. T_BIND_REQ is issued from non TCP/UDP 26633 * transport, e.g. icmp for raw socket, 26634 * in which case ipsq will be NULL. 26635 */ 26636 ASSERT(ipsq != NULL || 26637 (!IPCL_IS_TCP(connp) && !IPCL_IS_UDP(connp))); 26638 26639 /* Don't increment refcnt if this is a re-entry */ 26640 if (ipsq == NULL) 26641 CONN_INC_REF(connp); 26642 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 26643 connp, NULL) : ip_bind_v4(q, mp, connp); 26644 if (mp == NULL) 26645 return; 26646 if (IPCL_IS_TCP(connp)) { 26647 /* 26648 * In the case of TCP endpoint we 26649 * come here only for bind retries 26650 */ 26651 ASSERT(ipsq != NULL); 26652 CONN_INC_REF(connp); 26653 squeue_fill(connp->conn_sqp, mp, 26654 ip_resume_tcp_bind, connp, 26655 SQTAG_BIND_RETRY); 26656 return; 26657 } else if (IPCL_IS_UDP(connp)) { 26658 /* 26659 * In the case of UDP endpoint we 26660 * come here only for bind retries 26661 */ 26662 ASSERT(ipsq != NULL); 26663 udp_resume_bind(connp, mp); 26664 return; 26665 } 26666 qreply(q, mp); 26667 CONN_OPER_PENDING_DONE(connp); 26668 return; 26669 } 26670 case T_SVR4_OPTMGMT_REQ: 26671 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 26672 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 26673 26674 ASSERT(connp != NULL); 26675 if (!snmpcom_req(q, mp, ip_snmp_set, 26676 ip_snmp_get, cr)) { 26677 /* 26678 * Call svr4_optcom_req so that it can 26679 * generate the ack. We don't come here 26680 * if this operation is being restarted. 26681 * ip_restart_optmgmt will drop the conn ref. 26682 * In the case of ipsec option after the ipsec 26683 * load is complete conn_restart_ipsec_waiter 26684 * drops the conn ref. 26685 */ 26686 ASSERT(ipsq == NULL); 26687 CONN_INC_REF(connp); 26688 if (ip_check_for_ipsec_opt(q, mp)) 26689 return; 26690 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj); 26691 if (err != EINPROGRESS) { 26692 /* Operation is done */ 26693 CONN_OPER_PENDING_DONE(connp); 26694 } 26695 } 26696 return; 26697 case T_OPTMGMT_REQ: 26698 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 26699 /* 26700 * Note: No snmpcom_req support through new 26701 * T_OPTMGMT_REQ. 26702 * Call tpi_optcom_req so that it can 26703 * generate the ack. 26704 */ 26705 ASSERT(connp != NULL); 26706 ASSERT(ipsq == NULL); 26707 /* 26708 * We don't come here for restart. ip_restart_optmgmt 26709 * will drop the conn ref. In the case of ipsec option 26710 * after the ipsec load is complete 26711 * conn_restart_ipsec_waiter drops the conn ref. 26712 */ 26713 CONN_INC_REF(connp); 26714 if (ip_check_for_ipsec_opt(q, mp)) 26715 return; 26716 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj); 26717 if (err != EINPROGRESS) { 26718 /* Operation is done */ 26719 CONN_OPER_PENDING_DONE(connp); 26720 } 26721 return; 26722 case T_UNBIND_REQ: 26723 mp = ip_unbind(q, mp); 26724 qreply(q, mp); 26725 return; 26726 default: 26727 /* 26728 * Have to drop any DLPI messages coming down from 26729 * arp (such as an info_req which would cause ip 26730 * to receive an extra info_ack if it was passed 26731 * through. 26732 */ 26733 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 26734 (int)*(uint_t *)mp->b_rptr)); 26735 freemsg(mp); 26736 return; 26737 } 26738 /* NOTREACHED */ 26739 case IRE_DB_TYPE: { 26740 nce_t *nce; 26741 ill_t *ill; 26742 in6_addr_t gw_addr_v6; 26743 26744 26745 /* 26746 * This is a response back from a resolver. It 26747 * consists of a message chain containing: 26748 * IRE_MBLK-->LL_HDR_MBLK->pkt 26749 * The IRE_MBLK is the one we allocated in ip_newroute. 26750 * The LL_HDR_MBLK is the DLPI header to use to get 26751 * the attached packet, and subsequent ones for the 26752 * same destination, transmitted. 26753 */ 26754 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 26755 break; 26756 /* 26757 * First, check to make sure the resolution succeeded. 26758 * If it failed, the second mblk will be empty. 26759 * If it is, free the chain, dropping the packet. 26760 * (We must ire_delete the ire; that frees the ire mblk) 26761 * We're doing this now to support PVCs for ATM; it's 26762 * a partial xresolv implementation. When we fully implement 26763 * xresolv interfaces, instead of freeing everything here 26764 * we'll initiate neighbor discovery. 26765 * 26766 * For v4 (ARP and other external resolvers) the resolver 26767 * frees the message, so no check is needed. This check 26768 * is required, though, for a full xresolve implementation. 26769 * Including this code here now both shows how external 26770 * resolvers can NACK a resolution request using an 26771 * existing design that has no specific provisions for NACKs, 26772 * and also takes into account that the current non-ARP 26773 * external resolver has been coded to use this method of 26774 * NACKing for all IPv6 (xresolv) cases, 26775 * whether our xresolv implementation is complete or not. 26776 * 26777 */ 26778 ire = (ire_t *)mp->b_rptr; 26779 ill = ire_to_ill(ire); 26780 mp1 = mp->b_cont; /* dl_unitdata_req */ 26781 if (mp1->b_rptr == mp1->b_wptr) { 26782 if (ire->ire_ipversion == IPV6_VERSION) { 26783 /* 26784 * XRESOLV interface. 26785 */ 26786 ASSERT(ill->ill_flags & ILLF_XRESOLV); 26787 mutex_enter(&ire->ire_lock); 26788 gw_addr_v6 = ire->ire_gateway_addr_v6; 26789 mutex_exit(&ire->ire_lock); 26790 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 26791 nce = ndp_lookup_v6(ill, 26792 &ire->ire_addr_v6, B_FALSE); 26793 } else { 26794 nce = ndp_lookup_v6(ill, &gw_addr_v6, 26795 B_FALSE); 26796 } 26797 if (nce != NULL) { 26798 nce_resolv_failed(nce); 26799 ndp_delete(nce); 26800 NCE_REFRELE(nce); 26801 } 26802 } 26803 mp->b_cont = NULL; 26804 freemsg(mp1); /* frees the pkt as well */ 26805 ASSERT(ire->ire_nce == NULL); 26806 ire_delete((ire_t *)mp->b_rptr); 26807 return; 26808 } 26809 26810 /* 26811 * Split them into IRE_MBLK and pkt and feed it into 26812 * ire_add_then_send. Then in ire_add_then_send 26813 * the IRE will be added, and then the packet will be 26814 * run back through ip_wput. This time it will make 26815 * it to the wire. 26816 */ 26817 mp->b_cont = NULL; 26818 mp = mp1->b_cont; /* now, mp points to pkt */ 26819 mp1->b_cont = NULL; 26820 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 26821 if (ire->ire_ipversion == IPV6_VERSION) { 26822 /* 26823 * XRESOLV interface. Find the nce and put a copy 26824 * of the dl_unitdata_req in nce_res_mp 26825 */ 26826 ASSERT(ill->ill_flags & ILLF_XRESOLV); 26827 mutex_enter(&ire->ire_lock); 26828 gw_addr_v6 = ire->ire_gateway_addr_v6; 26829 mutex_exit(&ire->ire_lock); 26830 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 26831 nce = ndp_lookup_v6(ill, &ire->ire_addr_v6, 26832 B_FALSE); 26833 } else { 26834 nce = ndp_lookup_v6(ill, &gw_addr_v6, B_FALSE); 26835 } 26836 if (nce != NULL) { 26837 /* 26838 * We have to protect nce_res_mp here 26839 * from being accessed by other threads 26840 * while we change the mblk pointer. 26841 * Other functions will also lock the nce when 26842 * accessing nce_res_mp. 26843 * 26844 * The reason we change the mblk pointer 26845 * here rather than copying the resolved address 26846 * into the template is that, unlike with 26847 * ethernet, we have no guarantee that the 26848 * resolved address length will be 26849 * smaller than or equal to the lla length 26850 * with which the template was allocated, 26851 * (for ethernet, they're equal) 26852 * so we have to use the actual resolved 26853 * address mblk - which holds the real 26854 * dl_unitdata_req with the resolved address. 26855 * 26856 * Doing this is the same behavior as was 26857 * previously used in the v4 ARP case. 26858 */ 26859 mutex_enter(&nce->nce_lock); 26860 if (nce->nce_res_mp != NULL) 26861 freemsg(nce->nce_res_mp); 26862 nce->nce_res_mp = mp1; 26863 mutex_exit(&nce->nce_lock); 26864 /* 26865 * We do a fastpath probe here because 26866 * we have resolved the address without 26867 * using Neighbor Discovery. 26868 * In the non-XRESOLV v6 case, the fastpath 26869 * probe is done right after neighbor 26870 * discovery completes. 26871 */ 26872 if (nce->nce_res_mp != NULL) { 26873 int res; 26874 nce_fastpath_list_add(nce); 26875 res = ill_fastpath_probe(ill, 26876 nce->nce_res_mp); 26877 if (res != 0 && res != EAGAIN) 26878 nce_fastpath_list_delete(nce); 26879 } 26880 26881 ire_add_then_send(q, ire, mp); 26882 /* 26883 * Now we have to clean out any packets 26884 * that may have been queued on the nce 26885 * while it was waiting for address resolution 26886 * to complete. 26887 */ 26888 mutex_enter(&nce->nce_lock); 26889 mp1 = nce->nce_qd_mp; 26890 nce->nce_qd_mp = NULL; 26891 mutex_exit(&nce->nce_lock); 26892 while (mp1 != NULL) { 26893 mblk_t *nxt_mp; 26894 queue_t *fwdq = NULL; 26895 ill_t *inbound_ill; 26896 uint_t ifindex; 26897 26898 nxt_mp = mp1->b_next; 26899 mp1->b_next = NULL; 26900 /* 26901 * Retrieve ifindex stored in 26902 * ip_rput_data_v6() 26903 */ 26904 ifindex = 26905 (uint_t)(uintptr_t)mp1->b_prev; 26906 inbound_ill = 26907 ill_lookup_on_ifindex(ifindex, 26908 B_TRUE, NULL, NULL, NULL, 26909 NULL); 26910 mp1->b_prev = NULL; 26911 if (inbound_ill != NULL) 26912 fwdq = inbound_ill->ill_rq; 26913 26914 if (fwdq != NULL) { 26915 put(fwdq, mp1); 26916 ill_refrele(inbound_ill); 26917 } else 26918 put(WR(ill->ill_rq), mp1); 26919 mp1 = nxt_mp; 26920 } 26921 NCE_REFRELE(nce); 26922 } else { /* nce is NULL; clean up */ 26923 ire_delete(ire); 26924 freemsg(mp); 26925 freemsg(mp1); 26926 return; 26927 } 26928 } else { 26929 nce_t *arpce; 26930 /* 26931 * Link layer resolution succeeded. Recompute the 26932 * ire_nce. 26933 */ 26934 ASSERT(ire->ire_type & (IRE_CACHE|IRE_BROADCAST)); 26935 if ((arpce = ndp_lookup_v4(ill, 26936 (ire->ire_gateway_addr != INADDR_ANY ? 26937 &ire->ire_gateway_addr : &ire->ire_addr), 26938 B_FALSE)) == NULL) { 26939 freeb(ire->ire_mp); 26940 freeb(mp1); 26941 freemsg(mp); 26942 return; 26943 } 26944 mutex_enter(&arpce->nce_lock); 26945 arpce->nce_last = TICK_TO_MSEC(lbolt64); 26946 if (arpce->nce_state == ND_REACHABLE) { 26947 /* 26948 * Someone resolved this before us; 26949 * cleanup the res_mp. Since ire has 26950 * not been added yet, the call to ire_add_v4 26951 * from ire_add_then_send (when a dup is 26952 * detected) will clean up the ire. 26953 */ 26954 freeb(mp1); 26955 } else { 26956 if (arpce->nce_res_mp != NULL) 26957 freemsg(arpce->nce_res_mp); 26958 arpce->nce_res_mp = mp1; 26959 arpce->nce_state = ND_REACHABLE; 26960 } 26961 mutex_exit(&arpce->nce_lock); 26962 if (ire->ire_marks & IRE_MARK_NOADD) { 26963 /* 26964 * this ire will not be added to the ire 26965 * cache table, so we can set the ire_nce 26966 * here, as there are no atomicity constraints. 26967 */ 26968 ire->ire_nce = arpce; 26969 /* 26970 * We are associating this nce with the ire 26971 * so change the nce ref taken in 26972 * ndp_lookup_v4() from 26973 * NCE_REFHOLD to NCE_REFHOLD_NOTR 26974 */ 26975 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 26976 } else { 26977 NCE_REFRELE(arpce); 26978 } 26979 ire_add_then_send(q, ire, mp); 26980 } 26981 return; /* All is well, the packet has been sent. */ 26982 } 26983 case IRE_ARPRESOLVE_TYPE: { 26984 26985 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* fake_ire */ 26986 break; 26987 mp1 = mp->b_cont; /* dl_unitdata_req */ 26988 mp->b_cont = NULL; 26989 /* 26990 * First, check to make sure the resolution succeeded. 26991 * If it failed, the second mblk will be empty. 26992 */ 26993 if (mp1->b_rptr == mp1->b_wptr) { 26994 /* cleanup the incomplete ire, free queued packets */ 26995 freemsg(mp); /* fake ire */ 26996 freeb(mp1); /* dl_unitdata response */ 26997 return; 26998 } 26999 27000 /* 27001 * update any incomplete nce_t found. we lookup the ctable 27002 * and find the nce from the ire->ire_nce because we need 27003 * to pass the ire to ip_xmit_v4 later, and can find both 27004 * ire and nce in one lookup from the ctable. 27005 */ 27006 fake_ire = (ire_t *)mp->b_rptr; 27007 /* 27008 * By the time we come back here from ARP 27009 * the logical outgoing interface of the incomplete ire 27010 * we added in ire_forward could have disappeared, 27011 * causing the incomplete ire to also have 27012 * dissapeared. So we need to retreive the 27013 * proper ipif for the ire before looking 27014 * in ctable; do the ctablelookup based on ire_ipif_seqid 27015 */ 27016 ill = q->q_ptr; 27017 27018 /* Get the outgoing ipif */ 27019 mutex_enter(&ill->ill_lock); 27020 if (ill->ill_state_flags & ILL_CONDEMNED) { 27021 mutex_exit(&ill->ill_lock); 27022 freemsg(mp); /* fake ire */ 27023 freeb(mp1); /* dl_unitdata response */ 27024 return; 27025 } 27026 ipif = ipif_lookup_seqid(ill, fake_ire->ire_ipif_seqid); 27027 27028 if (ipif == NULL) { 27029 mutex_exit(&ill->ill_lock); 27030 ip1dbg(("logical intrf to incomplete ire vanished\n")); 27031 freemsg(mp); 27032 freeb(mp1); 27033 return; 27034 } 27035 ipif_refhold_locked(ipif); 27036 mutex_exit(&ill->ill_lock); 27037 ire = ire_ctable_lookup(fake_ire->ire_addr, 27038 fake_ire->ire_gateway_addr, IRE_CACHE, 27039 ipif, fake_ire->ire_zoneid, NULL, 27040 (MATCH_IRE_GW|MATCH_IRE_IPIF|MATCH_IRE_ZONEONLY)); 27041 ipif_refrele(ipif); 27042 if (ire == NULL) { 27043 /* 27044 * no ire was found; check if there is an nce 27045 * for this lookup; if it has no ire's pointing at it 27046 * cleanup. 27047 */ 27048 if ((nce = ndp_lookup_v4(ill, 27049 (fake_ire->ire_gateway_addr != INADDR_ANY ? 27050 &fake_ire->ire_gateway_addr : &fake_ire->ire_addr), 27051 B_FALSE)) != NULL) { 27052 /* 27053 * cleanup: just reset nce. 27054 * We check for refcnt 2 (one for the nce 27055 * hash list + 1 for the ref taken by 27056 * ndp_lookup_v4) to ensure that there are 27057 * no ire's pointing at the nce. 27058 */ 27059 if (nce->nce_refcnt == 2) { 27060 nce = nce_reinit(nce); 27061 } 27062 if (nce != NULL) 27063 NCE_REFRELE(nce); 27064 } 27065 freeb(mp1); /* dl_unitdata response */ 27066 freemsg(mp); /* fake ire */ 27067 return; 27068 } 27069 nce = ire->ire_nce; 27070 DTRACE_PROBE2(ire__arpresolve__type, 27071 ire_t *, ire, nce_t *, nce); 27072 ASSERT(nce->nce_state != ND_INITIAL); 27073 mutex_enter(&nce->nce_lock); 27074 nce->nce_last = TICK_TO_MSEC(lbolt64); 27075 if (nce->nce_state == ND_REACHABLE) { 27076 /* 27077 * Someone resolved this before us; 27078 * our response is not needed any more. 27079 */ 27080 mutex_exit(&nce->nce_lock); 27081 freeb(mp1); /* dl_unitdata response */ 27082 } else { 27083 if (nce->nce_res_mp != NULL) { 27084 freemsg(nce->nce_res_mp); 27085 /* existing dl_unitdata template */ 27086 } 27087 nce->nce_res_mp = mp1; 27088 nce->nce_state = ND_REACHABLE; 27089 mutex_exit(&nce->nce_lock); 27090 ire_fastpath(ire); 27091 } 27092 /* 27093 * The cached nce_t has been updated to be reachable; 27094 * Set the IRE_MARK_UNCACHED flag and free the fake_ire. 27095 */ 27096 fake_ire->ire_marks &= ~IRE_MARK_UNCACHED; 27097 freemsg(mp); 27098 /* 27099 * send out queued packets. 27100 */ 27101 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 27102 27103 IRE_REFRELE(ire); 27104 return; 27105 } 27106 default: 27107 break; 27108 } 27109 if (q->q_next) { 27110 putnext(q, mp); 27111 } else 27112 freemsg(mp); 27113 } 27114 27115 /* 27116 * Process IP options in an outbound packet. Modify the destination if there 27117 * is a source route option. 27118 * Returns non-zero if something fails in which case an ICMP error has been 27119 * sent and mp freed. 27120 */ 27121 static int 27122 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 27123 boolean_t mctl_present, zoneid_t zoneid) 27124 { 27125 ipoptp_t opts; 27126 uchar_t *opt; 27127 uint8_t optval; 27128 uint8_t optlen; 27129 ipaddr_t dst; 27130 intptr_t code = 0; 27131 mblk_t *mp; 27132 ire_t *ire = NULL; 27133 27134 ip2dbg(("ip_wput_options\n")); 27135 mp = ipsec_mp; 27136 if (mctl_present) { 27137 mp = ipsec_mp->b_cont; 27138 } 27139 27140 dst = ipha->ipha_dst; 27141 for (optval = ipoptp_first(&opts, ipha); 27142 optval != IPOPT_EOL; 27143 optval = ipoptp_next(&opts)) { 27144 opt = opts.ipoptp_cur; 27145 optlen = opts.ipoptp_len; 27146 ip2dbg(("ip_wput_options: opt %d, len %d\n", 27147 optval, optlen)); 27148 switch (optval) { 27149 uint32_t off; 27150 case IPOPT_SSRR: 27151 case IPOPT_LSRR: 27152 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27153 ip1dbg(( 27154 "ip_wput_options: bad option offset\n")); 27155 code = (char *)&opt[IPOPT_OLEN] - 27156 (char *)ipha; 27157 goto param_prob; 27158 } 27159 off = opt[IPOPT_OFFSET]; 27160 ip1dbg(("ip_wput_options: next hop 0x%x\n", 27161 ntohl(dst))); 27162 /* 27163 * For strict: verify that dst is directly 27164 * reachable. 27165 */ 27166 if (optval == IPOPT_SSRR) { 27167 ire = ire_ftable_lookup(dst, 0, 0, 27168 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 27169 MBLK_GETLABEL(mp), 27170 MATCH_IRE_TYPE | MATCH_IRE_SECATTR); 27171 if (ire == NULL) { 27172 ip1dbg(("ip_wput_options: SSRR not" 27173 " directly reachable: 0x%x\n", 27174 ntohl(dst))); 27175 goto bad_src_route; 27176 } 27177 ire_refrele(ire); 27178 } 27179 break; 27180 case IPOPT_RR: 27181 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27182 ip1dbg(( 27183 "ip_wput_options: bad option offset\n")); 27184 code = (char *)&opt[IPOPT_OLEN] - 27185 (char *)ipha; 27186 goto param_prob; 27187 } 27188 break; 27189 case IPOPT_TS: 27190 /* 27191 * Verify that length >=5 and that there is either 27192 * room for another timestamp or that the overflow 27193 * counter is not maxed out. 27194 */ 27195 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 27196 if (optlen < IPOPT_MINLEN_IT) { 27197 goto param_prob; 27198 } 27199 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27200 ip1dbg(( 27201 "ip_wput_options: bad option offset\n")); 27202 code = (char *)&opt[IPOPT_OFFSET] - 27203 (char *)ipha; 27204 goto param_prob; 27205 } 27206 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 27207 case IPOPT_TS_TSONLY: 27208 off = IPOPT_TS_TIMELEN; 27209 break; 27210 case IPOPT_TS_TSANDADDR: 27211 case IPOPT_TS_PRESPEC: 27212 case IPOPT_TS_PRESPEC_RFC791: 27213 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 27214 break; 27215 default: 27216 code = (char *)&opt[IPOPT_POS_OV_FLG] - 27217 (char *)ipha; 27218 goto param_prob; 27219 } 27220 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 27221 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 27222 /* 27223 * No room and the overflow counter is 15 27224 * already. 27225 */ 27226 goto param_prob; 27227 } 27228 break; 27229 } 27230 } 27231 27232 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 27233 return (0); 27234 27235 ip1dbg(("ip_wput_options: error processing IP options.")); 27236 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 27237 27238 param_prob: 27239 /* 27240 * Since ip_wput() isn't close to finished, we fill 27241 * in enough of the header for credible error reporting. 27242 */ 27243 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 27244 /* Failed */ 27245 freemsg(ipsec_mp); 27246 return (-1); 27247 } 27248 icmp_param_problem(q, ipsec_mp, (uint8_t)code, zoneid); 27249 return (-1); 27250 27251 bad_src_route: 27252 /* 27253 * Since ip_wput() isn't close to finished, we fill 27254 * in enough of the header for credible error reporting. 27255 */ 27256 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 27257 /* Failed */ 27258 freemsg(ipsec_mp); 27259 return (-1); 27260 } 27261 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid); 27262 return (-1); 27263 } 27264 27265 /* 27266 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 27267 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 27268 * thru /etc/system. 27269 */ 27270 #define CONN_MAXDRAINCNT 64 27271 27272 static void 27273 conn_drain_init(void) 27274 { 27275 int i; 27276 27277 conn_drain_list_cnt = conn_drain_nthreads; 27278 27279 if ((conn_drain_list_cnt == 0) || 27280 (conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 27281 /* 27282 * Default value of the number of drainers is the 27283 * number of cpus, subject to maximum of 8 drainers. 27284 */ 27285 if (boot_max_ncpus != -1) 27286 conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 27287 else 27288 conn_drain_list_cnt = MIN(max_ncpus, 8); 27289 } 27290 27291 conn_drain_list = kmem_zalloc(conn_drain_list_cnt * sizeof (idl_t), 27292 KM_SLEEP); 27293 27294 for (i = 0; i < conn_drain_list_cnt; i++) { 27295 mutex_init(&conn_drain_list[i].idl_lock, NULL, 27296 MUTEX_DEFAULT, NULL); 27297 } 27298 } 27299 27300 static void 27301 conn_drain_fini(void) 27302 { 27303 int i; 27304 27305 for (i = 0; i < conn_drain_list_cnt; i++) 27306 mutex_destroy(&conn_drain_list[i].idl_lock); 27307 kmem_free(conn_drain_list, conn_drain_list_cnt * sizeof (idl_t)); 27308 conn_drain_list = NULL; 27309 } 27310 27311 /* 27312 * Note: For an overview of how flowcontrol is handled in IP please see the 27313 * IP Flowcontrol notes at the top of this file. 27314 * 27315 * Flow control has blocked us from proceeding. Insert the given conn in one 27316 * of the conn drain lists. These conn wq's will be qenabled later on when 27317 * STREAMS flow control does a backenable. conn_walk_drain will enable 27318 * the first conn in each of these drain lists. Each of these qenabled conns 27319 * in turn enables the next in the list, after it runs, or when it closes, 27320 * thus sustaining the drain process. 27321 * 27322 * The only possible calling sequence is ip_wsrv (on conn) -> ip_wput -> 27323 * conn_drain_insert. Thus there can be only 1 instance of conn_drain_insert 27324 * running at any time, on a given conn, since there can be only 1 service proc 27325 * running on a queue at any time. 27326 */ 27327 void 27328 conn_drain_insert(conn_t *connp) 27329 { 27330 idl_t *idl; 27331 uint_t index; 27332 27333 mutex_enter(&connp->conn_lock); 27334 if (connp->conn_state_flags & CONN_CLOSING) { 27335 /* 27336 * The conn is closing as a result of which CONN_CLOSING 27337 * is set. Return. 27338 */ 27339 mutex_exit(&connp->conn_lock); 27340 return; 27341 } else if (connp->conn_idl == NULL) { 27342 /* 27343 * Assign the next drain list round robin. We dont' use 27344 * a lock, and thus it may not be strictly round robin. 27345 * Atomicity of load/stores is enough to make sure that 27346 * conn_drain_list_index is always within bounds. 27347 */ 27348 index = conn_drain_list_index; 27349 ASSERT(index < conn_drain_list_cnt); 27350 connp->conn_idl = &conn_drain_list[index]; 27351 index++; 27352 if (index == conn_drain_list_cnt) 27353 index = 0; 27354 conn_drain_list_index = index; 27355 } 27356 mutex_exit(&connp->conn_lock); 27357 27358 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 27359 if ((connp->conn_drain_prev != NULL) || 27360 (connp->conn_state_flags & CONN_CLOSING)) { 27361 /* 27362 * The conn is already in the drain list, OR 27363 * the conn is closing. We need to check again for 27364 * the closing case again since close can happen 27365 * after we drop the conn_lock, and before we 27366 * acquire the CONN_DRAIN_LIST_LOCK. 27367 */ 27368 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 27369 return; 27370 } else { 27371 idl = connp->conn_idl; 27372 } 27373 27374 /* 27375 * The conn is not in the drain list. Insert it at the 27376 * tail of the drain list. The drain list is circular 27377 * and doubly linked. idl_conn points to the 1st element 27378 * in the list. 27379 */ 27380 if (idl->idl_conn == NULL) { 27381 idl->idl_conn = connp; 27382 connp->conn_drain_next = connp; 27383 connp->conn_drain_prev = connp; 27384 } else { 27385 conn_t *head = idl->idl_conn; 27386 27387 connp->conn_drain_next = head; 27388 connp->conn_drain_prev = head->conn_drain_prev; 27389 head->conn_drain_prev->conn_drain_next = connp; 27390 head->conn_drain_prev = connp; 27391 } 27392 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 27393 } 27394 27395 /* 27396 * This conn is closing, and we are called from ip_close. OR 27397 * This conn has been serviced by ip_wsrv, and we need to do the tail 27398 * processing. 27399 * If this conn is part of the drain list, we may need to sustain the drain 27400 * process by qenabling the next conn in the drain list. We may also need to 27401 * remove this conn from the list, if it is done. 27402 */ 27403 static void 27404 conn_drain_tail(conn_t *connp, boolean_t closing) 27405 { 27406 idl_t *idl; 27407 27408 /* 27409 * connp->conn_idl is stable at this point, and no lock is needed 27410 * to check it. If we are called from ip_close, close has already 27411 * set CONN_CLOSING, thus freezing the value of conn_idl, and 27412 * called us only because conn_idl is non-null. If we are called thru 27413 * service, conn_idl could be null, but it cannot change because 27414 * service is single-threaded per queue, and there cannot be another 27415 * instance of service trying to call conn_drain_insert on this conn 27416 * now. 27417 */ 27418 ASSERT(!closing || (connp->conn_idl != NULL)); 27419 27420 /* 27421 * If connp->conn_idl is null, the conn has not been inserted into any 27422 * drain list even once since creation of the conn. Just return. 27423 */ 27424 if (connp->conn_idl == NULL) 27425 return; 27426 27427 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 27428 27429 if (connp->conn_drain_prev == NULL) { 27430 /* This conn is currently not in the drain list. */ 27431 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 27432 return; 27433 } 27434 idl = connp->conn_idl; 27435 if (idl->idl_conn_draining == connp) { 27436 /* 27437 * This conn is the current drainer. If this is the last conn 27438 * in the drain list, we need to do more checks, in the 'if' 27439 * below. Otherwwise we need to just qenable the next conn, 27440 * to sustain the draining, and is handled in the 'else' 27441 * below. 27442 */ 27443 if (connp->conn_drain_next == idl->idl_conn) { 27444 /* 27445 * This conn is the last in this list. This round 27446 * of draining is complete. If idl_repeat is set, 27447 * it means another flow enabling has happened from 27448 * the driver/streams and we need to another round 27449 * of draining. 27450 * If there are more than 2 conns in the drain list, 27451 * do a left rotate by 1, so that all conns except the 27452 * conn at the head move towards the head by 1, and the 27453 * the conn at the head goes to the tail. This attempts 27454 * a more even share for all queues that are being 27455 * drained. 27456 */ 27457 if ((connp->conn_drain_next != connp) && 27458 (idl->idl_conn->conn_drain_next != connp)) { 27459 idl->idl_conn = idl->idl_conn->conn_drain_next; 27460 } 27461 if (idl->idl_repeat) { 27462 qenable(idl->idl_conn->conn_wq); 27463 idl->idl_conn_draining = idl->idl_conn; 27464 idl->idl_repeat = 0; 27465 } else { 27466 idl->idl_conn_draining = NULL; 27467 } 27468 } else { 27469 /* 27470 * If the next queue that we are now qenable'ing, 27471 * is closing, it will remove itself from this list 27472 * and qenable the subsequent queue in ip_close(). 27473 * Serialization is acheived thru idl_lock. 27474 */ 27475 qenable(connp->conn_drain_next->conn_wq); 27476 idl->idl_conn_draining = connp->conn_drain_next; 27477 } 27478 } 27479 if (!connp->conn_did_putbq || closing) { 27480 /* 27481 * Remove ourself from the drain list, if we did not do 27482 * a putbq, or if the conn is closing. 27483 * Note: It is possible that q->q_first is non-null. It means 27484 * that these messages landed after we did a enableok() in 27485 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 27486 * service them. 27487 */ 27488 if (connp->conn_drain_next == connp) { 27489 /* Singleton in the list */ 27490 ASSERT(connp->conn_drain_prev == connp); 27491 idl->idl_conn = NULL; 27492 idl->idl_conn_draining = NULL; 27493 } else { 27494 connp->conn_drain_prev->conn_drain_next = 27495 connp->conn_drain_next; 27496 connp->conn_drain_next->conn_drain_prev = 27497 connp->conn_drain_prev; 27498 if (idl->idl_conn == connp) 27499 idl->idl_conn = connp->conn_drain_next; 27500 ASSERT(idl->idl_conn_draining != connp); 27501 27502 } 27503 connp->conn_drain_next = NULL; 27504 connp->conn_drain_prev = NULL; 27505 } 27506 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 27507 } 27508 27509 /* 27510 * Write service routine. Shared perimeter entry point. 27511 * ip_wsrv can be called in any of the following ways. 27512 * 1. The device queue's messages has fallen below the low water mark 27513 * and STREAMS has backenabled the ill_wq. We walk thru all the 27514 * the drain lists and backenable the first conn in each list. 27515 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 27516 * qenabled non-tcp upper layers. We start dequeing messages and call 27517 * ip_wput for each message. 27518 */ 27519 27520 void 27521 ip_wsrv(queue_t *q) 27522 { 27523 conn_t *connp; 27524 ill_t *ill; 27525 mblk_t *mp; 27526 27527 if (q->q_next) { 27528 ill = (ill_t *)q->q_ptr; 27529 if (ill->ill_state_flags == 0) { 27530 /* 27531 * The device flow control has opened up. 27532 * Walk through conn drain lists and qenable the 27533 * first conn in each list. This makes sense only 27534 * if the stream is fully plumbed and setup. 27535 * Hence the if check above. 27536 */ 27537 ip1dbg(("ip_wsrv: walking\n")); 27538 conn_walk_drain(); 27539 } 27540 return; 27541 } 27542 27543 connp = Q_TO_CONN(q); 27544 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 27545 27546 /* 27547 * 1. Set conn_draining flag to signal that service is active. 27548 * 27549 * 2. ip_output determines whether it has been called from service, 27550 * based on the last parameter. If it is IP_WSRV it concludes it 27551 * has been called from service. 27552 * 27553 * 3. Message ordering is preserved by the following logic. 27554 * i. A directly called ip_output (i.e. not thru service) will queue 27555 * the message at the tail, if conn_draining is set (i.e. service 27556 * is running) or if q->q_first is non-null. 27557 * 27558 * ii. If ip_output is called from service, and if ip_output cannot 27559 * putnext due to flow control, it does a putbq. 27560 * 27561 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 27562 * (causing an infinite loop). 27563 */ 27564 ASSERT(!connp->conn_did_putbq); 27565 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 27566 connp->conn_draining = 1; 27567 noenable(q); 27568 while ((mp = getq(q)) != NULL) { 27569 ASSERT(CONN_Q(q)); 27570 27571 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 27572 if (connp->conn_did_putbq) { 27573 /* ip_wput did a putbq */ 27574 break; 27575 } 27576 } 27577 /* 27578 * At this point, a thread coming down from top, calling 27579 * ip_wput, may end up queueing the message. We have not yet 27580 * enabled the queue, so ip_wsrv won't be called again. 27581 * To avoid this race, check q->q_first again (in the loop) 27582 * If the other thread queued the message before we call 27583 * enableok(), we will catch it in the q->q_first check. 27584 * If the other thread queues the message after we call 27585 * enableok(), ip_wsrv will be called again by STREAMS. 27586 */ 27587 connp->conn_draining = 0; 27588 enableok(q); 27589 } 27590 27591 /* Enable the next conn for draining */ 27592 conn_drain_tail(connp, B_FALSE); 27593 27594 connp->conn_did_putbq = 0; 27595 } 27596 27597 /* 27598 * Walk the list of all conn's calling the function provided with the 27599 * specified argument for each. Note that this only walks conn's that 27600 * have been bound. 27601 * Applies to both IPv4 and IPv6. 27602 */ 27603 static void 27604 conn_walk_fanout(pfv_t func, void *arg, zoneid_t zoneid) 27605 { 27606 conn_walk_fanout_table(ipcl_udp_fanout, ipcl_udp_fanout_size, 27607 func, arg, zoneid); 27608 conn_walk_fanout_table(ipcl_conn_fanout, ipcl_conn_fanout_size, 27609 func, arg, zoneid); 27610 conn_walk_fanout_table(ipcl_bind_fanout, ipcl_bind_fanout_size, 27611 func, arg, zoneid); 27612 conn_walk_fanout_table(ipcl_proto_fanout, 27613 A_CNT(ipcl_proto_fanout), func, arg, zoneid); 27614 conn_walk_fanout_table(ipcl_proto_fanout_v6, 27615 A_CNT(ipcl_proto_fanout_v6), func, arg, zoneid); 27616 } 27617 27618 /* 27619 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 27620 * of conns that need to be drained, check if drain is already in progress. 27621 * If so set the idl_repeat bit, indicating that the last conn in the list 27622 * needs to reinitiate the drain once again, for the list. If drain is not 27623 * in progress for the list, initiate the draining, by qenabling the 1st 27624 * conn in the list. The drain is self-sustaining, each qenabled conn will 27625 * in turn qenable the next conn, when it is done/blocked/closing. 27626 */ 27627 static void 27628 conn_walk_drain(void) 27629 { 27630 int i; 27631 idl_t *idl; 27632 27633 IP_STAT(ip_conn_walk_drain); 27634 27635 for (i = 0; i < conn_drain_list_cnt; i++) { 27636 idl = &conn_drain_list[i]; 27637 mutex_enter(&idl->idl_lock); 27638 if (idl->idl_conn == NULL) { 27639 mutex_exit(&idl->idl_lock); 27640 continue; 27641 } 27642 /* 27643 * If this list is not being drained currently by 27644 * an ip_wsrv thread, start the process. 27645 */ 27646 if (idl->idl_conn_draining == NULL) { 27647 ASSERT(idl->idl_repeat == 0); 27648 qenable(idl->idl_conn->conn_wq); 27649 idl->idl_conn_draining = idl->idl_conn; 27650 } else { 27651 idl->idl_repeat = 1; 27652 } 27653 mutex_exit(&idl->idl_lock); 27654 } 27655 } 27656 27657 /* 27658 * Walk an conn hash table of `count' buckets, calling func for each entry. 27659 */ 27660 static void 27661 conn_walk_fanout_table(connf_t *connfp, uint_t count, pfv_t func, void *arg, 27662 zoneid_t zoneid) 27663 { 27664 conn_t *connp; 27665 27666 while (count-- > 0) { 27667 mutex_enter(&connfp->connf_lock); 27668 for (connp = connfp->connf_head; connp != NULL; 27669 connp = connp->conn_next) { 27670 if (zoneid == GLOBAL_ZONEID || 27671 zoneid == connp->conn_zoneid) { 27672 CONN_INC_REF(connp); 27673 mutex_exit(&connfp->connf_lock); 27674 (*func)(connp, arg); 27675 mutex_enter(&connfp->connf_lock); 27676 CONN_DEC_REF(connp); 27677 } 27678 } 27679 mutex_exit(&connfp->connf_lock); 27680 connfp++; 27681 } 27682 } 27683 27684 /* ipcl_walk routine invoked for ip_conn_report for each conn. */ 27685 static void 27686 conn_report1(conn_t *connp, void *mp) 27687 { 27688 char buf1[INET6_ADDRSTRLEN]; 27689 char buf2[INET6_ADDRSTRLEN]; 27690 uint_t print_len, buf_len; 27691 27692 ASSERT(connp != NULL); 27693 27694 buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr; 27695 if (buf_len <= 0) 27696 return; 27697 (void) inet_ntop(AF_INET6, &connp->conn_srcv6, buf1, sizeof (buf1)), 27698 (void) inet_ntop(AF_INET6, &connp->conn_remv6, buf2, sizeof (buf2)), 27699 print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len, 27700 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 27701 "%5d %s/%05d %s/%05d\n", 27702 (void *)connp, (void *)CONNP_TO_RQ(connp), 27703 (void *)CONNP_TO_WQ(connp), connp->conn_zoneid, 27704 buf1, connp->conn_lport, 27705 buf2, connp->conn_fport); 27706 if (print_len < buf_len) { 27707 ((mblk_t *)mp)->b_wptr += print_len; 27708 } else { 27709 ((mblk_t *)mp)->b_wptr += buf_len; 27710 } 27711 } 27712 27713 /* 27714 * Named Dispatch routine to produce a formatted report on all conns 27715 * that are listed in one of the fanout tables. 27716 * This report is accessed by using the ndd utility to "get" ND variable 27717 * "ip_conn_status". 27718 */ 27719 /* ARGSUSED */ 27720 static int 27721 ip_conn_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 27722 { 27723 (void) mi_mpprintf(mp, 27724 "CONN " MI_COL_HDRPAD_STR 27725 "rfq " MI_COL_HDRPAD_STR 27726 "stq " MI_COL_HDRPAD_STR 27727 " zone local remote"); 27728 27729 /* 27730 * Because of the ndd constraint, at most we can have 64K buffer 27731 * to put in all conn info. So to be more efficient, just 27732 * allocate a 64K buffer here, assuming we need that large buffer. 27733 * This should be OK as only privileged processes can do ndd /dev/ip. 27734 */ 27735 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 27736 /* The following may work even if we cannot get a large buf. */ 27737 (void) mi_mpprintf(mp, "<< Out of buffer >>\n"); 27738 return (0); 27739 } 27740 27741 conn_walk_fanout(conn_report1, mp->b_cont, Q_TO_CONN(q)->conn_zoneid); 27742 return (0); 27743 } 27744 27745 /* 27746 * Determine if the ill and multicast aspects of that packets 27747 * "matches" the conn. 27748 */ 27749 boolean_t 27750 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 27751 zoneid_t zoneid) 27752 { 27753 ill_t *in_ill; 27754 boolean_t found; 27755 ipif_t *ipif; 27756 ire_t *ire; 27757 ipaddr_t dst, src; 27758 27759 dst = ipha->ipha_dst; 27760 src = ipha->ipha_src; 27761 27762 /* 27763 * conn_incoming_ill is set by IP_BOUND_IF which limits 27764 * unicast, broadcast and multicast reception to 27765 * conn_incoming_ill. conn_wantpacket itself is called 27766 * only for BROADCAST and multicast. 27767 * 27768 * 1) ip_rput supresses duplicate broadcasts if the ill 27769 * is part of a group. Hence, we should be receiving 27770 * just one copy of broadcast for the whole group. 27771 * Thus, if it is part of the group the packet could 27772 * come on any ill of the group and hence we need a 27773 * match on the group. Otherwise, match on ill should 27774 * be sufficient. 27775 * 27776 * 2) ip_rput does not suppress duplicate multicast packets. 27777 * If there are two interfaces in a ill group and we have 27778 * 2 applications (conns) joined a multicast group G on 27779 * both the interfaces, ilm_lookup_ill filter in ip_rput 27780 * will give us two packets because we join G on both the 27781 * interfaces rather than nominating just one interface 27782 * for receiving multicast like broadcast above. So, 27783 * we have to call ilg_lookup_ill to filter out duplicate 27784 * copies, if ill is part of a group. 27785 */ 27786 in_ill = connp->conn_incoming_ill; 27787 if (in_ill != NULL) { 27788 if (in_ill->ill_group == NULL) { 27789 if (in_ill != ill) 27790 return (B_FALSE); 27791 } else if (in_ill->ill_group != ill->ill_group) { 27792 return (B_FALSE); 27793 } 27794 } 27795 27796 if (!CLASSD(dst)) { 27797 if (IPCL_ZONE_MATCH(connp, zoneid)) 27798 return (B_TRUE); 27799 /* 27800 * The conn is in a different zone; we need to check that this 27801 * broadcast address is configured in the application's zone and 27802 * on one ill in the group. 27803 */ 27804 ipif = ipif_get_next_ipif(NULL, ill); 27805 if (ipif == NULL) 27806 return (B_FALSE); 27807 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 27808 connp->conn_zoneid, NULL, 27809 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP)); 27810 ipif_refrele(ipif); 27811 if (ire != NULL) { 27812 ire_refrele(ire); 27813 return (B_TRUE); 27814 } else { 27815 return (B_FALSE); 27816 } 27817 } 27818 27819 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 27820 connp->conn_zoneid == zoneid) { 27821 /* 27822 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 27823 * disabled, therefore we don't dispatch the multicast packet to 27824 * the sending zone. 27825 */ 27826 return (B_FALSE); 27827 } 27828 27829 if ((ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) && 27830 connp->conn_zoneid != zoneid) { 27831 /* 27832 * Multicast packet on the loopback interface: we only match 27833 * conns who joined the group in the specified zone. 27834 */ 27835 return (B_FALSE); 27836 } 27837 27838 if (connp->conn_multi_router) { 27839 /* multicast packet and multicast router socket: send up */ 27840 return (B_TRUE); 27841 } 27842 27843 mutex_enter(&connp->conn_lock); 27844 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 27845 mutex_exit(&connp->conn_lock); 27846 return (found); 27847 } 27848 27849 /* 27850 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 27851 */ 27852 /* ARGSUSED */ 27853 static void 27854 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 27855 { 27856 ill_t *ill = (ill_t *)q->q_ptr; 27857 mblk_t *mp1, *mp2; 27858 ipif_t *ipif; 27859 int err = 0; 27860 conn_t *connp = NULL; 27861 ipsq_t *ipsq; 27862 arc_t *arc; 27863 27864 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 27865 27866 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 27867 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 27868 27869 ASSERT(IAM_WRITER_ILL(ill)); 27870 mp2 = mp->b_cont; 27871 mp->b_cont = NULL; 27872 27873 /* 27874 * We have now received the arp bringup completion message 27875 * from ARP. Mark the arp bringup as done. Also if the arp 27876 * stream has already started closing, send up the AR_ARP_CLOSING 27877 * ack now since ARP is waiting in close for this ack. 27878 */ 27879 mutex_enter(&ill->ill_lock); 27880 ill->ill_arp_bringup_pending = 0; 27881 if (ill->ill_arp_closing) { 27882 mutex_exit(&ill->ill_lock); 27883 /* Let's reuse the mp for sending the ack */ 27884 arc = (arc_t *)mp->b_rptr; 27885 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 27886 arc->arc_cmd = AR_ARP_CLOSING; 27887 qreply(q, mp); 27888 } else { 27889 mutex_exit(&ill->ill_lock); 27890 freeb(mp); 27891 } 27892 27893 /* We should have an IOCTL waiting on this. */ 27894 ipsq = ill->ill_phyint->phyint_ipsq; 27895 ipif = ipsq->ipsq_pending_ipif; 27896 mp1 = ipsq_pending_mp_get(ipsq, &connp); 27897 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 27898 if (mp1 == NULL) { 27899 /* bringup was aborted by the user */ 27900 freemsg(mp2); 27901 return; 27902 } 27903 ASSERT(connp != NULL); 27904 q = CONNP_TO_WQ(connp); 27905 /* 27906 * If the DL_BIND_REQ fails, it is noted 27907 * in arc_name_offset. 27908 */ 27909 err = *((int *)mp2->b_rptr); 27910 if (err == 0) { 27911 if (ipif->ipif_isv6) { 27912 if ((err = ipif_up_done_v6(ipif)) != 0) 27913 ip0dbg(("ip_arp_done: init failed\n")); 27914 } else { 27915 if ((err = ipif_up_done(ipif)) != 0) 27916 ip0dbg(("ip_arp_done: init failed\n")); 27917 } 27918 } else { 27919 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 27920 } 27921 27922 freemsg(mp2); 27923 27924 if ((err == 0) && (ill->ill_up_ipifs)) { 27925 err = ill_up_ipifs(ill, q, mp1); 27926 if (err == EINPROGRESS) 27927 return; 27928 } 27929 27930 if (ill->ill_up_ipifs) { 27931 ill_group_cleanup(ill); 27932 } 27933 27934 /* 27935 * The ioctl must complete now without EINPROGRESS 27936 * since ipsq_pending_mp_get has removed the ioctl mblk 27937 * from ipsq_pending_mp. Otherwise the ioctl will be 27938 * stuck for ever in the ipsq. 27939 */ 27940 ASSERT(err != EINPROGRESS); 27941 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipif, ipsq); 27942 } 27943 27944 /* Allocate the private structure */ 27945 static int 27946 ip_priv_alloc(void **bufp) 27947 { 27948 void *buf; 27949 27950 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 27951 return (ENOMEM); 27952 27953 *bufp = buf; 27954 return (0); 27955 } 27956 27957 /* Function to delete the private structure */ 27958 void 27959 ip_priv_free(void *buf) 27960 { 27961 ASSERT(buf != NULL); 27962 kmem_free(buf, sizeof (ip_priv_t)); 27963 } 27964 27965 /* 27966 * The entry point for IPPF processing. 27967 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 27968 * routine just returns. 27969 * 27970 * When called, ip_process generates an ipp_packet_t structure 27971 * which holds the state information for this packet and invokes the 27972 * the classifier (via ipp_packet_process). The classification, depending on 27973 * configured filters, results in a list of actions for this packet. Invoking 27974 * an action may cause the packet to be dropped, in which case the resulting 27975 * mblk (*mpp) is NULL. proc indicates the callout position for 27976 * this packet and ill_index is the interface this packet on or will leave 27977 * on (inbound and outbound resp.). 27978 */ 27979 void 27980 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 27981 { 27982 mblk_t *mp; 27983 ip_priv_t *priv; 27984 ipp_action_id_t aid; 27985 int rc = 0; 27986 ipp_packet_t *pp; 27987 #define IP_CLASS "ip" 27988 27989 /* If the classifier is not loaded, return */ 27990 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 27991 return; 27992 } 27993 27994 mp = *mpp; 27995 ASSERT(mp != NULL); 27996 27997 /* Allocate the packet structure */ 27998 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 27999 if (rc != 0) { 28000 *mpp = NULL; 28001 freemsg(mp); 28002 return; 28003 } 28004 28005 /* Allocate the private structure */ 28006 rc = ip_priv_alloc((void **)&priv); 28007 if (rc != 0) { 28008 *mpp = NULL; 28009 freemsg(mp); 28010 ipp_packet_free(pp); 28011 return; 28012 } 28013 priv->proc = proc; 28014 priv->ill_index = ill_index; 28015 ipp_packet_set_private(pp, priv, ip_priv_free); 28016 ipp_packet_set_data(pp, mp); 28017 28018 /* Invoke the classifier */ 28019 rc = ipp_packet_process(&pp); 28020 if (pp != NULL) { 28021 mp = ipp_packet_get_data(pp); 28022 ipp_packet_free(pp); 28023 if (rc != 0) { 28024 freemsg(mp); 28025 *mpp = NULL; 28026 } 28027 } else { 28028 *mpp = NULL; 28029 } 28030 #undef IP_CLASS 28031 } 28032 28033 /* 28034 * Propagate a multicast group membership operation (add/drop) on 28035 * all the interfaces crossed by the related multirt routes. 28036 * The call is considered successful if the operation succeeds 28037 * on at least one interface. 28038 */ 28039 static int 28040 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 28041 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 28042 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 28043 mblk_t *first_mp) 28044 { 28045 ire_t *ire_gw; 28046 irb_t *irb; 28047 int error = 0; 28048 opt_restart_t *or; 28049 28050 irb = ire->ire_bucket; 28051 ASSERT(irb != NULL); 28052 28053 ASSERT(DB_TYPE(first_mp) == M_CTL); 28054 28055 or = (opt_restart_t *)first_mp->b_rptr; 28056 IRB_REFHOLD(irb); 28057 for (; ire != NULL; ire = ire->ire_next) { 28058 if ((ire->ire_flags & RTF_MULTIRT) == 0) 28059 continue; 28060 if (ire->ire_addr != group) 28061 continue; 28062 28063 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 28064 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 28065 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE); 28066 /* No resolver exists for the gateway; skip this ire. */ 28067 if (ire_gw == NULL) 28068 continue; 28069 28070 /* 28071 * This function can return EINPROGRESS. If so the operation 28072 * will be restarted from ip_restart_optmgmt which will 28073 * call ip_opt_set and option processing will restart for 28074 * this option. So we may end up calling 'fn' more than once. 28075 * This requires that 'fn' is idempotent except for the 28076 * return value. The operation is considered a success if 28077 * it succeeds at least once on any one interface. 28078 */ 28079 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 28080 NULL, fmode, src, first_mp); 28081 if (error == 0) 28082 or->or_private = CGTP_MCAST_SUCCESS; 28083 28084 if (ip_debug > 0) { 28085 ulong_t off; 28086 char *ksym; 28087 ksym = kobj_getsymname((uintptr_t)fn, &off); 28088 ip2dbg(("ip_multirt_apply_membership: " 28089 "called %s, multirt group 0x%08x via itf 0x%08x, " 28090 "error %d [success %u]\n", 28091 ksym ? ksym : "?", 28092 ntohl(group), ntohl(ire_gw->ire_src_addr), 28093 error, or->or_private)); 28094 } 28095 28096 ire_refrele(ire_gw); 28097 if (error == EINPROGRESS) { 28098 IRB_REFRELE(irb); 28099 return (error); 28100 } 28101 } 28102 IRB_REFRELE(irb); 28103 /* 28104 * Consider the call as successful if we succeeded on at least 28105 * one interface. Otherwise, return the last encountered error. 28106 */ 28107 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 28108 } 28109 28110 28111 /* 28112 * Issue a warning regarding a route crossing an interface with an 28113 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 28114 * amount of time is logged. 28115 */ 28116 static void 28117 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 28118 { 28119 hrtime_t current = gethrtime(); 28120 char buf[INET_ADDRSTRLEN]; 28121 28122 /* Convert interval in ms to hrtime in ns */ 28123 if (multirt_bad_mtu_last_time + 28124 ((hrtime_t)ip_multirt_log_interval * (hrtime_t)1000000) <= 28125 current) { 28126 cmn_err(CE_WARN, "ip: ignoring multiroute " 28127 "to %s, incorrect MTU %u (expected %u)\n", 28128 ip_dot_addr(ire->ire_addr, buf), 28129 ire->ire_max_frag, max_frag); 28130 28131 multirt_bad_mtu_last_time = current; 28132 } 28133 } 28134 28135 28136 /* 28137 * Get the CGTP (multirouting) filtering status. 28138 * If 0, the CGTP hooks are transparent. 28139 */ 28140 /* ARGSUSED */ 28141 static int 28142 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 28143 { 28144 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28145 28146 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 28147 return (0); 28148 } 28149 28150 28151 /* 28152 * Set the CGTP (multirouting) filtering status. 28153 * If the status is changed from active to transparent 28154 * or from transparent to active, forward the new status 28155 * to the filtering module (if loaded). 28156 */ 28157 /* ARGSUSED */ 28158 static int 28159 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 28160 cred_t *ioc_cr) 28161 { 28162 long new_value; 28163 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28164 28165 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 28166 new_value < 0 || new_value > 1) { 28167 return (EINVAL); 28168 } 28169 28170 /* 28171 * Do not enable CGTP filtering - thus preventing the hooks 28172 * from being invoked - if the version number of the 28173 * filtering module hooks does not match. 28174 */ 28175 if ((ip_cgtp_filter_ops != NULL) && 28176 (ip_cgtp_filter_ops->cfo_filter_rev != CGTP_FILTER_REV)) { 28177 cmn_err(CE_WARN, "IP: CGTP filtering version mismatch " 28178 "(module hooks version %d, expecting %d)\n", 28179 ip_cgtp_filter_ops->cfo_filter_rev, CGTP_FILTER_REV); 28180 return (ENOTSUP); 28181 } 28182 28183 if ((!*ip_cgtp_filter_value) && new_value) { 28184 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 28185 ip_cgtp_filter_ops == NULL ? 28186 " (module not loaded)" : ""); 28187 } 28188 if (*ip_cgtp_filter_value && (!new_value)) { 28189 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 28190 ip_cgtp_filter_ops == NULL ? 28191 " (module not loaded)" : ""); 28192 } 28193 28194 if (ip_cgtp_filter_ops != NULL) { 28195 int res; 28196 if ((res = ip_cgtp_filter_ops->cfo_change_state(new_value))) { 28197 return (res); 28198 } 28199 } 28200 28201 *ip_cgtp_filter_value = (boolean_t)new_value; 28202 28203 return (0); 28204 } 28205 28206 28207 /* 28208 * Return the expected CGTP hooks version number. 28209 */ 28210 int 28211 ip_cgtp_filter_supported(void) 28212 { 28213 return (ip_cgtp_filter_rev); 28214 } 28215 28216 28217 /* 28218 * CGTP hooks can be registered by directly touching ip_cgtp_filter_ops 28219 * or by invoking this function. In the first case, the version number 28220 * of the registered structure is checked at hooks activation time 28221 * in ip_cgtp_filter_set(). 28222 */ 28223 int 28224 ip_cgtp_filter_register(cgtp_filter_ops_t *ops) 28225 { 28226 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 28227 return (ENOTSUP); 28228 28229 ip_cgtp_filter_ops = ops; 28230 return (0); 28231 } 28232 28233 static squeue_func_t 28234 ip_squeue_switch(int val) 28235 { 28236 squeue_func_t rval = squeue_fill; 28237 28238 switch (val) { 28239 case IP_SQUEUE_ENTER_NODRAIN: 28240 rval = squeue_enter_nodrain; 28241 break; 28242 case IP_SQUEUE_ENTER: 28243 rval = squeue_enter; 28244 break; 28245 default: 28246 break; 28247 } 28248 return (rval); 28249 } 28250 28251 /* ARGSUSED */ 28252 static int 28253 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 28254 caddr_t addr, cred_t *cr) 28255 { 28256 int *v = (int *)addr; 28257 long new_value; 28258 28259 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 28260 return (EINVAL); 28261 28262 ip_input_proc = ip_squeue_switch(new_value); 28263 *v = new_value; 28264 return (0); 28265 } 28266 28267 /* ARGSUSED */ 28268 static int 28269 ip_int_set(queue_t *q, mblk_t *mp, char *value, 28270 caddr_t addr, cred_t *cr) 28271 { 28272 int *v = (int *)addr; 28273 long new_value; 28274 28275 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 28276 return (EINVAL); 28277 28278 *v = new_value; 28279 return (0); 28280 } 28281 28282 static void 28283 ip_kstat_init(void) 28284 { 28285 ip_named_kstat_t template = { 28286 { "forwarding", KSTAT_DATA_UINT32, 0 }, 28287 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 28288 { "inReceives", KSTAT_DATA_UINT32, 0 }, 28289 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 28290 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 28291 { "forwDatagrams", KSTAT_DATA_UINT32, 0 }, 28292 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 28293 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 28294 { "inDelivers", KSTAT_DATA_UINT32, 0 }, 28295 { "outRequests", KSTAT_DATA_UINT32, 0 }, 28296 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 28297 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 28298 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 28299 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 28300 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 28301 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 28302 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 28303 { "fragFails", KSTAT_DATA_UINT32, 0 }, 28304 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 28305 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 28306 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 28307 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 28308 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 28309 { "inErrs", KSTAT_DATA_UINT32, 0 }, 28310 { "noPorts", KSTAT_DATA_UINT32, 0 }, 28311 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 28312 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 28313 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 28314 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 28315 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 28316 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 28317 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 28318 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 28319 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 28320 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 28321 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 28322 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 28323 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 28324 }; 28325 28326 ip_mibkp = kstat_create("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 28327 NUM_OF_FIELDS(ip_named_kstat_t), 28328 0); 28329 if (!ip_mibkp) 28330 return; 28331 28332 template.forwarding.value.ui32 = WE_ARE_FORWARDING ? 1:2; 28333 template.defaultTTL.value.ui32 = (uint32_t)ip_def_ttl; 28334 template.reasmTimeout.value.ui32 = ip_g_frag_timeout; 28335 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 28336 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 28337 28338 template.netToMediaEntrySize.value.i32 = 28339 sizeof (mib2_ipNetToMediaEntry_t); 28340 28341 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 28342 28343 bcopy(&template, ip_mibkp->ks_data, sizeof (template)); 28344 28345 ip_mibkp->ks_update = ip_kstat_update; 28346 28347 kstat_install(ip_mibkp); 28348 } 28349 28350 static void 28351 ip_kstat_fini(void) 28352 { 28353 28354 if (ip_mibkp != NULL) { 28355 kstat_delete(ip_mibkp); 28356 ip_mibkp = NULL; 28357 } 28358 } 28359 28360 static int 28361 ip_kstat_update(kstat_t *kp, int rw) 28362 { 28363 ip_named_kstat_t *ipkp; 28364 28365 if (!kp || !kp->ks_data) 28366 return (EIO); 28367 28368 if (rw == KSTAT_WRITE) 28369 return (EACCES); 28370 28371 ipkp = (ip_named_kstat_t *)kp->ks_data; 28372 28373 ipkp->forwarding.value.ui32 = ip_mib.ipForwarding; 28374 ipkp->defaultTTL.value.ui32 = ip_mib.ipDefaultTTL; 28375 ipkp->inReceives.value.ui32 = ip_mib.ipInReceives; 28376 ipkp->inHdrErrors.value.ui32 = ip_mib.ipInHdrErrors; 28377 ipkp->inAddrErrors.value.ui32 = ip_mib.ipInAddrErrors; 28378 ipkp->forwDatagrams.value.ui32 = ip_mib.ipForwDatagrams; 28379 ipkp->inUnknownProtos.value.ui32 = ip_mib.ipInUnknownProtos; 28380 ipkp->inDiscards.value.ui32 = ip_mib.ipInDiscards; 28381 ipkp->inDelivers.value.ui32 = ip_mib.ipInDelivers; 28382 ipkp->outRequests.value.ui32 = ip_mib.ipOutRequests; 28383 ipkp->outDiscards.value.ui32 = ip_mib.ipOutDiscards; 28384 ipkp->outNoRoutes.value.ui32 = ip_mib.ipOutNoRoutes; 28385 ipkp->reasmTimeout.value.ui32 = ip_mib.ipReasmTimeout; 28386 ipkp->reasmReqds.value.ui32 = ip_mib.ipReasmReqds; 28387 ipkp->reasmOKs.value.ui32 = ip_mib.ipReasmOKs; 28388 ipkp->reasmFails.value.ui32 = ip_mib.ipReasmFails; 28389 ipkp->fragOKs.value.ui32 = ip_mib.ipFragOKs; 28390 ipkp->fragFails.value.ui32 = ip_mib.ipFragFails; 28391 ipkp->fragCreates.value.ui32 = ip_mib.ipFragCreates; 28392 28393 ipkp->routingDiscards.value.ui32 = ip_mib.ipRoutingDiscards; 28394 ipkp->inErrs.value.ui32 = ip_mib.tcpInErrs; 28395 ipkp->noPorts.value.ui32 = ip_mib.udpNoPorts; 28396 ipkp->inCksumErrs.value.ui32 = ip_mib.ipInCksumErrs; 28397 ipkp->reasmDuplicates.value.ui32 = ip_mib.ipReasmDuplicates; 28398 ipkp->reasmPartDups.value.ui32 = ip_mib.ipReasmPartDups; 28399 ipkp->forwProhibits.value.ui32 = ip_mib.ipForwProhibits; 28400 ipkp->udpInCksumErrs.value.ui32 = ip_mib.udpInCksumErrs; 28401 ipkp->udpInOverflows.value.ui32 = ip_mib.udpInOverflows; 28402 ipkp->rawipInOverflows.value.ui32 = ip_mib.rawipInOverflows; 28403 ipkp->ipsecInSucceeded.value.ui32 = ip_mib.ipsecInSucceeded; 28404 ipkp->ipsecInFailed.value.i32 = ip_mib.ipsecInFailed; 28405 28406 ipkp->inIPv6.value.ui32 = ip_mib.ipInIPv6; 28407 ipkp->outIPv6.value.ui32 = ip_mib.ipOutIPv6; 28408 ipkp->outSwitchIPv6.value.ui32 = ip_mib.ipOutSwitchIPv6; 28409 28410 return (0); 28411 } 28412 28413 static void 28414 icmp_kstat_init(void) 28415 { 28416 icmp_named_kstat_t template = { 28417 { "inMsgs", KSTAT_DATA_UINT32 }, 28418 { "inErrors", KSTAT_DATA_UINT32 }, 28419 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 28420 { "inTimeExcds", KSTAT_DATA_UINT32 }, 28421 { "inParmProbs", KSTAT_DATA_UINT32 }, 28422 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 28423 { "inRedirects", KSTAT_DATA_UINT32 }, 28424 { "inEchos", KSTAT_DATA_UINT32 }, 28425 { "inEchoReps", KSTAT_DATA_UINT32 }, 28426 { "inTimestamps", KSTAT_DATA_UINT32 }, 28427 { "inTimestampReps", KSTAT_DATA_UINT32 }, 28428 { "inAddrMasks", KSTAT_DATA_UINT32 }, 28429 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 28430 { "outMsgs", KSTAT_DATA_UINT32 }, 28431 { "outErrors", KSTAT_DATA_UINT32 }, 28432 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 28433 { "outTimeExcds", KSTAT_DATA_UINT32 }, 28434 { "outParmProbs", KSTAT_DATA_UINT32 }, 28435 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 28436 { "outRedirects", KSTAT_DATA_UINT32 }, 28437 { "outEchos", KSTAT_DATA_UINT32 }, 28438 { "outEchoReps", KSTAT_DATA_UINT32 }, 28439 { "outTimestamps", KSTAT_DATA_UINT32 }, 28440 { "outTimestampReps", KSTAT_DATA_UINT32 }, 28441 { "outAddrMasks", KSTAT_DATA_UINT32 }, 28442 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 28443 { "inChksumErrs", KSTAT_DATA_UINT32 }, 28444 { "inUnknowns", KSTAT_DATA_UINT32 }, 28445 { "inFragNeeded", KSTAT_DATA_UINT32 }, 28446 { "outFragNeeded", KSTAT_DATA_UINT32 }, 28447 { "outDrops", KSTAT_DATA_UINT32 }, 28448 { "inOverFlows", KSTAT_DATA_UINT32 }, 28449 { "inBadRedirects", KSTAT_DATA_UINT32 }, 28450 }; 28451 28452 icmp_mibkp = kstat_create("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 28453 NUM_OF_FIELDS(icmp_named_kstat_t), 28454 0); 28455 if (icmp_mibkp == NULL) 28456 return; 28457 28458 bcopy(&template, icmp_mibkp->ks_data, sizeof (template)); 28459 28460 icmp_mibkp->ks_update = icmp_kstat_update; 28461 28462 kstat_install(icmp_mibkp); 28463 } 28464 28465 static void 28466 icmp_kstat_fini(void) 28467 { 28468 28469 if (icmp_mibkp != NULL) { 28470 kstat_delete(icmp_mibkp); 28471 icmp_mibkp = NULL; 28472 } 28473 } 28474 28475 static int 28476 icmp_kstat_update(kstat_t *kp, int rw) 28477 { 28478 icmp_named_kstat_t *icmpkp; 28479 28480 if ((kp == NULL) || (kp->ks_data == NULL)) 28481 return (EIO); 28482 28483 if (rw == KSTAT_WRITE) 28484 return (EACCES); 28485 28486 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 28487 28488 icmpkp->inMsgs.value.ui32 = icmp_mib.icmpInMsgs; 28489 icmpkp->inErrors.value.ui32 = icmp_mib.icmpInErrors; 28490 icmpkp->inDestUnreachs.value.ui32 = icmp_mib.icmpInDestUnreachs; 28491 icmpkp->inTimeExcds.value.ui32 = icmp_mib.icmpInTimeExcds; 28492 icmpkp->inParmProbs.value.ui32 = icmp_mib.icmpInParmProbs; 28493 icmpkp->inSrcQuenchs.value.ui32 = icmp_mib.icmpInSrcQuenchs; 28494 icmpkp->inRedirects.value.ui32 = icmp_mib.icmpInRedirects; 28495 icmpkp->inEchos.value.ui32 = icmp_mib.icmpInEchos; 28496 icmpkp->inEchoReps.value.ui32 = icmp_mib.icmpInEchoReps; 28497 icmpkp->inTimestamps.value.ui32 = icmp_mib.icmpInTimestamps; 28498 icmpkp->inTimestampReps.value.ui32 = icmp_mib.icmpInTimestampReps; 28499 icmpkp->inAddrMasks.value.ui32 = icmp_mib.icmpInAddrMasks; 28500 icmpkp->inAddrMaskReps.value.ui32 = icmp_mib.icmpInAddrMaskReps; 28501 icmpkp->outMsgs.value.ui32 = icmp_mib.icmpOutMsgs; 28502 icmpkp->outErrors.value.ui32 = icmp_mib.icmpOutErrors; 28503 icmpkp->outDestUnreachs.value.ui32 = icmp_mib.icmpOutDestUnreachs; 28504 icmpkp->outTimeExcds.value.ui32 = icmp_mib.icmpOutTimeExcds; 28505 icmpkp->outParmProbs.value.ui32 = icmp_mib.icmpOutParmProbs; 28506 icmpkp->outSrcQuenchs.value.ui32 = icmp_mib.icmpOutSrcQuenchs; 28507 icmpkp->outRedirects.value.ui32 = icmp_mib.icmpOutRedirects; 28508 icmpkp->outEchos.value.ui32 = icmp_mib.icmpOutEchos; 28509 icmpkp->outEchoReps.value.ui32 = icmp_mib.icmpOutEchoReps; 28510 icmpkp->outTimestamps.value.ui32 = icmp_mib.icmpOutTimestamps; 28511 icmpkp->outTimestampReps.value.ui32 = icmp_mib.icmpOutTimestampReps; 28512 icmpkp->outAddrMasks.value.ui32 = icmp_mib.icmpOutAddrMasks; 28513 icmpkp->outAddrMaskReps.value.ui32 = icmp_mib.icmpOutAddrMaskReps; 28514 icmpkp->inCksumErrs.value.ui32 = icmp_mib.icmpInCksumErrs; 28515 icmpkp->inUnknowns.value.ui32 = icmp_mib.icmpInUnknowns; 28516 icmpkp->inFragNeeded.value.ui32 = icmp_mib.icmpInFragNeeded; 28517 icmpkp->outFragNeeded.value.ui32 = icmp_mib.icmpOutFragNeeded; 28518 icmpkp->outDrops.value.ui32 = icmp_mib.icmpOutDrops; 28519 icmpkp->inOverflows.value.ui32 = icmp_mib.icmpInOverflows; 28520 icmpkp->inBadRedirects.value.ui32 = icmp_mib.icmpInBadRedirects; 28521 28522 return (0); 28523 } 28524 28525 /* 28526 * This is the fanout function for raw socket opened for SCTP. Note 28527 * that it is called after SCTP checks that there is no socket which 28528 * wants a packet. Then before SCTP handles this out of the blue packet, 28529 * this function is called to see if there is any raw socket for SCTP. 28530 * If there is and it is bound to the correct address, the packet will 28531 * be sent to that socket. Note that only one raw socket can be bound to 28532 * a port. This is assured in ipcl_sctp_hash_insert(); 28533 */ 28534 void 28535 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 28536 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 28537 uint_t ipif_seqid, zoneid_t zoneid) 28538 { 28539 conn_t *connp; 28540 queue_t *rq; 28541 mblk_t *first_mp; 28542 boolean_t secure; 28543 ip6_t *ip6h; 28544 28545 first_mp = mp; 28546 if (mctl_present) { 28547 mp = first_mp->b_cont; 28548 secure = ipsec_in_is_secure(first_mp); 28549 ASSERT(mp != NULL); 28550 } else { 28551 secure = B_FALSE; 28552 } 28553 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 28554 28555 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha); 28556 if (connp == NULL) { 28557 sctp_ootb_input(first_mp, recv_ill, ipif_seqid, zoneid, 28558 mctl_present); 28559 return; 28560 } 28561 rq = connp->conn_rq; 28562 if (!canputnext(rq)) { 28563 CONN_DEC_REF(connp); 28564 BUMP_MIB(&ip_mib, rawipInOverflows); 28565 freemsg(first_mp); 28566 return; 28567 } 28568 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp) : 28569 CONN_INBOUND_POLICY_PRESENT_V6(connp)) || secure) { 28570 first_mp = ipsec_check_inbound_policy(first_mp, connp, 28571 (isv4 ? ipha : NULL), ip6h, mctl_present); 28572 if (first_mp == NULL) { 28573 CONN_DEC_REF(connp); 28574 return; 28575 } 28576 } 28577 /* 28578 * We probably should not send M_CTL message up to 28579 * raw socket. 28580 */ 28581 if (mctl_present) 28582 freeb(first_mp); 28583 28584 /* Initiate IPPF processing here if needed. */ 28585 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) || 28586 (!isv4 && IP6_IN_IPP(flags))) { 28587 ip_process(IPP_LOCAL_IN, &mp, 28588 recv_ill->ill_phyint->phyint_ifindex); 28589 if (mp == NULL) { 28590 CONN_DEC_REF(connp); 28591 return; 28592 } 28593 } 28594 28595 if (connp->conn_recvif || connp->conn_recvslla || 28596 ((connp->conn_ipv6_recvpktinfo || 28597 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 28598 (flags & IP_FF_IP6INFO))) { 28599 int in_flags = 0; 28600 28601 if (connp->conn_recvif || connp->conn_ipv6_recvpktinfo) { 28602 in_flags = IPF_RECVIF; 28603 } 28604 if (connp->conn_recvslla) { 28605 in_flags |= IPF_RECVSLLA; 28606 } 28607 if (isv4) { 28608 mp = ip_add_info(mp, recv_ill, in_flags); 28609 } else { 28610 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 28611 if (mp == NULL) { 28612 CONN_DEC_REF(connp); 28613 return; 28614 } 28615 } 28616 } 28617 28618 BUMP_MIB(&ip_mib, ipInDelivers); 28619 /* 28620 * We are sending the IPSEC_IN message also up. Refer 28621 * to comments above this function. 28622 */ 28623 putnext(rq, mp); 28624 CONN_DEC_REF(connp); 28625 } 28626 28627 /* 28628 * This function should be called only if all packet processing 28629 * including fragmentation is complete. Callers of this function 28630 * must set mp->b_prev to one of these values: 28631 * {0, IPP_FWD_OUT, IPP_LOCAL_OUT} 28632 * prior to handing over the mp as first argument to this function. 28633 * 28634 * If the ire passed by caller is incomplete, this function 28635 * queues the packet and if necessary, sends ARP request and bails. 28636 * If the ire passed is fully resolved, we simply prepend 28637 * the link-layer header to the packet, do ipsec hw acceleration 28638 * work if necessary, and send the packet out on the wire. 28639 * 28640 * NOTE: IPSEC will only call this function with fully resolved 28641 * ires if hw acceleration is involved. 28642 * TODO list : 28643 * a Handle M_MULTIDATA so that 28644 * tcp_multisend->tcp_multisend_data can 28645 * call ip_xmit_v4 directly 28646 * b Handle post-ARP work for fragments so that 28647 * ip_wput_frag can call this function. 28648 */ 28649 ipxmit_state_t 28650 ip_xmit_v4(mblk_t *mp, ire_t *ire, ipsec_out_t *io, boolean_t flow_ctl_enabled) 28651 { 28652 nce_t *arpce; 28653 queue_t *q; 28654 int ill_index; 28655 mblk_t *nxt_mp, *first_mp; 28656 boolean_t xmit_drop = B_FALSE; 28657 ip_proc_t proc; 28658 ill_t *out_ill; 28659 28660 arpce = ire->ire_nce; 28661 ASSERT(arpce != NULL); 28662 28663 DTRACE_PROBE2(ip__xmit__v4, ire_t *, ire, nce_t *, arpce); 28664 28665 mutex_enter(&arpce->nce_lock); 28666 switch (arpce->nce_state) { 28667 case ND_REACHABLE: 28668 /* If there are other queued packets, queue this packet */ 28669 if (arpce->nce_qd_mp != NULL) { 28670 if (mp != NULL) 28671 nce_queue_mp_common(arpce, mp, B_FALSE); 28672 mp = arpce->nce_qd_mp; 28673 } 28674 arpce->nce_qd_mp = NULL; 28675 mutex_exit(&arpce->nce_lock); 28676 28677 /* 28678 * Flush the queue. In the common case, where the 28679 * ARP is already resolved, it will go through the 28680 * while loop only once. 28681 */ 28682 while (mp != NULL) { 28683 28684 nxt_mp = mp->b_next; 28685 mp->b_next = NULL; 28686 /* 28687 * This info is needed for IPQOS to do COS marking 28688 * in ip_wput_attach_llhdr->ip_process. 28689 */ 28690 proc = (ip_proc_t)(uintptr_t)mp->b_prev; 28691 mp->b_prev = NULL; 28692 28693 /* set up ill index for outbound qos processing */ 28694 out_ill = ire->ire_ipif->ipif_ill; 28695 ill_index = out_ill->ill_phyint->phyint_ifindex; 28696 first_mp = ip_wput_attach_llhdr(mp, ire, proc, 28697 ill_index); 28698 if (first_mp == NULL) { 28699 xmit_drop = B_TRUE; 28700 if (proc == IPP_FWD_OUT) { 28701 BUMP_MIB(&ip_mib, ipInDiscards); 28702 } else { 28703 BUMP_MIB(&ip_mib, ipOutDiscards); 28704 } 28705 goto next_mp; 28706 } 28707 /* non-ipsec hw accel case */ 28708 if (io == NULL || !io->ipsec_out_accelerated) { 28709 /* send it */ 28710 q = ire->ire_stq; 28711 if (proc == IPP_FWD_OUT) { 28712 UPDATE_IB_PKT_COUNT(ire); 28713 } else { 28714 UPDATE_OB_PKT_COUNT(ire); 28715 } 28716 ire->ire_last_used_time = lbolt; 28717 28718 if (flow_ctl_enabled || canputnext(q)) { 28719 if (proc == IPP_FWD_OUT) { 28720 BUMP_MIB(&ip_mib, 28721 ipForwDatagrams); 28722 } 28723 28724 if (mp == NULL) 28725 goto next_mp; 28726 putnext(q, first_mp); 28727 } else { 28728 BUMP_MIB(&ip_mib, 28729 ipOutDiscards); 28730 xmit_drop = B_TRUE; 28731 freemsg(first_mp); 28732 } 28733 } else { 28734 /* 28735 * Safety Pup says: make sure this 28736 * is going to the right interface! 28737 */ 28738 ill_t *ill1 = 28739 (ill_t *)ire->ire_stq->q_ptr; 28740 int ifindex = 28741 ill1->ill_phyint->phyint_ifindex; 28742 if (ifindex != 28743 io->ipsec_out_capab_ill_index) { 28744 xmit_drop = B_TRUE; 28745 freemsg(mp); 28746 } else { 28747 ipsec_hw_putnext(ire->ire_stq, 28748 mp); 28749 } 28750 } 28751 next_mp: 28752 mp = nxt_mp; 28753 } /* while (mp != NULL) */ 28754 if (xmit_drop) 28755 return (SEND_FAILED); 28756 else 28757 return (SEND_PASSED); 28758 28759 case ND_INITIAL: 28760 case ND_INCOMPLETE: 28761 28762 /* 28763 * While we do send off packets to dests that 28764 * use fully-resolved CGTP routes, we do not 28765 * handle unresolved CGTP routes. 28766 */ 28767 ASSERT(!(ire->ire_flags & RTF_MULTIRT)); 28768 ASSERT(io == NULL || !io->ipsec_out_accelerated); 28769 28770 if (mp != NULL) { 28771 /* queue the packet */ 28772 nce_queue_mp_common(arpce, mp, B_FALSE); 28773 } 28774 28775 if (arpce->nce_state == ND_INCOMPLETE) { 28776 mutex_exit(&arpce->nce_lock); 28777 DTRACE_PROBE3(ip__xmit__incomplete, 28778 (ire_t *), ire, (mblk_t *), mp, 28779 (ipsec_out_t *), io); 28780 return (LOOKUP_IN_PROGRESS); 28781 } 28782 28783 arpce->nce_state = ND_INCOMPLETE; 28784 mutex_exit(&arpce->nce_lock); 28785 /* 28786 * Note that ire_add() (called from ire_forward()) 28787 * holds a ref on the ire until ARP is completed. 28788 */ 28789 28790 ire_arpresolve(ire, ire_to_ill(ire)); 28791 return (LOOKUP_IN_PROGRESS); 28792 default: 28793 ASSERT(0); 28794 mutex_exit(&arpce->nce_lock); 28795 return (LLHDR_RESLV_FAILED); 28796 } 28797 } 28798 28799 /* 28800 * Return B_TRUE if the buffers differ in length or content. 28801 * This is used for comparing extension header buffers. 28802 * Note that an extension header would be declared different 28803 * even if all that changed was the next header value in that header i.e. 28804 * what really changed is the next extension header. 28805 */ 28806 boolean_t 28807 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 28808 uint_t blen) 28809 { 28810 if (!b_valid) 28811 blen = 0; 28812 28813 if (alen != blen) 28814 return (B_TRUE); 28815 if (alen == 0) 28816 return (B_FALSE); /* Both zero length */ 28817 return (bcmp(abuf, bbuf, alen)); 28818 } 28819 28820 /* 28821 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 28822 * Return B_FALSE if memory allocation fails - don't change any state! 28823 */ 28824 boolean_t 28825 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 28826 const void *src, uint_t srclen) 28827 { 28828 void *dst; 28829 28830 if (!src_valid) 28831 srclen = 0; 28832 28833 ASSERT(*dstlenp == 0); 28834 if (src != NULL && srclen != 0) { 28835 dst = mi_alloc(srclen, BPRI_MED); 28836 if (dst == NULL) 28837 return (B_FALSE); 28838 } else { 28839 dst = NULL; 28840 } 28841 if (*dstp != NULL) 28842 mi_free(*dstp); 28843 *dstp = dst; 28844 *dstlenp = dst == NULL ? 0 : srclen; 28845 return (B_TRUE); 28846 } 28847 28848 /* 28849 * Replace what is in *dst, *dstlen with the source. 28850 * Assumes ip_allocbuf has already been called. 28851 */ 28852 void 28853 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 28854 const void *src, uint_t srclen) 28855 { 28856 if (!src_valid) 28857 srclen = 0; 28858 28859 ASSERT(*dstlenp == srclen); 28860 if (src != NULL && srclen != 0) 28861 bcopy(src, *dstp, srclen); 28862 } 28863 28864 /* 28865 * Free the storage pointed to by the members of an ip6_pkt_t. 28866 */ 28867 void 28868 ip6_pkt_free(ip6_pkt_t *ipp) 28869 { 28870 ASSERT(ipp->ipp_pathmtu == NULL && !(ipp->ipp_fields & IPPF_PATHMTU)); 28871 28872 if (ipp->ipp_fields & IPPF_HOPOPTS) { 28873 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 28874 ipp->ipp_hopopts = NULL; 28875 ipp->ipp_hopoptslen = 0; 28876 } 28877 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 28878 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 28879 ipp->ipp_rtdstopts = NULL; 28880 ipp->ipp_rtdstoptslen = 0; 28881 } 28882 if (ipp->ipp_fields & IPPF_DSTOPTS) { 28883 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 28884 ipp->ipp_dstopts = NULL; 28885 ipp->ipp_dstoptslen = 0; 28886 } 28887 if (ipp->ipp_fields & IPPF_RTHDR) { 28888 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 28889 ipp->ipp_rthdr = NULL; 28890 ipp->ipp_rthdrlen = 0; 28891 } 28892 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 28893 IPPF_RTHDR); 28894 } 28895